@agent-watch-ai/edge 0.2.5 → 0.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +512 -0
- package/README.md +217 -96
- package/SECURITY.md +69 -0
- package/dist/billing/session-billing.d.ts +19 -0
- package/dist/billing/session-billing.js +52 -0
- package/dist/cli/args.d.ts +10 -0
- package/dist/cli/args.js +20 -0
- package/dist/cli/constants/cli.constants.d.ts +50 -4
- package/dist/cli/constants/cli.constants.js +54 -5
- package/dist/cli/context.d.ts +33 -2
- package/dist/cli/context.js +84 -14
- package/dist/cli/doctor.js +309 -32
- package/dist/cli/hook-check.d.ts +23 -0
- package/dist/cli/hook-check.js +114 -0
- package/dist/cli/hook.js +118 -7
- package/dist/cli/index.d.ts +3 -1
- package/dist/cli/index.js +3 -1
- package/dist/cli/misc.d.ts +11 -0
- package/dist/cli/misc.js +80 -4
- package/dist/cli/npx-install.d.ts +37 -0
- package/dist/cli/npx-install.js +79 -0
- package/dist/cli/open-browser.d.ts +39 -0
- package/dist/cli/open-browser.js +62 -0
- package/dist/cli/setup.d.ts +17 -8
- package/dist/cli/setup.js +439 -98
- package/dist/cli/status.js +82 -13
- package/dist/cli/toggle.d.ts +9 -0
- package/dist/cli/toggle.js +113 -0
- package/dist/cli/types/cli.types.d.ts +37 -3
- package/dist/cli/ui.d.ts +7 -0
- package/dist/cli/ui.js +10 -1
- package/dist/cli/uninstall.js +39 -2
- package/dist/cli/update.d.ts +17 -0
- package/dist/cli/update.js +76 -0
- package/dist/cli.js +100 -13
- package/dist/config/config-store.d.ts +57 -0
- package/dist/config/config-store.js +250 -12
- package/dist/config/config.d.ts +53 -6
- package/dist/config/config.js +66 -6
- package/dist/config/constants/config.constants.d.ts +113 -6
- package/dist/config/constants/config.constants.js +126 -8
- package/dist/config/destination.d.ts +24 -0
- package/dist/config/destination.js +33 -0
- package/dist/config/index.d.ts +7 -4
- package/dist/config/index.js +5 -3
- package/dist/config/repo-config.d.ts +11 -7
- package/dist/config/repo-config.js +64 -15
- package/dist/config/root-config.d.ts +72 -0
- package/dist/config/root-config.js +170 -0
- package/dist/config/schemas/config.schema.d.ts +721 -84
- package/dist/config/schemas/config.schema.js +388 -15
- package/dist/config/types/config.types.d.ts +15 -1
- package/dist/config/types/destination.types.d.ts +8 -0
- package/dist/core/async.d.ts +36 -0
- package/dist/core/async.js +68 -0
- package/dist/core/index.d.ts +1 -1
- package/dist/core/index.js +1 -1
- package/dist/core/object.d.ts +5 -0
- package/dist/core/object.js +9 -4
- package/dist/core/pipe.d.ts +0 -13
- package/dist/core/pipe.js +1 -9
- package/dist/core/types/core.types.d.ts +9 -0
- package/dist/core/types/core.types.js +0 -3
- package/dist/core/which.d.ts +14 -0
- package/dist/core/which.js +43 -3
- package/dist/credentials/budget.d.ts +9 -0
- package/dist/credentials/budget.js +16 -0
- package/dist/credentials/command-runner.d.ts +16 -0
- package/dist/credentials/command-runner.js +37 -0
- package/dist/credentials/constants/credentials.constants.d.ts +111 -0
- package/dist/credentials/constants/credentials.constants.js +112 -0
- package/dist/credentials/credential-status.d.ts +14 -0
- package/dist/credentials/credential-status.js +34 -0
- package/dist/credentials/credentials.d.ts +72 -0
- package/dist/credentials/credentials.js +253 -0
- package/dist/credentials/device-api.d.ts +64 -0
- package/dist/credentials/device-api.js +150 -0
- package/dist/credentials/device-state.d.ts +31 -0
- package/dist/credentials/device-state.js +51 -0
- package/dist/credentials/identity.d.ts +62 -0
- package/dist/credentials/identity.js +90 -0
- package/dist/credentials/index.d.ts +7 -0
- package/dist/credentials/index.js +5 -0
- package/dist/credentials/migration.d.ts +46 -0
- package/dist/credentials/migration.js +164 -0
- package/dist/credentials/schemas/device-api.schema.d.ts +77 -0
- package/dist/credentials/schemas/device-api.schema.js +33 -0
- package/dist/credentials/schemas/device-state.schema.d.ts +51 -0
- package/dist/credentials/schemas/device-state.schema.js +20 -0
- package/dist/credentials/secret-store.d.ts +54 -0
- package/dist/credentials/secret-store.js +217 -0
- package/dist/credentials/sign-in.d.ts +18 -0
- package/dist/credentials/sign-in.js +86 -0
- package/dist/credentials/token-refresher.d.ts +39 -0
- package/dist/credentials/token-refresher.js +185 -0
- package/dist/credentials/types/credentials.types.d.ts +195 -0
- package/dist/credentials/types/credentials.types.js +1 -0
- package/dist/enforcement/constants/enforcement.constants.d.ts +47 -1
- package/dist/enforcement/constants/enforcement.constants.js +56 -0
- package/dist/enforcement/decision-cache.d.ts +20 -1
- package/dist/enforcement/decision-cache.js +65 -2
- package/dist/enforcement/decision-client.d.ts +9 -8
- package/dist/enforcement/decision-client.js +62 -16
- package/dist/enforcement/enforcement.d.ts +23 -2
- package/dist/enforcement/enforcement.js +103 -15
- package/dist/enforcement/index.d.ts +4 -3
- package/dist/enforcement/index.js +3 -2
- package/dist/enforcement/types/enforcement.types.d.ts +64 -0
- package/dist/enrollment/constants/enrollment.constants.d.ts +2 -6
- package/dist/enrollment/constants/enrollment.constants.js +2 -6
- package/dist/enrollment/enrollment.d.ts +16 -0
- package/dist/enrollment/enrollment.js +54 -0
- package/dist/enrollment/index.d.ts +3 -7
- package/dist/enrollment/index.js +1 -1
- package/dist/enrollment/types/enrollment.types.d.ts +9 -13
- package/dist/events/constants/enrich.constants.d.ts +15 -0
- package/dist/events/constants/enrich.constants.js +15 -0
- package/dist/events/constants/events.constants.d.ts +6 -2
- package/dist/events/constants/events.constants.js +5 -1
- package/dist/events/enrich.js +92 -10
- package/dist/events/event-id.d.ts +0 -8
- package/dist/events/event-id.js +0 -10
- package/dist/events/index.d.ts +2 -2
- package/dist/events/index.js +1 -1
- package/dist/events/product-event.d.ts +9 -6
- package/dist/events/types/repo-snapshot.types.d.ts +7 -0
- package/dist/events/types/session-billing.types.d.ts +16 -0
- package/dist/events/types/session-billing.types.js +1 -0
- package/dist/feature/constants/feature.constants.d.ts +7 -0
- package/dist/feature/constants/feature.constants.js +7 -0
- package/dist/feature/index.d.ts +1 -1
- package/dist/feature/index.js +1 -1
- package/dist/feature/ticket-candidates.d.ts +14 -0
- package/dist/feature/ticket-candidates.js +26 -8
- package/dist/git/blob-hash.d.ts +21 -0
- package/dist/git/blob-hash.js +69 -0
- package/dist/git/checkout-root.d.ts +27 -0
- package/dist/git/checkout-root.js +75 -0
- package/dist/git/constants/git.constants.d.ts +160 -0
- package/dist/git/constants/git.constants.js +181 -0
- package/dist/git/fingerprint.d.ts +74 -0
- package/dist/git/fingerprint.js +219 -0
- package/dist/git/gate-checkout.d.ts +44 -0
- package/dist/git/gate-checkout.js +201 -0
- package/dist/git/git-context.d.ts +45 -0
- package/dist/git/git-context.js +61 -3
- package/dist/git/index.d.ts +4 -2
- package/dist/git/index.js +2 -1
- package/dist/git/manifest-repository.d.ts +13 -0
- package/dist/git/manifest-repository.js +92 -0
- package/dist/git/repo-snapshot.d.ts +29 -3
- package/dist/git/repo-snapshot.js +110 -7
- package/dist/git/repository-root.d.ts +39 -0
- package/dist/git/repository-root.js +131 -0
- package/dist/git/types/git.types.d.ts +55 -0
- package/dist/git/types/repository-root.types.d.ts +11 -0
- package/dist/git/types/repository-root.types.js +1 -0
- package/dist/git/types/snapshot.types.d.ts +9 -0
- package/dist/pipeline/constants/pipeline.constants.d.ts +7 -0
- package/dist/pipeline/constants/pipeline.constants.js +7 -0
- package/dist/pipeline/hook-pipeline.js +360 -32
- package/dist/pipeline/types/pipeline.types.d.ts +39 -4
- package/dist/privacy/constants/privacy.constants.js +15 -1
- package/dist/privacy/index.d.ts +1 -0
- package/dist/privacy/index.js +1 -0
- package/dist/privacy/product-capture.d.ts +12 -0
- package/dist/privacy/product-capture.js +73 -0
- package/dist/privacy/sanitizer.d.ts +21 -3
- package/dist/privacy/sanitizer.js +52 -8
- package/dist/process/ancestors.d.ts +39 -0
- package/dist/process/ancestors.js +121 -0
- package/dist/process/constants/process.constants.d.ts +34 -0
- package/dist/process/constants/process.constants.js +34 -0
- package/dist/process/index.d.ts +7 -0
- package/dist/process/index.js +2 -0
- package/dist/process/types/process.types.d.ts +23 -0
- package/dist/process/types/process.types.js +1 -0
- package/dist/providers/antigravity/antigravity.adapter.d.ts +12 -1
- package/dist/providers/antigravity/antigravity.adapter.js +21 -3
- package/dist/providers/antigravity/antigravity.detect.d.ts +0 -9
- package/dist/providers/antigravity/antigravity.detect.js +1 -1
- package/dist/providers/antigravity/antigravity.provider.js +3 -2
- package/dist/providers/antigravity/constants/antigravity.constants.d.ts +2 -0
- package/dist/providers/antigravity/constants/antigravity.constants.js +2 -0
- package/dist/providers/claude/claude.adapter.d.ts +11 -1
- package/dist/providers/claude/claude.adapter.js +26 -5
- package/dist/providers/claude/claude.otel.d.ts +32 -18
- package/dist/providers/claude/claude.otel.js +81 -6
- package/dist/providers/claude/claude.provider.js +5 -2
- package/dist/providers/claude/constants/claude.constants.d.ts +2 -0
- package/dist/providers/claude/constants/claude.constants.js +2 -0
- package/dist/providers/claude/constants/claude.otel.constants.d.ts +4 -0
- package/dist/providers/claude/constants/claude.otel.constants.js +8 -0
- package/dist/providers/codex/codex.adapter.d.ts +15 -1
- package/dist/providers/codex/codex.adapter.js +29 -5
- package/dist/providers/codex/codex.otel.d.ts +24 -2
- package/dist/providers/codex/codex.otel.js +147 -30
- package/dist/providers/codex/codex.provider.js +5 -2
- package/dist/providers/codex/constants/codex.constants.d.ts +2 -0
- package/dist/providers/codex/constants/codex.constants.js +16 -0
- package/dist/providers/codex/constants/codex.otel.constants.d.ts +5 -0
- package/dist/providers/codex/constants/codex.otel.constants.js +5 -0
- package/dist/providers/constants/provider.constants.d.ts +0 -3
- package/dist/providers/constants/provider.constants.js +0 -3
- package/dist/providers/cursor/constants/cursor.constants.d.ts +7 -0
- package/dist/providers/cursor/constants/cursor.constants.js +7 -0
- package/dist/providers/cursor/cursor.adapter.d.ts +33 -1
- package/dist/providers/cursor/cursor.adapter.js +52 -3
- package/dist/providers/cursor/cursor.detect.d.ts +0 -7
- package/dist/providers/cursor/cursor.detect.js +1 -1
- package/dist/providers/cursor/cursor.provider.js +5 -2
- package/dist/providers/gemini/constants/gemini.constants.d.ts +9 -0
- package/dist/providers/gemini/constants/gemini.constants.js +9 -0
- package/dist/providers/gemini/constants/gemini.otel.constants.d.ts +3 -1
- package/dist/providers/gemini/constants/gemini.otel.constants.js +3 -0
- package/dist/providers/gemini/gemini.adapter.d.ts +14 -1
- package/dist/providers/gemini/gemini.adapter.js +25 -5
- package/dist/providers/gemini/gemini.detect.d.ts +0 -7
- package/dist/providers/gemini/gemini.detect.js +1 -1
- package/dist/providers/gemini/gemini.otel.d.ts +19 -20
- package/dist/providers/gemini/gemini.otel.js +121 -17
- package/dist/providers/gemini/gemini.provider.js +2 -1
- package/dist/providers/index.d.ts +1 -1
- package/dist/providers/index.js +1 -1
- package/dist/providers/loaders.d.ts +10 -0
- package/dist/providers/loaders.js +40 -0
- package/dist/providers/provider.d.ts +11 -0
- package/dist/providers/provider.js +1 -1
- package/dist/providers/registry.d.ts +11 -8
- package/dist/providers/registry.js +11 -15
- package/dist/providers/shared/constants/hook-refusal.constants.d.ts +7 -0
- package/dist/providers/shared/constants/hook-refusal.constants.js +7 -0
- package/dist/providers/shared/constants/shell-directories.constants.d.ts +30 -0
- package/dist/providers/shared/constants/shell-directories.constants.js +39 -0
- package/dist/providers/shared/constants/tooling.constants.d.ts +18 -2
- package/dist/providers/shared/constants/tooling.constants.js +19 -3
- package/dist/providers/shared/event-builder.d.ts +12 -14
- package/dist/providers/shared/event-builder.js +20 -14
- package/dist/providers/shared/exporter-allowlist.d.ts +20 -0
- package/dist/providers/shared/exporter-allowlist.js +49 -0
- package/dist/providers/shared/hook-config.d.ts +2 -21
- package/dist/providers/shared/hook-config.js +5 -4
- package/dist/providers/shared/hook-refusal.d.ts +12 -0
- package/dist/providers/shared/hook-refusal.js +17 -1
- package/dist/providers/shared/install-record.d.ts +12 -2
- package/dist/providers/shared/install-record.js +5 -2
- package/dist/providers/shared/shell-directories.d.ts +24 -0
- package/dist/providers/shared/shell-directories.js +90 -0
- package/dist/providers/shared/tooling.d.ts +12 -1
- package/dist/providers/shared/tooling.js +18 -0
- package/dist/providers/shared/types/adapter.types.d.ts +1 -2
- package/dist/providers/types/provider.types.d.ts +57 -1
- package/dist/snapshot/constants/snapshot.constants.d.ts +32 -0
- package/dist/snapshot/constants/snapshot.constants.js +33 -0
- package/dist/snapshot/snapshot-event.js +52 -11
- package/dist/snapshot/snapshot-pipeline.js +1 -1
- package/dist/snapshot/types/snapshot.types.d.ts +2 -0
- package/dist/storage/atomic-file.d.ts +7 -0
- package/dist/storage/atomic-file.js +1 -1
- package/dist/storage/constants/storage.constants.d.ts +17 -0
- package/dist/storage/constants/storage.constants.js +17 -0
- package/dist/storage/disabled.d.ts +13 -0
- package/dist/storage/disabled.js +24 -0
- package/dist/storage/index.d.ts +2 -0
- package/dist/storage/index.js +2 -0
- package/dist/storage/paths.js +3 -2
- package/dist/storage/schemas/storage.schema.d.ts +72 -0
- package/dist/storage/schemas/storage.schema.js +6 -0
- package/dist/storage/sweep-marker.d.ts +19 -0
- package/dist/storage/sweep-marker.js +55 -0
- package/dist/storage/types/storage.types.d.ts +2 -0
- package/dist/storage/uninstalled.d.ts +58 -0
- package/dist/storage/uninstalled.js +107 -0
- package/dist/transport/auth-block.d.ts +66 -0
- package/dist/transport/auth-block.js +129 -0
- package/dist/transport/constants/transport.constants.d.ts +82 -3
- package/dist/transport/constants/transport.constants.js +83 -4
- package/dist/transport/cooldown.js +8 -1
- package/dist/transport/delivery.d.ts +14 -6
- package/dist/transport/delivery.js +80 -55
- package/dist/transport/http-transport.d.ts +1 -0
- package/dist/transport/http-transport.js +52 -10
- package/dist/transport/index.d.ts +6 -3
- package/dist/transport/index.js +3 -0
- package/dist/transport/queue-partition.d.ts +91 -0
- package/dist/transport/queue-partition.js +143 -0
- package/dist/transport/queue.d.ts +99 -20
- package/dist/transport/queue.js +210 -69
- package/dist/transport/response-body.d.ts +25 -0
- package/dist/transport/response-body.js +64 -0
- package/dist/transport/send.d.ts +11 -0
- package/dist/transport/send.js +17 -0
- package/dist/transport/types/delivery-flow.types.d.ts +19 -0
- package/dist/transport/types/delivery-flow.types.js +1 -0
- package/dist/transport/types/transport.types.d.ts +45 -0
- package/dist/turns/claude-transcript.d.ts +19 -1
- package/dist/turns/claude-transcript.js +46 -1
- package/dist/turns/codex-originator.d.ts +11 -0
- package/dist/turns/codex-originator.js +31 -0
- package/dist/turns/codex-transcript.d.ts +30 -0
- package/dist/turns/codex-transcript.js +88 -0
- package/dist/turns/constants/turns.constants.d.ts +141 -5
- package/dist/turns/constants/turns.constants.js +155 -5
- package/dist/turns/file-identity.d.ts +27 -0
- package/dist/turns/file-identity.js +48 -0
- package/dist/turns/gemini-transcript.d.ts +30 -0
- package/dist/turns/gemini-transcript.js +96 -0
- package/dist/turns/index.d.ts +3 -3
- package/dist/turns/index.js +1 -1
- package/dist/turns/inherited-checkout.d.ts +54 -0
- package/dist/turns/inherited-checkout.js +155 -0
- package/dist/turns/native-session-titles.d.ts +26 -0
- package/dist/turns/native-session-titles.js +102 -0
- package/dist/turns/transcript.d.ts +3 -2
- package/dist/turns/transcript.js +5 -1
- package/dist/turns/turn-state.d.ts +224 -3
- package/dist/turns/turn-state.js +456 -21
- package/dist/turns/turn-summary.d.ts +1 -14
- package/dist/turns/turn-summary.js +87 -51
- package/dist/turns/turn-tracker.d.ts +21 -2
- package/dist/turns/turn-tracker.js +379 -56
- package/dist/turns/types/transcript.types.d.ts +5 -0
- package/dist/turns/types/turn-state.types.d.ts +116 -5
- package/dist/turns/types/turn-summary.types.d.ts +64 -14
- package/dist/turns/types/turn-tracker.types.d.ts +27 -1
- package/dist/turns/work-checkout.d.ts +65 -0
- package/dist/turns/work-checkout.js +666 -0
- package/dist/untraced/constants/untraced.constants.d.ts +31 -0
- package/dist/untraced/constants/untraced.constants.js +63 -0
- package/dist/untraced/untraced.d.ts +35 -0
- package/dist/untraced/untraced.js +105 -0
- package/dist/update/constants/update.constants.d.ts +18 -0
- package/dist/update/constants/update.constants.js +20 -0
- package/dist/update/update-check.d.ts +52 -0
- package/dist/update/update-check.js +113 -0
- package/docs/AUDIT_PROMPT.md +129 -0
- package/docs/DATA_HANDLING.md +648 -0
- package/docs/ENTERPRISE_DEPLOYMENT.md +179 -0
- package/docs/PERFORMANCE.md +81 -0
- package/package.json +14 -4
- package/dist/enrollment/enrollment-provider.d.ts +0 -5
- package/dist/enrollment/manual-enrollment.d.ts +0 -32
- package/dist/enrollment/manual-enrollment.js +0 -72
- package/example/README.md +0 -43
- package/example/server.mjs +0 -240
- /package/dist/{enrollment/enrollment-provider.js → config/types/destination.types.js} +0 -0
|
@@ -7,8 +7,25 @@ export const TOOL_COMPLETION_TYPES = new Set([
|
|
|
7
7
|
'file.read',
|
|
8
8
|
'file.edited'
|
|
9
9
|
]);
|
|
10
|
+
/** Canonical event types that mean "a tool is about to run": where a turn names its checkouts. */
|
|
11
|
+
export const TOOL_START_TYPES = new Set(['tool.started', 'shell.started', 'mcp.started']);
|
|
12
|
+
/**
|
|
13
|
+
* Completions whose shell command is read again for a checkout it created. A
|
|
14
|
+
* failed call is `tool.failed` whatever the tool; the provider's `shellCall`
|
|
15
|
+
* answers only for a shell.
|
|
16
|
+
*/
|
|
17
|
+
export const SHELL_REREAD_TYPES = new Set(['shell.completed', 'tool.failed']);
|
|
10
18
|
/** Orphaned turn state (a crash without Stop/SessionEnd) is deleted after this. */
|
|
11
19
|
export const TURN_STATE_TTL_MS = 24 * 60 * 60 * 1000;
|
|
20
|
+
/**
|
|
21
|
+
* How often the expiry sweep may actually walk the state root.
|
|
22
|
+
*
|
|
23
|
+
* It used to run after every closing turn and every session end — a `readdir`
|
|
24
|
+
* plus a `stat` per file of every recent session, on the hook path, almost
|
|
25
|
+
* always to find that nothing was 24 h old yet. Hourly keeps removal well
|
|
26
|
+
* inside the TTL's own tolerance and costs one `stat` on the common path.
|
|
27
|
+
*/
|
|
28
|
+
export const TURN_STATE_SWEEP_INTERVAL_MS = 60 * 60 * 1000;
|
|
12
29
|
/** Overlapping Stop hooks serialize only transcript usage allocation. */
|
|
13
30
|
export const USAGE_LOCK_WAIT_MS = 5_000;
|
|
14
31
|
export const USAGE_LOCK_POLL_MS = 25;
|
|
@@ -28,6 +45,73 @@ export const TRANSCRIPT_TAIL_BYTES = 4 * 1024 * 1024;
|
|
|
28
45
|
export const TRANSCRIPT_PATH_KEY = 'transcript_path';
|
|
29
46
|
/** Prefix distinguishing usage-claim files from turn records in a session dir. */
|
|
30
47
|
export const USAGE_CLAIM_PREFIX = 'usage-claim--';
|
|
48
|
+
/**
|
|
49
|
+
* The one file in a session dir that is not a turn record: what model the
|
|
50
|
+
* session is on, so a later hook's gate can state it.
|
|
51
|
+
*/
|
|
52
|
+
export const SESSION_MODEL_FILE = 'session.json';
|
|
53
|
+
/** The folder the session started in, written once by its first hook: `{ cwd }`. */
|
|
54
|
+
export const SESSION_START_FILE = 'session-start.json';
|
|
55
|
+
/**
|
|
56
|
+
* The checkout the session's last changing turn worked in: `{ root, repository, at }`.
|
|
57
|
+
* Carried by a turn that changes nothing, and read by the prompt gate (AWT-128).
|
|
58
|
+
*/
|
|
59
|
+
export const WORK_CHECKOUT_FILE = 'work-checkout.json';
|
|
60
|
+
/**
|
|
61
|
+
* The agent process this session runs in: `{ pid, started }`. Written by the
|
|
62
|
+
* first hook, so a session started later can recognise it among its ancestors.
|
|
63
|
+
*/
|
|
64
|
+
export const AGENT_PROCESS_FILE = 'agent-process.json';
|
|
65
|
+
/**
|
|
66
|
+
* What the session inherited from the process that started it (AWT-228):
|
|
67
|
+
* `{ parent?, root? }`. Written once, by the first hook of a session whose
|
|
68
|
+
* folder is no checkout, so that no later hook walks the process tree again.
|
|
69
|
+
*/
|
|
70
|
+
export const INHERITED_CHECKOUT_FILE = 'inherited-checkout.json';
|
|
71
|
+
/**
|
|
72
|
+
* The ticket keys the session's opening prompt named: `{ keys }` (AWT-326).
|
|
73
|
+
* Written once, by the first prompt, even when it names none, so that a later
|
|
74
|
+
* prompt can never move the session. Local only; the keys ride the summary.
|
|
75
|
+
*/
|
|
76
|
+
export const PROMPT_TICKETS_FILE = 'prompt-tickets.json';
|
|
77
|
+
/**
|
|
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.
|
|
80
|
+
*/
|
|
81
|
+
export const LIVENESS_IGNORED_FILES = new Set([PROMPT_TICKETS_FILE]);
|
|
82
|
+
/** Directory of the state root indexing live agent pids to their session: `<pid>.json`. Not a session. */
|
|
83
|
+
export const AGENT_PIDS_DIR = 'agent-pids';
|
|
84
|
+
/** Prefix of the once-per-session notice marker a checkout takes: `untraced-<root hash>.json`. Not a turn record. */
|
|
85
|
+
export const UNTRACED_MARKER_PREFIX = 'untraced-';
|
|
86
|
+
/**
|
|
87
|
+
* The session's last known titles: `{ custom?, generated? }`, each kept on its
|
|
88
|
+
* own. A turn whose transcript tail no longer reaches a title entry still sends
|
|
89
|
+
* the ones seen before.
|
|
90
|
+
*/
|
|
91
|
+
export const SESSION_TITLE_FILE = 'session-title.json';
|
|
92
|
+
/**
|
|
93
|
+
* Marker that the session's billing fact (`session.billing`) was handed to
|
|
94
|
+
* delivery, written once (AWT-476). Created only after a mode was classified, so
|
|
95
|
+
* a session that could not tell yet asks again on its next prompt.
|
|
96
|
+
*/
|
|
97
|
+
export const BILLING_SENT_FILE = 'billing-sent.json';
|
|
98
|
+
/** Session-wide files beside the records; none of them is a turn record. */
|
|
99
|
+
export const SESSION_FILES = new Set([
|
|
100
|
+
BILLING_SENT_FILE,
|
|
101
|
+
SESSION_MODEL_FILE,
|
|
102
|
+
SESSION_TITLE_FILE,
|
|
103
|
+
SESSION_START_FILE,
|
|
104
|
+
WORK_CHECKOUT_FILE,
|
|
105
|
+
AGENT_PROCESS_FILE,
|
|
106
|
+
INHERITED_CHECKOUT_FILE,
|
|
107
|
+
PROMPT_TICKETS_FILE
|
|
108
|
+
]);
|
|
109
|
+
/** Prefix of a checkout record's filename: `checkout--<turn>-<root>-<call>.json`. */
|
|
110
|
+
export const CHECKOUT_RECORD_PREFIX = 'checkout--';
|
|
111
|
+
/** Hash length of each part of a checkout record's filename. */
|
|
112
|
+
export const CHECKOUT_KEY_HASH_LENGTH = 16;
|
|
113
|
+
/** Checkouts one turn may name; past it, new ones are ignored (seven days of real turns peak at five). */
|
|
114
|
+
export const MAX_TURN_CHECKOUTS = 8;
|
|
31
115
|
/** Public provider labels; the internal id is an implementation detail. */
|
|
32
116
|
export const PROVIDER_LABELS = {
|
|
33
117
|
claude: 'claude-code',
|
|
@@ -38,12 +122,23 @@ export const PROVIDER_LABELS = {
|
|
|
38
122
|
};
|
|
39
123
|
/** Providers whose sessions run inside an editor rather than a terminal. */
|
|
40
124
|
export const IDE_SURFACE_PROVIDERS = new Set(['cursor', 'antigravity']);
|
|
125
|
+
/**
|
|
126
|
+
* The session id of the agent session that launched this one, set by the
|
|
127
|
+
* launcher (a spinoff in a new terminal tab, a review bot). Hooks inherit the
|
|
128
|
+
* agent's environment, so every hook of the child can read it (AWT-319).
|
|
129
|
+
*/
|
|
130
|
+
export const PARENT_SESSION_VAR = 'AGENTWATCH_PARENT_SESSION';
|
|
131
|
+
/** What a declared parent session id may look like: a provider session id, never free text. */
|
|
132
|
+
export const RE_PARENT_SESSION_ID = /^[\w.:-]{1,200}$/;
|
|
41
133
|
/** Claude Code reports its own surface here. */
|
|
42
134
|
export const CLAUDE_ENTRYPOINT_VAR = 'CLAUDE_CODE_ENTRYPOINT';
|
|
43
135
|
export const DEFAULT_SURFACE = 'cli';
|
|
136
|
+
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;
|
|
44
141
|
export const IDE_SURFACE = 'ide';
|
|
45
|
-
/** Separator between several prompts collapsed into one turn. */
|
|
46
|
-
export const PROMPT_JOIN_SEPARATOR = '\n---\n';
|
|
47
142
|
/**
|
|
48
143
|
* Cap on `files_touched` and `files_read`, matching the platform's own limit on
|
|
49
144
|
* either list.
|
|
@@ -56,14 +151,43 @@ export const PROMPT_JOIN_SEPARATOR = '\n---\n';
|
|
|
56
151
|
* truncated instead; that is a truncated list, not a lost turn.
|
|
57
152
|
*/
|
|
58
153
|
export const MAX_TURN_FILES = 500;
|
|
154
|
+
/**
|
|
155
|
+
* Bounds on the content a summary carries under the content flags. Applied after
|
|
156
|
+
* the text was scrubbed whole, never before: a cut through a credential leaves
|
|
157
|
+
* a prefix no pattern matches.
|
|
158
|
+
*
|
|
159
|
+
* ponytail: fixed character cuts, sized so a request plus a pasted link or a
|
|
160
|
+
* sheet id fits. A turn of 200 shell calls sends its first 50; per-tenant
|
|
161
|
+
* bounds would come from the backend.
|
|
162
|
+
*/
|
|
163
|
+
export const MAX_PROMPT_TEXT_LENGTH = 4000;
|
|
164
|
+
export const MAX_TOOL_INPUT_LENGTH = 2000;
|
|
165
|
+
export const MAX_TURN_TOOL_INPUTS = 50;
|
|
166
|
+
/**
|
|
167
|
+
* The longest session title sent, in UTF-16 units — what core cuts at — and
|
|
168
|
+
* never through a code point. Scrubbed whole first, like prompt text.
|
|
169
|
+
*/
|
|
170
|
+
export const MAX_SESSION_TITLE_LENGTH = 200;
|
|
171
|
+
/**
|
|
172
|
+
* Claude Code's title entries, by type, and the field holding the text: the
|
|
173
|
+
* session name (`--name`, `/rename`, a hook) is sent as `session_name`, the one
|
|
174
|
+
* Claude Code generates as `session_title`. Not
|
|
175
|
+
* `summary`: that type also holds compaction summaries and, in older releases,
|
|
176
|
+
* another conversation's title by `leafUuid`, so its text is not this session's name.
|
|
177
|
+
*/
|
|
178
|
+
export const CLAUDE_CUSTOM_TITLE_FIELDS = { 'custom-title': 'customTitle' };
|
|
179
|
+
export const CLAUDE_GENERATED_TITLE_FIELDS = { 'ai-title': 'aiTitle' };
|
|
180
|
+
/** A transcript line that may be a title entry; the rest of the tail is never parsed for one. */
|
|
181
|
+
export const RE_CLAUDE_TITLE_LINE = /"type"\s*:\s*"(?:custom-title|ai-title)"/;
|
|
59
182
|
/** Fallback tool name for a call the provider did not name. */
|
|
60
183
|
export const UNKNOWN_TOOL_NAME = 'unknown';
|
|
61
|
-
/** Metadata keys the tracker reads off canonical events. */
|
|
62
|
-
export const PROMPT_TEXT_KEY = 'promptText';
|
|
184
|
+
/** Metadata keys the tracker reads off canonical events. Evidence only: no text key exists. */
|
|
63
185
|
export const PROMPT_EVIDENCE_KEY = 'prompt';
|
|
64
|
-
export const RESPONSE_TEXT_KEY = 'responseText';
|
|
65
186
|
export const RESPONSE_EVIDENCE_KEY = 'response';
|
|
66
187
|
export const FILE_PATH_KEY = 'filePath';
|
|
188
|
+
export const EXTERNAL_PATH_KEY = 'externalPath';
|
|
189
|
+
export const PROMPT_TEXT_KEY = 'promptText';
|
|
190
|
+
export const PROMPT_JIRA_IDS_KEY = 'promptJiraIds';
|
|
67
191
|
/** Length of the hashed session directory name. */
|
|
68
192
|
export const SESSION_DIR_HASH_LENGTH = 32;
|
|
69
193
|
/** Length of the hashed lock names, which must be filesystem-safe. */
|
|
@@ -75,6 +199,14 @@ export const TRANSCRIPT_INPUT_TOKENS = 'input_tokens';
|
|
|
75
199
|
export const TRANSCRIPT_OUTPUT_TOKENS = 'output_tokens';
|
|
76
200
|
export const TRANSCRIPT_CACHE_READ_TOKENS = 'cache_read_input_tokens';
|
|
77
201
|
export const TRANSCRIPT_CACHE_CREATION_TOKENS = 'cache_creation_input_tokens';
|
|
202
|
+
/** Codex rollout rows the usage reader needs; every other row is skipped unparsed. */
|
|
203
|
+
export const CODEX_TOKEN_COUNT = 'token_count';
|
|
204
|
+
export const CODEX_TURN_CONTEXT = 'turn_context';
|
|
205
|
+
export const CODEX_LAST_TOKEN_USAGE = 'last_token_usage';
|
|
206
|
+
export const CODEX_CACHED_INPUT_TOKENS = 'cached_input_tokens';
|
|
207
|
+
/** Gemini CLI chat-file row type that carries a model reply, and its usage block. */
|
|
208
|
+
export const GEMINI_REPLY_TYPE = 'gemini';
|
|
209
|
+
export const GEMINI_TOKENS = 'tokens';
|
|
78
210
|
/** Every transcript token field, for weighting which model dominated a turn. */
|
|
79
211
|
export const TRANSCRIPT_TOKEN_FIELDS = [
|
|
80
212
|
TRANSCRIPT_INPUT_TOKENS,
|
|
@@ -84,3 +216,21 @@ export const TRANSCRIPT_TOKEN_FIELDS = [
|
|
|
84
216
|
];
|
|
85
217
|
/** Prefix for the content-hash id given to a transcript entry without one. */
|
|
86
218
|
export const ANONYMOUS_MESSAGE_ID_PREFIX = 'anon-';
|
|
219
|
+
/**
|
|
220
|
+
* Counts the commits between two oids that no remote-tracking ref reaches: the
|
|
221
|
+
* turn's own, as opposed to a pull, a reset or another branch's tip (SPEC §3a).
|
|
222
|
+
*
|
|
223
|
+
* @param from - The oid the turn started on.
|
|
224
|
+
* @param to - The oid or ref it ended on.
|
|
225
|
+
* @param since - Only commits made since then (ISO time): after a switch, the
|
|
226
|
+
* new branch's older commits are not the turn's.
|
|
227
|
+
* @returns The argument vector.
|
|
228
|
+
*/
|
|
229
|
+
export function gitOwnCommitCountArgs(from, to, since) {
|
|
230
|
+
// --ancestry-path: only commits descending from where the turn started, so a
|
|
231
|
+
// reset onto another local-only commit counts none.
|
|
232
|
+
const range = from === undefined ? [to] : ['--ancestry-path', `${from}..${to}`];
|
|
233
|
+
return ['rev-list', '--count', ...(since === undefined ? [] : [`--since=${since}`]), ...range, '--not', '--remotes', '--'];
|
|
234
|
+
}
|
|
235
|
+
/** Commit times are whole seconds; a commit in the second the turn first named its checkout is still the turn's. */
|
|
236
|
+
export const OWN_COMMIT_SINCE_SLACK_MS = 2000;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type { ContentHash } from '../git/types/git.types.js';
|
|
2
|
+
import type { AgentWatchEvent } from '../events/types/events.types.js';
|
|
3
|
+
import type { ToolRecord } from './types/turn-state.types.js';
|
|
4
|
+
import type { TurnStateStore } from './turn-state.js';
|
|
5
|
+
import type { TrackTurnOptions } from './types/turn-tracker.types.js';
|
|
6
|
+
/** What a turn in a folder with no git says about its files, for Agent Watch to match. */
|
|
7
|
+
export interface FileIdentity {
|
|
8
|
+
readonly contentHashes?: readonly ContentHash[];
|
|
9
|
+
readonly manifestRepository?: string;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Content ids of the files the turn's tools named, and the repository address
|
|
13
|
+
* its project manifest names, when the folder has no git context.
|
|
14
|
+
*
|
|
15
|
+
* Runs in the Stop hook only. ponytail: "no git" is the closing event carrying
|
|
16
|
+
* no repository, and with `capture.git` off that is true everywhere, so such a
|
|
17
|
+
* machine reports nothing; telling the two apart would cost the git lookup the
|
|
18
|
+
* flag turned off.
|
|
19
|
+
*
|
|
20
|
+
* @param store - Per-session state store, for the session's start folder.
|
|
21
|
+
* @param sessionId - Provider session id.
|
|
22
|
+
* @param tools - The turn's tool records, in the order they ran.
|
|
23
|
+
* @param stopEvent - The enriched closing event.
|
|
24
|
+
* @param options - Tracking options.
|
|
25
|
+
* @returns The fields to add; empty when files are off, git is present or nothing was found.
|
|
26
|
+
*/
|
|
27
|
+
export declare function resolveFileIdentity(store: TurnStateStore, sessionId: string, tools: readonly ToolRecord[], stopEvent: AgentWatchEvent, options: TrackTurnOptions): Promise<FileIdentity>;
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import path from 'node:path';
|
|
2
|
+
import { withinBudget } from '../core/async.js';
|
|
3
|
+
import { HOME_PLACEHOLDER_PREFIX } from '../events/constants/enrich.constants.js';
|
|
4
|
+
import { contentHashes } from '../git/blob-hash.js';
|
|
5
|
+
import { HASH_BUDGET_MS } from '../git/constants/git.constants.js';
|
|
6
|
+
import { manifestRepository } from '../git/manifest-repository.js';
|
|
7
|
+
/** The contract's bound on `manifest_repository` (AWT-227). */
|
|
8
|
+
const MAX_MANIFEST_REPOSITORY_LENGTH = 500;
|
|
9
|
+
/**
|
|
10
|
+
* Content ids of the files the turn's tools named, and the repository address
|
|
11
|
+
* its project manifest names, when the folder has no git context.
|
|
12
|
+
*
|
|
13
|
+
* Runs in the Stop hook only. ponytail: "no git" is the closing event carrying
|
|
14
|
+
* no repository, and with `capture.git` off that is true everywhere, so such a
|
|
15
|
+
* machine reports nothing; telling the two apart would cost the git lookup the
|
|
16
|
+
* flag turned off.
|
|
17
|
+
*
|
|
18
|
+
* @param store - Per-session state store, for the session's start folder.
|
|
19
|
+
* @param sessionId - Provider session id.
|
|
20
|
+
* @param tools - The turn's tool records, in the order they ran.
|
|
21
|
+
* @param stopEvent - The enriched closing event.
|
|
22
|
+
* @param options - Tracking options.
|
|
23
|
+
* @returns The fields to add; empty when files are off, git is present or nothing was found.
|
|
24
|
+
*/
|
|
25
|
+
export async function resolveFileIdentity(store, sessionId, tools, stopEvent, options) {
|
|
26
|
+
const { capture } = options.config;
|
|
27
|
+
if (!capture.files || !capture.git || stopEvent.git?.repository)
|
|
28
|
+
return {};
|
|
29
|
+
// `externalPath` is the file's place outside every checkout (home-relative as
|
|
30
|
+
// `~/x`, else absolute); it is the only absolute-ish path turn state keeps.
|
|
31
|
+
const absolute = (tool) => {
|
|
32
|
+
const external = tool.externalPath;
|
|
33
|
+
if (!external)
|
|
34
|
+
return undefined;
|
|
35
|
+
return external.startsWith(HOME_PLACEHOLDER_PREFIX) ? path.join(options.env.home, external.slice(HOME_PLACEHOLDER_PREFIX.length)) : external;
|
|
36
|
+
};
|
|
37
|
+
// Edits first, then reads, each in first-seen order.
|
|
38
|
+
const ordered = [...tools.filter((tool) => tool.access !== 'read'), ...tools.filter((tool) => tool.access === 'read')]
|
|
39
|
+
.map(absolute)
|
|
40
|
+
.filter((file) => file !== undefined);
|
|
41
|
+
// The session folder, not the Stop hook's cwd: a mid-session `cd` must not
|
|
42
|
+
// change what the paths are relative to (SPEC §5.7).
|
|
43
|
+
const root = (await store.readStart(sessionId)) ?? options.cwd;
|
|
44
|
+
// The manifest walk gets the hashing's budget too: on a slow mount the hook
|
|
45
|
+
// sends without an address rather than wait for one.
|
|
46
|
+
const [hashes, address] = await Promise.all([contentHashes(ordered, root), withinBudget(manifestRepository(root, options.env.home), HASH_BUDGET_MS)]);
|
|
47
|
+
return { contentHashes: hashes.length > 0 ? hashes : undefined, manifestRepository: address?.slice(0, MAX_MANIFEST_REPOSITORY_LENGTH) };
|
|
48
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import type { ReadTurnUsageRetry, TurnUsage } from './types/transcript.types.js';
|
|
2
|
+
/**
|
|
3
|
+
* Per-turn token usage from a Gemini CLI chat file
|
|
4
|
+
* (`~/.gemini/tmp/<project>/chats/session-*.jsonl`).
|
|
5
|
+
*
|
|
6
|
+
* Gemini CLI appends a row per reply message and appends it again, same `id`,
|
|
7
|
+
* once the response's `usageMetadata` arrives, adding `tokens: {input, output,
|
|
8
|
+
* cached, thoughts, tool, total}` (gemini-cli 0.46.0, `ChatRecordingService`).
|
|
9
|
+
* Only those numbers, the model name and the row's id and timestamp are read;
|
|
10
|
+
* user rows, thoughts and tool calls are never looked at, so nothing a
|
|
11
|
+
* developer wrote leaves this function.
|
|
12
|
+
*
|
|
13
|
+
* This is how Gemini usage reaches the platform on a metadata-only install,
|
|
14
|
+
* where its native OTel stays off because those logs carry tool content.
|
|
15
|
+
*
|
|
16
|
+
* Counts keep Google's meaning: `input` (promptTokenCount) includes the cached
|
|
17
|
+
* part, which `cachedInputTokens` repeats; thinking tokens are billed as output
|
|
18
|
+
* and tool-use prompt tokens as input, so both are folded in.
|
|
19
|
+
*
|
|
20
|
+
* ponytail: reads the same 4 MiB tail as the Claude reader; a turn longer than
|
|
21
|
+
* that undercounts. Upgrade path: remember the byte offset each Stop read to.
|
|
22
|
+
*
|
|
23
|
+
* @param transcriptPath - Path Gemini reported in the hook payload.
|
|
24
|
+
* @param sinceIso - When the turn started; earlier replies are another turn's.
|
|
25
|
+
* @param retry - How long to wait for the chat file flush to settle.
|
|
26
|
+
* @param untilIso - Upper bound, keeping a racing next prompt's replies out.
|
|
27
|
+
* @param excludeMessageIds - Replies another turn already claimed.
|
|
28
|
+
* @returns The turn's usage, or undefined when the file reports none.
|
|
29
|
+
*/
|
|
30
|
+
export declare function readGeminiTurnUsage(transcriptPath: string, sinceIso: string, retry?: ReadTurnUsageRetry, untilIso?: string, excludeMessageIds?: ReadonlySet<string>): Promise<TurnUsage | undefined>;
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import { asRecord, firstStringOf } from '../core/object.js';
|
|
2
|
+
import { finiteOrZero } from '../core/number.js';
|
|
3
|
+
import { accumulateUsage, parseJsonLine, readTranscriptTail, readUntilSettled } from './transcript.js';
|
|
4
|
+
import { GEMINI_REPLY_TYPE, GEMINI_TOKENS, TRANSCRIPT_CACHE_READ_TOKENS, TRANSCRIPT_INPUT_TOKENS, TRANSCRIPT_OUTPUT_TOKENS } from './constants/turns.constants.js';
|
|
5
|
+
/**
|
|
6
|
+
* Per-turn token usage from a Gemini CLI chat file
|
|
7
|
+
* (`~/.gemini/tmp/<project>/chats/session-*.jsonl`).
|
|
8
|
+
*
|
|
9
|
+
* Gemini CLI appends a row per reply message and appends it again, same `id`,
|
|
10
|
+
* once the response's `usageMetadata` arrives, adding `tokens: {input, output,
|
|
11
|
+
* cached, thoughts, tool, total}` (gemini-cli 0.46.0, `ChatRecordingService`).
|
|
12
|
+
* Only those numbers, the model name and the row's id and timestamp are read;
|
|
13
|
+
* user rows, thoughts and tool calls are never looked at, so nothing a
|
|
14
|
+
* developer wrote leaves this function.
|
|
15
|
+
*
|
|
16
|
+
* This is how Gemini usage reaches the platform on a metadata-only install,
|
|
17
|
+
* where its native OTel stays off because those logs carry tool content.
|
|
18
|
+
*
|
|
19
|
+
* Counts keep Google's meaning: `input` (promptTokenCount) includes the cached
|
|
20
|
+
* part, which `cachedInputTokens` repeats; thinking tokens are billed as output
|
|
21
|
+
* and tool-use prompt tokens as input, so both are folded in.
|
|
22
|
+
*
|
|
23
|
+
* ponytail: reads the same 4 MiB tail as the Claude reader; a turn longer than
|
|
24
|
+
* that undercounts. Upgrade path: remember the byte offset each Stop read to.
|
|
25
|
+
*
|
|
26
|
+
* @param transcriptPath - Path Gemini reported in the hook payload.
|
|
27
|
+
* @param sinceIso - When the turn started; earlier replies are another turn's.
|
|
28
|
+
* @param retry - How long to wait for the chat file flush to settle.
|
|
29
|
+
* @param untilIso - Upper bound, keeping a racing next prompt's replies out.
|
|
30
|
+
* @param excludeMessageIds - Replies another turn already claimed.
|
|
31
|
+
* @returns The turn's usage, or undefined when the file reports none.
|
|
32
|
+
*/
|
|
33
|
+
export async function readGeminiTurnUsage(transcriptPath, sinceIso, retry, untilIso, excludeMessageIds) {
|
|
34
|
+
return readUntilSettled(() => readOnce(transcriptPath, sinceIso, untilIso, excludeMessageIds), retry);
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* One pass over the chat file tail.
|
|
38
|
+
*
|
|
39
|
+
* @param transcriptPath - Chat file.
|
|
40
|
+
* @param sinceIso - Lower bound of the turn's window.
|
|
41
|
+
* @param untilIso - Upper bound, or undefined for "no upper bound".
|
|
42
|
+
* @param excludeMessageIds - Replies another turn already claimed.
|
|
43
|
+
* @returns The usage found in this pass.
|
|
44
|
+
*/
|
|
45
|
+
async function readOnce(transcriptPath, sinceIso, untilIso, excludeMessageIds) {
|
|
46
|
+
const since = Date.parse(sinceIso);
|
|
47
|
+
const until = untilIso === undefined ? Number.POSITIVE_INFINITY : Date.parse(untilIso);
|
|
48
|
+
if (Number.isNaN(since) || Number.isNaN(until))
|
|
49
|
+
return undefined;
|
|
50
|
+
const raw = await readTranscriptTail(transcriptPath);
|
|
51
|
+
if (raw === undefined)
|
|
52
|
+
return undefined;
|
|
53
|
+
// Later rows with the same id replace earlier ones: the rewrite is the one with tokens.
|
|
54
|
+
const byId = new Map();
|
|
55
|
+
for (const line of raw.split('\n')) {
|
|
56
|
+
if (!line.includes(`"${GEMINI_TOKENS}"`))
|
|
57
|
+
continue;
|
|
58
|
+
for (const row of replyRows(parseJsonLine(line))) {
|
|
59
|
+
const tokens = asRecord(row[GEMINI_TOKENS]);
|
|
60
|
+
const id = firstStringOf(row['id']);
|
|
61
|
+
const timestamp = typeof row['timestamp'] === 'string' ? Date.parse(row['timestamp']) : NaN;
|
|
62
|
+
if (!tokens || !id || row['type'] !== GEMINI_REPLY_TYPE)
|
|
63
|
+
continue;
|
|
64
|
+
if (Number.isNaN(timestamp) || timestamp < since || timestamp > until || excludeMessageIds?.has(id))
|
|
65
|
+
continue;
|
|
66
|
+
byId.set(id, {
|
|
67
|
+
model: firstStringOf(row['model']),
|
|
68
|
+
usage: {
|
|
69
|
+
[TRANSCRIPT_INPUT_TOKENS]: finiteOrZero(tokens['input']) + finiteOrZero(tokens['tool']),
|
|
70
|
+
[TRANSCRIPT_OUTPUT_TOKENS]: finiteOrZero(tokens['output']) + finiteOrZero(tokens['thoughts']),
|
|
71
|
+
[TRANSCRIPT_CACHE_READ_TOKENS]: finiteOrZero(tokens['cached'])
|
|
72
|
+
}
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
return accumulateUsage(byId);
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* The message records one chat-file line carries: the line itself, or, for a
|
|
80
|
+
* `{"$set": {"messages": [...]}}` snapshot (a resumed or rewritten session),
|
|
81
|
+
* each message inside it.
|
|
82
|
+
*
|
|
83
|
+
* @param row - The parsed line.
|
|
84
|
+
* @returns The records to look at.
|
|
85
|
+
*/
|
|
86
|
+
function replyRows(row) {
|
|
87
|
+
if (!row)
|
|
88
|
+
return [];
|
|
89
|
+
const snapshot = asRecord(row['$set'])?.['messages'];
|
|
90
|
+
if (!Array.isArray(snapshot))
|
|
91
|
+
return [row];
|
|
92
|
+
return snapshot.flatMap((message) => {
|
|
93
|
+
const record = asRecord(message);
|
|
94
|
+
return record ? [record] : [];
|
|
95
|
+
});
|
|
96
|
+
}
|
package/dist/turns/index.d.ts
CHANGED
|
@@ -3,13 +3,13 @@
|
|
|
3
3
|
* closes the turn into one flat summary, and the backend finalizes its usage
|
|
4
4
|
* from the atomic llm.call ledger.
|
|
5
5
|
*/
|
|
6
|
-
export type { AgentUsageSummary, BuildTurnSummaryInput, TouchedFiles,
|
|
7
|
-
export type { PromptRecord, ResponseRecord, ToolRecord, TurnRecord, TurnStateEntry } from './types/turn-state.types.js';
|
|
6
|
+
export type { AgentUsageSummary, BuildTurnSummaryInput, TouchedFiles, TurnSummaryEvent, TurnUsageStatus } from './types/turn-summary.types.js';
|
|
7
|
+
export type { PromptRecord, ResponseRecord, SessionModelRecord, ToolRecord, TurnRecord, TurnStateEntry } from './types/turn-state.types.js';
|
|
8
8
|
export type { ReadTurnUsageRetry, TranscriptReader, TranscriptUsageEntry, TurnUsage } from './types/transcript.types.js';
|
|
9
9
|
export type { TrackTurnOptions, TurnWindow } from './types/turn-tracker.types.js';
|
|
10
10
|
export type { AggregateTurnUsageOptions, UsageTotals } from './types/aggregate-usage.types.js';
|
|
11
11
|
export { PROVIDER_LABELS, TURN_STATE_TTL_MS, USAGE_RETRY } from './constants/turns.constants.js';
|
|
12
|
-
export {
|
|
12
|
+
export { buildTurnSummary } from './turn-summary.js';
|
|
13
13
|
export { TurnStateStore } from './turn-state.js';
|
|
14
14
|
export { readTurnUsage } from './claude-transcript.js';
|
|
15
15
|
export { readCursorTurnUsage } from './cursor-transcript.js';
|
package/dist/turns/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
export { PROVIDER_LABELS, TURN_STATE_TTL_MS, USAGE_RETRY } from './constants/turns.constants.js';
|
|
2
|
-
export {
|
|
2
|
+
export { buildTurnSummary } from './turn-summary.js';
|
|
3
3
|
export { TurnStateStore } from './turn-state.js';
|
|
4
4
|
export { readTurnUsage } from './claude-transcript.js';
|
|
5
5
|
export { readCursorTurnUsage } from './cursor-transcript.js';
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import type { TurnStateStore } from './turn-state.js';
|
|
2
|
+
import type { TrackTurnOptions } from './types/turn-tracker.types.js';
|
|
3
|
+
/** A checkout a session inherited, and the parent agent session it came from, when it came from one. */
|
|
4
|
+
export interface InheritedCheckout {
|
|
5
|
+
readonly root: string;
|
|
6
|
+
readonly parentSessionId?: string;
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* On a session's first hook: remember which process is its agent, and — when
|
|
10
|
+
* its folder is no checkout — what started that agent.
|
|
11
|
+
*
|
|
12
|
+
* Review bots, orchestrators and SDK apps start agents in folders with no git,
|
|
13
|
+
* and the process that started them usually knows the repository. The walk is
|
|
14
|
+
* one `ps` and, when no live parent session already answers, one `lsof`, all
|
|
15
|
+
* under one {@link WALK_DEADLINE_MS} deadline. A walk that ends, found or not,
|
|
16
|
+
* is final: the answer is written down. One the deadline cut is not (AWT-378):
|
|
17
|
+
* the cut is counted in the memo and {@link retryInheritance} walks again on
|
|
18
|
+
* the next hook. Process ids and folders stay in local state: only the
|
|
19
|
+
* checkout fields and the parent session id leave the machine, through the
|
|
20
|
+
* summary. Any failure leaves the session as it was.
|
|
21
|
+
*
|
|
22
|
+
* @param store - Per-session state store.
|
|
23
|
+
* @param sessionId - Provider session id.
|
|
24
|
+
* @param options - Tracking options.
|
|
25
|
+
* @param cuts - How many earlier walks the deadline cut.
|
|
26
|
+
*/
|
|
27
|
+
export declare function rememberInheritance(store: TurnStateStore, sessionId: string, options: TrackTurnOptions, cuts?: number): Promise<void>;
|
|
28
|
+
/**
|
|
29
|
+
* Walk again when the session's last walk was cut by its deadline, or failed.
|
|
30
|
+
*
|
|
31
|
+
* A busy machine can push `ps` past the deadline, and a cut walk read as final
|
|
32
|
+
* leaves every turn of the session without its repository. Each retry has the
|
|
33
|
+
* same deadline as the first walk, at most one runs per hook, and after
|
|
34
|
+
* {@link MAX_WALK_ATTEMPTS} the session gives up. A session whose walk ended
|
|
35
|
+
* pays one small file read per hook and never walks again.
|
|
36
|
+
*
|
|
37
|
+
* @param store - Per-session state store.
|
|
38
|
+
* @param sessionId - Provider session id.
|
|
39
|
+
* @param options - Tracking options.
|
|
40
|
+
*/
|
|
41
|
+
export declare function retryInheritance(store: TurnStateStore, sessionId: string, options: TrackTurnOptions): Promise<void>;
|
|
42
|
+
/**
|
|
43
|
+
* The checkout this session inherited, as of now.
|
|
44
|
+
*
|
|
45
|
+
* The nearest live parent agent session's last work checkout; failing that,
|
|
46
|
+
* the checkout the nearest ancestor's folder lay in. A parent that has ended or
|
|
47
|
+
* expired is skipped, and so is anything the caller does not admit.
|
|
48
|
+
*
|
|
49
|
+
* @param store - Per-session state store.
|
|
50
|
+
* @param sessionId - Provider session id.
|
|
51
|
+
* @param admits - The session's admission test for a checkout.
|
|
52
|
+
* @returns The checkout, or undefined when the session inherited none.
|
|
53
|
+
*/
|
|
54
|
+
export declare function resolveInherited(store: TurnStateStore, sessionId: string, admits: (root: string) => boolean): Promise<InheritedCheckout | undefined>;
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
import path from 'node:path';
|
|
2
|
+
import { canonicalRoot } from '../config/root-config.js';
|
|
3
|
+
import { debugLog } from '../core/logger.js';
|
|
4
|
+
import { checkoutRootOf } from '../git/checkout-root.js';
|
|
5
|
+
import { MAX_WALK_ATTEMPTS, WALK_DEADLINE_MS, agentProcess, ancestorChain, systemProcessTable } from '../process/index.js';
|
|
6
|
+
import { TURN_STATE_TTL_MS } from './constants/turns.constants.js';
|
|
7
|
+
/**
|
|
8
|
+
* On a session's first hook: remember which process is its agent, and — when
|
|
9
|
+
* its folder is no checkout — what started that agent.
|
|
10
|
+
*
|
|
11
|
+
* Review bots, orchestrators and SDK apps start agents in folders with no git,
|
|
12
|
+
* and the process that started them usually knows the repository. The walk is
|
|
13
|
+
* one `ps` and, when no live parent session already answers, one `lsof`, all
|
|
14
|
+
* under one {@link WALK_DEADLINE_MS} deadline. A walk that ends, found or not,
|
|
15
|
+
* is final: the answer is written down. One the deadline cut is not (AWT-378):
|
|
16
|
+
* the cut is counted in the memo and {@link retryInheritance} walks again on
|
|
17
|
+
* the next hook. Process ids and folders stay in local state: only the
|
|
18
|
+
* checkout fields and the parent session id leave the machine, through the
|
|
19
|
+
* summary. Any failure leaves the session as it was.
|
|
20
|
+
*
|
|
21
|
+
* @param store - Per-session state store.
|
|
22
|
+
* @param sessionId - Provider session id.
|
|
23
|
+
* @param options - Tracking options.
|
|
24
|
+
* @param cuts - How many earlier walks the deadline cut.
|
|
25
|
+
*/
|
|
26
|
+
export async function rememberInheritance(store, sessionId, options, cuts = 0) {
|
|
27
|
+
const deadline = new AbortController();
|
|
28
|
+
const timer = setTimeout(() => deadline.abort(), WALK_DEADLINE_MS);
|
|
29
|
+
let found;
|
|
30
|
+
let threw = false;
|
|
31
|
+
try {
|
|
32
|
+
// Raced, not merely checked: a `ps` that never answers must not hold the
|
|
33
|
+
// agent's hook. The deadline also kills the walk's `ps`/`lsof` children,
|
|
34
|
+
// and the abandoned walk writes nothing after it.
|
|
35
|
+
found = await Promise.race([walk(store, sessionId, options, deadline.signal), new Promise((resolve) => deadline.signal.addEventListener('abort', () => resolve(undefined)))]);
|
|
36
|
+
}
|
|
37
|
+
catch {
|
|
38
|
+
// A fixed sentence: the error could quote a path or a process.
|
|
39
|
+
debugLog('could not walk the processes above the agent');
|
|
40
|
+
threw = true;
|
|
41
|
+
}
|
|
42
|
+
finally {
|
|
43
|
+
clearTimeout(timer);
|
|
44
|
+
}
|
|
45
|
+
const cut = deadline.signal.aborted;
|
|
46
|
+
if (cut)
|
|
47
|
+
debugLog('the walk above the agent ran out of time');
|
|
48
|
+
// A cut or failed walk is counted, to be tried again; one that ended is
|
|
49
|
+
// final. A first walk that found nothing has nothing to write; a retry that
|
|
50
|
+
// found nothing still overwrites its count, so it is not walked again.
|
|
51
|
+
const memo = cut || threw ? { cuts: cuts + 1 } : (found ?? (cuts > 0 ? {} : undefined));
|
|
52
|
+
if (memo === undefined)
|
|
53
|
+
return;
|
|
54
|
+
try {
|
|
55
|
+
// ponytail: read-then-write, not a lock. Two hooks of one session racing
|
|
56
|
+
// their retries could both pass; the later one keeps its hands off an
|
|
57
|
+
// answer the other wrote. The window is one file read wide.
|
|
58
|
+
if (cuts > 0 && (await store.readInherited(sessionId))?.cuts !== cuts)
|
|
59
|
+
return;
|
|
60
|
+
await store.rememberInherited(sessionId, memo);
|
|
61
|
+
}
|
|
62
|
+
catch {
|
|
63
|
+
debugLog('could not remember what the session inherited');
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Walk again when the session's last walk was cut by its deadline, or failed.
|
|
68
|
+
*
|
|
69
|
+
* A busy machine can push `ps` past the deadline, and a cut walk read as final
|
|
70
|
+
* leaves every turn of the session without its repository. Each retry has the
|
|
71
|
+
* same deadline as the first walk, at most one runs per hook, and after
|
|
72
|
+
* {@link MAX_WALK_ATTEMPTS} the session gives up. A session whose walk ended
|
|
73
|
+
* pays one small file read per hook and never walks again.
|
|
74
|
+
*
|
|
75
|
+
* @param store - Per-session state store.
|
|
76
|
+
* @param sessionId - Provider session id.
|
|
77
|
+
* @param options - Tracking options.
|
|
78
|
+
*/
|
|
79
|
+
export async function retryInheritance(store, sessionId, options) {
|
|
80
|
+
const cuts = (await store.readInherited(sessionId).catch(() => undefined))?.cuts ?? 0;
|
|
81
|
+
if (cuts === 0 || cuts >= MAX_WALK_ATTEMPTS)
|
|
82
|
+
return;
|
|
83
|
+
// From where the session started, as the first walk was: the agent may have
|
|
84
|
+
// moved into a checkout since, and that must not decide what it inherited.
|
|
85
|
+
const start = await store.readStart(sessionId).catch(() => undefined);
|
|
86
|
+
await rememberInheritance(store, sessionId, { ...options, cwd: start ?? options.cwd }, cuts);
|
|
87
|
+
}
|
|
88
|
+
async function walk(store, sessionId, options, expired) {
|
|
89
|
+
const table = options.env.processes ?? systemProcessTable(expired);
|
|
90
|
+
const walks = options.config.capture.git && (await checkoutRootOf(options.cwd)) === undefined;
|
|
91
|
+
// Together: the listing and the agent's own line are two `ps` runs.
|
|
92
|
+
const [listing, agent] = await Promise.all([walks ? table.snapshot() : undefined, agentProcess(table)]);
|
|
93
|
+
if (!agent || expired.aborted)
|
|
94
|
+
return undefined;
|
|
95
|
+
await store.rememberAgentProcess(sessionId, { pid: agent.pid, started: agent.started });
|
|
96
|
+
// Checked again before each further lookup: past the deadline a `ps` would be spawned only to be killed.
|
|
97
|
+
if (!listing || expired.aborted)
|
|
98
|
+
return undefined;
|
|
99
|
+
const chain = ancestorChain(listing, agent.pid);
|
|
100
|
+
const parent = await liveParentOf(store, sessionId, chain, table);
|
|
101
|
+
// The parent's checkout is read again at the close, when it is freshest, but
|
|
102
|
+
// kept here too: a parent that ends first must not take the answer with it.
|
|
103
|
+
// The ancestors' folders are looked up only when the parent cannot answer.
|
|
104
|
+
const parentRoot = parent === undefined ? undefined : (await store.readWorkCheckout(parent))?.root;
|
|
105
|
+
const root = parentRoot ?? (expired.aborted ? undefined : await nearestCheckout(table.cwds.bind(table), chain.map((ancestor) => ancestor.pid)));
|
|
106
|
+
if (expired.aborted || (parent === undefined && root === undefined))
|
|
107
|
+
return undefined;
|
|
108
|
+
return { parent, root };
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* The checkout this session inherited, as of now.
|
|
112
|
+
*
|
|
113
|
+
* The nearest live parent agent session's last work checkout; failing that,
|
|
114
|
+
* the checkout the nearest ancestor's folder lay in. A parent that has ended or
|
|
115
|
+
* expired is skipped, and so is anything the caller does not admit.
|
|
116
|
+
*
|
|
117
|
+
* @param store - Per-session state store.
|
|
118
|
+
* @param sessionId - Provider session id.
|
|
119
|
+
* @param admits - The session's admission test for a checkout.
|
|
120
|
+
* @returns The checkout, or undefined when the session inherited none.
|
|
121
|
+
*/
|
|
122
|
+
export async function resolveInherited(store, sessionId, admits) {
|
|
123
|
+
const memo = await store.readInherited(sessionId);
|
|
124
|
+
if (!memo)
|
|
125
|
+
return undefined;
|
|
126
|
+
const parentMemo = memo.parent !== undefined && (await store.isLive(memo.parent, TURN_STATE_TTL_MS)) ? await store.readWorkCheckout(memo.parent) : undefined;
|
|
127
|
+
if (parentMemo && admits(parentMemo.root))
|
|
128
|
+
return { root: canonicalRoot(parentMemo.root), parentSessionId: memo.parent };
|
|
129
|
+
if (memo.root === undefined || !path.isAbsolute(memo.root) || !admits(memo.root))
|
|
130
|
+
return undefined;
|
|
131
|
+
return { root: canonicalRoot(memo.root) };
|
|
132
|
+
}
|
|
133
|
+
async function liveParentOf(store, sessionId, chain, table) {
|
|
134
|
+
for (const ancestor of chain) {
|
|
135
|
+
const hit = await store.sessionOfAgent(ancestor.pid);
|
|
136
|
+
if (!hit || hit.sessionId === sessionId)
|
|
137
|
+
continue;
|
|
138
|
+
// The start time is asked only of a pid that was an agent: a reused one is not.
|
|
139
|
+
if (hit.started !== undefined && (await table.info(ancestor.pid))?.started !== hit.started)
|
|
140
|
+
continue;
|
|
141
|
+
if (await store.isLive(hit.sessionId, TURN_STATE_TTL_MS))
|
|
142
|
+
return hit.sessionId;
|
|
143
|
+
}
|
|
144
|
+
return undefined;
|
|
145
|
+
}
|
|
146
|
+
async function nearestCheckout(cwds, pids) {
|
|
147
|
+
const folders = await cwds(pids);
|
|
148
|
+
for (const pid of pids) {
|
|
149
|
+
const folder = folders.get(pid);
|
|
150
|
+
const root = folder !== undefined && path.isAbsolute(folder) ? await checkoutRootOf(folder) : undefined;
|
|
151
|
+
if (root !== undefined)
|
|
152
|
+
return root;
|
|
153
|
+
}
|
|
154
|
+
return undefined;
|
|
155
|
+
}
|