@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
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { BODY_TOO_LARGE, CONTENT_LENGTH_HEADER, MAX_RESPONSE_BYTES } from './constants/transport.constants.js';
|
|
2
|
+
/**
|
|
3
|
+
* Read a small JSON body, refusing one too large to be the answer.
|
|
4
|
+
*
|
|
5
|
+
* Both bodies this guards — the batch's per-event counters and the enforcement
|
|
6
|
+
* decision — are a handful of integers, and both are decoded inside the coding
|
|
7
|
+
* agent's hook process. A compromised, misconfigured or proxy-intercepted
|
|
8
|
+
* endpoint can answer 200 with an arbitrarily large body, and the send timeout
|
|
9
|
+
* bounds the *request*, not the decode: without a cap the hook allocates
|
|
10
|
+
* whatever it is sent.
|
|
11
|
+
*
|
|
12
|
+
* `content-length` is checked first because it is free, and the stream is
|
|
13
|
+
* capped as well because a chunked response declares no length. Throwing is the
|
|
14
|
+
* right failure here: both call sites already treat an unreadable body as "no
|
|
15
|
+
* answer" and fail open.
|
|
16
|
+
*
|
|
17
|
+
* @param response - The response to decode.
|
|
18
|
+
* @returns The parsed body.
|
|
19
|
+
*/
|
|
20
|
+
export async function readCappedJson(response) {
|
|
21
|
+
if (Number(response.headers.get(CONTENT_LENGTH_HEADER)) > MAX_RESPONSE_BYTES) {
|
|
22
|
+
discardResponseBody(response);
|
|
23
|
+
throw new Error(BODY_TOO_LARGE);
|
|
24
|
+
}
|
|
25
|
+
return JSON.parse(await readCapped(response));
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Decode a body, giving up once it passes the cap.
|
|
29
|
+
*
|
|
30
|
+
* @param response - The response whose stream to read.
|
|
31
|
+
* @returns The decoded text.
|
|
32
|
+
*/
|
|
33
|
+
async function readCapped(response) {
|
|
34
|
+
if (!response.body)
|
|
35
|
+
return '';
|
|
36
|
+
const reader = response.body.getReader();
|
|
37
|
+
const decoder = new TextDecoder();
|
|
38
|
+
let text = '';
|
|
39
|
+
let total = 0;
|
|
40
|
+
try {
|
|
41
|
+
while (true) {
|
|
42
|
+
const { done, value } = await reader.read();
|
|
43
|
+
if (done)
|
|
44
|
+
return text + decoder.decode();
|
|
45
|
+
total += value.byteLength;
|
|
46
|
+
if (total > MAX_RESPONSE_BYTES) {
|
|
47
|
+
void reader.cancel().catch(() => undefined);
|
|
48
|
+
throw new Error(BODY_TOO_LARGE);
|
|
49
|
+
}
|
|
50
|
+
text += decoder.decode(value, { stream: true });
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
finally {
|
|
54
|
+
reader.releaseLock();
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Release unread responses without making cleanup part of the hook's latency.
|
|
59
|
+
* @param response - A response whose body the caller will not consume.
|
|
60
|
+
* @returns Nothing; cancellation errors cannot affect delivery or enforcement.
|
|
61
|
+
*/
|
|
62
|
+
export function discardResponseBody(response) {
|
|
63
|
+
void response.body?.cancel().catch(() => undefined);
|
|
64
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { ProductEvent } from '../events/product-event.js';
|
|
2
|
+
import type { DeliveryResult, EventTransport } from './types/transport.types.js';
|
|
3
|
+
/**
|
|
4
|
+
* Convert adapter exceptions to retryable failures without exposing request data.
|
|
5
|
+
* HTTP status owns retry semantics: the gateway reports partial publish failure
|
|
6
|
+
* as 503; accepted-batch counters are diagnostic, not transport failures.
|
|
7
|
+
* @param transport - The selected destination adapter.
|
|
8
|
+
* @param events - Records whose delivery must be confirmed.
|
|
9
|
+
* @returns The adapter result, or a safe retryable failure.
|
|
10
|
+
*/
|
|
11
|
+
export declare function sendEvents(transport: EventTransport, events: readonly ProductEvent[]): Promise<DeliveryResult>;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Convert adapter exceptions to retryable failures without exposing request data.
|
|
3
|
+
* HTTP status owns retry semantics: the gateway reports partial publish failure
|
|
4
|
+
* as 503; accepted-batch counters are diagnostic, not transport failures.
|
|
5
|
+
* @param transport - The selected destination adapter.
|
|
6
|
+
* @param events - Records whose delivery must be confirmed.
|
|
7
|
+
* @returns The adapter result, or a safe retryable failure.
|
|
8
|
+
*/
|
|
9
|
+
export async function sendEvents(transport, events) {
|
|
10
|
+
try {
|
|
11
|
+
return await transport.send(events);
|
|
12
|
+
}
|
|
13
|
+
catch {
|
|
14
|
+
// Adapter errors can contain URLs, headers or captured content.
|
|
15
|
+
return { ok: false, retryable: true, error: 'transport failed' };
|
|
16
|
+
}
|
|
17
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { ProductEvent } from '../../events/product-event.js';
|
|
2
|
+
import type { BackendAuthBlock } from '../auth-block.js';
|
|
3
|
+
import type { BackendCooldown } from '../cooldown.js';
|
|
4
|
+
import type { DeliveryStats } from '../delivery-stats.js';
|
|
5
|
+
import type { EventQueue } from '../queue.js';
|
|
6
|
+
import type { DeliveryOutcome, DeliveryResult, EventTransport } from './transport.types.js';
|
|
7
|
+
/** Immutable state for deciding, attempting, preserving and reporting delivery. */
|
|
8
|
+
export interface DeliveryFlowState {
|
|
9
|
+
readonly events: readonly ProductEvent[];
|
|
10
|
+
readonly transport: EventTransport | undefined;
|
|
11
|
+
readonly queue: EventQueue;
|
|
12
|
+
readonly drainBatchSize: number;
|
|
13
|
+
readonly cooldown: BackendCooldown | undefined;
|
|
14
|
+
readonly stats: DeliveryStats | undefined;
|
|
15
|
+
readonly authBlock: BackendAuthBlock | undefined;
|
|
16
|
+
readonly canSend: boolean;
|
|
17
|
+
readonly result?: DeliveryResult;
|
|
18
|
+
readonly outcome: DeliveryOutcome;
|
|
19
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { z } from 'zod';
|
|
2
2
|
import type { ProductEvent } from '../../events/product-event.js';
|
|
3
|
+
import type { CaptureConfig } from '../../config/types/config.types.js';
|
|
3
4
|
import type { queueEntrySchema } from '../schemas/queue.schema.js';
|
|
4
5
|
/** Per-event outcome counters the backend reports for an accepted batch. */
|
|
5
6
|
export interface DeliveryCounters {
|
|
@@ -14,8 +15,12 @@ export interface DeliveryResult {
|
|
|
14
15
|
/** Whether a failure is worth retrying later (network error, 5xx, 429...). */
|
|
15
16
|
readonly retryable: boolean;
|
|
16
17
|
readonly error?: string;
|
|
18
|
+
/** The local pass budget prevented or curtailed a request; no retry is charged. */
|
|
19
|
+
readonly deferred?: boolean;
|
|
17
20
|
/** Per-event outcomes from an accepted batch, when the backend sent them. */
|
|
18
21
|
readonly counters?: DeliveryCounters;
|
|
22
|
+
/** The gateway's reason for a 401, when it gave one the edge acts on (AWT-355). */
|
|
23
|
+
readonly refusal?: string;
|
|
19
24
|
}
|
|
20
25
|
export interface EventTransport {
|
|
21
26
|
send(events: readonly ProductEvent[]): Promise<DeliveryResult>;
|
|
@@ -29,11 +34,24 @@ export interface EventTransport {
|
|
|
29
34
|
readonly destination?: string;
|
|
30
35
|
}
|
|
31
36
|
export interface HttpTransportOptions {
|
|
37
|
+
readonly capture?: CaptureConfig;
|
|
32
38
|
readonly eventsUrl: string;
|
|
33
39
|
readonly token?: string;
|
|
40
|
+
/**
|
|
41
|
+
* The identity has a credential but no bearer to send right now (a refresh
|
|
42
|
+
* failed or the device was refused): defer every send, so records queue
|
|
43
|
+
* instead of going out unauthenticated and raising a block nothing lifts.
|
|
44
|
+
*/
|
|
45
|
+
readonly credentialPending?: boolean;
|
|
34
46
|
readonly installationId?: string;
|
|
35
47
|
readonly timeoutMs: number;
|
|
36
48
|
readonly fetchFn?: typeof fetch;
|
|
49
|
+
/** Monotonic deadline shared by direct delivery, drain and isolation probes. */
|
|
50
|
+
readonly deadline?: number;
|
|
51
|
+
/** Hook-owned pass duration, started on first send; omit for reusable transports. */
|
|
52
|
+
readonly budgetMs?: number;
|
|
53
|
+
/** Clock in the same units as deadline; performance.now in production. */
|
|
54
|
+
readonly nowMs?: () => number;
|
|
37
55
|
}
|
|
38
56
|
/** What one hook-path delivery attempt did. */
|
|
39
57
|
export interface DeliveryOutcome {
|
|
@@ -50,6 +68,16 @@ export interface QueueOptions {
|
|
|
50
68
|
readonly maxAttempts: number;
|
|
51
69
|
readonly maxEventAgeDays: number;
|
|
52
70
|
readonly now?: () => Date;
|
|
71
|
+
/**
|
|
72
|
+
* Sink for the entries the bound sacrifices on enqueue.
|
|
73
|
+
*
|
|
74
|
+
* On the options rather than on `enqueue`, because `enforceBound` runs under
|
|
75
|
+
* every enqueue — including the snapshot pipeline's, which has no stats of
|
|
76
|
+
* its own — and a deletion nobody counts is exactly the invisibility
|
|
77
|
+
* `DeliveryStatsSnapshot` exists to remove. Drain reports its own losses
|
|
78
|
+
* through the recorder passed to it.
|
|
79
|
+
*/
|
|
80
|
+
readonly stats?: DrainStatsRecorder;
|
|
53
81
|
}
|
|
54
82
|
export interface DrainStats {
|
|
55
83
|
readonly sent: number;
|
|
@@ -60,6 +88,23 @@ export interface DrainStats {
|
|
|
60
88
|
/** True when the drain lock was held elsewhere and this pass did nothing. */
|
|
61
89
|
readonly skipped: boolean;
|
|
62
90
|
}
|
|
91
|
+
/**
|
|
92
|
+
* A standing refusal of this identity's credential by one destination.
|
|
93
|
+
*
|
|
94
|
+
* `since` is when the refusals started, not when the last one happened: it is
|
|
95
|
+
* the number an administrator needs to tell "rotated an hour ago" from
|
|
96
|
+
* "revoked last week and nobody noticed".
|
|
97
|
+
*/
|
|
98
|
+
export interface AuthBlockState {
|
|
99
|
+
readonly destination: string;
|
|
100
|
+
/** The refusing HTTP status, 401 or 403. */
|
|
101
|
+
readonly status: number;
|
|
102
|
+
readonly since: string;
|
|
103
|
+
/** The gateway's stated reason, e.g. `token_expired` or `device_revoked`. */
|
|
104
|
+
readonly reason?: string;
|
|
105
|
+
/** The latest refusal; the re-probe wait counts from here. */
|
|
106
|
+
readonly lastRefusalAt?: string;
|
|
107
|
+
}
|
|
63
108
|
export interface DrainStatsRecorder {
|
|
64
109
|
recordRejected(count: number): Promise<void>;
|
|
65
110
|
recordDropped(count: number): Promise<void>;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { ReadTurnUsageRetry, TurnUsage } from './types/transcript.types.js';
|
|
1
|
+
import type { ReadTurnUsageRetry, SessionTitles, TurnUsage } from './types/transcript.types.js';
|
|
2
2
|
export type { ReadTurnUsageRetry, TranscriptUsageEntry, TurnUsage } from './types/transcript.types.js';
|
|
3
3
|
/**
|
|
4
4
|
* Authoritative per-turn token usage from Claude Code's transcript JSONL.
|
|
@@ -16,3 +16,21 @@ export type { ReadTurnUsageRetry, TranscriptUsageEntry, TurnUsage } from './type
|
|
|
16
16
|
* @returns The turn's usage, or undefined when the transcript reports none.
|
|
17
17
|
*/
|
|
18
18
|
export declare function readTurnUsage(transcriptPath: string, sinceIso: string, retry?: ReadTurnUsageRetry, untilIso?: string, excludeMessageIds?: ReadonlySet<string>): Promise<TurnUsage | undefined>;
|
|
19
|
+
/**
|
|
20
|
+
* The session's titles, from the tail of Claude Code's transcript.
|
|
21
|
+
*
|
|
22
|
+
* Titles change mid-session and Claude Code appends each new one, so the last
|
|
23
|
+
* entry of each kind is the current one. One tail read, no settle loop: a title
|
|
24
|
+
* is not part of the turn's flush, and a late one is simply the next turn's.
|
|
25
|
+
*
|
|
26
|
+
* @param transcriptPath - Path Claude Code reported in the hook payload.
|
|
27
|
+
* @returns The titles found; empty when the tail holds none or cannot be read.
|
|
28
|
+
*/
|
|
29
|
+
export declare function readClaudeSessionTitles(transcriptPath: string): Promise<SessionTitles>;
|
|
30
|
+
/**
|
|
31
|
+
* The latest title of each kind in a run of transcript lines.
|
|
32
|
+
*
|
|
33
|
+
* @param raw - Transcript text.
|
|
34
|
+
* @returns The latest custom title and the latest generated one.
|
|
35
|
+
*/
|
|
36
|
+
export declare function sessionTitlesFrom(raw: string): SessionTitles;
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import { asRecord } from '../core/object.js';
|
|
1
|
+
import { asRecord, compact } from '../core/object.js';
|
|
2
|
+
import { CLAUDE_CUSTOM_TITLE_FIELDS, CLAUDE_GENERATED_TITLE_FIELDS, RE_CLAUDE_TITLE_LINE } from './constants/turns.constants.js';
|
|
2
3
|
import { accumulateUsage, messageIdFor, parseJsonLine, readTranscriptTail, readUntilSettled } from './transcript.js';
|
|
3
4
|
/**
|
|
4
5
|
* Authoritative per-turn token usage from Claude Code's transcript JSONL.
|
|
@@ -78,3 +79,47 @@ function assistantUsageEntry(line, since, until, excludeMessageIds) {
|
|
|
78
79
|
const model = typeof message?.['model'] === 'string' ? message['model'] : undefined;
|
|
79
80
|
return { id, value: { model, usage } };
|
|
80
81
|
}
|
|
82
|
+
/**
|
|
83
|
+
* The session's titles, from the tail of Claude Code's transcript.
|
|
84
|
+
*
|
|
85
|
+
* Titles change mid-session and Claude Code appends each new one, so the last
|
|
86
|
+
* entry of each kind is the current one. One tail read, no settle loop: a title
|
|
87
|
+
* is not part of the turn's flush, and a late one is simply the next turn's.
|
|
88
|
+
*
|
|
89
|
+
* @param transcriptPath - Path Claude Code reported in the hook payload.
|
|
90
|
+
* @returns The titles found; empty when the tail holds none or cannot be read.
|
|
91
|
+
*/
|
|
92
|
+
export async function readClaudeSessionTitles(transcriptPath) {
|
|
93
|
+
const raw = await readTranscriptTail(transcriptPath);
|
|
94
|
+
if (raw === undefined)
|
|
95
|
+
return {};
|
|
96
|
+
return sessionTitlesFrom(raw);
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* The latest title of each kind in a run of transcript lines.
|
|
100
|
+
*
|
|
101
|
+
* @param raw - Transcript text.
|
|
102
|
+
* @returns The latest custom title and the latest generated one.
|
|
103
|
+
*/
|
|
104
|
+
export function sessionTitlesFrom(raw) {
|
|
105
|
+
let custom;
|
|
106
|
+
let generated;
|
|
107
|
+
for (const line of raw.split('\n')) {
|
|
108
|
+
if (!RE_CLAUDE_TITLE_LINE.test(line))
|
|
109
|
+
continue;
|
|
110
|
+
const entry = parseJsonLine(line);
|
|
111
|
+
const type = typeof entry?.['type'] === 'string' ? entry['type'] : '';
|
|
112
|
+
const customField = CLAUDE_CUSTOM_TITLE_FIELDS[type];
|
|
113
|
+
const generatedField = CLAUDE_GENERATED_TITLE_FIELDS[type];
|
|
114
|
+
if (customField)
|
|
115
|
+
custom = titleText(entry?.[customField]) ?? custom;
|
|
116
|
+
if (generatedField)
|
|
117
|
+
generated = titleText(entry?.[generatedField]) ?? generated;
|
|
118
|
+
}
|
|
119
|
+
return compact({ custom, generated });
|
|
120
|
+
}
|
|
121
|
+
function titleText(value) {
|
|
122
|
+
if (typeof value !== 'string')
|
|
123
|
+
return undefined;
|
|
124
|
+
return value.trim() || undefined;
|
|
125
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Whether a Codex transcript belongs to a headless `codex exec` run.
|
|
3
|
+
*
|
|
4
|
+
* The hook payload does not say how Codex was started; the transcript's first
|
|
5
|
+
* line (`session_meta`) does. Any failure answers false, so an unreadable
|
|
6
|
+
* transcript reports the default surface rather than failing the hook.
|
|
7
|
+
*
|
|
8
|
+
* @param transcriptPath - Path Codex reported in the hook payload.
|
|
9
|
+
* @returns True only when the originator is `codex_exec`.
|
|
10
|
+
*/
|
|
11
|
+
export declare function isCodexExecTranscript(transcriptPath: string): Promise<boolean>;
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import fs from 'node:fs/promises';
|
|
2
|
+
import { asRecord } from '../core/object.js';
|
|
3
|
+
import { CODEX_EXEC_ORIGINATOR, CODEX_META_HEAD_BYTES } from './constants/turns.constants.js';
|
|
4
|
+
/**
|
|
5
|
+
* Whether a Codex transcript belongs to a headless `codex exec` run.
|
|
6
|
+
*
|
|
7
|
+
* The hook payload does not say how Codex was started; the transcript's first
|
|
8
|
+
* line (`session_meta`) does. Any failure answers false, so an unreadable
|
|
9
|
+
* transcript reports the default surface rather than failing the hook.
|
|
10
|
+
*
|
|
11
|
+
* @param transcriptPath - Path Codex reported in the hook payload.
|
|
12
|
+
* @returns True only when the originator is `codex_exec`.
|
|
13
|
+
*/
|
|
14
|
+
export async function isCodexExecTranscript(transcriptPath) {
|
|
15
|
+
try {
|
|
16
|
+
const handle = await fs.open(transcriptPath, 'r');
|
|
17
|
+
try {
|
|
18
|
+
const buffer = Buffer.alloc(CODEX_META_HEAD_BYTES);
|
|
19
|
+
const { bytesRead } = await handle.read(buffer, 0, CODEX_META_HEAD_BYTES, 0);
|
|
20
|
+
const head = buffer.toString('utf8', 0, bytesRead);
|
|
21
|
+
const meta = asRecord(JSON.parse(head.split('\n', 1)[0] ?? ''));
|
|
22
|
+
return asRecord(meta?.['payload'])?.['originator'] === CODEX_EXEC_ORIGINATOR;
|
|
23
|
+
}
|
|
24
|
+
finally {
|
|
25
|
+
await handle.close();
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
catch {
|
|
29
|
+
return false;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import type { ReadTurnUsageRetry, TurnUsage } from './types/transcript.types.js';
|
|
2
|
+
/**
|
|
3
|
+
* Per-turn token usage from a Codex rollout (`~/.codex/sessions/…/rollout-*.jsonl`).
|
|
4
|
+
*
|
|
5
|
+
* Codex writes one `event_msg` / `token_count` row per model request, whose
|
|
6
|
+
* `info.last_token_usage` is that request's counts (verified on codex-cli
|
|
7
|
+
* 0.150.0: across 294 rows, every `last_token_usage` equals the step in
|
|
8
|
+
* `total_token_usage`). Only those numbers and the model named by the latest
|
|
9
|
+
* `turn_context` row are read; message, reasoning and tool rows are skipped
|
|
10
|
+
* unparsed, so nothing a developer wrote leaves this function.
|
|
11
|
+
*
|
|
12
|
+
* This is how Codex usage reaches the platform on a metadata-only install,
|
|
13
|
+
* where its native OTel stays off because those logs carry tool content.
|
|
14
|
+
*
|
|
15
|
+
* ponytail: reads the same 4 MiB tail as the Claude reader. A single turn whose
|
|
16
|
+
* tool output pushes its own early rows past that undercounts, and loses the
|
|
17
|
+
* rollout's model name (the Stop payload's `model` then names it). Upgrade
|
|
18
|
+
* path: remember the byte offset each Stop read to and read from there.
|
|
19
|
+
*
|
|
20
|
+
* Counts keep OpenAI's meaning: `inputTokens` includes the cached part, which
|
|
21
|
+
* `cachedInputTokens` repeats. The backend prices Codex that way.
|
|
22
|
+
*
|
|
23
|
+
* @param transcriptPath - Path Codex reported in the hook payload.
|
|
24
|
+
* @param sinceIso - When the turn started; earlier rows are another turn's.
|
|
25
|
+
* @param retry - How long to wait for the rollout flush to settle.
|
|
26
|
+
* @param untilIso - Upper bound, keeping a racing next prompt's rows out.
|
|
27
|
+
* @param excludeMessageIds - Rows another turn already claimed.
|
|
28
|
+
* @returns The turn's usage, or undefined when the rollout reports none.
|
|
29
|
+
*/
|
|
30
|
+
export declare function readCodexTurnUsage(transcriptPath: string, sinceIso: string, retry?: ReadTurnUsageRetry, untilIso?: string, excludeMessageIds?: ReadonlySet<string>): Promise<TurnUsage | undefined>;
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import { asRecord, firstStringOf } from '../core/object.js';
|
|
2
|
+
import { accumulateUsage, messageIdFor, parseJsonLine, readTranscriptTail, readUntilSettled } from './transcript.js';
|
|
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
|
+
/**
|
|
5
|
+
* Per-turn token usage from a Codex rollout (`~/.codex/sessions/…/rollout-*.jsonl`).
|
|
6
|
+
*
|
|
7
|
+
* Codex writes one `event_msg` / `token_count` row per model request, whose
|
|
8
|
+
* `info.last_token_usage` is that request's counts (verified on codex-cli
|
|
9
|
+
* 0.150.0: across 294 rows, every `last_token_usage` equals the step in
|
|
10
|
+
* `total_token_usage`). Only those numbers and the model named by the latest
|
|
11
|
+
* `turn_context` row are read; message, reasoning and tool rows are skipped
|
|
12
|
+
* unparsed, so nothing a developer wrote leaves this function.
|
|
13
|
+
*
|
|
14
|
+
* This is how Codex usage reaches the platform on a metadata-only install,
|
|
15
|
+
* where its native OTel stays off because those logs carry tool content.
|
|
16
|
+
*
|
|
17
|
+
* ponytail: reads the same 4 MiB tail as the Claude reader. A single turn whose
|
|
18
|
+
* tool output pushes its own early rows past that undercounts, and loses the
|
|
19
|
+
* rollout's model name (the Stop payload's `model` then names it). Upgrade
|
|
20
|
+
* path: remember the byte offset each Stop read to and read from there.
|
|
21
|
+
*
|
|
22
|
+
* Counts keep OpenAI's meaning: `inputTokens` includes the cached part, which
|
|
23
|
+
* `cachedInputTokens` repeats. The backend prices Codex that way.
|
|
24
|
+
*
|
|
25
|
+
* @param transcriptPath - Path Codex reported in the hook payload.
|
|
26
|
+
* @param sinceIso - When the turn started; earlier rows are another turn's.
|
|
27
|
+
* @param retry - How long to wait for the rollout flush to settle.
|
|
28
|
+
* @param untilIso - Upper bound, keeping a racing next prompt's rows out.
|
|
29
|
+
* @param excludeMessageIds - Rows another turn already claimed.
|
|
30
|
+
* @returns The turn's usage, or undefined when the rollout reports none.
|
|
31
|
+
*/
|
|
32
|
+
export async function readCodexTurnUsage(transcriptPath, sinceIso, retry, untilIso, excludeMessageIds) {
|
|
33
|
+
return readUntilSettled(() => readOnce(transcriptPath, sinceIso, untilIso, excludeMessageIds), retry);
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* One pass over the rollout tail.
|
|
37
|
+
*
|
|
38
|
+
* @param transcriptPath - Rollout file.
|
|
39
|
+
* @param sinceIso - Lower bound of the turn's window.
|
|
40
|
+
* @param untilIso - Upper bound, or undefined for "no upper bound".
|
|
41
|
+
* @param excludeMessageIds - Rows another turn already claimed.
|
|
42
|
+
* @returns The usage found in this pass.
|
|
43
|
+
*/
|
|
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)
|
|
51
|
+
return undefined;
|
|
52
|
+
const byRowId = new Map();
|
|
53
|
+
let model;
|
|
54
|
+
for (const line of raw.split('\n')) {
|
|
55
|
+
// Cheap prefilter: content rows are most of the file and never matter here.
|
|
56
|
+
if (!line.includes(CODEX_TOKEN_COUNT) && !line.includes(CODEX_TURN_CONTEXT))
|
|
57
|
+
continue;
|
|
58
|
+
const row = parseJsonLine(line);
|
|
59
|
+
const payload = asRecord(row?.['payload']);
|
|
60
|
+
if (!row || !payload)
|
|
61
|
+
continue;
|
|
62
|
+
if (row['type'] === CODEX_TURN_CONTEXT) {
|
|
63
|
+
model = firstStringOf(payload['model']) ?? model;
|
|
64
|
+
continue;
|
|
65
|
+
}
|
|
66
|
+
if (payload['type'] !== CODEX_TOKEN_COUNT)
|
|
67
|
+
continue;
|
|
68
|
+
const last = asRecord(asRecord(payload['info'])?.[CODEX_LAST_TOKEN_USAGE]);
|
|
69
|
+
const timestamp = typeof row['timestamp'] === 'string' ? Date.parse(row['timestamp']) : NaN;
|
|
70
|
+
if (!last || Number.isNaN(timestamp) || timestamp < since || timestamp > until)
|
|
71
|
+
continue;
|
|
72
|
+
// No row id in the format; the line hash carries the row's millisecond
|
|
73
|
+
// timestamp (and `ordinal`, which 0.150 writes), so it is unique per request
|
|
74
|
+
// and stable across re-reads.
|
|
75
|
+
const id = messageIdFor(undefined, line);
|
|
76
|
+
if (excludeMessageIds?.has(id))
|
|
77
|
+
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
|
+
});
|
|
86
|
+
}
|
|
87
|
+
return accumulateUsage(byRowId);
|
|
88
|
+
}
|
|
@@ -1,8 +1,25 @@
|
|
|
1
1
|
import type { ReadTurnUsageRetry } from '../types/transcript.types.js';
|
|
2
2
|
/** Canonical event types that mean "a tool finished doing something". */
|
|
3
3
|
export declare const TOOL_COMPLETION_TYPES: ReadonlySet<string>;
|
|
4
|
+
/** Canonical event types that mean "a tool is about to run": where a turn names its checkouts. */
|
|
5
|
+
export declare const TOOL_START_TYPES: ReadonlySet<string>;
|
|
6
|
+
/**
|
|
7
|
+
* Completions whose shell command is read again for a checkout it created. A
|
|
8
|
+
* failed call is `tool.failed` whatever the tool; the provider's `shellCall`
|
|
9
|
+
* answers only for a shell.
|
|
10
|
+
*/
|
|
11
|
+
export declare const SHELL_REREAD_TYPES: ReadonlySet<string>;
|
|
4
12
|
/** Orphaned turn state (a crash without Stop/SessionEnd) is deleted after this. */
|
|
5
13
|
export declare const TURN_STATE_TTL_MS: number;
|
|
14
|
+
/**
|
|
15
|
+
* How often the expiry sweep may actually walk the state root.
|
|
16
|
+
*
|
|
17
|
+
* It used to run after every closing turn and every session end — a `readdir`
|
|
18
|
+
* plus a `stat` per file of every recent session, on the hook path, almost
|
|
19
|
+
* always to find that nothing was 24 h old yet. Hourly keeps removal well
|
|
20
|
+
* inside the TTL's own tolerance and costs one `stat` on the common path.
|
|
21
|
+
*/
|
|
22
|
+
export declare const TURN_STATE_SWEEP_INTERVAL_MS: number;
|
|
6
23
|
/** Overlapping Stop hooks serialize only transcript usage allocation. */
|
|
7
24
|
export declare const USAGE_LOCK_WAIT_MS = 5000;
|
|
8
25
|
export declare const USAGE_LOCK_POLL_MS = 25;
|
|
@@ -22,16 +39,85 @@ export declare const TRANSCRIPT_TAIL_BYTES: number;
|
|
|
22
39
|
export declare const TRANSCRIPT_PATH_KEY = "transcript_path";
|
|
23
40
|
/** Prefix distinguishing usage-claim files from turn records in a session dir. */
|
|
24
41
|
export declare const USAGE_CLAIM_PREFIX = "usage-claim--";
|
|
42
|
+
/**
|
|
43
|
+
* The one file in a session dir that is not a turn record: what model the
|
|
44
|
+
* session is on, so a later hook's gate can state it.
|
|
45
|
+
*/
|
|
46
|
+
export declare const SESSION_MODEL_FILE = "session.json";
|
|
47
|
+
/** The folder the session started in, written once by its first hook: `{ cwd }`. */
|
|
48
|
+
export declare const SESSION_START_FILE = "session-start.json";
|
|
49
|
+
/**
|
|
50
|
+
* The checkout the session's last changing turn worked in: `{ root, repository, at }`.
|
|
51
|
+
* Carried by a turn that changes nothing, and read by the prompt gate (AWT-128).
|
|
52
|
+
*/
|
|
53
|
+
export declare const WORK_CHECKOUT_FILE = "work-checkout.json";
|
|
54
|
+
/**
|
|
55
|
+
* The agent process this session runs in: `{ pid, started }`. Written by the
|
|
56
|
+
* first hook, so a session started later can recognise it among its ancestors.
|
|
57
|
+
*/
|
|
58
|
+
export declare const AGENT_PROCESS_FILE = "agent-process.json";
|
|
59
|
+
/**
|
|
60
|
+
* What the session inherited from the process that started it (AWT-228):
|
|
61
|
+
* `{ parent?, root? }`. Written once, by the first hook of a session whose
|
|
62
|
+
* folder is no checkout, so that no later hook walks the process tree again.
|
|
63
|
+
*/
|
|
64
|
+
export declare const INHERITED_CHECKOUT_FILE = "inherited-checkout.json";
|
|
65
|
+
/**
|
|
66
|
+
* The ticket keys the session's opening prompt named: `{ keys }` (AWT-326).
|
|
67
|
+
* Written once, by the first prompt, even when it names none, so that a later
|
|
68
|
+
* prompt can never move the session. Local only; the keys ride the summary.
|
|
69
|
+
*/
|
|
70
|
+
export declare const PROMPT_TICKETS_FILE = "prompt-tickets.json";
|
|
71
|
+
/**
|
|
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.
|
|
74
|
+
*/
|
|
75
|
+
export declare const LIVENESS_IGNORED_FILES: ReadonlySet<string>;
|
|
76
|
+
/** Directory of the state root indexing live agent pids to their session: `<pid>.json`. Not a session. */
|
|
77
|
+
export declare const AGENT_PIDS_DIR = "agent-pids";
|
|
78
|
+
/** Prefix of the once-per-session notice marker a checkout takes: `untraced-<root hash>.json`. Not a turn record. */
|
|
79
|
+
export declare const UNTRACED_MARKER_PREFIX = "untraced-";
|
|
80
|
+
/**
|
|
81
|
+
* The session's last known titles: `{ custom?, generated? }`, each kept on its
|
|
82
|
+
* own. A turn whose transcript tail no longer reaches a title entry still sends
|
|
83
|
+
* the ones seen before.
|
|
84
|
+
*/
|
|
85
|
+
export declare const SESSION_TITLE_FILE = "session-title.json";
|
|
86
|
+
/**
|
|
87
|
+
* Marker that the session's billing fact (`session.billing`) was handed to
|
|
88
|
+
* delivery, written once (AWT-476). Created only after a mode was classified, so
|
|
89
|
+
* a session that could not tell yet asks again on its next prompt.
|
|
90
|
+
*/
|
|
91
|
+
export declare const BILLING_SENT_FILE = "billing-sent.json";
|
|
92
|
+
/** Session-wide files beside the records; none of them is a turn record. */
|
|
93
|
+
export declare const SESSION_FILES: ReadonlySet<string>;
|
|
94
|
+
/** Prefix of a checkout record's filename: `checkout--<turn>-<root>-<call>.json`. */
|
|
95
|
+
export declare const CHECKOUT_RECORD_PREFIX = "checkout--";
|
|
96
|
+
/** Hash length of each part of a checkout record's filename. */
|
|
97
|
+
export declare const CHECKOUT_KEY_HASH_LENGTH = 16;
|
|
98
|
+
/** Checkouts one turn may name; past it, new ones are ignored (seven days of real turns peak at five). */
|
|
99
|
+
export declare const MAX_TURN_CHECKOUTS = 8;
|
|
25
100
|
/** Public provider labels; the internal id is an implementation detail. */
|
|
26
101
|
export declare const PROVIDER_LABELS: Readonly<Record<string, string>>;
|
|
27
102
|
/** Providers whose sessions run inside an editor rather than a terminal. */
|
|
28
103
|
export declare const IDE_SURFACE_PROVIDERS: ReadonlySet<string>;
|
|
104
|
+
/**
|
|
105
|
+
* The session id of the agent session that launched this one, set by the
|
|
106
|
+
* launcher (a spinoff in a new terminal tab, a review bot). Hooks inherit the
|
|
107
|
+
* agent's environment, so every hook of the child can read it (AWT-319).
|
|
108
|
+
*/
|
|
109
|
+
export declare const PARENT_SESSION_VAR = "AGENTWATCH_PARENT_SESSION";
|
|
110
|
+
/** What a declared parent session id may look like: a provider session id, never free text. */
|
|
111
|
+
export declare const RE_PARENT_SESSION_ID: RegExp;
|
|
29
112
|
/** Claude Code reports its own surface here. */
|
|
30
113
|
export declare const CLAUDE_ENTRYPOINT_VAR = "CLAUDE_CODE_ENTRYPOINT";
|
|
31
114
|
export declare const DEFAULT_SURFACE = "cli";
|
|
115
|
+
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;
|
|
32
120
|
export declare const IDE_SURFACE = "ide";
|
|
33
|
-
/** Separator between several prompts collapsed into one turn. */
|
|
34
|
-
export declare const PROMPT_JOIN_SEPARATOR = "\n---\n";
|
|
35
121
|
/**
|
|
36
122
|
* Cap on `files_touched` and `files_read`, matching the platform's own limit on
|
|
37
123
|
* either list.
|
|
@@ -44,14 +130,43 @@ export declare const PROMPT_JOIN_SEPARATOR = "\n---\n";
|
|
|
44
130
|
* truncated instead; that is a truncated list, not a lost turn.
|
|
45
131
|
*/
|
|
46
132
|
export declare const MAX_TURN_FILES = 500;
|
|
133
|
+
/**
|
|
134
|
+
* Bounds on the content a summary carries under the content flags. Applied after
|
|
135
|
+
* the text was scrubbed whole, never before: a cut through a credential leaves
|
|
136
|
+
* a prefix no pattern matches.
|
|
137
|
+
*
|
|
138
|
+
* ponytail: fixed character cuts, sized so a request plus a pasted link or a
|
|
139
|
+
* sheet id fits. A turn of 200 shell calls sends its first 50; per-tenant
|
|
140
|
+
* bounds would come from the backend.
|
|
141
|
+
*/
|
|
142
|
+
export declare const MAX_PROMPT_TEXT_LENGTH = 4000;
|
|
143
|
+
export declare const MAX_TOOL_INPUT_LENGTH = 2000;
|
|
144
|
+
export declare const MAX_TURN_TOOL_INPUTS = 50;
|
|
145
|
+
/**
|
|
146
|
+
* The longest session title sent, in UTF-16 units — what core cuts at — and
|
|
147
|
+
* never through a code point. Scrubbed whole first, like prompt text.
|
|
148
|
+
*/
|
|
149
|
+
export declare const MAX_SESSION_TITLE_LENGTH = 200;
|
|
150
|
+
/**
|
|
151
|
+
* Claude Code's title entries, by type, and the field holding the text: the
|
|
152
|
+
* session name (`--name`, `/rename`, a hook) is sent as `session_name`, the one
|
|
153
|
+
* Claude Code generates as `session_title`. Not
|
|
154
|
+
* `summary`: that type also holds compaction summaries and, in older releases,
|
|
155
|
+
* another conversation's title by `leafUuid`, so its text is not this session's name.
|
|
156
|
+
*/
|
|
157
|
+
export declare const CLAUDE_CUSTOM_TITLE_FIELDS: Readonly<Record<string, string>>;
|
|
158
|
+
export declare const CLAUDE_GENERATED_TITLE_FIELDS: Readonly<Record<string, string>>;
|
|
159
|
+
/** A transcript line that may be a title entry; the rest of the tail is never parsed for one. */
|
|
160
|
+
export declare const RE_CLAUDE_TITLE_LINE: RegExp;
|
|
47
161
|
/** Fallback tool name for a call the provider did not name. */
|
|
48
162
|
export declare const UNKNOWN_TOOL_NAME = "unknown";
|
|
49
|
-
/** Metadata keys the tracker reads off canonical events. */
|
|
50
|
-
export declare const PROMPT_TEXT_KEY = "promptText";
|
|
163
|
+
/** Metadata keys the tracker reads off canonical events. Evidence only: no text key exists. */
|
|
51
164
|
export declare const PROMPT_EVIDENCE_KEY = "prompt";
|
|
52
|
-
export declare const RESPONSE_TEXT_KEY = "responseText";
|
|
53
165
|
export declare const RESPONSE_EVIDENCE_KEY = "response";
|
|
54
166
|
export declare const FILE_PATH_KEY = "filePath";
|
|
167
|
+
export declare const EXTERNAL_PATH_KEY = "externalPath";
|
|
168
|
+
export declare const PROMPT_TEXT_KEY = "promptText";
|
|
169
|
+
export declare const PROMPT_JIRA_IDS_KEY = "promptJiraIds";
|
|
55
170
|
/** Length of the hashed session directory name. */
|
|
56
171
|
export declare const SESSION_DIR_HASH_LENGTH = 32;
|
|
57
172
|
/** Length of the hashed lock names, which must be filesystem-safe. */
|
|
@@ -63,7 +178,28 @@ export declare const TRANSCRIPT_INPUT_TOKENS = "input_tokens";
|
|
|
63
178
|
export declare const TRANSCRIPT_OUTPUT_TOKENS = "output_tokens";
|
|
64
179
|
export declare const TRANSCRIPT_CACHE_READ_TOKENS = "cache_read_input_tokens";
|
|
65
180
|
export declare const TRANSCRIPT_CACHE_CREATION_TOKENS = "cache_creation_input_tokens";
|
|
181
|
+
/** Codex rollout rows the usage reader needs; every other row is skipped unparsed. */
|
|
182
|
+
export declare const CODEX_TOKEN_COUNT = "token_count";
|
|
183
|
+
export declare const CODEX_TURN_CONTEXT = "turn_context";
|
|
184
|
+
export declare const CODEX_LAST_TOKEN_USAGE = "last_token_usage";
|
|
185
|
+
export declare const CODEX_CACHED_INPUT_TOKENS = "cached_input_tokens";
|
|
186
|
+
/** Gemini CLI chat-file row type that carries a model reply, and its usage block. */
|
|
187
|
+
export declare const GEMINI_REPLY_TYPE = "gemini";
|
|
188
|
+
export declare const GEMINI_TOKENS = "tokens";
|
|
66
189
|
/** Every transcript token field, for weighting which model dominated a turn. */
|
|
67
190
|
export declare const TRANSCRIPT_TOKEN_FIELDS: readonly ["input_tokens", "output_tokens", "cache_read_input_tokens", "cache_creation_input_tokens"];
|
|
68
191
|
/** Prefix for the content-hash id given to a transcript entry without one. */
|
|
69
192
|
export declare const ANONYMOUS_MESSAGE_ID_PREFIX = "anon-";
|
|
193
|
+
/**
|
|
194
|
+
* Counts the commits between two oids that no remote-tracking ref reaches: the
|
|
195
|
+
* turn's own, as opposed to a pull, a reset or another branch's tip (SPEC §3a).
|
|
196
|
+
*
|
|
197
|
+
* @param from - The oid the turn started on.
|
|
198
|
+
* @param to - The oid or ref it ended on.
|
|
199
|
+
* @param since - Only commits made since then (ISO time): after a switch, the
|
|
200
|
+
* new branch's older commits are not the turn's.
|
|
201
|
+
* @returns The argument vector.
|
|
202
|
+
*/
|
|
203
|
+
export declare function gitOwnCommitCountArgs(from: string | undefined, to: string, since?: string): readonly string[];
|
|
204
|
+
/** Commit times are whole seconds; a commit in the second the turn first named its checkout is still the turn's. */
|
|
205
|
+
export declare const OWN_COMMIT_SINCE_SLACK_MS = 2000;
|