Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0fe3caa175 | ||
|
|
83d57ec6a2 | ||
|
|
e762f15844 | ||
|
|
1539fdc560 | ||
|
|
0e37a0f536 | ||
|
|
de32542c00 | ||
|
|
c1673e43fb | ||
|
|
a9233755c3 | ||
|
|
7ca81dc85a | ||
|
|
457da2e97d | ||
|
|
656d21520f | ||
|
|
92eabbed17 | ||
|
|
28acf2a8db | ||
|
|
c02852d87f | ||
|
|
ab86442156 | ||
|
|
bfb909a93a | ||
|
|
894fffc63b | ||
|
|
c94be59952 | ||
|
|
04d0495f12 | ||
|
|
9ccb6e2418 | ||
|
|
cd878f3c4d | ||
|
|
b650a8c156 | ||
|
|
4241429620 | ||
|
|
cc83c9aea2 | ||
|
|
1f0cb9f318 | ||
|
|
19520a63e5 | ||
|
|
ef203ce30a | ||
|
|
ff3f80e4c1 | ||
|
|
3da407e410 | ||
|
|
4e5b91d791 | ||
|
|
12b59a046d | ||
|
|
af1b9855ab | ||
|
|
3e878464f9 | ||
|
|
216be98652 | ||
|
|
f77baecb00 | ||
|
|
394c76b4cc | ||
|
|
b6defc5f31 | ||
|
|
5e1623f82f | ||
|
|
0544fa58fc | ||
|
|
057a4a8614 | ||
|
|
2a1ed36a26 | ||
|
|
605988e0a9 | ||
|
|
06e0a87a62 | ||
|
|
1d2fcec69e | ||
|
|
f5fbec6371 | ||
|
|
a9f441042e | ||
|
|
a8a04929af | ||
|
|
1e1f51af55 | ||
|
|
3c2b1f6654 | ||
|
|
e10d862f6a | ||
|
|
721968eeff | ||
|
|
446d94681b | ||
|
|
04dedde52c | ||
|
|
a4abafe0ad | ||
|
|
6bf24d73db | ||
|
|
20b4595a95 | ||
|
|
2341c6df36 | ||
|
|
eaabc254be | ||
|
|
68a17aed91 | ||
|
|
4f01b1604d | ||
|
|
bea1c69210 | ||
|
|
7f51f1c39e | ||
|
|
3c32b63066 |
No files matched your search
@@ -120,6 +120,7 @@ Do not expose these helper flags as daemon settings.
|
||||
| `models.<role>` | Workflow stages read requested role then fall back to `builder`; triage judge falls back `triage` to `planner`. | production-wired |
|
||||
| `providers.<name>.class`, `api_key_env`, `base_url` | Builds ai-sdk catalogue. `base_url` rejects parse errors, userinfo, query, and fragment. | production-wired |
|
||||
| `budgets.max_steps`, `max_tokens`, `wall_clock`, `gate_max_failures` | No loader defaults; `Budgets` documents zero as disabling. | production-wired |
|
||||
| `review.precision_gate`, `review.approve_before_post` | 2026-10-02: one control-plane resource `review-settings` (`internal/app/controlplane/review_settings.go`), seeded from `[review]`; the stored document outranks the file, layers field-by-field into `cfg.Review` (`runtimeConfigFrom`), and is watched live (`archie/control_plane.go`). Reaches a run through `Daemon.configFor` → `ForTask` → `TaskContext.Cfg`; the dashboard writes it on the Review settings page. | production-wired |
|
||||
| `diff_cap_lines` | Zero replaced by `400` in `finalize`; `StageDiffCap` treats `<= 0` as disabled. TOML zero cannot disable; negative value does. | production-wired |
|
||||
| `web.listen` | Empty becomes `127.0.0.1:8484`; literal `off` disables. No authentication. | production-wired |
|
||||
| `notify.webhook` | Empty disables; feasibility workflow POSTs human-decision notifications when set. | production-wired |
|
||||
@@ -169,13 +170,14 @@ looking at before reporting one:
|
||||
|
||||
| Key | Effective behaviour |
|
||||
| ----------------- | ------------------------------------------------------------------------------------------------------------------ |
|
||||
| `image` | Required only when enabled. |
|
||||
| `max_concurrency` | Zero means no limit; used by general task dispatcher and container pool. |
|
||||
| `max_uptime` | Default `60m` only when enabled; caps container lifetime. |
|
||||
| `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]`. |
|
||||
| `image` | Required at boot. Read by the container pool on every acquire (`containerLiveSettings` -> `Pool.current`, archie-core-zfb0.2), so a stored change applies without a restart. |
|
||||
| `max_concurrency` | Zero means no limit. Read by the pool per acquire and applied to the daemon's running task dispatcher on publish (`ResizeTaskDispatcher`). |
|
||||
| `max_uptime` | Default `60m` at boot; caps container lifetime. Read by the pool per acquire, so a stored change reaps the next container under the new cap. |
|
||||
| `volume_ttl` | Default `72h` only when enabled; negative rejected. Re-read per cleanup cycle. |
|
||||
| `pull_policy` | Default `missing` at boot. Pool code recognises `missing` and `always`; other strings silently skip pre-pull. Read by the pool per acquire. |
|
||||
| `registry_auth` | `SecretRef` (file-owned, `json:"-"`). Resolved at boot by `archied`'s `resolveRegistryAuth` and passed to `container.Config.RegistryAuth`; the pool base64url-encodes it into the `X-Registry-Auth` header (`internal/container/pool.go`'s `registryAuthHeader`). Zero ref = anonymous pull. Unresolvable/empty credential or an unencodable one warns and pulls anonymously rather than failing the pool. A half-named ref is refused by `configuration.validateContainers`. `container-runtime-policies` cannot carry a secret ref, so this stays boot-resolved and requires-restart, unlike `image`/`pull_policy`. |
|
||||
| `network` | Empty uses best-effort self-network detection at boot; the repository overlay pins `archie-core_default`. Read by the pool per acquire, falling back to the boot-resolved network when the live value is empty. |
|
||||
| `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`. |
|
||||
| `credentials` | Maps a Kit `credential@1` service to a secret (`config.CredentialBinding`). Own control-plane resource `credential-bindings`, `ApplyMode: live`, `json:"-"` on the field. `kitrun.Launcher` reads it fresh from the live `config.Holder` on every `Launch`, never a value captured at boot -- see `internal/infrastructure/kitrun/launcher.go`'s `resolveCredentials`. A binding alone grants nothing; see `org`/`granted_credentials` below. |
|
||||
| `org` / `granted_credentials` (root and per-identity) | An identity's org and the `credentials` service names it may resolve. `daemon.runKitTask` reads both fresh per dispatch (`d.configFor(task)`) and passes them to `kitrun.Request`; `config.ContainerConfig.BoundCredentials` computes the declared (Kit) ∩ granted (identity) ∩ org-matched intersection -- neither declared nor granted widens the other. |
|
||||
|
||||
@@ -193,6 +195,13 @@ looking at before reporting one:
|
||||
Telegram `/restart` re-runs same TOML overlay load, then replaces only gateway
|
||||
token and allowed-user list. Every other file setting is startup-only.
|
||||
|
||||
A stored `channel-settings` change is reconciled live by `archie-messaging`:
|
||||
on a new version it re-resolves the file, layers the store over it, and restarts
|
||||
only the channel whose transport settings changed (Telegram token/allowlist,
|
||||
email relay, webhook route/secret). `chat.email.listen_addr` and
|
||||
`chat.webhook_addr` are bound listen addresses, so those changes, and enabling
|
||||
or disabling a channel, are reported outstanding and take effect on restart.
|
||||
|
||||
## Treat multi-identity as partially wired
|
||||
|
||||
`finalize` requires each identity's `name`, `bot_user`, non-empty repos, forge
|
||||
@@ -216,8 +225,9 @@ Classify multi-identity changes field by field. Never label aggregate
|
||||
|
||||
## Preserve worker and secret boundaries
|
||||
|
||||
`Config.ForTask` creates a detached root snapshot: models, budgets, dispatch,
|
||||
diff cap, notification, and forge host. `TaskConfig.ToConfig` reconstructs
|
||||
`Config.ForTask` creates a detached root snapshot: models, budgets, the
|
||||
retry/remediation cap (`MaxRetries`), dispatch, diff cap, notification, and
|
||||
forge host. `TaskConfig.ToConfig` reconstructs
|
||||
only those fields. Forge tokens, provider values, NATS credential references,
|
||||
repos, and infrastructure settings do not enter `TaskConfig`.
|
||||
|
||||
|
||||
@@ -5,6 +5,92 @@ release's per-component sections are labelled beneath its heading.
|
||||
|
||||
## [Unreleased]
|
||||
|
||||
## [1.46.0] - 2026-10-02
|
||||
|
||||
### archied
|
||||
|
||||
- feat(workflow): declared workflow outputs for wait:true callers
|
||||
- feat(prreview): the operator-approval gate is PRReview's own stage-list entry
|
||||
- feat(staterpc): carry the review gate and re-review rounds, with one guarded response write
|
||||
- feat(prreview): the gate records the review it holds and posts the operator's answer
|
||||
- feat(taskactions): one approval path answers the review gate from every surface
|
||||
- feat(webui): the dashboard answers the review gate, including a re-review's instructions
|
||||
- feat(daemon): resolve the producing identity's org before publishing work
|
||||
- feat(webui): block invalid settings and show a field's former value
|
||||
- fix(workflow): derive a declared output's captures need for profile selection
|
||||
- fix(storepkg): validate every declared authority grant at the package descriptor
|
||||
- feat(archied): refresh the model catalog at runtime
|
||||
- feat(mcp): answer server-initiated sampling from the chat model
|
||||
- feat(container): send registry auth on image pull
|
||||
- feat(messaging): retract ephemeral replies after their TTL
|
||||
- feat(messaging): transcribe Telegram voice notes
|
||||
- fix(egress): take a run's credential kind from its binding, not the Kit
|
||||
- feat(webui): a live-apply setting reads as applied, never as a restart
|
||||
- feat(webui): the dashboard reads harness bindings and opens the setup terminal
|
||||
- feat(gateway): list the harness page in the dashboard page registry
|
||||
- feat(webui): the harness settings page and its setup terminal
|
||||
- fix(workflow): park environment gate failures without dispatching a builder
|
||||
- feat(messaging): adapter-facing clarify, picker and approval contract
|
||||
- feat(gateway): gate admin slash commands by sender policy
|
||||
- feat(plugins): load plugin and secret-engine files without a restart
|
||||
- fix(workflow): deliver the feasibility PRD to the forge issue
|
||||
- fix(messaging): report the daemon version this process was built from
|
||||
- fix(messaging): carry an inbound attachment's bytes to the gateway turn
|
||||
- fix(daemon): resume a retry whose worktree is gone
|
||||
- fix(archied): omit the max-token bound on reasoning-class models
|
||||
- fix(logging): file a task log record under its own event time
|
||||
- fix(control-plane): unify validation for the domain-managed kinds
|
||||
- feat(webui): let the operator choose the retry worktree mode
|
||||
- feat(agent): seed a starter SOUL and upgrade only untouched templates
|
||||
- feat(controlplane): add the writable review-settings resource
|
||||
- fix(messaging): transcribe voice notes on the model-owning side of the boundary
|
||||
- fix(messaging): gate the boundary on config reads, not only package links
|
||||
- feat(messaging): restart only the channel whose settings changed
|
||||
- feat(container): apply container-runtime-policies live
|
||||
- feat(tools): reconcile MCP servers and tool settings live
|
||||
- feat(messaging): add a per-turn interactive carrier for clarify and picker
|
||||
- feat(gateway): offer ask_user when the turn's channel can carry a question
|
||||
- feat(telegram): carry clarify and picker through the text fallback
|
||||
- fix(webui): pin the dashboard validators to the control plane's
|
||||
- refactor(agent): drop the SOUL upgrade branch with no shipped template
|
||||
- feat(agentworker): answer MCP sampling from the task's model
|
||||
- fix(workflow): bind the remediate worktree before the builder runs
|
||||
- fix(config): carry max_retries into the worker TaskConfig snapshot
|
||||
- fix(agentworker): supply the guardrail engine to every task run
|
||||
|
||||
### archie-agent
|
||||
|
||||
- feat(workflow): declared workflow outputs for wait:true callers
|
||||
- feat(prreview): the operator-approval gate is PRReview's own stage-list entry
|
||||
- feat(staterpc): carry the review gate and re-review rounds, with one guarded response write
|
||||
- feat(prreview): the gate records the review it holds and posts the operator's answer
|
||||
- feat(taskactions): one approval path answers the review gate from every surface
|
||||
- fix(workflow): derive a declared output's captures need for profile selection
|
||||
- fix(storepkg): validate every declared authority grant at the package descriptor
|
||||
- feat(mcp): answer server-initiated sampling from the chat model
|
||||
- feat(container): send registry auth on image pull
|
||||
- feat(messaging): retract ephemeral replies after their TTL
|
||||
- feat(messaging): transcribe Telegram voice notes
|
||||
- fix(egress): take a run's credential kind from its binding, not the Kit
|
||||
- fix(workflow): park environment gate failures without dispatching a builder
|
||||
- feat(messaging): adapter-facing clarify, picker and approval contract
|
||||
- feat(plugins): load plugin and secret-engine files without a restart
|
||||
- fix(workflow): deliver the feasibility PRD to the forge issue
|
||||
- fix(messaging): carry an inbound attachment's bytes to the gateway turn
|
||||
- fix(daemon): resume a retry whose worktree is gone
|
||||
- fix(logging): file a task log record under its own event time
|
||||
- fix(control-plane): unify validation for the domain-managed kinds
|
||||
- feat(webui): let the operator choose the retry worktree mode
|
||||
- fix(messaging): transcribe voice notes on the model-owning side of the boundary
|
||||
- feat(messaging): restart only the channel whose settings changed
|
||||
- feat(container): apply container-runtime-policies live
|
||||
- feat(tools): reconcile MCP servers and tool settings live
|
||||
- feat(messaging): add a per-turn interactive carrier for clarify and picker
|
||||
- feat(agentworker): answer MCP sampling from the task's model
|
||||
- fix(workflow): bind the remediate worktree before the builder runs
|
||||
- fix(config): carry max_retries into the worker TaskConfig snapshot
|
||||
- fix(agentworker): supply the guardrail engine to every task run
|
||||
|
||||
## [1.45.0] - 2026-09-30
|
||||
|
||||
### archied
|
||||
|
||||
@@ -89,8 +89,10 @@ domain migration.
|
||||
reload of `nats.url` logs an error requiring restart to prevent split-brain
|
||||
delivery.
|
||||
- **Container pull policy:** `containers.pull_policy` must be `"missing"`
|
||||
(default) or `"always"`. Private registries return 401 on `"always"` because
|
||||
no auth is sent. Refresh explicitly via `docker compose pull agent`.
|
||||
(default) or `"always"`. Private registries need `containers.registry_auth`
|
||||
(a `SecretRef` resolving to a `registry.AuthConfig` JSON document); without it
|
||||
the pull is anonymous and a private registry answers 401. Refresh explicitly
|
||||
via `docker compose pull agent`.
|
||||
- **Docker Compose:** `docker-compose.yml` carries optional external NATS and
|
||||
the `agent` build stanza. The agent uses a `build` profile so `up -d nats`
|
||||
never starts it as a persistent service.
|
||||
|
||||
@@ -57,6 +57,14 @@ var deletionGate = []bannedCategory{
|
||||
{"gateway runtime", []string{
|
||||
modulePath + "internal/gateway",
|
||||
}},
|
||||
// The PRD names "Direct LLM providers, model runtimes" among what the
|
||||
// Messaging Service must not own or link. These are the in-module
|
||||
// infrastructure packages that construct one; building either here means
|
||||
// a model-provider credential is in the messaging process.
|
||||
{"model providers", []string{
|
||||
modulePath + "internal/infrastructure/transcription",
|
||||
modulePath + "internal/infrastructure/embedding",
|
||||
}},
|
||||
{"dashboard", []string{
|
||||
modulePath + "internal/webui",
|
||||
}},
|
||||
@@ -92,13 +100,8 @@ func TestMessagingProcessLinksNoRuntimeItOnlyDials(t *testing.T) {
|
||||
if excepted(importPath, linked) {
|
||||
continue
|
||||
}
|
||||
for _, category := range deletionGate {
|
||||
for _, prefix := range category.prefixes {
|
||||
if !under(importPath, prefix) {
|
||||
continue
|
||||
}
|
||||
t.Errorf("archie-messaging links %s (%s); the Messaging Service reaches that capability over a contract, never in-process (docs/prds/messaging-service-boundary.md, deletion gate)", importPath, category.name)
|
||||
}
|
||||
if category, banned := bannedLink(importPath); banned {
|
||||
t.Errorf("archie-messaging links %s (%s); the Messaging Service reaches that capability over a contract, never in-process (docs/prds/messaging-service-boundary.md, deletion gate)", importPath, category)
|
||||
}
|
||||
}
|
||||
for _, exception := range gateExceptions {
|
||||
@@ -113,6 +116,40 @@ func TestMessagingProcessLinksNoRuntimeItOnlyDials(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// bannedLink reports the deletion-gate category importPath falls under, or
|
||||
// ("", false). Classifying a single package is the rule the unit test below
|
||||
// pins, so the gate can be exercised without a live `go list`.
|
||||
func bannedLink(importPath string) (category string, banned bool) {
|
||||
for _, c := range deletionGate {
|
||||
for _, prefix := range c.prefixes {
|
||||
if under(importPath, prefix) {
|
||||
return c.name, true
|
||||
}
|
||||
}
|
||||
}
|
||||
return "", false
|
||||
}
|
||||
|
||||
// TestDeletionGateBansModelProviderCapabilities pins the PRD category that the
|
||||
// voice-transcription violation slipped past: the link gate would not flag
|
||||
// anything merely because the process built an LLM client, so the capability
|
||||
// packages are named explicitly. gatewayrpc is checked as the negative, since
|
||||
// it is the permitted contract and must not be swept up by the internal/
|
||||
// infrastructure/ prefix.
|
||||
func TestDeletionGateBansModelProviderCapabilities(t *testing.T) {
|
||||
for _, pkg := range []string{
|
||||
modulePath + "internal/infrastructure/transcription",
|
||||
modulePath + "internal/infrastructure/embedding",
|
||||
} {
|
||||
if _, banned := bannedLink(pkg); !banned {
|
||||
t.Errorf("bannedLink(%q) = not banned; the Messaging Service must not construct a model-provider capability in-process", pkg)
|
||||
}
|
||||
}
|
||||
if category, banned := bannedLink(modulePath + "internal/infrastructure/gatewayrpc"); banned {
|
||||
t.Errorf("bannedLink(gatewayrpc) = %q; the Gateway contract is permitted", category)
|
||||
}
|
||||
}
|
||||
|
||||
// excepted reports whether importPath sits under an excepted package, recording
|
||||
// the ones actually linked so a stale exception fails.
|
||||
func excepted(importPath string, linked map[string]bool) bool {
|
||||
|
||||
@@ -0,0 +1,433 @@
|
||||
// The link gate in architecture_test.go measures which packages the Messaging
|
||||
// binary links. It cannot see a process that links nothing new but still reads
|
||||
// a decoded configuration value it is not allowed to own -- which is exactly
|
||||
// how voice transcription was wired into the Messaging Service while a green
|
||||
// link gate watched: internal/app/archiemessaging/config.go built an LLM
|
||||
// transcriber from [models].transcription and [providers.*], both of which
|
||||
// docs/prds/messaging-service-boundary.md reserves to the model-owning process.
|
||||
//
|
||||
// This file closes that shape of hole: it scans the Messaging Service's own
|
||||
// production sources for a read of a top-level config section the PRD forbids,
|
||||
// so re-adding the projection field fails here rather than shipping green.
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"go/ast"
|
||||
"go/parser"
|
||||
"go/token"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// messagingAppDir is the Messaging Service's application package, relative to
|
||||
// this package's directory (go test runs with the package dir as cwd).
|
||||
const messagingAppDir = "../../internal/app/archiemessaging"
|
||||
|
||||
// bannedConfigFields are the top-level config.Config fields the Messaging
|
||||
// Service must never read, by Go field name. The PRD's "Configuration
|
||||
// Ownership" section permits [chat.*], [services.*], [secrets] and the
|
||||
// path/identity keys the process needs to locate itself; every entry here is a
|
||||
// capability another process owns. internal/config's field names are the
|
||||
// concrete keys, because that is what a Go read names.
|
||||
var bannedConfigFields = map[string]bool{
|
||||
"Forge": true, // [forge]
|
||||
"Repos": true, // [repos]
|
||||
"Models": true, // [models]
|
||||
"Providers": true, // [providers]
|
||||
"Containers": true, // [containers]
|
||||
"NATS": true, // [nats]
|
||||
// [runners] has no top-level field in internal/config today; if one is
|
||||
// added its own name must be listed here with it.
|
||||
}
|
||||
|
||||
// configReadOffenders returns the banned "<holder>.<Field>" config reads in
|
||||
// src, in source order. A holder is anything the file gives the
|
||||
// internal/config.Config type: an explicitly typed parameter, var, receiver or
|
||||
// struct field, a short variable declaration or inferred var whose right-hand
|
||||
// side resolves to one ("cfg := doc.Config"), or a range variable over a slice
|
||||
// of them. The type is tracked by name because a name is what a Go read
|
||||
// selects on. A config value returned by a call this file does not declare is
|
||||
// outside a syntactic rule and stays untracked.
|
||||
func configReadOffenders(src []byte) []string {
|
||||
fset := token.NewFileSet()
|
||||
file, err := parser.ParseFile(fset, "config.go", src, parser.SkipObjectResolution)
|
||||
if err != nil {
|
||||
// A file that does not parse is reported as an offender rather than
|
||||
// ignored: silently skipping it would make this gate blind exactly
|
||||
// when the tree is broken.
|
||||
return []string{"parse error: " + err.Error()}
|
||||
}
|
||||
kinds := configKinds(file)
|
||||
|
||||
var offenders []string
|
||||
ast.Inspect(file, func(n ast.Node) bool {
|
||||
sel, ok := n.(*ast.SelectorExpr)
|
||||
if !ok || !bannedConfigFields[sel.Sel.Name] {
|
||||
return true
|
||||
}
|
||||
switch base := sel.X.(type) {
|
||||
case *ast.Ident:
|
||||
if kinds[base.Name] == configValue {
|
||||
offenders = append(offenders, base.Name+"."+sel.Sel.Name)
|
||||
}
|
||||
case *ast.SelectorExpr:
|
||||
// doc.Config, or the config-typed struct field h.cfg. Config is
|
||||
// named by configuration.Document in another package, so it
|
||||
// cannot come out of the kinds map; treat a selected Config as
|
||||
// a config holder unconditionally.
|
||||
if base.Sel.Name == "Config" || kinds[base.Sel.Name] == configValue {
|
||||
offenders = append(offenders, base.Sel.Name+"."+sel.Sel.Name)
|
||||
}
|
||||
}
|
||||
return true
|
||||
})
|
||||
return offenders
|
||||
}
|
||||
|
||||
// configKind is how an identifier or expression reaches a config.Config value.
|
||||
type configKind int
|
||||
|
||||
const (
|
||||
notConfig configKind = iota // not a config value
|
||||
configValue // config.Config
|
||||
configSlice // []config.Config, [N]config.Config or map[K]config.Config
|
||||
)
|
||||
|
||||
// configKinds resolves the identifiers in file that hold a config.Config, in
|
||||
// two passes because an inferred holder's source must be classified before the
|
||||
// holder's own name can be.
|
||||
func configKinds(file *ast.File) map[string]configKind {
|
||||
kinds := map[string]configKind{}
|
||||
ast.Inspect(file, func(n ast.Node) bool {
|
||||
switch node := n.(type) {
|
||||
case *ast.FuncDecl:
|
||||
classifyFields(node.Recv, kinds)
|
||||
classifyFields(node.Type.Params, kinds)
|
||||
case *ast.ValueSpec:
|
||||
classifyNames(node.Type, node.Names, kinds)
|
||||
case *ast.StructType:
|
||||
for _, field := range node.Fields.List {
|
||||
classifyNames(field.Type, field.Names, kinds)
|
||||
}
|
||||
}
|
||||
return true
|
||||
})
|
||||
// Inferred holders: a single source-order pass suffices because Go
|
||||
// requires a name to be declared before it is referenced.
|
||||
ast.Inspect(file, func(n ast.Node) bool {
|
||||
switch node := n.(type) {
|
||||
case *ast.AssignStmt:
|
||||
if node.Tok != token.DEFINE {
|
||||
return true
|
||||
}
|
||||
classifyValues(node.Lhs, node.Rhs, kinds)
|
||||
case *ast.ValueSpec:
|
||||
// An explicit type was handled above; here only "var cfg =
|
||||
// doc.Config", which infers the same way a := does.
|
||||
if node.Type != nil {
|
||||
return true
|
||||
}
|
||||
names := make([]ast.Expr, len(node.Names))
|
||||
for i, name := range node.Names {
|
||||
names[i] = name
|
||||
}
|
||||
classifyValues(names, node.Values, kinds)
|
||||
case *ast.RangeStmt:
|
||||
if exprKind(node.X, kinds) != configSlice {
|
||||
return true
|
||||
}
|
||||
if id, ok := node.Value.(*ast.Ident); ok {
|
||||
kinds[id.Name] = configValue
|
||||
}
|
||||
}
|
||||
return true
|
||||
})
|
||||
return kinds
|
||||
}
|
||||
|
||||
// classifyValues records each lhs identifier that resolves to a config kind of
|
||||
// the rhs expression in the same position. A shorter lhs or rhs is fine;
|
||||
// position is what pairs a name with its value.
|
||||
func classifyValues(lhs, rhs []ast.Expr, kinds map[string]configKind) {
|
||||
for i, target := range lhs {
|
||||
if i >= len(rhs) {
|
||||
return
|
||||
}
|
||||
id, ok := target.(*ast.Ident)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
if kind := exprKind(rhs[i], kinds); kind != notConfig {
|
||||
kinds[id.Name] = kind
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// classifyFields records every named field in fields whose type holds a
|
||||
// config.Config.
|
||||
func classifyFields(fields *ast.FieldList, kinds map[string]configKind) {
|
||||
if fields == nil {
|
||||
return
|
||||
}
|
||||
for _, field := range fields.List {
|
||||
classifyNames(field.Type, field.Names, kinds)
|
||||
}
|
||||
}
|
||||
|
||||
// classifyNames records names as holding the config kind of typ.
|
||||
func classifyNames(typ ast.Expr, names []*ast.Ident, kinds map[string]configKind) {
|
||||
kind := typeKind(typ)
|
||||
if kind == notConfig {
|
||||
return
|
||||
}
|
||||
for _, name := range names {
|
||||
kinds[name.Name] = kind
|
||||
}
|
||||
}
|
||||
|
||||
// exprKind resolves the config kind of an expression from the identifiers
|
||||
// already classified in kinds.
|
||||
func exprKind(expr ast.Expr, kinds map[string]configKind) configKind {
|
||||
switch e := expr.(type) {
|
||||
case *ast.Ident:
|
||||
return kinds[e.Name]
|
||||
case *ast.SelectorExpr:
|
||||
// doc.Config of a configuration.Document, or a struct field the file
|
||||
// declared as config.Config.
|
||||
if e.Sel.Name == "Config" {
|
||||
return configValue
|
||||
}
|
||||
return kinds[e.Sel.Name]
|
||||
case *ast.CompositeLit:
|
||||
return typeKind(e.Type)
|
||||
case *ast.StarExpr:
|
||||
return exprKind(e.X, kinds)
|
||||
case *ast.UnaryExpr:
|
||||
return exprKind(e.X, kinds)
|
||||
case *ast.ParenExpr:
|
||||
return exprKind(e.X, kinds)
|
||||
case *ast.IndexExpr:
|
||||
if exprKind(e.X, kinds) == configSlice {
|
||||
return configValue
|
||||
}
|
||||
}
|
||||
return notConfig
|
||||
}
|
||||
|
||||
// typeKind reports the config kind a type expression names: config.Config, or
|
||||
// a slice, array or map whose element is one.
|
||||
func typeKind(expr ast.Expr) configKind {
|
||||
switch t := expr.(type) {
|
||||
case *ast.ArrayType:
|
||||
if isConfigType(t.Elt) {
|
||||
return configSlice
|
||||
}
|
||||
case *ast.MapType:
|
||||
// A map's range value is its element; classify the map itself as a
|
||||
// slice-like source so a range over it yields config values.
|
||||
if isConfigType(t.Value) {
|
||||
return configSlice
|
||||
}
|
||||
case *ast.StarExpr:
|
||||
return typeKind(t.X)
|
||||
case *ast.ParenExpr:
|
||||
return typeKind(t.X)
|
||||
}
|
||||
if isConfigType(expr) {
|
||||
return configValue
|
||||
}
|
||||
return notConfig
|
||||
}
|
||||
|
||||
// isConfigType reports whether expr names internal/config's Config type,
|
||||
// directly or behind a pointer.
|
||||
func isConfigType(expr ast.Expr) bool {
|
||||
switch t := expr.(type) {
|
||||
case *ast.SelectorExpr:
|
||||
id, ok := t.X.(*ast.Ident)
|
||||
return ok && id.Name == "config" && t.Sel.Name == "Config"
|
||||
case *ast.StarExpr:
|
||||
return isConfigType(t.X)
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// TestMessagingReadsNoBannedConfigSection is the live gate: every production
|
||||
// source file in the Messaging Service's application package is scanned, and a
|
||||
// read of a PRD-forbidden section fails the build.
|
||||
func TestMessagingReadsNoBannedConfigSection(t *testing.T) {
|
||||
entries, err := os.ReadDir(messagingAppDir)
|
||||
if err != nil {
|
||||
t.Fatalf("read %s: %v", messagingAppDir, err)
|
||||
}
|
||||
scanned := 0
|
||||
for _, entry := range entries {
|
||||
name := entry.Name()
|
||||
if entry.IsDir() || !strings.HasSuffix(name, ".go") || strings.HasSuffix(name, "_test.go") {
|
||||
continue
|
||||
}
|
||||
src, err := os.ReadFile(filepath.Join(messagingAppDir, name))
|
||||
if err != nil {
|
||||
t.Fatalf("read %s: %v", name, err)
|
||||
}
|
||||
scanned++
|
||||
for _, offender := range configReadOffenders(src) {
|
||||
t.Errorf("%s reads %s; the Messaging Service must not read or decode that section (docs/prds/messaging-service-boundary.md, Configuration Ownership) -- the capability belongs to the model-owning process, reached over a contract", name, offender)
|
||||
}
|
||||
}
|
||||
// A gate that read no files would pass silently. Pin that it scanned the
|
||||
// package it names.
|
||||
if scanned == 0 {
|
||||
t.Fatalf("no production .go files found in %s: the gate asserted nothing", messagingAppDir)
|
||||
}
|
||||
}
|
||||
|
||||
// TestConfigReadRuleCatchesTheOldWiring proves the rule fails on a synthetic
|
||||
// copy of the shape that shipped: a projection built by reading
|
||||
// [models].transcription and [providers.*]. Without this, the live gate's rule
|
||||
// could rot into a no-op and still report a pass.
|
||||
func TestConfigReadRuleCatchesTheOldWiring(t *testing.T) {
|
||||
const reverted = `package archiemessaging
|
||||
|
||||
import (
|
||||
"github.com/samcharles93/archie-core/internal/config"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/transcription"
|
||||
)
|
||||
|
||||
func project(cfg config.Config) projection {
|
||||
return projection{
|
||||
models: cfg.Models,
|
||||
providers: cfg.Providers,
|
||||
}
|
||||
}
|
||||
|
||||
func setupTranscriber(cfg config.Config) {
|
||||
_, _ = transcription.New(cfg.Models, cfg.Providers, transcription.Options{})
|
||||
}
|
||||
`
|
||||
got := configReadOffenders([]byte(reverted))
|
||||
want := []string{"cfg.Models", "cfg.Providers", "cfg.Models", "cfg.Providers"}
|
||||
if strings.Join(got, ",") != strings.Join(want, ",") {
|
||||
t.Fatalf("configReadOffenders() = %v, want %v", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
// TestConfigReadRuleCatchesInferredHolders covers the holder shapes the first
|
||||
// cut of this rule missed. It only saw a config.Config written out as a
|
||||
// parameter or var type, so a value reached through an inferred short
|
||||
// variable declaration ("cfg := doc.Config") or held in a struct field
|
||||
// ("h.cfg.Models") left the forbidden [models]/[providers] read intact while
|
||||
// the rule saw no holder to blame. Each case here fails red against that rule:
|
||||
// the live gate would have reported a pass with the violation still shipping.
|
||||
func TestConfigReadRuleCatchesInferredHolders(t *testing.T) {
|
||||
cases := []struct {
|
||||
name string
|
||||
src string
|
||||
want []string
|
||||
}{
|
||||
{
|
||||
name: "short var decl from a Document's Config field",
|
||||
src: `package archiemessaging
|
||||
|
||||
import "github.com/samcharles93/archie-core/internal/infrastructure/configuration"
|
||||
|
||||
func f(doc configuration.Document) {
|
||||
cfg := doc.Config
|
||||
_ = cfg.Models
|
||||
}
|
||||
`,
|
||||
want: []string{"cfg.Models"},
|
||||
},
|
||||
{
|
||||
name: "inferred var without an explicit type",
|
||||
src: `package archiemessaging
|
||||
|
||||
import "github.com/samcharles93/archie-core/internal/infrastructure/configuration"
|
||||
|
||||
func f(doc configuration.Document) {
|
||||
var cfg = doc.Config
|
||||
_ = cfg.Models
|
||||
}
|
||||
`,
|
||||
want: []string{"cfg.Models"},
|
||||
},
|
||||
{
|
||||
name: "short var decl inherits config type from another holder",
|
||||
src: `package archiemessaging
|
||||
|
||||
import "github.com/samcharles93/archie-core/internal/config"
|
||||
|
||||
func f(cfg config.Config) {
|
||||
other := cfg
|
||||
_ = other.Providers
|
||||
}
|
||||
`,
|
||||
want: []string{"other.Providers"},
|
||||
},
|
||||
{
|
||||
name: "config held in a struct field",
|
||||
src: `package archiemessaging
|
||||
|
||||
import "github.com/samcharles93/archie-core/internal/config"
|
||||
|
||||
type holder struct {
|
||||
cfg config.Config
|
||||
}
|
||||
|
||||
func f(h holder) {
|
||||
_ = h.cfg.Providers
|
||||
}
|
||||
`,
|
||||
want: []string{"cfg.Providers"},
|
||||
},
|
||||
{
|
||||
name: "range variable over a slice of config",
|
||||
src: `package archiemessaging
|
||||
|
||||
import "github.com/samcharles93/archie-core/internal/config"
|
||||
|
||||
func f(cfgs []config.Config) {
|
||||
for _, cfg := range cfgs {
|
||||
_ = cfg.Models
|
||||
}
|
||||
}
|
||||
`,
|
||||
want: []string{"cfg.Models"},
|
||||
},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
got := configReadOffenders([]byte(tc.src))
|
||||
if strings.Join(got, ",") != strings.Join(tc.want, ",") {
|
||||
t.Fatalf("configReadOffenders() = %v, want %v", got, tc.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestConfigReadRuleAllowsThePermittedKeys proves the rule is not a blanket
|
||||
// ban on config access: the sections the PRD does permit keep passing, and a
|
||||
// nested chat field is not mistaken for a top-level one.
|
||||
func TestConfigReadRuleAllowsThePermittedKeys(t *testing.T) {
|
||||
const permitted = `package archiemessaging
|
||||
|
||||
import "github.com/samcharles93/archie-core/internal/config"
|
||||
|
||||
func project(cfg config.Config) projection {
|
||||
return projection{
|
||||
telegram: cfg.Chat.Telegram,
|
||||
email: cfg.Chat.Email,
|
||||
gateway: cfg.Services.Get("gateway").Target,
|
||||
workDir: cfg.WorkDir,
|
||||
botUser: cfg.BotUser,
|
||||
healthURL: cfg.Health.URL(),
|
||||
}
|
||||
}
|
||||
`
|
||||
if got := configReadOffenders([]byte(permitted)); len(got) != 0 {
|
||||
t.Fatalf("configReadOffenders(permitted) = %v, want none", got)
|
||||
}
|
||||
}
|
||||
@@ -131,11 +131,11 @@ func (f *fakeGateway) Cancel(context.Context, string) (messaging.ChatCancellatio
|
||||
|
||||
func (f *fakeGateway) SetPersona(context.Context, string, string) (bool, error) { return false, nil }
|
||||
|
||||
func (f *fakeGateway) ApplyTaskAction(context.Context, string, int64, taskstate.Action) (messaging.TaskActionResult, error) {
|
||||
func (f *fakeGateway) ApplyTaskAction(context.Context, string, int64, taskstate.Action, taskactions.ActionPayload) (messaging.TaskActionResult, error) {
|
||||
return messaging.TaskActionResult{}, nil
|
||||
}
|
||||
|
||||
func (f *fakeGateway) ApplyOperatorTaskAction(_ context.Context, actor taskactions.Actor, id int64, action taskstate.Action) (messaging.TaskActionResult, error) {
|
||||
func (f *fakeGateway) ApplyOperatorTaskAction(_ context.Context, actor taskactions.Actor, id int64, action taskstate.Action, _ taskactions.ActionPayload) (messaging.TaskActionResult, error) {
|
||||
f.mu.Lock()
|
||||
defer f.mu.Unlock()
|
||||
f.actions = append(f.actions, fmt.Sprintf("%d:%s", id, action))
|
||||
|
||||
@@ -115,6 +115,11 @@ builder = "openai/gpt-5.4"
|
||||
# (curators first). Unset, or a credential that fails to resolve, degrades
|
||||
# to the capability reporting unavailable -- archied still starts.
|
||||
# embedding = "openai/text-embedding-3-small"
|
||||
# `transcription` is optional: it turns Telegram voice notes into text before
|
||||
# the turn is dispatched, behind a "[voice transcription]" marker. Read by
|
||||
# archie-messaging. Unset, or a provider that does not resolve, leaves the
|
||||
# voice note as its media placeholder -- the turn still runs.
|
||||
# transcription = "openai/whisper-1"
|
||||
|
||||
[providers.openai]
|
||||
class = "openai"
|
||||
@@ -442,6 +447,15 @@ target = "127.0.0.1:9090" # REQUIRED. The State Store gRPC address arch
|
||||
# volume_ttl = "72h" # max age for persistent repo volumes and Git clone caches
|
||||
# pull_policy = "missing" # "missing" or "always"
|
||||
#
|
||||
# A private registry needs a credential the daemon sends on pull (an
|
||||
# anonymous pull always answers 401 there, whatever the policy). The value is
|
||||
# a Docker registry.AuthConfig JSON document -- {"username":"bot","password":"..."}
|
||||
# ("identitytoken"/"registrytoken" are alternatives; "serveraddress" is
|
||||
# optional and defaults to the image's registry). Unset means anonymous pulls,
|
||||
# exactly as before this key existed; a reference that cannot be resolved
|
||||
# warns and pulls anonymously rather than failing the daemon.
|
||||
# registry_auth = { engine = "env", key = "ARCHIE_REGISTRY_AUTH" }
|
||||
#
|
||||
# Agent profiles a workflow selects with `profile: <name>`. A workflow that
|
||||
# names none runs under [containers].image with every tool.
|
||||
# Once the State Store holds container policies, edit profiles in the
|
||||
|
||||
@@ -325,8 +325,10 @@ implemented in different packages:
|
||||
synthetic issue numbers.
|
||||
- Repository labels currently select workflows and mirror task state, coupling
|
||||
domain transitions to forge presentation.
|
||||
- Failure generally becomes `parked`, but failure classification and
|
||||
retryability are not represented explicitly.
|
||||
- Failure generally becomes `parked`, and the failure's classification is no
|
||||
longer implicit: the persisted execution record carries the class the code
|
||||
that parks recorded (the vocabulary lives in `internal/taskstate`), and
|
||||
retryability is read from it rather than inferred from reason text.
|
||||
- Generic observability events are emitted alongside state changes rather than
|
||||
being the authoritative domain events.
|
||||
|
||||
|
||||
@@ -335,16 +335,32 @@ Two properties matter to anyone changing this area:
|
||||
query, so boot records the last published settings and the reload re-applies
|
||||
them from there.
|
||||
- **Live kinds arrive on a watch.** Provider settings, model role assignments,
|
||||
repository policies and scheduling policy are `ApplyMode: live`
|
||||
(archie-core-zfb0.1): the daemon keeps a watch per kind and, on a new
|
||||
repository policies, scheduling policy, tool settings, plugin settings,
|
||||
review settings, channel settings and container runtime policies are
|
||||
`ApplyMode: live`: the daemon keeps a watch per kind
|
||||
(`boot.startRuntimeResourceWatches`, `runtimeResourceKinds`) and, on a new
|
||||
version, re-runs `boot.runtimeConfig` over the published snapshot and
|
||||
republishes through `config.Holder`. A candidate the layered document cannot
|
||||
run is refused before it is published — last-known-good keeps running and
|
||||
the refusal is reported through apply status. The kinds stay
|
||||
restart-required while a startup-built component still holds their value
|
||||
(tool, plugin, container and channel settings today); a kind joins the live
|
||||
list only with a consumer that re-reads it, and the model/provider half of
|
||||
it also rebuilds the gateway chat runtime's provider set and the chat model
|
||||
the refusal is reported through apply status. Tool settings reconcile in this
|
||||
process: the daemon diffs the stored MCP server set against the running
|
||||
providers — connecting added servers, disconnecting removed ones and
|
||||
reconnecting changed ones while an unchanged server keeps running — and
|
||||
rebuilds the `web_fetch` and `minimax` entries, whose config was captured at
|
||||
construction (`boot.reconcileToolSettings`). Container runtime policies
|
||||
join the live set because their consumers re-read the published config: the
|
||||
container pool reads image, pull policy, network, max uptime and concurrency
|
||||
cap on every acquire and the daemon resizes its running task dispatcher from
|
||||
the same publish; the fields the document cannot carry, `registry_auth`
|
||||
above all, stay boot-resolved and remain requires-restart. A kind whose
|
||||
consumer lives in another process reconciles there too: `archie-messaging`
|
||||
re-reads the stored channel settings on the apply-status restamp interval
|
||||
and restarts only the channel whose transport settings changed, while the
|
||||
listen addresses it has bound and the set of channels it composed stay
|
||||
process bindings that take effect on restart. A kind joins the live list
|
||||
only with a consumer that re-reads it; one whose startup-built consumer
|
||||
never does stays restart-required, and the model/provider half of the live
|
||||
set also rebuilds the gateway chat runtime's provider set and the chat model
|
||||
list the chat surfaces offer.
|
||||
|
||||
## Completion criteria
|
||||
|
||||
@@ -202,13 +202,13 @@ actually owned, not by where it used to be built:
|
||||
|
||||
| Seam | Resolution |
|
||||
| ---------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `Version` | Sourced from the Gateway via `ChatSnapshot.Version`. The Messaging Service never stamps or infers a build version; `cmd/archie-messaging` deliberately takes no version ldflags, so it cannot report a partial upgrade as a matched one. |
|
||||
| `Version` | Sourced from the Gateway via `ChatSnapshot.Version`. The Messaging Service never renders its own build into `/version`; `cmd/archie-messaging` does carry a `buildinfo` release stamp (added by `archie-core-k94o`, see the amendment below) for the updater's skew check and update verification, but `/version` still shows the Gateway's block so a partially upgraded deployment cannot report itself as matched. |
|
||||
| `Updates` (`/update`) | Built in the Messaging Service from `[chat.telegram].update_check_command` / `update_install_command` — its own configuration. `Enrich` is dropped: `componentInstallTypeEnricher` reads `[nats]`, which this service must not decode, so the check command's own reported install type stands unmodified. |
|
||||
| `UpdateReportPath` | A local state file under `work_dir`, keyed by the same `sha256(bot_user)[:8]` scheme the daemon used, so a pending report is found rather than written afresh. |
|
||||
| `ReleaseAnnouncements` | **Deliberately left nil**, for the same reason as `RunningVersions`. `releaseannounce.Announcer` announces nothing unless each `Component` carries a parseable version, and the only versions this service can obtain are the Gateway's own build stamps, not archied's. Wiring the announcer without them yields a no-op that reads as configured, so it stays unwired until component self-reporting exists. |
|
||||
| `ReleaseAnnouncements` | **Still left nil**, but no longer for the `RunningVersions` reason (that seam now self-reports — see the amendment below). `releaseannounce.Announcer` announces nothing unless each `Component` carries a parseable version, and the only version this process can vouch for is its own daemon-release stamp, not the agent's. Wiring the announcer without them yields a no-op that reads as configured, so it stays unwired until component self-reporting exists. |
|
||||
| `SetShowToolCalls` | Projected from `[chat].show_tool_calls`. |
|
||||
| `Reload` | Local: re-resolves the token and allowlist from this service's own config file and overlay. Reload was never a daemon fact. |
|
||||
| `RunningVersions` | **Deliberately left nil.** It exists to turn an installer's claim into a checked one, and only a component's own compiled-in build can vouch for it. The Messaging Service knows neither archied's build nor the observed archie-agent version (`daemon.AgentStatus`), and the Gateway's own `gatewayVersion` is a different binary's stamp — reporting it as the daemon's would manufacture exactly the false success the check exists to catch. Update reports therefore relay as unverified claims until a component self-report contract exists. |
|
||||
| `RunningVersions` | **Wired from this process's own release stamp** (`archie-core-ot33`; see the amendment below). `archie-messaging` is one of the processes in the archied release set (the installer's `GATEWAY_SERVICES`), built from the same `archied/v*` tag and stamped with `buildinfo.Version` by every build site, so its compiled-in build is evidence that the release the installer claims actually reached this host. It reports `daemon = buildinfo.Version` and reports nothing for `agent` (task-scoped, observed only through `daemon.AgentStatus`), leaving the agent Unverified. The Gateway's rendered block is deliberately not used: that is a different process's stamp. |
|
||||
| `Dangerous` | Stays nil. No daemon composition ever set it; `/rollback` and `/stop` reported "not configured" before the extraction and still do. |
|
||||
|
||||
The Messaging Service consequently reads `work_dir`, `bot_user`, `[chat]` and
|
||||
@@ -217,6 +217,22 @@ The Messaging Service consequently reads `work_dir`, `bot_user`, `[chat]` and
|
||||
polls from outside to confirm an update came back up; the rest of the PRD's
|
||||
prohibition list is untouched.
|
||||
|
||||
**Amended 2026-10-02 (`archie-core-ot33`, Telegram `RunningVersions`).** The
|
||||
2026-09-19 decision left `RunningVersions` nil because it judged this process
|
||||
could obtain only another binary's stamp. That was true when written and stopped
|
||||
being true three days later: `ec0f17a7` (`archie-core-k94o`) gave every host
|
||||
binary, `archie-messaging` included, a `buildinfo.Version` self-report so the
|
||||
updater could detect per-binary skew. `cmd/archie-messaging` now registers
|
||||
`buildinfo`'s `-version`, and every build site (`install.sh`,
|
||||
`scripts/archie-update-install`, `.github/workflows/deploy.yml`, `Taskfile.yml`)
|
||||
stamps it with the same `archied/v*` release as the rest of `GATEWAY_SERVICES`.
|
||||
The messaging process therefore vouches for the `daemon` component from
|
||||
`buildinfo.Version` — its own compiled-in build, never the Gateway's rendered
|
||||
block — and reports nothing for the agent. A relayed update report can now
|
||||
confirm the daemon and still names the agent as unchecked, while a binary the
|
||||
update did not replace reads as drift. `ReleaseAnnouncements` stays nil: it needs
|
||||
a version for every component, and this process can vouch only for itself.
|
||||
|
||||
**Phase 4 messaging severance: what must move, and the decision the PRD does
|
||||
not cover (2026-09-22, `archie-core-1ng1`).** The PRD's deletion gate cannot
|
||||
pass today. `cmd/archie-messaging/architecture_test.go` does not exist, and the
|
||||
|
||||
@@ -314,7 +314,7 @@
|
||||
"properties": {
|
||||
"data": {
|
||||
"contentEncoding": "base64",
|
||||
"description": "Data carries the attachment bytes in-process. An inbound path sets\nit after downloading the file; outbound senders never set it and\nread Path or URL instead. It never crosses a wire or persistence\nboundary -- it is a turn-scoped value, not a stored one.",
|
||||
"description": "Data carries the attachment bytes in-process. An inbound path sets\nit after downloading the file; outbound senders never set it and\nread Path or URL instead. It crosses the wire in one direction only:\nthe inbound request from a channel frontend to the Gateway process\nthat runs the turn, because that process never holds the platform\ncredential. It is never persisted, and the stored-history and\noutbound-event directions strip it.",
|
||||
"type": "string"
|
||||
},
|
||||
"duration": {
|
||||
@@ -350,7 +350,7 @@
|
||||
"type": "object"
|
||||
},
|
||||
"type": {
|
||||
"description": "Type is \"image\", \"video\", \"audio\", or \"document\".",
|
||||
"description": "Type is \"image\", \"video\", \"audio\", \"voice\", or \"document\".",
|
||||
"type": "string"
|
||||
},
|
||||
"url": {
|
||||
|
||||
@@ -16,7 +16,7 @@
|
||||
"url": "/dev/architecture/agent-system/",
|
||||
"title": "Agent System",
|
||||
"section": "Architecture",
|
||||
"body": "# Agent System\n\n**Status:** Current-state investigation in progress \n**Date:** 2026-07-28 \n**Tracking issue:** [#73](https://github.com/samcharles93/archie-core/issues/73)\n\n## Domain framing\n\nThe domain is the Agent System.\n\n`Task` is not a core Agent System concept. Forge tasks, forge issues, Jira\nissues, email, Telegram messages, webhooks, and future external inputs are\nchannel-specific messages. Channels translate those messages into requests to\nthe Agent System; external issue or task models MUST NOT shape execution state.\n\nThe current `store.Task` is a persisted workflow execution. Treating that row as\na task-domain aggregate would incorrectly turn Archie into an agentic issue\nboard.\n\n## Canonical agent interaction model\n\nAn **Agent** is a persistent assistant selected for an interaction. It is not a\nmodel invocation and is not a WorkflowExecution.\n\nEach Agent has its own:\n\n- immutable acting `IdentityID` for ownership and audit;\n- durable memory and learned information about the user;\n- perspective, specialisation, instructions, and communication behaviour;\n- conversation history and channel bindings;\n- available tools, capabilities, and policy;\n- continuity across model or provider changes.\n\nThe underlying model is a replaceable runtime dependency. Changing a model,\nprovider, or model version MUST NOT create a new Agent, discard its memory, or\nchange ownership of its history.\n\nA user MAY create multiple Agents. Each Agent develops its own memory and view\nof the user from the interactions and responsibilities assigned to it. Memories\nMUST NOT be silently pooled merely because Agents belong to the same user or\napplication.\n\nAgent and Identity are separate aggregates. Every Agent references exactly one\nacting IdentityID, and no two Agents share an agent identity. User, system, and\nservice-account identities may exist without an Agent.\n\nAn action initiated through an Agent records the Agent's acting IdentityID and,\nwhen applicable, the initiating user's IdentityID. A future fully automated\nWorkflowExecution MAY use an explicitly selected service-account identity for a\nconcrete unattended use case; this is not the default execution identity.\n\n## Workflow execution identity\n\nA Workflow is owned by an IdentityID. By default, every WorkflowExecution acts\nas the Workflow owner's identity.\n\nFuture delegated execution MAY add an explicit `RunAsIdentityID` assignment to a\nWorkflow. This allows a Workflow owner to select another user or service-account\nidentity for execution without changing Workflow ownership. Until that\ncapability is deliberately introduced, owner identity and execution identity are\nthe same.\n\nWorkflowExecution audit records MUST preserve the Workflow owner, effective\nexecution identity, initiating user when one exists, and any explicit delegation\nthat selected a different run-as identity.\n\n## Memory scopes\n\nThe Agent System requires four distinct durable memory scopes:\n\n- **global shared memory**: information intentionally available across the\n Archie application;\n- **agent-wide memory**: general expertise, operating knowledge, and perspective\n owned by one Agent and safe for that Agent to use across users;\n- **user-wide memory**: information associated with one user and available to\n authorised Agents serving that user;\n- **Agent-user relationship memory**: private history and learned context from\n one particular Agent's relationship with one particular user.\n\nAgent-wide memory is private to its owning Agent by default. Another Agent MUST\nNOT read, inherit, search, or mutate it implicitly.\n\nAgent-user relationship memory is addressed by both AgentID and User IdentityID.\nNeither another Agent serving the same user nor another user speaking to the\nsame Agent may access it implicitly.\n\nArchie is a multi-user system even when initially deployed for one person.\nMultiple users, including family members, may interact with the same configured\nAgents. Per-user memory is therefore an isolation and personalisation boundary,\nnot merely an attribution tag on the primary user's records.\n\nAn Agent MUST resolve the initiating user before assembling memory context.\nMemory belonging to one user MUST NOT become visible to another user merely\nbecause both users interact with the same Agent.\n\nShared memory does not erase provenance. Every memory record retains its scope,\nauthor or acting identity, originating user when applicable, source, and\nrevision history. The rules for selecting a write scope, granting access\nto global and user-wide memory, and authorising explicit cross-scope sharing are\nsettled by [`docs/prds/memory-engine-unification.md`](/dev/prds/memory-engine-unification/)\nand recorded in [`migration-decisions.md`](/dev/architecture/migration-decisions/#5-memory-placement-and-storage)\nsection 5.\n\nAn Agent interaction may answer conversationally, use a tool or capability, read\norLine truncated
|
||||
"body": "# Agent System\n\n**Status:** Current-state investigation in progress \n**Date:** 2026-07-28 \n**Tracking issue:** [#73](https://github.com/samcharles93/archie-core/issues/73)\n\n## Domain framing\n\nThe domain is the Agent System.\n\n`Task` is not a core Agent System concept. Forge tasks, forge issues, Jira\nissues, email, Telegram messages, webhooks, and future external inputs are\nchannel-specific messages. Channels translate those messages into requests to\nthe Agent System; external issue or task models MUST NOT shape execution state.\n\nThe current `store.Task` is a persisted workflow execution. Treating that row as\na task-domain aggregate would incorrectly turn Archie into an agentic issue\nboard.\n\n## Canonical agent interaction model\n\nAn **Agent** is a persistent assistant selected for an interaction. It is not a\nmodel invocation and is not a WorkflowExecution.\n\nEach Agent has its own:\n\n- immutable acting `IdentityID` for ownership and audit;\n- durable memory and learned information about the user;\n- perspective, specialisation, instructions, and communication behaviour;\n- conversation history and channel bindings;\n- available tools, capabilities, and policy;\n- continuity across model or provider changes.\n\nThe underlying model is a replaceable runtime dependency. Changing a model,\nprovider, or model version MUST NOT create a new Agent, discard its memory, or\nchange ownership of its history.\n\nA user MAY create multiple Agents. Each Agent develops its own memory and view\nof the user from the interactions and responsibilities assigned to it. Memories\nMUST NOT be silently pooled merely because Agents belong to the same user or\napplication.\n\nAgent and Identity are separate aggregates. Every Agent references exactly one\nacting IdentityID, and no two Agents share an agent identity. User, system, and\nservice-account identities may exist without an Agent.\n\nAn action initiated through an Agent records the Agent's acting IdentityID and,\nwhen applicable, the initiating user's IdentityID. A future fully automated\nWorkflowExecution MAY use an explicitly selected service-account identity for a\nconcrete unattended use case; this is not the default execution identity.\n\n## Workflow execution identity\n\nA Workflow is owned by an IdentityID. By default, every WorkflowExecution acts\nas the Workflow owner's identity.\n\nFuture delegated execution MAY add an explicit `RunAsIdentityID` assignment to a\nWorkflow. This allows a Workflow owner to select another user or service-account\nidentity for execution without changing Workflow ownership. Until that\ncapability is deliberately introduced, owner identity and execution identity are\nthe same.\n\nWorkflowExecution audit records MUST preserve the Workflow owner, effective\nexecution identity, initiating user when one exists, and any explicit delegation\nthat selected a different run-as identity.\n\n## Memory scopes\n\nThe Agent System requires four distinct durable memory scopes:\n\n- **global shared memory**: information intentionally available across the\n Archie application;\n- **agent-wide memory**: general expertise, operating knowledge, and perspective\n owned by one Agent and safe for that Agent to use across users;\n- **user-wide memory**: information associated with one user and available to\n authorised Agents serving that user;\n- **Agent-user relationship memory**: private history and learned context from\n one particular Agent's relationship with one particular user.\n\nAgent-wide memory is private to its owning Agent by default. Another Agent MUST\nNOT read, inherit, search, or mutate it implicitly.\n\nAgent-user relationship memory is addressed by both AgentID and User IdentityID.\nNeither another Agent serving the same user nor another user speaking to the\nsame Agent may access it implicitly.\n\nArchie is a multi-user system even when initially deployed for one person.\nMultiple users, including family members, may interact with the same configured\nAgents. Per-user memory is therefore an isolation and personalisation boundary,\nnot merely an attribution tag on the primary user's records.\n\nAn Agent MUST resolve the initiating user before assembling memory context.\nMemory belonging to one user MUST NOT become visible to another user merely\nbecause both users interact with the same Agent.\n\nShared memory does not erase provenance. Every memory record retains its scope,\nauthor or acting identity, originating user when applicable, source, and\nrevision history. The rules for selecting a write scope, granting access\nto global and user-wide memory, and authorising explicit cross-scope sharing are\nsettled by [`docs/prds/memory-engine-unification.md`](/dev/prds/memory-engine-unification/)\nand recorded in [`migration-decisions.md`](/dev/architecture/migration-decisions/#5-memory-placement-and-storage)\nsection 5.\n\nAn Agent interaction may answer conversationally, use a tool or capability, read\norLine truncated
|
||||
},
|
||||
{
|
||||
"url": "/dev/architecture/bindings/",
|
||||
@@ -28,7 +28,7 @@
|
||||
"url": "/dev/architecture/configuration/",
|
||||
"title": "Configuration",
|
||||
"section": "Architecture",
|
||||
"body": "# Configuration\n\n**Status:** Approved; existing-type assignment is ongoing \n**Date:** 2026-07-28 \n**Tracking issue:** [#73](https://github.com/samcharles93/archie-core/issues/73)\n\n## Ownership\n\nConfiguration has two separate responsibilities:\n\n1. `internal/infrastructure/configuration` loads files, environment variables,\n secret references, overlays, and compatibility representations.\n2. Each domain and plugin owns the typed runtime settings required for its\n behaviour.\n\n`internal/app` translates external input into those settings, validates\ncross-capability combinations, and supplies each component only its own\nsettings.\n\nThe following are prohibited:\n\n- passing complete application configuration into a domain;\n- making a domain depend on TOML, YAML, environment-variable names, secret\n references, or a configuration library;\n- defining a central runtime configuration type imported across the system;\n- placing plugin options in an unrelated global configuration model.\n\n## Private input document\n\n`internal/infrastructure/configuration` MAY use a complete decoded document\nwhile discovering files, decoding sections, and applying overlays. It is an\ninput-boundary DTO only.\n\nThe document:\n\n- remains private to the configuration infrastructure;\n- is composed from separately defined feature sections;\n- is immediately translated by application composition;\n- is never returned as the application's runtime model;\n- is never passed through runtime components.\n\nExternal DTOs and domain runtime settings are separate types.\n\nPlugins supply external configuration definitions through a narrow extension\ncontract. The core loader MUST NOT gain a field or recognised filename for every\nplugin.\n\nInvalid configuration input MUST NOT replace active valid settings or terminate\nunaffected capabilities. Configuration changes use the candidate validation,\npromotion, audit, and recovery requirements in\n[safe-change-and-recovery.md](/dev/architecture/safe-change-and-recovery/).\n\n## Dissolution of `internal/config`\n\n`internal/config` has no target location. It MUST be dissolved and MUST NOT\nsurvive as a shared package, configuration domain, or renamed global model.\n\nIts loading behaviour moves to `internal/infrastructure/configuration`. Existing\ntypes and methods are reassigned by behaviour. Repository execution policy,\ndispatch policy, retry policy, ecosystem defaults, tool policy, memory settings,\nprovider settings, and channel settings move to their\nrespective owners.\n\n## Repository settings\n\nThe existing `config.Repo` MUST NOT move intact into a repository domain or\nshared settings package. A managed repository is an application association:\n\n- work intake owns which repository references an identity monitors;\n- task execution owns gates, preflight, test protection, ecosystem behaviour,\n and change limits;\n- task lifecycle owns retry behaviour;\n- scheduling owns concurrency policy;\n- workspace management owns base branch and persistent-storage requirements.\n\nApplication composition correlates these settings by repository reference. No\ndomain receives a universal repository profile.\n\n## Documentation\n\nGenerated configuration reference describes external sections and their owning\ncapabilities. It MUST NOT establish a global Go configuration structure as the\npublic contract.\n\n## Value classification rule\n\nEvery value that controls behaviour MUST have an explicit owner and MUST be\nclassified as exactly one of:\n\n1. **Invariant** — changing it changes the meaning of the program, a wire\n protocol, or a domain concept. It may be a Go constant.\n2. **Runtime setting** — an operator, identity, domain, plugin, or deployment\n may legitimately choose a different value. It is typed configuration owned by\n the affected capability.\n3. **Policy value** — it expresses what is allowed, limited, routed, retried,\n protected, or gated. Its typed definition belongs to the owning domain and it\n is evaluated through the policy system.\n4. **Derived value** — it is computed from other settings and is not\n independently mutable.\n5. **Runtime state** — it changes as the application operates, such as the\n active model for a conversation. It is changed by a domain command and is not\n configuration merely because it can be changed.\n\nAn unclassified behavioural literal is a defect.\n\n`const` MUST NOT be used merely because a value currently has one default.\nConstants are reserved for actual invariants: enum discriminants, protocol\nversions, fixed compatibility tokens, and values whose alteration would change\nthe program's semantics rather than configure an installation.\n\nIn Go, a constant is a compile-time value and does not represent mutable runtime\nstorage. It generally has no address that can be observed or changed.\nArchitecturally, a constant therefore asserts that the value cannot change for\nthe lifetiLine truncated
|
||||
"body": "# Configuration\n\n**Status:** Approved; existing-type assignment is ongoing \n**Date:** 2026-07-28 \n**Tracking issue:** [#73](https://github.com/samcharles93/archie-core/issues/73)\n\n## Ownership\n\nConfiguration has two separate responsibilities:\n\n1. `internal/infrastructure/configuration` loads files, environment variables,\n secret references, overlays, and compatibility representations.\n2. Each domain and plugin owns the typed runtime settings required for its\n behaviour.\n\n`internal/app` translates external input into those settings, validates\ncross-capability combinations, and supplies each component only its own\nsettings.\n\nThe following are prohibited:\n\n- passing complete application configuration into a domain;\n- making a domain depend on TOML, YAML, environment-variable names, secret\n references, or a configuration library;\n- defining a central runtime configuration type imported across the system;\n- placing plugin options in an unrelated global configuration model.\n\n## Private input document\n\n`internal/infrastructure/configuration` MAY use a complete decoded document\nwhile discovering files, decoding sections, and applying overlays. It is an\ninput-boundary DTO only.\n\nThe document:\n\n- remains private to the configuration infrastructure;\n- is composed from separately defined feature sections;\n- is immediately translated by application composition;\n- is never returned as the application's runtime model;\n- is never passed through runtime components.\n\nExternal DTOs and domain runtime settings are separate types.\n\nPlugins supply external configuration definitions through a narrow extension\ncontract. The core loader MUST NOT gain a field or recognised filename for every\nplugin.\n\nInvalid configuration input MUST NOT replace active valid settings or terminate\nunaffected capabilities. Configuration changes use the candidate validation,\npromotion, audit, and recovery requirements in\n[safe-change-and-recovery.md](/dev/architecture/safe-change-and-recovery/).\n\n## Dissolution of `internal/config`\n\n`internal/config` has no target location. It MUST be dissolved and MUST NOT\nsurvive as a shared package, configuration domain, or renamed global model.\n\nIts loading behaviour moves to `internal/infrastructure/configuration`. Existing\ntypes and methods are reassigned by behaviour. Repository execution policy,\ndispatch policy, retry policy, ecosystem defaults, tool policy, memory settings,\nprovider settings, and channel settings move to their\nrespective owners.\n\n## Repository settings\n\nThe existing `config.Repo` MUST NOT move intact into a repository domain or\nshared settings package. A managed repository is an application association:\n\n- work intake owns which repository references an identity monitors;\n- task execution owns gates, preflight, test protection, ecosystem behaviour,\n and change limits;\n- task lifecycle owns retry behaviour;\n- scheduling owns concurrency policy;\n- workspace management owns base branch and persistent-storage requirements.\n\nApplication composition correlates these settings by repository reference. No\ndomain receives a universal repository profile.\n\n## Documentation\n\nGenerated configuration reference describes external sections and their owning\ncapabilities. It MUST NOT establish a global Go configuration structure as the\npublic contract.\n\n## Value classification rule\n\nEvery value that controls behaviour MUST have an explicit owner and MUST be\nclassified as exactly one of:\n\n1. **Invariant** — changing it changes the meaning of the program, a wire\n protocol, or a domain concept. It may be a Go constant.\n2. **Runtime setting** — an operator, identity, domain, plugin, or deployment\n may legitimately choose a different value. It is typed configuration owned by\n the affected capability.\n3. **Policy value** — it expresses what is allowed, limited, routed, retried,\n protected, or gated. Its typed definition belongs to the owning domain and it\n is evaluated through the policy system.\n4. **Derived value** — it is computed from other settings and is not\n independently mutable.\n5. **Runtime state** — it changes as the application operates, such as the\n active model for a conversation. It is changed by a domain command and is not\n configuration merely because it can be changed.\n\nAn unclassified behavioural literal is a defect.\n\n`const` MUST NOT be used merely because a value currently has one default.\nConstants are reserved for actual invariants: enum discriminants, protocol\nversions, fixed compatibility tokens, and values whose alteration would change\nthe program's semantics rather than configure an installation.\n\nIn Go, a constant is a compile-time value and does not represent mutable runtime\nstorage. It generally has no address that can be observed or changed.\nArchitecturally, a constant therefore asserts that the value cannot change for\nthe lifetiLine truncated
|
||||
},
|
||||
{
|
||||
"url": "/dev/architecture/dashboard-agent-tools/",
|
||||
@@ -64,7 +64,7 @@
|
||||
"url": "/dev/architecture/migration-decisions/",
|
||||
"title": "Architecture Migration Decisions",
|
||||
"section": "Architecture",
|
||||
"body": "# Architecture Migration Decisions\n\n**Status:** Active migration inventory \n**Date:** 2026-07-28 \n**Tracking issue:** [#73](https://github.com/samcharles93/archie-core/issues/73)\n\n## Purpose\n\nThis document records what remains to be decided before Archie's current\nimplementation can be migrated into the approved domain-oriented structure.\n\nThe product and core domain model are not being redesigned. Existing Archie\nbehaviour and the existing session implementation are the migration\nbaseline. Remaining decisions MUST be grounded in current packages, consumers,\ntests, persistence, and runtime wiring.\n\n## Confirmed domain packages\n\n```text\ninternal/domain/\n identity/ (approved, not yet materialised as a package)\n agent/ (approved, not yet materialised as a package)\n workflow/\n messaging/ (canonical Message/Conversation types only; gateway migration pending, see section 2)\n workintake/\n plugin/ (approved, not yet materialised as a package)\n curator/\n mapping/\n scheduling/\n```\n\n- `identity` owns persistent actors, lifecycle, ownership, attribution, and\n audit identity.\n- `agent` owns persistent assistants, specialisation, instructions,\n model-independent continuity, capability context, and associations with\n identity and memory.\n- `workflow` is a separate domain. It owns reusable, versioned Workflow\n definitions, WorkflowSteps, WorkflowExecutions, StepExecutions, Attempts,\n outcomes, and typed Workflow plugin contracts.\n- `messaging` owns canonical Messages, Conversations, branches, channel and\n source correlation, replies, acknowledgements, delivery attempts, events, and\n projections.\n- `workintake` owns admission, validation, deterministic routing, and accepted\n work requests for the optional transition into durable workflow-backed\n execution.\n- `plugin` owns generic plugin identity, discovery, compatibility, and lifecycle\n mechanics. It remains metadata-only; capability semantics and typed\n registration belong to the owning domain.\n- `curator` owns the curator engine family: long-running agent loops that\n reason over memory and perform maintenance tasks on a declared shape\n (check-in interval, tool set, attached memory engine). See epic\n archie-core-yp9.\n- `mapping` owns the payload field-mapping vocabulary: binding named fields to\n JSON paths inside a captured webhook payload, and detecting drift. See\n `docs/prds/payload-field-mapping.md`.\n- `scheduling` owns the ticker engine: deciding when a scheduled job runs and\n with what concurrency. It declares `JobSource`/`Runner` contracts and owns\n no storage or delivery. See epic archie-core-1786637496320-249-be6d9a20.\n\n**Reconciliation (2026-08-31):** `curator`, `mapping`, and `scheduling` existed\nin the tree without being added to this list when their epics landed. They are\nconfirmed as legitimate, narrowly-scoped domains under the same rules as the\noriginal six — each owns one capability family, declares typed contracts, and\nhas no cross-domain reach. This list was out of date, not the tree.\n\nConversely, `identity`, `agent`, `messaging`, and `plugin` are approved target\ndomains that do not yet exist as packages. Their behaviour currently lives in\npre-migration packages (see \"Complete package destination map\" below for the\nfull inventory). Do **not** create empty stub packages for them ahead of an\nactual migration PR — an unused stub is dead code and gives no test coverage.\nEach materialises when its corresponding migration decision below is resolved\nand implemented in the same change.\n\nMemory's final package placement remains undecided pending focused review of the\nexisting implementation.\n\nThe following are approved shared or composition packages, not domains:\n\n```text\ninternal/eventbus/\ninternal/policy/\ninternal/infrastructure/\ninternal/app/archied/\ninternal/app/agentworker/\n```\n\n## Remaining migration decisions\n\n### 1. Complete package destination map\n\nEvery current package MUST receive one explicit disposition:\n\n- move into a confirmed domain;\n- move under infrastructure as an implementation of a domain-owned contract;\n- move into application composition;\n- merge with an identified duplicate;\n- remain as an approved shared application contract;\n- move to a plugin, extra, deployment, example, or generated artifact;\n- be deleted after its replacement is proven.\n\nThe largest unreviewed areas include memory, tools and capabilities, workspace\nand worktree behaviour, gates, storage, events, web UI, containers, RPC\npackages, release management, and scheduling.\n\nThe destination matrix MUST name the behaviour owner, target path, dependencies\nto remove, state and contracts to preserve, migration prerequisites, and\ndeletion criteria.\n\n**`internal/webui` / UI Service — RATIFIED Phase 3 disposition (2026-09-07).**\nThe current owner is the in-process HTTP/SSE dashboard: SPA hosting, operator\nauthLine truncated
|
||||
"body": "# Architecture Migration Decisions\n\n**Status:** Active migration inventory \n**Date:** 2026-07-28 \n**Tracking issue:** [#73](https://github.com/samcharles93/archie-core/issues/73)\n\n## Purpose\n\nThis document records what remains to be decided before Archie's current\nimplementation can be migrated into the approved domain-oriented structure.\n\nThe product and core domain model are not being redesigned. Existing Archie\nbehaviour and the existing session implementation are the migration\nbaseline. Remaining decisions MUST be grounded in current packages, consumers,\ntests, persistence, and runtime wiring.\n\n## Confirmed domain packages\n\n```text\ninternal/domain/\n identity/ (approved, not yet materialised as a package)\n agent/ (approved, not yet materialised as a package)\n workflow/\n messaging/ (canonical Message/Conversation types only; gateway migration pending, see section 2)\n workintake/\n plugin/ (approved, not yet materialised as a package)\n curator/\n mapping/\n scheduling/\n```\n\n- `identity` owns persistent actors, lifecycle, ownership, attribution, and\n audit identity.\n- `agent` owns persistent assistants, specialisation, instructions,\n model-independent continuity, capability context, and associations with\n identity and memory.\n- `workflow` is a separate domain. It owns reusable, versioned Workflow\n definitions, WorkflowSteps, WorkflowExecutions, StepExecutions, Attempts,\n outcomes, and typed Workflow plugin contracts.\n- `messaging` owns canonical Messages, Conversations, branches, channel and\n source correlation, replies, acknowledgements, delivery attempts, events, and\n projections.\n- `workintake` owns admission, validation, deterministic routing, and accepted\n work requests for the optional transition into durable workflow-backed\n execution.\n- `plugin` owns generic plugin identity, discovery, compatibility, and lifecycle\n mechanics. It remains metadata-only; capability semantics and typed\n registration belong to the owning domain.\n- `curator` owns the curator engine family: long-running agent loops that\n reason over memory and perform maintenance tasks on a declared shape\n (check-in interval, tool set, attached memory engine). See epic\n archie-core-yp9.\n- `mapping` owns the payload field-mapping vocabulary: binding named fields to\n JSON paths inside a captured webhook payload, and detecting drift. See\n `docs/prds/payload-field-mapping.md`.\n- `scheduling` owns the ticker engine: deciding when a scheduled job runs and\n with what concurrency. It declares `JobSource`/`Runner` contracts and owns\n no storage or delivery. See epic archie-core-1786637496320-249-be6d9a20.\n\n**Reconciliation (2026-08-31):** `curator`, `mapping`, and `scheduling` existed\nin the tree without being added to this list when their epics landed. They are\nconfirmed as legitimate, narrowly-scoped domains under the same rules as the\noriginal six — each owns one capability family, declares typed contracts, and\nhas no cross-domain reach. This list was out of date, not the tree.\n\nConversely, `identity`, `agent`, `messaging`, and `plugin` are approved target\ndomains that do not yet exist as packages. Their behaviour currently lives in\npre-migration packages (see \"Complete package destination map\" below for the\nfull inventory). Do **not** create empty stub packages for them ahead of an\nactual migration PR — an unused stub is dead code and gives no test coverage.\nEach materialises when its corresponding migration decision below is resolved\nand implemented in the same change.\n\nMemory's final package placement remains undecided pending focused review of the\nexisting implementation.\n\nThe following are approved shared or composition packages, not domains:\n\n```text\ninternal/eventbus/\ninternal/policy/\ninternal/infrastructure/\ninternal/app/archied/\ninternal/app/agentworker/\n```\n\n## Remaining migration decisions\n\n### 1. Complete package destination map\n\nEvery current package MUST receive one explicit disposition:\n\n- move into a confirmed domain;\n- move under infrastructure as an implementation of a domain-owned contract;\n- move into application composition;\n- merge with an identified duplicate;\n- remain as an approved shared application contract;\n- move to a plugin, extra, deployment, example, or generated artifact;\n- be deleted after its replacement is proven.\n\nThe largest unreviewed areas include memory, tools and capabilities, workspace\nand worktree behaviour, gates, storage, events, web UI, containers, RPC\npackages, release management, and scheduling.\n\nThe destination matrix MUST name the behaviour owner, target path, dependencies\nto remove, state and contracts to preserve, migration prerequisites, and\ndeletion criteria.\n\n**`internal/webui` / UI Service — RATIFIED Phase 3 disposition (2026-09-07).**\nThe current owner is the in-process HTTP/SSE dashboard: SPA hosting, operator\nauthLine truncated
|
||||
},
|
||||
{
|
||||
"url": "/dev/architecture/observability/",
|
||||
@@ -130,7 +130,7 @@
|
||||
"url": "/dev/development/frontend-ui/",
|
||||
"title": "Frontend UI",
|
||||
"section": "Development",
|
||||
"body": "# Frontend UI\n\nAuthority: `.agents/skills/archie-web-ui` (stack facts, design-system rules,\ntone and accessibility). This page covers what a change must reach, not how it\nshould look.\n\n## Adding or changing a feature\n\n1. The feature lives in `ui/src/\u003cfeature\u003e/`. Move a helper to `ui/src/base/`\n only when a second feature needs it.\n2. Keep wire shapes, draft state and payload building in a plain `.ts` module\n beside the components (`ui/src/bindings/binding-draft.ts` is the pattern),\n and test it in `ui/test/*.test.ts`. Components stay thin.\n3. Every mutation goes through `api.*` in `ui/src/lib/api`, which sends the\n CSRF and Content-Type headers the server's mutation guard requires. A bare\n `fetch` is refused.\n4. A value the server also describes (task statuses, actions, workflow inputs)\n comes from the server. Keep a local default only so the first render works,\n and keep it in step (`ui/src/lib/task-meta.ts`).\n5. Run `task ui`, `task test:ui` and `cd ui \u0026\u0026 npm run typecheck`. Commit the\n regenerated `ui/dist/`; the Go binaries embed it. A fresh git worktree has\n no ignored `ui/node_modules`: run `cd ui \u0026\u0026 npm ci` before these commands.\n6. **One long-lived browser connection:** `ui/src/lib/api.ts` has the only\n `new EventSource`, owned by `stores/live-updates.ts`. Feature stores consume\n topics with `live.subscribe(topic, callback)`; do not open a page-owned SSE\n stream. Logs alone opt in while mounted, replacing that same EventSource\n with `?topics=logs\u0026since=\u003clast cursor\u003e`; a browser's `Last-Event-ID` only\n survives its own automatic reconnect, not a newly constructed EventSource.\n Task and log topics resume by cursor; control-plane, identity and apply-status\n topics replay the latest snapshot. Test a fetch and same-origin navigation\n after visiting Settings and Logs: the UI process serves HTTP/1.1 and six\n persistent streams can starve every later browser request.\n\n## Testing the dashboard\n\nAuthority: the runner itself is `ui/package.json`'s `test` script: `node --test\n--experimental-strip-types test/*.test.ts` over `ui/test/*.test.ts`. It runs\npure TypeScript modules with `node`'s type stripping — no SFC loader, no DOM,\nno second dependency beyond what the app already carries. That is the standard,\nand a decision record for when it is deliberately not enough:\n\n- **Test archie's logic in a module; leave SFC wiring alone.** Wire shapes,\n draft state, copy conditions, filters and payload building live in plain\n `.ts` files beside the components and are tested in `ui/test/`. Components\n stay thin, so there is normally nothing in an SFC that is archie logic.\n- **Installed primitives are upstream-tested, not re-tested here.**\n `reka-ui` (2.10.5 here) maintains colocated tests per component in its own\n repo (Vitest/jsdom + axe: Switch, TagsInput, NumberField, Select, the\n arrow-navigation composables). The npm package ships none of those files, so\n this coverage is inherited through the version bound, not the tarball —\n `grep -l test node_modules/reka-ui` finds nothing. Duplicating them as local\n mount tests would test a dependency.\n- **A `.vue` import cannot run under the module runner.** `node --test` fails\n such a module with `ERR_UNKNOWN_FILE_EXTENSION: .vue` and the runner has no\n built-in loader for SFCs. A file under `ui/test/` that imports a `.vue` is\n therefore a broken test, not a weak one.\n- **If a DOM mount harness is ever needed** (a page-level accessibility\n contract, cross-component interaction, focus behaviour): `happy-dom` +\n `@vue/test-utils` + `vitest` scoped to a separate DOM test glob, keeping the\n module runner for everything else. Measured on the ui tree (vitest 5.0.2,\n happy-dom 20.14.5, @vue/test-utils 2.5.1): they add 24 top-level\n `node_modules` entries and 27 MB on a 296 MB install, a working 2-test mount\n suite (role=radiogroup vs role=switch, aria-label, click activation against\n the real SFCs) runs in 1.2 s on top of the current 0.5 s module suite, and\n `vue-tsc` (7.4 s) dominates `task test:ui` either way. The single-runner\n alternative (absorbing the existing suite into vitest) was measured\n infeasible as-is: all 29 existing files import `node:test`, which vitest\n cannot collect (\"No test suite found in file\" for every one), so it would be\n a rewrite of the whole suite, and its runtime was not measured. Nothing in\n `ui/dist` or CI changes either way; happy-dom is pure JS, no browser binary.\n- **What the module standard does not verify:** whether a call site passes an\n `aria-label` or which `v-if` branch shows. Those live in SFC templates and\n are only visible with a mount. If a page's accessible-name wiring is the\n risk, say so in the page's task and reach for the harness decision above\n rather than quietly growing mount tests. Never suppress a lint rule or a\n test to keep Line truncated
|
||||
"body": "# Frontend UI\n\nAuthority: `.agents/skills/archie-web-ui` (stack facts, design-system rules,\ntone and accessibility). This page covers what a change must reach, not how it\nshould look.\n\n## Adding or changing a feature\n\n1. The feature lives in `ui/src/\u003cfeature\u003e/`. Move a helper to `ui/src/base/`\n only when a second feature needs it.\n2. Keep wire shapes, draft state and payload building in a plain `.ts` module\n beside the components (`ui/src/bindings/binding-draft.ts` is the pattern),\n and test it in `ui/test/*.test.ts`. Components stay thin.\n3. Every mutation goes through `api.*` in `ui/src/lib/api`, which sends the\n CSRF and Content-Type headers the server's mutation guard requires. A bare\n `fetch` is refused.\n4. A value the server also describes (task statuses, actions, workflow inputs)\n comes from the server. Keep a local default only so the first render works,\n and keep it in step (`ui/src/lib/task-meta.ts`).\n5. Run `task ui`, `task test:ui` and `cd ui \u0026\u0026 npm run typecheck`. Commit the\n regenerated `ui/dist/`; the Go binaries embed it. A fresh git worktree has\n no ignored `ui/node_modules`: run `cd ui \u0026\u0026 npm ci` before these commands.\n6. **One long-lived browser connection:** `ui/src/lib/api.ts` has the only\n `new EventSource`, owned by `stores/live-updates.ts`. Feature stores consume\n topics with `live.subscribe(topic, callback)`; do not open a page-owned SSE\n stream. Logs alone opt in while mounted, replacing that same EventSource\n with `?topics=logs\u0026since=\u003clast cursor\u003e`; a browser's `Last-Event-ID` only\n survives its own automatic reconnect, not a newly constructed EventSource.\n Task and log topics resume by cursor; control-plane, identity and apply-status\n topics replay the latest snapshot. Test a fetch and same-origin navigation\n after visiting Settings and Logs: the UI process serves HTTP/1.1 and six\n persistent streams can starve every later browser request.\n\n## Testing the dashboard\n\nAuthority: the runner itself is `ui/package.json`'s `test` script: `node --test\n--experimental-strip-types test/*.test.ts` over `ui/test/*.test.ts`. It runs\npure TypeScript modules with `node`'s type stripping — no SFC loader, no DOM,\nno second dependency beyond what the app already carries. That is the standard,\nand a decision record for when it is deliberately not enough:\n\n- **Test archie's logic in a module; leave SFC wiring alone.** Wire shapes,\n draft state, copy conditions, filters and payload building live in plain\n `.ts` files beside the components and are tested in `ui/test/`. Components\n stay thin, so there is normally nothing in an SFC that is archie logic.\n- **A dashboard rule that mirrors a server rule is pinned by a shared\n fixture, not two independent tests.** `ui/src/settings/validation.ts`\n hand-mirrors three control-plane validators, and the model-role rule had\n already drifted because nothing tied the two sides together. Each case now\n lives once in `internal/app/controlplane/testdata/dashboard-validators.json`:\n a Go test in that package runs the real server validators over it, and\n `ui/test/control-plane-validator-parity.test.ts` runs the real dashboard\n validators over the same cases. Changing one rule alone fails the other\n side's test. Add a case to the fixture, never to one side only.\n- **Installed primitives are upstream-tested, not re-tested here.**\n `reka-ui` (2.10.5 here) maintains colocated tests per component in its own\n repo (Vitest/jsdom + axe: Switch, TagsInput, NumberField, Select, the\n arrow-navigation composables). The npm package ships none of those files, so\n this coverage is inherited through the version bound, not the tarball —\n `grep -l test node_modules/reka-ui` finds nothing. Duplicating them as local\n mount tests would test a dependency.\n- **A `.vue` import cannot run under the module runner.** `node --test` fails\n such a module with `ERR_UNKNOWN_FILE_EXTENSION: .vue` and the runner has no\n built-in loader for SFCs. A file under `ui/test/` that imports a `.vue` is\n therefore a broken test, not a weak one.\n- **If a DOM mount harness is ever needed** (a page-level accessibility\n contract, cross-component interaction, focus behaviour): `happy-dom` +\n `@vue/test-utils` + `vitest` scoped to a separate DOM test glob, keeping the\n module runner for everything else. Measured on the ui tree (vitest 5.0.2,\n happy-dom 20.14.5, @vue/test-utils 2.5.1): they add 24 top-level\n `node_modules` entries and 27 MB on a 296 MB install, a working 2-test mount\n suite (role=radiogroup vs role=switch, aria-label, click activation against\n the real SFCs) runs in 1.2 s on top of the current 0.5 s module suite, and\n `vue-tsc` (7.4 s) dominates `task test:ui` either way. The single-runner\n alternative (absorbing the existing suite into vitest) was measured\n infeasible as-is: all 29 existing files impLine truncated
|
||||
},
|
||||
{
|
||||
"url": "/dev/development/merging-work/",
|
||||
@@ -154,7 +154,7 @@
|
||||
"url": "/dev/development/workflows/",
|
||||
"title": "Workflows",
|
||||
"section": "Development",
|
||||
"body": "# Workflows\n\nAuthority: `docs/architecture/agent-system.md` for the lifecycle,\n`docs/prds/event-automation.md` for inputs, repository modes and profiles.\n\nWorkflow definitions are YAML stored as a control-plane resource. The State\nStore validates them and the agent compiles them, each against the step\nvocabulary it registers, so both must see the same step types.\n\n## Adding a step type\n\n1. Write the factory in `internal/domain/workflow` as a `StepType`. It decodes\n and validates its settings and returns an error for anything it cannot\n honour. The factory runs when a definition is saved and again when it is\n compiled, so a bad definition is refused on save.\n2. Register it through a provider in\n `internal/infrastructure/workflowsteps/workflowsteps.go`. That one list is\n what every process registers, which is how the validating and executing\n sides agree.\n3. An agent-driven step builds on `AgentStage`; do not start a second path to\n the agent runner.\n4. A step that needs no repository goes in `repoFreeStepTypes` in\n `definition.go`, or workflows declaring `repository: none` or `optional`\n cannot use it. A step that needs one may assume `tc.Dir` holds a worktree\n only when the workflow's repository is `required`.\n5. A step that ends the workflow sets `tc.Outcome`. A workflow that runs out of\n steps without an outcome is parked as a definition bug.\n6. Add a parse test (settings accepted and refused) and a run test with fake\n `Trees` and agent runner.\n\n## Changing the definition format\n\n- The fields a caller needs without the engine (inputs, repository, profile)\n live in `task.WorkflowInterface`, because the dashboard process must not\n import the workflow engine (`cmd/archie-ui/architecture_test.go`). Anything a\n binding or the dashboard checks goes there.\n- The YAML decoder rejects unknown keys. Existing stored definitions must keep\n parsing, so a new key is optional with a default that preserves today's\n behaviour.\n- Examples in `examples/workflows/` are parsed against the registered\n vocabulary by `TestExampleWorkflowsParse`. Add one for a new capability.\n\n## Enabling and disabling\n\nThe `workflow-enablement` control-plane resource lists, per org, the workflows\nthat org has disabled; anything unlisted is enabled. Its readers are the\ndaemon's binding dispatch (a disabled workflow's binding is skipped like an\nunarmed one), the dashboard's binding save (refused) and binding list\n(`workflow_disabled`), and work requests. A new reader of \"may this org run\nthis workflow\" goes through `task.WorkflowEnablement.Enabled`, never a second\nlist. The dashboard acts in the default org until access resolves a caller's\nown.\n\n## Starting a workflow from chat\n\n`task_spawn` accepts `workflow` and an `inputs` object. The gateway forwards\ninputs through the task creation contract; it does not interpret individual\nworkflows. The daemon checks inputs against the pinned `WorkflowInterface`\nbefore acquiring a container and parks invalid requests with the reason.\n"
|
||||
"body": "# Workflows\n\nAuthority: `docs/architecture/agent-system.md` for the lifecycle,\n`docs/prds/event-automation.md` for inputs, repository modes and profiles.\n\nWorkflow definitions are YAML stored as a control-plane resource. The State\nStore validates them and the agent compiles them, each against the step\nvocabulary it registers, so both must see the same step types.\n\n## Adding a step type\n\n1. Write the factory in `internal/domain/workflow` as a `StepType`. It decodes\n and validates its settings and returns an error for anything it cannot\n honour. The factory runs when a definition is saved and again when it is\n compiled, so a bad definition is refused on save.\n2. Register it through a provider in\n `internal/infrastructure/workflowsteps/workflowsteps.go`. That one list is\n what every process registers, which is how the validating and executing\n sides agree.\n3. An agent-driven step builds on `AgentStage`; do not start a second path to\n the agent runner.\n4. A step that needs no repository goes in `repoFreeStepTypes` in\n `definition.go`, or workflows declaring `repository: none` or `optional`\n cannot use it. A step that needs one may assume `tc.Dir` holds a worktree\n only when the workflow's repository is `required`.\n5. A step that ends the workflow sets `tc.Outcome`. A workflow that runs out of\n steps without an outcome is parked as a definition bug.\n6. Add a parse test (settings accepted and refused) and a run test with fake\n `Trees` and agent runner.\n\n## Retiring a step type\n\nA step word cannot simply be deleted. A stored or pinned definition is\nvalidated as a whole collection, so one unknown word fails EVERY workflow's\ndefinition to decode and the workflow-definitions resource becomes unreadable.\nRetire a stage by removing it from its workflow's `Stages` and adding it to\n`retiredSteps` in `definition.go`, which keeps the word resolving as an inert\nstage. The retired stage must do nothing: the behaviour it used to perform now\nbelongs to whatever layer took it over (the remediate workflow's in-container\nresume moved to daemon worktree preparation, `archie-core-866m`; the workflow\nstill binds the daemon-prepared directory and the PR branch in a `prepare`\nstage before any agent work).\n\n## Changing the definition format\n\n- The fields a caller needs without the engine (inputs, repository, profile)\n live in `task.WorkflowInterface`, because the dashboard process must not\n import the workflow engine (`cmd/archie-ui/architecture_test.go`). Anything a\n binding or the dashboard checks goes there.\n- The YAML decoder rejects unknown keys. Existing stored definitions must keep\n parsing, so a new key is optional with a default that preserves today's\n behaviour.\n- A declared `outputs:` entry is written through a capture tool, so it forces\n the captures need: `task.WorkflowInterface.Needs` resolves that from the\n declaration rather than the author also writing `needs.captures`. A Kit\n profile whose harness serves no capture tools is refused before dispatch.\n- Examples in `examples/workflows/` are parsed against the registered\n vocabulary by `TestExampleWorkflowsParse`. Add one for a new capability.\n\n## Enabling and disabling\n\nThe `workflow-enablement` control-plane resource lists, per org, the workflows\nthat org has disabled; anything unlisted is enabled. Its readers are the\ndaemon's binding dispatch (a disabled workflow's binding is skipped like an\nunarmed one), the dashboard's binding save (refused) and binding list\n(`workflow_disabled`), and work requests. A new reader of \"may this org run\nthis workflow\" goes through `task.WorkflowEnablement.Enabled`, never a second\nlist. The dashboard acts in the default org until access resolves a caller's\nown.\n\n## Starting a workflow from chat\n\n`task_spawn` accepts `workflow` and an `inputs` object. The gateway forwards\ninputs through the task creation contract; it does not interpret individual\nworkflows. The daemon checks inputs against the pinned `WorkflowInterface`\nbefore acquiring a container and parks invalid requests with the reason.\n"
|
||||
},
|
||||
{
|
||||
"url": "/dev/prds/01-project-management/",
|
||||
@@ -352,7 +352,7 @@
|
||||
"url": "/dev/prds/mcp-tool-completion/",
|
||||
"title": "MCP client and tool-guardrail completion",
|
||||
"section": "PRDs",
|
||||
"body": "# MCP client and tool-guardrail completion\n\n**Status:** Approved\n\nEpic: `archie-core-1786637493439-140-2cee2e9b` / GitHub `#161` (\"[EPIC] Tools:\ncentral registry, MCP client, guardrails\").\n\nThis document is scoped to five sub-features of that epic: MCP\nserver-initiated sampling, the per-server parallel-tool-calls flag, sandboxed\nMCP tool servers, a Firecracker sandbox backend, and sequential dispatch with\nper-result previews.\n\nOut of scope, and not to be touched: the central registry\n(`internal/tools/registry.go`), all three MCP transports (stdio/HTTP/SSE,\n`internal/tools/mcp/transport*.go`), reconnect-with-backoff\n(`transport.go`), guardrail loop detection\n(`internal/tools/guardrail.go`), idempotent/mutating classification\n(`internal/tools/classification.go`), concurrent tool dispatch (`#183`), and\nprogressive disclosure wiring (`internal/app/archied`). These are settled and\nproduction-wired.\n\n## Problem\n\n`bd search \"MCP client\"` shows five closed transport beads and one open:\nserver-initiated sampling (`#151`). `internal/tools/mcp/client.go`\nholds a single `callMu *sync.Mutex` that serialises every `tools/call`\nregardless of server — `ParallelToolCalls` does not exist on the `MCPServer`\nconfig struct (`internal/config/config.go`), confirming `#177` is a\nreal gap, not a stale bead. `#178` (wire MCP tool servers for the chat agent,\nsandboxed) and `#179` (Firecracker substrate) have no corresponding code —\nMCP servers run as whatever the config's `Command`/`Args` launches,\nunsandboxed. `#182` (sequential tool dispatch with per-result previews) is\nopen; concurrent dispatch (`#183`) is closed, so there are two dispatch paths\nand this PRD decides how they coexist.\n\n## Design\n\nSub-features are independent; there is no new shared type this document\nintroduces. Each section below stands alone.\n\n## MCP server-initiated sampling (`#151`)\n\n**Decision.** Implement as a callback on `mcp.Client`, not a second\ntransport. The MCP spec's `sampling/createMessage` request flows server→client\nover the same connection each transport already maintains\n(`transport.go`'s stdio/HTTP/SSE all multiplex request and response frames).\nAdd a `SamplingHandler func(context.Context, SamplingRequest) (SamplingResult, error)`\nfield to the client's config struct next to the existing transport fields,\ndefaulting to nil (server sampling requests get a JSON-RPC \"not supported\"\nerror when unset — never a silent drop).\n\n**Call site.** `internal/tools/mcp/client.go` — add the handler field and a\ndispatch branch in whatever function demuxes incoming server\nmessages by method name (the same switch that already routes\n`notifications/*`). Wire a concrete handler in\n`internal/app/archied/main.go` that routes the sampling request through the\nsame chat-model call path `chatGenerateOptions` already uses, so a\nserver-initiated sample gets the daemon's configured model, not a second\nprovider client.\n\n**Reuse check.** The chat model invocation path already exists\n(`chatGenerateOptions`, `main.go`); this reuses it rather than adding a\nparallel model-calling path for MCP sampling.\n\n**Failure mapping.**\n\n| condition | contract error |\n| ------------------------------- | ------------------------------------------------------------------------------------------ |\n| no `SamplingHandler` configured | JSON-RPC method-not-found, per spec |\n| handler returns error | JSON-RPC internal error, message from handler |\n| handler timeout | JSON-RPC timeout error; use the transport's existing request-timeout config, not a new one |\n\n**Testing.** Fakeable entirely — a stub `SamplingHandler` and a fake\ntransport that emits a `sampling/createMessage` frame. No real backend\nneeded; belongs in `task check`.\n\n## Per-server parallel-tool-calls flag (`#177`)\n\n**Decision.** `ParallelToolCalls bool` (default `false`) goes on `MCPServer`\nin `internal/config/config.go`, next to `Transport`/`Command`. The\nmutex in `client.go` becomes per-server: when `ParallelToolCalls` is\nfalse (default, and the only behaviour), keep the single `callMu`\nserialising that server's calls exactly as now; when true, drop the mutex for\nthat server's `Client` instance entirely and let the caller's own\nconcurrency (already proven by the closed concurrent-dispatch bead, `#183`)\nhandle it. This is additive — no server's behaviour changes unless an operator\nopts in.\n\n**Call site.** `internal/config/config.go` (struct field),\n`internal/tools/mcp/client.go` (make the mutex conditional on the\nfield), and wherever `MCPServer` configs are turned into `Client` instances\nat daemon startup (the constructor that reads `internal/config`'s MCP block).\n`config.example.toml`'s commenLine truncated
|
||||
"body": "# MCP client and tool-guardrail completion\n\n**Status:** Approved\n\nEpic: `archie-core-1786637493439-140-2cee2e9b` / GitHub `#161` (\"[EPIC] Tools:\ncentral registry, MCP client, guardrails\").\n\nThis document is scoped to five sub-features of that epic: MCP\nserver-initiated sampling, the per-server parallel-tool-calls flag, sandboxed\nMCP tool servers, a Firecracker sandbox backend, and sequential dispatch with\nper-result previews.\n\nOut of scope, and not to be touched: the central registry\n(`internal/tools/registry.go`), all three MCP transports (stdio/HTTP/SSE,\n`internal/tools/mcp/transport*.go`), reconnect-with-backoff\n(`transport.go`), guardrail loop detection\n(`internal/tools/guardrail.go`), idempotent/mutating classification\n(`internal/tools/classification.go`), concurrent tool dispatch (`#183`), and\nprogressive disclosure wiring (`internal/app/archied`). These are settled and\nproduction-wired.\n\n## Problem\n\n`bd search \"MCP client\"` shows five closed transport beads and one open:\nserver-initiated sampling (`#151`). `internal/tools/mcp/client.go`\nholds a single `callMu *sync.Mutex` that serialises every `tools/call`\nregardless of server — `ParallelToolCalls` does not exist on the `MCPServer`\nconfig struct (`internal/config/config.go`), confirming `#177` is a\nreal gap, not a stale bead. `#178` (wire MCP tool servers for the chat agent,\nsandboxed) and `#179` (Firecracker substrate) have no corresponding code —\nMCP servers run as whatever the config's `Command`/`Args` launches,\nunsandboxed. `#182` (sequential tool dispatch with per-result previews) is\nopen; concurrent dispatch (`#183`) is closed, so there are two dispatch paths\nand this PRD decides how they coexist.\n\n## Design\n\nSub-features are independent; there is no new shared type this document\nintroduces. Each section below stands alone.\n\n## MCP server-initiated sampling (`#151`)\n\n**Decision.** Implement as a callback on `mcp.Client`, not a second\ntransport. The MCP spec's `sampling/createMessage` request flows server→client\nover the same connection each transport already maintains\n(`transport.go`'s stdio/HTTP/SSE all multiplex request and response frames).\nAdd a `SamplingHandler func(context.Context, SamplingRequest) (SamplingResult, error)`\nfield to the client's config struct next to the existing transport fields,\ndefaulting to nil (server sampling requests get a JSON-RPC \"not supported\"\nerror when unset — never a silent drop).\n\n**Call site.** `internal/tools/mcp/client.go` — add the handler field and a\ndispatch branch in whatever function demuxes incoming server\nmessages by method name (the same switch that already routes\n`notifications/*`). Wire a concrete handler in\n`internal/app/archied/main.go` that routes the sampling request through the\nsame chat-model call path `chatGenerateOptions` already uses, so a\nserver-initiated sample gets the daemon's configured model, not a second\nprovider client. The agent worker hosts the same configured MCP servers on the\ntask path and runs its own model runtime, so `internal/app/agentworker` wires\nthe same seam from the task's builder model: a server that samples succeeds\ninside a chat turn and inside a task, never one and not the other. The message\nmapping and the token bound are shared (`internal/agentexec`, next to the\nruntime both paths call models through) so both paths bound a sample\nidentically.\n\n**Reuse check.** The chat model invocation path already exists\n(`chatGenerateOptions`, `main.go`) and the task model path already exists\n(`agentexec.NewRuntime`, the runtime an agent stage runs on); this reuses both\nrather than adding a parallel model-calling path for MCP sampling.\n\n**Failure mapping.**\n\n| condition | contract error |\n| ------------------------------- | ------------------------------------------------------------------------------------------ |\n| no `SamplingHandler` configured | JSON-RPC method-not-found, per spec |\n| handler returns error | JSON-RPC internal error, message from handler |\n| handler timeout | JSON-RPC timeout error; use the transport's existing request-timeout config, not a new one |\n\n**Testing.** Fakeable entirely — a stub `SamplingHandler` and a fake\ntransport that emits a `sampling/createMessage` frame. No real backend\nneeded; belongs in `task check`.\n\n## Per-server parallel-tool-calls flag (`#177`)\n\n**Decision.** `ParallelToolCalls bool` (default `false`) goes on `MCPServer`\nin `internal/config/config.go`, next to `Transport`/`Command`. The\nmutex in `client.go` becomes per-server: when `ParallelToolCalls` is\nfalse (default, and the only behaviour), keep the single `callMu`\nserialising that server's calls exactly as now; when true, drop the muLine truncated
|
||||
},
|
||||
{
|
||||
"url": "/dev/prds/memory-engine-unification/",
|
||||
@@ -412,7 +412,7 @@
|
||||
"url": "/dev/prds/plugin-settings-live/",
|
||||
"title": "Plugin settings reconcile live",
|
||||
"section": "PRDs",
|
||||
"body": "# Plugin settings reconcile live\n\n**Status:** Draft\n**Tracking:** GitHub [#1154](https://github.com/samcharles93/archie-core/issues/1154), epic [#998](https://github.com/samcharles93/archie-core/issues/998)\n**Authority:** `docs/prds/runtime-control-plane.md` decides each feature owns how\nits changes take effect; `docs/architecture/plugins-and-extensions.md` owns the\nplugin engine rule; `docs/prds/control-plane-apply-status.md` owns where apply\nreports go.\n\n## Outcome\n\nAn operator drops a plugin, module or secret-engine file into the configured\ndirectory while archied runs, and the daemon loads it without a restart. An\nedit to an installed file replaces what is running. Deleting an installed file\nnever stops running code; apply status says so. A stored change to\n`plugin-settings` applies live for additions instead of requiring a restart.\n\n## Triggers\n\n`plugin-settings` joins the kinds archied watches (`PluginSettingsKind` moves\nto the live list in `internal/app/archied/control_plane.go`). After the daemon\nlayers a new version it reconciles the three directories the document names.\nA second trigger needs no document change: a poller reconciles on an interval,\nso a file dropped into a directory loads on its own. The poll interval is the\napply-status restamp constant; no filesystem-watch dependency is added. The\npoller reads the current document's directories from the running config each\ntick, so a changed `plugin_dir`, `module_dir` or `secret_engine_dir` retargets\nreconciliation without a restart.\n\n## Rules\n\n- Adding and changing load; removing never unloads. Yaegi interpreters are not\n reclaimed while a caller still exists and there is no unload path; the\n running set can only grow.\n- Each file is loaded through a fresh interpreter (the boot pattern). A changed\n file therefore loads cleanly, and replacement switches consumers to the new\n interpretation; files are content-hashed so only new and changed files load.\n- A file that fails to load is skipped and reported; already-running code keeps\n running. This is the degrade-and-skip convention `plugin.LoadDir` and\n `Registry.LoadDir` already apply at boot. Boot keeps its reject-at-load rule\n for the module directory; a broken file at runtime skips without aborting.\n- A directory that does not exist reconciles to an empty set.\n\n## The three registries\n\n- **Secret engines** (`internal/secret`). Reconciliation re-runs\n `Registry.LoadDir` against the live directory. `Register` replaces by name,\n so an edited engine takes effect for the next resolution. A deleted engine\n stays registered: existing references keep resolving through it until\n restart, and the record says so. Engines are stateless resolvers, so a swap\n needs no lifecycle step.\n- **Modules** (`internal/domain/eda/module`): reconciliation re-runs\n `ModuleRegistry.Register` for the kind's file. The invoker maps\n last-write-wins, and playbook dispatch resolves through the registry at\n invoke time, so the swap reaches a running playbook store unchanged. A file\n deleted for a kind leaves the old invoker in place, running; reported.\n- **Plugins** (`internal/plugin`): `Host` grows running-state operations beside\n `Register`, `Start` and `Stop`. One adds a single module while the host is\n running: it is inserted in dependency order, started alone, and on failure\n rolled back alone, leaving every previously started module running. The\n other replaces a module whose manifest id matches: stop it, start the new\n one, and restart the old one when the new start fails. Replacing a module\n that another depends on is refused, not swapped. These operations obey the\n plugin engine rule: the host still invokes start and stop itself, isolates\n the failure to the one module, and keeps a rollback. A deleted file makes no\n host call: the module runs on and is reported.\n\n## `skills_dir`\n\n`skills_dir` is not part of this change. The daemon refuses a candidate\n`plugin-settings` document whose `skills_dir` differs from the value it is\nrunning, reporting that the field applies on restart, until a skill\nreconciliation exists to carry it. The refusal is the existing rejected-edit\napply-status shape: the store keeps the version, the document is not applied,\nlast-known-good keeps running.\n\n## Apply status\n\nOne record: `archied` on `plugin-settings`. The reconciliation outcome is\nreported against the version the process is running. The record's error field\ncarries the outstanding reconciliation problem: a deleted file still running, a\nfile that failed to load, or the directory failing to read. A reconciliation\nwith nothing outstanding clears the error. The version stays the version the\nprocess is running; an add that succeeded is live even while a removal is\noutstanding, and the error says so rather than showing the kind as\nrestart-required. archie-gateway keeps boot-time loading; its record simply\nstays on the vLine truncated
|
||||
"body": "# Plugin settings reconcile live\n\n**Status:** Draft\n**Revision:** 2\n**Tracking:** GitHub [#1154](https://github.com/samcharles93/archie-core/issues/1154), epic [#998](https://github.com/samcharles93/archie-core/issues/998)\n**Authority:** `docs/prds/runtime-control-plane.md` decides each feature owns how\nits changes take effect; `docs/architecture/plugins-and-extensions.md` owns the\nplugin engine rule; `docs/prds/control-plane-apply-status.md` owns where apply\nreports go.\n\n## Outcome\n\nAn operator drops a plugin, module or secret-engine file into a configured\ndirectory while archied runs, and the daemon loads it without a restart. An\nedit to an installed file replaces what is running. A delete never stops running\ncode; apply status says so. A stored `plugin-settings` change applies live for\n`plugin_dir`, `module_dir` and `secret_engine_dir`; a change to `skills_dir` is\nrefused.\n\n## Triggers\n\n`PluginSettingsKind` joins `runtimeResourceKinds` in\n`internal/app/archied/control_plane.go`, and its descriptor `ApplyMode` becomes\n`live` in `internal/app/controlplane/operational_settings.go`.\n\nA poller reconciles on the apply-status restamp interval, so a dropped file\nloads without a filesystem watch. Each tick re-runs both halves the boot path\nruns, against the directories in the running config: the directory loads\n(`secret.Registry.LoadDir`, `ModuleRegistry.Register`, `plugin.LoadDir`) and the\nlayering (`boot.runtimeConfig`), which re-resolves provider credentials. A\nprovider disabled at boot for an unresolvable engine is re-resolved on the first\ntick that finds it; a changed directory retargets the next tick without a\nrestart.\n\n## Rules\n\n- Adding and changing load; removing never unloads. Yaegi interpreters are not\n reclaimed while a caller still exists and there is no unload path; the running\n set can only grow. A removal stays outstanding in the apply-status error until\n the process restarts.\n- Each file is loaded through a fresh interpreter (the boot pattern) and\n content-hashed, so only new and changed files load.\n- A file that fails to load is skipped and reported; already-running code keeps\n running, the degrade-and-skip convention `plugin.LoadDir` and\n `Registry.LoadDir` already apply at boot. Boot keeps its reject-at-load rule\n for the module directory; a broken file at runtime skips without aborting.\n- A directory that does not exist reconciles to an empty set.\n\n## The three registries\n\n- **Secret engines** (`internal/secret`). Reconciliation re-runs\n `Registry.LoadDir` against the live directory. `Register` replaces by name, so\n an edited engine takes effect for the next resolution and a dropped engine\n resolves on the next tick. A deleted engine stays registered: existing\n references keep resolving through it until restart. Engines are stateless\n resolvers, so a swap needs no lifecycle step.\n- **Modules** (`internal/domain/eda/module`). Reconciliation re-runs\n `ModuleRegistry.Register` for the kind's file. Playbook dispatch resolves\n through the registry at invoke time, so the swap reaches a running playbook\n store unchanged; a deleted file leaves the old invoker in place, running.\n- **Plugins** (`internal/plugin`). `Host` grows running-state operations beside\n `Register`, `Start` and `Stop`, and `Register` keeps its rule that\n registration is allowed only before the first `Start`, so these are new host\n methods rather than a relaxed `Register`. One adds a single module while the\n host is running: inserted in dependency order, started alone, and rolled back\n alone on failure, leaving every previously started module running. The other\n replaces a module whose manifest id matches: stop it, start the new one, and\n restart the old one when the new start fails; replacing a module another\n running module depends on is refused. A module's `Start` is its only check\n (`plugin.Module` has no probe, and one manifest id cannot be registered\n twice), so a replacement checks by starting and keeps a rollback rather than\n switching first. These operations obey the plugin engine rule: the host\n invokes start and stop itself and isolates the failure to one module. A\n deleted file makes no host call: the module runs on.\n\n## `skills_dir`\n\n`skills_dir` is not part of this change. The daemon refuses a `plugin-settings`\ndocument whose `skills_dir` differs from the value in force. The check is a\npre-layer comparison on the live path (`boot.applyRuntimeResourceUpdate` in\n`internal/app/archied/control_plane.go`), not an addition to\n`validatePluginSettings` in `internal/app/controlplane/operational_settings.go`,\nwhich stays value-agnostic per the #1143 decision recorded there: a\nchange-immutability rule needs the running config, and #1143's validates-nothing\nstill holds for values. The write lands, the store keeps the version, the daemon\npublishes nothing, the refusal is reported against that stored versLine truncated
|
||||
},
|
||||
{
|
||||
"url": "/dev/prds/pr-review-agent/",
|
||||
@@ -424,7 +424,7 @@
|
||||
"url": "/dev/prds/pr-review-operator-response/",
|
||||
"title": "PR review: the operator's answer to the approval gate",
|
||||
"section": "PRDs",
|
||||
"body": "# PR review: the operator's answer to the approval gate\n\n**Status:** Draft\n**Authority:** `docs/prds/pr-review-agent.md` (\"Operator approval\") for the gate, and `docs/prds/state-store-contract.md` (rev. 2e) for every task field that crosses the State Store.\n**Compounds with:** `docs/prds/execution-tree-state-machine.md` (a run cannot resume partway through its stage list).\n\n## Decision 1 — where the operator answers\n\nThe dashboard's `POST /api/tasks/{id}/action` (`internal/webui/api_tasks.go`, `handleTaskAction`) is the authoritative surface: the only one that resolves the actor from a credential and can carry a payload. Chat is a shortcut, not a second authority — `/approve` (`internal/gateway/gateway.go`, `Router.handleApprove`) answers the post decision with every offered finding selected, and gains no instruction or selection syntax.\n\n`taskstate.Action` gains `rereview`, and `taskstate.Actions` returns `approve`, `reject` and `rereview` for `WaitingHuman`. `approve` posts the review, `reject` discards it through the existing terminal decline, `rereview` reruns the review phases with instructions. The action body carries `instructions`, required by `rereview`, and `findings`, the finding keys `approve` posts; an absent selection means all of them.\n\nThe path is `handleTaskAction` → `applyOperatorTaskAction` → `Chat.Contract.ApplyOperatorTaskAction` → the daemon's `LocalChatAdapter` → `taskactions.Client` (NATS `archie.gateway.task-action`) → `taskactions.Service.Apply` → one guarded store write that records the decision and requeues. Chat's `/approve` path (`StoreTaskController.Approve` → `chatTaskControllerAdapter.ApproveChatTask`) must reach that same write through the same function, so the two surfaces cannot record different decisions for one operator intent.\n\nA requeue resumes the workflow the waiting state names, never a name the handler hardcodes. `taskactions.Service.apply` and `chatTaskControllerAdapter.ApproveChatTask` requeue every approval under `implement`, which is the feasibility handoff: an approval whose wait recorded `pr-review` would run the implement workflow. Feasibility must keep that handoff by naming `implement` on the task before it enters the wait, which is the invariant the handler needs — the workflow a task carries is the workflow it runs next. `Requeue` treats an empty workflow as \"keep the task's\".\n\n### The review the operator answers\n\nThe gate writes the review it is holding onto the task before it ends the run: one JSON column, `review_gate`, a sibling of `review_payload`, holding the scored findings, the head SHA, the pull request's identity and the workflow the wait resumes. The run's existing `Store.Update` persists it. The same column carries the answer back: the response path's guarded write fills in the outcome, the selection and the instructions, and clears the offer for a re-review.\n\nThe operator reads the offer through the existing task read — `taskView.Task` on `GET /api/tasks` and `GET /api/tasks/{id}/debug` — so no new read endpoint is needed.\n\nThis record is load-bearing because the run that computed the findings ends at the gate and the engine cannot resume it partway. Without it, \"post the selected findings\" cannot mean the findings the operator saw: the resumed run recomputes them with agent calls. An `approve` resume therefore posts the recorded review filtered by the selection, and the review the resumed phases recompute never reaches the output.\n\n## Decision 2 — the cap\n\nThe cap is a column: `rereview_rounds` on `tasks`, `bigint NOT NULL DEFAULT 0`, read by the response path and incremented in the same guarded write that requeues a re-review. A re-review at the cap is refused with the conflict sentinel the dashboard answers 409 with, and nothing is requeued. Two is a constant in the domain, not a config knob: the PRD fixes it.\n\n`Task.Attempt` cannot carry the count, because it counts every requeue. The two alternatives are worse.\n\n- The execution tree measures passes through the gate, not operator requests. A crash recovery (`RecoverStale`) or an operator retry re-runs the pipeline to the gate and would consume the cap; and a stage has no step read (`task.Store` carries none), so this route needs a new interface method anyway.\n- `Task.Notes` is the agent memory ledger, not scratch. `AgentStage.buildRequest` passes it into every generic agent request, `agentexec` renders it as a prompt section, and `persistAppendedNotes` appends to it. A counter parsed out of a field agents write to is the encoded-state defect this repository rejects, and instructions parked there would reach every unrelated workflow's agents.\n\n`RemediationRounds` is the precedent: one writer, one reader, one meaning, kept separate from `RetryCount` so unrelated retries cannot draw a budget down.\n\nThe write crosses the State Store. A goose migration adds the column; `internal/infrastructure/postgres/queriesLine truncated
|
||||
"body": "# PR review: the operator's answer to the approval gate\n\n**Status:** Draft\n**Authority:** `docs/prds/pr-review-agent.md` (\"Operator approval\") for the gate, and `docs/prds/state-store-contract.md` (rev. 2e) for every task field that crosses the State Store.\n**Compounds with:** `docs/prds/execution-tree-state-machine.md` (a run cannot resume partway through its stage list).\n\n## Decision 1 — where the operator answers\n\nThe dashboard's `POST /api/tasks/{id}/action` (`internal/webui/api_tasks.go`, `handleTaskAction`) is the authoritative surface: the only one that resolves the actor from a credential and can carry a payload. Chat is a shortcut, not a second authority — `/approve` (`internal/gateway/gateway.go`, `Router.handleApprove`) answers the post decision with every offered finding selected, and gains no instruction or selection syntax.\n\n`taskstate.Action` gains `rereview`, and `taskstate.Actions` returns `approve`, `reject` and `rereview` for `WaitingHuman`. `approve` posts the review, `reject` discards it through the existing terminal decline, `rereview` reruns the review phases with instructions. The action body carries `instructions`, required by `rereview`, and `findings`, the finding keys `approve` posts; an absent selection means all of them.\n\nThe path is `handleTaskAction` → `applyOperatorTaskAction` → `Chat.Contract.ApplyOperatorTaskAction` → the daemon's `LocalChatAdapter` → `taskactions.Client` (NATS `archie.gateway.task-action`) → `taskactions.Service.Apply` → one guarded store write that records the decision and requeues. Chat's `/approve` path (`StoreTaskController.Approve` → `chatTaskControllerAdapter.ApproveChatTask`) must reach that same write through the same function, so the two surfaces cannot record different decisions for one operator intent.\n\nA requeue resumes the workflow the waiting state names, never a name the handler hardcodes. `taskactions.Service.apply` and `chatTaskControllerAdapter.ApproveChatTask` requeue with an empty workflow, which `Requeue` reads as \"keep the task's\", so neither surface hardcodes a workflow name: an approval whose wait recorded `pr-review` resumes `pr-review`. The feasibility handoff therefore has to name `implement` on the task before it enters the wait, which is the invariant the handler relies on — the workflow a task carries is the workflow it runs next. `Requeue` treats an empty workflow as \"keep the task's\".\n\n### The review the operator answers\n\nThe gate writes the review it is holding onto the task before it ends the run: one JSON column, `review_gate`, a sibling of `review_payload`, holding the scored findings, the head SHA, the pull request's identity and the workflow the wait resumes. The run's existing `Store.Update` persists it. The same column carries the answer back: the response path's guarded write fills in the outcome, the selection and the instructions, and clears the offer for a re-review.\n\nThe operator reads the offer through the existing task read — `taskView.Task` on `GET /api/tasks` and `GET /api/tasks/{id}/debug` — so no new read endpoint is needed.\n\nThis record is load-bearing because the run that computed the findings ends at the gate and the engine cannot resume it partway. Without it, \"post the selected findings\" cannot mean the findings the operator saw: the resumed run recomputes them with agent calls. An `approve` resume therefore posts the recorded review filtered by the selection, and the review the resumed phases recompute never reaches the output.\n\n## Decision 2 — the cap\n\nThe cap is a column: `rereview_rounds` on `tasks`, `bigint NOT NULL DEFAULT 0`, read by the response path and incremented in the same guarded write that requeues a re-review. A re-review at the cap is refused with the conflict sentinel the dashboard answers 409 with, and nothing is requeued. Two is a constant in the domain, not a config knob: the PRD fixes it.\n\n`Task.Attempt` cannot carry the count, because it counts every requeue. The two alternatives are worse.\n\n- The execution tree measures passes through the gate, not operator requests. A crash recovery (`RecoverStale`) or an operator retry re-runs the pipeline to the gate and would consume the cap; and a stage has no step read (`task.Store` carries none), so this route needs a new interface method anyway.\n- `Task.Notes` is the agent memory ledger, not scratch. `AgentStage.buildRequest` passes it into every generic agent request, `agentexec` renders it as a prompt section, and `persistAppendedNotes` appends to it. A counter parsed out of a field agents write to is the encoded-state defect this repository rejects, and instructions parked there would reach every unrelated workflow's agents.\n\n`RemediationRounds` is the precedent: one writer, one reader, one meaning, kept separate from `RetryCount` so unrelated retries cannot draw a budget down.\n\nThe write crosses the State Store. A goose migration adds the column; `Line truncated
|
||||
},
|
||||
{
|
||||
"url": "/dev/prds/pr-review-remediation/",
|
||||
@@ -466,7 +466,7 @@
|
||||
"url": "/dev/prds/runtime-control-plane/",
|
||||
"title": "Runtime control plane",
|
||||
"section": "PRDs",
|
||||
"body": "# Runtime control plane\n\n**Status:** Approved \n**Date:** 2026-09-20 \n**Tracking:** `archie-core-j28m`, `archie-core-1786637498420-327-bacadaae`\n\n## Outcome\n\nArchie can be managed from the Web UI or a messaging channel. Both use the same\nAPI.\n\n## Model\n\nEach feature owns its settings and commands. The database stores them. There is\nno global configuration document.\n\nThis includes:\n\n- workflows;\n- identities and bindings;\n- providers and models;\n- repository policies;\n- channels and schedules;\n- capture mappings and bindings;\n- tool settings;\n- prompts and personas;\n- plugin settings.\n\nPlugin code remains installed code. Only its settings are stored in the\ndatabase.\n\nEach feature defines its data, validation, permissions, editor fields, and how\nchanges take effect. Each feature also defines its allowed actions. For example,\nidentities can be created, suspended, and retired.\n\nRepository and identity settings remain separate. They are not stored as large\nrecords that unrelated features must share.\n\nThe State Store process hosts the control-plane API on its existing gRPC server.\nNo new process is added. Feature code validates changes. Storage code only\nwrites versions and audit records.\n\nEvery write records who made it, where it came from, a request ID, and the\nversion being edited. The API layer sets the actor; the request body cannot.\nThe server validates the change, then writes it with its audit record. It rejects\nedits to an old version.\n\nThe request ID is the write's idempotency key, and it is scoped per org and\nper kind, never global. The key is `(org_id, kind, request_id)` — the unique\nindex `idx_resource_history_org_kind_request` (migration `0021_resources_per_org.sql`)\n— so replaying a request ID is the same org's prior write of the same kind, and\na request ID reused across orgs or across kinds is a separate write. This is the\nsame scoping as the task idempotency key `org/identity/owner/repo/number`\n(`docs/prds/orgs-and-access.md`).\n\n## API\n\nThe API has four operations:\n\n1. **Catalogue** tells clients what can be viewed or changed and how to edit it.\n2. **Query** reads data and versions without exposing secrets.\n3. **Command** asks the owning feature to make a change.\n4. **Watch** streams changes and errors to clients.\n\nComplex features can use custom screens. They still use the same commands.\n\nA dashboard session or channel sender must map to an Archie identity before it\ncan run a command. Messaging syntax maps to the same commands as the Web UI.\n\n`archie-ui` receives only an API client. It does not receive a database handle,\ndaemon pointer, global configuration object, or secret value.\n\nThe database stores the requested settings. Each process reports the version it\nis using or why it failed. The UI shows both.\n\nSome changes require a restart. The UI shows them as pending. When a live change\nreplaces a running component, Archie starts and checks the new one before\nswitching. If the check fails, the old one keeps running.\n\n## Bootstrap, migration, and recovery\n\nBootstrap contains only the database path, listen addresses, credentials, TLS,\nand encryption-key references. State Store and control-plane RPCs use the same\nendpoint.\n\nWhen upgraded, existing instances are migrated to the database. Existing\ndatabase values are not overwritten, with one exception: a resource whose\ndocument the build ships is refreshed when the seed itself wrote the stored\ncopy, because that copy is a previous release's document rather than an\noperator's. After migration, settings in TOML are ignored and cannot block\nState Store startup.\n\nThe Web UI remains available when `archied` or Gateway is down. It shows failed\nchanges and can correct or roll them back.\n\nOffline commands can back up, restore, validate, and roll back the database.\nRecovery does not require `archied` or the Web UI. The database stores secret\nreferences, not secret values.\n\n## Workflows\n\nUsers create and edit workflows in the Web UI. Workflows are stored as versioned\nYAML. Built-in workflows can be overridden and restored.\n\nA workflow is a list of steps. Each step has a known type and its own settings.\nPlugins can add step types. Each run keeps the workflow version it started with.\n\nYaegi, `.archie/stages/*.go`, and `.archie/gate.go` will be removed. Their useful\nbehaviour must first be available as normal workflow steps.\n\n## First implementation\n\nThe first implementation makes task limits editable: maximum model steps,\nmaximum runtime, and maximum consecutive gate failures.\n\nIt includes:\n\n- validated task-limit fields;\n- version history and audit records;\n- the four API operations;\n- authenticated Web UI and messaging access;\n- live updates and apply status;\n- a Pinia-backed editor in the Web UI;\n- a messaging command using the same API;\n- migration of existing `[budgets]` values;\n- an end-to-end restart test.\n\nRuLine truncated
|
||||
"body": "# Runtime control plane\n\n**Status:** Approved \n**Date:** 2026-09-20 \n**Tracking:** `archie-core-j28m`, `archie-core-1786637498420-327-bacadaae`\n\n## Outcome\n\nArchie can be managed from the Web UI or a messaging channel. Both use the same\nAPI.\n\n## Model\n\nEach feature owns its settings and commands. The database stores them. There is\nno global configuration document.\n\nThis includes:\n\n- workflows;\n- identities and bindings;\n- providers and models;\n- repository policies;\n- channels and schedules;\n- capture mappings and bindings;\n- tool settings;\n- prompts and personas;\n- plugin settings.\n\nPlugin code remains installed code. Only its settings are stored in the\ndatabase.\n\nEach feature defines its data, validation, permissions, editor fields, and how\nchanges take effect. Each feature also defines its allowed actions. For example,\nidentities can be created, suspended, and retired.\n\nRepository and identity settings remain separate. They are not stored as large\nrecords that unrelated features must share.\n\nThe State Store process hosts the control-plane API on its existing gRPC server.\nNo new process is added. Feature code validates changes. Storage code only\nwrites versions and audit records.\n\nEvery write records who made it, where it came from, a request ID, and the\nversion being edited. The API layer sets the actor; the request body cannot.\nThe server validates the change, then writes it with its audit record. It rejects\nedits to an old version.\n\nThe request ID is the write's idempotency key, and it is scoped per org and\nper kind, never global. The key is `(org_id, kind, request_id)` — the unique\nindex `idx_resource_history_org_kind_request` (migration `0021_resources_per_org.sql`)\n— so replaying a request ID is the same org's prior write of the same kind, and\na request ID reused across orgs or across kinds is a separate write. This is the\nsame scoping as the task idempotency key `org/identity/owner/repo/number`\n(`docs/prds/orgs-and-access.md`).\n\n## API\n\nThe API has four operations:\n\n1. **Catalogue** tells clients what can be viewed or changed and how to edit it.\n2. **Query** reads data and versions without exposing secrets.\n3. **Command** asks the owning feature to make a change.\n4. **Watch** streams changes and errors to clients.\n\nComplex features can use custom screens. They still use the same commands.\n\nA dashboard session or channel sender must map to an Archie identity before it\ncan run a command. Messaging syntax maps to the same commands as the Web UI.\n\n`archie-ui` receives only an API client. It does not receive a database handle,\ndaemon pointer, global configuration object, or secret value.\n\nThe database stores the requested settings. Each process reports the version it\nis using or why it failed. The UI shows both.\n\nSome changes require a restart. The UI shows them as pending. When a live change\nreplaces a running component, Archie starts and checks the new one before\nswitching. If the check fails, the old one keeps running.\n\n## Bootstrap, migration, and recovery\n\nBootstrap contains only the database path, listen addresses, credentials, TLS,\nand encryption-key references. State Store and control-plane RPCs use the same\nendpoint.\n\nWhen upgraded, existing instances are migrated to the database. Existing\ndatabase values are not overwritten, with one exception: a resource whose\ndocument the build ships is refreshed when the seed itself wrote the stored\ncopy, because that copy is a previous release's document rather than an\noperator's. After migration, settings in TOML are ignored and cannot block\nState Store startup.\n\nThe Web UI remains available when `archied` or Gateway is down. It shows failed\nchanges and can correct or roll them back.\n\nOffline commands can back up, restore, validate, and roll back the database.\nRecovery does not require `archied` or the Web UI. The database stores secret\nreferences, not secret values.\n\n## Workflows\n\nUsers create and edit workflows in the Web UI. Workflows are stored as versioned\nYAML. Built-in workflows can be overridden and restored.\n\nA workflow is a list of steps. Each step has a known type and its own settings.\nA step type is part of the build's step vocabulary, contributed as a provider at\nthe composition root; interpreted plugins contribute none\n(`docs/prds/workflow-step-vocabulary.md`). Each run keeps the workflow version it\nstarted with.\n\nYaegi, `.archie/stages/*.go`, and `.archie/gate.go` will be removed. Their useful\nbehaviour must first be available as normal workflow steps.\n\n## First implementation\n\nThe first implementation makes task limits editable: maximum model steps,\nmaximum runtime, and maximum consecutive gate failures.\n\nIt includes:\n\n- validated task-limit fields;\n- version history and audit records;\n- the four API operations;\n- authenticated Web UI and messaging access;\n- live updates and apply status;\n- a PiLine truncated
|
||||
},
|
||||
{
|
||||
"url": "/dev/prds/self-update-transaction/",
|
||||
@@ -562,7 +562,7 @@
|
||||
"url": "/dev/prds/workflow-call-outputs/",
|
||||
"title": "Declared outputs of a workflow run",
|
||||
"section": "PRDs",
|
||||
"body": "# Declared outputs of a workflow run\n\n**Status:** Draft\n**Date:** 2026-09-30\n**Bead:** `archie-core-t2db.43`\n**Authority:** `docs/prds/event-automation.md` for what a declared output is,\n`docs/prds/workflow-calls.md` for the call and its grants,\n`docs/prds/state-store-contract.md` for the State Store contract the wire\nchange must satisfy.\n**Supersedes:** `docs/prds/workflow-calls.md`, \"Waiting on the callee\": the\ncallee's transition detail is the human summary, not the stand-in for\nstructured outputs.\n\nA workflow declares named, typed outputs. A run writes each one once. A\n`wait: true` caller receives them, and may publish one as its own.\n\n## Where outputs are declared\n\n`outputs:` on the workflow definition, beside `inputs:`, decoded into\n`task.WorkflowInterface.Outputs map[string]OutputSpec` where\n`OutputSpec{Type, Required}`:\n\n```yaml\nid: firewall-contain\noutputs:\n contained: { type: bool, required: true }\n summary: { type: string }\n```\n\nNames and types are the input grammar: the same identifier shape, the same\ntype set, and the same check in `WorkflowInterface.Validate`, which both\n`ParseDefinition` and `ParseWorkflowInterface` call.\n\n**Forced by:** `ParseDefinition` decodes with `KnownFields(true)`, so a key no\ndecoded struct declares is refused — the declaration must live on\n`WorkflowInterface` for an author to write it at all, and that is where\n`docs/development/workflows.md` puts every field a process without the engine\nmust read. The type set is the shared value vocabulary: `TypeAccepts` and\n`ValueType` already check a mapped parameter and an input with it. `required`\ndefaults to false, and the generated YAML for a builtin that declares no\noutputs is unchanged, because a definition's digest pins a run.\n\n**Rejected:** outputs per step, or per call step, because a workflow returns a\nresult as itself rather than as one of its stages. Also `task.Definition`\ngaining outputs: no reader needs them — a binding offers inputs, and a call's\noutputs assignment is checked against the callee's YAML in the same\ncollection — and a declared field with no reader parses and does nothing.\n\n## How a run writes one\n\nEach declared output is offered to the run's agent stages as a capture tool\nnamed after the output, whose arguments are `{\"value\": ...}`; the accepted\ncall's `value` member is the output's value. The engine holds the accepted\nvalues on the task context and writes them into its own task row in the\n`Store.Update` call the run already makes. A deterministic stage may set the\nsame values in Go, and the run's finish check validates them identically.\n\n**Forced by:** a run's only structured results are capture calls\n(`agentexec.CaptureTool`, `Result.Captures`), and the workflow interface\nalready declares when a stage needs them (`task.WorkflowNeeds.Captures`).\nDeriving the tools from the declaration means an output cannot be written\nunder a name nobody declared, and it keeps one route to structured agent\noutput rather than adding a second beside the capture path. The row write is\nthe run's existing write: the task row is where a run's own state lives, and\nthe container already holds `Update` on its own row.\n\nThe tool is offered with `MaxCalls: 1`, and a stage is not offered an output\nthe run has written, so a run writes each output once: a repeat within a stage\nis answered with the tool's rejection and the agent carries on, and a later\nstage cannot overwrite a value. A stage's own capture tool may not share a\nname with a declared output, because the value would otherwise be ambiguous.\n\n**Rejected:** a `workflow.output` step type, because assigning the run's\nresult from a step needs a grammar for \"the previous stage's result\" that does\nnot exist, and a step that reaches the agent would be the second path\n`docs/development/workflows.md` forbids. Also a write RPC of its own: the\ncontainer's `Update` already reaches its own row, and a second write verb for\none column is a second thing to keep in step.\n\n## How a caller receives one\n\nOn a successful `wait: true` call the caller's stage holds the callee's\noutputs as a `map[string]any`. A call step's `outputs:` setting publishes one\nas one of the caller's own outputs:\n\n```yaml\nsteps:\n - type: workflow.call\n workflow: firewall-contain\n wait: true\n inputs: { src_ip: inputs.src_ip }\n outputs: { contained: outputs.contained }\n```\n\nThe key names the callee's declared name; the value names one of the caller's\nown with the prefix the call's `inputs:` already uses. A published value joins\nthe caller's output set for its run, so a caller returns its callee's result to\nits own caller, and a run may publish a callee's output and write its own\nothers.\n\nLifetime is the attempt. The run starts with an empty output set, every later\nstage of that attempt sees the values, and the finish write replaces the row's\nset wholesale — so nothing an attemLine truncated
|
||||
"body": "# Declared outputs of a workflow run\n\n**Status:** Draft\n**Date:** 2026-09-30\n**Bead:** `archie-core-t2db.43`\n**Authority:** `docs/prds/event-automation.md` for what a declared output is,\n`docs/prds/workflow-calls.md` for the call and its grants,\n`docs/prds/state-store-contract.md` for the State Store contract the wire\nchange must satisfy.\n**Supersedes:** `docs/prds/workflow-calls.md`, \"Waiting on the callee\": the\ncallee's transition detail is the human summary, not the stand-in for\nstructured outputs.\n\nA workflow declares named, typed outputs. A run writes each one once. A\n`wait: true` caller receives them, and may publish one as its own.\n\n## Where outputs are declared\n\n`outputs:` on the workflow definition, beside `inputs:`, decoded into\n`task.WorkflowInterface.Outputs map[string]OutputSpec` where\n`OutputSpec{Type, Required}`:\n\n```yaml\nid: firewall-contain\noutputs:\n contained: { type: bool, required: true }\n summary: { type: string }\n```\n\nNames and types are the input grammar: the same identifier shape, the same\ntype set, and the same check in `WorkflowInterface.Validate`, which both\n`ParseDefinition` and `ParseWorkflowInterface` call.\n\n**Forced by:** `ParseDefinition` decodes with `KnownFields(true)`, so a key no\ndecoded struct declares is refused — the declaration must live on\n`WorkflowInterface` for an author to write it at all, and that is where\n`docs/development/workflows.md` puts every field a process without the engine\nmust read. The type set is the shared value vocabulary: `TypeAccepts` and\n`ValueType` already check a mapped parameter and an input with it. `required`\ndefaults to false, and the generated YAML for a builtin that declares no\noutputs is unchanged, because a definition's digest pins a run.\n\n**Rejected:** outputs per step, or per call step, because a workflow returns a\nresult as itself rather than as one of its stages. Also `task.Definition`\ngaining outputs: no reader needs them — a binding offers inputs, and a call's\noutputs assignment is checked against the callee's YAML in the same\ncollection — and a declared field with no reader parses and does nothing.\n\n## How a run writes one\n\nEach declared output is offered to the run's agent stages as a capture tool\nnamed after the output, whose arguments are `{\"value\": ...}`; the accepted\ncall's `value` member is the output's value. The engine holds the accepted\nvalues on the task context and writes them into its own task row in the\n`Store.Update` call the run already makes. A deterministic stage may set the\nsame values in Go, and the run's finish check validates them identically.\n\n**Forced by:** a run's only structured results are capture calls\n(`agentexec.CaptureTool`, `Result.Captures`), and the workflow interface\nalready declares when a stage needs them (`task.WorkflowNeeds.Captures`).\n`WorkflowInterface.Needs` reports `captures` for a non-empty `Outputs`, so\ndeclaring an output alone requires the capability and a Kit profile that\nserves no capture tools is refused before dispatch.\nDeriving the tools from the declaration means an output cannot be written\nunder a name nobody declared, and it keeps one route to structured agent\noutput rather than adding a second beside the capture path. The row write is\nthe run's existing write: the task row is where a run's own state lives, and\nthe container already holds `Update` on its own row.\n\nThe tool is offered with `MaxCalls: 1`, and a stage is not offered an output\nthe run has written, so a run writes each output once: a repeat within a stage\nis answered with the tool's rejection and the agent carries on, and a later\nstage cannot overwrite a value. A stage's own capture tool may not share a\nname with a declared output, because the value would otherwise be ambiguous.\n\n**Rejected:** a `workflow.output` step type, because assigning the run's\nresult from a step needs a grammar for \"the previous stage's result\" that does\nnot exist, and a step that reaches the agent would be the second path\n`docs/development/workflows.md` forbids. Also a write RPC of its own: the\ncontainer's `Update` already reaches its own row, and a second write verb for\none column is a second thing to keep in step.\n\n## How a caller receives one\n\nOn a successful `wait: true` call the caller's stage holds the callee's\noutputs as a `map[string]any`. A call step's `outputs:` setting publishes one\nas one of the caller's own outputs:\n\n```yaml\nsteps:\n - type: workflow.call\n workflow: firewall-contain\n wait: true\n inputs: { src_ip: inputs.src_ip }\n outputs: { contained: outputs.contained }\n```\n\nThe key names the callee's declared name; the value names one of the caller's\nown with the prefix the call's `inputs:` already uses. A published value joins\nthe caller's output set for its run, so a caller returns its callee's result to\nits own caller, and a run may publish a callee's output and write its own\notheLine truncated
|
||||
},
|
||||
{
|
||||
"url": "/dev/prds/workflow-calls/",
|
||||
@@ -574,7 +574,7 @@
|
||||
"url": "/dev/prds/workflow-step-vocabulary/",
|
||||
"title": "Workflow step vocabulary: what a step type is and how binaries agree on it",
|
||||
"section": "PRDs",
|
||||
"body": "# Workflow step vocabulary: what a step type is and how binaries agree on it\n\n**Status:** Draft\n**Date:** 2026-09-29\n**Beads:** `archie-core-m7oc` (agreement between separately deployed binaries), `archie-core-ze7y` (the deleted `.archie/stages/*.go` equivalent)\n**Extends:** `docs/architecture/plugins-and-extensions.md` (plugin engine rule), `docs/prds/event-automation.md` (the workflow model step types serve)\n\n**Authority:** `docs/architecture/plugins-and-extensions.md` — the workflow step vocabulary is a Workflow capability family, and its extension point follows the plugin engine rule.\n\n## Decision\n\nA workflow step type is a named Go factory: the word a YAML workflow writes in\na step's `type` field, plus the factory that builds its stage. The name is a\nstable identifier; the identity also declares what plugin family claims it,\nso a contribution that shadows a shipped stage is refused instead of\noverriding it.\n\nThe Workflow domain owns the typed family contract — `StepTypeProvider` for a\ncontribution and `Manager` as the owning registry. A provider contributes a\nwhole set of step types under its own stable-identifier name; the manager\nvalidates the entire contribution before applying any of it (named provider,\nwell-formed step-type names, non-nil factories, no step type claimed twice,\nno claim on a name another provider holds) and applies it atomically. The\ngeneric `plugin.Plugin` contract stays metadata-only.\n\nThe vocabulary is a compiled-in, closed set. `internal/infrastructure/workflowsteps`\nis the one place a step type is bundled into any binary that resolves one: the\nshipped workflow stages, the repository-hook replacements (`workflow.diff-rules`)\nand the repository-free event steps each arrive as one provider, and adding a\nstep type means adding a provider to that set. Interpreted plugin sources\ncannot contribute step types: the Yaegi plugin host satisfies only the\nmetadata contract, and no bridge to `StepFactory` exists.\n\nEvery binary that resolves a step type builds its manager from that same\nprovider set at its composition root, before its first resolution, and\ninjects the manager everywhere it resolves definitions. Agreement is per\nbuild: the State Store, the daemon's workflow-definitions client and the agent\nworker built from the same source register identical vocabularies by\nconstruction; binaries built from different sources can disagree, and only a\nmatching deploy fixes that.\n\nAn unknown step type is refused twice, and never degraded. The validator\n`ParseDefinition` refuses a definition naming an unregistered type at save\ntime, on the validating side the composition root built (the State Store's\nworkflow-definitions resource validates there before anything is stored). The\ncompiler refuses the same definition at run, so a binary handed a definition\nwritten against a vocabulary it does not have fails that run loudly instead of\nsilently skipping steps. Both checks live in the Workflow domain against the\ninjected manager, which is why no binary can opt out of the check by\nsubstituting its own registry.\n\n## Relationship between the questions\n\n`archie-core-ze7y` is the same question seen from the deleted\nrepository-authored stages: those `.archie/stages/*.go` files held arbitrary\nstages, and their gate half landed as `workflow.diff-rules` settings rather\nthan interpreted Go. Their stage half has no replacement and does not get one\nthat approximates it: no interpreted-Go stage type is built, and no command\nstep type is created by this decision to stand in for them. The refusal of\n`.archie/stages/*.go` stays until the maintainer's trust decision\n(`archie-core-ndm8`) says otherwise.\n\nIf that decision ever allows a command step type, it arrives as a provider in\nthe compiled-in set under the rules above — a new contributor of exactly one\nstep type at a named provider. It is not a plugin contribution mechanism, and\nit does not change any answer in this document.\n\n## Rejected alternatives\n\n- **A vocabulary version or hash on the wire.** The executing side already\n refuses an unknown step type at its first compile, which is the mismatch\n detector, and it fails closed. A wire field would only narrow the\n deploy-skew window that a matching deploy closes, at the cost of a new\n contract field.\n- **A Yaegi-plugin-to-`StepFactory` bridge.** It would run repository-supplied\n interpreted code inside the daemon's process with daemon privileges. The\n trust boundary invariant requires repository-authored code to run in the\n task container, not in an interpreter embeddable into any binary.\n- **A package-level process-global registrar.** Global mutable registry state\n is unreadable at the composition root, collides with itself when one\n process builds the vocabulary twice, and is what made the first attempt at\n this question worse instead of better.\n- **Degrading on an unknown step type.** A run that skipsLine truncated
|
||||
"body": "# Workflow step vocabulary: what a step type is and how binaries agree on it\n\n**Status:** Draft\n**Date:** 2026-09-29\n**Beads:** `archie-core-m7oc` (agreement between separately deployed binaries), `archie-core-ze7y` (the deleted `.archie/stages/*.go` equivalent)\n**Extends:** `docs/architecture/plugins-and-extensions.md` (plugin engine rule), `docs/prds/event-automation.md` (the workflow model step types serve)\n\n**Authority:** `docs/architecture/plugins-and-extensions.md` — the workflow step vocabulary is a Workflow capability family, and its extension point follows the plugin engine rule.\n\n## Decision\n\nA workflow step type is a named Go factory: the word a YAML workflow writes in\na step's `type` field, plus the factory that builds its stage. The name is a\nstable identifier; the identity also declares what plugin family claims it,\nso a contribution that shadows a shipped stage is refused instead of\noverriding it.\n\nThe Workflow domain owns the typed family contract — `StepTypeProvider` for a\ncontribution and `Manager` as the owning registry. A provider contributes a\nwhole set of step types under its own stable-identifier name; the manager\nvalidates the entire contribution before applying any of it (named provider,\nwell-formed step-type names, non-nil factories, no step type claimed twice,\nno claim on a name another provider holds) and applies it atomically. The\ngeneric `plugin.Plugin` contract stays metadata-only.\n\nThe vocabulary is a compiled-in, closed set. `internal/infrastructure/workflowsteps`\nis the one place a step type is bundled into any binary that resolves one: the\nshipped workflow stages, the repository-hook replacements (`gate.diff-rules`)\nand the repository-free event steps each arrive as one provider, and adding a\nstep type means adding a provider to that set. Interpreted plugin sources\ncannot contribute step types: the Yaegi plugin host satisfies only the\nmetadata contract, and no bridge to `StepFactory` exists.\n\nEvery binary that resolves a step type builds its manager from that same\nprovider set at its composition root, before its first resolution, and\ninjects the manager everywhere it resolves definitions. Agreement is per\nbuild: the State Store, the daemon's workflow-definitions client and the agent\nworker built from the same source register identical vocabularies by\nconstruction; binaries built from different sources can disagree, and only a\nmatching deploy fixes that.\n\nAn unknown step type is refused twice, and never degraded. The validator\n`ParseDefinition` refuses a definition naming an unregistered type at save\ntime, on the validating side the composition root built (the State Store's\nworkflow-definitions resource validates there before anything is stored). The\ncompiler refuses the same definition at run, so a binary handed a definition\nwritten against a vocabulary it does not have fails that run loudly instead of\nsilently skipping steps. Both checks live in the Workflow domain against the\ninjected manager, which is why no binary can opt out of the check by\nsubstituting its own registry.\n\n## Relationship between the questions\n\n`archie-core-ze7y` is the same question seen from the deleted\nrepository-authored stages: those `.archie/stages/*.go` files held arbitrary\nstages, and their gate half landed as `gate.diff-rules` settings rather\nthan interpreted Go. Their stage half has no replacement and does not get one\nthat approximates it: no interpreted-Go stage type is built, and no command\nstep type is created by this decision to stand in for them. The refusal of\n`.archie/stages/*.go` stays until the maintainer's trust decision\n(`archie-core-ndm8`) says otherwise.\n\nIf that decision ever allows a command step type, it arrives as a provider in\nthe compiled-in set under the rules above — a new contributor of exactly one\nstep type at a named provider. It is not a plugin contribution mechanism, and\nit does not change any answer in this document.\n\n## Rejected alternatives\n\n- **A vocabulary version or hash on the wire.** The executing side already\n refuses an unknown step type at its first compile, which is the mismatch\n detector, and it fails closed. A wire field would only narrow the\n deploy-skew window that a matching deploy closes, at the cost of a new\n contract field.\n- **A Yaegi-plugin-to-`StepFactory` bridge.** It would run repository-supplied\n interpreted code inside the daemon's process with daemon privileges. The\n trust boundary invariant requires repository-authored code to run in the\n task container, not in an interpreter embeddable into any binary.\n- **A package-level process-global registrar.** Global mutable registry state\n is unreadable at the composition root, collides with itself when one\n process builds the vocabulary twice, and is what made the first attempt at\n this question worse instead of better.\n- **Degrading on an unknown step type.** A run that skips steps iLine truncated
|
||||
}
|
||||
],
|
||||
"nav": [
|
||||
|
||||
@@ -43,6 +43,15 @@ and a decision record for when it is deliberately not enough:
|
||||
draft state, copy conditions, filters and payload building live in plain
|
||||
`.ts` files beside the components and are tested in `ui/test/`. Components
|
||||
stay thin, so there is normally nothing in an SFC that is archie logic.
|
||||
- **A dashboard rule that mirrors a server rule is pinned by a shared
|
||||
fixture, not two independent tests.** `ui/src/settings/validation.ts`
|
||||
hand-mirrors three control-plane validators, and the model-role rule had
|
||||
already drifted because nothing tied the two sides together. Each case now
|
||||
lives once in `internal/app/controlplane/testdata/dashboard-validators.json`:
|
||||
a Go test in that package runs the real server validators over it, and
|
||||
`ui/test/control-plane-validator-parity.test.ts` runs the real dashboard
|
||||
validators over the same cases. Changing one rule alone fails the other
|
||||
side's test. Add a case to the fixture, never to one side only.
|
||||
- **Installed primitives are upstream-tested, not re-tested here.**
|
||||
`reka-ui` (2.10.5 here) maintains colocated tests per component in its own
|
||||
repo (Vitest/jsdom + axe: Switch, TagsInput, NumberField, Select, the
|
||||
|
||||
@@ -28,6 +28,19 @@ vocabulary it registers, so both must see the same step types.
|
||||
6. Add a parse test (settings accepted and refused) and a run test with fake
|
||||
`Trees` and agent runner.
|
||||
|
||||
## Retiring a step type
|
||||
|
||||
A step word cannot simply be deleted. A stored or pinned definition is
|
||||
validated as a whole collection, so one unknown word fails EVERY workflow's
|
||||
definition to decode and the workflow-definitions resource becomes unreadable.
|
||||
Retire a stage by removing it from its workflow's `Stages` and adding it to
|
||||
`retiredSteps` in `definition.go`, which keeps the word resolving as an inert
|
||||
stage. The retired stage must do nothing: the behaviour it used to perform now
|
||||
belongs to whatever layer took it over (the remediate workflow's in-container
|
||||
resume moved to daemon worktree preparation, `archie-core-866m`; the workflow
|
||||
still binds the daemon-prepared directory and the PR branch in a `prepare`
|
||||
stage before any agent work).
|
||||
|
||||
## Changing the definition format
|
||||
|
||||
- The fields a caller needs without the engine (inputs, repository, profile)
|
||||
@@ -37,6 +50,10 @@ vocabulary it registers, so both must see the same step types.
|
||||
- The YAML decoder rejects unknown keys. Existing stored definitions must keep
|
||||
parsing, so a new key is optional with a default that preserves today's
|
||||
behaviour.
|
||||
- A declared `outputs:` entry is written through a capture tool, so it forces
|
||||
the captures need: `task.WorkflowInterface.Needs` resolves that from the
|
||||
declaration rather than the author also writing `needs.captures`. A Kit
|
||||
profile whose harness serves no capture tools is refused before dispatch.
|
||||
- Examples in `examples/workflows/` are parsed against the registered
|
||||
vocabulary by `TestExampleWorkflowsParse`. Add one for a new capability.
|
||||
|
||||
|
||||
@@ -60,6 +60,7 @@
|
||||
"/news/archie/1.44.0/": "/news/1.44.0/",
|
||||
"/news/archie/1.44.1/": "/news/1.44.1/",
|
||||
"/news/archie/1.45.0/": "/news/1.45.0/",
|
||||
"/news/archie/1.46.0/": "/news/1.46.0/",
|
||||
"/news/archie/1.5.0/": "/news/1.5.0/",
|
||||
"/news/archie/1.6.0/": "/news/1.6.0/",
|
||||
"/news/archie/1.7.0/": "/news/1.7.0/",
|
||||
@@ -137,6 +138,7 @@
|
||||
"/news/archied/1.44.0/": "/news/1.44.0/",
|
||||
"/news/archied/1.44.1/": "/news/1.44.1/",
|
||||
"/news/archied/1.45.0/": "/news/1.45.0/",
|
||||
"/news/archied/1.46.0/": "/news/1.46.0/",
|
||||
"/news/archied/1.5.0/": "/news/1.5.0/",
|
||||
"/news/archied/1.6.0/": "/news/1.6.0/",
|
||||
"/news/archied/1.7.0/": "/news/1.7.0/",
|
||||
|
||||
@@ -1,4 +1,9 @@
|
||||
[
|
||||
{
|
||||
"version": "1.46.0",
|
||||
"date": "2026-10-02",
|
||||
"body": "### archied\n\n- feat(workflow): declared workflow outputs for wait:true callers\n- feat(prreview): the operator-approval gate is PRReview's own stage-list entry\n- feat(staterpc): carry the review gate and re-review rounds, with one guarded response write\n- feat(prreview): the gate records the review it holds and posts the operator's answer\n- feat(taskactions): one approval path answers the review gate from every surface\n- feat(webui): the dashboard answers the review gate, including a re-review's instructions\n- feat(daemon): resolve the producing identity's org before publishing work\n- feat(webui): block invalid settings and show a field's former value\n- fix(workflow): derive a declared output's captures need for profile selection\n- fix(storepkg): validate every declared authority grant at the package descriptor\n- feat(archied): refresh the model catalog at runtime\n- feat(mcp): answer server-initiated sampling from the chat model\n- feat(container): send registry auth on image pull\n- feat(messaging): retract ephemeral replies after their TTL\n- feat(messaging): transcribe Telegram voice notes\n- fix(egress): take a run's credential kind from its binding, not the Kit\n- feat(webui): a live-apply setting reads as applied, never as a restart\n- feat(webui): the dashboard reads harness bindings and opens the setup terminal\n- feat(gateway): list the harness page in the dashboard page registry\n- feat(webui): the harness settings page and its setup terminal\n- fix(workflow): park environment gate failures without dispatching a builder\n- feat(messaging): adapter-facing clarify, picker and approval contract\n- feat(gateway): gate admin slash commands by sender policy\n- feat(plugins): load plugin and secret-engine files without a restart\n- fix(workflow): deliver the feasibility PRD to the forge issue\n- fix(messaging): report the daemon version this process was built from\n- fix(messaging): carry an inbound attachment's bytes to the gateway turn\n- fix(daemon): resume a retry whose worktree is gone\n- fix(archied): omit the max-token bound on reasoning-class models\n- fix(logging): file a task log record under its own event time\n- fix(control-plane): unify validation for the domain-managed kinds\n- feat(webui): let the operator choose the retry worktree mode\n- feat(agent): seed a starter SOUL and upgrade only untouched templates\n- feat(controlplane): add the writable review-settings resource\n- fix(messaging): transcribe voice notes on the model-owning side of the boundary\n- fix(messaging): gate the boundary on config reads, not only package links\n- feat(messaging): restart only the channel whose settings changed\n- feat(container): apply container-runtime-policies live\n- feat(tools): reconcile MCP servers and tool settings live\n- feat(messaging): add a per-turn interactive carrier for clarify and picker\n- feat(gateway): offer ask_user when the turn's channel can carry a question\n- feat(telegram): carry clarify and picker through the text fallback\n- fix(webui): pin the dashboard validators to the control plane's\n- refactor(agent): drop the SOUL upgrade branch with no shipped template\n- feat(agentworker): answer MCP sampling from the task's model\n- fix(workflow): bind the remediate worktree before the builder runs\n- fix(config): carry max_retries into the worker TaskConfig snapshot\n- fix(agentworker): supply the guardrail engine to every task run\n\n### archie-agent\n\n- feat(workflow): declared workflow outputs for wait:true callers\n- feat(prreview): the operator-approval gate is PRReview's own stage-list entry\n- feat(staterpc): carry the review gate and re-review rounds, with one guarded response write\n- feat(prreview): the gate records the review it holds and posts the operator's answer\n- feat(taskactions): one approval path answers the review gate from every surface\n- fix(workflow): derive a declared output's captures need for profile selection\n- fix(storepkg): validate every declared authority grant at the package descriptor\n- feat(mcp): answer server-initiated sampling from the chat model\n- feat(container): send registry auth on image pull\n- feat(messaging): retract ephemeral replies after their TTL\n- feat(messaging): transcribe Telegram voice notes\n- fix(egress): take a run's credential kind from its binding, not the Kit\n- fix(workflow): park environment gate failures without dispatching a builder\n- feat(messaging): adapter-facing clarify, picker and approval contract\n- feat(plugins): load plugin and secret-engine files without a restart\n- fix(workflow): deliver the feasibility PRD to the forge issue\n- fix(messaging): carry an inbound attachment's bytes to the gateway turn\n- fix(daemon): resume a retry whose worktree is gone\n- fix(logging): file a task log record under its own event time\n- fix(control-plane): unify validation for the domain-managed kinds\n- feat(webui): let the operator choose the retry worktree mode\n- fix(messaging): transcribe Line truncated
|
||||
},
|
||||
{
|
||||
"version": "1.45.0",
|
||||
"date": "2026-09-30",
|
||||
|
||||
@@ -56,11 +56,18 @@ messages by method name (the same switch that already routes
|
||||
`internal/app/archied/main.go` that routes the sampling request through the
|
||||
same chat-model call path `chatGenerateOptions` already uses, so a
|
||||
server-initiated sample gets the daemon's configured model, not a second
|
||||
provider client.
|
||||
provider client. The agent worker hosts the same configured MCP servers on the
|
||||
task path and runs its own model runtime, so `internal/app/agentworker` wires
|
||||
the same seam from the task's builder model: a server that samples succeeds
|
||||
inside a chat turn and inside a task, never one and not the other. The message
|
||||
mapping and the token bound are shared (`internal/agentexec`, next to the
|
||||
runtime both paths call models through) so both paths bound a sample
|
||||
identically.
|
||||
|
||||
**Reuse check.** The chat model invocation path already exists
|
||||
(`chatGenerateOptions`, `main.go`); this reuses it rather than adding a
|
||||
parallel model-calling path for MCP sampling.
|
||||
(`chatGenerateOptions`, `main.go`) and the task model path already exists
|
||||
(`agentexec.NewRuntime`, the runtime an agent stage runs on); this reuses both
|
||||
rather than adding a parallel model-calling path for MCP sampling.
|
||||
|
||||
**Failure mapping.**
|
||||
|
||||
@@ -170,7 +177,8 @@ preview callbacks fire in order before the next call starts. `task check`.
|
||||
| MCP client call serialisation | `internal/tools/mcp/client.go` | change (`#177`) |
|
||||
| MCP server config | `internal/config/config.go` | new fields: `ParallelToolCalls`, `Sandboxed` |
|
||||
| Sampling dispatch | `internal/tools/mcp/client.go` | new (`#151`) |
|
||||
| Sampling handler wiring | `internal/app/archied/main.go` | new (`#151`), reuses `chatGenerateOptions` |
|
||||
| Sampling handler wiring (chat path) | `internal/app/archied/main.go` | new (`#151`), reuses `chatGenerateOptions` |
|
||||
| Sampling handler wiring (task path) | `internal/app/agentworker/mcp_sampling.go`, `mcp_providers.go` | new (`#151`), answers from the task's builder model |
|
||||
| Sandboxed MCP launch | MCP client constructor + `internal/container/pool.go` | new (`#178`/`#179`) |
|
||||
| Concurrent tool dispatch | dispatch entry point (closed `#183`) | none — already handles this |
|
||||
| Sequential tool dispatch + previews | same dispatch entry point | new (`#182`) |
|
||||
@@ -181,7 +189,7 @@ preview callbacks fire in order before the next call starts. `task check`.
|
||||
|
||||
| sub-feature | issue | implementer scope | suggested council lenses | why |
|
||||
| ------------------------------------ | -------------- | ---------------------------------------------------------------------------------- | ----------------------------------- | ----------------------------------------------------------------------------------------------- |
|
||||
| Server-initiated sampling | `#151` | `client.go` dispatch branch, `main.go` handler wiring | `lens-contract` | new JSON-RPC method surface crossing the MCP wire contract |
|
||||
| Server-initiated sampling | `#151` | `client.go` dispatch branch, chat-path and task-path handler wiring | `lens-contract` | new JSON-RPC method surface crossing the MCP wire contract |
|
||||
| Parallel-tool-calls flag | `#177` | config field, conditional mutex, constructor wiring | `lens-boundary` | per-server behaviour change must not leak into other servers' serialisation |
|
||||
| Sandboxed MCP + Firecracker deferral | `#178`, `#179` | config field, constructor routing through `ContainerPool`, pool entrypoint variant | `lens-operator`, `lens-deletionist` | operator: sandbox-down failure path; deletionist: keeps `#179` from becoming unused scaffolding |
|
||||
| Sequential dispatch with previews | `#182` | `DispatchMode` enum, sequential branch, preview reuse of streaming seam | `lens-maintainer` | two coexisting dispatch modes need a reader to tell which applies without re-deriving both |
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
# Plugin settings reconcile live
|
||||
|
||||
**Status:** Draft
|
||||
**Revision:** 2
|
||||
**Tracking:** GitHub [#1154](https://github.com/samcharles93/archie-core/issues/1154), epic [#998](https://github.com/samcharles93/archie-core/issues/998)
|
||||
**Authority:** `docs/prds/runtime-control-plane.md` decides each feature owns how
|
||||
its changes take effect; `docs/architecture/plugins-and-extensions.md` owns the
|
||||
@@ -9,34 +10,38 @@ reports go.
|
||||
|
||||
## Outcome
|
||||
|
||||
An operator drops a plugin, module or secret-engine file into the configured
|
||||
An operator drops a plugin, module or secret-engine file into a configured
|
||||
directory while archied runs, and the daemon loads it without a restart. An
|
||||
edit to an installed file replaces what is running. Deleting an installed file
|
||||
never stops running code; apply status says so. A stored change to
|
||||
`plugin-settings` applies live for additions instead of requiring a restart.
|
||||
edit to an installed file replaces what is running. A delete never stops running
|
||||
code; apply status says so. A stored `plugin-settings` change applies live for
|
||||
`plugin_dir`, `module_dir` and `secret_engine_dir`; a change to `skills_dir` is
|
||||
refused.
|
||||
|
||||
## Triggers
|
||||
|
||||
`plugin-settings` joins the kinds archied watches (`PluginSettingsKind` moves
|
||||
to the live list in `internal/app/archied/control_plane.go`). After the daemon
|
||||
layers a new version it reconciles the three directories the document names.
|
||||
A second trigger needs no document change: a poller reconciles on an interval,
|
||||
so a file dropped into a directory loads on its own. The poll interval is the
|
||||
apply-status restamp constant; no filesystem-watch dependency is added. The
|
||||
poller reads the current document's directories from the running config each
|
||||
tick, so a changed `plugin_dir`, `module_dir` or `secret_engine_dir` retargets
|
||||
reconciliation without a restart.
|
||||
`PluginSettingsKind` joins `runtimeResourceKinds` in
|
||||
`internal/app/archied/control_plane.go`, and its descriptor `ApplyMode` becomes
|
||||
`live` in `internal/app/controlplane/operational_settings.go`.
|
||||
|
||||
A poller reconciles on the apply-status restamp interval, so a dropped file
|
||||
loads without a filesystem watch. Each tick re-runs both halves the boot path
|
||||
runs, against the directories in the running config: the directory loads
|
||||
(`secret.Registry.LoadDir`, `ModuleRegistry.Register`, `plugin.LoadDir`) and the
|
||||
layering (`boot.runtimeConfig`), which re-resolves provider credentials. A
|
||||
provider disabled at boot for an unresolvable engine is re-resolved on the first
|
||||
tick that finds it; a changed directory retargets the next tick without a
|
||||
restart.
|
||||
|
||||
## Rules
|
||||
|
||||
- Adding and changing load; removing never unloads. Yaegi interpreters are not
|
||||
reclaimed while a caller still exists and there is no unload path; the
|
||||
running set can only grow.
|
||||
- Each file is loaded through a fresh interpreter (the boot pattern). A changed
|
||||
file therefore loads cleanly, and replacement switches consumers to the new
|
||||
interpretation; files are content-hashed so only new and changed files load.
|
||||
reclaimed while a caller still exists and there is no unload path; the running
|
||||
set can only grow. A removal stays outstanding in the apply-status error until
|
||||
the process restarts.
|
||||
- Each file is loaded through a fresh interpreter (the boot pattern) and
|
||||
content-hashed, so only new and changed files load.
|
||||
- A file that fails to load is skipped and reported; already-running code keeps
|
||||
running. This is the degrade-and-skip convention `plugin.LoadDir` and
|
||||
running, the degrade-and-skip convention `plugin.LoadDir` and
|
||||
`Registry.LoadDir` already apply at boot. Boot keeps its reject-at-load rule
|
||||
for the module directory; a broken file at runtime skips without aborting.
|
||||
- A directory that does not exist reconciles to an empty set.
|
||||
@@ -44,45 +49,86 @@ reconciliation without a restart.
|
||||
## The three registries
|
||||
|
||||
- **Secret engines** (`internal/secret`). Reconciliation re-runs
|
||||
`Registry.LoadDir` against the live directory. `Register` replaces by name,
|
||||
so an edited engine takes effect for the next resolution. A deleted engine
|
||||
stays registered: existing references keep resolving through it until
|
||||
restart, and the record says so. Engines are stateless resolvers, so a swap
|
||||
needs no lifecycle step.
|
||||
- **Modules** (`internal/domain/eda/module`): reconciliation re-runs
|
||||
`ModuleRegistry.Register` for the kind's file. The invoker maps
|
||||
last-write-wins, and playbook dispatch resolves through the registry at
|
||||
invoke time, so the swap reaches a running playbook store unchanged. A file
|
||||
deleted for a kind leaves the old invoker in place, running; reported.
|
||||
- **Plugins** (`internal/plugin`): `Host` grows running-state operations beside
|
||||
`Register`, `Start` and `Stop`. One adds a single module while the host is
|
||||
running: it is inserted in dependency order, started alone, and on failure
|
||||
rolled back alone, leaving every previously started module running. The
|
||||
other replaces a module whose manifest id matches: stop it, start the new
|
||||
one, and restart the old one when the new start fails. Replacing a module
|
||||
that another depends on is refused, not swapped. These operations obey the
|
||||
plugin engine rule: the host still invokes start and stop itself, isolates
|
||||
the failure to the one module, and keeps a rollback. A deleted file makes no
|
||||
host call: the module runs on and is reported.
|
||||
`Registry.LoadDir` against the live directory. `Register` replaces by name, so
|
||||
an edited engine takes effect for the next resolution and a dropped engine
|
||||
resolves on the next tick. A deleted engine stays registered: existing
|
||||
references keep resolving through it until restart. Engines are stateless
|
||||
resolvers, so a swap needs no lifecycle step.
|
||||
- **Modules** (`internal/domain/eda/module`). Reconciliation re-runs
|
||||
`ModuleRegistry.Register` for the kind's file. Playbook dispatch resolves
|
||||
through the registry at invoke time, so the swap reaches a running playbook
|
||||
store unchanged; a deleted file leaves the old invoker in place, running.
|
||||
- **Plugins** (`internal/plugin`). `Host` grows running-state operations beside
|
||||
`Register`, `Start` and `Stop`, and `Register` keeps its rule that
|
||||
registration is allowed only before the first `Start`, so these are new host
|
||||
methods rather than a relaxed `Register`. One adds a single module while the
|
||||
host is running: inserted in dependency order, started alone, and rolled back
|
||||
alone on failure, leaving every previously started module running. The other
|
||||
replaces a module whose manifest id matches: stop it, start the new one, and
|
||||
restart the old one when the new start fails; replacing a module another
|
||||
running module depends on is refused. A module's `Start` is its only check
|
||||
(`plugin.Module` has no probe, and one manifest id cannot be registered
|
||||
twice), so a replacement checks by starting and keeps a rollback rather than
|
||||
switching first. These operations obey the plugin engine rule: the host
|
||||
invokes start and stop itself and isolates the failure to one module. A
|
||||
deleted file makes no host call: the module runs on.
|
||||
|
||||
## `skills_dir`
|
||||
|
||||
`skills_dir` is not part of this change. The daemon refuses a candidate
|
||||
`plugin-settings` document whose `skills_dir` differs from the value it is
|
||||
running, reporting that the field applies on restart, until a skill
|
||||
reconciliation exists to carry it. The refusal is the existing rejected-edit
|
||||
apply-status shape: the store keeps the version, the document is not applied,
|
||||
`skills_dir` is not part of this change. The daemon refuses a `plugin-settings`
|
||||
document whose `skills_dir` differs from the value in force. The check is a
|
||||
pre-layer comparison on the live path (`boot.applyRuntimeResourceUpdate` in
|
||||
`internal/app/archied/control_plane.go`), not an addition to
|
||||
`validatePluginSettings` in `internal/app/controlplane/operational_settings.go`,
|
||||
which stays value-agnostic per the #1143 decision recorded there: a
|
||||
change-immutability rule needs the running config, and #1143's validates-nothing
|
||||
still holds for values. The write lands, the store keeps the version, the daemon
|
||||
publishes nothing, the refusal is reported against that stored version, and
|
||||
last-known-good keeps running.
|
||||
|
||||
The layering assigns all four directories from one document, so a refused
|
||||
`skills_dir` refuses the whole document and the other kinds still layer. That is
|
||||
acceptable: `skills_dir` is the one field with no live consumer, a
|
||||
whole-document refusal is the honest signal that none of it took effect, and an
|
||||
operator changing `skills_dir` alongside another directory writes the two
|
||||
changes separately.
|
||||
|
||||
## Apply status
|
||||
|
||||
One record: `archied` on `plugin-settings`. The reconciliation outcome is
|
||||
reported against the version the process is running. The record's error field
|
||||
carries the outstanding reconciliation problem: a deleted file still running, a
|
||||
file that failed to load, or the directory failing to read. A reconciliation
|
||||
with nothing outstanding clears the error. The version stays the version the
|
||||
process is running; an add that succeeded is live even while a removal is
|
||||
outstanding, and the error says so rather than showing the kind as
|
||||
restart-required. archie-gateway keeps boot-time loading; its record simply
|
||||
stays on the version it last applied until it restarts, which the per-process
|
||||
apply-status page already reads correctly.
|
||||
One record per process: `archied` on `plugin-settings`, reported against the
|
||||
version the process is running. The record's error field carries the outstanding
|
||||
problem: a deleted file still running, a file that failed to load, the directory
|
||||
failing to read, or a refused replace of a module another running module depends
|
||||
on. A clean reconciliation clears the error. An add that succeeded is live even
|
||||
while a removal is outstanding, and the error says so rather than showing the
|
||||
kind as restart-required.
|
||||
|
||||
`ApplyMode` becomes `live`, so `RestartPendingBanner` no longer names
|
||||
`plugin-settings`. The gateway's lag is shown instead by the per-process
|
||||
applied-version rows on the Plugins page, which read the state
|
||||
`applyStatusForKind` derives from `applied_version < storedVersion` in
|
||||
`ui/src/stores/control-plane.ts`; that derivation is independent of
|
||||
`ApplyMode`.
|
||||
|
||||
`archie-gateway` layers the `plugin-settings` document at boot through
|
||||
`boot.loadRuntimeConfig` and reports it, but never calls `boot.loadPlugins` or
|
||||
`boot.loadModules` and never reads the four directories, so it loads no plugin,
|
||||
module or secret engine. With no live watch its row lags the stored version
|
||||
until restart; that lag is a reporting lag, not a behaviour gap.
|
||||
|
||||
## Verification
|
||||
|
||||
- A plugin, module and secret-engine file dropped while the daemon runs loads on
|
||||
the next tick; an unchanged file is not reloaded.
|
||||
- An edited file replaces the running interpretation; a deleted file leaves the
|
||||
running code running, with the process's error naming it until restart.
|
||||
- A `skills_dir` change refuses the document: the store keeps the version, the
|
||||
record carries that version and the refusal, and the running `skills_dir` is
|
||||
unchanged.
|
||||
- A plugin replacement whose new `Start` fails restarts the old module and
|
||||
reports the failure; a replacement of a depended-on module is refused and the
|
||||
dependent keeps running.
|
||||
- A provider disabled at boot for a missing engine resolves again after the
|
||||
engine file loads and a tick re-layers.
|
||||
- The gateway reports the last version it layered at boot, loads no directory,
|
||||
and its lag appears in its applied-version row rather than the restart banner.
|
||||
@@ -12,7 +12,7 @@ The dashboard's `POST /api/tasks/{id}/action` (`internal/webui/api_tasks.go`, `h
|
||||
|
||||
The path is `handleTaskAction` → `applyOperatorTaskAction` → `Chat.Contract.ApplyOperatorTaskAction` → the daemon's `LocalChatAdapter` → `taskactions.Client` (NATS `archie.gateway.task-action`) → `taskactions.Service.Apply` → one guarded store write that records the decision and requeues. Chat's `/approve` path (`StoreTaskController.Approve` → `chatTaskControllerAdapter.ApproveChatTask`) must reach that same write through the same function, so the two surfaces cannot record different decisions for one operator intent.
|
||||
|
||||
A requeue resumes the workflow the waiting state names, never a name the handler hardcodes. `taskactions.Service.apply` and `chatTaskControllerAdapter.ApproveChatTask` requeue every approval under `implement`, which is the feasibility handoff: an approval whose wait recorded `pr-review` would run the implement workflow. Feasibility must keep that handoff by naming `implement` on the task before it enters the wait, which is the invariant the handler needs — the workflow a task carries is the workflow it runs next. `Requeue` treats an empty workflow as "keep the task's".
|
||||
A requeue resumes the workflow the waiting state names, never a name the handler hardcodes. `taskactions.Service.apply` and `chatTaskControllerAdapter.ApproveChatTask` requeue with an empty workflow, which `Requeue` reads as "keep the task's", so neither surface hardcodes a workflow name: an approval whose wait recorded `pr-review` resumes `pr-review`. The feasibility handoff therefore has to name `implement` on the task before it enters the wait, which is the invariant the handler relies on — the workflow a task carries is the workflow it runs next. `Requeue` treats an empty workflow as "keep the task's".
|
||||
|
||||
### The review the operator answers
|
||||
|
||||
|
||||
@@ -103,7 +103,10 @@ Users create and edit workflows in the Web UI. Workflows are stored as versioned
|
||||
YAML. Built-in workflows can be overridden and restored.
|
||||
|
||||
A workflow is a list of steps. Each step has a known type and its own settings.
|
||||
Plugins can add step types. Each run keeps the workflow version it started with.
|
||||
A step type is part of the build's step vocabulary, contributed as a provider at
|
||||
the composition root; interpreted plugins contribute none
|
||||
(`docs/prds/workflow-step-vocabulary.md`). Each run keeps the workflow version it
|
||||
started with.
|
||||
|
||||
Yaegi, `.archie/stages/*.go`, and `.archie/gate.go` will be removed. Their useful
|
||||
behaviour must first be available as normal workflow steps.
|
||||
|
||||
@@ -58,6 +58,9 @@ same values in Go, and the run's finish check validates them identically.
|
||||
**Forced by:** a run's only structured results are capture calls
|
||||
(`agentexec.CaptureTool`, `Result.Captures`), and the workflow interface
|
||||
already declares when a stage needs them (`task.WorkflowNeeds.Captures`).
|
||||
`WorkflowInterface.Needs` reports `captures` for a non-empty `Outputs`, so
|
||||
declaring an output alone requires the capability and a Kit profile that
|
||||
serves no capture tools is refused before dispatch.
|
||||
Deriving the tools from the declaration means an output cannot be written
|
||||
under a name nobody declared, and it keeps one route to structured agent
|
||||
output rather than adding a second beside the capture path. The row write is
|
||||
|
||||
@@ -25,7 +25,7 @@ generic `plugin.Plugin` contract stays metadata-only.
|
||||
|
||||
The vocabulary is a compiled-in, closed set. `internal/infrastructure/workflowsteps`
|
||||
is the one place a step type is bundled into any binary that resolves one: the
|
||||
shipped workflow stages, the repository-hook replacements (`workflow.diff-rules`)
|
||||
shipped workflow stages, the repository-hook replacements (`gate.diff-rules`)
|
||||
and the repository-free event steps each arrive as one provider, and adding a
|
||||
step type means adding a provider to that set. Interpreted plugin sources
|
||||
cannot contribute step types: the Yaegi plugin host satisfies only the
|
||||
@@ -53,7 +53,7 @@ substituting its own registry.
|
||||
|
||||
`archie-core-ze7y` is the same question seen from the deleted
|
||||
repository-authored stages: those `.archie/stages/*.go` files held arbitrary
|
||||
stages, and their gate half landed as `workflow.diff-rules` settings rather
|
||||
stages, and their gate half landed as `gate.diff-rules` settings rather
|
||||
than interpreted Go. Their stage half has no replacement and does not get one
|
||||
that approximates it: no interpreted-Go stage type is built, and no command
|
||||
step type is created by this decision to stand in for them. The refusal of
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
# The containment workflow a firewall-investigate run calls:
|
||||
# firewall-investigate (workflow.call, wait: true) hands its findings upward
|
||||
# from its declared summary, and this workflow's own agent stage decides and
|
||||
# records the containment as its own declared outputs. Paste it into the
|
||||
# dashboard's workflow definitions alongside firewall-investigate
|
||||
# (docs/prds/workflow-call-outputs.md).
|
||||
id: firewall-contain
|
||||
repository: none
|
||||
profile: network-investigator
|
||||
inputs:
|
||||
src_ip: { type: string, required: true }
|
||||
outputs:
|
||||
contained: { type: bool, required: true }
|
||||
summary: { type: string }
|
||||
steps:
|
||||
- type: workflow.call
|
||||
settings:
|
||||
workflow: firewall-investigate
|
||||
wait: true
|
||||
inputs: { src_ip: "inputs.src_ip" }
|
||||
outputs: { summary: "outputs.summary" }
|
||||
- type: agent.run
|
||||
settings:
|
||||
mission: >-
|
||||
Using the investigation summary the called workflow returned, decide
|
||||
whether the source address in the inputs should be contained. Write
|
||||
your decision with the contained tool, and a one-line rationale with
|
||||
the summary tool.
|
||||
@@ -2,17 +2,24 @@
|
||||
# src_ip from a mapped parameter and runs one agent mission in a scratch
|
||||
# workspace under the network-investigator profile. Paste it into the
|
||||
# dashboard's workflow definitions.
|
||||
#
|
||||
# Its declared output (summary) is offered to the agent stages as a capture
|
||||
# tool named after it; a wait:true caller publishes it as one of its own
|
||||
# outputs (docs/prds/workflow-call-outputs.md).
|
||||
id: firewall-investigate
|
||||
repository: none
|
||||
profile: network-investigator
|
||||
inputs:
|
||||
src_ip: { type: string, required: true }
|
||||
severity: { type: string }
|
||||
outputs:
|
||||
summary: { type: string }
|
||||
steps:
|
||||
- type: agent.run
|
||||
settings:
|
||||
mission: >-
|
||||
Investigate the source address in the inputs: who owns it, what it
|
||||
resolves to, and whether it is reachable. Report what you found and
|
||||
how serious it looks given the severity.
|
||||
how serious it looks given the severity, and write your one-line
|
||||
conclusion with the summary tool.
|
||||
read_only: true
|
||||
@@ -77,6 +77,27 @@ func runGit(t *testing.T, dir string, args ...string) {
|
||||
}
|
||||
}
|
||||
|
||||
// hostileSigningConfig writes a global git config that makes every commit
|
||||
// fail unless the fixture disables signing in the repository's own config.
|
||||
func hostileSigningConfig(t *testing.T) string {
|
||||
t.Helper()
|
||||
path := filepath.Join(t.TempDir(), "hostile.gitconfig")
|
||||
const cfg = "[user]\n\tname = Hostile\n\temail = hostile@example.com\n\tsigningkey = /nonexistent/signing-key\n[gpg]\n\tformat = ssh\n[commit]\n\tgpgsign = true\n"
|
||||
if err := os.WriteFile(path, []byte(cfg), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return path
|
||||
}
|
||||
|
||||
// TestFixtureSeedsARepositoryThatIgnoresHostSigningConfig pins the fixture's
|
||||
// hermeticity: it must not depend on the developer's global git identity or a
|
||||
// signing agent.
|
||||
func TestFixtureSeedsARepositoryThatIgnoresHostSigningConfig(t *testing.T) {
|
||||
t.Setenv("GIT_CONFIG_GLOBAL", hostileSigningConfig(t))
|
||||
t.Setenv("GIT_CONFIG_NOSYSTEM", "1")
|
||||
newFixture(t, "edit")
|
||||
}
|
||||
|
||||
type fixture struct {
|
||||
workspace string
|
||||
log string
|
||||
@@ -87,6 +108,7 @@ func newFixture(t *testing.T, scenario string) *fixture {
|
||||
t.Helper()
|
||||
ws := t.TempDir()
|
||||
runGit(t, ws, "init", "-q")
|
||||
runGit(t, ws, "config", "commit.gpgsign", "false")
|
||||
if err := os.WriteFile(filepath.Join(ws, "widget_test.go"), []byte("package widget\n"), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,66 @@
|
||||
package agentexec
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"github.com/samcharles93/ai-sdk/chat"
|
||||
|
||||
protocol "github.com/samcharles93/archie-core/internal/tools/mcp"
|
||||
)
|
||||
|
||||
// DefaultSamplingMaxTokens bounds a server-requested completion when the
|
||||
// request does not set maxTokens. A sampling request is a delegated
|
||||
// sub-completion, not a full turn, so one MCP server cannot spend a
|
||||
// turn-sized output allowance on a single request.
|
||||
const DefaultSamplingMaxTokens = 4096
|
||||
|
||||
// SamplingMessages maps a server's sampling messages onto chat messages. Both
|
||||
// MCP sampling host paths use it -- the daemon's chat handler and the agent
|
||||
// worker's task handler -- so the same server is answered the same way
|
||||
// wherever the client runs. A non-text content block or an unknown role is
|
||||
// rejected rather than silently dropped: answering from a partial prompt would
|
||||
// be a wrong answer, not a degraded one.
|
||||
func SamplingMessages(req protocol.SamplingRequest) ([]chat.Message, error) {
|
||||
if len(req.Messages) == 0 {
|
||||
return nil, errors.New("mcp sampling: the request has no messages")
|
||||
}
|
||||
messages := make([]chat.Message, 0, len(req.Messages))
|
||||
for i, message := range req.Messages {
|
||||
if message.Content.Type != "" && message.Content.Type != "text" {
|
||||
return nil, fmt.Errorf("mcp sampling: message %d has unsupported content type %q", i, message.Content.Type)
|
||||
}
|
||||
var role chat.Role
|
||||
switch message.Role {
|
||||
case "user":
|
||||
role = chat.RoleUser
|
||||
case "assistant":
|
||||
role = chat.RoleAssistant
|
||||
default:
|
||||
return nil, fmt.Errorf("mcp sampling: message %d has unsupported role %q", i, message.Role)
|
||||
}
|
||||
messages = append(messages, chat.Message{Role: role, Content: message.Content.Text})
|
||||
}
|
||||
return messages, nil
|
||||
}
|
||||
|
||||
// SamplingMaxTokens bounds a delegated completion to the request's own
|
||||
// maxTokens, falling back to DefaultSamplingMaxTokens, and never past the
|
||||
// model's own output ceiling (zero when the ceiling is unknown). A
|
||||
// reasoning-class model gets no bound at all: its provider rejects the
|
||||
// `max_tokens` parameter the chat-completions provider emits for a non-zero
|
||||
// bound. Both the daemon's chat path and the agent worker answer sampling
|
||||
// with this, so the same server gets the same bound on either path.
|
||||
func SamplingMaxTokens(requested, modelCeiling int, reasoning bool) int {
|
||||
maxTokens := requested
|
||||
if maxTokens <= 0 {
|
||||
maxTokens = DefaultSamplingMaxTokens
|
||||
}
|
||||
if modelCeiling > 0 && maxTokens > modelCeiling {
|
||||
maxTokens = modelCeiling
|
||||
}
|
||||
if reasoning {
|
||||
return 0
|
||||
}
|
||||
return maxTokens
|
||||
}
|
||||
@@ -0,0 +1,98 @@
|
||||
package agentexec
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/samcharles93/ai-sdk/chat"
|
||||
|
||||
protocol "github.com/samcharles93/archie-core/internal/tools/mcp"
|
||||
)
|
||||
|
||||
func TestSamplingMessagesMapsRolesAndRejectsUnsupportedInput(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
request protocol.SamplingRequest
|
||||
want []chat.Message
|
||||
wantErr string
|
||||
}{
|
||||
{
|
||||
name: "user and assistant text map to chat roles",
|
||||
request: protocol.SamplingRequest{Messages: []protocol.SamplingMessage{
|
||||
{Role: "user", Content: protocol.SamplingContent{Type: "text", Text: "2+2?"}},
|
||||
{Role: "assistant", Content: protocol.SamplingContent{Type: "", Text: "4"}},
|
||||
}},
|
||||
want: []chat.Message{
|
||||
{Role: chat.RoleUser, Content: "2+2?"},
|
||||
{Role: chat.RoleAssistant, Content: "4"},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "no messages is a refusal",
|
||||
request: protocol.SamplingRequest{},
|
||||
wantErr: "the request has no messages",
|
||||
},
|
||||
{
|
||||
name: "non-text content is a refusal",
|
||||
request: protocol.SamplingRequest{Messages: []protocol.SamplingMessage{
|
||||
{Role: "user", Content: protocol.SamplingContent{Type: "image"}},
|
||||
}},
|
||||
wantErr: "unsupported content type",
|
||||
},
|
||||
{
|
||||
name: "unknown role is a refusal",
|
||||
request: protocol.SamplingRequest{Messages: []protocol.SamplingMessage{
|
||||
{Role: "system", Content: protocol.SamplingContent{Type: "text", Text: "hi"}},
|
||||
}},
|
||||
wantErr: "unsupported role",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range tests {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
got, err := SamplingMessages(tc.request)
|
||||
if tc.wantErr != "" {
|
||||
if err == nil || !strings.Contains(err.Error(), tc.wantErr) {
|
||||
t.Fatalf("SamplingMessages error = %v, want it to mention %q", err, tc.wantErr)
|
||||
}
|
||||
return
|
||||
}
|
||||
if err != nil {
|
||||
t.Fatalf("SamplingMessages error = %v, want nil", err)
|
||||
}
|
||||
if len(got) != len(tc.want) {
|
||||
t.Fatalf("SamplingMessages returned %d messages, want %d", len(got), len(tc.want))
|
||||
}
|
||||
for i := range got {
|
||||
if got[i].Role != tc.want[i].Role || got[i].Content != tc.want[i].Content {
|
||||
t.Errorf("message %d = %+v, want %+v", i, got[i], tc.want[i])
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestSamplingMaxTokens(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
requested int
|
||||
ceiling int
|
||||
reasoning bool
|
||||
wantTokens int
|
||||
}{
|
||||
{name: "requested bound is honoured", requested: 64, ceiling: 1024, wantTokens: 64},
|
||||
{name: "absent request falls back to the default", requested: 0, ceiling: 8192, wantTokens: DefaultSamplingMaxTokens},
|
||||
{name: "request is capped by the model ceiling", requested: 8192, ceiling: 1024, wantTokens: 1024},
|
||||
{name: "unknown ceiling keeps the request", requested: 8192, ceiling: 0, wantTokens: 8192},
|
||||
{name: "reasoning model gets no bound", requested: 64, ceiling: 1024, reasoning: true, wantTokens: 0},
|
||||
{name: "reasoning model with absent request gets no bound", requested: 0, ceiling: 0, reasoning: true, wantTokens: 0},
|
||||
}
|
||||
|
||||
for _, tc := range tests {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
if got := SamplingMaxTokens(tc.requested, tc.ceiling, tc.reasoning); got != tc.wantTokens {
|
||||
t.Fatalf("SamplingMaxTokens(%d, %d, %v) = %d, want %d", tc.requested, tc.ceiling, tc.reasoning, got, tc.wantTokens)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -93,6 +93,18 @@ func TestSystemLogHandlerPublishesToTheTasksSystemSubject(t *testing.T) {
|
||||
if entry.Fields["component"] != "gate" {
|
||||
t.Errorf("Fields[component] = %v, want gate", entry.Fields["component"])
|
||||
}
|
||||
// The wire entry must carry the record's own event time. The daemon files
|
||||
// a task's records under the time the event happened (see
|
||||
// logging.TaskRegistry.Write), so a producer that dropped or re-stamped it
|
||||
// here would collapse a run's timing before it ever left the container.
|
||||
next.mu.Lock()
|
||||
wantTime := next.records[0].Time
|
||||
next.mu.Unlock()
|
||||
if entry.Time.IsZero() {
|
||||
t.Error("Entry.Time is the zero time, want the record's own event time")
|
||||
} else if !entry.Time.Equal(wantTime) {
|
||||
t.Errorf("Entry.Time = %s, want the record's event time %s", entry.Time, wantTime)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSystemLogHandlerAlwaysTeesToNextRegardlessOfPublishOutcome(t *testing.T) {
|
||||
|
||||
@@ -0,0 +1,93 @@
|
||||
package agentworker
|
||||
|
||||
import (
|
||||
"log/slog"
|
||||
"testing"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/agentexec"
|
||||
"github.com/samcharles93/archie-core/internal/config"
|
||||
"github.com/samcharles93/archie-core/internal/domain/workflow"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/postgres/pgstore"
|
||||
"github.com/samcharles93/archie-core/internal/taskrun"
|
||||
"github.com/samcharles93/archie-core/internal/tools"
|
||||
)
|
||||
|
||||
// guardrailRunYAML is a repository-none workflow with one agent stage: a task
|
||||
// run reaches AgentStage.handleResult once, and the engine that recorded that
|
||||
// outcome is observable by its call count and by the decision a low threshold
|
||||
// earns.
|
||||
const guardrailRunYAML = "id: guardrail\nrepository: none\nsteps:\n" +
|
||||
" - type: agent.run\n settings:\n mission: first\n"
|
||||
|
||||
// TestProductionWorkerDependenciesBuildGuardrails pins the producer: the
|
||||
// process that runs agent stages is archie-agent, so its composition root must
|
||||
// build the guardrail engine every TaskContext records against. A daemon-built
|
||||
// engine can never reach this process -- it is a separate binary -- so before
|
||||
// this wiring TaskContext.Guardrails was nil on every production run and no
|
||||
// decision was ever recorded.
|
||||
func TestProductionWorkerDependenciesBuildGuardrails(t *testing.T) {
|
||||
deps, err := productionWorkerDependencies()
|
||||
if err != nil {
|
||||
t.Fatalf("productionWorkerDependencies: %v", err)
|
||||
}
|
||||
if deps.guardrails == nil {
|
||||
t.Fatal("worker composition built no guardrail engine: TaskContext.Guardrails stays nil and no agent-stage decision is recorded")
|
||||
}
|
||||
}
|
||||
|
||||
// TestRunTaskRecordsGuardrailDecision is the end-to-end half: a workflow run
|
||||
// whose TaskContext carries the engine records its agent stage's outcome. Before
|
||||
// the wiring the engine was never assigned, so the run recorded zero -- the
|
||||
// guardrail engine observed nothing for any task.
|
||||
func TestRunTaskRecordsGuardrailDecision(t *testing.T) {
|
||||
ctx := t.Context()
|
||||
st := pgstore.Open(t)
|
||||
|
||||
task, err := st.EnqueueChatTask(ctx, "", "", "guardrail recording", "", "guardrail", "", nil)
|
||||
if err != nil {
|
||||
t.Fatalf("seed task: %v", err)
|
||||
}
|
||||
if err := st.Transition(ctx, task.ID, task.Status, workflow.StatusRunning, "started"); err != nil {
|
||||
t.Fatalf("claim task: %v", err)
|
||||
}
|
||||
task.Status = workflow.StatusRunning
|
||||
task.WorkflowDefinitionYAML = guardrailRunYAML
|
||||
task.WorkflowDefinitionDigest = workflow.DigestDefinition(guardrailRunYAML)
|
||||
if err := st.Update(ctx, task); err != nil {
|
||||
t.Fatalf("seed task: %v", err)
|
||||
}
|
||||
|
||||
workDir := t.TempDir()
|
||||
// A threshold a single stage reaches turns the recorded outcome into an
|
||||
// actual guardrail decision, so the test proves the run consulted the
|
||||
// engine rather than merely holding a reference.
|
||||
engine := tools.NewGuardrailEngine(tools.ToolCallGuardrailConfig{
|
||||
NoProgressWarnAfter: 1,
|
||||
HardStopAfterWarnRepeat: 1,
|
||||
})
|
||||
request := taskrun.Request{
|
||||
Task: task,
|
||||
Repo: config.Repo{},
|
||||
Cfg: config.Config{Models: map[string]string{"builder": "p/m"}}.ForTask(),
|
||||
WorkflowDefinition: guardrailRunYAML,
|
||||
}
|
||||
dependencies := taskDependencies{
|
||||
store: st,
|
||||
trees: &fakeScratchTrees{dir: workDir},
|
||||
steps: testSteps(t),
|
||||
guardrails: engine,
|
||||
}
|
||||
fakeRunner := runnerFactory(func(map[string]agentexec.Provider, *slog.Logger) agentexec.Runner {
|
||||
return stubRunner{}
|
||||
})
|
||||
if _, err := runTask(ctx, request, dependencies, fakeRunner, workDir, slog.New(slog.DiscardHandler)); err != nil {
|
||||
t.Fatalf("runTask: %v", err)
|
||||
}
|
||||
|
||||
if got := engine.TotalCalls(); got != 1 {
|
||||
t.Fatalf("guardrail engine recorded %d tool outcomes, want 1 (the agent stage): TaskContext.Guardrails is not wired to the engine the run was given", got)
|
||||
}
|
||||
if !engine.HardStopped() {
|
||||
t.Fatal("guardrail engine issued no decision for the run's agent stage: TaskContext.Guardrails is not wired to the engine the run was given")
|
||||
}
|
||||
}
|
||||
@@ -23,18 +23,26 @@ type mcpProviderSet struct {
|
||||
registry *tools.Registry
|
||||
}
|
||||
|
||||
type mcpProviderBuilder func(config.MCPServer) (toolprovider.Engine, string, error)
|
||||
type mcpProviderBuilder func(config.MCPServer, mcp.SamplingHandler) (toolprovider.Engine, string, error)
|
||||
|
||||
// startMCPProviders creates MCP transports from config, starts them,
|
||||
// discovers tools, and registers them into a local registry. Returns
|
||||
// nil when there are no servers configured.
|
||||
func startMCPProviders(ctx context.Context, servers []config.MCPServer, log *slog.Logger) (*mcpProviderSet, error) {
|
||||
return startMCPProvidersWith(ctx, servers, log, buildMCPProvider)
|
||||
// discovers tools, and registers them into a local registry. A non-nil
|
||||
// sampling handler answers server-initiated sampling/createMessage requests
|
||||
// (nil leaves the method refused, the client's own default). Returns nil when
|
||||
// there are no servers configured.
|
||||
func startMCPProviders(
|
||||
ctx context.Context,
|
||||
servers []config.MCPServer,
|
||||
sampling mcp.SamplingHandler,
|
||||
log *slog.Logger,
|
||||
) (*mcpProviderSet, error) {
|
||||
return startMCPProvidersWith(ctx, servers, sampling, log, buildMCPProvider)
|
||||
}
|
||||
|
||||
func startMCPProvidersWith(
|
||||
ctx context.Context,
|
||||
servers []config.MCPServer,
|
||||
sampling mcp.SamplingHandler,
|
||||
log *slog.Logger,
|
||||
build mcpProviderBuilder,
|
||||
) (*mcpProviderSet, error) {
|
||||
@@ -51,7 +59,7 @@ func startMCPProvidersWith(
|
||||
continue
|
||||
}
|
||||
|
||||
provider, transportType, err := build(server)
|
||||
provider, transportType, err := build(server, sampling)
|
||||
if errors.Is(err, errUnknownMCPTransport) {
|
||||
log.Warn("mcp server skipped: unknown transport", "name", name, "transport", transportType)
|
||||
continue
|
||||
@@ -78,7 +86,7 @@ func startMCPProvidersWith(
|
||||
return set, nil
|
||||
}
|
||||
|
||||
func buildMCPProvider(server config.MCPServer) (toolprovider.Engine, string, error) {
|
||||
func buildMCPProvider(server config.MCPServer, sampling mcp.SamplingHandler) (toolprovider.Engine, string, error) {
|
||||
name := strings.TrimSpace(server.Name)
|
||||
transportType := strings.ToLower(strings.TrimSpace(server.Transport))
|
||||
if transportType == "" {
|
||||
@@ -89,7 +97,7 @@ func buildMCPProvider(server config.MCPServer) (toolprovider.Engine, string, err
|
||||
if err != nil {
|
||||
return nil, transportType, err
|
||||
}
|
||||
return mcptoolprovider.New(name, transport, server.ParallelToolCalls), transportType, nil
|
||||
return mcptoolprovider.New(name, transport, server.ParallelToolCalls, mcptoolprovider.WithSamplingHandler(sampling)), transportType, nil
|
||||
}
|
||||
|
||||
func mcpTransportForServer(
|
||||
|
||||
@@ -167,7 +167,7 @@ func TestBuildMCPProviderSelectsTransport(t *testing.T) {
|
||||
|
||||
for _, tc := range tests {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
provider, transportType, err := buildMCPProvider(tc.server)
|
||||
provider, transportType, err := buildMCPProvider(tc.server, nil)
|
||||
if transportType != tc.wantTransport {
|
||||
t.Fatalf("transport = %q, want %q", transportType, tc.wantTransport)
|
||||
}
|
||||
@@ -344,7 +344,7 @@ func TestStartMCPProvidersPreservesOptionalProviderBehavior(t *testing.T) {
|
||||
"valid-two": {id: "valid-two", entries: []tools.ToolEntry{testTool("shared")}},
|
||||
}
|
||||
buildCalls := make(map[string]int)
|
||||
build := func(server config.MCPServer) (toolprovider.Engine, string, error) {
|
||||
build := func(server config.MCPServer, _ protocolmcp.SamplingHandler) (toolprovider.Engine, string, error) {
|
||||
buildCalls[server.Name]++
|
||||
switch server.Name {
|
||||
case "unknown":
|
||||
@@ -365,7 +365,7 @@ func TestStartMCPProvidersPreservesOptionalProviderBehavior(t *testing.T) {
|
||||
{Name: "valid-one"},
|
||||
{Name: "valid-two"},
|
||||
}
|
||||
set, err := startMCPProvidersWith(t.Context(), servers, slog.New(slog.DiscardHandler), build)
|
||||
set, err := startMCPProvidersWith(t.Context(), servers, nil, slog.New(slog.DiscardHandler), build)
|
||||
if err != nil {
|
||||
t.Fatalf("error = %v, want nil when valid providers remain available", err)
|
||||
}
|
||||
@@ -409,14 +409,14 @@ func TestStartMCPProvidersReturnsNilWithoutAvailableProviders(t *testing.T) {
|
||||
{
|
||||
name: "unknown transport is skipped without error",
|
||||
servers: []config.MCPServer{{Name: "unknown"}},
|
||||
build: func(config.MCPServer) (toolprovider.Engine, string, error) {
|
||||
build: func(config.MCPServer, protocolmcp.SamplingHandler) (toolprovider.Engine, string, error) {
|
||||
return nil, "unknown", errUnknownMCPTransport
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "first provider error is returned",
|
||||
servers: []config.MCPServer{{Name: "first"}, {Name: "second"}},
|
||||
build: func(server config.MCPServer) (toolprovider.Engine, string, error) {
|
||||
build: func(server config.MCPServer, _ protocolmcp.SamplingHandler) (toolprovider.Engine, string, error) {
|
||||
if server.Name == "first" {
|
||||
return nil, "stdio", firstErr
|
||||
}
|
||||
@@ -430,12 +430,12 @@ func TestStartMCPProvidersReturnsNilWithoutAvailableProviders(t *testing.T) {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
build := tc.build
|
||||
if build == nil {
|
||||
build = func(config.MCPServer) (toolprovider.Engine, string, error) {
|
||||
build = func(config.MCPServer, protocolmcp.SamplingHandler) (toolprovider.Engine, string, error) {
|
||||
t.Fatal("builder called without configured servers")
|
||||
return nil, "", nil
|
||||
}
|
||||
}
|
||||
set, err := startMCPProvidersWith(t.Context(), tc.servers, slog.New(slog.DiscardHandler), build)
|
||||
set, err := startMCPProvidersWith(t.Context(), tc.servers, nil, slog.New(slog.DiscardHandler), build)
|
||||
if set != nil {
|
||||
t.Fatalf("provider set = %#v, want nil", set)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,67 @@
|
||||
package agentworker
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/samcharles93/ai-sdk/core"
|
||||
"github.com/samcharles93/ai-sdk/runtime"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/agentexec"
|
||||
"github.com/samcharles93/archie-core/internal/config"
|
||||
"github.com/samcharles93/archie-core/internal/tools/mcp"
|
||||
)
|
||||
|
||||
// taskSamplingModel is the model a task-scoped MCP server's sampling request
|
||||
// is answered with: the task's builder model, the same fallback an agent stage
|
||||
// whose role is unset uses (workflow.AgentStage.resolveModel) and the same
|
||||
// role the daemon's chat path falls back to. Empty means the task has no model
|
||||
// to answer with.
|
||||
func taskSamplingModel(cfg config.TaskConfig) string {
|
||||
return strings.TrimSpace(cfg.Models["builder"])
|
||||
}
|
||||
|
||||
// taskSamplingHandler answers an MCP server's sampling/createMessage request
|
||||
// from the task's own model runtime, the same way the daemon's chat path
|
||||
// answers it from its chat model. The worker hosts the same configured MCP
|
||||
// servers as a chat turn, so without this a server's sampling request is
|
||||
// answered on one path and refused on the other. A missing runtime or model is
|
||||
// a refused request, never a dropped one: the client maps the returned error
|
||||
// to a JSON-RPC error for that request alone.
|
||||
func taskSamplingHandler(llm *runtime.Runtime, cfg config.TaskConfig) mcp.SamplingHandler {
|
||||
return func(ctx context.Context, req mcp.SamplingRequest) (mcp.SamplingResult, error) {
|
||||
if llm == nil {
|
||||
return mcp.SamplingResult{}, errors.New("mcp sampling: no model runtime is configured")
|
||||
}
|
||||
model := taskSamplingModel(cfg)
|
||||
if model == "" {
|
||||
return mcp.SamplingResult{}, errors.New("mcp sampling: no builder model is configured")
|
||||
}
|
||||
messages, err := agentexec.SamplingMessages(req)
|
||||
if err != nil {
|
||||
return mcp.SamplingResult{}, err
|
||||
}
|
||||
limits := cfg.ModelLimits[model]
|
||||
options := core.GenerateOptions{
|
||||
Messages: messages,
|
||||
System: req.SystemPrompt,
|
||||
MaxSteps: 1,
|
||||
MaxTokens: agentexec.SamplingMaxTokens(req.MaxTokens, limits.MaxOutputTokens, limits.Reasoning),
|
||||
}
|
||||
if req.Temperature != nil {
|
||||
options.Temperature = float32(*req.Temperature)
|
||||
}
|
||||
result, err := llm.Chat(ctx, model, options)
|
||||
if err != nil {
|
||||
return mcp.SamplingResult{}, fmt.Errorf("mcp sampling: %w", err)
|
||||
}
|
||||
return mcp.SamplingResult{
|
||||
Model: model,
|
||||
Role: "assistant",
|
||||
Content: mcp.SamplingContent{Type: "text", Text: result.Text},
|
||||
StopReason: string(result.FinishReason),
|
||||
}, nil
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,193 @@
|
||||
package agentworker
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"reflect"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/agentexec"
|
||||
"github.com/samcharles93/archie-core/internal/config"
|
||||
"github.com/samcharles93/archie-core/internal/tools/mcp"
|
||||
)
|
||||
|
||||
// capturedChatRequest is the JSON body the OpenAI-compatible provider sent for
|
||||
// the last call, which is where the max-token parameter is chosen.
|
||||
type capturedChatRequest struct {
|
||||
mu sync.Mutex
|
||||
body map[string]any
|
||||
}
|
||||
|
||||
func (c *capturedChatRequest) get() map[string]any {
|
||||
c.mu.Lock()
|
||||
defer c.mu.Unlock()
|
||||
return c.body
|
||||
}
|
||||
|
||||
// newCapturingChatAPI answers every completion and records the wire body, so a
|
||||
// test can assert the parameter the provider actually emitted rather than an
|
||||
// option the handler merely set.
|
||||
func newCapturingChatAPI(t *testing.T, capture *capturedChatRequest) *httptest.Server {
|
||||
t.Helper()
|
||||
api := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
var body map[string]any
|
||||
_ = json.NewDecoder(r.Body).Decode(&body)
|
||||
capture.mu.Lock()
|
||||
capture.body = body
|
||||
capture.mu.Unlock()
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_, _ = io.WriteString(w, `{"id":"chatcmpl-1","object":"chat.completion","created":1,"model":"test",`+
|
||||
`"choices":[{"index":0,"message":{"role":"assistant","content":"ok"},"finish_reason":"stop"}],`+
|
||||
`"usage":{"prompt_tokens":1,"completion_tokens":1,"total_tokens":2}}`)
|
||||
}))
|
||||
t.Cleanup(api.Close)
|
||||
return api
|
||||
}
|
||||
|
||||
// newSamplingModelServer stands in for an OpenAI-compatible provider so a
|
||||
// sampling request can be answered without a real backend.
|
||||
func newSamplingModelServer(t *testing.T) *httptest.Server {
|
||||
t.Helper()
|
||||
api := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_, _ = io.WriteString(w, `{"id":"chatcmpl-1","object":"chat.completion","created":1,"model":"gpt-5.6",`+
|
||||
`"choices":[{"index":0,"message":{"role":"assistant","content":"four"},"finish_reason":"stop"}],`+
|
||||
`"usage":{"prompt_tokens":1,"completion_tokens":1,"total_tokens":2}}`)
|
||||
}))
|
||||
t.Cleanup(api.Close)
|
||||
return api
|
||||
}
|
||||
|
||||
func testSamplingRequest() mcp.SamplingRequest {
|
||||
return mcp.SamplingRequest{
|
||||
Messages: []mcp.SamplingMessage{{
|
||||
Role: "user",
|
||||
Content: mcp.SamplingContent{Type: "text", Text: "2+2?"},
|
||||
}},
|
||||
MaxTokens: 16,
|
||||
}
|
||||
}
|
||||
|
||||
// TestTaskSamplingHandlerAnswersFromWorkerModel proves a task-scoped MCP
|
||||
// server's sampling request is answered by the worker's own model. A task
|
||||
// hosts the same configured MCP servers as a chat turn, so without this the
|
||||
// server is answered on the chat path and refused on the task path.
|
||||
func TestTaskSamplingHandlerAnswersFromWorkerModel(t *testing.T) {
|
||||
api := newSamplingModelServer(t)
|
||||
llm := agentexec.NewRuntime(map[string]agentexec.Provider{
|
||||
"openai": {Class: "openai", BaseURL: api.URL},
|
||||
})
|
||||
handler := taskSamplingHandler(llm, config.TaskConfig{Models: map[string]string{"builder": "openai/gpt-5.6"}})
|
||||
|
||||
result, err := handler(t.Context(), testSamplingRequest())
|
||||
if err != nil {
|
||||
t.Fatalf("sampling handler = %v, want a completion", err)
|
||||
}
|
||||
if result.Model != "openai/gpt-5.6" {
|
||||
t.Errorf("result.Model = %q, want the task's builder model", result.Model)
|
||||
}
|
||||
if result.Role != "assistant" || result.Content.Type != "text" || result.Content.Text != "four" {
|
||||
t.Errorf("result = %+v, want an assistant text completion", result)
|
||||
}
|
||||
}
|
||||
|
||||
// TestTaskSamplingHandlerRefusesCleanly proves a worker with no usable model
|
||||
// answers the request with an error instead of panicking or dropping it: the
|
||||
// server sees a failed request, not a dead session.
|
||||
func TestTaskSamplingHandlerRefusesCleanly(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
llm bool
|
||||
model string
|
||||
want string
|
||||
}{
|
||||
{name: "no model runtime", llm: false, model: "openai/gpt-5.6", want: "no model runtime"},
|
||||
{name: "no builder model configured", llm: true, want: "no builder model"},
|
||||
}
|
||||
|
||||
for _, tc := range tests {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
llm := agentexec.NewRuntime(nil)
|
||||
if tc.llm {
|
||||
llm = agentexec.NewRuntime(map[string]agentexec.Provider{"openai": {Class: "openai"}})
|
||||
}
|
||||
models := map[string]string{}
|
||||
if tc.model != "" {
|
||||
models["builder"] = tc.model
|
||||
}
|
||||
_, err := taskSamplingHandler(llm, config.TaskConfig{Models: models})(t.Context(), testSamplingRequest())
|
||||
if err == nil {
|
||||
t.Fatal("sampling handler returned no error with no usable model")
|
||||
}
|
||||
if !strings.Contains(err.Error(), tc.want) {
|
||||
t.Errorf("error = %q, want it to mention %q", err, tc.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestTaskSamplingBoundFollowsModelClass proves the worker consumes the
|
||||
// reasoning flag carried in TaskConfig.ModelLimits: a reasoning-class builder
|
||||
// model gets no max-token bound (its provider rejects the max_tokens parameter
|
||||
// the chat-completions provider emits), while a classic model gets the
|
||||
// request's own bound.
|
||||
func TestTaskSamplingBoundFollowsModelClass(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
reasoning bool
|
||||
wantBound bool
|
||||
wantValue int
|
||||
}{
|
||||
{name: "reasoning model", reasoning: true},
|
||||
{name: "classic model", wantBound: true, wantValue: 16},
|
||||
}
|
||||
|
||||
for _, tc := range tests {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
capture := &capturedChatRequest{}
|
||||
api := newCapturingChatAPI(t, capture)
|
||||
llm := agentexec.NewRuntime(map[string]agentexec.Provider{
|
||||
"openai": {Class: "openai", BaseURL: api.URL},
|
||||
})
|
||||
cfg := config.TaskConfig{
|
||||
Models: map[string]string{"builder": "openai/gpt-5.6"},
|
||||
ModelLimits: map[string]config.ModelLimits{
|
||||
"openai/gpt-5.6": {MaxOutputTokens: 128000, Reasoning: tc.reasoning},
|
||||
},
|
||||
}
|
||||
if _, err := taskSamplingHandler(llm, cfg)(t.Context(), testSamplingRequest()); err != nil {
|
||||
t.Fatalf("sampling handler error = %v", err)
|
||||
}
|
||||
bound, present := capture.get()["max_tokens"]
|
||||
if present != tc.wantBound {
|
||||
t.Fatalf("request body max_tokens present = %v (value %v), want %v; body = %v", present, bound, tc.wantBound, capture.get())
|
||||
}
|
||||
if present && bound != float64(tc.wantValue) {
|
||||
t.Fatalf("request body max_tokens = %v, want %d; body = %v", bound, tc.wantValue, capture.get())
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestBuildMCPProviderWiresTaskSamplingHandler proves the worker's MCP
|
||||
// provider builder hands the provider the sampling handler, so the seam a task
|
||||
// run builds providers through is the one that answers sampling -- not only
|
||||
// the handler in isolation.
|
||||
func TestBuildMCPProviderWiresTaskSamplingHandler(t *testing.T) {
|
||||
handler := taskSamplingHandler(agentexec.NewRuntime(map[string]agentexec.Provider{
|
||||
"openai": {Class: "openai"},
|
||||
}), config.TaskConfig{Models: map[string]string{"builder": "openai/gpt-5.6"}})
|
||||
|
||||
provider, _, err := buildMCPProvider(config.MCPServer{Name: "sampling", Command: "server"}, handler)
|
||||
if err != nil {
|
||||
t.Fatalf("buildMCPProvider: %v", err)
|
||||
}
|
||||
field := reflect.ValueOf(provider).Elem().FieldByName("samplingHandler")
|
||||
if field.IsNil() {
|
||||
t.Fatal("task MCP provider carries no sampling handler: a task-scoped server's sampling/createMessage request is refused")
|
||||
}
|
||||
}
|
||||
@@ -10,6 +10,8 @@ import (
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
|
||||
"github.com/samcharles93/ai-sdk/runtime"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/agentexec"
|
||||
"github.com/samcharles93/archie-core/internal/config"
|
||||
"github.com/samcharles93/archie-core/internal/domain/workflow"
|
||||
@@ -19,6 +21,7 @@ import (
|
||||
"github.com/samcharles93/archie-core/internal/installtype"
|
||||
"github.com/samcharles93/archie-core/internal/storage"
|
||||
"github.com/samcharles93/archie-core/internal/taskrun"
|
||||
"github.com/samcharles93/archie-core/internal/tools"
|
||||
"github.com/samcharles93/archie-core/internal/worktree"
|
||||
)
|
||||
|
||||
@@ -48,7 +51,15 @@ type hybridTrees struct {
|
||||
worktreeUID, worktreeGID int
|
||||
}
|
||||
|
||||
func (h *hybridTrees) Prepare(ctx context.Context, owner, repo, base string, issue int, title, body, labels string) (dir, branch string, err error) {
|
||||
// Prepare binds the task's already-prepared, bind-mounted worktree. The
|
||||
// daemon positioned it before the container started, so this resolves the
|
||||
// directory and branch rather than doing network work. A resume target (the
|
||||
// PR branch a remediation continues) is returned verbatim; a fresh target uses
|
||||
// the branch the daemon computed and persisted on the task row.
|
||||
func (h *hybridTrees) Prepare(ctx context.Context, owner, repo, base string, issue int, title, body, labels string, target workflow.PrepareTarget) (dir, branch string, err error) {
|
||||
if target != workflow.PrepareFresh {
|
||||
return h.localDir, string(target), nil
|
||||
}
|
||||
return h.localDir, h.branch, nil
|
||||
}
|
||||
|
||||
@@ -107,22 +118,6 @@ func (h *hybridTrees) Snapshot(ctx context.Context, dir, destDir string) error {
|
||||
return h.local.Snapshot(ctx, dir, destDir)
|
||||
}
|
||||
|
||||
// Resume is a no-op here for the same reason Prepare is: resuming a PR
|
||||
// branch needs the daemon's forge credential to fetch, so archied must run
|
||||
// it against the bind-mounted worktree before the container starts, the
|
||||
// same way it runs Prepare today. The container's worktree is already
|
||||
// resumed by the time a remediate run reaches this stage.
|
||||
func (h *hybridTrees) Resume(ctx context.Context, dir, branch string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Dir returns the bind-mounted worktree path archied already prepared,
|
||||
// ignoring its arguments -- the sandbox has no way to independently derive
|
||||
// or verify the path, same as Prepare above.
|
||||
func (h *hybridTrees) Dir(owner, repo string, issue int) string {
|
||||
return h.localDir
|
||||
}
|
||||
|
||||
func (h *hybridTrees) ChangedLines(ctx context.Context, dir, base string) (int, error) {
|
||||
return h.local.ChangedLines(ctx, dir, base)
|
||||
}
|
||||
@@ -192,6 +187,11 @@ type taskDependencies struct {
|
||||
// executing half of the contract whose validating half is the control
|
||||
// plane's (archie-core-fwmp).
|
||||
steps *workflow.Manager
|
||||
// guardrails is the guardrail engine agent stages record tool outcomes
|
||||
// against. It is built by this process's composition root (the workflow
|
||||
// runs in archie-agent, so the daemon's engine cannot reach it) and set on
|
||||
// every TaskContext; nil means guardrails are disabled.
|
||||
guardrails *tools.GuardrailEngine
|
||||
}
|
||||
|
||||
type runnerFactory func(map[string]agentexec.Provider, *slog.Logger) agentexec.Runner
|
||||
@@ -218,15 +218,17 @@ func applyToolLimits(agent agentexec.Runner, policy config.ToolPolicy, allow []s
|
||||
runner.AllowTools = allow
|
||||
}
|
||||
|
||||
// stageRunners builds the runner every agent stage uses.
|
||||
func stageRunners(req taskrun.Request, mcpSet *mcpProviderSet, newRunner runnerFactory, log *slog.Logger) (agentexec.Runner, error) {
|
||||
// stageRunners builds the runner every agent stage uses. llm is the task's
|
||||
// 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 *mcpProviderSet, newRunner runnerFactory, llm *runtime.Runtime, log *slog.Logger) (agentexec.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(nil), Spec: *req.Harness}, nil
|
||||
}
|
||||
var agent agentexec.Runner
|
||||
if mcpSet != nil && mcpSet.registry != nil {
|
||||
agent = agentexec.NewLoopRunner(agentexec.NewRuntime(req.Providers), log, mcpSet.registry)
|
||||
agent = agentexec.NewLoopRunner(llm, log, mcpSet.registry)
|
||||
} else {
|
||||
agent = newRunner(req.Providers, log)
|
||||
}
|
||||
@@ -277,8 +279,14 @@ func runTask(ctx context.Context, req taskrun.Request, dependencies taskDependen
|
||||
// start from.
|
||||
defer restoreWorktreeOwnership(trees, workDir, log)()
|
||||
|
||||
// Start MCP providers and build a local tool registry.
|
||||
mcpSet, mcpErr := startMCPProviders(ctx, req.MCPServers, log)
|
||||
// The MCP providers a task hosts share the same model runtime its agent
|
||||
// stages run on, so a server-initiated sampling request is answered from
|
||||
// the task's own model rather than a second provider client.
|
||||
var llm *runtime.Runtime
|
||||
if len(req.MCPServers) > 0 {
|
||||
llm = agentexec.NewRuntime(req.Providers)
|
||||
}
|
||||
mcpSet, mcpErr := startMCPProviders(ctx, req.MCPServers, taskSamplingHandler(llm, req.Cfg), log)
|
||||
if mcpSet != nil {
|
||||
defer mcpSet.cleanup(ctx, log)
|
||||
}
|
||||
@@ -286,7 +294,7 @@ func runTask(ctx context.Context, req taskrun.Request, dependencies taskDependen
|
||||
log.Warn("mcp providers had errors, continuing with available tools", "err", mcpErr)
|
||||
}
|
||||
|
||||
agent, err := stageRunners(req, mcpSet, newRunner, log)
|
||||
agent, err := stageRunners(req, mcpSet, newRunner, llm, log)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -310,16 +318,17 @@ func runTask(ctx context.Context, req taskrun.Request, dependencies taskDependen
|
||||
}
|
||||
|
||||
tc := &workflow.TaskContext{
|
||||
Task: req.Task,
|
||||
Repo: req.Repo,
|
||||
Cfg: req.Cfg.ToConfig(),
|
||||
Forge: dependencies.forge,
|
||||
Store: dependencies.store,
|
||||
Calls: dependencies.calls,
|
||||
Trees: trees,
|
||||
Agent: agent,
|
||||
Bus: bus,
|
||||
Log: log,
|
||||
Task: req.Task,
|
||||
Repo: req.Repo,
|
||||
Cfg: req.Cfg.ToConfig(),
|
||||
Forge: dependencies.forge,
|
||||
Store: dependencies.store,
|
||||
Calls: dependencies.calls,
|
||||
Trees: trees,
|
||||
Agent: agent,
|
||||
Bus: bus,
|
||||
Log: log,
|
||||
Guardrails: dependencies.guardrails,
|
||||
}
|
||||
// pr-review reads an external pull request's data from workDir, which the
|
||||
// daemon already populated before this task started (daemon's
|
||||
|
||||
@@ -170,6 +170,31 @@ func TestHybridTreesPushSkipsReconciliationWhenNotConfigured(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// TestHybridTreesPrepareResolvesTheTargetBranch pins the container half of the
|
||||
// workflow prepare seam: the daemon has already positioned the bind-mounted
|
||||
// worktree, so the adapter resolves its directory and returns the requested
|
||||
// resume target verbatim -- the branch a remediation continues -- while a fresh
|
||||
// prepare returns the branch the daemon persisted on the task row.
|
||||
func TestHybridTreesPrepareResolvesTheTargetBranch(t *testing.T) {
|
||||
trees := &hybridTrees{localDir: "/work/issue-1", branch: "archie/fresh"}
|
||||
|
||||
dir, branch, err := trees.Prepare(t.Context(), "o", "r", "main", 1, "title", "", "", workflow.PrepareTarget("archie/pr-branch"))
|
||||
if err != nil {
|
||||
t.Fatalf("Prepare(resume): %v", err)
|
||||
}
|
||||
if dir != "/work/issue-1" || branch != "archie/pr-branch" {
|
||||
t.Fatalf("Prepare(resume) = %q/%q, want /work/issue-1/archie/pr-branch", dir, branch)
|
||||
}
|
||||
|
||||
dir, branch, err = trees.Prepare(t.Context(), "o", "r", "main", 1, "title", "", "", workflow.PrepareFresh)
|
||||
if err != nil {
|
||||
t.Fatalf("Prepare(fresh): %v", err)
|
||||
}
|
||||
if dir != "/work/issue-1" || branch != "archie/fresh" {
|
||||
t.Fatalf("Prepare(fresh) = %q/%q, want /work/issue-1/archie/fresh", dir, branch)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHybridTreesCommitReconcilesOwnershipEvenWhenCommitFails(t *testing.T) {
|
||||
missing := filepath.Join(t.TempDir(), "does-not-exist")
|
||||
trees := &hybridTrees{
|
||||
@@ -301,8 +326,8 @@ type remoteManager struct {
|
||||
branch string
|
||||
}
|
||||
|
||||
func (r *remoteManager) Prepare(ctx context.Context, owner, repo, base string, issue int, title, body, labels string) (string, string, error) {
|
||||
dir, branch, err := r.manager.Prepare(ctx, owner, repo, base, issue, title, body, labels)
|
||||
func (r *remoteManager) Prepare(ctx context.Context, owner, repo, base string, issue int, title, body, labels string, target workflow.PrepareTarget) (string, string, error) {
|
||||
dir, branch, err := r.manager.Prepare(ctx, owner, repo, base, issue, title, body, labels, worktree.Target(target))
|
||||
if err == nil {
|
||||
r.dir = dir
|
||||
r.branch = branch
|
||||
@@ -338,6 +363,7 @@ func newLocalRemote(t *testing.T, owner, repo string) string {
|
||||
run(seed, "init", "-b", "main")
|
||||
run(seed, "config", "user.name", "seeder")
|
||||
run(seed, "config", "user.email", "seed@example.com")
|
||||
run(seed, "config", "commit.gpgsign", "false")
|
||||
if err := os.WriteFile(filepath.Join(seed, "README.md"), []byte("seed\n"), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -347,6 +373,26 @@ func newLocalRemote(t *testing.T, owner, repo string) string {
|
||||
return host
|
||||
}
|
||||
|
||||
// hostileSigningConfig writes a global git config that makes every commit
|
||||
// fail unless the fixture disables signing in the repository's own config.
|
||||
func hostileSigningConfig(t *testing.T) string {
|
||||
t.Helper()
|
||||
path := filepath.Join(t.TempDir(), "hostile.gitconfig")
|
||||
const cfg = "[user]\n\tname = Hostile\n\temail = hostile@example.com\n\tsigningkey = /nonexistent/signing-key\n[gpg]\n\tformat = ssh\n[commit]\n\tgpgsign = true\n"
|
||||
if err := os.WriteFile(path, []byte(cfg), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return path
|
||||
}
|
||||
|
||||
// TestNewLocalRemoteIgnoresHostSigningConfig pins the fixture's hermeticity:
|
||||
// it must not depend on the developer's global git identity or a signing agent.
|
||||
func TestNewLocalRemoteIgnoresHostSigningConfig(t *testing.T) {
|
||||
t.Setenv("GIT_CONFIG_GLOBAL", hostileSigningConfig(t))
|
||||
t.Setenv("GIT_CONFIG_NOSYSTEM", "1")
|
||||
newLocalRemote(t, "acme", "todo")
|
||||
}
|
||||
|
||||
func startEmbeddedTaskRPCServer(t *testing.T) *server.Server {
|
||||
t.Helper()
|
||||
srv := natssrv.RunRandClientPortServer()
|
||||
@@ -407,7 +453,7 @@ func TestRunTaskExecutesBootstrapWorkflowEndToEnd(t *testing.T) {
|
||||
fg := &prCapturingForge{}
|
||||
manager := &worktree.Manager{WorkDir: t.TempDir(), Token: "unused", BotUser: "archie-bot", BotEmail: "archie-bot@example.com", BaseURL: "file://" + host}
|
||||
remote := &remoteManager{manager: manager}
|
||||
hostDir, _, err := remote.Prepare(ctx, "acme", "widget", "main", 1, task.Title, "", "bootstrap")
|
||||
hostDir, _, err := remote.Prepare(ctx, "acme", "widget", "main", 1, task.Title, "", "bootstrap", workflow.PrepareFresh)
|
||||
if err != nil {
|
||||
t.Fatalf("prepare: %v", err)
|
||||
}
|
||||
@@ -613,7 +659,7 @@ func TestExecuteTaskRequestUsesInfrastructureRPCDependencies(t *testing.T) {
|
||||
BotEmail: "archie-bot@example.com",
|
||||
BaseURL: "file://" + host,
|
||||
}
|
||||
hostDir, branch, err := daemonTrees.Prepare(ctx, "acme", "rpc-widget", "main", 2, task.Title, "", "bootstrap")
|
||||
hostDir, branch, err := daemonTrees.Prepare(ctx, "acme", "rpc-widget", "main", 2, task.Title, "", "bootstrap", worktree.Fresh)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -650,7 +696,7 @@ func TestExecuteTaskRequestUsesInfrastructureRPCDependencies(t *testing.T) {
|
||||
Cfg: config.Config{}.ForTask(),
|
||||
WorktreeGrant: grant,
|
||||
}
|
||||
response, err := executeTaskRequest(ctx, request, transport, hostDir, slog.New(slog.DiscardHandler), testSteps(t))
|
||||
response, err := executeTaskRequest(ctx, request, transport, hostDir, slog.New(slog.DiscardHandler), testSteps(t), nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -705,7 +751,7 @@ func TestExecuteTaskRequestForwardsWorkflowEventsOverNATS(t *testing.T) {
|
||||
BotEmail: "archie-bot@example.com",
|
||||
BaseURL: "file://" + host,
|
||||
}
|
||||
hostDir, branch, err := daemonTrees.Prepare(ctx, "acme", "events-widget", "main", 3, task.Title, "", "bootstrap")
|
||||
hostDir, branch, err := daemonTrees.Prepare(ctx, "acme", "events-widget", "main", 3, task.Title, "", "bootstrap", worktree.Fresh)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -768,7 +814,7 @@ func TestExecuteTaskRequestForwardsWorkflowEventsOverNATS(t *testing.T) {
|
||||
Cfg: config.Config{}.ForTask(),
|
||||
WorktreeGrant: grant,
|
||||
}
|
||||
response, err := executeTaskRequest(ctx, request, transport, hostDir, slog.New(slog.DiscardHandler), testSteps(t))
|
||||
response, err := executeTaskRequest(ctx, request, transport, hostDir, slog.New(slog.DiscardHandler), testSteps(t), nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -898,7 +944,7 @@ func TestHybridTreesForwardsChangedFileStatsToItsLocalManager(t *testing.T) {
|
||||
WorkDir: t.TempDir(), Token: "unused",
|
||||
BotUser: "archie-bot", BotEmail: "archie-bot@example.com", BaseURL: "file://" + host,
|
||||
}
|
||||
dir, branch, err := manager.Prepare(ctx, "acme", "diffstat", "main", 4, "feat: widget", "", "")
|
||||
dir, branch, err := manager.Prepare(ctx, "acme", "diffstat", "main", 4, "feat: widget", "", "", worktree.Fresh)
|
||||
if err != nil {
|
||||
t.Fatalf("prepare: %v", err)
|
||||
}
|
||||
|
||||
@@ -34,7 +34,7 @@ type fakeScratchTrees struct {
|
||||
dir string
|
||||
}
|
||||
|
||||
func (f *fakeScratchTrees) Prepare(context.Context, string, string, string, int, string, string, string) (string, string, error) {
|
||||
func (f *fakeScratchTrees) Prepare(context.Context, string, string, string, int, string, string, string, workflow.PrepareTarget) (string, string, error) {
|
||||
return f.dir, "", nil
|
||||
}
|
||||
|
||||
|
||||
@@ -15,6 +15,7 @@ import (
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/workflowsteps"
|
||||
"github.com/samcharles93/archie-core/internal/storage"
|
||||
"github.com/samcharles93/archie-core/internal/taskrun"
|
||||
"github.com/samcharles93/archie-core/internal/tools"
|
||||
)
|
||||
|
||||
const rpcTimeout = 60 * time.Second
|
||||
@@ -64,6 +65,11 @@ type workerDependencies struct {
|
||||
// definitions against, registered by the composition root before the first
|
||||
// task is served (infrastructure/workflowsteps is the provider set).
|
||||
steps *workflow.Manager
|
||||
// guardrails is the guardrail engine every served task records agent-stage
|
||||
// outcomes against, built once at this process's composition root.
|
||||
// archie-agent is the process that runs the stages, so the engine that
|
||||
// observes them lives here; a live engine cannot cross the task handoff.
|
||||
guardrails *tools.GuardrailEngine
|
||||
}
|
||||
|
||||
// productionWorkerDependencies builds the worker's process dependencies. The
|
||||
@@ -87,7 +93,8 @@ func productionWorkerDependencies() (workerDependencies, error) {
|
||||
<-ctx.Done()
|
||||
log.Info("archie-agent shutting down")
|
||||
},
|
||||
steps: steps,
|
||||
steps: steps,
|
||||
guardrails: tools.NewGuardrailEngine(tools.DefaultGuardrailConfig()),
|
||||
}, nil
|
||||
}
|
||||
|
||||
@@ -134,7 +141,7 @@ func run(ctx context.Context, settings Settings, log *slog.Logger, dependencies
|
||||
log.Info("system log publisher attached", "task", taskID)
|
||||
|
||||
subscription, err := transport.SubscribeTasks(ctx, taskID, func(ctx context.Context, request taskrun.Request) (*taskrun.Response, error) {
|
||||
return executeTaskRequest(ctx, request, transport, workDir, log, dependencies.steps)
|
||||
return executeTaskRequest(ctx, request, transport, workDir, log, dependencies.steps, dependencies.guardrails)
|
||||
}, log)
|
||||
if err != nil {
|
||||
log.Error("taskrun subscribe failed", "err", err)
|
||||
@@ -159,15 +166,16 @@ type taskServiceTransport interface {
|
||||
EventPublisher() agentexec.EventPublisher
|
||||
}
|
||||
|
||||
func executeTaskRequest(ctx context.Context, request taskrun.Request, transport taskServiceTransport, workDir string, log *slog.Logger, steps *workflow.Manager) (*taskrun.Response, error) {
|
||||
func executeTaskRequest(ctx context.Context, request taskrun.Request, transport taskServiceTransport, workDir string, log *slog.Logger, steps *workflow.Manager, guardrails *tools.GuardrailEngine) (*taskrun.Response, error) {
|
||||
log.Info("running task", "task", request.Task.ID, "repo", request.Repo.FullName(), "issue", request.Task.IssueNumber)
|
||||
dependencies := taskDependencies{
|
||||
forge: transport.Forger(request.Task.Identity, rpcTimeout),
|
||||
store: transport.Store(rpcTimeout),
|
||||
calls: transport.Calls(rpcTimeout),
|
||||
trees: transport.Trees(request.Task.Identity, request.WorktreeGrant, rpcTimeout),
|
||||
events: transport.EventPublisher(),
|
||||
steps: steps,
|
||||
forge: transport.Forger(request.Task.Identity, rpcTimeout),
|
||||
store: transport.Store(rpcTimeout),
|
||||
calls: transport.Calls(rpcTimeout),
|
||||
trees: transport.Trees(request.Task.Identity, request.WorktreeGrant, rpcTimeout),
|
||||
events: transport.EventPublisher(),
|
||||
steps: steps,
|
||||
guardrails: guardrails,
|
||||
}
|
||||
response, err := runTask(ctx, request, dependencies, newTaskRunner, workDir, log)
|
||||
if err != nil {
|
||||
|
||||
@@ -17,6 +17,7 @@ import (
|
||||
"path/filepath"
|
||||
"slices"
|
||||
"strings"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"syscall"
|
||||
"time"
|
||||
@@ -31,6 +32,7 @@ import (
|
||||
controlpb "github.com/samcharles93/archie-core/internal/contracts/controlplane/v1"
|
||||
"github.com/samcharles93/archie-core/internal/daemon"
|
||||
"github.com/samcharles93/archie-core/internal/domain/access"
|
||||
"github.com/samcharles93/archie-core/internal/domain/agent"
|
||||
"github.com/samcharles93/archie-core/internal/domain/applystatus"
|
||||
"github.com/samcharles93/archie-core/internal/domain/curator"
|
||||
"github.com/samcharles93/archie-core/internal/domain/eda/module"
|
||||
@@ -39,6 +41,7 @@ import (
|
||||
"github.com/samcharles93/archie-core/internal/domain/health"
|
||||
"github.com/samcharles93/archie-core/internal/domain/identity"
|
||||
domainmemory "github.com/samcharles93/archie-core/internal/domain/memory"
|
||||
"github.com/samcharles93/archie-core/internal/domain/messaging"
|
||||
"github.com/samcharles93/archie-core/internal/domain/scheduling"
|
||||
"github.com/samcharles93/archie-core/internal/domain/storecontract"
|
||||
"github.com/samcharles93/archie-core/internal/domain/workflow"
|
||||
@@ -59,6 +62,7 @@ import (
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/staterpc"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/taskactions"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/toolbuilder"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/transcription"
|
||||
"github.com/samcharles93/archie-core/internal/logging"
|
||||
"github.com/samcharles93/archie-core/internal/plugin"
|
||||
"github.com/samcharles93/archie-core/internal/plugin/pluginextract"
|
||||
@@ -165,8 +169,22 @@ type boot struct {
|
||||
// closes; the Gateway also holds its serve claim on it.
|
||||
chatPool *pgxpool.Pool
|
||||
|
||||
// catalog, catalogModels and catalogMu are the model catalog the process
|
||||
// last loaded and the model references it contributes. The refresh loop
|
||||
// replaces both together while the config paths read them, so they are one
|
||||
// value behind one lock rather than two fields that can disagree.
|
||||
catalogMu sync.RWMutex
|
||||
catalog modelcatalog.Snapshot
|
||||
catalogModels []string
|
||||
// catalogCachePath is where the catalog's last download is cached, beside
|
||||
// the config file. Empty when boot had no file path to derive it from.
|
||||
catalogCachePath string
|
||||
// catalogURL overrides the catalog endpoint. Zero in production:
|
||||
// modelcatalog defaults to models.dev. Set only by tests.
|
||||
catalogURL string
|
||||
// catalogRefreshInterval is how often the running daemon re-reads the
|
||||
// catalog. The daemon's refresh loop reads it; zero means the default.
|
||||
catalogRefreshInterval time.Duration
|
||||
|
||||
bus *events.Bus
|
||||
// taskActionsConn is the standalone Gateway process's own NATS connection:
|
||||
@@ -238,6 +256,15 @@ type boot struct {
|
||||
// unrelated call.
|
||||
embeddings domainembedding.Client
|
||||
|
||||
// transcriber is the optional voice-transcription capability. It is built
|
||||
// beside the chat runtime from this process's own [models]/[providers],
|
||||
// which is the model-owning side of the Messaging boundary: a channel
|
||||
// frontend carries a voice note's bytes across the inbound wire, and this
|
||||
// process turns them into text before the turn is recorded. Nil when no
|
||||
// role is configured or the provider credential did not resolve; a turn
|
||||
// then keeps the frontend's media note rather than failing.
|
||||
transcriber messaging.Transcriber
|
||||
|
||||
toolReg *tools.Registry
|
||||
chatModels *chatModelManager
|
||||
personas *gateway.PersonaRegistry
|
||||
@@ -246,17 +273,34 @@ type boot struct {
|
||||
defaultChatIdentity string
|
||||
updateService *releaseupdate.Service
|
||||
|
||||
startGateways []func()
|
||||
capabilityHost *plugin.Host
|
||||
startGateways []func()
|
||||
capabilityHost *plugin.Host
|
||||
// pluginReconciler loads plugin, module and secret-engine files dropped into
|
||||
// the running config's directories without a restart. Nil before boot's
|
||||
// startPluginReconcile, and in processes that load no directory (the
|
||||
// Gateway), where a stored plugin-settings change still layers but loads
|
||||
// nothing.
|
||||
pluginReconciler *pluginReconciler
|
||||
trees *worktree.Manager
|
||||
worktreeGrants *worktreerpc.Grants
|
||||
identityRunners []*daemon.IdentityRunner
|
||||
memEngines *domainmemory.Registry
|
||||
curatorRegistry *curator.Registry
|
||||
curatorRuntime *curator.Runtime
|
||||
guardrails *tools.GuardrailEngine
|
||||
providerRegistry *toolprovider.Registry
|
||||
d *daemon.Daemon
|
||||
// mcpMu guards mcpApplied, which records the MCP servers the provider
|
||||
// registry was last built from, keyed by configured name. A live
|
||||
// tool-settings change diffs the stored server set against it so an
|
||||
// unchanged server is left running and untouched (mcp_reconcile.go); the
|
||||
// mutex is held because registerTools runs at boot while the
|
||||
// control-plane watch may already be applying an update.
|
||||
mcpMu sync.Mutex
|
||||
mcpApplied map[string]appliedMCPServer
|
||||
// mcpProvider builds the engine for one configured MCP server. Nil uses
|
||||
// configuredMCPProvider; a test injects a fake so the live reconciliation
|
||||
// can run without spawning a server process.
|
||||
mcpProvider func(config.MCPServer) (toolprovider.Engine, error)
|
||||
d *daemon.Daemon
|
||||
// schedulingEngine is the cron/scheduling ticker engine (setupScheduling).
|
||||
// Nil when no chat task creator is configured, in which case startServices
|
||||
// leaves it unstarted rather than running with no reachable job kind.
|
||||
@@ -530,19 +574,146 @@ func (b *boot) handleRequeue(ctx context.Context, requeue int64, once bool) (boo
|
||||
return !once, nil
|
||||
}
|
||||
|
||||
// catalogState returns the model catalog the process is running and the model
|
||||
// references it contributes. Both together, under one lock: the refresh loop
|
||||
// replaces them as a pair.
|
||||
func (b *boot) catalogState() (modelcatalog.Snapshot, []string) {
|
||||
b.catalogMu.RLock()
|
||||
defer b.catalogMu.RUnlock()
|
||||
return b.catalog, b.catalogModels
|
||||
}
|
||||
|
||||
// setCatalogState installs a loaded catalog. Called only after everything it
|
||||
// has to reach has been built, so a read that fails leaves the previous
|
||||
// snapshot running.
|
||||
func (b *boot) setCatalogState(snapshot modelcatalog.Snapshot, models []string) {
|
||||
b.catalogMu.Lock()
|
||||
defer b.catalogMu.Unlock()
|
||||
b.catalog = snapshot
|
||||
b.catalogModels = models
|
||||
}
|
||||
|
||||
// loadCatalog reads the model catalog once at boot and merges it into the
|
||||
// running config. A catalog that cannot be read is not fatal -- the configured
|
||||
// providers and models keep running, and the refresh loop retries the same
|
||||
// read -- but a catalog that *is* read must reach the config the process runs
|
||||
// on, not just the boot struct the chat runtime was built from.
|
||||
func (b *boot) loadCatalog(ctx context.Context, cfgPath string) {
|
||||
catalog, err := modelcatalog.Load(ctx, modelcatalog.Options{
|
||||
CachePath: filepath.Join(filepath.Dir(cfgPath), "models.json"),
|
||||
Getenv: b.secrets.Getenv,
|
||||
Configured: b.cfg.Providers,
|
||||
})
|
||||
b.catalog = catalog
|
||||
b.catalogCachePath = filepath.Join(filepath.Dir(cfgPath), "models.json")
|
||||
catalog, err := modelcatalog.Load(ctx, b.catalogOptions(b.catalogCachePath))
|
||||
if err != nil {
|
||||
b.log.Warn("model catalog unavailable; using configured providers and models", "err", err)
|
||||
return
|
||||
}
|
||||
b.catalogModels = applyModelCatalog(&b.cfg, catalog)
|
||||
b.log.Info("model catalog loaded", "providers", len(catalog.Providers), "models", len(b.catalogModels))
|
||||
models := applyModelCatalog(&b.cfg, catalog)
|
||||
// applyModelCatalog writes into the boot struct, while every consumer
|
||||
// reads config.Holder. Publishing the merged copy here is what puts the
|
||||
// catalog's model limits and discovered providers into the running config;
|
||||
// without it the first live update would be the first snapshot to carry
|
||||
// them.
|
||||
b.cfgHolder.Set(b.cfg.Clone())
|
||||
b.setCatalogState(catalog, models)
|
||||
b.log.Info("model catalog loaded", "providers", len(catalog.Providers), "models", len(models))
|
||||
}
|
||||
|
||||
// seedSoul writes the starter SOUL beside the config on first run, so the
|
||||
// identity a user can edit exists before the SOUL loader (#439) reads it. It
|
||||
// runs on the full process's boot tail rather than in loadConfig: an offline
|
||||
// command such as `-requeue` reads the config but must not write the operator's
|
||||
// files. A failure is logged, not fatal -- the loader falls back to the embedded
|
||||
// default, so a read-only config directory must not stop the process
|
||||
// (docs/prds/soul.md, "Prompt and failure contract").
|
||||
func (b *boot) seedSoul(cfgPath string) {
|
||||
result, err := configuration.SeedSoul(cfgPath, agent.ShippedSoul())
|
||||
if err != nil {
|
||||
b.log.Warn("soul: starter file unavailable", "err", err)
|
||||
return
|
||||
}
|
||||
if result.Action == configuration.SoulCreated {
|
||||
b.log.Info("soul: starter file written", "path", result.Path, "action", result.Action)
|
||||
}
|
||||
}
|
||||
|
||||
// refreshModelCatalog re-reads the catalog and republishes everything derived
|
||||
// from it, so a credential that becomes resolvable after boot or a model added
|
||||
// upstream reaches the running config, the dashboard and the chat runtime
|
||||
// without a restart. It is the live half of loadCatalog.
|
||||
//
|
||||
// A failed read keeps the snapshot already running: the model list must not go
|
||||
// blank because the catalog service had a bad minute. The caller decides
|
||||
// whether the failure is fatal to it -- the refresh loop logs and retries.
|
||||
func (b *boot) refreshModelCatalog(ctx context.Context) error {
|
||||
catalog, err := modelcatalog.Load(ctx, b.catalogOptions(b.catalogCachePath))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
base := b.cfgHolder.Get().Clone()
|
||||
models := applyModelCatalog(&base, catalog)
|
||||
cfg, _, err := b.runtimeConfig(ctx, base)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// Committed only once the layered config passed validation, so a refused
|
||||
// layering leaves the catalog the process is running alone.
|
||||
b.setCatalogState(catalog, models)
|
||||
b.publishConfig(ctx, cfg)
|
||||
b.rebuildChatModelRuntime(cfg)
|
||||
b.log.Info("model catalog refreshed", "providers", len(catalog.Providers), "models", len(models))
|
||||
return nil
|
||||
}
|
||||
|
||||
// catalogOptions is the one place the catalog read options are built: boot and
|
||||
// every live refresh read the same endpoint, the same cache file and the same
|
||||
// credential lookup. Getenv is the secrets registry, not the raw process
|
||||
// environment, so a key a secret engine starts resolving after boot is visible
|
||||
// to the next refresh. A variable exported into the shell after the process
|
||||
// started is not, and cannot be: the process environment is fixed at exec.
|
||||
func (b *boot) catalogOptions(cachePath string) modelcatalog.Options {
|
||||
opts := modelcatalog.Options{
|
||||
URL: b.catalogURL,
|
||||
CachePath: cachePath,
|
||||
}
|
||||
if b.secrets != nil {
|
||||
opts.Getenv = b.secrets.Getenv
|
||||
}
|
||||
if b.cfgHolder != nil {
|
||||
opts.Configured = b.cfgHolder.Get().Providers
|
||||
}
|
||||
return opts
|
||||
}
|
||||
|
||||
// modelCatalogRefreshInterval is how often the running daemon re-reads the
|
||||
// model catalog. The document itself changes on the order of weeks, but a
|
||||
// credential a secret engine starts resolving can appear at any moment, so the
|
||||
// interval trades one cheap HTTP read against how long an operator waits for a
|
||||
// newly usable provider to show up. It is deliberately the daemon's own
|
||||
// cadence and not an operator setting: the catalog is derived state, not a
|
||||
// stored resource, and a knob with no second consumer would be a setting that
|
||||
// parses and does nothing.
|
||||
const modelCatalogRefreshInterval = time.Hour
|
||||
|
||||
// startModelCatalogRefresh re-reads the catalog on an interval for the life of
|
||||
// the process. It belongs to the daemon, which owns the published
|
||||
// configuration; the standalone Gateway reads the catalog once at boot.
|
||||
func (b *boot) startModelCatalogRefresh(ctx context.Context) {
|
||||
every := b.catalogRefreshInterval
|
||||
if every <= 0 {
|
||||
every = modelCatalogRefreshInterval
|
||||
}
|
||||
go func() {
|
||||
ticker := time.NewTicker(every)
|
||||
defer ticker.Stop()
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return
|
||||
case <-ticker.C:
|
||||
if err := b.refreshModelCatalog(ctx); err != nil {
|
||||
b.log.Warn("model catalog refresh failed; keeping the loaded catalog", "err", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
// setupObservability builds the event bus and dashboard server. Every event is persisted (stamped with its row id) and
|
||||
@@ -698,7 +869,7 @@ func (b *boot) startEmbeddedNATS(ctx context.Context) (string, string, error) {
|
||||
// not require Docker; repository tasks park instead of falling back to a host
|
||||
// model loop.
|
||||
func (b *boot) setupContainers(ctx context.Context) func() {
|
||||
containerPool, storeBackend, closeDocker := startContainers(ctx, b.cfg, b.log)
|
||||
containerPool, storeBackend, closeDocker := startContainers(ctx, b.cfgHolder, b.secrets, b.log)
|
||||
b.containerPool = containerPool
|
||||
b.storeBackend = storeBackend
|
||||
return closeDocker
|
||||
@@ -721,6 +892,26 @@ func (b *boot) setupEmbeddings(cfg config.Config, log *slog.Logger) {
|
||||
}
|
||||
}
|
||||
|
||||
// setupTranscriber builds the optional voice-transcription capability. It is
|
||||
// the model-owning side of the Messaging Service boundary: the channel
|
||||
// frontend carries a voice note's audio bytes across the inbound wire but
|
||||
// holds no provider credential, so this process turns them into text before
|
||||
// the turn's message is recorded. Like setupEmbeddings, a client is wired only
|
||||
// when models["transcription"] names a provider/model and that provider's
|
||||
// credential resolves; a configured-but-unusable role is logged so the
|
||||
// degradation is visible, while a role never configured is silent.
|
||||
func (b *boot) setupTranscriber(cfg config.Config, log *slog.Logger) {
|
||||
client, ok := transcription.New(cfg.Models, cfg.Providers, transcription.Options{
|
||||
ResolveSecret: b.secrets.Resolve,
|
||||
})
|
||||
if ok {
|
||||
b.transcriber = client
|
||||
log.Info("voice transcription enabled", "role", transcription.Role)
|
||||
} else if cfg.Models[transcription.Role] != "" {
|
||||
log.Warn("voice transcription configured but unavailable; capability disabled", "role", transcription.Role)
|
||||
}
|
||||
}
|
||||
|
||||
// setupLLMAndChat wires the runtime, tool registry, model management,
|
||||
// personas and the dashboard's chat service.
|
||||
func (b *boot) setupLLMAndChat(ctx context.Context) error {
|
||||
@@ -1208,26 +1399,20 @@ func (b *boot) setupCurators(ctx context.Context) {
|
||||
b.addCleanup(shutdownCuratorRegistry(reg, log))
|
||||
}
|
||||
|
||||
func (b *boot) setupGuardrails() {
|
||||
log := b.log
|
||||
gc := tools.DefaultGuardrailConfig()
|
||||
b.guardrails = tools.NewGuardrailEngine(gc)
|
||||
log.Info(
|
||||
"guardrail engine enabled",
|
||||
"exact_failure_warn", gc.ExactFailureWarnAfter,
|
||||
"same_tool_failure_warn", gc.SameToolFailureWarnAfter,
|
||||
"no_progress_warn", gc.NoProgressWarnAfter,
|
||||
)
|
||||
}
|
||||
|
||||
// registerTools registers the tool providers with a lifecycle: workspace
|
||||
// file/shell tools and optional MCP servers. Memory tools
|
||||
// (memory_create/update/delete/list) are not registered here -- they are
|
||||
// built per turn onto the resolved Subject's scopes
|
||||
// (internal/gateway/turn_memory_tool.go), not once at boot.
|
||||
func (b *boot) registerTools(ctx context.Context) error {
|
||||
cfg, log := b.cfg, b.log
|
||||
// The running config, not b.cfg: boot is where the first stored version of
|
||||
// every kind was already layered in, and a watch that applied a later one
|
||||
// before this point published through the holder.
|
||||
cfg, log := b.cfgHolder.Get(), b.log
|
||||
b.providerRegistry = toolprovider.NewRegistry(b.toolReg)
|
||||
b.mcpMu.Lock()
|
||||
b.mcpApplied = make(map[string]appliedMCPServer, len(cfg.Tools.MCPServers))
|
||||
b.mcpMu.Unlock()
|
||||
// Workspace file and shell tools. Registered only when a workspace is
|
||||
// configured: these read, write and execute, so the directory is a
|
||||
// deliberate choice rather than a default.
|
||||
@@ -1246,7 +1431,7 @@ func (b *boot) registerTools(ctx context.Context) error {
|
||||
log.Info("workspace tools disabled (chat.workspace is unset)")
|
||||
}
|
||||
for _, srv := range cfg.Tools.MCPServers {
|
||||
provider, err := configuredMCPProvider(srv, cfg.WorkDir)
|
||||
provider, err := b.buildMCPProvider(srv)
|
||||
if err != nil {
|
||||
log.Warn("mcp tool provider skipped", "name", srv.Name, "err", err)
|
||||
continue
|
||||
@@ -1261,6 +1446,9 @@ func (b *boot) registerTools(ctx context.Context) error {
|
||||
log.Warn("mcp tool provider skipped", "name", srv.Name, "err", err)
|
||||
continue
|
||||
}
|
||||
b.mcpMu.Lock()
|
||||
b.mcpApplied[strings.TrimSpace(srv.Name)] = appliedMCPServer{server: srv, id: provider.Manifest().ID}
|
||||
b.mcpMu.Unlock()
|
||||
}
|
||||
if err := b.capabilityHost.Register(b.providerRegistry); err != nil {
|
||||
log.Error("tool-provider capability registration failed", "err", err)
|
||||
@@ -1310,21 +1498,7 @@ func (b *boot) registerStandaloneTools() {
|
||||
// web_fetch. Registered directly rather than as a tool provider: it has
|
||||
// no process to start or stop, so the provider lifecycle would buy
|
||||
// nothing. Disabled by configuration returns nil and advertises nothing.
|
||||
if entry := webfetch.Tool(webfetch.Config{
|
||||
Enabled: cfg.Tools.WebFetch.IsEnabled(),
|
||||
Timeout: cfg.Tools.WebFetch.Timeout.Std(),
|
||||
MaxBytes: cfg.Tools.WebFetch.MaxBytes,
|
||||
AllowPrivateNetworks: cfg.Tools.WebFetch.AllowPrivateNetworks,
|
||||
}); entry != nil {
|
||||
if err := b.toolReg.Register(*entry); err != nil {
|
||||
log.Warn("web_fetch registration failed", "err", err)
|
||||
} else {
|
||||
log.Info("web fetch enabled",
|
||||
"allow_private_networks", cfg.Tools.WebFetch.AllowPrivateNetworks)
|
||||
}
|
||||
} else {
|
||||
log.Info("web fetch disabled")
|
||||
}
|
||||
b.registerWebFetchTool(cfg)
|
||||
|
||||
// send_file. Rooted at the same workspace as the file tools and gated
|
||||
// by the same confinement, because it hands a host file to an outbound
|
||||
@@ -1343,6 +1517,30 @@ func (b *boot) registerStandaloneTools() {
|
||||
b.registerMinimaxTool(cfg, log)
|
||||
}
|
||||
|
||||
// registerWebFetchTool registers web_fetch from cfg. Split out of
|
||||
// registerStandaloneTools because a live tool-settings change rebuilds the
|
||||
// entry: the one a boot built captured its config at construction, so it
|
||||
// cannot read a changed value per call.
|
||||
func (b *boot) registerWebFetchTool(cfg config.Config) {
|
||||
log := b.log
|
||||
entry := webfetch.Tool(webfetch.Config{
|
||||
Enabled: cfg.Tools.WebFetch.IsEnabled(),
|
||||
Timeout: cfg.Tools.WebFetch.Timeout.Std(),
|
||||
MaxBytes: cfg.Tools.WebFetch.MaxBytes,
|
||||
AllowPrivateNetworks: cfg.Tools.WebFetch.AllowPrivateNetworks,
|
||||
})
|
||||
if entry == nil {
|
||||
log.Info("web fetch disabled")
|
||||
return
|
||||
}
|
||||
if err := b.toolReg.Register(*entry); err != nil {
|
||||
log.Warn("web_fetch registration failed", "err", err)
|
||||
return
|
||||
}
|
||||
log.Info("web fetch enabled",
|
||||
"allow_private_networks", cfg.Tools.WebFetch.AllowPrivateNetworks)
|
||||
}
|
||||
|
||||
// registerMinimaxTool registers generate_video. Off by default -- see
|
||||
// MinimaxConfig's doc comment, it spends real API credits per call -- and,
|
||||
// unlike web_fetch, needs a resolved credential before it can be
|
||||
@@ -1390,7 +1588,6 @@ func (b *boot) buildDaemon() {
|
||||
Bus: b.bus,
|
||||
Forge: b.forgeClient,
|
||||
Trees: b.trees,
|
||||
CapabilityHost: b.capabilityHost,
|
||||
Storage: b.storeBackend,
|
||||
Log: log,
|
||||
Tasks: b.natsClient,
|
||||
@@ -1399,8 +1596,6 @@ func (b *boot) buildDaemon() {
|
||||
StateStoreGrants: b.stateStoreGrants,
|
||||
ContainerPool: b.containerPool,
|
||||
KitLauncher: b.kitLauncher,
|
||||
Guardrails: b.guardrails,
|
||||
ToolRegistry: b.toolReg,
|
||||
Identities: b.identityRunners,
|
||||
RootIdentityID: identity.StableID(configuredIdentityNames(b.cfg)[0]),
|
||||
TaskLogs: b.taskLogs,
|
||||
@@ -1504,15 +1699,9 @@ func (b *boot) setupForgeWebhook() {
|
||||
// The reaction publisher is the consumer's supply side: the same typed
|
||||
// reaction the poll produces, keyed source-independently, so webhook and
|
||||
// poll deliveries of one review dedup (pr-review-remediation.md
|
||||
// decision 2).
|
||||
publishReaction := func(ctx context.Context, reaction workintake.ReviewCommentEnvelope) error {
|
||||
payload, err := reaction.Encode()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return b.d.Tasks.PublishUnique(ctx, reaction.Subject(), reaction.IdempotencyKey(), payload)
|
||||
}
|
||||
receiver := forgewebhook.New(secretValue, cfg.Dispatch.Trigger, cfg.Label, cfg.BotUser, b.d.PublishTask, publishReaction, log)
|
||||
// decision 2). The daemon's PublishReaction is the single reaction write
|
||||
// path: it stamps the root org a webhook delivery would otherwise lack.
|
||||
receiver := forgewebhook.New(secretValue, cfg.Dispatch.Trigger, cfg.Label, cfg.BotUser, b.d.PublishTask, b.d.PublishReaction, log)
|
||||
host, port := parseListenAddr(cfg.Forge.WebhookAddr, "0.0.0.0", 8645)
|
||||
addr := fmt.Sprintf("%s:%d", host, port)
|
||||
srv := &http.Server{Addr: addr, Handler: receiver}
|
||||
@@ -1562,10 +1751,11 @@ func (b *boot) configOrigins() []webui.ConfigOrigin {
|
||||
// the daemon's own configuration state. The daemon is the configuration
|
||||
// owner, so it is the process that renders this view (archie-core-ml30).
|
||||
func (b *boot) configViewInput(ctx context.Context) webui.ConfigViewInput {
|
||||
catalog, _ := b.catalogState()
|
||||
in := webui.ConfigViewInput{
|
||||
Config: b.cfgHolder.Get(),
|
||||
Provenance: b.configOrigins(),
|
||||
Catalog: catalogView(b.catalog),
|
||||
Catalog: catalogView(catalog),
|
||||
}
|
||||
if b.lastReload != nil {
|
||||
status := b.lastReload()
|
||||
|
||||
@@ -109,7 +109,7 @@ func TestConnectEmbeddedNATSUsesManagedWorkerBridge(t *testing.T) {
|
||||
DockerClient: dockerClient,
|
||||
Network: "workers",
|
||||
RequireHostGateway: true,
|
||||
}, slog.New(slog.DiscardHandler))
|
||||
}, nil, slog.New(slog.DiscardHandler))
|
||||
if err != nil {
|
||||
t.Fatalf("container.NewPool = %v", err)
|
||||
}
|
||||
|
||||
@@ -18,10 +18,13 @@ type chatModelManager struct {
|
||||
mu sync.RWMutex
|
||||
models []string
|
||||
active string
|
||||
// catalogs are the non-role sources the model set is derived from, kept
|
||||
// so SetConfigured can rebuild it after a live settings update without a
|
||||
// caller re-passing them.
|
||||
catalogs [][]string
|
||||
// configured, explicit and catalog are the sources the offered set is
|
||||
// derived from: the role assignments (live via SetConfigured), the
|
||||
// [chat].models catalog (fixed for the process), and the model catalog's
|
||||
// own refs (replaced wholesale by SetModelCatalog).
|
||||
configured map[string]string
|
||||
explicit []string
|
||||
catalog []string
|
||||
details map[string]gateway.ModelDetails
|
||||
providerNames map[string]string
|
||||
}
|
||||
@@ -63,13 +66,29 @@ func mergeModelRefs(configured map[string]string, catalogs ...[]string) []string
|
||||
}
|
||||
|
||||
func newChatModelManager(configured map[string]string, catalogs ...[]string) *chatModelManager {
|
||||
return &chatModelManager{
|
||||
models: mergeModelRefs(configured, catalogs...),
|
||||
active: pickChatDefault(configured),
|
||||
catalogs: append([][]string(nil), catalogs...),
|
||||
explicit := make([]string, 0, len(catalogs))
|
||||
for _, catalog := range catalogs {
|
||||
explicit = append(explicit, catalog...)
|
||||
}
|
||||
m := &chatModelManager{
|
||||
configured: configured,
|
||||
explicit: explicit,
|
||||
details: make(map[string]gateway.ModelDetails),
|
||||
providerNames: make(map[string]string),
|
||||
}
|
||||
m.active = pickChatDefault(configured)
|
||||
m.setModelsLocked()
|
||||
return m
|
||||
}
|
||||
|
||||
// setModelsLocked re-derives the offered set from the three sources and keeps
|
||||
// the active selection when the new set still carries it, falling back the way
|
||||
// construction does when it does not.
|
||||
func (m *chatModelManager) setModelsLocked() {
|
||||
m.models = mergeModelRefs(m.configured, m.explicit, m.catalog)
|
||||
if !slices.Contains(m.models, m.active) {
|
||||
m.active = pickChatDefault(m.configured)
|
||||
}
|
||||
}
|
||||
|
||||
// SetConfigured re-derives the model set the manager offers after a live
|
||||
@@ -81,16 +100,22 @@ func newChatModelManager(configured map[string]string, catalogs ...[]string) *ch
|
||||
func (m *chatModelManager) SetConfigured(configured map[string]string) {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
models := mergeModelRefs(configured, m.catalogs...)
|
||||
if !slices.Contains(models, m.active) {
|
||||
m.active = pickChatDefault(configured)
|
||||
}
|
||||
m.models = models
|
||||
m.configured = configured
|
||||
m.setModelsLocked()
|
||||
}
|
||||
|
||||
func (m *chatModelManager) ApplyModelCatalog(snapshot modelcatalog.Snapshot) {
|
||||
// SetModelCatalog replaces the model catalog's contribution to the manager:
|
||||
// the references it offers, the provider display names and the per-model
|
||||
// details. The previous snapshot's entries are dropped rather than merged --
|
||||
// a model or provider the catalog no longer publishes must stop being offered
|
||||
// and described, and these maps are the manager's own state rather than a
|
||||
// second catalogue to fall back on.
|
||||
func (m *chatModelManager) SetModelCatalog(snapshot modelcatalog.Snapshot, models []string) {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
m.catalog = slices.Clone(models)
|
||||
m.details = make(map[string]gateway.ModelDetails)
|
||||
m.providerNames = make(map[string]string)
|
||||
for _, provider := range snapshot.Providers {
|
||||
m.providerNames[provider.ID] = provider.Name
|
||||
for _, model := range provider.Models {
|
||||
@@ -104,6 +129,7 @@ func (m *chatModelManager) ApplyModelCatalog(snapshot modelcatalog.Snapshot) {
|
||||
}
|
||||
}
|
||||
}
|
||||
m.setModelsLocked()
|
||||
}
|
||||
|
||||
func (m *chatModelManager) ProviderDisplayName(provider string) string {
|
||||
|
||||
@@ -12,8 +12,10 @@ import (
|
||||
"github.com/samcharles93/ai-sdk/core"
|
||||
"github.com/samcharles93/ai-sdk/runtime"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/agentexec"
|
||||
"github.com/samcharles93/archie-core/internal/config"
|
||||
"github.com/samcharles93/archie-core/internal/daemon"
|
||||
"github.com/samcharles93/archie-core/internal/domain/messaging"
|
||||
"github.com/samcharles93/archie-core/internal/events"
|
||||
"github.com/samcharles93/archie-core/internal/gateway"
|
||||
"github.com/samcharles93/archie-core/internal/installtype"
|
||||
@@ -29,6 +31,10 @@ type chatSetup struct {
|
||||
// running yet at this point, so the reload-safe Holder is mostly a
|
||||
// formality, but matching the daemon's API keeps callers honest.
|
||||
Cfg *config.Holder
|
||||
// ToolLimits resolves the tool-output policy at each turn, so a live
|
||||
// tool-settings update reaches the next chat turn's tool options rather
|
||||
// than the value captured when the runner was built.
|
||||
ToolLimits func() agentexec.ToolLimits
|
||||
// LLM resolves the provider runtime at each use: a live provider-settings
|
||||
// or model-role-assignments update swaps the runtime wholesale (ai-sdk's
|
||||
// Runtime caches the provider instances it built), so the pointer cannot
|
||||
@@ -63,6 +69,12 @@ type chatSetup struct {
|
||||
// process makes, which the /status health source reads back. Nil disables
|
||||
// recording (the recorder's methods tolerate that rather than panicking).
|
||||
ProviderOutcomes *providerOutcomeRecorder
|
||||
// Transcriber is the voice-transcription capability built by
|
||||
// setupChatRuntime from this process's own [models]/[providers]. Every turn
|
||||
// runner carries it so a speech attachment is transcribed before its
|
||||
// message is recorded. Nil disables the capability (no role configured, or
|
||||
// the provider credential did not resolve), leaving the frontend's note.
|
||||
Transcriber messaging.Transcriber
|
||||
}
|
||||
|
||||
// daemonRunningVersions reports the component versions this process can
|
||||
@@ -175,7 +187,7 @@ func newChatTurnRunner(
|
||||
Sessions: sessionStore,
|
||||
Models: s.ChatModels,
|
||||
Personas: s.Personas,
|
||||
Model: newChatTurnModel(s.LLM, s.ToolReg, cfg.Chat.MaxSteps, toolLimits(cfg), s.ProviderOutcomes),
|
||||
Model: newChatTurnModel(s.LLM, s.ToolReg, cfg.Chat.MaxSteps, s.ToolLimits, s.ProviderOutcomes),
|
||||
TaskLister: s.ChatTaskLister,
|
||||
Tasks: s.ChatTasks,
|
||||
TaskLogs: s.ChatTaskLogs,
|
||||
@@ -190,6 +202,7 @@ func newChatTurnRunner(
|
||||
MemoryEngine: s.MemoryEngine,
|
||||
MemoryWriter: s.MemoryWriter,
|
||||
UserIdentity: userIdentityResolver(),
|
||||
Transcriber: s.Transcriber,
|
||||
Log: s.Log,
|
||||
})
|
||||
if err := runner.Recover(ctx); err != nil && s.Log != nil {
|
||||
|
||||
@@ -23,7 +23,10 @@ type chatTurnModel struct {
|
||||
llm func() *runtime.Runtime
|
||||
registry *tools.Registry
|
||||
maxSteps int
|
||||
limits agentexec.ToolLimits
|
||||
// limits resolves the tool-output policy at each turn, so a live
|
||||
// tool-settings update is the policy the next turn builds its tool options
|
||||
// with; a snapshot taken at construction would keep the boot limits.
|
||||
limits func() agentexec.ToolLimits
|
||||
// outcomes records each call's result for /status (see sendChatTurn).
|
||||
outcomes *providerOutcomeRecorder
|
||||
}
|
||||
@@ -32,7 +35,7 @@ func newChatTurnModel(
|
||||
llm func() *runtime.Runtime,
|
||||
registry *tools.Registry,
|
||||
maxSteps int,
|
||||
limits agentexec.ToolLimits,
|
||||
limits func() agentexec.ToolLimits,
|
||||
outcomes *providerOutcomeRecorder,
|
||||
) gateway.TurnModel {
|
||||
return &chatTurnModel{
|
||||
@@ -48,7 +51,11 @@ func (m *chatTurnModel) Prepare(
|
||||
ctx context.Context,
|
||||
req gateway.TurnPrepareContext,
|
||||
) (gateway.PreparedTurnModel, error) {
|
||||
options, err := chatGenerateOptions(ctx, nil, m.registry, m.maxSteps, m.limits, req.Extra, req.ContextWindow)
|
||||
limits := agentexec.ToolLimits{}
|
||||
if m.limits != nil {
|
||||
limits = m.limits()
|
||||
}
|
||||
options, err := chatGenerateOptions(ctx, nil, m.registry, m.maxSteps, limits, req.Extra, req.ContextWindow)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -60,6 +67,7 @@ func (m *chatTurnModel) Prepare(
|
||||
llm: m.llm(),
|
||||
model: req.Model,
|
||||
options: options,
|
||||
reasoning: req.Reasoning,
|
||||
toolInfo: toolSummaries(options.Tools),
|
||||
toolTokens: gateway.EstimateTokens(string(toolSchema)),
|
||||
outcomes: m.outcomes,
|
||||
@@ -68,9 +76,13 @@ func (m *chatTurnModel) Prepare(
|
||||
}
|
||||
|
||||
type preparedChatTurnModel struct {
|
||||
llm *runtime.Runtime
|
||||
model string
|
||||
options core.GenerateOptions
|
||||
llm *runtime.Runtime
|
||||
model string
|
||||
options core.GenerateOptions
|
||||
// reasoning is the catalog's class for this model, carried from the
|
||||
// prepare context so Generate can choose the output-token parameter the
|
||||
// provider accepts.
|
||||
reasoning bool
|
||||
toolInfo []gateway.ToolSummary
|
||||
toolTokens int
|
||||
outcomes *providerOutcomeRecorder
|
||||
@@ -178,6 +190,20 @@ func (m *preparedChatTurnModel) ToolSchemaTokens() int {
|
||||
return m.toolTokens
|
||||
}
|
||||
|
||||
// maxTokensForRequest returns the output-token bound to place on an ai-sdk
|
||||
// generate request for a model. A reasoning-class model gets no bound at all:
|
||||
// the OpenAI chat-completions provider serialises a non-zero bound as
|
||||
// `max_tokens`, which reasoning models reject with HTTP 400 ("Use
|
||||
// 'max_completion_tokens' instead"), and the pinned ai-sdk release has no
|
||||
// max_completion_tokens path, so no bound is the only usable form. A classic
|
||||
// model keeps the caller's bound unchanged.
|
||||
func maxTokensForRequest(reasoning bool, bound int) int {
|
||||
if reasoning {
|
||||
return 0
|
||||
}
|
||||
return bound
|
||||
}
|
||||
|
||||
func (m *preparedChatTurnModel) Generate(
|
||||
ctx context.Context,
|
||||
request gateway.TurnModelRequest,
|
||||
@@ -187,7 +213,7 @@ func (m *preparedChatTurnModel) Generate(
|
||||
options.Messages = buildTurnMessages(request)
|
||||
// This is one provider response's output allowance, not a turn-
|
||||
// continuation budget. Tool loops remain free to continue.
|
||||
options.MaxTokens = request.MaxOutputTokens
|
||||
options.MaxTokens = maxTokensForRequest(m.reasoning, request.MaxOutputTokens)
|
||||
// A runtime swapped to nil -- a live update that left no usable provider
|
||||
// configured -- is a refused turn, not a panic.
|
||||
if m.llm == nil {
|
||||
|
||||
@@ -2,12 +2,14 @@ package archied
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/agentexec"
|
||||
"github.com/samcharles93/archie-core/internal/app/controlplane"
|
||||
"github.com/samcharles93/archie-core/internal/config"
|
||||
"github.com/samcharles93/archie-core/internal/domain/applystatus"
|
||||
"github.com/samcharles93/archie-core/internal/domain/workflow"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/configuration"
|
||||
)
|
||||
@@ -75,7 +77,8 @@ func (b *boot) reloadConfig(ctx context.Context, doc *configuration.Document) er
|
||||
// drop them from the running config even though the file is
|
||||
// unchanged. Re-apply the same merge (idempotent: a catalog that
|
||||
// failed to load merges to identity).
|
||||
applyModelCatalog(&doc.Config, b.catalog)
|
||||
catalog, _ := b.catalogState()
|
||||
applyModelCatalog(&doc.Config, catalog)
|
||||
// Bounded: this runs on the signal loop, which handles nothing else
|
||||
// while it waits, and a State Store that has stopped answering must
|
||||
// surface as a failed reload rather than a SIGHUP that never returns.
|
||||
@@ -89,6 +92,10 @@ func (b *boot) reloadConfig(ctx context.Context, doc *configuration.Document) er
|
||||
old := b.cfgHolder.Get()
|
||||
b.currentProvenance.Store(&doc.Provenance)
|
||||
b.publishConfig(ctx, cfg)
|
||||
// The container pool reads the published config on every acquire, but the
|
||||
// dispatcher holds its limit; a reloaded containers.max_concurrency must
|
||||
// reach it the same way a control-plane update does.
|
||||
b.resizeTaskDispatcher(cfg.Containers.MaxConcurrency)
|
||||
if fields := changedNonReloadableFields(old, cfg); len(fields) > 0 {
|
||||
b.log.Warn("config reloaded; some changes require a restart",
|
||||
"fields", fields, "paths", doc.Provenance.Paths())
|
||||
@@ -257,16 +264,34 @@ func (b *boot) startLiveSettings(ctx context.Context) error {
|
||||
// (b.runtimeConfig, which applies the execution budgets the settings watch
|
||||
// published) and republishes through config.Holder, so the change takes
|
||||
// effect without a restart. Kinds stay out of this list while a
|
||||
// startup-built component still holds their value -- tool, plugin, container
|
||||
// and channel settings are frozen in components built at boot and remain
|
||||
// restart-required -- and a kind joins it only with a consumer that re-reads
|
||||
// it. Each kind is watched in its own goroutine, exactly the shape the
|
||||
// startup-built component still holds their value; none does today, so the
|
||||
// list is every control-plane kind. A kind joins it only with a consumer that
|
||||
// re-reads it. ToolSettingsKind is on it because applyRuntimeResourceUpdate
|
||||
// reconciles the MCP provider set and rebuilds the two config-derived tool
|
||||
// entries (boot.reconcileToolSettings, its consumer); a removed MCP server
|
||||
// really disconnects, because it is a child process with a Stop, not a Yaegi
|
||||
// interpreter. ContainerRuntimePoliciesKind is on it because both consumers
|
||||
// re-read the published config: the container pool takes its image, pull
|
||||
// policy, network, max-uptime and concurrency cap per acquire, and the daemon's
|
||||
// dispatcher is resized after the publish (applyRuntimeResourceUpdate below).
|
||||
// PluginSettingsKind is on the list because its only consumer is the directory
|
||||
// reconciliation, which reads the running config each tick; a removed plugin
|
||||
// or engine still cannot unload, so that kind reports the removal rather than
|
||||
// requiring a restart. ChannelSettingsKind is on the list for this process's
|
||||
// own re-layer; the Messaging Service, which owns the channel transports,
|
||||
// reconciles them separately and restarts only the channel whose settings
|
||||
// changed. Each kind is watched in its own goroutine, exactly the shape the
|
||||
// workflow-execution-settings watch established.
|
||||
var runtimeResourceKinds = []string{
|
||||
controlplane.ProviderSettingsKind,
|
||||
controlplane.ModelRoleAssignmentsKind,
|
||||
controlplane.RepositoryPoliciesKind,
|
||||
controlplane.SchedulingPolicyKind,
|
||||
controlplane.ToolSettingsKind,
|
||||
controlplane.PluginSettingsKind,
|
||||
controlplane.ReviewSettingsKind,
|
||||
controlplane.ChannelSettingsKind,
|
||||
controlplane.ContainerRuntimePoliciesKind,
|
||||
}
|
||||
|
||||
// startRuntimeResourceWatches keeps a watch per live kind established for the
|
||||
@@ -319,12 +344,20 @@ func (b *boot) applyRuntimeResourceUpdate(ctx context.Context, kind string, upda
|
||||
b.log.Error("runtime settings watch failed", "kind", kind, "err", update.Err)
|
||||
return
|
||||
}
|
||||
if kind == controlplane.PluginSettingsKind {
|
||||
if err := b.refuseSkillsDirChange(ctx); err != nil {
|
||||
b.applyStatus.Report(ctx, kind, update.Version, err)
|
||||
b.log.Error("plugin settings refused; the running settings stay", "version", update.Version, "err", err)
|
||||
return
|
||||
}
|
||||
}
|
||||
base := b.cfgHolder.Get()
|
||||
// Boot publishes before the model catalog is merged in, so the snapshot
|
||||
// this read returns can lack the catalog's model limits and discovered
|
||||
// providers. The merge is idempotent, and reloadConfig re-applies it
|
||||
// before layering for the same reason.
|
||||
applyModelCatalog(&base, b.catalog)
|
||||
catalog, _ := b.catalogState()
|
||||
applyModelCatalog(&base, catalog)
|
||||
cfg, _, err := b.runtimeConfig(ctx, base)
|
||||
if err != nil {
|
||||
// runtimeConfig reported the refusal through apply status for every
|
||||
@@ -337,19 +370,113 @@ func (b *boot) applyRuntimeResourceUpdate(ctx context.Context, kind string, upda
|
||||
switch kind {
|
||||
case controlplane.ProviderSettingsKind, controlplane.ModelRoleAssignmentsKind:
|
||||
b.rebuildChatModelRuntime(cfg)
|
||||
case controlplane.ToolSettingsKind:
|
||||
// The document's server set and tool entries are live now; reconcile
|
||||
// them and report the reconciliation's own outcome, which is the one
|
||||
// that knows whether a server failed to connect.
|
||||
if err := b.reconcileToolSettings(ctx, cfg); err != nil {
|
||||
b.applyStatus.Report(ctx, kind, update.Version, err)
|
||||
b.log.Error("tool settings reconciled with errors; the running servers stay", "version", update.Version, "err", err)
|
||||
}
|
||||
case controlplane.PluginSettingsKind:
|
||||
// The document's directories are live now; load what they hold and
|
||||
// report the reconciliation's own outcome, which is the one that knows
|
||||
// whether a removal is outstanding.
|
||||
b.reconcileRuntimePlugins(ctx)
|
||||
case controlplane.ContainerRuntimePoliciesKind:
|
||||
// The pool reads the republished config itself; the dispatcher holds
|
||||
// its limit, so a raise must be woken to admit queued work now and a
|
||||
// lower must be honoured as running tasks finish.
|
||||
b.resizeTaskDispatcher(cfg.Containers.MaxConcurrency)
|
||||
}
|
||||
}
|
||||
|
||||
// resizeTaskDispatcher applies a new containers.max_concurrency to the
|
||||
// daemon's running dispatcher. The pool needs no poke because it reads the
|
||||
// published config on every acquire. b.d is nil in the boot window in which
|
||||
// the watches are already live but the daemon is not built yet; there the
|
||||
// dispatcher is created later from the config the next drain reads.
|
||||
func (b *boot) resizeTaskDispatcher(maxConcurrency int) {
|
||||
if b.d != nil {
|
||||
b.d.ResizeTaskDispatcher(maxConcurrency)
|
||||
}
|
||||
}
|
||||
|
||||
// startPluginReconcile establishes the plugin, module and secret-engine
|
||||
// directory reconciliation on the apply-status restamp interval. Boot calls it
|
||||
// after loadPlugins, loadWorkflows and configuredSecretRegistry have loaded
|
||||
// the directories, so the seed records what is already running and the first
|
||||
// tick loads only what appears or changes afterwards.
|
||||
func (b *boot) startPluginReconcile(ctx context.Context) {
|
||||
r := newPluginReconciler(b.log, pluginReconcileTargets{
|
||||
host: b.capabilityHost,
|
||||
secrets: b.secrets,
|
||||
modules: b.modules,
|
||||
dirs: func() config.Config { return b.cfgHolder.Get() },
|
||||
relayer: func(ctx context.Context) error {
|
||||
cfg, _, err := b.runtimeConfig(ctx, b.cfgHolder.Get())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
b.publishConfig(ctx, cfg)
|
||||
return nil
|
||||
},
|
||||
report: func(ctx context.Context, err error) {
|
||||
b.applyStatus.Report(ctx, controlplane.PluginSettingsKind, b.applyStatus.AppliedVersion(controlplane.PluginSettingsKind), err)
|
||||
},
|
||||
})
|
||||
b.pluginReconciler = r
|
||||
r.seed()
|
||||
go r.run(ctx, applystatus.RestampInterval)
|
||||
}
|
||||
|
||||
// reconcileRuntimePlugins runs one directory reconciliation after a stored
|
||||
// plugin-settings change, so a new directory loads on the tick the document
|
||||
// lands rather than waiting for the poller.
|
||||
func (b *boot) reconcileRuntimePlugins(ctx context.Context) {
|
||||
if b.pluginReconciler == nil {
|
||||
return
|
||||
}
|
||||
_ = b.pluginReconciler.reconcile(ctx)
|
||||
}
|
||||
|
||||
// refuseSkillsDirChange refuses a stored plugin-settings document whose
|
||||
// skills_dir differs from the value in force. skills_dir is the one directory
|
||||
// with no live consumer, so a change to it stays a restart-scoped edit rather
|
||||
// than a value the daemon reports as applied. The check reads the running
|
||||
// config, so it cannot live in validatePluginSettings, which is value-agnostic
|
||||
// by the archie-core-1143 decision.
|
||||
func (b *boot) refuseSkillsDirChange(ctx context.Context) error {
|
||||
var stored struct {
|
||||
SkillsDir string `json:"skills_dir"`
|
||||
}
|
||||
_, found, err := b.controlPlane.Query(ctx, controlplane.PluginSettingsKind, func(value []byte) error {
|
||||
return json.Unmarshal(value, &stored)
|
||||
})
|
||||
if err != nil {
|
||||
return fmt.Errorf("read plugin settings: %w", err)
|
||||
}
|
||||
if !found {
|
||||
return nil
|
||||
}
|
||||
if running := b.cfgHolder.Get().SkillsDir; stored.SkillsDir != running {
|
||||
return fmt.Errorf("skills_dir applies on restart: stored %q, running %q", stored.SkillsDir, running)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// rebuildChatModelRuntime re-derives the gateway chat runtime's provider set
|
||||
// after a live change to provider-settings or model-role-assignments. The
|
||||
// turn runner reads the runtime through boot.chatLLM, so the swap reaches its
|
||||
// next turn without rebuilding the runner, and the model manager re-derives
|
||||
// the references it offers from the new role assignments.
|
||||
// the references it offers from the new role assignments and the catalog.
|
||||
func (b *boot) rebuildChatModelRuntime(cfg config.Config) {
|
||||
if b.chatModels == nil {
|
||||
return // this process serves no chat turns
|
||||
}
|
||||
b.setLLM(agentexec.NewRuntime(executionProviders(cfg)))
|
||||
b.chatModels.SetConfigured(cfg.Models)
|
||||
catalog, models := b.catalogState()
|
||||
b.chatModels.SetModelCatalog(catalog, models)
|
||||
b.log.Info("chat model runtime rebuilt", "providers", len(cfg.Providers), "models", len(cfg.Models))
|
||||
}
|
||||
@@ -15,6 +15,7 @@ import (
|
||||
"github.com/samcharles93/archie-core/internal/agentexec"
|
||||
"github.com/samcharles93/archie-core/internal/app/controlplane"
|
||||
pb "github.com/samcharles93/archie-core/internal/contracts/controlplane/v1"
|
||||
"github.com/samcharles93/archie-core/internal/daemon"
|
||||
"github.com/samcharles93/archie-core/internal/secret"
|
||||
)
|
||||
|
||||
@@ -240,3 +241,33 @@ func TestLiveModelRoleUpdateRebuildsTheChatModelRuntime(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestLiveContainerRuntimePoliciesReLayerAndResizeTheDispatcher pins the
|
||||
// container kind's live seam (archie-core-zfb0.2) at the apply path: a stored
|
||||
// container-runtime-policies document re-layers into the running config the
|
||||
// pool reads per acquire, and the daemon resizes its running dispatcher from
|
||||
// the same publish rather than requiring a restart.
|
||||
func TestLiveContainerRuntimePoliciesReLayerAndResizeTheDispatcher(t *testing.T) {
|
||||
values := databaseOwnedResources()
|
||||
values[controlplane.ContainerRuntimePoliciesKind] = map[string]any{
|
||||
"image": "archie:live", "max_concurrency": 5, "pull_policy": "missing",
|
||||
}
|
||||
stub := &resourceWatchStub{values: values, version: 3, storedVersion: map[string]int64{}, opens: map[string]int{}}
|
||||
b, _ := newResourceWatchBoot(t, stub)
|
||||
if err := b.loadRuntimeConfig(t.Context()); err != nil {
|
||||
t.Fatalf("loadRuntimeConfig: %v", err)
|
||||
}
|
||||
// A running daemon, so the resize arm executes against a real dispatcher
|
||||
// rather than taking the boot-window nil guard.
|
||||
b.d = &daemon.Daemon{Cfg: b.cfgHolder, Log: b.log}
|
||||
|
||||
b.applyRuntimeResourceUpdate(t.Context(), controlplane.ContainerRuntimePoliciesKind, controlplane.AppliedResource{Version: 4})
|
||||
|
||||
got := b.cfgHolder.Get().Containers
|
||||
if got.MaxConcurrency != 5 {
|
||||
t.Errorf("running max_concurrency = %d, want the stored 5", got.MaxConcurrency)
|
||||
}
|
||||
if got.Image != "archie:live" {
|
||||
t.Errorf("running image = %q, want the stored archie:live", got.Image)
|
||||
}
|
||||
}
|
||||
@@ -12,6 +12,7 @@ import (
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/config"
|
||||
"github.com/samcharles93/archie-core/internal/domain/applystatus"
|
||||
"github.com/samcharles93/archie-core/internal/domain/messaging"
|
||||
"github.com/samcharles93/archie-core/internal/events"
|
||||
"github.com/samcharles93/archie-core/internal/gateway"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/gatewayrpc"
|
||||
@@ -71,6 +72,7 @@ func RunGateway(ctx context.Context, options GatewayOptions) error {
|
||||
return err
|
||||
}
|
||||
b.loadCatalog(ctx, options.Config)
|
||||
b.seedSoul(options.Config)
|
||||
url, token := b.cfg.NATS.URL, ""
|
||||
var nc *natsio.Conn
|
||||
var err error
|
||||
@@ -189,18 +191,30 @@ func (b *boot) startGatewayRuntime(ctx context.Context, actor gateway.ChatTaskAc
|
||||
return contract, nil
|
||||
}
|
||||
|
||||
// inboundMessageHeadroomBytes leaves room for the protobuf framing and the
|
||||
// rest of the request around an attachment at the messaging contract's
|
||||
// ceiling, so the largest attachment a channel frontend may send is accepted
|
||||
// rather than rejected as an oversized gRPC message.
|
||||
const inboundMessageHeadroomBytes = 1 << 20
|
||||
|
||||
// gatewayServerOpts applies the transport security boundary: a loopback
|
||||
// listener is served insecure with no token; any non-loopback address
|
||||
// requires a Bearer [REDACTED], else the process fails closed. It mirrors
|
||||
// stateStoreServerOpts, minus the State Store's task-scoped grants: the
|
||||
// Gateway has one administrative token for all callers.
|
||||
//
|
||||
// The receive limit is raised above gRPC's 4 MB default because an inbound
|
||||
// request carries a channel attachment's bytes: the channel frontend
|
||||
// downloads the file, and this process -- which runs the turn -- holds no
|
||||
// platform credential to fetch it again.
|
||||
func gatewayServerOpts(listen, token string) (opts []grpc.ServerOption, loopback bool, err error) {
|
||||
recv := grpc.MaxRecvMsgSize(messaging.MaxInboundAttachmentBytes + inboundMessageHeadroomBytes)
|
||||
loopback, err = gatewayrpc.TargetIsLoopback(listen)
|
||||
if err != nil {
|
||||
return nil, false, err
|
||||
}
|
||||
if loopback {
|
||||
return nil, true, nil
|
||||
return []grpc.ServerOption{recv}, true, nil
|
||||
}
|
||||
if token == "" {
|
||||
return nil, false, fmt.Errorf(
|
||||
@@ -209,6 +223,7 @@ func gatewayServerOpts(listen, token string) (opts []grpc.ServerOption, loopback
|
||||
)
|
||||
}
|
||||
return []grpc.ServerOption{
|
||||
recv,
|
||||
grpc.ChainUnaryInterceptor(gatewayrpc.UnaryServerInterceptor(token)),
|
||||
grpc.ChainStreamInterceptor(gatewayrpc.StreamServerInterceptor(token)),
|
||||
}, false, nil
|
||||
|
||||
@@ -9,7 +9,9 @@ import (
|
||||
"github.com/samcharles93/archie-core/internal/config"
|
||||
"github.com/samcharles93/archie-core/internal/domain/agent"
|
||||
"github.com/samcharles93/archie-core/internal/domain/storecontract"
|
||||
"github.com/samcharles93/archie-core/internal/domain/taskactions"
|
||||
"github.com/samcharles93/archie-core/internal/gateway"
|
||||
"github.com/samcharles93/archie-core/internal/taskstate"
|
||||
"github.com/samcharles93/archie-core/internal/tools"
|
||||
)
|
||||
|
||||
@@ -23,9 +25,15 @@ func (b *boot) setupChatRuntime(ctx context.Context, cfg config.Config) error {
|
||||
// non-command message processing.
|
||||
providers := executionProviders(cfg)
|
||||
b.setLLM(agentexec.NewRuntime(providers))
|
||||
// Transcription is a model-role capability, like the chat models it sits
|
||||
// beside: it is built here, on the model-owning side, from this process's
|
||||
// own [models]/[providers]. The Messaging Service carries a voice note's
|
||||
// bytes to the Gateway and never holds the credential.
|
||||
b.setupTranscriber(cfg, b.log)
|
||||
b.toolReg = tools.NewRegistry()
|
||||
chatModels := newChatModelManager(cfg.Models, cfg.Chat.Models, b.catalogModels)
|
||||
chatModels.ApplyModelCatalog(b.catalog)
|
||||
chatModels := newChatModelManager(cfg.Models, cfg.Chat.Models)
|
||||
catalog, catalogModels := b.catalogState()
|
||||
chatModels.SetModelCatalog(catalog, catalogModels)
|
||||
b.chatModels = chatModels
|
||||
|
||||
// ── Persona registry ─────────────────────────────────────────────
|
||||
@@ -63,8 +71,14 @@ func (b *boot) setupChatRuntime(ctx context.Context, cfg config.Config) error {
|
||||
canceller = ec
|
||||
}
|
||||
b.chatController = gateway.NewStoreTaskController(chatTaskControllerAdapter{
|
||||
taskByID: b.stateStore.TaskByID,
|
||||
requeue: b.stateStore.Requeue,
|
||||
taskByID: b.stateStore.TaskByID,
|
||||
// Chat's /approve reaches the daemon's one task-action service rather
|
||||
// than its own requeue, so a chat approval and a dashboard approval
|
||||
// cannot record different decisions for one operator intent
|
||||
// (docs/prds/pr-review-operator-response.md, Decision 1).
|
||||
approve: func(ctx context.Context, scope *string, actor taskactions.Actor, taskID int64, res taskactions.ActionPayload) error {
|
||||
return b.taskActions().Apply(ctx, scope, actor, taskID, taskstate.ActionApprove, res)
|
||||
},
|
||||
cancelExecution: canceller.CancelExecution,
|
||||
})
|
||||
b.updateService = makeUpdateService(chatSetup{Cfg: config.NewHolder(cfg)})
|
||||
@@ -123,8 +137,13 @@ func (b *boot) setupGatewayChat(ctx context.Context, actor gateway.ChatTaskActor
|
||||
router.InitSessions(b.chatSessionStore)
|
||||
configureTaskCommands(router, b.chatTasks, b.chatController, chatTaskListerAdapter{tasks: b.stateStore.Tasks}, b.defaultChatIdentity)
|
||||
router.Health = b.statusHealth
|
||||
// ToolLimits reads the live holder, not cfg: the daemon's runtime-resource
|
||||
// watch republishes a tool-settings change through it, so a policy edit
|
||||
// applies to the next turn in the process that serves this runner.
|
||||
setup := chatSetup{
|
||||
Cfg: config.NewHolder(cfg), LLM: b.chatLLM, ChatModels: b.chatModels, ToolReg: b.toolReg,
|
||||
Cfg: config.NewHolder(cfg),
|
||||
ToolLimits: func() agentexec.ToolLimits { return toolLimits(b.cfgHolder.Get()) },
|
||||
LLM: b.chatLLM, ChatModels: b.chatModels, ToolReg: b.toolReg,
|
||||
Personas: b.personas, ChatTasks: b.chatTasks,
|
||||
ChatTaskLister: chatTaskListerAdapter{tasks: b.stateStore.Tasks},
|
||||
ChatTaskLogs: chatTaskLogReaderAdapter{tasks: b.stateStore.TaskByID, taskLogs: b.taskLogs},
|
||||
@@ -133,6 +152,9 @@ func (b *boot) setupGatewayChat(ctx context.Context, actor gateway.ChatTaskActor
|
||||
Bus: b.bus, Log: b.log,
|
||||
MemoryEngine: b.memoryStore(),
|
||||
MemoryWriter: b.memoryWriter(),
|
||||
// The transcription capability is built beside the chat runtime, from
|
||||
// the model-role configuration only this process reads.
|
||||
Transcriber: b.transcriber,
|
||||
// The Gateway executes the web chat's turns, so it is the process
|
||||
// that must record their outcomes for /status.
|
||||
ProviderOutcomes: b.providerOutcomes,
|
||||
|
||||
@@ -2,8 +2,11 @@ package archied
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"io"
|
||||
"log/slog"
|
||||
"net"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
@@ -25,8 +28,10 @@ func TestGatewayServerOptsLoopbackIsInsecure(t *testing.T) {
|
||||
if !loopback {
|
||||
t.Fatal("loopback listener should report loopback")
|
||||
}
|
||||
if len(opts) != 0 {
|
||||
t.Fatalf("loopback listener should have no server opts, got %d", len(opts))
|
||||
// Only the inbound size limit; a loopback listener installs no auth
|
||||
// interceptors (the count is the only handle on opaque ServerOptions).
|
||||
if len(opts) != 1 {
|
||||
t.Fatalf("loopback listener should install only the inbound size limit, got %d options", len(opts))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -41,8 +46,8 @@ func TestGatewayServerOptsNonLoopbackRequiresToken(t *testing.T) {
|
||||
if loopback {
|
||||
t.Fatal("non-loopback listener should not report loopback")
|
||||
}
|
||||
if len(opts) != 2 {
|
||||
t.Fatalf("non-loopback listener should install two server options (unary and stream auth interceptors), got %d", len(opts))
|
||||
if len(opts) != 3 {
|
||||
t.Fatalf("non-loopback listener should install three server options (inbound size limit plus unary and stream auth interceptors), got %d", len(opts))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -89,11 +94,11 @@ func (stubGatewayContract) SetPersona(context.Context, string, string) (bool, er
|
||||
return false, nil
|
||||
}
|
||||
|
||||
func (stubGatewayContract) ApplyTaskAction(context.Context, string, int64, taskstate.Action) (messaging.TaskActionResult, error) {
|
||||
func (stubGatewayContract) ApplyTaskAction(context.Context, string, int64, taskstate.Action, taskactions.ActionPayload) (messaging.TaskActionResult, error) {
|
||||
return messaging.TaskActionResult{}, nil
|
||||
}
|
||||
|
||||
func (stubGatewayContract) ApplyOperatorTaskAction(context.Context, taskactions.Actor, int64, taskstate.Action) (messaging.TaskActionResult, error) {
|
||||
func (stubGatewayContract) ApplyOperatorTaskAction(context.Context, taskactions.Actor, int64, taskstate.Action, taskactions.ActionPayload) (messaging.TaskActionResult, error) {
|
||||
return messaging.TaskActionResult{}, nil
|
||||
}
|
||||
|
||||
@@ -135,3 +140,73 @@ func TestGatewayServingAnnouncesReady(t *testing.T) {
|
||||
t.Fatalf("serveGatewayListener returned: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// recordingGatewayContract captures the inbound a Stream call reached the
|
||||
// Gateway with, so a test can prove the request's payload survived the wire.
|
||||
type recordingGatewayContract struct {
|
||||
stubGatewayContract
|
||||
mu sync.Mutex
|
||||
last messaging.Inbound
|
||||
}
|
||||
|
||||
func (c *recordingGatewayContract) Stream(_ context.Context, in messaging.Inbound) (<-chan messaging.ChatEvent, error) {
|
||||
c.mu.Lock()
|
||||
c.last = in
|
||||
c.mu.Unlock()
|
||||
events := make(chan messaging.ChatEvent)
|
||||
close(events)
|
||||
return events, nil
|
||||
}
|
||||
|
||||
// TestGatewayAcceptsAnInboundAttachmentOverTheDefaultGRPCLimit: the inbound
|
||||
// request now carries a channel attachment's bytes, and gRPC's default receive
|
||||
// limit is 4 MB -- well under the messaging contract's ceiling. A maximal
|
||||
// attachment must reach the turn, not fail the RPC with ResourceExhausted.
|
||||
func TestGatewayAcceptsAnInboundAttachmentOverTheDefaultGRPCLimit(t *testing.T) {
|
||||
rec := &recordingGatewayContract{}
|
||||
ln, err := (&net.ListenConfig{}).Listen(t.Context(), "tcp", "127.0.0.1:0")
|
||||
if err != nil {
|
||||
t.Fatalf("listen: %v", err)
|
||||
}
|
||||
opts, _, err := gatewayServerOpts(ln.Addr().String(), "")
|
||||
if err != nil {
|
||||
t.Fatalf("gatewayServerOpts: %v", err)
|
||||
}
|
||||
ctx, cancel := context.WithCancel(t.Context())
|
||||
defer cancel()
|
||||
serveCh := make(chan error, 1)
|
||||
go func() { serveCh <- serveGateway(ctx, ln, rec, nil, opts) }()
|
||||
defer func() {
|
||||
cancel()
|
||||
if err := <-serveCh; err != nil {
|
||||
t.Errorf("serveGateway: %v", err)
|
||||
}
|
||||
}()
|
||||
|
||||
conn, err := grpc.NewClient("passthrough:///"+ln.Addr().String(), grpc.WithTransportCredentials(insecure.NewCredentials()))
|
||||
if err != nil {
|
||||
t.Fatalf("dial: %v", err)
|
||||
}
|
||||
defer conn.Close()
|
||||
|
||||
raw := make([]byte, 5<<20) // larger than gRPC's 4 MB default receive limit
|
||||
stream, err := pb.NewChatServiceClient(conn).Stream(t.Context(), &pb.StreamRequest{
|
||||
Message: &pb.Message{Media: []*pb.Media{{Type: "image", MimeType: "image/jpeg", Data: raw}}},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("Stream with a %d-byte attachment: %v", len(raw), err)
|
||||
}
|
||||
if _, err := stream.Recv(); !errors.Is(err, io.EOF) {
|
||||
t.Fatalf("Recv after an empty stream: %v, want EOF", err)
|
||||
}
|
||||
|
||||
rec.mu.Lock()
|
||||
got := rec.last
|
||||
rec.mu.Unlock()
|
||||
if len(got.Media) != 1 {
|
||||
t.Fatalf("turn received %d attachments, want 1", len(got.Media))
|
||||
}
|
||||
if len(got.Media[0].Data) != len(raw) {
|
||||
t.Fatalf("turn received %d attachment bytes, want %d", len(got.Media[0].Data), len(raw))
|
||||
}
|
||||
}
|
||||
@@ -198,7 +198,7 @@ func npmCacheServerEnv(command, workDir string) []string {
|
||||
return mcp.NpmCacheEnv(command, filepath.Join(workDir, "mcp-npm-cache"))
|
||||
}
|
||||
|
||||
func configuredMCPProvider(server config.MCPServer, workDir string) (toolprovider.Engine, error) {
|
||||
func configuredMCPProvider(server config.MCPServer, workDir string, sampling mcp.SamplingHandler) (toolprovider.Engine, error) {
|
||||
name := strings.TrimSpace(server.Name)
|
||||
if name == "" {
|
||||
return nil, fmt.Errorf("MCP server name is required")
|
||||
@@ -208,6 +208,8 @@ func configuredMCPProvider(server config.MCPServer, workDir string) (toolprovide
|
||||
transportType = "stdio"
|
||||
}
|
||||
|
||||
samplingOption := mcptoolprovider.WithSamplingHandler(sampling)
|
||||
|
||||
switch transportType {
|
||||
case "stdio":
|
||||
command := strings.TrimSpace(server.Command)
|
||||
@@ -220,7 +222,7 @@ func configuredMCPProvider(server config.MCPServer, workDir string) (toolprovide
|
||||
Dir: server.WorkDir,
|
||||
Env: npmCacheServerEnv(command, workDir),
|
||||
})
|
||||
return mcptoolprovider.New(name, transport, server.ParallelToolCalls), nil
|
||||
return mcptoolprovider.New(name, transport, server.ParallelToolCalls, samplingOption), nil
|
||||
|
||||
case "http", "streamablehttp":
|
||||
url := strings.TrimSpace(server.URL)
|
||||
@@ -231,7 +233,7 @@ func configuredMCPProvider(server config.MCPServer, workDir string) (toolprovide
|
||||
Endpoint: url,
|
||||
Headers: server.Headers,
|
||||
})
|
||||
return mcptoolprovider.New(name, transport, server.ParallelToolCalls), nil
|
||||
return mcptoolprovider.New(name, transport, server.ParallelToolCalls, samplingOption), nil
|
||||
|
||||
case "sse":
|
||||
sseEndpoint := strings.TrimSpace(server.SSEEndpoint)
|
||||
@@ -243,7 +245,7 @@ func configuredMCPProvider(server config.MCPServer, workDir string) (toolprovide
|
||||
MessageEndpoint: strings.TrimSpace(server.MessageEndpoint),
|
||||
Headers: server.Headers,
|
||||
})
|
||||
return mcptoolprovider.New(name, transport, server.ParallelToolCalls), nil
|
||||
return mcptoolprovider.New(name, transport, server.ParallelToolCalls, samplingOption), nil
|
||||
|
||||
default:
|
||||
return nil, fmt.Errorf("MCP transport %q is not supported", transportType)
|
||||
@@ -379,6 +381,7 @@ func Run() int { //nolint:cyclop,funlen // the composition root's setup sequence
|
||||
return 0
|
||||
}
|
||||
b.loadCatalog(ctx, args.cfgPath)
|
||||
b.seedSoul(args.cfgPath)
|
||||
b.setupObservability(ctx)
|
||||
|
||||
if err := b.setupBackends(ctx); err != nil {
|
||||
@@ -388,6 +391,11 @@ func Run() int { //nolint:cyclop,funlen // the composition root's setup sequence
|
||||
if err := b.setupLLMAndChat(ctx); err != nil {
|
||||
return 1
|
||||
}
|
||||
// The catalog's consumers exist from here on, and the daemon is the
|
||||
// process that owns the published configuration, so this is where the
|
||||
// live catalog refresh belongs. The standalone Gateway reads the catalog
|
||||
// once at boot and runs no control-plane watches (RunGateway).
|
||||
b.startModelCatalogRefresh(ctx)
|
||||
if err := b.buildTreesAndIdentities(ctx); err != nil {
|
||||
return 1
|
||||
}
|
||||
@@ -404,6 +412,9 @@ func Run() int { //nolint:cyclop,funlen // the composition root's setup sequence
|
||||
if err := b.loadPlugins(); err != nil {
|
||||
return 1
|
||||
}
|
||||
// Every directory loader has run by here, so the reconciliation seeds the
|
||||
// running set and from its first tick loads only what appears or changes.
|
||||
b.startPluginReconcile(ctx)
|
||||
|
||||
if err := b.registerNATSRPC(); err != nil {
|
||||
return 1
|
||||
@@ -414,7 +425,6 @@ func Run() int { //nolint:cyclop,funlen // the composition root's setup sequence
|
||||
return 1
|
||||
}
|
||||
|
||||
b.setupGuardrails()
|
||||
if err := b.registerTools(ctx); err != nil {
|
||||
return 1
|
||||
}
|
||||
@@ -560,7 +570,7 @@ func (a chatTaskWriterAdapter) EnqueueChatTask(
|
||||
|
||||
type chatTaskControllerAdapter struct {
|
||||
taskByID func(context.Context, int64) (*workflow.Task, error)
|
||||
requeue func(context.Context, int64, string, string) error
|
||||
approve func(context.Context, *string, taskactions.Actor, int64, taskactions.ActionPayload) error
|
||||
cancelExecution func(context.Context, int64, string, string) ([]int64, error)
|
||||
}
|
||||
|
||||
@@ -578,8 +588,16 @@ func (a chatTaskControllerAdapter) ChatTaskStatus(ctx context.Context, taskID in
|
||||
return gateway.ChatTaskStatus{}, false, fmt.Errorf("task %d is not chat-originated", taskID)
|
||||
}
|
||||
|
||||
func (a chatTaskControllerAdapter) ApproveChatTask(ctx context.Context, taskID int64) error {
|
||||
return a.requeue(ctx, taskID, workflow.StatusWaitingHuman, "implement")
|
||||
func (a chatTaskControllerAdapter) ApproveChatTask(ctx context.Context, taskID int64, actor taskactions.Actor) error {
|
||||
if a.approve == nil {
|
||||
return fmt.Errorf("task approval is unavailable")
|
||||
}
|
||||
// The chat-bound identity is both the scope the task must belong to and
|
||||
// the actor the record names: a chat command has no cross-identity
|
||||
// authority, and /approve carries no selection syntax, so the review gate
|
||||
// answer posts every offered finding.
|
||||
scope := string(actor.Identity)
|
||||
return a.approve(ctx, &scope, actor, taskID, taskactions.ActionPayload{})
|
||||
}
|
||||
|
||||
func (a chatTaskControllerAdapter) CancelChatTask(ctx context.Context, taskID int64, reason string) error {
|
||||
@@ -684,15 +702,15 @@ type chatTaskActorAdapter struct {
|
||||
}
|
||||
|
||||
func (a chatTaskActorAdapter) ApplyChatTaskAction(
|
||||
ctx context.Context, identity *string, actor taskactions.Actor, taskID int64, action taskstate.Action,
|
||||
ctx context.Context, identity *string, actor taskactions.Actor, taskID int64, action taskstate.Action, res taskactions.ActionPayload,
|
||||
) (gateway.TaskActionResult, error) {
|
||||
if a.contract == nil {
|
||||
return gateway.TaskActionResult{}, gateway.ErrChatCapabilityUnavailable
|
||||
}
|
||||
if identity == nil {
|
||||
return a.contract.ApplyOperatorTaskAction(ctx, actor, taskID, action)
|
||||
return a.contract.ApplyOperatorTaskAction(ctx, actor, taskID, action, res)
|
||||
}
|
||||
return a.contract.ApplyTaskAction(ctx, *identity, taskID, action)
|
||||
return a.contract.ApplyTaskAction(ctx, *identity, taskID, action, res)
|
||||
}
|
||||
|
||||
func chatTaskProfiles(cfg config.Config) ([]gateway.TaskProfile, string) {
|
||||
@@ -955,10 +973,12 @@ func safePluginInfo(p plugin.Plugin) (name, version string) {
|
||||
// there is deliberately no host execution fallback.
|
||||
func startContainers(
|
||||
ctx context.Context,
|
||||
cfg config.Config,
|
||||
cfgHolder *config.Holder,
|
||||
secrets *secret.Registry,
|
||||
log *slog.Logger,
|
||||
) (*container.Pool, storage.Backend, func()) {
|
||||
noop := func() {}
|
||||
cfg := cfgHolder.Get()
|
||||
// Single Docker client shared between pool and storage backend.
|
||||
dockerCli, err := client.New(client.FromEnv)
|
||||
if err != nil {
|
||||
@@ -976,16 +996,15 @@ func startContainers(
|
||||
MaxConcurrency: cfg.Containers.MaxConcurrency,
|
||||
MaxUptime: cfg.Containers.MaxUptime.Std(),
|
||||
PullPolicy: cfg.Containers.PullPolicy,
|
||||
RegistryAuth: resolveRegistryAuth(cfg.Containers.RegistryAuth, secrets, log),
|
||||
Network: cfg.Containers.Network,
|
||||
DockerClient: dockerCli,
|
||||
// Only the embedded broker binds a discovered host gateway. The
|
||||
// standalone State Store uses its configured, container-reachable target.
|
||||
RequireHostGateway: cfg.NATS.Mode == config.NATSModeEmbedded,
|
||||
}, log)
|
||||
}, containerLiveSettings(cfgHolder), log)
|
||||
if err != nil {
|
||||
// A missing image is recoverable by hand. The daemon sends no registry
|
||||
// credentials on pull (internal/container/pool.go), so a private
|
||||
// registry always needs the operator's CLI to fetch it first.
|
||||
// A missing image is recoverable by hand.
|
||||
log.Error("autonomous workflows unavailable: container pool unavailable", "err", err,
|
||||
"hint", "run `docker compose pull agent` (or `build agent`) so the image is present locally")
|
||||
return nil, storage.NewDockerBackend(dockerCli), closeDocker
|
||||
@@ -1001,3 +1020,51 @@ func startContainers(
|
||||
closeDocker()
|
||||
}
|
||||
}
|
||||
|
||||
// containerLiveSettings reads the per-acquire container settings from the
|
||||
// running config. The pool calls it on every acquire, so a stored
|
||||
// container-runtime-policies change reaches the next container without a
|
||||
// restart. Only fields the resource document carries are taken live:
|
||||
// RegistryAuth is file-owned and stays the boot-resolved value, DockerClient
|
||||
// and RequireHostGateway are construction-time, and GracePeriod is not part
|
||||
// of the document.
|
||||
func containerLiveSettings(cfgHolder *config.Holder) func() container.Config {
|
||||
return func() container.Config {
|
||||
cfg := cfgHolder.Get()
|
||||
return container.Config{
|
||||
Image: cfg.Containers.Image,
|
||||
MaxConcurrency: cfg.Containers.MaxConcurrency,
|
||||
MaxUptime: cfg.Containers.MaxUptime.Std(),
|
||||
PullPolicy: cfg.Containers.PullPolicy,
|
||||
Network: cfg.Containers.Network,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// resolveRegistryAuth resolves [containers].registry_auth into the credential
|
||||
// the container pool sends on pull. It returns "" -- an anonymous pull, the
|
||||
// pool's behaviour before the field existed -- whenever the credential is not
|
||||
// configured or cannot be resolved, because a missing registry credential must
|
||||
// not stop autonomous work: the pool only needs it for a private registry. The
|
||||
// warning is what makes the 401 that a private registry will still answer
|
||||
// explicable after a typo or an unset secret. A ref that names only one half is
|
||||
// a config error caught by configuration.Validate, not a missing credential.
|
||||
func resolveRegistryAuth(ref secret.SecretRef, secrets *secret.Registry, log *slog.Logger) string {
|
||||
if ref == (secret.SecretRef{}) {
|
||||
return ""
|
||||
}
|
||||
if secrets == nil {
|
||||
log.Warn("registry credential unavailable; pulling without auth", "reason", "no secret registry", "engine", ref.Engine, "key", ref.Key)
|
||||
return ""
|
||||
}
|
||||
value, err := secrets.Resolve(ref)
|
||||
if err != nil {
|
||||
log.Warn("registry credential unavailable; pulling without auth", "err", err, "engine", ref.Engine, "key", ref.Key)
|
||||
return ""
|
||||
}
|
||||
if strings.TrimSpace(value) == "" {
|
||||
log.Warn("registry credential resolved empty; pulling without auth", "engine", ref.Engine, "key", ref.Key)
|
||||
return ""
|
||||
}
|
||||
return value
|
||||
}
|
||||
@@ -22,14 +22,17 @@ import (
|
||||
"github.com/samcharles93/archie-core/internal/config"
|
||||
"github.com/samcharles93/archie-core/internal/daemon"
|
||||
"github.com/samcharles93/archie-core/internal/domain/messaging"
|
||||
"github.com/samcharles93/archie-core/internal/domain/taskactions"
|
||||
"github.com/samcharles93/archie-core/internal/domain/workflow"
|
||||
"github.com/samcharles93/archie-core/internal/events"
|
||||
"github.com/samcharles93/archie-core/internal/forge"
|
||||
"github.com/samcharles93/archie-core/internal/forgerpc"
|
||||
"github.com/samcharles93/archie-core/internal/gateway"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/postgres/pgstore"
|
||||
taskactionstore "github.com/samcharles93/archie-core/internal/infrastructure/taskactions"
|
||||
"github.com/samcharles93/archie-core/internal/logging"
|
||||
"github.com/samcharles93/archie-core/internal/secret"
|
||||
"github.com/samcharles93/archie-core/internal/taskstate"
|
||||
"github.com/samcharles93/archie-core/internal/taskstate/taskstatetest"
|
||||
"github.com/samcharles93/archie-core/internal/tools"
|
||||
"github.com/samcharles93/archie-core/internal/worktree"
|
||||
@@ -199,6 +202,65 @@ func TestResolveForgeBuildsClientWhenTokenResolves(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// resolveRegistryAuth hands the container pool the resolved
|
||||
// [containers].registry_auth, and degrades to an anonymous pull (with a
|
||||
// warning) whenever there is nothing resolvable to hand it. A deployment with
|
||||
// no private registry must be unaffected by the field existing, and a private
|
||||
// registry whose secret is missing or misnamed must still let every task that
|
||||
// does not need it run -- the pool simply pulls without auth, as it always did,
|
||||
// and the warning explains the 401 that follows.
|
||||
func TestResolveRegistryAuthDegradesRatherThanFailing(t *testing.T) {
|
||||
t.Run("unconfigured is silent", func(t *testing.T) {
|
||||
rec := &recordingHandler{}
|
||||
if got := resolveRegistryAuth(secret.SecretRef{}, secret.NewRegistry(), slog.New(rec)); got != "" {
|
||||
t.Errorf("resolveRegistryAuth(empty) = %q, want empty", got)
|
||||
}
|
||||
if warns := rec.Warnings(); len(warns) != 0 {
|
||||
t.Errorf("unconfigured registry auth logged %v, want nothing", warns)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("resolved credential reaches the pool", func(t *testing.T) {
|
||||
t.Setenv("ARCHIE_TEST_REGISTRY_AUTH", `{"username":"bot","password":"s3cret"}`)
|
||||
rec := &recordingHandler{}
|
||||
got := resolveRegistryAuth(
|
||||
secret.SecretRef{Engine: "env", Key: "ARCHIE_TEST_REGISTRY_AUTH"},
|
||||
secret.NewRegistry(), slog.New(rec))
|
||||
if want := `{"username":"bot","password":"s3cret"}`; got != want {
|
||||
t.Errorf("resolveRegistryAuth = %q, want the resolved secret %q", got, want)
|
||||
}
|
||||
if warns := rec.Warnings(); len(warns) != 0 {
|
||||
t.Errorf("a resolvable credential logged %v, want nothing", warns)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("unresolvable credential degrades with a warning", func(t *testing.T) {
|
||||
rec := &recordingHandler{}
|
||||
got := resolveRegistryAuth(
|
||||
secret.SecretRef{Engine: "env", Key: "ARCHIE_TEST_REGISTRY_AUTH_DEFINITELY_UNSET"},
|
||||
secret.NewRegistry(), slog.New(rec))
|
||||
if got != "" {
|
||||
t.Errorf("resolveRegistryAuth = %q, want empty for an unresolvable credential", got)
|
||||
}
|
||||
warns := rec.Warnings()
|
||||
if len(warns) != 1 || !strings.Contains(warns[0], "registry credential unavailable") {
|
||||
t.Errorf("warnings = %v, want exactly one saying the credential was unavailable", warns)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("no secret registry degrades instead of panicking", func(t *testing.T) {
|
||||
rec := &recordingHandler{}
|
||||
got := resolveRegistryAuth(
|
||||
secret.SecretRef{Engine: "env", Key: "ANYTHING"}, nil, slog.New(rec))
|
||||
if got != "" {
|
||||
t.Errorf("resolveRegistryAuth = %q, want empty with no secret registry", got)
|
||||
}
|
||||
if warns := rec.Warnings(); len(warns) != 1 {
|
||||
t.Errorf("warnings = %v, want exactly one", warns)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestResolveProviderSecretSetsPrivateRuntimeEnvironment(t *testing.T) {
|
||||
registry := secret.NewRegistry()
|
||||
registry.Register(providerSecretEngine{value: "provider-secret"})
|
||||
@@ -431,7 +493,7 @@ func TestConfiguredMCPProviderSupportsAllTransportTypes(t *testing.T) {
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
provider, err := configuredMCPProvider(tt.server, t.TempDir())
|
||||
provider, err := configuredMCPProvider(tt.server, t.TempDir(), nil)
|
||||
if (err != nil) != tt.wantErr {
|
||||
t.Fatalf("configuredMCPProvider() error = %v, wantErr %t", err, tt.wantErr)
|
||||
}
|
||||
@@ -670,11 +732,19 @@ func TestChatTaskControllerAdapterRejectsForgeTask(t *testing.T) {
|
||||
|
||||
func TestChatTaskControllerAdapterTransitions(t *testing.T) {
|
||||
task := &workflow.Task{ID: 42, Source: workflow.SourceChat, Status: workflow.StatusWaitingHuman}
|
||||
var requeueFrom, requeueWorkflow, cancelReason, cancelTo string
|
||||
var approveScope string
|
||||
var approveActor taskactions.Actor
|
||||
var approveRes taskactions.ActionPayload
|
||||
var cancelReason, cancelTo string
|
||||
adapter := chatTaskControllerAdapter{
|
||||
taskByID: func(context.Context, int64) (*workflow.Task, error) { return task, nil },
|
||||
requeue: func(_ context.Context, _ int64, from, workflow string) error {
|
||||
requeueFrom, requeueWorkflow = from, workflow
|
||||
// Chat's approval reaches the daemon's one task-action service, not
|
||||
// its own requeue: this records what the adapter hands it.
|
||||
approve: func(_ context.Context, scope *string, actor taskactions.Actor, _ int64, res taskactions.ActionPayload) error {
|
||||
if scope != nil {
|
||||
approveScope = *scope
|
||||
}
|
||||
approveActor, approveRes = actor, res
|
||||
return nil
|
||||
},
|
||||
cancelExecution: func(_ context.Context, _ int64, reason, to string) ([]int64, error) {
|
||||
@@ -683,11 +753,17 @@ func TestChatTaskControllerAdapterTransitions(t *testing.T) {
|
||||
},
|
||||
}
|
||||
|
||||
if err := adapter.ApproveChatTask(context.Background(), task.ID); err != nil {
|
||||
if err := adapter.ApproveChatTask(context.Background(), task.ID, taskactions.ActorFromScope("archie")); err != nil {
|
||||
t.Fatalf("ApproveChatTask(): %v", err)
|
||||
}
|
||||
if requeueFrom != workflow.StatusWaitingHuman || requeueWorkflow != "implement" {
|
||||
t.Errorf("requeue = %q/%q, want waiting_human/implement", requeueFrom, requeueWorkflow)
|
||||
// The scope is the chat-bound identity, and /approve carries no
|
||||
// instruction or selection syntax, so the review-gate payload is empty
|
||||
// (an absent selection means every offered finding).
|
||||
if approveScope != "archie" || approveActor.Identity != "archie" {
|
||||
t.Errorf("approve scope/actor = %q/%q, want the chat-bound identity", approveScope, approveActor.Identity)
|
||||
}
|
||||
if approveRes.Instructions != "" || len(approveRes.Findings) != 0 {
|
||||
t.Errorf("approve payload = %+v, want the zero review response for chat", approveRes)
|
||||
}
|
||||
if err := adapter.CancelChatTask(context.Background(), task.ID, "cancelled by test"); err != nil {
|
||||
t.Fatalf("CancelChatTask(): %v", err)
|
||||
@@ -834,9 +910,15 @@ func TestChatTaskCommandsEndToEnd(t *testing.T) {
|
||||
chatTaskWriterAdapter{enqueue: st.EnqueueChatTask},
|
||||
profiles,
|
||||
)
|
||||
// Chat's /approve reaches the same task-action service the daemon wires
|
||||
// (docs/prds/pr-review-operator-response.md, Decision 1), over the real
|
||||
// store this test uses.
|
||||
actions := taskactionstore.NewService(taskactionstore.Store{TaskStore: st}, nil, nil, nil, nil, nil, nil, nil)
|
||||
controller := gateway.NewStoreTaskController(chatTaskControllerAdapter{
|
||||
taskByID: st.TaskByID,
|
||||
requeue: st.Requeue,
|
||||
taskByID: st.TaskByID,
|
||||
approve: func(ctx context.Context, scope *string, actor taskactions.Actor, taskID int64, res taskactions.ActionPayload) error {
|
||||
return actions.Apply(ctx, scope, actor, taskID, taskstate.ActionApprove, res)
|
||||
},
|
||||
cancelExecution: st.CancelExecution,
|
||||
})
|
||||
router := gateway.NewRouter(st, nil, "test")
|
||||
@@ -862,6 +944,17 @@ func TestChatTaskCommandsEndToEnd(t *testing.T) {
|
||||
t.Fatalf("spawned task = %+v", task)
|
||||
}
|
||||
|
||||
// Feasibility's deliver stage names implement on the task before it waits
|
||||
// (docs/prds/pr-review-operator-response.md, Decision 1), so the wait the
|
||||
// seed models carries that name; the approval resumes it.
|
||||
waiting, err := st.TaskByID(ctx, taskID)
|
||||
if err != nil || waiting == nil {
|
||||
t.Fatalf("task before wait = (%+v, %v)", waiting, err)
|
||||
}
|
||||
waiting.Workflow = "implement"
|
||||
if err := st.Update(ctx, waiting); err != nil {
|
||||
t.Fatalf("name the resuming workflow: %v", err)
|
||||
}
|
||||
taskstatetest.Seed(t.Context(), t, st, taskID, workflow.StatusQueued, workflow.StatusWaitingHuman, "await approval")
|
||||
task, err = st.TaskByID(ctx, taskID)
|
||||
if err != nil || task == nil || task.Status != workflow.StatusWaitingHuman {
|
||||
|
||||
@@ -0,0 +1,192 @@
|
||||
package archied
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"reflect"
|
||||
"slices"
|
||||
"strings"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/config"
|
||||
"github.com/samcharles93/archie-core/internal/tools/minimax"
|
||||
toolprovider "github.com/samcharles93/archie-core/internal/tools/provider"
|
||||
"github.com/samcharles93/archie-core/internal/tools/webfetch"
|
||||
)
|
||||
|
||||
// appliedMCPServer is one MCP server the running provider registry was built
|
||||
// from: the configuration it was built with and the manifest id it registered
|
||||
// under. A live tool-settings change diffs the stored server set against this
|
||||
// record, so a server that did not change is left running and untouched
|
||||
// (archie-core-zfb0.3).
|
||||
type appliedMCPServer struct {
|
||||
server config.MCPServer
|
||||
id string
|
||||
}
|
||||
|
||||
// buildMCPProvider builds the tool-provider engine for one configured MCP
|
||||
// server. boot.mcpProvider overrides it in tests; production uses
|
||||
// configuredMCPProvider with the running config's work directory and the
|
||||
// daemon's sampling handler. Boot registration and the live reconciliation
|
||||
// both reach the engine through here, so the two cannot build one differently.
|
||||
func (b *boot) buildMCPProvider(srv config.MCPServer) (toolprovider.Engine, error) {
|
||||
if b.mcpProvider != nil {
|
||||
return b.mcpProvider(srv)
|
||||
}
|
||||
return configuredMCPProvider(srv, b.cfgHolder.Get().WorkDir, b.mcpSamplingHandler())
|
||||
}
|
||||
|
||||
// reconcileToolSettings applies a live tool-settings change to the components
|
||||
// built once from that kind: the MCP provider set (diffed, so an unchanged
|
||||
// server keeps running), the web_fetch and minimax tool entries (rebuilt,
|
||||
// because the entry captured the config at construction) and the tool-output
|
||||
// policy (whose spill directory is ensured here; the task snapshot reads the
|
||||
// policy fresh per dispatch and boot.toolLimits is read per call). A nil tool
|
||||
// registry means this process built no tools, so only the config was layered.
|
||||
func (b *boot) reconcileToolSettings(ctx context.Context, cfg config.Config) error {
|
||||
if err := toolLimits(cfg).EnsureSpillDir(); err != nil {
|
||||
b.log.Warn("tool spill directory unavailable; large results will be truncated instead", "err", err)
|
||||
}
|
||||
var problems []error
|
||||
if err := b.reconcileMCPServers(ctx, cfg.Tools.MCPServers); err != nil {
|
||||
problems = append(problems, err)
|
||||
}
|
||||
if b.toolReg != nil {
|
||||
// Unregister first: the boot entry is being replaced, and a second
|
||||
// registration would collide on the tool name. Minimax resolves its
|
||||
// API key again as part of the rebuild, so a rotated credential
|
||||
// applies too.
|
||||
b.toolReg.Unregister(webfetch.ToolName, minimax.ToolName)
|
||||
b.registerWebFetchTool(cfg)
|
||||
b.registerMinimaxTool(cfg, b.log)
|
||||
}
|
||||
return errors.Join(problems...)
|
||||
}
|
||||
|
||||
// reconcileMCPServers brings the running provider set in line with the MCP
|
||||
// servers the layered configuration now names: a server that appeared
|
||||
// connects, one that disappeared disconnects, one whose configuration changed
|
||||
// reconnects, and an unchanged one is left running and untouched. It is
|
||||
// idempotent, so it can run on every stored tool-settings version without
|
||||
// disturbing the servers that did not change.
|
||||
//
|
||||
// A server whose engine cannot be built or started is reported and left out;
|
||||
// the servers already running keep running, the degrade-and-skip rule the boot
|
||||
// registration applies. A changed server whose new engine fails to start is
|
||||
// rolled back to the old one by the registry, so a refused change does not
|
||||
// leave the server down.
|
||||
func (b *boot) reconcileMCPServers(ctx context.Context, servers []config.MCPServer) error {
|
||||
if b.providerRegistry == nil {
|
||||
return nil
|
||||
}
|
||||
b.mcpMu.Lock()
|
||||
defer b.mcpMu.Unlock()
|
||||
|
||||
desired := make(map[string]config.MCPServer, len(servers))
|
||||
for _, srv := range servers {
|
||||
if name := strings.TrimSpace(srv.Name); name != "" {
|
||||
desired[name] = srv
|
||||
}
|
||||
}
|
||||
state := b.mcpRegistryState()
|
||||
var problems []error
|
||||
problems = append(problems, b.disconnectAbsentMCPServers(ctx, desired)...)
|
||||
problems = append(problems, b.applyDesiredMCPServers(ctx, desired, state)...)
|
||||
return errors.Join(problems...)
|
||||
}
|
||||
|
||||
// mcpRegistryState is the read side of the provider registry a diff needs: is
|
||||
// the family running at all, which ids are registered, and which are live.
|
||||
type mcpRegistryState struct {
|
||||
familyRunning bool
|
||||
registered map[string]bool
|
||||
live map[string]bool
|
||||
}
|
||||
|
||||
func (b *boot) mcpRegistryState() mcpRegistryState {
|
||||
registered := make(map[string]bool)
|
||||
for _, id := range b.providerRegistry.RegisteredIDs() {
|
||||
registered[id] = true
|
||||
}
|
||||
live := make(map[string]bool)
|
||||
for _, id := range b.providerRegistry.RunningIDs() {
|
||||
live[id] = true
|
||||
}
|
||||
return mcpRegistryState{
|
||||
familyRunning: b.providerRegistry.Running(),
|
||||
registered: registered,
|
||||
live: live,
|
||||
}
|
||||
}
|
||||
|
||||
// disconnectAbsentMCPServers removes every applied server the stored document
|
||||
// no longer names.
|
||||
func (b *boot) disconnectAbsentMCPServers(ctx context.Context, desired map[string]config.MCPServer) []error {
|
||||
var problems []error
|
||||
for name := range b.mcpApplied {
|
||||
if _, ok := desired[name]; ok {
|
||||
continue
|
||||
}
|
||||
applied := b.mcpApplied[name]
|
||||
if err := b.providerRegistry.Remove(ctx, applied.id); err != nil {
|
||||
problems = append(problems, fmt.Errorf("disconnect MCP server %q: %w", name, err))
|
||||
continue
|
||||
}
|
||||
delete(b.mcpApplied, name)
|
||||
}
|
||||
return problems
|
||||
}
|
||||
|
||||
// applyDesiredMCPServers connects, reconnects or restarts the servers the
|
||||
// stored document names, skipping the ones already running unchanged.
|
||||
func (b *boot) applyDesiredMCPServers(ctx context.Context, desired map[string]config.MCPServer, state mcpRegistryState) []error {
|
||||
names := make([]string, 0, len(desired))
|
||||
for name := range desired {
|
||||
names = append(names, name)
|
||||
}
|
||||
slices.Sort(names)
|
||||
|
||||
var problems []error
|
||||
for _, name := range names {
|
||||
srv := desired[name]
|
||||
if b.mcpServerCurrent(name, srv, state) {
|
||||
continue
|
||||
}
|
||||
engine, err := b.buildMCPProvider(srv)
|
||||
if err != nil {
|
||||
problems = append(problems, fmt.Errorf("MCP server %q: %w", name, err))
|
||||
continue
|
||||
}
|
||||
if err := b.applyMCPProvider(ctx, engine, state); err != nil {
|
||||
problems = append(problems, fmt.Errorf("connect MCP server %q: %w", name, err))
|
||||
continue
|
||||
}
|
||||
b.mcpApplied[name] = appliedMCPServer{server: srv, id: engine.Manifest().ID}
|
||||
}
|
||||
return problems
|
||||
}
|
||||
|
||||
// mcpServerCurrent reports whether the server is already running unchanged, so
|
||||
// a reconciliation can leave it untouched. Present means running while the
|
||||
// family runs, or still registered while it has not started; a
|
||||
// registered-but-not-running server on a running family is a refused earlier
|
||||
// start and is worth another attempt.
|
||||
func (b *boot) mcpServerCurrent(name string, srv config.MCPServer, state mcpRegistryState) bool {
|
||||
applied, ok := b.mcpApplied[name]
|
||||
if !ok || !reflect.DeepEqual(applied.server, srv) {
|
||||
return false
|
||||
}
|
||||
if !state.registered[applied.id] {
|
||||
return false
|
||||
}
|
||||
return !state.familyRunning || state.live[applied.id]
|
||||
}
|
||||
|
||||
// applyMCPProvider replaces a registered provider and adds an absent one,
|
||||
// whichever the registry needs for that id.
|
||||
func (b *boot) applyMCPProvider(ctx context.Context, engine toolprovider.Engine, state mcpRegistryState) error {
|
||||
if state.registered[engine.Manifest().ID] {
|
||||
return b.providerRegistry.Replace(ctx, engine)
|
||||
}
|
||||
return b.providerRegistry.Add(ctx, engine)
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
package archied
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"github.com/samcharles93/ai-sdk/core"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/agentexec"
|
||||
"github.com/samcharles93/archie-core/internal/tools/mcp"
|
||||
)
|
||||
|
||||
// mcpSamplingHandler answers an MCP server's sampling/createMessage request
|
||||
// from the daemon's own chat model, so a server can ask for a completion
|
||||
// without archie opening a second provider path. A missing runtime or model
|
||||
// is a refused request, never a dropped one: the client maps the returned
|
||||
// error to a JSON-RPC error response for that request alone, leaving the
|
||||
// server session intact.
|
||||
func (b *boot) mcpSamplingHandler() mcp.SamplingHandler {
|
||||
return func(ctx context.Context, req mcp.SamplingRequest) (mcp.SamplingResult, error) {
|
||||
llm := b.chatLLM()
|
||||
if llm == nil {
|
||||
return mcp.SamplingResult{}, errors.New("mcp sampling: no model runtime is configured")
|
||||
}
|
||||
if b.chatModels == nil {
|
||||
return mcp.SamplingResult{}, errors.New("mcp sampling: no chat model is configured")
|
||||
}
|
||||
model := b.chatModels.ActiveModel()
|
||||
if model == "" {
|
||||
return mcp.SamplingResult{}, errors.New("mcp sampling: no chat model is configured")
|
||||
}
|
||||
messages, err := agentexec.SamplingMessages(req)
|
||||
if err != nil {
|
||||
return mcp.SamplingResult{}, err
|
||||
}
|
||||
options := core.GenerateOptions{
|
||||
Messages: messages,
|
||||
System: req.SystemPrompt,
|
||||
MaxSteps: 1,
|
||||
MaxTokens: samplingMaxTokens(req, b.chatModels, model),
|
||||
}
|
||||
if req.Temperature != nil {
|
||||
options.Temperature = float32(*req.Temperature)
|
||||
}
|
||||
result, err := llm.Chat(ctx, model, options)
|
||||
// A sampling call is a model call this process made, so it is
|
||||
// recorded for /status like every other one; a nil recorder records
|
||||
// nothing.
|
||||
b.providerOutcomes.record(model, err)
|
||||
if err != nil {
|
||||
return mcp.SamplingResult{}, fmt.Errorf("mcp sampling: %w", err)
|
||||
}
|
||||
return mcp.SamplingResult{
|
||||
Model: model,
|
||||
Role: "assistant",
|
||||
Content: mcp.SamplingContent{Type: "text", Text: result.Text},
|
||||
StopReason: string(result.FinishReason),
|
||||
}, nil
|
||||
}
|
||||
}
|
||||
|
||||
// samplingMaxTokens bounds the completion to the request's own maxTokens,
|
||||
// falling back to a conservative default, and never past the model's own
|
||||
// output ceiling. The bound itself is shared with the agent worker's sampling
|
||||
// path (agentexec.SamplingMaxTokens) so the same server sees the same bound on
|
||||
// either path.
|
||||
func samplingMaxTokens(req mcp.SamplingRequest, models *chatModelManager, model string) int {
|
||||
details, ok := models.ModelDetails(model)
|
||||
return agentexec.SamplingMaxTokens(req.MaxTokens, details.MaxOutputTokens, ok && details.Reasoning)
|
||||
}
|
||||
@@ -0,0 +1,98 @@
|
||||
package archied
|
||||
|
||||
import (
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/agentexec"
|
||||
"github.com/samcharles93/archie-core/internal/tools/mcp"
|
||||
)
|
||||
|
||||
func newSamplingTestRuntime(t *testing.T) *httptest.Server {
|
||||
t.Helper()
|
||||
api := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_, _ = io.WriteString(w, `{"id":"chatcmpl-1","object":"chat.completion","created":1,"model":"gpt-5.6",`+
|
||||
`"choices":[{"index":0,"message":{"role":"assistant","content":"four"},"finish_reason":"stop"}],`+
|
||||
`"usage":{"prompt_tokens":1,"completion_tokens":1,"total_tokens":2}}`)
|
||||
}))
|
||||
t.Cleanup(api.Close)
|
||||
return api
|
||||
}
|
||||
|
||||
// TestMCPSamplingHandlerAnswersFromConfiguredModel proves a server-initiated
|
||||
// sampling request is answered by the daemon's own chat model and recorded on
|
||||
// /status like every other model call this process makes.
|
||||
func TestMCPSamplingHandlerAnswersFromConfiguredModel(t *testing.T) {
|
||||
api := newSamplingTestRuntime(t)
|
||||
|
||||
b := newBootstrap()
|
||||
b.setLLM(agentexec.NewRuntime(map[string]agentexec.Provider{
|
||||
"openai": {Class: "openai", BaseURL: api.URL},
|
||||
}))
|
||||
b.chatModels = newChatModelManager(map[string]string{"chat": "openai/gpt-5.6"})
|
||||
b.providerOutcomes = newProviderOutcomeRecorder()
|
||||
|
||||
result, err := b.mcpSamplingHandler()(t.Context(), mcp.SamplingRequest{
|
||||
Messages: []mcp.SamplingMessage{{
|
||||
Role: "user",
|
||||
Content: mcp.SamplingContent{Type: "text", Text: "2+2?"},
|
||||
}},
|
||||
MaxTokens: 16,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("sampling handler = %v, want a completion", err)
|
||||
}
|
||||
if result.Model != "openai/gpt-5.6" {
|
||||
t.Errorf("result.Model = %q, want the configured chat model", result.Model)
|
||||
}
|
||||
if result.Role != "assistant" || result.Content.Type != "text" || result.Content.Text != "four" {
|
||||
t.Errorf("result = %+v, want an assistant text completion", result)
|
||||
}
|
||||
outcome, attempted := b.providerOutcomes.LastChatModelOutcome()
|
||||
if !attempted || outcome.Model != "openai/gpt-5.6" || outcome.Err != "" {
|
||||
t.Errorf("recorded outcome = %+v (attempted %v), want the sampling call recorded as successful", outcome, attempted)
|
||||
}
|
||||
}
|
||||
|
||||
// TestMCPSamplingHandlerRefusesCleanlyWithNoModel proves a daemon with no
|
||||
// usable model answers the request with an error instead of panicking or
|
||||
// dropping it: the server sees a failed request, not a dead session.
|
||||
func TestMCPSamplingHandlerRefusesCleanlyWithNoModel(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
wired bool
|
||||
want string
|
||||
}{
|
||||
{name: "no model runtime", wired: false, want: "no model runtime"},
|
||||
{name: "no chat model configured", wired: true, want: "no chat model"},
|
||||
}
|
||||
|
||||
for _, tc := range tests {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
b := newBootstrap()
|
||||
if tc.wired {
|
||||
b.setLLM(agentexec.NewRuntime(map[string]agentexec.Provider{
|
||||
"openai": {Class: "openai"},
|
||||
}))
|
||||
}
|
||||
b.chatModels = newChatModelManager(nil)
|
||||
|
||||
_, err := b.mcpSamplingHandler()(t.Context(), mcp.SamplingRequest{
|
||||
Messages: []mcp.SamplingMessage{{
|
||||
Role: "user",
|
||||
Content: mcp.SamplingContent{Type: "text", Text: "hi"},
|
||||
}},
|
||||
})
|
||||
if err == nil {
|
||||
t.Fatal("sampling handler returned no error with no usable model")
|
||||
}
|
||||
if !strings.Contains(err.Error(), tc.want) {
|
||||
t.Errorf("error = %q, want it to mention %q", err, tc.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -22,7 +22,7 @@ func applyModelCatalog(cfg *config.Config, snapshot modelcatalog.Snapshot) []str
|
||||
for _, model := range provider.Models {
|
||||
ref := provider.ID + "/" + model.ID
|
||||
models = append(models, ref)
|
||||
limits[ref] = config.ModelLimits{ContextWindow: model.ContextWindow, MaxOutputTokens: model.MaxOutputTokens}
|
||||
limits[ref] = config.ModelLimits{ContextWindow: model.ContextWindow, MaxOutputTokens: model.MaxOutputTokens, Reasoning: model.Reasoning}
|
||||
}
|
||||
}
|
||||
cfg.ModelLimits = limits
|
||||
|
||||
@@ -0,0 +1,250 @@
|
||||
package archied
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"io/fs"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"slices"
|
||||
"strings"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/agentexec"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/modelcatalog"
|
||||
"github.com/samcharles93/archie-core/internal/secret"
|
||||
"github.com/samcharles93/archie-core/internal/webui"
|
||||
)
|
||||
|
||||
// catalogServer answers the model catalog read with a models.dev-shaped
|
||||
// document. It is the outside dependency the refresh loop actually meets:
|
||||
// modelcatalog.Load fetches this and caches what it read.
|
||||
func catalogServer(t *testing.T, document func() map[string]any) *httptest.Server {
|
||||
t.Helper()
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_ = json.NewEncoder(w).Encode(document())
|
||||
}))
|
||||
t.Cleanup(srv.Close)
|
||||
return srv
|
||||
}
|
||||
|
||||
// catalogProvider is one usable provider entry: an environment variable that
|
||||
// resolves (so modelcatalog keeps it) and the tool-callable models it offers.
|
||||
func catalogProvider(id, env string, models ...string) map[string]any {
|
||||
entries := make(map[string]any, len(models))
|
||||
for _, model := range models {
|
||||
entries[model] = map[string]any{
|
||||
"id": model, "name": strings.ToUpper(model), "tool_call": true,
|
||||
"limit": map[string]any{"context": 128000, "output": 16000},
|
||||
}
|
||||
}
|
||||
return map[string]any{
|
||||
"id": id, "name": strings.ToUpper(id), "npm": "@ai-sdk/openai-compatible",
|
||||
"api": "https://" + id + ".test/v1", "env": []string{env}, "models": entries,
|
||||
}
|
||||
}
|
||||
|
||||
// newCatalogBoot builds the smallest boot the refresh path runs against: the
|
||||
// control plane reads behind runtimeConfig, the secrets registry the catalog
|
||||
// resolves keys through, the chat runtime a refresh must reach, and a catalog
|
||||
// endpoint a test controls.
|
||||
func newCatalogBoot(t *testing.T, url string) *boot {
|
||||
t.Helper()
|
||||
b := newReloadBoot(t, &controlPlaneStub{values: databaseOwnedResources()})
|
||||
b.secrets = secret.NewRegistry()
|
||||
b.chatModels = newChatModelManager(fileConfig().Models)
|
||||
b.setLLM(agentexec.NewRuntime(executionProviders(fileConfig())))
|
||||
b.catalogURL = url
|
||||
b.catalogCachePath = filepath.Join(t.TempDir(), "models.json")
|
||||
if err := b.loadRuntimeConfig(t.Context()); err != nil {
|
||||
t.Fatalf("loadRuntimeConfig: %v", err)
|
||||
}
|
||||
return b
|
||||
}
|
||||
|
||||
func awaitCatalog(t *testing.T, b *boot, ref string) {
|
||||
t.Helper()
|
||||
deadline := time.Now().Add(5 * time.Second)
|
||||
for !slices.Contains(b.chatModels.Models(), ref) {
|
||||
if time.Now().After(deadline) {
|
||||
t.Fatalf("chat models = %v, want %s to appear within the refresh interval", b.chatModels.Models(), ref)
|
||||
}
|
||||
time.Sleep(2 * time.Millisecond)
|
||||
}
|
||||
}
|
||||
|
||||
// TestRefreshModelCatalogRepublishesEveryProjectionSite: a catalog read after
|
||||
// boot must reach everything derived from it -- the running config a workflow
|
||||
// stage resolves model limits from, the dashboard's published catalog view,
|
||||
// and the chat runtime's model list and details. A value that reaches the
|
||||
// boot struct alone parses and does nothing.
|
||||
func TestRefreshModelCatalogRepublishesEveryProjectionSite(t *testing.T) {
|
||||
srv := catalogServer(t, func() map[string]any {
|
||||
return map[string]any{"acme": catalogProvider("acme", "ACME_API_KEY", "acme-tool")}
|
||||
})
|
||||
t.Setenv("ACME_API_KEY", "resolved-by-the-secret-registry")
|
||||
b := newCatalogBoot(t, srv.URL)
|
||||
|
||||
if snapshot, _ := b.catalogState(); len(snapshot.Providers) != 0 {
|
||||
t.Fatalf("boot catalog = %+v, want the empty snapshot this test starts from", snapshot.Providers)
|
||||
}
|
||||
|
||||
if err := b.refreshModelCatalog(t.Context()); err != nil {
|
||||
t.Fatalf("refreshModelCatalog: %v", err)
|
||||
}
|
||||
|
||||
if got := b.cfgHolder.Get().ModelLimits["acme/acme-tool"]; got.ContextWindow != 128000 || got.MaxOutputTokens != 16000 {
|
||||
t.Errorf("running model limits = %+v, want the refreshed catalog's capacity", got)
|
||||
}
|
||||
if _, models := b.catalogState(); !slices.Contains(models, "acme/acme-tool") {
|
||||
t.Errorf("catalog models = %v, want the refreshed reference", models)
|
||||
}
|
||||
|
||||
view := webui.BuildConfigView(b.configViewInput(t.Context()))
|
||||
if len(view.Catalog) != 1 || view.Catalog[0].ID != "acme" {
|
||||
t.Fatalf("published catalog = %+v, want the refreshed provider", view.Catalog)
|
||||
}
|
||||
if !slices.Contains(view.Catalog[0].Models, "acme-tool") {
|
||||
t.Errorf("published catalog models = %v, want the refreshed model", view.Catalog[0].Models)
|
||||
}
|
||||
|
||||
if !slices.Contains(b.chatModels.Models(), "acme/acme-tool") {
|
||||
t.Errorf("chat models = %v, want the refreshed reference offered", b.chatModels.Models())
|
||||
}
|
||||
if _, ok := b.chatModels.ModelDetails("acme/acme-tool"); !ok {
|
||||
t.Error("chat model details missing the refreshed model")
|
||||
}
|
||||
if got := b.chatModels.ProviderDisplayName("acme"); got != "ACME" {
|
||||
t.Errorf("provider display name = %q, want the refreshed catalog's name", got)
|
||||
}
|
||||
}
|
||||
|
||||
// TestModelCatalogRefreshLoopPicksUpANewModelWhileTheDaemonRuns is the
|
||||
// without-a-restart proof: the process is already running on the catalog it
|
||||
// booted with, and a model that appears upstream afterwards is offered by the
|
||||
// next tick.
|
||||
func TestModelCatalogRefreshLoopPicksUpANewModelWhileTheDaemonRuns(t *testing.T) {
|
||||
var calls atomic.Int64
|
||||
srv := catalogServer(t, func() map[string]any {
|
||||
if calls.Add(1) == 1 {
|
||||
return map[string]any{"acme": catalogProvider("acme", "ACME_API_KEY", "acme-one")}
|
||||
}
|
||||
return map[string]any{"acme": catalogProvider("acme", "ACME_API_KEY", "acme-one", "acme-two")}
|
||||
})
|
||||
t.Setenv("ACME_API_KEY", "resolved-by-the-secret-registry")
|
||||
b := newCatalogBoot(t, srv.URL)
|
||||
if err := b.refreshModelCatalog(t.Context()); err != nil {
|
||||
t.Fatalf("initial refresh: %v", err)
|
||||
}
|
||||
if slices.Contains(b.chatModels.Models(), "acme/acme-two") {
|
||||
t.Fatal("the second model was offered before upstream published it")
|
||||
}
|
||||
|
||||
b.catalogRefreshInterval = 5 * time.Millisecond
|
||||
ctx, cancel := context.WithCancel(t.Context())
|
||||
defer cancel()
|
||||
b.startModelCatalogRefresh(ctx)
|
||||
|
||||
awaitCatalog(t, b, "acme/acme-two")
|
||||
if got := b.cfgHolder.Get().ModelLimits["acme/acme-two"]; got.ContextWindow != 128000 {
|
||||
t.Errorf("running model limits for the new model = %+v, want the refreshed capacity", got)
|
||||
}
|
||||
}
|
||||
|
||||
// TestRefreshModelCatalogKeepsTheLoadedCatalogWhenTheReadFails: the catalog
|
||||
// service having a bad minute must not blank the model list the process is
|
||||
// already running on. The failed read is reported; the snapshot stays.
|
||||
func TestRefreshModelCatalogKeepsTheLoadedCatalogWhenTheReadFails(t *testing.T) {
|
||||
srv := catalogServer(t, func() map[string]any {
|
||||
return map[string]any{"acme": catalogProvider("acme", "ACME_API_KEY", "acme-one")}
|
||||
})
|
||||
t.Setenv("ACME_API_KEY", "resolved-by-the-secret-registry")
|
||||
b := newCatalogBoot(t, srv.URL)
|
||||
if err := b.refreshModelCatalog(t.Context()); err != nil {
|
||||
t.Fatalf("refreshModelCatalog: %v", err)
|
||||
}
|
||||
|
||||
// The endpoint stops answering and the cache is gone, so the read has no
|
||||
// fallback left.
|
||||
srv.Close()
|
||||
if err := os.Remove(b.catalogCachePath); err != nil && !errors.Is(err, fs.ErrNotExist) {
|
||||
t.Fatalf("remove cache: %v", err)
|
||||
}
|
||||
if err := b.refreshModelCatalog(t.Context()); err == nil {
|
||||
t.Fatal("refreshModelCatalog error = nil, want the failed read reported")
|
||||
}
|
||||
|
||||
if _, models := b.catalogState(); !slices.Contains(models, "acme/acme-one") {
|
||||
t.Errorf("catalog models = %v, want the loaded catalog kept", models)
|
||||
}
|
||||
if got := b.cfgHolder.Get().ModelLimits["acme/acme-one"]; got.ContextWindow != 128000 {
|
||||
t.Errorf("running model limits = %+v, want the last-known-good catalog", got)
|
||||
}
|
||||
}
|
||||
|
||||
// TestBootCatalogReachesTheRunningConfig is the projection-site check for the
|
||||
// boot half: the catalog boot read must be in the config the process runs on,
|
||||
// not only in the boot struct the chat runtime was built from. Otherwise the
|
||||
// first live update is the first snapshot to carry a catalog model limit.
|
||||
func TestBootCatalogReachesTheRunningConfig(t *testing.T) {
|
||||
srv := catalogServer(t, func() map[string]any {
|
||||
return map[string]any{"acme": catalogProvider("acme", "ACME_API_KEY", "acme-one")}
|
||||
})
|
||||
t.Setenv("ACME_API_KEY", "resolved-by-the-secret-registry")
|
||||
b := newCatalogBoot(t, srv.URL)
|
||||
|
||||
b.loadCatalog(t.Context(), filepath.Join(t.TempDir(), "config.toml"))
|
||||
|
||||
if got := b.cfgHolder.Get().ModelLimits["acme/acme-one"]; got.ContextWindow != 128000 {
|
||||
t.Errorf("running model limits = %+v, want the catalog boot loaded", got)
|
||||
}
|
||||
if _, models := b.catalogState(); !slices.Contains(models, "acme/acme-one") {
|
||||
t.Errorf("catalog models = %v, want the boot catalog", models)
|
||||
}
|
||||
}
|
||||
|
||||
// TestChatModelManagerReplacesItsCatalogRatherThanMerging: a refresh must
|
||||
// drop what the previous snapshot contributed. A manager that only merged
|
||||
// would keep offering a model the catalog no longer publishes, and would
|
||||
// describe a provider that no longer exists.
|
||||
func TestChatModelManagerReplacesItsCatalogRatherThanMerging(t *testing.T) {
|
||||
manager := newChatModelManager(map[string]string{"chat": "acme/one"})
|
||||
manager.SetModelCatalog(
|
||||
modelcatalog.Snapshot{Providers: []modelcatalog.Provider{
|
||||
{ID: "acme", Name: "Acme", Models: []modelcatalog.Model{{ID: "one"}, {ID: "two"}}},
|
||||
}},
|
||||
[]string{"acme/one", "acme/two"},
|
||||
)
|
||||
if !slices.Contains(manager.Models(), "acme/two") {
|
||||
t.Fatalf("chat models = %v, want the catalog's models offered", manager.Models())
|
||||
}
|
||||
if got := manager.ProviderDisplayName("acme"); got != "Acme" {
|
||||
t.Fatalf("provider display name = %q, want the catalog's name", got)
|
||||
}
|
||||
|
||||
manager.SetModelCatalog(
|
||||
modelcatalog.Snapshot{Providers: []modelcatalog.Provider{
|
||||
{ID: "acme", Name: "Acme", Models: []modelcatalog.Model{{ID: "one"}}},
|
||||
}},
|
||||
[]string{"acme/one"},
|
||||
)
|
||||
|
||||
if slices.Contains(manager.Models(), "acme/two") {
|
||||
t.Errorf("chat models = %v, want the withdrawn model dropped", manager.Models())
|
||||
}
|
||||
if _, ok := manager.ModelDetails("acme/two"); ok {
|
||||
t.Error("chat model details still carry the withdrawn model")
|
||||
}
|
||||
if got := manager.ProviderDisplayName("gone"); got != "" {
|
||||
t.Errorf("provider display name for an absent provider = %q, want empty", got)
|
||||
}
|
||||
if !slices.Contains(manager.Models(), "acme/one") {
|
||||
t.Errorf("chat models = %v, want the surviving model kept", manager.Models())
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,163 @@
|
||||
package archied
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"sync"
|
||||
"testing"
|
||||
|
||||
"github.com/samcharles93/ai-sdk/runtime"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/agentexec"
|
||||
"github.com/samcharles93/archie-core/internal/gateway"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/modelcatalog"
|
||||
"github.com/samcharles93/archie-core/internal/tools"
|
||||
"github.com/samcharles93/archie-core/internal/tools/mcp"
|
||||
)
|
||||
|
||||
// capturedChatRequest is the JSON body the OpenAI-compatible provider sent for
|
||||
// the last call, which is where the max-token parameter name is chosen.
|
||||
type capturedChatRequest struct {
|
||||
mu sync.Mutex
|
||||
body map[string]any
|
||||
}
|
||||
|
||||
func (c *capturedChatRequest) get() map[string]any {
|
||||
c.mu.Lock()
|
||||
defer c.mu.Unlock()
|
||||
return c.body
|
||||
}
|
||||
|
||||
// newCapturingChatAPI answers every completion and records the wire body, so a
|
||||
// test can assert the parameter the provider actually emitted rather than an
|
||||
// option the composition root merely set.
|
||||
func newCapturingChatAPI(t *testing.T, capture *capturedChatRequest) *httptest.Server {
|
||||
t.Helper()
|
||||
api := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
var body map[string]any
|
||||
_ = json.NewDecoder(r.Body).Decode(&body)
|
||||
capture.mu.Lock()
|
||||
capture.body = body
|
||||
capture.mu.Unlock()
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_, _ = io.WriteString(w, `{"id":"chatcmpl-1","object":"chat.completion","created":1,"model":"test",`+
|
||||
`"choices":[{"index":0,"message":{"role":"assistant","content":"ok"},"finish_reason":"stop"}],`+
|
||||
`"usage":{"prompt_tokens":1,"completion_tokens":1,"total_tokens":2}}`)
|
||||
}))
|
||||
t.Cleanup(api.Close)
|
||||
return api
|
||||
}
|
||||
|
||||
// assertWireBound asserts the max-token parameter the provider emitted: absent
|
||||
// entirely for a reasoning model (its only accepted form with this ai-sdk
|
||||
// release), present with the caller's bound for a classic one.
|
||||
func assertWireBound(t *testing.T, capture *capturedChatRequest, wantBound bool, wantValue int) {
|
||||
t.Helper()
|
||||
bound, present := capture.get()["max_tokens"]
|
||||
if present != wantBound {
|
||||
t.Fatalf("request body max_tokens present = %v (value %v), want %v; body = %v", present, bound, wantBound, capture.get())
|
||||
}
|
||||
if present && bound != float64(wantValue) {
|
||||
t.Fatalf("request body max_tokens = %v, want %d; body = %v", bound, wantValue, capture.get())
|
||||
}
|
||||
}
|
||||
|
||||
// TestChatTurnParameterNameFollowsModelClass proves the chat turn path reaches
|
||||
// the provider without a max-token bound for a reasoning-class model, while a
|
||||
// classic model still carries one. The bound itself is unchanged; only the
|
||||
// parameter the chat-completions provider would serialise it into differs, and
|
||||
// reasoning models reject `max_tokens` with HTTP 400.
|
||||
func TestChatTurnParameterNameFollowsModelClass(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
reasoning bool
|
||||
wantBound bool
|
||||
modelRef string
|
||||
maxOutput int
|
||||
wantValue int
|
||||
}{
|
||||
{name: "reasoning model", reasoning: true, wantBound: false, modelRef: "openai/gpt-5.4", maxOutput: 4096},
|
||||
{name: "classic model", reasoning: false, wantBound: true, modelRef: "openai/gpt-4o", maxOutput: 4096, wantValue: 4096},
|
||||
}
|
||||
|
||||
for _, tc := range tests {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
capture := &capturedChatRequest{}
|
||||
api := newCapturingChatAPI(t, capture)
|
||||
llm := agentexec.NewRuntime(map[string]agentexec.Provider{
|
||||
"openai": {Class: "openai", BaseURL: api.URL},
|
||||
})
|
||||
model := newChatTurnModel(func() *runtime.Runtime { return llm }, tools.NewRegistry(), 1, func() agentexec.ToolLimits { return agentexec.ToolLimits{} }, nil)
|
||||
|
||||
prepared, err := model.Prepare(t.Context(), gateway.TurnPrepareContext{
|
||||
Model: tc.modelRef, Reasoning: tc.reasoning,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("Prepare() error = %v", err)
|
||||
}
|
||||
_, err = prepared.Generate(t.Context(), gateway.TurnModelRequest{
|
||||
Messages: []gateway.CompressedMessage{{Role: "user", Content: "hi"}},
|
||||
MaxOutputTokens: tc.maxOutput,
|
||||
}, nil)
|
||||
if err != nil {
|
||||
t.Fatalf("Generate() error = %v", err)
|
||||
}
|
||||
|
||||
assertWireBound(t, capture, tc.wantBound, tc.wantValue)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestMCPSamplingParameterNameFollowsModelClass proves the second place the
|
||||
// process sets a max-token bound -- server-requested sampling, which also
|
||||
// applies a default when the request is silent -- preserves it for a classic
|
||||
// model and drops it entirely for a reasoning one.
|
||||
func TestMCPSamplingParameterNameFollowsModelClass(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
modelRef string
|
||||
model modelcatalog.Model
|
||||
wantBound bool
|
||||
wantValue int
|
||||
}{
|
||||
{
|
||||
name: "reasoning model",
|
||||
modelRef: "openai/gpt-5.4",
|
||||
model: modelcatalog.Model{ID: "gpt-5.4", Reasoning: true, MaxOutputTokens: 128000},
|
||||
},
|
||||
{
|
||||
name: "classic model",
|
||||
modelRef: "openai/gpt-4o",
|
||||
model: modelcatalog.Model{ID: "gpt-4o", MaxOutputTokens: 128000},
|
||||
wantBound: true,
|
||||
wantValue: agentexec.DefaultSamplingMaxTokens,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range tests {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
capture := &capturedChatRequest{}
|
||||
api := newCapturingChatAPI(t, capture)
|
||||
b := newBootstrap()
|
||||
b.setLLM(agentexec.NewRuntime(map[string]agentexec.Provider{
|
||||
"openai": {Class: "openai", BaseURL: api.URL},
|
||||
}))
|
||||
b.chatModels = newChatModelManager(map[string]string{"chat": tc.modelRef})
|
||||
b.chatModels.SetModelCatalog(modelcatalog.Snapshot{
|
||||
Providers: []modelcatalog.Provider{{ID: "openai", Models: []modelcatalog.Model{tc.model}}},
|
||||
}, []string{tc.modelRef})
|
||||
b.providerOutcomes = newProviderOutcomeRecorder()
|
||||
|
||||
_, err := b.mcpSamplingHandler()(t.Context(), mcp.SamplingRequest{
|
||||
Messages: []mcp.SamplingMessage{{Role: "user", Content: mcp.SamplingContent{Type: "text", Text: "hi"}}},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("sampling handler error = %v", err)
|
||||
}
|
||||
|
||||
assertWireBound(t, capture, tc.wantBound, tc.wantValue)
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,306 @@
|
||||
package archied
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/fs"
|
||||
"log/slog"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"slices"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/config"
|
||||
"github.com/samcharles93/archie-core/internal/domain/eda/module"
|
||||
"github.com/samcharles93/archie-core/internal/plugin"
|
||||
"github.com/samcharles93/archie-core/internal/plugin/pluginextract"
|
||||
"github.com/samcharles93/archie-core/internal/secret"
|
||||
)
|
||||
|
||||
// The three directory families a plugin-settings document names and a
|
||||
// reconciliation reads.
|
||||
const (
|
||||
reconcilePlugins = "plugin"
|
||||
reconcileModules = "module"
|
||||
reconcileSecrets = "secret"
|
||||
)
|
||||
|
||||
// pluginReconciler loads plugin, module and secret-engine files that appear or
|
||||
// change in the directories the running configuration names, so an operator can
|
||||
// drop a file beside a running daemon and have it take effect without a
|
||||
// restart. Yaegi cannot unload an interpreter, so a removed file's code keeps
|
||||
// running and the reconciler reports it through apply status until the process
|
||||
// restarts (docs/prds/plugin-settings-live.md).
|
||||
//
|
||||
// Files are content-hashed, so a tick loads only what appeared or changed: the
|
||||
// boot load's files are seeded as already loaded. A file whose load fails is
|
||||
// forgotten until its content changes, the same degrade-and-skip rule the boot
|
||||
// loaders apply.
|
||||
type pluginReconciler struct {
|
||||
log *slog.Logger
|
||||
pluginReconcileTargets
|
||||
|
||||
// reconcileMu serializes reconciliations: the poller's tick and a stored
|
||||
// plugin-settings update both run one, and they share the loaded set.
|
||||
reconcileMu sync.Mutex
|
||||
loaded map[string]loadedPluginFile
|
||||
}
|
||||
|
||||
type pluginReconcileTargets struct {
|
||||
host *plugin.Host
|
||||
secrets *secret.Registry
|
||||
modules *module.ModuleRegistry
|
||||
// dirs returns the running configuration, the source of the three
|
||||
// directories. It is read each tick so a changed directory retargets the
|
||||
// next reconciliation without a restart.
|
||||
dirs func() config.Config
|
||||
// relayer re-runs the configuration layering after a secret engine loads
|
||||
// or changes, which re-resolves provider credentials against the new
|
||||
// engine. Nil in tests that do not layer.
|
||||
relayer func(ctx context.Context) error
|
||||
// report publishes the reconciliation outcome, including a nil error on a
|
||||
// clean pass so an outstanding removal clears.
|
||||
report func(ctx context.Context, err error)
|
||||
}
|
||||
|
||||
type loadedPluginFile struct {
|
||||
hash string
|
||||
category string
|
||||
// failed marks a file whose last load failed. It is not reported as a
|
||||
// removal while it is still present, and a content change retries it.
|
||||
failed bool
|
||||
}
|
||||
|
||||
func newPluginReconciler(log *slog.Logger, targets pluginReconcileTargets) *pluginReconciler {
|
||||
if log == nil {
|
||||
log = slog.New(slog.DiscardHandler)
|
||||
}
|
||||
return &pluginReconciler{
|
||||
log: log,
|
||||
pluginReconcileTargets: targets,
|
||||
loaded: make(map[string]loadedPluginFile),
|
||||
}
|
||||
}
|
||||
|
||||
// seed records the files the boot load already evaluated, so the first tick
|
||||
// loads only what appeared or changed since boot. The daemon calls it after
|
||||
// loadPlugins, loadWorkflows and configuredSecretRegistry have run.
|
||||
func (r *pluginReconciler) seed() {
|
||||
next := make(map[string]loadedPluginFile)
|
||||
for _, spec := range r.categories(r.dirs()) {
|
||||
paths, err := goFiles(spec.dir)
|
||||
if err != nil {
|
||||
r.log.Warn("plugin directory not seeded", "category", spec.category, "dir", spec.dir, "err", err)
|
||||
continue
|
||||
}
|
||||
for _, path := range paths {
|
||||
hash, err := fileHash(path)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
next[path] = loadedPluginFile{hash: hash, category: spec.category}
|
||||
}
|
||||
}
|
||||
r.reconcileMu.Lock()
|
||||
r.loaded = next
|
||||
r.reconcileMu.Unlock()
|
||||
}
|
||||
|
||||
// run reconciles on interval until ctx ends. The interval is the apply-status
|
||||
// restamp constant, so a dropped file needs no filesystem watch and a
|
||||
// reconciliation report is refreshed at the same cadence as every other apply
|
||||
// status record.
|
||||
func (r *pluginReconciler) run(ctx context.Context, interval time.Duration) {
|
||||
ticker := time.NewTicker(interval)
|
||||
defer ticker.Stop()
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return
|
||||
case <-ticker.C:
|
||||
_ = r.reconcile(ctx)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
type reconcileCategory struct {
|
||||
category string
|
||||
dir string
|
||||
}
|
||||
|
||||
func (r *pluginReconciler) categories(cfg config.Config) []reconcileCategory {
|
||||
return []reconcileCategory{
|
||||
{reconcilePlugins, cfg.PluginDir},
|
||||
{reconcileModules, cfg.ModuleDir},
|
||||
{reconcileSecrets, cfg.SecretEngineDir},
|
||||
}
|
||||
}
|
||||
|
||||
// reconcile re-reads the three directories against the running config and
|
||||
// loads every new or changed file. It returns the outstanding problems -- a
|
||||
// removal, a failed load, an unreadable directory -- joined, and reports the
|
||||
// same through apply status. A nil return is a clean pass and clears the
|
||||
// record's error.
|
||||
func (r *pluginReconciler) reconcile(ctx context.Context) error {
|
||||
r.reconcileMu.Lock()
|
||||
defer r.reconcileMu.Unlock()
|
||||
|
||||
var problems []error
|
||||
next := make(map[string]loadedPluginFile, len(r.loaded))
|
||||
|
||||
for _, spec := range r.categories(r.dirs()) {
|
||||
problems = append(problems, r.reconcileCategory(ctx, spec, next)...)
|
||||
}
|
||||
|
||||
for path, file := range r.loaded {
|
||||
if _, ok := next[path]; ok || file.failed {
|
||||
continue
|
||||
}
|
||||
problems = append(problems, fmt.Errorf("%s file %s was removed; it keeps running until the process restarts", file.category, path))
|
||||
}
|
||||
|
||||
r.loaded = next
|
||||
|
||||
// The layering re-runs every tick, not only when a secret engine changed:
|
||||
// it re-resolves provider credentials, so a provider disabled at boot for
|
||||
// an unresolvable engine is re-resolved on the first tick that finds it,
|
||||
// and a changed directory retargets the next tick without a restart
|
||||
// (docs/prds/plugin-settings-live.md, "Triggers").
|
||||
if r.relayer != nil {
|
||||
if err := r.relayer(ctx); err != nil {
|
||||
problems = append(problems, fmt.Errorf("re-resolve provider credentials: %w", err))
|
||||
}
|
||||
}
|
||||
|
||||
err := errors.Join(problems...)
|
||||
if r.report != nil {
|
||||
r.report(ctx, err)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
// reconcileCategory loads the new and changed files of one directory into
|
||||
// next, returning the problems it met. A directory that became unreadable
|
||||
// keeps the entries this category already had: it is not a removal of the
|
||||
// files loaded before that.
|
||||
func (r *pluginReconciler) reconcileCategory(ctx context.Context, spec reconcileCategory, next map[string]loadedPluginFile) []error {
|
||||
var problems []error
|
||||
paths, err := goFiles(spec.dir)
|
||||
if err != nil {
|
||||
problems = append(problems, fmt.Errorf("%s directory %q: %w", spec.category, spec.dir, err))
|
||||
for path, file := range r.loaded {
|
||||
if file.category == spec.category {
|
||||
next[path] = file
|
||||
}
|
||||
}
|
||||
return problems
|
||||
}
|
||||
for _, path := range paths {
|
||||
hash, err := fileHash(path)
|
||||
if err != nil {
|
||||
problems = append(problems, fmt.Errorf("read %s: %w", path, err))
|
||||
continue
|
||||
}
|
||||
if previous, ok := r.loaded[path]; ok && previous.hash == hash {
|
||||
next[path] = previous
|
||||
continue
|
||||
}
|
||||
if err := r.load(ctx, spec.category, path); err != nil {
|
||||
problems = append(problems, fmt.Errorf("%s %s: %w", spec.category, path, err))
|
||||
// Record the failed content so the same broken file is not retried
|
||||
// every tick; changing it retries the load.
|
||||
next[path] = loadedPluginFile{hash: hash, category: spec.category, failed: true}
|
||||
continue
|
||||
}
|
||||
next[path] = loadedPluginFile{hash: hash, category: spec.category}
|
||||
r.log.Info("runtime directory file loaded", "category", spec.category, "path", path)
|
||||
}
|
||||
return problems
|
||||
}
|
||||
|
||||
func (r *pluginReconciler) load(ctx context.Context, category, path string) error {
|
||||
switch category {
|
||||
case reconcilePlugins:
|
||||
return r.loadPlugin(ctx, path)
|
||||
case reconcileModules:
|
||||
return r.loadModule(path)
|
||||
case reconcileSecrets:
|
||||
if r.secrets == nil {
|
||||
return errors.New("no secret registry to load into")
|
||||
}
|
||||
return r.secrets.LoadFile(path, secretEngineSymbols...)
|
||||
default:
|
||||
return fmt.Errorf("unknown reconcile category %q", category)
|
||||
}
|
||||
}
|
||||
|
||||
func (r *pluginReconciler) loadPlugin(ctx context.Context, path string) error {
|
||||
if r.host == nil {
|
||||
return errors.New("no plugin host to load into")
|
||||
}
|
||||
loaded, err := plugin.LoadFile(path, pluginextract.Symbols)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
module, err := plugin.AdaptLegacy(loaded)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// The manifest id is the plugin's name, so an edited file that keeps its
|
||||
// name replaces the running module and a new name adds one.
|
||||
if r.host.Has(module.Manifest().ID) {
|
||||
return r.host.Replace(ctx, module)
|
||||
}
|
||||
return r.host.Add(ctx, module)
|
||||
}
|
||||
|
||||
func (r *pluginReconciler) loadModule(path string) error {
|
||||
if r.modules == nil {
|
||||
return errors.New("no module registry to load into")
|
||||
}
|
||||
kind := strings.TrimSuffix(filepath.Base(path), ".go")
|
||||
if !slices.Contains(module.Kinds(), kind) {
|
||||
// A .go file for a kind this build has no contract for; the boot load
|
||||
// iterates the known kinds and ignores it too.
|
||||
return nil
|
||||
}
|
||||
return r.modules.Register(kind, filepath.Dir(path))
|
||||
}
|
||||
|
||||
// goFiles lists the .go files directly under dir, sorted. A missing directory
|
||||
// reconciles to an empty set, the convention every boot loader already follows.
|
||||
func goFiles(dir string) ([]string, error) {
|
||||
if dir == "" {
|
||||
return nil, nil
|
||||
}
|
||||
entries, err := os.ReadDir(dir)
|
||||
if errors.Is(err, fs.ErrNotExist) {
|
||||
return nil, nil
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
paths := make([]string, 0, len(entries))
|
||||
for _, entry := range entries {
|
||||
if entry.IsDir() || !strings.HasSuffix(entry.Name(), ".go") {
|
||||
continue
|
||||
}
|
||||
paths = append(paths, filepath.Join(dir, entry.Name()))
|
||||
}
|
||||
slices.Sort(paths)
|
||||
return paths, nil
|
||||
}
|
||||
|
||||
func fileHash(path string) (string, error) {
|
||||
content, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
sum := sha256.Sum256(content)
|
||||
return hex.EncodeToString(sum[:]), nil
|
||||
}
|
||||
@@ -0,0 +1,297 @@
|
||||
package archied
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log/slog"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/app/controlplane"
|
||||
"github.com/samcharles93/archie-core/internal/config"
|
||||
"github.com/samcharles93/archie-core/internal/domain/eda/module"
|
||||
"github.com/samcharles93/archie-core/internal/plugin"
|
||||
"github.com/samcharles93/archie-core/internal/secret"
|
||||
)
|
||||
|
||||
// docs/prds/plugin-settings-live.md: a plugin or secret-engine file dropped
|
||||
// into the running configuration's directory loads without a restart, an
|
||||
// edited file replaces what runs, and a removed file keeps running with the
|
||||
// removal reported through apply status until the process restarts.
|
||||
|
||||
const reconcilerPluginSource = `package main
|
||||
|
||||
import "github.com/samcharles93/archie-core/internal/plugin"
|
||||
|
||||
var Plugin = plugin._Plugin{
|
||||
WName: func() string { return "hello" },
|
||||
WVersion: func() string { return "1.0.0" },
|
||||
}
|
||||
`
|
||||
|
||||
const reconcilerSecretSource = `package main
|
||||
|
||||
import "github.com/samcharles93/archie-core/internal/secret"
|
||||
|
||||
var Engine = secret._Engine{
|
||||
WName: func() string { return "hello" },
|
||||
WVersion: func() string { return "1.0.0" },
|
||||
WResolve: func(key string) (string, error) { return "one:" + key, nil },
|
||||
}
|
||||
`
|
||||
|
||||
const reconcilerEditedSecretSource = `package main
|
||||
|
||||
import "github.com/samcharles93/archie-core/internal/secret"
|
||||
|
||||
var Engine = secret._Engine{
|
||||
WName: func() string { return "hello" },
|
||||
WVersion: func() string { return "2.0.0" },
|
||||
WResolve: func(key string) (string, error) { return "two:" + key, nil },
|
||||
}
|
||||
`
|
||||
|
||||
// reconcilerHarness is one running directory set: a started plugin host, a
|
||||
// fresh secret registry, an empty module registry, and the config that names
|
||||
// the two directories a test writes into.
|
||||
type reconcilerHarness struct {
|
||||
root *plugin.Host
|
||||
secrets *secret.Registry
|
||||
cfg config.Config
|
||||
plugins string
|
||||
secretsD string
|
||||
mods *module.ModuleRegistry
|
||||
}
|
||||
|
||||
func newReconcilerHarness(t *testing.T) *reconcilerHarness {
|
||||
t.Helper()
|
||||
h := &reconcilerHarness{
|
||||
root: plugin.NewHost(),
|
||||
secrets: secret.NewRegistry(),
|
||||
mods: module.New(),
|
||||
plugins: t.TempDir(),
|
||||
secretsD: t.TempDir(),
|
||||
}
|
||||
if err := h.root.Start(t.Context()); err != nil {
|
||||
t.Fatalf("start host: %v", err)
|
||||
}
|
||||
h.cfg = config.Config{PluginDir: h.plugins, SecretEngineDir: h.secretsD}
|
||||
return h
|
||||
}
|
||||
|
||||
func (h *reconcilerHarness) reconciler() *pluginReconciler {
|
||||
return newPluginReconciler(slog.New(slog.DiscardHandler), pluginReconcileTargets{
|
||||
host: h.root,
|
||||
secrets: h.secrets,
|
||||
modules: h.mods,
|
||||
dirs: func() config.Config { return h.cfg },
|
||||
})
|
||||
}
|
||||
|
||||
func writeReconcilerFile(t *testing.T, path, source string) {
|
||||
t.Helper()
|
||||
if err := os.WriteFile(path, []byte(source), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPluginReconcilerLoadsADroppedPluginAndSecretEngine(t *testing.T) {
|
||||
h := newReconcilerHarness(t)
|
||||
r := h.reconciler()
|
||||
// Boot loaded the (empty) directories; seed is what the daemon does after
|
||||
// its own boot load so the first tick does not re-load the boot set.
|
||||
r.seed()
|
||||
|
||||
writeReconcilerFile(t, filepath.Join(h.plugins, "hello.go"), reconcilerPluginSource)
|
||||
writeReconcilerFile(t, filepath.Join(h.secretsD, "hello.go"), reconcilerSecretSource)
|
||||
|
||||
if err := r.reconcile(t.Context()); err != nil {
|
||||
t.Fatalf("reconcile: %v", err)
|
||||
}
|
||||
if !h.root.Has("hello") {
|
||||
t.Error("dropped plugin was not loaded in place")
|
||||
}
|
||||
engine, ok := h.secrets.Get("hello")
|
||||
if !ok {
|
||||
t.Fatal("dropped secret engine was not loaded in place")
|
||||
}
|
||||
value, err := engine.Resolve("k")
|
||||
if err != nil {
|
||||
t.Fatalf("Resolve: %v", err)
|
||||
}
|
||||
if value != "one:k" {
|
||||
t.Errorf("Resolve() = %q, want one:k", value)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPluginReconcilerReplacesAnEditedSecretEngine(t *testing.T) {
|
||||
h := newReconcilerHarness(t)
|
||||
r := h.reconciler()
|
||||
r.seed()
|
||||
|
||||
path := filepath.Join(h.secretsD, "hello.go")
|
||||
writeReconcilerFile(t, path, reconcilerSecretSource)
|
||||
if err := r.reconcile(t.Context()); err != nil {
|
||||
t.Fatalf("first reconcile: %v", err)
|
||||
}
|
||||
|
||||
writeReconcilerFile(t, path, reconcilerEditedSecretSource)
|
||||
if err := r.reconcile(t.Context()); err != nil {
|
||||
t.Fatalf("second reconcile: %v", err)
|
||||
}
|
||||
engine, ok := h.secrets.Get("hello")
|
||||
if !ok {
|
||||
t.Fatal("edited secret engine disappeared")
|
||||
}
|
||||
value, err := engine.Resolve("k")
|
||||
if err != nil {
|
||||
t.Fatalf("Resolve: %v", err)
|
||||
}
|
||||
if value != "two:k" {
|
||||
t.Errorf("Resolve() after edit = %q, want the replaced two:k", value)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPluginReconcilerReportsARemovedFileThatKeepsRunning(t *testing.T) {
|
||||
h := newReconcilerHarness(t)
|
||||
r := h.reconciler()
|
||||
r.seed()
|
||||
|
||||
pluginPath := filepath.Join(h.plugins, "hello.go")
|
||||
secretPath := filepath.Join(h.secretsD, "hello.go")
|
||||
writeReconcilerFile(t, pluginPath, reconcilerPluginSource)
|
||||
writeReconcilerFile(t, secretPath, reconcilerSecretSource)
|
||||
if err := r.reconcile(t.Context()); err != nil {
|
||||
t.Fatalf("first reconcile: %v", err)
|
||||
}
|
||||
|
||||
if err := os.Remove(pluginPath); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.Remove(secretPath); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
err := r.reconcile(t.Context())
|
||||
if err == nil {
|
||||
t.Fatal("a reconcile after two removals reported no problem")
|
||||
}
|
||||
for _, want := range []string{"hello.go", "removed", "restart"} {
|
||||
if !strings.Contains(err.Error(), want) {
|
||||
t.Errorf("removal report %q does not mention %q", err, want)
|
||||
}
|
||||
}
|
||||
// The running code is not unloaded: Yaegi has no unload path, so both the
|
||||
// module and the engine keep serving until the process restarts.
|
||||
if !h.root.Has("hello") {
|
||||
t.Error("a removed plugin stopped running")
|
||||
}
|
||||
if _, ok := h.secrets.Get("hello"); !ok {
|
||||
t.Error("a removed secret engine was unregistered")
|
||||
}
|
||||
|
||||
// Re-adding the file clears the removal: the replacement takes over and no
|
||||
// old interpreter is left serving the path.
|
||||
writeReconcilerFile(t, pluginPath, reconcilerPluginSource)
|
||||
writeReconcilerFile(t, secretPath, reconcilerSecretSource)
|
||||
if err := r.reconcile(t.Context()); err != nil {
|
||||
t.Fatalf("reconcile after re-adding the files: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPluginSettingsLiveUpdateLoadsTheNewDirectory(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
writeReconcilerFile(t, filepath.Join(dir, "hello.go"), reconcilerPluginSource)
|
||||
|
||||
running := fileConfig()
|
||||
b, status := newLiveApplyBoot(t, running)
|
||||
resources := databaseOwnedResources()
|
||||
resources[controlplane.PluginSettingsKind] = map[string]any{
|
||||
"plugin_dir": dir, "module_dir": "/db/modules", "secret_engine_dir": "/db/secrets", "skills_dir": "",
|
||||
}
|
||||
b.controlPlane = controlplane.NewRPCClient(&controlPlaneStub{values: resources})
|
||||
b.secrets = secret.NewRegistry()
|
||||
host := plugin.NewHost()
|
||||
if err := host.Start(t.Context()); err != nil {
|
||||
t.Fatalf("start host: %v", err)
|
||||
}
|
||||
b.pluginReconciler = newPluginReconciler(b.log, pluginReconcileTargets{
|
||||
host: host,
|
||||
secrets: b.secrets,
|
||||
modules: module.New(),
|
||||
dirs: func() config.Config { return b.cfgHolder.Get() },
|
||||
})
|
||||
b.pluginReconciler.seed()
|
||||
|
||||
b.applyRuntimeResourceUpdate(t.Context(), controlplane.PluginSettingsKind, controlplane.AppliedResource{Version: 2})
|
||||
|
||||
if got := b.cfgHolder.Get().PluginDir; got != dir {
|
||||
t.Fatalf("running plugin dir = %q, want the stored %q", got, dir)
|
||||
}
|
||||
if !host.Has("hello") {
|
||||
t.Fatal("a stored plugin_dir change did not load its plugin without a restart")
|
||||
}
|
||||
last, ok := status.last(controlplane.PluginSettingsKind)
|
||||
if !ok {
|
||||
t.Fatal("the live plugin-settings update reported no apply status")
|
||||
}
|
||||
if last.Error != "" {
|
||||
t.Errorf("apply status error = %q, want none for a live directory load", last.Error)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPluginSettingsSkillsDirChangeIsRefusedOnTheLivePath(t *testing.T) {
|
||||
running := fileConfig()
|
||||
b, status := newLiveApplyBoot(t, running)
|
||||
// The stored document's skills_dir (/db/skills) differs from the running
|
||||
// empty value, and skills_dir has no live consumer.
|
||||
b.controlPlane = controlplane.NewRPCClient(&controlPlaneStub{values: databaseOwnedResources()})
|
||||
|
||||
b.applyRuntimeResourceUpdate(t.Context(), controlplane.PluginSettingsKind, controlplane.AppliedResource{Version: 2})
|
||||
|
||||
if got := b.cfgHolder.Get().PluginDir; got != fileConfig().PluginDir {
|
||||
t.Errorf("a refused plugin-settings document published plugin_dir %q, want the file's %q", got, fileConfig().PluginDir)
|
||||
}
|
||||
last, ok := status.last(controlplane.PluginSettingsKind)
|
||||
if !ok {
|
||||
t.Fatal("the refused plugin-settings update reported no apply status")
|
||||
}
|
||||
if !strings.Contains(last.Error, "skills_dir") {
|
||||
t.Errorf("apply status error = %q, want it to name skills_dir", last.Error)
|
||||
}
|
||||
if last.AppliedVersion != 2 {
|
||||
t.Errorf("apply status version = %d, want the refused version 2", last.AppliedVersion)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPluginReconcilerReportsTheApplyStatusTruth(t *testing.T) {
|
||||
h := newReconcilerHarness(t)
|
||||
var reported []error
|
||||
reported = nil
|
||||
r := newPluginReconciler(slog.New(slog.DiscardHandler), pluginReconcileTargets{
|
||||
host: h.root,
|
||||
secrets: h.secrets,
|
||||
modules: h.mods,
|
||||
dirs: func() config.Config { return h.cfg },
|
||||
report: func(_ context.Context, err error) { reported = append(reported, err) },
|
||||
})
|
||||
r.seed()
|
||||
|
||||
writeReconcilerFile(t, filepath.Join(h.secretsD, "hello.go"), reconcilerSecretSource)
|
||||
if err := r.reconcile(t.Context()); err != nil {
|
||||
t.Fatalf("clean reconcile: %v", err)
|
||||
}
|
||||
if len(reported) != 1 || reported[0] != nil {
|
||||
t.Fatalf("clean reconcile reported %v, want one nil (a live add is applied)", reported)
|
||||
}
|
||||
|
||||
if err := os.Remove(filepath.Join(h.secretsD, "hello.go")); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := r.reconcile(t.Context()); err == nil {
|
||||
t.Fatal("removal reconcile reported no error")
|
||||
}
|
||||
if len(reported) != 2 || reported[1] == nil {
|
||||
t.Fatalf("removal reconcile reported %v, want the removal as the second record", reported)
|
||||
}
|
||||
}
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"os"
|
||||
"reflect"
|
||||
"strings"
|
||||
|
||||
"github.com/traefik/yaegi/stdlib/unrestricted"
|
||||
@@ -15,6 +16,15 @@ import (
|
||||
"github.com/samcharles93/archie-core/internal/secret/secretextract"
|
||||
)
|
||||
|
||||
// secretEngineSymbols is the Yaegi symbol set every secret-engine load uses:
|
||||
// the generated interface bridge, the host-side CLI helper, and unrestricted
|
||||
// symbols for parity with skillscript interpretation. The boot load and a
|
||||
// live directory reconciliation share it so an engine always loads the same
|
||||
// way.
|
||||
var secretEngineSymbols = []map[string]map[string]reflect.Value{
|
||||
secretextract.Symbols, enginehost.Symbols, unrestricted.Symbols,
|
||||
}
|
||||
|
||||
func configuredSecretRegistry(cfg *config.Config, log *slog.Logger) (*secret.Registry, error) {
|
||||
registry := secret.NewRegistry()
|
||||
if cfg.SecretEngineDir != "" {
|
||||
@@ -24,7 +34,7 @@ func configuredSecretRegistry(cfg *config.Config, log *slog.Logger) (*secret.Reg
|
||||
// host-side helper) because yaegi's interpreted os/exec cannot
|
||||
// pass an environment to child processes; unrestricted symbols
|
||||
// are included for parity with skillscript interpretation.
|
||||
loaded, err := registry.LoadDir(cfg.SecretEngineDir, secretextract.Symbols, enginehost.Symbols, unrestricted.Symbols)
|
||||
loaded, err := registry.LoadDir(cfg.SecretEngineDir, secretEngineSymbols...)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("load secret engines from %q: %w", cfg.SecretEngineDir, err)
|
||||
}
|
||||
|
||||
@@ -135,11 +135,13 @@ var reloadableFields = map[string]bool{
|
||||
// TestReloadableFieldsCoverForTaskSnapshot.
|
||||
"Label": true,
|
||||
"BotUser": true,
|
||||
// Worked example of the every-consumer criterion above: the sole
|
||||
// consumer is the webui maxRetriesFor handler (api_tasks.go:380,383),
|
||||
// which re-reads per request; the daemon never reads MaxRetries. The
|
||||
// per-task half of this list is pinned mechanically; this webui-only
|
||||
// entry is pinned by TestChangedNonReloadableFields.
|
||||
// MaxRetries has two consumers, both re-reading it: the webui
|
||||
// maxRetriesFor handler (api_tasks.go:380,383) reads it per request, and
|
||||
// ForTask carries it into every dispatched TaskConfig, where the
|
||||
// container's remediation round cap reads it (StageRemediationRoundCap).
|
||||
// The per-task half of this list is pinned mechanically by
|
||||
// TestReloadableFieldsCoverForTaskSnapshot; the webui-only entry is pinned
|
||||
// by TestChangedNonReloadableFields.
|
||||
"MaxRetries": true,
|
||||
// Tools.Policy (MaxResultChars/SpillDir) is carried into TaskConfig by
|
||||
// ForTask (config.go) and applied fresh per dispatch via
|
||||
@@ -156,17 +158,21 @@ var reloadableFields = map[string]bool{
|
||||
// "Selection": a Kit profile applies without a restart. Credentials is the
|
||||
// same shape one layer down: kitrun.Launcher reads Config.Get().Containers.
|
||||
// Credentials fresh on every Launch (CredentialBindingsKind), never a slice
|
||||
// captured once when the launcher was built. 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
|
||||
// captured once when the launcher was built. Image, MaxConcurrency, MaxUptime,
|
||||
// PullPolicy and Network are re-read per acquire by the container pool and
|
||||
// MaxConcurrency also resizes the running dispatcher on publish
|
||||
// (archie-core-zfb0.2), so a reload applies them without a restart.
|
||||
// RegistryAuth stays frozen in the startup-built pool: it is file-owned and
|
||||
// never part of the container-runtime-policies document, so a changed value
|
||||
// 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, "Profiles": true, "Credentials": true},
|
||||
"Forge": {"Host": true},
|
||||
"Containers": {
|
||||
"VolumeTTL": true, "Profiles": true, "Credentials": true,
|
||||
"Image": true, "MaxConcurrency": true, "MaxUptime": true, "PullPolicy": true, "Network": true,
|
||||
},
|
||||
"Forge": {"Host": true},
|
||||
// Policy is carried into TaskConfig by ForTask (config.go); MCPServers,
|
||||
// WebFetch and Minimax are not and stay requires-restart.
|
||||
"Tools": {"Policy": true},
|
||||
|
||||
@@ -198,28 +198,31 @@ func TestChangedNonReloadableFields(t *testing.T) {
|
||||
t.Fatalf("reloadable-only: got %v, want []", got)
|
||||
}
|
||||
|
||||
// Containers sub-field granularity: Image change warns, MaxConcurrency
|
||||
// and VolumeTTL changes do not.
|
||||
// Containers sub-field granularity: the fields the pool re-reads per
|
||||
// acquire, and the concurrency cap the dispatcher resizes on, are
|
||||
// reloadable; RegistryAuth stays frozen in the startup-built pool.
|
||||
c1 := base
|
||||
c1.Containers = config.ContainerConfig{Image: "img1", MaxConcurrency: 2, VolumeTTL: config.Duration(24 * time.Hour)}
|
||||
c2 := c1
|
||||
c2.Containers.Image = "img2"
|
||||
if got := changedNonReloadableFields(c1, c2); len(got) != 1 || got[0] != "Containers.Image" {
|
||||
t.Fatalf("Image change: got %v, want [Containers.Image]", got)
|
||||
if got := changedNonReloadableFields(c1, c2); len(got) != 0 {
|
||||
t.Fatalf("Image change: got %v, want [] (the pool reads it per acquire)", got)
|
||||
}
|
||||
c3 := c1
|
||||
c3.Containers.MaxConcurrency = 4
|
||||
// MaxConcurrency is NOT reloadable: the dispatchers re-read it but the
|
||||
// startup-built container pool captures it (container/pool.go:94), so
|
||||
// a change only partially applies and must warn requires-restart.
|
||||
if got := changedNonReloadableFields(c1, c3); len(got) != 1 || got[0] != "Containers.MaxConcurrency" {
|
||||
t.Fatalf("MaxConcurrency change: got %v, want [Containers.MaxConcurrency]", got)
|
||||
if got := changedNonReloadableFields(c1, c3); len(got) != 0 {
|
||||
t.Fatalf("MaxConcurrency change: got %v, want [] (the pool reads it and the dispatcher resizes)", got)
|
||||
}
|
||||
c4 := c1
|
||||
c4.Containers.VolumeTTL = config.Duration(48 * time.Hour)
|
||||
if got := changedNonReloadableFields(c1, c4); len(got) != 0 {
|
||||
t.Fatalf("VolumeTTL change: got %v, want []", got)
|
||||
}
|
||||
c5 := c1
|
||||
c5.Containers.RegistryAuth = config.SecretRef{Engine: "env", Key: "REGISTRY_AUTH"}
|
||||
if got := changedNonReloadableFields(c1, c5); len(got) != 1 || got[0] != "Containers.RegistryAuth" {
|
||||
t.Fatalf("RegistryAuth change: got %v, want [Containers.RegistryAuth] (the pool resolves it at boot)", got)
|
||||
}
|
||||
|
||||
// NATS is not reloadable: a URL change warns (the daemon's client is
|
||||
// startup-built; containers get the frozen ConnectedNATS).
|
||||
|
||||
@@ -15,6 +15,7 @@ import (
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/config"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/postgres/pgtest"
|
||||
"github.com/samcharles93/archie-core/internal/secret"
|
||||
)
|
||||
|
||||
// startupGrace is how long RunStateStore is given to either refuse the config
|
||||
@@ -46,7 +47,7 @@ func TestExternalServicesDoNotRequireBridgeNetwork(t *testing.T) {
|
||||
cfg.NATS.Mode = config.NATSModeExternal
|
||||
cfg.Containers.Network = "host"
|
||||
cfg.Services = config.Services{config.ServiceNameState: {Target: "state.example.test:9090"}}
|
||||
pool, _, cleanup := startContainers(t.Context(), cfg, slog.New(slog.DiscardHandler))
|
||||
pool, _, cleanup := startContainers(t.Context(), config.NewHolder(cfg), secret.NewRegistry(), slog.New(slog.DiscardHandler))
|
||||
defer cleanup()
|
||||
if pool == nil {
|
||||
t.Fatal("external services on host networking must construct a worker pool")
|
||||
|
||||
@@ -24,9 +24,9 @@ import (
|
||||
type testTaskActor struct{ b *boot }
|
||||
|
||||
func (a testTaskActor) ApplyChatTaskAction(
|
||||
ctx context.Context, identity *string, actor taskactions.Actor, taskID int64, action taskstate.Action,
|
||||
ctx context.Context, identity *string, actor taskactions.Actor, taskID int64, action taskstate.Action, res taskactions.ActionPayload,
|
||||
) (gateway.TaskActionResult, error) {
|
||||
if err := a.b.taskActions().Apply(ctx, identity, actor, taskID, action); err != nil {
|
||||
if err := a.b.taskActions().Apply(ctx, identity, actor, taskID, action, res); err != nil {
|
||||
return gateway.TaskActionResult{}, err
|
||||
}
|
||||
return gateway.TaskActionResult{
|
||||
@@ -84,7 +84,7 @@ func TestChatTaskActorAdapterCrossIdentityRefused(t *testing.T) {
|
||||
|
||||
for _, tc := range tests {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
_, err := adapter.ApplyChatTaskAction(ctx, &tc.actorIdentity, taskactions.Actor{}, task.ID, tc.action)
|
||||
_, err := adapter.ApplyChatTaskAction(ctx, &tc.actorIdentity, taskactions.Actor{}, task.ID, tc.action, taskactions.ActionPayload{})
|
||||
if err == nil {
|
||||
t.Fatalf("ApplyChatTaskAction() allowed cross-identity action %s by %q on task owned by %q",
|
||||
tc.action, tc.actorIdentity, task.Identity)
|
||||
@@ -152,7 +152,7 @@ func TestChatTaskActorAdapterRefusesDisallowedStateAction(t *testing.T) {
|
||||
}
|
||||
taskstatetest.Seed(ctx, t, st, task.ID, workflow.StatusQueued, tc.initialStatus, "setup")
|
||||
|
||||
_, err = adapter.ApplyChatTaskAction(ctx, new("archie"), taskactions.Actor{}, task.ID, tc.attemptAction)
|
||||
_, err = adapter.ApplyChatTaskAction(ctx, new("archie"), taskactions.Actor{}, task.ID, tc.attemptAction, taskactions.ActionPayload{})
|
||||
if err == nil {
|
||||
t.Fatalf("expected error attempting %s on %s task, got nil", tc.attemptAction, tc.initialStatus)
|
||||
}
|
||||
@@ -182,7 +182,7 @@ func TestChatTaskActorAdapterTaskNotFound(t *testing.T) {
|
||||
|
||||
adapter := newChatTaskActorForTest(t, st, config.Config{})
|
||||
|
||||
_, err := adapter.ApplyChatTaskAction(ctx, new("archie"), taskactions.Actor{}, 999999, taskstate.ActionAbandon)
|
||||
_, err := adapter.ApplyChatTaskAction(ctx, new("archie"), taskactions.Actor{}, 999999, taskstate.ActionAbandon, taskactions.ActionPayload{})
|
||||
if err == nil {
|
||||
t.Fatal("expected error for non-existent task, got nil")
|
||||
}
|
||||
@@ -257,7 +257,7 @@ func TestChatTaskActorAdapterAppliesActionsToStore(t *testing.T) {
|
||||
}
|
||||
taskstatetest.Seed(ctx, t, st, task.ID, workflow.StatusQueued, tc.initialStatus, "setup")
|
||||
|
||||
result, err := adapter.ApplyChatTaskAction(ctx, new("archie"), taskactions.Actor{}, task.ID, tc.action)
|
||||
result, err := adapter.ApplyChatTaskAction(ctx, new("archie"), taskactions.Actor{}, task.ID, tc.action, taskactions.ActionPayload{})
|
||||
if err != nil {
|
||||
t.Fatalf("ApplyChatTaskAction(%s) error = %v", tc.action, err)
|
||||
}
|
||||
@@ -292,7 +292,7 @@ func TestChatTaskActorAdapterNilContract(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
adapter := chatTaskActorAdapter{contract: nil}
|
||||
|
||||
_, err := adapter.ApplyChatTaskAction(ctx, new("archie"), taskactions.Actor{}, 1, taskstate.ActionAbandon)
|
||||
_, err := adapter.ApplyChatTaskAction(ctx, new("archie"), taskactions.Actor{}, 1, taskstate.ActionAbandon, taskactions.ActionPayload{})
|
||||
if err == nil {
|
||||
t.Fatal("expected error for nil contract, got nil")
|
||||
}
|
||||
|
||||
@@ -11,9 +11,12 @@ import (
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/config"
|
||||
"github.com/samcharles93/archie-core/internal/domain/storecontract"
|
||||
"github.com/samcharles93/archie-core/internal/domain/taskactions"
|
||||
"github.com/samcharles93/archie-core/internal/domain/workflow"
|
||||
"github.com/samcharles93/archie-core/internal/gateway"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/postgres/pgstore"
|
||||
taskactionstore "github.com/samcharles93/archie-core/internal/infrastructure/taskactions"
|
||||
"github.com/samcharles93/archie-core/internal/taskstate"
|
||||
"github.com/samcharles93/archie-core/internal/taskstate/taskstatetest"
|
||||
"github.com/samcharles93/archie-core/internal/webui"
|
||||
)
|
||||
@@ -80,9 +83,14 @@ func runChatAction(t *testing.T, ctx context.Context, st storecontract.TaskStore
|
||||
if !ok {
|
||||
t.Fatal("the task store cannot cancel executions")
|
||||
}
|
||||
// Chat's /approve reaches the daemon's one task-action service
|
||||
// (docs/prds/pr-review-operator-response.md, Decision 1).
|
||||
actions := taskactionstore.NewService(taskactionstore.Store{TaskStore: st}, nil, nil, nil, nil, nil, nil, nil)
|
||||
controller := gateway.NewStoreTaskController(chatTaskControllerAdapter{
|
||||
taskByID: st.TaskByID,
|
||||
requeue: st.Requeue,
|
||||
taskByID: st.TaskByID,
|
||||
approve: func(ctx context.Context, scope *string, actor taskactions.Actor, taskID int64, res taskactions.ActionPayload) error {
|
||||
return actions.Apply(ctx, scope, actor, taskID, taskstate.ActionApprove, res)
|
||||
},
|
||||
cancelExecution: canceller.CancelExecution,
|
||||
})
|
||||
var err error
|
||||
|
||||
@@ -0,0 +1,337 @@
|
||||
package archied
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/app/controlplane"
|
||||
"github.com/samcharles93/archie-core/internal/config"
|
||||
"github.com/samcharles93/archie-core/internal/plugin"
|
||||
"github.com/samcharles93/archie-core/internal/secret"
|
||||
"github.com/samcharles93/archie-core/internal/tools"
|
||||
toolprovider "github.com/samcharles93/archie-core/internal/tools/provider"
|
||||
"github.com/samcharles93/archie-core/internal/tools/webfetch"
|
||||
)
|
||||
|
||||
// fakeMCPEngine stands in for a configured MCP server so the live reconciliation
|
||||
// can be driven without spawning a process. It records each lifecycle call on
|
||||
// the shared recorder, keyed by configured server name.
|
||||
type fakeMCPEngine struct {
|
||||
name string
|
||||
tools []tools.ToolEntry
|
||||
rec *mcpEngineRecorder
|
||||
startErr error
|
||||
}
|
||||
|
||||
func (e *fakeMCPEngine) Manifest() plugin.Manifest {
|
||||
return plugin.Manifest{
|
||||
ID: "mcp." + e.name,
|
||||
Name: e.name,
|
||||
Version: "1.0.0",
|
||||
APIVersion: plugin.HostAPIVersion,
|
||||
Capabilities: []plugin.CapabilityKind{"tools"},
|
||||
}
|
||||
}
|
||||
|
||||
func (e *fakeMCPEngine) Start(context.Context) error {
|
||||
e.rec.recordStart(e.name)
|
||||
return e.startErr
|
||||
}
|
||||
|
||||
func (e *fakeMCPEngine) Stop(context.Context) error {
|
||||
e.rec.recordStop(e.name)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (e *fakeMCPEngine) Discover(context.Context) ([]tools.ToolEntry, error) {
|
||||
return append([]tools.ToolEntry(nil), e.tools...), nil
|
||||
}
|
||||
|
||||
// mcpEngineRecorder records what the live reconciliation did to each server and
|
||||
// hands the test a factory boot.mcpProvider can use.
|
||||
type mcpEngineRecorder struct {
|
||||
mu sync.Mutex
|
||||
builds map[string]int
|
||||
starts map[string]int
|
||||
stops map[string]int
|
||||
failNext map[string]error
|
||||
seen map[string]config.MCPServer
|
||||
}
|
||||
|
||||
func newMCPEngineRecorder() *mcpEngineRecorder {
|
||||
return &mcpEngineRecorder{
|
||||
builds: map[string]int{},
|
||||
starts: map[string]int{},
|
||||
stops: map[string]int{},
|
||||
failNext: map[string]error{},
|
||||
seen: map[string]config.MCPServer{},
|
||||
}
|
||||
}
|
||||
|
||||
func (r *mcpEngineRecorder) factory() func(config.MCPServer) (toolprovider.Engine, error) {
|
||||
return func(srv config.MCPServer) (toolprovider.Engine, error) {
|
||||
name := strings.TrimSpace(srv.Name)
|
||||
r.mu.Lock()
|
||||
r.builds[name]++
|
||||
r.seen[name] = srv
|
||||
err := r.failNext[name]
|
||||
delete(r.failNext, name)
|
||||
r.mu.Unlock()
|
||||
return &fakeMCPEngine{
|
||||
name: name,
|
||||
rec: r,
|
||||
startErr: err,
|
||||
tools: []tools.ToolEntry{{
|
||||
Name: "mcp." + name + ".tool",
|
||||
Toolset: "mcp",
|
||||
Handler: func(context.Context, map[string]any) (any, error) { return "ok", nil },
|
||||
}},
|
||||
}, nil
|
||||
}
|
||||
}
|
||||
|
||||
func (r *mcpEngineRecorder) recordStart(name string) {
|
||||
r.mu.Lock()
|
||||
defer r.mu.Unlock()
|
||||
r.starts[name]++
|
||||
}
|
||||
|
||||
func (r *mcpEngineRecorder) recordStop(name string) {
|
||||
r.mu.Lock()
|
||||
defer r.mu.Unlock()
|
||||
r.stops[name]++
|
||||
}
|
||||
|
||||
func (r *mcpEngineRecorder) failOnNextStart(name string, err error) {
|
||||
r.mu.Lock()
|
||||
defer r.mu.Unlock()
|
||||
r.failNext[name] = err
|
||||
}
|
||||
|
||||
func (r *mcpEngineRecorder) count(counts map[string]int, name string) int {
|
||||
r.mu.Lock()
|
||||
defer r.mu.Unlock()
|
||||
return counts[name]
|
||||
}
|
||||
|
||||
// toolSettingsDocument renders a stored tool-settings document carrying the
|
||||
// given MCP servers and an enabled web_fetch.
|
||||
func toolSettingsDocument(servers ...map[string]any) map[string]any {
|
||||
return map[string]any{
|
||||
"mcp_servers": servers,
|
||||
"policy": map[string]any{},
|
||||
"web_fetch": map[string]any{},
|
||||
"minimax": map[string]any{},
|
||||
}
|
||||
}
|
||||
|
||||
func mcpServerDoc(name string, parallel bool) map[string]any {
|
||||
return map[string]any{
|
||||
"name": name,
|
||||
"transport": "stdio",
|
||||
"command": "run-server",
|
||||
"parallel_tool_calls": parallel,
|
||||
}
|
||||
}
|
||||
|
||||
// newMCPReconcileBoot builds a boot whose provider registry is running, so the
|
||||
// live reconciliation acts on a real registry and a real tool index.
|
||||
func newMCPReconcileBoot(t *testing.T, base config.Config, stub *resourceWatchStub) (*boot, *applyStatusRecorder, *mcpEngineRecorder) {
|
||||
t.Helper()
|
||||
b, status := newLiveApplyBoot(t, base)
|
||||
b.controlPlane = controlplane.NewRPCClient(stub)
|
||||
b.secrets = secret.NewRegistry()
|
||||
b.toolReg = tools.NewRegistry()
|
||||
b.providerRegistry = toolprovider.NewRegistry(b.toolReg)
|
||||
if err := b.providerRegistry.Start(t.Context()); err != nil {
|
||||
t.Fatalf("start provider registry: %v", err)
|
||||
}
|
||||
b.mcpApplied = map[string]appliedMCPServer{}
|
||||
rec := newMCPEngineRecorder()
|
||||
b.mcpProvider = rec.factory()
|
||||
return b, status, rec
|
||||
}
|
||||
|
||||
// applyToolSettings stores the document and runs the live apply the watch
|
||||
// would run, the seam the daemon meets a stored tool-settings change through.
|
||||
func applyToolSettings(t *testing.T, b *boot, stub *resourceWatchStub, version int64, document map[string]any) {
|
||||
t.Helper()
|
||||
if stub.values == nil {
|
||||
stub.values = map[string]any{}
|
||||
}
|
||||
stub.values[controlplane.ToolSettingsKind] = document
|
||||
if stub.storedVersion == nil {
|
||||
stub.storedVersion = map[string]int64{}
|
||||
}
|
||||
stub.storedVersion[controlplane.ToolSettingsKind] = version
|
||||
b.applyRuntimeResourceUpdate(t.Context(), controlplane.ToolSettingsKind, controlplane.AppliedResource{Version: version})
|
||||
}
|
||||
|
||||
func TestLiveToolSettingsConnectsAnAddedServer(t *testing.T) {
|
||||
stub := &resourceWatchStub{values: databaseOwnedResources(), version: 10, storedVersion: map[string]int64{}, opens: map[string]int{}}
|
||||
b, _, rec := newMCPReconcileBoot(t, fileConfig(), stub)
|
||||
|
||||
applyToolSettings(t, b, stub, 11, toolSettingsDocument(mcpServerDoc("alpha", false)))
|
||||
|
||||
if got := rec.count(rec.starts, "alpha"); got != 1 {
|
||||
t.Errorf("alpha start count = %d, want 1", got)
|
||||
}
|
||||
if _, ok := b.toolReg.Get("mcp.alpha.tool"); !ok {
|
||||
t.Error("the added server's tool was not indexed")
|
||||
}
|
||||
if !b.providerRegistry.Has("mcp.alpha") {
|
||||
t.Error("the added server is not registered")
|
||||
}
|
||||
servers := b.cfgHolder.Get().Tools.MCPServers
|
||||
if len(servers) != 1 || servers[0].Name != "alpha" {
|
||||
t.Errorf("running config servers = %+v, want the added alpha", servers)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLiveToolSettingsLeavesAnUnchangedServerUntouched(t *testing.T) {
|
||||
stub := &resourceWatchStub{values: databaseOwnedResources(), version: 10, storedVersion: map[string]int64{}, opens: map[string]int{}}
|
||||
b, _, rec := newMCPReconcileBoot(t, fileConfig(), stub)
|
||||
|
||||
applyToolSettings(t, b, stub, 11, toolSettingsDocument(mcpServerDoc("alpha", false)))
|
||||
applyToolSettings(t, b, stub, 12, toolSettingsDocument(mcpServerDoc("alpha", false), mcpServerDoc("beta", false)))
|
||||
|
||||
if got := rec.count(rec.builds, "alpha"); got != 1 {
|
||||
t.Errorf("alpha was rebuilt on an unchanged update: builds = %d, want 1", got)
|
||||
}
|
||||
if got := rec.count(rec.starts, "alpha"); got != 1 {
|
||||
t.Errorf("alpha was reconnected on an unchanged update: starts = %d, want 1", got)
|
||||
}
|
||||
if got := rec.count(rec.stops, "alpha"); got != 0 {
|
||||
t.Errorf("alpha was stopped on an unchanged update: stops = %d, want 0", got)
|
||||
}
|
||||
if got := rec.count(rec.starts, "beta"); got != 1 {
|
||||
t.Errorf("beta start count = %d, want 1", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLiveToolSettingsReconnectsAChangedServer(t *testing.T) {
|
||||
stub := &resourceWatchStub{values: databaseOwnedResources(), version: 10, storedVersion: map[string]int64{}, opens: map[string]int{}}
|
||||
b, _, rec := newMCPReconcileBoot(t, fileConfig(), stub)
|
||||
|
||||
applyToolSettings(t, b, stub, 11, toolSettingsDocument(mcpServerDoc("alpha", false)))
|
||||
applyToolSettings(t, b, stub, 12, toolSettingsDocument(mcpServerDoc("alpha", true)))
|
||||
|
||||
if got := rec.count(rec.stops, "alpha"); got != 1 {
|
||||
t.Errorf("the changed server was not disconnected: stops = %d, want 1", got)
|
||||
}
|
||||
if got := rec.count(rec.starts, "alpha"); got != 2 {
|
||||
t.Errorf("the changed server was not reconnected: starts = %d, want 2", got)
|
||||
}
|
||||
if _, ok := b.toolReg.Get("mcp.alpha.tool"); !ok {
|
||||
t.Error("the reconnected server's tool left the index")
|
||||
}
|
||||
}
|
||||
|
||||
func TestLiveToolSettingsDisconnectsARemovedServer(t *testing.T) {
|
||||
stub := &resourceWatchStub{values: databaseOwnedResources(), version: 10, storedVersion: map[string]int64{}, opens: map[string]int{}}
|
||||
b, _, rec := newMCPReconcileBoot(t, fileConfig(), stub)
|
||||
|
||||
applyToolSettings(t, b, stub, 11, toolSettingsDocument(mcpServerDoc("alpha", false), mcpServerDoc("beta", false)))
|
||||
applyToolSettings(t, b, stub, 12, toolSettingsDocument(mcpServerDoc("beta", false)))
|
||||
|
||||
if got := rec.count(rec.stops, "alpha"); got != 1 {
|
||||
t.Errorf("the removed server was not disconnected: stops = %d, want 1", got)
|
||||
}
|
||||
if b.providerRegistry.Has("mcp.alpha") {
|
||||
t.Error("the removed server stayed registered")
|
||||
}
|
||||
if _, ok := b.toolReg.Get("mcp.alpha.tool"); ok {
|
||||
t.Error("the removed server's tool stayed indexed")
|
||||
}
|
||||
if got := rec.count(rec.stops, "beta"); got != 0 {
|
||||
t.Errorf("the surviving server was disturbed: stops = %d, want 0", got)
|
||||
}
|
||||
}
|
||||
|
||||
// TestLiveToolSettingsKeepsTheRunningServerWhenAChangeCannotStart is the path
|
||||
// where the change must NOT apply: a changed server whose replacement fails to
|
||||
// start is rolled back to the one that was running, and the refusal reaches
|
||||
// apply status rather than the kind silently reporting success.
|
||||
func TestLiveToolSettingsKeepsTheRunningServerWhenAChangeCannotStart(t *testing.T) {
|
||||
stub := &resourceWatchStub{values: databaseOwnedResources(), version: 10, storedVersion: map[string]int64{}, opens: map[string]int{}}
|
||||
b, status, rec := newMCPReconcileBoot(t, fileConfig(), stub)
|
||||
|
||||
applyToolSettings(t, b, stub, 11, toolSettingsDocument(mcpServerDoc("alpha", false)))
|
||||
rec.failOnNextStart("alpha", errors.New("handshake refused"))
|
||||
applyToolSettings(t, b, stub, 12, toolSettingsDocument(mcpServerDoc("alpha", true)))
|
||||
|
||||
// Two stops: the running server is disconnected for the replacement, and
|
||||
// the replacement's own half-start is cleaned up when its handshake fails.
|
||||
if got := rec.count(rec.stops, "alpha"); got != 2 {
|
||||
t.Errorf("stops = %d, want 2 (the replaced server and the failed replacement's cleanup)", got)
|
||||
}
|
||||
if got := rec.count(rec.starts, "alpha"); got != 3 {
|
||||
t.Errorf("starts = %d, want 3 (initial, refused replacement, rollback restart)", got)
|
||||
}
|
||||
if _, ok := b.toolReg.Get("mcp.alpha.tool"); !ok {
|
||||
t.Error("the refused change left the running server's tool out of the index")
|
||||
}
|
||||
if !b.providerRegistry.Has("mcp.alpha") {
|
||||
t.Error("the refused change left the server unregistered")
|
||||
}
|
||||
last, ok := status.last(controlplane.ToolSettingsKind)
|
||||
if !ok {
|
||||
t.Fatal("the refused change reported no apply status")
|
||||
}
|
||||
if !strings.Contains(last.Error, "alpha") {
|
||||
t.Errorf("apply status error = %q, want it to name the refused server", last.Error)
|
||||
}
|
||||
}
|
||||
|
||||
// TestLiveToolSettingsRebuildsTheWebFetchEntry: web_fetch captured its config
|
||||
// at construction, so a live change rebuilds the entry rather than leaving the
|
||||
// boot value in the model's tool list.
|
||||
func TestLiveToolSettingsRebuildsTheWebFetchEntry(t *testing.T) {
|
||||
stub := &resourceWatchStub{values: databaseOwnedResources(), version: 10, storedVersion: map[string]int64{}, opens: map[string]int{}}
|
||||
b, _, _ := newMCPReconcileBoot(t, fileConfig(), stub)
|
||||
|
||||
disabled := toolSettingsDocument()
|
||||
disabled["web_fetch"] = map[string]any{"enabled": false}
|
||||
applyToolSettings(t, b, stub, 11, disabled)
|
||||
if _, ok := b.toolReg.Get(webfetch.ToolName); ok {
|
||||
t.Error("web_fetch stayed registered after the stored document disabled it")
|
||||
}
|
||||
|
||||
applyToolSettings(t, b, stub, 12, toolSettingsDocument())
|
||||
if _, ok := b.toolReg.Get(webfetch.ToolName); !ok {
|
||||
t.Error("web_fetch was not registered after the stored document re-enabled it")
|
||||
}
|
||||
}
|
||||
|
||||
// TestLiveToolSettingsKeepsAnUnprojectedMCPHeader: the stored document cannot
|
||||
// carry secret-bearing headers, so the layering restores them from the running
|
||||
// base (controlplane.runtimeToolConfigFrom). The reconciliation must build the
|
||||
// reconnected engine from that layered server, not from the document alone, or
|
||||
// an authentication header disappears on the first live edit.
|
||||
func TestLiveToolSettingsKeepsAnUnprojectedMCPHeader(t *testing.T) {
|
||||
base := fileConfig()
|
||||
base.Tools.MCPServers = []config.MCPServer{{
|
||||
Name: "alpha",
|
||||
Transport: "http",
|
||||
URL: "https://alpha.example/mcp",
|
||||
Headers: map[string]string{"Authorization": "Bearer secret"},
|
||||
}}
|
||||
stub := &resourceWatchStub{values: databaseOwnedResources(), version: 10, storedVersion: map[string]int64{}, opens: map[string]int{}}
|
||||
b, _, rec := newMCPReconcileBoot(t, base, stub)
|
||||
|
||||
changed := mcpServerDoc("alpha", true)
|
||||
changed["transport"] = "http"
|
||||
changed["url"] = "https://alpha.example/mcp"
|
||||
delete(changed, "command")
|
||||
applyToolSettings(t, b, stub, 11, toolSettingsDocument(changed))
|
||||
|
||||
rec.mu.Lock()
|
||||
seen := rec.seen["alpha"]
|
||||
rec.mu.Unlock()
|
||||
if got := seen.Headers["Authorization"]; got != "Bearer secret" {
|
||||
t.Errorf("reconnected server headers = %v, want the unprojected Authorization header restored", seen.Headers)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package archied
|
||||
|
||||
import (
|
||||
"log/slog"
|
||||
"testing"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/config"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/transcription"
|
||||
"github.com/samcharles93/archie-core/internal/secret"
|
||||
)
|
||||
|
||||
// TestSetupTranscriberBuildsOnTheModelOwningSide pins where the voice
|
||||
// capability is constructed: beside the chat runtime, from the process's own
|
||||
// [models].transcription and [providers.*]. The Messaging Service carries the
|
||||
// audio bytes across the inbound wire but no longer builds this client.
|
||||
func TestSetupTranscriberBuildsOnTheModelOwningSide(t *testing.T) {
|
||||
log := slog.New(slog.DiscardHandler)
|
||||
|
||||
t.Run("configured role wires a transcriber", func(t *testing.T) {
|
||||
b := &boot{secrets: secret.NewRegistry()}
|
||||
b.setupTranscriber(config.Config{
|
||||
Models: map[string]string{transcription.Role: "local/whisper-large-v3"},
|
||||
Providers: map[string]config.Provider{
|
||||
"local": {Class: "openai-compatible", BaseURL: "http://127.0.0.1:8080/v1"},
|
||||
},
|
||||
}, log)
|
||||
if b.transcriber == nil {
|
||||
t.Fatal("transcriber = nil, want a client built from [models].transcription")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("absent role degrades to nil", func(t *testing.T) {
|
||||
b := &boot{secrets: secret.NewRegistry()}
|
||||
b.setupTranscriber(config.Config{}, log)
|
||||
if b.transcriber != nil {
|
||||
t.Fatalf("transcriber = %v, want nil when no role is configured", b.transcriber)
|
||||
}
|
||||
})
|
||||
}
|
||||
@@ -15,7 +15,7 @@ import (
|
||||
// channel. Only what an operator may see: no configuration value ever enters a
|
||||
// descriptor, so the manager cannot become a second source for a token or a
|
||||
// credential.
|
||||
func channelDescriptors(instances []channelInstance) []status.Descriptor {
|
||||
func channelDescriptors(instances []*channelInstance) []status.Descriptor {
|
||||
descriptors := make([]status.Descriptor, 0, len(instances))
|
||||
for _, instance := range instances {
|
||||
descriptors = append(descriptors, status.Descriptor{
|
||||
@@ -34,8 +34,8 @@ func channelDescriptors(instances []channelInstance) []status.Descriptor {
|
||||
// the single source for both the capability a descriptor declares and the action
|
||||
// a reload performs; deriving them separately is how a dashboard ends up offering
|
||||
// a button that reports success and changes nothing (archie-core-np7l).
|
||||
func reloadable(instance channelInstance) bool {
|
||||
gateway, ok := instance.channel.(*telegram.Gateway)
|
||||
func reloadable(instance *channelInstance) bool {
|
||||
gateway, ok := instance.current().(*telegram.Gateway)
|
||||
return ok && gateway.Reload != nil
|
||||
}
|
||||
|
||||
@@ -53,7 +53,7 @@ func (s *Service) ReloadChannel(_ context.Context, id string) error {
|
||||
if instance.name != id {
|
||||
continue
|
||||
}
|
||||
gateway, ok := instance.channel.(*telegram.Gateway)
|
||||
gateway, ok := instance.current().(*telegram.Gateway)
|
||||
if !ok || gateway.Reload == nil {
|
||||
return fmt.Errorf("messaging: channel %q does not support reload", id)
|
||||
}
|
||||
|
||||
@@ -144,7 +144,7 @@ func TestChannelDescriptorsDeclareCapabilities(t *testing.T) {
|
||||
// "telegram" correctly declares nothing.
|
||||
withReload := telegram.New("token-123", []int64{1}, slog.New(slog.DiscardHandler))
|
||||
withReload.Reload = func(*telegram.Gateway) error { return nil }
|
||||
descriptors := channelDescriptors([]channelInstance{
|
||||
descriptors := channelDescriptors([]*channelInstance{
|
||||
{name: "telegram", channel: withReload},
|
||||
{name: "email", channel: fakeChannel{}},
|
||||
{name: "webhook", channel: fakeChannel{}},
|
||||
|
||||
@@ -7,6 +7,8 @@ import (
|
||||
"net"
|
||||
"strconv"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/channels"
|
||||
"github.com/samcharles93/archie-core/internal/channels/email"
|
||||
@@ -14,6 +16,7 @@ import (
|
||||
"github.com/samcharles93/archie-core/internal/channels/telegram"
|
||||
"github.com/samcharles93/archie-core/internal/channels/webhook"
|
||||
"github.com/samcharles93/archie-core/internal/config"
|
||||
"github.com/samcharles93/archie-core/internal/domain/applystatus"
|
||||
"github.com/samcharles93/archie-core/internal/domain/health"
|
||||
"github.com/samcharles93/archie-core/internal/domain/messaging"
|
||||
"github.com/samcharles93/archie-core/internal/domain/storecontract"
|
||||
@@ -30,11 +33,44 @@ type deps struct {
|
||||
Chat messaging.ChatContract
|
||||
Health *health.Registry
|
||||
Settings *messaging.SettingsCommand
|
||||
// SettingsSource, when non-nil, enables the live channel-settings
|
||||
// reconcile: the service re-reads the stored channel-settings resource on
|
||||
// the apply-status restamp interval and restarts only the channels whose
|
||||
// settings changed. Nil leaves the service with no live path, which is the
|
||||
// honest state for a test or a process with no State Store.
|
||||
SettingsSource chatSettingsSource
|
||||
// ApplyReporter records what this process applied. Nil reports nothing.
|
||||
ApplyReporter *applystatus.Reporter
|
||||
// AppliedVersion is the channel-settings version already running when the
|
||||
// service starts, so the first reconcile tick does not re-apply it.
|
||||
AppliedVersion int64
|
||||
// ReconcileInterval overrides applystatus.RestampInterval, so a test can
|
||||
// drive the loop without waiting 30 seconds.
|
||||
ReconcileInterval time.Duration
|
||||
}
|
||||
|
||||
// channelInstance is one composed channel and the runtime state that lets the
|
||||
// service restart it alone. channel and cancel are guarded by mu; rebuild is
|
||||
// immutable. The channel swaps when the stored settings change, which is what a
|
||||
// restart is: build the replacement, stop the old run context, start the new
|
||||
// instance. A channel with no in-place reload seam is therefore still
|
||||
// reloadable.
|
||||
type channelInstance struct {
|
||||
name string
|
||||
channel channels.Channel
|
||||
rebuild func(ResolvedConfig) (channels.Channel, error)
|
||||
|
||||
mu sync.Mutex
|
||||
channel channels.Channel
|
||||
cancel context.CancelFunc
|
||||
supervised bool
|
||||
restartPending bool
|
||||
}
|
||||
|
||||
// current returns the channel instance currently serving, under the lock.
|
||||
func (c *channelInstance) current() channels.Channel {
|
||||
c.mu.Lock()
|
||||
defer c.mu.Unlock()
|
||||
return c.channel
|
||||
}
|
||||
|
||||
// Service manages the lifecycle of the extracted Messaging Service and its
|
||||
@@ -55,11 +91,26 @@ type Service struct {
|
||||
log *slog.Logger
|
||||
chat messaging.ChatContract
|
||||
health *health.Registry
|
||||
channels []channelInstance
|
||||
channels []*channelInstance
|
||||
// settingsSource re-reads the stored channel settings. Nil disables the
|
||||
// reconcile loop.
|
||||
settingsSource chatSettingsSource
|
||||
// reporter records the applied channel-settings version. Nil reports
|
||||
// nothing.
|
||||
reporter *applystatus.Reporter
|
||||
// appliedVersion is the channel-settings version the running channels were
|
||||
// built from, so a reconcile tick does not re-apply its own work.
|
||||
appliedVersion atomic.Int64
|
||||
// reconcileInterval is how often the stored settings are re-read.
|
||||
reconcileInterval time.Duration
|
||||
|
||||
mu sync.Mutex
|
||||
running bool
|
||||
stop func()
|
||||
// wg tracks the per-channel supervisors, so a shutdown waits for every
|
||||
// channel to stop before Start returns. A restart can start a replacement
|
||||
// supervisor, which is why it lives here rather than in Start's frame.
|
||||
wg sync.WaitGroup
|
||||
}
|
||||
|
||||
// compose builds the Service from resolved configuration. ctx is the service
|
||||
@@ -69,57 +120,122 @@ type Service struct {
|
||||
// dropped: a front-end the operator configured must never silently not run.
|
||||
func compose(ctx context.Context, d deps) (*Service, error) {
|
||||
srv := &Service{
|
||||
cfg: d.Config,
|
||||
log: d.Log,
|
||||
chat: d.Chat,
|
||||
health: d.Health,
|
||||
cfg: d.Config,
|
||||
log: d.Log,
|
||||
chat: d.Chat,
|
||||
health: d.Health,
|
||||
settingsSource: d.SettingsSource,
|
||||
reporter: d.ApplyReporter,
|
||||
reconcileInterval: d.ReconcileInterval,
|
||||
}
|
||||
srv.appliedVersion.Store(d.AppliedVersion)
|
||||
if srv.reconcileInterval <= 0 {
|
||||
srv.reconcileInterval = applystatus.RestampInterval
|
||||
}
|
||||
|
||||
if d.Config.TelegramToken != "" {
|
||||
if len(d.Config.Telegram.AllowedUserIDs) == 0 {
|
||||
d.Log.Warn("chat.telegram has no allowed_user_ids: every sender will be rejected. " +
|
||||
"Add your Telegram user id to chat.telegram.allowed_user_ids to enable the bot.")
|
||||
}
|
||||
tg := telegram.New(d.Config.TelegramToken, d.Config.Telegram.AllowedUserIDs, d.Log)
|
||||
tg.Settings = d.Settings
|
||||
configureTelegram(ctx, tg, d.Config, d.Chat, d.Log)
|
||||
if err := srv.add("telegram", tg, telegramValidateConfigMap(d.Config.Telegram)); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
instances, err := composeChannels(ctx, d)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if d.Config.Email.ListenAddr != "" {
|
||||
em := email.New(d.Config.Email.ListenAddr, d.Config.Email.RelayAddr, d.Log)
|
||||
if err := srv.add("email", em, map[string]any{
|
||||
"listen_addr": d.Config.Email.ListenAddr,
|
||||
"relay_addr": d.Config.Email.RelayAddr,
|
||||
}); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
if d.Config.WebhookAddr != "" {
|
||||
host, port := parseListenAddr(d.Config.WebhookAddr, "0.0.0.0", 8644)
|
||||
wh := webhook.New(host, port, webhookRoutes(d.Config.Webhook, d.Config.WebhookSecret), d.Log)
|
||||
if err := srv.add("webhook", wh, map[string]any{"host": host, "port": port}); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
srv.channels = instances
|
||||
srv.status = status.NewManager(channelDescriptors(srv.channels))
|
||||
srv.statusWriter = d.ChannelStatus
|
||||
srv.publish = make(chan struct{}, 1)
|
||||
return srv, nil
|
||||
}
|
||||
|
||||
// add validates a channel against its own ConfigSchema contract and
|
||||
// registers it.
|
||||
func (s *Service) add(name string, ch channels.Channel, cfg map[string]any) error {
|
||||
if err := ch.ValidateConfig(cfg); err != nil {
|
||||
return fmt.Errorf("chat.%s config invalid: %w", name, err)
|
||||
// composeChannels builds the configured channel set. Each instance carries the
|
||||
// factory a restart uses to build a replacement from a new resolved
|
||||
// configuration, which is what makes a channel with no in-place reload seam
|
||||
// still restarted alone rather than through a process restart.
|
||||
func composeChannels(ctx context.Context, d deps) ([]*channelInstance, error) {
|
||||
instances := make([]*channelInstance, 0, 3)
|
||||
if d.Config.TelegramToken != "" {
|
||||
instance, err := composeTelegram(ctx, d)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
instances = append(instances, instance)
|
||||
}
|
||||
s.channels = append(s.channels, channelInstance{name: name, channel: ch})
|
||||
return nil
|
||||
if d.Config.Email.ListenAddr != "" {
|
||||
instance, err := composeEmail(d)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
instances = append(instances, instance)
|
||||
}
|
||||
if d.Config.WebhookAddr != "" {
|
||||
instance, err := composeWebhook(d)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
instances = append(instances, instance)
|
||||
}
|
||||
return instances, nil
|
||||
}
|
||||
|
||||
func composeTelegram(ctx context.Context, d deps) (*channelInstance, error) {
|
||||
if len(d.Config.Telegram.AllowedUserIDs) == 0 {
|
||||
d.Log.Warn("chat.telegram has no allowed_user_ids: every sender will be rejected. " +
|
||||
"Add your Telegram user id to chat.telegram.allowed_user_ids to enable the bot.")
|
||||
}
|
||||
build := func(cfg ResolvedConfig) (channels.Channel, error) {
|
||||
if cfg.TelegramToken == "" {
|
||||
return nil, nil
|
||||
}
|
||||
tg := telegram.New(cfg.TelegramToken, cfg.Telegram.AllowedUserIDs, d.Log)
|
||||
tg.Settings = d.Settings
|
||||
configureTelegram(ctx, tg, cfg, d.Chat, d.Log)
|
||||
if err := tg.ValidateConfig(telegramValidateConfigMap(cfg.Telegram)); err != nil {
|
||||
return nil, fmt.Errorf("chat.telegram config invalid: %w", err)
|
||||
}
|
||||
return tg, nil
|
||||
}
|
||||
ch, err := build(d.Config)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &channelInstance{name: "telegram", channel: ch, rebuild: build}, nil
|
||||
}
|
||||
|
||||
func composeEmail(d deps) (*channelInstance, error) {
|
||||
build := func(cfg ResolvedConfig) (channels.Channel, error) {
|
||||
if cfg.Email.ListenAddr == "" {
|
||||
return nil, nil
|
||||
}
|
||||
em := email.New(cfg.Email.ListenAddr, cfg.Email.RelayAddr, d.Log)
|
||||
if err := em.ValidateConfig(map[string]any{
|
||||
"listen_addr": cfg.Email.ListenAddr,
|
||||
"relay_addr": cfg.Email.RelayAddr,
|
||||
}); err != nil {
|
||||
return nil, fmt.Errorf("chat.email config invalid: %w", err)
|
||||
}
|
||||
return em, nil
|
||||
}
|
||||
ch, err := build(d.Config)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &channelInstance{name: "email", channel: ch, rebuild: build}, nil
|
||||
}
|
||||
|
||||
func composeWebhook(d deps) (*channelInstance, error) {
|
||||
build := func(cfg ResolvedConfig) (channels.Channel, error) {
|
||||
if cfg.WebhookAddr == "" {
|
||||
return nil, nil
|
||||
}
|
||||
host, port := parseListenAddr(cfg.WebhookAddr, "0.0.0.0", 8644)
|
||||
wh := webhook.New(host, port, webhookRoutes(cfg.Webhook, cfg.WebhookSecret), d.Log)
|
||||
if err := wh.ValidateConfig(map[string]any{"host": host, "port": port}); err != nil {
|
||||
return nil, fmt.Errorf("chat.webhook config invalid: %w", err)
|
||||
}
|
||||
return wh, nil
|
||||
}
|
||||
ch, err := build(d.Config)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &channelInstance{name: "webhook", channel: ch, rebuild: build}, nil
|
||||
}
|
||||
|
||||
// telegramValidateConfigMap builds the map Gateway.ValidateConfig expects
|
||||
@@ -180,24 +296,17 @@ func (s *Service) Start(ctx context.Context) error {
|
||||
// opened during startup shows configured channels rather than none.
|
||||
s.signalPublish()
|
||||
}
|
||||
if s.settingsSource != nil {
|
||||
go s.reconcileLoop(ctx)
|
||||
}
|
||||
|
||||
var wg sync.WaitGroup
|
||||
for _, ch := range s.channels {
|
||||
c := ch
|
||||
wg.Go(func() {
|
||||
s.log.Info("starting channel", "name", c.name)
|
||||
if err := c.channel.Start(ctx, s.chat, s.lifecycleFor(c.name)); err != nil && ctx.Err() == nil {
|
||||
// The channel could not run: say so on the operator's surface, not
|
||||
// only in the log, which is the difference between a dashboard that
|
||||
// shows a dead channel and one that shows nothing at all.
|
||||
s.status.MarkFailed(c.name, err.Error())
|
||||
s.log.Error("channel stopped with error", "name", c.name, "err", err)
|
||||
return
|
||||
}
|
||||
// A channel that returns without an error has stopped, either because
|
||||
// the service was asked to or because its own loop ended.
|
||||
s.status.MarkStopped(c.name, "")
|
||||
})
|
||||
c.mu.Lock()
|
||||
c.supervised = true
|
||||
c.mu.Unlock()
|
||||
s.wg.Add(1)
|
||||
go s.runChannel(ctx, c)
|
||||
}
|
||||
|
||||
// Every configured channel has been handed its lifetime and the service
|
||||
@@ -214,18 +323,10 @@ func (s *Service) Start(ctx context.Context) error {
|
||||
|
||||
<-ctx.Done()
|
||||
|
||||
for _, ch := range s.channels {
|
||||
shutdownCtx, shutdownCancel := context.WithTimeout(context.WithoutCancel(ctx), s.cfg.Options.ShutdownTimeout)
|
||||
if err := ch.channel.Stop(shutdownCtx); err != nil {
|
||||
s.log.Warn("channel stop failed", "name", ch.name, "err", err)
|
||||
}
|
||||
shutdownCancel()
|
||||
}
|
||||
|
||||
wg.Wait()
|
||||
s.wg.Wait()
|
||||
|
||||
if s.statusWriter != nil {
|
||||
// The final report carries the stopped states the loop above recorded.
|
||||
// The final report carries the stopped states each supervisor recorded.
|
||||
// Its own context, because ctx is already cancelled.
|
||||
finalCtx, cancel := context.WithTimeout(context.WithoutCancel(ctx), s.cfg.Options.ShutdownTimeout)
|
||||
defer cancel()
|
||||
|
||||
@@ -0,0 +1,216 @@
|
||||
package archiemessaging
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"slices"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/channels/telegram"
|
||||
"github.com/samcharles93/archie-core/internal/config"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/controlplanerpc"
|
||||
"github.com/samcharles93/archie-core/internal/secret"
|
||||
)
|
||||
|
||||
// chatSettingsSource reads the stored channel-settings resource and layers it
|
||||
// over the file document. *controlplanerpc.Client satisfies it.
|
||||
type chatSettingsSource interface {
|
||||
RuntimeChatConfig(ctx context.Context, base config.ChatConfig) (config.ChatConfig, int64, error)
|
||||
}
|
||||
|
||||
// reconcileLoop re-reads the stored channel settings on the apply-status
|
||||
// restamp interval until ctx ends. A poll rather than a stream: the restamp
|
||||
// interval is the cadence every process already reports at, the messaging
|
||||
// process has no other control-plane watch to share a reconnect ladder with,
|
||||
// and a stored document that cannot be read must leave the running channels
|
||||
// alone (docs/prds/plugin-settings-live.md).
|
||||
func (s *Service) reconcileLoop(ctx context.Context) {
|
||||
ticker := time.NewTicker(s.reconcileInterval)
|
||||
defer ticker.Stop()
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return
|
||||
case <-ticker.C:
|
||||
if err := s.reconcileOnce(ctx); err != nil {
|
||||
s.log.Warn("channel settings reconcile failed", "err", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// reconcileOnce re-reads the stored channel settings and, when the version
|
||||
// moved, applies the change to the running channels and records the outcome in
|
||||
// apply status.
|
||||
//
|
||||
// The file document is re-resolved first, exactly as the /restart path does, so
|
||||
// the layering is the same projection boot used: the store overrides the file,
|
||||
// and a field the store omits keeps the file's value.
|
||||
func (s *Service) reconcileOnce(ctx context.Context) error {
|
||||
base, err := Resolve(s.currentConfig().Options, s.log)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
layered, version, err := s.settingsSource.RuntimeChatConfig(ctx, config.ChatConfig{
|
||||
Telegram: base.Telegram, Email: base.Email, Webhook: base.Webhook,
|
||||
WebhookAddr: base.WebhookAddr, ShowToolCalls: base.ShowToolCalls,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if version == 0 || version <= s.appliedVersion.Load() {
|
||||
return nil
|
||||
}
|
||||
next, err := resolveChatSecrets(base, layered)
|
||||
if err != nil {
|
||||
// A credential this process cannot resolve is a document rejection: the
|
||||
// running channels keep serving and the version stays reported as not
|
||||
// applied, so the operator sees the refusal rather than a false success.
|
||||
s.report(ctx, version, err)
|
||||
return err
|
||||
}
|
||||
applied, outstanding := s.applyChannelSettings(next)
|
||||
if applied {
|
||||
s.appliedVersion.Store(version)
|
||||
}
|
||||
s.report(ctx, version, outstanding)
|
||||
return nil
|
||||
}
|
||||
|
||||
// resolveChatSecrets carries the layered chat settings onto the resolved
|
||||
// projection, resolving the store's secret references. A channel whose token
|
||||
// cannot be resolved stays off rather than composing with an empty credential
|
||||
// (the startup rule, config.go resolveTelegramToken).
|
||||
func resolveChatSecrets(base ResolvedConfig, layered config.ChatConfig) (ResolvedConfig, error) {
|
||||
next := base
|
||||
next.Telegram, next.Email, next.Webhook = layered.Telegram, layered.Email, layered.Webhook
|
||||
next.WebhookAddr, next.ShowToolCalls = layered.WebhookAddr, layered.ShowToolCalls
|
||||
|
||||
secrets := secret.NewRegistry()
|
||||
token, err := resolveTelegramToken(layered.Telegram, secrets)
|
||||
if err != nil {
|
||||
return next, fmt.Errorf("resolve database telegram token: %w", err)
|
||||
}
|
||||
next.TelegramToken = token
|
||||
webhookSecret, err := resolveWebhookSecret(layered.Webhook, secrets)
|
||||
if err != nil {
|
||||
return next, fmt.Errorf("resolve database webhook secret: %w", err)
|
||||
}
|
||||
next.WebhookSecret = webhookSecret
|
||||
return next, nil
|
||||
}
|
||||
|
||||
// applyChannelSettings applies a freshly resolved chat config to the running
|
||||
// channels. A channel restarts only when its own transport settings changed;
|
||||
// every other channel keeps serving. It reports whether every requested restart
|
||||
// was carried out, so a rebuild that failed is retried rather than recorded as
|
||||
// applied.
|
||||
//
|
||||
// Machine-level settings do not restart a channel. The chat session defaults
|
||||
// (workspace, filesystem access, max steps, rate limit) are not part of the
|
||||
// messaging projection at all, and show_tool_calls is handed to the running
|
||||
// Telegram gateway in place, so both apply to turns that start after the change
|
||||
// rather than interrupting one in flight.
|
||||
//
|
||||
// The composed set and the listen addresses are fixed for the life of the
|
||||
// process. Enabling or disabling a channel and rebinding a listen address are
|
||||
// process bindings; the change is refused and reported for a restart instead of
|
||||
// applied (docs/prds/runtime-control-plane.md, "Some changes require a
|
||||
// restart").
|
||||
func (s *Service) applyChannelSettings(next ResolvedConfig) (bool, error) {
|
||||
current := s.currentConfig()
|
||||
effective, outstanding := pinProcessBindings(current, next)
|
||||
|
||||
if next.ShowToolCalls != current.ShowToolCalls {
|
||||
s.applyShowToolCalls(next.ShowToolCalls)
|
||||
}
|
||||
|
||||
applied := true
|
||||
for _, id := range channelChanges(current, effective) {
|
||||
if err := s.restartChannel(id, effective); err != nil {
|
||||
outstanding = append(outstanding, err.Error())
|
||||
applied = false
|
||||
}
|
||||
}
|
||||
// A failed restart leaves the running configuration in place, so the next
|
||||
// tick sees the same change and retries it. A change that was carried out
|
||||
// (including a binding change that was refused and reported) is recorded, so
|
||||
// it is not applied again on every tick.
|
||||
if applied {
|
||||
s.setConfig(effective)
|
||||
}
|
||||
if len(outstanding) == 0 {
|
||||
return applied, nil
|
||||
}
|
||||
return applied, errors.New(strings.Join(outstanding, "; "))
|
||||
}
|
||||
|
||||
// applyShowToolCalls updates the running Telegram gateways in place. Nothing
|
||||
// else consumes the setting, and it is read per reply, so it needs no restart.
|
||||
func (s *Service) applyShowToolCalls(show bool) {
|
||||
for _, c := range s.channels {
|
||||
if g, ok := c.current().(*telegram.Gateway); ok {
|
||||
g.SetShowToolCalls(show)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// pinProcessBindings holds the values a running process cannot change in place:
|
||||
// the listen addresses it already bound, and the set of channels it composed.
|
||||
// The returned configuration keeps the running value for each, and the returned
|
||||
// problems name what the operator changed and when it takes effect.
|
||||
func pinProcessBindings(current, next ResolvedConfig) (ResolvedConfig, []string) {
|
||||
effective := next
|
||||
var problems []string
|
||||
|
||||
if (current.TelegramToken != "") != (next.TelegramToken != "") {
|
||||
problems = append(problems, "chat.telegram enabled/disabled; takes effect on restart")
|
||||
effective.Telegram, effective.TelegramToken = current.Telegram, current.TelegramToken
|
||||
}
|
||||
if (current.Email.ListenAddr != "") != (next.Email.ListenAddr != "") {
|
||||
problems = append(problems, "chat.email enabled/disabled; takes effect on restart")
|
||||
effective.Email = current.Email
|
||||
}
|
||||
if (current.WebhookAddr != "") != (next.WebhookAddr != "") {
|
||||
problems = append(problems, "chat.webhook enabled/disabled; takes effect on restart")
|
||||
effective.Webhook, effective.WebhookAddr, effective.WebhookSecret = current.Webhook, current.WebhookAddr, current.WebhookSecret
|
||||
}
|
||||
|
||||
if current.Email.ListenAddr != "" && next.Email.ListenAddr != current.Email.ListenAddr {
|
||||
problems = append(problems, "chat.email.listen_addr changed; takes effect on restart")
|
||||
effective.Email.ListenAddr = current.Email.ListenAddr
|
||||
}
|
||||
if current.WebhookAddr != "" && next.WebhookAddr != current.WebhookAddr {
|
||||
problems = append(problems, "chat.webhook_addr changed; takes effect on restart")
|
||||
effective.WebhookAddr = current.WebhookAddr
|
||||
}
|
||||
return effective, problems
|
||||
}
|
||||
|
||||
// channelChanges returns the composed channels whose transport settings differ
|
||||
// between the running and the effective configuration. A channel absent from
|
||||
// current (not composed) never appears, and an unchanged channel is not
|
||||
// restarted.
|
||||
func channelChanges(current, next ResolvedConfig) []string {
|
||||
var changed []string
|
||||
if current.TelegramToken != "" &&
|
||||
(current.TelegramToken != next.TelegramToken || !slices.Equal(current.Telegram.AllowedUserIDs, next.Telegram.AllowedUserIDs)) {
|
||||
changed = append(changed, "telegram")
|
||||
}
|
||||
if current.Email.ListenAddr != "" && current.Email.RelayAddr != next.Email.RelayAddr {
|
||||
changed = append(changed, "email")
|
||||
}
|
||||
if current.WebhookAddr != "" && (current.Webhook != next.Webhook || current.WebhookSecret != next.WebhookSecret) {
|
||||
changed = append(changed, "webhook")
|
||||
}
|
||||
return changed
|
||||
}
|
||||
|
||||
func (s *Service) report(ctx context.Context, version int64, applyErr error) {
|
||||
if s.reporter == nil {
|
||||
return
|
||||
}
|
||||
s.reporter.Report(ctx, controlplanerpc.ChannelSettingsKind, version, applyErr)
|
||||
}
|
||||
@@ -0,0 +1,253 @@
|
||||
package archiemessaging
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/channels/status"
|
||||
"github.com/samcharles93/archie-core/internal/config"
|
||||
"github.com/samcharles93/archie-core/internal/domain/applystatus"
|
||||
"github.com/samcharles93/archie-core/internal/domain/messaging"
|
||||
"github.com/samcharles93/archie-core/internal/domain/storecontract"
|
||||
)
|
||||
|
||||
// channelByID returns the live instance the service currently serves for id.
|
||||
func channelByID(t *testing.T, srv *Service, id string) *channelInstance {
|
||||
t.Helper()
|
||||
for _, instance := range srv.channels {
|
||||
if instance.name == id {
|
||||
return instance
|
||||
}
|
||||
}
|
||||
t.Fatalf("no channel %q composed", id)
|
||||
return nil
|
||||
}
|
||||
|
||||
// channelAddrs reserves two loopback ports, one per channel.
|
||||
func channelAddrs(t *testing.T) (emailAddr, webhookAddr string) {
|
||||
t.Helper()
|
||||
return fmt.Sprintf("127.0.0.1:%d", freePort(t)), fmt.Sprintf("127.0.0.1:%d", freePort(t))
|
||||
}
|
||||
|
||||
// writeChannelConfig writes a file document with email and webhook enabled. A
|
||||
// file rather than a hand-built ResolvedConfig, because the reconcile path
|
||||
// re-resolves the file exactly as production does; a hand-built config would
|
||||
// prove nothing about that half.
|
||||
func writeChannelConfig(t *testing.T, emailAddr, webhookAddr, route string) string {
|
||||
t.Helper()
|
||||
path := filepath.Join(t.TempDir(), "config.toml")
|
||||
content := fmt.Sprintf("bot_user = \"testbot\"\n\n[chat]\nwebhook_addr = %q\n\n[chat.email]\nlisten_addr = %q\n\n[chat.webhook]\npath = %q\n",
|
||||
webhookAddr, emailAddr, route)
|
||||
if err := os.WriteFile(path, []byte(content), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return path
|
||||
}
|
||||
|
||||
func resolveChannels(t *testing.T, emailAddr, webhookAddr, route string) ResolvedConfig {
|
||||
t.Helper()
|
||||
cfg, err := Resolve(
|
||||
Options{Config: writeChannelConfig(t, emailAddr, webhookAddr, route), ShutdownTimeout: 2 * time.Second},
|
||||
slog.New(slog.DiscardHandler),
|
||||
)
|
||||
if err != nil {
|
||||
t.Fatalf("Resolve: %v", err)
|
||||
}
|
||||
return cfg
|
||||
}
|
||||
|
||||
// startComposed starts a composed service and waits for its channels to run.
|
||||
func startComposed(t *testing.T, d deps) *Service {
|
||||
t.Helper()
|
||||
srv, err := compose(t.Context(), d)
|
||||
if err != nil {
|
||||
t.Fatalf("compose: %v", err)
|
||||
}
|
||||
ctx, cancel := context.WithCancel(t.Context())
|
||||
started := make(chan error, 1)
|
||||
go func() { started <- srv.Start(ctx) }()
|
||||
t.Cleanup(func() {
|
||||
srv.Stop()
|
||||
cancel()
|
||||
select {
|
||||
case err := <-started:
|
||||
if err != nil {
|
||||
t.Errorf("srv.Start returned %v, want a clean stop", err)
|
||||
}
|
||||
case <-time.After(3 * time.Second):
|
||||
t.Error("srv.Start did not return after Stop")
|
||||
}
|
||||
})
|
||||
waitForState(t, srv, "email", status.StateRunning)
|
||||
waitForState(t, srv, "webhook", status.StateRunning)
|
||||
return srv
|
||||
}
|
||||
|
||||
// startChannels composes and starts email and webhook. Those two are the
|
||||
// channels a test can run without an external service, and a restart of one is
|
||||
// observable as the channel pointer changing.
|
||||
func startChannels(t *testing.T) *Service {
|
||||
t.Helper()
|
||||
emailAddr, webhookAddr := channelAddrs(t)
|
||||
return startComposed(t, deps{
|
||||
Config: resolveChannels(t, emailAddr, webhookAddr, "/a"),
|
||||
Log: slog.New(slog.DiscardHandler),
|
||||
Chat: &recordingChatContract{routes: make(chan messaging.Inbound, 1), reply: "ok"},
|
||||
})
|
||||
}
|
||||
|
||||
// TestApplyChannelSettingsRestartsOnlyTheChangedChannel is the bead's core
|
||||
// property: a change to one channel restarts that channel and leaves the others
|
||||
// serving. Before the per-channel supervisor this could only be a process
|
||||
// restart, which restarted every channel (archie-core-zfb0.4).
|
||||
func TestApplyChannelSettingsRestartsOnlyTheChangedChannel(t *testing.T) {
|
||||
srv := startChannels(t)
|
||||
webhookBefore := channelByID(t, srv, "webhook").current()
|
||||
emailBefore := channelByID(t, srv, "email").current()
|
||||
|
||||
next := srv.currentConfig()
|
||||
next.Webhook = config.WebhookRoute{Path: "/b"}
|
||||
if _, err := srv.applyChannelSettings(next); err != nil {
|
||||
t.Fatalf("applyChannelSettings: %v", err)
|
||||
}
|
||||
|
||||
if got := channelByID(t, srv, "webhook").current(); got == webhookBefore {
|
||||
t.Error("webhook did not restart after its route changed")
|
||||
}
|
||||
if got := channelByID(t, srv, "email").current(); got != emailBefore {
|
||||
t.Error("email restarted when only the webhook route changed")
|
||||
}
|
||||
}
|
||||
|
||||
// TestApplyChannelSettingsRefusesAListenAddressChange is the path where the
|
||||
// change must NOT apply: a bound listen address is a process binding, and
|
||||
// rebinding it in place is exactly the restart the epic keeps. The running
|
||||
// channel is left alone and the change is reported instead.
|
||||
func TestApplyChannelSettingsRefusesAListenAddressChange(t *testing.T) {
|
||||
srv := startChannels(t)
|
||||
before := channelByID(t, srv, "webhook").current()
|
||||
|
||||
next := srv.currentConfig()
|
||||
next.WebhookAddr = fmt.Sprintf("127.0.0.1:%d", freePort(t))
|
||||
_, err := srv.applyChannelSettings(next)
|
||||
if err == nil {
|
||||
t.Fatal("applyChannelSettings error = nil, want the listen-address change reported")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "webhook_addr") {
|
||||
t.Errorf("error = %v, want it to name chat.webhook_addr", err)
|
||||
}
|
||||
if got := channelByID(t, srv, "webhook").current(); got != before {
|
||||
t.Error("webhook restarted for a listen-address change; a bound address must not move in place")
|
||||
}
|
||||
}
|
||||
|
||||
// TestApplyChannelSettingsRestartsNothingWhenNothingChanged: the other half of
|
||||
// "only the channels whose settings changed" -- a tick that carries no change
|
||||
// must not restart a healthy channel.
|
||||
func TestApplyChannelSettingsRestartsNothingWhenNothingChanged(t *testing.T) {
|
||||
srv := startChannels(t)
|
||||
webhookBefore := channelByID(t, srv, "webhook").current()
|
||||
emailBefore := channelByID(t, srv, "email").current()
|
||||
|
||||
if _, err := srv.applyChannelSettings(srv.currentConfig()); err != nil {
|
||||
t.Fatalf("applyChannelSettings: %v", err)
|
||||
}
|
||||
if channelByID(t, srv, "webhook").current() != webhookBefore {
|
||||
t.Error("webhook restarted with no settings change")
|
||||
}
|
||||
if channelByID(t, srv, "email").current() != emailBefore {
|
||||
t.Error("email restarted with no settings change")
|
||||
}
|
||||
}
|
||||
|
||||
// fakeChannelSettings is the stored channel-settings resource a test owns. It
|
||||
// returns the document a real store would: the file's values with the stored
|
||||
// route layered over them, so the reconcile's layering is exercised rather than
|
||||
// bypassed.
|
||||
type fakeChannelSettings struct {
|
||||
mu sync.Mutex
|
||||
settings config.ChatConfig
|
||||
version int64
|
||||
}
|
||||
|
||||
func (f *fakeChannelSettings) RuntimeChatConfig(_ context.Context, base config.ChatConfig) (config.ChatConfig, int64, error) {
|
||||
f.mu.Lock()
|
||||
defer f.mu.Unlock()
|
||||
out := base
|
||||
out.Webhook = f.settings.Webhook
|
||||
return out, f.version, nil
|
||||
}
|
||||
|
||||
func (f *fakeChannelSettings) set(version int64, route config.WebhookRoute) {
|
||||
f.mu.Lock()
|
||||
defer f.mu.Unlock()
|
||||
f.version, f.settings.Webhook = version, route
|
||||
}
|
||||
|
||||
// recordingApplyStore records the apply-status writes the service makes.
|
||||
type recordingApplyStore struct {
|
||||
mu sync.Mutex
|
||||
writes []storecontract.ApplyStatus
|
||||
}
|
||||
|
||||
func (s *recordingApplyStore) PutApplyStatus(_ context.Context, st storecontract.ApplyStatus) error {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
s.writes = append(s.writes, st)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *recordingApplyStore) ListApplyStatus(context.Context) ([]storecontract.ApplyStatus, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (s *recordingApplyStore) appliedVersion() int64 {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
var version int64
|
||||
for _, st := range s.writes {
|
||||
version = max(version, st.AppliedVersion)
|
||||
}
|
||||
return version
|
||||
}
|
||||
|
||||
// TestReconcileAppliesANewStoredVersion drives the whole live path: the service
|
||||
// reads the stored channel settings on its interval, sees a newer version, and
|
||||
// restarts the channel it changed. The applied version reaches apply status, so
|
||||
// the dashboard stops showing the change as pending.
|
||||
func TestReconcileAppliesANewStoredVersion(t *testing.T) {
|
||||
emailAddr, webhookAddr := channelAddrs(t)
|
||||
source := &fakeChannelSettings{}
|
||||
source.set(1, config.WebhookRoute{Path: "/a"})
|
||||
store := &recordingApplyStore{}
|
||||
|
||||
srv := startComposed(t, deps{
|
||||
Config: resolveChannels(t, emailAddr, webhookAddr, "/a"),
|
||||
Log: slog.New(slog.DiscardHandler),
|
||||
Chat: &recordingChatContract{routes: make(chan messaging.Inbound, 1), reply: "ok"},
|
||||
SettingsSource: source,
|
||||
ApplyReporter: applystatus.New(applystatus.Messaging, store, slog.New(slog.DiscardHandler)),
|
||||
AppliedVersion: 1,
|
||||
ReconcileInterval: 5 * time.Millisecond,
|
||||
})
|
||||
|
||||
before := channelByID(t, srv, "webhook").current()
|
||||
source.set(2, config.WebhookRoute{Path: "/b"})
|
||||
|
||||
deadline := time.Now().Add(3 * time.Second)
|
||||
for time.Now().Before(deadline) {
|
||||
if channelByID(t, srv, "webhook").current() != before && store.appliedVersion() >= 2 {
|
||||
return
|
||||
}
|
||||
time.Sleep(5 * time.Millisecond)
|
||||
}
|
||||
t.Fatalf("after the stored version moved to 2: channel replaced=%v, applied version=%d; want both",
|
||||
channelByID(t, srv, "webhook").current() != before, store.appliedVersion())
|
||||
}
|
||||
@@ -36,6 +36,13 @@ func Run(ctx context.Context, o Options) error {
|
||||
// channelStatusStore is the same client, held where compose can reach it: the
|
||||
// channel report is published to the store this process already dials.
|
||||
var channelStatusStore storecontract.ChannelStatusStore
|
||||
// settingsSource and applyReporter are the live channel-settings path: the
|
||||
// control-plane client to re-read the resource, and the record of what this
|
||||
// process applied. Both stay nil without a State Store, which leaves the
|
||||
// service with no live path.
|
||||
var settingsSource chatSettingsSource
|
||||
var applyReporter *applystatus.Reporter
|
||||
var appliedVersion int64
|
||||
if cfg.Options.StateStore.Target != "" {
|
||||
stateStore, closeClient, dialErr := staterpc.Dial(cfg.Options.StateStore.Target, cfg.Options.StateStore.Token)
|
||||
if dialErr != nil {
|
||||
@@ -43,7 +50,8 @@ func Run(ctx context.Context, o Options) error {
|
||||
}
|
||||
closeStateStore = closeClient
|
||||
defer closeStateStore()
|
||||
chatSettings, channelVersion, settingsErr := controlplanerpc.NewRPCClient(stateStore.ControlPlane()).RuntimeChatConfig(ctx, config.ChatConfig{
|
||||
controlPlaneClient := controlplanerpc.NewRPCClient(stateStore.ControlPlane())
|
||||
chatSettings, channelVersion, settingsErr := controlPlaneClient.RuntimeChatConfig(ctx, config.ChatConfig{
|
||||
Telegram: cfg.Telegram, Email: cfg.Email, Webhook: cfg.Webhook, WebhookAddr: cfg.WebhookAddr, ShowToolCalls: cfg.ShowToolCalls,
|
||||
})
|
||||
if settingsErr != nil {
|
||||
@@ -64,15 +72,19 @@ func Run(ctx context.Context, o Options) error {
|
||||
}
|
||||
settings = messaging.NewSettingsCommand(messagingControlPlane{client: stateStore.ControlPlane()}).WithIdentities(stateStore)
|
||||
channelStatusStore = stateStore
|
||||
settingsSource, applyReporter, appliedVersion = controlPlaneClient, reporter, channelVersion
|
||||
}
|
||||
|
||||
srv, err := compose(ctx, deps{
|
||||
ChannelStatus: channelStatusStore,
|
||||
Config: cfg,
|
||||
Log: log,
|
||||
Chat: chat,
|
||||
Health: health,
|
||||
Settings: settings,
|
||||
ChannelStatus: channelStatusStore,
|
||||
Config: cfg,
|
||||
Log: log,
|
||||
Chat: chat,
|
||||
Health: health,
|
||||
Settings: settings,
|
||||
SettingsSource: settingsSource,
|
||||
ApplyReporter: applyReporter,
|
||||
AppliedVersion: appliedVersion,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
|
||||
@@ -0,0 +1,150 @@
|
||||
package archiemessaging
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/channels"
|
||||
)
|
||||
|
||||
// runChannel supervises one channel for the life of the service. It starts the
|
||||
// channel and, when the channel stops because a restart was requested, starts
|
||||
// the replacement. A channel that returns on its own is recorded and not
|
||||
// restarted, so a channel whose configuration cannot run does not spin.
|
||||
//
|
||||
// The supervisor is per channel rather than one loop over every channel
|
||||
// because a channel-settings change must restart exactly the channel whose
|
||||
// settings changed: cancelling one run context stops one channel and leaves the
|
||||
// rest serving (archie-core-zfb0.4).
|
||||
func (s *Service) runChannel(ctx context.Context, c *channelInstance) {
|
||||
defer s.wg.Done()
|
||||
for {
|
||||
c.mu.Lock()
|
||||
ch := c.channel
|
||||
runCtx, cancel := context.WithCancel(ctx)
|
||||
c.cancel = cancel
|
||||
c.mu.Unlock()
|
||||
|
||||
s.log.Info("starting channel", "name", c.name)
|
||||
err := ch.Start(runCtx, s.chat, s.lifecycleFor(c.name))
|
||||
|
||||
// Decide the next state under the same lock a restart takes, so a
|
||||
// restart racing this exit is either seen here (restartPending) or
|
||||
// observed as a channel that is no longer supervised.
|
||||
c.mu.Lock()
|
||||
c.cancel = nil
|
||||
shutdown := ctx.Err() != nil
|
||||
restart := c.restartPending
|
||||
if !shutdown && restart {
|
||||
c.restartPending = false
|
||||
}
|
||||
if shutdown || !restart {
|
||||
c.supervised = false
|
||||
}
|
||||
c.mu.Unlock()
|
||||
cancel()
|
||||
|
||||
// Stop is safe to call after the run context was cancelled: each
|
||||
// channel's Start returns having closed what it opened, and Stop is the
|
||||
// idempotent tail for the paths that leave a listener behind.
|
||||
s.stopChannel(ctx, c.name, ch)
|
||||
|
||||
if shutdown {
|
||||
if err != nil && !errors.Is(err, context.Canceled) {
|
||||
s.status.MarkFailed(c.name, err.Error())
|
||||
} else {
|
||||
s.status.MarkStopped(c.name, "")
|
||||
}
|
||||
return
|
||||
}
|
||||
if restart {
|
||||
// The replacement is already in c.channel (see restartChannel); the
|
||||
// next pass starts it.
|
||||
continue
|
||||
}
|
||||
// The channel stopped on its own.
|
||||
if err != nil {
|
||||
s.status.MarkFailed(c.name, err.Error())
|
||||
s.log.Error("channel stopped with error", "name", c.name, "err", err)
|
||||
} else {
|
||||
s.status.MarkStopped(c.name, "")
|
||||
}
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// restartChannel stops the running channel id and starts a replacement built
|
||||
// from next. The replacement is built before the running channel is touched, so
|
||||
// a configuration this build cannot run leaves the running channel serving
|
||||
// (the validate-before-switch rule in docs/prds/runtime-control-plane.md).
|
||||
//
|
||||
// A channel that is not currently supervised is not restarted: it already
|
||||
// stopped on its own, and resurrecting it here would race that exit. The caller
|
||||
// reports it.
|
||||
func (s *Service) restartChannel(id string, next ResolvedConfig) error {
|
||||
c := s.instanceByID(id)
|
||||
if c == nil {
|
||||
return fmt.Errorf("no channel %q", id)
|
||||
}
|
||||
if c.rebuild == nil {
|
||||
return fmt.Errorf("channel %q cannot be rebuilt", id)
|
||||
}
|
||||
replacement, err := c.rebuild(next)
|
||||
if err != nil {
|
||||
return fmt.Errorf("rebuild %q: %w", id, err)
|
||||
}
|
||||
if replacement == nil {
|
||||
return fmt.Errorf("channel %q has no configuration", id)
|
||||
}
|
||||
c.mu.Lock()
|
||||
if !c.supervised {
|
||||
c.mu.Unlock()
|
||||
return fmt.Errorf("channel %q is not running", id)
|
||||
}
|
||||
c.channel = replacement
|
||||
c.restartPending = true
|
||||
cancel := c.cancel
|
||||
c.mu.Unlock()
|
||||
if cancel != nil {
|
||||
cancel()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// stopChannel closes one channel's listener with its own bounded context, so a
|
||||
// stop that hangs cannot hold the shutdown past the configured timeout. The
|
||||
// parent context is stripped of its cancellation because this runs on the
|
||||
// shutdown path, where the parent is already cancelled.
|
||||
func (s *Service) stopChannel(ctx context.Context, name string, ch channels.Channel) {
|
||||
timeout := s.currentConfig().Options.ShutdownTimeout
|
||||
if timeout <= 0 {
|
||||
timeout = defaultShutdownTimeout
|
||||
}
|
||||
stopCtx, cancel := context.WithTimeout(context.WithoutCancel(ctx), timeout)
|
||||
defer cancel()
|
||||
if err := ch.Stop(stopCtx); err != nil {
|
||||
s.log.Warn("channel stop failed", "name", name, "err", err)
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Service) instanceByID(id string) *channelInstance {
|
||||
for _, c := range s.channels {
|
||||
if c.name == id {
|
||||
return c
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Service) currentConfig() ResolvedConfig {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
return s.cfg
|
||||
}
|
||||
|
||||
func (s *Service) setConfig(cfg ResolvedConfig) {
|
||||
s.mu.Lock()
|
||||
s.cfg = cfg
|
||||
s.mu.Unlock()
|
||||
}
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
"path/filepath"
|
||||
"time"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/buildinfo"
|
||||
"github.com/samcharles93/archie-core/internal/channels/telegram"
|
||||
"github.com/samcharles93/archie-core/internal/domain/messaging"
|
||||
"github.com/samcharles93/archie-core/internal/installtype"
|
||||
@@ -17,13 +18,15 @@ import (
|
||||
// configureTelegram wires the operator seams telegram.Gateway leaves to its
|
||||
// composition root. Which process may satisfy each one is settled in
|
||||
// docs/architecture/migration-decisions.md ("Telegram operator surface after
|
||||
// extraction"); notably RunningVersions, ReleaseAnnouncements and Dangerous
|
||||
// stay nil deliberately, because nothing this process can observe would make
|
||||
// them true.
|
||||
// extraction"); notably ReleaseAnnouncements and Dangerous stay nil, because
|
||||
// nothing this process can observe would make them true. RunningVersions is
|
||||
// supplied here from this process's own build stamp; see
|
||||
// messagingRunningVersions.
|
||||
func configureTelegram(ctx context.Context, g *telegram.Gateway, cfg ResolvedConfig, chat messaging.ChatContract, log *slog.Logger) {
|
||||
g.Version = gatewayVersionReporter(ctx, chat, cfg.Options.DependencyTimeout)
|
||||
g.SetShowToolCalls(cfg.ShowToolCalls)
|
||||
g.Reload = telegramReloader(cfg.Options, log)
|
||||
g.RunningVersions = messagingRunningVersions
|
||||
|
||||
if updates := updateService(cfg); updates != nil {
|
||||
g.Updates = updates
|
||||
@@ -33,6 +36,28 @@ func configureTelegram(ctx context.Context, g *telegram.Gateway, cfg ResolvedCon
|
||||
}
|
||||
}
|
||||
|
||||
// messagingRunningVersions reports the component versions this process can
|
||||
// vouch for, for checking a relayed update report against
|
||||
// (releaseupdate.Report.Verify).
|
||||
//
|
||||
// archie-messaging is one of the processes in the archied release set (the
|
||||
// installer's GATEWAY_SERVICES), built from the same archied/v* tag and stamped
|
||||
// with buildinfo.Version by every build site (install.sh, the updater, the
|
||||
// release workflow and Taskfile). Its own compiled-in build is therefore
|
||||
// evidence that the release the installer claims actually reached this host: a
|
||||
// binary the update did not replace still reports the version it was built
|
||||
// from, which Verify reads as drift rather than false success. Reading the
|
||||
// version from the Gateway instead would report a different process's stamp,
|
||||
// which is the failure mode the check exists to catch (see
|
||||
// docs/architecture/migration-decisions.md).
|
||||
//
|
||||
// The agent is deliberately absent: every archie-agent process is task-scoped
|
||||
// and observed only through daemon.AgentStatus, which this process does not
|
||||
// hold, so it must report Unverified rather than be guessed.
|
||||
func messagingRunningVersions() map[string]string {
|
||||
return map[string]string{releaseupdate.ComponentDaemon: buildinfo.Version}
|
||||
}
|
||||
|
||||
// gatewayVersionReporter renders /version from the Gateway's own build block.
|
||||
// It is read per call rather than captured at compose so an upgraded Gateway
|
||||
// is reported without restarting this process, and bounded by the same
|
||||
|
||||
@@ -8,9 +8,11 @@ import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/buildinfo"
|
||||
"github.com/samcharles93/archie-core/internal/channels/telegram"
|
||||
"github.com/samcharles93/archie-core/internal/config"
|
||||
"github.com/samcharles93/archie-core/internal/domain/messaging"
|
||||
"github.com/samcharles93/archie-core/internal/releaseupdate"
|
||||
)
|
||||
|
||||
type versionChatContract struct {
|
||||
@@ -84,17 +86,32 @@ func TestTelegramVersionReportsAnUnreachableGateway(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// TestTelegramRunningVersionsStaysUnwired pins the decision recorded in
|
||||
// docs/architecture/migration-decisions.md: RunningVersions turns an
|
||||
// installer's claim into a checked one, and only a component's own compiled-in
|
||||
// build can vouch for itself. This process knows neither archied's build nor
|
||||
// the observed archie-agent version, so reporting anything here would
|
||||
// manufacture the false success the check exists to catch.
|
||||
func TestTelegramRunningVersionsStaysUnwired(t *testing.T) {
|
||||
// TestTelegramRunningVersionsReportsTheMessagingBuild pins that the Messaging
|
||||
// Service vouches for the daemon component from its own compiled-in release
|
||||
// stamp. Every host binary in the archied release set -- archie-messaging
|
||||
// included -- is built from the same archied/v* tag and stamped with
|
||||
// buildinfo.Version by every build site (install.sh, the updater, the release
|
||||
// workflow and Taskfile), so this process's own build is evidence that the
|
||||
// release actually reached the host, and an old binary still running reports
|
||||
// the old version as drift. Reading it from the Gateway instead would be a
|
||||
// different process's stamp, which is the false success the check exists to
|
||||
// catch.
|
||||
//
|
||||
// It reports nothing for the agent: every archie-agent process is task-scoped
|
||||
// and observed only through daemon.AgentStatus, which this process does not
|
||||
// hold. Leaving it out makes it Unverified rather than guessed.
|
||||
func TestTelegramRunningVersionsReportsTheMessagingBuild(t *testing.T) {
|
||||
gateway := telegramConfig(t, ResolvedConfig{}, &versionChatContract{})
|
||||
|
||||
if gateway.RunningVersions != nil {
|
||||
t.Fatal("telegram RunningVersions is wired; update reports must stay unverified claims")
|
||||
if gateway.RunningVersions == nil {
|
||||
t.Fatal("telegram RunningVersions = nil, want the Messaging Service's own release stamp")
|
||||
}
|
||||
running := gateway.RunningVersions()
|
||||
if got := running[releaseupdate.ComponentDaemon]; got != buildinfo.Version {
|
||||
t.Fatalf("running[%q] = %q, want buildinfo.Version = %q", releaseupdate.ComponentDaemon, got, buildinfo.Version)
|
||||
}
|
||||
if _, ok := running[releaseupdate.ComponentAgent]; ok {
|
||||
t.Fatalf("running vouches for %q, which this process cannot observe: %#v", releaseupdate.ComponentAgent, running)
|
||||
}
|
||||
if gateway.Dangerous != nil {
|
||||
t.Fatal("telegram Dangerous is wired; no composition has ever supplied a command authority")
|
||||
|
||||
@@ -113,6 +113,15 @@ func compose(d deps) *webui.Server {
|
||||
if bindings, ok := d.Store.(storecontract.BindingStore); ok {
|
||||
srv.Bindings = bindings
|
||||
}
|
||||
// Harness OAuth token sets are a ratified State Store contract, and the
|
||||
// same client already carries them. Withholding it would leave the
|
||||
// harness page answering 503 on a deployment that has the store
|
||||
// (archie-core-egkf.7). The setup terminal itself is not wired here: its
|
||||
// implementation owns a container, which this process must not link, so
|
||||
// the route stays a documented 503 until a daemon-side contract exists.
|
||||
if secrets, ok := d.Store.(storecontract.HarnessSecretStore); ok {
|
||||
srv.HarnessSecrets = secrets
|
||||
}
|
||||
if eventTypes, ok := d.Store.(storecontract.EventTypeStore); ok {
|
||||
srv.EventTypes = eventTypes
|
||||
}
|
||||
|
||||
@@ -105,6 +105,19 @@ func TestRoutesWithoutAContractDegradeExplicitly(t *testing.T) {
|
||||
name: "capture inspector", method: http.MethodGet, path: "/api/captures",
|
||||
want: http.StatusOK, emptyJSON: `"enabled":true`,
|
||||
},
|
||||
{
|
||||
// The harness page's two routes: the bindings read needs the
|
||||
// control-plane resource and the setup terminal needs a transport
|
||||
// this process cannot own. In this composition neither is wired,
|
||||
// so both degrade explicitly rather than rendering an empty page
|
||||
// (archie-core-egkf.7).
|
||||
name: "harness bindings", method: http.MethodGet, path: "/api/harness/bindings",
|
||||
want: http.StatusServiceUnavailable,
|
||||
},
|
||||
{
|
||||
name: "harness setup terminal", method: http.MethodGet, path: "/api/harness/terminal?profile=claude-kit",
|
||||
want: http.StatusServiceUnavailable,
|
||||
},
|
||||
{
|
||||
// The browser reads this to decide which sections to show at
|
||||
// all, so it is the one route that must answer here.
|
||||
|
||||
@@ -72,11 +72,11 @@ func (f *fakeChat) Cancel(context.Context, string) (gateway.ChatCancellation, er
|
||||
|
||||
func (f *fakeChat) SetPersona(context.Context, string, string) (bool, error) { return false, nil }
|
||||
|
||||
func (f *fakeChat) ApplyTaskAction(context.Context, string, int64, taskstate.Action) (gateway.TaskActionResult, error) {
|
||||
func (f *fakeChat) ApplyTaskAction(context.Context, string, int64, taskstate.Action, taskactions.ActionPayload) (gateway.TaskActionResult, error) {
|
||||
return gateway.TaskActionResult{}, nil
|
||||
}
|
||||
|
||||
func (f *fakeChat) ApplyOperatorTaskAction(context.Context, taskactions.Actor, int64, taskstate.Action) (gateway.TaskActionResult, error) {
|
||||
func (f *fakeChat) ApplyOperatorTaskAction(context.Context, taskactions.Actor, int64, taskstate.Action, taskactions.ActionPayload) (gateway.TaskActionResult, error) {
|
||||
return gateway.TaskActionResult{}, nil
|
||||
}
|
||||
|
||||
|
||||
@@ -197,11 +197,11 @@ func TestRegistryRoutesAndValidatesDefinitions(t *testing.T) {
|
||||
found := false
|
||||
for _, descriptor := range catalog.Resources {
|
||||
if descriptor.Kind == ContainerRuntimePoliciesKind {
|
||||
found = descriptor.ApplyMode == "restart-required"
|
||||
found = descriptor.ApplyMode == "live"
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
t.Fatal("container runtime policies missing restart-required descriptor")
|
||||
t.Fatal("container runtime policies missing live descriptor")
|
||||
}
|
||||
|
||||
_, err = server.Command(t.Context(), &pb.CommandRequest{Kind: ContainerRuntimePoliciesKind, Command: "replace", ValueJson: []byte(`{"pull_policy":"sometimes"}`), Actor: "test", Source: "test", RequestId: "bad", ExpectedVersion: 1})
|
||||
|
||||
@@ -0,0 +1,80 @@
|
||||
package controlplane
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// The dashboard mirrors three of this package's validators by hand
|
||||
// (ui/src/settings/validation.ts), and the model-role rule had already drifted:
|
||||
// the dashboard required `^[^/\s]+\/\S+$` while the server only asks for a
|
||||
// slash, so the dashboard refused values this package accepts. The fixture at
|
||||
// testdata/dashboard-validators.json is the tie: this test runs the real server
|
||||
// validators over it, and ui/test/control-plane-validator-parity.test.ts runs
|
||||
// the real dashboard validators over the same cases. Changing a rule on one
|
||||
// side alone fails one of the two tests (archie-core-ui-dashboard-3).
|
||||
type dashboardValidatorFixture struct {
|
||||
Description string `json:"description"`
|
||||
Cases map[string][]dashboardValidatorCase `json:"cases"`
|
||||
}
|
||||
|
||||
type dashboardValidatorCase struct {
|
||||
Why string `json:"why"`
|
||||
Valid bool `json:"valid"`
|
||||
Value json.RawMessage `json:"value"`
|
||||
}
|
||||
|
||||
func TestDashboardValidatorsAgreeWithTheSharedFixture(t *testing.T) {
|
||||
raw, err := os.ReadFile(filepath.Join("testdata", "dashboard-validators.json"))
|
||||
if err != nil {
|
||||
t.Fatalf("read the shared validator fixture: %v", err)
|
||||
}
|
||||
var fixture dashboardValidatorFixture
|
||||
if err := json.Unmarshal(raw, &fixture); err != nil {
|
||||
t.Fatalf("decode the shared validator fixture: %v", err)
|
||||
}
|
||||
if fixture.Description == "" {
|
||||
t.Error("the fixture has no description; it is the contract the dashboard mirror is read against")
|
||||
}
|
||||
|
||||
// Each entry is the real entry point a Definition wires into Decode, keyed
|
||||
// to match the fixture (and the dashboard's own validator map).
|
||||
validators := map[string]func([]byte) error{
|
||||
ModelRoleAssignmentsKind: validateModelRoles,
|
||||
SchedulingPolicyKind: validateScheduling,
|
||||
SchedulesKind: func(input []byte) error { return validateAs(input, validateSchedules) },
|
||||
}
|
||||
|
||||
for kind, validate := range validators {
|
||||
cases, ok := fixture.Cases[kind]
|
||||
if !ok {
|
||||
t.Errorf("fixture has no cases for %s, so nothing pins the dashboard mirror of it", kind)
|
||||
continue
|
||||
}
|
||||
var accepted, refused int
|
||||
for _, testCase := range cases {
|
||||
err := validate(testCase.Value)
|
||||
switch {
|
||||
case testCase.Valid && err != nil:
|
||||
t.Errorf("%s: fixture case %q is valid, but the server refused it: %v", kind, testCase.Why, err)
|
||||
case !testCase.Valid && err == nil:
|
||||
t.Errorf("%s: fixture case %q is invalid, but the server accepted it", kind, testCase.Why)
|
||||
case testCase.Valid:
|
||||
accepted++
|
||||
default:
|
||||
refused++
|
||||
}
|
||||
}
|
||||
if accepted == 0 || refused == 0 {
|
||||
t.Errorf("%s: the fixture must pin both an accepted and a refused case (accepted=%d refused=%d)", kind, accepted, refused)
|
||||
}
|
||||
}
|
||||
|
||||
for kind := range fixture.Cases {
|
||||
if _, ok := validators[kind]; !ok {
|
||||
t.Errorf("fixture names %s, which this package has no validator for", kind)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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), workflowEnablementDefinition(), personaDefinition(), scheduleDefinition(), agentProfileDefinition(), credentialBindingsDefinition()}
|
||||
definitions := []Definition{workflowDefinition(), workflowDefinitionsDefinition(steps), workflowEnablementDefinition(), personaDefinition(), scheduleDefinition(), agentProfileDefinition(), credentialBindingsDefinition(), reviewSettingsDefinition()}
|
||||
definitions = append(definitions, modelDefinitions()...)
|
||||
definitions = append(definitions, operationalDefinitions()...)
|
||||
return definitions
|
||||
|
||||
@@ -41,15 +41,33 @@ type pluginSettings struct {
|
||||
func operationalDefinitions() []Definition {
|
||||
return []Definition{
|
||||
{Kind: RepositoryPoliciesKind, Title: "Repository policies", ApplyMode: "live", Document: []config.Repo{}, Seed: func(cfg config.Config) any { return cfg.Repos }, Validate: validateRepositories},
|
||||
{Kind: ChannelSettingsKind, Title: "Channel settings", ApplyMode: "restart-required", Document: channelSettings{}, Seed: seedChannels, Validate: validateChannels, Normalize: normalizeChannels},
|
||||
{Kind: ChannelSettingsKind, Title: "Channel settings", ApplyMode: "live", Document: channelSettings{}, Seed: seedChannels, Validate: validateChannels, Normalize: normalizeChannels},
|
||||
{Kind: SchedulingPolicyKind, Title: "Scheduling policy", ApplyMode: "live", Document: schedulingPolicy{}, Seed: func(cfg config.Config) any {
|
||||
return schedulingPolicy{PollInterval: cfg.PollInterval.Std().String(), MaxRetries: cfg.MaxRetries, Label: &cfg.Label, Dispatch: cfg.Dispatch}
|
||||
}, Validate: validateScheduling},
|
||||
{Kind: ToolSettingsKind, Title: "Tool and MCP settings", ApplyMode: "restart-required", Document: toolSettings{}, Seed: seedTools, Validate: validateTools},
|
||||
{Kind: PluginSettingsKind, Title: "Plugin settings", ApplyMode: "restart-required", Document: pluginSettings{}, Seed: func(cfg config.Config) any {
|
||||
// ApplyMode is live: the daemon diffs the stored MCP server set against
|
||||
// the running providers -- connecting added servers, disconnecting
|
||||
// removed ones and reconnecting changed ones while leaving unchanged
|
||||
// servers untouched -- and rebuilds the web_fetch and minimax entries
|
||||
// (archie-core-zfb0.3). A changed server whose new engine fails to start
|
||||
// is rolled back to the old one, so the refusal is reported rather than
|
||||
// leaving the server down.
|
||||
{Kind: ToolSettingsKind, Title: "Tool and MCP settings", ApplyMode: "live", Document: toolSettings{}, Seed: seedTools, Validate: validateTools},
|
||||
// ApplyMode is live for additions: a stored directory change re-layers live
|
||||
// and the daemon's reconciliation loads new and changed files without a
|
||||
// restart. A removal cannot unload Yaegi's interpreter, so it stays an
|
||||
// outstanding apply-status problem rather than a restart of the whole kind
|
||||
// (docs/prds/plugin-settings-live.md).
|
||||
{Kind: PluginSettingsKind, Title: "Plugin settings", ApplyMode: "live", Document: pluginSettings{}, Seed: func(cfg config.Config) any {
|
||||
return pluginSettings{cfg.PluginDir, cfg.ModuleDir, cfg.SecretEngineDir, cfg.SkillsDir}
|
||||
}, Validate: validatePluginSettings},
|
||||
{Kind: ContainerRuntimePoliciesKind, Title: "Container runtime policies", ApplyMode: "restart-required", Document: containerRuntimePolicies{}, Seed: seedContainerPolicies, Validate: validateContainers, Normalize: normalizeContainerPolicies},
|
||||
// ApplyMode is live: the container pool reads the published config on
|
||||
// every acquire (image, pull policy, network, max uptime, concurrency
|
||||
// cap) and the daemon resizes its running dispatcher after the publish,
|
||||
// so neither a deferred container nor a queued task needs a restart
|
||||
// (archie-core-zfb0.2). RegistryAuth is file-owned and never part of
|
||||
// this document, so it stays a boot-resolved value.
|
||||
{Kind: ContainerRuntimePoliciesKind, Title: "Container runtime policies", ApplyMode: "live", Document: containerRuntimePolicies{}, Seed: seedContainerPolicies, Validate: validateContainers, Normalize: normalizeContainerPolicies},
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
package controlplane
|
||||
|
||||
import (
|
||||
"github.com/samcharles93/archie-core/internal/config"
|
||||
)
|
||||
|
||||
// ReviewSettingsKind is the control-plane resource holding the pr-review
|
||||
// workflow's two dials: whether a post-worthiness pass runs before findings are
|
||||
// posted, and whether a human approves the draft before it posts. The file's
|
||||
// [review] section seeds it; the stored document outranks the file from then on
|
||||
// (docs/prds/review-settings-resource.md).
|
||||
const ReviewSettingsKind = "review-settings"
|
||||
|
||||
type reviewSettings struct {
|
||||
PrecisionGate bool `json:"precision_gate" title:"Precision gate" doc:"Run a post-worthiness pass before findings are posted."`
|
||||
ApproveBeforePost bool `json:"approve_before_post" title:"Approve before posting" doc:"Park a finished review for operator approval before it posts."`
|
||||
}
|
||||
|
||||
func reviewSettingsDefinition() Definition {
|
||||
return Definition{
|
||||
Kind: ReviewSettingsKind,
|
||||
Title: "Review settings",
|
||||
ApplyMode: "live",
|
||||
Document: reviewSettings{},
|
||||
Seed: func(cfg config.Config) any {
|
||||
return reviewSettings{PrecisionGate: cfg.Review.PrecisionGate, ApproveBeforePost: cfg.Review.ApproveBeforePost}
|
||||
},
|
||||
Validate: validateReviewSettings,
|
||||
}
|
||||
}
|
||||
|
||||
// validateReviewSettings accepts every value, deliberately: both dials are free
|
||||
// booleans with no cross-field rule, and a stored value reaches its consumers
|
||||
// through the same layered cfg.Review boot validates. It stays a real function
|
||||
// rather than an inline no-op so a rule, if one is ever settled, has one
|
||||
// obvious home (validatePluginSettings is the precedent).
|
||||
func validateReviewSettings(input []byte) error {
|
||||
return validateAs(input, func(reviewSettings) error { return nil })
|
||||
}
|
||||
@@ -160,6 +160,21 @@ func runtimeConfigFrom(ctx context.Context, reader controlplanerpc.ResourceReade
|
||||
}); err != nil {
|
||||
return config.Config{}, nil, err
|
||||
}
|
||||
// review-settings owns the pr-review dials once the store carries a value.
|
||||
// The two fields are assigned individually, not `out.Review = ...`: a field
|
||||
// added to config.Review later keeps the file's value until the stored
|
||||
// document carries it (docs/prds/review-settings-resource.md).
|
||||
if err := layerResource(ctx, reader, versions, ReviewSettingsKind, func(value []byte) error {
|
||||
var settings reviewSettings
|
||||
if err := json.Unmarshal(value, &settings); err != nil {
|
||||
return err
|
||||
}
|
||||
out.Review.PrecisionGate = settings.PrecisionGate
|
||||
out.Review.ApproveBeforePost = settings.ApproveBeforePost
|
||||
return nil
|
||||
}); err != nil {
|
||||
return config.Config{}, nil, err
|
||||
}
|
||||
return runtimeToolConfigFrom(ctx, reader, versions, out)
|
||||
}
|
||||
|
||||
@@ -213,9 +228,16 @@ func runtimeToolConfigFrom(ctx context.Context, reader controlplanerpc.ResourceR
|
||||
// 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.
|
||||
//
|
||||
// RegistryAuth is the same shape of file-owned field, with no resource of
|
||||
// its own to restore it later the way CredentialBindingsKind restores
|
||||
// Credentials: the document cannot carry a secret reference, so it has to
|
||||
// survive this assignment or the daemon pulls anonymously again.
|
||||
profiles := out.Containers.Profiles
|
||||
registryAuth := out.Containers.RegistryAuth
|
||||
out.Containers = policies.settings()
|
||||
out.Containers.Profiles = profiles
|
||||
out.Containers.RegistryAuth = registryAuth
|
||||
return nil
|
||||
}); err != nil {
|
||||
return config.Config{}, nil, err
|
||||
|
||||
@@ -537,7 +537,7 @@ func populateEveryField(t *testing.T, value reflect.Value, label string) {
|
||||
// TestSchedulingPolicySeedCarriesTheLabel: the seed a fresh store writes must
|
||||
// carry the file document's label, so the pairing a stored trigger requires
|
||||
// is judgeable at write time from the moment the resource exists.
|
||||
// TestRuntimeResourceKindsApplyLive pins the four kinds the daemon re-layers
|
||||
// TestRuntimeResourceKindsApplyLive pins the kinds the daemon re-layers
|
||||
// live (archie-core-zfb0.1) and the ones a startup-built consumer still
|
||||
// freezes, so a flip in either direction is a deliberate edit to the
|
||||
// definition, not a default that drifts.
|
||||
@@ -553,17 +553,17 @@ func TestRuntimeResourceKindsApplyLive(t *testing.T) {
|
||||
modes[definition.Kind] = definition.ApplyMode
|
||||
}
|
||||
for _, kind := range []string{
|
||||
ProviderSettingsKind, ModelRoleAssignmentsKind, RepositoryPoliciesKind, SchedulingPolicyKind, AgentProfileKind, CredentialBindingsKind,
|
||||
ProviderSettingsKind, ModelRoleAssignmentsKind, RepositoryPoliciesKind, SchedulingPolicyKind, AgentProfileKind, CredentialBindingsKind, ToolSettingsKind, PluginSettingsKind, ReviewSettingsKind, ChannelSettingsKind, ContainerRuntimePoliciesKind,
|
||||
} {
|
||||
if modes[kind] != "live" {
|
||||
t.Errorf("%s applies %q, want live: the daemon re-layers this kind on a watch", kind, modes[kind])
|
||||
}
|
||||
}
|
||||
for _, kind := range []string{ToolSettingsKind, PluginSettingsKind, ContainerRuntimePoliciesKind, ChannelSettingsKind} {
|
||||
if modes[kind] != "restart-required" {
|
||||
t.Errorf("%s applies %q, want restart-required: a startup-built component still holds it", kind, modes[kind])
|
||||
}
|
||||
}
|
||||
// No control-plane kind is restart-required any more: every kind's consumer
|
||||
// either re-reads the published config or reconciles on its own process's
|
||||
// watch. The process bindings that would be restart-required (listen
|
||||
// addresses, the NATS and database URLs, the state and work directories)
|
||||
// are not control-plane resource kinds at all.
|
||||
}
|
||||
|
||||
func TestSchedulingPolicySeedCarriesTheLabel(t *testing.T) {
|
||||
@@ -660,6 +660,33 @@ func TestRuntimeConfigLayersStoredProfiles(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// A stored container-runtime-policies document replaces the file's [containers]
|
||||
// section wholesale, so a file-owned field that document does not carry -- the
|
||||
// registry credential resolved at boot -- must be preserved across the
|
||||
// assignment or it is silently dropped and every private-registry pull is
|
||||
// anonymous again.
|
||||
func TestRuntimeConfigPreservesFileOwnedRegistryAuth(t *testing.T) {
|
||||
base := config.Config{Containers: config.ContainerConfig{
|
||||
Image: "agent:1",
|
||||
RegistryAuth: config.SecretRef{Engine: "env", Key: "REGISTRY_AUTH"},
|
||||
}}
|
||||
|
||||
client := NewRPCClient(&runtimeConfigClient{values: map[string]any{
|
||||
SchedulingPolicyKind: map[string]any{"poll_interval": "2m", "dispatch": map[string]any{"trigger": "assignee"}},
|
||||
ContainerRuntimePoliciesKind: map[string]any{"image": "agent:2", "pull_policy": "always"},
|
||||
}})
|
||||
got, _, err := client.RuntimeConfig(t.Context(), base)
|
||||
if err != nil {
|
||||
t.Fatalf("RuntimeConfig: %v", err)
|
||||
}
|
||||
if got.Containers.Image != "agent:2" {
|
||||
t.Fatalf("image = %q, want the stored document's", got.Containers.Image)
|
||||
}
|
||||
if want := (config.SecretRef{Engine: "env", Key: "REGISTRY_AUTH"}); got.Containers.RegistryAuth != want {
|
||||
t.Fatalf("registry_auth = %+v, want the file's %+v preserved across the container-policies layering", got.Containers.RegistryAuth, want)
|
||||
}
|
||||
}
|
||||
|
||||
// A stored CredentialBindingsKind value replaces the file's bindings
|
||||
// outright; no stored value at all leaves the file's in effect (the same
|
||||
// shape TestRuntimeConfigLayersStoredProfiles pins for AgentProfileKind, for
|
||||
@@ -698,3 +725,85 @@ func TestRuntimeConfigLayersStoredCredentialBindings(t *testing.T) {
|
||||
t.Fatalf("layering mutated the file document's credentials: %v", got)
|
||||
}
|
||||
}
|
||||
|
||||
// reviewSettingsKindWire is the kind the review-settings resource is addressed
|
||||
// by. It is deliberately the literal wire string rather than the package
|
||||
// constant so this test can state the contract before the constant exists, and
|
||||
// so a rename of the constant that the layering does not follow fails here
|
||||
// rather than silently testing a different kind.
|
||||
const reviewSettingsKindWire = "review-settings"
|
||||
|
||||
// The file document's [review] section seeds the review-settings kind, so a
|
||||
// deployment that never edited the dials runs the values in config.toml
|
||||
// (docs/prds/review-settings-resource.md, "Seeding").
|
||||
func TestReviewSettingsSeedCarriesBothDials(t *testing.T) {
|
||||
definition, ok := testServer(t, nil).definitions[reviewSettingsKindWire]
|
||||
if !ok {
|
||||
t.Fatalf("kind %q is not a control-plane resource", reviewSettingsKindWire)
|
||||
}
|
||||
seed, err := definition.seededValue(config.Config{Review: config.Review{PrecisionGate: true, ApproveBeforePost: false}})
|
||||
if err != nil {
|
||||
t.Fatalf("seededValue: %v", err)
|
||||
}
|
||||
var document map[string]any
|
||||
if err := json.Unmarshal(seed, &document); err != nil {
|
||||
t.Fatalf("decode seed: %v", err)
|
||||
}
|
||||
if document["precision_gate"] != true || document["approve_before_post"] != false {
|
||||
t.Fatalf("seed = %s, want the file document's precision_gate/approve_before_post", seed)
|
||||
}
|
||||
}
|
||||
|
||||
// A stored review-settings document outranks the file's [review] from then on,
|
||||
// and the layered value reaches a run through cfg.Review -- what
|
||||
// TaskContext.Cfg carries into stagePRPrecisionGate and
|
||||
// stagePROperatorApproval. The assertion is on cfg.Review after layering, not
|
||||
// on a struct the test set directly.
|
||||
func TestRuntimeConfigLayersStoredReviewSettings(t *testing.T) {
|
||||
base := config.Config{Review: config.Review{PrecisionGate: true, ApproveBeforePost: false}}
|
||||
|
||||
t.Run("a stored document outranks the file", func(t *testing.T) {
|
||||
got, versions, err := runtimeConfigFrom(t.Context(), reviewStoreReader{value: []byte(`{"precision_gate":false,"approve_before_post":true}`)}, base)
|
||||
if err != nil {
|
||||
t.Fatalf("runtimeConfigFrom: %v", err)
|
||||
}
|
||||
if want := (config.Review{PrecisionGate: false, ApproveBeforePost: true}); got.Review != want {
|
||||
t.Fatalf("cfg.Review = %+v, want the stored document %+v", got.Review, want)
|
||||
}
|
||||
if versions[reviewSettingsKindWire] != 7 {
|
||||
t.Fatalf("versions[%s] = %d, want the version the store answered with", reviewSettingsKindWire, versions[reviewSettingsKindWire])
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("no stored value keeps the file's", func(t *testing.T) {
|
||||
got, _, err := runtimeConfigFrom(t.Context(), absentReader{}, base)
|
||||
if err != nil {
|
||||
t.Fatalf("runtimeConfigFrom: %v", err)
|
||||
}
|
||||
if want := (config.Review{PrecisionGate: true, ApproveBeforePost: false}); got.Review != want {
|
||||
t.Fatalf("cfg.Review = %+v, want the file's %+v", got.Review, want)
|
||||
}
|
||||
})
|
||||
|
||||
if base.Review != (config.Review{PrecisionGate: true, ApproveBeforePost: false}) {
|
||||
t.Fatalf("layering mutated the file document's review settings: %+v", base.Review)
|
||||
}
|
||||
}
|
||||
|
||||
// reviewStoreReader answers only the review-settings kind, so this test does
|
||||
// not have to supply every other kind's document to reach the layering it
|
||||
// exercises.
|
||||
type reviewStoreReader struct{ value []byte }
|
||||
|
||||
func (r reviewStoreReader) Query(_ context.Context, kind string, decode func([]byte) error) (int64, bool, error) {
|
||||
if kind != reviewSettingsKindWire {
|
||||
return 0, false, nil
|
||||
}
|
||||
if r.value == nil {
|
||||
return 0, false, nil
|
||||
}
|
||||
if err := decode(r.value); err != nil {
|
||||
return 0, false, err
|
||||
}
|
||||
return 7, true, nil
|
||||
}
|
||||
@@ -0,0 +1,169 @@
|
||||
{
|
||||
"description": "Shared accept/reject fixtures for the control-plane validators the dashboard hand-mirrors in ui/src/settings/validation.ts. The control plane is the authority: every case's `valid` is what internal/app/controlplane accepts or refuses. The Go test in this package runs the real server validators over these cases; the node test ui/test/control-plane-validator-parity.test.ts runs the real dashboard validators over the same cases. A one-sided change to either rule makes one of the two tests fail, which is the tie that stops the two implementations drifting apart (archie-core-ui-dashboard-3). Add a case here, never only to one side.",
|
||||
"cases": {
|
||||
"model-role-assignments": [
|
||||
{
|
||||
"why": "a plain provider/model value",
|
||||
"value": { "builder": "openai/gpt-4o" },
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"why": "a trailing space: the server only asks for a slash, so the dashboard must not refuse it",
|
||||
"value": { "builder": "openai/gpt-4o " },
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"why": "a leading space: the server only asks for a slash, so the dashboard must not refuse it",
|
||||
"value": { "builder": " openai/gpt-4o" },
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"why": "a model id with a colon (a real ollama tag), which a stricter shape would wrongly refuse",
|
||||
"value": { "planner": "ollama/qwen2.5-coder:7b" },
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"why": "no slash at all",
|
||||
"value": { "builder": "gpt-4o" },
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"why": "an empty model: the server refuses it, so the dashboard must too (SystemModelsPage deletes the key rather than storing one)",
|
||||
"value": { "builder": "" },
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"why": "a blank role key",
|
||||
"value": { "": "openai/gpt-4o" },
|
||||
"valid": false
|
||||
}
|
||||
],
|
||||
"scheduling-policy": [
|
||||
{
|
||||
"why": "a positive interval, a valid trigger and the label the trigger needs",
|
||||
"value": {
|
||||
"poll_interval": "1m0s",
|
||||
"max_retries": 0,
|
||||
"label": "archie",
|
||||
"dispatch": { "trigger": "assignee" }
|
||||
},
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"why": "assignee does not require a label",
|
||||
"value": {
|
||||
"poll_interval": "30s",
|
||||
"max_retries": 3,
|
||||
"dispatch": { "trigger": "assignee" }
|
||||
},
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"why": "either requires a label, and one is present",
|
||||
"value": {
|
||||
"poll_interval": "1h30m",
|
||||
"label": "archie",
|
||||
"dispatch": { "trigger": "either" }
|
||||
},
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"why": "a fractional duration the server's parser accepts",
|
||||
"value": {
|
||||
"poll_interval": "1.5h",
|
||||
"dispatch": { "trigger": "assignee" }
|
||||
},
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"why": "a zero interval",
|
||||
"value": {
|
||||
"poll_interval": "0s",
|
||||
"label": "archie",
|
||||
"dispatch": { "trigger": "assignee" }
|
||||
},
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"why": "an interval the server's parser cannot read",
|
||||
"value": {
|
||||
"poll_interval": "nonsense",
|
||||
"label": "archie",
|
||||
"dispatch": { "trigger": "assignee" }
|
||||
},
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"why": "the label trigger with no label",
|
||||
"value": {
|
||||
"poll_interval": "30s",
|
||||
"max_retries": 3,
|
||||
"dispatch": { "trigger": "label" }
|
||||
},
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"why": "the either trigger with an empty label",
|
||||
"value": {
|
||||
"poll_interval": "30s",
|
||||
"max_retries": 0,
|
||||
"label": "",
|
||||
"dispatch": { "trigger": "either" }
|
||||
},
|
||||
"valid": false
|
||||
}
|
||||
],
|
||||
"schedules": [
|
||||
{
|
||||
"why": "a single valid workflow job",
|
||||
"value": [
|
||||
{
|
||||
"id": "daily",
|
||||
"kind": "workflow",
|
||||
"schedule": { "kind": "interval", "interval": "24h0m0s" }
|
||||
}
|
||||
],
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"why": "a cron job with a pool the server recognises",
|
||||
"value": [
|
||||
{
|
||||
"id": "nightly",
|
||||
"kind": "workflow",
|
||||
"pool": "parallel",
|
||||
"schedule": { "kind": "cron", "cron": "0 3 * * *" }
|
||||
}
|
||||
],
|
||||
"valid": true
|
||||
},
|
||||
{
|
||||
"why": "a job with no id",
|
||||
"value": [
|
||||
{
|
||||
"id": "",
|
||||
"kind": "workflow",
|
||||
"schedule": { "kind": "interval", "interval": "24h0m0s" }
|
||||
}
|
||||
],
|
||||
"valid": false
|
||||
},
|
||||
{
|
||||
"why": "two jobs sharing an id",
|
||||
"value": [
|
||||
{
|
||||
"id": "daily",
|
||||
"kind": "workflow",
|
||||
"schedule": { "kind": "interval", "interval": "24h0m0s" }
|
||||
},
|
||||
{
|
||||
"id": "daily",
|
||||
"kind": "workflow",
|
||||
"schedule": { "kind": "interval", "interval": "1h0m0s" }
|
||||
}
|
||||
],
|
||||
"valid": false
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,98 @@
|
||||
package channels
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log/slog"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/domain/messaging"
|
||||
)
|
||||
|
||||
// defaultEphemeralDeleteTimeout bounds one retraction. It is independent of
|
||||
// the turn's own context, which may already be cancelled by the time the TTL
|
||||
// elapses: the message was sent, so it still has to be removed.
|
||||
const defaultEphemeralDeleteTimeout = 30 * time.Second
|
||||
|
||||
// SendFunc delivers one message and returns the platform's view of it, with
|
||||
// at least the identifier a later retraction addresses. A sender whose
|
||||
// platform cannot name a delivered message leaves ID empty, and a retraction
|
||||
// of that message is then skipped rather than attempted blindly.
|
||||
type SendFunc func(ctx context.Context, event messaging.MessageEvent) (messaging.MessageEvent, error)
|
||||
|
||||
// EphemeralSender delivers EphemeralReplies and retracts them after their TTL.
|
||||
//
|
||||
// The retraction runs on its own goroutine, so a TTL that has not elapsed
|
||||
// never holds the turn that sent the reply. after is injected so the TTL
|
||||
// fires in tests without sleeping; nil selects time.After, which is the
|
||||
// production shape.
|
||||
type EphemeralSender struct {
|
||||
after func(time.Duration) <-chan time.Time
|
||||
timeout time.Duration
|
||||
log *slog.Logger
|
||||
wg sync.WaitGroup
|
||||
}
|
||||
|
||||
// NewEphemeralSender returns a sender whose retractions wait on after.
|
||||
func NewEphemeralSender(log *slog.Logger, after func(time.Duration) <-chan time.Time) *EphemeralSender {
|
||||
if after == nil {
|
||||
after = time.After
|
||||
}
|
||||
if log == nil {
|
||||
log = slog.Default()
|
||||
}
|
||||
return &EphemeralSender{
|
||||
after: after,
|
||||
timeout: defaultEphemeralDeleteTimeout,
|
||||
log: log,
|
||||
}
|
||||
}
|
||||
|
||||
// Send delivers reply.Event through send. When sender reports the Delete
|
||||
// capability, the delivered message is retracted once reply.TTL elapses;
|
||||
// otherwise the reply is left in place and the fact is logged once, at debug
|
||||
// level. A platform that cannot delete must never fail the turn, so the
|
||||
// degrade returns the send's outcome rather than an error.
|
||||
//
|
||||
// A send error is returned unchanged and no retraction is scheduled: there
|
||||
// is no message to remove. A non-positive TTL or a send that reported no
|
||||
// message identifier also schedules nothing.
|
||||
func (e *EphemeralSender) Send(ctx context.Context, sender any, reply messaging.EphemeralReply, send SendFunc) (messaging.MessageEvent, error) {
|
||||
sent, err := send(ctx, reply.Event)
|
||||
if err != nil {
|
||||
return sent, err
|
||||
}
|
||||
// A reply with no lifetime, or a send the platform could not name, has
|
||||
// nothing to schedule: there is either no deadline or no message to
|
||||
// address.
|
||||
if reply.TTL <= 0 || sent.ID == "" {
|
||||
return sent, nil
|
||||
}
|
||||
|
||||
deleter, ok := messaging.DeleterOf(sender)
|
||||
if !ok {
|
||||
e.log.Debug("channel cannot retract an ephemeral reply; leaving it in place",
|
||||
"platform", reply.Event.Platform, "type", reply.Event.Type)
|
||||
return sent, nil
|
||||
}
|
||||
|
||||
e.wg.Go(func() {
|
||||
<-e.after(reply.TTL)
|
||||
// The turn's context may already be cancelled by the time the TTL
|
||||
// elapses -- a delivered message is still retracted -- so the
|
||||
// retraction carries its own bounded, uncancellable context.
|
||||
delCtx, cancel := context.WithTimeout(context.WithoutCancel(ctx), e.timeout)
|
||||
defer cancel()
|
||||
if err := deleter.DeleteMessage(delCtx, sent); err != nil {
|
||||
e.log.Warn("ephemeral reply retraction failed",
|
||||
"platform", reply.Event.Platform, "error", err)
|
||||
}
|
||||
})
|
||||
return sent, nil
|
||||
}
|
||||
|
||||
// Wait blocks until every retraction started so far has finished. Tests only:
|
||||
// production callers never wait on a retraction's own TTL.
|
||||
func (e *EphemeralSender) Wait() {
|
||||
e.wg.Wait()
|
||||
}
|
||||
@@ -0,0 +1,261 @@
|
||||
package channels
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"errors"
|
||||
"log/slog"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/domain/messaging"
|
||||
)
|
||||
|
||||
// recordingDeleter is a sender that can retract messages. report controls the
|
||||
// Delete capability it advertises, so a test can prove the caller trusts the
|
||||
// capability rather than the method set.
|
||||
type recordingDeleter struct {
|
||||
report bool
|
||||
|
||||
mu sync.Mutex
|
||||
deleted []string
|
||||
err error
|
||||
}
|
||||
|
||||
func (d *recordingDeleter) DeleteMessage(_ context.Context, event messaging.MessageEvent) error {
|
||||
d.mu.Lock()
|
||||
defer d.mu.Unlock()
|
||||
d.deleted = append(d.deleted, event.ID)
|
||||
return d.err
|
||||
}
|
||||
|
||||
func (d *recordingDeleter) Capabilities() messaging.AdapterCapabilities {
|
||||
return messaging.AdapterCapabilities{Delete: d.report}
|
||||
}
|
||||
|
||||
func (d *recordingDeleter) deletions() []string {
|
||||
d.mu.Lock()
|
||||
defer d.mu.Unlock()
|
||||
return append([]string(nil), d.deleted...)
|
||||
}
|
||||
|
||||
// plainSender is a launch-scoped sender that offers no optional delivery
|
||||
// behavior at all -- the shape every adapter that cannot delete has.
|
||||
type plainSender struct{}
|
||||
|
||||
// syncBuffer is a bytes.Buffer safe for a log record written on a retraction
|
||||
// goroutine while the test reads it.
|
||||
type syncBuffer struct {
|
||||
mu sync.Mutex
|
||||
buf bytes.Buffer
|
||||
}
|
||||
|
||||
func (b *syncBuffer) Write(p []byte) (int, error) {
|
||||
b.mu.Lock()
|
||||
defer b.mu.Unlock()
|
||||
return b.buf.Write(p)
|
||||
}
|
||||
|
||||
func (b *syncBuffer) String() string {
|
||||
b.mu.Lock()
|
||||
defer b.mu.Unlock()
|
||||
return b.buf.String()
|
||||
}
|
||||
|
||||
func newDebugLogger(buf *syncBuffer) *slog.Logger {
|
||||
return slog.New(slog.NewTextHandler(buf, &slog.HandlerOptions{Level: slog.LevelDebug}))
|
||||
}
|
||||
|
||||
// sentEvent returns a send func that reports the platform's delivered event,
|
||||
// carrying id, so a retraction has something to address.
|
||||
func sentEvent(id string) SendFunc {
|
||||
return func(_ context.Context, event messaging.MessageEvent) (messaging.MessageEvent, error) {
|
||||
event.ID = id
|
||||
return event, nil
|
||||
}
|
||||
}
|
||||
|
||||
// An ephemeral reply must be retracted once its TTL elapses, not before, and
|
||||
// the retraction must address the message the send actually delivered. The
|
||||
// clock is injected, so the test proves the TTL ordering without sleeping.
|
||||
func TestEphemeralSenderRetractsAfterTTL(t *testing.T) {
|
||||
deleter := &recordingDeleter{report: true}
|
||||
fire := make(chan time.Time)
|
||||
var gotTTL time.Duration
|
||||
|
||||
sender := NewEphemeralSender(slog.New(slog.DiscardHandler), func(d time.Duration) <-chan time.Time {
|
||||
gotTTL = d
|
||||
return fire
|
||||
})
|
||||
|
||||
sent, err := sender.Send(t.Context(), deleter, messaging.EphemeralReply{
|
||||
Event: messaging.MessageEvent{Type: messaging.MsgText, Text: "running tool X…"},
|
||||
TTL: 5 * time.Second,
|
||||
}, sentEvent("42"))
|
||||
if err != nil {
|
||||
t.Fatalf("Send: %v", err)
|
||||
}
|
||||
if sent.ID != "42" {
|
||||
t.Fatalf("Send returned ID %q, want the delivered id", sent.ID)
|
||||
}
|
||||
if got := deleter.deletions(); len(got) != 0 {
|
||||
t.Fatalf("retracted %v before the TTL elapsed, want nothing yet", got)
|
||||
}
|
||||
|
||||
fire <- time.Now()
|
||||
sender.Wait()
|
||||
|
||||
got := deleter.deletions()
|
||||
if len(got) != 1 || got[0] != "42" {
|
||||
t.Fatalf("retracted %v, want exactly the delivered message 42", got)
|
||||
}
|
||||
if gotTTL != 5*time.Second {
|
||||
t.Fatalf("scheduled retraction after %v, want the reply's TTL 5s", gotTTL)
|
||||
}
|
||||
}
|
||||
|
||||
// A platform with no delete support must still deliver the reply: the
|
||||
// message is sent, left in place, and the capability gap is logged, never
|
||||
// returned as a turn error.
|
||||
func TestEphemeralSenderDegradesWhenSenderCannotDelete(t *testing.T) {
|
||||
var buf syncBuffer
|
||||
sender := NewEphemeralSender(newDebugLogger(&buf), func(time.Duration) <-chan time.Time {
|
||||
t.Error("scheduled a retraction for a sender with no delete support")
|
||||
return closedTimeChan()
|
||||
})
|
||||
|
||||
sendCalled := false
|
||||
_, err := sender.Send(t.Context(), &plainSender{}, messaging.EphemeralReply{
|
||||
Event: messaging.MessageEvent{Type: messaging.MsgText, Text: "thinking…"},
|
||||
TTL: time.Second,
|
||||
}, func(_ context.Context, event messaging.MessageEvent) (messaging.MessageEvent, error) {
|
||||
sendCalled = true
|
||||
event.ID = "7"
|
||||
return event, nil
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("Send returned an error for a sender that cannot delete: %v", err)
|
||||
}
|
||||
if !sendCalled {
|
||||
t.Fatal("the reply was not sent; it must degrade to a normal send")
|
||||
}
|
||||
|
||||
sender.Wait()
|
||||
if !strings.Contains(buf.String(), "cannot retract") {
|
||||
t.Fatalf("log = %q, want it to record the missed retraction", buf.String())
|
||||
}
|
||||
}
|
||||
|
||||
// The capability is the contract, not the method set: an adapter that
|
||||
// implements DeleteMessage but reports Delete:false must not be retracted.
|
||||
func TestEphemeralSenderTrustsCapabilityOverMethodSet(t *testing.T) {
|
||||
deleter := &recordingDeleter{report: false}
|
||||
fire := make(chan time.Time)
|
||||
close(fire) // fire at once if anything schedules a retraction
|
||||
|
||||
sender := NewEphemeralSender(slog.New(slog.DiscardHandler), func(time.Duration) <-chan time.Time {
|
||||
return fire
|
||||
})
|
||||
|
||||
if _, err := sender.Send(t.Context(), deleter, messaging.EphemeralReply{
|
||||
Event: messaging.MessageEvent{Type: messaging.MsgText, Text: "thinking…"},
|
||||
TTL: time.Second,
|
||||
}, sentEvent("9")); err != nil {
|
||||
t.Fatalf("Send: %v", err)
|
||||
}
|
||||
sender.Wait()
|
||||
|
||||
if got := deleter.deletions(); len(got) != 0 {
|
||||
t.Fatalf("retracted %v despite Capabilities().Delete == false", got)
|
||||
}
|
||||
}
|
||||
|
||||
// A failed send has no message to retract, and its error belongs to the
|
||||
// caller: Send returns it unchanged and schedules nothing.
|
||||
func TestEphemeralSenderPropagatesSendError(t *testing.T) {
|
||||
wantErr := errors.New("send refused")
|
||||
deleter := &recordingDeleter{report: true}
|
||||
sender := NewEphemeralSender(slog.New(slog.DiscardHandler), func(time.Duration) <-chan time.Time {
|
||||
t.Error("scheduled a retraction for a message that was never sent")
|
||||
return closedTimeChan()
|
||||
})
|
||||
|
||||
_, err := sender.Send(t.Context(), deleter, messaging.EphemeralReply{
|
||||
Event: messaging.MessageEvent{Type: messaging.MsgText, Text: "thinking…"},
|
||||
TTL: time.Second,
|
||||
}, func(context.Context, messaging.MessageEvent) (messaging.MessageEvent, error) {
|
||||
return messaging.MessageEvent{}, wantErr
|
||||
})
|
||||
if !errors.Is(err, wantErr) {
|
||||
t.Fatalf("Send error = %v, want %v", err, wantErr)
|
||||
}
|
||||
sender.Wait()
|
||||
if got := deleter.deletions(); len(got) != 0 {
|
||||
t.Fatalf("retracted %v after a failed send", got)
|
||||
}
|
||||
}
|
||||
|
||||
// A reply with no lifetime and one whose send reported no message id both
|
||||
// schedule nothing: there is either no deadline or no message to address.
|
||||
func TestEphemeralSenderSchedulesNothingWithoutTTLOrMessageID(t *testing.T) {
|
||||
closed := closedTimeChan()
|
||||
tests := []struct {
|
||||
name string
|
||||
ttl time.Duration
|
||||
id string
|
||||
}{
|
||||
{name: "zero TTL", ttl: 0, id: "11"},
|
||||
{name: "negative TTL", ttl: -time.Second, id: "11"},
|
||||
{name: "no message id", ttl: time.Second, id: ""},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
deleter := &recordingDeleter{report: true}
|
||||
sender := NewEphemeralSender(slog.New(slog.DiscardHandler), func(time.Duration) <-chan time.Time {
|
||||
return closed
|
||||
})
|
||||
if _, err := sender.Send(t.Context(), deleter, messaging.EphemeralReply{
|
||||
Event: messaging.MessageEvent{Type: messaging.MsgText, Text: "thinking…"},
|
||||
TTL: tt.ttl,
|
||||
}, sentEvent(tt.id)); err != nil {
|
||||
t.Fatalf("Send: %v", err)
|
||||
}
|
||||
sender.Wait()
|
||||
if got := deleter.deletions(); len(got) != 0 {
|
||||
t.Fatalf("retracted %v, want nothing scheduled", got)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// A retraction failure is logged, not returned: the reply was delivered, and
|
||||
// a platform hiccup removing it later must not surface as a turn error.
|
||||
func TestEphemeralSenderLogsRetractionFailure(t *testing.T) {
|
||||
var buf syncBuffer
|
||||
deleter := &recordingDeleter{report: true, err: errors.New("message not found")}
|
||||
fire := make(chan time.Time)
|
||||
close(fire)
|
||||
|
||||
sender := NewEphemeralSender(newDebugLogger(&buf), func(time.Duration) <-chan time.Time {
|
||||
return fire
|
||||
})
|
||||
if _, err := sender.Send(t.Context(), deleter, messaging.EphemeralReply{
|
||||
Event: messaging.MessageEvent{Type: messaging.MsgText, Text: "thinking…"},
|
||||
TTL: time.Second,
|
||||
}, sentEvent("13")); err != nil {
|
||||
t.Fatalf("Send: %v", err)
|
||||
}
|
||||
sender.Wait()
|
||||
|
||||
if !strings.Contains(buf.String(), "retraction failed") {
|
||||
t.Fatalf("log = %q, want it to record the failed retraction", buf.String())
|
||||
}
|
||||
}
|
||||
|
||||
func closedTimeChan() <-chan time.Time {
|
||||
ch := make(chan time.Time)
|
||||
close(ch)
|
||||
return ch
|
||||
}
|
||||
@@ -54,7 +54,19 @@ type approvalResult struct {
|
||||
}
|
||||
|
||||
// Compile-time guard.
|
||||
var _ messaging.ApprovalRequester = (*telegramApprover)(nil)
|
||||
var (
|
||||
_ messaging.ApprovalRequester = (*telegramApprover)(nil)
|
||||
_ messaging.CapabilityReporter = (*telegramApprover)(nil)
|
||||
)
|
||||
|
||||
// Capabilities reports that this approver renders a native approval prompt.
|
||||
// It claims Approval only: clarify and picker have no native Telegram UI
|
||||
// wired yet, so claiming them would advertise an interaction this type
|
||||
// cannot carry. A caller that sees Clarify/Picker false uses the shared text
|
||||
// fallback instead of silently dropping the question.
|
||||
func (a *telegramApprover) Capabilities() messaging.AdapterCapabilities {
|
||||
return messaging.AdapterCapabilities{Approval: true}
|
||||
}
|
||||
|
||||
// NewApprover returns an ApprovalRequester that renders prompts in the
|
||||
// given chat. recipient is the Telegram user who may approve; in a private
|
||||
|
||||
@@ -413,3 +413,23 @@ func TestParseApprovalCallback(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// The approver renders a native approval prompt, so it must report the
|
||||
// Approval capability -- and must not claim the interactive capabilities it
|
||||
// has no native UI for. A caller seeing Clarify/Picker false uses the shared
|
||||
// text fallback rather than dropping the question.
|
||||
func TestApproverReportsApprovalCapability(t *testing.T) {
|
||||
g, b, _ := newApprovalTestGateway(t)
|
||||
approver := newTestApprover(g, b)
|
||||
|
||||
caps := messaging.CapabilitiesOf(approver)
|
||||
if !caps.Approval {
|
||||
t.Error("telegram approver does not report the Approval capability")
|
||||
}
|
||||
if caps.Clarify || caps.Picker {
|
||||
t.Errorf("telegram approver over-claims interactive capabilities: %+v", caps)
|
||||
}
|
||||
if _, ok := messaging.ApproverOf(approver); !ok {
|
||||
t.Error("ApproverOf rejected an approver that reports the Approval capability")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
package telegram
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strconv"
|
||||
|
||||
"github.com/go-telegram/bot"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/domain/messaging"
|
||||
)
|
||||
|
||||
// telegramMessageDeleter removes messages the launch that built it sent,
|
||||
// through the Bot API's deleteMessage method.
|
||||
//
|
||||
// It captures the *bot.Bot at construction rather than reading a Gateway
|
||||
// field, matching telegramMediaSender and telegramApprover: a deleter belongs
|
||||
// to the launch that built it, so a /restart abandons in-flight retractions
|
||||
// with the outgoing bot instance instead of silently directing them through
|
||||
// the new one.
|
||||
type telegramMessageDeleter struct {
|
||||
bot *bot.Bot
|
||||
chatID int64
|
||||
}
|
||||
|
||||
var (
|
||||
_ messaging.MessageDeleter = (*telegramMessageDeleter)(nil)
|
||||
_ messaging.CapabilityReporter = (*telegramMessageDeleter)(nil)
|
||||
)
|
||||
|
||||
// NewMessageDeleter returns a MessageDeleter bound to one chat and to the bot
|
||||
// instance of the launch that created it. Construct it per launch, from the
|
||||
// same b that handled the update.
|
||||
func (g *Gateway) NewMessageDeleter(b *bot.Bot, chatID int64) messaging.MessageDeleter {
|
||||
return &telegramMessageDeleter{bot: b, chatID: chatID}
|
||||
}
|
||||
|
||||
// Capabilities reports Delete support. Telegram can remove any message the
|
||||
// bot sent in a chat it administers, so this is unconditionally true.
|
||||
func (d *telegramMessageDeleter) Capabilities() messaging.AdapterCapabilities {
|
||||
return messaging.AdapterCapabilities{Delete: true}
|
||||
}
|
||||
|
||||
// DeleteMessage removes the message event names. The event's ID is the
|
||||
// Telegram message ID the send returned; a non-numeric one cannot address a
|
||||
// platform message, so it is reported rather than sent to the API as a
|
||||
// doomed request.
|
||||
func (d *telegramMessageDeleter) DeleteMessage(ctx context.Context, event messaging.MessageEvent) error {
|
||||
id, err := strconv.Atoi(event.ID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("telegram delete: message id %q is not numeric: %w", event.ID, err)
|
||||
}
|
||||
if _, err := d.bot.DeleteMessage(ctx, &bot.DeleteMessageParams{
|
||||
ChatID: d.chatID,
|
||||
MessageID: id,
|
||||
}); err != nil {
|
||||
return fmt.Errorf("telegram delete message %d: %w", id, err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
package telegram
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/go-telegram/bot"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/domain/messaging"
|
||||
)
|
||||
|
||||
// statusNoticeTTL is how long a transient status notice stays in the chat
|
||||
// before it is retracted: long enough to be read, short enough not to become
|
||||
// history next to the conversation it reported on.
|
||||
const statusNoticeTTL = 8 * time.Second
|
||||
|
||||
// sendEphemeral delivers text and retracts it after statusNoticeTTL.
|
||||
//
|
||||
// The retraction is best-effort by contract: a platform that cannot delete
|
||||
// leaves the notice in place (channels.EphemeralSender logs the degrade), and
|
||||
// a failed send is logged rather than returned, because a status notice must
|
||||
// never take the operation it narrates down with it.
|
||||
func (g *Gateway) sendEphemeral(ctx context.Context, b *bot.Bot, chatID int64, messageThreadID int, text string) {
|
||||
sender := g.newEphemeralSender()
|
||||
_, err := sender.Send(ctx, g.NewMessageDeleter(b, chatID), messaging.EphemeralReply{
|
||||
Event: messaging.MessageEvent{
|
||||
Type: messaging.MsgText,
|
||||
Text: text,
|
||||
ChannelID: fmt.Sprintf("%d", chatID),
|
||||
Platform: "telegram",
|
||||
},
|
||||
TTL: statusNoticeTTL,
|
||||
}, func(ctx context.Context, event messaging.MessageEvent) (messaging.MessageEvent, error) {
|
||||
id := g.sendTextMessage(ctx, b, chatID, messageThreadID, event.Text)
|
||||
if id == 0 {
|
||||
// Nothing was delivered, so there is nothing to name or retract.
|
||||
return messaging.MessageEvent{}, nil
|
||||
}
|
||||
event.ID = fmt.Sprintf("%d", id)
|
||||
return event, nil
|
||||
})
|
||||
if err != nil {
|
||||
g.log.Error("ephemeral notice send failed", "error", err)
|
||||
}
|
||||
}
|
||||
|
||||
// sendTextMessage delivers one message and returns its Bot API message ID.
|
||||
//
|
||||
// It returns 0 for text that must be split across messages: a retraction
|
||||
// addresses exactly one message, so a reply that does not fit in one is left
|
||||
// intact rather than half-deleted.
|
||||
func (g *Gateway) sendTextMessage(ctx context.Context, b *bot.Bot, chatID int64, messageThreadID int, text string) int {
|
||||
blocks := markdownToBlocks(text)
|
||||
if blockTextLen(blocks) > messageMaxLen {
|
||||
g.sendMessage(ctx, b, chatID, messageThreadID, text)
|
||||
return 0
|
||||
}
|
||||
return g.sendBlocks(ctx, b, chatID, messageThreadID, blocks, "send ephemeral message failed")
|
||||
}
|
||||
@@ -0,0 +1,160 @@
|
||||
package telegram
|
||||
|
||||
import (
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/go-telegram/bot"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/channels"
|
||||
"github.com/samcharles93/archie-core/internal/domain/messaging"
|
||||
)
|
||||
|
||||
func newTestDeleter(t *testing.T, serverURL string, chatID int64) messaging.MessageDeleter {
|
||||
t.Helper()
|
||||
b, err := bot.New("1:test", bot.WithServerURL(serverURL), bot.WithSkipGetMe())
|
||||
if err != nil {
|
||||
t.Fatalf("new test bot: %v", err)
|
||||
}
|
||||
return New("1:test", []int64{42}, slog.New(slog.DiscardHandler)).NewMessageDeleter(b, chatID)
|
||||
}
|
||||
|
||||
// The delete capability must be reported, so an ephemeral reply's retraction
|
||||
// path can be chosen without attempting a doomed call.
|
||||
func TestMessageDeleterReportsDeleteCapability(t *testing.T) {
|
||||
_, srv := newMediaAPI()
|
||||
defer srv.Close()
|
||||
|
||||
deleter := newTestDeleter(t, srv.URL, 555)
|
||||
if caps := messaging.CapabilitiesOf(deleter); !caps.Delete {
|
||||
t.Error("telegram message deleter does not report the Delete capability")
|
||||
}
|
||||
}
|
||||
|
||||
// A retraction must reach the Bot API's deleteMessage method with the chat and
|
||||
// the message the send returned.
|
||||
func TestMessageDeleterCallsDeleteMessage(t *testing.T) {
|
||||
api, srv := newMediaAPI()
|
||||
api.body = `{"ok":true,"result":true}`
|
||||
defer srv.Close()
|
||||
|
||||
deleter := newTestDeleter(t, srv.URL, 555)
|
||||
if err := deleter.DeleteMessage(t.Context(), messaging.MessageEvent{ID: "77"}); err != nil {
|
||||
t.Fatalf("DeleteMessage: %v", err)
|
||||
}
|
||||
|
||||
method, form := api.called()
|
||||
if method != "deleteMessage" {
|
||||
t.Fatalf("Bot API method = %q, want deleteMessage", method)
|
||||
}
|
||||
if form["message_id"] != "77" {
|
||||
t.Errorf("message_id = %q, want 77", form["message_id"])
|
||||
}
|
||||
if form["chat_id"] != "555" {
|
||||
t.Errorf("chat_id = %q, want the bound chat 555", form["chat_id"])
|
||||
}
|
||||
}
|
||||
|
||||
// A message ID the platform never issued cannot address a message, so it is
|
||||
// reported before any request rather than sent as a doomed one.
|
||||
func TestMessageDeleterRejectsNonNumericID(t *testing.T) {
|
||||
api, srv := newMediaAPI()
|
||||
api.body = `{"ok":true,"result":true}`
|
||||
defer srv.Close()
|
||||
|
||||
deleter := newTestDeleter(t, srv.URL, 555)
|
||||
if err := deleter.DeleteMessage(t.Context(), messaging.MessageEvent{ID: "not-a-number"}); err == nil {
|
||||
t.Fatal("DeleteMessage accepted a non-numeric message ID")
|
||||
}
|
||||
if method, _ := api.called(); method != "" {
|
||||
t.Fatalf("Bot API method = %q, want no request for an undeliverable ID", method)
|
||||
}
|
||||
}
|
||||
|
||||
// An API failure must surface as an error so the caller can log it; a failed
|
||||
// retraction is never silent.
|
||||
func TestMessageDeleterReportsAPIError(t *testing.T) {
|
||||
api, srv := newMediaAPI()
|
||||
api.body = `{"ok":false,"error_code":400,"description":"message to delete not found"}`
|
||||
defer srv.Close()
|
||||
|
||||
deleter := newTestDeleter(t, srv.URL, 555)
|
||||
if err := deleter.DeleteMessage(t.Context(), messaging.MessageEvent{ID: "404"}); err == nil {
|
||||
t.Fatal("DeleteMessage reported success for a rejected request")
|
||||
}
|
||||
}
|
||||
|
||||
// sendEphemeral is the transport honouring an ephemeral reply end to end: it
|
||||
// sends the status notice, then retracts the message the send returned once
|
||||
// the injected clock's TTL fires. The injected sender keeps the test off a
|
||||
// real eight-second wait.
|
||||
func TestSendEphemeralRetractsStatusNotice(t *testing.T) {
|
||||
type call struct {
|
||||
method string
|
||||
form map[string]string
|
||||
}
|
||||
calls := make(chan call, 8)
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
parts := strings.Split(strings.Trim(r.URL.Path, "/"), "/")
|
||||
method := parts[len(parts)-1]
|
||||
_ = r.ParseMultipartForm(1 << 20)
|
||||
form := map[string]string{}
|
||||
for k, v := range r.Form {
|
||||
if len(v) > 0 {
|
||||
form[k] = v[0]
|
||||
}
|
||||
}
|
||||
calls <- call{method: method, form: form}
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
if method == "deleteMessage" {
|
||||
_, _ = w.Write([]byte(`{"ok":true,"result":true}`))
|
||||
return
|
||||
}
|
||||
_, _ = w.Write([]byte(`{"ok":true,"result":{"message_id":321}}`))
|
||||
}))
|
||||
defer srv.Close()
|
||||
|
||||
b, err := bot.New("1:test", bot.WithServerURL(srv.URL), bot.WithSkipGetMe())
|
||||
if err != nil {
|
||||
t.Fatalf("new test bot: %v", err)
|
||||
}
|
||||
|
||||
g := New("1:test", []int64{42}, slog.New(slog.DiscardHandler))
|
||||
g.newEphemeralSender = func() *channels.EphemeralSender {
|
||||
return channels.NewEphemeralSender(g.log, func(time.Duration) <-chan time.Time {
|
||||
ch := make(chan time.Time)
|
||||
close(ch)
|
||||
return ch
|
||||
})
|
||||
}
|
||||
|
||||
g.sendEphemeral(t.Context(), b, 555, 0, "✅ Archie reloaded.")
|
||||
|
||||
var sent, retracted *call
|
||||
deadline := time.After(2 * time.Second)
|
||||
for sent == nil || retracted == nil {
|
||||
select {
|
||||
case c := <-calls:
|
||||
switch c.method {
|
||||
case "sendRichMessage", "sendMessage":
|
||||
sent = &c
|
||||
case "deleteMessage":
|
||||
retracted = &c
|
||||
}
|
||||
case <-deadline:
|
||||
t.Fatalf("timed out; send=%v retraction=%v", sent, retracted)
|
||||
}
|
||||
}
|
||||
|
||||
if retracted.form["message_id"] != "321" {
|
||||
t.Errorf("retracted message_id = %q, want the sent message 321", retracted.form["message_id"])
|
||||
}
|
||||
if retracted.form["chat_id"] != "555" {
|
||||
t.Errorf("retraction chat_id = %q, want the chat the notice was sent to", retracted.form["chat_id"])
|
||||
}
|
||||
}
|
||||
@@ -82,10 +82,10 @@ func (f *fakeChatContract) SetPersona(ctx context.Context, id, name string) (boo
|
||||
return true, nil
|
||||
}
|
||||
|
||||
func (f *fakeChatContract) ApplyTaskAction(ctx context.Context, identity string, taskID int64, action taskstate.Action) (messaging.TaskActionResult, error) {
|
||||
func (f *fakeChatContract) ApplyTaskAction(ctx context.Context, identity string, taskID int64, action taskstate.Action, _ taskactions.ActionPayload) (messaging.TaskActionResult, error) {
|
||||
return messaging.TaskActionResult{}, nil
|
||||
}
|
||||
|
||||
func (f *fakeChatContract) ApplyOperatorTaskAction(ctx context.Context, actor taskactions.Actor, taskID int64, action taskstate.Action) (messaging.TaskActionResult, error) {
|
||||
func (f *fakeChatContract) ApplyOperatorTaskAction(ctx context.Context, actor taskactions.Actor, taskID int64, action taskstate.Action, _ taskactions.ActionPayload) (messaging.TaskActionResult, error) {
|
||||
return messaging.TaskActionResult{}, nil
|
||||
}
|
||||
@@ -40,42 +40,42 @@ func extractInboundMedia(msg *models.Message) (inboundMedia, bool) {
|
||||
}
|
||||
}
|
||||
}
|
||||
return inboundMedia{att, "[photo]"}, true
|
||||
return inboundMedia{attachment: att, note: "[photo]"}, true
|
||||
|
||||
case msg.Document != nil:
|
||||
return inboundMedia{messaging.MediaAttachment{
|
||||
return inboundMedia{attachment: messaging.MediaAttachment{
|
||||
Type: "document",
|
||||
FileID: msg.Document.FileID,
|
||||
MIMEType: msg.Document.MimeType,
|
||||
FileName: msg.Document.FileName,
|
||||
FileSize: nilIfZero(msg.Document.FileSize),
|
||||
}, documentNote(msg.Document.FileName)}, true
|
||||
}, note: documentNote(msg.Document.FileName)}, true
|
||||
|
||||
case msg.Video != nil:
|
||||
return inboundMedia{videoAttachment(msg.Video.FileID, msg.Video.MimeType, nilIfZero(msg.Video.FileSize), &msg.Video.Width, &msg.Video.Height, &msg.Video.Duration), "[video]"}, true
|
||||
return inboundMedia{attachment: videoAttachment(msg.Video.FileID, msg.Video.MimeType, nilIfZero(msg.Video.FileSize), &msg.Video.Width, &msg.Video.Height, &msg.Video.Duration), note: "[video]"}, true
|
||||
|
||||
case msg.VideoNote != nil:
|
||||
size := int64(msg.VideoNote.FileSize)
|
||||
return inboundMedia{videoAttachment(msg.VideoNote.FileID, "video/mp4", nilIfZero(size), nil, nil, &msg.VideoNote.Duration), "[video message]"}, true
|
||||
return inboundMedia{attachment: videoAttachment(msg.VideoNote.FileID, "video/mp4", nilIfZero(size), nil, nil, &msg.VideoNote.Duration), note: "[video message]"}, true
|
||||
|
||||
case msg.Audio != nil:
|
||||
return inboundMedia{messaging.MediaAttachment{
|
||||
return inboundMedia{attachment: messaging.MediaAttachment{
|
||||
Type: "audio",
|
||||
FileID: msg.Audio.FileID,
|
||||
MIMEType: msg.Audio.MimeType,
|
||||
FileName: msg.Audio.FileName,
|
||||
FileSize: nilIfZero(msg.Audio.FileSize),
|
||||
Duration: &msg.Audio.Duration,
|
||||
}, "[audio]"}, true
|
||||
}, note: "[audio]"}, true
|
||||
|
||||
case msg.Voice != nil:
|
||||
return inboundMedia{messaging.MediaAttachment{
|
||||
Type: "audio",
|
||||
return inboundMedia{attachment: messaging.MediaAttachment{
|
||||
Type: messaging.MediaTypeVoice,
|
||||
FileID: msg.Voice.FileID,
|
||||
MIMEType: msg.Voice.MimeType,
|
||||
FileSize: nilIfZero(msg.Voice.FileSize),
|
||||
Duration: &msg.Voice.Duration,
|
||||
}, "[voice message]"}, true
|
||||
}, note: "[voice message]"}, true
|
||||
}
|
||||
return inboundMedia{}, false
|
||||
}
|
||||
@@ -114,8 +114,9 @@ func nilIfZero(v int64) *int64 {
|
||||
// maxInboundDownloadBytes is the Bot API download ceiling: bots cannot
|
||||
// fetch files larger than 20 MB through getFile, so a larger attachment
|
||||
// must be refused with a clear notice rather than met with a Bot API 400
|
||||
// nobody reads.
|
||||
var maxInboundDownloadBytes int64 = 20 * 1024 * 1024
|
||||
// nobody reads. It is the messaging contract's inbound attachment ceiling,
|
||||
// so the value the Gateway must be able to receive stays in one place.
|
||||
var maxInboundDownloadBytes int64 = messaging.MaxInboundAttachmentBytes
|
||||
|
||||
var errInboundMediaTooLarge = errors.New("file exceeds the bot download limit")
|
||||
|
||||
|
||||
@@ -158,7 +158,7 @@ func TestExtractInboundMediaMapsTelegramKinds(t *testing.T) {
|
||||
msg: &models.Message{
|
||||
Voice: &models.Voice{FileID: "voice1", MimeType: "audio/ogg", Duration: 8},
|
||||
},
|
||||
wantType: "audio",
|
||||
wantType: messaging.MediaTypeVoice,
|
||||
wantFileID: "voice1",
|
||||
wantMIME: "audio/ogg",
|
||||
wantNote: "[voice message]",
|
||||
@@ -333,6 +333,107 @@ func TestDefaultHandlerSilentForNonContentMessages(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// voiceUpdate is a Telegram voice note from the authorized sender.
|
||||
func voiceUpdate(fileID string) *models.Message {
|
||||
return &models.Message{
|
||||
ID: 11,
|
||||
Date: 1,
|
||||
From: &models.User{ID: 42, Username: "sam"},
|
||||
Chat: models.Chat{ID: 7, Type: "private"},
|
||||
Voice: &models.Voice{FileID: fileID, MimeType: "audio/ogg", Duration: 8},
|
||||
}
|
||||
}
|
||||
|
||||
// TestDefaultHandlerMarksVoiceNoteAsSpeech drives the frontend half of the
|
||||
// boundary: the messaging process downloads the note, classifies it as speech
|
||||
// (messaging.MediaTypeVoice), and carries the bytes and the note to the turn.
|
||||
// Turning those bytes into text is the model-owning process's job -- this
|
||||
// process holds no provider credential -- so no transcription happens here.
|
||||
func TestDefaultHandlerMarksVoiceNoteAsSpeech(t *testing.T) {
|
||||
voiceBytes := []byte("ogg-voice-bytes")
|
||||
b, _ := newRecordedAPI(t, func(w http.ResponseWriter, r *http.Request) bool {
|
||||
if strings.HasPrefix(r.URL.Path, "/file/bot1:test/") {
|
||||
_, _ = w.Write(voiceBytes)
|
||||
return true
|
||||
}
|
||||
return false
|
||||
})
|
||||
|
||||
inboundCh := make(chan messaging.Inbound, 1)
|
||||
client := &fakeChatContract{
|
||||
streamFunc: func(_ context.Context, in messaging.Inbound) (<-chan messaging.ChatEvent, error) {
|
||||
inboundCh <- in
|
||||
ch := make(chan messaging.ChatEvent, 1)
|
||||
ch <- messaging.ChatEvent{Kind: "done", Text: "done", SessionID: "s1"}
|
||||
close(ch)
|
||||
return ch, nil
|
||||
},
|
||||
}
|
||||
g := testMediaGateway(b)
|
||||
|
||||
g.defaultHandler(client)(context.Background(), b, &models.Update{Message: voiceUpdate("voice1")})
|
||||
|
||||
select {
|
||||
case in := <-inboundCh:
|
||||
if len(in.Media) != 1 || in.Media[0].Type != messaging.MediaTypeVoice {
|
||||
t.Fatalf("inbound media = %#v, want the voice attachment marked as speech", in.Media)
|
||||
}
|
||||
if string(in.Media[0].Data) != string(voiceBytes) {
|
||||
t.Errorf("inbound media bytes = %q, want the downloaded voice bytes", in.Media[0].Data)
|
||||
}
|
||||
if in.Message.Text != "[voice message]" {
|
||||
t.Errorf("Text = %q, want the media note preserved for the model-owning side", in.Message.Text)
|
||||
}
|
||||
case <-time.After(5 * time.Second):
|
||||
t.Fatal("voice note never reached the chat turn")
|
||||
}
|
||||
}
|
||||
|
||||
// TestDefaultHandlerMarksPlainAudioAsAudio keeps the speech distinction: a
|
||||
// forwarded music file is ordinary audio, so the model-owning side knows not
|
||||
// to spend a transcription call on it.
|
||||
func TestDefaultHandlerMarksPlainAudioAsAudio(t *testing.T) {
|
||||
b, _ := newRecordedAPI(t, func(w http.ResponseWriter, r *http.Request) bool {
|
||||
if strings.HasPrefix(r.URL.Path, "/file/bot1:test/") {
|
||||
_, _ = w.Write([]byte("mp3-bytes"))
|
||||
return true
|
||||
}
|
||||
return false
|
||||
})
|
||||
|
||||
inboundCh := make(chan messaging.Inbound, 1)
|
||||
client := &fakeChatContract{
|
||||
streamFunc: func(_ context.Context, in messaging.Inbound) (<-chan messaging.ChatEvent, error) {
|
||||
inboundCh <- in
|
||||
ch := make(chan messaging.ChatEvent, 1)
|
||||
ch <- messaging.ChatEvent{Kind: "done", Text: "done", SessionID: "s1"}
|
||||
close(ch)
|
||||
return ch, nil
|
||||
},
|
||||
}
|
||||
g := testMediaGateway(b)
|
||||
|
||||
g.defaultHandler(client)(context.Background(), b, &models.Update{Message: &models.Message{
|
||||
ID: 12,
|
||||
Date: 1,
|
||||
From: &models.User{ID: 42, Username: "sam"},
|
||||
Chat: models.Chat{ID: 7, Type: "private"},
|
||||
Audio: &models.Audio{FileID: "aud1", FileName: "song.mp3", MimeType: "audio/mpeg", Duration: 200},
|
||||
}})
|
||||
|
||||
select {
|
||||
case in := <-inboundCh:
|
||||
if len(in.Media) != 1 || in.Media[0].Type != messaging.MediaTypeAudio {
|
||||
t.Fatalf("inbound media = %#v, want the plain audio attachment", in.Media)
|
||||
}
|
||||
if in.Message.Text != "[audio]" {
|
||||
t.Errorf("Text = %q, want the plain audio note", in.Message.Text)
|
||||
}
|
||||
case <-time.After(5 * time.Second):
|
||||
t.Fatal("audio update never reached the chat turn")
|
||||
}
|
||||
}
|
||||
|
||||
// TestDownloadMediaCoversSuccessAndLimits exercises the download the turn
|
||||
// relies on: the Bot API getFile lookup, the file fetch, and the size gate.
|
||||
func TestDownloadMediaCoversSuccessAndLimits(t *testing.T) {
|
||||
|
||||
@@ -0,0 +1,217 @@
|
||||
package telegram
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/go-telegram/bot"
|
||||
"github.com/go-telegram/bot/models"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/domain/messaging"
|
||||
)
|
||||
|
||||
// interactiveReplyTimeout bounds how long one clarify or picker request waits
|
||||
// for the human's reply. It matches the approval window so the two blocking
|
||||
// interactions expire together and neither can wedge a turn indefinitely.
|
||||
const interactiveReplyTimeout = messaging.ToolApprovalTimeout
|
||||
|
||||
// pendingReplyKey identifies the chat a blocked clarify/picker prompt belongs
|
||||
// to, so only the addressed human's next text is consumed as the answer.
|
||||
type pendingReplyKey struct {
|
||||
chatID int64
|
||||
threadID int
|
||||
recipient int64
|
||||
}
|
||||
|
||||
// pendingReply is one clarify/picker prompt waiting on the human's next text.
|
||||
// It is the text-reply analogue of pendingApproval, kept separate so the
|
||||
// button (callback) and message (text) models cannot entangle.
|
||||
type pendingReply struct {
|
||||
resultCh chan replyResult
|
||||
expiresAt time.Time
|
||||
}
|
||||
|
||||
// replyResult carries the human's typed answer (or the wait's error) back to
|
||||
// the blocked interaction.
|
||||
type replyResult struct {
|
||||
text string
|
||||
err error
|
||||
}
|
||||
|
||||
// telegramInteractor carries clarify and picker interactions for one chat on
|
||||
// the channel's shared text fallback. Telegram renders no generic question or
|
||||
// single-choice widget in the agent-turn path, so both interactions appear as
|
||||
// text the human answers by replying; reporting the Clarify and Picker
|
||||
// capabilities is therefore honest, because the adapter can carry the
|
||||
// interaction even though it costs a typed answer.
|
||||
type telegramInteractor struct {
|
||||
gw *Gateway
|
||||
bot *bot.Bot
|
||||
chatID int64
|
||||
threadID int
|
||||
recipient int64
|
||||
}
|
||||
|
||||
// Compile-time guards: the adapter carries both interactions and reports them
|
||||
// through the capability contract, so ClarifierOf/PickerOf resolve it.
|
||||
var (
|
||||
_ messaging.ClarifyRequester = (*telegramInteractor)(nil)
|
||||
_ messaging.PickerRequester = (*telegramInteractor)(nil)
|
||||
_ messaging.CapabilityReporter = (*telegramInteractor)(nil)
|
||||
)
|
||||
|
||||
// Capabilities reports that this adapter can carry a clarify question and a
|
||||
// single-choice picker. Both go through the shared text fallback rather than a
|
||||
// native widget, and the capability is about carrying the interaction, not
|
||||
// about the widget, so claiming them is what keeps a question from being
|
||||
// silently dropped.
|
||||
func (i *telegramInteractor) Capabilities() messaging.AdapterCapabilities {
|
||||
return messaging.AdapterCapabilities{Clarify: true, Picker: true}
|
||||
}
|
||||
|
||||
// NewInteractor returns the channel's clarify/picker adapter for one chat,
|
||||
// already resolved to the requesters its capability report advertises. The
|
||||
// gateway turn stores it with WithInteractive so its question tool can reach
|
||||
// the human.
|
||||
func (g *Gateway) NewInteractor(b *bot.Bot, chatID int64, threadID int, recipient int64) messaging.Interactive {
|
||||
i := &telegramInteractor{
|
||||
gw: g,
|
||||
bot: b,
|
||||
chatID: chatID,
|
||||
threadID: threadID,
|
||||
recipient: recipient,
|
||||
}
|
||||
return messaging.InteractiveOf(i)
|
||||
}
|
||||
|
||||
// RequestClarification poses the question as text and blocks for the reply.
|
||||
func (i *telegramInteractor) RequestClarification(ctx context.Context, req messaging.ClarifyRequest) (string, error) {
|
||||
ctx, cancel := context.WithTimeout(ctx, interactiveReplyTimeout)
|
||||
defer cancel()
|
||||
fallback := i.fallback()
|
||||
return fallback.RequestClarification(ctx, req)
|
||||
}
|
||||
|
||||
// RequestChoice poses the options as numbered text and matches the typed
|
||||
// reply. The domain's text fallback owns the rendering and matching, so the
|
||||
// degrade is one implementation rather than a second parser here.
|
||||
func (i *telegramInteractor) RequestChoice(ctx context.Context, req messaging.PickerRequest) (messaging.InteractiveChoice, error) {
|
||||
ctx, cancel := context.WithTimeout(ctx, interactiveReplyTimeout)
|
||||
defer cancel()
|
||||
fallback := i.fallback()
|
||||
return fallback.RequestChoice(ctx, req)
|
||||
}
|
||||
|
||||
// fallback builds a fresh text fallback for one interaction. Registering the
|
||||
// pending reply inside Send, before the prompt is delivered, closes the race
|
||||
// where a fast reply arrives before the blocked call is listening.
|
||||
func (i *telegramInteractor) fallback() messaging.TextFallback {
|
||||
key := pendingReplyKey{chatID: i.chatID, threadID: i.threadID, recipient: i.recipient}
|
||||
ch := make(chan replyResult, 1)
|
||||
return messaging.TextFallback{
|
||||
Send: func(ctx context.Context, text string) error {
|
||||
i.gw.registerPendingReply(key, pendingReply{
|
||||
resultCh: ch,
|
||||
expiresAt: time.Now().Add(interactiveReplyTimeout),
|
||||
})
|
||||
if err := i.send(ctx, text); err != nil {
|
||||
i.gw.removePendingReply(key)
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
},
|
||||
Reply: func(ctx context.Context) (string, error) {
|
||||
select {
|
||||
case res := <-ch:
|
||||
return res.text, res.err
|
||||
case <-ctx.Done():
|
||||
i.gw.removePendingReply(key)
|
||||
return "", ctx.Err()
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// send delivers one prompt. ForceReply makes the reply affordance explicit in
|
||||
// a chat with other traffic, so the human answers the question rather than
|
||||
// sending a message the turn cannot tell apart from a new request.
|
||||
func (i *telegramInteractor) send(ctx context.Context, text string) error {
|
||||
params := &bot.SendMessageParams{
|
||||
ChatID: i.chatID,
|
||||
Text: text,
|
||||
ReplyMarkup: &models.ForceReply{ForceReply: true, Selective: true},
|
||||
}
|
||||
if i.threadID != 0 {
|
||||
params.MessageThreadID = i.threadID
|
||||
}
|
||||
if _, err := i.bot.SendMessage(ctx, params); err != nil {
|
||||
return fmt.Errorf("send interactive prompt: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// registerPendingReply stores a pending interaction, pruning any that have
|
||||
// already expired.
|
||||
func (g *Gateway) registerPendingReply(key pendingReplyKey, pr pendingReply) {
|
||||
g.interactiveMu.Lock()
|
||||
defer g.interactiveMu.Unlock()
|
||||
now := time.Now()
|
||||
for existing, pending := range g.pendingReplies {
|
||||
if !pending.expiresAt.After(now) {
|
||||
delete(g.pendingReplies, existing)
|
||||
}
|
||||
}
|
||||
g.pendingReplies[key] = &pr
|
||||
}
|
||||
|
||||
// removePendingReply drops a pending interaction, if present.
|
||||
func (g *Gateway) removePendingReply(key pendingReplyKey) {
|
||||
g.interactiveMu.Lock()
|
||||
defer g.interactiveMu.Unlock()
|
||||
delete(g.pendingReplies, key)
|
||||
}
|
||||
|
||||
// consumePendingReply atomically claims the pending interaction for the chat
|
||||
// and delivers the typed answer. It rejects expired entries and reports
|
||||
// whether the text was consumed as an answer.
|
||||
func (g *Gateway) consumePendingReply(key pendingReplyKey, text string) bool {
|
||||
g.interactiveMu.Lock()
|
||||
pr, found := g.pendingReplies[key]
|
||||
expired := !found || !pr.expiresAt.After(time.Now())
|
||||
if !expired {
|
||||
delete(g.pendingReplies, key)
|
||||
}
|
||||
g.interactiveMu.Unlock()
|
||||
if expired {
|
||||
return false
|
||||
}
|
||||
select {
|
||||
case pr.resultCh <- replyResult{text: text}:
|
||||
return true
|
||||
default:
|
||||
// The channel already has a result (the wait timed out between the
|
||||
// expiry check and now); do not report a reply that was not taken.
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
// deliverInteractiveReply routes a text reply to a blocked clarify/picker
|
||||
// request when one is pending for this chat and sender, and reports whether
|
||||
// the message was consumed. A consumed reply is not dispatched as a new turn;
|
||||
// a command is never consumed, so /stop still cancels the blocked turn.
|
||||
func (g *Gateway) deliverInteractiveReply(msg *models.Message) bool {
|
||||
if msg == nil || msg.From == nil {
|
||||
return false
|
||||
}
|
||||
text := strings.TrimSpace(msg.Text)
|
||||
if text == "" || strings.HasPrefix(text, "/") {
|
||||
return false
|
||||
}
|
||||
return g.consumePendingReply(pendingReplyKey{
|
||||
chatID: msg.Chat.ID,
|
||||
threadID: msg.MessageThreadID,
|
||||
recipient: msg.From.ID,
|
||||
}, text)
|
||||
}
|
||||
@@ -0,0 +1,168 @@
|
||||
package telegram
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/go-telegram/bot/models"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/domain/messaging"
|
||||
)
|
||||
|
||||
// waitForPendingReply polls the gateway's pending-reply map until a blocked
|
||||
// interaction has registered itself. Polling the guarded map is race-free:
|
||||
// the register happens-before the requester blocks, and the map is guarded by
|
||||
// interactiveMu.
|
||||
func waitForPendingReply(t *testing.T, g *Gateway) {
|
||||
t.Helper()
|
||||
deadline := time.Now().Add(2 * time.Second)
|
||||
for time.Now().Before(deadline) {
|
||||
g.interactiveMu.Lock()
|
||||
n := len(g.pendingReplies)
|
||||
g.interactiveMu.Unlock()
|
||||
if n > 0 {
|
||||
return
|
||||
}
|
||||
time.Sleep(5 * time.Millisecond)
|
||||
}
|
||||
t.Fatal("no pending interactive reply was registered")
|
||||
}
|
||||
|
||||
func interactiveReplyMessage(chatID, recipient int64, text string) *models.Message {
|
||||
return &models.Message{
|
||||
From: &models.User{ID: recipient},
|
||||
Chat: models.Chat{ID: chatID, Type: models.ChatTypePrivate},
|
||||
Text: text,
|
||||
}
|
||||
}
|
||||
|
||||
// sentPrompt returns the text of the first sendMessage whose body contains
|
||||
// want, so a test can assert what the adapter actually rendered.
|
||||
func sentPrompt(requests []telegramRequest, want string) string {
|
||||
for _, request := range requests {
|
||||
if request.method == "sendMessage" && strings.Contains(request.form["text"], want) {
|
||||
return request.form["text"]
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func TestInteractorReportsClarifyAndPicker(t *testing.T) {
|
||||
g, b, _ := newApprovalTestGateway(t)
|
||||
interactor := g.NewInteractor(b, approvalTestChatID, 0, approvalTestRecipient)
|
||||
if interactor.Clarifier == nil {
|
||||
t.Error("interactor does not report the Clarify capability")
|
||||
}
|
||||
if interactor.Picker == nil {
|
||||
t.Error("interactor does not report the Picker capability")
|
||||
}
|
||||
}
|
||||
|
||||
// TestInteractivePickUsesTextFallback is the acceptance test for step 4 at the
|
||||
// adapter: Telegram has no generic picker widget in the turn path, so the
|
||||
// choice must be rendered as numbered text and matched from the human's typed
|
||||
// reply rather than failing or being dropped.
|
||||
func TestInteractivePickUsesTextFallback(t *testing.T) {
|
||||
g, b, requests := newApprovalTestGateway(t)
|
||||
interactor := g.NewInteractor(b, approvalTestChatID, 0, approvalTestRecipient)
|
||||
|
||||
type outcome struct {
|
||||
choice messaging.InteractiveChoice
|
||||
err error
|
||||
}
|
||||
done := make(chan outcome, 1)
|
||||
go func() {
|
||||
choice, err := interactor.Picker.RequestChoice(context.Background(), messaging.PickerRequest{
|
||||
Prompt: "Which branch?",
|
||||
Options: []messaging.InteractiveChoice{
|
||||
{ID: "main", Label: "main"},
|
||||
{ID: "release", Label: "release"},
|
||||
},
|
||||
})
|
||||
done <- outcome{choice: choice, err: err}
|
||||
}()
|
||||
|
||||
waitForPendingReply(t, g)
|
||||
if !g.deliverInteractiveReply(interactiveReplyMessage(approvalTestChatID, approvalTestRecipient, "2")) {
|
||||
t.Fatal("deliverInteractiveReply did not consume the typed reply")
|
||||
}
|
||||
|
||||
select {
|
||||
case got := <-done:
|
||||
if got.err != nil {
|
||||
t.Fatalf("RequestChoice error = %v", got.err)
|
||||
}
|
||||
if got.choice.ID != "release" {
|
||||
t.Errorf("choice = %+v, want release", got.choice)
|
||||
}
|
||||
case <-time.After(2 * time.Second):
|
||||
t.Fatal("RequestChoice did not return after the reply was delivered")
|
||||
}
|
||||
|
||||
if prompt := sentPrompt(*requests, "Which branch?"); !strings.Contains(prompt, "1) main") || !strings.Contains(prompt, "2) release") {
|
||||
t.Errorf("picker prompt was not rendered as numbered text: %q", prompt)
|
||||
}
|
||||
}
|
||||
|
||||
func TestInteractiveClarifyReturnsTypedAnswer(t *testing.T) {
|
||||
g, b, _ := newApprovalTestGateway(t)
|
||||
interactor := g.NewInteractor(b, approvalTestChatID, 0, approvalTestRecipient)
|
||||
|
||||
type outcome struct {
|
||||
answer string
|
||||
err error
|
||||
}
|
||||
done := make(chan outcome, 1)
|
||||
go func() {
|
||||
answer, err := interactor.Clarifier.RequestClarification(context.Background(), messaging.ClarifyRequest{Question: "Which environment?"})
|
||||
done <- outcome{answer: answer, err: err}
|
||||
}()
|
||||
|
||||
waitForPendingReply(t, g)
|
||||
if !g.deliverInteractiveReply(interactiveReplyMessage(approvalTestChatID, approvalTestRecipient, " staging ")) {
|
||||
t.Fatal("deliverInteractiveReply did not consume the typed reply")
|
||||
}
|
||||
|
||||
select {
|
||||
case got := <-done:
|
||||
if got.err != nil {
|
||||
t.Fatalf("RequestClarification error = %v", got.err)
|
||||
}
|
||||
if got.answer != "staging" {
|
||||
t.Errorf("answer = %q, want staging (trimmed)", got.answer)
|
||||
}
|
||||
case <-time.After(2 * time.Second):
|
||||
t.Fatal("RequestClarification did not return after the reply was delivered")
|
||||
}
|
||||
}
|
||||
|
||||
// A command typed while a prompt is pending must reach the command handler, not
|
||||
// be swallowed as the answer: /stop is how the human cancels the blocked turn.
|
||||
func TestDeliverInteractiveReplyIgnoresCommands(t *testing.T) {
|
||||
g, b, _ := newApprovalTestGateway(t)
|
||||
interactor := g.NewInteractor(b, approvalTestChatID, 0, approvalTestRecipient)
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 200*time.Millisecond)
|
||||
defer cancel()
|
||||
done := make(chan error, 1)
|
||||
go func() {
|
||||
_, err := interactor.Clarifier.RequestClarification(ctx, messaging.ClarifyRequest{Question: "Which environment?"})
|
||||
done <- err
|
||||
}()
|
||||
|
||||
waitForPendingReply(t, g)
|
||||
if g.deliverInteractiveReply(interactiveReplyMessage(approvalTestChatID, approvalTestRecipient, "/stop")) {
|
||||
t.Error("deliverInteractiveReply consumed a command as the answer")
|
||||
}
|
||||
|
||||
select {
|
||||
case err := <-done:
|
||||
if err == nil {
|
||||
t.Error("RequestClarification returned nil error after its context expired")
|
||||
}
|
||||
case <-time.After(2 * time.Second):
|
||||
t.Fatal("RequestClarification did not return after its context expired")
|
||||
}
|
||||
}
|
||||
@@ -107,6 +107,13 @@ type Gateway struct {
|
||||
approvalMu sync.Mutex
|
||||
pendingApprovals map[string]*pendingApproval
|
||||
|
||||
// interactive state (gateway clarify/picker). A blocked clarify or
|
||||
// picker waits on one entry here, keyed by the chat it was posed to, so
|
||||
// the next text from that human is consumed as the answer rather than
|
||||
// dispatched as a new turn.
|
||||
interactiveMu sync.Mutex
|
||||
pendingReplies map[pendingReplyKey]*pendingReply
|
||||
|
||||
// turns serialises chat turns per session off the update worker and
|
||||
// makes the running one cancellable by /stop. Rebuilt on every launch
|
||||
// so a restart abandons in-flight turns with the old bot instance.
|
||||
@@ -142,6 +149,12 @@ type Gateway struct {
|
||||
// where the library's default endpoint is used. It exists so launch's
|
||||
// wiring can be tested without reaching api.telegram.org.
|
||||
serverURL string
|
||||
|
||||
// newEphemeralSender builds the retracting sender sendEphemeral uses.
|
||||
// Defaults to channels.NewEphemeralSender; tests override it to inject a
|
||||
// clock so a status notice's retraction fires without waiting out its
|
||||
// TTL, matching liveReply's newMediaSender seam.
|
||||
newEphemeralSender func() *channels.EphemeralSender
|
||||
}
|
||||
|
||||
type UpdateService interface {
|
||||
@@ -161,7 +174,7 @@ type ReleaseAnnouncer interface {
|
||||
|
||||
// New returns an unstarted Gateway. Call Start to begin long-polling.
|
||||
func New(token string, allowedUserIDs []int64, log *slog.Logger) *Gateway {
|
||||
return &Gateway{
|
||||
g := &Gateway{
|
||||
Token: token,
|
||||
AllowedUserIDs: allowedUserIDs,
|
||||
restartCh: make(chan restartRequest, 1),
|
||||
@@ -172,8 +185,13 @@ func New(token string, allowedUserIDs []int64, log *slog.Logger) *Gateway {
|
||||
dangerousActions: make(map[string]dangerousAction),
|
||||
permanentApprovals: nil,
|
||||
pendingApprovals: make(map[string]*pendingApproval),
|
||||
pendingReplies: make(map[pendingReplyKey]*pendingReply),
|
||||
log: log.With("component", "gateway-telegram"),
|
||||
}
|
||||
g.newEphemeralSender = func() *channels.EphemeralSender {
|
||||
return channels.NewEphemeralSender(g.log, nil)
|
||||
}
|
||||
return g
|
||||
}
|
||||
|
||||
func (g *Gateway) Name() string { return "telegram" }
|
||||
@@ -221,8 +239,12 @@ func (g *Gateway) Start(ctx context.Context, client messaging.ChatContract, life
|
||||
return err
|
||||
}
|
||||
// Confirm to whoever asked, now that the new instance can send.
|
||||
// It is a status notice, not a record: it retracts itself after a
|
||||
// short TTL so it does not sit in the chat as a stale "reloaded"
|
||||
// line. The pre-restart "Reloading…" message from the outgoing
|
||||
// instance stays as the durable acknowledgement.
|
||||
if req := g.pendingRestart; req != nil && req.chatID != 0 {
|
||||
g.sendMessage(runCtx, b, req.chatID, req.threadID, "✅ Archie reloaded.")
|
||||
g.sendEphemeral(runCtx, b, req.chatID, req.threadID, "✅ Archie reloaded.")
|
||||
}
|
||||
if g.pendingRestart != nil {
|
||||
g.pendingRestart = nil
|
||||
@@ -532,6 +554,11 @@ func (g *Gateway) defaultHandler(client messaging.ChatContract) bot.HandlerFunc
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
// A text reply to a pending clarify/picker prompt is consumed here so
|
||||
// it answers the blocked interaction instead of starting a new turn.
|
||||
if g.deliverInteractiveReply(msg) {
|
||||
return
|
||||
}
|
||||
// Media messages arrive before the text gates: a photo has no text,
|
||||
// so checking it first is what keeps attachments from being dropped.
|
||||
if _, isMedia := extractInboundMedia(msg); isMedia {
|
||||
@@ -600,8 +627,9 @@ func (g *Gateway) handleCallback(ctx context.Context, b *bot.Bot, update *models
|
||||
// turnInbound renders a Telegram message as the channel-neutral inbound a
|
||||
// chat turn is built from, with media attachments attached. ok is false
|
||||
// for messages with neither text nor media, which are not turns at all.
|
||||
func turnInbound(msg *models.Message) (messaging.Inbound, bool) {
|
||||
media, hasMedia := extractInboundMedia(msg)
|
||||
// media/hasMedia are the caller's already-extracted attachment, so the
|
||||
// caller keeps the voice-note flag it needs without extracting twice.
|
||||
func turnInbound(msg *models.Message, media inboundMedia, hasMedia bool) (messaging.Inbound, bool) {
|
||||
if !hasMedia && msg.Text == "" {
|
||||
return messaging.Inbound{}, false
|
||||
}
|
||||
@@ -629,7 +657,8 @@ func turnInbound(msg *models.Message) (messaging.Inbound, bool) {
|
||||
}
|
||||
|
||||
func (g *Gateway) submitTurn(ctx context.Context, b *bot.Bot, msg *models.Message, client messaging.ChatContract) {
|
||||
gm, isTurn := turnInbound(msg)
|
||||
media, hasMedia := extractInboundMedia(msg)
|
||||
gm, isTurn := turnInbound(msg, media, hasMedia)
|
||||
if !isTurn {
|
||||
return
|
||||
}
|
||||
@@ -649,6 +678,10 @@ func (g *Gateway) submitTurn(ctx context.Context, b *bot.Bot, msg *models.Messag
|
||||
approver := g.NewApprover(b, chatID, threadID, msg.From.ID)
|
||||
turnCtx = messaging.WithApprovalRequester(turnCtx, approver)
|
||||
|
||||
// A clarify or picker prompt blocks the turn on this adapter; when the
|
||||
// channel cannot carry one the question tool is simply not offered.
|
||||
turnCtx = messaging.WithInteractive(turnCtx, g.NewInteractor(b, chatID, threadID, msg.From.ID))
|
||||
|
||||
// reply appears as it is written; the typing indicator covers the
|
||||
// gap before the first token and any non-streaming path.
|
||||
stopTyping := g.startTyping(turnCtx, b, chatID, threadID)
|
||||
@@ -865,12 +898,14 @@ func (g *Gateway) sendMessage(ctx context.Context, b *bot.Bot, chatID int64, mes
|
||||
}
|
||||
|
||||
// sendBlocks delivers one message as a Telegram rich message built from
|
||||
// structured blocks.
|
||||
// structured blocks and returns the Bot API message ID, or 0 when nothing was
|
||||
// delivered. The ID is what an EphemeralReply's retraction addresses; callers
|
||||
// that only send ignore it.
|
||||
//
|
||||
// Rich messages are a recent Bot API addition, so a rejection here is treated
|
||||
// as "unsupported" rather than fatal: fall back to sending the blocks as plain
|
||||
// text so the user still receives the reply, unformatted, instead of silence.
|
||||
func (g *Gateway) sendBlocks(ctx context.Context, b *bot.Bot, chatID int64, messageThreadID int, blocks []models.InputRichBlock, errMsg string) {
|
||||
func (g *Gateway) sendBlocks(ctx context.Context, b *bot.Bot, chatID int64, messageThreadID int, blocks []models.InputRichBlock, errMsg string) int {
|
||||
rich := &bot.SendRichMessageParams{
|
||||
ChatID: chatID,
|
||||
RichMessage: models.InputRichMessage{Blocks: blocks},
|
||||
@@ -878,9 +913,9 @@ func (g *Gateway) sendBlocks(ctx context.Context, b *bot.Bot, chatID int64, mess
|
||||
if messageThreadID != 0 {
|
||||
rich.MessageThreadID = messageThreadID
|
||||
}
|
||||
if _, err := b.SendRichMessage(ctx, rich); err == nil {
|
||||
if msg, err := b.SendRichMessage(ctx, rich); err == nil {
|
||||
g.log.Debug("telegram message sent", "renderer", telegramRenderVersion, "mode", "rich")
|
||||
return
|
||||
return msg.ID
|
||||
} else {
|
||||
g.log.Warn("rich send failed, retrying unformatted", "error", err)
|
||||
}
|
||||
@@ -889,10 +924,12 @@ func (g *Gateway) sendBlocks(ctx context.Context, b *bot.Bot, chatID int64, mess
|
||||
if messageThreadID != 0 {
|
||||
plain.MessageThreadID = messageThreadID
|
||||
}
|
||||
if _, err := b.SendMessage(ctx, plain); err != nil {
|
||||
if msg, err := b.SendMessage(ctx, plain); err != nil {
|
||||
g.log.Error(errMsg, "error", err)
|
||||
return 0
|
||||
} else {
|
||||
g.log.Warn("telegram message sent via plain fallback", "renderer", telegramRenderVersion, "mode", "plain")
|
||||
return msg.ID
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -140,6 +140,77 @@ func TestReportPendingUpdateSendsAndClearsReport(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// TestReportPendingUpdateVerifiesAgainstRunningVersions drives the acceptance
|
||||
// both ways through the real relay: a wired RunningVersions must reach the
|
||||
// message the operator reads, confirming a claim this process can vouch for and
|
||||
// contradicting one it cannot. Without the wiring the same report relays as
|
||||
// "I still can't confirm which version" on every success, which is the bug this
|
||||
// covers.
|
||||
func TestReportPendingUpdateVerifiesAgainstRunningVersions(t *testing.T) {
|
||||
const (
|
||||
daemon = releaseupdate.ComponentDaemon
|
||||
previous = "1.0.0"
|
||||
installed = "1.2.3"
|
||||
)
|
||||
tests := []struct {
|
||||
name string
|
||||
installed string
|
||||
wantText []string
|
||||
denyText []string
|
||||
}{
|
||||
{
|
||||
name: "confirmed when the running version matches the claim",
|
||||
installed: installed,
|
||||
wantText: []string{"update complete"},
|
||||
denyText: []string{"can't confirm", "did not take effect"},
|
||||
},
|
||||
{
|
||||
name: "contradicted when the running version differs from the claim",
|
||||
installed: "9.9.9",
|
||||
wantText: []string{"did not take effect", installed},
|
||||
denyText: []string{"update complete", "can't confirm"},
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
path := filepath.Join(t.TempDir(), "update-report.json")
|
||||
report := releaseupdate.Report{
|
||||
Channel: "telegram", ChatID: 7, ThreadID: 3,
|
||||
Previous: map[string]string{daemon: previous},
|
||||
Installed: map[string]string{daemon: test.installed},
|
||||
HealthCheck: "passed",
|
||||
}
|
||||
if err := releaseupdate.WritePendingReport(path, report); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
g := New("1:test", []int64{42}, slog.Default())
|
||||
g.UpdateReportPath = path
|
||||
g.RunningVersions = func() map[string]string { return map[string]string{daemon: installed} }
|
||||
b, requests := newTelegramTestBot(t)
|
||||
|
||||
g.reportPendingUpdate(context.Background(), b)
|
||||
|
||||
if len(*requests) != 1 {
|
||||
t.Fatalf("requests = %#v, want exactly one", *requests)
|
||||
}
|
||||
// sendMessage delivers a rich message first, so the operator text is
|
||||
// the rich_message body (or the plain text field on a fallback).
|
||||
text := strings.ToLower((*requests)[0].form["rich_message"] + " " + (*requests)[0].form["text"])
|
||||
for _, want := range test.wantText {
|
||||
if !strings.Contains(text, want) {
|
||||
t.Errorf("message = %q, want it to contain %q", text, want)
|
||||
}
|
||||
}
|
||||
for _, deny := range test.denyText {
|
||||
if strings.Contains(text, deny) {
|
||||
t.Errorf("message = %q, want it NOT to contain %q", text, deny)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestReportPendingUpdateClearsUnreadableReport: a corrupt report file
|
||||
// (partial write, disk issue, a future format the running binary doesn't
|
||||
// understand) must not jam forever -- without clearing it, every future
|
||||
|
||||
+41
-13
@@ -598,6 +598,11 @@ type IdentityConfig struct {
|
||||
type ModelLimits struct {
|
||||
ContextWindow int `json:"context_window"`
|
||||
MaxOutputTokens int `json:"max_output_tokens"`
|
||||
// Reasoning marks a reasoning-class model. It is derived from the model
|
||||
// catalog (never operator-authored) and carried to the agent worker so a
|
||||
// task-scoped MCP sampling request omits the max-token bound those models
|
||||
// reject, exactly as the daemon's chat path does.
|
||||
Reasoning bool `json:"reasoning,omitempty"`
|
||||
}
|
||||
|
||||
// TaskConfig is the non-secret subset of Config needed to run workflow
|
||||
@@ -605,17 +610,22 @@ type ModelLimits struct {
|
||||
type TaskConfig struct {
|
||||
// BotUser and BotEmail are non-secret commit attribution carried to the
|
||||
// sandbox because the worker owns local deterministic commits.
|
||||
BotUser string `json:"bot_user"`
|
||||
BotEmail string `json:"bot_email"`
|
||||
Models map[string]string `json:"models"`
|
||||
ModelLimits map[string]ModelLimits `json:"model_limits,omitempty"`
|
||||
Budgets Budgets `json:"budgets"`
|
||||
Dispatch Dispatch `json:"dispatch"`
|
||||
DiffCapLines int `json:"diff_cap_lines"`
|
||||
Notify Notify `json:"notify"`
|
||||
Forge TaskForge `json:"forge"`
|
||||
ToolPolicy ToolPolicy `json:"tool_policy"`
|
||||
Review Review `json:"review"`
|
||||
BotUser string `json:"bot_user"`
|
||||
BotEmail string `json:"bot_email"`
|
||||
Models map[string]string `json:"models"`
|
||||
ModelLimits map[string]ModelLimits `json:"model_limits,omitempty"`
|
||||
Budgets Budgets `json:"budgets"`
|
||||
// MaxRetries is the global retry/remediation cap workflow stages read
|
||||
// (remediate's round cap via Repo.EffectiveMaxRetries). It is carried to the
|
||||
// worker for the same reason as Budgets: the stage that enforces it runs
|
||||
// inside archie-agent, not the daemon.
|
||||
MaxRetries int `json:"max_retries"`
|
||||
Dispatch Dispatch `json:"dispatch"`
|
||||
DiffCapLines int `json:"diff_cap_lines"`
|
||||
Notify Notify `json:"notify"`
|
||||
Forge TaskForge `json:"forge"`
|
||||
ToolPolicy ToolPolicy `json:"tool_policy"`
|
||||
Review Review `json:"review"`
|
||||
}
|
||||
|
||||
// DiffCapOf returns a DiffCapLines value for n. It exists because the field is
|
||||
@@ -651,6 +661,7 @@ func (c Config) ForTask() TaskConfig {
|
||||
Models: cloneStringMap(c.Models),
|
||||
ModelLimits: maps.Clone(c.ModelLimits),
|
||||
Budgets: c.Budgets,
|
||||
MaxRetries: c.MaxRetries,
|
||||
Dispatch: Dispatch{Trigger: c.Dispatch.Trigger, AckReaction: c.Dispatch.AckReaction, Labels: cloneStringMap(c.Dispatch.Labels)},
|
||||
DiffCapLines: c.DiffCap(),
|
||||
Notify: c.Notify,
|
||||
@@ -671,6 +682,7 @@ func (tc TaskConfig) ToConfig() Config {
|
||||
Models: cloneStringMap(tc.Models),
|
||||
ModelLimits: maps.Clone(tc.ModelLimits),
|
||||
Budgets: tc.Budgets,
|
||||
MaxRetries: tc.MaxRetries,
|
||||
Dispatch: Dispatch{Trigger: tc.Dispatch.Trigger, AckReaction: tc.Dispatch.AckReaction, Labels: cloneStringMap(tc.Dispatch.Labels)},
|
||||
DiffCapLines: &tc.DiffCapLines,
|
||||
Notify: tc.Notify,
|
||||
@@ -897,6 +909,20 @@ type ContainerConfig struct {
|
||||
// seeded from this field but validated and stored (and reloaded live)
|
||||
// separately -- see internal/app/controlplane.
|
||||
Credentials []CredentialBinding `toml:"credentials" yaml:"credentials" json:"-"`
|
||||
// RegistryAuth is the credential a private registry requires when the
|
||||
// configured image (or a profile's) is pulled. It uses the same
|
||||
// secret-reference shape as every other credential in this file; the
|
||||
// resolved value is a Docker registry.AuthConfig JSON document, e.g.
|
||||
// {"username":"bot","password":"..."} ("identitytoken",
|
||||
// "registrytoken" and "serveraddress" are honoured when present but
|
||||
// optional -- the daemon derives the host from the image reference). The
|
||||
// zero value means anonymous pulls, which is what the pool has always
|
||||
// done; it never fails a deployment that has no private registry.
|
||||
// RegistryAuth is file-owned, not part of the control-plane JSON document
|
||||
// (container-runtime-policies): it is a secret reference, resolved at boot
|
||||
// and restart-required, so the pool receives the resolved value rather
|
||||
// than a stored one. See internal/container's encodeRegistryAuth.
|
||||
RegistryAuth SecretRef `toml:"registry_auth" yaml:"registry_auth" json:"-"`
|
||||
}
|
||||
|
||||
// CredentialBinding maps one Kit credential@1 service name to an org secret.
|
||||
@@ -1141,7 +1167,8 @@ type ChatConfig struct {
|
||||
// limiting off entirely -- it is an opt-in control, not a default
|
||||
// throttle that could surprise an existing deployment. This key seeds
|
||||
// the Channel settings resource, which the Web UI owns at runtime
|
||||
// (restart-required): editing the file does not change a stored value.
|
||||
// (live for the channel transports): editing the file does not change a
|
||||
// stored value.
|
||||
RateLimit RateLimitConfig `toml:"rate_limit" yaml:"rate_limit"`
|
||||
}
|
||||
|
||||
@@ -1151,7 +1178,8 @@ type ChatConfig struct {
|
||||
// internal/app/controlplane); the file key is only that resource's seed, and
|
||||
// the Web UI owns the value a running deployment limits with. Applied by
|
||||
// boot.startRateLimiter from the config boot.loadRuntimeConfig layers, so a
|
||||
// stored value takes effect on restart and the resource is restart-required.
|
||||
// stored change to the limit takes effect on the next Gateway restart; the
|
||||
// channel transports are the half the Messaging Service reconciles live.
|
||||
//
|
||||
// A sender is identified per channel: Telegram by numeric user ID, email by the
|
||||
// SMTP from address, webhook by the configured route path -- which is a source,
|
||||
|
||||
Loaded 100 of 325 files, more files were not shown because too many files have changed in this diff.
Show more
Reference in new issue
Block a user