@a5c-ai/krate 5.0.1-staging.3a341c33c

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 (178) hide show
  1. package/Dockerfile +31 -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 +2455 -0
  6. package/dist/krate-lifecycle.json +201 -0
  7. package/dist/krate-runtime-snapshot.json +2955 -0
  8. package/dist/krate-summary.json +722 -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-memory-controller.js +374 -0
  140. package/src/agent-mux-client.js +280 -0
  141. package/src/agent-permission-review.js +162 -0
  142. package/src/agent-stack-controller.js +296 -0
  143. package/src/agent-trigger-controller.js +108 -0
  144. package/src/agent-workspace-controller.js +208 -0
  145. package/src/api-controller.js +248 -0
  146. package/src/argocd-gitops.js +43 -0
  147. package/src/auth.js +265 -0
  148. package/src/component-catalog.js +41 -0
  149. package/src/control-plane.js +136 -0
  150. package/src/controller-client.js +38 -0
  151. package/src/controller-ui.js +551 -0
  152. package/src/data-plane.js +178 -0
  153. package/src/gitea-backend.js +95 -0
  154. package/src/handoff.js +98 -0
  155. package/src/hooks-events.js +63 -0
  156. package/src/http-server.js +151 -0
  157. package/src/identity-policy.js +86 -0
  158. package/src/index.js +32 -0
  159. package/src/kubernetes-controller.js +812 -0
  160. package/src/kubernetes-resource-gateway.js +48 -0
  161. package/src/operations.js +112 -0
  162. package/src/resource-model.js +211 -0
  163. package/src/runners-ci.js +48 -0
  164. package/src/runtime.js +196 -0
  165. package/src/web-ui.js +40 -0
  166. package/tests/agent-approval-controller.test.js +173 -0
  167. package/tests/agent-context-bundles.test.js +278 -0
  168. package/tests/agent-dispatch-controller.test.js +176 -0
  169. package/tests/agent-memory-controller.test.js +308 -0
  170. package/tests/agent-mux-client.test.js +204 -0
  171. package/tests/agent-permission-review.test.js +209 -0
  172. package/tests/agent-resources.test.js +228 -0
  173. package/tests/agent-stack-controller.test.js +221 -0
  174. package/tests/agent-trigger-controller.test.js +211 -0
  175. package/tests/agent-workspace-controller.test.js +215 -0
  176. package/tests/deployment.test.js +393 -0
  177. package/tests/e2e/lifecycle.test.js +117 -0
  178. package/tests/krate.test.js +727 -0
