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,53 +1,53 @@
1
- "use strict";
2
-
3
- const path = require("path");
4
- const { parseFlags, requireFlag, validatePattern } = require("../lib/args");
5
- const { ValidationError } = require("../lib/errors");
6
- const { runBuild } = require("../lib/workflow-build");
7
-
8
- const FLAG_DEFS = {
9
- "feature-key": { type: "string" },
10
- "project-path": { type: "string" },
11
- "active-slice": { type: "string", multiple: true },
12
- note: { type: "string", multiple: true },
13
- "debug-note": { type: "string", multiple: true },
14
- "parallel-reason": { type: "string" },
15
- complete: { type: "boolean" },
16
- blocked: { type: "boolean" },
17
- };
18
-
19
- const FEATURE_KEY_RE = /^[A-Z0-9_]+$/;
20
-
21
- function cmdBuild(args) {
22
- const { flags, positional } = parseFlags(args, FLAG_DEFS);
23
- if (positional.length > 0) {
24
- throw new ValidationError('Unexpected positional arguments. Use flags only for "build".');
25
- }
26
-
27
- const featureKey = requireFlag(flags, "feature-key", "feature-key");
28
- validatePattern(
29
- featureKey,
30
- FEATURE_KEY_RE,
31
- "feature-key",
32
- "Must use UPPER_SNAKE_CASE, for example ORDER_MGMT."
33
- );
34
-
35
- const projectPath = flags["project-path"] ? path.resolve(flags["project-path"]) : process.cwd();
36
- const result = runBuild(projectPath, featureKey, flags);
37
-
38
- console.log("SDTK-CODE workflow build");
39
- console.log(` Feature key: ${featureKey}`);
40
- console.log(` Lane: ${result.lane}`);
41
- console.log(` Status: ${result.phaseStatus}`);
42
- console.log(` Artifact: ${result.artifactPath}`);
43
- console.log(` Slices: ${result.activeSliceCount}`);
44
- console.log(` Mode: ${result.executionMode}`);
45
- console.log(` Next phase: ${result.nextPhase}`);
46
- console.log(` Engine: ${result.suggestedEngineSkills.join(", ")}`);
47
-
48
- return 0;
49
- }
50
-
51
- module.exports = {
52
- cmdBuild,
53
- };
1
+ "use strict";
2
+
3
+ const path = require("path");
4
+ const { parseFlags, requireFlag, validatePattern } = require("../lib/args");
5
+ const { ValidationError } = require("../lib/errors");
6
+ const { runBuild } = require("../lib/workflow-build");
7
+
8
+ const FLAG_DEFS = {
9
+ "feature-key": { type: "string" },
10
+ "project-path": { type: "string" },
11
+ "active-slice": { type: "string", multiple: true },
12
+ note: { type: "string", multiple: true },
13
+ "debug-note": { type: "string", multiple: true },
14
+ "parallel-reason": { type: "string" },
15
+ complete: { type: "boolean" },
16
+ blocked: { type: "boolean" },
17
+ };
18
+
19
+ const FEATURE_KEY_RE = /^[A-Z0-9_]+$/;
20
+
21
+ function cmdBuild(args) {
22
+ const { flags, positional } = parseFlags(args, FLAG_DEFS);
23
+ if (positional.length > 0) {
24
+ throw new ValidationError('Unexpected positional arguments. Use flags only for "build".');
25
+ }
26
+
27
+ const featureKey = requireFlag(flags, "feature-key", "feature-key");
28
+ validatePattern(
29
+ featureKey,
30
+ FEATURE_KEY_RE,
31
+ "feature-key",
32
+ "Must use UPPER_SNAKE_CASE, for example ORDER_MGMT."
33
+ );
34
+
35
+ const projectPath = flags["project-path"] ? path.resolve(flags["project-path"]) : process.cwd();
36
+ const result = runBuild(projectPath, featureKey, flags);
37
+
38
+ console.log("SDTK-CODE workflow build");
39
+ console.log(` Feature key: ${featureKey}`);
40
+ console.log(` Lane: ${result.lane}`);
41
+ console.log(` Status: ${result.phaseStatus}`);
42
+ console.log(` Artifact: ${result.artifactPath}`);
43
+ console.log(` Slices: ${result.activeSliceCount}`);
44
+ console.log(` Mode: ${result.executionMode}`);
45
+ console.log(` Next phase: ${result.nextPhase}`);
46
+ console.log(` Engine: ${result.suggestedEngineSkills.join(", ")}`);
47
+
48
+ return 0;
49
+ }
50
+
51
+ module.exports = {
52
+ cmdBuild,
53
+ };
@@ -1,52 +1,52 @@
1
- "use strict";
2
-
3
- const path = require("path");
4
- const { parseFlags, requireFlag, validatePattern } = require("../lib/args");
5
- const { ValidationError } = require("../lib/errors");
6
- const { runWorkflowDoctor } = require("../lib/workflow-doctor");
7
-
8
- const FLAG_DEFS = {
9
- "feature-key": { type: "string" },
10
- "project-path": { type: "string" },
11
- };
12
-
13
- const FEATURE_KEY_RE = /^[A-Z0-9_]+$/;
14
-
15
- function cmdDoctor(args) {
16
- const { flags, positional } = parseFlags(args, FLAG_DEFS);
17
- if (positional.length > 0) {
18
- throw new ValidationError('Unexpected positional arguments. Use flags only for "doctor".');
19
- }
20
-
21
- const featureKey = requireFlag(flags, "feature-key", "feature-key");
22
- validatePattern(
23
- featureKey,
24
- FEATURE_KEY_RE,
25
- "feature-key",
26
- "Must use UPPER_SNAKE_CASE, for example ORDER_MGMT."
27
- );
28
-
29
- const projectPath = flags["project-path"] ? path.resolve(flags["project-path"]) : process.cwd();
30
- const report = runWorkflowDoctor(projectPath, featureKey);
31
-
32
- console.log("SDTK-CODE workflow doctor");
33
- console.log(` Feature key: ${featureKey}`);
34
- console.log(` Artifact: ${report.state.exists ? report.state.artifactPath : "missing"}`);
35
- console.log(` Result: ${report.result}`);
36
-
37
- if (report.issues.length === 0) {
38
- console.log(" Issues: none");
39
- return 0;
40
- }
41
-
42
- report.issues.forEach((issue, index) => {
43
- console.log(` Issue ${index + 1}: ${issue}`);
44
- });
45
-
46
- console.log(" Repair: diagnostic only in BK-047-lite (no auto-repair)");
47
- return 1;
48
- }
49
-
50
- module.exports = {
51
- cmdDoctor,
1
+ "use strict";
2
+
3
+ const path = require("path");
4
+ const { parseFlags, requireFlag, validatePattern } = require("../lib/args");
5
+ const { ValidationError } = require("../lib/errors");
6
+ const { runWorkflowDoctor } = require("../lib/workflow-doctor");
7
+
8
+ const FLAG_DEFS = {
9
+ "feature-key": { type: "string" },
10
+ "project-path": { type: "string" },
11
+ };
12
+
13
+ const FEATURE_KEY_RE = /^[A-Z0-9_]+$/;
14
+
15
+ function cmdDoctor(args) {
16
+ const { flags, positional } = parseFlags(args, FLAG_DEFS);
17
+ if (positional.length > 0) {
18
+ throw new ValidationError('Unexpected positional arguments. Use flags only for "doctor".');
19
+ }
20
+
21
+ const featureKey = requireFlag(flags, "feature-key", "feature-key");
22
+ validatePattern(
23
+ featureKey,
24
+ FEATURE_KEY_RE,
25
+ "feature-key",
26
+ "Must use UPPER_SNAKE_CASE, for example ORDER_MGMT."
27
+ );
28
+
29
+ const projectPath = flags["project-path"] ? path.resolve(flags["project-path"]) : process.cwd();
30
+ const report = runWorkflowDoctor(projectPath, featureKey);
31
+
32
+ console.log("SDTK-CODE workflow doctor");
33
+ console.log(` Feature key: ${featureKey}`);
34
+ console.log(` Artifact: ${report.state.exists ? report.state.artifactPath : "missing"}`);
35
+ console.log(` Result: ${report.result}`);
36
+
37
+ if (report.issues.length === 0) {
38
+ console.log(" Issues: none");
39
+ return 0;
40
+ }
41
+
42
+ report.issues.forEach((issue, index) => {
43
+ console.log(` Issue ${index + 1}: ${issue}`);
44
+ });
45
+
46
+ console.log(" Repair: diagnostic only in BK-047-lite (no auto-repair)");
47
+ return 1;
48
+ }
49
+
50
+ module.exports = {
51
+ cmdDoctor,
52
52
  };
@@ -1,11 +1,11 @@
1
- "use strict";
2
-
3
- const { ValidationError } = require("../lib/errors");
4
-
5
- function cmdGenerate() {
6
- throw new ValidationError("generate command is not available in SDTK-CODE.");
7
- }
8
-
9
- module.exports = {
10
- cmdGenerate,
11
- };
1
+ "use strict";
2
+
3
+ const { ValidationError } = require("../lib/errors");
4
+
5
+ function cmdGenerate() {
6
+ throw new ValidationError("generate command is not available in SDTK-CODE.");
7
+ }
8
+
9
+ module.exports = {
10
+ cmdGenerate,
11
+ };
@@ -1,215 +1,215 @@
1
- "use strict";
2
-
3
- const fs = require("fs");
4
- const path = require("path");
5
- const { parseFlags, requireFlag } = require("../lib/args");
6
- const { ValidationError } = require("../lib/errors");
7
- const { DEFAULT_POLICY, loadPolicy, classifyCommand, policyFile } = require("../lib/trust-policy");
8
- const { appendEvent } = require("../lib/trust-events");
9
- const { loadScopes, getChangedFiles, checkFiles } = require("../lib/trust-file-scope");
10
- const { writeTraceReport } = require("../lib/trust-trace");
11
-
12
- const CHECK_FLAG_DEFS = {
13
- command: { type: "string" },
14
- "project-path": { type: "string" },
15
- "action-count": { type: "string" },
16
- };
17
-
18
- const INIT_FLAG_DEFS = {
19
- "project-path": { type: "string" },
20
- force: { type: "boolean" },
21
- };
22
-
23
- const SCOPE_FLAG_DEFS = {
24
- "feature-key": { type: "string" },
25
- "project-path": { type: "string" },
26
- base: { type: "string" },
27
- changed: { type: "string", multiple: true },
28
- };
29
-
30
- const TRACE_FLAG_DEFS = {
31
- "feature-key": { type: "string" },
32
- "project-path": { type: "string" },
33
- };
34
-
35
- // allow/ask are advisory observations (info); deny is recorded as blocked.
36
- function statusForVerdict(verdict) {
37
- return verdict === "deny" ? "blocked" : "info";
38
- }
39
-
40
- function resolveProjectPath(flags) {
41
- return flags["project-path"] ? path.resolve(flags["project-path"]) : process.cwd();
42
- }
43
-
44
- function parseActionCount(raw) {
45
- if (raw === undefined || raw === null || raw === "") {
46
- return undefined;
47
- }
48
- const value = Number(raw);
49
- if (!Number.isInteger(value) || value < 0) {
50
- throw new ValidationError(
51
- `Invalid value for --action-count: "${raw}". Must be a non-negative integer.`
52
- );
53
- }
54
- return value;
55
- }
56
-
57
- function runCheck(args) {
58
- const { flags, positional } = parseFlags(args, CHECK_FLAG_DEFS);
59
- if (positional.length > 0) {
60
- throw new ValidationError('Unexpected positional arguments for "guardrails check".');
61
- }
62
-
63
- const command = requireFlag(flags, "command", "command");
64
- const projectPath = resolveProjectPath(flags);
65
- const actionCount = parseActionCount(flags["action-count"]);
66
-
67
- const policy = loadPolicy(projectPath);
68
- const result = classifyCommand(policy, command, { actionCount });
69
-
70
- // Record a redacted trust event (BK-239 buildEvent redacts the command).
71
- appendEvent(projectPath, {
72
- toolkit: "sdtk-code",
73
- event_type: "guardrail_check",
74
- command,
75
- status: statusForVerdict(result.verdict),
76
- });
77
-
78
- console.log("SDTK-CODE guardrails check");
79
- console.log(` Verdict: ${result.verdict}`);
80
- console.log(` Reason: ${result.reason}`);
81
- console.log(
82
- ` Matched: ${result.matchedRule ? `${result.matchedRule.bucket}:${result.matchedRule.pattern}` : "none"}`
83
- );
84
- // Advisory classifier: deny is reported in stdout, not enforced via exit code.
85
- return 0;
86
- }
87
-
88
- function runInit(args) {
89
- const { flags, positional } = parseFlags(args, INIT_FLAG_DEFS);
90
- if (positional.length > 0) {
91
- throw new ValidationError('Unexpected positional arguments for "guardrails init".');
92
- }
93
-
94
- const projectPath = resolveProjectPath(flags);
95
- const target = policyFile(projectPath);
96
-
97
- if (fs.existsSync(target) && !flags.force) {
98
- throw new ValidationError(
99
- [
100
- `Refusing to overwrite existing trust policy: ${target}`,
101
- "Re-run with --force to replace it with the default policy pack.",
102
- ].join("\n")
103
- );
104
- }
105
-
106
- fs.mkdirSync(path.dirname(target), { recursive: true });
107
- fs.writeFileSync(target, `${JSON.stringify(DEFAULT_POLICY, null, 2)}\n`, "utf8");
108
-
109
- console.log("SDTK-CODE guardrails init");
110
- console.log(` Policy: ${target}`);
111
- console.log(` Rules: deny=${DEFAULT_POLICY.deny.length}, ask=${DEFAULT_POLICY.ask.length}, allow=${DEFAULT_POLICY.allow.length}`);
112
- return 0;
113
- }
114
-
115
- // file-scope verdict -> trust event status. fail is a blocking observation.
116
- function statusForScope(verdict) {
117
- return verdict === "fail" ? "blocked" : "info";
118
- }
119
-
120
- function runScope(args) {
121
- const { flags, positional } = parseFlags(args, SCOPE_FLAG_DEFS);
122
- if (positional.length > 0) {
123
- throw new ValidationError('Unexpected positional arguments for "guardrails scope".');
124
- }
125
-
126
- const featureKey = requireFlag(flags, "feature-key", "feature-key");
127
- const projectPath = resolveProjectPath(flags);
128
- const scopes = loadScopes(projectPath, featureKey);
129
-
130
- // Explicit --changed entries take precedence; otherwise diff against base via git.
131
- let changed = Array.isArray(flags.changed) ? flags.changed.filter(Boolean) : [];
132
- if (changed.length === 0) {
133
- try {
134
- changed = getChangedFiles(projectPath, flags.base || "HEAD");
135
- } catch (error) {
136
- throw new ValidationError(
137
- `Could not determine changed files (git): ${error.message}. ` +
138
- "Run inside a git repo, or pass --changed <file> entries explicitly."
139
- );
140
- }
141
- }
142
-
143
- const result = checkFiles(scopes, changed);
144
-
145
- appendEvent(projectPath, {
146
- toolkit: "sdtk-code",
147
- event_type: "file_scope_check",
148
- phase: "verify",
149
- status: statusForScope(result.verdict),
150
- files: result.outOfScope.concat(result.blockedHits),
151
- });
152
-
153
- console.log("SDTK-CODE guardrails scope");
154
- console.log(` Feature key: ${featureKey}`);
155
- console.log(` Scope source: ${scopes.source}${scopes.declared ? "" : " (no scope declared)"}`);
156
- console.log(` Verdict: ${result.verdict}`);
157
- console.log(` In scope: ${result.inScope.length}`);
158
- if (result.outOfScope.length > 0) {
159
- console.log(` Out of scope: ${result.outOfScope.join(", ")}`);
160
- }
161
- if (result.blockedHits.length > 0) {
162
- console.log(` Blocked: ${result.blockedHits.join(", ")}`);
163
- }
164
- // Advisory gate: verdict is reported in stdout, not enforced via exit code.
165
- return 0;
166
- }
167
-
168
- function runTrace(args) {
169
- const { flags, positional } = parseFlags(args, TRACE_FLAG_DEFS);
170
- if (positional.length > 0) {
171
- throw new ValidationError('Unexpected positional arguments for "guardrails trace".');
172
- }
173
-
174
- const featureKey = requireFlag(flags, "feature-key", "feature-key");
175
- const projectPath = resolveProjectPath(flags);
176
- const result = writeTraceReport(projectPath, featureKey);
177
- const totals = result.trace.totals;
178
-
179
- console.log("SDTK-CODE guardrails trace");
180
- console.log(` Feature key: ${featureKey}`);
181
- console.log(` Report: ${result.path}`);
182
- console.log(` Events: ${totals.events}`);
183
- console.log(` Failures: ${totals.failures}`);
184
- console.log(` Blocked: ${totals.blocked}`);
185
- console.log(` Decision gates: ${totals.decisionGates}`);
186
- return 0;
187
- }
188
-
189
- function cmdGuardrails(args) {
190
- const list = Array.isArray(args) ? args : [];
191
- const subcommand = list[0];
192
- if (!subcommand || subcommand.startsWith("-")) {
193
- throw new ValidationError('Missing guardrails subcommand. Use one of: check, init, scope, trace.');
194
- }
195
-
196
- const rest = list.slice(1);
197
- switch (subcommand) {
198
- case "check":
199
- return runCheck(rest);
200
- case "init":
201
- return runInit(rest);
202
- case "scope":
203
- return runScope(rest);
204
- case "trace":
205
- return runTrace(rest);
206
- default:
207
- throw new ValidationError(
208
- `Unknown guardrails subcommand: "${subcommand}". Use one of: check, init, scope, trace.`
209
- );
210
- }
211
- }
212
-
213
- module.exports = {
214
- cmdGuardrails,
215
- };
1
+ "use strict";
2
+
3
+ const fs = require("fs");
4
+ const path = require("path");
5
+ const { parseFlags, requireFlag } = require("../lib/args");
6
+ const { ValidationError } = require("../lib/errors");
7
+ const { DEFAULT_POLICY, loadPolicy, classifyCommand, policyFile } = require("../lib/trust-policy");
8
+ const { appendEvent } = require("../lib/trust-events");
9
+ const { loadScopes, getChangedFiles, checkFiles } = require("../lib/trust-file-scope");
10
+ const { writeTraceReport } = require("../lib/trust-trace");
11
+
12
+ const CHECK_FLAG_DEFS = {
13
+ command: { type: "string" },
14
+ "project-path": { type: "string" },
15
+ "action-count": { type: "string" },
16
+ };
17
+
18
+ const INIT_FLAG_DEFS = {
19
+ "project-path": { type: "string" },
20
+ force: { type: "boolean" },
21
+ };
22
+
23
+ const SCOPE_FLAG_DEFS = {
24
+ "feature-key": { type: "string" },
25
+ "project-path": { type: "string" },
26
+ base: { type: "string" },
27
+ changed: { type: "string", multiple: true },
28
+ };
29
+
30
+ const TRACE_FLAG_DEFS = {
31
+ "feature-key": { type: "string" },
32
+ "project-path": { type: "string" },
33
+ };
34
+
35
+ // allow/ask are advisory observations (info); deny is recorded as blocked.
36
+ function statusForVerdict(verdict) {
37
+ return verdict === "deny" ? "blocked" : "info";
38
+ }
39
+
40
+ function resolveProjectPath(flags) {
41
+ return flags["project-path"] ? path.resolve(flags["project-path"]) : process.cwd();
42
+ }
43
+
44
+ function parseActionCount(raw) {
45
+ if (raw === undefined || raw === null || raw === "") {
46
+ return undefined;
47
+ }
48
+ const value = Number(raw);
49
+ if (!Number.isInteger(value) || value < 0) {
50
+ throw new ValidationError(
51
+ `Invalid value for --action-count: "${raw}". Must be a non-negative integer.`
52
+ );
53
+ }
54
+ return value;
55
+ }
56
+
57
+ function runCheck(args) {
58
+ const { flags, positional } = parseFlags(args, CHECK_FLAG_DEFS);
59
+ if (positional.length > 0) {
60
+ throw new ValidationError('Unexpected positional arguments for "guardrails check".');
61
+ }
62
+
63
+ const command = requireFlag(flags, "command", "command");
64
+ const projectPath = resolveProjectPath(flags);
65
+ const actionCount = parseActionCount(flags["action-count"]);
66
+
67
+ const policy = loadPolicy(projectPath);
68
+ const result = classifyCommand(policy, command, { actionCount });
69
+
70
+ // Record a redacted trust event (BK-239 buildEvent redacts the command).
71
+ appendEvent(projectPath, {
72
+ toolkit: "sdtk-code",
73
+ event_type: "guardrail_check",
74
+ command,
75
+ status: statusForVerdict(result.verdict),
76
+ });
77
+
78
+ console.log("SDTK-CODE guardrails check");
79
+ console.log(` Verdict: ${result.verdict}`);
80
+ console.log(` Reason: ${result.reason}`);
81
+ console.log(
82
+ ` Matched: ${result.matchedRule ? `${result.matchedRule.bucket}:${result.matchedRule.pattern}` : "none"}`
83
+ );
84
+ // Advisory classifier: deny is reported in stdout, not enforced via exit code.
85
+ return 0;
86
+ }
87
+
88
+ function runInit(args) {
89
+ const { flags, positional } = parseFlags(args, INIT_FLAG_DEFS);
90
+ if (positional.length > 0) {
91
+ throw new ValidationError('Unexpected positional arguments for "guardrails init".');
92
+ }
93
+
94
+ const projectPath = resolveProjectPath(flags);
95
+ const target = policyFile(projectPath);
96
+
97
+ if (fs.existsSync(target) && !flags.force) {
98
+ throw new ValidationError(
99
+ [
100
+ `Refusing to overwrite existing trust policy: ${target}`,
101
+ "Re-run with --force to replace it with the default policy pack.",
102
+ ].join("\n")
103
+ );
104
+ }
105
+
106
+ fs.mkdirSync(path.dirname(target), { recursive: true });
107
+ fs.writeFileSync(target, `${JSON.stringify(DEFAULT_POLICY, null, 2)}\n`, "utf8");
108
+
109
+ console.log("SDTK-CODE guardrails init");
110
+ console.log(` Policy: ${target}`);
111
+ console.log(` Rules: deny=${DEFAULT_POLICY.deny.length}, ask=${DEFAULT_POLICY.ask.length}, allow=${DEFAULT_POLICY.allow.length}`);
112
+ return 0;
113
+ }
114
+
115
+ // file-scope verdict -> trust event status. fail is a blocking observation.
116
+ function statusForScope(verdict) {
117
+ return verdict === "fail" ? "blocked" : "info";
118
+ }
119
+
120
+ function runScope(args) {
121
+ const { flags, positional } = parseFlags(args, SCOPE_FLAG_DEFS);
122
+ if (positional.length > 0) {
123
+ throw new ValidationError('Unexpected positional arguments for "guardrails scope".');
124
+ }
125
+
126
+ const featureKey = requireFlag(flags, "feature-key", "feature-key");
127
+ const projectPath = resolveProjectPath(flags);
128
+ const scopes = loadScopes(projectPath, featureKey);
129
+
130
+ // Explicit --changed entries take precedence; otherwise diff against base via git.
131
+ let changed = Array.isArray(flags.changed) ? flags.changed.filter(Boolean) : [];
132
+ if (changed.length === 0) {
133
+ try {
134
+ changed = getChangedFiles(projectPath, flags.base || "HEAD");
135
+ } catch (error) {
136
+ throw new ValidationError(
137
+ `Could not determine changed files (git): ${error.message}. ` +
138
+ "Run inside a git repo, or pass --changed <file> entries explicitly."
139
+ );
140
+ }
141
+ }
142
+
143
+ const result = checkFiles(scopes, changed);
144
+
145
+ appendEvent(projectPath, {
146
+ toolkit: "sdtk-code",
147
+ event_type: "file_scope_check",
148
+ phase: "verify",
149
+ status: statusForScope(result.verdict),
150
+ files: result.outOfScope.concat(result.blockedHits),
151
+ });
152
+
153
+ console.log("SDTK-CODE guardrails scope");
154
+ console.log(` Feature key: ${featureKey}`);
155
+ console.log(` Scope source: ${scopes.source}${scopes.declared ? "" : " (no scope declared)"}`);
156
+ console.log(` Verdict: ${result.verdict}`);
157
+ console.log(` In scope: ${result.inScope.length}`);
158
+ if (result.outOfScope.length > 0) {
159
+ console.log(` Out of scope: ${result.outOfScope.join(", ")}`);
160
+ }
161
+ if (result.blockedHits.length > 0) {
162
+ console.log(` Blocked: ${result.blockedHits.join(", ")}`);
163
+ }
164
+ // Advisory gate: verdict is reported in stdout, not enforced via exit code.
165
+ return 0;
166
+ }
167
+
168
+ function runTrace(args) {
169
+ const { flags, positional } = parseFlags(args, TRACE_FLAG_DEFS);
170
+ if (positional.length > 0) {
171
+ throw new ValidationError('Unexpected positional arguments for "guardrails trace".');
172
+ }
173
+
174
+ const featureKey = requireFlag(flags, "feature-key", "feature-key");
175
+ const projectPath = resolveProjectPath(flags);
176
+ const result = writeTraceReport(projectPath, featureKey);
177
+ const totals = result.trace.totals;
178
+
179
+ console.log("SDTK-CODE guardrails trace");
180
+ console.log(` Feature key: ${featureKey}`);
181
+ console.log(` Report: ${result.path}`);
182
+ console.log(` Events: ${totals.events}`);
183
+ console.log(` Failures: ${totals.failures}`);
184
+ console.log(` Blocked: ${totals.blocked}`);
185
+ console.log(` Decision gates: ${totals.decisionGates}`);
186
+ return 0;
187
+ }
188
+
189
+ function cmdGuardrails(args) {
190
+ const list = Array.isArray(args) ? args : [];
191
+ const subcommand = list[0];
192
+ if (!subcommand || subcommand.startsWith("-")) {
193
+ throw new ValidationError('Missing guardrails subcommand. Use one of: check, init, scope, trace.');
194
+ }
195
+
196
+ const rest = list.slice(1);
197
+ switch (subcommand) {
198
+ case "check":
199
+ return runCheck(rest);
200
+ case "init":
201
+ return runInit(rest);
202
+ case "scope":
203
+ return runScope(rest);
204
+ case "trace":
205
+ return runTrace(rest);
206
+ default:
207
+ throw new ValidationError(
208
+ `Unknown guardrails subcommand: "${subcommand}". Use one of: check, init, scope, trace.`
209
+ );
210
+ }
211
+ }
212
+
213
+ module.exports = {
214
+ cmdGuardrails,
215
+ };