@antoneeo/kb-agentic-skill 1.4.7 → 1.5.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/scripts/lib.js CHANGED
@@ -1,202 +1,409 @@
1
- // Shared helpers for the KB Agentic npm scripts (init / postinstall / preuninstall).
2
- // Single source for client detection and skill-target paths.
3
-
4
- const fs = require('fs');
5
- const path = require('path');
6
- const os = require('os');
7
- const { execSync } = require('child_process');
8
-
9
- const PACKAGE_ROOT = path.resolve(__dirname, '..');
10
- const SKILL_SOURCE = path.join(PACKAGE_ROOT, 'skills', 'kb-agentic-skill');
11
- const TEMPLATES_PATH = path.join(SKILL_SOURCE, 'templates.md');
12
- // The directory name each client loads the skill from. Derived from the manifest,
13
- // never hard-coded by a consumer: three distributions share these scripts, and a
14
- // literal here is how a copy-fork starts installing under its sibling's name.
15
- const INSTALLED_SKILL_NAME = (() => {
16
- const m = fs.readFileSync(path.join(SKILL_SOURCE, 'SKILL.md'), 'utf8').match(/^name:\s*(\S+)/m);
17
- if (!m) throw new Error(`SKILL.md carries no 'name:' field: ${SKILL_SOURCE}`);
18
- return m[1];
19
- })();
20
-
21
- // The family's lens table, read from the shared `routing.md` rather than restated
22
- // here. Same reason as INSTALLED_SKILL_NAME above, and the same failure it prevents:
23
- // BOTH the row for this lens and the rows for its siblings used to be literals copied
24
- // between distributions, so kb and mkt wrote a multi-lens note announcing themselves
25
- // as the code lens. routing.md is one of the byte-identical shared files, so this
26
- // lookup cannot drift between distributions.
27
- // Lazy on purpose: preuninstall.js needs INSTALLED_SKILL_NAME and nothing else, and
28
- // must keep working on an installation damaged badly enough to have lost routing.md.
29
- function lensTable() {
30
- const routing = path.join(SKILL_SOURCE, 'routing.md');
31
- if (!fs.existsSync(routing)) throw new Error(`routing.md missing from the skill: ${SKILL_SOURCE}`);
32
- // Parsed as a table, not matched with a regex: a mis-escaped pattern matches the
33
- // empty string and yields undefined instead of throwing, which is how this lookup
34
- // failed the first time it was written.
35
- const table = new Map();
36
- for (const line of fs.readFileSync(routing, 'utf8').split(/\r?\n/)) {
37
- const cells = line.split('|').map((cell) => cell.trim());
38
- if (cells.length < 4) continue;
39
- const [, lens, skill] = cells;
40
- if (!/^[a-z]+$/.test(lens) || !/^`[a-z0-9-]+`$/.test(skill)) continue;
41
- const name = skill.slice(1, -1);
42
- // A duplicate row would otherwise be won silently by whichever came first, and a
43
- // wrong routing.md propagates byte-identically to every distribution.
44
- if (table.has(name)) throw new Error(`routing.md lists '${name}' more than once`);
45
- table.set(name, lens);
46
- }
47
- if (!table.size) throw new Error(`routing.md carries no lens table: ${routing}`);
48
- return table;
49
- }
50
-
51
- function selfLens() {
52
- const lens = lensTable().get(INSTALLED_SKILL_NAME);
53
- if (!lens) throw new Error(`routing.md has no lens row for '${INSTALLED_SKILL_NAME}'`);
54
- return lens;
55
- }
56
-
57
- // Sibling lenses of the same family: one shared core, one docs tree, a different
58
- // fidelity discipline each. Keyed by the installed skill directory name.
59
- function siblingLenses() {
60
- const table = lensTable();
61
- selfLens(); // this lens must be in the table too
62
- table.delete(INSTALLED_SKILL_NAME);
63
- return Object.fromEntries(table);
64
- }
65
-
66
- // One entry per supported AI client. `home` may be overridden by an env var
67
- // (Claude Desktop / portable installs); presence of the home dir counts as
68
- // detection even when the CLI is not on PATH.
69
-
70
- const CLIENTS = [
71
- {
72
- key: 'claude',
73
- label: 'Claude Code',
74
- cmd: 'claude',
75
- home: process.env.CLAUDE_CONFIG_DIR || path.join(os.homedir(), '.claude'),
76
- envVar: 'CLAUDE_CONFIG_DIR',
77
- reload: 'Restart Claude Code to load it. Invoke via Skill tool as "kb-agentic".',
78
- },
79
- {
80
- key: 'gemini',
81
- label: 'Gemini CLI',
82
- cmd: 'gemini',
83
- home: process.env.GEMINI_HOME || path.join(os.homedir(), '.gemini'),
84
- envVar: 'GEMINI_HOME',
85
- reload: 'Run "gemini skills reload" or restart Gemini CLI to load it.',
86
- },
87
- {
88
- key: 'codex',
89
- label: 'Codex AI',
90
- cmd: 'codex',
91
- home: process.env.CODEX_HOME || path.join(os.homedir(), '.codex'),
92
- envVar: 'CODEX_HOME',
93
- reload: 'Restart Codex to load it. Invoke it as "$kb-agentic" or by asking for KB Agentic.',
94
- },
95
- {
96
- key: 'antigravity',
97
- label: 'Google Antigravity',
98
- cmd: 'agy',
99
- home: process.env.ANTIGRAVITY_HOME || path.join(os.homedir(), '.gemini'),
100
- envVar: 'ANTIGRAVITY_HOME',
101
- skillsSubdir: 'config/skills',
102
- homeMarker: path.join(
103
- process.env.ANTIGRAVITY_HOME || path.join(os.homedir(), '.gemini'),
104
- 'config',
105
- 'skills',
106
- ),
107
- reload: 'Restart Antigravity, or run "agy skills reload", to load it. Invoke by asking for KB Agentic.',
108
- },
109
- ];
110
-
111
- function commandExists(cmd) {
112
- try {
113
- execSync(`${cmd} --version`, { stdio: 'ignore' });
114
- return true;
115
- } catch (e) {
116
- return false;
117
- }
118
- }
119
-
120
- function clientDetected(client) {
121
- const homePathToCheck = client.homeMarker || client.home;
122
- return commandExists(client.cmd)
123
- || Boolean(process.env[client.envVar])
124
- || fs.existsSync(homePathToCheck);
125
- }
126
-
127
- function skillTarget(client) {
128
- const subdir = client.skillsSubdir ? client.skillsSubdir.split('/') : ['skills'];
129
- return path.join(client.home, ...subdir, INSTALLED_SKILL_NAME);
130
- }
131
-
132
- function copyRecursive(src, dest) {
133
- if (typeof fs.cpSync === 'function') {
134
- fs.cpSync(src, dest, { recursive: true, force: true });
135
- return;
136
- }
137
- if (!fs.existsSync(dest)) fs.mkdirSync(dest, { recursive: true });
138
- for (const entry of fs.readdirSync(src, { withFileTypes: true })) {
139
- const s = path.join(src, entry.name);
140
- const d = path.join(dest, entry.name);
141
- if (entry.isDirectory()) copyRecursive(s, d);
142
- else fs.copyFileSync(s, d);
143
- }
144
- }
145
-
146
- function loadTemplates() {
147
- const text = fs.readFileSync(TEMPLATES_PATH, 'utf8');
148
- const lines = text.split(/\r?\n/);
149
- const sections = {};
150
- let heading = null;
151
- let block = null;
152
- for (const line of lines) {
153
- const h = line.match(/^##\s+(.*)$/);
154
- if (h && block === null) {
155
- heading = h[1].trim();
156
- sections[heading] = sections[heading] || [];
157
- continue;
158
- }
159
- if (/^```/.test(line)) {
160
- if (block === null) {
161
- block = [];
162
- } else {
163
- if (heading) sections[heading].push(block.join('\n') + '\n');
164
- block = null;
165
- }
166
- continue;
167
- }
168
- if (block !== null) block.push(line);
169
- }
170
- return sections;
171
- }
172
-
173
- function templateFor(sections, needle, index = 0) {
174
- const heading = Object.keys(sections).find((h) => h.includes(needle));
175
- const blocks = heading ? sections[heading] : undefined;
176
- if (!blocks || !blocks[index]) {
177
- throw new Error(
178
- `Template section containing "${needle}" (block ${index}) not found in ${TEMPLATES_PATH}. ` +
179
- 'The package is corrupted or templates.md was restructured: fix templates.md, do not improvise content.'
180
- );
181
- }
182
- return blocks[index];
183
- }
184
-
185
- module.exports = {
186
- PACKAGE_ROOT,
187
- SKILL_SOURCE,
188
- INSTALLED_SKILL_NAME,
189
- TEMPLATES_PATH,
190
- CLIENTS,
191
- commandExists,
192
- clientDetected,
193
- skillTarget,
194
- copyRecursive,
195
- loadTemplates,
196
- templateFor,
197
- };
198
-
199
- // Lazy: reading routing.md is deferred to the consumer that actually asks.
200
- Object.defineProperty(module.exports, 'SELF_LENS', { enumerable: true, get: selfLens });
201
- Object.defineProperty(module.exports, 'SIBLING_LENSES', { enumerable: true, get: siblingLenses });
202
-
1
+ // Shared helpers for the KB Agentic npm scripts (init / postinstall / preuninstall).
2
+ // Single source for client detection and skill-target paths.
3
+
4
+ const fs = require('fs');
5
+ const path = require('path');
6
+ const os = require('os');
7
+ const { execSync } = require('child_process');
8
+
9
+ const PACKAGE_ROOT = path.resolve(__dirname, '..');
10
+ const SKILL_SOURCE = path.join(PACKAGE_ROOT, 'skills', 'kb-agentic-skill');
11
+ const TEMPLATES_PATH = path.join(SKILL_SOURCE, 'templates.md');
12
+ // The directory name each client loads the skill from. Derived from the manifest,
13
+ // never hard-coded by a consumer: three distributions share these scripts, and a
14
+ // literal here is how a copy-fork starts installing under its sibling's name.
15
+ const INSTALLED_SKILL_NAME = (() => {
16
+ const m = fs.readFileSync(path.join(SKILL_SOURCE, 'SKILL.md'), 'utf8').match(/^name:\s*(\S+)/m);
17
+ if (!m) throw new Error(`SKILL.md carries no 'name:' field: ${SKILL_SOURCE}`);
18
+ return m[1];
19
+ })();
20
+
21
+ // The family's lens table, read from the shared `routing.md` rather than restated
22
+ // here. Same reason as INSTALLED_SKILL_NAME above, and the same failure it prevents:
23
+ // BOTH the row for this lens and the rows for its siblings used to be literals copied
24
+ // between distributions, so kb and mkt wrote a multi-lens note announcing themselves
25
+ // as the code lens. routing.md is one of the byte-identical shared files, so this
26
+ // lookup cannot drift between distributions.
27
+ // Lazy on purpose: preuninstall.js needs INSTALLED_SKILL_NAME and nothing else, and
28
+ // must keep working on an installation damaged badly enough to have lost routing.md.
29
+ function lensTable() {
30
+ const routing = path.join(SKILL_SOURCE, 'routing.md');
31
+ if (!fs.existsSync(routing)) throw new Error(`routing.md missing from the skill: ${SKILL_SOURCE}`);
32
+ // Parsed as a table, not matched with a regex: a mis-escaped pattern matches the
33
+ // empty string and yields undefined instead of throwing, which is how this lookup
34
+ // failed the first time it was written.
35
+ const table = new Map();
36
+ for (const line of fs.readFileSync(routing, 'utf8').split(/\r?\n/)) {
37
+ const cells = line.split('|').map((cell) => cell.trim());
38
+ if (cells.length < 4) continue;
39
+ const [, lens, skill] = cells;
40
+ if (!/^[a-z]+$/.test(lens) || !/^`[a-z0-9-]+`$/.test(skill)) continue;
41
+ const name = skill.slice(1, -1);
42
+ // A duplicate row would otherwise be won silently by whichever came first, and a
43
+ // wrong routing.md propagates byte-identically to every distribution.
44
+ if (table.has(name)) throw new Error(`routing.md lists '${name}' more than once`);
45
+ table.set(name, lens);
46
+ }
47
+ if (!table.size) throw new Error(`routing.md carries no lens table: ${routing}`);
48
+ return table;
49
+ }
50
+
51
+ function selfLens() {
52
+ const lens = lensTable().get(INSTALLED_SKILL_NAME);
53
+ if (!lens) throw new Error(`routing.md has no lens row for '${INSTALLED_SKILL_NAME}'`);
54
+ return lens;
55
+ }
56
+
57
+ // Sibling lenses of the same family: one shared core, one docs tree, a different
58
+ // fidelity discipline each. Keyed by the installed skill directory name.
59
+ function siblingLenses() {
60
+ const table = lensTable();
61
+ selfLens(); // this lens must be in the table too
62
+ table.delete(INSTALLED_SKILL_NAME);
63
+ return Object.fromEntries(table);
64
+ }
65
+
66
+ // One entry per supported AI client. `home` may be overridden by an env var
67
+ // (Claude Desktop / portable installs); presence of the home dir counts as
68
+ // detection even when the CLI is not on PATH.
69
+
70
+ const CLIENTS = [
71
+ {
72
+ key: 'claude',
73
+ label: 'Claude Code',
74
+ cmd: 'claude',
75
+ home: process.env.CLAUDE_CONFIG_DIR || path.join(os.homedir(), '.claude'),
76
+ envVar: 'CLAUDE_CONFIG_DIR',
77
+ reload: 'Restart Claude Code to load it. Invoke via Skill tool as "kb-agentic".',
78
+ },
79
+ {
80
+ key: 'gemini',
81
+ label: 'Gemini CLI',
82
+ cmd: 'gemini',
83
+ home: process.env.GEMINI_HOME || path.join(os.homedir(), '.gemini'),
84
+ envVar: 'GEMINI_HOME',
85
+ reload: 'Run "gemini skills reload" or restart Gemini CLI to load it.',
86
+ },
87
+ {
88
+ key: 'codex',
89
+ label: 'Codex AI',
90
+ cmd: 'codex',
91
+ home: process.env.CODEX_HOME || path.join(os.homedir(), '.codex'),
92
+ envVar: 'CODEX_HOME',
93
+ reload: 'Restart Codex to load it. Invoke it as "$kb-agentic" or by asking for KB Agentic.',
94
+ },
95
+ {
96
+ key: 'antigravity',
97
+ label: 'Google Antigravity',
98
+ cmd: 'agy',
99
+ home: process.env.ANTIGRAVITY_HOME || path.join(os.homedir(), '.gemini'),
100
+ envVar: 'ANTIGRAVITY_HOME',
101
+ skillsSubdir: 'config/skills',
102
+ homeMarker: path.join(
103
+ process.env.ANTIGRAVITY_HOME || path.join(os.homedir(), '.gemini'),
104
+ 'config',
105
+ 'skills',
106
+ ),
107
+ reload: 'Restart Antigravity, or run "agy skills reload", to load it. Invoke by asking for KB Agentic.',
108
+ },
109
+ ];
110
+
111
+ function commandExists(cmd) {
112
+ try {
113
+ execSync(`${cmd} --version`, { stdio: 'ignore' });
114
+ return true;
115
+ } catch (e) {
116
+ return false;
117
+ }
118
+ }
119
+
120
+ function clientDetected(client) {
121
+ const homePathToCheck = client.homeMarker || client.home;
122
+ return commandExists(client.cmd)
123
+ || Boolean(process.env[client.envVar])
124
+ || fs.existsSync(homePathToCheck);
125
+ }
126
+
127
+ function skillTarget(client) {
128
+ const subdir = client.skillsSubdir ? client.skillsSubdir.split('/') : ['skills'];
129
+ return path.join(client.home, ...subdir, INSTALLED_SKILL_NAME);
130
+ }
131
+
132
+ function copyRecursive(src, dest) {
133
+ if (typeof fs.cpSync === 'function') {
134
+ fs.cpSync(src, dest, { recursive: true, force: true });
135
+ return;
136
+ }
137
+ if (!fs.existsSync(dest)) fs.mkdirSync(dest, { recursive: true });
138
+ for (const entry of fs.readdirSync(src, { withFileTypes: true })) {
139
+ const s = path.join(src, entry.name);
140
+ const d = path.join(dest, entry.name);
141
+ if (entry.isDirectory()) copyRecursive(s, d);
142
+ else fs.copyFileSync(s, d);
143
+ }
144
+ }
145
+
146
+ function loadTemplates() {
147
+ const text = fs.readFileSync(TEMPLATES_PATH, 'utf8');
148
+ const lines = text.split(/\r?\n/);
149
+ const sections = {};
150
+ let heading = null;
151
+ let block = null;
152
+ for (const line of lines) {
153
+ const h = line.match(/^##\s+(.*)$/);
154
+ if (h && block === null) {
155
+ heading = h[1].trim();
156
+ sections[heading] = sections[heading] || [];
157
+ continue;
158
+ }
159
+ if (/^```/.test(line)) {
160
+ if (block === null) {
161
+ block = [];
162
+ } else {
163
+ if (heading) sections[heading].push(block.join('\n') + '\n');
164
+ block = null;
165
+ }
166
+ continue;
167
+ }
168
+ if (block !== null) block.push(line);
169
+ }
170
+ return sections;
171
+ }
172
+
173
+ function templateFor(sections, needle, index = 0) {
174
+ const heading = Object.keys(sections).find((h) => h.includes(needle));
175
+ const blocks = heading ? sections[heading] : undefined;
176
+ if (!blocks || !blocks[index]) {
177
+ throw new Error(
178
+ `Template section containing "${needle}" (block ${index}) not found in ${TEMPLATES_PATH}. ` +
179
+ 'The package is corrupted or templates.md was restructured: fix templates.md, do not improvise content.'
180
+ );
181
+ }
182
+ return blocks[index];
183
+ }
184
+
185
+ // --- SessionStart orientation hook (F-036) ---------------------------------
186
+ // ENFORCEMENT.md §4 says to wire this on every project that has a docs root and
187
+ // a Python interpreter. Until F-036 nothing did: it was a manual step, so it was
188
+ // skipped, and the guide router reached no agent that did not already know to
189
+ // look for it. The hook only INJECTS orientation -- it cannot force a skill
190
+ // invocation, and the mechanism that could (a blocking PreToolUse gate) is
191
+ // refused by the Vision's no-ceremony-ratchet Non-Goal.
192
+
193
+ const ORIENT_HOOK_TIMEOUT = 10;
194
+
195
+ // The validator's filename is DERIVED, never written here: the marketing lens
196
+ // ships `mkt_check.py` where the others ship `sdlc_check.py`, and this block is
197
+ // copied verbatim into all three distributions. A literal would make two of the
198
+ // three look for an entry point that does not exist.
199
+ const ORIENT_ENTRY_POINTS = ['sdlc_check.py', 'mkt_check.py'];
200
+ const ORIENT_ENTRY_POINT = (() => {
201
+ for (const name of ORIENT_ENTRY_POINTS) {
202
+ if (fs.existsSync(path.join(SKILL_SOURCE, 'scripts', name))) return name;
203
+ }
204
+ return ORIENT_ENTRY_POINTS[0];
205
+ })();
206
+
207
+ // Both settings layers are ALWAYS inspected for an existing hook, whichever one
208
+ // we would write to. Scanning only the target file left two holes: a project
209
+ // that starts un-vendored and later vendors flips the target from the local file
210
+ // to the shared one, finds nothing there, and appends a second hook; and a dead
211
+ // hook in the layer we are not writing stays invisible.
212
+ const ORIENT_SETTINGS_FILES = ['settings.json', 'settings.local.json'];
213
+
214
+ // A vendored path is built from a directory name read out of the TARGET
215
+ // repository, and the result is executed by the client as a shell command. That
216
+ // is repo-controlled input reaching a command line, so each segment must match
217
+ // this exactly -- `$(...)`, backticks and newlines are not "unusual names", they
218
+ // are the payload. The absolute branch cannot reach this input (it is built from
219
+ // os.homedir() and the client roster) but is filtered too, below.
220
+ const ORIENT_SAFE_SEGMENT = /^[A-Za-z0-9._-]+$/;
221
+ // Refused anywhere in a command path. NOT backslash: a Windows path is full of
222
+ // them and the shape this project already commits uses them.
223
+ const ORIENT_UNSAFE_IN_PATH = /["$`\r\n]/;
224
+
225
+ // Where a repo may legitimately vendor the validator. ENFORCEMENT §2 tells users
226
+ // to copy it next to their CI config (`tools/`), and a skill-authoring repo
227
+ // carries it under `skills/<lens>/scripts/`. Both are checked; the lens matching
228
+ // this distribution wins, so a monorepo vendoring several lenses cannot wire a
229
+ // sibling's validator against this lens's docs root.
230
+ function vendoredValidator(cwd) {
231
+ const direct = [];
232
+ for (const dir of ['tools', 'scripts']) {
233
+ direct.push([dir, ORIENT_ENTRY_POINT].join('/'));
234
+ }
235
+ const skillDirs = (() => {
236
+ let entries;
237
+ try {
238
+ entries = fs.readdirSync(path.join(cwd, 'skills'), { withFileTypes: true });
239
+ } catch (e) {
240
+ return []; // no skills/ here, or it is not a directory
241
+ }
242
+ const names = entries.filter((e) => e.isDirectory()).map((e) => e.name)
243
+ .filter((n) => ORIENT_SAFE_SEGMENT.test(n));
244
+ // This lens first: readdir order must not decide which lens we orient.
245
+ names.sort((a, b) => (a === INSTALLED_SKILL_NAME ? -1 : 0)
246
+ - (b === INSTALLED_SKILL_NAME ? -1 : 0));
247
+ return names.map((n) => ['skills', n, 'scripts', ORIENT_ENTRY_POINT].join('/'));
248
+ })();
249
+ for (const rel of direct.concat(skillDirs)) {
250
+ if (fs.existsSync(path.join(cwd, rel))) return rel;
251
+ }
252
+ return null;
253
+ }
254
+
255
+ function orientHookCommand(python, validator, hybrid) {
256
+ return python + ' "' + validator + '" orient' + (hybrid ? ' --hybrid' : '');
257
+ }
258
+
259
+ // The token in an EXISTING command that names a validator -- quoted or bare, and
260
+ // NOT simply "the first quoted thing". That naive rule failed both ways: a
261
+ // command that quotes the interpreter (`"C:\Py\python.exe" "...sdlc_check.py"`)
262
+ // returned the interpreter, which exists, so a DEAD hook reported as healthy;
263
+ // and a command with no quotes at all returned nothing, so a WORKING hook was
264
+ // reported broken. Returns null when no token names a validator: the caller must
265
+ // then say it cannot tell, never that the hook is broken.
266
+ function orientHookValidator(command) {
267
+ const cmd = String(command || '');
268
+ const re = /"([^"]*)"|(\S+)/g;
269
+ let m;
270
+ while ((m = re.exec(cmd)) !== null) {
271
+ const token = m[1] !== undefined ? m[1] : m[2];
272
+ if (ORIENT_ENTRY_POINTS.some((n) => token.endsWith(n))) return token;
273
+ }
274
+ return null;
275
+ }
276
+
277
+ // Accepts both shapes seen in the wild: the documented groups
278
+ // (`SessionStart: [{ hooks: [...] }]`) and hook objects placed directly in the
279
+ // array. Anything else is reported by the caller rather than silently replaced.
280
+ function findOrientHook(settings) {
281
+ const groups = settings && settings.hooks && settings.hooks.SessionStart;
282
+ if (!Array.isArray(groups)) return null;
283
+ for (const group of groups) {
284
+ if (!group || typeof group !== 'object') continue;
285
+ const inner = Array.isArray(group.hooks) ? group.hooks : [group];
286
+ for (const h of inner) {
287
+ const cmd = h && typeof h.command === 'string' ? h.command : '';
288
+ if (ORIENT_ENTRY_POINTS.some((n) => cmd.includes(n)) && / orient(\s|$)/.test(cmd)) {
289
+ return cmd;
290
+ }
291
+ }
292
+ }
293
+ return null;
294
+ }
295
+
296
+ // A settings object we can safely merge into: absent, or carrying the shapes we
297
+ // know. "I do not recognise this" must mean "write nothing", never "treat it as
298
+ // empty" -- the second is how a hand-written hook disappears.
299
+ function orientSettingsState(target) {
300
+ if (!fs.existsSync(target)) return { ok: true, settings: {} };
301
+ let settings;
302
+ try {
303
+ settings = JSON.parse(fs.readFileSync(target, 'utf8'));
304
+ } catch (e) {
305
+ return { ok: false, why: 'unreadable' };
306
+ }
307
+ if (!settings || typeof settings !== 'object' || Array.isArray(settings)) {
308
+ return { ok: false, why: 'not-an-object' };
309
+ }
310
+ const hooks = settings.hooks;
311
+ if (hooks !== undefined
312
+ && (!hooks || typeof hooks !== 'object' || Array.isArray(hooks))) {
313
+ return { ok: false, why: 'unexpected-hooks' };
314
+ }
315
+ if (hooks && hooks.SessionStart !== undefined && !Array.isArray(hooks.SessionStart)) {
316
+ return { ok: false, why: 'unexpected-hooks' };
317
+ }
318
+ return { ok: true, settings };
319
+ }
320
+
321
+ // Returns a RESULT CODE, never printed text: the caller owns the wording and the
322
+ // battery asserts on the code. Codes:
323
+ // wired | already | broken | unverifiable | malformed
324
+ // no-validator | no-python | unsafe-path | write-failed
325
+ function wireOrientHook(options) {
326
+ const cwd = options.cwd;
327
+ const client = options.client;
328
+ const python = options.python;
329
+ const hybrid = !!options.hybrid;
330
+ const docsLabel = options.docsLabel || 'project';
331
+
332
+ const vendored = vendoredValidator(cwd);
333
+ const absolute = path.join(skillTarget(client), 'scripts', ORIENT_ENTRY_POINT);
334
+ const validator = vendored || absolute;
335
+ if (!vendored && !fs.existsSync(absolute)) return { code: 'no-validator', validator };
336
+ if (!python) return { code: 'no-python', validator };
337
+ if (ORIENT_UNSAFE_IN_PATH.test(validator)) return { code: 'unsafe-path', validator };
338
+
339
+ const command = orientHookCommand(python, validator, hybrid);
340
+
341
+ // Inspect BOTH layers before deciding to write anything.
342
+ for (const name of ORIENT_SETTINGS_FILES) {
343
+ const p = path.join(cwd, '.claude', name);
344
+ const state = orientSettingsState(p);
345
+ if (!state.ok) {
346
+ if (fs.existsSync(p)) return { code: 'malformed', file: name, target: p, why: state.why, command };
347
+ continue;
348
+ }
349
+ const existing = findOrientHook(state.settings);
350
+ if (!existing) continue;
351
+ const found = orientHookValidator(existing);
352
+ if (found === null) {
353
+ return { code: 'unverifiable', file: name, target: p, existing, command };
354
+ }
355
+ const resolves = fs.existsSync(path.isAbsolute(found) ? found : path.join(cwd, found));
356
+ return resolves
357
+ ? { code: 'already', file: name, target: p, command: existing }
358
+ : { code: 'broken', file: name, target: p, existing, command };
359
+ }
360
+
361
+ // A machine-specific command must not reach the shared, committed file.
362
+ const file = vendored ? 'settings.json' : 'settings.local.json';
363
+ const target = path.join(cwd, '.claude', file);
364
+ const state = orientSettingsState(target);
365
+ if (!state.ok) return { code: 'malformed', file, target, why: state.why, command };
366
+ const settings = state.settings;
367
+
368
+ if (!settings.hooks) settings.hooks = {};
369
+ if (!Array.isArray(settings.hooks.SessionStart)) settings.hooks.SessionStart = [];
370
+ settings.hooks.SessionStart.push({
371
+ hooks: [{
372
+ type: 'command',
373
+ command,
374
+ timeout: ORIENT_HOOK_TIMEOUT,
375
+ statusMessage: 'Loading ' + docsLabel + ' orientation...',
376
+ }],
377
+ });
378
+
379
+ // The installer has already written seed files by this point; an unwritable
380
+ // settings file must not take the whole run down with a stack trace.
381
+ try {
382
+ fs.mkdirSync(path.dirname(target), { recursive: true });
383
+ fs.writeFileSync(target, JSON.stringify(settings, null, 2) + String.fromCharCode(10), 'utf8');
384
+ } catch (e) {
385
+ return { code: 'write-failed', file, target, command, error: e.message };
386
+ }
387
+ return { code: 'wired', file, target, command, local: !vendored };
388
+ }
389
+
390
+ module.exports = {
391
+ PACKAGE_ROOT,
392
+ SKILL_SOURCE,
393
+ INSTALLED_SKILL_NAME,
394
+ TEMPLATES_PATH,
395
+ CLIENTS,
396
+ commandExists,
397
+ clientDetected,
398
+ skillTarget,
399
+ wireOrientHook,
400
+ orientHookCommand,
401
+ copyRecursive,
402
+ loadTemplates,
403
+ templateFor,
404
+ };
405
+
406
+ // Lazy: reading routing.md is deferred to the consumer that actually asks.
407
+ Object.defineProperty(module.exports, 'SELF_LENS', { enumerable: true, get: selfLens });
408
+ Object.defineProperty(module.exports, 'SIBLING_LENSES', { enumerable: true, get: siblingLenses });
409
+
@@ -48,7 +48,7 @@ Blocks Edit/Write on protected paths when no `ANALYSIS_*.md` is `IN_PROGRESS`. I
48
48
  "hooks": [
49
49
  {
50
50
  "type": "command",
51
- "command": "python \"C:\\Users\\<user>\\.claude\\skills\\agentic-sdlc\\scripts\\sdlc_check.py\" gate --hook --protected \"src/auth;src/crypto\""
51
+ "command": "python \"C:\\Users\\<user>\\.claude\\skills\\kb-agentic\\scripts\\sdlc_check.py\" gate --hook --protected \"src/auth;src/crypto\""
52
52
  }
53
53
  ]
