@agent-watch-ai/edge 0.5.0 → 0.7.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 (298) hide show
  1. package/CHANGELOG.md +238 -7
  2. package/README.md +308 -8
  3. package/dist/billing/billing-mode.d.ts +37 -5
  4. package/dist/billing/billing-mode.js +28 -19
  5. package/dist/billing/constants/billing.constants.d.ts +0 -2
  6. package/dist/billing/constants/billing.constants.js +0 -2
  7. package/dist/billing/index.d.ts +2 -1
  8. package/dist/billing/index.js +1 -1
  9. package/dist/billing/session-billing.d.ts +3 -2
  10. package/dist/billing/session-billing.js +8 -8
  11. package/dist/billing/types/billing.types.d.ts +7 -0
  12. package/dist/capture/capture-env.d.ts +147 -0
  13. package/dist/capture/capture-env.js +301 -0
  14. package/dist/capture/captured-step.d.ts +88 -0
  15. package/dist/capture/captured-step.js +479 -0
  16. package/dist/capture/constants/capture.constants.d.ts +70 -0
  17. package/dist/capture/constants/capture.constants.js +72 -0
  18. package/dist/capture/launched-runs.d.ts +16 -0
  19. package/dist/capture/launched-runs.js +151 -0
  20. package/dist/capture/node.cjs +697 -0
  21. package/dist/capture/node.d.cts +32 -0
  22. package/dist/capture/python/sitecustomize.py +823 -0
  23. package/dist/capture/schemas/capture.schema.d.ts +35 -0
  24. package/dist/capture/schemas/capture.schema.js +31 -0
  25. package/dist/capture/types/capture.types.d.ts +20 -0
  26. package/dist/cli/constants/cli.constants.d.ts +2 -6
  27. package/dist/cli/constants/cli.constants.js +3 -7
  28. package/dist/cli/context.d.ts +19 -22
  29. package/dist/cli/context.js +23 -54
  30. package/dist/cli/doctor-run-capture.d.ts +13 -0
  31. package/dist/cli/doctor-run-capture.js +19 -0
  32. package/dist/cli/doctor.js +16 -67
  33. package/dist/cli/hook.d.ts +14 -0
  34. package/dist/cli/hook.js +40 -0
  35. package/dist/cli/index.d.ts +1 -1
  36. package/dist/cli/index.js +1 -1
  37. package/dist/cli/misc.js +10 -2
  38. package/dist/cli/replay.d.ts +33 -0
  39. package/dist/cli/replay.js +193 -0
  40. package/dist/cli/setup.d.ts +10 -2
  41. package/dist/cli/setup.js +154 -26
  42. package/dist/cli/status.js +45 -5
  43. package/dist/cli/toggle.js +10 -0
  44. package/dist/cli/types/cli.types.d.ts +15 -1
  45. package/dist/cli/uninstall.js +8 -22
  46. package/dist/cli.js +104 -17
  47. package/dist/config/config.d.ts +0 -2
  48. package/dist/config/config.js +0 -1
  49. package/dist/config/constants/config.constants.js +2 -0
  50. package/dist/config/events-transport.d.ts +15 -0
  51. package/dist/config/events-transport.js +29 -0
  52. package/dist/config/index.d.ts +2 -1
  53. package/dist/config/index.js +1 -0
  54. package/dist/config/root-config.d.ts +12 -0
  55. package/dist/config/root-config.js +24 -0
  56. package/dist/config/schemas/config.schema.d.ts +13 -0
  57. package/dist/config/schemas/config.schema.js +42 -6
  58. package/dist/config/types/config.types.d.ts +7 -0
  59. package/dist/core/index.d.ts +1 -1
  60. package/dist/core/index.js +1 -1
  61. package/dist/core/version.d.ts +9 -0
  62. package/dist/core/version.js +14 -0
  63. package/dist/core/which.d.ts +11 -0
  64. package/dist/core/which.js +18 -0
  65. package/dist/credentials/cloud-claim.d.ts +32 -0
  66. package/dist/credentials/cloud-claim.js +42 -0
  67. package/dist/credentials/constants/credentials.constants.d.ts +8 -1
  68. package/dist/credentials/constants/credentials.constants.js +9 -2
  69. package/dist/credentials/device-api.d.ts +15 -1
  70. package/dist/credentials/device-api.js +12 -1
  71. package/dist/credentials/sign-in.d.ts +27 -0
  72. package/dist/credentials/sign-in.js +31 -1
  73. package/dist/credentials/token-refresher.d.ts +5 -1
  74. package/dist/credentials/token-refresher.js +10 -2
  75. package/dist/events/constants/events.constants.d.ts +8 -2
  76. package/dist/events/constants/events.constants.js +7 -1
  77. package/dist/events/index.d.ts +2 -0
  78. package/dist/events/product-event.d.ts +10 -4
  79. package/dist/events/types/events.types.d.ts +6 -0
  80. package/dist/events/types/replay-progress.types.d.ts +26 -0
  81. package/dist/events/types/replay-progress.types.js +1 -0
  82. package/dist/events/types/repo-snapshot.types.d.ts +1 -4
  83. package/dist/events/types/repo-tree.types.d.ts +25 -0
  84. package/dist/events/types/repo-tree.types.js +1 -0
  85. package/dist/feature/ticket-candidates.d.ts +1 -1
  86. package/dist/git/checkout-root.d.ts +15 -0
  87. package/dist/git/checkout-root.js +18 -0
  88. package/dist/git/code-state.d.ts +2 -16
  89. package/dist/git/code-state.js +11 -12
  90. package/dist/git/constants/git.constants.d.ts +31 -11
  91. package/dist/git/constants/git.constants.js +36 -12
  92. package/dist/git/constants/tree.constants.d.ts +71 -0
  93. package/dist/git/constants/tree.constants.js +92 -0
  94. package/dist/git/fingerprint.d.ts +3 -6
  95. package/dist/git/fingerprint.js +5 -8
  96. package/dist/git/git-context.d.ts +1 -0
  97. package/dist/git/git-context.js +8 -3
  98. package/dist/git/index.d.ts +1 -2
  99. package/dist/git/index.js +1 -1
  100. package/dist/git/repo-snapshot.d.ts +13 -4
  101. package/dist/git/repo-snapshot.js +47 -9
  102. package/dist/git/repo-tree.d.ts +69 -0
  103. package/dist/git/repo-tree.js +130 -0
  104. package/dist/git/types/git.types.d.ts +5 -2
  105. package/dist/git/types/snapshot.types.d.ts +14 -0
  106. package/dist/pipeline/constants/pipeline.constants.d.ts +3 -0
  107. package/dist/pipeline/constants/pipeline.constants.js +3 -0
  108. package/dist/pipeline/hook-pipeline.js +112 -72
  109. package/dist/privacy/product-capture.js +36 -5
  110. package/dist/providers/antigravity/antigravity.capture.d.ts +2 -0
  111. package/dist/providers/antigravity/antigravity.capture.js +5 -0
  112. package/dist/providers/antigravity/antigravity.detect.js +8 -22
  113. package/dist/providers/antigravity/antigravity.provider.js +10 -3
  114. package/dist/providers/antigravity/constants/antigravity.constants.d.ts +1 -0
  115. package/dist/providers/antigravity/constants/antigravity.constants.js +1 -0
  116. package/dist/providers/claude/claude.adapter.js +9 -6
  117. package/dist/providers/claude/claude.detect.js +2 -17
  118. package/dist/providers/claude/claude.otel.d.ts +21 -3
  119. package/dist/providers/claude/claude.otel.js +216 -61
  120. package/dist/providers/claude/claude.provider.js +17 -1
  121. package/dist/providers/claude/claude.version.d.ts +9 -0
  122. package/dist/providers/claude/claude.version.js +28 -0
  123. package/dist/providers/claude/constants/claude.constants.d.ts +27 -0
  124. package/dist/providers/claude/constants/claude.constants.js +27 -0
  125. package/dist/providers/claude/constants/claude.otel.constants.d.ts +27 -0
  126. package/dist/providers/claude/constants/claude.otel.constants.js +45 -5
  127. package/dist/providers/claude/types/claude.types.d.ts +13 -0
  128. package/dist/providers/codex/codex.capture.d.ts +76 -0
  129. package/dist/providers/codex/codex.capture.js +454 -0
  130. package/dist/providers/codex/codex.detect.js +2 -4
  131. package/dist/providers/codex/codex.otel.d.ts +38 -2
  132. package/dist/providers/codex/codex.otel.js +50 -6
  133. package/dist/providers/codex/codex.provider.js +8 -0
  134. package/dist/providers/codex/codex.surface.d.ts +9 -0
  135. package/dist/{turns/codex-originator.js → providers/codex/codex.surface.js} +17 -3
  136. package/dist/providers/codex/codex.titles.d.ts +14 -0
  137. package/dist/providers/codex/codex.titles.js +35 -0
  138. package/dist/providers/codex/constants/codex.constants.d.ts +4 -0
  139. package/dist/providers/codex/constants/codex.constants.js +4 -0
  140. package/dist/providers/cursor/constants/cursor.constants.d.ts +17 -4
  141. package/dist/providers/cursor/constants/cursor.constants.js +25 -5
  142. package/dist/providers/cursor/cursor-mcp-memo.d.ts +34 -0
  143. package/dist/providers/cursor/cursor-mcp-memo.js +94 -0
  144. package/dist/providers/cursor/cursor.adapter.js +20 -82
  145. package/dist/providers/cursor/cursor.capture.d.ts +1 -0
  146. package/dist/providers/cursor/cursor.capture.js +4 -0
  147. package/dist/providers/cursor/cursor.detect.d.ts +5 -1
  148. package/dist/providers/cursor/cursor.detect.js +11 -26
  149. package/dist/providers/cursor/cursor.hooks.js +2 -2
  150. package/dist/providers/cursor/cursor.provider.js +16 -7
  151. package/dist/providers/cursor/cursor.titles.d.ts +14 -0
  152. package/dist/providers/cursor/cursor.titles.js +20 -0
  153. package/dist/providers/cursor/schemas/cursor.schema.d.ts +3 -3
  154. package/dist/providers/cursor/schemas/cursor.schema.js +5 -3
  155. package/dist/providers/gemini/constants/gemini.constants.d.ts +16 -0
  156. package/dist/providers/gemini/constants/gemini.constants.js +17 -0
  157. package/dist/providers/gemini/gemini.capture.d.ts +81 -0
  158. package/dist/providers/gemini/gemini.capture.js +210 -0
  159. package/dist/providers/gemini/gemini.detect.js +57 -24
  160. package/dist/providers/gemini/gemini.otel.d.ts +38 -2
  161. package/dist/providers/gemini/gemini.otel.js +56 -125
  162. package/dist/providers/gemini/gemini.provider.js +5 -0
  163. package/dist/providers/index.d.ts +1 -0
  164. package/dist/providers/index.js +1 -0
  165. package/dist/providers/provider.d.ts +8 -0
  166. package/dist/providers/provider.js +9 -0
  167. package/dist/providers/registry.d.ts +14 -0
  168. package/dist/providers/registry.js +14 -0
  169. package/dist/providers/shared/constants/hook-config.constants.d.ts +2 -0
  170. package/dist/providers/shared/constants/hook-config.constants.js +2 -0
  171. package/dist/providers/shared/constants/terminal-capture.constants.d.ts +6 -0
  172. package/dist/providers/shared/constants/terminal-capture.constants.js +6 -0
  173. package/dist/providers/shared/editor-paths.d.ts +8 -0
  174. package/dist/providers/shared/editor-paths.js +18 -0
  175. package/dist/providers/shared/hook-config.d.ts +5 -0
  176. package/dist/providers/shared/hook-config.js +39 -1
  177. package/dist/providers/shared/hooks-marker.d.ts +14 -0
  178. package/dist/providers/shared/hooks-marker.js +23 -0
  179. package/dist/providers/shared/install-record.d.ts +29 -0
  180. package/dist/providers/shared/install-record.js +52 -0
  181. package/dist/providers/shared/session-title-store.d.ts +23 -0
  182. package/dist/providers/shared/session-title-store.js +50 -0
  183. package/dist/providers/shared/terminal-capture.d.ts +41 -0
  184. package/dist/providers/shared/terminal-capture.js +109 -0
  185. package/dist/providers/shared/tooling.d.ts +21 -0
  186. package/dist/providers/shared/tooling.js +36 -1
  187. package/dist/providers/types/provider.types.d.ts +57 -4
  188. package/dist/replay/checkout-resolver.d.ts +29 -0
  189. package/dist/replay/checkout-resolver.js +69 -0
  190. package/dist/replay/codex-turns.d.ts +24 -0
  191. package/dist/replay/codex-turns.js +221 -0
  192. package/dist/replay/constants/replay.constants.d.ts +99 -0
  193. package/dist/replay/constants/replay.constants.js +102 -0
  194. package/dist/replay/describe.d.ts +10 -0
  195. package/dist/replay/describe.js +31 -0
  196. package/dist/replay/gemini-turns.d.ts +36 -0
  197. package/dist/replay/gemini-turns.js +192 -0
  198. package/dist/replay/machine.d.ts +34 -0
  199. package/dist/replay/machine.js +68 -0
  200. package/dist/replay/progress-event.d.ts +21 -0
  201. package/dist/replay/progress-event.js +45 -0
  202. package/dist/replay/replay-runner.d.ts +58 -0
  203. package/dist/replay/replay-runner.js +529 -0
  204. package/dist/replay/replay-scan.d.ts +83 -0
  205. package/dist/replay/replay-scan.js +170 -0
  206. package/dist/replay/replay-state.d.ts +108 -0
  207. package/dist/replay/replay-state.js +199 -0
  208. package/dist/replay/resume.d.ts +18 -0
  209. package/dist/replay/resume.js +34 -0
  210. package/dist/replay/start.d.ts +23 -0
  211. package/dist/replay/start.js +39 -0
  212. package/dist/replay/throttle.d.ts +35 -0
  213. package/dist/replay/throttle.js +41 -0
  214. package/dist/replay/transcript-turns.d.ts +35 -0
  215. package/dist/replay/transcript-turns.js +215 -0
  216. package/dist/replay/turn-event.d.ts +38 -0
  217. package/dist/replay/turn-event.js +116 -0
  218. package/dist/replay/types/replay.types.d.ts +162 -0
  219. package/dist/replay/types/replay.types.js +1 -0
  220. package/dist/replay/window.d.ts +28 -0
  221. package/dist/replay/window.js +41 -0
  222. package/dist/snapshot/branch-selection.d.ts +40 -23
  223. package/dist/snapshot/branch-selection.js +74 -42
  224. package/dist/snapshot/budget.js +2 -2
  225. package/dist/snapshot/constants/snapshot-stages.constants.d.ts +10 -0
  226. package/dist/snapshot/constants/snapshot-stages.constants.js +10 -0
  227. package/dist/snapshot/constants/snapshot.constants.d.ts +37 -4
  228. package/dist/snapshot/constants/snapshot.constants.js +37 -4
  229. package/dist/snapshot/index.d.ts +6 -1
  230. package/dist/snapshot/index.js +3 -1
  231. package/dist/snapshot/snapshot-event.d.ts +7 -0
  232. package/dist/snapshot/snapshot-event.js +8 -5
  233. package/dist/snapshot/snapshot-pipeline.js +89 -26
  234. package/dist/snapshot/snapshot-state.js +24 -2
  235. package/dist/snapshot/tree-event.d.ts +14 -0
  236. package/dist/snapshot/tree-event.js +91 -0
  237. package/dist/snapshot/tree-pipeline.d.ts +12 -0
  238. package/dist/snapshot/tree-pipeline.js +172 -0
  239. package/dist/snapshot/types/snapshot.types.d.ts +62 -2
  240. package/dist/storage/schemas/storage.schema.d.ts +2 -0
  241. package/dist/storage/schemas/storage.schema.js +6 -0
  242. package/dist/transport/constants/transport.constants.d.ts +5 -0
  243. package/dist/transport/constants/transport.constants.js +5 -0
  244. package/dist/transport/delivery-stats.d.ts +13 -1
  245. package/dist/transport/delivery-stats.js +18 -2
  246. package/dist/transport/delivery.d.ts +4 -3
  247. package/dist/transport/delivery.js +21 -7
  248. package/dist/transport/identity-delivery.d.ts +20 -0
  249. package/dist/transport/identity-delivery.js +41 -0
  250. package/dist/transport/index.d.ts +2 -0
  251. package/dist/transport/index.js +1 -0
  252. package/dist/transport/queue.js +40 -6
  253. package/dist/transport/types/delivery-flow.types.d.ts +7 -0
  254. package/dist/turns/claude-transcript.js +4 -12
  255. package/dist/turns/codex-transcript.d.ts +9 -0
  256. package/dist/turns/codex-transcript.js +27 -19
  257. package/dist/turns/constants/turns.constants.d.ts +13 -12
  258. package/dist/turns/constants/turns.constants.js +15 -19
  259. package/dist/turns/gemini-transcript.d.ts +19 -1
  260. package/dist/turns/gemini-transcript.js +29 -18
  261. package/dist/turns/index.d.ts +3 -4
  262. package/dist/turns/index.js +2 -3
  263. package/dist/turns/run-state.d.ts +71 -0
  264. package/dist/turns/run-state.js +114 -0
  265. package/dist/turns/tool-events.d.ts +11 -1
  266. package/dist/turns/tool-events.js +32 -9
  267. package/dist/turns/transcript.d.ts +15 -14
  268. package/dist/turns/transcript.js +23 -4
  269. package/dist/turns/turn-state.d.ts +49 -7
  270. package/dist/turns/turn-state.js +78 -10
  271. package/dist/turns/turn-summary.js +10 -6
  272. package/dist/turns/turn-tracker.d.ts +9 -16
  273. package/dist/turns/turn-tracker.js +162 -86
  274. package/dist/turns/types/transcript.types.d.ts +11 -1
  275. package/dist/turns/types/turn-provider.types.d.ts +33 -0
  276. package/dist/turns/types/turn-provider.types.js +1 -0
  277. package/dist/turns/types/turn-state.types.d.ts +9 -0
  278. package/dist/turns/types/turn-summary.types.d.ts +20 -6
  279. package/dist/turns/types/turn-tracker.types.d.ts +3 -1
  280. package/dist/turns/work-checkout.js +1 -2
  281. package/docs/AUDIT_PROMPT.md +10 -0
  282. package/docs/DATA_HANDLING.md +269 -32
  283. package/docs/ENTERPRISE_DEPLOYMENT.md +1 -1
  284. package/docs/PERFORMANCE.md +27 -0
  285. package/package.json +3 -2
  286. package/dist/events/canonical-event.d.ts +0 -10
  287. package/dist/events/canonical-event.js +0 -9
  288. package/dist/git/repository-root.d.ts +0 -39
  289. package/dist/git/repository-root.js +0 -131
  290. package/dist/git/types/repository-root.types.d.ts +0 -11
  291. package/dist/transport/transport.d.ts +0 -6
  292. package/dist/turns/codex-originator.d.ts +0 -11
  293. package/dist/turns/cursor-transcript.d.ts +0 -21
  294. package/dist/turns/cursor-transcript.js +0 -66
  295. package/dist/turns/native-session-titles.d.ts +0 -26
  296. package/dist/turns/native-session-titles.js +0 -102
  297. /package/dist/{git/types/repository-root.types.js → billing/types/billing.types.js} +0 -0
  298. /package/dist/{transport/transport.js → capture/types/capture.types.js} +0 -0
