@a5c-ai/krate 5.0.1-staging.f672fe79b

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (174) hide show
  1. package/Dockerfile +29 -0
  2. package/README.md +183 -0
  3. package/bin/krate-demo.mjs +23 -0
  4. package/bin/krate-server.mjs +14 -0
  5. package/dist/krate-controller-ui.json +2407 -0
  6. package/dist/krate-lifecycle.json +201 -0
  7. package/dist/krate-runtime-snapshot.json +2955 -0
  8. package/dist/krate-summary.json +687 -0
  9. package/docs/README.md +61 -0
  10. package/docs/agents/README.md +83 -0
  11. package/docs/agents/acceptance-test-matrix.md +193 -0
  12. package/docs/agents/agent-mux-adapter-contract.md +167 -0
  13. package/docs/agents/agent-mux-source-map.md +310 -0
  14. package/docs/agents/agent-run-memory-import-spec.md +256 -0
  15. package/docs/agents/agent-stack-management-spec.md +421 -0
  16. package/docs/agents/api-contract-spec.md +309 -0
  17. package/docs/agents/artifacts-writeback-spec.md +145 -0
  18. package/docs/agents/chart-packaging-spec.md +128 -0
  19. package/docs/agents/ci-orchestration-spec.md +140 -0
  20. package/docs/agents/context-assembly-spec.md +219 -0
  21. package/docs/agents/controller-reconciliation-spec.md +255 -0
  22. package/docs/agents/crd-schema-spec.md +315 -0
  23. package/docs/agents/decision-log-open-questions.md +169 -0
  24. package/docs/agents/developer-implementation-checklist.md +329 -0
  25. package/docs/agents/dispatching-design.md +262 -0
  26. package/docs/agents/glossary.md +66 -0
  27. package/docs/agents/implementation-blueprint.md +324 -0
  28. package/docs/agents/implementation-rollout-slices.md +251 -0
  29. package/docs/agents/memory-context-integration-spec.md +194 -0
  30. package/docs/agents/memory-ontology-schema-spec.md +253 -0
  31. package/docs/agents/memory-operations-runbook.md +121 -0
  32. package/docs/agents/mvp-vertical-slice-spec.md +146 -0
  33. package/docs/agents/observability-audit-spec.md +265 -0
  34. package/docs/agents/operator-runbook.md +174 -0
  35. package/docs/agents/org-memory-api-payload-examples.md +333 -0
  36. package/docs/agents/org-memory-controller-sequence-spec.md +181 -0
  37. package/docs/agents/org-memory-e2e-fixture-plan.md +161 -0
  38. package/docs/agents/org-memory-ui-implementation-map.md +114 -0
  39. package/docs/agents/org-memory-vertical-slice-spec.md +168 -0
  40. package/docs/agents/org-resource-model-delta-spec.md +111 -0
  41. package/docs/agents/org-route-resource-model-spec.md +183 -0
  42. package/docs/agents/org-scoping-namespace-spec.md +114 -0
  43. package/docs/agents/rbac-secrets-management-spec.md +406 -0
  44. package/docs/agents/repository-page-integration-spec.md +255 -0
  45. package/docs/agents/resource-contract-examples.md +808 -0
  46. package/docs/agents/resource-relationship-map.md +190 -0
  47. package/docs/agents/security-threat-model.md +188 -0
  48. package/docs/agents/shared-memory-company-brain-spec.md +358 -0
  49. package/docs/agents/storage-migration-spec.md +168 -0
  50. package/docs/agents/subagent-orchestration-spec.md +152 -0
  51. package/docs/agents/system-overview.md +88 -0
  52. package/docs/agents/tools-mcp-skills-spec.md +189 -0
  53. package/docs/agents/traceability-matrix.md +79 -0
  54. package/docs/agents/ui-flow-spec.md +211 -0
  55. package/docs/agents/ui-ux-system-spec.md +426 -0
  56. package/docs/agents/workspace-lifecycle-spec.md +166 -0
  57. package/docs/architecture-spec.md +78 -0
  58. package/docs/components/control-plane.md +78 -0
  59. package/docs/components/data-plane.md +69 -0
  60. package/docs/components/hooks-events.md +67 -0
  61. package/docs/components/identity-rbac-policy.md +73 -0
  62. package/docs/components/kubevela-oam.md +70 -0
  63. package/docs/components/operations-publishing.md +81 -0
  64. package/docs/components/runners-ci.md +66 -0
  65. package/docs/components/web-ui.md +94 -0
  66. package/docs/external/README.md +47 -0
  67. package/docs/external/bidirectional-sync-design.md +134 -0
  68. package/docs/external/cicd-interface.md +64 -0
  69. package/docs/external/external-backend-controllers.md +170 -0
  70. package/docs/external/external-backend-crds.md +234 -0
  71. package/docs/external/external-backend-ui-spec.md +151 -0
  72. package/docs/external/external-backend-ux-flows.md +115 -0
  73. package/docs/external/external-object-mapping.md +125 -0
  74. package/docs/external/git-forge-interface.md +68 -0
  75. package/docs/external/github-integration-design.md +151 -0
  76. package/docs/external/issue-tracking-interface.md +66 -0
  77. package/docs/external/provider-capability-manifests.md +204 -0
  78. package/docs/external/provider-catalog.md +139 -0
  79. package/docs/external/provider-rollout-testing.md +78 -0
  80. package/docs/external/research-results.md +48 -0
  81. package/docs/external/security-auth-permissions.md +81 -0
  82. package/docs/external/sync-state-machines.md +108 -0
  83. package/docs/external/unified-external-backend-model.md +107 -0
  84. package/docs/external/user-facing-changes.md +67 -0
  85. package/docs/gaps.md +161 -0
  86. package/docs/install.md +94 -0
  87. package/docs/krate-design.md +334 -0
  88. package/docs/local-minikube.md +55 -0
  89. package/docs/ontology/README.md +32 -0
  90. package/docs/ontology/bounded-contexts.md +29 -0
  91. package/docs/ontology/events-and-hooks.md +32 -0
  92. package/docs/ontology/oam-kubevela.md +32 -0
  93. package/docs/ontology/operations-and-release.md +25 -0
  94. package/docs/ontology/personas-and-actors.md +32 -0
  95. package/docs/ontology/policies-and-invariants.md +33 -0
  96. package/docs/ontology/problem-space.md +30 -0
  97. package/docs/ontology/resource-contracts.md +40 -0
  98. package/docs/ontology/resource-taxonomy.md +42 -0
  99. package/docs/ontology/runners-and-ci.md +29 -0
  100. package/docs/ontology/solution-space.md +24 -0
  101. package/docs/ontology/storage-and-data-boundaries.md +29 -0
  102. package/docs/ontology/validation-matrix.md +24 -0
  103. package/docs/ontology/web-ui-excellent-flows.md +32 -0
  104. package/docs/ontology/workflows.md +39 -0
  105. package/docs/ontology/world.md +35 -0
  106. package/docs/product-requirements.md +62 -0
  107. package/docs/roadmap-mvp.md +87 -0
  108. package/docs/system-requirements.md +90 -0
  109. package/docs/tests/README.md +53 -0
  110. package/docs/tests/agent-qa-plan.md +63 -0
  111. package/docs/tests/browser-ui-tests.md +62 -0
  112. package/docs/tests/ci-quality-gates.md +48 -0
  113. package/docs/tests/coverage-model.md +64 -0
  114. package/docs/tests/e2e-scenario-tests.md +53 -0
  115. package/docs/tests/fixtures-test-data.md +63 -0
  116. package/docs/tests/observability-reliability-tests.md +54 -0
  117. package/docs/tests/product-test-matrix.md +145 -0
  118. package/docs/tests/qa-adoption-roadmap.md +130 -0
  119. package/docs/tests/qa-automation-plan.md +101 -0
  120. package/docs/tests/security-compliance-tests.md +57 -0
  121. package/docs/tests/test-framework-tools.md +88 -0
  122. package/docs/tests/test-suite-layout.md +121 -0
  123. package/docs/tests/unit-integration-tests.md +48 -0
  124. package/docs/todo-kyverno +714 -0
  125. package/docs/user-stories.md +78 -0
  126. package/examples/minikube-demo.yaml +190 -0
  127. package/examples/oam-application.yaml +23 -0
  128. package/examples/policy-kyverno-pr-title.yaml +18 -0
  129. package/package.json +63 -0
  130. package/scripts/build.mjs +29 -0
  131. package/scripts/setup-minikube.mjs +65 -0
  132. package/scripts/smoke.mjs +37 -0
  133. package/scripts/validate-doc-coverage.mjs +152 -0
  134. package/scripts/validate-package.mjs +93 -0
  135. package/scripts/validate-ui.mjs +207 -0
  136. package/src/agent-approval-controller.js +123 -0
  137. package/src/agent-context-bundles.js +242 -0
  138. package/src/agent-dispatch-controller.js +86 -0
  139. package/src/agent-mux-client.js +280 -0
  140. package/src/agent-permission-review.js +162 -0
  141. package/src/agent-stack-controller.js +296 -0
  142. package/src/agent-trigger-controller.js +108 -0
  143. package/src/api-controller.js +206 -0
  144. package/src/argocd-gitops.js +43 -0
  145. package/src/auth.js +265 -0
  146. package/src/component-catalog.js +41 -0
  147. package/src/control-plane.js +136 -0
  148. package/src/controller-client.js +38 -0
  149. package/src/controller-ui.js +538 -0
  150. package/src/data-plane.js +178 -0
  151. package/src/gitea-backend.js +95 -0
  152. package/src/handoff.js +98 -0
  153. package/src/hooks-events.js +63 -0
  154. package/src/http-server.js +151 -0
  155. package/src/identity-policy.js +86 -0
  156. package/src/index.js +30 -0
  157. package/src/kubernetes-controller.js +812 -0
  158. package/src/kubernetes-resource-gateway.js +48 -0
  159. package/src/operations.js +112 -0
  160. package/src/resource-model.js +203 -0
  161. package/src/runners-ci.js +48 -0
  162. package/src/runtime.js +196 -0
  163. package/src/web-ui.js +40 -0
  164. package/tests/agent-approval-controller.test.js +173 -0
  165. package/tests/agent-context-bundles.test.js +278 -0
  166. package/tests/agent-dispatch-controller.test.js +176 -0
  167. package/tests/agent-mux-client.test.js +204 -0
  168. package/tests/agent-permission-review.test.js +209 -0
  169. package/tests/agent-resources.test.js +212 -0
  170. package/tests/agent-stack-controller.test.js +221 -0
  171. package/tests/agent-trigger-controller.test.js +211 -0
  172. package/tests/deployment.test.js +395 -0
  173. package/tests/e2e/lifecycle.test.js +117 -0
  174. package/tests/krate.test.js +727 -0