54
54
  }
@@ -72,6 +72,37 @@ Emits the `ai_docs/` orientation — reading guide (`README.md`), manifest (`IND
72
72
 
73
73
  **Wire it on every project that has `ai_docs/` and a Python interpreter.** It was opt-in until v1.16.0 and the field result was the defect this level exists to prevent: the guide router stayed unread unless the user asked for it by hand, so guides were written and never consulted. Prompt-level placement (Rule Zero declares the router verdict; Phase 1 reads the router) carries the process on its own — this hook is the backstop that survives long contexts, compaction and a session that never enters Phase 1 explicitly. Skip it only where Python is unavailable, and know what you are trading.
74
74
 
75
+ **`init` wires it for you (F-036).** Running `init` on a project that has a docs root
76
+ and a Python interpreter now installs this hook itself — it was a manual step until
77
+ 2026-08-25, and the field result was exactly the defect this level exists to prevent: an
78
+ agent worked a governed project without ever meeting the router. The snippet below is
79
+ the fallback for the cases init declines, and the list is exhaustive: **any client
80
+ other than Claude Code** (Codex and Gemini keep the manual snippet — no fixture in the
81
+ repository pins their hook schema, and writing one in an unverified shape is how a
82
+ wired-but-dead hook is born), no Python, the skill not installed yet, a settings file
83
+ that is not valid JSON or carries a `hooks`/`SessionStart` shape the writer does not
84
+ recognise (it never rewrites one it cannot read), a validator path holding a character
85
+ that cannot be placed in a command safely, or a settings file it cannot write. `init`
86
+ prints which case applied, per client — a silent skip is what let "documented default
87
+ that nobody installs" survive in the first place.
88
+
89
+ **Which settings file, and why it is not always the shared one.** The command names a
90
+ validator, and where that validator lives decides where the hook may be written:
91
+
92
+ | Case | Command | File |
93
+ |---|---|---|
94
+ | The repo vendors the validator (§2) | repo-relative | `.claude/settings.json` — portable, commit it |
95
+ | The validator is only in your skills directory (the normal case) | absolute | `.claude/settings.local.json` — machine-specific, git-ignored; each teammate runs `init` once |
96
+
97
+ Committing an absolute `C:\Users\<you>\...` path into the shared file hands every teammate a hook naming
98
+ a directory they do not have. `init` picks the file for you, and adds
99
+ `.claude/settings.local.json` to `.gitignore` when it uses the local one.
100
+
101
+ **A hook that is wired and DEAD is the worst of the three states**: it emits nothing at
102
+ every session AND it looks installed. `init` checks that an existing hook's validator
103
+ still resolves and reports it as BROKEN with the corrected command, rather than
104
+ reporting "already wired". It never rewrites the entry — it may be hand-tuned.
105
+
75
106
  Wire it via each client's SessionStart mechanism — the same command everywhere (add `--hybrid` on devPNT/Hybrid projects):
76
107
 
77
108
  Claude Code — in the project's `.claude/settings.json`:
@@ -84,7 +115,7 @@ Claude Code — in the project's `.claude/settings.json`:
84
115
  "hooks": [
85
116
  {
86
117
  "type": "command",
87
- "command": "python \"C:\\Users\\<user>\\.claude\\skills\\agentic-sdlc\\scripts\\sdlc_check.py\" orient"
118
+ "command": "python \"C:\\Users\\<user>\\.claude\\skills\\kb-agentic\\scripts\\sdlc_check.py\" orient"
88
119
  }
89
120
  ]
90
121
  }