sdtk-code-kit 0.3.0 → 0.3.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 (56) hide show
  1. package/README.md +204 -204
  2. package/assets/manifest/toolkit-bundle.manifest.json +147 -147
  3. package/assets/manifest/toolkit-bundle.sha256.txt +5 -5
  4. package/bin/sdtk-code.js +14 -14
  5. package/package.json +44 -44
  6. package/src/commands/agent-team.js +425 -174
  7. package/src/commands/build.js +53 -53
  8. package/src/commands/doctor.js +51 -51
  9. package/src/commands/generate.js +11 -11
  10. package/src/commands/guardrails.js +215 -215
  11. package/src/commands/help.js +264 -257
  12. package/src/commands/init.js +98 -98
  13. package/src/commands/plan.js +53 -53
  14. package/src/commands/readiness.js +77 -77
  15. package/src/commands/resume.js +61 -61
  16. package/src/commands/runtime.js +224 -224
  17. package/src/commands/ship.js +59 -59
  18. package/src/commands/sleep.js +132 -132
  19. package/src/commands/start.js +89 -89
  20. package/src/commands/status.js +77 -77
  21. package/src/commands/update.js +11 -11
  22. package/src/commands/verify.js +71 -71
  23. package/src/index.js +112 -112
  24. package/src/lib/agent-team-author.js +521 -0
  25. package/src/lib/agent-team-evidence.js +302 -0
  26. package/src/lib/agent-team-execute.js +892 -0
  27. package/src/lib/agent-team-export.js +411 -409
  28. package/src/lib/agent-team-plan.js +514 -514
  29. package/src/lib/agent-team-readiness.js +161 -0
  30. package/src/lib/agent-team-report.js +165 -0
  31. package/src/lib/args.js +107 -107
  32. package/src/lib/errors.js +41 -41
  33. package/src/lib/model-provider.js +219 -0
  34. package/src/lib/powershell.js +65 -65
  35. package/src/lib/project-target-guard.js +53 -53
  36. package/src/lib/scope.js +72 -72
  37. package/src/lib/ship-readiness.js +281 -281
  38. package/src/lib/sleep-plan.js +175 -175
  39. package/src/lib/sleep-report.js +229 -229
  40. package/src/lib/toolkit-payload.js +72 -72
  41. package/src/lib/trust-events.js +252 -185
  42. package/src/lib/trust-file-scope.js +154 -154
  43. package/src/lib/trust-policy.js +212 -212
  44. package/src/lib/trust-redact.js +58 -35
  45. package/src/lib/trust-trace.js +179 -179
  46. package/src/lib/update.js +344 -344
  47. package/src/lib/workflow-artifact.js +503 -503
  48. package/src/lib/workflow-build.js +127 -127
  49. package/src/lib/workflow-doctor.js +83 -83
  50. package/src/lib/workflow-handoff.js +1029 -1029
  51. package/src/lib/workflow-intake.js +101 -101
  52. package/src/lib/workflow-plan.js +193 -193
  53. package/src/lib/workflow-review-packet.js +650 -650
  54. package/src/lib/workflow-ship.js +213 -213
  55. package/src/lib/workflow-state.js +438 -438
  56. package/src/lib/workflow-verify.js +393 -393
