@scrylog/cli 0.3.0 → 0.3.1

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 (195) hide show
  1. package/dist/assets/highlights-eq9cgrbb.scm +604 -0
  2. package/dist/assets/highlights-ghv9g403.scm +205 -0
  3. package/dist/assets/highlights-hk7bwhj4.scm +284 -0
  4. package/dist/assets/highlights-r812a2qc.scm +150 -0
  5. package/dist/assets/highlights-x6tmsnaa.scm +115 -0
  6. package/dist/assets/injections-73j83es3.scm +27 -0
  7. package/dist/assets/parser.worker-xyhf83rf.js +4563 -0
  8. package/dist/assets/tree-sitter-3jzf13jk.wasm +0 -0
  9. package/dist/assets/tree-sitter-javascript-nd0q4pe9.wasm +0 -0
  10. package/dist/assets/tree-sitter-markdown-411r6y9b.wasm +0 -0
  11. package/dist/assets/tree-sitter-markdown_inline-j5349f42.wasm +0 -0
  12. package/dist/assets/tree-sitter-typescript-zxjzwt75.wasm +0 -0
  13. package/dist/assets/tree-sitter-zig-e78zbjpm.wasm +0 -0
  14. package/dist/cli/index.js +499 -0
  15. package/dist/cli/index.js.map +61 -0
  16. package/dist/daemon/index.js +670 -0
  17. package/dist/daemon/index.js.map +1475 -0
  18. package/dist/hooks/claude.js +6 -0
  19. package/dist/hooks/claude.js.map +11 -0
  20. package/dist/hooks/codex.js +10 -0
  21. package/dist/hooks/codex.js.map +11 -0
  22. package/dist/mcp/index.js +100 -0
  23. package/dist/mcp/index.js.map +231 -0
  24. package/dist/oc/index.js +21 -0
  25. package/dist/oc/index.js.map +10 -0
  26. package/package.json +16 -13
  27. package/src/api/docs/sessions.keys.ts +0 -6
  28. package/src/api/docs/sessions.ts +0 -82
  29. package/src/api/docs/system.keys.ts +0 -5
  30. package/src/api/docs/system.ts +0 -69
  31. package/src/api/docs/usage.keys.ts +0 -3
  32. package/src/api/docs/usage.ts +0 -24
  33. package/src/api/docs/webhooks.keys.ts +0 -5
  34. package/src/api/docs/webhooks.ts +0 -78
  35. package/src/api/index.ts +0 -18
  36. package/src/api/lib/auth.ts +0 -43
  37. package/src/api/lib/errors.ts +0 -28
  38. package/src/api/lib/route.ts +0 -18
  39. package/src/api/models/claude.ts +0 -4
  40. package/src/api/models/codex.ts +0 -4
  41. package/src/api/models/opencode.ts +0 -11
  42. package/src/api/routes/sessions.ts +0 -51
  43. package/src/api/routes/system.ts +0 -106
  44. package/src/api/routes/usage.ts +0 -15
  45. package/src/api/routes/webhooks/claude.ts +0 -149
  46. package/src/api/routes/webhooks/codex.ts +0 -175
  47. package/src/api/routes/webhooks/opencode.ts +0 -112
  48. package/src/api/routes.ts +0 -93
  49. package/src/api/schemas/common.ts +0 -29
  50. package/src/api/schemas/sessions.ts +0 -86
  51. package/src/api/schemas/usage.ts +0 -51
  52. package/src/api/schemas/webhooks/claude.ts +0 -32
  53. package/src/api/schemas/webhooks/codex.ts +0 -34
  54. package/src/api/schemas/webhooks/opencode.ts +0 -29
  55. package/src/api/services/claude-session.ts +0 -9
  56. package/src/api/services/codex-session.ts +0 -8
  57. package/src/api/services/opencode-session.ts +0 -73
  58. package/src/classifier.ts +0 -206
  59. package/src/cli/commands.ts +0 -714
  60. package/src/cli/index.ts +0 -18
  61. package/src/cli/program.ts +0 -175
  62. package/src/cli/prompt.ts +0 -44
  63. package/src/daemon/cli.ts +0 -27
  64. package/src/daemon/collectors/claude/README.md +0 -108
  65. package/src/daemon/collectors/claude/collector.ts +0 -423
  66. package/src/daemon/collectors/claude/discovery.ts +0 -127
  67. package/src/daemon/collectors/claude/hooks.ts +0 -181
  68. package/src/daemon/collectors/claude/index.ts +0 -8
  69. package/src/daemon/collectors/claude/jsonl.ts +0 -85
  70. package/src/daemon/collectors/claude/transcript.ts +0 -246
  71. package/src/daemon/collectors/codex/README.md +0 -147
  72. package/src/daemon/collectors/codex/collector.ts +0 -661
  73. package/src/daemon/collectors/codex/discovery.ts +0 -104
  74. package/src/daemon/collectors/codex/hooks.ts +0 -179
  75. package/src/daemon/collectors/codex/index.ts +0 -7
  76. package/src/daemon/collectors/codex/jsonl.ts +0 -108
  77. package/src/daemon/collectors/codex/transcript.ts +0 -299
  78. package/src/daemon/collectors/git.ts +0 -353
  79. package/src/daemon/collectors/herdr/collector.ts +0 -128
  80. package/src/daemon/collectors/herdr/index.ts +0 -2
  81. package/src/daemon/collectors/herdr/normalization.ts +0 -111
  82. package/src/daemon/collectors/opencode/README.md +0 -106
  83. package/src/daemon/collectors/opencode/collector.ts +0 -545
  84. package/src/daemon/collectors/opencode/discovery.ts +0 -257
  85. package/src/daemon/collectors/opencode/index.ts +0 -5
  86. package/src/daemon/collectors/opencode/session-builder.ts +0 -59
  87. package/src/daemon/collectors/opencode-db.ts +0 -294
  88. package/src/daemon/collectors/process.ts +0 -201
  89. package/src/daemon/collectors/tmux/README.md +0 -128
  90. package/src/daemon/collectors/tmux/collector.ts +0 -492
  91. package/src/daemon/collectors/tmux/enrichment.ts +0 -242
  92. package/src/daemon/collectors/tmux/index.ts +0 -4
  93. package/src/daemon/collectors/tmux/parse.ts +0 -128
  94. package/src/daemon/collectors/usage.ts +0 -259
  95. package/src/daemon/index.ts +0 -209
  96. package/src/daemon/scheduler.ts +0 -86
  97. package/src/daemon/store.ts +0 -630
  98. package/src/detect.ts +0 -143
  99. package/src/hooks/claude.ts +0 -596
  100. package/src/hooks/codex.ts +0 -517
  101. package/src/legacy/classifier.ts +0 -215
  102. package/src/legacy/detect.ts +0 -143
  103. package/src/mcp/client-server.ts +0 -21
  104. package/src/mcp/http-handler.ts +0 -126
  105. package/src/mcp/index.ts +0 -61
  106. package/src/mcp/resources/config-current.ts +0 -19
  107. package/src/mcp/resources/index.ts +0 -11
  108. package/src/mcp/resources/sessions-active.ts +0 -19
  109. package/src/mcp/resources/sessions-all.ts +0 -19
  110. package/src/mcp/schemas/sessions.ts +0 -3
  111. package/src/mcp/server.ts +0 -17
  112. package/src/mcp/services/api-adapter.ts +0 -48
  113. package/src/mcp/services/data-source.ts +0 -22
  114. package/src/mcp/services/store-adapter.ts +0 -40
  115. package/src/mcp/tools/get-active-sessions.ts +0 -16
  116. package/src/mcp/tools/get-session-children.ts +0 -24
  117. package/src/mcp/tools/get-session-messages.ts +0 -24
  118. package/src/mcp/tools/get-session.ts +0 -23
  119. package/src/mcp/tools/index.ts +0 -15
  120. package/src/mcp/tools/list-sessions.ts +0 -16
  121. package/src/oc/index.ts +0 -302
  122. package/src/service/manager.ts +0 -284
  123. package/src/shared/config.ts +0 -511
  124. package/src/shared/ipc.ts +0 -80
  125. package/src/shared/logger.ts +0 -248
  126. package/src/shared/paths.ts +0 -73
  127. package/src/shared/platform.ts +0 -110
  128. package/src/shared/session-exclusions.ts +0 -5
  129. package/src/shared/session-view.ts +0 -302
  130. package/src/shared/terminal-capabilities.ts +0 -130
  131. package/src/shared/types.ts +0 -330
  132. package/src/shared/utils.ts +0 -339
  133. package/src/shared/version.ts +0 -44
  134. package/src/tui/App.tsx +0 -240
  135. package/src/tui/client.ts +0 -163
  136. package/src/tui/components/BindingsModal.tsx +0 -82
  137. package/src/tui/components/DashboardHeader.tsx +0 -256
  138. package/src/tui/components/KeyBar.tsx +0 -67
  139. package/src/tui/components/SessionDetailModal.tsx +0 -169
  140. package/src/tui/components/SessionsView.tsx +0 -184
  141. package/src/tui/components/TabBar.tsx +0 -40
  142. package/src/tui/components/UsageSummaryPane.tsx +0 -272
  143. package/src/tui/components/UsageView.tsx +0 -242
  144. package/src/tui/components/tool-icons.ts +0 -24
  145. package/src/tui/index.tsx +0 -106
  146. package/src/tui/keybindings.ts +0 -49
  147. package/src/tui/model.ts +0 -295
  148. package/src/tui/state.ts +0 -77
  149. package/src/tui/theme.ts +0 -110
  150. package/src/types.ts +0 -113
  151. package/src/usage/README.md +0 -323
  152. package/src/usage/api.ts +0 -99
  153. package/src/usage/client.ts +0 -3
  154. package/src/usage/clients/anthropic-oauth-usage.ts +0 -258
  155. package/src/usage/clients/anthropic-web-usage.ts +0 -329
  156. package/src/usage/clients/claude-cli-probe.ts +0 -513
  157. package/src/usage/clients/codex-app-server.ts +0 -354
  158. package/src/usage/clients/codex-oauth-usage.ts +0 -240
  159. package/src/usage/clients/opencode-go-api.ts +0 -100
  160. package/src/usage/clients/opencode-go-usage.ts +0 -577
  161. package/src/usage/errors.ts +0 -33
  162. package/src/usage/index.ts +0 -20
  163. package/src/usage/local/opencode-go-local.ts +0 -194
  164. package/src/usage/mappers.ts +0 -282
  165. package/src/usage/planner.ts +0 -118
  166. package/src/usage/providers/claude.ts +0 -233
  167. package/src/usage/providers/codex.ts +0 -115
  168. package/src/usage/providers/opencode-go.ts +0 -115
  169. package/src/usage/resolver.ts +0 -4
  170. package/src/usage/resolvers/browser-cookies.ts +0 -808
  171. package/src/usage/resolvers/claude-auth.ts +0 -208
  172. package/src/usage/resolvers/claude-cli.ts +0 -24
  173. package/src/usage/resolvers/claude-desktop-auth.ts +0 -170
  174. package/src/usage/resolvers/claude-web.ts +0 -82
  175. package/src/usage/resolvers/codex-auth.ts +0 -174
  176. package/src/usage/resolvers/keychain-coordinator.ts +0 -397
  177. package/src/usage/resolvers/opencode-go-web.ts +0 -55
  178. package/src/usage/types.ts +0 -225
  179. package/src/usage/web-session-cache.ts +0 -37
  180. /package/{src/tui → dist/cli}/assets/providers/SOURCES.md +0 -0
  181. /package/{src/tui → dist/cli}/assets/providers/claude-terminal.png +0 -0
  182. /package/{src/tui → dist/cli}/assets/providers/claude-terminal.svg +0 -0
  183. /package/{src/tui → dist/cli}/assets/providers/claude.png +0 -0
  184. /package/{src/tui → dist/cli}/assets/providers/copilot.png +0 -0
  185. /package/{src/tui → dist/cli}/assets/providers/copilot.svg +0 -0
  186. /package/{src/tui → dist/cli}/assets/providers/openai-terminal.png +0 -0
  187. /package/{src/tui → dist/cli}/assets/providers/openai-terminal.svg +0 -0
  188. /package/{src/tui → dist/cli}/assets/providers/openai.png +0 -0
  189. /package/{src/tui → dist/cli}/assets/providers/opencode-terminal.png +0 -0
  190. /package/{src/tui → dist/cli}/assets/providers/opencode-terminal.svg +0 -0
  191. /package/{src/tui → dist/cli}/assets/providers/opencode.png +0 -0
  192. /package/{src/tui → dist/cli}/assets/providers/shell.png +0 -0
  193. /package/{src/tui → dist/cli}/assets/providers/shell.svg +0 -0
  194. /package/{src/tui → dist/cli}/assets/providers/unknown.png +0 -0
  195. /package/{src/tui → dist/cli}/assets/providers/unknown.svg +0 -0
