@abdulr7man/pensieve 0.0.0-stage → 1.2.2
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/LICENSE +684 -0
- package/README.md +180 -2
- package/VERSION +1 -0
- package/agents/README.md +16 -0
- package/agents/pensieve-code-auditor.md +58 -0
- package/agents/pensieve-mapper.md +46 -0
- package/agents/pensieve-planner.md +67 -0
- package/agents/pensieve-reconner.md +76 -0
- package/agents/pensieve-reporter.md +61 -0
- package/agents/pensieve-roe-checker.md +46 -0
- package/agents/pensieve-scoper.md +77 -0
- package/agents/pensieve-tester.md +83 -0
- package/agents/pensieve-validator.md +62 -0
- package/bin/pensieve +452 -0
- package/bin/pensieve-installer.cjs +5 -0
- package/commands/pensieve-close.md +6 -0
- package/commands/pensieve-map.md +6 -0
- package/commands/pensieve-new-engagement.md +6 -0
- package/commands/pensieve-pause.md +15 -0
- package/commands/pensieve-plan.md +6 -0
- package/commands/pensieve-progress.md +34 -0
- package/commands/pensieve-quick.md +8 -0
- package/commands/pensieve-recon.md +6 -0
- package/commands/pensieve-report.md +6 -0
- package/commands/pensieve-resume.md +6 -0
- package/commands/pensieve-retest.md +10 -0
- package/commands/pensieve-scope.md +49 -0
- package/commands/pensieve-surface.md +8 -0
- package/commands/pensieve-test.md +6 -0
- package/commands/pensieve-validate.md +6 -0
- package/docker/Dockerfile.pensieve-toolbox +75 -0
- package/docker/compose.pensieve-net.yml +37 -0
- package/docker/squid.conf.template +21 -0
- package/package.json +39 -4
- package/packs/ad/ad-killchain/checklist.md +64 -0
- package/packs/ad/ad-killchain/pack.yaml +19 -0
- package/packs/cloud/cloud-cis/checklist.md +66 -0
- package/packs/cloud/cloud-cis/pack.yaml +20 -0
- package/packs/fintech/practice-core/checklist.md +33 -0
- package/packs/fintech/practice-core/learnings.md +5 -0
- package/packs/fintech/practice-core/pack.yaml +18 -0
- package/packs/infra/ptes-infra/checklist.md +22 -0
- package/packs/infra/ptes-infra/pack.yaml +19 -0
- package/packs/mobile/owasp-masvs/checklist.md +80 -0
- package/packs/mobile/owasp-masvs/pack.yaml +20 -0
- package/packs/owasp-api-v4/checklist.md +85 -0
- package/packs/owasp-api-v4/learnings.md +6 -0
- package/packs/owasp-api-v4/pack.yaml +23 -0
- package/packs/owasp-api-v4/runbooks/API1/SKILL.md +66 -0
- package/packs/owasp-api-v4/runbooks/API10/SKILL.md +67 -0
- package/packs/owasp-api-v4/runbooks/API2/SKILL.md +129 -0
- package/packs/owasp-api-v4/runbooks/API3/SKILL.md +55 -0
- package/packs/owasp-api-v4/runbooks/API4/SKILL.md +59 -0
- package/packs/owasp-api-v4/runbooks/API5/SKILL.md +60 -0
- package/packs/owasp-api-v4/runbooks/API6/SKILL.md +63 -0
- package/packs/owasp-api-v4/runbooks/API7/SKILL.md +70 -0
- package/packs/owasp-api-v4/runbooks/API8/SKILL.md +72 -0
- package/packs/owasp-api-v4/runbooks/API9/SKILL.md +66 -0
- package/packs/thickclient/thickclient-core/checklist.md +60 -0
- package/packs/thickclient/thickclient-core/pack.yaml +24 -0
- package/packs/web/asvs-l2/checklist.md +50 -0
- package/packs/web/asvs-l2/learnings.md +6 -0
- package/packs/web/asvs-l2/pack.yaml +19 -0
- package/packs/web/asvs-l2/runbooks/V2-AUTH-ENUM/SKILL.md +75 -0
- package/packs/web/asvs-l2/runbooks/V2-AUTH-LIVE/SKILL.md +78 -0
- package/packs/web/asvs-l2/runbooks/V2-AUTH-MFA/SKILL.md +76 -0
- package/packs/web/asvs-l2/runbooks/V2-AUTH-RATE/SKILL.md +72 -0
- package/packs/web/owasp-testing-checklist/checklist.md +52 -0
- package/packs/web/owasp-testing-checklist/learnings.md +47 -0
- package/packs/web/owasp-testing-checklist/pack.yaml +21 -0
- package/packs/web/unauth-perimeter/checklist.md +72 -0
- package/packs/web/unauth-perimeter/pack.yaml +18 -0
- package/packs/web/unauth-perimeter/runbooks/PERIM-01/SKILL.md +60 -0
- package/packs/web/unauth-perimeter/runbooks/PERIM-02/SKILL.md +63 -0
- package/packs/web/unauth-perimeter/runbooks/PERIM-03/SKILL.md +62 -0
- package/packs/web/unauth-perimeter/runbooks/PERIM-04/SKILL.md +75 -0
- package/packs/web/unauth-perimeter/runbooks/PERIM-05/SKILL.md +66 -0
- package/packs/web/unauth-perimeter/runbooks/PERIM-06/SKILL.md +59 -0
- package/packs/web/unauth-perimeter/runbooks/PERIM-07/SKILL.md +56 -0
- package/packs/web/unauth-perimeter/runbooks/PERIM-08/SKILL.md +57 -0
- package/packs/web/unauth-perimeter/runbooks/PERIM-09/SKILL.md +62 -0
- package/packs/web/unauth-perimeter/runbooks/PERIM-10/SKILL.md +60 -0
- package/packs/whitebox/code-audit-core/checklist.md +36 -0
- package/packs/whitebox/code-audit-core/learnings.md +6 -0
- package/packs/whitebox/code-audit-core/pack.yaml +14 -0
- package/prompts/orchestrator-core.md +52 -0
- package/reference/attack-playbooks.md +57 -0
- package/reference/otp-relay.md +98 -0
- package/reference/param-profiles.md +48 -0
- package/reference/templates/FINDING.md +97 -0
- package/reference/tool-selection.md +64 -0
- package/reference/vulnerability-finding-standard.md +80 -0
- package/runtimes/hermes/hooks/injection-scan.py +290 -0
- package/runtimes/hermes/hooks/scope-guard.py +136 -0
- package/skills/pensieve/SKILL.md +134 -0
- package/skills/pensieve-close/SKILL.md +31 -0
- package/skills/pensieve-map/SKILL.md +25 -0
- package/skills/pensieve-new-engagement/SKILL.md +28 -0
- package/skills/pensieve-pause/SKILL.md +22 -0
- package/skills/pensieve-plan/SKILL.md +39 -0
- package/skills/pensieve-progress/SKILL.md +35 -0
- package/skills/pensieve-quick/SKILL.md +47 -0
- package/skills/pensieve-recon/SKILL.md +30 -0
- package/skills/pensieve-report/SKILL.md +46 -0
- package/skills/pensieve-resume/SKILL.md +33 -0
- package/skills/pensieve-retest/SKILL.md +35 -0
- package/skills/pensieve-scope/SKILL.md +35 -0
- package/skills/pensieve-surface/SKILL.md +36 -0
- package/skills/pensieve-test/SKILL.md +35 -0
- package/skills/pensieve-validate/SKILL.md +28 -0
- package/skills/pensieve-wiki/SKILL.md +39 -0
- package/src/installer/cli.cjs +209 -0
- package/src/installer/home.cjs +155 -0
- package/src/installer/runtimes.cjs +276 -0
- package/src/installer/uninstall.cjs +342 -0
- package/src/pensieve-sdk/bin/pensieve-sdk.cjs +1045 -0
- package/src/pensieve-sdk/lib/agent-translate.cjs +50 -0
- package/src/pensieve-sdk/lib/approvals.cjs +408 -0
- package/src/pensieve-sdk/lib/audit.cjs +290 -0
- package/src/pensieve-sdk/lib/checklist.cjs +337 -0
- package/src/pensieve-sdk/lib/clientlist.cjs +814 -0
- package/src/pensieve-sdk/lib/close.cjs +133 -0
- package/src/pensieve-sdk/lib/config.cjs +156 -0
- package/src/pensieve-sdk/lib/cvss.cjs +91 -0
- package/src/pensieve-sdk/lib/env.cjs +272 -0
- package/src/pensieve-sdk/lib/evidence.cjs +217 -0
- package/src/pensieve-sdk/lib/findings.cjs +1096 -0
- package/src/pensieve-sdk/lib/frontmatter.cjs +178 -0
- package/src/pensieve-sdk/lib/graph.cjs +607 -0
- package/src/pensieve-sdk/lib/ids.cjs +63 -0
- package/src/pensieve-sdk/lib/init.cjs +79 -0
- package/src/pensieve-sdk/lib/liveness.cjs +176 -0
- package/src/pensieve-sdk/lib/loop.cjs +619 -0
- package/src/pensieve-sdk/lib/migrate.cjs +57 -0
- package/src/pensieve-sdk/lib/monitor.cjs +69 -0
- package/src/pensieve-sdk/lib/pack.cjs +607 -0
- package/src/pensieve-sdk/lib/paths.cjs +73 -0
- package/src/pensieve-sdk/lib/pdf.cjs +291 -0
- package/src/pensieve-sdk/lib/recon.cjs +296 -0
- package/src/pensieve-sdk/lib/renderers/__pycache__/formal_report.cpython-314.pyc +0 -0
- package/src/pensieve-sdk/lib/renderers/formal_report.py +608 -0
- package/src/pensieve-sdk/lib/report.cjs +1135 -0
- package/src/pensieve-sdk/lib/runbook.cjs +124 -0
- package/src/pensieve-sdk/lib/sarif.cjs +154 -0
- package/src/pensieve-sdk/lib/scope.cjs +621 -0
- package/src/pensieve-sdk/lib/security.cjs +61 -0
- package/src/pensieve-sdk/lib/skills.cjs +193 -0
- package/src/pensieve-sdk/lib/state.cjs +610 -0
- package/src/pensieve-sdk/lib/subagent.cjs +658 -0
- package/src/pensieve-sdk/lib/target.cjs +149 -0
- package/src/pensieve-sdk/lib/tools.cjs +66 -0
- package/src/pensieve-sdk/lib/vault.cjs +122 -0
- package/src/pensieve-sdk/lib/vendor/cvss40.LICENSE +22 -0
- package/src/pensieve-sdk/lib/vendor/cvss40.js +1208 -0
- package/src/pensieve-sdk/package.json +11 -0
- package/templates/ACCOUNTS.md +35 -0
- package/templates/ATTACK-SURFACE.md +30 -0
- package/templates/FINDING.md +43 -0
- package/templates/PLAN.md +33 -0
- package/templates/PROJECT.md +29 -0
- package/templates/ROE.md +50 -0
- package/templates/RUN-LOG.md +19 -0
- package/templates/SCOPE.md +32 -0
- package/templates/VALIDATION.md +62 -0
- package/templates/report/REPORT.md +49 -0
- package/workflows/close.md +34 -0
- package/workflows/map.md +39 -0
- package/workflows/new-engagement.md +37 -0
- package/workflows/plan.md +45 -0
- package/workflows/progress.md +28 -0
- package/workflows/quick.md +67 -0
- package/workflows/recon.md +87 -0
- package/workflows/report.md +51 -0
- package/workflows/resume.md +26 -0
- package/workflows/retest.md +53 -0
- package/workflows/scope.md +47 -0
- package/workflows/surface.md +29 -0
- package/workflows/test.md +95 -0
- package/workflows/validate.md +34 -0
|
@@ -0,0 +1,610 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
/**
|
|
3
|
+
* state.cjs — engagement registry + current-engagement resolution + STATE.md status.
|
|
4
|
+
* Registry lives at $PENSIEVE_HOME/engagements.json (default ~/.pensieve/).
|
|
5
|
+
* Flat layout (v0.6 01-01): STATE.md lives at the engagement root; the
|
|
6
|
+
* engagement marker is STATE.md WITH a status frontmatter field — the old
|
|
7
|
+
* .pentest/ directory existence check is gone.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
const fs = require('node:fs');
|
|
11
|
+
const os = require('node:os');
|
|
12
|
+
const path = require('node:path');
|
|
13
|
+
const { spawnSync } = require('node:child_process');
|
|
14
|
+
const fm = require('./frontmatter.cjs');
|
|
15
|
+
const paths = require('./paths.cjs');
|
|
16
|
+
const approvals = require('./approvals.cjs');
|
|
17
|
+
|
|
18
|
+
function pensieveHome() {
|
|
19
|
+
return paths.pensieveHome();
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function registryFile() {
|
|
23
|
+
return path.join(pensieveHome(), 'engagements.json');
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function readRegistry() {
|
|
27
|
+
try {
|
|
28
|
+
return JSON.parse(fs.readFileSync(registryFile(), 'utf8'));
|
|
29
|
+
} catch {
|
|
30
|
+
return {};
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function writeRegistry(reg) {
|
|
35
|
+
fs.mkdirSync(pensieveHome(), { recursive: true });
|
|
36
|
+
fs.writeFileSync(registryFile(), JSON.stringify(reg, null, 2) + '\n');
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function register(engagementPath, id) {
|
|
40
|
+
const reg = readRegistry();
|
|
41
|
+
reg[id] = { id, path: engagementPath, registered_at: new Date().toISOString() };
|
|
42
|
+
writeRegistry(reg);
|
|
43
|
+
return reg[id];
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* list — registry entries + operational detail (M4 workstreams):
|
|
48
|
+
* status — STATE.md status, or null when the engagement dir is gone
|
|
49
|
+
* running — true when any of its containers are up (docker ps, one call)
|
|
50
|
+
*/
|
|
51
|
+
function list() {
|
|
52
|
+
const reg = readRegistry();
|
|
53
|
+
let containers = [];
|
|
54
|
+
try {
|
|
55
|
+
const out = spawnSync('docker', ['ps', '--format', '{{.Names}}'], { encoding: 'utf8' });
|
|
56
|
+
containers = (out.stdout || '').split('\n');
|
|
57
|
+
} catch { containers = []; } // docker absent: running=false everywhere
|
|
58
|
+
return Object.values(reg).map((e) => {
|
|
59
|
+
let status = null;
|
|
60
|
+
try { status = readStatus(e.path); } catch { /* dir moved or deleted */ }
|
|
61
|
+
const running = containers.some((c) => c.startsWith(`pensieve-${e.id}-`));
|
|
62
|
+
return { ...e, status, running };
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* isEngagement(dir) — the flat-layout engagement marker: STATE.md at the dir
|
|
68
|
+
* root whose frontmatter carries a status field. (Legacy .pentest/ dirs are
|
|
69
|
+
* NOT detected — run engagement.migrate on them first.)
|
|
70
|
+
*/
|
|
71
|
+
function isEngagement(dir) {
|
|
72
|
+
try {
|
|
73
|
+
const text = fs.readFileSync(paths.stateFile(dir), 'utf8');
|
|
74
|
+
return fm.get(text, 'status') != null;
|
|
75
|
+
} catch {
|
|
76
|
+
return false;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
function current(startDir) {
|
|
81
|
+
let dir = path.resolve(startDir || process.cwd());
|
|
82
|
+
while (true) {
|
|
83
|
+
if (isEngagement(dir)) return dir;
|
|
84
|
+
const parent = path.dirname(dir);
|
|
85
|
+
if (parent === dir) return null;
|
|
86
|
+
dir = parent;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
// lifecycle order (workflows advance it; retest/close = M3 lanes).
|
|
91
|
+
// v0.6 01-03 (brief 1.5 / ADR-012): the white-box lane — `validated-static`
|
|
92
|
+
// sits between validating and reporting. It is an optional middle stop for
|
|
93
|
+
// static-only (white-box) validation; the dynamic lane (validating ->
|
|
94
|
+
// reporting) is preserved explicitly below.
|
|
95
|
+
const STATUSES = ['initialized', 'scoped', 'recon', 'mapped', 'planned', 'testing', 'validating', 'validated-static', 'reporting', 'delivered', 'retesting', 'closed'];
|
|
96
|
+
|
|
97
|
+
// cold-resume support: what a fresh agent should do from each status
|
|
98
|
+
const NEXT_HINTS = {
|
|
99
|
+
initialized: 'run /pensieve-scope (interview → SCOPE/ROE → human approval)',
|
|
100
|
+
scoped: 'run /pensieve-recon (env up + containment self-check first)',
|
|
101
|
+
recon: 'run /pensieve-map (consolidate recon/ into ATTACK-SURFACE.md)',
|
|
102
|
+
mapped: 'run /pensieve-plan (packs → plans → tools.check → human approval)',
|
|
103
|
+
planned: 'run /pensieve-test (execute approved plans, evidence every claim)',
|
|
104
|
+
testing: 'run /pensieve-validate when plans are executed (independent replay)',
|
|
105
|
+
validating: 'run /pensieve-report when the register is settled (findings.gate)',
|
|
106
|
+
'validated-static': 'white-box lane: static validation done — run /pensieve-report (findings.gate)',
|
|
107
|
+
reporting: 'human approves report → state delivered',
|
|
108
|
+
delivered: 'fixes landed → /pensieve-retest; no retest needed → /pensieve-close',
|
|
109
|
+
retesting: 'verdicts recorded (findings.retest-record) → /pensieve-close',
|
|
110
|
+
closed: 'engagement archived; registry has the closed record',
|
|
111
|
+
};
|
|
112
|
+
|
|
113
|
+
// ---- checkpoint IDs (v0.6 05-01 / LangGraph-style checkpointer, file-backed) ----
|
|
114
|
+
// Every REAL status transition is a monotonic checkpoint. The id lives in
|
|
115
|
+
// STATE.md frontmatter (fast read) and the full audit record is appended to
|
|
116
|
+
// STATE_HISTORY.jsonl beside it (co-located with the engagement, per the
|
|
117
|
+
// engagements-stay-in-cwd rule). Idempotent re-sets are NOT checkpoints.
|
|
118
|
+
function historyFile(engagementDir) {
|
|
119
|
+
return path.join(path.resolve(engagementDir), 'STATE_HISTORY.jsonl');
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
function readHistory(engagementDir) {
|
|
123
|
+
const f = historyFile(engagementDir);
|
|
124
|
+
if (!fs.existsSync(f)) return [];
|
|
125
|
+
return fs.readFileSync(f, 'utf8')
|
|
126
|
+
.split('\n')
|
|
127
|
+
.filter(Boolean)
|
|
128
|
+
.map((l) => { try { return JSON.parse(l); } catch { return null; } })
|
|
129
|
+
.filter(Boolean);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
// next id = last history record, floored by any checkpoint already in the
|
|
133
|
+
// frontmatter (survives a deleted history file), +1. Monotonic per engagement.
|
|
134
|
+
function nextCheckpoint(engagementDir) {
|
|
135
|
+
const hist = readHistory(engagementDir);
|
|
136
|
+
let last = 0;
|
|
137
|
+
for (let i = hist.length - 1; i >= 0; i--) {
|
|
138
|
+
if (typeof hist[i].checkpoint === 'number') { last = hist[i].checkpoint; break; }
|
|
139
|
+
}
|
|
140
|
+
try {
|
|
141
|
+
const cur = fm.get(fs.readFileSync(stateFile(engagementDir), 'utf8'), 'checkpoint');
|
|
142
|
+
const n = typeof cur === 'number' ? cur : parseInt(cur, 10);
|
|
143
|
+
if (!isNaN(n) && n > last) last = n;
|
|
144
|
+
} catch { /* no readable frontmatter yet */ }
|
|
145
|
+
return last + 1;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
function appendHistory(engagementDir, rec) {
|
|
149
|
+
fs.appendFileSync(historyFile(engagementDir), JSON.stringify(rec) + '\n');
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/** BOX-02: does the engagement root carry a source/ directory? */
|
|
153
|
+
function hasSourceDir(engagementDir) {
|
|
154
|
+
const d = paths.resolve(engagementDir, 'source');
|
|
155
|
+
try { return fs.statSync(d).isDirectory(); } catch { return false; }
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/** BOX-03: does ACCOUNTS.md hold >=1 real credential row (not the template header
|
|
159
|
+
* or the |---| separator, first cell non-empty)? */
|
|
160
|
+
function hasCredentialRow(engagementDir) {
|
|
161
|
+
const f = paths.accountsFile(engagementDir);
|
|
162
|
+
if (!fs.existsSync(f)) return false;
|
|
163
|
+
let seenHeader = false;
|
|
164
|
+
for (const line of fs.readFileSync(f, 'utf8').split('\n')) {
|
|
165
|
+
const t = line.trim();
|
|
166
|
+
if (!t.startsWith('|')) continue;
|
|
167
|
+
if (/^\|[\s|:-]+\|?$/.test(t) && t.includes('-')) continue; // |---| separator
|
|
168
|
+
if (!seenHeader) { seenHeader = true; continue; } // first table row = header
|
|
169
|
+
if ((t.split('|')[1] || '').trim()) return true; // data row, non-empty first cell
|
|
170
|
+
}
|
|
171
|
+
return false;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* Phase 28 28-01 (HERM-04): delegation-receipt counts for the STATE.md
|
|
176
|
+
* digest — "<N> recorded / <M> verified", derived from runs/delegations/
|
|
177
|
+
* plus subagent.verify results:
|
|
178
|
+
* recorded — every receipt file under runs/delegations/
|
|
179
|
+
* verified — receipts whose agent's declared artifacts currently pass
|
|
180
|
+
* subagent.verify (one verify per distinct receipt agent);
|
|
181
|
+
* receipts whose agent can no longer be verified stay counted
|
|
182
|
+
* but never verified — the digest never fabricates progress.
|
|
183
|
+
* Deterministic: a pure function of the engagement dir contents (sorted
|
|
184
|
+
* file list, no clocks). Returns null when no receipts exist (the caller
|
|
185
|
+
* then leaves the digest untouched — the clientlist-digest convention).
|
|
186
|
+
*/
|
|
187
|
+
function receiptsDigest(engagementDir) {
|
|
188
|
+
const abs = path.resolve(engagementDir);
|
|
189
|
+
const dir = path.join(abs, 'runs', 'delegations');
|
|
190
|
+
let files = [];
|
|
191
|
+
try {
|
|
192
|
+
files = fs.readdirSync(dir)
|
|
193
|
+
.filter((f) => {
|
|
194
|
+
try { return fs.statSync(path.join(dir, f)).isFile(); } catch { return false; }
|
|
195
|
+
})
|
|
196
|
+
.sort();
|
|
197
|
+
} catch {
|
|
198
|
+
return null; // no runs/delegations/ dir — no receipts
|
|
199
|
+
}
|
|
200
|
+
if (files.length === 0) return null;
|
|
201
|
+
// Receipt head shape (record() writer): plain YAML lines up to the `---`
|
|
202
|
+
// body fence — the agent is read from the raw `agent:` line.
|
|
203
|
+
const perAgent = new Map(); // agent name -> receipt count
|
|
204
|
+
for (const f of files) {
|
|
205
|
+
let text;
|
|
206
|
+
try { text = fs.readFileSync(path.join(dir, f), 'utf8'); } catch { continue; }
|
|
207
|
+
const fence = text.indexOf('\n---\n');
|
|
208
|
+
const m = (fence === -1 ? text : text.slice(0, fence)).match(/^agent: (.*)$/m);
|
|
209
|
+
if (!m) continue;
|
|
210
|
+
let agent = m[1].trim();
|
|
211
|
+
if (agent.startsWith('"') && agent.endsWith('"')) {
|
|
212
|
+
try { agent = String(JSON.parse(agent)); } catch { /* keep the raw text */ }
|
|
213
|
+
}
|
|
214
|
+
perAgent.set(agent, (perAgent.get(agent) || 0) + 1);
|
|
215
|
+
}
|
|
216
|
+
let verified = 0;
|
|
217
|
+
// Lazy require: subagent pulls THIS module at load — the cycle stays at
|
|
218
|
+
// call time only (the clientlist digest hook uses the same pattern).
|
|
219
|
+
let verify = null;
|
|
220
|
+
try { verify = require('./subagent.cjs').verify; } catch { verify = null; }
|
|
221
|
+
if (verify) {
|
|
222
|
+
for (const [agent, n] of perAgent) {
|
|
223
|
+
try {
|
|
224
|
+
const v = verify({ engagementDir: abs, agent });
|
|
225
|
+
if (v && v.ok && v.verdict === 'pass') verified += n;
|
|
226
|
+
} catch { /* unknown agent now — its receipts stay unverified */ }
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
return `${files.length} recorded / ${verified} verified`;
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
/**
|
|
233
|
+
* Phase 30 30-02 (LOOP-04): pending adaptive-candidate count for the STATE.md
|
|
234
|
+
* digest — N = plans/PLAN-*.md whose frontmatter carries `origin: adaptive`
|
|
235
|
+
* and which have NO recorded approval. Approvals state is the source of
|
|
236
|
+
* truth: the count reuses approvals.approvedPlans (the SWARM-02 work-list /
|
|
237
|
+
* 29-01 coverage convention) and never re-parses approval records. Plans are
|
|
238
|
+
* only read for their `origin` frontmatter. Deterministic: a pure function
|
|
239
|
+
* of plans/ + approvals on disk (sorted ids, no clocks). Returns null when
|
|
240
|
+
* N=0 — the caller then leaves the digest untouched (the receipts/clientlist
|
|
241
|
+
* absent-when-empty convention: the line is absent, never zero-printed).
|
|
242
|
+
*/
|
|
243
|
+
function adaptiveDigest(engagementDir) {
|
|
244
|
+
const pending = approvals.approvedPlans(engagementDir).unapproved.filter((id) => {
|
|
245
|
+
try {
|
|
246
|
+
const file = path.join(paths.plansDir(engagementDir), `PLAN-${id}.md`);
|
|
247
|
+
return fm.get(fs.readFileSync(file, 'utf8'), 'origin') === 'adaptive';
|
|
248
|
+
} catch {
|
|
249
|
+
return false; // unreadable plan — never counted (fail-closed)
|
|
250
|
+
}
|
|
251
|
+
});
|
|
252
|
+
if (pending.length === 0) return null;
|
|
253
|
+
return pending.length;
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
// The STATE.md digest key for the 30-02 line — module-private so the hook
|
|
257
|
+
// and any future clear logic can never drift apart on the literal string.
|
|
258
|
+
const ADAPTIVE_DIGEST_KEY = 'adaptive candidates awaiting approval';
|
|
259
|
+
|
|
260
|
+
function setStatus(engagementDir, status, opts = {}) {
|
|
261
|
+
if (!STATUSES.includes(status)) throw new Error(`unknown status: ${status}`);
|
|
262
|
+
const file = paths.stateFile(engagementDir);
|
|
263
|
+
const text = fs.readFileSync(file, 'utf-8');
|
|
264
|
+
const cur = fm.get(text, 'status');
|
|
265
|
+
if (cur) {
|
|
266
|
+
const curIdx = STATUSES.indexOf(cur);
|
|
267
|
+
const nextIdx = STATUSES.indexOf(status);
|
|
268
|
+
// idempotent set, exactly one step forward, or the close edge
|
|
269
|
+
// delivered -> closed (engagements that skip the retest lane, ADR-021).
|
|
270
|
+
// REACH-03 (v1.1): early close — close.cjs (the only caller passing
|
|
271
|
+
// closeReason) may close from ANY status with a non-completed reason;
|
|
272
|
+
// the public state.set-status path never passes it, so the ladder stays
|
|
273
|
+
// tight for everyone else.
|
|
274
|
+
const earlyClose = status === 'closed' && opts.closeReason && opts.closeReason !== 'completed';
|
|
275
|
+
const closeEdge = status === 'closed' && (cur === 'delivered' || cur === 'retesting' || earlyClose);
|
|
276
|
+
// v0.6 01-03 (brief 1.5 / ADR-012): the white-box lane. validated-static
|
|
277
|
+
// is reached from testing or validating (static-only verification), and exits
|
|
278
|
+
// to reporting (one step forward, covered below). The dynamic lane
|
|
279
|
+
// (validating -> reporting) survived the insertion by being allowed
|
|
280
|
+
// explicitly — the white-box stop is optional.
|
|
281
|
+
const whiteBox =
|
|
282
|
+
(status === 'validated-static' && (cur === 'testing' || cur === 'validating')) ||
|
|
283
|
+
(cur === 'validating' && status === 'reporting');
|
|
284
|
+
if (nextIdx !== curIdx && nextIdx !== curIdx + 1 && !closeEdge && !whiteBox) {
|
|
285
|
+
throw new Error(`invalid transition: ${cur} -> ${status} (one step forward only)`);
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
// ---- v0.6 01-03 item 5 (ADR-023 / brief 1.10): checkpoints-as-state ----
|
|
290
|
+
// Boundary transitions REFUSE without the recorded approval frontmatter on
|
|
291
|
+
// the artifact being left behind. Documented checkpoints without recorded
|
|
292
|
+
// state are advisory, and agents treat advisory as optional — these gates
|
|
293
|
+
// make the hard invariant ("checkpoints are hard stops") true in code.
|
|
294
|
+
// Refusals name the missing approval and the command that records it.
|
|
295
|
+
if (status === 'scoped') {
|
|
296
|
+
// ---- v0.10 14-01 (SEED-004 / HARDEN-04): loopback-target containment gate ----
|
|
297
|
+
// Fail-closed: a loopback in-scope target (localhost / ::1 / 127.0.0.0/8) can
|
|
298
|
+
// never run under containment — inside pensieve-net it resolves to the toolbox
|
|
299
|
+
// itself, not the real target. Reject before the engagement reaches `scoped`.
|
|
300
|
+
const scope = require('./scope.cjs');
|
|
301
|
+
const scopeFile = paths.scopeFile(engagementDir);
|
|
302
|
+
if (fs.existsSync(scopeFile)) {
|
|
303
|
+
const bad = scope.loopbackTargets(scope.parse(fs.readFileSync(scopeFile, 'utf8')));
|
|
304
|
+
if (bad.length) {
|
|
305
|
+
throw new Error(
|
|
306
|
+
`scope gate: loopback in-scope target rejected (SEED-004/HARDEN-04) — ${bad.join(', ')} — `
|
|
307
|
+
+ 'join the target container to pensieve-net and scope its service name + internal port '
|
|
308
|
+
+ 'instead (see lab/run.sh), e.g. juice-shop:3000'
|
|
309
|
+
);
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
// Scope is finalized here: autonomous-mode engagements RECORD their
|
|
313
|
+
// pre-authorization (approved_by: autonomous-mode; pre-v1.2 'yolo'
|
|
314
|
+
// read-side aliases to it). No-op for other levels and when SCOPE.md
|
|
315
|
+
// does not exist yet — the recon boundary below re-checks.
|
|
316
|
+
approvals.autoApproveScope(engagementDir);
|
|
317
|
+
}
|
|
318
|
+
if (status === 'recon' && cur === 'scoped') {
|
|
319
|
+
// autonomous-mode: the pre-authorization is recorded now if it was not at
|
|
320
|
+
// scope time (guardrail failures refuse — the gate below then surfaces).
|
|
321
|
+
approvals.autoApproveScope(engagementDir);
|
|
322
|
+
if (!approvals.isApproved(engagementDir, 'scope')) {
|
|
323
|
+
throw new Error(
|
|
324
|
+
'checkpoint gate (ADR-023): scoped -> recon requires an approved SCOPE.md — '
|
|
325
|
+
+ 'record it with: pensieve-sdk approve --engagement <dir> --artifact scope --by <name>'
|
|
326
|
+
);
|
|
327
|
+
}
|
|
328
|
+
// ---- v0.6 05-02 (BOX-02 / BOX-03): box prerequisite gate ----
|
|
329
|
+
// Default-deny: the box level the scoper recorded decides which inputs the
|
|
330
|
+
// engagement must carry before recon. Lazy-require config (like the other
|
|
331
|
+
// gates below) to keep it off every non-recon consumer's load path.
|
|
332
|
+
const config = require('./config.cjs');
|
|
333
|
+
const { cfg } = config.load(engagementDir);
|
|
334
|
+
if (!['white', 'gray', 'black'].includes(cfg.box)) {
|
|
335
|
+
throw new Error(
|
|
336
|
+
'box gate: scoped -> recon refused — box level not set in config.yaml '
|
|
337
|
+
+ '(run: pensieve-sdk scope.set-box <white|gray|black> --engagement <dir>)'
|
|
338
|
+
);
|
|
339
|
+
}
|
|
340
|
+
// BOX-02: white-box needs the source/ lane, unless the operator waived it.
|
|
341
|
+
if (cfg.box === 'white' && !cfg.box_source_waiver && !hasSourceDir(engagementDir)) {
|
|
342
|
+
throw new Error(
|
|
343
|
+
'box gate: scoped -> recon refused — white-box requires a source/ directory '
|
|
344
|
+
+ 'in the engagement root (add it, or waive with: '
|
|
345
|
+
+ 'pensieve-sdk scope.set-box white --source-waiver --engagement <dir>)'
|
|
346
|
+
);
|
|
347
|
+
}
|
|
348
|
+
// BOX-03: gray/white need >=1 credential row in ACCOUNTS.md, unless waived.
|
|
349
|
+
if ((cfg.box === 'gray' || cfg.box === 'white')
|
|
350
|
+
&& !cfg.box_credentials_waiver && !hasCredentialRow(engagementDir)) {
|
|
351
|
+
throw new Error(
|
|
352
|
+
`box gate: scoped -> recon refused — ${cfg.box}-box requires >=1 credential row `
|
|
353
|
+
+ 'in ACCOUNTS.md (add one, or waive with: pensieve-sdk scope.set-box '
|
|
354
|
+
+ `${cfg.box} --credentials-waiver <self-provision|unauthenticated-only> --engagement <dir>)`
|
|
355
|
+
);
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
if (status === 'planned' && cur === 'mapped') {
|
|
359
|
+
// auto-cite gate (v0.6 01-05 / planner contract): the planner writes
|
|
360
|
+
// plans/PLAN-*.md (citing pack checklist ids) and then `set-status ...
|
|
361
|
+
// planned`, so the "planned" state is only ever reached AFTER every plan's
|
|
362
|
+
// cited ids have been verified. Fail-closed: a plan must cite only real
|
|
363
|
+
// ids from the pack it declares. (Lazy requires: checklist/packs pull in
|
|
364
|
+
// frontmatter+pack parsing — keep them out of every other consumer's load path.)
|
|
365
|
+
const checklistLib = require('./checklist.cjs');
|
|
366
|
+
const g = checklistLib.gate(engagementDir);
|
|
367
|
+
if (!g.ok) {
|
|
368
|
+
throw new Error(
|
|
369
|
+
'checkpoint gate (planner auto-cite): mapped -> planned refused — plan checklist ' +
|
|
370
|
+
'cites must resolve to real pack ids: ' + g.errors.join('; ')
|
|
371
|
+
);
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
if (status === 'testing' && cur === 'planned') {
|
|
375
|
+
// NPX-05 (v1.2): autonomous mode records the plan checkpoints here
|
|
376
|
+
// (approved_by autonomous-mode; guardrail failures refuse and the gate
|
|
377
|
+
// below surfaces them). semi-autonomous is a no-op — its plans go
|
|
378
|
+
// through ai-approve, scope/report stay human; supervised unchanged.
|
|
379
|
+
approvals.autoApprovePlans(engagementDir);
|
|
380
|
+
const pending = approvals.unapprovedPlans(engagementDir);
|
|
381
|
+
if (pending.length) {
|
|
382
|
+
throw new Error(
|
|
383
|
+
`checkpoint gate (ADR-023): planned -> testing requires every plan approved — unapproved: `
|
|
384
|
+
+ `${pending.map((id) => `plan:${id}`).join(', ')} `
|
|
385
|
+
+ '(pensieve-sdk approve --engagement <dir> --artifact plan:<id> --by <name>)'
|
|
386
|
+
);
|
|
387
|
+
}
|
|
388
|
+
}
|
|
389
|
+
if (status === 'delivered' && cur === 'reporting') {
|
|
390
|
+
// lazy requires: findings/report pull in evidence/pack/cvss — keep them
|
|
391
|
+
// out of the module-load path of every other state consumer.
|
|
392
|
+
const findingsLib = require('./findings.cjs');
|
|
393
|
+
const reportLib = require('./report.cjs');
|
|
394
|
+
// NPX-05 (v1.2): autonomous mode records the report checkpoint — but the
|
|
395
|
+
// mechanical gates below still have their say: the record is refused when
|
|
396
|
+
// the report file is missing or a guardrail is failing, and report.verify
|
|
397
|
+
// / the findings gate are NOT bypassed by it.
|
|
398
|
+
approvals.autoApproveReport(engagementDir);
|
|
399
|
+
const errors = [];
|
|
400
|
+
const g = findingsLib.gate(engagementDir);
|
|
401
|
+
if (!g.report_ready) {
|
|
402
|
+
errors.push(g.total === 0
|
|
403
|
+
? 'findings gate has not passed (register is empty — a clean engagement needs findings.attest-clean with evidence ids)'
|
|
404
|
+
: `findings gate has not passed (unresolved drafts: ${g.draft.join(', ')})`);
|
|
405
|
+
}
|
|
406
|
+
let v;
|
|
407
|
+
try {
|
|
408
|
+
v = reportLib.verifyReport(engagementDir);
|
|
409
|
+
} catch (e) {
|
|
410
|
+
v = { ok: false, error: e.message };
|
|
411
|
+
}
|
|
412
|
+
if (!v.ok) errors.push(`report.verify has not passed (${v.error})`);
|
|
413
|
+
if (!approvals.isApproved(engagementDir, 'report')) {
|
|
414
|
+
errors.push('report/REPORT.md is not approved — record it with: pensieve-sdk approve --engagement <dir> --artifact report --by <name>');
|
|
415
|
+
}
|
|
416
|
+
if (errors.length) {
|
|
417
|
+
throw new Error(`checkpoint gate (ADR-023): reporting -> delivered refused — ${errors.join('; ')}`);
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
const isTransition = !cur || cur !== status;
|
|
422
|
+
let out = fm.set(text, 'status', status);
|
|
423
|
+
out = fm.set(out, 'updated', new Date().toISOString());
|
|
424
|
+
out = fm.set(out, 'next', NEXT_HINTS[status]);
|
|
425
|
+
if (status === 'closed' && opts.closeReason) {
|
|
426
|
+
out = fm.set(out, 'close_reason', opts.closeReason);
|
|
427
|
+
}
|
|
428
|
+
// Phase 19 19-02 (SC4): clientlist progress rides the STATE.md digest when
|
|
429
|
+
// the engagement carries an imported client checklist — answered counts per
|
|
430
|
+
// tier, refreshed on every status write. Engagements without one keep the
|
|
431
|
+
// exact digest shape they had (no key added). Lazy require: clientlist
|
|
432
|
+
// pulls state — keep the cycle at call time only.
|
|
433
|
+
try {
|
|
434
|
+
const digest = require('./clientlist.cjs').progressDigest(engagementDir);
|
|
435
|
+
if (digest) out = fm.set(out, 'clientlist', digest);
|
|
436
|
+
} catch {
|
|
437
|
+
/* no clientlist / unreadable — digest unchanged */
|
|
438
|
+
}
|
|
439
|
+
// Phase 28 28-01 (HERM-04): delegation receipts ride the STATE.md digest
|
|
440
|
+
// when the engagement carries recorded receipts (runs/delegations/) —
|
|
441
|
+
// recorded / verified counts, refreshed on every status write, so wave
|
|
442
|
+
// progress is visible without reading the raw dir. Engagements without
|
|
443
|
+
// receipts keep the exact digest shape they had (no key added).
|
|
444
|
+
try {
|
|
445
|
+
const receipts = receiptsDigest(engagementDir);
|
|
446
|
+
if (receipts) out = fm.set(out, 'receipts', receipts);
|
|
447
|
+
} catch {
|
|
448
|
+
/* unreadable receipts — digest unchanged */
|
|
449
|
+
}
|
|
450
|
+
// Phase 30 30-02 (LOOP-04): pending adaptive candidates ride the STATE.md
|
|
451
|
+
// digest — `adaptive candidates awaiting approval: N`, N = plans/PLAN-*.md
|
|
452
|
+
// with frontmatter origin: adaptive and NO recorded approval (approvals
|
|
453
|
+
// state is the source of truth — adaptiveDigest reuses approvedPlans).
|
|
454
|
+
// Absent when zero (the receipts/clientlist absent-when-empty rule) — and
|
|
455
|
+
// unlike those monotonic counters, a line written by an earlier status
|
|
456
|
+
// write is REMOVED once the pending set clears: approve one -> N-1,
|
|
457
|
+
// approve all -> gone (the 30-02 transition truth).
|
|
458
|
+
try {
|
|
459
|
+
const adaptive = adaptiveDigest(engagementDir);
|
|
460
|
+
if (adaptive !== null) {
|
|
461
|
+
out = fm.set(out, ADAPTIVE_DIGEST_KEY, adaptive);
|
|
462
|
+
} else if (fm.get(out, ADAPTIVE_DIGEST_KEY) !== undefined) {
|
|
463
|
+
const { data, body } = fm.parse(out);
|
|
464
|
+
delete data[ADAPTIVE_DIGEST_KEY];
|
|
465
|
+
out = fm.stringify(data, body);
|
|
466
|
+
}
|
|
467
|
+
} catch {
|
|
468
|
+
/* unreadable plans — digest unchanged */
|
|
469
|
+
}
|
|
470
|
+
if (isTransition) {
|
|
471
|
+
// checkpoint id: monotonic per transition; mirrored into frontmatter + history
|
|
472
|
+
const checkpoint = nextCheckpoint(engagementDir);
|
|
473
|
+
out = fm.set(out, 'checkpoint', checkpoint);
|
|
474
|
+
fs.writeFileSync(file, out);
|
|
475
|
+
appendHistory(engagementDir, { checkpoint, from: cur || null, to: status, at: new Date().toISOString() });
|
|
476
|
+
} else {
|
|
477
|
+
fs.writeFileSync(file, out);
|
|
478
|
+
}
|
|
479
|
+
return status;
|
|
480
|
+
}
|
|
481
|
+
|
|
482
|
+
function stateFile(engagementDir) {
|
|
483
|
+
return paths.stateFile(engagementDir);
|
|
484
|
+
}
|
|
485
|
+
|
|
486
|
+
function readStatus(engagementDir) {
|
|
487
|
+
const text = fs.readFileSync(stateFile(engagementDir), 'utf8');
|
|
488
|
+
const status = fm.get(text, 'status');
|
|
489
|
+
// fail-closed (v0.6 01-02, brief 1.2): STATE.md is the engagement ground
|
|
490
|
+
// truth — a missing status frontmatter means human damage, NOT a fresh
|
|
491
|
+
// engagement. Never silently default to 'initialized'; point at state.sync.
|
|
492
|
+
if (status == null || status === '') {
|
|
493
|
+
throw new Error(
|
|
494
|
+
'STATE.md is missing its status frontmatter (fail-closed: STATE.md is the ground truth) — '
|
|
495
|
+
+ 'repair it with: pensieve-sdk state.sync --engagement <dir>'
|
|
496
|
+
);
|
|
497
|
+
}
|
|
498
|
+
return status;
|
|
499
|
+
}
|
|
500
|
+
|
|
501
|
+
/**
|
|
502
|
+
* sync(engagementDir) — reattach/repair STATE.md frontmatter WITHOUT touching
|
|
503
|
+
* the human-written body below it (byte-for-byte). Status resolution order:
|
|
504
|
+
* 1. a valid status already in the frontmatter wins (sync refreshes, never
|
|
505
|
+
* resets the ladder);
|
|
506
|
+
* 2. the registry entry for this path (entry.status, or 'closed' when the
|
|
507
|
+
* entry carries close.cjs's closed_at stamp);
|
|
508
|
+
* 3. 'initialized'.
|
|
509
|
+
* Tolerant by design: split never throws and unparseable yaml is rebuilt —
|
|
510
|
+
* this command exists precisely for STATE.md files other commands reject.
|
|
511
|
+
*/
|
|
512
|
+
function sync(engagementDir) {
|
|
513
|
+
if (!engagementDir) throw new Error('state.sync requires --engagement <dir>');
|
|
514
|
+
const root = path.resolve(engagementDir);
|
|
515
|
+
const file = stateFile(root);
|
|
516
|
+
if (!fs.existsSync(file)) {
|
|
517
|
+
throw new Error('no STATE.md at the engagement root — nothing to sync (run init first)');
|
|
518
|
+
}
|
|
519
|
+
const text = fs.readFileSync(file, 'utf8');
|
|
520
|
+
const { yaml, body } = fm.split(text);
|
|
521
|
+
let data = {};
|
|
522
|
+
let corrupted = false;
|
|
523
|
+
if (yaml !== null) {
|
|
524
|
+
try { data = fm.parseYaml(yaml); } catch { corrupted = true; data = {}; }
|
|
525
|
+
}
|
|
526
|
+
const hasValidStatus = !corrupted && typeof data.status === 'string' && STATUSES.includes(data.status);
|
|
527
|
+
|
|
528
|
+
let status; let source;
|
|
529
|
+
if (hasValidStatus) {
|
|
530
|
+
status = data.status; source = 'frontmatter';
|
|
531
|
+
} else {
|
|
532
|
+
const reg = readRegistry();
|
|
533
|
+
const entry = Object.values(reg).find((e) => e && e.path && path.resolve(e.path) === root);
|
|
534
|
+
if (entry && typeof entry.status === 'string' && STATUSES.includes(entry.status)) {
|
|
535
|
+
status = entry.status; source = 'registry';
|
|
536
|
+
} else if (entry && entry.closed_at) {
|
|
537
|
+
status = 'closed'; source = 'registry';
|
|
538
|
+
} else {
|
|
539
|
+
status = 'initialized'; source = 'default';
|
|
540
|
+
}
|
|
541
|
+
}
|
|
542
|
+
|
|
543
|
+
const merged = { ...data, status, updated: new Date().toISOString(), next: NEXT_HINTS[status] };
|
|
544
|
+
fs.writeFileSync(file, fm.stringify(merged, body));
|
|
545
|
+
return { file, status, source, repaired: !hasValidStatus };
|
|
546
|
+
}
|
|
547
|
+
|
|
548
|
+
/**
|
|
549
|
+
* pause/resume (continue-here.md): pausing is a NOTE, not a status — the
|
|
550
|
+
* status machine's truth does not move because a human stepped away.
|
|
551
|
+
*/
|
|
552
|
+
function pause(engagementDir, note) {
|
|
553
|
+
const status = readStatus(engagementDir);
|
|
554
|
+
const body = [
|
|
555
|
+
'---',
|
|
556
|
+
`paused: ${new Date().toISOString()}`,
|
|
557
|
+
`status: ${status}`,
|
|
558
|
+
'---',
|
|
559
|
+
'',
|
|
560
|
+
'# Continue here',
|
|
561
|
+
'',
|
|
562
|
+
`Status when paused: **${status}**`,
|
|
563
|
+
`Next: ${NEXT_HINTS[status]}`,
|
|
564
|
+
'',
|
|
565
|
+
note ? `Note: ${note}` : '',
|
|
566
|
+
'',
|
|
567
|
+
].join('\n');
|
|
568
|
+
const file = paths.continueHereFile(engagementDir);
|
|
569
|
+
fs.writeFileSync(file, body);
|
|
570
|
+
return file;
|
|
571
|
+
}
|
|
572
|
+
|
|
573
|
+
function resumeInfo(engagementDir) {
|
|
574
|
+
const status = readStatus(engagementDir);
|
|
575
|
+
const chFile = paths.continueHereFile(engagementDir);
|
|
576
|
+
let checkpoint = null;
|
|
577
|
+
try {
|
|
578
|
+
const c = fm.get(fs.readFileSync(stateFile(engagementDir), 'utf8'), 'checkpoint');
|
|
579
|
+
checkpoint = typeof c === 'number' ? c : (parseInt(c, 10) || null);
|
|
580
|
+
} catch { checkpoint = null; }
|
|
581
|
+
return {
|
|
582
|
+
status,
|
|
583
|
+
checkpoint,
|
|
584
|
+
next: NEXT_HINTS[status] || null,
|
|
585
|
+
continue_here: fs.existsSync(chFile) ? fs.readFileSync(chFile, 'utf8') : null,
|
|
586
|
+
};
|
|
587
|
+
}
|
|
588
|
+
|
|
589
|
+
/**
|
|
590
|
+
* prune() — registry hygiene (v0.6 01-02, brief 1.9): drop entries whose
|
|
591
|
+
* engagement dir no longer exists on disk, UNLESS the entry carries
|
|
592
|
+
* close.cjs's closed_at stamp — closed records are the archive and survive
|
|
593
|
+
* their directories. Reports both lists.
|
|
594
|
+
*/
|
|
595
|
+
function prune() {
|
|
596
|
+
const reg = readRegistry();
|
|
597
|
+
const kept = {};
|
|
598
|
+
const dropped = [];
|
|
599
|
+
const keptClosed = [];
|
|
600
|
+
for (const [id, e] of Object.entries(reg)) {
|
|
601
|
+
const exists = e && typeof e.path === 'string' && fs.existsSync(e.path);
|
|
602
|
+
if (exists) { kept[id] = e; continue; }
|
|
603
|
+
if (e && e.closed_at) { kept[id] = e; keptClosed.push(id); continue; }
|
|
604
|
+
dropped.push(id);
|
|
605
|
+
}
|
|
606
|
+
writeRegistry(kept);
|
|
607
|
+
return { dropped, kept_closed: keptClosed, remaining: Object.keys(kept).length };
|
|
608
|
+
}
|
|
609
|
+
|
|
610
|
+
module.exports = { register, list, current, isEngagement, setStatus, sync, prune, pause, resumeInfo, readStatus, readHistory, nextCheckpoint, historyFile, STATUSES, NEXT_HINTS, pensieveHome };
|