Merge layered exec environments into project decision work
Why: Integrate the exact Dev main pin while preserving trusted project context, vision routing, work-result context, and distinct migration identities.
Evidence: go vet ./...; go test ./... -count=1; go build ./...; CGO_ENABLED=0 go build ./cmd/agent-system; isolated PostgreSQL migration history tests.
Impact: Keep exec_env_layers at schema v35, move the feature migrations to v36-v46 in order, and fail closed on the old unpublished numbering.
Refs: 14e780c6fa32fdd4742b6232150c5efcb0d766ff; ab12fea474
This commit is contained in:
@@ -0,0 +1,146 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"flag"
|
||||
"fmt"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"agent-system-backend/internal/execenv"
|
||||
)
|
||||
|
||||
func runEnv(args []string) int {
|
||||
if len(args) == 0 {
|
||||
fmt.Fprintln(os.Stderr, "用法:agent-system env <ensure|lock|gc|doctor|rebase|python> --root <dir>")
|
||||
return 2
|
||||
}
|
||||
sub := args[0]
|
||||
fs := flag.NewFlagSet("env "+sub, flag.ExitOnError)
|
||||
rootPath := fs.String("root", "", "环境根目录")
|
||||
ttl := fs.Duration("ttl", 7*24*time.Hour, "gc 保留窗口")
|
||||
bin := fs.String("bin", "", "python link 要指向的解释器")
|
||||
_ = fs.Parse(args[1:])
|
||||
if *rootPath == "" {
|
||||
fmt.Fprintln(os.Stderr, "env: --root 必填")
|
||||
return 2
|
||||
}
|
||||
root, err := execenv.Open(*rootPath)
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, "env:", err)
|
||||
return 1
|
||||
}
|
||||
ctx := context.Background()
|
||||
switch sub {
|
||||
case "ensure":
|
||||
py, err := root.EnsurePython(ctx)
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, "env ensure:", err)
|
||||
return 1
|
||||
}
|
||||
id, err := execenv.NewBroker(root, nil, execenv.Pip{Python: py}).EnsureBase(ctx)
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, "env ensure:", err)
|
||||
return 1
|
||||
}
|
||||
fmt.Println(id)
|
||||
return runDoctor(ctx, root, py, true)
|
||||
case "lock":
|
||||
id, err := root.CurrentBase()
|
||||
if err != nil || id == "" {
|
||||
fmt.Fprintln(os.Stderr, "env lock: 没有 current 基础层")
|
||||
return 1
|
||||
}
|
||||
lock, err := root.ReadLock(id)
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, "env lock:", err)
|
||||
return 1
|
||||
}
|
||||
enc := json.NewEncoder(os.Stdout)
|
||||
enc.SetIndent("", " ")
|
||||
if err := enc.Encode(lock); err != nil {
|
||||
fmt.Fprintln(os.Stderr, "env lock:", err)
|
||||
return 1
|
||||
}
|
||||
return 0
|
||||
case "gc":
|
||||
removed, err := execenv.NewBroker(root, nil, execenv.Pip{}).GC(*ttl)
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, "env gc:", err)
|
||||
return 1
|
||||
}
|
||||
for _, id := range removed {
|
||||
fmt.Println(id)
|
||||
}
|
||||
return 0
|
||||
case "doctor":
|
||||
py, ok := root.PythonBin()
|
||||
if !ok {
|
||||
py = "/usr/bin/python3"
|
||||
}
|
||||
return runDoctor(ctx, root, py, true)
|
||||
case "rebase":
|
||||
py, err := root.EnsurePython(ctx)
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, "env rebase:", err)
|
||||
return 1
|
||||
}
|
||||
id, err := execenv.NewBroker(root, nil, execenv.Pip{Python: py}).Rebase(ctx)
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, "env rebase:", err)
|
||||
return 1
|
||||
}
|
||||
fmt.Println(id)
|
||||
return 0
|
||||
case "python":
|
||||
action := "install"
|
||||
if fs.NArg() > 0 {
|
||||
action = fs.Arg(0)
|
||||
}
|
||||
switch action {
|
||||
case "install":
|
||||
py, err := root.EnsurePython(ctx)
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, "env python:", err)
|
||||
return 1
|
||||
}
|
||||
fmt.Println(py)
|
||||
return 0
|
||||
case "link":
|
||||
if *bin == "" {
|
||||
fmt.Fprintln(os.Stderr, "env python link: --bin 必填")
|
||||
return 2
|
||||
}
|
||||
if err := root.LinkPython(*bin); err != nil {
|
||||
fmt.Fprintln(os.Stderr, "env python link:", err)
|
||||
return 1
|
||||
}
|
||||
fmt.Println(*bin)
|
||||
return 0
|
||||
default:
|
||||
fmt.Fprintln(os.Stderr, "env python: install | link")
|
||||
return 2
|
||||
}
|
||||
default:
|
||||
fmt.Fprintf(os.Stderr, "env: 未知动作 %s\n", sub)
|
||||
return 2
|
||||
}
|
||||
}
|
||||
|
||||
func runDoctor(ctx context.Context, root *execenv.Root, py string, sandbox bool) int {
|
||||
var pyPath []string
|
||||
if id, err := root.CurrentBase(); err == nil && id != "" {
|
||||
pyPath = append(pyPath, root.LayerDir(id))
|
||||
}
|
||||
mounts := append([]string{}, pyPath...)
|
||||
if prefix := root.PythonPrefix(); prefix != "" {
|
||||
mounts = append(mounts, prefix)
|
||||
}
|
||||
if err := execenv.Doctor(ctx, py, pyPath, nil, sandbox, mounts); err != nil {
|
||||
fmt.Fprintln(os.Stderr, "env doctor:", err)
|
||||
return 1
|
||||
}
|
||||
fmt.Println("doctor ok")
|
||||
return 0
|
||||
}
|
||||
@@ -46,9 +46,11 @@ func main() {
|
||||
os.Exit(runIdentity(os.Args[2:]))
|
||||
case "capabilities":
|
||||
os.Exit(runCapabilities(os.Args[2:]))
|
||||
case "env":
|
||||
os.Exit(runEnv(os.Args[2:]))
|
||||
default:
|
||||
fmt.Fprintf(os.Stderr, "agent-system: 未知子命令 %s\n", os.Args[1])
|
||||
fmt.Fprintln(os.Stderr, "用法:agent-system <serve|version|identity|user|apikey|config|capabilities> [参数]")
|
||||
fmt.Fprintln(os.Stderr, "用法:agent-system <serve|version|identity|user|apikey|config|capabilities|env> [参数]")
|
||||
os.Exit(2)
|
||||
}
|
||||
}
|
||||
@@ -68,6 +70,7 @@ func main() {
|
||||
execBackend := fs.String("exec-backend", "", "执行后端:direct(默认,现状直启)| sandbox(bwrap 隔离档:断网、只读系统根、仅会话目录可写、进程数限额)")
|
||||
execDepsDir := fs.String("exec-deps-dir", "", "sandbox 档只读依赖层目录(site-packages 等;空=不挂载)")
|
||||
execWorkspace := fs.String("exec-workspace", "", "sandbox 档工作区模式:空=直写会话目录 | overlay=冻结基底+私有 upper+CAS 回收(S0)")
|
||||
execEnvRoot := fs.String("exec-env-root", "", "分层 Python 环境根(解释器、基础层、扩展层;空=不启用)")
|
||||
tier := fs.String("tier", "", "部署档:空=auto(DSN→PG,否则 SQLite)| local(SQLite,不允许 DSN)| team(PG fail-closed:缺 DSN/未认证拒绝启动,PG 故障拒绝就绪)")
|
||||
pgMaxOpen := fs.Int("pg-max-open", 0, "PG 最大打开连接(0=默认 16)")
|
||||
pgMaxIdle := fs.Int("pg-max-idle", 0, "PG 最大空闲连接(0=默认 8)")
|
||||
@@ -93,7 +96,7 @@ func main() {
|
||||
Addr: *addr, DataDir: *data, PluginsDir: *pluginsDir, Mock: *mock,
|
||||
StorageDSN: *dsn, ArtifactsDir: *artifacts, AuthMode: *auth, Instance: *instance,
|
||||
InsecureListen: *insecure, AllowedHostsRaw: *allowedHosts, DisableExecTools: *noExec,
|
||||
ExecBackend: *execBackend, ExecDepsDir: *execDepsDir, ExecWorkspace: *execWorkspace,
|
||||
ExecBackend: *execBackend, ExecDepsDir: *execDepsDir, ExecWorkspace: *execWorkspace, ExecEnvRoot: *execEnvRoot,
|
||||
Tier: *tier, PGMaxOpen: *pgMaxOpen, PGMaxIdle: *pgMaxIdle,
|
||||
PGConnMaxIdleSec: *pgIdleSec, PGConnMaxLifetimeSec: *pgLifetimeSec, PGQueryTimeoutSec: *pgQueryTimeoutSec,
|
||||
}, cliSet)
|
||||
@@ -137,8 +140,8 @@ func main() {
|
||||
DisableUploadIndexing: extOpt.DisableUploadIndexing,
|
||||
DisableConvCardMirror: extOpt.DisableConvCardMirror,
|
||||
DisableExecTools: extOpt.DisableExecTools, ExecBackend: extOpt.ExecBackend, ExecDepsDir: resolved.ExecDepsDir,
|
||||
ExecWorkspace: resolved.ExecWorkspace,
|
||||
Tier: resolved.Tier,
|
||||
ExecWorkspace: resolved.ExecWorkspace, ExecEnvRoot: resolved.ExecEnvRoot,
|
||||
Tier: resolved.Tier,
|
||||
PGPool: store.PoolOptions{
|
||||
MaxOpenConn: resolved.PGMaxOpen, MaxIdleConn: resolved.PGMaxIdle,
|
||||
ConnMaxIdleTimeSec: resolved.PGConnMaxIdleSec, ConnMaxLifetimeSec: resolved.PGConnMaxLifetimeSec,
|
||||
@@ -162,6 +165,14 @@ func visited(fs *flag.FlagSet) map[string]bool {
|
||||
name = "allowedHosts"
|
||||
case "disable-exec-tools":
|
||||
name = "disableExecTools"
|
||||
case "exec-backend":
|
||||
name = "execBackend"
|
||||
case "exec-deps-dir":
|
||||
name = "execDepsDir"
|
||||
case "exec-workspace":
|
||||
name = "execWorkspace"
|
||||
case "exec-env-root":
|
||||
name = "execEnvRoot"
|
||||
case "pg-max-open":
|
||||
name = "pgMaxOpen"
|
||||
case "pg-max-idle":
|
||||
@@ -200,6 +211,7 @@ func runConfig(args []string) int {
|
||||
execBackend := fs.String("exec-backend", "", "")
|
||||
execDepsDir := fs.String("exec-deps-dir", "", "")
|
||||
execWorkspace := fs.String("exec-workspace", "", "")
|
||||
execEnvRoot := fs.String("exec-env-root", "", "")
|
||||
tier := fs.String("tier", "", "")
|
||||
pgMaxOpen := fs.Int("pg-max-open", 0, "")
|
||||
pgMaxIdle := fs.Int("pg-max-idle", 0, "")
|
||||
@@ -211,7 +223,7 @@ func runConfig(args []string) int {
|
||||
Addr: *addr, DataDir: *data, PluginsDir: *pluginsDir, Mock: *mock,
|
||||
StorageDSN: *dsn, ArtifactsDir: *artifacts, AuthMode: *auth, Instance: *instance,
|
||||
InsecureListen: *insecure, AllowedHostsRaw: *allowedHosts, DisableExecTools: *noExec,
|
||||
ExecBackend: *execBackend, ExecDepsDir: *execDepsDir, ExecWorkspace: *execWorkspace,
|
||||
ExecBackend: *execBackend, ExecDepsDir: *execDepsDir, ExecWorkspace: *execWorkspace, ExecEnvRoot: *execEnvRoot,
|
||||
Tier: *tier, PGMaxOpen: *pgMaxOpen, PGMaxIdle: *pgMaxIdle,
|
||||
PGConnMaxIdleSec: *pgIdleSec, PGConnMaxLifetimeSec: *pgLifetimeSec, PGQueryTimeoutSec: *pgQueryTimeoutSec,
|
||||
}, visited(fs))
|
||||
|
||||
@@ -301,6 +301,8 @@ var copyOrder = []string{
|
||||
"account_events",
|
||||
"redeem_attempts",
|
||||
"recovery_proposals", "recovery_proposal_proofs",
|
||||
// v35 分层 Python 环境(无外键;申请与会话绑定要随库迁走)
|
||||
"exec_env_requests", "exec_env_bindings",
|
||||
}
|
||||
|
||||
const controlDDL = `
|
||||
@@ -432,9 +434,8 @@ func validateSourceMigrationLedger(src *sql.DB) error {
|
||||
if err := rows.Scan(&version, &name); err != nil {
|
||||
return err
|
||||
}
|
||||
expected, known := store.MigrationName(version)
|
||||
if !known || expected != name {
|
||||
return fmt.Errorf("source migration identity mismatch at v%d; refuse to reinterpret another schema", version)
|
||||
if err := store.ValidateMigrationIdentity(version, name); err != nil {
|
||||
return fmt.Errorf("source %w", err)
|
||||
}
|
||||
}
|
||||
return rows.Err()
|
||||
|
||||
@@ -36,13 +36,13 @@ func TestMigrateRejectsSourceLedgerNameCollisionBeforeTargetOpen(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := db.Exec(`UPDATE schema_migrations SET name='work_task_bindings' WHERE version=31`); err != nil {
|
||||
if _, err := db.Exec(`UPDATE schema_migrations SET name='work_task_bindings' WHERE version=35`); err != nil {
|
||||
db.Close()
|
||||
t.Fatal(err)
|
||||
}
|
||||
db.Close()
|
||||
err = cmdMigrate([]string{"--source", source, "--target", "invalid://target", "--migration-id", "name-collision"})
|
||||
if err == nil || !strings.Contains(err.Error(), "source migration identity mismatch") {
|
||||
if err == nil || !strings.Contains(err.Error(), "source migration identity mismatch") || !strings.Contains(err.Error(), "legacy feature migration numbering") {
|
||||
t.Fatalf("candidate branch ledger was not rejected before opening target: %v", err)
|
||||
}
|
||||
}
|
||||
@@ -231,7 +231,7 @@ func TestBackupAndMigrateRejectCandidateRawContextScope(t *testing.T) {
|
||||
if _, err := db.Exec(`ALTER TABLE context_manifests ADD COLUMN resolve_target_id TEXT NOT NULL DEFAULT ''`); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := db.Exec(`DELETE FROM schema_migrations WHERE version=39`); err != nil {
|
||||
if _, err := db.Exec(`DELETE FROM schema_migrations WHERE version=40`); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := db.Exec(`INSERT INTO context_manifests(id,attempt_id,owner,target_id,project_id,input_sha256,delivery,manifest_json,created_at,scope_bound,resolve_goal,resolve_target_id) VALUES('candidate-ctx','candidate-attempt','alice','r1','p1','input-hash','attempted','{}','2026-10-14T00:00:00Z',1,'OLD_RESTRICTED_BODY','doc-private')`); err != nil {
|
||||
@@ -315,7 +315,7 @@ func TestBackupAndMigrateRejectPendingOrMissingContextScrubMarker(t *testing.T)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := db.Exec(`DELETE FROM schema_migrations WHERE version=39`); err != nil {
|
||||
if _, err := db.Exec(`DELETE FROM schema_migrations WHERE version=40`); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := db.Close(); err != nil {
|
||||
|
||||
Reference in New Issue
Block a user