sdtk-code-kit 0.3.0 → 0.3.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/README.md +204 -204
- package/assets/manifest/toolkit-bundle.manifest.json +147 -147
- package/assets/manifest/toolkit-bundle.sha256.txt +5 -5
- package/bin/sdtk-code.js +14 -14
- package/package.json +44 -44
- package/src/commands/agent-team.js +479 -174
- package/src/commands/build.js +53 -53
- package/src/commands/doctor.js +51 -51
- package/src/commands/generate.js +11 -11
- package/src/commands/guardrails.js +215 -215
- package/src/commands/help.js +266 -257
- package/src/commands/init.js +98 -98
- package/src/commands/plan.js +53 -53
- package/src/commands/readiness.js +77 -77
- package/src/commands/resume.js +61 -61
- package/src/commands/runtime.js +224 -224
- package/src/commands/ship.js +59 -59
- package/src/commands/sleep.js +132 -132
- package/src/commands/start.js +89 -89
- package/src/commands/status.js +77 -77
- package/src/commands/update.js +11 -11
- package/src/commands/verify.js +71 -71
- package/src/index.js +112 -112
- package/src/lib/agent-team-author.js +552 -0
- package/src/lib/agent-team-evidence.js +302 -0
- package/src/lib/agent-team-execute.js +892 -0
- package/src/lib/agent-team-export.js +411 -409
- package/src/lib/agent-team-plan.js +514 -514
- package/src/lib/agent-team-readiness.js +161 -0
- package/src/lib/agent-team-report.js +165 -0
- package/src/lib/agent-team-sleep-execute.js +524 -0
- package/src/lib/agent-team-sleep-session.js +278 -0
- package/src/lib/args.js +107 -107
- package/src/lib/errors.js +41 -41
- package/src/lib/model-provider.js +219 -0
- package/src/lib/powershell.js +65 -65
- package/src/lib/project-target-guard.js +53 -53
- package/src/lib/scope.js +72 -72
- package/src/lib/ship-readiness.js +281 -281
- package/src/lib/sleep-plan.js +175 -175
- package/src/lib/sleep-report.js +229 -229
- package/src/lib/toolkit-payload.js +72 -72
- package/src/lib/trust-events.js +264 -185
- package/src/lib/trust-file-scope.js +154 -154
- package/src/lib/trust-policy.js +212 -212
- package/src/lib/trust-redact.js +58 -35
- package/src/lib/trust-trace.js +179 -179
- package/src/lib/update.js +344 -344
- package/src/lib/workflow-artifact.js +503 -503
- package/src/lib/workflow-build.js +127 -127
- package/src/lib/workflow-doctor.js +83 -83
- package/src/lib/workflow-handoff.js +1029 -1029
- package/src/lib/workflow-intake.js +101 -101
- package/src/lib/workflow-plan.js +193 -193
- package/src/lib/workflow-review-packet.js +650 -650
- package/src/lib/workflow-ship.js +213 -213
- package/src/lib/workflow-state.js +438 -438
- package/src/lib/workflow-verify.js +393 -393
|
@@ -1,514 +1,514 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
const fs = require("fs");
|
|
4
|
-
const path = require("path");
|
|
5
|
-
const { ValidationError } = require("./errors");
|
|
6
|
-
const { STOP_CONDITIONS } = require("./sleep-plan");
|
|
7
|
-
const { computeReadiness, gatherSignals } = require("./ship-readiness");
|
|
8
|
-
const { generateId } = require("./trust-events");
|
|
9
|
-
|
|
10
|
-
const PLAN_SCHEMA = "sdtk.agent-team-plan.v1";
|
|
11
|
-
const SLEEP_PLAN_SCHEMA = "sdtk.sleep-plan.v1";
|
|
12
|
-
const READINESS_FLOOR = Object.freeze({
|
|
13
|
-
score: 75,
|
|
14
|
-
max: 85,
|
|
15
|
-
source: "ship-readiness.computeReadiness",
|
|
16
|
-
});
|
|
17
|
-
|
|
18
|
-
// Secret-bearing paths no role may ever touch. Mirrors the Sleep/Trust posture.
|
|
19
|
-
const SECRET_BLOCKED_SCOPES = Object.freeze([
|
|
20
|
-
".env",
|
|
21
|
-
".env.*",
|
|
22
|
-
"secrets/**",
|
|
23
|
-
"**/*.pem",
|
|
24
|
-
]);
|
|
25
|
-
|
|
26
|
-
const BOUNDARY_LINE =
|
|
27
|
-
"Preview only. This does not execute agents and Sleep execution is not available in this phase.";
|
|
28
|
-
|
|
29
|
-
// Canonical 8 stop-condition IDs, sourced (not re-listed) from the shipped sleep plan.
|
|
30
|
-
function stopConditionIds() {
|
|
31
|
-
return STOP_CONDITIONS.map((condition) => condition.id);
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
function list(value) {
|
|
35
|
-
return Array.isArray(value) ? value.filter((entry) => typeof entry === "string" && entry.length > 0) : [];
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
function uniqueScopes(base, extra) {
|
|
39
|
-
const seen = new Set();
|
|
40
|
-
const out = [];
|
|
41
|
-
for (const entry of [...list(base), ...list(extra)]) {
|
|
42
|
-
if (!seen.has(entry)) {
|
|
43
|
-
seen.add(entry);
|
|
44
|
-
out.push(entry);
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
return out;
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
/**
|
|
51
|
-
* Deterministic Phase A role skeleton (Orchestrator decision B1).
|
|
52
|
-
* Exactly one writer (`dev`); `pm` and `qa` are read-only. DESIGN/OPS roles are
|
|
53
|
-
* intentionally omitted unless their own handoff contracts exist as inputs
|
|
54
|
-
* (decision B2-A: do not fabricate placeholder roles).
|
|
55
|
-
*/
|
|
56
|
-
function deriveRoles(featureKey, scope, testObligations) {
|
|
57
|
-
const allowed = list(scope.allowed_file_scopes);
|
|
58
|
-
const blocked = uniqueScopes(scope.blocked_file_scopes, SECRET_BLOCKED_SCOPES);
|
|
59
|
-
const verificationCommands = list(testObligations);
|
|
60
|
-
|
|
61
|
-
return [
|
|
62
|
-
{
|
|
63
|
-
role: "pm",
|
|
64
|
-
runtime: "claude-code",
|
|
65
|
-
task_id: `${featureKey}_PM`,
|
|
66
|
-
task_slug: "SCOPE_REVIEW",
|
|
67
|
-
objective: `Review scope and acceptance boundary for ${featureKey}; confirm the declared file scopes and stop conditions are sufficient before any implementation.`,
|
|
68
|
-
allowed_file_scopes: ["governance/Features/**"],
|
|
69
|
-
blocked_file_scopes: [...SECRET_BLOCKED_SCOPES],
|
|
70
|
-
can_write: false,
|
|
71
|
-
test_obligations: [],
|
|
72
|
-
verification_commands: [],
|
|
73
|
-
evidence_required: ["review_report"],
|
|
74
|
-
},
|
|
75
|
-
{
|
|
76
|
-
role: "dev",
|
|
77
|
-
runtime: "codex",
|
|
78
|
-
task_id: `${featureKey}_DEV`,
|
|
79
|
-
task_slug: "IMPLEMENTATION",
|
|
80
|
-
objective: `Implement the bounded slices for ${featureKey} strictly within the declared allowed scope. Do not touch blocked paths. Stop at any stop condition.`,
|
|
81
|
-
allowed_file_scopes: allowed,
|
|
82
|
-
blocked_file_scopes: blocked,
|
|
83
|
-
can_write: true,
|
|
84
|
-
test_obligations: verificationCommands,
|
|
85
|
-
verification_commands: verificationCommands,
|
|
86
|
-
evidence_required: ["code_diff", "test_evidence"],
|
|
87
|
-
},
|
|
88
|
-
{
|
|
89
|
-
role: "qa",
|
|
90
|
-
runtime: "claude-code",
|
|
91
|
-
task_id: `${featureKey}_QA`,
|
|
92
|
-
task_slug: "VERIFICATION",
|
|
93
|
-
objective: `Verify the dev evidence for ${featureKey} against the declared test obligations and confirm no write landed outside the allowed scope.`,
|
|
94
|
-
allowed_file_scopes: [],
|
|
95
|
-
blocked_file_scopes: [...SECRET_BLOCKED_SCOPES],
|
|
96
|
-
can_write: false,
|
|
97
|
-
test_obligations: verificationCommands,
|
|
98
|
-
verification_commands: verificationCommands,
|
|
99
|
-
evidence_required: ["verification_report"],
|
|
100
|
-
},
|
|
101
|
-
];
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
/**
|
|
105
|
-
* Pure assembly + fail-closed validation from already-loaded inputs.
|
|
106
|
-
* No fs, no execution — safe to unit test with crafted objects.
|
|
107
|
-
*/
|
|
108
|
-
function buildAgentTeamPlan(handoff, sleepPlan, opts) {
|
|
109
|
-
opts = opts || {};
|
|
110
|
-
|
|
111
|
-
if (!handoff || typeof handoff !== "object") {
|
|
112
|
-
throw new ValidationError("NEEDS_CODE_HANDOFF: handoff object is missing or invalid.");
|
|
113
|
-
}
|
|
114
|
-
if (handoff.handoff_status !== "READY_FOR_SDTK_CODE") {
|
|
115
|
-
throw new ValidationError(
|
|
116
|
-
`NEEDS_CODE_HANDOFF: handoff status is "${handoff.handoff_status}", expected "READY_FOR_SDTK_CODE".`
|
|
117
|
-
);
|
|
118
|
-
}
|
|
119
|
-
if (!Array.isArray(handoff.implementation_slices) || handoff.implementation_slices.length === 0) {
|
|
120
|
-
throw new ValidationError("NEEDS_CODE_HANDOFF: handoff must have at least one implementation_slices entry.");
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
if (!sleepPlan || typeof sleepPlan !== "object" || sleepPlan.schema_version !== SLEEP_PLAN_SCHEMA) {
|
|
124
|
-
throw new ValidationError(
|
|
125
|
-
`NEEDS_SLEEP_PLAN: expected a ${SLEEP_PLAN_SCHEMA} contract. Run \`sdtk-code sleep plan\` first.`
|
|
126
|
-
);
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
const featureKey = handoff.feature_key;
|
|
130
|
-
if (sleepPlan.feature_key !== featureKey) {
|
|
131
|
-
throw new ValidationError(
|
|
132
|
-
`NEEDS_SLEEP_PLAN: sleep-plan feature_key "${sleepPlan.feature_key}" does not match handoff "${featureKey}".`
|
|
133
|
-
);
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
const scope = sleepPlan.scope && typeof sleepPlan.scope === "object" ? sleepPlan.scope : {};
|
|
137
|
-
if (scope.declared !== true) {
|
|
138
|
-
throw new ValidationError(
|
|
139
|
-
"NEEDS_AGENT_SCOPE: sleep-plan scope is not declared; an agent team cannot be bounded. Declare allowed/blocked scope first."
|
|
140
|
-
);
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
// Prefer sleep-plan obligations (already normalized), fall back to handoff.
|
|
144
|
-
const testObligations = list(sleepPlan.test_obligations).length > 0
|
|
145
|
-
? list(sleepPlan.test_obligations)
|
|
146
|
-
: list(handoff.test_obligations);
|
|
147
|
-
|
|
148
|
-
const roles = deriveRoles(featureKey, scope, testObligations);
|
|
149
|
-
|
|
150
|
-
// Fail-closed safety gates.
|
|
151
|
-
const writers = roles.filter((role) => role.can_write === true);
|
|
152
|
-
if (writers.length !== 1) {
|
|
153
|
-
throw new ValidationError(
|
|
154
|
-
`FAIL_SCOPE_UNSAFE: Phase A requires exactly one writer role, found ${writers.length}.`
|
|
155
|
-
);
|
|
156
|
-
}
|
|
157
|
-
const writer = writers[0];
|
|
158
|
-
if (writer.allowed_file_scopes.length === 0) {
|
|
159
|
-
throw new ValidationError(
|
|
160
|
-
"FAIL_SCOPE_UNSAFE: the writer role has no allowed_file_scopes; a writer with unbounded scope is rejected."
|
|
161
|
-
);
|
|
162
|
-
}
|
|
163
|
-
if (writer.verification_commands.length === 0) {
|
|
164
|
-
throw new ValidationError(
|
|
165
|
-
"FAIL_EVIDENCE_MISSING: the writer role has no verification_commands (no test obligations carried)."
|
|
166
|
-
);
|
|
167
|
-
}
|
|
168
|
-
for (const role of roles) {
|
|
169
|
-
if (!Array.isArray(role.evidence_required) || role.evidence_required.length === 0) {
|
|
170
|
-
throw new ValidationError(
|
|
171
|
-
`FAIL_EVIDENCE_MISSING: role "${role.role}" has no evidence_required.`
|
|
172
|
-
);
|
|
173
|
-
}
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
return {
|
|
177
|
-
schema_version: PLAN_SCHEMA,
|
|
178
|
-
feature_key: featureKey,
|
|
179
|
-
run_id: typeof opts.runId === "string" && opts.runId.length > 0 ? opts.runId : generateId("agt"),
|
|
180
|
-
run_mode: "preview",
|
|
181
|
-
runtime: "native-runtime-adapter-preview",
|
|
182
|
-
source: {
|
|
183
|
-
code_handoff_path: opts.codeHandoffPath || `docs/dev/CODE_HANDOFF_${featureKey}.json`,
|
|
184
|
-
sleep_plan_path: opts.sleepPlanPath || ".sdtk/trust/sleep-plan.json",
|
|
185
|
-
},
|
|
186
|
-
readiness_floor: { ...READINESS_FLOOR },
|
|
187
|
-
roles,
|
|
188
|
-
stop_conditions: stopConditionIds(),
|
|
189
|
-
deploy_confirm_points: Array.isArray(sleepPlan.deploy_confirm_points)
|
|
190
|
-
? [...sleepPlan.deploy_confirm_points]
|
|
191
|
-
: [],
|
|
192
|
-
generated_at: new Date().toISOString(),
|
|
193
|
-
};
|
|
194
|
-
}
|
|
195
|
-
|
|
196
|
-
/** Thin fs wrapper: read CODE_HANDOFF + sleep-plan, then buildAgentTeamPlan. */
|
|
197
|
-
function assembleAgentTeamPlan(projectPath, featureKey, opts) {
|
|
198
|
-
opts = opts || {};
|
|
199
|
-
const resolved = path.resolve(projectPath || ".");
|
|
200
|
-
|
|
201
|
-
const handoffRel = `docs/dev/CODE_HANDOFF_${featureKey}.json`;
|
|
202
|
-
const handoffPath = path.join(resolved, "docs", "dev", `CODE_HANDOFF_${featureKey}.json`);
|
|
203
|
-
let handoff;
|
|
204
|
-
try {
|
|
205
|
-
handoff = JSON.parse(fs.readFileSync(handoffPath, "utf8"));
|
|
206
|
-
} catch (_err) {
|
|
207
|
-
throw new ValidationError(
|
|
208
|
-
`NEEDS_CODE_HANDOFF: CODE_HANDOFF_${featureKey}.json not found or invalid at ${handoffPath}.`
|
|
209
|
-
);
|
|
210
|
-
}
|
|
211
|
-
|
|
212
|
-
const sleepPlanRel = ".sdtk/trust/sleep-plan.json";
|
|
213
|
-
const sleepPlanPath = path.join(resolved, ".sdtk", "trust", "sleep-plan.json");
|
|
214
|
-
let sleepPlan;
|
|
215
|
-
try {
|
|
216
|
-
sleepPlan = JSON.parse(fs.readFileSync(sleepPlanPath, "utf8"));
|
|
217
|
-
} catch (_err) {
|
|
218
|
-
throw new ValidationError(
|
|
219
|
-
`NEEDS_SLEEP_PLAN: no sleep plan at ${sleepPlanPath}. Run \`sdtk-code sleep plan --feature-key ${featureKey}\` first.`
|
|
220
|
-
);
|
|
221
|
-
}
|
|
222
|
-
|
|
223
|
-
return buildAgentTeamPlan(handoff, sleepPlan, {
|
|
224
|
-
runId: opts.runId,
|
|
225
|
-
codeHandoffPath: handoffRel,
|
|
226
|
-
sleepPlanPath: sleepPlanRel,
|
|
227
|
-
});
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
function bulletList(items, fallback) {
|
|
231
|
-
if (!Array.isArray(items) || items.length === 0) {
|
|
232
|
-
return `- ${fallback}`;
|
|
233
|
-
}
|
|
234
|
-
return items.map((item) => `- ${String(item)}`).join("\n");
|
|
235
|
-
}
|
|
236
|
-
|
|
237
|
-
/** Render one bounded role task packet (kept compact for /goal <4000 chars). */
|
|
238
|
-
function renderTaskPacket(plan, role) {
|
|
239
|
-
return [
|
|
240
|
-
`# Agent Task Packet — ${plan.feature_key} / ${role.role}`,
|
|
241
|
-
"",
|
|
242
|
-
BOUNDARY_LINE,
|
|
243
|
-
"",
|
|
244
|
-
`- Feature key: ${plan.feature_key}`,
|
|
245
|
-
`- Role: ${role.role}`,
|
|
246
|
-
`- Runtime target: ${role.runtime}`,
|
|
247
|
-
`- Can write: ${role.can_write ? "yes" : "no"}`,
|
|
248
|
-
`- Run id: ${plan.run_id}`,
|
|
249
|
-
`- Task id: ${role.task_id}`,
|
|
250
|
-
"",
|
|
251
|
-
"## Objective",
|
|
252
|
-
"",
|
|
253
|
-
role.objective,
|
|
254
|
-
"",
|
|
255
|
-
"## Allowed file scopes",
|
|
256
|
-
"",
|
|
257
|
-
bulletList(role.allowed_file_scopes, "none (read-only role)"),
|
|
258
|
-
"",
|
|
259
|
-
"## Blocked file scopes",
|
|
260
|
-
"",
|
|
261
|
-
bulletList(role.blocked_file_scopes, "none declared"),
|
|
262
|
-
"",
|
|
263
|
-
"## Verification commands",
|
|
264
|
-
"",
|
|
265
|
-
bulletList(role.verification_commands, "none"),
|
|
266
|
-
"",
|
|
267
|
-
"## Evidence required",
|
|
268
|
-
"",
|
|
269
|
-
bulletList(role.evidence_required, "none"),
|
|
270
|
-
"",
|
|
271
|
-
"## Stop conditions",
|
|
272
|
-
"",
|
|
273
|
-
bulletList(plan.stop_conditions, "none declared"),
|
|
274
|
-
"",
|
|
275
|
-
"## Boundary",
|
|
276
|
-
"",
|
|
277
|
-
"Do not execute outside this scope. Do not spawn processes, publish packages, or deploy.",
|
|
278
|
-
BOUNDARY_LINE,
|
|
279
|
-
"",
|
|
280
|
-
].join("\n");
|
|
281
|
-
}
|
|
282
|
-
|
|
283
|
-
function taskPacketFilename(role) {
|
|
284
|
-
return `${role.role.toUpperCase()}_${role.task_slug}.md`;
|
|
285
|
-
}
|
|
286
|
-
|
|
287
|
-
/** Write the machine-readable plan JSON; returns absolute path. */
|
|
288
|
-
function writeAgentTeamPlan(projectPath, plan) {
|
|
289
|
-
const resolved = path.resolve(projectPath || ".");
|
|
290
|
-
const dir = path.join(resolved, ".sdtk", "agent-team");
|
|
291
|
-
fs.mkdirSync(dir, { recursive: true });
|
|
292
|
-
const outPath = path.join(dir, `AGENT_TEAM_PLAN_${plan.feature_key}.json`);
|
|
293
|
-
fs.writeFileSync(outPath, JSON.stringify(plan, null, 2), "utf8");
|
|
294
|
-
return outPath;
|
|
295
|
-
}
|
|
296
|
-
|
|
297
|
-
/** Write one Markdown task packet per role; returns absolute paths. */
|
|
298
|
-
function writeTaskPackets(projectPath, plan) {
|
|
299
|
-
const resolved = path.resolve(projectPath || ".");
|
|
300
|
-
const dir = path.join(resolved, ".sdtk", "agent-team", "tasks", plan.feature_key);
|
|
301
|
-
fs.mkdirSync(dir, { recursive: true });
|
|
302
|
-
const written = [];
|
|
303
|
-
for (const role of plan.roles) {
|
|
304
|
-
const outPath = path.join(dir, taskPacketFilename(role));
|
|
305
|
-
fs.writeFileSync(outPath, renderTaskPacket(plan, role), "utf8");
|
|
306
|
-
written.push(outPath);
|
|
307
|
-
}
|
|
308
|
-
return written;
|
|
309
|
-
}
|
|
310
|
-
|
|
311
|
-
/** Load a previously written plan (preview reads, does not re-derive). */
|
|
312
|
-
function loadAgentTeamPlan(projectPath, featureKey) {
|
|
313
|
-
const resolved = path.resolve(projectPath || ".");
|
|
314
|
-
const planPath = path.join(resolved, ".sdtk", "agent-team", `AGENT_TEAM_PLAN_${featureKey}.json`);
|
|
315
|
-
let plan;
|
|
316
|
-
try {
|
|
317
|
-
plan = JSON.parse(fs.readFileSync(planPath, "utf8"));
|
|
318
|
-
} catch (_err) {
|
|
319
|
-
throw new ValidationError(
|
|
320
|
-
`NEEDS_AGENT_TEAM_PLAN: no agent-team plan at ${planPath}. Run \`sdtk-code agent-team plan --feature-key ${featureKey}\` first.`
|
|
321
|
-
);
|
|
322
|
-
}
|
|
323
|
-
return plan;
|
|
324
|
-
}
|
|
325
|
-
|
|
326
|
-
// Defensive coercion: never assume a loaded/corrupt plan has array fields.
|
|
327
|
-
function asArray(value) {
|
|
328
|
-
return Array.isArray(value) ? value : [];
|
|
329
|
-
}
|
|
330
|
-
|
|
331
|
-
function roleName(role) {
|
|
332
|
-
return role && typeof role.role === "string" && role.role.length > 0 ? role.role : "(unnamed)";
|
|
333
|
-
}
|
|
334
|
-
|
|
335
|
-
/**
|
|
336
|
-
* Structural preview projection of a loaded plan. Phase A does NOT gate on
|
|
337
|
-
* readiness (decision B3) — readiness is attached as informational metadata.
|
|
338
|
-
*
|
|
339
|
-
* This runs against an arbitrary on-disk plan JSON that may be corrupt or
|
|
340
|
-
* hand-edited, so every field access is defensive: a malformed plan must yield
|
|
341
|
-
* a FAIL verdict, never a PASS and never a thrown TypeError.
|
|
342
|
-
*/
|
|
343
|
-
function projectAgentTeamPreview(plan, readiness) {
|
|
344
|
-
const blockers = [];
|
|
345
|
-
if (!plan || typeof plan !== "object") {
|
|
346
|
-
blockers.push("Invalid agent-team plan — not an object.");
|
|
347
|
-
}
|
|
348
|
-
if (!plan || plan.schema_version !== PLAN_SCHEMA) {
|
|
349
|
-
blockers.push("Invalid agent-team plan — expected sdtk.agent-team-plan.v1.");
|
|
350
|
-
}
|
|
351
|
-
|
|
352
|
-
if (plan && plan.roles !== undefined && !Array.isArray(plan.roles)) {
|
|
353
|
-
blockers.push("Invalid agent-team plan — roles is present but not an array.");
|
|
354
|
-
} else if (!plan || !Array.isArray(plan.roles)) {
|
|
355
|
-
blockers.push("Invalid agent-team plan — roles is missing.");
|
|
356
|
-
}
|
|
357
|
-
|
|
358
|
-
// Only object entries are previewable; drop non-object junk defensively.
|
|
359
|
-
const roles = asArray(plan && plan.roles).filter((role) => role && typeof role === "object");
|
|
360
|
-
|
|
361
|
-
// Per-role structural validation — each scope/evidence field must be an array.
|
|
362
|
-
for (const role of roles) {
|
|
363
|
-
const name = roleName(role);
|
|
364
|
-
if (!Array.isArray(role.allowed_file_scopes)) {
|
|
365
|
-
blockers.push(`Role "${name}" allowed_file_scopes is missing or not an array.`);
|
|
366
|
-
}
|
|
367
|
-
if (!Array.isArray(role.blocked_file_scopes)) {
|
|
368
|
-
blockers.push(`Role "${name}" blocked_file_scopes is missing or not an array.`);
|
|
369
|
-
}
|
|
370
|
-
if (!Array.isArray(role.evidence_required) || role.evidence_required.length === 0) {
|
|
371
|
-
blockers.push(`Role "${name}" has no evidence_required.`);
|
|
372
|
-
}
|
|
373
|
-
if (!Array.isArray(role.verification_commands)) {
|
|
374
|
-
blockers.push(`Role "${name}" verification_commands is missing or not an array.`);
|
|
375
|
-
}
|
|
376
|
-
}
|
|
377
|
-
|
|
378
|
-
// Writer invariants: exactly one writer, with a bounded scope and verification.
|
|
379
|
-
const writers = roles.filter((role) => role.can_write === true);
|
|
380
|
-
if (writers.length !== 1) {
|
|
381
|
-
blockers.push(`Expected exactly one writer role, found ${writers.length}.`);
|
|
382
|
-
} else {
|
|
383
|
-
const writer = writers[0];
|
|
384
|
-
if (asArray(writer.allowed_file_scopes).length === 0) {
|
|
385
|
-
blockers.push(`Writer role "${roleName(writer)}" has empty allowed_file_scopes (unbounded writer).`);
|
|
386
|
-
}
|
|
387
|
-
if (asArray(writer.verification_commands).length === 0) {
|
|
388
|
-
blockers.push(`Writer role "${roleName(writer)}" has no verification_commands.`);
|
|
389
|
-
}
|
|
390
|
-
}
|
|
391
|
-
|
|
392
|
-
const expectedStops = stopConditionIds();
|
|
393
|
-
const planStops = asArray(plan && plan.stop_conditions);
|
|
394
|
-
const stopsMatch = planStops.length === expectedStops.length
|
|
395
|
-
&& expectedStops.every((id) => planStops.includes(id));
|
|
396
|
-
if (!stopsMatch) {
|
|
397
|
-
blockers.push("Stop conditions do not match the canonical 8 sleep-plan IDs.");
|
|
398
|
-
}
|
|
399
|
-
|
|
400
|
-
const verdict = blockers.length === 0
|
|
401
|
-
? "PASS_AGENT_TEAM_PREVIEW_READY"
|
|
402
|
-
: "FAIL_AGENT_TEAM_PREVIEW_NOT_READY";
|
|
403
|
-
|
|
404
|
-
return {
|
|
405
|
-
verdict,
|
|
406
|
-
feature_key: plan && plan.feature_key,
|
|
407
|
-
run_id: plan && plan.run_id,
|
|
408
|
-
blockers,
|
|
409
|
-
roles,
|
|
410
|
-
readiness: readiness || null,
|
|
411
|
-
generated_at: new Date().toISOString(),
|
|
412
|
-
};
|
|
413
|
-
}
|
|
414
|
-
|
|
415
|
-
// Renders even a FAIL/corrupt preview without throwing — every field access is
|
|
416
|
-
// coerced so a malformed plan still produces a readable fail-closed report.
|
|
417
|
-
function renderPreviewMarkdown(plan, preview) {
|
|
418
|
-
const safePlan = plan && typeof plan === "object" ? plan : {};
|
|
419
|
-
const source = safePlan.source && typeof safePlan.source === "object" ? safePlan.source : {};
|
|
420
|
-
const floor = safePlan.readiness_floor && typeof safePlan.readiness_floor === "object"
|
|
421
|
-
? safePlan.readiness_floor
|
|
422
|
-
: {};
|
|
423
|
-
|
|
424
|
-
const roleRows = asArray(preview && preview.roles).map((role) => {
|
|
425
|
-
const mode = role.can_write ? "write" : "read-only";
|
|
426
|
-
const allowedN = asArray(role.allowed_file_scopes).length;
|
|
427
|
-
const blockedN = asArray(role.blocked_file_scopes).length;
|
|
428
|
-
const evidence = asArray(role.evidence_required).join(", ");
|
|
429
|
-
return `| ${roleName(role)} | ${role.runtime || "?"} | ${mode} | ${allowedN} allowed / ${blockedN} blocked | ${evidence} |`;
|
|
430
|
-
});
|
|
431
|
-
|
|
432
|
-
const readinessLine = preview && preview.readiness
|
|
433
|
-
? `- Current readiness (informational, not a gate in Phase A): ${preview.readiness.score}/${preview.readiness.max} — ${preview.readiness.verdict}`
|
|
434
|
-
: "- Current readiness: unavailable";
|
|
435
|
-
|
|
436
|
-
const floorScore = floor.score != null ? floor.score : "?";
|
|
437
|
-
const floorMax = floor.max != null ? floor.max : "?";
|
|
438
|
-
const floorSource = floor.source || "ship-readiness.computeReadiness";
|
|
439
|
-
|
|
440
|
-
return [
|
|
441
|
-
`# Agent Team Preview — ${(preview && preview.feature_key) || safePlan.feature_key || "(unknown)"}`,
|
|
442
|
-
"",
|
|
443
|
-
BOUNDARY_LINE,
|
|
444
|
-
"",
|
|
445
|
-
"## Verdict",
|
|
446
|
-
"",
|
|
447
|
-
`- Verdict: ${preview && preview.verdict ? preview.verdict : "FAIL_AGENT_TEAM_PREVIEW_NOT_READY"}`,
|
|
448
|
-
`- Run id: ${(preview && preview.run_id) || safePlan.run_id || "(none)"}`,
|
|
449
|
-
"- Blockers:",
|
|
450
|
-
bulletList(preview && preview.blockers, "none"),
|
|
451
|
-
"",
|
|
452
|
-
"## Inputs",
|
|
453
|
-
"",
|
|
454
|
-
`- CODE_HANDOFF: ${source.code_handoff_path || "(unknown)"}`,
|
|
455
|
-
`- Sleep plan: ${source.sleep_plan_path || "(unknown)"}`,
|
|
456
|
-
"",
|
|
457
|
-
"## Roles",
|
|
458
|
-
"",
|
|
459
|
-
"| Role | Runtime | Mode | Scope | Evidence |",
|
|
460
|
-
"| --- | --- | --- | --- | --- |",
|
|
461
|
-
...(roleRows.length > 0 ? roleRows : ["| (none) | | | | |"]),
|
|
462
|
-
"",
|
|
463
|
-
"## Readiness floor",
|
|
464
|
-
"",
|
|
465
|
-
`- Floor: ${floorScore}/${floorMax} via ${floorSource}`,
|
|
466
|
-
readinessLine,
|
|
467
|
-
"",
|
|
468
|
-
"## Stop conditions",
|
|
469
|
-
"",
|
|
470
|
-
bulletList(asArray(safePlan.stop_conditions), "none declared"),
|
|
471
|
-
"",
|
|
472
|
-
"## Boundary",
|
|
473
|
-
"",
|
|
474
|
-
BOUNDARY_LINE,
|
|
475
|
-
"",
|
|
476
|
-
].join("\n");
|
|
477
|
-
}
|
|
478
|
-
|
|
479
|
-
function writeAgentTeamPreview(projectPath, featureKey, markdown) {
|
|
480
|
-
const resolved = path.resolve(projectPath || ".");
|
|
481
|
-
const outDir = path.join(resolved, "docs", "trust");
|
|
482
|
-
fs.mkdirSync(outDir, { recursive: true });
|
|
483
|
-
const outPath = path.join(outDir, `AGENT_TEAM_PREVIEW_${featureKey}.md`);
|
|
484
|
-
fs.writeFileSync(outPath, markdown, "utf8");
|
|
485
|
-
return outPath;
|
|
486
|
-
}
|
|
487
|
-
|
|
488
|
-
/** Informational readiness signal for the preview (never gates in Phase A). */
|
|
489
|
-
function gatherPreviewReadiness(projectPath, featureKey) {
|
|
490
|
-
try {
|
|
491
|
-
return computeReadiness(gatherSignals(projectPath, featureKey));
|
|
492
|
-
} catch (_err) {
|
|
493
|
-
return null;
|
|
494
|
-
}
|
|
495
|
-
}
|
|
496
|
-
|
|
497
|
-
module.exports = {
|
|
498
|
-
PLAN_SCHEMA,
|
|
499
|
-
SECRET_BLOCKED_SCOPES,
|
|
500
|
-
BOUNDARY_LINE,
|
|
501
|
-
stopConditionIds,
|
|
502
|
-
deriveRoles,
|
|
503
|
-
buildAgentTeamPlan,
|
|
504
|
-
assembleAgentTeamPlan,
|
|
505
|
-
writeAgentTeamPlan,
|
|
506
|
-
renderTaskPacket,
|
|
507
|
-
taskPacketFilename,
|
|
508
|
-
writeTaskPackets,
|
|
509
|
-
loadAgentTeamPlan,
|
|
510
|
-
projectAgentTeamPreview,
|
|
511
|
-
renderPreviewMarkdown,
|
|
512
|
-
writeAgentTeamPreview,
|
|
513
|
-
gatherPreviewReadiness,
|
|
514
|
-
};
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const fs = require("fs");
|
|
4
|
+
const path = require("path");
|
|
5
|
+
const { ValidationError } = require("./errors");
|
|
6
|
+
const { STOP_CONDITIONS } = require("./sleep-plan");
|
|
7
|
+
const { computeReadiness, gatherSignals } = require("./ship-readiness");
|
|
8
|
+
const { generateId } = require("./trust-events");
|
|
9
|
+
|
|
10
|
+
const PLAN_SCHEMA = "sdtk.agent-team-plan.v1";
|
|
11
|
+
const SLEEP_PLAN_SCHEMA = "sdtk.sleep-plan.v1";
|
|
12
|
+
const READINESS_FLOOR = Object.freeze({
|
|
13
|
+
score: 75,
|
|
14
|
+
max: 85,
|
|
15
|
+
source: "ship-readiness.computeReadiness",
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
// Secret-bearing paths no role may ever touch. Mirrors the Sleep/Trust posture.
|
|
19
|
+
const SECRET_BLOCKED_SCOPES = Object.freeze([
|
|
20
|
+
".env",
|
|
21
|
+
".env.*",
|
|
22
|
+
"secrets/**",
|
|
23
|
+
"**/*.pem",
|
|
24
|
+
]);
|
|
25
|
+
|
|
26
|
+
const BOUNDARY_LINE =
|
|
27
|
+
"Preview only. This does not execute agents and Sleep execution is not available in this phase.";
|
|
28
|
+
|
|
29
|
+
// Canonical 8 stop-condition IDs, sourced (not re-listed) from the shipped sleep plan.
|
|
30
|
+
function stopConditionIds() {
|
|
31
|
+
return STOP_CONDITIONS.map((condition) => condition.id);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function list(value) {
|
|
35
|
+
return Array.isArray(value) ? value.filter((entry) => typeof entry === "string" && entry.length > 0) : [];
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function uniqueScopes(base, extra) {
|
|
39
|
+
const seen = new Set();
|
|
40
|
+
const out = [];
|
|
41
|
+
for (const entry of [...list(base), ...list(extra)]) {
|
|
42
|
+
if (!seen.has(entry)) {
|
|
43
|
+
seen.add(entry);
|
|
44
|
+
out.push(entry);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
return out;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Deterministic Phase A role skeleton (Orchestrator decision B1).
|
|
52
|
+
* Exactly one writer (`dev`); `pm` and `qa` are read-only. DESIGN/OPS roles are
|
|
53
|
+
* intentionally omitted unless their own handoff contracts exist as inputs
|
|
54
|
+
* (decision B2-A: do not fabricate placeholder roles).
|
|
55
|
+
*/
|
|
56
|
+
function deriveRoles(featureKey, scope, testObligations) {
|
|
57
|
+
const allowed = list(scope.allowed_file_scopes);
|
|
58
|
+
const blocked = uniqueScopes(scope.blocked_file_scopes, SECRET_BLOCKED_SCOPES);
|
|
59
|
+
const verificationCommands = list(testObligations);
|
|
60
|
+
|
|
61
|
+
return [
|
|
62
|
+
{
|
|
63
|
+
role: "pm",
|
|
64
|
+
runtime: "claude-code",
|
|
65
|
+
task_id: `${featureKey}_PM`,
|
|
66
|
+
task_slug: "SCOPE_REVIEW",
|
|
67
|
+
objective: `Review scope and acceptance boundary for ${featureKey}; confirm the declared file scopes and stop conditions are sufficient before any implementation.`,
|
|
68
|
+
allowed_file_scopes: ["governance/Features/**"],
|
|
69
|
+
blocked_file_scopes: [...SECRET_BLOCKED_SCOPES],
|
|
70
|
+
can_write: false,
|
|
71
|
+
test_obligations: [],
|
|
72
|
+
verification_commands: [],
|
|
73
|
+
evidence_required: ["review_report"],
|
|
74
|
+
},
|
|
75
|
+
{
|
|
76
|
+
role: "dev",
|
|
77
|
+
runtime: "codex",
|
|
78
|
+
task_id: `${featureKey}_DEV`,
|
|
79
|
+
task_slug: "IMPLEMENTATION",
|
|
80
|
+
objective: `Implement the bounded slices for ${featureKey} strictly within the declared allowed scope. Do not touch blocked paths. Stop at any stop condition.`,
|
|
81
|
+
allowed_file_scopes: allowed,
|
|
82
|
+
blocked_file_scopes: blocked,
|
|
83
|
+
can_write: true,
|
|
84
|
+
test_obligations: verificationCommands,
|
|
85
|
+
verification_commands: verificationCommands,
|
|
86
|
+
evidence_required: ["code_diff", "test_evidence"],
|
|
87
|
+
},
|
|
88
|
+
{
|
|
89
|
+
role: "qa",
|
|
90
|
+
runtime: "claude-code",
|
|
91
|
+
task_id: `${featureKey}_QA`,
|
|
92
|
+
task_slug: "VERIFICATION",
|
|
93
|
+
objective: `Verify the dev evidence for ${featureKey} against the declared test obligations and confirm no write landed outside the allowed scope.`,
|
|
94
|
+
allowed_file_scopes: [],
|
|
95
|
+
blocked_file_scopes: [...SECRET_BLOCKED_SCOPES],
|
|
96
|
+
can_write: false,
|
|
97
|
+
test_obligations: verificationCommands,
|
|
98
|
+
verification_commands: verificationCommands,
|
|
99
|
+
evidence_required: ["verification_report"],
|
|
100
|
+
},
|
|
101
|
+
];
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Pure assembly + fail-closed validation from already-loaded inputs.
|
|
106
|
+
* No fs, no execution — safe to unit test with crafted objects.
|
|
107
|
+
*/
|
|
108
|
+
function buildAgentTeamPlan(handoff, sleepPlan, opts) {
|
|
109
|
+
opts = opts || {};
|
|
110
|
+
|
|
111
|
+
if (!handoff || typeof handoff !== "object") {
|
|
112
|
+
throw new ValidationError("NEEDS_CODE_HANDOFF: handoff object is missing or invalid.");
|
|
113
|
+
}
|
|
114
|
+
if (handoff.handoff_status !== "READY_FOR_SDTK_CODE") {
|
|
115
|
+
throw new ValidationError(
|
|
116
|
+
`NEEDS_CODE_HANDOFF: handoff status is "${handoff.handoff_status}", expected "READY_FOR_SDTK_CODE".`
|
|
117
|
+
);
|
|
118
|
+
}
|
|
119
|
+
if (!Array.isArray(handoff.implementation_slices) || handoff.implementation_slices.length === 0) {
|
|
120
|
+
throw new ValidationError("NEEDS_CODE_HANDOFF: handoff must have at least one implementation_slices entry.");
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
if (!sleepPlan || typeof sleepPlan !== "object" || sleepPlan.schema_version !== SLEEP_PLAN_SCHEMA) {
|
|
124
|
+
throw new ValidationError(
|
|
125
|
+
`NEEDS_SLEEP_PLAN: expected a ${SLEEP_PLAN_SCHEMA} contract. Run \`sdtk-code sleep plan\` first.`
|
|
126
|
+
);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
const featureKey = handoff.feature_key;
|
|
130
|
+
if (sleepPlan.feature_key !== featureKey) {
|
|
131
|
+
throw new ValidationError(
|
|
132
|
+
`NEEDS_SLEEP_PLAN: sleep-plan feature_key "${sleepPlan.feature_key}" does not match handoff "${featureKey}".`
|
|
133
|
+
);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
const scope = sleepPlan.scope && typeof sleepPlan.scope === "object" ? sleepPlan.scope : {};
|
|
137
|
+
if (scope.declared !== true) {
|
|
138
|
+
throw new ValidationError(
|
|
139
|
+
"NEEDS_AGENT_SCOPE: sleep-plan scope is not declared; an agent team cannot be bounded. Declare allowed/blocked scope first."
|
|
140
|
+
);
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
// Prefer sleep-plan obligations (already normalized), fall back to handoff.
|
|
144
|
+
const testObligations = list(sleepPlan.test_obligations).length > 0
|
|
145
|
+
? list(sleepPlan.test_obligations)
|
|
146
|
+
: list(handoff.test_obligations);
|
|
147
|
+
|
|
148
|
+
const roles = deriveRoles(featureKey, scope, testObligations);
|
|
149
|
+
|
|
150
|
+
// Fail-closed safety gates.
|
|
151
|
+
const writers = roles.filter((role) => role.can_write === true);
|
|
152
|
+
if (writers.length !== 1) {
|
|
153
|
+
throw new ValidationError(
|
|
154
|
+
`FAIL_SCOPE_UNSAFE: Phase A requires exactly one writer role, found ${writers.length}.`
|
|
155
|
+
);
|
|
156
|
+
}
|
|
157
|
+
const writer = writers[0];
|
|
158
|
+
if (writer.allowed_file_scopes.length === 0) {
|
|
159
|
+
throw new ValidationError(
|
|
160
|
+
"FAIL_SCOPE_UNSAFE: the writer role has no allowed_file_scopes; a writer with unbounded scope is rejected."
|
|
161
|
+
);
|
|
162
|
+
}
|
|
163
|
+
if (writer.verification_commands.length === 0) {
|
|
164
|
+
throw new ValidationError(
|
|
165
|
+
"FAIL_EVIDENCE_MISSING: the writer role has no verification_commands (no test obligations carried)."
|
|
166
|
+
);
|
|
167
|
+
}
|
|
168
|
+
for (const role of roles) {
|
|
169
|
+
if (!Array.isArray(role.evidence_required) || role.evidence_required.length === 0) {
|
|
170
|
+
throw new ValidationError(
|
|
171
|
+
`FAIL_EVIDENCE_MISSING: role "${role.role}" has no evidence_required.`
|
|
172
|
+
);
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
return {
|
|
177
|
+
schema_version: PLAN_SCHEMA,
|
|
178
|
+
feature_key: featureKey,
|
|
179
|
+
run_id: typeof opts.runId === "string" && opts.runId.length > 0 ? opts.runId : generateId("agt"),
|
|
180
|
+
run_mode: "preview",
|
|
181
|
+
runtime: "native-runtime-adapter-preview",
|
|
182
|
+
source: {
|
|
183
|
+
code_handoff_path: opts.codeHandoffPath || `docs/dev/CODE_HANDOFF_${featureKey}.json`,
|
|
184
|
+
sleep_plan_path: opts.sleepPlanPath || ".sdtk/trust/sleep-plan.json",
|
|
185
|
+
},
|
|
186
|
+
readiness_floor: { ...READINESS_FLOOR },
|
|
187
|
+
roles,
|
|
188
|
+
stop_conditions: stopConditionIds(),
|
|
189
|
+
deploy_confirm_points: Array.isArray(sleepPlan.deploy_confirm_points)
|
|
190
|
+
? [...sleepPlan.deploy_confirm_points]
|
|
191
|
+
: [],
|
|
192
|
+
generated_at: new Date().toISOString(),
|
|
193
|
+
};
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
/** Thin fs wrapper: read CODE_HANDOFF + sleep-plan, then buildAgentTeamPlan. */
|
|
197
|
+
function assembleAgentTeamPlan(projectPath, featureKey, opts) {
|
|
198
|
+
opts = opts || {};
|
|
199
|
+
const resolved = path.resolve(projectPath || ".");
|
|
200
|
+
|
|
201
|
+
const handoffRel = `docs/dev/CODE_HANDOFF_${featureKey}.json`;
|
|
202
|
+
const handoffPath = path.join(resolved, "docs", "dev", `CODE_HANDOFF_${featureKey}.json`);
|
|
203
|
+
let handoff;
|
|
204
|
+
try {
|
|
205
|
+
handoff = JSON.parse(fs.readFileSync(handoffPath, "utf8"));
|
|
206
|
+
} catch (_err) {
|
|
207
|
+
throw new ValidationError(
|
|
208
|
+
`NEEDS_CODE_HANDOFF: CODE_HANDOFF_${featureKey}.json not found or invalid at ${handoffPath}.`
|
|
209
|
+
);
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
const sleepPlanRel = ".sdtk/trust/sleep-plan.json";
|
|
213
|
+
const sleepPlanPath = path.join(resolved, ".sdtk", "trust", "sleep-plan.json");
|
|
214
|
+
let sleepPlan;
|
|
215
|
+
try {
|
|
216
|
+
sleepPlan = JSON.parse(fs.readFileSync(sleepPlanPath, "utf8"));
|
|
217
|
+
} catch (_err) {
|
|
218
|
+
throw new ValidationError(
|
|
219
|
+
`NEEDS_SLEEP_PLAN: no sleep plan at ${sleepPlanPath}. Run \`sdtk-code sleep plan --feature-key ${featureKey}\` first.`
|
|
220
|
+
);
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
return buildAgentTeamPlan(handoff, sleepPlan, {
|
|
224
|
+
runId: opts.runId,
|
|
225
|
+
codeHandoffPath: handoffRel,
|
|
226
|
+
sleepPlanPath: sleepPlanRel,
|
|
227
|
+
});
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
function bulletList(items, fallback) {
|
|
231
|
+
if (!Array.isArray(items) || items.length === 0) {
|
|
232
|
+
return `- ${fallback}`;
|
|
233
|
+
}
|
|
234
|
+
return items.map((item) => `- ${String(item)}`).join("\n");
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
/** Render one bounded role task packet (kept compact for /goal <4000 chars). */
|
|
238
|
+
function renderTaskPacket(plan, role) {
|
|
239
|
+
return [
|
|
240
|
+
`# Agent Task Packet — ${plan.feature_key} / ${role.role}`,
|
|
241
|
+
"",
|
|
242
|
+
BOUNDARY_LINE,
|
|
243
|
+
"",
|
|
244
|
+
`- Feature key: ${plan.feature_key}`,
|
|
245
|
+
`- Role: ${role.role}`,
|
|
246
|
+
`- Runtime target: ${role.runtime}`,
|
|
247
|
+
`- Can write: ${role.can_write ? "yes" : "no"}`,
|
|
248
|
+
`- Run id: ${plan.run_id}`,
|
|
249
|
+
`- Task id: ${role.task_id}`,
|
|
250
|
+
"",
|
|
251
|
+
"## Objective",
|
|
252
|
+
"",
|
|
253
|
+
role.objective,
|
|
254
|
+
"",
|
|
255
|
+
"## Allowed file scopes",
|
|
256
|
+
"",
|
|
257
|
+
bulletList(role.allowed_file_scopes, "none (read-only role)"),
|
|
258
|
+
"",
|
|
259
|
+
"## Blocked file scopes",
|
|
260
|
+
"",
|
|
261
|
+
bulletList(role.blocked_file_scopes, "none declared"),
|
|
262
|
+
"",
|
|
263
|
+
"## Verification commands",
|
|
264
|
+
"",
|
|
265
|
+
bulletList(role.verification_commands, "none"),
|
|
266
|
+
"",
|
|
267
|
+
"## Evidence required",
|
|
268
|
+
"",
|
|
269
|
+
bulletList(role.evidence_required, "none"),
|
|
270
|
+
"",
|
|
271
|
+
"## Stop conditions",
|
|
272
|
+
"",
|
|
273
|
+
bulletList(plan.stop_conditions, "none declared"),
|
|
274
|
+
"",
|
|
275
|
+
"## Boundary",
|
|
276
|
+
"",
|
|
277
|
+
"Do not execute outside this scope. Do not spawn processes, publish packages, or deploy.",
|
|
278
|
+
BOUNDARY_LINE,
|
|
279
|
+
"",
|
|
280
|
+
].join("\n");
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
function taskPacketFilename(role) {
|
|
284
|
+
return `${role.role.toUpperCase()}_${role.task_slug}.md`;
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
/** Write the machine-readable plan JSON; returns absolute path. */
|
|
288
|
+
function writeAgentTeamPlan(projectPath, plan) {
|
|
289
|
+
const resolved = path.resolve(projectPath || ".");
|
|
290
|
+
const dir = path.join(resolved, ".sdtk", "agent-team");
|
|
291
|
+
fs.mkdirSync(dir, { recursive: true });
|
|
292
|
+
const outPath = path.join(dir, `AGENT_TEAM_PLAN_${plan.feature_key}.json`);
|
|
293
|
+
fs.writeFileSync(outPath, JSON.stringify(plan, null, 2), "utf8");
|
|
294
|
+
return outPath;
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
/** Write one Markdown task packet per role; returns absolute paths. */
|
|
298
|
+
function writeTaskPackets(projectPath, plan) {
|
|
299
|
+
const resolved = path.resolve(projectPath || ".");
|
|
300
|
+
const dir = path.join(resolved, ".sdtk", "agent-team", "tasks", plan.feature_key);
|
|
301
|
+
fs.mkdirSync(dir, { recursive: true });
|
|
302
|
+
const written = [];
|
|
303
|
+
for (const role of plan.roles) {
|
|
304
|
+
const outPath = path.join(dir, taskPacketFilename(role));
|
|
305
|
+
fs.writeFileSync(outPath, renderTaskPacket(plan, role), "utf8");
|
|
306
|
+
written.push(outPath);
|
|
307
|
+
}
|
|
308
|
+
return written;
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
/** Load a previously written plan (preview reads, does not re-derive). */
|
|
312
|
+
function loadAgentTeamPlan(projectPath, featureKey) {
|
|
313
|
+
const resolved = path.resolve(projectPath || ".");
|
|
314
|
+
const planPath = path.join(resolved, ".sdtk", "agent-team", `AGENT_TEAM_PLAN_${featureKey}.json`);
|
|
315
|
+
let plan;
|
|
316
|
+
try {
|
|
317
|
+
plan = JSON.parse(fs.readFileSync(planPath, "utf8"));
|
|
318
|
+
} catch (_err) {
|
|
319
|
+
throw new ValidationError(
|
|
320
|
+
`NEEDS_AGENT_TEAM_PLAN: no agent-team plan at ${planPath}. Run \`sdtk-code agent-team plan --feature-key ${featureKey}\` first.`
|
|
321
|
+
);
|
|
322
|
+
}
|
|
323
|
+
return plan;
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
// Defensive coercion: never assume a loaded/corrupt plan has array fields.
|
|
327
|
+
function asArray(value) {
|
|
328
|
+
return Array.isArray(value) ? value : [];
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
function roleName(role) {
|
|
332
|
+
return role && typeof role.role === "string" && role.role.length > 0 ? role.role : "(unnamed)";
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
/**
|
|
336
|
+
* Structural preview projection of a loaded plan. Phase A does NOT gate on
|
|
337
|
+
* readiness (decision B3) — readiness is attached as informational metadata.
|
|
338
|
+
*
|
|
339
|
+
* This runs against an arbitrary on-disk plan JSON that may be corrupt or
|
|
340
|
+
* hand-edited, so every field access is defensive: a malformed plan must yield
|
|
341
|
+
* a FAIL verdict, never a PASS and never a thrown TypeError.
|
|
342
|
+
*/
|
|
343
|
+
function projectAgentTeamPreview(plan, readiness) {
|
|
344
|
+
const blockers = [];
|
|
345
|
+
if (!plan || typeof plan !== "object") {
|
|
346
|
+
blockers.push("Invalid agent-team plan — not an object.");
|
|
347
|
+
}
|
|
348
|
+
if (!plan || plan.schema_version !== PLAN_SCHEMA) {
|
|
349
|
+
blockers.push("Invalid agent-team plan — expected sdtk.agent-team-plan.v1.");
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
if (plan && plan.roles !== undefined && !Array.isArray(plan.roles)) {
|
|
353
|
+
blockers.push("Invalid agent-team plan — roles is present but not an array.");
|
|
354
|
+
} else if (!plan || !Array.isArray(plan.roles)) {
|
|
355
|
+
blockers.push("Invalid agent-team plan — roles is missing.");
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
// Only object entries are previewable; drop non-object junk defensively.
|
|
359
|
+
const roles = asArray(plan && plan.roles).filter((role) => role && typeof role === "object");
|
|
360
|
+
|
|
361
|
+
// Per-role structural validation — each scope/evidence field must be an array.
|
|
362
|
+
for (const role of roles) {
|
|
363
|
+
const name = roleName(role);
|
|
364
|
+
if (!Array.isArray(role.allowed_file_scopes)) {
|
|
365
|
+
blockers.push(`Role "${name}" allowed_file_scopes is missing or not an array.`);
|
|
366
|
+
}
|
|
367
|
+
if (!Array.isArray(role.blocked_file_scopes)) {
|
|
368
|
+
blockers.push(`Role "${name}" blocked_file_scopes is missing or not an array.`);
|
|
369
|
+
}
|
|
370
|
+
if (!Array.isArray(role.evidence_required) || role.evidence_required.length === 0) {
|
|
371
|
+
blockers.push(`Role "${name}" has no evidence_required.`);
|
|
372
|
+
}
|
|
373
|
+
if (!Array.isArray(role.verification_commands)) {
|
|
374
|
+
blockers.push(`Role "${name}" verification_commands is missing or not an array.`);
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
// Writer invariants: exactly one writer, with a bounded scope and verification.
|
|
379
|
+
const writers = roles.filter((role) => role.can_write === true);
|
|
380
|
+
if (writers.length !== 1) {
|
|
381
|
+
blockers.push(`Expected exactly one writer role, found ${writers.length}.`);
|
|
382
|
+
} else {
|
|
383
|
+
const writer = writers[0];
|
|
384
|
+
if (asArray(writer.allowed_file_scopes).length === 0) {
|
|
385
|
+
blockers.push(`Writer role "${roleName(writer)}" has empty allowed_file_scopes (unbounded writer).`);
|
|
386
|
+
}
|
|
387
|
+
if (asArray(writer.verification_commands).length === 0) {
|
|
388
|
+
blockers.push(`Writer role "${roleName(writer)}" has no verification_commands.`);
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
const expectedStops = stopConditionIds();
|
|
393
|
+
const planStops = asArray(plan && plan.stop_conditions);
|
|
394
|
+
const stopsMatch = planStops.length === expectedStops.length
|
|
395
|
+
&& expectedStops.every((id) => planStops.includes(id));
|
|
396
|
+
if (!stopsMatch) {
|
|
397
|
+
blockers.push("Stop conditions do not match the canonical 8 sleep-plan IDs.");
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
const verdict = blockers.length === 0
|
|
401
|
+
? "PASS_AGENT_TEAM_PREVIEW_READY"
|
|
402
|
+
: "FAIL_AGENT_TEAM_PREVIEW_NOT_READY";
|
|
403
|
+
|
|
404
|
+
return {
|
|
405
|
+
verdict,
|
|
406
|
+
feature_key: plan && plan.feature_key,
|
|
407
|
+
run_id: plan && plan.run_id,
|
|
408
|
+
blockers,
|
|
409
|
+
roles,
|
|
410
|
+
readiness: readiness || null,
|
|
411
|
+
generated_at: new Date().toISOString(),
|
|
412
|
+
};
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
// Renders even a FAIL/corrupt preview without throwing — every field access is
|
|
416
|
+
// coerced so a malformed plan still produces a readable fail-closed report.
|
|
417
|
+
function renderPreviewMarkdown(plan, preview) {
|
|
418
|
+
const safePlan = plan && typeof plan === "object" ? plan : {};
|
|
419
|
+
const source = safePlan.source && typeof safePlan.source === "object" ? safePlan.source : {};
|
|
420
|
+
const floor = safePlan.readiness_floor && typeof safePlan.readiness_floor === "object"
|
|
421
|
+
? safePlan.readiness_floor
|
|
422
|
+
: {};
|
|
423
|
+
|
|
424
|
+
const roleRows = asArray(preview && preview.roles).map((role) => {
|
|
425
|
+
const mode = role.can_write ? "write" : "read-only";
|
|
426
|
+
const allowedN = asArray(role.allowed_file_scopes).length;
|
|
427
|
+
const blockedN = asArray(role.blocked_file_scopes).length;
|
|
428
|
+
const evidence = asArray(role.evidence_required).join(", ");
|
|
429
|
+
return `| ${roleName(role)} | ${role.runtime || "?"} | ${mode} | ${allowedN} allowed / ${blockedN} blocked | ${evidence} |`;
|
|
430
|
+
});
|
|
431
|
+
|
|
432
|
+
const readinessLine = preview && preview.readiness
|
|
433
|
+
? `- Current readiness (informational, not a gate in Phase A): ${preview.readiness.score}/${preview.readiness.max} — ${preview.readiness.verdict}`
|
|
434
|
+
: "- Current readiness: unavailable";
|
|
435
|
+
|
|
436
|
+
const floorScore = floor.score != null ? floor.score : "?";
|
|
437
|
+
const floorMax = floor.max != null ? floor.max : "?";
|
|
438
|
+
const floorSource = floor.source || "ship-readiness.computeReadiness";
|
|
439
|
+
|
|
440
|
+
return [
|
|
441
|
+
`# Agent Team Preview — ${(preview && preview.feature_key) || safePlan.feature_key || "(unknown)"}`,
|
|
442
|
+
"",
|
|
443
|
+
BOUNDARY_LINE,
|
|
444
|
+
"",
|
|
445
|
+
"## Verdict",
|
|
446
|
+
"",
|
|
447
|
+
`- Verdict: ${preview && preview.verdict ? preview.verdict : "FAIL_AGENT_TEAM_PREVIEW_NOT_READY"}`,
|
|
448
|
+
`- Run id: ${(preview && preview.run_id) || safePlan.run_id || "(none)"}`,
|
|
449
|
+
"- Blockers:",
|
|
450
|
+
bulletList(preview && preview.blockers, "none"),
|
|
451
|
+
"",
|
|
452
|
+
"## Inputs",
|
|
453
|
+
"",
|
|
454
|
+
`- CODE_HANDOFF: ${source.code_handoff_path || "(unknown)"}`,
|
|
455
|
+
`- Sleep plan: ${source.sleep_plan_path || "(unknown)"}`,
|
|
456
|
+
"",
|
|
457
|
+
"## Roles",
|
|
458
|
+
"",
|
|
459
|
+
"| Role | Runtime | Mode | Scope | Evidence |",
|
|
460
|
+
"| --- | --- | --- | --- | --- |",
|
|
461
|
+
...(roleRows.length > 0 ? roleRows : ["| (none) | | | | |"]),
|
|
462
|
+
"",
|
|
463
|
+
"## Readiness floor",
|
|
464
|
+
"",
|
|
465
|
+
`- Floor: ${floorScore}/${floorMax} via ${floorSource}`,
|
|
466
|
+
readinessLine,
|
|
467
|
+
"",
|
|
468
|
+
"## Stop conditions",
|
|
469
|
+
"",
|
|
470
|
+
bulletList(asArray(safePlan.stop_conditions), "none declared"),
|
|
471
|
+
"",
|
|
472
|
+
"## Boundary",
|
|
473
|
+
"",
|
|
474
|
+
BOUNDARY_LINE,
|
|
475
|
+
"",
|
|
476
|
+
].join("\n");
|
|
477
|
+
}
|
|
478
|
+
|
|
479
|
+
function writeAgentTeamPreview(projectPath, featureKey, markdown) {
|
|
480
|
+
const resolved = path.resolve(projectPath || ".");
|
|
481
|
+
const outDir = path.join(resolved, "docs", "trust");
|
|
482
|
+
fs.mkdirSync(outDir, { recursive: true });
|
|
483
|
+
const outPath = path.join(outDir, `AGENT_TEAM_PREVIEW_${featureKey}.md`);
|
|
484
|
+
fs.writeFileSync(outPath, markdown, "utf8");
|
|
485
|
+
return outPath;
|
|
486
|
+
}
|
|
487
|
+
|
|
488
|
+
/** Informational readiness signal for the preview (never gates in Phase A). */
|
|
489
|
+
function gatherPreviewReadiness(projectPath, featureKey) {
|
|
490
|
+
try {
|
|
491
|
+
return computeReadiness(gatherSignals(projectPath, featureKey));
|
|
492
|
+
} catch (_err) {
|
|
493
|
+
return null;
|
|
494
|
+
}
|
|
495
|
+
}
|
|
496
|
+
|
|
497
|
+
module.exports = {
|
|
498
|
+
PLAN_SCHEMA,
|
|
499
|
+
SECRET_BLOCKED_SCOPES,
|
|
500
|
+
BOUNDARY_LINE,
|
|
501
|
+
stopConditionIds,
|
|
502
|
+
deriveRoles,
|
|
503
|
+
buildAgentTeamPlan,
|
|
504
|
+
assembleAgentTeamPlan,
|
|
505
|
+
writeAgentTeamPlan,
|
|
506
|
+
renderTaskPacket,
|
|
507
|
+
taskPacketFilename,
|
|
508
|
+
writeTaskPackets,
|
|
509
|
+
loadAgentTeamPlan,
|
|
510
|
+
projectAgentTeamPreview,
|
|
511
|
+
renderPreviewMarkdown,
|
|
512
|
+
writeAgentTeamPreview,
|
|
513
|
+
gatherPreviewReadiness,
|
|
514
|
+
};
|