sdtk-code-kit 0.1.3 → 0.2.0
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 +202 -185
- package/assets/manifest/toolkit-bundle.manifest.json +49 -49
- package/assets/manifest/toolkit-bundle.sha256.txt +24 -24
- package/assets/toolkit/toolkit/scripts/install-claude-skills.ps1 +78 -78
- package/assets/toolkit/toolkit/scripts/install-codex-skills.ps1 +150 -150
- package/assets/toolkit/toolkit/scripts/uninstall-claude-skills.ps1 +62 -62
- package/assets/toolkit/toolkit/scripts/uninstall-codex-skills.ps1 +82 -82
- package/assets/toolkit/toolkit/sdtk-spec.config.profiles.example.json +12 -12
- package/assets/toolkit/toolkit/skills/code-brainstorm/SKILL.md +55 -55
- package/assets/toolkit/toolkit/skills/code-debug/SKILL.md +1 -1
- package/assets/toolkit/toolkit/skills/code-debug/references/root-cause-tracing.md +68 -68
- package/assets/toolkit/toolkit/skills/code-execute/SKILL.md +74 -74
- package/assets/toolkit/toolkit/skills/code-finish/SKILL.md +205 -205
- package/assets/toolkit/toolkit/skills/code-parallel/SKILL.md +64 -64
- package/assets/toolkit/toolkit/skills/code-parallel/references/code-quality-reviewer-prompt.md +26 -26
- package/assets/toolkit/toolkit/skills/code-parallel/references/implementer-prompt.md +113 -113
- package/assets/toolkit/toolkit/skills/code-parallel/references/spec-reviewer-prompt.md +61 -61
- package/assets/toolkit/toolkit/skills/code-plan/SKILL.md +85 -85
- package/assets/toolkit/toolkit/skills/code-review/SKILL.md +79 -79
- package/assets/toolkit/toolkit/skills/code-review/references/checklist.md +125 -125
- package/assets/toolkit/toolkit/skills/code-review/references/code-reviewer.md +146 -146
- package/assets/toolkit/toolkit/skills/code-ship/SKILL.md +52 -52
- package/assets/toolkit/toolkit/skills/code-tdd/SKILL.md +1 -1
- package/assets/toolkit/toolkit/skills/code-tdd/references/testing-anti-patterns.md +299 -299
- package/assets/toolkit/toolkit/skills/code-verify/SKILL.md +144 -144
- package/assets/toolkit/toolkit/skills/code-worktree/SKILL.md +13 -13
- package/assets/toolkit/toolkit/templates/CODE_WORKFLOW_TEMPLATE.md +140 -112
- package/bin/sdtk-code.js +14 -14
- package/package.json +4 -4
- package/src/commands/build.js +53 -53
- package/src/commands/doctor.js +52 -0
- package/src/commands/generate.js +11 -11
- package/src/commands/guardrails.js +123 -0
- package/src/commands/help.js +28 -4
- package/src/commands/plan.js +53 -53
- package/src/commands/readiness.js +77 -0
- package/src/commands/resume.js +62 -0
- package/src/commands/runtime.js +224 -224
- package/src/commands/ship.js +59 -59
- package/src/commands/start.js +89 -89
- package/src/commands/status.js +78 -0
- package/src/commands/verify.js +71 -66
- package/src/index.js +32 -1
- package/src/lib/args.js +107 -107
- package/src/lib/errors.js +41 -41
- package/src/lib/powershell.js +65 -65
- package/src/lib/scope.js +72 -72
- package/src/lib/ship-readiness.js +281 -0
- package/src/lib/toolkit-payload.js +72 -72
- package/src/lib/trust-events.js +166 -0
- package/src/lib/trust-file-scope.js +154 -0
- package/src/lib/trust-policy.js +212 -0
- package/src/lib/trust-redact.js +35 -0
- package/src/lib/trust-trace.js +179 -0
- package/src/lib/workflow-artifact.js +503 -496
- package/src/lib/workflow-build.js +127 -127
- package/src/lib/workflow-doctor.js +84 -0
- package/src/lib/workflow-handoff.js +1029 -937
- package/src/lib/workflow-intake.js +101 -101
- package/src/lib/workflow-plan.js +193 -193
- package/src/lib/workflow-review-packet.js +89 -0
- package/src/lib/workflow-ship.js +213 -213
- package/src/lib/workflow-state.js +439 -0
- package/src/lib/workflow-verify.js +43 -6
|
@@ -1,72 +1,72 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
const crypto = require("crypto");
|
|
4
|
-
const fs = require("fs");
|
|
5
|
-
const path = require("path");
|
|
6
|
-
const { IntegrityError } = require("./errors");
|
|
7
|
-
|
|
8
|
-
const PACKAGE_ROOT = path.resolve(__dirname, "..", "..");
|
|
9
|
-
const ASSETS_DIR = path.join(PACKAGE_ROOT, "assets", "toolkit");
|
|
10
|
-
const MANIFEST_PATH = path.join(
|
|
11
|
-
PACKAGE_ROOT,
|
|
12
|
-
"assets",
|
|
13
|
-
"manifest",
|
|
14
|
-
"toolkit-bundle.manifest.json"
|
|
15
|
-
);
|
|
16
|
-
|
|
17
|
-
function loadManifest() {
|
|
18
|
-
if (!fs.existsSync(MANIFEST_PATH)) {
|
|
19
|
-
throw new IntegrityError(
|
|
20
|
-
`SDTK-CODE toolkit manifest not found: ${MANIFEST_PATH}\nRun "npm run build:payload" to sync toolkit assets.`
|
|
21
|
-
);
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
try {
|
|
25
|
-
return JSON.parse(fs.readFileSync(MANIFEST_PATH, "utf-8"));
|
|
26
|
-
} catch (error) {
|
|
27
|
-
throw new IntegrityError(`Failed to parse SDTK-CODE toolkit manifest: ${error.message}`);
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
function hashFile(filePath) {
|
|
32
|
-
return crypto.createHash("sha256").update(fs.readFileSync(filePath)).digest("hex");
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
function verify() {
|
|
36
|
-
const manifest = loadManifest();
|
|
37
|
-
if (!Array.isArray(manifest.files)) {
|
|
38
|
-
throw new IntegrityError("SDTK-CODE toolkit manifest has no files array.");
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
const errors = [];
|
|
42
|
-
for (const entry of manifest.files) {
|
|
43
|
-
const filePath = path.join(ASSETS_DIR, entry.path);
|
|
44
|
-
if (!fs.existsSync(filePath)) {
|
|
45
|
-
errors.push(`MISSING: ${entry.path}`);
|
|
46
|
-
continue;
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
const actualHash = hashFile(filePath);
|
|
50
|
-
if (actualHash !== entry.sha256) {
|
|
51
|
-
errors.push(
|
|
52
|
-
`HASH MISMATCH: ${entry.path}\n expected: ${entry.sha256}\n actual: ${actualHash}`
|
|
53
|
-
);
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
if (errors.length > 0) {
|
|
58
|
-
throw new IntegrityError(`SDTK-CODE toolkit payload integrity check failed:\n${errors.join("\n")}`);
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
function resolvePayloadFile(relativePath) {
|
|
63
|
-
return path.join(ASSETS_DIR, relativePath);
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
module.exports = {
|
|
67
|
-
ASSETS_DIR,
|
|
68
|
-
loadManifest,
|
|
69
|
-
hashFile,
|
|
70
|
-
resolvePayloadFile,
|
|
71
|
-
verify,
|
|
72
|
-
};
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const crypto = require("crypto");
|
|
4
|
+
const fs = require("fs");
|
|
5
|
+
const path = require("path");
|
|
6
|
+
const { IntegrityError } = require("./errors");
|
|
7
|
+
|
|
8
|
+
const PACKAGE_ROOT = path.resolve(__dirname, "..", "..");
|
|
9
|
+
const ASSETS_DIR = path.join(PACKAGE_ROOT, "assets", "toolkit");
|
|
10
|
+
const MANIFEST_PATH = path.join(
|
|
11
|
+
PACKAGE_ROOT,
|
|
12
|
+
"assets",
|
|
13
|
+
"manifest",
|
|
14
|
+
"toolkit-bundle.manifest.json"
|
|
15
|
+
);
|
|
16
|
+
|
|
17
|
+
function loadManifest() {
|
|
18
|
+
if (!fs.existsSync(MANIFEST_PATH)) {
|
|
19
|
+
throw new IntegrityError(
|
|
20
|
+
`SDTK-CODE toolkit manifest not found: ${MANIFEST_PATH}\nRun "npm run build:payload" to sync toolkit assets.`
|
|
21
|
+
);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
try {
|
|
25
|
+
return JSON.parse(fs.readFileSync(MANIFEST_PATH, "utf-8"));
|
|
26
|
+
} catch (error) {
|
|
27
|
+
throw new IntegrityError(`Failed to parse SDTK-CODE toolkit manifest: ${error.message}`);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function hashFile(filePath) {
|
|
32
|
+
return crypto.createHash("sha256").update(fs.readFileSync(filePath)).digest("hex");
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function verify() {
|
|
36
|
+
const manifest = loadManifest();
|
|
37
|
+
if (!Array.isArray(manifest.files)) {
|
|
38
|
+
throw new IntegrityError("SDTK-CODE toolkit manifest has no files array.");
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const errors = [];
|
|
42
|
+
for (const entry of manifest.files) {
|
|
43
|
+
const filePath = path.join(ASSETS_DIR, entry.path);
|
|
44
|
+
if (!fs.existsSync(filePath)) {
|
|
45
|
+
errors.push(`MISSING: ${entry.path}`);
|
|
46
|
+
continue;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const actualHash = hashFile(filePath);
|
|
50
|
+
if (actualHash !== entry.sha256) {
|
|
51
|
+
errors.push(
|
|
52
|
+
`HASH MISMATCH: ${entry.path}\n expected: ${entry.sha256}\n actual: ${actualHash}`
|
|
53
|
+
);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
if (errors.length > 0) {
|
|
58
|
+
throw new IntegrityError(`SDTK-CODE toolkit payload integrity check failed:\n${errors.join("\n")}`);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function resolvePayloadFile(relativePath) {
|
|
63
|
+
return path.join(ASSETS_DIR, relativePath);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
module.exports = {
|
|
67
|
+
ASSETS_DIR,
|
|
68
|
+
loadManifest,
|
|
69
|
+
hashFile,
|
|
70
|
+
resolvePayloadFile,
|
|
71
|
+
verify,
|
|
72
|
+
};
|
|
@@ -0,0 +1,166 @@
|
|
|
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
|
+
const canonical = [sessionId || "", toolkit || "", eventType || "", command || "", timestamp].join("|");
|
|
88
|
+
|
|
89
|
+
return {
|
|
90
|
+
event_id: fingerprintId("evt", canonical),
|
|
91
|
+
session_id: sessionId,
|
|
92
|
+
toolkit,
|
|
93
|
+
phase: nullableString(source.phase),
|
|
94
|
+
event_type: eventType,
|
|
95
|
+
command,
|
|
96
|
+
status: normalizeStatus(source.status),
|
|
97
|
+
cwd: typeof source.cwd === "string" ? redact(source.cwd) : null,
|
|
98
|
+
files: normalizeFiles(source.files),
|
|
99
|
+
timestamp,
|
|
100
|
+
redaction: "applied",
|
|
101
|
+
truncated,
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
function readExistingEventIds(file) {
|
|
106
|
+
if (!fs.existsSync(file)) {
|
|
107
|
+
return new Set();
|
|
108
|
+
}
|
|
109
|
+
const ids = new Set();
|
|
110
|
+
const content = fs.readFileSync(file, "utf8");
|
|
111
|
+
for (const line of content.split(/\r?\n/)) {
|
|
112
|
+
if (!line.trim()) {
|
|
113
|
+
continue;
|
|
114
|
+
}
|
|
115
|
+
try {
|
|
116
|
+
const event = JSON.parse(line);
|
|
117
|
+
if (event && typeof event.event_id === "string") {
|
|
118
|
+
ids.add(event.event_id);
|
|
119
|
+
}
|
|
120
|
+
} catch (_error) {
|
|
121
|
+
// Append-only logs should remain writable even if one old line is malformed.
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
return ids;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* Append a redacted trust event.
|
|
129
|
+
*
|
|
130
|
+
* The caller must provide a stable timestamp when retry-idempotency is desired.
|
|
131
|
+
* If timestamp is omitted, each call receives the current ISO timestamp and is
|
|
132
|
+
* treated as a distinct observation.
|
|
133
|
+
*/
|
|
134
|
+
function appendEvent(projectPath, raw) {
|
|
135
|
+
const event = buildEvent(raw);
|
|
136
|
+
const file = eventsFile(projectPath, event.timestamp.slice(0, 10));
|
|
137
|
+
fs.mkdirSync(path.dirname(file), { recursive: true });
|
|
138
|
+
|
|
139
|
+
const existingIds = readExistingEventIds(file);
|
|
140
|
+
if (existingIds.has(event.event_id)) {
|
|
141
|
+
return { event, written: false };
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
fs.appendFileSync(file, `${JSON.stringify(event)}\n`, "utf8");
|
|
145
|
+
return { event, written: true };
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
function withKeyedLock(key, fn) {
|
|
149
|
+
const prev = locks.get(key) || Promise.resolve();
|
|
150
|
+
const next = prev.then(fn, fn);
|
|
151
|
+
const cleanup = next.then(
|
|
152
|
+
() => {},
|
|
153
|
+
() => {},
|
|
154
|
+
);
|
|
155
|
+
locks.set(key, cleanup);
|
|
156
|
+
cleanup.then(() => {
|
|
157
|
+
if (locks.get(key) === cleanup) {
|
|
158
|
+
locks.delete(key);
|
|
159
|
+
}
|
|
160
|
+
});
|
|
161
|
+
return next;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
void withKeyedLock;
|
|
165
|
+
|
|
166
|
+
module.exports = { generateId, fingerprintId, buildEvent, appendEvent, eventsDir, eventsFile };
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const childProcess = require("child_process");
|
|
4
|
+
const fs = require("fs");
|
|
5
|
+
const path = require("path");
|
|
6
|
+
|
|
7
|
+
function normalizePatterns(value) {
|
|
8
|
+
if (!Array.isArray(value)) {
|
|
9
|
+
return [];
|
|
10
|
+
}
|
|
11
|
+
return value.filter((entry) => typeof entry === "string" && entry.length > 0);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
function loadScopes(projectPath, featureKey, opts = {}) {
|
|
15
|
+
const optAllowed = normalizePatterns(opts.allowed);
|
|
16
|
+
const optBlocked = normalizePatterns(opts.blocked);
|
|
17
|
+
if (Array.isArray(opts.allowed) || Array.isArray(opts.blocked)) {
|
|
18
|
+
return {
|
|
19
|
+
allowed: optAllowed,
|
|
20
|
+
blocked: optBlocked,
|
|
21
|
+
declared: optAllowed.length > 0 || optBlocked.length > 0,
|
|
22
|
+
source: "opts",
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const handoffPath = path.join(
|
|
27
|
+
path.resolve(projectPath || "."),
|
|
28
|
+
"docs",
|
|
29
|
+
"dev",
|
|
30
|
+
`CODE_HANDOFF_${featureKey}.json`,
|
|
31
|
+
);
|
|
32
|
+
|
|
33
|
+
try {
|
|
34
|
+
const payload = JSON.parse(fs.readFileSync(handoffPath, "utf8"));
|
|
35
|
+
const allowed = normalizePatterns(payload.allowed_file_scopes);
|
|
36
|
+
const blocked = normalizePatterns(payload.blocked_file_scopes);
|
|
37
|
+
if (Array.isArray(payload.allowed_file_scopes) || Array.isArray(payload.blocked_file_scopes)) {
|
|
38
|
+
return {
|
|
39
|
+
allowed,
|
|
40
|
+
blocked,
|
|
41
|
+
declared: allowed.length > 0 || blocked.length > 0,
|
|
42
|
+
source: "handoff",
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
} catch (_error) {
|
|
46
|
+
// Missing or invalid optional forward-compat handoff fields fall back cleanly.
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
return { allowed: [], blocked: [], declared: false, source: "none" };
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function escapeRegexChar(char) {
|
|
53
|
+
return /[\\^$+?.()|[\]{}]/.test(char) ? `\\${char}` : char;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
function globToRegex(pattern) {
|
|
57
|
+
let source = "";
|
|
58
|
+
for (let index = 0; index < pattern.length; index += 1) {
|
|
59
|
+
const char = pattern[index];
|
|
60
|
+
const next = pattern[index + 1];
|
|
61
|
+
if (char === "*") {
|
|
62
|
+
if (next === "*") {
|
|
63
|
+
source += ".*";
|
|
64
|
+
index += 1;
|
|
65
|
+
} else {
|
|
66
|
+
source += "[^/]*";
|
|
67
|
+
}
|
|
68
|
+
} else if (char === "?") {
|
|
69
|
+
source += "[^/]";
|
|
70
|
+
} else {
|
|
71
|
+
source += escapeRegexChar(char);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
return new RegExp(`^${source}$`);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
function normalizePathForMatch(file) {
|
|
78
|
+
return String(file || "").replace(/\\/g, "/");
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
function matchOne(file, pattern) {
|
|
82
|
+
if (typeof pattern !== "string" || pattern.length === 0) {
|
|
83
|
+
return false;
|
|
84
|
+
}
|
|
85
|
+
const normalizedFile = normalizePathForMatch(file);
|
|
86
|
+
if (pattern.startsWith("re:")) {
|
|
87
|
+
return new RegExp(`^${pattern.slice(3)}$`).test(normalizedFile);
|
|
88
|
+
}
|
|
89
|
+
return globToRegex(normalizePathForMatch(pattern)).test(normalizedFile);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
function matchScope(file, patterns) {
|
|
93
|
+
return normalizePatterns(patterns).some((pattern) => matchOne(file, pattern));
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
function checkFiles(scopes, changedFiles) {
|
|
97
|
+
const allowed = normalizePatterns(scopes && scopes.allowed);
|
|
98
|
+
const blocked = normalizePatterns(scopes && scopes.blocked);
|
|
99
|
+
const declared = Boolean(scopes && scopes.declared);
|
|
100
|
+
const files = Array.isArray(changedFiles)
|
|
101
|
+
? changedFiles.filter((entry) => typeof entry === "string" && entry.length > 0)
|
|
102
|
+
: [];
|
|
103
|
+
|
|
104
|
+
if (!declared && allowed.length === 0 && blocked.length === 0) {
|
|
105
|
+
return {
|
|
106
|
+
inScope: [],
|
|
107
|
+
outOfScope: [],
|
|
108
|
+
blockedHits: [],
|
|
109
|
+
verdict: "needs_human",
|
|
110
|
+
declared: false,
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
const inScope = [];
|
|
115
|
+
const outOfScope = [];
|
|
116
|
+
const blockedHits = [];
|
|
117
|
+
const hasAllowed = allowed.length > 0;
|
|
118
|
+
|
|
119
|
+
for (const file of files) {
|
|
120
|
+
if (matchScope(file, blocked)) {
|
|
121
|
+
blockedHits.push(file);
|
|
122
|
+
continue;
|
|
123
|
+
}
|
|
124
|
+
if (!hasAllowed || matchScope(file, allowed)) {
|
|
125
|
+
inScope.push(file);
|
|
126
|
+
} else {
|
|
127
|
+
outOfScope.push(file);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
const verdict = blockedHits.length > 0 || outOfScope.length > 0 ? "fail" : "pass";
|
|
132
|
+
return { inScope, outOfScope, blockedHits, verdict, declared: true };
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
function getChangedFiles(projectPath, baseRef = "HEAD") {
|
|
136
|
+
const result = childProcess.spawnSync(
|
|
137
|
+
"git",
|
|
138
|
+
["diff", "--name-only", baseRef],
|
|
139
|
+
{
|
|
140
|
+
cwd: path.resolve(projectPath || "."),
|
|
141
|
+
encoding: "utf8",
|
|
142
|
+
errors: "replace",
|
|
143
|
+
},
|
|
144
|
+
);
|
|
145
|
+
if (result.error) {
|
|
146
|
+
throw result.error;
|
|
147
|
+
}
|
|
148
|
+
if (result.status !== 0) {
|
|
149
|
+
throw new Error((result.stderr || "git diff --name-only failed").trim());
|
|
150
|
+
}
|
|
151
|
+
return result.stdout.split(/\r?\n/).map((line) => line.trim()).filter(Boolean);
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
module.exports = { loadScopes, matchScope, checkFiles, getChangedFiles };
|
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const fs = require("fs");
|
|
4
|
+
const path = require("path");
|
|
5
|
+
const { ValidationError } = require("./errors");
|
|
6
|
+
|
|
7
|
+
// Default allow/ask/deny pack (BK-240 §5), derived from the reference harness
|
|
8
|
+
// settings.json command classes. Patterns starting with "^" are treated as
|
|
9
|
+
// anchored regular expressions; all other patterns are case-sensitive
|
|
10
|
+
// substring matches against the command string. NO shell execution: this is
|
|
11
|
+
// advisory classification only.
|
|
12
|
+
const DEFAULT_POLICY = {
|
|
13
|
+
deny: [
|
|
14
|
+
"^rm -rf",
|
|
15
|
+
"^rm -fr",
|
|
16
|
+
"^sudo ",
|
|
17
|
+
"git reset --hard",
|
|
18
|
+
"git push --force",
|
|
19
|
+
"git push -f",
|
|
20
|
+
"mkfs",
|
|
21
|
+
"^dd ",
|
|
22
|
+
".env",
|
|
23
|
+
".pem",
|
|
24
|
+
"secrets/",
|
|
25
|
+
"^psql",
|
|
26
|
+
"^mysql",
|
|
27
|
+
"^mongod",
|
|
28
|
+
],
|
|
29
|
+
ask: [
|
|
30
|
+
"npm install",
|
|
31
|
+
"npm publish",
|
|
32
|
+
"npm exec",
|
|
33
|
+
"npx",
|
|
34
|
+
"git merge",
|
|
35
|
+
"git rebase",
|
|
36
|
+
"git clean -fd",
|
|
37
|
+
"git clean -fx",
|
|
38
|
+
"git clean -fdx",
|
|
39
|
+
],
|
|
40
|
+
allow: [
|
|
41
|
+
"git status",
|
|
42
|
+
"git diff",
|
|
43
|
+
"git log",
|
|
44
|
+
"git branch",
|
|
45
|
+
"git show",
|
|
46
|
+
"npm test",
|
|
47
|
+
"npm run test",
|
|
48
|
+
"npm run lint",
|
|
49
|
+
"npm run build",
|
|
50
|
+
"pytest",
|
|
51
|
+
"python -m unittest",
|
|
52
|
+
"rg ",
|
|
53
|
+
"ls ",
|
|
54
|
+
"node --check",
|
|
55
|
+
],
|
|
56
|
+
// limits.max_actions is omitted by default; callers may add it (and supply
|
|
57
|
+
// --action-count) to enable the action-count cap.
|
|
58
|
+
limits: {},
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
function assertPatternArray(value, label) {
|
|
62
|
+
if (!Array.isArray(value)) {
|
|
63
|
+
throw new ValidationError(
|
|
64
|
+
`Invalid trust policy: "${label}" must be an array of string patterns.`
|
|
65
|
+
);
|
|
66
|
+
}
|
|
67
|
+
value.forEach((entry, index) => {
|
|
68
|
+
if (typeof entry !== "string") {
|
|
69
|
+
throw new ValidationError(
|
|
70
|
+
`Invalid trust policy: "${label}[${index}]" must be a string pattern.`
|
|
71
|
+
);
|
|
72
|
+
}
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function validatePolicy(obj) {
|
|
77
|
+
if (!obj || typeof obj !== "object" || Array.isArray(obj)) {
|
|
78
|
+
throw new ValidationError("Invalid trust policy: expected a JSON object.");
|
|
79
|
+
}
|
|
80
|
+
assertPatternArray(obj.deny, "deny");
|
|
81
|
+
assertPatternArray(obj.ask, "ask");
|
|
82
|
+
assertPatternArray(obj.allow, "allow");
|
|
83
|
+
|
|
84
|
+
if (obj.limits !== undefined && obj.limits !== null) {
|
|
85
|
+
if (typeof obj.limits !== "object" || Array.isArray(obj.limits)) {
|
|
86
|
+
throw new ValidationError('Invalid trust policy: "limits" must be an object.');
|
|
87
|
+
}
|
|
88
|
+
if (
|
|
89
|
+
obj.limits.max_actions !== undefined &&
|
|
90
|
+
typeof obj.limits.max_actions !== "number"
|
|
91
|
+
) {
|
|
92
|
+
throw new ValidationError(
|
|
93
|
+
'Invalid trust policy: "limits.max_actions" must be a number.'
|
|
94
|
+
);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
return obj;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
function policyFile(projectPath) {
|
|
101
|
+
return path.join(path.resolve(projectPath || "."), ".sdtk", "trust", "policy.json");
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
function loadPolicy(projectPath) {
|
|
105
|
+
const file = policyFile(projectPath);
|
|
106
|
+
if (!fs.existsSync(file)) {
|
|
107
|
+
return DEFAULT_POLICY;
|
|
108
|
+
}
|
|
109
|
+
let parsed;
|
|
110
|
+
try {
|
|
111
|
+
parsed = JSON.parse(fs.readFileSync(file, "utf8"));
|
|
112
|
+
} catch (error) {
|
|
113
|
+
throw new ValidationError(
|
|
114
|
+
`Invalid trust policy: ${file} is not valid JSON (${error.message}).`
|
|
115
|
+
);
|
|
116
|
+
}
|
|
117
|
+
return validatePolicy(parsed);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
// Match a single pattern against the command string. "^"-prefixed patterns are
|
|
121
|
+
// anchored regexes; everything else is a case-sensitive substring match. A
|
|
122
|
+
// malformed regex pattern is treated as a non-match so classify never crashes.
|
|
123
|
+
function matchesPattern(pattern, command) {
|
|
124
|
+
if (typeof pattern !== "string" || pattern.length === 0) {
|
|
125
|
+
return false;
|
|
126
|
+
}
|
|
127
|
+
if (pattern.startsWith("^")) {
|
|
128
|
+
try {
|
|
129
|
+
return new RegExp(pattern).test(command);
|
|
130
|
+
} catch (_error) {
|
|
131
|
+
return false;
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
return command.includes(pattern);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
function firstMatch(patterns, command) {
|
|
138
|
+
if (!Array.isArray(patterns)) {
|
|
139
|
+
return null;
|
|
140
|
+
}
|
|
141
|
+
for (const pattern of patterns) {
|
|
142
|
+
if (matchesPattern(pattern, command)) {
|
|
143
|
+
return pattern;
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
return null;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
// Classify a command as allow/ask/deny against a policy. Most-restrictive-wins,
|
|
150
|
+
// deterministic. Stateless: the caller supplies opts.actionCount; trust-policy
|
|
151
|
+
// performs no session counting. Never executes the command.
|
|
152
|
+
function classifyCommand(policy, command, opts) {
|
|
153
|
+
const cmd = typeof command === "string" ? command : "";
|
|
154
|
+
const options = opts && typeof opts === "object" ? opts : {};
|
|
155
|
+
const limits = policy && policy.limits && typeof policy.limits === "object" ? policy.limits : {};
|
|
156
|
+
|
|
157
|
+
// 1. action-count cap.
|
|
158
|
+
if (
|
|
159
|
+
typeof options.actionCount === "number" &&
|
|
160
|
+
typeof limits.max_actions === "number" &&
|
|
161
|
+
options.actionCount >= limits.max_actions
|
|
162
|
+
) {
|
|
163
|
+
return {
|
|
164
|
+
verdict: "deny",
|
|
165
|
+
reason: `action-count cap reached (${options.actionCount}/${limits.max_actions})`,
|
|
166
|
+
matchedRule: { bucket: "limit", pattern: "limits.max_actions" },
|
|
167
|
+
};
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
// 2. deny → 3. ask → 4. allow (first match wins per bucket).
|
|
171
|
+
const denyHit = firstMatch(policy && policy.deny, cmd);
|
|
172
|
+
if (denyHit) {
|
|
173
|
+
return {
|
|
174
|
+
verdict: "deny",
|
|
175
|
+
reason: `denied by policy rule: ${denyHit}`,
|
|
176
|
+
matchedRule: { bucket: "deny", pattern: denyHit },
|
|
177
|
+
};
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
const askHit = firstMatch(policy && policy.ask, cmd);
|
|
181
|
+
if (askHit) {
|
|
182
|
+
return {
|
|
183
|
+
verdict: "ask",
|
|
184
|
+
reason: `requires confirmation by policy rule: ${askHit}`,
|
|
185
|
+
matchedRule: { bucket: "ask", pattern: askHit },
|
|
186
|
+
};
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
const allowHit = firstMatch(policy && policy.allow, cmd);
|
|
190
|
+
if (allowHit) {
|
|
191
|
+
return {
|
|
192
|
+
verdict: "allow",
|
|
193
|
+
reason: `allowed by policy rule: ${allowHit}`,
|
|
194
|
+
matchedRule: { bucket: "allow", pattern: allowHit },
|
|
195
|
+
};
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
// 5. no match → conservative ask.
|
|
199
|
+
return {
|
|
200
|
+
verdict: "ask",
|
|
201
|
+
reason: "no matching allow rule; defaulting to ask (conservative)",
|
|
202
|
+
matchedRule: null,
|
|
203
|
+
};
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
module.exports = {
|
|
207
|
+
DEFAULT_POLICY,
|
|
208
|
+
loadPolicy,
|
|
209
|
+
classifyCommand,
|
|
210
|
+
validatePolicy,
|
|
211
|
+
policyFile,
|
|
212
|
+
};
|