@adhdev/daemon-core 0.9.82-rc.17 → 0.9.82-rc.170

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 (221) hide show
  1. package/dist/boot/process-hardening.d.ts +50 -0
  2. package/dist/chat/source-machine.d.ts +166 -0
  3. package/dist/chat/source-resolver.d.ts +104 -0
  4. package/dist/chat/subscription-updates.d.ts +1 -0
  5. package/dist/cli-adapter-types.d.ts +22 -2
  6. package/dist/cli-adapters/cli-script-runner.d.ts +117 -0
  7. package/dist/cli-adapters/cli-state-engine.d.ts +178 -0
  8. package/dist/cli-adapters/provider-cli-adapter.d.ts +106 -63
  9. package/dist/cli-adapters/provider-cli-parse.d.ts +4 -0
  10. package/dist/cli-adapters/provider-cli-shared.d.ts +29 -0
  11. package/dist/cli-adapters/raw-terminal-io.d.ts +37 -0
  12. package/dist/commands/handler.d.ts +110 -0
  13. package/dist/commands/mesh-coordinator.d.ts +72 -1
  14. package/dist/commands/router.d.ts +22 -0
  15. package/dist/config/chat-history.d.ts +7 -0
  16. package/dist/config/config.d.ts +5 -0
  17. package/dist/config/mesh-config.d.ts +71 -1
  18. package/dist/git/git-commands.d.ts +5 -1
  19. package/dist/index.d.ts +35 -6
  20. package/dist/index.js +18621 -3760
  21. package/dist/index.js.map +1 -1
  22. package/dist/index.mjs +18525 -3732
  23. package/dist/index.mjs.map +1 -1
  24. package/dist/installer.d.ts +1 -4
  25. package/dist/ipc/local-ipc-server.d.ts +91 -0
  26. package/dist/launch.d.ts +1 -1
  27. package/dist/logging/async-batch-writer.d.ts +10 -0
  28. package/dist/mesh/beads-db.d.ts +72 -0
  29. package/dist/mesh/contracts.d.ts +164 -0
  30. package/dist/mesh/coordinator-prompt.d.ts +30 -0
  31. package/dist/mesh/coordinator-registry.d.ts +59 -0
  32. package/dist/mesh/mesh-active-work.d.ts +90 -0
  33. package/dist/mesh/mesh-events.d.ts +96 -5
  34. package/dist/mesh/mesh-fast-forward.d.ts +39 -0
  35. package/dist/mesh/mesh-host-ownership.d.ts +9 -0
  36. package/dist/mesh/mesh-ledger.d.ts +58 -1
  37. package/dist/mesh/mesh-refine-status.d.ts +26 -0
  38. package/dist/mesh/mesh-work-queue.d.ts +44 -5
  39. package/dist/mesh/preview-freshness.d.ts +18 -0
  40. package/dist/mesh/refine-config.d.ts +193 -0
  41. package/dist/mesh/worktree-bootstrap-config.d.ts +113 -0
  42. package/dist/providers/approval-utils.d.ts +13 -0
  43. package/dist/providers/cli-provider-instance.d.ts +9 -3
  44. package/dist/providers/contracts.d.ts +98 -5
  45. package/dist/providers/external-sources.d.ts +71 -0
  46. package/dist/providers/native-history/antigravity-cli-transcript.d.ts +100 -0
  47. package/dist/providers/native-history/claude-cli-transcript.d.ts +70 -0
  48. package/dist/providers/native-history/codex-cli-transcript.d.ts +73 -0
  49. package/dist/providers/native-history/dispatcher.d.ts +24 -0
  50. package/dist/providers/native-history/hermes-cli-transcript.d.ts +30 -0
  51. package/dist/providers/native-history/index.d.ts +13 -0
  52. package/dist/providers/provider-instance.d.ts +3 -0
  53. package/dist/providers/provider-loader.d.ts +26 -4
  54. package/dist/providers/provider-trust.d.ts +31 -0
  55. package/dist/providers/read-chat-contract.d.ts +29 -0
  56. package/dist/providers/sdk/v1/builders/acp/detect-status.d.ts +68 -0
  57. package/dist/providers/sdk/v1/builders/cli/detect-status.d.ts +85 -0
  58. package/dist/providers/sdk/v1/builders/cli/parse-approval-squash.d.ts +59 -0
  59. package/dist/providers/sdk/v1/builders/cli/parse-approval.d.ts +65 -0
  60. package/dist/providers/sdk/v1/builders/cli/parse-session.d.ts +91 -0
  61. package/dist/providers/sdk/v1/builders/cli/visible-region.d.ts +42 -0
  62. package/dist/providers/sdk/v1/fixture-tooling/format.d.ts +126 -0
  63. package/dist/providers/sdk/v1/fixture-tooling/index.d.ts +8 -0
  64. package/dist/providers/sdk/v1/fixture-tooling/replay.d.ts +38 -0
  65. package/dist/providers/sdk/v1/index.d.ts +30 -0
  66. package/dist/providers/sdk/v1/sandbox/README-design.d.ts +193 -0
  67. package/dist/providers/sdk/v1/sandbox/require-whitelist.d.ts +74 -0
  68. package/dist/providers/sdk/v1/sandbox/script-runner.d.ts +98 -0
  69. package/dist/providers/sdk/v1/types/cli/index.d.ts +277 -0
  70. package/dist/providers/sdk/v1/types/common/index.d.ts +169 -0
  71. package/dist/providers/sdk/v1/validators/index.d.ts +5 -0
  72. package/dist/providers/sdk/v1/validators/manifest.d.ts +40 -0
  73. package/dist/providers/sdk/v1/validators/taint.d.ts +52 -0
  74. package/dist/providers/spec/adapter.d.ts +56 -0
  75. package/dist/providers/spec/cli-adapter.d.ts +95 -0
  76. package/dist/providers/spec/driver.d.ts +149 -0
  77. package/dist/providers/spec/evaluator.d.ts +47 -0
  78. package/dist/providers/spec/loader.d.ts +14 -0
  79. package/dist/providers/spec/native-history-executor.d.ts +39 -0
  80. package/dist/providers/spec/route.d.ts +4 -0
  81. package/dist/providers/spec/schema.gen.d.ts +511 -0
  82. package/dist/providers/spec/types.d.ts +212 -0
  83. package/dist/providers/transcript-v2.d.ts +176 -0
  84. package/dist/providers/types/interactive-prompt.d.ts +48 -0
  85. package/dist/repo-mesh-types.d.ts +100 -1
  86. package/dist/sessions/registry.d.ts +3 -0
  87. package/dist/shared-types.d.ts +42 -1
  88. package/dist/status/reporter.d.ts +2 -0
  89. package/dist/status/snapshot.d.ts +1 -0
  90. package/dist/types.d.ts +5 -0
  91. package/package.json +7 -2
  92. package/src/agent-stream/poller.ts +2 -3
  93. package/src/boot/daemon-lifecycle.ts +17 -10
  94. package/src/boot/process-hardening.ts +89 -0
  95. package/src/chat/source-machine.ts +534 -0
  96. package/src/chat/source-resolver.ts +0 -0
  97. package/src/chat/subscription-updates.ts +14 -1
  98. package/src/cli-adapter-types.d.ts +1 -0
  99. package/src/cli-adapter-types.ts +21 -2
  100. package/src/cli-adapters/cli-script-runner.ts +421 -0
  101. package/src/cli-adapters/cli-state-engine.ts +1086 -0
  102. package/src/cli-adapters/provider-cli-adapter.d.ts +1 -1
  103. package/src/cli-adapters/provider-cli-adapter.ts +670 -1137
  104. package/src/cli-adapters/provider-cli-parse.d.ts +1 -0
  105. package/src/cli-adapters/provider-cli-parse.ts +13 -0
  106. package/src/cli-adapters/provider-cli-runtime.ts +3 -1
  107. package/src/cli-adapters/provider-cli-shared.d.ts +2 -0
  108. package/src/cli-adapters/provider-cli-shared.ts +59 -11
  109. package/src/cli-adapters/raw-terminal-io.ts +252 -0
  110. package/src/cli-adapters/terminal-backends/ghostty-vt-backend.ts +17 -1
  111. package/src/cli-adapters/terminal-backends/xterm-backend.ts +8 -1
  112. package/src/commands/chat-commands.ts +1597 -60
  113. package/src/commands/cli-manager.ts +214 -3
  114. package/src/commands/handler.ts +809 -2
  115. package/src/commands/mesh-coordinator.ts +231 -127
  116. package/src/commands/router.ts +3559 -502
  117. package/src/config/chat-history.ts +87 -24
  118. package/src/config/config.ts +12 -0
  119. package/src/config/mesh-config.ts +264 -2
  120. package/src/config/recent-activity.ts +8 -2
  121. package/src/daemon/dev-cli-debug.ts +10 -1
  122. package/src/detection/ide-detector.ts +26 -16
  123. package/src/git/git-commands.ts +17 -5
  124. package/src/git/git-worktree.ts +8 -1
  125. package/src/index.ts +113 -5
  126. package/src/installer.d.ts +1 -1
  127. package/src/installer.ts +8 -6
  128. package/src/ipc/local-ipc-server.ts +278 -0
  129. package/src/launch.d.ts +1 -1
  130. package/src/launch.ts +37 -28
  131. package/src/logging/async-batch-writer.ts +55 -0
  132. package/src/logging/logger.ts +2 -1
  133. package/src/mesh/beads-db.ts +479 -0
  134. package/src/mesh/contracts.ts +329 -0
  135. package/src/mesh/coordinator-prompt.ts +204 -30
  136. package/src/mesh/coordinator-registry.ts +121 -0
  137. package/src/mesh/mesh-active-work.ts +437 -0
  138. package/src/mesh/mesh-events.ts +879 -61
  139. package/src/mesh/mesh-fast-forward.ts +430 -0
  140. package/src/mesh/mesh-host-ownership.ts +73 -0
  141. package/src/mesh/mesh-ledger.ts +457 -104
  142. package/src/mesh/mesh-refine-status.ts +144 -0
  143. package/src/mesh/mesh-work-queue.ts +216 -158
  144. package/src/mesh/preview-freshness.ts +118 -0
  145. package/src/mesh/refine-config.ts +366 -0
  146. package/src/mesh/worktree-bootstrap-config.ts +247 -0
  147. package/src/providers/approval-utils.d.ts +4 -0
  148. package/src/providers/approval-utils.ts +47 -5
  149. package/src/providers/chat-message-normalization.ts +4 -11
  150. package/src/providers/cli-provider-instance.ts +452 -54
  151. package/src/providers/contracts.ts +105 -5
  152. package/src/providers/external-sources.ts +218 -0
  153. package/src/providers/ide-provider-instance.ts +19 -5
  154. package/src/providers/native-history/antigravity-cli-transcript.ts +643 -0
  155. package/src/providers/native-history/claude-cli-transcript.ts +396 -0
  156. package/src/providers/native-history/codex-cli-transcript.ts +419 -0
  157. package/src/providers/native-history/dispatcher.ts +227 -0
  158. package/src/providers/native-history/hermes-cli-transcript.ts +230 -0
  159. package/src/providers/native-history/index.ts +30 -0
  160. package/src/providers/provider-instance.ts +3 -0
  161. package/src/providers/provider-loader.ts +585 -50
  162. package/src/providers/provider-schema.ts +31 -13
  163. package/src/providers/provider-trust.ts +114 -0
  164. package/src/providers/read-chat-contract.ts +76 -16
  165. package/src/providers/sdk/README.md +49 -0
  166. package/src/providers/sdk/v1/builders/acp/detect-status.ts +144 -0
  167. package/src/providers/sdk/v1/builders/cli/detect-status.ts +262 -0
  168. package/src/providers/sdk/v1/builders/cli/parse-approval-squash.ts +158 -0
  169. package/src/providers/sdk/v1/builders/cli/parse-approval.ts +250 -0
  170. package/src/providers/sdk/v1/builders/cli/parse-session.ts +276 -0
  171. package/src/providers/sdk/v1/builders/cli/visible-region.ts +143 -0
  172. package/src/providers/sdk/v1/fixture-tooling/format.ts +130 -0
  173. package/src/providers/sdk/v1/fixture-tooling/index.ts +22 -0
  174. package/src/providers/sdk/v1/fixture-tooling/replay.ts +352 -0
  175. package/src/providers/sdk/v1/index.ts +152 -0
  176. package/src/providers/sdk/v1/sandbox/README-design.ts +195 -0
  177. package/src/providers/sdk/v1/sandbox/require-whitelist.ts +472 -0
  178. package/src/providers/sdk/v1/sandbox/script-runner.ts +150 -0
  179. package/src/providers/sdk/v1/schemas/cli/provider.schema.json +458 -0
  180. package/src/providers/sdk/v1/schemas/primitives/acp-session-protocol-v1.json +131 -0
  181. package/src/providers/sdk/v1/schemas/primitives/native-history-codex-rollout-v1.json +66 -0
  182. package/src/providers/sdk/v1/schemas/primitives/tui-approval-squash-v1.json +91 -0
  183. package/src/providers/sdk/v1/schemas/primitives/tui-assistant-block-v1.json +91 -0
  184. package/src/providers/sdk/v1/schemas/primitives/tui-cue-ordering-v1.json +47 -0
  185. package/src/providers/sdk/v1/schemas/primitives/tui-dispatch-order-v1.json +32 -0
  186. package/src/providers/sdk/v1/schemas/primitives/tui-footer-chrome-v1.json +42 -0
  187. package/src/providers/sdk/v1/schemas/primitives/tui-index-finder-v1.json +27 -0
  188. package/src/providers/sdk/v1/schemas/primitives/tui-modal-v1.json +125 -0
  189. package/src/providers/sdk/v1/schemas/primitives/tui-prompt-marker-v1.json +45 -0
  190. package/src/providers/sdk/v1/schemas/primitives/tui-session-id-extraction-v1.json +46 -0
  191. package/src/providers/sdk/v1/schemas/primitives/tui-settled-prompt-v1.json +71 -0
  192. package/src/providers/sdk/v1/schemas/primitives/tui-spinner-v1.json +83 -0
  193. package/src/providers/sdk/v1/schemas/primitives/tui-transcript-pty-v1.json +83 -0
  194. package/src/providers/sdk/v1/schemas/primitives/tui-visible-region-v1.json +57 -0
  195. package/src/providers/sdk/v1/schemas/primitives/tui-welcome-screen-v1.json +35 -0
  196. package/src/providers/sdk/v1/types/cli/index.ts +365 -0
  197. package/src/providers/sdk/v1/types/common/index.ts +210 -0
  198. package/src/providers/sdk/v1/validators/index.ts +19 -0
  199. package/src/providers/sdk/v1/validators/manifest.ts +110 -0
  200. package/src/providers/sdk/v1/validators/taint.ts +309 -0
  201. package/src/providers/spec/adapter.ts +168 -0
  202. package/src/providers/spec/cli-adapter.ts +430 -0
  203. package/src/providers/spec/driver.ts +502 -0
  204. package/src/providers/spec/evaluator.ts +298 -0
  205. package/src/providers/spec/loader.ts +130 -0
  206. package/src/providers/spec/native-history-executor.ts +623 -0
  207. package/src/providers/spec/route.ts +51 -0
  208. package/src/providers/spec/schema.gen.ts +511 -0
  209. package/src/providers/spec/schema.json +211 -0
  210. package/src/providers/spec/types.ts +231 -0
  211. package/src/providers/transcript-v2.ts +567 -0
  212. package/src/providers/types/interactive-prompt.ts +297 -0
  213. package/src/providers/version-archive.ts +38 -20
  214. package/src/repo-mesh-types.ts +110 -1
  215. package/src/sessions/registry.ts +3 -0
  216. package/src/shared-types.ts +45 -1
  217. package/src/status/builders.ts +24 -6
  218. package/src/status/reporter.ts +15 -0
  219. package/src/status/snapshot.ts +84 -25
  220. package/src/system/host-memory.ts +29 -12
  221. package/src/types.ts +5 -0
