chore(runtime): delete Kit and external agent harness support (#1877) #1887

Merged
sam merged 8 commits from chore/delete-kit-harness into main 2026-11-12 11:27:21 +00:00
168 changed files with 1884 additions and 11213 deletions

No files matched your search

+7
View File
@@ -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
@@ -26,6 +27,12 @@ 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.
- 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
### archied
+2 -2
View File
@@ -29,9 +29,9 @@ and calls its composition root in `internal/app/<service>`.
| --- | --- | --- |
| `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`
+10 -1
View File
@@ -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
-46
View File
@@ -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
}
+7 -4
View File
@@ -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 <backup|restore|validate|rollback> -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 <backup|restore|validate|rollback> -h | access reset -h\n\n")
flag.PrintDefaults()
}
flag.Parse()
+6 -1
View File
@@ -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"
+5 -3
View File
@@ -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`
@@ -30,7 +29,10 @@ that reference is on a registry that needs a login -- see
(<https://offloaded.dev>) —
`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
+32 -1
View File
@@ -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
+7 -3
View File
@@ -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"
+6 -3
View File
@@ -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"
+6 -3
View File
@@ -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"
+6 -3
View File
@@ -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"
+1 -1
View File
@@ -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
+3 -2
View File
@@ -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:
-414
View File
@@ -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.
+2 -4
View File
@@ -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` |
+15 -1
View File
@@ -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
+1 -5
View File
@@ -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
+1 -42
View File
@@ -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=
+75 -6
View File
@@ -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.
@@ -219,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}"
@@ -266,10 +276,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 +335,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 +351,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 +368,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 +561,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 ""
-153
View File
@@ -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)
}
-59
View File
@@ -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)
}
-127
View File
@@ -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"]
}
-46
View File
@@ -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)
}
})
}
}
-171
View File
@@ -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")
}
-107
View File
@@ -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 }
@@ -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{} }
-66
View File
@@ -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 }
-30
View File
@@ -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 }
-126
View File
@@ -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))
})
}
-550
View File
@@ -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
}
-41
View File
@@ -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)
}
}
}
+14
View File
@@ -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
}
-15
View File
@@ -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{})
}
+1 -5
View File
@@ -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)
+3 -14
View File
@@ -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
}
+59
View File
@@ -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())
}
-254
View File
@@ -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)
},
}
}
-97
View File
@@ -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))
}
}
}
-95
View File
@@ -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)
}
})
}
}
-135
View File
@@ -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)
}
+2 -13
View File
@@ -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) }
@@ -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())
}
@@ -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)
@@ -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)
}
})
}
}
+1 -1
View File
@@ -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)
}
+2 -6
View File
@@ -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)
}
-346
View File
@@ -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)
}
}
@@ -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)
}
+1 -2
View File
@@ -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())
-5
View File
@@ -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
}
-14
View File
@@ -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
}
-1
View File
@@ -18,7 +18,6 @@ type eventCaptureStore interface { //nolint:interfacebloat // composite of the s
storecontract.PlaybookDispatcher
storecontract.EventTypeStore
storecontract.SourceStore
storecontract.HarnessSecretStore
toolCallWriter
}
+5 -3
View File
@@ -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
+99
View File
@@ -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
}
+7 -6
View File
@@ -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
+11 -6
View File
@@ -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
}
-4
View File
@@ -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
+31 -36
View File
@@ -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.
@@ -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.
@@ -704,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
@@ -713,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"`
}
@@ -784,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))
@@ -793,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)
@@ -807,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
@@ -832,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)
}
@@ -853,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)
-1
View File
@@ -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")
}
-274
View File
@@ -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 '/^<!-- BEGIN ARCHIE -->$/ { managed=1; next } /^<!-- END ARCHIE -->$/ { managed=0; next } !managed { print }' "$KIT_PATH" > "$tmp"
fi
printf '%s\n' '<!-- BEGIN ARCHIE -->' >> "$tmp"
printf %s "$KIT_CONTENT" | base64 -d >> "$tmp"
printf '\n%s\n' '<!-- END ARCHIE -->' >> "$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
}
-82
View File
@@ -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, "<!-- BEGIN ARCHIE -->") != 1 {
t.Fatalf("managed guidance was not replaced: %q", out)
}
})
}
}
-50
View File
@@ -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())
}
-54
View File
@@ -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")
}
}
-34
View File
@@ -13,7 +13,6 @@ import (
"fmt"
"io"
"log/slog"
"maps"
"os"
"path/filepath"
"strconv"
@@ -148,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
@@ -321,29 +317,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()
@@ -640,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.
-61
View File
@@ -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() }
@@ -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
}
@@ -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",
}
-576
View File
@@ -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
}
@@ -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",
}
@@ -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
}
@@ -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",
}
+4 -14
View File
@@ -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" +
+1 -1
View File
@@ -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 {
+3 -28
View File
@@ -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
-214
View File
@@ -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
}
+83
View File
@@ -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
}
+1 -1
View File
@@ -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")
-30
View File
@@ -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.
-7
View File
@@ -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 {
+1 -1
View File
@@ -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.
-16
View File
@@ -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
}
@@ -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
}
@@ -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.
-4
View File
@@ -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
+1 -1
View File
@@ -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"`
+14
View File
@@ -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 -5
View File
@@ -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}
+1 -1
View File
@@ -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
+1 -1
View File
@@ -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.
+14 -26
View File
@@ -23,24 +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
}
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
@@ -53,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 (e.g. harness OAuth secrets) 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
@@ -95,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: "<marker>:v1:<fingerprint>:<base64url(nonce‖ct‖tag)>".
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)
@@ -128,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")
@@ -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.",
@@ -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
}
@@ -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 {
@@ -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
}
@@ -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 {
@@ -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
@@ -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
@@ -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) {
@@ -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
}
Loaded 100 of 168 files, more files were not shown because too many files have changed in this diff. Show more