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.
Files changed (58) 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 +479 -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 +266 -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 +552 -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/agent-team-sleep-execute.js +524 -0
  32. package/src/lib/agent-team-sleep-session.js +278 -0
  33. package/src/lib/args.js +107 -107
  34. package/src/lib/errors.js +41 -41
  35. package/src/lib/model-provider.js +219 -0
  36. package/src/lib/powershell.js +65 -65
  37. package/src/lib/project-target-guard.js +53 -53
  38. package/src/lib/scope.js +72 -72
  39. package/src/lib/ship-readiness.js +281 -281
  40. package/src/lib/sleep-plan.js +175 -175
  41. package/src/lib/sleep-report.js +229 -229
  42. package/src/lib/toolkit-payload.js +72 -72
  43. package/src/lib/trust-events.js +264 -185
  44. package/src/lib/trust-file-scope.js +154 -154
  45. package/src/lib/trust-policy.js +212 -212
  46. package/src/lib/trust-redact.js +58 -35
  47. package/src/lib/trust-trace.js +179 -179
  48. package/src/lib/update.js +344 -344
  49. package/src/lib/workflow-artifact.js +503 -503
  50. package/src/lib/workflow-build.js +127 -127
  51. package/src/lib/workflow-doctor.js +83 -83
  52. package/src/lib/workflow-handoff.js +1029 -1029
  53. package/src/lib/workflow-intake.js +101 -101
  54. package/src/lib/workflow-plan.js +193 -193
  55. package/src/lib/workflow-review-packet.js +650 -650
  56. package/src/lib/workflow-ship.js +213 -213
  57. package/src/lib/workflow-state.js +438 -438
  58. package/src/lib/workflow-verify.js +393 -393
