@agent-watch-ai/edge 0.2.5 → 0.4.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 +558 -0
- package/README.md +222 -97
- package/SECURITY.md +72 -0
- package/dist/billing/session-billing.d.ts +19 -0
- package/dist/billing/session-billing.js +52 -0
- package/dist/cli/args.d.ts +10 -0
- package/dist/cli/args.js +20 -0
- package/dist/cli/constants/cli.constants.d.ts +52 -4
- package/dist/cli/constants/cli.constants.js +56 -5
- package/dist/cli/context.d.ts +33 -2
- package/dist/cli/context.js +84 -14
- package/dist/cli/doctor.js +321 -33
- package/dist/cli/hook-check.d.ts +23 -0
- package/dist/cli/hook-check.js +114 -0
- package/dist/cli/hook.js +118 -7
- package/dist/cli/index.d.ts +3 -1
- package/dist/cli/index.js +3 -1
- package/dist/cli/misc.d.ts +11 -0
- package/dist/cli/misc.js +80 -4
- package/dist/cli/npx-install.d.ts +37 -0
- package/dist/cli/npx-install.js +79 -0
- package/dist/cli/open-browser.d.ts +39 -0
- package/dist/cli/open-browser.js +62 -0
- package/dist/cli/setup.d.ts +17 -8
- package/dist/cli/setup.js +553 -103
- package/dist/cli/status.js +82 -13
- package/dist/cli/toggle.d.ts +9 -0
- package/dist/cli/toggle.js +117 -0
- package/dist/cli/types/cli.types.d.ts +43 -3
- package/dist/cli/ui.d.ts +7 -0
- package/dist/cli/ui.js +10 -1
- package/dist/cli/uninstall.js +39 -2
- package/dist/cli/update.d.ts +17 -0
- package/dist/cli/update.js +76 -0
- package/dist/cli.js +110 -12
- package/dist/config/config-store.d.ts +57 -0
- package/dist/config/config-store.js +250 -12
- package/dist/config/config.d.ts +53 -6
- package/dist/config/config.js +66 -6
- package/dist/config/constants/config.constants.d.ts +116 -6
- package/dist/config/constants/config.constants.js +131 -8
- package/dist/config/destination.d.ts +24 -0
- package/dist/config/destination.js +33 -0
- package/dist/config/index.d.ts +7 -4
- package/dist/config/index.js +5 -3
- package/dist/config/repo-config.d.ts +11 -7
- package/dist/config/repo-config.js +64 -15
- package/dist/config/root-config.d.ts +72 -0
- package/dist/config/root-config.js +170 -0
- package/dist/config/schemas/config.schema.d.ts +831 -84
- package/dist/config/schemas/config.schema.js +398 -16
- package/dist/config/types/config.types.d.ts +15 -1
- package/dist/config/types/destination.types.d.ts +8 -0
- package/dist/core/async.d.ts +36 -0
- package/dist/core/async.js +68 -0
- package/dist/core/index.d.ts +1 -1
- package/dist/core/index.js +1 -1
- package/dist/core/object.d.ts +5 -0
- package/dist/core/object.js +9 -4
- package/dist/core/pipe.d.ts +0 -13
- package/dist/core/pipe.js +1 -9
- package/dist/core/types/core.types.d.ts +9 -0
- package/dist/core/types/core.types.js +0 -3
- package/dist/core/which.d.ts +14 -0
- package/dist/core/which.js +43 -3
- package/dist/credentials/budget.d.ts +9 -0
- package/dist/credentials/budget.js +16 -0
- package/dist/credentials/command-runner.d.ts +16 -0
- package/dist/credentials/command-runner.js +37 -0
- package/dist/credentials/constants/credentials.constants.d.ts +119 -0
- package/dist/credentials/constants/credentials.constants.js +120 -0
- package/dist/credentials/credential-status.d.ts +14 -0
- package/dist/credentials/credential-status.js +38 -0
- package/dist/credentials/credentials.d.ts +86 -0
- package/dist/credentials/credentials.js +278 -0
- package/dist/credentials/device-api.d.ts +73 -0
- package/dist/credentials/device-api.js +161 -0
- package/dist/credentials/device-state.d.ts +31 -0
- package/dist/credentials/device-state.js +51 -0
- package/dist/credentials/identity.d.ts +73 -0
- package/dist/credentials/identity.js +101 -0
- package/dist/credentials/index.d.ts +7 -0
- package/dist/credentials/index.js +5 -0
- package/dist/credentials/migration.d.ts +46 -0
- package/dist/credentials/migration.js +164 -0
- package/dist/credentials/schemas/device-api.schema.d.ts +77 -0
- package/dist/credentials/schemas/device-api.schema.js +33 -0
- package/dist/credentials/schemas/device-state.schema.d.ts +51 -0
- package/dist/credentials/schemas/device-state.schema.js +20 -0
- package/dist/credentials/secret-store.d.ts +54 -0
- package/dist/credentials/secret-store.js +217 -0
- package/dist/credentials/sign-in.d.ts +31 -0
- package/dist/credentials/sign-in.js +105 -0
- package/dist/credentials/token-refresher.d.ts +39 -0
- package/dist/credentials/token-refresher.js +185 -0
- package/dist/credentials/types/credentials.types.d.ts +206 -0
- package/dist/credentials/types/credentials.types.js +1 -0
- package/dist/enforcement/constants/enforcement.constants.d.ts +47 -1
- package/dist/enforcement/constants/enforcement.constants.js +56 -0
- package/dist/enforcement/decision-cache.d.ts +20 -1
- package/dist/enforcement/decision-cache.js +65 -2
- package/dist/enforcement/decision-client.d.ts +9 -8
- package/dist/enforcement/decision-client.js +64 -17
- package/dist/enforcement/enforcement.d.ts +23 -2
- package/dist/enforcement/enforcement.js +107 -17
- package/dist/enforcement/index.d.ts +4 -3
- package/dist/enforcement/index.js +3 -2
- package/dist/enforcement/types/enforcement.types.d.ts +66 -2
- package/dist/enrollment/constants/enrollment.constants.d.ts +2 -6
- package/dist/enrollment/constants/enrollment.constants.js +2 -6
- package/dist/enrollment/enrollment.d.ts +16 -0
- package/dist/enrollment/enrollment.js +54 -0
- package/dist/enrollment/index.d.ts +3 -7
- package/dist/enrollment/index.js +1 -1
- package/dist/enrollment/types/enrollment.types.d.ts +9 -13
- package/dist/events/constants/enrich.constants.d.ts +15 -0
- package/dist/events/constants/enrich.constants.js +15 -0
- package/dist/events/constants/events.constants.d.ts +6 -2
- package/dist/events/constants/events.constants.js +5 -1
- package/dist/events/enrich.js +92 -10
- package/dist/events/event-id.d.ts +0 -8
- package/dist/events/event-id.js +0 -10
- package/dist/events/index.d.ts +2 -2
- package/dist/events/index.js +1 -1
- package/dist/events/product-event.d.ts +9 -6
- package/dist/events/types/repo-snapshot.types.d.ts +7 -0
- package/dist/events/types/session-billing.types.d.ts +16 -0
- package/dist/events/types/session-billing.types.js +1 -0
- package/dist/feature/constants/feature.constants.d.ts +7 -0
- package/dist/feature/constants/feature.constants.js +7 -0
- package/dist/feature/index.d.ts +1 -1
- package/dist/feature/index.js +1 -1
- package/dist/feature/ticket-candidates.d.ts +14 -0
- package/dist/feature/ticket-candidates.js +26 -8
- package/dist/git/blob-hash.d.ts +21 -0
- package/dist/git/blob-hash.js +69 -0
- package/dist/git/checkout-root.d.ts +27 -0
- package/dist/git/checkout-root.js +75 -0
- package/dist/git/constants/git.constants.d.ts +160 -0
- package/dist/git/constants/git.constants.js +181 -0
- package/dist/git/fingerprint.d.ts +74 -0
- package/dist/git/fingerprint.js +219 -0
- package/dist/git/gate-checkout.d.ts +44 -0
- package/dist/git/gate-checkout.js +201 -0
- package/dist/git/git-context.d.ts +45 -0
- package/dist/git/git-context.js +61 -3
- package/dist/git/index.d.ts +4 -2
- package/dist/git/index.js +2 -1
- package/dist/git/manifest-repository.d.ts +13 -0
- package/dist/git/manifest-repository.js +92 -0
- package/dist/git/repo-snapshot.d.ts +29 -3
- package/dist/git/repo-snapshot.js +110 -7
- package/dist/git/repository-root.d.ts +39 -0
- package/dist/git/repository-root.js +131 -0
- package/dist/git/types/git.types.d.ts +55 -0
- package/dist/git/types/repository-root.types.d.ts +11 -0
- package/dist/git/types/repository-root.types.js +1 -0
- package/dist/git/types/snapshot.types.d.ts +9 -0
- package/dist/pipeline/constants/pipeline.constants.d.ts +10 -0
- package/dist/pipeline/constants/pipeline.constants.js +10 -0
- package/dist/pipeline/hook-pipeline.js +410 -33
- package/dist/pipeline/types/pipeline.types.d.ts +39 -4
- package/dist/privacy/constants/privacy.constants.js +15 -1
- package/dist/privacy/index.d.ts +1 -0
- package/dist/privacy/index.js +1 -0
- package/dist/privacy/product-capture.d.ts +12 -0
- package/dist/privacy/product-capture.js +75 -0
- package/dist/privacy/sanitizer.d.ts +21 -3
- package/dist/privacy/sanitizer.js +52 -8
- package/dist/process/ancestors.d.ts +39 -0
- package/dist/process/ancestors.js +121 -0
- package/dist/process/constants/process.constants.d.ts +34 -0
- package/dist/process/constants/process.constants.js +34 -0
- package/dist/process/index.d.ts +7 -0
- package/dist/process/index.js +2 -0
- package/dist/process/types/process.types.d.ts +23 -0
- package/dist/process/types/process.types.js +1 -0
- package/dist/providers/antigravity/antigravity.adapter.d.ts +12 -1
- package/dist/providers/antigravity/antigravity.adapter.js +21 -3
- package/dist/providers/antigravity/antigravity.detect.d.ts +0 -9
- package/dist/providers/antigravity/antigravity.detect.js +1 -1
- package/dist/providers/antigravity/antigravity.provider.js +3 -2
- package/dist/providers/antigravity/constants/antigravity.constants.d.ts +2 -0
- package/dist/providers/antigravity/constants/antigravity.constants.js +2 -0
- package/dist/providers/claude/claude.adapter.d.ts +11 -1
- package/dist/providers/claude/claude.adapter.js +26 -5
- package/dist/providers/claude/claude.otel.d.ts +32 -18
- package/dist/providers/claude/claude.otel.js +81 -6
- package/dist/providers/claude/claude.provider.js +5 -2
- package/dist/providers/claude/constants/claude.constants.d.ts +2 -0
- package/dist/providers/claude/constants/claude.constants.js +2 -0
- package/dist/providers/claude/constants/claude.otel.constants.d.ts +4 -0
- package/dist/providers/claude/constants/claude.otel.constants.js +8 -0
- package/dist/providers/codex/codex.adapter.d.ts +15 -1
- package/dist/providers/codex/codex.adapter.js +29 -5
- package/dist/providers/codex/codex.otel.d.ts +36 -3
- package/dist/providers/codex/codex.otel.js +199 -32
- package/dist/providers/codex/codex.provider.js +5 -2
- package/dist/providers/codex/constants/codex.constants.d.ts +2 -0
- package/dist/providers/codex/constants/codex.constants.js +16 -0
- package/dist/providers/codex/constants/codex.otel.constants.d.ts +10 -0
- package/dist/providers/codex/constants/codex.otel.constants.js +10 -0
- package/dist/providers/constants/provider.constants.d.ts +8 -3
- package/dist/providers/constants/provider.constants.js +8 -3
- package/dist/providers/cursor/constants/cursor.constants.d.ts +9 -0
- package/dist/providers/cursor/constants/cursor.constants.js +9 -0
- package/dist/providers/cursor/cursor.adapter.d.ts +33 -1
- package/dist/providers/cursor/cursor.adapter.js +129 -4
- package/dist/providers/cursor/cursor.detect.d.ts +0 -7
- package/dist/providers/cursor/cursor.detect.js +1 -1
- package/dist/providers/cursor/cursor.provider.js +5 -2
- package/dist/providers/gemini/constants/gemini.constants.d.ts +9 -0
- package/dist/providers/gemini/constants/gemini.constants.js +9 -0
- package/dist/providers/gemini/constants/gemini.otel.constants.d.ts +9 -1
- package/dist/providers/gemini/constants/gemini.otel.constants.js +9 -0
- package/dist/providers/gemini/gemini.adapter.d.ts +14 -1
- package/dist/providers/gemini/gemini.adapter.js +25 -5
- package/dist/providers/gemini/gemini.detect.d.ts +0 -7
- package/dist/providers/gemini/gemini.detect.js +1 -1
- package/dist/providers/gemini/gemini.otel.d.ts +30 -21
- package/dist/providers/gemini/gemini.otel.js +166 -22
- package/dist/providers/gemini/gemini.provider.js +2 -1
- package/dist/providers/index.d.ts +1 -1
- package/dist/providers/index.js +1 -1
- package/dist/providers/loaders.d.ts +10 -0
- package/dist/providers/loaders.js +40 -0
- package/dist/providers/provider.d.ts +11 -0
- package/dist/providers/provider.js +1 -1
- package/dist/providers/registry.d.ts +11 -8
- package/dist/providers/registry.js +11 -15
- package/dist/providers/shared/bearer-rewrite.d.ts +19 -0
- package/dist/providers/shared/bearer-rewrite.js +46 -0
- package/dist/providers/shared/constants/hook-refusal.constants.d.ts +7 -0
- package/dist/providers/shared/constants/hook-refusal.constants.js +7 -0
- package/dist/providers/shared/constants/shell-directories.constants.d.ts +30 -0
- package/dist/providers/shared/constants/shell-directories.constants.js +39 -0
- package/dist/providers/shared/constants/tooling.constants.d.ts +24 -2
- package/dist/providers/shared/constants/tooling.constants.js +25 -3
- package/dist/providers/shared/event-builder.d.ts +12 -14
- package/dist/providers/shared/event-builder.js +20 -14
- package/dist/providers/shared/exporter-allowlist.d.ts +20 -0
- package/dist/providers/shared/exporter-allowlist.js +49 -0
- package/dist/providers/shared/hook-config.d.ts +2 -21
- package/dist/providers/shared/hook-config.js +5 -4
- package/dist/providers/shared/hook-refusal.d.ts +12 -0
- package/dist/providers/shared/hook-refusal.js +17 -1
- package/dist/providers/shared/install-record.d.ts +12 -2
- package/dist/providers/shared/install-record.js +5 -2
- package/dist/providers/shared/shell-directories.d.ts +24 -0
- package/dist/providers/shared/shell-directories.js +90 -0
- package/dist/providers/shared/tooling.d.ts +40 -1
- package/dist/providers/shared/tooling.js +70 -1
- package/dist/providers/shared/types/adapter.types.d.ts +1 -2
- package/dist/providers/types/provider.types.d.ts +81 -1
- package/dist/snapshot/constants/snapshot.constants.d.ts +32 -0
- package/dist/snapshot/constants/snapshot.constants.js +33 -0
- package/dist/snapshot/snapshot-event.js +52 -11
- package/dist/snapshot/snapshot-pipeline.js +1 -1
- package/dist/snapshot/types/snapshot.types.d.ts +2 -0
- package/dist/storage/atomic-file.d.ts +21 -0
- package/dist/storage/atomic-file.js +20 -1
- package/dist/storage/constants/storage.constants.d.ts +19 -0
- package/dist/storage/constants/storage.constants.js +19 -0
- package/dist/storage/disabled.d.ts +13 -0
- package/dist/storage/disabled.js +24 -0
- package/dist/storage/index.d.ts +2 -0
- package/dist/storage/index.js +2 -0
- package/dist/storage/paths.js +3 -2
- package/dist/storage/schemas/storage.schema.d.ts +72 -0
- package/dist/storage/schemas/storage.schema.js +6 -0
- package/dist/storage/sweep-marker.d.ts +19 -0
- package/dist/storage/sweep-marker.js +55 -0
- package/dist/storage/types/storage.types.d.ts +2 -0
- package/dist/storage/uninstalled.d.ts +58 -0
- package/dist/storage/uninstalled.js +107 -0
- package/dist/transport/auth-block.d.ts +66 -0
- package/dist/transport/auth-block.js +129 -0
- package/dist/transport/constants/transport.constants.d.ts +82 -3
- package/dist/transport/constants/transport.constants.js +83 -4
- package/dist/transport/cooldown.js +8 -1
- package/dist/transport/delivery.d.ts +14 -6
- package/dist/transport/delivery.js +80 -55
- package/dist/transport/http-transport.d.ts +1 -0
- package/dist/transport/http-transport.js +52 -10
- package/dist/transport/index.d.ts +6 -3
- package/dist/transport/index.js +3 -0
- package/dist/transport/queue-partition.d.ts +91 -0
- package/dist/transport/queue-partition.js +143 -0
- package/dist/transport/queue.d.ts +99 -20
- package/dist/transport/queue.js +210 -69
- package/dist/transport/response-body.d.ts +25 -0
- package/dist/transport/response-body.js +64 -0
- package/dist/transport/send.d.ts +11 -0
- package/dist/transport/send.js +17 -0
- package/dist/transport/types/delivery-flow.types.d.ts +19 -0
- package/dist/transport/types/delivery-flow.types.js +1 -0
- package/dist/transport/types/transport.types.d.ts +45 -0
- package/dist/turns/claude-transcript.d.ts +19 -1
- package/dist/turns/claude-transcript.js +46 -1
- package/dist/turns/codex-originator.d.ts +11 -0
- package/dist/turns/codex-originator.js +31 -0
- package/dist/turns/codex-transcript.d.ts +30 -0
- package/dist/turns/codex-transcript.js +88 -0
- package/dist/turns/constants/turns.constants.d.ts +141 -5
- package/dist/turns/constants/turns.constants.js +155 -5
- package/dist/turns/file-identity.d.ts +27 -0
- package/dist/turns/file-identity.js +48 -0
- package/dist/turns/gemini-transcript.d.ts +30 -0
- package/dist/turns/gemini-transcript.js +96 -0
- package/dist/turns/index.d.ts +3 -3
- package/dist/turns/index.js +1 -1
- package/dist/turns/inherited-checkout.d.ts +54 -0
- package/dist/turns/inherited-checkout.js +155 -0
- package/dist/turns/native-session-titles.d.ts +26 -0
- package/dist/turns/native-session-titles.js +102 -0
- package/dist/turns/transcript.d.ts +3 -2
- package/dist/turns/transcript.js +5 -1
- package/dist/turns/turn-state.d.ts +224 -3
- package/dist/turns/turn-state.js +456 -21
- package/dist/turns/turn-summary.d.ts +1 -14
- package/dist/turns/turn-summary.js +87 -51
- package/dist/turns/turn-tracker.d.ts +21 -2
- package/dist/turns/turn-tracker.js +379 -56
- package/dist/turns/types/transcript.types.d.ts +5 -0
- package/dist/turns/types/turn-state.types.d.ts +116 -5
- package/dist/turns/types/turn-summary.types.d.ts +64 -14
- package/dist/turns/types/turn-tracker.types.d.ts +27 -1
- package/dist/turns/work-checkout.d.ts +67 -0
- package/dist/turns/work-checkout.js +700 -0
- package/dist/untraced/constants/untraced.constants.d.ts +31 -0
- package/dist/untraced/constants/untraced.constants.js +63 -0
- package/dist/untraced/untraced.d.ts +35 -0
- package/dist/untraced/untraced.js +105 -0
- package/dist/update/constants/update.constants.d.ts +18 -0
- package/dist/update/constants/update.constants.js +20 -0
- package/dist/update/update-check.d.ts +52 -0
- package/dist/update/update-check.js +113 -0
- package/docs/AUDIT_PROMPT.md +129 -0
- package/docs/DATA_HANDLING.md +655 -0
- package/docs/ENTERPRISE_DEPLOYMENT.md +194 -0
- package/docs/PERFORMANCE.md +81 -0
- package/package.json +14 -4
- package/dist/enrollment/enrollment-provider.d.ts +0 -5
- package/dist/enrollment/manual-enrollment.d.ts +0 -32
- package/dist/enrollment/manual-enrollment.js +0 -72
- package/example/README.md +0 -43
- package/example/server.mjs +0 -240
- /package/dist/{enrollment/enrollment-provider.js → config/types/destination.types.js} +0 -0
package/dist/turns/turn-state.js
CHANGED
|
@@ -1,11 +1,13 @@
|
|
|
1
1
|
import fs from 'node:fs/promises';
|
|
2
2
|
import path from 'node:path';
|
|
3
|
-
import { asRecord } from '../core/object.js';
|
|
3
|
+
import { asRecord, compact } from '../core/object.js';
|
|
4
4
|
import { sha256Hex } from '../events/event-id.js';
|
|
5
|
+
import { MAX_TICKET_KEYS } from '../feature/constants/feature.constants.js';
|
|
5
6
|
import { writeFileAtomic } from '../storage/atomic-file.js';
|
|
6
7
|
import { readJsonFile } from '../storage/json-file.js';
|
|
7
|
-
import { SECRET_FILE_MODE } from '../storage/constants/storage.constants.js';
|
|
8
|
-
import {
|
|
8
|
+
import { SECRET_FILE_MODE, SWEEP_MARKER_FILE } from '../storage/constants/storage.constants.js';
|
|
9
|
+
import { claimSweep } from '../storage/sweep-marker.js';
|
|
10
|
+
import { AGENT_PIDS_DIR, AGENT_PROCESS_FILE, BILLING_SENT_FILE, INHERITED_CHECKOUT_FILE, LIVENESS_IGNORED_FILES, PROMPT_TICKETS_FILE, RE_UNSAFE_NAME_CHARS, SESSION_DIR_HASH_LENGTH, SESSION_FILES, SESSION_MODEL_FILE, SESSION_START_FILE, SESSION_TITLE_FILE, TURN_STATE_SWEEP_INTERVAL_MS, UNTRACED_MARKER_PREFIX, USAGE_CLAIM_PREFIX, WORK_CHECKOUT_FILE } from './constants/turns.constants.js';
|
|
9
11
|
/**
|
|
10
12
|
* Per-session accumulator for turn summaries.
|
|
11
13
|
*
|
|
@@ -56,21 +58,7 @@ export class TurnStateStore {
|
|
|
56
58
|
* masquerade as "no records", or the turn would silently lose its context.
|
|
57
59
|
*/
|
|
58
60
|
async collectEntries(sessionId) {
|
|
59
|
-
|
|
60
|
-
const names = await readSessionDir(dir);
|
|
61
|
-
if (!names)
|
|
62
|
-
return [];
|
|
63
|
-
const entries = [];
|
|
64
|
-
for (const name of names) {
|
|
65
|
-
if (!name.endsWith('.json') || name.startsWith(USAGE_CLAIM_PREFIX))
|
|
66
|
-
continue;
|
|
67
|
-
const file = path.join(dir, name);
|
|
68
|
-
const read = await readJsonFile(file);
|
|
69
|
-
if (read.state !== 'ok' || !isTurnRecord(read.value))
|
|
70
|
-
continue;
|
|
71
|
-
entries.push({ file, record: read.value });
|
|
72
|
-
}
|
|
73
|
-
return entries.sort((a, b) => a.record.at.localeCompare(b.record.at));
|
|
61
|
+
return entriesIn(this.sessionDir(sessionId));
|
|
74
62
|
}
|
|
75
63
|
/**
|
|
76
64
|
* Delete exactly these record files; concurrent appends are untouched.
|
|
@@ -122,12 +110,308 @@ export class TurnStateStore {
|
|
|
122
110
|
return claimed;
|
|
123
111
|
}
|
|
124
112
|
/**
|
|
125
|
-
*
|
|
113
|
+
* Remember what model this session is on.
|
|
114
|
+
*
|
|
115
|
+
* Claude Code names its model on SessionStart alone, so the hook that gates a
|
|
116
|
+
* later prompt has no way to learn it from its own payload; the agents that do
|
|
117
|
+
* name it on every hook simply rewrite the same file. One file per session,
|
|
118
|
+
* beside the records, cleared with them.
|
|
119
|
+
*
|
|
120
|
+
* No timestamp: the sweep ages a session by its files' mtimes, and nothing
|
|
121
|
+
* else has a use for when the agent said it.
|
|
122
|
+
*
|
|
123
|
+
* @param sessionId - Provider session id.
|
|
124
|
+
* @param model - Model exactly as the collector spells it in reported usage.
|
|
125
|
+
*/
|
|
126
|
+
async rememberModel(sessionId, model) {
|
|
127
|
+
const file = path.join(this.sessionDir(sessionId), SESSION_MODEL_FILE);
|
|
128
|
+
const record = { model };
|
|
129
|
+
// 0600 like every other file here: what a tenant runs is theirs to know.
|
|
130
|
+
await writeFileAtomic(file, JSON.stringify(record), SECRET_FILE_MODE);
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* Forget which model a session was on.
|
|
134
|
+
*
|
|
135
|
+
* Called when a session starts naming no model, which is the one moment this
|
|
136
|
+
* store knows the memo must not be inherited. `clear` at SessionEnd swallows
|
|
137
|
+
* only ENOENT, so a cleanup that failed on anything else — EACCES, EBUSY, a
|
|
138
|
+
* full disk — leaves the memo behind; a session id that is then reused, as
|
|
139
|
+
* `--resume` does, would have its gate state the *previous* session's model.
|
|
140
|
+
* That is worse than stating none: it is a confident wrong answer on a path
|
|
141
|
+
* whose whole contract is that anything short of certainty allows the turn.
|
|
142
|
+
*
|
|
143
|
+
* @param sessionId - Provider session id.
|
|
144
|
+
*/
|
|
145
|
+
async forgetModel(sessionId) {
|
|
146
|
+
await fs.rm(path.join(this.sessionDir(sessionId), SESSION_MODEL_FILE), { force: true });
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* The model last remembered for a session.
|
|
150
|
+
*
|
|
151
|
+
* Read on the gate path, where an answer is worth more than completeness: a
|
|
152
|
+
* file that is missing, unreadable or no longer the shape this code knows
|
|
153
|
+
* means the gate states no model, never that it waits or throws.
|
|
154
|
+
*
|
|
155
|
+
* @param sessionId - Provider session id.
|
|
156
|
+
* @returns The model, or undefined when none was usably remembered.
|
|
157
|
+
*/
|
|
158
|
+
async readModel(sessionId) {
|
|
159
|
+
const read = await readJsonFile(path.join(this.sessionDir(sessionId), SESSION_MODEL_FILE));
|
|
160
|
+
if (read.state !== 'ok')
|
|
161
|
+
return undefined;
|
|
162
|
+
const model = asRecord(read.value)?.['model'];
|
|
163
|
+
if (typeof model !== 'string' || model === '')
|
|
164
|
+
return undefined;
|
|
165
|
+
return model;
|
|
166
|
+
}
|
|
167
|
+
/**
|
|
168
|
+
* The names of a session's files, and nothing read: how a tool hook learns
|
|
169
|
+
* which checkouts its turn already named, for the price of one `readdir`.
|
|
170
|
+
*
|
|
171
|
+
* @param sessionId - Provider session id.
|
|
172
|
+
* @returns The file names; empty when the session has none.
|
|
173
|
+
*/
|
|
174
|
+
async names(sessionId) {
|
|
175
|
+
return readSessionDirSafely(this.sessionDir(sessionId));
|
|
176
|
+
}
|
|
177
|
+
/**
|
|
178
|
+
* Remember the folder the session started in, once.
|
|
179
|
+
*
|
|
180
|
+
* The tenant and the no-roots boundary are asked of it, and the cwd each
|
|
181
|
+
* later hook reports follows the agent's `cd`. The first hook of a session
|
|
182
|
+
* writes it; every later one finds it there (`wx` fails, which is the point).
|
|
183
|
+
*
|
|
184
|
+
* @param sessionId - Provider session id.
|
|
185
|
+
* @param cwd - The first hook's cwd.
|
|
186
|
+
* @returns Whether this call created it, that is, whether this is the session's first hook.
|
|
187
|
+
*/
|
|
188
|
+
async rememberStart(sessionId, cwd) {
|
|
189
|
+
const file = path.join(this.sessionDir(sessionId), SESSION_START_FILE);
|
|
190
|
+
return createOnce(file, JSON.stringify({ cwd }));
|
|
191
|
+
}
|
|
192
|
+
/**
|
|
193
|
+
* Take the session's one billing fact.
|
|
194
|
+
*
|
|
195
|
+
* @param sessionId - Provider session id.
|
|
196
|
+
* @returns Whether this call took it, that is, whether the fact is still unsent.
|
|
197
|
+
*/
|
|
198
|
+
async takeBillingFact(sessionId) {
|
|
199
|
+
return createOnce(path.join(this.sessionDir(sessionId), BILLING_SENT_FILE), '{}');
|
|
200
|
+
}
|
|
201
|
+
/**
|
|
202
|
+
* Take the session's one notice for a checkout.
|
|
203
|
+
*
|
|
204
|
+
* @param sessionId - Provider session id.
|
|
205
|
+
* @param rootHash - Hash of the checkout root.
|
|
206
|
+
* @returns Whether this call took it, that is, whether the notice is still unshown.
|
|
207
|
+
*/
|
|
208
|
+
async takeNotice(sessionId, rootHash) {
|
|
209
|
+
const file = path.join(this.sessionDir(sessionId), `${UNTRACED_MARKER_PREFIX}${rootHash}.json`);
|
|
210
|
+
return createOnce(file, '{}');
|
|
211
|
+
}
|
|
212
|
+
/**
|
|
213
|
+
* Remember the agent process the session runs in, and index it by pid.
|
|
214
|
+
*
|
|
215
|
+
* @param sessionId - Provider session id.
|
|
216
|
+
* @param agent - The agent's pid and start time.
|
|
217
|
+
*/
|
|
218
|
+
async rememberAgentProcess(sessionId, agent) {
|
|
219
|
+
await writeFileAtomic(path.join(this.sessionDir(sessionId), AGENT_PROCESS_FILE), JSON.stringify(agent), SECRET_FILE_MODE);
|
|
220
|
+
await writeFileAtomic(this.pidFile(agent.pid), JSON.stringify({ sessionId, started: agent.started }), SECRET_FILE_MODE);
|
|
221
|
+
}
|
|
222
|
+
/**
|
|
223
|
+
* The session whose agent process this is.
|
|
224
|
+
*
|
|
225
|
+
* @param pid - A process id found among a child's ancestors.
|
|
226
|
+
* @returns The session id and the start time remembered with it, or undefined
|
|
227
|
+
* when none runs there. The caller compares the start time: a pid whose
|
|
228
|
+
* process started at another time is a reused pid, not that agent.
|
|
229
|
+
*/
|
|
230
|
+
async sessionOfAgent(pid) {
|
|
231
|
+
const read = await readJsonFile(this.pidFile(pid));
|
|
232
|
+
const entry = read.state === 'ok' ? asRecord(read.value) : undefined;
|
|
233
|
+
const sessionId = entry?.['sessionId'];
|
|
234
|
+
const remembered = entry?.['started'];
|
|
235
|
+
if (typeof sessionId !== 'string')
|
|
236
|
+
return undefined;
|
|
237
|
+
return { sessionId, started: typeof remembered === 'string' ? remembered : undefined };
|
|
238
|
+
}
|
|
239
|
+
/**
|
|
240
|
+
* Whether a session has been written to within `maxAgeMs`: not ended, not expired.
|
|
241
|
+
*
|
|
242
|
+
* @param sessionId - Provider session id.
|
|
243
|
+
* @param maxAgeMs - How recent its newest file must be.
|
|
244
|
+
* @returns True when the session's state is there and fresh.
|
|
245
|
+
*/
|
|
246
|
+
async isLive(sessionId, maxAgeMs) {
|
|
247
|
+
return (await newestMtime(this.sessionDir(sessionId), LIVENESS_IGNORED_FILES)) > Date.now() - maxAgeMs;
|
|
248
|
+
}
|
|
249
|
+
/**
|
|
250
|
+
* Remember what this session inherited from the process that started it.
|
|
251
|
+
*
|
|
252
|
+
* @param sessionId - Provider session id.
|
|
253
|
+
* @param memo - The parent session and the nearest ancestor checkout.
|
|
254
|
+
*/
|
|
255
|
+
async rememberInherited(sessionId, memo) {
|
|
256
|
+
await writeFileAtomic(path.join(this.sessionDir(sessionId), INHERITED_CHECKOUT_FILE), JSON.stringify(memo), SECRET_FILE_MODE);
|
|
257
|
+
}
|
|
258
|
+
/**
|
|
259
|
+
* What this session inherited.
|
|
260
|
+
*
|
|
261
|
+
* @param sessionId - Provider session id.
|
|
262
|
+
* @returns The memo, or undefined when the first hook found nothing to inherit. A memo can hold no answer: `cuts` while a walk is to be tried again, or nothing after a retry found nothing.
|
|
263
|
+
*/
|
|
264
|
+
async readInherited(sessionId) {
|
|
265
|
+
const read = await readJsonFile(path.join(this.sessionDir(sessionId), INHERITED_CHECKOUT_FILE));
|
|
266
|
+
const memo = read.state === 'ok' ? asRecord(read.value) : undefined;
|
|
267
|
+
const parent = memo?.['parent'];
|
|
268
|
+
const root = memo?.['root'];
|
|
269
|
+
const cuts = memo?.['cuts'];
|
|
270
|
+
if (!memo)
|
|
271
|
+
return undefined;
|
|
272
|
+
return {
|
|
273
|
+
parent: typeof parent === 'string' ? parent : undefined,
|
|
274
|
+
root: typeof root === 'string' && path.isAbsolute(root) ? root : undefined,
|
|
275
|
+
cuts: typeof cuts === 'number' && Number.isInteger(cuts) && cuts > 0 ? cuts : undefined
|
|
276
|
+
};
|
|
277
|
+
}
|
|
278
|
+
/**
|
|
279
|
+
* The folder the session started in.
|
|
280
|
+
*
|
|
281
|
+
* @param sessionId - Provider session id.
|
|
282
|
+
* @returns The folder, or undefined when none was usably remembered.
|
|
283
|
+
*/
|
|
284
|
+
async readStart(sessionId) {
|
|
285
|
+
const read = await readJsonFile(path.join(this.sessionDir(sessionId), SESSION_START_FILE));
|
|
286
|
+
const cwd = read.state === 'ok' ? asRecord(read.value)?.['cwd'] : undefined;
|
|
287
|
+
return typeof cwd === 'string' && path.isAbsolute(cwd) ? cwd : undefined;
|
|
288
|
+
}
|
|
289
|
+
/**
|
|
290
|
+
* Remember the session's titles, for a turn whose transcript tail no longer
|
|
291
|
+
* reaches a title entry.
|
|
292
|
+
*
|
|
293
|
+
* @param sessionId - Provider session id.
|
|
294
|
+
* @param titles - The custom and generated titles as the transcript holds them.
|
|
295
|
+
*/
|
|
296
|
+
async rememberSessionTitles(sessionId, titles) {
|
|
297
|
+
await writeFileAtomic(path.join(this.sessionDir(sessionId), SESSION_TITLE_FILE), JSON.stringify(titles), SECRET_FILE_MODE);
|
|
298
|
+
}
|
|
299
|
+
/**
|
|
300
|
+
* The titles this session last had.
|
|
301
|
+
*
|
|
302
|
+
* @param sessionId - Provider session id.
|
|
303
|
+
* @returns The titles; empty when none were usably remembered.
|
|
304
|
+
*/
|
|
305
|
+
async readSessionTitles(sessionId) {
|
|
306
|
+
const read = await readJsonFile(path.join(this.sessionDir(sessionId), SESSION_TITLE_FILE));
|
|
307
|
+
const memo = read.state === 'ok' ? asRecord(read.value) : undefined;
|
|
308
|
+
const custom = memo?.['custom'];
|
|
309
|
+
const generated = memo?.['generated'];
|
|
310
|
+
return compact({
|
|
311
|
+
custom: typeof custom === 'string' && custom ? custom : undefined,
|
|
312
|
+
generated: typeof generated === 'string' && generated ? generated : undefined
|
|
313
|
+
});
|
|
314
|
+
}
|
|
315
|
+
/**
|
|
316
|
+
* Remember the checkout this session's turn just changed.
|
|
317
|
+
*
|
|
318
|
+
* @param sessionId - Provider session id.
|
|
319
|
+
* @param memo - Its root, repository and when.
|
|
320
|
+
*/
|
|
321
|
+
async rememberWorkCheckout(sessionId, memo) {
|
|
322
|
+
await writeFileAtomic(path.join(this.sessionDir(sessionId), WORK_CHECKOUT_FILE), JSON.stringify(memo), SECRET_FILE_MODE);
|
|
323
|
+
}
|
|
324
|
+
/**
|
|
325
|
+
* The checkout this session last changed.
|
|
326
|
+
*
|
|
327
|
+
* @param sessionId - Provider session id.
|
|
328
|
+
* @returns The memo, or undefined when none was usably remembered.
|
|
329
|
+
*/
|
|
330
|
+
async readWorkCheckout(sessionId) {
|
|
331
|
+
return workCheckoutIn(this.sessionDir(sessionId));
|
|
332
|
+
}
|
|
333
|
+
/**
|
|
334
|
+
* What every other session still active on this machine has changed, for a
|
|
335
|
+
* turn that shares a checkout with them (SPEC §3a): the files its open turn's
|
|
336
|
+
* edit tools named, the branch each checkout its newest open turn named was
|
|
337
|
+
* on at first sight, and its last changed checkout with that turn's branch and
|
|
338
|
+
* files.
|
|
339
|
+
*
|
|
340
|
+
* Active means written to within `maxAgeMs` and not ended, the same test as
|
|
341
|
+
* {@link isLive}. A session whose state cannot be read counts as not active,
|
|
342
|
+
* so the turn behaves as it did before shared checkouts were told apart.
|
|
343
|
+
*
|
|
344
|
+
* ponytail: reads every live session's open records on each close — a
|
|
345
|
+
* `readdir` and a small file per record, no git. Upgrade path: an edit index
|
|
346
|
+
* per checkout root, written by the tool hooks.
|
|
347
|
+
*
|
|
348
|
+
* @param sessionId - The closing session, left out.
|
|
349
|
+
* @param maxAgeMs - How recent a session's newest file must be.
|
|
350
|
+
* @returns One entry per other active session.
|
|
351
|
+
*/
|
|
352
|
+
async othersActivity(sessionId, maxAgeMs) {
|
|
353
|
+
const own = path.basename(this.sessionDir(sessionId));
|
|
354
|
+
const cutoff = Date.now() - maxAgeMs;
|
|
355
|
+
const names = (await readSessionDirSafely(this.turnsDir)).filter((name) => name !== own && name !== SWEEP_MARKER_FILE && name !== AGENT_PIDS_DIR);
|
|
356
|
+
const found = await Promise.all(names.map(async (name) => activityIn(path.join(this.turnsDir, name), cutoff).catch(() => undefined)));
|
|
357
|
+
return found.filter((activity) => activity !== undefined);
|
|
358
|
+
}
|
|
359
|
+
/**
|
|
360
|
+
* Remember the ticket keys the session's opening prompt named, once.
|
|
361
|
+
*
|
|
362
|
+
* Created exclusively, like the start folder: concurrent hooks cannot
|
|
363
|
+
* overwrite it, and a later prompt finds it there (`wx` fails, which is the
|
|
364
|
+
* point). An empty list is written too, so a session whose first prompt named
|
|
365
|
+
* no ticket is never claimed by a later one.
|
|
366
|
+
*
|
|
367
|
+
* @param sessionId - Provider session id.
|
|
368
|
+
* @param keys - The opening prompt's keys, possibly none.
|
|
369
|
+
* @returns Whether this call wrote it, that is, whether this was the opening prompt.
|
|
370
|
+
*/
|
|
371
|
+
async rememberPromptTickets(sessionId, keys) {
|
|
372
|
+
const file = path.join(this.sessionDir(sessionId), PROMPT_TICKETS_FILE);
|
|
373
|
+
return createOnce(file, JSON.stringify({ keys }));
|
|
374
|
+
}
|
|
375
|
+
/**
|
|
376
|
+
* The ticket keys the session's opening prompt named.
|
|
377
|
+
*
|
|
378
|
+
* @param sessionId - Provider session id.
|
|
379
|
+
* @returns The string keys, at most `MAX_TICKET_KEYS`; empty when none was usably remembered.
|
|
380
|
+
*/
|
|
381
|
+
async readPromptTickets(sessionId) {
|
|
382
|
+
const read = await readJsonFile(path.join(this.sessionDir(sessionId), PROMPT_TICKETS_FILE));
|
|
383
|
+
const keys = read.state === 'ok' ? asRecord(read.value)?.['keys'] : undefined;
|
|
384
|
+
if (!Array.isArray(keys))
|
|
385
|
+
return [];
|
|
386
|
+
return keys.filter((key) => typeof key === 'string').slice(0, MAX_TICKET_KEYS);
|
|
387
|
+
}
|
|
388
|
+
/**
|
|
389
|
+
* Drop a session's state at SessionEnd, all but its opening prompt's keys.
|
|
390
|
+
*
|
|
391
|
+
* `--resume` reuses the session id, and without the memo the resumed
|
|
392
|
+
* session's first prompt ("continue") would become its opening prompt and
|
|
393
|
+
* claim it with no keys. The memo holds keys only, never prompt text, and the
|
|
394
|
+
* expiry sweep removes it with the directory once it ages out — counted from
|
|
395
|
+
* SessionEnd, not from the opening prompt, so a session that ran all day can
|
|
396
|
+
* still be resumed the next morning.
|
|
126
397
|
*
|
|
127
398
|
* @param sessionId - Provider session id.
|
|
128
399
|
*/
|
|
129
400
|
async clear(sessionId) {
|
|
130
|
-
await
|
|
401
|
+
const read = await readJsonFile(path.join(this.sessionDir(sessionId), AGENT_PROCESS_FILE));
|
|
402
|
+
const pid = read.state === 'ok' ? asRecord(read.value)?.['pid'] : undefined;
|
|
403
|
+
// Only this session's own entry: the pid may by now be another session's.
|
|
404
|
+
if (typeof pid === 'number' && (await this.sessionOfAgent(pid))?.sessionId === sessionId)
|
|
405
|
+
await fs.rm(this.pidFile(pid), { force: true });
|
|
406
|
+
const dir = this.sessionDir(sessionId);
|
|
407
|
+
const names = await readSessionDir(dir);
|
|
408
|
+
if (!names?.includes(PROMPT_TICKETS_FILE)) {
|
|
409
|
+
await fs.rm(dir, { recursive: true, force: true });
|
|
410
|
+
return;
|
|
411
|
+
}
|
|
412
|
+
await Promise.all(names.filter((name) => name !== PROMPT_TICKETS_FILE).map((name) => fs.rm(path.join(dir, name), { recursive: true, force: true })));
|
|
413
|
+
const now = new Date();
|
|
414
|
+
await fs.utimes(path.join(dir, PROMPT_TICKETS_FILE), now, now).catch(() => undefined);
|
|
131
415
|
}
|
|
132
416
|
/**
|
|
133
417
|
* Remove sessions whose newest record is older than `maxAgeMs`.
|
|
@@ -135,16 +419,57 @@ export class TurnStateStore {
|
|
|
135
419
|
* Crashed or abandoned sessions never see a Stop or SessionEnd, and their
|
|
136
420
|
* raw prompt text must not sit on disk indefinitely.
|
|
137
421
|
*
|
|
422
|
+
* The session's model memo is one more file in that directory, and the age is
|
|
423
|
+
* the newest mtime in it, so remembering a model can only ever make a session
|
|
424
|
+
* look younger than its records — never older, and never sweepable while it is
|
|
425
|
+
* still being written to.
|
|
426
|
+
*
|
|
427
|
+
* Throttled by a marker in the state root: the walk used to run on every
|
|
428
|
+
* closing turn, which is hundreds of syscalls to learn that nothing had aged
|
|
429
|
+
* out. Removal now happens within the TTL plus the sweep interval, which is
|
|
430
|
+
* what the retention window's tolerance allows.
|
|
431
|
+
*
|
|
138
432
|
* @param maxAgeMs - Age past which a session is considered abandoned.
|
|
139
433
|
*/
|
|
140
434
|
async sweep(maxAgeMs) {
|
|
435
|
+
if (!(await claimSweep(path.join(this.turnsDir, SWEEP_MARKER_FILE), TURN_STATE_SWEEP_INTERVAL_MS, Date.now())))
|
|
436
|
+
return;
|
|
141
437
|
const sessionDirs = await readSessionDirSafely(this.turnsDir);
|
|
142
438
|
const cutoff = Date.now() - maxAgeMs;
|
|
143
439
|
for (const name of sessionDirs) {
|
|
440
|
+
// The marker lives in this directory too, and it is not a session: without
|
|
441
|
+
// this it is handed to `removeIfStale`, which fails with ENOTDIR into a
|
|
442
|
+
// catch that reports it as concurrent hook activity.
|
|
443
|
+
if (name === SWEEP_MARKER_FILE)
|
|
444
|
+
continue;
|
|
445
|
+
if (name === AGENT_PIDS_DIR) {
|
|
446
|
+
await this.pruneAgentPids(path.join(this.turnsDir, name), cutoff);
|
|
447
|
+
continue;
|
|
448
|
+
}
|
|
144
449
|
const dir = path.join(this.turnsDir, name);
|
|
145
450
|
await removeIfStale(dir, cutoff);
|
|
146
451
|
}
|
|
147
452
|
}
|
|
453
|
+
/**
|
|
454
|
+
* Drop the pid entries whose session is gone or idle past the cutoff.
|
|
455
|
+
*
|
|
456
|
+
* By the session's own freshness, not the entry's: the entry is written once,
|
|
457
|
+
* by the session's first hook, and a long-lived parent outlasts its mtime.
|
|
458
|
+
*
|
|
459
|
+
* @param dir - The agent pid index.
|
|
460
|
+
* @param cutoff - Epoch milliseconds; a session idle since before this is abandoned.
|
|
461
|
+
*/
|
|
462
|
+
async pruneAgentPids(dir, cutoff) {
|
|
463
|
+
for (const file of await readSessionDirSafely(dir)) {
|
|
464
|
+
const read = await readJsonFile(path.join(dir, file));
|
|
465
|
+
const sessionId = read.state === 'ok' ? asRecord(read.value)?.['sessionId'] : undefined;
|
|
466
|
+
if (typeof sessionId !== 'string' || (await newestMtime(this.sessionDir(sessionId))) < cutoff)
|
|
467
|
+
await fs.rm(path.join(dir, file), { force: true });
|
|
468
|
+
}
|
|
469
|
+
}
|
|
470
|
+
pidFile(pid) {
|
|
471
|
+
return path.join(this.turnsDir, AGENT_PIDS_DIR, `${Math.trunc(pid)}.json`);
|
|
472
|
+
}
|
|
148
473
|
/**
|
|
149
474
|
* Directory holding one session's state.
|
|
150
475
|
*
|
|
@@ -158,6 +483,99 @@ export class TurnStateStore {
|
|
|
158
483
|
return path.join(this.turnsDir, sha256Hex(sessionId).slice(0, SESSION_DIR_HASH_LENGTH));
|
|
159
484
|
}
|
|
160
485
|
}
|
|
486
|
+
/**
|
|
487
|
+
* Every turn record in one session directory, oldest first.
|
|
488
|
+
*
|
|
489
|
+
* @param dir - Session directory.
|
|
490
|
+
* @returns The entries; empty when the directory does not exist.
|
|
491
|
+
* @throws When the directory exists but cannot be listed.
|
|
492
|
+
*/
|
|
493
|
+
async function entriesIn(dir) {
|
|
494
|
+
const names = await readSessionDir(dir);
|
|
495
|
+
if (!names)
|
|
496
|
+
return [];
|
|
497
|
+
const entries = [];
|
|
498
|
+
for (const name of names) {
|
|
499
|
+
if (!name.endsWith('.json') || name.startsWith(USAGE_CLAIM_PREFIX) || SESSION_FILES.has(name) || name.startsWith(UNTRACED_MARKER_PREFIX))
|
|
500
|
+
continue;
|
|
501
|
+
const file = path.join(dir, name);
|
|
502
|
+
const read = await readJsonFile(file);
|
|
503
|
+
if (read.state !== 'ok' || !isTurnRecord(read.value))
|
|
504
|
+
continue;
|
|
505
|
+
entries.push({ file, record: read.value });
|
|
506
|
+
}
|
|
507
|
+
return entries.sort((a, b) => a.record.at.localeCompare(b.record.at));
|
|
508
|
+
}
|
|
509
|
+
/**
|
|
510
|
+
* One session directory's work-checkout memo, type-checked field by field.
|
|
511
|
+
*
|
|
512
|
+
* @param dir - Session directory.
|
|
513
|
+
* @returns The memo, or undefined when none was usably remembered.
|
|
514
|
+
*/
|
|
515
|
+
async function workCheckoutIn(dir) {
|
|
516
|
+
const read = await readJsonFile(path.join(dir, WORK_CHECKOUT_FILE));
|
|
517
|
+
const memo = read.state === 'ok' ? asRecord(read.value) : undefined;
|
|
518
|
+
const root = memo?.['root'];
|
|
519
|
+
const repository = memo?.['repository'];
|
|
520
|
+
const at = memo?.['at'];
|
|
521
|
+
const branch = memo?.['branch'];
|
|
522
|
+
const files = memo?.['files'];
|
|
523
|
+
if (typeof root !== 'string' || !path.isAbsolute(root) || typeof repository !== 'string' || typeof at !== 'string')
|
|
524
|
+
return undefined;
|
|
525
|
+
return compact({
|
|
526
|
+
root,
|
|
527
|
+
repository,
|
|
528
|
+
at,
|
|
529
|
+
branch: typeof branch === 'string' && branch ? branch : undefined,
|
|
530
|
+
files: Array.isArray(files) ? files.filter((file) => typeof file === 'string') : undefined
|
|
531
|
+
});
|
|
532
|
+
}
|
|
533
|
+
/**
|
|
534
|
+
* What one session directory says its session changed, when it is active.
|
|
535
|
+
*
|
|
536
|
+
* @param dir - Session directory.
|
|
537
|
+
* @param cutoff - Epoch milliseconds; a session idle since before this is not active.
|
|
538
|
+
* @returns The activity, or undefined when the session is not active.
|
|
539
|
+
*/
|
|
540
|
+
async function activityIn(dir, cutoff) {
|
|
541
|
+
if ((await newestMtime(dir, LIVENESS_IGNORED_FILES)) <= cutoff)
|
|
542
|
+
return undefined;
|
|
543
|
+
const [memo, entries] = await Promise.all([workCheckoutIn(dir), entriesIn(dir)]);
|
|
544
|
+
// Records of a turn that never reached its Stop (an interrupt) stay until the
|
|
545
|
+
// session ends; only the newest turn says what the session is doing now.
|
|
546
|
+
// ponytail: an interrupted newest turn still counts until the session's next
|
|
547
|
+
// prompt or its end, and can only withhold another session's branch, never
|
|
548
|
+
// report a wrong one. Upgrade path: age the newest turn by its last record.
|
|
549
|
+
const latest = entries.at(-1)?.record.turnId;
|
|
550
|
+
const edits = [];
|
|
551
|
+
const baselines = [];
|
|
552
|
+
for (const { record } of entries) {
|
|
553
|
+
const current = record.turnId === latest;
|
|
554
|
+
const branch = record.kind === 'checkout' ? asRecord(record.baseline)?.['branch'] : undefined;
|
|
555
|
+
if (current && record.kind === 'checkout' && typeof record.root === 'string' && typeof branch === 'string' && branch)
|
|
556
|
+
baselines.push({ root: record.root, branch });
|
|
557
|
+
if (record.kind !== 'tool' || record.access !== 'edit' || typeof record.filePath !== 'string' || typeof record.repositoryRoot !== 'string')
|
|
558
|
+
continue;
|
|
559
|
+
edits.push({ root: record.repositoryRoot, filePath: record.filePath, current });
|
|
560
|
+
}
|
|
561
|
+
return { memo, edits, baselines };
|
|
562
|
+
}
|
|
563
|
+
/**
|
|
564
|
+
* Create a file exclusively, making its directory first.
|
|
565
|
+
*
|
|
566
|
+
* @param file - Path to create.
|
|
567
|
+
* @param body - File contents.
|
|
568
|
+
* @returns Whether this call created it; false when it already existed.
|
|
569
|
+
* @throws On any failure other than the file already existing.
|
|
570
|
+
*/
|
|
571
|
+
async function createOnce(file, body) {
|
|
572
|
+
await fs.mkdir(path.dirname(file), { recursive: true });
|
|
573
|
+
return fs.writeFile(file, body, { flag: 'wx', mode: SECRET_FILE_MODE }).then(() => true, (error) => {
|
|
574
|
+
if (error.code !== 'EEXIST')
|
|
575
|
+
throw error;
|
|
576
|
+
return false;
|
|
577
|
+
});
|
|
578
|
+
}
|
|
161
579
|
/**
|
|
162
580
|
* List a session directory, distinguishing "never written" from "broken".
|
|
163
581
|
*
|
|
@@ -211,6 +629,23 @@ async function removeIfStale(dir, cutoff) {
|
|
|
211
629
|
// Concurrent hook activity in this session; leave it for the next sweep.
|
|
212
630
|
}
|
|
213
631
|
}
|
|
632
|
+
/**
|
|
633
|
+
* The newest mtime among a directory's files.
|
|
634
|
+
*
|
|
635
|
+
* @param dir - A session directory.
|
|
636
|
+
* @param ignored - File names whose age does not count.
|
|
637
|
+
* @returns Epoch milliseconds; 0 when the directory is missing or empty.
|
|
638
|
+
*/
|
|
639
|
+
async function newestMtime(dir, ignored = new Set()) {
|
|
640
|
+
let newest = 0;
|
|
641
|
+
for (const file of await readSessionDirSafely(dir)) {
|
|
642
|
+
if (ignored.has(file))
|
|
643
|
+
continue;
|
|
644
|
+
const stat = await fs.stat(path.join(dir, file)).catch(() => undefined);
|
|
645
|
+
newest = Math.max(newest, stat?.mtimeMs ?? 0);
|
|
646
|
+
}
|
|
647
|
+
return newest;
|
|
648
|
+
}
|
|
214
649
|
/**
|
|
215
650
|
* Filesystem-safe form of an untrusted record id.
|
|
216
651
|
*
|
|
@@ -230,5 +665,5 @@ function isTurnRecord(value) {
|
|
|
230
665
|
const record = asRecord(value);
|
|
231
666
|
if (!record || typeof record['at'] !== 'string')
|
|
232
667
|
return false;
|
|
233
|
-
return record['kind'] === 'prompt' || record['kind'] === 'tool' || record['kind'] === 'response';
|
|
668
|
+
return record['kind'] === 'prompt' || record['kind'] === 'tool' || record['kind'] === 'response' || record['kind'] === 'checkout';
|
|
234
669
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { BuildTurnSummaryInput, TurnSummaryEvent } from './types/turn-summary.types.js';
|
|
2
|
-
export type { AgentUsageSummary, BuildTurnSummaryInput, TouchedFiles,
|
|
2
|
+
export type { AgentUsageSummary, BuildTurnSummaryInput, TouchedFiles, TurnSummaryEvent, TurnUsageStatus, WorkEvidence } from './types/turn-summary.types.js';
|
|
3
3
|
/**
|
|
4
4
|
* Flatten one turn's accumulated state into the single product record the hook
|
|
5
5
|
* path emits.
|
|
@@ -12,16 +12,3 @@ export type { AgentUsageSummary, BuildTurnSummaryInput, TouchedFiles, TurnRespon
|
|
|
12
12
|
* @returns The summary, with absent fields omitted rather than null.
|
|
13
13
|
*/
|
|
14
14
|
export declare function buildTurnSummary(input: BuildTurnSummaryInput): TurnSummaryEvent;
|
|
15
|
-
/**
|
|
16
|
-
* Recompute content evidence from the text that is actually transmitted.
|
|
17
|
-
*
|
|
18
|
-
* Evidence is captured before sanitization, but the sanitizer truncates and
|
|
19
|
-
* redacts; a backend verifying length or hash against the received text would
|
|
20
|
-
* then reject honest events. When the text is absent (capture disabled), the
|
|
21
|
-
* capture-time evidence still describes the content the developer saw, and is
|
|
22
|
-
* kept as the only description there is.
|
|
23
|
-
*
|
|
24
|
-
* @param summary - Summary about to be sent.
|
|
25
|
-
* @returns A copy whose evidence matches its own text.
|
|
26
|
-
*/
|
|
27
|
-
export declare function alignContentEvidence(summary: TurnSummaryEvent): TurnSummaryEvent;
|