@@ -0,0 +1,242 @@
1
+ import { createHash } from 'node:crypto';
2
+ import { createResource, clone } from './resource-model.js';
3
+
4
+ export const AGENT_CONTEXT_BUNDLES_BOUNDARY = {
5
+ role: 'agent-context-bundles',
6
+ scope: 'Context assembly, redaction, and digest computation for agent dispatch',
7
+ owns: ['prompt layer collection', 'redaction patterns', 'size enforcement', 'digest computation'],
8
+ delegatesTo: ['resource-model'],
9
+ mustNotOwn: ['secret values', 'runtime execution', 'Agent Mux sessions']
10
+ };
11
+
12
+ const PROMPT_LAYER_MAX = 64 * 1024; // 64 KiB
13
+ const BUNDLE_MAX = 750 * 1024; // 750 KiB
14
+ const MAX_ATTACHMENTS = 32;
15
+
16
+ // Redaction patterns (ordered by priority)
17
+ const REDACTION_PATTERNS = [
18
+ { kind: 'secret-key', pattern: /(?:API_KEY|API_SECRET|SECRET_KEY|ACCESS_KEY|PRIVATE_KEY|AUTH_TOKEN|PASSWORD|PASSWD|CREDENTIALS?)\s*[=:]\s*['"]?([^\s'"}{,\]]+)/gi },
19
+ { kind: 'provider-token', pattern: /\b(sk-[a-zA-Z0-9]{20,}|ghp_[a-zA-Z0-9]{36,}|gho_[a-zA-Z0-9]{36,}|glpat-[a-zA-Z0-9\-_]{20,}|xoxb-[a-zA-Z0-9\-]+|xoxp-[a-zA-Z0-9\-]+)\b/g },
20
+ { kind: 'bearer-token', pattern: /Bearer\s+[a-zA-Z0-9\-._~+\/]+=*/gi },
21
+ { kind: 'private-key', pattern: /-----BEGIN\s+(?:RSA\s+|EC\s+|DSA\s+|OPENSSH\s+)?PRIVATE\s+KEY-----[\s\S]*?-----END\s+(?:RSA\s+|EC\s+|DSA\s+|OPENSSH\s+)?PRIVATE\s+KEY-----/g },
22
+ { kind: 'base64-credential', pattern: /\b[A-Za-z0-9+\/]{40,}={0,2}\b/g },
23
+ ];
24
+
25
+ function sha256(data) {
26
+ return createHash('sha256').update(data).digest('hex');
27
+ }
28
+
29
+ function truncateToLimit(text, limit) {
30
+ if (typeof text !== 'string') return '';
31
+ if (text.length <= limit) return text;
32
+ return text.slice(0, limit);
33
+ }
34
+
35
+ function applyRedactions(text) {
36
+ if (typeof text !== 'string' || text.length === 0) return { text, counts: {} };
37
+ const counts = {};
38
+ let redacted = text;
39
+ for (const { kind, pattern } of REDACTION_PATTERNS) {
40
+ const fresh = new RegExp(pattern.source, pattern.flags);
41
+ const before = redacted;
42
+ redacted = redacted.replace(fresh, (match) => {
43
+ counts[kind] = (counts[kind] || 0) + 1;
44
+ return `[REDACTED:${kind}]`;
45
+ });
46
+ }
47
+ return { text: redacted, counts };
48
+ }
49
+
50
+ function mergeRedactionCounts(target, source) {
51
+ for (const [kind, count] of Object.entries(source)) {
52
+ target[kind] = (target[kind] || 0) + count;
53
+ }
54
+ }
55
+
56
+ export function createRedactionManifest(redactionCounts) {
57
+ const total = Object.values(redactionCounts).reduce((sum, n) => sum + n, 0);
58
+ return { total, byKind: clone(redactionCounts) };
59
+ }
60
+
61
+ export function assembleContextBundle({ stack, repository, ref, sourceRefs = [], contextLabels = [], redactionPolicy, resources = {} }) {
62
+ const namespace = stack?.metadata?.namespace || 'default';
63
+ const organizationRef = stack?.spec?.organizationRef || 'default';
64
+ const allRedactionCounts = {};
65
+
66
+ // Step 1 — Collect prompt layers
67
+ const rawSystem = stack?.spec?.prompt?.system || '';
68
+ const rawDeveloper = stack?.spec?.prompt?.developer || '';
69
+ const rawTask = stack?.spec?.prompt?.task || '';
70
+
71
+ // Collect skill fragments
72
+ const skillFragments = [];
73
+ const skillRefs = stack?.spec?.skillRefs || [];
74
+ const agentSkills = resources.AgentSkill || [];
75
+ for (const skillRef of skillRefs) {
76
+ const skill = agentSkills.find((s) => s.metadata?.name === skillRef);
77
+ if (skill?.spec?.promptFragment) {
78
+ skillFragments.push({ name: skillRef, content: skill.spec.promptFragment });
79
+ }
80
+ }
81
+
82
+ // Collect label fragments
83
+ const labelFragments = [];
84
+ const agentContextLabels = resources.AgentContextLabel || [];
85
+ for (const labelRef of contextLabels) {
86
+ const label = agentContextLabels.find((l) => l.metadata?.name === labelRef);
87
+ if (label?.spec?.promptFragment) {
88
+ labelFragments.push({ name: labelRef, content: label.spec.promptFragment });
89
+ }
90
+ }
91
+
92
+ // Step 2 — Truncate each layer and compute digests
93
+ const system = truncateToLimit(rawSystem, PROMPT_LAYER_MAX);
94
+ const developer = truncateToLimit(rawDeveloper, PROMPT_LAYER_MAX);
95
+ const task = truncateToLimit(rawTask, PROMPT_LAYER_MAX);
96
+
97
+ // Apply redaction to prompt layers
98
+ const systemRedacted = applyRedactions(system);
99
+ mergeRedactionCounts(allRedactionCounts, systemRedacted.counts);
100
+ const developerRedacted = applyRedactions(developer);
101
+ mergeRedactionCounts(allRedactionCounts, developerRedacted.counts);
102
+ const taskRedacted = applyRedactions(task);
103
+ mergeRedactionCounts(allRedactionCounts, taskRedacted.counts);
104
+
105
+ const systemDigest = sha256(systemRedacted.text);
106
+ const developerDigest = sha256(developerRedacted.text);
107
+ const taskDigest = sha256(taskRedacted.text);
108
+
109
+ const skillLayerDigests = [];
110
+ const redactedSkillFragments = [];
111
+ for (const frag of skillFragments) {
112
+ const truncated = truncateToLimit(frag.content, PROMPT_LAYER_MAX);
113
+ const redacted = applyRedactions(truncated);
114
+ mergeRedactionCounts(allRedactionCounts, redacted.counts);
115
+ const digest = sha256(redacted.text);
116
+ skillLayerDigests.push({ role: `skill:${frag.name}`, digest, sizeBytes: redacted.text.length });
117
+ redactedSkillFragments.push({ name: frag.name, content: redacted.text });
118
+ }
119
+
120
+ const labelLayerDigests = [];
121
+ const redactedLabelFragments = [];
122
+ for (const frag of labelFragments) {
123
+ const truncated = truncateToLimit(frag.content, PROMPT_LAYER_MAX);
124
+ const redacted = applyRedactions(truncated);
125
+ mergeRedactionCounts(allRedactionCounts, redacted.counts);
126
+ const digest = sha256(redacted.text);
127
+ labelLayerDigests.push({ role: `label:${frag.name}`, digest, sizeBytes: redacted.text.length });
128
+ redactedLabelFragments.push({ name: frag.name, content: redacted.text });
129
+ }
130
+
131
+ // Step 3 — Collect sources from sourceRefs
132
+ const sources = [];
133
+ const limitedSourceRefs = sourceRefs.slice(0, MAX_ATTACHMENTS);
134
+ for (const srcRef of limitedSourceRefs) {
135
+ const content = truncateToLimit(srcRef.content || '', PROMPT_LAYER_MAX);
136
+ const redacted = applyRedactions(content);
137
+ mergeRedactionCounts(allRedactionCounts, redacted.counts);
138
+ sources.push({
139
+ kind: srcRef.kind || 'unknown',
140
+ ref: srcRef.ref || '',
141
+ content: redacted.text,
142
+ digest: sha256(redacted.text)
143
+ });
144
+ }
145
+
146
+ // Step 5 — Enforce total size
147
+ let wasTruncated = false;
148
+ const TRUNC_MARKER = '[...truncated at 750KiB limit]';
149
+ const TRUNC_MARKER_LEN = TRUNC_MARKER.length;
150
+
151
+ const computeTotalSize = () =>
152
+ systemRedacted.text.length + developerRedacted.text.length + taskRedacted.text.length +
153
+ redactedSkillFragments.reduce((s, f) => s + f.content.length, 0) +
154
+ redactedLabelFragments.reduce((s, f) => s + f.content.length, 0) +
155
+ sources.reduce((s, src) => s + src.content.length, 0);
156
+
157
+ let totalSize = computeTotalSize();
158
+
159
+ if (totalSize > BUNDLE_MAX) {
160
+ wasTruncated = true;
161
+ // Build a list of truncatable items (sources first, then labels, then skills)
162
+ // sorted by descending size so we cut the largest first
163
+ const truncatableItems = [
164
+ ...sources.map((s, i) => ({ type: 'source', index: i })),
165
+ ...redactedLabelFragments.map((f, i) => ({ type: 'label', index: i })),
166
+ ...redactedSkillFragments.map((f, i) => ({ type: 'skill', index: i }))
167
+ ];
168
+
169
+ const getContent = (t) => {
170
+ if (t.type === 'source') return sources[t.index].content;
171
+ if (t.type === 'label') return redactedLabelFragments[t.index].content;
172
+ return redactedSkillFragments[t.index].content;
173
+ };
174
+
175
+ truncatableItems.sort((a, b) => getContent(b).length - getContent(a).length);
176
+
177
+ for (const target of truncatableItems) {
178
+ totalSize = computeTotalSize();
179
+ if (totalSize <= BUNDLE_MAX) break;
180
+
181
+ const currentContent = getContent(target);
182
+ const excess = totalSize - BUNDLE_MAX;
183
+ // We need to remove at least `excess` bytes, but also account for the marker we add
184
+ const cutAmount = excess + TRUNC_MARKER_LEN;
185
+ const newLen = Math.max(0, currentContent.length - cutAmount);
186
+ const truncatedContent = currentContent.slice(0, newLen) + TRUNC_MARKER;
187
+
188
+ if (target.type === 'source') {
189
+ sources[target.index].content = truncatedContent;
190
+ sources[target.index].digest = sha256(truncatedContent);
191
+ } else if (target.type === 'label') {
192
+ redactedLabelFragments[target.index].content = truncatedContent;
193
+ labelLayerDigests[target.index].digest = sha256(truncatedContent);
194
+ labelLayerDigests[target.index].sizeBytes = truncatedContent.length;
195
+ } else if (target.type === 'skill') {
196
+ redactedSkillFragments[target.index].content = truncatedContent;
197
+ skillLayerDigests[target.index].digest = sha256(truncatedContent);
198
+ skillLayerDigests[target.index].sizeBytes = truncatedContent.length;
199
+ }
200
+ }
201
+ }
202
+
203
+ // Step 6 — Compute bundle digest
204
+ const promptLayers = [
205
+ { role: 'system', digest: systemDigest, sizeBytes: systemRedacted.text.length },
206
+ { role: 'developer', digest: developerDigest, sizeBytes: developerRedacted.text.length },
207
+ { role: 'task', digest: taskDigest, sizeBytes: taskRedacted.text.length },
208
+ ...skillLayerDigests,
209
+ ...labelLayerDigests,
210
+ ];
211
+
212
+ const digestPayload = JSON.stringify({
213
+ promptLayers,
214
+ sources: sources.map((s) => ({ kind: s.kind, ref: s.ref, digest: s.digest }))
215
+ });
216
+ const digest = sha256(digestPayload);
217
+
218
+ const redactionManifest = createRedactionManifest(allRedactionCounts);
219
+
220
+ // Step 7 — Build the resource
221
+ const resource = createResource('AgentContextBundle', { name: `bundle-${digest.slice(0, 12)}`, namespace }, {
222
+ organizationRef,
223
+ dispatchRun: '',
224
+ digest,
225
+ sources: limitedSourceRefs.map((s) => ({ kind: s.kind || 'unknown', ref: s.ref || '' })),
226
+ promptLayers,
227
+ redactions: redactionManifest,
228
+ limits: { maxBytes: BUNDLE_MAX, truncated: wasTruncated },
229
+ });
230
+
231
+ // Store actual text content in _content (in-memory only, not part of resource spec)
232
+ resource._content = {
233
+ system: systemRedacted.text,
234
+ developer: developerRedacted.text,
235
+ task: taskRedacted.text,
236
+ skillFragments: redactedSkillFragments,
237
+ labelFragments: redactedLabelFragments,
238
+ sources
239
+ };
240
+
241
+ return resource;
242
+ }
@@ -0,0 +1,86 @@
1
+ import { createPermissionReviewer } from './agent-permission-review.js';
2
+ import { createAgentStackController } from './agent-stack-controller.js';
3
+ import { assembleContextBundle } from './agent-context-bundles.js';
4
+ import { createResource, clone } from './resource-model.js';
5
+ import { createAgentMuxClient } from './agent-mux-client.js';
6
+
7
+ export const AGENT_DISPATCH_CONTROLLER_BOUNDARY = {
8
+ role: 'agent-dispatch-controller',
9
+ scope: 'Manual dispatch orchestration with permission gating and context assembly',
10
+ owns: ['dispatch creation', 'attempt lifecycle', 'Agent Mux session binding'],
11
+ delegatesTo: ['agent-permission-review', 'agent-stack-controller', 'agent-context-bundles', 'agent-mux-client'],
12
+ mustNotOwn: ['secret values', 'UI rendering']
13
+ };
14
+
15
+ export function createAgentDispatchController(options = {}) {
16
+ const permissionReviewer = options.permissionReviewer || createPermissionReviewer();
17
+ const stackController = options.stackController || createAgentStackController();
18
+ const agentMuxClient = options.agentMuxClient || createAgentMuxClient();
19
+
20
+ return {
21
+ role: 'agent-dispatch-controller',
22
+
23
+ async createManualDispatch({ repository, ref, sourceRefs = [], agentStack, taskKind, actor, namespace = 'default', organizationRef = 'default', resources = {} }) {
24
+ // 1. Find stack
25
+ const stack = (resources.AgentStack || []).find(s => s.metadata?.name === agentStack);
26
+ if (!stack) return { error: true, reason: 'stack-not-found', message: `AgentStack '${agentStack}' not found` };
27
+
28
+ // 2. Permission review
29
+ const review = permissionReviewer.reviewPermissions({ repository, ref, actor, agentStack, resources });
30
+ if (review.decision === 'denied') {
31
+ return { error: true, reason: 'permission-denied', message: 'Dispatch denied by permission review', review };
32
+ }
33
+ const permissionSnapshot = permissionReviewer.createPermissionSnapshot(review);
34
+
35
+ // 3. Assemble context bundle
36
+ const contextBundle = assembleContextBundle({ stack, repository, ref, sourceRefs, contextLabels: [], resources });
37
+
38
+ // 4. Create resources
39
+ const now = new Date().toISOString();
40
+ const runName = `dispatch-${Date.now()}`;
41
+
42
+ const run = createResource('AgentDispatchRun', { name: runName, namespace }, {
43
+ organizationRef,
44
+ repository,
45
+ sourceRefs: clone(sourceRefs),
46
+ agentStack,
47
+ taskKind: taskKind || 'diagnostic',
48
+ contextBundleRef: contextBundle.metadata.name,
49
+ });
50
+ run.status = { phase: 'Pending', queuedAt: now };
51
+
52
+ const attempt = createResource('AgentDispatchAttempt', { name: `${runName}-attempt-1`, namespace }, {
53
+ organizationRef,
54
+ agentDispatchRun: runName,
55
+ attemptReason: 'initial',
56
+ agentStackSnapshot: clone(stack.spec),
57
+ contextBundleDigest: contextBundle.spec.digest,
58
+ });
59
+ attempt.status = { permissionSnapshot, queueEnteredAt: now };
60
+
61
+ // 5. Try Agent Mux launch
62
+ if (agentMuxClient.isAvailable()) {
63
+ try {
64
+ const session = await agentMuxClient.launchSession({ stack, contextBundle, permissionSnapshot });
65
+ if (session && session.runId) {
66
+ attempt.status.agentMuxRunId = session.runId;
67
+ attempt.status.agentMuxSessionId = session.sessionId;
68
+ run.status.phase = 'Running';
69
+ attempt.status.startedAt = now;
70
+ } else {
71
+ run.status.phase = 'Queued';
72
+ run.status.conditions = [{ type: 'AgentMuxBound', status: 'False', reason: 'LaunchFailed', message: 'Agent Mux launch returned no session' }];
73
+ }
74
+ } catch {
75
+ run.status.phase = 'Queued';
76
+ run.status.conditions = [{ type: 'AgentMuxBound', status: 'False', reason: 'LaunchFailed' }];
77
+ }
78
+ } else {
79
+ run.status.phase = 'Queued';
80
+ run.status.conditions = [{ type: 'AgentMuxBound', status: 'False', reason: 'Unavailable', message: 'Agent Mux gateway not configured' }];
81
+ }
82
+
83
+ return { error: false, run, attempt, contextBundle, permissionSnapshot };
84
+ }
85
+ };
86
+ }