@@ -0,0 +1,623 @@
1
+ /**
2
+ * Declarative native-history executor.
3
+ *
4
+ * Reads a spec.json's `native_history.source` block and turns it into a
5
+ * NativeHistoryResult by talking directly to the on-disk store (jsonl
6
+ * file or sqlite db). No per-provider TypeScript reader required —
7
+ * adding a new provider is just authoring spec.json.
8
+ *
9
+ * Two source kinds:
10
+ * - jsonl — newest matching file inside a path (with variable expansion)
11
+ * + jsonpath-lite map for each record
12
+ * - sqlite — read-only better-sqlite3 handle, two queries (session pick
13
+ * + message fetch) + jsonpath-lite map for each row
14
+ *
15
+ * Exotic formats can still ship a provider-local reader via
16
+ * `native_history.override_path` — the dispatcher in provider-loader picks
17
+ * that path instead of calling this executor.
18
+ */
19
+ 'use strict';
20
+
21
+ import * as fs from 'node:fs';
22
+ import * as os from 'node:os';
23
+ import * as path from 'node:path';
24
+ import type {
25
+ NativeHistoryConfig,
26
+ NativeHistoryJsonlSource,
27
+ NativeHistoryMessageMap,
28
+ NativeHistorySqliteSource,
29
+ } from './types.js';
30
+
31
+ export interface NativeHistoryInput {
32
+ agentType?: string;
33
+ sessionId?: string;
34
+ providerSessionId?: string;
35
+ historySessionId?: string;
36
+ workspace?: string;
37
+ /** Daemon-side wall clock at the moment the session was registered.
38
+ * Native-history file lookups use this as the lower bound: any file
39
+ * whose mtime is before the current session started can't be from
40
+ * this session, so it's excluded from newest-recent matching. The
41
+ * caller (chat-history pipeline) populates this from the session
42
+ * registry; specs/executor never need to know how it's sourced. */
43
+ sessionStartedAtMs?: number;
44
+ /** Env overrides the daemon set on the spawned CLI. The mesh
45
+ * coordinator points hermes at a per-coordinator HERMES_HOME so
46
+ * the hermes process writes its state.db into a tmp directory
47
+ * instead of ~/.hermes. expandPath consults this map before
48
+ * process.env so the native-history reader follows the spawned
49
+ * child's view of HERMES_HOME / similar overrides; without it
50
+ * the reader would always look at ~/.hermes and miss every
51
+ * coordinator-session transcript. */
52
+ envOverrides?: Record<string, string>;
53
+ args?: Record<string, unknown>;
54
+ }
55
+
56
+ export interface NativeHistoryMessage {
57
+ role: 'user' | 'assistant' | 'system';
58
+ content: string;
59
+ receivedAt: number;
60
+ kind?: string;
61
+ }
62
+
63
+ export interface NativeHistoryResult {
64
+ messages: NativeHistoryMessage[];
65
+ providerSessionId?: string;
66
+ sourcePath: string;
67
+ sourceMtimeMs: number;
68
+ nativeHistoryCoverage?: 'full' | 'partial' | 'best-effort';
69
+ }
70
+
71
+ const UUID_RE = /([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})/i;
72
+
73
+ export function executeNativeHistory(cfg: NativeHistoryConfig, input: NativeHistoryInput): NativeHistoryResult | null {
74
+ if (!cfg?.source) return null;
75
+ if (cfg.source.kind === 'jsonl') return executeJsonl(cfg.source, input);
76
+ if (cfg.source.kind === 'sqlite') return executeSqlite(cfg.source, input);
77
+ return null;
78
+ }
79
+
80
+ // ────────────────────────────────────────────────────────────────────────────
81
+ // JSONL
82
+ // ────────────────────────────────────────────────────────────────────────────
83
+
84
+ function executeJsonl(src: NativeHistoryJsonlSource, input: NativeHistoryInput): NativeHistoryResult | null {
85
+ const resolved = expandPath(src.path, input);
86
+ if (!resolved) return null;
87
+
88
+ const windowMs = typeof src.recent_window_ms === 'number' ? src.recent_window_ms : 5 * 60_000;
89
+ const filePat = src.file_pattern ? globToRegex(src.file_pattern) : /.*\.jsonl$/;
90
+
91
+ // path can be:
92
+ // - a concrete file → used as-is
93
+ // - a concrete dir → newest matching file inside recent_window_ms
94
+ // - a path with `*` or `**` segments → walk all dirs that match the
95
+ // glob, pick the newest matching file across all matches. Lets
96
+ // specs like ~/.gemini/antigravity-cli/brain/*/.system_generated/logs
97
+ // resolve transparently without a per-provider override.
98
+ // The session-start cutoff guarantees a fresh dashboard view can't
99
+ // pick up a transcript file from a session that ended before this
100
+ // one started. recent_window_ms only controls how far back we'd
101
+ // otherwise look for a matching file; the session-start floor wins
102
+ // when it's later.
103
+ const sessionFloor = typeof input.sessionStartedAtMs === 'number' ? input.sessionStartedAtMs : 0;
104
+ let sourcePath: string | null = null;
105
+ if (resolved.includes('*')) {
106
+ sourcePath = newestRecentFileAcrossGlob(resolved, filePat, windowMs, sessionFloor);
107
+ } else {
108
+ let stat: fs.Stats | null = null;
109
+ try { stat = fs.statSync(resolved); } catch { return null; }
110
+ if (stat.isFile()) {
111
+ sourcePath = resolved;
112
+ } else if (stat.isDirectory()) {
113
+ sourcePath = newestRecentFile(resolved, filePat, windowMs, sessionFloor);
114
+ }
115
+ }
116
+ if (!sourcePath) return null;
117
+
118
+ const mtime = safeMtimeMs(sourcePath);
119
+ const lines = readJsonlLines(sourcePath);
120
+ if (lines.length === 0) return null;
121
+
122
+ // session id: filename uuid or extracted from first record
123
+ let providerSessionId: string | undefined;
124
+ if (src.session_id_from === 'first_record' && src.session_id_path) {
125
+ const v = jsonPathGet(lines[0], src.session_id_path);
126
+ if (typeof v === 'string' && v) providerSessionId = v;
127
+ } else if (src.session_id_from === 'filename_uuid' || !src.session_id_from) {
128
+ const m = path.basename(sourcePath).match(UUID_RE);
129
+ if (m) providerSessionId = m[1];
130
+ }
131
+
132
+ const requested = input.providerSessionId || '';
133
+ if (requested && providerSessionId && providerSessionId !== requested) return null;
134
+
135
+ const filter = src.message_filter ? compileWhere(src.message_filter.where) : null;
136
+ const messages: NativeHistoryMessage[] = [];
137
+ for (let i = 0; i < lines.length; i += 1) {
138
+ const rec = lines[i];
139
+ if (filter && !filter(rec)) continue;
140
+ const msg = projectMessage(rec, src.message_map, i, lines.length, mtime);
141
+ if (msg) messages.push(msg);
142
+ }
143
+ if (messages.length === 0) return null;
144
+
145
+ return {
146
+ messages,
147
+ providerSessionId,
148
+ sourcePath,
149
+ sourceMtimeMs: mtime,
150
+ nativeHistoryCoverage: 'full',
151
+ };
152
+ }
153
+
154
+ function readJsonlLines(p: string): any[] {
155
+ let text: string;
156
+ try { text = fs.readFileSync(p, 'utf8'); } catch { return []; }
157
+ const out: any[] = [];
158
+ for (const line of text.split('\n')) {
159
+ const trimmed = line.trim();
160
+ if (!trimmed) continue;
161
+ try { out.push(JSON.parse(trimmed)); } catch { /* skip malformed line */ }
162
+ }
163
+ return out;
164
+ }
165
+
166
+ // ────────────────────────────────────────────────────────────────────────────
167
+ // SQLite
168
+ // ────────────────────────────────────────────────────────────────────────────
169
+
170
+ function executeSqlite(src: NativeHistorySqliteSource, input: NativeHistoryInput): NativeHistoryResult | null {
171
+ const resolved = expandPath(src.path, input);
172
+ if (!resolved || !fs.existsSync(resolved)) return null;
173
+
174
+ let Database: any;
175
+ try {
176
+ // eslint-disable-next-line @typescript-eslint/no-require-imports
177
+ Database = require('better-sqlite3');
178
+ } catch { return null; }
179
+
180
+ let db: any;
181
+ try { db = new Database(resolved, { readonly: true, fileMustExist: true }); }
182
+ catch { return null; }
183
+
184
+ try {
185
+ let sessionRow: any;
186
+ try {
187
+ // session_query may reference `?` to receive the session's
188
+ // start-time floor in seconds (e.g. WHERE started_at >= ?).
189
+ // That gives spec authors a robust way to keep prior-session
190
+ // rows out of a fresh dashboard view without inventing their
191
+ // own time arithmetic in SQL. When the caller didn't pass a
192
+ // session floor (i.e. no live session is associated with the
193
+ // call), we use 0 so spec queries that bind `?` still produce
194
+ // a sane result rather than choking the whole executor.
195
+ const sessionFloorSeconds = typeof input.sessionStartedAtMs === 'number'
196
+ ? Math.floor(input.sessionStartedAtMs / 1000)
197
+ : 0;
198
+ const stmt = db.prepare(src.session_query);
199
+ try {
200
+ sessionRow = stmt.get(sessionFloorSeconds);
201
+ } catch {
202
+ sessionRow = stmt.get();
203
+ }
204
+ } catch { return null; }
205
+ if (!sessionRow) return null;
206
+ // First column of the first row is the session id.
207
+ const sessionIdRaw = Object.values(sessionRow)[0];
208
+ const sessionId = sessionIdRaw == null ? '' : String(sessionIdRaw);
209
+ if (!sessionId) return null;
210
+
211
+ const requested = input.providerSessionId || '';
212
+ if (requested && sessionId !== requested) return null;
213
+
214
+ const messageRows: any[] = db.prepare(src.message_query).all(sessionId);
215
+ if (!messageRows || messageRows.length === 0) return null;
216
+
217
+ const mtime = safeMtimeMs(resolved);
218
+ const messages: NativeHistoryMessage[] = [];
219
+ for (let i = 0; i < messageRows.length; i += 1) {
220
+ const msg = projectMessage(messageRows[i], src.message_map, i, messageRows.length, mtime);
221
+ if (msg) messages.push(msg);
222
+ }
223
+ if (messages.length === 0) return null;
224
+
225
+ return {
226
+ messages,
227
+ providerSessionId: sessionId,
228
+ sourcePath: resolved,
229
+ sourceMtimeMs: mtime,
230
+ nativeHistoryCoverage: 'full',
231
+ };
232
+ } finally {
233
+ try { db.close(); } catch { /* ignore */ }
234
+ }
235
+ }
236
+
237
+ // ────────────────────────────────────────────────────────────────────────────
238
+ // Path expansion + globbing
239
+ // ────────────────────────────────────────────────────────────────────────────
240
+
241
+ function expandPath(template: string, input: NativeHistoryInput): string | null {
242
+ if (!template) return null;
243
+ let out = template;
244
+ if (out.startsWith('~/') || out === '~') {
245
+ out = path.join(os.homedir(), out.slice(2));
246
+ }
247
+ // ${VAR} expands from envOverrides (the spawned child's view) first,
248
+ // then process.env. ${VAR:-fallback} keeps the bash-style default
249
+ // so spec authors can say e.g. ${HERMES_HOME:-~/.hermes}/state.db
250
+ // and have it work both for coordinator-launched sessions (where
251
+ // HERMES_HOME is set to a tmpdir) and normal sessions.
252
+ out = out.replace(/\$\{([A-Z_][A-Z0-9_]*)(?::-(.*?))?\}/g, (_m, name, fallback) => {
253
+ const v = input.envOverrides?.[name] ?? process.env[name];
254
+ return v != null && v !== '' ? v : (fallback ?? '');
255
+ });
256
+ // Re-expand ~ in case the fallback used it.
257
+ if (out.startsWith('~/')) out = path.join(os.homedir(), out.slice(2));
258
+ const now = new Date();
259
+ // claude (and a couple of other Anthropic-CLI–style providers) writes
260
+ // its per-cwd transcript under the *resolved* path
261
+ // (/private/tmp/foo, not /tmp/foo on macOS where /tmp -> /private/tmp).
262
+ // Without realpath, the spec template `~/.claude/projects/{cwd_dashed}/…`
263
+ // builds `-tmp-foo` and never finds the actual `-private-tmp-foo` dir.
264
+ const workspaceRaw = input.workspace ?? '';
265
+ let workspaceResolved = workspaceRaw;
266
+ if (workspaceRaw) {
267
+ try { workspaceResolved = fs.realpathSync(workspaceRaw); }
268
+ catch { /* path may not exist yet — keep the raw value */ }
269
+ }
270
+ const vars: Record<string, string> = {
271
+ cwd: workspaceResolved,
272
+ cwd_dashed: workspaceResolved.replace(/\//g, '-'),
273
+ session_id: input.providerSessionId || input.sessionId || input.historySessionId || '',
274
+ yyyy: String(now.getUTCFullYear()),
275
+ mm: String(now.getUTCMonth() + 1).padStart(2, '0'),
276
+ dd: String(now.getUTCDate()).padStart(2, '0'),
277
+ };
278
+ // Replace {var}. If a referenced variable is empty (e.g. session_id
279
+ // before the agent has allocated one), return null so the caller
280
+ // doesn't accidentally fall through to an unrelated newest-file
281
+ // match. Wildcards (`*`) are explicitly allowed to pass — the dir
282
+ // glob walker handles them separately.
283
+ let missing = false;
284
+ out = out.replace(/\{([a-zA-Z_][a-zA-Z0-9_]*)\}/g, (_m, name) => {
285
+ const v = vars[name] ?? '';
286
+ if (!v) missing = true;
287
+ return v;
288
+ });
289
+ if (missing) return null;
290
+ return out;
291
+ }
292
+
293
+ function globToRegex(pattern: string): RegExp {
294
+ // Minimal glob: `*` → `[^/]*`, `?` → `[^/]`, `.` → `\.`. Anchored.
295
+ const re = pattern
296
+ .replace(/[.+^${}()|[\]\\]/g, '\\$&')
297
+ .replace(/\*/g, '[^/]*')
298
+ .replace(/\?/g, '[^/]');
299
+ return new RegExp(`^${re}$`);
300
+ }
301
+
302
+ /**
303
+ * Resolve a path with `*` segments to all concrete directories that match,
304
+ * then pick the newest file inside any of them. `*` matches one path
305
+ * component (no slashes); `**` matches zero or more components. Filenames
306
+ * are matched against `pattern`, not the glob — file_pattern is the right
307
+ * place for the leaf match.
308
+ */
309
+ function expandDirGlob(template: string): string[] {
310
+ const parts = template.split('/');
311
+ let dirs: string[] = parts[0] === '' ? ['/'] : [parts[0]];
312
+ for (let i = 1; i < parts.length; i += 1) {
313
+ const seg = parts[i];
314
+ if (!seg) continue;
315
+ const next: string[] = [];
316
+ if (seg === '**') {
317
+ for (const d of dirs) walkAllDirs(d, next);
318
+ dirs = next;
319
+ continue;
320
+ }
321
+ if (seg.includes('*') || seg.includes('?')) {
322
+ const re = globToRegex(seg);
323
+ for (const d of dirs) {
324
+ let entries: fs.Dirent[];
325
+ try { entries = fs.readdirSync(d, { withFileTypes: true }); } catch { continue; }
326
+ for (const e of entries) {
327
+ if (e.isDirectory() && re.test(e.name)) next.push(path.join(d, e.name));
328
+ }
329
+ }
330
+ } else {
331
+ for (const d of dirs) {
332
+ const candidate = path.join(d, seg);
333
+ let stat: fs.Stats | null = null;
334
+ try { stat = fs.statSync(candidate); } catch { continue; }
335
+ if (stat.isDirectory()) next.push(candidate);
336
+ }
337
+ }
338
+ dirs = next;
339
+ }
340
+ return dirs;
341
+ }
342
+
343
+ function walkAllDirs(root: string, out: string[]): void {
344
+ let entries: fs.Dirent[];
345
+ try { entries = fs.readdirSync(root, { withFileTypes: true }); } catch { return; }
346
+ out.push(root);
347
+ for (const e of entries) {
348
+ if (e.isDirectory()) walkAllDirs(path.join(root, e.name), out);
349
+ }
350
+ }
351
+
352
+ function newestRecentFileAcrossGlob(template: string, pattern: RegExp, windowMs: number, sessionFloorMs = 0): string | null {
353
+ const dirs = expandDirGlob(template);
354
+ const cutoff = Math.max(Date.now() - windowMs, sessionFloorMs);
355
+ let best: { p: string; mtime: number } | null = null;
356
+ for (const d of dirs) {
357
+ let entries: fs.Dirent[];
358
+ try { entries = fs.readdirSync(d, { withFileTypes: true }); } catch { continue; }
359
+ for (const e of entries) {
360
+ if (!e.isFile() || !pattern.test(e.name)) continue;
361
+ const p = path.join(d, e.name);
362
+ const mtime = safeMtimeMs(p);
363
+ if (mtime < cutoff) continue;
364
+ if (!best || mtime > best.mtime) best = { p, mtime };
365
+ }
366
+ }
367
+ return best ? best.p : null;
368
+ }
369
+
370
+ function newestRecentFile(dir: string, pattern: RegExp, windowMs: number, sessionFloorMs = 0): string | null {
371
+ let entries: fs.Dirent[];
372
+ try { entries = fs.readdirSync(dir, { withFileTypes: true }); } catch { return null; }
373
+ const cutoff = Math.max(Date.now() - windowMs, sessionFloorMs);
374
+ let best: { p: string; mtime: number } | null = null;
375
+ for (const e of entries) {
376
+ if (!e.isFile() || !pattern.test(e.name)) continue;
377
+ const p = path.join(dir, e.name);
378
+ const mtime = safeMtimeMs(p);
379
+ if (mtime < cutoff) continue;
380
+ if (!best || mtime > best.mtime) best = { p, mtime };
381
+ }
382
+ return best ? best.p : null;
383
+ }
384
+
385
+ function safeMtimeMs(p: string): number {
386
+ try { return Math.floor(fs.statSync(p).mtimeMs); } catch { return 0; }
387
+ }
388
+
389
+ // ────────────────────────────────────────────────────────────────────────────
390
+ // jsonpath-lite + projection
391
+ // ────────────────────────────────────────────────────────────────────────────
392
+
393
+ /**
394
+ * Resolve `$.a.b[0].c` against a record. Strings without leading `$` are
395
+ * literals. Supports `||` fallback between paths so a single message_map
396
+ * entry can pick the first non-empty value across alternative locations
397
+ * (e.g. agy's content vs. thinking).
398
+ */
399
+ function jsonPathGet(record: any, expr: string): unknown {
400
+ if (typeof expr !== 'string') return undefined;
401
+ if (expr.includes('||')) {
402
+ for (const alt of expr.split('||')) {
403
+ const v = jsonPathGet(record, alt.trim());
404
+ if (v != null && v !== '') return v;
405
+ }
406
+ return undefined;
407
+ }
408
+ if (!expr.startsWith('$')) return expr;
409
+ let cur: any = record;
410
+ let i = 1;
411
+ while (i < expr.length && cur != null) {
412
+ const ch = expr[i];
413
+ if (ch === '.') { i += 1; continue; }
414
+ if (ch === '[') {
415
+ const close = expr.indexOf(']', i);
416
+ if (close < 0) return undefined;
417
+ const idx = Number(expr.slice(i + 1, close));
418
+ if (!Number.isInteger(idx)) return undefined;
419
+ cur = cur[idx];
420
+ i = close + 1;
421
+ continue;
422
+ }
423
+ let end = i;
424
+ while (end < expr.length && expr[end] !== '.' && expr[end] !== '[') end += 1;
425
+ const key = expr.slice(i, end);
426
+ cur = cur[key];
427
+ i = end;
428
+ }
429
+ return cur;
430
+ }
431
+
432
+ function projectMessage(record: any, map: NativeHistoryMessageMap, index: number, total: number, sourceMtimeMs: number): NativeHistoryMessage | null {
433
+ const roleRaw = jsonPathGet(record, map.role);
434
+ const contentRaw = jsonPathGet(record, map.content);
435
+ const role = normalizeRole(roleRaw);
436
+ let content = stringifyContent(contentRaw);
437
+ if (content && map.content_strip) {
438
+ for (const tag of map.content_strip) {
439
+ const safeTag = tag.replace(/[.+^${}()|[\]\\]/g, '\\$&');
440
+ const re = new RegExp(`<${safeTag}\\b[^>]*>[\\s\\S]*?<\\/${safeTag}\\s*>`, 'gi');
441
+ content = content.replace(re, '');
442
+ }
443
+ }
444
+ if (content && map.content_unwrap) {
445
+ for (const tag of map.content_unwrap) {
446
+ const safeTag = tag.replace(/[.+^${}()|[\]\\]/g, '\\$&');
447
+ const open = new RegExp(`<${safeTag}\\b[^>]*>`, 'gi');
448
+ const close = new RegExp(`<\\/${safeTag}\\s*>`, 'gi');
449
+ content = content.replace(open, '').replace(close, '');
450
+ }
451
+ }
452
+ if (content) content = content.trim();
453
+ if (!content) return null;
454
+
455
+ // Records are passed in chronological order (oldest → newest), so the
456
+ // last record's receivedAt should be ~sourceMtimeMs (when the file was
457
+ // last touched) and earlier records should walk backwards. Earlier
458
+ // version had this inverted, which made the dashboard render bubbles
459
+ // in reverse order and produce the "chat jumping" effect.
460
+ let receivedAt = sourceMtimeMs - ((total - 1 - index) * 1000);
461
+ if (map.timestamp_ms) {
462
+ const tsRaw = jsonPathGet(record, map.timestamp_ms);
463
+ const parsed = parseTimestamp(tsRaw);
464
+ if (parsed != null) receivedAt = parsed;
465
+ }
466
+ const kindRaw = map.kind ? jsonPathGet(record, map.kind) : undefined;
467
+ const kind = typeof kindRaw === 'string' && kindRaw ? kindRaw : 'standard';
468
+ return { role, content, receivedAt, kind };
469
+ }
470
+
471
+ /**
472
+ * Coerce a timestamp value to epoch milliseconds. Accepts:
473
+ * - number (ms or seconds — heuristic: < 1e12 means seconds)
474
+ * - ISO 8601 string ("2026-06-05T01:25:28Z")
475
+ * - numeric string
476
+ *
477
+ * Returns null when the value can't be parsed; caller falls back to the
478
+ * monotonic-by-index estimate.
479
+ */
480
+ function parseTimestamp(v: unknown): number | null {
481
+ if (v == null) return null;
482
+ if (typeof v === 'number' && Number.isFinite(v) && v > 0) {
483
+ return v < 1e12 ? Math.floor(v * 1000) : Math.floor(v);
484
+ }
485
+ if (typeof v === 'string' && v) {
486
+ const trimmed = v.trim();
487
+ const asNum = Number(trimmed);
488
+ if (Number.isFinite(asNum) && asNum > 0) {
489
+ return asNum < 1e12 ? Math.floor(asNum * 1000) : Math.floor(asNum);
490
+ }
491
+ const parsed = Date.parse(trimmed);
492
+ if (Number.isFinite(parsed)) return parsed;
493
+ }
494
+ return null;
495
+ }
496
+
497
+ function normalizeRole(r: unknown): 'user' | 'assistant' | 'system' {
498
+ const s = String(r ?? '').toLowerCase();
499
+ if (s === 'user' || s === 'human' || s === 'user_explicit') return 'user';
500
+ if (s === 'assistant' || s === 'ai' || s === 'model') return 'assistant';
501
+ if (s === 'tool' || s === 'tool_result' || s === 'function') return 'assistant';
502
+ return 'system';
503
+ }
504
+
505
+ /**
506
+ * Coerce a content value to a plain string the dashboard can render.
507
+ *
508
+ * Many providers ship structured content (claude messages are arrays of
509
+ * typed blocks: text / tool_use / tool_result). We collapse those to
510
+ * their text-bearing parts so the dashboard doesn't show raw JSON
511
+ * fragments in the transcript. Tool calls/results are intentionally
512
+ * dropped — the daemon's chat schema is for user-visible turns.
513
+ *
514
+ * Order of attempts:
515
+ * 1. string → as-is
516
+ * 2. array of blocks → join the `text` field of each
517
+ * block that has one; if none have
518
+ * a text field, fall through
519
+ * 3. object with a top-level `text` → that string
520
+ * 4. last resort → JSON.stringify
521
+ */
522
+ function stringifyContent(v: unknown): string {
523
+ if (v == null) return '';
524
+ if (typeof v === 'string') return v;
525
+ if (Array.isArray(v)) {
526
+ const parts: string[] = [];
527
+ for (const block of v) {
528
+ if (block == null) continue;
529
+ if (typeof block === 'string') { parts.push(block); continue; }
530
+ if (typeof block === 'object') {
531
+ const t = (block as any).text;
532
+ if (typeof t === 'string' && t) { parts.push(t); continue; }
533
+ // tool_use / tool_result / image / etc — skip from the
534
+ // user-facing transcript. They re-surface via the
535
+ // adapter's tool-event channel if/when that's wired.
536
+ }
537
+ }
538
+ if (parts.length > 0) return parts.join('\n');
539
+ return '';
540
+ }
541
+ if (typeof v === 'object') {
542
+ const t = (v as any).text;
543
+ if (typeof t === 'string') return t;
544
+ }
545
+ try { return JSON.stringify(v); } catch { return String(v); }
546
+ }
547
+
548
+ // ────────────────────────────────────────────────────────────────────────────
549
+ // where-clause mini-language
550
+ //
551
+ // Grammar (intentionally tiny — keep spec readable):
552
+ // expr := term (('&&' | '||') term)*
553
+ // term := path op literal
554
+ // op := '==' | '!=' | '>' | '<' | '>=' | '<='
555
+ // path := jsonpath like '$.foo.bar[0].baz'
556
+ // literal := string ('"…"' or "'…'") | number | true | false | null
557
+ //
558
+ // No grouping, no negation. If you need more, write a real reader file
559
+ // behind `native_history.override_path`.
560
+ // ────────────────────────────────────────────────────────────────────────────
561
+
562
+ interface WhereTerm { path: string; op: string; lit: unknown; negate?: boolean }
563
+
564
+ function compileWhere(src: string): (record: any) => boolean {
565
+ const ors: WhereTerm[][] = [];
566
+ for (const orChunk of src.split('||')) {
567
+ const ands: WhereTerm[] = [];
568
+ for (const andChunk of orChunk.split('&&')) {
569
+ const term = parseTerm(andChunk.trim());
570
+ if (term) ands.push(term);
571
+ }
572
+ if (ands.length > 0) ors.push(ands);
573
+ }
574
+ return (record: any) => ors.some(ands => ands.every(t => evalTerm(t, record)));
575
+ }
576
+
577
+ function parseTerm(src: string): WhereTerm | null {
578
+ let s = src.trim();
579
+ let negate = false;
580
+ if (s.startsWith('!')) { negate = true; s = s.slice(1).trim(); }
581
+ // Function-call form: startsWith($.x, "y") / endsWith($.x, "y") / contains($.x, "y")
582
+ const fnMatch = s.match(/^(startsWith|endsWith|contains)\s*\(\s*(.+?)\s*,\s*(.+?)\s*\)$/);
583
+ if (fnMatch) {
584
+ const [, op, pathExpr, litExpr] = fnMatch;
585
+ return { path: pathExpr, op, lit: parseLiteral(litExpr), negate };
586
+ }
587
+ // Binary comparison: <path> <op> <literal>
588
+ const opMatch = s.match(/^(.+?)\s*(==|!=|>=|<=|>|<)\s*(.+)$/);
589
+ if (!opMatch) return null;
590
+ const [, lhs, op, rhsRaw] = opMatch;
591
+ return { path: lhs.trim(), op, lit: parseLiteral(rhsRaw.trim()), negate };
592
+ }
593
+
594
+ function parseLiteral(src: string): unknown {
595
+ if (src === 'true') return true;
596
+ if (src === 'false') return false;
597
+ if (src === 'null') return null;
598
+ if ((src.startsWith('"') && src.endsWith('"')) || (src.startsWith("'") && src.endsWith("'"))) {
599
+ return src.slice(1, -1);
600
+ }
601
+ const n = Number(src);
602
+ if (!Number.isNaN(n)) return n;
603
+ return src; // bareword — treated as string
604
+ }
605
+
606
+ function evalTerm(t: WhereTerm, record: any): boolean {
607
+ const lhs = jsonPathGet(record, t.path);
608
+ const lit = t.lit;
609
+ let result: boolean;
610
+ switch (t.op) {
611
+ case '==': result = lhs === lit; break;
612
+ case '!=': result = lhs !== lit; break;
613
+ case '>': result = Number(lhs) > Number(lit); break;
614
+ case '<': result = Number(lhs) < Number(lit); break;
615
+ case '>=': result = Number(lhs) >= Number(lit); break;
616
+ case '<=': result = Number(lhs) <= Number(lit); break;
617
+ case 'startsWith': result = typeof lhs === 'string' && typeof lit === 'string' && lhs.startsWith(lit); break;
618
+ case 'endsWith': result = typeof lhs === 'string' && typeof lit === 'string' && lhs.endsWith(lit); break;
619
+ case 'contains': result = typeof lhs === 'string' && typeof lit === 'string' && lhs.includes(lit); break;
620
+ default: result = false;
621
+ }
622
+ return t.negate ? !result : result;
623
+ }
@@ -0,0 +1,51 @@
1
+ /**
2
+ * Routing — given a CLI provider definition, decide whether to drive
3
+ * it via the legacy ProviderCliAdapter (scripts/v1/*.js parsers +
4
+ * cli-state-engine) or via SpecCliAdapter (single spec.json +
5
+ * SpecDriver). The gate is the presence of spec.json in the resolved
6
+ * provider directory.
7
+ *
8
+ * This is the only place in the daemon that knows which path a given
9
+ * provider takes. Providers can be migrated one at a time by adding
10
+ * a spec.json next to their scripts/v1/, then deleting scripts/v1/.
11
+ */
12
+ 'use strict';
13
+
14
+ import * as fs from 'node:fs';
15
+ import * as path from 'node:path';
16
+ import { ProviderCliAdapter } from '../../cli-adapters/provider-cli-adapter.js';
17
+ import type { CliProviderModule } from '../../cli-adapters/provider-cli-adapter.js';
18
+ import type { PtyTransportFactory } from '../../cli-adapters/pty-transport.js';
19
+ import type { CliAdapter } from '../../cli-adapter-types.js';
20
+ import { SpecCliAdapter } from './cli-adapter.js';
21
+ import { LOG } from '../../logging/logger.js';
22
+
23
+ export function createCliAdapter(
24
+ provider: CliProviderModule,
25
+ workingDir: string,
26
+ cliArgs: string[],
27
+ extraEnv: Record<string, string>,
28
+ transportFactory?: PtyTransportFactory,
29
+ ): CliAdapter {
30
+ // Prefer the path provider-loader already resolved (it walks the
31
+ // compatibility[i].spec → specs/default.json → spec.json chain).
32
+ // Fall back to the legacy spec.json-in-provider-dir lookup if the
33
+ // loader didn't attach one — keeps out-of-tree providers that still
34
+ // use the original layout working.
35
+ const resolvedSpecPath = (provider as unknown as { _resolvedSpecPath?: string })._resolvedSpecPath;
36
+ const dir = (provider as unknown as { _resolvedProviderDir?: string })._resolvedProviderDir;
37
+ let specPath: string | undefined = resolvedSpecPath && fs.existsSync(resolvedSpecPath) ? resolvedSpecPath : undefined;
38
+ if (!specPath && dir) {
39
+ const legacy = path.join(dir, 'spec.json');
40
+ if (fs.existsSync(legacy)) specPath = legacy;
41
+ }
42
+ if (specPath) {
43
+ try {
44
+ LOG.info('spec-route', `[${provider.type}] routing through SpecCliAdapter (${path.relative(dir || '', specPath) || specPath})`);
45
+ return new SpecCliAdapter(specPath, workingDir, cliArgs, extraEnv, transportFactory);
46
+ } catch (err) {
47
+ LOG.warn('spec-route', `[${provider.type}] spec invalid, falling back to ProviderCliAdapter: ${(err as Error).message}`);
48
+ }
49
+ }
50
+ return new ProviderCliAdapter(provider, workingDir, cliArgs, extraEnv, transportFactory) as unknown as CliAdapter;
51
+ }