sdtk-code-kit 0.2.0 → 0.2.1

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.
Files changed (75) hide show
  1. package/README.md +201 -201
  2. package/assets/manifest/toolkit-bundle.manifest.json +147 -147
  3. package/assets/manifest/toolkit-bundle.sha256.txt +28 -28
  4. package/assets/toolkit/toolkit/AGENTS.md +219 -219
  5. package/assets/toolkit/toolkit/install.ps1 +156 -156
  6. package/assets/toolkit/toolkit/scripts/install-claude-skills.ps1 +78 -78
  7. package/assets/toolkit/toolkit/scripts/install-codex-skills.ps1 +150 -150
  8. package/assets/toolkit/toolkit/scripts/uninstall-claude-skills.ps1 +62 -62
  9. package/assets/toolkit/toolkit/scripts/uninstall-codex-skills.ps1 +82 -82
  10. package/assets/toolkit/toolkit/sdtk-spec.config.json +6 -6
  11. package/assets/toolkit/toolkit/sdtk-spec.config.profiles.example.json +12 -12
  12. package/assets/toolkit/toolkit/skills/code-brainstorm/SKILL.md +55 -55
  13. package/assets/toolkit/toolkit/skills/code-debug/SKILL.md +296 -296
  14. package/assets/toolkit/toolkit/skills/code-debug/references/root-cause-tracing.md +68 -68
  15. package/assets/toolkit/toolkit/skills/code-discover/SKILL.md +54 -54
  16. package/assets/toolkit/toolkit/skills/code-execute/SKILL.md +74 -74
  17. package/assets/toolkit/toolkit/skills/code-finish/SKILL.md +205 -205
  18. package/assets/toolkit/toolkit/skills/code-parallel/SKILL.md +64 -64
  19. package/assets/toolkit/toolkit/skills/code-parallel/references/code-quality-reviewer-prompt.md +26 -26
  20. package/assets/toolkit/toolkit/skills/code-parallel/references/implementer-prompt.md +113 -113
  21. package/assets/toolkit/toolkit/skills/code-parallel/references/spec-reviewer-prompt.md +61 -61
  22. package/assets/toolkit/toolkit/skills/code-plan/SKILL.md +85 -85
  23. package/assets/toolkit/toolkit/skills/code-review/SKILL.md +79 -79
  24. package/assets/toolkit/toolkit/skills/code-review/references/checklist.md +125 -125
  25. package/assets/toolkit/toolkit/skills/code-review/references/code-reviewer.md +146 -146
  26. package/assets/toolkit/toolkit/skills/code-ship/SKILL.md +52 -52
  27. package/assets/toolkit/toolkit/skills/code-tdd/SKILL.md +378 -378
  28. package/assets/toolkit/toolkit/skills/code-tdd/references/testing-anti-patterns.md +299 -299
  29. package/assets/toolkit/toolkit/skills/code-verify/SKILL.md +144 -144
  30. package/assets/toolkit/toolkit/skills/code-worktree/SKILL.md +228 -228
  31. package/assets/toolkit/toolkit/templates/CODE_WORKFLOW_TEMPLATE.md +140 -140
  32. package/bin/sdtk-code.js +14 -14
  33. package/package.json +44 -44
  34. package/src/commands/build.js +53 -53
  35. package/src/commands/doctor.js +51 -51
  36. package/src/commands/generate.js +11 -11
  37. package/src/commands/guardrails.js +215 -123
  38. package/src/commands/help.js +244 -244
  39. package/src/commands/init.js +98 -98
  40. package/src/commands/plan.js +53 -53
  41. package/src/commands/readiness.js +77 -77
  42. package/src/commands/resume.js +61 -61
  43. package/src/commands/runtime.js +224 -224
  44. package/src/commands/ship.js +59 -59
  45. package/src/commands/sleep.js +132 -0
  46. package/src/commands/start.js +89 -89
  47. package/src/commands/status.js +77 -77
  48. package/src/commands/update.js +11 -11
  49. package/src/commands/verify.js +71 -71
  50. package/src/index.js +108 -104
  51. package/src/lib/args.js +107 -107
  52. package/src/lib/errors.js +41 -41
  53. package/src/lib/powershell.js +65 -65
  54. package/src/lib/project-target-guard.js +53 -53
  55. package/src/lib/scope.js +72 -72
  56. package/src/lib/ship-readiness.js +281 -281
  57. package/src/lib/sleep-plan.js +175 -0
  58. package/src/lib/sleep-report.js +229 -0
  59. package/src/lib/toolkit-payload.js +72 -72
  60. package/src/lib/trust-events.js +166 -166
  61. package/src/lib/trust-file-scope.js +154 -154
  62. package/src/lib/trust-policy.js +212 -212
  63. package/src/lib/trust-redact.js +35 -35
  64. package/src/lib/trust-trace.js +179 -179
  65. package/src/lib/update.js +344 -344
  66. package/src/lib/workflow-artifact.js +503 -503
  67. package/src/lib/workflow-build.js +127 -127
  68. package/src/lib/workflow-doctor.js +83 -83
  69. package/src/lib/workflow-handoff.js +1029 -1029
  70. package/src/lib/workflow-intake.js +101 -101
  71. package/src/lib/workflow-plan.js +193 -193
  72. package/src/lib/workflow-review-packet.js +650 -650
  73. package/src/lib/workflow-ship.js +213 -213
  74. package/src/lib/workflow-state.js +438 -438
  75. package/src/lib/workflow-verify.js +393 -393
