Merge crew/claude-997: 429 for rate-limited webhooks, agent profiles as a live resource, per-stage profile via workflow.call, harness needs enforced at dispatch

This commit is contained in:
sam committed 2026-09-27 05:27:09 +10:00
commit c07c04e45a
27 files changed
+614 -120

No files matched your search

@@ -175,6 +175,7 @@ looking at before reporting one:
| `volume_ttl` | Default `72h` only when enabled; negative rejected. |
| `pull_policy` | Default `missing` when enabled. Pool code recognises `missing` and `always`; other strings silently skip pre-pull. |
| `network` | Empty uses best-effort self-network detection. Repository overlay pins `archie-core_default`. |
| `profiles` | Named agent execution environments (image or Kit); resolved per task dispatch (`ContainerConfig.Profile`). Own control-plane resource `agent-profiles` (`json:"-"` on the field so `container-runtime-policies` never carries it), `ApplyMode: live` -- a profile change applies without a restart, unlike the rest of `[containers]`. |
### Channel settings
+1 -1
View File
@@ -328,7 +328,7 @@
"url": "/dev/prds/external-agent-harness/",
"title": "External agent harnesses",
"section": "PRDs",
"body": "# External agent harnesses\n\n**Status:** Approved\n**Authority:** `docs/architecture/agent-system.md` (agent execution as a data\nboundary); settles the harness non-goal deferred by\n`docs/prds/service-decomposition.md`.\n**Compounds with:** `docs/prds/execution-tree-state-machine.md` (a harness\nrun is one `agent` StepExecution), `docs/prds/binding-secret-encryption.md`\n(the at-rest envelope reused for harness credentials).\n\n## Decision\n\nA workflow stage can run on an external coding-agent CLI instead of the\nbuilt-in agent loop: Claude Code, Codex, Pi, OMP, GitHub Copilot CLI, or any\nother CLI packaged as a Docker Sandbox Kit v3. Each authenticates with an API\nkey or the operator's subscription login, and can carry the operator's own\nskills, plugins, subagents and instructions.\n\nTwo boundaries hold regardless of what the harness does:\n\n- **The real credential never enters the container.** The container sees\n sentinel values; archie's egress proxy substitutes the real credential on\n outbound requests and performs OAuth refresh on the host.\n- **Archie's workflow guarantees are enforced outside the harness.** Git,\n protected paths, read-only stages, gates and budgets are checked by archie\n around the process and against the worktree afterwards.\n\n## Kits\n\nA harness is packaged as a Kit v3 image: an OCI image whose\n`vnd.docker.sandbox.kit.descriptor` manifest annotation declares what the\nharness needs. Archie reads descriptors with the published\n`github.com/docker/sandbox-kit-spec/v3/spec` package and does not maintain\nits own descriptor format. Published Kits run unmodified.\n\nArchie is a partial Kit runtime. It implements these capability types:\n\n| Type | Archie behaviour |\n| ------------------- | ----------------------------------------------------------------------------------------- |\n| `credential@1` | Proxy-managed only, described below. |\n| `network-policy@1` | Enforced by the egress proxy. |\n| `volume@1` | A Docker volume keyed by WorkflowExecution, deleted with it. |\n| `lifecycle@1` | Install and startup hooks run on container create. |\n| `agent-sessions@1` | The headless `prompt` and `resume` invocations for the stage. |\n| `agent-skills@1` | The operator's skills store, mounted read-only whatever the Kit asks. |\n| `agent-context@1` | Archie's stage instructions, delivered where the Kit declares. |\n| `resources@1` | Container CPU and memory limits. |\n\nEvery other type fails closed: a required entry refuses the launch, an\noptional one is skipped and recorded on the step. `credential@1` with\n`passthrough: true` and any credential for a forge service are refused.\n\nThe operator's own setup (plugins, subagents, instructions, settings) is a\nmixin Kit the operator builds `FROM` or alongside the harness Kit. It is\nversioned image content, rewritten on every create, never state a run can\nmodify.\n\n## Contract\n\nA harness run is a second implementation of `agentexec.Runner`. It takes the\nsame `Request` and returns the same `Result`, so workflows, the daemon and\nthe State Store do not know which runner ran a stage.\n\n`agent-sessions@1` supplies the invocation. Archie supplies, per CLI, an\noutput adapter that translates the CLI's JSON output stream into\n`ToolCallReporter` calls, `Usage` and a session ID. A CLI with no adapter can\nrun stages with no capture tools, reporting no usage; its token budget falls\nback to wall clock.\n\nWorkflow definition validation rejects, at load time, a stage whose needs the\nselected harness cannot meet: capture tools without an adapter, or a gate\nretry without a `resume` verb.\n\n## Selection\n\nA harness is an agent profile. `docs/prds/event-automation.md` defines a\nprofile as the execution environment a workflow selects; a harness profile\nnames a Kit composition (the workload Kit pinned by digest, and zero or more\nmixin Kits) in place of a container image. Everything else about profiles\nholds: a workflow names its profile, a stage may name a different one, and a\nprofile holds no secrets.\n\n`archie-agent` builds the stage's runner from the stage's profile: the\nai-sdk agent loop for an image profile, the harness runner for a Kit\nprofile. Image profiles, and every workflow already written against them,\nare unchanged. A Kit profile named by no workflow is a validation error.\n\nProfiles are org resources in the State Store, so a Kit profile belongs to\none org, is granted to its workspaces like any profile, and applies without\na restart.\n\n## Organisations\n\n`docs/prds/Line truncated
"body": "# External agent harnesses\n\n**Status:** Approved\n**Authority:** `docs/architecture/agent-system.md` (agent execution as a data\nboundary); settles the harness non-goal deferred by\n`docs/prds/service-decomposition.md`.\n**Compounds with:** `docs/prds/execution-tree-state-machine.md` (a harness\nrun is one `agent` StepExecution), `docs/prds/binding-secret-encryption.md`\n(the at-rest envelope reused for harness credentials).\n\n## Decision\n\nA workflow stage can run on an external coding-agent CLI instead of the\nbuilt-in agent loop: Claude Code, Codex, Pi, OMP, GitHub Copilot CLI, or any\nother CLI packaged as a Docker Sandbox Kit v3. Each authenticates with an API\nkey or the operator's subscription login, and can carry the operator's own\nskills, plugins, subagents and instructions.\n\nTwo boundaries hold regardless of what the harness does:\n\n- **The real credential never enters the container.** The container sees\n sentinel values; archie's egress proxy substitutes the real credential on\n outbound requests and performs OAuth refresh on the host.\n- **Archie's workflow guarantees are enforced outside the harness.** Git,\n protected paths, read-only stages, gates and budgets are checked by archie\n around the process and against the worktree afterwards.\n\n## Kits\n\nA harness is packaged as a Kit v3 image: an OCI image whose\n`vnd.docker.sandbox.kit.descriptor` manifest annotation declares what the\nharness needs. Archie reads descriptors with the published\n`github.com/docker/sandbox-kit-spec/v3/spec` package and does not maintain\nits own descriptor format. Published Kits run unmodified.\n\nArchie is a partial Kit runtime. It implements these capability types:\n\n| Type | Archie behaviour |\n| ------------------- | ----------------------------------------------------------------------------------------- |\n| `credential@1` | Proxy-managed only, described below. |\n| `network-policy@1` | Enforced by the egress proxy. |\n| `volume@1` | A Docker volume keyed by WorkflowExecution, deleted with it. |\n| `lifecycle@1` | Install and startup hooks run on container create. |\n| `agent-sessions@1` | The headless `prompt` and `resume` invocations for the stage. |\n| `agent-skills@1` | The operator's skills store, mounted read-only whatever the Kit asks. |\n| `agent-context@1` | Archie's stage instructions, delivered where the Kit declares. |\n| `resources@1` | Container CPU and memory limits. |\n\nEvery other type fails closed: a required entry refuses the launch, an\noptional one is skipped and recorded on the step. `credential@1` with\n`passthrough: true` and any credential for a forge service are refused.\n\nThe operator's own setup (plugins, subagents, instructions, settings) is a\nmixin Kit the operator builds `FROM` or alongside the harness Kit. It is\nversioned image content, rewritten on every create, never state a run can\nmodify.\n\n## Contract\n\nA harness run is a second implementation of `agentexec.Runner`. It takes the\nsame `Request` and returns the same `Result`, so workflows, the daemon and\nthe State Store do not know which runner ran a stage.\n\n`agent-sessions@1` supplies the invocation. Archie supplies, per CLI, an\noutput adapter that translates the CLI's JSON output stream into\n`ToolCallReporter` calls, `Usage` and a session ID. A CLI with no adapter can\nrun stages with no capture tools, reporting no usage; its token budget falls\nback to wall clock.\n\nWorkflow definition validation rejects, at load time, a stage whose needs the\nselected harness cannot meet: capture tools without an adapter, or a gate\nretry without a `resume` verb.\n\n## Selection\n\nA harness is an agent profile. `docs/prds/event-automation.md` defines a\nprofile as the execution environment a workflow selects; a harness profile\nnames a Kit composition (the workload Kit pinned by digest, and zero or more\nmixin Kits) in place of a container image. Everything else about profiles\nholds: a workflow names its profile, a stage may name a different one, and a\nprofile holds no secrets.\n\n`archie-agent` builds the stage's runner from the stage's profile: the\nai-sdk agent loop for an image profile, the harness runner for a Kit\nprofile. Image profiles, and every workflow already written against them,\nare unchanged. A Kit profile named by no workflow is a validation error.\n\nA stage naming a different profile than its workflow's is a separate run,\nnot a mid-container runner switch: a Kit task runs in one container\nprovisioned for one profile (Container layout), so a sLine truncated
},
{
"url": "/dev/prds/identity-decision/",
+13
View File
@@ -85,6 +85,19 @@ 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.
@@ -5,8 +5,10 @@ import (
"encoding/json"
"log/slog"
"testing"
"time"
"github.com/samcharles93/archie-core/internal/config"
"github.com/samcharles93/archie-core/internal/domain/workflow"
"github.com/samcharles93/archie-core/internal/infrastructure/configuration"
"github.com/samcharles93/archie-core/internal/infrastructure/postgres/pgstore"
"github.com/samcharles93/archie-core/internal/webui"
@@ -136,3 +138,30 @@ func TestPublishConfigSnapshotWithoutConfiguration(t *testing.T) {
t.Fatalf("ConfigSnapshot = (found %v, %v), want nothing published", found, err)
}
}
// TestLiveSettingsApplyRepublishesTheConfigSnapshot: the dashboard renders the
// published projection, not the daemon's Holder, so a live apply that changes
// the running config must republish it or the page keeps showing the old
// values until the next reload.
func TestLiveSettingsApplyRepublishesTheConfigSnapshot(t *testing.T) {
st := pgstore.Open(t)
t.Cleanup(func() { _ = st.Close() })
b, _ := newLiveApplyBoot(t, fileConfig())
b.stateStore = st
if err := b.applyWorkflowExecutionSettings(t.Context(), workflow.ExecutionSettings{MaxModelToolSteps: 25, MaxRuntime: time.Hour, MaxConsecutiveGateFailures: 4}, 2); err != nil {
t.Fatalf("live update: %v", err)
}
snapshot, found, err := st.ConfigSnapshot(context.Background())
if err != nil || !found {
t.Fatalf("ConfigSnapshot = (found %v, %v), want the live apply to publish one", found, err)
}
var view webui.ConfigView
if err := json.Unmarshal(snapshot.Document, &view); err != nil {
t.Fatalf("decode published document: %v", err)
}
if view.Budgets.MaxSteps != 25 || view.Budgets.GateMaxFailures != 4 {
t.Errorf("published budgets = %+v, want the live-applied limits", view.Budgets)
}
}
+1 -1
View File
@@ -216,7 +216,7 @@ func (b *boot) applyWorkflowExecutionSettings(ctx context.Context, settings work
return err
}
b.executionSettings.Store(&candidate.settings)
b.cfgHolder.Set(candidate.cfg)
b.publishConfig(ctx, candidate.cfg)
b.applyStatus.Report(ctx, controlplane.WorkflowExecutionSettingsKind, version, nil)
b.log.Info("workflow execution settings applied", "version", version)
if limit, uptime := settings.MaxTaskRuntime, candidate.cfg.Containers.MaxUptime.Std(); uptime > 0 && (limit <= 0 || limit > uptime) {
+13 -9
View File
@@ -148,16 +148,20 @@ var reloadableFields = map[string]bool{
// reloadableSubFields are sub-fields of structs that are otherwise
// startup-built. VolumeTTL is re-read per cycle in cleanupExpiredStorage;
// everything else in Containers (Image, MaxConcurrency,
// MaxUptime, PullPolicy, Network) is frozen in the startup-built
// container pool -- the dispatchers re-read MaxConcurrency but the pool
// captures it at construction (container/pool.go:94,163), so a change
// only partially applies and must warn requires-restart. Forge.Host is
// carried into TaskContext by ForTask (display/link building only); the
// forge client itself is startup-built, so Type/Token/TokenEnv stay
// requires-restart.
// Profiles is resolved fresh per dispatch (daemon.configFor ->
// Containers.Profile, called from pinTaskProfile) and is also its own
// live-applying control-plane resource (AgentProfileKind), never captured at
// container-pool construction -- docs/prds/external-agent-harness.md
// "Selection": a Kit profile applies without a restart. Everything else in
// Containers (Image, MaxConcurrency, MaxUptime, PullPolicy, Network) is
// frozen in the startup-built container pool -- the dispatchers re-read
// MaxConcurrency but the pool captures it at construction
// (container/pool.go:94,163), so a change only partially applies and must
// warn requires-restart. Forge.Host is carried into TaskContext by ForTask
// (display/link building only); the forge client itself is startup-built, so
// Type/Token/TokenEnv stay requires-restart.
var reloadableSubFields = map[string]map[string]bool{
"Containers": {"VolumeTTL": true},
"Containers": {"VolumeTTL": true, "Profiles": true},
"Forge": {"Host": true},
// Policy is carried into TaskConfig by ForTask (config.go); MCPServers,
// WebFetch and Minimax are not and stay requires-restart.
@@ -0,0 +1,67 @@
package controlplane
import "github.com/samcharles93/archie-core/internal/config"
// AgentProfileKind is the control-plane resource for Kit and image agent
// profiles. It exists so a Kit profile applies without a restart
// (docs/prds/external-agent-harness.md, "Selection"): [containers].profiles
// in config.toml is only its seed, ContainerRuntimePoliciesKind carries every
// other container setting and none of this one (config.ContainerConfig.
// Profiles is json:"-"), and this resource's own ApplyMode is "live".
//
// Its document is agentProfile (container_settings.go's snake_case wire
// type), not config.AgentProfile directly: that internal type carries no
// json tags, so encoding/json would fall back to its Go field names --
// "Tools", "Image" -- exactly the defect archie-core-1171 fixed for the rest
// of this document family.
const AgentProfileKind = "agent-profiles"
func agentProfileDefinition() Definition {
return Definition{
Kind: AgentProfileKind,
Title: "Agent profiles",
ApplyMode: "live",
Document: map[string]agentProfile{},
Seed: seedAgentProfiles,
Validate: validateAgentProfiles,
}
}
func seedAgentProfiles(cfg config.Config) any {
return agentProfilesDocument(cfg.Containers.Profiles)
}
// agentProfilesDocument maps the internal type to the document's wire type.
func agentProfilesDocument(profiles map[string]config.AgentProfile) map[string]agentProfile {
if profiles == nil {
return nil
}
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}
}
return out
}
// agentProfilesSettings reverses agentProfilesDocument, for the layering in
// runtime_config.go and for validation against config.ValidateAgentProfiles.
func agentProfilesSettings(profiles map[string]agentProfile) map[string]config.AgentProfile {
if profiles == nil {
return nil
}
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}
}
return out
}
func validateAgentProfiles(input []byte) error {
// The rules live in the config package and are shared with the file
// document's own validation (config.ContainerConfig.ValidateProfiles):
// which profiles are valid cannot differ between the file that seeds
// this resource and the resource that replaces it.
return validateAs(input, func(profiles map[string]agentProfile) error {
return config.ValidateAgentProfiles(agentProfilesSettings(profiles))
})
}
+24 -25
View File
@@ -17,14 +17,18 @@ import (
// "MaxConcurrency", "LegacyEnabled" -- the only Go-cased resource left in the
// registry (archie-core-1171). The two durations are config.Duration, which
// already writes the string the file accepts; the leak was the keys only.
//
// Profiles is deliberately not a field here: config.ContainerConfig.Profiles
// is json:"-" (its own resource, AgentProfileKind, applies live where the
// rest of this one is restart-required -- docs/prds/external-agent-harness.md
// "Selection"), so this document never carries it either.
type containerRuntimePolicies struct {
Image string `json:"image"`
MaxConcurrency int `json:"max_concurrency"`
MaxUptime config.Duration `json:"max_uptime"`
VolumeTTL config.Duration `json:"volume_ttl"`
PullPolicy string `json:"pull_policy"`
Network string `json:"network"`
Profiles map[string]agentProfile `json:"profiles,omitempty"`
Image string `json:"image"`
MaxConcurrency int `json:"max_concurrency"`
MaxUptime config.Duration `json:"max_uptime"`
VolumeTTL config.Duration `json:"volume_ttl"`
PullPolicy string `json:"pull_policy"`
Network string `json:"network"`
}
// agentProfile is one named execution environment as the document writes it.
@@ -43,7 +47,6 @@ type agentProfile struct {
var legacyContainerKeys = map[string]string{
"Image": "image", "MaxConcurrency": "max_concurrency", "MaxUptime": "max_uptime",
"VolumeTTL": "volume_ttl", "PullPolicy": "pull_policy", "Network": "network",
"Profiles": "profiles",
}
// legacyProfileKeys are the same fallback at the profile nesting level.
@@ -74,6 +77,11 @@ func (p *containerRuntimePolicies) UnmarshalJSON(data []byte) error {
// consumer -- so it is dropped rather than renamed: the strict decode would
// otherwise refuse a document every deployment of that window stores.
delete(raw, "LegacyEnabled")
// "profiles"/"Profiles" carried config.ContainerConfig.Profiles before it
// moved to its own resource, AgentProfileKind; a document stored under
// either name before that still decodes, with the key simply dropped.
delete(raw, "profiles")
delete(raw, "Profiles")
encoded, err := json.Marshal(raw)
if err != nil {
return err
@@ -87,33 +95,22 @@ func (p *containerRuntimePolicies) UnmarshalJSON(data []byte) error {
}
// settings maps the document onto the internal config struct the daemon runs.
// Profiles is never set here: config.ContainerConfig.Profiles is json:"-",
// and AgentProfileKind's own layering step in runtime_config.go is what sets
// it, after this one runs.
func (p containerRuntimePolicies) settings() config.ContainerConfig {
out := config.ContainerConfig{
return config.ContainerConfig{
Image: p.Image, MaxConcurrency: p.MaxConcurrency, MaxUptime: p.MaxUptime,
VolumeTTL: p.VolumeTTL, PullPolicy: p.PullPolicy, Network: p.Network,
}
if p.Profiles != nil {
out.Profiles = make(map[string]config.AgentProfile, len(p.Profiles))
for name, profile := range p.Profiles {
out.Profiles[name] = config.AgentProfile{Image: profile.Image, Kit: profile.Kit, Adapter: profile.Adapter, Tools: profile.Tools}
}
}
return out
}
func seedContainerPolicies(cfg config.Config) any {
containers := cfg.Containers
policies := containerRuntimePolicies{
return containerRuntimePolicies{
Image: containers.Image, MaxConcurrency: containers.MaxConcurrency, MaxUptime: containers.MaxUptime,
VolumeTTL: containers.VolumeTTL, PullPolicy: containers.PullPolicy, Network: containers.Network,
}
if containers.Profiles != nil {
policies.Profiles = make(map[string]agentProfile, len(containers.Profiles))
for name, profile := range containers.Profiles {
policies.Profiles[name] = agentProfile{Image: profile.Image, Kit: profile.Kit, Adapter: profile.Adapter, Tools: profile.Tools}
}
}
return policies
}
func validateContainers(input []byte) error {
@@ -130,7 +127,9 @@ func validateContainers(input []byte) error {
if strings.TrimSpace(settings.Image) == "" {
return fmt.Errorf("container image is required for autonomous workflow workers")
}
return settings.ValidateProfiles()
// Profiles is json:"-" on config.ContainerConfig: it is
// AgentProfileKind's document now, validated there.
return nil
})
}
@@ -33,7 +33,6 @@ func TestContainerRuntimePoliciesSeedIsTheDocumentShape(t *testing.T) {
Image: "archie-agent:test", MaxConcurrency: 4,
MaxUptime: config.Duration(2 * time.Hour), VolumeTTL: config.Duration(24 * time.Hour),
PullPolicy: "missing", Network: "bridge",
Profiles: map[string]config.AgentProfile{"net": {Tools: []string{"whois"}}},
}}
encoded, err := json.Marshal(containerPoliciesDefinition(t).Seed(cfg))
if err != nil {
@@ -53,33 +52,31 @@ func TestContainerRuntimePoliciesSeedIsTheDocumentShape(t *testing.T) {
t.Errorf("seed %s = %#v, want %#v", key, document[key], value)
}
}
profiles, ok := document["profiles"].(map[string]any)
if !ok {
t.Fatalf("profiles = %#v, want an object", document["profiles"])
}
net, ok := profiles["net"].(map[string]any)
if !ok || net["tools"] == nil {
t.Fatalf("profiles.net = %#v, want the snake_case tools", profiles["net"])
}
// The Go field names encoding/json fell back to when the document was the
// internal config struct -- at both nesting levels.
for _, legacy := range []string{"Image", "MaxConcurrency", "MaxUptime", "VolumeTTL", "PullPolicy", "Network", "Profiles"} {
// internal config struct.
for _, legacy := range []string{"Image", "MaxConcurrency", "MaxUptime", "VolumeTTL", "PullPolicy", "Network"} {
if _, present := document[legacy]; present {
t.Errorf("seed carries the Go-cased key %q", legacy)
}
}
for _, legacy := range []string{"Image", "Kit", "Adapter", "Tools"} {
if _, present := net[legacy]; present {
t.Errorf("profiles.net carries the Go-cased key %q", legacy)
}
// Profiles is its own resource (AgentProfileKind, applying live where the
// rest of this document is restart-required), so it never appears here at
// all, under either casing.
if _, present := document["profiles"]; present {
t.Error("seed carries profiles: it belongs to AgentProfileKind now")
}
if _, present := document["Profiles"]; present {
t.Error("seed carries Profiles: it belongs to AgentProfileKind now")
}
}
// legacyContainerDocument is a document as an earlier revision stored it: the
// Go field names encoding/json fell back to while the document was the internal
// config struct, and LegacyEnabled from the short window in which the removed
// containers.enabled switch still decoded. Every store written before the
// reshape holds a document like this one, so it has to keep working.
// config struct, LegacyEnabled from the short window in which the removed
// containers.enabled switch still decoded, and Profiles from before it moved to
// its own resource, AgentProfileKind. Every store written before either reshape
// holds a document like this one, so it has to keep working -- Profiles simply
// drops, the way LegacyEnabled already does.
const legacyContainerDocument = `{"Image":"archie-agent:test","MaxConcurrency":4,"MaxUptime":"2h0m0s","VolumeTTL":"24h0m0s","PullPolicy":"missing","Network":"bridge","LegacyEnabled":false,"Profiles":{"net":{"Image":"proxy:1","Tools":["whois"]}}}`
// TestContainerRuntimePoliciesReadsADocumentWrittenBeforeTheReshape covers the
@@ -99,7 +96,7 @@ func TestContainerRuntimePoliciesReadsADocumentWrittenBeforeTheReshape(t *testin
if err != nil {
t.Fatalf("Decode(legacy) = %v", err)
}
for _, legacy := range []string{`"Image"`, `"MaxConcurrency"`, `"LegacyEnabled"`, `"Profiles"`, `"Tools"`} {
for _, legacy := range []string{`"Image"`, `"MaxConcurrency"`, `"LegacyEnabled"`, `"Profiles"`, `"profiles"`} {
if strings.Contains(string(decoded), legacy) {
t.Fatalf("decoded document = %s, want the canonical keys without %q", decoded, legacy)
}
@@ -111,10 +108,6 @@ func TestContainerRuntimePoliciesReadsADocumentWrittenBeforeTheReshape(t *testin
VolumeTTL string `json:"volume_ttl"`
PullPolicy string `json:"pull_policy"`
Network string `json:"network"`
Profiles map[string]struct {
Image string `json:"image"`
Tools []string `json:"tools"`
} `json:"profiles"`
}
if err := json.Unmarshal(decoded, &document); err != nil {
t.Fatalf("unmarshal decoded document: %v", err)
@@ -124,23 +117,16 @@ func TestContainerRuntimePoliciesReadsADocumentWrittenBeforeTheReshape(t *testin
document.PullPolicy != "missing" || document.Network != "bridge" {
t.Errorf("document = %+v, want the legacy values preserved in the current shape", document)
}
if len(document.Profiles) != 1 || document.Profiles["net"].Image != "proxy:1" {
t.Errorf("profiles = %+v, want the legacy values preserved", document.Profiles)
}
}
// TestContainerRuntimePoliciesKeepsDisallowUnknownFields: the legacy tolerance
// renames the keys an existing store holds and nothing else, because swallowing
// an unknown key decodes as "unset" and an unset image is a broken worker. A
// misspelling is refused, at the top level and inside a profile.
// renames the keys an existing store holds and nothing else (plus dropping
// profiles/Profiles, which no longer belongs to this document at all), because
// swallowing any other unknown key decodes as "unset" and an unset image is a
// broken worker.
func TestContainerRuntimePoliciesKeepsDisallowUnknownFields(t *testing.T) {
for _, document := range []string{
`{"image":"archie-agent:test","maxconcurrency":4}`,
`{"image":"archie-agent:test","profiles":{"net":{"toolz":["whois"]}}}`,
} {
if err := containerPoliciesDefinition(t).Validate([]byte(document)); err == nil {
t.Errorf("Validate(%s) accepted a misspelled key", document)
}
if err := containerPoliciesDefinition(t).Validate([]byte(`{"image":"archie-agent:test","maxconcurrency":4}`)); err == nil {
t.Error("Validate accepted a misspelled key")
}
}
@@ -155,7 +141,7 @@ func TestContainerRuntimePoliciesLayersLegacyDocuments(t *testing.T) {
}{
{
name: "the current shape",
stored: `{"image":"archie-agent:test","max_concurrency":4,"max_uptime":"2h0m0s","volume_ttl":"24h0m0s","pull_policy":"missing","network":"bridge","profiles":{"net":{"image":"proxy:1","tools":["whois"]}}}`,
stored: `{"image":"archie-agent:test","max_concurrency":4,"max_uptime":"2h0m0s","volume_ttl":"24h0m0s","pull_policy":"missing","network":"bridge"}`,
},
{
name: "the Go-cased shape an existing store holds",
@@ -175,8 +161,11 @@ func TestContainerRuntimePoliciesLayersLegacyDocuments(t *testing.T) {
got.Containers.MaxUptime != config.Duration(2*time.Hour) || got.Containers.Network != "bridge" {
t.Fatalf("Containers = %+v, want the stored document applied", got.Containers)
}
if len(got.Containers.Profiles) != 1 || got.Containers.Profiles["net"].Image != "proxy:1" {
t.Fatalf("Profiles = %+v, want the stored profiles applied", got.Containers.Profiles)
// Profiles is AgentProfileKind's document now, not this one's; this
// resource carries none, under either casing, so it never reaches
// out.Containers.Profiles.
if len(got.Containers.Profiles) != 0 {
t.Fatalf("Profiles = %+v, want none: container-runtime-policies carries no profiles", got.Containers.Profiles)
}
})
}
+1 -1
View File
@@ -8,7 +8,7 @@ import (
// Schemas are not declared here: each Definition names its document type, and
// Definition.Descriptor derives the JSON Schema from it (internal/app/controlplane/schema.go).
func builtinDefinitions(steps workflow.StepRegistry) []Definition {
definitions := []Definition{workflowDefinition(), workflowDefinitionsDefinition(steps), personaDefinition(), scheduleDefinition()}
definitions := []Definition{workflowDefinition(), workflowDefinitionsDefinition(steps), personaDefinition(), scheduleDefinition(), agentProfileDefinition()}
definitions = append(definitions, modelDefinitions()...)
definitions = append(definitions, operationalDefinitions()...)
return definitions
+21 -17
View File
@@ -204,29 +204,33 @@ func runtimeToolConfigFrom(ctx context.Context, reader controlplanerpc.ResourceR
return config.Config{}, nil, err
}
if err := layerResource(ctx, reader, versions, ContainerRuntimePoliciesKind, func(value []byte) error {
// A document that omits profiles (one stored before they existed)
// inherits the file's. One that carries them replaces the file's:
// the mapping below would otherwise merge into the file's map, so a
// profile deleted from the store would come back from the file. Both
// spellings are checked, because documents written before the document
// had its own shape carry the Go-cased key.
var keys map[string]json.RawMessage
if err := json.Unmarshal(value, &keys); err != nil {
return err
}
_, carriesProfiles := keys["profiles"]
if !carriesProfiles {
_, carriesProfiles = keys["Profiles"]
}
var policies containerRuntimePolicies
if err := json.Unmarshal(value, &policies); err != nil {
return err
}
inherited := out.Containers.Profiles
// Profiles survives this assignment untouched: containerRuntimePolicies
// carries no Profiles field at all (it is AgentProfileKind's document
// now), and settings() never sets it, so whatever the AgentProfileKind
// layering step below assigns is what's left standing regardless of
// which of the two runs first.
profiles := out.Containers.Profiles
out.Containers = policies.settings()
if !carriesProfiles {
out.Containers.Profiles = inherited
out.Containers.Profiles = profiles
return nil
}); err != nil {
return config.Config{}, nil, err
}
// AgentProfileKind is a separate resource from ContainerRuntimePoliciesKind
// (config.ContainerConfig.Profiles is json:"-") so a Kit profile applies
// without a restart: a stored value replaces the file's outright rather
// than merging into it, the same reason ModelRoleAssignmentsKind above
// reassigns out.Models wholesale instead of decoding into the existing map.
if err := layerResource(ctx, reader, versions, AgentProfileKind, func(value []byte) error {
var profiles map[string]agentProfile
if err := json.Unmarshal(value, &profiles); err != nil {
return err
}
out.Containers.Profiles = agentProfilesSettings(profiles)
return nil
}); err != nil {
return config.Config{}, nil, err
@@ -59,6 +59,7 @@ func TestRuntimeConfigUsesDatabaseResourcesAndPreservesBootstrapOnlySecrets(t *t
ToolSettingsKind: map[string]any{"mcp_servers": []map[string]any{{"name": "docs", "transport": "http", "url": "https://mcp.example.com", "headers_configured": true}}, "policy": map[string]any{}, "web_fetch": map[string]any{}, "minimax": map[string]any{"enabled": false, "credential_configured": false}},
PluginSettingsKind: map[string]any{"plugin_dir": "/plugins", "module_dir": "/modules", "secret_engine_dir": "/secrets", "skills_dir": "/skills"},
ContainerRuntimePoliciesKind: map[string]any{"image": "archie:next", "pull_policy": "missing"},
AgentProfileKind: map[string]any{"net": map[string]any{"tools": []string{"whois"}}},
}})
got, versions, err := client.RuntimeConfig(t.Context(), base)
@@ -69,7 +70,7 @@ func TestRuntimeConfigUsesDatabaseResourcesAndPreservesBootstrapOnlySecrets(t *t
// publishes is the one it actually layered in (archie-core-pskb).
for _, kind := range []string{
ProviderSettingsKind, ModelRoleAssignmentsKind, RepositoryPoliciesKind, ChannelSettingsKind,
SchedulingPolicyKind, ToolSettingsKind, PluginSettingsKind, ContainerRuntimePoliciesKind,
SchedulingPolicyKind, ToolSettingsKind, PluginSettingsKind, ContainerRuntimePoliciesKind, AgentProfileKind,
} {
if versions[kind] != 2 {
t.Errorf("versions[%s] = %d, want the 2 the store answered with", kind, versions[kind])
@@ -551,7 +552,7 @@ func TestRuntimeResourceKindsApplyLive(t *testing.T) {
modes[definition.Kind] = definition.ApplyMode
}
for _, kind := range []string{
ProviderSettingsKind, ModelRoleAssignmentsKind, RepositoryPoliciesKind, SchedulingPolicyKind,
ProviderSettingsKind, ModelRoleAssignmentsKind, RepositoryPoliciesKind, SchedulingPolicyKind, AgentProfileKind,
} {
if modes[kind] != "live" {
t.Errorf("%s applies %q, want live: the daemon re-layers this kind on a watch", kind, modes[kind])
@@ -583,31 +584,47 @@ func TestSchedulingPolicySeedCarriesTheLabel(t *testing.T) {
}
}
// Stored container policies that carry profiles replace the file's outright;
// ones stored before profiles existed inherit the file's.
// absentReader answers every kind as not-found, so a test can exercise
// runtimeConfigFrom's leave-the-file-value-in-effect path (real ImportConfig
// never leaves a kind unseeded, but a store the migration has not reached yet
// -- or a resource kind added after this document was last stored -- does).
type absentReader struct{}
func (absentReader) Query(context.Context, string, func([]byte) error) (int64, bool, error) {
return 0, false, nil
}
// A stored AgentProfileKind value replaces the file's profiles outright; no
// stored value at all leaves the file's in effect (agent-profiles is its own
// resource, seeded from but independent of container-runtime-policies, so a
// Kit profile applies without a restart -- docs/prds/external-agent-harness.md
// "Selection").
func TestRuntimeConfigLayersStoredProfiles(t *testing.T) {
base := config.Config{Containers: config.ContainerConfig{Image: "agent:1", Profiles: map[string]config.AgentProfile{
"file-only": {Image: "file:1"},
}}}
for _, tt := range []struct {
name string
stored map[string]any
want []string
}{
{name: "stored profiles replace the file's", stored: map[string]any{"image": "agent:2", "profiles": map[string]any{"net": map[string]any{"tools": []string{"whois"}}}}, want: []string{"net"}},
{name: "a stored value replaces the file's", stored: map[string]any{"net": map[string]any{"tools": []string{"whois"}}}, want: []string{"net"}},
{
// The Go-cased spelling documents written before the document had its
// own shape carry: the profile it names must still replace the file's.
name: "legacy Go-cased profiles replace the file's",
stored: map[string]any{"Image": "agent:2", "Profiles": map[string]any{"net": map[string]any{"Tools": []string{"whois"}}}},
// The Go-cased spelling documents written before agent-profiles had
// its own document shape carry, at the profile-nesting level
// (agentProfile.UnmarshalJSON): the profile it names must still
// replace the file's.
name: "a legacy Go-cased stored value replaces the file's",
stored: map[string]any{"net": map[string]any{"Tools": []string{"whois"}}},
want: []string{"net"},
},
{name: "a document without profiles inherits the file's", stored: map[string]any{"image": "agent:2"}, want: []string{"file-only"}},
} {
t.Run(tt.name, func(t *testing.T) {
client := NewRPCClient(&runtimeConfigClient{values: map[string]any{
SchedulingPolicyKind: map[string]any{"poll_interval": "2m", "dispatch": map[string]any{"trigger": "assignee"}},
ContainerRuntimePoliciesKind: tt.stored,
ContainerRuntimePoliciesKind: map[string]any{"image": "agent:2"},
AgentProfileKind: tt.stored,
}})
got, _, err := client.RuntimeConfig(t.Context(), base)
if err != nil {
@@ -622,6 +639,21 @@ func TestRuntimeConfigLayersStoredProfiles(t *testing.T) {
}
})
}
t.Run("no stored value inherits the file's", func(t *testing.T) {
got, _, err := runtimeConfigFrom(t.Context(), absentReader{}, base)
if err != nil {
t.Fatalf("runtimeConfigFrom: %v", err)
}
var names []string
for name := range got.Containers.Profiles {
names = append(names, name)
}
if want := []string{"file-only"}; !reflect.DeepEqual(names, want) {
t.Fatalf("profiles = %v, want %v", names, want)
}
})
if got := base.Containers.Profiles; len(got) != 1 {
t.Fatalf("layering mutated the file document's profiles: %v", got)
}
@@ -112,11 +112,11 @@ func TestResourceValidatorsRejectWhatEffectiveValidationRejects(t *testing.T) {
wantErr: false,
},
{
name: "container profile with an empty tool name",
kind: ContainerRuntimePoliciesKind,
value: containerRuntimePolicies{Image: "archie-agent:test", Profiles: map[string]agentProfile{
name: "agent profile with an empty tool name",
kind: AgentProfileKind,
value: map[string]agentProfile{
"net": {Tools: []string{""}},
}},
},
mutate: func(cfg *config.Config) {
cfg.Containers.Profiles = map[string]config.AgentProfile{"net": {Tools: []string{""}}}
},
+11
View File
@@ -195,6 +195,17 @@ func (g *Gateway) handleWebhook(route *RouteConfig) http.HandlerFunc {
return
}
// A rate-limited delivery was dropped before reaching the LLM, not
// completed: it must not be echoed back as a successful "origin" reply
// (chat prose, meant for a human) or 202'd as accepted (indistinguishable
// from a delivered event, so the source would never retry it). 429 lets
// the source back off and retry, the way internal/webhookguard.RateLimiter's
// other callers already respond (archie-core-1173).
if reply.RateLimited {
http.Error(w, "rate limited", http.StatusTooManyRequests)
return
}
if route.DeliverTo == "origin" && reply.Text != "" {
w.Header().Set("Content-Type", "text/plain")
_, _ = w.Write([]byte(reply.Text))
+29
View File
@@ -120,6 +120,35 @@ func TestWebhookHandlerRoute(t *testing.T) {
}
}
// TestWebhookHandlerRateLimitedDeliveryIsRejected pins archie-core-1173: a
// rate-limited webhook delivery must be surfaced to the origin as an
// observable rejection (429), never as chat prose echoed back with a 200, and
// never as a 202 that claims a dropped event was accepted (the origin would
// then never retry it).
func TestWebhookHandlerRateLimitedDeliveryIsRejected(t *testing.T) {
for _, deliverTo := range []string{"origin", ""} {
t.Run("DeliverTo="+deliverTo, func(t *testing.T) {
log := slog.New(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{Level: slog.LevelWarn}))
g := New("", 0, []RouteConfig{{Path: "/hook", DeliverTo: deliverTo}}, log)
g.client = &fakeChatContract{
routeFunc: func(context.Context, messaging.Inbound) (messaging.ChatReply, error) {
return messaging.ChatReply{Text: "You're sending messages too quickly. Please wait a moment and try again.", RateLimited: true}, nil
},
}
handler := g.WebhookHandler()
rec := httptest.NewRecorder()
handler.ServeHTTP(rec, httptest.NewRequestWithContext(context.Background(), http.MethodPost, "/hook", strings.NewReader(`{"text":"hello"}`)))
if rec.Code != http.StatusTooManyRequests {
t.Errorf("status = %d, want 429 so the origin can back off and retry", rec.Code)
}
if strings.Contains(rec.Body.String(), "too quickly") {
t.Errorf("body = %q, want no chat prose echoed to a webhook origin", rec.Body.String())
}
})
}
}
func TestWebhookHandlerInvalidSignature(t *testing.T) {
g := New("", 0, []RouteConfig{{Path: "/hook", Secret: "secret"}}, slog.Default())
g.client = &fakeChatContract{}
+15 -2
View File
@@ -846,7 +846,11 @@ type ContainerConfig struct {
// Profiles are the named agent profiles a workflow selects with
// `profile:`. A workflow that names none runs under the default profile:
// Image and every tool.
Profiles map[string]AgentProfile `toml:"profiles" yaml:"profiles"`
// Profiles is excluded from the control-plane JSON document
// (container-runtime-policies): profiles are their own resource,
// AgentProfileKind, seeded from this field but validated and stored
// (and reloaded live) separately -- see internal/app/controlplane.
Profiles map[string]AgentProfile `toml:"profiles" yaml:"profiles" json:"-"`
}
// AgentProfile is a named execution environment for an agent. Secrets and
@@ -893,7 +897,16 @@ func (p AgentProfile) IsKit() bool { return len(p.Kit) > 0 }
// 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.
func (c ContainerConfig) ValidateProfiles() error {
for name, p := range c.Profiles {
return ValidateAgentProfiles(c.Profiles)
}
// ValidateAgentProfiles is ValidateProfiles's rule set, standalone so the
// control-plane AgentProfileKind resource (internal/app/controlplane) can
// apply the same rules to a stored value: the file document and the
// resource that replaces it must not be able to disagree about which
// profiles are valid.
func ValidateAgentProfiles(profiles map[string]AgentProfile) error {
for name, p := range profiles {
if strings.TrimSpace(name) == "" {
return fmt.Errorf("containers.profiles: a profile name must not be empty")
}
+21 -1
View File
@@ -1757,7 +1757,9 @@ 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 {
return profile, true
if err = validateProfileMeetsNeeds(profile, iface.Needs); err == nil {
return profile, true
}
}
}
d.Log.Error("agent profile unavailable", "task", task.ID, "err", err)
@@ -1765,6 +1767,24 @@ func (d *Daemon) pinTaskProfile(ctx context.Context, task *workflow.Task) (confi
return config.AgentProfile{}, false
}
// validateProfileMeetsNeeds rejects a Kit profile whose harness cannot meet
// what the workflow declares it needs (docs/prds/external-agent-harness.md,
// "Contract"): a workflow needing captures but naming a profile whose Kit CLI
// serves none (no adapter, or one with no MCP config to register archie-agent
// mcp against -- e.g. Codex, Pi, OMP per agentexec.harnessAdapters). An image
// profile is never checked: the built-in agent loop always supports both
// captures and gate retries, so Needs constrains only the harness path.
func validateProfileMeetsNeeds(profile config.AgentProfile, needs workflowtask.WorkflowNeeds) error {
if !profile.IsKit() || !needs.Captures {
return nil
}
adapter, ok := agentexec.LookupHarnessAdapter(profile.Adapter)
if !ok || len(adapter.MCPConfig) == 0 {
return fmt.Errorf("needs.captures is set, but profile adapter %q serves no capture tools", profile.Adapter)
}
return nil
}
// publicationGrant issues the capability to publish the task's branch. A task
// with no repository has nothing to publish and gets none.
func (d *Daemon) publicationGrant(ctx context.Context, task *workflow.Task) (string, func(), bool) {
+151
View File
@@ -2353,6 +2353,115 @@ func TestRunViaAgentCarriesTheWorkflowProfile(t *testing.T) {
}
}
// TestPinTaskProfileRefusesCapturesTheHarnessCannotServe is the harness PRD's
// (Contract) "workflow definition validation rejects... a stage whose needs
// the selected harness cannot meet: capture tools without an adapter". A Kit
// profile's own adapter -- not the workflow -- decides whether captures are
// servable, so a workflow that needs them but names a profile whose CLI
// serves no capture tools (empty MCPConfig, e.g. Codex/Pi/OMP) must be
// refused rather than silently running with no way to return its result.
func TestPinTaskProfileRefusesCapturesTheHarnessCannotServe(t *testing.T) {
d, s, _ := daemonWithNATS(t)
cfg := d.Cfg.Get()
cfg.Containers.Profiles = map[string]config.AgentProfile{
"serves-captures": {Kit: []string{"kit@sha256:ab"}, Adapter: agentexec.AdapterClaudeCode},
"no-captures": {Kit: []string{"kit@sha256:ab"}, Adapter: agentexec.AdapterCodex},
"no-adapter": {Kit: []string{"kit@sha256:ab"}},
}
d.Cfg.Set(cfg)
d.WorkflowDefinitions = &workflowDefinitionsStub{collection: workflow.WorkflowDefinitionCollection{Definitions: []workflow.WorkflowDefinitionEntry{
{ID: "needs-captures-served", YAML: "id: needs-captures-served\nrepository: none\nprofile: serves-captures\nneeds: {captures: true}\nsteps:\n - type: agent.run\n settings: {mission: go}\n"},
{ID: "needs-captures-codex", YAML: "id: needs-captures-codex\nrepository: none\nprofile: no-captures\nneeds: {captures: true}\nsteps:\n - type: agent.run\n settings: {mission: go}\n"},
{ID: "needs-captures-bare", YAML: "id: needs-captures-bare\nrepository: none\nprofile: no-adapter\nneeds: {captures: true}\nsteps:\n - type: agent.run\n settings: {mission: go}\n"},
}}, version: 1}
ctx := context.Background()
claim := func(number int, wf string) *workflow.Task {
t.Helper()
if _, err := s.EnqueueIssue(ctx, "acme", "widget", number, "t", "b", "", ""); err != nil {
t.Fatal(err)
}
task, err := s.ClaimNext(ctx)
if err != nil || task == nil {
t.Fatalf("claim: (%v, %v)", task, err)
}
task.Workflow = wf
return task
}
served := claim(51, "needs-captures-served")
if _, ok := d.pinTaskProfile(ctx, served); !ok {
t.Fatal("a Kit profile whose adapter serves captures was refused")
}
for _, tt := range []struct {
number int
workflow string
}{
{52, "needs-captures-codex"},
{53, "needs-captures-bare"},
} {
task := claim(tt.number, tt.workflow)
if _, ok := d.pinTaskProfile(ctx, task); ok {
t.Fatalf("%s: a workflow needing captures was not refused by a profile that cannot serve them", tt.workflow)
}
got, err := s.TaskByID(ctx, task.ID)
if err != nil || got.Status != workflow.StatusParked || !strings.Contains(got.ParkReason, "captures") {
t.Fatalf("%s: task = %+v, %v; want parked naming the unmet captures need", tt.workflow, got, err)
}
}
}
// TestWorkflowCallGivesACalleeItsOwnProfile is the harness PRD's "a stage may
// name a different profile" (docs/prds/external-agent-harness.md, Selection):
// there is no mid-container runner switch, and none is built here. A stage
// wanting a different profile -- a Kit harness where the rest of the workflow
// runs on the built-in loop -- is its own callee workflow, started the way
// workflow.call already starts one (StartCall), with its own agent profile
// resolved independently by pinTaskProfile off the callee's own task row.
// TestWorkflowCallerConformance (staterpc) already proves the callee's
// Workflow field is independent of the caller's; this proves that
// independence is what makes Kit-vs-image runner selection independent too.
func TestWorkflowCallGivesACalleeItsOwnProfile(t *testing.T) {
d, s, _ := daemonWithNATS(t)
cfg := d.Cfg.Get()
cfg.Containers.Profiles = map[string]config.AgentProfile{
"net": {Image: "agent-net:1"},
"contain": {Kit: []string{"contain-kit@sha256:ab"}, Adapter: agentexec.AdapterClaudeCode},
}
d.Cfg.Set(cfg)
d.WorkflowDefinitions = &workflowDefinitionsStub{collection: workflow.WorkflowDefinitionCollection{Definitions: []workflow.WorkflowDefinitionEntry{
{ID: "investigate", YAML: "id: investigate\nprofile: net\nsteps:\n - type: implement.prepare\n"},
{ID: "contain", YAML: "id: contain\nrepository: none\nprofile: contain\nsteps:\n - type: agent.run\n settings: {mission: contain it}\n"},
}}, version: 1}
ctx := context.Background()
if _, err := s.EnqueueIssue(ctx, "acme", "widget", 41, "t", "b", "", ""); err != nil {
t.Fatal(err)
}
caller, err := s.ClaimNext(ctx)
if err != nil || caller == nil {
t.Fatalf("claim caller: (%v, %v)", caller, err)
}
caller.Workflow = "investigate"
// Exactly the call a workflow.call step makes (workflow_call.go's
// runWorkflowCall -> tc.Calls.StartCall), not a hand-rolled shortcut.
callee, err := s.StartCall(ctx, caller.ID, "contain", nil)
if err != nil || callee == nil {
t.Fatalf("StartCall: (%+v, %v)", callee, err)
}
callerProfile, ok := d.pinTaskProfile(ctx, caller)
if !ok || callerProfile.IsKit() {
t.Fatalf("caller profile = %+v, %v; want a non-Kit profile", callerProfile, ok)
}
calleeProfile, ok := d.pinTaskProfile(ctx, callee)
if !ok || !calleeProfile.IsKit() || calleeProfile.Adapter != agentexec.AdapterClaudeCode {
t.Fatalf("callee profile = %+v, %v; want the Kit profile its own workflow names", calleeProfile, ok)
}
}
// A container that dies mid-run (max-uptime reaper, OOM, crash) never answers
// the taskrun request. The run must end with the task parked, not left
// running with the worker blocked on a reply that cannot come.
@@ -2521,3 +2630,45 @@ func TestKitProfileRunsTheTaskOnItsHarness(t *testing.T) {
t.Fatalf("released %v, removed %v; want the run released and its volumes removed", launcher.released, launcher.removed)
}
}
// TestRunKitTaskThreadsGateRetriesToTheLauncher: KitLauncher.Launch is where
// the Kit's actual agent-sessions capability is known (it composes the Kit),
// so that is where docs/prds/external-agent-harness.md's "a gate retry
// without a resume verb" refusal has to run (kit.ValidateNeeds, wired in
// kitrun.Launcher.Launch). This proves runKitTask carries the workflow's
// declared retry budget there rather than dropping it.
func TestRunKitTaskThreadsGateRetriesToTheLauncher(t *testing.T) {
d, s, busClient := daemonWithNATS(t)
launcher := &fakeKitLauncher{}
d.KitLauncher = launcher
d.ConnectedNATS.URL = "nats://172.17.0.1:4222"
ctx := context.Background()
if _, err := s.EnqueueIssue(ctx, "acme", "widget", 32, "t", "b", "", ""); err != nil {
t.Fatal(err)
}
task, err := s.ClaimNext(ctx)
if err != nil || task == nil {
t.Fatalf("claim: (%v, %v)", task, err)
}
task.WorkflowDefinitionYAML = "id: contain\nrepository: none\nneeds: {gate_retries: 4}\nsteps:\n - type: agent.run\n settings: {mission: go}\n"
// runKitTask calls through to runViaAgent after Launch, which blocks on a
// NATS reply bounded by the task wall-clock budget (zero here, i.e. no
// bound at all) -- a handler that never responds hangs the daemon
// goroutine forever rather than timing out, so this must answer even
// though the assertion below only cares about what Launch already saw.
sub, err := mustCoreConn(t, busClient).Subscribe(agentnats.SubjectForTask(task.ID), func(msg *natsio.Msg) {
data, _ := json.Marshal(taskrun.Response{Status: workflow.StatusPROpen})
_ = msg.Respond(data)
})
if err != nil {
t.Fatalf("subscribe: %v", err)
}
t.Cleanup(func() { _ = sub.Unsubscribe() })
d.runKitTask(ctx, task, config.Repo{Owner: "acme", Name: "widget", Base: "main"}, t.TempDir(),
config.AgentProfile{Kit: []string{"kit@sha256:ab"}, Adapter: agentexec.AdapterClaudeCode})
if launcher.got.GateRetries != 4 {
t.Fatalf("launch request GateRetries = %d, want the workflow's declared 4", launcher.got.GateRetries)
}
}
+16 -5
View File
@@ -8,6 +8,7 @@ import (
"github.com/samcharles93/archie-core/internal/config"
"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/kit"
"github.com/samcharles93/archie-core/internal/infrastructure/kitrun"
"github.com/samcharles93/archie-core/internal/taskstate"
@@ -51,12 +52,22 @@ func (d *Daemon) runKitTask(ctx context.Context, task *workflow.Task, repo confi
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
}
run, err := d.KitLauncher.Launch(ctx, kitrun.Request{
Execution: fmt.Sprintf("task-%d", task.ID),
Kit: profile.Kit,
Adapter: profile.Adapter,
WorkDir: workDir,
WorkerEnv: env,
Execution: fmt.Sprintf("task-%d", task.ID),
Kit: profile.Kit,
Adapter: profile.Adapter,
WorkDir: workDir,
WorkerEnv: env,
GateRetries: iface.Needs.GateRetries,
})
if err != nil {
park("kit launch failed", err)
+6
View File
@@ -24,6 +24,12 @@ type ChatSnapshot struct {
type ChatReply struct {
Text string
SessionID string
// RateLimited reports whether Text is the inbound rate-limit reply rather
// than a real dispatch outcome. A human-facing channel can ignore it and
// send Text as usual; a channel with no human reading replies (a webhook)
// must not treat it as a normal reply or a delivered event -- see
// archie-core-1173.
RateLimited bool
}
// ChatCancellation is what Cancel returns.
@@ -42,6 +42,25 @@ type WorkflowInterface struct {
// Profile names a [containers.profiles] entry. It is resolved when a task
// is dispatched, not when the workflow is saved.
Profile string `yaml:"profile,omitempty" json:"profile,omitempty"`
// Needs declares what this workflow's agent stages require of the
// profile they run under -- checked against the profile's actual
// capabilities before dispatch (docs/prds/external-agent-harness.md,
// "Contract"): a stage that returns structured output needs a harness
// with a capture-tools-capable output adapter, and a gated stage that
// may retry needs the Kit's agent-sessions resume verb. Only a Kit
// profile is checked against it -- an image profile's built-in agent
// loop always supports both.
Needs WorkflowNeeds `yaml:"needs" json:"needs"`
}
// WorkflowNeeds is WorkflowInterface's declared harness requirements.
type WorkflowNeeds struct {
// Captures declares that a stage returns structured output through
// capture tools.
Captures bool `yaml:"captures,omitempty" json:"captures,omitempty"`
// GateRetries is the largest gate-retry budget any stage declares. Zero
// means no stage gates its result, so no resume capability is needed.
GateRetries int `yaml:"gate_retries,omitempty" json:"gate_retries,omitempty"`
}
var (
@@ -75,6 +94,9 @@ func (w WorkflowInterface) Validate() error {
return fmt.Errorf("workflow input %q type %q must be one of %s", name, spec.Type, strings.Join(inputTypes, ", "))
}
}
if w.Needs.GateRetries < 0 {
return fmt.Errorf("workflow needs.gate_retries must not be negative")
}
return nil
}
@@ -18,6 +18,7 @@ func TestParseWorkflowInterface(t *testing.T) {
{name: "unknown mode", src: "id: a\nrepository: sometimes\n", wantErr: "must be none, optional or required"},
{name: "unknown input type", src: "id: a\ninputs:\n ip: {type: ipv4}\n", wantErr: `type "ipv4"`},
{name: "malformed input name", src: "id: a\ninputs:\n src-ip: {type: string}\n", wantErr: `input "src-ip"`},
{name: "negative gate_retries", src: "id: a\nneeds: {gate_retries: -1}\n", wantErr: "gate_retries must not be negative"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
@@ -38,6 +39,19 @@ func TestParseWorkflowInterface(t *testing.T) {
}
}
// TestParseWorkflowInterfaceNeeds pins the declared-needs vocabulary
// (docs/prds/external-agent-harness.md, "Contract"): a stage's requirements
// of its harness are static, readable metadata, not derived at run time.
func TestParseWorkflowInterfaceNeeds(t *testing.T) {
w, err := ParseWorkflowInterface("id: a\nprofile: contain\nneeds: {captures: true, gate_retries: 3}\n")
if err != nil {
t.Fatal(err)
}
if !w.Needs.Captures || w.Needs.GateRetries != 3 {
t.Fatalf("Needs = %+v, want {Captures:true GateRetries:3}", w.Needs)
}
}
func TestCheckInputs(t *testing.T) {
w := WorkflowInterface{Inputs: map[string]InputSpec{
"src_ip": {Type: "string", Required: true},
+2 -2
View File
@@ -89,12 +89,12 @@ func (a *LocalChatAdapter) RecentTurns(ctx context.Context, id string, n int) ([
}
func (a *LocalChatAdapter) Route(ctx context.Context, in Inbound) (ChatReply, error) {
reply, err := a.Router.Route(ctx, in)
reply, rateLimited, err := a.Router.RouteResult(ctx, in)
if err != nil {
return ChatReply{}, err
}
id, err := a.Router.ResolveSessionKey(ctx, in)
return ChatReply{Text: reply, SessionID: id}, err
return ChatReply{Text: reply, SessionID: id, RateLimited: rateLimited}, err
}
func (a *LocalChatAdapter) Cancel(ctx context.Context, id string) (ChatCancellation, error) {
+14 -2
View File
@@ -269,10 +269,22 @@ func inboundBudgetKey(in Inbound) string {
// Route dispatches msg and returns the reply. Gateway-local commands
// are handled directly; everything else goes to the LLM responder.
func (r *Router) Route(ctx context.Context, in Inbound) (string, error) {
reply, _, err := r.RouteResult(ctx, in)
return reply, err
}
// RouteResult is Route, plus whether the sender was blocked by its inbound
// rate limit. A caller with a human reading the reply (chat channels) can
// ignore the flag and treat the reply text as usual; a caller with no human
// there (a webhook) needs it to answer with a proper rejection instead of
// echoing the rate-limit prose as a successful delivery, or accepting an
// event that was actually dropped (archie-core-1173).
func (r *Router) RouteResult(ctx context.Context, in Inbound) (reply string, rateLimited bool, err error) {
if r.checkRateLimit(in) {
return rateLimitReply, nil
return rateLimitReply, true, nil
}
return r.route(ctx, in)
reply, err = r.route(ctx, in)
return reply, false, err
}
// route is Route's continuation once the caller has already cleared the
+13
View File
@@ -161,6 +161,19 @@ func FromMerge(merged *spec.MergeResult) (*Plan, error) {
return plan, nil
}
// ValidateNeeds rejects a Plan that cannot meet what the workflow's stages
// declared they need (docs/prds/external-agent-harness.md, "Contract"): a
// gate that may retry needs the Kit's agent-sessions resume verb, because
// archie resumes the session with the gate's output rather than starting
// over. gateRetries is zero for a workflow with no gated stage, which no
// Kit composition can fail to meet.
func ValidateNeeds(plan *Plan, gateRetries int) error {
if gateRetries > 0 && (plan.Sessions == nil || len(plan.Sessions.Resume) == 0) {
return fmt.Errorf("needs.gate_retries is %d, but the kit declares no %s resume verb to retry with", gateRetries, typeAgentSessions)
}
return nil
}
func refusalReason(c spec.Capability) (string, error) {
if !supported[c.Type] {
return "not provided by archie", nil
+45
View File
@@ -208,6 +208,51 @@ func TestCompose(t *testing.T) {
}
}
// TestValidateNeeds pins the harness PRD's "a gate retry without a resume
// verb" refusal (Contract): archie resumes the harness's own session with the
// gate's output rather than starting a fresh one, so a gated stage's retry
// budget is unmeetable by a Kit whose agent-sessions capability names no
// resume verb.
func TestValidateNeeds(t *testing.T) {
promptOnly := descriptor(t, spec.KindWorkload, `
- type: com.docker.sandbox/agent-sessions@1
config:
prompt: [-p, "{{.Prompt}}"]`)
withResume := descriptor(t, spec.KindWorkload, sessions)
tests := []struct {
name string
descriptor *spec.Descriptor
gateRetries int
wantErr bool
}{
{"no gated stage, no resume verb", promptOnly, 0, false},
{"gated stage, no resume verb", promptOnly, 2, true},
{"gated stage, a resume verb", withResume, 2, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
plan, err := Admit(tt.descriptor)
if err != nil {
t.Fatalf("Admit: %v", err)
}
sessions, err := spec.AgentSessionsOf(plan.Capabilities)
if err != nil {
t.Fatalf("AgentSessionsOf: %v", err)
}
plan.Sessions = sessions
err = ValidateNeeds(plan, tt.gateRetries)
if tt.wantErr && err == nil {
t.Fatal("ValidateNeeds accepted a gate retry budget the kit cannot resume")
}
if !tt.wantErr && err != nil {
t.Fatalf("ValidateNeeds: %v", err)
}
})
}
}
func TestDecodePublished(t *testing.T) {
published := "schemaVersion: \"3\"\nkind: workload\ncapabilities:" + sessions + "\n"
tests := []struct {
@@ -49,6 +49,12 @@ type Request struct {
Adapter string
WorkDir string
WorkerEnv []string
// GateRetries is the workflow's declared gate-retry budget
// (task.WorkflowNeeds.GateRetries); zero means no stage gates its
// result. A composition with no agent-sessions resume verb cannot meet
// a positive one, and Launch refuses it rather than starting a
// container no gate failure could ever resume.
GateRetries int
}
// Run is a started Kit task.
@@ -78,6 +84,9 @@ func (l *Launcher) Launch(ctx context.Context, req Request) (*Run, error) {
if err != nil {
return nil, err
}
if err := kit.ValidateNeeds(plan, req.GateRetries); err != nil {
return nil, err
}
network, err := spec.NetworkPolicyOf(plan.Capabilities)
if err != nil {
return nil, err