@a5c-ai/krate 5.0.1-staging.04a3db697

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 (246) 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 +3067 -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/gaps-agent-mux-to-krate-crds.md +298 -0
  27. package/docs/agents/glossary.md +66 -0
  28. package/docs/agents/implementation-blueprint.md +324 -0
  29. package/docs/agents/implementation-rollout-slices.md +251 -0
  30. package/docs/agents/memory-context-integration-spec.md +194 -0
  31. package/docs/agents/memory-ontology-schema-spec.md +253 -0
  32. package/docs/agents/memory-operations-runbook.md +121 -0
  33. package/docs/agents/mvp-vertical-slice-spec.md +146 -0
  34. package/docs/agents/observability-audit-spec.md +265 -0
  35. package/docs/agents/operator-runbook.md +174 -0
  36. package/docs/agents/org-memory-api-payload-examples.md +333 -0
  37. package/docs/agents/org-memory-controller-sequence-spec.md +181 -0
  38. package/docs/agents/org-memory-e2e-fixture-plan.md +161 -0
  39. package/docs/agents/org-memory-ui-implementation-map.md +114 -0
  40. package/docs/agents/org-memory-vertical-slice-spec.md +168 -0
  41. package/docs/agents/org-resource-model-delta-spec.md +111 -0
  42. package/docs/agents/org-route-resource-model-spec.md +183 -0
  43. package/docs/agents/org-scoping-namespace-spec.md +114 -0
  44. package/docs/agents/rbac-secrets-management-spec.md +406 -0
  45. package/docs/agents/repository-page-integration-spec.md +255 -0
  46. package/docs/agents/resource-contract-examples.md +808 -0
  47. package/docs/agents/resource-relationship-map.md +190 -0
  48. package/docs/agents/security-threat-model.md +188 -0
  49. package/docs/agents/shared-memory-company-brain-spec.md +358 -0
  50. package/docs/agents/storage-migration-spec.md +168 -0
  51. package/docs/agents/subagent-orchestration-spec.md +152 -0
  52. package/docs/agents/system-overview.md +88 -0
  53. package/docs/agents/tools-mcp-skills-spec.md +189 -0
  54. package/docs/agents/traceability-matrix.md +79 -0
  55. package/docs/agents/ui-flow-spec.md +211 -0
  56. package/docs/agents/ui-ux-system-spec.md +426 -0
  57. package/docs/agents/workspace-lifecycle-spec.md +166 -0
  58. package/docs/architecture-spec.md +78 -0
  59. package/docs/components/control-plane.md +78 -0
  60. package/docs/components/data-plane.md +69 -0
  61. package/docs/components/hooks-events.md +67 -0
  62. package/docs/components/identity-rbac-policy.md +73 -0
  63. package/docs/components/kubevela-oam.md +70 -0
  64. package/docs/components/operations-publishing.md +81 -0
  65. package/docs/components/runners-ci.md +66 -0
  66. package/docs/components/web-ui.md +94 -0
  67. package/docs/external/README.md +47 -0
  68. package/docs/external/bidirectional-sync-design.md +134 -0
  69. package/docs/external/cicd-interface.md +64 -0
  70. package/docs/external/external-backend-controllers.md +170 -0
  71. package/docs/external/external-backend-crds.md +234 -0
  72. package/docs/external/external-backend-ui-spec.md +151 -0
  73. package/docs/external/external-backend-ux-flows.md +115 -0
  74. package/docs/external/external-object-mapping.md +125 -0
  75. package/docs/external/git-forge-interface.md +68 -0
  76. package/docs/external/github-integration-design.md +151 -0
  77. package/docs/external/issue-tracking-interface.md +66 -0
  78. package/docs/external/provider-capability-manifests.md +204 -0
  79. package/docs/external/provider-catalog.md +139 -0
  80. package/docs/external/provider-rollout-testing.md +78 -0
  81. package/docs/external/research-results.md +48 -0
  82. package/docs/external/security-auth-permissions.md +81 -0
  83. package/docs/external/sync-state-machines.md +108 -0
  84. package/docs/external/unified-external-backend-model.md +107 -0
  85. package/docs/external/user-facing-changes.md +67 -0
  86. package/docs/gaps.md +161 -0
  87. package/docs/install.md +94 -0
  88. package/docs/krate-design.md +334 -0
  89. package/docs/local-minikube.md +55 -0
  90. package/docs/ontology/README.md +32 -0
  91. package/docs/ontology/bounded-contexts.md +29 -0
  92. package/docs/ontology/events-and-hooks.md +32 -0
  93. package/docs/ontology/oam-kubevela.md +32 -0
  94. package/docs/ontology/operations-and-release.md +25 -0
  95. package/docs/ontology/personas-and-actors.md +32 -0
  96. package/docs/ontology/policies-and-invariants.md +33 -0
  97. package/docs/ontology/problem-space.md +30 -0
  98. package/docs/ontology/resource-contracts.md +40 -0
  99. package/docs/ontology/resource-taxonomy.md +42 -0
  100. package/docs/ontology/runners-and-ci.md +29 -0
  101. package/docs/ontology/solution-space.md +24 -0
  102. package/docs/ontology/storage-and-data-boundaries.md +29 -0
  103. package/docs/ontology/validation-matrix.md +24 -0
  104. package/docs/ontology/web-ui-excellent-flows.md +32 -0
  105. package/docs/ontology/workflows.md +39 -0
  106. package/docs/ontology/world.md +35 -0
  107. package/docs/product-requirements.md +62 -0
  108. package/docs/roadmap-mvp.md +87 -0
  109. package/docs/system-requirements.md +90 -0
  110. package/docs/tests/README.md +53 -0
  111. package/docs/tests/agent-qa-plan.md +63 -0
  112. package/docs/tests/browser-ui-tests.md +62 -0
  113. package/docs/tests/ci-quality-gates.md +48 -0
  114. package/docs/tests/coverage-model.md +64 -0
  115. package/docs/tests/e2e-scenario-tests.md +53 -0
  116. package/docs/tests/fixtures-test-data.md +63 -0
  117. package/docs/tests/observability-reliability-tests.md +54 -0
  118. package/docs/tests/product-test-matrix.md +145 -0
  119. package/docs/tests/qa-adoption-roadmap.md +130 -0
  120. package/docs/tests/qa-automation-plan.md +101 -0
  121. package/docs/tests/security-compliance-tests.md +57 -0
  122. package/docs/tests/test-framework-tools.md +88 -0
  123. package/docs/tests/test-suite-layout.md +121 -0
  124. package/docs/tests/unit-integration-tests.md +48 -0
  125. package/docs/todo-kyverno +714 -0
  126. package/docs/todos.md +4 -0
  127. package/docs/user-stories.md +78 -0
  128. package/examples/minikube-demo.yaml +190 -0
  129. package/examples/oam-application.yaml +23 -0
  130. package/examples/policy-kyverno-pr-title.yaml +18 -0
  131. package/package.json +63 -0
  132. package/scripts/build.mjs +29 -0
  133. package/scripts/setup-minikube.mjs +65 -0
  134. package/scripts/smoke.mjs +37 -0
  135. package/scripts/validate-doc-coverage.mjs +152 -0
  136. package/scripts/validate-package.mjs +93 -0
  137. package/scripts/validate-ui.mjs +236 -0
  138. package/src/agent-adapter-controller.js +169 -0
  139. package/src/agent-approval-controller.js +170 -0
  140. package/src/agent-context-bundles.js +242 -0
  141. package/src/agent-dispatch-controller.js +209 -0
  142. package/src/agent-gateway-config-controller.js +147 -0
  143. package/src/agent-memory-controller.js +357 -0
  144. package/src/agent-memory-import.js +327 -0
  145. package/src/agent-memory-query.js +292 -0
  146. package/src/agent-memory-repository-source-controller.js +255 -0
  147. package/src/agent-mux-client.js +280 -0
  148. package/src/agent-permission-review.js +250 -0
  149. package/src/agent-project-controller.js +117 -0
  150. package/src/agent-provider-config-controller.js +150 -0
  151. package/src/agent-secret-config-grant-controller.js +282 -0
  152. package/src/agent-session-transcript-controller.js +189 -0
  153. package/src/agent-stack-controller.js +347 -0
  154. package/src/agent-subagent-controller.js +160 -0
  155. package/src/agent-transport-binding-controller.js +121 -0
  156. package/src/agent-trigger-controller.js +321 -0
  157. package/src/agent-workspace-controller.js +447 -0
  158. package/src/agent-writeback-controller.js +302 -0
  159. package/src/api-controller.js +541 -0
  160. package/src/argocd-gitops.js +43 -0
  161. package/src/async-controller.js +207 -0
  162. package/src/audit-controller.js +191 -0
  163. package/src/auth.js +307 -0
  164. package/src/component-catalog.js +41 -0
  165. package/src/control-plane.js +136 -0
  166. package/src/controller-client.js +50 -0
  167. package/src/controller-ui.js +551 -0
  168. package/src/data-plane.js +178 -0
  169. package/src/event-bus.js +61 -0
  170. package/src/external/conflict-controller.js +225 -0
  171. package/src/external/github/auth.js +96 -0
  172. package/src/external/github/cicd.js +180 -0
  173. package/src/external/github/git-forge.js +240 -0
  174. package/src/external/github/index.js +144 -0
  175. package/src/external/github/issue-tracking.js +163 -0
  176. package/src/external/provider-adapter.js +161 -0
  177. package/src/external/provider-resource-factory.js +161 -0
  178. package/src/external/sync-controller.js +235 -0
  179. package/src/external/webhook-controller.js +144 -0
  180. package/src/external/write-controller.js +283 -0
  181. package/src/gitea-backend.js +95 -0
  182. package/src/gitea-service.js +173 -0
  183. package/src/handoff.js +98 -0
  184. package/src/hooks-events.js +63 -0
  185. package/src/http-server.js +377 -0
  186. package/src/identity-policy.js +86 -0
  187. package/src/index.js +55 -0
  188. package/src/kubernetes-controller-async.js +511 -0
  189. package/src/kubernetes-controller.js +878 -0
  190. package/src/kubernetes-resource-gateway.js +48 -0
  191. package/src/operations.js +112 -0
  192. package/src/org-scoping.js +5 -0
  193. package/src/resource-model.js +221 -0
  194. package/src/runners-ci.js +48 -0
  195. package/src/runtime.js +196 -0
  196. package/src/snapshot-cache.js +157 -0
  197. package/src/web-ui.js +40 -0
  198. package/tests/agent-adapter-controller.test.js +361 -0
  199. package/tests/agent-approval-controller.test.js +173 -0
  200. package/tests/agent-context-bundles.test.js +278 -0
  201. package/tests/agent-dispatch-controller.test.js +315 -0
  202. package/tests/agent-gateway-config-controller.test.js +386 -0
  203. package/tests/agent-memory-controller.test.js +308 -0
  204. package/tests/agent-memory-import-snapshot.test.js +477 -0
  205. package/tests/agent-memory-query.test.js +404 -0
  206. package/tests/agent-memory-repository-source.test.js +514 -0
  207. package/tests/agent-mux-client.test.js +204 -0
  208. package/tests/agent-permission-review-v2.test.js +317 -0
  209. package/tests/agent-permission-review.test.js +209 -0
  210. package/tests/agent-project-controller.test.js +302 -0
  211. package/tests/agent-provider-config-controller.test.js +376 -0
  212. package/tests/agent-resources.test.js +228 -0
  213. package/tests/agent-secret-config-grant.test.js +231 -0
  214. package/tests/agent-session-transcript-controller.test.js +499 -0
  215. package/tests/agent-stack-controller.test.js +221 -0
  216. package/tests/agent-subagent-controller.test.js +201 -0
  217. package/tests/agent-transport-binding-controller.test.js +294 -0
  218. package/tests/agent-trigger-controller.test.js +211 -0
  219. package/tests/agent-trigger-routes.test.js +190 -0
  220. package/tests/agent-trigger-sources.test.js +245 -0
  221. package/tests/agent-workspace-controller.test.js +181 -0
  222. package/tests/agent-writeback.test.js +292 -0
  223. package/tests/approval-persistence.test.js +171 -0
  224. package/tests/async-controller.test.js +252 -0
  225. package/tests/audit-controller.test.js +227 -0
  226. package/tests/deployment.test.js +396 -0
  227. package/tests/e2e/lifecycle.test.js +117 -0
  228. package/tests/external-github-forge.test.js +560 -0
  229. package/tests/external-github-issues-cicd.test.js +520 -0
  230. package/tests/external-integration.test.js +470 -0
  231. package/tests/external-persistence.test.js +340 -0
  232. package/tests/external-provider-adapter.test.js +365 -0
  233. package/tests/external-resource-model.test.js +215 -0
  234. package/tests/external-webhook-sync.test.js +287 -0
  235. package/tests/external-write-conflict.test.js +353 -0
  236. package/tests/gitea-service.test.js +253 -0
  237. package/tests/health-check-real.test.js +165 -0
  238. package/tests/integration/full-flow.test.js +266 -0
  239. package/tests/krate.test.js +727 -0
  240. package/tests/memory-search-wiring.test.js +270 -0
  241. package/tests/org-scoping.test.js +687 -0
  242. package/tests/session-cookie-hmac.test.js +151 -0
  243. package/tests/snapshot-performance.test.js +247 -0
  244. package/tests/sse-events.test.js +107 -0
  245. package/tests/workspace-volumes.test.js +312 -0
  246. package/tests/writeback-persistence.test.js +207 -0