@@ -0,0 +1,302 @@
1
+ "use strict";
2
+
3
+ const fs = require("fs");
4
+ const path = require("path");
5
+ const { ValidationError } = require("./errors");
6
+ const { safeSegment } = require("./agent-team-export");
7
+ const { loadAgentTeamPlan } = require("./agent-team-plan");
8
+ const { STOP_CONDITIONS } = require("./sleep-plan");
9
+
10
+ const EVIDENCE_SCHEMA_VERSION = "sdtk.agent-team-evidence.v1";
11
+ const VALID_ROLES = Object.freeze(["pm", "dev", "qa"]);
12
+ const VALID_STOP_CONDITIONS = new Set(
13
+ STOP_CONDITIONS.map((condition) => condition && condition.id).filter(Boolean)
14
+ );
15
+
16
+ function asArray(value) {
17
+ return Array.isArray(value) ? value : [];
18
+ }
19
+
20
+ function roleName(value) {
21
+ return String(value || "").toLowerCase();
22
+ }
23
+
24
+ function evidenceBaseDir(projectPath) {
25
+ return path.resolve(projectPath || ".", ".sdtk", "agent-team", "evidence");
26
+ }
27
+
28
+ function assertPathUnderBase(resolvedPath, expectedBase) {
29
+ const resolved = path.resolve(resolvedPath);
30
+ const base = path.resolve(expectedBase);
31
+ if (resolved !== base && !resolved.startsWith(base + path.sep)) {
32
+ throw new ValidationError("FAIL_UNSAFE_EXPORT_PATH: resolved path outside expected base");
33
+ }
34
+ return resolved;
35
+ }
36
+
37
+ function resolveEvidencePath(projectPath, featureKey, role) {
38
+ const safeFeatureKey = safeSegment(featureKey, "feature_key");
39
+ const safeRole = safeSegment(roleName(role), "role");
40
+ const base = evidenceBaseDir(projectPath);
41
+ const dir = assertPathUnderBase(path.join(base, safeFeatureKey), base);
42
+ const outputPath = assertPathUnderBase(
43
+ path.join(dir, `${safeRole.toUpperCase()}.evidence.json`),
44
+ base
45
+ );
46
+ return outputPath;
47
+ }
48
+
49
+ function requireExpectedBase(expectedBase) {
50
+ if (typeof expectedBase !== "string" || expectedBase.length === 0) {
51
+ throw new TypeError("expectedBase is required for agent-team atomic writes");
52
+ }
53
+ return expectedBase;
54
+ }
55
+
56
+ function tmpPathFor(resolved) {
57
+ const nonce = `${process.pid}.${Date.now()}.${Math.random().toString(36).slice(2)}`;
58
+ return `${resolved}.${nonce}.tmp`;
59
+ }
60
+
61
+ function lockPid(lockPath) {
62
+ try {
63
+ const raw = fs.readFileSync(lockPath, "utf8").trim();
64
+ const pid = Number(raw);
65
+ return Number.isInteger(pid) && pid > 0 ? pid : null;
66
+ } catch (_error) {
67
+ return null;
68
+ }
69
+ }
70
+
71
+ function processIsAlive(pid) {
72
+ if (!pid) {
73
+ return false;
74
+ }
75
+ try {
76
+ process.kill(pid, 0);
77
+ return true;
78
+ } catch (error) {
79
+ return !(error && error.code === "ESRCH");
80
+ }
81
+ }
82
+
83
+ function removeStaleLock(lockPath) {
84
+ try {
85
+ fs.unlinkSync(lockPath);
86
+ return true;
87
+ } catch (error) {
88
+ if (error && error.code === "ENOENT") {
89
+ return true;
90
+ }
91
+ return false;
92
+ }
93
+ }
94
+
95
+ function reserveExclusive(lockPath, lockExistsMessage) {
96
+ for (let attempt = 0; attempt < 2; attempt++) {
97
+ try {
98
+ const fd = fs.openSync(lockPath, "wx");
99
+ try {
100
+ fs.writeSync(fd, `${process.pid}
101
+ `);
102
+ } finally {
103
+ fs.closeSync(fd);
104
+ }
105
+ return;
106
+ } catch (error) {
107
+ if (!error || error.code !== "EEXIST") {
108
+ throw error;
109
+ }
110
+ if (!processIsAlive(lockPid(lockPath)) && removeStaleLock(lockPath)) {
111
+ continue;
112
+ }
113
+ throw new ValidationError(
114
+ lockExistsMessage || `FAIL_EVIDENCE_ALREADY_SUBMITTED: evidence submission already in progress at ${lockPath}`
115
+ );
116
+ }
117
+ }
118
+ }
119
+
120
+ function atomicWrite(outputPath, content, expectedBase, opts) {
121
+ const options = opts || {};
122
+ const resolved = assertPathUnderBase(outputPath, requireExpectedBase(expectedBase));
123
+ fs.mkdirSync(path.dirname(resolved), { recursive: true });
124
+ const tmpPath = tmpPathFor(resolved);
125
+ const lockPath = `${resolved}.lock`;
126
+ let locked = false;
127
+ let succeeded = false;
128
+
129
+ try {
130
+ if (options.exclusive === true) {
131
+ reserveExclusive(lockPath, options.lockExistsMessage);
132
+ locked = true;
133
+ if (fs.existsSync(resolved)) {
134
+ throw new ValidationError(options.alreadyExistsMessage || `FAIL_EVIDENCE_ALREADY_SUBMITTED: evidence already exists at ${resolved}`);
135
+ }
136
+ }
137
+ fs.writeFileSync(tmpPath, content, "utf8");
138
+ fs.renameSync(tmpPath, resolved);
139
+ succeeded = true;
140
+ return resolved;
141
+ } finally {
142
+ if (!succeeded && fs.existsSync(tmpPath)) {
143
+ fs.unlinkSync(tmpPath);
144
+ }
145
+ if (locked && fs.existsSync(lockPath)) {
146
+ fs.unlinkSync(lockPath);
147
+ }
148
+ }
149
+ }
150
+
151
+ function atomicWriteJson(outputPath, data, expectedBase, opts) {
152
+ return atomicWrite(outputPath, JSON.stringify(data, null, 2), expectedBase, opts);
153
+ }
154
+
155
+ function atomicWriteText(outputPath, text, expectedBase, opts) {
156
+ return atomicWrite(outputPath, String(text), expectedBase, opts);
157
+ }
158
+
159
+ function schemaError(message) {
160
+ throw new ValidationError(`FAIL_EVIDENCE_SCHEMA: ${message}`);
161
+ }
162
+
163
+ function requiredString(value, field) {
164
+ if (typeof value !== "string" || value.length === 0) {
165
+ schemaError(`${field} must not be empty`);
166
+ }
167
+ }
168
+
169
+ function requiredStringArray(value, field) {
170
+ if (!Array.isArray(value) || !value.every((entry) => typeof entry === "string")) {
171
+ schemaError(`${field} must be an array of strings`);
172
+ }
173
+ }
174
+
175
+ function findRoleSpec(plan, role) {
176
+ const normalized = roleName(role);
177
+ return asArray(plan && plan.roles).find((entry) => roleName(entry && entry.role) === normalized) || null;
178
+ }
179
+
180
+ function validateCommitRange(commitRange) {
181
+ if (commitRange === undefined || commitRange === null) {
182
+ return;
183
+ }
184
+ if (typeof commitRange !== "object" || Array.isArray(commitRange)) {
185
+ schemaError("commit_range must be an object when present");
186
+ }
187
+ requiredString(commitRange.before, "commit_range.before");
188
+ requiredString(commitRange.after, "commit_range.after");
189
+ }
190
+
191
+ function validateEvidenceFile(evidenceJson, plan) {
192
+ if (!evidenceJson || typeof evidenceJson !== "object" || Array.isArray(evidenceJson)) {
193
+ schemaError("evidence file must be a JSON object");
194
+ }
195
+ if (evidenceJson.schema_version !== EVIDENCE_SCHEMA_VERSION) {
196
+ schemaError(`schema_version must be ${EVIDENCE_SCHEMA_VERSION}`);
197
+ }
198
+
199
+ requiredString(evidenceJson.feature_key, "feature_key");
200
+ requiredString(evidenceJson.role, "role");
201
+ const featureKey = safeSegment(evidenceJson.feature_key, "feature_key");
202
+ const role = roleName(safeSegment(evidenceJson.role, "role"));
203
+ if (!VALID_ROLES.includes(role)) {
204
+ schemaError(`role \"${role}\" is not supported`);
205
+ }
206
+ if (!plan || typeof plan !== "object") {
207
+ throw new ValidationError("NEEDS_AGENT_TEAM_PLAN: agent-team plan is missing or invalid.");
208
+ }
209
+ if (plan.feature_key !== featureKey) {
210
+ schemaError(`feature_key \"${featureKey}\" does not match plan feature_key \"${plan.feature_key}\"`);
211
+ }
212
+
213
+ const roleSpec = findRoleSpec(plan, role);
214
+ if (!roleSpec) {
215
+ schemaError(`role \"${role}\" is not present in agent-team plan`);
216
+ }
217
+ if (typeof evidenceJson.run_id !== "string" || evidenceJson.run_id.length === 0) {
218
+ schemaError("run_id must not be empty");
219
+ }
220
+ if (evidenceJson.run_id !== plan.run_id) {
221
+ throw new ValidationError("FAIL_EVIDENCE_RUN_MISMATCH: evidence run_id does not match agent-team plan run_id.");
222
+ }
223
+
224
+ requiredString(evidenceJson.submitted_at, "submitted_at");
225
+ requiredString(evidenceJson.summary, "summary");
226
+ requiredStringArray(evidenceJson.commands_run, "commands_run");
227
+ requiredString(evidenceJson.verification_evidence, "verification_evidence");
228
+ requiredStringArray(evidenceJson.blockers, "blockers");
229
+
230
+ if (evidenceJson.stop_condition_hit !== null && evidenceJson.stop_condition_hit !== undefined) {
231
+ if (typeof evidenceJson.stop_condition_hit !== "string" || !VALID_STOP_CONDITIONS.has(evidenceJson.stop_condition_hit)) {
232
+ schemaError(`stop_condition_hit must be null or one of: ${Array.from(VALID_STOP_CONDITIONS).join(", ")}`);
233
+ }
234
+ }
235
+
236
+ if (!evidenceJson.evidence || typeof evidenceJson.evidence !== "object" || Array.isArray(evidenceJson.evidence)) {
237
+ schemaError("evidence must be an object");
238
+ }
239
+ const missing = asArray(roleSpec.evidence_required).filter((key) => !(key in evidenceJson.evidence));
240
+ if (missing.length > 0) {
241
+ throw new ValidationError(`FAIL_EVIDENCE_INCOMPLETE: missing required evidence key(s): ${missing.join(", ")}`);
242
+ }
243
+
244
+ validateCommitRange(evidenceJson.commit_range);
245
+ return { valid: true, roleSpec };
246
+ }
247
+
248
+ function submitEvidence(projectPath, featureKey, role, evidenceFilePath) {
249
+ const safeFeatureKey = safeSegment(featureKey, "feature_key");
250
+ const safeRole = roleName(safeSegment(role, "role"));
251
+ const plan = loadAgentTeamPlan(projectPath, safeFeatureKey);
252
+ const roleSpec = findRoleSpec(plan, safeRole);
253
+ if (!roleSpec) {
254
+ schemaError(`role \"${safeRole}\" is not present in agent-team plan`);
255
+ }
256
+
257
+ let parsed;
258
+ try {
259
+ parsed = JSON.parse(fs.readFileSync(evidenceFilePath, "utf8"));
260
+ } catch (_error) {
261
+ schemaError(`evidence file could not be read or parsed: ${evidenceFilePath}`);
262
+ }
263
+
264
+ requiredString(parsed.feature_key, "feature_key");
265
+ requiredString(parsed.role, "role");
266
+ if (parsed.feature_key !== safeFeatureKey) {
267
+ schemaError(`feature_key \"${parsed.feature_key}\" does not match --feature-key \"${safeFeatureKey}\"`);
268
+ }
269
+ if (roleName(parsed.role) !== safeRole) {
270
+ schemaError(`role \"${parsed.role}\" does not match --role \"${safeRole}\"`);
271
+ }
272
+ validateEvidenceFile(parsed, plan);
273
+
274
+ const outputPath = resolveEvidencePath(projectPath, safeFeatureKey, safeRole);
275
+ atomicWriteJson(outputPath, parsed, evidenceBaseDir(projectPath), {
276
+ exclusive: true,
277
+ alreadyExistsMessage: `FAIL_EVIDENCE_ALREADY_SUBMITTED: evidence already exists at ${outputPath}`,
278
+ lockExistsMessage: `FAIL_EVIDENCE_ALREADY_SUBMITTED: evidence submission already in progress at ${outputPath}.lock`,
279
+ });
280
+
281
+ return {
282
+ outputPath,
283
+ run_id: plan.run_id,
284
+ role: safeRole,
285
+ stop_condition_hit: parsed.stop_condition_hit || null,
286
+ };
287
+ }
288
+
289
+ module.exports = {
290
+ EVIDENCE_SCHEMA_VERSION,
291
+ VALID_ROLES,
292
+ VALID_STOP_CONDITIONS,
293
+ asArray,
294
+ roleName,
295
+ evidenceBaseDir,
296
+ assertPathUnderBase,
297
+ resolveEvidencePath,
298
+ atomicWriteJson,
299
+ atomicWriteText,
300
+ validateEvidenceFile,
301
+ submitEvidence,
302
+ };