@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
@@ -124,3 +124,8 @@ export declare const DELIVERY_STAGE_NAMES: {
124
124
  /** In an identity's state directory: its cached access token and its device credential state (AWT-356). */
125
125
  export declare const ACCESS_TOKEN_FILE_NAME = "access-token.json";
126
126
  export declare const DEVICE_STATE_FILE_NAME = "device.json";
127
+ /**
128
+ * How long a `repo.tree` whose upload the delivery budget cut short waits before the next try.
129
+ * Not an attempt: the tree was fine, the link was slow. A catalog tree is never urgent.
130
+ */
131
+ export declare const TREE_DEFERRED_RETRY_MS: number;
@@ -124,3 +124,8 @@ export const DELIVERY_STAGE_NAMES = {
124
124
  /** In an identity's state directory: its cached access token and its device credential state (AWT-356). */
125
125
  export const ACCESS_TOKEN_FILE_NAME = 'access-token.json';
126
126
  export const DEVICE_STATE_FILE_NAME = 'device.json';
127
+ /**
128
+ * How long a `repo.tree` whose upload the delivery budget cut short waits before the next try.
129
+ * Not an attempt: the tree was fine, the link was slow. A catalog tree is never urgent.
130
+ */
131
+ export const TREE_DEFERRED_RETRY_MS = 15 * 60_000;
@@ -1,5 +1,10 @@
1
1
  import type { DeliveryStatsSnapshot } from './types/transport.types.js';
2
2
  export type { DeliveryStatsSnapshot } from './types/transport.types.js';
3
+ /** The tally, with the captured calls that never became queue entries. */
4
+ export interface CapturedDeliveryStats extends DeliveryStatsSnapshot {
5
+ readonly totalCapturedDropped: number;
6
+ readonly lastCapturedDroppedAt: string;
7
+ }
3
8
  /**
4
9
  * Persisted tally of everything the local queue lost, and why.
5
10
  *
@@ -23,7 +28,7 @@ export declare class DeliveryStats {
23
28
  *
24
29
  * @returns The snapshot, or undefined when there is nothing recorded.
25
30
  */
26
- read(): Promise<DeliveryStatsSnapshot | undefined>;
31
+ read(): Promise<CapturedDeliveryStats | undefined>;
27
32
  /**
28
33
  * Note events the backend refused inside an accepted batch. Never throws.
29
34
  *
@@ -36,6 +41,13 @@ export declare class DeliveryStats {
36
41
  * @param count - How many were lost.
37
42
  */
38
43
  recordDropped(count: number): Promise<void>;
44
+ /**
45
+ * Note captured calls that could not be attached to a session's run, or that
46
+ * went past a process's spool limit. Never throws.
47
+ *
48
+ * @param count - How many were dropped.
49
+ */
50
+ recordCapturedDropped(count: number): Promise<void>;
39
51
  /**
40
52
  * Note a status the transport will not retry. Never throws.
41
53
  *
@@ -35,7 +35,7 @@ export class DeliveryStats {
35
35
  try {
36
36
  const raw = JSON.parse(await fs.readFile(this.file, 'utf8'));
37
37
  // A file written before the dropped/refusal counters existed still reads.
38
- if (typeof raw.totalRejected !== 'number' && typeof raw.totalDropped !== 'number' && typeof raw.lastRefusalStatus !== 'number') {
38
+ if (typeof raw.totalRejected !== 'number' && typeof raw.totalDropped !== 'number' && typeof raw.lastRefusalStatus !== 'number' && typeof raw.totalCapturedDropped !== 'number') {
39
39
  return undefined;
40
40
  }
41
41
  return {
@@ -46,7 +46,9 @@ export class DeliveryStats {
46
46
  lastDroppedCount: numberOr(raw.lastDroppedCount, 0),
47
47
  lastDroppedAt: stringOr(raw.lastDroppedAt),
48
48
  lastRefusalStatus: numberOr(raw.lastRefusalStatus, 0),
49
- lastRefusalAt: stringOr(raw.lastRefusalAt)
49
+ lastRefusalAt: stringOr(raw.lastRefusalAt),
50
+ totalCapturedDropped: numberOr(raw.totalCapturedDropped, 0),
51
+ lastCapturedDroppedAt: stringOr(raw.lastCapturedDroppedAt)
50
52
  };
51
53
  }
52
54
  catch {
@@ -77,6 +79,18 @@ export class DeliveryStats {
77
79
  lastDroppedAt: at
78
80
  }));
79
81
  }
82
+ /**
83
+ * Note captured calls that could not be attached to a session's run, or that
84
+ * went past a process's spool limit. Never throws.
85
+ *
86
+ * @param count - How many were dropped.
87
+ */
88
+ async recordCapturedDropped(count) {
89
+ await this.record(count, (current, at) => ({
90
+ totalCapturedDropped: (current?.totalCapturedDropped ?? 0) + count,
91
+ lastCapturedDroppedAt: at
92
+ }));
93
+ }
80
94
  /**
81
95
  * Note a status the transport will not retry. Never throws.
82
96
  *
@@ -155,6 +169,8 @@ async function releaseQuietly(release) {
155
169
  */
156
170
  function emptySnapshot() {
157
171
  return {
172
+ totalCapturedDropped: 0,
173
+ lastCapturedDroppedAt: '',
158
174
  totalRejected: 0,
159
175
  lastRejectedCount: 0,
160
176
  lastRejectedAt: '',
@@ -4,8 +4,8 @@ import type { BackendCooldown } from './cooldown.js';
4
4
  import type { DeliveryStats } from './delivery-stats.js';
5
5
  import type { EventQueue } from './queue.js';
6
6
  import type { DeliveryOutcome, EventTransport } from './types/transport.types.js';
7
- export { BACKEND_COOLDOWN_MS } from './constants/transport.constants.js';
8
- export type { DeliveryOutcome } from './types/transport.types.js';
7
+ /** Refreshes the device token and returns a transport carrying it, or undefined when there is nothing to retry with. */
8
+ export type RefreshTransport = () => Promise<EventTransport | undefined>;
9
9
  /**
10
10
  * Hook-path delivery: one quick direct send, then whatever the backlog allows.
11
11
  *
@@ -29,6 +29,7 @@ export type { DeliveryOutcome } from './types/transport.types.js';
29
29
  * @param cooldown - Persisted circuit breaker, when enabled.
30
30
  * @param stats - Sink for permanent losses, when enabled.
31
31
  * @param authBlock - Persisted credential refusal, when enabled.
32
+ * @param refresh - After a plain 401, asks for a transport on a new device token; used at most once per pass.
32
33
  * @returns What was delivered, queued, drained and rejected.
33
34
  */
34
- export declare function deliverEvents(events: readonly ProductEvent[], transport: EventTransport | undefined, queue: EventQueue, drainBatchSize: number, cooldown?: BackendCooldown, stats?: DeliveryStats, authBlock?: BackendAuthBlock): Promise<DeliveryOutcome>;
35
+ export declare function deliverEvents(events: readonly ProductEvent[], transport: EventTransport | undefined, queue: EventQueue, drainBatchSize: number, cooldown?: BackendCooldown, stats?: DeliveryStats, authBlock?: BackendAuthBlock, refresh?: RefreshTransport): Promise<DeliveryOutcome>;
@@ -2,7 +2,6 @@ import { debugLog } from '../core/logger.js';
2
2
  import { next, runFlow, step } from '../core/pipe.js';
3
3
  import { ANY_DESTINATION, AUTH_REJECTED_STATUSES, BACKEND_COOLDOWN_MS, DELIVERY_STAGE_NAMES } from './constants/transport.constants.js';
4
4
  import { sendEvents } from './send.js';
5
- export { BACKEND_COOLDOWN_MS } from './constants/transport.constants.js';
6
5
  const DELIVERY_STAGES = [
7
6
  step(DELIVERY_STAGE_NAMES.select, selectDelivery),
8
7
  step(DELIVERY_STAGE_NAMES.send, sendCurrent),
@@ -33,14 +32,18 @@ const DELIVERY_STAGES = [
33
32
  * @param cooldown - Persisted circuit breaker, when enabled.
34
33
  * @param stats - Sink for permanent losses, when enabled.
35
34
  * @param authBlock - Persisted credential refusal, when enabled.
35
+ * @param refresh - After a plain 401, asks for a transport on a new device token; used at most once per pass.
36
36
  * @returns What was delivered, queued, drained and rejected.
37
37
  */
38
- export async function deliverEvents(events, transport, queue, drainBatchSize, cooldown, stats, authBlock) {
39
- const flow = await runFlow(DELIVERY_STAGES, {
40
- events, transport, queue, drainBatchSize, cooldown, stats, authBlock,
38
+ export async function deliverEvents(events, transport, queue, drainBatchSize, cooldown, stats, authBlock, refresh) {
39
+ // ponytail: `refresh` rides on the flow state beside the typed fields (the
40
+ // state type file is not part of this change); a variable avoids the excess-property check.
41
+ const initial = {
42
+ events, transport, queue, drainBatchSize, cooldown, stats, authBlock, refresh,
41
43
  canSend: false,
42
44
  outcome: { delivered: 0, queued: 0, drained: 0, rejected: 0 }
43
- });
45
+ };
46
+ const flow = await runFlow(DELIVERY_STAGES, initial);
44
47
  // A failed filesystem write must remain observable to the enclosing hook
45
48
  // flow; returning a successful outcome would falsely claim durable delivery.
46
49
  if (!flow.completed)
@@ -60,9 +63,20 @@ async function selectDelivery(state) {
60
63
  async function sendCurrent(state) {
61
64
  if (!state.canSend || !state.transport || state.events.length === 0)
62
65
  return next(state);
63
- const result = await sendEvents(state.transport, state.events);
66
+ let transport = state.transport;
67
+ let result = await sendEvents(transport, state.events);
68
+ const { refresh } = state;
69
+ // A plain 401 (no reason) for a device token means the gateway dropped it,
70
+ // e.g. a second holder refreshed: refresh once and resend once.
71
+ if (!result.ok && result.status === 401 && !result.refusal && refresh) {
72
+ const renewed = await refresh().catch(() => undefined);
73
+ if (renewed) {
74
+ transport = renewed;
75
+ result = await sendEvents(transport, state.events);
76
+ }
77
+ }
64
78
  const outcome = result.ok ? { ...state.outcome, delivered: state.events.length, rejected: result.counters?.rejected ?? 0 } : state.outcome;
65
- return next({ ...state, result, outcome });
79
+ return next({ ...state, transport, result, outcome });
66
80
  }
67
81
  async function preserveUnsent(state) {
68
82
  if (state.events.length === 0 || state.result?.ok)
@@ -0,0 +1,20 @@
1
+ import type { AgentWatchConfig } from '../config/types/config.types.js';
2
+ import type { AgentWatchPaths } from '../storage/types/storage.types.js';
3
+ import type { IdentityDelivery } from './types/delivery-flow.types.js';
4
+ /**
5
+ * The per-identity state one delivery reads and writes: the offline queue
6
+ * partition, the loss tally, the backend cooldown and the auth block.
7
+ *
8
+ * Shared by the hook and the CLI so both open the same identity's files with
9
+ * the same limits, and so they cannot drift apart on whose block or cooldown
10
+ * they read. Construction writes nothing; settling a legacy backlog is the
11
+ * caller's call.
12
+ *
13
+ * @param paths - Resolved storage paths.
14
+ * @param token - The credential naming the identity (`identityOf`); its
15
+ * files are derived once here, from these same paths.
16
+ * @param delivery - The delivery limits that apply.
17
+ * @param now - Clock for record ages, the tally, the cooldown and the block.
18
+ * @returns The four handles.
19
+ */
20
+ export declare function identityDelivery(paths: AgentWatchPaths, token: string | undefined, delivery: AgentWatchConfig['delivery'], now: () => Date): IdentityDelivery;
@@ -0,0 +1,41 @@
1
+ import { BackendAuthBlock } from './auth-block.js';
2
+ import { BackendCooldown } from './cooldown.js';
3
+ import { DeliveryStats } from './delivery-stats.js';
4
+ import { EventQueue } from './queue.js';
5
+ import { identityPaths } from './queue-partition.js';
6
+ /**
7
+ * The per-identity state one delivery reads and writes: the offline queue
8
+ * partition, the loss tally, the backend cooldown and the auth block.
9
+ *
10
+ * Shared by the hook and the CLI so both open the same identity's files with
11
+ * the same limits, and so they cannot drift apart on whose block or cooldown
12
+ * they read. Construction writes nothing; settling a legacy backlog is the
13
+ * caller's call.
14
+ *
15
+ * @param paths - Resolved storage paths.
16
+ * @param token - The credential naming the identity (`identityOf`); its
17
+ * files are derived once here, from these same paths.
18
+ * @param delivery - The delivery limits that apply.
19
+ * @param now - Clock for record ages, the tally, the cooldown and the block.
20
+ * @returns The four handles.
21
+ */
22
+ export function identityDelivery(paths, token, delivery, now) {
23
+ const identity = identityPaths(paths, token);
24
+ const stats = new DeliveryStats(identity.statsFile, now, paths.locksDir);
25
+ return {
26
+ queue: new EventQueue({
27
+ queueDir: identity.queueDir,
28
+ locksDir: paths.locksDir,
29
+ maxEvents: delivery.maxQueueEvents,
30
+ maxAttempts: delivery.maxAttempts,
31
+ maxEventAgeDays: delivery.maxEventAgeDays,
32
+ now,
33
+ // So the entries the bound sacrifices are counted wherever an enqueue
34
+ // happens, the snapshot pipeline's included.
35
+ stats
36
+ }),
37
+ stats,
38
+ cooldown: new BackendCooldown(identity.cooldownFile, now),
39
+ authBlock: new BackendAuthBlock(identity.authBlockFile, now)
40
+ };
41
+ }
@@ -12,5 +12,7 @@ export type { IdentityPaths } from './queue-partition.js';
12
12
  export { BackendCooldown } from './cooldown.js';
13
13
  export { DeliveryStats } from './delivery-stats.js';
14
14
  export { deliverEvents } from './delivery.js';
15
+ export { identityDelivery } from './identity-delivery.js';
16
+ export type { IdentityDelivery } from './types/delivery-flow.types.js';
15
17
  export { sendEvents } from './send.js';
16
18
  export { discardResponseBody, readCappedJson } from './response-body.js';
@@ -6,5 +6,6 @@ export { identityPaths, queuePartition, settleLegacyQueue, unattributedCount, un
6
6
  export { BackendCooldown } from './cooldown.js';
7
7
  export { DeliveryStats } from './delivery-stats.js';
8
8
  export { deliverEvents } from './delivery.js';
9
+ export { identityDelivery } from './identity-delivery.js';
9
10
  export { sendEvents } from './send.js';
10
11
  export { discardResponseBody, readCappedJson } from './response-body.js';
@@ -6,7 +6,7 @@ import { writeFileAtomic } from '../storage/atomic-file.js';
6
6
  import { acquireLock } from '../storage/lock.js';
7
7
  import { claimSweep } from '../storage/sweep-marker.js';
8
8
  import { SECRET_FILE_MODE, SWEEP_MARKER_FILE } from '../storage/constants/storage.constants.js';
9
- import { ANY_DESTINATION, AUTH_REJECTED_STATUSES, BACKOFF_BASE_MS, BACKOFF_JITTER_MIN, BACKOFF_JITTER_RANGE, BACKOFF_MAX_MS, MAX_ISOLATION_SENDS, MS_PER_DAY, QUEUE_DRAIN_LOCK, QUEUE_FILE_SUFFIX, QUEUE_SCAN_CURSOR_FILE, QUEUE_SWEEP_INTERVAL_MS, RE_UNSAFE_QUEUE_NAME } from './constants/transport.constants.js';
9
+ import { ANY_DESTINATION, AUTH_REJECTED_STATUSES, BACKOFF_BASE_MS, BACKOFF_JITTER_MIN, BACKOFF_JITTER_RANGE, BACKOFF_MAX_MS, MAX_ISOLATION_SENDS, MS_PER_DAY, QUEUE_DRAIN_LOCK, QUEUE_FILE_SUFFIX, QUEUE_SCAN_CURSOR_FILE, QUEUE_SWEEP_INTERVAL_MS, RE_UNSAFE_QUEUE_NAME, TREE_DEFERRED_RETRY_MS } from './constants/transport.constants.js';
10
10
  import { sendEvents } from './send.js';
11
11
  import { queueEntrySchema } from './schemas/queue.schema.js';
12
12
  export { ANY_DESTINATION } from './constants/transport.constants.js';
@@ -103,10 +103,7 @@ export class EventQueue {
103
103
  const collected = await this.collectDue(transport, nowMs, maxBatch);
104
104
  // Oldest first among what was scanned: hash-ordered filenames would
105
105
  // otherwise let a large backlog defer the same late-sorting entries.
106
- const batch = collected.due
107
- .slice()
108
- .sort((a, b) => Date.parse(a.entry.firstQueuedAt) - Date.parse(b.entry.firstQueuedAt))
109
- .slice(0, maxBatch);
106
+ const batch = treesLastAndAlone(collected.due.slice().sort((a, b) => Date.parse(a.entry.firstQueuedAt) - Date.parse(b.entry.firstQueuedAt)), maxBatch);
110
107
  const sent = batch.length === 0 ? EMPTY_DELTA : await this.sendBatch(transport, batch, nowMs, authBlock);
111
108
  const stats = { ...mergeDeltas(mergeDeltas(swept, collected.delta), sent), skipped: false };
112
109
  await report(statsRecorder, stats);
@@ -323,8 +320,16 @@ export class EventQueue {
323
320
  await authBlock?.clear();
324
321
  return { ...EMPTY_DELTA, sent: batch.length, rejected: reportRejected(result.counters?.rejected, 'the drained batch') };
325
322
  }
326
- if (result.deferred)
323
+ if (result.deferred) {
324
+ // A tree the budget cut short would be cut short again on the next hook,
325
+ // re-uploading megabytes on a slow link every time: postpone it, without
326
+ // spending an attempt, since nothing was wrong with it.
327
+ const [only] = batch;
328
+ if (batch.length === 1 && only && isTreeEntry(only)) {
329
+ await writeFileAtomic(only.file, JSON.stringify({ ...only.entry, nextAttemptAt: new Date(nowMs + TREE_DEFERRED_RETRY_MS).toISOString() }), SECRET_FILE_MODE);
330
+ }
327
331
  return { ...EMPTY_DELTA, failed: batch.length };
332
+ }
328
333
  debugLog('queue drain failed', result.error ?? `status ${result.status}`);
329
334
  if (result.status !== undefined && AUTH_REJECTED_STATUSES.has(result.status)) {
330
335
  // The credential was refused, so nothing in this batch is at fault and
@@ -517,6 +522,35 @@ function mergeDeltas(left, right) {
517
522
  rejected: left.rejected + right.rejected
518
523
  };
519
524
  }
525
+ /**
526
+ * The batch one pass sends: everything else first, and a `repo.tree` only
527
+ * alone, when nothing else is due.
528
+ *
529
+ * A tree is up to 4 MiB on its own and the gateway refuses a body over 8 MiB
530
+ * without retry, so two trees, or one beside a backlog, could lose a whole
531
+ * batch. Last, not first: a tree that keeps deferring on a slow link would
532
+ * otherwise sit at the head and starve the spend records behind it, and a
533
+ * catalog tree is never as urgent as a turn.
534
+ *
535
+ * @param ordered - Due entries, oldest first.
536
+ * @param maxBatch - Ceiling on the batch.
537
+ * @returns The entries to send now.
538
+ */
539
+ function treesLastAndAlone(ordered, maxBatch) {
540
+ const others = ordered.filter((due) => !isTreeEntry(due));
541
+ if (others.length > 0)
542
+ return others.slice(0, maxBatch);
543
+ return ordered.slice(0, 1);
544
+ }
545
+ /**
546
+ * Whether a queued entry is a `repo.tree`, the one record large enough to be batched on its own.
547
+ *
548
+ * @param due - A due entry.
549
+ * @returns True for a tree.
550
+ */
551
+ function isTreeEntry(due) {
552
+ return due.entry.event.event?.type === 'repo.tree';
553
+ }
520
554
  /**
521
555
  * Log events the backend permanently refused inside an accepted batch.
522
556
  *
@@ -17,3 +17,10 @@ export interface DeliveryFlowState {
17
17
  readonly result?: DeliveryResult;
18
18
  readonly outcome: DeliveryOutcome;
19
19
  }
20
+ /** One identity's delivery state, opened together so the hook and the CLI read the same files. */
21
+ export interface IdentityDelivery {
22
+ readonly queue: EventQueue;
23
+ readonly stats: DeliveryStats;
24
+ readonly cooldown: BackendCooldown;
25
+ readonly authBlock: BackendAuthBlock;
26
+ }
@@ -1,6 +1,6 @@
1
1
  import { asRecord, compact } from '../core/object.js';
2
2
  import { CLAUDE_CUSTOM_TITLE_FIELDS, CLAUDE_GENERATED_TITLE_FIELDS, RE_CLAUDE_TITLE_LINE } from './constants/turns.constants.js';
3
- import { accumulateUsage, messageIdFor, parseJsonLine, readTranscriptTail, readUntilSettled } from './transcript.js';
3
+ import { accumulateUsage, messageIdFor, parseJsonLine, readTranscriptTail, readUntilSettled, readWindowedTail } from './transcript.js';
4
4
  /**
5
5
  * Authoritative per-turn token usage from Claude Code's transcript JSONL.
6
6
  *
@@ -29,18 +29,10 @@ export async function readTurnUsage(transcriptPath, sinceIso, retry, untilIso, e
29
29
  * @returns The usage found in this pass.
30
30
  */
31
31
  async function readOnce(transcriptPath, sinceIso, untilIso, excludeMessageIds) {
32
- const since = Date.parse(sinceIso);
33
- if (Number.isNaN(since))
34
- return undefined;
35
- // Entries after `until` belong to the next prompt racing into the same file.
36
- // A late FLUSH of *this* turn's entry still passes, because its own message
37
- // timestamp predates the Stop.
38
- const until = untilIso === undefined ? Number.POSITIVE_INFINITY : Date.parse(untilIso);
39
- if (Number.isNaN(until))
40
- return undefined;
41
- const raw = await readTranscriptTail(transcriptPath);
42
- if (raw === undefined)
32
+ const tail = await readWindowedTail(transcriptPath, sinceIso, untilIso);
33
+ if (tail === undefined)
43
34
  return undefined;
35
+ const { raw, since, until } = tail;
44
36
  const byMessageId = new Map();
45
37
  for (const line of raw.split('\n')) {
46
38
  if (!line.trim())
@@ -28,3 +28,12 @@ import type { ReadTurnUsageRetry, TurnUsage } from './types/transcript.types.js'
28
28
  * @returns The turn's usage, or undefined when the rollout reports none.
29
29
  */
30
30
  export declare function readCodexTurnUsage(transcriptPath: string, sinceIso: string, retry?: ReadTurnUsageRetry, untilIso?: string, excludeMessageIds?: ReadonlySet<string>): Promise<TurnUsage | undefined>;
31
+ /**
32
+ * The counts one rollout row carries, when it is an `event_msg` / `token_count` row.
33
+ *
34
+ * Shared with replay, which reads whole rollouts, so both count a request the same way.
35
+ *
36
+ * @param payload - The row's `payload`.
37
+ * @returns The request's counts under the transcript field names, or undefined for any other row.
38
+ */
39
+ export declare function codexRowUsage(payload: Readonly<Record<string, unknown>>): Readonly<Record<string, unknown>> | undefined;
@@ -1,5 +1,5 @@
1
1
  import { asRecord, firstStringOf } from '../core/object.js';
2
- import { accumulateUsage, messageIdFor, parseJsonLine, readTranscriptTail, readUntilSettled } from './transcript.js';
2
+ import { accumulateUsage, messageIdFor, parseJsonLine, readUntilSettled, readWindowedTail } from './transcript.js';
3
3
  import { CODEX_CACHED_INPUT_TOKENS, CODEX_LAST_TOKEN_USAGE, CODEX_TOKEN_COUNT, CODEX_TURN_CONTEXT, TRANSCRIPT_CACHE_READ_TOKENS, TRANSCRIPT_INPUT_TOKENS, TRANSCRIPT_OUTPUT_TOKENS } from './constants/turns.constants.js';
4
4
  /**
5
5
  * Per-turn token usage from a Codex rollout (`~/.codex/sessions/…/rollout-*.jsonl`).
@@ -42,13 +42,10 @@ export async function readCodexTurnUsage(transcriptPath, sinceIso, retry, untilI
42
42
  * @returns The usage found in this pass.
43
43
  */
44
44
  async function readOnce(transcriptPath, sinceIso, untilIso, excludeMessageIds) {
45
- const since = Date.parse(sinceIso);
46
- const until = untilIso === undefined ? Number.POSITIVE_INFINITY : Date.parse(untilIso);
47
- if (Number.isNaN(since) || Number.isNaN(until))
48
- return undefined;
49
- const raw = await readTranscriptTail(transcriptPath);
50
- if (raw === undefined)
45
+ const tail = await readWindowedTail(transcriptPath, sinceIso, untilIso);
46
+ if (tail === undefined)
51
47
  return undefined;
48
+ const { raw, since, until } = tail;
52
49
  const byRowId = new Map();
53
50
  let model;
54
51
  for (const line of raw.split('\n')) {
@@ -63,11 +60,9 @@ async function readOnce(transcriptPath, sinceIso, untilIso, excludeMessageIds) {
63
60
  model = firstStringOf(payload['model']) ?? model;
64
61
  continue;
65
62
  }
66
- if (payload['type'] !== CODEX_TOKEN_COUNT)
67
- continue;
68
- const last = asRecord(asRecord(payload['info'])?.[CODEX_LAST_TOKEN_USAGE]);
63
+ const usage = codexRowUsage(payload);
69
64
  const timestamp = typeof row['timestamp'] === 'string' ? Date.parse(row['timestamp']) : NaN;
70
- if (!last || Number.isNaN(timestamp) || timestamp < since || timestamp > until)
65
+ if (!usage || Number.isNaN(timestamp) || timestamp < since || timestamp > until)
71
66
  continue;
72
67
  // No row id in the format; the line hash carries the row's millisecond
73
68
  // timestamp (and `ordinal`, which 0.150 writes), so it is unique per request
@@ -75,14 +70,27 @@ async function readOnce(transcriptPath, sinceIso, untilIso, excludeMessageIds) {
75
70
  const id = messageIdFor(undefined, line);
76
71
  if (excludeMessageIds?.has(id))
77
72
  continue;
78
- byRowId.set(id, {
79
- model,
80
- usage: {
81
- [TRANSCRIPT_INPUT_TOKENS]: last[TRANSCRIPT_INPUT_TOKENS],
82
- [TRANSCRIPT_OUTPUT_TOKENS]: last[TRANSCRIPT_OUTPUT_TOKENS],
83
- [TRANSCRIPT_CACHE_READ_TOKENS]: last[CODEX_CACHED_INPUT_TOKENS]
84
- }
85
- });
73
+ byRowId.set(id, { model, usage });
86
74
  }
87
75
  return accumulateUsage(byRowId);
88
76
  }
77
+ /**
78
+ * The counts one rollout row carries, when it is an `event_msg` / `token_count` row.
79
+ *
80
+ * Shared with replay, which reads whole rollouts, so both count a request the same way.
81
+ *
82
+ * @param payload - The row's `payload`.
83
+ * @returns The request's counts under the transcript field names, or undefined for any other row.
84
+ */
85
+ export function codexRowUsage(payload) {
86
+ if (payload['type'] !== CODEX_TOKEN_COUNT)
87
+ return undefined;
88
+ const last = asRecord(asRecord(payload['info'])?.[CODEX_LAST_TOKEN_USAGE]);
89
+ if (!last)
90
+ return undefined;
91
+ return {
92
+ [TRANSCRIPT_INPUT_TOKENS]: last[TRANSCRIPT_INPUT_TOKENS],
93
+ [TRANSCRIPT_OUTPUT_TOKENS]: last[TRANSCRIPT_OUTPUT_TOKENS],
94
+ [TRANSCRIPT_CACHE_READ_TOKENS]: last[CODEX_CACHED_INPUT_TOKENS]
95
+ };
96
+ }
@@ -37,6 +37,8 @@ export declare const USAGE_RETRY: ReadTurnUsageRetry;
37
37
  export declare const TRANSCRIPT_TAIL_BYTES: number;
38
38
  /** Payload key every supported agent reports its transcript path under. */
39
39
  export declare const TRANSCRIPT_PATH_KEY = "transcript_path";
40
+ /** Marker of a closed turn; no `.json`, so it is never read as a record (AWT-701). */
41
+ export declare const CLOSED_TURN_PREFIX = "closed-turn--";
40
42
  /** Prefix distinguishing usage-claim files from turn records in a session dir. */
41
43
  export declare const USAGE_CLAIM_PREFIX = "usage-claim--";
42
44
  /**
@@ -44,6 +46,11 @@ export declare const USAGE_CLAIM_PREFIX = "usage-claim--";
44
46
  * session is on, so a later hook's gate can state it.
45
47
  */
46
48
  export declare const SESSION_MODEL_FILE = "session.json";
49
+ /**
50
+ * The mode the agent named when the session started: `{ mode?, isCloudAgent? }`.
51
+ * Cursor says it on sessionStart alone, and every turn summary of the session carries it.
52
+ */
53
+ export declare const SESSION_MODE_FILE = "session-mode.json";
47
54
  /** The folder the session started in, written once by its first hook: `{ cwd }`. */
48
55
  export declare const SESSION_START_FILE = "session-start.json";
49
56
  /**
@@ -68,9 +75,11 @@ export declare const INHERITED_CHECKOUT_FILE = "inherited-checkout.json";
68
75
  * prompt can never move the session. Local only; the keys ride the summary.
69
76
  */
70
77
  export declare const PROMPT_TICKETS_FILE = "prompt-tickets.json";
78
+ /** Session files SessionEnd keeps for a resume under the same id: the opening prompt's keys and the session mode. */
79
+ export declare const SESSION_RESUME_FILES: ReadonlySet<string>;
71
80
  /**
72
- * Files that say nothing about whether a session is still running. The prompt
73
- * memo outlives SessionEnd on purpose, so it must not keep the session live.
81
+ * Files that say nothing about whether a session is still running. The resume
82
+ * memos outlive SessionEnd on purpose, so they must not keep the session live.
74
83
  */
75
84
  export declare const LIVENESS_IGNORED_FILES: ReadonlySet<string>;
76
85
  /** Directory of the state root indexing live agent pids to their session: `<pid>.json`. Not a session. */
@@ -97,10 +106,6 @@ export declare const CHECKOUT_RECORD_PREFIX = "checkout--";
97
106
  export declare const CHECKOUT_KEY_HASH_LENGTH = 16;
98
107
  /** Checkouts one turn may name; past it, new ones are ignored (seven days of real turns peak at five). */
99
108
  export declare const MAX_TURN_CHECKOUTS = 8;
100
- /** Public provider labels; the internal id is an implementation detail. */
101
- export declare const PROVIDER_LABELS: Readonly<Record<string, string>>;
102
- /** Providers whose sessions run inside an editor rather than a terminal. */
103
- export declare const IDE_SURFACE_PROVIDERS: ReadonlySet<string>;
104
109
  /**
105
110
  * The session id of the agent session that launched this one, set by the
106
111
  * launcher (a spinoff in a new terminal tab, a review bot). Hooks inherit the
@@ -109,15 +114,11 @@ export declare const IDE_SURFACE_PROVIDERS: ReadonlySet<string>;
109
114
  export declare const PARENT_SESSION_VAR = "AGENTWATCH_PARENT_SESSION";
110
115
  /** What a declared parent session id may look like: a provider session id, never free text. */
111
116
  export declare const RE_PARENT_SESSION_ID: RegExp;
112
- /** Claude Code reports its own surface here. */
113
- export declare const CLAUDE_ENTRYPOINT_VAR = "CLAUDE_CODE_ENTRYPOINT";
114
117
  export declare const DEFAULT_SURFACE = "cli";
115
118
  export declare const EXEC_SURFACE = "exec";
116
- /** `session_meta.originator` Codex writes for a headless `codex exec` run. */
117
- export declare const CODEX_EXEC_ORIGINATOR = "codex_exec";
118
- /** How much of a Codex transcript to read for its `session_meta` line (about 20 KB in practice). */
119
- export declare const CODEX_META_HEAD_BYTES: number;
120
119
  export declare const IDE_SURFACE = "ide";
120
+ /** A session the agent runs in the vendor's cloud (`session.isCloudAgent`), whichever agent it is. */
121
+ export declare const CLOUD_SURFACE = "cloud";
121
122
  /**
122
123
  * Cap on `files_touched` and `files_read`, matching the platform's own limit on
123
124
  * either list.
@@ -43,6 +43,8 @@ export const USAGE_RETRY = { attempts: 6, delayMs: 250, minSettleMs: 500 };
43
43
  export const TRANSCRIPT_TAIL_BYTES = 4 * 1024 * 1024;
44
44
  /** Payload key every supported agent reports its transcript path under. */
45
45
  export const TRANSCRIPT_PATH_KEY = 'transcript_path';
46
+ /** Marker of a closed turn; no `.json`, so it is never read as a record (AWT-701). */
47
+ export const CLOSED_TURN_PREFIX = 'closed-turn--';
46
48
  /** Prefix distinguishing usage-claim files from turn records in a session dir. */
47
49
  export const USAGE_CLAIM_PREFIX = 'usage-claim--';
48
50
  /**
@@ -50,6 +52,11 @@ export const USAGE_CLAIM_PREFIX = 'usage-claim--';
50
52
  * session is on, so a later hook's gate can state it.
51
53
  */
52
54
  export const SESSION_MODEL_FILE = 'session.json';
55
+ /**
56
+ * The mode the agent named when the session started: `{ mode?, isCloudAgent? }`.
57
+ * Cursor says it on sessionStart alone, and every turn summary of the session carries it.
58
+ */
59
+ export const SESSION_MODE_FILE = 'session-mode.json';
53
60
  /** The folder the session started in, written once by its first hook: `{ cwd }`. */
54
61
  export const SESSION_START_FILE = 'session-start.json';
55
62
  /**
@@ -74,11 +81,13 @@ export const INHERITED_CHECKOUT_FILE = 'inherited-checkout.json';
74
81
  * prompt can never move the session. Local only; the keys ride the summary.
75
82
  */
76
83
  export const PROMPT_TICKETS_FILE = 'prompt-tickets.json';
84
+ /** Session files SessionEnd keeps for a resume under the same id: the opening prompt's keys and the session mode. */
85
+ export const SESSION_RESUME_FILES = new Set([PROMPT_TICKETS_FILE, SESSION_MODE_FILE]);
77
86
  /**
78
- * Files that say nothing about whether a session is still running. The prompt
79
- * memo outlives SessionEnd on purpose, so it must not keep the session live.
87
+ * Files that say nothing about whether a session is still running. The resume
88
+ * memos outlive SessionEnd on purpose, so they must not keep the session live.
80
89
  */
81
- export const LIVENESS_IGNORED_FILES = new Set([PROMPT_TICKETS_FILE]);
90
+ export const LIVENESS_IGNORED_FILES = SESSION_RESUME_FILES;
82
91
  /** Directory of the state root indexing live agent pids to their session: `<pid>.json`. Not a session. */
83
92
  export const AGENT_PIDS_DIR = 'agent-pids';
84
93
  /** Prefix of the once-per-session notice marker a checkout takes: `untraced-<root hash>.json`. Not a turn record. */
@@ -99,6 +108,7 @@ export const BILLING_SENT_FILE = 'billing-sent.json';
99
108
  export const SESSION_FILES = new Set([
100
109
  BILLING_SENT_FILE,
101
110
  SESSION_MODEL_FILE,
111
+ SESSION_MODE_FILE,
102
112
  SESSION_TITLE_FILE,
103
113
  SESSION_START_FILE,
104
114
  WORK_CHECKOUT_FILE,
@@ -112,16 +122,6 @@ export const CHECKOUT_RECORD_PREFIX = 'checkout--';
112
122
  export const CHECKOUT_KEY_HASH_LENGTH = 16;
113
123
  /** Checkouts one turn may name; past it, new ones are ignored (seven days of real turns peak at five). */
114
124
  export const MAX_TURN_CHECKOUTS = 8;
115
- /** Public provider labels; the internal id is an implementation detail. */
116
- export const PROVIDER_LABELS = {
117
- claude: 'claude-code',
118
- codex: 'codex',
119
- cursor: 'cursor',
120
- gemini: 'gemini',
121
- antigravity: 'antigravity'
122
- };
123
- /** Providers whose sessions run inside an editor rather than a terminal. */
124
- export const IDE_SURFACE_PROVIDERS = new Set(['cursor', 'antigravity']);
125
125
  /**
126
126
  * The session id of the agent session that launched this one, set by the
127
127
  * launcher (a spinoff in a new terminal tab, a review bot). Hooks inherit the
@@ -130,15 +130,11 @@ export const IDE_SURFACE_PROVIDERS = new Set(['cursor', 'antigravity']);
130
130
  export const PARENT_SESSION_VAR = 'AGENTWATCH_PARENT_SESSION';
131
131
  /** What a declared parent session id may look like: a provider session id, never free text. */
132
132
  export const RE_PARENT_SESSION_ID = /^[\w.:-]{1,200}$/;
133
- /** Claude Code reports its own surface here. */
134
- export const CLAUDE_ENTRYPOINT_VAR = 'CLAUDE_CODE_ENTRYPOINT';
135
133
  export const DEFAULT_SURFACE = 'cli';
136
134
  export const EXEC_SURFACE = 'exec';
137
- /** `session_meta.originator` Codex writes for a headless `codex exec` run. */
138
- export const CODEX_EXEC_ORIGINATOR = 'codex_exec';
139
- /** How much of a Codex transcript to read for its `session_meta` line (about 20 KB in practice). */
140
- export const CODEX_META_HEAD_BYTES = 256 * 1024;
141
135
  export const IDE_SURFACE = 'ide';
136
+ /** A session the agent runs in the vendor's cloud (`session.isCloudAgent`), whichever agent it is. */
137
+ export const CLOUD_SURFACE = 'cloud';
142
138
  /**
143
139
  * Cap on `files_touched` and `files_read`, matching the platform's own limit on
144
140
  * either list.
@@ -1,4 +1,4 @@
1
- import type { ReadTurnUsageRetry, TurnUsage } from './types/transcript.types.js';
1
+ import type { ReadTurnUsageRetry, TranscriptUsageEntry, TurnUsage } from './types/transcript.types.js';
2
2
  /**
3
3
  * Per-turn token usage from a Gemini CLI chat file
4
4
  * (`~/.gemini/tmp/<project>/chats/session-*.jsonl`).
@@ -28,3 +28,21 @@ import type { ReadTurnUsageRetry, TurnUsage } from './types/transcript.types.js'
28
28
  * @returns The turn's usage, or undefined when the file reports none.
29
29
  */
30
30
  export declare function readGeminiTurnUsage(transcriptPath: string, sinceIso: string, retry?: ReadTurnUsageRetry, untilIso?: string, excludeMessageIds?: ReadonlySet<string>): Promise<TurnUsage | undefined>;
31
+ /**
32
+ * The usage one chat-file record carries, when it is a reply with `tokens`.
33
+ *
34
+ * Shared with replay, which reads whole chat files, so both count a reply the same way.
35
+ *
36
+ * @param row - One message record.
37
+ * @returns The reply's model and counts, or undefined for any other record.
38
+ */
39
+ export declare function geminiReplyUsage(row: Readonly<Record<string, unknown>>): TranscriptUsageEntry | undefined;
40
+ /**
41
+ * The message records one chat-file line carries: the line itself, or, for a
42
+ * `{"$set": {"messages": [...]}}` snapshot (a resumed or rewritten session),
43
+ * each message inside it.
44
+ *
45
+ * @param row - The parsed line.
46
+ * @returns The records to look at.
47
+ */
48
+ export declare function replyRows(row: Record<string, unknown> | undefined): readonly Record<string, unknown>[];