@@ -0,0 +1,541 @@
1
+ import { createKubernetesResourceGateway } from './kubernetes-resource-gateway.js';
2
+ import { clearSnapshotCache } from './snapshot-cache.js';
3
+ import { createPermissionReviewer } from './agent-permission-review.js';
4
+ import { createAgentDispatchController } from './agent-dispatch-controller.js';
5
+ import { createAgentApprovalController } from './agent-approval-controller.js';
6
+ import { createAgentTriggerController } from './agent-trigger-controller.js';
7
+ import { createAgentWorkspaceController } from './agent-workspace-controller.js';
8
+ import { createAgentMemoryController } from './agent-memory-controller.js';
9
+ import { orgNamespaceName, normalizeOrgSlug } from './org-scoping.js';
10
+ import { globalEventBus } from './event-bus.js';
11
+ import { createSyncController } from './external/sync-controller.js';
12
+ import { createWebhookController } from './external/webhook-controller.js';
13
+ import { createWriteController } from './external/write-controller.js';
14
+ import { createConflictController } from './external/conflict-controller.js';
15
+
16
+ export const KRATE_API_CONTROLLER_BOUNDARY = {
17
+ role: 'krate-api-controller',
18
+ scope: 'HTTP/application facade for validation, request orchestration, user-facing DTOs, API errors, and workflow affordances',
19
+ owns: ['input validation', 'forge DTOs', 'API errors', 'workflow affordances', 'controller UI snapshots'],
20
+ delegatesTo: ['kubernetes-resource-gateway', 'git-data-plane'],
21
+ mustNotOwn: ['kubectl process execution', 'Kubernetes reconciliation loops', 'watch stream internals', 'repository storage internals']
22
+ };
23
+
24
+ export function createKrateApiController(options = {}) {
25
+ const resourceGateway = options.resourceGateway || createKubernetesResourceGateway(options);
26
+ const namespace = options.namespace || resourceGateway.namespace || process.env.KRATE_NAMESPACE || 'krate-system';
27
+ const onAuditEvent = typeof options.onAuditEvent === 'function' ? options.onAuditEvent : null;
28
+
29
+ function emitAuditEvent(resource, operation) {
30
+ if (!onAuditEvent) return;
31
+ try {
32
+ const org = resource.spec?.organizationRef || resource.metadata?.labels?.['krate.a5c.ai/org'] || '';
33
+ onAuditEvent({
34
+ operation,
35
+ org,
36
+ namespace: org ? orgNamespaceName(org) : (resource.metadata?.namespace || namespace),
37
+ kind: resource.kind,
38
+ name: resource.metadata?.name,
39
+ timestamp: new Date().toISOString()
40
+ });
41
+ } catch {
42
+ // Audit failures must not crash apply operations
43
+ }
44
+ }
45
+
46
+ return {
47
+ role: 'krate-api-controller',
48
+ namespace,
49
+ resourceGateway,
50
+ resourceDefinitions: resourceGateway.resourceDefinitions,
51
+ async snapshot() {
52
+ return withArchitecture(await resourceGateway.snapshot(), namespace);
53
+ },
54
+ async listRepositoriesForForge() {
55
+ const resources = await resourceGateway.list('Repository');
56
+ return normalizeResourceList(resources).map((resource) => repositoryForgeSummary(resource, namespace));
57
+ },
58
+ async getRepositoryForgeView(name) {
59
+ const resource = await resourceGateway.get('Repository', name);
60
+ const repository = resource?.resource || resource;
61
+ return repositoryForgeView(repository, namespace);
62
+ },
63
+ async listResource(kindOrPlural) {
64
+ return resourceGateway.list(kindOrPlural);
65
+ },
66
+ async listResourceForOrg(org, kindOrPlural) {
67
+ const orgNs = orgNamespaceName(normalizeOrgSlug(org));
68
+ // Client-side filtering is used because the resource gateway's list()
69
+ // method does not currently support namespace-scoped listing. The
70
+ // gateway aggregates resources across namespaces at snapshot time, so
71
+ // filtering here is both correct and consistent with the gateway API.
72
+ const result = await resourceGateway.list(kindOrPlural);
73
+ const items = normalizeResourceList(result).filter(
74
+ (item) => item.metadata?.namespace === orgNs
75
+ );
76
+ return { ...result, items };
77
+ },
78
+ async getResource(kindOrPlural, name) {
79
+ return resourceGateway.get(kindOrPlural, name);
80
+ },
81
+ async applyResource(resource) {
82
+ // Cross-org admission check: if the resource has an organizationRef,
83
+ // ensure the namespace matches the org's derived namespace.
84
+ const resourceOrg = resource.spec?.organizationRef;
85
+ const resourceNs = resource.metadata?.namespace;
86
+ if (resourceOrg) {
87
+ const expectedNs = orgNamespaceName(resourceOrg);
88
+ if (resourceNs && resourceNs !== expectedNs) {
89
+ // Explicit namespace does not match the org — reject
90
+ throw new Error(
91
+ `Cross-org namespace mismatch: resource organizationRef "${resourceOrg}" expects namespace "${expectedNs}" but got "${resourceNs}"`
92
+ );
93
+ }
94
+ if (!resourceNs) {
95
+ // organizationRef present but no namespace — auto-assign
96
+ resource = {
97
+ ...resource,
98
+ metadata: { ...resource.metadata, namespace: expectedNs }
99
+ };
100
+ }
101
+ }
102
+ const result = await resourceGateway.apply(resource);
103
+ clearSnapshotCache();
104
+ const appliedResource = result.resource || resource;
105
+ emitAuditEvent(appliedResource, result.operation || 'apply');
106
+ globalEventBus.emitResourceChange(
107
+ appliedResource.kind || resource.kind || 'Unknown',
108
+ appliedResource.metadata?.name || resource.metadata?.name || 'unknown',
109
+ result.operation || 'apply'
110
+ );
111
+ return result;
112
+ },
113
+ async applyResourceForOrg(orgSlug, resource) {
114
+ const slug = normalizeOrgSlug(orgSlug);
115
+ const orgNs = orgNamespaceName(slug);
116
+ const resourceOrg = resource.spec?.organizationRef;
117
+ if (resourceOrg && normalizeOrgSlug(resourceOrg) !== slug) {
118
+ throw new Error(
119
+ `Org mismatch: resource organizationRef "${resourceOrg}" does not match target org "${slug}"`
120
+ );
121
+ }
122
+ const scopedResource = {
123
+ ...resource,
124
+ metadata: { ...resource.metadata, namespace: orgNs },
125
+ spec: { ...resource.spec, organizationRef: slug }
126
+ };
127
+ const result = await resourceGateway.apply(scopedResource);
128
+ clearSnapshotCache();
129
+ const appliedResource = result.resource || scopedResource;
130
+ emitAuditEvent(appliedResource, result.operation || 'apply');
131
+ globalEventBus.emitResourceChange(
132
+ appliedResource.kind || scopedResource.kind || 'Unknown',
133
+ appliedResource.metadata?.name || scopedResource.metadata?.name || 'unknown',
134
+ result.operation || 'apply'
135
+ );
136
+ return { ...result, resource: appliedResource };
137
+ },
138
+ async deleteResource(kindOrPlural, name) {
139
+ const result = await resourceGateway.delete(kindOrPlural, name);
140
+ clearSnapshotCache();
141
+ emitAuditEvent(
142
+ { kind: kindOrPlural, metadata: { name, namespace }, spec: {} },
143
+ 'delete'
144
+ );
145
+ globalEventBus.emitResourceChange(kindOrPlural, name, 'delete');
146
+ return result;
147
+ },
148
+ async deleteResourceForOrg(orgSlug, kindOrPlural, name) {
149
+ const slug = normalizeOrgSlug(orgSlug);
150
+ const orgNs = orgNamespaceName(slug);
151
+ // Verify the resource exists and belongs to the org before deleting
152
+ const existing = await resourceGateway.get(kindOrPlural, name);
153
+ const resource = existing?.resource || existing;
154
+ if (resource) {
155
+ const resourceNs = resource.metadata?.namespace;
156
+ if (!resourceNs || resourceNs !== orgNs) {
157
+ throw new Error(
158
+ `Cross-org denial: resource "${name}" is in namespace "${resourceNs || '(none)'}" which does not match org "${slug}" namespace "${orgNs}"`
159
+ );
160
+ }
161
+ }
162
+ const result = await resourceGateway.delete(kindOrPlural, name);
163
+ clearSnapshotCache();
164
+ emitAuditEvent(
165
+ { kind: kindOrPlural, metadata: { name, namespace: orgNs }, spec: { organizationRef: slug } },
166
+ 'delete'
167
+ );
168
+ globalEventBus.emitResourceChange(kindOrPlural, name, 'delete');
169
+ return result;
170
+ },
171
+ async getResourceForOrg(orgSlug, kindOrPlural, name) {
172
+ const slug = normalizeOrgSlug(orgSlug);
173
+ const orgNs = orgNamespaceName(slug);
174
+ const existing = await resourceGateway.get(kindOrPlural, name);
175
+ const resource = existing?.resource || existing;
176
+ if (resource) {
177
+ const resourceNs = resource.metadata?.namespace;
178
+ if (!resourceNs || resourceNs !== orgNs) {
179
+ throw new Error(
180
+ `Cross-org denial: resource "${name}" is in namespace "${resourceNs || '(none)'}" which does not match org "${slug}" namespace "${orgNs}"`
181
+ );
182
+ }
183
+ }
184
+ return existing;
185
+ },
186
+ async createRepository(input) {
187
+ const created = await resourceGateway.createRepository(input);
188
+ const repository = created?.resource || created;
189
+ emitAuditEvent(
190
+ repository?.kind ? repository : { kind: 'Repository', metadata: repository?.metadata || { name: input.name || input.metadata?.name }, spec: repository?.spec || input.spec || input },
191
+ 'create-repository'
192
+ );
193
+ return {
194
+ operation: created?.operation || 'create-repository',
195
+ command: created?.command || 'kubectl apply -f -',
196
+ repository: repositoryForgeSummary(repository, namespace),
197
+ resource: repository
198
+ };
199
+ },
200
+ async createOrganization(input) {
201
+ const result = await resourceGateway.createOrganization(input);
202
+ const orgResource = result?.organization || result?.resource || result;
203
+ emitAuditEvent(
204
+ orgResource?.kind ? orgResource : { kind: 'Organization', metadata: orgResource?.metadata || { name: input.slug || input.name || input.metadata?.name }, spec: orgResource?.spec || input.spec || input },
205
+ 'create-organization'
206
+ );
207
+ return result;
208
+ },
209
+ watchResource(resourcePath, handlers = {}) {
210
+ return resourceGateway.watch(resourcePath, handlers);
211
+ },
212
+ async reviewAgentPermissions(input) {
213
+ const reviewer = createPermissionReviewer();
214
+ const snapshot = await this.snapshot();
215
+ return reviewer.reviewPermissions({
216
+ ...input,
217
+ resources: snapshot.resources
218
+ });
219
+ },
220
+ async dispatchAgent(input) {
221
+ const snapshot = await this.snapshot();
222
+ const controller = createAgentDispatchController(input.controllerOptions || {});
223
+ return controller.createManualDispatch({
224
+ ...input,
225
+ resources: snapshot.resources
226
+ });
227
+ },
228
+ async approveAgentAction(input) {
229
+ const snapshot = await this.snapshot();
230
+ const approvalController = createAgentApprovalController();
231
+ return approvalController.recordDecision({
232
+ ...input,
233
+ decision: 'approve',
234
+ resources: snapshot.resources
235
+ });
236
+ },
237
+ async denyAgentAction(input) {
238
+ const snapshot = await this.snapshot();
239
+ const approvalController = createAgentApprovalController();
240
+ return approvalController.recordDecision({
241
+ ...input,
242
+ decision: 'deny',
243
+ resources: snapshot.resources
244
+ });
245
+ },
246
+ async processWebhookEvent(input) {
247
+ const snapshot = await this.snapshot();
248
+ const dispatchController = createAgentDispatchController(input.controllerOptions || {});
249
+ const triggerController = createAgentTriggerController({ dispatchController });
250
+ return triggerController.processEvent({
251
+ event: input.event,
252
+ resources: snapshot.resources,
253
+ namespace: input.namespace || namespace,
254
+ organizationRef: input.organizationRef || 'default',
255
+ });
256
+ },
257
+ async provisionAgentWorkspace(input) {
258
+ const workspaceController = createAgentWorkspaceController();
259
+ return workspaceController.provisionWorkspace({
260
+ ...input,
261
+ namespace: input.namespace || namespace,
262
+ organizationRef: input.organizationRef || 'default'
263
+ });
264
+ },
265
+ async archiveAgentWorkspace(input) {
266
+ const snapshot = await this.snapshot();
267
+ const workspaceController = createAgentWorkspaceController();
268
+ return workspaceController.archiveWorkspace({
269
+ ...input,
270
+ resources: snapshot.resources
271
+ });
272
+ },
273
+ async linkWorkItem(input) {
274
+ const workspaceController = createAgentWorkspaceController();
275
+ return workspaceController.linkWorkItem({
276
+ ...input,
277
+ namespace: input.namespace || namespace,
278
+ organizationRef: input.organizationRef || 'default'
279
+ });
280
+ },
281
+ async queryAgentMemory(input) {
282
+ const memoryController = createAgentMemoryController();
283
+ return memoryController.queryMemory({
284
+ ...input,
285
+ namespace: input.namespace || namespace,
286
+ organizationRef: input.organizationRef || 'default'
287
+ });
288
+ },
289
+ async createMemoryImport(input) {
290
+ const memoryController = createAgentMemoryController();
291
+ return memoryController.createImport({
292
+ ...input,
293
+ namespace: input.namespace || namespace,
294
+ organizationRef: input.organizationRef || 'default'
295
+ });
296
+ },
297
+
298
+ // ---------------------------------------------------------------------------
299
+ // External controller integration
300
+ // ---------------------------------------------------------------------------
301
+
302
+ /**
303
+ * Sync an external resource into the Krate resource store.
304
+ * Creates a SyncController with a persistFn that calls applyResource, then
305
+ * upserts the resource and optionally advances the watermark.
306
+ *
307
+ * @param {string} bindingName
308
+ * @param {{ kind, localName, namespace?, spec, externalEnvelope, watermark? }} options
309
+ */
310
+ async syncExternalBinding(bindingName, options = {}) {
311
+ const self = this;
312
+ const syncController = createSyncController({
313
+ persistFn: async (resource) => {
314
+ await resourceGateway.apply(resource);
315
+ }
316
+ });
317
+
318
+ const {
319
+ kind,
320
+ localName,
321
+ namespace: resourceNamespace = 'default',
322
+ spec = {},
323
+ externalEnvelope,
324
+ watermark
325
+ } = options;
326
+
327
+ const resource = syncController.upsertResource({
328
+ kind,
329
+ localName,
330
+ namespace: resourceNamespace,
331
+ spec,
332
+ externalEnvelope
333
+ });
334
+
335
+ if (watermark) {
336
+ syncController.updateWatermark(bindingName, watermark);
337
+ }
338
+
339
+ // Keep a reference to the sync controller so getExternalSyncStatus can read it
340
+ if (!self._syncControllers) self._syncControllers = new Map();
341
+ self._syncControllers.set(bindingName, syncController);
342
+
343
+ return { resource, bindingName };
344
+ },
345
+
346
+ /**
347
+ * Create a write intent for an external operation.
348
+ *
349
+ * @param {{ interfaceKey, operation, payload?, resourceRef, requiresApproval?,
350
+ * maxRetries?, namespace?, organizationRef? }} input
351
+ */
352
+ async createExternalWriteIntent(input) {
353
+ const writeController = createWriteController({
354
+ persistFn: async (resource) => {
355
+ await resourceGateway.apply(resource);
356
+ }
357
+ });
358
+ return writeController.createWriteIntent(input);
359
+ },
360
+
361
+ /**
362
+ * Approve a PendingApproval ExternalWriteIntent.
363
+ *
364
+ * @param {{ intentName, approvedBy, resources? }} opts
365
+ */
366
+ async approveExternalWriteIntent(opts) {
367
+ const writeController = createWriteController({
368
+ persistFn: async (resource) => {
369
+ await resourceGateway.apply(resource);
370
+ }
371
+ });
372
+ return writeController.approveWriteIntent(opts);
373
+ },
374
+
375
+ /**
376
+ * Cancel (reject) an ExternalWriteIntent.
377
+ *
378
+ * @param {{ intentName, cancelledBy, resources? }} opts
379
+ */
380
+ async cancelExternalWriteIntent({ intentName, cancelledBy, resources } = {}) {
381
+ const writeController = createWriteController({
382
+ persistFn: async (resource) => {
383
+ await resourceGateway.apply(resource);
384
+ }
385
+ });
386
+ return writeController.rejectWriteIntent({
387
+ intentName,
388
+ rejectedBy: cancelledBy,
389
+ reason: 'cancelled',
390
+ resources
391
+ });
392
+ },
393
+
394
+ /**
395
+ * Detect a conflict between local and external field values.
396
+ *
397
+ * @param {{ resourceRef, fieldPath, localValue, externalValue, namespace?, organizationRef? }} input
398
+ */
399
+ async detectExternalConflict(input) {
400
+ const conflictController = createConflictController({
401
+ persistFn: async (resource) => {
402
+ await resourceGateway.apply(resource);
403
+ }
404
+ });
405
+ return conflictController.detectConflict(input);
406
+ },
407
+
408
+ /**
409
+ * Resolve an Open ExternalSyncConflict using the specified strategy.
410
+ *
411
+ * @param {{ conflictName, strategy, resolvedValue?, resources? }} opts
412
+ */
413
+ async resolveExternalConflict(opts) {
414
+ const conflictController = createConflictController({
415
+ persistFn: async (resource) => {
416
+ await resourceGateway.apply(resource);
417
+ }
418
+ });
419
+ return conflictController.resolveConflict(opts);
420
+ },
421
+
422
+ /**
423
+ * Return the current sync state for a binding (watermark, etc.).
424
+ *
425
+ * @param {string} bindingName
426
+ * @returns {{ bindingName: string, watermark: string|null }}
427
+ */
428
+ async getExternalSyncStatus(bindingName) {
429
+ const syncController = this._syncControllers?.get(bindingName);
430
+ const watermark = syncController ? syncController.getWatermark(bindingName) : null;
431
+ return { bindingName, watermark };
432
+ },
433
+
434
+ /**
435
+ * Process an inbound external webhook payload.
436
+ * Creates a WebhookController, processes the delivery, and emits events.
437
+ *
438
+ * @param {{ deliveryId, eventType, payload, rawBody, providerType?, secret? }} params
439
+ */
440
+ async processExternalWebhook({ deliveryId, eventType, payload, rawBody, providerType, secret } = {}) {
441
+ const webhookController = createWebhookController({ secret: secret || '' });
442
+ return webhookController.processDelivery({ deliveryId, eventType, payload, rawBody });
443
+ }
444
+ };
445
+ }
446
+
447
+ export function withArchitecture(snapshot, namespace = snapshot?.namespace || 'default') {
448
+ return {
449
+ ...snapshot,
450
+ architecture: {
451
+ apiController: {
452
+ ...KRATE_API_CONTROLLER_BOUNDARY,
453
+ owns: [...KRATE_API_CONTROLLER_BOUNDARY.owns, '/api/controller', '/api/orgs/:org/resources', '/api/orgs/:org/repositories', '/api/watch/orgs/:org/*'],
454
+ scope: `${KRATE_API_CONTROLLER_BOUNDARY.scope}; never owns Kubernetes reconciliation loops`
455
+ },
456
+ resourceGateway: {
457
+ role: 'kubernetes-resource-gateway',
458
+ scope: 'Narrow application port translating API controller intent into Kubernetes resource-client calls',
459
+ namespace,
460
+ delegatesTo: ['kubernetes-resource-client']
461
+ },
462
+ kubernetesClient: {
463
+ role: 'kubernetes-resource-client',
464
+ scope: 'kubectl-backed Kubernetes API discovery, SubjectAccessReview checks, list/get/apply/delete/watch; no UI flow or product workflow ownership',
465
+ namespace,
466
+ owns: ['Krate CRDs', 'aggregated API resources', 'Kubernetes watch streams']
467
+ },
468
+ kubernetesReconciler: {
469
+ role: 'krate-kubernetes-reconciler',
470
+ scope: 'Repository status projection, repository hosting intent, policy projection, and data-plane sync intent; never owns HTTP routes or browser flows',
471
+ namespace,
472
+ delegatesTo: ['kubernetes-resource-gateway', 'git-data-plane']
473
+ },
474
+ dataPlane: {
475
+ role: 'git-data-plane',
476
+ scope: 'Repository streaming, SSH hosting, object storage, search indexing, and warm receive-pack paths',
477
+ boundary: process.env.KRATE_GITEA_HTTP_URL || 'repository service not configured'
478
+ }
479
+ }
480
+ };
481
+ }
482
+
483
+ export function repositoryForgeSummary(resource, namespace = 'krate-system') {
484
+ const metadata = resource?.metadata || {};
485
+ const spec = resource?.spec || {};
486
+ const name = metadata.name || 'unknown-repository';
487
+ const repositoryNamespace = metadata.namespace || namespace;
488
+ const org = spec.organizationRef || metadata.labels?.['krate.a5c.ai/org'] || 'default';
489
+ const repoPath = `/orgs/${encodeURIComponent(org)}/repositories/${encodeURIComponent(name)}`;
490
+ return {
491
+ kind: 'Repository',
492
+ name,
493
+ org,
494
+ namespace: repositoryNamespace,
495
+ visibility: spec.visibility || 'internal',
496
+ defaultBranch: spec.defaultBranch || 'main',
497
+ phase: resource?.status?.phase || (resource ? 'Ready' : 'Unknown'),
498
+ href: `${repoPath}/code`,
499
+ cloneUrl: spec.gitHosting?.httpUrl || `<krate-repository-service>/${encodeURIComponent(org)}/${name}.git`,
500
+ actions: {
501
+ code: `${repoPath}/code`,
502
+ pullRequests: `${repoPath}/pull-requests`,
503
+ issues: `${repoPath}/issues`,
504
+ runs: `${repoPath}/runs`,
505
+ pipelines: `${repoPath}/runs`,
506
+ hooks: `${repoPath}/hooks`,
507
+ settings: `${repoPath}/settings`,
508
+ yaml: `/orgs/${encodeURIComponent(org)}/advanced-plans?kind=Repository&name=${encodeURIComponent(name)}`
509
+ },
510
+ kubectl: {
511
+ get: `kubectl get repositories.krate.a5c.ai ${name} -n ${repositoryNamespace} -o yaml`,
512
+ delete: `kubectl delete repositories.krate.a5c.ai ${name} -n ${repositoryNamespace}`
513
+ }
514
+ };
515
+ }
516
+
517
+ export function repositoryForgeView(resource, namespace = 'default') {
518
+ const summary = repositoryForgeSummary(resource, namespace);
519
+ return {
520
+ ...summary,
521
+ primaryFlow: 'browse-code-open-pr-review-merge',
522
+ emptyState: resource ? null : 'Repository resource is not available from the Kubernetes resource gateway.',
523
+ sections: [
524
+ { id: 'code', label: 'Code', href: summary.actions.code, state: 'branch-and-path-aware' },
525
+ { id: 'pull-requests', label: 'Pull requests', href: summary.actions.pullRequests, state: 'review-merge-checks' },
526
+ { id: 'issues', label: 'Issues', href: summary.actions.issues, state: 'triage-policy-aware' },
527
+ { id: 'runs', label: 'Runs', href: summary.actions.runs, state: 'runner-and-job-aware' },
528
+ { id: 'hooks', label: 'Hooks', href: summary.actions.hooks, state: 'delivery-replay-aware' },
529
+ { id: 'settings', label: 'Settings', href: summary.actions.settings, state: 'branch-protection-rbac-danger-actions' }
530
+ ]
531
+ };
532
+ }
533
+
534
+ function normalizeResourceList(result) {
535
+ if (Array.isArray(result)) return result;
536
+ if (Array.isArray(result?.items)) return result.items;
537
+ if (Array.isArray(result?.resources)) return result.resources;
538
+ if (result?.resource) return [result.resource];
539
+ return [];
540
+ }
541
+
@@ -0,0 +1,43 @@
1
+ export function createArgoCdApplication({
2
+ name = 'krate',
3
+ namespace = 'argocd',
4
+ project = 'default',
5
+ repoURL,
6
+ path = 'charts/krate',
7
+ targetRevision = 'HEAD',
8
+ destinationNamespace = 'krate-system',
9
+ destinationServer = 'https://kubernetes.default.svc',
10
+ automated = true
11
+ } = {}) {
12
+ if (!repoURL) throw new Error('Argo CD Application requires repoURL');
13
+ return {
14
+ apiVersion: 'argoproj.io/v1alpha1',
15
+ kind: 'Application',
16
+ metadata: {
17
+ name,
18
+ namespace,
19
+ labels: {
20
+ 'app.kubernetes.io/part-of': 'krate',
21
+ 'krate.a5c.ai/gitops-engine': 'argocd'
22
+ }
23
+ },
24
+ spec: {
25
+ project,
26
+ source: { repoURL, targetRevision, path },
27
+ destination: { server: destinationServer, namespace: destinationNamespace },
28
+ syncPolicy: automated ? {
29
+ automated: { prune: true, selfHeal: true },
30
+ syncOptions: ['CreateNamespace=true']
31
+ } : { syncOptions: ['CreateNamespace=true'] }
32
+ }
33
+ };
34
+ }
35
+
36
+ export function createKrateGitOpsPlan({ repoURL, namespace = 'krate-system', applicationName = 'krate' }) {
37
+ return {
38
+ engine: 'argocd',
39
+ application: createArgoCdApplication({ name: applicationName, repoURL, destinationNamespace: namespace }),
40
+ requiredClusterResources: ['Application.argoproj.io', 'Namespace', 'ServiceAccount', 'RBAC', 'APIService', 'Krate CRDs'],
41
+ syncGuarantees: ['automated prune', 'automated selfHeal', 'namespace creation']
42
+ };
43
+ }