@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
|
@@ -17,9 +17,49 @@ export const OTLP_PROTOCOL = 'http/json';
|
|
|
17
17
|
export const RE_HOOK_SUFFIX = /\s+hook\s+--agent\s+\S+\s*$/;
|
|
18
18
|
/** Where Claude Code's exporters send: the OTLP base every signal derives from. */
|
|
19
19
|
export const OTLP_ENDPOINT_KEY = 'OTEL_EXPORTER_OTLP_ENDPOINT';
|
|
20
|
+
/** Each signal's own endpoint key, mapped to the exporter switch that signal reads. */
|
|
21
|
+
export const OTLP_SIGNAL_EXPORTER_KEYS = new Map([
|
|
22
|
+
['OTEL_EXPORTER_OTLP_LOGS_ENDPOINT', 'OTEL_LOGS_EXPORTER'],
|
|
23
|
+
['OTEL_EXPORTER_OTLP_TRACES_ENDPOINT', 'OTEL_TRACES_EXPORTER'],
|
|
24
|
+
['OTEL_EXPORTER_OTLP_METRICS_ENDPOINT', 'OTEL_METRICS_EXPORTER']
|
|
25
|
+
]);
|
|
20
26
|
/** Per-signal endpoints, which override the base for that one signal. */
|
|
21
|
-
export const OTLP_SIGNAL_ENDPOINT_KEYS = [
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
];
|
|
27
|
+
export const OTLP_SIGNAL_ENDPOINT_KEYS = [...OTLP_SIGNAL_EXPORTER_KEYS.keys()];
|
|
28
|
+
/** Claude's enhanced-traces beta switch, which only adds the llm_request spans. */
|
|
29
|
+
export const TRACES_BETA_KEY = 'CLAUDE_CODE_ENHANCED_TELEMETRY_BETA';
|
|
30
|
+
/** Keys a signal's exporter needs beside itself, by exporter switch. */
|
|
31
|
+
export const SIGNAL_EXPORTER_EXTRA_KEYS = new Map([['OTEL_TRACES_EXPORTER', [TRACES_BETA_KEY]]]);
|
|
32
|
+
/** Keys every exporting signal needs, whoever's collector it sends to. */
|
|
33
|
+
export const OTEL_SHARED_KEYS = ['CLAUDE_CODE_ENABLE_TELEMETRY', 'OTEL_EXPORTER_OTLP_PROTOCOL'];
|
|
34
|
+
/**
|
|
35
|
+
* Claude's own content-logging switches, forced off.
|
|
36
|
+
*
|
|
37
|
+
* Never treated as a foreign key: a user who set `OTEL_LOG_USER_PROMPTS=1`
|
|
38
|
+
* before installing is exactly the case this exists to override, and refusing
|
|
39
|
+
* to configure them would leave prompt logging on *and* drop the llm.call
|
|
40
|
+
* ledger. Overriding a switch that only ever reduces what leaves the machine
|
|
41
|
+
* is not the redirection the conflict check protects against.
|
|
42
|
+
*/
|
|
43
|
+
export const CONTENT_LOG_OFF = {
|
|
44
|
+
OTEL_LOG_USER_PROMPTS: '0',
|
|
45
|
+
OTEL_LOG_TOOL_DETAILS: '0',
|
|
46
|
+
OTEL_LOG_TOOL_CONTENT: '0'
|
|
47
|
+
};
|
|
48
|
+
/**
|
|
49
|
+
* Every value any AgentWatch setup writes for an env key, whatever the signals
|
|
50
|
+
* were then. Without an install record, a key counts as ours only when it holds
|
|
51
|
+
* one of these. The endpoint is judged on its own (an Agent Watch host).
|
|
52
|
+
*/
|
|
53
|
+
export const AGENTWATCH_ENV_VALUES = new Map([
|
|
54
|
+
['CLAUDE_CODE_ENABLE_TELEMETRY', new Set(['1'])],
|
|
55
|
+
...Object.entries(CONTENT_LOG_OFF).map(([key, value]) => [key, new Set([value])]),
|
|
56
|
+
...[...OTLP_SIGNAL_EXPORTER_KEYS.values()].map((key) => [key, new Set([OTEL_EXPORTER_OTLP, OTEL_EXPORTER_NONE])]),
|
|
57
|
+
['OTEL_EXPORTER_OTLP_PROTOCOL', new Set([OTLP_PROTOCOL])],
|
|
58
|
+
[TRACES_BETA_KEY, new Set(['1'])]
|
|
59
|
+
]);
|
|
60
|
+
/** What uninstall says about keys it left, by why it left them. */
|
|
61
|
+
export const KEPT_REASON_MESSAGES = {
|
|
62
|
+
foreignEndpoint: `${OTLP_ENDPOINT_KEY} is not an AgentWatch endpoint, so they belong to that collector`,
|
|
63
|
+
noEndpoint: `the file names no ${OTLP_ENDPOINT_KEY}, so nothing proves they are AgentWatch's`,
|
|
64
|
+
ownSignal: 'a signal there exports to its own endpoint and needs them'
|
|
65
|
+
};
|
|
@@ -1,3 +1,16 @@
|
|
|
1
1
|
import type { z } from 'zod';
|
|
2
2
|
import type { claudePayloadSchema } from '../schemas/claude.schema.js';
|
|
3
3
|
export type ClaudePayload = z.infer<typeof claudePayloadSchema>;
|
|
4
|
+
/**
|
|
5
|
+
* Why uninstall left AgentWatch-looking keys: the base endpoint is someone
|
|
6
|
+
* else's, the file names no base endpoint to judge, or a signal exports to the
|
|
7
|
+
* developer's own endpoint.
|
|
8
|
+
*/
|
|
9
|
+
export type KeptReason = 'foreignEndpoint' | 'noEndpoint' | 'ownSignal';
|
|
10
|
+
/** Env keys uninstall drops, and keys it leaves because nothing proves them ours. */
|
|
11
|
+
export interface EnvRemoval {
|
|
12
|
+
readonly removable: ReadonlySet<string>;
|
|
13
|
+
readonly kept: readonly string[];
|
|
14
|
+
/** Why `kept` was left; absent when an install record decided everything. */
|
|
15
|
+
readonly reason?: KeptReason;
|
|
16
|
+
}
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import type { UnknownRecord } from '../../core/types/core.types.js';
|
|
2
|
+
import type { RunCaptureStatus, SetupContext, SetupOutcome } from '../types/provider.types.js';
|
|
3
|
+
/**
|
|
4
|
+
* The file with `set` holding the block's two capture keys, every other line as it was.
|
|
5
|
+
*
|
|
6
|
+
* Line edits, not a re-serialization: this file holds the developer's comments. No
|
|
7
|
+
* markers around them either, unlike the `[otel]` block: Codex writes its own tables
|
|
8
|
+
* inside a marker span when it re-serializes the file, and the two keys are found by
|
|
9
|
+
* name, so there is nothing a span would add.
|
|
10
|
+
*
|
|
11
|
+
* @param raw - The whole config.toml.
|
|
12
|
+
* @param parsed - The same, parsed.
|
|
13
|
+
* @param block - The `set` table as it should end up.
|
|
14
|
+
* @returns The next file; undefined when no line edit gives exactly that.
|
|
15
|
+
*/
|
|
16
|
+
export declare function withCaptureSet(raw: string, parsed: UnknownRecord, block: UnknownRecord): string | undefined;
|
|
17
|
+
/**
|
|
18
|
+
* Merge the two keys into `[shell_environment_policy] set`. The block's value is the
|
|
19
|
+
* developer's; `set` replaces what the shell has, so the ambient one moves in too.
|
|
20
|
+
*
|
|
21
|
+
* ponytail: uninstall cannot tell an ambient value copied here from one the developer
|
|
22
|
+
* wrote, so it stays in `set` and keeps masking the shell's, as in Claude Code's env block.
|
|
23
|
+
* The upgrade is recording what setup copied.
|
|
24
|
+
*
|
|
25
|
+
* A file Edge cannot edit is said, not failed: capture is optional beside the export.
|
|
26
|
+
*
|
|
27
|
+
* @param context - Environment, paths and config.
|
|
28
|
+
* @param source - The package's shims.
|
|
29
|
+
* @returns Whether the file changed, and what to tell the developer.
|
|
30
|
+
*/
|
|
31
|
+
export declare function applyCodexCapture(context: SetupContext, source?: {
|
|
32
|
+
node: string;
|
|
33
|
+
python: string;
|
|
34
|
+
}): Promise<SetupOutcome>;
|
|
35
|
+
/**
|
|
36
|
+
* Take our part of the two keys out, then the shared copy once nothing else needs it.
|
|
37
|
+
*
|
|
38
|
+
* Fails only when our keys stay behind: a file holding nothing of ours, in whatever form,
|
|
39
|
+
* is not this step's to complain about.
|
|
40
|
+
*
|
|
41
|
+
* @param context - Environment and paths.
|
|
42
|
+
* @returns Whether the file changed, and what to tell the developer.
|
|
43
|
+
*/
|
|
44
|
+
export declare function removeCodexCapture(context: SetupContext): Promise<SetupOutcome>;
|
|
45
|
+
/**
|
|
46
|
+
* The capture keys a `shell_environment_policy` stops, and the ones it only names.
|
|
47
|
+
*
|
|
48
|
+
* Codex's config reference (2026-11-02): `set` values are injected after exclusions, and
|
|
49
|
+
* include filters can still remove them. So an allowlist that misses a key strips it. An
|
|
50
|
+
* exclude that names one should not, per the reference, but a developer who wrote it
|
|
51
|
+
* probably expects it to, so it is said rather than called off.
|
|
52
|
+
*
|
|
53
|
+
* ponytail: only the user's config.toml is read. A project `.codex/config.toml` or a
|
|
54
|
+
* profile can filter too; the upgrade is the layer stack Codex itself merges.
|
|
55
|
+
*
|
|
56
|
+
* @param policy - The table as parsed.
|
|
57
|
+
* @returns The keys an allowlist strips and the keys an exclude names.
|
|
58
|
+
*/
|
|
59
|
+
export declare function filteredCaptureKeys(policy: UnknownRecord): {
|
|
60
|
+
stripped: string[];
|
|
61
|
+
excluded: string[];
|
|
62
|
+
};
|
|
63
|
+
/**
|
|
64
|
+
* Whether what Codex's shell tool starts loads the shims, and when not, why.
|
|
65
|
+
*
|
|
66
|
+
* Gated on `otel` as Claude Code's is, not on the tool-content consent that holds Codex's
|
|
67
|
+
* own export back: a captured call is usage only, it carries no tool content.
|
|
68
|
+
*
|
|
69
|
+
* @param context - Environment, paths and config.
|
|
70
|
+
* @param source - The package's shims.
|
|
71
|
+
* @returns On, or off with the reason.
|
|
72
|
+
*/
|
|
73
|
+
export declare function inspectCodexCapture(context: SetupContext, source?: {
|
|
74
|
+
node: string;
|
|
75
|
+
python: string;
|
|
76
|
+
}): Promise<RunCaptureStatus>;
|
|
@@ -0,0 +1,454 @@
|
|
|
1
|
+
import fs from 'node:fs/promises';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import { isDeepStrictEqual } from 'node:util';
|
|
4
|
+
import { parse as parseToml, stringify as stringifyToml } from 'smol-toml';
|
|
5
|
+
import { copiedShims, inspectCaptureEnv, installedCaptureEnv, isLinkedConfig, linkedConfigReason, removeCaptureCopy, shimPaths, withoutCaptureEnv } from '../../capture/capture-env.js';
|
|
6
|
+
import { RUN_CAPTURE_ENV_NODE, RUN_CAPTURE_ENV_PYTHON, SPOOL_DIR_NAME } from '../../capture/constants/capture.constants.js';
|
|
7
|
+
import { otelEnabled } from '../../config/config.js';
|
|
8
|
+
import { asRecord } from '../../core/object.js';
|
|
9
|
+
import { backupFile, currentMode, writeFileAtomic, writeIfUnchanged } from '../../storage/atomic-file.js';
|
|
10
|
+
import { SECRET_FILE_MODE } from '../../storage/constants/storage.constants.js';
|
|
11
|
+
import { codexConfigTomlPath } from './codex.detect.js';
|
|
12
|
+
import { RE_TOML_TABLE_HEADER } from './constants/codex.otel.constants.js';
|
|
13
|
+
const POLICY_KEY = 'shell_environment_policy';
|
|
14
|
+
const SET_KEY = 'set';
|
|
15
|
+
const POLICY_HEADER = `[${POLICY_KEY}]`;
|
|
16
|
+
const SET_HEADER = `[${POLICY_KEY}.${SET_KEY}]`;
|
|
17
|
+
const SANDBOX_KEY = 'sandbox_workspace_write';
|
|
18
|
+
const ROOTS_KEY = 'writable_roots';
|
|
19
|
+
const PROFILE_KEY = 'default_permissions';
|
|
20
|
+
const SANDBOX_HEADER = `[${SANDBOX_KEY}]`;
|
|
21
|
+
const CAPTURE_COMMENT = '# AgentWatch run capture; `agentwatch uninstall` takes it out';
|
|
22
|
+
const CAPTURE_KEYS = new Set([RUN_CAPTURE_ENV_NODE, RUN_CAPTURE_ENV_PYTHON]);
|
|
23
|
+
const RE_INLINE_SET = /^\s*set\s*=\s*\{/;
|
|
24
|
+
const RE_LEADING_SPACE = /^\s*/;
|
|
25
|
+
const RE_KEY_LINE = /^\s*"?([\w-]+)"?\s*=/;
|
|
26
|
+
const RE_POLICY_HEADER = /^\s*\[\s*"?shell_environment_policy"?\s*[.\]]/;
|
|
27
|
+
const RE_SANDBOX_HEADER = /^\s*\[\s*"?sandbox_workspace_write"?\s*\]/;
|
|
28
|
+
const RE_ROOTS_LINE = /^\s*"?writable_roots"?\s*=\s*\[/;
|
|
29
|
+
const RE_COMMA_AFTER = /^\s*,[ \t]*/;
|
|
30
|
+
const RE_COMMA_BEFORE = /,\s*$/;
|
|
31
|
+
const RE_OWN_LINE_INDENT = /\n[ \t]*$/;
|
|
32
|
+
const RE_WILDCARD_LITERAL = /[.+^${}()|[\]\\]/g;
|
|
33
|
+
const RE_WILDCARD_ANY = /\*/g;
|
|
34
|
+
const RE_WILDCARD_ONE = /\?/g;
|
|
35
|
+
function tryParse(raw) {
|
|
36
|
+
try {
|
|
37
|
+
return asRecord(parseToml(raw));
|
|
38
|
+
}
|
|
39
|
+
catch {
|
|
40
|
+
return undefined;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
async function readConfig(file) {
|
|
44
|
+
try {
|
|
45
|
+
return await fs.readFile(file, 'utf8');
|
|
46
|
+
}
|
|
47
|
+
catch (error) {
|
|
48
|
+
return error.code === 'ENOENT' ? undefined : null;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
/** The line after `start`'s section: the next table header, or the end. */
|
|
52
|
+
function sectionEnd(lines, start) {
|
|
53
|
+
const next = lines.findIndex((line, index) => index > start && RE_TOML_TABLE_HEADER.test(line));
|
|
54
|
+
return next === -1 ? lines.length : next;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Where the file keeps `set`, as far as a line edit can reach it.
|
|
58
|
+
*
|
|
59
|
+
* A root-level `shell_environment_policy.set.X` or an inline policy table is legal TOML
|
|
60
|
+
* a line edit cannot safely extend, so it is refused and `inspect` says so.
|
|
61
|
+
*/
|
|
62
|
+
function setForm(lines, parsed) {
|
|
63
|
+
const policy = parsed[POLICY_KEY];
|
|
64
|
+
if (policy === undefined)
|
|
65
|
+
return { kind: 'none' };
|
|
66
|
+
const set = asRecord(policy)?.[SET_KEY];
|
|
67
|
+
const table = lines.findIndex((line) => line.trim().startsWith(SET_HEADER));
|
|
68
|
+
if (table !== -1)
|
|
69
|
+
return { kind: 'table', line: table };
|
|
70
|
+
const header = lines.findIndex((line) => line.trim().startsWith(POLICY_HEADER));
|
|
71
|
+
if (set === undefined)
|
|
72
|
+
return lines.some((line) => RE_POLICY_HEADER.test(line)) ? { kind: 'none' } : { kind: 'unmergeable' };
|
|
73
|
+
if (header === -1)
|
|
74
|
+
return { kind: 'unmergeable' };
|
|
75
|
+
const inline = lines.slice(header + 1, sectionEnd(lines, header)).findIndex((line) => RE_INLINE_SET.test(line));
|
|
76
|
+
return inline === -1 ? { kind: 'unmergeable' } : { kind: 'inline', line: header + 1 + inline };
|
|
77
|
+
}
|
|
78
|
+
function pairsOf(values) {
|
|
79
|
+
return Object.entries(values).map(([key, value]) => stringifyToml({ [key]: value }).trim());
|
|
80
|
+
}
|
|
81
|
+
function captureKeys(block) {
|
|
82
|
+
return Object.fromEntries(Object.entries(block).filter(([key]) => CAPTURE_KEYS.has(key)));
|
|
83
|
+
}
|
|
84
|
+
function withTableKeys(lines, header, block) {
|
|
85
|
+
const end = sectionEnd(lines, header);
|
|
86
|
+
const kept = lines.slice(header + 1, end).filter((line) => !CAPTURE_KEYS.has(RE_KEY_LINE.exec(line)?.[1] ?? ''));
|
|
87
|
+
const pairs = pairsOf(captureKeys(block));
|
|
88
|
+
const comments = kept.filter((line) => line.trim().startsWith('#'));
|
|
89
|
+
const empty = pairs.length === 0 && kept.every((line) => line.trim() === '' || line.trim().startsWith('#'));
|
|
90
|
+
// The table says nothing any more; a header left behind would be our leftover. A comment
|
|
91
|
+
// here stays: it may head the next table, as our `[otel]` marker does.
|
|
92
|
+
if (empty)
|
|
93
|
+
return [...lines.slice(0, lines[header - 1]?.trim() === CAPTURE_COMMENT ? header - 1 : header), ...comments, ...lines.slice(end)];
|
|
94
|
+
return [...lines.slice(0, header + 1), ...pairs, ...kept, ...lines.slice(end)];
|
|
95
|
+
}
|
|
96
|
+
/** Where the inline table closes: the first `}` that ends valid TOML, as a value or a comment can hold one too. */
|
|
97
|
+
function inlineTableEnd(line) {
|
|
98
|
+
for (let at = line.indexOf('}'); at !== -1; at = line.indexOf('}', at + 1)) {
|
|
99
|
+
if (tryParse(line.slice(0, at + 1)) !== undefined)
|
|
100
|
+
return at;
|
|
101
|
+
}
|
|
102
|
+
return line.length;
|
|
103
|
+
}
|
|
104
|
+
function withInlineKeys(lines, at, block) {
|
|
105
|
+
const line = lines[at];
|
|
106
|
+
const pairs = pairsOf(block);
|
|
107
|
+
const indent = RE_LEADING_SPACE.exec(line)?.[0] ?? '';
|
|
108
|
+
const comment = line.slice(inlineTableEnd(line) + 1);
|
|
109
|
+
return [...lines.slice(0, at), ...(pairs.length === 0 ? [] : [`${indent}${SET_KEY} = { ${pairs.join(', ')} }${comment}`]), ...lines.slice(at + 1)];
|
|
110
|
+
}
|
|
111
|
+
function appendedTable(raw, header, pairs) {
|
|
112
|
+
const body = [CAPTURE_COMMENT, header, ...pairs, ''].join('\n');
|
|
113
|
+
if (raw.trim() === '')
|
|
114
|
+
return body;
|
|
115
|
+
return `${raw}${raw.endsWith('\n') ? '' : '\n'}\n${body}`;
|
|
116
|
+
}
|
|
117
|
+
function appended(raw, block) {
|
|
118
|
+
const pairs = pairsOf(captureKeys(block));
|
|
119
|
+
return pairs.length === 0 ? raw : appendedTable(raw, SET_HEADER, pairs);
|
|
120
|
+
}
|
|
121
|
+
/** Everything but the two keys, for the check that a line edit changed nothing else. */
|
|
122
|
+
function withoutCaptureKeys(parsed) {
|
|
123
|
+
const policy = asRecord(parsed[POLICY_KEY]);
|
|
124
|
+
const set = asRecord(policy?.[SET_KEY]) ?? {};
|
|
125
|
+
const others = Object.fromEntries(Object.entries(set).filter(([key]) => !CAPTURE_KEYS.has(key)));
|
|
126
|
+
const nextPolicy = Object.fromEntries(Object.entries(policy ?? {}).filter(([key]) => key !== SET_KEY));
|
|
127
|
+
return { ...parsed, [POLICY_KEY]: { ...nextPolicy, others } };
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* The file with `set` holding the block's two capture keys, every other line as it was.
|
|
131
|
+
*
|
|
132
|
+
* Line edits, not a re-serialization: this file holds the developer's comments. No
|
|
133
|
+
* markers around them either, unlike the `[otel]` block: Codex writes its own tables
|
|
134
|
+
* inside a marker span when it re-serializes the file, and the two keys are found by
|
|
135
|
+
* name, so there is nothing a span would add.
|
|
136
|
+
*
|
|
137
|
+
* @param raw - The whole config.toml.
|
|
138
|
+
* @param parsed - The same, parsed.
|
|
139
|
+
* @param block - The `set` table as it should end up.
|
|
140
|
+
* @returns The next file; undefined when no line edit gives exactly that.
|
|
141
|
+
*/
|
|
142
|
+
export function withCaptureSet(raw, parsed, block) {
|
|
143
|
+
const lines = raw.split('\n');
|
|
144
|
+
const form = setForm(lines, parsed);
|
|
145
|
+
if (form.kind === 'unmergeable')
|
|
146
|
+
return undefined;
|
|
147
|
+
const next = form.kind === 'none'
|
|
148
|
+
? appended(raw, block)
|
|
149
|
+
: (form.kind === 'table' ? withTableKeys(lines, form.line, block) : withInlineKeys(lines, form.line, block)).join('\n');
|
|
150
|
+
const result = tryParse(next);
|
|
151
|
+
// Deep equality, as removing a table moves the keys after it: their order is not a change.
|
|
152
|
+
if (result === undefined || !isDeepStrictEqual(withoutCaptureKeys(result), withoutCaptureKeys(parsed)))
|
|
153
|
+
return undefined;
|
|
154
|
+
const set = asRecord(asRecord(result[POLICY_KEY])?.[SET_KEY]) ?? {};
|
|
155
|
+
return [...CAPTURE_KEYS].every((key) => set[key] === block[key]) ? next : undefined;
|
|
156
|
+
}
|
|
157
|
+
/** The folder a shim writes to, and the only part of the data dir the sandbox gets: the rest holds credentials. */
|
|
158
|
+
function spoolRoot(dataDir) {
|
|
159
|
+
return path.join(dataDir, SPOOL_DIR_NAME);
|
|
160
|
+
}
|
|
161
|
+
function stringsOf(value) {
|
|
162
|
+
return Array.isArray(value) ? value.filter((item) => typeof item === 'string') : [];
|
|
163
|
+
}
|
|
164
|
+
function rootsOf(parsed) {
|
|
165
|
+
return stringsOf(asRecord(parsed[SANDBOX_KEY])?.[ROOTS_KEY]);
|
|
166
|
+
}
|
|
167
|
+
/** The root as a TOML string. */
|
|
168
|
+
function rootLiteral(root) {
|
|
169
|
+
return stringifyToml({ [ROOTS_KEY]: [root] }).trim().slice(`${ROOTS_KEY} = [ `.length, -' ]'.length);
|
|
170
|
+
}
|
|
171
|
+
/** The config with our root gone, and an empty array or table counted as none, for the check that an edit changed nothing else. */
|
|
172
|
+
function withoutSpoolRoot(parsed, root) {
|
|
173
|
+
const table = asRecord(parsed[SANDBOX_KEY]);
|
|
174
|
+
if (table === undefined)
|
|
175
|
+
return { ...parsed };
|
|
176
|
+
const roots = (table[ROOTS_KEY] ?? []).filter((item) => item !== root);
|
|
177
|
+
const others = Object.fromEntries(Object.entries(table).filter(([key]) => key !== ROOTS_KEY));
|
|
178
|
+
const kept = roots.length > 0 ? { ...others, [ROOTS_KEY]: roots } : others;
|
|
179
|
+
const rest = Object.fromEntries(Object.entries(parsed).filter(([key]) => key !== SANDBOX_KEY));
|
|
180
|
+
return Object.keys(kept).length > 0 ? { ...rest, [SANDBOX_KEY]: kept } : rest;
|
|
181
|
+
}
|
|
182
|
+
/** The lines with our root first in the array, so a closing bracket or a comment after it never matters. */
|
|
183
|
+
function linesWithRoot(lines, header, literal) {
|
|
184
|
+
const end = sectionEnd(lines, header);
|
|
185
|
+
const at = lines.findIndex((line, index) => index > header && index < end && RE_ROOTS_LINE.test(line));
|
|
186
|
+
if (at === -1)
|
|
187
|
+
return [...lines.slice(0, header + 1), `${ROOTS_KEY} = [${literal}]`, ...lines.slice(header + 1)];
|
|
188
|
+
const line = lines[at];
|
|
189
|
+
const open = line.indexOf('[') + 1;
|
|
190
|
+
const rest = line.slice(open);
|
|
191
|
+
const comma = rest === '' ? ',' : ', ';
|
|
192
|
+
const separator = rest.trimStart().startsWith(']') ? '' : comma;
|
|
193
|
+
return [...lines.slice(0, at), `${line.slice(0, open)}${literal}${separator}${rest}`, ...lines.slice(at + 1)];
|
|
194
|
+
}
|
|
195
|
+
/** The array's text without our entry and its comma, an entry on its own line going with the line. */
|
|
196
|
+
function arrayWithoutRoot(text, literal) {
|
|
197
|
+
const at = text.indexOf(literal);
|
|
198
|
+
if (at === -1)
|
|
199
|
+
return undefined;
|
|
200
|
+
const before = text.slice(0, at);
|
|
201
|
+
const after = text.slice(at + literal.length);
|
|
202
|
+
const comma = RE_COMMA_AFTER.exec(after)?.[0];
|
|
203
|
+
const head = comma === undefined ? before.replace(RE_COMMA_BEFORE, '') : before;
|
|
204
|
+
const tail = after.slice(comma?.length ?? 0);
|
|
205
|
+
return RE_OWN_LINE_INDENT.test(head) && tail.startsWith('\n') ? `${head.replace(RE_OWN_LINE_INDENT, '')}${tail}` : `${head}${tail}`;
|
|
206
|
+
}
|
|
207
|
+
/** The lines without our root; the key goes once the array is empty, and the table with it only if our comment heads it. */
|
|
208
|
+
function linesWithoutRoot(lines, header, literal) {
|
|
209
|
+
const end = sectionEnd(lines, header);
|
|
210
|
+
const at = lines.findIndex((line, index) => index > header && index < end && RE_ROOTS_LINE.test(line));
|
|
211
|
+
if (at === -1)
|
|
212
|
+
return undefined;
|
|
213
|
+
let close = at;
|
|
214
|
+
while (close < end && tryParse(lines.slice(at, close + 1).join('\n')) === undefined)
|
|
215
|
+
close += 1;
|
|
216
|
+
const text = arrayWithoutRoot(lines.slice(at, close + 1).join('\n'), literal);
|
|
217
|
+
if (text === undefined)
|
|
218
|
+
return undefined;
|
|
219
|
+
const empty = !text.includes('#') && stringsOf(tryParse(text)?.[ROOTS_KEY]).length === 0;
|
|
220
|
+
const next = [...lines.slice(0, at), ...(empty ? [] : text.split('\n')), ...lines.slice(close + 1)];
|
|
221
|
+
const nextEnd = sectionEnd(next, header);
|
|
222
|
+
const body = next.slice(header + 1, nextEnd);
|
|
223
|
+
if (next[header - 1]?.trim() !== CAPTURE_COMMENT || !body.every((line) => line.trim() === '' || line.trim().startsWith('#')))
|
|
224
|
+
return next;
|
|
225
|
+
return [...next.slice(0, header - 1), ...body.filter((line) => line.trim() !== ''), ...next.slice(nextEnd)];
|
|
226
|
+
}
|
|
227
|
+
/**
|
|
228
|
+
* The file with the spool in `[sandbox_workspace_write] writable_roots` or out of it, every
|
|
229
|
+
* other line as it was. A line edit for the reason `withCaptureSet` gives, and refused by
|
|
230
|
+
* the same check when it would change anything else.
|
|
231
|
+
*
|
|
232
|
+
* Our table carries our comment; a table without it is the developer's and stays, even empty.
|
|
233
|
+
*
|
|
234
|
+
* @param raw - The whole config.toml.
|
|
235
|
+
* @param parsed - The same, parsed.
|
|
236
|
+
* @param root - The spool folder.
|
|
237
|
+
* @param wanted - Whether it should end up in the array.
|
|
238
|
+
* @returns The next file; undefined when no line edit gives exactly that.
|
|
239
|
+
*/
|
|
240
|
+
function withSpoolRoot(raw, parsed, root, wanted) {
|
|
241
|
+
if (rootsOf(parsed).includes(root) === wanted)
|
|
242
|
+
return raw;
|
|
243
|
+
const table = parsed[SANDBOX_KEY];
|
|
244
|
+
const roots = asRecord(table)?.[ROOTS_KEY];
|
|
245
|
+
if (roots !== undefined && !Array.isArray(roots))
|
|
246
|
+
return undefined;
|
|
247
|
+
const literal = rootLiteral(root);
|
|
248
|
+
const lines = raw.split('\n');
|
|
249
|
+
const header = lines.findIndex((line) => RE_SANDBOX_HEADER.test(line));
|
|
250
|
+
if (header === -1 && (table !== undefined || !wanted))
|
|
251
|
+
return undefined;
|
|
252
|
+
const edit = wanted ? linesWithRoot : linesWithoutRoot;
|
|
253
|
+
const edited = header === -1 ? appendedTable(raw, SANDBOX_HEADER, [`${ROOTS_KEY} = [${literal}]`]) : edit(lines, header, literal)?.join('\n');
|
|
254
|
+
const result = edited === undefined ? undefined : tryParse(edited);
|
|
255
|
+
if (result === undefined || !isDeepStrictEqual(withoutSpoolRoot(result, root), withoutSpoolRoot(parsed, root)))
|
|
256
|
+
return undefined;
|
|
257
|
+
return rootsOf(result).includes(root) === wanted ? edited : undefined;
|
|
258
|
+
}
|
|
259
|
+
function setBlock(parsed) {
|
|
260
|
+
return asRecord(asRecord(parsed[POLICY_KEY])?.[SET_KEY]) ?? {};
|
|
261
|
+
}
|
|
262
|
+
async function writeConfig(file, raw, next, context) {
|
|
263
|
+
if (next === (raw ?? ''))
|
|
264
|
+
return false;
|
|
265
|
+
await backupFile(file, context.paths.backupsDir, context.env.now());
|
|
266
|
+
if (raw === undefined) {
|
|
267
|
+
await writeFileAtomic(file, next);
|
|
268
|
+
return true;
|
|
269
|
+
}
|
|
270
|
+
// Its own mode, so a file holding the exporter's bearer stays 0600. Through a link, as only
|
|
271
|
+
// a removal reaches one (`applyCodexCapture`). Codex rewrites this file as it runs: a change
|
|
272
|
+
// since our read is Codex's, and it wins until the next setup.
|
|
273
|
+
const target = await fs.realpath(file).catch(() => file);
|
|
274
|
+
return writeIfUnchanged(target, raw, next, (await currentMode(target)) ?? SECRET_FILE_MODE);
|
|
275
|
+
}
|
|
276
|
+
/**
|
|
277
|
+
* Merge the two keys into `[shell_environment_policy] set`. The block's value is the
|
|
278
|
+
* developer's; `set` replaces what the shell has, so the ambient one moves in too.
|
|
279
|
+
*
|
|
280
|
+
* ponytail: uninstall cannot tell an ambient value copied here from one the developer
|
|
281
|
+
* wrote, so it stays in `set` and keeps masking the shell's, as in Claude Code's env block.
|
|
282
|
+
* The upgrade is recording what setup copied.
|
|
283
|
+
*
|
|
284
|
+
* A file Edge cannot edit is said, not failed: capture is optional beside the export.
|
|
285
|
+
*
|
|
286
|
+
* @param context - Environment, paths and config.
|
|
287
|
+
* @param source - The package's shims.
|
|
288
|
+
* @returns Whether the file changed, and what to tell the developer.
|
|
289
|
+
*/
|
|
290
|
+
export async function applyCodexCapture(context, source = shimPaths()) {
|
|
291
|
+
const file = codexConfigTomlPath(context.env);
|
|
292
|
+
const raw = await readConfig(file);
|
|
293
|
+
if (raw === null)
|
|
294
|
+
return { ok: true, changed: false, messages: [`run capture skipped: ${file} is unreadable`] };
|
|
295
|
+
if (await isLinkedConfig(file))
|
|
296
|
+
return { ok: true, changed: false, messages: [`run capture skipped: ${linkedConfigReason(file)}`] };
|
|
297
|
+
const parsed = tryParse(raw ?? '');
|
|
298
|
+
if (parsed === undefined)
|
|
299
|
+
return { ok: true, changed: false, messages: [`run capture skipped: ${file} is unparseable`] };
|
|
300
|
+
const block = await installedCaptureEnv(setBlock(parsed), context.env.vars, context.paths.dataDir, source);
|
|
301
|
+
const next = withCaptureSet(raw ?? '', parsed, block);
|
|
302
|
+
if (next === undefined)
|
|
303
|
+
return { ok: true, changed: false, messages: [unmergeable(file)] };
|
|
304
|
+
// A permissions profile replaces this table, and Codex's reference says not to combine them.
|
|
305
|
+
const rooted = parsed[PROFILE_KEY] === undefined ? withSpoolRoot(next, tryParse(next), spoolRoot(context.paths.dataDir), true) : next;
|
|
306
|
+
const changed = await writeConfig(file, raw, rooted ?? next, context);
|
|
307
|
+
return {
|
|
308
|
+
ok: true,
|
|
309
|
+
changed,
|
|
310
|
+
messages: [...(changed ? ['run capture configured; restart running Codex sessions to pick it up'] : []), ...(rooted === undefined ? [unmergeableRoots(file)] : [])]
|
|
311
|
+
};
|
|
312
|
+
}
|
|
313
|
+
/**
|
|
314
|
+
* Take our part of the two keys out, then the shared copy once nothing else needs it.
|
|
315
|
+
*
|
|
316
|
+
* Fails only when our keys stay behind: a file holding nothing of ours, in whatever form,
|
|
317
|
+
* is not this step's to complain about.
|
|
318
|
+
*
|
|
319
|
+
* @param context - Environment and paths.
|
|
320
|
+
* @returns Whether the file changed, and what to tell the developer.
|
|
321
|
+
*/
|
|
322
|
+
export async function removeCodexCapture(context) {
|
|
323
|
+
const file = codexConfigTomlPath(context.env);
|
|
324
|
+
const raw = await readConfig(file);
|
|
325
|
+
const copy = copiedShims(context.paths.dataDir);
|
|
326
|
+
const parsed = raw ? tryParse(raw) : {};
|
|
327
|
+
const root = spoolRoot(context.paths.dataDir);
|
|
328
|
+
if (raw === null || (parsed === undefined && raw !== undefined && (raw.includes(copy.node) || raw.includes(copy.python) || raw.includes(root))))
|
|
329
|
+
return { ok: false, changed: false, messages: [`run capture: cannot read ${file}; take NODE_OPTIONS and PYTHONPATH out of ${POLICY_KEY}.${SET_KEY}, and ${root} out of ${SANDBOX_KEY}.${ROOTS_KEY}, by hand`] };
|
|
330
|
+
const block = setBlock(parsed ?? {});
|
|
331
|
+
const kept = withoutCaptureEnv(block, copy);
|
|
332
|
+
if (parsed === undefined || ([...CAPTURE_KEYS].every((key) => kept[key] === block[key]) && !rootsOf(parsed).includes(root))) {
|
|
333
|
+
await removeCaptureCopy(context.paths.dataDir, context.env);
|
|
334
|
+
return { ok: true, changed: false, messages: [] };
|
|
335
|
+
}
|
|
336
|
+
const unset = [...CAPTURE_KEYS].every((key) => kept[key] === block[key]) ? (raw ?? '') : withCaptureSet(raw ?? '', parsed, kept);
|
|
337
|
+
if (unset === undefined)
|
|
338
|
+
return { ok: false, changed: false, messages: [unmergeable(file)] };
|
|
339
|
+
const next = withSpoolRoot(unset, tryParse(unset), root, false);
|
|
340
|
+
const changed = await writeConfig(file, raw, next ?? unset, context);
|
|
341
|
+
await removeCaptureCopy(context.paths.dataDir, context.env);
|
|
342
|
+
if (!changed && (next ?? unset) !== raw)
|
|
343
|
+
return { ok: false, changed: false, messages: [`run capture: Codex changed ${file} during this; run the command again`] };
|
|
344
|
+
if (next === undefined)
|
|
345
|
+
return { ok: false, changed, messages: [unmergeableRoots(file)] };
|
|
346
|
+
return { ok: true, changed, messages: changed ? ['run capture configuration removed'] : [] };
|
|
347
|
+
}
|
|
348
|
+
function unmergeable(file) {
|
|
349
|
+
return `run capture: ${file} writes ${POLICY_KEY}.${SET_KEY} in a form Edge cannot edit; move it under ${SET_HEADER}`;
|
|
350
|
+
}
|
|
351
|
+
function unmergeableRoots(file) {
|
|
352
|
+
return `run capture: ${file} writes ${SANDBOX_KEY}.${ROOTS_KEY} in a form Edge cannot edit; move it under ${SANDBOX_HEADER}`;
|
|
353
|
+
}
|
|
354
|
+
/** Codex's patterns are case-insensitive, with `*` and `?`. */
|
|
355
|
+
function matches(pattern, key) {
|
|
356
|
+
const source = pattern.replace(RE_WILDCARD_LITERAL, '\\$&').replace(RE_WILDCARD_ANY, '.*').replace(RE_WILDCARD_ONE, '.');
|
|
357
|
+
return new RegExp(`^${source}$`, 'i').test(key);
|
|
358
|
+
}
|
|
359
|
+
function patternsNamed(filters, verdict) {
|
|
360
|
+
return Object.keys(filters).filter((pattern) => filters[pattern] === verdict);
|
|
361
|
+
}
|
|
362
|
+
/**
|
|
363
|
+
* The capture keys a `shell_environment_policy` stops, and the ones it only names.
|
|
364
|
+
*
|
|
365
|
+
* Codex's config reference (2026-11-02): `set` values are injected after exclusions, and
|
|
366
|
+
* include filters can still remove them. So an allowlist that misses a key strips it. An
|
|
367
|
+
* exclude that names one should not, per the reference, but a developer who wrote it
|
|
368
|
+
* probably expects it to, so it is said rather than called off.
|
|
369
|
+
*
|
|
370
|
+
* ponytail: only the user's config.toml is read. A project `.codex/config.toml` or a
|
|
371
|
+
* profile can filter too; the upgrade is the layer stack Codex itself merges.
|
|
372
|
+
*
|
|
373
|
+
* @param policy - The table as parsed.
|
|
374
|
+
* @returns The keys an allowlist strips and the keys an exclude names.
|
|
375
|
+
*/
|
|
376
|
+
export function filteredCaptureKeys(policy) {
|
|
377
|
+
const filters = asRecord(policy['filters']) ?? {};
|
|
378
|
+
const includes = [...stringsOf(policy['include_only']), ...patternsNamed(filters, 'include')];
|
|
379
|
+
const excludes = [...stringsOf(policy['exclude']), ...patternsNamed(filters, 'exclude')];
|
|
380
|
+
const keys = [...CAPTURE_KEYS];
|
|
381
|
+
return {
|
|
382
|
+
stripped: includes.length === 0 ? [] : keys.filter((key) => !includes.some((pattern) => matches(pattern, key))),
|
|
383
|
+
excluded: keys.filter((key) => excludes.some((pattern) => matches(pattern, key)))
|
|
384
|
+
};
|
|
385
|
+
}
|
|
386
|
+
/**
|
|
387
|
+
* Whether what Codex's shell tool starts loads the shims, and when not, why.
|
|
388
|
+
*
|
|
389
|
+
* Gated on `otel` as Claude Code's is, not on the tool-content consent that holds Codex's
|
|
390
|
+
* own export back: a captured call is usage only, it carries no tool content.
|
|
391
|
+
*
|
|
392
|
+
* @param context - Environment, paths and config.
|
|
393
|
+
* @param source - The package's shims.
|
|
394
|
+
* @returns On, or off with the reason.
|
|
395
|
+
*/
|
|
396
|
+
export async function inspectCodexCapture(context, source = shimPaths()) {
|
|
397
|
+
const file = codexConfigTomlPath(context.env);
|
|
398
|
+
const raw = await readConfig(file);
|
|
399
|
+
const parsed = raw ? tryParse(raw) : {};
|
|
400
|
+
if (raw === null || parsed === undefined)
|
|
401
|
+
return { on: false, reason: `${file} cannot be read as TOML` };
|
|
402
|
+
const status = await inspectCaptureEnv(setBlock(parsed), context.config, otelEnabled(context.config), context.paths.dataDir, source);
|
|
403
|
+
if (!status.on && (await isLinkedConfig(file)))
|
|
404
|
+
return { on: false, reason: linkedConfigReason(file) };
|
|
405
|
+
if (!status.on)
|
|
406
|
+
return setForm((raw ?? '').split('\n'), parsed).kind === 'unmergeable' ? { on: false, reason: unmergeable(file) } : status;
|
|
407
|
+
const { stripped, excluded } = filteredCaptureKeys(asRecord(parsed[POLICY_KEY]) ?? {});
|
|
408
|
+
if (stripped.length > 0)
|
|
409
|
+
return { on: false, reason: `${POLICY_KEY} in ${file} allows only what it lists, which strips ${stripped.join(', ')}; add them to its include filters` };
|
|
410
|
+
const root = spoolRoot(context.paths.dataDir);
|
|
411
|
+
const reaches = rootsOf(parsed).some((entry) => root === entry || root.startsWith(`${entry}/`));
|
|
412
|
+
if (parsed[PROFILE_KEY] === undefined && !reaches)
|
|
413
|
+
return { on: false, reason: await missingRootReason(file, raw ?? '', parsed, root) };
|
|
414
|
+
const warnings = [
|
|
415
|
+
...(status.warning ? [status.warning] : []),
|
|
416
|
+
...(excluded.length > 0 ? [`${POLICY_KEY} in ${file} excludes ${excluded.join(', ')}; Codex documents that \`${SET_KEY}\` survives that, so capture stays on unless you see otherwise`] : []),
|
|
417
|
+
...(parsed[PROFILE_KEY] === undefined && (parsed['sandbox_mode'] === undefined || parsed['sandbox_mode'] === 'workspace-write') ? [sandboxLimits()] : []),
|
|
418
|
+
...(parsed['sandbox_mode'] === 'read-only' ? [`sandbox_mode = "read-only" in ${file} denies a sandboxed command's write to ${root}, so what it runs is not counted`] : []),
|
|
419
|
+
...(typeof parsed[PROFILE_KEY] === 'string' && parsed[PROFILE_KEY] !== ':danger-full-access' ? [`${PROFILE_KEY} in ${file} selects a permissions profile, which Edge does not edit; what a sandboxed command runs is counted only if that profile lets it write to ${root}`] : [])
|
|
420
|
+
];
|
|
421
|
+
return warnings.length > 0 ? { on: true, warning: warnings.join('; ') } : { on: true };
|
|
422
|
+
}
|
|
423
|
+
/**
|
|
424
|
+
* What the spool root does not settle in Codex's workspace-write sandbox, also Codex's mode
|
|
425
|
+
* when the config names none: a call needs the network, and its record a session.
|
|
426
|
+
*
|
|
427
|
+
* ponytail: said on every platform, though `ps` was only seen denied on macOS. The upgrade
|
|
428
|
+
* is attribution that works inside the sandbox, which takes this away.
|
|
429
|
+
*
|
|
430
|
+
* @returns The sentence for status and doctor.
|
|
431
|
+
*/
|
|
432
|
+
function sandboxLimits() {
|
|
433
|
+
return 'inside Codex\'s workspace-write sandbox a script\'s call is not counted yet: the network is off unless network_access = true, and attribution is not supported there, as the sandbox denies `ps`; what runs under sandbox_mode = "danger-full-access" or as an escalated command is counted';
|
|
434
|
+
}
|
|
435
|
+
/**
|
|
436
|
+
* Why a sandboxed command cannot reach the spool, and what fixes it. The shim swallows the
|
|
437
|
+
* failed write, so without this `status` would say on while nothing is counted.
|
|
438
|
+
*
|
|
439
|
+
* ponytail: reads config.toml only, so `codex --sandbox`, `codex exec`'s read-only default
|
|
440
|
+
* and a profile are not seen. The upgrade is the layer stack Codex itself merges.
|
|
441
|
+
*
|
|
442
|
+
* @param file - The config file.
|
|
443
|
+
* @param raw - Its text.
|
|
444
|
+
* @param parsed - The same, parsed.
|
|
445
|
+
* @param root - The spool folder.
|
|
446
|
+
* @returns The sentence for status and doctor.
|
|
447
|
+
*/
|
|
448
|
+
async function missingRootReason(file, raw, parsed, root) {
|
|
449
|
+
if (await isLinkedConfig(file))
|
|
450
|
+
return linkedConfigReason(file);
|
|
451
|
+
if (withSpoolRoot(raw, parsed, root, true) === undefined)
|
|
452
|
+
return unmergeableRoots(file);
|
|
453
|
+
return `Codex's sandbox denies a sandboxed command's write to ${root}, so what it runs is not counted; run \`agentwatch setup\` to add it to ${SANDBOX_KEY}.${ROOTS_KEY}`;
|
|
454
|
+
}
|
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
import fs from 'node:fs';
|
|
2
2
|
import path from 'node:path';
|
|
3
3
|
import { findExecutable } from '../../core/which.js';
|
|
4
|
-
import {
|
|
5
|
-
import { HOOK_COMMAND_MARKER } from '../constants/provider.constants.js';
|
|
4
|
+
import { hooksMarkerPresent } from '../shared/hooks-marker.js';
|
|
6
5
|
import { CODEX_CONFIG_FILE, CODEX_EXECUTABLE, CODEX_HOME_DIR, CODEX_HOME_VAR, CODEX_HOOKS_FILE } from './constants/codex.constants.js';
|
|
7
6
|
/**
|
|
8
7
|
* Codex's configuration root.
|
|
@@ -47,12 +46,11 @@ export async function detectCodex(env) {
|
|
|
47
46
|
if (executablePath)
|
|
48
47
|
evidence.push(`codex executable on PATH (${executablePath})`);
|
|
49
48
|
const hookConfigPath = codexHooksJsonPath(env);
|
|
50
|
-
const hooksFile = await readJsonFile(hookConfigPath);
|
|
51
49
|
return {
|
|
52
50
|
detected: evidence.length > 0,
|
|
53
51
|
evidence,
|
|
54
52
|
executablePath,
|
|
55
53
|
hookConfigPath,
|
|
56
|
-
hooksInstalled:
|
|
54
|
+
hooksInstalled: await hooksMarkerPresent(hookConfigPath)
|
|
57
55
|
};
|
|
58
56
|
}
|