@jjh32456/longlive 0.0.0-stage → 2.0.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 +235 -0
- package/LICENSE +21 -0
- package/QUICKSTART.md +362 -0
- package/README.en.md +64 -0
- package/README.md +256 -2
- package/SPEC.md +211 -0
- package/build-info.json +1665 -0
- package/cordis.patch.yml +208 -0
- package/docs/A1-MIGRATION.md +75 -0
- package/docs/ANSWER-GUARD.md +92 -0
- package/docs/ARCHITECTURE.md +128 -0
- package/docs/AUTO-RECALL-WIRING.md +115 -0
- package/docs/AUTO-RECALL.md +237 -0
- package/docs/CONCEPTS.md +143 -0
- package/docs/CONFIG.md +839 -0
- package/docs/ERRORS.md +271 -0
- package/docs/FAQ.md +186 -0
- package/docs/FEATURES.md +579 -0
- package/docs/INDEX-FRESHNESS.md +21 -0
- package/docs/METRICS.md +244 -0
- package/docs/MIGRATION-1.6.0.md +220 -0
- package/docs/OPERATIONS.md +439 -0
- package/docs/PANEL-INTEGRATION.md +190 -0
- package/docs/PRIVACY.md +88 -0
- package/docs/QUICKSTART.md +155 -0
- package/docs/RAW-INDEX-POLICY.md +7 -0
- package/docs/README.en.md +48 -0
- package/docs/RESIDENT-WATCHER.md +90 -0
- package/docs/SESSION-WATCH.md +214 -0
- package/docs/TOOLS.md +184 -0
- package/docs/TROUBLESHOOTING.md +329 -0
- package/icon.svg +11 -0
- package/lib/client.js +436 -0
- package/locale/en.json +6 -0
- package/locale/zh.json +6 -0
- package/package.json +163 -4
- package/runbook.md +343 -0
- package/scripts/archive-digest-build.mjs +668 -0
- package/scripts/auto-truncate-run.ps1 +752 -0
- package/scripts/backfill-auto-cards.mjs +98 -0
- package/scripts/edges-build.mjs +1003 -0
- package/scripts/index-maintain.mjs +39 -0
- package/scripts/index-reconcile.mjs +49 -0
- package/scripts/index-status.mjs +28 -0
- package/scripts/install-resident-watcher.ps1 +15 -0
- package/scripts/install-session-watch.ps1 +332 -0
- package/scripts/install-surgery-task.ps1 +174 -0
- package/scripts/lib/auto-truncate-run.ps1 +512 -0
- package/scripts/lib/session-watch-fixture.mjs +207 -0
- package/scripts/longlive-doctor.mjs +578 -0
- package/scripts/metrics-read.mjs +79 -0
- package/scripts/metrics-sample.mjs +215 -0
- package/scripts/migrate-180.mjs +163 -0
- package/scripts/migrate-200.mjs +8 -0
- package/scripts/migrate-a1-legacy.mjs +62 -0
- package/scripts/migrate-lifecycle.mjs +157 -0
- package/scripts/pack.mjs +392 -0
- package/scripts/recall-index-build.mjs +128 -0
- package/scripts/recall-links-build.mjs +420 -0
- package/scripts/rescore-cards.mjs +36 -0
- package/scripts/resident-watcher-status.ps1 +15 -0
- package/scripts/resident-watcher.ps1 +643 -0
- package/scripts/session-watch-drill.mjs +553 -0
- package/scripts/session-watch-status.ps1 +146 -0
- package/scripts/session-watch.mjs +1367 -0
- package/scripts/surgery-inspect.mjs +122 -0
- package/scripts/surgery-run.cmd +34 -0
- package/scripts/truncate-cli.mjs +279 -0
- package/scripts/unattended-surgery.ps1 +1019 -0
- package/scripts/uninstall-resident-watcher.ps1 +15 -0
- package/scripts/uninstall-session-watch.ps1 +192 -0
- package/scripts/verify-card-anchors.mjs +138 -0
- package/scripts/watch-loop.ps1 +916 -0
- package/src/actions/audit.js +125 -0
- package/src/actions/evidence.js +122 -0
- package/src/actions/fs.js +162 -0
- package/src/activity/index.js +873 -0
- package/src/activity/model.js +351 -0
- package/src/activity/store.js +786 -0
- package/src/activity/summary.js +180 -0
- package/src/activity/thinking.js +191 -0
- package/src/adapters/constants.js +11 -0
- package/src/adapters/engram/data/aliases.json +51 -0
- package/src/adapters/engram/data/policy.json +8 -0
- package/src/adapters/engram/data/routing-rules.json +546 -0
- package/src/adapters/engram/index.js +278 -0
- package/src/adapters/registry.js +315 -0
- package/src/answer/audit.js +238 -0
- package/src/answer/claim-guard.mjs +234 -0
- package/src/answer/evidence.js +30 -0
- package/src/answer/guard.js +1370 -0
- package/src/answer/index.js +1086 -0
- package/src/answer/query.js +111 -0
- package/src/answer/second-hop.mjs +101 -0
- package/src/auto-truncate-status.js +164 -0
- package/src/auto-truncate.js +1181 -0
- package/src/autoprocedure/candidate.js +342 -0
- package/src/autoprocedure/config.js +122 -0
- package/src/autoprocedure/evidence.js +345 -0
- package/src/autoprocedure/index.js +83 -0
- package/src/autoprocedure/ledger.js +329 -0
- package/src/autoprocedure/lint.js +232 -0
- package/src/autoprocedure/llm.js +409 -0
- package/src/autoprocedure/locks.js +102 -0
- package/src/autoprocedure/paths.js +39 -0
- package/src/autoprocedure/privacy.js +87 -0
- package/src/autoprocedure/rules.js +288 -0
- package/src/autoprocedure/runtime.js +718 -0
- package/src/autoprocedure/session-reader.js +328 -0
- package/src/backup.js +310 -0
- package/src/bus/bus.mjs +1446 -0
- package/src/codec.js +269 -0
- package/src/compaction/cut-engine.js +44 -0
- package/src/compaction/engine-core.js +1237 -0
- package/src/compaction/entry.js +228 -0
- package/src/compaction.js +230 -0
- package/src/conclusion.js +366 -0
- package/src/config.js +835 -0
- package/src/d6/runtime.js +517 -0
- package/src/d6/surface.js +181 -0
- package/src/deps.js +189 -0
- package/src/engine-status.js +141 -0
- package/src/features.js +1035 -0
- package/src/handoff/index.js +1077 -0
- package/src/handoff/model.js +1058 -0
- package/src/handoff/store.js +1233 -0
- package/src/heartbeat.js +385 -0
- package/src/home.js +16 -0
- package/src/host-version.js +280 -0
- package/src/hot-truncate-diag.js +98 -0
- package/src/identity/card-marker.js +281 -0
- package/src/identity/handover.js +845 -0
- package/src/identity/index.js +337 -0
- package/src/identity/inject-position.js +133 -0
- package/src/identity/writeback.js +344 -0
- package/src/identity.js +118 -0
- package/src/index.js +647 -0
- package/src/lifecycle/gap-clocks.js +364 -0
- package/src/lifecycle/index.js +269 -0
- package/src/lifecycle/proactive.js +1426 -0
- package/src/live-target.js +212 -0
- package/src/live.js +1603 -0
- package/src/log-rotation.js +153 -0
- package/src/memory/a1-migration.js +190 -0
- package/src/memory/activity-capture.js +229 -0
- package/src/memory/anchor-fallback.js +116 -0
- package/src/memory/anchors.js +194 -0
- package/src/memory/archive-cold-state.mjs +794 -0
- package/src/memory/archive-staging.mjs +305 -0
- package/src/memory/archive.mjs +415 -0
- package/src/memory/audit-records.js +222 -0
- package/src/memory/auto-cards.js +441 -0
- package/src/memory/capture-host.js +297 -0
- package/src/memory/capture.js +496 -0
- package/src/memory/card-anchors.js +203 -0
- package/src/memory/card-priority.js +110 -0
- package/src/memory/cards.js +425 -0
- package/src/memory/catchup.mjs +534 -0
- package/src/memory/conclusion-distill.js +381 -0
- package/src/memory/conclusion-drafts.js +243 -0
- package/src/memory/conflicts.js +1060 -0
- package/src/memory/consolidate.js +834 -0
- package/src/memory/decay.js +311 -0
- package/src/memory/distill.js +485 -0
- package/src/memory/event-index.js +1826 -0
- package/src/memory/evidence-chain.js +295 -0
- package/src/memory/evidence.js +213 -0
- package/src/memory/filter-runner.js +555 -0
- package/src/memory/filter.js +1035 -0
- package/src/memory/forget-v2.js +680 -0
- package/src/memory/forget.js +531 -0
- package/src/memory/frames-activation.js +249 -0
- package/src/memory/frontmatter.js +454 -0
- package/src/memory/fts-query.js +342 -0
- package/src/memory/governance.js +604 -0
- package/src/memory/index-freshness.js +234 -0
- package/src/memory/index.js +435 -0
- package/src/memory/l2-gate.js +169 -0
- package/src/memory/land.mjs +743 -0
- package/src/memory/lifecycle.js +235 -0
- package/src/memory/lock.js +121 -0
- package/src/memory/maintenance.js +1149 -0
- package/src/memory/manifest.js +245 -0
- package/src/memory/memory-health.mjs +607 -0
- package/src/memory/migrate-200.js +1681 -0
- package/src/memory/migrate-lifecycle.js +1808 -0
- package/src/memory/migrate.js +139 -0
- package/src/memory/paths.js +229 -0
- package/src/memory/procedure-candidate.mjs +377 -0
- package/src/memory/procedure.js +982 -0
- package/src/memory/prospective-memory.mjs +362 -0
- package/src/memory/provenance.js +119 -0
- package/src/memory/recall-feedback.mjs +406 -0
- package/src/memory/recall-v2.js +218 -0
- package/src/memory/recall.js +357 -0
- package/src/memory/reconcile.js +265 -0
- package/src/memory/records.js +433 -0
- package/src/memory/reinforce.js +281 -0
- package/src/memory/retention.js +162 -0
- package/src/memory/review.js +124 -0
- package/src/memory/state.js +279 -0
- package/src/memory/tiers.js +168 -0
- package/src/memory/topic-phrases.js +73 -0
- package/src/memory/trust.js +97 -0
- package/src/memory/turn-budget.mjs +290 -0
- package/src/memory/version-chain.js +264 -0
- package/src/memory/wake-bridge.mjs +2999 -0
- package/src/memory/wal.js +137 -0
- package/src/memory/working-frame.js +779 -0
- package/src/metrics/classify.mjs +191 -0
- package/src/metrics/estimate.mjs +108 -0
- package/src/metrics/index.mjs +204 -0
- package/src/metrics/measure.mjs +462 -0
- package/src/metrics/prestep-latency.mjs +307 -0
- package/src/metrics/store.mjs +264 -0
- package/src/original.js +294 -0
- package/src/policy/threshold-policy.js +224 -0
- package/src/policy-profile.js +463 -0
- package/src/recall/anaphora.mjs +87 -0
- package/src/recall/archive-config.mjs +130 -0
- package/src/recall/archive-fallback.mjs +390 -0
- package/src/recall/archive-search.mjs +428 -0
- package/src/recall/archive-state.mjs +588 -0
- package/src/recall/brain.mjs +501 -0
- package/src/recall/cards.mjs +202 -0
- package/src/recall/current-line.mjs +110 -0
- package/src/recall/edges.js +398 -0
- package/src/recall/embedding.mjs +698 -0
- package/src/recall/entities.mjs +112 -0
- package/src/recall/explain.mjs +443 -0
- package/src/recall/force-policy.mjs +138 -0
- package/src/recall/gate-log.mjs +280 -0
- package/src/recall/gate-metrics.mjs +191 -0
- package/src/recall/index.js +1337 -0
- package/src/recall/inject-order.mjs +279 -0
- package/src/recall/link-recall.mjs +183 -0
- package/src/recall/links.mjs +696 -0
- package/src/recall/privacy.mjs +208 -0
- package/src/recall/recall-core.mjs +810 -0
- package/src/recall/shared-visibility.js +302 -0
- package/src/recall/time-normalizer.mjs +196 -0
- package/src/recall/turn-budget-inject.mjs +154 -0
- package/src/retention/cold-gate.mjs +146 -0
- package/src/retention/config.js +163 -0
- package/src/retention/index.js +801 -0
- package/src/role/activation.js +191 -0
- package/src/role/attention.js +318 -0
- package/src/role/base.js +36 -0
- package/src/role/channels.js +423 -0
- package/src/role/index.js +425 -0
- package/src/role/interfaces.js +115 -0
- package/src/role/personality.js +370 -0
- package/src/role/readers.js +151 -0
- package/src/role/relationships.js +477 -0
- package/src/role/store.js +202 -0
- package/src/role/writeprotect.js +373 -0
- package/src/router/aliases.json +182 -0
- package/src/router/background/agent-runner.mjs +239 -0
- package/src/router/background/anchor.mjs +323 -0
- package/src/router/background/audit.mjs +337 -0
- package/src/router/background/budget-ledger.mjs +648 -0
- package/src/router/background/host-sim.mjs +451 -0
- package/src/router/background/index.mjs +81 -0
- package/src/router/background/memory.mjs +63 -0
- package/src/router/background/mock-llm.mjs +203 -0
- package/src/router/background/queue.mjs +224 -0
- package/src/router/background/retry.mjs +255 -0
- package/src/router/background/router.mjs +1459 -0
- package/src/router/background/stream.mjs +276 -0
- package/src/router/background/tool-policy.mjs +255 -0
- package/src/router/design-headings.json +221 -0
- package/src/router/face/config.mjs +155 -0
- package/src/router/face/context.mjs +284 -0
- package/src/router/face/face.mjs +466 -0
- package/src/router/face/identity.mjs +130 -0
- package/src/router/face/index.mjs +65 -0
- package/src/router/face/journal.mjs +119 -0
- package/src/router/face/llm.mjs +252 -0
- package/src/router/face/outbox.mjs +153 -0
- package/src/router/face/paths.mjs +54 -0
- package/src/router/judge.mjs +987 -0
- package/src/router/policies/generic.json +131 -0
- package/src/router/route.js +1766 -0
- package/src/router/router.mjs +535 -0
- package/src/router/routing-rules.json +816 -0
- package/src/router/run-sim.mjs +530 -0
- package/src/router/runtime.js +569 -0
- package/src/router/service/apply.mjs +1218 -0
- package/src/router/service/board.mjs +712 -0
- package/src/router/service/caller.mjs +131 -0
- package/src/router/service/config.mjs +361 -0
- package/src/router/service/context.mjs +314 -0
- package/src/router/service/hint.mjs +668 -0
- package/src/router/service/lease.mjs +494 -0
- package/src/router/service/live.mjs +251 -0
- package/src/router/service/lock.mjs +550 -0
- package/src/router/service/merge.mjs +255 -0
- package/src/router/service/model-resolve.mjs +350 -0
- package/src/router/service/paths.mjs +168 -0
- package/src/router/service/proposals.mjs +1461 -0
- package/src/router/service/queue.mjs +477 -0
- package/src/router/service/reconcile.mjs +271 -0
- package/src/router/service/service.mjs +2998 -0
- package/src/router/service/sources.mjs +352 -0
- package/src/router/service/status.mjs +520 -0
- package/src/router/service/store.mjs +476 -0
- package/src/router/service/ticklock.mjs +239 -0
- package/src/router/service/tokens.mjs +95 -0
- package/src/router/service/tools.mjs +248 -0
- package/src/router/service/wake.mjs +544 -0
- package/src/router/service/wal.mjs +685 -0
- package/src/router/shadow.mjs +821 -0
- package/src/surgery-recovery.js +1909 -0
- package/src/tool-output.js +485 -0
- package/src/tools.js +3262 -0
- package/src/truncate-visible.js +2416 -0
- package/src/truncate.js +999 -0
- package/src/ui/actions.js +1312 -0
- package/src/ui/flags.js +259 -0
- package/src/ui/panel-api.js +371 -0
- package/src/ui/panel-auth.js +251 -0
- package/src/ui/panel-integrity.js +243 -0
- package/src/ui/routes-readonly.js +759 -0
- package/src/ui/routes.js +284 -0
- package/src/util/append-store.js +368 -0
- package/src/util/local-day.js +30 -0
- package/src/util/redact.js +227 -0
- package/src/util/stale-locks.js +148 -0
- package/src/version.js +99 -0
- package/test/fixtures/pre-step-container.mjs +100 -0
- package/test/p1_probe.mjs +97 -0
|
@@ -0,0 +1,553 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// End-to-end drill for the resident session watchdog.
|
|
3
|
+
//
|
|
4
|
+
// It builds throwaway DSH homes under the OS temp directory, writes v4 session
|
|
5
|
+
// fixtures with the package's own encoder, and drives the real watcher + real
|
|
6
|
+
// truncate CLI through:
|
|
7
|
+
//
|
|
8
|
+
// check profile config is read; mode resolves to dryrun
|
|
9
|
+
// dryrun a planned cut; session bytes/hash do not move; receipt says so
|
|
10
|
+
// request profile mode flips to request; one real cut; backup + archive +
|
|
11
|
+
// receipt all present and verified
|
|
12
|
+
// kill profile enabled=false; the watcher does nothing
|
|
13
|
+
// live fresh live row; the watcher refuses and leaves the file alone
|
|
14
|
+
// corrupt an unparseable session is reported, but the process still exits 0
|
|
15
|
+
// loop a running loop notices enabled=false within one poll and exits on
|
|
16
|
+
// stop.flag
|
|
17
|
+
//
|
|
18
|
+
// No real session is read or written: every path is under --workdir, which
|
|
19
|
+
// defaults to a new directory under the OS temp dir. The drill never installs
|
|
20
|
+
// anything and never touches Task Scheduler.
|
|
21
|
+
//
|
|
22
|
+
// node scripts/session-watch-drill.mjs
|
|
23
|
+
// node scripts/session-watch-drill.mjs --workdir <drill-dir> --clean
|
|
24
|
+
//
|
|
25
|
+
// stdout is one JSON document; the same document is written to
|
|
26
|
+
// <workdir>/drill-result.json. Exit 0 = every check passed, 1 = a check failed,
|
|
27
|
+
// 2 = bad arguments.
|
|
28
|
+
|
|
29
|
+
import fs from 'node:fs';
|
|
30
|
+
import os from 'node:os';
|
|
31
|
+
import path from 'node:path';
|
|
32
|
+
import crypto from 'node:crypto';
|
|
33
|
+
import { spawn, spawnSync } from 'node:child_process';
|
|
34
|
+
import { fileURLToPath } from 'node:url';
|
|
35
|
+
|
|
36
|
+
import { parseSession } from '../src/codec.js';
|
|
37
|
+
import { sessionEstimate } from '../src/truncate-visible.js';
|
|
38
|
+
import { writeFixture, writeLiveRow, writeCorruptSession } from './lib/session-watch-fixture.mjs';
|
|
39
|
+
|
|
40
|
+
const HERE = path.dirname(fileURLToPath(import.meta.url));
|
|
41
|
+
const REPO = path.resolve(HERE, '..');
|
|
42
|
+
const WATCHER = path.join(HERE, 'session-watch.mjs');
|
|
43
|
+
const CLI = path.join(HERE, 'truncate-cli.mjs');
|
|
44
|
+
const NODE = process.execPath;
|
|
45
|
+
|
|
46
|
+
function usage() {
|
|
47
|
+
return [
|
|
48
|
+
'session-watch drill',
|
|
49
|
+
'',
|
|
50
|
+
' node scripts/session-watch-drill.mjs [--workdir <dir>] [--clean] [--verbose]',
|
|
51
|
+
'',
|
|
52
|
+
' --workdir where to build throwaway homes (default: OS temp)',
|
|
53
|
+
' --clean delete the workdir on success (default: keep evidence)',
|
|
54
|
+
' --verbose print each check to stderr',
|
|
55
|
+
].join('\n');
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function parseArgs(argv) {
|
|
59
|
+
const options = { workdir: null, clean: false, verbose: false, help: false };
|
|
60
|
+
for (let i = 0; i < argv.length; i += 1) {
|
|
61
|
+
const arg = argv[i];
|
|
62
|
+
if (arg === '--help' || arg === '-h') options.help = true;
|
|
63
|
+
else if (arg === '--clean') options.clean = true;
|
|
64
|
+
else if (arg === '--verbose') options.verbose = true;
|
|
65
|
+
else if (arg === '--workdir') {
|
|
66
|
+
const value = argv[i + 1];
|
|
67
|
+
if (!value || value.startsWith('--')) throw new Error('--workdir needs a value');
|
|
68
|
+
i += 1;
|
|
69
|
+
options.workdir = path.resolve(value);
|
|
70
|
+
} else throw new Error(`unknown argument: ${arg}`);
|
|
71
|
+
}
|
|
72
|
+
return options;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
function iso(now = Date.now()) {
|
|
76
|
+
return new Date(now).toISOString();
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
function sha256(buffer) {
|
|
80
|
+
return crypto.createHash('sha256').update(buffer).digest('hex');
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
function sha256File(file) {
|
|
84
|
+
return sha256(fs.readFileSync(file));
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function readJson(file) {
|
|
88
|
+
return JSON.parse(fs.readFileSync(file, 'utf8'));
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
function ensureDir(dir) {
|
|
92
|
+
fs.mkdirSync(dir, { recursive: true });
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
function listFiles(dir, predicate = () => true) {
|
|
96
|
+
if (!fs.existsSync(dir)) return [];
|
|
97
|
+
return fs.readdirSync(dir).filter(predicate).sort();
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
function safeName(value) {
|
|
101
|
+
return String(value).replace(/[^A-Za-z0-9._-]/g, '_');
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
function writeProfile(file, { enabled, mode, cooldownMinutes = 0 }) {
|
|
105
|
+
const yaml = [
|
|
106
|
+
'# Drill profile: the watcher reads only the autoTruncate block.',
|
|
107
|
+
'plugins:',
|
|
108
|
+
' session-longlive:',
|
|
109
|
+
' autoTruncate:',
|
|
110
|
+
` enabled: ${enabled}`,
|
|
111
|
+
` mode: ${mode}`,
|
|
112
|
+
' tokensThreshold: 1000',
|
|
113
|
+
' minTurns: 1',
|
|
114
|
+
' keepTurns: 4',
|
|
115
|
+
' keepTokens: 100000',
|
|
116
|
+
' firstUserCapChars: 4000',
|
|
117
|
+
' boundaryRetries: 3',
|
|
118
|
+
` cooldownMinutes: ${cooldownMinutes}`,
|
|
119
|
+
'',
|
|
120
|
+
].join('\n');
|
|
121
|
+
ensureDir(path.dirname(file));
|
|
122
|
+
fs.writeFileSync(file, yaml, 'utf8');
|
|
123
|
+
return yaml;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
function makeEnv(workdir, name, { enabled, mode, cooldownMinutes = 0 } = {}) {
|
|
127
|
+
const root = path.join(workdir, safeName(name));
|
|
128
|
+
const dshHome = path.join(root, 'dsh-home');
|
|
129
|
+
const sessionsRoot = path.join(dshHome, 'sessions');
|
|
130
|
+
const memoryRoot = path.join(root, 'memory');
|
|
131
|
+
const profilePath = path.join(root, 'profile', 'cordis.patch.yml');
|
|
132
|
+
const configPath = path.join(root, 'config.json');
|
|
133
|
+
ensureDir(sessionsRoot);
|
|
134
|
+
ensureDir(memoryRoot);
|
|
135
|
+
writeProfile(profilePath, { enabled, mode, cooldownMinutes });
|
|
136
|
+
const config = {
|
|
137
|
+
version: 1,
|
|
138
|
+
enabled: true,
|
|
139
|
+
mode: 'dryrun',
|
|
140
|
+
intervalSeconds: 1,
|
|
141
|
+
settleSeconds: 0,
|
|
142
|
+
tokensThreshold: 1000,
|
|
143
|
+
minTurns: 1,
|
|
144
|
+
keepTurns: 4,
|
|
145
|
+
keepTokens: 100000,
|
|
146
|
+
firstUserCapChars: 4000,
|
|
147
|
+
boundaryRetries: 3,
|
|
148
|
+
cooldownMinutes,
|
|
149
|
+
profileConfig: profilePath,
|
|
150
|
+
memoryRoot,
|
|
151
|
+
sessionsRoot,
|
|
152
|
+
dshHome,
|
|
153
|
+
nodeExe: NODE,
|
|
154
|
+
cliPath: CLI,
|
|
155
|
+
scanMaxFiles: 20,
|
|
156
|
+
cliTimeoutSeconds: 120,
|
|
157
|
+
logMaxBytes: 1024 * 1024,
|
|
158
|
+
};
|
|
159
|
+
fs.writeFileSync(configPath, `${JSON.stringify(config, null, 2)}\n`, 'utf8');
|
|
160
|
+
return {
|
|
161
|
+
name,
|
|
162
|
+
root,
|
|
163
|
+
dshHome,
|
|
164
|
+
sessionsRoot,
|
|
165
|
+
memoryRoot,
|
|
166
|
+
profilePath,
|
|
167
|
+
configPath,
|
|
168
|
+
config,
|
|
169
|
+
receiptDir: path.join(memoryRoot, '.longlive', 'session-watch', 'receipts'),
|
|
170
|
+
statePath: path.join(memoryRoot, '.longlive', 'session-watch', 'state.json'),
|
|
171
|
+
logPath: path.join(memoryRoot, '.longlive', 'logs', 'session-watch.log'),
|
|
172
|
+
lastTickPath: path.join(memoryRoot, '.longlive', 'session-watch', 'last-tick.json'),
|
|
173
|
+
stopFlag: path.join(memoryRoot, '.longlive', 'session-watch', 'stop.flag'),
|
|
174
|
+
};
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
function runWatcher(env, args) {
|
|
178
|
+
const started = Date.now();
|
|
179
|
+
const result = spawnSync(NODE, [WATCHER, '--config', env.configPath, ...args], {
|
|
180
|
+
cwd: REPO,
|
|
181
|
+
env: { ...process.env, DSH_HOME: env.dshHome, DSH_MEM: env.memoryRoot },
|
|
182
|
+
encoding: 'utf8',
|
|
183
|
+
windowsHide: true,
|
|
184
|
+
timeout: 120_000,
|
|
185
|
+
maxBuffer: 32 * 1024 * 1024,
|
|
186
|
+
});
|
|
187
|
+
let json = null;
|
|
188
|
+
try {
|
|
189
|
+
json = JSON.parse(String(result.stdout ?? '').trim());
|
|
190
|
+
} catch {
|
|
191
|
+
json = null;
|
|
192
|
+
}
|
|
193
|
+
return {
|
|
194
|
+
args,
|
|
195
|
+
exitCode: result.status,
|
|
196
|
+
signal: result.signal ?? null,
|
|
197
|
+
durationMs: Date.now() - started,
|
|
198
|
+
json,
|
|
199
|
+
stdout: String(result.stdout ?? ''),
|
|
200
|
+
stderr: String(result.stderr ?? ''),
|
|
201
|
+
};
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
function parseOk(file) {
|
|
205
|
+
try {
|
|
206
|
+
return sessionEstimate(parseSession(fs.readFileSync(file)));
|
|
207
|
+
} catch {
|
|
208
|
+
return null;
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
function backupFiles(sessionFile) {
|
|
213
|
+
const dir = path.dirname(sessionFile);
|
|
214
|
+
return listFiles(dir, (name) => name.includes('.bak-')).map((name) => path.join(dir, name));
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
function archiveFiles(sessionFile) {
|
|
218
|
+
const dir = `${sessionFile}.archive`;
|
|
219
|
+
return listFiles(dir).map((name) => path.join(dir, name));
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
function waitFor(fn, timeoutMs, intervalMs = 200) {
|
|
223
|
+
const deadline = Date.now() + timeoutMs;
|
|
224
|
+
return new Promise((resolve) => {
|
|
225
|
+
const poll = () => {
|
|
226
|
+
let value = false;
|
|
227
|
+
try { value = fn(); } catch { value = false; }
|
|
228
|
+
if (value) return resolve(true);
|
|
229
|
+
if (Date.now() >= deadline) return resolve(false);
|
|
230
|
+
setTimeout(poll, intervalMs);
|
|
231
|
+
};
|
|
232
|
+
poll();
|
|
233
|
+
});
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
function sleep(ms) {
|
|
237
|
+
return new Promise((resolve) => { setTimeout(resolve, ms); });
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
function createReport(verbose) {
|
|
241
|
+
const checks = [];
|
|
242
|
+
const phases = {};
|
|
243
|
+
const check = (phase, name, ok, detail = {}) => {
|
|
244
|
+
const entry = { phase, name, ok: Boolean(ok), ...detail };
|
|
245
|
+
checks.push(entry);
|
|
246
|
+
if (!ok || verbose) {
|
|
247
|
+
process.stderr.write(`${ok ? 'PASS' : 'FAIL'} ${phase}/${name}${detail && Object.keys(detail).length ? ` ${JSON.stringify(detail)}` : ''}\n`);
|
|
248
|
+
}
|
|
249
|
+
return Boolean(ok);
|
|
250
|
+
};
|
|
251
|
+
return { checks, phases, check };
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
// -------------------------------------------------------------------- phases
|
|
255
|
+
function phaseCheck(env, check) {
|
|
256
|
+
const run = runWatcher(env, ['--check']);
|
|
257
|
+
check('check', 'exit-0', run.exitCode === 0, { exitCode: run.exitCode, stderr: run.stderr.slice(0, 500) });
|
|
258
|
+
const json = run.json;
|
|
259
|
+
check('check', 'json-ok', Boolean(json?.ok === true));
|
|
260
|
+
check('check', 'mode-dryrun-from-profile', json?.mode === 'dryrun', { mode: json?.mode ?? null, source: json?.modeSource ?? null });
|
|
261
|
+
check('check', 'mode-source-profile', String(json?.modeSource ?? '').startsWith('profile:'), { source: json?.modeSource ?? null });
|
|
262
|
+
check('check', 'enabled', json?.enabled === true);
|
|
263
|
+
check('check', 'profile-path', path.resolve(String(json?.profileConfig ?? '')) === path.resolve(env.profilePath));
|
|
264
|
+
return run;
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
function phaseDryrun(env, fixture, check) {
|
|
268
|
+
const before = fs.readFileSync(fixture.file);
|
|
269
|
+
const beforeSha = sha256(before);
|
|
270
|
+
const beforeEstimate = parseOk(fixture.file);
|
|
271
|
+
const run = runWatcher(env, ['--once']);
|
|
272
|
+
check('dryrun', 'exit-0', run.exitCode === 0, { exitCode: run.exitCode });
|
|
273
|
+
const json = run.json;
|
|
274
|
+
check('dryrun', 'json-ok', Boolean(json?.ok === true), { status: json?.status ?? null });
|
|
275
|
+
check('dryrun', 'mode-dryrun', json?.mode === 'dryrun', { mode: json?.mode ?? null });
|
|
276
|
+
check('dryrun', 'scanned-1', json?.scanned === 1, { scanned: json?.scanned ?? null });
|
|
277
|
+
const action = json?.actions?.[0] ?? null;
|
|
278
|
+
check('dryrun', 'action-planned', action?.status === 'planned', { status: action?.status ?? null });
|
|
279
|
+
check('dryrun', 'action-verification', action?.verificationOk === true);
|
|
280
|
+
check('dryrun', 'estimates-available', Boolean(beforeEstimate && beforeEstimate.tokens > 0), { estimate: beforeEstimate });
|
|
281
|
+
const after = fs.readFileSync(fixture.file);
|
|
282
|
+
check('dryrun', 'session-bytes-unchanged', sha256(after) === beforeSha, { beforeSha, afterSha: sha256(after) });
|
|
283
|
+
check('dryrun', 'no-backup', backupFiles(fixture.file).length === 0, { backups: backupFiles(fixture.file) });
|
|
284
|
+
check('dryrun', 'no-archive', archiveFiles(fixture.file).length === 0, { archives: archiveFiles(fixture.file) });
|
|
285
|
+
check('dryrun', 'no-inflight', !fs.existsSync(path.join(path.dirname(fixture.file), '.truncate-visible-inflight.json')));
|
|
286
|
+
const receipts = listFiles(env.receiptDir, (name) => name.endsWith('.json'));
|
|
287
|
+
check('dryrun', 'receipt-written', receipts.length === 1, { receipts });
|
|
288
|
+
let receipt = null;
|
|
289
|
+
if (receipts.length > 0) receipt = readJson(path.join(env.receiptDir, receipts[0]));
|
|
290
|
+
check('dryrun', 'receipt-mode-dryrun', receipt?.mode === 'dryrun', { mode: receipt?.mode ?? null });
|
|
291
|
+
check('dryrun', 'receipt-wrote-session-false', receipt?.wroteSession === false);
|
|
292
|
+
check('dryrun', 'receipt-verification-ok', receipt?.verification?.ok === true);
|
|
293
|
+
return { run, receipt, beforeSha, beforeEstimate };
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
function phaseRequest(env, fixture, check, beforeSha) {
|
|
297
|
+
writeProfile(env.profilePath, { enabled: true, mode: 'request', cooldownMinutes: 0 });
|
|
298
|
+
const before = fs.readFileSync(fixture.file);
|
|
299
|
+
const beforeStat = fs.statSync(fixture.file);
|
|
300
|
+
const beforeReceipts = listFiles(env.receiptDir, (name) => name.endsWith('.json')).length;
|
|
301
|
+
const run = runWatcher(env, ['--once']);
|
|
302
|
+
check('request', 'exit-0', run.exitCode === 0, { exitCode: run.exitCode });
|
|
303
|
+
const json = run.json;
|
|
304
|
+
check('request', 'mode-request', json?.mode === 'request', { mode: json?.mode ?? null });
|
|
305
|
+
const action = json?.actions?.[0] ?? null;
|
|
306
|
+
check('request', 'action-truncated', action?.status === 'truncated', { status: action?.status ?? null, cliStatus: action?.cliStatus ?? null });
|
|
307
|
+
check('request', 'verification-ok', action?.verificationOk === true);
|
|
308
|
+
const after = fs.readFileSync(fixture.file);
|
|
309
|
+
const afterStat = fs.statSync(fixture.file);
|
|
310
|
+
check('request', 'bytes-shrunk', afterStat.size < beforeStat.size, {
|
|
311
|
+
beforeBytes: beforeStat.size, afterBytes: afterStat.size, actionBefore: action?.bytesBefore ?? null, actionAfter: action?.bytesAfter ?? null,
|
|
312
|
+
});
|
|
313
|
+
check('request', 'sha-changed', sha256(after) !== beforeSha);
|
|
314
|
+
const afterEstimate = parseOk(fixture.file);
|
|
315
|
+
check('request', 'still-parseable', Boolean(afterEstimate), { afterEstimate });
|
|
316
|
+
const backups = backupFiles(fixture.file);
|
|
317
|
+
check('request', 'backup-exists', backups.length === 1, { backups });
|
|
318
|
+
if (backups.length === 1) {
|
|
319
|
+
check('request', 'backup-hash-matches-before', sha256File(backups[0]) === beforeSha, {
|
|
320
|
+
backup: backups[0], backupSha: sha256File(backups[0]), beforeSha,
|
|
321
|
+
});
|
|
322
|
+
}
|
|
323
|
+
const archives = archiveFiles(fixture.file);
|
|
324
|
+
check('request', 'archive-exists', archives.length === 1, { archives });
|
|
325
|
+
check('request', 'no-inflight', !fs.existsSync(path.join(path.dirname(fixture.file), '.truncate-visible-inflight.json')));
|
|
326
|
+
const receipts = listFiles(env.receiptDir, (name) => name.endsWith('.json'));
|
|
327
|
+
check('request', 'one-new-receipt', receipts.length === beforeReceipts + 1, { beforeReceipts, receipts });
|
|
328
|
+
let receipt = null;
|
|
329
|
+
for (const name of receipts) {
|
|
330
|
+
const candidate = readJson(path.join(env.receiptDir, name));
|
|
331
|
+
if (candidate?.mode === 'request') receipt = candidate;
|
|
332
|
+
}
|
|
333
|
+
check('request', 'receipt-mode-request', receipt?.mode === 'request', { mode: receipt?.mode ?? null });
|
|
334
|
+
check('request', 'receipt-wrote-session', receipt?.wroteSession === true);
|
|
335
|
+
check('request', 'receipt-verification-ok', receipt?.verification?.ok === true);
|
|
336
|
+
return { run, receipt, afterBytes: afterStat.size, afterEstimate, backups, archives };
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
function phaseKillSwitch(workdir, check) {
|
|
340
|
+
const env = makeEnv(workdir, 'kill-switch', { enabled: false, mode: 'request' });
|
|
341
|
+
const fixture = writeFixture({ sessionsRoot: env.sessionsRoot, id: 'session-drill-kill', turns: 20, padding: 500 });
|
|
342
|
+
const beforeSha = sha256File(fixture.file);
|
|
343
|
+
const run = runWatcher(env, ['--once']);
|
|
344
|
+
check('kill', 'exit-0', run.exitCode === 0, { exitCode: run.exitCode });
|
|
345
|
+
check('kill', 'status-disabled', run.json?.status === 'disabled', { status: run.json?.status ?? null });
|
|
346
|
+
check('kill', 'mode-request-but-disabled', run.json?.mode === 'request' && run.json?.enabled === false, {
|
|
347
|
+
mode: run.json?.mode ?? null, enabled: run.json?.enabled ?? null,
|
|
348
|
+
});
|
|
349
|
+
check('kill', 'no-receipt', listFiles(env.receiptDir).length === 0);
|
|
350
|
+
check('kill', 'session-unchanged', sha256File(fixture.file) === beforeSha);
|
|
351
|
+
check('kill', 'no-backup', backupFiles(fixture.file).length === 0);
|
|
352
|
+
return { env, fixture, run };
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
function phaseLive(workdir, check) {
|
|
356
|
+
const env = makeEnv(workdir, 'live-block', { enabled: true, mode: 'request' });
|
|
357
|
+
const fixture = writeFixture({ sessionsRoot: env.sessionsRoot, id: 'session-drill-live', turns: 20, padding: 500 });
|
|
358
|
+
const live = writeLiveRow({ memoryRoot: env.memoryRoot, sessionId: 'session-drill-live', state: 'running' });
|
|
359
|
+
const beforeSha = sha256File(fixture.file);
|
|
360
|
+
const run = runWatcher(env, ['--once']);
|
|
361
|
+
check('live', 'exit-0', run.exitCode === 0, { exitCode: run.exitCode });
|
|
362
|
+
check('live', 'no-action', (run.json?.actions ?? []).length === 0, { actions: run.json?.actions ?? [] });
|
|
363
|
+
const blocked = run.json?.blocked ?? [];
|
|
364
|
+
check('live', 'blocked-health', blocked.length === 1 && blocked[0]?.stage === 'health', { blocked });
|
|
365
|
+
check('live', 'blocked-by-live-registry', (blocked[0]?.reasons ?? []).some((reason) => reason.signal === 'live-registry' && reason.state === 'running'), { blocked });
|
|
366
|
+
check('live', 'session-unchanged', sha256File(fixture.file) === beforeSha);
|
|
367
|
+
check('live', 'no-receipt', listFiles(env.receiptDir).length === 0);
|
|
368
|
+
check('live', 'live-row-exists', fs.existsSync(live.file));
|
|
369
|
+
return { env, fixture, run, live };
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
function phaseCorrupt(workdir, check) {
|
|
373
|
+
const env = makeEnv(workdir, 'corrupt-fail-soft', { enabled: true, mode: 'request' });
|
|
374
|
+
writeCorruptSession({ sessionsRoot: env.sessionsRoot, id: 'session-drill-corrupt' });
|
|
375
|
+
const run = runWatcher(env, ['--once']);
|
|
376
|
+
check('corrupt', 'exit-0-fail-soft', run.exitCode === 0, { exitCode: run.exitCode });
|
|
377
|
+
check('corrupt', 'degraded-not-crash', run.json?.ok === false && run.json?.status === 'degraded', {
|
|
378
|
+
runOk: run.json?.ok ?? null, status: run.json?.status ?? null,
|
|
379
|
+
});
|
|
380
|
+
const parseError = (run.json?.errors ?? []).find((error) => error.stage === 'parse');
|
|
381
|
+
check('corrupt', 'parse-error-reported', Boolean(parseError), { errors: run.json?.errors ?? [] });
|
|
382
|
+
check('corrupt', 'no-action', (run.json?.actions ?? []).length === 0);
|
|
383
|
+
check('corrupt', 'log-written', fs.existsSync(env.logPath));
|
|
384
|
+
return { env, run };
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
async function phaseLoop(workdir, check) {
|
|
388
|
+
const env = makeEnv(workdir, 'loop-kill', { enabled: true, mode: 'dryrun', cooldownMinutes: 60 });
|
|
389
|
+
const fixture = writeFixture({ sessionsRoot: env.sessionsRoot, id: 'session-drill-loop', turns: 20, padding: 500 });
|
|
390
|
+
const stdoutPath = path.join(env.root, 'loop.stdout.log');
|
|
391
|
+
const stderrPath = path.join(env.root, 'loop.stderr.log');
|
|
392
|
+
const out = fs.openSync(stdoutPath, 'w');
|
|
393
|
+
const err = fs.openSync(stderrPath, 'w');
|
|
394
|
+
const child = spawn(NODE, [WATCHER, '--config', env.configPath, '--loop'], {
|
|
395
|
+
cwd: REPO,
|
|
396
|
+
env: { ...process.env, DSH_HOME: env.dshHome, DSH_MEM: env.memoryRoot },
|
|
397
|
+
stdio: ['ignore', out, err],
|
|
398
|
+
windowsHide: true,
|
|
399
|
+
});
|
|
400
|
+
fs.closeSync(out);
|
|
401
|
+
fs.closeSync(err);
|
|
402
|
+
const exitPromise = new Promise((resolve) => {
|
|
403
|
+
child.once('exit', (code, signal) => resolve({ code, signal }));
|
|
404
|
+
child.once('error', (error) => resolve({ code: null, signal: null, error: String(error?.message ?? error) }));
|
|
405
|
+
});
|
|
406
|
+
|
|
407
|
+
const firstTick = await waitFor(() => fs.existsSync(env.lastTickPath), 15_000);
|
|
408
|
+
check('loop', 'first-tick', firstTick, { lastTickPath: env.lastTickPath });
|
|
409
|
+
const firstReceipts = listFiles(env.receiptDir, (name) => name.endsWith('.json'));
|
|
410
|
+
check('loop', 'first-dryrun-receipt', firstReceipts.length >= 1, { receipts: firstReceipts });
|
|
411
|
+
|
|
412
|
+
writeProfile(env.profilePath, { enabled: false, mode: 'dryrun', cooldownMinutes: 60 });
|
|
413
|
+
const disabledLogged = await waitFor(() => {
|
|
414
|
+
if (!fs.existsSync(env.logPath)) return false;
|
|
415
|
+
return fs.readFileSync(env.logPath, 'utf8').includes('"event":"disabled"');
|
|
416
|
+
}, 12_000, 300);
|
|
417
|
+
check('loop', 'disabled-observed', disabledLogged);
|
|
418
|
+
const countAtDisable = listFiles(env.receiptDir, (name) => name.endsWith('.json')).length;
|
|
419
|
+
await sleep(6_000);
|
|
420
|
+
const countAfterWait = listFiles(env.receiptDir, (name) => name.endsWith('.json')).length;
|
|
421
|
+
check('loop', 'no-receipt-while-disabled', countAfterWait === countAtDisable, { countAtDisable, countAfterWait });
|
|
422
|
+
|
|
423
|
+
fs.writeFileSync(env.stopFlag, `${iso()}\n`, 'utf8');
|
|
424
|
+
const exited = await Promise.race([
|
|
425
|
+
exitPromise,
|
|
426
|
+
sleep(15_000).then(() => ({ timeout: true })),
|
|
427
|
+
]);
|
|
428
|
+
check('loop', 'stop-flag-exits', !exited.timeout, { exit: exited });
|
|
429
|
+
if (exited.timeout) {
|
|
430
|
+
try { child.kill(); } catch { /* already gone */ }
|
|
431
|
+
} else {
|
|
432
|
+
check('loop', 'exit-code-0', exited.code === 0, { exit: exited });
|
|
433
|
+
}
|
|
434
|
+
return {
|
|
435
|
+
env,
|
|
436
|
+
fixture,
|
|
437
|
+
firstReceipts,
|
|
438
|
+
countAtDisable,
|
|
439
|
+
countAfterWait,
|
|
440
|
+
exit: exited,
|
|
441
|
+
stdoutPath,
|
|
442
|
+
stderrPath,
|
|
443
|
+
};
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
// ---------------------------------------------------------------------- main
|
|
447
|
+
async function main() {
|
|
448
|
+
let options;
|
|
449
|
+
try {
|
|
450
|
+
options = parseArgs(process.argv.slice(2));
|
|
451
|
+
} catch (error) {
|
|
452
|
+
process.stderr.write(`${error.message}\n`);
|
|
453
|
+
process.exitCode = 2;
|
|
454
|
+
return;
|
|
455
|
+
}
|
|
456
|
+
if (options.help) {
|
|
457
|
+
process.stdout.write(`${usage()}\n`);
|
|
458
|
+
return;
|
|
459
|
+
}
|
|
460
|
+
const startedAt = Date.now();
|
|
461
|
+
const stamp = new Date(startedAt).toISOString().replace(/[-:]/g, '').replace(/\..*$/, '').replace('T', '-');
|
|
462
|
+
const workdir = options.workdir ?? path.join(os.tmpdir(), `dsh-session-watch-drill-${stamp}`);
|
|
463
|
+
if (fs.existsSync(workdir)) {
|
|
464
|
+
const entries = fs.readdirSync(workdir);
|
|
465
|
+
if (entries.length > 0) {
|
|
466
|
+
throw new Error(`workdir is not empty: ${workdir}; pass a new --workdir or clean it first`);
|
|
467
|
+
}
|
|
468
|
+
}
|
|
469
|
+
ensureDir(workdir);
|
|
470
|
+
|
|
471
|
+
const report = createReport(options.verbose);
|
|
472
|
+
const { check } = report;
|
|
473
|
+
report.workdir = workdir;
|
|
474
|
+
report.startedAt = iso(startedAt);
|
|
475
|
+
report.node = NODE;
|
|
476
|
+
report.watcher = WATCHER;
|
|
477
|
+
report.cli = CLI;
|
|
478
|
+
|
|
479
|
+
// 1. config gate -----------------------------------------------------------------
|
|
480
|
+
const dryEnv = makeEnv(workdir, 'dry-run', { enabled: true, mode: 'dryrun' });
|
|
481
|
+
const fixture = writeFixture({ sessionsRoot: dryEnv.sessionsRoot, id: 'session-drill-dry', turns: 20, padding: 500 });
|
|
482
|
+
report.phases.check = phaseCheck(dryEnv, check);
|
|
483
|
+
report.fixture = { path: fixture.file, bytes: fixture.bytes, turns: 20, padding: 500 };
|
|
484
|
+
|
|
485
|
+
// 2. dry-run must not write -------------------------------------------------------
|
|
486
|
+
const dry = phaseDryrun(dryEnv, fixture, check);
|
|
487
|
+
report.phases.dryrun = { exitCode: dry.run.exitCode, action: dry.run.json?.actions?.[0] ?? null, receipt: dry.receipt };
|
|
488
|
+
|
|
489
|
+
// 3. request must write once ------------------------------------------------------
|
|
490
|
+
const request = phaseRequest(dryEnv, fixture, check, dry.beforeSha);
|
|
491
|
+
report.phases.request = {
|
|
492
|
+
exitCode: request.run.exitCode,
|
|
493
|
+
action: request.run.json?.actions?.[0] ?? null,
|
|
494
|
+
receipt: request.receipt,
|
|
495
|
+
bytesAfter: request.afterBytes,
|
|
496
|
+
afterEstimate: request.afterEstimate,
|
|
497
|
+
backups: request.backups,
|
|
498
|
+
archives: request.archives,
|
|
499
|
+
};
|
|
500
|
+
|
|
501
|
+
// 4. kill switch ------------------------------------------------------------------
|
|
502
|
+
const kill = phaseKillSwitch(workdir, check);
|
|
503
|
+
report.phases.kill = { env: kill.env.root, run: kill.run.json };
|
|
504
|
+
|
|
505
|
+
// 5. live row ---------------------------------------------------------------------
|
|
506
|
+
const live = phaseLive(workdir, check);
|
|
507
|
+
report.phases.live = { env: live.env.root, run: live.run.json, liveRow: live.live.file };
|
|
508
|
+
|
|
509
|
+
// 6. corrupt session: noisy, not fatal --------------------------------------------
|
|
510
|
+
const corrupt = phaseCorrupt(workdir, check);
|
|
511
|
+
report.phases.corrupt = { env: corrupt.env.root, run: corrupt.run.json };
|
|
512
|
+
|
|
513
|
+
// 7. loop: enabled=false takes effect without a restart ---------------------------
|
|
514
|
+
const loop = await phaseLoop(workdir, check);
|
|
515
|
+
report.phases.loop = {
|
|
516
|
+
env: loop.env.root,
|
|
517
|
+
firstReceipts: loop.firstReceipts,
|
|
518
|
+
countAtDisable: loop.countAtDisable,
|
|
519
|
+
countAfterWait: loop.countAfterWait,
|
|
520
|
+
exit: loop.exit,
|
|
521
|
+
};
|
|
522
|
+
|
|
523
|
+
report.finishedAt = iso();
|
|
524
|
+
report.durationMs = Date.now() - startedAt;
|
|
525
|
+
report.checks = report.checks;
|
|
526
|
+
report.failed = report.checks.filter((entry) => !entry.ok);
|
|
527
|
+
report.ok = report.failed.length === 0;
|
|
528
|
+
|
|
529
|
+
const resultPath = path.join(workdir, 'drill-result.json');
|
|
530
|
+
fs.writeFileSync(resultPath, `${JSON.stringify(report, null, 2)}\n`, 'utf8');
|
|
531
|
+
report.resultPath = resultPath;
|
|
532
|
+
|
|
533
|
+
if (options.clean && report.ok) {
|
|
534
|
+
const tmpRoot = path.resolve(os.tmpdir());
|
|
535
|
+
const resolved = path.resolve(workdir);
|
|
536
|
+
const insideTmp = resolved.toLowerCase().startsWith(tmpRoot.toLowerCase() + path.sep);
|
|
537
|
+
if (!insideTmp || !path.basename(resolved).startsWith('dsh-session-watch-drill-')) {
|
|
538
|
+
process.stderr.write(`refusing to clean a non-drill directory: ${resolved}\n`);
|
|
539
|
+
} else {
|
|
540
|
+
fs.rmSync(resolved, { recursive: true, force: true });
|
|
541
|
+
process.stdout.write(`${JSON.stringify({ ok: true, cleaned: resolved, checks: report.checks.length }, null, 2)}\n`);
|
|
542
|
+
return;
|
|
543
|
+
}
|
|
544
|
+
}
|
|
545
|
+
|
|
546
|
+
process.stdout.write(`${JSON.stringify(report, null, 2)}\n`);
|
|
547
|
+
process.exitCode = report.ok ? 0 : 1;
|
|
548
|
+
}
|
|
549
|
+
|
|
550
|
+
main().catch((error) => {
|
|
551
|
+
process.stderr.write(`${error?.stack ?? error}\n`);
|
|
552
|
+
process.exitCode = 1;
|
|
553
|
+
});
|