From 0033933a58ba4c0ce9bc540f3e12044018bdebb0 Mon Sep 17 00:00:00 2001 From: Sam Catlow Date: Wed, 11 Nov 2026 02:49:37 +1100 Subject: [PATCH 1/7] perf(store): index the six unindexed foreign keys (#1860) Postgres does not index the referencing side of a foreign key, so deleting or updating a parent row scanned the child table once per referencing row for the six keys that had no matching index: identity aliases and subjects, org memberships and agents, installed package requirements, and the task a handoff came from. Migration 0074 adds one supporting index per key; the foreign-key integrity checks on the restored snapshot pass, with every one of the 17 constraints now covered. --- CHANGELOG.md | 4 ++++ .../postgres/migrations/0074_fk_indexes.sql | 19 +++++++++++++++++++ 2 files changed, 23 insertions(+) create mode 100644 internal/infrastructure/postgres/migrations/0074_fk_indexes.sql diff --git a/CHANGELOG.md b/CHANGELOG.md index f201e3b17..12e73c3d9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -26,6 +26,10 @@ release's per-component sections are labelled beneath its heading. - Conversation titles are written from your message and Archie's reply, name what the conversation is about rather than the mood, and wait for an exchange with a subject. - Session memory never records passwords, tokens or other secrets, and adds only facts it does not already hold, so memory stops filling with restated duplicates. +### archie-state-store + +- Six foreign keys now have an index on the referencing column, so deleting a parent row looks its children up instead of scanning the child table: identity aliases and subjects, org memberships and agents, installed package requirements, and the task a handoff came from. + ## [1.62.0] - 2026-11-10 ### archied diff --git a/internal/infrastructure/postgres/migrations/0074_fk_indexes.sql b/internal/infrastructure/postgres/migrations/0074_fk_indexes.sql new file mode 100644 index 000000000..79953fd98 --- /dev/null +++ b/internal/infrastructure/postgres/migrations/0074_fk_indexes.sql @@ -0,0 +1,19 @@ +-- +goose Up +-- Postgres does not index the referencing side of a foreign key, so a parent +-- delete or key update scans the child table for each referencing row. These +-- indexes back the six child lookups that had no matching index. +CREATE INDEX idx_identity_aliases_identity_id ON identity_aliases (identity_id); +CREATE INDEX idx_identity_subjects_identity_id ON identity_subjects (identity_id); +CREATE INDEX idx_memberships_org_id ON memberships (org_id); +CREATE INDEX idx_org_agents_org_id ON org_agents (org_id); +CREATE INDEX idx_installed_package_requirements_org_id_required_name ON installed_package_requirements (org_id, required_name); +CREATE INDEX idx_tasks_handoff_from_task_id ON tasks (handoff_from_task_id); + +-- +goose Down + +DROP INDEX idx_tasks_handoff_from_task_id; +DROP INDEX idx_installed_package_requirements_org_id_required_name; +DROP INDEX idx_org_agents_org_id; +DROP INDEX idx_memberships_org_id; +DROP INDEX idx_identity_subjects_identity_id; +DROP INDEX idx_identity_aliases_identity_id; -- 2.54.0 From c155bb24ba39a477cab7782d3be0f78a4ed67813 Mon Sep 17 00:00:00 2001 From: Sam Catlow Date: Wed, 11 Nov 2026 02:59:02 +1100 Subject: [PATCH 2/7] fix(taskactions): record the retention rule for archived task children (#1859) Archiving deletes the task row but deliberately keeps its child rows: execution history, usage and the dispatch ledger are records about the work, not attachments to the row. Nothing recorded that rule, so the kept rows had no parent row to explain them and no query could say what happened to an archived task. Record the rule in the archive event's data. The kind is protected from audit retention and already projects into the audit feed, so that event is the archived-task record: it names the kept child sets and leaves event rows to the instance's per-kind retention. --- CHANGELOG.md | 1 + internal/domain/taskactions/service.go | 14 ++++++++++++++ 2 files changed, 15 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 12e73c3d9..097e806b0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ release's per-component sections are labelled beneath its heading. - A model provider that refuses a task's credential or has run out of credit now blocks the task with a fix to follow, instead of leaving a raw API error, and the task is flagged for retry once the provider settings change. - New Google providers added from the catalogue work without setting a base URL. +- Archiving a task now records that its execution history is kept deliberately and which child tables hold it, so its audit event explains what happened to the task after its row was removed. ### archie-ui diff --git a/internal/domain/taskactions/service.go b/internal/domain/taskactions/service.go index 4d7fb932e..e44d8b6f5 100644 --- a/internal/domain/taskactions/service.go +++ b/internal/domain/taskactions/service.go @@ -598,6 +598,20 @@ func (s Service) applyMerge(ctx context.Context, task *Task, actor Actor, o outc func (s Service) applyArchive(ctx context.Context, task *Task, actor Actor, o outcome) (outcome, error) { o.event.Kind, o.event.Detail = events.KindTaskArchiveRequested, actor.describe("archive requested") + // Archiving removes the task row but deliberately keeps its child rows: + // execution history, usage and the dispatch ledger are records about the + // work, not attachments to the row. This event is their provenance and says + // what was kept; audit retention never prunes it. Events are named apart + // because the instance's per-kind retention, not this decision, owns their + // lifetime, and run credentials are not listed because they expire and the + // sweep removes them. + o.event.Data = map[string]any{ + "retention": "kept", + "children": []string{ + "transitions", "step_executions", "model_usage", "tool_calls", "binding_dispatches", + }, + "events": "audit_retention", + } id, err := s.Store.ArchiveTask(ctx, task.ID, task.Status, o.event) if err != nil { return o, err -- 2.54.0 From 161e8f95bf20e6e01a440e5071bb0a64fc8a51e4 Mon Sep 17 00:00:00 2001 From: Sam Catlow Date: Wed, 11 Nov 2026 03:03:13 +1100 Subject: [PATCH 3/7] perf(store): share one control-plane version read across all watchers (#1858) The control-plane Watch was a 250 ms database poll per subscription: an idle instance ran about 180 transactions a second (archied 14 streams, gateway 5, dashboard 26), almost all sequential scans of the small resources table. The State Store now keeps one shared view of every stored resource version, read once per 250 ms window and reused by every Watch stream; a stream reads its own resource only when its kind's version advanced, so idle database work is one read per interval for the whole process whatever the subscriber count. The watch contract is unchanged: resume from AfterVersion, no missed update (versions are monotonic per org and kind), the immediate seed probe, the same 250 ms latency bound (streams wake on the same window boundary), and a store error still ends the stream with Unavailable so the clients' existing reconnect backoff applies. --- CHANGELOG.md | 1 + .../controlplanestore/controlplane.go | 26 +-- .../infrastructure/controlplanestore/watch.go | 163 ++++++++++++++++++ 3 files changed, 165 insertions(+), 25 deletions(-) create mode 100644 internal/infrastructure/controlplanestore/watch.go diff --git a/CHANGELOG.md b/CHANGELOG.md index 097e806b0..78f45de06 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -30,6 +30,7 @@ release's per-component sections are labelled beneath its heading. ### archie-state-store - Six foreign keys now have an index on the referencing column, so deleting a parent row looks its children up instead of scanning the child table: identity aliases and subjects, org memberships and agents, installed package requirements, and the task a handoff came from. +- The control plane no longer polls the database once per watch subscriber: every watcher shares one version read per 250 ms, so an idle instance drops from roughly 180 transactions a second to a handful while a resource change still reaches every watcher on the same schedule. ## [1.62.0] - 2026-11-10 diff --git a/internal/infrastructure/controlplanestore/controlplane.go b/internal/infrastructure/controlplanestore/controlplane.go index 1e199de25..dc4545e14 100644 --- a/internal/infrastructure/controlplanestore/controlplane.go +++ b/internal/infrastructure/controlplanestore/controlplane.go @@ -48,6 +48,7 @@ const defaultHistoryLimit = 200 type Server struct { pb.UnimplementedControlPlaneServiceServer store ResourceStore + watch watchSnapshot definitions map[string]Definition ordered []Definition steps workflow.StepRegistry @@ -163,31 +164,6 @@ func (s *Server) Command(ctx context.Context, request *pb.CommandRequest) (*pb.C return &pb.CommandResponse{Resource: resourceProto(resource)}, nil } -func (s *Server) Watch(request *pb.WatchRequest, stream pb.ControlPlaneService_WatchServer) error { - if _, ok := s.definitions[request.GetKind()]; !ok { - return status.Error(codes.NotFound, "resource not found") - } - version := request.AfterVersion - ticker := time.NewTicker(250 * time.Millisecond) - defer ticker.Stop() - for { - resource, err := s.resource(stream.Context(), requestOrg(stream.Context()), request.Kind) - if err == nil && resource.Version > version { - if err := stream.Send(&pb.WatchResponse{Resource: resourceProto(resource)}); err != nil { - return err - } - version = resource.Version - } else if err != nil && !errors.Is(err, storecontract.ErrResourceNotFound) { - return mapError(err) - } - select { - case <-stream.Context().Done(): - return stream.Context().Err() - case <-ticker.C: - } - } -} - // orgSeededKinds are the kinds every organisation holds its own document of. // Reading one an org has never written answers with the empty document: an // org that has configured nothing has nothing, rather than the system org's diff --git a/internal/infrastructure/controlplanestore/watch.go b/internal/infrastructure/controlplanestore/watch.go new file mode 100644 index 000000000..5f1a23724 --- /dev/null +++ b/internal/infrastructure/controlplanestore/watch.go @@ -0,0 +1,163 @@ +package controlplanestore + +import ( + "context" + "errors" + "sync" + "time" + + "google.golang.org/grpc/codes" + "google.golang.org/grpc/status" + + pb "github.com/samcharles93/archie-core/internal/contracts/controlplane/v1" + "github.com/samcharles93/archie-core/internal/domain/storecontract" +) + +// watchPollInterval is how often the process refreshes the shared view of +// stored resource versions. Every Watch stream on this Server reads that one +// view, so the State Store's idle database work is one ListResources per window +// however many subscribers exist. Before #1858 each stream ran its own 250 ms +// poll, which was one read per subscriber per tick: about 180 transactions a +// second on an idle instance, near all of them scans of the small resources +// table. The interval is deliberately the old per-stream cadence, so a change +// still reaches a watcher within the same 250 ms it did before. +const watchPollInterval = 250 * time.Millisecond + +// resourceVersionKey identifies one org's copy of a resource kind. +type resourceVersionKey struct { + orgID string + kind string +} + +// watchSnapshot is the control plane's shared view of every stored resource +// version, read from the store at most once per watchPollInterval window. Watch +// streams read it instead of querying the store themselves: the first stream to +// tick in a window refreshes it and the rest of that window's streams reuse the +// result. +// +// An error is shared for the window too, so a store that has stopped answering +// ends each stream once and lets the clients' existing reconnect backoff take +// over, rather than every stream retrying the store every 250 ms. +type watchSnapshot struct { + mu sync.Mutex + window int64 + versions map[resourceVersionKey]int64 + err error +} + +// storedVersions answers every stored resource's version, reading the store at +// most once per watchPollInterval window. The caller reads the returned map +// without a lock: a refresh replaces it and never mutates it. +// +// Streams wait for the window boundary before asking (waitForWatchWindow), so +// the first stream to wake refreshes the snapshot and every other stream reads +// the version it just published. A change committed in one window is therefore +// delivered at the boundary after it, within the interval: the same latency +// bound the per-stream poll had, while the store is asked once per window for +// the whole process. +func (s *Server) storedVersions(ctx context.Context) (map[resourceVersionKey]int64, error) { + snapshot := &s.watch + window := time.Now().UnixNano() / int64(watchPollInterval) + snapshot.mu.Lock() + defer snapshot.mu.Unlock() + if snapshot.window == window { + return snapshot.versions, snapshot.err + } + resources, err := s.store.ListResources(ctx) + snapshot.window, snapshot.versions, snapshot.err = window, nil, nil + if err != nil { + if ctx.Err() != nil { + // A stream that went away while it was the one refreshing the + // snapshot must not hand its cancellation to every other watcher: + // leave the window unfilled so the next stream reads the store again. + snapshot.window = 0 + return nil, err + } + snapshot.err = err + return nil, err + } + versions := make(map[resourceVersionKey]int64, len(resources)) + for _, resource := range resources { + versions[resourceVersionKey{orgID: resource.OrgID, kind: resource.Kind}] = resource.Version + } + snapshot.versions = versions + return versions, nil +} + +// waitForWatchWindow blocks until the next watchPollInterval boundary, +// reporting false when ctx ends first. Every stream in the process wakes just +// after the boundary, so their reads of the shared snapshot land in the same +// window: one store read serves them all, and none of them serves a snapshot +// older than one interval. +func waitForWatchWindow(ctx context.Context) bool { + now := time.Now() + next := now.Truncate(watchPollInterval).Add(watchPollInterval) + timer := time.NewTimer(next.Sub(now)) + defer timer.Stop() + select { + case <-ctx.Done(): + return false + case <-timer.C: + return true + } +} + +// Watch streams one org's copy of a control-plane resource from the version the +// caller already has. A stream delivers the current document whenever its +// version is newer than the last one delivered, so a reconnect that resumes +// from AfterVersion neither replays a version the client has applied nor skips +// one it has not. Versions written between two polls coalesce into the newest, +// exactly as they did when each stream polled for itself. +func (s *Server) Watch(request *pb.WatchRequest, stream pb.ControlPlaneService_WatchServer) error { + if _, ok := s.definitions[request.GetKind()]; !ok { + return status.Error(codes.NotFound, "resource not found") + } + ctx := stream.Context() + orgID := requestOrg(ctx) + kind := request.GetKind() + version := request.AfterVersion + // The first read is immediate: it seeds an org-seeded kind and answers a + // version written before this stream existed, without waiting a tick. + if err := s.deliverVersion(stream, orgID, kind, &version); err != nil { + return err + } + for { + if !waitForWatchWindow(ctx) { + return ctx.Err() + } + // One snapshot read serves every stream in this process; a stream only + // reads its resource when the snapshot says its kind advanced. + versions, err := s.storedVersions(ctx) + if err != nil { + return mapError(err) + } + if versions[resourceVersionKey{orgID: orgID, kind: kind}] <= version { + continue + } + if err := s.deliverVersion(stream, orgID, kind, &version); err != nil { + return err + } + } +} + +// deliverVersion reads the org's copy of kind and sends it when it is newer +// than *version, the last version this stream delivered, advancing *version on +// a successful send. A kind with no stored value is not an error: the watch +// keeps waiting for the first write. +func (s *Server) deliverVersion(stream pb.ControlPlaneService_WatchServer, orgID, kind string, version *int64) error { + resource, err := s.resource(stream.Context(), orgID, kind) + if errors.Is(err, storecontract.ErrResourceNotFound) { + return nil + } + if err != nil { + return mapError(err) + } + if resource.Version <= *version { + return nil + } + if err := stream.Send(&pb.WatchResponse{Resource: resourceProto(resource)}); err != nil { + return err + } + *version = resource.Version + return nil +} -- 2.54.0 From c54d328f0d851aad3e95ba4c8b87aa1045af0dad Mon Sep 17 00:00:00 2001 From: Sam Catlow Date: Wed, 11 Nov 2026 03:12:01 +1100 Subject: [PATCH 4/7] feat(store): run services as a non-superuser role and name every connection (#1857) Every service connected to PostgreSQL as the database owner, which is a superuser, so anything that compromised an app process owned the cluster, and every connection reached the server with an empty application_name, so pg_stat_activity could not say which service a session belonged to. Split the DB credential into two login roles and name every pool: - archie_migrator owns public and its objects and runs goose. Migrations run on a short-lived pool opened from the new migration_database_url, before the serving pool opens. It is not a superuser. - archie_runtime serves requests with SELECT, INSERT, UPDATE, DELETE, sequence USAGE/SELECT and function EXECUTE only. It has no CREATE privilege, so it cannot change the schema. - Every pool sets application_name: archie-state-store, archie-gateway, and a -migrate / -recovery / -schema / -provision suffix on the transient administrative connections. Existing one-URL installs keep working: an absent migration_database_url falls back to database_url and the migration runs on the serving pool exactly as before. Provisioning is a new idempotent `archie-state-store provision` subcommand: it needs an admin URL the services never hold (database owner or superuser), creates or reconciles both roles, hands every public-schema object to the migration role, revokes PUBLIC's CREATE on public, sets row-level grants, and sets the migration role's default privileges so tables future migrations create are usable without a hand-run grant. It stores SCRAM verifiers, refuses superuser roles, and verifies the boundary before committing. install.sh now provisions the bundled path and separates the credentials: the superuser password the Compose database is created with lives only in a compose-only postgres.env, while the env file the units load holds the non-superuser PGPASSWORD. A bundled install that predates the split is upgraded in place: the roles are created, the superuser password is rotated out of the env file, and config.toml is moved to the runtime and migration URLs. External databases keep their operator-managed credential; the deployments README documents the one-shot provision command. setup writes both bundled URLs, dev:postgres provisions the dev database, the e2e stack provisions its throwaway server, and the deployment templates point at the two roles. --- CHANGELOG.md | 1 + Taskfile.yml | 11 +- cmd/archie-state-store/main.go | 11 +- config.example.toml | 7 +- deployments/INSTRUCTIONS.md | 5 +- deployments/README.md | 33 +- deployments/dev.toml | 10 +- deployments/docker-nats-stack.toml | 9 +- deployments/single-forge-github.toml | 9 +- deployments/standalone.toml | 9 +- docker-compose.yml | 5 +- e2e/stack.sh | 16 +- install.sh | 76 ++- internal/app/archiegateway/chat_runtime.go | 2 +- internal/app/statestore/pool.go | 8 +- internal/app/statestore/provision.go | 99 ++++ internal/app/statestore/recovery.go | 13 +- internal/app/statestore/recovery_postgres.go | 17 +- internal/config/config.go | 21 +- .../configuration/denied_keys.go | 1 + .../configuration/setup/setup.go | 57 +- .../configuration/setup/setup_test.go | 62 +- .../infrastructure/postgres/maintenance.go | 2 +- internal/infrastructure/postgres/postgres.go | 71 ++- internal/infrastructure/postgres/provision.go | 534 ++++++++++++++++++ .../infrastructure/postgres/provision_test.go | 264 +++++++++ 26 files changed, 1252 insertions(+), 101 deletions(-) create mode 100644 internal/app/statestore/provision.go create mode 100644 internal/infrastructure/postgres/provision.go create mode 100644 internal/infrastructure/postgres/provision_test.go diff --git a/CHANGELOG.md b/CHANGELOG.md index 78f45de06..dca350a5f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -31,6 +31,7 @@ release's per-component sections are labelled beneath its heading. - Six foreign keys now have an index on the referencing column, so deleting a parent row looks its children up instead of scanning the child table: identity aliases and subjects, org memberships and agents, installed package requirements, and the task a handoff came from. - The control plane no longer polls the database once per watch subscriber: every watcher shares one version read per 250 ms, so an idle instance drops from roughly 180 transactions a second to a handful while a resource change still reaches every watcher on the same schedule. +- The bundled install now runs the services as a least-privilege database role that can read and write rows but cannot change the schema; a separate migration role owns the schema and applies migrations, and every connection names its service in `pg_stat_activity`. The installer provisions both roles, and re-running it moves an existing bundled install onto them, so a compromised service no longer holds a credential that owns the cluster. ## [1.62.0] - 2026-11-10 diff --git a/Taskfile.yml b/Taskfile.yml index a7a6f0f40..0462d26da 100644 --- a/Taskfile.yml +++ b/Taskfile.yml @@ -146,7 +146,7 @@ tasks: done dev:postgres: - desc: Start PostgreSQL and wait for readiness + desc: Start PostgreSQL, wait for readiness, provision the database roles cmds: - docker compose up -d postgres - | @@ -160,6 +160,15 @@ tasks: done echo "postgres did not become healthy within 60s" >&2 exit 1 + - | + # docker-compose.yml creates the dev database with a superuser named + # archie; provision the least-privilege roles deployments/dev.toml + # points at. Idempotent, so this also upgrades a volume created before + # the role split. + go run ./cmd/archie-state-store provision \ + -admin-url "postgres://archie:archie@127.0.0.1:5432/archie?sslmode=disable" \ + -runtime-url "postgres://archie_runtime:archie@127.0.0.1:5432/archie?sslmode=disable" \ + -migration-url "postgres://archie_migrator:archie@127.0.0.1:5432/archie?sslmode=disable" dev:wait-state: internal: true diff --git a/cmd/archie-state-store/main.go b/cmd/archie-state-store/main.go index 8c2568cdf..a3ee8bd25 100644 --- a/cmd/archie-state-store/main.go +++ b/cmd/archie-state-store/main.go @@ -19,10 +19,13 @@ import ( func main() { os.Exit(run()) } func run() int { - // A leading non-flag argument selects a recovery subcommand; anything else, + // A leading non-flag argument selects a subcommand; anything else, // including no arguments at all, serves the State Store contract. An - // unrecognised word is refused rather than ignored, so a mistyped recovery - // command never leaves an operator watching a server start instead. + // unrecognised word is refused rather than ignored, so a mistyped command + // never leaves an operator watching a server start instead. + if args := os.Args[1:]; statestore.IsProvisionArgs(args) { + return statestore.RunProvision(context.Background(), args[1:], os.Stdout, os.Stderr) + } if args := os.Args[1:]; statestore.IsAccessResetArgs(args) { return statestore.RunAccessReset(context.Background(), args[2:], os.Stderr) } @@ -36,7 +39,7 @@ func run() int { flag.StringVar(&options.Listen, "listen", "", "state store gRPC listen address (defaults to [services.state].listen, else 127.0.0.1:9090)") flag.StringVar(&options.ReadyAddr, "ready-addr", "", "optional readiness HTTP listen address (e.g. 127.0.0.1:9091)") flag.Usage = func() { - fmt.Fprintf(flag.CommandLine.Output(), "usage: archie-state-store [flags] # serve the State Store gRPC contract\n\noffline recovery: archie-state-store -h | access reset -h\n\n") + fmt.Fprintf(flag.CommandLine.Output(), "usage: archie-state-store [flags] # serve the State Store gRPC contract\n\ndatabase roles: archie-state-store provision -h\noffline recovery: archie-state-store -h | access reset -h\n\n") flag.PrintDefaults() } flag.Parse() diff --git a/config.example.toml b/config.example.toml index 1ab6dd86d..d226f32c3 100644 --- a/config.example.toml +++ b/config.example.toml @@ -7,7 +7,12 @@ # Secrets are never written here: each is a {engine, key} reference. The # built-in "env" engine reads key as an environment variable. -# database_url = "postgres://archie:archie@localhost:5432/archie?sslmode=disable" +# The URL the services connect as: a least-privilege role, created by +# `archie-state-store provision`. migration_database_url is the role that owns +# the schema and runs migrations; when it is absent, database_url does both, +# which is what a single-role install does. +# database_url = "postgres://archie_runtime:secret@localhost:5432/archie?sslmode=disable" +# migration_database_url = "postgres://archie_migrator:secret@localhost:5432/archie?sslmode=disable" # work_dir = "~/.local/share/archie/work" # state_dir = "~/.local/share/archie" diff --git a/deployments/INSTRUCTIONS.md b/deployments/INSTRUCTIONS.md index b50f1d56c..9c45883d8 100644 --- a/deployments/INSTRUCTIONS.md +++ b/deployments/INSTRUCTIONS.md @@ -30,7 +30,10 @@ that reference is on a registry that needs a login -- see () — `standalone.toml` (no forge, the simplest start), `single-forge-github.toml` and `docker-nats-stack.toml` cover the common cases. Every one needs - PostgreSQL 18 at `database_url`. Copy whichever fits to: + PostgreSQL 18 with the `archie_migrator` and `archie_runtime` roles created + once by `archie-state-store provision` (see `deployments/README.md`); + `docker compose up -d postgres` plus that command gives the local default. + Copy whichever fits to: ```bash mkdir -p ~/.config/archie diff --git a/deployments/README.md b/deployments/README.md index ea93cb4c5..456e691f5 100644 --- a/deployments/README.md +++ b/deployments/README.md @@ -42,7 +42,9 @@ systemctl --user start archied Start order does not matter: a service waits for its peers and reports them as degraded meanwhile. Every template needs PostgreSQL 18 at `database_url`; -`docker compose up -d postgres` provides the default one. +`docker compose up -d postgres` provides the default server. Its two login +roles are created once per database by `archie-state-store provision` -- see +[Database roles](#database-roles). The State Store service (`archie-state-store`) owns the task data in the PostgreSQL database `database_url` names and serves the `StateStore` gRPC @@ -54,6 +56,35 @@ gateway address, because a container cannot reach the host's loopback. A non-loopback address needs `[tls]` certificates from your own CA; see `config.example.toml`. +## Database roles + +The services do not connect as the database owner. `archie-state-store +provision` creates, or reconciles, two login roles in an existing PostgreSQL +database and hands the schema to one of them: + +- `archie_migrator` owns the schema and its objects and runs the goose + migrations; +- `archie_runtime` may read and write rows (SELECT, INSERT, UPDATE, DELETE, + sequence USAGE, function EXECUTE) and has no CREATE privilege, so it cannot + change the schema. + +Run it once per database with a role allowed to create roles and take ownership +of the schema (the database owner or a superuser), then point the templates at +the two roles: `database_url` is the runtime URL and `migration_database_url` +the migration one. The command is idempotent, so re-running it after an upgrade +reconciles objects a migration added. + +```bash +archie-state-store provision \ + -admin-url "postgres://owner:secret@db:5432/archie" \ + -runtime-url "postgres://archie_runtime:secret@db:5432/archie" \ + -migration-url "postgres://archie_migrator:secret@db:5432/archie" +``` + +The bundled installer and `task dev` run this for you. A configuration with only +`database_url` keeps working exactly as before: that one role applies the +schema and serves, which is what an install that has not split the roles does. + ## Running the dashboard The dashboard is served by the standalone UI Service (`archie-ui`), not by diff --git a/deployments/dev.toml b/deployments/dev.toml index 11dc77fff..0e6e9d7c7 100644 --- a/deployments/dev.toml +++ b/deployments/dev.toml @@ -9,12 +9,16 @@ # [web] entirely. # `task dev` runs archie-state-store against this overlay, and the State Store -# fails closed without a PostgreSQL URL. This must reach the `postgres` +# fails closed without a PostgreSQL URL. These must reach the `postgres` # service in docker-compose.yml, which `task dev` brings up first via -# dev:postgres -- keep the URL and that service in agreement. +# dev:postgres -- keep them and that service in agreement. dev:postgres also +# provisions the two roles: the services serve as archie_runtime and migrate +# as archie_migrator, while the Compose-created archie role is only the +# admin that created them. bot_user = "archie-dev" -database_url = "postgres://archie:archie@127.0.0.1:5432/archie?sslmode=disable" +database_url = "postgres://archie_runtime:archie@127.0.0.1:5432/archie?sslmode=disable" +migration_database_url = "postgres://archie_migrator:archie@127.0.0.1:5432/archie?sslmode=disable" [web] listen = "127.0.0.1:8484" diff --git a/deployments/docker-nats-stack.toml b/deployments/docker-nats-stack.toml index 071fc515e..644a7dddf 100644 --- a/deployments/docker-nats-stack.toml +++ b/deployments/docker-nats-stack.toml @@ -16,9 +16,12 @@ bot_user = "archie" work_dir = "~/.local/share/archie/work" state_dir = "~/.local/share/archie" # NATS store, task logs # The State Store's PostgreSQL connection (PostgreSQL 18, required). -# `docker compose up -d postgres` provides this local default; point it at -# your own Postgres 18 server otherwise. -database_url = "postgres://archie:archie@127.0.0.1:5432/archie?sslmode=disable" +# `docker compose up -d postgres` provides the local default; point it at +# your own Postgres 18 server otherwise. The services serve as archie_runtime +# and migrations run as archie_migrator; create both once with +# `archie-state-store provision -h` (deployments/README.md). +database_url = "postgres://archie_runtime:archie@127.0.0.1:5432/archie?sslmode=disable" +migration_database_url = "postgres://archie_migrator:archie@127.0.0.1:5432/archie?sslmode=disable" [services.gateway] target = "127.0.0.1:8585" diff --git a/deployments/single-forge-github.toml b/deployments/single-forge-github.toml index 95c112723..04efac69e 100644 --- a/deployments/single-forge-github.toml +++ b/deployments/single-forge-github.toml @@ -8,9 +8,12 @@ work_dir = "~/.local/share/archie/work" state_dir = "~/.local/share/archie" # NATS store, task logs # The State Store's PostgreSQL connection (PostgreSQL 18, required). -# `docker compose up -d postgres` provides this local default; point it at -# your own Postgres 18 server otherwise. -database_url = "postgres://archie:archie@127.0.0.1:5432/archie?sslmode=disable" +# `docker compose up -d postgres` provides the local default; point it at +# your own Postgres 18 server otherwise. The services serve as archie_runtime +# and migrations run as archie_migrator; create both once with +# `archie-state-store provision -h` (deployments/README.md). +database_url = "postgres://archie_runtime:archie@127.0.0.1:5432/archie?sslmode=disable" +migration_database_url = "postgres://archie_migrator:archie@127.0.0.1:5432/archie?sslmode=disable" poll_interval = "60s" label = "archie" bot_user = "archie-bot" diff --git a/deployments/standalone.toml b/deployments/standalone.toml index 8368a609e..53a0aeda8 100644 --- a/deployments/standalone.toml +++ b/deployments/standalone.toml @@ -15,9 +15,12 @@ bot_user = "archie" work_dir = "~/.local/share/archie/work" state_dir = "~/.local/share/archie" # NATS store, task logs # PostgreSQL 18 connection (required). -# `docker compose up -d postgres` provides this local default; point it at -# your own Postgres 18 server otherwise. -database_url = "postgres://archie:archie@127.0.0.1:5432/archie?sslmode=disable" +# `docker compose up -d postgres` provides the local default; point it at +# your own Postgres 18 server otherwise. The services serve as archie_runtime +# and migrations run as archie_migrator; create both once with +# `archie-state-store provision -h` (deployments/README.md). +database_url = "postgres://archie_runtime:archie@127.0.0.1:5432/archie?sslmode=disable" +migration_database_url = "postgres://archie_migrator:archie@127.0.0.1:5432/archie?sslmode=disable" [services.gateway] target = "127.0.0.1:8585" diff --git a/docker-compose.yml b/docker-compose.yml index de4026b40..4bc5958ba 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -19,8 +19,9 @@ services: # The standalone archie-state-store process's database. PostgreSQL 18 is # the required minimum and target; `postgres.Open` refuses anything older. # `task dev` brings this up before the State Store via dev:postgres. The - # credentials and database name below are the dev default mirrored by - # deployments/dev.toml's database_url -- change both together. + # This superuser is the dev database's admin: `task dev` runs archie-state-store + # provision after first start to create the least-privilege roles the + # mirroring deployments/dev.toml URLs name -- change them together. postgres: image: postgres:18 environment: diff --git a/e2e/stack.sh b/e2e/stack.sh index 5b458e771..296d3a308 100755 --- a/e2e/stack.sh +++ b/e2e/stack.sh @@ -72,7 +72,10 @@ start_pg() { prepare() { mkdir -p "$dir"/{bin,logs,pids} "$XDG_CONFIG_HOME/archie" "$XDG_DATA_HOME" "$XDG_STATE_HOME" [ -f "$XDG_CONFIG_HOME/archie/config.toml" ] || cp "$repo/config.example.toml" "$XDG_CONFIG_HOME/archie/config.toml" - sed "s#^database_url = .*#database_url = \"postgres://archie@127.0.0.1:$pgport/archie?sslmode=disable\"#" \ + # The services serve from the least-privilege role and migrate as the schema + # owner; initdb's archie role is the throwaway server's superuser only. + sed -e "s#^database_url = .*#database_url = \"postgres://archie_runtime@127.0.0.1:$pgport/archie?sslmode=disable\"#" \ + -e "s#^migration_database_url = .*#migration_database_url = \"postgres://archie_migrator@127.0.0.1:$pgport/archie?sslmode=disable\"#" \ "$repo/deployments/dev.toml" >"$dir/dev.toml" local version version=$(git -C "$repo" describe --tags --exact-match --match 'v[0-9]*' 2>/dev/null || true) @@ -101,6 +104,16 @@ start_service() { spawn "$1" "$repo" "$dir/bin/$1" -config-overlay "$dir/dev.toml" } +# provision_roles creates the least-privilege roles the stack connects as. The +# throwaway server runs with trust auth, so the role URLs' passwords are never +# checked; they are here only because provision sets the role password it reads. +provision_roles() { + "$dir/bin/archie-state-store" provision \ + -admin-url "postgres://archie@127.0.0.1:$pgport/archie?sslmode=disable" \ + -runtime-url "postgres://archie_runtime:e2e@127.0.0.1:$pgport/archie?sslmode=disable" \ + -migration-url "postgres://archie_migrator:e2e@127.0.0.1:$pgport/archie?sslmode=disable" +} + stop_pid() { local f=$1 pid [ -f "$f" ] || return 0 @@ -123,6 +136,7 @@ start() { trap 'trap - ERR; stop' ERR prepare start_pg 9>&- + provision_roles start_service archie-state-store wait_port 9091 archie-state-store 90 local s diff --git a/install.sh b/install.sh index 56dafa002..62a61325c 100755 --- a/install.sh +++ b/install.sh @@ -91,6 +91,17 @@ export ARCHIE_INSTANCE="${INSTANCE}" ARCHIE_CONFIG_DIR="${XDG_CONFIG_HOME}/${INSTANCE}" ARCHIE_DATA_DIR="${XDG_DATA_HOME}/${INSTANCE}" ENV_FILE="${ARCHIE_CONFIG_DIR}/env" +# The database roles a bundled install serves from: the least-privilege role +# the services connect as, and the role that owns the schema and runs +# migrations. The names mirror postgres.RuntimeRole/MigrationRole, which setup +# writes into the bundled configuration. +RUNTIME_ROLE=archie_runtime +MIGRATION_ROLE=archie_migrator +# The credential the bundled PostgreSQL is created with and the installer uses +# once, to create the two roles. It lives in this Compose-only file, which the +# systemd units never load, so no service holds a credential that owns the +# cluster. +PG_ADMIN_ENV="${ARCHIE_DATA_DIR}/postgres.env" # unit maps a base unit name to this instance's: unchanged for the default # instance, suffixed with the name after archie- otherwise. @@ -266,10 +277,55 @@ else echo " [SKIP] ${ARCHIE_CONFIG_DIR}/config.toml already exists. Re-run 'archied setup' to change it." fi +# provision_database reconciles the two database roles a bundled install +# serves from: an archie_migrator that owns the schema and runs goose, and an +# archie_runtime with row access only. It also moves the superuser password out +# of the env file the units load: a config written before the split connects as +# the bundled superuser, so its password is rotated to the Compose-only one +# once the roles exist. Idempotent, so re-running the installer reconciles an +# already-split install. +provision_database() { + local role_pw admin_pw connect_pw split=false + role_pw="$(sed -n "s/^PGPASSWORD='\([^']*\)'$/\1/p" "${ENV_FILE}" | tail -n1)" + admin_pw="$(sed -n "s/^POSTGRES_PASSWORD='\([^']*\)'$/\1/p" "${PG_ADMIN_ENV}" | tail -n1)" + if [ -z "${role_pw}" ] || [ -z "${admin_pw}" ]; then + echo " [WARN] Cannot provision database roles: ${ENV_FILE} needs PGPASSWORD and ${PG_ADMIN_ENV} needs POSTGRES_PASSWORD." >&2 + return + fi + if ! grep -q '^migration_database_url' "${ARCHIE_CONFIG_DIR}/config.toml"; then + split=true + fi + # A pre-split install authenticates as the superuser with the env file's + # PGPASSWORD; a split one authenticates with the Compose-only password. + if [ "${split}" = true ]; then connect_pw="${role_pw}"; else connect_pw="${admin_pw}"; fi + echo "==> Provisioning the database roles..." + if ! PGPASSWORD="${connect_pw}" ARCHIE_NEW_ADMIN_PASSWORD="${admin_pw}" \ + "${ARCHIE_BIN_DIR}/archie-state-store" provision \ + -admin-url "postgres://archie@/archie?host=${PG_SOCKET_DIR}" \ + -runtime-url "postgres://${RUNTIME_ROLE}:${role_pw}@/archie?host=${PG_SOCKET_DIR}" \ + -migration-url "postgres://${MIGRATION_ROLE}:${role_pw}@/archie?host=${PG_SOCKET_DIR}" \ + -rotate-admin-password-env ARCHIE_NEW_ADMIN_PASSWORD; then + echo " [WARN] Database role provisioning failed; not restarting the services until it succeeds. Re-run the installer once the database accepts the admin credential." >&2 + AUTO_START=false + return + fi + if [ "${split}" = true ]; then + if grep -q '^database_url = "postgres://archie@' "${ARCHIE_CONFIG_DIR}/config.toml"; then + sed -i "s#^database_url = \"postgres://archie@#database_url = \"postgres://${RUNTIME_ROLE}@#" "${ARCHIE_CONFIG_DIR}/config.toml" + sed -i "/^database_url = /a migration_database_url = \"postgres://${MIGRATION_ROLE}@/archie?host=${PG_SOCKET_DIR}\"" "${ARCHIE_CONFIG_DIR}/config.toml" + echo " [OK] config.toml now connects as ${RUNTIME_ROLE} and migrates as ${MIGRATION_ROLE}." + else + echo " [WARN] Could not move database_url to ${RUNTIME_ROLE}; set database_url and migration_database_url by hand." >&2 + fi + fi +} + # 4. PostgreSQL. Every service fails closed without it. The bundled database # has no network at all: the services reach it through its Unix socket, # mounted at PG_SOCKET_DIR, so it publishes no port and cannot collide with -# another PostgreSQL. Its password is the PGPASSWORD setup generated. +# another PostgreSQL. The superuser it is created with is only the installer's +# bootstrap credential; the services connect as the two provisioned roles and +# read their password from the env file's PGPASSWORD as before. PG_SOCKET_DIR="${ARCHIE_DATA_DIR}/postgres" PG_COMPOSE="${ARCHIE_DATA_DIR}/postgres.yml" if ! grep -qF "host=${PG_SOCKET_DIR}" "${ARCHIE_CONFIG_DIR}/config.toml"; then @@ -280,6 +336,15 @@ elif ! command -v docker &>/dev/null; then else echo "==> Starting the bundled PostgreSQL 18..." mkdir -p "${PG_SOCKET_DIR}" + # The superuser password is generated here rather than reused from setup: it + # is the one credential that must never be in the env file the units load. + # Written before the first start so a failed run can be retried with it. + if [ ! -f "${PG_ADMIN_ENV}" ]; then + previous_umask="$(umask)" + umask 077 + printf "POSTGRES_PASSWORD='%s'\n" "$(od -An -N24 -tx1 /dev/urandom | tr -d ' \n')" >"${PG_ADMIN_ENV}" + umask "${previous_umask}" + fi cat >"${PG_COMPOSE}" <<'EOF' services: postgres: @@ -287,7 +352,7 @@ services: network_mode: none environment: POSTGRES_USER: archie - POSTGRES_PASSWORD: ${PGPASSWORD:-} + POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-} POSTGRES_DB: archie POSTGRES_INITDB_ARGS: --auth-local=scram-sha-256 PGDATA: /var/lib/postgresql/data/pgdata @@ -304,10 +369,12 @@ services: volumes: pg_data: EOF - if ! docker compose -p "${INSTANCE}" --env-file "${ENV_FILE}" -f "${PG_COMPOSE}" up -d --wait; then + if ! docker compose -p "${INSTANCE}" --env-file "${PG_ADMIN_ENV}" -f "${PG_COMPOSE}" up -d --wait; then echo " [WARN] The bundled PostgreSQL did not start, so the services are installed but not started." >&2 echo " Check: docker compose -p "${INSTANCE}" -f ${PG_COMPOSE} logs" >&2 AUTO_START=false + else + provision_database fi fi @@ -495,6 +562,9 @@ echo "Installation Details:" echo " - Binaries : ${ARCHIE_BIN_DIR}/{archied,archie-state-store,archie-gateway,archie-ui,archie-messaging}" echo " - Config : ${ARCHIE_CONFIG_DIR}/config.toml" echo " - Secrets : ${ENV_FILE}" +if [ -f "${PG_ADMIN_ENV}" ]; then + echo " - DB admin : ${PG_ADMIN_ENV} # compose-only, never loaded by the units" +fi echo " - Data : ${ARCHIE_DATA_DIR}/" echo "" diff --git a/internal/app/archiegateway/chat_runtime.go b/internal/app/archiegateway/chat_runtime.go index 195e16df4..5ae5042dd 100644 --- a/internal/app/archiegateway/chat_runtime.go +++ b/internal/app/archiegateway/chat_runtime.go @@ -21,7 +21,7 @@ import ( ) func (b *server) openChatSessions(ctx context.Context) error { - pool, err := postgres.OpenMigrated(ctx, b.cfg.DatabaseURL, "the conversation store") + pool, err := postgres.OpenMigrated(ctx, b.cfg.DatabaseURL, b.cfg.MigrationURL(), postgres.AppGateway) if err != nil { return fmt.Errorf("open conversation store: %w", err) } diff --git a/internal/app/statestore/pool.go b/internal/app/statestore/pool.go index 6f2e673aa..f3e82df7f 100644 --- a/internal/app/statestore/pool.go +++ b/internal/app/statestore/pool.go @@ -9,13 +9,15 @@ import ( // openStateStorePool opens the State Store process's one process-scoped // PostgreSQL pool, applies the schema and claims serve ownership of the // database for the process's life, so a second State Store against the same -// database refuses to start. A missing or invalid database_url or a migration -// failure is a startup failure. +// database refuses to start. The schema is applied through +// migration_database_url when the configuration splits the migration role from +// the serving one. A missing +// or invalid database_url or a migration failure is a startup failure. // // The pool is stored on boot.pg and closed at shutdown; nothing opens a second // pool per subsystem. func (b *server) openStateStorePool(ctx context.Context) error { - pool, err := postgres.OpenMigrated(ctx, b.cfg.DatabaseURL, "the State Store") + pool, err := postgres.OpenMigrated(ctx, b.cfg.DatabaseURL, b.cfg.MigrationURL(), postgres.AppStateStore) if err != nil { b.log.ErrorContext(ctx, "open state store postgres pool", "err", err) return err diff --git a/internal/app/statestore/provision.go b/internal/app/statestore/provision.go new file mode 100644 index 000000000..20207844a --- /dev/null +++ b/internal/app/statestore/provision.go @@ -0,0 +1,99 @@ +package statestore + +import ( + "context" + "errors" + "flag" + "fmt" + "io" + "os" + "strings" + + "github.com/samcharles93/archie-core/internal/infrastructure/postgres" +) + +// provisionCommand is the argv[1] token that runs the one-time database role +// setup instead of serving. Provisioning has to happen before the first +// migration and needs a credential the services never hold, so it cannot be a +// migration and is not part of any service's boot. +const provisionCommand = "provision" + +// provisionUsage is the whole flag surface of the provisioning command. +const provisionUsage = `usage: archie-state-store provision [flags] + +provision reconciles the PostgreSQL roles an install serves from: + + - a migration role that owns the schema and runs goose; + - a runtime role with row access only (SELECT, INSERT, UPDATE, DELETE, plus + sequence USAGE and function EXECUTE), which the services connect as. + +It creates the two roles when missing, hands every existing public-schema +object to the migration role, and sets the default privileges that keep the +objects future migrations create usable by the runtime role. It is idempotent: +re-running it on a provisioned database reconciles it back to that state. + +The admin URL needs a role that may create roles and take ownership of the +schema (the database owner or a superuser). It is a bootstrap credential: it is +not one of the two roles above and the services never read it. + +Passwords come from each URL's userinfo, or from PGPASSWORD when the URL does +not carry one. + + -admin-url URL role that creates and owns (database owner or superuser) + -runtime-url URL least-privilege role the services connect as + -migration-url URL role that owns the schema and runs migrations + -rotate-admin-password-env NAME + replace the admin role's password with the value of + environment variable NAME once provisioning succeeds +` + +// IsProvisionArgs reports whether args selects the role provisioning command. +func IsProvisionArgs(args []string) bool { + return len(args) > 0 && args[0] == provisionCommand +} + +// RunProvision performs the role provisioning and returns the process exit +// code: 0 provisioned, 1 the provisioning failed, 2 the command line was +// wrong. +func RunProvision(ctx context.Context, args []string, stdout, stderr io.Writer) int { + flags := flag.NewFlagSet(provisionCommand, flag.ContinueOnError) + flags.SetOutput(stderr) + flags.Usage = func() { fmt.Fprint(stderr, provisionUsage) } + var options postgres.ProvisionOptions + flags.StringVar(&options.AdminURL, "admin-url", "", "PostgreSQL URL of a role allowed to create roles and own the schema (database owner or superuser)") + flags.StringVar(&options.RuntimeURL, "runtime-url", "", "PostgreSQL URL of the least-privilege role the services connect as") + flags.StringVar(&options.MigrationURL, "migration-url", "", "PostgreSQL URL of the role that owns the schema and runs migrations") + rotateEnv := flags.String("rotate-admin-password-env", "", "name of an environment variable holding the admin role's new password") + if err := flags.Parse(args); err != nil { + // A requested help is not a wrong command line: the usage above is the + // answer. + if errors.Is(err, flag.ErrHelp) { + return 0 + } + return 2 + } + if flags.NArg() > 0 { + fmt.Fprintf(stderr, "provision: unexpected argument %q\n", flags.Arg(0)) + return 2 + } + if *rotateEnv != "" { + password, ok := os.LookupEnv(*rotateEnv) + if !ok || password == "" { + fmt.Fprintf(stderr, "provision: %s is unset or empty\n", *rotateEnv) + return 2 + } + options.NewAdminPassword = password + } + + result, err := postgres.Provision(ctx, options) + if err != nil { + fmt.Fprintf(stderr, "provision: %v\n", err) + return 1 + } + if len(result.RolesCreated) > 0 { + fmt.Fprintf(stdout, "provision: created %s\n", strings.Join(result.RolesCreated, ", ")) + } + fmt.Fprintf(stdout, "provision: %s owns the schema; %s may read and write rows but not change the schema\n", result.MigrationRole, result.RuntimeRole) + fmt.Fprintf(stdout, "provision: reconciled %d tables, %d sequences and %d functions\n", result.Tables, result.Sequences, result.Functions) + return 0 +} diff --git a/internal/app/statestore/recovery.go b/internal/app/statestore/recovery.go index 5f3c084c2..e629d82d4 100644 --- a/internal/app/statestore/recovery.go +++ b/internal/app/statestore/recovery.go @@ -4,12 +4,13 @@ // starting, and the in-band remedy -- replaying an earlier revision while the // State Store is up -- is unavailable in exactly the case that needs it // -// Every operation but import works on the PostgreSQL database database_url -// names (state_store_recovery_postgres.go). The ones that write refuse while a -// serving process owns the store. Backup is the exception and deliberately so: -// the update installer cannot stop the process that runs it, so the snapshot -// that makes a failed update recoverable is taken against a serving store -// through a consistent copy. +// Every operation but import works on the PostgreSQL database +// migration_database_url names, or database_url when the install has not split +// the roles (state_store_recovery_postgres.go). The ones that write refuse +// while a serving process owns the store. Backup is the exception and +// deliberately so: the update installer cannot stop the process that runs it, +// so the snapshot that makes a failed update recoverable is taken against a +// serving store through a consistent copy. // // The control-plane server both validate and rollback read through is built by // openStateStoreControlPlane -- the same root helper the served path uses -- so diff --git a/internal/app/statestore/recovery_postgres.go b/internal/app/statestore/recovery_postgres.go index 7ba6f5f5d..a18f74cfa 100644 --- a/internal/app/statestore/recovery_postgres.go +++ b/internal/app/statestore/recovery_postgres.go @@ -11,15 +11,18 @@ import ( ) // runPostgresRecovery performs one recovery operation on the PostgreSQL -// database the configuration names. There is no automatic schema rollback: -// goose Down drops tables, so the way back from a migration is restoring the -// snapshot taken before it, which loses every write made after that snapshot. +// database the configuration names. It connects as the migration role +// (migration_database_url, falling back to database_url): restoring and +// rolling back replace schema objects, which only the schema's owner may do. +// There is no automatic schema rollback: goose Down drops tables, so the way +// back from a migration is restoring the snapshot taken before it, which loses +// every write made after that snapshot. func runPostgresRecovery(ctx context.Context, options RecoveryOptions) (string, error) { cfg, err := bootConfig(ctx, options) if err != nil { return "", err } - url := cfg.DatabaseURL + url := cfg.MigrationURL() if url == "" { return "", errors.New("database_url is required: the configuration names no PostgreSQL database") } @@ -55,7 +58,7 @@ func runPostgresRecovery(ctx context.Context, options RecoveryOptions) (string, // openRecoveryPool opens the database without migrating it: recovery inspects // or repairs the schema it finds, and refuses one newer than this binary. func openRecoveryPool(ctx context.Context, url string) (*pgxpool.Pool, int64, error) { - pool, err := postgres.Open(ctx, url) + pool, err := postgres.Open(ctx, url, postgres.AppStateStore+"-recovery") if err != nil { return nil, 0, err } @@ -119,7 +122,9 @@ func SchemaVersion(ctx context.Context, options RecoveryOptions) (int64, error) if cfg.DatabaseURL == "" { return 0, errors.New("database_url is required: the configuration names no PostgreSQL database") } - pool, err := postgres.Open(ctx, cfg.DatabaseURL) + // The updater asks this while the services serve, so it reads the version + // through the runtime role: SELECT on goose's table is all it needs. + pool, err := postgres.Open(ctx, cfg.DatabaseURL, postgres.AppStateStore+"-schema") if err != nil { return 0, err } diff --git a/internal/config/config.go b/internal/config/config.go index dd0ee8ad8..d737b80e0 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -366,10 +366,15 @@ type Config struct { // embedded NATS store and its endpoint file, the task-log registry, and // the readiness disk probe's data target. Bootstrap-only. StateDir string `toml:"state_dir" yaml:"state_dir"` - // DatabaseURL is the PostgreSQL URL the State Store opens at boot. Required; + // DatabaseURL is the PostgreSQL URL the services open at boot. Required; // bootstrap-only. - DatabaseURL string `toml:"database_url" yaml:"database_url"` - PollInterval Duration `toml:"poll_interval" yaml:"poll_interval"` + DatabaseURL string `toml:"database_url" yaml:"database_url"` + // MigrationDatabaseURL is the PostgreSQL URL migrations and schema-changing + // recovery run under: a role that owns the schema, not the one serving + // requests. Empty falls back to DatabaseURL, which is how a single-role + // install keeps working. Bootstrap-only. + MigrationDatabaseURL string `toml:"migration_database_url" yaml:"migration_database_url"` + PollInterval Duration `toml:"poll_interval" yaml:"poll_interval"` // Label marks issues archie should pick up. Label string `toml:"label" yaml:"label"` BotUser string `toml:"bot_user" yaml:"bot_user"` @@ -428,6 +433,16 @@ type Config struct { Curators []CuratorDefinition `toml:"curators" yaml:"curators" json:"curators,omitempty"` } +// MigrationURL is the URL migrations and schema-changing recovery run under. +// It falls back to DatabaseURL so an install that has not split the runtime +// and migration roles behaves as it always has. +func (c Config) MigrationURL() string { + if c.MigrationDatabaseURL != "" { + return c.MigrationDatabaseURL + } + return c.DatabaseURL +} + // IdentityConfig is a per-identity configuration subset. Forge access comes // from the identity's organisation connections; runtime settings come from // the shared control plane. diff --git a/internal/infrastructure/configuration/denied_keys.go b/internal/infrastructure/configuration/denied_keys.go index 4a01c1729..d20586b71 100644 --- a/internal/infrastructure/configuration/denied_keys.go +++ b/internal/infrastructure/configuration/denied_keys.go @@ -4,6 +4,7 @@ package configuration // to the reason shown to the operator. var DeniedKeys = map[string]string{ "database_url": "Set in the config file. Needs a restart.", + "migration_database_url": "Set in the config file. Needs a restart.", "state_dir": "Set in the config file. Needs a restart.", "work_dir": "Set in the config file. Needs a restart.", "forge.type": "Needs a restart.", diff --git a/internal/infrastructure/configuration/setup/setup.go b/internal/infrastructure/configuration/setup/setup.go index bcb522158..977152798 100644 --- a/internal/infrastructure/configuration/setup/setup.go +++ b/internal/infrastructure/configuration/setup/setup.go @@ -12,6 +12,7 @@ import ( "github.com/samcharles93/archie-core/internal/config" "github.com/samcharles93/archie-core/internal/infrastructure/configuration" "github.com/samcharles93/archie-core/internal/infrastructure/configuration/tomlwrite" + "github.com/samcharles93/archie-core/internal/infrastructure/postgres" ) // SecretSink receives the secret values setup collects. Put buffers; the @@ -82,11 +83,15 @@ func RunParams(ctx context.Context, p Prompter, discovery ModelDiscovery, secret } } - dbURL, err := stepDatabase(ctx, p, secrets, existing.DatabaseURL, params.DatabaseURL) + dbURL, migrationURL, err := stepDatabase(ctx, p, secrets, existing.DatabaseURL, params.DatabaseURL) if err != nil { return nil, err } - add(tableEdits{"": {"database_url": tomlwrite.String(dbURL)}}) + dbEdits := tableEdits{"": {"database_url": tomlwrite.String(dbURL)}} + if migrationURL != "" { + dbEdits[""]["migration_database_url"] = tomlwrite.String(migrationURL) + } + add(dbEdits) botUser := params.BotUser if strings.TrimSpace(botUser) == "" { @@ -135,14 +140,15 @@ func RunParams(ctx context.Context, p Prompter, discovery ModelDiscovery, secret return edits, nil } -// stepDatabase returns the PostgreSQL URL. A blank answer on a fresh install -// means the bundled database. On a re-run with an existing URL the bundled -// database is an explicit choice rather than something reachable only by -// typing its socket URL; keeping the existing URL is the first option, so -// the default answer is unchanged. -func stepDatabase(ctx context.Context, p Prompter, secrets SecretSink, existing, param string) (string, error) { +// stepDatabase returns the PostgreSQL URLs the install serves from: the +// runtime role's URL and, for the bundled database, the migration role's. A +// blank answer on a fresh install means the bundled database. On a re-run with +// an existing URL the bundled database is an explicit choice rather than +// something reachable only by typing its socket URL; keeping the existing URL +// is the first option, so the default answer is unchanged. +func stepDatabase(ctx context.Context, p Prompter, secrets SecretSink, existing, param string) (runtimeURL, migrationURL string, err error) { if strings.TrimSpace(param) != "" { - return param, nil + return param, "", nil } if strings.TrimSpace(existing) != "" { choice, err := p.Select(ctx, "Database:", []string{ @@ -151,48 +157,53 @@ func stepDatabase(ctx context.Context, p Prompter, secrets SecretSink, existing, "Enter a different PostgreSQL 18 URL", }) if err != nil { - return "", fmt.Errorf("setup: database choice: %w", err) + return "", "", fmt.Errorf("setup: database choice: %w", err) } switch choice { case 0: - return existing, nil + return existing, "", nil case 1: return bundledDatabase(secrets) default: url, err := p.ReadLine(ctx, "PostgreSQL 18 URL: ", "") if err != nil { - return "", fmt.Errorf("setup: database url: %w", err) + return "", "", fmt.Errorf("setup: database url: %w", err) } if strings.TrimSpace(url) == "" { // A blank answer keeps the existing URL, as it always has. - return existing, nil + return existing, "", nil } - return url, nil + return url, "", nil } } prompt := "PostgreSQL 18 URL (blank for the bundled database with a generated password): " url, err := p.ReadLine(ctx, prompt, "") if err != nil { - return "", fmt.Errorf("setup: database url: %w", err) + return "", "", fmt.Errorf("setup: database url: %w", err) } if strings.TrimSpace(url) != "" { - return url, nil + return url, "", nil } return bundledDatabase(secrets) } // bundledDatabase switches to the bundled database: no network, reached -// through its Unix socket, with a generated password that goes only to the -// env file as PGPASSWORD, which pgx and Compose both read, so config.toml -// holds no secret. -func bundledDatabase(secrets SecretSink) (string, error) { +// through its Unix socket. The services connect as the least-privilege runtime +// role and migrations run as the schema-owning migration role; one generated +// password goes only to the env file as PGPASSWORD, which pgx reads for both +// passwordless URLs, so config.toml holds no secret. The superuser credential +// the bundled server is created with lives in the installer's Compose-only +// env file, never here. +func bundledDatabase(secrets SecretSink) (runtimeURL, migrationURL string, err error) { pw := make([]byte, 24) if _, err := rand.Read(pw); err != nil { - return "", fmt.Errorf("setup: generate database password: %w", err) + return "", "", fmt.Errorf("setup: generate database password: %w", err) } password := hex.EncodeToString(pw) if err := secrets.Put("env", "PGPASSWORD", password); err != nil { - return "", fmt.Errorf("setup: store database password: %w", err) + return "", "", fmt.Errorf("setup: store database password: %w", err) } - return "postgres://archie@/archie?host=" + configuration.DefaultPostgresSocketDir(), nil + socket := configuration.DefaultPostgresSocketDir() + return "postgres://" + postgres.RuntimeRole + "@/archie?host=" + socket, + "postgres://" + postgres.MigrationRole + "@/archie?host=" + socket, nil } diff --git a/internal/infrastructure/configuration/setup/setup_test.go b/internal/infrastructure/configuration/setup/setup_test.go index aa8120eb0..59cf40fb0 100644 --- a/internal/infrastructure/configuration/setup/setup_test.go +++ b/internal/infrastructure/configuration/setup/setup_test.go @@ -77,7 +77,11 @@ const ( ) func bundledURL() string { - return "postgres://archie@/archie?host=" + configuration.DefaultPostgresSocketDir() + return "postgres://archie_runtime@/archie?host=" + configuration.DefaultPostgresSocketDir() +} + +func bundledMigrationURL() string { + return "postgres://archie_migrator@/archie?host=" + configuration.DefaultPostgresSocketDir() } // TestRunParamsDatabaseChoice pins the re-run database step at the setup API @@ -92,12 +96,13 @@ func TestRunParamsDatabaseChoice(t *testing.T) { // existing is the prefilled database_url; empty means fresh install. existing string // param is Params.DatabaseURL (the -database-url flag). - param string - selectAnswers map[string]int - lineAnswers map[string]string - wantURL string - wantPassword bool - wantChoice bool // a database Select menu must be presented + param string + selectAnswers map[string]int + lineAnswers map[string]string + wantURL string + wantMigrationURL string // empty means the flow must not write one + wantPassword bool + wantChoice bool // a database Select menu must be presented }{ { name: "re-run keep existing is the default choice", @@ -107,12 +112,13 @@ func TestRunParamsDatabaseChoice(t *testing.T) { wantChoice: true, }, { - name: "re-run switch to the bundled database", - existing: existing, - selectAnswers: map[string]int{databasePrompt: bundledChoice}, - wantURL: bundledURL(), - wantPassword: true, - wantChoice: true, + name: "re-run switch to the bundled database", + existing: existing, + selectAnswers: map[string]int{databasePrompt: bundledChoice}, + wantURL: bundledURL(), + wantMigrationURL: bundledMigrationURL(), + wantPassword: true, + wantChoice: true, }, { name: "re-run enter a different URL", @@ -131,12 +137,13 @@ func TestRunParamsDatabaseChoice(t *testing.T) { wantChoice: true, }, { - name: "fresh install blank answer means the bundled database", - existing: "", - lineAnswers: map[string]string{"PostgreSQL 18 URL (blank for the bundled database with a generated password): ": ""}, - wantURL: bundledURL(), - wantPassword: true, - wantChoice: false, + name: "fresh install blank answer means the bundled database", + existing: "", + lineAnswers: map[string]string{"PostgreSQL 18 URL (blank for the bundled database with a generated password): ": ""}, + wantURL: bundledURL(), + wantMigrationURL: bundledMigrationURL(), + wantPassword: true, + wantChoice: false, }, { name: "database-url flag wins without prompting", @@ -167,15 +174,28 @@ func TestRunParamsDatabaseChoice(t *testing.T) { t.Fatalf("RunParams: %v", err) } - var gotURL string + var gotURL, gotMigrationURL string for _, e := range edits { - if e.Table == "" && e.Key == "database_url" { + if e.Table != "" { + continue + } + switch e.Key { + case "database_url": gotURL = e.Value + case "migration_database_url": + gotMigrationURL = e.Value } } if gotURL != tomlwrite.String(tt.wantURL) { t.Errorf("database_url = %s, want %s", gotURL, tomlwrite.String(tt.wantURL)) } + wantMigrationURL := "" + if tt.wantMigrationURL != "" { + wantMigrationURL = tomlwrite.String(tt.wantMigrationURL) + } + if gotMigrationURL != wantMigrationURL { + t.Errorf("migration_database_url = %q, want %q", gotMigrationURL, wantMigrationURL) + } password, hasPassword := sink.puts["env/PGPASSWORD"] if hasPassword != tt.wantPassword { diff --git a/internal/infrastructure/postgres/maintenance.go b/internal/infrastructure/postgres/maintenance.go index bf211ce44..2d8676522 100644 --- a/internal/infrastructure/postgres/maintenance.go +++ b/internal/infrastructure/postgres/maintenance.go @@ -139,7 +139,7 @@ func Restore(ctx context.Context, databaseURL, from string) error { if err != nil { return err } - pool, err := Open(ctx, databaseURL) + pool, err := Open(ctx, databaseURL, AppStateStore+"-restore") if err != nil { return fmt.Errorf("postgres: restore cannot confirm every service is stopped: %w", err) } diff --git a/internal/infrastructure/postgres/postgres.go b/internal/infrastructure/postgres/postgres.go index f5d442ab2..e453c4a50 100644 --- a/internal/infrastructure/postgres/postgres.go +++ b/internal/infrastructure/postgres/postgres.go @@ -18,6 +18,18 @@ import ( // minServerVersion is PostgreSQL 18 in server_version_num form. const minServerVersion = 180000 +// The application_name each service sets on its connections, so +// pg_stat_activity says which service a connection belongs to. PostgreSQL +// truncates the value at 63 bytes, which every name below is inside. +const ( + AppStateStore = "archie-state-store" + AppGateway = "archie-gateway" +) + +// migrationSuffix marks the transient pool that applies migrations, so the +// activity view distinguishes it from the pool that serves traffic. +const migrationSuffix = "-migrate" + //go:embed migrations/*.sql var migrations embed.FS @@ -42,8 +54,21 @@ func checkServerVersion(num int) error { } // Open connects a pool to url and refuses a server older than PostgreSQL 18. -func Open(ctx context.Context, url string) (*pgxpool.Pool, error) { - pool, err := pgxpool.New(ctx, url) +// applicationName is the service name every connection reports to +// pg_stat_activity; it overrides any name the URL carries, so no connection +// arrives unnamed because a deployment template forgot one. +func Open(ctx context.Context, url, applicationName string) (*pgxpool.Pool, error) { + poolConfig, err := pgxpool.ParseConfig(url) + if err != nil { + return nil, fmt.Errorf("postgres: open: %w", err) + } + if applicationName != "" { + if poolConfig.ConnConfig.RuntimeParams == nil { + poolConfig.ConnConfig.RuntimeParams = map[string]string{} + } + poolConfig.ConnConfig.RuntimeParams["application_name"] = applicationName + } + pool, err := pgxpool.NewWithConfig(ctx, poolConfig) if err != nil { return nil, fmt.Errorf("postgres: open: %w", err) } @@ -60,20 +85,44 @@ func Open(ctx context.Context, url string) (*pgxpool.Pool, error) { } // OpenMigrated opens and migrates the pool a service owns its tables in. -// service names the owner in the error an empty database_url produces. -func OpenMigrated(ctx context.Context, url, service string) (*pgxpool.Pool, error) { - if url == "" { - return nil, fmt.Errorf("database_url is required: %s is Postgres-only", service) +// applicationName names the service in pg_stat_activity. +// +// runtimeURL is the pool the service serves from; migrationURL is the role +// that owns the schema and applies migrations. An empty migrationURL falls +// back to runtimeURL: an install that has not split the roles keeps working +// exactly as before, applying the schema through its one role. +func OpenMigrated(ctx context.Context, runtimeURL, migrationURL, applicationName string) (*pgxpool.Pool, error) { + if runtimeURL == "" { + return nil, fmt.Errorf("database_url is required: %s is PostgreSQL-only", applicationName) } - pool, err := Open(ctx, url) + if migrationURL == "" { + migrationURL = runtimeURL + } + if migrationURL == runtimeURL { + // One role for both jobs: migrate on the serving pool, as before. + pool, err := Open(ctx, runtimeURL, applicationName) + if err != nil { + return nil, err + } + if err := Migrate(ctx, pool, Migrations()); err != nil { + pool.Close() + return nil, err + } + return pool, nil + } + // The migration role is not the serving role: migrate on a short-lived + // pool of its own, then open the serving pool. The two do not overlap, so a + // runtime role with no schema privileges never carries goose's work. + migrationPool, err := Open(ctx, migrationURL, applicationName+migrationSuffix) if err != nil { return nil, err } - if err := Migrate(ctx, pool, Migrations()); err != nil { - pool.Close() - return nil, err + migrateErr := Migrate(ctx, migrationPool, Migrations()) + migrationPool.Close() + if migrateErr != nil { + return nil, migrateErr } - return pool, nil + return Open(ctx, runtimeURL, applicationName) } // Migrate applies the goose migrations in fsys. A session-level advisory lock diff --git a/internal/infrastructure/postgres/provision.go b/internal/infrastructure/postgres/provision.go new file mode 100644 index 000000000..c74354d24 --- /dev/null +++ b/internal/infrastructure/postgres/provision.go @@ -0,0 +1,534 @@ +package postgres + +import ( + "context" + "crypto/hmac" + "crypto/rand" + "crypto/sha256" + "encoding/base64" + "errors" + "fmt" + "net/url" + "os" + "strings" + + "github.com/jackc/pgx/v5" + "golang.org/x/crypto/pbkdf2" +) + +// The roles the bundled installer provisions. An install pointed at its own +// database may name different roles in its URLs; these are the names setup +// writes into the bundled configuration. +const ( + RuntimeRole = "archie_runtime" + MigrationRole = "archie_migrator" +) + +// provisionAdminSuffix names the provisioning connection in pg_stat_activity. +const provisionAdminSuffix = "-provision" + +// ProvisionOptions names the three roles one provision run reconciles. +type ProvisionOptions struct { + // AdminURL is a role allowed to create roles and take ownership of the + // schema: the database owner or a superuser. It is a bootstrap credential, + // used once into the database and never stored by the services, and it + // must not be one of the two roles being provisioned. + AdminURL string + // RuntimeURL is the least-privilege role the services connect as. + RuntimeURL string + // MigrationURL is the role that owns the schema and runs migrations. + MigrationURL string + // NewAdminPassword, when set, replaces the admin role's password at the end + // of a successful run. The installer uses it to move the superuser + // credential out of the env file the services load. + NewAdminPassword string +} + +// ProvisionResult reports what one provision run reconciled. +type ProvisionResult struct { + RuntimeRole string + MigrationRole string + AdminRole string + // RolesCreated lists the roles this run created; a re-run creates none. + RolesCreated []string + // Tables, Sequences and Functions count the public objects this run took + // ownership of. A fresh database has none yet; the migration role's default + // privileges cover the objects its first migration creates. + Tables int + Sequences int + Functions int +} + +// provisionRoles is the role set a run reconciles, with the password each +// login must have afterwards. +type provisionRoles struct { + admin provisionRole + runtime provisionRole + migration provisionRole +} + +type provisionRole struct { + name string + password string +} + +// Provision makes the database safe to serve from: it creates the migration +// role that owns the schema and the runtime role with only row access, then +// hands every existing object to the migration role and sets the default +// privileges that keep objects created by future migrations usable by the +// runtime. It is idempotent: running it on a provisioned database reconciles +// it back to that state. +// +// The connection URLs also carry each role's password (or PGPASSWORD when they +// do not). Provisioning is the only place a superuser password is needed; it +// is not stored anywhere the services read. +func Provision(ctx context.Context, opts ProvisionOptions) (ProvisionResult, error) { + roles, err := parseProvisionURLs(opts, os.Getenv) + if err != nil { + return ProvisionResult{}, err + } + pool, err := Open(ctx, opts.AdminURL, AppStateStore+provisionAdminSuffix) + if err != nil { + return ProvisionResult{}, err + } + defer pool.Close() + + tx, err := pool.Begin(ctx) + if err != nil { + return ProvisionResult{}, fmt.Errorf("postgres: provision: begin: %w", err) + } + defer func() { _ = tx.Rollback(ctx) }() + result, err := provisionTx(ctx, tx, roles, opts.NewAdminPassword) + if err != nil { + return ProvisionResult{}, err + } + if err := tx.Commit(ctx); err != nil { + return ProvisionResult{}, fmt.Errorf("postgres: provision: commit: %w", err) + } + return result, nil +} + +// provisionTx performs one provisioning run inside tx, so a failure at any +// step leaves the database as it was. +func provisionTx(ctx context.Context, tx pgx.Tx, roles provisionRoles, newAdminPassword string) (ProvisionResult, error) { + result := ProvisionResult{ + RuntimeRole: roles.runtime.name, + MigrationRole: roles.migration.name, + AdminRole: roles.admin.name, + } + for _, role := range []provisionRole{roles.runtime, roles.migration} { + created, err := ensureProvisionRole(ctx, tx, role) + if err != nil { + return ProvisionResult{}, err + } + if created { + result.RolesCreated = append(result.RolesCreated, role.name) + } + } + // The schema owner must change before the objects move: PostgreSQL only + // lets an object's owner (or a superuser) hand it to the migration role. + for _, statement := range schemaStatements(roles) { + if _, err := tx.Exec(ctx, statement); err != nil { + return ProvisionResult{}, fmt.Errorf("postgres: provision %s: %w", statement, err) + } + } + objects, err := schemaObjects(ctx, tx, roles.migration.name) + if err != nil { + return ProvisionResult{}, err + } + for _, object := range objects { + statement := object.ownerStatement(roles.migration.name) + if _, err := tx.Exec(ctx, statement); err != nil { + return ProvisionResult{}, fmt.Errorf("postgres: provision %s: %w", statement, err) + } + switch object.kind { + case objectTable, objectForeignTable, objectPartitionedTable: + result.Tables++ + case objectSequence: + result.Sequences++ + default: + result.Functions++ + } + } + if newAdminPassword != "" { + if err := setRolePassword(ctx, tx, roles.admin.name, newAdminPassword); err != nil { + return ProvisionResult{}, err + } + } + if err := verifyProvision(ctx, tx, roles); err != nil { + return ProvisionResult{}, err + } + return result, nil +} + +// ensureProvisionRole creates role as a non-superuser login when it is missing, +// and reconciles an existing role back to that shape. It refuses a role that +// is still a superuser: demoting a superuser is the operator's decision, not a +// side effect of an install. +func ensureProvisionRole(ctx context.Context, tx pgx.Tx, role provisionRole) (bool, error) { + verifier, err := scramVerifier(role.password) + if err != nil { + return false, err + } + var superuser bool + err = tx.QueryRow(ctx, "SELECT rolsuper FROM pg_roles WHERE rolname = $1", role.name).Scan(&superuser) + switch { + case errors.Is(err, pgx.ErrNoRows): + statement := "CREATE ROLE " + quoteIdent(role.name) + " LOGIN NOSUPERUSER NOCREATEDB NOCREATEROLE PASSWORD " + quoteLiteral(verifier) + if _, err := tx.Exec(ctx, statement); err != nil { + return false, fmt.Errorf("postgres: provision create role %s: %w", role.name, err) + } + return true, nil + case err != nil: + return false, fmt.Errorf("postgres: provision read role %s: %w", role.name, err) + case superuser: + return false, fmt.Errorf("postgres: provision refuses role %q: it is a superuser; name a least-privilege role instead", role.name) + } + // An existing role may have been created by hand with more than it needs. + statement := "ALTER ROLE " + quoteIdent(role.name) + " LOGIN NOSUPERUSER NOCREATEDB NOCREATEROLE PASSWORD " + quoteLiteral(verifier) + if _, err := tx.Exec(ctx, statement); err != nil { + return false, fmt.Errorf("postgres: provision update role %s: %w", role.name, err) + } + return false, nil +} + +// setRolePassword stores a new password for role without sending the plaintext +// to the server as SQL. +func setRolePassword(ctx context.Context, tx pgx.Tx, role, password string) error { + verifier, err := scramVerifier(password) + if err != nil { + return err + } + statement := "ALTER ROLE " + quoteIdent(role) + " PASSWORD " + quoteLiteral(verifier) + if _, err := tx.Exec(ctx, statement); err != nil { + return fmt.Errorf("postgres: provision rotate %s password: %w", role, err) + } + return nil +} + +// schemaStatements is the fixed part of a provision run: who owns the schema +// and what the runtime role may do in it. Every statement is idempotent, so a +// re-run is a reconciliation rather than a second install. +func schemaStatements(roles provisionRoles) []string { + runtime := quoteIdent(roles.runtime.name) + migration := quoteIdent(roles.migration.name) + return []string{ + // The migration role owns the namespace it manufactures the schema in. + "ALTER SCHEMA public OWNER TO " + migration, + // PostgreSQL 15 made this the default for a new database; an install + // created before that may still let every role create objects here. + "REVOKE CREATE ON SCHEMA public FROM PUBLIC", + "GRANT USAGE ON SCHEMA public TO " + runtime, + "GRANT USAGE, CREATE ON SCHEMA public TO " + migration, + // The runtime role reads and writes rows, and nothing else: no TRUNCATE, + // no ownership, no CREATE. The app's queries never ask for more. + "GRANT SELECT, INSERT, UPDATE, DELETE ON ALL TABLES IN SCHEMA public TO " + runtime, + "GRANT USAGE, SELECT ON ALL SEQUENCES IN SCHEMA public TO " + runtime, + "GRANT EXECUTE ON ALL FUNCTIONS IN SCHEMA public TO " + runtime, + // Tables and sequences a future migration creates are owned by the + // migration role, so its default privileges are what keeps the runtime + // role able to use them without a later grant. + "ALTER DEFAULT PRIVILEGES FOR ROLE " + migration + " IN SCHEMA public GRANT SELECT, INSERT, UPDATE, DELETE ON TABLES TO " + runtime, + "ALTER DEFAULT PRIVILEGES FOR ROLE " + migration + " IN SCHEMA public GRANT USAGE, SELECT ON SEQUENCES TO " + runtime, + "ALTER DEFAULT PRIVILEGES FOR ROLE " + migration + " IN SCHEMA public GRANT EXECUTE ON FUNCTIONS TO " + runtime, + } +} + +// objectKind is a public-schema object class the migration role must own. +type objectKind string + +const ( + objectTable objectKind = "table" + objectPartitionedTable objectKind = "partitioned table" + objectSequence objectKind = "sequence" + objectView objectKind = "view" + objectMaterializedView objectKind = "materialized view" + objectForeignTable objectKind = "foreign table" + objectFunction objectKind = "function" + objectProcedure objectKind = "procedure" +) + +// schemaObject is one public-schema object whose ownership moves to the +// migration role. +type schemaObject struct { + kind objectKind + // reference is the object's name for a relation, or its regprocedure text + // for a function; both are engine-generated, never operator input. + reference string +} + +// ownerStatement renders the ALTER that hands object to role. Relations are +// quoted; a function reference is already the regprocedure form PostgreSQL +// renders, argument types included. +func (o schemaObject) ownerStatement(role string) string { + if o.kind == objectFunction || o.kind == objectProcedure { + return "ALTER " + o.kind.alterKeyword() + " " + o.reference + " OWNER TO " + quoteIdent(role) + } + return "ALTER " + o.kind.alterKeyword() + " " + pgx.Identifier{"public", o.reference}.Sanitize() + " OWNER TO " + quoteIdent(role) +} + +// alterKeyword is the object class's ALTER target. A partitioned table is +// altered as a table: PostgreSQL has no ALTER PARTITIONED TABLE. +func (k objectKind) alterKeyword() string { + switch k { + case objectTable, objectPartitionedTable: + return "TABLE" + case objectSequence: + return "SEQUENCE" + case objectView: + return "VIEW" + case objectMaterializedView: + return "MATERIALIZED VIEW" + case objectForeignTable: + return "FOREIGN TABLE" + case objectProcedure: + return "PROCEDURE" + default: + return "FUNCTION" + } +} + +// schemaObjects lists the public-schema objects someone other than the +// migration role still owns. Objects an extension owns are left alone, and +// indexes, row types and sequences owned by a table column follow the table's +// owner, so the relation loop does not repeat them. A partition has its own +// dependency on the partitioned table, so it is listed and transferred too. +func schemaObjects(ctx context.Context, tx pgx.Tx, migrationRole string) ([]schemaObject, error) { + rows, err := tx.Query(ctx, ` + SELECT c.relkind, c.relname + FROM pg_class c + JOIN pg_namespace n ON n.oid = c.relnamespace + WHERE n.nspname = 'public' + AND c.relkind IN ('r', 'p', 'S', 'v', 'm', 'f') + AND pg_get_userbyid(c.relowner) <> $1 + AND NOT EXISTS ( + SELECT 1 FROM pg_depend d + WHERE d.classid = 'pg_class'::regclass + AND d.objid = c.oid + AND d.deptype = 'e') + AND (c.relkind <> 'S' OR NOT EXISTS ( + SELECT 1 FROM pg_depend d + WHERE d.classid = 'pg_class'::regclass + AND d.objid = c.oid + AND d.refclassid = 'pg_class'::regclass + AND d.deptype IN ('a', 'i'))) + ORDER BY c.relkind, c.relname`, migrationRole) + if err != nil { + return nil, fmt.Errorf("postgres: provision list schema objects: %w", err) + } + var objects []schemaObject + for rows.Next() { + var relkind, name string + if err := rows.Scan(&relkind, &name); err != nil { + rows.Close() + return nil, fmt.Errorf("postgres: provision list schema objects: %w", err) + } + kind, ok := relationKinds[relkind] + if !ok { + // An object class this provisioner does not know how to hand over; + // leaving it behind is visible in verifyProvision's counts. + continue + } + objects = append(objects, schemaObject{kind: kind, reference: name}) + } + rows.Close() + if err := rows.Err(); err != nil { + return nil, fmt.Errorf("postgres: provision list schema objects: %w", err) + } + + rows, err = tx.Query(ctx, ` + SELECT p.prokind, p.oid::regprocedure::text + FROM pg_proc p + JOIN pg_namespace n ON n.oid = p.pronamespace + WHERE n.nspname = 'public' + AND p.prokind IN ('f', 'p') + AND pg_get_userbyid(p.proowner) <> $1 + AND NOT EXISTS ( + SELECT 1 FROM pg_depend d + WHERE d.classid = 'pg_proc'::regclass + AND d.objid = p.oid + AND d.deptype = 'e') + ORDER BY p.oid::regprocedure::text`, migrationRole) + if err != nil { + return nil, fmt.Errorf("postgres: provision list schema functions: %w", err) + } + defer rows.Close() + for rows.Next() { + var prokind, reference string + if err := rows.Scan(&prokind, &reference); err != nil { + return nil, fmt.Errorf("postgres: provision list schema functions: %w", err) + } + kind := objectFunction + if prokind == "p" { + kind = objectProcedure + } + objects = append(objects, schemaObject{kind: kind, reference: reference}) + } + if err := rows.Err(); err != nil { + return nil, fmt.Errorf("postgres: provision list schema functions: %w", err) + } + return objects, nil +} + +// relationKinds maps pg_class.relkind to the object class an ALTER names. +var relationKinds = map[string]objectKind{ + "r": objectTable, + "p": objectPartitionedTable, + "S": objectSequence, + "v": objectView, + "m": objectMaterializedView, + "f": objectForeignTable, +} + +// verifyProvision refuses a state that would leave the runtime role able to +// change the schema, or either role able to reach superuser powers through a +// role membership. +func verifyProvision(ctx context.Context, tx pgx.Tx, roles provisionRoles) error { + for _, role := range []provisionRole{roles.runtime, roles.migration} { + var superuser bool + if err := tx.QueryRow(ctx, "SELECT rolsuper FROM pg_roles WHERE rolname = $1", role.name).Scan(&superuser); err != nil { + return fmt.Errorf("postgres: provision verify role %s: %w", role.name, err) + } + if superuser { + return fmt.Errorf("postgres: provision left role %q a superuser", role.name) + } + if err := verifyNoSuperuserMembership(ctx, tx, role.name); err != nil { + return err + } + } + var canCreate bool + if err := tx.QueryRow(ctx, "SELECT has_schema_privilege($1, 'public', 'CREATE')", roles.runtime.name).Scan(&canCreate); err != nil { + return fmt.Errorf("postgres: provision verify runtime schema privileges: %w", err) + } + if canCreate { + return fmt.Errorf("postgres: provision left %q able to create schema objects; migrations would not be the migration role's alone", roles.runtime.name) + } + var migrationCanCreate bool + if err := tx.QueryRow(ctx, "SELECT has_schema_privilege($1, 'public', 'CREATE')", roles.migration.name).Scan(&migrationCanCreate); err != nil { + return fmt.Errorf("postgres: provision verify migration schema privileges: %w", err) + } + if !migrationCanCreate { + return fmt.Errorf("postgres: provision left %q unable to create schema objects", roles.migration.name) + } + return nil +} + +// verifyNoSuperuserMembership refuses a role that inherits a superuser's +// powers through role membership, which rolsuper alone does not show. +func verifyNoSuperuserMembership(ctx context.Context, tx pgx.Tx, role string) error { + rows, err := tx.Query(ctx, ` + SELECT parent.rolname + FROM pg_auth_members m + JOIN pg_roles parent ON parent.oid = m.roleid + JOIN pg_roles member ON member.oid = m.member + WHERE member.rolname = $1 AND parent.rolsuper`, role) + if err != nil { + return fmt.Errorf("postgres: provision verify role memberships: %w", err) + } + defer rows.Close() + if rows.Next() { + var parent string + if err := rows.Scan(&parent); err != nil { + return fmt.Errorf("postgres: provision verify role memberships: %w", err) + } + return fmt.Errorf("postgres: provision refuses role %q: it is a member of superuser role %q", role, parent) + } + return rows.Err() +} + +// parseProvisionURLs reads the three roles out of the URLs the installer +// passes, taking a password from the URL's userinfo or, for a passwordless +// URL, from PGPASSWORD. It refuses a plan that would leave the runtime role +// with powers the services never need. +func parseProvisionURLs(opts ProvisionOptions, getenv func(string) string) (provisionRoles, error) { + admin, err := roleFromURL(opts.AdminURL, "admin_url", getenv, false) + if err != nil { + return provisionRoles{}, err + } + runtime, err := roleFromURL(opts.RuntimeURL, "runtime_url", getenv, true) + if err != nil { + return provisionRoles{}, err + } + migration, err := roleFromURL(opts.MigrationURL, "migration_url", getenv, true) + if err != nil { + return provisionRoles{}, err + } + if runtime.name == migration.name { + return provisionRoles{}, fmt.Errorf("postgres: provision: runtime_url and migration_url name the same role %q; they must differ", runtime.name) + } + for _, role := range []provisionRole{runtime, migration} { + if role.name == admin.name { + return provisionRoles{}, fmt.Errorf("postgres: provision: %q is the admin role; runtime_url and migration_url must name the roles being provisioned", role.name) + } + } + return provisionRoles{admin: admin, runtime: runtime, migration: migration}, nil +} + +// roleFromURL extracts one login role from a PostgreSQL URL. requirePassword +// is set for the two provisioned roles, whose password must be known so the +// run can set it; the admin role may authenticate through the environment. +func roleFromURL(raw, label string, getenv func(string) string, requirePassword bool) (provisionRole, error) { + if strings.TrimSpace(raw) == "" { + return provisionRole{}, fmt.Errorf("postgres: provision: %s is required", label) + } + parsed, err := url.Parse(raw) + if err != nil || (parsed.Scheme != "postgres" && parsed.Scheme != "postgresql") { + return provisionRole{}, fmt.Errorf("postgres: provision: %s must be a postgres:// URL naming a login role", label) + } + if parsed.User == nil || parsed.User.Username() == "" { + return provisionRole{}, fmt.Errorf("postgres: provision: %s must name the role in its URL userinfo", label) + } + role := provisionRole{name: parsed.User.Username()} + if password, ok := parsed.User.Password(); ok && password != "" { + role.password = password + } else if password := getenv(databasePasswordEnv); password != "" { + role.password = password + } + if requirePassword && role.password == "" { + return provisionRole{}, fmt.Errorf("postgres: provision: %s carries no password and %s is unset", label, databasePasswordEnv) + } + return role, nil +} + +// databasePasswordEnv is the variable pgx and the PostgreSQL client tools read +// a connection password from when a URL does not carry one. +const databasePasswordEnv = "PGPASSWORD" + +// scramVerifier returns the SCRAM-SHA-256 verifier PostgreSQL stores for +// password. ALTER ROLE accepts a pre-computed verifier, so the plaintext never +// travels in a statement another session's statement log can capture. +func scramVerifier(password string) (string, error) { + if password == "" { + return "", errors.New("postgres: provision: a role password is required") + } + const iterations = 4096 + salt := make([]byte, 16) + if _, err := rand.Read(salt); err != nil { + return "", fmt.Errorf("postgres: provision: salt: %w", err) + } + salted := pbkdf2.Key([]byte(password), salt, iterations, sha256.Size, sha256.New) + storedKey := sha256.Sum256(hmacSHA256(salted, []byte("Client Key"))) + serverKey := hmacSHA256(salted, []byte("Server Key")) + return fmt.Sprintf("SCRAM-SHA-256$%d:%s$%s:%s", + iterations, + base64.StdEncoding.EncodeToString(salt), + base64.StdEncoding.EncodeToString(storedKey[:]), + base64.StdEncoding.EncodeToString(serverKey), + ), nil +} + +func hmacSHA256(key, message []byte) []byte { + mac := hmac.New(sha256.New, key) + mac.Write(message) + return mac.Sum(nil) +} + +// quoteIdent quotes an SQL identifier such as a role or table name. +func quoteIdent(name string) string { return pgx.Identifier{name}.Sanitize() } + +// quoteLiteral quotes an SQL string literal. scramVerifier's output is +// base64 and separators only, so it can never contain the quote being escaped; +// the escaping is here so a future caller cannot inject through the value. +func quoteLiteral(value string) string { + return "'" + strings.ReplaceAll(value, "'", "''") + "'" +} diff --git a/internal/infrastructure/postgres/provision_test.go b/internal/infrastructure/postgres/provision_test.go new file mode 100644 index 000000000..1c1e1fa8c --- /dev/null +++ b/internal/infrastructure/postgres/provision_test.go @@ -0,0 +1,264 @@ +package postgres + +import ( + "encoding/base64" + "regexp" + "strings" + "testing" +) + +// testProvisionRoles is the role set the statement tests reconcile. +func testProvisionRoles() provisionRoles { + return provisionRoles{ + admin: provisionRole{name: "archie", password: "admin-password"}, + runtime: provisionRole{name: RuntimeRole, password: "runtime-password"}, + migration: provisionRole{name: MigrationRole, password: "migration-password"}, + } +} + +// TestParseProvisionURLs pins what the installer may hand the provisioner: a +// role per URL, a password from the URL or PGPASSWORD, and a refusal for any +// plan that would leave the runtime role owning schema changes. +func TestParseProvisionURLs(t *testing.T) { + const ( + adminURL = "postgres://archie:admin@db.internal/archie" + runtimeURL = "postgres://archie_runtime:runtime@db.internal/archie" + migrationURL = "postgres://archie_migrator:migration@db.internal/archie" + ) + tests := []struct { + name string + opts ProvisionOptions + env map[string]string + want provisionRoles + wantErr string + }{ + { + name: "passwords come from the URL userinfo", + opts: ProvisionOptions{AdminURL: adminURL, RuntimeURL: runtimeURL, MigrationURL: migrationURL}, + want: provisionRoles{ + admin: provisionRole{name: "archie", password: "admin"}, + runtime: provisionRole{name: RuntimeRole, password: "runtime"}, + migration: provisionRole{name: MigrationRole, password: "migration"}, + }, + }, + { + name: "passwordless role URLs take PGPASSWORD", + opts: ProvisionOptions{ + AdminURL: "postgres://archie@/archie?host=/run/archie/postgres", + RuntimeURL: "postgres://archie_runtime@/archie?host=/run/archie/postgres", + MigrationURL: "postgres://archie_migrator@/archie?host=/run/archie/postgres", + }, + env: map[string]string{databasePasswordEnv: "from-env"}, + want: provisionRoles{ + admin: provisionRole{name: "archie", password: "from-env"}, + runtime: provisionRole{name: RuntimeRole, password: "from-env"}, + migration: provisionRole{name: MigrationRole, password: "from-env"}, + }, + }, + { + name: "an admin URL needs no password of its own", + opts: ProvisionOptions{ + AdminURL: "postgres://archie@/archie?host=/run/archie/postgres", + RuntimeURL: runtimeURL, + MigrationURL: migrationURL, + }, + want: provisionRoles{ + admin: provisionRole{name: "archie"}, + runtime: provisionRole{name: RuntimeRole, password: "runtime"}, + migration: provisionRole{name: MigrationRole, password: "migration"}, + }, + }, + { + name: "a runtime role with no password is refused", + opts: ProvisionOptions{AdminURL: adminURL, RuntimeURL: "postgres://archie_runtime@db.internal/archie", MigrationURL: migrationURL}, + wantErr: databasePasswordEnv, + }, + { + name: "the same role cannot be runtime and migration", + opts: ProvisionOptions{AdminURL: adminURL, RuntimeURL: runtimeURL, MigrationURL: "postgres://archie_runtime:other@db.internal/archie"}, + wantErr: "same role", + }, + { + name: "the admin role cannot be provisioned as runtime", + opts: ProvisionOptions{AdminURL: adminURL, RuntimeURL: adminURL, MigrationURL: migrationURL}, + wantErr: "admin role", + }, + { + name: "a non-PostgreSQL URL is refused", + opts: ProvisionOptions{AdminURL: "mysql://archie:admin@db.internal/archie", RuntimeURL: runtimeURL, MigrationURL: migrationURL}, + wantErr: "postgres:// URL", + }, + { + name: "a URL without a role is refused", + opts: ProvisionOptions{AdminURL: adminURL, RuntimeURL: "postgres://db.internal/archie", MigrationURL: migrationURL}, + wantErr: "userinfo", + }, + { + name: "a key=value connection string is refused, it names no role", + opts: ProvisionOptions{AdminURL: adminURL, RuntimeURL: "host=db.internal user=archie_runtime", MigrationURL: migrationURL}, + wantErr: "postgres:// URL", + }, + { + name: "a missing role URL is named in the error", + opts: ProvisionOptions{AdminURL: adminURL, RuntimeURL: runtimeURL}, + wantErr: "migration_url is required", + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + getenv := func(key string) string { return tt.env[key] } + got, err := parseProvisionURLs(tt.opts, getenv) + if tt.wantErr != "" { + if err == nil || !strings.Contains(err.Error(), tt.wantErr) { + t.Fatalf("parseProvisionURLs error = %v, want one naming %q", err, tt.wantErr) + } + return + } + if err != nil { + t.Fatalf("parseProvisionURLs: %v", err) + } + if got != tt.want { + t.Errorf("parseProvisionURLs = %+v, want %+v", got, tt.want) + } + }) + } +} + +// TestSchemaStatements pins the privilege boundary: the runtime role reads and +// writes rows and nothing else, the migration role owns the schema, and the +// migration role's default privileges cover the objects future migrations +// create. A regression here is a security regression, not a formatting one. +func TestSchemaStatements(t *testing.T) { + roles := testProvisionRoles() + runtime := quoteIdent(roles.runtime.name) + migration := quoteIdent(roles.migration.name) + statements := schemaStatements(roles) + joined := strings.Join(statements, "\n") + + required := []string{ + "ALTER SCHEMA public OWNER TO " + migration, + "REVOKE CREATE ON SCHEMA public FROM PUBLIC", + "GRANT USAGE ON SCHEMA public TO " + runtime, + "GRANT USAGE, CREATE ON SCHEMA public TO " + migration, + "GRANT SELECT, INSERT, UPDATE, DELETE ON ALL TABLES IN SCHEMA public TO " + runtime, + "GRANT USAGE, SELECT ON ALL SEQUENCES IN SCHEMA public TO " + runtime, + "GRANT EXECUTE ON ALL FUNCTIONS IN SCHEMA public TO " + runtime, + "ALTER DEFAULT PRIVILEGES FOR ROLE " + migration + " IN SCHEMA public GRANT SELECT, INSERT, UPDATE, DELETE ON TABLES TO " + runtime, + "ALTER DEFAULT PRIVILEGES FOR ROLE " + migration + " IN SCHEMA public GRANT USAGE, SELECT ON SEQUENCES TO " + runtime, + "ALTER DEFAULT PRIVILEGES FOR ROLE " + migration + " IN SCHEMA public GRANT EXECUTE ON FUNCTIONS TO " + runtime, + } + for _, statement := range required { + if !strings.Contains(joined, statement) { + t.Errorf("provisioning lacks %q", statement) + } + } + + // Every statement that mentions the runtime role must be a row-level grant + // or a default privilege: nothing may let it create, own, truncate or + // elevate. + forbiddenForRuntime := []string{" CREATE ", " SUPERUSER", " CREATEDB", " CREATEROLE", " OWNER TO ", "TRUNCATE", "ALL PRIVILEGES"} + for _, statement := range statements { + if !strings.Contains(statement, runtime) { + continue + } + for _, forbidden := range forbiddenForRuntime { + if strings.Contains(statement, forbidden) { + t.Errorf("statement grants the runtime role more than rows: %q", statement) + } + } + } + + // The default privileges are what make the next migration's tables usable + // without a hand-run grant; one per object class is the contract. + var defaults int + for _, statement := range statements { + if strings.HasPrefix(statement, "ALTER DEFAULT PRIVILEGES FOR ROLE "+migration) { + defaults++ + } + } + if defaults != 3 { + t.Errorf("default privileges for %s = %d statements, want tables, sequences and functions", roles.migration.name, defaults) + } +} + +// TestSchemaStatementsQuoteRoleNames pins that role names arriving in URLs are +// quoted as identifiers, not spliced into SQL. +func TestSchemaStatementsQuoteRoleNames(t *testing.T) { + roles := provisionRoles{ + runtime: provisionRole{name: `runtime"; DROP TABLE tasks; --`}, + migration: provisionRole{name: `migration"role`}, + } + joined := strings.Join(schemaStatements(roles), "\n") + if !strings.Contains(joined, `"runtime""; DROP TABLE tasks; --"`) { + t.Errorf("runtime role name is not quoted as one identifier:\n%s", joined) + } + if !strings.Contains(joined, `"migration""role"`) { + t.Errorf("migration role name is not quoted as one identifier:\n%s", joined) + } +} + +// TestOwnerStatement pins the ALTER an object gets when its ownership moves to +// the migration role, including PostgreSQL's keyword for each object class. +func TestOwnerStatement(t *testing.T) { + const role = MigrationRole + tests := []struct { + name string + object schemaObject + want string + }{ + {"table", schemaObject{kind: objectTable, reference: "tasks"}, `ALTER TABLE "public"."tasks" OWNER TO "archie_migrator"`}, + {"partitioned table alters as a table", schemaObject{kind: objectPartitionedTable, reference: "events"}, `ALTER TABLE "public"."events" OWNER TO "archie_migrator"`}, + {"sequence", schemaObject{kind: objectSequence, reference: "tasks_id_seq"}, `ALTER SEQUENCE "public"."tasks_id_seq" OWNER TO "archie_migrator"`}, + {"view", schemaObject{kind: objectView, reference: "open_tasks"}, `ALTER VIEW "public"."open_tasks" OWNER TO "archie_migrator"`}, + {"materialized view", schemaObject{kind: objectMaterializedView, reference: "summary"}, `ALTER MATERIALIZED VIEW "public"."summary" OWNER TO "archie_migrator"`}, + {"foreign table", schemaObject{kind: objectForeignTable, reference: "remote"}, `ALTER FOREIGN TABLE "public"."remote" OWNER TO "archie_migrator"`}, + {"function", schemaObject{kind: objectFunction, reference: "audit_json_diff(jsonb,jsonb)"}, `ALTER FUNCTION audit_json_diff(jsonb,jsonb) OWNER TO "archie_migrator"`}, + {"procedure", schemaObject{kind: objectProcedure, reference: "sweep(integer)"}, `ALTER PROCEDURE sweep(integer) OWNER TO "archie_migrator"`}, + {"a quoted relation name stays one identifier", schemaObject{kind: objectTable, reference: `bad"name`}, `ALTER TABLE "public"."bad""name" OWNER TO "archie_migrator"`}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := tt.object.ownerStatement(role); got != tt.want { + t.Errorf("ownerStatement = %q, want %q", got, tt.want) + } + }) + } +} + +// TestSCRAMVerifier pins that the provisioner stores PostgreSQL a SCRAM +// verifier rather than a plaintext password, and that the verifier has the +// shape the server accepts. +func TestSCRAMVerifier(t *testing.T) { + verifier, err := scramVerifier("correct horse battery staple") + if err != nil { + t.Fatalf("scramVerifier: %v", err) + } + if strings.Contains(verifier, "correct horse") { + t.Fatalf("verifier contains the plaintext password: %q", verifier) + } + shape := regexp.MustCompile(`^SCRAM-SHA-256\$4096:([A-Za-z0-9+/=]+)\$([A-Za-z0-9+/=]+):([A-Za-z0-9+/=]+)$`) + match := shape.FindStringSubmatch(verifier) + if match == nil { + t.Fatalf("verifier %q is not SCRAM-SHA-256$iterations:salt$stored:server", verifier) + } + for i, want := range map[int]int{1: 16, 2: 32, 3: 32} { + decoded, err := base64.StdEncoding.DecodeString(match[i]) + if err != nil { + t.Fatalf("part %d of the verifier does not decode: %v", i, err) + } + if len(decoded) != want { + t.Errorf("part %d decodes to %d bytes, want %d", i, len(decoded), want) + } + } + again, err := scramVerifier("correct horse battery staple") + if err != nil { + t.Fatalf("scramVerifier: %v", err) + } + if again == verifier { + t.Error("two verifiers of the same password are equal; the salt is not random") + } + + if _, err := scramVerifier(""); err == nil { + t.Error("scramVerifier accepted an empty password") + } +} -- 2.54.0 From f92c5b45367582745a3d06026dc4f017f29974a0 Mon Sep 17 00:00:00 2001 From: Sam Catlow Date: Thu, 12 Nov 2026 21:29:05 +1100 Subject: [PATCH 5/7] chore(runtime): delete Kit and external agent harness support (#1877) Archie runs its own agent loop only. Removes Kit resolution and launch, the harness runner and adapters, the setup terminal and sandbox view, harness secrets and OAuth capture (table dropped in migration 0075), the task-tools contract that only Kit containers reached, and the sandbox-kit-spec dependency. Agent profiles carry image and tools only. The egress proxy keeps the model-session path; the sandbox relay code stays for #1867. --- cmd/archie-agent/main.go | 46 -- deployments/INSTRUCTIONS.md | 3 +- deployments/systemd-user-service.md | 2 +- docs/prds/external-agent-harness.md | 414 ------------- docs/security/authorization.md | 6 +- go.mod | 6 +- go.sum | 43 +- install.sh | 5 +- internal/agentexec/capture_mcp.go | 153 ----- internal/agentexec/harness_adapters.go | 59 -- internal/agentexec/harness_mcp.go | 127 ---- internal/agentexec/harness_mcp_test.go | 46 -- internal/agentexec/harness_output_claude.go | 171 ------ internal/agentexec/harness_output_codex.go | 107 ---- internal/agentexec/harness_output_copilot.go | 60 -- internal/agentexec/harness_output_pi.go | 66 -- internal/agentexec/harness_proc_other.go | 30 - internal/agentexec/harness_proc_unix.go | 126 ---- internal/agentexec/harness_runner.go | 550 ----------------- internal/agentexec/harness_runner_test.go | 41 -- internal/agentexec/helpers.go | 14 + internal/app/agentworker/mcp.go | 15 - internal/app/agentworker/task_execution.go | 6 +- internal/app/archied/bootstrap.go | 17 +- internal/app/archied/daemon_rpc.go | 59 ++ internal/app/archied/harness.go | 254 -------- internal/app/archied/harness_run.go | 97 --- internal/app/archied/harness_run_test.go | 95 --- internal/app/archied/kit_launcher.go | 135 ---- internal/app/archied/model_egress.go | 15 +- internal/app/archied/model_egress_setup.go | 46 ++ .../app/archied/run_credential_resolver.go | 31 +- .../archied/run_credential_resolver_test.go | 44 -- internal/app/archiegateway/gateway.go | 8 +- internal/app/archiegateway/task_tools.go | 346 ----------- internal/app/archiegateway/task_tools_test.go | 281 --------- internal/app/archiegateway/tools.go | 3 +- internal/app/archieui/compose.go | 5 - internal/app/archieui/run.go | 14 - internal/app/statestore/eda.go | 1 - internal/app/statestore/server.go | 4 - internal/config/config.go | 46 +- internal/config/servicespec.go | 1 - internal/container/kit.go | 274 --------- internal/container/kit_test.go | 82 --- internal/container/logs.go | 50 -- internal/container/logs_test.go | 54 -- internal/container/pool.go | 24 - internal/container/pty.go | 61 -- .../contracts/gateway/v1/task_tools.pb.go | 382 ------------ .../gateway/v1/task_tools_grpc.pb.go | 203 ------ internal/contracts/harness/v1/harness.pb.go | 576 ------------------ .../contracts/harness/v1/harness_grpc.pb.go | 131 ---- .../contracts/state/v1/harness_secret.pb.go | 402 ------------ .../state/v1/harness_secret_grpc.pb.go | 169 ----- internal/daemon/agent_status_test.go | 2 +- internal/daemon/daemon.go | 31 +- internal/daemon/kit_task.go | 214 ------- internal/daemon/model_egress.go | 83 +++ internal/daemon/model_egress_test.go | 2 +- internal/domain/agentrun/protocol.go | 30 - internal/domain/harnesssecret/context.go | 16 - .../domain/harnesssecret/harnesssecret.go | 40 -- internal/domain/storecontract/credentials.go | 9 - internal/domain/storecontract/errors.go | 4 - internal/domain/workflow/test_run.go | 7 +- internal/gateway/dashboard_tools.go | 2 +- .../infrastructure/bindingcipher/cipher.go | 13 +- .../controlplanestore/agent_profiles.go | 4 +- .../controlplanestore/container_settings.go | 8 +- .../controlplanestore/credential_bindings.go | 4 +- .../controlplanestore/profiles_projector.go | 10 +- internal/infrastructure/egress/credentials.go | 119 +--- internal/infrastructure/egress/oauth.go | 316 ---------- internal/infrastructure/egress/policy.go | 20 +- internal/infrastructure/egress/proxy.go | 267 +------- .../egress/proxy_record_test.go | 140 ----- .../{kitrun/env.go => egress/sandbox_env.go} | 18 +- internal/infrastructure/egress/substitute.go | 2 +- .../infrastructure/egress/task_tools_test.go | 83 --- .../infrastructure/harnessrpc/harnessrpc.go | 162 ----- .../harnesssession/harnesssession.go | 160 ----- .../harnesssession/harnesssession_test.go | 54 -- .../infrastructure/kit/credential_file.go | 199 ------ internal/infrastructure/kit/kit.go | 129 ---- internal/infrastructure/kit/kit_test.go | 227 ------- internal/infrastructure/kit/launch.go | 315 ---------- internal/infrastructure/kitrun/launcher.go | 527 ---------------- .../infrastructure/kitrun/launcher_test.go | 131 ---- internal/infrastructure/postgres/edastore.go | 1 - .../postgres/harness_secrets.go | 129 ---- .../migrations/0075_drop_harness_secrets.sql | 11 + .../postgresdb/harness_secrets.sql.go | 59 -- .../postgres/postgresdb/models.go | 7 - .../postgres/queries/harness_secrets.sql | 16 - internal/infrastructure/staterpc/client.go | 97 ++- .../staterpc/harness_secret_test.go | 175 ------ .../staterpc/harness_secrets.go | 97 --- internal/infrastructure/staterpc/server.go | 5 - internal/infrastructure/staterpc/values.go | 53 +- internal/infrastructure/tasktools/client.go | 62 -- internal/infrastructure/tasktools/http.go | 47 -- internal/releaseupdate/topology.go | 3 +- internal/skill/skill.go | 3 +- internal/taskrun/taskrun.go | 3 - internal/webui/api_setup.go | 23 +- internal/webui/api_setup_test.go | 31 +- internal/webui/authorization_surfaces_test.go | 3 - internal/webui/authorize.go | 13 +- internal/webui/harness.go | 259 -------- internal/webui/server.go | 19 - internal/webui/tasks.go | 5 +- internal/webui/tasks_sandbox.go | 71 --- internal/webui/tasks_sandbox_test.go | 87 --- proto/buf.yaml | 9 + proto/gateway/v1/task_tools.proto | 20 - proto/harness/v1/harness.proto | 72 --- proto/state/v1/harness_secret.proto | 44 -- tools/canary-rehearse.sh | 2 +- ui/package.json | 2 - ui/pnpm-lock.yaml | 16 - ui/src/harness/SetupTerminal.vue | 127 ---- ui/src/harness/harness.ts | 74 --- ui/src/lib/api.ts | 1 - ui/src/lib/terminalTheme.ts | 45 -- ui/src/router/index.ts | 3 - ui/src/settings/AgentProfilesPage.vue | 174 +----- ui/src/style.css | 14 - ui/src/tasks/SandboxView.vue | 115 ---- ui/src/tasks/TabBar.vue | 10 +- ui/src/tasks/TaskRunPanels.vue | 7 - ui/src/tasks/task-run.ts | 1 - ui/test/harness.test.ts | 59 -- 133 files changed, 406 insertions(+), 11022 deletions(-) delete mode 100644 docs/prds/external-agent-harness.md delete mode 100644 internal/agentexec/capture_mcp.go delete mode 100644 internal/agentexec/harness_adapters.go delete mode 100644 internal/agentexec/harness_mcp.go delete mode 100644 internal/agentexec/harness_mcp_test.go delete mode 100644 internal/agentexec/harness_output_claude.go delete mode 100644 internal/agentexec/harness_output_codex.go delete mode 100644 internal/agentexec/harness_output_copilot.go delete mode 100644 internal/agentexec/harness_output_pi.go delete mode 100644 internal/agentexec/harness_proc_other.go delete mode 100644 internal/agentexec/harness_proc_unix.go delete mode 100644 internal/agentexec/harness_runner.go delete mode 100644 internal/agentexec/harness_runner_test.go delete mode 100644 internal/app/agentworker/mcp.go create mode 100644 internal/app/archied/daemon_rpc.go delete mode 100644 internal/app/archied/harness.go delete mode 100644 internal/app/archied/harness_run.go delete mode 100644 internal/app/archied/harness_run_test.go delete mode 100644 internal/app/archied/kit_launcher.go create mode 100644 internal/app/archied/model_egress_setup.go delete mode 100644 internal/app/archiegateway/task_tools.go delete mode 100644 internal/app/archiegateway/task_tools_test.go delete mode 100644 internal/container/kit.go delete mode 100644 internal/container/kit_test.go delete mode 100644 internal/container/logs.go delete mode 100644 internal/container/logs_test.go delete mode 100644 internal/container/pty.go delete mode 100644 internal/contracts/gateway/v1/task_tools.pb.go delete mode 100644 internal/contracts/gateway/v1/task_tools_grpc.pb.go delete mode 100644 internal/contracts/harness/v1/harness.pb.go delete mode 100644 internal/contracts/harness/v1/harness_grpc.pb.go delete mode 100644 internal/contracts/state/v1/harness_secret.pb.go delete mode 100644 internal/contracts/state/v1/harness_secret_grpc.pb.go delete mode 100644 internal/daemon/kit_task.go create mode 100644 internal/daemon/model_egress.go delete mode 100644 internal/domain/harnesssecret/context.go delete mode 100644 internal/domain/harnesssecret/harnesssecret.go delete mode 100644 internal/infrastructure/egress/oauth.go delete mode 100644 internal/infrastructure/egress/proxy_record_test.go rename internal/infrastructure/{kitrun/env.go => egress/sandbox_env.go} (76%) delete mode 100644 internal/infrastructure/egress/task_tools_test.go delete mode 100644 internal/infrastructure/harnessrpc/harnessrpc.go delete mode 100644 internal/infrastructure/harnesssession/harnesssession.go delete mode 100644 internal/infrastructure/harnesssession/harnesssession_test.go delete mode 100644 internal/infrastructure/kit/credential_file.go delete mode 100644 internal/infrastructure/kit/kit.go delete mode 100644 internal/infrastructure/kit/kit_test.go delete mode 100644 internal/infrastructure/kit/launch.go delete mode 100644 internal/infrastructure/kitrun/launcher.go delete mode 100644 internal/infrastructure/kitrun/launcher_test.go delete mode 100644 internal/infrastructure/postgres/harness_secrets.go create mode 100644 internal/infrastructure/postgres/migrations/0075_drop_harness_secrets.sql delete mode 100644 internal/infrastructure/postgres/postgresdb/harness_secrets.sql.go delete mode 100644 internal/infrastructure/postgres/queries/harness_secrets.sql delete mode 100644 internal/infrastructure/staterpc/harness_secret_test.go delete mode 100644 internal/infrastructure/staterpc/harness_secrets.go delete mode 100644 internal/infrastructure/tasktools/client.go delete mode 100644 internal/infrastructure/tasktools/http.go delete mode 100644 internal/webui/harness.go delete mode 100644 internal/webui/tasks_sandbox.go delete mode 100644 internal/webui/tasks_sandbox_test.go delete mode 100644 proto/gateway/v1/task_tools.proto delete mode 100644 proto/harness/v1/harness.proto delete mode 100644 proto/state/v1/harness_secret.proto delete mode 100644 ui/src/harness/SetupTerminal.vue delete mode 100644 ui/src/harness/harness.ts delete mode 100644 ui/src/lib/terminalTheme.ts delete mode 100644 ui/src/tasks/SandboxView.vue delete mode 100644 ui/test/harness.test.ts diff --git a/cmd/archie-agent/main.go b/cmd/archie-agent/main.go index ccf7200ed..f215e01ff 100644 --- a/cmd/archie-agent/main.go +++ b/cmd/archie-agent/main.go @@ -21,17 +21,6 @@ import ( type workerRunner func(context.Context, agentworker.Settings, *slog.Logger) error -// runHold blocks until signalled and exits 0. It is the keepalive for a Kit -// container that runs no worker: the agent binary is archie's own and is -// mounted into every Kit, so the container needs no binary the image may not -// ship, and holds no environment of its own. -func runHold() int { - ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM) - defer stop() - <-ctx.Done() - return 0 -} - func main() { os.Exit(run()) } @@ -41,15 +30,9 @@ func run() int { } func runCommand(args []string, getenv func(string) string, stderr io.Writer, runWorker workerRunner) int { - if len(args) > 0 && args[0] == "hold" { - return runHold() - } if len(args) > 0 && args[0] == "relay" { return runRelay(args[1:], stderr, agentworker.RunRelay) } - if len(args) > 0 && args[0] == "mcp" { - return runMCP(args[1:], stderr, agentworker.RunCaptureMCP) - } flags := flag.NewFlagSet("archie-agent", flag.ContinueOnError) flags.SetOutput(stderr) natsURLFlag := flags.String("nats-url", "", "NATS server URL (defaults to NATS_URL)") @@ -162,32 +145,3 @@ func runRelay(args []string, stderr io.Writer, serve relayServer) int { } return 0 } - -type mcpServer func(ctx context.Context, specPath, capturesPath string) error - -// runMCP serves a stage's capture tools to a harness over stdio. The -// harness starts it, so it runs as the harness user and holds no secrets. -func runMCP(args []string, stderr io.Writer, serve mcpServer) int { - flags := flag.NewFlagSet("archie-agent mcp", flag.ContinueOnError) - flags.SetOutput(stderr) - spec := flags.String("spec", "", "JSON file listing the stage's capture tools") - captures := flags.String("captures", "", "file accepted capture calls are appended to") - if err := flags.Parse(args); err != nil { - if errors.Is(err, flag.ErrHelp) { - return 0 - } - return 2 - } - if *spec == "" || *captures == "" { - fmt.Fprintln(stderr, "error: -spec and -captures are required") - flags.Usage() - return 1 - } - ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM) - defer stop() - if err := serve(ctx, *spec, *captures); err != nil { - fmt.Fprintln(stderr, "error:", err) - return 1 - } - return 0 -} diff --git a/deployments/INSTRUCTIONS.md b/deployments/INSTRUCTIONS.md index 9c45883d8..4f85c816c 100644 --- a/deployments/INSTRUCTIONS.md +++ b/deployments/INSTRUCTIONS.md @@ -14,8 +14,7 @@ contracts (or NATS, for task actions): Without `archie-messaging` the channels are simply absent, so it needs a unit of its own (`deployments/systemd-user-service.md`). -`archie-agent` (the per-task sandboxed runtime) has no unit of its own: it -ships beside `archied` because a Kit run mounts it from that directory. The +`archie-agent` (the per-task sandboxed runtime) has no unit of its own. The agent image is a separate thing, pulled for the containers `archied` spawns per task: `[containers].image` defaults to `git.catlow.cloud/sam/archie-agent:latest`, while this archive's `release.json` diff --git a/deployments/systemd-user-service.md b/deployments/systemd-user-service.md index 043a645a2..5f9169d2c 100644 --- a/deployments/systemd-user-service.md +++ b/deployments/systemd-user-service.md @@ -54,7 +54,7 @@ Open the dashboard to configure the organisation, workspace and first administra Complete setup before exposing it publicly; remote access requires an HTTPS reverse proxy. **Auth note:** `archie-ui` authenticates to the Gateway, State Store and -harness with its `[tls]` certificate (`archie-ui.pem`) from `config.toml`. +daemon with its `[tls]` certificate (`archie-ui.pem`) from `config.toml`. For supervision, `GET /healthz` on `[web].listen` is the process's liveness probe (token-free by design); the authenticated readiness surface lives at diff --git a/docs/prds/external-agent-harness.md b/docs/prds/external-agent-harness.md deleted file mode 100644 index 000951322..000000000 --- a/docs/prds/external-agent-harness.md +++ /dev/null @@ -1,414 +0,0 @@ -# External agent harnesses - -**Status:** Approved **Scope:** the harness model and, in "Session contract", -the duplex session that the setup terminal and the run-scoped live view both -use. Code cites this file for both (`ui/src/harness/SetupTerminal.vue`, -`internal/infrastructure/postgres/queries/harness_secrets.sql`). - -## Decision - -A workflow stage can run on an external coding-agent CLI instead of the built-in -agent loop: Claude Code, Codex, Pi, OMP, GitHub Copilot CLI, or any other CLI -packaged as a Docker Sandbox Kit v3. Each authenticates with an API key or the -operator's subscription login, and can carry the operator's own skills, plugins, -subagents and instructions. - -Two boundaries hold regardless of what the harness does: - -- **The real credential never enters the container.** The container sees - sentinel values; archie's egress proxy substitutes the real credential on - outbound requests and performs OAuth refresh on the host. -- **Archie's workflow guarantees are enforced outside the harness.** Git, - protected paths, read-only stages, gates and budgets are checked by archie - around the process and against the worktree afterwards. - -## Kits - -A harness is packaged as a Kit v3 image: an OCI image whose -`vnd.docker.sandbox.kit.descriptor` manifest annotation declares what the -harness needs. Archie reads descriptors with the published -`github.com/docker/sandbox-kit-spec/v3/spec` package and does not maintain its -own descriptor format. Published Kits run unmodified. - -Archie is a partial Kit runtime. It implements these capability types: - -| Type | Archie behaviour | -| ------------------ | ------------------------------------------------------------------------------------------------------------------------- | -| `credential@1` | Proxy-managed only, described below. | -| `network-policy@1` | Enforced by the egress proxy. | -| `volume@1` | A Docker volume keyed by WorkflowExecution, deleted with it. | -| `lifecycle@1` | Install and startup hooks run on container create. | -| `agent-sessions@1` | The headless `prompt` and `resume` invocations for the stage. | -| `agent-skills@1` | Discovery paths for bundled skills and optional shared skills; host access is always read-only. | -| `agent-skill@1` | Selected image-bundled skills copied into the declared discovery paths. | -| `agent-context@1` | The resolved discovery directory, defaulting beside the workspace; preserve instructions outside Archie-managed sections. | -| `resources@1` | Container CPU and memory limits. | - -Every other type fails closed: a required entry refuses the launch, an optional -one is skipped and recorded on the step. `credential@1` with `passthrough: true` -and any credential for a forge service are refused. - -The operator's own setup (plugins, subagents, instructions, settings) is a mixin -Kit, published with the harness Kit as a Kit set: the set merges their layers -into one image. It is versioned image content, rewritten on every create, never -state a run can modify. - -## Contract - -A harness run is a second implementation of `agentexec.Runner`. It takes the -same `Request` and returns the same `Result`, so workflows, the daemon and the -State Store do not know which runner ran a stage. - -`agent-sessions@1` supplies the invocation. Archie supplies, per CLI, an output -adapter that translates the CLI's JSON output stream into `ToolCallReporter` -calls, `Usage` and a session ID. A CLI with no adapter can run stages with no -capture tools, reporting no usage; its token budget falls back to wall clock. - -Workflow definition validation rejects, at load time, a stage whose needs the -selected harness cannot meet: capture tools without an adapter, or a gate retry -without a `resume` verb. - -## Selection - -A harness is an agent profile. `docs/prds/event-automation.md` defines a profile -as the execution environment a workflow selects; a harness profile names one Kit -pinned by digest, a workload Kit or a published Kit set, in place of a container -image. Archie runs that Kit's image and composes no Kits itself; a bare mixin is -refused. Everything else about profiles holds: a workflow names its profile, a -stage may name a different one, and a profile holds no secrets. - -`archie-agent` builds the stage's runner from the stage's profile: the ai-sdk -agent loop for an image profile, the harness runner for a Kit profile. Image -profiles, and every workflow already written against them, are unchanged. A Kit -profile named by no workflow is a validation error. - -A stage naming a different profile than its workflow's is a separate run, not a -mid-container runner switch: a Kit task runs in one container provisioned for -one profile (Container layout), so a stage that needs a different one is its own -callee, started the way `workflow.call` already starts one -(`docs/prds/event-automation.md` "Workflows call workflows"), with its own agent -profile resolved off its own task row. This needs no mechanism beyond -`workflow.call` plus per-task profile resolution (`daemon.pinTaskProfile`), both -of which already treat every task's profile independently of any caller's -- see -`TestWorkflowCallerConformance` (staterpc) for the callee's independent -`Workflow` field and `TestWorkflowCallGivesACalleeItsOwnProfile` (daemon) for -the Kit-vs-image runner selection that independence produces. - -Profiles are org resources in the State Store, so a Kit profile belongs to one -org, is granted to its workspaces like any profile, and applies without a -restart. - -## Organisations - -`docs/prds/orgs-and-access.md` is the boundary; a harness adds nothing that -crosses it. - -- A Kit profile, its credential bindings and its OAuth tokens belong to one org. - No record is shared between orgs. -- A harness run is a run like any other: it acts as the workflow's identity, - under the run credential issued at dispatch. -- The egress proxy injects a credential only when the run credential carries the - secret the binding names. A Kit that declares a service the identity is not - granted gets no credential: a required one refuses the launch, an optional one - is skipped. -- A Kit's network policy is what the Kit asks for; the identity's network grants - are what the org allows. The proxy enforces the intersection. -- Concurrency slots are counted per binding within its org. - -## Credentials - -A credential binding maps a Kit's `credential@1` `service` name to an org -secret, which the org grants to identities like any secret. The secret holds one -of: - -- **API key:** a value in the org's secret store. -- **OAuth:** tokens captured by the setup terminal, stored in the State Store - under the at-rest envelope of `binding-secret-encryption.md` with its own - domain separator. - -The egress proxy resolves the binding for each request against the run -credential. The container gets the Kit's sentinel in the named variable or -credential file. OAuth refresh happens at the proxy, which intercepts the Kit's -`tokenEndpoint`, so a refreshed token is written to the State Store and never -returned to the container. - -Runs sharing an OAuth binding share its subscription's rate limit. -`max_concurrent` on the binding (default 1) caps its simultaneous runs. A stage -waiting for a slot is `pending`, not `running`. - -## Session contract - -A **session** is a duplex PTY into a Kit container the daemon owns. One concept -covers both targets; only the address, the lifetime and the established egress -phase differ. - -| | Setup session | Run session (deferred) | -| --------- | ------------------------------------------------------------- | ------------------------------ | -| Address | `{org, profile}` | `{org, task, attempt}` | -| Container | ephemeral, from the profile's Kit | the task's running container | -| Purpose | run the CLI's own login so the proxy captures the tokens | watch and optionally take over | -| Lifetime | bounded by the session TTL; the container is removed on close | bounded by the task | - -- **Transport.** The daemon serves `harness.v1.HarnessService`, a bidi gRPC - stream, and the dashboard implements its `HarnessTerminal` interface - (`internal/webui`) over that client; the browser's WebSocket route is - unchanged. The contract lives on the daemon because the daemon owns - containers: the dashboard process must not link `internal/container`, - `internal/app/archied` or `internal/agentexec`. -- **Authorisation.** Opening a session is the `update` action on the secret, - decided by the dashboard's authorizer before the route runs - (`internal/webui/authorize.go`). The dashboard then dials the daemon with the - harness service token. The daemon does not re-authorise an operator; it trusts the - asserted `{org, profile}` exactly as the task-action RPC trusts an asserted - actor. Because that authority is a shell in a container holding an org's - credential, the harness listener requires `HARNESS_TOKEN` even on loopback, - unlike the State Store's loopback convention. -- **Egress phase.** Token interception is filtered by phase (`oauth.go`'s - `rule.runtime == atRun`), and `EnterRuntime` is one-way. A setup session - therefore follows a run's lifecycle exactly: install hooks at the install - phase, `EnterRuntime` before the shell. A credential whose token endpoint must - be captured declares `phase: runtime`; a credential capturing at install phase - is captured by the install hooks that run before the switch. -- **Credentials.** Capture is gated: the proxy intercepts a token endpoint only - for a service the session is granted (`Proxy.granted`). A setup session has no - task, so the daemon registers a session grant: its proxy token resolves to - `{org, the Kit's declared credential services the org binds}`. That is the - intersection the `update`-on-secret authorisation covers. The real token never - enters the container: the response is sentinelized and stored as the org's - harness secret. -- **Container discipline.** The setup container runs a keepalive entrypoint with - an empty `Config.Env`; the PTY is an exec as the Kit's harness user with the - harness env and nothing inherited from any worker. No worktree is mounted. It - is acquired through the container pool, so it carries the `archie-daemon` - label, the concurrency cap and the max-uptime reaper; the session TTL is the - smaller bound. Closing revokes the egress session, removes the sandbox network - and the Kit volumes, then releases the container. -- **Kit unavailable.** The daemon serves the contract only when it has a - container pool and a Kit launcher; otherwise there is no listener, and a - client's refused connection is gRPC's `Unavailable`. The dashboard's existing - failure surface is the honest degrade; `/api/harness`'s `terminal` field means - "a client is configured", not "the daemon can run a Kit". -- **Run target.** `RunTarget{task_id, attempt}` opens a read-only view of a - running task's sandbox; it ships as the peek. The daemon resolves the pair - itself, so the dashboard never learns a container name: the task store says - whether the task is `running` and which attempt is current, and the Kit - launcher names the container `kitrun.TaskContainer(task_id, attempt)` (one - container per attempt, removed when the run ends; volumes remain task-scoped - for retries). A finished or unknown task, a task that is not running, an - attempt other than the running one, or a container that is gone is `NotFound` - with the reason in the message; an unreachable task store or container engine - is `Unavailable`. The view follows the container's stdout and stderr, - including the harness output forwarded by the worker, carries no stdin and no - exec, and ends when the task's container ends or the client leaves, so it - confers no authority over the container. A read-only tail of the existing - per-attempt logs was rejected: those refresh only on task state changes and - are the daemon's account, not the sandbox's. Mid-stream failures are shown as - a reason in the terminal before it closes. - - **Authorisation.** The dashboard route `GET /api/tasks/{id}/sandbox` is - decided as `read-logs` on the task before the handler runs, so a caller sees - a sandbox exactly when they may read that task's log. The daemon checks that - the task belongs to the asserted org before following output. - - **Grants.** The view takes no grant of its own. The run's egress session - (its proxy token, install then `EnterRuntime`) already belongs to the run - and is revoked by `Launcher.Release` when the task ends; the view reads - after that phase switch and dies with the container. - - **Credentials.** The sandbox holds sentinels, never real credentials: API - keys are `egress.Sentinel` (`credentials.go`), and an OAuth token set is - sentinelised in the response and swapped back by the proxy only on a granted - service's resource host in the matching phase (`oauth.go`, `injectOAuth`). - The proxy runs in the daemon outside the container, so a shell in the - container sees only sentinels. - - **Takeover (interactive PTY into a running task) is out of scope for this - issue.** It needs the dangerous-command authority (#1574) to - gate it, and that authority is not wired for it: the harness listener has no - approval step; `ToolSetOptions.Approval` in `internal/agentexec/modelloop` - is never set by a caller, so task runs never build approval-required tools; - approvals reach an operator only on the chat paths (dashboard ask, Telegram, - Gateway). Takeover would also need an audit record naming the actor, task, - attempt and open/close times, and revocation tied to task end (the exec is - closed with the container). Those follow the setup session's shape - (`harnesssession.Session`, `container.ExecPTY`) once the authority exists. -- **Audit.** Opening and closing a session and a successful capture are recorded - with the asserted org and profile. A run view's open and close are logged with - the task and attempt; it reads output only, so it has no takeover record. - -Each session is the only interactive path into a harness container. - -## Egress - -Only Kit profiles run on the egress path. An image profile keeps the network -access its identity is granted, because workflows such as a network investigator -need raw DNS, ICMP and arbitrary TCP that no HTTP proxy carries. - -- **Sandbox network.** Each Kit run gets its own internal Docker bridge on which - the host holds no address. Host services, including those listening on every - interface, are unreachable from it by any address. -- **Relay.** One relay container, `archie-agent relay`, sits on the sandbox - networks under a fixed alias and on the host-reachable network. It forwards - three fixed ports, to the proxy, NATS and the State Store, and can reach - nothing else. It holds no secrets. -- **Proxy.** The proxy runs inside `archied`, listening on the host gateway of - the host-reachable network. It terminates TLS with a per-daemon CA installed - into the container at create, identifies the container by a per-run proxy - password distinct from the worker's run grant, and applies that run's network - policy and credential inject rules. Upstream dials refuse loopback, and refuse - internal addresses unless the policy names the exact IP, so an allowed name - cannot resolve back into the host. - -## Container layout - -A Kit task runs in one container, from the Kit composition's image, on the -task's sandbox network. The worker and every harness invocation share it; they -are separated by user, not by container, so a run's cost scales with the run and -not with how many agents it fans out to. - -- **Worker.** Archie mounts its own `archie-agent` binary read-only and runs it - as the container's entrypoint, as root. It holds the task's NATS and State - Store credentials in its own environment and in root-only files, runs the - workflow, and serves `archie-agent mcp`. The Kit image's entrypoint is not - PID 1. -- **Harness.** Every invocation runs as the Kit's user (the image's `User`, - default uid 1000) with an explicit environment: the Kit's variables, the proxy - environment and the credential sentinels, and nothing inherited from the - worker. That user cannot read the worker's environment or files, or trace it. - Sibling invocations in one run share the user and the worktree. -- **Hooks.** `lifecycle@1` install and startup hooks run as the user each - declares, before the worker accepts work. -- **Mounts.** The task worktree at the Kit's workspace directory, owned by the - Kit's user, and the read-only skills store. Nothing else from the host. -- **Network.** The relay forwards three fixed ports on the sandbox network: the - egress proxy, NATS and the State Store. The worker reaches NATS and the State - Store with its credentials; a harness can open those ports but holds no - credential either accepts. -- **Volumes.** `volume@1` paths persist for the life of the WorkflowExecution, - which covers session resume across gate iterations and retries. They are not - shared between executions. - -## Archie tools inside the harness - -Capture tools return a stage's structured results through `archie-agent mcp`, a -stdio MCP server. The same command forwards central-tool discovery and calls -over Streamable HTTP to `http://archie.tools/mcp` through the egress proxy. -Claude Code, Codex, Pi, OMP and GitHub Copilot CLI register it using their -native MCP configuration. Registration files are per-stage and outside the -worktree; the Kit's bundled settings remain intact. A CLI that exits without -initializing the required MCP server fails the stage. - -`archied` handles that exact endpoint inside the authenticated task proxy; it is -not a listener or an upstream network destination. It is available only during -task runtime, never during install or a setup session. The harness supplies a -sentinel, and the proxy injects a separate random gateway token held only on the -host. Ending the proxy session revokes that token and cancels active calls. - -The proxy reaches `archie-gateway` through its authenticated gRPC contract. The -Gateway derives the task, execution principal and org from a live run grant on -every request. Its tool list is bounded by the profile in that org and the -package's accepted tool authority, checked again at invocation. Workspace tools, -chat file delivery, conversation-only tools and tools that require an -unavailable approver are not served. Tool result limits apply without exposing -host spill paths; media is returned as MCP content. - -Task MCP providers run in the Gateway with task-bound sessions and sampling. -Sampling uses the task's model and credentials, records task usage and refuses -revoked grants. It does not use the chat identity or chat model. The worker does -not start another copy of those providers for Kit tasks. The harness's own -built-in tools remain outside Archie's tool-result limits. - -## Enforcement around the harness - -| Property | Enforcement | -| -------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -| Model never runs git | No forge credential can be bound. Archie records `HEAD` and every ref before the run; any change fails the step (`failed`, policy class). The workflow commits the stage's changes. | -| Protected paths | A diff touching a `Protection` path fails the step. | -| Read-only stage | Any worktree diff fails the step. | -| Gate | Archie runs the stage's gate after the harness exits. On failure it resumes the session with the gate output, up to the stage's iteration budget. | -| Budget | Archie kills the process on wall-clock timeout, or when streamed usage passes the token budget. | -| Cancellation | Cancelling the step kills the process group, then the container. | -| Egress | Only the proxy is reachable; the Kit's network policy decides the rest. | - -Harness settings that deny git or protected paths are added where the CLI -supports them, to save wasted turns. They are never the enforcement. - -## Execution tree - -A harness run is one `agent` StepExecution. Subagents the harness spawns -internally are not recorded as children. Their usage is included in the step's -`tokens_used` when the adapter reports it. - -## Agents never hold a harness - -An Agent reaches a harness only by invoking a Workflow whose stage names it. No -Agent tool, chat tool or capability exposes a harness, its setup terminal or its -credential. The stage boundary is what carries the container, the git and path -checks, the gate and the budget; a harness outside a stage has none of them. - -## Out of scope - -- Chat through a harness. The gateway chat loop stays on the built-in runtime. -- `network-policy@2` HTTP method and path rules; a Kit requiring it is refused. -- Recording the harness's internal subagents as StepExecutions. -- Cost in currency. -- Choosing a harness automatically per task. - -## Plan - -1. Descriptor resolution with the Kit spec package; capability support table and - fail-closed refusal. -2. Egress proxy: internal network, per-daemon CA, per-container token, - `network-policy@1`. -3. `credential@1`: API-key bindings, sentinel rendering, inject rules. -4. Harness `Runner`: container create with worktree, volumes, lifecycle hooks - and the mounted `archie-agent`; `agent-sessions@1` invocation; worktree - checks for git, protected paths and read-only; budget and cancellation. -5. Claude Code output adapter; `archie-agent mcp`; gate-and-resume loop. -6. Kit profiles, runner selection from the stage's profile, and definition - validation. -7. OAuth bindings as org secrets: token-endpoint interception, encrypted - storage, per-binding concurrency; dashboard setup terminal. -8. Output adapters for Codex, Pi, OMP and GitHub Copilot CLI. - -## Verification - -- Every named adapter discovers and calls a permitted central tool through the - proxy and records a local capture. A guessed tool outside the profile or - accepted package authority is refused. -- The gateway token never appears in the container, generated MCP files, - worktree, logs or events. It is not the container's proxy password. -- A permission change after discovery takes effect at the next call; ending the - proxy session cancels active calls, and a revoked grant cannot execute tools - or sample a model. -- Bundled skills remain visible with shared skills missing or disabled; shared - mounts are per-skill and read-only. Explicit context directories preserve - non-managed instructions and refuse symlinked staging paths. -- The same workflow definition completes on `builtin` and on the Claude Code Kit - with the same `Result` shape and captures. -- After `/login` in the setup terminal, the container's credential file holds - only sentinels, the State Store holds the encrypted token, and a task run - authenticates. -- A real API key or OAuth token never appears in the container environment, - filesystem, worktree, task log or event stream. -- A request to a host outside the Kit's runtime allow list fails, including a - direct connection that bypasses the proxy settings. -- A harness instructed to `git commit` fails its step, and the task branch is - unchanged. -- A harness edit to a protected test file fails the step in a TDD fix stage. -- A read-only feasibility stage that writes a file fails. -- A file a run writes into a Kit-managed path such as the CLI's settings file is - gone on the next execution. -- A Kit requiring an unsupported capability is refused at launch with the - capability named. -- A stage with a failing gate resumes the same session with the gate output, - then succeeds or exhausts its iterations. -- Two tasks sharing an OAuth binding with `max_concurrent = 1` run one at a - time; the second waits as `pending`. -- Stopping a run kills the harness process and removes the container. -- A harness invocation cannot read the worker's NATS or State Store credential - from its environment, its files or `/proc`. -- A run fanning out to many agent invocations uses one container. -- A Kit profile in org A cannot be selected, and its binding cannot be resolved, - by a workflow in org B. -- A harness run whose identity is not granted a Kit's required credential is - refused at launch; the proxy never injects a secret outside the run - credential. -- The firewall investigation example runs unchanged on an image profile with - direct network access. diff --git a/docs/security/authorization.md b/docs/security/authorization.md index 7abc2c3f8..7c546abf6 100644 --- a/docs/security/authorization.md +++ b/docs/security/authorization.md @@ -12,7 +12,7 @@ certification. ## Transport and authority The browser uses HTTP/JSON and SSE to the UI service. The UI service calls State -Store, Gateway, Messaging and Harness through their gRPC contracts. Only the +Store, Gateway, Messaging and the daemon through their gRPC contracts. Only the State Store opens the task/control-plane/account database; Gateway owns conversation storage. Browser HTTP handlers never open either database. @@ -79,7 +79,7 @@ accounts by unverified email, or create an administrator. | Per-person memory: `archiegateway/chat_identity.go`, gateway memory tools, `sessioncurator` | Verified web sender identity scopes reads/writes. Curator refuses unidentified or mixed participants instead of guessing a person. | Gateway memory and session-curator tests | | Workflow/task actions: `webui/api_tasks.go`, `domain/taskactions`, `staterpc` | Policy and stored workspace before action; domain transition, authority and durable dispatch guards remain in force. | HTTP deny matrix, task action/domain tests, task grant tests | | Incoming webhooks: `captureintake`, access delivery engine | Verify HMAC before parsing; derive source/org from stored source; evaluate source delivery policies and egress rules. No browser-user credential is inferred. | Capture tests, `TestAuthorizeDelivery` | -| Control-plane configuration, secrets, harness and extensions | Browser policy and instance-only restrictions where registered; service contracts carry principal/org; task grants cannot invoke administrative RPCs. | HTTP deny matrix, `TestAdministrationRoutes`, `TestChannelReloadRoute`, `TestConfineHarnessOrg` | +| Control-plane configuration, secrets and extensions | Browser policy and instance-only restrictions where registered; service contracts carry principal/org; task grants cannot invoke administrative RPCs. | HTTP deny matrix, `TestAdministrationRoutes`, `TestChannelReloadRoute` | | Audit feed: `webui/api_audit.go`, `domain/audit` | `read_audit` on the `audit` resource; org scope and the instance-wide exception come from the authenticated principal, never the query string. Export carries the same grant, needs an explicit range of at most a year, and is rate-limited per identity, one at a time. Redaction runs in the State Store adapter before an entry crosses gRPC. | `TestReadAuditRoleBoundary`, `TestAuditRoutesFollowTheReadAuditRole`, `TestHTTPPolicyCannotBeBypassed`, `TestAuditErrorMapping`, `TestAuditCSVCellNeutralisesFormulas` | | Live HTTP/SSE and diagnostics | Authenticated policy gate applies before stream creation; org-scoped event reads; instance logs require administrative visibility. | HTTP deny matrix, instance-only tests, `TestServiceLogsRequireInstanceCredential` | | Background schedules, dispatch, curation | Service/agent authority, not an implicit browser user. Task-scoped grants and org scopes remain distinct from initiating operator attribution. | Existing dispatch, grant, memory and org-scope tests | @@ -146,8 +146,6 @@ action/resource; the handler may impose a stricter record or instance boundary. | GET | `/api/event-types` | | GET | `/api/extensions` | | GET | `/api/extensions/catalogue` | -| GET | `/api/harness/bindings` | -| GET | `/api/harness/terminal` | | GET | `/api/identities` | | GET | `/api/logs` | | GET | `/api/mappings` | diff --git a/go.mod b/go.mod index 41e24b1a3..2efb46951 100644 --- a/go.mod +++ b/go.mod @@ -11,9 +11,8 @@ require ( github.com/cenkalti/backoff/v5 v5.0.3 // indirect github.com/cespare/xxhash/v2 v2.3.0 // indirect github.com/cloudflare/circl v1.6.5 // indirect + github.com/containerd/errdefs v1.0.0 // indirect github.com/containerd/errdefs/pkg v0.3.0 // indirect - github.com/containerd/log v0.2.0 // indirect - github.com/containerd/platforms v1.0.0-rc.5 // indirect github.com/distribution/reference v0.6.0 // indirect github.com/docker/go-connections v0.8.2 // indirect github.com/docker/go-units v0.5.0 // indirect @@ -51,7 +50,6 @@ require ( github.com/segmentio/encoding v0.5.4 // indirect github.com/sergi/go-diff v1.4.0 // indirect github.com/sethvargo/go-retry v0.5.0 // indirect - github.com/sirupsen/logrus v1.10.2 // indirect github.com/tinylib/msgp v1.6.5 // indirect github.com/x448/float16 v0.8.4 // indirect github.com/yosida95/uritemplate/v3 v3.0.2 // indirect @@ -72,9 +70,7 @@ require ( cel.dev/cel-go v0.33.2 github.com/BurntSushi/toml v1.6.0 github.com/cedar-policy/cedar-go v1.9.0 - github.com/containerd/errdefs v1.0.0 github.com/coreos/go-oidc/v3 v3.21.0 - github.com/docker/sandbox-kit-spec/v3 v3.0.0-m.12 github.com/go-git/go-billy/v6 v6.0.0-beta.1 github.com/go-git/go-git/v6 v6.0.0-beta.1 github.com/go-jose/go-jose/v4 v4.1.9 diff --git a/go.sum b/go.sum index f973425e8..8fc317514 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,3 @@ -cel.dev/cel-go v0.32.0 h1:irvpFKr5EuGPyxeME03ERh0rii1TX+BDAnB9eL3IvNk= -cel.dev/cel-go v0.32.0/go.mod h1:DnVip7tpJSsgZymwfT+m1tnEVy3ivAjSMXPx12YrMkU= cel.dev/cel-go v0.33.2 h1:eAcEdBguc6AFrGbPOyE0g168Uey4kKFW9J6qsBLSeMU= cel.dev/cel-go v0.33.2/go.mod h1:DnVip7tpJSsgZymwfT+m1tnEVy3ivAjSMXPx12YrMkU= cel.dev/expr v0.25.3 h1:A2jO8jwOugrrovveCWfj0KEZOfqiLgAcwjpHPhzIGw0= @@ -20,8 +18,6 @@ github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5 h1:0CwZNZbxp69SHPd github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5/go.mod h1:wHh0iHkYZB8zMSxRWpUBQtwG5a7fFgvEO+odwuTv2gs= github.com/bufbuild/protocompile v0.14.1 h1:iA73zAf/fyljNjQKwYzUHD6AD4R8KMasmwa/FBatYVw= github.com/bufbuild/protocompile v0.14.1/go.mod h1:ppVdAIhbr2H8asPk6k4pY7t9zB1OU5DoEw9xY/FUi1c= -github.com/cedar-policy/cedar-go v1.8.0 h1:9gcU7EHXwHC2RMdpph68yTAkdB3behTTssC+kt4GoS8= -github.com/cedar-policy/cedar-go v1.8.0/go.mod h1:h5+3CVW1oI5LXVskJG+my9TFCYI5yjh/+Ul3EJie6MI= github.com/cedar-policy/cedar-go v1.9.0 h1:p6KSbPT0E79gO5mXNdwAqbDPn4ypz7gX5WV0ScLWmFc= github.com/cedar-policy/cedar-go v1.9.0/go.mod h1:+pmZnfwQkIUKO9qHzoL2wg8Q6xn7c9le15sexhinvVw= github.com/cenkalti/backoff/v5 v5.0.3 h1:ZN+IMa753KfX5hd8vVaMixjnqRZ3y8CuJKRKj1xcsSM= @@ -34,10 +30,6 @@ github.com/containerd/errdefs v1.0.0 h1:tg5yIfIlQIrxYtu9ajqY42W3lpS19XqdxRQeEwYG github.com/containerd/errdefs v1.0.0/go.mod h1:+YBYIdtsnF4Iw6nWZhJcqGSg/dwvV7tyJ/kCkyJ2k+M= github.com/containerd/errdefs/pkg v0.3.0 h1:9IKJ06FvyNlexW690DXuQNx2KA2cUJXx151Xdx3ZPPE= github.com/containerd/errdefs/pkg v0.3.0/go.mod h1:NJw6s9HwNuRhnjJhM7pylWwMyAkmCQvQ4GpJHEqRLVk= -github.com/containerd/log v0.2.0 h1:BewD/umNgVnoczglOpX8eRMyEy5t5iPlu5AIpnWDONc= -github.com/containerd/log v0.2.0/go.mod h1:/M7L7CXKcPTfNC74XzaK+5H5KbO5+4lJVpuVI6vRLoM= -github.com/containerd/platforms v1.0.0-rc.5 h1:vXd569rDrz8LeMXzAnBsy6LADV5YtsD8oyaRarxdmSU= -github.com/containerd/platforms v1.0.0-rc.5/go.mod h1:lKlMXyLybmBedS/JJm11uDofzI8L2v0J2ZbYvNsbq1A= github.com/coreos/go-oidc/v3 v3.21.0 h1:wZo4Q9Pum8dYEj0eMUPrqR+kvuGkeUplbLpNCkBqoWM= github.com/coreos/go-oidc/v3 v3.21.0/go.mod h1:DYCf24+ncYi+XkIH97GY1+dqoRlbaSI26KVTCI9SrY4= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= @@ -48,10 +40,6 @@ github.com/docker/go-connections v0.8.2 h1:9d9DEokx5W7qlr0shkFXIxEA7+TTD+llwzcyk github.com/docker/go-connections v0.8.2/go.mod h1:no1qkHdjq7kLMGUXYAduOhYPSJxxvgWBh7ogVvptn3Q= github.com/docker/go-units v0.5.0 h1:69rxXcBk27SvSaaxTtLh/8llcHD8vYHT7WSdRZ/jvr4= github.com/docker/go-units v0.5.0/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk= -github.com/docker/sandbox-kit-spec/v3 v3.0.0-m.8 h1:ay2XEj0yW3uzsNwN3KXS/c4nlneHONFKaHlrVhu2WCQ= -github.com/docker/sandbox-kit-spec/v3 v3.0.0-m.8/go.mod h1:4G6VgFAp9eaQkwy7DbZbOfWRD/Mjvc/TnEtQBRRMEsA= -github.com/docker/sandbox-kit-spec/v3 v3.0.0-m.12 h1:ZPkgVmmfBTEYUdVpEHVFF8BxTXFrYONQ3cDTgAc/JXc= -github.com/docker/sandbox-kit-spec/v3 v3.0.0-m.12/go.mod h1:s/lMVROKewN90s0OQ4WrOi6v3+IBDtRI75Hmol8rG4Y= github.com/dustin/go-humanize v1.1.0 h1:dbKTrvD0klcbBV/h4AWJdMuZogJACoMlvWIWZ5b2xWg= github.com/dustin/go-humanize v1.1.0/go.mod h1:hc1CvRkJMsgxqjmjMQF3QNRAZBwY8AXBAzKYoSX9sFI= github.com/emirpasic/gods v1.18.1 h1:FXtiHYKDGKCW2KzwZKx0iC0PQmdlorYgdFG9jPXJ1Bc= @@ -61,8 +49,6 @@ github.com/fatih/color v1.19.0 h1:Zp3PiM21/9Ld6FzSKyL5c/BULoe/ONr9KlbYVOfG8+w= github.com/fatih/color v1.19.0/go.mod h1:zNk67I0ZUT1bEGsSGyCZYZNrHuTkJJB+r6Q9VuMi0LE= github.com/felixge/httpsnoop v1.1.0 h1:3YtUj32ZZkqZtt3sZZsClsymw/QDuVfpNhoA31zeORc= github.com/felixge/httpsnoop v1.1.0/go.mod h1:Zqxgdd+1Rkcz8euOqdr7lqgCRJztwr5hp9vDSi5UZCE= -github.com/fxamacker/cbor/v2 v2.9.6 h1:NHZPxuUjJXQjX9itA785y/5NKSlI4HQ+S7noZsmI7hs= -github.com/fxamacker/cbor/v2 v2.9.6/go.mod h1:4EaZp6ClVunETjQ4GHmGNzysDK91Lr+r6Yz6tJqzVCw= github.com/fxamacker/cbor/v2 v2.10.0 h1:HVF3T5vYwLqO81ebPirWEPNv2SMrUMD3HLnKgEr6uWE= github.com/fxamacker/cbor/v2 v2.10.0/go.mod h1:uSwlToFuT9BZAKRxxVm3qQC8DR4tlWl7D6pf20iRYH4= github.com/gliderlabs/ssh v0.3.8 h1:a4YXD1V7xMF9g5nTkdfnja3Sxy1PVDCj1Zg4Wb8vY6c= @@ -75,8 +61,6 @@ github.com/go-git/go-git-fixtures/v6 v6.0.0-alpha.1 h1:gmqi2jvsreu0s8JMLylYDFq4s github.com/go-git/go-git-fixtures/v6 v6.0.0-alpha.1/go.mod h1:ECf1MqJlBdYpKggBrOXjo/0EnvRZx6D++I86UYjPgAQ= github.com/go-git/go-git/v6 v6.0.0-beta.1 h1:1t+vJYGDBN6f9GRWskgWx5UjXPFUa7wfffZZZ2C5GtA= github.com/go-git/go-git/v6 v6.0.0-beta.1/go.mod h1:ugDlr38Wwc+fOw85J+Oluh5TNXQtJgYMcriOljg3l4s= -github.com/go-jose/go-jose/v4 v4.1.6 h1:sNRkftxFJxwsILiQukqp5z83QBSmntVwmgRY7AqBDO4= -github.com/go-jose/go-jose/v4 v4.1.6/go.mod h1:x4oUasVrzR7071A4TnHLGSPpNOm2a21K9Kf04k1rs08= github.com/go-jose/go-jose/v4 v4.1.9 h1:pKfeQkig1NmgK4hJrZYXLEYURXfoJB61Larw0uIW4gY= github.com/go-jose/go-jose/v4 v4.1.9/go.mod h1:x4oUasVrzR7071A4TnHLGSPpNOm2a21K9Kf04k1rs08= github.com/go-json-experiment/json v0.0.0-20260820222146-c27c302e5fc3 h1:UADEEmDKgfXbtnGJZ97beY5XLo9ZechG1nlU4KnRrkE= @@ -106,8 +90,6 @@ github.com/google/jsonschema-go v0.4.3 h1:/DBOLZTfDow7pe2GmaJNhltueGTtDKICi8V8p+ github.com/google/jsonschema-go v0.4.3/go.mod h1:r5quNTdLOYEz95Ru18zA0ydNbBuYoo9tgaYcxEYhJVE= github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= -github.com/grpc-ecosystem/grpc-gateway/v2 v2.32.0 h1:QMILzF8BA7xftE4zVORkRrlv08m12St3csK4RsyRgRs= -github.com/grpc-ecosystem/grpc-gateway/v2 v2.32.0/go.mod h1:+ZQ6iUL8+ATyxGDQNuRKpNwJFdGDUi0e7E1QL9MvI7U= github.com/grpc-ecosystem/grpc-gateway/v2 v2.33.0 h1:5T5Ns3kMpWr+z3s5ZA0e7ljVyWNVNxK76sHPAxi5D1k= github.com/grpc-ecosystem/grpc-gateway/v2 v2.33.0/go.mod h1:oWNYw09cH8wl8fFgC5hKlBLxR4XfKy2KQpP8MJjeCcc= github.com/hashicorp/go-hclog v1.6.3 h1:Qr2kF+eVWjTiYmU7Y31tYlP1h0q/X3Nl3tPGdaB11/k= @@ -122,8 +104,6 @@ github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsI github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg= github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 h1:iCEnooe7UlwOQYpKFhBabPMi4aNAfoODPEFNiAnClxo= github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM= -github.com/jackc/pgx/v5 v5.11.0 h1:IzBBtyK9AHqf98cctWFifYSci2hgQR/cd56wB4p+ogg= -github.com/jackc/pgx/v5 v5.11.0/go.mod h1:mal1tBGAFfLHvZzaYh77YS/eC6IX9OWbRV1QIIM0Jn4= github.com/jackc/pgx/v5 v5.12.0 h1:hWwax8YiGT0YiN50U1oIDn5kgR7KC2tG3rmavwpPkxY= github.com/jackc/pgx/v5 v5.12.0/go.mod h1:u2Fi6no03HCSBYE+ZVdLxbwdu3xyi2vU+K/YNJioSds= github.com/jackc/puddle/v2 v2.2.3 h1:f8HoREISb3l5eO9coPgK7U3hKLhWGokOFVcThl5jrao= @@ -161,18 +141,12 @@ github.com/moby/moby/client v0.6.2 h1:ekSPlMccrZGcw6Scb8T9D39qE0mB+XupU/yS0zNw9N github.com/moby/moby/client v0.6.2/go.mod h1:DGcnO7qmPJrqobx/ggQgQ/AUk+2wgVdQv7uxB0/mEsc= github.com/modelcontextprotocol/go-sdk v1.8.0 h1:KIvahhYqwtbeniWVPs3TcXEA7b8jEtwfBpOTAI+Urx4= github.com/modelcontextprotocol/go-sdk v1.8.0/go.mod h1:dL7u98E/zjJTGzEq+j30jQ8K2k1mb6LeAH4inEcSGts= -github.com/nats-io/jwt/v2 v2.8.2 h1:XXRgB60MSTnqsRwejQurVDs/hcv2dkt+86GjI+I/bMc= -github.com/nats-io/jwt/v2 v2.8.2/go.mod h1:Ag/56sq9OblL4JgdYufDd16Egb17Kr/8WwwuO/forVc= github.com/nats-io/jwt/v2 v2.8.3 h1:pqImTmlqH2zPHKNdRXO0YRyWif2diGRlrtLFJ2RxM2I= github.com/nats-io/jwt/v2 v2.8.3/go.mod h1:OOY2rP/UU8tcfoqhbHmBKcNdpFMKvmxIkmFFa16M4wI= -github.com/nats-io/nats-server/v2 v2.15.1 h1:fK3AeYFIji1Smng60jB0hVoAPZezA/UzsaI3BqXFNEI= -github.com/nats-io/nats-server/v2 v2.15.1/go.mod h1:yeuYRtKVKIFtmMz2/YPk+6hvmNBnk+WwTUzErBdH6IE= github.com/nats-io/nats-server/v2 v2.15.2 h1:yLsgt2kslgymvxvjFq1WGWck5hkat2YYfbXvQ145tzU= github.com/nats-io/nats-server/v2 v2.15.2/go.mod h1:VXHCZgDglV5wO2B8ZE58yG2HzqxQxYUbQqibUh5Xcr8= github.com/nats-io/nats.go v1.55.0 h1:Ng8QKHdvdHIWoaiq0+d/aH1BWkxvcJxWJ0Y/9BPKx3M= github.com/nats-io/nats.go v1.55.0/go.mod h1:y+DZoD1oBOYfZTU681eTUiUjI0vbqYGixNVFHcjHJ0k= -github.com/nats-io/nkeys v0.4.16 h1:rd5oAuLOb8mnAycB0xleuEBNS1pVVnN0fv/FF34Eypg= -github.com/nats-io/nkeys v0.4.16/go.mod h1:llLgWoI0o4z/Q57q2R1kHfmocyhGV6VG/U18Glg1Afs= github.com/nats-io/nkeys v0.4.17 h1:JAJrlDhOyQqOt0a1eg4cbIZ1Kc7+vmkCigAltpwQpVA= github.com/nats-io/nkeys v0.4.17/go.mod h1:llLgWoI0o4z/Q57q2R1kHfmocyhGV6VG/U18Glg1Afs= github.com/nats-io/nuid v1.0.1 h1:5iA8DT8V7q8WK2EScv2padNa/rTESc1KdnPw4TC2paw= @@ -208,8 +182,6 @@ github.com/sergi/go-diff v1.4.0 h1:n/SP9D5ad1fORl+llWyN+D6qoUETXNZARKjyY2/KVCw= github.com/sergi/go-diff v1.4.0/go.mod h1:A0bzQcvG0E7Rwjx0REVgAGH58e96+X0MeOfepqsbeW4= github.com/sethvargo/go-retry v0.5.0 h1:JYfFKmVJ8ER06GReN1efzc5cm52BrVsFVzmcHiKJwfY= github.com/sethvargo/go-retry v0.5.0/go.mod h1:tvsjdKG6xfiCx4LSiUZ06kcv38xvdVQwv8R6/VnnVWg= -github.com/sirupsen/logrus v1.10.2 h1:G2SED73/qrAu6YwbdxOD6peLkCBI3z7L+ykJFTXJBBo= -github.com/sirupsen/logrus v1.10.2/go.mod h1:SLEg8TqYulVKKfIGHldVp2K2aYz2DKSVBq4g/H5bR7Q= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= @@ -253,16 +225,10 @@ go.uber.org/mock v0.6.0 h1:hyF9dfmbgIX5EfOdasqLsWD6xqpNZlXblLB/Dbnwv3Y= go.uber.org/mock v0.6.0/go.mod h1:KiVJ4BqZJaMj4svdfmHM0AUx4NJYO8ZNpPnZn1Z+BBU= go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0= go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y= -go.yaml.in/yaml/v3 v3.0.5 h1:N6y/pJk8buWs9NY5ERU2HSMfm+IuD/OtfdAnq6kESPw= -go.yaml.in/yaml/v3 v3.0.5/go.mod h1:HVTZu1O7/Vkt2N+BFy8Zza+lnLsABggaTM2ZpNIGuKg= go.yaml.in/yaml/v3 v3.0.6 h1:zcDfvjXEqTQmpjniUBLc91xYJMB2c4QOFxEF/LCEckM= go.yaml.in/yaml/v3 v3.0.6/go.mod h1:HVTZu1O7/Vkt2N+BFy8Zza+lnLsABggaTM2ZpNIGuKg= -golang.org/x/crypto v0.58.0 h1:COkYLr4k7nDI4r6QtMML2AfzeIswWHAJthLS6K/M/54= -golang.org/x/crypto v0.58.0/go.mod h1:Xh+kl5A+M0gMiWDytH2Hhr5Z7WbNCpv/eB/EFgfSk+w= golang.org/x/crypto v0.58.1-0.20261009181718-2f1f834330b2 h1:zGDaB+/Mus2mScKvZ23k+W9NAUbP56sNM9SptgvWI5A= golang.org/x/crypto v0.58.1-0.20261009181718-2f1f834330b2/go.mod h1:Xh+kl5A+M0gMiWDytH2Hhr5Z7WbNCpv/eB/EFgfSk+w= -golang.org/x/exp v0.0.0-20261014143641-f0251eb4aa3e h1:HgI6W1OolKK6tcV74is+rl9SMT0njsHlp0TbQiGXI/A= -golang.org/x/exp v0.0.0-20261014143641-f0251eb4aa3e/go.mod h1:DBoxwWhjaW8DJJw/YeM4zpeOKMC1FMXSblsYy+1mFSw= golang.org/x/exp v0.0.0-20261106192419-47ce03d25ef0 h1:1GO4fJiyGakz0Liu2Ve1qPAwQYPkKlG1Q8iO2CYjtFA= golang.org/x/exp v0.0.0-20261106192419-47ce03d25ef0/go.mod h1:J3U+E8RzytcGvwz+uJuAP4zULJ2TkAQBnmPZMexpdXs= golang.org/x/mod v0.42.0 h1:ICeL3t2L30ljSPHHxDnR4fqVcBt/zR2zJAb9juavmQg= @@ -288,19 +254,12 @@ golang.org/x/text v0.43.0 h1:1QivrlhwAsnMOcqgOdl9my7s9OyZl8O5/UGUTJVXoWc= golang.org/x/text v0.43.0/go.mod h1:hbSIYA/amXcRXTFSZqh8tWHEoqRvvbKPJ2uz/KU+a+s= golang.org/x/time v0.16.0 h1:vMb6ptszcQMkcwiRTAuNNU50gom6++Q/6gY2hDM6VDE= golang.org/x/time v0.16.0/go.mod h1:rVKOqvZeKvrDKTQiAHJ7wmwP0RzleSphoEA9RcdLA0s= -golang.org/x/tools v0.52.0 h1:ryxg4U63oaUYk7uOLTF37n43f4qgtGvqV4Ioit1BaEs= -golang.org/x/tools v0.52.0/go.mod h1:ItcP5UNaVGG+Yc4eIYmaFLNJ4soEopkx5Y2d4lcEurs= golang.org/x/tools v0.52.1-0.20261106145633-d407941a339a h1:Yqozs0FNPRoPGS4hNGlrNwRENWr5H5nVG9sN/f+5640= +golang.org/x/tools v0.52.1-0.20261106145633-d407941a339a/go.mod h1:ItcP5UNaVGG+Yc4eIYmaFLNJ4soEopkx5Y2d4lcEurs= gonum.org/v1/gonum v0.17.0 h1:VbpOemQlsSMrYmn7T2OUvQ4dqxQXU+ouZFQsZOx50z4= gonum.org/v1/gonum v0.17.0/go.mod h1:El3tOrEuMpv2UdMrbNlKEh9vd86bmQ6vqIcDwxEOc1E= -google.golang.org/genproto/googleapis/api v0.0.0-20261019180522-ce908d29299b h1:nB3spRIaSPtxJpxb0k41yljROvm2Jrwfs2pSNdKS5y8= -google.golang.org/genproto/googleapis/api v0.0.0-20261019180522-ce908d29299b/go.mod h1:9N4vtjEwvgQKEdlFVQO0M44ErhecQ9DCSQfGoZ51M6w= -google.golang.org/genproto/googleapis/api v0.0.0-20261026165753-2ffab32bce7e h1:e789sLZjeTdc02ZCQGTSzHOu0fR73+dwPQVKUp67Z88= -google.golang.org/genproto/googleapis/api v0.0.0-20261026165753-2ffab32bce7e/go.mod h1:vHV3wJM9WZyfV7GyB+OQGq5dWVrFwCDxapPK4WlRD88= google.golang.org/genproto/googleapis/api v0.0.0-20261104224025-d1e00e9f33ab h1:n6IDWZ1RzI/NberLoLLFNrjl1H45uhc4+WYsUf5lPPc= google.golang.org/genproto/googleapis/api v0.0.0-20261104224025-d1e00e9f33ab/go.mod h1:vHV3wJM9WZyfV7GyB+OQGq5dWVrFwCDxapPK4WlRD88= -google.golang.org/genproto/googleapis/rpc v0.0.0-20261019180522-ce908d29299b h1:6WNWHZk91dp4gZ25642OALkuG1UVNZbwENJD19DnB8k= -google.golang.org/genproto/googleapis/rpc v0.0.0-20261019180522-ce908d29299b/go.mod h1:OaIUM3+LpYcK2GXM4FTmhWoIq371Owdr+Cc7/BsYHHc= google.golang.org/genproto/googleapis/rpc v0.0.0-20261104224025-d1e00e9f33ab h1:8sYjjefPfQ+FZEh1ZwBAqK/NifJVhOxut8nRh3SJzZM= google.golang.org/genproto/googleapis/rpc v0.0.0-20261104224025-d1e00e9f33ab/go.mod h1:OaIUM3+LpYcK2GXM4FTmhWoIq371Owdr+Cc7/BsYHHc= google.golang.org/grpc v1.86.0-dev h1:FLq7hcApuJeJ70J9+k0FvmXsNCwS8B2/MeAfYdy3GHY= diff --git a/install.sh b/install.sh index 62a61325c..20367124e 100755 --- a/install.sh +++ b/install.sh @@ -230,9 +230,8 @@ else fi TOPOLOGY_UNITS="$(tr -d '\n' <"${STAGE}/release.json" | sed -n 's/.*"units": *\[\([^]]*\)\].*/\1/p' | tr -d '" ' | tr , ' ')" -# Binaries that run beside the units without a service of their own: a Kit run -# mounts archie-agent from archied's directory, so it must be installed with -# them and replaced with them. +# Binaries that run beside the units without a service of their own are +# installed and replaced with them. TOPOLOGY_BINARIES="$(tr -d '\n' <"${STAGE}/release.json" | sed -n 's/.*"binaries": *\[\([^]]*\)\].*/\1/p' | tr -d '" ' | tr , ' ')" for cmd in ${TOPOLOGY_UNITS} ${TOPOLOGY_BINARIES}; do install -m755 "${STAGE}/${cmd}" "${ARCHIE_BIN_DIR}/${cmd}" diff --git a/internal/agentexec/capture_mcp.go b/internal/agentexec/capture_mcp.go deleted file mode 100644 index 340ae12d4..000000000 --- a/internal/agentexec/capture_mcp.go +++ /dev/null @@ -1,153 +0,0 @@ -package agentexec - -import ( - "bufio" - "bytes" - "context" - "encoding/json" - "errors" - "fmt" - "io" - "os" - "sync" - "sync/atomic" - - "github.com/modelcontextprotocol/go-sdk/mcp" - - "github.com/samcharles93/archie-core/internal/domain/agentrun" -) - -const mcpConfigPlaceholder = "{{.MCPConfig}}" - -// captureRecord is one accepted capture call, as the MCP server appends it -// to the captures file. -type captureRecord struct { - Tool string `json:"tool"` - Args json.RawMessage `json:"args"` -} - -// AcceptCapture decides whether one capture call is kept. Both the built-in -// loop and the harness path use it, and the harness path applies it twice: -// in the MCP server, and again when the worker reads the captures back. -func AcceptCapture(spec agentrun.CaptureTool, accepted int, value json.RawMessage) (string, bool) { - if spec.MaxCalls > 0 && accepted >= spec.MaxCalls { - return fmt.Sprintf("%s rejected: maximum call count is %d", spec.Name, spec.MaxCalls), false - } - if !json.Valid(value) { - return spec.Name + " rejected: arguments are not valid JSON", false - } - if rejection, ok := ValidateCaptureArgs(spec, value); !ok { - return rejection, false - } - return spec.Name + " recorded", true -} - -func serveHarnessMCP(ctx context.Context, specs []agentrun.CaptureTool, sink io.Writer, t mcp.Transport, server *mcp.Server, central bool) error { - if central { - cleanup, err := addCentralTools(ctx, server, specs) - if err != nil { - return err - } - defer cleanup() - } - var mu sync.Mutex - accepted := map[string]int{} - for _, spec := range specs { - schema := spec.Parameters - if len(schema) == 0 { - schema = json.RawMessage(`{"type":"object"}`) - } - server.AddTool(&mcp.Tool{Name: spec.Name, Description: spec.Description, InputSchema: schema}, - func(_ context.Context, req *mcp.CallToolRequest) (*mcp.CallToolResult, error) { - mu.Lock() - defer mu.Unlock() - args := append(json.RawMessage(nil), req.Params.Arguments...) - reply, ok := AcceptCapture(spec, accepted[spec.Name], args) - if ok { - line, err := json.Marshal(captureRecord{Tool: spec.Name, Args: args}) - if err != nil { - return nil, err - } - if _, err := sink.Write(append(line, '\n')); err != nil { - return nil, fmt.Errorf("record %s: %w", spec.Name, err) - } - accepted[spec.Name]++ - } - return &mcp.CallToolResult{Content: []mcp.Content{&mcp.TextContent{Text: reply}}}, nil - }) - } - return server.Run(ctx, t) -} - -// readCaptures reads a captures file back into a stage's captures. The file -// is writable by the harness user, so nothing in it is trusted: every record -// is checked again, and anything that is not an accepted call of a declared -// tool is dropped. -func readCaptures(path string, specs []agentrun.CaptureTool) (map[string][]json.RawMessage, error) { - raw, err := os.ReadFile(path) - if errors.Is(err, os.ErrNotExist) { - return nil, nil - } - if err != nil { - return nil, err - } - byName := make(map[string]agentrun.CaptureTool, len(specs)) - for _, s := range specs { - byName[s.Name] = s - } - captures := map[string][]json.RawMessage{} - scanner := bufio.NewScanner(bytes.NewReader(raw)) - scanner.Buffer(make([]byte, 64*1024), 16*1024*1024) - for scanner.Scan() { - var rec captureRecord - if json.Unmarshal(scanner.Bytes(), &rec) != nil { - continue - } - spec, declared := byName[rec.Tool] - if !declared { - continue - } - if _, ok := AcceptCapture(spec, len(captures[rec.Tool]), rec.Args); ok { - captures[rec.Tool] = append(captures[rec.Tool], rec.Args) - } - } - if len(captures) == 0 { - return nil, scanner.Err() - } - return captures, scanner.Err() -} - -// ServeCaptureFiles serves the capture tools listed in specPath, appending -// accepted calls to capturesPath. The files are the ones prepareCaptures -// writes, so this package owns their format end to end. -func ServeCaptureFiles(ctx context.Context, specPath, capturesPath string, t mcp.Transport) error { - raw, err := os.ReadFile(specPath) - if err != nil { - return err - } - var spec harnessMCPSpec - if err := json.Unmarshal(raw, &spec); err != nil { - return fmt.Errorf("capture tool spec: %w", err) - } - sink, err := os.OpenFile(capturesPath, os.O_WRONLY|os.O_APPEND, 0) - if err != nil { - return err - } - defer sink.Close() - server := mcp.NewServer(&mcp.Implementation{Name: "archie", Version: "1"}, nil) - var ready atomic.Bool - server.AddReceivingMiddleware(func(next mcp.MethodHandler) mcp.MethodHandler { - return func(ctx context.Context, method string, req mcp.Request) (mcp.Result, error) { - result, err := next(ctx, method, req) - if err == nil && (method == "tools/list" || method == "tools/call") && !ready.Load() { - // A call is already recorded by now, so a failed marker must not - // turn it into a tool error; the runner reports the missing marker. - if os.WriteFile(capturesPath+".ready", nil, 0o600) == nil { - ready.Store(true) - } - } - return result, err - } - }) - return serveHarnessMCP(ctx, spec.Captures, sink, t, server, spec.CentralTools) -} diff --git a/internal/agentexec/harness_adapters.go b/internal/agentexec/harness_adapters.go deleted file mode 100644 index fc644306e..000000000 --- a/internal/agentexec/harness_adapters.go +++ /dev/null @@ -1,59 +0,0 @@ -package agentexec - -import ( - "context" - - "github.com/samcharles93/archie-core/internal/domain/agentrun" -) - -// AdapterClaudeCode names the Claude Code stream-json output adapter. -const AdapterClaudeCode = "claude-code" - -// Adapters for the other supported CLIs' JSON output streams. -const ( - AdapterCodex = "codex" - AdapterPi = "pi" - AdapterOMP = "omp" - AdapterCopilot = "copilot" -) - -// HarnessAdapter is what archie knows about one CLI: how to read its output -// stream and how to register archie's MCP server with it. -type HarnessAdapter struct { - NewOutput func() HarnessOutput - MCPConfig []string -} - -var harnessAdapters = map[string]HarnessAdapter{ - AdapterClaudeCode: { - NewOutput: func() HarnessOutput { return newClaudeCodeHarnessOutput() }, - MCPConfig: []string{"--mcp-config", mcpConfigPlaceholder}, - }, - AdapterCodex: {NewOutput: func() HarnessOutput { return &codexHarnessOutput{} }, MCPConfig: []string{"-c", mcpConfigPlaceholder}}, - AdapterPi: {NewOutput: func() HarnessOutput { return &piHarnessOutput{} }, MCPConfig: []string{"--extension", mcpConfigPlaceholder}}, - AdapterOMP: {NewOutput: func() HarnessOutput { return &piHarnessOutput{} }, MCPConfig: []string{"--extension", mcpConfigPlaceholder}}, - AdapterCopilot: { - NewOutput: func() HarnessOutput { return newCopilotHarnessOutput() }, - MCPConfig: []string{"--additional-mcp-config", "@" + mcpConfigPlaceholder}, - }, -} - -// LookupHarnessAdapter returns the named adapter. The empty name is a CLI -// with no adapter: it reports nothing and serves no capture tools. -func LookupHarnessAdapter(name string) (HarnessAdapter, bool) { - a, ok := harnessAdapters[name] - return a, ok -} - -// HarnessStages runs every stage of a task on one harness: a Kit task's -// container is built from its harness, so no stage in it runs anywhere else. -type HarnessStages struct { - Runner agentrun.Runner - Spec agentrun.HarnessSpec -} - -func (r HarnessStages) Run(ctx context.Context, workspace string, req agentrun.Request, report agentrun.Reporter) (agentrun.Result, error) { - spec := r.Spec - req.Harness = &spec - return r.Runner.Run(ctx, workspace, req, report) -} diff --git a/internal/agentexec/harness_mcp.go b/internal/agentexec/harness_mcp.go deleted file mode 100644 index 4129abe99..000000000 --- a/internal/agentexec/harness_mcp.go +++ /dev/null @@ -1,127 +0,0 @@ -package agentexec - -import ( - "context" - "encoding/json" - "fmt" - "net/http" - "net/url" - "os" - "path/filepath" - "strconv" - "strings" - - "github.com/modelcontextprotocol/go-sdk/mcp" - - "github.com/samcharles93/archie-core/internal/domain/agentrun" - "github.com/samcharles93/archie-core/internal/infrastructure/tasktools" -) - -type harnessMCPSpec struct { - Captures []agentrun.CaptureTool `json:"captures"` - CentralTools bool `json:"central_tools"` -} - -// Each adapter registers one stdio bridge; it holds only proxy credentials -// and sentinels, and never writes invocation configuration into the worktree. -func harnessMCPRegistration(adapter, dir, configPath string, server map[string]any) (string, error) { - switch adapter { - case AdapterCodex: - args, err := json.Marshal(server["args"]) - if err != nil { - return "", err - } - command, ok := server["command"].(string) - if !ok { - return "", fmt.Errorf("MCP command is missing") - } - return "mcp_servers.archie={command=" + strconv.Quote(command) + ",args=" + string(args) + ",env={HTTP_PROXY=" + strconv.Quote(harnessMCPEnvironmentValue(server)) + "},required=true}", nil - case AdapterPi: - server["exposure"] = "direct" - definition, err := json.Marshal(server) - if err != nil { - return "", err - } - path := filepath.Join(dir, "archie-mcp.ts") - script := "export default function(pi) { if (typeof pi.registerMcpServer !== 'function') throw new Error('This Pi version does not support native MCP registration'); pi.registerMcpServer('archie', " + string(definition) + "); }\n" - if err := os.WriteFile(path, []byte(script), 0o644); err != nil { - return "", err - } - return path, nil - case AdapterOMP: - // OMP discovers .mcp.json in an explicitly loaded extension package. - path := filepath.Join(dir, "index.ts") - if err := os.WriteFile(path, []byte("export default function() {}\n"), 0o644); err != nil { - return "", err - } - data, err := os.ReadFile(configPath) - if err != nil { - return "", err - } - if err := os.WriteFile(filepath.Join(dir, ".mcp.json"), data, 0o644); err != nil { - return "", err - } - return dir, nil - default: - return configPath, nil - } -} - -type centralTransport struct{ base http.RoundTripper } - -func (t centralTransport) RoundTrip(r *http.Request) (*http.Response, error) { - if r.URL.String() != tasktools.Endpoint { - return nil, fmt.Errorf("central tool request names an unexpected endpoint") - } - r = r.Clone(r.Context()) - r.Header.Set("Authorization", "Bearer "+tasktools.Sentinel) - return t.base.RoundTrip(r) -} - -func addCentralTools(ctx context.Context, server *mcp.Server, captures []agentrun.CaptureTool) (func(), error) { - proxy, err := url.Parse(os.Getenv("HTTP_PROXY")) - if err != nil || proxy.Host == "" { - return nil, fmt.Errorf("central tools require the task egress proxy") - } - transport := &http.Transport{Proxy: http.ProxyURL(proxy)} - client := mcp.NewClient(&mcp.Implementation{Name: "archie-harness", Version: "1"}, nil) - session, err := client.Connect(ctx, &mcp.StreamableClientTransport{Endpoint: tasktools.Endpoint, HTTPClient: &http.Client{Transport: centralTransport{base: transport}, CheckRedirect: func(*http.Request, []*http.Request) error { return http.ErrUseLastResponse }}, DisableStandaloneSSE: true, MaxRetries: -1, MaxEventSize: -1}, nil) - if err != nil { - transport.CloseIdleConnections() - return nil, fmt.Errorf("connect central tools: %w", err) - } - closeSession := func() { _ = session.Close(); transport.CloseIdleConnections() } - names := map[string]bool{} - for _, spec := range captures { - names[spec.Name] = true - } - for tool, err := range session.Tools(ctx, nil) { - if err != nil { - closeSession() - return nil, fmt.Errorf("list central tools: %w", err) - } - if names[tool.Name] { - closeSession() - return nil, fmt.Errorf("central tool %q collides with a capture tool", tool.Name) - } - server.AddTool(tool, func(ctx context.Context, req *mcp.CallToolRequest) (*mcp.CallToolResult, error) { - return session.CallTool(ctx, &mcp.CallToolParams{Name: req.Params.Name, Arguments: req.Params.Arguments}) - }) - } - return closeSession, nil -} - -func harnessMCPEnvironment(env []string) map[string]string { - out := map[string]string{} - for _, entry := range env { - if value, ok := strings.CutPrefix(entry, "HTTP_PROXY="); ok { - out["HTTP_PROXY"] = value - } - } - return out -} - -func harnessMCPEnvironmentValue(server map[string]any) string { - env, _ := server["env"].(map[string]string) - return env["HTTP_PROXY"] -} diff --git a/internal/agentexec/harness_mcp_test.go b/internal/agentexec/harness_mcp_test.go deleted file mode 100644 index ac51ddd9b..000000000 --- a/internal/agentexec/harness_mcp_test.go +++ /dev/null @@ -1,46 +0,0 @@ -//go:build unix - -package agentexec - -import ( - "encoding/json" - "os" - "os/exec" - "testing" - - "github.com/samcharles93/archie-core/internal/domain/agentrun" -) - -// Some CLIs log an unsupported MCP configuration but still exit successfully. -func TestHarnessRequiresMCPInitialization(t *testing.T) { - for _, central := range []bool{false, true} { - t.Run(map[bool]string{false: "captures", true: "central tools"}[central], func(t *testing.T) { - workspace, err := os.MkdirTemp("", "archie-mcp-regression-") //nolint:usetesting // The harness user cannot traverse testing's private parent directory. - if err != nil { - t.Fatal(err) - } - defer os.RemoveAll(workspace) - for _, args := range [][]string{{"init", "-q"}, {"-c", "user.name=fixture", "-c", "user.email=fixture@example.invalid", "commit", "--allow-empty", "-q", "-m", "fixture"}} { - cmd := exec.CommandContext(t.Context(), "git", append([]string{"-C", workspace}, args...)...) - if raw, err := cmd.CombinedOutput(); err != nil { - t.Fatalf("git fixture %v: %s", err, raw) - } - } - h := &agentrun.HarnessSpec{Launch: []string{"sh", "-c", "exit 0", "harness"}, Prompt: []string{"{{.Prompt}}"}, MCPConfig: []string{"{{.MCPConfig}}"}, Env: []string{"PATH=/usr/bin:/bin"}, CentralTools: central} - if os.Geteuid() == 0 { - h.User = "65534" - } - req := agentrun.Request{Version: agentrun.ProtocolVersion, TaskID: 7, Attempt: 1, Stage: "test", Mission: "return a result", Model: "fixture/model", Harness: h} - if !central { - req.CaptureTools = []agentrun.CaptureTool{{Name: "capture_result", Parameters: json.RawMessage(`{"type":"object"}`)}} - } - result, err := NewHarnessRunner().Run(t.Context(), workspace, req, agentrun.Reporter{}) - if err != nil { - t.Fatal(err) - } - if result.Status == agentrun.StatusPassed || result.StopReason != StopExited { - t.Fatalf("unsupported MCP silently passed: %+v", result) - } - }) - } -} diff --git a/internal/agentexec/harness_output_claude.go b/internal/agentexec/harness_output_claude.go deleted file mode 100644 index 732d534eb..000000000 --- a/internal/agentexec/harness_output_claude.go +++ /dev/null @@ -1,171 +0,0 @@ -package agentexec - -import ( - "bytes" - "encoding/json" - "strings" - - "github.com/samcharles93/archie-core/internal/domain/agentrun" -) - -// claudeCodeHarnessOutput reads Claude Code's newline-delimited stream-json -// output. It keeps tool-use IDs until the corresponding user tool_result -// arrives and retains assistant usage as a fallback for interrupted runs. -type claudeCodeHarnessOutput struct { - sessionID string - pending map[string]string - - messageUsage agentrun.Usage - seenMessages map[string]struct{} - hasResult bool - resultUsage agentrun.Usage -} - -func newClaudeCodeHarnessOutput() *claudeCodeHarnessOutput { - return &claudeCodeHarnessOutput{ - pending: make(map[string]string), - seenMessages: make(map[string]struct{}), - } -} - -func (o *claudeCodeHarnessOutput) Line(line []byte, report agentrun.Reporter) { - var event struct { - Type string `json:"type"` - SessionID string `json:"session_id"` - Message json.RawMessage `json:"message"` - Usage json.RawMessage `json:"usage"` - } - if json.Unmarshal(line, &event) != nil { - return - } - if event.SessionID != "" { - o.sessionID = event.SessionID - } - - switch event.Type { - case "assistant": - o.assistant(event.Message) - case "user": - o.user(event.Message, report) - case "result": - if usage, ok := decodeClaudeUsage(event.Usage); ok { - o.resultUsage = usage - o.hasResult = true - } - } -} - -func (o *claudeCodeHarnessOutput) assistant(raw json.RawMessage) { - var message struct { - ID string `json:"id"` - Usage json.RawMessage `json:"usage"` - Content []struct { - Type string `json:"type"` - ID string `json:"id"` - Name string `json:"name"` - Input any `json:"input"` - } `json:"content"` - } - if json.Unmarshal(raw, &message) != nil { - return - } - if usage, ok := decodeClaudeUsage(message.Usage); ok && (message.ID == "" || o.markMessage(message.ID)) { - o.messageUsage = addUsage(o.messageUsage, usage) - } - for _, block := range message.Content { - if block.Type == "tool_use" && block.ID != "" && block.Name != "" { - o.pending[block.ID] = block.Name - } - } -} - -func (o *claudeCodeHarnessOutput) user(raw json.RawMessage, report agentrun.Reporter) { - var message struct { - Content []struct { - Type string `json:"type"` - ToolUseID string `json:"tool_use_id"` - Content json.RawMessage `json:"content"` - IsError bool `json:"is_error"` - } `json:"content"` - } - if json.Unmarshal(raw, &message) != nil { - return - } - for _, block := range message.Content { - if block.Type != "tool_result" || block.ToolUseID == "" { - continue - } - tool, ok := o.pending[block.ToolUseID] - if !ok { - continue - } - delete(o.pending, block.ToolUseID) - report.ReportToolCall(agentrun.ToolCallReport{ - Tool: tool, Detail: ClipToolCallDetail(claudeResultContent(block.Content)), - Failed: block.IsError, - }) - } -} - -func (o *claudeCodeHarnessOutput) markMessage(id string) bool { - if _, seen := o.seenMessages[id]; seen { - return false - } - o.seenMessages[id] = struct{}{} - return true -} - -func (o *claudeCodeHarnessOutput) SessionID() string { return o.sessionID } - -func (o *claudeCodeHarnessOutput) Usage() agentrun.Usage { - if o.hasResult { - return o.resultUsage - } - return o.messageUsage -} - -func decodeClaudeUsage(raw json.RawMessage) (agentrun.Usage, bool) { - var fields struct { - InputTokens int `json:"input_tokens"` - OutputTokens int `json:"output_tokens"` - CacheReadInputTokens int `json:"cache_read_input_tokens"` - CacheCreationInputTokens int `json:"cache_creation_input_tokens"` - } - if len(raw) == 0 || bytes.Equal(bytes.TrimSpace(raw), []byte("null")) || json.Unmarshal(raw, &fields) != nil { - return agentrun.Usage{}, false - } - return usageFromFields(fields.InputTokens, fields.OutputTokens, fields.CacheReadInputTokens, fields.CacheCreationInputTokens), true -} - -// usageFromFields folds cache reads and writes into PromptTokens, as the -// built-in loop's Anthropic provider does: Claude's input_tokens excludes -// them, and budgets must count a harness run the way they count a built-in -// one. -func usageFromFields(input, output, cacheRead, cacheCreation int) agentrun.Usage { - prompt := input + cacheRead + cacheCreation - return agentrun.Usage{ - PromptTokens: prompt, CompletionTokens: output, TotalTokens: prompt + output, - CachedTokens: cacheRead, CacheCreationTokens: cacheCreation, - } -} - -func claudeResultContent(raw json.RawMessage) string { - var content string - if json.Unmarshal(raw, &content) == nil { - return content - } - var blocks []struct { - Type string `json:"type"` - Text string `json:"text"` - } - if json.Unmarshal(raw, &blocks) != nil { - return "" - } - texts := make([]string, 0, len(blocks)) - for _, block := range blocks { - if block.Type == "text" { - texts = append(texts, block.Text) - } - } - return strings.Join(texts, "\n") -} diff --git a/internal/agentexec/harness_output_codex.go b/internal/agentexec/harness_output_codex.go deleted file mode 100644 index 656ec453e..000000000 --- a/internal/agentexec/harness_output_codex.go +++ /dev/null @@ -1,107 +0,0 @@ -package agentexec - -import ( - "encoding/json" - "strings" - - "github.com/samcharles93/archie-core/internal/domain/agentrun" -) - -// codexHarnessOutput reads `codex exec --json`. Each tool item is reported -// when its item.completed event arrives; usage comes from turn.completed, so -// a run killed mid-turn reports none. -type codexHarnessOutput struct { - threadID string - usage agentrun.Usage -} - -func (o *codexHarnessOutput) Line(line []byte, report agentrun.Reporter) { - var event struct { - Type string `json:"type"` - ThreadID string `json:"thread_id"` - Usage *struct { - InputTokens int `json:"input_tokens"` - CachedInputTokens int `json:"cached_input_tokens"` - CacheWriteTokens int `json:"cache_write_input_tokens"` - OutputTokens int `json:"output_tokens"` - } `json:"usage"` - Item codexItem `json:"item"` - } - if json.Unmarshal(line, &event) != nil { - return - } - switch event.Type { - case "thread.started": - o.threadID = event.ThreadID - case "turn.completed": - if u := event.Usage; u != nil { - // Codex follows OpenAI: input_tokens already includes cached input. - o.usage = addUsage(o.usage, agentrun.Usage{ - PromptTokens: u.InputTokens, CompletionTokens: u.OutputTokens, - TotalTokens: u.InputTokens + u.OutputTokens, - CachedTokens: u.CachedInputTokens, CacheCreationTokens: u.CacheWriteTokens, - }) - } - case "item.completed": - if call, ok := event.Item.report(); ok { - report.ReportToolCall(call) - } - } -} - -type codexItem struct { - Type string `json:"type"` - Status string `json:"status"` - AggregatedOutput string `json:"aggregated_output"` - Tool string `json:"tool"` - Result *struct { - Content []struct { - Type string `json:"type"` - Text string `json:"text"` - } `json:"content"` - } `json:"result"` - Error *struct { - Message string `json:"message"` - } `json:"error"` - Changes []struct { - Path string `json:"path"` - } `json:"changes"` - Query string `json:"query"` -} - -func (item codexItem) report() (agentrun.ToolCallReport, bool) { - call := agentrun.ToolCallReport{Tool: item.Type, Failed: item.Status == "failed"} - switch item.Type { - case "command_execution": - call.Detail = item.AggregatedOutput - case "mcp_tool_call": - call.Tool = item.Tool - switch { - case item.Error != nil: - call.Detail, call.Failed = item.Error.Message, true - case item.Result != nil: - texts := make([]string, 0, len(item.Result.Content)) - for _, block := range item.Result.Content { - if block.Type == "text" { - texts = append(texts, block.Text) - } - } - call.Detail = strings.Join(texts, "\n") - } - case "file_change": - paths := make([]string, 0, len(item.Changes)) - for _, change := range item.Changes { - paths = append(paths, change.Path) - } - call.Detail = strings.Join(paths, "\n") - case "web_search": - call.Detail = item.Query - default: - return agentrun.ToolCallReport{}, false - } - call.Detail = ClipToolCallDetail(call.Detail) - return call, true -} - -func (o *codexHarnessOutput) SessionID() string { return o.threadID } -func (o *codexHarnessOutput) Usage() agentrun.Usage { return o.usage } diff --git a/internal/agentexec/harness_output_copilot.go b/internal/agentexec/harness_output_copilot.go deleted file mode 100644 index 5afc2e27f..000000000 --- a/internal/agentexec/harness_output_copilot.go +++ /dev/null @@ -1,60 +0,0 @@ -package agentexec - -import ( - "encoding/json" - - "github.com/samcharles93/archie-core/internal/domain/agentrun" -) - -// copilotHarnessOutput reads GitHub Copilot CLI's `--output-format json`. -// The stream carries no token counts, so a Copilot run reports no usage and -// its budget is wall clock. -type copilotHarnessOutput struct { - sessionID string - pending map[string]string -} - -func newCopilotHarnessOutput() *copilotHarnessOutput { - return &copilotHarnessOutput{pending: make(map[string]string)} -} - -func (o *copilotHarnessOutput) Line(line []byte, report agentrun.Reporter) { - var event struct { - Type string `json:"type"` - SessionID string `json:"sessionId"` - Data struct { - ToolCallID string `json:"toolCallId"` - ToolName string `json:"toolName"` - MCPToolName string `json:"mcpToolName"` - Success bool `json:"success"` - Result struct { - Content string `json:"content"` - } `json:"result"` - } `json:"data"` - } - if json.Unmarshal(line, &event) != nil { - return - } - switch event.Type { - case "result": - o.sessionID = event.SessionID - case "tool.execution_start": - name := event.Data.MCPToolName - if name == "" { - name = event.Data.ToolName - } - if event.Data.ToolCallID != "" && name != "" { - o.pending[event.Data.ToolCallID] = name - } - case "tool.execution_complete": - tool, ok := o.pending[event.Data.ToolCallID] - if !ok { - return - } - delete(o.pending, event.Data.ToolCallID) - report.ReportToolCall(agentrun.ToolCallReport{Tool: tool, Detail: ClipToolCallDetail(event.Data.Result.Content), Failed: !event.Data.Success}) - } -} - -func (o *copilotHarnessOutput) SessionID() string { return o.sessionID } -func (o *copilotHarnessOutput) Usage() agentrun.Usage { return agentrun.Usage{} } diff --git a/internal/agentexec/harness_output_pi.go b/internal/agentexec/harness_output_pi.go deleted file mode 100644 index df2a0f611..000000000 --- a/internal/agentexec/harness_output_pi.go +++ /dev/null @@ -1,66 +0,0 @@ -package agentexec - -import ( - "encoding/json" - "strings" - - "github.com/samcharles93/archie-core/internal/domain/agentrun" -) - -// piHarnessOutput reads `--mode json` from Pi and OMP, which share the -// event stream. Usage is summed from each assistant message_end, so an -// interrupted run still reports what it spent. -type piHarnessOutput struct { - sessionID string - usage agentrun.Usage -} - -func (o *piHarnessOutput) Line(line []byte, report agentrun.Reporter) { - var event struct { - Type string `json:"type"` - ID string `json:"id"` - ToolName string `json:"toolName"` - IsError bool `json:"isError"` - Result struct { - Content []struct { - Type string `json:"type"` - Text string `json:"text"` - } `json:"content"` - } `json:"result"` - Message struct { - Role string `json:"role"` - Usage *struct { - Input int `json:"input"` - Output int `json:"output"` - CacheRead int `json:"cacheRead"` - CacheWrite int `json:"cacheWrite"` - } `json:"usage"` - } `json:"message"` - } - if json.Unmarshal(line, &event) != nil { - return - } - switch event.Type { - case "session": - o.sessionID = event.ID - case "message_end": - // Pi's input excludes cache reads and writes, as Claude's does. - if u := event.Message.Usage; event.Message.Role == "assistant" && u != nil { - o.usage = addUsage(o.usage, usageFromFields(u.Input, u.Output, u.CacheRead, u.CacheWrite)) - } - case "tool_execution_end": - if event.ToolName == "" { - return - } - texts := make([]string, 0, len(event.Result.Content)) - for _, block := range event.Result.Content { - if block.Type == "text" { - texts = append(texts, block.Text) - } - } - report.ReportToolCall(agentrun.ToolCallReport{Tool: event.ToolName, Detail: ClipToolCallDetail(strings.Join(texts, "\n")), Failed: event.IsError}) - } -} - -func (o *piHarnessOutput) SessionID() string { return o.sessionID } -func (o *piHarnessOutput) Usage() agentrun.Usage { return o.usage } diff --git a/internal/agentexec/harness_proc_other.go b/internal/agentexec/harness_proc_other.go deleted file mode 100644 index 2e0c655cc..000000000 --- a/internal/agentexec/harness_proc_other.go +++ /dev/null @@ -1,30 +0,0 @@ -//go:build !unix - -package agentexec - -import ( - "errors" - "os/exec" -) - -// setProcessGroup falls back to killing the harness process alone where -// process groups are unavailable. -func setProcessGroup(*exec.Cmd) {} - -// reapGroup has nothing to do where process groups are unavailable. -func reapGroup(*exec.Cmd) {} - -// runAsHarnessUser refuses a named harness user where user switching is -// unavailable, rather than running the harness as the worker. -func runAsHarnessUser(_ *exec.Cmd, name string) error { - if name != "" { - return errors.New("harness user switching is unsupported on this platform") - } - return nil -} - -// ownForHarness has nothing to do where the harness runs as the worker. -func ownForHarness(string, ...string) error { return nil } - -// ownTreeForHarness has nothing to do where the harness runs as the worker. -func ownTreeForHarness(string, string) error { return nil } diff --git a/internal/agentexec/harness_proc_unix.go b/internal/agentexec/harness_proc_unix.go deleted file mode 100644 index 00bfd07d2..000000000 --- a/internal/agentexec/harness_proc_unix.go +++ /dev/null @@ -1,126 +0,0 @@ -//go:build unix - -package agentexec - -import ( - "errors" - "fmt" - "io/fs" - "os" - "os/exec" - "os/user" - "path/filepath" - "strconv" - "strings" - "syscall" -) - -// setProcessGroup puts the harness in its own process group and makes -// cancellation kill the whole group. -func setProcessGroup(cmd *exec.Cmd) { - cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true} - cmd.Cancel = func() error { return syscall.Kill(-cmd.Process.Pid, syscall.SIGKILL) } -} - -// reapGroup kills whatever the command left running in its process group, so a -// daemonised child cannot outlive the invocation that started it. -func reapGroup(cmd *exec.Cmd) { - if cmd.Process != nil { - _ = syscall.Kill(-cmd.Process.Pid, syscall.SIGKILL) - } -} - -// runAsHarnessUser drops the invocation to the Kit's user. A worker running -// as root refuses to start a harness with no user, because the harness would -// then share the worker's user and could read its credentials. -func runAsHarnessUser(cmd *exec.Cmd, name string) error { - if name == "" { - if os.Geteuid() == 0 { - return errors.New("harness has no user and the worker runs as root; refusing to give the harness the worker's privileges") - } - return nil - } - cred, err := harnessCredential(name) - if err != nil { - return err - } - cmd.SysProcAttr.Credential = cred - return nil -} - -// harnessCredential resolves a user name or "uid[:gid]" to a credential. -// Root is refused whichever way it is named. IDs are bounded to 31 bits so -// they convert to int for os.Chown on every platform. -func harnessCredential(name string) (*syscall.Credential, error) { - uidText, gidText, hasGid := strings.Cut(name, ":") - uid, uidErr := strconv.ParseUint(uidText, 10, 31) - var cred syscall.Credential - if uidErr == nil { - cred.Uid, cred.Gid = uint32(uid), uint32(uid) - } else { - u, err := user.Lookup(uidText) - if err != nil { - return nil, fmt.Errorf("harness user %q: %w", uidText, err) - } - id, err := strconv.ParseUint(u.Uid, 10, 31) - if err != nil { - return nil, fmt.Errorf("harness user %q has non-numeric uid %q", uidText, u.Uid) - } - group, err := strconv.ParseUint(u.Gid, 10, 31) - if err != nil { - return nil, fmt.Errorf("harness user %q has non-numeric gid %q", uidText, u.Gid) - } - cred.Uid, cred.Gid = uint32(id), uint32(group) - } - if hasGid { - gid, err := strconv.ParseUint(gidText, 10, 31) - if err != nil { - return nil, fmt.Errorf("harness group %q is not numeric", gidText) - } - cred.Gid = uint32(gid) - } - if cred.Uid == 0 { - return nil, fmt.Errorf("harness user %q is root; a harness never runs with the worker's privileges", name) - } - return &cred, nil -} - -// ownForHarness gives paths to the harness user when the worker is root, so -// the harness's MCP server can write its captures. -func ownForHarness(name string, paths ...string) error { - if name == "" || os.Geteuid() != 0 { - return nil - } - cred, err := harnessCredential(name) - if err != nil { - return err - } - for _, p := range paths { - if err := os.Chown(p, int(cred.Uid), int(cred.Gid)); err != nil { - return err - } - } - return nil -} - -// ownTreeForHarness gives the worktree to the harness user when the worker -// is root, except .git: the harness edits files and never writes the -// repository, so its refs and config stay the worker's. -func ownTreeForHarness(name, root string) error { - if name == "" || os.Geteuid() != 0 { - return nil - } - cred, err := harnessCredential(name) - if err != nil { - return err - } - return filepath.WalkDir(root, func(path string, d fs.DirEntry, err error) error { - if err != nil { - return err - } - if d.IsDir() && d.Name() == ".git" && path != root { - return filepath.SkipDir - } - return os.Lchown(path, int(cred.Uid), int(cred.Gid)) - }) -} diff --git a/internal/agentexec/harness_runner.go b/internal/agentexec/harness_runner.go deleted file mode 100644 index 241cc0d31..000000000 --- a/internal/agentexec/harness_runner.go +++ /dev/null @@ -1,550 +0,0 @@ -package agentexec - -import ( - "bufio" - "bytes" - "context" - "crypto/sha256" - "encoding/hex" - "encoding/json" - "errors" - "fmt" - "io" - "maps" - "os" - "os/exec" - "path/filepath" - "slices" - "strings" - "time" - - git "github.com/go-git/go-git/v6" - "github.com/go-git/go-git/v6/plumbing" - - "github.com/samcharles93/archie-core/internal/domain/agentrun" -) - -// Harness outcomes beyond StatusPassed. The strings match the built-in -// loop's, so a stage reads the same whichever runner ran it. -const ( - StatusParked = "parked" - StopTimedOut = "timed_out" - StopPolicy = "policy_violation" - StopGate = "gate_failed" - StopExited = "harness_exited" -) - -const ( - promptPlaceholder = "{{.Prompt}}" - sessionPlaceholder = "{{.SessionID}}" - stderrTailBytes = 2000 - killGrace = 2 * time.Second -) - -// HarnessOutput reads one harness invocation's standard output. An adapter -// knows its CLI's stream format; it reports tool calls as they complete and -// the session to resume. -type HarnessOutput interface { - Line(line []byte, report agentrun.Reporter) - SessionID() string - Usage() agentrun.Usage -} - -// HarnessRunner runs a stage on an external coding-agent CLI. Archie's -// guarantees do not depend on the CLI obeying its prompt: the runner checks -// the worktree and the repository's refs and configuration after every -// invocation, runs the gate itself, and owns the process's lifetime. -type HarnessRunner struct{} - -// NewHarnessRunner returns a runner that reads each invocation with the -// adapter the request names. -func NewHarnessRunner() *HarnessRunner { return &HarnessRunner{} } - -func (r *HarnessRunner) Run(ctx context.Context, workspace string, req agentrun.Request, report agentrun.Reporter) (agentrun.Result, error) { - if err := validateHarness(req); err != nil { - return agentrun.Result{}, err - } - if err := ownTreeForHarness(req.Harness.User, workspace); err != nil { - return agentrun.Result{}, fmt.Errorf("give the worktree to the harness user: %w", err) - } - mcpConfig, capturesPath, cleanup, err := r.prepareCaptures(req) - if err != nil { - return agentrun.Result{}, err - } - defer cleanup() - res, err := r.run(ctx, workspace, req, mcpConfig, report) - if capturesPath != "" { - if res.Status == agentrun.StatusPassed { - if _, readyErr := os.Stat(capturesPath + ".ready"); readyErr != nil { - return park(res, StopExited, "harness did not initialize Archie MCP; check CLI MCP support and startup errors"), err - } - } - captures, readErr := readCaptures(capturesPath, req.CaptureTools) - if readErr != nil { - return res, errors.Join(err, fmt.Errorf("read captures: %w", readErr)) - } - res.Captures = captures - } - return res, err -} - -func (r *HarnessRunner) run(ctx context.Context, workspace string, req agentrun.Request, mcpConfig string, report agentrun.Reporter) (agentrun.Result, error) { - res := agentrun.Result{Version: agentrun.ProtocolVersion, TaskID: req.TaskID, Attempt: req.Attempt, Stage: req.Stage} - before, err := snapshotRepo(workspace) - if err != nil { - return agentrun.Result{}, fmt.Errorf("snapshot workspace before harness: %w", err) - } - runCtx := ctx - if req.Budget.WallClock > 0 { - var cancel context.CancelFunc - runCtx, cancel = context.WithTimeout(ctx, req.Budget.WallClock) - defer cancel() - } - - maxIterations := 1 - if len(req.Harness.Resume) > 0 || len(req.Harness.Continue) > 0 { - maxIterations = max(1, req.Gate.MaxConsecutiveFailures) - } - prompt := harnessPrompt(workspace, req) - verb := req.Harness.Prompt - session := "" - for { - res.Iterations++ - out := r.output(req.Harness.Adapter) - exitErr := invoke(runCtx, workspace, req.Harness, harnessArgv(req.Harness, verb, prompt, session, mcpConfig), out, report) - if id := out.SessionID(); id != "" { - session = id - } - res.Usage = addUsage(res.Usage, out.Usage()) - res.TokensUsed = res.Usage.TotalTokens - if ctx.Err() != nil { - return res, ctx.Err() - } - - res, stopped, err := settle(runCtx, workspace, before, req, res, exitErr) - if err != nil || stopped { - return res, err - } - - gateOutput, gateErr := runHarnessGate(runCtx, req.Harness, req.Gate.Commands, workspace) - if gateErr == nil { - res.Status = agentrun.StatusPassed - return res, nil - } - if runCtx.Err() != nil { - return park(res, StopTimedOut, "harness exceeded its wall-clock budget during the gate"), nil - } - if res.Iterations >= maxIterations { - return park(res, StopGate, gateOutput), nil - } - verb = resumeVerb(req.Harness, session) - if len(verb) == 0 { - return park(res, StopGate, gateOutput), nil - } - prompt = "The quality gate failed. Fix the cause, then stop.\n\n" + gateOutput - } -} - -// settle checks one finished invocation. Guarantees come first: a policy -// violation parks the step even when the invocation also timed out or -// failed, because what the harness did matters more than how it ended. -func settle(runCtx context.Context, workspace string, before repoState, req agentrun.Request, res agentrun.Result, exitErr error) (agentrun.Result, bool, error) { - after, err := snapshotRepo(workspace) - if err != nil { - return res, true, fmt.Errorf("snapshot workspace after harness: %w", err) - } - res.Changes = changedPaths(before, after) - // An exhausted budget is an outcome of the step, not a runner error. - outOfTime := errors.Is(runCtx.Err(), context.DeadlineExceeded) - switch violation := policyViolation(before, after, res.Changes, req); { - case violation != "": - return park(res, StopPolicy, violation), true, nil - case outOfTime: - return park(res, StopTimedOut, "harness exceeded its wall-clock budget"), true, nil - case exitErr != nil: - return park(res, StopExited, exitErr.Error()), true, nil - } - return res, false, nil -} - -func (r *HarnessRunner) output(adapter string) HarnessOutput { - if a, ok := LookupHarnessAdapter(adapter); ok { - return a.NewOutput() - } - return silentOutput{} -} - -func validateHarness(req agentrun.Request) error { - if err := req.Validate(); err != nil { - return err - } - h := req.Harness - if h == nil || len(h.Launch) == 0 { - return errors.New("harness runner needs a harness spec with a launch command") - } - if !slices.ContainsFunc(h.Prompt, func(s string) bool { return strings.Contains(s, promptPlaceholder) }) { - return fmt.Errorf("harness prompt verb must carry %s, or the mission is never passed", promptPlaceholder) - } - if _, ok := LookupHarnessAdapter(h.Adapter); h.Adapter != "" && !ok { - return fmt.Errorf("harness output adapter %q is unknown", h.Adapter) - } - if req.Gate.MaxConsecutiveFailures > 1 && len(h.Resume) == 0 && len(h.Continue) == 0 { - return errors.New("stage retries its gate but the harness declares no resume or continue verb") - } - if len(h.Resume) > 0 && !slices.ContainsFunc(h.Resume, func(s string) bool { return strings.Contains(s, sessionPlaceholder) }) { - return fmt.Errorf("harness resume verb must carry %s", sessionPlaceholder) - } - if (len(req.CaptureTools) > 0 || h.CentralTools) && !slices.ContainsFunc(h.MCPConfig, func(s string) bool { return strings.Contains(s, mcpConfigPlaceholder) }) { - return errors.New("stage needs Archie tools but the harness registers no MCP server; its results would be lost") - } - return nil -} - -// harnessArgv is the launch command, then the verb, then the prompt verb -// when the verb is a resume or continue, with placeholders substituted as -// raw values rather than shell text. -func harnessArgv(h *agentrun.HarnessSpec, verb []string, prompt, session, mcpConfig string) []string { - argv := slices.Clone(h.Launch) - tails := [][]string{verb} - if !slices.Equal(verb, h.Prompt) { - tails = append(tails, h.Prompt) - } - if mcpConfig != "" { - tails = append([][]string{h.MCPConfig}, tails...) - } - for _, tail := range tails { - for _, arg := range tail { - arg = strings.ReplaceAll(arg, promptPlaceholder, prompt) - arg = strings.ReplaceAll(arg, sessionPlaceholder, session) - arg = strings.ReplaceAll(arg, mcpConfigPlaceholder, mcpConfig) - argv = append(argv, arg) - } - } - return argv -} - -// resumeVerb continues the session the last invocation reported, falling -// back to the most recent session when the CLI reported none. It returns nil -// when neither is possible: resuming with an empty session ID would start a -// broken command. -func resumeVerb(h *agentrun.HarnessSpec, sessionID string) []string { - if len(h.Resume) > 0 && sessionID != "" { - return slices.Clone(h.Resume) - } - if len(h.Continue) > 0 { - return slices.Clone(h.Continue) - } - return nil -} - -// invoke runs one harness invocation in its own process group, feeding -// stdout to out line by line. Cancelling ctx kills the whole group, so a -// CLI's children cannot outlive the step. -func invoke(ctx context.Context, workspace string, h *agentrun.HarnessSpec, argv []string, out HarnessOutput, report agentrun.Reporter) error { - cmd := exec.CommandContext(ctx, argv[0], argv[1:]...) - cmd.Dir = workspace - cmd.WaitDelay = killGrace - // A nil Env would inherit the worker's environment and its credentials. - cmd.Env = append([]string{}, h.Env...) - setProcessGroup(cmd) - if err := runAsHarnessUser(cmd, h.User); err != nil { - return err - } - stdout, err := cmd.StdoutPipe() - if err != nil { - return err - } - var stderr tailBuffer - cmd.Stderr = io.MultiWriter(&stderr, os.Stderr) - if err := cmd.Start(); err != nil { - return fmt.Errorf("start harness: %w", err) - } - // Keep the raw harness activity visible in the container's live output. - output := io.TeeReader(stdout, os.Stdout) - scanner := bufio.NewScanner(output) - scanner.Buffer(make([]byte, 64*1024), 16*1024*1024) - for scanner.Scan() { - out.Line(scanner.Bytes(), report) - } - if err := scanner.Err(); err != nil { - // Stop the CLI rather than leave it writing into a pipe nobody parses. - _ = cmd.Cancel() - _, _ = io.Copy(io.Discard, output) - _ = cmd.Wait() - return fmt.Errorf("read harness output: %w", err) - } - waitErr := cmd.Wait() - reapGroup(cmd) - if err := waitErr; err != nil { - if ctx.Err() != nil { - return ctx.Err() - } - return fmt.Errorf("harness exited: %w: %s", err, strings.TrimSpace(stderr.String())) - } - return nil -} - -func harnessPrompt(workspace string, req agentrun.Request) string { - var b strings.Builder - b.WriteString(req.Mission) - b.WriteString("\n\n## Rules\n\n") - b.WriteString(ProjectScopedRules(workspace, req.ExtraRules)) - b.WriteString("\nDo not run git commands that change history, refs or configuration. Archie commits your changes.") - if req.ReadOnly { - b.WriteString("\nThis stage is read-only. Do not create, modify or delete any file.") - } - if patterns := slices.Concat(req.Protection.Suffixes, req.Protection.Globs); len(patterns) > 0 { - b.WriteString("\nDo not modify files matching: " + strings.Join(patterns, ", ")) - } - if len(req.Gate.Commands) > 0 { - b.WriteString("\nYour work must pass these commands:") - for _, c := range req.Gate.Commands { - b.WriteString("\n- " + strings.Join(c.Argv, " ")) - } - } - if strings.TrimSpace(req.Notes) != "" { - b.WriteString("\n\n## Notes\n\n" + req.Notes) - } - return b.String() -} - -// runHarnessGate runs the gate commands confined like the harness: they -// execute code the harness just wrote, so they get its user, its environment -// and its process group rather than the worker's. -func runHarnessGate(ctx context.Context, h *agentrun.HarnessSpec, commands []agentrun.Command, workspace string) (string, error) { - for _, c := range commands { - if len(c.Argv) == 0 { - continue - } - cmd := exec.CommandContext(ctx, c.Argv[0], c.Argv[1:]...) - cmd.Dir = workspace - cmd.WaitDelay = killGrace - cmd.Env = append([]string{}, h.Env...) - setProcessGroup(cmd) - if err := runAsHarnessUser(cmd, h.User); err != nil { - return "", err - } - output, err := cmd.CombinedOutput() - reapGroup(cmd) - switch { - case c.ExpectFailure && err == nil: - return fmt.Sprintf("[%s] expected this command to fail, but it passed:\n%s", c.Name, clipGate(output)), - fmt.Errorf("gate %s: expected failure", c.Name) - case !c.ExpectFailure && err != nil: - return fmt.Sprintf("[%s] %s\n%s", c.Name, strings.Join(c.Argv, " "), clipGate(output)), fmt.Errorf("gate %s: %w", c.Name, err) - } - } - return "", nil -} - -func clipGate(out []byte) string { - const limit = 4000 - s := strings.TrimSpace(string(out)) - if len(s) <= limit { - return s - } - return s[:limit/2] + "\n...[truncated]...\n" + s[len(s)-limit/2:] -} - -func park(res agentrun.Result, stop, detail string) agentrun.Result { - res.Status, res.StopReason, res.Detail = StatusParked, stop, detail - return res -} - -func addUsage(a, b agentrun.Usage) agentrun.Usage { - return agentrun.Usage{ - PromptTokens: a.PromptTokens + b.PromptTokens, - CompletionTokens: a.CompletionTokens + b.CompletionTokens, - TotalTokens: a.TotalTokens + b.TotalTokens, - CachedTokens: a.CachedTokens + b.CachedTokens, - CacheCreationTokens: a.CacheCreationTokens + b.CacheCreationTokens, - } -} - -// repoState is what the runner compares across an invocation: every ref -// (HEAD included), the repository configuration, and a content hash of each -// path git reports as not clean. -type repoState struct { - refs map[string]string - config string - dirty map[string]string -} - -func snapshotRepo(workspace string) (repoState, error) { - repo, err := git.PlainOpen(workspace) - if err != nil { - return repoState{}, err - } - state := repoState{refs: map[string]string{}, dirty: map[string]string{}} - if head, err := repo.Storer.Reference(plumbing.HEAD); err == nil { - state.refs[plumbing.HEAD.String()] = head.String() - } - iter, err := repo.References() - if err != nil { - return repoState{}, err - } - if err := iter.ForEach(func(ref *plumbing.Reference) error { - state.refs[ref.Name().String()] = ref.String() - return nil - }); err != nil { - return repoState{}, err - } - cfg, err := repo.Config() - if err != nil { - return repoState{}, err - } - raw, err := cfg.Marshal() - if err != nil { - return repoState{}, err - } - state.config = string(raw) - wt, err := repo.Worktree() - if err != nil { - return repoState{}, err - } - status, err := wt.Status() - if err != nil { - return repoState{}, err - } - for path, st := range status { - if st.Worktree == git.Unmodified && st.Staging == git.Unmodified { - continue - } - state.dirty[path] = contentHash(filepath.Join(workspace, path)) - } - return state, nil -} - -func contentHash(path string) string { - data, err := os.ReadFile(path) - if err != nil { - return "absent" - } - sum := sha256.Sum256(data) - return hex.EncodeToString(sum[:]) -} - -// changedPaths is every path whose state differs across the invocation. A -// path already dirty and left untouched is not a change of this run. -func changedPaths(before, after repoState) []string { - var changed []string - for path := range mergeKeys(before.dirty, after.dirty) { - if before.dirty[path] != after.dirty[path] { - changed = append(changed, path) - } - } - slices.Sort(changed) - return changed -} - -func mergeKeys(a, b map[string]string) map[string]struct{} { - keys := make(map[string]struct{}, len(a)+len(b)) - for k := range maps.Keys(a) { - keys[k] = struct{}{} - } - for k := range maps.Keys(b) { - keys[k] = struct{}{} - } - return keys -} - -func policyViolation(before, after repoState, changes []string, req agentrun.Request) string { - if !maps.Equal(before.refs, after.refs) { - var moved []string - for name := range mergeKeys(before.refs, after.refs) { - if before.refs[name] != after.refs[name] { - moved = append(moved, name) - } - } - slices.Sort(moved) - return "the harness changed git refs: " + strings.Join(moved, ", ") - } - if before.config != after.config { - return "the harness changed the git configuration" - } - if req.ReadOnly && len(changes) > 0 { - return "the harness wrote in a read-only stage: " + strings.Join(changes, ", ") - } - if protected := ProtectionMatcher(req.Protection, false); protected != nil { - var hit []string - for _, path := range changes { - if protected(path) { - hit = append(hit, path) - } - } - if len(hit) > 0 { - return "the harness modified protected paths: " + strings.Join(hit, ", ") - } - } - return "" -} - -type silentOutput struct{} - -func (silentOutput) Line([]byte, agentrun.Reporter) {} -func (silentOutput) SessionID() string { return "" } -func (silentOutput) Usage() agentrun.Usage { return agentrun.Usage{} } - -// tailBuffer keeps the last stderrTailBytes written to it. -type tailBuffer struct{ buf bytes.Buffer } - -func (t *tailBuffer) Write(p []byte) (int, error) { - t.buf.Write(p) - if extra := t.buf.Len() - stderrTailBytes; extra > 0 { - t.buf.Next(extra) - } - return len(p), nil -} - -func (t *tailBuffer) String() string { return t.buf.String() } - -// prepareCaptures writes the capture tools' spec and an MCP config pointing -// the CLI at archie's MCP server, and creates the captures file the server -// appends to. The directory belongs to the harness user, whose MCP server -// writes into it. -func (r *HarnessRunner) prepareCaptures(req agentrun.Request) (config, captures string, cleanup func(), err error) { - cleanup = func() {} - if len(req.CaptureTools) == 0 && !req.Harness.CentralTools { - return "", "", cleanup, nil - } - dir, err := os.MkdirTemp("", "archie-captures-") - if err != nil { - return "", "", cleanup, err - } - cleanup = func() { _ = os.RemoveAll(dir) } - fail := func(err error) (string, string, func(), error) { - cleanup() - return "", "", func() {}, err - } - spec, err := json.Marshal(harnessMCPSpec{Captures: req.CaptureTools, CentralTools: req.Harness.CentralTools}) - if err != nil { - return fail(err) - } - specPath, capturesPath, configPath := filepath.Join(dir, "tools.json"), filepath.Join(dir, "captures.jsonl"), filepath.Join(dir, "mcp.json") - self, err := os.Executable() - if err != nil { - return fail(err) - } - server := map[string]any{ - "env": harnessMCPEnvironment(req.Harness.Env), - "command": self, - "args": []string{"mcp", "-spec", specPath, "-captures", capturesPath}, - } - cfg, err := json.Marshal(map[string]any{"mcpServers": map[string]any{"archie": server}}) - if err != nil { - return fail(err) - } - for path, content := range map[string][]byte{specPath: spec, configPath: cfg, capturesPath: nil} { - if err := os.WriteFile(path, content, 0o600); err != nil { - return fail(err) - } - } - if err := ownForHarness(req.Harness.User, dir, specPath, configPath, capturesPath); err != nil { - return fail(err) - } - registration, err := harnessMCPRegistration(req.Harness.Adapter, dir, configPath, server) - if err != nil { - return fail(err) - } - return registration, capturesPath, cleanup, nil -} diff --git a/internal/agentexec/harness_runner_test.go b/internal/agentexec/harness_runner_test.go deleted file mode 100644 index 55826ad84..000000000 --- a/internal/agentexec/harness_runner_test.go +++ /dev/null @@ -1,41 +0,0 @@ -//go:build unix - -package agentexec - -import ( - "os" - "os/exec" - "strings" - "testing" - - "github.com/samcharles93/archie-core/internal/domain/agentrun" -) - -func TestHarnessLiveOutput(t *testing.T) { - if os.Getenv("ARCHIE_TEST_HARNESS_OUTPUT") == "1" { - adapter := &codexHarnessOutput{} - user := "" - if os.Geteuid() == 0 { - user = "65534" - } - err := invoke(t.Context(), "/tmp", &agentrun.HarnessSpec{User: user}, []string{"sh", "-c", `printf '%s\n' '{"type":"thread.started","thread_id":"live-thread"}'; printf '%s\n' 'live diagnostic' >&2`}, adapter, agentrun.Reporter{}) - if err != nil { - t.Fatal(err) - } - if adapter.SessionID() != "live-thread" { - t.Fatal("forwarding lost the adapter input") - } - return - } - cmd := exec.CommandContext(t.Context(), os.Args[0], "-test.run=^TestHarnessLiveOutput$") - cmd.Env = append(os.Environ(), "ARCHIE_TEST_HARNESS_OUTPUT=1") - got, err := cmd.CombinedOutput() - if err != nil { - t.Fatalf("harness: %v: %s", err, got) - } - for _, want := range []string{"live-thread", "live diagnostic"} { - if !strings.Contains(string(got), want) { - t.Errorf("worker output %q is missing %q", got, want) - } - } -} diff --git a/internal/agentexec/helpers.go b/internal/agentexec/helpers.go index cf80deb84..3d20661f3 100644 --- a/internal/agentexec/helpers.go +++ b/internal/agentexec/helpers.go @@ -177,3 +177,17 @@ func isNonEmptyString(object map[string]json.RawMessage, field string) bool { raw, ok := object[field] return ok && json.Unmarshal(raw, &text) == nil && strings.TrimSpace(text) != "" } + +// AcceptCapture decides whether one capture call is kept. +func AcceptCapture(spec agentrun.CaptureTool, accepted int, value json.RawMessage) (string, bool) { + if spec.MaxCalls > 0 && accepted >= spec.MaxCalls { + return fmt.Sprintf("%s rejected: maximum call count is %d", spec.Name, spec.MaxCalls), false + } + if !json.Valid(value) { + return spec.Name + " rejected: arguments are not valid JSON", false + } + if rejection, ok := ValidateCaptureArgs(spec, value); !ok { + return rejection, false + } + return spec.Name + " recorded", true +} diff --git a/internal/app/agentworker/mcp.go b/internal/app/agentworker/mcp.go deleted file mode 100644 index 0fcd86f00..000000000 --- a/internal/app/agentworker/mcp.go +++ /dev/null @@ -1,15 +0,0 @@ -package agentworker - -import ( - "context" - - "github.com/modelcontextprotocol/go-sdk/mcp" - - "github.com/samcharles93/archie-core/internal/agentexec" -) - -// RunCaptureMCP serves a stage's capture tools to its harness over stdio -// until the harness closes the connection or ctx is cancelled. -func RunCaptureMCP(ctx context.Context, specPath, capturesPath string) error { - return agentexec.ServeCaptureFiles(ctx, specPath, capturesPath, &mcp.StdioTransport{}) -} diff --git a/internal/app/agentworker/task_execution.go b/internal/app/agentworker/task_execution.go index e280ff89d..51405f9e4 100644 --- a/internal/app/agentworker/task_execution.go +++ b/internal/app/agentworker/task_execution.go @@ -184,10 +184,6 @@ func applyToolLimits(agent agentrun.Runner, policy config.ToolPolicy, allow []st // shared model runtime (the one its MCP providers answer sampling from); the // registry path reuses it rather than building a second one. func stageRunners(req taskrun.Request, mcpSet *taskmcp.Set, newRunner runnerFactory, llm *runtime.Runtime, log *slog.Logger) (agentrun.Runner, error) { - if req.Harness != nil { - // The built-in loop has no route to a model from a Kit container. - return agentexec.HarnessStages{Runner: agentexec.NewHarnessRunner(), Spec: *req.Harness}, nil - } var agent agentrun.Runner if mcpSet != nil && mcpSet.Registry != nil { agent = modelloop.NewLoopRunner(llm, log, mcpSet.Registry) @@ -345,7 +341,7 @@ func CompilePinnedWorkflow(req *taskrun.Request, steps *workflow.Manager) (workf } func taskMCPProviders(ctx context.Context, req taskrun.Request, deps taskDependencies, log *slog.Logger) (*runtime.Runtime, *taskmcp.Set) { - if req.Harness != nil || len(req.MCPServers) == 0 { + if len(req.MCPServers) == 0 { return nil, nil } llm := modelloop.NewRuntimeOver(deps.modelTransport, req.Providers, req.Cfg.ModelLimits, nil) diff --git a/internal/app/archied/bootstrap.go b/internal/app/archied/bootstrap.go index 6230d1f88..898056917 100644 --- a/internal/app/archied/bootstrap.go +++ b/internal/app/archied/bootstrap.go @@ -47,7 +47,6 @@ import ( "github.com/samcharles93/archie-core/internal/infrastructure/eventbus/nats" "github.com/samcharles93/archie-core/internal/infrastructure/extension" "github.com/samcharles93/archie-core/internal/infrastructure/issuerpc" - "github.com/samcharles93/archie-core/internal/infrastructure/kitrun" "github.com/samcharles93/archie-core/internal/infrastructure/logrpc" "github.com/samcharles93/archie-core/internal/infrastructure/messagingrpc" "github.com/samcharles93/archie-core/internal/infrastructure/modelcatalog" @@ -159,16 +158,10 @@ type boot struct { natsToken string containerPool *container.Pool - // kitLauncher starts Kit profile tasks; nil when the egress path cannot - // be built on this host. - kitLauncher *kitrun.Launcher // modelEgress gives native tasks model access through the egress proxy; // nil when the proxy cannot start. - modelEgress daemon.ModelEgress - // sessionGrants records live setup sessions' granted credential services - // for the egress proxy's resolver. - sessionGrants *sessionGrants - storeBackend storage.Backend + modelEgress daemon.ModelEgress + storeBackend storage.Backend // runtimeVersions records the version of every control-plane kind boot's // layering applied. Set once at boot before the runtime-resource watches @@ -613,7 +606,6 @@ func (b *boot) buildDaemon() { Reactions: b.reactionClient, RunCredentials: b.stateStoreGrants, ContainerPool: b.containerPool, - KitLauncher: b.kitLauncher, ModelEgress: b.modelEgress, TaskModels: b.taskModels, TaskInstructions: b.taskInstructions, @@ -752,13 +744,10 @@ func (b *boot) startServices(ctx context.Context) error { func (b *boot) setupBackends(ctx context.Context) error { closeContainers := b.setupContainers(ctx) - // Registered before the Kit launcher so its cleanups (the harness - // listener and the egress relay) run first: cleanups unwind LIFO, and a - // session teardown needs the Docker client the pool still owns. b.addCleanup(closeContainers) err := b.connectNATS(ctx) if err == nil { - b.setupKitLauncher(ctx) + b.setupModelEgress(ctx) } return err } diff --git a/internal/app/archied/daemon_rpc.go b/internal/app/archied/daemon_rpc.go new file mode 100644 index 000000000..4c5e00d33 --- /dev/null +++ b/internal/app/archied/daemon_rpc.go @@ -0,0 +1,59 @@ +package archied + +import ( + "context" + "errors" + "net" + "time" + + "google.golang.org/grpc" + + "github.com/samcharles93/archie-core/internal/app/servicekit" + "github.com/samcharles93/archie-core/internal/config" + daemonpb "github.com/samcharles93/archie-core/internal/contracts/daemon/v1" + "github.com/samcharles93/archie-core/internal/domain/access" + "github.com/samcharles93/archie-core/internal/domain/presence" + "github.com/samcharles93/archie-core/internal/infrastructure/rpcidentity" +) + +// setupDaemonRPC serves the daemon contract. Without certificates it binds +// only loopback. +func (b *boot) setupDaemonRPC(ctx context.Context) { + conn := b.cfg.Services.Get(config.ServiceNameDaemon) + listen, err := servicekit.ResolveListen(config.ServiceNameDaemon, "", conn.Listen) + if err != nil { + b.log.WarnContext(ctx, "daemon contract disabled", "err", err) + return + } + id, err := servicekit.Identity(b.cfg.TLS, presence.Daemon) + if err != nil { + b.log.WarnContext(ctx, "daemon contract disabled", "err", err) + return + } + creds, err := id.ServerOption(listen, false) + if err != nil { + b.log.WarnContext(ctx, "daemon contract disabled", "err", err) + return + } + ln, err := (&net.ListenConfig{}).Listen(ctx, "tcp", listen) + if err != nil { + b.log.WarnContext(ctx, "daemon contract disabled: listen", "err", err) + return + } + workspaces, _ := b.stateStore.(access.WorkspaceLister) + callers := rpcidentity.Callers{Principals: b.d.Principals, Workspaces: workspaces} + srv := grpc.NewServer(append(servicekit.ServerLimits(), creds, grpc.ChainUnaryInterceptor(callers.Unary()), grpc.ChainStreamInterceptor(callers.Stream()))...) + daemonpb.RegisterDaemonServiceServer(srv, &repositoryServer{boot: b}) + go func() { + if err := srv.Serve(ln); err != nil && !errors.Is(err, grpc.ErrServerStopped) { + b.log.ErrorContext(ctx, "daemon contract listener stopped", "err", err) + } + }() + b.addCleanup(func() { + timer := time.AfterFunc(10*time.Second, srv.Stop) + srv.GracefulStop() + timer.Stop() + _ = ln.Close() + }) + b.log.InfoContext(ctx, "daemon contract serving", "addr", ln.Addr().String()) +} diff --git a/internal/app/archied/harness.go b/internal/app/archied/harness.go deleted file mode 100644 index 6575b836d..000000000 --- a/internal/app/archied/harness.go +++ /dev/null @@ -1,254 +0,0 @@ -package archied - -import ( - "context" - "errors" - "io" - "log/slog" - "net" - "slices" - "sync" - "time" - - "google.golang.org/grpc" - "google.golang.org/grpc/codes" - "google.golang.org/grpc/status" - - "github.com/samcharles93/archie-core/internal/app/servicekit" - "github.com/samcharles93/archie-core/internal/config" - "github.com/samcharles93/archie-core/internal/container" - daemonpb "github.com/samcharles93/archie-core/internal/contracts/daemon/v1" - pb "github.com/samcharles93/archie-core/internal/contracts/harness/v1" - "github.com/samcharles93/archie-core/internal/domain/access" - "github.com/samcharles93/archie-core/internal/domain/org" - "github.com/samcharles93/archie-core/internal/domain/presence" - "github.com/samcharles93/archie-core/internal/infrastructure/harnesssession" - "github.com/samcharles93/archie-core/internal/infrastructure/kitrun" - "github.com/samcharles93/archie-core/internal/infrastructure/rpcidentity" -) - -// setupSessionTTL bounds a setup container's life independently of the pool's -// max-uptime reaper: a login that never finishes must not hold a container -// until the reaper's (longer) cap. -const setupSessionTTL = 30 * time.Minute - -// sessionGrants records the credential services each live setup session is -// bound for. A session's proxy token names no task, so the egress proxy's run -// resolver consults this registry: without it, the token endpoint is never -// intercepted and no OAuth token is ever captured. -type sessionGrants struct { - mu sync.RWMutex - m map[string]sessionGrant -} - -type sessionGrant struct { - org string - services []string -} - -func (g *sessionGrants) Grant(token, org string, services []string) { - g.mu.Lock() - defer g.mu.Unlock() - if g.m == nil { - g.m = map[string]sessionGrant{} - } - g.m[token] = sessionGrant{org: org, services: slices.Clone(services)} -} - -func (g *sessionGrants) Revoke(token string) { - g.mu.Lock() - defer g.mu.Unlock() - delete(g.m, token) -} - -// GrantedFor reports the org and granted services a session token carries. -func (g *sessionGrants) GrantedFor(token string) (string, []string, bool) { - g.mu.RLock() - defer g.mu.RUnlock() - grant, ok := g.m[token] - return grant.org, grant.services, ok -} - -// harnessServer serves the daemon's harness session contract. -type harnessServer struct { - pb.UnimplementedHarnessServiceServer - sessions *harnesssession.Manager - // runs serves the run target; nil answers Unavailable. - runs *runView - log *slog.Logger -} - -// Open dispatches on the first frame's target. Setup sessions open a PTY; a run -// target is a read-only view of the task's running sandbox. -func (h *harnessServer) Open(stream pb.HarnessService_OpenServer) error { - first, err := stream.Recv() - if err != nil { - return err - } - target := first.GetTarget() - if target == nil || target.GetOrg() == "" { - return status.Error(codes.InvalidArgument, "the first frame must name a session target with an org") - } - if target.GetOrg() != string(org.OrgFromContext(stream.Context())) { - return status.Error(codes.PermissionDenied, "harness session belongs to another org") - } - switch t := target.GetTarget().(type) { - case *pb.OpenTarget_Setup: - if t.Setup.GetProfile() == "" { - return status.Error(codes.InvalidArgument, "the setup target names no profile") - } - return h.openSetup(stream, target.GetOrg(), t.Setup.GetProfile(), first.GetSize()) - case *pb.OpenTarget_Run: - if t.Run.GetTaskId() <= 0 { - return status.Error(codes.InvalidArgument, "the run target names no task") - } - return h.openRun(stream, target.GetOrg(), t.Run) - default: - return status.Error(codes.InvalidArgument, "the target names no session") - } -} - -// openSetup opens one setup session and pumps frames until the shell exits or -// either side closes. Teardown runs on every return. -func (h *harnessServer) openSetup(stream pb.HarnessService_OpenServer, org, profile string, size *pb.ConsoleSize) error { - session, err := h.sessions.Open(stream.Context(), org, profile, int(size.GetRows()), int(size.GetCols())) - if err != nil { - h.log.Warn("setup session failed to open", "org", org, "profile", profile, "err", err) - // The failure rides the stream as a frame, not only as a status: the - // dashboard's terminal shows the frame's text, which is the operator's - // only honest account of why the session did not open. - return stream.Send(&pb.OpenResponse{Frame: &pb.OpenResponse_Error{Error: err.Error()}}) - } - defer func() { - _ = session.Close() - h.log.Info("setup session closed", "org", org, "profile", profile) - }() - h.log.Info("setup session opened", "org", org, "profile", profile) - - output := make(chan error, 1) - go func() { output <- h.pumpOutput(stream, session) }() - - input := make(chan error, 1) - go func() { input <- h.pumpInput(stream, session) }() - - select { - case err := <-output: - if err != nil { - return status.Error(codes.Internal, err.Error()) - } - code, codeErr := session.ExitCode(context.WithoutCancel(stream.Context())) - if codeErr != nil { - return nil - } - return stream.Send(&pb.OpenResponse{Frame: &pb.OpenResponse_ExitCode{ExitCode: int32(code)}}) - case err := <-input: - if errors.Is(err, io.EOF) { - return nil - } - return err - } -} - -// pumpOutput copies terminal output to the stream until the shell ends. -func (h *harnessServer) pumpOutput(stream pb.HarnessService_OpenServer, session *harnesssession.Session) error { - buf := make([]byte, 32<<10) - for { - n, err := session.Read(buf) - if n > 0 { - frame := &pb.OpenResponse{Frame: &pb.OpenResponse_Stdout{Stdout: slices.Clone(buf[:n])}} - if sendErr := stream.Send(frame); sendErr != nil { - return sendErr - } - } - if err != nil { - if errors.Is(err, io.EOF) { - return nil - } - return err - } - } -} - -// pumpInput copies client frames to the session until the client closes. -func (h *harnessServer) pumpInput(stream pb.HarnessService_OpenServer, session *harnesssession.Session) error { - for { - req, err := stream.Recv() - if err != nil { - return err - } - if stdin, ok := req.GetFrame().(*pb.OpenRequest_Stdin); ok { - if _, err := session.Write(stdin.Stdin); err != nil { - return err - } - } - } -} - -// setupDaemonRPC serves repository checks and optional terminal sessions. -// Without certificates it binds only loopback and does not serve terminals. -func (b *boot) setupDaemonRPC(ctx context.Context) { - conn := b.cfg.Services.Get(config.ServiceNameDaemon) - listen, err := servicekit.ResolveListen(config.ServiceNameDaemon, "", conn.Listen) - if err != nil { - b.log.WarnContext(ctx, "harness sessions disabled", "err", err) - return - } - id, err := servicekit.Identity(b.cfg.TLS, presence.Daemon) - if err != nil { - b.log.WarnContext(ctx, "harness sessions disabled", "err", err) - return - } - creds, err := id.ServerOption(listen, false) - if err != nil { - b.log.WarnContext(ctx, "harness sessions disabled", "err", err) - return - } - ln, err := (&net.ListenConfig{}).Listen(ctx, "tcp", listen) - if err != nil { - b.log.WarnContext(ctx, "harness sessions disabled: listen", "err", err) - return - } - workspaces, _ := b.stateStore.(access.WorkspaceLister) - callers := rpcidentity.Callers{Principals: b.d.Principals, Workspaces: workspaces} - srv := grpc.NewServer(append(servicekit.ServerLimits(), creds, grpc.ChainUnaryInterceptor(callers.Unary()), grpc.ChainStreamInterceptor(callers.Stream()))...) - daemonpb.RegisterDaemonServiceServer(srv, &repositoryServer{boot: b}) - // Terminal sessions still require certificates and a container launcher. - if id != nil && b.kitLauncher != nil && b.containerPool != nil { - manager := &harnesssession.Manager{Launcher: b.kitLauncher, Client: b.containerPool.Client(), Profile: func(name string) (config.AgentProfile, error) { return b.cfgHolder.Get().Containers.Profile(name) }, TTL: setupSessionTTL} - pb.RegisterHarnessServiceServer(srv, &harnessServer{sessions: manager, runs: b.runView(), log: b.log}) - } - - go func() { - if err := srv.Serve(ln); err != nil && !errors.Is(err, grpc.ErrServerStopped) { - b.log.ErrorContext(ctx, "harness listener stopped", "err", err) - } - }() - b.addCleanup(func() { - // An open session stream would hold GracefulStop until its TTL; give - // in-flight frames a moment, then stop. Mirrors the State Store's - // shutdown so a session cannot hold shutdown to the watchdog leash. - timer := time.AfterFunc(10*time.Second, srv.Stop) - srv.GracefulStop() - timer.Stop() - _ = ln.Close() - }) - b.log.InfoContext(ctx, "harness sessions enabled", "addr", ln.Addr().String()) -} - -var _ kitrun.GrantRegistry = (*sessionGrants)(nil) - -// runView wires the run target to the task store and the container pool. The -// store is the daemon's own contract client, so a task it cannot read answers -// Unavailable rather than guessing. -func (b *boot) runView() *runView { - if b.stateStore == nil || b.containerPool == nil { - return nil - } - cli := b.containerPool.Client() - return &runView{ - tasks: b.stateStore, - follow: func(ctx context.Context, taskID, attempt int64) (io.ReadCloser, error) { - return container.FollowLogs(ctx, cli, kitrun.TaskContainer(b.containerPool.Instance(), taskID, attempt), runTail) - }, - } -} diff --git a/internal/app/archied/harness_run.go b/internal/app/archied/harness_run.go deleted file mode 100644 index 02f7b79cf..000000000 --- a/internal/app/archied/harness_run.go +++ /dev/null @@ -1,97 +0,0 @@ -package archied - -import ( - "context" - "errors" - "fmt" - "io" - - "github.com/containerd/errdefs" - "google.golang.org/grpc/codes" - "google.golang.org/grpc/status" - - "github.com/samcharles93/archie-core/internal/container" - pb "github.com/samcharles93/archie-core/internal/contracts/harness/v1" - "github.com/samcharles93/archie-core/internal/domain/org" - "github.com/samcharles93/archie-core/internal/domain/workflow" -) - -// runTail is how much recent output a view opens with, in lines. -const runTail = 200 - -// taskLookup reads the task a run target names. -type taskLookup interface { - TaskByID(ctx context.Context, id int64) (*workflow.Task, error) -} - -// runView resolves a run target to its container and follows its output. It -// is read-only by construction: it holds no exec and no stdin path, so a view -// confers no authority over the container. -type runView struct { - tasks taskLookup - follow func(ctx context.Context, taskID, attempt int64) (io.ReadCloser, error) -} - -// open refuses a task the daemon does not know, one that is not running, and -// an attempt that is not the running one. Each refusal states its reason: the -// container is gone with the attempt, so there is nothing to answer -// "unavailable" about. -func (v *runView) open(ctx context.Context, target *pb.RunTarget) (io.ReadCloser, error) { - t, err := v.tasks.TaskByID(ctx, target.GetTaskId()) - if err != nil { - return nil, status.Error(codes.Unavailable, "the task store is unavailable: "+err.Error()) - } - switch { - case t == nil || t.OrgOrDefault() != string(org.OrgFromContext(ctx)): - return nil, status.Errorf(codes.NotFound, "task %d is unknown", target.GetTaskId()) - case t.Status != workflow.StatusRunning: - return nil, status.Errorf(codes.NotFound, "task %d is %s, not running; its sandbox is gone", t.ID, t.Status) - case int64(t.Attempt) != target.GetAttempt(): - return nil, status.Errorf(codes.NotFound, "attempt %d is not running; task %d is on attempt %d", target.GetAttempt(), t.ID, t.Attempt) - } - logs, err := v.follow(ctx, t.ID, target.GetAttempt()) - if err != nil { - if errdefs.IsNotFound(err) || errors.Is(err, container.ErrNotRunning) { - return nil, status.Errorf(codes.NotFound, "task %d has no running sandbox: %v", t.ID, err) - } - return nil, status.Errorf(codes.Unavailable, "the sandbox view is unavailable: %v", err) - } - return logs, nil -} - -// openRun streams a running task's sandbox output until the task's container -// ends or the client leaves. Client frames are ignored: this is the peek, not -// a takeover. -func (h *harnessServer) openRun(stream pb.HarnessService_OpenServer, orgID string, target *pb.RunTarget) error { - if h.runs == nil { - return status.Error(codes.Unavailable, "run-scoped views need the task store and the container pool") - } - logs, err := h.runs.open(org.WithOrg(stream.Context(), org.OrgID(orgID)), target) - if err != nil { - return err - } - defer func() { _ = logs.Close() }() - // An empty frame tells the client the view opened before the sandbox has - // printed anything. - if err := stream.Send(&pb.OpenResponse{Frame: &pb.OpenResponse_Stdout{}}); err != nil { - return err - } - h.log.Info("run view opened", "task", target.GetTaskId(), "attempt", target.GetAttempt()) - defer h.log.Info("run view closed", "task", target.GetTaskId(), "attempt", target.GetAttempt()) - - buf := make([]byte, 32<<10) - for { - n, err := logs.Read(buf) - if n > 0 { - if sendErr := stream.Send(&pb.OpenResponse{Frame: &pb.OpenResponse_Stdout{Stdout: append([]byte(nil), buf[:n]...)}}); sendErr != nil { - return sendErr - } - } - if err != nil { - if err == io.EOF || stream.Context().Err() != nil { - return nil - } - return status.Error(codes.Unavailable, fmt.Sprintf("the sandbox output ended: %v", err)) - } - } -} diff --git a/internal/app/archied/harness_run_test.go b/internal/app/archied/harness_run_test.go deleted file mode 100644 index 2f155033b..000000000 --- a/internal/app/archied/harness_run_test.go +++ /dev/null @@ -1,95 +0,0 @@ -package archied - -import ( - "context" - "errors" - "io" - "strings" - "testing" - - "github.com/containerd/errdefs" - "google.golang.org/grpc/codes" - "google.golang.org/grpc/status" - - pb "github.com/samcharles93/archie-core/internal/contracts/harness/v1" - "github.com/samcharles93/archie-core/internal/domain/org" - "github.com/samcharles93/archie-core/internal/domain/workflow" -) - -type fakeTasks struct { - task *workflow.Task - err error -} - -func (f fakeTasks) TaskByID(context.Context, int64) (*workflow.Task, error) { return f.task, f.err } - -func TestRunViewOpen(t *testing.T) { - running := &workflow.Task{ID: 7, Status: workflow.StatusRunning, Attempt: 2} - cases := []struct { - name string - tasks fakeTasks - attempt int64 - follow error - code codes.Code - reason string - }{ - {"running attempt", fakeTasks{task: running}, 2, nil, codes.OK, ""}, - {"unknown task", fakeTasks{}, 2, nil, codes.NotFound, "unknown"}, - {"finished task", fakeTasks{task: &workflow.Task{ID: 7, Status: workflow.StatusParked, Attempt: 2}}, 2, nil, codes.NotFound, "not running"}, - {"earlier attempt", fakeTasks{task: running}, 1, nil, codes.NotFound, "attempt 1 is not running"}, - {"container gone", fakeTasks{task: running}, 2, errdefs.ErrNotFound, codes.NotFound, "no running sandbox"}, - {"store down", fakeTasks{err: errors.New("dial")}, 2, nil, codes.Unavailable, "unavailable"}, - {"other org", fakeTasks{task: &workflow.Task{ID: 7, Org: "other", Status: workflow.StatusRunning, Attempt: 2}}, 2, nil, codes.NotFound, "unknown"}, - {"docker down", fakeTasks{task: running}, 2, errors.New("connection refused"), codes.Unavailable, "unavailable"}, - } - for _, tc := range cases { - t.Run(tc.name, func(t *testing.T) { - v := &runView{tasks: tc.tasks, follow: func(context.Context, int64, int64) (io.ReadCloser, error) { - if tc.follow != nil { - return nil, tc.follow - } - return io.NopCloser(strings.NewReader("")), nil - }} - rc, err := v.open(org.WithOrg(t.Context(), org.DefaultOrgID), &pb.RunTarget{TaskId: 7, Attempt: tc.attempt}) - if got := status.Code(err); got != tc.code { - t.Fatalf("code = %v (%v), want %v", got, err, tc.code) - } - if err != nil && !strings.Contains(err.Error(), tc.reason) { - t.Fatalf("reason %q does not state %q", err, tc.reason) - } - if rc != nil { - _ = rc.Close() - } - }) - } -} - -type harnessOpenStream struct { - pb.HarnessService_OpenServer - ctx context.Context //nolint:containedctx // gRPC streams expose their context through Context(). - target *pb.OpenTarget -} - -func (s harnessOpenStream) Context() context.Context { return s.ctx } -func (s harnessOpenStream) Recv() (*pb.OpenRequest, error) { - return &pb.OpenRequest{Frame: &pb.OpenRequest_Target{Target: s.target}}, nil -} - -func TestHarnessSessionCallerOrg(t *testing.T) { - for _, tt := range []struct { - name string - caller org.OrgID - target string - }{ - {"tenant cannot open another tenant", "acme", "victim"}, - {"system caller cannot name another tenant", org.DefaultOrgID, "victim"}, - {"unattributed caller cannot name a tenant", "", "victim"}, - } { - t.Run(tt.name, func(t *testing.T) { - stream := harnessOpenStream{ctx: org.WithOrg(t.Context(), tt.caller), target: &pb.OpenTarget{Org: tt.target, Target: &pb.OpenTarget_Setup{Setup: &pb.SetupTarget{Profile: "kit"}}}} - if err := (&harnessServer{}).Open(stream); status.Code(err) != codes.PermissionDenied { - t.Fatalf("error %v, want PermissionDenied", err) - } - }) - } -} diff --git a/internal/app/archied/kit_launcher.go b/internal/app/archied/kit_launcher.go deleted file mode 100644 index 9cccc1cd7..000000000 --- a/internal/app/archied/kit_launcher.go +++ /dev/null @@ -1,135 +0,0 @@ -package archied - -import ( - "context" - "errors" - "log/slog" - "net" - "net/http" - "os" - "path/filepath" - "strings" - "time" - - "github.com/docker/sandbox-kit-spec/v3/fetch" - - "github.com/samcharles93/archie-core/internal/app/servicekit" - "github.com/samcharles93/archie-core/internal/config" - "github.com/samcharles93/archie-core/internal/domain/presence" - "github.com/samcharles93/archie-core/internal/events" - "github.com/samcharles93/archie-core/internal/infrastructure/egress" - "github.com/samcharles93/archie-core/internal/infrastructure/kitrun" - "github.com/samcharles93/archie-core/internal/infrastructure/tasktools" -) - -// setupKitLauncher builds the egress path Kit tasks run on: the per-daemon -// CA, the proxy on the worker bridge's host gateway, and the relay. It -// degrades to no launcher, which parks Kit tasks, rather than failing boot: -// image profiles do not need it. -func (b *boot) setupKitLauncher(ctx context.Context) { - pool, log := b.containerPool, b.log - if pool == nil || pool.HostGateway() == "" { - log.InfoContext(ctx, "kit harness runs disabled: they need the managed container pool on a bridge with a host gateway") - return - } - exe, err := os.Executable() - if err != nil { - log.WarnContext(ctx, "kit harness runs disabled", "err", err) - return - } - agentBinary := filepath.Join(filepath.Dir(exe), "archie-agent") - caDir := filepath.Join(b.cfg.StateDir, "egress") - ca, err := egress.LoadOrCreateCA(caDir) - if err != nil { - log.WarnContext(ctx, "kit harness runs disabled: egress CA", "err", err) - return - } - ln, err := (&net.ListenConfig{}).Listen(ctx, "tcp", net.JoinHostPort(pool.HostGateway(), "0")) - if err != nil { - log.WarnContext(ctx, "kit harness runs disabled: egress proxy listener", "err", err) - return - } - endpoints := kitrun.Endpoints{NATS: b.natsURL, StateStore: strings.TrimSpace(b.cfg.Services.Get(config.ServiceNameState).Target)} - cmd, err := kitrun.RelayCommand(ln.Addr().String(), endpoints) - if err != nil { - _ = ln.Close() - log.WarnContext(ctx, "kit harness runs disabled: egress relay", "err", err) - return - } - // The proxy resolves each credential from the run credential the - // container logs in with, against the live grants and bindings; a setup - // session's token resolves through the session grants instead. - b.sessionGrants = &sessionGrants{} - resolver := runCredentialResolver{runs: b.stateStoreGrants.Client, sessions: b.sessionGrants, models: b.taskModels, config: b.cfgHolder, secrets: b.secrets, credentials: b.credentialWatch} - // Without a harness secret store, Register refuses a Kit with a - // required OAuth credential. - oauthStore, _ := b.stateStore.(egress.OAuthStore) - gateway, cleanup, err := b.connectTaskTools() - if err != nil { - _ = ln.Close() - log.WarnContext(ctx, "kit task tools unavailable", "err", err) - return - } - b.addCleanup(cleanup) - proxy := egress.NewProxy(ca, egress.ProxyOptions{ - Resolver: resolver, OAuthStore: oauthStore, TaskTools: gateway, - // A test run's recorded deliveries land on the task's timeline through - // the same bus every other event uses. - Events: func(event events.Event) { //nolint:contextcheck // the egress callback carries no request context - if b.bus != nil { - b.bus.Publish(context.Background(), event) - } - }, - }) - srv := &http.Server{Handler: proxy, ReadHeaderTimeout: 30 * time.Second} - go func() { - if err := srv.Serve(ln); err != nil && !errors.Is(err, http.ErrServerClosed) { - log.ErrorContext(ctx, "egress proxy stopped", "err", err) - } - }() - // Native tasks need only the proxy for model access, so it serves them - // even when a Kit-only piece below fails. - b.modelEgress = modelEgress{proxy: proxy, addr: ln.Addr().String(), caFile: egress.CACertPath(caDir), config: b.cfgHolder, secrets: b.secrets} - fetcher, err := fetch.New(fetch.WithDockerCredentials()) - if err != nil { - b.addCleanup(func() { _ = srv.Close() }) - log.WarnContext(ctx, "kit harness runs disabled: kit registry client", "err", err) - return - } - networks := egress.NewNetworks(pool.Client(), egress.RelaySpec{ - Name: b.cfg.Instance + "-egress-relay", Image: b.cfg.Containers.Image, - Entrypoint: []string{"archie-agent"}, Cmd: cmd, Network: pool.NetworkName(), - }) - b.addCleanup(shutdownEgress(networks, srv, log)) - kitLauncher := &kitrun.Launcher{ - Pool: pool, Fetch: fetcher, Proxy: proxy, Networks: networks, - AgentBinary: agentBinary, CAFile: egress.CACertPath(caDir), - Config: b.cfgHolder, Secrets: b.secrets, OAuth: oauthStore, - Grants: b.sessionGrants, - } - b.kitLauncher = kitLauncher - log.InfoContext(ctx, "kit harness runs enabled", "proxy", ln.Addr().String()) -} - -// shutdownEgress removes the relay and stops the proxy. It derives its own -// budget because the boot context is cancelled by the time shutdown runs. -// -//nolint:contextcheck -func shutdownEgress(networks *egress.Networks, srv *http.Server, log *slog.Logger) func() { - return func() { - stopCtx, cancel := context.WithTimeout(context.Background(), 10*time.Second) - defer cancel() - if err := networks.Close(stopCtx); err != nil { - log.Warn("egress relay removal failed", "err", err) - } - _ = srv.Close() - } -} - -func (b *boot) connectTaskTools() (*tasktools.Client, func(), error) { - id, err := servicekit.Identity(b.cfg.TLS, presence.Daemon) - if err != nil { - return nil, nil, err - } - return tasktools.Dial(b.cfg.Services.Get(config.ServiceNameGateway).Target, id) -} diff --git a/internal/app/archied/model_egress.go b/internal/app/archied/model_egress.go index 95612307a..5229c1ab6 100644 --- a/internal/app/archied/model_egress.go +++ b/internal/app/archied/model_egress.go @@ -7,8 +7,6 @@ import ( "slices" "strings" - "github.com/docker/sandbox-kit-spec/v3/spec" - "github.com/samcharles93/archie-core/internal/config" "github.com/samcharles93/archie-core/internal/daemon" "github.com/samcharles93/archie-core/internal/infrastructure/egress" @@ -67,18 +65,9 @@ func (m modelEgress) Open(token, org string, providers map[string]config.Provide hosts = append(hosts, host) env = append(env, keyed[name]+"="+egress.SentinelFor(name)) } - // A model session never records, whatever its task is: this is the one - // session that must carry provider traffic for real, and it carries nothing - // else because its network policy allows only provider hosts. A test run's - // recordings happen on the Kit session that carries the rest of the run. - session, err := m.proxy.Register(egress.SessionOptions{ - Token: token, Org: org, Substitutions: subs, Secrets: secrets, - Network: &spec.PhasedNetwork{Runtime: &spec.NetworkRules{Allow: hosts}}, - }) - if err != nil { + if _, err := m.proxy.Register(egress.SessionOptions{Token: token, Allow: hosts, Substitutions: subs, Secrets: secrets}); err != nil { return daemon.ModelSession{}, err } - session.EnterRuntime() return daemon.ModelSession{ Env: append(env, egress.ModelProxyEnvFor(m.addr, token, hosts, modelProxyCAPath)...), Mounts: []storage.Mount{{Type: storage.MountTypeBind, Source: m.caFile, Destination: modelProxyCAPath, Shared: true}}, @@ -108,4 +97,4 @@ func (m modelEgress) providerKey(name string, p config.Provider, org string, bou return value, nil } -func (m modelEgress) Close(ctx context.Context, token string) { m.proxy.Revoke(ctx, token) } +func (m modelEgress) Close(_ context.Context, token string) { m.proxy.Revoke(token) } diff --git a/internal/app/archied/model_egress_setup.go b/internal/app/archied/model_egress_setup.go new file mode 100644 index 000000000..b1bd3f76e --- /dev/null +++ b/internal/app/archied/model_egress_setup.go @@ -0,0 +1,46 @@ +package archied + +import ( + "context" + "errors" + "net" + "net/http" + "path/filepath" + "time" + + "github.com/samcharles93/archie-core/internal/infrastructure/egress" +) + +// setupModelEgress starts the egress proxy task containers send their model +// calls through, on the worker bridge's host gateway. It degrades to no +// proxy rather than failing boot; a task that needs a keyed provider then +// reports why it cannot start. +func (b *boot) setupModelEgress(ctx context.Context) { + pool, log := b.containerPool, b.log + if pool == nil || pool.HostGateway() == "" { + log.InfoContext(ctx, "model egress disabled: it needs the managed container pool on a bridge with a host gateway") + return + } + caDir := filepath.Join(b.cfg.StateDir, "egress") + ca, err := egress.LoadOrCreateCA(caDir) + if err != nil { + log.WarnContext(ctx, "model egress disabled: egress CA", "err", err) + return + } + ln, err := (&net.ListenConfig{}).Listen(ctx, "tcp", net.JoinHostPort(pool.HostGateway(), "0")) + if err != nil { + log.WarnContext(ctx, "model egress disabled: egress proxy listener", "err", err) + return + } + resolver := runCredentialResolver{runs: b.stateStoreGrants.Client, models: b.taskModels, config: b.cfgHolder, secrets: b.secrets, credentials: b.credentialWatch} + proxy := egress.NewProxy(ca, egress.ProxyOptions{Resolver: resolver}) + srv := &http.Server{Handler: proxy, ReadHeaderTimeout: 30 * time.Second} + go func() { + if err := srv.Serve(ln); err != nil && !errors.Is(err, http.ErrServerClosed) { + log.ErrorContext(ctx, "egress proxy stopped", "err", err) + } + }() + b.addCleanup(func() { _ = srv.Close() }) + b.modelEgress = modelEgress{proxy: proxy, addr: ln.Addr().String(), caFile: egress.CACertPath(caDir), config: b.cfgHolder, secrets: b.secrets} + log.InfoContext(ctx, "model egress enabled", "proxy", ln.Addr().String()) +} diff --git a/internal/app/archied/run_credential_resolver.go b/internal/app/archied/run_credential_resolver.go index 1577afef3..54f70fada 100644 --- a/internal/app/archied/run_credential_resolver.go +++ b/internal/app/archied/run_credential_resolver.go @@ -10,22 +10,14 @@ import ( "github.com/samcharles93/archie-core/internal/infrastructure/egress" ) -// runCredentialResolver is the egress proxy's Resolver: it resolves a Kit -// credential for the run a run credential names, when the request is made, -// from the live grants and bindings. A revoked credential, a grant removed -// mid-run or a binding in another org stops resolving at once. -// -// A setup session's token names no task, so the resolver consults the session -// grants first: the session is bound for the org's bindings of the Kit's -// declared services, which is the intersection the operator's update-on-secret -// authorisation covers. +// runCredentialResolver is the egress proxy's Resolver: it resolves a key +// for the run a run credential names, when the request is made, from the live +// grants and bindings. A revoked credential, a grant removed mid-run or a +// binding in another org stops resolving at once. type runCredentialResolver struct { runs interface { TaskForCredential(ctx context.Context, token string) (*workflow.Task, error) } - sessions interface { - GrantedFor(token string) (org string, services []string, ok bool) - } // models answers a task's org's own providers. Nil grants only the // instance providers. models daemon.TaskModels @@ -38,11 +30,6 @@ type runCredentialResolver struct { } func (r runCredentialResolver) Resolve(ctx context.Context, credential, service string) (string, error) { - if r.sessions != nil { - if org, services, ok := r.sessions.GrantedFor(credential); ok { - return r.resolveBinding(ctx, org, services, service) - } - } task, err := r.runs.TaskForCredential(ctx, credential) if err != nil { return "", egress.ErrUnbound @@ -58,19 +45,15 @@ func (r runCredentialResolver) Resolve(ctx context.Context, credential, service return r.resolveBinding(ctx, org, granted, service) } -// resolveBinding resolves service from the org's binding when the run or -// session is granted it. An OAuth binding resolves to the empty string: the -// proxy supplies the org's token set itself. +// resolveBinding resolves service from the org's binding when the run is +// granted it. func (r runCredentialResolver) resolveBinding(ctx context.Context, org string, granted []string, service string) (string, error) { cfg := r.config.Get() binding, ok := cfg.Containers.BoundCredentials(org, granted, []string{service})[service] if !ok { return "", egress.ErrUnbound } - if binding.Secret == (config.SecretRef{}) { - return "", nil - } - if r.secrets == nil { + if binding.Secret == (config.SecretRef{}) || r.secrets == nil { return "", egress.ErrUnbound } value, err := r.secrets.Resolve(binding.Secret) diff --git a/internal/app/archied/run_credential_resolver_test.go b/internal/app/archied/run_credential_resolver_test.go index 5ec7117ca..aff5a98e8 100644 --- a/internal/app/archied/run_credential_resolver_test.go +++ b/internal/app/archied/run_credential_resolver_test.go @@ -75,47 +75,3 @@ func TestEgressResolvesOnlyGrantedCredentials(t *testing.T) { }) } } - -// TestEgressResolvesASessionGrant pins that a setup session's token, which -// names no task, resolves only the granted services of its own org. A grant -// that widened to another org's binding would hand the container a credential -// the operator's update-on-secret authorisation never covered. -func TestEgressResolvesASessionGrant(t *testing.T) { - cfg := config.Config{Containers: config.ContainerConfig{Credentials: []config.CredentialBinding{ - {Service: "github", Org: "acme", Secret: config.SecretRef{Engine: "env", Key: "ACME_GH"}}, - {Service: "linear", Org: "acme"}, - }}} - sessions := &sessionGrants{} - sessions.Grant("acme-session", "acme", []string{"github", "linear"}) - sessions.Grant("other-session", "other", []string{"github"}) - r := runCredentialResolver{ - runs: runsByToken{"acme-run": {ID: 1, Identity: "acme-bot", Org: "acme"}}, - sessions: sessions, - config: config.NewHolder(cfg), - secrets: secretValues{"ACME_GH": "acme-gh"}, - } - tests := []struct { - name, credential, service, want string - bound bool - }{ - {name: "a bound api key resolves", credential: "acme-session", service: "github", want: "acme-gh", bound: true}, - {name: "a bound oauth service resolves to the proxy's own token set", credential: "acme-session", service: "linear", bound: true}, - {name: "a service outside the grant is unbound", credential: "acme-session", service: "nope"}, - {name: "another org's binding is unbound", credential: "other-session", service: "github"}, - {name: "an unknown token falls through to the task path", credential: "acme-run", service: "github"}, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - got, err := r.Resolve(context.Background(), tt.credential, tt.service) - if !tt.bound { - if !errors.Is(err, egress.ErrUnbound) { - t.Fatalf("got %q, %v; want ErrUnbound", got, err) - } - return - } - if err != nil || got != tt.want { - t.Fatalf("got %q, %v; want %q", got, err, tt.want) - } - }) - } -} diff --git a/internal/app/archiegateway/gateway.go b/internal/app/archiegateway/gateway.go index 035b97476..5a9f53b0d 100644 --- a/internal/app/archiegateway/gateway.go +++ b/internal/app/archiegateway/gateway.go @@ -8,7 +8,6 @@ import ( "google.golang.org/grpc" "github.com/samcharles93/archie-core/internal/app/servicekit" - pb "github.com/samcharles93/archie-core/internal/contracts/gateway/v1" "github.com/samcharles93/archie-core/internal/domain/access" "github.com/samcharles93/archie-core/internal/domain/messaging" "github.com/samcharles93/archie-core/internal/events" @@ -31,9 +30,7 @@ func (b *server) serveGatewayListener(ctx context.Context, listener net.Listener // begins accepting the moment it is called, and a unit whose // TimeoutStartSec expires first would restart a healthy process. b.announceReady() - taskTools := &taskToolsServer{gateway: b, authority: b.stateStore, configFor: b.taskToolsConfig, sessions: make(map[[32]byte]*taskToolSession)} - defer taskTools.Close(ctx) - return serveGateway(ctx, listener, contract, b.chatSessionStore, b.chatCatalog(), opts, taskTools) + return serveGateway(ctx, listener, contract, b.chatSessionStore, b.chatCatalog(), opts) } func (b *server) startGatewayRuntime(ctx context.Context, actor gateway.ChatTaskActor) (gateway.ChatContract, error) { @@ -101,10 +98,9 @@ func gatewayServerOpts(listen string, id *rpctls.Identity, principals access.Pri }, nil } -func serveGateway(ctx context.Context, listener net.Listener, contract gateway.ChatContract, sessions gateway.SessionStore, catalog gatewayrpc.Catalog, opts []grpc.ServerOption, taskTools pb.TaskToolsServiceServer) error { +func serveGateway(ctx context.Context, listener net.Listener, contract gateway.ChatContract, sessions gateway.SessionStore, catalog gatewayrpc.Catalog, opts []grpc.ServerOption) error { server := grpc.NewServer(append(servicekit.ServerLimits(), opts...)...) gatewayrpc.RegisterServer(server, contract, sessions, catalog) - pb.RegisterTaskToolsServiceServer(server, taskTools) // Serve's bounded drain returns before the session store and tools close. return servicekit.Serve(ctx, server, listener) } diff --git a/internal/app/archiegateway/task_tools.go b/internal/app/archiegateway/task_tools.go deleted file mode 100644 index 245ab59a3..000000000 --- a/internal/app/archiegateway/task_tools.go +++ /dev/null @@ -1,346 +0,0 @@ -package archiegateway - -import ( - "context" - "crypto/sha256" - "encoding/json" - "errors" - "fmt" - "maps" - "path/filepath" - "slices" - "sync" - "time" - - "github.com/modelcontextprotocol/go-sdk/mcp" - "github.com/samcharles93/ai-sdk/runtime" - "google.golang.org/grpc/codes" - "google.golang.org/grpc/status" - - "github.com/samcharles93/archie-core/internal/agentexec" - "github.com/samcharles93/archie-core/internal/agentexec/modelloop" - "github.com/samcharles93/archie-core/internal/config" - pb "github.com/samcharles93/archie-core/internal/contracts/gateway/v1" - "github.com/samcharles93/archie-core/internal/domain/access" - "github.com/samcharles93/archie-core/internal/domain/identity" - "github.com/samcharles93/archie-core/internal/domain/org" - "github.com/samcharles93/archie-core/internal/domain/storecontract" - "github.com/samcharles93/archie-core/internal/domain/storepkg" - "github.com/samcharles93/archie-core/internal/domain/usage" - "github.com/samcharles93/archie-core/internal/domain/workflow" - "github.com/samcharles93/archie-core/internal/domain/workflow/task" - "github.com/samcharles93/archie-core/internal/infrastructure/taskmcp" - "github.com/samcharles93/archie-core/internal/tools" - protocol "github.com/samcharles93/archie-core/internal/tools/mcp" -) - -type taskToolSession struct { - mu sync.Mutex - lifetime context.Context //nolint:containedctx // Providers belong to the run, not one RPC. - providers *taskmcp.Set - configHash [32]byte - cancel context.CancelFunc -} - -type taskToolAuthority interface { - TaskPrincipalForCredential(context.Context, string) (*task.Task, identity.IdentityID, error) - PrincipalFor(context.Context, identity.IdentityID) (access.Principal, error) - GetInstalled(context.Context, string) (storepkg.Installed, error) - TaskModels(context.Context, int64) (config.OrgModels, error) - RecordUsage(context.Context, usage.Record) error -} - -type taskToolsServer struct { - pb.UnimplementedTaskToolsServiceServer - gateway *server - authority taskToolAuthority - configFor func(context.Context) (config.Config, error) - mu sync.Mutex - sessions map[[32]byte]*taskToolSession -} - -func (s *taskToolsServer) authorize(ctx context.Context, credential string) (context.Context, *task.Task, config.Config, []string, error) { - if access.SourceFromContext(ctx) != "archied" { - return nil, nil, config.Config{}, nil, status.Error(codes.PermissionDenied, "task tools require the daemon") - } - if credential == "" { - return nil, nil, config.Config{}, nil, status.Error(codes.Unauthenticated, "task grant required") - } - t, principal, err := s.authority.TaskPrincipalForCredential(ctx, credential) - if err != nil { - s.close(ctx, credential) - code := codes.Unauthenticated - if errors.Is(err, storecontract.ErrUnavailable) { - code = codes.Unavailable - } - return nil, nil, config.Config{}, nil, status.Error(code, "task grant unavailable") - } - if t.Status != "running" { - s.close(ctx, credential) - return nil, nil, config.Config{}, nil, status.Error(codes.PermissionDenied, "task is not running") - } - p, err := s.authority.PrincipalFor(ctx, principal) - if err != nil || string(p.Org) != t.OrgOrDefault() { - return nil, nil, config.Config{}, nil, status.Error(codes.PermissionDenied, "task identity unavailable") - } - ctx = org.WithScope(org.WithOrg(access.WithPrincipal(access.WithActor(ctx, string(principal)), p), p.Org), p.Org) - iface, err := task.ParseWorkflowInterface(t.WorkflowDefinitionYAML) - if err != nil { - return nil, nil, config.Config{}, nil, status.Error(codes.FailedPrecondition, "task workflow unavailable") - } - cfg, err := s.configFor(ctx) - if err != nil { - return nil, nil, config.Config{}, nil, status.Error(codes.Unavailable, "task tool settings unavailable") - } - profile, err := cfg.Containers.Profile(iface.Profile) - if err != nil || profile.Kit == "" { - return nil, nil, config.Config{}, nil, status.Error(codes.FailedPrecondition, "task has no Kit profile") - } - packaged := workflow.PackageOf(t.WorkflowDefinitionYAML) - var accepted []string - if packaged != "" { - installed, err := s.authority.GetInstalled(ctx, packaged) - if err == nil && installed.AcceptedAuthority != nil { - accepted = installed.AcceptedAuthority.Tools - } - } - return ctx, t, cfg, storepkg.AllowedTools(profile.Tools, packaged != "", accepted), nil -} - -func taskToolAllowed(entry tools.ToolEntry, allowed []string) bool { - return entry.Toolset != "workspace" && entry.Name != "send_file" && !entry.Classification.IsApprovalRequired() && (allowed == nil || slices.Contains(allowed, entry.Name)) -} - -func (s *taskToolsServer) entries(ctx context.Context, t *task.Task, cfg config.Config, credential string, allowed []string) ([]tools.ToolEntry, error) { - entries := []tools.ToolEntry{} - if allowed != nil && len(allowed) == 0 { - s.close(ctx, credential) - return entries, nil - } - for _, entry := range s.gateway.toolReg.All() { - // Task MCP providers have their own sampling identity and lifecycle. - if entry.Toolset != "mcp" && taskToolAllowed(entry, allowed) { - entries = append(entries, entry) - } - } - providers, err := s.providers(ctx, t, cfg, credential) - if err != nil { - return nil, err - } - if providers != nil { - for _, entry := range providers.Registry.All() { - if taskToolAllowed(entry, allowed) { - entries = append(entries, entry) - } - } - } - return entries, nil -} - -func (s *taskToolsServer) providers(ctx context.Context, t *task.Task, cfg config.Config, credential string) (*taskmcp.Set, error) { - if len(cfg.Tools.MCPServers) == 0 { - s.close(ctx, credential) - return nil, nil - } - models, err := s.authority.TaskModels(ctx, t.ID) - if err != nil { - return nil, fmt.Errorf("task sampling model: %w", err) - } - own := cfg.Providers - cfg.Providers = maps.Clone(s.gateway.cfgHolder.Get().Providers) - cfg = cfg.WithOrgModels(models) - keys := map[string]string{} - for name := range models.Providers { - provider, ok := own[name] - if !ok { - return nil, fmt.Errorf("task provider settings unavailable") - } - if provider.APIKey != (config.SecretRef{}) { - value, err := s.gateway.secrets.Resolve(provider.APIKey) - if err != nil || value == "" { - return nil, fmt.Errorf("task provider credential unavailable") - } - keys[name] = value - } - } - encoded, err := json.Marshal(struct { - Servers []config.MCPServer - Models map[string]string - Providers map[string]config.Provider - Keys map[string]string - Limits map[string]config.ModelLimits - }{cfg.Tools.MCPServers, cfg.Models, cfg.Providers, keys, cfg.ModelLimits}) - if err != nil { - return nil, err - } - return s.loadProviders(ctx, t, cfg, credential, keys, sha256.Sum256(encoded)) -} - -func (s *taskToolsServer) loadProviders(ctx context.Context, t *task.Task, cfg config.Config, credential string, keys map[string]string, hash [32]byte) (*taskmcp.Set, error) { - key := sha256.Sum256([]byte(credential)) - s.mu.Lock() - current := s.sessions[key] - if current == nil || current.lifetime.Err() != nil { - lifetime, cancel := context.WithCancel(context.WithoutCancel(ctx)) - current = &taskToolSession{lifetime: lifetime, cancel: cancel} - s.sessions[key] = current - } - s.mu.Unlock() - current.mu.Lock() - defer current.mu.Unlock() - if err := current.lifetime.Err(); err != nil { - return nil, err - } - if current.providers != nil { - if current.configHash == hash { - return current.providers, nil - } - current.providers.Close(context.WithoutCancel(ctx), s.gateway.log) - current.providers = nil - } - // Cancel an unfinished startup with its RPC; a ready provider stays run-scoped. - stop := context.AfterFunc(ctx, current.cancel) - defer stop() - llm := modelloop.NewRuntimeWithKeys(agentexec.ProvidersFromConfig(cfg.Providers), cfg.ModelLimits, keys) - cacheDir := "" - if cfg.WorkDir != "" { - cacheDir = filepath.Join(cfg.WorkDir, "mcp-npm-cache") - } - providers, err := taskmcp.Start(current.lifetime, cfg.Tools.MCPServers, s.taskSampling(llm, cfg, t, credential), s.gateway.log, cacheDir) //nolint:contextcheck // The run owns provider process lifetime. - if err != nil || ctx.Err() != nil { - current.cancel() - if providers != nil { - providers.Close(context.WithoutCancel(ctx), s.gateway.log) - } - return nil, errors.Join(err, ctx.Err()) - } - current.providers, current.configHash = providers, hash - return providers, nil -} - -func (s *taskToolsServer) ListTaskTools(ctx context.Context, req *pb.ListTaskToolsRequest) (*pb.ListTaskToolsResponse, error) { - ctx, t, cfg, allowed, err := s.authorize(ctx, req.RunCredential) - if err != nil { - return nil, err - } - entries, err := s.entries(ctx, t, cfg, req.RunCredential, allowed) - if err != nil { - return nil, status.Error(codes.Unavailable, "task tool providers unavailable") - } - set, err := modelloop.BuildToolSetFrom(entries, modelloop.ToolSetOptions{}) - if err != nil { - return nil, status.Error(codes.Unavailable, "task tool discovery failed") - } - result := []*mcp.Tool{} - for _, entry := range entries { - tool, ok := set[entry.Name] - if !ok { - continue - } - result = append(result, &mcp.Tool{Name: tool.Name, Description: tool.Description, InputSchema: tool.Parameters}) - } - data, err := json.Marshal(result) - if err != nil { - return nil, status.Error(codes.Internal, "encode task tools") - } - return &pb.ListTaskToolsResponse{ToolsJson: data}, nil -} - -func (s *taskToolsServer) CallTaskTool(ctx context.Context, req *pb.CallTaskToolRequest) (*pb.CallTaskToolResponse, error) { - ctx, t, cfg, allowed, err := s.authorize(ctx, req.RunCredential) - if err != nil { - return nil, err - } - entries, err := s.entries(ctx, t, cfg, req.RunCredential, allowed) - if err != nil { - return nil, status.Error(codes.Unavailable, "task tool providers unavailable") - } - entry, ok := findTaskTool(entries, req.Name) - if !ok { - return nil, status.Error(codes.PermissionDenied, "tool is not allowed for this task") - } - result, err := executeTaskTool(ctx, entry, req.ArgumentsJson, cfg.Tools.Policy.MaxResultChars) - if err != nil { - return nil, status.Error(codes.InvalidArgument, "task tool call failed: "+err.Error()) - } - data, err := json.Marshal(result) - if err != nil { - return nil, status.Error(codes.Internal, "encode task tool result") - } - return &pb.CallTaskToolResponse{ResultJson: data}, nil -} - -func findTaskTool(entries []tools.ToolEntry, name string) (tools.ToolEntry, bool) { - for _, entry := range entries { - if entry.Name == name { - return entry, true - } - } - return tools.ToolEntry{}, false -} - -func (s *taskToolsServer) CloseTaskTools(ctx context.Context, req *pb.CloseTaskToolsRequest) (*pb.CloseTaskToolsResponse, error) { - if access.SourceFromContext(ctx) != "archied" { - return nil, status.Error(codes.PermissionDenied, "task tools require the daemon") - } - s.close(ctx, req.RunCredential) - return &pb.CloseTaskToolsResponse{}, nil -} - -func (s *taskToolsServer) close(parent context.Context, credential string) { - key := sha256.Sum256([]byte(credential)) - s.mu.Lock() - current := s.sessions[key] - delete(s.sessions, key) - s.mu.Unlock() - if current == nil { - return - } - current.cancel() - ctx, cancel := context.WithTimeout(context.WithoutCancel(parent), 10*time.Second) - defer cancel() - current.mu.Lock() - defer current.mu.Unlock() - if current.providers != nil { - current.providers.Close(ctx, s.gateway.log) - } -} - -func (s *taskToolsServer) Close(parent context.Context) { - s.mu.Lock() - sessions := s.sessions - s.sessions = make(map[[32]byte]*taskToolSession) - s.mu.Unlock() - for _, session := range sessions { - session.cancel() - ctx, cancel := context.WithTimeout(context.WithoutCancel(parent), 10*time.Second) - session.mu.Lock() - if session.providers != nil { - session.providers.Close(ctx, s.gateway.log) - } - session.mu.Unlock() - cancel() - } -} - -func (b *server) taskToolsConfig(ctx context.Context) (config.Config, error) { - base := b.cfgHolder.Get() - // These documents belong to the task's org; absence must not inherit - // the instance's MCP credentials or agent profiles. - base.Tools.MCPServers = nil - base.Containers.Profiles = nil - base.Providers = nil - cfg, _, err := b.controlPlane.RuntimeConfig(ctx, base) - return cfg, err -} - -func (s *taskToolsServer) taskSampling(llm *runtime.Runtime, cfg config.Config, t *task.Task, credential string) protocol.SamplingHandler { - sample := taskmcp.SamplingHandler(llm, cfg.ForTask(), taskmcp.UsageRecorder(s.authority, t, s.gateway.log)) - return func(ctx context.Context, request protocol.SamplingRequest) (protocol.SamplingResult, error) { - live, _, err := s.authority.TaskPrincipalForCredential(ctx, credential) - if err != nil || live.ID != t.ID || live.Status != task.StatusRunning || live.OrgOrDefault() != t.OrgOrDefault() { - return protocol.SamplingResult{}, errors.New("task grant no longer authorizes sampling") - } - return sample(ctx, request) - } -} diff --git a/internal/app/archiegateway/task_tools_test.go b/internal/app/archiegateway/task_tools_test.go deleted file mode 100644 index 3ed3810ff..000000000 --- a/internal/app/archiegateway/task_tools_test.go +++ /dev/null @@ -1,281 +0,0 @@ -package archiegateway - -import ( - "context" - "encoding/json" - "errors" - "log/slog" - "net" - "net/http" - "net/http/httptest" - "net/url" - "slices" - "testing" - "time" - - "github.com/modelcontextprotocol/go-sdk/mcp" - "google.golang.org/grpc" - "google.golang.org/grpc/codes" - "google.golang.org/grpc/credentials/insecure" - "google.golang.org/grpc/status" - "google.golang.org/grpc/test/bufconn" - - "github.com/samcharles93/archie-core/internal/agentexec/modelloop" - "github.com/samcharles93/archie-core/internal/config" - pb "github.com/samcharles93/archie-core/internal/contracts/gateway/v1" - "github.com/samcharles93/archie-core/internal/domain/access" - "github.com/samcharles93/archie-core/internal/domain/agentrun" - "github.com/samcharles93/archie-core/internal/domain/identity" - "github.com/samcharles93/archie-core/internal/domain/org" - "github.com/samcharles93/archie-core/internal/domain/storepkg" - "github.com/samcharles93/archie-core/internal/domain/usage" - "github.com/samcharles93/archie-core/internal/domain/workflow/task" - "github.com/samcharles93/archie-core/internal/infrastructure/egress" - "github.com/samcharles93/archie-core/internal/infrastructure/rpcidentity" - "github.com/samcharles93/archie-core/internal/infrastructure/staterpc" - "github.com/samcharles93/archie-core/internal/infrastructure/tasktools" - "github.com/samcharles93/archie-core/internal/tools" - protocol "github.com/samcharles93/archie-core/internal/tools/mcp" -) - -type taskToolGrants struct { - taskToolAuthority - task task.Task - principal access.Principal - accepted *storepkg.Authority - revoked bool - recorded []usage.Record -} - -func (a *taskToolGrants) TaskPrincipalForCredential(context.Context, string) (*task.Task, identity.IdentityID, error) { - if a.revoked { - return nil, "", errors.New("revoked") - } - return &a.task, a.principal.IdentityID, nil -} - -func (a *taskToolGrants) PrincipalFor(context.Context, identity.IdentityID) (access.Principal, error) { - return a.principal, nil -} - -func (a *taskToolGrants) RecordUsage(_ context.Context, value usage.Record) error { - a.recorded = append(a.recorded, value) - return nil -} - -func (a *taskToolGrants) GetInstalled(context.Context, string) (storepkg.Installed, error) { - return storepkg.Installed{AcceptedAuthority: a.accepted}, nil -} - -func TestTaskToolAuthorization(t *testing.T) { - for _, tt := range []struct { - name, caller, token, tool string - profile []string - packaged, revoked bool - accepted *storepkg.Authority - principalOrg org.OrgID - status string - want codes.Code - }{ - {name: "allowed", tool: "fetch", want: codes.OK}, - {name: "profile denial", tool: "fetch", profile: []string{"other"}, want: codes.PermissionDenied}, - {name: "unaccepted package", tool: "fetch", packaged: true, want: codes.PermissionDenied}, - {name: "accepted package", tool: "fetch", packaged: true, accepted: &storepkg.Authority{Tools: []string{"fetch"}}}, - {name: "package intersection", tool: "fetch", packaged: true, accepted: &storepkg.Authority{Tools: []string{"fetch"}}, profile: []string{"other"}, want: codes.PermissionDenied}, - {name: "workspace never served", tool: "shell", want: codes.PermissionDenied}, - {name: "chat files never served", tool: "send_file", want: codes.PermissionDenied}, - {name: "approval unavailable", tool: "danger", want: codes.PermissionDenied}, - {name: "revoked", tool: "fetch", revoked: true, want: codes.Unauthenticated}, - {name: "not running", tool: "fetch", status: "completed", want: codes.PermissionDenied}, - {name: "other org principal", tool: "fetch", principalOrg: "victim", want: codes.PermissionDenied}, - {name: "non daemon", caller: "archie-ui", tool: "fetch", want: codes.PermissionDenied}, - } { - t.Run(tt.name, func(t *testing.T) { - authority := &taskToolGrants{task: task.Task{ID: 7, Org: "acme", Status: "running", WorkflowDefinitionYAML: "id: work\nprofile: kit\nidentity: bot\n"}, principal: access.Principal{IdentityID: "bot", Org: "acme"}, accepted: tt.accepted, revoked: tt.revoked} - if tt.packaged { - authority.task.WorkflowDefinitionYAML += "package: sample\n" - } - if tt.status != "" { - authority.task.Status = tt.status - } - if tt.principalOrg != "" { - authority.principal.Org = tt.principalOrg - } - called := 0 - reg := tools.NewRegistry() - for _, entry := range []tools.ToolEntry{ - {Name: "fetch", Toolset: "web"}, {Name: "shell", Toolset: "workspace"}, {Name: "send_file", Toolset: "media"}, {Name: "danger", Classification: tools.RequiresApproval}, - } { - entry.Schema = tools.JSONSchema{"type": "object"} - entry.Handler = func(ctx context.Context, _ map[string]any) (any, error) { - called++ - p, _ := access.PrincipalFromContext(ctx) - if p.IdentityID != "bot" || org.OrgFromContext(ctx) != "acme" { - t.Fatal("tool executed with caller-selected authority") - } - return "result", nil - } - if err := reg.Register(entry); err != nil { - t.Fatal(err) - } - } - cfg := config.Config{Containers: config.ContainerConfig{Profiles: map[string]config.AgentProfile{"kit": {Kit: "image@sha256:pinned", Tools: tt.profile}}}} - s := &taskToolsServer{gateway: &server{cfgHolder: config.NewHolder(cfg), toolReg: reg, log: slog.New(slog.DiscardHandler)}, authority: authority, configFor: func(context.Context) (config.Config, error) { return cfg, nil }, sessions: make(map[[32]byte]*taskToolSession)} - caller := tt.caller - if caller == "" { - caller = "archied" - } - ctx := access.WithSource(t.Context(), caller) - result, err := s.ListTaskTools(ctx, &pb.ListTaskToolsRequest{RunCredential: "grant"}) - if err == nil { - var listed []*mcp.Tool - if err := json.Unmarshal(result.ToolsJson, &listed); err != nil { - t.Fatal(err) - } - for _, tool := range listed { - if slices.Contains([]string{"shell", "send_file", "danger"}, tool.Name) { - t.Fatalf("unsafe tool listed: %s", tool.Name) - } - } - } - _, err = s.CallTaskTool(ctx, &pb.CallTaskToolRequest{RunCredential: "grant", Name: tt.tool, ArgumentsJson: []byte(`{}`)}) - if status.Code(err) != tt.want { - t.Fatalf("error %v, want %v", err, tt.want) - } - if tt.want == codes.OK && called != 1 { - t.Fatalf("calls %d, want 1", called) - } - if tt.want != codes.OK && called != 0 { - t.Fatal("denied request executed") - } - // Authorization must be checked again after a successful discovery. - authority.revoked = true - if _, err := s.CallTaskTool(ctx, &pb.CallTaskToolRequest{RunCredential: "grant", Name: "fetch", ArgumentsJson: []byte(`{}`)}); err == nil { - t.Fatal("revoked grant remained usable") - } - }) - } -} - -func TestTaskToolGatewayThroughProxy(t *testing.T) { - authority := &taskToolGrants{task: task.Task{ID: 7, Org: "acme", Status: "running", WorkflowDefinitionYAML: "id: work\nprofile: kit\nidentity: bot\n"}, principal: access.Principal{IdentityID: "bot", Org: "acme"}} - reg := tools.NewRegistry() - calls := 0 - blocked := make(chan struct{}) - if err := reg.Register(tools.ToolEntry{Name: "echo", Toolset: "web", Schema: tools.JSONSchema{"type": "object", "properties": map[string]any{"value": map[string]any{"type": "string"}}, "required": []string{"value"}}, Handler: func(ctx context.Context, args map[string]any) (any, error) { - calls++ - if org.OrgFromContext(ctx) != "acme" { - t.Error("lost task org") - } - if args["value"] == "block" { - close(blocked) - <-ctx.Done() - return nil, ctx.Err() - } - return args["value"], nil - }}); err != nil { - t.Fatal(err) - } - cfg := config.Config{Containers: config.ContainerConfig{Profiles: map[string]config.AgentProfile{"kit": {Kit: "pinned", Tools: []string{"echo"}}}}} - service := &taskToolsServer{gateway: &server{cfgHolder: config.NewHolder(cfg), toolReg: reg, log: slog.New(slog.DiscardHandler)}, authority: authority, configFor: func(context.Context) (config.Config, error) { return cfg, nil }, sessions: make(map[[32]byte]*taskToolSession)} - defer service.Close(t.Context()) - listener := bufconn.Listen(1 << 20) - rpc := grpc.NewServer(grpc.ChainUnaryInterceptor((rpcidentity.Callers{}).Unary())) - pb.RegisterTaskToolsServiceServer(rpc, service) - go func() { _ = rpc.Serve(listener) }() - defer rpc.Stop() - conn, err := grpc.NewClient("passthrough:///gateway", grpc.WithTransportCredentials(insecure.NewCredentials()), grpc.WithContextDialer(func(context.Context, string) (net.Conn, error) { return listener.Dial() }), grpc.WithUnaryInterceptor(staterpc.UnaryClientCredentialInterceptor("archied", ""))) - if err != nil { - t.Fatal(err) - } - defer conn.Close() - proxy := egress.NewProxy(nil, egress.ProxyOptions{TaskTools: tasktools.NewClient(conn)}) - session, err := proxy.Register(egress.SessionOptions{Token: "proxy-password", RunCredential: "grant", Org: "acme"}) - if err != nil { - t.Fatal(err) - } - defer proxy.Revoke(t.Context(), session.Token()) - session.EnterRuntime() - httpServer := httptest.NewServer(proxy) - defer httpServer.Close() - proxyURL, err := url.Parse(httpServer.URL) - if err != nil { - t.Fatal(err) - } - proxyURL.User = url.UserPassword("archie", session.Token()) - transport := &http.Transport{Proxy: http.ProxyURL(proxyURL)} - defer transport.CloseIdleConnections() - client := mcp.NewClient(&mcp.Implementation{Name: "test-harness", Version: "1"}, nil) - clientSession, err := client.Connect(t.Context(), &mcp.StreamableClientTransport{Endpoint: tasktools.Endpoint, HTTPClient: &http.Client{Transport: taskSentinelTransport{base: transport}}, DisableStandaloneSSE: true, MaxRetries: -1}, nil) - if err != nil { - t.Fatal(err) - } - defer clientSession.Close() - listed, err := clientSession.ListTools(t.Context(), nil) - if err != nil || len(listed.Tools) != 1 || listed.Tools[0].Name != "echo" { - t.Fatalf("discovery %v %v", listed, err) - } - result, err := clientSession.CallTool(t.Context(), &mcp.CallToolParams{Name: "echo", Arguments: map[string]any{"value": "hello"}}) - if err != nil || result.IsError || calls != 1 { - t.Fatalf("call %v %v calls %d", result, err, calls) - } - if _, err := clientSession.CallTool(t.Context(), &mcp.CallToolParams{Name: "forbidden", Arguments: map[string]any{}}); err == nil { - t.Fatal("unknown tool accepted") - } - if calls != 1 { - t.Fatal("unknown tool executed") - } - samples := 0 - model := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { - samples++ - w.Header().Set("Content-Type", "application/json") - _, _ = w.Write([]byte(`{"id":"sample","object":"chat.completion","model":"task","choices":[{"index":0,"message":{"role":"assistant","content":"sampled"},"finish_reason":"stop"}],"usage":{"prompt_tokens":3,"completion_tokens":2,"total_tokens":5}}`)) - })) - defer model.Close() - llm := modelloop.NewRuntime(map[string]agentrun.Provider{"fixture": {Class: "openai", BaseURL: model.URL + "/v1"}}, nil) - sample := service.taskSampling(llm, config.Config{Models: map[string]string{"default": "fixture/task"}}, &authority.task, "grant") - if _, err := sample(t.Context(), protocol.SamplingRequest{MaxTokens: 5, Messages: []protocol.SamplingMessage{{Role: "user", Content: protocol.SamplingContent{Type: "text", Text: "sample"}}}}); err != nil { - t.Fatal(err) - } - if samples != 1 || len(authority.recorded) != 1 || authority.recorded[0].TaskID != 7 || authority.recorded[0].Source != usage.SourceTask { - t.Fatal("sampling lost task attribution") - } - authority.revoked = true - result, err = clientSession.CallTool(t.Context(), &mcp.CallToolParams{Name: "echo", Arguments: map[string]any{"value": "again"}}) - if err == nil && !result.IsError { - t.Fatal("revoked grant executed") - } - if calls != 1 { - t.Fatal("revoked tool executed") - } - if _, err := sample(t.Context(), protocol.SamplingRequest{}); err == nil || samples != 1 { - t.Fatal("revoked grant sampled a model") - } - authority.revoked = false - ctx, cancel := context.WithTimeout(t.Context(), 2*time.Second) - defer cancel() - finished := make(chan error, 1) - go func() { - _, err := clientSession.CallTool(ctx, &mcp.CallToolParams{Name: "echo", Arguments: map[string]any{"value": "block"}}) - finished <- err - }() - select { - case <-blocked: - case <-ctx.Done(): - t.Fatal("tool call did not start") - } - proxy.Revoke(ctx, session.Token()) - select { - case <-finished: - case <-ctx.Done(): - t.Fatal("proxy revocation did not cancel the active call") - } -} - -type taskSentinelTransport struct{ base http.RoundTripper } - -func (t taskSentinelTransport) RoundTrip(r *http.Request) (*http.Response, error) { - r = r.Clone(r.Context()) - r.Header.Set("Authorization", "Bearer "+tasktools.Sentinel) - return t.base.RoundTrip(r) -} diff --git a/internal/app/archiegateway/tools.go b/internal/app/archiegateway/tools.go index fbcc4966a..c6995a329 100644 --- a/internal/app/archiegateway/tools.go +++ b/internal/app/archiegateway/tools.go @@ -104,8 +104,7 @@ func (b *server) registerStandaloneTools(ctx context.Context) { } // registerWorkflowTools puts the workflow and integration tools in the shared -// registry, so chat turns and Kit harnesses (through the task-tool RPCs) both -// reach them, each in its own caller's org. +// registry, so chat turns reach them, each in its caller's org. func (b *server) registerWorkflowTools(ctx context.Context) { authz := infraaccess.StartLive(ctx, b.stateStore.ListPolicies, b.stateStore.PoliciesVersion, b.log) workflows := controlplanerpc.NewWorkflows(b.stateStore.ControlPlane()) diff --git a/internal/app/archieui/compose.go b/internal/app/archieui/compose.go index f51906e5e..171dca2e2 100644 --- a/internal/app/archieui/compose.go +++ b/internal/app/archieui/compose.go @@ -112,11 +112,6 @@ func compose(ctx context.Context, d deps) *webui.Server { if bindings, ok := d.Store.(storecontract.BindingStore); ok { srv.Bindings = bindings } - // Harness token status comes from the State Store; the setup terminal is not - // available here. - if secrets, ok := d.Store.(storecontract.HarnessSecretStore); ok { - srv.HarnessSecrets = secrets - } if eventTypes, ok := d.Store.(storecontract.EventTypeStore); ok { srv.EventTypes = eventTypes } diff --git a/internal/app/archieui/run.go b/internal/app/archieui/run.go index ecc6deb60..3b4ef4d95 100644 --- a/internal/app/archieui/run.go +++ b/internal/app/archieui/run.go @@ -21,7 +21,6 @@ import ( "github.com/samcharles93/archie-core/internal/infrastructure/controlplanerpc" "github.com/samcharles93/archie-core/internal/infrastructure/daemonrpc" "github.com/samcharles93/archie-core/internal/infrastructure/gatewayrpc" - "github.com/samcharles93/archie-core/internal/infrastructure/harnessrpc" "github.com/samcharles93/archie-core/internal/infrastructure/staterpc" "github.com/samcharles93/archie-core/internal/infrastructure/telemetry" "github.com/samcharles93/archie-core/internal/webui" @@ -150,19 +149,6 @@ func wireDaemon(opts Options, srv *webui.Server, cleanups *[]func()) error { } srv.RepositoryChecks = checks *cleanups = append(*cleanups, closeChecks) - return wireHarness(opts, srv, cleanups) -} - -// wireHarness attaches the setup terminal and the run-scoped sandbox view to -// the session contract the daemon serves on its listener. -func wireHarness(opts Options, srv *webui.Server, cleanups *[]func()) error { - harness, closeHarness, err := harnessrpc.Dial(opts.Daemon.Target, opts.identity) - if err != nil { - return err - } - srv.HarnessTerminal = harness - srv.SandboxView = harness - *cleanups = append(*cleanups, closeHarness) return nil } diff --git a/internal/app/statestore/eda.go b/internal/app/statestore/eda.go index 078806715..6b9052565 100644 --- a/internal/app/statestore/eda.go +++ b/internal/app/statestore/eda.go @@ -18,7 +18,6 @@ type eventCaptureStore interface { //nolint:interfacebloat // composite of the s storecontract.PlaybookDispatcher storecontract.EventTypeStore storecontract.SourceStore - storecontract.HarnessSecretStore toolCallWriter } diff --git a/internal/app/statestore/server.go b/internal/app/statestore/server.go index 753b34282..93dd12f1c 100644 --- a/internal/app/statestore/server.go +++ b/internal/app/statestore/server.go @@ -325,10 +325,6 @@ func (b *server) stateStoreDeps(grants *staterpc.TaskGrants) staterpc.Deps { deps.EventTypes = b.eda deps.Sources = b.eda deps.MappingMatches = b.eda - // HarnessSecrets rides the same encrypted-at-rest store as source - // webhook secrets, under its - // own domain separator (bindingcipher.HarnessSecretDomain). - deps.HarnessSecrets = b.eda } // tool_call events project into the tool_calls collection on the same // event-capture store: this process legitimately owns both, so the diff --git a/internal/config/config.go b/internal/config/config.go index d737b80e0..da53690d7 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -353,7 +353,7 @@ type Config struct { TLS TLS `toml:"tls" yaml:"tls"` WorkDir string `toml:"work_dir" yaml:"work_dir"` // SkillsDir is an optional directory of */SKILL.md skills. Its workflows - // override built-ins, and Kits mount it read-only. + // override built-ins. SkillsDir string `toml:"skills_dir" yaml:"skills_dir"` // MaxRetries caps how many times a parked task is retried before // being permanently parked (status "dead"). Defaults to 3. @@ -719,7 +719,7 @@ type ContainerConfig struct { // Profiles are named agent profiles a workflow selects with `profile:`. // Stored as their own control-plane resource. Profiles map[string]AgentProfile `toml:"profiles" yaml:"profiles" json:"-"` - // Credentials binds Kit credential service names to org secrets. Stored as + // Credentials binds credential service names to org secrets. Stored as // their own control-plane resource. Credentials []CredentialBinding `toml:"credentials" yaml:"credentials" json:"-"` // RegistryAuth resolves to a Docker registry.AuthConfig JSON document for @@ -728,18 +728,15 @@ type ContainerConfig struct { RegistryAuth SecretRef `toml:"registry_auth" yaml:"registry_auth" json:"-"` } -// CredentialBinding maps one Kit credential@1 service name to an org secret. +// CredentialBinding maps one credential service name, such as a model +// provider's, to an org secret. type CredentialBinding struct { - // Service names the credential@1 service a Kit's descriptor declares - // (docker/sandbox-kit-spec spec.CredentialCapability.Service). Service string `toml:"service" yaml:"service"` // Org is the org this binding belongs to. Empty means the default org. A // run whose identity's Org does not match never resolves this binding, // however it is granted. Org string `toml:"org" yaml:"org"` - // Secret is where an API key's real value is resolved from. A service - // the Kit declares OAuth-managed leaves it empty: its tokens are the - // org's captured harness secret in the State Store. + // Secret is where the key's real value is resolved from. Secret SecretRef `toml:"secret" yaml:"secret"` } @@ -799,8 +796,8 @@ func credentialOrg(value string) string { return value } -// ValidateCredentialBindings rejects a binding with an empty service name or -// duplicated (service, org) pair: two bindings resolving the same Kit +// ValidateCredentialBindings rejects a binding with an empty service name, no +// secret, or a duplicated (service, org) pair: two bindings resolving the same // credential in the same org is ambiguous, not a fallback chain. func (c ContainerConfig) ValidateCredentialBindings() error { seen := make(map[string]bool, len(c.Credentials)) @@ -808,6 +805,9 @@ func (c ContainerConfig) ValidateCredentialBindings() error { if strings.TrimSpace(b.Service) == "" { return fmt.Errorf("containers.credentials: a service name must not be empty") } + if b.Secret == (SecretRef{}) { + return fmt.Errorf("containers.credentials: service %q names no secret", b.Service) + } key := credentialOrg(b.Org) + "\x00" + b.Service if seen[key] { return fmt.Errorf("containers.credentials: service %q is bound more than once for org %q", b.Service, b.Org) @@ -822,14 +822,6 @@ func (c ContainerConfig) ValidateCredentialBindings() error { type AgentProfile struct { // Image is the container image; empty means [containers].image. Image string `toml:"image" yaml:"image"` - // Kit makes this a harness profile: one workload Kit or published Kit - // set, pinned by digest. Mixins are composed by publishing a set, which - // merges their layers into one image. The task container runs that - // image and every agent stage runs on its CLI. Exclusive with Image. - Kit string `toml:"kit" yaml:"kit"` - // Adapter names the output adapter reading the Kit's CLI; empty runs it - // with no capture tools and no usage. - Adapter string `toml:"adapter" yaml:"adapter"` // Tools allowlists the tools archie adds to the agent (MCP servers, // repository scripts and skill plugins) by name. Empty allows them all. // The agent loop's own file tools are always present, read-only when a @@ -847,17 +839,14 @@ func (c ContainerConfig) Profile(name string) (AgentProfile, error) { if !ok { return AgentProfile{}, fmt.Errorf("agent profile %q is not configured", name) } - if p.Image == "" && !p.IsKit() { + if p.Image == "" { p.Image = c.Image } return p, nil } -// IsKit reports whether the profile runs a Kit harness. -func (p AgentProfile) IsKit() bool { return p.Kit != "" } - -// ValidateProfiles rejects a profile with an empty name, an empty tool -// name, or a Kit that is not pinned by digest or also names an image. +// ValidateProfiles rejects a profile with an empty name or an empty tool +// name. func (c ContainerConfig) ValidateProfiles() error { return ValidateAgentProfiles(c.Profiles) } @@ -868,15 +857,6 @@ func ValidateAgentProfiles(profiles map[string]AgentProfile) error { if strings.TrimSpace(name) == "" { return fmt.Errorf("containers.profiles: a profile name must not be empty") } - switch { - case p.IsKit() && p.Image != "": - return fmt.Errorf("containers.profiles.%s: image and kit are exclusive; a Kit profile runs the Kit's image", name) - case !p.IsKit() && p.Adapter != "": - return fmt.Errorf("containers.profiles.%s: adapter needs a kit", name) - } - if p.IsKit() && !strings.Contains(p.Kit, "@sha256:") { - return fmt.Errorf("containers.profiles.%s.kit: %q must be pinned by digest", name, p.Kit) - } for _, tool := range p.Tools { if strings.TrimSpace(tool) == "" { return fmt.Errorf("containers.profiles.%s.tools: a tool name must not be empty", name) diff --git a/internal/config/servicespec.go b/internal/config/servicespec.go index 159ea9c6c..24aff4f3c 100644 --- a/internal/config/servicespec.go +++ b/internal/config/servicespec.go @@ -111,6 +111,5 @@ func init() { RegisterService(ServiceBoth, ServiceNameGateway, "127.0.0.1:8585", "127.0.0.1:8585") RegisterService(ServiceClient, ServiceNameState, "", "127.0.0.1:9090") RegisterService(ServiceBoth, ServiceNameMessaging, "127.0.0.1:8586", "127.0.0.1:8586") - // The daemon listener also serves the harness session contract. RegisterService(ServiceBoth, ServiceNameDaemon, "127.0.0.1:8587", "127.0.0.1:8587") } diff --git a/internal/container/kit.go b/internal/container/kit.go deleted file mode 100644 index 507700118..000000000 --- a/internal/container/kit.go +++ /dev/null @@ -1,274 +0,0 @@ -package container - -import ( - "bytes" - "context" - "encoding/base64" - "errors" - "fmt" - "path" - "strings" - - "github.com/docker/sandbox-kit-spec/v3/spec" - "github.com/moby/moby/api/pkg/stdcopy" - "github.com/moby/moby/api/types/container" - "github.com/moby/moby/api/types/mount" - "github.com/moby/moby/client" - - "github.com/samcharles93/archie-core/internal/infrastructure/kit" -) - -const hookOutputTail = 2000 - -// KitSpec is one Kit task container: the workload image running archie's -// worker as root, on the task's sandbox network, with the Kit's launch. -type KitSpec struct { - Name string - Image string - Network string - // Worker is the entrypoint that runs archie-agent as the container's - // root process, and WorkerEnv its environment, credentials included. - Worker []string - WorkerEnv []string - Binds []string - Labels map[string]string - Launch kit.Launch - // InstallDone runs after the install hooks and before anything else, so - // the caller can end the install phase: the egress session moves to its - // runtime policy and install-phase credentials stop resolving. - InstallDone func() -} - -// StartKit creates and starts a Kit container and runs its install and -// startup hooks. A failing hook removes the container and returns its -// output. -func StartKit(ctx context.Context, cli *client.Client, s KitSpec) (string, error) { - var mounts []mount.Mount - for _, v := range s.Launch.Volumes { - if _, err := cli.VolumeCreate(ctx, client.VolumeCreateOptions{Name: v.Name, Labels: s.Labels}); err != nil { - return "", fmt.Errorf("create kit volume %s: %w", v.Name, err) - } - mounts = append(mounts, mount.Mount{Type: mount.TypeVolume, Source: v.Name, Target: v.Path}) - } - // Docker's own init becomes PID 1 and reaps orphans. The worker is PID 1 - // otherwise, and a detached hook that forks and exits leaves zombies the - // worker never waits on, until the container's PID table is exhausted. - hostInit := true - created, err := cli.ContainerCreate(ctx, client.ContainerCreateOptions{ - Name: s.Name, - Config: &container.Config{ - Image: s.Image, - Entrypoint: s.Worker, - Cmd: []string{}, - User: "0", - Env: s.WorkerEnv, - WorkingDir: kit.WorkspaceDir, - Labels: s.Labels, - }, - HostConfig: &container.HostConfig{ - NetworkMode: container.NetworkMode(s.Network), - Binds: s.Binds, - Mounts: mounts, - Init: &hostInit, - }, - }) - if err != nil { - return "", fmt.Errorf("create kit container: %w", err) - } - fail := func(err error) (string, error) { - _, rmErr := cli.ContainerRemove(context.WithoutCancel(ctx), created.ID, client.ContainerRemoveOptions{Force: true}) - return "", errors.Join(err, rmErr) - } - if _, err := cli.ContainerStart(ctx, created.ID, client.ContainerStartOptions{}); err != nil { - return fail(fmt.Errorf("start kit container: %w", err)) - } - if err := installKit(ctx, cli, created.ID, s.Launch); err != nil { - return fail(err) - } - if s.InstallDone != nil { - s.InstallDone() - } - for _, f := range s.Launch.Files { - if err := writeKitFile(ctx, cli, created.ID, s.Launch.Harness.User, f.Path, f.Content, f.Mode, f.Overwrite); err != nil { - return fail(fmt.Errorf("write kit file %s: %w", f.Path, err)) - } - } - for _, f := range s.Launch.Context { - if err := writeKitContext(ctx, cli, created.ID, f); err != nil { - return fail(fmt.Errorf("write agent context %s: %w", f.Path, err)) - } - } - for _, skill := range s.Launch.Skills { - if err := stageKitSkill(ctx, cli, created.ID, s.Launch.Harness.User, skill); err != nil { - return fail(err) - } - } - for i, h := range s.Launch.Startup { - if err := runHook(ctx, cli, created.ID, h); err != nil { - return fail(fmt.Errorf("startup hook %d: %w", i+1, err)) - } - } - return created.ID, nil -} - -func installKit(ctx context.Context, cli *client.Client, id string, launch kit.Launch) error { - for _, f := range launch.InstallFiles { - if err := writeKitFile(ctx, cli, id, launch.Harness.User, f.Path, f.Content, f.Mode, f.Overwrite); err != nil { - return fmt.Errorf("write install credential file %s: %w", f.Path, err) - } - } - for i, h := range launch.Install { - if err := runHook(ctx, cli, id, h); err != nil { - return fmt.Errorf("install hook %d: %w", i+1, err) - } - } - for _, f := range launch.InstallFiles { - code, out, err := ExecIn(ctx, cli, id, launch.Harness.User, []string{"KIT_PATH=" + f.Path}, []string{"sh", "-c", `rm -f -- "$KIT_PATH"`}) - if err != nil { - return fmt.Errorf("remove install credential file %s: %w", f.Path, err) - } - if code != 0 { - return fmt.Errorf("remove install credential file %s: exit %d: %s", f.Path, code, out) - } - } - return nil -} - -func stageKitSkill(ctx context.Context, cli *client.Client, id, user string, skill kit.Skill) error { - code, out, err := ExecIn(ctx, cli, id, "0", []string{"SKILL_SOURCE=" + skill.Source, "SKILL_TARGET=" + skill.Target, "SKILL_USER=" + user}, []string{"sh", "-ec", ` -[ -d "$SKILL_SOURCE" ] && [ -f "$SKILL_SOURCE/SKILL.md" ] || exit 1 -[ "$SKILL_SOURCE" != "$SKILL_TARGET" ] || exit 0 -at="$SKILL_TARGET" -while [ "$at" != / ]; do - [ ! -L "$at" ] || exit 1 - at=$(dirname "$at") -done -[ ! -e "$SKILL_TARGET" ] || exit 0 -mkdir -p "$(dirname "$SKILL_TARGET")" -cp -R "$SKILL_SOURCE" "$SKILL_TARGET" -chown -R "$SKILL_USER" "$SKILL_TARGET" -`}) - if err != nil { - return fmt.Errorf("stage bundled skill %s: %w", skill.Source, err) - } - if code != 0 { - return fmt.Errorf("stage bundled skill %s: exit %d: %s", skill.Source, code, out) - } - return nil -} - -// Replace only Archie's managed guidance, retaining the image's instructions. -func writeKitContext(ctx context.Context, cli *client.Client, id string, f spec.File) error { - code, out, err := ExecIn(ctx, cli, id, "0", []string{"KIT_PATH=" + f.Path, "KIT_CONTENT=" + base64.StdEncoding.EncodeToString([]byte(f.Content))}, []string{"sh", "-ec", ` -at="$KIT_PATH" -while [ "$at" != / ]; do - [ ! -L "$at" ] || exit 1 - at=$(dirname "$at") -done -[ ! -e "$KIT_PATH" ] || [ -f "$KIT_PATH" ] || exit 1 -mkdir -p "$(dirname "$KIT_PATH")" -tmp=$(mktemp "$KIT_PATH.XXXXXX") -trap 'rm -f "$tmp"' 0 -if [ -f "$KIT_PATH" ]; then - awk '/^$/ { managed=1; next } /^$/ { managed=0; next } !managed { print }' "$KIT_PATH" > "$tmp" -fi -printf '%s\n' '' >> "$tmp" -printf %s "$KIT_CONTENT" | base64 -d >> "$tmp" -printf '\n%s\n' '' >> "$tmp" -chmod 0644 "$tmp" -mv "$tmp" "$KIT_PATH" -`}) - if err != nil { - return err - } - if code != 0 { - return fmt.Errorf("exited %d: %s", code, out) - } - return nil -} - -// RemoveKitVolumes removes an execution's Kit volumes once the execution -// has ended; a retry of the same execution reuses them until then. -func RemoveKitVolumes(ctx context.Context, cli *client.Client, volumes []kit.Volume) error { - var errs []error - for _, v := range volumes { - if _, err := cli.VolumeRemove(ctx, v.Name, client.VolumeRemoveOptions{Force: true}); err != nil { - errs = append(errs, fmt.Errorf("remove kit volume %s: %w", v.Name, err)) - } - } - return errors.Join(errs...) -} - -func runHook(ctx context.Context, cli *client.Client, id string, h kit.Hook) error { - if h.Background { - exec, err := cli.ExecCreate(ctx, id, client.ExecCreateOptions{User: h.User, Env: h.Env, Cmd: h.Argv, WorkingDir: kit.WorkspaceDir}) - if err != nil { - return err - } - _, err = cli.ExecStart(ctx, exec.ID, client.ExecStartOptions{Detach: true}) - return err - } - code, out, err := ExecIn(ctx, cli, id, h.User, h.Env, h.Argv) - if err != nil { - return err - } - if code != 0 { - return fmt.Errorf("%s exited %d: %s", strings.Join(h.Argv, " "), code, out) - } - return nil -} - -// writeKitFile writes a lifecycle file as the harness user, so the file -// belongs to the agent as the Kit spec requires. -func writeKitFile(ctx context.Context, cli *client.Client, id, user, target, content, mode string, overwrite *bool) error { - script := `mkdir -p "$(dirname "$KIT_PATH")" && printf %s "$KIT_CONTENT" | base64 -d > "$KIT_PATH"` - if mode != "" { - script += ` && chmod "$KIT_MODE" "$KIT_PATH"` - } - if overwrite != nil && !*overwrite { - script = `[ -e "$KIT_PATH" ] || { ` + script + `; }` - } - env := []string{ - "KIT_PATH=" + path.Clean(target), - "KIT_CONTENT=" + base64.StdEncoding.EncodeToString([]byte(content)), - "KIT_MODE=" + mode, - } - code, out, err := ExecIn(ctx, cli, id, user, env, []string{"sh", "-c", script}) - if err != nil { - return err - } - if code != 0 { - return fmt.Errorf("exited %d: %s", code, out) - } - return nil -} - -// ExecIn runs argv in a container as user with exactly env, and returns its -// exit code and the tail of its combined output. -func ExecIn(ctx context.Context, cli *client.Client, id, user string, env, argv []string) (int, string, error) { - exec, err := cli.ExecCreate(ctx, id, client.ExecCreateOptions{ - User: user, Env: env, Cmd: argv, WorkingDir: kit.WorkspaceDir, - AttachStdout: true, AttachStderr: true, - }) - if err != nil { - return 0, "", err - } - attached, err := cli.ExecAttach(ctx, exec.ID, client.ExecAttachOptions{}) - if err != nil { - return 0, "", err - } - defer attached.Close() - var out bytes.Buffer - if _, err := stdcopy.StdCopy(&out, &out, attached.Reader); err != nil { - return 0, "", err - } - inspected, err := cli.ExecInspect(ctx, exec.ID, client.ExecInspectOptions{}) - if err != nil { - return 0, "", err - } - text := strings.TrimSpace(out.String()) - if len(text) > hookOutputTail { - text = text[len(text)-hookOutputTail:] - } - return inspected.ExitCode, text, nil -} diff --git a/internal/container/kit_test.go b/internal/container/kit_test.go deleted file mode 100644 index 1f2fe9115..000000000 --- a/internal/container/kit_test.go +++ /dev/null @@ -1,82 +0,0 @@ -package container - -import ( - "context" - "os" - "path/filepath" - "strings" - "testing" - - "github.com/docker/sandbox-kit-spec/v3/spec" - "github.com/moby/moby/client" - - "github.com/samcharles93/archie-core/internal/domain/agentrun" - "github.com/samcharles93/archie-core/internal/infrastructure/kit" -) - -func TestKitRuntimeDiscovery(t *testing.T) { - if testing.Short() { - t.Skip("requires Docker") - } - cli, err := client.New(client.FromEnv) - if err != nil { - t.Fatal(err) - } - defer cli.Close() - shared := t.TempDir() - if err := os.Chmod(shared, 0o755); err != nil { - t.Fatal(err) - } - if err := os.WriteFile(filepath.Join(shared, "SKILL.md"), []byte("shared body"), 0o644); err != nil { - t.Fatal(err) - } - for _, tc := range []struct { - name string - symlink bool - }{ - {name: "preserves and replaces managed guidance"}, - {name: "refuses symlink guidance", symlink: true}, - } { - t.Run(tc.name, func(t *testing.T) { - script := `test -f /tmp/install.json; mkdir -p /tmp/discovery /tmp/bundle; printf 'SKILL body' > /tmp/bundle/SKILL.md; printf 'image instructions\n' > /tmp/discovery/AGENTS.md` - if tc.symlink { - script += `; mv /tmp/discovery/AGENTS.md /tmp/other; ln -s /tmp/other /tmp/discovery/AGENTS.md` - } - launch := kit.Launch{ - Harness: agentrun.HarnessSpec{User: "0"}, Install: []kit.Hook{{User: "0", Argv: []string{"sh", "-ec", script}}}, InstallFiles: []spec.File{{Path: "/tmp/install.json", Content: "install sentinel"}}, - Files: []spec.File{{Path: "/tmp/runtime.json", Content: "runtime sentinel"}}, - Startup: []kit.Hook{{User: "0", Argv: []string{"sh", "-ec", "test ! -e /tmp/install.json; test -f /tmp/runtime.json"}}}, - Context: []spec.File{{Path: "/tmp/discovery/AGENTS.md", Content: "first guidance"}}, Skills: []kit.Skill{{Source: "/tmp/bundle", Target: "/tmp/skills/bundle"}}, - } - id, err := StartKit(t.Context(), cli, KitSpec{Image: "alpine:3", Worker: []string{"tail", "-f", "/dev/null"}, Binds: []string{shared + ":/tmp/skills/shared:ro,z"}, Launch: launch}) - if tc.symlink { - if err == nil { - _, _ = cli.ContainerRemove(context.Background(), id, client.ContainerRemoveOptions{Force: true}) - t.Fatal("symlink was followed") - } - return - } - if err != nil { - t.Fatal(err) - } - t.Cleanup(func() { - _, _ = cli.ContainerRemove(context.Background(), id, client.ContainerRemoveOptions{Force: true}) - }) - if err := writeKitContext(t.Context(), cli, id, spec.File{Path: "/tmp/discovery/AGENTS.md", Content: "replacement guidance"}); err != nil { - t.Fatal(err) - } - code, out, err := ExecIn(t.Context(), cli, id, "0", nil, []string{"sh", "-ec", "cat /tmp/discovery/AGENTS.md; cat /tmp/skills/bundle/SKILL.md; cat /tmp/skills/shared/SKILL.md; if echo overwrite > /tmp/skills/shared/SKILL.md 2>/dev/null; then exit 1; fi"}) - if err != nil || code != 0 { - t.Fatalf("read discovery: %d %q %v", code, out, err) - } - for _, want := range []string{"image instructions", "replacement guidance", "SKILL body", "shared body"} { - if !strings.Contains(out, want) { - t.Errorf("missing %q in %q", want, out) - } - } - if strings.Contains(out, "first guidance") || strings.Count(out, "") != 1 { - t.Fatalf("managed guidance was not replaced: %q", out) - } - }) - } -} diff --git a/internal/container/logs.go b/internal/container/logs.go deleted file mode 100644 index 0c4f4293c..000000000 --- a/internal/container/logs.go +++ /dev/null @@ -1,50 +0,0 @@ -package container - -import ( - "context" - "errors" - "io" - "strconv" - - "github.com/moby/moby/api/pkg/stdcopy" - "github.com/moby/moby/client" -) - -// ErrNotRunning reports a container that exists but has stopped. -var ErrNotRunning = errors.New("container is not running") - -// FollowLogs streams a running container's stdout and stderr, merged, from -// its recent past onward. It is read-only: nothing the caller does reaches -// the container. Closing the reader ends the follow. -func FollowLogs(ctx context.Context, cli *client.Client, name string, tail int) (io.ReadCloser, error) { - inspected, err := cli.ContainerInspect(ctx, name, client.ContainerInspectOptions{}) - if err != nil { - return nil, err - } - if inspected.Container.State == nil || !inspected.Container.State.Running { - return nil, ErrNotRunning - } - logs, err := cli.ContainerLogs(ctx, inspected.Container.ID, client.ContainerLogsOptions{ - ShowStdout: true, ShowStderr: true, Follow: true, Tail: strconv.Itoa(tail), - }) - if err != nil { - return nil, err - } - pr, pw := io.Pipe() - go func() { - // A Kit container has no TTY, so the stream is multiplexed. - _, err := stdcopy.StdCopy(pw, pw, logs) - _ = logs.Close() - _ = pw.CloseWithError(err) - }() - return &logStream{PipeReader: pr, logs: logs}, nil -} - -type logStream struct { - *io.PipeReader - logs io.ReadCloser -} - -func (s *logStream) Close() error { - return errors.Join(s.PipeReader.Close(), s.logs.Close()) -} diff --git a/internal/container/logs_test.go b/internal/container/logs_test.go deleted file mode 100644 index 00f6d6528..000000000 --- a/internal/container/logs_test.go +++ /dev/null @@ -1,54 +0,0 @@ -package container - -import ( - "context" - "net/http" - "net/http/httptest" - "strings" - "testing" - "time" - - "github.com/moby/moby/client" -) - -func TestFollowLogsCloseQuietStream(t *testing.T) { - closed := make(chan struct{}) - server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - switch { - case strings.HasSuffix(r.URL.Path, "/json"): - w.Header().Set("Content-Type", "application/json") - _, _ = w.Write([]byte(`{"Id":"running-container","State":{"Running":true}}`)) - case strings.HasSuffix(r.URL.Path, "/logs"): - w.WriteHeader(http.StatusOK) - if err := http.NewResponseController(w).Flush(); err != nil { - t.Error(err) - return - } - <-r.Context().Done() - close(closed) - default: - http.NotFound(w, r) - } - })) - defer server.Close() - cli, err := client.New(client.WithHost(server.URL), client.WithAPIVersion("1.54")) - if err != nil { - t.Fatal(err) - } - defer cli.Close() - ctx, cancel := context.WithCancel(t.Context()) - defer cancel() - stream, err := FollowLogs(ctx, cli, "task-attempt", 200) - if err != nil { - t.Fatal(err) - } - defer stream.Close() - if err := stream.Close(); err != nil { - t.Fatal(err) - } - select { - case <-closed: - case <-time.After(time.Second): - t.Fatal("closing the reader retained the quiet Docker log stream") - } -} diff --git a/internal/container/pool.go b/internal/container/pool.go index b706cf98c..c45953446 100644 --- a/internal/container/pool.go +++ b/internal/container/pool.go @@ -13,7 +13,6 @@ import ( "fmt" "io" "log/slog" - "maps" "os" "path/filepath" "strconv" @@ -321,29 +320,6 @@ func (p *Pool) Acquire(ctx context.Context, image string, mounts []storage.Mount return p.started(ctx, cfg.MaxUptime, resp.ID, name), nil } -// AcquireKit starts a Kit task container under the same concurrency cap, -// lifetime cap and exit watch as Acquire. Release tears it down. -func (p *Pool) AcquireKit(ctx context.Context, s KitSpec) (*Container, error) { - cfg := p.current() - if err := p.ensureImage(ctx, cfg, s.Image); err != nil { - return nil, err - } - if err := p.reserve(cfg.MaxConcurrency); err != nil { - return nil, err - } - s.Labels = maps.Clone(s.Labels) - if s.Labels == nil { - s.Labels = map[string]string{} - } - s.Labels[daemonLabel] = p.cfg.Instance - id, err := StartKit(ctx, p.cli, s) - if err != nil { - p.unreserve() - return nil, err - } - return p.started(ctx, cfg.MaxUptime, id, s.Name), nil -} - func (p *Pool) reserve(maxConcurrency int) error { p.mu.Lock() defer p.mu.Unlock() diff --git a/internal/container/pty.go b/internal/container/pty.go deleted file mode 100644 index 0143fccc0..000000000 --- a/internal/container/pty.go +++ /dev/null @@ -1,61 +0,0 @@ -package container - -import ( - "bufio" - "context" - "net" - - "github.com/moby/moby/client" - - "github.com/samcharles93/archie-core/internal/infrastructure/kit" -) - -// PTY is an interactive exec attached to a container's TTY: writes are -// terminal input, reads are terminal output, and Close closes the hijacked -// socket so a blocked Read unblocks. Docker cannot reattach to a disconnected -// exec, so the caller holds this for the life of the session. -type PTY struct { - execID string - conn net.Conn - reader *bufio.Reader - cli *client.Client -} - -// ExecPTY starts argv in a container as user with exactly env, on a TTY. It -// mirrors ExecIn's explicit user and env: the exec must never inherit the -// container's worker environment. -func ExecPTY(ctx context.Context, cli *client.Client, id, user string, env, argv []string, rows, cols uint) (*PTY, error) { - exec, err := cli.ExecCreate(ctx, id, client.ExecCreateOptions{ - User: user, Env: env, Cmd: argv, WorkingDir: kit.WorkspaceDir, - TTY: true, AttachStdin: true, AttachStdout: true, AttachStderr: true, - ConsoleSize: client.ConsoleSize{Height: rows, Width: cols}, - }) - if err != nil { - return nil, err - } - attached, err := cli.ExecAttach(ctx, exec.ID, client.ExecAttachOptions{TTY: true}) - if err != nil { - return nil, err - } - return &PTY{execID: exec.ID, conn: attached.Conn, reader: attached.Reader, cli: cli}, nil -} - -// Read reads terminal output. A TTY stream is raw, so there is no -// stdout/stderr to demultiplex. -func (p *PTY) Read(b []byte) (int, error) { return p.reader.Read(b) } - -// Write writes terminal input. -func (p *PTY) Write(b []byte) (int, error) { return p.conn.Write(b) } - -// ExitCode reports the exec's exit status. It is meaningful only once the -// output stream has ended. -func (p *PTY) ExitCode(ctx context.Context) (int, error) { - inspected, err := p.cli.ExecInspect(ctx, p.execID, client.ExecInspectOptions{}) - if err != nil { - return 0, err - } - return inspected.ExitCode, nil -} - -// Close closes the hijacked socket, which unblocks a blocked Read. -func (p *PTY) Close() error { return p.conn.Close() } diff --git a/internal/contracts/gateway/v1/task_tools.pb.go b/internal/contracts/gateway/v1/task_tools.pb.go deleted file mode 100644 index 7800f7324..000000000 --- a/internal/contracts/gateway/v1/task_tools.pb.go +++ /dev/null @@ -1,382 +0,0 @@ -// Code generated by protoc-gen-go. DO NOT EDIT. -// versions: -// protoc-gen-go v1.36.11 -// protoc (unknown) -// source: gateway/v1/task_tools.proto - -package gatewayv1 - -import ( - protoreflect "google.golang.org/protobuf/reflect/protoreflect" - protoimpl "google.golang.org/protobuf/runtime/protoimpl" - reflect "reflect" - sync "sync" - unsafe "unsafe" -) - -const ( - // Verify that this generated code is sufficiently up-to-date. - _ = protoimpl.EnforceVersion(20 - protoimpl.MinVersion) - // Verify that runtime/protoimpl is sufficiently up-to-date. - _ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20) -) - -type ListTaskToolsRequest struct { - state protoimpl.MessageState `protogen:"open.v1"` - RunCredential string `protobuf:"bytes,1,opt,name=run_credential,json=runCredential,proto3" json:"run_credential,omitempty"` - unknownFields protoimpl.UnknownFields - sizeCache protoimpl.SizeCache -} - -func (x *ListTaskToolsRequest) Reset() { - *x = ListTaskToolsRequest{} - mi := &file_gateway_v1_task_tools_proto_msgTypes[0] - ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) - ms.StoreMessageInfo(mi) -} - -func (x *ListTaskToolsRequest) String() string { - return protoimpl.X.MessageStringOf(x) -} - -func (*ListTaskToolsRequest) ProtoMessage() {} - -func (x *ListTaskToolsRequest) ProtoReflect() protoreflect.Message { - mi := &file_gateway_v1_task_tools_proto_msgTypes[0] - if x != nil { - ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) - if ms.LoadMessageInfo() == nil { - ms.StoreMessageInfo(mi) - } - return ms - } - return mi.MessageOf(x) -} - -// Deprecated: Use ListTaskToolsRequest.ProtoReflect.Descriptor instead. -func (*ListTaskToolsRequest) Descriptor() ([]byte, []int) { - return file_gateway_v1_task_tools_proto_rawDescGZIP(), []int{0} -} - -func (x *ListTaskToolsRequest) GetRunCredential() string { - if x != nil { - return x.RunCredential - } - return "" -} - -type ListTaskToolsResponse struct { - state protoimpl.MessageState `protogen:"open.v1"` - ToolsJson []byte `protobuf:"bytes,1,opt,name=tools_json,json=toolsJson,proto3" json:"tools_json,omitempty"` - unknownFields protoimpl.UnknownFields - sizeCache protoimpl.SizeCache -} - -func (x *ListTaskToolsResponse) Reset() { - *x = ListTaskToolsResponse{} - mi := &file_gateway_v1_task_tools_proto_msgTypes[1] - ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) - ms.StoreMessageInfo(mi) -} - -func (x *ListTaskToolsResponse) String() string { - return protoimpl.X.MessageStringOf(x) -} - -func (*ListTaskToolsResponse) ProtoMessage() {} - -func (x *ListTaskToolsResponse) ProtoReflect() protoreflect.Message { - mi := &file_gateway_v1_task_tools_proto_msgTypes[1] - if x != nil { - ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) - if ms.LoadMessageInfo() == nil { - ms.StoreMessageInfo(mi) - } - return ms - } - return mi.MessageOf(x) -} - -// Deprecated: Use ListTaskToolsResponse.ProtoReflect.Descriptor instead. -func (*ListTaskToolsResponse) Descriptor() ([]byte, []int) { - return file_gateway_v1_task_tools_proto_rawDescGZIP(), []int{1} -} - -func (x *ListTaskToolsResponse) GetToolsJson() []byte { - if x != nil { - return x.ToolsJson - } - return nil -} - -type CallTaskToolRequest struct { - state protoimpl.MessageState `protogen:"open.v1"` - RunCredential string `protobuf:"bytes,1,opt,name=run_credential,json=runCredential,proto3" json:"run_credential,omitempty"` - Name string `protobuf:"bytes,2,opt,name=name,proto3" json:"name,omitempty"` - ArgumentsJson []byte `protobuf:"bytes,3,opt,name=arguments_json,json=argumentsJson,proto3" json:"arguments_json,omitempty"` - unknownFields protoimpl.UnknownFields - sizeCache protoimpl.SizeCache -} - -func (x *CallTaskToolRequest) Reset() { - *x = CallTaskToolRequest{} - mi := &file_gateway_v1_task_tools_proto_msgTypes[2] - ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) - ms.StoreMessageInfo(mi) -} - -func (x *CallTaskToolRequest) String() string { - return protoimpl.X.MessageStringOf(x) -} - -func (*CallTaskToolRequest) ProtoMessage() {} - -func (x *CallTaskToolRequest) ProtoReflect() protoreflect.Message { - mi := &file_gateway_v1_task_tools_proto_msgTypes[2] - if x != nil { - ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) - if ms.LoadMessageInfo() == nil { - ms.StoreMessageInfo(mi) - } - return ms - } - return mi.MessageOf(x) -} - -// Deprecated: Use CallTaskToolRequest.ProtoReflect.Descriptor instead. -func (*CallTaskToolRequest) Descriptor() ([]byte, []int) { - return file_gateway_v1_task_tools_proto_rawDescGZIP(), []int{2} -} - -func (x *CallTaskToolRequest) GetRunCredential() string { - if x != nil { - return x.RunCredential - } - return "" -} - -func (x *CallTaskToolRequest) GetName() string { - if x != nil { - return x.Name - } - return "" -} - -func (x *CallTaskToolRequest) GetArgumentsJson() []byte { - if x != nil { - return x.ArgumentsJson - } - return nil -} - -type CallTaskToolResponse struct { - state protoimpl.MessageState `protogen:"open.v1"` - ResultJson []byte `protobuf:"bytes,1,opt,name=result_json,json=resultJson,proto3" json:"result_json,omitempty"` - unknownFields protoimpl.UnknownFields - sizeCache protoimpl.SizeCache -} - -func (x *CallTaskToolResponse) Reset() { - *x = CallTaskToolResponse{} - mi := &file_gateway_v1_task_tools_proto_msgTypes[3] - ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) - ms.StoreMessageInfo(mi) -} - -func (x *CallTaskToolResponse) String() string { - return protoimpl.X.MessageStringOf(x) -} - -func (*CallTaskToolResponse) ProtoMessage() {} - -func (x *CallTaskToolResponse) ProtoReflect() protoreflect.Message { - mi := &file_gateway_v1_task_tools_proto_msgTypes[3] - if x != nil { - ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) - if ms.LoadMessageInfo() == nil { - ms.StoreMessageInfo(mi) - } - return ms - } - return mi.MessageOf(x) -} - -// Deprecated: Use CallTaskToolResponse.ProtoReflect.Descriptor instead. -func (*CallTaskToolResponse) Descriptor() ([]byte, []int) { - return file_gateway_v1_task_tools_proto_rawDescGZIP(), []int{3} -} - -func (x *CallTaskToolResponse) GetResultJson() []byte { - if x != nil { - return x.ResultJson - } - return nil -} - -type CloseTaskToolsRequest struct { - state protoimpl.MessageState `protogen:"open.v1"` - RunCredential string `protobuf:"bytes,1,opt,name=run_credential,json=runCredential,proto3" json:"run_credential,omitempty"` - unknownFields protoimpl.UnknownFields - sizeCache protoimpl.SizeCache -} - -func (x *CloseTaskToolsRequest) Reset() { - *x = CloseTaskToolsRequest{} - mi := &file_gateway_v1_task_tools_proto_msgTypes[4] - ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) - ms.StoreMessageInfo(mi) -} - -func (x *CloseTaskToolsRequest) String() string { - return protoimpl.X.MessageStringOf(x) -} - -func (*CloseTaskToolsRequest) ProtoMessage() {} - -func (x *CloseTaskToolsRequest) ProtoReflect() protoreflect.Message { - mi := &file_gateway_v1_task_tools_proto_msgTypes[4] - if x != nil { - ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) - if ms.LoadMessageInfo() == nil { - ms.StoreMessageInfo(mi) - } - return ms - } - return mi.MessageOf(x) -} - -// Deprecated: Use CloseTaskToolsRequest.ProtoReflect.Descriptor instead. -func (*CloseTaskToolsRequest) Descriptor() ([]byte, []int) { - return file_gateway_v1_task_tools_proto_rawDescGZIP(), []int{4} -} - -func (x *CloseTaskToolsRequest) GetRunCredential() string { - if x != nil { - return x.RunCredential - } - return "" -} - -type CloseTaskToolsResponse struct { - state protoimpl.MessageState `protogen:"open.v1"` - unknownFields protoimpl.UnknownFields - sizeCache protoimpl.SizeCache -} - -func (x *CloseTaskToolsResponse) Reset() { - *x = CloseTaskToolsResponse{} - mi := &file_gateway_v1_task_tools_proto_msgTypes[5] - ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) - ms.StoreMessageInfo(mi) -} - -func (x *CloseTaskToolsResponse) String() string { - return protoimpl.X.MessageStringOf(x) -} - -func (*CloseTaskToolsResponse) ProtoMessage() {} - -func (x *CloseTaskToolsResponse) ProtoReflect() protoreflect.Message { - mi := &file_gateway_v1_task_tools_proto_msgTypes[5] - if x != nil { - ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) - if ms.LoadMessageInfo() == nil { - ms.StoreMessageInfo(mi) - } - return ms - } - return mi.MessageOf(x) -} - -// Deprecated: Use CloseTaskToolsResponse.ProtoReflect.Descriptor instead. -func (*CloseTaskToolsResponse) Descriptor() ([]byte, []int) { - return file_gateway_v1_task_tools_proto_rawDescGZIP(), []int{5} -} - -var File_gateway_v1_task_tools_proto protoreflect.FileDescriptor - -const file_gateway_v1_task_tools_proto_rawDesc = "" + - "\n" + - "\x1bgateway/v1/task_tools.proto\x12\n" + - "gateway.v1\"=\n" + - "\x14ListTaskToolsRequest\x12%\n" + - "\x0erun_credential\x18\x01 \x01(\tR\rrunCredential\"6\n" + - "\x15ListTaskToolsResponse\x12\x1d\n" + - "\n" + - "tools_json\x18\x01 \x01(\fR\ttoolsJson\"w\n" + - "\x13CallTaskToolRequest\x12%\n" + - "\x0erun_credential\x18\x01 \x01(\tR\rrunCredential\x12\x12\n" + - "\x04name\x18\x02 \x01(\tR\x04name\x12%\n" + - "\x0earguments_json\x18\x03 \x01(\fR\rargumentsJson\"7\n" + - "\x14CallTaskToolResponse\x12\x1f\n" + - "\vresult_json\x18\x01 \x01(\fR\n" + - "resultJson\">\n" + - "\x15CloseTaskToolsRequest\x12%\n" + - "\x0erun_credential\x18\x01 \x01(\tR\rrunCredential\"\x18\n" + - "\x16CloseTaskToolsResponse2\x94\x02\n" + - "\x10TaskToolsService\x12T\n" + - "\rListTaskTools\x12 .gateway.v1.ListTaskToolsRequest\x1a!.gateway.v1.ListTaskToolsResponse\x12Q\n" + - "\fCallTaskTool\x12\x1f.gateway.v1.CallTaskToolRequest\x1a .gateway.v1.CallTaskToolResponse\x12W\n" + - "\x0eCloseTaskTools\x12!.gateway.v1.CloseTaskToolsRequest\x1a\".gateway.v1.CloseTaskToolsResponseB\xb6\x01\n" + - "\x0ecom.gateway.v1B\x0eTaskToolsProtoP\x01ZKgithub.com/samcharles93/archie-core/internal/contracts/gateway/v1;gatewayv1\xa2\x02\x03GXX\xaa\x02\n" + - "Gateway.V1\xca\x02\n" + - "Gateway\\V1\xe2\x02\x16Gateway\\V1\\GPBMetadata\xea\x02\vGateway::V1b\x06proto3" - -var ( - file_gateway_v1_task_tools_proto_rawDescOnce sync.Once - file_gateway_v1_task_tools_proto_rawDescData []byte -) - -func file_gateway_v1_task_tools_proto_rawDescGZIP() []byte { - file_gateway_v1_task_tools_proto_rawDescOnce.Do(func() { - file_gateway_v1_task_tools_proto_rawDescData = protoimpl.X.CompressGZIP(unsafe.Slice(unsafe.StringData(file_gateway_v1_task_tools_proto_rawDesc), len(file_gateway_v1_task_tools_proto_rawDesc))) - }) - return file_gateway_v1_task_tools_proto_rawDescData -} - -var file_gateway_v1_task_tools_proto_msgTypes = make([]protoimpl.MessageInfo, 6) -var file_gateway_v1_task_tools_proto_goTypes = []any{ - (*ListTaskToolsRequest)(nil), // 0: gateway.v1.ListTaskToolsRequest - (*ListTaskToolsResponse)(nil), // 1: gateway.v1.ListTaskToolsResponse - (*CallTaskToolRequest)(nil), // 2: gateway.v1.CallTaskToolRequest - (*CallTaskToolResponse)(nil), // 3: gateway.v1.CallTaskToolResponse - (*CloseTaskToolsRequest)(nil), // 4: gateway.v1.CloseTaskToolsRequest - (*CloseTaskToolsResponse)(nil), // 5: gateway.v1.CloseTaskToolsResponse -} -var file_gateway_v1_task_tools_proto_depIdxs = []int32{ - 0, // 0: gateway.v1.TaskToolsService.ListTaskTools:input_type -> gateway.v1.ListTaskToolsRequest - 2, // 1: gateway.v1.TaskToolsService.CallTaskTool:input_type -> gateway.v1.CallTaskToolRequest - 4, // 2: gateway.v1.TaskToolsService.CloseTaskTools:input_type -> gateway.v1.CloseTaskToolsRequest - 1, // 3: gateway.v1.TaskToolsService.ListTaskTools:output_type -> gateway.v1.ListTaskToolsResponse - 3, // 4: gateway.v1.TaskToolsService.CallTaskTool:output_type -> gateway.v1.CallTaskToolResponse - 5, // 5: gateway.v1.TaskToolsService.CloseTaskTools:output_type -> gateway.v1.CloseTaskToolsResponse - 3, // [3:6] is the sub-list for method output_type - 0, // [0:3] is the sub-list for method input_type - 0, // [0:0] is the sub-list for extension type_name - 0, // [0:0] is the sub-list for extension extendee - 0, // [0:0] is the sub-list for field type_name -} - -func init() { file_gateway_v1_task_tools_proto_init() } -func file_gateway_v1_task_tools_proto_init() { - if File_gateway_v1_task_tools_proto != nil { - return - } - type x struct{} - out := protoimpl.TypeBuilder{ - File: protoimpl.DescBuilder{ - GoPackagePath: reflect.TypeOf(x{}).PkgPath(), - RawDescriptor: unsafe.Slice(unsafe.StringData(file_gateway_v1_task_tools_proto_rawDesc), len(file_gateway_v1_task_tools_proto_rawDesc)), - NumEnums: 0, - NumMessages: 6, - NumExtensions: 0, - NumServices: 1, - }, - GoTypes: file_gateway_v1_task_tools_proto_goTypes, - DependencyIndexes: file_gateway_v1_task_tools_proto_depIdxs, - MessageInfos: file_gateway_v1_task_tools_proto_msgTypes, - }.Build() - File_gateway_v1_task_tools_proto = out.File - file_gateway_v1_task_tools_proto_goTypes = nil - file_gateway_v1_task_tools_proto_depIdxs = nil -} diff --git a/internal/contracts/gateway/v1/task_tools_grpc.pb.go b/internal/contracts/gateway/v1/task_tools_grpc.pb.go deleted file mode 100644 index 0e64a957a..000000000 --- a/internal/contracts/gateway/v1/task_tools_grpc.pb.go +++ /dev/null @@ -1,203 +0,0 @@ -// Code generated by protoc-gen-go-grpc. DO NOT EDIT. -// versions: -// - protoc-gen-go-grpc v1.6.2 -// - protoc (unknown) -// source: gateway/v1/task_tools.proto - -package gatewayv1 - -import ( - context "context" - grpc "google.golang.org/grpc" - codes "google.golang.org/grpc/codes" - status "google.golang.org/grpc/status" -) - -// This is a compile-time assertion to ensure that this generated file -// is compatible with the grpc package it is being compiled against. -// Requires gRPC-Go v1.64.0 or later. -const _ = grpc.SupportPackageIsVersion9 - -const ( - TaskToolsService_ListTaskTools_FullMethodName = "/gateway.v1.TaskToolsService/ListTaskTools" - TaskToolsService_CallTaskTool_FullMethodName = "/gateway.v1.TaskToolsService/CallTaskTool" - TaskToolsService_CloseTaskTools_FullMethodName = "/gateway.v1.TaskToolsService/CloseTaskTools" -) - -// TaskToolsServiceClient is the client API for TaskToolsService service. -// -// For semantics around ctx use and closing/ending streaming RPCs, please refer to https://pkg.go.dev/google.golang.org/grpc/?tab=doc#ClientConn.NewStream. -// -// TaskToolsService executes central tools for a live task grant. Only the -// daemon calls it; identities, orgs and permissions are resolved by the server. -type TaskToolsServiceClient interface { - ListTaskTools(ctx context.Context, in *ListTaskToolsRequest, opts ...grpc.CallOption) (*ListTaskToolsResponse, error) - CallTaskTool(ctx context.Context, in *CallTaskToolRequest, opts ...grpc.CallOption) (*CallTaskToolResponse, error) - CloseTaskTools(ctx context.Context, in *CloseTaskToolsRequest, opts ...grpc.CallOption) (*CloseTaskToolsResponse, error) -} - -type taskToolsServiceClient struct { - cc grpc.ClientConnInterface -} - -func NewTaskToolsServiceClient(cc grpc.ClientConnInterface) TaskToolsServiceClient { - return &taskToolsServiceClient{cc} -} - -func (c *taskToolsServiceClient) ListTaskTools(ctx context.Context, in *ListTaskToolsRequest, opts ...grpc.CallOption) (*ListTaskToolsResponse, error) { - cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...) - out := new(ListTaskToolsResponse) - err := c.cc.Invoke(ctx, TaskToolsService_ListTaskTools_FullMethodName, in, out, cOpts...) - if err != nil { - return nil, err - } - return out, nil -} - -func (c *taskToolsServiceClient) CallTaskTool(ctx context.Context, in *CallTaskToolRequest, opts ...grpc.CallOption) (*CallTaskToolResponse, error) { - cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...) - out := new(CallTaskToolResponse) - err := c.cc.Invoke(ctx, TaskToolsService_CallTaskTool_FullMethodName, in, out, cOpts...) - if err != nil { - return nil, err - } - return out, nil -} - -func (c *taskToolsServiceClient) CloseTaskTools(ctx context.Context, in *CloseTaskToolsRequest, opts ...grpc.CallOption) (*CloseTaskToolsResponse, error) { - cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...) - out := new(CloseTaskToolsResponse) - err := c.cc.Invoke(ctx, TaskToolsService_CloseTaskTools_FullMethodName, in, out, cOpts...) - if err != nil { - return nil, err - } - return out, nil -} - -// TaskToolsServiceServer is the server API for TaskToolsService service. -// All implementations must embed UnimplementedTaskToolsServiceServer -// for forward compatibility. -// -// TaskToolsService executes central tools for a live task grant. Only the -// daemon calls it; identities, orgs and permissions are resolved by the server. -type TaskToolsServiceServer interface { - ListTaskTools(context.Context, *ListTaskToolsRequest) (*ListTaskToolsResponse, error) - CallTaskTool(context.Context, *CallTaskToolRequest) (*CallTaskToolResponse, error) - CloseTaskTools(context.Context, *CloseTaskToolsRequest) (*CloseTaskToolsResponse, error) - mustEmbedUnimplementedTaskToolsServiceServer() -} - -// UnimplementedTaskToolsServiceServer must be embedded to have -// forward compatible implementations. -// -// NOTE: this should be embedded by value instead of pointer to avoid a nil -// pointer dereference when methods are called. -type UnimplementedTaskToolsServiceServer struct{} - -func (UnimplementedTaskToolsServiceServer) ListTaskTools(context.Context, *ListTaskToolsRequest) (*ListTaskToolsResponse, error) { - return nil, status.Error(codes.Unimplemented, "method ListTaskTools not implemented") -} -func (UnimplementedTaskToolsServiceServer) CallTaskTool(context.Context, *CallTaskToolRequest) (*CallTaskToolResponse, error) { - return nil, status.Error(codes.Unimplemented, "method CallTaskTool not implemented") -} -func (UnimplementedTaskToolsServiceServer) CloseTaskTools(context.Context, *CloseTaskToolsRequest) (*CloseTaskToolsResponse, error) { - return nil, status.Error(codes.Unimplemented, "method CloseTaskTools not implemented") -} -func (UnimplementedTaskToolsServiceServer) mustEmbedUnimplementedTaskToolsServiceServer() {} -func (UnimplementedTaskToolsServiceServer) testEmbeddedByValue() {} - -// UnsafeTaskToolsServiceServer may be embedded to opt out of forward compatibility for this service. -// Use of this interface is not recommended, as added methods to TaskToolsServiceServer will -// result in compilation errors. -type UnsafeTaskToolsServiceServer interface { - mustEmbedUnimplementedTaskToolsServiceServer() -} - -func RegisterTaskToolsServiceServer(s grpc.ServiceRegistrar, srv TaskToolsServiceServer) { - // If the following call panics, it indicates UnimplementedTaskToolsServiceServer was - // embedded by pointer and is nil. This will cause panics if an - // unimplemented method is ever invoked, so we test this at initialization - // time to prevent it from happening at runtime later due to I/O. - if t, ok := srv.(interface{ testEmbeddedByValue() }); ok { - t.testEmbeddedByValue() - } - s.RegisterService(&TaskToolsService_ServiceDesc, srv) -} - -func _TaskToolsService_ListTaskTools_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { - in := new(ListTaskToolsRequest) - if err := dec(in); err != nil { - return nil, err - } - if interceptor == nil { - return srv.(TaskToolsServiceServer).ListTaskTools(ctx, in) - } - info := &grpc.UnaryServerInfo{ - Server: srv, - FullMethod: TaskToolsService_ListTaskTools_FullMethodName, - } - handler := func(ctx context.Context, req interface{}) (interface{}, error) { - return srv.(TaskToolsServiceServer).ListTaskTools(ctx, req.(*ListTaskToolsRequest)) - } - return interceptor(ctx, in, info, handler) -} - -func _TaskToolsService_CallTaskTool_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { - in := new(CallTaskToolRequest) - if err := dec(in); err != nil { - return nil, err - } - if interceptor == nil { - return srv.(TaskToolsServiceServer).CallTaskTool(ctx, in) - } - info := &grpc.UnaryServerInfo{ - Server: srv, - FullMethod: TaskToolsService_CallTaskTool_FullMethodName, - } - handler := func(ctx context.Context, req interface{}) (interface{}, error) { - return srv.(TaskToolsServiceServer).CallTaskTool(ctx, req.(*CallTaskToolRequest)) - } - return interceptor(ctx, in, info, handler) -} - -func _TaskToolsService_CloseTaskTools_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { - in := new(CloseTaskToolsRequest) - if err := dec(in); err != nil { - return nil, err - } - if interceptor == nil { - return srv.(TaskToolsServiceServer).CloseTaskTools(ctx, in) - } - info := &grpc.UnaryServerInfo{ - Server: srv, - FullMethod: TaskToolsService_CloseTaskTools_FullMethodName, - } - handler := func(ctx context.Context, req interface{}) (interface{}, error) { - return srv.(TaskToolsServiceServer).CloseTaskTools(ctx, req.(*CloseTaskToolsRequest)) - } - return interceptor(ctx, in, info, handler) -} - -// TaskToolsService_ServiceDesc is the grpc.ServiceDesc for TaskToolsService service. -// It's only intended for direct use with grpc.RegisterService, -// and not to be introspected or modified (even as a copy) -var TaskToolsService_ServiceDesc = grpc.ServiceDesc{ - ServiceName: "gateway.v1.TaskToolsService", - HandlerType: (*TaskToolsServiceServer)(nil), - Methods: []grpc.MethodDesc{ - { - MethodName: "ListTaskTools", - Handler: _TaskToolsService_ListTaskTools_Handler, - }, - { - MethodName: "CallTaskTool", - Handler: _TaskToolsService_CallTaskTool_Handler, - }, - { - MethodName: "CloseTaskTools", - Handler: _TaskToolsService_CloseTaskTools_Handler, - }, - }, - Streams: []grpc.StreamDesc{}, - Metadata: "gateway/v1/task_tools.proto", -} diff --git a/internal/contracts/harness/v1/harness.pb.go b/internal/contracts/harness/v1/harness.pb.go deleted file mode 100644 index 895d3c138..000000000 --- a/internal/contracts/harness/v1/harness.pb.go +++ /dev/null @@ -1,576 +0,0 @@ -// Code generated by protoc-gen-go. DO NOT EDIT. -// versions: -// protoc-gen-go v1.36.11 -// protoc (unknown) -// source: harness/v1/harness.proto - -package harnessv1 - -import ( - protoreflect "google.golang.org/protobuf/reflect/protoreflect" - protoimpl "google.golang.org/protobuf/runtime/protoimpl" - reflect "reflect" - sync "sync" - unsafe "unsafe" -) - -const ( - // Verify that this generated code is sufficiently up-to-date. - _ = protoimpl.EnforceVersion(20 - protoimpl.MinVersion) - // Verify that runtime/protoimpl is sufficiently up-to-date. - _ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20) -) - -// OpenRequest is one client frame. The first frame must be target; a frame -// with no field set is a keepalive the server ignores. -type OpenRequest struct { - state protoimpl.MessageState `protogen:"open.v1"` - // Types that are valid to be assigned to Frame: - // - // *OpenRequest_Target - // *OpenRequest_Stdin - Frame isOpenRequest_Frame `protobuf_oneof:"frame"` - // size is the terminal size the client wants, in character cells. The - // dashboard sends it with the target frame so the PTY is created at the - // right size; the daemon falls back to its own default when it is absent. - Size *ConsoleSize `protobuf:"bytes,3,opt,name=size,proto3" json:"size,omitempty"` - unknownFields protoimpl.UnknownFields - sizeCache protoimpl.SizeCache -} - -func (x *OpenRequest) Reset() { - *x = OpenRequest{} - mi := &file_harness_v1_harness_proto_msgTypes[0] - ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) - ms.StoreMessageInfo(mi) -} - -func (x *OpenRequest) String() string { - return protoimpl.X.MessageStringOf(x) -} - -func (*OpenRequest) ProtoMessage() {} - -func (x *OpenRequest) ProtoReflect() protoreflect.Message { - mi := &file_harness_v1_harness_proto_msgTypes[0] - if x != nil { - ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) - if ms.LoadMessageInfo() == nil { - ms.StoreMessageInfo(mi) - } - return ms - } - return mi.MessageOf(x) -} - -// Deprecated: Use OpenRequest.ProtoReflect.Descriptor instead. -func (*OpenRequest) Descriptor() ([]byte, []int) { - return file_harness_v1_harness_proto_rawDescGZIP(), []int{0} -} - -func (x *OpenRequest) GetFrame() isOpenRequest_Frame { - if x != nil { - return x.Frame - } - return nil -} - -func (x *OpenRequest) GetTarget() *OpenTarget { - if x != nil { - if x, ok := x.Frame.(*OpenRequest_Target); ok { - return x.Target - } - } - return nil -} - -func (x *OpenRequest) GetStdin() []byte { - if x != nil { - if x, ok := x.Frame.(*OpenRequest_Stdin); ok { - return x.Stdin - } - } - return nil -} - -func (x *OpenRequest) GetSize() *ConsoleSize { - if x != nil { - return x.Size - } - return nil -} - -type isOpenRequest_Frame interface { - isOpenRequest_Frame() -} - -type OpenRequest_Target struct { - // target names the session and must be the first frame. - Target *OpenTarget `protobuf:"bytes,1,opt,name=target,proto3,oneof"` -} - -type OpenRequest_Stdin struct { - // stdin is terminal input bytes. - Stdin []byte `protobuf:"bytes,2,opt,name=stdin,proto3,oneof"` -} - -func (*OpenRequest_Target) isOpenRequest_Frame() {} - -func (*OpenRequest_Stdin) isOpenRequest_Frame() {} - -// OpenResponse is one server frame. -type OpenResponse struct { - state protoimpl.MessageState `protogen:"open.v1"` - // Types that are valid to be assigned to Frame: - // - // *OpenResponse_Stdout - // *OpenResponse_ExitCode - // *OpenResponse_Error - Frame isOpenResponse_Frame `protobuf_oneof:"frame"` - unknownFields protoimpl.UnknownFields - sizeCache protoimpl.SizeCache -} - -func (x *OpenResponse) Reset() { - *x = OpenResponse{} - mi := &file_harness_v1_harness_proto_msgTypes[1] - ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) - ms.StoreMessageInfo(mi) -} - -func (x *OpenResponse) String() string { - return protoimpl.X.MessageStringOf(x) -} - -func (*OpenResponse) ProtoMessage() {} - -func (x *OpenResponse) ProtoReflect() protoreflect.Message { - mi := &file_harness_v1_harness_proto_msgTypes[1] - if x != nil { - ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) - if ms.LoadMessageInfo() == nil { - ms.StoreMessageInfo(mi) - } - return ms - } - return mi.MessageOf(x) -} - -// Deprecated: Use OpenResponse.ProtoReflect.Descriptor instead. -func (*OpenResponse) Descriptor() ([]byte, []int) { - return file_harness_v1_harness_proto_rawDescGZIP(), []int{1} -} - -func (x *OpenResponse) GetFrame() isOpenResponse_Frame { - if x != nil { - return x.Frame - } - return nil -} - -func (x *OpenResponse) GetStdout() []byte { - if x != nil { - if x, ok := x.Frame.(*OpenResponse_Stdout); ok { - return x.Stdout - } - } - return nil -} - -func (x *OpenResponse) GetExitCode() int32 { - if x != nil { - if x, ok := x.Frame.(*OpenResponse_ExitCode); ok { - return x.ExitCode - } - } - return 0 -} - -func (x *OpenResponse) GetError() string { - if x != nil { - if x, ok := x.Frame.(*OpenResponse_Error); ok { - return x.Error - } - } - return "" -} - -type isOpenResponse_Frame interface { - isOpenResponse_Frame() -} - -type OpenResponse_Stdout struct { - // stdout is terminal output bytes. - Stdout []byte `protobuf:"bytes,1,opt,name=stdout,proto3,oneof"` -} - -type OpenResponse_ExitCode struct { - // exit_code ends the session: the command's exit status. - ExitCode int32 `protobuf:"varint,2,opt,name=exit_code,json=exitCode,proto3,oneof"` -} - -type OpenResponse_Error struct { - // error ends the session when it could not be opened or was aborted. - Error string `protobuf:"bytes,3,opt,name=error,proto3,oneof"` -} - -func (*OpenResponse_Stdout) isOpenResponse_Frame() {} - -func (*OpenResponse_ExitCode) isOpenResponse_Frame() {} - -func (*OpenResponse_Error) isOpenResponse_Frame() {} - -// OpenTarget addresses one session. A setup session is addressed by Kit -// profile and is ephemeral; a run session is addressed by task and attempt and -// is bounded by the task. -type OpenTarget struct { - state protoimpl.MessageState `protogen:"open.v1"` - Org string `protobuf:"bytes,1,opt,name=org,proto3" json:"org,omitempty"` - // Types that are valid to be assigned to Target: - // - // *OpenTarget_Setup - // *OpenTarget_Run - Target isOpenTarget_Target `protobuf_oneof:"target"` - unknownFields protoimpl.UnknownFields - sizeCache protoimpl.SizeCache -} - -func (x *OpenTarget) Reset() { - *x = OpenTarget{} - mi := &file_harness_v1_harness_proto_msgTypes[2] - ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) - ms.StoreMessageInfo(mi) -} - -func (x *OpenTarget) String() string { - return protoimpl.X.MessageStringOf(x) -} - -func (*OpenTarget) ProtoMessage() {} - -func (x *OpenTarget) ProtoReflect() protoreflect.Message { - mi := &file_harness_v1_harness_proto_msgTypes[2] - if x != nil { - ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) - if ms.LoadMessageInfo() == nil { - ms.StoreMessageInfo(mi) - } - return ms - } - return mi.MessageOf(x) -} - -// Deprecated: Use OpenTarget.ProtoReflect.Descriptor instead. -func (*OpenTarget) Descriptor() ([]byte, []int) { - return file_harness_v1_harness_proto_rawDescGZIP(), []int{2} -} - -func (x *OpenTarget) GetOrg() string { - if x != nil { - return x.Org - } - return "" -} - -func (x *OpenTarget) GetTarget() isOpenTarget_Target { - if x != nil { - return x.Target - } - return nil -} - -func (x *OpenTarget) GetSetup() *SetupTarget { - if x != nil { - if x, ok := x.Target.(*OpenTarget_Setup); ok { - return x.Setup - } - } - return nil -} - -func (x *OpenTarget) GetRun() *RunTarget { - if x != nil { - if x, ok := x.Target.(*OpenTarget_Run); ok { - return x.Run - } - } - return nil -} - -type isOpenTarget_Target interface { - isOpenTarget_Target() -} - -type OpenTarget_Setup struct { - Setup *SetupTarget `protobuf:"bytes,2,opt,name=setup,proto3,oneof"` -} - -type OpenTarget_Run struct { - Run *RunTarget `protobuf:"bytes,3,opt,name=run,proto3,oneof"` -} - -func (*OpenTarget_Setup) isOpenTarget_Target() {} - -func (*OpenTarget_Run) isOpenTarget_Target() {} - -// SetupTarget runs the CLI's own login in an ephemeral container built from -// the named agent profile's Kit. -type SetupTarget struct { - state protoimpl.MessageState `protogen:"open.v1"` - Profile string `protobuf:"bytes,1,opt,name=profile,proto3" json:"profile,omitempty"` - unknownFields protoimpl.UnknownFields - sizeCache protoimpl.SizeCache -} - -func (x *SetupTarget) Reset() { - *x = SetupTarget{} - mi := &file_harness_v1_harness_proto_msgTypes[3] - ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) - ms.StoreMessageInfo(mi) -} - -func (x *SetupTarget) String() string { - return protoimpl.X.MessageStringOf(x) -} - -func (*SetupTarget) ProtoMessage() {} - -func (x *SetupTarget) ProtoReflect() protoreflect.Message { - mi := &file_harness_v1_harness_proto_msgTypes[3] - if x != nil { - ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) - if ms.LoadMessageInfo() == nil { - ms.StoreMessageInfo(mi) - } - return ms - } - return mi.MessageOf(x) -} - -// Deprecated: Use SetupTarget.ProtoReflect.Descriptor instead. -func (*SetupTarget) Descriptor() ([]byte, []int) { - return file_harness_v1_harness_proto_rawDescGZIP(), []int{3} -} - -func (x *SetupTarget) GetProfile() string { - if x != nil { - return x.Profile - } - return "" -} - -// RunTarget is a running task's sandbox. The daemon follows its output -// read-only and ignores stdin; a finished or unknown task or attempt is -// NotFound, with the reason in the status message. -type RunTarget struct { - state protoimpl.MessageState `protogen:"open.v1"` - TaskId int64 `protobuf:"varint,1,opt,name=task_id,json=taskId,proto3" json:"task_id,omitempty"` - Attempt int64 `protobuf:"varint,2,opt,name=attempt,proto3" json:"attempt,omitempty"` - unknownFields protoimpl.UnknownFields - sizeCache protoimpl.SizeCache -} - -func (x *RunTarget) Reset() { - *x = RunTarget{} - mi := &file_harness_v1_harness_proto_msgTypes[4] - ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) - ms.StoreMessageInfo(mi) -} - -func (x *RunTarget) String() string { - return protoimpl.X.MessageStringOf(x) -} - -func (*RunTarget) ProtoMessage() {} - -func (x *RunTarget) ProtoReflect() protoreflect.Message { - mi := &file_harness_v1_harness_proto_msgTypes[4] - if x != nil { - ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) - if ms.LoadMessageInfo() == nil { - ms.StoreMessageInfo(mi) - } - return ms - } - return mi.MessageOf(x) -} - -// Deprecated: Use RunTarget.ProtoReflect.Descriptor instead. -func (*RunTarget) Descriptor() ([]byte, []int) { - return file_harness_v1_harness_proto_rawDescGZIP(), []int{4} -} - -func (x *RunTarget) GetTaskId() int64 { - if x != nil { - return x.TaskId - } - return 0 -} - -func (x *RunTarget) GetAttempt() int64 { - if x != nil { - return x.Attempt - } - return 0 -} - -// ConsoleSize is a terminal size in character cells. -type ConsoleSize struct { - state protoimpl.MessageState `protogen:"open.v1"` - Rows uint32 `protobuf:"varint,1,opt,name=rows,proto3" json:"rows,omitempty"` - Cols uint32 `protobuf:"varint,2,opt,name=cols,proto3" json:"cols,omitempty"` - unknownFields protoimpl.UnknownFields - sizeCache protoimpl.SizeCache -} - -func (x *ConsoleSize) Reset() { - *x = ConsoleSize{} - mi := &file_harness_v1_harness_proto_msgTypes[5] - ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) - ms.StoreMessageInfo(mi) -} - -func (x *ConsoleSize) String() string { - return protoimpl.X.MessageStringOf(x) -} - -func (*ConsoleSize) ProtoMessage() {} - -func (x *ConsoleSize) ProtoReflect() protoreflect.Message { - mi := &file_harness_v1_harness_proto_msgTypes[5] - if x != nil { - ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) - if ms.LoadMessageInfo() == nil { - ms.StoreMessageInfo(mi) - } - return ms - } - return mi.MessageOf(x) -} - -// Deprecated: Use ConsoleSize.ProtoReflect.Descriptor instead. -func (*ConsoleSize) Descriptor() ([]byte, []int) { - return file_harness_v1_harness_proto_rawDescGZIP(), []int{5} -} - -func (x *ConsoleSize) GetRows() uint32 { - if x != nil { - return x.Rows - } - return 0 -} - -func (x *ConsoleSize) GetCols() uint32 { - if x != nil { - return x.Cols - } - return 0 -} - -var File_harness_v1_harness_proto protoreflect.FileDescriptor - -const file_harness_v1_harness_proto_rawDesc = "" + - "\n" + - "\x18harness/v1/harness.proto\x12\n" + - "harness.v1\"\x8d\x01\n" + - "\vOpenRequest\x120\n" + - "\x06target\x18\x01 \x01(\v2\x16.harness.v1.OpenTargetH\x00R\x06target\x12\x16\n" + - "\x05stdin\x18\x02 \x01(\fH\x00R\x05stdin\x12+\n" + - "\x04size\x18\x03 \x01(\v2\x17.harness.v1.ConsoleSizeR\x04sizeB\a\n" + - "\x05frame\"h\n" + - "\fOpenResponse\x12\x18\n" + - "\x06stdout\x18\x01 \x01(\fH\x00R\x06stdout\x12\x1d\n" + - "\texit_code\x18\x02 \x01(\x05H\x00R\bexitCode\x12\x16\n" + - "\x05error\x18\x03 \x01(\tH\x00R\x05errorB\a\n" + - "\x05frame\"\x84\x01\n" + - "\n" + - "OpenTarget\x12\x10\n" + - "\x03org\x18\x01 \x01(\tR\x03org\x12/\n" + - "\x05setup\x18\x02 \x01(\v2\x17.harness.v1.SetupTargetH\x00R\x05setup\x12)\n" + - "\x03run\x18\x03 \x01(\v2\x15.harness.v1.RunTargetH\x00R\x03runB\b\n" + - "\x06target\"'\n" + - "\vSetupTarget\x12\x18\n" + - "\aprofile\x18\x01 \x01(\tR\aprofile\">\n" + - "\tRunTarget\x12\x17\n" + - "\atask_id\x18\x01 \x01(\x03R\x06taskId\x12\x18\n" + - "\aattempt\x18\x02 \x01(\x03R\aattempt\"5\n" + - "\vConsoleSize\x12\x12\n" + - "\x04rows\x18\x01 \x01(\rR\x04rows\x12\x12\n" + - "\x04cols\x18\x02 \x01(\rR\x04cols2O\n" + - "\x0eHarnessService\x12=\n" + - "\x04Open\x12\x17.harness.v1.OpenRequest\x1a\x18.harness.v1.OpenResponse(\x010\x01B\xb4\x01\n" + - "\x0ecom.harness.v1B\fHarnessProtoP\x01ZKgithub.com/samcharles93/archie-core/internal/contracts/harness/v1;harnessv1\xa2\x02\x03HXX\xaa\x02\n" + - "Harness.V1\xca\x02\n" + - "Harness\\V1\xe2\x02\x16Harness\\V1\\GPBMetadata\xea\x02\vHarness::V1b\x06proto3" - -var ( - file_harness_v1_harness_proto_rawDescOnce sync.Once - file_harness_v1_harness_proto_rawDescData []byte -) - -func file_harness_v1_harness_proto_rawDescGZIP() []byte { - file_harness_v1_harness_proto_rawDescOnce.Do(func() { - file_harness_v1_harness_proto_rawDescData = protoimpl.X.CompressGZIP(unsafe.Slice(unsafe.StringData(file_harness_v1_harness_proto_rawDesc), len(file_harness_v1_harness_proto_rawDesc))) - }) - return file_harness_v1_harness_proto_rawDescData -} - -var file_harness_v1_harness_proto_msgTypes = make([]protoimpl.MessageInfo, 6) -var file_harness_v1_harness_proto_goTypes = []any{ - (*OpenRequest)(nil), // 0: harness.v1.OpenRequest - (*OpenResponse)(nil), // 1: harness.v1.OpenResponse - (*OpenTarget)(nil), // 2: harness.v1.OpenTarget - (*SetupTarget)(nil), // 3: harness.v1.SetupTarget - (*RunTarget)(nil), // 4: harness.v1.RunTarget - (*ConsoleSize)(nil), // 5: harness.v1.ConsoleSize -} -var file_harness_v1_harness_proto_depIdxs = []int32{ - 2, // 0: harness.v1.OpenRequest.target:type_name -> harness.v1.OpenTarget - 5, // 1: harness.v1.OpenRequest.size:type_name -> harness.v1.ConsoleSize - 3, // 2: harness.v1.OpenTarget.setup:type_name -> harness.v1.SetupTarget - 4, // 3: harness.v1.OpenTarget.run:type_name -> harness.v1.RunTarget - 0, // 4: harness.v1.HarnessService.Open:input_type -> harness.v1.OpenRequest - 1, // 5: harness.v1.HarnessService.Open:output_type -> harness.v1.OpenResponse - 5, // [5:6] is the sub-list for method output_type - 4, // [4:5] is the sub-list for method input_type - 4, // [4:4] is the sub-list for extension type_name - 4, // [4:4] is the sub-list for extension extendee - 0, // [0:4] is the sub-list for field type_name -} - -func init() { file_harness_v1_harness_proto_init() } -func file_harness_v1_harness_proto_init() { - if File_harness_v1_harness_proto != nil { - return - } - file_harness_v1_harness_proto_msgTypes[0].OneofWrappers = []any{ - (*OpenRequest_Target)(nil), - (*OpenRequest_Stdin)(nil), - } - file_harness_v1_harness_proto_msgTypes[1].OneofWrappers = []any{ - (*OpenResponse_Stdout)(nil), - (*OpenResponse_ExitCode)(nil), - (*OpenResponse_Error)(nil), - } - file_harness_v1_harness_proto_msgTypes[2].OneofWrappers = []any{ - (*OpenTarget_Setup)(nil), - (*OpenTarget_Run)(nil), - } - type x struct{} - out := protoimpl.TypeBuilder{ - File: protoimpl.DescBuilder{ - GoPackagePath: reflect.TypeOf(x{}).PkgPath(), - RawDescriptor: unsafe.Slice(unsafe.StringData(file_harness_v1_harness_proto_rawDesc), len(file_harness_v1_harness_proto_rawDesc)), - NumEnums: 0, - NumMessages: 6, - NumExtensions: 0, - NumServices: 1, - }, - GoTypes: file_harness_v1_harness_proto_goTypes, - DependencyIndexes: file_harness_v1_harness_proto_depIdxs, - MessageInfos: file_harness_v1_harness_proto_msgTypes, - }.Build() - File_harness_v1_harness_proto = out.File - file_harness_v1_harness_proto_goTypes = nil - file_harness_v1_harness_proto_depIdxs = nil -} diff --git a/internal/contracts/harness/v1/harness_grpc.pb.go b/internal/contracts/harness/v1/harness_grpc.pb.go deleted file mode 100644 index 552c0426e..000000000 --- a/internal/contracts/harness/v1/harness_grpc.pb.go +++ /dev/null @@ -1,131 +0,0 @@ -// Code generated by protoc-gen-go-grpc. DO NOT EDIT. -// versions: -// - protoc-gen-go-grpc v1.6.2 -// - protoc (unknown) -// source: harness/v1/harness.proto - -package harnessv1 - -import ( - context "context" - grpc "google.golang.org/grpc" - codes "google.golang.org/grpc/codes" - status "google.golang.org/grpc/status" -) - -// This is a compile-time assertion to ensure that this generated file -// is compatible with the grpc package it is being compiled against. -// Requires gRPC-Go v1.64.0 or later. -const _ = grpc.SupportPackageIsVersion9 - -const ( - HarnessService_Open_FullMethodName = "/harness.v1.HarnessService/Open" -) - -// HarnessServiceClient is the client API for HarnessService service. -// -// For semantics around ctx use and closing/ending streaming RPCs, please refer to https://pkg.go.dev/google.golang.org/grpc/?tab=doc#ClientConn.NewStream. -// -// HarnessService opens one duplex PTY session into a Kit container the daemon -// owns. The dashboard's setup terminal consumes the setup target; the -// run-scoped live view consumes the run target and is read-only. See -// docs/prds/external-agent-harness.md, "Session contract". -type HarnessServiceClient interface { - // Open carries a session: the first request frame names the target, later - // frames carry terminal input and sizing, and the response stream carries - // terminal output until the command exits or the session ends. - Open(ctx context.Context, opts ...grpc.CallOption) (grpc.BidiStreamingClient[OpenRequest, OpenResponse], error) -} - -type harnessServiceClient struct { - cc grpc.ClientConnInterface -} - -func NewHarnessServiceClient(cc grpc.ClientConnInterface) HarnessServiceClient { - return &harnessServiceClient{cc} -} - -func (c *harnessServiceClient) Open(ctx context.Context, opts ...grpc.CallOption) (grpc.BidiStreamingClient[OpenRequest, OpenResponse], error) { - cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...) - stream, err := c.cc.NewStream(ctx, &HarnessService_ServiceDesc.Streams[0], HarnessService_Open_FullMethodName, cOpts...) - if err != nil { - return nil, err - } - x := &grpc.GenericClientStream[OpenRequest, OpenResponse]{ClientStream: stream} - return x, nil -} - -// This type alias is provided for backwards compatibility with existing code that references the prior non-generic stream type by name. -type HarnessService_OpenClient = grpc.BidiStreamingClient[OpenRequest, OpenResponse] - -// HarnessServiceServer is the server API for HarnessService service. -// All implementations must embed UnimplementedHarnessServiceServer -// for forward compatibility. -// -// HarnessService opens one duplex PTY session into a Kit container the daemon -// owns. The dashboard's setup terminal consumes the setup target; the -// run-scoped live view consumes the run target and is read-only. See -// docs/prds/external-agent-harness.md, "Session contract". -type HarnessServiceServer interface { - // Open carries a session: the first request frame names the target, later - // frames carry terminal input and sizing, and the response stream carries - // terminal output until the command exits or the session ends. - Open(grpc.BidiStreamingServer[OpenRequest, OpenResponse]) error - mustEmbedUnimplementedHarnessServiceServer() -} - -// UnimplementedHarnessServiceServer must be embedded to have -// forward compatible implementations. -// -// NOTE: this should be embedded by value instead of pointer to avoid a nil -// pointer dereference when methods are called. -type UnimplementedHarnessServiceServer struct{} - -func (UnimplementedHarnessServiceServer) Open(grpc.BidiStreamingServer[OpenRequest, OpenResponse]) error { - return status.Error(codes.Unimplemented, "method Open not implemented") -} -func (UnimplementedHarnessServiceServer) mustEmbedUnimplementedHarnessServiceServer() {} -func (UnimplementedHarnessServiceServer) testEmbeddedByValue() {} - -// UnsafeHarnessServiceServer may be embedded to opt out of forward compatibility for this service. -// Use of this interface is not recommended, as added methods to HarnessServiceServer will -// result in compilation errors. -type UnsafeHarnessServiceServer interface { - mustEmbedUnimplementedHarnessServiceServer() -} - -func RegisterHarnessServiceServer(s grpc.ServiceRegistrar, srv HarnessServiceServer) { - // If the following call panics, it indicates UnimplementedHarnessServiceServer was - // embedded by pointer and is nil. This will cause panics if an - // unimplemented method is ever invoked, so we test this at initialization - // time to prevent it from happening at runtime later due to I/O. - if t, ok := srv.(interface{ testEmbeddedByValue() }); ok { - t.testEmbeddedByValue() - } - s.RegisterService(&HarnessService_ServiceDesc, srv) -} - -func _HarnessService_Open_Handler(srv interface{}, stream grpc.ServerStream) error { - return srv.(HarnessServiceServer).Open(&grpc.GenericServerStream[OpenRequest, OpenResponse]{ServerStream: stream}) -} - -// This type alias is provided for backwards compatibility with existing code that references the prior non-generic stream type by name. -type HarnessService_OpenServer = grpc.BidiStreamingServer[OpenRequest, OpenResponse] - -// HarnessService_ServiceDesc is the grpc.ServiceDesc for HarnessService service. -// It's only intended for direct use with grpc.RegisterService, -// and not to be introspected or modified (even as a copy) -var HarnessService_ServiceDesc = grpc.ServiceDesc{ - ServiceName: "harness.v1.HarnessService", - HandlerType: (*HarnessServiceServer)(nil), - Methods: []grpc.MethodDesc{}, - Streams: []grpc.StreamDesc{ - { - StreamName: "Open", - Handler: _HarnessService_Open_Handler, - ServerStreams: true, - ClientStreams: true, - }, - }, - Metadata: "harness/v1/harness.proto", -} diff --git a/internal/contracts/state/v1/harness_secret.pb.go b/internal/contracts/state/v1/harness_secret.pb.go deleted file mode 100644 index 933182e44..000000000 --- a/internal/contracts/state/v1/harness_secret.pb.go +++ /dev/null @@ -1,402 +0,0 @@ -// Code generated by protoc-gen-go. DO NOT EDIT. -// versions: -// protoc-gen-go v1.36.11 -// protoc (unknown) -// source: state/v1/harness_secret.proto - -package statev1 - -import ( - protoreflect "google.golang.org/protobuf/reflect/protoreflect" - protoimpl "google.golang.org/protobuf/runtime/protoimpl" - timestamppb "google.golang.org/protobuf/types/known/timestamppb" - reflect "reflect" - sync "sync" - unsafe "unsafe" -) - -const ( - // Verify that this generated code is sufficiently up-to-date. - _ = protoimpl.EnforceVersion(20 - protoimpl.MinVersion) - // Verify that runtime/protoimpl is sufficiently up-to-date. - _ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20) -) - -// HarnessSecret mirrors internal/domain/harnesssecret.Secret -- one org's -// captured OAuth token set for a credential binding's service -// (docs/prds/external-agent-harness.md, Credentials). -type HarnessSecret struct { - state protoimpl.MessageState `protogen:"open.v1"` - Service string `protobuf:"bytes,2,opt,name=service,proto3" json:"service,omitempty"` - AccessToken string `protobuf:"bytes,3,opt,name=access_token,json=accessToken,proto3" json:"access_token,omitempty"` - RefreshToken string `protobuf:"bytes,4,opt,name=refresh_token,json=refreshToken,proto3" json:"refresh_token,omitempty"` - TokenType string `protobuf:"bytes,5,opt,name=token_type,json=tokenType,proto3" json:"token_type,omitempty"` - ExpiresAt *timestamppb.Timestamp `protobuf:"bytes,6,opt,name=expires_at,json=expiresAt,proto3" json:"expires_at,omitempty"` - UpdatedAt *timestamppb.Timestamp `protobuf:"bytes,7,opt,name=updated_at,json=updatedAt,proto3" json:"updated_at,omitempty"` - Scopes []string `protobuf:"bytes,8,rep,name=scopes,proto3" json:"scopes,omitempty"` - unknownFields protoimpl.UnknownFields - sizeCache protoimpl.SizeCache -} - -func (x *HarnessSecret) Reset() { - *x = HarnessSecret{} - mi := &file_state_v1_harness_secret_proto_msgTypes[0] - ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) - ms.StoreMessageInfo(mi) -} - -func (x *HarnessSecret) String() string { - return protoimpl.X.MessageStringOf(x) -} - -func (*HarnessSecret) ProtoMessage() {} - -func (x *HarnessSecret) ProtoReflect() protoreflect.Message { - mi := &file_state_v1_harness_secret_proto_msgTypes[0] - if x != nil { - ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) - if ms.LoadMessageInfo() == nil { - ms.StoreMessageInfo(mi) - } - return ms - } - return mi.MessageOf(x) -} - -// Deprecated: Use HarnessSecret.ProtoReflect.Descriptor instead. -func (*HarnessSecret) Descriptor() ([]byte, []int) { - return file_state_v1_harness_secret_proto_rawDescGZIP(), []int{0} -} - -func (x *HarnessSecret) GetService() string { - if x != nil { - return x.Service - } - return "" -} - -func (x *HarnessSecret) GetAccessToken() string { - if x != nil { - return x.AccessToken - } - return "" -} - -func (x *HarnessSecret) GetRefreshToken() string { - if x != nil { - return x.RefreshToken - } - return "" -} - -func (x *HarnessSecret) GetTokenType() string { - if x != nil { - return x.TokenType - } - return "" -} - -func (x *HarnessSecret) GetExpiresAt() *timestamppb.Timestamp { - if x != nil { - return x.ExpiresAt - } - return nil -} - -func (x *HarnessSecret) GetUpdatedAt() *timestamppb.Timestamp { - if x != nil { - return x.UpdatedAt - } - return nil -} - -func (x *HarnessSecret) GetScopes() []string { - if x != nil { - return x.Scopes - } - return nil -} - -type GetHarnessSecretRequest struct { - state protoimpl.MessageState `protogen:"open.v1"` - Service string `protobuf:"bytes,2,opt,name=service,proto3" json:"service,omitempty"` - RunCredential string `protobuf:"bytes,3,opt,name=run_credential,json=runCredential,proto3" json:"run_credential,omitempty"` - unknownFields protoimpl.UnknownFields - sizeCache protoimpl.SizeCache -} - -func (x *GetHarnessSecretRequest) Reset() { - *x = GetHarnessSecretRequest{} - mi := &file_state_v1_harness_secret_proto_msgTypes[1] - ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) - ms.StoreMessageInfo(mi) -} - -func (x *GetHarnessSecretRequest) String() string { - return protoimpl.X.MessageStringOf(x) -} - -func (*GetHarnessSecretRequest) ProtoMessage() {} - -func (x *GetHarnessSecretRequest) ProtoReflect() protoreflect.Message { - mi := &file_state_v1_harness_secret_proto_msgTypes[1] - if x != nil { - ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) - if ms.LoadMessageInfo() == nil { - ms.StoreMessageInfo(mi) - } - return ms - } - return mi.MessageOf(x) -} - -// Deprecated: Use GetHarnessSecretRequest.ProtoReflect.Descriptor instead. -func (*GetHarnessSecretRequest) Descriptor() ([]byte, []int) { - return file_state_v1_harness_secret_proto_rawDescGZIP(), []int{1} -} - -func (x *GetHarnessSecretRequest) GetService() string { - if x != nil { - return x.Service - } - return "" -} - -func (x *GetHarnessSecretRequest) GetRunCredential() string { - if x != nil { - return x.RunCredential - } - return "" -} - -type GetHarnessSecretResponse struct { - state protoimpl.MessageState `protogen:"open.v1"` - Secret *HarnessSecret `protobuf:"bytes,1,opt,name=secret,proto3" json:"secret,omitempty"` - Org string `protobuf:"bytes,2,opt,name=org,proto3" json:"org,omitempty"` - unknownFields protoimpl.UnknownFields - sizeCache protoimpl.SizeCache -} - -func (x *GetHarnessSecretResponse) Reset() { - *x = GetHarnessSecretResponse{} - mi := &file_state_v1_harness_secret_proto_msgTypes[2] - ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) - ms.StoreMessageInfo(mi) -} - -func (x *GetHarnessSecretResponse) String() string { - return protoimpl.X.MessageStringOf(x) -} - -func (*GetHarnessSecretResponse) ProtoMessage() {} - -func (x *GetHarnessSecretResponse) ProtoReflect() protoreflect.Message { - mi := &file_state_v1_harness_secret_proto_msgTypes[2] - if x != nil { - ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) - if ms.LoadMessageInfo() == nil { - ms.StoreMessageInfo(mi) - } - return ms - } - return mi.MessageOf(x) -} - -// Deprecated: Use GetHarnessSecretResponse.ProtoReflect.Descriptor instead. -func (*GetHarnessSecretResponse) Descriptor() ([]byte, []int) { - return file_state_v1_harness_secret_proto_rawDescGZIP(), []int{2} -} - -func (x *GetHarnessSecretResponse) GetSecret() *HarnessSecret { - if x != nil { - return x.Secret - } - return nil -} - -func (x *GetHarnessSecretResponse) GetOrg() string { - if x != nil { - return x.Org - } - return "" -} - -type PutHarnessSecretRequest struct { - state protoimpl.MessageState `protogen:"open.v1"` - Secret *HarnessSecret `protobuf:"bytes,1,opt,name=secret,proto3" json:"secret,omitempty"` - RunCredential string `protobuf:"bytes,2,opt,name=run_credential,json=runCredential,proto3" json:"run_credential,omitempty"` - unknownFields protoimpl.UnknownFields - sizeCache protoimpl.SizeCache -} - -func (x *PutHarnessSecretRequest) Reset() { - *x = PutHarnessSecretRequest{} - mi := &file_state_v1_harness_secret_proto_msgTypes[3] - ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) - ms.StoreMessageInfo(mi) -} - -func (x *PutHarnessSecretRequest) String() string { - return protoimpl.X.MessageStringOf(x) -} - -func (*PutHarnessSecretRequest) ProtoMessage() {} - -func (x *PutHarnessSecretRequest) ProtoReflect() protoreflect.Message { - mi := &file_state_v1_harness_secret_proto_msgTypes[3] - if x != nil { - ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) - if ms.LoadMessageInfo() == nil { - ms.StoreMessageInfo(mi) - } - return ms - } - return mi.MessageOf(x) -} - -// Deprecated: Use PutHarnessSecretRequest.ProtoReflect.Descriptor instead. -func (*PutHarnessSecretRequest) Descriptor() ([]byte, []int) { - return file_state_v1_harness_secret_proto_rawDescGZIP(), []int{3} -} - -func (x *PutHarnessSecretRequest) GetSecret() *HarnessSecret { - if x != nil { - return x.Secret - } - return nil -} - -func (x *PutHarnessSecretRequest) GetRunCredential() string { - if x != nil { - return x.RunCredential - } - return "" -} - -type PutHarnessSecretResponse struct { - state protoimpl.MessageState `protogen:"open.v1"` - unknownFields protoimpl.UnknownFields - sizeCache protoimpl.SizeCache -} - -func (x *PutHarnessSecretResponse) Reset() { - *x = PutHarnessSecretResponse{} - mi := &file_state_v1_harness_secret_proto_msgTypes[4] - ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) - ms.StoreMessageInfo(mi) -} - -func (x *PutHarnessSecretResponse) String() string { - return protoimpl.X.MessageStringOf(x) -} - -func (*PutHarnessSecretResponse) ProtoMessage() {} - -func (x *PutHarnessSecretResponse) ProtoReflect() protoreflect.Message { - mi := &file_state_v1_harness_secret_proto_msgTypes[4] - if x != nil { - ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) - if ms.LoadMessageInfo() == nil { - ms.StoreMessageInfo(mi) - } - return ms - } - return mi.MessageOf(x) -} - -// Deprecated: Use PutHarnessSecretResponse.ProtoReflect.Descriptor instead. -func (*PutHarnessSecretResponse) Descriptor() ([]byte, []int) { - return file_state_v1_harness_secret_proto_rawDescGZIP(), []int{4} -} - -var File_state_v1_harness_secret_proto protoreflect.FileDescriptor - -const file_state_v1_harness_secret_proto_rawDesc = "" + - "\n" + - "\x1dstate/v1/harness_secret.proto\x12\bstate.v1\x1a\x1fgoogle/protobuf/timestamp.proto\"\xa9\x02\n" + - "\rHarnessSecret\x12\x18\n" + - "\aservice\x18\x02 \x01(\tR\aservice\x12!\n" + - "\faccess_token\x18\x03 \x01(\tR\vaccessToken\x12#\n" + - "\rrefresh_token\x18\x04 \x01(\tR\frefreshToken\x12\x1d\n" + - "\n" + - "token_type\x18\x05 \x01(\tR\ttokenType\x129\n" + - "\n" + - "expires_at\x18\x06 \x01(\v2\x1a.google.protobuf.TimestampR\texpiresAt\x129\n" + - "\n" + - "updated_at\x18\a \x01(\v2\x1a.google.protobuf.TimestampR\tupdatedAt\x12\x16\n" + - "\x06scopes\x18\b \x03(\tR\x06scopesJ\x04\b\x01\x10\x02R\x03org\"e\n" + - "\x17GetHarnessSecretRequest\x12\x18\n" + - "\aservice\x18\x02 \x01(\tR\aservice\x12%\n" + - "\x0erun_credential\x18\x03 \x01(\tR\rrunCredentialJ\x04\b\x01\x10\x02R\x03org\"]\n" + - "\x18GetHarnessSecretResponse\x12/\n" + - "\x06secret\x18\x01 \x01(\v2\x17.state.v1.HarnessSecretR\x06secret\x12\x10\n" + - "\x03org\x18\x02 \x01(\tR\x03org\"q\n" + - "\x17PutHarnessSecretRequest\x12/\n" + - "\x06secret\x18\x01 \x01(\v2\x17.state.v1.HarnessSecretR\x06secret\x12%\n" + - "\x0erun_credential\x18\x02 \x01(\tR\rrunCredential\"\x1a\n" + - "\x18PutHarnessSecretResponse2\xcc\x01\n" + - "\x14HarnessSecretService\x12Y\n" + - "\x10GetHarnessSecret\x12!.state.v1.GetHarnessSecretRequest\x1a\".state.v1.GetHarnessSecretResponse\x12Y\n" + - "\x10PutHarnessSecret\x12!.state.v1.PutHarnessSecretRequest\x1a\".state.v1.PutHarnessSecretResponseB\xac\x01\n" + - "\fcom.state.v1B\x12HarnessSecretProtoP\x01ZGgithub.com/samcharles93/archie-core/internal/contracts/state/v1;statev1\xa2\x02\x03SXX\xaa\x02\bState.V1\xca\x02\bState\\V1\xe2\x02\x14State\\V1\\GPBMetadata\xea\x02\tState::V1b\x06proto3" - -var ( - file_state_v1_harness_secret_proto_rawDescOnce sync.Once - file_state_v1_harness_secret_proto_rawDescData []byte -) - -func file_state_v1_harness_secret_proto_rawDescGZIP() []byte { - file_state_v1_harness_secret_proto_rawDescOnce.Do(func() { - file_state_v1_harness_secret_proto_rawDescData = protoimpl.X.CompressGZIP(unsafe.Slice(unsafe.StringData(file_state_v1_harness_secret_proto_rawDesc), len(file_state_v1_harness_secret_proto_rawDesc))) - }) - return file_state_v1_harness_secret_proto_rawDescData -} - -var file_state_v1_harness_secret_proto_msgTypes = make([]protoimpl.MessageInfo, 5) -var file_state_v1_harness_secret_proto_goTypes = []any{ - (*HarnessSecret)(nil), // 0: state.v1.HarnessSecret - (*GetHarnessSecretRequest)(nil), // 1: state.v1.GetHarnessSecretRequest - (*GetHarnessSecretResponse)(nil), // 2: state.v1.GetHarnessSecretResponse - (*PutHarnessSecretRequest)(nil), // 3: state.v1.PutHarnessSecretRequest - (*PutHarnessSecretResponse)(nil), // 4: state.v1.PutHarnessSecretResponse - (*timestamppb.Timestamp)(nil), // 5: google.protobuf.Timestamp -} -var file_state_v1_harness_secret_proto_depIdxs = []int32{ - 5, // 0: state.v1.HarnessSecret.expires_at:type_name -> google.protobuf.Timestamp - 5, // 1: state.v1.HarnessSecret.updated_at:type_name -> google.protobuf.Timestamp - 0, // 2: state.v1.GetHarnessSecretResponse.secret:type_name -> state.v1.HarnessSecret - 0, // 3: state.v1.PutHarnessSecretRequest.secret:type_name -> state.v1.HarnessSecret - 1, // 4: state.v1.HarnessSecretService.GetHarnessSecret:input_type -> state.v1.GetHarnessSecretRequest - 3, // 5: state.v1.HarnessSecretService.PutHarnessSecret:input_type -> state.v1.PutHarnessSecretRequest - 2, // 6: state.v1.HarnessSecretService.GetHarnessSecret:output_type -> state.v1.GetHarnessSecretResponse - 4, // 7: state.v1.HarnessSecretService.PutHarnessSecret:output_type -> state.v1.PutHarnessSecretResponse - 6, // [6:8] is the sub-list for method output_type - 4, // [4:6] is the sub-list for method input_type - 4, // [4:4] is the sub-list for extension type_name - 4, // [4:4] is the sub-list for extension extendee - 0, // [0:4] is the sub-list for field type_name -} - -func init() { file_state_v1_harness_secret_proto_init() } -func file_state_v1_harness_secret_proto_init() { - if File_state_v1_harness_secret_proto != nil { - return - } - type x struct{} - out := protoimpl.TypeBuilder{ - File: protoimpl.DescBuilder{ - GoPackagePath: reflect.TypeOf(x{}).PkgPath(), - RawDescriptor: unsafe.Slice(unsafe.StringData(file_state_v1_harness_secret_proto_rawDesc), len(file_state_v1_harness_secret_proto_rawDesc)), - NumEnums: 0, - NumMessages: 5, - NumExtensions: 0, - NumServices: 1, - }, - GoTypes: file_state_v1_harness_secret_proto_goTypes, - DependencyIndexes: file_state_v1_harness_secret_proto_depIdxs, - MessageInfos: file_state_v1_harness_secret_proto_msgTypes, - }.Build() - File_state_v1_harness_secret_proto = out.File - file_state_v1_harness_secret_proto_goTypes = nil - file_state_v1_harness_secret_proto_depIdxs = nil -} diff --git a/internal/contracts/state/v1/harness_secret_grpc.pb.go b/internal/contracts/state/v1/harness_secret_grpc.pb.go deleted file mode 100644 index 6298c5a89..000000000 --- a/internal/contracts/state/v1/harness_secret_grpc.pb.go +++ /dev/null @@ -1,169 +0,0 @@ -// Code generated by protoc-gen-go-grpc. DO NOT EDIT. -// versions: -// - protoc-gen-go-grpc v1.6.2 -// - protoc (unknown) -// source: state/v1/harness_secret.proto - -package statev1 - -import ( - context "context" - grpc "google.golang.org/grpc" - codes "google.golang.org/grpc/codes" - status "google.golang.org/grpc/status" -) - -// This is a compile-time assertion to ensure that this generated file -// is compatible with the grpc package it is being compiled against. -// Requires gRPC-Go v1.64.0 or later. -const _ = grpc.SupportPackageIsVersion9 - -const ( - HarnessSecretService_GetHarnessSecret_FullMethodName = "/state.v1.HarnessSecretService/GetHarnessSecret" - HarnessSecretService_PutHarnessSecret_FullMethodName = "/state.v1.HarnessSecretService/PutHarnessSecret" -) - -// HarnessSecretServiceClient is the client API for HarnessSecretService service. -// -// For semantics around ctx use and closing/ending streaming RPCs, please refer to https://pkg.go.dev/google.golang.org/grpc/?tab=doc#ClientConn.NewStream. -// -// Harness OAuth secrets (docs/prds/external-agent-harness.md, -// Credentials). Administrative: the setup terminal (an `update` action on -// the secret, Authorizer-gated) and the egress proxy's token-endpoint -// interception (acting as the daemon, not a worker) are the only callers. -type HarnessSecretServiceClient interface { - GetHarnessSecret(ctx context.Context, in *GetHarnessSecretRequest, opts ...grpc.CallOption) (*GetHarnessSecretResponse, error) - PutHarnessSecret(ctx context.Context, in *PutHarnessSecretRequest, opts ...grpc.CallOption) (*PutHarnessSecretResponse, error) -} - -type harnessSecretServiceClient struct { - cc grpc.ClientConnInterface -} - -func NewHarnessSecretServiceClient(cc grpc.ClientConnInterface) HarnessSecretServiceClient { - return &harnessSecretServiceClient{cc} -} - -func (c *harnessSecretServiceClient) GetHarnessSecret(ctx context.Context, in *GetHarnessSecretRequest, opts ...grpc.CallOption) (*GetHarnessSecretResponse, error) { - cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...) - out := new(GetHarnessSecretResponse) - err := c.cc.Invoke(ctx, HarnessSecretService_GetHarnessSecret_FullMethodName, in, out, cOpts...) - if err != nil { - return nil, err - } - return out, nil -} - -func (c *harnessSecretServiceClient) PutHarnessSecret(ctx context.Context, in *PutHarnessSecretRequest, opts ...grpc.CallOption) (*PutHarnessSecretResponse, error) { - cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...) - out := new(PutHarnessSecretResponse) - err := c.cc.Invoke(ctx, HarnessSecretService_PutHarnessSecret_FullMethodName, in, out, cOpts...) - if err != nil { - return nil, err - } - return out, nil -} - -// HarnessSecretServiceServer is the server API for HarnessSecretService service. -// All implementations must embed UnimplementedHarnessSecretServiceServer -// for forward compatibility. -// -// Harness OAuth secrets (docs/prds/external-agent-harness.md, -// Credentials). Administrative: the setup terminal (an `update` action on -// the secret, Authorizer-gated) and the egress proxy's token-endpoint -// interception (acting as the daemon, not a worker) are the only callers. -type HarnessSecretServiceServer interface { - GetHarnessSecret(context.Context, *GetHarnessSecretRequest) (*GetHarnessSecretResponse, error) - PutHarnessSecret(context.Context, *PutHarnessSecretRequest) (*PutHarnessSecretResponse, error) - mustEmbedUnimplementedHarnessSecretServiceServer() -} - -// UnimplementedHarnessSecretServiceServer must be embedded to have -// forward compatible implementations. -// -// NOTE: this should be embedded by value instead of pointer to avoid a nil -// pointer dereference when methods are called. -type UnimplementedHarnessSecretServiceServer struct{} - -func (UnimplementedHarnessSecretServiceServer) GetHarnessSecret(context.Context, *GetHarnessSecretRequest) (*GetHarnessSecretResponse, error) { - return nil, status.Error(codes.Unimplemented, "method GetHarnessSecret not implemented") -} -func (UnimplementedHarnessSecretServiceServer) PutHarnessSecret(context.Context, *PutHarnessSecretRequest) (*PutHarnessSecretResponse, error) { - return nil, status.Error(codes.Unimplemented, "method PutHarnessSecret not implemented") -} -func (UnimplementedHarnessSecretServiceServer) mustEmbedUnimplementedHarnessSecretServiceServer() {} -func (UnimplementedHarnessSecretServiceServer) testEmbeddedByValue() {} - -// UnsafeHarnessSecretServiceServer may be embedded to opt out of forward compatibility for this service. -// Use of this interface is not recommended, as added methods to HarnessSecretServiceServer will -// result in compilation errors. -type UnsafeHarnessSecretServiceServer interface { - mustEmbedUnimplementedHarnessSecretServiceServer() -} - -func RegisterHarnessSecretServiceServer(s grpc.ServiceRegistrar, srv HarnessSecretServiceServer) { - // If the following call panics, it indicates UnimplementedHarnessSecretServiceServer was - // embedded by pointer and is nil. This will cause panics if an - // unimplemented method is ever invoked, so we test this at initialization - // time to prevent it from happening at runtime later due to I/O. - if t, ok := srv.(interface{ testEmbeddedByValue() }); ok { - t.testEmbeddedByValue() - } - s.RegisterService(&HarnessSecretService_ServiceDesc, srv) -} - -func _HarnessSecretService_GetHarnessSecret_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { - in := new(GetHarnessSecretRequest) - if err := dec(in); err != nil { - return nil, err - } - if interceptor == nil { - return srv.(HarnessSecretServiceServer).GetHarnessSecret(ctx, in) - } - info := &grpc.UnaryServerInfo{ - Server: srv, - FullMethod: HarnessSecretService_GetHarnessSecret_FullMethodName, - } - handler := func(ctx context.Context, req interface{}) (interface{}, error) { - return srv.(HarnessSecretServiceServer).GetHarnessSecret(ctx, req.(*GetHarnessSecretRequest)) - } - return interceptor(ctx, in, info, handler) -} - -func _HarnessSecretService_PutHarnessSecret_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { - in := new(PutHarnessSecretRequest) - if err := dec(in); err != nil { - return nil, err - } - if interceptor == nil { - return srv.(HarnessSecretServiceServer).PutHarnessSecret(ctx, in) - } - info := &grpc.UnaryServerInfo{ - Server: srv, - FullMethod: HarnessSecretService_PutHarnessSecret_FullMethodName, - } - handler := func(ctx context.Context, req interface{}) (interface{}, error) { - return srv.(HarnessSecretServiceServer).PutHarnessSecret(ctx, req.(*PutHarnessSecretRequest)) - } - return interceptor(ctx, in, info, handler) -} - -// HarnessSecretService_ServiceDesc is the grpc.ServiceDesc for HarnessSecretService service. -// It's only intended for direct use with grpc.RegisterService, -// and not to be introspected or modified (even as a copy) -var HarnessSecretService_ServiceDesc = grpc.ServiceDesc{ - ServiceName: "state.v1.HarnessSecretService", - HandlerType: (*HarnessSecretServiceServer)(nil), - Methods: []grpc.MethodDesc{ - { - MethodName: "GetHarnessSecret", - Handler: _HarnessSecretService_GetHarnessSecret_Handler, - }, - { - MethodName: "PutHarnessSecret", - Handler: _HarnessSecretService_PutHarnessSecret_Handler, - }, - }, - Streams: []grpc.StreamDesc{}, - Metadata: "state/v1/harness_secret.proto", -} diff --git a/internal/daemon/agent_status_test.go b/internal/daemon/agent_status_test.go index 7100e7e62..2db1cb82a 100644 --- a/internal/daemon/agent_status_test.go +++ b/internal/daemon/agent_status_test.go @@ -71,7 +71,7 @@ func TestRunViaAgentRequiresTerminalStatus(t *testing.T) { Log: slog.New(slog.DiscardHandler), } d.runViaAgent(t.Context(), &workflow.Task{ID: 7, Attempt: 1, Owner: "owner", Repo: "repo", Workflow: "implement"}, - config.Repo{}, config.AgentProfile{}, nil, "") + config.Repo{}, config.AgentProfile{}, "") if tc.wantPark == "" { if len(store.parks) != 0 { diff --git a/internal/daemon/daemon.go b/internal/daemon/daemon.go index 25c1e349c..159275cef 100644 --- a/internal/daemon/daemon.go +++ b/internal/daemon/daemon.go @@ -24,7 +24,6 @@ import ( "github.com/samcharles93/archie-core/internal/config" "github.com/samcharles93/archie-core/internal/container" "github.com/samcharles93/archie-core/internal/domain/access" - "github.com/samcharles93/archie-core/internal/domain/agentrun" "github.com/samcharles93/archie-core/internal/domain/binding" "github.com/samcharles93/archie-core/internal/domain/eda/playbook" "github.com/samcharles93/archie-core/internal/domain/identity" @@ -155,8 +154,6 @@ type Daemon struct { // Required whenever ConnectedStateStore.URL is set: the task is parked // rather than handed the daemon's own administrative token. RunCredentials RunCredentialIssuer - // KitLauncher starts tasks whose agent profile is a Kit. Nil parks them. - KitLauncher KitLauncher // ModelEgress gives native task containers model access without provider // keys. Nil parks a task whose providers need a key. ModelEgress ModelEgress @@ -1730,12 +1727,6 @@ func (d *Daemon) process(ctx context.Context, task *workflow.Task, hold *taskHol if !ok { return } - if profile.IsKit() { - d.runKitTask(ctx, task, repo, workDir, profile) - d.teardownStorage(ctx, task, repo, workDir) - d.cleanupTerminalTaskWorktree(ctx, task, trees) - return - } ctr, credential, revokeCredential, ok := d.acquireTaskContainer(ctx, task, repo, workDir, profile.Image) if !ok { return @@ -1747,7 +1738,7 @@ func (d *Daemon) process(ctx context.Context, task *workflow.Task, hold *taskHol // store, forge and push operations. limitCtx, stopLimit := withTaskTimeLimit(ctx, d.configFor(task).Budgets.TaskWallClock.Std()) runCtx, stopWatch := withContainerExit(limitCtx, ctr) - d.runViaAgent(runCtx, task, repo, profile, nil, credential) + d.runViaAgent(runCtx, task, repo, profile, credential) stopWatch() stopLimit() @@ -2116,7 +2107,7 @@ func (d *Daemon) recordPark(ctx context.Context, taskID int64, reason string) { // // credential is the task's run credential; the agent presents it to push the // task's branch. -func (d *Daemon) runViaAgent(ctx context.Context, task *workflow.Task, repo config.Repo, profile config.AgentProfile, harness *agentrun.HarnessSpec, credential string) { +func (d *Daemon) runViaAgent(ctx context.Context, task *workflow.Task, repo config.Repo, profile config.AgentProfile, credential string) { cfg, err := d.taskConfig(ctx, task) if err != nil { d.parkRunningTask(ctx, task, err.Error(), taskstate.ParkTransient) @@ -2142,7 +2133,6 @@ func (d *Daemon) runViaAgent(ctx context.Context, task *workflow.Task, repo conf RunCredential: credential, WorkflowDefinition: task.WorkflowDefinitionYAML, Tools: d.toolAllowlist(ctx, task, profile.Tools), - Harness: harness, } req.Trace = map[string]string{} otel.GetTextMapPropagator().Inject(ctx, propagation.MapCarrier(req.Trace)) @@ -2232,9 +2222,7 @@ func (d *Daemon) pinTaskProfile(ctx context.Context, task *workflow.Task) (confi if err == nil { var profile config.AgentProfile if profile, err = d.configFor(task).Containers.Profile(iface.Profile); err == nil { - if err = validateProfileMeetsNeeds(profile, iface.Needs()); err == nil { - return profile, true - } + return profile, true } } d.Log.ErrorContext(ctx, "agent profile unavailable", "task", task.ID, "err", err) @@ -2242,19 +2230,6 @@ func (d *Daemon) pinTaskProfile(ctx context.Context, task *workflow.Task) (confi return config.AgentProfile{}, false } -// validateProfileMeetsNeeds rejects a Kit profile whose harness cannot serve -// the captures the workflow needs. Image profiles always pass. -func validateProfileMeetsNeeds(profile config.AgentProfile, needs workflowtask.WorkflowNeeds) error { - if !profile.IsKit() || (!needs.Captures && len(profile.Tools) == 0) { - return nil - } - adapter, ok := agentexec.LookupHarnessAdapter(profile.Adapter) - if !ok || len(adapter.MCPConfig) == 0 { - return fmt.Errorf("profile adapter %q cannot register the required Archie MCP tools", profile.Adapter) - } - return nil -} - // pinWorkflowDefinition ensures the task carries a valid definition pin for // the workflow it names and that its org has the workflow enabled. Every run // passes through here, whatever started it, so this is the one enablement diff --git a/internal/daemon/kit_task.go b/internal/daemon/kit_task.go deleted file mode 100644 index cb7fd5175..000000000 --- a/internal/daemon/kit_task.go +++ /dev/null @@ -1,214 +0,0 @@ -package daemon - -import ( - "context" - "fmt" - "os" - "time" - - "github.com/samcharles93/archie-core/internal/config" - "github.com/samcharles93/archie-core/internal/domain/identity" - "github.com/samcharles93/archie-core/internal/domain/workflow" - workflowtask "github.com/samcharles93/archie-core/internal/domain/workflow/task" - "github.com/samcharles93/archie-core/internal/infrastructure/egress" - "github.com/samcharles93/archie-core/internal/infrastructure/kit" - "github.com/samcharles93/archie-core/internal/infrastructure/kitrun" - "github.com/samcharles93/archie-core/internal/storage" - "github.com/samcharles93/archie-core/internal/taskstate" -) - -// KitLauncher starts and ends the container of a task whose agent profile -// is a Kit. -type KitLauncher interface { - Launch(ctx context.Context, req kitrun.Request) (*kitrun.Run, error) - Release(ctx context.Context, run *kitrun.Run) error - RemoveVolumes(ctx context.Context, volumes []kit.Volume) error -} - -// runKitTask runs a task in its Kit's container, every agent stage on the -// Kit's harness. The worker reaches NATS and the State Store only through -// the egress relay, and its environment carries no model provider key. -func (d *Daemon) runKitTask(ctx context.Context, task *workflow.Task, repo config.Repo, workDir string, profile config.AgentProfile) { - park := func(reason string, err error) { - d.Log.ErrorContext(ctx, reason, "task", task.ID, "err", err) - d.parkRunningTask(ctx, task, reason+": "+err.Error(), taskstate.ParkTransient) - } - if d.KitLauncher == nil { - park("kit profile", fmt.Errorf("kit harness runs are unavailable on this daemon")) - return - } - if err := writeTaskBrief(workDir, task); err != nil { - park("task.json write failed", err) - return - } - credential, revokeCredential, err := d.runCredential(ctx, task) - if err != nil { - park("run credential failed", err) - return - } - defer revokeCredential() - env, err := kitrun.WorkerEnv(kitrun.Endpoints{ - NATS: d.ConnectedNATS.URL, NATSToken: d.ConnectedNATS.ContainerToken(), - StateStore: d.ConnectedStateStore.URL, StateStoreToken: credential, StateStoreCA: d.ConnectedStateStore.CA, - }, os.Getuid(), os.Getgid()) - if err != nil { - park("kit worker environment", err) - return - } - // The interface is reparsed rather than threaded through pinTaskProfile's - // return: task.WorkflowDefinitionYAML is already pinned by the time this - // runs, parsing it is pure and cheap, and pinTaskProfile's signature stays - // unchanged for its other callers. - iface, err := workflowtask.ParseWorkflowInterface(task.WorkflowDefinitionYAML) - if err != nil { - park("kit workflow interface", err) - return - } - // Org and GrantedServices are the dispatching identity's own facts, read - // fresh here rather than cached anywhere: the same per-task - // config.Config d.configFor already resolves for every other identity - // override (Forge, Budgets, ...). - taskCfg := d.configFor(task) - credentialOrg, grantedServices := taskCfg.CredentialAccess(string(identity.SystemID)) - var record *egress.RecordOptions - if task.IsTestRun() { - record = &egress.RecordOptions{TaskID: task.ID, ModelHosts: modelHosts(d.Cfg.Get().Providers)} - } - run, err := d.KitLauncher.Launch(ctx, kitrun.Request{ - TaskID: task.ID, - Attempt: task.Attempt, - Kit: profile.Kit, - Adapter: profile.Adapter, - WorkDir: workDir, - WorkerEnv: env, - GateRetries: iface.Needs().GateRetries, - Org: credentialOrg, - GrantedServices: grantedServices, - RunCredential: credential, - Record: record, - }) - if err != nil { - park("kit launch failed", err) - return - } - - limitCtx, stopLimit := withTaskTimeLimit(ctx, d.configFor(task).Budgets.TaskWallClock.Std()) - runCtx, stopWatch := withContainerExit(limitCtx, run.Container) - d.runViaAgent(runCtx, task, repo, profile, &run.Harness, credential) - stopWatch() - stopLimit() - - if err := d.KitLauncher.Release(ctx, run); err != nil { - d.Log.WarnContext(ctx, "kit release failed", "task", task.ID, "err", err) - } - d.removeEndedKitVolumes(ctx, task.ID, run.Volumes) -} - -// removeEndedKitVolumes removes the execution's Kit volumes unless the task -// parked: a retry of a parked task is the same execution and resumes from -// them. -func (d *Daemon) removeEndedKitVolumes(ctx context.Context, taskID int64, volumes []kit.Volume) { - if len(volumes) == 0 || d.Store == nil { - return - } - lookupCtx, cancel := context.WithTimeout(context.WithoutCancel(ctx), 10*time.Second) - defer cancel() - latest, err := d.Store.TaskByID(lookupCtx, taskID) - if err != nil || latest == nil || latest.Status == workflow.StatusParked { - return - } - if err := d.KitLauncher.RemoveVolumes(lookupCtx, volumes); err != nil { - d.Log.WarnContext(ctx, "kit volume removal failed", "task", taskID, "err", err) - } -} - -// ModelEgress opens a native task's model proxy session. The container holds -// a sentinel under each provider's key variable and reaches the providers -// through the egress proxy, which swaps in the org's bound key. -type ModelEgress interface { - Open(token, org string, providers map[string]config.Provider) (ModelSession, error) - Close(ctx context.Context, token string) -} - -// TaskInstructions answers the instance and organisation instructions for a -// task's org. -type TaskInstructions interface { - TaskInstructions(ctx context.Context, taskID int64) (instance, organisation string, err error) -} - -// TaskModels answers what a task's org resolves models against. -type TaskModels interface { - TaskModels(ctx context.Context, taskID int64) (config.OrgModels, error) -} - -// taskConfig is task's configuration with its org's model aliases and own -// providers, narrowed to the providers those aliases reach, each carrying the -// variable its credential is read from. A provider no alias names is not part -// of the run at all: requiring its credential would park a task whose models -// never touch it. -func (d *Daemon) taskConfig(ctx context.Context, task *workflow.Task) (config.Config, error) { - cfg := d.configFor(task) - if d.TaskModels != nil { - models, err := d.TaskModels.TaskModels(ctx, task.ID) - if err != nil { - return config.Config{}, fmt.Errorf("resolve the task's models: %w", err) - } - cfg = cfg.WithOrgModels(models) - } - cfg.Providers = cfg.ModelProviders() - return cfg, nil -} - -// ModelSession is what a native container needs for its model proxy session. -type ModelSession struct { - Env []string - Mounts []storage.Mount -} - -// openModelEgress opens task's model session, or refuses: provider keys -// never reach a container, so without the proxy no model call can run. A -// workflow with no model step needs no session and no provider credential. -func (d *Daemon) openModelEgress(ctx context.Context, task *workflow.Task, credential string) (ModelSession, func(), error) { - if !workflow.UsesModel(task.WorkflowDefinitionYAML) { - return ModelSession{}, func() {}, nil - } - cfg, err := d.taskConfig(ctx, task) - if err != nil { - return ModelSession{}, nil, err - } - if d.ModelEgress == nil { - for _, p := range cfg.Providers { - if p.APIKeyEnv != "" { - return ModelSession{}, nil, blockedPark{taskstate.BlockerModelCredential, fmt.Errorf("model egress proxy is unavailable on this daemon, and provider keys never enter a container")} - } - } - return ModelSession{}, func() {}, nil - } - if credential == "" { - return ModelSession{}, nil, fmt.Errorf("model egress needs a run credential, and no State Store issues one") - } - session, err := d.ModelEgress.Open(credential, task.OrgOrDefault(), cfg.Providers) - if err != nil { - return ModelSession{}, nil, blockedPark{taskstate.BlockerModelCredential, err} - } - return session, func() { d.ModelEgress.Close(ctx, credential) }, nil -} - -// modelHosts is every host a configured provider is reached at. A test run -// records its deliveries, but a model call is how the run reads, so these hosts -// are let through whatever their method: this errs broad on purpose, because a -// provider left out would silently swallow the run's model calls, and no -// provider host is a delivery destination. -func modelHosts(providers map[string]config.Provider) []string { - seen := make(map[string]bool, len(providers)) - hosts := make([]string, 0, len(providers)) - for _, p := range providers { - host, err := egress.ProviderHost(p.Class, p.BaseURL) - if err != nil || seen[host] { - continue - } - seen[host] = true - hosts = append(hosts, host) - } - return hosts -} diff --git a/internal/daemon/model_egress.go b/internal/daemon/model_egress.go new file mode 100644 index 000000000..f8f17e5f0 --- /dev/null +++ b/internal/daemon/model_egress.go @@ -0,0 +1,83 @@ +package daemon + +import ( + "context" + "fmt" + + "github.com/samcharles93/archie-core/internal/config" + "github.com/samcharles93/archie-core/internal/domain/workflow" + "github.com/samcharles93/archie-core/internal/storage" + "github.com/samcharles93/archie-core/internal/taskstate" +) + +// ModelEgress opens a native task's model proxy session. The container holds +// a sentinel under each provider's key variable and reaches the providers +// through the egress proxy, which swaps in the org's bound key. +type ModelEgress interface { + Open(token, org string, providers map[string]config.Provider) (ModelSession, error) + Close(ctx context.Context, token string) +} + +// TaskInstructions answers the instance and organisation instructions for a +// task's org. +type TaskInstructions interface { + TaskInstructions(ctx context.Context, taskID int64) (instance, organisation string, err error) +} + +// TaskModels answers what a task's org resolves models against. +type TaskModels interface { + TaskModels(ctx context.Context, taskID int64) (config.OrgModels, error) +} + +// taskConfig is task's configuration with its org's model aliases and own +// providers, narrowed to the providers those aliases reach, each carrying the +// variable its credential is read from. A provider no alias names is not part +// of the run at all: requiring its credential would park a task whose models +// never touch it. +func (d *Daemon) taskConfig(ctx context.Context, task *workflow.Task) (config.Config, error) { + cfg := d.configFor(task) + if d.TaskModels != nil { + models, err := d.TaskModels.TaskModels(ctx, task.ID) + if err != nil { + return config.Config{}, fmt.Errorf("resolve the task's models: %w", err) + } + cfg = cfg.WithOrgModels(models) + } + cfg.Providers = cfg.ModelProviders() + return cfg, nil +} + +// ModelSession is what a native container needs for its model proxy session. +type ModelSession struct { + Env []string + Mounts []storage.Mount +} + +// openModelEgress opens task's model session, or refuses: provider keys +// never reach a container, so without the proxy no model call can run. A +// workflow with no model step needs no session and no provider credential. +func (d *Daemon) openModelEgress(ctx context.Context, task *workflow.Task, credential string) (ModelSession, func(), error) { + if !workflow.UsesModel(task.WorkflowDefinitionYAML) { + return ModelSession{}, func() {}, nil + } + cfg, err := d.taskConfig(ctx, task) + if err != nil { + return ModelSession{}, nil, err + } + if d.ModelEgress == nil { + for _, p := range cfg.Providers { + if p.APIKeyEnv != "" { + return ModelSession{}, nil, blockedPark{taskstate.BlockerModelCredential, fmt.Errorf("model egress proxy is unavailable on this daemon, and provider keys never enter a container")} + } + } + return ModelSession{}, func() {}, nil + } + if credential == "" { + return ModelSession{}, nil, fmt.Errorf("model egress needs a run credential, and no State Store issues one") + } + session, err := d.ModelEgress.Open(credential, task.OrgOrDefault(), cfg.Providers) + if err != nil { + return ModelSession{}, nil, blockedPark{taskstate.BlockerModelCredential, err} + } + return session, func() { d.ModelEgress.Close(ctx, credential) }, nil +} diff --git a/internal/daemon/model_egress_test.go b/internal/daemon/model_egress_test.go index 0f1e42ad1..0609a7c99 100644 --- a/internal/daemon/model_egress_test.go +++ b/internal/daemon/model_egress_test.go @@ -101,7 +101,7 @@ func TestRunViaAgentCarriesProviderCredentialVariable(t *testing.T) { } task := &workflow.Task{ID: 7, Identity: string(sys), Attempt: 1, Workflow: "implement", WorkflowDefinitionYAML: "id: w\nsteps:\n - type: agent.run\n"} - d.runViaAgent(t.Context(), task, config.Repo{}, config.AgentProfile{}, nil, "run-credential") + d.runViaAgent(t.Context(), task, config.Repo{}, config.AgentProfile{}, "run-credential") if bus.request == nil { t.Fatal("the daemon sent no taskrun request") diff --git a/internal/domain/agentrun/protocol.go b/internal/domain/agentrun/protocol.go index f6b1ff20e..9707f955e 100644 --- a/internal/domain/agentrun/protocol.go +++ b/internal/domain/agentrun/protocol.go @@ -81,36 +81,6 @@ type Request struct { // History is the conversation of an earlier run of this stage that stopped // on its step budget; the run continues from it. History []TranscriptEntry `json:"history,omitempty"` - // Harness, when set, runs the stage on an external coding-agent CLI - // instead of the built-in loop. The worker in a Kit container is the - // daemon's own archie-agent build, so it always understands this field. - Harness *HarnessSpec `json:"harness,omitempty"` -} - -// HarnessSpec is how to drive a Kit's agent headlessly: its launch argv -// (image entrypoint and cmd) and the agent-sessions@1 verb tails appended -// to it. Prompt must carry {{.Prompt}}; Resume, when set, {{.SessionID}}. -type HarnessSpec struct { - // User is the Kit's user, a name or "uid[:gid]", that every invocation - // runs as. It is never root: the worker's credentials live in the same - // container, readable only by the worker's user. - User string `json:"user,omitempty"` - // Env is the invocation's entire environment. Nothing is inherited from - // the worker, whose environment carries its credentials. - Env []string `json:"env,omitempty"` - // Adapter names the output adapter reading the CLI's stream; empty is - // a CLI with none. - Adapter string `json:"adapter,omitempty"` - Launch []string `json:"launch"` - Prompt []string `json:"prompt"` - Resume []string `json:"resume,omitempty"` - Continue []string `json:"continue,omitempty"` - // MCPConfig registers archie's MCP server with the CLI: an argv tail - // carrying {{.MCPConfig}}, the path of a standard mcpServers config. - // A stage with capture tools needs it. - MCPConfig []string `json:"mcp_config,omitempty"` - // CentralTools enables the proxy-only central MCP endpoint. - CentralTools bool `json:"central_tools,omitempty"` } // Validate rejects requests that this runner cannot safely interpret. diff --git a/internal/domain/harnesssecret/context.go b/internal/domain/harnesssecret/context.go deleted file mode 100644 index 0850d69dc..000000000 --- a/internal/domain/harnesssecret/context.go +++ /dev/null @@ -1,16 +0,0 @@ -package harnesssecret - -import "context" - -type runCredentialKey struct{} - -// WithRunCredential binds a proxy or launcher secret operation to its task grant. -func WithRunCredential(ctx context.Context, credential string) context.Context { - return context.WithValue(ctx, runCredentialKey{}, credential) -} - -// RunCredential returns the task grant authorizing a secret operation. -func RunCredential(ctx context.Context) string { - credential, _ := ctx.Value(runCredentialKey{}).(string) - return credential -} diff --git a/internal/domain/harnesssecret/harnesssecret.go b/internal/domain/harnesssecret/harnesssecret.go deleted file mode 100644 index 79be1b14b..000000000 --- a/internal/domain/harnesssecret/harnesssecret.go +++ /dev/null @@ -1,40 +0,0 @@ -// Package harnesssecret defines a credential binding's OAuth token set for -// one org. -package harnesssecret - -import ( - "errors" - "time" -) - -// Secret is one org's captured OAuth token set for a credential binding's -// service. -type Secret struct { - Org string - Service string - AccessToken string - RefreshToken string - TokenType string - ExpiresAt time.Time - // Scopes are the scopes the provider granted, as the token response's - // space-delimited `scope` field reported them (RFC 6749 section 5.1). - // The set is kept across a refresh that does not repeat the field. - Scopes []string - UpdatedAt time.Time -} - -// Validate rejects a Secret with no identity or no access token: the setup -// terminal capture and the proxy's refresh write both produce a complete -// token set or nothing. -func (s Secret) Validate() error { - if s.Org == "" { - return errors.New("harnesssecret: org is required") - } - if s.Service == "" { - return errors.New("harnesssecret: service is required") - } - if s.AccessToken == "" { - return errors.New("harnesssecret: access token is required") - } - return nil -} diff --git a/internal/domain/storecontract/credentials.go b/internal/domain/storecontract/credentials.go index 1785d62b2..64bf445b6 100644 --- a/internal/domain/storecontract/credentials.go +++ b/internal/domain/storecontract/credentials.go @@ -3,17 +3,8 @@ package storecontract import ( "context" "time" - - "github.com/samcharles93/archie-core/internal/domain/harnesssecret" ) -// HarnessSecretStore stores one org's OAuth token set per credential binding -// service, encrypted at rest. -type HarnessSecretStore interface { - GetHarnessSecret(ctx context.Context, org, service string) (harnesssecret.Secret, error) - PutHarnessSecret(ctx context.Context, s harnesssecret.Secret) error -} - // RunCredentialStore keeps run credentials by digest: the SHA-256 of the // token a task's container presents. A lookup of an unknown or expired // digest reports ErrRunCredentialUnknown. diff --git a/internal/domain/storecontract/errors.go b/internal/domain/storecontract/errors.go index 04457e2c9..7b7535ac0 100644 --- a/internal/domain/storecontract/errors.go +++ b/internal/domain/storecontract/errors.go @@ -58,10 +58,6 @@ var ( // ErrSourceSigningStale is returned when a signing write's expected // from state does not match the stored one. ErrSourceSigningStale = errors.New("store: source signing does not match expected state") - // ErrHarnessSecretNotFound is returned when no OAuth token set is - // stored for an org/service pair -- the setup terminal has not - // captured one yet. - ErrHarnessSecretNotFound = errors.New("store: harness secret not found") // ErrRereviewCapReached is returned when a review gate re-review would // round past prreview.MaxRereviewRounds. The task stays waiting and its // gate row is unchanged. The dashboard answers it 409, like every other diff --git a/internal/domain/workflow/test_run.go b/internal/domain/workflow/test_run.go index 83e227763..06270363f 100644 --- a/internal/domain/workflow/test_run.go +++ b/internal/domain/workflow/test_run.go @@ -21,11 +21,8 @@ type Deliveries interface { // RecordDeliveries makes tc record every outward delivery its steps ask for // and perform none of them, so an operator can run a draft workflow and see // what it would do without it reaching anyone. The forge and push writes are -// wrapped here; a Kit run's network traffic and central tool calls are -// recorded by the egress proxy (egress.RecordOptions), which is also why a -// recording command.run still executes: only its network egress is recorded. A -// native container's own network has no proxy in front of it, so nothing it -// sends is recorded. +// wrapped here. A container's own network has no proxy in front of it, so +// nothing it sends is recorded. func RecordDeliveries(tc *TaskContext) { tc.Deliveries = recordDeliveries{tc: tc} tc.Forge = recordedForge{Forger: tc.Forge, deliveries: tc.Deliveries} diff --git a/internal/gateway/dashboard_tools.go b/internal/gateway/dashboard_tools.go index 2eb5140cb..c50d34826 100644 --- a/internal/gateway/dashboard_tools.go +++ b/internal/gateway/dashboard_tools.go @@ -57,7 +57,7 @@ var dashboardPages = []DashboardPage{ {Path: "/settings/plugins", Label: "Plugins", Description: "Extension directories loaded at startup."}, {Path: "/settings/secret-engines", Label: "Secret engines"}, {Path: "/settings/container-runtime", Label: "Container runtime"}, - {Path: "/settings/agent-profiles", Label: "Agent profiles", Description: "Execution profiles (image or Kit harness), credential sign-in status and the harness setup terminal."}, + {Path: "/settings/agent-profiles", Label: "Agent profiles", Description: "Execution profiles: the image and tools each agent runs with."}, } // DashboardPages returns a copy of the dashboard page registry. diff --git a/internal/infrastructure/bindingcipher/cipher.go b/internal/infrastructure/bindingcipher/cipher.go index b8b9904df..97ad8c0c3 100644 --- a/internal/infrastructure/bindingcipher/cipher.go +++ b/internal/infrastructure/bindingcipher/cipher.go @@ -32,15 +32,8 @@ type Domain struct { aad []byte } -var ( - // BindingDomain seals binding and source webhook secrets. Not row-bound. - BindingDomain = Domain{marker: "arcie-binding", aad: []byte("arcie-binding-secret")} - // HarnessSecretDomain seals harness OAuth token sets - // -- its own - // separator, so a row cannot be moved from oauth_secrets into a - // bindings-secret column (or back) and still authenticate. - HarnessSecretDomain = Domain{marker: "arcie-harness", aad: []byte("arcie-harness-secret")} -) +// BindingDomain seals binding and source webhook secrets. Not row-bound. +var BindingDomain = Domain{marker: "arcie-binding", aad: []byte("arcie-binding-secret")} // bindingKeyFingerprintLen is the number of hex characters of the // SHA-256(material) fingerprint embedded in the envelope (8 bytes, far more @@ -59,7 +52,7 @@ type BindingCipher interface { // under BindingDomain. Decrypt(envelope string) (string, error) // EncryptDomain and DecryptDomain are Encrypt/Decrypt for a caller-named - // domain, so a second column (e.g. harness OAuth secrets) can share this + // domain, so a second column can share this // cipher's keyring without sharing BindingDomain's AAD. EncryptDomain(d Domain, plaintext string) (string, error) DecryptDomain(d Domain, envelope string) (string, error) diff --git a/internal/infrastructure/controlplanestore/agent_profiles.go b/internal/infrastructure/controlplanestore/agent_profiles.go index cb748506f..e6e687858 100644 --- a/internal/infrastructure/controlplanestore/agent_profiles.go +++ b/internal/infrastructure/controlplanestore/agent_profiles.go @@ -29,7 +29,7 @@ func agentProfilesDocument(profiles map[string]config.AgentProfile) map[string]a } out := make(map[string]agentProfile, len(profiles)) for name, p := range profiles { - out[name] = agentProfile{Image: p.Image, Kit: p.Kit, Adapter: p.Adapter, Tools: p.Tools} + out[name] = agentProfile{Image: p.Image, Tools: p.Tools} } return out } @@ -42,7 +42,7 @@ func agentProfilesSettings(profiles map[string]agentProfile) map[string]config.A } out := make(map[string]config.AgentProfile, len(profiles)) for name, p := range profiles { - out[name] = config.AgentProfile{Image: p.Image, Kit: p.Kit, Adapter: p.Adapter, Tools: p.Tools} + out[name] = config.AgentProfile{Image: p.Image, Tools: p.Tools} } return out } diff --git a/internal/infrastructure/controlplanestore/container_settings.go b/internal/infrastructure/controlplanestore/container_settings.go index 79f44e3e0..bed1f7b05 100644 --- a/internal/infrastructure/controlplanestore/container_settings.go +++ b/internal/infrastructure/controlplanestore/container_settings.go @@ -22,10 +22,8 @@ type containerRuntimePolicies struct { // agentProfile is one named execution environment as the document writes it. type agentProfile struct { - Image string `json:"image,omitempty"` - Kit string `json:"kit,omitempty"` - Adapter string `json:"adapter,omitempty"` - Tools []string `json:"tools,omitempty"` + Image string `json:"image,omitempty"` + Tools []string `json:"tools,omitempty"` } // legacyContainerKeys maps Go-cased keys from older stored documents to @@ -37,7 +35,7 @@ var legacyContainerKeys = map[string]string{ // legacyProfileKeys are the same fallback at the profile nesting level. var legacyProfileKeys = map[string]string{ - "Image": "image", "Kit": "kit", "Adapter": "adapter", "Tools": "tools", + "Image": "image", "Tools": "tools", } func (p *agentProfile) UnmarshalJSON(data []byte) error { diff --git a/internal/infrastructure/controlplanestore/credential_bindings.go b/internal/infrastructure/controlplanestore/credential_bindings.go index a3f363ee4..17ea82460 100644 --- a/internal/infrastructure/controlplanestore/credential_bindings.go +++ b/internal/infrastructure/controlplanestore/credential_bindings.go @@ -2,8 +2,8 @@ package controlplanestore import "github.com/samcharles93/archie-core/internal/config" -// CredentialBindingsKind is the resource mapping Kit credential service -// names to org secrets. Applied live on each launch. +// CredentialBindingsKind is the resource mapping credential service names to +// org secrets. Applied live on each launch. const CredentialBindingsKind = "credential-bindings" // credentialBinding is one config.CredentialBinding as the document writes diff --git a/internal/infrastructure/controlplanestore/profiles_projector.go b/internal/infrastructure/controlplanestore/profiles_projector.go index 22a02bdac..12c6ba38d 100644 --- a/internal/infrastructure/controlplanestore/profiles_projector.go +++ b/internal/infrastructure/controlplanestore/profiles_projector.go @@ -15,11 +15,9 @@ import ( // profileFile is one contributed profile: a name and the profile it defines. // The image is a reference to a published image, never a build. type profileFile struct { - Name string `yaml:"name"` - Image string `yaml:"image"` - Kit string `yaml:"kit"` - Adapter string `yaml:"adapter"` - Tools []string `yaml:"tools"` + Name string `yaml:"name"` + Image string `yaml:"image"` + Tools []string `yaml:"tools"` } // ProfileProjector merges an installed package's agent profiles into the @@ -50,7 +48,7 @@ func (p *ProfileProjector) Apply(ctx context.Context, orgID, name, digest string if file.Name == "" { return fmt.Errorf("contributed profile %q has no name", contentName) } - incoming[file.Name] = agentProfile{Image: file.Image, Kit: file.Kit, Adapter: file.Adapter, Tools: file.Tools} + incoming[file.Name] = agentProfile{Image: file.Image, Tools: file.Tools} recorded = append(recorded, storepkg.ProjectionEntry{Family: storepkg.FamilyProfiles, EntryID: file.Name}) } err := projectDocument(ctx, p.store, orgID, AgentProfileKind, "package:"+name+"@"+digest, "package-project:"+digest, func(current []byte) ([]byte, error) { diff --git a/internal/infrastructure/egress/credentials.go b/internal/infrastructure/egress/credentials.go index 240b01a93..cdce374f1 100644 --- a/internal/infrastructure/egress/credentials.go +++ b/internal/infrastructure/egress/credentials.go @@ -3,126 +3,17 @@ package egress import ( "context" "errors" - "fmt" - "net/http" - "slices" - "strings" - - "github.com/docker/sandbox-kit-spec/v3/spec" ) -// Sentinel is the value a sandbox sees in place of a proxy-managed key, so -// it can tell a credential is wired without ever holding it. +// Sentinel prefixes the value a container sees in place of a proxy-managed +// key, so it can tell a key is wired without ever holding it. const Sentinel = "archie-proxy-managed" -// CredentialKind is how a run carries a credential service, decided by the -// org's binding. An absent service is not carried. -type CredentialKind string - -const ( - // CredentialAPIKey is an API key the proxy injects from the org secret - // the binding names. - CredentialAPIKey CredentialKind = "apikey" - // CredentialOAuth is the org's captured OAuth token set, which the proxy - // holds: the binding names no secret of its own. - CredentialOAuth CredentialKind = "oauth" -) - -// ErrUnbound reports that the run has no binding for a service. It is the -// only resolver error an optional credential may pass over; any other error -// stops the request. +// ErrUnbound reports that the run has no binding for a service. var ErrUnbound = errors.New("egress: credential not bound") -// Resolver supplies a credential's real value for the run a run credential -// names. An implementation returns only secrets the run's identity is -// granted, so what a Kit declares is always narrowed by what the org granted. +// Resolver supplies a key's real value for the run a run credential names. +// An implementation returns only secrets the run's identity is granted. type Resolver interface { Resolve(ctx context.Context, credential, service string) (string, error) } - -// injection is one compiled credential@1 apiKey inject rule. -type injection struct { - service string - required bool - phases spec.Phases - domain pattern - header string - format string - basic bool - username string -} - -// compileInjections compiles the credential@1 apiKey inject rules this run -// can fire. A service the run carries as OAuth contributes none: its value is -// the org's token set, which the proxy holds and injects on its own paths, so -// an API-key rule for it would only put an empty credential on the wire. -func compileInjections(creds []spec.CredentialCapability, bound map[string]CredentialKind) []injection { - var out []injection - for _, c := range creds { - if c.APIKey == nil || bound[c.Service] == CredentialOAuth { - continue - } - for _, rule := range c.APIKey.Inject { - format := rule.Format - if format == "" { - format = "%s" - } - out = append(out, injection{ - service: c.Service, - required: c.Required, - phases: slices.Clone(c.Phase), - domain: compilePattern(rule.Domain), - header: rule.Header, - format: format, - basic: strings.EqualFold(rule.Scheme, "basic"), - username: rule.Username, - }) - } - } - return out -} - -// inject presents the session's credentials on a request policy has already -// admitted. Only rules for this phase and this host apply, so a key is never -// sent anywhere its Kit did not name. -func (p *Proxy) inject(ctx context.Context, s *Session, r *http.Request, host string, port int) error { - phase := s.phase() - for _, rule := range s.injections { - if !slices.Contains(rule.phases, phase) || !rule.domain.matches(normalizeHost(host), port) { - continue - } - if p.resolver == nil { - if rule.required { - return fmt.Errorf("credential %q is required and no resolver is configured", rule.service) - } - continue - } - secret, err := p.resolver.Resolve(ctx, s.credential(), rule.service) - switch { - case errors.Is(err, ErrUnbound) && !rule.required: - continue - case errors.Is(err, ErrUnbound): - return fmt.Errorf("credential %q is required and not bound for this run", rule.service) - case err != nil: - return fmt.Errorf("resolve credential %q: %w", rule.service, err) - } - if rule.basic { - r.SetBasicAuth(rule.username, secret) - continue - } - r.Header.Set(rule.header, fmt.Sprintf(rule.format, secret)) - } - return nil -} - -// SentinelEnv is the environment a sandbox gets for its Kit's API keys: the -// sentinel under each named key. An inject-only key has no presence at all. -func SentinelEnv(creds []spec.CredentialCapability) []string { - var env []string - for _, c := range creds { - if c.APIKey != nil && c.APIKey.Name != "" { - env = append(env, c.APIKey.Name+"="+Sentinel) - } - } - return env -} diff --git a/internal/infrastructure/egress/oauth.go b/internal/infrastructure/egress/oauth.go deleted file mode 100644 index 725dfaa0b..000000000 --- a/internal/infrastructure/egress/oauth.go +++ /dev/null @@ -1,316 +0,0 @@ -package egress - -import ( - "bytes" - "context" - "encoding/json" - "errors" - "fmt" - "io" - "net/http" - "slices" - "strconv" - "strings" - "time" - - "github.com/docker/sandbox-kit-spec/v3/spec" - - "github.com/samcharles93/archie-core/internal/domain/access" - "github.com/samcharles93/archie-core/internal/domain/harnesssecret" -) - -// maxTokenExchange bounds a token request or response body the proxy -// buffers to rewrite; real ones are a few hundred bytes. -const maxTokenExchange = 1 << 20 - -// OAuthStore persists one org's OAuth token set per credential binding -// service. It is -// storecontract.HarnessSecretStore's method set, declared narrow here. -type OAuthStore interface { - GetHarnessSecret(ctx context.Context, org, service string) (harnesssecret.Secret, error) - PutHarnessSecret(ctx context.Context, s harnesssecret.Secret) error -} - -// oauthRule is one compiled credential@1 OAuth credential: the token -// endpoint the proxy intercepts, the resource hosts whose requests carry the -// access token, the in-container sentinels and the provider's response -// field names. -type oauthRule struct { - service string - required bool - phases spec.Phases - tokenHost pattern - tokenPath string - resourceHosts []pattern - sentinels spec.Sentinels - responseFields spec.ResponseFields -} - -// IsOAuthManaged reports whether the proxy holds c's tokens: an OAuth -// credential with a token endpoint that is not a passthrough downgrade. -func IsOAuthManaged(c spec.CredentialCapability) bool { - return c.OAuth != nil && !c.OAuth.Passthrough && c.OAuth.TokenEndpoint != nil -} - -// compileOAuthRules compiles the credential@1 OAuth rules this run can -// exercise. A service the run carries as an API key contributes none: its -// token endpoint is not a path this run has a token set to exchange, so -// intercepting it could only try to capture one the org never bound. -func compileOAuthRules(creds []spec.CredentialCapability, bound map[string]CredentialKind) []oauthRule { - var out []oauthRule - for _, c := range creds { - if !IsOAuthManaged(c) || bound[c.Service] == CredentialAPIKey { - continue - } - rule := oauthRule{ - service: c.Service, required: c.Required, phases: slices.Clone(c.Phase), - tokenHost: compilePattern(c.OAuth.TokenEndpoint.Host), tokenPath: c.OAuth.TokenEndpoint.Path, - } - for _, h := range c.OAuth.ResourceHosts { - rule.resourceHosts = append(rule.resourceHosts, compilePattern(h)) - } - if c.OAuth.Sentinels != nil { - rule.sentinels = *c.OAuth.Sentinels - } - if c.OAuth.ResponseFields != nil { - rule.responseFields = *c.OAuth.ResponseFields - } - out = append(out, rule) - } - return out -} - -// tokenEndpointRule returns the session's rule whose token endpoint r is a -// POST to. The caller refuses requests outside its granted phases. -func tokenEndpointRule(s *Session, r *http.Request, host string, port int) (oauthRule, bool) { - if r.Method != http.MethodPost { - return oauthRule{}, false - } - for _, rule := range s.oauth { - if !rule.tokenHost.matches(normalizeHost(host), port) { - continue - } - if rule.tokenPath != "" && r.URL.Path != rule.tokenPath { - continue - } - return rule, true - } - return oauthRule{}, false -} - -// granted reports whether the run credential carries rule's service. The -// org's stored token is read or written only for a granted run: a Kit -// declaring a service does not by itself reach the org's secret. An optional -// ungranted service is skipped; a required one is an error. -func (p *Proxy) granted(ctx context.Context, s *Session, rule oauthRule) (bool, error) { - var err error - switch { - case p.oauth == nil: - err = errors.New("no harness secret store is configured") - case p.resolver == nil: - err = ErrUnbound - default: - _, err = p.resolver.Resolve(ctx, s.credential(), rule.service) - } - switch { - case err == nil: - return true, nil - case errors.Is(err, ErrUnbound) && !rule.required: - return false, nil - case errors.Is(err, ErrUnbound): - return false, fmt.Errorf("credential %q is required and not bound for this run", rule.service) - default: - return false, fmt.Errorf("credential %q: %w", rule.service, err) - } -} - -// injectOAuth swaps the sentinel access token for the stored real one on a -// request to one of a granted rule's resource hosts. A request not carrying -// the sentinel never touches the store. -func (p *Proxy) injectOAuth(ctx context.Context, s *Session, r *http.Request, host string, port int) error { - ctx = s.oauthContext(ctx) - phase := s.phase() - for _, rule := range s.oauth { - if !slices.Contains(rule.phases, phase) || rule.sentinels.AccessToken == "" || - !matchesAny(rule.resourceHosts, normalizeHost(host), port) || !headerCarries(r.Header, rule.sentinels.AccessToken) { - continue - } - ok, err := p.granted(ctx, s, rule) - if err != nil { - return err - } - if !ok { - continue - } - current, err := p.oauth.GetHarnessSecret(ctx, s.org, rule.service) - if err != nil { - return fmt.Errorf("credential %q has no captured OAuth token: %w", rule.service, err) - } - for _, vs := range r.Header { - for i, v := range vs { - vs[i] = strings.ReplaceAll(v, rule.sentinels.AccessToken, current.AccessToken) - } - } - } - return nil -} - -// interceptOAuth proxies a token endpoint request, swapping the sentinel -// refresh token for the stored one. Successful responses are stored and -// returned to the container with sentinels. -func (p *Proxy) interceptOAuth(ctx context.Context, w http.ResponseWriter, r *http.Request, s *Session, rule oauthRule, scheme, target string) { - ctx = s.oauthContext(ctx) - body, err := io.ReadAll(io.LimitReader(r.Body, maxTokenExchange)) - if err != nil { - http.Error(w, "read token request: "+err.Error(), http.StatusBadGateway) - return - } - var current harnesssecret.Secret - if sentinel := rule.sentinels.RefreshToken; sentinel != "" && bytes.Contains(body, []byte(sentinel)) { - current, err = p.oauth.GetHarnessSecret(ctx, s.org, rule.service) - if err != nil { - http.Error(w, "credential "+rule.service+" has no captured OAuth token (run the setup terminal first): "+err.Error(), http.StatusBadGateway) - return - } - body = bytes.ReplaceAll(body, []byte(sentinel), []byte(current.RefreshToken)) - } - out, err := http.NewRequestWithContext(ctx, http.MethodPost, scheme+"://"+target+r.URL.RequestURI(), bytes.NewReader(body)) - if err != nil { - http.Error(w, "build upstream token request: "+err.Error(), http.StatusBadGateway) - return - } - out.Host = r.Host - out.Header = r.Header.Clone() - out.Header.Del("Proxy-Authorization") - out.Header.Del("Proxy-Connection") - // The response is parsed and rewritten here, so it must arrive as - // plain JSON; the transport negotiates and decodes gzip on its own. - out.Header.Del("Accept-Encoding") - resp, err := p.transport.RoundTrip(out) - if err != nil { - http.Error(w, "upstream token endpoint "+target+": "+err.Error(), http.StatusBadGateway) - return - } - defer resp.Body.Close() - respBody, err := io.ReadAll(io.LimitReader(resp.Body, maxTokenExchange)) - if err != nil { - http.Error(w, "read token response: "+err.Error(), http.StatusBadGateway) - return - } - if resp.StatusCode >= 200 && resp.StatusCode < 300 { - captured, err := rule.capture(s.org, respBody, current) - if err == nil { - err = p.oauth.PutHarnessSecret(ctx, captured) - } - if err != nil { - http.Error(w, "capture OAuth token: "+err.Error(), http.StatusBadGateway) - return - } - if respBody, err = rule.sentinelize(respBody); err != nil { - http.Error(w, "rewrite token response: "+err.Error(), http.StatusBadGateway) - return - } - } - for k, vs := range resp.Header { - for _, v := range vs { - w.Header().Add(k, v) - } - } - w.Header().Set("Content-Length", strconv.Itoa(len(respBody))) - w.WriteHeader(resp.StatusCode) - _, _ = w.Write(respBody) -} - -// responseFieldNames returns the provider's token-response field names, -// defaulting to the OAuth 2.0 ones the rule does not override. -func (rule oauthRule) responseFieldNames() (access, refresh, expiresIn string) { - access, refresh, expiresIn = "access_token", "refresh_token", "expires_in" - if rule.responseFields.AccessToken != "" { - access = rule.responseFields.AccessToken - } - if rule.responseFields.RefreshToken != "" { - refresh = rule.responseFields.RefreshToken - } - if rule.responseFields.ExpiresIn != "" { - expiresIn = rule.responseFields.ExpiresIn - } - return access, refresh, expiresIn -} - -// capture parses a successful token response into the token set to store. -// A provider that does not rotate refresh tokens omits the field, so the -// previous refresh token is kept. -func (rule oauthRule) capture(org string, body []byte, previous harnesssecret.Secret) (harnesssecret.Secret, error) { - access, refresh, expiresIn := rule.responseFieldNames() - var parsed map[string]any - if err := json.Unmarshal(body, &parsed); err != nil { - return harnesssecret.Secret{}, fmt.Errorf("parse token response: %w", err) - } - secret := harnesssecret.Secret{Org: org, Service: rule.service, RefreshToken: previous.RefreshToken, Scopes: previous.Scopes} - secret.AccessToken, _ = parsed[access].(string) - if v, ok := parsed[refresh].(string); ok && v != "" { - secret.RefreshToken = v - } - secret.TokenType, _ = parsed["token_type"].(string) - // A provider may report the granted scopes only on the login exchange, so - // an absent (or empty) scope keeps the set the previous capture held -- - // the same rule the refresh token follows. - if v, ok := parsed["scope"].(string); ok && v != "" { - secret.Scopes = strings.Fields(v) - } - if v, ok := parsed[expiresIn].(float64); ok { - secret.ExpiresAt = time.Now().Add(time.Duration(v) * time.Second) - } - if secret.AccessToken == "" { - return harnesssecret.Secret{}, fmt.Errorf("token response has no %q field", access) - } - return secret, nil -} - -// sentinelize replaces a captured response's access and refresh tokens with -// the Kit's sentinels. A token field with no declared sentinel cannot be -// hidden, so it fails rather than hand the container a real token. -func (rule oauthRule) sentinelize(body []byte) ([]byte, error) { - var parsed map[string]any - if err := json.Unmarshal(body, &parsed); err != nil { - return nil, err - } - access, refresh, _ := rule.responseFieldNames() - for field, sentinel := range map[string]string{access: rule.sentinels.AccessToken, refresh: rule.sentinels.RefreshToken} { - if _, ok := parsed[field]; !ok { - continue - } - if sentinel == "" { - return nil, fmt.Errorf("credential %q declares no sentinel for %q", rule.service, field) - } - parsed[field] = sentinel - } - return json.Marshal(parsed) -} - -func matchesAny(patterns []pattern, host string, port int) bool { - for _, p := range patterns { - if p.matches(host, port) { - return true - } - } - return false -} - -func headerCarries(h http.Header, value string) bool { - for _, vs := range h { - for _, v := range vs { - if strings.Contains(v, value) { - return true - } - } - } - return false -} - -func (s *Session) oauthContext(ctx context.Context) context.Context { - if s.principal.IdentityID != "" { - ctx = access.WithPrincipal(ctx, s.principal) - } - return harnesssecret.WithRunCredential(ctx, s.runCredential) -} diff --git a/internal/infrastructure/egress/policy.go b/internal/infrastructure/egress/policy.go index 109788cee..de0ca5543 100644 --- a/internal/infrastructure/egress/policy.go +++ b/internal/infrastructure/egress/policy.go @@ -4,11 +4,9 @@ import ( "net" "strconv" "strings" - - "github.com/docker/sandbox-kit-spec/v3/spec" ) -// pattern is one network-policy@1 entry: an exact host, a host on one port, +// pattern is one allow-list entry: an exact host, a host on one port, // a single-label wildcard, or everything. type pattern struct { everything bool @@ -17,18 +15,9 @@ type pattern struct { port int // 0 means any port } -// rules is a compiled allow/deny pair for one phase. Deny wins; an empty or -// absent phase allows nothing, so egress is closed unless a Kit asks for it. +// rules is a compiled allow list. An empty one allows nothing. type rules struct { allow []pattern - deny []pattern -} - -func compileRules(r *spec.NetworkRules) rules { - if r == nil { - return rules{} - } - return rules{allow: compilePatterns(r.Allow), deny: compilePatterns(r.Deny)} } func compilePatterns(entries []string) []pattern { @@ -79,11 +68,6 @@ func (p pattern) matches(host string, port int) bool { func (r rules) allows(host string, port int) bool { host = normalizeHost(host) - for _, p := range r.deny { - if p.matches(host, port) { - return false - } - } for _, p := range r.allow { if p.matches(host, port) { return true diff --git a/internal/infrastructure/egress/proxy.go b/internal/infrastructure/egress/proxy.go index 189c4b7f6..41c1f295a 100644 --- a/internal/infrastructure/egress/proxy.go +++ b/internal/infrastructure/egress/proxy.go @@ -2,30 +2,18 @@ package egress import ( "context" - "crypto/rand" "crypto/tls" "crypto/x509" "encoding/base64" - "encoding/hex" - "encoding/json" "errors" "fmt" "net" "net/http" "net/http/httputil" - "slices" "strconv" "strings" "sync" - "sync/atomic" "time" - - "github.com/docker/sandbox-kit-spec/v3/spec" - "github.com/modelcontextprotocol/go-sdk/mcp" - - "github.com/samcharles93/archie-core/internal/domain/access" - "github.com/samcharles93/archie-core/internal/events" - "github.com/samcharles93/archie-core/internal/infrastructure/tasktools" ) // proxyUser is the fixed user name in a sandbox's proxy URL; the session @@ -38,117 +26,51 @@ const handshakeTimeout = 10 * time.Second // host addresses; it is as internal as RFC 1918 space. var cgnat = &net.IPNet{IP: net.IPv4(100, 64, 0, 0), Mask: net.CIDRMask(10, 32)} -// Session is one sandbox container's standing with the proxy: its token and -// its Kit's network policy. It starts in the install phase; the runner moves -// it to runtime before the workload's entrypoint starts. +// Session is one container's standing with the proxy: its token, the hosts +// it may reach, and the keys it may have substituted on the way. type Session struct { token string - runCredential string - gatewayToken string - lifetime context.Context //nolint:containedctx // Session lifetime cancels active HTTP requests. - cancel context.CancelFunc - principal access.Principal - org string - install rules - runtime rules - injections []injection + allow rules substitutions []substitution secrets map[string]string - oauth []oauthRule - record *RecordOptions - atRun atomic.Bool } -// Token is the proxy password the container may hold; it is not its task grant. -func (s *Session) Token() string { return s.token } - -// EnterRuntime switches the session from install-phase to runtime-phase -// egress. It is one-way: install egress never reopens. -func (s *Session) EnterRuntime() { s.atRun.Store(true) } - -func (s *Session) phase() string { - if s.atRun.Load() { - return "runtime" - } - return "install" -} - -func (s *Session) rules() rules { - if s.atRun.Load() { - return s.runtime - } - return s.install -} - -// ProxyOptions are the proxy's upstream dependencies. Both are optional: -// the zero value dials the network through the upstream guard and verifies +// ProxyOptions are the proxy's upstream dependencies. All are optional: the +// zero value dials the network through the upstream guard and verifies // upstream TLS against the system roots. type ProxyOptions struct { UpstreamRoots *x509.CertPool Dial func(ctx context.Context, network, addr string) (net.Conn, error) - // Resolver supplies credentials for injection. Without one, a request - // that needs a required credential is refused. + // Resolver supplies the keys a session does not carry itself. Without + // one, a substitution for such a key is refused. Resolver Resolver - // OAuthStore holds the org token sets OAuth credentials use. Without - // one, Register refuses a session with a required OAuth credential. - OAuthStore OAuthStore - TaskTools tasktools.Backend - // Events publishes a recorded delivery on the daemon's event stream, so it - // lands on the task's timeline like every other event. Nil records nowhere. - Events func(events.Event) } -// RecordOptions make a session a test run's: what its container attempts is -// recorded on TaskID's timeline as "would ..." and answered with a synthetic -// success instead of being forwarded. -type RecordOptions struct { - TaskID int64 - // ModelHosts are the hosts the run's model calls go to. They are never - // recorded or swallowed whatever their method: a provider request is how - // the run reads, and a recorded one leaves an operator nothing to look at. - ModelHosts []string -} - -// SessionOptions describe one container's run: the run credential it acts -// under, the org whose OAuth token sets it uses, and its Kit's network -// policy and credential requests. +// SessionOptions describe one container's session. type SessionOptions struct { - // Token is the container's proxy password. RunCredential stays on the host. + // Token is the container's proxy password, and the run credential the + // Resolver narrows keys by. Token string - // RunCredential scopes task OAuth operations; setup sessions forward Principal. - RunCredential string - Principal access.Principal - Org string - Network *spec.PhasedNetwork - // Credentials are what the Kit declares; Bound maps each service the run - // carries to its kind. A rule fires only for a service bound with the same - // kind. - Credentials []spec.CredentialCapability - Bound map[string]CredentialKind + // Allow lists the hosts the container may reach. Nothing else is allowed. + Allow []string // Substitutions swap a service's sentinel for its key on requests to the // service's host. Substitutions []Substitution - // Secrets are credentials the run itself carries, keyed by service. A + // Secrets are keys the run itself carries, keyed by service. A // substitution prefers them over the Resolver, so a run-supplied key needs // no credential binding. Secrets map[string]string - // Record makes this a test run's session. Nil forwards everything, which is - // what a live run and a setup session get. - Record *RecordOptions } -// Proxy is the egress proxy sandbox containers reach through their relay. -// It authenticates each request to a session, applies that session's -// network policy, terminates TLS with the daemon CA, and forwards to the -// real upstream with verified TLS. +// Proxy is the egress proxy task containers send their model calls through. +// It authenticates each request to a session, applies that session's allow +// list, terminates TLS with the daemon CA, substitutes keys, and forwards to +// the real upstream with verified TLS. type Proxy struct { ca *CA dial func(ctx context.Context, network, addr string) (net.Conn, error) transport *http.Transport resolver Resolver - oauth OAuthStore - taskTools tasktools.Backend - events func(events.Event) mu sync.RWMutex sessions map[string]*Session @@ -157,7 +79,7 @@ type Proxy struct { type sessionKey struct{} func NewProxy(ca *CA, opts ProxyOptions) *Proxy { - p := &Proxy{ca: ca, dial: opts.Dial, resolver: opts.Resolver, oauth: opts.OAuthStore, taskTools: opts.TaskTools, events: opts.Events, sessions: map[string]*Session{}} + p := &Proxy{ca: ca, dial: opts.Dial, resolver: opts.Resolver, sessions: map[string]*Session{}} p.transport = &http.Transport{ DialContext: p.dialUpstream, TLSClientConfig: &tls.Config{RootCAs: opts.UpstreamRoots, MinVersion: tls.VersionTLS12}, @@ -167,32 +89,14 @@ func NewProxy(ca *CA, opts ProxyOptions) *Proxy { return p } -// Register opens a session for one container. A nil network policy allows -// no egress in either phase. +// Register opens a session for one container. func (p *Proxy) Register(opts SessionOptions) (*Session, error) { if opts.Token == "" { return nil, errors.New("egress session needs the run credential") } s := &Session{ - token: opts.Token, org: opts.Org, runCredential: opts.RunCredential, principal: opts.Principal, - injections: compileInjections(opts.Credentials, opts.Bound), oauth: compileOAuthRules(opts.Credentials, opts.Bound), - substitutions: compileSubstitutions(opts.Substitutions), secrets: opts.Secrets, record: opts.Record, - } - if opts.RunCredential != "" && p.taskTools != nil { - var token [32]byte - if _, err := rand.Read(token[:]); err != nil { - return nil, err - } - s.gatewayToken = hex.EncodeToString(token[:]) - s.lifetime, s.cancel = context.WithCancel(context.Background()) - } - for _, rule := range s.oauth { - if rule.required && p.oauth == nil { - return nil, fmt.Errorf("credential %q is OAuth-managed and no harness secret store is configured", rule.service) - } - } - if opts.Network != nil { - s.install, s.runtime = compileRules(opts.Network.Install), compileRules(opts.Network.Runtime) + token: opts.Token, allow: rules{allow: compilePatterns(opts.Allow)}, + substitutions: compileSubstitutions(opts.Substitutions), secrets: opts.Secrets, } p.mu.Lock() p.sessions[s.token] = s @@ -201,17 +105,10 @@ func (p *Proxy) Register(opts SessionOptions) (*Session, error) { } // Revoke ends a session. Requests already forwarded finish; new ones fail. -func (p *Proxy) Revoke(parent context.Context, token string) { +func (p *Proxy) Revoke(token string) { p.mu.Lock() - s := p.sessions[token] delete(p.sessions, token) p.mu.Unlock() - if s != nil && s.cancel != nil { - s.cancel() - ctx, cancel := context.WithTimeout(context.WithoutCancel(parent), 10*time.Second) - defer cancel() - _ = p.taskTools.Close(ctx, s.runCredential) - } } func (p *Proxy) session(r *http.Request) *Session { @@ -239,10 +136,6 @@ func (p *Proxy) ServeHTTP(w http.ResponseWriter, r *http.Request) { http.Error(w, "proxy authentication required", http.StatusProxyAuthRequired) return } - if normalizeHost(r.URL.Hostname()) == "archie.tools" { - p.serveTaskTools(r.Context(), w, r, s) - return - } switch { case r.Method == http.MethodConnect: p.serveConnect(w, r, s) @@ -252,7 +145,7 @@ func (p *Proxy) ServeHTTP(w http.ResponseWriter, r *http.Request) { http.Error(w, err.Error(), http.StatusBadRequest) return } - if !s.rules().allows(host, port) { + if !s.allow.allows(host, port) { http.Error(w, "egress to "+net.JoinHostPort(host, strconv.Itoa(port))+" is not allowed", http.StatusForbidden) return } @@ -268,7 +161,7 @@ func (p *Proxy) serveConnect(w http.ResponseWriter, r *http.Request, s *Session) http.Error(w, err.Error(), http.StatusBadRequest) return } - if !s.rules().allows(host, port) { + if !s.allow.allows(host, port) { http.Error(w, "egress to "+net.JoinHostPort(host, strconv.Itoa(port))+" is not allowed", http.StatusForbidden) return } @@ -311,7 +204,7 @@ func (p *Proxy) serveConnect(w http.ResponseWriter, r *http.Request, s *Session) http.Error(w, "request host does not match its tunnel", http.StatusMisdirectedRequest) return } - if !s.rules().allows(host, port) { + if !s.allow.allows(host, port) { http.Error(w, "egress to "+net.JoinHostPort(host, strconv.Itoa(port))+" is no longer allowed", http.StatusForbidden) return } @@ -324,44 +217,9 @@ func (p *Proxy) serveConnect(w http.ResponseWriter, r *http.Request, s *Session) func (p *Proxy) forward(ctx context.Context, w http.ResponseWriter, r *http.Request, s *Session, scheme, host string, port int) { target := net.JoinHostPort(host, strconv.Itoa(port)) - if s.records(r.Method, host) { - // Recorded, never forwarded: an empty 2xx is all the client needs to - // carry on, and it must not learn which destinations are real. - p.recordDelivery(s, r.Method+" "+host+r.URL.Path) - w.WriteHeader(http.StatusOK) - return - } - if rule, ok := tokenEndpointRule(s, r, host, port); ok { - if !slices.Contains(rule.phases, s.phase()) { - http.Error(w, "credential "+rule.service+" is not available in this phase", http.StatusForbidden) - return - } - granted, err := p.granted(ctx, s, rule) - if err != nil { - http.Error(w, "egress to "+target+": "+err.Error(), http.StatusBadGateway) - return - } - if !granted { - // The request is to a declared OAuth credential's token endpoint - // but this run is not granted the service. Proxying it would hand - // the container the provider's real tokens un-sentinelized, so the - // token endpoint is refused rather than forwarded. - http.Error(w, "credential "+rule.service+" is not bound for this run", http.StatusForbidden) - return - } - p.interceptOAuth(ctx, w, r, s, rule, scheme, target) - return - } - // Injection happens before anything is sent, so a credential that - // cannot be resolved never reaches upstream half-applied. - err := p.inject(ctx, s, r, host, port) - if err == nil { - err = p.injectOAuth(ctx, s, r, host, port) - } - if err == nil { - err = p.substitute(ctx, s, r, host, port) - } - if err != nil { + // Substitution happens before anything is sent, so a key that cannot be + // resolved never reaches upstream half-applied. + if err := p.substitute(ctx, s, r, host, port); err != nil { http.Error(w, "egress to "+target+": "+err.Error(), http.StatusBadGateway) return } @@ -383,26 +241,6 @@ func (p *Proxy) forward(ctx context.Context, w http.ResponseWriter, r *http.Requ rp.ServeHTTP(w, r) } -// records reports whether this request is a delivery a recording session must -// record instead of perform: a write, to a host the run's model traffic does -// not go to. Reads are never deliveries. -func (s *Session) records(method, host string) bool { - switch method { - case http.MethodGet, http.MethodHead, http.MethodOptions: - return false - } - return s.record != nil && !slices.Contains(s.record.ModelHosts, host) -} - -// recordDelivery reports one delivery a recording session never made, as -// "would ..." on the task's timeline. -func (p *Proxy) recordDelivery(s *Session, detail string) { - if p.events == nil || s.record == nil { - return - } - p.events(events.Event{Kind: events.KindDeliveryRecorded, TaskID: s.record.TaskID, Detail: "would " + detail}) -} - // dialUpstream resolves and dials an upstream, refusing addresses that // would turn the proxy into a path back into the host or its networks. func (p *Proxy) dialUpstream(ctx context.Context, network, addr string) (net.Conn, error) { @@ -428,7 +266,7 @@ func (p *Proxy) dialUpstream(ctx context.Context, network, addr string) (net.Con } var guardErr error for _, ip := range ips { - named := s != nil && s.rules().namesIP(ip) + named := s != nil && s.allow.namesIP(ip) if guardErr = guardUpstream(ip, named); guardErr != nil { continue } @@ -514,50 +352,3 @@ func (c *closeNotifyConn) Close() error { c.once.Do(func() { close(c.done) }) return c.Conn.Close() } - -func (p *Proxy) serveTaskTools(parent context.Context, w http.ResponseWriter, r *http.Request, s *Session) { - if s.gatewayToken == "" || s.phase() != "runtime" || r.URL.String() != tasktools.Endpoint || r.Host != "archie.tools" { - http.Error(w, "task tools are unavailable for this session or target", http.StatusForbidden) - return - } - if r.Header.Get("Authorization") != "Bearer "+tasktools.Sentinel { - http.Error(w, "task tools sentinel required", http.StatusUnauthorized) - return - } - // Only this authenticated proxy hop supplies the host-held gateway token. - r = r.Clone(parent) - r.Header.Set("Authorization", "Bearer "+s.gatewayToken) - r.Header.Del("Proxy-Authorization") - - ctx, cancel := context.WithCancel(parent) - defer cancel() - stop := context.AfterFunc(s.lifetime, cancel) //nolint:contextcheck // Session revocation is an additional cancellation source. - defer stop() - backend := p.taskTools - if s.record != nil { - // A central tool call is a delivery too. Discovery still runs: an agent - // that cannot list its tools cannot use any. - backend = recordedTools{Backend: backend, proxy: p, session: s} - } - tasktools.ServeHTTP(ctx, w, r, backend, s.runCredential, s.gatewayToken) -} - -// recordedTools records each central tool call instead of making it. -type recordedTools struct { - tasktools.Backend - proxy *Proxy - session *Session -} - -func (t recordedTools) Call(_ context.Context, _, tool string, _ json.RawMessage) (*mcp.CallToolResult, error) { - t.proxy.recordDelivery(t.session, "call "+tool) - return &mcp.CallToolResult{Content: []mcp.Content{&mcp.TextContent{Text: "would call " + tool}}}, nil -} - -// credential is the host-held task grant, or a setup session's own grant. -func (s *Session) credential() string { - if s.runCredential != "" { - return s.runCredential - } - return s.token -} diff --git a/internal/infrastructure/egress/proxy_record_test.go b/internal/infrastructure/egress/proxy_record_test.go deleted file mode 100644 index c360af81c..000000000 --- a/internal/infrastructure/egress/proxy_record_test.go +++ /dev/null @@ -1,140 +0,0 @@ -package egress - -import ( - "context" - "encoding/json" - "net" - "net/http" - "net/http/httptest" - "sync/atomic" - "testing" - - "github.com/modelcontextprotocol/go-sdk/mcp" - - "github.com/samcharles93/archie-core/internal/events" -) - -// A recording session forwards none of the run's deliveries: each is answered -// with a synthetic success and reported on the task, so an operator sees what -// the draft would have sent. Reads still pass, and so do model calls whatever -// their method -- a recorded one would leave the run nothing to read. -func TestRecordedSessionRecordsDeliveries(t *testing.T) { - t.Parallel() - - cases := []struct { - name string - method string - host string - modelHosts []string - wantDetail string // empty means the request must be forwarded - }{ - {name: "a write is recorded and not forwarded", method: http.MethodPost, host: "hooks.example", wantDetail: "would POST hooks.example/hook"}, - {name: "a read is forwarded", method: http.MethodGet, host: "hooks.example"}, - {name: "a write to a model host is forwarded", method: http.MethodPost, host: "api.model.example", modelHosts: []string{"api.model.example"}}, - } - for _, tc := range cases { - t.Run(tc.name, func(t *testing.T) { - t.Parallel() - - var forwarded atomic.Bool - upstream := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { - forwarded.Store(true) - w.WriteHeader(http.StatusNoContent) - })) - defer upstream.Close() - - var recorded []events.Event - proxy := NewProxy(nil, ProxyOptions{ - Dial: func(ctx context.Context, _, _ string) (net.Conn, error) { - return (&net.Dialer{}).DialContext(ctx, "tcp", upstream.Listener.Addr().String()) - }, - Events: func(event events.Event) { recorded = append(recorded, event) }, - }) - session, err := proxy.Register(SessionOptions{Token: "proxy-password", Record: &RecordOptions{TaskID: 7, ModelHosts: tc.modelHosts}}) - if err != nil { - t.Fatalf("register session: %v", err) - } - - req := httptest.NewRequestWithContext(context.Background(), tc.method, "http://"+tc.host+"/hook", nil) - rec := httptest.NewRecorder() - proxy.forward(context.Background(), rec, req, session, "http", tc.host, 80) - - if tc.wantDetail == "" { - if !forwarded.Load() { - t.Fatalf("%s was not forwarded: %d %q", tc.method, rec.Code, rec.Body.String()) - } - if len(recorded) != 0 { - t.Fatalf("a forwarded request was recorded: %+v", recorded) - } - return - } - if forwarded.Load() { - t.Fatalf("%s reached the upstream", tc.method) - } - if rec.Code != http.StatusOK || rec.Body.Len() != 0 { - t.Fatalf("recorded request answered %d %q, want an empty 200", rec.Code, rec.Body.String()) - } - if len(recorded) != 1 || recorded[0].Kind != events.KindDeliveryRecorded || - recorded[0].TaskID != 7 || recorded[0].Detail != tc.wantDetail { - t.Fatalf("recorded %+v, want one %q on task 7", recorded, tc.wantDetail) - } - }) - } -} - -// A recording session performs no central tool call, so a mutating tool cannot -// act for real during a test run. Discovery still reaches the gateway: a run -// that cannot list its tools cannot use any. -func TestRecordedToolsPerformNoCall(t *testing.T) { - t.Parallel() - - var listed, called atomic.Int32 - backend := stubBackend{ - list: func(context.Context, string) ([]*mcp.Tool, error) { - listed.Add(1) - return []*mcp.Tool{{Name: "memory_create"}}, nil - }, - call: func(context.Context, string, string, json.RawMessage) (*mcp.CallToolResult, error) { - called.Add(1) - return nil, nil - }, - } - var recorded []events.Event - proxy := NewProxy(nil, ProxyOptions{Events: func(event events.Event) { recorded = append(recorded, event) }}) - session := &Session{record: &RecordOptions{TaskID: 7}} - tools := recordedTools{Backend: backend, proxy: proxy, session: session} - - if _, err := tools.List(context.Background(), "grant"); err != nil || listed.Load() != 1 { - t.Fatalf("discovery did not reach the gateway: listed=%d err=%v", listed.Load(), err) - } - result, err := tools.Call(context.Background(), "grant", "memory_create", nil) - if err != nil { - t.Fatalf("a recorded call reports success: %v", err) - } - if called.Load() != 0 { - t.Fatalf("a recorded call reached the gateway") - } - text, ok := result.Content[0].(*mcp.TextContent) - if !ok || text.Text != "would call memory_create" { - t.Fatalf("answered %+v", result.Content) - } - if len(recorded) != 1 || recorded[0].Kind != events.KindDeliveryRecorded || - recorded[0].TaskID != 7 || recorded[0].Detail != "would call memory_create" { - t.Fatalf("recorded %+v, want one \"would call memory_create\" on task 7", recorded) - } -} - -type stubBackend struct { - list func(context.Context, string) ([]*mcp.Tool, error) - call func(context.Context, string, string, json.RawMessage) (*mcp.CallToolResult, error) -} - -func (b stubBackend) List(ctx context.Context, credential string) ([]*mcp.Tool, error) { - return b.list(ctx, credential) -} - -func (b stubBackend) Call(ctx context.Context, credential, tool string, args json.RawMessage) (*mcp.CallToolResult, error) { - return b.call(ctx, credential, tool, args) -} - -func (b stubBackend) Close(context.Context, string) error { return nil } diff --git a/internal/infrastructure/kitrun/env.go b/internal/infrastructure/egress/sandbox_env.go similarity index 76% rename from internal/infrastructure/kitrun/env.go rename to internal/infrastructure/egress/sandbox_env.go index ce96ebcd3..f07369deb 100644 --- a/internal/infrastructure/kitrun/env.go +++ b/internal/infrastructure/egress/sandbox_env.go @@ -1,4 +1,4 @@ -package kitrun +package egress import ( "fmt" @@ -6,12 +6,10 @@ import ( "net/url" "strconv" "strings" - - "github.com/samcharles93/archie-core/internal/infrastructure/egress" ) -// Endpoints are the host-reachable addresses a Kit task's worker needs, -// with the task's own credentials for them. +// Endpoints are the host-reachable addresses a sandboxed task's worker +// needs, with the task's own credentials for them. type Endpoints struct { NATS string NATSToken string @@ -20,7 +18,7 @@ type Endpoints struct { StateStoreCA []byte } -// WorkerEnv is the worker's entire environment in a Kit container: NATS and +// WorkerEnv is the worker's entire environment in a sandboxed container: NATS and // the State Store at the relay's fixed ports, and the task's credentials. // Nothing else crosses, so no model provider key enters the container. func WorkerEnv(e Endpoints, uid, gid int) ([]string, error) { @@ -28,12 +26,12 @@ func WorkerEnv(e Endpoints, uid, gid int) ([]string, error) { if err != nil || u.Scheme == "" { return nil, fmt.Errorf("nats url %q: want scheme://host:port", e.NATS) } - env := []string{"NATS_URL=" + u.Scheme + "://" + relayAddr(egress.RelayNATSPort)} + env := []string{"NATS_URL=" + u.Scheme + "://" + relayAddr(RelayNATSPort)} if e.NATSToken != "" { env = append(env, "NATS_TOKEN="+e.NATSToken) } if e.StateStore != "" { - env = append(env, "STATE_STORE_URL="+relayAddr(egress.RelayStateStorePort)) + env = append(env, "STATE_STORE_URL="+relayAddr(RelayStateStorePort)) if e.StateStoreToken != "" { env = append(env, "STATE_STORE_TOKEN="+e.StateStoreToken) } @@ -62,7 +60,7 @@ func relayTargets(e Endpoints) (nats, stateStore string, err error) { } func relayAddr(port int) string { - return net.JoinHostPort(egress.RelayAlias, strconv.Itoa(port)) + return net.JoinHostPort(RelayAlias, strconv.Itoa(port)) } // RelayCommand is the relay's archie-agent arguments, forwarding its fixed @@ -72,5 +70,5 @@ func RelayCommand(proxy string, e Endpoints) ([]string, error) { if err != nil { return nil, err } - return egress.RelayArgs(proxy, nats, stateStore), nil + return RelayArgs(proxy, nats, stateStore), nil } diff --git a/internal/infrastructure/egress/substitute.go b/internal/infrastructure/egress/substitute.go index 07678e12a..24f37c0ea 100644 --- a/internal/infrastructure/egress/substitute.go +++ b/internal/infrastructure/egress/substitute.go @@ -72,7 +72,7 @@ func (p *Proxy) keyFor(ctx context.Context, s *Session, service string) (string, if p.resolver == nil { return "", fmt.Errorf("credential %q has no resolver", service) } - key, err := p.resolver.Resolve(ctx, s.credential(), service) + key, err := p.resolver.Resolve(ctx, s.token, service) if errors.Is(err, ErrUnbound) { return "", fmt.Errorf("credential %q is not bound for this run", service) } diff --git a/internal/infrastructure/egress/task_tools_test.go b/internal/infrastructure/egress/task_tools_test.go deleted file mode 100644 index 2b540e9aa..000000000 --- a/internal/infrastructure/egress/task_tools_test.go +++ /dev/null @@ -1,83 +0,0 @@ -package egress - -import ( - "context" - "encoding/base64" - "encoding/json" - "net/http" - "net/http/httptest" - "strings" - "testing" - - "github.com/modelcontextprotocol/go-sdk/mcp" - - "github.com/samcharles93/archie-core/internal/infrastructure/tasktools" -) - -type proxyTaskTools struct{ calls int } - -func (b *proxyTaskTools) List(context.Context, string) ([]*mcp.Tool, error) { - b.calls++ - return []*mcp.Tool{}, nil -} - -func (*proxyTaskTools) Call(context.Context, string, string, json.RawMessage) (*mcp.CallToolResult, error) { - return &mcp.CallToolResult{}, nil -} -func (*proxyTaskTools) Close(context.Context, string) error { return nil } - -func TestTaskToolsProxyConfinement(t *testing.T) { - for _, tt := range []struct { - name, target, auth string - task, runtime, revoked bool - want int - }{ - {name: "runtime", target: tasktools.Endpoint, auth: tasktools.Sentinel, task: true, runtime: true, want: http.StatusOK}, - {name: "install", target: tasktools.Endpoint, auth: tasktools.Sentinel, task: true, want: http.StatusForbidden}, - {name: "setup", target: tasktools.Endpoint, auth: tasktools.Sentinel, runtime: true, want: http.StatusForbidden}, - {name: "revoked", target: tasktools.Endpoint, auth: tasktools.Sentinel, task: true, runtime: true, revoked: true, want: http.StatusProxyAuthRequired}, - {name: "forged bearer", target: tasktools.Endpoint, auth: "forged", task: true, runtime: true, want: http.StatusUnauthorized}, - {name: "other path", target: "http://archie.tools/other", auth: tasktools.Sentinel, task: true, runtime: true, want: http.StatusForbidden}, - {name: "query", target: tasktools.Endpoint + "?target=victim", auth: tasktools.Sentinel, task: true, runtime: true, want: http.StatusForbidden}, - {name: "other port", target: "http://archie.tools:8080/mcp", auth: tasktools.Sentinel, task: true, runtime: true, want: http.StatusForbidden}, - {name: "other host", target: "http://attacker.example/mcp", auth: tasktools.Sentinel, task: true, runtime: true, want: http.StatusForbidden}, - } { - t.Run(tt.name, func(t *testing.T) { - backend := &proxyTaskTools{} - proxy := NewProxy(nil, ProxyOptions{TaskTools: backend}) - opts := SessionOptions{Token: "proxy-password"} - if tt.task { - opts.RunCredential = "run-grant" - } - session, err := proxy.Register(opts) - if err != nil { - t.Fatal(err) - } - if tt.runtime { - session.EnterRuntime() - } - if tt.revoked { - proxy.Revoke(t.Context(), session.Token()) - } - if session.gatewayToken != "" && (session.gatewayToken == session.Token() || session.gatewayToken == tasktools.Sentinel) { - t.Fatal("gateway token is container-visible") - } - req := httptest.NewRequestWithContext(t.Context(), http.MethodPost, tt.target, strings.NewReader(`{"jsonrpc":"2.0","id":1,"method":"initialize","params":{"protocolVersion":"2025-03-26","capabilities":{},"clientInfo":{"name":"test","version":"1"}}}`)) - req.Header.Set("Proxy-Authorization", "Basic "+base64.StdEncoding.EncodeToString([]byte("archie:"+session.Token()))) - req.Header.Set("Authorization", "Bearer "+tt.auth) - req.Header.Set("Content-Type", "application/json") - req.Header.Set("Accept", "application/json, text/event-stream") - response := httptest.NewRecorder() - proxy.ServeHTTP(response, req) - if response.Code != tt.want { - t.Fatalf("status %d, want %d: %s", response.Code, tt.want, response.Body.String()) - } - if tt.want != http.StatusOK && backend.calls != 0 { - t.Fatal("rejected request reached gateway") - } - if session.gatewayToken != "" && strings.Contains(response.Body.String(), session.gatewayToken) { - t.Fatal("gateway token escaped in response") - } - }) - } -} diff --git a/internal/infrastructure/harnessrpc/harnessrpc.go b/internal/infrastructure/harnessrpc/harnessrpc.go deleted file mode 100644 index 1280fb1e9..000000000 --- a/internal/infrastructure/harnessrpc/harnessrpc.go +++ /dev/null @@ -1,162 +0,0 @@ -// Package harnessrpc adapts the daemon-served harness session contract -// (internal/contracts/harness/v1) to the dashboard's HarnessTerminal -// interface and the daemon's gRPC listener. The daemon owns the container, so -// this is the only contract a session crosses. -package harnessrpc - -import ( - "context" - "errors" - "fmt" - "io" - "strings" - "sync" - - "google.golang.org/grpc" - - pb "github.com/samcharles93/archie-core/internal/contracts/harness/v1" - "github.com/samcharles93/archie-core/internal/domain/presence" - "github.com/samcharles93/archie-core/internal/infrastructure/rpcidentity" - "github.com/samcharles93/archie-core/internal/infrastructure/rpctls" - "github.com/samcharles93/archie-core/internal/infrastructure/telemetry" -) - -// Client opens sessions against a daemon's HarnessService. -type Client struct { - svc pb.HarnessServiceClient -} - -// Dial returns a harness client for target. A session is a shell in a -// container that holds an org's credential, so it needs a service -// certificate even on loopback. The cleanup closes the connection. -func Dial(target string, id *rpctls.Identity, options ...grpc.DialOption) (*Client, func(), error) { - target = strings.TrimSpace(target) - if target == "" { - return nil, nil, errors.New("harness target is required") - } - if id == nil { - return nil, nil, errors.New("harness sessions need [tls] ca_file and cert_dir") - } - creds, err := id.DialOption(target, presence.Daemon) - if err != nil { - return nil, nil, fmt.Errorf("harness: %w", err) - } - conn, err := grpc.NewClient(target, append([]grpc.DialOption{creds, telemetry.ClientOption()}, options...)...) - if err != nil { - return nil, nil, fmt.Errorf("create harness client: %w", err) - } - return &Client{svc: pb.NewHarnessServiceClient(conn)}, func() { _ = conn.Close() }, nil -} - -// Open starts a setup session for profile at the requested terminal size and -// returns its duplex stream. The returned session satisfies the dashboard's -// HarnessTerminal interface. A failure the server reports before any output -// arrives is returned here; a failure after the session is open surfaces on -// Read. -func (c *Client) Open(ctx context.Context, orgID, profile string, rows, cols int) (io.ReadWriteCloser, error) { - streamCtx, cancel := context.WithCancel(ctx) - stream, err := c.svc.Open(rpcidentity.Outgoing(streamCtx, presence.UI)) - if err != nil { - cancel() - return nil, err - } - target := &pb.OpenRequest{ - Frame: &pb.OpenRequest_Target{Target: &pb.OpenTarget{ - Org: orgID, - Target: &pb.OpenTarget_Setup{Setup: &pb.SetupTarget{Profile: profile}}, - }}, - // The daemon clamps this; zero means "use your default". - Size: &pb.ConsoleSize{Rows: uint32(max(rows, 0)), Cols: uint32(max(cols, 0))}, - } - if err := stream.Send(target); err != nil { - cancel() - return nil, err - } - return &session{stream: stream, cancel: cancel}, nil -} - -// OpenRun starts a read-only view of a running task's sandbox and returns its -// output. The daemon refuses a finished or unknown task or attempt, and that -// reason is returned here, before any output, as a gRPC status the caller can -// map. Writes to the returned stream are not sent: a view has no stdin. -func (c *Client) OpenRun(ctx context.Context, orgID string, taskID, attempt int64) (io.ReadCloser, error) { - streamCtx, cancel := context.WithCancel(ctx) - stream, err := c.svc.Open(rpcidentity.Outgoing(streamCtx, presence.UI)) - if err != nil { - cancel() - return nil, err - } - target := &pb.OpenRequest{Frame: &pb.OpenRequest_Target{Target: &pb.OpenTarget{ - Org: orgID, - Target: &pb.OpenTarget_Run{Run: &pb.RunTarget{TaskId: taskID, Attempt: attempt}}, - }}} - if err := stream.Send(target); err != nil { - cancel() - return nil, err - } - s := &session{stream: stream, cancel: cancel} - // The first frame decides whether the view opened: a refusal arrives as - // the stream's status before any output. - s.mu.Lock() - err = s.recv() - s.mu.Unlock() - if err != nil && !errors.Is(err, io.EOF) { - cancel() - return nil, err - } - return s, nil -} - -// session adapts the bidi stream to io.ReadWriteCloser. Bytes are written as -// stdin frames and read from stdout frames; an error frame or an exit code -// ends the stream. -type session struct { - stream pb.HarnessService_OpenClient - cancel context.CancelFunc - - mu sync.Mutex - pending []byte -} - -func (s *session) Write(p []byte) (int, error) { - if err := s.stream.Send(&pb.OpenRequest{Frame: &pb.OpenRequest_Stdin{Stdin: p}}); err != nil { - return 0, err - } - return len(p), nil -} - -func (s *session) Read(p []byte) (int, error) { - s.mu.Lock() - defer s.mu.Unlock() - for len(s.pending) == 0 { - if err := s.recv(); err != nil { - return 0, err - } - } - n := copy(p, s.pending) - s.pending = s.pending[n:] - return n, nil -} - -// recv reads one server frame into pending; an empty stdout frame leaves it -// empty. -func (s *session) recv() error { - resp, err := s.stream.Recv() - if err != nil { - return err - } - switch frame := resp.GetFrame().(type) { - case *pb.OpenResponse_Stdout: - s.pending = frame.Stdout - case *pb.OpenResponse_ExitCode: - return io.EOF - case *pb.OpenResponse_Error: - return errors.New(frame.Error) - } - return nil -} - -func (s *session) Close() error { - s.cancel() - return nil -} diff --git a/internal/infrastructure/harnesssession/harnesssession.go b/internal/infrastructure/harnesssession/harnesssession.go deleted file mode 100644 index 63ef802d4..000000000 --- a/internal/infrastructure/harnesssession/harnesssession.go +++ /dev/null @@ -1,160 +0,0 @@ -// Package harnesssession runs the daemon side of a harness session: it builds -// an ephemeral Kit-profile container, opens a PTY into it as the Kit's harness -// user, and tears the container down when the session closes. It owns no -// transport; internal/app/archied serves the gRPC contract over it. -package harnesssession - -import ( - "context" - "crypto/rand" - "encoding/hex" - "errors" - "fmt" - "sync" - "time" - - "github.com/moby/moby/client" - - "github.com/samcharles93/archie-core/internal/config" - "github.com/samcharles93/archie-core/internal/container" - "github.com/samcharles93/archie-core/internal/infrastructure/kitrun" -) - -// The address errors a caller distinguishes: an unknown profile is the -// caller's mistake, not a daemon that cannot run containers. -var ( - ErrProfileUnknown = errors.New("agent profile is not configured") - ErrProfileNotKit = errors.New("agent profile is not a Kit profile") - // ErrNoShell reports a Kit image that provides no login shell, so a setup - // terminal cannot be opened in it. - ErrNoShell = errors.New("the Kit image provides no shell") -) - -// Launcher starts and ends a setup session's container. *kitrun.Launcher -// satisfies it. -type Launcher interface { - LaunchSetup(ctx context.Context, req kitrun.SetupRequest) (*kitrun.Run, error) - Release(ctx context.Context, run *kitrun.Run) error -} - -// Manager opens setup sessions. -type Manager struct { - Launcher Launcher - // Client is the container pool's Docker client, used for the PTY exec. - Client *client.Client - // Profile resolves an agent profile by name from the running config. - Profile func(name string) (config.AgentProfile, error) - // TTL bounds a session's life. Zero means the container pool's max-uptime - // reaper is the only bound. - TTL time.Duration -} - -// Terminal geometry: the fallback when the client asks for no size, and the -// largest the PTY is created with. -const ( - terminalDefaultRows = 24 - terminalDefaultCols = 80 - terminalMaxCells = 1000 -) - -// Open starts a setup session at the requested terminal size, in character -// cells, and returns its duplex PTY. A zero or out-of-range size falls back to -// the default rather than failing the open. -func (m *Manager) Open(ctx context.Context, org, profile string, rows, cols int) (*Session, error) { - p, err := m.Profile(profile) - if err != nil { - return nil, fmt.Errorf("%w: %s", ErrProfileUnknown, profile) - } - if !p.IsKit() { - return nil, fmt.Errorf("%w: %s", ErrProfileNotKit, profile) - } - id, err := sessionID() - if err != nil { - return nil, err - } - run, err := m.Launcher.LaunchSetup(ctx, kitrun.SetupRequest{Session: id, Kit: p.Kit, Org: org}) - if err != nil { - return nil, err - } - shell, err := pickShell(func(argv []string) (int, error) { - code, _, err := container.ExecIn(ctx, m.Client, run.Container.ID, run.Harness.User, run.Harness.Env, argv) - return code, err - }) - if err != nil { - _ = m.Launcher.Release(context.WithoutCancel(ctx), run) - return nil, fmt.Errorf("%w: %s", ErrNoShell, p.Kit) - } - pty, err := container.ExecPTY(ctx, m.Client, run.Container.ID, run.Harness.User, run.Harness.Env, shell, - uint(terminalSize(rows, terminalDefaultRows)), uint(terminalSize(cols, terminalDefaultCols))) - if err != nil { - _ = m.Launcher.Release(context.WithoutCancel(ctx), run) - return nil, err - } - s := &Session{pty: pty, run: run, release: m.Launcher.Release} - if m.TTL > 0 { - //nolint:contextcheck // teardown must outlive the stream context that opened the session - s.timer = time.AfterFunc(m.TTL, func() { _ = s.Close() }) - } - return s, nil -} - -// Session is one open setup session. Close is idempotent. -type Session struct { - pty *container.PTY - run *kitrun.Run - release func(context.Context, *kitrun.Run) error - timer *time.Timer - - once sync.Once -} - -func (s *Session) Read(p []byte) (int, error) { return s.pty.Read(p) } -func (s *Session) Write(p []byte) (int, error) { return s.pty.Write(p) } - -// ExitCode reports the shell's exit status once its output has ended. -func (s *Session) ExitCode(ctx context.Context) (int, error) { return s.pty.ExitCode(ctx) } - -// Close ends the PTY and tears the session down: the container, its volumes, -// its network and its egress session. Teardown uses its own context because -// the session's is usually already cancelled. -func (s *Session) Close() error { - var err error - s.once.Do(func() { - if s.timer != nil { - s.timer.Stop() - } - _ = s.pty.Close() - err = s.release(context.Background(), s.run) - }) - return err -} - -// terminalSize bounds a requested row or column count. -func terminalSize(v, def int) int { - if v < 1 || v > terminalMaxCells { - return def - } - return v -} - -// pickShell returns the login shell the image provides, preferring bash over -// sh, or ErrNoShell when it provides neither. The image, not archie, decides -// what is installed, so the probe runs in the container. -func pickShell(probe func(argv []string) (int, error)) ([]string, error) { - for _, shell := range []string{"bash", "sh"} { - if code, err := probe([]string{shell, "-c", "exit 0"}); err == nil && code == 0 { - return []string{shell, "-l"}, nil - } - } - return nil, ErrNoShell -} - -// sessionID is the per-session key for the container, network and volumes. It -// is random so two sessions for one profile never collide on a container name. -func sessionID() (string, error) { - var b [8]byte - if _, err := rand.Read(b[:]); err != nil { - return "", fmt.Errorf("mint session id: %w", err) - } - return "setup-" + hex.EncodeToString(b[:]), nil -} diff --git a/internal/infrastructure/harnesssession/harnesssession_test.go b/internal/infrastructure/harnesssession/harnesssession_test.go deleted file mode 100644 index c61356088..000000000 --- a/internal/infrastructure/harnesssession/harnesssession_test.go +++ /dev/null @@ -1,54 +0,0 @@ -package harnesssession - -import ( - "errors" - "slices" - "testing" -) - -// A setup terminal must run a shell the image provides, not one archie assumes. -// bash is preferred when both exist; neither is the image's honest failure. -func TestPickShell(t *testing.T) { - t.Parallel() - - cases := []struct { - name string - have map[string]bool - failing bool - want []string - wantErr bool - }{ - {name: "bash wins when both exist", have: map[string]bool{"bash": true, "sh": true}, want: []string{"bash", "-l"}}, - {name: "sh when bash is absent", have: map[string]bool{"sh": true}, want: []string{"sh", "-l"}}, - {name: "neither shell is an error", have: map[string]bool{}, wantErr: true}, - {name: "a probe that cannot run is not a shell", failing: true, wantErr: true}, - } - - for _, tc := range cases { - t.Run(tc.name, func(t *testing.T) { - t.Parallel() - probe := func(argv []string) (int, error) { - if tc.failing { - return 0, errors.New("exec failed") - } - if tc.have[argv[0]] { - return 0, nil - } - return 127, nil - } - got, err := pickShell(probe) - if tc.wantErr { - if !errors.Is(err, ErrNoShell) { - t.Fatalf("pickShell() = %v, want ErrNoShell", err) - } - return - } - if err != nil { - t.Fatalf("pickShell() = %v", err) - } - if !slices.Equal(got, tc.want) { - t.Errorf("pickShell() = %v, want %v", got, tc.want) - } - }) - } -} diff --git a/internal/infrastructure/kit/credential_file.go b/internal/infrastructure/kit/credential_file.go deleted file mode 100644 index 58cb3de39..000000000 --- a/internal/infrastructure/kit/credential_file.go +++ /dev/null @@ -1,199 +0,0 @@ -package kit - -import ( - "encoding/json" - "fmt" - "strings" - - "github.com/BurntSushi/toml" - "github.com/docker/sandbox-kit-spec/v3/spec" -) - -// Credential-file placeholders. -const ( - accessPlaceholder = "{{.AccessToken}}" - refreshPlaceholder = "{{.RefreshToken}}" - expiresPlaceholder = "{{.ExpiresAt}}" - scopesPlaceholder = "{{.Scopes}}" - // primaryKeyPlaceholder is the spec's omit marker, not a value to render: - // see substitutePlaceholders. - primaryKeyPlaceholder = "{{.PrimaryApiKey}}" - - // credentialFileMode is owner-only: the file holds sentinels, but the - // path is still a credential the agent alone should read. - credentialFileMode = "0600" -) - -// renderCredentialFile renders c's oauth credential file with sentinels for -// tokens and the stored scopes and expiry. -func renderCredentialFile(c spec.CredentialCapability, facts OAuthFacts) (string, error) { - cf := c.OAuth.CredentialFile - var sentinels spec.Sentinels - if c.OAuth.Sentinels != nil { - sentinels = *c.OAuth.Sentinels - } - expiresAt := int64(0) - if !facts.ExpiresAt.IsZero() { - // Both in-spec OAuth credential files (Claude Code's credentials.json - // and OpenCode's auth.json) carry the epoch in milliseconds. - expiresAt = facts.ExpiresAt.UnixMilli() - } - structure, err := substitutePlaceholders(cf.Structure, sentinels, facts.Scopes, expiresAt) - if err != nil { - return "", fmt.Errorf("credential %q credential file: %w", c.Service, err) - } - encoded, err := encodeCredentialFile(cf.Format, structure) - if err != nil { - return "", fmt.Errorf("credential %q credential file: %w", c.Service, err) - } - return encoded, nil -} - -// substitutePlaceholders replaces every leaf that references a placeholder -// with the typed value the target encoding expects, and refuses a placeholder -// archie does not render rather than leaving it standing in the file. -func substitutePlaceholders(v any, sentinels spec.Sentinels, scopes []string, expiresAt int64) (any, error) { - switch t := v.(type) { - case map[string]any: - out := make(map[string]any, len(t)) - for k, e := range t { - // No key is ever captured, so drop the enclosing key. - if s, ok := e.(string); ok && s == primaryKeyPlaceholder { - continue - } - sub, err := substitutePlaceholders(e, sentinels, scopes, expiresAt) - if err != nil { - return nil, fmt.Errorf("%s: %w", k, err) - } - out[k] = sub - } - return out, nil - case []any: - out := make([]any, len(t)) - for i, e := range t { - sub, err := substitutePlaceholders(e, sentinels, scopes, expiresAt) - if err != nil { - return nil, fmt.Errorf("[%d]: %w", i, err) - } - out[i] = sub - } - return out, nil - case string: - return substituteString(t, sentinels, scopes, expiresAt) - default: - return v, nil - } -} - -func substituteString(s string, sentinels spec.Sentinels, scopes []string, expiresAt int64) (any, error) { - switch s { - case accessPlaceholder: - return tokenSentinel(sentinels.AccessToken, "access-token") - case refreshPlaceholder: - return tokenSentinel(sentinels.RefreshToken, "refresh-token") - case expiresPlaceholder: - return expiresAt, nil - case scopesPlaceholder: - return grantedScopes(scopes) - } - out, err := spliceSentinels(s, sentinels) - if err != nil { - return nil, err - } - if err := valuePlaceholderInString(out); err != nil { - return nil, err - } - if at := firstPlaceholder(out); at != "" { - return nil, fmt.Errorf("placeholder %s is not rendered by archie", at) - } - return out, nil -} - -// tokenSentinel returns the sentinel standing in for a token, or an error when -// the Kit declared none: a token placeholder that cannot be hidden must not -// reach the file. -func tokenSentinel(sentinel, name string) (string, error) { - if sentinel == "" { - return "", fmt.Errorf("no %s sentinel is declared for this credential", name) - } - return sentinel, nil -} - -// grantedScopes renders {{.Scopes}} as the array the target encoding expects. -// The spec declares no omission case for it the way it does for -// {{.PrimaryApiKey}}, so an empty set refuses rather than writing an empty -// array, which would silently change the CLI's capability decisions. -func grantedScopes(scopes []string) ([]string, error) { - if len(scopes) == 0 { - return nil, fmt.Errorf("%s: no scopes were captured for this credential; re-run the setup terminal so the provider's granted scopes are recorded", scopesPlaceholder) - } - return scopes, nil -} - -// spliceSentinels replaces a token placeholder that rides inside a larger -// string, e.g. "Bearer {{.AccessToken}}". -func spliceSentinels(s string, sentinels spec.Sentinels) (string, error) { - for _, tok := range []struct{ placeholder, sentinel, name string }{ - {accessPlaceholder, sentinels.AccessToken, "access-token"}, - {refreshPlaceholder, sentinels.RefreshToken, "refresh-token"}, - } { - if !strings.Contains(s, tok.placeholder) { - continue - } - sentinel, err := tokenSentinel(tok.sentinel, tok.name) - if err != nil { - return "", err - } - s = strings.ReplaceAll(s, tok.placeholder, sentinel) - } - return s, nil -} - -// valuePlaceholderInString rejects a value placeholder left inside a larger -// string: a number or an array renders in the encoding's own type and cannot -// ride inside a string, and the primary key is omitted rather than rendered. -func valuePlaceholderInString(s string) error { - for _, whole := range []string{expiresPlaceholder, scopesPlaceholder} { - if strings.Contains(s, whole) { - return fmt.Errorf("%s must be a field's whole value", whole) - } - } - if strings.Contains(s, primaryKeyPlaceholder) { - return fmt.Errorf("%s is omitted, never rendered, so it cannot appear inside a larger string", primaryKeyPlaceholder) - } - return nil -} - -// firstPlaceholder reports the first {{...}} occurrence in s, or "". -func firstPlaceholder(s string) string { - start := strings.Index(s, "{{") - if start < 0 { - return "" - } - end := strings.Index(s[start:], "}}") - if end < 0 { - return s[start:] - } - return s[start : start+end+2] -} - -// encodeCredentialFile writes the substituted structure in the Kit's declared -// format: json unless it names toml. -func encodeCredentialFile(format string, structure any) (string, error) { - switch format { - case "", "json": - b, err := json.Marshal(structure) - if err != nil { - return "", err - } - return string(b), nil - case "toml": - var buf strings.Builder - if err := toml.NewEncoder(&buf).Encode(structure); err != nil { - return "", err - } - return buf.String(), nil - default: - return "", fmt.Errorf("unknown format %q", format) - } -} diff --git a/internal/infrastructure/kit/kit.go b/internal/infrastructure/kit/kit.go deleted file mode 100644 index a7c5f87c6..000000000 --- a/internal/infrastructure/kit/kit.go +++ /dev/null @@ -1,129 +0,0 @@ -// Package kit decides whether archie can run a Kit v3 harness and with what -// capabilities. -package kit - -import ( - "context" - "errors" - "fmt" - - "github.com/docker/sandbox-kit-spec/v3/fetch" - "github.com/docker/sandbox-kit-spec/v3/spec" -) - -const ( - typeCredential = spec.CapabilityCredential - typeAgentSessions = spec.CapabilityAgentSessions -) - -// supported is every capability type archie provides. Anything else is -// refused when required and skipped when optional: approximating a type -// archie does not implement would grant a Kit something nobody reviewed. -// Keys are the spec package's constants, not literals, so a dependency bump -// that renames a type fails to compile here instead of silently unclaiming -// it; TestSupportedCapabilitiesAreKnown guards the reverse direction. -var supported = map[string]bool{ - typeCredential: true, - spec.CapabilityNetworkPolicy: true, - spec.CapabilityVolume: true, - spec.CapabilityLifecycle: true, - typeAgentSessions: true, - spec.CapabilityAgentSkills: true, - spec.CapabilityAgentSkill: true, - spec.CapabilityAgentContext: true, - spec.CapabilityResources: true, -} - -// forgeServices are credential services a harness never receives: the model -// never runs git, so it never holds a credential that could push. -var forgeServices = map[string]bool{ - "github": true, - "gitlab": true, - "gitea": true, - "forgejo": true, - "bitbucket": true, -} - -// Plan is what archie launches: the composed descriptor, the capabilities -// it provides, and what it skipped. -type Plan struct { - Descriptor *spec.Descriptor - Capabilities []spec.Capability - Skipped []spec.SelectionRecord - Sessions *spec.AgentSessions - ContextSources []spec.ContextSource -} - -// SelectCapability applies Archie's runtime policy before composition. -func SelectCapability(ctx context.Context, _ spec.SelectionTarget, _ spec.Descriptor, c spec.Capability) spec.CapabilityDecision { - if err := ctx.Err(); err != nil { - return spec.CapabilityDecision{Message: err.Error()} - } - reason, err := refusalReason(c) - if err != nil { - return spec.CapabilityDecision{Message: err.Error()} - } - return spec.CapabilityDecision{Accepted: reason == "", Message: reason} -} - -// FromResolved reads the profile's selected Kit declarations: -// a workload Kit or a published Kit set, never a bare mixin. archie drives a -// harness headlessly, so it must carry an agent-sessions prompt verb. -func FromResolved(resolved *fetch.Resolved) (*Plan, error) { - if resolved.Descriptor.Kind != spec.KindWorkload { - return nil, errors.New("kit is a mixin: publish a Kit set composing it with a workload Kit, and name the set") - } - plan := &Plan{Descriptor: resolved.Descriptor, Capabilities: resolved.Descriptor.Capabilities} - for _, selection := range resolved.Selections { - plan.Skipped = append(plan.Skipped, selection.Selection.Skipped...) - } - for _, unit := range resolved.Kits { - contexts, err := spec.AgentContextsOf(unit.Descriptor.Capabilities) - if err != nil { - return nil, err - } - for _, ac := range contexts { - if ac.ContentFile != "" || ac.Content != "" { - plan.ContextSources = append(plan.ContextSources, spec.ContextSource{Reference: unit.Reference, Path: ac.ContentFile, Content: ac.Content}) - } - } - } - sessions, err := spec.AgentSessionsOf(plan.Capabilities) - if err != nil { - return nil, err - } - if sessions == nil || len(sessions.Prompt) == 0 { - return nil, fmt.Errorf("kit composition declares no %s prompt verb: archie cannot run it headlessly", typeAgentSessions) - } - plan.Sessions = sessions - return plan, nil -} - -// ValidateNeeds rejects a Plan without a resume verb when the gate may -// retry. -func ValidateNeeds(plan *Plan, gateRetries int) error { - if gateRetries > 0 && (plan.Sessions == nil || len(plan.Sessions.Resume) == 0) { - return fmt.Errorf("needs.gate_retries is %d, but the kit declares no %s resume verb to retry with", gateRetries, typeAgentSessions) - } - return nil -} - -func refusalReason(c spec.Capability) (string, error) { - if !supported[c.Type] { - return "not provided by archie", nil - } - if c.Type != typeCredential { - return "", nil - } - var cred spec.Credential - if err := spec.DecodeCapabilityConfig(c, &cred); err != nil { - return "", err - } - if forgeServices[cred.Service] { - return fmt.Sprintf("service %q is a forge credential; harnesses never hold one", cred.Service), nil - } - if cred.OAuth != nil && cred.OAuth.Passthrough { - return fmt.Sprintf("service %q asks for OAuth passthrough; real tokens never enter the container", cred.Service), nil - } - return "", nil -} diff --git a/internal/infrastructure/kit/kit_test.go b/internal/infrastructure/kit/kit_test.go deleted file mode 100644 index 6457423c7..000000000 --- a/internal/infrastructure/kit/kit_test.go +++ /dev/null @@ -1,227 +0,0 @@ -package kit - -import ( - "context" - "encoding/json" - "errors" - "net/http" - "net/http/httptest" - "slices" - "strconv" - "strings" - "testing" - - "github.com/docker/sandbox-kit-spec/v3/fetch" - "github.com/docker/sandbox-kit-spec/v3/spec" - "github.com/opencontainers/go-digest" - - "github.com/samcharles93/archie-core/internal/infrastructure/egress" -) - -func TestKitCapabilitySelection(t *testing.T) { - t.Parallel() - cases := []struct { - name string - optional bool - refused string - wantError bool - }{ - {"optional unknown group", true, "com.example/unavailable@1", false}, - {"required unknown group", false, "com.example/unavailable@1", true}, - {"optional forge group", true, spec.CapabilityCredential, false}, - {"required forge group", false, spec.CapabilityCredential, true}, - } - for _, tc := range cases { - t.Run(tc.name, func(t *testing.T) { - rejected := spec.Capability{Type: tc.refused} - if tc.refused == spec.CapabilityCredential { - rejected.Config = map[string]any{"service": "github", "phase": "runtime", "apiKey": map[string]any{"name": "GH_TOKEN"}} - } - value := "resolved-value" - descriptor := spec.Descriptor{SchemaVersion: spec.SchemaVersion, Kind: spec.KindWorkload, Args: map[string]spec.Arg{"setting": {Default: &value, Env: "SETTING"}}, Capabilities: []spec.Capability{ - {Type: spec.CapabilityAgentSessions, Config: map[string]any{"prompt": []string{"agent", "${{ kit.args.setting }}", "{{.Prompt}}"}}}, - {Type: spec.CapabilityAgentContext, Config: map[string]any{"filename": "AGENTS.md", "content": "retained guidance"}}, - {Group: &spec.CapabilityGroup{Optional: tc.optional, Capabilities: []spec.Capability{ - rejected, - {Type: spec.CapabilityLifecycle, Config: map[string]any{"startup": []any{map[string]any{"command": "touch /must-not-run"}}, "files": []any{map[string]any{"path": "/must-not-write", "content": "rejected"}}}}, - {Type: spec.CapabilityAgentContext, Config: map[string]any{"content": "must not appear"}}, - }}}, - }} - client, ref := kitRegistry(t, descriptor) - resolved, err := client.Resolve(t.Context(), []fetch.Request{{Reference: ref}}, fetch.WithCapabilitySelector(SelectCapability)) - if tc.wantError { - if err == nil || !strings.Contains(err.Error(), "required capability selection rejected") { - t.Fatalf("required group error=%v", err) - } - return - } - if err != nil { - t.Fatal(err) - } - plan, err := FromResolved(resolved) - if err != nil { - t.Fatal(err) - } - launch, err := Assemble(plan, ImageConfig{}, LaunchParams{}) - if err != nil { - t.Fatal(err) - } - if len(launch.Startup) != 0 || len(launch.Files) != 0 { - t.Fatalf("rejected group effects survived: %+v", launch) - } - if len(plan.ContextSources) != 1 || plan.ContextSources[0].Content != "retained guidance" { - t.Fatalf("context sources=%+v", plan.ContextSources) - } - if resolved.ContainerEnv["SETTING"] != value || plan.Sessions.Prompt[1] != value { - t.Fatalf("argument exports or prompt not resolved: %+v", resolved) - } - if len(plan.Skipped) != 1 { - t.Fatalf("skipped=%+v", plan.Skipped) - } - ctx, cancel := context.WithCancel(t.Context()) - cancel() - if _, err := client.Resolve(ctx, []fetch.Request{{Reference: ref}}, fetch.WithCapabilitySelector(SelectCapability)); !errors.Is(err, context.Canceled) { - t.Fatalf("canceled resolve=%v", err) - } - }) - } -} - -func kitRegistry(t *testing.T, descriptor spec.Descriptor) (*fetch.Client, string) { - t.Helper() - raw, err := json.Marshal(descriptor) - if err != nil { - t.Fatal(err) - } - manifest, err := json.Marshal(map[string]any{ - "schemaVersion": 2, "mediaType": "application/vnd.oci.image.manifest.v1+json", - "config": map[string]any{"mediaType": "application/vnd.oci.image.config.v1+json", "digest": digest.FromString("config"), "size": 2}, - "layers": []any{map[string]any{"mediaType": "application/vnd.oci.image.layer.v1.tar+gzip", "digest": digest.FromString("layer"), "size": 1}}, - "annotations": map[string]string{spec.AnnotationDescriptor: string(raw)}, - }) - if err != nil { - t.Fatal(err) - } - server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - if !strings.Contains(r.URL.Path, "/manifests/") { - http.NotFound(w, r) - return - } - w.Header().Set("Content-Type", "application/vnd.oci.image.manifest.v1+json") - w.Header().Set("Docker-Content-Digest", digest.FromBytes(manifest).String()) - w.Header().Set("Content-Length", strconv.Itoa(len(manifest))) - if r.Method != http.MethodHead { - _, _ = w.Write(manifest) - } - })) - t.Cleanup(server.Close) - client, err := fetch.New() - if err != nil { - t.Fatal(err) - } - return client, strings.TrimPrefix(server.URL, "http://") + "/kit:1.0.0" -} - -func TestKitContextDiscovery(t *testing.T) { - t.Parallel() - for _, tc := range []struct{ name, directory, want string }{ - {"legacy", "", "/archie/AGENTS.md"}, - {"explicit", "/home/agent/.codex", "/home/agent/.codex/AGENTS.md"}, - } { - t.Run(tc.name, func(t *testing.T) { - descriptor := spec.Descriptor{SchemaVersion: spec.SchemaVersion, Kind: spec.KindWorkload, Capabilities: []spec.Capability{ - {Type: spec.CapabilityAgentSessions, Config: map[string]any{"prompt": []string{"agent", "{{.Prompt}}"}}}, - {Type: spec.CapabilityAgentContext, Config: map[string]any{"filename": "AGENTS.md", "directory": tc.directory, "content": "guidance"}}, - }} - if tc.directory == "" { - delete(descriptor.Capabilities[1].Config, "directory") - } - client, ref := kitRegistry(t, descriptor) - resolved, err := client.Resolve(t.Context(), []fetch.Request{{Reference: ref}}, fetch.WithCapabilitySelector(SelectCapability)) - if err != nil { - t.Fatal(err) - } - plan, err := FromResolved(resolved) - if err != nil { - t.Fatal(err) - } - files, err := contextFiles(plan) - if err != nil { - t.Fatal(err) - } - if got := files[len(files)-1].Path; got != tc.want { - t.Fatalf("discovery path=%q, want %q", got, tc.want) - } - }) - } -} - -func TestKitCredentialExposureByPhase(t *testing.T) { - t.Parallel() - for _, tc := range []struct { - name string - phases spec.Phases - install, runtime bool - }{ - {"install", spec.Phases{"install"}, true, false}, - {"runtime", spec.Phases{"runtime"}, false, true}, - {"both", spec.Phases{"install", "runtime"}, true, true}, - } { - t.Run(tc.name, func(t *testing.T) { - credential := spec.Credential{Service: "provider", Phase: tc.phases, APIKey: &spec.APIKey{Name: "PROVIDER_KEY"}} - entry, err := spec.CapabilityWithConfig(spec.Capability{Type: spec.CapabilityCredential}, credential) - if err != nil { - t.Fatal(err) - } - plan := &Plan{Capabilities: []spec.Capability{*entry, {Type: spec.CapabilityLifecycle, Config: map[string]any{ - "install": []any{map[string]any{"command": "true", "env": []string{"PROVIDER_KEY"}}}, - "startup": []any{map[string]any{"command": "true", "env": []string{"PROVIDER_KEY"}}}, - }}}} - launch, err := Assemble(plan, ImageConfig{}, LaunchParams{}) - if err != nil { - t.Fatal(err) - } - for _, check := range []struct { - name string - env []string - want bool - }{ - {"install", launch.Install[0].Env, tc.install}, {"startup", launch.Startup[0].Env, tc.runtime}, {"harness", launch.Harness.Env, tc.runtime}, - } { - if got := slices.Contains(check.env, "PROVIDER_KEY="+egress.Sentinel); got != check.want { - t.Errorf("%s sentinel exposed=%v, want %v", check.name, got, check.want) - } - } - credential.APIKey = nil - credential.OAuth = &spec.OAuth{Sentinels: &spec.Sentinels{AccessToken: "access-sentinel"}, CredentialFile: &spec.CredentialFile{Path: "/tmp/provider.json", Structure: map[string]any{"token": "{{.AccessToken}}"}}} - entry, err = spec.CapabilityWithConfig(spec.Capability{Type: spec.CapabilityCredential}, credential) - if err != nil { - t.Fatal(err) - } - plan.Capabilities[0] = *entry - launch, err = Assemble(plan, ImageConfig{}, LaunchParams{Bound: map[string]egress.CredentialKind{"provider": egress.CredentialOAuth}}) - if err != nil { - t.Fatal(err) - } - if got := len(launch.InstallFiles) > 0; got != tc.install { - t.Errorf("install file present=%v, want %v", got, tc.install) - } - if got := len(launch.Files) > 0; got != tc.runtime { - t.Errorf("runtime file present=%v, want %v", got, tc.runtime) - } - }) - } -} - -func TestSupportedCapabilitiesAreKnown(t *testing.T) { - t.Parallel() - known := make(map[string]bool) - for _, typ := range spec.KnownCapabilities() { - known[typ] = true - } - for typ := range supported { - if !known[typ] { - t.Errorf("supported capability type %q is not known to the sandbox-kit-spec version in go.mod; the claim is stale", typ) - } - } -} diff --git a/internal/infrastructure/kit/launch.go b/internal/infrastructure/kit/launch.go deleted file mode 100644 index dc8a44d44..000000000 --- a/internal/infrastructure/kit/launch.go +++ /dev/null @@ -1,315 +0,0 @@ -package kit - -import ( - "crypto/sha256" - "encoding/hex" - "fmt" - "path" - "regexp" - "slices" - "strings" - "time" - - "github.com/docker/sandbox-kit-spec/v3/spec" - - "github.com/samcharles93/archie-core/internal/domain/agentrun" - "github.com/samcharles93/archie-core/internal/infrastructure/egress" -) - -const ( - // DefaultHarnessUser is the Kit agent user: the harness runs as it when - // the image names no user, or names root. - DefaultHarnessUser = "1000" - // WorkspaceDir is where a Kit container mounts the task worktree. Its - // parent holds the agent-context profile: beside the worktree, not in - // it, and below the filesystem root, which some CLIs stop short of when - // they look upward for instruction files. - WorkspaceDir = "/archie/workspace" - - installHookUser = "0" -) - -// hookBaseline is the platform baseline a hook sees besides its declared -// variables, taken from the image's environment, never the host's. -var hookBaseline = []string{"PATH", "HOME", "HOSTNAME", "TERM"} - -var unsafeVolumeChars = regexp.MustCompile(`[^a-zA-Z0-9_.-]`) - -// ImageConfig is the workload image's runtime contract. -type ImageConfig struct { - Entrypoint []string - Cmd []string - Env []string - User string -} - -// OAuthFacts is the non-secret part of a bound OAuth credential's stored -// token set that a credential file renders: the scopes the provider granted -// and when the access token expires. The tokens themselves never cross into -// kit, so a real token cannot be written to the file. -type OAuthFacts struct { - Scopes []string - ExpiresAt time.Time -} - -// LaunchParams are the run-specific inputs to a Kit container. -type LaunchParams struct { - // Instance prefixes the Kit's volume names, so instances sharing a - // Docker host never mount each other's volumes. - Instance string - // Execution keys the Kit's volumes, so they survive retries of one - // WorkflowExecution and are never shared with another. - Execution string - ProxyToken string - CAPath string - // Bound maps each credential service the run carries to its kind. Values - // never leave the host. - Bound map[string]egress.CredentialKind - // OAuth is the non-secret facts of each bound OAuth credential's stored - // token set, keyed by service, that a Kit's credentialFile renders: the - // granted scopes and the expiry. The tokens themselves never cross into - // kit, so a real token cannot be written. - OAuth map[string]OAuthFacts -} - -// Hook is one lifecycle hook, ready to exec in the container. -type Hook struct { - Argv []string - User string - Env []string - Background bool -} - -// Volume is one volume@1 path backed by a named volume. -type Volume struct { - Name string - Path string -} - -// Launch is everything archie needs to run a composed Kit for one run. -type Launch struct { - Harness agentrun.HarnessSpec - Install []Hook - Startup []Hook - Files []spec.File - InstallFiles []spec.File - // Context is the agent-context profile and the inline bodies it points - // at, written as root so a run can read but not change them. - Context []spec.File - Skills []Skill - Volumes []Volume -} - -// Skill exposes an image-bundled directory at an agent's discovery path. -type Skill struct { - Source string - Target string -} - -// Assemble derives a Kit container's launch from its admitted plan and the -// workload image's config. The harness never runs as root, and no hook sees -// a variable it did not declare beyond the image-derived baseline and the -// egress settings every sandbox process needs. -func Assemble(p *Plan, img ImageConfig, params LaunchParams) (Launch, error) { - installCreds, err := spec.CredentialsOfPhase(p.Capabilities, "install") - if err != nil { - return Launch{}, err - } - runtimeCreds, err := spec.CredentialsOfPhase(p.Capabilities, "runtime") - if err != nil { - return Launch{}, err - } - lifecycle, err := spec.LifecycleOf(p.Capabilities) - if err != nil { - return Launch{}, err - } - volumes, err := spec.VolumesOf(p.Capabilities) - if err != nil { - return Launch{}, err - } - - proxyEnv := egress.ProxyEnv(params.ProxyToken, params.CAPath) - installVars := credentialVars(installCreds, params.Bound) - runtimeVars := credentialVars(runtimeCreds, params.Bound) - - l := Launch{Harness: agentrun.HarnessSpec{ - User: harnessUser(img.User), - Launch: slices.Concat(img.Entrypoint, img.Cmd), - Env: slices.Concat(img.Env, []string{"WORKSPACE_DIR=" + WorkspaceDir}, proxyEnv, egress.SentinelEnv(runtimeCreds)), - }} - if s := p.Sessions; s != nil { - l.Harness.Prompt, l.Harness.Resume, l.Harness.Continue = s.Prompt, s.Resume, s.Continue - } - - baseline := slices.Concat(pick(img.Env, hookBaseline), proxyEnv) - if lifecycle != nil { - for _, h := range lifecycle.Install { - l.Install = append(l.Install, Hook{ - Argv: slices.Clone(h.Command), User: orDefault(h.User, installHookUser), - Env: slices.Concat(baseline, declared(h.Env, installVars)), - }) - } - for _, h := range lifecycle.Startup { - l.Startup = append(l.Startup, Hook{ - Argv: slices.Clone(h.Command), User: orDefault(h.User, DefaultHarnessUser), - Env: slices.Concat(baseline, declared(h.Env, runtimeVars)), Background: h.Background, - }) - } - l.Files = lifecycle.Files - } - l.InstallFiles, err = credentialFiles(installCreds, params.Bound, params.OAuth) - if err != nil { - return Launch{}, err - } - credential, err := credentialFiles(runtimeCreds, params.Bound, params.OAuth) - if err != nil { - return Launch{}, err - } - l.Files = append(l.Files, credential...) - for _, v := range volumes { - l.Volumes = append(l.Volumes, Volume{Name: volumeName(params.Instance, params.Execution, v.Path), Path: v.Path}) - } - if l.Context, err = contextFiles(p); err != nil { - return Launch{}, err - } - l.Skills, err = bundledSkills(p.Capabilities) - if err != nil { - return Launch{}, err - } - return l, nil -} - -func bundledSkills(capabilities []spec.Capability) ([]Skill, error) { - bundles, err := spec.AgentSkillRequestsOf(capabilities) - if err != nil { - return nil, err - } - discovery, err := spec.AgentSkillsOf(capabilities) - if err != nil { - return nil, err - } - var skills []Skill - for _, destination := range discovery { - for _, bundle := range bundles { - skills = append(skills, Skill{Source: bundle.Path, Target: path.Join(destination.Path, spec.AgentSkillName(bundle.AgentSkill))}) - } - } - return skills, nil -} - -func credentialVars(creds []spec.CredentialCapability, bound map[string]egress.CredentialKind) map[string]string { - vars := map[string]string{"WORKSPACE_DIR": WorkspaceDir} - for _, c := range creds { - vars[credentialModeVar(c.Service)] = credentialMode(bound[c.Service]) - } - for _, entry := range egress.SentinelEnv(creds) { - name, value, _ := strings.Cut(entry, "=") - vars[name] = value - } - return vars -} - -// credentialFiles renders the credential file for each OAuth-bound -// credential that declares one. -func credentialFiles(creds []spec.CredentialCapability, bound map[string]egress.CredentialKind, oauth map[string]OAuthFacts) ([]spec.File, error) { - var files []spec.File - for _, c := range creds { - if c.OAuth == nil || c.OAuth.CredentialFile == nil || bound[c.Service] != egress.CredentialOAuth { - continue - } - content, err := renderCredentialFile(c, oauth[c.Service]) - if err != nil { - return nil, err - } - files = append(files, spec.File{Path: c.OAuth.CredentialFile.Path, Content: content, Mode: credentialFileMode}) - } - return files, nil -} - -// contextFiles writes agent-context@1: a profile file beside the workspace -// indexing each Kit's body. -func contextFiles(p *Plan) ([]spec.File, error) { - ac, err := spec.AgentContextOf(p.Capabilities) - if err != nil || ac == nil || ac.Filename == "" { - return nil, err - } - root := ac.Directory - if root == "" { - root = path.Dir(WorkspaceDir) - } - var files []spec.File - var index strings.Builder - index.WriteString("# Archie\n\nArchie runs this environment. Each task's rules are in its prompt.\n") - for i, src := range p.ContextSources { - if i == 0 { - index.WriteString("\n## Kits\n\nRead a Kit's guidance when its tools are relevant.\n\n") - } - at := src.Path - if at == "" { - at = path.Join(root, "archie-kit-context", fmt.Sprintf("%d.md", i+1)) - files = append(files, spec.File{Path: at, Content: src.Content, Mode: "0644"}) - } - fmt.Fprintf(&index, "- `%s`: %s\n", src.Reference, at) - } - return append(files, spec.File{Path: path.Join(root, path.Base(ac.Filename)), Content: index.String(), Mode: "0644"}), nil -} - -func harnessUser(imageUser string) string { - name, _, _ := strings.Cut(imageUser, ":") - if name == "" || name == "root" || name == "0" { - return DefaultHarnessUser - } - return imageUser -} - -func credentialModeVar(service string) string { - return "SBX_CRED_" + strings.ToUpper(strings.ReplaceAll(service, "-", "_")) + "_MODE" -} - -// credentialModeNone is the mode variable's value for a service this run does -// not carry at all. -const credentialModeNone = "none" - -// credentialMode is one service's SBX_CRED_*_MODE value: how this run carries -// the credential, or none when it carries none at all. -func credentialMode(kind egress.CredentialKind) string { - if kind == "" { - return credentialModeNone - } - return string(kind) -} - -// declared resolves a hook's declared variable names against the runtime -// variables archie provides; a name archie does not provide stays unset. -func declared(names []string, vars map[string]string) []string { - var env []string - for _, name := range names { - if v, ok := vars[name]; ok { - env = append(env, name+"="+v) - } - } - return env -} - -// pick returns the entries of env whose names are listed. -func pick(env, names []string) []string { - var out []string - for _, e := range env { - if name, _, _ := strings.Cut(e, "="); slices.Contains(names, name) { - out = append(out, e) - } - } - return out -} - -func orDefault(v, fallback string) string { - if v == "" { - return fallback - } - return v -} - -func volumeName(instance, execution, path string) string { - sum := sha256.Sum256([]byte(path)) - return instance + "-kit-" + unsafeVolumeChars.ReplaceAllString(execution, "_") + "-" + hex.EncodeToString(sum[:6]) -} diff --git a/internal/infrastructure/kitrun/launcher.go b/internal/infrastructure/kitrun/launcher.go deleted file mode 100644 index 9360061fe..000000000 --- a/internal/infrastructure/kitrun/launcher.go +++ /dev/null @@ -1,527 +0,0 @@ -// Package kitrun starts a Kit task: it composes the profile's Kits, opens -// the task's egress session and sandbox network, and starts the container -// with archie's worker as root and the harness spec the worker runs stages on. -package kitrun - -import ( - "context" - "crypto/rand" - "encoding/hex" - "errors" - "fmt" - "maps" - "os" - "path" - "path/filepath" - "slices" - "strings" - "sync" - - "github.com/docker/sandbox-kit-spec/v3/fetch" - "github.com/docker/sandbox-kit-spec/v3/spec" - - "github.com/samcharles93/archie-core/internal/agentexec" - "github.com/samcharles93/archie-core/internal/config" - "github.com/samcharles93/archie-core/internal/container" - "github.com/samcharles93/archie-core/internal/domain/access" - "github.com/samcharles93/archie-core/internal/domain/agentrun" - "github.com/samcharles93/archie-core/internal/domain/harnesssecret" - "github.com/samcharles93/archie-core/internal/infrastructure/egress" - "github.com/samcharles93/archie-core/internal/infrastructure/kit" - "github.com/samcharles93/archie-core/internal/skill" -) - -// SecretResolver resolves a credential binding's secret value. *secret.Registry -// satisfies it; the interface stays narrow so kitrun does not need the whole -// secret engine surface, and a test can fake it with no engine at all. -type SecretResolver interface { - Resolve(config.SecretRef) (string, error) -} - -// OAuthSecrets reads an org's stored OAuth token set for a bound service, so a -// Kit's credential file can render that set's expiry. It is the store's read -// half only: kitrun never writes a token set, and nothing but the expiry -// leaves oauthExpiries, so a real token cannot reach the credential file -type OAuthSecrets interface { - GetHarnessSecret(ctx context.Context, org, service string) (harnesssecret.Secret, error) -} - -const ( - agentPath = "/opt/archie/archie-agent" - caPath = "/opt/archie/ca.pem" -) - -// GrantRegistry records a live session's granted credential services, so the -// egress proxy can resolve a session token that names no task. It is optional: -// a Launcher without one serves task runs only. -type GrantRegistry interface { - Grant(token, org string, services []string) - Revoke(token string) -} - -// Launcher starts Kit task containers. -type Launcher struct { - Pool *container.Pool - Fetch *fetch.Client - Proxy *egress.Proxy - Networks *egress.Networks - // Grants records a setup session's granted services while it is open. - // Nil refuses a setup session rather than opening one whose token the - // egress proxy cannot resolve. - Grants GrantRegistry - // AgentBinary and CAFile are host paths mounted read-only into every - // Kit container: the worker's own binary and the egress CA. - AgentBinary string - CAFile string - // Config and Secrets decide, at launch, which declared credentials the run - // is bound for and of which kind. The values themselves are resolved by - // the proxy's Resolver per request. Nil Secrets leaves every API-key - // credential unbound. - Config *config.Holder - Secrets SecretResolver - // OAuth reads the stored token set whose expiry a Kit's credential file - // renders. A nil OAuth refuses the launch of a Kit that renders one, the - // same way a nil OAuthStore makes the proxy refuse a required OAuth - // credential. - OAuth OAuthSecrets - - mu sync.Mutex - started bool -} - -// Request is one Kit task to start. -type Request struct { - // TaskID keys reusable volumes; Attempt names the container and network. - TaskID int64 - Attempt int - // Kit is the digest-pinned workload Kit or published Kit set. - Kit string - Adapter string - WorkDir string - WorkerEnv []string - // GateRetries is the workflow's gate-retry budget. Launch refuses a positive - // budget without a resume verb. - GateRetries int - // Org and GrantedServices are the dispatching identity's own facts. A Kit - // credential resolves only where these agree with a configured - // CredentialBinding and the Kit's declared service; neither side widens the - // other. - Org string - GrantedServices []string - // RunCredential is the worker grant, retained only on the host and in the - // worker environment. The harness gets a separate proxy password. - RunCredential string - // Record is set for a test run: every delivery its container attempts is - // recorded on the task's timeline and performed none of. - Record *egress.RecordOptions -} - -// Run is a started Kit task. -type Run struct { - Container *container.Container - Harness agentrun.HarnessSpec - // Volumes outlive the run; remove them with RemoveVolumes once the - // execution has ended. - Volumes []kit.Volume - network string - token string - execution string - grants GrantRegistry - // ephemeral marks a setup session, whose volumes are never reused by a - // later run and so are removed with it. - ephemeral bool -} - -// SetupRequest is one ephemeral setup session to start: a Kit-profile -// container whose only job is to run a CLI's own login so the proxy captures -// the tokens at the token endpoint. -type SetupRequest struct { - // Session keys the container, network and volumes, and roots its - // container name. - Session string - // Kit is the profile's digest-pinned Kit or published Kit set. - Kit string - // Org is the org whose credential bindings the session may use. A setup - // session carries no task and no identity grant, so its bound services - // are the org's bindings for the Kit's declared services. - Org string -} - -// LaunchSetup starts an ephemeral setup session's container. Unlike Launch it -// mounts no worktree and runs no worker: the container holds a keepalive and -// the operator's PTY runs the CLI's login inside it. The launch runs the -// Kit's install hooks at the install phase and then enters runtime, exactly -// as a task run does, so a runtime-phase credential's token endpoint is the -// one the proxy intercepts. -func (l *Launcher) LaunchSetup(ctx context.Context, req SetupRequest) (*Run, error) { - if req.Kit == "" { - return nil, errors.New("kit profile names no kit") - } - if l.Grants == nil || l.Config == nil { - return nil, errors.New("setup sessions need a session grant registry and a configuration holder") - } - if err := l.startEgress(ctx); err != nil { - return nil, err - } - k, err := l.read(ctx, req.Kit, 0) - if err != nil { - return nil, err - } - bound, services := orgCredentialKinds(l.Config.Get().Containers.Credentials, req.Org, k.creds) - token, err := newProxyToken() - if err != nil { - return nil, err - } - principal, _ := access.PrincipalFromContext(ctx) - session, err := l.Proxy.Register(egress.SessionOptions{Principal: principal, Token: token, Org: req.Org, Network: k.network, Credentials: k.creds, Bound: bound}) - if err != nil { - return nil, err - } - run := &Run{network: l.Pool.Instance() + "-kit-" + req.Session, token: session.Token(), execution: req.Session, grants: l.Grants, ephemeral: true} - // Bound is deliberately empty here: a credential file renders the stored - // scopes and expiry, and there is no stored token set yet -- that is the - // state this session exists to change. The Kit's sentinels still reach the - // harness environment. - launch, err := kit.Assemble(k.plan, k.img, kit.LaunchParams{Instance: l.Pool.Instance(), Execution: req.Session, ProxyToken: session.Token(), CAPath: caPath}) - if err != nil { - l.release(ctx, run) - return nil, err - } - run.Harness, run.Volumes = launch.Harness, launch.Volumes - l.Grants.Grant(session.Token(), req.Org, services) - if err := l.Networks.Create(ctx, run.network); err != nil { - l.release(ctx, run) - return nil, err - } - // The container runs archie's own agent as a keepalive rather than the - // image's entrypoint (the harness CLI, which would exit) and rather than an - // image binary such as sleep, which a Kit image need not provide. The - // environment stays empty (WorkerEnv nil): the harness env is passed - // explicitly on the exec, and no worker credential reaches it. - run.Container, err = l.Pool.AcquireKit(ctx, container.KitSpec{ - Name: run.network, - Image: req.Kit, - Network: run.network, - Worker: []string{agentPath, "hold"}, - Binds: append([]string{ - l.AgentBinary + ":" + agentPath + ":ro", - l.CAFile + ":" + caPath + ":ro", - }, skillsBinds(k.skills, l.skillsDir())...), - Launch: launch, - InstallDone: session.EnterRuntime, - }) - if err != nil { - l.release(ctx, run) - return nil, errors.Join(err, l.Networks.Remove(context.WithoutCancel(ctx), run.network)) - } - return run, nil -} - -// orgCredentialKinds is the kind of each of the Kit's declared services the -// org binds, with no identity grant in the intersection: a setup session acts -// on the org's secret, not on a run's grant. -func orgCredentialKinds(bindings []config.CredentialBinding, org string, creds []spec.CredentialCapability) (map[string]egress.CredentialKind, []string) { - declared := make([]string, len(creds)) - for i, c := range creds { - declared[i] = c.Service - } - bound := config.ContainerConfig{Credentials: bindings}.BoundCredentials(org, declared, declared) - kinds := make(map[string]egress.CredentialKind, len(bound)) - services := make([]string, 0, len(bound)) - for service, b := range bound { - if b.Secret == (config.SecretRef{}) { - kinds[service] = egress.CredentialOAuth - } else { - kinds[service] = egress.CredentialAPIKey - } - services = append(services, service) - } - return kinds, services -} - -// newProxyToken mints a container proxy password, separate from worker grants. -func newProxyToken() (string, error) { - var b [32]byte - if _, err := rand.Read(b[:]); err != nil { - return "", fmt.Errorf("mint session credential: %w", err) - } - return hex.EncodeToString(b[:]), nil -} - -// TaskExecution is the execution key of a task's Kit run, and TaskContainer the -// name its container carries. The run-scoped live view resolves a task to its -// container by this name. -func TaskExecution(taskID int64) string { return fmt.Sprintf("task-%d", taskID) } - -// TaskContainer is the container name of a task's Kit run. -func TaskContainer(instance string, taskID, attempt int64) string { - return fmt.Sprintf("%s-kit-%s-attempt-%d", instance, TaskExecution(taskID), attempt) -} - -// Launch reads the request's Kit and starts its container. -func (l *Launcher) Launch(ctx context.Context, req Request) (*Run, error) { - execution := TaskExecution(req.TaskID) - containerName := TaskContainer(l.Pool.Instance(), req.TaskID, int64(req.Attempt)) - if req.Kit == "" { - return nil, errors.New("kit profile names no kit") - } - adapter, ok := agentexec.LookupHarnessAdapter(req.Adapter) - if req.Adapter != "" && !ok { - return nil, fmt.Errorf("harness output adapter %q is unknown", req.Adapter) - } - if err := l.startEgress(ctx); err != nil { - return nil, err - } - k, err := l.read(ctx, req.Kit, req.GateRetries) - if err != nil { - return nil, err - } - kinds := l.boundKinds(req, k.creds) - facts, err := l.oauthFacts(harnesssecret.WithRunCredential(ctx, req.RunCredential), req.Org, k.creds, kinds) - if err != nil { - return nil, err - } - session, err := l.taskSession(req, k, kinds) - if err != nil { - return nil, err - } - run := &Run{network: containerName, token: session.Token(), execution: execution} - launch, err := kit.Assemble(k.plan, k.img, kit.LaunchParams{Instance: l.Pool.Instance(), Execution: execution, ProxyToken: session.Token(), CAPath: caPath, Bound: kinds, OAuth: facts}) - if err != nil { - l.release(ctx, run) - return nil, err - } - launch.Harness.Adapter, launch.Harness.MCPConfig = req.Adapter, adapter.MCPConfig - launch.Harness.CentralTools = len(adapter.MCPConfig) > 0 - run.Harness, run.Volumes = launch.Harness, launch.Volumes - if err := l.Networks.Create(ctx, run.network); err != nil { - l.release(ctx, run) - return nil, err - } - run.Container, err = l.Pool.AcquireKit(ctx, container.KitSpec{ - Name: containerName, - Image: req.Kit, - Network: run.network, - Worker: []string{agentPath}, - WorkerEnv: req.WorkerEnv, - Binds: append([]string{ - l.AgentBinary + ":" + agentPath + ":ro", - l.CAFile + ":" + caPath + ":ro", - req.WorkDir + ":" + kit.WorkspaceDir, - }, skillsBinds(k.skills, l.skillsDir())...), - Launch: launch, - InstallDone: session.EnterRuntime, - }) - if err != nil { - l.release(ctx, run) - return nil, errors.Join(err, l.Networks.Remove(context.WithoutCancel(ctx), run.network)) - } - return run, nil -} - -// boundKinds is the kind of each declared credential the run is bound for: -// declared by the Kit, granted to the run's identity, bound in its org, and -// for an API key, resolvable now. The proxy resolves the value itself per -// request, through the same intersection. -func (l *Launcher) boundKinds(req Request, creds []spec.CredentialCapability) map[string]egress.CredentialKind { - kinds := map[string]egress.CredentialKind{} - if l.Config == nil { - return kinds - } - current := l.Config.Get().Containers.Credentials - if len(current) == 0 { - return kinds - } - declared := make([]string, len(creds)) - oauth := map[string]bool{} - for i, c := range creds { - declared[i] = c.Service - oauth[c.Service] = egress.IsOAuthManaged(c) - } - bindings := config.ContainerConfig{Credentials: current}.BoundCredentials(req.Org, req.GrantedServices, declared) - for service, binding := range bindings { - switch { - case binding.Secret == (config.SecretRef{}): - // No secret: only OAuth gives the service a value. - if oauth[service] { - kinds[service] = egress.CredentialOAuth - } - case l.Secrets != nil: - if _, err := l.Secrets.Resolve(binding.Secret); err == nil { - kinds[service] = egress.CredentialAPIKey - } - } - } - return kinds -} - -// oauthFacts reads the stored scopes and expiry for each OAuth-bound -// credential with a credential file. -func (l *Launcher) oauthFacts(ctx context.Context, org string, creds []spec.CredentialCapability, kinds map[string]egress.CredentialKind) (map[string]kit.OAuthFacts, error) { - facts := map[string]kit.OAuthFacts{} - for _, c := range creds { - if c.OAuth == nil || c.OAuth.CredentialFile == nil || kinds[c.Service] != egress.CredentialOAuth { - continue - } - if l.OAuth == nil { - return nil, fmt.Errorf("credential %q renders a credential file, but no harness secret store is configured", c.Service) - } - secret, err := l.OAuth.GetHarnessSecret(ctx, org, c.Service) - if err != nil { - return nil, fmt.Errorf("credential %q renders a credential file and has no captured OAuth token (run the setup terminal first): %w", c.Service, err) - } - facts[c.Service] = kit.OAuthFacts{Scopes: secret.Scopes, ExpiresAt: secret.ExpiresAt} - } - return facts, nil -} - -// release undoes what Launch already did for run: its proxy session. Call sites past this point pass the network/container errors -// through their own cleanup; release only ever needs to run once per Launch -// failure, so it takes no error to join. -func (l *Launcher) release(ctx context.Context, run *Run) { - l.Proxy.Revoke(ctx, run.token) - if run.grants != nil { - run.grants.Revoke(run.token) - } -} - -// kitNeeds is what a profile's Kit asks of its run, read from its admitted -// plan. -type kitNeeds struct { - plan *kit.Plan - img kit.ImageConfig - network *spec.PhasedNetwork - creds []spec.CredentialCapability - skills []spec.AgentSkillsCapability -} - -// read fetches and admits the profile's Kit, checks it can meet the -// workflow's gate retries, and decodes what it asks of the run. -func (l *Launcher) read(ctx context.Context, ref string, gateRetries int) (kitNeeds, error) { - img, err := l.imageConfig(ctx, ref) - if err != nil { - return kitNeeds{}, err - } - environment := make(map[string]string) - for _, entry := range img.Env { - name, value, _ := strings.Cut(entry, "=") - environment[name] = value - } - resolved, err := l.Fetch.Resolve(ctx, []fetch.Request{{Reference: ref}}, fetch.WithCapabilitySelector(kit.SelectCapability), fetch.WithEnvironment(environment, nil)) - if err != nil { - return kitNeeds{}, fmt.Errorf("read kit: %w", err) - } - k := kitNeeds{img: img} - if k.plan, err = kit.FromResolved(resolved); err != nil { - return kitNeeds{}, err - } - if err := kit.ValidateNeeds(k.plan, gateRetries); err != nil { - return kitNeeds{}, err - } - for _, name := range slices.Sorted(maps.Keys(resolved.ContainerEnv)) { - k.img.Env = append(k.img.Env, name+"="+resolved.ContainerEnv[name]) - } - if k.network, err = spec.NetworkPolicyOf(k.plan.Capabilities); err != nil { - return kitNeeds{}, err - } - if k.creds, err = spec.CredentialsOf(k.plan.Capabilities); err != nil { - return kitNeeds{}, err - } - if k.skills, err = spec.AgentSkillsOf(k.plan.Capabilities); err != nil { - return kitNeeds{}, err - } - return k, nil -} - -// skillsDir is the operator's live skills_dir, or "" with no config. -func (l *Launcher) skillsDir() string { - if l.Config == nil { - return "" - } - return l.Config.Get().SkillsDir -} - -// Release stops the run's container, then removes its network, revokes its -// credential grant and ends its egress session. A setup session's volumes are -// removed too: they are keyed to one ephemeral session and no later run reuses -// them. A task run keeps its volumes for the life of its execution. -func (l *Launcher) Release(ctx context.Context, run *Run) error { - l.Pool.Release(ctx, run.Container) - l.release(ctx, run) - err := l.Networks.Remove(context.WithoutCancel(ctx), run.network) - if run.ephemeral { - err = errors.Join(err, l.RemoveVolumes(context.WithoutCancel(ctx), run.Volumes)) - } - return err -} - -// RemoveVolumes removes an ended execution's Kit volumes. -func (l *Launcher) RemoveVolumes(ctx context.Context, volumes []kit.Volume) error { - return container.RemoveKitVolumes(ctx, l.Pool.Client(), volumes) -} - -// startEgress starts the relay on first use, so a daemon with no Kit -// profile runs no relay. -func (l *Launcher) startEgress(ctx context.Context) error { - l.mu.Lock() - defer l.mu.Unlock() - if l.started { - return nil - } - if err := l.Networks.Start(ctx); err != nil { - return err - } - l.started = true - return nil -} - -func (l *Launcher) imageConfig(ctx context.Context, ref string) (kit.ImageConfig, error) { - if err := l.Pool.EnsureImage(ctx, ref); err != nil { - return kit.ImageConfig{}, err - } - inspected, err := l.Pool.Client().ImageInspect(ctx, ref) - if err != nil { - return kit.ImageConfig{}, fmt.Errorf("inspect kit image %s: %w", ref, err) - } - cfg := inspected.Config - if cfg == nil { - return kit.ImageConfig{}, fmt.Errorf("kit image %s has no config", ref) - } - return kit.ImageConfig{ - Entrypoint: slices.Clone(cfg.Entrypoint), Cmd: slices.Clone(cfg.Cmd), - Env: slices.Clone(cfg.Env), User: cfg.User, - }, nil -} - -// Mount skills individually to preserve bundled discovery. The shared SELinux -// label lets separate run containers read the same read-only store. -func skillsBinds(asks []spec.AgentSkillsCapability, sharedDir string) []string { - if sharedDir == "" { - return nil - } - var binds []string - store := skill.StoreDir(sharedDir) - entries, err := os.ReadDir(store) - if err != nil { - return nil - } - for _, a := range asks { - for _, entry := range entries { - if entry.IsDir() { - binds = append(binds, filepath.Join(store, entry.Name())+":"+path.Join(a.Path, entry.Name())+":ro,z") - } - } - } - return binds -} - -func (l *Launcher) taskSession(req Request, k kitNeeds, kinds map[string]egress.CredentialKind) (*egress.Session, error) { - if req.RunCredential == "" { - return nil, errors.New("kit tasks require a run credential") - } - token, err := newProxyToken() - if err != nil { - return nil, err - } - return l.Proxy.Register(egress.SessionOptions{RunCredential: req.RunCredential, Token: token, Org: req.Org, Network: k.network, Credentials: k.creds, Bound: kinds, Record: req.Record}) -} diff --git a/internal/infrastructure/kitrun/launcher_test.go b/internal/infrastructure/kitrun/launcher_test.go deleted file mode 100644 index ae5eafad2..000000000 --- a/internal/infrastructure/kitrun/launcher_test.go +++ /dev/null @@ -1,131 +0,0 @@ -package kitrun - -import ( - "os" - "path/filepath" - "strings" - "testing" - - "github.com/docker/sandbox-kit-spec/v3/spec" - - "github.com/samcharles93/archie-core/internal/config" - "github.com/samcharles93/archie-core/internal/infrastructure/egress" - "github.com/samcharles93/archie-core/internal/infrastructure/kit" - "github.com/samcharles93/archie-core/internal/skill" -) - -// A setup session has no task and no identity grant, so it must bind only the -// org's own bindings for the Kit's declared services. Widening that -// intersection would let one org's terminal carry another org's credential, -// or a service the Kit never declared. -func TestOrgCredentialKinds(t *testing.T) { - t.Parallel() - - bindings := []config.CredentialBinding{ - {Service: "github", Org: "acme", Secret: config.SecretRef{Engine: "builtin", Key: "gh"}}, - {Service: "linear", Org: "acme"}, - {Service: "github", Org: "other", Secret: config.SecretRef{Engine: "builtin", Key: "gh"}}, - } - creds := []spec.CredentialCapability{{Service: "github"}, {Service: "linear"}, {Service: "unbound"}} - - cases := []struct { - name string - org string - want map[string]egress.CredentialKind - services []string - }{ - { - name: "binds the org's own declared services", - org: "acme", - want: map[string]egress.CredentialKind{ - "github": egress.CredentialAPIKey, - "linear": egress.CredentialOAuth, - }, - services: []string{"github", "linear"}, - }, - { - name: "another org's binding never resolves", - org: "other", - want: map[string]egress.CredentialKind{"github": egress.CredentialAPIKey}, - services: []string{ - "github", - }, - }, - { - name: "unknown org binds nothing", - org: "nobody", - want: map[string]egress.CredentialKind{}, - services: []string{}, - }, - } - - for _, tc := range cases { - t.Run(tc.name, func(t *testing.T) { - t.Parallel() - got, services := orgCredentialKinds(bindings, tc.org, creds) - if len(got) != len(tc.want) { - t.Fatalf("kinds = %v, want %v", got, tc.want) - } - for service, kind := range tc.want { - if got[service] != kind { - t.Errorf("kind[%s] = %q, want %q", service, got[service], kind) - } - } - if len(services) != len(tc.services) { - t.Fatalf("services = %v, want %v", services, tc.services) - } - }) - } -} - -func TestSharedSkillsDoNotMaskImageDiscovery(t *testing.T) { - t.Parallel() - root := t.TempDir() - store := skill.StoreDir(root) - if err := os.MkdirAll(filepath.Join(store, "shared"), 0o700); err != nil { - t.Fatal(err) - } - if err := os.Symlink("/etc", filepath.Join(store, "escape")); err != nil { - t.Fatal(err) - } - for _, tc := range []struct { - name, root string - want int - }{ - {"shared", root, 1}, {"disabled", "", 0}, {"missing", filepath.Join(root, "missing"), 0}, - } { - t.Run(tc.name, func(t *testing.T) { - binds := skillsBinds([]spec.AgentSkillsCapability{{Path: "/agent/skills"}}, tc.root) - if len(binds) != tc.want { - t.Fatalf("binds=%v", binds) - } - if len(binds) > 0 && binds[0] != filepath.Join(store, "shared")+":/agent/skills/shared:ro,z" { - t.Fatalf("discovery directory masked or writable: %v", binds) - } - }) - } -} - -func TestHarnessProxyPasswordDoesNotExposeRunGrant(t *testing.T) { - l := &Launcher{Proxy: egress.NewProxy(nil, egress.ProxyOptions{})} - req := Request{RunCredential: "worker-state-store-grant", Org: "acme"} - session, err := l.taskSession(req, kitNeeds{}, nil) - if err != nil { - t.Fatal(err) - } - defer l.Proxy.Revoke(t.Context(), session.Token()) - if session.Token() == req.RunCredential { - t.Fatal("harness proxy password exposes the worker's State Store grant") - } - launch, err := kit.Assemble(&kit.Plan{}, kit.ImageConfig{}, kit.LaunchParams{ProxyToken: session.Token()}) - if err != nil { - t.Fatal(err) - } - env := strings.Join(launch.Harness.Env, "\n") - if strings.Contains(env, req.RunCredential) { - t.Fatal("worker grant entered the harness environment") - } - if !strings.Contains(env, session.Token()) { - t.Fatal("harness has no proxy password") - } -} diff --git a/internal/infrastructure/postgres/edastore.go b/internal/infrastructure/postgres/edastore.go index 3e23ba3ab..8e5621b86 100644 --- a/internal/infrastructure/postgres/edastore.go +++ b/internal/infrastructure/postgres/edastore.go @@ -40,7 +40,6 @@ var ( _ storecontract.DispatchLedger = (*EDA)(nil) _ storecontract.BindingDispatcher = (*EDA)(nil) _ storecontract.PlaybookDispatcher = (*EDA)(nil) - _ storecontract.HarnessSecretStore = (*EDA)(nil) ) // EDA is the PostgreSQL-backed EDA persistence. diff --git a/internal/infrastructure/postgres/harness_secrets.go b/internal/infrastructure/postgres/harness_secrets.go deleted file mode 100644 index 3d0b68619..000000000 --- a/internal/infrastructure/postgres/harness_secrets.go +++ /dev/null @@ -1,129 +0,0 @@ -package postgres - -import ( - "context" - "encoding/json" - "errors" - "fmt" - "time" - - "github.com/jackc/pgx/v5" - - "github.com/samcharles93/archie-core/internal/domain/harnesssecret" - "github.com/samcharles93/archie-core/internal/domain/storecontract" - "github.com/samcharles93/archie-core/internal/infrastructure/bindingcipher" - "github.com/samcharles93/archie-core/internal/infrastructure/postgres/postgresdb" -) - -// harnessSecretPayload is the JSON shape sealed into harness_secrets.secret_enc: -// the whole token set behind one envelope, never separate plaintext columns. -type harnessSecretPayload struct { - AccessToken string `json:"access_token"` - RefreshToken string `json:"refresh_token"` - TokenType string `json:"token_type"` - ExpiresAt time.Time `json:"expires_at"` - Scopes []string `json:"scopes,omitempty"` -} - -// GetHarnessSecret returns the stored OAuth token set for an org/service -// credential binding, or storecontract.ErrHarnessSecretNotFound. -func (s *EDA) GetHarnessSecret(ctx context.Context, org, service string) (harnesssecret.Secret, error) { - row, err := s.q.GetHarnessSecret(ctx, postgresdb.GetHarnessSecretParams{Org: org, Service: service}) - if errors.Is(err, pgx.ErrNoRows) { - return harnesssecret.Secret{}, storecontract.ErrHarnessSecretNotFound - } - if err != nil { - return harnesssecret.Secret{}, fmt.Errorf("postgres: get harness secret: %w", err) - } - payload, err := s.openHarnessSecret(row.SecretEnc) - if err != nil { - return harnesssecret.Secret{}, err - } - return harnesssecret.Secret{ - Org: row.Org, Service: row.Service, - AccessToken: payload.AccessToken, RefreshToken: payload.RefreshToken, - TokenType: payload.TokenType, ExpiresAt: payload.ExpiresAt, - Scopes: payload.Scopes, - UpdatedAt: row.UpdatedAt, - }, nil -} - -// PutHarnessSecret upserts an org/service credential binding's OAuth token -// set. Called by the setup terminal's first capture and by the egress -// proxy's every refresh. -func (s *EDA) PutHarnessSecret(ctx context.Context, secret harnesssecret.Secret) error { - if err := secret.Validate(); err != nil { - return err - } - sealed, err := s.sealHarnessSecret(harnessSecretPayload{ - AccessToken: secret.AccessToken, RefreshToken: secret.RefreshToken, - TokenType: secret.TokenType, ExpiresAt: secret.ExpiresAt, - Scopes: secret.Scopes, - }) - if err != nil { - return err - } - tx, err := s.pool.Begin(ctx) - if err != nil { - return err - } - defer func() { _ = tx.Rollback(ctx) }() - q := postgresdb.New(tx) - inserted, err := q.PutHarnessSecret(ctx, postgresdb.PutHarnessSecretParams{ - Org: secret.Org, Service: secret.Service, SecretEnc: sealed, - }) - if err != nil { - return fmt.Errorf("postgres: put harness secret: %w", err) - } - // The trail records that the credential changed, never any part of it: no - // access token, refresh token or ciphertext. The insert flag is read by the - // same statement, so a concurrent first capture is an update, not a second - // create. A first capture has a nil old side. - var oldValue []byte - if !inserted { - if oldValue, err = auditJSON(map[string]bool{"changed": true}); err != nil { - return err - } - } - newValue, err := auditJSON(map[string]bool{"changed": true}) - if err != nil { - return err - } - if err := insertAudit(ctx, q, "harness_secrets", secret.Service, "credential", oldValue, newValue, 0, secret.Org); err != nil { - return err - } - return tx.Commit(ctx) -} - -// sealHarnessSecret encrypts the token set under HarnessSecretDomain. An -// encryption key is required. -func (s *EDA) sealHarnessSecret(payload harnessSecretPayload) (string, error) { - if s.cipher == nil { - return "", errors.New("postgres: harness secrets require an encryption key") - } - raw, err := json.Marshal(payload) - if err != nil { - return "", fmt.Errorf("postgres: marshal harness secret: %w", err) - } - sealed, err := s.cipher.EncryptDomain(bindingcipher.HarnessSecretDomain, string(raw)) - if err != nil { - return "", fmt.Errorf("postgres: encrypt harness secret: %w", err) - } - return sealed, nil -} - -// openHarnessSecret reverses sealHarnessSecret. -func (s *EDA) openHarnessSecret(stored string) (harnessSecretPayload, error) { - if s.cipher == nil { - return harnessSecretPayload{}, errors.New("postgres: harness secrets require an encryption key") - } - raw, err := s.cipher.DecryptDomain(bindingcipher.HarnessSecretDomain, stored) - if err != nil { - return harnessSecretPayload{}, fmt.Errorf("postgres: decrypt harness secret: %w", err) - } - var payload harnessSecretPayload - if err := json.Unmarshal([]byte(raw), &payload); err != nil { - return harnessSecretPayload{}, fmt.Errorf("postgres: unmarshal harness secret: %w", err) - } - return payload, nil -} diff --git a/internal/infrastructure/postgres/migrations/0075_drop_harness_secrets.sql b/internal/infrastructure/postgres/migrations/0075_drop_harness_secrets.sql new file mode 100644 index 000000000..e04f99f09 --- /dev/null +++ b/internal/infrastructure/postgres/migrations/0075_drop_harness_secrets.sql @@ -0,0 +1,11 @@ +-- +goose Up +DROP TABLE IF EXISTS harness_secrets; + +-- +goose Down +CREATE TABLE harness_secrets ( + org TEXT NOT NULL, + service TEXT NOT NULL, + secret_enc TEXT NOT NULL, + updated_at TIMESTAMPTZ NOT NULL DEFAULT now(), + PRIMARY KEY (org, service) +); diff --git a/internal/infrastructure/postgres/postgresdb/harness_secrets.sql.go b/internal/infrastructure/postgres/postgresdb/harness_secrets.sql.go deleted file mode 100644 index 6a159263d..000000000 --- a/internal/infrastructure/postgres/postgresdb/harness_secrets.sql.go +++ /dev/null @@ -1,59 +0,0 @@ -// Code generated by sqlc. DO NOT EDIT. -// versions: -// sqlc v1.31.1 -// source: harness_secrets.sql - -package postgresdb - -import ( - "context" -) - -const getHarnessSecret = `-- name: GetHarnessSecret :one - -SELECT org, service, secret_enc, updated_at -FROM harness_secrets WHERE org = $1 AND service = $2 -` - -type GetHarnessSecretParams struct { - Org string - Service string -} - -// Harness OAuth secret queries (docs/prds/external-agent-harness.md, -// Credentials). One row per org/service; secret_enc holds the whole token -// set (access token, refresh token, token type, expiry) as one encrypted -// envelope over a JSON payload, never separate plaintext columns. -func (q *Queries) GetHarnessSecret(ctx context.Context, arg GetHarnessSecretParams) (HarnessSecret, error) { - row := q.db.QueryRow(ctx, getHarnessSecret, arg.Org, arg.Service) - var i HarnessSecret - err := row.Scan( - &i.Org, - &i.Service, - &i.SecretEnc, - &i.UpdatedAt, - ) - return i, err -} - -const putHarnessSecret = `-- name: PutHarnessSecret :one -INSERT INTO harness_secrets (org, service, secret_enc, updated_at) -VALUES ($1, $2, $3, now()) -ON CONFLICT (org, service) DO UPDATE SET secret_enc = EXCLUDED.secret_enc, updated_at = now() -RETURNING (xmax = 0) AS inserted -` - -type PutHarnessSecretParams struct { - Org string - Service string - SecretEnc string -} - -// inserted is true only when this statement created the row: xmax is zero for -// a fresh insert and nonzero for the upsert's update side. -func (q *Queries) PutHarnessSecret(ctx context.Context, arg PutHarnessSecretParams) (bool, error) { - row := q.db.QueryRow(ctx, putHarnessSecret, arg.Org, arg.Service, arg.SecretEnc) - var inserted bool - err := row.Scan(&inserted) - return inserted, err -} diff --git a/internal/infrastructure/postgres/postgresdb/models.go b/internal/infrastructure/postgres/postgresdb/models.go index d7eb05c57..cbb57646b 100644 --- a/internal/infrastructure/postgres/postgresdb/models.go +++ b/internal/infrastructure/postgres/postgresdb/models.go @@ -225,13 +225,6 @@ type EventType struct { WorkspaceID string } -type HarnessSecret struct { - Org string - Service string - SecretEnc string - UpdatedAt time.Time -} - type Identity struct { ID string Kind string diff --git a/internal/infrastructure/postgres/queries/harness_secrets.sql b/internal/infrastructure/postgres/queries/harness_secrets.sql deleted file mode 100644 index 3a8f70dbb..000000000 --- a/internal/infrastructure/postgres/queries/harness_secrets.sql +++ /dev/null @@ -1,16 +0,0 @@ --- Harness OAuth secret queries (docs/prds/external-agent-harness.md, --- Credentials). One row per org/service; secret_enc holds the whole token --- set (access token, refresh token, token type, expiry) as one encrypted --- envelope over a JSON payload, never separate plaintext columns. - --- name: GetHarnessSecret :one -SELECT org, service, secret_enc, updated_at -FROM harness_secrets WHERE org = $1 AND service = $2; - --- inserted is true only when this statement created the row: xmax is zero for --- a fresh insert and nonzero for the upsert's update side. --- name: PutHarnessSecret :one -INSERT INTO harness_secrets (org, service, secret_enc, updated_at) -VALUES ($1, $2, $3, now()) -ON CONFLICT (org, service) DO UPDATE SET secret_enc = EXCLUDED.secret_enc, updated_at = now() -RETURNING (xmax = 0) AS inserted; diff --git a/internal/infrastructure/staterpc/client.go b/internal/infrastructure/staterpc/client.go index a1e52ecf1..6a0ca70c1 100644 --- a/internal/infrastructure/staterpc/client.go +++ b/internal/infrastructure/staterpc/client.go @@ -18,31 +18,30 @@ import ( // as gatewayrpc.Client asserts both messaging.ChatContract and // messaging.SessionStore. type Client struct { - packages pb.PackageServiceClient - taskGrant pb.TaskGrantServiceClient - harnessSecret pb.HarnessSecretServiceClient - account pb.AccountServiceClient - identity pb.IdentityServiceClient - task pb.TaskServiceClient - execution pb.ExecutionServiceClient - usageOutbox *usageOutbox - review pb.ReviewServiceClient - taskEvent pb.TaskEventServiceClient - stats pb.StatsServiceClient - taskLog pb.TaskLogServiceClient - status pb.StatusServiceClient - capture pb.CaptureServiceClient - mapping pb.MappingServiceClient - eventType pb.EventTypeServiceClient - binding pb.BindingServiceClient - dispatch pb.DispatchServiceClient - source pb.SourceServiceClient - access pb.AccessServiceClient - org pb.OrgServiceClient - alert pb.AlertServiceClient - audit pb.AuditServiceClient - schedule pb.ScheduleServiceClient - controlPlane controlpb.ControlPlaneServiceClient + packages pb.PackageServiceClient + taskGrant pb.TaskGrantServiceClient + account pb.AccountServiceClient + identity pb.IdentityServiceClient + task pb.TaskServiceClient + execution pb.ExecutionServiceClient + usageOutbox *usageOutbox + review pb.ReviewServiceClient + taskEvent pb.TaskEventServiceClient + stats pb.StatsServiceClient + taskLog pb.TaskLogServiceClient + status pb.StatusServiceClient + capture pb.CaptureServiceClient + mapping pb.MappingServiceClient + eventType pb.EventTypeServiceClient + binding pb.BindingServiceClient + dispatch pb.DispatchServiceClient + source pb.SourceServiceClient + access pb.AccessServiceClient + org pb.OrgServiceClient + alert pb.AlertServiceClient + audit pb.AuditServiceClient + schedule pb.ScheduleServiceClient + controlPlane controlpb.ControlPlaneServiceClient } // NewClient wraps conn's generated clients. The client keeps a local usage @@ -50,30 +49,29 @@ type Client struct { // the store accepts it. Close stops that retry worker. func NewClient(conn grpc.ClientConnInterface) *Client { c := &Client{ - packages: pb.NewPackageServiceClient(conn), - taskGrant: pb.NewTaskGrantServiceClient(conn), - harnessSecret: pb.NewHarnessSecretServiceClient(conn), - account: pb.NewAccountServiceClient(conn), - identity: pb.NewIdentityServiceClient(conn), - task: pb.NewTaskServiceClient(conn), - execution: pb.NewExecutionServiceClient(conn), - review: pb.NewReviewServiceClient(conn), - taskEvent: pb.NewTaskEventServiceClient(conn), - stats: pb.NewStatsServiceClient(conn), - taskLog: pb.NewTaskLogServiceClient(conn), - status: pb.NewStatusServiceClient(conn), - capture: pb.NewCaptureServiceClient(conn), - mapping: pb.NewMappingServiceClient(conn), - eventType: pb.NewEventTypeServiceClient(conn), - binding: pb.NewBindingServiceClient(conn), - dispatch: pb.NewDispatchServiceClient(conn), - source: pb.NewSourceServiceClient(conn), - access: pb.NewAccessServiceClient(conn), - org: pb.NewOrgServiceClient(conn), - alert: pb.NewAlertServiceClient(conn), - audit: pb.NewAuditServiceClient(conn), - schedule: pb.NewScheduleServiceClient(conn), - controlPlane: controlpb.NewControlPlaneServiceClient(conn), + packages: pb.NewPackageServiceClient(conn), + taskGrant: pb.NewTaskGrantServiceClient(conn), + account: pb.NewAccountServiceClient(conn), + identity: pb.NewIdentityServiceClient(conn), + task: pb.NewTaskServiceClient(conn), + execution: pb.NewExecutionServiceClient(conn), + review: pb.NewReviewServiceClient(conn), + taskEvent: pb.NewTaskEventServiceClient(conn), + stats: pb.NewStatsServiceClient(conn), + taskLog: pb.NewTaskLogServiceClient(conn), + status: pb.NewStatusServiceClient(conn), + capture: pb.NewCaptureServiceClient(conn), + mapping: pb.NewMappingServiceClient(conn), + eventType: pb.NewEventTypeServiceClient(conn), + binding: pb.NewBindingServiceClient(conn), + dispatch: pb.NewDispatchServiceClient(conn), + source: pb.NewSourceServiceClient(conn), + access: pb.NewAccessServiceClient(conn), + org: pb.NewOrgServiceClient(conn), + alert: pb.NewAlertServiceClient(conn), + audit: pb.NewAuditServiceClient(conn), + schedule: pb.NewScheduleServiceClient(conn), + controlPlane: controlpb.NewControlPlaneServiceClient(conn), } c.usageOutbox = newUsageOutbox(c.writeUsage) return c @@ -92,7 +90,6 @@ func (c *Client) Close() error { } var ( - _ storecontract.HarnessSecretStore = (*Client)(nil) _ task.Store = (*Client)(nil) _ task.Caller = (*Client)(nil) _ storecontract.TaskStore = (*Client)(nil) diff --git a/internal/infrastructure/staterpc/harness_secret_test.go b/internal/infrastructure/staterpc/harness_secret_test.go deleted file mode 100644 index 1f4a778bd..000000000 --- a/internal/infrastructure/staterpc/harness_secret_test.go +++ /dev/null @@ -1,175 +0,0 @@ -package staterpc - -import ( - "context" - "errors" - "strings" - "testing" - "time" - - "google.golang.org/grpc" - "google.golang.org/grpc/codes" - "google.golang.org/grpc/status" - "google.golang.org/protobuf/encoding/protowire" - "google.golang.org/protobuf/proto" - - pb "github.com/samcharles93/archie-core/internal/contracts/state/v1" - "github.com/samcharles93/archie-core/internal/domain/harnesssecret" - "github.com/samcharles93/archie-core/internal/domain/org" - "github.com/samcharles93/archie-core/internal/domain/storecontract" - "github.com/samcharles93/archie-core/internal/domain/workflow/task" -) - -type harnessCapture struct{ got harnesssecret.Secret } - -func (h *harnessCapture) GetHarnessSecret(_ context.Context, org, service string) (harnesssecret.Secret, error) { - h.got = harnesssecret.Secret{Org: org, Service: service} - return h.got, nil -} - -func (h *harnessCapture) PutHarnessSecret(_ context.Context, value harnesssecret.Secret) error { - h.got = value - return nil -} - -func TestHarnessSecretsUseCallerOrg(t *testing.T) { - for _, caller := range []org.OrgID{"acme", org.DefaultOrgID, ""} { - t.Run(string(caller), func(t *testing.T) { - capture := &harnessCapture{} - s := &harnessSecretServer{server: &server{deps: Deps{HarnessSecrets: capture}}} - ctx := org.WithOrg(context.Background(), caller) - // A stale or malicious client still sending field 1 cannot choose the org. - wire := protowire.AppendString(protowire.AppendTag(nil, 1, protowire.BytesType), "victim") - req := &pb.GetHarnessSecretRequest{} - if err := proto.Unmarshal(wire, req); err != nil { - t.Fatal(err) - } - req.Service = "provider" - if _, err := s.GetHarnessSecret(ctx, req); err != nil { - t.Fatal(err) - } - if capture.got.Org != string(org.OrgFromContext(ctx)) { - t.Fatalf("read org = %q, want caller org %q", capture.got.Org, org.OrgFromContext(ctx)) - } - secret := &pb.HarnessSecret{} - if err := proto.Unmarshal(wire, secret); err != nil { - t.Fatal(err) - } - secret.Service = "provider" - if _, err := s.PutHarnessSecret(ctx, &pb.PutHarnessSecretRequest{Secret: secret}); err != nil { - t.Fatal(err) - } - if capture.got.Org != string(org.OrgFromContext(ctx)) { - t.Fatalf("write org = %q, want caller org %q", capture.got.Org, org.OrgFromContext(ctx)) - } - }) - } -} - -type harnessTasks struct { - storecontract.TaskStore - err error -} - -func (h harnessTasks) TaskByID(context.Context, int64) (*task.Task, error) { - return &task.Task{ID: 7, Org: "acme"}, h.err -} - -func TestHarnessSecretsUseTaskGrant(t *testing.T) { - for _, tt := range []struct { - name, token string - scope org.OrgID - taskErr error - want codes.Code - }{ - {name: "live grant", token: strings.Repeat("ab", 32)}, - {name: "unknown grant", token: strings.Repeat("cd", 32), want: codes.Unauthenticated}, - {name: "another org", token: strings.Repeat("ab", 32), scope: "other", want: codes.PermissionDenied}, - {name: "system principal cannot cross org", token: strings.Repeat("ab", 32), scope: org.DefaultOrgID, want: codes.PermissionDenied}, - {name: "task lookup failure", token: strings.Repeat("ab", 32), taskErr: errors.New("store unavailable"), want: codes.Internal}, - } { - t.Run(tt.name, func(t *testing.T) { - grants := &TaskGrants{Store: credentials{}} - if err := grants.register(t.Context(), 7, strings.Repeat("ab", 32), time.Hour); err != nil { - t.Fatal(err) - } - capture := &harnessCapture{} - s := &harnessSecretServer{server: &server{deps: Deps{Grants: grants, Tasks: harnessTasks{err: tt.taskErr}, HarnessSecrets: capture}}} - ctx := org.WithScope(t.Context(), tt.scope) - for _, write := range []bool{false, true} { - capture.got = harnesssecret.Secret{} - var err error - if write { - _, err = s.PutHarnessSecret(ctx, &pb.PutHarnessSecretRequest{RunCredential: tt.token, Secret: &pb.HarnessSecret{Service: "provider"}}) - } else { - _, err = s.GetHarnessSecret(ctx, &pb.GetHarnessSecretRequest{RunCredential: tt.token, Service: "provider"}) - } - if status.Code(err) != tt.want { - t.Fatalf("write %v: error %v, want %v", write, err, tt.want) - } - if tt.want == codes.OK && capture.got.Org != "acme" { - t.Fatalf("write %v: org %q, want acme", write, capture.got.Org) - } - if tt.want != codes.OK && capture.got.Org != "" { - t.Fatalf("unauthorized access reached store: %+v", capture.got) - } - } - }) - } -} - -type harnessRPC struct { - pb.HarnessSecretServiceClient - server *harnessSecretServer -} - -func (h harnessRPC) GetHarnessSecret(ctx context.Context, r *pb.GetHarnessSecretRequest, _ ...grpc.CallOption) (*pb.GetHarnessSecretResponse, error) { - return h.server.GetHarnessSecret(ctx, r) -} - -func (h harnessRPC) PutHarnessSecret(ctx context.Context, r *pb.PutHarnessSecretRequest, _ ...grpc.CallOption) (*pb.PutHarnessSecretResponse, error) { - return h.server.PutHarnessSecret(ctx, r) -} - -func TestHarnessSecretClientGrant(t *testing.T) { - grants := &TaskGrants{Store: credentials{}} - token := strings.Repeat("ab", 32) - if err := grants.register(t.Context(), 7, token, time.Hour); err != nil { - t.Fatal(err) - } - capture := &harnessCapture{} - c := &Client{harnessSecret: harnessRPC{server: &harnessSecretServer{server: &server{deps: Deps{Grants: grants, Tasks: harnessTasks{}, HarnessSecrets: capture}}}}} - ctx := harnesssecret.WithRunCredential(t.Context(), token) - for _, tt := range []struct { - name, org string - revoke bool - want codes.Code - }{ - {name: "task org", org: "acme"}, - {name: "another org", org: "victim", want: codes.PermissionDenied}, - {name: "revoked grant", org: "acme", revoke: true, want: codes.Unauthenticated}, - } { - t.Run(tt.name, func(t *testing.T) { - if tt.revoke { - if err := grants.revoke(ctx, token); err != nil { - t.Fatal(err) - } - } - secret, err := c.GetHarnessSecret(ctx, tt.org, "provider") - if status.Code(err) != tt.want { - t.Fatalf("error %v, want %v", err, tt.want) - } - if tt.want == codes.OK { - if secret.Org != "acme" { - t.Fatalf("secret org %q, want acme", secret.Org) - } - if err := c.PutHarnessSecret(ctx, secret); err != nil { - t.Fatal(err) - } - if capture.got.Org != "acme" { - t.Fatalf("stored org %q, want acme", capture.got.Org) - } - } - }) - } -} diff --git a/internal/infrastructure/staterpc/harness_secrets.go b/internal/infrastructure/staterpc/harness_secrets.go deleted file mode 100644 index ba3f16129..000000000 --- a/internal/infrastructure/staterpc/harness_secrets.go +++ /dev/null @@ -1,97 +0,0 @@ -package staterpc - -import ( - "context" - - "google.golang.org/grpc/codes" - "google.golang.org/grpc/status" - - pb "github.com/samcharles93/archie-core/internal/contracts/state/v1" - "github.com/samcharles93/archie-core/internal/domain/harnesssecret" - "github.com/samcharles93/archie-core/internal/domain/org" -) - -type harnessSecretServer struct { - pb.UnimplementedHarnessSecretServiceServer - *server -} - -// harnessOrg derives scope from a live run credential or the authenticated caller. -func (s *server) harnessOrg(ctx context.Context, credential string) (string, error) { - if credential == "" { - return string(org.OrgFromContext(ctx)), nil - } - if s.deps.Grants == nil || s.deps.Tasks == nil { - return "", status.Error(codes.Unavailable, "task grants unavailable") - } - id, err := s.deps.Grants.taskFor(ctx, credential) - if err != nil { - return "", s.deps.Grants.unavailable(ctx, "harnessOrg", err) - } - if id == 0 { - return "", status.Error(codes.Unauthenticated, "invalid task grant") - } - t, err := s.deps.Tasks.TaskByID(ctx, id) - if err != nil { - return "", mapError(err) - } - target := t.OrgOrDefault() - if scope, ok := org.Scope(ctx); ok && string(scope) != target { - return "", status.Error(codes.PermissionDenied, "harness secret belongs to another org") - } - return target, nil -} - -func (s *harnessSecretServer) GetHarnessSecret(ctx context.Context, r *pb.GetHarnessSecretRequest) (*pb.GetHarnessSecretResponse, error) { - if s.deps.HarnessSecrets == nil { - return nil, status.Error(codes.Unavailable, "harness secret store unavailable") - } - orgID, err := s.harnessOrg(ctx, r.RunCredential) - if err != nil { - return nil, err - } - secret, err := s.deps.HarnessSecrets.GetHarnessSecret(ctx, orgID, r.Service) - if err != nil { - return nil, s.logErr("GetHarnessSecret", err) - } - return &pb.GetHarnessSecretResponse{Secret: harnessSecretProto(secret), Org: orgID}, nil -} - -func (s *harnessSecretServer) PutHarnessSecret(ctx context.Context, r *pb.PutHarnessSecretRequest) (*pb.PutHarnessSecretResponse, error) { - if s.deps.HarnessSecrets == nil { - return nil, status.Error(codes.Unavailable, "harness secret store unavailable") - } - value := harnessSecretValue(r.Secret) - orgID, err := s.harnessOrg(ctx, r.RunCredential) - if err != nil { - return nil, err - } - value.Org = orgID - if err := s.deps.HarnessSecrets.PutHarnessSecret(ctx, value); err != nil { - return nil, s.logErr("PutHarnessSecret", err) - } - return &pb.PutHarnessSecretResponse{}, nil -} - -// GetHarnessSecret returns the stored OAuth token set for an org/service -// credential binding, or storecontract.ErrHarnessSecretNotFound. -func (c *Client) GetHarnessSecret(ctx context.Context, org, service string) (harnesssecret.Secret, error) { - r, err := c.harnessSecret.GetHarnessSecret(ctx, &pb.GetHarnessSecretRequest{Service: service, RunCredential: harnesssecret.RunCredential(ctx)}) - if err != nil { - return harnesssecret.Secret{}, unmapError(err) - } - if r.Org != org { - return harnesssecret.Secret{}, status.Error(codes.PermissionDenied, "harness secret belongs to another org") - } - value := harnessSecretValue(r.Secret) - value.Org = r.Org - return value, nil -} - -// PutHarnessSecret upserts an org/service credential binding's OAuth token -// set, called by the setup terminal's first capture and by the egress -// proxy's every refresh. -func (c *Client) PutHarnessSecret(ctx context.Context, s harnesssecret.Secret) error { - _, err := c.harnessSecret.PutHarnessSecret(ctx, &pb.PutHarnessSecretRequest{Secret: harnessSecretProto(s), RunCredential: harnesssecret.RunCredential(ctx)}) - return unmapError(err) -} diff --git a/internal/infrastructure/staterpc/server.go b/internal/infrastructure/staterpc/server.go index aa699e830..e1e47c733 100644 --- a/internal/infrastructure/staterpc/server.go +++ b/internal/infrastructure/staterpc/server.go @@ -124,10 +124,6 @@ type Deps struct { // task-scoped-grant callable. Optional: nil answers ListSteps with // codes.Unavailable. StepReader storecontract.StepReader - // HarnessSecrets persists harness OAuth token sets. - // Optional: nil - // answers both RPCs with codes.Unavailable. - HarnessSecrets storecontract.HarnessSecretStore // PlaybookDispatcher is the side-effecting-action idempotency ledger. // Optional: nil disables the // two playbook dispatch RPCs with codes.Unavailable. @@ -185,7 +181,6 @@ func RegisterServer(registrar grpc.ServiceRegistrar, deps Deps) { s := &server{deps: deps} pb.RegisterPackageServiceServer(registrar, &packageServer{server: s}) pb.RegisterTaskGrantServiceServer(registrar, &taskGrantServer{server: s}) - pb.RegisterHarnessSecretServiceServer(registrar, &harnessSecretServer{server: s}) pb.RegisterAccountServiceServer(registrar, &accountServer{server: s}) pb.RegisterIdentityServiceServer(registrar, &identityServer{server: s}) pb.RegisterTaskServiceServer(registrar, &taskServer{server: s}) diff --git a/internal/infrastructure/staterpc/values.go b/internal/infrastructure/staterpc/values.go index 1e8e320e7..40d1e26be 100644 --- a/internal/infrastructure/staterpc/values.go +++ b/internal/infrastructure/staterpc/values.go @@ -5,7 +5,6 @@ import ( "encoding/json" "errors" "fmt" - "slices" "time" "google.golang.org/grpc/codes" @@ -16,7 +15,6 @@ import ( "github.com/samcharles93/archie-core/internal/domain/access" "github.com/samcharles93/archie-core/internal/domain/binding" "github.com/samcharles93/archie-core/internal/domain/eventtype" - "github.com/samcharles93/archie-core/internal/domain/harnesssecret" "github.com/samcharles93/archie-core/internal/domain/health" "github.com/samcharles93/archie-core/internal/domain/mapping" "github.com/samcharles93/archie-core/internal/domain/org" @@ -368,27 +366,6 @@ func stepExecutionValue(s *pb.StepExecution) task.StepExecution { } } -func harnessSecretProto(s harnesssecret.Secret) *pb.HarnessSecret { - return &pb.HarnessSecret{ - Service: s.Service, AccessToken: s.AccessToken, - RefreshToken: s.RefreshToken, TokenType: s.TokenType, - ExpiresAt: timestamp(s.ExpiresAt), UpdatedAt: timestamp(s.UpdatedAt), - Scopes: s.Scopes, - } -} - -func harnessSecretValue(s *pb.HarnessSecret) harnesssecret.Secret { - if s == nil { - return harnesssecret.Secret{} - } - return harnesssecret.Secret{ - Service: s.Service, AccessToken: s.AccessToken, - RefreshToken: s.RefreshToken, TokenType: s.TokenType, - ExpiresAt: timeValue(s.ExpiresAt), UpdatedAt: timeValue(s.UpdatedAt), - Scopes: slices.Clone(s.Scopes), - } -} - func stageStatProto(s storecontract.StageStat) *pb.StageStat { return &pb.StageStat{Workflow: s.Workflow, Stage: s.Stage, Runs: int64(s.Runs), AvgMs: int64(s.AvgMs), Errors: int64(s.Errors)} } @@ -415,21 +392,20 @@ func dayTokensValue(d *pb.DayTokens) storecontract.DayTokens { // are the wire contract for sentinel identity -- the client rehydrates by // matching (code, message), never by parsing free-form text. const ( - msgStaleTransition = "stale transition" - msgIllegalTransition = "illegal transition" - msgInvalidStep = "step execution invalid" - msgBindingNotFound = "binding not found" - msgMappingNotFound = "mapping not found" - msgEventTypeNotFound = "event type not found" - msgEventTypeOverlap = "event type overlap" - msgEventTypeInvalid = "event type invalid" - msgBindingOverlap = "binding overlap" - msgBindingTransition = "binding transition rejected" - msgAlreadyDispatched = "already dispatched" - msgSourceNotFound = "source not found" - msgSourcePathTaken = "source path taken" - msgSourceSigning = "source signing stale" - msgHarnessSecretNotFound = "harness secret not found" + msgStaleTransition = "stale transition" + msgIllegalTransition = "illegal transition" + msgInvalidStep = "step execution invalid" + msgBindingNotFound = "binding not found" + msgMappingNotFound = "mapping not found" + msgEventTypeNotFound = "event type not found" + msgEventTypeOverlap = "event type overlap" + msgEventTypeInvalid = "event type invalid" + msgBindingOverlap = "binding overlap" + msgBindingTransition = "binding transition rejected" + msgAlreadyDispatched = "already dispatched" + msgSourceNotFound = "source not found" + msgSourcePathTaken = "source path taken" + msgSourceSigning = "source signing stale" // msgRereviewCapReached is the canonical message of // storecontract.ErrRereviewCapReached on the wire. Changing it breaks // errors.Is on the client without changing behaviour visibly. @@ -530,7 +506,6 @@ var wireErrors = []struct { {storecontract.ErrSourcePathTaken, codes.AlreadyExists, msgSourcePathTaken}, {org.ErrInvalidWorkspace, codes.InvalidArgument, org.ErrInvalidWorkspace.Error()}, {org.ErrLastOwner, codes.FailedPrecondition, org.ErrLastOwner.Error()}, - {storecontract.ErrHarnessSecretNotFound, codes.NotFound, msgHarnessSecretNotFound}, {storecontract.ErrRereviewCapReached, codes.FailedPrecondition, msgRereviewCapReached}, {storepkg.ErrNotFound, codes.NotFound, storepkg.ErrNotFound.Error()}, {storepkg.ErrInstalled, codes.AlreadyExists, storepkg.ErrInstalled.Error()}, diff --git a/internal/infrastructure/tasktools/client.go b/internal/infrastructure/tasktools/client.go deleted file mode 100644 index 019d459b9..000000000 --- a/internal/infrastructure/tasktools/client.go +++ /dev/null @@ -1,62 +0,0 @@ -// Package tasktools adapts task-scoped central tools to the Gateway contract. -package tasktools - -import ( - "context" - "encoding/json" - - "github.com/modelcontextprotocol/go-sdk/mcp" - "google.golang.org/grpc" - - pb "github.com/samcharles93/archie-core/internal/contracts/gateway/v1" - "github.com/samcharles93/archie-core/internal/domain/messaging" - "github.com/samcharles93/archie-core/internal/domain/presence" - "github.com/samcharles93/archie-core/internal/infrastructure/rpctls" - "github.com/samcharles93/archie-core/internal/infrastructure/staterpc" - "github.com/samcharles93/archie-core/internal/infrastructure/telemetry" -) - -// Client never accepts an identity, org or permission list from the harness. -type Client struct{ rpc pb.TaskToolsServiceClient } - -func NewClient(conn grpc.ClientConnInterface) *Client { - return &Client{rpc: pb.NewTaskToolsServiceClient(conn)} -} - -func (c *Client) List(ctx context.Context, credential string) ([]*mcp.Tool, error) { - r, err := c.rpc.ListTaskTools(ctx, &pb.ListTaskToolsRequest{RunCredential: credential}) - if err != nil { - return nil, err - } - var tools []*mcp.Tool - err = json.Unmarshal(r.ToolsJson, &tools) - return tools, err -} - -func (c *Client) Call(ctx context.Context, credential, name string, args json.RawMessage) (*mcp.CallToolResult, error) { - r, err := c.rpc.CallTaskTool(ctx, &pb.CallTaskToolRequest{RunCredential: credential, Name: name, ArgumentsJson: args}) - if err != nil { - return nil, err - } - var result mcp.CallToolResult - err = json.Unmarshal(r.ResultJson, &result) - return &result, err -} - -func (c *Client) Close(ctx context.Context, credential string) error { - _, err := c.rpc.CloseTaskTools(ctx, &pb.CloseTaskToolsRequest{RunCredential: credential}) - return err -} - -// Dial opens the Gateway's task-tools contract as the authenticated daemon. -func Dial(target string, id *rpctls.Identity) (*Client, func(), error) { - creds, err := id.DialOption(target, presence.Gateway) - if err != nil { - return nil, nil, err - } - conn, err := grpc.NewClient(target, creds, telemetry.ClientOption(), grpc.WithUnaryInterceptor(staterpc.UnaryClientCredentialInterceptor(presence.Daemon, "")), grpc.WithDefaultCallOptions(grpc.MaxCallRecvMsgSize(messaging.MaxInboundAttachmentBytes+(1<<20)))) - if err != nil { - return nil, nil, err - } - return NewClient(conn), func() { _ = conn.Close() }, nil -} diff --git a/internal/infrastructure/tasktools/http.go b/internal/infrastructure/tasktools/http.go deleted file mode 100644 index f81feecd1..000000000 --- a/internal/infrastructure/tasktools/http.go +++ /dev/null @@ -1,47 +0,0 @@ -package tasktools - -import ( - "context" - "crypto/subtle" - "encoding/json" - "net/http" - - "github.com/modelcontextprotocol/go-sdk/mcp" -) - -const ( - Endpoint = "http://archie.tools/mcp" - Sentinel = "archie-task-tools-sentinel" -) - -type Backend interface { - List(context.Context, string) ([]*mcp.Tool, error) - Call(context.Context, string, string, json.RawMessage) (*mcp.CallToolResult, error) - Close(context.Context, string) error -} - -// ServeHTTP creates a fresh view so tool discovery never outlives permissions. -func ServeHTTP(ctx context.Context, w http.ResponseWriter, r *http.Request, backend Backend, credential, token string) { - if token == "" || subtle.ConstantTimeCompare([]byte(r.Header.Get("Authorization")), []byte("Bearer "+token)) != 1 { - http.Error(w, "invalid gateway credential", http.StatusUnauthorized) - return - } - tools, err := backend.List(ctx, credential) - if err != nil { - http.Error(w, "task tools unavailable", http.StatusBadGateway) - return - } - server := mcp.NewServer(&mcp.Implementation{Name: "archie-central", Version: "1"}, nil) - for _, tool := range tools { - server.AddTool(tool, func(methodCtx context.Context, req *mcp.CallToolRequest) (*mcp.CallToolResult, error) { - // MCP handler contexts may outlive HTTP; proxy revocation must not. - callCtx, cancel := context.WithCancel(methodCtx) - defer cancel() - stop := context.AfterFunc(ctx, cancel) - defer stop() - return backend.Call(callCtx, credential, req.Params.Name, req.Params.Arguments) - }) - } - // The proxy has already authenticated the session and confined Host and path. - mcp.NewStreamableHTTPHandler(func(*http.Request) *mcp.Server { return server }, &mcp.StreamableHTTPOptions{Stateless: true, DisableLocalhostProtection: true}).ServeHTTP(w, r.WithContext(ctx)) -} diff --git a/internal/releaseupdate/topology.go b/internal/releaseupdate/topology.go index 2600cd2d3..aee784e50 100644 --- a/internal/releaseupdate/topology.go +++ b/internal/releaseupdate/topology.go @@ -9,8 +9,7 @@ type Topology struct { Units []string `json:"units"` ConfigSections []string `json:"config_sections"` // Binaries are the release's host binaries that run beside the units - // without a service of their own, such as the archie-agent a Kit run - // mounts from archied's directory. They are installed and switched with + // without a service of their own. They are installed and switched with // the units and are never started, stopped or demanded as a unit. Binaries []string `json:"binaries,omitempty"` } diff --git a/internal/skill/skill.go b/internal/skill/skill.go index 66e1705c5..bd82389e4 100644 --- a/internal/skill/skill.go +++ b/internal/skill/skill.go @@ -178,8 +178,7 @@ func SkillForWorkflow(catalog []CatalogEntry, workflow string) *CatalogEntry { const skillsDir = ".agents/skills" -// StoreDir is where root keeps its skills: the directory a skills store -// mounted into a harness exposes. +// StoreDir is where root keeps its skills. func StoreDir(root string) string { return filepath.Join(root, skillsDir) } // Parse extracts YAML frontmatter and body from SKILL.md content. diff --git a/internal/taskrun/taskrun.go b/internal/taskrun/taskrun.go index c48519fb3..45dc94713 100644 --- a/internal/taskrun/taskrun.go +++ b/internal/taskrun/taskrun.go @@ -30,9 +30,6 @@ type Request struct { WorkflowDefinition string `json:"workflow_definition"` // Tools is the tool allowlist: nil allows every tool, an empty list none. Tools []string `json:"tools"` - // Harness, set for a Kit profile, runs every agent stage on the Kit's - // CLI instead of the built-in loop. - Harness *agentrun.HarnessSpec `json:"harness,omitempty"` // Instructions are the instance and organisation layers every agent // stage follows. Instructions workflow.Instructions `json:"instructions"` diff --git a/internal/webui/api_setup.go b/internal/webui/api_setup.go index a662287f3..f3e0f091e 100644 --- a/internal/webui/api_setup.go +++ b/internal/webui/api_setup.go @@ -32,8 +32,7 @@ type ModelReader interface { // The control-plane resources the effective-model read layers: the org's own // providers and the credential bindings that carry their keys. Spelled here -// rather than imported so the dashboard package stays a leaf, as -// api_harness.go does for "credential-bindings". +// rather than imported so the dashboard package stays a leaf. const ( providerSettingsKind = "provider-settings" credentialBindingsKind = "credential-bindings" @@ -132,7 +131,6 @@ func (s *Server) callerEffectiveModels(ctx context.Context) ([]effectiveModel, e if s.ControlPlane == nil || s.CheckSecret == nil || len(models) == 0 { return models, nil } - orgID := string(org.OrgFromContext(ctx)) var providers map[string]providerDoc if err := s.queryResourceValue(ctx, providerSettingsKind, &providers); err != nil { return nil, err @@ -145,7 +143,7 @@ func (s *Server) callerEffectiveModels(ctx context.Context) ([]effectiveModel, e return nil, err } for i := range models { - models[i].Credential, models[i].Detail = s.providerCredential(ctx, orgID, models[i].Provider, providers, bindings) + models[i].Credential, models[i].Detail = s.providerCredential(ctx, models[i].Provider, providers, bindings) } return models, nil } @@ -164,13 +162,12 @@ func (s *Server) queryResourceValue(ctx context.Context, kind string, into any) } // providerCredential reports whether provider's credential is present and -// resolves for orgID, and names what is missing when it does not. The key -// comes from the provider's own api_key_ref, or from the org's credential -// binding named after the provider, or from its api_key_env; a binding with no -// secret is OAuth, whose credential is the captured token set. A provider +// resolves, and names what is missing when it does not. The key comes from +// the provider's own api_key_ref, or from the org's credential binding named +// after the provider, or from its api_key_env. A provider // that is not the org's own and has no binding is an instance provider: its // credential is the instance admin's, not this org's to configure. -func (s *Server) providerCredential(ctx context.Context, orgID, provider string, providers map[string]providerDoc, bindings []credentialBindingDoc) (bool, string) { +func (s *Server) providerCredential(ctx context.Context, provider string, providers map[string]providerDoc, bindings []credentialBindingDoc) (bool, string) { doc, own := providers[provider] if own && doc.APIKeyRef != (config.SecretRef{}) { return s.secretCredential(ctx, provider, doc.APIKeyRef) @@ -182,13 +179,7 @@ func (s *Server) providerCredential(ctx context.Context, orgID, provider string, if binding.Secret != (config.SecretRef{}) { return s.secretCredential(ctx, provider, binding.Secret) } - if s.HarnessSecrets != nil { - if token, err := s.HarnessSecrets.GetHarnessSecret(ctx, orgID, provider); err == nil && token.Validate() == nil { - return true, "" - } - return false, fmt.Sprintf("provider %q has no captured sign-in for credential binding %q", provider, provider) - } - return false, fmt.Sprintf("credential binding %q names no secret and no sign-in is captured", provider) + return false, fmt.Sprintf("credential binding %q names no secret", provider) } if own { if doc.APIKeyEnv != "" { diff --git a/internal/webui/api_setup_test.go b/internal/webui/api_setup_test.go index 511c18e9c..0bde4b07b 100644 --- a/internal/webui/api_setup_test.go +++ b/internal/webui/api_setup_test.go @@ -11,7 +11,6 @@ import ( "google.golang.org/grpc" controlpb "github.com/samcharles93/archie-core/internal/contracts/controlplane/v1" - "github.com/samcharles93/archie-core/internal/domain/harnesssecret" "github.com/samcharles93/archie-core/internal/domain/storecontract" "github.com/samcharles93/archie-core/internal/secret" ) @@ -54,18 +53,6 @@ func (c setupControl) Query(_ context.Context, r *controlpb.QueryRequest, _ ...g return &controlpb.QueryResponse{Resource: &controlpb.Resource{Kind: r.Kind, ValueJson: []byte(value)}}, nil } -type setupHarnessSecrets struct { - storecontract.HarnessSecretStore - tokens map[string]harnesssecret.Secret -} - -func (h setupHarnessSecrets) GetHarnessSecret(_ context.Context, _, service string) (harnesssecret.Secret, error) { - if token, ok := h.tokens[service]; ok { - return token, nil - } - return harnesssecret.Secret{}, storecontract.ErrHarnessSecretNotFound -} - func setupRequest(t *testing.T, srv *Server) []SetupStep { t.Helper() req := httptest.NewRequestWithContext(t.Context(), http.MethodGet, "/api/setup", nil) @@ -101,7 +88,6 @@ func TestHandleSetupModelStep(t *testing.T) { aliases map[string]string providers string bindings string - tokens map[string]harnesssecret.Secret check func(context.Context, secret.SecretRef) (bool, error) open bool detail string @@ -168,23 +154,13 @@ func TestHandleSetupModelStep(t *testing.T) { open: false, }, { - name: "oauth binding with a captured token set completes the step", + name: "binding without a secret keeps the step open", aliases: map[string]string{"default": "acme/gpt-5"}, providers: `{"acme":{"class":"openai"}}`, bindings: `[{"service":"acme"}]`, - tokens: map[string]harnesssecret.Secret{"acme": {Org: "org", Service: "acme", AccessToken: "token"}}, - check: resolving, - open: false, - }, - { - name: "oauth binding without a captured token set keeps the step open", - aliases: map[string]string{"default": "acme/gpt-5"}, - providers: `{"acme":{"class":"openai"}}`, - bindings: `[{"service":"acme"}]`, - tokens: map[string]harnesssecret.Secret{}, check: resolving, open: true, - detail: `provider "acme" has no captured sign-in for credential binding "acme"`, + detail: `credential binding "acme" names no secret`, }, { name: "api_key_env resolves through the env engine", @@ -206,9 +182,6 @@ func TestHandleSetupModelStep(t *testing.T) { srv.ControlPlane = setupControl{providers: tt.providers, bindings: tt.bindings} } srv.CheckSecret = tt.check - if tt.tokens != nil { - srv.HarnessSecrets = setupHarnessSecrets{tokens: tt.tokens} - } step := modelStep(setupRequest(t, srv)) if !tt.open { if step != nil { diff --git a/internal/webui/authorization_surfaces_test.go b/internal/webui/authorization_surfaces_test.go index 5f94a80ea..9fea2db20 100644 --- a/internal/webui/authorization_surfaces_test.go +++ b/internal/webui/authorization_surfaces_test.go @@ -58,8 +58,6 @@ func TestHTTPPolicyCannotBeBypassed(t *testing.T) { {"GET", "/api/event-types"}, {"GET", "/api/extensions"}, {"GET", "/api/extensions/catalogue"}, - {"GET", "/api/harness/bindings"}, - {"GET", "/api/harness/terminal"}, {"GET", "/api/identities"}, {"GET", "/api/logs"}, {"GET", "/api/repositories"}, @@ -85,7 +83,6 @@ func TestHTTPPolicyCannotBeBypassed(t *testing.T) { {"GET", "/api/tasks/7/dispatches"}, {"GET", "/api/tasks/7/logs"}, {"GET", "/api/tasks/7/logs/download"}, - {"GET", "/api/tasks/7/sandbox"}, {"GET", "/api/tasks/7/transcript"}, {"GET", "/api/tokens"}, {"GET", "/api/usage"}, diff --git a/internal/webui/authorize.go b/internal/webui/authorize.go index 6f9c92cb7..74d0cc116 100644 --- a/internal/webui/authorize.go +++ b/internal/webui/authorize.go @@ -307,7 +307,7 @@ func routeOverride(r *http.Request, action access.Action, path string) (access.A return command, access.KindBinding, segmentValue(path, 2), true } switch { - case matchSegment(path, "/api/tasks/", "/logs"), matchSegment(path, "/api/tasks/", "/sandbox"): + case matchSegment(path, "/api/tasks/", "/logs"): return access.ActionReadLogs, access.KindTask, segmentValue(path, 2), true case matchPrefix(path, "/api/access"): return accessRoute(action, path) @@ -331,8 +331,6 @@ func routeOverride(r *http.Request, action access.Action, path string) (access.A case matchSegment(path, "/api/sources/", "/secret"): // Rotating a source's secret hands back its value. return access.ActionReadSecret, access.KindSecret, segmentValue(path, 2), true - case matchPrefix(path, "/api/harness/terminal"), matchPrefix(path, "/api/harness/bindings"): - return harnessSecretRoute(path) case matchPrefix(path, "/api/channels"), matchPrefix(path, "/api/config"), matchPrefix(path, "/api/logs"), matchPrefix(path, "/api/tokens"): return dashboardRoute(action, path) @@ -340,15 +338,6 @@ func routeOverride(r *http.Request, action access.Action, path string) (access.A return action, "", "", false } -// harnessSecretRoute maps the harness surfaces, which work with the binding's -// secret: opening the setup terminal updates it, the bindings view reads it. -func harnessSecretRoute(path string) (access.Action, access.ResourceKind, string, bool) { - if matchPrefix(path, "/api/harness/terminal") { - return access.ActionUpdate, access.KindSecret, "", true - } - return access.ActionRead, access.KindSecret, "", true -} - // dashboardRoute maps the dashboard's own routes: channel settings are // instance administration, while config, logs and a person's own tokens act // only as that person, needing no more than a dashboard read. diff --git a/internal/webui/harness.go b/internal/webui/harness.go deleted file mode 100644 index a4f4fe062..000000000 --- a/internal/webui/harness.go +++ /dev/null @@ -1,259 +0,0 @@ -package webui - -import ( - "context" - "encoding/json" - "errors" - "fmt" - "io" - "log/slog" - "net/http" - "net/url" - "sort" - "strconv" - "strings" - "time" - - "golang.org/x/net/websocket" - - controlpb "github.com/samcharles93/archie-core/internal/contracts/controlplane/v1" - "github.com/samcharles93/archie-core/internal/domain/org" - "github.com/samcharles93/archie-core/internal/domain/storecontract" -) - -// The control-plane resources the harness page reads. The kinds are spelled -// here rather than imported from internal/infrastructure/controlplanestore so the dashboard -// package stays a leaf: api_workflow_enablement.go does the same for -// "workflow-enablement". -const ( - harnessBindingsKind = "credential-bindings" - harnessProfilesKind = "agent-profiles" -) - -// harnessBindingDoc is the part of one credential-bindings entry -// (controlplane.credentialBinding) this page reads: the service it binds. -// The secret reference is decoded too so a malformed document is rejected -// rather than silently read as service-only, but it is never resolved here. -type harnessBindingDoc struct { - Service string `json:"service"` - Org string `json:"org,omitempty"` - Secret struct { - Engine string `json:"engine,omitempty"` - Key string `json:"key,omitempty"` - } `json:"secret"` -} - -// harnessProfileDoc is the part of one agent-profiles entry -// (controlplane.agentProfile) this page reads: the Kit a setup terminal can -// be opened for. -type harnessProfileDoc struct { - Kit string `json:"kit"` -} - -// HarnessTerminal opens a setup session to a Kit profile's container PTY at -// the requested terminal size, in character cells. Nil answers 503. -type HarnessTerminal interface { - Open(ctx context.Context, orgID, profile string, rows, cols int) (io.ReadWriteCloser, error) -} - -// Terminal geometry: the dashboard's fallback when a size is absent or out of -// range, and the largest it will ask for. -const ( - terminalDefaultRows = 24 - terminalDefaultCols = 80 - terminalMaxCells = 1000 -) - -// terminalSize reads a positive row or column count from the query, falling -// back to def when it is absent or out of range rather than failing the open. -func terminalSize(query url.Values, key string, def int) int { - n, err := strconv.Atoi(query.Get(key)) - if err != nil || n < 1 || n > terminalMaxCells { - return def - } - return n -} - -// harnessBindingView is one credential binding as the page renders it: the -// service, whether an OAuth token set has been captured, and that set's -// expiry, scopes and capture time. Access and refresh tokens are never in -// the shape -- the page reports capture state, not credentials. -type harnessBindingView struct { - Service string `json:"service"` - Captured bool `json:"captured"` - ExpiresAt *time.Time `json:"expires_at,omitempty"` - UpdatedAt *time.Time `json:"updated_at,omitempty"` - Scopes []string `json:"scopes,omitempty"` -} - -// harnessView is the page's whole read: the bindings, the Kit profiles a -// setup terminal can target, and whether this process can open one. -type harnessView struct { - Terminal bool `json:"terminal"` - Bindings []harnessBindingView `json:"bindings"` - Profiles []string `json:"profiles"` -} - -// harnessAPI owns the /api/harness routes. Each dependency is optional and a -// handler answers 503 when the one it needs is absent. -type harnessAPI struct { - controlPlane controlpb.ControlPlaneServiceClient - secrets storecontract.HarnessSecretStore - terminal HarnessTerminal - log *slog.Logger -} - -func newHarnessAPI(s *Server) *harnessAPI { - return &harnessAPI{controlPlane: s.ControlPlane, secrets: s.HarnessSecrets, terminal: s.HarnessTerminal, log: s.Log} -} - -func (a *harnessAPI) logf(msg string, args ...any) { warnf(a.log, msg, args...) } - -func (a *harnessAPI) register(mux *http.ServeMux) { - mux.HandleFunc("GET /api/harness/bindings", a.handleHarnessBindings) - mux.HandleFunc("GET /api/harness/terminal", a.handleHarnessTerminal) -} - -// handleHarnessBindings lists the org's credential bindings joined with -// their captured OAuth state. It requires both contracts the join reads: an -// unconfigured process answers 503 rather than rendering a page that claims -// the org has no bindings, which it cannot know. -func (a *harnessAPI) handleHarnessBindings(w http.ResponseWriter, r *http.Request) { - if a.controlPlane == nil || a.secrets == nil { - http.Error(w, "harness bindings not configured", http.StatusServiceUnavailable) - return - } - orgID := string(org.OrgFromContext(r.Context())) - bindings, err := a.harnessBindings(r.Context(), orgID) - if err != nil { - a.logf("list harness bindings", "err", err) - http.Error(w, "list harness bindings failed", http.StatusBadGateway) - return - } - profiles, err := a.harnessProfiles(r.Context()) - if err != nil { - a.logf("list harness profiles", "err", err) - http.Error(w, "list harness bindings failed", http.StatusBadGateway) - return - } - writeJSON(w, harnessView{Terminal: a.terminal != nil, Bindings: bindings, Profiles: profiles}) -} - -// harnessBindings reads the credential-bindings document and each service's -// captured token state. A service with no captured set is not an error: it -// is exactly the state the setup terminal exists to change. -func (a *harnessAPI) harnessBindings(ctx context.Context, orgID string) ([]harnessBindingView, error) { - response, err := a.controlPlane.Query(ctx, &controlpb.QueryRequest{Kind: harnessBindingsKind}) - if err != nil { - return nil, err - } - var docs []harnessBindingDoc - if response.Resource != nil && len(response.Resource.ValueJson) > 0 { - if err := json.Unmarshal(response.Resource.ValueJson, &docs); err != nil { - return nil, err - } - } - views := make([]harnessBindingView, 0, len(docs)) - for _, doc := range docs { - if strings.TrimSpace(doc.Service) == "" { - continue - } - view := harnessBindingView{Service: doc.Service} - secret, err := a.secrets.GetHarnessSecret(ctx, orgID, doc.Service) - switch { - case err == nil: - view.Captured = true - if !secret.ExpiresAt.IsZero() { - expires := secret.ExpiresAt - view.ExpiresAt = &expires - } - if !secret.UpdatedAt.IsZero() { - updated := secret.UpdatedAt - view.UpdatedAt = &updated - } - view.Scopes = secret.Scopes - case errors.Is(err, storecontract.ErrHarnessSecretNotFound): - // Not captured yet: the page shows the binding as unconfigured. - default: - return nil, err - } - views = append(views, view) - } - return views, nil -} - -// harnessProfiles returns the agent profiles that name a Kit, sorted so the -// page's list does not depend on Go's map iteration order. -func (a *harnessAPI) harnessProfiles(ctx context.Context) ([]string, error) { - response, err := a.controlPlane.Query(ctx, &controlpb.QueryRequest{Kind: harnessProfilesKind}) - if err != nil { - return nil, err - } - var docs map[string]harnessProfileDoc - if response.Resource != nil && len(response.Resource.ValueJson) > 0 { - if err := json.Unmarshal(response.Resource.ValueJson, &docs); err != nil { - return nil, err - } - } - profiles := make([]string, 0, len(docs)) - for name, doc := range docs { - if strings.TrimSpace(doc.Kit) != "" { - profiles = append(profiles, name) - } - } - sort.Strings(profiles) - return profiles, nil -} - -// handleHarnessTerminal upgrades the request to a WebSocket and bridges it to -// a setup session opened for the named Kit profile. Opening the terminal is -// the update action on the secret, decided by the authorizer before this -// handler runs (authorize.go's route override). -func (a *harnessAPI) handleHarnessTerminal(w http.ResponseWriter, r *http.Request) { - if a.terminal == nil { - http.Error(w, "setup terminal not configured", http.StatusServiceUnavailable) - return - } - if !strings.EqualFold(r.Header.Get("Upgrade"), "websocket") { - http.Error(w, "websocket upgrade required", http.StatusUpgradeRequired) - return - } - profile := strings.TrimSpace(r.URL.Query().Get("profile")) - if profile == "" { - http.Error(w, "profile is required", http.StatusBadRequest) - return - } - rows := terminalSize(r.URL.Query(), "rows", terminalDefaultRows) - cols := terminalSize(r.URL.Query(), "cols", terminalDefaultCols) - session, err := a.terminal.Open(r.Context(), string(org.OrgFromContext(r.Context())), profile, rows, cols) - if err != nil { - a.logf("open setup terminal", "err", err, "profile", profile) - http.Error(w, "setup terminal failed", http.StatusBadGateway) - return - } - defer func() { _ = session.Close() }() - websocket.Handler(func(ws *websocket.Conn) { - defer func() { _ = ws.Close() }() - bridgeTerminal(ws, session) - }).ServeHTTP(w, r) -} - -// bridgeTerminal pumps bytes between the dashboard WebSocket and the setup -// session until either side closes, then closes both so the surviving copy -// unblocks. It is a free function so a test can drive it without a listener. -// A session failure is written to the socket first: the terminal would -// otherwise only close, leaving the operator no reason. -func bridgeTerminal(ws *websocket.Conn, session io.ReadWriteCloser) { - done := make(chan struct{}, 2) - go func() { _, _ = io.Copy(session, ws); done <- struct{}{} }() - go func() { - if _, err := io.Copy(ws, session); err != nil { - _, _ = fmt.Fprintf(ws, "\r\n\x1b[31m%s\x1b[0m\r\n", err) - } - done <- struct{}{} - }() - <-done - _ = ws.Close() - _ = session.Close() - <-done -} diff --git a/internal/webui/server.go b/internal/webui/server.go index e51a70750..3ac5eced5 100644 --- a/internal/webui/server.go +++ b/internal/webui/server.go @@ -216,20 +216,6 @@ type Server struct { // /api/sources route answer 503 and marks no binding unsigned. Sources storecontract.SourceStore - // HarnessSecrets reports each credential binding's OAuth token status, never - // the values. Nil answers 503. - HarnessSecrets storecontract.HarnessSecretStore - - // HarnessTerminal opens the setup terminal, a duplex PTY session in an - // ephemeral Kit container. Optional: nil answers /api/harness/terminal 503. - // The implementation belongs to a process that owns containers; the - // dashboard consumes it over a contract and never links one. - HarnessTerminal HarnessTerminal - - // SandboxView follows a running task's sandbox output, read-only. - // Optional: nil answers /api/tasks/{id}/sandbox 503. - SandboxView SandboxView - // TelegramUpdateReportPath and TelegramUpdateChatID route a dashboard // update's post-restart report to Telegram. Either empty uses // UpdateReportPath. @@ -333,10 +319,6 @@ func (s *Server) registerMappingAndBindingRoutes(mux *http.ServeMux) { } } -func (s *Server) registerHarnessRoutes(mux *http.ServeMux) { - newHarnessAPI(s).register(mux) -} - func (s *Server) registerConfigAndLogRoutes(mux *http.ServeMux) { newExtensionsAPI(s).register(mux) mux.HandleFunc("GET /api/control-plane/apply-status", instanceOnly(s.handleApplyStatus)) @@ -362,7 +344,6 @@ func (s *Server) Handler() http.Handler { s.registerCoreRoutes(mux) s.registerTaskRoutes(mux) s.registerMappingAndBindingRoutes(mux) - s.registerHarnessRoutes(mux) s.registerConfigAndLogRoutes(mux) s.registerChatRoutes(mux) diff --git a/internal/webui/tasks.go b/internal/webui/tasks.go index 399c4d3c0..a17fa0ce5 100644 --- a/internal/webui/tasks.go +++ b/internal/webui/tasks.go @@ -10,13 +10,12 @@ import ( ) // tasksAPI owns the /api/tasks routes and /api/task-meta. Optional -// dependencies (steps, taskLogs, sandbox, chat) stay nil-safe: each handler +// dependencies (steps, taskLogs, chat) stay nil-safe: each handler // reports the feature unavailable rather than the module being absent. type tasksAPI struct { store storecontract.TaskStore steps storecontract.StepReader taskLogs TaskLogSource - sandbox SandboxView chat *ChatService log *slog.Logger @@ -31,7 +30,6 @@ func newTasksAPI(s *Server) *tasksAPI { store: s.Store, steps: s.Steps, taskLogs: s.TaskLogs, - sandbox: s.SandboxView, chat: s.Chat, log: s.Log, authorize: s.authorizeTaskMutation, @@ -61,6 +59,5 @@ func (a *tasksAPI) register(mux *http.ServeMux) { mux.HandleFunc("GET /api/tasks/{id}/debug", a.handleTaskDebug) mux.HandleFunc("GET /api/tasks/{id}/logs", a.handleTaskLogs) mux.HandleFunc("GET /api/tasks/{id}/transcript", a.handleTaskTranscript) - mux.HandleFunc("GET /api/tasks/{id}/sandbox", a.handleTaskSandbox) mux.HandleFunc("GET /api/tasks/{id}/logs/download", a.handleTaskLogDownload) } diff --git a/internal/webui/tasks_sandbox.go b/internal/webui/tasks_sandbox.go deleted file mode 100644 index 361f1e16d..000000000 --- a/internal/webui/tasks_sandbox.go +++ /dev/null @@ -1,71 +0,0 @@ -package webui - -import ( - "context" - "io" - "net/http" - "strings" - - "golang.org/x/net/websocket" - "google.golang.org/grpc/status" - - "github.com/samcharles93/archie-core/internal/domain/org" -) - -// SandboxView follows a running task attempt's sandbox output, read-only. A -// finished or unknown attempt is refused with a gRPC status whose message -// states why. The implementation belongs to a process that owns containers; -// the dashboard consumes it over a contract and never links one. Nil answers -// 503. -type SandboxView interface { - OpenRun(ctx context.Context, orgID string, taskID, attempt int64) (io.ReadCloser, error) -} - -// handleTaskSandbox streams a running attempt's sandbox output to a WebSocket. -// Reading it is the same authority as reading the task's log, decided before -// this handler runs. Input from the browser is discarded: a view has no stdin. -func (a *tasksAPI) handleTaskSandbox(w http.ResponseWriter, r *http.Request) { - if a.sandbox == nil { - http.Error(w, "sandbox view not configured", http.StatusServiceUnavailable) - return - } - if !strings.EqualFold(r.Header.Get("Upgrade"), "websocket") { - http.Error(w, "websocket upgrade required", http.StatusUpgradeRequired) - return - } - t, attempt, ok := a.taskAttemptTarget(w, r) - if !ok { - return - } - // The refusal rides the socket as text, not as an HTTP status: a browser - // WebSocket cannot read a failed upgrade's body, and the operator's only - // honest account of why there is no view is the daemon's stated reason. - //nolint:contextcheck // websocket.Handler has no context parameter; the handler derives its context from ws.Request() - websocket.Handler(func(ws *websocket.Conn) { - defer func() { _ = ws.Close() }() - ctx, cancel := context.WithCancel(ws.Request().Context()) - defer cancel() - go func() { _, _ = io.Copy(io.Discard, ws); cancel() }() - view, err := a.sandbox.OpenRun(ctx, string(org.OrgFromContext(ws.Request().Context())), t.ID, int64(attempt)) - if err != nil { - a.logf("open sandbox view", "err", err, "task", t.ID, "attempt", attempt) - _, _ = io.WriteString(ws, sandboxRefusal(err)) - return - } - defer func() { _ = view.Close() }() - stopClose := context.AfterFunc(ctx, func() { _ = view.Close() }) - defer stopClose() - ws.PayloadType = websocket.BinaryFrame - if _, err := io.Copy(ws, view); err != nil && ctx.Err() == nil { - _ = websocket.Message.Send(ws, sandboxRefusal(err)) - } - }).ServeHTTP(w, r) -} - -// sandboxRefusal is the reason shown to the operator. -func sandboxRefusal(err error) string { - if st, ok := status.FromError(err); ok && st.Message() != "" { - return st.Message() - } - return "the sandbox view is unavailable" -} diff --git a/internal/webui/tasks_sandbox_test.go b/internal/webui/tasks_sandbox_test.go deleted file mode 100644 index 20c79472e..000000000 --- a/internal/webui/tasks_sandbox_test.go +++ /dev/null @@ -1,87 +0,0 @@ -package webui - -import ( - "context" - "io" - "net/http" - "net/http/httptest" - "strings" - "sync" - "testing" - "time" - - "golang.org/x/net/websocket" - "google.golang.org/grpc/codes" - "google.golang.org/grpc/status" - - storecontract "github.com/samcharles93/archie-core/internal/domain/storecontract" - "github.com/samcharles93/archie-core/internal/domain/workflow" -) - -type sandboxTasks struct{ storecontract.TaskStore } - -func (sandboxTasks) TaskByID(context.Context, int64) (*workflow.Task, error) { - return &workflow.Task{ID: 7, Attempt: 2, Status: workflow.StatusRunning}, nil -} - -type sandboxStream struct { - *io.PipeReader - closed chan struct{} - once sync.Once -} - -func (s *sandboxStream) Close() error { - s.once.Do(func() { close(s.closed) }) - return s.PipeReader.Close() -} - -func (s *sandboxStream) OpenRun(context.Context, string, int64, int64) (io.ReadCloser, error) { - return s, nil -} - -func TestSandboxStreamLifetime(t *testing.T) { - for _, name := range []string{"browser leaves quiet view", "output fails"} { - t.Run(name, func(t *testing.T) { - pr, pw := io.Pipe() - view := &sandboxStream{PipeReader: pr, closed: make(chan struct{})} - s := &Server{Store: sandboxTasks{}, SandboxView: view} - a := newTasksAPI(s) - mux := http.NewServeMux() - mux.HandleFunc("GET /api/tasks/{id}/sandbox", a.handleTaskSandbox) - server := httptest.NewServer(mux) - defer server.Close() - defer view.Close() - defer pw.Close() - ws, err := websocket.Dial("ws"+strings.TrimPrefix(server.URL, "http")+"/api/tasks/7/sandbox?attempt=2", "", server.URL) - if err != nil { - t.Fatal(err) - } - defer ws.Close() - if err := ws.SetDeadline(time.Now().Add(2 * time.Second)); err != nil { - t.Fatal(err) - } - go func() { - _, _ = io.WriteString(pw, "ready") - if name == "output fails" { - _ = pw.CloseWithError(status.Error(codes.Unavailable, "lost sandbox output")) - } - }() - var message string - if err := websocket.Message.Receive(ws, &message); err != nil || message != "ready" { - t.Fatalf("initial output = %q, %v", message, err) - } - if name == "browser leaves quiet view" { - _ = ws.Close() - } else { - if err := websocket.Message.Receive(ws, &message); err != nil || !strings.Contains(message, "lost sandbox output") { - t.Fatalf("failure reason = %q, %v", message, err) - } - } - select { - case <-view.closed: - case <-time.After(time.Second): - t.Fatal("sandbox stream remained open") - } - }) - } -} diff --git a/proto/buf.yaml b/proto/buf.yaml index a98632b25..d018f12b1 100644 --- a/proto/buf.yaml +++ b/proto/buf.yaml @@ -7,6 +7,13 @@ lint: breaking: use: - PACKAGE + # One-off: the external-harness contracts were deleted with Kit support + # (harness.v1, state.v1 HarnessSecretService, gateway.v1 TaskToolsService). + # A package or service deletion cannot be path-ignored. Drop this once main + # carries the deletion. + except: + - PACKAGE_NO_DELETE + - PACKAGE_SERVICE_NO_DELETE # Two intentional wire breaks, both widening an id-shaped field from int64 to # string in place, on the same field numbers and names. The PocketBase # migration moved the event-capture contract's record ids to PocketBase's @@ -61,6 +68,8 @@ breaking: PACKAGE_MESSAGE_NO_DELETE: - controlplane/v1/controlplane.proto - forge/v1/forge.proto + - gateway/v1/task_tools.proto + - state/v1/harness_secret.proto FIELD_NO_DELETE: - state/v1 - controlplane/v1/controlplane.proto diff --git a/proto/gateway/v1/task_tools.proto b/proto/gateway/v1/task_tools.proto deleted file mode 100644 index a8dbf2f98..000000000 --- a/proto/gateway/v1/task_tools.proto +++ /dev/null @@ -1,20 +0,0 @@ -syntax = "proto3"; -package gateway.v1; - -// TaskToolsService executes central tools for a live task grant. Only the -// daemon calls it; identities, orgs and permissions are resolved by the server. -service TaskToolsService { - rpc ListTaskTools(ListTaskToolsRequest) returns (ListTaskToolsResponse); - rpc CallTaskTool(CallTaskToolRequest) returns (CallTaskToolResponse); - rpc CloseTaskTools(CloseTaskToolsRequest) returns (CloseTaskToolsResponse); -} -message ListTaskToolsRequest { string run_credential = 1; } -message ListTaskToolsResponse { bytes tools_json = 1; } -message CallTaskToolRequest { - string run_credential = 1; - string name = 2; - bytes arguments_json = 3; -} -message CallTaskToolResponse { bytes result_json = 1; } -message CloseTaskToolsRequest { string run_credential = 1; } -message CloseTaskToolsResponse {} diff --git a/proto/harness/v1/harness.proto b/proto/harness/v1/harness.proto deleted file mode 100644 index 35c178e89..000000000 --- a/proto/harness/v1/harness.proto +++ /dev/null @@ -1,72 +0,0 @@ -syntax = "proto3"; - -package harness.v1; - -// HarnessService opens one duplex PTY session into a Kit container the daemon -// owns. The dashboard's setup terminal consumes the setup target; the -// run-scoped live view consumes the run target and is read-only. See -// docs/prds/external-agent-harness.md, "Session contract". -service HarnessService { - // Open carries a session: the first request frame names the target, later - // frames carry terminal input and sizing, and the response stream carries - // terminal output until the command exits or the session ends. - rpc Open(stream OpenRequest) returns (stream OpenResponse); -} - -// OpenRequest is one client frame. The first frame must be target; a frame -// with no field set is a keepalive the server ignores. -message OpenRequest { - oneof frame { - // target names the session and must be the first frame. - OpenTarget target = 1; - // stdin is terminal input bytes. - bytes stdin = 2; - } - // size is the terminal size the client wants, in character cells. The - // dashboard sends it with the target frame so the PTY is created at the - // right size; the daemon falls back to its own default when it is absent. - ConsoleSize size = 3; -} - -// OpenResponse is one server frame. -message OpenResponse { - oneof frame { - // stdout is terminal output bytes. - bytes stdout = 1; - // exit_code ends the session: the command's exit status. - int32 exit_code = 2; - // error ends the session when it could not be opened or was aborted. - string error = 3; - } -} - -// OpenTarget addresses one session. A setup session is addressed by Kit -// profile and is ephemeral; a run session is addressed by task and attempt and -// is bounded by the task. -message OpenTarget { - string org = 1; - oneof target { - SetupTarget setup = 2; - RunTarget run = 3; - } -} - -// SetupTarget runs the CLI's own login in an ephemeral container built from -// the named agent profile's Kit. -message SetupTarget { - string profile = 1; -} - -// RunTarget is a running task's sandbox. The daemon follows its output -// read-only and ignores stdin; a finished or unknown task or attempt is -// NotFound, with the reason in the status message. -message RunTarget { - int64 task_id = 1; - int64 attempt = 2; -} - -// ConsoleSize is a terminal size in character cells. -message ConsoleSize { - uint32 rows = 1; - uint32 cols = 2; -} diff --git a/proto/state/v1/harness_secret.proto b/proto/state/v1/harness_secret.proto deleted file mode 100644 index 440cfca44..000000000 --- a/proto/state/v1/harness_secret.proto +++ /dev/null @@ -1,44 +0,0 @@ -syntax = "proto3"; - -package state.v1; - -import "google/protobuf/timestamp.proto"; - -// Harness OAuth secrets (docs/prds/external-agent-harness.md, -// Credentials). Administrative: the setup terminal (an `update` action on -// the secret, Authorizer-gated) and the egress proxy's token-endpoint -// interception (acting as the daemon, not a worker) are the only callers. -service HarnessSecretService { - rpc GetHarnessSecret(GetHarnessSecretRequest) returns (GetHarnessSecretResponse); - rpc PutHarnessSecret(PutHarnessSecretRequest) returns (PutHarnessSecretResponse); -} - -// HarnessSecret mirrors internal/domain/harnesssecret.Secret -- one org's -// captured OAuth token set for a credential binding's service -// (docs/prds/external-agent-harness.md, Credentials). -message HarnessSecret { - reserved 1; - reserved "org"; - string service = 2; - string access_token = 3; - string refresh_token = 4; - string token_type = 5; - google.protobuf.Timestamp expires_at = 6; - google.protobuf.Timestamp updated_at = 7; - repeated string scopes = 8; -} -message GetHarnessSecretRequest { - reserved 1; - reserved "org"; - string service = 2; - string run_credential = 3; -} -message GetHarnessSecretResponse { - HarnessSecret secret = 1; - string org = 2; -} -message PutHarnessSecretRequest { - HarnessSecret secret = 1; - string run_credential = 2; -} -message PutHarnessSecretResponse {} diff --git a/tools/canary-rehearse.sh b/tools/canary-rehearse.sh index 33f62b9a0..72cdf30cc 100755 --- a/tools/canary-rehearse.sh +++ b/tools/canary-rehearse.sh @@ -165,7 +165,7 @@ mkdir -p "$XDG_CONFIG_HOME/archie" "$XDG_DATA_HOME" "$XDG_STATE_HOME" "$ARCHIE_U cfg=$XDG_CONFIG_HOME/archie/config.toml # A throwaway CA and per-service certificates, the way `task dev:tls` issues -# them: archie-ui dials the daemon's harness sessions over mTLS even on +# them: archie-ui dials the daemon contract over mTLS even on # loopback, so the host cannot come up without [tls]. tls=$work/tls mkdir -p "$tls" diff --git a/ui/package.json b/ui/package.json index de58784c1..f62e4ae3c 100644 --- a/ui/package.json +++ b/ui/package.json @@ -19,8 +19,6 @@ "@vue-flow/background": "^1.3.2", "@vue-flow/core": "^1.48.2", "@vueuse/core": "^15.0.0", - "@xterm/addon-fit": "^0.11.0", - "@xterm/xterm": "^6.0.0", "class-variance-authority": "^0.7.1", "clsx": "^2.1.1", "cn": "^0.5.1", diff --git a/ui/pnpm-lock.yaml b/ui/pnpm-lock.yaml index 80891895f..8e86d9494 100644 --- a/ui/pnpm-lock.yaml +++ b/ui/pnpm-lock.yaml @@ -190,12 +190,6 @@ importers: '@vueuse/core': specifier: ^15.0.0 version: 15.0.0(vue@3.5.44(typescript@6.0.3)) - '@xterm/addon-fit': - specifier: ^0.11.0 - version: 0.11.0 - '@xterm/xterm': - specifier: ^6.0.0 - version: 6.0.0 class-variance-authority: specifier: ^0.7.1 version: 0.7.1 @@ -758,12 +752,6 @@ packages: peerDependencies: vue: ^3.5.0 - '@xterm/addon-fit@0.11.0': - resolution: {integrity: sha512-jYcgT6xtVYhnhgxh3QgYDnnNMYTcf8ElbxxFzX0IZo+vabQqSPAjC3c1wJrKB5E19VwQei89QCiZZP86DCPF7g==} - - '@xterm/xterm@6.0.0': - resolution: {integrity: sha512-TQwDdQGtwwDt+2cgKDLn0IRaSxYu1tSUjgKarSDkUM0ZNiSRXFpjxEsvc/Zgc5kq5omJ+V0a8/kIM2WD3sMOYg==} - acorn@8.19.0: resolution: {integrity: sha512-oJlA3XiRm7Cyk6qFD2Jn8ak9B7jSy0qy00ADO3+8dpT0LSjFihQYv4C02LFCSYJV3Q37xYwwRy5m+IpIiUzqWw==} engines: {node: '>=0.4.0'} @@ -1893,10 +1881,6 @@ snapshots: dependencies: vue: 3.5.44(typescript@6.0.3) - '@xterm/addon-fit@0.11.0': {} - - '@xterm/xterm@6.0.0': {} - acorn@8.19.0: {} alien-signals@3.2.1: {} diff --git a/ui/src/harness/SetupTerminal.vue b/ui/src/harness/SetupTerminal.vue deleted file mode 100644 index a7c00d9df..000000000 --- a/ui/src/harness/SetupTerminal.vue +++ /dev/null @@ -1,127 +0,0 @@ - - - diff --git a/ui/src/harness/harness.ts b/ui/src/harness/harness.ts deleted file mode 100644 index 160d89e7d..000000000 --- a/ui/src/harness/harness.ts +++ /dev/null @@ -1,74 +0,0 @@ -/** - * The harness page's wire shapes and payload building, kept in a plain - * module beside the components so it runs under the dashboard's module test - * runner (docs/development/frontend-ui.md, "Testing the dashboard"). - * - * The setup terminal is a WebSocket because the browser cannot set headers on - * a terminal connection: the shared-token gate reads the same HttpOnly cookie - * the SSE stream uses. - */ - -/** One credential binding as GET /api/harness/bindings reports it. */ -export interface HarnessBinding { - service: string; - captured: boolean; - /** RFC 3339; absent when no token set has been captured. */ - expires_at?: string; - updated_at?: string; - scopes?: string[]; -} - -/** The page's whole read. */ -export interface HarnessState { - /** Whether this process can open a setup terminal at all. */ - terminal: boolean; - bindings: HarnessBinding[]; - /** The agent profiles that name a Kit, i.e. what a terminal can target. */ - profiles: string[]; -} - -/** How a captured token reads on the page. */ -export type BindingStatus = "unconfigured" | "expired" | "expiring" | "ready"; - -/** A captured token this close to expiry reads as "expiring". */ -export const EXPIRING_WINDOW_MS = 30 * 60 * 1000; - -/** - * bindingStatus classifies a binding's capture state. A binding with no - * captured set is "unconfigured" whatever else it carries; a captured set - * with no parseable expiry counts as ready, because the provider did not tell - * archie when it lapses and guessing would be wrong. - */ -export function bindingStatus( - binding: HarnessBinding, - now: number = Date.now(), -): BindingStatus { - if (!binding.captured) return "unconfigured"; - if (!binding.expires_at) return "ready"; - const expires = Date.parse(binding.expires_at); - if (Number.isNaN(expires)) return "ready"; - if (expires <= now) return "expired"; - if (expires - now <= EXPIRING_WINDOW_MS) return "expiring"; - return "ready"; -} - -/** - * setupTerminalURL builds the WebSocket URL for one Kit profile, carrying the - * terminal size the browser measured so the remote PTY is created with it. - * The location is a parameter rather than window so the module stays runnable - * outside a browser. - */ -export function setupTerminalURL( - profile: string, - location: Pick, - cols = 80, - rows = 24, -): string { - const scheme = location.protocol === "https:" ? "wss:" : "ws:"; - const query = new URLSearchParams({ - profile, - cols: String(cols), - rows: String(rows), - }).toString(); - return `${scheme}//${location.host}/api/harness/terminal?${query}`; -} diff --git a/ui/src/lib/api.ts b/ui/src/lib/api.ts index 456e35114..0bde0ba3f 100644 --- a/ui/src/lib/api.ts +++ b/ui/src/lib/api.ts @@ -416,7 +416,6 @@ export const api = { request(`/api/bindings/${encodeURIComponent(id)}/pause`, { method: "POST" }), bindingResume: (id: string) => request(`/api/bindings/${encodeURIComponent(id)}/resume`, { method: "POST" }), - harnessBindings: () => request("/api/harness/bindings"), sources: (workspace?: string) => request("/api/sources" + qs({ workspace })), // sourceCreate makes a source in one of the org's workspaces; an empty diff --git a/ui/src/lib/terminalTheme.ts b/ui/src/lib/terminalTheme.ts deleted file mode 100644 index b5f223c23..000000000 --- a/ui/src/lib/terminalTheme.ts +++ /dev/null @@ -1,45 +0,0 @@ -/** - * The xterm palette, read from the --terminal-* theme tokens so a terminal - * follows the dashboard theme instead of hard-coded hex. DOM-bound, unlike - * the pure helpers in format.ts. - */ - -/** Structural subset of xterm's ITheme, so this module need not import @xterm/xterm. */ -export interface TerminalTheme { - background: string; - foreground: string; - cursor: string; - selectionBackground: string; -} - -function readToken(name: string): string { - return getComputedStyle(document.documentElement).getPropertyValue(name).trim(); -} - -/** The current terminal palette from the theme tokens in style.css. */ -export function terminalTheme(): TerminalTheme { - return { - background: readToken("--terminal-bg"), - foreground: readToken("--terminal-fg"), - cursor: readToken("--terminal-cursor"), - selectionBackground: readToken("--terminal-selection"), - }; -} - -/** - * Applies the terminal theme now and again on every theme switch. The - * appearance store rewrites data-theme on — including OS-level - * changes while the preference is "system" — so watching that one attribute - * covers live switching. Returns the stop function. - */ -export function followTerminalTheme(apply: (theme: TerminalTheme) => void): () => void { - apply(terminalTheme()); - const observer = new MutationObserver((mutations) => { - if (mutations.some((m) => m.attributeName === "data-theme")) apply(terminalTheme()); - }); - observer.observe(document.documentElement, { - attributes: true, - attributeFilter: ["data-theme"], - }); - return () => observer.disconnect(); -} diff --git a/ui/src/router/index.ts b/ui/src/router/index.ts index dfe7ba791..102623c7f 100644 --- a/ui/src/router/index.ts +++ b/ui/src/router/index.ts @@ -402,9 +402,6 @@ const routes = [ component: () => import("@/settings/AgentProfilesPage.vue"), meta: { navPath: "/settings", settings: true, label: "Agent profiles", section: "settings" }, }, - // The harness sign-in section lives on the agent-profiles page: its setup - // terminal targets the Kit profiles that page edits. - { path: "/settings/harness", redirect: "/settings/agent-profiles", meta: { nav: false } }, { path: "/settings/history", redirect: (to: RouteLocationGeneric) => ({ path: "/org/audit", query: to.query, hash: to.hash }), meta: { nav: false } }, { path: "/settings/audit", redirect: (to: RouteLocationGeneric) => ({ path: "/org/audit", query: to.query, hash: to.hash }), meta: { nav: false } }, { diff --git a/ui/src/settings/AgentProfilesPage.vue b/ui/src/settings/AgentProfilesPage.vue index 53c2b5c34..bf6f9b563 100644 --- a/ui/src/settings/AgentProfilesPage.vue +++ b/ui/src/settings/AgentProfilesPage.vue @@ -1,30 +1,18 @@ -
- -
- - -
- - Harness bindings are not configured - - -
- -
- -
-
diff --git a/ui/src/style.css b/ui/src/style.css index 801f97de1..476f2b2d3 100644 --- a/ui/src/style.css +++ b/ui/src/style.css @@ -140,8 +140,6 @@ --color-input: var(--input); --color-ring: var(--ring); --color-hairline: var(--hairline); - /* Terminal (xterm) host box; the palette itself is read via getComputedStyle. */ - --color-terminal: var(--terminal-bg); --color-sidebar: var(--card); --color-sidebar-foreground: var(--card-foreground); @@ -239,12 +237,6 @@ --workspace: #16171a; --hairline: #2a2c30; - /* Terminal well: darker than the card so the stream reads as a well. */ - --terminal-bg: #0b0f14; - --terminal-fg: var(--foreground); - --terminal-cursor: var(--foreground); - --terminal-selection: #264f78; - --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.4); --shadow-md: 0 8px 28px rgba(0, 0, 0, 0.4); --shadow-workspace: none; @@ -309,12 +301,6 @@ --workspace: #f5f4f1; --hairline: #d6d3cc; - /* Paper inset: the light-theme terminal is light, not a dark well. */ - --terminal-bg: #f5f4f1; - --terminal-fg: var(--foreground); - --terminal-cursor: var(--foreground); - --terminal-selection: #c9d8e8; - --shadow-sm: 0 1px 2px rgba(31, 31, 29, 0.06); --shadow-md: 0 8px 28px rgba(31, 31, 29, 0.1); --shadow-workspace: none; diff --git a/ui/src/tasks/SandboxView.vue b/ui/src/tasks/SandboxView.vue deleted file mode 100644 index 8273327c4..000000000 --- a/ui/src/tasks/SandboxView.vue +++ /dev/null @@ -1,115 +0,0 @@ - - - diff --git a/ui/src/tasks/TabBar.vue b/ui/src/tasks/TabBar.vue index 6ea753764..462eb8a55 100644 --- a/ui/src/tasks/TabBar.vue +++ b/ui/src/tasks/TabBar.vue @@ -1,10 +1,7 @@ diff --git a/ui/src/tasks/task-run.ts b/ui/src/tasks/task-run.ts index cdfe663a9..b755518d0 100644 --- a/ui/src/tasks/task-run.ts +++ b/ui/src/tasks/task-run.ts @@ -30,7 +30,6 @@ export const RUN_TABS: TaskRunTab[] = [ { id: "log", label: "Log" }, { id: "changes", label: "Changes" }, { id: "debug", label: "Debug" }, - { id: "sandbox", label: "Sandbox" }, ]; const TAB_IDS = new Set(RUN_TABS.map((tab) => tab.id)); diff --git a/ui/test/harness.test.ts b/ui/test/harness.test.ts deleted file mode 100644 index effb98dab..000000000 --- a/ui/test/harness.test.ts +++ /dev/null @@ -1,59 +0,0 @@ -import assert from "node:assert/strict"; -import test from "node:test"; - -import { - bindingStatus, - setupTerminalURL, - EXPIRING_WINDOW_MS, -} from "../src/harness/harness.ts"; - -import type { HarnessBinding } from "../src/harness/harness.ts"; - -const now = Date.parse("2026-10-02T12:00:00Z"); - -function binding(overrides: Partial = {}): HarnessBinding { - return { service: "github", captured: false, ...overrides }; -} - -test("credential status distinguishes uncaptured, ready, expiring and expired bindings", () => { - const cases: Array<[Partial, string]> = [ - [{}, "unconfigured"], - [{ captured: true, expires_at: "2026-10-02T13:00:00Z" }, "ready"], - [{ captured: true, expires_at: new Date(now + EXPIRING_WINDOW_MS - 1000).toISOString() }, "expiring"], - [{ captured: true, expires_at: "2026-10-02T11:00:00Z" }, "expired"], - [{ captured: true }, "ready"], - [{ captured: true, expires_at: "not-a-date" }, "ready"], - ]; - for (const [overrides, expected] of cases) { - assert.equal(bindingStatus(binding(overrides), now), expected, JSON.stringify(overrides)); - } -}); - -test("setup terminals preserve secure transport, encode the profile and carry the size", () => { - const cases: Array<[string, Pick, number, number, string]> = [ - [ - "claude-kit", - { protocol: "http:", host: "127.0.0.1:8484" }, - 80, - 24, - "ws://127.0.0.1:8484/api/harness/terminal?profile=claude-kit&cols=80&rows=24", - ], - [ - "claude kit", - { protocol: "https:", host: "archie.example" }, - 80, - 24, - "wss://archie.example/api/harness/terminal?profile=claude+kit&cols=80&rows=24", - ], - [ - "claude-kit", - { protocol: "https:", host: "archie.example" }, - 120, - 30, - "wss://archie.example/api/harness/terminal?profile=claude-kit&cols=120&rows=30", - ], - ]; - for (const [profile, location, cols, rows, expected] of cases) { - assert.equal(setupTerminalURL(profile, location, cols, rows), expected, profile); - } -}); -- 2.54.0 From 658a4cb50ed1a05c9a04796515d6092783190ce8 Mon Sep 17 00:00:00 2001 From: Sam Catlow Date: Thu, 12 Nov 2026 21:42:17 +1100 Subject: [PATCH 6/7] chore(runtime): remove code left dead by the Kit removal (#1877) Drops Client.TaskPrincipalForCredential and the principal_id field only it read, Reporter.ReportToolCall, Pool.EnsureImage and Pool.Instance, and the binding cipher's exported domain API. Clears the remaining harness references from the audit target kinds, docs and comments. --- CLAUDE.md | 4 +-- internal/container/pool.go | 10 ------ internal/contracts/state/v1/task_grant.pb.go | 18 +++------- internal/domain/agentrun/runner.go | 7 ---- internal/domain/audit/audit.go | 2 +- internal/domain/storepkg/descriptor.go | 2 +- internal/events/events.go | 2 +- .../infrastructure/bindingcipher/cipher.go | 33 ++++++++----------- .../infrastructure/staterpc/task_grants.go | 11 +------ proto/state/v1/task_grant.proto | 4 +-- 10 files changed, 26 insertions(+), 67 deletions(-) diff --git a/CLAUDE.md b/CLAUDE.md index eccdfe835..059ac66d0 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -29,9 +29,9 @@ and calls its composition root in `internal/app/`. | --- | --- | --- | | `archie-state-store` | State Store and Control Plane contracts; owns the task and control-plane PostgreSQL tables | PostgreSQL | | `archie-gateway` | Chat, conversation state, task tools | State Store | -| `archied` | Scheduling, workflow runs, harness, Daemon contract | State Store, Gateway, Messaging | +| `archied` | Scheduling, workflow runs, task containers, Daemon contract | State Store, Gateway, Messaging | | `archie-messaging` | External channel frontends | Gateway | -| `archie-ui` | Dashboard HTTP API (`internal/webui`) and the embedded SPA | State Store, Gateway, daemon, harness | +| `archie-ui` | Dashboard HTTP API (`internal/webui`) and the embedded SPA | State Store, Gateway, daemon | | `archie-agent` | The worker inside each task container | NATS | `internal/domain` holds behaviour and interfaces; `internal/infrastructure` diff --git a/internal/container/pool.go b/internal/container/pool.go index c45953446..72b7845f4 100644 --- a/internal/container/pool.go +++ b/internal/container/pool.go @@ -147,9 +147,6 @@ func (p *Pool) current() Config { // daemonLabel marks containers a pool owns; its value is the instance name. const daemonLabel = "archie-daemon" -// Instance is the name of the archie instance the pool belongs to. -func (p *Pool) Instance() string { return p.cfg.Instance } - // Config is the subset of daemon container configuration the pool needs. type Config struct { Image string @@ -616,13 +613,6 @@ func (p *Pool) Close() error { // ── helpers ────────────────────────────────────────────────────────── -// EnsureImage pulls ref under the live pull policy unless the pool has -// already made it available. The configured image was pulled by NewPool and -// recorded, so a caller naming it skips a second pull. -func (p *Pool) EnsureImage(ctx context.Context, ref string) error { - return p.ensureImage(ctx, p.current(), ref) -} - // ensureImage pulls ref under cfg's pull policy unless it is already present // in the pool. It is the live read an acquire uses; EnsureImage is the // exported form for callers that only name a ref. diff --git a/internal/contracts/state/v1/task_grant.pb.go b/internal/contracts/state/v1/task_grant.pb.go index c668861a9..ded359dcf 100644 --- a/internal/contracts/state/v1/task_grant.pb.go +++ b/internal/contracts/state/v1/task_grant.pb.go @@ -248,10 +248,8 @@ type ResolveTaskGrantResponse struct { TaskId int64 `protobuf:"varint,1,opt,name=task_id,json=taskId,proto3" json:"task_id,omitempty"` // Canonical execution identity; empty names the instance root RPC route. ExecutionIdentity string `protobuf:"bytes,2,opt,name=execution_identity,json=executionIdentity,proto3" json:"execution_identity,omitempty"` - // Canonical principal, including the instance root. - PrincipalId string `protobuf:"bytes,3,opt,name=principal_id,json=principalId,proto3" json:"principal_id,omitempty"` - unknownFields protoimpl.UnknownFields - sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + sizeCache protoimpl.SizeCache } func (x *ResolveTaskGrantResponse) Reset() { @@ -298,13 +296,6 @@ func (x *ResolveTaskGrantResponse) GetExecutionIdentity() string { return "" } -func (x *ResolveTaskGrantResponse) GetPrincipalId() string { - if x != nil { - return x.PrincipalId - } - return "" -} - var File_state_v1_task_grant_proto protoreflect.FileDescriptor const file_state_v1_task_grant_proto_rawDesc = "" + @@ -319,11 +310,10 @@ const file_state_v1_task_grant_proto_rawDesc = "" + "\x05token\x18\x01 \x01(\tR\x05token\"\x19\n" + "\x17RevokeTaskGrantResponse\"/\n" + "\x17ResolveTaskGrantRequest\x12\x14\n" + - "\x05token\x18\x01 \x01(\tR\x05token\"\x85\x01\n" + + "\x05token\x18\x01 \x01(\tR\x05token\"v\n" + "\x18ResolveTaskGrantResponse\x12\x17\n" + "\atask_id\x18\x01 \x01(\x03R\x06taskId\x12-\n" + - "\x12execution_identity\x18\x02 \x01(\tR\x11executionIdentity\x12!\n" + - "\fprincipal_id\x18\x03 \x01(\tR\vprincipalId2\xa3\x02\n" + + "\x12execution_identity\x18\x02 \x01(\tR\x11executionIdentityJ\x04\b\x03\x10\x04R\fprincipal_id2\xa3\x02\n" + "\x10TaskGrantService\x12\\\n" + "\x11RegisterTaskGrant\x12\".state.v1.RegisterTaskGrantRequest\x1a#.state.v1.RegisterTaskGrantResponse\x12V\n" + "\x0fRevokeTaskGrant\x12 .state.v1.RevokeTaskGrantRequest\x1a!.state.v1.RevokeTaskGrantResponse\x12Y\n" + diff --git a/internal/domain/agentrun/runner.go b/internal/domain/agentrun/runner.go index 1b4c73085..a0b31114c 100644 --- a/internal/domain/agentrun/runner.go +++ b/internal/domain/agentrun/runner.go @@ -27,13 +27,6 @@ type Reporter struct { Transcript func(TranscriptEntry) } -// ReportToolCall forwards report to r.ToolCall, if set. -func (r Reporter) ReportToolCall(report ToolCallReport) { - if r.ToolCall != nil { - r.ToolCall(report) - } -} - // ReportTranscript forwards entry to r.Transcript, if set. func (r Reporter) ReportTranscript(entry TranscriptEntry) { if r.Transcript != nil { diff --git a/internal/domain/audit/audit.go b/internal/domain/audit/audit.go index 64723965d..a6bf9a453 100644 --- a/internal/domain/audit/audit.go +++ b/internal/domain/audit/audit.go @@ -109,7 +109,7 @@ func TargetKinds() []string { "capture", "secret", "member", "dashboard", "event", "orgs", "workspaces", "memberships", "org_agents", "mappings", "bindings", "event_types", "sources", - "installed_packages", "harness_secrets", + "installed_packages", // audit_retention is the retention sweep's coverage table, audit is // the resource kind a denied feed read names, and audit_export is the // row a completed export writes; all reach the feed as target kinds. diff --git a/internal/domain/storepkg/descriptor.go b/internal/domain/storepkg/descriptor.go index b66a6f517..4a601434d 100644 --- a/internal/domain/storepkg/descriptor.go +++ b/internal/domain/storepkg/descriptor.go @@ -165,7 +165,7 @@ func validSurface(surface string) bool { return surface == SurfaceSecretEngine || surface == SurfaceForge || surface == SurfaceChannel } -// PackageRef identifies a required package or Kit. References are pinned to +// PackageRef identifies a required package. References are pinned to // immutable content digests so dependency resolution cannot drift by tag. type PackageRef struct { Name string `yaml:"name"` diff --git a/internal/events/events.go b/internal/events/events.go index 3e9c7c552..1a8bafdfb 100644 --- a/internal/events/events.go +++ b/internal/events/events.go @@ -29,7 +29,7 @@ const ( // entry kind; data: agentrun.TranscriptEntry's fields. KindAgentTranscript = "agent_transcript" // KindDeliveryRecorded records one outward delivery a test run did not - // perform: a forge write, a push, an HTTP request, a central tool call. + // perform: a forge write, a push or an HTTP request. // detail: what it would have done, phrased "would ...". KindDeliveryRecorded = "delivery_recorded" KindParked = "parked" // data: reason diff --git a/internal/infrastructure/bindingcipher/cipher.go b/internal/infrastructure/bindingcipher/cipher.go index 97ad8c0c3..f371c3fda 100644 --- a/internal/infrastructure/bindingcipher/cipher.go +++ b/internal/infrastructure/bindingcipher/cipher.go @@ -23,17 +23,17 @@ const bindingEnvelopeVersion = "v1" // bindingNonceLen is GCM's recommended 12-byte nonce. const bindingNonceLen = 12 -// Domain is an envelope's marker and additional-authenticated-data: the +// domain is an envelope's marker and additional-authenticated-data: the // separator that stops a ciphertext sealed for one column being relocated to // a different one and still authenticating. // Domains share the same keyring; only the marker and AAD differ. -type Domain struct { +type domain struct { marker string aad []byte } -// BindingDomain seals binding and source webhook secrets. Not row-bound. -var BindingDomain = Domain{marker: "arcie-binding", aad: []byte("arcie-binding-secret")} +// bindingDomain seals binding and source webhook secrets. Not row-bound. +var bindingDomain = domain{marker: "arcie-binding", aad: []byte("arcie-binding-secret")} // bindingKeyFingerprintLen is the number of hex characters of the // SHA-256(material) fingerprint embedded in the envelope (8 bytes, far more @@ -46,16 +46,11 @@ const bindingKeyFingerprintLen = 16 // use. type BindingCipher interface { // Encrypt returns the sealed envelope for a plaintext secret, under - // BindingDomain. + // bindingDomain. Encrypt(plaintext string) (string, error) // Decrypt returns the plaintext secret for a sealed envelope sealed - // under BindingDomain. + // under bindingDomain. Decrypt(envelope string) (string, error) - // EncryptDomain and DecryptDomain are Encrypt/Decrypt for a caller-named - // domain, so a second column can share this - // cipher's keyring without sharing BindingDomain's AAD. - EncryptDomain(d Domain, plaintext string) (string, error) - DecryptDomain(d Domain, envelope string) (string, error) } // bindingCipher is AES-256-GCM with keys SHA-256(material). The active key @@ -88,19 +83,19 @@ func NewBindingCipher(active string, previous []string) (*bindingCipher, error) return c, nil } -// Encrypt seals plaintext under BindingDomain with the active key. +// Encrypt seals plaintext under bindingDomain with the active key. func (c *bindingCipher) Encrypt(plaintext string) (string, error) { - return c.EncryptDomain(BindingDomain, plaintext) + return c.encryptDomain(bindingDomain, plaintext) } -// Decrypt opens an envelope sealed under BindingDomain. +// Decrypt opens an envelope sealed under bindingDomain. func (c *bindingCipher) Decrypt(envelope string) (string, error) { - return c.DecryptDomain(BindingDomain, envelope) + return c.decryptDomain(bindingDomain, envelope) } -// EncryptDomain seals plaintext with the active key and returns the versioned +// encryptDomain seals plaintext with the active key and returns the versioned // envelope: ":v1::". -func (c *bindingCipher) EncryptDomain(d Domain, plaintext string) (string, error) { +func (c *bindingCipher) encryptDomain(d domain, plaintext string) (string, error) { block, err := aes.NewCipher(c.activeKey) if err != nil { return "", fmt.Errorf("store: binding cipher: %w", err) @@ -121,9 +116,9 @@ func (c *bindingCipher) EncryptDomain(d Domain, plaintext string) (string, error c.activeFingerprint, base64.RawURLEncoding.EncodeToString(payload)), nil } -// DecryptDomain decrypts an envelope sealed under d. An unknown key or wrong +// decryptDomain decrypts an envelope sealed under d. An unknown key or wrong // domain is an error. -func (c *bindingCipher) DecryptDomain(d Domain, envelope string) (string, error) { +func (c *bindingCipher) decryptDomain(d domain, envelope string) (string, error) { parts := strings.SplitN(envelope, ":", 4) if len(parts) != 4 || parts[0] != d.marker || parts[1] != bindingEnvelopeVersion { return "", errors.New("store: binding cipher: unrecognised envelope") diff --git a/internal/infrastructure/staterpc/task_grants.go b/internal/infrastructure/staterpc/task_grants.go index e08b3854d..60a59dc27 100644 --- a/internal/infrastructure/staterpc/task_grants.go +++ b/internal/infrastructure/staterpc/task_grants.go @@ -282,7 +282,7 @@ func (s *taskGrantServer) ResolveTaskGrant(ctx context.Context, r *pb.ResolveTas if err != nil { return nil, status.Error(codes.Unavailable, "execution identity unavailable") } - return &pb.ResolveTaskGrantResponse{TaskId: id, ExecutionIdentity: string(principal), PrincipalId: string(principal)}, nil + return &pb.ResolveTaskGrantResponse{TaskId: id, ExecutionIdentity: string(principal)}, nil } // RegisterTaskGrant creates fresh random material at the daemon and registers @@ -421,15 +421,6 @@ func (s *server) executionPrincipal(ctx context.Context, t *task.Task) (identity return value.ID, nil } -// TaskPrincipalForCredential resolves the task and its canonical execution principal. -func (c *Client) TaskPrincipalForCredential(ctx context.Context, token string) (*task.Task, identity.IdentityID, error) { - r, err := c.taskGrant.ResolveTaskGrant(ctx, &pb.ResolveTaskGrantRequest{Token: token}) - if err != nil { - return nil, "", unmapError(err) - } - t, err := c.TaskByID(ctx, r.TaskId) - return t, identity.IdentityID(r.PrincipalId), err -} type taskGrantServer struct { pb.UnimplementedTaskGrantServiceServer diff --git a/proto/state/v1/task_grant.proto b/proto/state/v1/task_grant.proto index 9cb899112..3bd7dac43 100644 --- a/proto/state/v1/task_grant.proto +++ b/proto/state/v1/task_grant.proto @@ -26,6 +26,6 @@ message ResolveTaskGrantResponse { int64 task_id = 1; // Canonical execution identity; empty names the instance root RPC route. string execution_identity = 2; - // Canonical principal, including the instance root. - string principal_id = 3; + reserved 3; + reserved "principal_id"; } -- 2.54.0 From b97eb45275ad8d59e0e092bee8b7cd5422d437c5 Mon Sep 17 00:00:00 2001 From: Sam Catlow Date: Thu, 12 Nov 2026 21:42:27 +1100 Subject: [PATCH 7/7] style(staterpc): drop a blank line left by the removal --- internal/infrastructure/staterpc/task_grants.go | 1 - 1 file changed, 1 deletion(-) diff --git a/internal/infrastructure/staterpc/task_grants.go b/internal/infrastructure/staterpc/task_grants.go index 60a59dc27..f4c33b5a6 100644 --- a/internal/infrastructure/staterpc/task_grants.go +++ b/internal/infrastructure/staterpc/task_grants.go @@ -421,7 +421,6 @@ func (s *server) executionPrincipal(ctx context.Context, t *task.Task) (identity return value.ID, nil } - type taskGrantServer struct { pb.UnimplementedTaskGrantServiceServer *server -- 2.54.0