@@ -1,185 +1,264 @@
1
- "use strict";
2
-
3
- const crypto = require("crypto");
4
- const fs = require("fs");
5
- const path = require("path");
6
- const { redact } = require("./trust-redact");
7
-
8
- const locks = new Map();
9
- const MAX_COMMAND_LENGTH = 2000;
10
-
11
- function generateId(prefix) {
12
- const ts = Date.now().toString(36);
13
- const rand = crypto.randomUUID().replace(/-/g, "").slice(0, 12);
14
- return `${prefix}_${ts}_${rand}`;
15
- }
16
-
17
- function fingerprintId(prefix, content) {
18
- const hash = crypto.createHash("sha256").update(String(content)).digest("hex");
19
- return `${prefix}_${hash.slice(0, 16)}`;
20
- }
21
-
22
- function eventsDir(projectPath) {
23
- return path.join(path.resolve(projectPath || "."), ".sdtk", "trust", "events");
24
- }
25
-
26
- function eventsFile(projectPath, date) {
27
- const day = typeof date === "string" ? date : new Date(date || Date.now()).toISOString().slice(0, 10);
28
- return path.join(eventsDir(projectPath), `${day}.jsonl`);
29
- }
30
-
31
- function nullableString(value) {
32
- return typeof value === "string" && value.length > 0 ? value : null;
33
- }
34
-
35
- function normalizeStatus(value) {
36
- return typeof value === "string" && value.length > 0 ? value : "info";
37
- }
38
-
39
- function inferEventType(raw) {
40
- if (!raw || typeof raw !== "object") {
41
- return null;
42
- }
43
- if (typeof raw.event_type === "string" && raw.event_type.length > 0) {
44
- return raw.event_type;
45
- }
46
- const hookType = typeof raw.hook_type === "string" ? raw.hook_type : "";
47
- const toolName = typeof raw.tool_name === "string" ? raw.tool_name : "";
48
- if (hookType === "post_tool_failure") {
49
- return "command_failure";
50
- }
51
- if (hookType === "post_tool" || hookType === "task_completed") {
52
- return "post_command";
53
- }
54
- if (hookType === "pre_tool" || /(?:bash|shell|exec|run)/i.test(toolName)) {
55
- return "pre_command";
56
- }
57
- return null;
58
- }
59
-
60
- function normalizeFiles(value) {
61
- if (!Array.isArray(value)) {
62
- return [];
63
- }
64
- return value.filter((entry) => typeof entry === "string").map((entry) => redact(entry));
65
- }
66
-
67
- function normalizeCommand(value) {
68
- if (typeof value !== "string") {
69
- return { command: null, truncated: false };
70
- }
71
- const redacted = redact(value);
72
- if (redacted.length > MAX_COMMAND_LENGTH) {
73
- return { command: redacted.slice(0, MAX_COMMAND_LENGTH), truncated: true };
74
- }
75
- return { command: redacted, truncated: false };
76
- }
77
-
78
- function buildEvent(raw) {
79
- const source = raw && typeof raw === "object" ? raw : {};
80
- const timestamp = typeof source.timestamp === "string" && source.timestamp.length > 0
81
- ? source.timestamp
82
- : new Date().toISOString();
83
- const { command, truncated } = normalizeCommand(source.command);
84
- const sessionId = nullableString(source.session_id);
85
- const toolkit = nullableString(source.toolkit);
86
- const eventType = inferEventType(source);
87
- // Agent-team traceability (BK-260). Redacted like any other string field and
88
- // folded into the dedup identity so distinct runs/roles emitting the same
89
- // command are not collapsed into a single event. Existing callers omit these,
90
- // so the fields default to null and keep dedup deterministic.
91
- const runId = source.run_id != null ? redact(String(source.run_id)) : null;
92
- const agentRole = source.agent_role != null ? redact(String(source.agent_role)) : null;
93
- const taskId = source.task_id != null ? redact(String(source.task_id)) : null;
94
- const canonical = [
95
- sessionId || "",
96
- toolkit || "",
97
- eventType || "",
98
- command || "",
99
- runId || "",
100
- agentRole || "",
101
- taskId || "",
102
- timestamp,
103
- ].join("|");
104
-
105
- return {
106
- event_id: fingerprintId("evt", canonical),
107
- session_id: sessionId,
108
- toolkit,
109
- phase: nullableString(source.phase),
110
- event_type: eventType,
111
- command,
112
- run_id: runId,
113
- agent_role: agentRole,
114
- task_id: taskId,
115
- status: normalizeStatus(source.status),
116
- cwd: typeof source.cwd === "string" ? redact(source.cwd) : null,
117
- files: normalizeFiles(source.files),
118
- timestamp,
119
- redaction: "applied",
120
- truncated,
121
- };
122
- }
123
-
124
- function readExistingEventIds(file) {
125
- if (!fs.existsSync(file)) {
126
- return new Set();
127
- }
128
- const ids = new Set();
129
- const content = fs.readFileSync(file, "utf8");
130
- for (const line of content.split(/\r?\n/)) {
131
- if (!line.trim()) {
132
- continue;
133
- }
134
- try {
135
- const event = JSON.parse(line);
136
- if (event && typeof event.event_id === "string") {
137
- ids.add(event.event_id);
138
- }
139
- } catch (_error) {
140
- // Append-only logs should remain writable even if one old line is malformed.
141
- }
142
- }
143
- return ids;
144
- }
145
-
146
- /**
147
- * Append a redacted trust event.
148
- *
149
- * The caller must provide a stable timestamp when retry-idempotency is desired.
150
- * If timestamp is omitted, each call receives the current ISO timestamp and is
151
- * treated as a distinct observation.
152
- */
153
- function appendEvent(projectPath, raw) {
154
- const event = buildEvent(raw);
155
- const file = eventsFile(projectPath, event.timestamp.slice(0, 10));
156
- fs.mkdirSync(path.dirname(file), { recursive: true });
157
-
158
- const existingIds = readExistingEventIds(file);
159
- if (existingIds.has(event.event_id)) {
160
- return { event, written: false };
161
- }
162
-
163
- fs.appendFileSync(file, `${JSON.stringify(event)}\n`, "utf8");
164
- return { event, written: true };
165
- }
166
-
167
- function withKeyedLock(key, fn) {
168
- const prev = locks.get(key) || Promise.resolve();
169
- const next = prev.then(fn, fn);
170
- const cleanup = next.then(
171
- () => {},
172
- () => {},
173
- );
174
- locks.set(key, cleanup);
175
- cleanup.then(() => {
176
- if (locks.get(key) === cleanup) {
177
- locks.delete(key);
178
- }
179
- });
180
- return next;
181
- }
182
-
183
- void withKeyedLock;
184
-
185
- module.exports = { generateId, fingerprintId, buildEvent, appendEvent, eventsDir, eventsFile };
1
+ "use strict";
2
+
3
+ const crypto = require("crypto");
4
+ const fs = require("fs");
5
+ const path = require("path");
6
+ const { redact } = require("./trust-redact");
7
+
8
+ const locks = new Map();
9
+ const MAX_COMMAND_LENGTH = 2000;
10
+
11
+ function generateId(prefix) {
12
+ const ts = Date.now().toString(36);
13
+ const rand = crypto.randomUUID().replace(/-/g, "").slice(0, 12);
14
+ return `${prefix}_${ts}_${rand}`;
15
+ }
16
+
17
+ function fingerprintId(prefix, content) {
18
+ const hash = crypto.createHash("sha256").update(String(content)).digest("hex");
19
+ return `${prefix}_${hash.slice(0, 16)}`;
20
+ }
21
+
22
+ function eventsDir(projectPath) {
23
+ return path.join(path.resolve(projectPath || "."), ".sdtk", "trust", "events");
24
+ }
25
+
26
+ function eventsFile(projectPath, date) {
27
+ const day = typeof date === "string" ? date : new Date(date || Date.now()).toISOString().slice(0, 10);
28
+ return path.join(eventsDir(projectPath), `${day}.jsonl`);
29
+ }
30
+
31
+ function nullableString(value) {
32
+ return typeof value === "string" && value.length > 0 ? value : null;
33
+ }
34
+
35
+ function normalizeStatus(value) {
36
+ return typeof value === "string" && value.length > 0 ? value : "info";
37
+ }
38
+
39
+ function inferEventType(raw) {
40
+ if (!raw || typeof raw !== "object") {
41
+ return null;
42
+ }
43
+ if (typeof raw.event_type === "string" && raw.event_type.length > 0) {
44
+ return raw.event_type;
45
+ }
46
+ const hookType = typeof raw.hook_type === "string" ? raw.hook_type : "";
47
+ const toolName = typeof raw.tool_name === "string" ? raw.tool_name : "";
48
+ if (hookType === "post_tool_failure") {
49
+ return "command_failure";
50
+ }
51
+ if (hookType === "post_tool" || hookType === "task_completed") {
52
+ return "post_command";
53
+ }
54
+ if (hookType === "pre_tool" || /(?:bash|shell|exec|run)/i.test(toolName)) {
55
+ return "pre_command";
56
+ }
57
+ return null;
58
+ }
59
+
60
+ function normalizeFiles(value) {
61
+ if (!Array.isArray(value)) {
62
+ return [];
63
+ }
64
+ return value.filter((entry) => typeof entry === "string").map((entry) => redact(entry));
65
+ }
66
+
67
+
68
+ function normalizeContextManifest(value) {
69
+ if (!Array.isArray(value)) {
70
+ return [];
71
+ }
72
+ return value.map((entry) => {
73
+ const source = entry && typeof entry === "object" ? entry : {};
74
+ return {
75
+ path: source.path != null ? redact(String(source.path)) : null,
76
+ snapshot_sha256: typeof source.snapshot_sha256 === "string" ? source.snapshot_sha256 : null,
77
+ };
78
+ }).filter((entry) => entry.path && entry.snapshot_sha256);
79
+ }
80
+
81
+ function normalizeTokensUsed(value) {
82
+ if (!value || typeof value !== "object") {
83
+ return null;
84
+ }
85
+ return {
86
+ input_tokens: typeof value.input_tokens === "number" ? value.input_tokens : null,
87
+ output_tokens: typeof value.output_tokens === "number" ? value.output_tokens : null,
88
+ };
89
+ }
90
+
91
+ function normalizeCommand(value) {
92
+ if (typeof value !== "string") {
93
+ return { command: null, truncated: false };
94
+ }
95
+ const redacted = redact(value);
96
+ if (redacted.length > MAX_COMMAND_LENGTH) {
97
+ return { command: redacted.slice(0, MAX_COMMAND_LENGTH), truncated: true };
98
+ }
99
+ return { command: redacted, truncated: false };
100
+ }
101
+
102
+ function buildEvent(raw) {
103
+ const source = raw && typeof raw === "object" ? raw : {};
104
+ const timestamp = typeof source.timestamp === "string" && source.timestamp.length > 0
105
+ ? source.timestamp
106
+ : new Date().toISOString();
107
+ const { command, truncated } = normalizeCommand(source.command);
108
+ const sessionId = nullableString(source.session_id);
109
+ const toolkit = nullableString(source.toolkit);
110
+ const eventType = inferEventType(source);
111
+ // Agent-team traceability (BK-260). Redacted like any other string field and
112
+ // folded into the dedup identity so distinct runs/roles emitting the same
113
+ // command are not collapsed into a single event. Existing callers omit these,
114
+ // so the fields default to null and keep dedup deterministic.
115
+ const runId = source.run_id != null ? redact(String(source.run_id)) : null;
116
+ const agentRole = source.agent_role != null ? redact(String(source.agent_role)) : null;
117
+ const taskId = source.task_id != null ? redact(String(source.task_id)) : null;
118
+ const featureKey = source.feature_key != null ? redact(String(source.feature_key)) : null;
119
+ const stopConditionHit = source.stop_condition_hit != null
120
+ ? String(source.stop_condition_hit) : null;
121
+ const evidenceQualityOk = typeof source.evidence_quality_ok === "boolean"
122
+ ? source.evidence_quality_ok : null;
123
+ const overallVerdict = source.overall_verdict != null
124
+ ? String(source.overall_verdict) : null;
125
+ const rolesNotReadyCount = typeof source.roles_not_ready_count === "number"
126
+ ? source.roles_not_ready_count : null;
127
+ const reportPath = source.report_path != null ? redact(String(source.report_path)) : null;
128
+ const worktreePath = source.worktree_path != null ? redact(String(source.worktree_path)) : null;
129
+ const actionsExecuted = typeof source.actions_executed === "number"
130
+ ? source.actions_executed : null;
131
+ const executionMode = source.execution_mode != null
132
+ ? String(source.execution_mode) : null;
133
+ const workflowType = source.workflow_type != null
134
+ ? String(source.workflow_type) : null;
135
+ const iterationIndex = typeof source.iteration_index === "number"
136
+ ? source.iteration_index : null;
137
+ const readinessVerdict = source.readiness_verdict != null
138
+ ? String(source.readiness_verdict) : null;
139
+ const sessionCostUsd = typeof source.session_cost_usd === "number"
140
+ ? source.session_cost_usd : null;
141
+ const sessionOutputTokens = typeof source.session_output_tokens === "number"
142
+ ? source.session_output_tokens : null;
143
+ const modelProvider = source.model_provider != null ? redact(String(source.model_provider)) : null;
144
+ const modelId = source.model_id != null ? redact(String(source.model_id)) : null;
145
+ const promptSha256 = source.prompt_sha256 != null ? String(source.prompt_sha256) : null;
146
+ const contextManifest = normalizeContextManifest(source.context_manifest);
147
+ const producedPatchSha256 = source.produced_patch_sha256 != null
148
+ ? String(source.produced_patch_sha256) : null;
149
+ const modelCallCount = typeof source.model_call_count === "number"
150
+ ? source.model_call_count : null;
151
+ const tokensUsed = normalizeTokensUsed(source.tokens_used);
152
+ const canonical = [
153
+ sessionId || "",
154
+ toolkit || "",
155
+ eventType || "",
156
+ command || "",
157
+ runId || "",
158
+ agentRole || "",
159
+ taskId || "",
160
+ timestamp,
161
+ ].join("|");
162
+
163
+ return {
164
+ event_id: fingerprintId("evt", canonical),
165
+ session_id: sessionId,
166
+ toolkit,
167
+ phase: nullableString(source.phase),
168
+ event_type: eventType,
169
+ command,
170
+ run_id: runId,
171
+ agent_role: agentRole,
172
+ task_id: taskId,
173
+ feature_key: featureKey,
174
+ stop_condition_hit: stopConditionHit,
175
+ evidence_quality_ok: evidenceQualityOk,
176
+ overall_verdict: overallVerdict,
177
+ roles_not_ready_count: rolesNotReadyCount,
178
+ report_path: reportPath,
179
+ ...(Object.prototype.hasOwnProperty.call(source, "worktree_path") ? { worktree_path: worktreePath } : {}),
180
+ ...(Object.prototype.hasOwnProperty.call(source, "actions_executed") ? { actions_executed: actionsExecuted } : {}),
181
+ ...(Object.prototype.hasOwnProperty.call(source, "execution_mode") ? { execution_mode: executionMode } : {}),
182
+ ...(Object.prototype.hasOwnProperty.call(source, "workflow_type") ? { workflow_type: workflowType } : {}),
183
+ ...(Object.prototype.hasOwnProperty.call(source, "iteration_index") ? { iteration_index: iterationIndex } : {}),
184
+ ...(Object.prototype.hasOwnProperty.call(source, "readiness_verdict") ? { readiness_verdict: readinessVerdict } : {}),
185
+ ...(Object.prototype.hasOwnProperty.call(source, "session_cost_usd") ? { session_cost_usd: sessionCostUsd } : {}),
186
+ ...(Object.prototype.hasOwnProperty.call(source, "session_output_tokens") ? { session_output_tokens: sessionOutputTokens } : {}),
187
+ ...(Object.prototype.hasOwnProperty.call(source, "model_provider") ? { model_provider: modelProvider } : {}),
188
+ ...(Object.prototype.hasOwnProperty.call(source, "model_id") ? { model_id: modelId } : {}),
189
+ ...(Object.prototype.hasOwnProperty.call(source, "prompt_sha256") ? { prompt_sha256: promptSha256 } : {}),
190
+ ...(Object.prototype.hasOwnProperty.call(source, "context_manifest") ? { context_manifest: contextManifest } : {}),
191
+ ...(Object.prototype.hasOwnProperty.call(source, "produced_patch_sha256") ? { produced_patch_sha256: producedPatchSha256 } : {}),
192
+ ...(Object.prototype.hasOwnProperty.call(source, "model_call_count") ? { model_call_count: modelCallCount } : {}),
193
+ ...(Object.prototype.hasOwnProperty.call(source, "tokens_used") ? { tokens_used: tokensUsed } : {}),
194
+ status: normalizeStatus(source.status),
195
+ cwd: typeof source.cwd === "string" ? redact(source.cwd) : null,
196
+ files: normalizeFiles(source.files),
197
+ timestamp,
198
+ redaction: "applied",
199
+ truncated,
200
+ };
201
+ }
202
+
203
+ function readExistingEventIds(file) {
204
+ if (!fs.existsSync(file)) {
205
+ return new Set();
206
+ }
207
+ const ids = new Set();
208
+ const content = fs.readFileSync(file, "utf8");
209
+ for (const line of content.split(/\r?\n/)) {
210
+ if (!line.trim()) {
211
+ continue;
212
+ }
213
+ try {
214
+ const event = JSON.parse(line);
215
+ if (event && typeof event.event_id === "string") {
216
+ ids.add(event.event_id);
217
+ }
218
+ } catch (_error) {
219
+ // Append-only logs should remain writable even if one old line is malformed.
220
+ }
221
+ }
222
+ return ids;
223
+ }
224
+
225
+ /**
226
+ * Append a redacted trust event.
227
+ *
228
+ * The caller must provide a stable timestamp when retry-idempotency is desired.
229
+ * If timestamp is omitted, each call receives the current ISO timestamp and is
230
+ * treated as a distinct observation.
231
+ */
232
+ function appendEvent(projectPath, raw) {
233
+ const event = buildEvent(raw);
234
+ const file = eventsFile(projectPath, event.timestamp.slice(0, 10));
235
+ fs.mkdirSync(path.dirname(file), { recursive: true });
236
+
237
+ const existingIds = readExistingEventIds(file);
238
+ if (existingIds.has(event.event_id)) {
239
+ return { event, written: false };
240
+ }
241
+
242
+ fs.appendFileSync(file, `${JSON.stringify(event)}\n`, "utf8");
243
+ return { event, written: true };
244
+ }
245
+
246
+ function withKeyedLock(key, fn) {
247
+ const prev = locks.get(key) || Promise.resolve();
248
+ const next = prev.then(fn, fn);
249
+ const cleanup = next.then(
250
+ () => {},
251
+ () => {},
252
+ );
253
+ locks.set(key, cleanup);
254
+ cleanup.then(() => {
255
+ if (locks.get(key) === cleanup) {
256
+ locks.delete(key);
257
+ }
258
+ });
259
+ return next;
260
+ }
261
+
262
+ void withKeyedLock;
263
+
264
+ module.exports = { generateId, fingerprintId, buildEvent, appendEvent, eventsDir, eventsFile };