From c8dd169a1fdc1451fab3b475c14f9636a7f5bf6c Mon Sep 17 00:00:00 2001 From: weichen Date: Wed, 14 Oct 2026 03:42:28 +0000 Subject: [PATCH 01/15] Add scoped project command audit read slice Why: Project audit cannot use owner-wide logs or current conversation project state to establish trusted attribution. Evidence: RED cases were observed locally; go test ./pkg/extension ./internal/store ./internal/app -count=1 passed on this tree (SELF). Impact: Add optional X2 read and audit.read callback, immutable project/name snapshots, safe owner-only projection, and explicit unsupported coverage. Cross-member publication remains pending. Refs: Stage 5 W9; Stage 6 X2 public extension request --- .task/feature-rdm-two-trees-project-audit.md | 20 +++ internal/app/app.go | 2 +- internal/app/hostcaps.go | 13 +- internal/app/project_audit.go | 103 +++++++++++ internal/app/project_audit_test.go | 101 +++++++++++ internal/store/audit.go | 63 ++++++- internal/store/migrate.go | 9 + internal/store/migrate_compat.go | 1 + internal/store/project_audit_test.go | 174 +++++++++++++++++++ pkg/extension/extension.go | 4 +- pkg/extension/project_audit.go | 75 ++++++++ pkg/extension/surface.txt | 11 ++ 12 files changed, 565 insertions(+), 11 deletions(-) create mode 100644 .task/feature-rdm-two-trees-project-audit.md create mode 100644 internal/app/project_audit.go create mode 100644 internal/app/project_audit_test.go create mode 100644 internal/store/project_audit_test.go create mode 100644 pkg/extension/project_audit.go diff --git a/.task/feature-rdm-two-trees-project-audit.md b/.task/feature-rdm-two-trees-project-audit.md new file mode 100644 index 0000000..06329d8 --- /dev/null +++ b/.task/feature-rdm-two-trees-project-audit.md @@ -0,0 +1,20 @@ +# W9 Backend project audit read slice + +- Scope: TT-A01/A02/A04/U04, approved stage 5 W9. Stage 6 local implementation; human review, formal acceptance, PR, CI, tag, and deployment are pending. +- Branch/worktree: `feature/rdm-two-trees-project-audit` at `/home/weichen/projects/Agent-System-Backend-wt-feature-rdm-two-trees-project-audit`. +- R0: Backend authoritative `origin/main` `5d5fc9891c0eb8994545ccb8b740535d9b99fb0e`; main was clean and equal to the fetched remote before this worktree was created. Core gitlink in this worktree is `5773e74086791731059730bd3726dc8c9cd1381f`. +- Startup receipt: `/tmp/backend-w9-precheck-20261014-01.json` (SELF, exit 2, `SYNC_REQUIRED` for the separate Dev checkout `feature/rdm-increment-acceptance-note`, six commits behind Dev `origin/main`). This Backend task uses the independent-development path in Backend `AGENTS.md`; it does not write that Dev checkout or its task card. The receipt is preserved as a failure, not relabeled READY. + +## Implemented local slice + +- Optional `ProjectAuditHost` and domain `audit.read` registration. A different plugin, absent registration, or unbound actor cannot read. +- Store write snapshots the project from the owner-matched conversation; query requires the authenticated actor's owner and that immutable project. This deliberately excludes other members' private conversations and unbound historical rows, and does not move old receipts when a conversation changes projects. +- Command audit stores the current verified account display name at write time; old rows expose `not-recorded`. Public receipts omit raw payload and parameter digest. Cursor uses project-and-owner scoped source cutoff. +- Coverage explicitly marks cross-member published operations, legacy project attribution, run relations, model/background receipts, and resource bodies unsupported. This is not the full W9 or complete project audit. + +## Verification and remaining work + +- The session saw RED for the missing store and host interfaces, unscoped cutoff, cross-member private disclosure, and conversation-project reassignment before the respective fixes. These RED outputs were not captured by `make evidence`, so this card does not use them as formal evidence. +- SELF receipt `/tmp/backend-w9-go-test-20261014-01.json`: `go test ./...`, exit 0, run on the pre-commit worktree based on R0. +- SELF receipt `/tmp/backend-w9-build-20261014-01.json`: `CGO_ENABLED=0 go build ./cmd/agent-system`, exit 0, run on that pre-commit tree. Generated local binary removed after the build. +- Required next X2 contract: persist trusted operation/run/resource attribution and publication or visibility at source, with a per-object metadata/body disclosure callback and cursor binding to actor, authorization epoch, project, filter, and cutoff. Then test two members reading published project operations while private conversations, revoked objects, and bodies without current rights remain hidden. Current `command_audit` has owner, conversation, raw envelope, and new project/name snapshots, but no proof that an operation was published or that its resource may be disclosed. Add real model/tool/background receipts, delayed receipt reconciliation, history/export checks, and PostgreSQL/combined consumer verification before claiming W9 complete. diff --git a/internal/app/app.go b/internal/app/app.go index 33749b7..8ec4808 100644 --- a/internal/app/app.go +++ b/internal/app/app.go @@ -597,7 +597,7 @@ func Run(opt Options) error { // hostcaps:hostapi 闭包与进程内插件句柄共用的 owner 作用域能力(EXT-1 批注 1)。 authzSlot := &projectAuthz{} projectSlot := &projectLifecycleSlot{} - caps := hostcaps{projects: projectSlot, st: st, pipe: pipe, set: setSvc, authz: authzSlot, surfaces: surfaceSvc} + caps := hostcaps{projects: projectSlot, projectAudit: &projectAuditSlot{}, st: st, pipe: pipe, set: setSvc, authz: authzSlot, surfaces: surfaceSvc} // EXT-13:回合内检索(knowledge_search / @knowledge 注入)与 HTTP/Host 检索面 // 同一适配器授权;槽为共享指针,扩展在 applyCompiled 期间注册后即生效。 mgr.SetSharedKnowledgeAllow(func(owner string, projectID core.ID) bool { diff --git a/internal/app/hostcaps.go b/internal/app/hostcaps.go index 866a4f4..caf3e7a 100644 --- a/internal/app/hostcaps.go +++ b/internal/app/hostcaps.go @@ -21,12 +21,13 @@ import ( // 同一实现同时服务 hostapi.Server 的 Search/Complete 闭包与进程内 // inprocHost(EXT-1 批注 1:复用现有 owner 作用域函数,不新造句柄)。 type hostcaps struct { - projects *projectLifecycleSlot - st *store.Store - pipe *rag.Pipeline - set *settings.Service - authz *projectAuthz // EXT-8 适配器槽(共享指针;首个注册者生效) - surfaces *surfacesvc.Service // EXT-17 Surface 能力槽(nil = 未装配,明确拒绝) + projects *projectLifecycleSlot + projectAudit *projectAuditSlot + st *store.Store + pipe *rag.Pipeline + set *settings.Service + authz *projectAuthz // EXT-8 适配器槽(共享指针;首个注册者生效) + surfaces *surfacesvc.Service // EXT-17 Surface 能力槽(nil = 未装配,明确拒绝) } // projectAuthz EXT-8 资源授权适配器槽:进程内扩展注册,组合根装配进 Server。 diff --git a/internal/app/project_audit.go b/internal/app/project_audit.go new file mode 100644 index 0000000..07562fe --- /dev/null +++ b/internal/app/project_audit.go @@ -0,0 +1,103 @@ +package app + +import ( + "context" + "errors" + "sync" + + "agent-system-backend/pkg/extension" + core "agent-system-core" +) + +// projectAuditSlot binds the audit.read policy to its registering domain plugin. +// A different plugin cannot borrow that policy through its own Host handle. +type projectAuditSlot struct { + mu sync.RWMutex + holder string + check extension.ProjectAuditAuthorizer +} + +func (s *projectAuditSlot) register(pluginID string, check extension.ProjectAuditAuthorizer) error { + if pluginID == "" || check == nil { + return extension.ErrProjectAuditUnsupported + } + s.mu.Lock() + defer s.mu.Unlock() + if s.check != nil { + return errors.New("extension: project audit authorizer already registered") + } + s.holder, s.check = pluginID, check + return nil +} + +func (r compiledRegistry) RegisterProjectAuditAuthorizer(extension.ProjectAuditAuthorizer) error { + return errors.New("extension: declarative plugins cannot register project audit authorizers") +} + +func (r pluginRegistry) RegisterProjectAuditAuthorizer(check extension.ProjectAuditAuthorizer) error { + if r.caps.projectAudit == nil { + return extension.ErrProjectAuditUnsupported + } + return r.caps.projectAudit.register(r.pluginID, check) +} + +func (r recordRegistry) RegisterProjectAuditAuthorizer(check extension.ProjectAuditAuthorizer) error { + reg, ok := r.Registry.(extension.ProjectAuditRegistry) + if !ok { + return extension.ErrProjectAuditUnsupported + } + return reg.RegisterProjectAuditAuthorizer(check) +} + +var _ extension.ProjectAuditHost = inprocHost{} + +func (h inprocHost) QueryProjectAudit(ctx context.Context, q extension.ProjectAuditQuery) (extension.ProjectAuditPage, error) { + if err := ctx.Err(); err != nil { + return extension.ProjectAuditPage{}, err + } + if h.actor == "" || h.actor != h.owner || q.ProjectID == "" { + return extension.ProjectAuditPage{}, extension.ErrProjectAuditDenied + } + if h.caps.projectAudit == nil || h.caps.st == nil { + return extension.ProjectAuditPage{}, extension.ErrProjectAuditUnsupported + } + slot := h.caps.projectAudit + slot.mu.RLock() + holder, check := slot.holder, slot.check + slot.mu.RUnlock() + if holder != h.pluginID || check == nil { + return extension.ProjectAuditPage{}, extension.ErrProjectAuditUnsupported + } + ok, err := check(h.actor, core.ID(q.ProjectID)) + if err != nil || !ok { + return extension.ProjectAuditPage{}, extension.ErrProjectAuditDenied + } + if err := ctx.Err(); err != nil { + return extension.ProjectAuditPage{}, err + } + rows, cutoff, next, err := h.caps.st.ListProjectCommandAudits(h.owner, string(q.ProjectID), q.BeforeOffset, q.CutoffOffset, q.Limit) + if err != nil { + return extension.ProjectAuditPage{}, err + } + page := extension.ProjectAuditPage{ + Receipts: make([]extension.HostAuditReceipt, 0, len(rows)), + NextOffset: next, CutoffOffset: cutoff, + Coverage: extension.ProjectAuditCoverage{ + CommandAudit: "actor-owned-project-bound-only", CrossMember: "unsupported", LegacyProjectBinding: "not-recorded", RunRelations: "unsupported", ModelReceipts: "unsupported", + BackgroundReceipts: "unsupported", ResourceBodies: "unsupported", + }, + } + for _, row := range rows { + nameStatus := "not-recorded" + if row.ActorName != "" { + nameStatus = "recorded" + } + page.Receipts = append(page.Receipts, extension.HostAuditReceipt{ + SourceOffset: row.Offset, Time: row.Envelope.Time, ActorID: row.Envelope.Actor, + ActorName: row.ActorName, ActorNameStatus: nameStatus, + CommandID: row.Envelope.CommandID, State: string(row.Envelope.State), + OperationID: row.Envelope.OperationID, ConversationID: row.Envelope.ConversationID, + }) + } + return page, nil +} diff --git a/internal/app/project_audit_test.go b/internal/app/project_audit_test.go new file mode 100644 index 0000000..4157fa9 --- /dev/null +++ b/internal/app/project_audit_test.go @@ -0,0 +1,101 @@ +package app + +import ( + "context" + "encoding/json" + "errors" + "path/filepath" + "strings" + "testing" + "time" + + agentcommand "agent-command" + "agent-system-backend/internal/store" + "agent-system-backend/pkg/extension" + core "agent-system-core" +) + +func TestProjectAuditHostChecksDomainReadAndProjectsStoredReceipts(t *testing.T) { + st, err := store.OpenSQLite(filepath.Join(t.TempDir(), "audit.db")) + if err != nil { + t.Fatal(err) + } + defer st.Close() + for _, owner := range []string{"alice", "bob"} { + if err := st.EnsureUser(owner, strings.ToUpper(owner)); err != nil { + t.Fatal(err) + } + } + a, err := st.CreateProject("alice", "A") + if err != nil { + t.Fatal(err) + } + b, err := st.CreateProject("alice", "B") + if err != nil { + t.Fatal(err) + } + ac, err := st.CreateConversation("alice", string(a.ID), "A", "") + if err != nil { + t.Fatal(err) + } + bc, err := st.CreateConversation("alice", string(b.ID), "B", "") + if err != nil { + t.Fatal(err) + } + shared, err := st.CreateConversationInProject("bob", string(a.ID), "A shared", "") + if err != nil { + t.Fatal(err) + } + for _, item := range []struct{ owner, conversation, command string }{ + {"alice", string(ac.ID), "a.command"}, + {"alice", string(bc.ID), "b.command"}, + {"bob", string(shared.ID), "shared.command"}, + } { + if err := st.AppendAudit(item.owner, agentcommand.AuditEnvelope{Time: time.Now(), Actor: item.owner, CommandID: item.command, ConversationID: item.conversation, State: agentcommand.StateCommitted, ParamsDigest: "sensitive-digest"}); err != nil { + t.Fatal(err) + } + } + slot := &projectAuditSlot{} + caps := hostcaps{st: st, projectAudit: slot} + reg := pluginRegistry{pluginID: "rdm", caps: caps} + allowed := true + if err := reg.RegisterProjectAuditAuthorizer(func(actor string, projectID core.ID) (bool, error) { + return allowed && actor == "bob" && projectID == a.ID, nil + }); err != nil { + t.Fatal(err) + } + h := inprocHost{caps: caps, owner: "bob", actor: "bob", pluginID: "rdm"} + page, err := h.QueryProjectAudit(context.Background(), extension.ProjectAuditQuery{ProjectID: a.ID, Limit: 10}) + if err != nil { + t.Fatal(err) + } + if len(page.Receipts) != 1 || page.Receipts[0].CommandID != "shared.command" { + t.Fatalf("A page: %+v", page) + } + if page.Coverage.CommandAudit != "actor-owned-project-bound-only" || page.Coverage.CrossMember != "unsupported" || page.Coverage.LegacyProjectBinding != "not-recorded" || page.Coverage.RunRelations != "unsupported" || page.Coverage.ModelReceipts != "unsupported" { + t.Fatalf("coverage: %+v", page.Coverage) + } + if page.Receipts[0].ActorName != "BOB" { + t.Fatalf("actor names: %+v", page.Receipts) + } + raw, err := json.Marshal(page) + if err != nil { + t.Fatal(err) + } + if strings.Contains(string(raw), "sensitive-digest") || strings.Contains(string(raw), "paramsDigest") || strings.Contains(string(raw), "payload") { + t.Fatalf("raw audit payload leaked: %s", raw) + } + if _, err := h.QueryProjectAudit(context.Background(), extension.ProjectAuditQuery{ProjectID: b.ID}); !errors.Is(err, extension.ErrProjectAuditDenied) { + t.Fatalf("project B: %v", err) + } + if _, err := (inprocHost{caps: caps, owner: "bob", actor: "bob", pluginID: "other"}).QueryProjectAudit(context.Background(), extension.ProjectAuditQuery{ProjectID: a.ID}); !errors.Is(err, extension.ErrProjectAuditUnsupported) { + t.Fatalf("other plugin: %v", err) + } + if _, err := (inprocHost{caps: caps, owner: "bob", pluginID: "rdm"}).QueryProjectAudit(context.Background(), extension.ProjectAuditQuery{ProjectID: a.ID}); !errors.Is(err, extension.ErrProjectAuditDenied) { + t.Fatalf("unbound actor: %v", err) + } + allowed = false + if _, err := h.QueryProjectAudit(context.Background(), extension.ProjectAuditQuery{ProjectID: a.ID, BeforeOffset: page.NextOffset, CutoffOffset: page.CutoffOffset}); !errors.Is(err, extension.ErrProjectAuditDenied) { + t.Fatalf("revoked audit.read: %v", err) + } +} diff --git a/internal/store/audit.go b/internal/store/audit.go index b5479a8..d77301a 100644 --- a/internal/store/audit.go +++ b/internal/store/audit.go @@ -3,6 +3,7 @@ package store import ( "database/sql" "encoding/json" + "errors" "time" agentcommand "agent-command" @@ -16,13 +17,69 @@ func (s *Store) AppendAudit(owner string, env agentcommand.AuditEnvelope) error if err != nil { return err } - _, err = s.db.Exec(s.q(`INSERT INTO command_audit (time, command_id, source, entrance, state, operation_id, conversation_id, owner, payload) - VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)`), + _, err = s.db.Exec(s.q(`INSERT INTO command_audit (time, command_id, source, entrance, state, operation_id, conversation_id, owner, payload, actor_name, project_id) + VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, COALESCE((SELECT name FROM users WHERE owner=?), ''), + COALESCE((SELECT project_id FROM conversations WHERE id=? AND owner=?), ''))`), env.Time.UTC().Format(time.RFC3339Nano), env.CommandID, string(env.Source), env.Entrance, string(env.State), - env.OperationID, env.ConversationID, owner, string(raw)) + env.OperationID, env.ConversationID, owner, string(raw), env.Actor, env.ConversationID, owner) return err } +// ProjectCommandAuditRow is an internal command receipt. Callers must project +// only approved fields; the envelope contains a digest and is not a public DTO. +type ProjectCommandAuditRow struct { + Offset int64 + ActorName string // empty on old rows or when no trusted account name was recorded + Envelope agentcommand.AuditEnvelope +} + +// ListProjectCommandAudits reads the project resolved from a trusted conversation +// at append time. Reassigning or deleting that conversation cannot rewrite the +// original project. Legacy rows without this snapshot are excluded: current +// project membership cannot establish their historical project attribution. +// Only the authenticated owner can read this first private slice. +func (s *Store) ListProjectCommandAudits(actorOwner, projectID string, before, cutoff int64, limit int) ([]ProjectCommandAuditRow, int64, int64, error) { + if actorOwner == "" || projectID == "" || before < 0 || cutoff < 0 || (before != 0 && cutoff == 0) { + return nil, 0, 0, errors.New("project audit: invalid scope or cursor") + } + if limit <= 0 || limit > 200 { + limit = 50 + } + if cutoff == 0 { + if err := s.db.QueryRow(s.q(`SELECT COALESCE(MAX(id),0) FROM command_audit + WHERE project_id=? AND owner=?`), projectID, actorOwner).Scan(&cutoff); err != nil { + return nil, 0, 0, err + } + } + rows, err := s.db.Query(s.q(`SELECT id, actor_name, payload FROM command_audit + WHERE project_id=? AND owner=? AND id<=? AND (?=0 OR id Date: Wed, 14 Oct 2026 04:05:10 +0000 Subject: [PATCH 02/15] Add verified project audit links and button actors Why: Owner-only command audit cannot prove shared business commits, and button dispatch omitted its authenticated actor. Evidence: Local go test ./... passed; isolated PostgreSQL cutoff, link, and SQLite-to-PostgreSQL migration receipts are recorded as SELF in the task card. Impact: Add an optional domain-verified metadata link, exact authorized cross-member read, idempotent storage, actor propagation, and honest PostgreSQL cutoff coverage. Broad audit pagination and other sources remain pending. Refs: Stage 5 W9; approved Stage 6 audit-link design adjustment r1 --- .task/feature-rdm-two-trees-project-audit.md | 11 +- cmd/storage-migrate/integration_test.go | 9 + cmd/storage-migrate/main.go | 4 +- cmd/storage-migrate/main_test.go | 6 +- internal/app/app.go | 12 +- internal/app/project_audit.go | 193 ++++++++++++++++++- internal/app/project_audit_link_test.go | 137 +++++++++++++ internal/app/project_audit_test.go | 2 +- internal/app/surface_audit_actor_test.go | 50 +++++ internal/app/surface_dispatch.go | 25 +++ internal/store/migrate.go | 9 + internal/store/migrate_compat.go | 1 + internal/store/project_audit_links.go | 143 ++++++++++++++ internal/store/project_audit_links_test.go | 84 ++++++++ internal/store/project_audit_test.go | 60 ++++++ pkg/extension/extension.go | 4 +- pkg/extension/project_audit.go | 78 +++++++- pkg/extension/surface.txt | 13 ++ 18 files changed, 812 insertions(+), 29 deletions(-) create mode 100644 internal/app/project_audit_link_test.go create mode 100644 internal/app/surface_audit_actor_test.go create mode 100644 internal/app/surface_dispatch.go create mode 100644 internal/store/project_audit_links.go create mode 100644 internal/store/project_audit_links_test.go diff --git a/.task/feature-rdm-two-trees-project-audit.md b/.task/feature-rdm-two-trees-project-audit.md index 06329d8..05a3595 100644 --- a/.task/feature-rdm-two-trees-project-audit.md +++ b/.task/feature-rdm-two-trees-project-audit.md @@ -1,20 +1,25 @@ -# W9 Backend project audit read slice +# W9 Backend project audit candidate - Scope: TT-A01/A02/A04/U04, approved stage 5 W9. Stage 6 local implementation; human review, formal acceptance, PR, CI, tag, and deployment are pending. - Branch/worktree: `feature/rdm-two-trees-project-audit` at `/home/weichen/projects/Agent-System-Backend-wt-feature-rdm-two-trees-project-audit`. - R0: Backend authoritative `origin/main` `5d5fc9891c0eb8994545ccb8b740535d9b99fb0e`; main was clean and equal to the fetched remote before this worktree was created. Core gitlink in this worktree is `5773e74086791731059730bd3726dc8c9cd1381f`. - Startup receipt: `/tmp/backend-w9-precheck-20261014-01.json` (SELF, exit 2, `SYNC_REQUIRED` for the separate Dev checkout `feature/rdm-increment-acceptance-note`, six commits behind Dev `origin/main`). This Backend task uses the independent-development path in Backend `AGENTS.md`; it does not write that Dev checkout or its task card. The receipt is preserved as a failure, not relabeled READY. +- Approved affected design: `rdm-two-trees-stage6-audit-link-design-adjustment-r1.md`, SHA256 `385d9d7570217ddcce9fe60a60c99dc58c9a4875ad790584a724d09a210a6e52`; user approval in the task session covers this local Backend increment, not tag publication or Stage 7 acceptance. ## Implemented local slice - Optional `ProjectAuditHost` and domain `audit.read` registration. A different plugin, absent registration, or unbound actor cannot read. - Store write snapshots the project from the owner-matched conversation; query requires the authenticated actor's owner and that immutable project. This deliberately excludes other members' private conversations and unbound historical rows, and does not move old receipts when a conversation changes projects. - Command audit stores the current verified account display name at write time; old rows expose `not-recorded`. Public receipts omit raw payload and parameter digest. Cursor uses project-and-owner scoped source cutoff. -- Coverage explicitly marks cross-member published operations, legacy project attribution, run relations, model/background receipts, and resource bodies unsupported. This is not the full W9 or complete project audit. +- Initial commit `c8dd169a1fdc1451fab3b475c14f9636a7f5bf6c` exposed only the actor-owned project slice. The next local increment fixes button actor propagation and adds a trusted domain-commit link publisher, verifier callback, exact linked metadata read, current `audit.read` and per-object metadata checks, and durable idempotency. Unlinked personal conversations remain private. +- Exact-source linked reads do not enumerate hidden links or reveal raw payload, private conversation ID, or content. Body disclosure and a broad project link list are not implemented; the public coverage reports the narrower capability. ## Verification and remaining work - The session saw RED for the missing store and host interfaces, unscoped cutoff, cross-member private disclosure, and conversation-project reassignment before the respective fixes. These RED outputs were not captured by `make evidence`, so this card does not use them as formal evidence. - SELF receipt `/tmp/backend-w9-go-test-20261014-01.json`: `go test ./...`, exit 0, run on the pre-commit worktree based on R0. - SELF receipt `/tmp/backend-w9-build-20261014-01.json`: `CGO_ENABLED=0 go build ./cmd/agent-system`, exit 0, run on that pre-commit tree. Generated local binary removed after the build. -- Required next X2 contract: persist trusted operation/run/resource attribution and publication or visibility at source, with a per-object metadata/body disclosure callback and cursor binding to actor, authorization epoch, project, filter, and cutoff. Then test two members reading published project operations while private conversations, revoked objects, and bodies without current rights remain hidden. Current `command_audit` has owner, conversation, raw envelope, and new project/name snapshots, but no proof that an operation was published or that its resource may be disclosed. Add real model/tool/background receipts, delayed receipt reconciliation, history/export checks, and PostgreSQL/combined consumer verification before claiming W9 complete. +- SELF PostgreSQL receipt `/tmp/backend-w9-pg-cutoff-20261014-01.json`: isolated PostgreSQL 18.6 instance on port 55442 reproduced a low-ID transaction committing after a higher visible ID. `/tmp/backend-w9-pg-link-20261014-02.json` verifies the new link table, retry/conflict behavior, and the cutoff counterexample against isolated schemas. Both exited 0. The current PG cutoff means only `offset-upper-bound-only`; it is **not** a fixed historical visibility snapshot or safe incremental cursor. +- SELF PostgreSQL receipt `/tmp/backend-w9-pg-migrate-20261014-01.json`: isolated SQLite→PostgreSQL migration preserved one linked receipt and advanced its target sequence; the same migration ID replayed idempotently. The temporary PG instance was stopped and removed after verification. +- Integration constraint: this branch uses schema v31 (audit attribution) and v32 (links), while the independent W6 Backend branch also has a local v31. Neither branch has consumed the other's unmerged work. After the first branch lands, the later branch owner must sync the new main, renumber its unpublished migrations without rewriting an already released version, and rerun fresh/old SQLite and PG migration plus combined regression on the actual merged source. These isolated tests do not prove W6+W9 compatibility. +- Required next W9 work: add a committed-order waterline or materialize the authorized ID set at the first read, then prove with PG low-ID late commits that history and cross-page incremental reads neither omit nor duplicate operations. The Backend project-event source, actual RDM committed-event verifier/outbox consumer, run ID propagation from command dispatch, model/tool/background receipts, body/detail disclosure, broad authorized list pagination, history/export, and combined product verification remain unfinished. The exact-link slice is not Stage 7 audit acceptance. diff --git a/cmd/storage-migrate/integration_test.go b/cmd/storage-migrate/integration_test.go index d4997c0..57cb9f0 100644 --- a/cmd/storage-migrate/integration_test.go +++ b/cmd/storage-migrate/integration_test.go @@ -85,6 +85,15 @@ func TestMigrateEndToEndAndIdempotent(t *testing.T) { if _, err := db.Exec(`INSERT INTO command_audit (time, command_id, source, entrance, state, operation_id, conversation_id, payload, owner) VALUES ('2026-10-11T03:00:00Z','cmd-2','http','chat','done','op-2','c1','{}','alice')`); err != nil { t.Fatalf("序列推进失败(复制后插入撞主键):%v", err) } + var linkedResource string + if err := db.QueryRow(`SELECT resource_id FROM project_audit_links WHERE host_audit_offset=7 AND source_id='commit-1'`).Scan(&linkedResource); err != nil || linkedResource != "doc-1" { + t.Fatalf("审计链接迁移丢失: resource=%q err=%v", linkedResource, err) + } + var newLinkID int64 + if err := db.QueryRow(`INSERT INTO project_audit_links(host_audit_offset,project_id,owner,source_kind,source_id,operation_id,resource_kind,resource_id,run_id,disclosure_class,idempotency_key,created_at) + VALUES (7,'p1','alice','domain-commit','commit-2','op-1','document','doc-2','','metadata','outbox-2','2026-10-11T03:00:01Z') RETURNING id`).Scan(&newLinkID); err != nil || newLinkID <= 3 { + t.Fatalf("审计链接序列未推进: id=%d err=%v", newLinkID, err) + } // 报告不含秘密值。 var report string if err := db.QueryRow(`SELECT report FROM migration_control WHERE migration_id='mig-e2e'`).Scan(&report); err != nil || strings.Contains(report, "sk-secret-value") { diff --git a/cmd/storage-migrate/main.go b/cmd/storage-migrate/main.go index 7ee9cb2..884ca51 100644 --- a/cmd/storage-migrate/main.go +++ b/cmd/storage-migrate/main.go @@ -284,7 +284,7 @@ var copyOrder = []string{ "projects", "users", "profiles", "settings", "plugins", "conversations", "runs", "messages", "events", "artifacts", "hitl_cards", "plans", "todos", "knowledge", "knowledge_vectors", - "activity_cards", "project_event_shares", "command_audit", "prompt_modules", + "activity_cards", "project_event_shares", "command_audit", "project_audit_links", "prompt_modules", "sessions", "api_keys", "plugin_grants", "plugin_schema", "plugin_kv", "background_tasks", "memory_entries", "audit_events", "pending_confirms", "llm_usage_daily", @@ -784,7 +784,7 @@ func advanceSequences(db *sql.DB, table string) error { // checkOwners:业务行引用的 owner 必须存在于 users(单租户默认 ”/'local' // 除外)。未知 owner 阻塞迁移(U4/R7:不静默带过未知归属)。 func checkOwners(db *sql.DB, src *sql.DB) error { - ownerTables := []string{"projects", "conversations", "runs", "artifacts", "knowledge", "hitl_cards", "profiles", "activity_cards", "project_event_shares", "command_audit"} + ownerTables := []string{"projects", "conversations", "runs", "artifacts", "knowledge", "hitl_cards", "profiles", "activity_cards", "project_event_shares", "command_audit", "project_audit_links"} for _, t := range ownerTables { if !sqliteHasTable(src, t) { continue diff --git a/cmd/storage-migrate/main_test.go b/cmd/storage-migrate/main_test.go index 8731e0e..7071e34 100644 --- a/cmd/storage-migrate/main_test.go +++ b/cmd/storage-migrate/main_test.go @@ -70,8 +70,10 @@ func seedEdgeRows(t *testing.T, st *store.Store) { mustExec(t, st, `INSERT INTO activity_cards (id, project_id, conversation_id, ref, owner, payload, status, pinned, created_at, updated_at, version) VALUES ('ac1', 'p1', 'c1', '', 'alice', '{"title":"卡"}', 'active', 1, '2026-10-11T01:00:00Z', '2026-10-11T01:00:00Z', 2)`) // 命令审计(SERIAL 主键:显式 id 插入)。 - mustExec(t, st, `INSERT INTO command_audit (id, time, command_id, source, entrance, state, operation_id, conversation_id, payload, owner) VALUES - (7, '2026-10-11T01:00:00Z', 'cmd-1', 'http', 'chat', 'done', 'op-1', 'c1', '{"actor":"alice"}', 'alice')`) + mustExec(t, st, `INSERT INTO command_audit (id, time, command_id, source, entrance, state, operation_id, conversation_id, payload, owner, project_id, actor_name) VALUES + (7, '2026-10-11T01:00:00Z', 'cmd-1', 'http', 'chat', 'committed', 'op-1', 'c1', '{"actor":"alice","commandId":"cmd-1","operationId":"op-1","state":"committed","conversationId":"c1"}', 'alice', 'p1', '爱丽丝')`) + mustExec(t, st, `INSERT INTO project_audit_links (id,host_audit_offset,project_id,owner,source_kind,source_id,operation_id,resource_kind,resource_id,run_id,disclosure_class,idempotency_key,created_at) VALUES + (3,7,'p1','alice','domain-commit','commit-1','op-1','document','doc-1','','metadata','outbox-1','2026-10-11T01:00:01Z')`) // 设置('' 默认租户 scope)。 mustExec(t, st, `INSERT INTO settings (owner, scope, "values") VALUES ('', 'runtime', '{"rateLimitPerMin":0}')`) // 秘密字段(值不外泄:迁移按行复制,报告无正文)。 diff --git a/internal/app/app.go b/internal/app/app.go index 8ec4808..57fbe00 100644 --- a/internal/app/app.go +++ b/internal/app/app.go @@ -557,17 +557,7 @@ func Run(opt Options) error { // U5:页面动作分发口——surfacesvc.SubmitAction 经命令总线四入口同源分发 //(审计信封、参数校验、DangerHigh 内联确认卡全复用;Confirmed 不透传)。 surfaceSvc.SetDispatcher(func(ctx context.Context, d surfacesvc.ActionDispatch) (surfacesvc.ActionOutcome, error) { - resp := bus.Dispatch(ctx, cmdbus.Request{ID: d.CommandID, Params: d.Params, - Entrance: "button", Owner: d.Owner, ConversationID: d.ConversationID}) - state := "committed" - if resp.Receipt.State != "" { - state = string(resp.Receipt.State) - } - cardID := "" - if resp.Card != nil { - cardID = string(resp.Card.ID) - } - return surfacesvc.ActionOutcome{State: state, CardID: cardID, Receipt: resp.Receipt, Payload: resp.Payload}, nil + return dispatchSurfaceAction(ctx, bus, d) }) // U3:generative-ui 工具注入闸——activation 台账关闭时命令不投影给该 owner // 的模型(禁用即不注入能力;执行端守卫仍在 surfacesvc)。 diff --git a/internal/app/project_audit.go b/internal/app/project_audit.go index 07562fe..53be92d 100644 --- a/internal/app/project_audit.go +++ b/internal/app/project_audit.go @@ -5,6 +5,7 @@ import ( "errors" "sync" + "agent-system-backend/internal/store" "agent-system-backend/pkg/extension" core "agent-system-core" ) @@ -12,9 +13,11 @@ import ( // projectAuditSlot binds the audit.read policy to its registering domain plugin. // A different plugin cannot borrow that policy through its own Host handle. type projectAuditSlot struct { - mu sync.RWMutex - holder string - check extension.ProjectAuditAuthorizer + mu sync.RWMutex + holder string + check extension.ProjectAuditAuthorizer + verifier extension.ProjectAuditLinkVerifier + disclose extension.ProjectAuditDisclosureAuthorizer } func (s *projectAuditSlot) register(pluginID string, check extension.ProjectAuditAuthorizer) error { @@ -23,13 +26,39 @@ func (s *projectAuditSlot) register(pluginID string, check extension.ProjectAudi } s.mu.Lock() defer s.mu.Unlock() - if s.check != nil { + if s.check != nil || (s.holder != "" && s.holder != pluginID) { return errors.New("extension: project audit authorizer already registered") } s.holder, s.check = pluginID, check return nil } +func (s *projectAuditSlot) registerVerifier(pluginID string, verify extension.ProjectAuditLinkVerifier) error { + if pluginID == "" || verify == nil { + return extension.ErrProjectAuditUnsupported + } + s.mu.Lock() + defer s.mu.Unlock() + if s.verifier != nil || (s.holder != "" && s.holder != pluginID) { + return errors.New("extension: project audit verifier already registered") + } + s.holder, s.verifier = pluginID, verify + return nil +} + +func (s *projectAuditSlot) registerDisclosure(pluginID string, disclose extension.ProjectAuditDisclosureAuthorizer) error { + if pluginID == "" || disclose == nil { + return extension.ErrProjectAuditUnsupported + } + s.mu.Lock() + defer s.mu.Unlock() + if s.disclose != nil || (s.holder != "" && s.holder != pluginID) { + return errors.New("extension: project audit disclosure authorizer already registered") + } + s.holder, s.disclose = pluginID, disclose + return nil +} + func (r compiledRegistry) RegisterProjectAuditAuthorizer(extension.ProjectAuditAuthorizer) error { return errors.New("extension: declarative plugins cannot register project audit authorizers") } @@ -49,7 +78,111 @@ func (r recordRegistry) RegisterProjectAuditAuthorizer(check extension.ProjectAu return reg.RegisterProjectAuditAuthorizer(check) } +func (r compiledRegistry) RegisterProjectAuditLinkVerifier(extension.ProjectAuditLinkVerifier) error { + return errors.New("extension: declarative plugins cannot register project audit link verifiers") +} +func (r compiledRegistry) RegisterProjectAuditDisclosureAuthorizer(extension.ProjectAuditDisclosureAuthorizer) error { + return errors.New("extension: declarative plugins cannot register project audit disclosure authorizers") +} +func (r pluginRegistry) RegisterProjectAuditLinkVerifier(verify extension.ProjectAuditLinkVerifier) error { + if r.caps.projectAudit == nil { + return extension.ErrProjectAuditUnsupported + } + return r.caps.projectAudit.registerVerifier(r.pluginID, verify) +} +func (r pluginRegistry) RegisterProjectAuditDisclosureAuthorizer(disclose extension.ProjectAuditDisclosureAuthorizer) error { + if r.caps.projectAudit == nil { + return extension.ErrProjectAuditUnsupported + } + return r.caps.projectAudit.registerDisclosure(r.pluginID, disclose) +} +func (r recordRegistry) RegisterProjectAuditLinkVerifier(verify extension.ProjectAuditLinkVerifier) error { + reg, ok := r.Registry.(extension.ProjectAuditLinkRegistry) + if !ok { + return extension.ErrProjectAuditUnsupported + } + return reg.RegisterProjectAuditLinkVerifier(verify) +} +func (r recordRegistry) RegisterProjectAuditDisclosureAuthorizer(disclose extension.ProjectAuditDisclosureAuthorizer) error { + reg, ok := r.Registry.(extension.ProjectAuditLinkRegistry) + if !ok { + return extension.ErrProjectAuditUnsupported + } + return reg.RegisterProjectAuditDisclosureAuthorizer(disclose) +} + var _ extension.ProjectAuditHost = inprocHost{} +var _ extension.ProjectAuditPublisher = inprocHost{} + +func (h inprocHost) PublishProjectAuditLink(ctx context.Context, req extension.ProjectAuditLinkRequest) (extension.ProjectAuditLinkReceipt, error) { + if err := ctx.Err(); err != nil { + return extension.ProjectAuditLinkReceipt{}, err + } + if h.caps.projectAudit == nil || h.caps.st == nil { + return extension.ProjectAuditLinkReceipt{}, extension.ErrProjectAuditUnsupported + } + slot := h.caps.projectAudit + slot.mu.RLock() + holder, verify := slot.holder, slot.verifier + slot.mu.RUnlock() + if holder != h.pluginID || verify == nil { + return extension.ProjectAuditLinkReceipt{}, extension.ErrProjectAuditUnsupported + } + if h.owner == "" || (h.actor != "" && h.actor != h.owner) || req.HostAuditOffset <= 0 || req.ProjectID == "" || req.SourceKind != "domain-commit" || req.SourceID == "" || req.OperationID == "" || req.ResourceKind == "" || req.ResourceID == "" || req.DisclosureClass != "metadata" || req.IdempotencyKey == "" { + return extension.ProjectAuditLinkReceipt{}, extension.ErrProjectAuditDenied + } + row, projectID, err := h.caps.st.CommandAuditForLink(h.owner, req.HostAuditOffset) + if errors.Is(err, store.ErrNotFound) { + return extension.ProjectAuditLinkReceipt{}, extension.ErrProjectAuditDenied + } + if err != nil { + return extension.ProjectAuditLinkReceipt{}, err + } + if projectID != string(req.ProjectID) || row.Envelope.Actor != h.owner || row.Envelope.OperationID != req.OperationID || string(row.Envelope.State) != "committed" || row.Envelope.RunID != req.RunID { + return extension.ProjectAuditLinkReceipt{}, extension.ErrProjectAuditDenied + } + if req.RunID != "" { + bound, err := h.caps.st.RunBoundToAudit(h.owner, projectID, row.Envelope.ConversationID, req.RunID) + if err != nil { + return extension.ProjectAuditLinkReceipt{}, err + } + if !bound { + return extension.ProjectAuditLinkReceipt{}, extension.ErrProjectAuditDenied + } + } + previous, lookupErr := h.caps.st.ProjectAuditLinkByKey(h.owner, req.IdempotencyKey) + if lookupErr == nil { + if previous.HostAuditOffset != req.HostAuditOffset || previous.ProjectID != string(req.ProjectID) || previous.SourceKind != req.SourceKind || previous.SourceID != req.SourceID || previous.OperationID != req.OperationID || previous.ResourceKind != req.ResourceKind || previous.ResourceID != req.ResourceID || previous.RunID != req.RunID || previous.DisclosureClass != req.DisclosureClass { + return extension.ProjectAuditLinkReceipt{}, extension.ErrProjectAuditLinkConflict + } + return extension.ProjectAuditLinkReceipt{LinkID: previous.ID, HostAuditOffset: previous.HostAuditOffset}, nil + } + if !errors.Is(lookupErr, store.ErrNotFound) { + return extension.ProjectAuditLinkReceipt{}, lookupErr + } + proof := extension.ProjectAuditLinkProof{ProjectAuditLinkRequest: req, Owner: h.owner} + ok, err := verify(ctx, proof) + if err != nil { + return extension.ProjectAuditLinkReceipt{}, err + } + if !ok { + return extension.ProjectAuditLinkReceipt{}, extension.ErrProjectAuditDenied + } + if err := ctx.Err(); err != nil { + return extension.ProjectAuditLinkReceipt{}, err + } + link := store.ProjectAuditLink{HostAuditOffset: req.HostAuditOffset, ProjectID: projectID, Owner: h.owner, + SourceKind: req.SourceKind, SourceID: req.SourceID, OperationID: req.OperationID, ResourceKind: req.ResourceKind, + ResourceID: req.ResourceID, RunID: req.RunID, DisclosureClass: req.DisclosureClass, IdempotencyKey: req.IdempotencyKey} + saved, err := h.caps.st.SaveProjectAuditLink(link) + if errors.Is(err, store.ErrConflict) { + return extension.ProjectAuditLinkReceipt{}, extension.ErrProjectAuditLinkConflict + } + if err != nil { + return extension.ProjectAuditLinkReceipt{}, err + } + return extension.ProjectAuditLinkReceipt{LinkID: saved.ID, HostAuditOffset: saved.HostAuditOffset}, nil +} func (h inprocHost) QueryProjectAudit(ctx context.Context, q extension.ProjectAuditQuery) (extension.ProjectAuditPage, error) { if err := ctx.Err(); err != nil { @@ -69,21 +202,31 @@ func (h inprocHost) QueryProjectAudit(ctx context.Context, q extension.ProjectAu return extension.ProjectAuditPage{}, extension.ErrProjectAuditUnsupported } ok, err := check(h.actor, core.ID(q.ProjectID)) - if err != nil || !ok { + if err != nil { + return extension.ProjectAuditPage{}, err + } + if !ok { return extension.ProjectAuditPage{}, extension.ErrProjectAuditDenied } if err := ctx.Err(); err != nil { return extension.ProjectAuditPage{}, err } + if q.LinkedSourceKind != "" || q.LinkedSourceID != "" || q.LinkedHostOffset != 0 { + return h.queryLinkedProjectAudit(ctx, q, slot) + } rows, cutoff, next, err := h.caps.st.ListProjectCommandAudits(h.owner, string(q.ProjectID), q.BeforeOffset, q.CutoffOffset, q.Limit) if err != nil { return extension.ProjectAuditPage{}, err } + cutoffConsistency := "committed-prefix" + if h.caps.st.Dialect() == "postgres" { + cutoffConsistency = "offset-upper-bound-only" + } page := extension.ProjectAuditPage{ Receipts: make([]extension.HostAuditReceipt, 0, len(rows)), NextOffset: next, CutoffOffset: cutoff, Coverage: extension.ProjectAuditCoverage{ - CommandAudit: "actor-owned-project-bound-only", CrossMember: "unsupported", LegacyProjectBinding: "not-recorded", RunRelations: "unsupported", ModelReceipts: "unsupported", + CommandAudit: "actor-owned-project-bound-only", CutoffConsistency: cutoffConsistency, CrossMember: "unsupported", LegacyProjectBinding: "not-recorded", RunRelations: "unsupported", ModelReceipts: "unsupported", BackgroundReceipts: "unsupported", ResourceBodies: "unsupported", }, } @@ -101,3 +244,41 @@ func (h inprocHost) QueryProjectAudit(ctx context.Context, q extension.ProjectAu } return page, nil } + +func (h inprocHost) queryLinkedProjectAudit(ctx context.Context, q extension.ProjectAuditQuery, slot *projectAuditSlot) (extension.ProjectAuditPage, error) { + if q.LinkedSourceKind != "domain-commit" || q.LinkedSourceID == "" || q.LinkedHostOffset <= 0 || q.BeforeOffset != 0 || q.CutoffOffset != 0 { + return extension.ProjectAuditPage{}, extension.ErrProjectAuditUnsupported + } + slot.mu.RLock() + disclose := slot.disclose + slot.mu.RUnlock() + if disclose == nil { + return extension.ProjectAuditPage{}, extension.ErrProjectAuditUnsupported + } + link, row, err := h.caps.st.ProjectAuditLinkedReceipt(string(q.ProjectID), q.LinkedSourceKind, q.LinkedSourceID, q.LinkedHostOffset) + if errors.Is(err, store.ErrNotFound) { + return extension.ProjectAuditPage{}, extension.ErrProjectAuditDenied + } + if err != nil { + return extension.ProjectAuditPage{}, err + } + resource := extension.ProjectAuditLinkedResource{ProjectID: q.ProjectID, Owner: link.Owner, SourceKind: link.SourceKind, SourceID: link.SourceID, + OperationID: link.OperationID, ResourceKind: link.ResourceKind, ResourceID: link.ResourceID, RunID: link.RunID, DisclosureClass: link.DisclosureClass} + allowed, err := disclose(ctx, h.actor, resource) + if err != nil { + return extension.ProjectAuditPage{}, err + } + if !allowed.Metadata { + return extension.ProjectAuditPage{}, extension.ErrProjectAuditDenied + } + nameStatus := "not-recorded" + if row.ActorName != "" { + nameStatus = "recorded" + } + return extension.ProjectAuditPage{Receipts: []extension.HostAuditReceipt{{SourceOffset: row.Offset, Time: row.Envelope.Time, + ActorID: row.Envelope.Actor, ActorName: row.ActorName, ActorNameStatus: nameStatus, CommandID: row.Envelope.CommandID, + State: string(row.Envelope.State), OperationID: row.Envelope.OperationID}}, + Coverage: extension.ProjectAuditCoverage{CommandAudit: "verified-exact-link-only", CrossMember: "verified-exact-link-only", + CutoffConsistency: "exact-link", LegacyProjectBinding: "not-recorded", RunRelations: "unsupported", + ModelReceipts: "unsupported", BackgroundReceipts: "unsupported", ResourceBodies: "unsupported"}}, nil +} diff --git a/internal/app/project_audit_link_test.go b/internal/app/project_audit_link_test.go new file mode 100644 index 0000000..5940436 --- /dev/null +++ b/internal/app/project_audit_link_test.go @@ -0,0 +1,137 @@ +package app + +import ( + "context" + "encoding/json" + "errors" + "path/filepath" + "strings" + "testing" + "time" + + agentcommand "agent-command" + "agent-system-backend/internal/store" + "agent-system-backend/pkg/extension" + core "agent-system-core" +) + +func TestProjectAuditLinkSharesOnlyVerifiedCommittedMetadata(t *testing.T) { + st, err := store.OpenSQLite(filepath.Join(t.TempDir(), "audit.db")) + if err != nil { + t.Fatal(err) + } + defer st.Close() + if err := st.EnsureUser("alice", "Alice at action"); err != nil { + t.Fatal(err) + } + if err := st.EnsureUser("bob", "Bob"); err != nil { + t.Fatal(err) + } + p, err := st.CreateProject("alice", "A") + if err != nil { + t.Fatal(err) + } + otherProject, err := st.CreateProject("alice", "B") + if err != nil { + t.Fatal(err) + } + c, err := st.CreateConversation("alice", string(p.ID), "private", "") + if err != nil { + t.Fatal(err) + } + if err := st.AppendAudit("alice", agentcommand.AuditEnvelope{Time: time.Now(), Actor: "alice", CommandID: "rdm.confirm", OperationID: "op-1", ConversationID: string(c.ID), State: agentcommand.StateCommitted, ParamsDigest: "secret-digest"}); err != nil { + t.Fatal(err) + } + private, _, _, err := st.ListProjectCommandAudits("alice", string(p.ID), 0, 0, 10) + if err != nil { + t.Fatal(err) + } + offset := private[0].Offset + slot := &projectAuditSlot{} + caps := hostcaps{st: st, projectAudit: slot} + reg := pluginRegistry{pluginID: "rdm", caps: caps} + auditAllowed := true + if err := reg.RegisterProjectAuditAuthorizer(func(actor string, projectID core.ID) (bool, error) { + return auditAllowed && actor == "bob" && projectID == p.ID, nil + }); err != nil { + t.Fatal(err) + } + if err := reg.RegisterProjectAuditLinkVerifier(func(_ context.Context, proof extension.ProjectAuditLinkProof) (bool, error) { + return proof.Owner == "alice" && proof.ProjectID == p.ID && proof.SourceKind == "domain-commit" && proof.SourceID == "commit-1" && proof.OperationID == "op-1" && proof.ResourceKind == "document" && proof.ResourceID == "doc-1", nil + }); err != nil { + t.Fatal(err) + } + metadataAllowed := true + if err := reg.RegisterProjectAuditDisclosureAuthorizer(func(_ context.Context, actor string, resource extension.ProjectAuditLinkedResource) (extension.ProjectAuditDisclosure, error) { + return extension.ProjectAuditDisclosure{Metadata: metadataAllowed && actor == "bob" && resource.ResourceID == "doc-1"}, nil + }); err != nil { + t.Fatal(err) + } + bob := inprocHost{caps: caps, pluginID: "rdm", owner: "bob", actor: "bob"} + q := extension.ProjectAuditQuery{ProjectID: p.ID, LinkedSourceKind: "domain-commit", LinkedSourceID: "commit-1", LinkedHostOffset: offset} + if _, err := bob.QueryProjectAudit(context.Background(), q); !errors.Is(err, extension.ErrProjectAuditDenied) { + t.Fatalf("unlinked private audit: %v", err) + } + alicePublisher := inprocHost{caps: caps, pluginID: "rdm", owner: "alice"} // trusted outbox-scoped host + linkReq := extension.ProjectAuditLinkRequest{ProjectID: p.ID, HostAuditOffset: offset, SourceKind: "domain-commit", SourceID: "commit-1", OperationID: "op-1", ResourceKind: "document", ResourceID: "doc-1", DisclosureClass: "metadata", IdempotencyKey: "outbox-1"} + if rows, err := bob.QueryProjectAudit(context.Background(), extension.ProjectAuditQuery{ProjectID: p.ID}); err != nil || len(rows.Receipts) != 0 { + t.Fatalf("private owner row visible without exact link: %+v %v", rows, err) + } + wrongProject := linkReq + wrongProject.ProjectID = otherProject.ID + if _, err := alicePublisher.PublishProjectAuditLink(context.Background(), wrongProject); !errors.Is(err, extension.ErrProjectAuditDenied) { + t.Fatalf("wrong host project: %v", err) + } + uncommitted := linkReq + uncommitted.SourceID, uncommitted.IdempotencyKey = "uncommitted", "outbox-uncommitted" + if _, err := alicePublisher.PublishProjectAuditLink(context.Background(), uncommitted); !errors.Is(err, extension.ErrProjectAuditDenied) { + t.Fatalf("uncommitted source: %v", err) + } + if _, err := (inprocHost{caps: caps, pluginID: "other", owner: "alice"}).PublishProjectAuditLink(context.Background(), linkReq); !errors.Is(err, extension.ErrProjectAuditUnsupported) { + t.Fatalf("other plugin: %v", err) + } + first, err := alicePublisher.PublishProjectAuditLink(context.Background(), linkReq) + if err != nil { + t.Fatal(err) + } + retry, err := alicePublisher.PublishProjectAuditLink(context.Background(), linkReq) + if err != nil || retry.LinkID != first.LinkID { + t.Fatalf("idempotent retry: first=%+v retry=%+v err=%v", first, retry, err) + } + page, err := bob.QueryProjectAudit(context.Background(), q) + if err != nil { + t.Fatal(err) + } + if len(page.Receipts) != 1 || page.Receipts[0].CommandID != "rdm.confirm" || page.Receipts[0].ActorName != "Alice at action" || page.Receipts[0].ConversationID != "" || page.Coverage.CrossMember != "verified-exact-link-only" { + t.Fatalf("linked page: %+v", page) + } + raw, err := json.Marshal(page) + if err != nil { + t.Fatal(err) + } + if strings.Contains(string(raw), "secret-digest") || strings.Contains(string(raw), string(c.ID)) { + t.Fatalf("private receipt fields leaked: %s", raw) + } + metadataAllowed = false + if _, err := bob.QueryProjectAudit(context.Background(), q); !errors.Is(err, extension.ErrProjectAuditDenied) { + t.Fatalf("revoked object metadata: %v", err) + } + metadataAllowed, auditAllowed = true, false + if _, err := bob.QueryProjectAudit(context.Background(), q); !errors.Is(err, extension.ErrProjectAuditDenied) { + t.Fatalf("revoked audit.read: %v", err) + } + linkReq.ResourceID = "doc-2" + if _, err := alicePublisher.PublishProjectAuditLink(context.Background(), linkReq); !errors.Is(err, extension.ErrProjectAuditLinkConflict) { + t.Fatalf("same key different link: %v", err) + } + metadataAllowed, auditAllowed = true, true + if err := st.Close(); err != nil { + t.Fatal(err) + } + if _, err := alicePublisher.PublishProjectAuditLink(context.Background(), linkReq); err == nil || errors.Is(err, extension.ErrProjectAuditDenied) { + t.Fatalf("storage outage during publish must remain retryable: %v", err) + } + if _, err := bob.QueryProjectAudit(context.Background(), q); err == nil || errors.Is(err, extension.ErrProjectAuditDenied) { + t.Fatalf("storage outage during linked read must be explicit: %v", err) + } +} diff --git a/internal/app/project_audit_test.go b/internal/app/project_audit_test.go index 4157fa9..3640da9 100644 --- a/internal/app/project_audit_test.go +++ b/internal/app/project_audit_test.go @@ -72,7 +72,7 @@ func TestProjectAuditHostChecksDomainReadAndProjectsStoredReceipts(t *testing.T) if len(page.Receipts) != 1 || page.Receipts[0].CommandID != "shared.command" { t.Fatalf("A page: %+v", page) } - if page.Coverage.CommandAudit != "actor-owned-project-bound-only" || page.Coverage.CrossMember != "unsupported" || page.Coverage.LegacyProjectBinding != "not-recorded" || page.Coverage.RunRelations != "unsupported" || page.Coverage.ModelReceipts != "unsupported" { + if page.Coverage.CommandAudit != "actor-owned-project-bound-only" || page.Coverage.CrossMember != "unsupported" || page.Coverage.LegacyProjectBinding != "not-recorded" || page.Coverage.RunRelations != "unsupported" || page.Coverage.ModelReceipts != "unsupported" || page.Coverage.CutoffConsistency != "committed-prefix" { t.Fatalf("coverage: %+v", page.Coverage) } if page.Receipts[0].ActorName != "BOB" { diff --git a/internal/app/surface_audit_actor_test.go b/internal/app/surface_audit_actor_test.go new file mode 100644 index 0000000..2749d7a --- /dev/null +++ b/internal/app/surface_audit_actor_test.go @@ -0,0 +1,50 @@ +package app + +import ( + "context" + "path/filepath" + "testing" + "time" + + agentcommand "agent-command" + "agent-system-backend/internal/cmdbus" + "agent-system-backend/internal/store" + "agent-system-backend/internal/surfacesvc" +) + +func TestSurfaceButtonDispatchUsesAuthenticatedActor(t *testing.T) { + st, err := store.OpenSQLite(filepath.Join(t.TempDir(), "audit.db")) + if err != nil { + t.Fatal(err) + } + defer st.Close() + if err := st.EnsureUser("alice", "Alice at action"); err != nil { + t.Fatal(err) + } + p, err := st.CreateProject("alice", "A") + if err != nil { + t.Fatal(err) + } + c, err := st.CreateConversation("alice", string(p.ID), "work", "") + if err != nil { + t.Fatal(err) + } + bus := cmdbus.New(st, nil, func() time.Time { return time.Now().UTC() }) + if err := bus.RegisterBuiltin(agentcommand.CommandSpec{ID: "demo.submit", Title: "Submit"}, "", func(context.Context, cmdbus.Request) (any, error) { return "ok", nil }); err != nil { + t.Fatal(err) + } + result, err := dispatchSurfaceAction(context.Background(), bus, surfacesvc.ActionDispatch{CommandID: "demo.submit", Owner: "alice", ConversationID: string(c.ID)}) + if err != nil { + t.Fatal(err) + } + if result.State != "committed" { + t.Fatalf("state: %s", result.State) + } + rows, _, _, err := st.ListProjectCommandAudits("alice", string(p.ID), 0, 0, 10) + if err != nil { + t.Fatal(err) + } + if len(rows) != 1 || rows[0].Envelope.Actor != "alice" || rows[0].ActorName != "Alice at action" { + t.Fatalf("button audit actor: %+v", rows) + } +} diff --git a/internal/app/surface_dispatch.go b/internal/app/surface_dispatch.go new file mode 100644 index 0000000..7a8137a --- /dev/null +++ b/internal/app/surface_dispatch.go @@ -0,0 +1,25 @@ +package app + +import ( + "context" + + "agent-system-backend/internal/cmdbus" + "agent-system-backend/internal/surfacesvc" +) + +// dispatchSurfaceAction receives Owner from SurfaceService's authenticated +// caller. The button is acting for that user, so both Actor and Owner must be +// populated before the command bus writes its audit envelope. +func dispatchSurfaceAction(ctx context.Context, bus *cmdbus.Bus, d surfacesvc.ActionDispatch) (surfacesvc.ActionOutcome, error) { + resp := bus.Dispatch(ctx, cmdbus.Request{ID: d.CommandID, Params: d.Params, + Entrance: "button", Actor: d.Owner, Owner: d.Owner, ConversationID: d.ConversationID}) + state := "committed" + if resp.Receipt.State != "" { + state = string(resp.Receipt.State) + } + cardID := "" + if resp.Card != nil { + cardID = string(resp.Card.ID) + } + return surfacesvc.ActionOutcome{State: state, CardID: cardID, Receipt: resp.Receipt, Payload: resp.Payload}, nil +} diff --git a/internal/store/migrate.go b/internal/store/migrate.go index 003b913..0c44edb 100644 --- a/internal/store/migrate.go +++ b/internal/store/migrate.go @@ -857,6 +857,15 @@ func init() { "sqlite": {`DROP INDEX IF EXISTS idx_command_audit_project_owner_offset`, `ALTER TABLE command_audit DROP COLUMN project_id`, `ALTER TABLE command_audit DROP COLUMN actor_name`}, "postgres": {`DROP INDEX IF EXISTS idx_command_audit_project_owner_offset`, `ALTER TABLE command_audit DROP COLUMN project_id`, `ALTER TABLE command_audit DROP COLUMN actor_name`}, }}, + migration{version: 32, name: "project_audit_committed_links", + up: map[string][]string{ + "sqlite": {`CREATE TABLE project_audit_links (id INTEGER PRIMARY KEY AUTOINCREMENT,host_audit_offset INTEGER NOT NULL REFERENCES command_audit(id),project_id TEXT NOT NULL,owner TEXT NOT NULL,source_kind TEXT NOT NULL,source_id TEXT NOT NULL,operation_id TEXT NOT NULL,resource_kind TEXT NOT NULL,resource_id TEXT NOT NULL,run_id TEXT NOT NULL DEFAULT '',disclosure_class TEXT NOT NULL,idempotency_key TEXT NOT NULL,created_at TEXT NOT NULL,UNIQUE(owner,idempotency_key),UNIQUE(host_audit_offset,source_kind,source_id))`, `CREATE INDEX idx_project_audit_links_source ON project_audit_links(project_id,source_kind,source_id,host_audit_offset)`}, + "postgres": {`CREATE TABLE project_audit_links (id BIGSERIAL PRIMARY KEY,host_audit_offset BIGINT NOT NULL REFERENCES command_audit(id),project_id TEXT NOT NULL,owner TEXT NOT NULL,source_kind TEXT NOT NULL,source_id TEXT NOT NULL,operation_id TEXT NOT NULL,resource_kind TEXT NOT NULL,resource_id TEXT NOT NULL,run_id TEXT NOT NULL DEFAULT '',disclosure_class TEXT NOT NULL,idempotency_key TEXT NOT NULL,created_at TEXT NOT NULL,UNIQUE(owner,idempotency_key),UNIQUE(host_audit_offset,source_kind,source_id))`, `CREATE INDEX idx_project_audit_links_source ON project_audit_links(project_id,source_kind,source_id,host_audit_offset)`}, + }, + down: map[string][]string{ + "sqlite": {`DROP TABLE IF EXISTS project_audit_links`}, + "postgres": {`DROP TABLE IF EXISTS project_audit_links`}, + }}, ) } diff --git a/internal/store/migrate_compat.go b/internal/store/migrate_compat.go index 88fd006..9160ed1 100644 --- a/internal/store/migrate_compat.go +++ b/internal/store/migrate_compat.go @@ -36,6 +36,7 @@ var migrationBreaksOldReaders = map[int]bool{ 29: false, // redeem_attempts:新表,旧读者不受影响 30: false, // project_event_publication_proofs:新表,旧读者不受影响 31: false, // command_audit_attribution_snapshot:仅加列/索引,旧读者不受影响 + 32: false, // project_audit_committed_links:新表,旧读者不受影响 } // SchemaTarget 是本二进制会迁移到的 schema 版本。 diff --git a/internal/store/project_audit_links.go b/internal/store/project_audit_links.go new file mode 100644 index 0000000..5b58dd8 --- /dev/null +++ b/internal/store/project_audit_links.go @@ -0,0 +1,143 @@ +package store + +import ( + "database/sql" + "encoding/json" + "errors" +) + +// ProjectAuditLink is an immutable association written only after a trusted +// domain verifier has checked the committed source event. +type ProjectAuditLink struct { + ID int64 + HostAuditOffset int64 + ProjectID string + Owner string + SourceKind string + SourceID string + OperationID string + ResourceKind string + ResourceID string + RunID string + DisclosureClass string + IdempotencyKey string +} + +func (s *Store) CommandAuditForLink(owner string, offset int64) (ProjectCommandAuditRow, string, error) { + var row ProjectCommandAuditRow + var projectID, raw string + if owner == "" || offset <= 0 { + return row, "", ErrNotFound + } + err := s.db.QueryRow(s.q(`SELECT id,actor_name,project_id,payload FROM command_audit WHERE id=? AND owner=? AND project_id<>''`), offset, owner). + Scan(&row.Offset, &row.ActorName, &projectID, &raw) + if errors.Is(err, sql.ErrNoRows) { + return row, "", ErrNotFound + } + if err != nil { + return row, "", err + } + if err := json.Unmarshal([]byte(raw), &row.Envelope); err != nil { + return row, "", err + } + return row, projectID, nil +} + +func (s *Store) RunBoundToAudit(owner, projectID, conversationID, runID string) (bool, error) { + if owner == "" || projectID == "" || conversationID == "" || runID == "" { + return false, nil + } + var one int + err := s.db.QueryRow(s.q(`SELECT 1 FROM runs WHERE id=? AND owner=? AND project_id=? AND conversation_id=?`), runID, owner, projectID, conversationID).Scan(&one) + if errors.Is(err, sql.ErrNoRows) { + return false, nil + } + return err == nil, err +} + +const projectAuditLinkColumns = `id,host_audit_offset,project_id,owner,source_kind,source_id,operation_id,resource_kind,resource_id,run_id,disclosure_class,idempotency_key` +const projectAuditLinkAliasedColumns = `l.id,l.host_audit_offset,l.project_id,l.owner,l.source_kind,l.source_id,l.operation_id,l.resource_kind,l.resource_id,l.run_id,l.disclosure_class,l.idempotency_key` + +func scanProjectAuditLink(scan func(...any) error) (ProjectAuditLink, error) { + var link ProjectAuditLink + err := scan(&link.ID, &link.HostAuditOffset, &link.ProjectID, &link.Owner, &link.SourceKind, &link.SourceID, + &link.OperationID, &link.ResourceKind, &link.ResourceID, &link.RunID, &link.DisclosureClass, &link.IdempotencyKey) + if errors.Is(err, sql.ErrNoRows) { + return link, ErrNotFound + } + return link, err +} + +func (s *Store) ProjectAuditLinkByKey(owner, key string) (ProjectAuditLink, error) { + if owner == "" || key == "" { + return ProjectAuditLink{}, ErrNotFound + } + return scanProjectAuditLink(func(dst ...any) error { + return s.db.QueryRow(s.q(`SELECT `+projectAuditLinkColumns+` FROM project_audit_links WHERE owner=? AND idempotency_key=?`), owner, key).Scan(dst...) + }) +} + +func sameProjectAuditLink(a, b ProjectAuditLink) bool { + return a.HostAuditOffset == b.HostAuditOffset && a.ProjectID == b.ProjectID && a.Owner == b.Owner && + a.SourceKind == b.SourceKind && a.SourceID == b.SourceID && a.OperationID == b.OperationID && + a.ResourceKind == b.ResourceKind && a.ResourceID == b.ResourceID && a.RunID == b.RunID && + a.DisclosureClass == b.DisclosureClass && a.IdempotencyKey == b.IdempotencyKey +} + +// SaveProjectAuditLink is idempotent for an identical key and binding. Both the +// key and the host/source triple are unique; racing or conflicting retries +// resolve by reading the committed winner, never by overwriting it. +func (s *Store) SaveProjectAuditLink(link ProjectAuditLink) (ProjectAuditLink, error) { + var id int64 + err := s.db.QueryRow(s.q(`INSERT INTO project_audit_links(host_audit_offset,project_id,owner,source_kind,source_id,operation_id,resource_kind,resource_id,run_id,disclosure_class,idempotency_key,created_at) + VALUES (?,?,?,?,?,?,?,?,?,?,?,?) ON CONFLICT DO NOTHING RETURNING id`), link.HostAuditOffset, link.ProjectID, link.Owner, + link.SourceKind, link.SourceID, link.OperationID, link.ResourceKind, link.ResourceID, link.RunID, link.DisclosureClass, + link.IdempotencyKey, nowISO()).Scan(&id) + if err == nil { + link.ID = id + return link, nil + } + if !errors.Is(err, sql.ErrNoRows) { + return ProjectAuditLink{}, err + } + previous, err := scanProjectAuditLink(func(dst ...any) error { + return s.db.QueryRow(s.q(`SELECT `+projectAuditLinkColumns+` FROM project_audit_links + WHERE (owner=? AND idempotency_key=?) OR (host_audit_offset=? AND source_kind=? AND source_id=?) LIMIT 1`), + link.Owner, link.IdempotencyKey, link.HostAuditOffset, link.SourceKind, link.SourceID).Scan(dst...) + }) + if err != nil { + return ProjectAuditLink{}, err + } + if !sameProjectAuditLink(previous, link) { + return ProjectAuditLink{}, ErrConflict + } + return previous, nil +} + +// ProjectAuditLinkedReceipt reads one exact association and the underlying +// host row. It does not enumerate private links or decide field disclosure. +func (s *Store) ProjectAuditLinkedReceipt(projectID, sourceKind, sourceID string, hostOffset int64) (ProjectAuditLink, ProjectCommandAuditRow, error) { + var row ProjectCommandAuditRow + var link ProjectAuditLink + var raw string + if projectID == "" || sourceKind == "" || sourceID == "" || hostOffset <= 0 { + return link, row, ErrNotFound + } + err := s.db.QueryRow(s.q(`SELECT `+projectAuditLinkAliasedColumns+`,a.actor_name,a.payload FROM project_audit_links l + JOIN command_audit a ON a.id=l.host_audit_offset AND a.owner=l.owner AND a.project_id=l.project_id + WHERE l.project_id=? AND l.source_kind=? AND l.source_id=? AND l.host_audit_offset=?`), + projectID, sourceKind, sourceID, hostOffset).Scan(&link.ID, &link.HostAuditOffset, &link.ProjectID, &link.Owner, + &link.SourceKind, &link.SourceID, &link.OperationID, &link.ResourceKind, &link.ResourceID, &link.RunID, + &link.DisclosureClass, &link.IdempotencyKey, &row.ActorName, &raw) + if errors.Is(err, sql.ErrNoRows) { + return link, row, ErrNotFound + } + if err != nil { + return link, row, err + } + row.Offset = link.HostAuditOffset + if err := json.Unmarshal([]byte(raw), &row.Envelope); err != nil { + return link, row, err + } + return link, row, nil +} diff --git a/internal/store/project_audit_links_test.go b/internal/store/project_audit_links_test.go new file mode 100644 index 0000000..b77838c --- /dev/null +++ b/internal/store/project_audit_links_test.go @@ -0,0 +1,84 @@ +package store + +import ( + "errors" + "testing" + "time" + + agentcommand "agent-command" +) + +func TestProjectAuditLinkStoreIdempotenceAndExactScope(t *testing.T) { + st := openTestStore(t) // SQLite locally; isolated PostgreSQL schema when configured. + if err := st.EnsureUser("alice", "Alice"); err != nil { + t.Fatal(err) + } + a, err := st.CreateProject("alice", "A") + if err != nil { + t.Fatal(err) + } + b, err := st.CreateProject("alice", "B") + if err != nil { + t.Fatal(err) + } + c, err := st.CreateConversation("alice", string(a.ID), "work", "") + if err != nil { + t.Fatal(err) + } + if err := st.AppendAudit("alice", agentcommand.AuditEnvelope{Time: time.Now(), Actor: "alice", CommandID: "rdm.confirm", OperationID: "op-1", ConversationID: string(c.ID), State: agentcommand.StateCommitted}); err != nil { + t.Fatal(err) + } + rows, _, _, err := st.ListProjectCommandAudits("alice", string(a.ID), 0, 0, 10) + if err != nil { + t.Fatal(err) + } + link := ProjectAuditLink{HostAuditOffset: rows[0].Offset, ProjectID: string(a.ID), Owner: "alice", SourceKind: "domain-commit", SourceID: "commit-1", OperationID: "op-1", ResourceKind: "document", ResourceID: "doc-1", DisclosureClass: "metadata", IdempotencyKey: "outbox-1"} + first, err := st.SaveProjectAuditLink(link) + if err != nil { + t.Fatal(err) + } + retry, err := st.SaveProjectAuditLink(link) + if err != nil || retry.ID != first.ID { + t.Fatalf("retry: first=%+v retry=%+v err=%v", first, retry, err) + } + if found, err := st.ProjectAuditLinkByKey("alice", "outbox-1"); err != nil || found.ID != first.ID { + t.Fatalf("key lookup: %+v %v", found, err) + } + if _, _, err := st.ProjectAuditLinkedReceipt(string(b.ID), "domain-commit", "commit-1", rows[0].Offset); !errors.Is(err, ErrNotFound) { + t.Fatalf("wrong project: %v", err) + } + if _, _, err := st.ProjectAuditLinkedReceipt(string(a.ID), "domain-commit", "other", rows[0].Offset); !errors.Is(err, ErrNotFound) { + t.Fatalf("wrong source: %v", err) + } + got, audit, err := st.ProjectAuditLinkedReceipt(string(a.ID), "domain-commit", "commit-1", rows[0].Offset) + if err != nil || got.ID != first.ID || audit.Envelope.CommandID != "rdm.confirm" { + t.Fatalf("linked read: %+v %+v %v", got, audit, err) + } + link.ResourceID = "doc-2" + if _, err := st.SaveProjectAuditLink(link); !errors.Is(err, ErrConflict) { + t.Fatalf("same key different binding: %v", err) + } + link.ResourceID = "doc-1" + link.IdempotencyKey = "outbox-2" + if _, err := st.SaveProjectAuditLink(link); !errors.Is(err, ErrConflict) { + t.Fatalf("same source different key: %v", err) + } + if err := st.EnsureUser("bob", "Bob"); err != nil { + t.Fatal(err) + } + bobConversation, err := st.CreateConversationInProject("bob", string(a.ID), "shared work", "") + if err != nil { + t.Fatal(err) + } + if err := st.AppendAudit("bob", agentcommand.AuditEnvelope{Time: time.Now(), Actor: "bob", CommandID: "rdm.confirm", OperationID: "op-bob", ConversationID: string(bobConversation.ID), State: agentcommand.StateCommitted}); err != nil { + t.Fatal(err) + } + bobRows, _, _, err := st.ListProjectCommandAudits("bob", string(a.ID), 0, 0, 10) + if err != nil { + t.Fatal(err) + } + bobLink := ProjectAuditLink{HostAuditOffset: bobRows[0].Offset, ProjectID: string(a.ID), Owner: "bob", SourceKind: "domain-commit", SourceID: "commit-bob", OperationID: "op-bob", ResourceKind: "document", ResourceID: "doc-bob", DisclosureClass: "metadata", IdempotencyKey: "outbox-1"} + if _, err := st.SaveProjectAuditLink(bobLink); err != nil { + t.Fatalf("another owner may use the same outbox key: %v", err) + } +} diff --git a/internal/store/project_audit_test.go b/internal/store/project_audit_test.go index 2ccb55b..db91605 100644 --- a/internal/store/project_audit_test.go +++ b/internal/store/project_audit_test.go @@ -2,6 +2,7 @@ package store import ( "encoding/json" + "os" "path/filepath" "testing" "time" @@ -172,3 +173,62 @@ func TestProjectCommandAuditDoesNotMoveWithConversation(t *testing.T) { t.Fatalf("project attribution changed: A=%+v B=%+v", oldProject, newProject) } } + +// PostgreSQL sequence IDs are allocated before commit. This test records the +// current upper-bound semantics: a lower ID can appear after a page cutoff was +// captured, so callers must not describe the cutoff as a visibility snapshot. +func TestProjectAuditPostgresCutoffLateCommit(t *testing.T) { + if os.Getenv("AGENT_PG_TEST_DSN") == "" { + t.Skip("AGENT_PG_TEST_DSN unset: PG concurrency check NOT_RUN") + } + st := openTestStore(t) + if st.Dialect() != "postgres" { + t.Fatal("expected postgres") + } + if err := st.EnsureUser("alice", "Alice"); err != nil { + t.Fatal(err) + } + p, err := st.CreateProject("alice", "A") + if err != nil { + t.Fatal(err) + } + c, err := st.CreateConversation("alice", string(p.ID), "work", "") + if err != nil { + t.Fatal(err) + } + env := agentcommand.AuditEnvelope{Time: time.Now(), Actor: "alice", CommandID: "late-low-id", ConversationID: string(c.ID)} + raw, err := json.Marshal(env) + if err != nil { + t.Fatal(err) + } + tx, err := st.db.Begin() + if err != nil { + t.Fatal(err) + } + defer tx.Rollback() + var lowID int64 + if err := tx.QueryRow(st.q(`INSERT INTO command_audit(time,command_id,source,entrance,state,operation_id,conversation_id,owner,payload,actor_name,project_id) + VALUES (?,?,'','','','',?,?,?,?,?) RETURNING id`), time.Now().UTC().Format(time.RFC3339Nano), "late-low-id", string(c.ID), "alice", string(raw), "Alice", string(p.ID)).Scan(&lowID); err != nil { + t.Fatal(err) + } + if err := st.AppendAudit("alice", agentcommand.AuditEnvelope{Time: time.Now(), Actor: "alice", CommandID: "committed-high-id", ConversationID: string(c.ID)}); err != nil { + t.Fatal(err) + } + first, cutoff, _, err := st.ListProjectCommandAudits("alice", string(p.ID), 0, 0, 10) + if err != nil { + t.Fatal(err) + } + if len(first) != 1 || first[0].Envelope.CommandID != "committed-high-id" || lowID >= cutoff { + t.Fatalf("first page: rows=%+v low=%d cutoff=%d", first, lowID, cutoff) + } + if err := tx.Commit(); err != nil { + t.Fatal(err) + } + later, fixed, _, err := st.ListProjectCommandAudits("alice", string(p.ID), 0, cutoff, 10) + if err != nil { + t.Fatal(err) + } + if fixed != cutoff || len(later) != 2 || later[1].Envelope.CommandID != "late-low-id" { + t.Fatalf("late commit not exposed: rows=%+v cutoff=%d", later, fixed) + } +} diff --git a/pkg/extension/extension.go b/pkg/extension/extension.go index 2996105..fca22f9 100644 --- a/pkg/extension/extension.go +++ b/pkg/extension/extension.go @@ -36,7 +36,9 @@ import ( // ResourceAuthzQuery.Source for per-document publication authorization. // 2.13.0-experimental: optional ProjectAuditHost and ProjectAuditRegistry (X2 // project-bound command-audit read slice and domain audit.read callback). -const SurfaceVersion = "2.13.0-experimental" +// 2.14.0-experimental: X2 optional committed project-audit link publisher, +// verifier, disclosure callback, and exact linked receipt read. +const SurfaceVersion = "2.14.0-experimental" // ErrNotLinked 表示进程没有空白导入 link 包,Main 无法启动服务。 var ErrNotLinked = errors.New("extension: runner not linked; blank-import agent-system-backend/pkg/extension/link") diff --git a/pkg/extension/project_audit.go b/pkg/extension/project_audit.go index 43beaed..e993508 100644 --- a/pkg/extension/project_audit.go +++ b/pkg/extension/project_audit.go @@ -20,18 +20,89 @@ type ProjectAuditRegistry interface { } var ( - ErrProjectAuditDenied = errors.New("extension: project audit denied") - ErrProjectAuditUnsupported = errors.New("extension: project audit unsupported") + ErrProjectAuditDenied = errors.New("extension: project audit denied") + ErrProjectAuditUnsupported = errors.New("extension: project audit unsupported") + ErrProjectAuditLinkConflict = errors.New("extension: project audit link conflict") ) +// ProjectAuditLinkRequest binds one host command receipt to an already +// committed domain fact. Owner is never caller supplied; the host handle and +// stored audit row establish it. This first slice supports domain-commit only. +type ProjectAuditLinkRequest struct { + ProjectID core.ID `json:"projectId"` + HostAuditOffset int64 `json:"hostAuditOffset"` + SourceKind string `json:"sourceKind"` + SourceID string `json:"sourceId"` + OperationID string `json:"operationId"` + ResourceKind string `json:"resourceKind"` + ResourceID string `json:"resourceId"` + RunID string `json:"runId,omitempty"` + DisclosureClass string `json:"disclosureClass"` + IdempotencyKey string `json:"idempotencyKey"` +} + +// ProjectAuditLinkProof contains the caller request plus identity and source +// fields re-read from trusted host storage. The domain verifier must check its +// committed source event and operation/resource attribution, not a model claim. +type ProjectAuditLinkProof struct { + ProjectAuditLinkRequest + Owner string +} + +type ProjectAuditLinkVerifier func(context.Context, ProjectAuditLinkProof) (bool, error) + +// ProjectAuditLinkedResource is the stored, immutable link presented to the +// current domain disclosure policy. A caller ID alone never authorizes it. +type ProjectAuditLinkedResource struct { + ProjectID core.ID + Owner string + SourceKind string + SourceID string + OperationID string + ResourceKind string + ResourceID string + RunID string + DisclosureClass string +} + +type ProjectAuditDisclosure struct { + Metadata bool + Body bool +} + +type ProjectAuditDisclosureAuthorizer func(context.Context, string, ProjectAuditLinkedResource) (ProjectAuditDisclosure, error) + +// ProjectAuditLinkRegistry is optional. Only the registering compiled domain +// plugin may publish or read its links. +type ProjectAuditLinkRegistry interface { + RegisterProjectAuditLinkVerifier(ProjectAuditLinkVerifier) error + RegisterProjectAuditDisclosureAuthorizer(ProjectAuditDisclosureAuthorizer) error +} + +type ProjectAuditLinkReceipt struct { + LinkID int64 `json:"linkId"` + HostAuditOffset int64 `json:"hostAuditOffset"` +} + +type ProjectAuditPublisher interface { + PublishProjectAuditLink(context.Context, ProjectAuditLinkRequest) (ProjectAuditLinkReceipt, error) +} + // ProjectAuditQuery reads one project-bound command-audit slice. Offset is a // host source offset, not a cross-domain event sequence. A nonzero BeforeOffset -// must be paired with the CutoffOffset returned on the first page. +// must be paired with the CutoffOffset returned on the first page. On PostgreSQL +// that cutoff bounds offsets but is not a fixed visibility snapshot: a lower ID +// from a still-open transaction may commit later. See Coverage.CutoffConsistency. type ProjectAuditQuery struct { ProjectID core.ID `json:"projectId"` BeforeOffset int64 `json:"beforeOffset,omitempty"` CutoffOffset int64 `json:"cutoffOffset,omitempty"` Limit int `json:"limit,omitempty"` + // Linked* select one exact committed link. This avoids enumerating hidden + // private links or leaking their counts before cursor policy is complete. + LinkedSourceKind string `json:"linkedSourceKind,omitempty"` + LinkedSourceID string `json:"linkedSourceId,omitempty"` + LinkedHostOffset int64 `json:"linkedHostOffset,omitempty"` } // HostAuditReceipt is a server-projected receipt, never a raw audit payload. @@ -52,6 +123,7 @@ type HostAuditReceipt struct { // host sources and old unbound rows cannot be inferred from owner-wide audit. type ProjectAuditCoverage struct { CommandAudit string `json:"commandAudit"` + CutoffConsistency string `json:"cutoffConsistency"` CrossMember string `json:"crossMember"` LegacyProjectBinding string `json:"legacyProjectBinding"` RunRelations string `json:"runRelations"` diff --git a/pkg/extension/surface.txt b/pkg/extension/surface.txt index 66b08a9..2e88d0e 100644 --- a/pkg/extension/surface.txt +++ b/pkg/extension/surface.txt @@ -9,6 +9,7 @@ AdminZoneSpec Compiled ErrNotLinked ErrProjectAuditDenied +ErrProjectAuditLinkConflict ErrProjectAuditUnsupported Handler Host @@ -37,9 +38,21 @@ NewScopedHost Options ProjectAuditAuthorizer ProjectAuditCoverage +ProjectAuditDisclosure +ProjectAuditDisclosureAuthorizer ProjectAuditHost ProjectAuditHost.QueryProjectAudit +ProjectAuditLinkProof +ProjectAuditLinkReceipt +ProjectAuditLinkRegistry +ProjectAuditLinkRegistry.RegisterProjectAuditDisclosureAuthorizer +ProjectAuditLinkRegistry.RegisterProjectAuditLinkVerifier +ProjectAuditLinkRequest +ProjectAuditLinkVerifier +ProjectAuditLinkedResource ProjectAuditPage +ProjectAuditPublisher +ProjectAuditPublisher.PublishProjectAuditLink ProjectAuditQuery ProjectAuditRegistry ProjectAuditRegistry.RegisterProjectAuditAuthorizer From 44ccd63d3bec14cb87ee39ba5376247562326a75 Mon Sep 17 00:00:00 2001 From: weichen Date: Wed, 14 Oct 2026 04:09:58 +0000 Subject: [PATCH 03/15] Add trusted project context to work model calls Why: Project work currently reaches model calls without current authorized project facts or a persisted input receipt. Evidence: SELF go test ./... -count=1, targeted go test -race for W6 paths, go vet ./..., and CGO_ENABLED=0 go build ./cmd/agent-system passed on this change set. Impact: Add optional X1 interfaces, trusted persisted work-task binding, request-scoped context injection, evidence reauthorization, and attempted/responded/unknown manifests. Existing non-project behavior stays available. Refs: TT-C01 TT-C02 TT-C03 TT-C04; stage5 W6; Dev stage6 X1 public extension request r1 --- cmd/storage-migrate/main.go | 4 +- internal/app/app.go | 32 +- internal/app/compiled.go | 3 +- internal/app/hostcaps.go | 11 +- internal/app/project_context.go | 328 +++++++++++++++++++ internal/app/project_context_test.go | 347 +++++++++++++++++++++ internal/projectcontext/context.go | 82 +++++ internal/projectcontext/context_test.go | 43 +++ internal/projectcontext/receipt.go | 57 ++++ internal/runmgr/project_context.go | 226 ++++++++++++++ internal/runmgr/project_context_test.go | 194 ++++++++++++ internal/runmgr/runmgr.go | 39 ++- internal/runtime/llm.go | 38 ++- internal/runtime/llm_direct.go | 122 +++++++- internal/runtime/project_context_test.go | 193 ++++++++++++ internal/runtime/project_stream.go | 165 ++++++++++ internal/runtime/squad_runner.go | 8 +- internal/runtime/sub_loop.go | 1 + internal/runtime/transcript_compact.go | 8 +- internal/runtime/workflow_compiled.go | 3 +- internal/runtime/workflow_compiled_test.go | 45 +++ internal/store/context_manifest.go | 50 +++ internal/store/migrate.go | 7 + internal/store/migrate_compat.go | 1 + internal/store/work_tasks.go | 57 ++++ pkg/extension/extension.go | 5 +- pkg/extension/host.go | 2 + pkg/extension/link/link.go | 1 + pkg/extension/project_context.go | 142 +++++++++ pkg/extension/surface.txt | 24 ++ 30 files changed, 2191 insertions(+), 47 deletions(-) create mode 100644 internal/app/project_context.go create mode 100644 internal/app/project_context_test.go create mode 100644 internal/projectcontext/context.go create mode 100644 internal/projectcontext/context_test.go create mode 100644 internal/projectcontext/receipt.go create mode 100644 internal/runmgr/project_context.go create mode 100644 internal/runmgr/project_context_test.go create mode 100644 internal/runtime/project_context_test.go create mode 100644 internal/runtime/project_stream.go create mode 100644 internal/store/context_manifest.go create mode 100644 internal/store/work_tasks.go create mode 100644 pkg/extension/project_context.go diff --git a/cmd/storage-migrate/main.go b/cmd/storage-migrate/main.go index 7ee9cb2..2229194 100644 --- a/cmd/storage-migrate/main.go +++ b/cmd/storage-migrate/main.go @@ -286,7 +286,7 @@ var copyOrder = []string{ "hitl_cards", "plans", "todos", "knowledge", "knowledge_vectors", "activity_cards", "project_event_shares", "command_audit", "prompt_modules", "sessions", "api_keys", - "plugin_grants", "plugin_schema", "plugin_kv", "background_tasks", + "plugin_grants", "plugin_schema", "plugin_kv", "background_tasks", "work_task_bindings", "memory_entries", "audit_events", "pending_confirms", "llm_usage_daily", "secret_keys", "settings_snapshots", "regen_pending", "context_entries", "user_commands", "pending_notices", "recovery_holds", @@ -784,7 +784,7 @@ func advanceSequences(db *sql.DB, table string) error { // checkOwners:业务行引用的 owner 必须存在于 users(单租户默认 ”/'local' // 除外)。未知 owner 阻塞迁移(U4/R7:不静默带过未知归属)。 func checkOwners(db *sql.DB, src *sql.DB) error { - ownerTables := []string{"projects", "conversations", "runs", "artifacts", "knowledge", "hitl_cards", "profiles", "activity_cards", "project_event_shares", "command_audit"} + ownerTables := []string{"projects", "conversations", "runs", "artifacts", "knowledge", "hitl_cards", "profiles", "activity_cards", "project_event_shares", "command_audit", "work_task_bindings"} for _, t := range ownerTables { if !sqliteHasTable(src, t) { continue diff --git a/internal/app/app.go b/internal/app/app.go index 33749b7..0f9a111 100644 --- a/internal/app/app.go +++ b/internal/app/app.go @@ -60,16 +60,17 @@ import ( ) type Options struct { - Addr string - DataDir string - PluginsDir string - ForceMock bool - StorageDSN string // 空 = 嵌入式 SQLite 档;非空 = PostgreSQL 档(多用户/多实例) - ArtifactsDir string // PG 档工件对象目录(空 = /artifacts) - AuthMode string // auto(默认:PG 档开、嵌入式关)| on | off - InstanceID string // 空 = 主机名:监听地址;写入 runs.instance_id,启动对账只收口本实例 - InsecureListen bool - AllowedHosts []string + Addr string + DataDir string + PluginsDir string + ForceMock bool + StorageDSN string // 空 = 嵌入式 SQLite 档;非空 = PostgreSQL 档(多用户/多实例) + ArtifactsDir string // PG 档工件对象目录(空 = /artifacts) + AuthMode string // auto(默认:PG 档开、嵌入式关)| on | off + RequireProjectContext bool + InstanceID string // 空 = 主机名:监听地址;写入 runs.instance_id,启动对账只收口本实例 + InsecureListen bool + AllowedHosts []string // DisableUploadIndexing EXT-9:true 时上传端点不自动写检索索引(发布决定 // 移交下游命令层);零值=false=现状。由 link 层自 extension.Options 透传。 DisableUploadIndexing bool @@ -380,6 +381,7 @@ func Run(opt Options) error { return nil }, } + cfg.Stream = runtime.GuardProjectStream(rc, "", cfg.Client.Stream) tavilyNow := effNow.GetString("tools.tavily.apiKeys", "") if active, ok := st.ActiveSecret(store.OwnerDefault, "tools.tavily"); ok { tavilyNow = active @@ -597,7 +599,8 @@ func Run(opt Options) error { // hostcaps:hostapi 闭包与进程内插件句柄共用的 owner 作用域能力(EXT-1 批注 1)。 authzSlot := &projectAuthz{} projectSlot := &projectLifecycleSlot{} - caps := hostcaps{projects: projectSlot, st: st, pipe: pipe, set: setSvc, authz: authzSlot, surfaces: surfaceSvc} + contextSlot := &projectContextSlot{} + caps := hostcaps{projects: projectSlot, contexts: contextSlot, st: st, pipe: pipe, set: setSvc, authz: authzSlot, surfaces: surfaceSvc} // EXT-13:回合内检索(knowledge_search / @knowledge 注入)与 HTTP/Host 检索面 // 同一适配器授权;槽为共享指针,扩展在 applyCompiled 期间注册后即生效。 mgr.SetSharedKnowledgeAllow(func(owner string, projectID core.ID) bool { @@ -610,7 +613,14 @@ func Run(opt Options) error { // EXT-11:公开受限 Host 工厂(下游恢复路径经 extension.NewScopedHost 重建句柄)。 wireScopedHostFactory(caps) compiledSummary := applyCompiled(skillsRoot, bus, caps) + if err := requireProjectContextProvider(opt.RequireProjectContext, contextSlot); err != nil { + return err + } mgr.SetProjectWorkAuthorization(projectSlot.work()) + if contextSlot.active() { + mgr.SetProjectContextResolver(contextSlot.resolve) + mgr.SetProjectEvidenceReader(contextSlot.readEvidence) + } mgr.SetKnowledgeProjectScope(func(ctx context.Context, actor string, id core.ID) (core.ID, error) { return projectSlot.resolve(ctx, caps, actor, id) }) diff --git a/internal/app/compiled.go b/internal/app/compiled.go index da47164..f73b6f3 100644 --- a/internal/app/compiled.go +++ b/internal/app/compiled.go @@ -64,7 +64,8 @@ func (r pluginRegistry) AddCommandHandler(spec json.RawMessage, h extension.Hand return fmt.Errorf("extension: command spec: %w", err) } return r.bus.RegisterPluginCommand(s, func(ctx context.Context, req cmdbus.Request) (any, error) { - host := inprocHost{caps: r.caps, pluginID: r.pluginID, owner: req.Owner, actor: req.Actor} + host := inprocHost{caps: r.caps, pluginID: r.pluginID, owner: req.Owner, actor: req.Actor, + conversationID: core.ID(req.ConversationID), hostProjectID: core.ID(req.HostProjectID)} return h(ctx, host, extension.Request{ ID: req.ID, Params: req.Params, Entrance: req.Entrance, Actor: req.Actor, Owner: req.Owner, ConversationID: req.ConversationID, diff --git a/internal/app/hostcaps.go b/internal/app/hostcaps.go index 866a4f4..890adb6 100644 --- a/internal/app/hostcaps.go +++ b/internal/app/hostcaps.go @@ -22,6 +22,7 @@ import ( // inprocHost(EXT-1 批注 1:复用现有 owner 作用域函数,不新造句柄)。 type hostcaps struct { projects *projectLifecycleSlot + contexts *projectContextSlot st *store.Store pipe *rag.Pipeline set *settings.Service @@ -170,10 +171,12 @@ func (c hostcaps) parse(ctx context.Context, filename, mime string, content []by // actor 是本次分发主体(通常等于 owner);AdminProvider 能力经 actor 查询 // (EXT-18)。非 dispatch 上下文(EXT-11 scoped host)actor 为空——IsAdmin 恒 false。 type inprocHost struct { - caps hostcaps - pluginID string - owner string - actor string // EXT-18:分发主体(空=非 dispatch 上下文) + caps hostcaps + pluginID string + owner string + actor string // EXT-18:分发主体(空=非 dispatch 上下文) + conversationID core.ID // dispatch-bound, never taken from completion arguments + hostProjectID core.ID // dispatch project; rechecked against stored conversation } func (h inprocHost) PluginID() string { return h.pluginID } diff --git a/internal/app/project_context.go b/internal/app/project_context.go new file mode 100644 index 0000000..fb056d3 --- /dev/null +++ b/internal/app/project_context.go @@ -0,0 +1,328 @@ +package app + +import ( + "context" + "crypto/sha256" + "errors" + "fmt" + "strings" + "sync" + "time" + + "agent-system-backend/internal/projectcontext" + "agent-system-backend/internal/runtime" + "agent-system-backend/pkg/extension" + core "agent-system-core" +) + +type projectContextSlot struct { + mu sync.RWMutex + holder string + provider extension.ProjectContextProvider +} + +func (s *projectContextSlot) active() bool { + if s == nil { + return false + } + s.mu.RLock() + defer s.mu.RUnlock() + return s.provider != nil +} + +func requireProjectContextProvider(required bool, slot *projectContextSlot) error { + if required && !slot.active() { + return errors.New("extension: required project context provider not assembled") + } + return nil +} + +func (r compiledRegistry) RegisterProjectContextProvider(extension.ProjectContextProvider) error { + return errors.New("extension: declarative plugins cannot register project context providers") +} + +func (r pluginRegistry) RegisterProjectContextProvider(p extension.ProjectContextProvider) error { + if r.caps.contexts == nil || r.pluginID == "" || p == nil { + return errors.New("extension: project context provider unavailable") + } + slot := r.caps.contexts + slot.mu.Lock() + defer slot.mu.Unlock() + if slot.provider != nil { + return errors.New("extension: project context provider already registered") + } + slot.holder, slot.provider = r.pluginID, p + return nil +} + +func (r recordRegistry) RegisterProjectContextProvider(p extension.ProjectContextProvider) error { + reg, ok := r.Registry.(extension.ProjectContextRegistry) + if !ok { + return errors.New("extension: project context registration unavailable") + } + return reg.RegisterProjectContextProvider(p) +} + +func (s *projectContextSlot) resolve(ctx context.Context, req extension.ProjectContextRequest) (extension.ProjectContextBundle, error) { + if s == nil { + return extension.ProjectContextBundle{}, errors.New("extension: project context provider missing") + } + s.mu.RLock() + p, holder := s.provider, s.holder + s.mu.RUnlock() + if p == nil { + return extension.ProjectContextBundle{}, errors.New("extension: project context provider missing") + } + if req.Actor == "" || req.ProjectID == "" { + return extension.ProjectContextBundle{}, errors.New("extension: trusted project scope missing") + } + req.PluginID = holder + bounded, cancel := context.WithTimeout(ctx, 3*time.Second) + defer cancel() + b, err := p.Resolve(bounded, req) + if err != nil { + return extension.ProjectContextBundle{}, err + } + if err := bounded.Err(); err != nil { + return extension.ProjectContextBundle{}, err + } + if b.ProjectID != req.ProjectID || b.Status != extension.ProjectContextReady { + return extension.ProjectContextBundle{}, fmt.Errorf("extension: project context unavailable: %s", b.Status) + } + return b, nil +} + +func (s *projectContextSlot) readEvidence(ctx context.Context, req extension.ProjectEvidenceRequest) (extension.ProjectEvidence, error) { + if s == nil || req.Scope.Actor == "" || req.Scope.ProjectID == "" || req.Reference.ID == "" { + return extension.ProjectEvidence{}, errors.New("extension: trusted evidence scope missing") + } + s.mu.RLock() + p, holder := s.provider, s.holder + s.mu.RUnlock() + if p == nil { + return extension.ProjectEvidence{}, errors.New("extension: project context provider missing") + } + req.Scope.PluginID = holder + bounded, cancel := context.WithTimeout(ctx, 3*time.Second) + defer cancel() + evidence, err := p.ReadEvidence(bounded, req) + if err != nil { + return extension.ProjectEvidence{}, err + } + if err := bounded.Err(); err != nil { + return extension.ProjectEvidence{}, err + } + if evidence.Reference != req.Reference || len([]rune(evidence.Text)) > req.Scope.BudgetChars { + return extension.ProjectEvidence{}, errors.New("extension: project evidence response mismatches reference or budget") + } + return evidence, nil +} + +const workContextBudgetChars = 12000 + +// TaskCreateForWork fixes the project binding while the authenticated command +// dispatch still owns its conversation. Legacy TaskCreate has no such binding. +func (h inprocHost) TaskCreateForWork(kind, title string) (string, error) { + if h.actor == "" || h.actor != h.owner || h.pluginID == "" || h.conversationID == "" || h.caps.st == nil || !h.caps.contexts.active() { + return "", errors.New("extension: trusted project work dispatch required") + } + conv, err := h.caps.st.GetConversation(h.owner, string(h.conversationID)) + if err != nil { + return "", err + } + if conv.ProjectID == "" || (h.hostProjectID != "" && h.hostProjectID != conv.ProjectID) || h.caps.projects == nil || h.caps.projects.work() == nil { + return "", errors.New("extension: trusted project work scope missing") + } + ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second) + defer cancel() + allowed, err := h.caps.projects.work()(ctx, h.actor, conv.ProjectID) + if err != nil || !allowed { + return "", errors.New("extension: project work denied") + } + task, err := h.caps.st.CreateWorkTask(h.owner, h.pluginID, string(conv.ProjectID), string(conv.ID), kind, title) + return task.ID, err +} + +// completeForWork is the dispatch-scoped completion path. The conversation is +// reloaded so a plugin's goal/target and a stale dispatch project cannot select +// another project's context. Work authorization is checked on every call. +func (h inprocHost) CompleteForWork(ctx context.Context, req extension.WorkCompletionRequest) (extension.WorkCompletionResult, error) { + if strings.TrimSpace(req.Goal) == "" { + return extension.WorkCompletionResult{}, errors.New("extension: work goal required") + } + if h.caps.st == nil { + return extension.WorkCompletionResult{}, errors.New("extension: trusted work store missing") + } + actor, convID, targetID := h.actor, h.conversationID, string(h.conversationID) + var boundProjectID core.ID + if actor == "" { + if req.TaskID == "" || convID != "" { + return extension.WorkCompletionResult{}, errors.New("extension: persisted work task required for scoped host") + } + binding, err := h.caps.st.WorkTaskScope(h.owner, h.pluginID, req.TaskID) + if err != nil { + return extension.WorkCompletionResult{}, err + } + if binding.Status == "done" || binding.Status == "completed" || binding.Status == "failed" || binding.Status == "cancelled" { + return extension.WorkCompletionResult{}, errors.New("extension: work task already terminal") + } + actor, convID, boundProjectID, targetID = binding.Owner, core.ID(binding.ConversationID), core.ID(binding.ProjectID), binding.TaskID + } else if actor != h.owner || convID == "" || req.TaskID != "" { + return extension.WorkCompletionResult{}, errors.New("extension: trusted work dispatch required") + } + conv, err := h.caps.st.GetConversation(h.owner, string(convID)) + if err != nil { + return extension.WorkCompletionResult{}, err + } + if conv.ProjectID == "" || (h.hostProjectID != "" && h.hostProjectID != conv.ProjectID) || (boundProjectID != "" && boundProjectID != conv.ProjectID) { + return extension.WorkCompletionResult{}, errors.New("extension: trusted project scope mismatch") + } + if h.caps.projects == nil || h.caps.projects.work() == nil { + return extension.WorkCompletionResult{}, errors.New("extension: project work authorization missing") + } + allowed, err := h.caps.projects.work()(ctx, actor, conv.ProjectID) + if err != nil { + return extension.WorkCompletionResult{}, err + } + if !allowed { + return extension.WorkCompletionResult{}, errors.New("extension: project work denied") + } + requested := extension.ProjectContextRequest{ + Actor: actor, ProjectID: conv.ProjectID, ConversationID: conv.ID, TaskID: core.ID(req.TaskID), + Goal: req.Goal, TargetID: req.TargetID, BudgetChars: workContextBudgetChars, + } + bundle, err := h.caps.contexts.resolve(ctx, requested) + if err != nil { + return extension.WorkCompletionResult{}, err + } + evidenceText, usedEvidence, err := h.readWorkEvidence(ctx, requested, bundle, req.EvidenceRefs) + if err != nil { + return extension.WorkCompletionResult{}, err + } + prompt, manifest, err := assembleWorkCompletionWithEvidence(req.Goal, bundle, workContextBudgetChars, evidenceText, usedEvidence) + if err != nil { + return extension.WorkCompletionResult{}, err + } + manifest.ProjectID = conv.ProjectID + manifest.TaskID = req.TaskID + route := runtime.ResolveChatModel(core.AgentProfile{}, h.caps.set.Effective(h.owner, "")) + manifest.Model = route.Model + if route.Model == "" || route.Base == "" { + return extension.WorkCompletionResult{}, errors.New("llm route not configured") + } + // Resolve can be slow. A member may be revoked while the provider is + // assembling summaries, so check the live work policy again at send time. + allowed, err = h.caps.projects.work()(ctx, actor, conv.ProjectID) + if err != nil { + return extension.WorkCompletionResult{}, err + } + if !allowed { + return extension.WorkCompletionResult{}, errors.New("extension: project work denied") + } + current, err := h.caps.contexts.resolve(ctx, requested) + if err != nil { + return extension.WorkCompletionResult{}, err + } + if err := projectcontext.ValidateCurrent(manifest, current); err != nil { + return extension.WorkCompletionResult{}, err + } + for _, used := range manifest.ReadEvidence { + found := false + for _, ref := range current.Evidence { + if ref.ID != used.ReferenceID || ref.SourceID != used.SourceID || ref.Revision != used.Revision || ref.Hash != used.SourceHash { + continue + } + scope := requested + scope.BudgetChars = 8000 + now, err := h.caps.contexts.readEvidence(ctx, extension.ProjectEvidenceRequest{Scope: scope, Reference: ref}) + if err != nil { + return extension.WorkCompletionResult{}, err + } + if fmt.Sprintf("%x", sha256.Sum256([]byte(now.Text))) == used.InputSHA256 { + found = true + } + break + } + if !found { + return extension.WorkCompletionResult{}, errors.New("extension: requested work evidence changed before model send") + } + } + attemptID, err := projectcontext.NewAttemptID() + if err != nil { + return extension.WorkCompletionResult{}, err + } + manifest.AttemptID, manifest.Delivery = attemptID, "attempted" + if err := h.caps.st.AppendContextManifest(h.owner, targetID, manifest); err != nil { + return extension.WorkCompletionResult{}, err + } + text, err := completeChat(ctx, route.Base, route.Key, route.Model, prompt) + if err != nil { + manifest.Delivery = "unknown" + _ = h.caps.st.AppendContextManifest(h.owner, targetID, manifest) + return extension.WorkCompletionResult{}, err + } + manifest.Delivery = "responded" + if err := h.caps.st.AppendContextManifest(h.owner, targetID, manifest); err != nil { + return extension.WorkCompletionResult{}, err + } + return extension.WorkCompletionResult{Text: text, Manifest: manifest}, nil +} + +func assembleWorkCompletion(goal string, bundle extension.ProjectContextBundle, budget int) (string, extension.ProjectContextManifest, error) { + return assembleWorkCompletionWithEvidence(goal, bundle, budget, "", nil) +} + +func assembleWorkCompletionWithEvidence(goal string, bundle extension.ProjectContextBundle, budget int, evidenceText string, used []extension.ProjectContextManifestEntry) (string, extension.ProjectContextManifest, error) { + prefix := "Work goal:\n" + goal + "\n\n" + ref, m, err := projectcontext.Assemble(bundle, budget-len([]rune(prefix))-len([]rune(evidenceText))) + if err != nil { + return "", m, err + } + prompt := prefix + ref + evidenceText + m.BudgetChars = budget + m.ReadEvidence = used + m.InputSHA256 = fmt.Sprintf("%x", sha256.Sum256([]byte(prompt))) + return prompt, m, nil +} + +func (h inprocHost) readWorkEvidence(ctx context.Context, scope extension.ProjectContextRequest, bundle extension.ProjectContextBundle, ids []string) (string, []extension.ProjectContextManifestEntry, error) { + if len(ids) > 4 { + return "", nil, errors.New("extension: too many work evidence references") + } + var b strings.Builder + var used []extension.ProjectContextManifestEntry + seen := make(map[string]bool, len(ids)) + for _, id := range ids { + if id == "" || seen[id] { + return "", nil, errors.New("extension: invalid or duplicate work evidence reference") + } + seen[id] = true + var selected extension.ProjectEvidenceRef + for _, ref := range bundle.Evidence { + if ref.ID == id { + selected = ref + break + } + } + if selected.ID == "" { + return "", nil, errors.New("extension: work evidence reference not in current bundle") + } + readScope := scope + readScope.BudgetChars = 8000 + evidence, err := h.caps.contexts.readEvidence(ctx, extension.ProjectEvidenceRequest{Scope: readScope, Reference: selected}) + if err != nil { + return "", nil, err + } + b.WriteString("\n[Evidence ") + b.WriteString(selected.ID) + b.WriteString(" source ") + b.WriteString(selected.SourceID) + b.WriteString(" revision ") + b.WriteString(selected.Revision) + b.WriteString("]\n") + b.WriteString(evidence.Text) + used = append(used, extension.ProjectContextManifestEntry{ReferenceID: selected.ID, SourceID: selected.SourceID, Revision: selected.Revision, SourceHash: selected.Hash, + InputSHA256: fmt.Sprintf("%x", sha256.Sum256([]byte(evidence.Text)))}) + } + return b.String(), used, nil +} diff --git a/internal/app/project_context_test.go b/internal/app/project_context_test.go new file mode 100644 index 0000000..833615b --- /dev/null +++ b/internal/app/project_context_test.go @@ -0,0 +1,347 @@ +package app + +import ( + "context" + "crypto/sha256" + "encoding/json" + "fmt" + "net/http" + "net/http/httptest" + "path/filepath" + "strings" + "testing" + + "agent-system-backend/internal/settings" + "agent-system-backend/internal/store" + "agent-system-backend/pkg/extension" + core "agent-system-core" +) + +type testProjectContextProvider struct { + resolve func(extension.ProjectContextRequest) (extension.ProjectContextBundle, error) + read func(extension.ProjectEvidenceRequest) (extension.ProjectEvidence, error) +} + +type deadlineContextProvider struct{ sawDeadline *bool } + +func (p deadlineContextProvider) Resolve(ctx context.Context, req extension.ProjectContextRequest) (extension.ProjectContextBundle, error) { + _, *p.sawDeadline = ctx.Deadline() + return extension.ProjectContextBundle{Status: extension.ProjectContextReady, ProjectID: req.ProjectID}, nil +} +func (p deadlineContextProvider) ReadEvidence(ctx context.Context, req extension.ProjectEvidenceRequest) (extension.ProjectEvidence, error) { + _, *p.sawDeadline = ctx.Deadline() + return extension.ProjectEvidence{Reference: req.Reference, Text: "detail"}, nil +} + +func TestProjectContextProviderCallsHaveBoundedDeadline(t *testing.T) { + seen := false + slot := &projectContextSlot{} + if err := (pluginRegistry{pluginID: "rdm", caps: hostcaps{contexts: slot}}).RegisterProjectContextProvider(deadlineContextProvider{sawDeadline: &seen}); err != nil { + t.Fatal(err) + } + if _, err := slot.resolve(context.Background(), extension.ProjectContextRequest{Actor: "alice", ProjectID: "p1"}); err != nil || !seen { + t.Fatalf("Resolve missing bounded context: seen=%v err=%v", seen, err) + } + seen = false + if _, err := slot.readEvidence(context.Background(), extension.ProjectEvidenceRequest{Scope: extension.ProjectContextRequest{Actor: "alice", ProjectID: "p1", BudgetChars: 100}, Reference: extension.ProjectEvidenceRef{ID: "r1"}}); err != nil || !seen { + t.Fatalf("ReadEvidence missing bounded context: seen=%v err=%v", seen, err) + } +} + +func (p testProjectContextProvider) Resolve(_ context.Context, req extension.ProjectContextRequest) (extension.ProjectContextBundle, error) { + return p.resolve(req) +} + +func (p testProjectContextProvider) ReadEvidence(_ context.Context, req extension.ProjectEvidenceRequest) (extension.ProjectEvidence, error) { + if p.read != nil { + return p.read(req) + } + return extension.ProjectEvidence{}, nil +} + +func TestProjectContextRegistryOptionalAndSingleOwner(t *testing.T) { + slot := &projectContextSlot{} + reg := recordRegistry{Registry: pluginRegistry{pluginID: "rdm", caps: hostcaps{contexts: slot}}} + optional, ok := any(reg).(extension.ProjectContextRegistry) + if !ok { + t.Fatal("record registry hides optional project context registration") + } + provider := testProjectContextProvider{resolve: func(extension.ProjectContextRequest) (extension.ProjectContextBundle, error) { + return extension.ProjectContextBundle{Status: extension.ProjectContextReady}, nil + }} + if err := optional.RegisterProjectContextProvider(provider); err != nil { + t.Fatal(err) + } + if err := optional.RegisterProjectContextProvider(provider); err == nil { + t.Fatal("duplicate provider replaced the first provider") + } + if err := (compiledRegistry{}).RegisterProjectContextProvider(provider); err == nil { + t.Fatal("declarative provider accepted") + } +} + +func TestRequiredProjectContextFailsWhenProviderNotAssembled(t *testing.T) { + slot := &projectContextSlot{} + if err := requireProjectContextProvider(true, slot); err == nil { + t.Fatal("required project context silently degraded") + } + if err := requireProjectContextProvider(false, slot); err != nil { + t.Fatalf("optional legacy path blocked: %v", err) + } + provider := testProjectContextProvider{resolve: func(extension.ProjectContextRequest) (extension.ProjectContextBundle, error) { + return extension.ProjectContextBundle{}, nil + }} + if err := (pluginRegistry{pluginID: "rdm", caps: hostcaps{contexts: slot}}).RegisterProjectContextProvider(provider); err != nil { + t.Fatal(err) + } + if err := requireProjectContextProvider(true, slot); err != nil { + t.Fatalf("registered provider rejected: %v", err) + } +} + +func TestCompleteForWorkUsesStoredScopeAndActualInputManifest(t *testing.T) { + st, err := store.OpenSQLite(filepath.Join(t.TempDir(), "test.db")) + if err != nil { + t.Fatal(err) + } + defer st.Close() + p, err := st.CreateProject("alice", "Project") + if err != nil { + t.Fatal(err) + } + other, err := st.CreateProject("alice", "Other") + if err != nil { + t.Fatal(err) + } + conv, err := st.CreateConversation("alice", p.ID, "Work", "") + if err != nil { + t.Fatal(err) + } + var sent string + modelCalls := 0 + model := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + modelCalls++ + var payload struct { + Messages []struct { + Content string `json:"content"` + } `json:"messages"` + } + if err := json.NewDecoder(r.Body).Decode(&payload); err != nil { + t.Error(err) + } + for _, m := range payload.Messages { + sent += m.Content + } + _, _ = w.Write([]byte(`{"choices":[{"message":{"content":"done"}}]}`)) + })) + defer model.Close() + set := settings.NewService(st) + set.SetSystemOverride(map[string]any{"models": map[string]any{"chat": map[string]any{"baseUrl": model.URL, "model": "test-model"}}}) + projects := &projectLifecycleSlot{} + if err := (pluginRegistry{pluginID: "rdm", caps: hostcaps{projects: projects}}).RegisterProjectLifecycle(completeLifecycle()); err != nil { + t.Fatal(err) + } + contexts := &projectContextSlot{} + called := 0 + oversized := false + revokeDuringResolve := false + revokeDocDuringResolve := false + authEpoch := "duty-1" + provider := testProjectContextProvider{resolve: func(req extension.ProjectContextRequest) (extension.ProjectContextBundle, error) { + called++ + if req.Actor != "alice" || req.ProjectID != core.ID(p.ID) || req.ConversationID != core.ID(conv.ID) || req.PluginID != "rdm" { + t.Errorf("untrusted scope: %+v", req) + } + item := extension.ProjectContextItem{SourceID: "decision-1", Revision: "r3", Hash: "source-hash", Text: "Authorized summary", Required: true} + if oversized { + item.Text = strings.Repeat("x", workContextBudgetChars+1) + } + if revokeDuringResolve { + projects.mu.Lock() + projects.provider.Work = func(context.Context, string, core.ID) (bool, error) { return false, nil } + projects.mu.Unlock() + } + epoch := authEpoch + if revokeDocDuringResolve { + authEpoch = "duty-2" + revokeDocDuringResolve = false + } + return extension.ProjectContextBundle{Status: extension.ProjectContextReady, ProjectID: core.ID(p.ID), Version: "v7", AuthorizationEpoch: epoch, Items: []extension.ProjectContextItem{item}}, nil + }} + if err := (pluginRegistry{pluginID: "rdm", caps: hostcaps{contexts: contexts}}).RegisterProjectContextProvider(provider); err != nil { + t.Fatal(err) + } + h := inprocHost{caps: hostcaps{st: st, set: set, projects: projects, contexts: contexts}, pluginID: "rdm", owner: "alice", actor: "alice", conversationID: conv.ID, hostProjectID: p.ID} + work := any(h).(extension.WorkCompletionHost) + result, err := work.CompleteForWork(context.Background(), extension.WorkCompletionRequest{Goal: "Draft report"}) + if err != nil { + t.Fatal(err) + } + if result.Text != "done" || !strings.Contains(sent, "Authorized summary") || !strings.Contains(sent, "Draft report") || result.Manifest.InputSHA256 == "" || len(result.Manifest.Included) != 1 { + t.Fatalf("input or manifest mismatch: result=%+v sent=%q", result, sent) + } + if want := fmt.Sprintf("%x", sha256.Sum256([]byte(sent))); result.Manifest.InputSHA256 != want { + t.Fatalf("manifest input hash %q differs from actual request content hash %q", result.Manifest.InputSHA256, want) + } + if got, want := result.Manifest.Included[0].InputSHA256, fmt.Sprintf("%x", sha256.Sum256([]byte("Authorized summary"))); got != want || result.Manifest.Included[0].SourceHash != "source-hash" { + t.Fatalf("manifest does not distinguish source hash and actual text hash: %+v", result.Manifest.Included[0]) + } + if called != 2 { + t.Fatalf("provider called %d times, want initial and pre-send reads", called) + } + stored, err := st.ListContextManifests("alice", string(conv.ID)) + if err != nil || len(stored) != 2 || stored[0].Delivery != "attempted" || stored[1].Delivery != "responded" || stored[0].AttemptID != stored[1].AttemptID || stored[1].InputSHA256 != result.Manifest.InputSHA256 { + t.Fatalf("work completion manifest not persisted: got=%+v err=%v", stored, err) + } + h.hostProjectID = other.ID + if _, err := h.CompleteForWork(context.Background(), extension.WorkCompletionRequest{Goal: "Draft report"}); err == nil || called != 2 { + t.Fatal("forged project scope reached provider") + } + h.hostProjectID = p.ID + h.conversationID = "" + if _, err := h.CompleteForWork(context.Background(), extension.WorkCompletionRequest{Goal: "Draft report"}); err == nil || called != 2 { + t.Fatal("scoped host without dispatch scope reached provider") + } + h.conversationID = conv.ID + projects.mu.Lock() + projects.provider.Work = func(context.Context, string, core.ID) (bool, error) { return false, nil } + projects.mu.Unlock() + if _, err := h.CompleteForWork(context.Background(), extension.WorkCompletionRequest{Goal: "Draft report"}); err == nil || called != 2 { + t.Fatal("revoked project work reached provider") + } + projects.mu.Lock() + projects.provider.Work = func(context.Context, string, core.ID) (bool, error) { return true, nil } + projects.mu.Unlock() + oversized = true + if _, err := h.CompleteForWork(context.Background(), extension.WorkCompletionRequest{Goal: "Draft report"}); err == nil || modelCalls != 1 { + t.Fatal("required context was dropped and model still called") + } + oversized = false + revokeDuringResolve = true + if _, err := h.CompleteForWork(context.Background(), extension.WorkCompletionRequest{Goal: "Draft report"}); err == nil || modelCalls != 1 { + t.Fatal("revocation during context resolution reached model") + } + projects.mu.Lock() + projects.provider.Work = func(context.Context, string, core.ID) (bool, error) { return true, nil } + projects.mu.Unlock() + revokeDuringResolve = false + revokeDocDuringResolve = true + if _, err := h.CompleteForWork(context.Background(), extension.WorkCompletionRequest{Goal: "Draft report"}); err == nil || modelCalls != 1 { + t.Fatal("document duty revoked while project membership remains reached model") + } + taskHost := any(h).(extension.WorkTaskHost) + taskID, err := taskHost.TaskCreateForWork("compose", "Draft report") + if err != nil { + t.Fatal(err) + } + scoped := inprocHost{caps: h.caps, pluginID: "rdm", owner: "alice"} + if _, err := scoped.TaskCreateForWork("compose", "Forged"); err == nil { + t.Fatal("scoped host created a work task without dispatch scope") + } + if _, err := scoped.CompleteForWork(context.Background(), extension.WorkCompletionRequest{Goal: "Resume report", TaskID: taskID}); err != nil || modelCalls != 2 { + t.Fatalf("persisted plugin task did not restore trusted work scope: err=%v calls=%d", err, modelCalls) + } + otherPlugin := inprocHost{caps: h.caps, pluginID: "other", owner: "alice"} + if _, err := otherPlugin.CompleteForWork(context.Background(), extension.WorkCompletionRequest{Goal: "Steal task", TaskID: taskID}); err == nil || modelCalls != 2 { + t.Fatal("other plugin reused task binding") + } + otherOwner := inprocHost{caps: h.caps, pluginID: "rdm", owner: "bob"} + if _, err := otherOwner.CompleteForWork(context.Background(), extension.WorkCompletionRequest{Goal: "Steal task", TaskID: taskID}); err == nil || modelCalls != 2 { + t.Fatal("other owner reused task binding") + } + legacyTaskID, err := h.TaskCreate("legacy", "Unbound", string(conv.ID)) + if err != nil { + t.Fatal(err) + } + if _, err := scoped.CompleteForWork(context.Background(), extension.WorkCompletionRequest{Goal: "Old task", TaskID: legacyTaskID}); err == nil || modelCalls != 2 { + t.Fatal("old unbound task was treated as trusted work scope") + } + if _, err := st.DB().Exec(`UPDATE conversations SET project_id=? WHERE id=?`, other.ID, conv.ID); err != nil { + t.Fatal(err) + } + if _, err := scoped.CompleteForWork(context.Background(), extension.WorkCompletionRequest{Goal: "Moved project", TaskID: taskID}); err == nil || modelCalls != 2 { + t.Fatal("task silently followed a conversation to another project") + } +} + +func TestWorkContextBudgetKeepsRequiredEvidenceFirst(t *testing.T) { + bundle := extension.ProjectContextBundle{Status: extension.ProjectContextReady, ProjectID: "project-1", Items: []extension.ProjectContextItem{ + {SourceID: "optional", Text: "12345678"}, + {SourceID: "required", Text: "must keep", Required: true}, + }} + prompt, manifest, err := assembleWorkCompletion("goal", bundle, 140) + if err != nil { + t.Fatal(err) + } + if !strings.Contains(prompt, "must keep") || strings.Contains(prompt, "12345678") { + t.Fatalf("required evidence lost to optional evidence: %q", prompt) + } + if len([]rune(prompt)) > 140 { + t.Fatalf("final prompt exceeded the budget: %d", len([]rune(prompt))) + } + if len(manifest.Included) != 1 || manifest.Included[0].SourceID != "required" || len(manifest.Dropped) != 1 || manifest.Dropped[0].SourceID != "optional" { + t.Fatalf("manifest does not match selected input: %+v", manifest) + } +} + +func TestCompleteForWorkReadsOnlyCurrentBundleEvidence(t *testing.T) { + st, err := store.OpenSQLite(filepath.Join(t.TempDir(), "evidence.db")) + if err != nil { + t.Fatal(err) + } + defer st.Close() + p, err := st.CreateProject("alice", "P") + if err != nil { + t.Fatal(err) + } + conv, err := st.CreateConversation("alice", p.ID, "C", "") + if err != nil { + t.Fatal(err) + } + var sent string + model := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + var body struct { + Messages []struct { + Content string `json:"content"` + } `json:"messages"` + } + _ = json.NewDecoder(r.Body).Decode(&body) + for _, m := range body.Messages { + sent += m.Content + } + _, _ = w.Write([]byte(`{"choices":[{"message":{"content":"done"}}]}`)) + })) + defer model.Close() + set := settings.NewService(st) + set.SetSystemOverride(map[string]any{"models": map[string]any{"chat": map[string]any{"baseUrl": model.URL, "model": "test-model"}}}) + projects := &projectLifecycleSlot{} + if err := (pluginRegistry{pluginID: "rdm", caps: hostcaps{projects: projects}}).RegisterProjectLifecycle(completeLifecycle()); err != nil { + t.Fatal(err) + } + contexts := &projectContextSlot{} + ref := extension.ProjectEvidenceRef{ID: "ref-1", SourceID: "doc-1", Revision: "r2", Hash: "source-hash"} + readCalls := 0 + provider := testProjectContextProvider{ + resolve: func(req extension.ProjectContextRequest) (extension.ProjectContextBundle, error) { + return extension.ProjectContextBundle{Status: extension.ProjectContextReady, ProjectID: req.ProjectID, Evidence: []extension.ProjectEvidenceRef{ref}}, nil + }, + read: func(req extension.ProjectEvidenceRequest) (extension.ProjectEvidence, error) { + readCalls++ + if req.Reference != ref || req.Scope.ProjectID != core.ID(p.ID) { + t.Errorf("untrusted evidence scope: %+v", req) + } + return extension.ProjectEvidence{Reference: req.Reference, Text: "current restricted detail"}, nil + }, + } + if err := (pluginRegistry{pluginID: "rdm", caps: hostcaps{contexts: contexts}}).RegisterProjectContextProvider(provider); err != nil { + t.Fatal(err) + } + h := inprocHost{caps: hostcaps{st: st, set: set, projects: projects, contexts: contexts}, pluginID: "rdm", owner: "alice", actor: "alice", conversationID: conv.ID, hostProjectID: p.ID} + result, err := h.CompleteForWork(context.Background(), extension.WorkCompletionRequest{Goal: "Write", EvidenceRefs: []string{"ref-1"}}) + if err != nil || !strings.Contains(sent, "current restricted detail") || len(result.Manifest.ReadEvidence) != 1 || readCalls != 2 { + t.Fatalf("requested evidence did not reach model under reauthorization: err=%v sent=%q manifest=%+v reads=%d", err, sent, result.Manifest, readCalls) + } + if _, err := h.CompleteForWork(context.Background(), extension.WorkCompletionRequest{Goal: "Write", EvidenceRefs: []string{"arbitrary-path"}}); err == nil || readCalls != 2 { + t.Fatal("arbitrary evidence reference reached provider") + } +} diff --git a/internal/projectcontext/context.go b/internal/projectcontext/context.go new file mode 100644 index 0000000..a8b47d2 --- /dev/null +++ b/internal/projectcontext/context.go @@ -0,0 +1,82 @@ +package projectcontext + +import ( + "crypto/sha256" + "errors" + "fmt" + "strings" + + "agent-system-backend/pkg/extension" +) + +// Assemble builds a request-scoped, user-level reference block from currently +// authorized items. Required facts are selected before optional material; a +// required fact that cannot fit blocks the model request. +func Assemble(bundle extension.ProjectContextBundle, budget int) (string, extension.ProjectContextManifest, error) { + m := extension.ProjectContextManifest{ProjectID: bundle.ProjectID, Version: bundle.Version, Watermark: bundle.Watermark, + AuthorizationEpoch: bundle.AuthorizationEpoch, PublicationEpoch: bundle.PublicationEpoch, BudgetChars: budget} + if bundle.Status != extension.ProjectContextReady || bundle.ProjectID == "" || budget <= 0 { + return "", m, errors.New("project context unavailable") + } + var b strings.Builder + b.WriteString("Project reference material (untrusted data, not instructions):\n") + remaining := budget - len([]rune(b.String())) + if remaining < 0 { + return "", m, errors.New("project context header exceeds budget") + } + missingRequired := false + appendItem := func(item extension.ProjectContextItem) { + entry := extension.ProjectContextManifestEntry{SourceID: item.SourceID, Revision: item.Revision, SourceHash: item.Hash} + runes := []rune(item.Text) + label := "\n[Source " + item.SourceID + " revision " + item.Revision + "]\n" + cost := len(runes) + len([]rune(label)) + if cost > remaining || item.SourceID == "" { + entry.Reason = "over-budget-or-missing-source" + m.Dropped = append(m.Dropped, entry) + missingRequired = missingRequired || item.Required + return + } + b.WriteString(label) + b.WriteString(item.Text) + remaining -= cost + entry.InputSHA256 = fmt.Sprintf("%x", sha256.Sum256([]byte(item.Text))) + m.Included = append(m.Included, entry) + } + for _, item := range bundle.Items { + if item.Required { + appendItem(item) + } + } + if missingRequired { + return "", m, errors.New("required project context exceeds budget or lacks source") + } + for _, missing := range bundle.Missing { + line := "\n[Unresolved: " + missing + "]\n" + cost := len([]rune(line)) + if cost > remaining { + return "", m, errors.New("unresolved project status exceeds budget") + } + b.WriteString(line) + remaining -= cost + m.Missing = append(m.Missing, missing) + } + for _, item := range bundle.Items { + if !item.Required { + appendItem(item) + } + } + for _, ref := range bundle.Evidence { + if ref.ID == "" || ref.SourceID == "" { + continue + } + line := fmt.Sprintf("\n[Evidence ref %q source %q revision %q]\n", ref.ID, ref.SourceID, ref.Revision) + cost := len([]rune(line)) + if cost > remaining { + continue + } + b.WriteString(line) + remaining -= cost + m.OfferedReferences = append(m.OfferedReferences, ref) + } + return b.String(), m, nil +} diff --git a/internal/projectcontext/context_test.go b/internal/projectcontext/context_test.go new file mode 100644 index 0000000..bf26a3a --- /dev/null +++ b/internal/projectcontext/context_test.go @@ -0,0 +1,43 @@ +package projectcontext + +import ( + "strings" + "testing" + + "agent-system-backend/pkg/extension" +) + +func TestAssembleOffersOnlyBudgetedEvidenceReferences(t *testing.T) { + bundle := extension.ProjectContextBundle{Status: extension.ProjectContextReady, ProjectID: "p1", Evidence: []extension.ProjectEvidenceRef{{ID: "ref-1", SourceID: "doc-1", Revision: "r2"}, {ID: strings.Repeat("x", 1000), SourceID: "doc-2"}}} + text, manifest, err := Assemble(bundle, 180) + if err != nil { + t.Fatal(err) + } + if !strings.Contains(text, "ref-1") || strings.Contains(text, strings.Repeat("x", 1000)) || len(manifest.OfferedReferences) != 1 || manifest.OfferedReferences[0].ID != "ref-1" { + t.Fatalf("reference offer does not match model input: text=%q manifest=%+v", text, manifest) + } +} + +func TestValidateCurrentRejectsWithdrawnOfferedReference(t *testing.T) { + ref := extension.ProjectEvidenceRef{ID: "ref-1", SourceID: "doc-1", Revision: "r2"} + old := extension.ProjectContextBundle{Status: extension.ProjectContextReady, ProjectID: "p1", Evidence: []extension.ProjectEvidenceRef{ref}} + _, manifest, err := Assemble(old, 180) + if err != nil { + t.Fatal(err) + } + current := extension.ProjectContextBundle{Status: extension.ProjectContextReady, ProjectID: "p1"} + if err := ValidateCurrent(manifest, current); err == nil { + t.Fatal("reference withdrawn before model send was still offered") + } +} + +func TestAssembleIncludesUnresolvedMissingStatus(t *testing.T) { + bundle := extension.ProjectContextBundle{Status: extension.ProjectContextReady, ProjectID: "p1", Missing: []string{"unresolved conflict"}} + text, manifest, err := Assemble(bundle, 180) + if err != nil { + t.Fatal(err) + } + if !strings.Contains(text, "unresolved conflict") || len(manifest.Missing) != 1 { + t.Fatalf("missing status hidden from model or receipt: text=%q manifest=%+v", text, manifest) + } +} diff --git a/internal/projectcontext/receipt.go b/internal/projectcontext/receipt.go new file mode 100644 index 0000000..5cd9b2d --- /dev/null +++ b/internal/projectcontext/receipt.go @@ -0,0 +1,57 @@ +package projectcontext + +import ( + "crypto/rand" + "crypto/sha256" + "errors" + "fmt" + + "agent-system-backend/pkg/extension" +) + +func NewAttemptID() (string, error) { + var id [16]byte + if _, err := rand.Read(id[:]); err != nil { + return "", err + } + return fmt.Sprintf("%x", id), nil +} + +// ValidateCurrent rejects a stale candidate when a later provider read has +// changed the permission/publication epoch or any source actually selected for +// model input. Candidate-only references do not count as model evidence. +func ValidateCurrent(m extension.ProjectContextManifest, b extension.ProjectContextBundle) error { + if b.Status != extension.ProjectContextReady || b.ProjectID != m.ProjectID || b.Version != m.Version || + b.Watermark != m.Watermark || b.AuthorizationEpoch != m.AuthorizationEpoch || b.PublicationEpoch != m.PublicationEpoch { + return errors.New("project context changed before model send") + } + for _, used := range m.Included { + found := false + for _, item := range b.Items { + if item.SourceID != used.SourceID || item.Revision != used.Revision || item.Hash != used.SourceHash { + continue + } + actual := fmt.Sprintf("%x", sha256.Sum256([]byte(item.Text))) + if actual == used.InputSHA256 { + found = true + break + } + } + if !found { + return fmt.Errorf("project context source %q changed before model send", used.SourceID) + } + } + for _, offered := range m.OfferedReferences { + found := false + for _, current := range b.Evidence { + if current == offered { + found = true + break + } + } + if !found { + return fmt.Errorf("project evidence reference %q withdrawn before model send", offered.ID) + } + } + return nil +} diff --git a/internal/runmgr/project_context.go b/internal/runmgr/project_context.go new file mode 100644 index 0000000..5236b45 --- /dev/null +++ b/internal/runmgr/project_context.go @@ -0,0 +1,226 @@ +package runmgr + +import ( + "context" + "crypto/sha256" + "errors" + "fmt" + "strings" + + agentllm "agent-native/runtime/llm" + + "agent-system-backend/internal/projectcontext" + "agent-system-backend/pkg/extension" + core "agent-system-core" +) + +// SetProjectContextResolver activates project-context reads for persisted runs. +// Nil preserves the existing non-project and pre-provider behavior. +func (m *Manager) SetProjectContextResolver(fn func(context.Context, extension.ProjectContextRequest) (extension.ProjectContextBundle, error)) { + m.projectContextResolver = fn +} + +func (m *Manager) SetProjectEvidenceReader(fn func(context.Context, extension.ProjectEvidenceRequest) (extension.ProjectEvidence, error)) { + m.projectEvidenceReader = fn +} + +func (c *runCtx) ProjectContextEnabled() bool { + return c.run.ProjectID != "" && c.mgr.projectContextResolver != nil +} + +func (c *runCtx) contextRequest() extension.ProjectContextRequest { + c.projectContextMu.Lock() + defer c.projectContextMu.Unlock() + return c.projectContextReq +} + +func (c *runCtx) setContextRequest(req extension.ProjectContextRequest) { + c.projectContextMu.Lock() + c.projectContextReq = req + c.projectContextMu.Unlock() +} + +type projectEvidenceRecord struct { + ref extension.ProjectEvidenceRef + text string +} + +// ContextEvidenceInRequest reports only details still present in the actual +// model request after transcript compaction and window cleanup. +func (c *runCtx) ContextEvidenceInRequest(messages []agentllm.Message) []extension.ProjectContextManifestEntry { + c.projectEvidenceMu.Lock() + defer c.projectEvidenceMu.Unlock() + var out []extension.ProjectContextManifestEntry + for _, used := range c.projectEvidenceUsed { + for _, msg := range messages { + if !strings.Contains(msg.Content, used.text) { + continue + } + out = append(out, extension.ProjectContextManifestEntry{ + ReferenceID: used.ref.ID, + SourceID: used.ref.SourceID, Revision: used.ref.Revision, SourceHash: used.ref.Hash, + InputSHA256: fmt.Sprintf("%x", sha256.Sum256([]byte(used.text))), + }) + break + } + } + return out +} + +// ProjectEvidence accepts only a stable reference listed by a fresh Resolve. +// The provider must reauthorize the selected resource when reading its body. +func (c *runCtx) ProjectEvidence(ctx context.Context, id string) (extension.ProjectEvidence, error) { + if id == "" || c.mgr.projectEvidenceReader == nil || !c.ProjectContextEnabled() { + return extension.ProjectEvidence{}, errors.New("project evidence unavailable") + } + prior := c.contextRequest() + budget := prior.BudgetChars + if budget <= 0 { + budget = 12000 + } + bundle, active, err := c.ProjectContext(ctx, prior.Goal, budget) + if err != nil || !active { + return extension.ProjectEvidence{}, errors.New("project evidence scope unavailable") + } + for _, ref := range bundle.Evidence { + if ref.ID != id { + continue + } + scope := c.contextRequest() + scope.BudgetChars = 8000 + evidence, err := c.mgr.projectEvidenceReader(ctx, extension.ProjectEvidenceRequest{Scope: scope, Reference: ref}) + if err != nil { + return extension.ProjectEvidence{}, err + } + if evidence.Reference != ref || strings.TrimSpace(evidence.Text) == "" || len([]rune(evidence.Text)) > 8000 { + return extension.ProjectEvidence{}, errors.New("project evidence response does not match authorized reference or budget") + } + c.projectEvidenceMu.Lock() + c.projectEvidenceUsed = append(c.projectEvidenceUsed, projectEvidenceRecord{ref: ref, text: evidence.Text}) + c.projectEvidenceMu.Unlock() + return evidence, nil + } + return extension.ProjectEvidence{}, errors.New("project evidence reference not in current bundle") +} + +// ProjectContext reloads identity and project from trusted run and conversation +// rows on every model request, including a resumed or background run. +func (c *runCtx) ProjectContext(ctx context.Context, goal string, budget int) (extension.ProjectContextBundle, bool, error) { + if c.run.ProjectID == "" || c.mgr.projectContextResolver == nil { + return extension.ProjectContextBundle{}, false, nil + } + if c.mgr.projectWorkAuthorization == nil { + return extension.ProjectContextBundle{}, true, errors.New("project work authorization missing") + } + run, err := c.mgr.st.GetRunByID(string(c.run.ID)) + if err != nil { + return extension.ProjectContextBundle{}, true, err + } + if run.Owner == "" || run.Owner != c.run.Owner || run.ProjectID != c.run.ProjectID || run.ConversationID != c.run.ConversationID { + return extension.ProjectContextBundle{}, true, errors.New("persisted run scope mismatch") + } + conv, err := c.mgr.st.GetConversation(string(run.Owner), string(run.ConversationID)) + if err != nil { + return extension.ProjectContextBundle{}, true, err + } + if conv.ProjectID != run.ProjectID { + return extension.ProjectContextBundle{}, true, errors.New("persisted conversation scope mismatch") + } + if err := c.mgr.authorizeProjectWork(ctx, string(run.Owner), run.ProjectID); err != nil { + return extension.ProjectContextBundle{}, true, err + } + req := extension.ProjectContextRequest{ + Actor: string(run.Owner), ProjectID: run.ProjectID, ConversationID: run.ConversationID, + RunID: run.ID, Goal: goal, BudgetChars: budget, + } + bundle, err := c.mgr.projectContextResolver(ctx, req) + if err != nil { + return extension.ProjectContextBundle{}, true, err + } + if bundle.ProjectID != run.ProjectID || bundle.Status != extension.ProjectContextReady { + return extension.ProjectContextBundle{}, true, errors.New("project context unavailable for trusted run") + } + c.setContextRequest(req) + return bundle, true, nil +} + +// SaveContextManifest persists a receipt for the exact request prepared by the +// runtime. It does not store model-visible text or candidate-only references. +func (c *runCtx) SaveContextManifest(ctx context.Context, m extension.ProjectContextManifest) error { + if err := ctx.Err(); err != nil { + return err + } + run, err := c.mgr.st.GetRunByID(string(c.run.ID)) + if err != nil { + return err + } + if run.Owner != c.run.Owner || run.ProjectID != c.run.ProjectID || run.ConversationID != c.run.ConversationID || m.ProjectID != run.ProjectID { + return errors.New("context manifest run scope mismatch") + } + // A response/unknown receipt records an already attempted transport. A + // revocation after send must not erase that audit fact. + if m.Delivery != "responded" && m.Delivery != "unknown" { + if err := c.mgr.authorizeProjectWork(ctx, string(run.Owner), core.ID(run.ProjectID)); err != nil { + return err + } + } + if c.mgr.projectContextResolver != nil && m.Delivery != "responded" && m.Delivery != "unknown" { + resolvedReq := c.contextRequest() + if resolvedReq.RunID != run.ID || resolvedReq.ProjectID != run.ProjectID { + return errors.New("context manifest missing resolved run scope") + } + current, err := c.mgr.projectContextResolver(ctx, resolvedReq) + if err != nil { + return err + } + if err := projectcontext.ValidateCurrent(m, current); err != nil { + return err + } + c.projectEvidenceMu.Lock() + pastReads := append([]projectEvidenceRecord(nil), c.projectEvidenceUsed...) + c.projectEvidenceMu.Unlock() + for _, used := range pastReads { + stillListed := false + for _, ref := range current.Evidence { + if ref == used.ref { + stillListed = true + break + } + } + if !stillListed || c.mgr.projectEvidenceReader == nil { + return errors.New("previous project evidence no longer authorized") + } + scope := resolvedReq + scope.BudgetChars = 8000 + now, err := c.mgr.projectEvidenceReader(ctx, extension.ProjectEvidenceRequest{Scope: scope, Reference: used.ref}) + if err != nil || now.Reference != used.ref || now.Text != used.text { + return errors.New("previous project evidence changed before model send") + } + } + for _, used := range m.ReadEvidence { + if c.mgr.projectEvidenceReader == nil { + return errors.New("project evidence reader missing at model send") + } + matched := false + for _, ref := range current.Evidence { + if ref.ID != used.ReferenceID || ref.SourceID != used.SourceID || ref.Revision != used.Revision || ref.Hash != used.SourceHash { + continue + } + scope := resolvedReq + scope.BudgetChars = 8000 + evidence, err := c.mgr.projectEvidenceReader(ctx, extension.ProjectEvidenceRequest{Scope: scope, Reference: ref}) + if err != nil { + return err + } + if evidence.Reference == ref && fmt.Sprintf("%x", sha256.Sum256([]byte(evidence.Text))) == used.InputSHA256 { + matched = true + break + } + } + if !matched { + return errors.New("project evidence changed before model send") + } + } + } + return c.mgr.st.AppendContextManifest(string(run.Owner), string(run.ID), m) +} diff --git a/internal/runmgr/project_context_test.go b/internal/runmgr/project_context_test.go new file mode 100644 index 0000000..94f6a4e --- /dev/null +++ b/internal/runmgr/project_context_test.go @@ -0,0 +1,194 @@ +package runmgr + +import ( + "context" + "sync" + "testing" + + agentllm "agent-native/runtime/llm" + "agent-system-backend/internal/projectcontext" + "agent-system-backend/pkg/extension" + core "agent-system-core" +) + +func TestProjectContextUsesPersistedRunAndRefreshesAuthorization(t *testing.T) { + st := openRecoveryStore(t) + m := newRecoveryManager(t, st) + conv := recoveryConversation(t, st) + run := recoveryRun(t, st, conv, core.RunPending) + allowed := true + m.SetProjectWorkAuthorization(func(context.Context, string, core.ID) (bool, error) { return allowed, nil }) + called := 0 + m.SetProjectContextResolver(func(_ context.Context, req extension.ProjectContextRequest) (extension.ProjectContextBundle, error) { + called++ + if req.Actor != string(run.Owner) || req.ProjectID != run.ProjectID || req.ConversationID != run.ConversationID || req.RunID != run.ID { + t.Errorf("context scope did not come from persisted run: %+v", req) + } + return extension.ProjectContextBundle{Status: extension.ProjectContextReady, ProjectID: req.ProjectID}, nil + }) + rc := &runCtx{mgr: m, run: run, ctx: context.Background()} + if _, active, err := rc.ProjectContext(context.Background(), "work goal", 900); err != nil || !active || called != 1 { + t.Fatalf("valid persisted scope rejected: active=%v called=%d err=%v", active, called, err) + } + allowed = false + if _, _, err := rc.ProjectContext(context.Background(), "work goal", 900); err == nil || called != 1 { + t.Fatal("revoked project still reached context provider") + } + allowed = true + rc.run.ProjectID = "forged" + if _, _, err := rc.ProjectContext(context.Background(), "work goal", 900); err == nil || called != 1 { + t.Fatal("in-memory project override reached context provider") + } +} + +func TestProjectContextResumeReloadsCurrentVersionAndAuthorization(t *testing.T) { + st := openRecoveryStore(t) + m := newRecoveryManager(t, st) + conv := recoveryConversation(t, st) + run := recoveryRun(t, st, conv, core.RunInterrupted) + allowed, version := true, "v1" + m.SetProjectWorkAuthorization(func(context.Context, string, core.ID) (bool, error) { return allowed, nil }) + m.SetProjectContextResolver(func(_ context.Context, req extension.ProjectContextRequest) (extension.ProjectContextBundle, error) { + return extension.ProjectContextBundle{Status: extension.ProjectContextReady, ProjectID: req.ProjectID, Version: version}, nil + }) + first := &runCtx{mgr: m, run: run, ctx: context.Background()} + if b, _, err := first.ProjectContext(context.Background(), "work", 900); err != nil || b.Version != "v1" { + t.Fatalf("initial context: %+v err=%v", b, err) + } + reloaded, err := st.GetRunByID(string(run.ID)) + if err != nil { + t.Fatal(err) + } + resumed := &runCtx{mgr: m, run: reloaded, ctx: context.Background()} + version = "v2" + if b, _, err := resumed.ProjectContext(context.Background(), "work", 900); err != nil || b.Version != "v2" { + t.Fatalf("resumed context reused stale version: %+v err=%v", b, err) + } + allowed = false + if _, _, err := resumed.ProjectContext(context.Background(), "work", 900); err == nil { + t.Fatal("resumed context ignored current revocation") + } +} + +func TestContextManifestPersistsByOwnerAndRun(t *testing.T) { + st := openRecoveryStore(t) + m := newRecoveryManager(t, st) + conv := recoveryConversation(t, st) + run := recoveryRun(t, st, conv, core.RunPending) + rc := &runCtx{mgr: m, run: run, ctx: context.Background()} + manifest := extension.ProjectContextManifest{ProjectID: run.ProjectID, Version: "v3", Model: "model-1", InputSHA256: "abc", Included: []extension.ProjectContextManifestEntry{{SourceID: "s1", Revision: "r2", InputSHA256: "def"}}} + if err := rc.SaveContextManifest(context.Background(), manifest); err != nil { + t.Fatal(err) + } + got, err := st.ListContextManifests(string(run.Owner), string(run.ID)) + if err != nil || len(got) != 1 || got[0].Version != "v3" || got[0].Included[0].SourceID != "s1" { + t.Fatalf("manifest missing from persisted run audit: got=%+v err=%v", got, err) + } + other, err := st.ListContextManifests("other", string(run.ID)) + if err != nil || len(other) != 0 { + t.Fatalf("cross-owner manifest visible: got=%+v err=%v", other, err) + } +} + +func TestContextManifestRejectsDocumentDutyRevocationBeforeSend(t *testing.T) { + st := openRecoveryStore(t) + m := newRecoveryManager(t, st) + conv := recoveryConversation(t, st) + run := recoveryRun(t, st, conv, core.RunPending) + m.SetProjectWorkAuthorization(func(context.Context, string, core.ID) (bool, error) { return true, nil }) + epoch := "duty-1" + m.SetProjectContextResolver(func(_ context.Context, req extension.ProjectContextRequest) (extension.ProjectContextBundle, error) { + return extension.ProjectContextBundle{Status: extension.ProjectContextReady, ProjectID: req.ProjectID, Version: "v1", AuthorizationEpoch: epoch, + Items: []extension.ProjectContextItem{{SourceID: "restricted-doc", Text: "private text", Required: true}}}, nil + }) + rc := &runCtx{mgr: m, run: run, ctx: context.Background()} + bundle, active, err := rc.ProjectContext(context.Background(), "work", 900) + if err != nil || !active { + t.Fatalf("initial context: active=%v err=%v", active, err) + } + _, manifest, err := projectcontext.Assemble(bundle, 900) + if err != nil { + t.Fatal(err) + } + manifest.InputSHA256 = "request-hash" + epoch = "duty-2" // project work remains allowed; document duty changed + if err := rc.SaveContextManifest(context.Background(), manifest); err == nil { + t.Fatal("stale document duty accepted at model send") + } + stored, err := st.ListContextManifests(string(run.Owner), string(run.ID)) + if err != nil || len(stored) != 0 { + t.Fatalf("revoked context wrote a send receipt: %+v err=%v", stored, err) + } +} + +func TestProjectEvidenceOnlyReadsCurrentBundleReference(t *testing.T) { + st := openRecoveryStore(t) + m := newRecoveryManager(t, st) + conv := recoveryConversation(t, st) + run := recoveryRun(t, st, conv, core.RunPending) + m.SetProjectWorkAuthorization(func(context.Context, string, core.ID) (bool, error) { return true, nil }) + listed := true + m.SetProjectContextResolver(func(_ context.Context, req extension.ProjectContextRequest) (extension.ProjectContextBundle, error) { + b := extension.ProjectContextBundle{Status: extension.ProjectContextReady, ProjectID: req.ProjectID} + if listed { + b.Evidence = []extension.ProjectEvidenceRef{{ID: "ref-1", SourceID: "doc-1", Revision: "r3", Hash: "source-hash"}} + } + return b, nil + }) + read := 0 + m.SetProjectEvidenceReader(func(_ context.Context, req extension.ProjectEvidenceRequest) (extension.ProjectEvidence, error) { + read++ + if req.Scope.RunID != run.ID || req.Reference.ID != "ref-1" { + t.Errorf("untrusted evidence request: %+v", req) + } + return extension.ProjectEvidence{Reference: req.Reference, Text: "authorized detail"}, nil + }) + rc := &runCtx{mgr: m, run: run, ctx: context.Background()} + if _, err := rc.ProjectEvidence(context.Background(), "forged-ref"); err == nil || read != 0 { + t.Fatal("arbitrary reference reached provider") + } + evidence, err := rc.ProjectEvidence(context.Background(), "ref-1") + if err != nil || evidence.Text != "authorized detail" || read != 1 { + t.Fatalf("current reference rejected: evidence=%+v read=%d err=%v", evidence, read, err) + } + if got := rc.ContextEvidenceInRequest([]agentllm.Message{{Role: agentllm.RoleTool, Content: "authorized detail"}}); len(got) != 1 || got[0].SourceID != "doc-1" { + t.Fatalf("actual evidence read missing from request manifest: %+v", got) + } + if got := rc.ContextEvidenceInRequest([]agentllm.Message{{Role: agentllm.RoleTool, Content: "cleared old tool result"}}); len(got) != 0 { + t.Fatalf("cleared evidence still counted as model input: %+v", got) + } + listed = false + if _, err := rc.ProjectEvidence(context.Background(), "ref-1"); err == nil || read != 1 { + t.Fatal("revoked reference reached provider") + } + if err := rc.SaveContextManifest(context.Background(), extension.ProjectContextManifest{ProjectID: run.ProjectID, InputSHA256: "new-request-hash"}); err == nil { + t.Fatal("new model request accepted after an earlier evidence read was revoked") + } +} + +func TestConcurrentProjectEvidenceReadsKeepTrustedScope(t *testing.T) { + st := openRecoveryStore(t) + m := newRecoveryManager(t, st) + conv := recoveryConversation(t, st) + run := recoveryRun(t, st, conv, core.RunPending) + m.SetProjectWorkAuthorization(func(context.Context, string, core.ID) (bool, error) { return true, nil }) + ref := extension.ProjectEvidenceRef{ID: "r1", SourceID: "doc", Revision: "v1"} + m.SetProjectContextResolver(func(_ context.Context, req extension.ProjectContextRequest) (extension.ProjectContextBundle, error) { + return extension.ProjectContextBundle{Status: extension.ProjectContextReady, ProjectID: req.ProjectID, Evidence: []extension.ProjectEvidenceRef{ref}}, nil + }) + m.SetProjectEvidenceReader(func(_ context.Context, req extension.ProjectEvidenceRequest) (extension.ProjectEvidence, error) { + return extension.ProjectEvidence{Reference: req.Reference, Text: "body"}, nil + }) + rc := &runCtx{mgr: m, run: run, ctx: context.Background()} + var wg sync.WaitGroup + for i := 0; i < 8; i++ { + wg.Add(1) + go func() { + defer wg.Done() + if _, err := rc.ProjectEvidence(context.Background(), "r1"); err != nil { + t.Error(err) + } + }() + } + wg.Wait() +} diff --git a/internal/runmgr/runmgr.go b/internal/runmgr/runmgr.go index b3f3d99..1ed8485 100644 --- a/internal/runmgr/runmgr.go +++ b/internal/runmgr/runmgr.go @@ -33,6 +33,7 @@ import ( "agent-system-backend/internal/store" "agent-system-backend/internal/tools" "agent-system-backend/internal/workflow" + "agent-system-backend/pkg/extension" core "agent-system-core" ) @@ -61,6 +62,8 @@ type Manager struct { knowledgeResourceAllow func(owner string, doc core.KnowledgeDoc) bool knowledgeProjectScope func(context.Context, string, core.ID) (core.ID, error) projectWorkAuthorization func(context.Context, string, core.ID) (bool, error) + projectContextResolver func(context.Context, extension.ProjectContextRequest) (extension.ProjectContextBundle, error) + projectEvidenceReader func(context.Context, extension.ProjectEvidenceRequest) (extension.ProjectEvidence, error) // commands EXT-14:命令即工具的投影与分发源(nil = 未接线,命令不进工具清单)。 commands CommandSource indexer core.DocumentIndexer // 可选索引入口(nil = store 整文档单块) @@ -313,22 +316,26 @@ type sharedRetriever interface { // ---- runCtx 实现(runtime.RunCtx)---- type runCtx struct { - mgr *Manager - run core.Run - prof core.AgentProfile - eff core.EffectiveSettings - ctx context.Context - h *runHandle - userMessage core.Message - attachments []core.ParsedDocument - inputPaths []string - subTask string - subHint string - subAgent string - subModel string // 系统派发覆盖("chat" = 子代理用对话同档;空 = 按 spec) - launch *workflow.Launch - files *filestate.Tracker - permissionMode string + mgr *Manager + run core.Run + projectContextReq extension.ProjectContextRequest + projectContextMu sync.Mutex + projectEvidenceMu sync.Mutex + projectEvidenceUsed []projectEvidenceRecord + prof core.AgentProfile + eff core.EffectiveSettings + ctx context.Context + h *runHandle + userMessage core.Message + attachments []core.ParsedDocument + inputPaths []string + subTask string + subHint string + subAgent string + subModel string // 系统派发覆盖("chat" = 子代理用对话同档;空 = 按 spec) + launch *workflow.Launch + files *filestate.Tracker + permissionMode string // 材料整理(附件即保证):预启动说明注入第 0 回合;ensureMaterial 兜底幂等。 materialNote string materialEnsured bool diff --git a/internal/runtime/llm.go b/internal/runtime/llm.go index d06dae1..8f7e654 100644 --- a/internal/runtime/llm.go +++ b/internal/runtime/llm.go @@ -1,6 +1,7 @@ package runtime import ( + "context" "encoding/json" "fmt" "math" @@ -15,8 +16,10 @@ import ( "agent-native/runtime/policy" agenttools "agent-native/runtime/tools" "agent-system-backend/internal/ctxasm" + "agent-system-backend/internal/projectcontext" "agent-system-backend/internal/prompts" "agent-system-backend/internal/quality" + "agent-system-backend/pkg/extension" core "agent-system-core" ) @@ -68,7 +71,8 @@ const ( toolCardAppend = "card_append" toolCardCommit = "card_commit" // confirm_choice 发起待确认选择卡(材料整理完成后选交付物;I2)。 - toolConfirmChoice = "confirm_choice" + toolConfirmChoice = "confirm_choice" + toolReadProjectEvidence = "read_project_evidence" ) // resolveSystem 经提示词库组装 system 段(基座+环境+命中技能),并产出可观测条目 @@ -260,6 +264,12 @@ func (l *LLMRunner) Execute(rc RunCtx) error { // 组装初始上下文。 hist := rc.History() currentUser, _ := lastAddressed(hist) + if projectContextEnabled(rc) { + // Historical assistant/tool text has no durable resource provenance. A + // later duty revocation cannot safely reauthorize it, so project work + // starts from the current user turn and a fresh provider bundle. + hist = []core.Message{currentUser} + } var sys []agentllm.Message var promptObs []string var taskBlocks []core.PromptBlock @@ -346,6 +356,14 @@ func (l *LLMRunner) Execute(rc RunCtx) error { Parameters: json.RawMessage(`{"type":"object","properties":{}}`), }) } + if reader, ok := rc.(interface { + ProjectContextEnabled() bool + ProjectEvidence(context.Context, string) (extension.ProjectEvidence, error) + }); ok && reader.ProjectContextEnabled() { + decls = append(decls, agentllm.ToolDecl{Name: toolReadProjectEvidence, + Description: "Read one currently authorized project evidence reference offered in this request. Access is checked again at read time.", + Parameters: json.RawMessage(`{"type":"object","properties":{"reference":{"type":"string"}},"required":["reference"]}`)}) + } for _, t := range toolList(rc) { if prof.Tools.ToolAllowed(t.Name) { td := t @@ -367,9 +385,27 @@ func (l *LLMRunner) Execute(rc RunCtx) error { EnvUpdate: envUpdateLine(rc, route.Model), TaskBlocks: taskBlocks, } + if source, ok := rc.(interface { + ProjectContext(context.Context, string, int) (extension.ProjectContextBundle, bool, error) + }); ok { + refresh.ProjectContext = func() (string, *extension.ProjectContextManifest, error) { + const budget = 12000 + bundle, active, err := source.ProjectContext(rc.Context(), currentUserText(rc), budget) + if err != nil || !active { + return "", nil, err + } + body, m, err := projectcontext.Assemble(bundle, budget) + return body, &m, err + } + } return l.runDirect(rc, stream, route.Model, msgs, decls, streamOpts, cardflow, eff, maxTurns, maxTools, refresh) } +func projectContextEnabled(rc RunCtx) bool { + source, ok := rc.(interface{ ProjectContextEnabled() bool }) + return ok && source.ProjectContextEnabled() +} + func permissionModeOf(rc RunCtx) string { if m, ok := rc.(interface{ PermissionMode() string }); ok { return m.PermissionMode() diff --git a/internal/runtime/llm_direct.go b/internal/runtime/llm_direct.go index aa40273..e0307be 100644 --- a/internal/runtime/llm_direct.go +++ b/internal/runtime/llm_direct.go @@ -2,6 +2,7 @@ package runtime import ( "context" + "crypto/sha256" "encoding/json" "fmt" "log" @@ -19,8 +20,10 @@ import ( agentsession "agent-native/runtime/session" agenttools "agent-native/runtime/tools" "agent-system-backend/internal/ctxasm" + "agent-system-backend/internal/projectcontext" "agent-system-backend/internal/squadadapter" "agent-system-backend/internal/wfcompile" + "agent-system-backend/pkg/extension" core "agent-system-core" ) @@ -219,7 +222,51 @@ func (l *LLMRunner) runDirect(rc RunCtx, stream agentllm.StreamFunc, model strin if len(msgs) > 0 { user = msgs[len(msgs)-1] } + var projectErr error + var projectManifest *extension.ProjectContextManifest wrapped := func(ctx context.Context, model string, messages []agentllm.Message, _ []agentllm.ToolDecl, opts agentllm.StreamOptions) <-chan agentllm.Event { + fail := func(err error) <-chan agentllm.Event { + out := make(chan agentllm.Event, 1) + out <- agentllm.Event{Kind: agentllm.KindError, Err: err} + close(out) + return out + } + if projectErr != nil { + return fail(projectErr) + } + var manifest *extension.ProjectContextManifest + var saveManifest func(context.Context, extension.ProjectContextManifest) error + if projectManifest != nil { + copy := *projectManifest + manifest = © + manifest.Model = model + if err := checkProjectInputBudget(rc, messages, decls); err != nil { + return fail(err) + } + wire, err := json.Marshal(messages) + if err != nil { + return fail(err) + } + manifest.InputSHA256 = fmt.Sprintf("%x", sha256.Sum256(wire)) + if ledger, ok := rc.(interface { + ContextEvidenceInRequest([]agentllm.Message) []extension.ProjectContextManifestEntry + }); ok { + manifest.ReadEvidence = ledger.ContextEvidenceInRequest(messages) + } + attemptID, err := projectcontext.NewAttemptID() + if err != nil { + return fail(err) + } + manifest.AttemptID, manifest.Delivery = attemptID, "attempted" + if sink, ok := rc.(interface { + SaveContextManifest(context.Context, extension.ProjectContextManifest) error + }); ok { + saveManifest = sink.SaveContextManifest + if err := saveManifest(ctx, *manifest); err != nil { + return fail(err) + } + } + } if streamOpts.MaxTokens > 0 { opts.MaxTokens = streamOpts.MaxTokens } @@ -227,9 +274,24 @@ func (l *LLMRunner) runDirect(rc RunCtx, stream agentllm.StreamFunc, model strin opts.MessageShape = streamOpts.MessageShape } in := stream(ctx, model, messages, decls, opts) - out := make(chan agentllm.Event) + out := make(chan agentllm.Event, 1) go func() { - defer close(out) + terminalRecorded := false + record := func(status string) error { + if manifest == nil || saveManifest == nil { + return nil + } + manifest.Delivery = status + receiptCtx, cancel := context.WithTimeout(context.Background(), 3*time.Second) + defer cancel() + return saveManifest(receiptCtx, *manifest) + } + defer func() { + if !terminalRecorded { + _ = record("unknown") + } + close(out) + }() for ev := range in { if ev.Kind == agentllm.KindToolCallDelta { st.noteToolDelta(ev) @@ -242,11 +304,29 @@ func (l *LLMRunner) runDirect(rc RunCtx, stream agentllm.StreamFunc, model strin ev.Message.StopReason = agentllm.StopToolCalls } } + if ev.Kind == agentllm.KindMessageEnd || ev.Kind == agentllm.KindError { + status := "unknown" + if ev.Kind == agentllm.KindMessageEnd { + status = "responded" + } + if err := record(status); err != nil { + terminalRecorded = true + select { + case out <- agentllm.Event{Kind: agentllm.KindError, Err: err}: + case <-ctx.Done(): + } + return + } + terminalRecorded = true + } select { case out <- ev: case <-ctx.Done(): return } + if terminalRecorded { + return + } } }() return out @@ -269,7 +349,7 @@ func (l *LLMRunner) runDirect(rc RunCtx, stream agentllm.StreamFunc, model strin // 打开失败降级为无 Session(旧行为:ctxasm 整段组装,不阻断对话)。 var sessStore *agentsession.Store seedRun := true - if sandbox != "" { + if sandbox != "" && !projectContextEnabled(rc) { transcriptPath := filepath.Join(sandbox, ".agentx", "transcript.jsonl") rev := contextRevision(rc.History()) if opened, oerr := agentsession.Open(transcriptPath); oerr == nil { @@ -343,6 +423,14 @@ func (l *LLMRunner) runDirect(rc RunCtx, stream agentllm.StreamFunc, model strin Reminders: func(turn int) []string { var out []string out = append(out, st.reminders(turn)...) + projectErr, projectManifest = nil, nil + if refresh.ProjectContext != nil { + var body string + body, projectManifest, projectErr = refresh.ProjectContext() + if projectErr == nil && body != "" { + out = append(out, body) + } + } // 快慢双速入口前置(SR-B10 实路径):带附件的任务在第 0 回合就把 // 双速选项摆给模型——模型对开场指令的遵循远好于中途提醒(实测 // MiniMax 无视中途 system-reminder,但对工作方法开场指令照办)。 @@ -397,11 +485,11 @@ func (l *LLMRunner) runDirect(rc RunCtx, stream agentllm.StreamFunc, model strin // 只见 160 字摘要——实测会虚构章节、承诺未做之事。开启或异常兜底时才重写。 composeWanted := st.eff.GetBool("runtime.composeFinal", false) if err == nil && res.Err == nil && len(st.evidence) > 0 && composeWanted { - composed = st.composeFinal(stream, model, res.Notice, streamOpts) + composed = st.composeFinal(GuardProjectStream(rc, currentUserText(rc), stream), model, res.Notice, streamOpts) } if err == nil && res.Err == nil && !composed && res.Notice == "" && strings.TrimSpace(st.turnBody) == "" && len(st.evidence) > 0 { // 兜底:工作轮结束但没有任何给用户的正文(罕见),才用重写层补一段。 - composed = st.composeFinal(stream, model, "", streamOpts) + composed = st.composeFinal(GuardProjectStream(rc, currentUserText(rc), stream), model, "", streamOpts) } if res.Notice != "" && !composed { st.finalParts = append(st.finalParts, core.Part{Type: core.PartText, Text: res.Notice}) @@ -939,6 +1027,30 @@ func (st *directRun) runToolCtx(ctx context.Context, name, args string) agenttoo } case toolRunWorkflow: out = st.runRegisteredWorkflow(call.Arguments) + case toolReadProjectEvidence: + var a struct { + Reference string `json:"reference"` + } + if e := json.Unmarshal([]byte(call.Arguments), &a); e != nil || strings.TrimSpace(a.Reference) == "" { + err = fmt.Errorf("project evidence requires a reference") + break + } + reader, ok := st.rc.(interface { + ProjectEvidence(context.Context, string) (extension.ProjectEvidence, error) + }) + if !ok { + err = fmt.Errorf("project evidence unavailable") + break + } + var evidence extension.ProjectEvidence + evidence, err = reader.ProjectEvidence(ctx, a.Reference) + if err == nil { + if evidence.Reference.ID != a.Reference { + err = fmt.Errorf("project evidence reference mismatch") + } else { + out = evidence.Text + } + } case toolCard: out = st.submitCard(call.Arguments) case toolCardBegin: diff --git a/internal/runtime/project_context_test.go b/internal/runtime/project_context_test.go new file mode 100644 index 0000000..d07002f --- /dev/null +++ b/internal/runtime/project_context_test.go @@ -0,0 +1,193 @@ +package runtime + +import ( + "context" + "crypto/sha256" + "encoding/json" + "errors" + "fmt" + "strings" + "testing" + + agentllm "agent-native/runtime/llm" + "agent-squad/squad" + "agent-system-backend/pkg/extension" + core "agent-system-core" +) + +type projectRunCtx struct { + *fakeRunCtx + bundle extension.ProjectContextBundle + readErr error + reads int + manifests []extension.ProjectContextManifest + evidence func(string) (extension.ProjectEvidence, error) + budgetSettings core.EffectiveSettings +} + +func (c *projectRunCtx) Settings() core.EffectiveSettings { + if c.budgetSettings != nil { + return c.budgetSettings + } + return c.fakeRunCtx.Settings() +} + +func (c *projectRunCtx) ProjectContext(context.Context, string, int) (extension.ProjectContextBundle, bool, error) { + c.reads++ + return c.bundle, true, c.readErr +} + +func (c *projectRunCtx) SaveContextManifest(_ context.Context, m extension.ProjectContextManifest) error { + c.manifests = append(c.manifests, m) + return nil +} + +func (c *projectRunCtx) ProjectContextEnabled() bool { return true } +func (c *projectRunCtx) ProjectEvidence(_ context.Context, ref string) (extension.ProjectEvidence, error) { + return c.evidence(ref) +} + +func TestProjectContextRefreshesPerModelRequestWithoutPersistingOldSummary(t *testing.T) { + var requests [][]agentllm.Message + base := scriptStream(textTurns("private old model summary", "ok"), nil) + runner := &LLMRunner{Stream: func(ctx context.Context, model string, msgs []agentllm.Message, tools []agentllm.ToolDecl, opts agentllm.StreamOptions) <-chan agentllm.Event { + requests = append(requests, append([]agentllm.Message(nil), msgs...)) + return base(ctx, model, msgs, tools, opts) + }} + c := &projectRunCtx{fakeRunCtx: &fakeRunCtx{ctx: context.Background(), sandbox: t.TempDir(), projectID: "project-1", prof: core.AgentProfile{Model: core.ModelRoute{Chat: "test-model"}}}} + c.bundle = extension.ProjectContextBundle{Status: extension.ProjectContextReady, ProjectID: "project-1", Version: "v1", Items: []extension.ProjectContextItem{{SourceID: "s1", Text: "summary-v1", Required: true}}} + if err := runner.Execute(c); err != nil { + t.Fatal(err) + } + c.bundle.Version = "v2" + c.bundle.Items[0].Text = "summary-v2" + if err := runner.Execute(c); err != nil { + t.Fatal(err) + } + if len(requests) != 2 || !strings.Contains(joinReq(requests[0]), "summary-v1") || !strings.Contains(joinReq(requests[1]), "summary-v2") || strings.Contains(joinReq(requests[1]), "summary-v1") || strings.Contains(joinReq(requests[1]), "private old model summary") { + t.Fatalf("project context not refreshed: first=%q second=%q", joinReq(requests[0]), joinReq(requests[1])) + } + if c.reads != 2 || len(c.manifests) != 4 || c.manifests[0].Delivery != "attempted" || c.manifests[1].Delivery != "responded" || c.manifests[2].Version != "v2" || c.manifests[3].Delivery != "responded" || c.manifests[2].AttemptID != c.manifests[3].AttemptID || c.manifests[3].InputSHA256 == "" { + t.Fatalf("actual request manifests missing: reads=%d manifests=%+v", c.reads, c.manifests) + } + wire, err := json.Marshal(requests[1]) + if err != nil { + t.Fatal(err) + } + if want := fmt.Sprintf("%x", sha256.Sum256(wire)); c.manifests[3].InputSHA256 != want { + t.Fatalf("manifest hash %q does not match actual model request %q", c.manifests[3].InputSHA256, want) + } +} + +func TestProjectContextResolutionFailureBlocksModelRequest(t *testing.T) { + called := false + runner := &LLMRunner{Stream: func(ctx context.Context, model string, msgs []agentllm.Message, tools []agentllm.ToolDecl, opts agentllm.StreamOptions) <-chan agentllm.Event { + called = true + return scriptStream(textTurns("bad"), nil)(ctx, model, msgs, tools, opts) + }} + c := &projectRunCtx{fakeRunCtx: &fakeRunCtx{ctx: context.Background(), projectID: "project-1", prof: core.AgentProfile{Model: core.ModelRoute{Chat: "test-model"}}}, readErr: errors.New("permission revoked")} + if err := runner.Execute(c); err == nil || called { + t.Fatalf("context resolution failure reached model: err=%v called=%v", err, called) + } +} + +func TestReadProjectEvidenceToolUsesScopedReader(t *testing.T) { + c := &projectRunCtx{fakeRunCtx: &fakeRunCtx{projectID: "p1"}, evidence: func(ref string) (extension.ProjectEvidence, error) { + if ref != "ref-1" { + return extension.ProjectEvidence{}, errors.New("reference denied") + } + return extension.ProjectEvidence{Reference: extension.ProjectEvidenceRef{ID: ref}, Text: "current authorized detail"}, nil + }} + st := &directRun{l: &LLMRunner{}, rc: c, eff: c.Settings()} + if got := st.runToolCtx(context.Background(), toolReadProjectEvidence, `{"reference":"ref-1"}`); got.IsError || !strings.Contains(got.Content, "current authorized detail") { + t.Fatalf("current evidence unavailable: %+v", got) + } + if got := st.runToolCtx(context.Background(), toolReadProjectEvidence, `{"reference":"../../arbitrary"}`); !got.IsError { + t.Fatalf("arbitrary evidence reference accepted: %+v", got) + } +} + +func TestSubagentModelRequestUsesProjectContextAndManifest(t *testing.T) { + var sent []agentllm.Message + base := scriptStream(textTurns("done"), nil) + sub := &SubLLMRunner{Stream: func(ctx context.Context, model string, msgs []agentllm.Message, tools []agentllm.ToolDecl, opts agentllm.StreamOptions) <-chan agentllm.Event { + sent = append([]agentllm.Message(nil), msgs...) + return base(ctx, model, msgs, tools, opts) + }} + c := &projectRunCtx{fakeRunCtx: &fakeRunCtx{ctx: context.Background(), projectID: "project-1", prof: core.AgentProfile{Model: core.ModelRoute{Chat: "test-model", Lightweight: "test-lite"}}}, + bundle: extension.ProjectContextBundle{Status: extension.ProjectContextReady, ProjectID: "project-1", Items: []extension.ProjectContextItem{{SourceID: "s1", Text: "current project fact", Required: true}}}} + if err := sub.Execute(c); err != nil { + t.Fatal(err) + } + if !strings.Contains(joinReq(sent), "current project fact") || len(c.manifests) != 2 || c.manifests[1].Delivery != "responded" { + t.Fatalf("background subagent missed project context: input=%q manifests=%+v", joinReq(sent), c.manifests) + } +} + +func TestProjectStreamRejectsProjectRunWithoutTrustedContextPort(t *testing.T) { + called := false + base := func(ctx context.Context, model string, msgs []agentllm.Message, tools []agentllm.ToolDecl, opts agentllm.StreamOptions) <-chan agentllm.Event { + called = true + return scriptStream(textTurns("bad"), nil)(ctx, model, msgs, tools, opts) + } + stream := GuardProjectStream(&fakeRunCtx{projectID: "p1"}, "goal", base) + for ev := range stream(context.Background(), "test", []agentllm.Message{{Role: agentllm.RoleUser, Content: "goal"}}, nil, agentllm.StreamOptions{}) { + if ev.Kind != agentllm.KindError { + t.Fatalf("missing scope port did not fail closed: %+v", ev) + } + } + if called { + t.Fatal("model reached without a trusted project context port") + } +} + +func TestProjectStreamBlocksRequestExceedingFinalModelBudget(t *testing.T) { + called := false + base := func(ctx context.Context, model string, msgs []agentllm.Message, tools []agentllm.ToolDecl, opts agentllm.StreamOptions) <-chan agentllm.Event { + called = true + return scriptStream(textTurns("bad"), nil)(ctx, model, msgs, tools, opts) + } + c := &projectRunCtx{fakeRunCtx: &fakeRunCtx{projectID: "p1"}, bundle: extension.ProjectContextBundle{Status: extension.ProjectContextReady, ProjectID: "p1", Items: []extension.ProjectContextItem{{SourceID: "large", Text: strings.Repeat("x", 11000), Required: true}}}, budgetSettings: core.EffectiveSettings{"models": map[string]any{"chat": map[string]any{"contextWindow": float64(4096), "outputReserve": float64(512), "contextBuffer": float64(512)}}}} + for ev := range GuardProjectStream(c, "goal", base)(context.Background(), "test", []agentllm.Message{{Role: agentllm.RoleUser, Content: "goal"}}, nil, agentllm.StreamOptions{}) { + if ev.Kind != agentllm.KindError { + t.Fatalf("oversized request did not fail: %+v", ev) + } + } + if called || len(c.manifests) != 0 { + t.Fatalf("oversized project request reached model or recorded an attempt: called=%v manifests=%+v", called, c.manifests) + } +} + +type projectSquadCtx struct { + *entryFake + manifests []extension.ProjectContextManifest +} + +func (c *projectSquadCtx) Run() core.Run { + return core.Run{ID: "run-squad", Owner: "alice", ProjectID: "project-1"} +} +func (c *projectSquadCtx) ProjectContext(context.Context, string, int) (extension.ProjectContextBundle, bool, error) { + return extension.ProjectContextBundle{Status: extension.ProjectContextReady, ProjectID: "project-1", Items: []extension.ProjectContextItem{{SourceID: "squad-source", Text: "squad project fact", Required: true}}}, true, nil +} +func (c *projectSquadCtx) SaveContextManifest(_ context.Context, m extension.ProjectContextManifest) error { + c.manifests = append(c.manifests, m) + return nil +} + +func TestSquadEntryModelRequestUsesProjectContext(t *testing.T) { + var sent []agentllm.Message + base := writeThenStop(nil, nil) + runner := &SquadRunner{Router: fixedRoute{mode: squad.RouteDirect}, Checker: squad.MinimalChecker{}, Hitl: squad.HitlPolicy{Mode: squad.HitlDisabled}, + Stream: func(ctx context.Context, model string, msgs []agentllm.Message, tools []agentllm.ToolDecl, opts agentllm.StreamOptions) <-chan agentllm.Event { + sent = append([]agentllm.Message(nil), msgs...) + return base(ctx, model, msgs, tools, opts) + }} + dir := t.TempDir() + c := &projectSquadCtx{entryFake: &entryFake{text: "write report", sandbox: dir, settings: core.EffectiveSettings{"models": map[string]any{"chat": map[string]any{"model": "test-model", "baseUrl": "http://test"}}, "storage": map[string]any{"dataDir": dir}}}} + if err := runner.Execute(c); err != nil { + t.Fatal(err) + } + if !strings.Contains(joinReq(sent), "squad project fact") || len(c.manifests) < 2 || c.manifests[len(c.manifests)-1].Delivery != "responded" { + t.Fatalf("squad model missed project context: input=%q manifests=%+v", joinReq(sent), c.manifests) + } +} diff --git a/internal/runtime/project_stream.go b/internal/runtime/project_stream.go new file mode 100644 index 0000000..b8aad50 --- /dev/null +++ b/internal/runtime/project_stream.go @@ -0,0 +1,165 @@ +package runtime + +import ( + "context" + "crypto/sha256" + "encoding/json" + "fmt" + "time" + + agentllm "agent-native/runtime/llm" + "agent-native/runtime/reminders" + "agent-system-backend/internal/ctxasm" + "agent-system-backend/internal/projectcontext" + "agent-system-backend/pkg/extension" + core "agent-system-core" +) + +// GuardProjectStream injects project reference material only into a model +// request copy. This is used by execution paths that do not use LLMRunner's +// direct loop; no project or no registered provider keeps the old request. +func GuardProjectStream(rc any, goal string, stream agentllm.StreamFunc) agentllm.StreamFunc { + source, ok := rc.(interface { + ProjectContext(context.Context, string, int) (extension.ProjectContextBundle, bool, error) + }) + if stream == nil { + return stream + } + if !ok { + if scope, hasRun := rc.(interface{ Run() core.Run }); hasRun && scope.Run().ProjectID != "" { + return func(context.Context, string, []agentllm.Message, []agentllm.ToolDecl, agentllm.StreamOptions) <-chan agentllm.Event { + out := make(chan agentllm.Event, 1) + out <- agentllm.Event{Kind: agentllm.KindError, Err: fmt.Errorf("trusted project context port missing")} + close(out) + return out + } + } + return stream + } + return func(ctx context.Context, model string, messages []agentllm.Message, tools []agentllm.ToolDecl, opts agentllm.StreamOptions) <-chan agentllm.Event { + fail := func(err error) <-chan agentllm.Event { + out := make(chan agentllm.Event, 1) + out <- agentllm.Event{Kind: agentllm.KindError, Err: err} + close(out) + return out + } + const budget = 12000 + requestGoal := goal + if requestGoal == "" { + for i := len(messages) - 1; i >= 0; i-- { + if messages[i].Role == agentllm.RoleUser { + requestGoal = messages[i].Content + break + } + } + } + bundle, active, err := source.ProjectContext(ctx, requestGoal, budget) + if err != nil { + return fail(err) + } + if !active { + return stream(ctx, model, messages, tools, opts) + } + body, manifest, err := projectcontext.Assemble(bundle, budget) + if err != nil { + return fail(err) + } + request := reminders.Attach(messages, []string{body}) + if err := checkProjectInputBudget(rc, request, tools); err != nil { + return fail(err) + } + wire, err := json.Marshal(request) + if err != nil { + return fail(err) + } + manifest.Model = model + manifest.InputSHA256 = fmt.Sprintf("%x", sha256.Sum256(wire)) + if ledger, ok := rc.(interface { + ContextEvidenceInRequest([]agentllm.Message) []extension.ProjectContextManifestEntry + }); ok { + manifest.ReadEvidence = ledger.ContextEvidenceInRequest(request) + } + attemptID, err := projectcontext.NewAttemptID() + if err != nil { + return fail(err) + } + manifest.AttemptID, manifest.Delivery = attemptID, "attempted" + var save func(context.Context, extension.ProjectContextManifest) error + if sink, ok := rc.(interface { + SaveContextManifest(context.Context, extension.ProjectContextManifest) error + }); ok { + save = sink.SaveContextManifest + if err := save(ctx, manifest); err != nil { + return fail(err) + } + } + in := stream(ctx, model, request, tools, opts) + out := make(chan agentllm.Event, 1) + go func() { + defer close(out) + terminal := false + finish := func(status string) error { + if save == nil { + return nil + } + manifest.Delivery = status + receiptCtx, cancel := context.WithTimeout(context.Background(), 3*time.Second) + defer cancel() + return save(receiptCtx, manifest) + } + defer func() { + if !terminal { + _ = finish("unknown") + } + }() + for ev := range in { + if ev.Kind == agentllm.KindMessageEnd || ev.Kind == agentllm.KindError { + status := "unknown" + if ev.Kind == agentllm.KindMessageEnd { + status = "responded" + } + if err := finish(status); err != nil { + select { + case out <- agentllm.Event{Kind: agentllm.KindError, Err: err}: + case <-ctx.Done(): + } + terminal = true + return + } + terminal = true + } + select { + case out <- ev: + case <-ctx.Done(): + return + } + } + }() + return out + } +} + +func checkProjectInputBudget(rc any, messages []agentllm.Message, tools []agentllm.ToolDecl) error { + settings, ok := rc.(interface{ Settings() core.EffectiveSettings }) + if !ok { + return fmt.Errorf("project model budget unavailable") + } + eff := settings.Settings() + limit := int(eff.GetNumber("models.chat.contextWindow", 128000) - + eff.GetNumber("models.chat.outputReserve", 16384) - + eff.GetNumber("models.chat.contextBuffer", 4096)) + if limit <= 0 { + return fmt.Errorf("project model input budget exhausted") + } + used := 0 + for _, msg := range messages { + used += ctxasm.EstimateTokens(msg.Content) + 8 + } + for _, tool := range tools { + used += ctxasm.EstimateTokens(tool.Description+string(tool.Parameters)) + 8 + } + if used > limit { + return fmt.Errorf("project model input exceeds budget: estimated %d tokens, available %d", used, limit) + } + return nil +} diff --git a/internal/runtime/squad_runner.go b/internal/runtime/squad_runner.go index 22775cb..bc789e5 100644 --- a/internal/runtime/squad_runner.go +++ b/internal/runtime/squad_runner.go @@ -65,12 +65,16 @@ func (s *SquadRunner) Execute(rc RunCtx) error { if router == nil { router = newModelRouter(rc) } + stream := s.Stream + if stream != nil { + stream = GuardProjectStream(rc, entryGoal(rc), stream) + } res, err := squadadapter.RunEntry(rc, squadadapter.EntryOptions{ DataDir: rc.Settings().GetString("storage.dataDir", "./data"), Goal: entryGoal(rc), HostTools: bridgeHostTools(rc, produced), OnEvent: onEvent, - Stream: s.Stream, + Stream: stream, Router: router, Checker: s.Checker, Hitl: s.Hitl, @@ -140,7 +144,7 @@ func newModelRouter(rc RunCtx) modelRouter { return modelRouter{} } client := openLLMClient(route.Base, route.Key, route.Model) - return modelRouter{stream: client.Stream, model: route.Model} + return modelRouter{stream: GuardProjectStream(rc, "", client.Stream), model: route.Model} } func (r modelRouter) Route(ctx context.Context, goal squad.Goal) (*squad.RouteDecision, error) { diff --git a/internal/runtime/sub_loop.go b/internal/runtime/sub_loop.go index 3e5f302..a9dc89c 100644 --- a/internal/runtime/sub_loop.go +++ b/internal/runtime/sub_loop.go @@ -76,6 +76,7 @@ func (l *SubLLMRunner) Execute(rc RunCtx) error { client := openLLMClient(route.Base, route.Key, route.Model) stream = client.Stream } + stream = GuardProjectStream(rc, subagentUserText(rc), stream) if spec.Model == "chat" { stream = agentllm.WithFallback(stream, ChatFallbacks(rc.Settings()), nil) } diff --git a/internal/runtime/transcript_compact.go b/internal/runtime/transcript_compact.go index 06dbdbc..cdb8cb7 100644 --- a/internal/runtime/transcript_compact.go +++ b/internal/runtime/transcript_compact.go @@ -7,6 +7,7 @@ import ( agentllm "agent-native/runtime/llm" agentsession "agent-native/runtime/session" + "agent-system-backend/pkg/extension" core "agent-system-core" ) @@ -16,9 +17,10 @@ const compactKeepMessages = 16 // 请求侧生效、不落转录):个人指令/会话要求等尾注、环境更新行、本轮新命中的 // 提示词模块正文。 type turnRefresh struct { - Notes []string // 尾注(个人指令、会话要求、世界书条目) - EnvUpdate string // 日期/模型/工作区文件清单的更新行 - TaskBlocks []core.PromptBlock + Notes []string // 尾注(个人指令、会话要求、世界书条目) + EnvUpdate string // 日期/模型/工作区文件清单的更新行 + TaskBlocks []core.PromptBlock + ProjectContext func() (string, *extension.ProjectContextManifest, error) } // maybeCompact 在转录估算超过窗口阈值时先做结构化压缩:9 段模板摘要 + 保留 diff --git a/internal/runtime/workflow_compiled.go b/internal/runtime/workflow_compiled.go index ab7a3d3..428cd90 100644 --- a/internal/runtime/workflow_compiled.go +++ b/internal/runtime/workflow_compiled.go @@ -81,9 +81,10 @@ func judgeStreamFor(rc RunCtx) agentllm.StreamFunc { } client := openLLMClient(route.Base, route.Key, route.Model) model := route.Model - return func(ctx context.Context, _ string, msgs []agentllm.Message, tools []agentllm.ToolDecl, opts agentllm.StreamOptions) <-chan agentllm.Event { + base := func(ctx context.Context, _ string, msgs []agentllm.Message, tools []agentllm.ToolDecl, opts agentllm.StreamOptions) <-chan agentllm.Event { return client.Stream(ctx, model, msgs, tools, opts) } + return GuardProjectStream(rc, "", base) } // planCompiledWorkflow 规划分支(run_workflow goal=):规划子代理产出 diff --git a/internal/runtime/workflow_compiled_test.go b/internal/runtime/workflow_compiled_test.go index 134322d..70faa07 100644 --- a/internal/runtime/workflow_compiled_test.go +++ b/internal/runtime/workflow_compiled_test.go @@ -12,6 +12,7 @@ import ( agentllm "agent-native/runtime/llm" "agent-system-backend/internal/workflow" + "agent-system-backend/pkg/extension" core "agent-system-core" ) @@ -292,4 +293,48 @@ func TestJudgeStreamUsesConfiguredModel(t *testing.T) { } } +type projectJudgeCtx struct { + *wfFake + manifests []extension.ProjectContextManifest +} + +func (c *projectJudgeCtx) Run() core.Run { + return core.Run{ID: "run-judge", Owner: "alice", ProjectID: "project-1"} +} +func (c *projectJudgeCtx) ProjectContext(context.Context, string, int) (extension.ProjectContextBundle, bool, error) { + return extension.ProjectContextBundle{Status: extension.ProjectContextReady, ProjectID: "project-1", Items: []extension.ProjectContextItem{{SourceID: "judge-source", Text: "current project rule", Required: true}}}, true, nil +} +func (c *projectJudgeCtx) SaveContextManifest(_ context.Context, m extension.ProjectContextManifest) error { + c.manifests = append(c.manifests, m) + return nil +} + +func TestWorkflowJudgeRequestUsesProjectContext(t *testing.T) { + var sent string + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + var body struct { + Messages []struct { + Content string `json:"content"` + } `json:"messages"` + } + _ = json.NewDecoder(r.Body).Decode(&body) + for _, m := range body.Messages { + sent += m.Content + } + w.Header().Set("Content-Type", "text/event-stream") + _, _ = io.WriteString(w, "data: {\"choices\":[{\"delta\":{\"content\":\"ok\"}}]}\n\n") + _, _ = io.WriteString(w, "data: [DONE]\n\n") + })) + defer srv.Close() + c := &projectJudgeCtx{wfFake: &wfFake{settings: core.EffectiveSettings{"models": map[string]any{"chat": map[string]any{"baseUrl": srv.URL, "apiKey": "test-key", "model": "test-model"}}}}} + for ev := range judgeStreamFor(c)(context.Background(), "test-model", []agentllm.Message{{Role: agentllm.RoleUser, Content: "judge"}}, nil, agentllm.StreamOptions{}) { + if ev.Kind == agentllm.KindError { + t.Fatalf("judge stream failed: %+v", ev) + } + } + if !strings.Contains(sent, "current project rule") || len(c.manifests) != 2 || c.manifests[1].Delivery != "responded" { + t.Fatalf("workflow judge missed project context: input=%q manifests=%+v", sent, c.manifests) + } +} + func (*wfFake) Surfaces() []core.SurfaceSnapshot { return nil } diff --git a/internal/store/context_manifest.go b/internal/store/context_manifest.go new file mode 100644 index 0000000..947b42f --- /dev/null +++ b/internal/store/context_manifest.go @@ -0,0 +1,50 @@ +package store + +import ( + "encoding/json" + "errors" + + "agent-system-backend/pkg/extension" +) + +const contextManifestAction = "model.context_manifest" + +// AppendContextManifest stores a content-free receipt before a model request. +// The caller must supply a previously verified run or conversation target. +func (s *Store) AppendContextManifest(owner, targetID string, m extension.ProjectContextManifest) error { + if owner == "" || targetID == "" || m.ProjectID == "" || m.InputSHA256 == "" { + return errors.New("context manifest requires owner, target, project and input hash") + } + raw, err := json.Marshal(m) + if err != nil { + return err + } + return s.AppendAuditEvent(AuditEvent{Owner: owner, Actor: owner, Action: contextManifestAction, Target: targetID, Evidence: string(raw)}) +} + +// ListContextManifests reads receipts only within the authenticated owner's +// scope. The target is a run ID for ordinary work, or a conversation ID for +// direct WorkCompletionHost calls. +func (s *Store) ListContextManifests(owner, targetID string) ([]extension.ProjectContextManifest, error) { + if owner == "" || targetID == "" { + return nil, errors.New("context manifest query requires owner and target") + } + rows, err := s.db.Query(s.q(`SELECT evidence FROM audit_events WHERE owner=? AND action=? AND target=? ORDER BY id`), owner, contextManifestAction, targetID) + if err != nil { + return nil, err + } + defer rows.Close() + var out []extension.ProjectContextManifest + for rows.Next() { + var raw string + if err := rows.Scan(&raw); err != nil { + return nil, err + } + var m extension.ProjectContextManifest + if err := json.Unmarshal([]byte(raw), &m); err != nil { + return nil, err + } + out = append(out, m) + } + return out, rows.Err() +} diff --git a/internal/store/migrate.go b/internal/store/migrate.go index c31e0e8..56a087f 100644 --- a/internal/store/migrate.go +++ b/internal/store/migrate.go @@ -848,6 +848,13 @@ func init() { }, down: map[string][]string{"sqlite": {`DROP TABLE IF EXISTS project_event_shares`}, "postgres": {`DROP TABLE IF EXISTS project_event_shares`}}, }, + migration{version: 31, name: "work_task_bindings", + up: map[string][]string{ + "sqlite": {`CREATE TABLE work_task_bindings(task_id TEXT PRIMARY KEY,owner TEXT NOT NULL,plugin_id TEXT NOT NULL,conversation_id TEXT NOT NULL,project_id TEXT NOT NULL,created_at TEXT NOT NULL,FOREIGN KEY(task_id) REFERENCES background_tasks(id) ON DELETE CASCADE)`}, + "postgres": {`CREATE TABLE work_task_bindings(task_id TEXT PRIMARY KEY,owner TEXT NOT NULL,plugin_id TEXT NOT NULL,conversation_id TEXT NOT NULL,project_id TEXT NOT NULL,created_at TEXT NOT NULL,FOREIGN KEY(task_id) REFERENCES background_tasks(id) ON DELETE CASCADE)`}, + }, + down: map[string][]string{"sqlite": {`DROP TABLE IF EXISTS work_task_bindings`}, "postgres": {`DROP TABLE IF EXISTS work_task_bindings`}}, + }, ) } diff --git a/internal/store/migrate_compat.go b/internal/store/migrate_compat.go index 86c692c..07944d4 100644 --- a/internal/store/migrate_compat.go +++ b/internal/store/migrate_compat.go @@ -35,6 +35,7 @@ var migrationBreaksOldReaders = map[int]bool{ 28: false, // auth_version_account_events:仅加列与新表,旧读者不受影响 29: false, // redeem_attempts:新表,旧读者不受影响 30: false, // project_event_publication_proofs:新表,旧读者不受影响 + 31: false, // work_task_bindings:新增绑定表,旧后台任务读面不受影响 } // SchemaTarget 是本二进制会迁移到的 schema 版本。 diff --git a/internal/store/work_tasks.go b/internal/store/work_tasks.go new file mode 100644 index 0000000..cb89826 --- /dev/null +++ b/internal/store/work_tasks.go @@ -0,0 +1,57 @@ +package store + +import ( + "context" + "database/sql" + "errors" +) + +type WorkTaskBinding struct { + TaskID string + Owner string + PluginID string + ConversationID string + ProjectID string + Status string +} + +// CreateWorkTask atomically saves the legacy task row and its trusted creation +// scope. A legacy TaskCreate row has no binding and cannot enter work recovery. +func (s *Store) CreateWorkTask(owner, pluginID, projectID, conversationID, kind, title string) (BackgroundTask, error) { + if owner == "" || pluginID == "" || projectID == "" || conversationID == "" || kind == "" { + return BackgroundTask{}, errors.New("work task scope incomplete") + } + tx, err := s.db.BeginTx(context.Background(), nil) + if err != nil { + return BackgroundTask{}, err + } + defer tx.Rollback() + now := nowISO() + t := BackgroundTask{ID: NewID("task"), Owner: owner, Kind: kind, Title: title, Status: "queued", ConversationID: conversationID, CreatedAt: now, UpdatedAt: now} + if _, err := tx.Exec(s.q(`INSERT INTO background_tasks(id,owner,kind,title,status,progress,error,conversation_id,created_at,updated_at) VALUES(?,?,?,?,?,?,?,?,?,?)`), + t.ID, owner, kind, title, t.Status, 0, "", conversationID, now, now); err != nil { + return BackgroundTask{}, err + } + if _, err := tx.Exec(s.q(`INSERT INTO work_task_bindings(task_id,owner,plugin_id,conversation_id,project_id,created_at) VALUES(?,?,?,?,?,?)`), + t.ID, owner, pluginID, conversationID, projectID, now); err != nil { + return BackgroundTask{}, err + } + if err := tx.Commit(); err != nil { + return BackgroundTask{}, err + } + return t, nil +} + +// WorkTaskScope resolves a task only under its original owner and plugin. +func (s *Store) WorkTaskScope(owner, pluginID, taskID string) (WorkTaskBinding, error) { + var b WorkTaskBinding + if owner == "" || pluginID == "" || taskID == "" { + return b, ErrNotFound + } + err := s.db.QueryRow(s.q(`SELECT b.task_id,b.owner,b.plugin_id,b.conversation_id,b.project_id,t.status FROM work_task_bindings b JOIN background_tasks t ON t.id=b.task_id WHERE b.task_id=? AND b.owner=? AND b.plugin_id=? AND t.owner=? AND t.conversation_id=b.conversation_id`), + taskID, owner, pluginID, owner).Scan(&b.TaskID, &b.Owner, &b.PluginID, &b.ConversationID, &b.ProjectID, &b.Status) + if errors.Is(err, sql.ErrNoRows) { + return WorkTaskBinding{}, ErrNotFound + } + return b, err +} diff --git a/pkg/extension/extension.go b/pkg/extension/extension.go index 2454e4e..f36e91e 100644 --- a/pkg/extension/extension.go +++ b/pkg/extension/extension.go @@ -34,7 +34,7 @@ import ( // 分区组件自身的命令面 fail-closed)。 // 2.12.0-experimental: optional ProjectHost/ProjectLifecycleRegistry and trusted // ResourceAuthzQuery.Source for per-document publication authorization. -const SurfaceVersion = "2.12.0-experimental" +const SurfaceVersion = "2.13.0-experimental" // ErrNotLinked 表示进程没有空白导入 link 包,Main 无法启动服务。 var ErrNotLinked = errors.New("extension: runner not linked; blank-import agent-system-backend/pkg/extension/link") @@ -179,6 +179,9 @@ type Options struct { StorageDSN string ArtifactsDir string AuthMode string + // RequireProjectContext makes startup fail if no in-process X1 provider + // registered. Downstream products opt in when project context is mandatory. + RequireProjectContext bool // DisableUploadIndexing 关闭上传端点的自动检索索引(EXT-9;零值=false=现状: // 可解析文本照旧进索引。下游以发布状态机自管索引时置 true)。 DisableUploadIndexing bool diff --git a/pkg/extension/host.go b/pkg/extension/host.go index c528b81..9d36b71 100644 --- a/pkg/extension/host.go +++ b/pkg/extension/host.go @@ -41,6 +41,8 @@ type Handler func(ctx context.Context, h Host, req Request) (any, error) // 可选能力接口(type assertion 获取,断言失败=明确报错): // - AdminProvider — EXT-18:调用方平台管理角色查询 // - SurfaceHost — EXT-17:页面保存/读取 +// - WorkCompletionHost — X1:受信项目工作生成 +// - WorkTaskHost — X1:分发期建立可恢复的受信项目任务绑定 type Host interface { // PluginID 返回注册处理器的插件标识(形如 rdm)。 PluginID() string diff --git a/pkg/extension/link/link.go b/pkg/extension/link/link.go index 1d6a41f..fda2744 100644 --- a/pkg/extension/link/link.go +++ b/pkg/extension/link/link.go @@ -12,6 +12,7 @@ func init() { return app.Run(app.Options{ Addr: opt.Addr, DataDir: opt.DataDir, PluginsDir: opt.PluginsDir, ForceMock: opt.Mock, StorageDSN: opt.StorageDSN, ArtifactsDir: opt.ArtifactsDir, AuthMode: opt.AuthMode, + RequireProjectContext: opt.RequireProjectContext, DisableUploadIndexing: opt.DisableUploadIndexing, DisableConvCardMirror: opt.DisableConvCardMirror, DisableExecTools: opt.DisableExecTools, diff --git a/pkg/extension/project_context.go b/pkg/extension/project_context.go new file mode 100644 index 0000000..ad85007 --- /dev/null +++ b/pkg/extension/project_context.go @@ -0,0 +1,142 @@ +package extension + +import ( + "context" + + core "agent-system-core" +) + +// ProjectContextRegistry is optional. An in-process plugin may register one +// provider; existing Registry implementations are unchanged. +type ProjectContextRegistry interface { + RegisterProjectContextProvider(ProjectContextProvider) error +} + +// ProjectContextRequest is built by the host from an authenticated dispatch or +// persisted run. Plugins and model arguments cannot choose Actor or ProjectID. +type ProjectContextRequest struct { + Actor string + PluginID string + ProjectID core.ID + ConversationID core.ID + TaskID core.ID + RunID core.ID + StepID core.ID + Goal string + TargetID string + BudgetChars int + LastWatermark string +} + +type ProjectContextStatus string + +const ( + ProjectContextReady ProjectContextStatus = "ready" + ProjectContextNotFound ProjectContextStatus = "not-found" + ProjectContextDenied ProjectContextStatus = "denied" + ProjectContextIncomplete ProjectContextStatus = "incomplete" +) + +// ProjectContextItem is a currently authorized summary or requirement. Text is +// untrusted source material and must enter the model as user-level context. +type ProjectContextItem struct { + SourceID string + Revision string + Hash string + Text string + Required bool +} + +type ProjectEvidenceRef struct { + ID string + SourceID string + Revision string + Hash string +} + +type ProjectContextBundle struct { + Status ProjectContextStatus + ProjectID core.ID + Version string + Watermark string + AuthorizationEpoch string + PublicationEpoch string + Items []ProjectContextItem + Evidence []ProjectEvidenceRef + Missing []string +} + +// ProjectEvidenceRequest permits only a reference returned by Resolve for the +// same trusted scope. The provider must recheck current access at read time. +type ProjectEvidenceRequest struct { + Scope ProjectContextRequest + Reference ProjectEvidenceRef +} + +type ProjectEvidence struct { + Reference ProjectEvidenceRef + Text string +} + +type ProjectContextProvider interface { + Resolve(context.Context, ProjectContextRequest) (ProjectContextBundle, error) + ReadEvidence(context.Context, ProjectEvidenceRequest) (ProjectEvidence, error) +} + +// WorkCompletionRequest gives the generation goal and optional target. It has +// no owner/project field; both are resolved from the dispatch host's stored +// conversation immediately before the model call. +type WorkCompletionRequest struct { + Goal string + TargetID string + TaskID string // recovery locator only; host verifies its persisted owner/plugin/project binding + EvidenceRefs []string // optional stable refs from the current provider bundle; no paths or owners +} + +// WorkTaskHost creates a persisted task bound to this dispatch's authenticated +// owner, plugin, conversation and project. Legacy Host.TaskCreate is unbound and +// cannot be used to recover a WorkCompletionHost scope. +type WorkTaskHost interface { + TaskCreateForWork(kind, title string) (taskID string, err error) +} + +type ProjectContextManifestEntry struct { + ReferenceID string + SourceID string + Revision string + SourceHash string + InputSHA256 string + Reason string +} + +// ProjectContextManifest describes the text actually passed to the completion +// client. Candidate references in a bundle are never listed as included input. +type ProjectContextManifest struct { + ProjectID core.ID + TaskID string + Version string + Watermark string + AuthorizationEpoch string + PublicationEpoch string + AttemptID string + Delivery string // attempted | responded | unknown; never implies user-visible delivery + Model string + BudgetChars int + InputSHA256 string + Included []ProjectContextManifestEntry + Dropped []ProjectContextManifestEntry + OfferedReferences []ProjectEvidenceRef + ReadEvidence []ProjectContextManifestEntry + Missing []string +} + +type WorkCompletionResult struct { + Text string + Manifest ProjectContextManifest +} + +// WorkCompletionHost is optional and available only on a dispatch-scoped Host. +// A Host obtained via NewScopedHost has no trusted conversation scope. +type WorkCompletionHost interface { + CompleteForWork(context.Context, WorkCompletionRequest) (WorkCompletionResult, error) +} diff --git a/pkg/extension/surface.txt b/pkg/extension/surface.txt index 7d88ba4..c3a08ec 100644 --- a/pkg/extension/surface.txt +++ b/pkg/extension/surface.txt @@ -32,6 +32,24 @@ Main NewHostClient NewScopedHost Options +ProjectContextBundle +ProjectContextDenied +ProjectContextIncomplete +ProjectContextItem +ProjectContextManifest +ProjectContextManifestEntry +ProjectContextNotFound +ProjectContextProvider +ProjectContextProvider.ReadEvidence +ProjectContextProvider.Resolve +ProjectContextReady +ProjectContextRegistry +ProjectContextRegistry.RegisterProjectContextProvider +ProjectContextRequest +ProjectContextStatus +ProjectEvidence +ProjectEvidenceRef +ProjectEvidenceRequest ProjectHost ProjectHost.EnsureProject ProjectHost.OwnedProject @@ -67,3 +85,9 @@ SurfaceHost.SurfaceList SurfaceHost.SurfaceSave SurfaceSaveRequest SurfaceVersion +WorkCompletionHost +WorkCompletionHost.CompleteForWork +WorkCompletionRequest +WorkCompletionResult +WorkTaskHost +WorkTaskHost.TaskCreateForWork From a8fa2fe6c9172674b50c8548487d2f4e3abdb0ab Mon Sep 17 00:00:00 2001 From: weichen Date: Wed, 14 Oct 2026 04:11:09 +0000 Subject: [PATCH 04/15] Bind trusted operation IDs before domain handlers Why: A domain outbox could not correlate its committed event with the host receipt because the bus generated the operation ID after the handler returned. Evidence: RED-to-GREEN bus and compiled-handler tests; local go test ./... -count=1 and isolated PostgreSQL operation/link checks passed as SELF. Impact: Expose a host-generated operation ID to handlers and resolve a unique committed audit offset after dispatch; missing receipts remain retryable and ambiguous rows fail closed. Refs: Stage 5 W9; approved Stage 6 audit-link design adjustment r1 --- .task/feature-rdm-two-trees-project-audit.md | 4 +- internal/app/compiled.go | 2 +- internal/app/project_audit.go | 15 ++++- internal/app/project_audit_link_test.go | 11 +++- internal/app/surface_audit_actor_test.go | 61 ++++++++++++++++++++ internal/cmdbus/cmdbus.go | 8 ++- internal/cmdbus/cmdbus_test.go | 28 +++++++++ internal/store/project_audit_links.go | 32 ++++++++++ internal/store/project_audit_links_test.go | 13 +++++ pkg/extension/extension.go | 4 +- pkg/extension/host.go | 3 + pkg/extension/project_audit.go | 6 +- pkg/extension/surface.txt | 1 + 13 files changed, 180 insertions(+), 8 deletions(-) diff --git a/.task/feature-rdm-two-trees-project-audit.md b/.task/feature-rdm-two-trees-project-audit.md index 05a3595..6fb6a96 100644 --- a/.task/feature-rdm-two-trees-project-audit.md +++ b/.task/feature-rdm-two-trees-project-audit.md @@ -12,6 +12,7 @@ - Store write snapshots the project from the owner-matched conversation; query requires the authenticated actor's owner and that immutable project. This deliberately excludes other members' private conversations and unbound historical rows, and does not move old receipts when a conversation changes projects. - Command audit stores the current verified account display name at write time; old rows expose `not-recorded`. Public receipts omit raw payload and parameter digest. Cursor uses project-and-owner scoped source cutoff. - Initial commit `c8dd169a1fdc1451fab3b475c14f9636a7f5bf6c` exposed only the actor-owned project slice. The next local increment fixes button actor propagation and adds a trusted domain-commit link publisher, verifier callback, exact linked metadata read, current `audit.read` and per-object metadata checks, and durable idempotency. Unlinked personal conversations remain private. +- Trusted command dispatch now creates an operation ID before calling the compiled handler and passes that ID through `extension.Request`. The handler can commit the same ID with its domain event/outbox; the final Backend audit/receipt uses it. Client `params.operationId` is ignored. An outbox link request with `HostAuditOffset=0` resolves a unique committed host row by owner, fixed project, and operation ID; a missing row returns retryable `ErrProjectAuditPending`, while duplicate rows fail closed. The separate idempotency key still controls retry equivalence. - Exact-source linked reads do not enumerate hidden links or reveal raw payload, private conversation ID, or content. Body disclosure and a broad project link list are not implemented; the public coverage reports the narrower capability. ## Verification and remaining work @@ -21,5 +22,6 @@ - SELF receipt `/tmp/backend-w9-build-20261014-01.json`: `CGO_ENABLED=0 go build ./cmd/agent-system`, exit 0, run on that pre-commit tree. Generated local binary removed after the build. - SELF PostgreSQL receipt `/tmp/backend-w9-pg-cutoff-20261014-01.json`: isolated PostgreSQL 18.6 instance on port 55442 reproduced a low-ID transaction committing after a higher visible ID. `/tmp/backend-w9-pg-link-20261014-02.json` verifies the new link table, retry/conflict behavior, and the cutoff counterexample against isolated schemas. Both exited 0. The current PG cutoff means only `offset-upper-bound-only`; it is **not** a fixed historical visibility snapshot or safe incremental cursor. - SELF PostgreSQL receipt `/tmp/backend-w9-pg-migrate-20261014-01.json`: isolated SQLite→PostgreSQL migration preserved one linked receipt and advanced its target sequence; the same migration ID replayed idempotently. The temporary PG instance was stopped and removed after verification. +- SELF PostgreSQL receipt `/tmp/backend-w9-pg-operation-20261014-01.json`: a fresh isolated PG instance reran the link lookup, idempotency/conflict, and low-ID late-commit checks after trusted operation-offset resolution was added. The instance was stopped and removed. - Integration constraint: this branch uses schema v31 (audit attribution) and v32 (links), while the independent W6 Backend branch also has a local v31. Neither branch has consumed the other's unmerged work. After the first branch lands, the later branch owner must sync the new main, renumber its unpublished migrations without rewriting an already released version, and rerun fresh/old SQLite and PG migration plus combined regression on the actual merged source. These isolated tests do not prove W6+W9 compatibility. -- Required next W9 work: add a committed-order waterline or materialize the authorized ID set at the first read, then prove with PG low-ID late commits that history and cross-page incremental reads neither omit nor duplicate operations. The Backend project-event source, actual RDM committed-event verifier/outbox consumer, run ID propagation from command dispatch, model/tool/background receipts, body/detail disclosure, broad authorized list pagination, history/export, and combined product verification remain unfinished. The exact-link slice is not Stage 7 audit acceptance. +- Required next W9 work: add a committed-order waterline or materialize the authorized ID set at the first read, then prove with PG low-ID late commits that history and cross-page incremental reads neither omit nor duplicate operations. The Backend project-event source, actual RDM committed-event verifier/outbox consumer, run ID propagation from command dispatch, model/tool/background receipts, body/detail disclosure, broad authorized list pagination, history/export, and combined product verification remain unfinished. A domain commit followed by a lost Backend audit append leaves the link pending; recovery must retain and reconcile that unknown state without claiming a host receipt. The exact-link slice is not Stage 7 audit acceptance. diff --git a/internal/app/compiled.go b/internal/app/compiled.go index da47164..5af5757 100644 --- a/internal/app/compiled.go +++ b/internal/app/compiled.go @@ -67,7 +67,7 @@ func (r pluginRegistry) AddCommandHandler(spec json.RawMessage, h extension.Hand host := inprocHost{caps: r.caps, pluginID: r.pluginID, owner: req.Owner, actor: req.Actor} return h(ctx, host, extension.Request{ ID: req.ID, Params: req.Params, Entrance: req.Entrance, - Actor: req.Actor, Owner: req.Owner, ConversationID: req.ConversationID, + Actor: req.Actor, Owner: req.Owner, ConversationID: req.ConversationID, OperationID: req.OperationID, HostProjectID: req.HostProjectID, }) }) diff --git a/internal/app/project_audit.go b/internal/app/project_audit.go index 53be92d..4b5101b 100644 --- a/internal/app/project_audit.go +++ b/internal/app/project_audit.go @@ -128,9 +128,22 @@ func (h inprocHost) PublishProjectAuditLink(ctx context.Context, req extension.P if holder != h.pluginID || verify == nil { return extension.ProjectAuditLinkReceipt{}, extension.ErrProjectAuditUnsupported } - if h.owner == "" || (h.actor != "" && h.actor != h.owner) || req.HostAuditOffset <= 0 || req.ProjectID == "" || req.SourceKind != "domain-commit" || req.SourceID == "" || req.OperationID == "" || req.ResourceKind == "" || req.ResourceID == "" || req.DisclosureClass != "metadata" || req.IdempotencyKey == "" { + if h.owner == "" || (h.actor != "" && h.actor != h.owner) || req.HostAuditOffset < 0 || req.ProjectID == "" || req.SourceKind != "domain-commit" || req.SourceID == "" || req.OperationID == "" || req.ResourceKind == "" || req.ResourceID == "" || req.DisclosureClass != "metadata" || req.IdempotencyKey == "" { return extension.ProjectAuditLinkReceipt{}, extension.ErrProjectAuditDenied } + if req.HostAuditOffset == 0 { + resolved, err := h.caps.st.CommittedAuditOffset(h.owner, string(req.ProjectID), req.OperationID) + if errors.Is(err, store.ErrNotFound) { + return extension.ProjectAuditLinkReceipt{}, extension.ErrProjectAuditPending + } + if errors.Is(err, store.ErrConflict) { + return extension.ProjectAuditLinkReceipt{}, extension.ErrProjectAuditLinkConflict + } + if err != nil { + return extension.ProjectAuditLinkReceipt{}, err + } + req.HostAuditOffset = resolved + } row, projectID, err := h.caps.st.CommandAuditForLink(h.owner, req.HostAuditOffset) if errors.Is(err, store.ErrNotFound) { return extension.ProjectAuditLinkReceipt{}, extension.ErrProjectAuditDenied diff --git a/internal/app/project_audit_link_test.go b/internal/app/project_audit_link_test.go index 5940436..8c805ca 100644 --- a/internal/app/project_audit_link_test.go +++ b/internal/app/project_audit_link_test.go @@ -73,12 +73,13 @@ func TestProjectAuditLinkSharesOnlyVerifiedCommittedMetadata(t *testing.T) { t.Fatalf("unlinked private audit: %v", err) } alicePublisher := inprocHost{caps: caps, pluginID: "rdm", owner: "alice"} // trusted outbox-scoped host - linkReq := extension.ProjectAuditLinkRequest{ProjectID: p.ID, HostAuditOffset: offset, SourceKind: "domain-commit", SourceID: "commit-1", OperationID: "op-1", ResourceKind: "document", ResourceID: "doc-1", DisclosureClass: "metadata", IdempotencyKey: "outbox-1"} + linkReq := extension.ProjectAuditLinkRequest{ProjectID: p.ID, SourceKind: "domain-commit", SourceID: "commit-1", OperationID: "op-1", ResourceKind: "document", ResourceID: "doc-1", DisclosureClass: "metadata", IdempotencyKey: "outbox-1"} if rows, err := bob.QueryProjectAudit(context.Background(), extension.ProjectAuditQuery{ProjectID: p.ID}); err != nil || len(rows.Receipts) != 0 { t.Fatalf("private owner row visible without exact link: %+v %v", rows, err) } wrongProject := linkReq wrongProject.ProjectID = otherProject.ID + wrongProject.HostAuditOffset = offset if _, err := alicePublisher.PublishProjectAuditLink(context.Background(), wrongProject); !errors.Is(err, extension.ErrProjectAuditDenied) { t.Fatalf("wrong host project: %v", err) } @@ -90,10 +91,18 @@ func TestProjectAuditLinkSharesOnlyVerifiedCommittedMetadata(t *testing.T) { if _, err := (inprocHost{caps: caps, pluginID: "other", owner: "alice"}).PublishProjectAuditLink(context.Background(), linkReq); !errors.Is(err, extension.ErrProjectAuditUnsupported) { t.Fatalf("other plugin: %v", err) } + pending := linkReq + pending.OperationID, pending.IdempotencyKey = "op-not-yet-recorded", "outbox-pending" + if _, err := alicePublisher.PublishProjectAuditLink(context.Background(), pending); !errors.Is(err, extension.ErrProjectAuditPending) { + t.Fatalf("host receipt race must be retryable: %v", err) + } first, err := alicePublisher.PublishProjectAuditLink(context.Background(), linkReq) if err != nil { t.Fatal(err) } + if first.HostAuditOffset != offset { + t.Fatalf("trusted resolved offset: got %d want %d", first.HostAuditOffset, offset) + } retry, err := alicePublisher.PublishProjectAuditLink(context.Background(), linkReq) if err != nil || retry.LinkID != first.LinkID { t.Fatalf("idempotent retry: first=%+v retry=%+v err=%v", first, retry, err) diff --git a/internal/app/surface_audit_actor_test.go b/internal/app/surface_audit_actor_test.go index 2749d7a..d57eae1 100644 --- a/internal/app/surface_audit_actor_test.go +++ b/internal/app/surface_audit_actor_test.go @@ -2,6 +2,7 @@ package app import ( "context" + "encoding/json" "path/filepath" "testing" "time" @@ -10,6 +11,8 @@ import ( "agent-system-backend/internal/cmdbus" "agent-system-backend/internal/store" "agent-system-backend/internal/surfacesvc" + "agent-system-backend/pkg/extension" + core "agent-system-core" ) func TestSurfaceButtonDispatchUsesAuthenticatedActor(t *testing.T) { @@ -48,3 +51,61 @@ func TestSurfaceButtonDispatchUsesAuthenticatedActor(t *testing.T) { t.Fatalf("button audit actor: %+v", rows) } } + +func TestCompiledHandlerReceivesHostOperationID(t *testing.T) { + st, err := store.OpenSQLite(filepath.Join(t.TempDir(), "audit.db")) + if err != nil { + t.Fatal(err) + } + defer st.Close() + p, err := st.CreateProject("alice", "A") + if err != nil { + t.Fatal(err) + } + c, err := st.CreateConversation("alice", string(p.ID), "work", "") + if err != nil { + t.Fatal(err) + } + bus := cmdbus.New(st, nil, func() time.Time { return time.Now().UTC() }) + slot := &projectAuditSlot{} + caps := hostcaps{st: st, projectAudit: slot} + reg := pluginRegistry{compiledRegistry: compiledRegistry{bus: bus}, pluginID: "rdm", caps: caps} + var seen string + if err := reg.AddCommandHandler(json.RawMessage(`{"id":"rdm.confirm","title":"Confirm"}`), func(_ context.Context, _ extension.Host, req extension.Request) (any, error) { + seen = req.OperationID + return "domain-committed", nil + }); err != nil { + t.Fatal(err) + } + resp := bus.Dispatch(context.Background(), cmdbus.Request{ID: "rdm.confirm", Actor: "alice", Owner: "alice", ConversationID: string(c.ID)}) + rows, _, _, err := st.ListProjectCommandAudits("alice", string(p.ID), 0, 0, 10) + if err != nil { + t.Fatal(err) + } + if seen == "" || resp.Receipt.OperationID != seen || len(rows) != 1 || rows[0].Envelope.OperationID != seen { + t.Fatalf("handler/domain correlation: seen=%q receipt=%+v rows=%+v", seen, resp.Receipt, rows) + } + if err := reg.RegisterProjectAuditAuthorizer(func(actor string, project core.ID) (bool, error) { return actor == "bob" && project == p.ID, nil }); err != nil { + t.Fatal(err) + } + if err := reg.RegisterProjectAuditLinkVerifier(func(_ context.Context, proof extension.ProjectAuditLinkProof) (bool, error) { + return proof.Owner == "alice" && proof.ProjectID == p.ID && proof.OperationID == seen && proof.SourceID == "domain-commit-1" && proof.ResourceID == "doc-1", nil + }); err != nil { + t.Fatal(err) + } + if err := reg.RegisterProjectAuditDisclosureAuthorizer(func(_ context.Context, actor string, r extension.ProjectAuditLinkedResource) (extension.ProjectAuditDisclosure, error) { + return extension.ProjectAuditDisclosure{Metadata: actor == "bob" && r.ResourceID == "doc-1"}, nil + }); err != nil { + t.Fatal(err) + } + publisher := inprocHost{caps: caps, owner: "alice", pluginID: "rdm"} + linked, err := publisher.PublishProjectAuditLink(context.Background(), extension.ProjectAuditLinkRequest{ProjectID: p.ID, OperationID: seen, SourceKind: "domain-commit", SourceID: "domain-commit-1", ResourceKind: "document", ResourceID: "doc-1", DisclosureClass: "metadata", IdempotencyKey: "outbox-1"}) + if err != nil || linked.HostAuditOffset != rows[0].Offset { + t.Fatalf("publish by trusted operation: %+v %v", linked, err) + } + bob := inprocHost{caps: caps, owner: "bob", actor: "bob", pluginID: "rdm"} + page, err := bob.QueryProjectAudit(context.Background(), extension.ProjectAuditQuery{ProjectID: p.ID, LinkedSourceKind: "domain-commit", LinkedSourceID: "domain-commit-1", LinkedHostOffset: linked.HostAuditOffset}) + if err != nil || len(page.Receipts) != 1 || page.Receipts[0].OperationID != seen { + t.Fatalf("linked read: %+v %v", page, err) + } +} diff --git a/internal/cmdbus/cmdbus.go b/internal/cmdbus/cmdbus.go index 9b63e7d..8507aa7 100644 --- a/internal/cmdbus/cmdbus.go +++ b/internal/cmdbus/cmdbus.go @@ -89,6 +89,7 @@ type Request struct { Actor string Owner string ConversationID string + OperationID string // overwritten by the trusted bus before handler execution AnchorMessageID string // 确认卡锚定的发起消息(内联渲染) Confirmed bool // 危险命令经确认卡放行后的执行 HostProjectID string // EXT-18:分发所属基座项目(空=无项目上下文) @@ -279,12 +280,15 @@ func (b *Bus) Dispatch(ctx context.Context, req Request) Response { if !ok { return receipt(agentcommand.StateRejected, "", "no host handler bound for "+req.ID) } + // The domain handler can persist this trusted ID with its transaction/outbox. + // Never accept an ID from Params or the incoming Request. A handler failure + // still writes only a rejected audit receipt with no committed operation ID. + req.OperationID = fmt.Sprintf("op_%s_%d", strings.ReplaceAll(req.ID, ".", "_"), b.now().UnixNano()) payload, err := h(ctx, req) if err != nil { return receipt(agentcommand.StateRejected, "", "rejected: "+err.Error()) } - op := fmt.Sprintf("op_%s_%d", strings.ReplaceAll(strings.TrimPrefix(req.ID, ""), ".", "_"), b.now().UnixNano()) - return Response{Receipt: b.auditReceipt(req, agentcommand.StateCommitted, op, "", start), Payload: payload} + return Response{Receipt: b.auditReceipt(req, agentcommand.StateCommitted, req.OperationID, "", start), Payload: payload} } // Confirm 确认卡回道:actionID=confirm 执行挂起请求(entrance 归一 card),其余视为取消。 diff --git a/internal/cmdbus/cmdbus_test.go b/internal/cmdbus/cmdbus_test.go index 7b4e3f3..4d64948 100644 --- a/internal/cmdbus/cmdbus_test.go +++ b/internal/cmdbus/cmdbus_test.go @@ -87,6 +87,34 @@ func TestDispatchFourEntrancesSameAudit(t *testing.T) { } } +func TestHandlerReceivesCommittedOperationIDBeforeDomainWrite(t *testing.T) { + audit := &memAudit{} + bus := New(audit, nil, func() time.Time { return time.Unix(1700000000, 123).UTC() }) + var seen string + if err := bus.RegisterBuiltin(agentcommand.CommandSpec{ID: "rdm.confirm", Title: "Confirm"}, "", func(_ context.Context, req Request) (any, error) { + seen = req.OperationID + return "committed-domain-event", nil + }); err != nil { + t.Fatal(err) + } + resp := bus.Dispatch(context.Background(), Request{ID: "rdm.confirm", Actor: "alice", Owner: "alice", Params: map[string]any{"operationId": "forged-client-id"}}) + if seen == "" || seen == "forged-client-id" || resp.Receipt.OperationID != seen || len(audit.logs) != 1 || audit.logs[0].OperationID != seen { + t.Fatalf("operation correlation missing: handler=%q receipt=%+v audit=%+v", seen, resp.Receipt, audit.logs) + } + if err := bus.RegisterBuiltin(agentcommand.CommandSpec{ID: "rdm.fail", Title: "Fail"}, "", func(_ context.Context, req Request) (any, error) { + if req.OperationID == "" { + t.Fatal("handler must receive a tentative host operation ID") + } + return nil, errors.New("domain transaction rejected") + }); err != nil { + t.Fatal(err) + } + failed := bus.Dispatch(context.Background(), Request{ID: "rdm.fail", Actor: "alice", Owner: "alice"}) + if failed.Receipt.State != agentcommand.StateRejected || failed.Receipt.OperationID != "" || len(audit.logs) != 2 || audit.logs[1].OperationID != "" { + t.Fatalf("failed handler was marked committed: %+v audits=%+v", failed.Receipt, audit.logs) + } +} + func TestDispatchUnknownAndBadParams(t *testing.T) { a := &memAudit{} b := newTestBus(a, &memCards{}) diff --git a/internal/store/project_audit_links.go b/internal/store/project_audit_links.go index 5b58dd8..6d5c96f 100644 --- a/internal/store/project_audit_links.go +++ b/internal/store/project_audit_links.go @@ -23,6 +23,38 @@ type ProjectAuditLink struct { IdempotencyKey string } +// CommittedAuditOffset resolves exactly one persisted host receipt. A domain +// outbox can have the operation ID before the bus appends its audit row; no row +// yet is a retryable pending state, while duplicates are ambiguous and refused. +func (s *Store) CommittedAuditOffset(owner, projectID, operationID string) (int64, error) { + if owner == "" || projectID == "" || operationID == "" { + return 0, ErrNotFound + } + rows, err := s.db.Query(s.q(`SELECT id FROM command_audit WHERE owner=? AND project_id=? AND operation_id=? AND state='committed' ORDER BY id LIMIT 2`), owner, projectID, operationID) + if err != nil { + return 0, err + } + defer rows.Close() + var ids []int64 + for rows.Next() { + var id int64 + if err := rows.Scan(&id); err != nil { + return 0, err + } + ids = append(ids, id) + } + if err := rows.Err(); err != nil { + return 0, err + } + if len(ids) == 0 { + return 0, ErrNotFound + } + if len(ids) > 1 { + return 0, ErrConflict + } + return ids[0], nil +} + func (s *Store) CommandAuditForLink(owner string, offset int64) (ProjectCommandAuditRow, string, error) { var row ProjectCommandAuditRow var projectID, raw string diff --git a/internal/store/project_audit_links_test.go b/internal/store/project_audit_links_test.go index b77838c..64b5b07 100644 --- a/internal/store/project_audit_links_test.go +++ b/internal/store/project_audit_links_test.go @@ -32,6 +32,13 @@ func TestProjectAuditLinkStoreIdempotenceAndExactScope(t *testing.T) { if err != nil { t.Fatal(err) } + resolved, err := st.CommittedAuditOffset("alice", string(a.ID), "op-1") + if err != nil || resolved != rows[0].Offset { + t.Fatalf("trusted operation lookup: %d %v", resolved, err) + } + if _, err := st.CommittedAuditOffset("alice", string(a.ID), "op-pending"); !errors.Is(err, ErrNotFound) { + t.Fatalf("unrecorded operation should wait: %v", err) + } link := ProjectAuditLink{HostAuditOffset: rows[0].Offset, ProjectID: string(a.ID), Owner: "alice", SourceKind: "domain-commit", SourceID: "commit-1", OperationID: "op-1", ResourceKind: "document", ResourceID: "doc-1", DisclosureClass: "metadata", IdempotencyKey: "outbox-1"} first, err := st.SaveProjectAuditLink(link) if err != nil { @@ -81,4 +88,10 @@ func TestProjectAuditLinkStoreIdempotenceAndExactScope(t *testing.T) { if _, err := st.SaveProjectAuditLink(bobLink); err != nil { t.Fatalf("another owner may use the same outbox key: %v", err) } + if err := st.AppendAudit("alice", agentcommand.AuditEnvelope{Time: time.Now(), Actor: "alice", CommandID: "rdm.confirm", OperationID: "op-1", ConversationID: string(c.ID), State: agentcommand.StateCommitted}); err != nil { + t.Fatal(err) + } + if _, err := st.CommittedAuditOffset("alice", string(a.ID), "op-1"); !errors.Is(err, ErrConflict) { + t.Fatalf("ambiguous host operation must not choose one: %v", err) + } } diff --git a/pkg/extension/extension.go b/pkg/extension/extension.go index fca22f9..793f074 100644 --- a/pkg/extension/extension.go +++ b/pkg/extension/extension.go @@ -38,7 +38,9 @@ import ( // project-bound command-audit read slice and domain audit.read callback). // 2.14.0-experimental: X2 optional committed project-audit link publisher, // verifier, disclosure callback, and exact linked receipt read. -const SurfaceVersion = "2.14.0-experimental" +// 2.15.0-experimental: trusted Request.OperationID before handler execution and +// retryable ProjectAuditLinkRequest.HostAuditOffset=0 resolution. +const SurfaceVersion = "2.15.0-experimental" // ErrNotLinked 表示进程没有空白导入 link 包,Main 无法启动服务。 var ErrNotLinked = errors.New("extension: runner not linked; blank-import agent-system-backend/pkg/extension/link") diff --git a/pkg/extension/host.go b/pkg/extension/host.go index c528b81..134d61c 100644 --- a/pkg/extension/host.go +++ b/pkg/extension/host.go @@ -14,6 +14,9 @@ type Request struct { Actor string Owner string ConversationID string + // OperationID is generated by the trusted host bus before this handler runs. + // Persist it with an actual domain commit/outbox; never treat it as a retry key. + OperationID string // HostProjectID 本次分发所属基座项目(EXT-18 可选上下文;空=无项目 // 上下文——API 入口无对话或对话未关联项目时为零值)。下游按显式映射 // 解析(如 RDM rdm_host_project_map),不得据此替代自身授权判定。 diff --git a/pkg/extension/project_audit.go b/pkg/extension/project_audit.go index e993508..0900efe 100644 --- a/pkg/extension/project_audit.go +++ b/pkg/extension/project_audit.go @@ -23,11 +23,15 @@ var ( ErrProjectAuditDenied = errors.New("extension: project audit denied") ErrProjectAuditUnsupported = errors.New("extension: project audit unsupported") ErrProjectAuditLinkConflict = errors.New("extension: project audit link conflict") + ErrProjectAuditPending = errors.New("extension: committed host audit receipt pending") ) // ProjectAuditLinkRequest binds one host command receipt to an already // committed domain fact. Owner is never caller supplied; the host handle and -// stored audit row establish it. This first slice supports domain-commit only. +// stored audit row establish it. HostAuditOffset=0 asks the host to resolve a +// unique committed row by its trusted owner/project/OperationID, which lets an +// outbox wait for the bus audit commit after the domain handler returns. This +// first slice supports domain-commit only. type ProjectAuditLinkRequest struct { ProjectID core.ID `json:"projectId"` HostAuditOffset int64 `json:"hostAuditOffset"` diff --git a/pkg/extension/surface.txt b/pkg/extension/surface.txt index 2e88d0e..6f06b8f 100644 --- a/pkg/extension/surface.txt +++ b/pkg/extension/surface.txt @@ -10,6 +10,7 @@ Compiled ErrNotLinked ErrProjectAuditDenied ErrProjectAuditLinkConflict +ErrProjectAuditPending ErrProjectAuditUnsupported Handler Host From 0e8cd040cc75c329eb8950676acc7a9a08487ad2 Mon Sep 17 00:00:00 2001 From: weichen Date: Wed, 14 Oct 2026 04:29:38 +0000 Subject: [PATCH 05/15] Bind audit publishers and randomize operation IDs Why: Clock-derived IDs could collide and string-scoped hosts could impersonate a registered audit publisher. Evidence: Fixed-clock concurrent, ambiguous private offset, forged scoped-host, and registered publisher RED-to-GREEN tests; go test ./... -count=1 and targeted race test passed locally as SELF. Impact: Generate 192-bit host IDs, reject ambiguous explicit offsets, and issue a publication-only capability at verifier registration. Refs: Stage 5 W9; approved Stage 6 audit-link design adjustment r1; review of a8fa2fe --- .task/feature-rdm-two-trees-project-audit.md | 2 + internal/app/project_audit.go | 103 +++++++++++++------ internal/app/project_audit_link_test.go | 48 ++++++++- internal/app/surface_audit_actor_test.go | 10 +- internal/cmdbus/cmdbus.go | 11 +- internal/cmdbus/cmdbus_test.go | 32 ++++++ pkg/extension/extension.go | 4 +- pkg/extension/project_audit.go | 12 ++- pkg/extension/surface.txt | 2 + 9 files changed, 180 insertions(+), 44 deletions(-) diff --git a/.task/feature-rdm-two-trees-project-audit.md b/.task/feature-rdm-two-trees-project-audit.md index 6fb6a96..f72e33b 100644 --- a/.task/feature-rdm-two-trees-project-audit.md +++ b/.task/feature-rdm-two-trees-project-audit.md @@ -13,6 +13,8 @@ - Command audit stores the current verified account display name at write time; old rows expose `not-recorded`. Public receipts omit raw payload and parameter digest. Cursor uses project-and-owner scoped source cutoff. - Initial commit `c8dd169a1fdc1451fab3b475c14f9636a7f5bf6c` exposed only the actor-owned project slice. The next local increment fixes button actor propagation and adds a trusted domain-commit link publisher, verifier callback, exact linked metadata read, current `audit.read` and per-object metadata checks, and durable idempotency. Unlinked personal conversations remain private. - Trusted command dispatch now creates an operation ID before calling the compiled handler and passes that ID through `extension.Request`. The handler can commit the same ID with its domain event/outbox; the final Backend audit/receipt uses it. Client `params.operationId` is ignored. An outbox link request with `HostAuditOffset=0` resolves a unique committed host row by owner, fixed project, and operation ID; a missing row returns retryable `ErrProjectAuditPending`, while duplicate rows fail closed. The separate idempotency key still controls retry equivalence. +- Independent review of `a8fa2fe` found two P2 security defects: clock-based operation IDs could collide; and `NewScopedHost(owner,"rdm")` let another compiled plugin self-report the publisher's plugin ID. The new local increment uses 192 bits from `crypto/rand` for each host-generated operation ID. Both zero-offset and explicit-offset publication require a unique committed row for owner/project/operation; a historical duplicate rejects publication, including attempts to link another private command. Verifier registration returns a private `ProjectAuditPublisherFactory` for the registering plugin's background outbox; a generic scoped Host has no publisher method. Fixed-clock concurrent IDs, forged params, ambiguous private offset, forged scoped Host, and legitimate registered publisher each have focused tests. +- The minted publisher exposes only `ProjectAuditPublisher`, not the full owner-scoped `Host`; tests reject attempts to use `NewScopedHost` with a self-reported `rdm` string as publisher authority. A publisher is a registration-time capability, while `IdempotencyKey` remains the separate retry identity. - Exact-source linked reads do not enumerate hidden links or reveal raw payload, private conversation ID, or content. Body disclosure and a broad project link list are not implemented; the public coverage reports the narrower capability. ## Verification and remaining work diff --git a/internal/app/project_audit.go b/internal/app/project_audit.go index 4b5101b..8a005cc 100644 --- a/internal/app/project_audit.go +++ b/internal/app/project_audit.go @@ -13,11 +13,41 @@ import ( // projectAuditSlot binds the audit.read policy to its registering domain plugin. // A different plugin cannot borrow that policy through its own Host handle. type projectAuditSlot struct { - mu sync.RWMutex - holder string - check extension.ProjectAuditAuthorizer - verifier extension.ProjectAuditLinkVerifier - disclose extension.ProjectAuditDisclosureAuthorizer + mu sync.RWMutex + holder string + check extension.ProjectAuditAuthorizer + verifier extension.ProjectAuditLinkVerifier + disclose extension.ProjectAuditDisclosureAuthorizer + publisherToken *auditPublisherToken +} + +// This identity is available only to the publisher factory returned at +// registration. A caller-provided pluginID cannot manufacture the pointer. +type auditPublisherToken struct{ marker byte } + +type projectAuditPublisherFactory struct { + caps hostcaps + pluginID string + token *auditPublisherToken +} + +func (f projectAuditPublisherFactory) ForOwner(owner string) (extension.ProjectAuditPublisher, error) { + if owner == "" || f.token == nil { + return nil, extension.ErrProjectAuditDenied + } + return projectAuditPublisher{caps: f.caps, owner: owner, pluginID: f.pluginID, token: f.token}, nil +} + +type projectAuditPublisher struct { + caps hostcaps + owner string + pluginID string + token *auditPublisherToken +} + +func (p projectAuditPublisher) PublishProjectAuditLink(ctx context.Context, req extension.ProjectAuditLinkRequest) (extension.ProjectAuditLinkReceipt, error) { + h := inprocHost{caps: p.caps, owner: p.owner, pluginID: p.pluginID} + return h.publishProjectAuditLink(ctx, req, p.token) } func (s *projectAuditSlot) register(pluginID string, check extension.ProjectAuditAuthorizer) error { @@ -33,17 +63,18 @@ func (s *projectAuditSlot) register(pluginID string, check extension.ProjectAudi return nil } -func (s *projectAuditSlot) registerVerifier(pluginID string, verify extension.ProjectAuditLinkVerifier) error { +func (s *projectAuditSlot) registerVerifier(pluginID string, verify extension.ProjectAuditLinkVerifier) (*auditPublisherToken, error) { if pluginID == "" || verify == nil { - return extension.ErrProjectAuditUnsupported + return nil, extension.ErrProjectAuditUnsupported } s.mu.Lock() defer s.mu.Unlock() if s.verifier != nil || (s.holder != "" && s.holder != pluginID) { - return errors.New("extension: project audit verifier already registered") + return nil, errors.New("extension: project audit verifier already registered") } s.holder, s.verifier = pluginID, verify - return nil + s.publisherToken = &auditPublisherToken{marker: 1} + return s.publisherToken, nil } func (s *projectAuditSlot) registerDisclosure(pluginID string, disclose extension.ProjectAuditDisclosureAuthorizer) error { @@ -78,17 +109,21 @@ func (r recordRegistry) RegisterProjectAuditAuthorizer(check extension.ProjectAu return reg.RegisterProjectAuditAuthorizer(check) } -func (r compiledRegistry) RegisterProjectAuditLinkVerifier(extension.ProjectAuditLinkVerifier) error { - return errors.New("extension: declarative plugins cannot register project audit link verifiers") +func (r compiledRegistry) RegisterProjectAuditLinkVerifier(extension.ProjectAuditLinkVerifier) (extension.ProjectAuditPublisherFactory, error) { + return nil, errors.New("extension: declarative plugins cannot register project audit link verifiers") } func (r compiledRegistry) RegisterProjectAuditDisclosureAuthorizer(extension.ProjectAuditDisclosureAuthorizer) error { return errors.New("extension: declarative plugins cannot register project audit disclosure authorizers") } -func (r pluginRegistry) RegisterProjectAuditLinkVerifier(verify extension.ProjectAuditLinkVerifier) error { +func (r pluginRegistry) RegisterProjectAuditLinkVerifier(verify extension.ProjectAuditLinkVerifier) (extension.ProjectAuditPublisherFactory, error) { if r.caps.projectAudit == nil { - return extension.ErrProjectAuditUnsupported + return nil, extension.ErrProjectAuditUnsupported } - return r.caps.projectAudit.registerVerifier(r.pluginID, verify) + token, err := r.caps.projectAudit.registerVerifier(r.pluginID, verify) + if err != nil { + return nil, err + } + return projectAuditPublisherFactory{caps: r.caps, pluginID: r.pluginID, token: token}, nil } func (r pluginRegistry) RegisterProjectAuditDisclosureAuthorizer(disclose extension.ProjectAuditDisclosureAuthorizer) error { if r.caps.projectAudit == nil { @@ -96,10 +131,10 @@ func (r pluginRegistry) RegisterProjectAuditDisclosureAuthorizer(disclose extens } return r.caps.projectAudit.registerDisclosure(r.pluginID, disclose) } -func (r recordRegistry) RegisterProjectAuditLinkVerifier(verify extension.ProjectAuditLinkVerifier) error { +func (r recordRegistry) RegisterProjectAuditLinkVerifier(verify extension.ProjectAuditLinkVerifier) (extension.ProjectAuditPublisherFactory, error) { reg, ok := r.Registry.(extension.ProjectAuditLinkRegistry) if !ok { - return extension.ErrProjectAuditUnsupported + return nil, extension.ErrProjectAuditUnsupported } return reg.RegisterProjectAuditLinkVerifier(verify) } @@ -112,9 +147,10 @@ func (r recordRegistry) RegisterProjectAuditDisclosureAuthorizer(disclose extens } var _ extension.ProjectAuditHost = inprocHost{} -var _ extension.ProjectAuditPublisher = inprocHost{} +var _ extension.ProjectAuditPublisher = projectAuditPublisher{} +var _ extension.ProjectAuditPublisherFactory = projectAuditPublisherFactory{} -func (h inprocHost) PublishProjectAuditLink(ctx context.Context, req extension.ProjectAuditLinkRequest) (extension.ProjectAuditLinkReceipt, error) { +func (h inprocHost) publishProjectAuditLink(ctx context.Context, req extension.ProjectAuditLinkRequest, capability *auditPublisherToken) (extension.ProjectAuditLinkReceipt, error) { if err := ctx.Err(); err != nil { return extension.ProjectAuditLinkReceipt{}, err } @@ -123,27 +159,34 @@ func (h inprocHost) PublishProjectAuditLink(ctx context.Context, req extension.P } slot := h.caps.projectAudit slot.mu.RLock() - holder, verify := slot.holder, slot.verifier + holder, verify, registeredToken := slot.holder, slot.verifier, slot.publisherToken slot.mu.RUnlock() - if holder != h.pluginID || verify == nil { + if holder != h.pluginID || verify == nil || registeredToken == nil || capability != registeredToken { return extension.ProjectAuditLinkReceipt{}, extension.ErrProjectAuditUnsupported } if h.owner == "" || (h.actor != "" && h.actor != h.owner) || req.HostAuditOffset < 0 || req.ProjectID == "" || req.SourceKind != "domain-commit" || req.SourceID == "" || req.OperationID == "" || req.ResourceKind == "" || req.ResourceID == "" || req.DisclosureClass != "metadata" || req.IdempotencyKey == "" { return extension.ProjectAuditLinkReceipt{}, extension.ErrProjectAuditDenied } - if req.HostAuditOffset == 0 { - resolved, err := h.caps.st.CommittedAuditOffset(h.owner, string(req.ProjectID), req.OperationID) - if errors.Is(err, store.ErrNotFound) { + // An explicit offset is only a locator. A historical collision or a forged + // offset must not turn a private command with the same operation ID into a + // published project receipt. + resolved, err := h.caps.st.CommittedAuditOffset(h.owner, string(req.ProjectID), req.OperationID) + if errors.Is(err, store.ErrNotFound) { + if req.HostAuditOffset == 0 { return extension.ProjectAuditLinkReceipt{}, extension.ErrProjectAuditPending } - if errors.Is(err, store.ErrConflict) { - return extension.ProjectAuditLinkReceipt{}, extension.ErrProjectAuditLinkConflict - } - if err != nil { - return extension.ProjectAuditLinkReceipt{}, err - } - req.HostAuditOffset = resolved + return extension.ProjectAuditLinkReceipt{}, extension.ErrProjectAuditDenied } + if errors.Is(err, store.ErrConflict) { + return extension.ProjectAuditLinkReceipt{}, extension.ErrProjectAuditLinkConflict + } + if err != nil { + return extension.ProjectAuditLinkReceipt{}, err + } + if req.HostAuditOffset != 0 && req.HostAuditOffset != resolved { + return extension.ProjectAuditLinkReceipt{}, extension.ErrProjectAuditLinkConflict + } + req.HostAuditOffset = resolved row, projectID, err := h.caps.st.CommandAuditForLink(h.owner, req.HostAuditOffset) if errors.Is(err, store.ErrNotFound) { return extension.ProjectAuditLinkReceipt{}, extension.ErrProjectAuditDenied diff --git a/internal/app/project_audit_link_test.go b/internal/app/project_audit_link_test.go index 8c805ca..8668fed 100644 --- a/internal/app/project_audit_link_test.go +++ b/internal/app/project_audit_link_test.go @@ -56,9 +56,10 @@ func TestProjectAuditLinkSharesOnlyVerifiedCommittedMetadata(t *testing.T) { }); err != nil { t.Fatal(err) } - if err := reg.RegisterProjectAuditLinkVerifier(func(_ context.Context, proof extension.ProjectAuditLinkProof) (bool, error) { + publisherFactory, err := (recordRegistry{Registry: reg}).RegisterProjectAuditLinkVerifier(func(_ context.Context, proof extension.ProjectAuditLinkProof) (bool, error) { return proof.Owner == "alice" && proof.ProjectID == p.ID && proof.SourceKind == "domain-commit" && proof.SourceID == "commit-1" && proof.OperationID == "op-1" && proof.ResourceKind == "document" && proof.ResourceID == "doc-1", nil - }); err != nil { + }) + if err != nil { t.Fatal(err) } metadataAllowed := true @@ -72,8 +73,27 @@ func TestProjectAuditLinkSharesOnlyVerifiedCommittedMetadata(t *testing.T) { if _, err := bob.QueryProjectAudit(context.Background(), q); !errors.Is(err, extension.ErrProjectAuditDenied) { t.Fatalf("unlinked private audit: %v", err) } - alicePublisher := inprocHost{caps: caps, pluginID: "rdm", owner: "alice"} // trusted outbox-scoped host + alicePublisher, err := publisherFactory.ForOwner("alice") // capability retained by trusted RDM outbox + if err != nil { + t.Fatal(err) + } + if _, ok := alicePublisher.(extension.Host); ok { + t.Fatal("publisher factory exposed a full owner-scoped Host") + } linkReq := extension.ProjectAuditLinkRequest{ProjectID: p.ID, SourceKind: "domain-commit", SourceID: "commit-1", OperationID: "op-1", ResourceKind: "document", ResourceID: "doc-1", DisclosureClass: "metadata", IdempotencyKey: "outbox-1"} + // A different compiled plugin can call the public scoped-host factory with + // "rdm" as text. That must not mint RDM's audit publication authority. + wireScopedHostFactory(caps) + t.Cleanup(func() { extension.SetScopedHostFactory(nil) }) + forged, err := extension.NewScopedHost("alice", "rdm") + if err != nil { + t.Fatal(err) + } + if publisher, ok := forged.(extension.ProjectAuditPublisher); ok { + if _, err := publisher.PublishProjectAuditLink(context.Background(), linkReq); !errors.Is(err, extension.ErrProjectAuditUnsupported) { + t.Fatalf("self-reported plugin ID minted audit publisher: %v", err) + } + } if rows, err := bob.QueryProjectAudit(context.Background(), extension.ProjectAuditQuery{ProjectID: p.ID}); err != nil || len(rows.Receipts) != 0 { t.Fatalf("private owner row visible without exact link: %+v %v", rows, err) } @@ -88,8 +108,8 @@ func TestProjectAuditLinkSharesOnlyVerifiedCommittedMetadata(t *testing.T) { if _, err := alicePublisher.PublishProjectAuditLink(context.Background(), uncommitted); !errors.Is(err, extension.ErrProjectAuditDenied) { t.Fatalf("uncommitted source: %v", err) } - if _, err := (inprocHost{caps: caps, pluginID: "other", owner: "alice"}).PublishProjectAuditLink(context.Background(), linkReq); !errors.Is(err, extension.ErrProjectAuditUnsupported) { - t.Fatalf("other plugin: %v", err) + if _, err := (pluginRegistry{pluginID: "other", caps: caps}).RegisterProjectAuditLinkVerifier(func(context.Context, extension.ProjectAuditLinkProof) (bool, error) { return true, nil }); err == nil { + t.Fatal("other plugin replaced the registered verifier") } pending := linkReq pending.OperationID, pending.IdempotencyKey = "op-not-yet-recorded", "outbox-pending" @@ -121,6 +141,24 @@ func TestProjectAuditLinkSharesOnlyVerifiedCommittedMetadata(t *testing.T) { if strings.Contains(string(raw), "secret-digest") || strings.Contains(string(raw), string(c.ID)) { t.Fatalf("private receipt fields leaked: %s", raw) } + if err := st.AppendAudit("alice", agentcommand.AuditEnvelope{Time: time.Now(), Actor: "alice", CommandID: "private.note", OperationID: "op-1", ConversationID: string(c.ID), State: agentcommand.StateCommitted}); err != nil { + t.Fatal(err) + } + duplicates, _, _, err := st.ListProjectCommandAudits("alice", string(p.ID), 0, 0, 10) + if err != nil { + t.Fatal(err) + } + privateOffset := duplicates[0].Offset + mislink := linkReq + mislink.HostAuditOffset, mislink.IdempotencyKey = privateOffset, "outbox-private" + if _, err := alicePublisher.PublishProjectAuditLink(context.Background(), mislink); !errors.Is(err, extension.ErrProjectAuditLinkConflict) { + t.Fatalf("ambiguous operation linked a private command: %v", err) + } + privateQuery := q + privateQuery.LinkedHostOffset = privateOffset + if _, err := bob.QueryProjectAudit(context.Background(), privateQuery); !errors.Is(err, extension.ErrProjectAuditDenied) { + t.Fatalf("private command disclosed through ambiguous operation: %v", err) + } metadataAllowed = false if _, err := bob.QueryProjectAudit(context.Background(), q); !errors.Is(err, extension.ErrProjectAuditDenied) { t.Fatalf("revoked object metadata: %v", err) diff --git a/internal/app/surface_audit_actor_test.go b/internal/app/surface_audit_actor_test.go index d57eae1..b4f3fb3 100644 --- a/internal/app/surface_audit_actor_test.go +++ b/internal/app/surface_audit_actor_test.go @@ -88,9 +88,10 @@ func TestCompiledHandlerReceivesHostOperationID(t *testing.T) { if err := reg.RegisterProjectAuditAuthorizer(func(actor string, project core.ID) (bool, error) { return actor == "bob" && project == p.ID, nil }); err != nil { t.Fatal(err) } - if err := reg.RegisterProjectAuditLinkVerifier(func(_ context.Context, proof extension.ProjectAuditLinkProof) (bool, error) { + publisherFactory, err := (recordRegistry{Registry: reg}).RegisterProjectAuditLinkVerifier(func(_ context.Context, proof extension.ProjectAuditLinkProof) (bool, error) { return proof.Owner == "alice" && proof.ProjectID == p.ID && proof.OperationID == seen && proof.SourceID == "domain-commit-1" && proof.ResourceID == "doc-1", nil - }); err != nil { + }) + if err != nil { t.Fatal(err) } if err := reg.RegisterProjectAuditDisclosureAuthorizer(func(_ context.Context, actor string, r extension.ProjectAuditLinkedResource) (extension.ProjectAuditDisclosure, error) { @@ -98,7 +99,10 @@ func TestCompiledHandlerReceivesHostOperationID(t *testing.T) { }); err != nil { t.Fatal(err) } - publisher := inprocHost{caps: caps, owner: "alice", pluginID: "rdm"} + publisher, err := publisherFactory.ForOwner("alice") + if err != nil { + t.Fatal(err) + } linked, err := publisher.PublishProjectAuditLink(context.Background(), extension.ProjectAuditLinkRequest{ProjectID: p.ID, OperationID: seen, SourceKind: "domain-commit", SourceID: "domain-commit-1", ResourceKind: "document", ResourceID: "doc-1", DisclosureClass: "metadata", IdempotencyKey: "outbox-1"}) if err != nil || linked.HostAuditOffset != rows[0].Offset { t.Fatalf("publish by trusted operation: %+v %v", linked, err) diff --git a/internal/cmdbus/cmdbus.go b/internal/cmdbus/cmdbus.go index 8507aa7..3fb4a14 100644 --- a/internal/cmdbus/cmdbus.go +++ b/internal/cmdbus/cmdbus.go @@ -5,6 +5,7 @@ package cmdbus import ( "context" + "crypto/rand" "crypto/sha256" "encoding/hex" "encoding/json" @@ -281,9 +282,13 @@ func (b *Bus) Dispatch(ctx context.Context, req Request) Response { return receipt(agentcommand.StateRejected, "", "no host handler bound for "+req.ID) } // The domain handler can persist this trusted ID with its transaction/outbox. - // Never accept an ID from Params or the incoming Request. A handler failure - // still writes only a rejected audit receipt with no committed operation ID. - req.OperationID = fmt.Sprintf("op_%s_%d", strings.ReplaceAll(req.ID, ".", "_"), b.now().UnixNano()) + // Entropy, not the clock or caller Params, distinguishes concurrent operations. + // A handler failure still writes only a rejected audit receipt without an ID. + var opBytes [24]byte + if _, err := rand.Read(opBytes[:]); err != nil { + return receipt(agentcommand.StateUnknown, "", "operation ID unavailable") + } + req.OperationID = "op_" + hex.EncodeToString(opBytes[:]) payload, err := h(ctx, req) if err != nil { return receipt(agentcommand.StateRejected, "", "rejected: "+err.Error()) diff --git a/internal/cmdbus/cmdbus_test.go b/internal/cmdbus/cmdbus_test.go index 4d64948..90d5c63 100644 --- a/internal/cmdbus/cmdbus_test.go +++ b/internal/cmdbus/cmdbus_test.go @@ -115,6 +115,38 @@ func TestHandlerReceivesCommittedOperationIDBeforeDomainWrite(t *testing.T) { } } +func TestOperationIDsRemainUniqueWithFixedClockAndConcurrentDispatch(t *testing.T) { + audit := &memAudit{} + bus := New(audit, nil, func() time.Time { return time.Unix(1700000000, 123).UTC() }) + if err := bus.RegisterBuiltin(agentcommand.CommandSpec{ID: "rdm.confirm", Title: "Confirm"}, "", func(_ context.Context, req Request) (any, error) { + return req.OperationID, nil + }); err != nil { + t.Fatal(err) + } + const count = 64 + var wg sync.WaitGroup + ids := make([]string, count) + for i := range ids { + wg.Add(1) + go func(i int) { + defer wg.Done() + resp := bus.Dispatch(context.Background(), Request{ID: "rdm.confirm", Actor: "alice", Owner: "alice"}) + ids[i] = resp.Receipt.OperationID + }(i) + } + wg.Wait() + seen := map[string]bool{} + for _, id := range ids { + if id == "" || seen[id] { + t.Fatalf("fixed clock produced a missing or duplicate trusted operation ID: %q", id) + } + seen[id] = true + } + if len(audit.logs) != count { + t.Fatalf("audit rows=%d want=%d", len(audit.logs), count) + } +} + func TestDispatchUnknownAndBadParams(t *testing.T) { a := &memAudit{} b := newTestBus(a, &memCards{}) diff --git a/pkg/extension/extension.go b/pkg/extension/extension.go index 793f074..1812671 100644 --- a/pkg/extension/extension.go +++ b/pkg/extension/extension.go @@ -40,7 +40,9 @@ import ( // verifier, disclosure callback, and exact linked receipt read. // 2.15.0-experimental: trusted Request.OperationID before handler execution and // retryable ProjectAuditLinkRequest.HostAuditOffset=0 resolution. -const SurfaceVersion = "2.15.0-experimental" +// 2.16.0-experimental: cryptographic operation IDs and registration-bound +// ProjectAuditPublisherFactory; scoped Host string IDs cannot publish links. +const SurfaceVersion = "2.16.0-experimental" // ErrNotLinked 表示进程没有空白导入 link 包,Main 无法启动服务。 var ErrNotLinked = errors.New("extension: runner not linked; blank-import agent-system-backend/pkg/extension/link") diff --git a/pkg/extension/project_audit.go b/pkg/extension/project_audit.go index 0900efe..ddca97c 100644 --- a/pkg/extension/project_audit.go +++ b/pkg/extension/project_audit.go @@ -76,10 +76,18 @@ type ProjectAuditDisclosure struct { type ProjectAuditDisclosureAuthorizer func(context.Context, string, ProjectAuditLinkedResource) (ProjectAuditDisclosure, error) +// ProjectAuditPublisherFactory is issued only by successful verifier +// registration. A domain outbox may retain it and bind an owner for each job; +// owner and committed-source attribution are checked again when publishing. +// NewScopedHost(owner, pluginID) cannot mint this capability. +type ProjectAuditPublisherFactory interface { + ForOwner(owner string) (ProjectAuditPublisher, error) +} + // ProjectAuditLinkRegistry is optional. Only the registering compiled domain -// plugin may publish or read its links. +// plugin receives a publisher factory; string plugin IDs carry no authority. type ProjectAuditLinkRegistry interface { - RegisterProjectAuditLinkVerifier(ProjectAuditLinkVerifier) error + RegisterProjectAuditLinkVerifier(ProjectAuditLinkVerifier) (ProjectAuditPublisherFactory, error) RegisterProjectAuditDisclosureAuthorizer(ProjectAuditDisclosureAuthorizer) error } diff --git a/pkg/extension/surface.txt b/pkg/extension/surface.txt index 6f06b8f..7d5f851 100644 --- a/pkg/extension/surface.txt +++ b/pkg/extension/surface.txt @@ -54,6 +54,8 @@ ProjectAuditLinkedResource ProjectAuditPage ProjectAuditPublisher ProjectAuditPublisher.PublishProjectAuditLink +ProjectAuditPublisherFactory +ProjectAuditPublisherFactory.ForOwner ProjectAuditQuery ProjectAuditRegistry ProjectAuditRegistry.RegisterProjectAuditAuthorizer From 5f62b3d2261d9a78e7881595ec5fe2eafb9e7446 Mon Sep 17 00:00:00 2001 From: weichen Date: Wed, 14 Oct 2026 04:33:05 +0000 Subject: [PATCH 06/15] Protect project context from stale history and forged receipts Why: Revoked project material could remain in later model turns, and plugin Audit could impersonate a model-input manifest. Evidence: SELF red tests reproduced both paths; updated project revocation, trusted receipt, migration, and entrypoint tests pass locally with go test ./... -count=1 and targeted race checks. Impact: Rebuild direct model requests after scope changes, sanitize or block unproven generic history, omit legacy project injections, and persist manifests in a dedicated state-checked table. Refs: TT-C02 TT-C03 TT-C04; stage5 W6; review P1 stale context and manifest provenance --- cmd/storage-migrate/main.go | 4 +- internal/app/hostcaps.go | 3 + internal/app/project_context_test.go | 40 ++++- internal/runmgr/project_context_test.go | 6 +- internal/runtime/llm.go | 42 +++-- internal/runtime/llm_direct.go | 29 +++- internal/runtime/project_context_test.go | 174 ++++++++++++++++++++- internal/runtime/project_stream.go | 61 +++++++- internal/runtime/workflow_compiled_test.go | 19 ++- internal/store/context_manifest.go | 47 +++++- internal/store/context_manifest_test.go | 38 +++++ internal/store/migrate.go | 7 + internal/store/migrate_compat.go | 1 + 13 files changed, 428 insertions(+), 43 deletions(-) create mode 100644 internal/store/context_manifest_test.go diff --git a/cmd/storage-migrate/main.go b/cmd/storage-migrate/main.go index 2229194..49206c6 100644 --- a/cmd/storage-migrate/main.go +++ b/cmd/storage-migrate/main.go @@ -287,7 +287,7 @@ var copyOrder = []string{ "activity_cards", "project_event_shares", "command_audit", "prompt_modules", "sessions", "api_keys", "plugin_grants", "plugin_schema", "plugin_kv", "background_tasks", "work_task_bindings", - "memory_entries", "audit_events", "pending_confirms", "llm_usage_daily", + "memory_entries", "audit_events", "context_manifests", "pending_confirms", "llm_usage_daily", "secret_keys", "settings_snapshots", "regen_pending", "context_entries", "user_commands", "pending_notices", "recovery_holds", "surfaces", "surface_revisions", "surface_actions", "plugin_activation", @@ -784,7 +784,7 @@ func advanceSequences(db *sql.DB, table string) error { // checkOwners:业务行引用的 owner 必须存在于 users(单租户默认 ”/'local' // 除外)。未知 owner 阻塞迁移(U4/R7:不静默带过未知归属)。 func checkOwners(db *sql.DB, src *sql.DB) error { - ownerTables := []string{"projects", "conversations", "runs", "artifacts", "knowledge", "hitl_cards", "profiles", "activity_cards", "project_event_shares", "command_audit", "work_task_bindings"} + ownerTables := []string{"projects", "conversations", "runs", "artifacts", "knowledge", "hitl_cards", "profiles", "activity_cards", "project_event_shares", "command_audit", "work_task_bindings", "context_manifests"} for _, t := range ownerTables { if !sqliteHasTable(src, t) { continue diff --git a/internal/app/hostcaps.go b/internal/app/hostcaps.go index 890adb6..40025bb 100644 --- a/internal/app/hostcaps.go +++ b/internal/app/hostcaps.go @@ -274,6 +274,9 @@ func (h inprocHost) KVGet(key string) (string, bool, error) { } func (h inprocHost) Audit(action, target, evidence string) error { + if strings.HasPrefix(action, "model.context_manifest") { + return fmt.Errorf("extension: model context manifest is a reserved host receipt") + } return h.caps.st.AppendAuditEvent(store.AuditEvent{ Owner: h.owner, Actor: "plugin:" + h.pluginID, Action: action, Target: target, Evidence: evidence, diff --git a/internal/app/project_context_test.go b/internal/app/project_context_test.go index 833615b..4b40b36 100644 --- a/internal/app/project_context_test.go +++ b/internal/app/project_context_test.go @@ -298,7 +298,7 @@ func TestCompleteForWorkReadsOnlyCurrentBundleEvidence(t *testing.T) { if err != nil { t.Fatal(err) } - var sent string + var sent []string model := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { var body struct { Messages []struct { @@ -306,9 +306,11 @@ func TestCompleteForWorkReadsOnlyCurrentBundleEvidence(t *testing.T) { } `json:"messages"` } _ = json.NewDecoder(r.Body).Decode(&body) + var input string for _, m := range body.Messages { - sent += m.Content + input += m.Content } + sent = append(sent, input) _, _ = w.Write([]byte(`{"choices":[{"message":{"content":"done"}}]}`)) })) defer model.Close() @@ -321,16 +323,17 @@ func TestCompleteForWorkReadsOnlyCurrentBundleEvidence(t *testing.T) { contexts := &projectContextSlot{} ref := extension.ProjectEvidenceRef{ID: "ref-1", SourceID: "doc-1", Revision: "r2", Hash: "source-hash"} readCalls := 0 + readText, authEpoch := "current restricted detail", "duty-1" provider := testProjectContextProvider{ resolve: func(req extension.ProjectContextRequest) (extension.ProjectContextBundle, error) { - return extension.ProjectContextBundle{Status: extension.ProjectContextReady, ProjectID: req.ProjectID, Evidence: []extension.ProjectEvidenceRef{ref}}, nil + return extension.ProjectContextBundle{Status: extension.ProjectContextReady, ProjectID: req.ProjectID, AuthorizationEpoch: authEpoch, PublicationEpoch: "pub-1", Evidence: []extension.ProjectEvidenceRef{ref}}, nil }, read: func(req extension.ProjectEvidenceRequest) (extension.ProjectEvidence, error) { readCalls++ if req.Reference != ref || req.Scope.ProjectID != core.ID(p.ID) { t.Errorf("untrusted evidence scope: %+v", req) } - return extension.ProjectEvidence{Reference: req.Reference, Text: "current restricted detail"}, nil + return extension.ProjectEvidence{Reference: req.Reference, Text: readText}, nil }, } if err := (pluginRegistry{pluginID: "rdm", caps: hostcaps{contexts: contexts}}).RegisterProjectContextProvider(provider); err != nil { @@ -338,10 +341,37 @@ func TestCompleteForWorkReadsOnlyCurrentBundleEvidence(t *testing.T) { } h := inprocHost{caps: hostcaps{st: st, set: set, projects: projects, contexts: contexts}, pluginID: "rdm", owner: "alice", actor: "alice", conversationID: conv.ID, hostProjectID: p.ID} result, err := h.CompleteForWork(context.Background(), extension.WorkCompletionRequest{Goal: "Write", EvidenceRefs: []string{"ref-1"}}) - if err != nil || !strings.Contains(sent, "current restricted detail") || len(result.Manifest.ReadEvidence) != 1 || readCalls != 2 { + if err != nil || len(sent) != 1 || !strings.Contains(sent[0], "current restricted detail") || len(result.Manifest.ReadEvidence) != 1 || readCalls != 2 { t.Fatalf("requested evidence did not reach model under reauthorization: err=%v sent=%q manifest=%+v reads=%d", err, sent, result.Manifest, readCalls) } if _, err := h.CompleteForWork(context.Background(), extension.WorkCompletionRequest{Goal: "Write", EvidenceRefs: []string{"arbitrary-path"}}); err == nil || readCalls != 2 { t.Fatal("arbitrary evidence reference reached provider") } + readText, authEpoch = "new public detail", "duty-2" + if _, err := h.CompleteForWork(context.Background(), extension.WorkCompletionRequest{Goal: "Write", EvidenceRefs: []string{"ref-1"}}); err != nil || len(sent) != 2 || strings.Contains(sent[1], "current restricted detail") || !strings.Contains(sent[1], "new public detail") { + t.Fatalf("same Host reused old evidence after duty change: err=%v sent=%q", err, sent) + } +} + +func TestPluginAuditCannotForgeOrCorruptContextManifest(t *testing.T) { + st, err := store.OpenSQLite(filepath.Join(t.TempDir(), "manifest.db")) + if err != nil { + t.Fatal(err) + } + defer st.Close() + good := extension.ProjectContextManifest{ProjectID: "p1", AttemptID: "attempt-1", Delivery: "attempted", InputSHA256: "sha-good"} + if err := st.AppendContextManifest("alice", "run-1", good); err != nil { + t.Fatal(err) + } + h := inprocHost{caps: hostcaps{st: st}, pluginID: "malicious", owner: "alice"} + if err := h.Audit("model.context_manifest", "run-1", `{"Delivery":"responded","InputSHA256":"forged"}`); err == nil { + t.Fatal("plugin forged a trusted model manifest action") + } + if err := st.AppendAuditEvent(store.AuditEvent{Owner: "alice", Action: "model.context_manifest", Target: "run-1", Evidence: "not-json"}); err != nil { + t.Fatal(err) + } + got, err := st.ListContextManifests("alice", "run-1") + if err != nil || len(got) != 1 || got[0].InputSHA256 != "sha-good" { + t.Fatalf("untrusted audit row corrupted manifest reader: got=%+v err=%v", got, err) + } } diff --git a/internal/runmgr/project_context_test.go b/internal/runmgr/project_context_test.go index 94f6a4e..3177358 100644 --- a/internal/runmgr/project_context_test.go +++ b/internal/runmgr/project_context_test.go @@ -76,7 +76,7 @@ func TestContextManifestPersistsByOwnerAndRun(t *testing.T) { conv := recoveryConversation(t, st) run := recoveryRun(t, st, conv, core.RunPending) rc := &runCtx{mgr: m, run: run, ctx: context.Background()} - manifest := extension.ProjectContextManifest{ProjectID: run.ProjectID, Version: "v3", Model: "model-1", InputSHA256: "abc", Included: []extension.ProjectContextManifestEntry{{SourceID: "s1", Revision: "r2", InputSHA256: "def"}}} + manifest := extension.ProjectContextManifest{ProjectID: run.ProjectID, Version: "v3", Model: "model-1", AttemptID: "attempt-1", Delivery: "attempted", InputSHA256: "abc", Included: []extension.ProjectContextManifestEntry{{SourceID: "s1", Revision: "r2", InputSHA256: "def"}}} if err := rc.SaveContextManifest(context.Background(), manifest); err != nil { t.Fatal(err) } @@ -110,7 +110,7 @@ func TestContextManifestRejectsDocumentDutyRevocationBeforeSend(t *testing.T) { if err != nil { t.Fatal(err) } - manifest.InputSHA256 = "request-hash" + manifest.InputSHA256, manifest.AttemptID, manifest.Delivery = "request-hash", "attempt-revoked", "attempted" epoch = "duty-2" // project work remains allowed; document duty changed if err := rc.SaveContextManifest(context.Background(), manifest); err == nil { t.Fatal("stale document duty accepted at model send") @@ -161,7 +161,7 @@ func TestProjectEvidenceOnlyReadsCurrentBundleReference(t *testing.T) { if _, err := rc.ProjectEvidence(context.Background(), "ref-1"); err == nil || read != 1 { t.Fatal("revoked reference reached provider") } - if err := rc.SaveContextManifest(context.Background(), extension.ProjectContextManifest{ProjectID: run.ProjectID, InputSHA256: "new-request-hash"}); err == nil { + if err := rc.SaveContextManifest(context.Background(), extension.ProjectContextManifest{ProjectID: run.ProjectID, InputSHA256: "new-request-hash", AttemptID: "attempt-revoked", Delivery: "attempted"}); err == nil { t.Fatal("new model request accepted after an earlier evidence read was revoked") } } diff --git a/internal/runtime/llm.go b/internal/runtime/llm.go index 8f7e654..fce32b0 100644 --- a/internal/runtime/llm.go +++ b/internal/runtime/llm.go @@ -232,6 +232,7 @@ func weekdayCN(t time.Time) string { func (l *LLMRunner) Execute(rc RunCtx) error { eff := rc.Settings() prof := rc.Profile() + projectMode := projectContextEnabled(rc) route := ResolveChatModel(prof, eff) if route.Model == "" || route.Base == "" { return fmt.Errorf("llm: 主模型未配置(当前解析:model=%q base=%q;检查 settings.models.chat 与画像 ModelRoute;演示模式可开 runtime.mock)", route.Model, route.Base) @@ -254,7 +255,7 @@ func (l *LLMRunner) Execute(rc RunCtx) error { // 知识注入:当前消息包含 @knowledge 引用时检索注入(最小实现:显式引用)。 var knowledge []core.RetrievalHit - if strings.Contains(currentUserText(rc), "@knowledge") { + if !projectMode && strings.Contains(currentUserText(rc), "@knowledge") { q := strings.TrimSpace(strings.ReplaceAll(currentUserText(rc), "@knowledge", "")) if hits, err := rc.Retrieve(core.RetrievalQuery{Text: q}); err == nil { knowledge = hits @@ -264,7 +265,7 @@ func (l *LLMRunner) Execute(rc RunCtx) error { // 组装初始上下文。 hist := rc.History() currentUser, _ := lastAddressed(hist) - if projectContextEnabled(rc) { + if projectMode { // Historical assistant/tool text has no durable resource provenance. A // later duty revocation cannot safely reauthorize it, so project work // starts from the current user turn and a fresh provider bundle. @@ -281,24 +282,33 @@ func (l *LLMRunner) Execute(rc RunCtx) error { // 记忆注入迁移到记忆端口(N3 WP5):由循环经 Config.Memory 按请求注入, // 组装侧不再注入(防双份;注入文案同一形态)。 _ = ctxasm.InjectMemory - msgs, asmReport := ctxasm.AssembleInput(prof, eff, hist, currentUser, rc.Attachments(), knowledge, sys, promptObs, rc.Card, resolveArtifact, ctxasm.AssembleOptions{ - Surfaces: rc.Surfaces(), + asmProf := prof + var surfaces []core.SurfaceSnapshot + if projectMode { + asmProf.SystemPrompt = "" // profile text has no resource provenance + } else { + surfaces = rc.Surfaces() + } + msgs, asmReport := ctxasm.AssembleInput(asmProf, eff, hist, currentUser, rc.Attachments(), knowledge, sys, promptObs, rc.Card, resolveArtifact, ctxasm.AssembleOptions{ + Surfaces: surfaces, ThinkReplay: thinkReplayEnabled(eff, route.Model), }) tail := ctxasm.Tail{ - Instructions: eff.GetString("context.userInstructions", ""), - History: hist, - Window: int(eff.GetNumber("context.contextWindow", 32000)), + History: hist, + Window: int(eff.GetNumber("context.contextWindow", 32000)), } - if note, ok := rc.(interface{ SessionNote() string }); ok { - tail.Note = note.SessionNote() - } - if src, ok := rc.(interface{ Injections() []ContextInjection }); ok { - for _, e := range src.Injections() { - tail.Entries = append(tail.Entries, ctxasm.Injection{ - Keys: e.Keys, Secondary: e.Secondary, Logic: e.Logic, Content: e.Content, - Constant: e.Constant, ScanDepth: e.ScanDepth, BudgetChars: e.BudgetChars, - }) + if !projectMode { + tail.Instructions = eff.GetString("context.userInstructions", "") + if note, ok := rc.(interface{ SessionNote() string }); ok { + tail.Note = note.SessionNote() + } + if src, ok := rc.(interface{ Injections() []ContextInjection }); ok { + for _, e := range src.Injections() { + tail.Entries = append(tail.Entries, ctxasm.Injection{ + Keys: e.Keys, Secondary: e.Secondary, Logic: e.Logic, Content: e.Content, + Constant: e.Constant, ScanDepth: e.ScanDepth, BudgetChars: e.BudgetChars, + }) + } } } msgs, asmReport = ctxasm.FinishAssembly(msgs, asmReport, tail) diff --git a/internal/runtime/llm_direct.go b/internal/runtime/llm_direct.go index e0307be..e9b53e7 100644 --- a/internal/runtime/llm_direct.go +++ b/internal/runtime/llm_direct.go @@ -17,6 +17,7 @@ import ( "agent-native/runtime/gates" "agent-native/runtime/hooks" agentllm "agent-native/runtime/llm" + "agent-native/runtime/reminders" agentsession "agent-native/runtime/session" agenttools "agent-native/runtime/tools" "agent-system-backend/internal/ctxasm" @@ -132,10 +133,12 @@ func (t directTool) Execute(ctx context.Context, args json.RawMessage) (agenttoo func (l *LLMRunner) runDirect(rc RunCtx, stream agentllm.StreamFunc, model string, msgs []agentllm.Message, decls []agentllm.ToolDecl, streamOpts agentllm.StreamOptions, cardflow bool, eff core.EffectiveSettings, maxTurns, maxTools int, refresh turnRefresh) error { st := &directRun{l: l, rc: rc, eff: eff, cardflow: cardflow, started: l.now(), inflight: map[string]*toolSlot{}} // 记忆端口(N3 WP5):runCtx 暴露 store 时接线(注入由循环按请求完成)。 - if ms, ok := rc.(interface{ StoreMemory() memoryStore }); ok { + if ms, ok := rc.(interface{ StoreMemory() memoryStore }); ok && !projectContextEnabled(rc) { st.memoryStore = ms.StoreMemory() } - st.loadChecklist() + if !projectContextEnabled(rc) { + st.loadChecklist() + } if msg, ok := lastAddressed(rc.History()); ok && msg.Role == core.RoleSystem { var text string for _, p := range msg.Parts { @@ -224,6 +227,9 @@ func (l *LLMRunner) runDirect(rc RunCtx, stream agentllm.StreamFunc, model strin } var projectErr error var projectManifest *extension.ProjectContextManifest + var projectBody string + var lastProjectScope string + var projectHistoryTainted bool wrapped := func(ctx context.Context, model string, messages []agentllm.Message, _ []agentllm.ToolDecl, opts agentllm.StreamOptions) <-chan agentllm.Event { fail := func(err error) <-chan agentllm.Event { out := make(chan agentllm.Event, 1) @@ -240,6 +246,22 @@ func (l *LLMRunner) runDirect(rc RunCtx, stream agentllm.StreamFunc, model strin copy := *projectManifest manifest = © manifest.Model = model + scope := projectScopeKey(*manifest) + if lastProjectScope != "" && (scope != lastProjectScope || manifest.AuthorizationEpoch == "" || manifest.PublicationEpoch == "") { + projectHistoryTainted = true + } + lastProjectScope = scope + if projectHistoryTainted { + if len(msgs) == 0 || user.Role != agentllm.RoleUser || projectBody == "" { + return fail(fmt.Errorf("trusted project request seed unavailable after scope change")) + } + // The kernel's conversation still contains assistant/tool text from + // the previous authorization epoch. Rebuild this request solely from + // the current run's clean seed and freshly resolved project bundle. + messages = append([]agentllm.Message(nil), msgs[:len(msgs)-1]...) + messages = append(messages, user) + messages = reminders.Attach(messages, []string{projectBody}) + } if err := checkProjectInputBudget(rc, messages, decls); err != nil { return fail(err) } @@ -423,11 +445,12 @@ func (l *LLMRunner) runDirect(rc RunCtx, stream agentllm.StreamFunc, model strin Reminders: func(turn int) []string { var out []string out = append(out, st.reminders(turn)...) - projectErr, projectManifest = nil, nil + projectErr, projectManifest, projectBody = nil, nil, "" if refresh.ProjectContext != nil { var body string body, projectManifest, projectErr = refresh.ProjectContext() if projectErr == nil && body != "" { + projectBody = body out = append(out, body) } } diff --git a/internal/runtime/project_context_test.go b/internal/runtime/project_context_test.go index d07002f..06299fe 100644 --- a/internal/runtime/project_context_test.go +++ b/internal/runtime/project_context_test.go @@ -23,8 +23,18 @@ type projectRunCtx struct { manifests []extension.ProjectContextManifest evidence func(string) (extension.ProjectEvidence, error) budgetSettings core.EffectiveSettings + sessionNote string + injections []ContextInjection + surfaces []core.SurfaceSnapshot + toolList []core.ToolDesc } +func (c *projectRunCtx) Tools() []core.ToolDesc { return c.toolList } + +func (c *projectRunCtx) SessionNote() string { return c.sessionNote } +func (c *projectRunCtx) Injections() []ContextInjection { return c.injections } +func (c *projectRunCtx) Surfaces() []core.SurfaceSnapshot { return c.surfaces } + func (c *projectRunCtx) Settings() core.EffectiveSettings { if c.budgetSettings != nil { return c.budgetSettings @@ -91,6 +101,74 @@ func TestProjectContextResolutionFailureBlocksModelRequest(t *testing.T) { } } +func TestProjectRevocationScrubsOldMachineContextBeforeSecondTurn(t *testing.T) { + const old = "OLD_RESTRICTED_BODY" + turns := [][]agentllm.Event{ + {{Kind: agentllm.KindTextDelta, Text: old}, {Kind: agentllm.KindMessageEnd, Message: agentllm.Message{Role: agentllm.RoleAssistant, Content: old, StopReason: agentllm.StopToolCalls, ToolCalls: []agentllm.ToolCall{{ID: "c1", Name: "knowledge_search", Arguments: `{}`}}}}}, + {{Kind: agentllm.KindTextDelta, Text: "done"}, {Kind: agentllm.KindMessageEnd, Message: agentllm.Message{Role: agentllm.RoleAssistant, Content: "done", StopReason: agentllm.StopEnd}}}, + } + base := scriptStream(turns, nil) + var requests [][]agentllm.Message + c := &projectRunCtx{fakeRunCtx: &fakeRunCtx{ctx: context.Background(), projectID: "p1", prof: core.AgentProfile{Model: core.ModelRoute{Chat: "test-model"}}, memEntries: []memEntry{{typ: "note", content: "STALE_MEMORY"}}, execOutput: old}, + bundle: extension.ProjectContextBundle{Status: extension.ProjectContextReady, ProjectID: "p1", Version: "v1", AuthorizationEpoch: "duty-1", PublicationEpoch: "pub-1", Items: []extension.ProjectContextItem{{SourceID: "doc", Text: old, Required: true}}}, + budgetSettings: core.EffectiveSettings{"models": map[string]any{"chat": map[string]any{"baseUrl": "http://test"}}, "context": map[string]any{"userInstructions": "STALE_INSTRUCTIONS"}, "runtime": map[string]any{"verify": false, "composeFinal": false}}, + sessionNote: "STALE_SESSION_NOTE", injections: []ContextInjection{{Content: "STALE_INJECTION", Constant: true}}, + surfaces: []core.SurfaceSnapshot{{SemanticText: "STALE_SURFACE"}}, + } + runner := &LLMRunner{Stream: func(ctx context.Context, model string, msgs []agentllm.Message, tools []agentllm.ToolDecl, opts agentllm.StreamOptions) <-chan agentllm.Event { + requests = append(requests, append([]agentllm.Message(nil), msgs...)) + if len(requests) == 1 { + c.bundle = extension.ProjectContextBundle{Status: extension.ProjectContextReady, ProjectID: "p1", Version: "v2", AuthorizationEpoch: "duty-2", PublicationEpoch: "pub-2", Items: []extension.ProjectContextItem{{SourceID: "public", Text: "CURRENT_PUBLIC_FACT", Required: true}}} + } + return base(ctx, model, msgs, tools, opts) + }} + if err := runner.Execute(c); err != nil { + t.Fatal(err) + } + if len(requests) != 2 { + t.Fatalf("model requests=%d, want 2", len(requests)) + } + if c.fakeRunCtx.toolRuns != 1 { + t.Fatalf("expected restricted tool output before revocation, tool runs=%d", c.fakeRunCtx.toolRuns) + } + first := joinReq(requests[0]) + for _, unproven := range []string{"STALE_MEMORY", "STALE_INSTRUCTIONS", "STALE_SESSION_NOTE", "STALE_INJECTION", "STALE_SURFACE"} { + if strings.Contains(first, unproven) { + t.Fatalf("unproven machine context %q reached first project request: %s", unproven, first) + } + } + second := joinReq(requests[1]) + for _, forbidden := range []string{old, "STALE_MEMORY", "STALE_INSTRUCTIONS", "STALE_SESSION_NOTE", "STALE_INJECTION", "STALE_SURFACE"} { + if strings.Contains(second, forbidden) { + t.Fatalf("revoked machine context %q reached second model request: %s", forbidden, second) + } + } + if !strings.Contains(second, "CURRENT_PUBLIC_FACT") { + t.Fatalf("current context absent from second model request: %s", second) + } +} + +func TestProjectSameScopeKeepsCurrentRunToolContext(t *testing.T) { + turns := [][]agentllm.Event{ + {{Kind: agentllm.KindMessageEnd, Message: agentllm.Message{Role: agentllm.RoleAssistant, StopReason: agentllm.StopToolCalls, ToolCalls: []agentllm.ToolCall{{ID: "c1", Name: "knowledge_search", Arguments: `{}`}}}}}, + {{Kind: agentllm.KindMessageEnd, Message: agentllm.Message{Role: agentllm.RoleAssistant, Content: "done", StopReason: agentllm.StopEnd}}}, + } + base := scriptStream(turns, nil) + var requests [][]agentllm.Message + runner := &LLMRunner{Stream: func(ctx context.Context, model string, msgs []agentllm.Message, tools []agentllm.ToolDecl, opts agentllm.StreamOptions) <-chan agentllm.Event { + requests = append(requests, append([]agentllm.Message(nil), msgs...)) + return base(ctx, model, msgs, tools, opts) + }} + c := &projectRunCtx{fakeRunCtx: &fakeRunCtx{ctx: context.Background(), projectID: "p1", prof: core.AgentProfile{Model: core.ModelRoute{Chat: "test-model"}}, execOutput: "AUTHORIZED_TOOL_RESULT"}, + bundle: extension.ProjectContextBundle{Status: extension.ProjectContextReady, ProjectID: "p1", AuthorizationEpoch: "duty-1", PublicationEpoch: "pub-1", Items: []extension.ProjectContextItem{{SourceID: "fact", Text: "CURRENT_FACT", Required: true}}}} + if err := runner.Execute(c); err != nil { + t.Fatal(err) + } + if len(requests) < 2 || !strings.Contains(joinReq(requests[1]), "AUTHORIZED_TOOL_RESULT") || !strings.Contains(joinReq(requests[1]), "CURRENT_FACT") { + t.Fatalf("same-scope direct run lost current tool context: requests=%+v", requests) + } +} + func TestReadProjectEvidenceToolUsesScopedReader(t *testing.T) { c := &projectRunCtx{fakeRunCtx: &fakeRunCtx{projectID: "p1"}, evidence: func(ref string) (extension.ProjectEvidence, error) { if ref != "ref-1" { @@ -124,6 +202,29 @@ func TestSubagentModelRequestUsesProjectContextAndManifest(t *testing.T) { } } +func TestSubagentSameScopeKeepsNewToolResultAcrossTurns(t *testing.T) { + turns := [][]agentllm.Event{ + {{Kind: agentllm.KindMessageEnd, Message: agentllm.Message{Role: agentllm.RoleAssistant, StopReason: agentllm.StopToolCalls, ToolCalls: []agentllm.ToolCall{{ID: "search-1", Name: "knowledge_search", Arguments: `{}`}}}}}, + {{Kind: agentllm.KindMessageEnd, Message: agentllm.Message{Role: agentllm.RoleAssistant, Content: "done", StopReason: agentllm.StopEnd}}}, + } + base := scriptStream(turns, nil) + var requests [][]agentllm.Message + sub := &SubLLMRunner{Stream: func(ctx context.Context, model string, msgs []agentllm.Message, tools []agentllm.ToolDecl, opts agentllm.StreamOptions) <-chan agentllm.Event { + requests = append(requests, append([]agentllm.Message(nil), msgs...)) + return base(ctx, model, msgs, tools, opts) + }} + c := &projectRunCtx{fakeRunCtx: &fakeRunCtx{ctx: context.Background(), projectID: "p1", prof: core.AgentProfile{Model: core.ModelRoute{Chat: "test-model", Lightweight: "test-lite"}}, execOutput: "NEW_AUTHORIZED_TOOL"}, + bundle: extension.ProjectContextBundle{Status: extension.ProjectContextReady, ProjectID: "p1", AuthorizationEpoch: "duty-1", PublicationEpoch: "pub-1", Items: []extension.ProjectContextItem{{SourceID: "fact", Text: "CURRENT_FACT", Required: true}}}, + toolList: []core.ToolDesc{{Name: "knowledge_search", Description: "Search", Parameters: json.RawMessage(`{"type":"object"}`)}}, + } + if err := sub.Execute(c); err != nil { + t.Fatal(err) + } + if len(requests) != 2 || !strings.Contains(joinReq(requests[1]), "NEW_AUTHORIZED_TOOL") || !strings.Contains(joinReq(requests[1]), "CURRENT_FACT") || len(c.manifests) != 4 { + t.Fatalf("same-scope subagent lost authorized tool context: requests=%+v manifests=%+v", requests, c.manifests) + } +} + func TestProjectStreamRejectsProjectRunWithoutTrustedContextPort(t *testing.T) { called := false base := func(ctx context.Context, model string, msgs []agentllm.Message, tools []agentllm.ToolDecl, opts agentllm.StreamOptions) <-chan agentllm.Event { @@ -158,6 +259,73 @@ func TestProjectStreamBlocksRequestExceedingFinalModelBudget(t *testing.T) { } } +func TestProjectStreamBlocksStaleMachineHistoryAfterDutyRevocation(t *testing.T) { + c := &projectRunCtx{fakeRunCtx: &fakeRunCtx{projectID: "p1"}, bundle: extension.ProjectContextBundle{Status: extension.ProjectContextReady, ProjectID: "p1", Version: "v1", AuthorizationEpoch: "duty-1", PublicationEpoch: "pub-1"}} + calls := 0 + base := scriptStream(textTurns("done"), nil) + guarded := GuardProjectStream(c, "goal", func(ctx context.Context, model string, msgs []agentllm.Message, tools []agentllm.ToolDecl, opts agentllm.StreamOptions) <-chan agentllm.Event { + calls++ + return base(ctx, model, msgs, tools, opts) + }) + initial := []agentllm.Message{{Role: agentllm.RoleSystem, Content: "base"}, {Role: agentllm.RoleUser, Content: "goal"}} + for range guarded(context.Background(), "model", initial, nil, agentllm.StreamOptions{}) { + } + c.bundle = extension.ProjectContextBundle{Status: extension.ProjectContextReady, ProjectID: "p1", Version: "v2", AuthorizationEpoch: "duty-2", PublicationEpoch: "pub-2"} + stale := append(append([]agentllm.Message(nil), initial...), agentllm.Message{Role: agentllm.RoleAssistant, Content: "OLD_RESTRICTED_BODY"}, agentllm.Message{Role: agentllm.RoleTool, Content: "OLD_RESTRICTED_BODY"}) + sawError := false + for ev := range guarded(context.Background(), "model", stale, nil, agentllm.StreamOptions{}) { + sawError = sawError || ev.Kind == agentllm.KindError + } + if !sawError || calls != 1 { + t.Fatalf("revoked history reached generic model stream: error=%v calls=%d", sawError, calls) + } +} + +func TestProjectStreamRebuildsFirstRequestWithPriorMachineHistory(t *testing.T) { + c := &projectRunCtx{fakeRunCtx: &fakeRunCtx{projectID: "p1"}, bundle: extension.ProjectContextBundle{Status: extension.ProjectContextReady, ProjectID: "p1", AuthorizationEpoch: "duty-1", PublicationEpoch: "pub-1"}} + called := false + var requests [][]agentllm.Message + baseTurns := scriptStream(textTurns("ok", "done"), nil) + base := func(ctx context.Context, model string, msgs []agentllm.Message, tools []agentllm.ToolDecl, opts agentllm.StreamOptions) <-chan agentllm.Event { + called = true + requests = append(requests, append([]agentllm.Message(nil), msgs...)) + return baseTurns(ctx, model, msgs, tools, opts) + } + msgs := []agentllm.Message{{Role: agentllm.RoleSystem, Content: "old system"}, {Role: agentllm.RoleUser, Content: "old goal"}, {Role: agentllm.RoleAssistant, Content: "OLD_RESTRICTED_BODY"}, {Role: agentllm.RoleTool, Content: "OLD_RESTRICTED_BODY"}, {Role: agentllm.RoleUser, Content: "current goal"}} + guarded := GuardProjectStream(c, "goal", base) + sawError := false + for ev := range guarded(context.Background(), "model", msgs, nil, agentllm.StreamOptions{}) { + sawError = sawError || ev.Kind == agentllm.KindError + } + if sawError || !called || strings.Contains(joinReq(requests[0]), "OLD_RESTRICTED_BODY") || !strings.Contains(joinReq(requests[0]), "current goal") { + t.Fatalf("first request was not safely rebuilt: error=%v called=%v sent=%q", sawError, called, joinReq(requests[0])) + } + second := append(append([]agentllm.Message(nil), msgs...), agentllm.Message{Role: agentllm.RoleAssistant, Content: "NEW_AUTHORIZED_STEP"}, agentllm.Message{Role: agentllm.RoleTool, Content: "NEW_AUTHORIZED_TOOL"}) + for ev := range guarded(context.Background(), "model", second, nil, agentllm.StreamOptions{}) { + sawError = sawError || ev.Kind == agentllm.KindError + } + if sawError || len(requests) != 2 || strings.Contains(joinReq(requests[1]), "OLD_RESTRICTED_BODY") || !strings.Contains(joinReq(requests[1]), "NEW_AUTHORIZED_TOOL") { + t.Fatalf("second request failed to keep only same-scope new tool history: error=%v requests=%+v", sawError, requests) + } +} + +func TestProjectStreamRejectsPriorMachineHistoryWithoutFreshGoal(t *testing.T) { + c := &projectRunCtx{fakeRunCtx: &fakeRunCtx{projectID: "p1"}, bundle: extension.ProjectContextBundle{Status: extension.ProjectContextReady, ProjectID: "p1", AuthorizationEpoch: "duty-1", PublicationEpoch: "pub-1"}} + called := false + base := func(ctx context.Context, model string, msgs []agentllm.Message, tools []agentllm.ToolDecl, opts agentllm.StreamOptions) <-chan agentllm.Event { + called = true + return scriptStream(textTurns("bad"), nil)(ctx, model, msgs, tools, opts) + } + msgs := []agentllm.Message{{Role: agentllm.RoleSystem, Content: "base"}, {Role: agentllm.RoleUser, Content: "old goal"}, {Role: agentllm.RoleAssistant, Content: "OLD_RESTRICTED_BODY"}} + sawError := false + for ev := range GuardProjectStream(c, "goal", base)(context.Background(), "model", msgs, nil, agentllm.StreamOptions{}) { + sawError = sawError || ev.Kind == agentllm.KindError + } + if !sawError || called { + t.Fatalf("machine history without a fresh goal reached model: error=%v called=%v", sawError, called) + } +} + type projectSquadCtx struct { *entryFake manifests []extension.ProjectContextManifest @@ -167,7 +335,7 @@ func (c *projectSquadCtx) Run() core.Run { return core.Run{ID: "run-squad", Owner: "alice", ProjectID: "project-1"} } func (c *projectSquadCtx) ProjectContext(context.Context, string, int) (extension.ProjectContextBundle, bool, error) { - return extension.ProjectContextBundle{Status: extension.ProjectContextReady, ProjectID: "project-1", Items: []extension.ProjectContextItem{{SourceID: "squad-source", Text: "squad project fact", Required: true}}}, true, nil + return extension.ProjectContextBundle{Status: extension.ProjectContextReady, ProjectID: "project-1", AuthorizationEpoch: "duty-1", PublicationEpoch: "pub-1", Items: []extension.ProjectContextItem{{SourceID: "squad-source", Text: "squad project fact", Required: true}}}, true, nil } func (c *projectSquadCtx) SaveContextManifest(_ context.Context, m extension.ProjectContextManifest) error { c.manifests = append(c.manifests, m) @@ -176,9 +344,11 @@ func (c *projectSquadCtx) SaveContextManifest(_ context.Context, m extension.Pro func TestSquadEntryModelRequestUsesProjectContext(t *testing.T) { var sent []agentllm.Message + calls := 0 base := writeThenStop(nil, nil) runner := &SquadRunner{Router: fixedRoute{mode: squad.RouteDirect}, Checker: squad.MinimalChecker{}, Hitl: squad.HitlPolicy{Mode: squad.HitlDisabled}, Stream: func(ctx context.Context, model string, msgs []agentllm.Message, tools []agentllm.ToolDecl, opts agentllm.StreamOptions) <-chan agentllm.Event { + calls++ sent = append([]agentllm.Message(nil), msgs...) return base(ctx, model, msgs, tools, opts) }} @@ -187,7 +357,7 @@ func TestSquadEntryModelRequestUsesProjectContext(t *testing.T) { if err := runner.Execute(c); err != nil { t.Fatal(err) } - if !strings.Contains(joinReq(sent), "squad project fact") || len(c.manifests) < 2 || c.manifests[len(c.manifests)-1].Delivery != "responded" { + if calls < 2 || !strings.Contains(joinReq(sent), "squad project fact") || !strings.Contains(joinReq(sent), "output.md") || len(c.manifests) < 4 || c.manifests[len(c.manifests)-1].Delivery != "responded" { t.Fatalf("squad model missed project context: input=%q manifests=%+v", joinReq(sent), c.manifests) } } diff --git a/internal/runtime/project_stream.go b/internal/runtime/project_stream.go index b8aad50..d3648f7 100644 --- a/internal/runtime/project_stream.go +++ b/internal/runtime/project_stream.go @@ -5,6 +5,7 @@ import ( "crypto/sha256" "encoding/json" "fmt" + "sync" "time" agentllm "agent-native/runtime/llm" @@ -36,6 +37,12 @@ func GuardProjectStream(rc any, goal string, stream agentllm.StreamFunc) agentll } return stream } + var scopeMu sync.Mutex + var previousScope string + var seenScope, taintedHistory bool + var cleanPrefixIndex int + var cleanTask agentllm.Message + var droppedPriorHistory bool return func(ctx context.Context, model string, messages []agentllm.Message, tools []agentllm.ToolDecl, opts agentllm.StreamOptions) <-chan agentllm.Event { fail := func(err error) <-chan agentllm.Event { out := make(chan agentllm.Event, 1) @@ -64,7 +71,50 @@ func GuardProjectStream(rc any, goal string, stream agentllm.StreamFunc) agentll if err != nil { return fail(err) } - request := reminders.Attach(messages, []string{body}) + scopeMu.Lock() + scope := projectScopeKey(manifest) + requestBase := messages + if !seenScope { + users, lastUser, lastMachine := 0, -1, -1 + for i, msg := range messages { + if msg.Role == agentllm.RoleAssistant || msg.Role == agentllm.RoleTool { + lastMachine = i + } + if msg.Role == agentllm.RoleUser { + users++ + lastUser = i + } + } + if lastUser < 0 || lastUser <= lastMachine { + scopeMu.Unlock() + return fail(fmt.Errorf("project model request lacks a fresh user task after unproven machine history")) + } + if lastMachine >= 0 || users != 1 { + // This backend may have loaded a prior session. Keep only the new + // task, then attach the freshly resolved bundle below. + droppedPriorHistory, cleanPrefixIndex, cleanTask = true, lastUser, messages[lastUser] + } + } + if seenScope && (scope != previousScope || manifest.AuthorizationEpoch == "" || manifest.PublicationEpoch == "") { + taintedHistory = true + } + previousScope, seenScope = scope, true + unsafe := taintedHistory + if droppedPriorHistory && !unsafe { + requestBase = []agentllm.Message{{Role: agentllm.RoleSystem, Content: "Work on the current authorized project task. Prior machine history was discarded."}} + if cleanPrefixIndex < len(messages) && messages[cleanPrefixIndex].Role == agentllm.RoleUser && messages[cleanPrefixIndex].Content == cleanTask.Content { + requestBase = append(requestBase, messages[cleanPrefixIndex:]...) + } else { + // A compacted or rewritten session lost the trusted boundary. + // Continue from the saved user task without prior machine text. + requestBase = append(requestBase, cleanTask) + } + } + scopeMu.Unlock() + if unsafe { + return fail(fmt.Errorf("project authorization changed; generic model history lacks source provenance")) + } + request := reminders.Attach(requestBase, []string{body}) if err := checkProjectInputBudget(rc, request, tools); err != nil { return fail(err) } @@ -163,3 +213,12 @@ func checkProjectInputBudget(rc any, messages []agentllm.Message, tools []agentl } return nil } + +func projectScopeKey(m extension.ProjectContextManifest) string { + selected, _ := json.Marshal(struct { + Included []extension.ProjectContextManifestEntry + Offered []extension.ProjectEvidenceRef + Missing []string + }{m.Included, m.OfferedReferences, m.Missing}) + return fmt.Sprintf("%s\x00%s\x00%s\x00%s\x00%s\x00%s", m.ProjectID, m.Version, m.Watermark, m.AuthorizationEpoch, m.PublicationEpoch, selected) +} diff --git a/internal/runtime/workflow_compiled_test.go b/internal/runtime/workflow_compiled_test.go index 70faa07..6308895 100644 --- a/internal/runtime/workflow_compiled_test.go +++ b/internal/runtime/workflow_compiled_test.go @@ -302,7 +302,7 @@ func (c *projectJudgeCtx) Run() core.Run { return core.Run{ID: "run-judge", Owner: "alice", ProjectID: "project-1"} } func (c *projectJudgeCtx) ProjectContext(context.Context, string, int) (extension.ProjectContextBundle, bool, error) { - return extension.ProjectContextBundle{Status: extension.ProjectContextReady, ProjectID: "project-1", Items: []extension.ProjectContextItem{{SourceID: "judge-source", Text: "current project rule", Required: true}}}, true, nil + return extension.ProjectContextBundle{Status: extension.ProjectContextReady, ProjectID: "project-1", AuthorizationEpoch: "duty-1", PublicationEpoch: "pub-1", Items: []extension.ProjectContextItem{{SourceID: "judge-source", Text: "current project rule", Required: true}}}, true, nil } func (c *projectJudgeCtx) SaveContextManifest(_ context.Context, m extension.ProjectContextManifest) error { c.manifests = append(c.manifests, m) @@ -310,7 +310,7 @@ func (c *projectJudgeCtx) SaveContextManifest(_ context.Context, m extension.Pro } func TestWorkflowJudgeRequestUsesProjectContext(t *testing.T) { - var sent string + var sent []string srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { var body struct { Messages []struct { @@ -318,21 +318,30 @@ func TestWorkflowJudgeRequestUsesProjectContext(t *testing.T) { } `json:"messages"` } _ = json.NewDecoder(r.Body).Decode(&body) + var input string for _, m := range body.Messages { - sent += m.Content + input += m.Content } + sent = append(sent, input) w.Header().Set("Content-Type", "text/event-stream") _, _ = io.WriteString(w, "data: {\"choices\":[{\"delta\":{\"content\":\"ok\"}}]}\n\n") _, _ = io.WriteString(w, "data: [DONE]\n\n") })) defer srv.Close() c := &projectJudgeCtx{wfFake: &wfFake{settings: core.EffectiveSettings{"models": map[string]any{"chat": map[string]any{"baseUrl": srv.URL, "apiKey": "test-key", "model": "test-model"}}}}} - for ev := range judgeStreamFor(c)(context.Background(), "test-model", []agentllm.Message{{Role: agentllm.RoleUser, Content: "judge"}}, nil, agentllm.StreamOptions{}) { + stream := judgeStreamFor(c) + for ev := range stream(context.Background(), "test-model", []agentllm.Message{{Role: agentllm.RoleUser, Content: "judge"}}, nil, agentllm.StreamOptions{}) { if ev.Kind == agentllm.KindError { t.Fatalf("judge stream failed: %+v", ev) } } - if !strings.Contains(sent, "current project rule") || len(c.manifests) != 2 || c.manifests[1].Delivery != "responded" { + second := []agentllm.Message{{Role: agentllm.RoleSystem, Content: "judge role"}, {Role: agentllm.RoleUser, Content: "judge"}, {Role: agentllm.RoleAssistant, Content: "previous authorized step"}, {Role: agentllm.RoleTool, Content: "NEW_AUTHORIZED_TOOL"}} + for ev := range stream(context.Background(), "test-model", second, nil, agentllm.StreamOptions{}) { + if ev.Kind == agentllm.KindError { + t.Fatalf("second judge stream failed: %+v", ev) + } + } + if len(sent) != 2 || !strings.Contains(sent[0], "current project rule") || !strings.Contains(sent[1], "NEW_AUTHORIZED_TOOL") || !strings.Contains(sent[1], "current project rule") || len(c.manifests) != 4 || c.manifests[3].Delivery != "responded" { t.Fatalf("workflow judge missed project context: input=%q manifests=%+v", sent, c.manifests) } } diff --git a/internal/store/context_manifest.go b/internal/store/context_manifest.go index 947b42f..5179f6b 100644 --- a/internal/store/context_manifest.go +++ b/internal/store/context_manifest.go @@ -1,25 +1,60 @@ package store import ( + "database/sql" "encoding/json" "errors" + "fmt" "agent-system-backend/pkg/extension" ) const contextManifestAction = "model.context_manifest" -// AppendContextManifest stores a content-free receipt before a model request. -// The caller must supply a previously verified run or conversation target. +// AppendContextManifest writes to the trusted receipt table, never to the +// plugin-writable generic audit log. A terminal state must follow a matching +// attempted state for the same owner, target, project and input hash. func (s *Store) AppendContextManifest(owner, targetID string, m extension.ProjectContextManifest) error { - if owner == "" || targetID == "" || m.ProjectID == "" || m.InputSHA256 == "" { - return errors.New("context manifest requires owner, target, project and input hash") + if owner == "" || targetID == "" || m.ProjectID == "" || m.InputSHA256 == "" || m.AttemptID == "" { + return errors.New("context manifest requires owner, target, project, attempt and input hash") + } + if m.Delivery != "attempted" && m.Delivery != "responded" && m.Delivery != "unknown" { + return errors.New("context manifest has invalid delivery state") } raw, err := json.Marshal(m) if err != nil { return err } - return s.AppendAuditEvent(AuditEvent{Owner: owner, Actor: owner, Action: contextManifestAction, Target: targetID, Evidence: string(raw)}) + tx, err := s.db.Begin() + if err != nil { + return err + } + defer tx.Rollback() + if m.Delivery != "attempted" { + var found int + err := tx.QueryRow(s.q(`SELECT 1 FROM context_manifests WHERE attempt_id=? AND delivery='attempted' AND owner=? AND target_id=? AND project_id=? AND input_sha256=?`), + m.AttemptID, owner, targetID, m.ProjectID, m.InputSHA256).Scan(&found) + if errors.Is(err, sql.ErrNoRows) { + return errors.New("context manifest terminal receipt lacks matching attempt") + } + if err != nil { + return err + } + var terminal int + err = tx.QueryRow(s.q(`SELECT 1 FROM context_manifests WHERE attempt_id=? AND delivery IN ('responded','unknown') LIMIT 1`), m.AttemptID).Scan(&terminal) + if err == nil { + return errors.New("context manifest attempt already terminal") + } + if !errors.Is(err, sql.ErrNoRows) { + return err + } + } + _, err = tx.Exec(s.q(`INSERT INTO context_manifests(id,attempt_id,owner,target_id,project_id,input_sha256,delivery,manifest_json,created_at) VALUES(?,?,?,?,?,?,?,?,?)`), + NewID("ctxm"), m.AttemptID, owner, targetID, m.ProjectID, m.InputSHA256, m.Delivery, string(raw), nowISO()) + if err != nil { + return fmt.Errorf("insert context manifest: %w", err) + } + return tx.Commit() } // ListContextManifests reads receipts only within the authenticated owner's @@ -29,7 +64,7 @@ func (s *Store) ListContextManifests(owner, targetID string) ([]extension.Projec if owner == "" || targetID == "" { return nil, errors.New("context manifest query requires owner and target") } - rows, err := s.db.Query(s.q(`SELECT evidence FROM audit_events WHERE owner=? AND action=? AND target=? ORDER BY id`), owner, contextManifestAction, targetID) + rows, err := s.db.Query(s.q(`SELECT manifest_json FROM context_manifests WHERE owner=? AND target_id=? ORDER BY created_at,id`), owner, targetID) if err != nil { return nil, err } diff --git a/internal/store/context_manifest_test.go b/internal/store/context_manifest_test.go new file mode 100644 index 0000000..7d9018d --- /dev/null +++ b/internal/store/context_manifest_test.go @@ -0,0 +1,38 @@ +package store + +import ( + "path/filepath" + "testing" + + "agent-system-backend/pkg/extension" +) + +func TestContextManifestTerminalRequiresMatchingAttempt(t *testing.T) { + st, err := OpenSQLite(filepath.Join(t.TempDir(), "receipt.db")) + if err != nil { + t.Fatal(err) + } + defer st.Close() + m := extension.ProjectContextManifest{ProjectID: "p1", AttemptID: "attempt-1", InputSHA256: "input-hash", Delivery: "responded"} + if err := st.AppendContextManifest("alice", "run-1", m); err == nil { + t.Fatal("terminal receipt without attempted send") + } + m.Delivery = "attempted" + if err := st.AppendContextManifest("alice", "run-1", m); err != nil { + t.Fatal(err) + } + if err := st.AppendContextManifest("alice", "run-1", m); err == nil { + t.Fatal("duplicate attempted receipt") + } + m.Delivery = "responded" + if err := st.AppendContextManifest("alice", "run-2", m); err == nil { + t.Fatal("attempt reused across target") + } + if err := st.AppendContextManifest("alice", "run-1", m); err != nil { + t.Fatal(err) + } + m.Delivery = "unknown" + if err := st.AppendContextManifest("alice", "run-1", m); err == nil { + t.Fatal("one attempt acquired two terminal states") + } +} diff --git a/internal/store/migrate.go b/internal/store/migrate.go index 56a087f..5f26e2c 100644 --- a/internal/store/migrate.go +++ b/internal/store/migrate.go @@ -855,6 +855,13 @@ func init() { }, down: map[string][]string{"sqlite": {`DROP TABLE IF EXISTS work_task_bindings`}, "postgres": {`DROP TABLE IF EXISTS work_task_bindings`}}, }, + migration{version: 32, name: "trusted_context_manifests", + up: map[string][]string{ + "sqlite": {`CREATE TABLE context_manifests(id TEXT PRIMARY KEY,attempt_id TEXT NOT NULL,owner TEXT NOT NULL,target_id TEXT NOT NULL,project_id TEXT NOT NULL,input_sha256 TEXT NOT NULL,delivery TEXT NOT NULL CHECK(delivery IN ('attempted','responded','unknown')),manifest_json TEXT NOT NULL,created_at TEXT NOT NULL,UNIQUE(attempt_id,delivery))`, `CREATE INDEX idx_context_manifests_owner_target ON context_manifests(owner,target_id,created_at)`, `CREATE UNIQUE INDEX uniq_context_manifest_terminal ON context_manifests(attempt_id) WHERE delivery IN ('responded','unknown')`}, + "postgres": {`CREATE TABLE context_manifests(id TEXT PRIMARY KEY,attempt_id TEXT NOT NULL,owner TEXT NOT NULL,target_id TEXT NOT NULL,project_id TEXT NOT NULL,input_sha256 TEXT NOT NULL,delivery TEXT NOT NULL CHECK(delivery IN ('attempted','responded','unknown')),manifest_json TEXT NOT NULL,created_at TEXT NOT NULL,UNIQUE(attempt_id,delivery))`, `CREATE INDEX idx_context_manifests_owner_target ON context_manifests(owner,target_id,created_at)`, `CREATE UNIQUE INDEX uniq_context_manifest_terminal ON context_manifests(attempt_id) WHERE delivery IN ('responded','unknown')`}, + }, + down: map[string][]string{"sqlite": {`DROP INDEX IF EXISTS uniq_context_manifest_terminal`, `DROP INDEX IF EXISTS idx_context_manifests_owner_target`, `DROP TABLE IF EXISTS context_manifests`}, "postgres": {`DROP INDEX IF EXISTS uniq_context_manifest_terminal`, `DROP INDEX IF EXISTS idx_context_manifests_owner_target`, `DROP TABLE IF EXISTS context_manifests`}}, + }, ) } diff --git a/internal/store/migrate_compat.go b/internal/store/migrate_compat.go index 07944d4..6458fd4 100644 --- a/internal/store/migrate_compat.go +++ b/internal/store/migrate_compat.go @@ -36,6 +36,7 @@ var migrationBreaksOldReaders = map[int]bool{ 29: false, // redeem_attempts:新表,旧读者不受影响 30: false, // project_event_publication_proofs:新表,旧读者不受影响 31: false, // work_task_bindings:新增绑定表,旧后台任务读面不受影响 + 32: false, // trusted_context_manifests:独立可信回执表,旧读者不受影响 } // SchemaTarget 是本二进制会迁移到的 schema 版本。 From 7a00b20cd4282c62aa4560085a804ea8c2a60349 Mon Sep 17 00:00:00 2001 From: weichen Date: Wed, 14 Oct 2026 04:55:14 +0000 Subject: [PATCH 07/15] Bind workflow judge inputs to trusted step attempts Why: Compiled workflow critics could resend a prior prompt output after document duty or publication changed across separate model guards. Evidence: RED cross-guard revocation and retry tests; go test ./... -count=1 and targeted go test -race passed locally before commit. Impact: Bind materialized prompt bytes to the engine step attempt and responded model receipt; recheck current project sources and epochs before judging, failing closed for unproven or ambiguous output. Refs: RDM two-trees stage5 W6; stage6 X1 public extension request; W6 independent review P1. --- cmd/storage-migrate/main.go | 4 +- internal/projectcontext/workflow_step.go | 16 +++ internal/runmgr/project_context.go | 107 ++++++++++++++++ internal/runmgr/project_context_test.go | 88 ++++++++++++++ internal/runtime/project_stream.go | 8 ++ internal/runtime/workflow_compiled.go | 32 +++++ internal/runtime/workflow_compiled_test.go | 65 +++++++++- internal/store/migrate.go | 7 ++ internal/store/migrate_compat.go | 1 + internal/store/workflow_step_source.go | 128 ++++++++++++++++++++ internal/store/workflow_step_source_test.go | 75 ++++++++++++ internal/wfcompile/host.go | 32 ++++- internal/wfcompile/project_backend.go | 37 ++++++ internal/wfcompile/project_sources.go | 107 ++++++++++++++++ internal/wfcompile/run.go | 21 +++- internal/wfcompile/wfcompile_test.go | 116 ++++++++++++++++++ pkg/extension/project_context.go | 33 ++--- 17 files changed, 852 insertions(+), 25 deletions(-) create mode 100644 internal/projectcontext/workflow_step.go create mode 100644 internal/store/workflow_step_source.go create mode 100644 internal/store/workflow_step_source_test.go create mode 100644 internal/wfcompile/project_backend.go create mode 100644 internal/wfcompile/project_sources.go diff --git a/cmd/storage-migrate/main.go b/cmd/storage-migrate/main.go index 49206c6..92de9f2 100644 --- a/cmd/storage-migrate/main.go +++ b/cmd/storage-migrate/main.go @@ -287,7 +287,7 @@ var copyOrder = []string{ "activity_cards", "project_event_shares", "command_audit", "prompt_modules", "sessions", "api_keys", "plugin_grants", "plugin_schema", "plugin_kv", "background_tasks", "work_task_bindings", - "memory_entries", "audit_events", "context_manifests", "pending_confirms", "llm_usage_daily", + "memory_entries", "audit_events", "context_manifests", "workflow_step_sources", "pending_confirms", "llm_usage_daily", "secret_keys", "settings_snapshots", "regen_pending", "context_entries", "user_commands", "pending_notices", "recovery_holds", "surfaces", "surface_revisions", "surface_actions", "plugin_activation", @@ -784,7 +784,7 @@ func advanceSequences(db *sql.DB, table string) error { // checkOwners:业务行引用的 owner 必须存在于 users(单租户默认 ”/'local' // 除外)。未知 owner 阻塞迁移(U4/R7:不静默带过未知归属)。 func checkOwners(db *sql.DB, src *sql.DB) error { - ownerTables := []string{"projects", "conversations", "runs", "artifacts", "knowledge", "hitl_cards", "profiles", "activity_cards", "project_event_shares", "command_audit", "work_task_bindings", "context_manifests"} + ownerTables := []string{"projects", "conversations", "runs", "artifacts", "knowledge", "hitl_cards", "profiles", "activity_cards", "project_event_shares", "command_audit", "work_task_bindings", "context_manifests", "workflow_step_sources"} for _, t := range ownerTables { if !sqliteHasTable(src, t) { continue diff --git a/internal/projectcontext/workflow_step.go b/internal/projectcontext/workflow_step.go new file mode 100644 index 0000000..048d808 --- /dev/null +++ b/internal/projectcontext/workflow_step.go @@ -0,0 +1,16 @@ +package projectcontext + +import "context" + +type workflowStepAttemptKey struct{} + +// WithWorkflowStepAttempt is used by the trusted squad executor adapter. The +// value comes from the engine's persisted step attempt, never from model text. +func WithWorkflowStepAttempt(ctx context.Context, attemptID string) context.Context { + return context.WithValue(ctx, workflowStepAttemptKey{}, attemptID) +} + +func WorkflowStepAttempt(ctx context.Context) string { + value, _ := ctx.Value(workflowStepAttemptKey{}).(string) + return value +} diff --git a/internal/runmgr/project_context.go b/internal/runmgr/project_context.go index 5236b45..072e671 100644 --- a/internal/runmgr/project_context.go +++ b/internal/runmgr/project_context.go @@ -6,6 +6,7 @@ import ( "errors" "fmt" "strings" + "time" agentllm "agent-native/runtime/llm" @@ -28,6 +29,30 @@ func (c *runCtx) ProjectContextEnabled() bool { return c.run.ProjectID != "" && c.mgr.projectContextResolver != nil } +// ValidateProjectCrossGuard links separate model streams in one persisted run. +// A new guard cannot treat a workflow/user prompt as fresh when earlier model +// work under the same run used another authorization or publication revision. +func (c *runCtx) ValidateProjectCrossGuard(ctx context.Context, current extension.ProjectContextBundle) error { + if !c.ProjectContextEnabled() { + return nil + } + if err := ctx.Err(); err != nil { + return err + } + prior, err := c.mgr.st.ListContextManifests(string(c.run.Owner), string(c.run.ID)) + if err != nil { + return err + } + for _, m := range prior { + if m.ProjectID != current.ProjectID || m.AuthorizationEpoch == "" || m.PublicationEpoch == "" || + current.AuthorizationEpoch == "" || current.PublicationEpoch == "" || + m.AuthorizationEpoch != current.AuthorizationEpoch || m.PublicationEpoch != current.PublicationEpoch || m.Watermark != current.Watermark { + return errors.New("prior project model work used a different authorization or publication revision") + } + } + return nil +} + func (c *runCtx) contextRequest() extension.ProjectContextRequest { c.projectContextMu.Lock() defer c.projectContextMu.Unlock() @@ -224,3 +249,85 @@ func (c *runCtx) SaveContextManifest(ctx context.Context, m extension.ProjectCon } return c.mgr.st.AppendContextManifest(string(run.Owner), string(run.ID), m) } + +// InvalidateProjectStepOutput prevents a retry from inheriting the prior +// attempt's binding, even when the new attempt produces identical bytes. +func (c *runCtx) InvalidateProjectStepOutput(ctx context.Context, stepID string) error { + if err := ctx.Err(); err != nil { + return err + } + return c.mgr.st.UnbindWorkflowStepSource(string(c.run.Owner), string(c.run.ID), stepID) +} + +// BindProjectStepOutput stores exact bytes and their responded model attempt +// after a prompt step has materialized. The step start timestamp fences out +// receipts from earlier steps; the workflow sink rejects concurrent ambiguity. +func (c *runCtx) BindProjectStepOutput(ctx context.Context, stepID, stepAttemptID, kind string, startedAt time.Time, output []byte) error { + if kind != "prompt" || !c.ProjectContextEnabled() { + return errors.New("project prompt step provenance unavailable") + } + bundle, active, err := c.ProjectContext(ctx, "", 12000) + if err != nil || !active { + return errors.New("project step current scope unavailable") + } + bound, err := c.mgr.st.BindWorkflowStepSource(string(c.run.Owner), string(c.run.ID), stepID, stepAttemptID, kind, startedAt, output) + if err != nil { + return err + } + if bound.ProjectID != string(bundle.ProjectID) || bound.AuthorizationEpoch != bundle.AuthorizationEpoch || + bound.PublicationEpoch != bundle.PublicationEpoch || bound.Watermark != bundle.Watermark { + return errors.New("project step source changed before binding") + } + return nil +} + +// VerifyProjectStepOutput is the last gate before a workflow critic receives +// output.md. It checks the persisted run+step association, current file bytes, +// the trusted responded request, and live domain authorization epochs. +func (c *runCtx) VerifyProjectStepOutput(ctx context.Context, stepID, stepAttemptID, kind string, output []byte) error { + if kind != "prompt" || !c.ProjectContextEnabled() { + return errors.New("project workflow output has no verifiable prompt source") + } + bound, err := c.mgr.st.WorkflowStepSource(string(c.run.Owner), string(c.run.ID), stepID) + if err != nil { + return errors.New("project workflow output has no trusted step binding") + } + if bound.ProjectID != string(c.run.ProjectID) || bound.StepAttemptID != stepAttemptID || bound.OutputSHA256 != sha256.Sum256(output) { + return errors.New("project workflow output bytes or project scope changed") + } + receipt, err := c.mgr.st.RespondedContextManifest(string(c.run.Owner), string(c.run.ID), bound.AttemptID) + if err != nil || receipt.ProjectID != c.run.ProjectID || receipt.WorkflowStepAttemptID != stepAttemptID || receipt.AuthorizationEpoch != bound.AuthorizationEpoch || + receipt.PublicationEpoch != bound.PublicationEpoch || receipt.Watermark != bound.Watermark || receipt.Version != bound.Version { + return errors.New("project workflow output lost its trusted model source") + } + current, active, err := c.ProjectContext(ctx, "", 12000) + if err != nil || !active || current.ProjectID != receipt.ProjectID || + current.AuthorizationEpoch != receipt.AuthorizationEpoch || current.PublicationEpoch != receipt.PublicationEpoch || current.Watermark != receipt.Watermark { + return errors.New("project workflow output authorization or publication changed") + } + if err := projectcontext.ValidateCurrent(receipt, current); err != nil { + return fmt.Errorf("project workflow output source changed: %w", err) + } + for _, used := range receipt.ReadEvidence { + if c.mgr.projectEvidenceReader == nil { + return errors.New("project workflow evidence reader unavailable") + } + matched := false + for _, ref := range current.Evidence { + if ref.ID != used.ReferenceID || ref.SourceID != used.SourceID || ref.Revision != used.Revision || ref.Hash != used.SourceHash { + continue + } + scope := c.contextRequest() + scope.BudgetChars = 8000 + now, err := c.mgr.projectEvidenceReader(ctx, extension.ProjectEvidenceRequest{Scope: scope, Reference: ref}) + if err == nil && now.Reference == ref && fmt.Sprintf("%x", sha256.Sum256([]byte(now.Text))) == used.InputSHA256 { + matched = true + } + break + } + if !matched { + return errors.New("project workflow evidence permission or content changed") + } + } + return nil +} diff --git a/internal/runmgr/project_context_test.go b/internal/runmgr/project_context_test.go index 3177358..c18aee1 100644 --- a/internal/runmgr/project_context_test.go +++ b/internal/runmgr/project_context_test.go @@ -4,6 +4,7 @@ import ( "context" "sync" "testing" + "time" agentllm "agent-native/runtime/llm" "agent-system-backend/internal/projectcontext" @@ -70,6 +71,93 @@ func TestProjectContextResumeReloadsCurrentVersionAndAuthorization(t *testing.T) } } +func TestProjectJudgeChecksPersistedSourceEpochAcrossGuards(t *testing.T) { + st := openRecoveryStore(t) + m := newRecoveryManager(t, st) + conv := recoveryConversation(t, st) + run := recoveryRun(t, st, conv, core.RunPending) + m.SetProjectWorkAuthorization(func(context.Context, string, core.ID) (bool, error) { return true, nil }) + epoch := "duty-1" + publication := "pub-1" + items := []extension.ProjectContextItem{{SourceID: "restricted-doc", Revision: "r1", Hash: "h1", Text: "OLD_RESTRICTED_BODY", Required: true}} + m.SetProjectContextResolver(func(_ context.Context, req extension.ProjectContextRequest) (extension.ProjectContextBundle, error) { + return extension.ProjectContextBundle{Status: extension.ProjectContextReady, ProjectID: req.ProjectID, AuthorizationEpoch: epoch, PublicationEpoch: publication, Watermark: "commit-1", Items: items}, nil + }) + rc := &runCtx{mgr: m, run: run, ctx: context.Background()} + started := time.Now().Add(-time.Second) + bundle, _, err := rc.ProjectContext(context.Background(), "write output", 900) + if err != nil { + t.Fatal(err) + } + _, manifest, err := projectcontext.Assemble(bundle, 900) + if err != nil { + t.Fatal(err) + } + manifest.InputSHA256, manifest.AttemptID, manifest.Delivery = "input-hash", "step-attempt", "attempted" + manifest.WorkflowStepAttemptID = "engine-run/draft/1" + if err := rc.SaveContextManifest(context.Background(), manifest); err != nil { + t.Fatal(err) + } + manifest.Delivery = "responded" + if err := rc.SaveContextManifest(context.Background(), manifest); err != nil { + t.Fatal(err) + } + if err := rc.BindProjectStepOutput(context.Background(), "draft", "engine-run/draft/1", "prompt", started, []byte("OLD_RESTRICTED_BODY")); err != nil { + t.Fatal(err) + } + if err := rc.VerifyProjectStepOutput(context.Background(), "draft", "engine-run/draft/1", "prompt", []byte("OLD_RESTRICTED_BODY")); err != nil { + t.Fatalf("same epoch output rejected: %v", err) + } + if err := rc.ValidateProjectCrossGuard(context.Background(), bundle); err != nil { + t.Fatalf("same epoch cross-guard request rejected: %v", err) + } + if err := rc.VerifyProjectStepOutput(context.Background(), "draft", "engine-run/draft/1", "prompt", []byte("replaced bytes")); err == nil { + t.Fatal("replaced output accepted") + } + if err := rc.VerifyProjectStepOutput(context.Background(), "draft", "engine-run/draft/2", "prompt", []byte("OLD_RESTRICTED_BODY")); err == nil { + t.Fatal("second execution reused first attempt's identical output") + } + if err := rc.VerifyProjectStepOutput(context.Background(), "other", "engine-run/other/1", "prompt", []byte("OLD_RESTRICTED_BODY")); err == nil { + t.Fatal("another step reused output binding") + } + items = nil // provider withdraws the document while retaining project membership and epoch + if err := rc.VerifyProjectStepOutput(context.Background(), "draft", "engine-run/draft/1", "prompt", []byte("OLD_RESTRICTED_BODY")); err == nil { + t.Fatal("withdrawn document accepted with unchanged authorization epoch") + } + items = []extension.ProjectContextItem{{SourceID: "restricted-doc", Revision: "r1", Hash: "h1", Text: "OLD_RESTRICTED_BODY", Required: true}} + epoch = "duty-2" + current, _, err := rc.ProjectContext(context.Background(), "judge", 900) + if err != nil { + t.Fatal(err) + } + if err := rc.ValidateProjectCrossGuard(context.Background(), current); err == nil { + t.Fatal("cross-guard request ignored persisted epoch change") + } + if err := rc.VerifyProjectStepOutput(context.Background(), "draft", "engine-run/draft/1", "prompt", []byte("OLD_RESTRICTED_BODY")); err == nil { + t.Fatal("revoked source epoch accepted for workflow judge") + } + reloaded, err := st.GetRunByID(string(run.ID)) + if err != nil { + t.Fatal(err) + } + resumed := &runCtx{mgr: m, run: reloaded, ctx: context.Background()} + if err := resumed.VerifyProjectStepOutput(context.Background(), "draft", "engine-run/draft/1", "prompt", []byte("OLD_RESTRICTED_BODY")); err == nil { + t.Fatal("resumed judge ignored revocation") + } + epoch, publication = "duty-1", "pub-2" + if err := resumed.VerifyProjectStepOutput(context.Background(), "draft", "engine-run/draft/1", "prompt", []byte("OLD_RESTRICTED_BODY")); err == nil { + t.Fatal("resumed judge ignored publication epoch change") + } + other := recoveryRun(t, st, conv, core.RunPending) + noReceipt := &runCtx{mgr: m, run: other, ctx: context.Background()} + if err := noReceipt.BindProjectStepOutput(context.Background(), "draft", "engine-run/draft/1", "prompt", started, []byte("tool body")); err == nil { + t.Fatal("tool-only output without model provenance accepted") + } + if err := noReceipt.VerifyProjectStepOutput(context.Background(), "draft", "engine-run/draft/1", "prompt", []byte("tool body")); err == nil { + t.Fatal("another run reused output binding") + } +} + func TestContextManifestPersistsByOwnerAndRun(t *testing.T) { st := openRecoveryStore(t) m := newRecoveryManager(t, st) diff --git a/internal/runtime/project_stream.go b/internal/runtime/project_stream.go index d3648f7..aae73ae 100644 --- a/internal/runtime/project_stream.go +++ b/internal/runtime/project_stream.go @@ -67,6 +67,13 @@ func GuardProjectStream(rc any, goal string, stream agentllm.StreamFunc) agentll if !active { return stream(ctx, model, messages, tools, opts) } + if checker, ok := rc.(interface { + ValidateProjectCrossGuard(context.Context, extension.ProjectContextBundle) error + }); ok { + if err := checker.ValidateProjectCrossGuard(ctx, bundle); err != nil { + return fail(err) + } + } body, manifest, err := projectcontext.Assemble(bundle, budget) if err != nil { return fail(err) @@ -134,6 +141,7 @@ func GuardProjectStream(rc any, goal string, stream agentllm.StreamFunc) agentll return fail(err) } manifest.AttemptID, manifest.Delivery = attemptID, "attempted" + manifest.WorkflowStepAttemptID = projectcontext.WorkflowStepAttempt(ctx) var save func(context.Context, extension.ProjectContextManifest) error if sink, ok := rc.(interface { SaveContextManifest(context.Context, extension.ProjectContextManifest) error diff --git a/internal/runtime/workflow_compiled.go b/internal/runtime/workflow_compiled.go index 428cd90..ac56ec4 100644 --- a/internal/runtime/workflow_compiled.go +++ b/internal/runtime/workflow_compiled.go @@ -64,6 +64,8 @@ func (w CompiledWorkflowRunner) runLaunch(rc RunCtx, launch workflow.Launch, opt } if deps.JudgeStream == nil { deps.JudgeStream = judgeStreamFor(rc) + } else { + deps.JudgeStream = GuardProjectStream(rc, "", deps.JudgeStream) } return wfcompile.RunCompiled(workflowHost{rc: rc}, deps, launch, workflow.Vars{ Input: currentUserText(rc), @@ -169,3 +171,33 @@ func (h workflowHost) ConversationSandbox() string { } return "" } + +func (h workflowHost) InvalidateProjectStepOutput(ctx context.Context, stepID string) error { + invalidate, ok := h.rc.(interface { + InvalidateProjectStepOutput(context.Context, string) error + }) + if !ok { + return fmt.Errorf("project workflow step invalidation unavailable") + } + return invalidate.InvalidateProjectStepOutput(ctx, stepID) +} + +func (h workflowHost) BindProjectStepOutput(ctx context.Context, stepID, stepAttemptID, kind string, startedAt time.Time, output []byte) error { + binder, ok := h.rc.(interface { + BindProjectStepOutput(context.Context, string, string, string, time.Time, []byte) error + }) + if !ok { + return fmt.Errorf("project workflow step binding unavailable") + } + return binder.BindProjectStepOutput(ctx, stepID, stepAttemptID, kind, startedAt, output) +} + +func (h workflowHost) VerifyProjectStepOutput(ctx context.Context, stepID, stepAttemptID, kind string, output []byte) error { + verifier, ok := h.rc.(interface { + VerifyProjectStepOutput(context.Context, string, string, string, []byte) error + }) + if !ok { + return fmt.Errorf("project workflow step verification unavailable") + } + return verifier.VerifyProjectStepOutput(ctx, stepID, stepAttemptID, kind, output) +} diff --git a/internal/runtime/workflow_compiled_test.go b/internal/runtime/workflow_compiled_test.go index 6308895..51b1301 100644 --- a/internal/runtime/workflow_compiled_test.go +++ b/internal/runtime/workflow_compiled_test.go @@ -11,6 +11,7 @@ import ( "testing" agentllm "agent-native/runtime/llm" + "agent-system-backend/internal/projectcontext" "agent-system-backend/internal/workflow" "agent-system-backend/pkg/extension" core "agent-system-core" @@ -296,18 +297,31 @@ func TestJudgeStreamUsesConfiguredModel(t *testing.T) { type projectJudgeCtx struct { *wfFake manifests []extension.ProjectContextManifest + authEpoch string } func (c *projectJudgeCtx) Run() core.Run { return core.Run{ID: "run-judge", Owner: "alice", ProjectID: "project-1"} } func (c *projectJudgeCtx) ProjectContext(context.Context, string, int) (extension.ProjectContextBundle, bool, error) { - return extension.ProjectContextBundle{Status: extension.ProjectContextReady, ProjectID: "project-1", AuthorizationEpoch: "duty-1", PublicationEpoch: "pub-1", Items: []extension.ProjectContextItem{{SourceID: "judge-source", Text: "current project rule", Required: true}}}, true, nil + epoch := c.authEpoch + if epoch == "" { + epoch = "duty-1" + } + return extension.ProjectContextBundle{Status: extension.ProjectContextReady, ProjectID: "project-1", AuthorizationEpoch: epoch, PublicationEpoch: "pub-1", Items: []extension.ProjectContextItem{{SourceID: "judge-source", Text: "current project rule", Required: true}}}, true, nil } func (c *projectJudgeCtx) SaveContextManifest(_ context.Context, m extension.ProjectContextManifest) error { c.manifests = append(c.manifests, m) return nil } +func (c *projectJudgeCtx) ValidateProjectCrossGuard(_ context.Context, current extension.ProjectContextBundle) error { + for _, m := range c.manifests { + if m.AuthorizationEpoch != current.AuthorizationEpoch || m.PublicationEpoch != current.PublicationEpoch || m.Watermark != current.Watermark { + return fmt.Errorf("prior workflow model source was revoked") + } + } + return nil +} func TestWorkflowJudgeRequestUsesProjectContext(t *testing.T) { var sent []string @@ -346,4 +360,53 @@ func TestWorkflowJudgeRequestUsesProjectContext(t *testing.T) { } } +func TestWorkflowModelReceiptCarriesEngineStepAttempt(t *testing.T) { + c := &projectJudgeCtx{wfFake: &wfFake{}, authEpoch: "duty-1"} + ctx := projectcontext.WithWorkflowStepAttempt(context.Background(), "engine-run/draft/2") + for ev := range GuardProjectStream(c, "draft", scriptStream(textTurns("done"), nil))(ctx, "model", []agentllm.Message{{Role: agentllm.RoleUser, Content: "write"}}, nil, agentllm.StreamOptions{}) { + if ev.Kind == agentllm.KindError { + t.Fatalf("step model failed: %+v", ev) + } + } + if len(c.manifests) != 2 || c.manifests[0].WorkflowStepAttemptID != "engine-run/draft/2" || c.manifests[1].WorkflowStepAttemptID != "engine-run/draft/2" || c.manifests[1].Delivery != "responded" { + t.Fatalf("engine attempt missing from trusted model receipts: %+v", c.manifests) + } +} + +func TestWorkflowJudgeBlocksOutputFromEarlierAuthorizationEpoch(t *testing.T) { + const old = "OLD_RESTRICTED_BODY" + seen := false + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + var body struct { + Messages []struct { + Content string `json:"content"` + } `json:"messages"` + } + _ = json.NewDecoder(r.Body).Decode(&body) + for _, m := range body.Messages { + if strings.Contains(m.Content, old) { + seen = true + } + } + w.Header().Set("Content-Type", "text/event-stream") + _, _ = io.WriteString(w, "data: [DONE]\n\n") + })) + defer srv.Close() + c := &projectJudgeCtx{wfFake: &wfFake{settings: core.EffectiveSettings{"models": map[string]any{"chat": map[string]any{"baseUrl": srv.URL, "apiKey": "test-key", "model": "test-model"}}}}, authEpoch: "duty-1"} + step := GuardProjectStream(c, "write", scriptStream(textTurns("step done"), nil)) + for range step(context.Background(), "test-model", []agentllm.Message{{Role: agentllm.RoleUser, Content: "write report"}}, nil, agentllm.StreamOptions{}) { + } + if len(c.manifests) != 2 || c.manifests[1].Delivery != "responded" { + t.Fatalf("step source receipt missing: %+v", c.manifests) + } + c.authEpoch = "duty-2" // member remains; document duty and publication changed before critic + blocked := false + for ev := range judgeStreamFor(c)(context.Background(), "test-model", []agentllm.Message{{Role: agentllm.RoleUser, Content: "Judge output.md:\n" + old}}, nil, agentllm.StreamOptions{}) { + blocked = blocked || ev.Kind == agentllm.KindError + } + if seen || !blocked { + t.Fatalf("old workflow output reached a separate judge guard: seen=%v blocked=%v", seen, blocked) + } +} + func (*wfFake) Surfaces() []core.SurfaceSnapshot { return nil } diff --git a/internal/store/migrate.go b/internal/store/migrate.go index 5f26e2c..f5ab653 100644 --- a/internal/store/migrate.go +++ b/internal/store/migrate.go @@ -862,6 +862,13 @@ func init() { }, down: map[string][]string{"sqlite": {`DROP INDEX IF EXISTS uniq_context_manifest_terminal`, `DROP INDEX IF EXISTS idx_context_manifests_owner_target`, `DROP TABLE IF EXISTS context_manifests`}, "postgres": {`DROP INDEX IF EXISTS uniq_context_manifest_terminal`, `DROP INDEX IF EXISTS idx_context_manifests_owner_target`, `DROP TABLE IF EXISTS context_manifests`}}, }, + migration{version: 33, name: "workflow_step_sources", + up: map[string][]string{ + "sqlite": {`CREATE TABLE workflow_step_sources(run_id TEXT NOT NULL,step_id TEXT NOT NULL,owner TEXT NOT NULL,project_id TEXT NOT NULL,step_attempt_id TEXT NOT NULL,output_sha256 TEXT NOT NULL,attempt_id TEXT NOT NULL,authorization_epoch TEXT NOT NULL,publication_epoch TEXT NOT NULL,watermark TEXT NOT NULL,version TEXT NOT NULL,created_at TEXT NOT NULL,PRIMARY KEY(run_id,step_id),FOREIGN KEY(run_id) REFERENCES runs(id) ON DELETE CASCADE)`}, + "postgres": {`CREATE TABLE workflow_step_sources(run_id TEXT NOT NULL,step_id TEXT NOT NULL,owner TEXT NOT NULL,project_id TEXT NOT NULL,step_attempt_id TEXT NOT NULL,output_sha256 TEXT NOT NULL,attempt_id TEXT NOT NULL,authorization_epoch TEXT NOT NULL,publication_epoch TEXT NOT NULL,watermark TEXT NOT NULL,version TEXT NOT NULL,created_at TEXT NOT NULL,PRIMARY KEY(run_id,step_id),FOREIGN KEY(run_id) REFERENCES runs(id) ON DELETE CASCADE)`}, + }, + down: map[string][]string{"sqlite": {`DROP TABLE IF EXISTS workflow_step_sources`}, "postgres": {`DROP TABLE IF EXISTS workflow_step_sources`}}, + }, ) } diff --git a/internal/store/migrate_compat.go b/internal/store/migrate_compat.go index 6458fd4..efb7cb2 100644 --- a/internal/store/migrate_compat.go +++ b/internal/store/migrate_compat.go @@ -37,6 +37,7 @@ var migrationBreaksOldReaders = map[int]bool{ 30: false, // project_event_publication_proofs:新表,旧读者不受影响 31: false, // work_task_bindings:新增绑定表,旧后台任务读面不受影响 32: false, // trusted_context_manifests:独立可信回执表,旧读者不受影响 + 33: false, // workflow_step_sources:新增逐步骤来源绑定表,旧读者不受影响 } // SchemaTarget 是本二进制会迁移到的 schema 版本。 diff --git a/internal/store/workflow_step_source.go b/internal/store/workflow_step_source.go new file mode 100644 index 0000000..3ff9efd --- /dev/null +++ b/internal/store/workflow_step_source.go @@ -0,0 +1,128 @@ +package store + +import ( + "crypto/sha256" + "database/sql" + "encoding/hex" + "encoding/json" + "errors" + "fmt" + "time" + + "agent-system-backend/pkg/extension" +) + +type WorkflowStepSource struct { + RunID, StepID, Owner, ProjectID, StepAttemptID string + OutputSHA256 [32]byte + AttemptID, AuthorizationEpoch, PublicationEpoch, Watermark, Version string +} + +// UnbindWorkflowStepSource removes an earlier attempt before the engine begins +// another execution of the same step. A failed/unknown retry cannot inherit a +// previous output's proof, even if the output bytes happen to be identical. +func (s *Store) UnbindWorkflowStepSource(owner, runID, stepID string) error { + if owner == "" || runID == "" || stepID == "" { + return errors.New("workflow step source scope is incomplete") + } + _, err := s.db.Exec(s.q(`DELETE FROM workflow_step_sources WHERE owner=? AND run_id=? AND step_id=?`), owner, runID, stepID) + return err +} + +// BindWorkflowStepSource associates a materialized prompt step with only the +// responded model receipts created during that step. The caller must have +// excluded parallel prompt execution, whose receipt attribution is ambiguous. +func (s *Store) BindWorkflowStepSource(owner, runID, stepID, stepAttemptID, kind string, startedAt time.Time, output []byte) (WorkflowStepSource, error) { + if kind != "prompt" || owner == "" || runID == "" || stepID == "" || stepAttemptID == "" || startedAt.IsZero() { + return WorkflowStepSource{}, errors.New("workflow step source requires a started prompt step") + } + run, err := s.GetRunByID(runID) + if err != nil || run.Owner != owner || run.ProjectID == "" { + return WorkflowStepSource{}, ErrNotFound + } + rows, err := s.db.Query(s.q(`SELECT created_at,manifest_json FROM context_manifests WHERE owner=? AND target_id=? AND delivery='responded'`), owner, runID) + if err != nil { + return WorkflowStepSource{}, err + } + defer rows.Close() + var latest extension.ProjectContextManifest + var latestAt time.Time + for rows.Next() { + var atRaw, raw string + if err := rows.Scan(&atRaw, &raw); err != nil { + return WorkflowStepSource{}, err + } + at, err := time.Parse(time.RFC3339Nano, atRaw) + if err != nil { + return WorkflowStepSource{}, err + } + if at.Before(startedAt) { + continue + } + var m extension.ProjectContextManifest + if err := json.Unmarshal([]byte(raw), &m); err != nil { + return WorkflowStepSource{}, err + } + if m.WorkflowStepAttemptID != stepAttemptID { + continue + } + if m.ProjectID != run.ProjectID || m.AuthorizationEpoch == "" || m.PublicationEpoch == "" || m.AttemptID == "" { + return WorkflowStepSource{}, errors.New("workflow step model receipt lacks trusted project epochs") + } + if !latestAt.IsZero() && (m.ProjectID != latest.ProjectID || m.AuthorizationEpoch != latest.AuthorizationEpoch || m.PublicationEpoch != latest.PublicationEpoch || m.Watermark != latest.Watermark) { + return WorkflowStepSource{}, errors.New("workflow step model receipts span project authorization revisions") + } + if latestAt.IsZero() || at.After(latestAt) { + latest, latestAt = m, at + } + } + if err := rows.Err(); err != nil { + return WorkflowStepSource{}, err + } + if latestAt.IsZero() { + return WorkflowStepSource{}, errors.New("workflow prompt output has no responded model receipt") + } + b := WorkflowStepSource{RunID: runID, StepID: stepID, Owner: owner, ProjectID: string(run.ProjectID), StepAttemptID: stepAttemptID, OutputSHA256: sha256.Sum256(output), + AttemptID: latest.AttemptID, AuthorizationEpoch: latest.AuthorizationEpoch, PublicationEpoch: latest.PublicationEpoch, Watermark: latest.Watermark, Version: latest.Version} + _, err = s.db.Exec(s.q(`INSERT INTO workflow_step_sources(run_id,step_id,owner,project_id,step_attempt_id,output_sha256,attempt_id,authorization_epoch,publication_epoch,watermark,version,created_at) VALUES(?,?,?,?,?,?,?,?,?,?,?,?) ON CONFLICT(run_id,step_id) DO UPDATE SET owner=excluded.owner,project_id=excluded.project_id,step_attempt_id=excluded.step_attempt_id,output_sha256=excluded.output_sha256,attempt_id=excluded.attempt_id,authorization_epoch=excluded.authorization_epoch,publication_epoch=excluded.publication_epoch,watermark=excluded.watermark,version=excluded.version,created_at=excluded.created_at`), + runID, stepID, owner, b.ProjectID, b.StepAttemptID, hex.EncodeToString(b.OutputSHA256[:]), b.AttemptID, b.AuthorizationEpoch, b.PublicationEpoch, b.Watermark, b.Version, nowISO()) + if err != nil { + return WorkflowStepSource{}, fmt.Errorf("bind workflow step output: %w", err) + } + return b, nil +} + +func (s *Store) WorkflowStepSource(owner, runID, stepID string) (WorkflowStepSource, error) { + var b WorkflowStepSource + var hash string + err := s.db.QueryRow(s.q(`SELECT run_id,step_id,owner,project_id,step_attempt_id,output_sha256,attempt_id,authorization_epoch,publication_epoch,watermark,version FROM workflow_step_sources WHERE owner=? AND run_id=? AND step_id=?`), + owner, runID, stepID).Scan(&b.RunID, &b.StepID, &b.Owner, &b.ProjectID, &b.StepAttemptID, &hash, &b.AttemptID, &b.AuthorizationEpoch, &b.PublicationEpoch, &b.Watermark, &b.Version) + if errors.Is(err, sql.ErrNoRows) { + return WorkflowStepSource{}, ErrNotFound + } + if err != nil { + return WorkflowStepSource{}, err + } + decoded, err := hex.DecodeString(hash) + if err != nil || len(decoded) != len(b.OutputSHA256) { + return WorkflowStepSource{}, errors.New("workflow step source has invalid output hash") + } + copy(b.OutputSHA256[:], decoded) + return b, nil +} + +func (s *Store) RespondedContextManifest(owner, runID, attemptID string) (extension.ProjectContextManifest, error) { + var raw string + err := s.db.QueryRow(s.q(`SELECT manifest_json FROM context_manifests WHERE owner=? AND target_id=? AND attempt_id=? AND delivery='responded'`), owner, runID, attemptID).Scan(&raw) + if errors.Is(err, sql.ErrNoRows) { + return extension.ProjectContextManifest{}, ErrNotFound + } + if err != nil { + return extension.ProjectContextManifest{}, err + } + var m extension.ProjectContextManifest + if err := json.Unmarshal([]byte(raw), &m); err != nil { + return extension.ProjectContextManifest{}, err + } + return m, nil +} diff --git a/internal/store/workflow_step_source_test.go b/internal/store/workflow_step_source_test.go new file mode 100644 index 0000000..2c68602 --- /dev/null +++ b/internal/store/workflow_step_source_test.go @@ -0,0 +1,75 @@ +package store + +import ( + "crypto/sha256" + "path/filepath" + "testing" + "time" + + "agent-system-backend/pkg/extension" + core "agent-system-core" +) + +func TestWorkflowStepSourceRequiresRespondedManifestAndExactRunStep(t *testing.T) { + st, err := OpenSQLite(filepath.Join(t.TempDir(), "step.db")) + if err != nil { + t.Fatal(err) + } + defer st.Close() + p, err := st.CreateProject("alice", "P") + if err != nil { + t.Fatal(err) + } + c, err := st.CreateConversation("alice", string(p.ID), "C", "") + if err != nil { + t.Fatal(err) + } + r := core.Run{Owner: "alice", ConversationID: c.ID, ProjectID: p.ID, Kind: core.RunConversation, Status: core.RunPending} + if err := st.CreateRun(&r); err != nil { + t.Fatal(err) + } + started := time.Now().Add(-time.Second) + m := extension.ProjectContextManifest{ProjectID: p.ID, AttemptID: "attempt-1", WorkflowStepAttemptID: "engine-run/draft/1", Delivery: "attempted", InputSHA256: "input", AuthorizationEpoch: "duty-1", PublicationEpoch: "pub-1", Watermark: "w1"} + if err := st.AppendContextManifest("alice", string(r.ID), m); err != nil { + t.Fatal(err) + } + if _, err := st.BindWorkflowStepSource("alice", string(r.ID), "draft", "engine-run/draft/1", "prompt", started, []byte("old body")); err == nil { + t.Fatal("unanswered model attempt bound output") + } + unrelated := m + unrelated.AttemptID = "other-model-attempt" + unrelated.WorkflowStepAttemptID = "engine-run/other/1" + if err := st.AppendContextManifest("alice", string(r.ID), unrelated); err != nil { + t.Fatal(err) + } + unrelated.Delivery = "responded" + if err := st.AppendContextManifest("alice", string(r.ID), unrelated); err != nil { + t.Fatal(err) + } + if _, err := st.BindWorkflowStepSource("alice", string(r.ID), "draft", "engine-run/draft/1", "prompt", started, []byte("old body")); err == nil { + t.Fatal("another step's responded attempt bound draft output") + } + m.Delivery = "responded" + if err := st.AppendContextManifest("alice", string(r.ID), m); err != nil { + t.Fatal(err) + } + bound, err := st.BindWorkflowStepSource("alice", string(r.ID), "draft", "engine-run/draft/1", "prompt", started, []byte("old body")) + if err != nil || bound.AttemptID != "attempt-1" || bound.StepAttemptID != "engine-run/draft/1" || bound.OutputSHA256 != sha256.Sum256([]byte("old body")) { + t.Fatalf("source binding missed actual output: %+v err=%v", bound, err) + } + if _, err := st.WorkflowStepSource("alice", string(r.ID), "other"); err == nil { + t.Fatal("another step reused output binding") + } + if _, err := st.WorkflowStepSource("bob", string(r.ID), "draft"); err == nil { + t.Fatal("another owner read output binding") + } + if _, err := st.BindWorkflowStepSource("alice", string(r.ID), "tool", "engine-run/tool/1", "tool", started, []byte("tool body")); err == nil { + t.Fatal("tool output bound as model-sourced material") + } + if err := st.UnbindWorkflowStepSource("alice", string(r.ID), "draft"); err != nil { + t.Fatal(err) + } + if _, err := st.WorkflowStepSource("alice", string(r.ID), "draft"); err == nil { + t.Fatal("retry retained an earlier source binding") + } +} diff --git a/internal/wfcompile/host.go b/internal/wfcompile/host.go index 67434b6..b0ca13a 100644 --- a/internal/wfcompile/host.go +++ b/internal/wfcompile/host.go @@ -36,6 +36,15 @@ type Host interface { Tools() []core.ToolDesc } +// ProjectStepProvenance is implemented by the trusted Backend run host. Prompt +// output is bound after materialization; a critic must verify the exact bytes +// and current project authorization before it includes output in a model call. +type ProjectStepProvenance interface { + InvalidateProjectStepOutput(context.Context, string) error + BindProjectStepOutput(context.Context, string, string, string, time.Time, []byte) error + VerifyProjectStepOutput(context.Context, string, string, string, []byte) error +} + // Spawner 可选子代理派发能力(脚本回调 agent.spawn 用)。 type Spawner interface { SpawnAgent(agentID, name, task, hint string) (core.AgentResultPayload, error) @@ -74,10 +83,11 @@ const ( // hostRunner 宿主 intent 步执行器(squad.IntentRunner 实现)。 type hostRunner struct { - rc Host - deps Deps - scripts map[string]string - subOf map[string]string + rc Host + deps Deps + scripts map[string]string + subOf map[string]string + stepKinds map[string]string mu sync.Mutex verdicts map[string]string // verdict 字段 → 最近一次判定 @@ -421,6 +431,20 @@ func (r *hostRunner) doCritic(ctx context.Context, step squad.IntentStep) (strin target := step.Slots["target_step"] field := step.Slots["verdict_field"] output := r.readStepOutput(step.WorkDir, target) + if r.rc.Run().ProjectID != "" { + guard, ok := r.rc.(ProjectStepProvenance) + if !ok { + return "", nil, fmt.Errorf("项目工作流产物缺少可信来源校验") + } + wsDir := filepath.Dir(filepath.Dir(step.WorkDir)) + stepAttemptID, err := completedStepAttempt(wsDir, target) + if err != nil { + return "", nil, fmt.Errorf("项目工作流步骤 %s 状态不可核验: %w", target, err) + } + if err := guard.VerifyProjectStepOutput(ctx, target, stepAttemptID, r.stepKinds[target], []byte(output)); err != nil { + return "", nil, fmt.Errorf("项目工作流步骤 %s 产物依据不可用: %w", target, err) + } + } verdict := r.judge(ctx, step.Slots["goal"], step.Slots["criteria"], output) fields := map[string]string{} if verdict.Passed { diff --git a/internal/wfcompile/project_backend.go b/internal/wfcompile/project_backend.go new file mode 100644 index 0000000..47e2e1c --- /dev/null +++ b/internal/wfcompile/project_backend.go @@ -0,0 +1,37 @@ +package wfcompile + +import ( + "context" + + "agent-squad/squad" + "agent-system-backend/internal/projectcontext" +) + +// projectStepBackends carries the engine-generated execution ID through the +// executor's request context into each model manifest. This associates a +// responded model request with exactly one workflow step attempt. +func projectStepBackends(base *squad.BackendResolver) *squad.BackendResolver { + wrapped := squad.NewBackendResolver() + for _, name := range base.ListBackends() { + backendName := name + wrapped.Register(backendName, func(role squad.RoleSpec) (squad.Executor, error) { + backend, _, err := base.Resolve(role, backendName) + if err != nil { + return nil, err + } + return &projectStepExecutor{next: backend}, nil + }) + } + return wrapped +} + +type projectStepExecutor struct{ next squad.Executor } + +func (e *projectStepExecutor) Name() string { return e.next.Name() } + +func (e *projectStepExecutor) Execute(ctx context.Context, req squad.ExecRequest) (*squad.ExecResult, error) { + if req.ExecutionID == "" { + return &squad.ExecResult{Success: false, ErrorReason: "project workflow step attempt unavailable"}, nil + } + return e.next.Execute(projectcontext.WithWorkflowStepAttempt(ctx, req.ExecutionID), req) +} diff --git a/internal/wfcompile/project_sources.go b/internal/wfcompile/project_sources.go new file mode 100644 index 0000000..0c971ab --- /dev/null +++ b/internal/wfcompile/project_sources.go @@ -0,0 +1,107 @@ +package wfcompile + +import ( + "context" + "errors" + "os" + "path/filepath" + "sync" + "time" + + "agent-squad/squad" +) + +// completedStepAttempt comes from the squad engine's persisted state, not a +// model response or plugin argument. It identifies the exact execution that +// produced output.md, including after a workflow resumes. +func completedStepAttempt(wsDir, stepID string) (string, error) { + state, err := (squad.FileStore{}).LoadRun(wsDir) + if err != nil { + return "", err + } + for _, step := range state.Plan.Workflow.Steps { + if step.ID == stepID && step.Status == "completed" && step.AttemptID != "" { + return step.AttemptID, nil + } + } + return "", errors.New("completed step attempt unavailable") +} + +// projectOutputSink binds only a completed prompt step's materialized bytes. +// Parallel prompt executions have no unambiguous request-to-step attribution +// in the current backend contract, so they receive no binding and the critic +// fails closed. A recovered completion without its start event likewise needs +// an existing persisted binding to pass its critic. +type projectOutputSink struct { + rc ProjectStepProvenance + ctx context.Context + root string + subOf map[string]string + kinds map[string]string + mu sync.Mutex + started map[string]time.Time + ambiguous map[string]bool +} + +func newProjectOutputSink(rc Host, root string, subOf, kinds map[string]string) squad.ProgressSink { + binder := rc.(ProjectStepProvenance) + return &projectOutputSink{rc: binder, ctx: rc.Context(), root: root, subOf: subOf, kinds: kinds, + started: map[string]time.Time{}, ambiguous: map[string]bool{}} +} + +func (s *projectOutputSink) Emit(ev squad.Event) { + if ev.StepID == "" { + return + } + s.mu.Lock() + switch ev.Type { + case squad.EvStepStarted: + if s.kinds[ev.StepID] == "prompt" { + if err := s.rc.InvalidateProjectStepOutput(s.ctx, ev.StepID); err != nil { + s.ambiguous[ev.StepID] = true + } + } + if len(s.started) != 0 { + if s.kinds[ev.StepID] == "prompt" { + s.ambiguous[ev.StepID] = true + } + for id := range s.started { + if s.kinds[id] == "prompt" { + s.ambiguous[id] = true + } + } + } + s.started[ev.StepID] = ev.At + s.mu.Unlock() + return + case squad.EvStepCompleted: + startedAt, started := s.started[ev.StepID] + ambiguous := s.ambiguous[ev.StepID] + delete(s.started, ev.StepID) + delete(s.ambiguous, ev.StepID) + s.mu.Unlock() + if s.kinds[ev.StepID] != "prompt" || !started || startedAt.IsZero() || ambiguous { + return + } + sub := s.subOf[ev.StepID] + if sub == "" { + return + } + data, err := os.ReadFile(filepath.Join(s.root, "runs", ev.RunID, filepath.FromSlash(sub), "output.md")) + if err == nil { + wsDir := filepath.Join(s.root, "runs", ev.RunID) + attemptID, attemptErr := completedStepAttempt(wsDir, ev.StepID) + if attemptErr == nil { + _ = s.rc.BindProjectStepOutput(s.ctx, ev.StepID, attemptID, "prompt", startedAt, data) + } + } + return + case squad.EvStepFailed: + delete(s.started, ev.StepID) + delete(s.ambiguous, ev.StepID) + s.mu.Unlock() + return + default: + s.mu.Unlock() + } +} diff --git a/internal/wfcompile/run.go b/internal/wfcompile/run.go index 661c5bf..8799e13 100644 --- a/internal/wfcompile/run.go +++ b/internal/wfcompile/run.go @@ -76,6 +76,15 @@ func RunCompiled(rc Host, deps Deps, launch workflow.Launch, vars workflow.Vars, if err != nil { return Outcome{}, err } + stepKinds := make(map[string]string, len(def.Steps)) + for _, step := range def.Steps { + stepKinds[step.ID] = step.Kind + } + if rc.Run().ProjectID != "" { + if _, ok := rc.(ProjectStepProvenance); !ok { + return Outcome{}, fmt.Errorf("项目工作流缺少可信步骤产物绑定能力") + } + } registry := squad.NewRoleRegistry() for _, role := range compiled.Plan.Squad.Agents { @@ -87,9 +96,17 @@ func RunCompiled(rc Host, deps Deps, launch workflow.Launch, vars workflow.Vars, if backends == nil { backends = squad.NewBackendResolver() } + if rc.Run().ProjectID != "" { + backends = projectStepBackends(backends) + } runner := &hostRunner{ rc: rc, deps: deps, scripts: launch.Scripts, subOf: compiled.SubOf, - verdicts: map[string]string{}, lastWhy: map[string]string{}, fails: map[string]int{}, + stepKinds: stepKinds, + verdicts: map[string]string{}, lastWhy: map[string]string{}, fails: map[string]int{}, + } + var progress squad.ProgressSink = squad.NopSink{} + if rc.Run().ProjectID != "" { + progress = newProjectOutputSink(rc, filepath.Join(deps.DataDir, "wf", string(rc.Run().ID)), compiled.SubOf, stepKinds) } engine, err := squad.NewEngine(squad.Options{ DataDir: filepath.Join(deps.DataDir, "wf", string(rc.Run().ID)), @@ -102,7 +119,7 @@ func RunCompiled(rc Host, deps Deps, launch workflow.Launch, vars workflow.Vars, Checker: passChecker{}, IntentCatalog: compiled.Catalog, IntentRunner: runner, - Sink: squad.NopSink{}, + Sink: progress, }) if err != nil { return Outcome{}, fmt.Errorf("workflow: 引擎装配: %w", err) diff --git a/internal/wfcompile/wfcompile_test.go b/internal/wfcompile/wfcompile_test.go index 6881c70..a9a57e2 100644 --- a/internal/wfcompile/wfcompile_test.go +++ b/internal/wfcompile/wfcompile_test.go @@ -2,15 +2,19 @@ package wfcompile import ( "context" + "crypto/sha256" "encoding/json" "fmt" "os" + "path/filepath" "strings" "sync" "testing" + "time" agentllm "agent-native/runtime/llm" "agent-squad/squad" + "agent-system-backend/internal/projectcontext" "agent-system-backend/internal/workflow" core "agent-system-core" ) @@ -452,6 +456,118 @@ func TestRunCompiledDemoBrief(t *testing.T) { } } +type projectSourceHost struct { + *fakeHost + boundID string + boundHash [32]byte + verified int + invalidated int +} + +type stepContextProbe struct{ got string } + +func (p *stepContextProbe) Name() string { return "probe" } +func (p *stepContextProbe) Execute(ctx context.Context, _ squad.ExecRequest) (*squad.ExecResult, error) { + p.got = projectcontext.WorkflowStepAttempt(ctx) + return &squad.ExecResult{Success: true}, nil +} + +func TestProjectStepBackendCarriesEngineAttempt(t *testing.T) { + probe := &stepContextProbe{} + base := squad.NewBackendResolver() + base.RegisterInstance("probe", probe) + backend, _, err := projectStepBackends(base).Resolve(squad.RoleSpec{Role: "writer", Backend: "probe"}, "") + if err != nil { + t.Fatal(err) + } + if _, err := backend.Execute(context.Background(), squad.ExecRequest{ExecutionID: "engine-run/draft/2"}); err != nil { + t.Fatal(err) + } + if probe.got != "engine-run/draft/2" { + t.Fatalf("model backend lost trusted step attempt: %q", probe.got) + } +} + +func (h *projectSourceHost) Run() core.Run { + return core.Run{ID: "run_project_wf", ProjectID: "p1", Owner: "alice"} +} +func (h *projectSourceHost) BindProjectStepOutput(_ context.Context, stepID, stepAttemptID, kind string, startedAt time.Time, output []byte) error { + if kind != "prompt" || stepAttemptID == "" || startedAt.IsZero() { + return fmt.Errorf("untrusted step binding: %s %s %s", stepID, kind, startedAt) + } + h.boundID, h.boundHash = stepID, sha256.Sum256(output) + return nil +} +func (h *projectSourceHost) VerifyProjectStepOutput(_ context.Context, stepID, stepAttemptID, kind string, output []byte) error { + h.verified++ + if stepID != h.boundID || stepAttemptID == "" || kind != "prompt" || sha256.Sum256(output) != h.boundHash { + return fmt.Errorf("step output provenance mismatch: bound=%q/%x got=%q/%x", h.boundID, h.boundHash, stepID, sha256.Sum256(output)) + } + return nil +} +func (h *projectSourceHost) InvalidateProjectStepOutput(_ context.Context, stepID string) error { + if stepID == h.boundID { + h.boundID = "" + h.boundHash = [32]byte{} + } + h.invalidated++ + return nil +} + +func TestProjectOutputSinkRejectsConcurrentAndRetriedAttribution(t *testing.T) { + h := &projectSourceHost{fakeHost: &fakeHost{}} + root := t.TempDir() + sub := "tasks/01_draft" + path := filepath.Join(root, "runs", "engine-run", sub, "output.md") + if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil { + t.Fatal(err) + } + if err := os.WriteFile(path, []byte("model body"), 0o600); err != nil { + t.Fatal(err) + } + sink := newProjectOutputSink(h, root, map[string]string{"draft": sub}, map[string]string{"draft": "prompt", "other": "tool"}) + started := time.Now().Add(-time.Second) + if err := h.BindProjectStepOutput(context.Background(), "draft", "engine-run/draft/1", "prompt", started, []byte("old attempt")); err != nil { + t.Fatal(err) + } + sink.Emit(squad.Event{Type: squad.EvStepStarted, RunID: "engine-run", StepID: "draft", At: started}) + if err := h.VerifyProjectStepOutput(context.Background(), "draft", "engine-run/draft/1", "prompt", []byte("old attempt")); err == nil { + t.Fatal("retry retained previous attempt's binding") + } + sink.Emit(squad.Event{Type: squad.EvStepStarted, RunID: "engine-run", StepID: "other", At: started.Add(time.Millisecond)}) + sink.Emit(squad.Event{Type: squad.EvStepCompleted, RunID: "engine-run", StepID: "draft", At: time.Now()}) + if h.boundID != "" || h.invalidated == 0 { + t.Fatalf("ambiguous retry rebound output: id=%q invalidated=%d", h.boundID, h.invalidated) + } +} + +func TestProjectPromptCriticBindsActualStepOutputBeforeJudge(t *testing.T) { + f := &projectSourceHost{fakeHost: &fakeHost{hitl: []core.HITLResponse{{ChoiceID: "short"}, {ChoiceID: "yes"}}}} + be := &seqBackend{texts: []string{"draft text\n核对标记:P4"}} + _, err := RunCompiled(f, Deps{DataDir: t.TempDir(), Backends: seqResolver(t, be), DefaultBackend: "seq", JudgeStream: verdictStream(verdictPass)}, + workflow.Launch{Def: parseDef(t, demoBriefJSON)}, workflow.Vars{Input: "write", Slots: map[string]string{"topic": "topic"}}, RunOptions{}) + if err != nil { + t.Fatal(err) + } + if f.boundID != "draft" || f.boundHash != sha256.Sum256([]byte("draft text\n核对标记:P4\n")) || f.verified == 0 { + t.Fatalf("project workflow output lacked trusted step binding: id=%q verified=%d", f.boundID, f.verified) + } +} + +func TestProjectToolCriticRejectsUnprovenOutputBeforeModel(t *testing.T) { + f := &projectSourceHost{fakeHost: &fakeHost{toolNames: []string{"time_now"}}} + def := core.WorkflowDef{ID: "tool-critic", Title: "Tool review", Steps: []core.WorkflowStep{{ID: "get", Title: "Read", Kind: "tool", Tool: "time_now", Critic: &core.StepCritic{Criteria: "check"}}}} + called := false + judge := func(ctx context.Context, model string, messages []agentllm.Message, tools []agentllm.ToolDecl, opts agentllm.StreamOptions) <-chan agentllm.Event { + called = true + return verdictStream(verdictPass)(ctx, model, messages, tools, opts) + } + _, err := RunCompiled(f, Deps{DataDir: t.TempDir(), JudgeStream: judge}, workflow.Launch{Def: def}, workflow.Vars{Input: "read"}, RunOptions{}) + if err == nil || called || !strings.Contains(err.Error(), "产物依据不可用") { + t.Fatalf("unproven tool output reached judge: err=%v called=%v", err, called) + } +} + // critic 预算耗尽仍不通过 → 工作流失败(P4 fail-closed)。 func TestRunCompiledCriticExhaustedFails(t *testing.T) { def := parseDef(t, demoBriefJSON) diff --git a/pkg/extension/project_context.go b/pkg/extension/project_context.go index ad85007..2010b0c 100644 --- a/pkg/extension/project_context.go +++ b/pkg/extension/project_context.go @@ -112,22 +112,23 @@ type ProjectContextManifestEntry struct { // ProjectContextManifest describes the text actually passed to the completion // client. Candidate references in a bundle are never listed as included input. type ProjectContextManifest struct { - ProjectID core.ID - TaskID string - Version string - Watermark string - AuthorizationEpoch string - PublicationEpoch string - AttemptID string - Delivery string // attempted | responded | unknown; never implies user-visible delivery - Model string - BudgetChars int - InputSHA256 string - Included []ProjectContextManifestEntry - Dropped []ProjectContextManifestEntry - OfferedReferences []ProjectEvidenceRef - ReadEvidence []ProjectContextManifestEntry - Missing []string + ProjectID core.ID + TaskID string + Version string + Watermark string + AuthorizationEpoch string + PublicationEpoch string + AttemptID string + WorkflowStepAttemptID string + Delivery string // attempted | responded | unknown; never implies user-visible delivery + Model string + BudgetChars int + InputSHA256 string + Included []ProjectContextManifestEntry + Dropped []ProjectContextManifestEntry + OfferedReferences []ProjectEvidenceRef + ReadEvidence []ProjectContextManifestEntry + Missing []string } type WorkCompletionResult struct { From 6ba53e43ede296e192bb8875d284ba5a9dc27acc Mon Sep 17 00:00:00 2001 From: weichen Date: Wed, 14 Oct 2026 05:08:35 +0000 Subject: [PATCH 08/15] Preserve workflow model Resolve scopes for source checks Why: A goal-sensitive project provider can return different authorized sources for a prompt step and its judge, causing a valid workflow to fail when the judge re-resolves with an empty goal. Evidence: RED goal-sensitive step test; full compiled workflow positive and withdrawal negative tests; go test ./... -count=1 and targeted race passed locally before commit. Impact: Persist each trusted model attempt selector outside the public manifest, enforce receipt state continuity, and reauthorize every contributing model turn with its original selector before judging. Refs: RDM two-trees stage5 W6; W6 independent review P2. --- internal/projectcontext/workflow_step.go | 15 ++ internal/runmgr/project_context.go | 92 +++++++----- internal/runmgr/project_context_test.go | 154 +++++++++++++++++++- internal/runtime/project_stream.go | 4 +- internal/store/context_manifest.go | 37 ++++- internal/store/context_manifest_test.go | 23 +++ internal/store/migrate.go | 10 ++ internal/store/migrate_compat.go | 1 + internal/store/workflow_step_source.go | 54 ++++++- internal/store/workflow_step_source_test.go | 21 ++- 10 files changed, 360 insertions(+), 51 deletions(-) diff --git a/internal/projectcontext/workflow_step.go b/internal/projectcontext/workflow_step.go index 048d808..186adbf 100644 --- a/internal/projectcontext/workflow_step.go +++ b/internal/projectcontext/workflow_step.go @@ -3,6 +3,9 @@ package projectcontext import "context" type workflowStepAttemptKey struct{} +type resolveScopeKey struct{} + +type resolveScope struct{ goal, targetID string } // WithWorkflowStepAttempt is used by the trusted squad executor adapter. The // value comes from the engine's persisted step attempt, never from model text. @@ -14,3 +17,15 @@ func WorkflowStepAttempt(ctx context.Context) string { value, _ := ctx.Value(workflowStepAttemptKey{}).(string) return value } + +// WithResolveScope carries the exact selector used for this model request to +// Backend's trusted receipt writer. It is saved separately from the public +// final-input manifest and survives a resumed workflow judge. +func WithResolveScope(ctx context.Context, goal, targetID string) context.Context { + return context.WithValue(ctx, resolveScopeKey{}, resolveScope{goal: goal, targetID: targetID}) +} + +func ResolveScope(ctx context.Context) (goal, targetID string, ok bool) { + scope, ok := ctx.Value(resolveScopeKey{}).(resolveScope) + return scope.goal, scope.targetID, ok +} diff --git a/internal/runmgr/project_context.go b/internal/runmgr/project_context.go index 072e671..763eb84 100644 --- a/internal/runmgr/project_context.go +++ b/internal/runmgr/project_context.go @@ -11,6 +11,7 @@ import ( agentllm "agent-native/runtime/llm" "agent-system-backend/internal/projectcontext" + "agent-system-backend/internal/store" "agent-system-backend/pkg/extension" core "agent-system-core" ) @@ -182,6 +183,10 @@ func (c *runCtx) SaveContextManifest(ctx context.Context, m extension.ProjectCon if run.Owner != c.run.Owner || run.ProjectID != c.run.ProjectID || run.ConversationID != c.run.ConversationID || m.ProjectID != run.ProjectID { return errors.New("context manifest run scope mismatch") } + resolveGoal, resolveTarget, scopeBound := projectcontext.ResolveScope(ctx) + if scopeBound && resolveTarget != "" { + return errors.New("run context does not support a model-selected Resolve target") + } // A response/unknown receipt records an already attempted transport. A // revocation after send must not erase that audit fact. if m.Delivery != "responded" && m.Delivery != "unknown" { @@ -191,6 +196,10 @@ func (c *runCtx) SaveContextManifest(ctx context.Context, m extension.ProjectCon } if c.mgr.projectContextResolver != nil && m.Delivery != "responded" && m.Delivery != "unknown" { resolvedReq := c.contextRequest() + if scopeBound { + resolvedReq = extension.ProjectContextRequest{Actor: string(run.Owner), ProjectID: run.ProjectID, + ConversationID: run.ConversationID, RunID: run.ID, Goal: resolveGoal, TargetID: resolveTarget, BudgetChars: m.BudgetChars} + } if resolvedReq.RunID != run.ID || resolvedReq.ProjectID != run.ProjectID { return errors.New("context manifest missing resolved run scope") } @@ -247,6 +256,10 @@ func (c *runCtx) SaveContextManifest(ctx context.Context, m extension.ProjectCon } } } + if scopeBound { + return c.mgr.st.AppendContextManifestWithScope(string(run.Owner), string(run.ID), m, + store.ContextManifestScope{Goal: resolveGoal, TargetID: resolveTarget, Bound: true}) + } return c.mgr.st.AppendContextManifest(string(run.Owner), string(run.ID), m) } @@ -266,17 +279,12 @@ func (c *runCtx) BindProjectStepOutput(ctx context.Context, stepID, stepAttemptI if kind != "prompt" || !c.ProjectContextEnabled() { return errors.New("project prompt step provenance unavailable") } - bundle, active, err := c.ProjectContext(ctx, "", 12000) - if err != nil || !active { - return errors.New("project step current scope unavailable") - } - bound, err := c.mgr.st.BindWorkflowStepSource(string(c.run.Owner), string(c.run.ID), stepID, stepAttemptID, kind, startedAt, output) + _, err := c.mgr.st.BindWorkflowStepSource(string(c.run.Owner), string(c.run.ID), stepID, stepAttemptID, kind, startedAt, output) if err != nil { return err } - if bound.ProjectID != string(bundle.ProjectID) || bound.AuthorizationEpoch != bundle.AuthorizationEpoch || - bound.PublicationEpoch != bundle.PublicationEpoch || bound.Watermark != bundle.Watermark { - return errors.New("project step source changed before binding") + if err := c.VerifyProjectStepOutput(ctx, stepID, stepAttemptID, kind, output); err != nil { + return errors.Join(err, c.mgr.st.UnbindWorkflowStepSource(string(c.run.Owner), string(c.run.ID), stepID)) } return nil } @@ -295,39 +303,53 @@ func (c *runCtx) VerifyProjectStepOutput(ctx context.Context, stepID, stepAttemp if bound.ProjectID != string(c.run.ProjectID) || bound.StepAttemptID != stepAttemptID || bound.OutputSHA256 != sha256.Sum256(output) { return errors.New("project workflow output bytes or project scope changed") } - receipt, err := c.mgr.st.RespondedContextManifest(string(c.run.Owner), string(c.run.ID), bound.AttemptID) - if err != nil || receipt.ProjectID != c.run.ProjectID || receipt.WorkflowStepAttemptID != stepAttemptID || receipt.AuthorizationEpoch != bound.AuthorizationEpoch || - receipt.PublicationEpoch != bound.PublicationEpoch || receipt.Watermark != bound.Watermark || receipt.Version != bound.Version { + receipts, err := c.mgr.st.RespondedWorkflowStepManifests(string(c.run.Owner), string(c.run.ID), stepAttemptID) + if err != nil { return errors.New("project workflow output lost its trusted model source") } - current, active, err := c.ProjectContext(ctx, "", 12000) - if err != nil || !active || current.ProjectID != receipt.ProjectID || - current.AuthorizationEpoch != receipt.AuthorizationEpoch || current.PublicationEpoch != receipt.PublicationEpoch || current.Watermark != receipt.Watermark { - return errors.New("project workflow output authorization or publication changed") - } - if err := projectcontext.ValidateCurrent(receipt, current); err != nil { - return fmt.Errorf("project workflow output source changed: %w", err) - } - for _, used := range receipt.ReadEvidence { - if c.mgr.projectEvidenceReader == nil { - return errors.New("project workflow evidence reader unavailable") + matchedBound := false + for _, source := range receipts { + receipt, selector := source.Manifest, source.Scope + if !selector.Bound || selector.TargetID != "" || receipt.ProjectID != c.run.ProjectID || + receipt.WorkflowStepAttemptID != stepAttemptID || receipt.AuthorizationEpoch != bound.AuthorizationEpoch || + receipt.PublicationEpoch != bound.PublicationEpoch || receipt.Watermark != bound.Watermark || receipt.Version != bound.Version { + return errors.New("project workflow output lost its trusted model source") } - matched := false - for _, ref := range current.Evidence { - if ref.ID != used.ReferenceID || ref.SourceID != used.SourceID || ref.Revision != used.Revision || ref.Hash != used.SourceHash { - continue + if receipt.AttemptID == bound.AttemptID { + matchedBound = true + } + current, active, err := c.ProjectContext(ctx, selector.Goal, receipt.BudgetChars) + if err != nil || !active || current.ProjectID != receipt.ProjectID || + current.AuthorizationEpoch != receipt.AuthorizationEpoch || current.PublicationEpoch != receipt.PublicationEpoch || current.Watermark != receipt.Watermark { + return errors.New("project workflow output authorization or publication changed") + } + if err := projectcontext.ValidateCurrent(receipt, current); err != nil { + return fmt.Errorf("project workflow output source changed: %w", err) + } + for _, used := range receipt.ReadEvidence { + if c.mgr.projectEvidenceReader == nil { + return errors.New("project workflow evidence reader unavailable") } - scope := c.contextRequest() - scope.BudgetChars = 8000 - now, err := c.mgr.projectEvidenceReader(ctx, extension.ProjectEvidenceRequest{Scope: scope, Reference: ref}) - if err == nil && now.Reference == ref && fmt.Sprintf("%x", sha256.Sum256([]byte(now.Text))) == used.InputSHA256 { - matched = true + matched := false + for _, ref := range current.Evidence { + if ref.ID != used.ReferenceID || ref.SourceID != used.SourceID || ref.Revision != used.Revision || ref.Hash != used.SourceHash { + continue + } + scope := extension.ProjectContextRequest{Actor: string(c.run.Owner), ProjectID: c.run.ProjectID, + ConversationID: c.run.ConversationID, RunID: c.run.ID, Goal: selector.Goal, TargetID: selector.TargetID, BudgetChars: 8000} + now, err := c.mgr.projectEvidenceReader(ctx, extension.ProjectEvidenceRequest{Scope: scope, Reference: ref}) + if err == nil && now.Reference == ref && fmt.Sprintf("%x", sha256.Sum256([]byte(now.Text))) == used.InputSHA256 { + matched = true + } + break + } + if !matched { + return errors.New("project workflow evidence permission or content changed") } - break - } - if !matched { - return errors.New("project workflow evidence permission or content changed") } } + if !matchedBound { + return errors.New("project workflow output binding lost its responded model attempt") + } return nil } diff --git a/internal/runmgr/project_context_test.go b/internal/runmgr/project_context_test.go index c18aee1..290802f 100644 --- a/internal/runmgr/project_context_test.go +++ b/internal/runmgr/project_context_test.go @@ -2,12 +2,19 @@ package runmgr import ( "context" + "fmt" + "os" + "strings" "sync" "testing" "time" agentllm "agent-native/runtime/llm" + "agent-squad/squad" "agent-system-backend/internal/projectcontext" + "agent-system-backend/internal/runtime" + "agent-system-backend/internal/wfcompile" + "agent-system-backend/internal/workflow" "agent-system-backend/pkg/extension" core "agent-system-core" ) @@ -95,11 +102,12 @@ func TestProjectJudgeChecksPersistedSourceEpochAcrossGuards(t *testing.T) { } manifest.InputSHA256, manifest.AttemptID, manifest.Delivery = "input-hash", "step-attempt", "attempted" manifest.WorkflowStepAttemptID = "engine-run/draft/1" - if err := rc.SaveContextManifest(context.Background(), manifest); err != nil { + sourceCtx := projectcontext.WithResolveScope(context.Background(), "write output", "") + if err := rc.SaveContextManifest(sourceCtx, manifest); err != nil { t.Fatal(err) } manifest.Delivery = "responded" - if err := rc.SaveContextManifest(context.Background(), manifest); err != nil { + if err := rc.SaveContextManifest(sourceCtx, manifest); err != nil { t.Fatal(err) } if err := rc.BindProjectStepOutput(context.Background(), "draft", "engine-run/draft/1", "prompt", started, []byte("OLD_RESTRICTED_BODY")); err != nil { @@ -158,6 +166,148 @@ func TestProjectJudgeChecksPersistedSourceEpochAcrossGuards(t *testing.T) { } } +func TestProjectJudgeRetainsGoalSensitiveSourceScope(t *testing.T) { + st := openRecoveryStore(t) + m := newRecoveryManager(t, st) + conv := recoveryConversation(t, st) + run := recoveryRun(t, st, conv, core.RunPending) + m.SetProjectWorkAuthorization(func(context.Context, string, core.ID) (bool, error) { return true, nil }) + available := true + var goals []string + m.SetProjectContextResolver(func(_ context.Context, req extension.ProjectContextRequest) (extension.ProjectContextBundle, error) { + goals = append(goals, req.Goal) + item := extension.ProjectContextItem{SourceID: "source-b", Revision: "r1", Hash: "hb", Text: "PUBLIC_B", Required: true} + if strings.Contains(req.Goal, "draft report") && available { + item = extension.ProjectContextItem{SourceID: "source-a", Revision: "r1", Hash: "ha", Text: "AUTHORIZED_A", Required: true} + } + return extension.ProjectContextBundle{Status: extension.ProjectContextReady, ProjectID: req.ProjectID, AuthorizationEpoch: "duty-1", PublicationEpoch: "pub-1", Watermark: "w1", Items: []extension.ProjectContextItem{item}}, nil + }) + rc := &runCtx{mgr: m, run: run, ctx: context.Background()} + const goal = "draft report from approved source" + bundle, _, err := rc.ProjectContext(context.Background(), goal, 900) + if err != nil { + t.Fatal(err) + } + _, receipt, err := projectcontext.Assemble(bundle, 900) + if err != nil { + t.Fatal(err) + } + receipt.InputSHA256, receipt.AttemptID, receipt.WorkflowStepAttemptID = "input-hash", "model-attempt", "engine-run/draft/1" + receipt.Delivery = "attempted" + sourceCtx := projectcontext.WithResolveScope(context.Background(), goal, "") + if err := rc.SaveContextManifest(sourceCtx, receipt); err != nil { + t.Fatal(err) + } + receipt.Delivery = "responded" + if err := rc.SaveContextManifest(sourceCtx, receipt); err != nil { + t.Fatal(err) + } + // The final model turn uses Source B; output may still carry Source A from + // the first turn. Revalidate both receipts at the critic boundary. + bundleB, _, err := rc.ProjectContext(context.Background(), "revise using B", 900) + if err != nil { + t.Fatal(err) + } + _, second, err := projectcontext.Assemble(bundleB, 900) + if err != nil { + t.Fatal(err) + } + second.InputSHA256, second.AttemptID, second.WorkflowStepAttemptID = "second-input-hash", "model-attempt-2", "engine-run/draft/1" + secondCtx := projectcontext.WithResolveScope(context.Background(), "revise using B", "") + second.Delivery = "attempted" + if err := rc.SaveContextManifest(secondCtx, second); err != nil { + t.Fatal(err) + } + second.Delivery = "responded" + if err := rc.SaveContextManifest(secondCtx, second); err != nil { + t.Fatal(err) + } + started := time.Now().Add(-time.Second) + if err := rc.BindProjectStepOutput(context.Background(), "draft", "engine-run/draft/1", "prompt", started, []byte("report A")); err != nil { + t.Fatalf("same-scope binding rejected: %v", err) + } + if err := rc.VerifyProjectStepOutput(context.Background(), "draft", "engine-run/draft/1", "prompt", []byte("report A")); err != nil { + t.Fatalf("goal-sensitive provider rejected authorized critic: %v; goals=%q", err, goals) + } + available = false + if err := rc.VerifyProjectStepOutput(context.Background(), "draft", "engine-run/draft/1", "prompt", []byte("report A")); err == nil { + t.Fatal("withdrawn source passed goal-sensitive revalidation") + } +} + +type goalSensitiveStepBackend struct { + stream agentllm.StreamFunc + sent string +} + +func (*goalSensitiveStepBackend) Name() string { return "goal-sensitive-test" } +func (b *goalSensitiveStepBackend) Execute(ctx context.Context, req squad.ExecRequest) (*squad.ExecResult, error) { + for ev := range b.stream(ctx, "test-model", []agentllm.Message{{Role: agentllm.RoleUser, Content: req.Prompt}}, nil, agentllm.StreamOptions{}) { + if ev.Kind == agentllm.KindError { + return &squad.ExecResult{Success: false, ErrorReason: ev.Err.Error()}, nil + } + } + b.sent = req.Prompt + if err := os.WriteFile(req.WorkDir+"/output.md", []byte("AUTHORIZED_A report\n"), 0o600); err != nil { + return nil, err + } + return &squad.ExecResult{Success: true, Patches: map[string][]byte{"output.md": []byte("AUTHORIZED_A report\n")}}, nil +} + +func TestCompiledWorkflowJudgeAcceptsGoalSensitiveProviderAndRejectsWithdrawal(t *testing.T) { + st := openRecoveryStore(t) + mgr := newRecoveryManager(t, st) + conv := recoveryConversation(t, st) + run := recoveryRun(t, st, conv, core.RunPending) + mgr.SetProjectWorkAuthorization(func(context.Context, string, core.ID) (bool, error) { return true, nil }) + available := true + var resolved []string + mgr.SetProjectContextResolver(func(_ context.Context, req extension.ProjectContextRequest) (extension.ProjectContextBundle, error) { + resolved = append(resolved, req.Goal) + item := extension.ProjectContextItem{SourceID: "source-b", Revision: "r1", Hash: "hb", Text: "PUBLIC_B", Required: true} + if strings.Contains(req.Goal, "draft report") && !strings.Contains(req.Goal, "你是验收员") && available { + item = extension.ProjectContextItem{SourceID: "source-a", Revision: "r1", Hash: "ha", Text: "AUTHORIZED_A", Required: true} + } + return extension.ProjectContextBundle{Status: extension.ProjectContextReady, ProjectID: req.ProjectID, AuthorizationEpoch: "duty-1", PublicationEpoch: "pub-1", Watermark: "w1", Items: []extension.ProjectContextItem{item}}, nil + }) + rc := &runCtx{mgr: mgr, run: run, ctx: context.Background()} + stepBase := func(_ context.Context, _ string, messages []agentllm.Message, _ []agentllm.ToolDecl, _ agentllm.StreamOptions) <-chan agentllm.Event { + ch := make(chan agentllm.Event, 1) + if !strings.Contains(messages[len(messages)-1].Content, "AUTHORIZED_A") { + ch <- agentllm.Event{Kind: agentllm.KindError, Err: fmt.Errorf("draft model did not receive source A")} + } else { + ch <- agentllm.Event{Kind: agentllm.KindMessageEnd, Message: agentllm.Message{Role: agentllm.RoleAssistant, Content: "drafted"}} + } + close(ch) + return ch + } + judgeCalls := 0 + judgeBase := func(_ context.Context, _ string, messages []agentllm.Message, _ []agentllm.ToolDecl, _ agentllm.StreamOptions) <-chan agentllm.Event { + judgeCalls++ + ch := make(chan agentllm.Event, 1) + ch <- agentllm.Event{Kind: agentllm.KindMessageEnd, Message: agentllm.Message{Role: agentllm.RoleAssistant, Content: `{"passed":true,"reason":"ok","nextAction":""}`}} + close(ch) + return ch + } + backend := &goalSensitiveStepBackend{stream: runtime.GuardProjectStream(rc, "", stepBase)} + backends := squad.NewBackendResolver() + backends.RegisterInstance(backend.Name(), backend) + def := core.WorkflowDef{ID: "goal-sensitive", Title: "Report", Steps: []core.WorkflowStep{{ID: "draft", Title: "Draft", Kind: "prompt", Agent: "writer", Prompt: "draft report using the approved source", Critic: &core.StepCritic{Criteria: "contains report"}}}} + _, err := wfcompile.RunCompiled(rc, wfcompile.Deps{DataDir: t.TempDir(), Backends: backends, DefaultBackend: backend.Name(), JudgeStream: runtime.GuardProjectStream(rc, "", judgeBase)}, + workflow.Launch{Def: def}, workflow.Vars{Input: "draft report"}, wfcompile.RunOptions{}) + if err != nil || judgeCalls != 1 || backend.sent == "" { + t.Fatalf("authorized goal-sensitive workflow failed: err=%v judgeCalls=%d sent=%q resolved=%q", err, judgeCalls, backend.sent, resolved) + } + bound, err := st.WorkflowStepSource(string(run.Owner), string(run.ID), "draft") + if err != nil { + t.Fatal(err) + } + available = false + if err := rc.VerifyProjectStepOutput(context.Background(), "draft", bound.StepAttemptID, "prompt", []byte("AUTHORIZED_A report\n")); err == nil { + t.Fatal("withdrawn source A passed resumed goal-sensitive judge") + } +} + func TestContextManifestPersistsByOwnerAndRun(t *testing.T) { st := openRecoveryStore(t) m := newRecoveryManager(t, st) diff --git a/internal/runtime/project_stream.go b/internal/runtime/project_stream.go index aae73ae..31fd97c 100644 --- a/internal/runtime/project_stream.go +++ b/internal/runtime/project_stream.go @@ -146,7 +146,9 @@ func GuardProjectStream(rc any, goal string, stream agentllm.StreamFunc) agentll if sink, ok := rc.(interface { SaveContextManifest(context.Context, extension.ProjectContextManifest) error }); ok { - save = sink.SaveContextManifest + save = func(saveCtx context.Context, receipt extension.ProjectContextManifest) error { + return sink.SaveContextManifest(projectcontext.WithResolveScope(saveCtx, requestGoal, ""), receipt) + } if err := save(ctx, manifest); err != nil { return fail(err) } diff --git a/internal/store/context_manifest.go b/internal/store/context_manifest.go index 5179f6b..779f7b2 100644 --- a/internal/store/context_manifest.go +++ b/internal/store/context_manifest.go @@ -11,10 +11,29 @@ import ( const contextManifestAction = "model.context_manifest" +// ContextManifestScope records the exact trusted Resolve selector separately +// from the public final-input manifest. Bound distinguishes an intentionally +// empty goal from a legacy receipt with no recoverable selector. +type ContextManifestScope struct { + Goal, TargetID string + Bound bool +} + // AppendContextManifest writes to the trusted receipt table, never to the // plugin-writable generic audit log. A terminal state must follow a matching // attempted state for the same owner, target, project and input hash. func (s *Store) AppendContextManifest(owner, targetID string, m extension.ProjectContextManifest) error { + return s.appendContextManifest(owner, targetID, m, ContextManifestScope{}) +} + +func (s *Store) AppendContextManifestWithScope(owner, targetID string, m extension.ProjectContextManifest, scope ContextManifestScope) error { + if !scope.Bound { + return errors.New("trusted context manifest resolve scope is not bound") + } + return s.appendContextManifest(owner, targetID, m, scope) +} + +func (s *Store) appendContextManifest(owner, targetID string, m extension.ProjectContextManifest, scope ContextManifestScope) error { if owner == "" || targetID == "" || m.ProjectID == "" || m.InputSHA256 == "" || m.AttemptID == "" { return errors.New("context manifest requires owner, target, project, attempt and input hash") } @@ -31,15 +50,19 @@ func (s *Store) AppendContextManifest(owner, targetID string, m extension.Projec } defer tx.Rollback() if m.Delivery != "attempted" { - var found int - err := tx.QueryRow(s.q(`SELECT 1 FROM context_manifests WHERE attempt_id=? AND delivery='attempted' AND owner=? AND target_id=? AND project_id=? AND input_sha256=?`), - m.AttemptID, owner, targetID, m.ProjectID, m.InputSHA256).Scan(&found) + var attemptedGoal, attemptedTarget string + var attemptedBound int + err := tx.QueryRow(s.q(`SELECT resolve_goal,resolve_target_id,scope_bound FROM context_manifests WHERE attempt_id=? AND delivery='attempted' AND owner=? AND target_id=? AND project_id=? AND input_sha256=?`), + m.AttemptID, owner, targetID, m.ProjectID, m.InputSHA256).Scan(&attemptedGoal, &attemptedTarget, &attemptedBound) if errors.Is(err, sql.ErrNoRows) { return errors.New("context manifest terminal receipt lacks matching attempt") } if err != nil { return err } + if attemptedGoal != scope.Goal || attemptedTarget != scope.TargetID || (attemptedBound != 0) != scope.Bound { + return errors.New("context manifest terminal resolve scope changed") + } var terminal int err = tx.QueryRow(s.q(`SELECT 1 FROM context_manifests WHERE attempt_id=? AND delivery IN ('responded','unknown') LIMIT 1`), m.AttemptID).Scan(&terminal) if err == nil { @@ -49,8 +72,12 @@ func (s *Store) AppendContextManifest(owner, targetID string, m extension.Projec return err } } - _, err = tx.Exec(s.q(`INSERT INTO context_manifests(id,attempt_id,owner,target_id,project_id,input_sha256,delivery,manifest_json,created_at) VALUES(?,?,?,?,?,?,?,?,?)`), - NewID("ctxm"), m.AttemptID, owner, targetID, m.ProjectID, m.InputSHA256, m.Delivery, string(raw), nowISO()) + bound := 0 + if scope.Bound { + bound = 1 + } + _, err = tx.Exec(s.q(`INSERT INTO context_manifests(id,attempt_id,owner,target_id,project_id,input_sha256,delivery,manifest_json,created_at,resolve_goal,resolve_target_id,scope_bound) VALUES(?,?,?,?,?,?,?,?,?,?,?,?)`), + NewID("ctxm"), m.AttemptID, owner, targetID, m.ProjectID, m.InputSHA256, m.Delivery, string(raw), nowISO(), scope.Goal, scope.TargetID, bound) if err != nil { return fmt.Errorf("insert context manifest: %w", err) } diff --git a/internal/store/context_manifest_test.go b/internal/store/context_manifest_test.go index 7d9018d..c4411b8 100644 --- a/internal/store/context_manifest_test.go +++ b/internal/store/context_manifest_test.go @@ -36,3 +36,26 @@ func TestContextManifestTerminalRequiresMatchingAttempt(t *testing.T) { t.Fatal("one attempt acquired two terminal states") } } + +func TestContextManifestTerminalCannotChangeTrustedResolveScope(t *testing.T) { + st, err := OpenSQLite(filepath.Join(t.TempDir(), "scope.db")) + if err != nil { + t.Fatal(err) + } + defer st.Close() + m := extension.ProjectContextManifest{ProjectID: "p1", AttemptID: "model-1", WorkflowStepAttemptID: "engine/draft/1", InputSHA256: "input-hash", Delivery: "attempted"} + scope := ContextManifestScope{Goal: "draft from A", Bound: true} + if err := st.AppendContextManifestWithScope("alice", "run-1", m, scope); err != nil { + t.Fatal(err) + } + m.Delivery = "responded" + if err := st.AppendContextManifestWithScope("alice", "run-1", m, ContextManifestScope{Goal: "draft from B", Bound: true}); err == nil { + t.Fatal("terminal receipt changed its trusted Resolve goal") + } + if err := st.AppendContextManifest("alice", "run-1", m); err == nil { + t.Fatal("terminal receipt dropped its trusted Resolve scope") + } + if err := st.AppendContextManifestWithScope("alice", "run-1", m, scope); err != nil { + t.Fatal(err) + } +} diff --git a/internal/store/migrate.go b/internal/store/migrate.go index f5ab653..bf6fab9 100644 --- a/internal/store/migrate.go +++ b/internal/store/migrate.go @@ -869,6 +869,16 @@ func init() { }, down: map[string][]string{"sqlite": {`DROP TABLE IF EXISTS workflow_step_sources`}, "postgres": {`DROP TABLE IF EXISTS workflow_step_sources`}}, }, + migration{version: 34, name: "context_manifest_resolve_scope", + up: map[string][]string{ + "sqlite": {`ALTER TABLE context_manifests ADD COLUMN resolve_goal TEXT NOT NULL DEFAULT ''`, `ALTER TABLE context_manifests ADD COLUMN resolve_target_id TEXT NOT NULL DEFAULT ''`, `ALTER TABLE context_manifests ADD COLUMN scope_bound INTEGER NOT NULL DEFAULT 0`}, + "postgres": {`ALTER TABLE context_manifests ADD COLUMN resolve_goal TEXT NOT NULL DEFAULT ''`, `ALTER TABLE context_manifests ADD COLUMN resolve_target_id TEXT NOT NULL DEFAULT ''`, `ALTER TABLE context_manifests ADD COLUMN scope_bound INTEGER NOT NULL DEFAULT 0`}, + }, + down: map[string][]string{ + "sqlite": {`ALTER TABLE context_manifests DROP COLUMN scope_bound`, `ALTER TABLE context_manifests DROP COLUMN resolve_target_id`, `ALTER TABLE context_manifests DROP COLUMN resolve_goal`}, + "postgres": {`ALTER TABLE context_manifests DROP COLUMN scope_bound`, `ALTER TABLE context_manifests DROP COLUMN resolve_target_id`, `ALTER TABLE context_manifests DROP COLUMN resolve_goal`}, + }, + }, ) } diff --git a/internal/store/migrate_compat.go b/internal/store/migrate_compat.go index efb7cb2..7b17ce6 100644 --- a/internal/store/migrate_compat.go +++ b/internal/store/migrate_compat.go @@ -38,6 +38,7 @@ var migrationBreaksOldReaders = map[int]bool{ 31: false, // work_task_bindings:新增绑定表,旧后台任务读面不受影响 32: false, // trusted_context_manifests:独立可信回执表,旧读者不受影响 33: false, // workflow_step_sources:新增逐步骤来源绑定表,旧读者不受影响 + 34: false, // context_manifest_resolve_scope:可信回执增加原Resolve范围,旧读者不受影响 } // SchemaTarget 是本二进制会迁移到的 schema 版本。 diff --git a/internal/store/workflow_step_source.go b/internal/store/workflow_step_source.go index 3ff9efd..e5bbfad 100644 --- a/internal/store/workflow_step_source.go +++ b/internal/store/workflow_step_source.go @@ -40,7 +40,7 @@ func (s *Store) BindWorkflowStepSource(owner, runID, stepID, stepAttemptID, kind if err != nil || run.Owner != owner || run.ProjectID == "" { return WorkflowStepSource{}, ErrNotFound } - rows, err := s.db.Query(s.q(`SELECT created_at,manifest_json FROM context_manifests WHERE owner=? AND target_id=? AND delivery='responded'`), owner, runID) + rows, err := s.db.Query(s.q(`SELECT created_at,manifest_json,resolve_target_id,scope_bound FROM context_manifests WHERE owner=? AND target_id=? AND delivery='responded'`), owner, runID) if err != nil { return WorkflowStepSource{}, err } @@ -48,8 +48,9 @@ func (s *Store) BindWorkflowStepSource(owner, runID, stepID, stepAttemptID, kind var latest extension.ProjectContextManifest var latestAt time.Time for rows.Next() { - var atRaw, raw string - if err := rows.Scan(&atRaw, &raw); err != nil { + var atRaw, raw, resolveTarget string + var scopeBound int + if err := rows.Scan(&atRaw, &raw, &resolveTarget, &scopeBound); err != nil { return WorkflowStepSource{}, err } at, err := time.Parse(time.RFC3339Nano, atRaw) @@ -66,10 +67,13 @@ func (s *Store) BindWorkflowStepSource(owner, runID, stepID, stepAttemptID, kind if m.WorkflowStepAttemptID != stepAttemptID { continue } + if scopeBound != 1 || resolveTarget != "" { + return WorkflowStepSource{}, errors.New("workflow step model receipt lacks trusted original Resolve scope") + } if m.ProjectID != run.ProjectID || m.AuthorizationEpoch == "" || m.PublicationEpoch == "" || m.AttemptID == "" { return WorkflowStepSource{}, errors.New("workflow step model receipt lacks trusted project epochs") } - if !latestAt.IsZero() && (m.ProjectID != latest.ProjectID || m.AuthorizationEpoch != latest.AuthorizationEpoch || m.PublicationEpoch != latest.PublicationEpoch || m.Watermark != latest.Watermark) { + if !latestAt.IsZero() && (m.ProjectID != latest.ProjectID || m.AuthorizationEpoch != latest.AuthorizationEpoch || m.PublicationEpoch != latest.PublicationEpoch || m.Watermark != latest.Watermark || m.Version != latest.Version) { return WorkflowStepSource{}, errors.New("workflow step model receipts span project authorization revisions") } if latestAt.IsZero() || at.After(latestAt) { @@ -92,6 +96,48 @@ func (s *Store) BindWorkflowStepSource(owner, runID, stepID, stepAttemptID, kind return b, nil } +type ScopedContextManifest struct { + Manifest extension.ProjectContextManifest + Scope ContextManifestScope +} + +// RespondedWorkflowStepManifests returns every model response from this exact +// engine step attempt. A final output can depend on earlier model turns, so a +// critic must revalidate all of them, not just the last response. +func (s *Store) RespondedWorkflowStepManifests(owner, runID, stepAttemptID string) ([]ScopedContextManifest, error) { + if owner == "" || runID == "" || stepAttemptID == "" { + return nil, errors.New("workflow receipt scope is incomplete") + } + rows, err := s.db.Query(s.q(`SELECT manifest_json,resolve_goal,resolve_target_id,scope_bound FROM context_manifests WHERE owner=? AND target_id=? AND delivery='responded' ORDER BY created_at,id`), owner, runID) + if err != nil { + return nil, err + } + defer rows.Close() + var out []ScopedContextManifest + for rows.Next() { + var raw, goal, target string + var bound int + if err := rows.Scan(&raw, &goal, &target, &bound); err != nil { + return nil, err + } + var manifest extension.ProjectContextManifest + if err := json.Unmarshal([]byte(raw), &manifest); err != nil { + return nil, err + } + if manifest.WorkflowStepAttemptID != stepAttemptID { + continue + } + out = append(out, ScopedContextManifest{Manifest: manifest, Scope: ContextManifestScope{Goal: goal, TargetID: target, Bound: bound == 1}}) + } + if err := rows.Err(); err != nil { + return nil, err + } + if len(out) == 0 { + return nil, ErrNotFound + } + return out, nil +} + func (s *Store) WorkflowStepSource(owner, runID, stepID string) (WorkflowStepSource, error) { var b WorkflowStepSource var hash string diff --git a/internal/store/workflow_step_source_test.go b/internal/store/workflow_step_source_test.go index 2c68602..f58d9f7 100644 --- a/internal/store/workflow_step_source_test.go +++ b/internal/store/workflow_step_source_test.go @@ -30,7 +30,8 @@ func TestWorkflowStepSourceRequiresRespondedManifestAndExactRunStep(t *testing.T } started := time.Now().Add(-time.Second) m := extension.ProjectContextManifest{ProjectID: p.ID, AttemptID: "attempt-1", WorkflowStepAttemptID: "engine-run/draft/1", Delivery: "attempted", InputSHA256: "input", AuthorizationEpoch: "duty-1", PublicationEpoch: "pub-1", Watermark: "w1"} - if err := st.AppendContextManifest("alice", string(r.ID), m); err != nil { + scope := ContextManifestScope{Goal: "draft report", Bound: true} + if err := st.AppendContextManifestWithScope("alice", string(r.ID), m, scope); err != nil { t.Fatal(err) } if _, err := st.BindWorkflowStepSource("alice", string(r.ID), "draft", "engine-run/draft/1", "prompt", started, []byte("old body")); err == nil { @@ -39,18 +40,18 @@ func TestWorkflowStepSourceRequiresRespondedManifestAndExactRunStep(t *testing.T unrelated := m unrelated.AttemptID = "other-model-attempt" unrelated.WorkflowStepAttemptID = "engine-run/other/1" - if err := st.AppendContextManifest("alice", string(r.ID), unrelated); err != nil { + if err := st.AppendContextManifestWithScope("alice", string(r.ID), unrelated, scope); err != nil { t.Fatal(err) } unrelated.Delivery = "responded" - if err := st.AppendContextManifest("alice", string(r.ID), unrelated); err != nil { + if err := st.AppendContextManifestWithScope("alice", string(r.ID), unrelated, scope); err != nil { t.Fatal(err) } if _, err := st.BindWorkflowStepSource("alice", string(r.ID), "draft", "engine-run/draft/1", "prompt", started, []byte("old body")); err == nil { t.Fatal("another step's responded attempt bound draft output") } m.Delivery = "responded" - if err := st.AppendContextManifest("alice", string(r.ID), m); err != nil { + if err := st.AppendContextManifestWithScope("alice", string(r.ID), m, scope); err != nil { t.Fatal(err) } bound, err := st.BindWorkflowStepSource("alice", string(r.ID), "draft", "engine-run/draft/1", "prompt", started, []byte("old body")) @@ -66,6 +67,18 @@ func TestWorkflowStepSourceRequiresRespondedManifestAndExactRunStep(t *testing.T if _, err := st.BindWorkflowStepSource("alice", string(r.ID), "tool", "engine-run/tool/1", "tool", started, []byte("tool body")); err == nil { t.Fatal("tool output bound as model-sourced material") } + legacy := m + legacy.AttemptID, legacy.WorkflowStepAttemptID, legacy.Delivery = "legacy-model", "engine-run/legacy/1", "attempted" + if err := st.AppendContextManifest("alice", string(r.ID), legacy); err != nil { + t.Fatal(err) + } + legacy.Delivery = "responded" + if err := st.AppendContextManifest("alice", string(r.ID), legacy); err != nil { + t.Fatal(err) + } + if _, err := st.BindWorkflowStepSource("alice", string(r.ID), "legacy", "engine-run/legacy/1", "prompt", started, []byte("legacy body")); err == nil { + t.Fatal("model receipt without bound original Resolve scope became a workflow source") + } if err := st.UnbindWorkflowStepSource("alice", string(r.ID), "draft"); err != nil { t.Fatal(err) } From ecb86f4309193bf40b0c3b2846de68eae3a2dcdb Mon Sep 17 00:00:00 2001 From: weichen Date: Wed, 14 Oct 2026 05:38:11 +0000 Subject: [PATCH 09/15] Recheck project workflow sources without storing prompts Why: Goal-sensitive workflow verification persisted model prompts and prior output text in trusted receipt rows, while cross-step model inputs could reuse stale machine output. Evidence: RED receipt privacy test; full Go suite, targeted race, isolated PostgreSQL store/runmgr/storage-migrate tests, vet, and static build passed locally before commit. A known unrelated TempDir cleanup flake failed once and passed on targeted and serial full replay. Impact: Add optional same-provider source recheck, verify every included item and evidence body before reuse, gate cross-step outputs, reject unsettled attempts, remove raw scope columns, scrub isolated candidate v34 SQLite/PG schemas, and reject unsafe backups. Refs: Approved RDM stage6 W6 source-recheck design adjustment r1; W6 independent review. --- cmd/storage-migrate/integration_test.go | 43 +++++ cmd/storage-migrate/main.go | 23 +++ cmd/storage-migrate/main_test.go | 53 ++++++ internal/app/app.go | 1 + internal/app/project_context.go | 27 +++ internal/app/project_context_test.go | 44 ++++- internal/projectcontext/context.go | 10 +- internal/projectcontext/context_test.go | 11 +- internal/runmgr/project_context.go | 194 ++++++++++++++------ internal/runmgr/project_context_test.go | 156 +++++++++++++++- internal/runmgr/runmgr.go | 1 + internal/runtime/project_context_test.go | 16 +- internal/runtime/workflow_compiled_test.go | 2 +- internal/store/context_manifest.go | 19 +- internal/store/context_manifest_test.go | 134 +++++++++++++- internal/store/migrate.go | 73 +++++++- internal/store/migrate_compat.go | 1 + internal/store/workflow_step_source.go | 52 +++++- internal/store/workflow_step_source_test.go | 17 +- internal/wfcompile/host.go | 11 ++ internal/wfcompile/project_backend.go | 17 +- internal/wfcompile/project_sources.go | 36 ++++ internal/wfcompile/run.go | 5 +- internal/wfcompile/wfcompile_test.go | 17 +- pkg/extension/project_context.go | 21 +++ pkg/extension/surface.txt | 4 + 26 files changed, 884 insertions(+), 104 deletions(-) diff --git a/cmd/storage-migrate/integration_test.go b/cmd/storage-migrate/integration_test.go index d4997c0..5fa87c9 100644 --- a/cmd/storage-migrate/integration_test.go +++ b/cmd/storage-migrate/integration_test.go @@ -9,8 +9,51 @@ import ( "testing" "agent-system-backend/internal/store" + "agent-system-backend/pkg/extension" ) +func TestMigrateTrustedContextManifestWithoutRawScope(t *testing.T) { + target, cleanup := pgSchemaDSN(t) + defer cleanup() + fixture := buildFixture(t) + src, err := store.OpenSQLite(fixture) + if err != nil { + t.Fatal(err) + } + m := extension.ProjectContextManifest{ProjectID: "p1", AttemptID: "model-attempt", InputSHA256: "request-hash", Delivery: "attempted", + Included: []extension.ProjectContextManifestEntry{{SourceID: "source-a", Revision: "r1", SourceHash: "ha", InputSHA256: "input-hash"}}} + if err := src.AppendContextManifestWithScope("alice", "r1", m, store.ContextManifestScope{Bound: true}); err != nil { + t.Fatal(err) + } + m.Delivery = "responded" + if err := src.AppendContextManifestWithScope("alice", "r1", m, store.ContextManifestScope{Bound: true}); err != nil { + t.Fatal(err) + } + if err := src.Close(); err != nil { + t.Fatal(err) + } + snapshot := filepath.Join(t.TempDir(), "snapshot.db") + if err := cmdBackup([]string{"--source", fixture, "--out", snapshot}); err != nil { + t.Fatal(err) + } + if err := cmdMigrate([]string{"--source", snapshot, "--target", target, "--migration-id", "context-copy"}); err != nil { + t.Fatal(err) + } + pg, err := store.OpenPostgres(target, filepath.Join(t.TempDir(), "artifacts")) + if err != nil { + t.Fatal(err) + } + defer pg.Close() + receipts, err := pg.ListContextManifests("alice", "r1") + if err != nil || len(receipts) != 2 || len(receipts[1].Included) != 1 || receipts[1].Included[0].SourceID != "source-a" { + t.Fatalf("content-free model receipts not copied: count=%d err=%v", len(receipts), err) + } + var rawColumns int + if err := pg.DB().QueryRow(`SELECT COUNT(*) FROM information_schema.columns WHERE table_schema=current_schema() AND table_name='context_manifests' AND column_name IN ('resolve_goal','resolve_target_id')`).Scan(&rawColumns); err != nil || rawColumns != 0 { + t.Fatalf("migrated PG schema has raw prompt columns: count=%d err=%v", rawColumns, err) + } +} + // U4 集成(环境门控 AGENT_PG_TEST_DSN:CI test-pg job / 本机隔离实例): // 夹具 → 受管迁移 → 校验和验证 → 幂等重跑 → 断点续传 → 非空目标拒绝 → // 未知 owner 阻塞。每用例独立 PG schema(与 store 测试同隔离约定)。 diff --git a/cmd/storage-migrate/main.go b/cmd/storage-migrate/main.go index 92de9f2..93d9463 100644 --- a/cmd/storage-migrate/main.go +++ b/cmd/storage-migrate/main.go @@ -242,6 +242,9 @@ func cmdBackup(args []string) error { return err } defer db.Close() + if err := refuseCandidateRawContextScope(db); err != nil { + return err + } if _, err := db.Exec(`VACUUM INTO ?`, *out); err != nil { return fmt.Errorf("vacuum into: %w", err) } @@ -336,6 +339,9 @@ func cmdMigrate(args []string) error { return err } defer src.Close() + if err := refuseCandidateRawContextScope(src); err != nil { + return err + } snapID, _, err := fileSHA256(*source) if err != nil { return err @@ -410,6 +416,23 @@ func cmdMigrate(args []string) error { return writeReport(rep, *out) } +// Candidate W6 v34 may contain full model prompts in private scope columns. +// Never copy or back up such a source implicitly; it must be migrated and +// reviewed in an isolated Backend store before this tool accepts it. +func refuseCandidateRawContextScope(db *sql.DB) error { + if !sqliteHasTable(db, "context_manifests") { + return nil + } + var columns int + if err := db.QueryRow(`SELECT COUNT(*) FROM pragma_table_info('context_manifests') WHERE name IN ('resolve_goal','resolve_target_id')`).Scan(&columns); err != nil { + return err + } + if columns > 0 { + return fmt.Errorf("candidate context scope text columns remain; isolate and scrub source before backup or migration") + } + return nil +} + // guardIdentity:目标非空且身份不同即拒绝(R7:不覆盖他人数据)。 // - 存在其他 migration_id 的完成记录 → 拒绝; // - 同 migration_id 但 source_snapshot_id 不同(换了快照续传)→ 拒绝; diff --git a/cmd/storage-migrate/main_test.go b/cmd/storage-migrate/main_test.go index 8731e0e..24b9170 100644 --- a/cmd/storage-migrate/main_test.go +++ b/cmd/storage-migrate/main_test.go @@ -1,7 +1,10 @@ package main import ( + "bytes" + "database/sql" "encoding/json" + "os" "path/filepath" "strings" "testing" @@ -195,3 +198,53 @@ func TestCopyOrderCoversAllFixtureTables(t *testing.T) { t.Fatalf("copyOrder 漏表(新表加入 schema 后须同步工具清单):%v", missing) } } + +func TestBackupAndMigrateRejectCandidateRawContextScope(t *testing.T) { + path := buildFixture(t) + db, err := sql.Open("sqlite", path) + if err != nil { + t.Fatal(err) + } + if _, err := db.Exec(`ALTER TABLE context_manifests ADD COLUMN resolve_goal TEXT NOT NULL DEFAULT ''`); err != nil { + t.Fatal(err) + } + 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=35`); 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 { + t.Fatal(err) + } + if err := db.Close(); err != nil { + t.Fatal(err) + } + out := filepath.Join(t.TempDir(), "unsafe-backup.db") + if err := cmdBackup([]string{"--source", path, "--out", out}); err == nil || !strings.Contains(err.Error(), "candidate context scope text") { + t.Fatalf("backup accepted candidate v34 raw scope: %v", err) + } + if _, err := os.Stat(out); !os.IsNotExist(err) { + t.Fatalf("rejected backup still created output: %v", err) + } + if err := cmdMigrate([]string{"--source", path, "--target", "postgres://invalid", "--migration-id", "unsafe"}); err == nil || !strings.Contains(err.Error(), "candidate context scope text") { + t.Fatalf("migration accepted candidate v34 raw source: %v", err) + } + scrubbed, err := store.OpenSQLite(path) + if err != nil { + t.Fatal(err) + } + if err := scrubbed.Close(); err != nil { + t.Fatal(err) + } + if err := cmdBackup([]string{"--source", path, "--out", out}); err != nil { + t.Fatalf("scrubbed source rejected: %v", err) + } + data, err := os.ReadFile(out) + if err != nil { + t.Fatal(err) + } + if bytes.Contains(data, []byte("OLD_RESTRICTED_BODY")) { + t.Fatal("backup retained candidate raw scope body") + } +} diff --git a/internal/app/app.go b/internal/app/app.go index 0f9a111..af7521e 100644 --- a/internal/app/app.go +++ b/internal/app/app.go @@ -620,6 +620,7 @@ func Run(opt Options) error { if contextSlot.active() { mgr.SetProjectContextResolver(contextSlot.resolve) mgr.SetProjectEvidenceReader(contextSlot.readEvidence) + mgr.SetProjectContextSourceRechecker(contextSlot.recheck) } mgr.SetKnowledgeProjectScope(func(ctx context.Context, actor string, id core.ID) (core.ID, error) { return projectSlot.resolve(ctx, caps, actor, id) diff --git a/internal/app/project_context.go b/internal/app/project_context.go index fb056d3..1f1aeeb 100644 --- a/internal/app/project_context.go +++ b/internal/app/project_context.go @@ -118,6 +118,33 @@ func (s *projectContextSlot) readEvidence(ctx context.Context, req extension.Pro return evidence, nil } +func (s *projectContextSlot) recheck(ctx context.Context, req extension.ProjectContextSourceRecheckRequest) (extension.ProjectContextSourceRecheckResult, error) { + if s == nil || req.Scope.Actor == "" || req.Scope.ProjectID == "" || req.Scope.Goal != "" { + return extension.ProjectContextSourceRecheckResult{}, errors.New("extension: trusted source recheck scope missing") + } + s.mu.RLock() + p, holder := s.provider, s.holder + s.mu.RUnlock() + rechecker, ok := p.(extension.ProjectContextSourceRechecker) + if !ok { + return extension.ProjectContextSourceRecheckResult{}, errors.New("extension: project source rechecker unavailable") + } + req.Scope.PluginID = holder + bounded, cancel := context.WithTimeout(ctx, 3*time.Second) + defer cancel() + result, err := rechecker.Recheck(bounded, req) + if err != nil { + return extension.ProjectContextSourceRecheckResult{}, err + } + if err := bounded.Err(); err != nil { + return extension.ProjectContextSourceRecheckResult{}, err + } + if result.Bundle.ProjectID != req.Scope.ProjectID || result.Bundle.Status != extension.ProjectContextReady { + return extension.ProjectContextSourceRecheckResult{}, errors.New("extension: project sources no longer authorized") + } + return result, nil +} + const workContextBudgetChars = 12000 // TaskCreateForWork fixes the project binding while the authenticated command diff --git a/internal/app/project_context_test.go b/internal/app/project_context_test.go index 4b40b36..0725ff8 100644 --- a/internal/app/project_context_test.go +++ b/internal/app/project_context_test.go @@ -22,6 +22,46 @@ type testProjectContextProvider struct { read func(extension.ProjectEvidenceRequest) (extension.ProjectEvidence, error) } +type testSourceRecheckProvider struct { + testProjectContextProvider + called *bool +} + +func (p testSourceRecheckProvider) Recheck(ctx context.Context, req extension.ProjectContextSourceRecheckRequest) (extension.ProjectContextSourceRecheckResult, error) { + if _, ok := ctx.Deadline(); !ok || req.Scope.PluginID != "rdm" || req.Scope.Actor != "alice" || req.Scope.ProjectID != "p1" || req.Scope.Goal != "" { + return extension.ProjectContextSourceRecheckResult{}, fmt.Errorf("untrusted or unbounded recheck scope") + } + *p.called = true + return extension.ProjectContextSourceRecheckResult{Bundle: extension.ProjectContextBundle{Status: extension.ProjectContextReady, ProjectID: req.Scope.ProjectID}}, nil +} + +func TestProjectSourceRecheckUsesSameRegisteredProviderAndDeadline(t *testing.T) { + called := false + slot := &projectContextSlot{} + provider := testSourceRecheckProvider{called: &called} + if err := (pluginRegistry{pluginID: "rdm", caps: hostcaps{contexts: slot}}).RegisterProjectContextProvider(provider); err != nil { + t.Fatal(err) + } + if _, err := slot.recheck(context.Background(), extension.ProjectContextSourceRecheckRequest{Scope: extension.ProjectContextRequest{Actor: "alice", ProjectID: "p1"}}); err != nil || !called { + t.Fatalf("registered source rechecker unavailable: called=%v err=%v", called, err) + } + if _, err := slot.recheck(context.Background(), extension.ProjectContextSourceRecheckRequest{Scope: extension.ProjectContextRequest{Actor: "alice", ProjectID: "p1", Goal: "OLD_RESTRICTED_BODY"}}); err == nil { + t.Fatal("raw Goal entered source recheck provider") + } + cancelled, cancel := context.WithCancel(context.Background()) + cancel() + if _, err := slot.recheck(cancelled, extension.ProjectContextSourceRecheckRequest{Scope: extension.ProjectContextRequest{Actor: "alice", ProjectID: "p1"}}); err == nil { + t.Fatal("cancelled source recheck returned authorized context") + } + legacy := &projectContextSlot{} + if err := (pluginRegistry{pluginID: "rdm", caps: hostcaps{contexts: legacy}}).RegisterProjectContextProvider(testProjectContextProvider{}); err != nil { + t.Fatal(err) + } + if _, err := legacy.recheck(context.Background(), extension.ProjectContextSourceRecheckRequest{Scope: extension.ProjectContextRequest{Actor: "alice", ProjectID: "p1"}}); err == nil { + t.Fatal("provider without optional rechecker silently accepted old sources") + } +} + type deadlineContextProvider struct{ sawDeadline *bool } func (p deadlineContextProvider) Resolve(ctx context.Context, req extension.ProjectContextRequest) (extension.ProjectContextBundle, error) { @@ -266,8 +306,8 @@ func TestCompleteForWorkUsesStoredScopeAndActualInputManifest(t *testing.T) { func TestWorkContextBudgetKeepsRequiredEvidenceFirst(t *testing.T) { bundle := extension.ProjectContextBundle{Status: extension.ProjectContextReady, ProjectID: "project-1", Items: []extension.ProjectContextItem{ - {SourceID: "optional", Text: "12345678"}, - {SourceID: "required", Text: "must keep", Required: true}, + {SourceID: "optional", Revision: "r1", Hash: "h1", Text: "12345678"}, + {SourceID: "required", Revision: "r1", Hash: "h2", Text: "must keep", Required: true}, }} prompt, manifest, err := assembleWorkCompletion("goal", bundle, 140) if err != nil { diff --git a/internal/projectcontext/context.go b/internal/projectcontext/context.go index a8b47d2..255c799 100644 --- a/internal/projectcontext/context.go +++ b/internal/projectcontext/context.go @@ -27,10 +27,16 @@ func Assemble(bundle extension.ProjectContextBundle, budget int) (string, extens missingRequired := false appendItem := func(item extension.ProjectContextItem) { entry := extension.ProjectContextManifestEntry{SourceID: item.SourceID, Revision: item.Revision, SourceHash: item.Hash} + if item.SourceID == "" || item.Revision == "" || item.Hash == "" { + entry.Reason = "unverifiable-source" + m.Dropped = append(m.Dropped, entry) + missingRequired = missingRequired || item.Required + return + } runes := []rune(item.Text) label := "\n[Source " + item.SourceID + " revision " + item.Revision + "]\n" cost := len(runes) + len([]rune(label)) - if cost > remaining || item.SourceID == "" { + if cost > remaining { entry.Reason = "over-budget-or-missing-source" m.Dropped = append(m.Dropped, entry) missingRequired = missingRequired || item.Required @@ -66,7 +72,7 @@ func Assemble(bundle extension.ProjectContextBundle, budget int) (string, extens } } for _, ref := range bundle.Evidence { - if ref.ID == "" || ref.SourceID == "" { + if ref.ID == "" || ref.SourceID == "" || ref.Revision == "" || ref.Hash == "" { continue } line := fmt.Sprintf("\n[Evidence ref %q source %q revision %q]\n", ref.ID, ref.SourceID, ref.Revision) diff --git a/internal/projectcontext/context_test.go b/internal/projectcontext/context_test.go index bf26a3a..abaeda3 100644 --- a/internal/projectcontext/context_test.go +++ b/internal/projectcontext/context_test.go @@ -8,7 +8,7 @@ import ( ) func TestAssembleOffersOnlyBudgetedEvidenceReferences(t *testing.T) { - bundle := extension.ProjectContextBundle{Status: extension.ProjectContextReady, ProjectID: "p1", Evidence: []extension.ProjectEvidenceRef{{ID: "ref-1", SourceID: "doc-1", Revision: "r2"}, {ID: strings.Repeat("x", 1000), SourceID: "doc-2"}}} + bundle := extension.ProjectContextBundle{Status: extension.ProjectContextReady, ProjectID: "p1", Evidence: []extension.ProjectEvidenceRef{{ID: "ref-1", SourceID: "doc-1", Revision: "r2", Hash: "h1"}, {ID: strings.Repeat("x", 1000), SourceID: "doc-2", Revision: "r2", Hash: "h2"}}} text, manifest, err := Assemble(bundle, 180) if err != nil { t.Fatal(err) @@ -19,7 +19,7 @@ func TestAssembleOffersOnlyBudgetedEvidenceReferences(t *testing.T) { } func TestValidateCurrentRejectsWithdrawnOfferedReference(t *testing.T) { - ref := extension.ProjectEvidenceRef{ID: "ref-1", SourceID: "doc-1", Revision: "r2"} + ref := extension.ProjectEvidenceRef{ID: "ref-1", SourceID: "doc-1", Revision: "r2", Hash: "h1"} old := extension.ProjectContextBundle{Status: extension.ProjectContextReady, ProjectID: "p1", Evidence: []extension.ProjectEvidenceRef{ref}} _, manifest, err := Assemble(old, 180) if err != nil { @@ -41,3 +41,10 @@ func TestAssembleIncludesUnresolvedMissingStatus(t *testing.T) { t.Fatalf("missing status hidden from model or receipt: text=%q manifest=%+v", text, manifest) } } + +func TestAssembleRejectsRequiredSourceWithoutStableRevisionAndHash(t *testing.T) { + bundle := extension.ProjectContextBundle{Status: extension.ProjectContextReady, ProjectID: "p1", Items: []extension.ProjectContextItem{{SourceID: "legacy", Text: "private", Required: true}}} + if _, _, err := Assemble(bundle, 900); err == nil { + t.Fatal("legacy source without revision/hash entered recoverable project model input") + } +} diff --git a/internal/runmgr/project_context.go b/internal/runmgr/project_context.go index 763eb84..11c02b8 100644 --- a/internal/runmgr/project_context.go +++ b/internal/runmgr/project_context.go @@ -5,6 +5,7 @@ import ( "crypto/sha256" "errors" "fmt" + "slices" "strings" "time" @@ -26,6 +27,10 @@ func (m *Manager) SetProjectEvidenceReader(fn func(context.Context, extension.Pr m.projectEvidenceReader = fn } +func (m *Manager) SetProjectContextSourceRechecker(fn func(context.Context, extension.ProjectContextSourceRecheckRequest) (extension.ProjectContextSourceRecheckResult, error)) { + m.projectSourceRechecker = fn +} + func (c *runCtx) ProjectContextEnabled() bool { return c.run.ProjectID != "" && c.mgr.projectContextResolver != nil } @@ -44,12 +49,114 @@ func (c *runCtx) ValidateProjectCrossGuard(ctx context.Context, current extensio if err != nil { return err } + terminal := map[string]string{} for _, m := range prior { + if m.Delivery == "responded" || m.Delivery == "unknown" { + terminal[m.AttemptID] = m.Delivery + } + } + for _, m := range prior { + if m.Delivery == "attempted" { + if terminal[m.AttemptID] != "responded" { + return errors.New("prior project model attempt has no trusted response") + } + continue + } + if m.Delivery != "responded" { + return errors.New("prior project model delivery is unknown") + } if m.ProjectID != current.ProjectID || m.AuthorizationEpoch == "" || m.PublicationEpoch == "" || current.AuthorizationEpoch == "" || current.PublicationEpoch == "" || m.AuthorizationEpoch != current.AuthorizationEpoch || m.PublicationEpoch != current.PublicationEpoch || m.Watermark != current.Watermark { return errors.New("prior project model work used a different authorization or publication revision") } + if err := c.recheckManifest(ctx, m, ""); err != nil { + return err + } + } + return nil +} + +// recheckManifest reauthorizes the exact source identities that were actually +// present in a prior model request. It never persists or reconstructs a Goal. +func (c *runCtx) recheckManifest(ctx context.Context, m extension.ProjectContextManifest, stepID string) error { + if m.Delivery != "responded" || c.mgr.projectSourceRechecker == nil { + return errors.New("project source recheck unavailable") + } + if m.BudgetChars <= 0 || len(m.Included)+len(m.OfferedReferences)+len(m.ReadEvidence) == 0 { + return errors.New("prior project model input has no stable source") + } + for _, entry := range m.Included { + if entry.SourceID == "" || entry.Revision == "" || entry.SourceHash == "" || entry.InputSHA256 == "" { + return errors.New("prior project model input has incomplete source identity") + } + } + for _, ref := range m.OfferedReferences { + if ref.ID == "" || ref.SourceID == "" || ref.Revision == "" || ref.Hash == "" { + return errors.New("prior project evidence reference is incomplete") + } + } + scope, err := c.trustedProjectContextRequest(ctx, m.BudgetChars) + if err != nil { + return err + } + scope.StepID = core.ID(stepID) + result, err := c.mgr.projectSourceRechecker(ctx, extension.ProjectContextSourceRecheckRequest{ + Scope: scope, Included: append([]extension.ProjectContextManifestEntry(nil), m.Included...), + OfferedReferences: append([]extension.ProjectEvidenceRef(nil), m.OfferedReferences...), + ReadEvidence: append([]extension.ProjectContextManifestEntry(nil), m.ReadEvidence...), + }) + if err != nil { + return err + } + current := result.Bundle + expectedRefs := map[string]extension.ProjectEvidenceRef{} + for _, ref := range m.OfferedReferences { + expectedRefs[ref.ID] = ref + } + for _, used := range m.ReadEvidence { + ref := extension.ProjectEvidenceRef{ID: used.ReferenceID, SourceID: used.SourceID, Revision: used.Revision, Hash: used.SourceHash} + if ref.ID == "" || ref.SourceID == "" || ref.Revision == "" || ref.Hash == "" { + return errors.New("read project evidence lacks stable reference") + } + if existing, ok := expectedRefs[ref.ID]; ok && existing != ref { + return errors.New("project evidence reference identity changed") + } + expectedRefs[ref.ID] = ref + } + if len(current.Items) != len(m.Included) || len(current.Evidence) != len(expectedRefs) || !slices.Equal(current.Missing, m.Missing) { + return errors.New("project source recheck returned an incomplete source set") + } + seenRefs := map[string]bool{} + for _, ref := range current.Evidence { + if ref != expectedRefs[ref.ID] || seenRefs[ref.ID] { + return errors.New("project source recheck returned an unrelated evidence reference") + } + seenRefs[ref.ID] = true + } + if err := projectcontext.ValidateCurrent(m, current); err != nil { + return err + } + for _, used := range m.ReadEvidence { + if used.ReferenceID == "" || used.InputSHA256 == "" || c.mgr.projectEvidenceReader == nil { + return errors.New("project evidence recheck unavailable") + } + matched := false + for _, ref := range current.Evidence { + if ref.ID != used.ReferenceID || ref.SourceID != used.SourceID || ref.Revision != used.Revision || ref.Hash != used.SourceHash { + continue + } + readScope := scope + readScope.BudgetChars = 8000 + now, err := c.mgr.projectEvidenceReader(ctx, extension.ProjectEvidenceRequest{Scope: readScope, Reference: ref}) + if err == nil && now.Reference == ref && fmt.Sprintf("%x", sha256.Sum256([]byte(now.Text))) == used.InputSHA256 { + matched = true + } + break + } + if !matched { + return errors.New("project evidence permission or content changed") + } } return nil } @@ -135,41 +242,50 @@ func (c *runCtx) ProjectContext(ctx context.Context, goal string, budget int) (e if c.run.ProjectID == "" || c.mgr.projectContextResolver == nil { return extension.ProjectContextBundle{}, false, nil } - if c.mgr.projectWorkAuthorization == nil { - return extension.ProjectContextBundle{}, true, errors.New("project work authorization missing") - } - run, err := c.mgr.st.GetRunByID(string(c.run.ID)) + req, err := c.trustedProjectContextRequest(ctx, budget) if err != nil { return extension.ProjectContextBundle{}, true, err } - if run.Owner == "" || run.Owner != c.run.Owner || run.ProjectID != c.run.ProjectID || run.ConversationID != c.run.ConversationID { - return extension.ProjectContextBundle{}, true, errors.New("persisted run scope mismatch") - } - conv, err := c.mgr.st.GetConversation(string(run.Owner), string(run.ConversationID)) - if err != nil { - return extension.ProjectContextBundle{}, true, err - } - if conv.ProjectID != run.ProjectID { - return extension.ProjectContextBundle{}, true, errors.New("persisted conversation scope mismatch") - } - if err := c.mgr.authorizeProjectWork(ctx, string(run.Owner), run.ProjectID); err != nil { - return extension.ProjectContextBundle{}, true, err - } - req := extension.ProjectContextRequest{ - Actor: string(run.Owner), ProjectID: run.ProjectID, ConversationID: run.ConversationID, - RunID: run.ID, Goal: goal, BudgetChars: budget, - } + req.Goal = goal bundle, err := c.mgr.projectContextResolver(ctx, req) if err != nil { return extension.ProjectContextBundle{}, true, err } - if bundle.ProjectID != run.ProjectID || bundle.Status != extension.ProjectContextReady { + if bundle.ProjectID != req.ProjectID || bundle.Status != extension.ProjectContextReady { return extension.ProjectContextBundle{}, true, errors.New("project context unavailable for trusted run") } c.setContextRequest(req) return bundle, true, nil } +func (c *runCtx) trustedProjectContextRequest(ctx context.Context, budget int) (extension.ProjectContextRequest, error) { + if c.mgr.projectWorkAuthorization == nil { + return extension.ProjectContextRequest{}, errors.New("project work authorization missing") + } + run, err := c.mgr.st.GetRunByID(string(c.run.ID)) + if err != nil { + return extension.ProjectContextRequest{}, err + } + if run.Owner == "" || run.Owner != c.run.Owner || run.ProjectID != c.run.ProjectID || run.ConversationID != c.run.ConversationID { + return extension.ProjectContextRequest{}, errors.New("persisted run scope mismatch") + } + conv, err := c.mgr.st.GetConversation(string(run.Owner), string(run.ConversationID)) + if err != nil { + return extension.ProjectContextRequest{}, err + } + if conv.ProjectID != run.ProjectID { + return extension.ProjectContextRequest{}, errors.New("persisted conversation scope mismatch") + } + if err := c.mgr.authorizeProjectWork(ctx, string(run.Owner), run.ProjectID); err != nil { + return extension.ProjectContextRequest{}, err + } + req := extension.ProjectContextRequest{ + Actor: string(run.Owner), ProjectID: run.ProjectID, ConversationID: run.ConversationID, + RunID: run.ID, BudgetChars: budget, + } + return req, nil +} + // SaveContextManifest persists a receipt for the exact request prepared by the // runtime. It does not store model-visible text or candidate-only references. func (c *runCtx) SaveContextManifest(ctx context.Context, m extension.ProjectContextManifest) error { @@ -258,7 +374,7 @@ func (c *runCtx) SaveContextManifest(ctx context.Context, m extension.ProjectCon } if scopeBound { return c.mgr.st.AppendContextManifestWithScope(string(run.Owner), string(run.ID), m, - store.ContextManifestScope{Goal: resolveGoal, TargetID: resolveTarget, Bound: true}) + store.ContextManifestScope{Bound: true}) } return c.mgr.st.AppendContextManifest(string(run.Owner), string(run.ID), m) } @@ -309,8 +425,8 @@ func (c *runCtx) VerifyProjectStepOutput(ctx context.Context, stepID, stepAttemp } matchedBound := false for _, source := range receipts { - receipt, selector := source.Manifest, source.Scope - if !selector.Bound || selector.TargetID != "" || receipt.ProjectID != c.run.ProjectID || + receipt := source.Manifest + if !source.Scope.Bound || receipt.ProjectID != c.run.ProjectID || receipt.WorkflowStepAttemptID != stepAttemptID || receipt.AuthorizationEpoch != bound.AuthorizationEpoch || receipt.PublicationEpoch != bound.PublicationEpoch || receipt.Watermark != bound.Watermark || receipt.Version != bound.Version { return errors.New("project workflow output lost its trusted model source") @@ -318,35 +434,9 @@ func (c *runCtx) VerifyProjectStepOutput(ctx context.Context, stepID, stepAttemp if receipt.AttemptID == bound.AttemptID { matchedBound = true } - current, active, err := c.ProjectContext(ctx, selector.Goal, receipt.BudgetChars) - if err != nil || !active || current.ProjectID != receipt.ProjectID || - current.AuthorizationEpoch != receipt.AuthorizationEpoch || current.PublicationEpoch != receipt.PublicationEpoch || current.Watermark != receipt.Watermark { - return errors.New("project workflow output authorization or publication changed") - } - if err := projectcontext.ValidateCurrent(receipt, current); err != nil { + if err := c.recheckManifest(ctx, receipt, stepID); err != nil { return fmt.Errorf("project workflow output source changed: %w", err) } - for _, used := range receipt.ReadEvidence { - if c.mgr.projectEvidenceReader == nil { - return errors.New("project workflow evidence reader unavailable") - } - matched := false - for _, ref := range current.Evidence { - if ref.ID != used.ReferenceID || ref.SourceID != used.SourceID || ref.Revision != used.Revision || ref.Hash != used.SourceHash { - continue - } - scope := extension.ProjectContextRequest{Actor: string(c.run.Owner), ProjectID: c.run.ProjectID, - ConversationID: c.run.ConversationID, RunID: c.run.ID, Goal: selector.Goal, TargetID: selector.TargetID, BudgetChars: 8000} - now, err := c.mgr.projectEvidenceReader(ctx, extension.ProjectEvidenceRequest{Scope: scope, Reference: ref}) - if err == nil && now.Reference == ref && fmt.Sprintf("%x", sha256.Sum256([]byte(now.Text))) == used.InputSHA256 { - matched = true - } - break - } - if !matched { - return errors.New("project workflow evidence permission or content changed") - } - } } if !matchedBound { return errors.New("project workflow output binding lost its responded model attempt") diff --git a/internal/runmgr/project_context_test.go b/internal/runmgr/project_context_test.go index 290802f..3e7b938 100644 --- a/internal/runmgr/project_context_test.go +++ b/internal/runmgr/project_context_test.go @@ -2,6 +2,7 @@ package runmgr import ( "context" + "crypto/sha256" "fmt" "os" "strings" @@ -19,6 +20,69 @@ import ( core "agent-system-core" ) +func TestProjectSourceRecheckVerifiesIncludedRefsAndEvidenceBody(t *testing.T) { + st := openRecoveryStore(t) + mgr := newRecoveryManager(t, st) + conv := recoveryConversation(t, st) + run := recoveryRun(t, st, conv, core.RunPending) + mgr.SetProjectWorkAuthorization(func(context.Context, string, core.ID) (bool, error) { return true, nil }) + item := extension.ProjectContextItem{SourceID: "source-a", Revision: "r1", Hash: "ha", Text: "AUTHORIZED_A", Required: true} + ref := extension.ProjectEvidenceRef{ID: "ref-a", SourceID: "doc-a", Revision: "r1", Hash: "hd"} + bundle := extension.ProjectContextBundle{Status: extension.ProjectContextReady, ProjectID: run.ProjectID, Version: "v1", Watermark: "w1", AuthorizationEpoch: "duty-1", PublicationEpoch: "pub-1", Items: []extension.ProjectContextItem{item}, Evidence: []extension.ProjectEvidenceRef{ref}} + _, receipt, err := projectcontext.Assemble(bundle, 900) + if err != nil { + t.Fatal(err) + } + receipt.Delivery = "responded" + receipt.ReadEvidence = []extension.ProjectContextManifestEntry{{ReferenceID: ref.ID, SourceID: ref.SourceID, Revision: ref.Revision, SourceHash: ref.Hash, InputSHA256: fmt.Sprintf("%x", sha256.Sum256([]byte("DETAIL_A")))}} + rc := &runCtx{mgr: mgr, run: run, ctx: context.Background()} + current := bundle + readBody := "DETAIL_A" + offeredCount := 1 + mgr.SetProjectContextSourceRechecker(func(_ context.Context, req extension.ProjectContextSourceRecheckRequest) (extension.ProjectContextSourceRecheckResult, error) { + if req.Scope.Actor != string(run.Owner) || req.Scope.ProjectID != run.ProjectID || req.Scope.RunID != run.ID || req.Scope.ConversationID != run.ConversationID || req.Scope.StepID != "draft" || req.Scope.Goal != "" || len(req.Included) != 1 || len(req.OfferedReferences) != offeredCount || len(req.ReadEvidence) != 1 { + return extension.ProjectContextSourceRecheckResult{}, fmt.Errorf("source recheck lost trusted run scope or actual input refs") + } + return extension.ProjectContextSourceRecheckResult{Bundle: current}, nil + }) + mgr.SetProjectEvidenceReader(func(_ context.Context, req extension.ProjectEvidenceRequest) (extension.ProjectEvidence, error) { + if req.Scope.Actor != string(run.Owner) || req.Scope.RunID != run.ID || req.Scope.StepID != "draft" || req.Scope.Goal != "" || req.Reference != ref { + return extension.ProjectEvidence{}, fmt.Errorf("evidence read lost current trusted scope") + } + return extension.ProjectEvidence{Reference: req.Reference, Text: readBody}, nil + }) + if err := rc.recheckManifest(context.Background(), receipt, "draft"); err != nil { + t.Fatalf("same authorized sources rejected: %v", err) + } + // A detailed body may be in the final request even when its reference was + // not offered in that request's short context. Recheck that exact read too. + receipt.OfferedReferences = nil + offeredCount = 0 + if err := rc.recheckManifest(context.Background(), receipt, "draft"); err != nil { + t.Fatalf("read evidence not offered as a short ref was rejected: %v", err) + } + receipt.OfferedReferences = []extension.ProjectEvidenceRef{ref} + offeredCount = 1 + current.Items = []extension.ProjectContextItem{{SourceID: item.SourceID, Revision: item.Revision, Hash: item.Hash, Text: "CHANGED", Required: true}} + if err := rc.recheckManifest(context.Background(), receipt, "draft"); err == nil { + t.Fatal("provider echoed old source hash but changed model-visible text") + } + current = bundle + current.Evidence = nil + if err := rc.recheckManifest(context.Background(), receipt, "draft"); err == nil { + t.Fatal("withdrawn offered reference accepted") + } + current = bundle + readBody = "REVOKED_DETAIL" + if err := rc.recheckManifest(context.Background(), receipt, "draft"); err == nil { + t.Fatal("changed ReadEvidence body accepted") + } + mgr.SetProjectContextSourceRechecker(nil) + if err := rc.recheckManifest(context.Background(), receipt, "draft"); err == nil { + t.Fatal("missing rechecker accepted old machine context") + } +} + func TestProjectContextUsesPersistedRunAndRefreshesAuthorization(t *testing.T) { st := openRecoveryStore(t) m := newRecoveryManager(t, st) @@ -90,6 +154,10 @@ func TestProjectJudgeChecksPersistedSourceEpochAcrossGuards(t *testing.T) { m.SetProjectContextResolver(func(_ context.Context, req extension.ProjectContextRequest) (extension.ProjectContextBundle, error) { return extension.ProjectContextBundle{Status: extension.ProjectContextReady, ProjectID: req.ProjectID, AuthorizationEpoch: epoch, PublicationEpoch: publication, Watermark: "commit-1", Items: items}, nil }) + m.SetProjectContextSourceRechecker(func(_ context.Context, req extension.ProjectContextSourceRecheckRequest) (extension.ProjectContextSourceRecheckResult, error) { + return extension.ProjectContextSourceRecheckResult{Bundle: extension.ProjectContextBundle{Status: extension.ProjectContextReady, ProjectID: req.Scope.ProjectID, + AuthorizationEpoch: epoch, PublicationEpoch: publication, Watermark: "commit-1", Items: items}}, nil + }) rc := &runCtx{mgr: m, run: run, ctx: context.Background()} started := time.Now().Add(-time.Second) bundle, _, err := rc.ProjectContext(context.Background(), "write output", 900) @@ -182,6 +250,14 @@ func TestProjectJudgeRetainsGoalSensitiveSourceScope(t *testing.T) { } return extension.ProjectContextBundle{Status: extension.ProjectContextReady, ProjectID: req.ProjectID, AuthorizationEpoch: "duty-1", PublicationEpoch: "pub-1", Watermark: "w1", Items: []extension.ProjectContextItem{item}}, nil }) + m.SetProjectContextSourceRechecker(func(_ context.Context, req extension.ProjectContextSourceRecheckRequest) (extension.ProjectContextSourceRecheckResult, error) { + item := extension.ProjectContextItem{SourceID: "source-b", Revision: "r1", Hash: "hb", Text: "PUBLIC_B", Required: true} + if len(req.Included) == 1 && req.Included[0].SourceID == "source-a" && available { + item = extension.ProjectContextItem{SourceID: "source-a", Revision: "r1", Hash: "ha", Text: "AUTHORIZED_A", Required: true} + } + return extension.ProjectContextSourceRecheckResult{Bundle: extension.ProjectContextBundle{Status: extension.ProjectContextReady, ProjectID: req.Scope.ProjectID, + AuthorizationEpoch: "duty-1", PublicationEpoch: "pub-1", Watermark: "w1", Items: []extension.ProjectContextItem{item}}}, nil + }) rc := &runCtx{mgr: m, run: run, ctx: context.Background()} const goal = "draft report from approved source" bundle, _, err := rc.ProjectContext(context.Background(), goal, 900) @@ -238,10 +314,12 @@ func TestProjectJudgeRetainsGoalSensitiveSourceScope(t *testing.T) { type goalSensitiveStepBackend struct { stream agentllm.StreamFunc sent string + calls int } func (*goalSensitiveStepBackend) Name() string { return "goal-sensitive-test" } func (b *goalSensitiveStepBackend) Execute(ctx context.Context, req squad.ExecRequest) (*squad.ExecResult, error) { + b.calls++ for ev := range b.stream(ctx, "test-model", []agentllm.Message{{Role: agentllm.RoleUser, Content: req.Prompt}}, nil, agentllm.StreamOptions{}) { if ev.Kind == agentllm.KindError { return &squad.ExecResult{Success: false, ErrorReason: ev.Err.Error()}, nil @@ -254,6 +332,52 @@ func (b *goalSensitiveStepBackend) Execute(ctx context.Context, req squad.ExecRe return &squad.ExecResult{Success: true, Patches: map[string][]byte{"output.md": []byte("AUTHORIZED_A report\n")}}, nil } +func TestCompiledWorkflowSecondPromptRejectsRevokedPriorOutput(t *testing.T) { + st := openRecoveryStore(t) + mgr := newRecoveryManager(t, st) + conv := recoveryConversation(t, st) + run := recoveryRun(t, st, conv, core.RunPending) + mgr.SetProjectWorkAuthorization(func(context.Context, string, core.ID) (bool, error) { return true, nil }) + available := true + item := extension.ProjectContextItem{SourceID: "source-a", Revision: "r1", Hash: "ha", Text: "AUTHORIZED_A", Required: true} + mgr.SetProjectContextResolver(func(_ context.Context, req extension.ProjectContextRequest) (extension.ProjectContextBundle, error) { + items := []extension.ProjectContextItem{item} + if !available { + items = []extension.ProjectContextItem{{SourceID: "source-b", Revision: "r1", Hash: "hb", Text: "PUBLIC_B", Required: true}} + } + return extension.ProjectContextBundle{Status: extension.ProjectContextReady, ProjectID: req.ProjectID, AuthorizationEpoch: "duty-1", PublicationEpoch: "pub-1", Watermark: "w1", Items: items}, nil + }) + rechecks := 0 + mgr.SetProjectContextSourceRechecker(func(_ context.Context, req extension.ProjectContextSourceRecheckRequest) (extension.ProjectContextSourceRecheckResult, error) { + rechecks++ + result := extension.ProjectContextSourceRecheckResult{Bundle: extension.ProjectContextBundle{Status: extension.ProjectContextReady, ProjectID: req.Scope.ProjectID, AuthorizationEpoch: "duty-1", PublicationEpoch: "pub-1", Watermark: "w1", Items: []extension.ProjectContextItem{item}}} + if rechecks == 1 { + available = false + } else { + result.Bundle.Items = nil + } + return result, nil + }) + rc := &runCtx{mgr: mgr, run: run, ctx: context.Background()} + base := func(_ context.Context, _ string, _ []agentllm.Message, _ []agentllm.ToolDecl, _ agentllm.StreamOptions) <-chan agentllm.Event { + ch := make(chan agentllm.Event, 1) + ch <- agentllm.Event{Kind: agentllm.KindMessageEnd, Message: agentllm.Message{Role: agentllm.RoleAssistant, Content: "done"}} + close(ch) + return ch + } + backend := &goalSensitiveStepBackend{stream: runtime.GuardProjectStream(rc, "", base)} + backends := squad.NewBackendResolver() + backends.RegisterInstance(backend.Name(), backend) + def := core.WorkflowDef{ID: "two-prompts", Title: "Two prompts", Steps: []core.WorkflowStep{ + {ID: "draft", Title: "Draft", Kind: "prompt", Agent: "writer", Prompt: "draft report"}, + {ID: "rewrite", Title: "Rewrite", Kind: "prompt", Agent: "writer", Prompt: "revise {{steps.draft.output}}"}, + }} + _, err := wfcompile.RunCompiled(rc, wfcompile.Deps{DataDir: t.TempDir(), Backends: backends, DefaultBackend: backend.Name()}, workflow.Launch{Def: def}, workflow.Vars{Input: "draft"}, wfcompile.RunOptions{}) + if err == nil || backend.calls != 1 || rechecks < 2 { + t.Fatalf("second prompt consumed revoked first output: err=%v calls=%d rechecks=%d", err, backend.calls, rechecks) + } +} + func TestCompiledWorkflowJudgeAcceptsGoalSensitiveProviderAndRejectsWithdrawal(t *testing.T) { st := openRecoveryStore(t) mgr := newRecoveryManager(t, st) @@ -270,6 +394,14 @@ func TestCompiledWorkflowJudgeAcceptsGoalSensitiveProviderAndRejectsWithdrawal(t } return extension.ProjectContextBundle{Status: extension.ProjectContextReady, ProjectID: req.ProjectID, AuthorizationEpoch: "duty-1", PublicationEpoch: "pub-1", Watermark: "w1", Items: []extension.ProjectContextItem{item}}, nil }) + mgr.SetProjectContextSourceRechecker(func(_ context.Context, req extension.ProjectContextSourceRecheckRequest) (extension.ProjectContextSourceRecheckResult, error) { + item := extension.ProjectContextItem{SourceID: "source-b", Revision: "r1", Hash: "hb", Text: "PUBLIC_B", Required: true} + if len(req.Included) == 1 && req.Included[0].SourceID == "source-a" && available { + item = extension.ProjectContextItem{SourceID: "source-a", Revision: "r1", Hash: "ha", Text: "AUTHORIZED_A", Required: true} + } + return extension.ProjectContextSourceRecheckResult{Bundle: extension.ProjectContextBundle{Status: extension.ProjectContextReady, ProjectID: req.Scope.ProjectID, + AuthorizationEpoch: "duty-1", PublicationEpoch: "pub-1", Watermark: "w1", Items: []extension.ProjectContextItem{item}}}, nil + }) rc := &runCtx{mgr: mgr, run: run, ctx: context.Background()} stepBase := func(_ context.Context, _ string, messages []agentllm.Message, _ []agentllm.ToolDecl, _ agentllm.StreamOptions) <-chan agentllm.Event { ch := make(chan agentllm.Event, 1) @@ -298,6 +430,26 @@ func TestCompiledWorkflowJudgeAcceptsGoalSensitiveProviderAndRejectsWithdrawal(t if err != nil || judgeCalls != 1 || backend.sent == "" { t.Fatalf("authorized goal-sensitive workflow failed: err=%v judgeCalls=%d sent=%q resolved=%q", err, judgeCalls, backend.sent, resolved) } + rows, err := st.DB().Query(`SELECT manifest_json FROM context_manifests WHERE owner=$1 AND target_id=$2`, string(run.Owner), string(run.ID)) + if err != nil { + t.Fatal(err) + } + for rows.Next() { + var raw string + if err := rows.Scan(&raw); err != nil { + rows.Close() + t.Fatal(err) + } + if strings.Contains(raw, "AUTHORIZED_A report") || strings.Contains(raw, "draft report using") { + rows.Close() + t.Fatal("workflow model prompt or output body entered trusted receipt JSON") + } + } + if err := rows.Err(); err != nil { + rows.Close() + t.Fatal(err) + } + rows.Close() bound, err := st.WorkflowStepSource(string(run.Owner), string(run.ID), "draft") if err != nil { t.Fatal(err) @@ -337,7 +489,7 @@ func TestContextManifestRejectsDocumentDutyRevocationBeforeSend(t *testing.T) { epoch := "duty-1" m.SetProjectContextResolver(func(_ context.Context, req extension.ProjectContextRequest) (extension.ProjectContextBundle, error) { return extension.ProjectContextBundle{Status: extension.ProjectContextReady, ProjectID: req.ProjectID, Version: "v1", AuthorizationEpoch: epoch, - Items: []extension.ProjectContextItem{{SourceID: "restricted-doc", Text: "private text", Required: true}}}, nil + Items: []extension.ProjectContextItem{{SourceID: "restricted-doc", Revision: "r1", Hash: "h1", Text: "private text", Required: true}}}, nil }) rc := &runCtx{mgr: m, run: run, ctx: context.Background()} bundle, active, err := rc.ProjectContext(context.Background(), "work", 900) @@ -410,7 +562,7 @@ func TestConcurrentProjectEvidenceReadsKeepTrustedScope(t *testing.T) { conv := recoveryConversation(t, st) run := recoveryRun(t, st, conv, core.RunPending) m.SetProjectWorkAuthorization(func(context.Context, string, core.ID) (bool, error) { return true, nil }) - ref := extension.ProjectEvidenceRef{ID: "r1", SourceID: "doc", Revision: "v1"} + ref := extension.ProjectEvidenceRef{ID: "r1", SourceID: "doc", Revision: "v1", Hash: "h1"} m.SetProjectContextResolver(func(_ context.Context, req extension.ProjectContextRequest) (extension.ProjectContextBundle, error) { return extension.ProjectContextBundle{Status: extension.ProjectContextReady, ProjectID: req.ProjectID, Evidence: []extension.ProjectEvidenceRef{ref}}, nil }) diff --git a/internal/runmgr/runmgr.go b/internal/runmgr/runmgr.go index 1ed8485..fee313a 100644 --- a/internal/runmgr/runmgr.go +++ b/internal/runmgr/runmgr.go @@ -64,6 +64,7 @@ type Manager struct { projectWorkAuthorization func(context.Context, string, core.ID) (bool, error) projectContextResolver func(context.Context, extension.ProjectContextRequest) (extension.ProjectContextBundle, error) projectEvidenceReader func(context.Context, extension.ProjectEvidenceRequest) (extension.ProjectEvidence, error) + projectSourceRechecker func(context.Context, extension.ProjectContextSourceRecheckRequest) (extension.ProjectContextSourceRecheckResult, error) // commands EXT-14:命令即工具的投影与分发源(nil = 未接线,命令不进工具清单)。 commands CommandSource indexer core.DocumentIndexer // 可选索引入口(nil = store 整文档单块) diff --git a/internal/runtime/project_context_test.go b/internal/runtime/project_context_test.go index 06299fe..0edb559 100644 --- a/internal/runtime/project_context_test.go +++ b/internal/runtime/project_context_test.go @@ -65,7 +65,7 @@ func TestProjectContextRefreshesPerModelRequestWithoutPersistingOldSummary(t *te return base(ctx, model, msgs, tools, opts) }} c := &projectRunCtx{fakeRunCtx: &fakeRunCtx{ctx: context.Background(), sandbox: t.TempDir(), projectID: "project-1", prof: core.AgentProfile{Model: core.ModelRoute{Chat: "test-model"}}}} - c.bundle = extension.ProjectContextBundle{Status: extension.ProjectContextReady, ProjectID: "project-1", Version: "v1", Items: []extension.ProjectContextItem{{SourceID: "s1", Text: "summary-v1", Required: true}}} + c.bundle = extension.ProjectContextBundle{Status: extension.ProjectContextReady, ProjectID: "project-1", Version: "v1", Items: []extension.ProjectContextItem{{SourceID: "s1", Revision: "r1", Hash: "h1", Text: "summary-v1", Required: true}}} if err := runner.Execute(c); err != nil { t.Fatal(err) } @@ -110,7 +110,7 @@ func TestProjectRevocationScrubsOldMachineContextBeforeSecondTurn(t *testing.T) base := scriptStream(turns, nil) var requests [][]agentllm.Message c := &projectRunCtx{fakeRunCtx: &fakeRunCtx{ctx: context.Background(), projectID: "p1", prof: core.AgentProfile{Model: core.ModelRoute{Chat: "test-model"}}, memEntries: []memEntry{{typ: "note", content: "STALE_MEMORY"}}, execOutput: old}, - bundle: extension.ProjectContextBundle{Status: extension.ProjectContextReady, ProjectID: "p1", Version: "v1", AuthorizationEpoch: "duty-1", PublicationEpoch: "pub-1", Items: []extension.ProjectContextItem{{SourceID: "doc", Text: old, Required: true}}}, + bundle: extension.ProjectContextBundle{Status: extension.ProjectContextReady, ProjectID: "p1", Version: "v1", AuthorizationEpoch: "duty-1", PublicationEpoch: "pub-1", Items: []extension.ProjectContextItem{{SourceID: "doc", Revision: "r1", Hash: "h1", Text: old, Required: true}}}, budgetSettings: core.EffectiveSettings{"models": map[string]any{"chat": map[string]any{"baseUrl": "http://test"}}, "context": map[string]any{"userInstructions": "STALE_INSTRUCTIONS"}, "runtime": map[string]any{"verify": false, "composeFinal": false}}, sessionNote: "STALE_SESSION_NOTE", injections: []ContextInjection{{Content: "STALE_INJECTION", Constant: true}}, surfaces: []core.SurfaceSnapshot{{SemanticText: "STALE_SURFACE"}}, @@ -118,7 +118,7 @@ func TestProjectRevocationScrubsOldMachineContextBeforeSecondTurn(t *testing.T) runner := &LLMRunner{Stream: func(ctx context.Context, model string, msgs []agentllm.Message, tools []agentllm.ToolDecl, opts agentllm.StreamOptions) <-chan agentllm.Event { requests = append(requests, append([]agentllm.Message(nil), msgs...)) if len(requests) == 1 { - c.bundle = extension.ProjectContextBundle{Status: extension.ProjectContextReady, ProjectID: "p1", Version: "v2", AuthorizationEpoch: "duty-2", PublicationEpoch: "pub-2", Items: []extension.ProjectContextItem{{SourceID: "public", Text: "CURRENT_PUBLIC_FACT", Required: true}}} + c.bundle = extension.ProjectContextBundle{Status: extension.ProjectContextReady, ProjectID: "p1", Version: "v2", AuthorizationEpoch: "duty-2", PublicationEpoch: "pub-2", Items: []extension.ProjectContextItem{{SourceID: "public", Revision: "r1", Hash: "h2", Text: "CURRENT_PUBLIC_FACT", Required: true}}} } return base(ctx, model, msgs, tools, opts) }} @@ -160,7 +160,7 @@ func TestProjectSameScopeKeepsCurrentRunToolContext(t *testing.T) { return base(ctx, model, msgs, tools, opts) }} c := &projectRunCtx{fakeRunCtx: &fakeRunCtx{ctx: context.Background(), projectID: "p1", prof: core.AgentProfile{Model: core.ModelRoute{Chat: "test-model"}}, execOutput: "AUTHORIZED_TOOL_RESULT"}, - bundle: extension.ProjectContextBundle{Status: extension.ProjectContextReady, ProjectID: "p1", AuthorizationEpoch: "duty-1", PublicationEpoch: "pub-1", Items: []extension.ProjectContextItem{{SourceID: "fact", Text: "CURRENT_FACT", Required: true}}}} + bundle: extension.ProjectContextBundle{Status: extension.ProjectContextReady, ProjectID: "p1", AuthorizationEpoch: "duty-1", PublicationEpoch: "pub-1", Items: []extension.ProjectContextItem{{SourceID: "fact", Revision: "r1", Hash: "h1", Text: "CURRENT_FACT", Required: true}}}} if err := runner.Execute(c); err != nil { t.Fatal(err) } @@ -193,7 +193,7 @@ func TestSubagentModelRequestUsesProjectContextAndManifest(t *testing.T) { return base(ctx, model, msgs, tools, opts) }} c := &projectRunCtx{fakeRunCtx: &fakeRunCtx{ctx: context.Background(), projectID: "project-1", prof: core.AgentProfile{Model: core.ModelRoute{Chat: "test-model", Lightweight: "test-lite"}}}, - bundle: extension.ProjectContextBundle{Status: extension.ProjectContextReady, ProjectID: "project-1", Items: []extension.ProjectContextItem{{SourceID: "s1", Text: "current project fact", Required: true}}}} + bundle: extension.ProjectContextBundle{Status: extension.ProjectContextReady, ProjectID: "project-1", Items: []extension.ProjectContextItem{{SourceID: "s1", Revision: "r1", Hash: "h1", Text: "current project fact", Required: true}}}} if err := sub.Execute(c); err != nil { t.Fatal(err) } @@ -214,7 +214,7 @@ func TestSubagentSameScopeKeepsNewToolResultAcrossTurns(t *testing.T) { return base(ctx, model, msgs, tools, opts) }} c := &projectRunCtx{fakeRunCtx: &fakeRunCtx{ctx: context.Background(), projectID: "p1", prof: core.AgentProfile{Model: core.ModelRoute{Chat: "test-model", Lightweight: "test-lite"}}, execOutput: "NEW_AUTHORIZED_TOOL"}, - bundle: extension.ProjectContextBundle{Status: extension.ProjectContextReady, ProjectID: "p1", AuthorizationEpoch: "duty-1", PublicationEpoch: "pub-1", Items: []extension.ProjectContextItem{{SourceID: "fact", Text: "CURRENT_FACT", Required: true}}}, + bundle: extension.ProjectContextBundle{Status: extension.ProjectContextReady, ProjectID: "p1", AuthorizationEpoch: "duty-1", PublicationEpoch: "pub-1", Items: []extension.ProjectContextItem{{SourceID: "fact", Revision: "r1", Hash: "h1", Text: "CURRENT_FACT", Required: true}}}, toolList: []core.ToolDesc{{Name: "knowledge_search", Description: "Search", Parameters: json.RawMessage(`{"type":"object"}`)}}, } if err := sub.Execute(c); err != nil { @@ -248,7 +248,7 @@ func TestProjectStreamBlocksRequestExceedingFinalModelBudget(t *testing.T) { called = true return scriptStream(textTurns("bad"), nil)(ctx, model, msgs, tools, opts) } - c := &projectRunCtx{fakeRunCtx: &fakeRunCtx{projectID: "p1"}, bundle: extension.ProjectContextBundle{Status: extension.ProjectContextReady, ProjectID: "p1", Items: []extension.ProjectContextItem{{SourceID: "large", Text: strings.Repeat("x", 11000), Required: true}}}, budgetSettings: core.EffectiveSettings{"models": map[string]any{"chat": map[string]any{"contextWindow": float64(4096), "outputReserve": float64(512), "contextBuffer": float64(512)}}}} + c := &projectRunCtx{fakeRunCtx: &fakeRunCtx{projectID: "p1"}, bundle: extension.ProjectContextBundle{Status: extension.ProjectContextReady, ProjectID: "p1", Items: []extension.ProjectContextItem{{SourceID: "large", Revision: "r1", Hash: "h1", Text: strings.Repeat("x", 11000), Required: true}}}, budgetSettings: core.EffectiveSettings{"models": map[string]any{"chat": map[string]any{"contextWindow": float64(4096), "outputReserve": float64(512), "contextBuffer": float64(512)}}}} for ev := range GuardProjectStream(c, "goal", base)(context.Background(), "test", []agentllm.Message{{Role: agentllm.RoleUser, Content: "goal"}}, nil, agentllm.StreamOptions{}) { if ev.Kind != agentllm.KindError { t.Fatalf("oversized request did not fail: %+v", ev) @@ -335,7 +335,7 @@ func (c *projectSquadCtx) Run() core.Run { return core.Run{ID: "run-squad", Owner: "alice", ProjectID: "project-1"} } func (c *projectSquadCtx) ProjectContext(context.Context, string, int) (extension.ProjectContextBundle, bool, error) { - return extension.ProjectContextBundle{Status: extension.ProjectContextReady, ProjectID: "project-1", AuthorizationEpoch: "duty-1", PublicationEpoch: "pub-1", Items: []extension.ProjectContextItem{{SourceID: "squad-source", Text: "squad project fact", Required: true}}}, true, nil + return extension.ProjectContextBundle{Status: extension.ProjectContextReady, ProjectID: "project-1", AuthorizationEpoch: "duty-1", PublicationEpoch: "pub-1", Items: []extension.ProjectContextItem{{SourceID: "squad-source", Revision: "r1", Hash: "h1", Text: "squad project fact", Required: true}}}, true, nil } func (c *projectSquadCtx) SaveContextManifest(_ context.Context, m extension.ProjectContextManifest) error { c.manifests = append(c.manifests, m) diff --git a/internal/runtime/workflow_compiled_test.go b/internal/runtime/workflow_compiled_test.go index 51b1301..1f7148f 100644 --- a/internal/runtime/workflow_compiled_test.go +++ b/internal/runtime/workflow_compiled_test.go @@ -308,7 +308,7 @@ func (c *projectJudgeCtx) ProjectContext(context.Context, string, int) (extensio if epoch == "" { epoch = "duty-1" } - return extension.ProjectContextBundle{Status: extension.ProjectContextReady, ProjectID: "project-1", AuthorizationEpoch: epoch, PublicationEpoch: "pub-1", Items: []extension.ProjectContextItem{{SourceID: "judge-source", Text: "current project rule", Required: true}}}, true, nil + return extension.ProjectContextBundle{Status: extension.ProjectContextReady, ProjectID: "project-1", AuthorizationEpoch: epoch, PublicationEpoch: "pub-1", Items: []extension.ProjectContextItem{{SourceID: "judge-source", Revision: "r1", Hash: "h1", Text: "current project rule", Required: true}}}, true, nil } func (c *projectJudgeCtx) SaveContextManifest(_ context.Context, m extension.ProjectContextManifest) error { c.manifests = append(c.manifests, m) diff --git a/internal/store/context_manifest.go b/internal/store/context_manifest.go index 779f7b2..48e52a9 100644 --- a/internal/store/context_manifest.go +++ b/internal/store/context_manifest.go @@ -11,12 +11,10 @@ import ( const contextManifestAction = "model.context_manifest" -// ContextManifestScope records the exact trusted Resolve selector separately -// from the public final-input manifest. Bound distinguishes an intentionally -// empty goal from a legacy receipt with no recoverable selector. +// ContextManifestScope records only that the request came from a trusted model +// guard. Raw goals and model output are never persisted in this receipt. type ContextManifestScope struct { - Goal, TargetID string - Bound bool + Bound bool } // AppendContextManifest writes to the trusted receipt table, never to the @@ -50,17 +48,16 @@ func (s *Store) appendContextManifest(owner, targetID string, m extension.Projec } defer tx.Rollback() if m.Delivery != "attempted" { - var attemptedGoal, attemptedTarget string var attemptedBound int - err := tx.QueryRow(s.q(`SELECT resolve_goal,resolve_target_id,scope_bound FROM context_manifests WHERE attempt_id=? AND delivery='attempted' AND owner=? AND target_id=? AND project_id=? AND input_sha256=?`), - m.AttemptID, owner, targetID, m.ProjectID, m.InputSHA256).Scan(&attemptedGoal, &attemptedTarget, &attemptedBound) + err := tx.QueryRow(s.q(`SELECT scope_bound FROM context_manifests WHERE attempt_id=? AND delivery='attempted' AND owner=? AND target_id=? AND project_id=? AND input_sha256=?`), + m.AttemptID, owner, targetID, m.ProjectID, m.InputSHA256).Scan(&attemptedBound) if errors.Is(err, sql.ErrNoRows) { return errors.New("context manifest terminal receipt lacks matching attempt") } if err != nil { return err } - if attemptedGoal != scope.Goal || attemptedTarget != scope.TargetID || (attemptedBound != 0) != scope.Bound { + if (attemptedBound != 0) != scope.Bound { return errors.New("context manifest terminal resolve scope changed") } var terminal int @@ -76,8 +73,8 @@ func (s *Store) appendContextManifest(owner, targetID string, m extension.Projec if scope.Bound { bound = 1 } - _, err = tx.Exec(s.q(`INSERT INTO context_manifests(id,attempt_id,owner,target_id,project_id,input_sha256,delivery,manifest_json,created_at,resolve_goal,resolve_target_id,scope_bound) VALUES(?,?,?,?,?,?,?,?,?,?,?,?)`), - NewID("ctxm"), m.AttemptID, owner, targetID, m.ProjectID, m.InputSHA256, m.Delivery, string(raw), nowISO(), scope.Goal, scope.TargetID, bound) + _, err = tx.Exec(s.q(`INSERT INTO context_manifests(id,attempt_id,owner,target_id,project_id,input_sha256,delivery,manifest_json,created_at,scope_bound) VALUES(?,?,?,?,?,?,?,?,?,?)`), + NewID("ctxm"), m.AttemptID, owner, targetID, m.ProjectID, m.InputSHA256, m.Delivery, string(raw), nowISO(), bound) if err != nil { return fmt.Errorf("insert context manifest: %w", err) } diff --git a/internal/store/context_manifest_test.go b/internal/store/context_manifest_test.go index c4411b8..2259897 100644 --- a/internal/store/context_manifest_test.go +++ b/internal/store/context_manifest_test.go @@ -1,7 +1,10 @@ package store import ( + "bytes" + "os" "path/filepath" + "strings" "testing" "agent-system-backend/pkg/extension" @@ -37,6 +40,132 @@ func TestContextManifestTerminalRequiresMatchingAttempt(t *testing.T) { } } +func TestContextManifestDoesNotPersistWorkflowOutputInResolveScope(t *testing.T) { + st, err := OpenSQLite(filepath.Join(t.TempDir(), "scope-privacy.db")) + if err != nil { + t.Fatal(err) + } + defer st.Close() + const restricted = "OLD_RESTRICTED_BODY" + m := extension.ProjectContextManifest{ProjectID: "project-1", AttemptID: "judge-attempt", InputSHA256: "input-hash", Delivery: "attempted"} + scope := ContextManifestScope{Bound: true} + if err := st.AppendContextManifestWithScope("alice", "run-1", m, scope); err != nil { + t.Fatal(err) + } + m.Delivery = "responded" + if err := st.AppendContextManifestWithScope("alice", "run-1", m, scope); err != nil { + t.Fatal(err) + } + rows, err := st.db.Query(st.q(`SELECT delivery,manifest_json FROM context_manifests WHERE attempt_id=? ORDER BY created_at,id`), m.AttemptID) + if err != nil { + t.Fatal(err) + } + defer rows.Close() + seen := 0 + for rows.Next() { + var delivery, manifest string + if err := rows.Scan(&delivery, &manifest); err != nil { + t.Fatal(err) + } + seen++ + if strings.Contains(manifest, restricted) { + t.Errorf("%s trusted receipt persisted workflow output body", delivery) + } + } + if err := rows.Err(); err != nil { + t.Fatal(err) + } + if seen != 2 { + t.Fatalf("expected attempted and responded receipts, got %d", seen) + } + var rawColumns int + if err := st.db.QueryRow(`SELECT COUNT(*) FROM pragma_table_info('context_manifests') WHERE name IN ('resolve_goal','resolve_target_id')`).Scan(&rawColumns); err != nil || rawColumns != 0 { + t.Fatalf("fresh receipt schema retains raw scope columns: columns=%d err=%v", rawColumns, err) + } +} + +func TestCandidateV34RawScopeColumnsAreScrubbedOnUpgrade(t *testing.T) { + path := filepath.Join(t.TempDir(), "candidate-v34.db") + st, err := OpenSQLite(path) + if err != nil { + t.Fatal(err) + } + m := extension.ProjectContextManifest{ProjectID: "p1", AttemptID: "old-model", InputSHA256: "input", Delivery: "attempted"} + if err := st.AppendContextManifestWithScope("alice", "run-1", m, ContextManifestScope{Bound: true}); err != nil { + t.Fatal(err) + } + for _, query := range []string{ + `DELETE FROM schema_migrations WHERE version=35`, + `ALTER TABLE context_manifests ADD COLUMN resolve_goal TEXT NOT NULL DEFAULT ''`, + `ALTER TABLE context_manifests ADD COLUMN resolve_target_id TEXT NOT NULL DEFAULT ''`, + `UPDATE context_manifests SET resolve_goal='OLD_RESTRICTED_BODY',resolve_target_id='doc-private'`, + } { + if _, err := st.db.Exec(query); err != nil { + t.Fatal(err) + } + } + if err := st.Close(); err != nil { + t.Fatal(err) + } + upgraded, err := OpenSQLite(path) + if err != nil { + t.Fatal(err) + } + defer upgraded.Close() + var columns int + if err := upgraded.db.QueryRow(`SELECT COUNT(*) FROM pragma_table_info('context_manifests') WHERE name IN ('resolve_goal','resolve_target_id')`).Scan(&columns); err != nil || columns != 0 { + t.Fatalf("candidate raw scope columns survived v35: columns=%d err=%v", columns, err) + } + var count int + if err := upgraded.db.QueryRow(`SELECT COUNT(*) FROM context_manifests WHERE attempt_id='old-model'`).Scan(&count); err != nil || count != 1 { + t.Fatalf("trusted manifest disappeared during scrub: count=%d err=%v", count, err) + } + for _, suffix := range []string{"", "-wal"} { + data, err := os.ReadFile(path + suffix) + if err == nil && bytes.Contains(data, []byte("OLD_RESTRICTED_BODY")) { + t.Fatalf("candidate raw scope bytes remain in SQLite %s after upgrade", suffix) + } + } +} + +func TestCandidateV34PostgresRawScopeScrubAndFreshSchema(t *testing.T) { + st := openTestStore(t) + if st.Dialect() != "postgres" { + t.Skip("isolated PostgreSQL DSN not configured") + } + checkColumns := func(want int) { + t.Helper() + var count int + if err := st.db.QueryRow(`SELECT COUNT(*) FROM information_schema.columns WHERE table_schema=current_schema() AND table_name='context_manifests' AND column_name IN ('resolve_goal','resolve_target_id')`).Scan(&count); err != nil || count != want { + t.Fatalf("raw scope columns=%d want=%d err=%v", count, want, err) + } + } + checkColumns(0) + m := extension.ProjectContextManifest{ProjectID: "p1", AttemptID: "candidate-model", InputSHA256: "input", Delivery: "attempted"} + if err := st.AppendContextManifestWithScope("alice", "run-1", m, ContextManifestScope{Bound: true}); err != nil { + t.Fatal(err) + } + for _, query := range []string{ + `DELETE FROM schema_migrations WHERE version=35`, + `ALTER TABLE context_manifests ADD COLUMN resolve_goal TEXT NOT NULL DEFAULT ''`, + `ALTER TABLE context_manifests ADD COLUMN resolve_target_id TEXT NOT NULL DEFAULT ''`, + `UPDATE context_manifests SET resolve_goal='OLD_RESTRICTED_BODY',resolve_target_id='doc-private'`, + } { + if _, err := st.db.Exec(query); err != nil { + t.Fatal(err) + } + } + checkColumns(2) + if err := st.migrate(); err != nil { + t.Fatal(err) + } + checkColumns(0) + var count int + if err := st.db.QueryRow(`SELECT COUNT(*) FROM context_manifests WHERE attempt_id='candidate-model'`).Scan(&count); err != nil || count != 1 { + t.Fatalf("trusted manifest lost during PG scrub: count=%d err=%v", count, err) + } +} + func TestContextManifestTerminalCannotChangeTrustedResolveScope(t *testing.T) { st, err := OpenSQLite(filepath.Join(t.TempDir(), "scope.db")) if err != nil { @@ -44,14 +173,11 @@ func TestContextManifestTerminalCannotChangeTrustedResolveScope(t *testing.T) { } defer st.Close() m := extension.ProjectContextManifest{ProjectID: "p1", AttemptID: "model-1", WorkflowStepAttemptID: "engine/draft/1", InputSHA256: "input-hash", Delivery: "attempted"} - scope := ContextManifestScope{Goal: "draft from A", Bound: true} + scope := ContextManifestScope{Bound: true} if err := st.AppendContextManifestWithScope("alice", "run-1", m, scope); err != nil { t.Fatal(err) } m.Delivery = "responded" - if err := st.AppendContextManifestWithScope("alice", "run-1", m, ContextManifestScope{Goal: "draft from B", Bound: true}); err == nil { - t.Fatal("terminal receipt changed its trusted Resolve goal") - } if err := st.AppendContextManifest("alice", "run-1", m); err == nil { t.Fatal("terminal receipt dropped its trusted Resolve scope") } diff --git a/internal/store/migrate.go b/internal/store/migrate.go index bf6fab9..b9bb9af 100644 --- a/internal/store/migrate.go +++ b/internal/store/migrate.go @@ -3,6 +3,7 @@ package store import ( "context" "database/sql" + "errors" "fmt" "strings" "time" @@ -871,14 +872,18 @@ func init() { }, migration{version: 34, name: "context_manifest_resolve_scope", up: map[string][]string{ - "sqlite": {`ALTER TABLE context_manifests ADD COLUMN resolve_goal TEXT NOT NULL DEFAULT ''`, `ALTER TABLE context_manifests ADD COLUMN resolve_target_id TEXT NOT NULL DEFAULT ''`, `ALTER TABLE context_manifests ADD COLUMN scope_bound INTEGER NOT NULL DEFAULT 0`}, - "postgres": {`ALTER TABLE context_manifests ADD COLUMN resolve_goal TEXT NOT NULL DEFAULT ''`, `ALTER TABLE context_manifests ADD COLUMN resolve_target_id TEXT NOT NULL DEFAULT ''`, `ALTER TABLE context_manifests ADD COLUMN scope_bound INTEGER NOT NULL DEFAULT 0`}, + "sqlite": {`ALTER TABLE context_manifests ADD COLUMN scope_bound INTEGER NOT NULL DEFAULT 0`}, + "postgres": {`ALTER TABLE context_manifests ADD COLUMN scope_bound INTEGER NOT NULL DEFAULT 0`}, }, down: map[string][]string{ - "sqlite": {`ALTER TABLE context_manifests DROP COLUMN scope_bound`, `ALTER TABLE context_manifests DROP COLUMN resolve_target_id`, `ALTER TABLE context_manifests DROP COLUMN resolve_goal`}, - "postgres": {`ALTER TABLE context_manifests DROP COLUMN scope_bound`, `ALTER TABLE context_manifests DROP COLUMN resolve_target_id`, `ALTER TABLE context_manifests DROP COLUMN resolve_goal`}, + "sqlite": {`ALTER TABLE context_manifests DROP COLUMN scope_bound`}, + "postgres": {`ALTER TABLE context_manifests DROP COLUMN scope_bound`}, }, }, + migration{version: 35, name: "scrub_candidate_context_scope_text", + up: map[string][]string{"sqlite": {`SELECT 1`}, "postgres": {`SELECT 1`}}, + down: map[string][]string{"sqlite": {`SELECT 1`}, "postgres": {`SELECT 1`}}, + }, ) } @@ -971,10 +976,35 @@ func (s *Store) migrate() error { if s.dialect.name() == "postgres" { return s.migratePG() } + var candidateRawColumns int + if err := s.db.QueryRow(`SELECT COUNT(*) FROM pragma_table_info('context_manifests') WHERE name IN ('resolve_goal','resolve_target_id')`).Scan(&candidateRawColumns); err != nil { + return err + } // SQLite:_txlock=immediate(OpenSQLite DSN)保证迁移事务在 BEGIN 即取写锁, // 跨进程迁移者按 busy_timeout 排队;后续版本读取发生在同一事务内, // 前一个迁移者提交后本进程看到的已是完整账本(无部分版本可见)。 - return s.migratePass() + if err := s.migratePass(); err != nil { + return err + } + if candidateRawColumns > 0 { + // The prior candidate schema may have stored full prompts. VACUUM is + // outside the DDL transaction; it compacts the isolated SQLite file + // after v35 drops those columns. Refuse to serve if compaction fails. + if _, err := s.db.Exec(`PRAGMA secure_delete=ON`); err != nil { + return fmt.Errorf("candidate context scrub: %w", err) + } + if _, err := s.db.Exec(`VACUUM`); err != nil { + return fmt.Errorf("candidate context scrub: %w", err) + } + var busy, logFrames, checkpointed int + if err := s.db.QueryRow(`PRAGMA wal_checkpoint(TRUNCATE)`).Scan(&busy, &logFrames, &checkpointed); err != nil { + return fmt.Errorf("candidate context scrub checkpoint: %w", err) + } + if busy != 0 { + return errors.New("candidate context scrub checkpoint remains busy") + } + } + return nil } // migratePG 以会话级咨询锁串行化并发迁移者(KTD8):锁在专用连接上获取, @@ -1052,6 +1082,11 @@ func (s *Store) migratePass() error { if applied[m.version] { continue } + if m.version == 35 { + if err := scrubCandidateContextScope(tx, s.dialect.name()); err != nil { + return fmt.Errorf("migrate v35: %w", err) + } + } stmts := m.up[s.dialect.name()] if len(stmts) == 0 { return fmt.Errorf("migrate: v%d %s 缺少 %s 方言", m.version, m.name, s.dialect.name()) @@ -1073,6 +1108,34 @@ func (s *Store) migratePass() error { return tx.Commit() } +// Candidate v34 was never released, but isolated test databases may already +// contain raw Resolve goals. Fresh v34 creates no such columns; v35 removes +// them conditionally from the earlier candidate schema before serving. +func scrubCandidateContextScope(tx *sql.Tx, dialect string) error { + for _, column := range []string{"resolve_goal", "resolve_target_id"} { + var exists int + var err error + if dialect == "sqlite" { + err = tx.QueryRow(`SELECT COUNT(*) FROM pragma_table_info('context_manifests') WHERE name=?`, column).Scan(&exists) + } else { + err = tx.QueryRow(`SELECT COUNT(*) FROM information_schema.columns WHERE table_schema=current_schema() AND table_name='context_manifests' AND column_name=$1`, column).Scan(&exists) + } + if err != nil { + return err + } + if exists == 0 { + continue + } + if _, err := tx.Exec(`UPDATE context_manifests SET ` + column + `=''`); err != nil { + return err + } + if _, err := tx.Exec(`ALTER TABLE context_manifests DROP COLUMN ` + column); err != nil { + return err + } + } + return nil +} + func latestMigrationVersion() int { if len(migrations) == 0 { return 0 diff --git a/internal/store/migrate_compat.go b/internal/store/migrate_compat.go index 7b17ce6..a3addac 100644 --- a/internal/store/migrate_compat.go +++ b/internal/store/migrate_compat.go @@ -39,6 +39,7 @@ var migrationBreaksOldReaders = map[int]bool{ 32: false, // trusted_context_manifests:独立可信回执表,旧读者不受影响 33: false, // workflow_step_sources:新增逐步骤来源绑定表,旧读者不受影响 34: false, // context_manifest_resolve_scope:可信回执增加原Resolve范围,旧读者不受影响 + 35: false, // scrub_candidate_context_scope_text:候选v34原文列清理,旧读者不受影响 } // SchemaTarget 是本二进制会迁移到的 schema 版本。 diff --git a/internal/store/workflow_step_source.go b/internal/store/workflow_step_source.go index e5bbfad..7806c6b 100644 --- a/internal/store/workflow_step_source.go +++ b/internal/store/workflow_step_source.go @@ -40,17 +40,18 @@ func (s *Store) BindWorkflowStepSource(owner, runID, stepID, stepAttemptID, kind if err != nil || run.Owner != owner || run.ProjectID == "" { return WorkflowStepSource{}, ErrNotFound } - rows, err := s.db.Query(s.q(`SELECT created_at,manifest_json,resolve_target_id,scope_bound FROM context_manifests WHERE owner=? AND target_id=? AND delivery='responded'`), owner, runID) + rows, err := s.db.Query(s.q(`SELECT created_at,delivery,manifest_json,scope_bound FROM context_manifests WHERE owner=? AND target_id=?`), owner, runID) if err != nil { return WorkflowStepSource{}, err } defer rows.Close() var latest extension.ProjectContextManifest var latestAt time.Time + states := map[string]uint8{} for rows.Next() { - var atRaw, raw, resolveTarget string + var atRaw, delivery, raw string var scopeBound int - if err := rows.Scan(&atRaw, &raw, &resolveTarget, &scopeBound); err != nil { + if err := rows.Scan(&atRaw, &delivery, &raw, &scopeBound); err != nil { return WorkflowStepSource{}, err } at, err := time.Parse(time.RFC3339Nano, atRaw) @@ -67,7 +68,19 @@ func (s *Store) BindWorkflowStepSource(owner, runID, stepID, stepAttemptID, kind if m.WorkflowStepAttemptID != stepAttemptID { continue } - if scopeBound != 1 || resolveTarget != "" { + switch delivery { + case "attempted": + states[m.AttemptID] |= 1 + continue + case "unknown": + states[m.AttemptID] |= 4 + continue + case "responded": + states[m.AttemptID] |= 2 + default: + return WorkflowStepSource{}, errors.New("workflow model receipt has unknown delivery state") + } + if scopeBound != 1 { return WorkflowStepSource{}, errors.New("workflow step model receipt lacks trusted original Resolve scope") } if m.ProjectID != run.ProjectID || m.AuthorizationEpoch == "" || m.PublicationEpoch == "" || m.AttemptID == "" { @@ -83,6 +96,11 @@ func (s *Store) BindWorkflowStepSource(owner, runID, stepID, stepAttemptID, kind if err := rows.Err(); err != nil { return WorkflowStepSource{}, err } + for _, state := range states { + if state != 3 { + return WorkflowStepSource{}, errors.New("workflow step has an unsettled or unknown model attempt") + } + } if latestAt.IsZero() { return WorkflowStepSource{}, errors.New("workflow prompt output has no responded model receipt") } @@ -108,16 +126,17 @@ func (s *Store) RespondedWorkflowStepManifests(owner, runID, stepAttemptID strin if owner == "" || runID == "" || stepAttemptID == "" { return nil, errors.New("workflow receipt scope is incomplete") } - rows, err := s.db.Query(s.q(`SELECT manifest_json,resolve_goal,resolve_target_id,scope_bound FROM context_manifests WHERE owner=? AND target_id=? AND delivery='responded' ORDER BY created_at,id`), owner, runID) + rows, err := s.db.Query(s.q(`SELECT delivery,manifest_json,scope_bound FROM context_manifests WHERE owner=? AND target_id=? ORDER BY created_at,id`), owner, runID) if err != nil { return nil, err } defer rows.Close() var out []ScopedContextManifest + states := map[string]uint8{} for rows.Next() { - var raw, goal, target string + var delivery, raw string var bound int - if err := rows.Scan(&raw, &goal, &target, &bound); err != nil { + if err := rows.Scan(&delivery, &raw, &bound); err != nil { return nil, err } var manifest extension.ProjectContextManifest @@ -127,11 +146,28 @@ func (s *Store) RespondedWorkflowStepManifests(owner, runID, stepAttemptID strin if manifest.WorkflowStepAttemptID != stepAttemptID { continue } - out = append(out, ScopedContextManifest{Manifest: manifest, Scope: ContextManifestScope{Goal: goal, TargetID: target, Bound: bound == 1}}) + switch delivery { + case "attempted": + states[manifest.AttemptID] |= 1 + continue + case "unknown": + states[manifest.AttemptID] |= 4 + continue + case "responded": + states[manifest.AttemptID] |= 2 + default: + return nil, errors.New("workflow model receipt has unknown delivery state") + } + out = append(out, ScopedContextManifest{Manifest: manifest, Scope: ContextManifestScope{Bound: bound == 1}}) } if err := rows.Err(); err != nil { return nil, err } + for _, state := range states { + if state != 3 { + return nil, errors.New("workflow step has an unsettled or unknown model attempt") + } + } if len(out) == 0 { return nil, ErrNotFound } diff --git a/internal/store/workflow_step_source_test.go b/internal/store/workflow_step_source_test.go index f58d9f7..50eed31 100644 --- a/internal/store/workflow_step_source_test.go +++ b/internal/store/workflow_step_source_test.go @@ -30,7 +30,7 @@ func TestWorkflowStepSourceRequiresRespondedManifestAndExactRunStep(t *testing.T } started := time.Now().Add(-time.Second) m := extension.ProjectContextManifest{ProjectID: p.ID, AttemptID: "attempt-1", WorkflowStepAttemptID: "engine-run/draft/1", Delivery: "attempted", InputSHA256: "input", AuthorizationEpoch: "duty-1", PublicationEpoch: "pub-1", Watermark: "w1"} - scope := ContextManifestScope{Goal: "draft report", Bound: true} + scope := ContextManifestScope{Bound: true} if err := st.AppendContextManifestWithScope("alice", string(r.ID), m, scope); err != nil { t.Fatal(err) } @@ -79,6 +79,21 @@ func TestWorkflowStepSourceRequiresRespondedManifestAndExactRunStep(t *testing.T if _, err := st.BindWorkflowStepSource("alice", string(r.ID), "legacy", "engine-run/legacy/1", "prompt", started, []byte("legacy body")); err == nil { t.Fatal("model receipt without bound original Resolve scope became a workflow source") } + unknown := m + unknown.AttemptID, unknown.Delivery = "unknown-model-attempt", "attempted" + if err := st.AppendContextManifestWithScope("alice", string(r.ID), unknown, scope); err != nil { + t.Fatal(err) + } + unknown.Delivery = "unknown" + if err := st.AppendContextManifestWithScope("alice", string(r.ID), unknown, scope); err != nil { + t.Fatal(err) + } + if _, err := st.RespondedWorkflowStepManifests("alice", string(r.ID), "engine-run/draft/1"); err == nil { + t.Fatal("unknown model attempt hidden by earlier responded attempt") + } + if _, err := st.BindWorkflowStepSource("alice", string(r.ID), "draft", "engine-run/draft/1", "prompt", started, []byte("old body")); err == nil { + t.Fatal("unknown model attempt rebound completed step") + } if err := st.UnbindWorkflowStepSource("alice", string(r.ID), "draft"); err != nil { t.Fatal(err) } diff --git a/internal/wfcompile/host.go b/internal/wfcompile/host.go index b0ca13a..12a57da 100644 --- a/internal/wfcompile/host.go +++ b/internal/wfcompile/host.go @@ -98,6 +98,17 @@ type hostRunner struct { } func (r *hostRunner) Run(ctx context.Context, step squad.IntentStep) (*squad.ExecResult, error) { + if r.rc.Run().ProjectID != "" { + guard, ok := r.rc.(ProjectStepProvenance) + if !ok { + return &squad.ExecResult{Success: false, ErrorReason: "project workflow source guard unavailable"}, nil + } + wsDir := filepath.Dir(filepath.Dir(step.WorkDir)) + strict := step.IntentID == IntentTool || step.IntentID == IntentArtifact || step.IntentID == IntentScript || step.IntentID == IntentCritic + if err := verifyPriorWorkflowOutputs(ctx, guard, wsDir, r.subOf, r.stepKinds, strict); err != nil { + return &squad.ExecResult{Success: false, ErrorReason: err.Error()}, nil + } + } var content string var fields map[string]string var err error diff --git a/internal/wfcompile/project_backend.go b/internal/wfcompile/project_backend.go index 47e2e1c..08eb01a 100644 --- a/internal/wfcompile/project_backend.go +++ b/internal/wfcompile/project_backend.go @@ -2,6 +2,7 @@ package wfcompile import ( "context" + "path/filepath" "agent-squad/squad" "agent-system-backend/internal/projectcontext" @@ -10,7 +11,7 @@ import ( // projectStepBackends carries the engine-generated execution ID through the // executor's request context into each model manifest. This associates a // responded model request with exactly one workflow step attempt. -func projectStepBackends(base *squad.BackendResolver) *squad.BackendResolver { +func projectStepBackends(base *squad.BackendResolver, rc ProjectStepProvenance, subOf, kinds map[string]string) *squad.BackendResolver { wrapped := squad.NewBackendResolver() for _, name := range base.ListBackends() { backendName := name @@ -19,13 +20,17 @@ func projectStepBackends(base *squad.BackendResolver) *squad.BackendResolver { if err != nil { return nil, err } - return &projectStepExecutor{next: backend}, nil + return &projectStepExecutor{next: backend, rc: rc, subOf: subOf, kinds: kinds}, nil }) } return wrapped } -type projectStepExecutor struct{ next squad.Executor } +type projectStepExecutor struct { + next squad.Executor + rc ProjectStepProvenance + subOf, kinds map[string]string +} func (e *projectStepExecutor) Name() string { return e.next.Name() } @@ -33,5 +38,11 @@ func (e *projectStepExecutor) Execute(ctx context.Context, req squad.ExecRequest if req.ExecutionID == "" { return &squad.ExecResult{Success: false, ErrorReason: "project workflow step attempt unavailable"}, nil } + if e.rc != nil { + wsDir := filepath.Dir(filepath.Dir(req.WorkDir)) + if err := verifyPriorWorkflowOutputs(ctx, e.rc, wsDir, e.subOf, e.kinds, true); err != nil { + return &squad.ExecResult{Success: false, ErrorReason: err.Error()}, nil + } + } return e.next.Execute(projectcontext.WithWorkflowStepAttempt(ctx, req.ExecutionID), req) } diff --git a/internal/wfcompile/project_sources.go b/internal/wfcompile/project_sources.go index 0c971ab..c191f94 100644 --- a/internal/wfcompile/project_sources.go +++ b/internal/wfcompile/project_sources.go @@ -3,6 +3,7 @@ package wfcompile import ( "context" "errors" + "fmt" "os" "path/filepath" "sync" @@ -11,6 +12,41 @@ import ( "agent-squad/squad" ) +// verifyPriorWorkflowOutputs is shared by model and host steps. The engine's +// task brief/blackboard/handoff can carry earlier output as a fresh user +// prompt, so every completed machine step must have a current source proof. +func verifyPriorWorkflowOutputs(ctx context.Context, rc ProjectStepProvenance, wsDir string, subOf, kinds map[string]string, modelSend bool) error { + state, err := (squad.FileStore{}).LoadRun(wsDir) + if err != nil { + return fmt.Errorf("project workflow state unavailable: %w", err) + } + for _, step := range state.Plan.Workflow.Steps { + if step.Status != "completed" { + continue + } + switch kinds[step.ID] { + case "prompt": + sub := subOf[step.ID] + if sub == "" || step.AttemptID == "" { + return fmt.Errorf("project workflow step %s lacks source identity", step.ID) + } + data, err := os.ReadFile(filepath.Join(wsDir, filepath.FromSlash(sub), "output.md")) + if err != nil { + return fmt.Errorf("project workflow step %s output unavailable: %w", step.ID, err) + } + if err := rc.VerifyProjectStepOutput(ctx, step.ID, step.AttemptID, "prompt", data); err != nil { + return fmt.Errorf("project workflow step %s output source changed: %w", step.ID, err) + } + case "hitl", "critic": // human input or a verdict whose prompt source was checked above + default: + if modelSend { + return fmt.Errorf("project workflow step %s has unproven machine output", step.ID) + } + } + } + return nil +} + // completedStepAttempt comes from the squad engine's persisted state, not a // model response or plugin argument. It identifies the exact execution that // produced output.md, including after a workflow resumes. diff --git a/internal/wfcompile/run.go b/internal/wfcompile/run.go index 8799e13..41c011b 100644 --- a/internal/wfcompile/run.go +++ b/internal/wfcompile/run.go @@ -80,6 +80,9 @@ func RunCompiled(rc Host, deps Deps, launch workflow.Launch, vars workflow.Vars, for _, step := range def.Steps { stepKinds[step.ID] = step.Kind } + for _, pair := range compiled.Critics { + stepKinds[pair.EvalID] = "critic" + } if rc.Run().ProjectID != "" { if _, ok := rc.(ProjectStepProvenance); !ok { return Outcome{}, fmt.Errorf("项目工作流缺少可信步骤产物绑定能力") @@ -97,7 +100,7 @@ func RunCompiled(rc Host, deps Deps, launch workflow.Launch, vars workflow.Vars, backends = squad.NewBackendResolver() } if rc.Run().ProjectID != "" { - backends = projectStepBackends(backends) + backends = projectStepBackends(backends, rc.(ProjectStepProvenance), compiled.SubOf, stepKinds) } runner := &hostRunner{ rc: rc, deps: deps, scripts: launch.Scripts, subOf: compiled.SubOf, diff --git a/internal/wfcompile/wfcompile_test.go b/internal/wfcompile/wfcompile_test.go index a9a57e2..7ec0bfa 100644 --- a/internal/wfcompile/wfcompile_test.go +++ b/internal/wfcompile/wfcompile_test.go @@ -476,7 +476,7 @@ func TestProjectStepBackendCarriesEngineAttempt(t *testing.T) { probe := &stepContextProbe{} base := squad.NewBackendResolver() base.RegisterInstance("probe", probe) - backend, _, err := projectStepBackends(base).Resolve(squad.RoleSpec{Role: "writer", Backend: "probe"}, "") + backend, _, err := projectStepBackends(base, nil, nil, nil).Resolve(squad.RoleSpec{Role: "writer", Backend: "probe"}, "") if err != nil { t.Fatal(err) } @@ -563,11 +563,24 @@ func TestProjectToolCriticRejectsUnprovenOutputBeforeModel(t *testing.T) { return verdictStream(verdictPass)(ctx, model, messages, tools, opts) } _, err := RunCompiled(f, Deps{DataDir: t.TempDir(), JudgeStream: judge}, workflow.Launch{Def: def}, workflow.Vars{Input: "read"}, RunOptions{}) - if err == nil || called || !strings.Contains(err.Error(), "产物依据不可用") { + if err == nil || called || (!strings.Contains(err.Error(), "产物依据不可用") && !strings.Contains(err.Error(), "unproven machine output")) { t.Fatalf("unproven tool output reached judge: err=%v called=%v", err, called) } } +func TestProjectModelStepRejectsEarlierToolOutputInTaskBrief(t *testing.T) { + f := &projectSourceHost{fakeHost: &fakeHost{toolNames: []string{"time_now"}}} + be := &seqBackend{texts: []string{"should not run"}} + def := core.WorkflowDef{ID: "tool-then-model", Title: "Tool then model", Steps: []core.WorkflowStep{ + {ID: "get", Title: "Read", Kind: "tool", Tool: "time_now"}, + {ID: "draft", Title: "Draft", Kind: "prompt", Agent: "writer", Prompt: "use {{steps.get.output}}"}, + }} + _, err := RunCompiled(f, Deps{DataDir: t.TempDir(), Backends: seqResolver(t, be), DefaultBackend: "seq"}, workflow.Launch{Def: def}, workflow.Vars{Input: "write"}, RunOptions{}) + if err == nil || be.calls != 0 || !strings.Contains(err.Error(), "unproven machine output") { + t.Fatalf("tool output entered project model step: err=%v modelCalls=%d", err, be.calls) + } +} + // critic 预算耗尽仍不通过 → 工作流失败(P4 fail-closed)。 func TestRunCompiledCriticExhaustedFails(t *testing.T) { def := parseDef(t, demoBriefJSON) diff --git a/pkg/extension/project_context.go b/pkg/extension/project_context.go index 2010b0c..9c207bb 100644 --- a/pkg/extension/project_context.go +++ b/pkg/extension/project_context.go @@ -83,6 +83,27 @@ type ProjectContextProvider interface { ReadEvidence(context.Context, ProjectEvidenceRequest) (ProjectEvidence, error) } +// ProjectContextSourceRecheckRequest names only sources already present in a +// trusted model-input receipt. Scope identity is rebuilt by Backend from the +// persisted run; no prompt, output body, or caller-chosen owner is accepted. +type ProjectContextSourceRecheckRequest struct { + Scope ProjectContextRequest + Included []ProjectContextManifestEntry + OfferedReferences []ProjectEvidenceRef + ReadEvidence []ProjectContextManifestEntry +} + +// ProjectContextSourceRecheckResult contains current authorized source text +// only for transient Backend hash verification. It is never stored as a new +// model receipt. Every requested source/ref must be present in Bundle. +type ProjectContextSourceRecheckResult struct{ Bundle ProjectContextBundle } + +// ProjectContextSourceRechecker is an optional method of the same registered +// ProjectContextProvider. Project workflow reuse fails closed without it. +type ProjectContextSourceRechecker interface { + Recheck(context.Context, ProjectContextSourceRecheckRequest) (ProjectContextSourceRecheckResult, error) +} + // WorkCompletionRequest gives the generation goal and optional target. It has // no owner/project field; both are resolved from the dispatch host's stored // conversation immediately before the model call. diff --git a/pkg/extension/surface.txt b/pkg/extension/surface.txt index c3a08ec..e8d69ad 100644 --- a/pkg/extension/surface.txt +++ b/pkg/extension/surface.txt @@ -46,6 +46,10 @@ ProjectContextReady ProjectContextRegistry ProjectContextRegistry.RegisterProjectContextProvider ProjectContextRequest +ProjectContextSourceRecheckRequest +ProjectContextSourceRecheckResult +ProjectContextSourceRechecker +ProjectContextSourceRechecker.Recheck ProjectContextStatus ProjectEvidence ProjectEvidenceRef From 63ebc85695790e7d0c2b758e5c0ed1b4adaec008 Mon Sep 17 00:00:00 2001 From: weichen Date: Wed, 14 Oct 2026 05:41:37 +0000 Subject: [PATCH 10/15] Make PostgreSQL project audit cutoffs commit ordered Why: PostgreSQL sequence IDs could commit out of order and make fixed project audit cursors permanently skip a late row. Evidence: SELF PG cross-page RED, CACHE 10 counterexample, configuration drift RED/GREEN, and pre-commit Go, PG migration, and race receipts are recorded in the W9 task card. Fixed-HEAD checks follow this commit. Impact: Guard implicit command audit ID allocation with a transaction advisory lock; reject unsafe sequence/default drift; preserve explicit-ID storage migration; report committed-prefix only for verified project pages. Refs: Approved W9 PostgreSQL cutoff adjustment r1; W9 Backend task card. --- .task/feature-rdm-two-trees-project-audit.md | 12 +- cmd/storage-migrate/main.go | 24 +- internal/app/project_audit.go | 6 +- internal/store/audit.go | 25 + internal/store/migrate.go | 53 ++ internal/store/migrate_compat.go | 1 + internal/store/project_audit_sequence_test.go | 500 ++++++++++++++++++ internal/store/project_audit_test.go | 69 ++- pkg/extension/extension.go | 4 +- pkg/extension/project_audit.go | 7 +- 10 files changed, 661 insertions(+), 40 deletions(-) create mode 100644 internal/store/project_audit_sequence_test.go diff --git a/.task/feature-rdm-two-trees-project-audit.md b/.task/feature-rdm-two-trees-project-audit.md index f72e33b..6eb2de3 100644 --- a/.task/feature-rdm-two-trees-project-audit.md +++ b/.task/feature-rdm-two-trees-project-audit.md @@ -5,6 +5,7 @@ - R0: Backend authoritative `origin/main` `5d5fc9891c0eb8994545ccb8b740535d9b99fb0e`; main was clean and equal to the fetched remote before this worktree was created. Core gitlink in this worktree is `5773e74086791731059730bd3726dc8c9cd1381f`. - Startup receipt: `/tmp/backend-w9-precheck-20261014-01.json` (SELF, exit 2, `SYNC_REQUIRED` for the separate Dev checkout `feature/rdm-increment-acceptance-note`, six commits behind Dev `origin/main`). This Backend task uses the independent-development path in Backend `AGENTS.md`; it does not write that Dev checkout or its task card. The receipt is preserved as a failure, not relabeled READY. - Approved affected design: `rdm-two-trees-stage6-audit-link-design-adjustment-r1.md`, SHA256 `385d9d7570217ddcce9fe60a60c99dc58c9a4875ad790584a724d09a210a6e52`; user approval in the task session covers this local Backend increment, not tag publication or Stage 7 acceptance. +- Approved PostgreSQL cutoff adjustment: `rdm-two-trees-stage6-w9-pg-audit-cutoff-adjustment-r1.md`, SHA256 `2357b715a7c8f3451b34cc25a578d655e8a48f3f26d9cf7561ea7a13998ca857`; scope is isolated Backend implementation/testing, with no real database migration or release authorization. ## Implemented local slice @@ -16,14 +17,19 @@ - Independent review of `a8fa2fe` found two P2 security defects: clock-based operation IDs could collide; and `NewScopedHost(owner,"rdm")` let another compiled plugin self-report the publisher's plugin ID. The new local increment uses 192 bits from `crypto/rand` for each host-generated operation ID. Both zero-offset and explicit-offset publication require a unique committed row for owner/project/operation; a historical duplicate rejects publication, including attempts to link another private command. Verifier registration returns a private `ProjectAuditPublisherFactory` for the registering plugin's background outbox; a generic scoped Host has no publisher method. Fixed-clock concurrent IDs, forged params, ambiguous private offset, forged scoped Host, and legitimate registered publisher each have focused tests. - The minted publisher exposes only `ProjectAuditPublisher`, not the full owner-scoped `Host`; tests reject attempts to use `NewScopedHost` with a self-reported `rdm` string as publisher authority. A publisher is a registration-time capability, while `IdempotencyKey` remains the separate retry identity. - Exact-source linked reads do not enumerate hidden links or reveal raw payload, private conversation ID, or content. Body disclosure and a broad project link list are not implemented; the public coverage reports the narrower capability. +- A PostgreSQL-only v33 default function now takes a table-specific transaction advisory lock before assigning `command_audit.id`, making supported implicit-ID writes commit in source-offset order. Migration and each PG append/project page verify CACHE 1, ascending non-cyclic positive IDs, the exact guarded default expression, and the next ID above the committed maximum; unsafe drift fails closed. SQLite remains single-writer. The owner-only project page reports `committed-prefix` only after the runtime check. Explicit online IDs and sequence maintenance remain prohibited; offline `storage-migrate` advances the owned sequence after copying IDs. ## Verification and remaining work - The session saw RED for the missing store and host interfaces, unscoped cutoff, cross-member private disclosure, and conversation-project reassignment before the respective fixes. These RED outputs were not captured by `make evidence`, so this card does not use them as formal evidence. - SELF receipt `/tmp/backend-w9-go-test-20261014-01.json`: `go test ./...`, exit 0, run on the pre-commit worktree based on R0. - SELF receipt `/tmp/backend-w9-build-20261014-01.json`: `CGO_ENABLED=0 go build ./cmd/agent-system`, exit 0, run on that pre-commit tree. Generated local binary removed after the build. -- SELF PostgreSQL receipt `/tmp/backend-w9-pg-cutoff-20261014-01.json`: isolated PostgreSQL 18.6 instance on port 55442 reproduced a low-ID transaction committing after a higher visible ID. `/tmp/backend-w9-pg-link-20261014-02.json` verifies the new link table, retry/conflict behavior, and the cutoff counterexample against isolated schemas. Both exited 0. The current PG cutoff means only `offset-upper-bound-only`; it is **not** a fixed historical visibility snapshot or safe incremental cursor. +- Historical SELF PostgreSQL receipt `/tmp/backend-w9-pg-cutoff-20261014-01.json`: isolated PostgreSQL 18.6 instance on port 55442 reproduced a low-ID transaction committing after a higher visible ID. `/tmp/backend-w9-pg-link-20261014-02.json` verifies the link table, retry/conflict behavior, and the pre-v33 cutoff counterexample. These receipts describe the old `offset-upper-bound-only` candidate, not the new v33 guarantee. - SELF PostgreSQL receipt `/tmp/backend-w9-pg-migrate-20261014-01.json`: isolated SQLite→PostgreSQL migration preserved one linked receipt and advanced its target sequence; the same migration ID replayed idempotently. The temporary PG instance was stopped and removed after verification. - SELF PostgreSQL receipt `/tmp/backend-w9-pg-operation-20261014-01.json`: a fresh isolated PG instance reran the link lookup, idempotency/conflict, and low-ID late-commit checks after trusted operation-offset resolution was added. The instance was stopped and removed. -- Integration constraint: this branch uses schema v31 (audit attribution) and v32 (links), while the independent W6 Backend branch also has a local v31. Neither branch has consumed the other's unmerged work. After the first branch lands, the later branch owner must sync the new main, renumber its unpublished migrations without rewriting an already released version, and rerun fresh/old SQLite and PG migration plus combined regression on the actual merged source. These isolated tests do not prove W6+W9 compatibility. -- Required next W9 work: add a committed-order waterline or materialize the authorized ID set at the first read, then prove with PG low-ID late commits that history and cross-page incremental reads neither omit nor duplicate operations. The Backend project-event source, actual RDM committed-event verifier/outbox consumer, run ID propagation from command dispatch, model/tool/background receipts, body/detail disclosure, broad authorized list pagination, history/export, and combined product verification remain unfinished. A domain commit followed by a lost Backend audit append leaves the link pending; recovery must retain and reconcile that unknown state without claiming a host receipt. The exact-link slice is not Stage 7 audit acceptance. +- New PG cross-page RED receipt `/tmp/backend-w9-pg-crosspage-red-20261014-01.json`: before v33, page 1 returned offsets 4 and 2, page 2 returned 1 after offset 3 committed, and the paged view omitted 3. The same test now passes with v33. Isolated PG tests also cover CACHE 10 counterexample/rejection, CACHE 1, sequence rewind into a vacant low ID, down/up with unchanged link IDs, advisory-lock timeout and rollback gaps, and SQLite→PG explicit-ID migration. +- Historical pre-commit SELF load receipt `/tmp/backend-w9-pg-load-precommit-20261014-01.json` captured an initial v33 variant whose unlocked status function later produced false `cutoff unsafe` errors under concurrency; it is **not** valid performance acceptance evidence. The corrected status check reads committed MAX before non-MVCC sequence state and does not lock readers; the default ID allocator alone holds the transaction advisory lock and rechecks after `nextval`. `TestProjectAuditConcurrentReadWriteNeverReportsFalseDrift` and repeated PG load/cross-page runs protect that ordering. Three-condition pre-commit SELF raw receipts: `/tmp/backend-w9-pg-load-readlock-precommit-20261014-01.json` uses a test-only locked status wrapper, while `/tmp/backend-w9-pg-load-unlocked2-precommit-20261014-01.json` uses the final MAX-first unlocked status; each run also logs its v32 direct-INSERT baseline, PostgreSQL 18.6 version, 192 writes per mode/8 workers, elapsed time, p95, and throughput. The wrapper adds overhead and all measurements vary with hardware/load; no performance threshold has been approved, so they are Stage 6 cost evidence only. +- Pre-commit SELF `/tmp/backend-w9-default-drift-red-20261014-01.json` showed that a replacement default containing the guarded function name but executing unguarded `nextval` was accepted; `/tmp/backend-w9-default-drift-green-20261014-01.json` verifies exact-expression rejection. Pre-commit SELF `/tmp/backend-w9-precommit-all-20261014-02.json` and `/tmp/backend-w9-precommit-pg-20261014-02.json` passed before this last guard tightening; fixed-HEAD runs are required below. +- After the guard tightening, pre-commit SELF receipts `/tmp/backend-w9-precommit-all-final-20261014-01.json` (`go test ./... -count=1`), `/tmp/backend-w9-precommit-pg-final-20261014-01.json` (isolated PG store/storage-migrate), and `/tmp/backend-w9-precommit-pg-race-20261014-01.json` (cross-page/concurrent read-write with race detector) all exited 0. These still identify the dirty pre-commit tree, so they do not replace fixed-HEAD verification. +- Integration constraint: this branch uses schema v31 (audit attribution), v32 (links), and v33 (PG cutoff), while the independent W6 Backend branch also has a local v31. Neither branch has consumed the other's unmerged work. After the first branch lands, the later branch owner must sync the new main, renumber its unpublished migrations without rewriting an already released version, and rerun fresh/old SQLite and PG migration plus combined regression on the actual merged source. These isolated tests do not prove W6+W9 compatibility. +- Required next W9 work: real RDM committed-event verifier/outbox consumer, Backend project-event source, run ID propagation, model/tool/background receipts, body/detail disclosure, broad authorized linked-operation pagination, history/export, and combined product verification remain unfinished. A domain commit followed by a lost Backend audit append leaves the link pending; recovery must retain and reconcile that unknown state without claiming a host receipt. Exact-link and owner-only paging do not satisfy Stage 7 audit acceptance. PostgreSQL v33 adds a global command-audit append lock; its measured wait/throughput cost has no approved performance threshold and needs human Stage 6 review. diff --git a/cmd/storage-migrate/main.go b/cmd/storage-migrate/main.go index 884ca51..3a4f366 100644 --- a/cmd/storage-migrate/main.go +++ b/cmd/storage-migrate/main.go @@ -752,30 +752,34 @@ func sqliteBool(v any) bool { // 后续应用插入会撞主键)。无序列表为空操作。 func advanceSequences(db *sql.DB, table string) error { rows, err := db.Query(` - SELECT pg_get_serial_sequence($1, column_name) + SELECT column_name, pg_get_serial_sequence(format('%I.%I',table_schema,table_name), column_name) FROM information_schema.columns - WHERE table_schema=current_schema() AND table_name=$1 - AND (column_default LIKE 'nextval%' OR is_identity='YES')`, table) + WHERE table_schema=current_schema() AND table_name=$1`, table) if err != nil { return err } defer rows.Close() - var seqs []string + var seqs []struct{ name, column string } for rows.Next() { - var s any - if err := rows.Scan(&s); err != nil { + var column string + var seq sql.NullString + if err := rows.Scan(&column, &seq); err != nil { return err } - if s != nil { - seqs = append(seqs, s.(string)) + if seq.Valid { + seqs = append(seqs, struct{ name, column string }{seq.String, column}) } } if err := rows.Err(); err != nil { return err } for _, s := range seqs { - if _, err := db.Exec(fmt.Sprintf(`SELECT setval('%s', COALESCE((SELECT MAX(id) FROM "%s"), 1))`, s, table)); err != nil { - return fmt.Errorf("setval %s: %w", s, err) + // Sequence ownership remains authoritative when a column's default is a + // guarded function rather than a literal nextval expression. + column := `"` + strings.ReplaceAll(s.column, `"`, `""`) + `"` + tableName := `"` + strings.ReplaceAll(table, `"`, `""`) + `"` + if _, err := db.Exec(fmt.Sprintf(`SELECT setval($1::regclass, COALESCE((SELECT MAX(%s) FROM %s), 1))`, column, tableName), s.name); err != nil { + return fmt.Errorf("setval %s (%s): %w", s.name, s.column, err) } } return nil diff --git a/internal/app/project_audit.go b/internal/app/project_audit.go index 8a005cc..43a7183 100644 --- a/internal/app/project_audit.go +++ b/internal/app/project_audit.go @@ -274,15 +274,11 @@ func (h inprocHost) QueryProjectAudit(ctx context.Context, q extension.ProjectAu if err != nil { return extension.ProjectAuditPage{}, err } - cutoffConsistency := "committed-prefix" - if h.caps.st.Dialect() == "postgres" { - cutoffConsistency = "offset-upper-bound-only" - } page := extension.ProjectAuditPage{ Receipts: make([]extension.HostAuditReceipt, 0, len(rows)), NextOffset: next, CutoffOffset: cutoff, Coverage: extension.ProjectAuditCoverage{ - CommandAudit: "actor-owned-project-bound-only", CutoffConsistency: cutoffConsistency, CrossMember: "unsupported", LegacyProjectBinding: "not-recorded", RunRelations: "unsupported", ModelReceipts: "unsupported", + CommandAudit: "actor-owned-project-bound-only", CutoffConsistency: "committed-prefix", CrossMember: "unsupported", LegacyProjectBinding: "not-recorded", RunRelations: "unsupported", ModelReceipts: "unsupported", BackgroundReceipts: "unsupported", ResourceBodies: "unsupported", }, } diff --git a/internal/store/audit.go b/internal/store/audit.go index d77301a..6894c02 100644 --- a/internal/store/audit.go +++ b/internal/store/audit.go @@ -4,6 +4,7 @@ import ( "database/sql" "encoding/json" "errors" + "fmt" "time" agentcommand "agent-command" @@ -13,6 +14,9 @@ import ( // owner 是租户身份(认证注入),与信封里的 Actor(发起主体)语义不同:查询按 // owner 过滤,跨租户审计不可见。 func (s *Store) AppendAudit(owner string, env agentcommand.AuditEnvelope) error { + if err := s.requireProjectAuditCommittedPrefix(); err != nil { + return err + } raw, err := json.Marshal(env) if err != nil { return err @@ -33,6 +37,24 @@ type ProjectCommandAuditRow struct { Envelope agentcommand.AuditEnvelope } +// ErrAuditCutoffUnsafe prevents a PostgreSQL page from claiming a fixed cutoff +// when the installed ID generator or sequence no longer proves commit order. +var ErrAuditCutoffUnsafe = errors.New("store: project audit cutoff unsafe") + +func (s *Store) requireProjectAuditCommittedPrefix() error { + if s.dialect.name() != "postgres" { + return nil + } // SQLite serializes writers. + var safe bool + if err := s.db.QueryRow(`SELECT project_audit_cutoff_safe('command_audit'::regclass)`).Scan(&safe); err != nil { + return fmt.Errorf("%w: %v", ErrAuditCutoffUnsafe, err) + } + if !safe { + return ErrAuditCutoffUnsafe + } + return nil +} + // ListProjectCommandAudits reads the project resolved from a trusted conversation // at append time. Reassigning or deleting that conversation cannot rewrite the // original project. Legacy rows without this snapshot are excluded: current @@ -42,6 +64,9 @@ func (s *Store) ListProjectCommandAudits(actorOwner, projectID string, before, c if actorOwner == "" || projectID == "" || before < 0 || cutoff < 0 || (before != 0 && cutoff == 0) { return nil, 0, 0, errors.New("project audit: invalid scope or cursor") } + if err := s.requireProjectAuditCommittedPrefix(); err != nil { + return nil, 0, 0, err + } if limit <= 0 || limit > 200 { limit = 50 } diff --git a/internal/store/migrate.go b/internal/store/migrate.go index 0c44edb..24a9763 100644 --- a/internal/store/migrate.go +++ b/internal/store/migrate.go @@ -866,6 +866,59 @@ func init() { "sqlite": {`DROP TABLE IF EXISTS project_audit_links`}, "postgres": {`DROP TABLE IF EXISTS project_audit_links`}, }}, + migration{version: 33, name: "project_audit_committed_prefix", + up: map[string][]string{ + // SQLite has one writer and one connection per Store; this migration + // only changes PostgreSQL's implicit command_audit ID allocation. + "sqlite": {`SELECT 1`}, + "postgres": { + `LOCK TABLE command_audit IN ACCESS EXCLUSIVE MODE`, + `CREATE FUNCTION project_audit_cutoff_safe(target regclass) RETURNS boolean LANGUAGE plpgsql VOLATILE AS $$ + DECLARE seq regclass; default_expr text; increment_by bigint; min_value bigint; start_value bigint; cache_size bigint; cycles boolean; + last_number numeric; was_called boolean; next_number numeric; min_id bigint; max_id bigint; + BEGIN + seq := pg_get_serial_sequence(target::text,'id')::regclass; + IF seq IS NULL THEN RETURN false; END IF; + SELECT pg_get_expr(d.adbin,d.adrelid) INTO default_expr FROM pg_attrdef d + JOIN pg_attribute a ON a.attrelid=d.adrelid AND a.attnum=d.adnum + WHERE d.adrelid=target AND a.attname='id'; + IF default_expr IS DISTINCT FROM format('next_command_audit_id(%L::regclass)',target::text) THEN RETURN false; END IF; + SELECT seqincrement,seqmin,seqstart,seqcache,seqcycle + INTO increment_by,min_value,start_value,cache_size,cycles FROM pg_sequence WHERE seqrelid=seq; + IF increment_by IS NULL OR increment_by<=0 OR min_value<=0 OR start_value<=0 OR cache_size<>1 OR cycles THEN RETURN false; END IF; + -- Supported writers allocate under the lock before they commit. Read + -- committed rows first, then the non-MVCC sequence state: a writer + -- committing between these reads can only advance the latter. + EXECUTE format('SELECT COALESCE(MIN(id),1),COALESCE(MAX(id),0) FROM %s',target) INTO min_id,max_id; + EXECUTE format('SELECT last_value,is_called FROM %s',seq) INTO last_number,was_called; + next_number := last_number; + IF was_called THEN next_number := last_number+increment_by; END IF; + RETURN min_id>0 AND next_number>0 AND next_number>max_id; + END $$`, + `CREATE FUNCTION next_command_audit_id(target regclass) RETURNS bigint LANGUAGE plpgsql VOLATILE AS $$ + DECLARE seq regclass; allocated bigint; max_id bigint; + BEGIN + PERFORM pg_advisory_xact_lock(715328491,target::oid::int4); + IF NOT project_audit_cutoff_safe(target) THEN RAISE EXCEPTION 'project audit cutoff sequence is unsafe'; END IF; + seq := pg_get_serial_sequence(target::text,'id')::regclass; + allocated := nextval(seq); + EXECUTE format('SELECT COALESCE(MAX(id),0) FROM %s',target) INTO max_id; + IF allocated<=0 OR allocated<=max_id THEN RAISE EXCEPTION 'project audit ID did not advance beyond committed rows'; END IF; + RETURN allocated; + END $$`, + `ALTER TABLE command_audit ALTER COLUMN id SET DEFAULT next_command_audit_id('command_audit'::regclass)`, + `DO $$ BEGIN IF NOT project_audit_cutoff_safe('command_audit'::regclass) THEN RAISE EXCEPTION 'project audit cutoff migration precondition failed'; END IF; END $$`, + }, + }, + down: map[string][]string{ + "sqlite": {`SELECT 1`}, + "postgres": { + `LOCK TABLE command_audit IN ACCESS EXCLUSIVE MODE`, + `ALTER TABLE command_audit ALTER COLUMN id SET DEFAULT nextval(pg_get_serial_sequence('command_audit','id')::regclass)`, + `DROP FUNCTION next_command_audit_id(regclass)`, + `DROP FUNCTION project_audit_cutoff_safe(regclass)`, + }, + }}, ) } diff --git a/internal/store/migrate_compat.go b/internal/store/migrate_compat.go index 9160ed1..a653f3b 100644 --- a/internal/store/migrate_compat.go +++ b/internal/store/migrate_compat.go @@ -37,6 +37,7 @@ var migrationBreaksOldReaders = map[int]bool{ 30: false, // project_event_publication_proofs:新表,旧读者不受影响 31: false, // command_audit_attribution_snapshot:仅加列/索引,旧读者不受影响 32: false, // project_audit_committed_links:新表,旧读者不受影响 + 33: false, // project_audit_committed_prefix:PG 审计 ID 默认函数,旧读者兼容 } // SchemaTarget 是本二进制会迁移到的 schema 版本。 diff --git a/internal/store/project_audit_sequence_test.go b/internal/store/project_audit_sequence_test.go new file mode 100644 index 0000000..022f2b8 --- /dev/null +++ b/internal/store/project_audit_sequence_test.go @@ -0,0 +1,500 @@ +package store + +import ( + "context" + "encoding/json" + "fmt" + "os" + "sort" + "strings" + "sync" + "testing" + "time" + + agentcommand "agent-command" +) + +func openProjectAuditPG(t *testing.T) *Store { + t.Helper() + if os.Getenv("AGENT_PG_TEST_DSN") == "" { + t.Skip("AGENT_PG_TEST_DSN unset: isolated PostgreSQL check NOT_RUN") + } + st := openTestStore(t) + if st.Dialect() != "postgres" { + t.Fatal("expected PostgreSQL") + } + return st +} + +func commandAuditSequenceName(t *testing.T, st *Store) string { + t.Helper() + var name string + if err := st.db.QueryRow(`SELECT pg_get_serial_sequence('command_audit','id')`).Scan(&name); err != nil { + t.Fatal(err) + } + if name == "" { + t.Fatal("command_audit has no owned sequence") + } + return name +} + +func TestProjectAuditSequenceMigrationRejectsCacheTen(t *testing.T) { + st := openProjectAuditPG(t) + if err := st.MigrateDown(32); err != nil { + t.Fatal(err) + } + seq := commandAuditSequenceName(t, st) + if _, err := st.db.Exec("ALTER SEQUENCE " + seq + " CACHE 10"); err != nil { + t.Fatal(err) + } + if err := st.migrate(); err == nil { + t.Fatal("migration accepted CACHE 10, which permits out-of-order IDs") + } + if _, err := st.db.Exec("ALTER SEQUENCE " + seq + " CACHE 1"); err != nil { + t.Fatal(err) + } + if err := st.migrate(); err != nil { + t.Fatalf("safe CACHE 1 migration: %v", err) + } +} + +func TestProjectAuditSequenceRuntimeRejectsConfigurationDrift(t *testing.T) { + st := openProjectAuditPG(t) + seq := commandAuditSequenceName(t, st) + if _, err := st.db.Exec("ALTER SEQUENCE " + seq + " CACHE 10"); err != nil { + t.Fatal(err) + } + if _, _, _, err := st.ListProjectCommandAudits("alice", "project", 0, 0, 10); err == nil { + t.Fatal("project audit reported a cutoff with CACHE 10") + } + if err := st.AppendAudit("alice", agentcommand.AuditEnvelope{Time: time.Now(), Actor: "alice", CommandID: "unsafe-cache"}); err == nil { + t.Fatal("append allocated an ID with CACHE 10") + } + if _, err := st.db.Exec("ALTER SEQUENCE " + seq + " CACHE 1"); err != nil { + t.Fatal(err) + } + if err := st.AppendAudit("alice", agentcommand.AuditEnvelope{Time: time.Now(), Actor: "alice", CommandID: "safe-cache"}); err != nil { + t.Fatalf("safe append after reset: %v", err) + } + if _, err := st.db.Exec("ALTER SEQUENCE " + seq + " CYCLE"); err != nil { + t.Fatal(err) + } + if _, _, _, err := st.ListProjectCommandAudits("alice", "project", 0, 0, 10); err == nil { + t.Fatal("read accepted cyclic audit IDs") + } + if err := st.AppendAudit("alice", agentcommand.AuditEnvelope{Time: time.Now(), Actor: "alice", CommandID: "cyclic"}); err == nil { + t.Fatal("append accepted cyclic audit IDs") + } + if _, err := st.db.Exec("ALTER SEQUENCE " + seq + " NO CYCLE"); err != nil { + t.Fatal(err) + } + if _, err := st.db.Exec("ALTER SEQUENCE " + seq + " MINVALUE -10"); err != nil { + t.Fatal(err) + } + if _, _, _, err := st.ListProjectCommandAudits("alice", "project", 0, 0, 10); err == nil { + t.Fatal("read accepted a nonpositive audit ID range") + } + if _, err := st.db.Exec("ALTER SEQUENCE " + seq + " MINVALUE 1"); err != nil { + t.Fatal(err) + } + if _, err := st.db.Exec(`ALTER TABLE command_audit ALTER COLUMN id SET DEFAULT nextval(pg_get_serial_sequence('command_audit','id')::regclass)`); err != nil { + t.Fatal(err) + } + if _, _, _, err := st.ListProjectCommandAudits("alice", "project", 0, 0, 10); err == nil { + t.Fatal("read accepted an unguarded sequence default") + } + if err := st.AppendAudit("alice", agentcommand.AuditEnvelope{Time: time.Now(), Actor: "alice", CommandID: "unguarded-default"}); err == nil { + t.Fatal("append accepted an unguarded sequence default") + } + if _, err := st.db.Exec(`ALTER TABLE command_audit ALTER COLUMN id SET DEFAULT CASE WHEN false THEN next_command_audit_id('command_audit'::regclass) ELSE nextval(pg_get_serial_sequence('command_audit','id')::regclass) END`); err != nil { + t.Fatal(err) + } + if _, _, _, err := st.ListProjectCommandAudits("alice", "project", 0, 0, 10); err == nil { + t.Fatal("read accepted a replaced default that merely mentions the guarded function") + } + if _, err := st.db.Exec(`ALTER TABLE command_audit ALTER COLUMN id SET DEFAULT next_command_audit_id('command_audit'::regclass)`); err != nil { + t.Fatal(err) + } +} + +func TestProjectAuditSequenceRejectsRewindIntoUnusedGap(t *testing.T) { + st := openProjectAuditPG(t) + p, err := st.CreateProject("alice", "A") + if err != nil { + t.Fatal(err) + } + c, err := st.CreateConversation("alice", string(p.ID), "work", "") + if err != nil { + t.Fatal(err) + } + appendAudit := func(id string) error { + return st.AppendAudit("alice", agentcommand.AuditEnvelope{Time: time.Now(), Actor: "alice", CommandID: id, ConversationID: string(c.ID), State: agentcommand.StateCommitted}) + } + if err := appendAudit("first"); err != nil { + t.Fatal(err) + } + seq := commandAuditSequenceName(t, st) + if _, err := st.db.Exec("SELECT nextval('" + seq + "'::regclass)"); err != nil { + t.Fatal(err) + } // consume one ID without a row + if err := appendAudit("third"); err != nil { + t.Fatal(err) + } + if _, err := st.db.Exec("SELECT setval('" + seq + "'::regclass,1)"); err != nil { + t.Fatal(err) + } + if err := appendAudit("rewound-into-gap"); err == nil { + t.Fatal("rewound sequence inserted a low ID into a committed-prefix gap") + } + if _, _, _, err := st.ListProjectCommandAudits("alice", string(p.ID), 0, 0, 10); err == nil { + t.Fatal("read claimed a safe cutoff after sequence rewind") + } + if _, err := st.db.Exec("SELECT setval('" + seq + "'::regclass,3)"); err != nil { + t.Fatal(err) + } + if err := appendAudit("recovered"); err != nil { + t.Fatalf("append after restoring sequence: %v", err) + } +} + +// Advisory locking alone does not fix cached sequence allocation: session A +// can hold 2..10 while session B commits 11, then A later commits 2. +func TestProjectAuditSequenceCacheTenDefeatsAdvisoryLock(t *testing.T) { + st := openProjectAuditPG(t) + for _, query := range []string{ + `CREATE TABLE cache_probe(id BIGSERIAL PRIMARY KEY)`, + `ALTER SEQUENCE cache_probe_id_seq CACHE 10`, + `CREATE FUNCTION cache_probe_next_id(target regclass) RETURNS bigint LANGUAGE plpgsql AS $$ + BEGIN PERFORM pg_advisory_xact_lock(715328491,target::oid::int4); + RETURN nextval(pg_get_serial_sequence(target::text,'id')::regclass); END $$`, + `ALTER TABLE cache_probe ALTER COLUMN id SET DEFAULT cache_probe_next_id('cache_probe'::regclass)`, + } { + if _, err := st.db.Exec(query); err != nil { + t.Fatal(err) + } + } + ctx := context.Background() + a, err := st.db.Conn(ctx) + if err != nil { + t.Fatal(err) + } + defer a.Close() + b, err := st.db.Conn(ctx) + if err != nil { + t.Fatal(err) + } + defer b.Close() + var first, other, late int64 + if err := a.QueryRowContext(ctx, `INSERT INTO cache_probe DEFAULT VALUES RETURNING id`).Scan(&first); err != nil { + t.Fatal(err) + } + if err := b.QueryRowContext(ctx, `INSERT INTO cache_probe DEFAULT VALUES RETURNING id`).Scan(&other); err != nil { + t.Fatal(err) + } + if err := a.QueryRowContext(ctx, `INSERT INTO cache_probe DEFAULT VALUES RETURNING id`).Scan(&late); err != nil { + t.Fatal(err) + } + if first != 1 || other != 11 || late != 2 { + t.Fatalf("CACHE 10 counterexample changed: A=%d B=%d A-late=%d", first, other, late) + } +} + +func TestProjectAuditSequenceNeverCommitsPreUpgradeCachedLowID(t *testing.T) { + st := openProjectAuditPG(t) + if err := st.MigrateDown(32); err != nil { + t.Fatal(err) + } + seq := commandAuditSequenceName(t, st) + if _, err := st.db.Exec("ALTER SEQUENCE " + seq + " CACHE 10"); err != nil { + t.Fatal(err) + } + ctx := context.Background() + a, err := st.db.Conn(ctx) + if err != nil { + t.Fatal(err) + } + defer a.Close() + b, err := st.db.Conn(ctx) + if err != nil { + t.Fatal(err) + } + defer b.Close() + insert := `INSERT INTO command_audit(time,command_id,source,entrance,state,operation_id,conversation_id,owner,payload) + VALUES ('2026-10-14T00:00:00Z','legacy','','','committed','','','alice','{}') RETURNING id` + var first, high, late int64 + if err := a.QueryRowContext(ctx, insert).Scan(&first); err != nil { + t.Fatal(err) + } + if err := b.QueryRowContext(ctx, insert).Scan(&high); err != nil { + t.Fatal(err) + } + if first != 1 || high != 11 { + t.Fatalf("old cached allocation: first=%d high=%d", first, high) + } + if _, err := st.db.Exec("ALTER SEQUENCE " + seq + " CACHE 1"); err != nil { + t.Fatal(err) + } + if err := st.migrate(); err != nil { + t.Fatalf("upgrade with idle cached connections: %v", err) + } + if err := a.QueryRowContext(ctx, insert).Scan(&late); err == nil && late <= high { + t.Fatalf("old connection committed late low ID %d below %d", late, high) + } + var count int + if err := st.db.QueryRow(`SELECT COUNT(*) FROM command_audit WHERE id=2`).Scan(&count); err != nil || count != 0 { + t.Fatalf("late cached row persisted: count=%d err=%v", count, err) + } +} + +func TestProjectAuditSequenceMigrationRoundTripPreservesLinks(t *testing.T) { + st := openProjectAuditPG(t) + if err := st.EnsureUser("alice", "Alice"); err != nil { + t.Fatal(err) + } + p, err := st.CreateProject("alice", "A") + if err != nil { + t.Fatal(err) + } + c, err := st.CreateConversation("alice", string(p.ID), "work", "") + if err != nil { + t.Fatal(err) + } + if err := st.AppendAudit("alice", agentcommand.AuditEnvelope{Time: time.Now(), Actor: "alice", CommandID: "rdm.confirm", OperationID: "op-1", ConversationID: string(c.ID), State: agentcommand.StateCommitted}); err != nil { + t.Fatal(err) + } + rows, _, _, err := st.ListProjectCommandAudits("alice", string(p.ID), 0, 0, 10) + if err != nil { + t.Fatal(err) + } + offset := rows[0].Offset + link := ProjectAuditLink{HostAuditOffset: offset, ProjectID: string(p.ID), Owner: "alice", SourceKind: "domain-commit", SourceID: "commit-1", OperationID: "op-1", ResourceKind: "document", ResourceID: "doc-1", DisclosureClass: "metadata", IdempotencyKey: "outbox-1"} + if _, err := st.SaveProjectAuditLink(link); err != nil { + t.Fatal(err) + } + if err := st.MigrateDown(32); err != nil { + t.Fatalf("down to v32: %v", err) + } + if _, _, _, err := st.ListProjectCommandAudits("alice", string(p.ID), 0, 0, 10); err == nil { + t.Fatal("old default exposed a strong cutoff after down") + } + if _, row, err := st.ProjectAuditLinkedReceipt(string(p.ID), "domain-commit", "commit-1", offset); err != nil || row.Offset != offset { + t.Fatalf("link changed during down: %+v %v", row, err) + } + if err := st.migrate(); err != nil { + t.Fatalf("up to v33: %v", err) + } + rows, _, _, err = st.ListProjectCommandAudits("alice", string(p.ID), 0, 0, 10) + if err != nil || len(rows) != 1 || rows[0].Offset != offset { + t.Fatalf("restored audit: %+v %v", rows, err) + } + if err := st.AppendAudit("alice", agentcommand.AuditEnvelope{Time: time.Now(), Actor: "alice", CommandID: "after-up", ConversationID: string(c.ID), State: agentcommand.StateCommitted}); err != nil { + t.Fatal(err) + } + rows, _, _, err = st.ListProjectCommandAudits("alice", string(p.ID), 0, 0, 10) + if err != nil || len(rows) != 2 || rows[0].Offset <= offset { + t.Fatalf("sequence not advanced after up: %+v %v", rows, err) + } +} + +func TestProjectAuditSequenceLockTimeoutAndRollback(t *testing.T) { + st := openProjectAuditPG(t) + p, err := st.CreateProject("alice", "A") + if err != nil { + t.Fatal(err) + } + c, err := st.CreateConversation("alice", string(p.ID), "work", "") + if err != nil { + t.Fatal(err) + } + tx, err := st.db.Begin() + if err != nil { + t.Fatal(err) + } + defer tx.Rollback() + insert := st.q(`INSERT INTO command_audit(time,command_id,source,entrance,state,operation_id,conversation_id,owner,payload,actor_name,project_id) + VALUES (?,?,'','','committed','',?,?,?,?,?) RETURNING id`) + args := []any{time.Now().UTC().Format(time.RFC3339Nano), "held", string(c.ID), "alice", `{}`, "Alice", string(p.ID)} + var heldID int64 + if err := tx.QueryRow(insert, args...).Scan(&heldID); err != nil { + t.Fatal(err) + } + ctx := context.Background() + conn, err := st.db.Conn(ctx) + if err != nil { + t.Fatal(err) + } + defer conn.Close() + if _, err := conn.ExecContext(ctx, `SET lock_timeout='150ms'`); err != nil { + t.Fatal(err) + } + var blockedID int64 + err = conn.QueryRowContext(ctx, insert, args...).Scan(&blockedID) + if err == nil || !strings.Contains(err.Error(), "lock timeout") { + t.Fatalf("second writer did not stop on advisory lock: id=%d err=%v", blockedID, err) + } + if _, err := conn.ExecContext(ctx, `RESET lock_timeout`); err != nil { + t.Fatal(err) + } + if err := tx.Rollback(); err != nil { + t.Fatal(err) + } + if err := st.AppendAudit("alice", agentcommand.AuditEnvelope{Time: time.Now(), Actor: "alice", CommandID: "after-rollback", ConversationID: string(c.ID), State: agentcommand.StateCommitted}); err != nil { + t.Fatal(err) + } + rows, cutoff, _, err := st.ListProjectCommandAudits("alice", string(p.ID), 0, 0, 10) + if err != nil || len(rows) != 1 || rows[0].Offset <= heldID || cutoff != rows[0].Offset { + t.Fatalf("rollback gap affected cutoff: rows=%+v cutoff=%d held=%d err=%v", rows, cutoff, heldID, err) + } +} + +func TestProjectAuditSequenceConcurrentLoad(t *testing.T) { + st := openProjectAuditPG(t) + var serverVersion string + if err := st.db.QueryRow(`SHOW server_version`).Scan(&serverVersion); err != nil { + t.Fatal(err) + } + if err := st.EnsureUser("alice", "Alice"); err != nil { + t.Fatal(err) + } + p, err := st.CreateProject("alice", "A") + if err != nil { + t.Fatal(err) + } + c, err := st.CreateConversation("alice", string(p.ID), "work", "") + if err != nil { + t.Fatal(err) + } + const writers, each = 8, 24 + run := func(label string, write func(agentcommand.AuditEnvelope) error) (time.Duration, time.Duration) { + t.Helper() + durations := make([]time.Duration, writers*each) + errors := make(chan error, writers*each) + start := time.Now() + var wg sync.WaitGroup + for worker := 0; worker < writers; worker++ { + wg.Add(1) + go func(worker int) { + defer wg.Done() + for i := 0; i < each; i++ { + env := agentcommand.AuditEnvelope{Time: time.Now(), Actor: "alice", CommandID: fmt.Sprintf("%s-%d-%d", label, worker, i), ConversationID: string(c.ID), State: agentcommand.StateCommitted} + at := time.Now() + if err := write(env); err != nil { + errors <- err + return + } + durations[worker*each+i] = time.Since(at) + } + }(worker) + } + wg.Wait() + close(errors) + for err := range errors { + t.Fatalf("%s append: %v", label, err) + } + elapsed := time.Since(start) + sort.Slice(durations, func(i, j int) bool { return durations[i] < durations[j] }) + return elapsed, durations[len(durations)*95/100] + } + if err := st.MigrateDown(32); err != nil { + t.Fatal(err) + } + oldQuery := st.q(`INSERT INTO command_audit(time,command_id,source,entrance,state,operation_id,conversation_id,owner,payload,actor_name,project_id) + VALUES (?,?,?,?,?,?,?,?,?,COALESCE((SELECT name FROM users WHERE owner=?),''),COALESCE((SELECT project_id FROM conversations WHERE id=? AND owner=?),''))`) + oldElapsed, oldP95 := run("old", func(env agentcommand.AuditEnvelope) error { + raw, err := json.Marshal(env) + if err != nil { + return err + } + _, err = st.db.Exec(oldQuery, env.Time.UTC().Format(time.RFC3339Nano), env.CommandID, "", "", string(env.State), "", env.ConversationID, "alice", string(raw), env.Actor, env.ConversationID, "alice") + return err + }) + if err := st.migrate(); err != nil { + t.Fatalf("upgrade after baseline: %v", err) + } + statusMode := "MAX-first unlocked status" + if os.Getenv("AGENT_AUDIT_BENCH_READ_LOCK") == "1" { + // A test-only wrapper reconstructs the earlier status-read lock so the + // isolated load receipt can compare its cost against the final design. + if _, err := st.db.Exec(`ALTER FUNCTION project_audit_cutoff_safe(regclass) RENAME TO project_audit_cutoff_unlocked`); err != nil { + t.Fatal(err) + } + if _, err := st.db.Exec(`CREATE FUNCTION project_audit_cutoff_safe(target regclass) RETURNS boolean LANGUAGE plpgsql VOLATILE AS $$ + BEGIN PERFORM pg_advisory_xact_lock(715328491,target::oid::int4); + RETURN project_audit_cutoff_unlocked(target); END $$`); err != nil { + t.Fatal(err) + } + statusMode = "test-only locked status wrapper" + } + newElapsed, newP95 := run("guarded", func(env agentcommand.AuditEnvelope) error { return st.AppendAudit("alice", env) }) + t.Logf("isolated PostgreSQL %s audit append: %d writes per mode / %d workers; v32 direct INSERT=%s p95=%s (%.0f/s), v33 Store.AppendAudit with %s=%s p95=%s (%.0f/s); hardware/load dependent SELF only", + serverVersion, writers*each, writers, oldElapsed, oldP95, float64(writers*each)/oldElapsed.Seconds(), statusMode, newElapsed, newP95, float64(writers*each)/newElapsed.Seconds()) + seen := map[string]bool{} + var before, cutoff int64 + for { + page, fixed, next, err := st.ListProjectCommandAudits("alice", string(p.ID), before, cutoff, 100) + if err != nil { + t.Fatal(err) + } + if cutoff == 0 { + cutoff = fixed + } + for _, row := range page { + if seen[row.Envelope.CommandID] { + t.Fatalf("duplicate paged command: %s", row.Envelope.CommandID) + } + seen[row.Envelope.CommandID] = true + } + if next == 0 { + break + } + before = next + } + if len(seen) != 2*writers*each { + t.Fatalf("paged commands=%d want=%d", len(seen), 2*writers*each) + } +} + +func TestProjectAuditConcurrentReadWriteNeverReportsFalseDrift(t *testing.T) { + st := openProjectAuditPG(t) + p, err := st.CreateProject("alice", "A") + if err != nil { + t.Fatal(err) + } + c, err := st.CreateConversation("alice", string(p.ID), "work", "") + if err != nil { + t.Fatal(err) + } + const writers, writes, readers, reads = 6, 30, 4, 40 + errs := make(chan error, writers*writes+readers*reads) + var wg sync.WaitGroup + for worker := 0; worker < writers; worker++ { + wg.Add(1) + go func(worker int) { + defer wg.Done() + for i := 0; i < writes; i++ { + err := st.AppendAudit("alice", agentcommand.AuditEnvelope{Time: time.Now(), Actor: "alice", CommandID: fmt.Sprintf("write-%d-%d", worker, i), ConversationID: string(c.ID), State: agentcommand.StateCommitted}) + if err != nil { + errs <- fmt.Errorf("append: %w", err) + return + } + } + }(worker) + } + for reader := 0; reader < readers; reader++ { + wg.Add(1) + go func() { + defer wg.Done() + for i := 0; i < reads; i++ { + if _, _, _, err := st.ListProjectCommandAudits("alice", string(p.ID), 0, 0, 10); err != nil { + errs <- fmt.Errorf("read: %w", err) + return + } + } + }() + } + wg.Wait() + close(errs) + for err := range errs { + t.Fatal(err) + } +} diff --git a/internal/store/project_audit_test.go b/internal/store/project_audit_test.go index db91605..7234dd7 100644 --- a/internal/store/project_audit_test.go +++ b/internal/store/project_audit_test.go @@ -174,10 +174,10 @@ func TestProjectCommandAuditDoesNotMoveWithConversation(t *testing.T) { } } -// PostgreSQL sequence IDs are allocated before commit. This test records the -// current upper-bound semantics: a lower ID can appear after a page cutoff was -// captured, so callers must not describe the cutoff as a visibility snapshot. -func TestProjectAuditPostgresCutoffLateCommit(t *testing.T) { +// Under the old PostgreSQL SERIAL default, an uncommitted middle ID can be +// skipped after a higher committed row advances the page cursor past it. +// The committed-prefix implementation must keep the later writer waiting. +func TestProjectAuditPostgresCursorNeverSkipsLateCommit(t *testing.T) { if os.Getenv("AGENT_PG_TEST_DSN") == "" { t.Skip("AGENT_PG_TEST_DSN unset: PG concurrency check NOT_RUN") } @@ -196,7 +196,12 @@ func TestProjectAuditPostgresCutoffLateCommit(t *testing.T) { if err != nil { t.Fatal(err) } - env := agentcommand.AuditEnvelope{Time: time.Now(), Actor: "alice", CommandID: "late-low-id", ConversationID: string(c.ID)} + for _, command := range []string{"lower-one", "lower-two"} { + if err := st.AppendAudit("alice", agentcommand.AuditEnvelope{Time: time.Now(), Actor: "alice", CommandID: command, ConversationID: string(c.ID), State: agentcommand.StateCommitted}); err != nil { + t.Fatal(err) + } + } + env := agentcommand.AuditEnvelope{Time: time.Now(), Actor: "alice", CommandID: "middle-pending", ConversationID: string(c.ID), State: agentcommand.StateCommitted} raw, err := json.Marshal(env) if err != nil { t.Fatal(err) @@ -206,29 +211,57 @@ func TestProjectAuditPostgresCutoffLateCommit(t *testing.T) { t.Fatal(err) } defer tx.Rollback() - var lowID int64 + var middleID int64 if err := tx.QueryRow(st.q(`INSERT INTO command_audit(time,command_id,source,entrance,state,operation_id,conversation_id,owner,payload,actor_name,project_id) - VALUES (?,?,'','','','',?,?,?,?,?) RETURNING id`), time.Now().UTC().Format(time.RFC3339Nano), "late-low-id", string(c.ID), "alice", string(raw), "Alice", string(p.ID)).Scan(&lowID); err != nil { + VALUES (?,?,'','','committed','',?,?,?,?,?) RETURNING id`), time.Now().UTC().Format(time.RFC3339Nano), "middle-pending", string(c.ID), "alice", string(raw), "Alice", string(p.ID)).Scan(&middleID); err != nil { t.Fatal(err) } - if err := st.AppendAudit("alice", agentcommand.AuditEnvelope{Time: time.Now(), Actor: "alice", CommandID: "committed-high-id", ConversationID: string(c.ID)}); err != nil { - t.Fatal(err) + highDone := make(chan error, 1) + started := make(chan struct{}) + go func() { + close(started) + highDone <- st.AppendAudit("alice", agentcommand.AuditEnvelope{Time: time.Now(), Actor: "alice", CommandID: "higher-committed", ConversationID: string(c.ID), State: agentcommand.StateCommitted}) + }() + <-started + highCommittedEarly := false + select { + case err := <-highDone: + if err != nil { + t.Fatal(err) + } + highCommittedEarly = true + case <-time.After(250 * time.Millisecond): } - first, cutoff, _, err := st.ListProjectCommandAudits("alice", string(p.ID), 0, 0, 10) - if err != nil { - t.Fatal(err) - } - if len(first) != 1 || first[0].Envelope.CommandID != "committed-high-id" || lowID >= cutoff { - t.Fatalf("first page: rows=%+v low=%d cutoff=%d", first, lowID, cutoff) + var first []ProjectCommandAuditRow + var cutoff, next int64 + if highCommittedEarly { + first, cutoff, next, err = st.ListProjectCommandAudits("alice", string(p.ID), 0, 0, 2) + if err != nil { + tx.Rollback() + t.Fatal(err) + } } if err := tx.Commit(); err != nil { t.Fatal(err) } - later, fixed, _, err := st.ListProjectCommandAudits("alice", string(p.ID), 0, cutoff, 10) + if !highCommittedEarly { + if err := <-highDone; err != nil { + t.Fatal(err) + } + first, cutoff, next, err = st.ListProjectCommandAudits("alice", string(p.ID), 0, 0, 2) + if err != nil { + t.Fatal(err) + } + } + second, fixed, _, err := st.ListProjectCommandAudits("alice", string(p.ID), next, cutoff, 2) if err != nil { t.Fatal(err) } - if fixed != cutoff || len(later) != 2 || later[1].Envelope.CommandID != "late-low-id" { - t.Fatalf("late commit not exposed: rows=%+v cutoff=%d", later, fixed) + seen := map[string]bool{} + for _, row := range append(first, second...) { + seen[row.Envelope.CommandID] = true + } + if next == 0 || fixed != cutoff || len(seen) != 4 || !seen["middle-pending"] { + t.Fatalf("cross-page audit omitted a committed row: early=%v middleID=%d first=%+v second=%+v cutoff=%d next=%d", highCommittedEarly, middleID, first, second, cutoff, next) } } diff --git a/pkg/extension/extension.go b/pkg/extension/extension.go index 1812671..173e5f0 100644 --- a/pkg/extension/extension.go +++ b/pkg/extension/extension.go @@ -42,7 +42,9 @@ import ( // retryable ProjectAuditLinkRequest.HostAuditOffset=0 resolution. // 2.16.0-experimental: cryptographic operation IDs and registration-bound // ProjectAuditPublisherFactory; scoped Host string IDs cannot publish links. -const SurfaceVersion = "2.16.0-experimental" +// 2.17.0-experimental: PostgreSQL project audit pages require a verified +// committed-prefix ID generator; unsafe sequence/default drift rejects reads. +const SurfaceVersion = "2.17.0-experimental" // ErrNotLinked 表示进程没有空白导入 link 包,Main 无法启动服务。 var ErrNotLinked = errors.New("extension: runner not linked; blank-import agent-system-backend/pkg/extension/link") diff --git a/pkg/extension/project_audit.go b/pkg/extension/project_audit.go index ddca97c..d1592fe 100644 --- a/pkg/extension/project_audit.go +++ b/pkg/extension/project_audit.go @@ -102,9 +102,10 @@ type ProjectAuditPublisher interface { // ProjectAuditQuery reads one project-bound command-audit slice. Offset is a // host source offset, not a cross-domain event sequence. A nonzero BeforeOffset -// must be paired with the CutoffOffset returned on the first page. On PostgreSQL -// that cutoff bounds offsets but is not a fixed visibility snapshot: a lower ID -// from a still-open transaction may commit later. See Coverage.CutoffConsistency. +// must be paired with the CutoffOffset returned on the first page. PostgreSQL +// pages are served only while the installed generator and sequence prove a +// committed prefix; otherwise the host rejects the read instead of claiming a +// fixed snapshot. See Coverage.CutoffConsistency. type ProjectAuditQuery struct { ProjectID core.ID `json:"projectId"` BeforeOffset int64 `json:"beforeOffset,omitempty"` From d8129abead96be7c35ab2650e949ac817745e786 Mon Sep 17 00:00:00 2001 From: weichen Date: Wed, 14 Oct 2026 05:48:41 +0000 Subject: [PATCH 11/15] Persist candidate scope scrub state across restarts Why: A crash after dropping candidate raw prompt columns but before SQLite compaction left physical prompt bytes without a visible reason to retry, and storage backup could accept that file. Evidence: RED post-commit interruption and pending-backup tests; full Go suite, targeted race, isolated PostgreSQL package tests, vet, and build passed locally before commit. Impact: Commit a durable pending marker with v35, retry compaction and WAL truncation until completion, conservatively re-scrub earlier v35 candidates, and reject backup or migration while cleanup is pending or unverified. Refs: Approved RDM stage6 W6 source-recheck design adjustment r1; W6 independent review crash-window finding. --- cmd/storage-migrate/main.go | 15 +++++ cmd/storage-migrate/main_test.go | 58 +++++++++++++++- internal/store/context_manifest_test.go | 89 +++++++++++++++++++++++++ internal/store/migrate.go | 70 +++++++++++++++---- 4 files changed, 218 insertions(+), 14 deletions(-) diff --git a/cmd/storage-migrate/main.go b/cmd/storage-migrate/main.go index 93d9463..3a60187 100644 --- a/cmd/storage-migrate/main.go +++ b/cmd/storage-migrate/main.go @@ -430,6 +430,21 @@ func refuseCandidateRawContextScope(db *sql.DB) error { if columns > 0 { return fmt.Errorf("candidate context scope text columns remain; isolate and scrub source before backup or migration") } + var version int + if sqliteHasTable(db, "schema_migrations") { + if err := db.QueryRow(`SELECT COALESCE(MAX(version),0) FROM schema_migrations`).Scan(&version); err != nil { + return err + } + } + if version >= 34 { + if !sqliteHasTable(db, "context_scope_scrub_state") { + return fmt.Errorf("candidate context physical scrub marker missing; isolate and scrub source before backup or migration") + } + var pending int + if err := db.QueryRow(`SELECT pending FROM context_scope_scrub_state WHERE id=1`).Scan(&pending); err != nil || pending != 0 { + return fmt.Errorf("candidate context physical scrub pending or unverified; isolate and scrub source before backup or migration") + } + } return nil } diff --git a/cmd/storage-migrate/main_test.go b/cmd/storage-migrate/main_test.go index 24b9170..de09be6 100644 --- a/cmd/storage-migrate/main_test.go +++ b/cmd/storage-migrate/main_test.go @@ -173,7 +173,8 @@ func TestCopyOrderCoversAllFixtureTables(t *testing.T) { for _, t := range copyOrder { known[t] = true } - known["schema_migrations"] = true // 迁移器管理,不随迁 + known["schema_migrations"] = true // 迁移器管理,不随迁 + known["context_scope_scrub_state"] = true // SQLite 候选原文物理清理状态,不随迁 // 运行态表不随迁:执行许可是进程瞬态;执行租约与跨实例命令属当前 // 实例的调度现场(接管语义在 U5 内由 lease/fencing 处理,不复制历史)。 for _, rt := range []string{"admission_leases", "conversation_executions", "run_commands"} { @@ -248,3 +249,58 @@ func TestBackupAndMigrateRejectCandidateRawContextScope(t *testing.T) { t.Fatal("backup retained candidate raw scope body") } } + +func TestBackupAndMigrateRejectPendingOrMissingContextScrubMarker(t *testing.T) { + path := buildFixture(t) + db, err := sql.Open("sqlite", path) + if err != nil { + t.Fatal(err) + } + for _, query := range []string{ + `CREATE TABLE IF NOT EXISTS context_scope_scrub_state(id INTEGER PRIMARY KEY,pending INTEGER NOT NULL)`, + `INSERT OR REPLACE INTO context_scope_scrub_state(id,pending) VALUES(1,1)`, + } { + if _, err := db.Exec(query); err != nil { + t.Fatal(err) + } + } + if err := db.Close(); err != nil { + t.Fatal(err) + } + assertRejected := func(name string) { + t.Helper() + out := filepath.Join(t.TempDir(), name+".db") + if err := cmdBackup([]string{"--source", path, "--out", out}); err == nil { + t.Fatalf("backup accepted %s context scrub state", name) + } + if _, err := os.Stat(out); !os.IsNotExist(err) { + t.Fatalf("rejected %s backup created output: %v", name, err) + } + if err := cmdMigrate([]string{"--source", path, "--target", "postgres://invalid", "--migration-id", name}); err == nil { + t.Fatalf("migration accepted %s context scrub state", name) + } + } + assertRejected("pending") + db, err = sql.Open("sqlite", path) + if err != nil { + t.Fatal(err) + } + if _, err := db.Exec(`DROP TABLE context_scope_scrub_state`); err != nil { + t.Fatal(err) + } + if err := db.Close(); err != nil { + t.Fatal(err) + } + assertRejected("missing") + db, err = sql.Open("sqlite", path) + if err != nil { + t.Fatal(err) + } + if _, err := db.Exec(`DELETE FROM schema_migrations WHERE version=35`); err != nil { + t.Fatal(err) + } + if err := db.Close(); err != nil { + t.Fatal(err) + } + assertRejected("v34-missing") +} diff --git a/internal/store/context_manifest_test.go b/internal/store/context_manifest_test.go index 2259897..2200a42 100644 --- a/internal/store/context_manifest_test.go +++ b/internal/store/context_manifest_test.go @@ -128,6 +128,95 @@ func TestCandidateV34RawScopeColumnsAreScrubbedOnUpgrade(t *testing.T) { } } +func TestCandidateV34CommittedDropKeepsPendingScrubAcrossRestart(t *testing.T) { + path := filepath.Join(t.TempDir(), "candidate-crash.db") + st, err := OpenSQLite(path) + if err != nil { + t.Fatal(err) + } + m := extension.ProjectContextManifest{ProjectID: "p1", AttemptID: "crash-model", InputSHA256: "input", Delivery: "attempted"} + if err := st.AppendContextManifestWithScope("alice", "run-1", m, ContextManifestScope{Bound: true}); err != nil { + t.Fatal(err) + } + for _, query := range []string{ + `DELETE FROM schema_migrations WHERE version=35`, + `ALTER TABLE context_manifests ADD COLUMN resolve_goal TEXT NOT NULL DEFAULT ''`, + `ALTER TABLE context_manifests ADD COLUMN resolve_target_id TEXT NOT NULL DEFAULT ''`, + `UPDATE context_manifests SET resolve_goal='OLD_RESTRICTED_BODY',resolve_target_id='doc-private'`, + } { + if _, err := st.db.Exec(query); err != nil { + t.Fatal(err) + } + } + // Apply only the transactional DDL and ledger. A process crash at this + // boundary must not make the next open mistake the file for scrubbed. + if err := st.migratePass(); err != nil { + t.Fatal(err) + } + var pending int + if err := st.db.QueryRow(`SELECT pending FROM context_scope_scrub_state WHERE id=1`).Scan(&pending); err != nil || pending != 1 { + t.Fatalf("committed v35 lost physical scrub obligation: pending=%d err=%v", pending, err) + } + if err := st.Close(); err != nil { + t.Fatal(err) + } + restarted, err := OpenSQLite(path) + if err != nil { + t.Fatal(err) + } + defer restarted.Close() + if err := restarted.db.QueryRow(`SELECT pending FROM context_scope_scrub_state WHERE id=1`).Scan(&pending); err != nil || pending != 0 { + t.Fatalf("restart did not complete pending scrub: pending=%d err=%v", pending, err) + } + for _, suffix := range []string{"", "-wal"} { + data, err := os.ReadFile(path + suffix) + if err == nil && bytes.Contains(data, []byte("OLD_RESTRICTED_BODY")) { + t.Fatalf("restart left candidate raw bytes in SQLite %s", suffix) + } + } +} + +func TestEarlierV35WithoutMarkerIsConservativelyRescrubbed(t *testing.T) { + path := filepath.Join(t.TempDir(), "old-v35.db") + st, err := OpenSQLite(path) + if err != nil { + t.Fatal(err) + } + m := extension.ProjectContextManifest{ProjectID: "p1", AttemptID: "old-v35-model", InputSHA256: "input", Delivery: "attempted"} + if err := st.AppendContextManifestWithScope("alice", "run-1", m, ContextManifestScope{Bound: true}); err != nil { + t.Fatal(err) + } + for _, query := range []string{ + `ALTER TABLE context_manifests ADD COLUMN resolve_goal TEXT NOT NULL DEFAULT ''`, + `UPDATE context_manifests SET resolve_goal='OLD_RESTRICTED_BODY'`, + `UPDATE context_manifests SET resolve_goal=''`, + `ALTER TABLE context_manifests DROP COLUMN resolve_goal`, + `DROP TABLE context_scope_scrub_state`, + } { + if _, err := st.db.Exec(query); err != nil { + t.Fatal(err) + } + } + if err := st.Close(); err != nil { + t.Fatal(err) + } + restarted, err := OpenSQLite(path) + if err != nil { + t.Fatal(err) + } + defer restarted.Close() + var pending int + if err := restarted.db.QueryRow(`SELECT pending FROM context_scope_scrub_state WHERE id=1`).Scan(&pending); err != nil || pending != 0 { + t.Fatalf("earlier v35 without marker was not re-scrubbed: pending=%d err=%v", pending, err) + } + for _, suffix := range []string{"", "-wal"} { + data, err := os.ReadFile(path + suffix) + if err == nil && bytes.Contains(data, []byte("OLD_RESTRICTED_BODY")) { + t.Fatalf("old v35 raw bytes survived re-scrub in SQLite %s", suffix) + } + } +} + func TestCandidateV34PostgresRawScopeScrubAndFreshSchema(t *testing.T) { st := openTestStore(t) if st.Dialect() != "postgres" { diff --git a/internal/store/migrate.go b/internal/store/migrate.go index b9bb9af..5ca9f0d 100644 --- a/internal/store/migrate.go +++ b/internal/store/migrate.go @@ -881,8 +881,11 @@ func init() { }, }, migration{version: 35, name: "scrub_candidate_context_scope_text", - up: map[string][]string{"sqlite": {`SELECT 1`}, "postgres": {`SELECT 1`}}, - down: map[string][]string{"sqlite": {`SELECT 1`}, "postgres": {`SELECT 1`}}, + up: map[string][]string{ + "sqlite": {`CREATE TABLE IF NOT EXISTS context_scope_scrub_state(id INTEGER PRIMARY KEY CHECK(id=1),pending INTEGER NOT NULL CHECK(pending IN (0,1)))`}, + "postgres": {`SELECT 1`}, + }, + down: map[string][]string{"sqlite": {`DROP TABLE IF EXISTS context_scope_scrub_state`}, "postgres": {`SELECT 1`}}, }, ) } @@ -976,17 +979,17 @@ func (s *Store) migrate() error { if s.dialect.name() == "postgres" { return s.migratePG() } - var candidateRawColumns int - if err := s.db.QueryRow(`SELECT COUNT(*) FROM pragma_table_info('context_manifests') WHERE name IN ('resolve_goal','resolve_target_id')`).Scan(&candidateRawColumns); err != nil { - return err - } // SQLite:_txlock=immediate(OpenSQLite DSN)保证迁移事务在 BEGIN 即取写锁, // 跨进程迁移者按 busy_timeout 排队;后续版本读取发生在同一事务内, // 前一个迁移者提交后本进程看到的已是完整账本(无部分版本可见)。 if err := s.migratePass(); err != nil { return err } - if candidateRawColumns > 0 { + pending, err := s.ensureContextScrubState() + if err != nil { + return err + } + if pending { // The prior candidate schema may have stored full prompts. VACUUM is // outside the DDL transaction; it compacts the isolated SQLite file // after v35 drops those columns. Refuse to serve if compaction fails. @@ -1003,10 +1006,38 @@ func (s *Store) migrate() error { if busy != 0 { return errors.New("candidate context scrub checkpoint remains busy") } + if _, err := s.db.Exec(`UPDATE context_scope_scrub_state SET pending=0 WHERE id=1 AND pending=1`); err != nil { + return fmt.Errorf("candidate context scrub completion: %w", err) + } } return nil } +// ensureContextScrubState handles databases that ran the earlier candidate +// v35 before a durable marker existed. They are conservatively re-scrubbed. +func (s *Store) ensureContextScrubState() (bool, error) { + var rawColumns int + if err := s.db.QueryRow(`SELECT COUNT(*) FROM pragma_table_info('context_manifests') WHERE name IN ('resolve_goal','resolve_target_id')`).Scan(&rawColumns); err != nil { + return false, err + } + if rawColumns != 0 { + return false, errors.New("candidate context raw scope columns remain after migration") + } + if _, err := s.db.Exec(`CREATE TABLE IF NOT EXISTS context_scope_scrub_state(id INTEGER PRIMARY KEY CHECK(id=1),pending INTEGER NOT NULL CHECK(pending IN (0,1)))`); err != nil { + return false, err + } + // An absent row may be a partially initialized earlier candidate; mark it + // pending before any physical cleanup. A fresh v35 inserted pending=0. + if _, err := s.db.Exec(`INSERT OR IGNORE INTO context_scope_scrub_state(id,pending) VALUES(1,1)`); err != nil { + return false, err + } + var pending int + if err := s.db.QueryRow(`SELECT pending FROM context_scope_scrub_state WHERE id=1`).Scan(&pending); err != nil { + return false, err + } + return pending != 0, nil +} + // migratePG 以会话级咨询锁串行化并发迁移者(KTD8):锁在专用连接上获取, // 等锁者在轮到自己后重新读取账本——前一个迁移者已完成时本轮为空操作。 // 锁与迁移事务分离:迁移语句走连接池,锁只负责让 migrator 全局互斥。 @@ -1082,8 +1113,10 @@ func (s *Store) migratePass() error { if applied[m.version] { continue } + candidateRawScope := false if m.version == 35 { - if err := scrubCandidateContextScope(tx, s.dialect.name()); err != nil { + candidateRawScope, err = scrubCandidateContextScope(tx, s.dialect.name()) + if err != nil { return fmt.Errorf("migrate v35: %w", err) } } @@ -1096,6 +1129,15 @@ func (s *Store) migratePass() error { return fmt.Errorf("migrate v%d (%s): %w", m.version, m.name, err) } } + if m.version == 35 && s.dialect.name() == "sqlite" { + pending := 0 + if candidateRawScope { + pending = 1 + } + if _, err := tx.Exec(`INSERT OR REPLACE INTO context_scope_scrub_state(id,pending) VALUES(1,?)`, pending); err != nil { + return fmt.Errorf("migrate v35 scrub marker: %w", err) + } + } // 故障注入点:DDL 已执行、版本未登记——历史实现在此处崩溃会留下 // 「DDL 生效而账本缺失」的中间态;同事务设计下这里失败必须整体回滚。 if fault := s.takeMigrateFault(); fault != nil { @@ -1111,7 +1153,8 @@ func (s *Store) migratePass() error { // Candidate v34 was never released, but isolated test databases may already // contain raw Resolve goals. Fresh v34 creates no such columns; v35 removes // them conditionally from the earlier candidate schema before serving. -func scrubCandidateContextScope(tx *sql.Tx, dialect string) error { +func scrubCandidateContextScope(tx *sql.Tx, dialect string) (bool, error) { + found := false for _, column := range []string{"resolve_goal", "resolve_target_id"} { var exists int var err error @@ -1121,19 +1164,20 @@ func scrubCandidateContextScope(tx *sql.Tx, dialect string) error { err = tx.QueryRow(`SELECT COUNT(*) FROM information_schema.columns WHERE table_schema=current_schema() AND table_name='context_manifests' AND column_name=$1`, column).Scan(&exists) } if err != nil { - return err + return false, err } if exists == 0 { continue } + found = true if _, err := tx.Exec(`UPDATE context_manifests SET ` + column + `=''`); err != nil { - return err + return false, err } if _, err := tx.Exec(`ALTER TABLE context_manifests DROP COLUMN ` + column); err != nil { - return err + return false, err } } - return nil + return found, nil } func latestMigrationVersion() int { From 130ac344aaba988026fbc497c9548cdb59f5774f Mon Sep 17 00:00:00 2001 From: weichen Date: Wed, 14 Oct 2026 06:15:01 +0000 Subject: [PATCH 12/15] Guard every PostgreSQL command audit write Why: Explicit low IDs bypassed the implicit sequence allocator and could commit after a higher audit page cutoff. Evidence: Isolated PostgreSQL RED receipt for cross-page omission; local GREEN for explicit and default writes, trigger drift, isolation, mutation denial, offline copy rollback, retry, and sequence non-rewind. Fixed-HEAD receipts follow. Impact: Add v34 all-writer and append-only guards; reject unsafe strong reads; make command audit storage migration exclusive and atomic with guard restoration and a forward-only sequence. Refs: Approved W9 all-writers cutoff adjustment r1; W9 Backend task card. --- .task/feature-rdm-two-trees-project-audit.md | 8 +- cmd/storage-migrate/integration_test.go | 99 +++++++++++++ cmd/storage-migrate/main.go | 103 ++++++++++++- internal/app/project_audit_test.go | 137 ++++++++++++++++++ internal/store/audit.go | 5 +- internal/store/migrate.go | 78 ++++++++++ internal/store/migrate_compat.go | 1 + internal/store/project_audit_sequence_test.go | 114 ++++++++++++++- pkg/extension/extension.go | 4 +- pkg/extension/project_audit.go | 3 +- 10 files changed, 537 insertions(+), 15 deletions(-) diff --git a/.task/feature-rdm-two-trees-project-audit.md b/.task/feature-rdm-two-trees-project-audit.md index 6eb2de3..04598fb 100644 --- a/.task/feature-rdm-two-trees-project-audit.md +++ b/.task/feature-rdm-two-trees-project-audit.md @@ -6,6 +6,7 @@ - Startup receipt: `/tmp/backend-w9-precheck-20261014-01.json` (SELF, exit 2, `SYNC_REQUIRED` for the separate Dev checkout `feature/rdm-increment-acceptance-note`, six commits behind Dev `origin/main`). This Backend task uses the independent-development path in Backend `AGENTS.md`; it does not write that Dev checkout or its task card. The receipt is preserved as a failure, not relabeled READY. - Approved affected design: `rdm-two-trees-stage6-audit-link-design-adjustment-r1.md`, SHA256 `385d9d7570217ddcce9fe60a60c99dc58c9a4875ad790584a724d09a210a6e52`; user approval in the task session covers this local Backend increment, not tag publication or Stage 7 acceptance. - Approved PostgreSQL cutoff adjustment: `rdm-two-trees-stage6-w9-pg-audit-cutoff-adjustment-r1.md`, SHA256 `2357b715a7c8f3451b34cc25a578d655e8a48f3f26d9cf7561ea7a13998ca857`; scope is isolated Backend implementation/testing, with no real database migration or release authorization. +- Approved all-writers affected design: `rdm-two-trees-stage6-w9-all-writers-cutoff-adjustment-r1.md`, SHA256 `44f6f7cd34bf95a9c1eb844bfdd70a5c2233e1b5ffc89b3091359dcb5fc3b5d8`; the direct user approval allows the local v34 correction and isolated tests only. ## Implemented local slice @@ -17,7 +18,9 @@ - Independent review of `a8fa2fe` found two P2 security defects: clock-based operation IDs could collide; and `NewScopedHost(owner,"rdm")` let another compiled plugin self-report the publisher's plugin ID. The new local increment uses 192 bits from `crypto/rand` for each host-generated operation ID. Both zero-offset and explicit-offset publication require a unique committed row for owner/project/operation; a historical duplicate rejects publication, including attempts to link another private command. Verifier registration returns a private `ProjectAuditPublisherFactory` for the registering plugin's background outbox; a generic scoped Host has no publisher method. Fixed-clock concurrent IDs, forged params, ambiguous private offset, forged scoped Host, and legitimate registered publisher each have focused tests. - The minted publisher exposes only `ProjectAuditPublisher`, not the full owner-scoped `Host`; tests reject attempts to use `NewScopedHost` with a self-reported `rdm` string as publisher authority. A publisher is a registration-time capability, while `IdempotencyKey` remains the separate retry identity. - Exact-source linked reads do not enumerate hidden links or reveal raw payload, private conversation ID, or content. Body disclosure and a broad project link list are not implemented; the public coverage reports the narrower capability. -- A PostgreSQL-only v33 default function now takes a table-specific transaction advisory lock before assigning `command_audit.id`, making supported implicit-ID writes commit in source-offset order. Migration and each PG append/project page verify CACHE 1, ascending non-cyclic positive IDs, the exact guarded default expression, and the next ID above the committed maximum; unsafe drift fails closed. SQLite remains single-writer. The owner-only project page reports `committed-prefix` only after the runtime check. Explicit online IDs and sequence maintenance remain prohibited; offline `storage-migrate` advances the owned sequence after copying IDs. +- A PostgreSQL-only v33 default function takes a table-specific transaction advisory lock before assigning `command_audit.id`, making supported implicit-ID writes commit in source-offset order. Migration and each PG append/project page verify CACHE 1, ascending non-cyclic positive IDs, the exact guarded default expression, and the next ID above the committed maximum; unsafe drift fails closed. SQLite remains single-writer. The owner-only project page reports `committed-prefix` only after runtime checks. v34 below closes the explicit-ID bypass; online sequence maintenance remains prohibited. +- Isolated PG RED receipt `/tmp/backend-w9-explicit-low-red-20261014-01.json` proved v33 still misreported `committed-prefix`: explicit ID 3 committed after normal ID 4 and disappeared across pages. Local v34 adds `ENABLE ALWAYS` insert and append-only mutation triggers. Every online INSERT checks READ COMMITTED, obtains the same transaction lock, and rejects a nonpositive, nonmonotone, or beyond-reserved ID. UPDATE/DELETE reject. PG append and every strong project page verify both trigger identities, exact event shape, and enablement; the existing default allocator also checks this guard. This is a local candidate, not yet independently reviewed or deployed. +- `storage-migrate` now copies `command_audit` within one ACCESS EXCLUSIVE transaction: it disables only the named insert guard, copies explicit IDs, verifies count and checksum, advances the owned sequence only forward, restores and verifies the guard, and records done before commit. Failure rolls back table data, trigger state, and done; nontransactional `setval` may leave a safe forward gap. Idempotent done checks guard health before reporting success. Other tables retain their existing path. ## Verification and remaining work @@ -31,5 +34,6 @@ - Historical pre-commit SELF load receipt `/tmp/backend-w9-pg-load-precommit-20261014-01.json` captured an initial v33 variant whose unlocked status function later produced false `cutoff unsafe` errors under concurrency; it is **not** valid performance acceptance evidence. The corrected status check reads committed MAX before non-MVCC sequence state and does not lock readers; the default ID allocator alone holds the transaction advisory lock and rechecks after `nextval`. `TestProjectAuditConcurrentReadWriteNeverReportsFalseDrift` and repeated PG load/cross-page runs protect that ordering. Three-condition pre-commit SELF raw receipts: `/tmp/backend-w9-pg-load-readlock-precommit-20261014-01.json` uses a test-only locked status wrapper, while `/tmp/backend-w9-pg-load-unlocked2-precommit-20261014-01.json` uses the final MAX-first unlocked status; each run also logs its v32 direct-INSERT baseline, PostgreSQL 18.6 version, 192 writes per mode/8 workers, elapsed time, p95, and throughput. The wrapper adds overhead and all measurements vary with hardware/load; no performance threshold has been approved, so they are Stage 6 cost evidence only. - Pre-commit SELF `/tmp/backend-w9-default-drift-red-20261014-01.json` showed that a replacement default containing the guarded function name but executing unguarded `nextval` was accepted; `/tmp/backend-w9-default-drift-green-20261014-01.json` verifies exact-expression rejection. Pre-commit SELF `/tmp/backend-w9-precommit-all-20261014-02.json` and `/tmp/backend-w9-precommit-pg-20261014-02.json` passed before this last guard tightening; fixed-HEAD runs are required below. - After the guard tightening, pre-commit SELF receipts `/tmp/backend-w9-precommit-all-final-20261014-01.json` (`go test ./... -count=1`), `/tmp/backend-w9-precommit-pg-final-20261014-01.json` (isolated PG store/storage-migrate), and `/tmp/backend-w9-precommit-pg-race-20261014-01.json` (cross-page/concurrent read-write with race detector) all exited 0. These still identify the dirty pre-commit tree, so they do not replace fixed-HEAD verification. -- Integration constraint: this branch uses schema v31 (audit attribution), v32 (links), and v33 (PG cutoff), while the independent W6 Backend branch also has a local v31. Neither branch has consumed the other's unmerged work. After the first branch lands, the later branch owner must sync the new main, renumber its unpublished migrations without rewriting an already released version, and rerun fresh/old SQLite and PG migration plus combined regression on the actual merged source. These isolated tests do not prove W6+W9 compatibility. +- v34 pre-commit SELF `/tmp/backend-w9-allwriters-load-precommit-20261014-01.json` records isolated PostgreSQL 18.6, three runs of 192 writes per mode with 8 workers, comparing v32 direct INSERT with v34 guarded `Store.AppendAudit`; it also logs three mixed runs of 180 writes with 6 writers and 160 200-row pages with 4 readers. Protected write throughput ranged about 2.7–3.0k/s, write p95 6.4–6.6ms, and mixed large-page p95 1.2–1.9ms on this one host. These are SELF cost signals with no approved threshold and no Stage 7 acceptance meaning; fixed-HEAD tests still follow. +- Integration constraint: this branch uses schema v31 (audit attribution), v32 (links), v33 (PG implicit cutoff), and v34 (all-writer guard), while the independent W6 Backend branch also has a local v31. Neither branch has consumed the other's unmerged work. After the first branch lands, the later branch owner must sync the new main, renumber its unpublished migrations without rewriting an already released version, and rerun fresh/old SQLite and PG migration plus combined regression on the actual merged source. These isolated tests do not prove W6+W9 compatibility. - Required next W9 work: real RDM committed-event verifier/outbox consumer, Backend project-event source, run ID propagation, model/tool/background receipts, body/detail disclosure, broad authorized linked-operation pagination, history/export, and combined product verification remain unfinished. A domain commit followed by a lost Backend audit append leaves the link pending; recovery must retain and reconcile that unknown state without claiming a host receipt. Exact-link and owner-only paging do not satisfy Stage 7 audit acceptance. PostgreSQL v33 adds a global command-audit append lock; its measured wait/throughput cost has no approved performance threshold and needs human Stage 6 review. diff --git a/cmd/storage-migrate/integration_test.go b/cmd/storage-migrate/integration_test.go index 57cb9f0..7df23a7 100644 --- a/cmd/storage-migrate/integration_test.go +++ b/cmd/storage-migrate/integration_test.go @@ -101,6 +101,105 @@ func TestMigrateEndToEndAndIdempotent(t *testing.T) { } } +func TestCommandAuditExclusiveCopyRollsBackAndNeverRewindsSequence(t *testing.T) { + target, cleanup := pgSchemaDSN(t) + defer cleanup() + fixture := buildFixture(t) + src, err := openSourceRO(fixture) + if err != nil { + t.Fatal(err) + } + defer src.Close() + st, err := store.OpenPostgres(target, filepath.Join(t.TempDir(), "artifacts")) + if err != nil { + t.Fatal(err) + } + defer st.Close() + db := st.DB() + if _, err := db.Exec(controlDDL); err != nil { + t.Fatal(err) + } + cols, pk, err := targetColumns(db, "command_audit") + if err != nil { + t.Fatal(err) + } + bools, err := boolColumns(db, "command_audit", cols) + if err != nil { + t.Fatal(err) + } + count, checksum, err := sourceScan(src, "command_audit", cols, pk, bools) + if err != nil { + t.Fatal(err) + } + blocker, err := db.Begin() + if err != nil { + t.Fatal(err) + } + var existing int + if err := blocker.QueryRow(`SELECT COUNT(*) FROM command_audit`).Scan(&existing); err != nil { + t.Fatal(err) + } + timeoutDB, err := sql.Open("pgx", store.AppendPGRuntimeParam(target, "lock_timeout", "100")) + if err != nil { + t.Fatal(err) + } + if _, err := migrateCommandAudit(timeoutDB, src, "retry-audit", cols, pk, bools, count, checksum); err == nil || !strings.Contains(err.Error(), "lock timeout") { + t.Fatalf("exclusive copy did not fail on a held reader: %v", err) + } + timeoutDB.Close() + if err := blocker.Rollback(); err != nil { + t.Fatal(err) + } + if _, err := migrateCommandAudit(db, src, "retry-audit", cols, pk, bools, count+1, checksum); err == nil { + t.Fatal("mismatched copy committed despite in-transaction target count check") + } + var rows, done int + if err := db.QueryRow(`SELECT COUNT(*) FROM command_audit`).Scan(&rows); err != nil || rows != 0 { + t.Fatalf("failed copy left rows: %d %v", rows, err) + } + if err := db.QueryRow(`SELECT COUNT(*) FROM migration_table_state WHERE table_name='command_audit'`).Scan(&done); err != nil || done != 0 { + t.Fatalf("failed copy recorded done: %d %v", done, err) + } + var enabled string + if err := db.QueryRow(`SELECT tgenabled FROM pg_trigger WHERE tgrelid='command_audit'::regclass AND tgname='project_audit_insert_guard_v1'`).Scan(&enabled); err != nil || enabled != "A" { + t.Fatalf("failed copy left guard disabled: %q %v", enabled, err) + } + var seq string + if err := db.QueryRow(`SELECT pg_get_serial_sequence('command_audit','id')`).Scan(&seq); err != nil { + t.Fatal(err) + } + // setval is non-transactional; an interrupted offline attempt can leave a + // forward gap even though its rows and trigger-state changes roll back. + tx, err := db.Begin() + if err != nil { + t.Fatal(err) + } + if _, err := tx.Exec(`LOCK TABLE command_audit IN ACCESS EXCLUSIVE MODE`); err != nil { + t.Fatal(err) + } + if _, err := tx.Exec(`ALTER TABLE command_audit DISABLE TRIGGER project_audit_insert_guard_v1`); err != nil { + t.Fatal(err) + } + if _, err := tx.Exec(`SELECT setval($1::regclass,50,true)`, seq); err != nil { + t.Fatal(err) + } + if err := tx.Rollback(); err != nil { + t.Fatal(err) + } + if _, err := migrateCommandAudit(db, src, "retry-audit", cols, pk, bools, count, checksum); err != nil { + t.Fatalf("retry after rollback: %v", err) + } + var last int64 + if err := db.QueryRow(`SELECT last_value FROM ` + seq).Scan(&last); err != nil || last < 50 { + t.Fatalf("copy rewound sequence: last=%d err=%v", last, err) + } + var nextID int64 + if err := db.QueryRow(`INSERT INTO command_audit(time,command_id,source,entrance,state,operation_id,conversation_id,owner,payload) + VALUES ('2026-10-14T00:00:00Z','after-copy','','','committed','','','alice','{}') RETURNING id`).Scan(&nextID); err != nil || nextID <= 50 { + t.Fatalf("post-copy ID did not advance: id=%d err=%v", nextID, err) + } +} + func TestMigrateResumeAfterInterrupt(t *testing.T) { target, cleanup := pgSchemaDSN(t) defer cleanup() diff --git a/cmd/storage-migrate/main.go b/cmd/storage-migrate/main.go index 3a4f366..65b571c 100644 --- a/cmd/storage-migrate/main.go +++ b/cmd/storage-migrate/main.go @@ -508,9 +508,18 @@ func migrateTable(db *sql.DB, src *sql.DB, migrationID, table string) (map[strin if countNow != srcCount || sumNow != srcChecksum { return nil, fmt.Errorf("源与登记不符(疑似换快照):count %d/%d checksum %s…/%s…", countNow, srcCount, sumNow[:12], srcChecksum[:12]) } + if table == "command_audit" { + var guarded bool + if err := db.QueryRow(`SELECT project_audit_cutoff_safe('command_audit'::regclass) AND project_audit_writers_guarded('command_audit'::regclass)`).Scan(&guarded); err != nil || !guarded { + return nil, fmt.Errorf("command_audit idempotent guard is unsafe: safe=%v err=%v", guarded, err) + } + } entry := map[string]any{"table": table, "source_count": countNow, "target_count": dstCountStored, "checksum": sumNow, "state": "done(idempotent)"} return entry, nil } + if table == "command_audit" { + return migrateCommandAudit(db, src, migrationID, cols, pk, bools, countNow, sumNow) + } if err := copyRows(db, src, table, cols); err != nil { return nil, err } @@ -663,7 +672,11 @@ func sourceScan(src *sql.DB, table string, cols, pk []string, bools map[string]b return scanChecksum(rows, cols, bools) } -func targetScan(db *sql.DB, table string, cols, pk []string, bools map[string]bool) (int64, string, error) { +type rowsQuerier interface { + Query(string, ...any) (*sql.Rows, error) +} + +func targetScan(db rowsQuerier, table string, cols, pk []string, bools map[string]bool) (int64, string, error) { rows, err := db.Query(`SELECT ` + quoteJoin(cols, `"`) + ` FROM "` + table + `" ORDER BY ` + quoteJoin(pk, `"`)) if err != nil { return 0, "", err @@ -686,6 +699,18 @@ func copyRows(db *sql.DB, src *sql.DB, table string, cols []string) error { if err != nil { return err } + tx, err := db.Begin() + if err != nil { + return err + } + defer tx.Rollback() + if err := copyRowsInTx(tx, src, table, cols, bools); err != nil { + return err + } + return tx.Commit() +} + +func copyRowsInTx(tx *sql.Tx, src *sql.DB, table string, cols []string, bools map[string]bool) error { colList := quoteJoin(cols, `"`) ph := make([]string, len(cols)) for i := range ph { @@ -702,11 +727,6 @@ func copyRows(db *sql.DB, src *sql.DB, table string, cols []string) error { for i := range vals { ptrs[i] = &vals[i] } - tx, err := db.Begin() - if err != nil { - return err - } - defer tx.Rollback() stmt, err := tx.Prepare(fmt.Sprintf(`INSERT INTO "%s" (%s) VALUES (%s) ON CONFLICT DO NOTHING`, table, colList, placeholders)) if err != nil { return err @@ -734,7 +754,76 @@ func copyRows(db *sql.DB, src *sql.DB, table string, cols []string) error { if err := srcRows.Err(); err != nil { return err } - return tx.Commit() + return nil +} + +// command_audit alone needs an offline transaction: explicit source IDs do not +// pass the online monotone guard, and disabling it must never become visible +// separately from the copied rows, sequence advance, or done marker. +func migrateCommandAudit(db *sql.DB, src *sql.DB, migrationID string, cols, pk []string, bools map[string]bool, sourceCount int64, sourceChecksum string) (map[string]any, error) { + tx, err := db.Begin() + if err != nil { + return nil, err + } + defer tx.Rollback() + if _, err := tx.Exec(`LOCK TABLE command_audit IN ACCESS EXCLUSIVE MODE`); err != nil { + return nil, err + } + if _, err := tx.Exec(`ALTER TABLE command_audit DISABLE TRIGGER project_audit_insert_guard_v1`); err != nil { + return nil, err + } + if err := copyRowsInTx(tx, src, "command_audit", cols, bools); err != nil { + return nil, err + } + targetCount, targetChecksum, err := targetScan(tx, "command_audit", cols, pk, bools) + if err != nil { + return nil, err + } + if targetCount != sourceCount || targetChecksum != sourceChecksum { + return nil, fmt.Errorf("command_audit copy mismatch: count %d/%d checksum %s/%s", targetCount, sourceCount, targetChecksum, sourceChecksum) + } + if err := advanceCommandAuditSequence(tx); err != nil { + return nil, err + } + if _, err := tx.Exec(`ALTER TABLE command_audit ENABLE ALWAYS TRIGGER project_audit_insert_guard_v1`); err != nil { + return nil, err + } + var guarded bool + if err := tx.QueryRow(`SELECT project_audit_cutoff_safe('command_audit'::regclass) AND project_audit_writers_guarded('command_audit'::regclass)`).Scan(&guarded); err != nil || !guarded { + return nil, fmt.Errorf("command_audit guard not restored: safe=%v err=%v", guarded, err) + } + if _, err := tx.Exec(`INSERT INTO migration_table_state (migration_id, table_name, source_count, target_count, checksum, state, finished_at) + VALUES ($1,'command_audit',$2,$3,$4,'done',$5) + ON CONFLICT(migration_id, table_name) DO UPDATE SET source_count=excluded.source_count, target_count=excluded.target_count, checksum=excluded.checksum, state=excluded.state, finished_at=excluded.finished_at`, + migrationID, sourceCount, targetCount, sourceChecksum, nowISO()); err != nil { + return nil, err + } + if err := tx.Commit(); err != nil { + return nil, err + } + return map[string]any{"table": "command_audit", "source_count": sourceCount, "target_count": targetCount, "checksum": sourceChecksum, "state": "done"}, nil +} + +func advanceCommandAuditSequence(tx *sql.Tx) error { + var seq string + if err := tx.QueryRow(`SELECT pg_get_serial_sequence('command_audit','id')`).Scan(&seq); err != nil || seq == "" { + return fmt.Errorf("command_audit sequence missing: %v", err) + } + var last int64 + var called bool + if err := tx.QueryRow(fmt.Sprintf(`SELECT last_value,is_called FROM %s`, seq)).Scan(&last, &called); err != nil { + return err + } + var maxID sql.NullInt64 + if err := tx.QueryRow(`SELECT MAX(id) FROM command_audit`).Scan(&maxID); err != nil { + return err + } + if maxID.Valid && (maxID.Int64 > last || (maxID.Int64 == last && !called)) { + if _, err := tx.Exec(`SELECT setval($1::regclass,$2,true)`, seq, maxID.Int64); err != nil { + return err + } + } + return nil } func sqliteBool(v any) bool { diff --git a/internal/app/project_audit_test.go b/internal/app/project_audit_test.go index 3640da9..7bda52b 100644 --- a/internal/app/project_audit_test.go +++ b/internal/app/project_audit_test.go @@ -2,8 +2,10 @@ package app import ( "context" + "database/sql" "encoding/json" "errors" + "os" "path/filepath" "strings" "testing" @@ -99,3 +101,138 @@ func TestProjectAuditHostChecksDomainReadAndProjectsStoredReceipts(t *testing.T) t.Fatalf("revoked audit.read: %v", err) } } + +// A direct explicit ID bypasses the v33 default allocator. This must never +// yield a committed-prefix page that permanently skips the late lower row. +func TestProjectAuditPostgresExplicitLowIDCannotHideAcrossPages(t *testing.T) { + dsn := os.Getenv("AGENT_PG_TEST_DSN") + if dsn == "" { + t.Skip("AGENT_PG_TEST_DSN unset: isolated PostgreSQL check NOT_RUN") + } + admin, err := sql.Open("pgx", dsn) + if err != nil { + t.Fatal(err) + } + t.Cleanup(func() { admin.Close() }) + if _, err := admin.Exec(`CREATE EXTENSION IF NOT EXISTS pg_trgm`); err != nil { + t.Fatal(err) + } + schema := "w9_" + strings.ToLower(string(store.NewID("audit"))) + if _, err := admin.Exec(`CREATE SCHEMA ` + schema); err != nil { + t.Fatal(err) + } + t.Cleanup(func() { admin.Exec(`DROP SCHEMA IF EXISTS ` + schema + ` CASCADE`) }) + st, err := store.OpenPostgres(store.AppendPGRuntimeParam(dsn, "search_path", schema+",public"), filepath.Join(t.TempDir(), "artifacts")) + if err != nil { + t.Fatal(err) + } + defer st.Close() + if err := st.EnsureUser("alice", "Alice"); err != nil { + t.Fatal(err) + } + project, err := st.CreateProject("alice", "A") + if err != nil { + t.Fatal(err) + } + conversation, err := st.CreateConversation("alice", string(project.ID), "work", "") + if err != nil { + t.Fatal(err) + } + for _, command := range []string{"first", "second"} { + if err := st.AppendAudit("alice", agentcommand.AuditEnvelope{Time: time.Now(), Actor: "alice", CommandID: command, ConversationID: string(conversation.ID), State: agentcommand.StateCommitted}); err != nil { + t.Fatal(err) + } + } + insert := `INSERT INTO command_audit(time,command_id,source,entrance,state,operation_id,conversation_id,owner,payload,actor_name,project_id) + VALUES ($1,$2,'','','committed','',$3,'alice',$4,'Alice',$5) RETURNING id` + gap, err := st.DB().Begin() + if err != nil { + t.Fatal(err) + } + defer gap.Rollback() + var gapID int64 + if err := gap.QueryRow(insert, time.Now().UTC().Format(time.RFC3339Nano), "rolled-back-gap", string(conversation.ID), `{}`, string(project.ID)).Scan(&gapID); err != nil { + t.Fatal(err) + } + if gapID != 3 { + t.Fatalf("rollback gap ID=%d want=3", gapID) + } + if err := gap.Rollback(); err != nil { + t.Fatal(err) + } + a, err := st.DB().Begin() + if err != nil { + t.Fatal(err) + } + defer a.Rollback() + env := agentcommand.AuditEnvelope{Time: time.Now(), Actor: "alice", CommandID: "explicit-late", ConversationID: string(conversation.ID), State: agentcommand.StateCommitted} + raw, err := json.Marshal(env) + if err != nil { + t.Fatal(err) + } + _, err = a.Exec(`INSERT INTO command_audit(id,time,command_id,source,entrance,state,operation_id,conversation_id,owner,payload,actor_name,project_id) + VALUES (3,$1,$2,'','','committed','',$3,'alice',$4,'Alice',$5)`, env.Time.UTC().Format(time.RFC3339Nano), env.CommandID, string(conversation.ID), string(raw), string(project.ID)) + if err != nil { + // Rejecting the online explicit ID is also a safe outcome. + t.Logf("online explicit low ID rejected: %v", err) + return + } + slot := &projectAuditSlot{} + caps := hostcaps{st: st, projectAudit: slot} + reg := pluginRegistry{pluginID: "rdm", caps: caps} + if err := reg.RegisterProjectAuditAuthorizer(func(actor string, projectID core.ID) (bool, error) { + return actor == "alice" && projectID == project.ID, nil + }); err != nil { + t.Fatal(err) + } + h := inprocHost{caps: caps, owner: "alice", actor: "alice", pluginID: "rdm"} + committed := make(chan error, 1) + started := make(chan struct{}) + go func() { + close(started) + committed <- st.AppendAudit("alice", agentcommand.AuditEnvelope{Time: time.Now(), Actor: "alice", CommandID: "implicit-high", ConversationID: string(conversation.ID), State: agentcommand.StateCommitted}) + }() + <-started + var first extension.ProjectAuditPage + highCommittedEarly := false + select { + case err := <-committed: + if err != nil { + t.Fatal(err) + } + highCommittedEarly = true + case <-time.After(500 * time.Millisecond): + } + if highCommittedEarly { + first, err = h.QueryProjectAudit(context.Background(), extension.ProjectAuditQuery{ProjectID: project.ID, Limit: 2}) + if err != nil { + t.Fatal(err) + } + if len(first.Receipts) != 2 || first.Receipts[0].SourceOffset != 4 || first.Receipts[1].SourceOffset != 2 || first.CutoffOffset != 4 || first.NextOffset != 2 || first.Coverage.CutoffConsistency != "committed-prefix" { + t.Fatalf("counterexample first page changed: %+v", first) + } + } + if err := a.Commit(); err != nil { + t.Fatal(err) + } + if !highCommittedEarly { + if err := <-committed; err != nil { + t.Fatal(err) + } + first, err = h.QueryProjectAudit(context.Background(), extension.ProjectAuditQuery{ProjectID: project.ID, Limit: 2}) + if err != nil { + t.Fatal(err) + } + } + second, err := h.QueryProjectAudit(context.Background(), extension.ProjectAuditQuery{ProjectID: project.ID, BeforeOffset: first.NextOffset, CutoffOffset: first.CutoffOffset, Limit: 2}) + if err != nil { + t.Fatal(err) + } + seen := map[int64]bool{} + for _, receipt := range append(first.Receipts, second.Receipts...) { + seen[receipt.SourceOffset] = true + } + if !seen[3] || len(seen) != 4 { + t.Fatalf("committed-prefix omitted explicit low ID 3: early=%v first=%+v second=%+v", highCommittedEarly, first, second) + } +} diff --git a/internal/store/audit.go b/internal/store/audit.go index 6894c02..9b41a1c 100644 --- a/internal/store/audit.go +++ b/internal/store/audit.go @@ -38,7 +38,8 @@ type ProjectCommandAuditRow struct { } // ErrAuditCutoffUnsafe prevents a PostgreSQL page from claiming a fixed cutoff -// when the installed ID generator or sequence no longer proves commit order. +// when the ID generator, sequence, or all-writer database guard cannot prove +// commit order. var ErrAuditCutoffUnsafe = errors.New("store: project audit cutoff unsafe") func (s *Store) requireProjectAuditCommittedPrefix() error { @@ -46,7 +47,7 @@ func (s *Store) requireProjectAuditCommittedPrefix() error { return nil } // SQLite serializes writers. var safe bool - if err := s.db.QueryRow(`SELECT project_audit_cutoff_safe('command_audit'::regclass)`).Scan(&safe); err != nil { + if err := s.db.QueryRow(`SELECT project_audit_cutoff_safe('command_audit'::regclass) AND project_audit_writers_guarded('command_audit'::regclass)`).Scan(&safe); err != nil { return fmt.Errorf("%w: %v", ErrAuditCutoffUnsafe, err) } if !safe { diff --git a/internal/store/migrate.go b/internal/store/migrate.go index 24a9763..2ab5a1c 100644 --- a/internal/store/migrate.go +++ b/internal/store/migrate.go @@ -919,6 +919,84 @@ func init() { `DROP FUNCTION project_audit_cutoff_safe(regclass)`, }, }}, + migration{version: 34, name: "project_audit_all_writers_guard", + up: map[string][]string{ + "sqlite": {`SELECT 1`}, + "postgres": { + `LOCK TABLE command_audit IN ACCESS EXCLUSIVE MODE`, + `CREATE FUNCTION project_audit_insert_guard_v1() RETURNS trigger LANGUAGE plpgsql VOLATILE AS $$ + DECLARE seq regclass; last_number numeric; was_called boolean; max_id bigint; + BEGIN + IF current_setting('transaction_isolation') <> 'read committed' THEN + RAISE EXCEPTION 'command audit insert requires read committed isolation'; + END IF; + PERFORM pg_advisory_xact_lock(715328491,TG_RELID::int4); + IF NOT project_audit_cutoff_safe(TG_RELID) OR NOT project_audit_writers_guarded(TG_RELID) THEN + RAISE EXCEPTION 'command audit write guard is unsafe'; + END IF; + seq := pg_get_serial_sequence(TG_RELID::regclass::text,'id')::regclass; + EXECUTE format('SELECT last_value,is_called FROM %s',seq) INTO last_number,was_called; + EXECUTE format('SELECT COALESCE(MAX(id),0) FROM %s',TG_RELID::regclass) INTO max_id; + IF NEW.id IS NULL OR NEW.id<=0 OR NEW.id<=max_id OR NOT was_called OR NEW.id>last_number THEN + RAISE EXCEPTION 'command audit ID is not commit-monotone'; + END IF; + RETURN NEW; + END $$`, + `CREATE FUNCTION project_audit_mutation_guard_v1() RETURNS trigger LANGUAGE plpgsql AS $$ + BEGIN RAISE EXCEPTION 'command audit is append-only'; END $$`, + `CREATE TRIGGER project_audit_insert_guard_v1 BEFORE INSERT ON command_audit FOR EACH ROW EXECUTE FUNCTION project_audit_insert_guard_v1()`, + `CREATE TRIGGER project_audit_mutation_guard_v1 BEFORE UPDATE OR DELETE ON command_audit FOR EACH ROW EXECUTE FUNCTION project_audit_mutation_guard_v1()`, + `ALTER TABLE command_audit ENABLE ALWAYS TRIGGER project_audit_insert_guard_v1`, + `ALTER TABLE command_audit ENABLE ALWAYS TRIGGER project_audit_mutation_guard_v1`, + `CREATE FUNCTION project_audit_writers_guarded(target regclass) RETURNS boolean LANGUAGE plpgsql VOLATILE AS $$ + DECLARE insert_ok boolean; mutation_ok boolean; + BEGIN + SELECT EXISTS (SELECT 1 FROM pg_trigger WHERE tgrelid=target AND tgname='project_audit_insert_guard_v1' + AND NOT tgisinternal AND tgenabled='A' AND tgtype=7 + AND tgfoid=to_regprocedure('project_audit_insert_guard_v1()')) INTO insert_ok; + SELECT EXISTS (SELECT 1 FROM pg_trigger WHERE tgrelid=target AND tgname='project_audit_mutation_guard_v1' + AND NOT tgisinternal AND tgenabled='A' AND tgtype=27 + AND tgfoid=to_regprocedure('project_audit_mutation_guard_v1()')) INTO mutation_ok; + RETURN insert_ok AND mutation_ok; + END $$`, + `CREATE OR REPLACE FUNCTION next_command_audit_id(target regclass) RETURNS bigint LANGUAGE plpgsql VOLATILE AS $$ + DECLARE seq regclass; allocated bigint; max_id bigint; + BEGIN + PERFORM pg_advisory_xact_lock(715328491,target::oid::int4); + IF NOT project_audit_cutoff_safe(target) OR NOT project_audit_writers_guarded(target) THEN + RAISE EXCEPTION 'project audit cutoff guard is unsafe'; + END IF; + seq := pg_get_serial_sequence(target::text,'id')::regclass; + allocated := nextval(seq); + EXECUTE format('SELECT COALESCE(MAX(id),0) FROM %s',target) INTO max_id; + IF allocated<=0 OR allocated<=max_id THEN RAISE EXCEPTION 'project audit ID did not advance beyond committed rows'; END IF; + RETURN allocated; + END $$`, + `DO $$ BEGIN IF NOT project_audit_cutoff_safe('command_audit'::regclass) OR NOT project_audit_writers_guarded('command_audit'::regclass) THEN RAISE EXCEPTION 'project audit writer guard migration precondition failed'; END IF; END $$`, + }, + }, + down: map[string][]string{ + "sqlite": {`SELECT 1`}, + "postgres": { + `LOCK TABLE command_audit IN ACCESS EXCLUSIVE MODE`, + `CREATE OR REPLACE FUNCTION next_command_audit_id(target regclass) RETURNS bigint LANGUAGE plpgsql VOLATILE AS $$ + DECLARE seq regclass; allocated bigint; max_id bigint; + BEGIN + PERFORM pg_advisory_xact_lock(715328491,target::oid::int4); + IF NOT project_audit_cutoff_safe(target) THEN RAISE EXCEPTION 'project audit cutoff sequence is unsafe'; END IF; + seq := pg_get_serial_sequence(target::text,'id')::regclass; + allocated := nextval(seq); + EXECUTE format('SELECT COALESCE(MAX(id),0) FROM %s',target) INTO max_id; + IF allocated<=0 OR allocated<=max_id THEN RAISE EXCEPTION 'project audit ID did not advance beyond committed rows'; END IF; + RETURN allocated; + END $$`, + `DROP TRIGGER project_audit_insert_guard_v1 ON command_audit`, + `DROP TRIGGER project_audit_mutation_guard_v1 ON command_audit`, + `DROP FUNCTION project_audit_insert_guard_v1()`, + `DROP FUNCTION project_audit_mutation_guard_v1()`, + `DROP FUNCTION project_audit_writers_guarded(regclass)`, + }, + }}, ) } diff --git a/internal/store/migrate_compat.go b/internal/store/migrate_compat.go index a653f3b..a4f7766 100644 --- a/internal/store/migrate_compat.go +++ b/internal/store/migrate_compat.go @@ -38,6 +38,7 @@ var migrationBreaksOldReaders = map[int]bool{ 31: false, // command_audit_attribution_snapshot:仅加列/索引,旧读者不受影响 32: false, // project_audit_committed_links:新表,旧读者不受影响 33: false, // project_audit_committed_prefix:PG 审计 ID 默认函数,旧读者兼容 + 34: false, // project_audit_all_writers_guard:PG 在线审计写入门禁,旧读者兼容 } // SchemaTarget 是本二进制会迁移到的 schema 版本。 diff --git a/internal/store/project_audit_sequence_test.go b/internal/store/project_audit_sequence_test.go index 022f2b8..5742ffc 100644 --- a/internal/store/project_audit_sequence_test.go +++ b/internal/store/project_audit_sequence_test.go @@ -2,6 +2,7 @@ package store import ( "context" + "database/sql" "encoding/json" "fmt" "os" @@ -26,6 +27,88 @@ func openProjectAuditPG(t *testing.T) *Store { return st } +func TestProjectAuditAllWritersRejectUnsafeOnlineChanges(t *testing.T) { + st := openProjectAuditPG(t) + if err := st.AppendAudit("alice", agentcommand.AuditEnvelope{Time: time.Now(), Actor: "alice", CommandID: "first", State: agentcommand.StateCommitted}); err != nil { + t.Fatal(err) + } + row := `INSERT INTO command_audit(id,time,command_id,source,entrance,state,operation_id,conversation_id,owner,payload) + VALUES ($1,'2026-10-14T00:00:00Z','explicit','','','committed','','','alice','{}')` + if _, err := st.db.Exec(row, 100); err == nil { + t.Fatal("online explicit ID above reserved sequence position was accepted") + } + if _, err := st.db.Exec(`UPDATE command_audit SET command_id='changed' WHERE id=1`); err == nil { + t.Fatal("online audit UPDATE was accepted") + } + if _, err := st.db.Exec(`DELETE FROM command_audit WHERE id=1`); err == nil { + t.Fatal("online audit DELETE was accepted") + } + for _, level := range []sql.IsolationLevel{sql.LevelRepeatableRead, sql.LevelSerializable} { + tx, err := st.db.BeginTx(context.Background(), &sql.TxOptions{Isolation: level}) + if err != nil { + t.Fatal(err) + } + _, err = tx.Exec(`INSERT INTO command_audit(time,command_id,source,entrance,state,operation_id,conversation_id,owner,payload) + VALUES ('2026-10-14T00:00:00Z','stale-snapshot','','','committed','','','alice','{}')`) + tx.Rollback() + if err == nil || !strings.Contains(err.Error(), "read committed") { + t.Fatalf("isolation %v did not fail closed: %v", level, err) + } + } + seq := commandAuditSequenceName(t, st) + if _, err := st.db.Exec(`SELECT nextval($1::regclass)`, seq); err != nil { + t.Fatal(err) + } + if _, err := st.db.Exec(`INSERT INTO command_audit(id,time,command_id,source,entrance,state,operation_id,conversation_id,owner,payload) + VALUES (4,'2026-10-14T00:00:00Z','first-of-batch','','','committed','','','alice','{}'), + (1,'2026-10-14T00:00:00Z','decreasing','','','committed','','','alice','{}')`); err == nil { + t.Fatal("unordered multirow audit INSERT was accepted") + } + if _, err := st.db.Exec(row+` ON CONFLICT DO NOTHING`, 1); err == nil { + t.Fatal("online replay bypassed the INSERT guard through ON CONFLICT") + } + var count int + if err := st.db.QueryRow(`SELECT COUNT(*) FROM command_audit`).Scan(&count); err != nil || count != 1 { + t.Fatalf("unsafe writes persisted: count=%d err=%v", count, err) + } +} + +func TestProjectAuditAllWritersGuardDriftFailsClosed(t *testing.T) { + st := openProjectAuditPG(t) + for _, trigger := range []string{"project_audit_insert_guard_v1", "project_audit_mutation_guard_v1"} { + if _, err := st.db.Exec(`ALTER TABLE command_audit DISABLE TRIGGER ` + trigger); err != nil { + t.Fatal(err) + } + if _, _, _, err := st.ListProjectCommandAudits("alice", "project", 0, 0, 10); err == nil { + t.Fatalf("read accepted disabled trigger %s", trigger) + } + if err := st.AppendAudit("alice", agentcommand.AuditEnvelope{Time: time.Now(), Actor: "alice", CommandID: "drift"}); err == nil { + t.Fatalf("append accepted disabled trigger %s", trigger) + } + if _, err := st.db.Exec(`ALTER TABLE command_audit ENABLE ALWAYS TRIGGER ` + trigger); err != nil { + t.Fatal(err) + } + } + if _, err := st.db.Exec(`CREATE OR REPLACE TRIGGER project_audit_insert_guard_v1 BEFORE INSERT ON command_audit FOR EACH ROW EXECUTE FUNCTION project_audit_mutation_guard_v1()`); err != nil { + t.Fatal(err) + } + if _, err := st.db.Exec(`ALTER TABLE command_audit ENABLE ALWAYS TRIGGER project_audit_insert_guard_v1`); err != nil { + t.Fatal(err) + } + if _, _, _, err := st.ListProjectCommandAudits("alice", "project", 0, 0, 10); err == nil { + t.Fatal("read accepted rebound insert trigger") + } + if _, err := st.db.Exec(`CREATE OR REPLACE TRIGGER project_audit_insert_guard_v1 BEFORE INSERT ON command_audit FOR EACH ROW EXECUTE FUNCTION project_audit_insert_guard_v1()`); err != nil { + t.Fatal(err) + } + if _, err := st.db.Exec(`ALTER TABLE command_audit ENABLE ALWAYS TRIGGER project_audit_insert_guard_v1`); err != nil { + t.Fatal(err) + } + if err := st.AppendAudit("alice", agentcommand.AuditEnvelope{Time: time.Now(), Actor: "alice", CommandID: "restored"}); err != nil { + t.Fatalf("restored guard: %v", err) + } +} + func commandAuditSequenceName(t *testing.T, st *Store) string { t.Helper() var name string @@ -335,6 +418,12 @@ func TestProjectAuditSequenceLockTimeoutAndRollback(t *testing.T) { if _, err := conn.ExecContext(ctx, `RESET lock_timeout`); err != nil { t.Fatal(err) } + cancelCtx, cancel := context.WithTimeout(ctx, 150*time.Millisecond) + defer cancel() + var canceledID int64 + if err := conn.QueryRowContext(cancelCtx, insert, args...).Scan(&canceledID); err == nil { + t.Fatalf("canceled writer passed advisory lock: id=%d", canceledID) + } if err := tx.Rollback(); err != nil { t.Fatal(err) } @@ -426,7 +515,7 @@ func TestProjectAuditSequenceConcurrentLoad(t *testing.T) { statusMode = "test-only locked status wrapper" } newElapsed, newP95 := run("guarded", func(env agentcommand.AuditEnvelope) error { return st.AppendAudit("alice", env) }) - t.Logf("isolated PostgreSQL %s audit append: %d writes per mode / %d workers; v32 direct INSERT=%s p95=%s (%.0f/s), v33 Store.AppendAudit with %s=%s p95=%s (%.0f/s); hardware/load dependent SELF only", + t.Logf("isolated PostgreSQL %s audit append: %d writes per mode / %d workers; v32 direct INSERT=%s p95=%s (%.0f/s), v34 Store.AppendAudit with %s=%s p95=%s (%.0f/s); hardware/load dependent SELF only", serverVersion, writers*each, writers, oldElapsed, oldP95, float64(writers*each)/oldElapsed.Seconds(), statusMode, newElapsed, newP95, float64(writers*each)/newElapsed.Seconds()) seen := map[string]bool{} var before, cutoff int64 @@ -464,8 +553,14 @@ func TestProjectAuditConcurrentReadWriteNeverReportsFalseDrift(t *testing.T) { if err != nil { t.Fatal(err) } + for i := 0; i < 210; i++ { + if err := st.AppendAudit("alice", agentcommand.AuditEnvelope{Time: time.Now(), Actor: "alice", CommandID: fmt.Sprintf("seed-%d", i), ConversationID: string(c.ID), State: agentcommand.StateCommitted}); err != nil { + t.Fatal(err) + } + } const writers, writes, readers, reads = 6, 30, 4, 40 errs := make(chan error, writers*writes+readers*reads) + readDurations := make(chan time.Duration, readers*reads) var wg sync.WaitGroup for worker := 0; worker < writers; worker++ { wg.Add(1) @@ -485,10 +580,17 @@ func TestProjectAuditConcurrentReadWriteNeverReportsFalseDrift(t *testing.T) { go func() { defer wg.Done() for i := 0; i < reads; i++ { - if _, _, _, err := st.ListProjectCommandAudits("alice", string(p.ID), 0, 0, 10); err != nil { + started := time.Now() + page, _, _, err := st.ListProjectCommandAudits("alice", string(p.ID), 0, 0, 200) + if err != nil { errs <- fmt.Errorf("read: %w", err) return } + if len(page) != 200 { + errs <- fmt.Errorf("large page rows=%d want=200", len(page)) + return + } + readDurations <- time.Since(started) } }() } @@ -497,4 +599,12 @@ func TestProjectAuditConcurrentReadWriteNeverReportsFalseDrift(t *testing.T) { for err := range errs { t.Fatal(err) } + close(readDurations) + var samples []time.Duration + for duration := range readDurations { + samples = append(samples, duration) + } + sort.Slice(samples, func(i, j int) bool { return samples[i] < samples[j] }) + t.Logf("isolated PG mixed load: %d append writes / %d writers and %d 200-row pages / %d readers; page p95=%s, SELF cost signal only", + writers*writes, writers, readers*reads, readers, samples[len(samples)*95/100]) } diff --git a/pkg/extension/extension.go b/pkg/extension/extension.go index 173e5f0..0287fc8 100644 --- a/pkg/extension/extension.go +++ b/pkg/extension/extension.go @@ -44,7 +44,9 @@ import ( // ProjectAuditPublisherFactory; scoped Host string IDs cannot publish links. // 2.17.0-experimental: PostgreSQL project audit pages require a verified // committed-prefix ID generator; unsafe sequence/default drift rejects reads. -const SurfaceVersion = "2.17.0-experimental" +// 2.18.0-experimental: all PostgreSQL audit writes require the database +// insert guard and append-only policy before a committed-prefix claim. +const SurfaceVersion = "2.18.0-experimental" // ErrNotLinked 表示进程没有空白导入 link 包,Main 无法启动服务。 var ErrNotLinked = errors.New("extension: runner not linked; blank-import agent-system-backend/pkg/extension/link") diff --git a/pkg/extension/project_audit.go b/pkg/extension/project_audit.go index d1592fe..c05d6dc 100644 --- a/pkg/extension/project_audit.go +++ b/pkg/extension/project_audit.go @@ -103,7 +103,8 @@ type ProjectAuditPublisher interface { // ProjectAuditQuery reads one project-bound command-audit slice. Offset is a // host source offset, not a cross-domain event sequence. A nonzero BeforeOffset // must be paired with the CutoffOffset returned on the first page. PostgreSQL -// pages are served only while the installed generator and sequence prove a +// pages are served only while the installed generator, sequence, and all-writer +// database guards prove a // committed prefix; otherwise the host rejects the read instead of claiming a // fixed snapshot. See Coverage.CutoffConsistency. type ProjectAuditQuery struct { From 14dc48de79b27878ab1cf4ad81ce11258599d0ed Mon Sep 17 00:00:00 2001 From: weichen Date: Wed, 14 Oct 2026 06:39:18 +0000 Subject: [PATCH 13/15] Reject reused migration identities before data copy Why: Parallel unpublished candidates reused numeric versions, so version-only checks could reinterpret a different schema after combination. Evidence: SQLite ledger and source-copy mismatch tests failed before the guard and pass now; PostgreSQL store/migrator and full Go suites pass. Impact: Fail closed on version-name mismatch before opening a migration target or serving a database. Refs: feature/rdm-two-trees-latest-templates Backend combination probe --- cmd/storage-migrate/main.go | 23 +++++++++++++++++++++++ cmd/storage-migrate/main_test.go | 17 +++++++++++++++++ internal/store/migrate.go | 16 +++++++++++----- internal/store/migrate_atomic_test.go | 18 ++++++++++++++++++ internal/store/migrate_compat.go | 11 +++++++++++ 5 files changed, 80 insertions(+), 5 deletions(-) diff --git a/cmd/storage-migrate/main.go b/cmd/storage-migrate/main.go index 63a6beb..4ee8cf4 100644 --- a/cmd/storage-migrate/main.go +++ b/cmd/storage-migrate/main.go @@ -350,6 +350,9 @@ func cmdMigrate(args []string) error { if err != nil { return err } + if err := validateSourceMigrationLedger(src); err != nil { + return err + } // 目标:先用 store 同款迁移器建 schema/扩展(OpenPostgres 会跑迁移与 // 未知版本门禁),再落控制表。DSN 不回显、不入报告。 @@ -416,6 +419,26 @@ func cmdMigrate(args []string) error { return writeReport(rep, *out) } +func validateSourceMigrationLedger(src *sql.DB) error { + rows, err := src.Query(`SELECT version,name FROM schema_migrations`) + if err != nil { + return err + } + defer rows.Close() + for rows.Next() { + var version int + var name string + 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) + } + } + return rows.Err() +} + // Candidate context-scope v35 may contain full model prompts in private columns. // Never copy or back up such a source implicitly; it must be migrated and // reviewed in an isolated Backend store before this tool accepts it. diff --git a/cmd/storage-migrate/main_test.go b/cmd/storage-migrate/main_test.go index e89ee3f..c7d8005 100644 --- a/cmd/storage-migrate/main_test.go +++ b/cmd/storage-migrate/main_test.go @@ -30,6 +30,23 @@ func buildFixture(t *testing.T) string { return path } +func TestMigrateRejectsSourceLedgerNameCollisionBeforeTargetOpen(t *testing.T) { + source := buildFixture(t) + db, err := sql.Open("sqlite", source) + if err != nil { + t.Fatal(err) + } + if _, err := db.Exec(`UPDATE schema_migrations SET name='work_task_bindings' WHERE version=31`); 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") { + t.Fatalf("candidate branch ledger was not rejected before opening target: %v", err) + } +} + func seedEdgeRows(t *testing.T, st *store.Store) { t.Helper() // users(含 is_admin/must_change 布尔)与单租户默认身份。 diff --git a/internal/store/migrate.go b/internal/store/migrate.go index 6248aa3..6103aac 100644 --- a/internal/store/migrate.go +++ b/internal/store/migrate.go @@ -1254,26 +1254,32 @@ func (s *Store) migratePass() error { return fmt.Errorf("migrate: %w", err) } applied := map[int]bool{} - known := map[int]bool{} + known := map[int]string{} for _, m := range migrations { - known[m.version] = true + known[m.version] = m.name } - rows, err := tx.Query(`SELECT version FROM schema_migrations`) + rows, err := tx.Query(`SELECT version,name FROM schema_migrations`) if err != nil { return fmt.Errorf("migrate: %w", err) } for rows.Next() { var v int - if err := rows.Scan(&v); err != nil { + var name string + if err := rows.Scan(&v, &name); err != nil { rows.Close() return fmt.Errorf("migrate: %w", err) } // 兼容门禁(KTD8 应用侧只核对兼容版本):库里有本二进制不认识的版本 // 说明库由更新的构建迁移过——拒绝启动,不冒险在未知 schema 上服务。 - if !known[v] { + expectedName, knownVersion := known[v] + if !knownVersion { rows.Close() return fmt.Errorf("migrate: 数据库 schema 版本 v%d 超出本构建(最高 v%d);拒绝启动,请升级二进制", v, latestMigrationVersion()) } + if name != expectedName { + rows.Close() + return fmt.Errorf("migrate: migration identity mismatch at v%d; refuse to reinterpret another schema", v) + } applied[v] = true } if err := rows.Err(); err != nil { diff --git a/internal/store/migrate_atomic_test.go b/internal/store/migrate_atomic_test.go index e2421c4..7256f15 100644 --- a/internal/store/migrate_atomic_test.go +++ b/internal/store/migrate_atomic_test.go @@ -87,6 +87,24 @@ func assertFullLedger(t *testing.T, st *Store) { } } +// Separate feature candidates reused version 31 before current main assigned +// that number to account_audit. A version match alone must not silently treat +// the candidate's different schema as an applied release migration. +func TestMigrateRejectsLedgerNameMismatch(t *testing.T) { + path := filepath.Join(t.TempDir(), "name-mismatch.db") + st := openSQLiteAt(t, path) + if _, err := st.db.Exec(`UPDATE schema_migrations SET name='work_task_bindings' WHERE version=31`); err != nil { + t.Fatal(err) + } + st.Close() + if reopened, err := OpenSQLite(path); err == nil { + reopened.Close() + t.Fatal("reused migration version with different name was accepted") + } else if !strings.Contains(err.Error(), "migration identity mismatch") { + t.Fatalf("missing fail-closed migration identity error: %v", err) + } +} + // DDL 与账本同事务:在「DDL 已执行、版本未登记」处注入故障 → 全部回滚, // 不留下 v6 的表,也不留下 v6 的账本行(历史两步实现的中间态缺口)。 func TestMigrateAtomicDDLAndLedger(t *testing.T) { diff --git a/internal/store/migrate_compat.go b/internal/store/migrate_compat.go index 774bc90..3f408b5 100644 --- a/internal/store/migrate_compat.go +++ b/internal/store/migrate_compat.go @@ -50,6 +50,17 @@ var migrationBreaksOldReaders = map[int]bool{ // SchemaTarget 是本二进制会迁移到的 schema 版本。 func SchemaTarget() int { return latestMigrationVersion() } +// MigrationName lets the offline copier reject an unpublished branch schema +// that reused a numeric version for a different migration before copying rows. +func MigrationName(version int) (string, bool) { + for _, migration := range migrations { + if migration.version == version { + return migration.name, true + } + } + return "", false +} + // SchemaCompatFloor 是旧制品仍能读取的最高破坏性版本。0 表示当前全部迁移都不破坏旧读。 func SchemaCompatFloor() int { floor := 0 From f4ed6b063ff3aaa7cf3917c8b40bd0eb10e8d291 Mon Sep 17 00:00:00 2001 From: weichen Date: Wed, 14 Oct 2026 06:49:34 +0000 Subject: [PATCH 14/15] Verify nonempty workflow context tables survive migration Why: Combined W6 and W9 storage validation must cover actual work task and step-source rows, not only empty tables. Evidence: Isolated PostgreSQL migration test copies and reads both rows with their trusted scope intact. Impact: Adds integration coverage for the combined v31-v40 migration; no production behavior change. Refs: feature/rdm-two-trees-latest-templates Backend combination probe --- cmd/storage-migrate/integration_test.go | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/cmd/storage-migrate/integration_test.go b/cmd/storage-migrate/integration_test.go index 87b05d9..1f2d341 100644 --- a/cmd/storage-migrate/integration_test.go +++ b/cmd/storage-migrate/integration_test.go @@ -29,6 +29,15 @@ func TestMigrateTrustedContextManifestWithoutRawScope(t *testing.T) { if err := src.AppendContextManifestWithScope("alice", "r1", m, store.ContextManifestScope{Bound: true}); err != nil { t.Fatal(err) } + task, err := src.CreateWorkTask("alice", "rdm", "p1", "c1", "document", "Prepare document") + if err != nil { + t.Fatal(err) + } + stepHash := strings.Repeat("a", 64) + if _, err := src.DB().Exec(`INSERT INTO workflow_step_sources(run_id,step_id,owner,project_id,step_attempt_id,output_sha256,attempt_id,authorization_epoch,publication_epoch,watermark,version,created_at) VALUES(?,?,?,?,?,?,?,?,?,?,?,?)`, + "r1", "draft", "alice", "p1", "step-attempt", stepHash, "model-attempt", "auth-1", "pub-1", "water-1", "v1", "2026-10-14T00:00:00Z"); err != nil { + t.Fatal(err) + } if err := src.Close(); err != nil { t.Fatal(err) } @@ -48,6 +57,14 @@ func TestMigrateTrustedContextManifestWithoutRawScope(t *testing.T) { if err != nil || len(receipts) != 2 || len(receipts[1].Included) != 1 || receipts[1].Included[0].SourceID != "source-a" { t.Fatalf("content-free model receipts not copied: count=%d err=%v", len(receipts), err) } + workScope, err := pg.WorkTaskScope("alice", "rdm", task.ID) + if err != nil || workScope.ProjectID != "p1" || workScope.ConversationID != "c1" { + t.Fatalf("trusted work task binding not copied: scope=%+v err=%v", workScope, err) + } + stepSource, err := pg.WorkflowStepSource("alice", "r1", "draft") + if err != nil || stepSource.StepAttemptID != "step-attempt" || stepSource.AttemptID != "model-attempt" || stepSource.ProjectID != "p1" { + t.Fatalf("workflow step source not copied: source=%+v err=%v", stepSource, err) + } var rawColumns int if err := pg.DB().QueryRow(`SELECT COUNT(*) FROM information_schema.columns WHERE table_schema=current_schema() AND table_name='context_manifests' AND column_name IN ('resolve_goal','resolve_target_id')`).Scan(&rawColumns); err != nil || rawColumns != 0 { t.Fatalf("migrated PG schema has raw prompt columns: count=%d err=%v", rawColumns, err) From d9b00c23c818bfc4a8d8233c845266fabd863137 Mon Sep 17 00:00:00 2001 From: weichen Date: Wed, 14 Oct 2026 06:52:29 +0000 Subject: [PATCH 15/15] Refuse rollback after migration ledger identity drift Why: An already-open Store could otherwise execute a numbered down migration after its ledger name changed. Evidence: The targeted rollback test failed before the guard and passed after it; focused SQLite and isolated PostgreSQL store/migrator checks passed. Impact: MigrationStatus and MigrateDown fail closed on version-name mismatch before changing schema. Refs: feature/rdm-two-trees-latest-templates Backend combination probe --- internal/store/migrate.go | 9 +++++++-- internal/store/migrate_atomic_test.go | 16 ++++++++++++++++ 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/internal/store/migrate.go b/internal/store/migrate.go index 6103aac..ea39d6b 100644 --- a/internal/store/migrate.go +++ b/internal/store/migrate.go @@ -1381,7 +1381,7 @@ func (s *Store) execInTx(stmts []string) error { // MigrationStatus 返回已应用版本列表(诊断/验收用)。 func (s *Store) MigrationStatus() ([]int, error) { - rows, err := s.db.Query(`SELECT version FROM schema_migrations ORDER BY version`) + rows, err := s.db.Query(`SELECT version,name FROM schema_migrations ORDER BY version`) if err != nil { return nil, err } @@ -1389,9 +1389,14 @@ func (s *Store) MigrationStatus() ([]int, error) { out := []int{} for rows.Next() { var v int - if err := rows.Scan(&v); err != nil { + var name string + if err := rows.Scan(&v, &name); err != nil { return nil, err } + expected, known := MigrationName(v) + if !known || expected != name { + return nil, fmt.Errorf("migrate: migration identity mismatch at v%d; refuse to roll back another schema", v) + } out = append(out, v) } return out, rows.Err() diff --git a/internal/store/migrate_atomic_test.go b/internal/store/migrate_atomic_test.go index 7256f15..ca8f1d0 100644 --- a/internal/store/migrate_atomic_test.go +++ b/internal/store/migrate_atomic_test.go @@ -105,6 +105,22 @@ func TestMigrateRejectsLedgerNameMismatch(t *testing.T) { } } +func TestMigrateDownRejectsLedgerNameDriftBeforeChangingSchema(t *testing.T) { + path := filepath.Join(t.TempDir(), "down-name-drift.db") + st := openSQLiteAt(t, path) + if _, err := st.db.Exec(`UPDATE schema_migrations SET name='wrong-candidate' WHERE version=40`); err != nil { + t.Fatal(err) + } + err := st.MigrateDown(39) + if err == nil || !strings.Contains(err.Error(), "migration identity mismatch") { + t.Fatalf("rollback accepted a changed migration identity: %v", err) + } + var remain int + if err := st.db.QueryRow(`SELECT COUNT(*) FROM schema_migrations WHERE version=40`).Scan(&remain); err != nil || remain != 1 { + t.Fatalf("rollback changed ledger before refusing: remaining=%d err=%v", remain, err) + } +} + // DDL 与账本同事务:在「DDL 已执行、版本未登记」处注入故障 → 全部回滚, // 不留下 v6 的表,也不留下 v6 的账本行(历史两步实现的中间态缺口)。 func TestMigrateAtomicDDLAndLedger(t *testing.T) {