@@ -1,281 +1,281 @@
1
- "use strict";
2
-
3
- const fs = require("fs");
4
- const path = require("path");
5
-
6
- // ---------------------------------------------------------------------------
7
- // Pure scorer — no fs, no git. All signals supplied by the caller.
8
- // ---------------------------------------------------------------------------
9
-
10
- const GATE_POINTS = {
11
- pass: 1.0,
12
- partial: 0.5,
13
- fail: 0,
14
- absent: 0,
15
- };
16
-
17
- function gateScore(gate) {
18
- const factor = GATE_POINTS[typeof gate === "string" ? gate : "absent"];
19
- return factor !== undefined ? factor : 0;
20
- }
21
-
22
- function codeVerifyPoints(verifyGates) {
23
- const gates = verifyGates && typeof verifyGates === "object" ? verifyGates : {};
24
- const spec = gateScore(gates.spec) * 10;
25
- const quality = gateScore(gates.quality) * 10;
26
- const evidence = gateScore(gates.evidence) * 10;
27
- return { spec, quality, evidence, total: spec + quality + evidence };
28
- }
29
-
30
- function codeVerifyNote(verifyGates) {
31
- const gates = verifyGates && typeof verifyGates === "object" ? verifyGates : {};
32
- const parts = [];
33
- for (const key of ["spec", "quality", "evidence"]) {
34
- const g = typeof gates[key] === "string" ? gates[key] : "absent";
35
- if (g !== "pass") {
36
- parts.push(`${key}:${g}`);
37
- }
38
- }
39
- return parts.length > 0 ? `non-pass gates: ${parts.join(", ")}` : "all gates pass";
40
- }
41
-
42
- function fileScopePoints(verdict) {
43
- if (verdict === "pass") return 10;
44
- if (verdict === "needs_human") return 5;
45
- return 0;
46
- }
47
-
48
- // Deterministic, component-based readiness scorer.
49
- // signals === null triggers ENV_BLOCKED immediately.
50
- // Components that are N/A (DESIGN when uiScope=false) are excluded from max.
51
- function computeReadiness(signals) {
52
- if (signals === null || signals === undefined) {
53
- return {
54
- score: 0,
55
- max: 0,
56
- components: [],
57
- verdict: "ENV_BLOCKED",
58
- blockers: ["Signals could not be gathered — check project path and workflow artifacts."],
59
- warnings: [],
60
- };
61
- }
62
-
63
- const components = [];
64
- let score = 0;
65
- let max = 0;
66
- const blockers = [];
67
- const warnings = [];
68
-
69
- function addComponent(name, points, compMax, note) {
70
- components.push({ name, points, max: compMax, note: note || "" });
71
- score += points;
72
- max += compMax;
73
- }
74
-
75
- // 1. SPEC (15 pts) — hasCodeHandoff
76
- const hasCode = Boolean(signals.hasCodeHandoff);
77
- addComponent("SPEC", hasCode ? 15 : 0, 15, hasCode ? "CODE handoff present" : "CODE handoff not found");
78
- if (!hasCode) {
79
- warnings.push("CODE handoff not found — SPEC component scored 0.");
80
- }
81
-
82
- // 2. DESIGN (15 pts) — only when uiScope === true
83
- if (signals.uiScope === true) {
84
- const hasDesign = Boolean(signals.hasDesignHandoff);
85
- addComponent("DESIGN", hasDesign ? 15 : 0, 15, hasDesign ? "DESIGN handoff present" : "DESIGN handoff not found");
86
- if (!hasDesign) {
87
- warnings.push("DESIGN handoff not found — DESIGN component scored 0.");
88
- }
89
- }
90
-
91
- // 3. CODE_VERIFY (30 pts) — verifyGates spec(10) + quality(10) + evidence(10)
92
- const vg = signals.verifyGates && typeof signals.verifyGates === "object" ? signals.verifyGates : {};
93
- const cvPts = codeVerifyPoints(signals.verifyGates);
94
- addComponent("CODE_VERIFY", cvPts.total, 30, codeVerifyNote(signals.verifyGates));
95
- const evidenceGate = typeof vg.evidence === "string" ? vg.evidence : "absent";
96
- if (evidenceGate === "fail" || evidenceGate === "absent") {
97
- blockers.push(`CODE verification evidence missing or failed (evidence gate: ${evidenceGate}).`);
98
- }
99
-
100
- // 4. GUARDRAIL (15 pts) — denyCount===0 → full; any deny → 0 + blocker
101
- const guardrail = signals.guardrail && typeof signals.guardrail === "object" ? signals.guardrail : {};
102
- const denyCount = typeof guardrail.denyCount === "number" ? guardrail.denyCount : 0;
103
- const guardrailPts = denyCount === 0 ? 15 : 0;
104
- addComponent(
105
- "GUARDRAIL",
106
- guardrailPts,
107
- 15,
108
- denyCount === 0 ? "no unresolved deny events" : `${denyCount} unresolved deny event(s)`
109
- );
110
- if (denyCount > 0) {
111
- blockers.push(`Unresolved guardrail deny events: ${denyCount}.`);
112
- }
113
-
114
- // 5. FILE_SCOPE (10 pts) — pass→10, needs_human→5+warning, fail→0+blocker, absent→0
115
- const fsObj = signals.fileScope && typeof signals.fileScope === "object" ? signals.fileScope : {};
116
- const fsVerdict = typeof fsObj.verdict === "string" ? fsObj.verdict : "absent";
117
- const fsPts = fileScopePoints(fsVerdict);
118
- addComponent(
119
- "FILE_SCOPE",
120
- fsPts,
121
- 10,
122
- `file-scope verdict: ${fsVerdict}`
123
- );
124
- if (fsVerdict === "needs_human") {
125
- warnings.push("File-scope gate needs human review — partial credit only.");
126
- } else if (fsVerdict === "fail") {
127
- blockers.push("File-scope check failed — out-of-scope file edits detected.");
128
- }
129
-
130
- // 6. OPS (15 pts) — hasOpsHandoff
131
- const hasOps = Boolean(signals.hasOpsHandoff);
132
- addComponent("OPS", hasOps ? 15 : 0, 15, hasOps ? "OPS handoff present" : "OPS handoff evidence absent");
133
- if (!hasOps) {
134
- warnings.push("OPS handoff evidence absent — OPS component scored 0.");
135
- }
136
-
137
- // Verdict mapping (most-restrictive-wins, deterministic)
138
- let verdict;
139
- if (blockers.length > 0) {
140
- verdict = "FAIL";
141
- } else if (warnings.length > 0) {
142
- verdict = "PASS_WITH_NOTES";
143
- } else {
144
- verdict = "PASS";
145
- }
146
-
147
- return { score, max, components, verdict, blockers, warnings };
148
- }
149
-
150
- // ---------------------------------------------------------------------------
151
- // Thin signal gatherer — reads fs artifacts; wraps exceptions → null.
152
- // Not the test target; computeReadiness is the tested unit.
153
- // ---------------------------------------------------------------------------
154
-
155
- function parseGateField(verifyBlock, label) {
156
- const escaped = label.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
157
- const match = verifyBlock.match(new RegExp(`- ${escaped}: \`([^\\r\\n\`]+)\``));
158
- return match ? match[1] : null;
159
- }
160
-
161
- function extractVerifyBlock(text) {
162
- const match = text.match(/<!-- SDTK-CODE:VERIFY:START -->([\s\S]*?)<!-- SDTK-CODE:VERIFY:END -->/);
163
- return match ? match[1] : null;
164
- }
165
-
166
- function readAllEvents(eventsDir) {
167
- const events = [];
168
- let files;
169
- try {
170
- files = fs.readdirSync(eventsDir).filter((f) => f.endsWith(".jsonl"));
171
- } catch (_e) {
172
- return events;
173
- }
174
- for (const file of files) {
175
- const filePath = path.join(eventsDir, file);
176
- try {
177
- const lines = fs.readFileSync(filePath, "utf8").split(/\r?\n/);
178
- for (const line of lines) {
179
- if (!line.trim()) continue;
180
- try {
181
- events.push(JSON.parse(line));
182
- } catch (_e) {
183
- // skip malformed line
184
- }
185
- }
186
- } catch (_e) {
187
- // skip unreadable file
188
- }
189
- }
190
- return events;
191
- }
192
-
193
- function gatherSignals(projectPath, featureKey) {
194
- try {
195
- const resolved = path.resolve(projectPath || ".");
196
- const signals = {};
197
-
198
- // SPEC handoff
199
- const codeHandoffPath = path.join(resolved, "docs", "dev", `CODE_HANDOFF_${featureKey}.json`);
200
- signals.hasCodeHandoff = fs.existsSync(codeHandoffPath);
201
-
202
- // OPS handoff
203
- const opsHandoffPath = path.join(resolved, "docs", "dev", `OPS_HANDOFF_${featureKey}.json`);
204
- signals.hasOpsHandoff = fs.existsSync(opsHandoffPath);
205
-
206
- // UI scope detection: check CODE_HANDOFF required_refs for docs/design/ entries
207
- signals.uiScope = false;
208
- signals.hasDesignHandoff = false;
209
- if (signals.hasCodeHandoff) {
210
- try {
211
- const codeHandoff = JSON.parse(fs.readFileSync(codeHandoffPath, "utf8"));
212
- if (Array.isArray(codeHandoff.required_refs)) {
213
- signals.uiScope = codeHandoff.required_refs.some(
214
- (r) => typeof r === "string" && r.startsWith("docs/design/")
215
- );
216
- }
217
- } catch (_e) {
218
- // ignore malformed CODE_HANDOFF for signal gathering
219
- }
220
- }
221
- if (signals.uiScope) {
222
- const designHandoffPath = path.join(resolved, "docs", "dev", `DESIGN_HANDOFF_${featureKey}.md`);
223
- signals.hasDesignHandoff = fs.existsSync(designHandoffPath);
224
- }
225
-
226
- // verifyGates from CODE_WORKFLOW artifact VERIFY block
227
- signals.verifyGates = { spec: "absent", quality: "absent", evidence: "absent" };
228
- const workflowPath = path.join(resolved, "docs", "dev", `CODE_WORKFLOW_${featureKey}.md`);
229
- if (fs.existsSync(workflowPath)) {
230
- try {
231
- const text = fs.readFileSync(workflowPath, "utf8");
232
- const verifyBlock = extractVerifyBlock(text);
233
- if (verifyBlock) {
234
- const specGate = parseGateField(verifyBlock, "Spec/compliance gate");
235
- const qualityGate = parseGateField(verifyBlock, "Quality gate");
236
- const evidenceGate = parseGateField(verifyBlock, "Evidence gate");
237
- if (specGate) signals.verifyGates.spec = specGate;
238
- if (qualityGate) signals.verifyGates.quality = qualityGate;
239
- if (evidenceGate) signals.verifyGates.evidence = evidenceGate;
240
- }
241
- } catch (_e) {
242
- // leave verifyGates as absent
243
- }
244
- }
245
-
246
- // Trust events: guardrail_check and file_scope_check
247
- signals.guardrail = { denyCount: 0, askCount: 0 };
248
- signals.fileScope = { verdict: "absent" };
249
- const eventsDir = path.join(resolved, ".sdtk", "trust", "events");
250
- if (fs.existsSync(eventsDir)) {
251
- const events = readAllEvents(eventsDir);
252
- let denyCount = 0;
253
- let infoCount = 0;
254
- const fileScopeEvents = [];
255
- for (const event of events) {
256
- if (event.event_type === "guardrail_check") {
257
- if (event.status === "blocked") denyCount++;
258
- else infoCount++;
259
- }
260
- if (event.event_type === "file_scope_check") {
261
- fileScopeEvents.push(event);
262
- }
263
- }
264
- signals.guardrail = { denyCount, askCount: infoCount };
265
- if (fileScopeEvents.length > 0) {
266
- const last = fileScopeEvents[fileScopeEvents.length - 1];
267
- let verdict = "pass";
268
- if (last.status === "blocked") verdict = "fail";
269
- else if (last.status === "needs_human") verdict = "needs_human";
270
- signals.fileScope = { verdict };
271
- }
272
- }
273
-
274
- return signals;
275
- } catch (_error) {
276
- // Any top-level read failure → null signals → ENV_BLOCKED in computeReadiness
277
- return null;
278
- }
279
- }
280
-
281
- module.exports = { computeReadiness, gatherSignals };
1
+ "use strict";
2
+
3
+ const fs = require("fs");
4
+ const path = require("path");
5
+
6
+ // ---------------------------------------------------------------------------
7
+ // Pure scorer — no fs, no git. All signals supplied by the caller.
8
+ // ---------------------------------------------------------------------------
9
+
10
+ const GATE_POINTS = {
11
+ pass: 1.0,
12
+ partial: 0.5,
13
+ fail: 0,
14
+ absent: 0,
15
+ };
16
+
17
+ function gateScore(gate) {
18
+ const factor = GATE_POINTS[typeof gate === "string" ? gate : "absent"];
19
+ return factor !== undefined ? factor : 0;
20
+ }
21
+
22
+ function codeVerifyPoints(verifyGates) {
23
+ const gates = verifyGates && typeof verifyGates === "object" ? verifyGates : {};
24
+ const spec = gateScore(gates.spec) * 10;
25
+ const quality = gateScore(gates.quality) * 10;
26
+ const evidence = gateScore(gates.evidence) * 10;
27
+ return { spec, quality, evidence, total: spec + quality + evidence };
28
+ }
29
+
30
+ function codeVerifyNote(verifyGates) {
31
+ const gates = verifyGates && typeof verifyGates === "object" ? verifyGates : {};
32
+ const parts = [];
33
+ for (const key of ["spec", "quality", "evidence"]) {
34
+ const g = typeof gates[key] === "string" ? gates[key] : "absent";
35
+ if (g !== "pass") {
36
+ parts.push(`${key}:${g}`);
37
+ }
38
+ }
39
+ return parts.length > 0 ? `non-pass gates: ${parts.join(", ")}` : "all gates pass";
40
+ }
41
+
42
+ function fileScopePoints(verdict) {
43
+ if (verdict === "pass") return 10;
44
+ if (verdict === "needs_human") return 5;
45
+ return 0;
46
+ }
47
+
48
+ // Deterministic, component-based readiness scorer.
49
+ // signals === null triggers ENV_BLOCKED immediately.
50
+ // Components that are N/A (DESIGN when uiScope=false) are excluded from max.
51
+ function computeReadiness(signals) {
52
+ if (signals === null || signals === undefined) {
53
+ return {
54
+ score: 0,
55
+ max: 0,
56
+ components: [],
57
+ verdict: "ENV_BLOCKED",
58
+ blockers: ["Signals could not be gathered — check project path and workflow artifacts."],
59
+ warnings: [],
60
+ };
61
+ }
62
+
63
+ const components = [];
64
+ let score = 0;
65
+ let max = 0;
66
+ const blockers = [];
67
+ const warnings = [];
68
+
69
+ function addComponent(name, points, compMax, note) {
70
+ components.push({ name, points, max: compMax, note: note || "" });
71
+ score += points;
72
+ max += compMax;
73
+ }
74
+
75
+ // 1. SPEC (15 pts) — hasCodeHandoff
76
+ const hasCode = Boolean(signals.hasCodeHandoff);
77
+ addComponent("SPEC", hasCode ? 15 : 0, 15, hasCode ? "CODE handoff present" : "CODE handoff not found");
78
+ if (!hasCode) {
79
+ warnings.push("CODE handoff not found — SPEC component scored 0.");
80
+ }
81
+
82
+ // 2. DESIGN (15 pts) — only when uiScope === true
83
+ if (signals.uiScope === true) {
84
+ const hasDesign = Boolean(signals.hasDesignHandoff);
85
+ addComponent("DESIGN", hasDesign ? 15 : 0, 15, hasDesign ? "DESIGN handoff present" : "DESIGN handoff not found");
86
+ if (!hasDesign) {
87
+ warnings.push("DESIGN handoff not found — DESIGN component scored 0.");
88
+ }
89
+ }
90
+
91
+ // 3. CODE_VERIFY (30 pts) — verifyGates spec(10) + quality(10) + evidence(10)
92
+ const vg = signals.verifyGates && typeof signals.verifyGates === "object" ? signals.verifyGates : {};
93
+ const cvPts = codeVerifyPoints(signals.verifyGates);
94
+ addComponent("CODE_VERIFY", cvPts.total, 30, codeVerifyNote(signals.verifyGates));
95
+ const evidenceGate = typeof vg.evidence === "string" ? vg.evidence : "absent";
96
+ if (evidenceGate === "fail" || evidenceGate === "absent") {
97
+ blockers.push(`CODE verification evidence missing or failed (evidence gate: ${evidenceGate}).`);
98
+ }
99
+
100
+ // 4. GUARDRAIL (15 pts) — denyCount===0 → full; any deny → 0 + blocker
101
+ const guardrail = signals.guardrail && typeof signals.guardrail === "object" ? signals.guardrail : {};
102
+ const denyCount = typeof guardrail.denyCount === "number" ? guardrail.denyCount : 0;
103
+ const guardrailPts = denyCount === 0 ? 15 : 0;
104
+ addComponent(
105
+ "GUARDRAIL",
106
+ guardrailPts,
107
+ 15,
108
+ denyCount === 0 ? "no unresolved deny events" : `${denyCount} unresolved deny event(s)`
109
+ );
110
+ if (denyCount > 0) {
111
+ blockers.push(`Unresolved guardrail deny events: ${denyCount}.`);
112
+ }
113
+
114
+ // 5. FILE_SCOPE (10 pts) — pass→10, needs_human→5+warning, fail→0+blocker, absent→0
115
+ const fsObj = signals.fileScope && typeof signals.fileScope === "object" ? signals.fileScope : {};
116
+ const fsVerdict = typeof fsObj.verdict === "string" ? fsObj.verdict : "absent";
117
+ const fsPts = fileScopePoints(fsVerdict);
118
+ addComponent(
119
+ "FILE_SCOPE",
120
+ fsPts,
121
+ 10,
122
+ `file-scope verdict: ${fsVerdict}`
123
+ );
124
+ if (fsVerdict === "needs_human") {
125
+ warnings.push("File-scope gate needs human review — partial credit only.");
126
+ } else if (fsVerdict === "fail") {
127
+ blockers.push("File-scope check failed — out-of-scope file edits detected.");
128
+ }
129
+
130
+ // 6. OPS (15 pts) — hasOpsHandoff
131
+ const hasOps = Boolean(signals.hasOpsHandoff);
132
+ addComponent("OPS", hasOps ? 15 : 0, 15, hasOps ? "OPS handoff present" : "OPS handoff evidence absent");
133
+ if (!hasOps) {
134
+ warnings.push("OPS handoff evidence absent — OPS component scored 0.");
135
+ }
136
+
137
+ // Verdict mapping (most-restrictive-wins, deterministic)
138
+ let verdict;
139
+ if (blockers.length > 0) {
140
+ verdict = "FAIL";
141
+ } else if (warnings.length > 0) {
142
+ verdict = "PASS_WITH_NOTES";
143
+ } else {
144
+ verdict = "PASS";
145
+ }
146
+
147
+ return { score, max, components, verdict, blockers, warnings };
148
+ }
149
+
150
+ // ---------------------------------------------------------------------------
151
+ // Thin signal gatherer — reads fs artifacts; wraps exceptions → null.
152
+ // Not the test target; computeReadiness is the tested unit.
153
+ // ---------------------------------------------------------------------------
154
+
155
+ function parseGateField(verifyBlock, label) {
156
+ const escaped = label.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
157
+ const match = verifyBlock.match(new RegExp(`- ${escaped}: \`([^\\r\\n\`]+)\``));
158
+ return match ? match[1] : null;
159
+ }
160
+
161
+ function extractVerifyBlock(text) {
162
+ const match = text.match(/<!-- SDTK-CODE:VERIFY:START -->([\s\S]*?)<!-- SDTK-CODE:VERIFY:END -->/);
163
+ return match ? match[1] : null;
164
+ }
165
+
166
+ function readAllEvents(eventsDir) {
167
+ const events = [];
168
+ let files;
169
+ try {
170
+ files = fs.readdirSync(eventsDir).filter((f) => f.endsWith(".jsonl"));
171
+ } catch (_e) {
172
+ return events;
173
+ }
174
+ for (const file of files) {
175
+ const filePath = path.join(eventsDir, file);
176
+ try {
177
+ const lines = fs.readFileSync(filePath, "utf8").split(/\r?\n/);
178
+ for (const line of lines) {
179
+ if (!line.trim()) continue;
180
+ try {
181
+ events.push(JSON.parse(line));
182
+ } catch (_e) {
183
+ // skip malformed line
184
+ }
185
+ }
186
+ } catch (_e) {
187
+ // skip unreadable file
188
+ }
189
+ }
190
+ return events;
191
+ }
192
+
193
+ function gatherSignals(projectPath, featureKey) {
194
+ try {
195
+ const resolved = path.resolve(projectPath || ".");
196
+ const signals = {};
197
+
198
+ // SPEC handoff
199
+ const codeHandoffPath = path.join(resolved, "docs", "dev", `CODE_HANDOFF_${featureKey}.json`);
200
+ signals.hasCodeHandoff = fs.existsSync(codeHandoffPath);
201
+
202
+ // OPS handoff
203
+ const opsHandoffPath = path.join(resolved, "docs", "dev", `OPS_HANDOFF_${featureKey}.json`);
204
+ signals.hasOpsHandoff = fs.existsSync(opsHandoffPath);
205
+
206
+ // UI scope detection: check CODE_HANDOFF required_refs for docs/design/ entries
207
+ signals.uiScope = false;
208
+ signals.hasDesignHandoff = false;
209
+ if (signals.hasCodeHandoff) {
210
+ try {
211
+ const codeHandoff = JSON.parse(fs.readFileSync(codeHandoffPath, "utf8"));
212
+ if (Array.isArray(codeHandoff.required_refs)) {
213
+ signals.uiScope = codeHandoff.required_refs.some(
214
+ (r) => typeof r === "string" && r.startsWith("docs/design/")
215
+ );
216
+ }
217
+ } catch (_e) {
218
+ // ignore malformed CODE_HANDOFF for signal gathering
219
+ }
220
+ }
221
+ if (signals.uiScope) {
222
+ const designHandoffPath = path.join(resolved, "docs", "dev", `DESIGN_HANDOFF_${featureKey}.md`);
223
+ signals.hasDesignHandoff = fs.existsSync(designHandoffPath);
224
+ }
225
+
226
+ // verifyGates from CODE_WORKFLOW artifact VERIFY block
227
+ signals.verifyGates = { spec: "absent", quality: "absent", evidence: "absent" };
228
+ const workflowPath = path.join(resolved, "docs", "dev", `CODE_WORKFLOW_${featureKey}.md`);
229
+ if (fs.existsSync(workflowPath)) {
230
+ try {
231
+ const text = fs.readFileSync(workflowPath, "utf8");
232
+ const verifyBlock = extractVerifyBlock(text);
233
+ if (verifyBlock) {
234
+ const specGate = parseGateField(verifyBlock, "Spec/compliance gate");
235
+ const qualityGate = parseGateField(verifyBlock, "Quality gate");
236
+ const evidenceGate = parseGateField(verifyBlock, "Evidence gate");
237
+ if (specGate) signals.verifyGates.spec = specGate;
238
+ if (qualityGate) signals.verifyGates.quality = qualityGate;
239
+ if (evidenceGate) signals.verifyGates.evidence = evidenceGate;
240
+ }
241
+ } catch (_e) {
242
+ // leave verifyGates as absent
243
+ }
244
+ }
245
+
246
+ // Trust events: guardrail_check and file_scope_check
247
+ signals.guardrail = { denyCount: 0, askCount: 0 };
248
+ signals.fileScope = { verdict: "absent" };
249
+ const eventsDir = path.join(resolved, ".sdtk", "trust", "events");
250
+ if (fs.existsSync(eventsDir)) {
251
+ const events = readAllEvents(eventsDir);
252
+ let denyCount = 0;
253
+ let infoCount = 0;
254
+ const fileScopeEvents = [];
255
+ for (const event of events) {
256
+ if (event.event_type === "guardrail_check") {
257
+ if (event.status === "blocked") denyCount++;
258
+ else infoCount++;
259
+ }
260
+ if (event.event_type === "file_scope_check") {
261
+ fileScopeEvents.push(event);
262
+ }
263
+ }
264
+ signals.guardrail = { denyCount, askCount: infoCount };
265
+ if (fileScopeEvents.length > 0) {
266
+ const last = fileScopeEvents[fileScopeEvents.length - 1];
267
+ let verdict = "pass";
268
+ if (last.status === "blocked") verdict = "fail";
269
+ else if (last.status === "needs_human") verdict = "needs_human";
270
+ signals.fileScope = { verdict };
271
+ }
272
+ }
273
+
274
+ return signals;
275
+ } catch (_error) {
276
+ // Any top-level read failure → null signals → ENV_BLOCKED in computeReadiness
277
+ return null;
278
+ }
279
+ }
280
+
281
+ module.exports = { computeReadiness, gatherSignals };