@agent-watch-ai/edge 0.6.0 → 0.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +219 -9
- package/README.md +303 -4
- package/dist/billing/billing-mode.d.ts +37 -5
- package/dist/billing/billing-mode.js +28 -19
- package/dist/billing/constants/billing.constants.d.ts +0 -2
- package/dist/billing/constants/billing.constants.js +0 -2
- package/dist/billing/index.d.ts +2 -1
- package/dist/billing/index.js +1 -1
- package/dist/billing/session-billing.d.ts +3 -2
- package/dist/billing/session-billing.js +8 -8
- package/dist/billing/types/billing.types.d.ts +7 -0
- package/dist/capture/capture-env.d.ts +147 -0
- package/dist/capture/capture-env.js +301 -0
- package/dist/capture/captured-step.d.ts +88 -0
- package/dist/capture/captured-step.js +479 -0
- package/dist/capture/constants/capture.constants.d.ts +70 -0
- package/dist/capture/constants/capture.constants.js +72 -0
- package/dist/capture/launched-runs.d.ts +16 -0
- package/dist/capture/launched-runs.js +151 -0
- package/dist/capture/node.cjs +697 -0
- package/dist/capture/node.d.cts +32 -0
- package/dist/capture/python/sitecustomize.py +823 -0
- package/dist/capture/schemas/capture.schema.d.ts +35 -0
- package/dist/capture/schemas/capture.schema.js +31 -0
- package/dist/capture/types/capture.types.d.ts +20 -0
- package/dist/cli/constants/cli.constants.d.ts +2 -6
- package/dist/cli/constants/cli.constants.js +3 -7
- package/dist/cli/context.d.ts +19 -22
- package/dist/cli/context.js +23 -54
- package/dist/cli/doctor-run-capture.d.ts +13 -0
- package/dist/cli/doctor-run-capture.js +19 -0
- package/dist/cli/doctor.js +16 -67
- package/dist/cli/hook.d.ts +14 -0
- package/dist/cli/hook.js +40 -0
- package/dist/cli/index.d.ts +1 -1
- package/dist/cli/index.js +1 -1
- package/dist/cli/misc.js +10 -2
- package/dist/cli/replay.d.ts +33 -0
- package/dist/cli/replay.js +193 -0
- package/dist/cli/setup.d.ts +10 -2
- package/dist/cli/setup.js +154 -26
- package/dist/cli/status.js +45 -5
- package/dist/cli/toggle.js +10 -0
- package/dist/cli/types/cli.types.d.ts +15 -1
- package/dist/cli/uninstall.js +8 -22
- package/dist/cli.js +104 -17
- package/dist/config/config.d.ts +0 -2
- package/dist/config/config.js +0 -1
- package/dist/config/constants/config.constants.js +2 -0
- package/dist/config/events-transport.d.ts +15 -0
- package/dist/config/events-transport.js +29 -0
- package/dist/config/index.d.ts +2 -1
- package/dist/config/index.js +1 -0
- package/dist/config/root-config.d.ts +12 -0
- package/dist/config/root-config.js +24 -0
- package/dist/config/schemas/config.schema.d.ts +13 -0
- package/dist/config/schemas/config.schema.js +42 -6
- package/dist/config/types/config.types.d.ts +7 -0
- package/dist/core/index.d.ts +1 -1
- package/dist/core/index.js +1 -1
- package/dist/core/version.d.ts +9 -0
- package/dist/core/version.js +14 -0
- package/dist/core/which.d.ts +11 -0
- package/dist/core/which.js +18 -0
- package/dist/credentials/cloud-claim.d.ts +32 -0
- package/dist/credentials/cloud-claim.js +42 -0
- package/dist/credentials/constants/credentials.constants.d.ts +8 -1
- package/dist/credentials/constants/credentials.constants.js +9 -2
- package/dist/credentials/device-api.d.ts +15 -1
- package/dist/credentials/device-api.js +12 -1
- package/dist/credentials/sign-in.d.ts +27 -0
- package/dist/credentials/sign-in.js +31 -1
- package/dist/credentials/token-refresher.d.ts +5 -1
- package/dist/credentials/token-refresher.js +10 -2
- package/dist/events/constants/events.constants.d.ts +5 -2
- package/dist/events/constants/events.constants.js +4 -1
- package/dist/events/index.d.ts +1 -0
- package/dist/events/product-event.d.ts +6 -3
- package/dist/events/types/events.types.d.ts +6 -0
- package/dist/events/types/replay-progress.types.d.ts +26 -0
- package/dist/events/types/replay-progress.types.js +1 -0
- package/dist/events/types/repo-tree.types.d.ts +2 -1
- package/dist/feature/ticket-candidates.d.ts +1 -1
- package/dist/git/checkout-root.d.ts +15 -0
- package/dist/git/checkout-root.js +18 -0
- package/dist/git/code-state.d.ts +2 -16
- package/dist/git/code-state.js +11 -12
- package/dist/git/constants/git.constants.d.ts +31 -11
- package/dist/git/constants/git.constants.js +36 -12
- package/dist/git/fingerprint.d.ts +3 -6
- package/dist/git/fingerprint.js +5 -8
- package/dist/git/git-context.js +5 -1
- package/dist/git/index.d.ts +1 -2
- package/dist/git/index.js +1 -1
- package/dist/git/repo-snapshot.d.ts +13 -4
- package/dist/git/repo-snapshot.js +47 -9
- package/dist/git/types/snapshot.types.d.ts +2 -0
- package/dist/pipeline/constants/pipeline.constants.d.ts +3 -0
- package/dist/pipeline/constants/pipeline.constants.js +3 -0
- package/dist/pipeline/hook-pipeline.js +81 -57
- package/dist/privacy/product-capture.js +2 -0
- package/dist/providers/antigravity/antigravity.capture.d.ts +2 -0
- package/dist/providers/antigravity/antigravity.capture.js +5 -0
- package/dist/providers/antigravity/antigravity.detect.js +8 -22
- package/dist/providers/antigravity/antigravity.provider.js +10 -3
- package/dist/providers/antigravity/constants/antigravity.constants.d.ts +1 -0
- package/dist/providers/antigravity/constants/antigravity.constants.js +1 -0
- package/dist/providers/claude/claude.adapter.js +9 -6
- package/dist/providers/claude/claude.detect.js +2 -17
- package/dist/providers/claude/claude.otel.d.ts +21 -3
- package/dist/providers/claude/claude.otel.js +216 -61
- package/dist/providers/claude/claude.provider.js +17 -1
- package/dist/providers/claude/claude.version.d.ts +9 -0
- package/dist/providers/claude/claude.version.js +28 -0
- package/dist/providers/claude/constants/claude.constants.d.ts +27 -0
- package/dist/providers/claude/constants/claude.constants.js +27 -0
- package/dist/providers/claude/constants/claude.otel.constants.d.ts +27 -0
- package/dist/providers/claude/constants/claude.otel.constants.js +45 -5
- package/dist/providers/claude/types/claude.types.d.ts +13 -0
- package/dist/providers/codex/codex.capture.d.ts +76 -0
- package/dist/providers/codex/codex.capture.js +454 -0
- package/dist/providers/codex/codex.detect.js +2 -4
- package/dist/providers/codex/codex.otel.d.ts +38 -2
- package/dist/providers/codex/codex.otel.js +50 -6
- package/dist/providers/codex/codex.provider.js +8 -0
- package/dist/providers/codex/codex.surface.d.ts +9 -0
- package/dist/{turns/codex-originator.js → providers/codex/codex.surface.js} +17 -3
- package/dist/providers/codex/codex.titles.d.ts +14 -0
- package/dist/providers/codex/codex.titles.js +35 -0
- package/dist/providers/codex/constants/codex.constants.d.ts +4 -0
- package/dist/providers/codex/constants/codex.constants.js +4 -0
- package/dist/providers/cursor/constants/cursor.constants.d.ts +17 -4
- package/dist/providers/cursor/constants/cursor.constants.js +25 -5
- package/dist/providers/cursor/cursor-mcp-memo.d.ts +34 -0
- package/dist/providers/cursor/cursor-mcp-memo.js +94 -0
- package/dist/providers/cursor/cursor.adapter.js +20 -82
- package/dist/providers/cursor/cursor.capture.d.ts +1 -0
- package/dist/providers/cursor/cursor.capture.js +4 -0
- package/dist/providers/cursor/cursor.detect.d.ts +5 -1
- package/dist/providers/cursor/cursor.detect.js +11 -26
- package/dist/providers/cursor/cursor.hooks.js +2 -2
- package/dist/providers/cursor/cursor.provider.js +16 -7
- package/dist/providers/cursor/cursor.titles.d.ts +14 -0
- package/dist/providers/cursor/cursor.titles.js +20 -0
- package/dist/providers/cursor/schemas/cursor.schema.d.ts +3 -3
- package/dist/providers/cursor/schemas/cursor.schema.js +5 -3
- package/dist/providers/gemini/constants/gemini.constants.d.ts +16 -0
- package/dist/providers/gemini/constants/gemini.constants.js +17 -0
- package/dist/providers/gemini/gemini.capture.d.ts +81 -0
- package/dist/providers/gemini/gemini.capture.js +210 -0
- package/dist/providers/gemini/gemini.detect.js +57 -24
- package/dist/providers/gemini/gemini.otel.d.ts +38 -2
- package/dist/providers/gemini/gemini.otel.js +56 -125
- package/dist/providers/gemini/gemini.provider.js +5 -0
- package/dist/providers/index.d.ts +1 -0
- package/dist/providers/index.js +1 -0
- package/dist/providers/provider.d.ts +8 -0
- package/dist/providers/provider.js +9 -0
- package/dist/providers/registry.d.ts +14 -0
- package/dist/providers/registry.js +14 -0
- package/dist/providers/shared/constants/hook-config.constants.d.ts +2 -0
- package/dist/providers/shared/constants/hook-config.constants.js +2 -0
- package/dist/providers/shared/constants/terminal-capture.constants.d.ts +6 -0
- package/dist/providers/shared/constants/terminal-capture.constants.js +6 -0
- package/dist/providers/shared/editor-paths.d.ts +8 -0
- package/dist/providers/shared/editor-paths.js +18 -0
- package/dist/providers/shared/hook-config.d.ts +5 -0
- package/dist/providers/shared/hook-config.js +39 -1
- package/dist/providers/shared/hooks-marker.d.ts +14 -0
- package/dist/providers/shared/hooks-marker.js +23 -0
- package/dist/providers/shared/install-record.d.ts +29 -0
- package/dist/providers/shared/install-record.js +52 -0
- package/dist/providers/shared/session-title-store.d.ts +23 -0
- package/dist/providers/shared/session-title-store.js +50 -0
- package/dist/providers/shared/terminal-capture.d.ts +41 -0
- package/dist/providers/shared/terminal-capture.js +109 -0
- package/dist/providers/shared/tooling.d.ts +21 -0
- package/dist/providers/shared/tooling.js +36 -1
- package/dist/providers/types/provider.types.d.ts +57 -4
- package/dist/replay/checkout-resolver.d.ts +29 -0
- package/dist/replay/checkout-resolver.js +69 -0
- package/dist/replay/codex-turns.d.ts +24 -0
- package/dist/replay/codex-turns.js +221 -0
- package/dist/replay/constants/replay.constants.d.ts +99 -0
- package/dist/replay/constants/replay.constants.js +102 -0
- package/dist/replay/describe.d.ts +10 -0
- package/dist/replay/describe.js +31 -0
- package/dist/replay/gemini-turns.d.ts +36 -0
- package/dist/replay/gemini-turns.js +192 -0
- package/dist/replay/machine.d.ts +34 -0
- package/dist/replay/machine.js +68 -0
- package/dist/replay/progress-event.d.ts +21 -0
- package/dist/replay/progress-event.js +45 -0
- package/dist/replay/replay-runner.d.ts +58 -0
- package/dist/replay/replay-runner.js +529 -0
- package/dist/replay/replay-scan.d.ts +83 -0
- package/dist/replay/replay-scan.js +170 -0
- package/dist/replay/replay-state.d.ts +108 -0
- package/dist/replay/replay-state.js +199 -0
- package/dist/replay/resume.d.ts +18 -0
- package/dist/replay/resume.js +34 -0
- package/dist/replay/start.d.ts +23 -0
- package/dist/replay/start.js +39 -0
- package/dist/replay/throttle.d.ts +35 -0
- package/dist/replay/throttle.js +41 -0
- package/dist/replay/transcript-turns.d.ts +35 -0
- package/dist/replay/transcript-turns.js +215 -0
- package/dist/replay/turn-event.d.ts +38 -0
- package/dist/replay/turn-event.js +116 -0
- package/dist/replay/types/replay.types.d.ts +162 -0
- package/dist/replay/types/replay.types.js +1 -0
- package/dist/replay/window.d.ts +28 -0
- package/dist/replay/window.js +41 -0
- package/dist/snapshot/branch-selection.d.ts +40 -23
- package/dist/snapshot/branch-selection.js +71 -41
- package/dist/snapshot/constants/snapshot-stages.constants.d.ts +3 -1
- package/dist/snapshot/constants/snapshot-stages.constants.js +3 -1
- package/dist/snapshot/constants/snapshot.constants.d.ts +34 -7
- package/dist/snapshot/constants/snapshot.constants.js +34 -7
- package/dist/snapshot/index.d.ts +2 -2
- package/dist/snapshot/index.js +1 -1
- package/dist/snapshot/snapshot-pipeline.js +89 -26
- package/dist/snapshot/snapshot-state.js +6 -2
- package/dist/snapshot/tree-pipeline.d.ts +1 -1
- package/dist/snapshot/tree-pipeline.js +34 -15
- package/dist/snapshot/types/snapshot.types.d.ts +20 -3
- package/dist/storage/schemas/storage.schema.d.ts +2 -0
- package/dist/storage/schemas/storage.schema.js +6 -0
- package/dist/transport/delivery-stats.d.ts +13 -1
- package/dist/transport/delivery-stats.js +18 -2
- package/dist/transport/delivery.d.ts +4 -3
- package/dist/transport/delivery.js +21 -7
- package/dist/transport/identity-delivery.d.ts +20 -0
- package/dist/transport/identity-delivery.js +41 -0
- package/dist/transport/index.d.ts +2 -0
- package/dist/transport/index.js +1 -0
- package/dist/transport/types/delivery-flow.types.d.ts +7 -0
- package/dist/turns/claude-transcript.js +4 -12
- package/dist/turns/codex-transcript.d.ts +9 -0
- package/dist/turns/codex-transcript.js +27 -19
- package/dist/turns/constants/turns.constants.d.ts +13 -12
- package/dist/turns/constants/turns.constants.js +15 -19
- package/dist/turns/gemini-transcript.d.ts +19 -1
- package/dist/turns/gemini-transcript.js +29 -18
- package/dist/turns/index.d.ts +3 -4
- package/dist/turns/index.js +2 -3
- package/dist/turns/run-state.d.ts +71 -0
- package/dist/turns/run-state.js +114 -0
- package/dist/turns/tool-events.d.ts +11 -1
- package/dist/turns/tool-events.js +32 -9
- package/dist/turns/transcript.d.ts +15 -14
- package/dist/turns/transcript.js +23 -4
- package/dist/turns/turn-state.d.ts +49 -7
- package/dist/turns/turn-state.js +78 -10
- package/dist/turns/turn-summary.js +7 -2
- package/dist/turns/turn-tracker.d.ts +9 -16
- package/dist/turns/turn-tracker.js +162 -86
- package/dist/turns/types/transcript.types.d.ts +11 -1
- package/dist/turns/types/turn-provider.types.d.ts +33 -0
- package/dist/turns/types/turn-provider.types.js +1 -0
- package/dist/turns/types/turn-state.types.d.ts +9 -0
- package/dist/turns/types/turn-summary.types.d.ts +17 -1
- package/dist/turns/types/turn-tracker.types.d.ts +3 -1
- package/dist/turns/work-checkout.js +1 -2
- package/docs/AUDIT_PROMPT.md +10 -0
- package/docs/DATA_HANDLING.md +193 -7
- package/docs/ENTERPRISE_DEPLOYMENT.md +1 -1
- package/docs/PERFORMANCE.md +10 -0
- package/package.json +3 -2
- package/dist/events/canonical-event.d.ts +0 -10
- package/dist/events/canonical-event.js +0 -9
- package/dist/git/repository-root.d.ts +0 -39
- package/dist/git/repository-root.js +0 -131
- package/dist/git/types/repository-root.types.d.ts +0 -11
- package/dist/transport/transport.d.ts +0 -6
- package/dist/turns/codex-originator.d.ts +0 -11
- package/dist/turns/cursor-transcript.d.ts +0 -21
- package/dist/turns/cursor-transcript.js +0 -66
- package/dist/turns/native-session-titles.d.ts +0 -26
- package/dist/turns/native-session-titles.js +0 -102
- /package/dist/{git/types/repository-root.types.js → billing/types/billing.types.js} +0 -0
- /package/dist/{transport/transport.js → capture/types/capture.types.js} +0 -0
|
@@ -0,0 +1,479 @@
|
|
|
1
|
+
import fs from 'node:fs/promises';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import { eventsUrl } from '../config/config.js';
|
|
4
|
+
import { selectRoot, servesMultipleIdentities } from '../config/root-config.js';
|
|
5
|
+
import { debugLog } from '../core/logger.js';
|
|
6
|
+
import { next } from '../core/pipe.js';
|
|
7
|
+
import { hasCredential, identityFingerprint, identityOf } from '../credentials/identity.js';
|
|
8
|
+
import { buildLlmCall } from '../events/llm-call.js';
|
|
9
|
+
import { sha256Hex } from '../events/event-id.js';
|
|
10
|
+
import { CANONICAL_PROVIDER_NAMES } from '../otlp/constants/otlp.constants.js';
|
|
11
|
+
import { applyProductCapture } from '../privacy/product-capture.js';
|
|
12
|
+
import { SECRET_FILE_MODE, SWEEP_MARKER_FILE } from '../storage/constants/storage.constants.js';
|
|
13
|
+
import { DeliveryStats } from '../transport/delivery-stats.js';
|
|
14
|
+
import { identityPaths } from '../transport/queue-partition.js';
|
|
15
|
+
import { agentProcess, systemProcessTable } from '../process/ancestors.js';
|
|
16
|
+
import { latestRunIn, RunStateStore } from '../turns/run-state.js';
|
|
17
|
+
import { TurnStateStore } from '../turns/turn-state.js';
|
|
18
|
+
import { resolveSurface } from '../turns/turn-tracker.js';
|
|
19
|
+
import { CONSUMING_ABANDONED_MS, CONSUMING_FILE_SUFFIX, DROPPED_FILE_SUFFIX, MAX_ID_LENGTH, MAX_RECORDS_PER_HOOK, MAX_SPOOL_FILE_BYTES, RE_SCRIPT_FILE, SPOOL_DIR_NAME, SPOOL_FILE_SUFFIX, SPOOL_IDENTITY_FILE, UNNAMED_COMMAND, WIRE_CONVENTIONS } from './constants/capture.constants.js';
|
|
20
|
+
import { runEntry } from './launched-runs.js';
|
|
21
|
+
import { spoolRecordSchema } from './schemas/capture.schema.js';
|
|
22
|
+
const MS_PER_DAY = 86_400_000;
|
|
23
|
+
const SENDS_EVERYTHING = async () => true;
|
|
24
|
+
/** UTC offsets differ by quarter hours, and two zones by 26 hours at most. */
|
|
25
|
+
const ZONE_STEP_MS = 900_000;
|
|
26
|
+
const MAX_ZONE_GAP_MS = 26 * 3_600_000;
|
|
27
|
+
function sessionLookup(store) {
|
|
28
|
+
const known = new Map();
|
|
29
|
+
return async (record) => {
|
|
30
|
+
for (const ancestor of record.ancestors) {
|
|
31
|
+
const pending = known.get(ancestor.pid) ?? store.sessionOfAgent(ancestor.pid);
|
|
32
|
+
known.set(ancestor.pid, pending);
|
|
33
|
+
const hit = await pending;
|
|
34
|
+
if (hit && (hit.started === undefined || sameStart(hit.started, ancestor.started)))
|
|
35
|
+
return hit.sessionId;
|
|
36
|
+
}
|
|
37
|
+
return undefined;
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Whether two `ps` start times name one moment.
|
|
42
|
+
*
|
|
43
|
+
* `ps` prints local time, and a script may set its own `TZ` (`TZ=UTC pytest`), so the
|
|
44
|
+
* shim's text and the hook's can differ by a zone offset.
|
|
45
|
+
*
|
|
46
|
+
* ponytail: a reused pid started a whole number of quarter hours from the agent, to
|
|
47
|
+
* the second, passes as the agent. Reading zone-free start times would close it, but
|
|
48
|
+
* `ps` has no such field on both platforms.
|
|
49
|
+
*
|
|
50
|
+
* @param a - One start time.
|
|
51
|
+
* @param b - The other.
|
|
52
|
+
* @returns True when they are one moment in some pair of zones.
|
|
53
|
+
*/
|
|
54
|
+
function sameStart(a, b) {
|
|
55
|
+
const gap = Math.abs(Date.parse(a) - Date.parse(b));
|
|
56
|
+
return a === b || (gap <= MAX_ZONE_GAP_MS && gap % ZONE_STEP_MS === 0);
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Usage as the ledger reads it under this agent's name.
|
|
60
|
+
*
|
|
61
|
+
* Shims report one convention (input without cache reads, output with reasoning).
|
|
62
|
+
* Core reads a row's counts by its `provider`, and a captured call's provider is
|
|
63
|
+
* the launching agent, so an OpenAI script under Claude Code must state its fresh
|
|
64
|
+
* input alone or its cached tokens are billed twice.
|
|
65
|
+
*
|
|
66
|
+
* @param usage - The shim's counts.
|
|
67
|
+
* @param agent - The wire provider.
|
|
68
|
+
* @returns The counts for `buildLlmCall`.
|
|
69
|
+
*/
|
|
70
|
+
export function wireUsage(usage, agent) {
|
|
71
|
+
if (!usage)
|
|
72
|
+
return undefined;
|
|
73
|
+
const convention = WIRE_CONVENTIONS[agent] ?? { cachedInInput: false, reasoningInOutput: true };
|
|
74
|
+
const inputTokens = convention.cachedInInput && usage.input_tokens !== undefined ? usage.input_tokens + (usage.cached_input_tokens ?? 0) : usage.input_tokens;
|
|
75
|
+
const outputTokens = !convention.reasoningInOutput && usage.output_tokens !== undefined ? Math.max(0, usage.output_tokens - (usage.reasoning_output_tokens ?? 0)) : usage.output_tokens;
|
|
76
|
+
return {
|
|
77
|
+
inputTokens,
|
|
78
|
+
cachedInputTokens: usage.cached_input_tokens,
|
|
79
|
+
cacheCreationInputTokens: usage.cache_creation_input_tokens,
|
|
80
|
+
outputTokens,
|
|
81
|
+
reasoningOutputTokens: usage.reasoning_output_tokens,
|
|
82
|
+
totalTokens: usage.total_tokens
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* The `llm.call` of one captured record.
|
|
87
|
+
*
|
|
88
|
+
* `provider` is the id the agent's native route sends and `call_id` the provider's
|
|
89
|
+
* request id, exactly as that route sets it (`normalize.ts`): the idempotency key is
|
|
90
|
+
* then the native record's, so core keeps one row. `billing_mode: api`, because a
|
|
91
|
+
* script's metered call on a subscription session would otherwise inherit the
|
|
92
|
+
* session's mode and read as covered by plan.
|
|
93
|
+
*
|
|
94
|
+
* @param record - The spool line.
|
|
95
|
+
* @param input - Who launched it and which run it belongs to.
|
|
96
|
+
* @param input.providerId - The launching agent's provider id.
|
|
97
|
+
* @param input.surface - The surface it runs on.
|
|
98
|
+
* @param input.sessionId - The launching session.
|
|
99
|
+
* @param input.run - The run this call belongs to.
|
|
100
|
+
* @returns The event.
|
|
101
|
+
*/
|
|
102
|
+
export function buildCapturedCall(record, input) {
|
|
103
|
+
const agent = CANONICAL_PROVIDER_NAMES[input.providerId] ?? input.providerId;
|
|
104
|
+
const callId = record.request_id ?? `captured-${sha256Hex(`${record.pid}:${record.started}:${record.seq}`).slice(0, MAX_ID_LENGTH - 16)}`;
|
|
105
|
+
const call = buildLlmCall({
|
|
106
|
+
provider: agent,
|
|
107
|
+
surface: input.surface,
|
|
108
|
+
callId,
|
|
109
|
+
providerRequestId: record.request_id,
|
|
110
|
+
sessionId: input.sessionId,
|
|
111
|
+
turnId: input.run.turnId,
|
|
112
|
+
model: record.model,
|
|
113
|
+
billingMode: 'api',
|
|
114
|
+
status: record.status >= 200 && record.status < 300 ? 'completed' : 'failed',
|
|
115
|
+
correlation: input.run.turnId ? 'exact' : 'session',
|
|
116
|
+
usage: wireUsage(record.usage, agent),
|
|
117
|
+
endedAt: record.at
|
|
118
|
+
});
|
|
119
|
+
return { ...call, run_entry: input.run.entry, platform: record.provider };
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* Create or remove the spool folder: shims write only while it exists.
|
|
123
|
+
*
|
|
124
|
+
* A stage of its own, before the sign-in stop, because it has to run when Edge is
|
|
125
|
+
* signed out, the one case that must capture nothing and must not hold back
|
|
126
|
+
* records to send under whoever signs in next.
|
|
127
|
+
*
|
|
128
|
+
* The machine's answer, not this folder's: the spool is shared, so a hook in a folder
|
|
129
|
+
* no root claims must not delete what a root's sessions have spooled.
|
|
130
|
+
*
|
|
131
|
+
* @param state - Current flow state.
|
|
132
|
+
* @returns The state, unchanged.
|
|
133
|
+
*/
|
|
134
|
+
export async function armCaptureStage(state) {
|
|
135
|
+
if (state.dryRun)
|
|
136
|
+
return next(state);
|
|
137
|
+
const dir = spoolDir(state);
|
|
138
|
+
const machine = state.globalConfig.config;
|
|
139
|
+
const identities = [machine, ...Object.values(machine.roots ?? {})].map(identityOf).filter((identity) => identity !== undefined);
|
|
140
|
+
const current = [...new Set(identities.map(identityFingerprint))].sort();
|
|
141
|
+
const armedFor = current.join(',');
|
|
142
|
+
const marker = path.join(state.paths.dataDir, SPOOL_IDENTITY_FILE);
|
|
143
|
+
try {
|
|
144
|
+
const previous = await fs.readFile(marker, 'utf8').catch(() => undefined);
|
|
145
|
+
// Records spooled under one set of credentials never go out under another: a new
|
|
146
|
+
// root can claim a folder whose calls were spooled under the machine's credential.
|
|
147
|
+
if (identities.length === 0 || (previous !== undefined && previous !== armedFor))
|
|
148
|
+
await fs.rm(dir, { recursive: true, force: true });
|
|
149
|
+
if (identities.length > 0)
|
|
150
|
+
await fs.mkdir(dir, { recursive: true, mode: 0o700 });
|
|
151
|
+
if (previous !== armedFor)
|
|
152
|
+
await fs.writeFile(marker, armedFor, { mode: SECRET_FILE_MODE });
|
|
153
|
+
}
|
|
154
|
+
catch {
|
|
155
|
+
debugLog('capture: could not arm the spool');
|
|
156
|
+
}
|
|
157
|
+
return next(state);
|
|
158
|
+
}
|
|
159
|
+
/**
|
|
160
|
+
* Remember this shell call as a run, and attach what the spool holds to its runs.
|
|
161
|
+
*
|
|
162
|
+
* Runs on every hook on the machine, because a background run's last calls arrive
|
|
163
|
+
* whenever the next hook does. Never fails the flow: every record not yet consumed
|
|
164
|
+
* is still on disk for the next hook.
|
|
165
|
+
*
|
|
166
|
+
* @param state - Current flow state.
|
|
167
|
+
* @param queue - The queue of the identity this hook sends as.
|
|
168
|
+
* @returns The state, unchanged.
|
|
169
|
+
*/
|
|
170
|
+
export async function capturedStage(state, queue) {
|
|
171
|
+
if (state.dryRun)
|
|
172
|
+
return next(state);
|
|
173
|
+
try {
|
|
174
|
+
await rememberThisRun(state);
|
|
175
|
+
// A hook with no credential has nothing to send as; the spool waits for one that does.
|
|
176
|
+
if (hasCredential(state.config))
|
|
177
|
+
await attachSpool(state, queue);
|
|
178
|
+
}
|
|
179
|
+
catch {
|
|
180
|
+
// A fixed sentence: the error could quote a path.
|
|
181
|
+
debugLog('capture: attach failed; the spool stays for the next hook');
|
|
182
|
+
}
|
|
183
|
+
return next(state);
|
|
184
|
+
}
|
|
185
|
+
/**
|
|
186
|
+
* The same, on the one hook that is about to clear the session: the attach runs
|
|
187
|
+
* before the tracker removes what it maps a record to.
|
|
188
|
+
*
|
|
189
|
+
* @param state - Current flow state.
|
|
190
|
+
* @param queue - The queue of the identity this hook sends as.
|
|
191
|
+
* @returns The state, unchanged.
|
|
192
|
+
*/
|
|
193
|
+
export async function capturedAtSessionEnd(state, queue) {
|
|
194
|
+
if (!state.events.some((event) => event.event.type === 'session.ended'))
|
|
195
|
+
return next(state);
|
|
196
|
+
return capturedStage(state, queue);
|
|
197
|
+
}
|
|
198
|
+
async function rememberThisRun(state) {
|
|
199
|
+
const event = state.events.find((candidate) => candidate.event.type === 'shell.started');
|
|
200
|
+
const sessionId = event?.session.id;
|
|
201
|
+
if (!event || !sessionId)
|
|
202
|
+
return;
|
|
203
|
+
const call = state.provider.shellCall?.(state.payload);
|
|
204
|
+
if (typeof call?.command !== 'string')
|
|
205
|
+
return;
|
|
206
|
+
const directory = typeof call.workdir === 'string' && path.isAbsolute(call.workdir) ? call.workdir : state.cwd;
|
|
207
|
+
const entry = await runEntry(call.command, directory, state.config.capture.files !== false);
|
|
208
|
+
if (!entry)
|
|
209
|
+
return;
|
|
210
|
+
const store = new TurnStateStore(state.paths.turnsDir);
|
|
211
|
+
const surface = await resolveSurface(state.provider, state.env, state.payload);
|
|
212
|
+
await rememberAgentIfMoved(state, store, sessionId);
|
|
213
|
+
await new RunStateStore(store).rememberRun(sessionId, { providerId: state.provider.id, surface, turnId: event.session.turnId, entry, at: state.env.now().toISOString() });
|
|
214
|
+
}
|
|
215
|
+
/**
|
|
216
|
+
* Index this session's current agent process, when it is not the one on record.
|
|
217
|
+
*
|
|
218
|
+
* The tracker records it on a session's first hook. `--resume` after an unclean exit
|
|
219
|
+
* has no first hook, and without this its runs would map to the dead process's pid.
|
|
220
|
+
* The parent lookup is one small file; the `ps` runs only when that misses.
|
|
221
|
+
*
|
|
222
|
+
* @param state - Current flow state.
|
|
223
|
+
* @param store - Per-session state.
|
|
224
|
+
* @param sessionId - The session running this shell call.
|
|
225
|
+
*/
|
|
226
|
+
async function rememberAgentIfMoved(state, store, sessionId) {
|
|
227
|
+
const table = state.env.processes ?? systemProcessTable();
|
|
228
|
+
if ((await store.sessionOfAgent(table.ppid))?.sessionId === sessionId)
|
|
229
|
+
return;
|
|
230
|
+
const agent = await agentProcess(table);
|
|
231
|
+
if (agent)
|
|
232
|
+
await store.rememberAgentProcess(sessionId, { pid: agent.pid, started: agent.started });
|
|
233
|
+
}
|
|
234
|
+
function spoolDir(state) {
|
|
235
|
+
return path.join(state.paths.dataDir, SPOOL_DIR_NAME);
|
|
236
|
+
}
|
|
237
|
+
function parseLine(line) {
|
|
238
|
+
try {
|
|
239
|
+
const parsed = spoolRecordSchema.safeParse(JSON.parse(line));
|
|
240
|
+
return parsed.success ? parsed.data : undefined;
|
|
241
|
+
}
|
|
242
|
+
catch {
|
|
243
|
+
return undefined;
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
function isSpoolShaped(stat) {
|
|
247
|
+
return stat.isFile() && stat.size <= MAX_SPOOL_FILE_BYTES;
|
|
248
|
+
}
|
|
249
|
+
async function readRecords(file) {
|
|
250
|
+
const records = [];
|
|
251
|
+
let bad = 0;
|
|
252
|
+
// lstat first: a FIFO named like a spool file would hang the hook on the read.
|
|
253
|
+
if (!isSpoolShaped(await fs.lstat(file)))
|
|
254
|
+
throw new Error('not a spool file');
|
|
255
|
+
for (const line of (await fs.readFile(file, 'utf8')).split('\n')) {
|
|
256
|
+
if (line === '')
|
|
257
|
+
continue;
|
|
258
|
+
const record = parseLine(line);
|
|
259
|
+
if (record)
|
|
260
|
+
records.push(record);
|
|
261
|
+
if (!record)
|
|
262
|
+
bad++;
|
|
263
|
+
}
|
|
264
|
+
return { records, bad };
|
|
265
|
+
}
|
|
266
|
+
async function spoolNames(dir) {
|
|
267
|
+
// A link in the folder's place would aim every removal below at what it names.
|
|
268
|
+
if (!(await fs.lstat(dir).catch(() => undefined))?.isDirectory())
|
|
269
|
+
return [];
|
|
270
|
+
return fs.readdir(dir).then((names) => names.filter((name) => name !== SWEEP_MARKER_FILE), () => []);
|
|
271
|
+
}
|
|
272
|
+
/**
|
|
273
|
+
* Whether a session's spool file is this hook's to send.
|
|
274
|
+
*
|
|
275
|
+
* A file's calls go out on the sending identity's credential. On a machine serving
|
|
276
|
+
* several, another session's file may belong to another tenant, and is left for
|
|
277
|
+
* that session's own hook. With no roots there is one tenant, and this asks nothing.
|
|
278
|
+
*
|
|
279
|
+
* Only a provable match: both in one root. A session started in no root may be routed
|
|
280
|
+
* to whichever root claims the checkout it works in (`inheritedRootConfig`), and that
|
|
281
|
+
* routing is only known when its own turn closes, so its calls wait for that hook.
|
|
282
|
+
*
|
|
283
|
+
* @param state - Current flow state.
|
|
284
|
+
* @param store - Per-session state.
|
|
285
|
+
* @param sessionId - The session the record maps to.
|
|
286
|
+
* @returns False when the record must wait.
|
|
287
|
+
*/
|
|
288
|
+
async function sendsAsThisHook(state, store, sessionId) {
|
|
289
|
+
if (state.summary !== undefined && state.events.some((event) => event.session.id === sessionId))
|
|
290
|
+
return true;
|
|
291
|
+
const roots = state.globalConfig.config.roots;
|
|
292
|
+
const start = await store.readStart(sessionId).catch(() => undefined);
|
|
293
|
+
const root = start === undefined ? undefined : selectRoot(roots, start);
|
|
294
|
+
return root !== undefined && root.path === selectRoot(roots, state.cwd)?.path;
|
|
295
|
+
}
|
|
296
|
+
/**
|
|
297
|
+
* Take spool files one by one, attach their records, queue the calls.
|
|
298
|
+
*
|
|
299
|
+
* A rename is the claim, so two hooks never take one file; the file goes only after
|
|
300
|
+
* the queue has the calls, and one abandoned mid-way is taken again, where the
|
|
301
|
+
* queue's deterministic ids make the second pass a no-op. A process still appending
|
|
302
|
+
* simply starts a new file.
|
|
303
|
+
*/
|
|
304
|
+
async function attachSpool(state, queue) {
|
|
305
|
+
const dir = spoolDir(state);
|
|
306
|
+
const names = await spoolNames(dir);
|
|
307
|
+
if (names.length === 0)
|
|
308
|
+
return;
|
|
309
|
+
const store = new TurnStateStore(state.paths.turnsDir);
|
|
310
|
+
const runs = new RunStateStore(store);
|
|
311
|
+
const sessionOf = sessionLookup(store);
|
|
312
|
+
const sendsHere = sendsHereLookup(state, store);
|
|
313
|
+
const runLists = new Map();
|
|
314
|
+
const oldest = state.env.now().getTime() - state.config.delivery.maxEventAgeDays * MS_PER_DAY;
|
|
315
|
+
const destination = eventsUrl(state.config);
|
|
316
|
+
const stats = new DeliveryStats(identityPaths(state.paths, identityOf(state.config)).statsFile, state.env.now, state.paths.locksDir);
|
|
317
|
+
let dropped = 0;
|
|
318
|
+
let taken = 0;
|
|
319
|
+
for (const name of names) {
|
|
320
|
+
if (taken >= MAX_RECORDS_PER_HOOK)
|
|
321
|
+
break;
|
|
322
|
+
const file = path.join(dir, name);
|
|
323
|
+
const stat = await fs.lstat(file).catch(() => undefined);
|
|
324
|
+
if (!stat)
|
|
325
|
+
continue;
|
|
326
|
+
// Not a shim's work. Removed and counted, so it cannot stop every later hook.
|
|
327
|
+
if (!isSpoolShaped(stat)) {
|
|
328
|
+
await fs.rm(file, { recursive: true, force: true });
|
|
329
|
+
dropped++;
|
|
330
|
+
continue;
|
|
331
|
+
}
|
|
332
|
+
if (name.endsWith(DROPPED_FILE_SUFFIX)) {
|
|
333
|
+
const claimed = `${file}.${process.pid}${CONSUMING_FILE_SUFFIX}`;
|
|
334
|
+
if (await rename(file, claimed)) {
|
|
335
|
+
dropped += (await fs.stat(claimed)).size;
|
|
336
|
+
await fs.rm(claimed, { force: true });
|
|
337
|
+
}
|
|
338
|
+
continue;
|
|
339
|
+
}
|
|
340
|
+
const claimable = name.endsWith(SPOOL_FILE_SUFFIX) || (name.includes(SPOOL_FILE_SUFFIX) && name.endsWith(CONSUMING_FILE_SUFFIX) && (await isAbandoned(file, state.env.now().getTime())));
|
|
341
|
+
if (!claimable)
|
|
342
|
+
continue;
|
|
343
|
+
// Asked of an abandoned file too: the hook that died may have been another tenant's.
|
|
344
|
+
if (!(await holdsCallsOfThisHook(file, sessionOf, sendsHere)))
|
|
345
|
+
continue;
|
|
346
|
+
const claimed = name.endsWith(SPOOL_FILE_SUFFIX) ? `${file}.${process.pid}${CONSUMING_FILE_SUFFIX}` : file;
|
|
347
|
+
if (claimed !== file && !(await rename(file, claimed)))
|
|
348
|
+
continue;
|
|
349
|
+
const { records, bad } = await readRecords(claimed);
|
|
350
|
+
const calls = [];
|
|
351
|
+
const attached = new Map();
|
|
352
|
+
const foreign = [];
|
|
353
|
+
dropped += bad;
|
|
354
|
+
taken += records.length;
|
|
355
|
+
for (const record of records) {
|
|
356
|
+
const sessionId = Date.parse(record.at) >= oldest ? await sessionOf(record) : undefined;
|
|
357
|
+
// Per record: a reused pid can put two tenants' calls in one file.
|
|
358
|
+
if (sessionId && !(await sendsHere(sessionId))) {
|
|
359
|
+
foreign.push(record);
|
|
360
|
+
continue;
|
|
361
|
+
}
|
|
362
|
+
// By the process's start, not the call's time: a background run's later calls
|
|
363
|
+
// stay on the turn that started it, whatever ran since.
|
|
364
|
+
const list = sessionId ? (runLists.get(sessionId) ?? (await runs.runs(sessionId))) : [];
|
|
365
|
+
if (sessionId)
|
|
366
|
+
runLists.set(sessionId, list);
|
|
367
|
+
const run = latestRunIn(list, record.started, record.script);
|
|
368
|
+
if (!sessionId || !run) {
|
|
369
|
+
dropped++;
|
|
370
|
+
continue;
|
|
371
|
+
}
|
|
372
|
+
// The agent that launched the run, not the one whose hook this is: any agent's
|
|
373
|
+
// hook drains the spool, and the idempotency key must be the launching agent's.
|
|
374
|
+
// A file name remembered before `capture.files` went off goes no further.
|
|
375
|
+
const entry = state.config.capture.files === false && RE_SCRIPT_FILE.test(run.entry) ? UNNAMED_COMMAND : run.entry;
|
|
376
|
+
const call = applyProductCapture(buildCapturedCall(record, { providerId: run.providerId, surface: run.surface, sessionId, run: { turnId: run.turnId, entry } }), state.config.capture);
|
|
377
|
+
if (call)
|
|
378
|
+
calls.push(call);
|
|
379
|
+
attached.set(sessionId, record.at);
|
|
380
|
+
}
|
|
381
|
+
await queue.enqueue(calls, destination);
|
|
382
|
+
for (const [sessionId, at] of attached)
|
|
383
|
+
await runs.rememberCaptured(sessionId, at);
|
|
384
|
+
// Before the claim goes, so a crash between the two repeats rather than loses them.
|
|
385
|
+
if (foreign.length > 0) {
|
|
386
|
+
const left = `${claimed}.left${SPOOL_FILE_SUFFIX}`;
|
|
387
|
+
// Never through a link: a sandboxed process can write this folder, and this hook runs outside the sandbox.
|
|
388
|
+
await fs.rm(left, { force: true });
|
|
389
|
+
await fs.writeFile(left, foreign.map((record) => `${JSON.stringify(record)}\n`).join(''), { mode: SECRET_FILE_MODE, flag: 'wx' });
|
|
390
|
+
}
|
|
391
|
+
await fs.rm(claimed, { force: true });
|
|
392
|
+
}
|
|
393
|
+
await stats.recordCapturedDropped(dropped);
|
|
394
|
+
}
|
|
395
|
+
// The mtime is reset, or a file idle for a minute would read as abandoned the moment it is claimed.
|
|
396
|
+
async function rename(from, to) {
|
|
397
|
+
return fs.rename(from, to).then(async () => {
|
|
398
|
+
const now = new Date();
|
|
399
|
+
await fs.utimes(to, now, now).catch(() => undefined);
|
|
400
|
+
return true;
|
|
401
|
+
}, () => false);
|
|
402
|
+
}
|
|
403
|
+
async function isAbandoned(file, now) {
|
|
404
|
+
const stat = await fs.stat(file).catch(() => undefined);
|
|
405
|
+
return stat !== undefined && now - stat.mtimeMs > CONSUMING_ABANDONED_MS;
|
|
406
|
+
}
|
|
407
|
+
/**
|
|
408
|
+
* Whether a spool file holds anything for this hook to do: a call it may send, or a
|
|
409
|
+
* record no session claims, which any hook may drop. A file wholly another tenant's
|
|
410
|
+
* is not claimed, so it is not rewritten on every hook until its session comes.
|
|
411
|
+
*
|
|
412
|
+
* @param file - The spool file.
|
|
413
|
+
* @param sessionOf - Maps a record to its session.
|
|
414
|
+
* @param sendsHere - Whether a session's calls go out on this hook's credential.
|
|
415
|
+
* @returns False when the file must wait for another session's hook.
|
|
416
|
+
*/
|
|
417
|
+
async function holdsCallsOfThisHook(file, sessionOf, sendsHere) {
|
|
418
|
+
if (sendsHere === SENDS_EVERYTHING)
|
|
419
|
+
return true;
|
|
420
|
+
const { records } = await readRecords(file).catch(() => ({ records: [] }));
|
|
421
|
+
for (const record of records) {
|
|
422
|
+
const sessionId = await sessionOf(record);
|
|
423
|
+
if (!sessionId || (await sendsHere(sessionId)))
|
|
424
|
+
return true;
|
|
425
|
+
}
|
|
426
|
+
return records.length === 0;
|
|
427
|
+
}
|
|
428
|
+
/**
|
|
429
|
+
* Whether each session's calls go out on this hook's credential, asked once per session.
|
|
430
|
+
*
|
|
431
|
+
* @param state - Current flow state.
|
|
432
|
+
* @param store - Per-session state.
|
|
433
|
+
* @returns The question, or the constant answer on a machine with one identity.
|
|
434
|
+
*/
|
|
435
|
+
function sendsHereLookup(state, store) {
|
|
436
|
+
if (!servesMultipleIdentities(state.globalConfig.config))
|
|
437
|
+
return SENDS_EVERYTHING;
|
|
438
|
+
const known = new Map();
|
|
439
|
+
return (sessionId) => {
|
|
440
|
+
const pending = known.get(sessionId) ?? sendsAsThisHook(state, store, sessionId);
|
|
441
|
+
known.set(sessionId, pending);
|
|
442
|
+
return pending;
|
|
443
|
+
};
|
|
444
|
+
}
|
|
445
|
+
/**
|
|
446
|
+
* Whether the session's current run has produced a captured call, so the
|
|
447
|
+
* untraced-runs notice has nothing to say.
|
|
448
|
+
*
|
|
449
|
+
* Before the attach step in the flow, so it looks at the spool as well as at what
|
|
450
|
+
* an earlier hook already attached.
|
|
451
|
+
*
|
|
452
|
+
* @param state - Current flow state.
|
|
453
|
+
* @param sessionId - The session whose shell call just completed.
|
|
454
|
+
* @returns True when a captured call of the current run exists.
|
|
455
|
+
*/
|
|
456
|
+
export async function runWasCaptured(state, sessionId) {
|
|
457
|
+
const store = new TurnStateStore(state.paths.turnsDir);
|
|
458
|
+
const runs = new RunStateStore(store);
|
|
459
|
+
const run = await runs.latestRunBefore(sessionId, state.env.now().toISOString());
|
|
460
|
+
if (!run)
|
|
461
|
+
return false;
|
|
462
|
+
const since = Date.parse(run.at);
|
|
463
|
+
const last = await runs.lastCaptured(sessionId);
|
|
464
|
+
if (last !== undefined && Date.parse(last) >= since)
|
|
465
|
+
return true;
|
|
466
|
+
const dir = spoolDir(state);
|
|
467
|
+
const sessionOf = sessionLookup(store);
|
|
468
|
+
for (const name of await spoolNames(dir)) {
|
|
469
|
+
// A claimed file counts too: another hook holding it has not attached it yet.
|
|
470
|
+
if (!name.includes(SPOOL_FILE_SUFFIX))
|
|
471
|
+
continue;
|
|
472
|
+
const { records } = await readRecords(path.join(dir, name)).catch(() => ({ records: [] }));
|
|
473
|
+
for (const record of records) {
|
|
474
|
+
if (Date.parse(record.at) >= since && (await sessionOf(record)) === sessionId)
|
|
475
|
+
return true;
|
|
476
|
+
}
|
|
477
|
+
}
|
|
478
|
+
return false;
|
|
479
|
+
}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
/** Where shims spool, under the data dir. The folder's existence is what arms them. */
|
|
2
|
+
export declare const SPOOL_DIR_NAME = "captured";
|
|
3
|
+
export declare const SPOOL_FILE_SUFFIX = ".jsonl";
|
|
4
|
+
export declare const DROPPED_FILE_SUFFIX = ".dropped";
|
|
5
|
+
export declare const CONSUMING_FILE_SUFFIX = ".consuming";
|
|
6
|
+
/** A hook that died holding a spool file left it here; another picks it up after this. */
|
|
7
|
+
export declare const CONSUMING_ABANDONED_MS = 60000;
|
|
8
|
+
/** Stop taking new spool files past this many records, so a hook stays short. */
|
|
9
|
+
export declare const MAX_RECORDS_PER_HOOK = 2000;
|
|
10
|
+
/** The two limits the bridge's `llm.call` holds a captured call to. */
|
|
11
|
+
export declare const MAX_RUN_ENTRY_LENGTH = 500;
|
|
12
|
+
export declare const MAX_ID_LENGTH = 200;
|
|
13
|
+
export declare const STAGE_CAPTURE_ARM = "capture-arm";
|
|
14
|
+
export declare const STAGE_CAPTURED_AT_END = "captured-at-session-end";
|
|
15
|
+
export declare const STAGE_CAPTURED = "captured";
|
|
16
|
+
export declare const RUN_CAPTURE_ENV_NODE = "NODE_OPTIONS";
|
|
17
|
+
export declare const RUN_CAPTURE_ENV_PYTHON = "PYTHONPATH";
|
|
18
|
+
/** What a built install ships, relative to the package root, and setup copies. */
|
|
19
|
+
export declare const NODE_SHIM_PATH: string;
|
|
20
|
+
export declare const PYTHON_SHIM_DIR: string;
|
|
21
|
+
export declare const PYTHON_SHIM_FILE = "sitecustomize.py";
|
|
22
|
+
/** What a shell call does before the program it came to run. */
|
|
23
|
+
export declare const RE_CD: RegExp;
|
|
24
|
+
export declare const RE_SHELL_EXPANDED: RegExp;
|
|
25
|
+
export declare const RE_SETUP_COMMAND: RegExp;
|
|
26
|
+
export declare const EDGE_PACKAGE_NAME = "@agent-watch-ai/edge";
|
|
27
|
+
export declare const RE_REQUIRE_FLAG: RegExp;
|
|
28
|
+
export declare const RE_OUTER_QUOTES: RegExp;
|
|
29
|
+
export declare const RE_NODE_OPTIONS_UNESCAPE: RegExp;
|
|
30
|
+
/** Where setup copies the shims, under the data dir: see `copiedShims`. */
|
|
31
|
+
export declare const CAPTURE_COPY_DIR = "capture";
|
|
32
|
+
/** The shape of a shim's entry in a merged value. Only a candidate: `insideEdge` decides. */
|
|
33
|
+
export declare const RE_OUR_REQUIRE: RegExp;
|
|
34
|
+
export declare const RE_OUR_PYTHON_DIR: RegExp;
|
|
35
|
+
export declare const RE_NEEDS_DOUBLE_QUOTES: RegExp;
|
|
36
|
+
export declare const RE_NODE_OPTIONS_ESCAPE: RegExp;
|
|
37
|
+
/**
|
|
38
|
+
* How the ledger reads a provider's counts, by the agent id a captured call is
|
|
39
|
+
* sent under. Mirrors core's `token-semantics`: the convention follows the row's
|
|
40
|
+
* `provider`, and a captured call's provider is the launching agent, whoever
|
|
41
|
+
* served the call.
|
|
42
|
+
*/
|
|
43
|
+
export declare const WIRE_CONVENTIONS: Readonly<Record<string, {
|
|
44
|
+
readonly cachedInInput: boolean;
|
|
45
|
+
readonly reasoningInOutput: boolean;
|
|
46
|
+
}>>;
|
|
47
|
+
/** `<runner> -c`, `-e` and friends: an inline program has no file. */
|
|
48
|
+
export declare const INLINE_FLAGS: ReadonlySet<string>;
|
|
49
|
+
export declare const MODULE_FLAG = "-m";
|
|
50
|
+
export declare const RE_SCRIPT_RUNNER: RegExp;
|
|
51
|
+
export declare const RE_PYTHON: RegExp;
|
|
52
|
+
export declare const RE_SCRIPT_FILE: RegExp;
|
|
53
|
+
export declare const RE_MODULE_NAME: RegExp;
|
|
54
|
+
/** An assignment or a URL is an argument's value, never a program's name. */
|
|
55
|
+
export declare const RE_NOT_A_NAME: RegExp;
|
|
56
|
+
export declare const RE_ENV_ASSIGNMENT: RegExp;
|
|
57
|
+
/** Shell words: adjacent quoted, substituted and bare parts are one word, so `A="b c"` and `A=$(b c)` stay whole. */
|
|
58
|
+
export declare const RE_COMMAND_WORD: RegExp;
|
|
59
|
+
export declare const RE_WORD_QUOTES: RegExp;
|
|
60
|
+
export declare const RE_SUBSTITUTION: RegExp;
|
|
61
|
+
/** One character of `&&`, `||`, `;`, `|` or a newline; an empty part between two is dropped. */
|
|
62
|
+
export declare const RE_SEPARATOR_CHAR: RegExp;
|
|
63
|
+
/** A command whose program has no name safe to send. */
|
|
64
|
+
export declare const UNNAMED_COMMAND = "command";
|
|
65
|
+
/** A spool file past this is no shim's work: 10,000 lines of a few hundred bytes. */
|
|
66
|
+
export declare const MAX_SPOOL_FILE_BYTES: number;
|
|
67
|
+
/** Beside the spool: which identities it was armed for. */
|
|
68
|
+
export declare const SPOOL_IDENTITY_FILE = "captured.identity";
|
|
69
|
+
/** A relative preload: kept from the shell it would run from every repository. */
|
|
70
|
+
export declare const RE_RELATIVE_PRELOAD: RegExp;
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import path from 'node:path';
|
|
2
|
+
/** Where shims spool, under the data dir. The folder's existence is what arms them. */
|
|
3
|
+
export const SPOOL_DIR_NAME = 'captured';
|
|
4
|
+
export const SPOOL_FILE_SUFFIX = '.jsonl';
|
|
5
|
+
export const DROPPED_FILE_SUFFIX = '.dropped';
|
|
6
|
+
export const CONSUMING_FILE_SUFFIX = '.consuming';
|
|
7
|
+
/** A hook that died holding a spool file left it here; another picks it up after this. */
|
|
8
|
+
export const CONSUMING_ABANDONED_MS = 60_000;
|
|
9
|
+
/** Stop taking new spool files past this many records, so a hook stays short. */
|
|
10
|
+
export const MAX_RECORDS_PER_HOOK = 2000;
|
|
11
|
+
/** The two limits the bridge's `llm.call` holds a captured call to. */
|
|
12
|
+
export const MAX_RUN_ENTRY_LENGTH = 500;
|
|
13
|
+
export const MAX_ID_LENGTH = 200;
|
|
14
|
+
export const STAGE_CAPTURE_ARM = 'capture-arm';
|
|
15
|
+
export const STAGE_CAPTURED_AT_END = 'captured-at-session-end';
|
|
16
|
+
export const STAGE_CAPTURED = 'captured';
|
|
17
|
+
export const RUN_CAPTURE_ENV_NODE = 'NODE_OPTIONS';
|
|
18
|
+
export const RUN_CAPTURE_ENV_PYTHON = 'PYTHONPATH';
|
|
19
|
+
/** What a built install ships, relative to the package root, and setup copies. */
|
|
20
|
+
export const NODE_SHIM_PATH = path.join('dist', 'capture', 'node.cjs');
|
|
21
|
+
export const PYTHON_SHIM_DIR = path.join('dist', 'capture', 'python');
|
|
22
|
+
export const PYTHON_SHIM_FILE = 'sitecustomize.py';
|
|
23
|
+
/** What a shell call does before the program it came to run. */
|
|
24
|
+
export const RE_CD = /^(?:cd|pushd)\s+("[^"]*"|'[^']*'|\S+)\s*$/;
|
|
25
|
+
export const RE_SHELL_EXPANDED = /[$~`*]/;
|
|
26
|
+
export const RE_SETUP_COMMAND = /^(?:cd|pushd|export|source|set|unset|\.)(?:\s|$)/;
|
|
27
|
+
export const EDGE_PACKAGE_NAME = '@agent-watch-ai/edge';
|
|
28
|
+
export const RE_REQUIRE_FLAG = /^--require[= ]/;
|
|
29
|
+
export const RE_OUTER_QUOTES = /^"|"$/g;
|
|
30
|
+
export const RE_NODE_OPTIONS_UNESCAPE = /\\(["\\])/g;
|
|
31
|
+
/** Where setup copies the shims, under the data dir: see `copiedShims`. */
|
|
32
|
+
export const CAPTURE_COPY_DIR = 'capture';
|
|
33
|
+
/** The shape of a shim's entry in a merged value. Only a candidate: `insideEdge` decides. */
|
|
34
|
+
export const RE_OUR_REQUIRE = /(?:^|\s)--require[= ](?:"(?:[^"\\]|\\.)*capture[\\/]node\.cjs"|\S*capture[\\/]node\.cjs)(?=\s|$)/g;
|
|
35
|
+
export const RE_OUR_PYTHON_DIR = /[\\/]capture[\\/]python[\\/]?$/;
|
|
36
|
+
export const RE_NEEDS_DOUBLE_QUOTES = /[\s"\\]/;
|
|
37
|
+
export const RE_NODE_OPTIONS_ESCAPE = /(["\\])/g;
|
|
38
|
+
/**
|
|
39
|
+
* How the ledger reads a provider's counts, by the agent id a captured call is
|
|
40
|
+
* sent under. Mirrors core's `token-semantics`: the convention follows the row's
|
|
41
|
+
* `provider`, and a captured call's provider is the launching agent, whoever
|
|
42
|
+
* served the call.
|
|
43
|
+
*/
|
|
44
|
+
export const WIRE_CONVENTIONS = {
|
|
45
|
+
'claude-code': { cachedInInput: false, reasoningInOutput: true },
|
|
46
|
+
codex: { cachedInInput: true, reasoningInOutput: true },
|
|
47
|
+
gemini: { cachedInInput: true, reasoningInOutput: false }
|
|
48
|
+
};
|
|
49
|
+
/** `<runner> -c`, `-e` and friends: an inline program has no file. */
|
|
50
|
+
export const INLINE_FLAGS = new Set(['-c', '-e', '-p', '--eval', '--print']);
|
|
51
|
+
export const MODULE_FLAG = '-m';
|
|
52
|
+
export const RE_SCRIPT_RUNNER = /^(?:npm|pnpm|yarn)$/;
|
|
53
|
+
export const RE_PYTHON = /^python[\d.]*$/;
|
|
54
|
+
export const RE_SCRIPT_FILE = /\.(?:py|pyw|js|mjs|cjs|ts|mts|cts|tsx|jsx)$/i;
|
|
55
|
+
export const RE_MODULE_NAME = /^[\w.-]+$/;
|
|
56
|
+
/** An assignment or a URL is an argument's value, never a program's name. */
|
|
57
|
+
export const RE_NOT_A_NAME = /=|:\/\/|:_/;
|
|
58
|
+
export const RE_ENV_ASSIGNMENT = /^\w+=/;
|
|
59
|
+
/** Shell words: adjacent quoted, substituted and bare parts are one word, so `A="b c"` and `A=$(b c)` stay whole. */
|
|
60
|
+
export const RE_COMMAND_WORD = /(?:\$\([^)]*\)|`[^`]*`|"(?:[^"\\]|\\.)*"|'[^']*'|\\.|[^\s"'`\\])+/g;
|
|
61
|
+
export const RE_WORD_QUOTES = /["']/g;
|
|
62
|
+
export const RE_SUBSTITUTION = /\$[({']|`/;
|
|
63
|
+
/** One character of `&&`, `||`, `;`, `|` or a newline; an empty part between two is dropped. */
|
|
64
|
+
export const RE_SEPARATOR_CHAR = /[;&|\n]/;
|
|
65
|
+
/** A command whose program has no name safe to send. */
|
|
66
|
+
export const UNNAMED_COMMAND = 'command';
|
|
67
|
+
/** A spool file past this is no shim's work: 10,000 lines of a few hundred bytes. */
|
|
68
|
+
export const MAX_SPOOL_FILE_BYTES = 32 * 1024 * 1024;
|
|
69
|
+
/** Beside the spool: which identities it was armed for. */
|
|
70
|
+
export const SPOOL_IDENTITY_FILE = 'captured.identity';
|
|
71
|
+
/** A relative preload: kept from the shell it would run from every repository. */
|
|
72
|
+
export const RE_RELATIVE_PRELOAD = /(?:^|\s)(?:--require|--import|--loader|--experimental-loader|-r)(?:[= ]|(?=[^\s=-]))(?:"(?!\/)[^"]*"|(?!["/])\S+)/g;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The name a shell call's captured calls are labelled with.
|
|
3
|
+
*
|
|
4
|
+
* The first segment that runs a program, by the same closed list the untraced
|
|
5
|
+
* notice uses, else the command's first word: a captured call may come from a run
|
|
6
|
+
* that list does not know (`./eval.py`, `make eval`) and still belongs to this
|
|
7
|
+
* call's turn. A script is named relative to its checkout, by file name alone
|
|
8
|
+
* outside one, and only by the runner's name when file names are off. Scrubbed
|
|
9
|
+
* here, as a turn summary's paths are: only this leaves the command.
|
|
10
|
+
*
|
|
11
|
+
* @param command - The shell command, read in memory only.
|
|
12
|
+
* @param directory - Where it runs.
|
|
13
|
+
* @param filesAllowed - Whether capture policy allows file names.
|
|
14
|
+
* @returns The entry, or undefined for an empty command.
|
|
15
|
+
*/
|
|
16
|
+
export declare function runEntry(command: string, directory: string, filesAllowed: boolean): Promise<string | undefined>;
|