@synth-deploy/server 0.1.0

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 (317) hide show
  1. package/dist/agent/debrief-retention.d.ts +12 -0
  2. package/dist/agent/debrief-retention.d.ts.map +1 -0
  3. package/dist/agent/debrief-retention.js +27 -0
  4. package/dist/agent/debrief-retention.js.map +1 -0
  5. package/dist/agent/envoy-client.d.ts +216 -0
  6. package/dist/agent/envoy-client.d.ts.map +1 -0
  7. package/dist/agent/envoy-client.js +266 -0
  8. package/dist/agent/envoy-client.js.map +1 -0
  9. package/dist/agent/envoy-registry.d.ts +102 -0
  10. package/dist/agent/envoy-registry.d.ts.map +1 -0
  11. package/dist/agent/envoy-registry.js +319 -0
  12. package/dist/agent/envoy-registry.js.map +1 -0
  13. package/dist/agent/health-checker.d.ts +39 -0
  14. package/dist/agent/health-checker.d.ts.map +1 -0
  15. package/dist/agent/health-checker.js +49 -0
  16. package/dist/agent/health-checker.js.map +1 -0
  17. package/dist/agent/mcp-client-manager.d.ts +36 -0
  18. package/dist/agent/mcp-client-manager.d.ts.map +1 -0
  19. package/dist/agent/mcp-client-manager.js +106 -0
  20. package/dist/agent/mcp-client-manager.js.map +1 -0
  21. package/dist/agent/stale-deployment-detector.d.ts +15 -0
  22. package/dist/agent/stale-deployment-detector.d.ts.map +1 -0
  23. package/dist/agent/stale-deployment-detector.js +50 -0
  24. package/dist/agent/stale-deployment-detector.js.map +1 -0
  25. package/dist/agent/step-runner.d.ts +31 -0
  26. package/dist/agent/step-runner.d.ts.map +1 -0
  27. package/dist/agent/step-runner.js +80 -0
  28. package/dist/agent/step-runner.js.map +1 -0
  29. package/dist/agent/synth-agent.d.ts +168 -0
  30. package/dist/agent/synth-agent.d.ts.map +1 -0
  31. package/dist/agent/synth-agent.js +1195 -0
  32. package/dist/agent/synth-agent.js.map +1 -0
  33. package/dist/api/agent.d.ts +36 -0
  34. package/dist/api/agent.d.ts.map +1 -0
  35. package/dist/api/agent.js +867 -0
  36. package/dist/api/agent.js.map +1 -0
  37. package/dist/api/api-keys.d.ts +4 -0
  38. package/dist/api/api-keys.d.ts.map +1 -0
  39. package/dist/api/api-keys.js +118 -0
  40. package/dist/api/api-keys.js.map +1 -0
  41. package/dist/api/artifacts.d.ts +5 -0
  42. package/dist/api/artifacts.d.ts.map +1 -0
  43. package/dist/api/artifacts.js +142 -0
  44. package/dist/api/artifacts.js.map +1 -0
  45. package/dist/api/auth.d.ts +4 -0
  46. package/dist/api/auth.d.ts.map +1 -0
  47. package/dist/api/auth.js +280 -0
  48. package/dist/api/auth.js.map +1 -0
  49. package/dist/api/deployments.d.ts +11 -0
  50. package/dist/api/deployments.d.ts.map +1 -0
  51. package/dist/api/deployments.js +1098 -0
  52. package/dist/api/deployments.js.map +1 -0
  53. package/dist/api/environments.d.ts +5 -0
  54. package/dist/api/environments.d.ts.map +1 -0
  55. package/dist/api/environments.js +69 -0
  56. package/dist/api/environments.js.map +1 -0
  57. package/dist/api/envoy-reports.d.ts +17 -0
  58. package/dist/api/envoy-reports.d.ts.map +1 -0
  59. package/dist/api/envoy-reports.js +138 -0
  60. package/dist/api/envoy-reports.js.map +1 -0
  61. package/dist/api/envoys.d.ts +5 -0
  62. package/dist/api/envoys.d.ts.map +1 -0
  63. package/dist/api/envoys.js +192 -0
  64. package/dist/api/envoys.js.map +1 -0
  65. package/dist/api/fleet.d.ts +11 -0
  66. package/dist/api/fleet.d.ts.map +1 -0
  67. package/dist/api/fleet.js +394 -0
  68. package/dist/api/fleet.js.map +1 -0
  69. package/dist/api/graph.d.ts +8 -0
  70. package/dist/api/graph.d.ts.map +1 -0
  71. package/dist/api/graph.js +355 -0
  72. package/dist/api/graph.js.map +1 -0
  73. package/dist/api/health.d.ts +20 -0
  74. package/dist/api/health.d.ts.map +1 -0
  75. package/dist/api/health.js +248 -0
  76. package/dist/api/health.js.map +1 -0
  77. package/dist/api/idp-schemas.d.ts +41 -0
  78. package/dist/api/idp-schemas.d.ts.map +1 -0
  79. package/dist/api/idp-schemas.js +17 -0
  80. package/dist/api/idp-schemas.js.map +1 -0
  81. package/dist/api/idp.d.ts +6 -0
  82. package/dist/api/idp.d.ts.map +1 -0
  83. package/dist/api/idp.js +620 -0
  84. package/dist/api/idp.js.map +1 -0
  85. package/dist/api/intake.d.ts +10 -0
  86. package/dist/api/intake.d.ts.map +1 -0
  87. package/dist/api/intake.js +418 -0
  88. package/dist/api/intake.js.map +1 -0
  89. package/dist/api/partitions.d.ts +5 -0
  90. package/dist/api/partitions.d.ts.map +1 -0
  91. package/dist/api/partitions.js +113 -0
  92. package/dist/api/partitions.js.map +1 -0
  93. package/dist/api/progress-event-store.d.ts +62 -0
  94. package/dist/api/progress-event-store.d.ts.map +1 -0
  95. package/dist/api/progress-event-store.js +118 -0
  96. package/dist/api/progress-event-store.js.map +1 -0
  97. package/dist/api/schemas.d.ts +1000 -0
  98. package/dist/api/schemas.d.ts.map +1 -0
  99. package/dist/api/schemas.js +328 -0
  100. package/dist/api/schemas.js.map +1 -0
  101. package/dist/api/security-boundaries.d.ts +4 -0
  102. package/dist/api/security-boundaries.d.ts.map +1 -0
  103. package/dist/api/security-boundaries.js +32 -0
  104. package/dist/api/security-boundaries.js.map +1 -0
  105. package/dist/api/settings.d.ts +4 -0
  106. package/dist/api/settings.d.ts.map +1 -0
  107. package/dist/api/settings.js +99 -0
  108. package/dist/api/settings.js.map +1 -0
  109. package/dist/api/system.d.ts +75 -0
  110. package/dist/api/system.d.ts.map +1 -0
  111. package/dist/api/system.js +558 -0
  112. package/dist/api/system.js.map +1 -0
  113. package/dist/api/telemetry.d.ts +4 -0
  114. package/dist/api/telemetry.d.ts.map +1 -0
  115. package/dist/api/telemetry.js +24 -0
  116. package/dist/api/telemetry.js.map +1 -0
  117. package/dist/api/users.d.ts +4 -0
  118. package/dist/api/users.d.ts.map +1 -0
  119. package/dist/api/users.js +173 -0
  120. package/dist/api/users.js.map +1 -0
  121. package/dist/archive-unpacker.d.ts +24 -0
  122. package/dist/archive-unpacker.d.ts.map +1 -0
  123. package/dist/archive-unpacker.js +239 -0
  124. package/dist/archive-unpacker.js.map +1 -0
  125. package/dist/artifact-analyzer.d.ts +59 -0
  126. package/dist/artifact-analyzer.d.ts.map +1 -0
  127. package/dist/artifact-analyzer.js +334 -0
  128. package/dist/artifact-analyzer.js.map +1 -0
  129. package/dist/auth/idp/index.d.ts +9 -0
  130. package/dist/auth/idp/index.d.ts.map +1 -0
  131. package/dist/auth/idp/index.js +5 -0
  132. package/dist/auth/idp/index.js.map +1 -0
  133. package/dist/auth/idp/ldap.d.ts +56 -0
  134. package/dist/auth/idp/ldap.d.ts.map +1 -0
  135. package/dist/auth/idp/ldap.js +276 -0
  136. package/dist/auth/idp/ldap.js.map +1 -0
  137. package/dist/auth/idp/oidc.d.ts +27 -0
  138. package/dist/auth/idp/oidc.d.ts.map +1 -0
  139. package/dist/auth/idp/oidc.js +97 -0
  140. package/dist/auth/idp/oidc.js.map +1 -0
  141. package/dist/auth/idp/role-mapping.d.ts +9 -0
  142. package/dist/auth/idp/role-mapping.d.ts.map +1 -0
  143. package/dist/auth/idp/role-mapping.js +16 -0
  144. package/dist/auth/idp/role-mapping.js.map +1 -0
  145. package/dist/auth/idp/saml.d.ts +40 -0
  146. package/dist/auth/idp/saml.d.ts.map +1 -0
  147. package/dist/auth/idp/saml.js +117 -0
  148. package/dist/auth/idp/saml.js.map +1 -0
  149. package/dist/auth/idp/types.d.ts +23 -0
  150. package/dist/auth/idp/types.d.ts.map +1 -0
  151. package/dist/auth/idp/types.js +2 -0
  152. package/dist/auth/idp/types.js.map +1 -0
  153. package/dist/fleet/fleet-executor.d.ts +35 -0
  154. package/dist/fleet/fleet-executor.d.ts.map +1 -0
  155. package/dist/fleet/fleet-executor.js +228 -0
  156. package/dist/fleet/fleet-executor.js.map +1 -0
  157. package/dist/fleet/fleet-store.d.ts +13 -0
  158. package/dist/fleet/fleet-store.d.ts.map +1 -0
  159. package/dist/fleet/fleet-store.js +13 -0
  160. package/dist/fleet/fleet-store.js.map +1 -0
  161. package/dist/fleet/index.d.ts +5 -0
  162. package/dist/fleet/index.d.ts.map +1 -0
  163. package/dist/fleet/index.js +4 -0
  164. package/dist/fleet/index.js.map +1 -0
  165. package/dist/fleet/representative-selector.d.ts +15 -0
  166. package/dist/fleet/representative-selector.d.ts.map +1 -0
  167. package/dist/fleet/representative-selector.js +71 -0
  168. package/dist/fleet/representative-selector.js.map +1 -0
  169. package/dist/graph/graph-executor.d.ts +36 -0
  170. package/dist/graph/graph-executor.d.ts.map +1 -0
  171. package/dist/graph/graph-executor.js +348 -0
  172. package/dist/graph/graph-executor.js.map +1 -0
  173. package/dist/graph/graph-inference.d.ts +22 -0
  174. package/dist/graph/graph-inference.d.ts.map +1 -0
  175. package/dist/graph/graph-inference.js +149 -0
  176. package/dist/graph/graph-inference.js.map +1 -0
  177. package/dist/graph/graph-store.d.ts +12 -0
  178. package/dist/graph/graph-store.d.ts.map +1 -0
  179. package/dist/graph/graph-store.js +61 -0
  180. package/dist/graph/graph-store.js.map +1 -0
  181. package/dist/graph/index.d.ts +5 -0
  182. package/dist/graph/index.d.ts.map +1 -0
  183. package/dist/graph/index.js +4 -0
  184. package/dist/graph/index.js.map +1 -0
  185. package/dist/index.d.ts +2 -0
  186. package/dist/index.d.ts.map +1 -0
  187. package/dist/index.js +837 -0
  188. package/dist/index.js.map +1 -0
  189. package/dist/intake/index.d.ts +6 -0
  190. package/dist/intake/index.d.ts.map +1 -0
  191. package/dist/intake/index.js +5 -0
  192. package/dist/intake/index.js.map +1 -0
  193. package/dist/intake/intake-processor.d.ts +17 -0
  194. package/dist/intake/intake-processor.d.ts.map +1 -0
  195. package/dist/intake/intake-processor.js +99 -0
  196. package/dist/intake/intake-processor.js.map +1 -0
  197. package/dist/intake/intake-store.d.ts +7 -0
  198. package/dist/intake/intake-store.d.ts.map +1 -0
  199. package/dist/intake/intake-store.js +7 -0
  200. package/dist/intake/intake-store.js.map +1 -0
  201. package/dist/intake/registry-poller.d.ts +41 -0
  202. package/dist/intake/registry-poller.d.ts.map +1 -0
  203. package/dist/intake/registry-poller.js +202 -0
  204. package/dist/intake/registry-poller.js.map +1 -0
  205. package/dist/intake/webhook-handlers.d.ts +37 -0
  206. package/dist/intake/webhook-handlers.d.ts.map +1 -0
  207. package/dist/intake/webhook-handlers.js +268 -0
  208. package/dist/intake/webhook-handlers.js.map +1 -0
  209. package/dist/logger.d.ts +5 -0
  210. package/dist/logger.d.ts.map +1 -0
  211. package/dist/logger.js +15 -0
  212. package/dist/logger.js.map +1 -0
  213. package/dist/mcp/resources.d.ts +9 -0
  214. package/dist/mcp/resources.d.ts.map +1 -0
  215. package/dist/mcp/resources.js +72 -0
  216. package/dist/mcp/resources.js.map +1 -0
  217. package/dist/mcp/server.d.ts +15 -0
  218. package/dist/mcp/server.d.ts.map +1 -0
  219. package/dist/mcp/server.js +20 -0
  220. package/dist/mcp/server.js.map +1 -0
  221. package/dist/mcp/tools.d.ts +9 -0
  222. package/dist/mcp/tools.d.ts.map +1 -0
  223. package/dist/mcp/tools.js +88 -0
  224. package/dist/mcp/tools.js.map +1 -0
  225. package/dist/middleware/auth.d.ts +29 -0
  226. package/dist/middleware/auth.d.ts.map +1 -0
  227. package/dist/middleware/auth.js +76 -0
  228. package/dist/middleware/auth.js.map +1 -0
  229. package/dist/middleware/permissions.d.ts +13 -0
  230. package/dist/middleware/permissions.d.ts.map +1 -0
  231. package/dist/middleware/permissions.js +32 -0
  232. package/dist/middleware/permissions.js.map +1 -0
  233. package/dist/pattern-store.d.ts +104 -0
  234. package/dist/pattern-store.d.ts.map +1 -0
  235. package/dist/pattern-store.js +299 -0
  236. package/dist/pattern-store.js.map +1 -0
  237. package/package.json +54 -0
  238. package/src/agent/debrief-retention.ts +44 -0
  239. package/src/agent/envoy-client.ts +474 -0
  240. package/src/agent/envoy-registry.ts +384 -0
  241. package/src/agent/health-checker.ts +70 -0
  242. package/src/agent/mcp-client-manager.ts +131 -0
  243. package/src/agent/stale-deployment-detector.ts +79 -0
  244. package/src/agent/step-runner.ts +124 -0
  245. package/src/agent/synth-agent.ts +1567 -0
  246. package/src/api/agent.ts +1075 -0
  247. package/src/api/api-keys.ts +129 -0
  248. package/src/api/artifacts.ts +194 -0
  249. package/src/api/auth.ts +320 -0
  250. package/src/api/deployments.ts +1347 -0
  251. package/src/api/environments.ts +97 -0
  252. package/src/api/envoy-reports.ts +159 -0
  253. package/src/api/envoys.ts +237 -0
  254. package/src/api/fleet.ts +510 -0
  255. package/src/api/graph.ts +516 -0
  256. package/src/api/health.ts +311 -0
  257. package/src/api/idp-schemas.ts +19 -0
  258. package/src/api/idp.ts +735 -0
  259. package/src/api/intake.ts +537 -0
  260. package/src/api/partitions.ts +147 -0
  261. package/src/api/progress-event-store.ts +153 -0
  262. package/src/api/schemas.ts +376 -0
  263. package/src/api/security-boundaries.ts +54 -0
  264. package/src/api/settings.ts +118 -0
  265. package/src/api/system.ts +704 -0
  266. package/src/api/telemetry.ts +32 -0
  267. package/src/api/users.ts +210 -0
  268. package/src/archive-unpacker.ts +271 -0
  269. package/src/artifact-analyzer.ts +438 -0
  270. package/src/auth/idp/index.ts +8 -0
  271. package/src/auth/idp/ldap.ts +340 -0
  272. package/src/auth/idp/oidc.ts +117 -0
  273. package/src/auth/idp/role-mapping.ts +22 -0
  274. package/src/auth/idp/saml.ts +148 -0
  275. package/src/auth/idp/types.ts +22 -0
  276. package/src/fleet/fleet-executor.ts +309 -0
  277. package/src/fleet/fleet-store.ts +13 -0
  278. package/src/fleet/index.ts +4 -0
  279. package/src/fleet/representative-selector.ts +83 -0
  280. package/src/graph/graph-executor.ts +446 -0
  281. package/src/graph/graph-inference.ts +184 -0
  282. package/src/graph/graph-store.ts +75 -0
  283. package/src/graph/index.ts +4 -0
  284. package/src/index.ts +916 -0
  285. package/src/intake/index.ts +5 -0
  286. package/src/intake/intake-processor.ts +111 -0
  287. package/src/intake/intake-store.ts +7 -0
  288. package/src/intake/registry-poller.ts +230 -0
  289. package/src/intake/webhook-handlers.ts +328 -0
  290. package/src/logger.ts +19 -0
  291. package/src/mcp/resources.ts +98 -0
  292. package/src/mcp/server.ts +34 -0
  293. package/src/mcp/tools.ts +117 -0
  294. package/src/middleware/auth.ts +103 -0
  295. package/src/middleware/permissions.ts +35 -0
  296. package/src/pattern-store.ts +409 -0
  297. package/tests/agent-mode.test.ts +536 -0
  298. package/tests/api-handlers.test.ts +1245 -0
  299. package/tests/archive-unpacker.test.ts +179 -0
  300. package/tests/artifact-analyzer.test.ts +240 -0
  301. package/tests/auth-middleware.test.ts +189 -0
  302. package/tests/decision-diary.test.ts +957 -0
  303. package/tests/diary-reader.test.ts +782 -0
  304. package/tests/envoy-client.test.ts +342 -0
  305. package/tests/envoy-reports.test.ts +156 -0
  306. package/tests/mcp-tools.test.ts +213 -0
  307. package/tests/orchestration.test.ts +536 -0
  308. package/tests/partition-deletion.test.ts +143 -0
  309. package/tests/partition-isolation.test.ts +830 -0
  310. package/tests/pattern-store.test.ts +371 -0
  311. package/tests/rbac-enforcement.test.ts +409 -0
  312. package/tests/ssrf-validation.test.ts +56 -0
  313. package/tests/stale-deployment.test.ts +85 -0
  314. package/tests/step-runner.test.ts +308 -0
  315. package/tests/ui-journey.test.ts +330 -0
  316. package/tsconfig.json +11 -0
  317. package/vitest.config.ts +27 -0