@@ -1,661 +0,0 @@
1
- import { existsSync, statSync, unwatchFile, watch, watchFile } from "node:fs";
2
- import type { FSWatcher } from "node:fs";
3
- import { join, basename } from "node:path";
4
- import type { ScrylogConfig } from "../../../shared/config";
5
- import type { SessionModel } from "../../../shared/types";
6
- import type { DaemonStore } from "../../store";
7
- import type { Collector } from "../../scheduler";
8
- import type { Logger } from "../../../shared/logger";
9
- import { NOOP_LOGGER } from "../../../shared/logger";
10
- import { ensureHooksDirSafe } from "../../../shared/platform";
11
- import type { GitCollector } from "../git";
12
- import { parseJsonlFile } from "./jsonl";
13
- import { extractActiveSpawnedSubagents, extractMeta, extractState, extractSummary } from "./transcript";
14
- import { CodexHookFileData, isValidHookData, readHookData, isHookNewer, getHookTime, mergeHookIntoModel, mergeCodexMeta } from "./hooks";
15
- import { discoverTranscripts, getCodexDir } from "./discovery";
16
-
17
- interface WatchedTranscript {
18
- sessionId: string;
19
- path: string;
20
- lastModified: number;
21
- size: number;
22
- processedLastModified?: number;
23
- processedSize?: number;
24
- }
25
-
26
- interface TranscriptEntry {
27
- timestamp?: string;
28
- type?: string;
29
- payload?: {
30
- type?: string;
31
- name?: string;
32
- role?: string;
33
- content?: Array<{ type: string; text?: string }>;
34
- };
35
- }
36
-
37
- type TerminalTranscriptEvent = "task_complete" | "turn_aborted";
38
-
39
- /** Detects a request_user_input function call in the transcript.
40
- * This is the ONLY case where the transcript may override hook state,
41
- * because Codex does not fire a hook for this specific tool. */
42
- function hasRequestUserInput(entries: TranscriptEntry[]): boolean {
43
- for (let i = entries.length - 1; i >= 0; i--) {
44
- const entry = entries[i];
45
- if (entry?.type === "response_item" && entry.payload?.type === "function_call" && entry.payload?.name === "request_user_input") {
46
- return true;
47
- }
48
- // Skip turn_aborted placeholders — they are not real user responses
49
- if (entry?.type === "response_item" && entry.payload?.type === "message" && entry.payload?.role === "user") {
50
- const text = entry.payload.content?.map((c) => c.text ?? "").join("") ?? "";
51
- if (text.includes("<turn_aborted>")) continue;
52
- }
53
- // Stop at the first meaningful response_item we hit backwards
54
- if (entry?.type === "response_item") break;
55
- }
56
- return false;
57
- }
58
-
59
- function hasPendingProposedPlan(entries: TranscriptEntry[]): boolean {
60
- for (let i = entries.length - 1; i >= 0; i--) {
61
- const entry = entries[i];
62
- if (entry?.type !== "response_item") continue;
63
-
64
- if (entry.payload?.type === "message" && entry.payload?.role === "user") {
65
- const text = entry.payload.content?.map((c) => c.text ?? "").join("") ?? "";
66
- if (text.includes("<turn_aborted>")) continue;
67
- return false;
68
- }
69
-
70
- if (entry.payload?.type !== "message" || entry.payload.role !== "assistant") {
71
- return false;
72
- }
73
-
74
- const text = entry.payload.content?.map((c) => c.text ?? "").join("") ?? "";
75
- return text.trimStart().startsWith("<proposed_plan>");
76
- }
77
-
78
- return false;
79
- }
80
-
81
- function hasAnsweredRequestUserInputWork(entries: TranscriptEntry[]): boolean {
82
- let sawWorkAfterAnswer = false;
83
- let sawRequestUserInputOutput = false;
84
-
85
- for (let i = entries.length - 1; i >= 0; i--) {
86
- const entry = entries[i];
87
- if (entry?.type !== "response_item" && entry?.type !== "event_msg") continue;
88
-
89
- if (entry.type === "response_item") {
90
- const payloadType = entry.payload?.type;
91
- if (payloadType === "message" && entry.payload?.role === "user") {
92
- const text = entry.payload.content?.map((c) => c.text ?? "").join("") ?? "";
93
- if (text.includes("<turn_aborted>")) continue;
94
- return false;
95
- }
96
-
97
- if (payloadType === "function_call" && entry.payload?.name === "request_user_input") {
98
- return sawRequestUserInputOutput && sawWorkAfterAnswer;
99
- }
100
-
101
- if (payloadType === "function_call_output") {
102
- sawRequestUserInputOutput = true;
103
- continue;
104
- }
105
-
106
- if (payloadType === "reasoning" || payloadType === "function_call") {
107
- sawWorkAfterAnswer = true;
108
- continue;
109
- }
110
-
111
- if (payloadType === "message" && entry.payload?.role === "assistant") {
112
- sawWorkAfterAnswer = true;
113
- }
114
- continue;
115
- }
116
-
117
- if (entry.payload?.type === "agent_message" || entry.payload?.type === "item_completed") {
118
- sawWorkAfterAnswer = true;
119
- }
120
- }
121
-
122
- return false;
123
- }
124
-
125
- export function preserveLocationMetadata(model: SessionModel, existing?: SessionModel): SessionModel {
126
- if (!existing) return model;
127
- return {
128
- ...model,
129
- source: existing.source,
130
- project: existing.project ?? model.project,
131
- tmux: existing.tmux ?? model.tmux,
132
- herdr: existing.herdr ?? model.herdr,
133
- legacyPaneId: existing.legacyPaneId ?? model.legacyPaneId,
134
- processTree: existing.processTree ?? model.processTree,
135
- processChain: existing.processChain ?? model.processChain,
136
- sample: existing.sample ?? model.sample,
137
- toolReasons: existing.toolReasons ?? model.toolReasons,
138
- stateReasons: existing.stateReasons ?? model.stateReasons,
139
- stableForMs: existing.stableForMs ?? model.stableForMs,
140
- currentCommand: existing.currentCommand ?? model.currentCommand,
141
- currentPath: existing.currentPath ?? model.currentPath,
142
- panePid: existing.panePid ?? model.panePid,
143
- tty: existing.tty ?? model.tty,
144
- paneTitle: existing.paneTitle ?? model.paneTitle,
145
- };
146
- }
147
-
148
- function findTerminalEventAfterHook(entries: TranscriptEntry[], hookEventTime?: number): TerminalTranscriptEvent | null {
149
- if (!hookEventTime) return null;
150
-
151
- for (let i = entries.length - 1; i >= 0; i--) {
152
- const entry = entries[i];
153
- if (entry?.type !== "event_msg") continue;
154
- const eventType = entry.payload?.type;
155
- if (eventType !== "task_complete" && eventType !== "turn_aborted") continue;
156
-
157
- const timestamp = entry.timestamp ? Date.parse(entry.timestamp) : Number.NaN;
158
- if (!Number.isFinite(timestamp) || timestamp <= hookEventTime) return null;
159
- return eventType;
160
- }
161
-
162
- return null;
163
- }
164
-
165
- export class CodexCollector implements Collector {
166
- name = "codex";
167
- private store?: DaemonStore;
168
- private config?: ScrylogConfig;
169
- private logger: Logger = NOOP_LOGGER;
170
- private watched = new Map<string, WatchedTranscript>();
171
- private hookWatched = new Set<string>();
172
- private hookDirWatcher?: FSWatcher;
173
- private lastProcessedSeq = new Map<string, number>();
174
- private discoveryTimer?: ReturnType<typeof setInterval>;
175
- private fallbackTimer?: ReturnType<typeof setInterval>;
176
-
177
- constructor(private readonly gitCollector?: GitCollector) {}
178
-
179
- async start(store: DaemonStore, config: ScrylogConfig, logger: Logger): Promise<void> {
180
- this.store = store;
181
- this.config = config;
182
- this.logger = logger;
183
-
184
- const codexDir = getCodexDir(config);
185
- logger.info("codex", "Collector started", { event: "collector.started", collector: this.name, codexDir, hooksEnabled: config.codex.hooksEnabled });
186
-
187
- if (config.codex.hooksEnabled) {
188
- this.startHooksDirWatcher(config.codex.hooksDir);
189
- }
190
-
191
- this.run();
192
- this.discoveryTimer = setInterval(() => this.run(), 30_000);
193
- this.fallbackTimer = setInterval(() => this.rereadWatched(), 5_000);
194
- }
195
-
196
- async stop(): Promise<void> {
197
- if (this.discoveryTimer) clearInterval(this.discoveryTimer);
198
- if (this.fallbackTimer) clearInterval(this.fallbackTimer);
199
-
200
- for (const { path } of this.watched.values()) {
201
- try { unwatchFile(path); } catch { /* ignore */ }
202
- }
203
- this.watched.clear();
204
-
205
- for (const hookFile of this.hookWatched) {
206
- try { unwatchFile(hookFile); } catch { /* ignore */ }
207
- }
208
- this.hookWatched.clear();
209
- this.lastProcessedSeq.clear();
210
-
211
- if (this.hookDirWatcher) {
212
- try { this.hookDirWatcher.close(); } catch { /* ignore */ }
213
- this.hookDirWatcher = undefined;
214
- }
215
-
216
- this.logger.info("codex", "Collector stopped", { event: "collector.stopped", collector: this.name });
217
- }
218
-
219
- // ── Internal ───────────────────────────────────────────────────────────────
220
-
221
- private run(): void {
222
- const store = this.store!;
223
- const config = this.config!;
224
- const codexDir = getCodexDir(config);
225
-
226
- // Build set of session IDs that are hook-managed, so discoverTranscripts
227
- // can include their transcripts even past the 24h cutoff.
228
- const hookManagedIds = new Set<string>();
229
- for (const session of store.getSessions()) {
230
- if (session.detectionSource === "codex-hook" && session.tool === "codex") {
231
- hookManagedIds.add(session.codex?.sessionId ?? session.id.replace(/^codex:/, ""));
232
- }
233
- }
234
-
235
- const transcripts = discoverTranscripts(codexDir, hookManagedIds);
236
-
237
- this.logger.debug("codex", "Discovery cycle", {
238
- event: "collector.discovery_completed",
239
- collector: this.name,
240
- codexDir,
241
- transcripts: transcripts.length,
242
- });
243
-
244
- for (const transcript of transcripts) {
245
- this.processTranscript(transcript);
246
- }
247
-
248
- const trackedPaths = new Set(transcripts.map((t) => t.path));
249
- for (const path of trackedPaths) {
250
- if (!this.watched.has(path)) {
251
- const transcript = transcripts.find((t) => t.path === path);
252
- if (!transcript) continue;
253
- this.watched.set(path, transcript);
254
- this.logger.debug("codex", "Watching transcript", { sessionId: transcript.sessionId, path });
255
- watchFile(path, { interval: 2000 }, () => {
256
- const t = this.watched.get(path);
257
- if (t) this.processTranscript(t);
258
- });
259
- }
260
- }
261
-
262
- for (const [path, transcript] of this.watched) {
263
- if (!trackedPaths.has(path)) {
264
- try { unwatchFile(path); } catch { /* ignore */ }
265
- this.watched.delete(path);
266
- this.logger.debug("codex", "Stopped watching transcript", { sessionId: transcript.sessionId });
267
- // Don't remove hook-managed sessions — they have an authoritative lifecycle
268
- // managed by hooks. Only remove transcript-sourced sessions.
269
- const existingSession = store.getSession(`codex:${transcript.sessionId}`);
270
- if (existingSession?.detectionSource !== "codex-hook" && existingSession?.source === "standalone") {
271
- store.removeSession(`codex:${transcript.sessionId}`);
272
- }
273
- }
274
- }
275
-
276
- store.setCollectorStatus(this.name, { running: true, lastRunAt: Date.now() });
277
- }
278
-
279
- private processTranscript(transcript: import("./discovery").DiscoveredTranscript): void {
280
- const store = this.store!;
281
- const config = this.config!;
282
- const transcriptMaxBytes = config.retention?.transcriptMaxBytes;
283
- const entries = parseJsonlFile(transcript.path, transcriptMaxBytes);
284
- const meta = extractMeta(entries);
285
- const sessionId = meta.sessionId || transcript.sessionId;
286
- const storeId = `codex:${sessionId}`;
287
- const existingSession = store.getSession(storeId);
288
- const watched = this.watched.get(transcript.path);
289
- if (
290
- watched?.processedLastModified === transcript.lastModified
291
- && watched.processedSize === transcript.size
292
- && existingSession
293
- ) {
294
- return;
295
- }
296
-
297
- const transcriptState = extractState(entries);
298
- const transcriptSummary = extractSummary(entries);
299
-
300
- // Transcript-based state: detect questions and agent messages
301
- let model: SessionModel = {
302
- id: `codex:${transcript.sessionId}`,
303
- source: "standalone",
304
- tool: "codex",
305
- state: transcriptState,
306
- summary: transcriptSummary ?? (meta.sessionId ? `Codex session` : "Codex session"),
307
- lastActivity: transcript.lastModified,
308
- codex: {
309
- sessionId,
310
- transcriptPath: transcript.path,
311
- ...(meta.model ? { model: meta.model } : {}),
312
- ...(meta.forkedFromId ? { forkedFromId: meta.forkedFromId } : {}),
313
- ...(meta.parentSessionId ? { parentSessionId: meta.parentSessionId } : {}),
314
- ...(meta.agentNickname ? { agentNickname: meta.agentNickname } : {}),
315
- ...(meta.agentRole ? { agentRole: meta.agentRole } : {}),
316
- },
317
- ...(meta.parentSessionId ? { parentSessionId: meta.parentSessionId } : {}),
318
- hasAttention: transcriptState === "waiting-input",
319
- detectionSource: "codex-transcript",
320
- };
321
- model.id = storeId;
322
- if (meta.agentNickname && !transcriptSummary) {
323
- model.summary = `Codex sub-agent ${meta.agentNickname}`;
324
- }
325
-
326
- // Hooks are the authoritative source of state for Codex sessions.
327
- // Transcripts enrich metadata (model, subagents, transcriptPath) but
328
- // never override state, hasAttention, or activeTool.
329
- //
330
- // EXCEPTION: Codex does not fire a hook for request_user_input. When the
331
- // transcript detects this specific tool call, it may override the hook state
332
- // to waiting-input. This is the ONLY transcript override permitted.
333
- const transcriptHasRequestUserInput = transcriptState === "waiting-input" && hasRequestUserInput(entries);
334
- const transcriptHasProposedPlan = hasPendingProposedPlan(entries);
335
- const transcriptIndicatesWaitingInput = transcriptHasRequestUserInput || transcriptHasProposedPlan;
336
- const transcriptIndicatesWorking = !transcriptIndicatesWaitingInput && hasAnsweredRequestUserInputWork(entries);
337
- const transcriptOverridesHook = existingSession?.detectionSource === "codex-hook" && transcriptIndicatesWaitingInput;
338
- const terminalTranscriptEvent = existingSession?.detectionSource === "codex-hook"
339
- ? findTerminalEventAfterHook(entries, existingSession.codex?.hookEventTime)
340
- : null;
341
- const terminalOverridesHook = Boolean(terminalTranscriptEvent && !transcriptIndicatesWaitingInput && !transcriptIndicatesWorking);
342
-
343
- // Check for hook data first — hooks are always authoritative when present.
344
- let hookDataFromFile: Partial<SessionModel> | null = null;
345
- if (config.codex.hooksEnabled) {
346
- const fileHookData = readHookData(config.codex.hooksDir, sessionId) ?? readHookData(config.codex.hooksDir, transcript.sessionId);
347
- if (fileHookData && getHookTime(fileHookData) >= transcript.lastModified && isHookNewer(existingSession, fileHookData)) {
348
- hookDataFromFile = fileHookData;
349
- }
350
- }
351
-
352
- if (hookDataFromFile) {
353
- // Hook data exists and is authoritative — migrate to codex-hook if needed.
354
- model = mergeHookIntoModel(model, hookDataFromFile, transcript.path);
355
- model = preserveLocationMetadata(model, existingSession);
356
- } else if (existingSession?.detectionSource === "codex-hook") {
357
- // Hook-managed session with no fresh hook data. Transcript may override
358
- // only for request_user_input (the one case Codex does not fire a hook).
359
- const resolvedState = transcriptOverridesHook ? "waiting-input"
360
- : transcriptIndicatesWorking ? "working"
361
- : terminalOverridesHook ? "idle"
362
- : existingSession.state;
363
- const resolvedHasAttention = transcriptOverridesHook ? true
364
- : transcriptIndicatesWorking ? false
365
- : terminalOverridesHook ? false
366
- : existingSession.hasAttention;
367
- const resolvedSummary = transcriptOverridesHook
368
- ? (extractSummary(entries) ?? existingSession.summary)
369
- : (existingSession.summary || model.summary);
370
-
371
- model = {
372
- ...model,
373
- state: resolvedState,
374
- hasAttention: resolvedHasAttention,
375
- summary: resolvedSummary,
376
- activeTool: (terminalOverridesHook || transcriptIndicatesWorking) ? undefined : existingSession.activeTool,
377
- detectionSource: "codex-hook",
378
- codex: mergeCodexMeta(
379
- {
380
- sessionId: existingSession.codex?.sessionId ?? transcript.sessionId,
381
- transcriptPath: existingSession.codex?.transcriptPath || transcript.path,
382
- ...(meta.model ? { model: meta.model } : {}),
383
- ...(meta.forkedFromId ? { forkedFromId: meta.forkedFromId } : {}),
384
- ...(meta.parentSessionId ? { parentSessionId: meta.parentSessionId } : {}),
385
- ...(meta.agentNickname ? { agentNickname: meta.agentNickname } : {}),
386
- ...(meta.agentRole ? { agentRole: meta.agentRole } : {}),
387
- ...(transcriptOverridesHook ? { attentionKind: "question" } : {}),
388
- ...(terminalOverridesHook ? { attentionKind: "none" } : {}),
389
- },
390
- existingSession.codex ?? {},
391
- ),
392
- ...(existingSession.parentSessionId ?? meta.parentSessionId ? { parentSessionId: existingSession.parentSessionId ?? meta.parentSessionId } : {}),
393
- lastActivity: existingSession.lastActivity,
394
- };
395
- model = preserveLocationMetadata(model, existingSession);
396
- } else if (existingSession?.detectionSource === "codex-transcript" && transcriptIndicatesWaitingInput) {
397
- // Transcript-only session where the agent is waiting for user input.
398
- // Codex did not fire a hook for this state, so the transcript is the
399
- // authoritative source. When a hook finally arrives, detectionSource
400
- // migrates to codex-hook automatically via the hook handlers.
401
- model = {
402
- ...model,
403
- state: "waiting-input",
404
- hasAttention: true,
405
- summary: extractSummary(entries) ?? model.summary,
406
- detectionSource: "codex-transcript",
407
- codex: mergeCodexMeta(
408
- {
409
- sessionId: existingSession.codex?.sessionId ?? transcript.sessionId,
410
- transcriptPath: existingSession.codex?.transcriptPath || transcript.path,
411
- ...(meta.model ? { model: meta.model } : {}),
412
- ...(meta.forkedFromId ? { forkedFromId: meta.forkedFromId } : {}),
413
- ...(meta.parentSessionId ? { parentSessionId: meta.parentSessionId } : {}),
414
- ...(meta.agentNickname ? { agentNickname: meta.agentNickname } : {}),
415
- ...(meta.agentRole ? { agentRole: meta.agentRole } : {}),
416
- attentionKind: "question",
417
- },
418
- existingSession.codex ?? {},
419
- ),
420
- ...(existingSession.parentSessionId ?? meta.parentSessionId ? { parentSessionId: existingSession.parentSessionId ?? meta.parentSessionId } : {}),
421
- };
422
- model = preserveLocationMetadata(model, existingSession);
423
- } else if (!existingSession && transcriptIndicatesWaitingInput) {
424
- model = {
425
- ...model,
426
- state: "waiting-input",
427
- hasAttention: true,
428
- codex: {
429
- sessionId,
430
- transcriptPath: transcript.path,
431
- ...(model.codex ?? {}),
432
- attentionKind: "question",
433
- },
434
- };
435
- }
436
-
437
- if (existingSession && existingSession.detectionSource !== "codex-hook" && existingSession.detectionSource !== "codex-transcript") {
438
- model = preserveLocationMetadata(model, existingSession);
439
- }
440
-
441
- // Resolve git project info from currentPath (populated by hooks)
442
- if (this.gitCollector && model.currentPath && !model.project) {
443
- const project = this.gitCollector.resolve(model.currentPath);
444
- if (project) model = { ...model, project };
445
- }
446
-
447
- store.upsertSession(model);
448
- this.upsertActiveSpawnedSubagents(model, extractActiveSpawnedSubagents(entries));
449
- const currentWatched = this.watched.get(transcript.path);
450
- if (currentWatched) {
451
- this.watched.set(transcript.path, {
452
- ...currentWatched,
453
- lastModified: transcript.lastModified,
454
- size: transcript.size,
455
- processedLastModified: transcript.lastModified,
456
- processedSize: transcript.size,
457
- });
458
- }
459
- this.logger.debug("codex", "Processed transcript", { event: "transcript.processed", sessionId, state: model.state, detectionSource: model.detectionSource, path: transcript.path });
460
-
461
- this.syncChildSessionIds();
462
- }
463
-
464
- private upsertActiveSpawnedSubagents(parent: SessionModel, spawned: ReturnType<typeof extractActiveSpawnedSubagents>): void {
465
- for (const subagent of spawned) {
466
- const id = `codex:${subagent.agentId}`;
467
- const existing = this.store!.getSession(id);
468
- if (existing) {
469
- if (existing.state === "working" || existing.state === "waiting-input") continue;
470
- this.store!.upsertSession({
471
- ...existing,
472
- state: "working",
473
- lastActivity: Math.max(existing.lastActivity, parent.lastActivity),
474
- parentSessionId: existing.parentSessionId ?? parent.id,
475
- codex: mergeCodexMeta(
476
- {
477
- sessionId: existing.codex?.sessionId ?? subagent.agentId,
478
- transcriptPath: existing.codex?.transcriptPath ?? "",
479
- },
480
- {
481
- ...(existing.codex ?? {}),
482
- agentId: existing.codex?.agentId ?? subagent.agentId,
483
- ...(subagent.agentType ? { agentType: subagent.agentType } : {}),
484
- ...(subagent.nickname ? { agentNickname: subagent.nickname } : {}),
485
- parentSessionId: existing.parentSessionId ?? parent.id,
486
- },
487
- ),
488
- });
489
- continue;
490
- }
491
- this.store!.upsertSession({
492
- id,
493
- source: "standalone",
494
- tool: "codex",
495
- state: "working",
496
- summary: subagent.nickname ? `Codex sub-agent ${subagent.nickname}` : "Codex sub-agent",
497
- lastActivity: parent.lastActivity,
498
- parentSessionId: parent.id,
499
- detectionSource: "codex-transcript",
500
- codex: {
501
- sessionId: subagent.agentId,
502
- transcriptPath: "",
503
- agentId: subagent.agentId,
504
- ...(subagent.agentType ? { agentType: subagent.agentType } : {}),
505
- ...(subagent.nickname ? { agentNickname: subagent.nickname } : {}),
506
- parentSessionId: parent.id,
507
- },
508
- });
509
- }
510
- }
511
-
512
- /** Re-process all watched transcripts (fallback polling) */
513
- private rereadWatched(): void {
514
- for (const transcript of this.watched.values()) {
515
- try {
516
- const stat = statSync(transcript.path);
517
- this.processTranscript({ ...transcript, lastModified: stat.mtimeMs, size: stat.size });
518
- } catch {
519
- // File may have been removed
520
- }
521
- }
522
- }
523
-
524
- /** Watch the hooks directory for new/changed files so state updates are near-immediate */
525
- private startHooksDirWatcher(hooksDir: string): void {
526
- try {
527
- ensureHooksDirSafe(hooksDir);
528
- } catch {
529
- // Directory may be unwritable or fail ownership/permission validation — handled below
530
- }
531
-
532
- if (!existsSync(hooksDir)) {
533
- this.logger.debug("codex", "Hooks dir not found — skipping watcher", { hooksDir });
534
- return;
535
- }
536
-
537
- // Watch for new files appearing in the hooks dir
538
- try {
539
- this.hookDirWatcher = watch(hooksDir, (eventType, filename) => {
540
- if (!filename || !filename.endsWith(".json")) return;
541
- const sessionId = basename(filename, ".json");
542
- this.onHookFileChanged(hooksDir, sessionId, filename);
543
- });
544
- } catch {
545
- this.logger.debug("codex", "Could not watch hooks dir", { hooksDir });
546
- }
547
- }
548
-
549
- /** Called when a hook file is created or modified */
550
- private onHookFileChanged(hooksDir: string, sessionId: string, filename: string): void {
551
- const hookFile = join(hooksDir, filename);
552
-
553
- // Start watching this specific file if not already
554
- if (!this.hookWatched.has(hookFile)) {
555
- this.hookWatched.add(hookFile);
556
- watchFile(hookFile, { interval: 500 }, () => {
557
- this.onHookFileChanged(hooksDir, sessionId, filename);
558
- });
559
- }
560
-
561
- // Skip duplicate processing: if the hookSeq hasn't changed, this is a
562
- // redundant event from the directory watcher and per-file watchFile.
563
- const hookData = readHookData(hooksDir, sessionId);
564
- if (!hookData) {
565
- // Hook file was deleted (Stop with stop_hook_active=false) — clean up watcher
566
- this.unwatchHookFile(hookFile);
567
- return;
568
- }
569
- const incomingSeq = hookData.codex?.hookSeq ?? 0;
570
- const lastSeq = this.lastProcessedSeq.get(sessionId) ?? -1;
571
- if (incomingSeq <= lastSeq) return;
572
- this.lastProcessedSeq.set(sessionId, incomingSeq);
573
-
574
- this.logger.debug("codex", "Hook received — refreshing session", { event: "integration.event_received", sessionId, hookSeq: incomingSeq });
575
-
576
- // Find the corresponding transcript to re-process
577
- const transcriptEntry = [...this.watched.values()].find((t) => t.sessionId === sessionId);
578
- if (transcriptEntry) {
579
- try {
580
- const stat = statSync(transcriptEntry.path);
581
- this.processTranscript({ ...transcriptEntry, lastModified: stat.mtimeMs, size: stat.size });
582
- } catch {
583
- // Transcript may have been removed
584
- }
585
- return;
586
- }
587
-
588
- // The transcript may not be watched yet (e.g. a session created via hook before transcript is discovered).
589
- // hookData was already read above for dedup — use it to create a minimal session entry.
590
-
591
- const existing = this.store!.getSession(`codex:${sessionId}`);
592
- if (existing && !isHookNewer(existing, hookData)) {
593
- return;
594
- }
595
- if (existing) {
596
- // Merge hook data into existing session
597
- const mergedSummary = hookData.summary ?? existing.summary;
598
- this.store!.upsertSession({
599
- ...existing,
600
- ...hookData,
601
- id: existing.id,
602
- source: existing.source,
603
- summary: mergedSummary,
604
- lastActivity: (hookData.lastActivity as number | undefined) ?? existing.lastActivity,
605
- activeTool: hookData.activeTool ?? existing.activeTool,
606
- codex: mergeCodexMeta(
607
- {
608
- sessionId,
609
- transcriptPath: (hookData.codex?.transcriptPath || existing.codex?.transcriptPath || "") as string,
610
- },
611
- hookData.codex ?? {},
612
- ),
613
- });
614
- this.logger.debug("codex", "Hook merged into session (no transcript)", { event: "session.upsert_from_hook", sessionId, state: hookData.state });
615
- } else {
616
- // Minimal stub for a session discovered only via hook
617
- const currentPath = hookData.currentPath as string | undefined;
618
- const project = (this.gitCollector && currentPath)
619
- ? this.gitCollector.resolve(currentPath) ?? undefined
620
- : undefined;
621
- const stub: SessionModel = {
622
- id: `codex:${sessionId}`,
623
- source: "standalone",
624
- tool: "codex",
625
- state: (hookData.state as SessionModel["state"]) ?? "idle",
626
- summary: hookData.summary ?? "Codex session",
627
- lastActivity: (hookData.lastActivity as number) ?? Date.now(),
628
- hasAttention: hookData.hasAttention,
629
- activeTool: hookData.activeTool,
630
- currentPath,
631
- project,
632
- parentSessionId: hookData.parentSessionId,
633
- detectionSource: "codex-hook",
634
- codex: mergeCodexMeta(
635
- {
636
- sessionId,
637
- transcriptPath: (hookData.codex?.transcriptPath ?? "") as string,
638
- },
639
- hookData.codex ?? {},
640
- ),
641
- };
642
- this.store!.upsertSession(stub);
643
- this.logger.debug("codex", "Session created from hook", { event: "session.upsert_from_hook", sessionId, parentSessionId: hookData.parentSessionId, state: stub.state });
644
- }
645
-
646
- this.syncChildSessionIds();
647
- }
648
-
649
- /** Stop watching a per-file watcher and clean up tracking state */
650
- private unwatchHookFile(hookFile: string): void {
651
- if (this.hookWatched.has(hookFile)) {
652
- this.hookWatched.delete(hookFile);
653
- try { unwatchFile(hookFile); } catch { /* ignore */ }
654
- }
655
- }
656
-
657
- /** Rebuild childSessionIds on parent sessions after any session upsert */
658
- private syncChildSessionIds(): void {
659
- this.store!.syncChildSessionIds();
660
- }
661
- }