@@ -0,0 +1,529 @@
1
+ import crypto from 'node:crypto';
2
+ import os from 'node:os';
3
+ import path from 'node:path';
4
+ import { isDisabled } from '../storage/disabled.js';
5
+ import { debugLog } from '../core/logger.js';
6
+ import { repositoryIdentity, runGit } from '../git/git-context.js';
7
+ import { CANONICAL_PROVIDER_NAMES } from '../otlp/constants/otlp.constants.js';
8
+ import { runSnapshotPipeline } from '../snapshot/snapshot-pipeline.js';
9
+ import { SnapshotStateStore } from '../snapshot/snapshot-state.js';
10
+ import { runTreePipeline } from '../snapshot/tree-pipeline.js';
11
+ import { acquireLock } from '../storage/lock.js';
12
+ import { ANY_DESTINATION, AUTH_REJECTED_STATUSES } from '../transport/constants/transport.constants.js';
13
+ import { sendEvents } from '../transport/send.js';
14
+ import { PREPASS_PERSIST_EVERY, REPLAY_BATCH_INTERVAL_MS, REPLAY_BATCH_TURNS, REPLAY_FILE_PAUSE_MS, REPLAY_GIVE_UP_FAILURES, REPLAY_HEARTBEAT_MS, REPLAY_LIVE_TURN_GRACE_MS, REPLAY_NICE, REPLAY_PROGRESS_EVERY_BATCHES, REPLAY_RUN_BUDGET_MS, REPLAY_SNAPSHOT_BUDGET_MS } from './constants/replay.constants.js';
15
+ import { checkoutOfTurn } from './checkout-resolver.js';
16
+ import { buildReplayProgress } from './progress-event.js';
17
+ import { countsByAgent, listSources, sourceOf } from './replay-scan.js';
18
+ import { decideStart, isRunning, readReplayRecord, setPending, writeReplayRecord } from './replay-state.js';
19
+ import { isWorthWaiting, nextBackoff, throttleVerdict } from './throttle.js';
20
+ import { buildReplayTurnEvent } from './turn-event.js';
21
+ import { firstOpenInstant, replayWindow } from './window.js';
22
+ /** The scan writes its state this often, not per file: the state holds a map of every transcript. */
23
+ const SCAN_PERSIST_EVERY = 20;
24
+ /**
25
+ * Replay a developer's earlier Claude Code, Codex and Gemini CLI sessions to the backend, slowly.
26
+ *
27
+ * In order: decide whether to begin, continue or leave an earlier replay;
28
+ * count the window's transcripts; send their turns newest first in small
29
+ * batches, each preceded by a look at the machine; send one snapshot per
30
+ * repository; end with a final progress record. A crash or a `--stop` leaves a
31
+ * checkpoint per finished file, and every turn is idempotent downstream, so a
32
+ * rerun costs bandwidth, never correctness.
33
+ *
34
+ * @param ports - The machine, the identity and the transport, handed in.
35
+ * @param request - What was asked.
36
+ * @returns How it ended.
37
+ */
38
+ export async function runReplay(ports, request) {
39
+ const claimed = request.dryRun ? { record: freshRecord(ports, request.days), fresh: true } : await claim(ports, request);
40
+ if ('result' in claimed)
41
+ return claimed.result;
42
+ lowerPriority();
43
+ return execute(ports, request, claimed.record, claimed.fresh);
44
+ }
45
+ /**
46
+ * Take the replay, under a short lock, so two starters (setup and a hook) cannot both run it.
47
+ *
48
+ * @param ports - The machine.
49
+ * @param request - What was asked.
50
+ * @returns The record to run and whether it is a new replay, or why not.
51
+ */
52
+ async function claim(ports, request) {
53
+ // In the identity's own state directory, not `locks/`: the throttle reads that directory's mtime as "a hook is working".
54
+ const release = await acquireLock(path.dirname(ports.stateFile), 'replay-start', ports.env.now);
55
+ if (!release)
56
+ return { result: { outcome: 'skipped', message: 'another replay is starting' } };
57
+ try {
58
+ const existing = await readReplayRecord(ports.stateFile);
59
+ const decision = decideStart(existing, { identity: ports.identity, resume: request.resume, skipIfExisting: request.skipIfExisting, nowMs: ports.env.now().getTime() }, ports.alive);
60
+ if (decision.action === 'skip')
61
+ return { result: { outcome: 'skipped', message: decision.message } };
62
+ const fresh = decision.action === 'fresh';
63
+ const record = fresh ? freshRecord(ports, request.days) : continuedRecord(decision.record, ports);
64
+ await writeReplayRecord(ports.stateFile, record);
65
+ await setPending(ports.stateFile, true);
66
+ return { record, fresh };
67
+ }
68
+ finally {
69
+ await release();
70
+ }
71
+ }
72
+ function freshRecord(ports, days) {
73
+ const now = ports.env.now();
74
+ const window = replayWindow(now, days);
75
+ const at = now.toISOString();
76
+ return {
77
+ replayId: crypto.randomUUID(),
78
+ identity: ports.identity,
79
+ state: 'scanning',
80
+ windowStart: window.start.toISOString(),
81
+ windowEnd: window.end.toISOString(),
82
+ startedAt: at,
83
+ updatedAt: at,
84
+ pid: process.pid,
85
+ heartbeatAt: at,
86
+ turnsTotal: 0,
87
+ turnsSent: 0,
88
+ sessionsTotal: 0,
89
+ sessionsSent: 0,
90
+ batches: 0,
91
+ refused: 0,
92
+ snapshotted: [],
93
+ files: {},
94
+ agents: ports.sources.map((source) => source.agent)
95
+ };
96
+ }
97
+ /**
98
+ * An unfinished replay, taken over by this process.
99
+ *
100
+ * The window's start may only move later: the month can have turned since, and
101
+ * the open charge period with it.
102
+ *
103
+ * @param record - What the dead or stopped run left.
104
+ * @param ports - The machine.
105
+ * @returns The record, owned by this process.
106
+ */
107
+ function continuedRecord(record, ports) {
108
+ const now = ports.env.now();
109
+ const start = Math.max(Date.parse(record.windowStart), firstOpenInstant(now).getTime());
110
+ const at = now.toISOString();
111
+ return { ...record, state: 'running', pause: undefined, windowStart: new Date(start).toISOString(), windowEnd: at, updatedAt: at, pid: process.pid, heartbeatAt: at };
112
+ }
113
+ /** `nice` 19, so the operating system hands every other process the CPU first. Fails open: some systems refuse. */
114
+ function lowerPriority() {
115
+ try {
116
+ os.setPriority(0, REPLAY_NICE);
117
+ }
118
+ catch (error) {
119
+ debugLog('replay: could not lower its priority:', error);
120
+ }
121
+ }
122
+ /**
123
+ * Send a progress record for the replay's state.
124
+ *
125
+ * Never throws: a progress record is a courtesy. A terminal one that cannot be
126
+ * sent is kept in the offline queue, because the last word is the one the
127
+ * platform is waiting for.
128
+ *
129
+ * @param ports - The machine.
130
+ * @param record - The replay's state.
131
+ * @param terminal - Whether this is the last record of the run.
132
+ */
133
+ async function reportProgress(ports, record, terminal) {
134
+ try {
135
+ const event = buildReplayProgress(record, { developerId: ports.developerId, installationId: ports.installationId });
136
+ const transport = await ports.transport();
137
+ const result = transport ? await sendEvents(transport, [event]) : undefined;
138
+ if (!result?.ok && terminal)
139
+ await ports.queue.enqueue([event], transport?.destination ?? ANY_DESTINATION);
140
+ }
141
+ catch (error) {
142
+ debugLog('replay: progress record not sent:', error);
143
+ }
144
+ }
145
+ /**
146
+ * `agentwatch replay --stop`: end the replay, wherever it is running.
147
+ *
148
+ * A running replay notices the written state at its next write and reports
149
+ * its own end; one that is not running is ended and reported here.
150
+ *
151
+ * @param ports - The machine.
152
+ * @returns What happened.
153
+ */
154
+ export async function stopReplay(ports) {
155
+ const record = await readReplayRecord(ports.stateFile);
156
+ if (!record || record.identity !== ports.identity || record.state === 'done' || record.state === 'stopped')
157
+ return { outcome: 'skipped', message: 'no replay to stop' };
158
+ const running = isRunning(record, ports.env.now().getTime(), ports.alive);
159
+ const stopped = { ...record, state: 'stopped', pause: undefined, pid: running ? record.pid : undefined, updatedAt: ports.env.now().toISOString() };
160
+ await writeReplayRecord(ports.stateFile, stopped);
161
+ if (!running) {
162
+ await setPending(ports.stateFile, false);
163
+ await reportProgress(ports, stopped, true);
164
+ }
165
+ return { outcome: 'stopped', message: running ? 'replay stopping' : 'replay stopped', record: stopped };
166
+ }
167
+ /**
168
+ * Why a gate that did not say go ends the run.
169
+ *
170
+ * @param gated - What the gate said.
171
+ * @returns The exit.
172
+ */
173
+ function exitOfGate(gated) {
174
+ if (gated === 'stop')
175
+ return 'stop';
176
+ return gated === 'budget' ? 'budget' : 'give-up';
177
+ }
178
+ /**
179
+ * The run itself. One closure holds the record, because everything it does is
180
+ * "change the record, write it, say so": `persist`, `report`, `nap`, `gate`.
181
+ */
182
+ async function execute(ports, request, start, fresh) {
183
+ const dry = request.dryRun;
184
+ const clock = () => ports.env.now().getTime();
185
+ const startedMs = clock();
186
+ const sinceMs = Date.parse(start.windowStart);
187
+ const capture = ports.config.capture;
188
+ let record = start;
189
+ let waitedMs = 0;
190
+ let stopRequested = false;
191
+ const persist = async (patch) => {
192
+ const at = ports.env.now().toISOString();
193
+ record = { ...record, ...patch, updatedAt: at, heartbeatAt: at };
194
+ if (dry)
195
+ return;
196
+ // A `--stop` written while this runs must not be written over.
197
+ const disk = await readReplayRecord(ports.stateFile);
198
+ if (disk?.replayId === record.replayId && disk.state === 'stopped' && record.state !== 'stopped') {
199
+ stopRequested = true;
200
+ return;
201
+ }
202
+ await writeReplayRecord(ports.stateFile, record);
203
+ };
204
+ const report = async (terminal) => {
205
+ if (!dry)
206
+ await reportProgress(ports, record, terminal);
207
+ };
208
+ /** Wait, in pieces short enough that the heartbeat never goes stale. `idle` waits do not count against the run's budget. */
209
+ const nap = async (ms, idle) => {
210
+ if (dry)
211
+ return;
212
+ for (let left = ms; left > 0; left -= REPLAY_HEARTBEAT_MS) {
213
+ const piece = Math.min(left, REPLAY_HEARTBEAT_MS);
214
+ await ports.sleep(piece);
215
+ waitedMs += idle ? piece : 0;
216
+ await persist({});
217
+ if (stopRequested)
218
+ return;
219
+ }
220
+ };
221
+ /** The machine's say-so before a batch: go, or wait it out and ask again. */
222
+ const gate = async () => {
223
+ if (dry)
224
+ return 'go';
225
+ for (;;) {
226
+ // `agentwatch off` ends it too: a disabled install sends nothing, replay included.
227
+ if (stopRequested || (await isDisabled(ports.paths)))
228
+ return 'stop';
229
+ if (clock() - startedMs - waitedMs >= REPLAY_RUN_BUDGET_MS)
230
+ return 'budget';
231
+ const transport = await ports.transport();
232
+ if (!transport)
233
+ return 'backend';
234
+ if (transport.destination && (await ports.authBlock.holds(transport.destination)))
235
+ return 'backend';
236
+ const verdict = (await ports.cooldown.active()) ? { go: false, pause: 'backend', waitMs: REPLAY_HEARTBEAT_MS } : throttleVerdict(await ports.machine());
237
+ if (verdict.go) {
238
+ if (record.state === 'paused') {
239
+ await persist({ state: 'running', pause: undefined });
240
+ await report(false);
241
+ }
242
+ return 'go';
243
+ }
244
+ if (record.state !== 'paused' || record.pause !== verdict.pause) {
245
+ await persist({ state: 'paused', pause: verdict.pause });
246
+ await report(false);
247
+ }
248
+ await nap(verdict.waitMs, true);
249
+ }
250
+ };
251
+ const giveUp = async (pause) => {
252
+ await persist({ state: 'paused', pause });
253
+ return 'give-up';
254
+ };
255
+ /** What a failed send means: an auth refusal ends the run, a hard refusal drops the batch, anything else is waited out. */
256
+ const refused = async (result, transport, count) => {
257
+ if (result.status !== undefined && AUTH_REJECTED_STATUSES.has(result.status)) {
258
+ if (transport.destination)
259
+ await ports.authBlock.raise(transport.destination, result.status, result.refusal);
260
+ return giveUp('backend');
261
+ }
262
+ if (isWorthWaiting(result.status, result.retryable))
263
+ return undefined;
264
+ // The backend said no and will say it again: do not wedge the whole replay on twenty turns.
265
+ await persist({ refused: record.refused + count });
266
+ ports.log(`the backend refused ${count} replayed turn(s) with HTTP ${result.status ?? '?'}; skipping them`);
267
+ return 'ok';
268
+ };
269
+ /** One batch to the backend, retried with backoff while the backend or the network is down. */
270
+ const send = async (events) => {
271
+ if (dry || events.length === 0)
272
+ return 'ok';
273
+ let backoff;
274
+ for (let failures = 1;; failures += 1) {
275
+ const gated = await gate();
276
+ if (gated === 'stop' || gated === 'budget')
277
+ return gated;
278
+ if (gated === 'backend')
279
+ return giveUp('backend');
280
+ const transport = (await ports.transport());
281
+ const result = await sendEvents(transport, events);
282
+ if (result.ok)
283
+ return 'ok';
284
+ const exit = await refused(result, transport, events.length);
285
+ if (exit)
286
+ return exit;
287
+ if (failures >= REPLAY_GIVE_UP_FAILURES)
288
+ return giveUp('backend');
289
+ backoff = nextBackoff(backoff);
290
+ debugLog(`replay: send failed (${result.status ?? 'network'}); waiting ${backoff} ms`);
291
+ if (record.pause !== 'backend') {
292
+ await persist({ state: 'paused', pause: 'backend' });
293
+ await report(false);
294
+ }
295
+ await nap(backoff, true);
296
+ }
297
+ };
298
+ const finish = async (exit, counts) => {
299
+ if (exit === 'stop') {
300
+ await persist({ state: 'stopped', pause: undefined, pid: undefined });
301
+ await setPending(ports.stateFile, false);
302
+ await report(true);
303
+ return { outcome: 'stopped', message: 'replay stopped', record };
304
+ }
305
+ if (exit === 'ok') {
306
+ await persist({ state: 'done', pause: undefined, pid: undefined, turnsTotal: record.turnsSent, sessionsTotal: record.sessionsSent });
307
+ if (!dry)
308
+ await setPending(ports.stateFile, false);
309
+ await report(true);
310
+ return { outcome: 'done', message: `replayed ${record.turnsSent} turns from ${record.sessionsSent} sessions`, record, dry: counts };
311
+ }
312
+ await persist({ state: 'paused', pause: exit === 'budget' ? 'budget' : (record.pause ?? 'backend'), pid: undefined });
313
+ await report(false);
314
+ return { outcome: 'paused', message: 'replay paused; it continues on the next agent session or with `agentwatch replay --resume`', record };
315
+ };
316
+ // 1. Count what there is.
317
+ if (fresh || Object.keys(record.files).length === 0) {
318
+ await report(false);
319
+ const found = await listSources(ports.sources, sinceMs);
320
+ const files = {};
321
+ let total = 0;
322
+ for (const [index, transcript] of found.entries()) {
323
+ const entry = { mtimeMs: transcript.mtimeMs, size: transcript.size, turns: 0, done: false, agent: transcript.agent };
324
+ const turns = (await sourceOf(ports.sources, entry)?.estimate(transcript.file, sinceMs)) ?? 0;
325
+ files[transcript.file] = { ...entry, turns };
326
+ total += turns;
327
+ if (index % SCAN_PERSIST_EVERY === 0)
328
+ await persist({ files: { ...files }, turnsTotal: total, sessionsTotal: found.length });
329
+ await nap(REPLAY_FILE_PAUSE_MS, false);
330
+ }
331
+ await persist({ files, turnsTotal: total, sessionsTotal: found.length });
332
+ }
333
+ await persist({ state: 'running', pause: undefined });
334
+ await report(false);
335
+ // 2. Send the turns, newest transcript first.
336
+ const pending = Object.entries(record.files)
337
+ .filter(([, entry]) => !entry.done)
338
+ .sort(([, a], [, b]) => b.mtimeMs - a.mtimeMs)
339
+ .map(([file]) => file);
340
+ const cutoffMs = clock() - REPLAY_LIVE_TURN_GRACE_MS;
341
+ const seen = new Set();
342
+ const repositories = new Map();
343
+ const context = { developerId: ports.developerId, developerName: ports.developerName, installationId: ports.installationId, capture, billingMode: ports.billingMode };
344
+ let buffer = [];
345
+ let finished = [];
346
+ const sessionOfFile = new Map();
347
+ let built = 0;
348
+ const builtBy = new Map();
349
+ /** Send the buffer; every file read through to its end by now has all its turns in what has been sent. */
350
+ const flush = async () => {
351
+ const exit = await send(buffer);
352
+ if (exit !== 'ok')
353
+ return exit;
354
+ const files = { ...record.files };
355
+ for (const file of finished)
356
+ files[file] = { ...files[file], done: true, session: sessionOfFile.get(file) ?? files[file]?.session };
357
+ built += buffer.length;
358
+ await persist({
359
+ files,
360
+ turnsSent: record.turnsSent + (dry ? 0 : buffer.length),
361
+ // Distinct sessions: a resumed Codex session leaves one rollout file per resume. A file with no turn counts as its own.
362
+ sessionsSent: new Set(Object.entries(files).filter(([, entry]) => entry.done).map(([name, entry]) => entry.session ?? name)).size,
363
+ batches: record.batches + 1
364
+ });
365
+ if (!dry && buffer.length > 0) {
366
+ if (record.batches % REPLAY_PROGRESS_EVERY_BATCHES === 0)
367
+ await report(false);
368
+ await nap(REPLAY_BATCH_INTERVAL_MS, false);
369
+ }
370
+ buffer = [];
371
+ finished = [];
372
+ return stopRequested ? 'stop' : 'ok';
373
+ };
374
+ // What each folder's transcripts say about it, read before sending: a folder that is gone (a
375
+ // review's temp tree) is known only by the Codex rollout that ran in it, and a Claude session
376
+ // in the same folder borrows that. Every file of the run is read, finished ones too, so the answer
377
+ // does not depend on where a resumed run paused. `knownRepos` maps a repository to a checkout of it
378
+ // the run has seen, only to find the capture file that narrows it. Scope is never decided from it:
379
+ // a folder that is gone belongs to a root by its path alone, as for the live hook.
380
+ const folders = new Map();
381
+ const knownRepos = new Map();
382
+ const newestFirst = Object.entries(record.files).sort(([, x], [, y]) => y.mtimeMs - x.mtimeMs);
383
+ for (const [index, [file, entry]] of newestFirst.entries()) {
384
+ // A long pass must still look alive, and still hear a `--stop`.
385
+ if (index % PREPASS_PERSIST_EVERY === 0)
386
+ await persist({});
387
+ if (stopRequested)
388
+ return finish('stop');
389
+ const id = await sourceOf(ports.sources, entry)
390
+ ?.identity?.(file)
391
+ .catch(() => undefined);
392
+ if (!id)
393
+ continue;
394
+ // The newest rollout that names a remote, whole: fields of two sessions are never mixed.
395
+ if (!folders.get(id.cwd)?.remote)
396
+ folders.set(id.cwd, id);
397
+ // Only this identity's folders: another root's are neither read nor told about.
398
+ const known = ports.inScope(id.cwd) ? await ports.checkout(id.cwd) : undefined;
399
+ if (known && !knownRepos.has(known.repository))
400
+ knownRepos.set(known.repository, known.root);
401
+ }
402
+ const withFolderIdentity = (turn) => {
403
+ const own = turn.cwd && !turn.remote ? folders.get(turn.cwd) : undefined;
404
+ return own ? { ...turn, remote: own.remote, commit: turn.branch ? undefined : own.commit, branch: turn.branch ?? own.branch } : turn;
405
+ };
406
+ /** The checkout of the repository a gone folder's transcript names, whose capture file narrows it. */
407
+ const rootOfRemote = (turn) => {
408
+ const remote = withFolderIdentity(turn).remote;
409
+ const repository = remote ? repositoryIdentity('', remote).remote : undefined;
410
+ return repository ? knownRepos.get(repository) : undefined;
411
+ };
412
+ for (const file of pending) {
413
+ const source = sourceOf(ports.sources, record.files[file]);
414
+ const billingMode = source?.billingMode ?? ports.billingMode;
415
+ try {
416
+ // A file of an agent this run has no source for (none should be) is read as empty and marked done.
417
+ for await (const turn of source?.read(file, capture.promptText === true) ?? []) {
418
+ const startedAt = Date.parse(turn.startedAt);
419
+ const endedAt = Date.parse(turn.endedAt);
420
+ if (startedAt < sinceMs || endedAt > cutoffMs || seen.has(turn.turnId))
421
+ continue;
422
+ if (turn.cwd && !ports.inScope(turn.cwd))
423
+ continue;
424
+ seen.add(turn.turnId);
425
+ // What a live hook in that folder would capture: a repository's own file may narrow it.
426
+ // A repository found some other way (a tool's file, a gone folder's remote) answers to its own checkout's file.
427
+ let turnCapture = turn.cwd ? await ports.captureFor(turn.cwd) : capture;
428
+ let checkout = turnCapture.git !== false ? await checkoutOfTurn(turn, ports.checkout, (found) => ports.inScope(found.root)) : undefined;
429
+ // ponytail: also learned as turns resolve, for Claude- and Gemini-only checkouts the pre-pass cannot see;
430
+ // a gone folder read before its repository is first seen answers to the global capture. Ceiling: a
431
+ // pre-pass over every transcript's folder; upgrade path: an `identity` on those sources.
432
+ if (checkout && !knownRepos.has(checkout.repository))
433
+ knownRepos.set(checkout.repository, checkout.root);
434
+ // Another folder's own capture file narrows the turn's, never widens it.
435
+ const otherRoot = checkout ? checkout.evidence && checkout.root : rootOfRemote(turn);
436
+ if (otherRoot)
437
+ turnCapture = narrower(turnCapture, await ports.captureFor(otherRoot));
438
+ if (checkout?.evidence && turnCapture.git === false)
439
+ checkout = undefined;
440
+ sessionOfFile.set(file, turn.sessionId);
441
+ if (checkout && !repositories.has(checkout.root))
442
+ repositories.set(checkout.root, { repository: checkout.repository, sessionId: turn.sessionId, surface: turn.surface ?? 'cli', provider: turn.provider });
443
+ buffer.push(buildReplayTurnEvent(checkout ? turn : withFolderIdentity(turn), checkout, { ...context, capture: turnCapture, billingMode }));
444
+ builtBy.set(turn.provider, (builtBy.get(turn.provider) ?? 0) + 1);
445
+ if (buffer.length < REPLAY_BATCH_TURNS)
446
+ continue;
447
+ const exit = await flush();
448
+ if (exit !== 'ok')
449
+ return finish(exit);
450
+ }
451
+ }
452
+ catch (error) {
453
+ // A transcript that cannot be read is skipped, not the replay.
454
+ debugLog('replay: skipped an unreadable transcript:', error);
455
+ }
456
+ finished.push(file);
457
+ if (!dry)
458
+ await ports.sleep(REPLAY_FILE_PAUSE_MS);
459
+ }
460
+ const last = await flush();
461
+ if (last !== 'ok')
462
+ return finish(last);
463
+ // 3. One snapshot per repository the turns ran in, at the end: they are what makes the branches known.
464
+ if (!dry && capture.git !== false) {
465
+ for (const [root, seenRepository] of repositories) {
466
+ if (record.snapshotted.includes(root))
467
+ continue;
468
+ const gated = await gate();
469
+ if (gated !== 'go')
470
+ return finish(exitOfGate(gated));
471
+ await snapshotOf(ports, root, seenRepository);
472
+ await persist({ snapshotted: [...record.snapshotted, root] });
473
+ await nap(REPLAY_FILE_PAUSE_MS, false);
474
+ }
475
+ }
476
+ return finish('ok', { files: pending.length, estimate: record.turnsTotal, turns: built, repositories: repositories.size, agents: countsByAgent(pending, record.files, builtBy) });
477
+ }
478
+ /**
479
+ * The snapshot, and tree, of one repository, then a drain so they leave now.
480
+ *
481
+ * Both are the hook path's own flows, with a longer deadline: no hook is
482
+ * waiting on this one. The queue holds them if the send fails.
483
+ *
484
+ * @param ports - The machine.
485
+ * @param root - The checkout root.
486
+ * @param seen - Which repository and session it was.
487
+ */
488
+ async function snapshotOf(ports, root, seen) {
489
+ try {
490
+ // The label a live turn of that agent carries: `claude-code` for Claude Code, as before.
491
+ const label = CANONICAL_PROVIDER_NAMES[seen.provider] ?? seen.provider;
492
+ const input = {
493
+ cwd: root,
494
+ files: (await ports.captureFor(root)).files !== false,
495
+ identity: ports.identity,
496
+ repository: seen.repository,
497
+ provider: label,
498
+ surface: seen.surface,
499
+ agentName: label,
500
+ developerId: ports.developerId,
501
+ installationId: ports.installationId,
502
+ sessionId: seen.sessionId,
503
+ capturedAt: ports.env.now().toISOString(),
504
+ // Real time, as the hook's: this bounds git subprocesses, which the wall clock times.
505
+ deadline: Date.now() + REPLAY_SNAPSHOT_BUDGET_MS,
506
+ run: runGit
507
+ };
508
+ const store = new SnapshotStateStore(ports.paths.snapshotsDir);
509
+ await runSnapshotPipeline({ input, store, queue: ports.queue });
510
+ if (input.files)
511
+ await runTreePipeline({ input, store, queue: ports.queue });
512
+ const transport = await ports.transport();
513
+ if (transport)
514
+ await ports.queue.drain(transport, ports.config.delivery.drainBatchSize, undefined, ports.authBlock);
515
+ }
516
+ catch (error) {
517
+ debugLog('replay: snapshot failed:', error);
518
+ }
519
+ }
520
+ /**
521
+ * Two capture configs at once: a flag is on only where both have it on.
522
+ *
523
+ * @param own - The turn's own folder's.
524
+ * @param other - The repository's whose checkout the turn's work was found in.
525
+ * @returns The narrower of the two.
526
+ */
527
+ function narrower(own, other) {
528
+ return Object.fromEntries(Object.entries(own).map(([key, on]) => [key, on && other[key]]));
529
+ }
@@ -0,0 +1,83 @@
1
+ import type { ReplayFileEntry, ReplaySource, TranscriptFile } from './types/replay.types.js';
2
+ /** A listed file and the source it belongs to. */
3
+ export interface SourcedFile extends TranscriptFile {
4
+ readonly agent: string;
5
+ }
6
+ /**
7
+ * Files and built turns per agent, for the dry run.
8
+ *
9
+ * @param files - The files the run read.
10
+ * @param entries - The record's file map that lists them.
11
+ * @param builtBy - Turns built per agent.
12
+ * @returns The counts.
13
+ */
14
+ export declare function countsByAgent(files: readonly string[], entries: Readonly<Record<string, ReplayFileEntry>>, builtBy: ReadonlyMap<string, number>): Record<string, {
15
+ readonly files: number;
16
+ readonly turns: number;
17
+ }>;
18
+ /**
19
+ * Where Claude Code keeps its transcripts.
20
+ *
21
+ * @param home - The developer's home.
22
+ * @returns `~/.claude/projects`.
23
+ */
24
+ export declare function claudeProjectsDir(home: string): string;
25
+ /**
26
+ * Claude Code's transcripts as a replay source.
27
+ *
28
+ * @param projectsDir - `~/.claude/projects`.
29
+ * @returns The source.
30
+ */
31
+ export declare function claudeSource(projectsDir: string): ReplaySource;
32
+ /**
33
+ * Every source's files, newest first, each tagged with its source.
34
+ *
35
+ * @param sources - The agents to list.
36
+ * @param sinceMs - The window's start.
37
+ * @returns The files.
38
+ */
39
+ export declare function listSources(sources: readonly ReplaySource[], sinceMs: number): Promise<SourcedFile[]>;
40
+ /**
41
+ * The source a counted file belongs to. A file counted before Codex and Gemini replay carries no agent: it is Claude Code's.
42
+ *
43
+ * @param sources - The run's sources.
44
+ * @param entry - The file's entry in the record.
45
+ * @returns The source, or undefined when this run has no source for it.
46
+ */
47
+ export declare function sourceOf(sources: readonly ReplaySource[], entry: ReplayFileEntry | undefined): ReplaySource | undefined;
48
+ /**
49
+ * The transcripts that may hold a turn from the window, newest first.
50
+ *
51
+ * A file last written before the window began cannot hold a turn inside it, so
52
+ * one `stat` rules it out without opening it. Only `<project>/<session>.jsonl`
53
+ * is listed: the sub-agent files below a session are counted in their parent's
54
+ * turn, as live hooks count them.
55
+ *
56
+ * @param projectsDir - `~/.claude/projects`.
57
+ * @param sinceMs - The window's start.
58
+ * @returns The files, most recently written first; empty when Claude Code has left none.
59
+ */
60
+ export declare function listTranscripts(projectsDir: string, sinceMs: number): Promise<TranscriptFile[]>;
61
+ /**
62
+ * The `.jsonl` files directly in a folder last written inside the window: one `stat` each, no open.
63
+ *
64
+ * @param dir - The folder.
65
+ * @param sinceMs - The window's start.
66
+ * @param prefix - A name prefix the files must carry, if any.
67
+ * @returns The files; empty when the folder is missing.
68
+ */
69
+ export declare function recentFiles(dir: string, sinceMs: number, prefix?: string): Promise<TranscriptFile[]>;
70
+ /**
71
+ * About how many prompts a file holds from the window, without parsing it.
72
+ *
73
+ * Reads the file as text and tests each line against a pattern: no JSON is
74
+ * decoded, which is what makes the count pass cheap. It is an estimate for the
75
+ * progress bar (it counts a prompt that got no answer, which is never sent);
76
+ * the sent total replaces it when the replay ends.
77
+ *
78
+ * @param file - A transcript file.
79
+ * @param sinceMs - The window's start.
80
+ * @param opensTurn - What a line that opens a turn looks like; Claude Code's typed prompt by default.
81
+ * @returns The estimate.
82
+ */
83
+ export declare function estimateTurns(file: string, sinceMs: number, opensTurn?: RegExp): Promise<number>;