@@ -0,0 +1,867 @@
1
+ import { z } from "zod";
2
+ import { QueryRequestSchema } from "./schemas.js";
3
+ import { requirePermission } from "../middleware/permissions.js";
4
+ // ---------------------------------------------------------------------------
5
+ // Input sanitization — prevent prompt injection and control character abuse
6
+ // ---------------------------------------------------------------------------
7
+ /** @internal Exported for testing only */
8
+ export function sanitizeUserInput(text) {
9
+ // Strip control characters except newline and tab
10
+ let sanitized = text.replace(/[\x00-\x08\x0B\x0C\x0E-\x1F]/g, '');
11
+ // Truncate to prevent prompt stuffing
12
+ if (sanitized.length > 1000) {
13
+ sanitized = sanitized.slice(0, 1000);
14
+ }
15
+ // Escape angle brackets to prevent XML tag injection
16
+ sanitized = sanitized.replace(/</g, '&lt;').replace(/>/g, '&gt;');
17
+ return sanitized;
18
+ }
19
+ /** @internal Exported for testing only */
20
+ export function validateExtractedVersion(version) {
21
+ // Accept semver and common pre-release formats
22
+ return /^\d+\.\d+\.\d+(-[a-zA-Z0-9._]+)?$/.test(version);
23
+ }
24
+ /** @internal Exported for testing only */
25
+ export function validateExtractedVariables(vars) {
26
+ const validated = {};
27
+ for (const [key, value] of Object.entries(vars)) {
28
+ // Key must be alphanumeric + underscore, value max 500 chars
29
+ if (/^[A-Za-z_][A-Za-z0-9_]*$/.test(key) && typeof value === 'string' && value.length <= 500) {
30
+ validated[key] = value;
31
+ }
32
+ }
33
+ return validated;
34
+ }
35
+ const MAX_ENTITY_LIST_SIZE = 100;
36
+ function appendEntityNames(parts, label, entities, includeEntities) {
37
+ if (!includeEntities) {
38
+ parts.push(`\n${label}: (entity data omitted by configuration)`);
39
+ return;
40
+ }
41
+ parts.push(`\n${label}:`);
42
+ const capped = entities.slice(0, MAX_ENTITY_LIST_SIZE);
43
+ for (const e of capped) {
44
+ parts.push(` - "${e.name}"`);
45
+ }
46
+ if (entities.length > MAX_ENTITY_LIST_SIZE) {
47
+ parts.push(` (… and ${entities.length - MAX_ENTITY_LIST_SIZE} more)`);
48
+ }
49
+ if (entities.length === 0)
50
+ parts.push(" (none configured)");
51
+ }
52
+ /** Build a case-insensitive name→ID map for a list of entities. */
53
+ function buildNameMap(entities) {
54
+ const map = new Map();
55
+ for (const e of entities) {
56
+ const key = e.name.toLowerCase();
57
+ // First match wins — duplicates are inherently ambiguous
58
+ if (!map.has(key))
59
+ map.set(key, e.id);
60
+ }
61
+ return map;
62
+ }
63
+ // ---------------------------------------------------------------------------
64
+ // Context generation — signals from deployment data
65
+ // ---------------------------------------------------------------------------
66
+ function generateContext(deployments, environmentStore, partitionStore) {
67
+ const allDeployments = deployments.list();
68
+ const allEnvironments = environmentStore.list();
69
+ const signals = [];
70
+ // --- Deployment trends ---
71
+ const now = Date.now();
72
+ const last24h = allDeployments.filter((d) => now - new Date(d.createdAt).getTime() < 24 * 60 * 60 * 1000);
73
+ const recentFailed = last24h.filter((d) => d.status === "failed");
74
+ if (recentFailed.length > 0) {
75
+ const rate = Math.round((recentFailed.length / Math.max(last24h.length, 1)) * 100);
76
+ signals.push({
77
+ type: "trend",
78
+ severity: rate > 50 ? "critical" : "warning",
79
+ title: `${recentFailed.length} failed deployment${recentFailed.length > 1 ? "s" : ""} in last 24h`,
80
+ detail: `${rate}% failure rate across ${last24h.length} recent deployments`,
81
+ });
82
+ }
83
+ if (last24h.length === 0 && allDeployments.length > 0) {
84
+ signals.push({
85
+ type: "trend",
86
+ severity: "info",
87
+ title: "No deployments in last 24 hours",
88
+ detail: `Last deployment was ${allDeployments.length > 0 ? formatAgo(new Date(allDeployments[allDeployments.length - 1].createdAt)) : "never"}`,
89
+ });
90
+ }
91
+ // --- Environment health signals ---
92
+ for (const env of allEnvironments) {
93
+ const envDeployments = allDeployments
94
+ .filter((d) => d.environmentId === env.id)
95
+ .sort((a, b) => new Date(b.createdAt).getTime() - new Date(a.createdAt).getTime());
96
+ if (envDeployments.length > 0 && envDeployments[0].status === "failed") {
97
+ signals.push({
98
+ type: "health",
99
+ severity: "warning",
100
+ title: `Last deployment to ${env.name} failed`,
101
+ detail: envDeployments[0].failureReason ?? "Unknown failure",
102
+ relatedEntity: { type: "environment", id: env.id, name: env.name },
103
+ });
104
+ }
105
+ // Consecutive failures
106
+ const consecutiveFails = envDeployments.filter((d, i) => {
107
+ if (i > 2)
108
+ return false;
109
+ return d.status === "failed";
110
+ }).length;
111
+ if (consecutiveFails >= 2) {
112
+ signals.push({
113
+ type: "health",
114
+ severity: "critical",
115
+ title: `${env.name}: ${consecutiveFails} consecutive failures`,
116
+ detail: `Environment may have an infrastructure issue. Last ${consecutiveFails} deployments all failed.`,
117
+ relatedEntity: { type: "environment", id: env.id, name: env.name },
118
+ });
119
+ }
120
+ }
121
+ // --- Configuration drift warnings ---
122
+ const partitions = partitionStore.list();
123
+ for (const partition of partitions) {
124
+ for (const env of allEnvironments) {
125
+ const conflicts = detectDrift(partition, env);
126
+ if (conflicts.length > 0) {
127
+ signals.push({
128
+ type: "drift",
129
+ severity: "warning",
130
+ title: `Config drift: ${partition.name} / ${env.name}`,
131
+ detail: `${conflicts.length} variable${conflicts.length > 1 ? "s" : ""} may conflict: ${conflicts.join(", ")}`,
132
+ relatedEntity: { type: "partition", id: partition.id, name: partition.name },
133
+ });
134
+ }
135
+ }
136
+ }
137
+ // --- Recent activity summary ---
138
+ const sorted = [...allDeployments].sort((a, b) => new Date(b.createdAt).getTime() - new Date(a.createdAt).getTime());
139
+ const lastDeploy = sorted[0];
140
+ const succeeded = allDeployments.filter((d) => d.status === "succeeded").length;
141
+ const environmentSummary = allEnvironments.map((env) => {
142
+ const envDeploys = allDeployments.filter((d) => d.environmentId === env.id);
143
+ const lastEnvDeploy = envDeploys.sort((a, b) => new Date(b.createdAt).getTime() - new Date(a.createdAt).getTime())[0];
144
+ return {
145
+ id: env.id,
146
+ name: env.name,
147
+ lastDeployStatus: lastEnvDeploy?.status ?? null,
148
+ deployCount: envDeploys.length,
149
+ variableCount: Object.keys(env.variables).length,
150
+ };
151
+ });
152
+ return {
153
+ signals,
154
+ recentActivity: {
155
+ deploymentsLast24h: last24h.length,
156
+ successRate: allDeployments.length > 0
157
+ ? `${Math.round((succeeded / allDeployments.length) * 100)}%`
158
+ : "—",
159
+ lastDeployment: lastDeploy
160
+ ? {
161
+ version: lastDeploy.version,
162
+ environment: allEnvironments.find((e) => e.id === lastDeploy.environmentId)?.name ?? lastDeploy.environmentId ?? "—",
163
+ status: lastDeploy.status,
164
+ ago: formatAgo(new Date(lastDeploy.createdAt)),
165
+ }
166
+ : null,
167
+ },
168
+ environmentSummary,
169
+ };
170
+ }
171
+ function detectDrift(partition, environment) {
172
+ const conflicts = [];
173
+ const envPatterns = {
174
+ production: [/\bstag/i, /\bdev\b/i],
175
+ staging: [/\bprod/i],
176
+ development: [/\bprod/i, /\bstag/i],
177
+ };
178
+ const patternsToCheck = envPatterns[environment.name.toLowerCase()];
179
+ if (!patternsToCheck)
180
+ return conflicts;
181
+ for (const [key, value] of Object.entries(partition.variables)) {
182
+ if (patternsToCheck.some((p) => p.test(value))) {
183
+ conflicts.push(key);
184
+ }
185
+ }
186
+ return conflicts;
187
+ }
188
+ function formatAgo(date) {
189
+ const ms = Date.now() - date.getTime();
190
+ const seconds = Math.floor(ms / 1000);
191
+ if (seconds < 60)
192
+ return `${seconds}s ago`;
193
+ const minutes = Math.floor(seconds / 60);
194
+ if (minutes < 60)
195
+ return `${minutes}m ago`;
196
+ const hours = Math.floor(minutes / 60);
197
+ if (hours < 24)
198
+ return `${hours}h ago`;
199
+ const days = Math.floor(hours / 24);
200
+ return `${days}d ago`;
201
+ }
202
+ // ---------------------------------------------------------------------------
203
+ // Route registration
204
+ // ---------------------------------------------------------------------------
205
+ export function registerAgentRoutes(app, agent, partitions, environments, artifacts, deployments, debrief, settings, llm, envoyRegistry, telemetry, analyzer) {
206
+ /**
207
+ * Get deployment context — signals, trends, health, drift.
208
+ * Fills the space where manual action buttons collapse.
209
+ */
210
+ app.get("/api/agent/context", { preHandler: [requirePermission("deployment.view")] }, async () => {
211
+ return generateContext(deployments, environments, partitions);
212
+ });
213
+ /**
214
+ * Canvas query — classifies a natural language query and returns
215
+ * a structured action telling the UI what view to render.
216
+ * Navigation/data intents resolve entities and return view params.
217
+ */
218
+ app.post("/api/agent/query", { preHandler: [requirePermission("deployment.view")] }, async (request, reply) => {
219
+ const parsed = QueryRequestSchema.safeParse(request.body);
220
+ if (!parsed.success) {
221
+ return reply.status(400).send({ error: "Invalid input", details: parsed.error.format() });
222
+ }
223
+ const query = parsed.data.query.trim();
224
+ const lower = query.toLowerCase();
225
+ const allArtifacts = artifacts.list();
226
+ const allPartitions = partitions.list();
227
+ const allEnvironments = environments.list();
228
+ const artifactMap = new Map(allArtifacts.map((a) => [a.id, a.name]));
229
+ const environmentMap = new Map(allEnvironments.map((e) => [e.id, e.name]));
230
+ const partitionMap = new Map(allPartitions.map((p) => [p.id, p.name]));
231
+ // --- LLM classification (when available) ---
232
+ const queryEntityExposure = settings.get().agent.llmEntityExposure ?? "names";
233
+ if (llm && llm.isAvailable()) {
234
+ const llmAction = await classifyQueryWithLlm(llm, query, allArtifacts, allPartitions, allEnvironments, deployments, debrief, queryEntityExposure !== "none");
235
+ if (llmAction) {
236
+ // For "annotate" action: save annotation to artifact, trigger re-analysis
237
+ if (llmAction.action === "annotate") {
238
+ const { artifactName, field, correction } = llmAction.params;
239
+ const target = allArtifacts.find((a) => a.name.toLowerCase() === (artifactName ?? "").toLowerCase());
240
+ if (target && correction) {
241
+ artifacts.addAnnotation(target.id, {
242
+ field: field || "summary",
243
+ correction,
244
+ annotatedBy: "channel",
245
+ annotatedAt: new Date(),
246
+ });
247
+ debrief.record({
248
+ partitionId: null,
249
+ deploymentId: null,
250
+ agent: "server",
251
+ decisionType: "artifact-analysis",
252
+ decision: `User correction recorded for "${target.name}" via channel: ${correction}`,
253
+ reasoning: `Operator typed a natural-language correction into the Synth Channel. Field: ${field || "summary"}.`,
254
+ context: { artifactName: target.name, field: field || "summary", correction, source: "channel" },
255
+ });
256
+ // Trigger async re-analysis with the new annotation
257
+ if (analyzer) {
258
+ const updated = artifacts.get(target.id);
259
+ if (updated) {
260
+ analyzer.reanalyzeWithAnnotations(updated).then((revised) => {
261
+ if (revised)
262
+ artifacts.update(target.id, { analysis: revised });
263
+ }).catch(() => { });
264
+ }
265
+ }
266
+ return {
267
+ action: "answer",
268
+ view: "",
269
+ params: {},
270
+ title: "Correction recorded",
271
+ content: `Got it — I've noted that **${target.name}** ${correction}. Re-analyzing now to update my understanding.`,
272
+ };
273
+ }
274
+ // Artifact not found — fall through to answer
275
+ }
276
+ // For "answer" action: fetch real data and generate a markdown response
277
+ if (llmAction.action === "answer") {
278
+ const answered = await answerQueryWithData(llm, query, deployments.list(), allArtifacts, allPartitions, allEnvironments);
279
+ if (answered) {
280
+ debrief.record({
281
+ partitionId: null,
282
+ deploymentId: null,
283
+ agent: "server",
284
+ decisionType: "system",
285
+ decision: `Canvas query answered analytically`,
286
+ reasoning: `LLM classified "${query}" as analytical answer, responded with ${answered.content.length} chars of markdown`,
287
+ context: { query, action: "answer" },
288
+ });
289
+ return answered;
290
+ }
291
+ // Fall through to regex fallback if answer generation failed
292
+ }
293
+ else {
294
+ debrief.record({
295
+ partitionId: null,
296
+ deploymentId: null,
297
+ agent: "server",
298
+ decisionType: "system",
299
+ decision: `Canvas query classified as ${llmAction.action}: ${llmAction.view}`,
300
+ reasoning: `LLM classified "${query}" → ${llmAction.action}/${llmAction.view}`,
301
+ context: { query, action: llmAction },
302
+ });
303
+ return llmAction;
304
+ }
305
+ }
306
+ }
307
+ // --- Regex fallback classification ---
308
+ // Artifact correction: "Dockerfile.server is actually for nginx", "api-service is a nodejs app"
309
+ const correctionPatterns = [
310
+ /\b(?:is\s+actually|is\s+really|should\s+be|is\s+a|is\s+an)\b/i,
311
+ /\bcorrect(?:ion)?:/i,
312
+ /\bactually\s+(?:a|an|the)\b/i,
313
+ ];
314
+ if (correctionPatterns.some((p) => p.test(query))) {
315
+ for (const art of allArtifacts) {
316
+ if (query.toLowerCase().includes(art.name.toLowerCase())) {
317
+ artifacts.addAnnotation(art.id, {
318
+ field: "summary",
319
+ correction: query,
320
+ annotatedBy: "channel",
321
+ annotatedAt: new Date(),
322
+ });
323
+ if (analyzer) {
324
+ const updated = artifacts.get(art.id);
325
+ if (updated) {
326
+ analyzer.reanalyzeWithAnnotations(updated).then((revised) => {
327
+ if (revised)
328
+ artifacts.update(art.id, { analysis: revised });
329
+ }).catch(() => { });
330
+ }
331
+ }
332
+ return {
333
+ action: "answer",
334
+ view: "",
335
+ params: {},
336
+ title: "Correction recorded",
337
+ content: `Got it — I've noted your correction about **${art.name}**. Re-analyzing now.`,
338
+ };
339
+ }
340
+ }
341
+ }
342
+ // Create partition: "create partition Acme Corp" → return create intent for UI confirmation
343
+ const createPartitionMatch = query.match(/\bcreate\s+partition\s+(.+)/i);
344
+ if (createPartitionMatch) {
345
+ const name = createPartitionMatch[1].trim();
346
+ return { action: "create", view: "partition-detail", params: { name }, title: `Create "${name}"` };
347
+ }
348
+ // Create artifact: "create artifact api-service" or "create operation api-service" → return create intent for UI confirmation
349
+ const createArtifactMatch = query.match(/\bcreate\s+(?:artifact|operation)\s+(.+)/i);
350
+ if (createArtifactMatch) {
351
+ const name = createArtifactMatch[1].trim();
352
+ return { action: "create", view: "artifact-list", params: { name }, title: `Create "${name}"` };
353
+ }
354
+ // Show specific partition
355
+ for (const p of allPartitions) {
356
+ const name = p.name.toLowerCase();
357
+ if (lower.includes(name) && (lower.includes("partition") || lower.includes("show"))) {
358
+ return { action: "navigate", view: "partition-detail", params: { id: p.id }, title: p.name };
359
+ }
360
+ }
361
+ // Show specific environment
362
+ for (const e of allEnvironments) {
363
+ const name = e.name.toLowerCase();
364
+ if (lower.includes(name) && (lower.includes("environment") || lower.includes("env"))) {
365
+ return { action: "navigate", view: "environment-detail", params: { id: e.id }, title: e.name };
366
+ }
367
+ }
368
+ // Show specific deployment by ID
369
+ const deployIdMatch = lower.match(/(?:deployment|deploy)\s+([a-f0-9-]{36})/);
370
+ if (deployIdMatch) {
371
+ return { action: "navigate", view: "deployment-detail", params: { id: deployIdMatch[1] }, title: "Deployment" };
372
+ }
373
+ // Failed deployments / what failed → markdown table
374
+ if (/\b(fail|failed|failures|what failed|broken)\b/.test(lower)) {
375
+ const failed = deployments.list().filter((d) => d.status === "failed")
376
+ .sort((a, b) => new Date(b.createdAt).getTime() - new Date(a.createdAt).getTime());
377
+ const content = buildDeploymentTable(failed, artifactMap, environmentMap, partitionMap);
378
+ return { action: "answer", view: "", params: {}, title: "Failed Deployments", content };
379
+ }
380
+ // Settings / configuration
381
+ if (/\b(settings|preferences|configure)\b/.test(lower) || (lower.includes("config") && !/\bconfiguration-resolved\b/.test(lower))) {
382
+ return { action: "navigate", view: "settings", params: {}, title: "Settings" };
383
+ }
384
+ // Artifacts list → markdown table
385
+ if (/\b(artifacts|artifact list|operations|operation list|manage artifacts)\b/.test(lower)) {
386
+ const rows = allArtifacts.map((a) => `| ${a.name} | ${a.type} |`).join("\n");
387
+ const content = allArtifacts.length > 0
388
+ ? `| Artifact | Type |\n|----------|------|\n${rows}`
389
+ : "_No artifacts configured._";
390
+ return { action: "answer", view: "", params: {}, title: "Artifacts", content };
391
+ }
392
+ // Debrief / decision diary
393
+ if (/\b(debrief|decision diary|decisions|decision log|decision history)\b/.test(lower)) {
394
+ return { action: "navigate", view: "debrief", params: {}, title: "Debrief" };
395
+ }
396
+ // Deployment history / recent deployments → markdown table
397
+ if (/\b(deployment|history|recent|deployments)\b/.test(lower)) {
398
+ let deps = deployments.list()
399
+ .sort((a, b) => new Date(b.createdAt).getTime() - new Date(a.createdAt).getTime());
400
+ // Scope to a specific artifact if mentioned
401
+ for (const a of allArtifacts) {
402
+ if (lower.includes(a.name.toLowerCase())) {
403
+ deps = deps.filter((d) => d.artifactId === a.id);
404
+ break;
405
+ }
406
+ }
407
+ const content = buildDeploymentTable(deps, artifactMap, environmentMap, partitionMap);
408
+ const title = deps.length < deployments.list().length ? "Deployment History" : "Recent Deployments";
409
+ return { action: "answer", view: "", params: {}, title, content };
410
+ }
411
+ // Signals / drift / health
412
+ if (/\b(signal|signals|drift|health|alert|alerts)\b/.test(lower)) {
413
+ return { action: "navigate", view: "overview", params: { focus: "signals" }, title: "Signals" };
414
+ }
415
+ // Show all partitions
416
+ if (/\b(partitions|all partitions|partition list|manage partitions)\b/.test(lower)) {
417
+ return { action: "navigate", view: "partition-list", params: {}, title: "Partitions" };
418
+ }
419
+ // Fallback: navigate to overview
420
+ return { action: "navigate", view: "overview", params: {}, title: "Overview" };
421
+ });
422
+ // -------------------------------------------------------------------------
423
+ // Pre-flight context — deterministic data + LLM editorialization
424
+ // -------------------------------------------------------------------------
425
+ const PreFlightRequestSchema = z.object({
426
+ artifactId: z.string().min(1),
427
+ environmentId: z.string().min(1),
428
+ partitionId: z.string().optional(),
429
+ version: z.string().optional(),
430
+ });
431
+ app.post("/api/agent/pre-flight", { preHandler: [requirePermission("deployment.view")] }, async (request, reply) => {
432
+ const parsed = PreFlightRequestSchema.safeParse(request.body);
433
+ if (!parsed.success) {
434
+ return reply.status(400).send({ error: "Invalid input", details: parsed.error.format() });
435
+ }
436
+ const { artifactId, environmentId, partitionId, version } = parsed.data;
437
+ // --- 1. Target health: check envoy health for the environment ---
438
+ let targetHealth = {
439
+ status: "healthy",
440
+ details: "No envoys registered — health check not applicable",
441
+ };
442
+ if (envoyRegistry) {
443
+ const envName = environments.get(environmentId)?.name ?? environmentId;
444
+ const envoy = envoyRegistry.findForEnvironment(envName);
445
+ if (envoy) {
446
+ const healthStatus = envoy.lastHealthStatus;
447
+ if (healthStatus === "healthy") {
448
+ targetHealth = { status: "healthy", details: `Envoy "${envoy.name}" is healthy` };
449
+ }
450
+ else if (healthStatus === "degraded") {
451
+ targetHealth = { status: "degraded", details: `Envoy "${envoy.name}" is degraded` };
452
+ }
453
+ else if (healthStatus === "unreachable") {
454
+ targetHealth = { status: "unreachable", details: `Envoy "${envoy.name}" is unreachable` };
455
+ }
456
+ else {
457
+ targetHealth = { status: "healthy", details: `Envoy "${envoy.name}" registered, health not yet checked` };
458
+ }
459
+ }
460
+ }
461
+ // --- 2. Recent history ---
462
+ const now = new Date();
463
+ const twentyFourHoursAgo = new Date(now.getTime() - 24 * 60 * 60 * 1000);
464
+ const sevenDaysAgo = new Date(now.getTime() - 7 * 24 * 60 * 60 * 1000);
465
+ const latestToEnv = deployments.findLatestByEnvironment(environmentId);
466
+ const deploymentsToday = deployments.countByEnvironment(environmentId, twentyFourHoursAgo);
467
+ const recentArtifactDeploys = deployments.findRecentByArtifact(artifactId, sevenDaysAgo);
468
+ const recentFailures = recentArtifactDeploys.filter((d) => d.status === "failed").length;
469
+ const recentHistory = {
470
+ lastDeployment: latestToEnv
471
+ ? {
472
+ status: latestToEnv.status,
473
+ completedAt: (latestToEnv.completedAt ?? latestToEnv.createdAt).toISOString(),
474
+ version: latestToEnv.version,
475
+ }
476
+ : undefined,
477
+ recentFailures,
478
+ deploymentsToday,
479
+ };
480
+ // --- 3. Cross-system context queries ---
481
+ const crossSystemContext = [];
482
+ // Check if this version was rolled back anywhere
483
+ if (version) {
484
+ const rolledBack = deployments.findByArtifactVersion(artifactId, version, "rolled_back");
485
+ if (rolledBack.length > 0) {
486
+ const envNames = rolledBack.map((d) => environments.get(d.environmentId ?? "")?.name ?? d.environmentId ?? "unknown");
487
+ crossSystemContext.push(`This version (${version}) was rolled back from ${envNames.join(", ")} previously`);
488
+ }
489
+ const failed = deployments.findByArtifactVersion(artifactId, version, "failed");
490
+ if (failed.length > 0) {
491
+ const envNames = failed.map((d) => environments.get(d.environmentId ?? "")?.name ?? d.environmentId ?? "unknown");
492
+ crossSystemContext.push(`This version (${version}) failed deployment to ${envNames.join(", ")}`);
493
+ }
494
+ }
495
+ // Check recent failure patterns for this artifact
496
+ if (recentFailures > 2) {
497
+ crossSystemContext.push(`${recentFailures} failed deployments for this artifact in the last 7 days — investigate before proceeding`);
498
+ }
499
+ // Check if the last deployment to this environment failed
500
+ if (latestToEnv && latestToEnv.status === "failed") {
501
+ crossSystemContext.push(`The last deployment to this environment failed (${latestToEnv.failureReason ?? "unknown reason"})`);
502
+ }
503
+ // Check deployment volume
504
+ if (deploymentsToday >= 5) {
505
+ crossSystemContext.push(`High deployment volume: ${deploymentsToday} deployments to this environment in the last 24 hours`);
506
+ }
507
+ // --- 4. LLM recommendation ---
508
+ let recommendation = {
509
+ action: "proceed",
510
+ reasoning: "Agent recommendation unavailable — review the context above and decide.",
511
+ confidence: 0,
512
+ };
513
+ let llmAvailable = false;
514
+ if (llm && llm.isAvailable()) {
515
+ const artifactName = artifacts.get(artifactId)?.name ?? artifactId;
516
+ const envName = environments.get(environmentId)?.name ?? environmentId;
517
+ const partitionName = partitionId ? (partitions.get(partitionId)?.name ?? partitionId) : null;
518
+ const promptParts = [
519
+ `You are an intelligent deployment advisor. Analyze the following pre-flight context and provide a directional recommendation.`,
520
+ `\nArtifact: ${artifactName}`,
521
+ `Target environment: ${envName}`,
522
+ partitionName ? `Partition: ${partitionName}` : null,
523
+ version ? `Version: ${version}` : null,
524
+ `\nTarget health: ${targetHealth.status} — ${targetHealth.details}`,
525
+ `\nRecent history:`,
526
+ ` Deployments to this environment in last 24h: ${deploymentsToday}`,
527
+ ` Recent failures for this artifact (7d): ${recentFailures}`,
528
+ latestToEnv
529
+ ? ` Last deployment to this env: ${latestToEnv.status} (${latestToEnv.version}, ${formatAgo(latestToEnv.completedAt ?? latestToEnv.createdAt)})`
530
+ : ` No previous deployments to this environment`,
531
+ crossSystemContext.length > 0
532
+ ? `\nCross-system observations:\n${crossSystemContext.map((c) => ` - ${c}`).join("\n")}`
533
+ : `\nNo cross-system concerns detected.`,
534
+ ].filter(Boolean);
535
+ const systemPrompt = `You are a deployment advisor for Synth. Given pre-flight context, you MUST respond with ONLY a JSON object (no markdown, no explanation) with this schema:
536
+ {
537
+ "action": "proceed" | "wait" | "investigate",
538
+ "reasoning": "<1-2 sentences, directional — 'I recommend proceeding' / 'I'd wait' / 'Investigate first' style>",
539
+ "confidence": <0-1 number>
540
+ }
541
+
542
+ Be directional: say what you recommend, not "here are some data points." Use first person. Be specific.`;
543
+ try {
544
+ const timeout = new Promise((_, reject) => setTimeout(() => reject(new Error("Pre-flight LLM timeout (15s)")), 15000));
545
+ const llmResult = await Promise.race([
546
+ llm.classify({
547
+ prompt: promptParts.join("\n"),
548
+ systemPrompt,
549
+ promptSummary: `Pre-flight recommendation for ${artifactName} → ${envName}`,
550
+ partitionId: partitionId ?? null,
551
+ maxTokens: 512,
552
+ }),
553
+ timeout,
554
+ ]);
555
+ if (llmResult.ok) {
556
+ try {
557
+ let text = llmResult.text.trim();
558
+ if (text.startsWith("```")) {
559
+ text = text.replace(/^```(?:json)?\n?/, "").replace(/\n?```$/, "");
560
+ }
561
+ const parsed = JSON.parse(text);
562
+ if (parsed.action && parsed.reasoning && typeof parsed.confidence === "number") {
563
+ recommendation = {
564
+ action: parsed.action,
565
+ reasoning: parsed.reasoning,
566
+ confidence: Math.max(0, Math.min(1, parsed.confidence)),
567
+ };
568
+ llmAvailable = true;
569
+ }
570
+ }
571
+ catch {
572
+ // JSON parse failed — use deterministic fallback
573
+ }
574
+ }
575
+ }
576
+ catch (llmError) {
577
+ // LLM call failed or timed out — record to debrief and use deterministic fallback
578
+ debrief.record({
579
+ partitionId: partitionId ?? null,
580
+ deploymentId: null,
581
+ agent: "server",
582
+ decisionType: "pre-flight-llm-failure",
583
+ decision: "Pre-flight LLM recommendation failed",
584
+ reasoning: llmError instanceof Error ? llmError.message : String(llmError),
585
+ context: { artifactId, environmentId, partitionId: partitionId ?? null },
586
+ });
587
+ }
588
+ }
589
+ // --- 5. Deterministic fallback recommendation if LLM was unavailable ---
590
+ if (!llmAvailable) {
591
+ if (targetHealth.status === "unreachable") {
592
+ recommendation = {
593
+ action: "investigate",
594
+ reasoning: "The target envoy is unreachable. Investigate infrastructure health before deploying.",
595
+ confidence: 0,
596
+ };
597
+ }
598
+ else if (recentFailures > 2 || (latestToEnv && latestToEnv.status === "failed")) {
599
+ recommendation = {
600
+ action: "investigate",
601
+ reasoning: "Recent failures detected. Review the failure history before proceeding.",
602
+ confidence: 0,
603
+ };
604
+ }
605
+ else if (targetHealth.status === "degraded") {
606
+ recommendation = {
607
+ action: "wait",
608
+ reasoning: "The target envoy is degraded. Consider waiting for it to stabilize.",
609
+ confidence: 0,
610
+ };
611
+ }
612
+ }
613
+ const result = {
614
+ targetHealth,
615
+ recentHistory,
616
+ crossSystemContext,
617
+ recommendation,
618
+ llmAvailable,
619
+ };
620
+ // --- 6. Debrief + telemetry ---
621
+ debrief.record({
622
+ partitionId: partitionId ?? null,
623
+ deploymentId: null,
624
+ agent: "server",
625
+ decisionType: "cross-system-context",
626
+ decision: `Pre-flight context generated: ${recommendation.action} (confidence: ${recommendation.confidence})`,
627
+ reasoning: recommendation.reasoning,
628
+ context: {
629
+ artifactId,
630
+ environmentId,
631
+ partitionId: partitionId ?? null,
632
+ version: version ?? null,
633
+ targetHealth: targetHealth.status,
634
+ recentFailures,
635
+ deploymentsToday,
636
+ crossSystemSignals: crossSystemContext.length,
637
+ llmAvailable,
638
+ },
639
+ });
640
+ if (telemetry) {
641
+ telemetry.record({
642
+ actor: "agent",
643
+ action: "agent.pre-flight.generated",
644
+ target: { type: "deployment", id: `${artifactId}:${environmentId}` },
645
+ details: {
646
+ recommendation: recommendation.action,
647
+ confidence: recommendation.confidence,
648
+ llmAvailable,
649
+ },
650
+ });
651
+ }
652
+ return result;
653
+ });
654
+ // -------------------------------------------------------------------------
655
+ // Pre-flight user response — records what the user did after seeing context
656
+ // -------------------------------------------------------------------------
657
+ const PreFlightResponseSchema = z.object({
658
+ artifactId: z.string().min(1),
659
+ environmentId: z.string().min(1),
660
+ partitionId: z.string().optional(),
661
+ action: z.enum(["proceeded", "waited", "canceled"]),
662
+ recommendedAction: z.enum(["proceed", "wait", "investigate"]),
663
+ });
664
+ app.post("/api/agent/pre-flight/response", { preHandler: [requirePermission("deployment.view")] }, async (request, reply) => {
665
+ const parsed = PreFlightResponseSchema.safeParse(request.body);
666
+ if (!parsed.success) {
667
+ return reply.status(400).send({ error: "Invalid input", details: parsed.error.format() });
668
+ }
669
+ const { artifactId, environmentId, partitionId, action, recommendedAction } = parsed.data;
670
+ debrief.record({
671
+ partitionId: partitionId ?? null,
672
+ deploymentId: null,
673
+ agent: "server",
674
+ decisionType: "cross-system-context",
675
+ decision: `User ${action} after pre-flight recommendation to ${recommendedAction}`,
676
+ reasoning: `System recommended "${recommendedAction}", user chose to "${action}".`,
677
+ context: { artifactId, environmentId, partitionId: partitionId ?? null, recommendedAction, userAction: action },
678
+ });
679
+ return { ok: true };
680
+ });
681
+ }
682
+ // ---------------------------------------------------------------------------
683
+ // Deterministic markdown table builders (used in regex fallback)
684
+ // ---------------------------------------------------------------------------
685
+ function buildDeploymentTable(deps, artifactMap, environmentMap, partitionMap) {
686
+ if (deps.length === 0)
687
+ return "_No matching deployments found._";
688
+ const rows = deps
689
+ .slice(0, 50)
690
+ .map((d) => {
691
+ const art = artifactMap.get(d.artifactId) ?? d.artifactId;
692
+ const env = (d.environmentId ? environmentMap.get(d.environmentId) : undefined) ?? d.environmentId ?? "—";
693
+ const part = d.partitionId ? (partitionMap.get(d.partitionId) ?? d.partitionId) : "—";
694
+ const date = new Date(d.createdAt).toLocaleString();
695
+ // Embed a synth:// deep-link so the UI can navigate to the deployment detail
696
+ const versionLink = `[v${d.version}](synth://deployment-detail?id=${d.id})`;
697
+ return `| ${d.status} | ${art} | ${versionLink} | ${env} | ${part} | ${date} |`;
698
+ })
699
+ .join("\n");
700
+ return [
701
+ "| Status | Artifact | Version | Environment | Partition | Date |",
702
+ "|--------|----------|---------|-------------|-----------|------|",
703
+ rows,
704
+ ].join("\n");
705
+ }
706
+ // ---------------------------------------------------------------------------
707
+ // LLM-powered query classification
708
+ // ---------------------------------------------------------------------------
709
+ function buildQueryClassificationPrompt() {
710
+ return `You are a query classifier for Synth's agent canvas. Given a natural language query from a deployment engineer, classify it into one of these actions:
711
+
712
+ 1. "navigate" — The user wants to drill into a specific named entity (e.g., "show partition Alpha", "open environment staging", "view deployment abc-123"). Only use this when there is a specific entity to navigate to.
713
+ 2. "create" — The user wants to create a new entity (e.g., "create partition Acme Corp", "create operation api-service")
714
+ 3. "answer" — Use this for EVERYTHING ELSE: data requests, lists, filters, analysis, comparisons, summaries. Examples: "show me failed deployments", "what failed", "recent deployments", "how many succeeded last week", "give me all deployments for api-service", "compare environments", "summarize activity". The response will be rendered as a formatted markdown table or narrative — NOT a navigation panel.
715
+ 4. "annotate" — The user is providing a correction or clarification about a specific artifact. Examples: "Dockerfile.server is actually for nginx", "that artifact is a Node.js app not a docker image", "the api-service type should be nodejs", "correct: Dockerfile.envoy is the load balancer". Use this when the user is teaching Synth something about an artifact.
716
+
717
+ Return a JSON object with this exact schema:
718
+ {
719
+ "action": "navigate" | "create" | "answer" | "annotate",
720
+ "view": "<view-name or empty string for answer/create/annotate>",
721
+ "params": { ... },
722
+ "title": "<human-readable title, e.g. 'Failed Deployments' or 'Deployment History'>"
723
+ }
724
+
725
+ View names (for navigate only):
726
+ - "partition-detail" — show specific partition (params: { "id": "<partition-name>" })
727
+ - "environment-detail" — show specific environment (params: { "id": "<environment-name>" })
728
+ - "deployment-detail" — show specific deployment (params: { "id": "<deployment-id>" })
729
+ - "overview" — show the operational overview (params: {})
730
+ - "settings" — show application settings (params: {})
731
+
732
+ For "annotate" actions, set view to "" and params to:
733
+ { "artifactName": "<exact artifact name from the known artifacts list>", "field": "summary|type|deploymentIntent", "correction": "<the user's correction in their own words>" }
734
+
735
+ Rules:
736
+ - ONLY use entity names from the provided lists. Never invent names.
737
+ - If the query is a data/list/filter request, ALWAYS use "answer" — never "navigate".
738
+ - If the query is ambiguous between navigation and data, prefer "answer".
739
+ - For "create" actions, include the entity name in params: { "name": "..." }.
740
+ - For "answer" and "create" actions, set view to "" and params to {}.
741
+ - For "annotate", artifactName MUST match an artifact from the known list exactly.
742
+ - Return ONLY valid JSON, no markdown, no explanation.`;
743
+ }
744
+ async function classifyQueryWithLlm(llm, query, allArtifacts, allPartitions, allEnvironments, deploymentStore, _debrief, includeEntities) {
745
+ const parts = [`<user-query>${sanitizeUserInput(query)}</user-query>`];
746
+ appendEntityNames(parts, "Known partitions", allPartitions, includeEntities);
747
+ appendEntityNames(parts, "Known environments", allEnvironments, includeEntities);
748
+ appendEntityNames(parts, "Known artifacts", allArtifacts, includeEntities);
749
+ const llmResult = await llm.classify({
750
+ prompt: parts.join("\n"),
751
+ systemPrompt: buildQueryClassificationPrompt(),
752
+ promptSummary: `Canvas query classification: "${query}"`,
753
+ partitionId: null,
754
+ maxTokens: 512,
755
+ });
756
+ if (!llmResult.ok)
757
+ return null;
758
+ try {
759
+ let text = llmResult.text.trim();
760
+ if (text.startsWith("```")) {
761
+ text = text.replace(/^```(?:json)?\n?/, "").replace(/\n?```$/, "");
762
+ }
763
+ const parsed = JSON.parse(text);
764
+ if (!parsed.action || !parsed.view)
765
+ return null;
766
+ // Build name→ID maps for local resolution
767
+ const partitionNameMap = buildNameMap(allPartitions);
768
+ const environmentNameMap = buildNameMap(allEnvironments);
769
+ // The LLM now returns names in params — resolve to IDs locally
770
+ if (parsed.params?.id) {
771
+ const idLower = parsed.params.id.toLowerCase();
772
+ if (parsed.view === "partition-detail") {
773
+ const resolvedId = partitionNameMap.get(idLower);
774
+ if (!resolvedId)
775
+ return null;
776
+ parsed.params.id = resolvedId;
777
+ }
778
+ else if (parsed.view === "environment-detail") {
779
+ const resolvedId = environmentNameMap.get(idLower);
780
+ if (!resolvedId)
781
+ return null;
782
+ parsed.params.id = resolvedId;
783
+ }
784
+ }
785
+ if (parsed.params?.partitionId) {
786
+ const resolvedId = partitionNameMap.get(parsed.params.partitionId.toLowerCase());
787
+ if (!resolvedId) {
788
+ delete parsed.params.partitionId;
789
+ }
790
+ else {
791
+ parsed.params.partitionId = resolvedId;
792
+ }
793
+ }
794
+ return parsed;
795
+ }
796
+ catch {
797
+ return null;
798
+ }
799
+ }
800
+ // ---------------------------------------------------------------------------
801
+ // LLM-powered analytical answer with real DB data
802
+ // ---------------------------------------------------------------------------
803
+ async function answerQueryWithData(llm, query, allDeployments, allArtifacts, allPartitions, allEnvironments) {
804
+ const now = Date.now();
805
+ // Build a concise data context from real records (last 50 deployments)
806
+ const recentDeployments = [...allDeployments]
807
+ .sort((a, b) => new Date(b.createdAt).getTime() - new Date(a.createdAt).getTime())
808
+ .slice(0, 50);
809
+ const artifactMap = new Map(allArtifacts.map((a) => [a.id, a.name]));
810
+ const environmentMap = new Map(allEnvironments.map((e) => [e.id, e.name]));
811
+ const partitionMap = new Map(allPartitions.map((p) => [p.id, p.name]));
812
+ const deploymentRows = recentDeployments.map((d) => {
813
+ const ageMs = now - new Date(d.createdAt).getTime();
814
+ const ageHours = Math.round(ageMs / (1000 * 60 * 60));
815
+ const age = ageHours < 24 ? `${ageHours}h ago` : `${Math.round(ageHours / 24)}d ago`;
816
+ const art = artifactMap.get(d.artifactId) ?? d.artifactId;
817
+ const env = (d.environmentId ? environmentMap.get(d.environmentId) : undefined) ?? d.environmentId ?? "—";
818
+ const part = d.partitionId ? ` (${partitionMap.get(d.partitionId) ?? d.partitionId})` : "";
819
+ // Include synth:// deep-link for UI navigation
820
+ return `- id:${d.id} | ${art} v${d.version} → ${env}${part}: ${d.status} (${age})`;
821
+ }).join("\n");
822
+ const artifactRows = allArtifacts.map((a) => `- ${a.name} (${a.type})`).join("\n");
823
+ const partitionRows = allPartitions.map((p) => `- ${p.name}`).join("\n");
824
+ const environmentRows = allEnvironments.map((e) => `- ${e.name}`).join("\n");
825
+ const contextBlock = [
826
+ `<deployments-recent count="${recentDeployments.length}">`,
827
+ deploymentRows || "(none)",
828
+ `</deployments-recent>`,
829
+ `<artifacts count="${allArtifacts.length}">`,
830
+ artifactRows || "(none)",
831
+ `</artifacts>`,
832
+ `<environments count="${allEnvironments.length}">`,
833
+ environmentRows || "(none)",
834
+ `</environments>`,
835
+ `<partitions count="${allPartitions.length}">`,
836
+ partitionRows || "(none)",
837
+ `</partitions>`,
838
+ ].join("\n");
839
+ const systemPrompt = `You are Synth, an intelligent deployment system. A deployment engineer has asked you a question. Answer it using the real deployment data provided — do not fabricate records or invent names.
840
+
841
+ Format your response as markdown:
842
+ - Use tables for tabular/comparative data. When a deployment is listed in a table, make its version a markdown link using the format: [v1.2.3](synth://deployment-detail?id=<deployment-id>) — use the actual id from the data (the id: prefix in each row).
843
+ - For partition or environment rows, you may link using: [Name](synth://partition-detail?id=<partition-name>) or [Name](synth://environment-detail?id=<env-name>)
844
+ - Use numbered lists for sequences or steps
845
+ - Use code blocks for configs, IDs, or technical strings
846
+ - Be specific and factual — reference actual artifact names, environments, and statuses from the data
847
+ - If the data doesn't contain enough information to answer precisely, say so clearly
848
+
849
+ Keep the response concise and directly useful to an engineer.`;
850
+ const prompt = [
851
+ `<user-query>${sanitizeUserInput(query)}</user-query>`,
852
+ contextBlock,
853
+ ].join("\n");
854
+ const llmResult = await llm.reason({
855
+ prompt,
856
+ systemPrompt,
857
+ promptSummary: `Analytical answer for: "${query}"`,
858
+ partitionId: null,
859
+ maxTokens: 2048,
860
+ });
861
+ if (!llmResult.ok)
862
+ return null;
863
+ const content = llmResult.text.trim();
864
+ const title = query.length > 60 ? query.slice(0, 57) + "..." : query;
865
+ return { action: "answer", view: "", params: {}, title, content };
866
+ }
867
+ //# sourceMappingURL=agent.js.map