@@ -0,0 +1,140 @@
1
+ # Agent CI orchestration spec
2
+
3
+ ## Purpose
4
+
5
+ CI is one source of agent work in the broader Krate agent orchestration model. Failed checks, pipeline events, flaky-test clusters, release gates, and artifact reviews should select a configured `AgentStack` and create an `AgentDispatchRun` that appears beside normal `Pipeline` and `Job` records.
6
+
7
+ The canonical stack/resource model lives in [Agent stack management spec](./agent-stack-management-spec.md). This document defines the CI-specific trigger, context, runner, and acceptance requirements.
8
+
9
+ ## Primary CI use cases
10
+
11
+ ### Failed PR check diagnosis
12
+
13
+ 1. A `Pipeline` or `Job` fails for a PR.
14
+ 2. Krate extracts the failed job, step, log window, failure signature, artifacts, changed files, branch, commit SHA, and related PR discussion.
15
+ 3. The PR check panel offers `Diagnose with agent`.
16
+ 4. A trigger rule or manual action selects an `AgentStack` such as `claude-code-ci-diagnoser`.
17
+ 5. The agent receives a bounded context bundle with logs, artifacts, diff context, repo policy, and requested output format.
18
+ 6. The agent returns a diagnosis, likely fix paths, subagent findings, and optional patch artifact.
19
+
20
+ ### Failed PR check repair
21
+
22
+ 1. A maintainer chooses `Attempt repair` from a failed check or comments `@agent fix failing check`.
23
+ 2. Krate creates an `AgentDispatchRun` linked to the failed `Pipeline`, `Job`, PR, and commit SHA.
24
+ 3. The run is scheduled on a runner pool compatible with the PR trust tier.
25
+ 4. The selected stack may invoke subagents such as diagnoser, test-fixer, reviewer, or validator.
26
+ 5. The agent produces a patch artifact, branch proposal, suggested diff, or PR update request.
27
+ 6. Krate requires approval before pushing commits, updating the PR, submitting a review, or rerunning privileged workflows.
28
+
29
+ ### Flaky test triage
30
+
31
+ 1. Krate groups recent failures by `failure.signature`, job metadata, test names, and affected paths.
32
+ 2. A scheduled or manual trigger runs a triage stack against failed and passing runs.
33
+ 3. The agent summarizes suspected flake source, affected tests, owners, confidence, and recommended next actions.
34
+ 4. Krate writes findings to an issue/work item/saved triage view only when policy allows.
35
+
36
+ ### Release gate review
37
+
38
+ 1. Develop, staging, main, or tag publish workflows produce chart, image, UI, package-validation, checksum artifacts, and branch deployments where configured.
39
+ 2. A release-review stack inspects artifact completeness, checksum consistency, package/chart/image version alignment, release note gaps, and deployment policy.
40
+ 3. The agent cannot publish directly; it produces a release-readiness report unless a privileged approval path is configured.
41
+
42
+ ## CI signals agents need
43
+
44
+ Agents should not scrape CI pages. Krate should pass structured context from its own resources and artifacts.
45
+
46
+ Required context bundle fields:
47
+
48
+ - `repository`: name, default branch, clone URL, visibility, trust tier;
49
+ - `ref`: branch, commit SHA, PR head/base, fork status;
50
+ - `source`: failed check, manual, PR comment, issue comment, label, scheduled scan, webhook;
51
+ - `actor`: user, bot, team, permission decision;
52
+ - `pullRequest`: title, body, labels, changed files, review state, mergeability;
53
+ - `pipeline`: workflow name, run ID, status, conclusion, check name, URL;
54
+ - `job`: job ID, step name, exit code, runner pool, image, duration;
55
+ - `logs`: bounded log excerpts with redaction status;
56
+ - `artifacts`: artifact names, types, URLs, digests, retention;
57
+ - `failure`: signature, stack trace, test names, file paths, similar runs;
58
+ - `stack`: selected `AgentStack`, tool profile, MCP servers, skills, subagents, approval mode;
59
+ - `policy`: allowed tools, write-back mode, approval mode, network/secret restrictions;
60
+ - `identity`: runtime ServiceAccount, runner ServiceAccount, native roles, Secret grants, and ConfigMap grants;
61
+ - `contextLabels`: selected prompt fragments and provenance.
62
+
63
+ ## CI trigger contracts
64
+
65
+ ### Failed check trigger
66
+
67
+ - Input: `Pipeline` or `Job` status transitions to failed.
68
+ - Match keys: repository, PR, workflow, job, step, branch, path filters, failure signature.
69
+ - Dedupe key: repository + PR/check source + commit SHA + job + step + failure signature + rule.
70
+ - Default action: create a diagnosis suggestion; auto-run only if repository policy enables it.
71
+ - Required context: failed log excerpt, job metadata, changed files, artifacts, similar failures.
72
+
73
+ ### Check rerun trigger
74
+
75
+ - Input: rerun requested by human, rule, or approved agent output.
76
+ - Match keys: previous dispatch run, target pipeline/job/step, actor, approval record.
77
+ - Dedupe key: original run + approved artifact digest + target check.
78
+ - Default action: create a new `Pipeline` resource or external workflow rerun and link it back to the agent dispatch.
79
+
80
+ ### Release trigger
81
+
82
+ - Input: tag push, main publish workflow, or release-candidate schedule.
83
+ - Match keys: version, chart package, npm artifact, image tag, checksum artifact, release branch.
84
+ - Dedupe key: version + commit SHA + artifact digest set.
85
+ - Default action: produce a release-readiness report and approval item for privileged publishing gaps.
86
+
87
+ ### Flaky triage trigger
88
+
89
+ - Input: scheduled scan or repeated failure signature threshold.
90
+ - Match keys: failure signature, job name, test name, branch, path, owner, time window.
91
+ - Dedupe key: signature + query digest + result set digest.
92
+ - Default action: summarize, create/update work item, and optionally dispatch repair after approval.
93
+
94
+ ## Runner and execution requirements
95
+
96
+ - Agent dispatch attempts must schedule through `RunnerPool` policy or an explicitly configured external Agent Mux gateway.
97
+ - Fork PRs and untrusted refs must use untrusted pools and receive no privileged secrets.
98
+ - Trusted agents may receive scoped secrets only through runner policy and only for approved task kinds.
99
+ - Runner pods must use a policy-selected Kubernetes ServiceAccount, and untrusted/forked refs must not receive privileged ServiceAccounts.
100
+ - CI-triggered agents must validate `AgentSecretGrant`, `AgentConfigGrant`, and native RBAC before launch.
101
+ - Tool, MCP, skill, and model-provider Secret/ConfigMap requirements must be shown in the run context snapshot.
102
+ - Agent workspaces must be isolated per dispatch attempt and bound to repository/ref/pipeline identity.
103
+ - Long-running sessions must publish queue, start, heartbeat, token/cost, subagent, artifact, approval, and terminal events.
104
+ - Cancelling an `AgentDispatchRun` must cancel the active Agent Mux run/session and mark the current attempt cancelled.
105
+ - Rerun-from-step or rerun-after-fix must create new `Pipeline` resources or external workflow attempts and link them to the agent dispatch.
106
+
107
+ ## Approval and write-back requirements
108
+
109
+ - Diagnosis summaries can be posted automatically only when repository policy allows bot comments.
110
+ - Pushing commits, updating PR branches, opening PRs, approving reviews, rerunning privileged workflows, publishing release artifacts, or accessing privileged MCP/tool/secret/network capabilities must require explicit approval unless a repository admin configures a narrower exception.
111
+ - Every write-back records actor, approving user, trigger rule, agent stack snapshot, dispatch attempt, context bundle digest, prompt hash, artifact digest, and target object.
112
+ - Approval UI must show assembled prompt, context labels, stack/tools/MCP/skills/subagents, runner pool, runtime ServiceAccount, runner ServiceAccount, requested Secret/ConfigMap access, requested action, and target branch/PR/check.
113
+
114
+ ## Chat and run view requirements
115
+
116
+ The run detail page should feel like a CI check page plus an Agent Mux transcript.
117
+
118
+ Recommended layout:
119
+
120
+ - Header: repository, source object, task kind, status, agent stack, runner pool, runtime ServiceAccount, runner ServiceAccount, linked check, branch/SHA, approval state.
121
+ - Left panel: PR/check context, failed step, changed files, labels, context labels, logs, artifacts.
122
+ - Center panel: Agent Mux transcript and live event stream.
123
+ - Right panel: attempts, queue timing, runner pod/job, ServiceAccounts, native RBAC, tools/actions, MCP servers, skills, Secret/ConfigMap grants, subagents, approvals, artifacts, write-back controls.
124
+ - Footer/composer: continuation prompt, attach more context, approve/reject action, cancel/retry/resume/fork when supported.
125
+
126
+ ## Observability requirements
127
+
128
+ - Metrics: queued dispatches, wait latency, duration, cancellation count, approval wait time, token/cost estimate, subagent count, write-back count, failed dispatches, dedupe drops.
129
+ - Events: trigger matched, dispatch skipped, context assembled, approval requested, run queued, run started, subagent started/completed, artifact produced, write-back requested, write-back completed.
130
+ - Logs: Agent Mux gateway calls, runner scheduling decisions, ServiceAccount selection, RBAC admission decisions, Secret/ConfigMap grant decisions, context redaction decisions, webhook/CI event correlation IDs, MCP health checks.
131
+
132
+ ## Acceptance criteria
133
+
134
+ - A failed PR check can create a linked agent diagnosis run with bounded logs, artifact references, and selected `AgentStack` snapshot.
135
+ - A maintainer can dispatch an agent from a PR comment or label and see resolved prompt/context/tools/subagents before privileged work starts.
136
+ - Fork PR agent runs are forced onto untrusted runner pools, unprivileged ServiceAccounts, and cannot receive privileged secrets.
137
+ - A repair attempt can produce a patch artifact without automatically pushing it.
138
+ - A human approval can convert an approved patch artifact into a branch update, PR comment, review, or workflow rerun.
139
+ - Agent run detail streams transcript/events and links back to source PR, check, pipeline, job, workspace, session, and artifacts.
140
+ - Repeated CI failures are deduped by source object, commit SHA, failed job/step, failure signature, context digest, and rule.
@@ -0,0 +1,219 @@
1
+ # Agent context assembly and prompt safety spec
2
+
3
+ ## Purpose
4
+
5
+ Agent dispatch is only safe if Krate can explain exactly what context entered the prompt and launch options. This document defines how to assemble `AgentContextBundle` resources from repository pages, CI events, issues, PRs, artifacts, context labels, skills, tools, and user prompts.
6
+
7
+ It is grounded in the current Krate UI structure: repository routes already provide Code, Issues, Pull Requests, Runs, Hooks, and Settings surfaces, and `ui-shell.jsx` exposes YAML plan panels for advanced resource visibility.
8
+
9
+ ## Context assembly principles
10
+
11
+ - Context is a durable resource, not a transient UI string.
12
+ - Every prompt fragment must have provenance.
13
+ - Context labels are reviewed prompt fragments, not hidden commands.
14
+ - Secret values are never included in prompt, preview, transcript, artifact, or audit records.
15
+ - Redaction happens before the bundle is snapshotted.
16
+ - Retries should use the original context snapshot unless the user explicitly refreshes it.
17
+ - Prompt preview must show all included sources, labels, skills, and attachments before dispatch.
18
+
19
+ ## `AgentContextBundle` schema
20
+
21
+ Important fields:
22
+
23
+ ```yaml
24
+ spec:
25
+ dispatchRun: adr-01hx
26
+ sourceRefs:
27
+ repository: krate
28
+ pullRequest: krate/42
29
+ pipeline: pipeline-01hx
30
+ job: job-01hx-test
31
+ issue: krate/91
32
+ path: docs/agents
33
+ prompt:
34
+ user: string
35
+ renderedSystemDigest: sha256:...
36
+ renderedDeveloperDigest: sha256:...
37
+ renderedTaskDigest: sha256:...
38
+ contextLabels:
39
+ - name: ci-failure-summary
40
+ generation: 4
41
+ digest: sha256:...
42
+ sources:
43
+ - kind: repository-file
44
+ ref: refs/heads/staging
45
+ path: docs/agents/README.md
46
+ digest: sha256:...
47
+ - kind: pipeline-log
48
+ name: job-01hx-test
49
+ redactionStatus: redacted
50
+ attachments:
51
+ - kind: log-excerpt
52
+ artifactRef: artifact-01hx-log
53
+ digest: sha256:...
54
+ redactions:
55
+ - kind: secret-pattern
56
+ count: 3
57
+ replacement: "[REDACTED:secret]"
58
+ limits:
59
+ maxBytes: 750000
60
+ truncated: false
61
+ status:
62
+ phase: Ready
63
+ digest: sha256:...
64
+ conditions: []
65
+ ```
66
+
67
+ ## Context sources by route
68
+
69
+ | Route/surface | Default context | Optional context |
70
+ | --- | --- | --- |
71
+ | `/orgs/[org]/repositories/[repo]/code` | repo, branch/ref, selected path, file metadata, repository instructions | selected file contents, recent commits, workspace state |
72
+ | `/orgs/[org]/repositories/[repo]/issues` | issue title/body/labels/comments, context labels, linked workspace/session/runs | child issues, related PRs, artifacts |
73
+ | `/orgs/[org]/repositories/[repo]/pull-requests` | PR title/body, source/target branch, changed files, checks, review state | diff hunks, comments, previous agent artifacts |
74
+ | `/orgs/[org]/repositories/[repo]/runs` | pipeline/job status, failed step, runner pool, logs, artifacts | similar failures, rerun history, cache metadata |
75
+ | `/orgs/[org]/repositories/[repo]/hooks` | webhook delivery, headers metadata, event type, matched rule | replay history, dedupe/coalescing records |
76
+ | `/agents/rules` dry-run | sample event, matched rule, rendered prompt, permission review | fixture payloads, expected dedupe key |
77
+ | `/agents/runs/[run]` continuation | previous context digest, transcript summary, current artifacts | new user-provided context, selected files/logs |
78
+
79
+ ## Prompt layers
80
+
81
+ Krate should render prompt layers separately:
82
+
83
+ 1. stack system prompt;
84
+ 2. stack developer prompt;
85
+ 3. skill prompt fragments;
86
+ 4. context label prompt fragments;
87
+ 5. trigger prompt template;
88
+ 6. user task prompt;
89
+ 7. bounded source summaries and attachments.
90
+
91
+ Each layer gets a digest and provenance entry. The UI can show rendered text where safe, but large attachments should show summaries and digests.
92
+
93
+ ## Redaction policy
94
+
95
+ Redaction happens in this order:
96
+
97
+ 1. explicit Secret/ConfigMap deny list from permission review;
98
+ 2. known secret key names and token patterns;
99
+ 3. provider credentials and OAuth tokens;
100
+ 4. private keys and kubeconfigs;
101
+ 5. webhook signatures and auth headers;
102
+ 6. repository policy-defined patterns;
103
+ 7. user-specified redaction patterns.
104
+
105
+ Redaction output records counts and categories, not raw values.
106
+
107
+ ## Size and truncation policy
108
+
109
+ Default limits:
110
+
111
+ - prompt text: 64 KiB;
112
+ - log excerpts: 256 KiB per job;
113
+ - diff context: 256 KiB;
114
+ - total bundle: 750 KiB for standard dispatch;
115
+ - max attachment count: 32.
116
+
117
+ When truncation happens:
118
+
119
+ - mark `limits.truncated=true`;
120
+ - include what was omitted and why;
121
+ - prefer keeping first actionable error, changed-file summary, and source breadcrumbs;
122
+ - allow user to attach more context explicitly from the run detail page.
123
+
124
+ ## Context label safety
125
+
126
+ `AgentContextLabel` must include:
127
+
128
+ - reviewed prompt fragment;
129
+ - allowed source types;
130
+ - owner/reviewer metadata;
131
+ - generation and digest;
132
+ - allowed stacks or repositories;
133
+ - unsafe phrase/pattern validation status.
134
+
135
+ Labels cannot:
136
+
137
+ - reference Secret values;
138
+ - grant tools or permissions;
139
+ - change approval mode;
140
+ - override runner pool or ServiceAccount;
141
+ - hide prompt text from preview.
142
+
143
+ ## Context bundle lifecycle
144
+
145
+ 1. User or trigger proposes context.
146
+ 2. Context assembler resolves sources and permissions.
147
+ 3. Redactor removes sensitive values.
148
+ 4. Bundle digest is computed.
149
+ 5. Permission review digest is attached.
150
+ 6. Bundle is snapshotted before dispatch attempt creation.
151
+ 7. Retry uses the same bundle unless user selects refresh.
152
+ 8. Refreshed context creates a new digest and attempt reason.
153
+
154
+ ## UI requirements
155
+
156
+ The dispatch composer and run detail page must show:
157
+
158
+ - source refs and route origin;
159
+ - selected stack and prompt layers;
160
+ - context labels and generation/digest;
161
+ - attachments and truncation state;
162
+ - redaction summary;
163
+ - permission review summary;
164
+ - final context digest.
165
+
166
+ Denied or warning states:
167
+
168
+ - source unavailable;
169
+ - attachment too large;
170
+ - redaction failed;
171
+ - context label drifted;
172
+ - selected label not allowed for route/source;
173
+ - prompt template field missing;
174
+ - refreshed context differs from original run.
175
+
176
+ ## Controller responsibilities
177
+
178
+ Future file:
179
+
180
+ - `src/agent-context-bundles.js`
181
+
182
+ Responsibilities:
183
+
184
+ - collect sources from Krate resources;
185
+ - render prompt layers;
186
+ - redact sensitive values;
187
+ - produce digest and manifest;
188
+ - write `AgentContextBundle` metadata and object storage attachments;
189
+ - expose preview for UI without launching an agent.
190
+
191
+ ## Acceptance criteria
192
+
193
+ - A dispatch can be recreated from context bundle metadata without secret values.
194
+ - Prompt preview shows every injected context label and skill fragment.
195
+ - A missing or drifted context label blocks dispatch or retry according to policy.
196
+ - Large logs are truncated with visible explanation.
197
+ - Redaction failures fail closed.
198
+ - Context bundle digest appears on dispatch run, attempt, approval, artifacts, and audit events.
199
+
200
+ ## Company brain memory sources
201
+
202
+ `AgentContextBundle` must support company brain memory as a dedicated source family. Memory sources include Atlas-style graph YAML records, Markdown records with YAML frontmatter, free-form Markdown grep excerpts, ontology reports, and generated indexes tied to a Git commit.
203
+
204
+ Required memory fields inside a bundle:
205
+
206
+ ```yaml
207
+ memory:
208
+ repositoryRef: org-company-brain
209
+ requestedRef: main
210
+ resolvedCommit: abcdef1234567890
211
+ snapshotRef: memory-snapshot-01hx
212
+ queryManifestDigest: sha256:...
213
+ ontologyDigest: sha256:...
214
+ indexDigest: sha256:...
215
+ selectedRecordsDigest: sha256:...
216
+ selectedExcerptsDigest: sha256:...
217
+ ```
218
+
219
+ A retry uses the original memory snapshot unless the user explicitly refreshes memory. A dispatch may also specify `refAt` to run with memory from a prior timestamp; Krate resolves that timestamp to the latest approved commit at or before it and records both the historical commit and the current commit for diff warnings.
@@ -0,0 +1,255 @@
1
+ # Agent controller reconciliation spec
2
+
3
+ ## Purpose
4
+
5
+ This document defines the controller loops needed for agent orchestration. It is grounded in the current Krate implementation:
6
+
7
+ - `src/api-controller.js` is an HTTP/application facade and should not own long-running reconciliation loops.
8
+ - `src/kubernetes-controller.js` already models list/get/apply/delete/watch over Kubernetes-style resources.
9
+ - `src/controller-ui.js` converts controller snapshots into UI-friendly view models.
10
+ - `apps/web/app/api/watch/[[...resource]]/route.js` streams org-scoped Krate live events as SSE.
11
+
12
+ Agent implementation should add focused controllers and keep UI/API handlers thin.
13
+
14
+ ## Controller architecture
15
+
16
+ | Controller | Watches | Writes | Purpose |
17
+ | --- | --- | --- | --- |
18
+ | `agent-stack-controller` | stack/tool/MCP/skill/subagent/context/rbac/grant resources | stack status, capability requirements | Validate stack readiness. |
19
+ | `agent-rbac-controller` | users, teams, RepositoryPermission, AgentServiceAccount, AgentRoleBinding, native RBAC | native ServiceAccounts/Roles/RoleBindings, status | Sync Kubernetes identity/RBAC intent. |
20
+ | `agent-secret-config-controller` | Secret, ConfigMap, grants, capabilities | grant status, requirement status | Validate secret/config access and drift. |
21
+ | `agent-trigger-controller` | WebhookDelivery, Pipeline, Job, Issue, PullRequest, AgentTriggerRule | AgentTriggerExecution, AgentDispatchRun | Match events and create runs. |
22
+ | `agent-dispatch-controller` | AgentDispatchRun, AgentDispatchAttempt | attempts, sessions, artifacts, approvals, status | Launch/reconcile Agent Mux runs. |
23
+ | `agent-workspace-controller` | AgentWorkspacePolicy, AgentWorkspace, work-item links | workspace status, links | Manage worktrees/runtime state. |
24
+ | `agent-approval-controller` | AgentApproval, artifacts, write-back requests | approval status, repository/PR/check writes | Gate privileged actions. |
25
+ | `agent-ui-projection-controller` | all agent resources | controller-ui model additions | Build efficient route view models. |
26
+
27
+ ## Shared reconciliation rules
28
+
29
+ - Reconcile by desired resource state and observed external state, not by UI events.
30
+ - Use `metadata.generation` and `status.observedGeneration` to avoid stale status updates.
31
+ - Every external side effect needs an idempotency key.
32
+ - Controllers must tolerate restart, duplicate events, partial Agent Mux outages, and Kubernetes watch reconnects.
33
+ - Conditions should explain every blocked UI action.
34
+ - Secret values must never be copied into status, audit events, logs, or prompt previews.
35
+
36
+ ## Idempotency keys
37
+
38
+ | Side effect | Key |
39
+ | --- | --- |
40
+ | Trigger execution | source event UID + rule generation + dedupe key |
41
+ | Dispatch run creation | trigger execution UID or manual dispatch request UID |
42
+ | Attempt creation | dispatch run UID + attempt number + reason |
43
+ | Agent Mux launch | attempt UID + stack snapshot digest + context digest |
44
+ | Workspace provision | workspace policy + repo + ref + work item + attempt UID |
45
+ | Approval request | attempt UID + action type + target + artifact digest |
46
+ | Write-back | approval UID + artifact digest + target object |
47
+ | Native RBAC sync | AgentRoleBinding UID + roleRef + subject + scope |
48
+ | Secret/config grant review | grant UID + target metadata version + subject |
49
+
50
+ ## `agent-stack-controller`
51
+
52
+ Inputs:
53
+
54
+ - `AgentStack`, `AgentToolProfile`, `AgentMcpServer`, `AgentSkill`, `AgentSubagent`, `AgentContextLabel`.
55
+ - `AgentServiceAccount`, `AgentRoleBinding`, `AgentSecretGrant`, `AgentConfigGrant`.
56
+ - Agent Mux capability manifests.
57
+
58
+ Reconcile steps:
59
+
60
+ 1. Load stack and referenced config resources.
61
+ 2. Query Agent Mux capabilities for adapter/model/session/tool support.
62
+ 3. Compute `AgentCapabilityRequirement` for tools, MCP, skills, subagents, model provider, and runtime.
63
+ 4. Call permission review for ServiceAccount/RBAC/Secret/Config access.
64
+ 5. Probe MCP health where required.
65
+ 6. Set readiness conditions and warnings.
66
+ 7. Emit UI projection hints for stack builder.
67
+
68
+ Outputs:
69
+
70
+ - `AgentStack.status.conditions`.
71
+ - `AgentCapabilityRequirement` records.
72
+ - Audit events for readiness transitions when they affect dispatch admission.
73
+
74
+ ## `agent-rbac-controller`
75
+
76
+ Inputs:
77
+
78
+ - `User`, `Team`, `IdentityMapping`, `RepositoryPermission`.
79
+ - `AgentServiceAccount`, `AgentRoleBinding`.
80
+ - Native Kubernetes `ServiceAccount`, `Role`, `ClusterRole`, `RoleBinding`, `ClusterRoleBinding`.
81
+
82
+ Reconcile steps:
83
+
84
+ 1. Resolve Krate subject into Kubernetes user/group/ServiceAccount.
85
+ 2. Determine whether Krate owns or imports the native object.
86
+ 3. Run bind/escalate checks for requested role changes.
87
+ 4. Apply or update owned native RBAC objects.
88
+ 5. Detect drift and set conditions.
89
+ 6. Notify stack controller when dependent identity or roles changed.
90
+
91
+ Failure handling:
92
+
93
+ - Missing subject: `SubjectsResolved=False`.
94
+ - Escalation denied: `EscalationAdmitted=False` and no native apply.
95
+ - Drift on owned privileged role: block dependent dispatches until resolved.
96
+
97
+ ## `agent-secret-config-controller`
98
+
99
+ Inputs:
100
+
101
+ - Native `Secret` and `ConfigMap` metadata.
102
+ - `AgentSecretGrant`, `AgentConfigGrant`.
103
+ - `AgentCapabilityRequirement`.
104
+ - `AgentDispatchAttempt` for active snapshots.
105
+
106
+ Reconcile steps:
107
+
108
+ 1. Check target Secret/ConfigMap existence and requested key names.
109
+ 2. Check native RBAC visibility for metadata and apply policy for sensitive ConfigMap keys.
110
+ 3. Match grants to capability requirements.
111
+ 4. Mark stale grants when target metadata version changes.
112
+ 5. Update affected stack readiness and active dispatch warnings.
113
+
114
+ Failure handling:
115
+
116
+ - Secret value unavailable to controller: acceptable; values are not required for metadata validation.
117
+ - Secret missing: block new dispatch and mark active snapshots stale.
118
+ - Key removed: block retry/resume and show affected consumers.
119
+
120
+ ## `agent-trigger-controller`
121
+
122
+ Inputs:
123
+
124
+ - `WebhookDelivery`, `Pipeline`, `Job`, `Issue`, `PullRequest`, labels, comments, schedules, manual dispatch requests.
125
+ - `AgentTriggerRule`.
126
+
127
+ Reconcile steps:
128
+
129
+ 1. Normalize event into a trigger payload.
130
+ 2. Persist `AgentTriggerExecution` before dispatch.
131
+ 3. Evaluate lifecycle, matcher, actor, repository/ref trust, dedupe, and concurrency.
132
+ 4. Build context bundle plan and run permission review.
133
+ 5. Create `AgentDispatchRun` when admitted.
134
+ 6. Mark execution as created, coalesced, rejected, or waiting for approval.
135
+
136
+ Outputs:
137
+
138
+ - `AgentTriggerExecution`.
139
+ - `AgentDispatchRun` and initial source breadcrumbs.
140
+
141
+ ## `agent-dispatch-controller`
142
+
143
+ Inputs:
144
+
145
+ - `AgentDispatchRun`, `AgentDispatchAttempt`, `AgentContextBundle`, permission snapshots.
146
+ - Agent Mux gateway/client.
147
+
148
+ Reconcile steps:
149
+
150
+ 1. Create initial or retry attempt.
151
+ 2. Materialize immutable stack/context/permission snapshots.
152
+ 3. Select runner/external gateway and workspace.
153
+ 4. Launch Agent Mux run/session with admitted tools, secrets, configs, and runtime identity references.
154
+ 5. Persist Agent Mux run/session IDs.
155
+ 6. Reconcile event stream into status, artifact, approval, cost, and subagent records.
156
+ 7. Transition run to terminal state or waiting state.
157
+
158
+ Failure handling:
159
+
160
+ - Agent Mux unavailable: retry with backoff while attempt remains queued/starting.
161
+ - Session binding pending: set `AgentMuxSessionBound=False` with pending reason.
162
+ - Adapter rejects launch options: fail attempt and keep permission snapshot for diagnosis.
163
+
164
+ ## `agent-workspace-controller`
165
+
166
+ Inputs:
167
+
168
+ - `AgentWorkspacePolicy`, `AgentWorkspace`, `WorkItemWorkspaceLink`, dispatch attempts.
169
+
170
+ Reconcile steps:
171
+
172
+ 1. Decide workspace mode from policy and trust tier.
173
+ 2. Provision/link worktree when needed.
174
+ 3. Record branch, head, dirty state, ahead/behind, runtime URLs, and missing path state.
175
+ 4. Handle pin, archive, cleanup, recover, notes, and rebase actions.
176
+ 5. Link workspace to issue/PR/run/session.
177
+
178
+ ## `agent-approval-controller`
179
+
180
+ Inputs:
181
+
182
+ - `AgentApproval`, write-back requests, artifacts, dispatch attempts.
183
+
184
+ Reconcile steps:
185
+
186
+ 1. Validate approver identity and native RBAC.
187
+ 2. Validate approval still matches current artifact digest and target object.
188
+ 3. Apply approved action idempotently.
189
+ 4. Write audit event and update approval/run status.
190
+
191
+ ## UI projection integration
192
+
193
+ `src/controller-ui.js` should add an `agents` view model with:
194
+
195
+ - stack readiness counters;
196
+ - active dispatches and pending approvals;
197
+ - missing permission warnings;
198
+ - repository-scoped agent affordances for code/issues/PRs/runs/settings;
199
+ - watch resource names for `LiveWatchPanel`.
200
+
201
+ The existing app can initially expose agent resources through advanced resource tables, then add typed pages once controllers exist.
202
+
203
+ ## Memory controller responsibilities
204
+
205
+ The memory controller reconciles the company brain control plane:
206
+
207
+ - watches `AgentMemoryRepository`, validates reachability, default branch, layout, and index freshness;
208
+ - watches `AgentMemorySource`, computes allowed path/kind scopes, and projects missing grants into `AgentCapabilityRequirement`;
209
+ - resolves current, explicit, snapshot-tag, and `refAt` memory refs for dispatch admission;
210
+ - creates `AgentMemorySnapshot` and `AgentMemoryQuery` records during context assembly;
211
+ - validates `AgentMemoryUpdate` patches, opens review branches/PRs when allowed, and records approval/merge status;
212
+ - updates `AgentMemoryOntology` status from parser, graph, frontmatter, edge, owner, and secret-scan validators;
213
+ - preserves historical snapshots even when memory sources or repositories are later disabled.
214
+
215
+ ## Org-scoped controller reconciliation
216
+
217
+ Every controller reconciliation starts by resolving `organizationRef` and namespace. Controllers may cache cluster-wide watches, but each side effect must be namespaced to the owning org. Agent, memory, runner, trigger, deployment, repository, and secret/config controllers reject cross-org references unless an explicit sharing policy exists.
218
+
219
+ The memory controller also reconciles `AgentRunMemoryImport` records by reading admitted `.a5c` run/session/journal metadata, redacting it, normalizing it into the org memory repository, and linking imported records to the source `AgentDispatchRun` or Babysitter run ID.
220
+
221
+ ## Org admission and memory import reconciler pseudocode
222
+
223
+ Org admission should run as a shared preflight:
224
+
225
+ ```text
226
+ resolveOrg(resource)
227
+ assertNamespaceMatchesOrg(resource.namespace, org.namespace)
228
+ assertOrgLabels(resource.metadata.labels, org)
229
+ for ref in resource.spec.refs:
230
+ assertSameOrg(ref, org) or assertSharingPolicy(ref, org)
231
+ assertKubernetesRbac(actor, org.namespace, verb, resource)
232
+ assertKratePermission(actor, org, action)
233
+ emitAuditPreflight(actor, org, resource, action)
234
+ ```
235
+
236
+ `AgentRunMemoryImport` reconciliation:
237
+
238
+ ```text
239
+ resolve import org and source run
240
+ verify source repository/session/run belongs to org
241
+ collect admitted MEMORY.md, session, journal, task, artifact metadata
242
+ compute source digests
243
+ redact secrets and unsafe prompt instructions
244
+ normalize to Markdown/YAML memory files
245
+ validate ontology/frontmatter/edges/owners
246
+ open or update memory repo PR
247
+ wait for approval/merge
248
+ record resulting memory commit and indexes
249
+ ```
250
+
251
+ The reconciler must be idempotent by source digest, target path, and import generation.
252
+
253
+ ## Sequence spec reference
254
+
255
+ The detailed org-memory sequences are defined in [Org memory controller sequence spec](./org-memory-controller-sequence-spec.md). Controller implementation should keep that document as the source of truth for ordering, idempotency keys, status conditions, and cross-org denial behavior.