@saasontools/strauss-kb 0.1.2 → 0.1.3
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 +96 -5
- package/dist/{chunk-WFHYWZX5.js → chunk-FSI4Q2FD.js} +3 -2
- package/dist/chunk-FSI4Q2FD.js.map +1 -0
- package/dist/chunk-HYNAEAPM.js +2323 -0
- package/dist/chunk-HYNAEAPM.js.map +1 -0
- package/dist/{chunk-KGM34MYU.js → chunk-QLTB77W4.js} +3 -2
- package/dist/chunk-QLTB77W4.js.map +1 -0
- package/dist/cli-main.cjs +1357 -567
- package/dist/cli-main.cjs.map +1 -1
- package/dist/cli-main.js +2 -2
- package/dist/index.cjs +1121 -298
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +278 -12
- package/dist/index.d.ts +278 -12
- package/dist/index.js +45 -3
- package/dist/index.js.map +1 -1
- package/dist/mcp-main.cjs +1352 -562
- package/dist/mcp-main.cjs.map +1 -1
- package/dist/mcp-main.js +2 -2
- package/package.json +1 -1
- package/dist/chunk-KGM34MYU.js.map +0 -1
- package/dist/chunk-WFHYWZX5.js.map +0 -1
- package/dist/chunk-ZSYSHJVZ.js +0 -1527
- package/dist/chunk-ZSYSHJVZ.js.map +0 -1
package/dist/cli-main.cjs
CHANGED
|
@@ -24,10 +24,10 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
|
|
|
24
24
|
));
|
|
25
25
|
|
|
26
26
|
// src/cli.ts
|
|
27
|
-
var
|
|
27
|
+
var import_node_path7 = require("path");
|
|
28
28
|
|
|
29
|
-
// src/
|
|
30
|
-
var
|
|
29
|
+
// src/decision-record.ts
|
|
30
|
+
var import_zod3 = require("zod");
|
|
31
31
|
|
|
32
32
|
// src/compose.ts
|
|
33
33
|
var import_zod2 = require("zod");
|
|
@@ -198,6 +198,16 @@ var composeInputSchema = import_zod2.z.object({
|
|
|
198
198
|
sources: import_zod2.z.array(kbSourceSchema).optional(),
|
|
199
199
|
/** No source exists, as a claim rather than a sentinel in `sources`. */
|
|
200
200
|
assumption: import_zod2.z.boolean().optional(),
|
|
201
|
+
/**
|
|
202
|
+
* OKF `stale_after`: the absolute date this record stops being trusted.
|
|
203
|
+
* Anything the outside world can change — pricing, quotas, versions,
|
|
204
|
+
* reception counts — should carry one.
|
|
205
|
+
*/
|
|
206
|
+
stale_after: import_zod2.z.string().regex(/^\d{4}-\d{2}-\d{2}$/, {
|
|
207
|
+
message: "stale_after must be YYYY-MM-DD"
|
|
208
|
+
}).refine((date) => !Number.isNaN(Date.parse(date)), {
|
|
209
|
+
message: "stale_after must be a real date"
|
|
210
|
+
}).optional(),
|
|
201
211
|
verify: import_zod2.z.array(import_zod2.z.string().min(1)).optional(),
|
|
202
212
|
tags: import_zod2.z.array(import_zod2.z.string().min(1)).optional(),
|
|
203
213
|
/** Concept ids this record relates to; rendered as body links. */
|
|
@@ -230,6 +240,7 @@ function composeRecord(type, input, writtenBy, writtenAt) {
|
|
|
230
240
|
verified: [],
|
|
231
241
|
strauss_status: spec.initialStatus
|
|
232
242
|
};
|
|
243
|
+
if (parsed.stale_after) frontmatter.stale_after = parsed.stale_after;
|
|
233
244
|
if (parsed.anchors?.length) frontmatter.strauss_anchors = parsed.anchors;
|
|
234
245
|
if (parsed.verify?.length) frontmatter.strauss_verify = parsed.verify;
|
|
235
246
|
if (parsed.tags?.length) frontmatter.tags = parsed.tags;
|
|
@@ -266,7 +277,6 @@ ${text}`);
|
|
|
266
277
|
}
|
|
267
278
|
|
|
268
279
|
// src/decision-record.ts
|
|
269
|
-
var import_zod3 = require("zod");
|
|
270
280
|
var DECISION_TYPE = "decision";
|
|
271
281
|
var NO_DECISION_SLUG = "none";
|
|
272
282
|
var decisionInputSchema = composeInputSchema.omit({ sections: true }).extend({
|
|
@@ -304,458 +314,1349 @@ function composeNoDecisionRecord(reason, writtenBy, writtenAt) {
|
|
|
304
314
|
);
|
|
305
315
|
}
|
|
306
316
|
|
|
307
|
-
// src/
|
|
308
|
-
var
|
|
317
|
+
// src/commands/answer.ts
|
|
318
|
+
var import_zod6 = require("zod");
|
|
309
319
|
|
|
310
|
-
// src/kb-
|
|
320
|
+
// src/kb-pins/budgets.ts
|
|
321
|
+
function asBudgets(value) {
|
|
322
|
+
if (value === null || typeof value !== "object") return {};
|
|
323
|
+
const table = value;
|
|
324
|
+
const pick = (key, min) => {
|
|
325
|
+
const raw = table[key];
|
|
326
|
+
return typeof raw === "number" && Number.isInteger(raw) && raw >= min ? raw : void 0;
|
|
327
|
+
};
|
|
328
|
+
const budgetTokens = pick("budgetTokens", 1);
|
|
329
|
+
const fullUnderTokens = pick("fullUnderTokens", 0);
|
|
330
|
+
return {
|
|
331
|
+
...budgetTokens ? { budgetTokens } : {},
|
|
332
|
+
...fullUnderTokens !== void 0 ? { fullUnderTokens } : {}
|
|
333
|
+
};
|
|
334
|
+
}
|
|
335
|
+
function contextProfileBudgets(manifest, profile) {
|
|
336
|
+
const table = manifest.context;
|
|
337
|
+
if (table === null || typeof table !== "object") return {};
|
|
338
|
+
const entries = table;
|
|
339
|
+
return {
|
|
340
|
+
...asBudgets(entries["default"]),
|
|
341
|
+
...profile ? asBudgets(entries[profile]) : {}
|
|
342
|
+
};
|
|
343
|
+
}
|
|
344
|
+
function mergedContextBudgets(merged, profile) {
|
|
345
|
+
const layered = ["user", "local", "project"].map((layer) => {
|
|
346
|
+
const manifest = merged.manifests[layer];
|
|
347
|
+
return manifest ? contextProfileBudgets(manifest, profile) : {};
|
|
348
|
+
});
|
|
349
|
+
return { ...layered[0], ...layered[1], ...layered[2] };
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
// src/kb-pins/errors.ts
|
|
353
|
+
var KbPinsMalformedError = class extends Error {
|
|
354
|
+
constructor(file, cause) {
|
|
355
|
+
super(`pin manifest ${file} is not readable (${cause}) \u2014 fix or remove it`);
|
|
356
|
+
this.name = "KbPinsMalformedError";
|
|
357
|
+
}
|
|
358
|
+
};
|
|
359
|
+
var KbBaseFrozenError = class extends Error {
|
|
360
|
+
constructor(bundlePath2, layer) {
|
|
361
|
+
super(
|
|
362
|
+
`${bundlePath2} is frozen (read-only) by this workspace's ${layer} pin manifest \u2014 re-pin with --unfreeze, or unpin, to change it`
|
|
363
|
+
);
|
|
364
|
+
this.name = "KbBaseFrozenError";
|
|
365
|
+
}
|
|
366
|
+
};
|
|
367
|
+
|
|
368
|
+
// src/kb-pins/frozen.ts
|
|
369
|
+
var import_node_path3 = require("path");
|
|
370
|
+
|
|
371
|
+
// src/kb-pins/layers.ts
|
|
372
|
+
var import_promises = require("fs/promises");
|
|
373
|
+
var import_node_os = require("os");
|
|
374
|
+
var import_node_path2 = require("path");
|
|
375
|
+
|
|
376
|
+
// src/kb-pins/model.ts
|
|
377
|
+
var import_node_path = require("path");
|
|
311
378
|
var import_zod4 = require("zod");
|
|
312
|
-
var
|
|
313
|
-
var
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
379
|
+
var PINS_FILE = (0, import_node_path.join)(".strauss", "kb-pins.json");
|
|
380
|
+
var PINS_LOCAL_FILE = (0, import_node_path.join)(".strauss", "kb-pins.local.json");
|
|
381
|
+
var PIN_LAYERS = ["project", "local", "user"];
|
|
382
|
+
var pinSchema = import_zod4.z.object({
|
|
383
|
+
/** Relative to the manifest's root, so the file is committable. */
|
|
384
|
+
path: import_zod4.z.string().min(1),
|
|
385
|
+
pinnedAt: import_zod4.z.string().min(1).optional(),
|
|
386
|
+
/**
|
|
387
|
+
* How `context` renders this base. `full` preloads the whole base into
|
|
388
|
+
* the block regardless of the full-under threshold — for a base whose
|
|
389
|
+
* contents should simply be present, the way an ADR base should be —
|
|
390
|
+
* still answering to the block budget, with an index fallback that says
|
|
391
|
+
* so when it cannot fit. `index` never upgrades, whatever the threshold.
|
|
392
|
+
* Absent: the profile's full-under threshold decides. Invalid values
|
|
393
|
+
* degrade to absent rather than failing the manifest.
|
|
394
|
+
*/
|
|
395
|
+
mode: import_zod4.z.enum(["full", "index"]).optional().catch(void 0),
|
|
396
|
+
/**
|
|
397
|
+
* Context profiles this pin surfaces in (e.g. only at session-start,
|
|
398
|
+
* not per turn). Absent: every profile. A run without a profile sees
|
|
399
|
+
* every pin. A base that only matters to one skill is better loaded by
|
|
400
|
+
* that skill at point of use than pinned at all — pins are what every
|
|
401
|
+
* session should see.
|
|
402
|
+
*/
|
|
403
|
+
profiles: import_zod4.z.array(import_zod4.z.string()).optional().catch(void 0),
|
|
404
|
+
/**
|
|
405
|
+
* The base is concluded — a finished piece of research, a frozen ADR
|
|
406
|
+
* set. Write commands against it refuse while this workspace holds the
|
|
407
|
+
* pin, and `context` labels it read-only. Workspace policy, not base
|
|
408
|
+
* state: the base itself stays copyable and writable elsewhere.
|
|
409
|
+
*/
|
|
410
|
+
frozen: import_zod4.z.boolean().optional().catch(void 0)
|
|
411
|
+
}).passthrough();
|
|
412
|
+
var pinsManifestSchema = import_zod4.z.object({
|
|
413
|
+
pins: import_zod4.z.array(pinSchema).default([]),
|
|
414
|
+
/**
|
|
415
|
+
* Per-repo budgets for the `context` command, keyed by profile —
|
|
416
|
+
* `"session-start"`, `"compact"`, `"turn"`, or `"default"` for all of
|
|
417
|
+
* them. Deliberately untyped here: a typo'd budget must degrade to the
|
|
418
|
+
* built-in default, not make the whole manifest unreadable and silence
|
|
419
|
+
* the index at every session start. `contextProfileBudgets` does the
|
|
420
|
+
* tolerant read.
|
|
421
|
+
*/
|
|
422
|
+
context: import_zod4.z.unknown().optional()
|
|
423
|
+
}).passthrough();
|
|
424
|
+
|
|
425
|
+
// src/kb-pins/layers.ts
|
|
426
|
+
function userRoot() {
|
|
427
|
+
return process.env.STRAUSS_KB_USER_ROOT || (0, import_node_os.homedir)();
|
|
324
428
|
}
|
|
325
|
-
function
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
429
|
+
function layerRoot(workspaceDir, layer) {
|
|
430
|
+
return layer === "user" ? userRoot() : (0, import_node_path2.resolve)(workspaceDir);
|
|
431
|
+
}
|
|
432
|
+
function layerFile(workspaceDir, layer) {
|
|
433
|
+
return (0, import_node_path2.join)(
|
|
434
|
+
layerRoot(workspaceDir, layer),
|
|
435
|
+
layer === "local" ? PINS_LOCAL_FILE : PINS_FILE
|
|
436
|
+
);
|
|
437
|
+
}
|
|
438
|
+
async function readPinsLayer(workspaceDir, layer) {
|
|
439
|
+
const file = layerFile(workspaceDir, layer);
|
|
440
|
+
let raw;
|
|
441
|
+
try {
|
|
442
|
+
raw = await (0, import_promises.readFile)(file, "utf8");
|
|
443
|
+
} catch {
|
|
444
|
+
return { pins: [] };
|
|
445
|
+
}
|
|
446
|
+
let parsed;
|
|
447
|
+
try {
|
|
448
|
+
parsed = JSON.parse(raw);
|
|
449
|
+
} catch (error) {
|
|
450
|
+
throw new KbPinsMalformedError(
|
|
451
|
+
file,
|
|
452
|
+
error instanceof Error ? error.message : "invalid JSON"
|
|
453
|
+
);
|
|
454
|
+
}
|
|
455
|
+
const manifest = pinsManifestSchema.safeParse(parsed);
|
|
456
|
+
if (!manifest.success) {
|
|
457
|
+
throw new KbPinsMalformedError(
|
|
458
|
+
file,
|
|
459
|
+
manifest.error.issues[0]?.message ?? "invalid shape"
|
|
460
|
+
);
|
|
461
|
+
}
|
|
462
|
+
return manifest.data;
|
|
463
|
+
}
|
|
464
|
+
async function writePinsLayer(workspaceDir, layer, manifest) {
|
|
465
|
+
const file = layerFile(workspaceDir, layer);
|
|
466
|
+
await (0, import_promises.mkdir)((0, import_node_path2.dirname)(file), { recursive: true });
|
|
467
|
+
await (0, import_promises.writeFile)(file, `${JSON.stringify(manifest, null, 2)}
|
|
468
|
+
`, "utf8");
|
|
469
|
+
}
|
|
470
|
+
function resolvePinPath(rootDir, path) {
|
|
471
|
+
return (0, import_node_path2.isAbsolute)(path) ? (0, import_node_path2.resolve)(path) : (0, import_node_path2.resolve)(rootDir, path.split("/").join(import_node_path2.sep));
|
|
472
|
+
}
|
|
473
|
+
function storablePath(rootDir, bundlePath2) {
|
|
474
|
+
const rel = (0, import_node_path2.relative)((0, import_node_path2.resolve)(rootDir), (0, import_node_path2.resolve)(bundlePath2));
|
|
475
|
+
return (rel === "" ? "." : rel).split(import_node_path2.sep).join("/");
|
|
476
|
+
}
|
|
477
|
+
async function readMergedPins(workspaceDir) {
|
|
478
|
+
const manifests = {};
|
|
479
|
+
const pins = [];
|
|
480
|
+
const seen = /* @__PURE__ */ new Set();
|
|
481
|
+
for (const layer of PIN_LAYERS) {
|
|
482
|
+
let manifest;
|
|
331
483
|
try {
|
|
332
|
-
|
|
484
|
+
manifest = await readPinsLayer(workspaceDir, layer);
|
|
333
485
|
} catch {
|
|
334
|
-
|
|
335
|
-
return;
|
|
486
|
+
continue;
|
|
336
487
|
}
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
488
|
+
manifests[layer] = manifest;
|
|
489
|
+
const root = layerRoot(workspaceDir, layer);
|
|
490
|
+
for (const entry of manifest.pins) {
|
|
491
|
+
const absolutePath = resolvePinPath(root, entry.path);
|
|
492
|
+
if (seen.has(absolutePath)) continue;
|
|
493
|
+
seen.add(absolutePath);
|
|
494
|
+
pins.push({ ...entry, layer, absolutePath });
|
|
341
495
|
}
|
|
342
|
-
|
|
343
|
-
}
|
|
344
|
-
return { entries, malformed };
|
|
496
|
+
}
|
|
497
|
+
return { pins, manifests };
|
|
345
498
|
}
|
|
346
499
|
|
|
347
|
-
// src/
|
|
348
|
-
function
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
};
|
|
500
|
+
// src/kb-pins/frozen.ts
|
|
501
|
+
async function assertBaseNotFrozen(workspaceDir, bundlePath2) {
|
|
502
|
+
const merged = await readMergedPins(workspaceDir);
|
|
503
|
+
const absolute = (0, import_node_path3.resolve)(bundlePath2);
|
|
504
|
+
const pin = merged.pins.find((entry) => entry.absolutePath === absolute);
|
|
505
|
+
if (pin?.frozen === true) {
|
|
506
|
+
throw new KbBaseFrozenError(pin.path, pin.layer);
|
|
507
|
+
}
|
|
356
508
|
}
|
|
357
509
|
|
|
358
|
-
// src/
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
if (existing.depth > 0 && !existing.via.includes(edge)) {
|
|
378
|
-
existing.via.push(edge);
|
|
379
|
-
}
|
|
380
|
-
continue;
|
|
381
|
-
}
|
|
382
|
-
reached.set(record.conceptId, { record, depth, via: [edge] });
|
|
383
|
-
next.push(record);
|
|
384
|
-
}
|
|
385
|
-
}
|
|
386
|
-
}
|
|
387
|
-
frontier = next;
|
|
388
|
-
}
|
|
389
|
-
return [...reached.values()].sort(byGeneratedAt);
|
|
510
|
+
// src/kb-pins/list.ts
|
|
511
|
+
async function listPins(store, workspaceDir) {
|
|
512
|
+
const merged = await readMergedPins(workspaceDir);
|
|
513
|
+
return Promise.all(
|
|
514
|
+
merged.pins.map(async (entry) => {
|
|
515
|
+
const records = await store.list(entry.absolutePath);
|
|
516
|
+
return {
|
|
517
|
+
path: entry.path,
|
|
518
|
+
layer: entry.layer,
|
|
519
|
+
pinnedAt: entry.pinnedAt ?? null,
|
|
520
|
+
absolutePath: entry.absolutePath,
|
|
521
|
+
valid: records.length > 0,
|
|
522
|
+
recordCount: records.length,
|
|
523
|
+
mode: entry.mode ?? null,
|
|
524
|
+
profiles: entry.profiles ?? null,
|
|
525
|
+
frozen: entry.frozen === true
|
|
526
|
+
};
|
|
527
|
+
})
|
|
528
|
+
);
|
|
390
529
|
}
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
}
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
(source) => mine.has(source.id)
|
|
530
|
+
|
|
531
|
+
// src/kb-pins/pin.ts
|
|
532
|
+
async function pinBase(store, workspaceDir, bundlePath2, at, options = {}) {
|
|
533
|
+
const layer = options.layer ?? "project";
|
|
534
|
+
const root = layerRoot(workspaceDir, layer);
|
|
535
|
+
const manifest = await readPinsLayer(workspaceDir, layer);
|
|
536
|
+
const absolute = resolvePinPath(root, storablePath(root, bundlePath2));
|
|
537
|
+
const existing = manifest.pins.find(
|
|
538
|
+
(entry2) => resolvePinPath(root, entry2.path) === absolute
|
|
539
|
+
);
|
|
540
|
+
const records = await store.list(absolute);
|
|
541
|
+
const warning = records.length === 0 ? `no records found at ${absolute} \u2014 pinned anyway; bases are routinely pinned before they are populated` : void 0;
|
|
542
|
+
const fields = {
|
|
543
|
+
...options.mode ? { mode: options.mode } : {},
|
|
544
|
+
...options.profiles?.length ? { profiles: options.profiles } : {},
|
|
545
|
+
...options.frozen !== void 0 ? { frozen: options.frozen } : {}
|
|
546
|
+
};
|
|
547
|
+
if (existing) {
|
|
548
|
+
const updated = { ...existing, ...fields };
|
|
549
|
+
if (Object.keys(fields).length) {
|
|
550
|
+
await writePinsLayer(workspaceDir, layer, {
|
|
551
|
+
...manifest,
|
|
552
|
+
pins: manifest.pins.map(
|
|
553
|
+
(entry2) => entry2 === existing ? updated : entry2
|
|
416
554
|
)
|
|
417
|
-
);
|
|
555
|
+
});
|
|
418
556
|
}
|
|
557
|
+
return {
|
|
558
|
+
path: existing.path,
|
|
559
|
+
layer,
|
|
560
|
+
pinnedAt: existing.pinnedAt ?? at,
|
|
561
|
+
alreadyPinned: true,
|
|
562
|
+
...updated.mode ? { mode: updated.mode } : {},
|
|
563
|
+
...updated.profiles ? { profiles: updated.profiles } : {},
|
|
564
|
+
...updated.frozen !== void 0 ? { frozen: updated.frozen } : {},
|
|
565
|
+
...warning ? { warning } : {}
|
|
566
|
+
};
|
|
419
567
|
}
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
568
|
+
const entry = {
|
|
569
|
+
path: storablePath(root, bundlePath2),
|
|
570
|
+
pinnedAt: at,
|
|
571
|
+
...fields
|
|
572
|
+
};
|
|
573
|
+
await writePinsLayer(workspaceDir, layer, {
|
|
574
|
+
...manifest,
|
|
575
|
+
pins: [...manifest.pins, entry]
|
|
576
|
+
});
|
|
577
|
+
return {
|
|
578
|
+
path: entry.path,
|
|
579
|
+
layer,
|
|
580
|
+
pinnedAt: at,
|
|
581
|
+
alreadyPinned: false,
|
|
582
|
+
...fields,
|
|
583
|
+
...warning ? { warning } : {}
|
|
584
|
+
};
|
|
429
585
|
}
|
|
430
586
|
|
|
431
|
-
// src/
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
const
|
|
435
|
-
const
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
}
|
|
441
|
-
|
|
442
|
-
const by = fm.strauss_superseded_by;
|
|
443
|
-
if (!by) {
|
|
444
|
-
report("superseded_by", conceptId2, "superseded with no replacement");
|
|
445
|
-
} else if (!byId.has(by)) {
|
|
446
|
-
report("superseded_by", conceptId2, `replacement ${by} is missing`);
|
|
447
|
-
} else if (!byId.get(by)?.frontmatter.strauss_supersedes?.includes(conceptId2)) {
|
|
448
|
-
report("backlink", by, `does not list ${conceptId2} in supersedes`);
|
|
449
|
-
}
|
|
450
|
-
}
|
|
451
|
-
for (const old of fm.strauss_supersedes ?? []) {
|
|
452
|
-
const previous = byId.get(old);
|
|
453
|
-
if (!previous) {
|
|
454
|
-
report("supersedes", conceptId2, `target ${old} is missing`);
|
|
455
|
-
} else if (previous.frontmatter.strauss_status !== "superseded") {
|
|
456
|
-
report("supersedes", conceptId2, `${old} is not marked superseded`);
|
|
457
|
-
}
|
|
587
|
+
// src/kb-pins/unpin.ts
|
|
588
|
+
var import_node_path4 = require("path");
|
|
589
|
+
async function unpinBase(workspaceDir, bundlePath2) {
|
|
590
|
+
const layers = [];
|
|
591
|
+
for (const layer of PIN_LAYERS) {
|
|
592
|
+
const root = layerRoot(workspaceDir, layer);
|
|
593
|
+
let manifest;
|
|
594
|
+
try {
|
|
595
|
+
manifest = await readPinsLayer(workspaceDir, layer);
|
|
596
|
+
} catch {
|
|
597
|
+
continue;
|
|
458
598
|
}
|
|
459
|
-
|
|
460
|
-
|
|
599
|
+
const absolute = resolvePinPath(root, storablePath(root, bundlePath2));
|
|
600
|
+
const kept = manifest.pins.filter(
|
|
601
|
+
(entry) => resolvePinPath(root, entry.path) !== absolute
|
|
602
|
+
);
|
|
603
|
+
if (kept.length !== manifest.pins.length) {
|
|
604
|
+
await writePinsLayer(workspaceDir, layer, { ...manifest, pins: kept });
|
|
605
|
+
layers.push(layer);
|
|
461
606
|
}
|
|
462
607
|
}
|
|
463
|
-
return
|
|
608
|
+
return {
|
|
609
|
+
path: storablePath((0, import_node_path4.resolve)(workspaceDir), bundlePath2),
|
|
610
|
+
removed: layers.length > 0,
|
|
611
|
+
layers
|
|
612
|
+
};
|
|
464
613
|
}
|
|
465
614
|
|
|
466
|
-
// src/commands.ts
|
|
467
|
-
var
|
|
468
|
-
var
|
|
615
|
+
// src/commands/model.ts
|
|
616
|
+
var import_zod5 = require("zod");
|
|
617
|
+
var bundlePath = import_zod5.z.string().min(1).describe("Absolute path to the knowledge base directory.");
|
|
618
|
+
var conceptId = import_zod5.z.string().min(1).describe("e.g. decision.cursor-v2");
|
|
469
619
|
function define(command) {
|
|
470
620
|
return command;
|
|
471
621
|
}
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
type: import_zod6.z.enum(KB_RECORD_TYPES),
|
|
489
|
-
input: composeInputSchema
|
|
490
|
-
}),
|
|
491
|
-
fromArgv: async (argv, path, stdin) => ({
|
|
492
|
-
bundlePath: path,
|
|
493
|
-
type: argv[1],
|
|
494
|
-
input: JSON.parse(await stdin())
|
|
495
|
-
}),
|
|
496
|
-
run: async ({ store, actor, now }, { bundlePath: path, type, input }) => {
|
|
497
|
-
const record = await store.write(
|
|
498
|
-
path,
|
|
499
|
-
composeRecord(type, input, actor, now()),
|
|
500
|
-
actor
|
|
501
|
-
);
|
|
502
|
-
return { conceptId: record.conceptId };
|
|
503
|
-
}
|
|
504
|
-
}),
|
|
505
|
-
define({
|
|
506
|
-
name: "write-decision",
|
|
507
|
-
tool: "kb_write_decision",
|
|
508
|
-
usage: "write-decision < decision.json",
|
|
509
|
-
description: [
|
|
510
|
-
"Write a decision. Takes `alternative` and `impact` as fields rather than free sections, because what was rejected is the part a later reader cannot reconstruct from the code \u2014 a heading is too easy to leave empty.",
|
|
511
|
-
"",
|
|
512
|
-
"What belongs in one:",
|
|
513
|
-
'- Record a decision when a later reader would otherwise "simplify" the constraint away. If the diff already answers the question, there is nothing here to write.',
|
|
514
|
-
"- `alternative` is what you turned down and why, not a list of everything considered.",
|
|
515
|
-
"- A reference to material you read goes in `sources`; a reference to code goes in `anchors`; a reference to another record goes in `relatedConceptIds`."
|
|
516
|
-
].join("\n"),
|
|
517
|
-
input: import_zod6.z.object({ bundlePath, input: decisionInputSchema }),
|
|
518
|
-
fromArgv: async (_argv, path, stdin) => ({
|
|
519
|
-
bundlePath: path,
|
|
520
|
-
input: JSON.parse(await stdin())
|
|
521
|
-
}),
|
|
522
|
-
run: async ({ store, actor, now }, { bundlePath: path, input }) => {
|
|
523
|
-
const record = await store.write(
|
|
524
|
-
path,
|
|
525
|
-
composeDecisionRecord(input, actor, now()),
|
|
526
|
-
actor
|
|
527
|
-
);
|
|
528
|
-
return { conceptId: record.conceptId };
|
|
529
|
-
}
|
|
530
|
-
}),
|
|
531
|
-
define({
|
|
532
|
-
name: "no-decision",
|
|
533
|
-
tool: "kb_no_decision",
|
|
534
|
-
usage: "no-decision <reason...>",
|
|
535
|
-
description: 'Claim in one sentence that there was nothing to decide. Gating on "did you write a decision?" rewards writing a junk one; gating on "did you answer?" does not, so silence has to be expressible. Idempotent \u2014 restating it is not a collision.',
|
|
536
|
-
input: import_zod6.z.object({ bundlePath, reason: import_zod6.z.string().min(1) }),
|
|
537
|
-
fromArgv: (argv, path) => ({
|
|
538
|
-
bundlePath: path,
|
|
539
|
-
reason: argv.slice(1).join(" ").trim()
|
|
540
|
-
}),
|
|
541
|
-
run: async ({ store, actor, now }, { bundlePath: path, reason }) => {
|
|
542
|
-
const record = await store.write(
|
|
543
|
-
path,
|
|
544
|
-
{ ...composeNoDecisionRecord(reason, actor, now()), overwrite: true },
|
|
545
|
-
actor
|
|
546
|
-
);
|
|
547
|
-
return { conceptId: record.conceptId };
|
|
548
|
-
}
|
|
549
|
-
}),
|
|
550
|
-
define({
|
|
551
|
-
name: "status",
|
|
552
|
-
tool: "kb_status",
|
|
553
|
-
usage: "status <concept-id> <status>",
|
|
554
|
-
description: "Move a record's status, leaving everything else alone. Uses a compare-and-swap, so a concurrent change fails loudly rather than being overwritten.",
|
|
555
|
-
input: import_zod6.z.object({
|
|
556
|
-
bundlePath,
|
|
557
|
-
conceptId,
|
|
558
|
-
status: import_zod6.z.enum(KB_RECORD_STATUSES)
|
|
559
|
-
}),
|
|
560
|
-
fromArgv: (argv, path) => ({
|
|
561
|
-
bundlePath: path,
|
|
562
|
-
conceptId: argv[1],
|
|
563
|
-
status: argv[2]
|
|
564
|
-
}),
|
|
565
|
-
run: async ({ store, actor }, { bundlePath: path, conceptId: id, status }) => {
|
|
566
|
-
const record = await store.setStatus(path, id, status, actor);
|
|
567
|
-
return { conceptId: record.conceptId, status };
|
|
568
|
-
}
|
|
569
|
-
}),
|
|
570
|
-
define({
|
|
571
|
-
name: "supersede",
|
|
572
|
-
tool: "kb_supersede",
|
|
573
|
-
usage: "supersede <concept-id> <replacement-id>",
|
|
574
|
-
description: "Mark a record superseded by another, linking both directions. Use this rather than editing a record whose meaning changed \u2014 a record that quietly becomes something else invalidates every reference to it, and the earlier understanding is what a later trace needs.",
|
|
575
|
-
input: import_zod6.z.object({ bundlePath, conceptId, replacementId: conceptId }),
|
|
576
|
-
fromArgv: (argv, path) => ({
|
|
577
|
-
bundlePath: path,
|
|
578
|
-
conceptId: argv[1],
|
|
579
|
-
replacementId: argv[2]
|
|
580
|
-
}),
|
|
581
|
-
run: async ({ store, actor }, { bundlePath: path, conceptId: id, replacementId }) => {
|
|
582
|
-
await store.supersede(path, id, replacementId, actor);
|
|
583
|
-
return { superseded: id, replacedBy: replacementId };
|
|
584
|
-
}
|
|
585
|
-
}),
|
|
586
|
-
define({
|
|
587
|
-
name: "answer",
|
|
588
|
-
tool: "kb_answer",
|
|
589
|
-
usage: "answer <concept-id> <answer...>",
|
|
590
|
-
description: "Resolve an open question: sets the status, stamps who answered and when, and appends an Answer section. If the answer overturns an assumption or a decision, that is a supersession \u2014 do it explicitly.",
|
|
591
|
-
input: import_zod6.z.object({ bundlePath, conceptId, answer: import_zod6.z.string().min(1) }),
|
|
592
|
-
fromArgv: (argv, path) => ({
|
|
593
|
-
bundlePath: path,
|
|
594
|
-
conceptId: argv[1],
|
|
595
|
-
answer: argv.slice(2).join(" ").trim()
|
|
596
|
-
}),
|
|
597
|
-
run: async ({ store, actor }, { bundlePath: path, conceptId: id, answer }) => {
|
|
598
|
-
const record = await store.answer(path, id, answer, actor);
|
|
599
|
-
return { conceptId: record.conceptId };
|
|
600
|
-
}
|
|
601
|
-
}),
|
|
602
|
-
define({
|
|
603
|
-
name: "load",
|
|
604
|
-
tool: "kb_load",
|
|
605
|
-
usage: "load [type] [--budget N]",
|
|
606
|
-
description: "Load the whole knowledge base at once, each record with its standing. Prefer this over searching: these bases run to a few thousand tokens, and a reader holding all of it has perfect recall and knows why it is asking, which no ranker does. Superseded records arrive under `superseded` as name, replacement and date only \u2014 their bodies no longer hold, and reading one later in a long session is the mistake this prevents; pass the id to kb_trace when you need the history. Rejected and unresolved records arrive whole: what was turned down, and what is still open, is the part a diff cannot show you. Refuses with a count rather than truncating when the base is too large \u2014 a truncated base is indistinguishable from a complete one, and would have you conclude something was never decided from a slice you did not know was a slice.",
|
|
607
|
-
input: import_zod6.z.object({
|
|
608
|
-
bundlePath,
|
|
609
|
-
type: import_zod6.z.enum(KB_RECORD_TYPES).optional(),
|
|
610
|
-
budgetTokens: import_zod6.z.number().int().positive().optional().describe("Approximate token ceiling. Defaults to 25000.")
|
|
611
|
-
}),
|
|
612
|
-
fromArgv: (argv, path) => {
|
|
613
|
-
const at = argv.indexOf("--budget");
|
|
614
|
-
return {
|
|
615
|
-
bundlePath: path,
|
|
616
|
-
...argv[1] && argv[1] !== "--budget" ? { type: argv[1] } : {},
|
|
617
|
-
...at !== -1 && argv[at + 1] ? { budgetTokens: Number(argv[at + 1]) } : {}
|
|
618
|
-
};
|
|
619
|
-
},
|
|
620
|
-
run: async ({ store }, { bundlePath: path, type, budgetTokens }) => {
|
|
621
|
-
const result = await store.load(path, {
|
|
622
|
-
...type ? { type } : {},
|
|
623
|
-
...budgetTokens ? { budgetTokens } : {}
|
|
624
|
-
});
|
|
625
|
-
if (!result.loaded) return result;
|
|
626
|
-
return {
|
|
627
|
-
...result,
|
|
628
|
-
records: result.records.map((hit) => ({
|
|
629
|
-
conceptId: hit.record.conceptId,
|
|
630
|
-
title: hit.record.frontmatter.title ?? null,
|
|
631
|
-
standing: hit.standing,
|
|
632
|
-
supersededBy: hit.heads.map((head) => head.conceptId),
|
|
633
|
-
warnings: hit.warnings,
|
|
634
|
-
anchors: hit.record.frontmatter.strauss_anchors ?? [],
|
|
635
|
-
body: hit.record.body
|
|
636
|
-
}))
|
|
637
|
-
};
|
|
638
|
-
}
|
|
622
|
+
function argvFlag(argv, name) {
|
|
623
|
+
const at = argv.indexOf(name);
|
|
624
|
+
return at !== -1 ? argv[at + 1] : void 0;
|
|
625
|
+
}
|
|
626
|
+
|
|
627
|
+
// src/commands/answer.ts
|
|
628
|
+
var answerCommand = define({
|
|
629
|
+
name: "answer",
|
|
630
|
+
tool: "kb_answer",
|
|
631
|
+
usage: "answer <concept-id> <answer...>",
|
|
632
|
+
description: "Resolve an open question: sets the status, stamps who answered and when, and appends an Answer section. If the answer overturns an assumption or a decision, that is a supersession \u2014 do it explicitly.",
|
|
633
|
+
input: import_zod6.z.object({ bundlePath, conceptId, answer: import_zod6.z.string().min(1) }),
|
|
634
|
+
fromArgv: (argv, path) => ({
|
|
635
|
+
bundlePath: path,
|
|
636
|
+
conceptId: argv[1],
|
|
637
|
+
answer: argv.slice(2).join(" ").trim()
|
|
639
638
|
}),
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
639
|
+
run: async ({ store, actor }, { bundlePath: path, conceptId: id, answer }) => {
|
|
640
|
+
await assertBaseNotFrozen(process.cwd(), path);
|
|
641
|
+
const record = await store.answer(path, id, answer, actor);
|
|
642
|
+
return { conceptId: record.conceptId };
|
|
643
|
+
}
|
|
644
|
+
});
|
|
645
|
+
|
|
646
|
+
// src/commands/context.ts
|
|
647
|
+
var import_zod7 = require("zod");
|
|
648
|
+
|
|
649
|
+
// src/kb-context.ts
|
|
650
|
+
var import_promises2 = require("fs/promises");
|
|
651
|
+
|
|
652
|
+
// src/adjudicate.ts
|
|
653
|
+
var STANDING = {
|
|
654
|
+
accepted: "current",
|
|
655
|
+
resolved: "current",
|
|
656
|
+
draft: "unsettled",
|
|
657
|
+
proposed: "unsettled",
|
|
658
|
+
open: "open",
|
|
659
|
+
rejected: "rejected",
|
|
660
|
+
superseded: "superseded"
|
|
661
|
+
};
|
|
662
|
+
function adjudicate(hits, bundle, now = /* @__PURE__ */ new Date()) {
|
|
663
|
+
const byId = new Map(bundle.map((record) => [record.conceptId, record]));
|
|
664
|
+
return hits.map((record) => {
|
|
665
|
+
const status = record.frontmatter.strauss_status;
|
|
666
|
+
const warnings = [];
|
|
667
|
+
let heads = [];
|
|
668
|
+
if (status === "superseded") {
|
|
669
|
+
const resolved = resolveHeads(record, byId);
|
|
670
|
+
heads = resolved.heads;
|
|
671
|
+
warnings.push(...resolved.warnings);
|
|
672
|
+
if (heads.length) {
|
|
673
|
+
warnings.push({
|
|
674
|
+
kind: "superseded",
|
|
675
|
+
by: heads.map((head) => head.conceptId)
|
|
676
|
+
});
|
|
677
|
+
}
|
|
678
|
+
} else if (status === "rejected") {
|
|
679
|
+
warnings.push({ kind: "rejected" });
|
|
680
|
+
} else if (status === "draft" || status === "proposed") {
|
|
681
|
+
warnings.push({ kind: "unsettled", status });
|
|
682
|
+
} else if (status === "open") {
|
|
683
|
+
warnings.push({ kind: "unresolved-question" });
|
|
684
|
+
}
|
|
685
|
+
const staleAfter = record.frontmatter.stale_after;
|
|
686
|
+
if (staleAfter && Date.parse(staleAfter) < now.getTime()) {
|
|
687
|
+
warnings.push({ kind: "stale", staleAfter });
|
|
688
|
+
}
|
|
689
|
+
if (!record.frontmatter.verified?.length) {
|
|
690
|
+
warnings.push({ kind: "unverified" });
|
|
691
|
+
}
|
|
692
|
+
return { record, standing: STANDING[status], heads, warnings };
|
|
693
|
+
});
|
|
694
|
+
}
|
|
695
|
+
function resolveHeads(from, byId) {
|
|
696
|
+
const warnings = [];
|
|
697
|
+
const heads = /* @__PURE__ */ new Map();
|
|
698
|
+
const seen = /* @__PURE__ */ new Set([from.conceptId]);
|
|
699
|
+
const queue = [from];
|
|
700
|
+
while (queue.length) {
|
|
701
|
+
const current = queue.shift();
|
|
702
|
+
const next = successors(current, byId);
|
|
703
|
+
for (const missing of next.missing) {
|
|
704
|
+
warnings.push({ kind: "broken-chain", missing });
|
|
705
|
+
}
|
|
706
|
+
if (!next.records.length) {
|
|
707
|
+
if (current.conceptId !== from.conceptId)
|
|
708
|
+
heads.set(current.conceptId, current);
|
|
709
|
+
continue;
|
|
710
|
+
}
|
|
711
|
+
for (const record of next.records) {
|
|
712
|
+
if (seen.has(record.conceptId)) {
|
|
713
|
+
warnings.push({ kind: "chain-cycle", through: [...seen] });
|
|
714
|
+
continue;
|
|
715
|
+
}
|
|
716
|
+
seen.add(record.conceptId);
|
|
717
|
+
queue.push(record);
|
|
718
|
+
}
|
|
719
|
+
}
|
|
720
|
+
if (heads.size > 1) {
|
|
721
|
+
warnings.push({ kind: "forked-chain", heads: [...heads.keys()] });
|
|
722
|
+
}
|
|
723
|
+
return { heads: [...heads.values()], warnings };
|
|
724
|
+
}
|
|
725
|
+
function successors(record, byId) {
|
|
726
|
+
const ids = /* @__PURE__ */ new Set();
|
|
727
|
+
const forward = record.frontmatter.strauss_superseded_by;
|
|
728
|
+
if (forward) ids.add(forward);
|
|
729
|
+
for (const [id, candidate] of byId) {
|
|
730
|
+
if (candidate.frontmatter.strauss_supersedes?.includes(record.conceptId)) {
|
|
731
|
+
ids.add(id);
|
|
732
|
+
}
|
|
733
|
+
}
|
|
734
|
+
const records = [];
|
|
735
|
+
const missing = [];
|
|
736
|
+
for (const id of ids) {
|
|
737
|
+
const found = byId.get(id);
|
|
738
|
+
if (found) records.push(found);
|
|
739
|
+
else missing.push(id);
|
|
740
|
+
}
|
|
741
|
+
return { records, missing };
|
|
742
|
+
}
|
|
743
|
+
|
|
744
|
+
// src/kb-index.ts
|
|
745
|
+
var INDEX_FILE = "INDEX.md";
|
|
746
|
+
var HEADING = "# KB Index";
|
|
747
|
+
function renderIndex(records) {
|
|
748
|
+
const lines = [...records].sort((left, right) => left.conceptId.localeCompare(right.conceptId)).map(renderIndexLine);
|
|
749
|
+
return `${HEADING}
|
|
750
|
+
|
|
751
|
+
${lines.join("\n")}
|
|
752
|
+
`;
|
|
753
|
+
}
|
|
754
|
+
function renderIndexLine(record) {
|
|
755
|
+
const { frontmatter: fm } = record;
|
|
756
|
+
const parts = [fm.type, fm.strauss_status];
|
|
757
|
+
if (fm.tags?.length) parts.push(`tags: ${fm.tags.join(", ")}`);
|
|
758
|
+
if (fm.description) parts.push(fm.description);
|
|
759
|
+
return `- [${fm.title ?? record.conceptId}](${record.conceptId}.md) \u2014 ${parts.join(" \xB7 ")}`;
|
|
760
|
+
}
|
|
761
|
+
function indexIsStale(stored, expected) {
|
|
762
|
+
return stored !== expected;
|
|
763
|
+
}
|
|
764
|
+
|
|
765
|
+
// src/kb-context.ts
|
|
766
|
+
var HEADING2 = "## Knowledge bases (pinned)";
|
|
767
|
+
var DEFAULT_CONTEXT_BUDGET = 4e3;
|
|
768
|
+
var CONTEXT_PROFILES = {
|
|
769
|
+
"session-start": { fullUnderTokens: 1500 },
|
|
770
|
+
compact: { budgetTokens: 2500 },
|
|
771
|
+
turn: { budgetTokens: 2500 }
|
|
772
|
+
};
|
|
773
|
+
function approxTokens(text) {
|
|
774
|
+
return Math.ceil(text.length / 4);
|
|
775
|
+
}
|
|
776
|
+
function preamble() {
|
|
777
|
+
return [
|
|
778
|
+
HEADING2,
|
|
779
|
+
"",
|
|
780
|
+
"What follows is an index of this workspace's pinned knowledge bases \u2014",
|
|
781
|
+
"concept ids, titles and standing only. The record bodies are NOT in this",
|
|
782
|
+
"context.",
|
|
783
|
+
"",
|
|
784
|
+
"Consult records only through the strauss-kb MCP tools: `kb_load` (the",
|
|
785
|
+
"preferred first call), `kb_query`, and `kb_trace`, passing the",
|
|
786
|
+
"`bundlePath` listed with each base. Do not read record files directly:",
|
|
787
|
+
"a raw file read bypasses supersession resolution, and a superseded or",
|
|
788
|
+
"rejected record file reads exactly like a current one \u2014 only the store",
|
|
789
|
+
"resolves chains and standing.",
|
|
790
|
+
"",
|
|
791
|
+
"KB content loaded earlier in a long session may have been compacted",
|
|
792
|
+
"away. Before answering a question one of these bases governs, load it",
|
|
793
|
+
"again at the point of use \u2014 reloading a small base costs a few thousand",
|
|
794
|
+
"tokens."
|
|
795
|
+
].join("\n");
|
|
796
|
+
}
|
|
797
|
+
async function renderBase(store, path, absolutePath, fullUnderTokens, pinMode, budgetTokens) {
|
|
798
|
+
const bundle = await store.list(absolutePath);
|
|
799
|
+
if (bundle.length === 0) {
|
|
800
|
+
return {
|
|
801
|
+
path,
|
|
802
|
+
absolutePath,
|
|
803
|
+
mode: "empty",
|
|
804
|
+
body: "No readable records yet \u2014 pinned ahead of being populated."
|
|
805
|
+
};
|
|
806
|
+
}
|
|
807
|
+
const fullCap = pinMode === "full" ? budgetTokens : pinMode === "index" ? 0 : fullUnderTokens;
|
|
808
|
+
let degradedFrom;
|
|
809
|
+
if (fullCap > 0) {
|
|
810
|
+
const full = await store.load(absolutePath, {
|
|
811
|
+
budgetTokens: fullCap
|
|
812
|
+
});
|
|
813
|
+
if (!full.loaded && pinMode === "full") {
|
|
814
|
+
degradedFrom = { approxTokens: full.approxTokens };
|
|
815
|
+
}
|
|
816
|
+
if (full.loaded) {
|
|
817
|
+
const records = full.records.map(
|
|
818
|
+
(hit) => [
|
|
819
|
+
`#### ${hit.record.conceptId} \u2014 ${hit.record.frontmatter.title ?? "(untitled)"} (${hit.standing})`,
|
|
820
|
+
"",
|
|
821
|
+
hit.record.body.trim()
|
|
822
|
+
].join("\n")
|
|
823
|
+
);
|
|
824
|
+
const superseded2 = full.superseded.map(
|
|
825
|
+
(entry) => `- \`${entry.conceptId}\` \u2192 superseded by ${entry.supersededBy.map((id) => `\`${id}\``).join(", ") || "(missing replacement)"}`
|
|
826
|
+
);
|
|
827
|
+
return {
|
|
828
|
+
path,
|
|
829
|
+
absolutePath,
|
|
830
|
+
mode: "full",
|
|
831
|
+
body: [
|
|
832
|
+
...records,
|
|
833
|
+
...superseded2.length ? [
|
|
834
|
+
"#### Superseded (bodies withheld \u2014 kb_trace reaches them)",
|
|
835
|
+
...superseded2
|
|
836
|
+
] : []
|
|
837
|
+
].join("\n\n")
|
|
838
|
+
};
|
|
839
|
+
}
|
|
840
|
+
}
|
|
841
|
+
const adjudicated = adjudicate(bundle, bundle);
|
|
842
|
+
const lines = adjudicated.filter((hit) => hit.standing !== "superseded").map((hit) => renderIndexLine(hit.record));
|
|
843
|
+
const superseded = adjudicated.filter((hit) => hit.standing === "superseded").map(
|
|
844
|
+
(hit) => `- \`${hit.record.conceptId}\` \u2192 superseded by ${hit.heads.map((head) => `\`${head.conceptId}\``).join(", ") || "(missing replacement)"}`
|
|
845
|
+
);
|
|
846
|
+
return {
|
|
847
|
+
path,
|
|
848
|
+
absolutePath,
|
|
849
|
+
mode: "index",
|
|
850
|
+
body: [...lines, ...superseded].join("\n"),
|
|
851
|
+
...degradedFrom ? { degradedFrom } : {}
|
|
852
|
+
};
|
|
853
|
+
}
|
|
854
|
+
async function buildContext(store, workspaceDir, options = {}) {
|
|
855
|
+
const builtin = options.profile ? CONTEXT_PROFILES[options.profile] ?? {} : {};
|
|
856
|
+
let budgetTokens = options.budgetTokens ?? builtin.budgetTokens ?? DEFAULT_CONTEXT_BUDGET;
|
|
857
|
+
let fullUnderTokens = options.fullUnderTokens ?? builtin.fullUnderTokens ?? 0;
|
|
858
|
+
const merged = await readMergedPins(workspaceDir);
|
|
859
|
+
const fromManifest = mergedContextBudgets(merged, options.profile);
|
|
860
|
+
budgetTokens = options.budgetTokens ?? fromManifest.budgetTokens ?? builtin.budgetTokens ?? DEFAULT_CONTEXT_BUDGET;
|
|
861
|
+
fullUnderTokens = options.fullUnderTokens ?? fromManifest.fullUnderTokens ?? builtin.fullUnderTokens ?? 0;
|
|
862
|
+
const pins = merged.pins.filter(
|
|
863
|
+
(pin) => !pin.profiles?.length || !options.profile || pin.profiles.includes(options.profile)
|
|
864
|
+
);
|
|
865
|
+
if (pins.length === 0) {
|
|
866
|
+
return {
|
|
867
|
+
block: "",
|
|
868
|
+
refused: false,
|
|
869
|
+
approxTokens: 0,
|
|
870
|
+
budgetTokens,
|
|
871
|
+
bases: []
|
|
872
|
+
};
|
|
873
|
+
}
|
|
874
|
+
const sections = await Promise.all(
|
|
875
|
+
pins.map(async (pin) => ({
|
|
876
|
+
section: await renderBase(
|
|
877
|
+
store,
|
|
878
|
+
pin.path,
|
|
879
|
+
pin.absolutePath,
|
|
880
|
+
fullUnderTokens,
|
|
881
|
+
pin.mode,
|
|
882
|
+
budgetTokens
|
|
883
|
+
),
|
|
884
|
+
frozen: pin.frozen === true
|
|
667
885
|
}))
|
|
886
|
+
);
|
|
887
|
+
const modeLabel = {
|
|
888
|
+
index: "index only \u2014 record bodies are not here",
|
|
889
|
+
full: "full records \u2014 this base arrives whole",
|
|
890
|
+
empty: "empty"
|
|
891
|
+
};
|
|
892
|
+
for (const { section } of sections) {
|
|
893
|
+
if (section.degradedFrom) {
|
|
894
|
+
options.warn?.({
|
|
895
|
+
operation: "kb.context.full-pin-degraded",
|
|
896
|
+
path: section.path,
|
|
897
|
+
approxTokens: section.degradedFrom.approxTokens,
|
|
898
|
+
budgetTokens
|
|
899
|
+
});
|
|
900
|
+
}
|
|
901
|
+
}
|
|
902
|
+
const rendered = sections.map(({ section, frozen }) => {
|
|
903
|
+
const label = section.degradedFrom ? `index only \u2014 pinned \`mode: full\`, but its ~${section.degradedFrom.approxTokens} tokens exceed this block's ${budgetTokens}-token budget; kb_load it directly (load's budget is separate), or raise this profile's budget` : modeLabel[section.mode];
|
|
904
|
+
return [
|
|
905
|
+
`### ${section.path} (${label}${frozen ? " \xB7 frozen, read-only" : ""})`,
|
|
906
|
+
"",
|
|
907
|
+
`bundlePath: \`${section.absolutePath}\``,
|
|
908
|
+
"",
|
|
909
|
+
section.body
|
|
910
|
+
].join("\n");
|
|
911
|
+
});
|
|
912
|
+
const block = [preamble(), "", rendered.join("\n\n"), ""].join("\n");
|
|
913
|
+
const bases = sections.map(({ section }) => ({
|
|
914
|
+
path: section.path,
|
|
915
|
+
absolutePath: section.absolutePath,
|
|
916
|
+
approxTokens: approxTokens(section.body)
|
|
917
|
+
}));
|
|
918
|
+
const total = approxTokens(block);
|
|
919
|
+
if (total > budgetTokens) {
|
|
920
|
+
options.warn?.({
|
|
921
|
+
operation: "kb.context.refused",
|
|
922
|
+
approxTokens: total,
|
|
923
|
+
budgetTokens,
|
|
924
|
+
bases: bases.map((base) => base.path)
|
|
925
|
+
});
|
|
926
|
+
const refusal = [
|
|
927
|
+
HEADING2,
|
|
928
|
+
"",
|
|
929
|
+
`The pinned index runs to ~${total} tokens, past the ${budgetTokens}-token`,
|
|
930
|
+
"budget, and was not emitted \u2014 a truncated index is indistinguishable",
|
|
931
|
+
"from a complete one. The pinned bases:",
|
|
932
|
+
"",
|
|
933
|
+
...bases.map(
|
|
934
|
+
(base) => `- ${base.path} \u2014 ~${base.approxTokens} tokens (bundlePath: \`${base.absolutePath}\`)`
|
|
935
|
+
),
|
|
936
|
+
"",
|
|
937
|
+
"For the question at hand, read what you need now \u2014 `kb_load` a base",
|
|
938
|
+
"(its own budget is separate), or `kb_index` for one base's shape.",
|
|
939
|
+
"",
|
|
940
|
+
"To bring this block back under budget, in order of preference:",
|
|
941
|
+
"- supersede or resolve stale records \u2014 the base shrinks, the knowledge keeps",
|
|
942
|
+
"- force a large base to index lines: `strauss-kb pin <path> --mode index`",
|
|
943
|
+
"- scope a pin to the profiles that need it: `strauss-kb pin <path> --profiles session-start`",
|
|
944
|
+
"- raise this profile's budget under `context` in .strauss/kb-pins.json",
|
|
945
|
+
"- unpin what no session actually needs",
|
|
946
|
+
""
|
|
947
|
+
].join("\n");
|
|
948
|
+
return {
|
|
949
|
+
block: refusal,
|
|
950
|
+
refused: true,
|
|
951
|
+
approxTokens: total,
|
|
952
|
+
budgetTokens,
|
|
953
|
+
bases
|
|
954
|
+
};
|
|
955
|
+
}
|
|
956
|
+
return { block, refused: false, approxTokens: total, budgetTokens, bases };
|
|
957
|
+
}
|
|
958
|
+
function toHookJson(block, event) {
|
|
959
|
+
return JSON.stringify({
|
|
960
|
+
hookSpecificOutput: {
|
|
961
|
+
hookEventName: event,
|
|
962
|
+
additionalContext: block
|
|
963
|
+
}
|
|
964
|
+
});
|
|
965
|
+
}
|
|
966
|
+
var CONTEXT_BEGIN = "<!-- strauss-kb:begin -->";
|
|
967
|
+
var CONTEXT_END = "<!-- strauss-kb:end -->";
|
|
968
|
+
async function syncInstructions(file, block) {
|
|
969
|
+
const existing = await (0, import_promises2.readFile)(file, "utf8").catch(() => null);
|
|
970
|
+
const region = block ? `${CONTEXT_BEGIN}
|
|
971
|
+
${block.trim()}
|
|
972
|
+
${CONTEXT_END}` : null;
|
|
973
|
+
if (existing === null) {
|
|
974
|
+
if (!region) return { file, action: "unchanged" };
|
|
975
|
+
await (0, import_promises2.writeFile)(file, `${region}
|
|
976
|
+
`, "utf8");
|
|
977
|
+
return { file, action: "created" };
|
|
978
|
+
}
|
|
979
|
+
const begin = existing.indexOf(CONTEXT_BEGIN);
|
|
980
|
+
const end = existing.indexOf(CONTEXT_END);
|
|
981
|
+
if (begin !== -1 && end !== -1 && end >= begin) {
|
|
982
|
+
const before = existing.slice(0, begin);
|
|
983
|
+
const after = existing.slice(end + CONTEXT_END.length);
|
|
984
|
+
const next = region ? `${before}${region}${after}` : `${before.replace(/\n+$/, "\n")}${after.replace(/^\n+/, "\n")}`;
|
|
985
|
+
if (next === existing) return { file, action: "unchanged" };
|
|
986
|
+
await (0, import_promises2.writeFile)(file, next, "utf8");
|
|
987
|
+
return { file, action: region ? "replaced" : "removed" };
|
|
988
|
+
}
|
|
989
|
+
if (!region) return { file, action: "unchanged" };
|
|
990
|
+
await (0, import_promises2.writeFile)(
|
|
991
|
+
file,
|
|
992
|
+
`${existing.replace(/\n*$/, "\n\n")}${region}
|
|
993
|
+
`,
|
|
994
|
+
"utf8"
|
|
995
|
+
);
|
|
996
|
+
return { file, action: "appended" };
|
|
997
|
+
}
|
|
998
|
+
|
|
999
|
+
// src/commands/context.ts
|
|
1000
|
+
var contextCommand = define({
|
|
1001
|
+
name: "context",
|
|
1002
|
+
tool: "kb_context",
|
|
1003
|
+
usage: "context [--profile NAME] [--budget N] [--full-under N] [--format json] [--event NAME]",
|
|
1004
|
+
description: "The pinned-base index block, for injection at every context birth \u2014 startup, clear, resume, and after compaction. An index, not the content: concept ids, titles and standing, with the bodies left behind kb_load at the point of use. Emits nothing when nothing is pinned. Refuses with the list of bases and their sizes rather than truncating past its budget. Budgets resolve most-specific-first: explicit flags, then the workspace manifests' `context` tables (per profile, over their `default`), then the built-in profile (session-start, compact, turn), then package defaults \u2014 so a repo tunes its own numbers in .strauss/kb-pins.json without touching hook commands. Like kb_schema and kb_types this takes no bundlePath \u2014 it reads the workspace pin manifests, because which bases a session should see is workspace state, not a property of one base.",
|
|
1005
|
+
input: import_zod7.z.object({
|
|
1006
|
+
budgetTokens: import_zod7.z.number().int().positive().optional().describe(
|
|
1007
|
+
"Ceiling on the whole emitted block; past it the command refuses with a list of bases rather than truncating. Defaults to 4000."
|
|
1008
|
+
),
|
|
1009
|
+
fullUnderTokens: import_zod7.z.number().int().positive().optional().describe(
|
|
1010
|
+
"Per-base rendering threshold, applied before the budget: a base whose complete load fits under this arrives as full records instead of index lines, and the whole block still answers to budgetTokens. Off by default \u2014 index-only is the safe default at a context birth, because injected bodies outlive the qualifiers on them; the session-start profile opts tiny bases in at 1500."
|
|
1011
|
+
),
|
|
1012
|
+
profile: import_zod7.z.string().optional().describe(
|
|
1013
|
+
"Named budget set: built-ins are session-start (full-under 1500), compact and turn (budget 2500); the manifests' `context` tables override per repo. Unknown names fall through to defaults rather than failing."
|
|
1014
|
+
),
|
|
1015
|
+
format: import_zod7.z.enum(["markdown", "json"]).optional().describe(
|
|
1016
|
+
"CLI envelope for hook protocols that require strict JSON on stdout. MCP callers omit this \u2014 the block itself is identical."
|
|
1017
|
+
),
|
|
1018
|
+
event: import_zod7.z.string().optional().describe(
|
|
1019
|
+
"hookEventName stamped into the JSON envelope. Only meaningful with format=json."
|
|
1020
|
+
)
|
|
1021
|
+
}),
|
|
1022
|
+
fromArgv: (argv) => {
|
|
1023
|
+
const budget = argvFlag(argv, "--budget");
|
|
1024
|
+
const fullUnder = argvFlag(argv, "--full-under");
|
|
1025
|
+
const profile = argvFlag(argv, "--profile");
|
|
1026
|
+
const format = argvFlag(argv, "--format");
|
|
1027
|
+
const event = argvFlag(argv, "--event");
|
|
1028
|
+
return {
|
|
1029
|
+
...budget ? { budgetTokens: Number(budget) } : {},
|
|
1030
|
+
...fullUnder ? { fullUnderTokens: Number(fullUnder) } : {},
|
|
1031
|
+
...profile ? { profile } : {},
|
|
1032
|
+
...format ? { format } : {},
|
|
1033
|
+
...event ? { event } : {}
|
|
1034
|
+
};
|
|
1035
|
+
},
|
|
1036
|
+
run: async ({ store }, { budgetTokens, fullUnderTokens, profile, format, event }) => {
|
|
1037
|
+
const result = await buildContext(store, process.cwd(), {
|
|
1038
|
+
...budgetTokens ? { budgetTokens } : {},
|
|
1039
|
+
...fullUnderTokens ? { fullUnderTokens } : {},
|
|
1040
|
+
...profile ? { profile } : {},
|
|
1041
|
+
// Degradations — a full pin that could not fit, a refused block — go
|
|
1042
|
+
// to stderr as well as into the block itself: stderr is diagnostics on
|
|
1043
|
+
// both surfaces (hooks discard it, MCP logs it), so an operator can
|
|
1044
|
+
// see budget pressure without reading injected context.
|
|
1045
|
+
warn: (entry) => process.stderr.write(`${JSON.stringify(entry)}
|
|
1046
|
+
`)
|
|
1047
|
+
});
|
|
1048
|
+
if (!result.block) return "";
|
|
1049
|
+
return format === "json" ? toHookJson(result.block, event ?? "SessionStart") : result.block;
|
|
1050
|
+
}
|
|
1051
|
+
});
|
|
1052
|
+
|
|
1053
|
+
// src/commands/list.ts
|
|
1054
|
+
var import_zod8 = require("zod");
|
|
1055
|
+
var listCommand = define({
|
|
1056
|
+
name: "list",
|
|
1057
|
+
tool: "kb_list",
|
|
1058
|
+
usage: "list [type]",
|
|
1059
|
+
description: "Every record, optionally narrowed to one type. Use kb_query when you have a question; this is for enumerating.",
|
|
1060
|
+
input: import_zod8.z.object({ bundlePath, type: import_zod8.z.enum(KB_RECORD_TYPES).optional() }),
|
|
1061
|
+
fromArgv: (argv, path) => ({ bundlePath: path, type: argv[1] }),
|
|
1062
|
+
run: async ({ store }, { bundlePath: path, type }) => (await store.list(path, type)).map((record) => ({
|
|
1063
|
+
conceptId: record.conceptId,
|
|
1064
|
+
title: record.frontmatter.title ?? null,
|
|
1065
|
+
description: record.frontmatter.description ?? null,
|
|
1066
|
+
status: record.frontmatter.strauss_status,
|
|
1067
|
+
anchors: record.frontmatter.strauss_anchors ?? []
|
|
1068
|
+
}))
|
|
1069
|
+
});
|
|
1070
|
+
|
|
1071
|
+
// src/commands/load.ts
|
|
1072
|
+
var import_zod9 = require("zod");
|
|
1073
|
+
var loadCommand = define({
|
|
1074
|
+
name: "load",
|
|
1075
|
+
tool: "kb_load",
|
|
1076
|
+
usage: "load [type] [--budget N]",
|
|
1077
|
+
description: "Load the whole knowledge base at once, each record with its standing. Prefer this over searching: these bases run to a few thousand tokens, and a reader holding all of it has perfect recall and knows why it is asking, which no ranker does. Superseded records arrive under `superseded` as name, replacement and date only \u2014 their bodies no longer hold, and reading one later in a long session is the mistake this prevents; pass the id to kb_trace when you need the history. Rejected and unresolved records arrive whole: what was turned down, and what is still open, is the part a diff cannot show you. Refuses with a count rather than truncating when the base is too large \u2014 a truncated base is indistinguishable from a complete one, and would have you conclude something was never decided from a slice you did not know was a slice. Call at the point of use, not once per session: a base loaded early is summarised away by compaction, so if the visible context holds no records from this base and the question at hand is one it might govern, load before answering \u2014 never conclude nothing was decided from a context with no KB content in it. This tool (with kb_query and kb_trace) is the only supported way to read a base; a raw file read bypasses supersession resolution and returns replaced records as if current.",
|
|
1078
|
+
input: import_zod9.z.object({
|
|
1079
|
+
bundlePath,
|
|
1080
|
+
type: import_zod9.z.enum(KB_RECORD_TYPES).optional(),
|
|
1081
|
+
budgetTokens: import_zod9.z.number().int().positive().optional().describe("Approximate token ceiling. Defaults to 25000.")
|
|
1082
|
+
}),
|
|
1083
|
+
fromArgv: (argv, path) => {
|
|
1084
|
+
const budget = argvFlag(argv, "--budget");
|
|
1085
|
+
return {
|
|
1086
|
+
bundlePath: path,
|
|
1087
|
+
...argv[1] && argv[1] !== "--budget" ? { type: argv[1] } : {},
|
|
1088
|
+
...budget ? { budgetTokens: Number(budget) } : {}
|
|
1089
|
+
};
|
|
1090
|
+
},
|
|
1091
|
+
run: async ({ store }, { bundlePath: path, type, budgetTokens }) => {
|
|
1092
|
+
const result = await store.load(path, {
|
|
1093
|
+
...type ? { type } : {},
|
|
1094
|
+
...budgetTokens ? { budgetTokens } : {}
|
|
1095
|
+
});
|
|
1096
|
+
if (!result.loaded) return result;
|
|
1097
|
+
return {
|
|
1098
|
+
...result,
|
|
1099
|
+
records: result.records.map((hit) => ({
|
|
1100
|
+
conceptId: hit.record.conceptId,
|
|
1101
|
+
title: hit.record.frontmatter.title ?? null,
|
|
1102
|
+
standing: hit.standing,
|
|
1103
|
+
supersededBy: hit.heads.map((head) => head.conceptId),
|
|
1104
|
+
warnings: hit.warnings,
|
|
1105
|
+
anchors: hit.record.frontmatter.strauss_anchors ?? [],
|
|
1106
|
+
body: hit.record.body
|
|
1107
|
+
}))
|
|
1108
|
+
};
|
|
1109
|
+
}
|
|
1110
|
+
});
|
|
1111
|
+
|
|
1112
|
+
// src/commands/log.ts
|
|
1113
|
+
var import_zod10 = require("zod");
|
|
1114
|
+
var logCommand = define({
|
|
1115
|
+
name: "log",
|
|
1116
|
+
tool: "kb_log",
|
|
1117
|
+
usage: "log",
|
|
1118
|
+
description: "What touched what, and when. The only artifact here that cannot be reconstructed from the records, so malformed lines are reported rather than repaired.",
|
|
1119
|
+
input: import_zod10.z.object({ bundlePath }),
|
|
1120
|
+
fromArgv: (_argv, path) => ({ bundlePath: path }),
|
|
1121
|
+
run: ({ store }, { bundlePath: path }) => store.readLog(path)
|
|
1122
|
+
});
|
|
1123
|
+
|
|
1124
|
+
// src/commands/no-decision.ts
|
|
1125
|
+
var import_zod11 = require("zod");
|
|
1126
|
+
var noDecisionCommand = define({
|
|
1127
|
+
name: "no-decision",
|
|
1128
|
+
tool: "kb_no_decision",
|
|
1129
|
+
usage: "no-decision <reason...>",
|
|
1130
|
+
description: 'Claim in one sentence that there was nothing to decide. Gating on "did you write a decision?" rewards writing a junk one; gating on "did you answer?" does not, so silence has to be expressible. Idempotent \u2014 restating it is not a collision.',
|
|
1131
|
+
input: import_zod11.z.object({ bundlePath, reason: import_zod11.z.string().min(1) }),
|
|
1132
|
+
fromArgv: (argv, path) => ({
|
|
1133
|
+
bundlePath: path,
|
|
1134
|
+
reason: argv.slice(1).join(" ").trim()
|
|
1135
|
+
}),
|
|
1136
|
+
run: async ({ store, actor, now }, { bundlePath: path, reason }) => {
|
|
1137
|
+
await assertBaseNotFrozen(process.cwd(), path);
|
|
1138
|
+
const record = await store.write(
|
|
1139
|
+
path,
|
|
1140
|
+
{ ...composeNoDecisionRecord(reason, actor, now()), overwrite: true },
|
|
1141
|
+
actor
|
|
1142
|
+
);
|
|
1143
|
+
return { conceptId: record.conceptId };
|
|
1144
|
+
}
|
|
1145
|
+
});
|
|
1146
|
+
|
|
1147
|
+
// src/commands/pin.ts
|
|
1148
|
+
var import_zod12 = require("zod");
|
|
1149
|
+
var pinCommand = define({
|
|
1150
|
+
name: "pin",
|
|
1151
|
+
tool: "kb_pin",
|
|
1152
|
+
usage: "pin [bundle-path] [--mode full|index] [--profiles a,b] [--local|--user] [--frozen|--unfreeze]",
|
|
1153
|
+
description: "Pin a base into a workspace pin manifest, so `context` surfaces it at every context birth. Three layers, nearest wins: the committed project manifest (.strauss/kb-pins.json, the default), `--local` (.strauss/kb-pins.local.json, personal and gitignored), and `--user` (~/.strauss/kb-pins.json, every workspace). Idempotent \u2014 re-pinning changes nothing unless --mode, --profiles, or --frozen/--unfreeze are given, which update just those fields. `--mode full` preloads the whole base into the block regardless of the full-under threshold; `--mode index` never upgrades. `--profiles` scopes the pin to named context profiles. `--frozen` marks the base concluded: write commands against it refuse and `context` labels it read-only. A path with no records yet succeeds with a warning; bases are routinely pinned before they are populated. Pins are workspace state: the pinned base itself is never touched.",
|
|
1154
|
+
input: import_zod12.z.object({
|
|
1155
|
+
bundlePath,
|
|
1156
|
+
mode: import_zod12.z.enum(["full", "index"]).optional().describe(
|
|
1157
|
+
"full: always emit this base's records whole (still under the block budget); index: never upgrade. Absent: the profile's full-under threshold decides."
|
|
1158
|
+
),
|
|
1159
|
+
profiles: import_zod12.z.array(import_zod12.z.string()).optional().describe("Context profiles this pin surfaces in. Absent: all of them."),
|
|
1160
|
+
layer: import_zod12.z.enum(["project", "local", "user"]).optional().describe(
|
|
1161
|
+
"Which manifest to write: project (committed, default), local (personal, gitignored), user (~/.strauss, every workspace)."
|
|
1162
|
+
),
|
|
1163
|
+
frozen: import_zod12.z.boolean().optional().describe(
|
|
1164
|
+
"true: the base is concluded \u2014 writes against it refuse while pinned. false: lift a freeze."
|
|
1165
|
+
)
|
|
1166
|
+
}),
|
|
1167
|
+
fromArgv: (argv, path) => {
|
|
1168
|
+
const positional = argv[1] && !argv[1].startsWith("--") ? argv[1] : path;
|
|
1169
|
+
const mode = argvFlag(argv, "--mode");
|
|
1170
|
+
const profiles = argvFlag(argv, "--profiles");
|
|
1171
|
+
const layer = argv.includes("--user") ? "user" : argv.includes("--local") ? "local" : void 0;
|
|
1172
|
+
const frozen = argv.includes("--frozen") ? true : argv.includes("--unfreeze") ? false : void 0;
|
|
1173
|
+
return {
|
|
1174
|
+
bundlePath: positional,
|
|
1175
|
+
...mode ? { mode } : {},
|
|
1176
|
+
...profiles ? {
|
|
1177
|
+
profiles: profiles.split(",").map((p) => p.trim()).filter(Boolean)
|
|
1178
|
+
} : {},
|
|
1179
|
+
...layer ? { layer } : {},
|
|
1180
|
+
...frozen !== void 0 ? { frozen } : {}
|
|
1181
|
+
};
|
|
1182
|
+
},
|
|
1183
|
+
run: ({ store, now }, { bundlePath: path, mode, profiles, layer, frozen }) => pinBase(store, process.cwd(), path, now(), {
|
|
1184
|
+
...mode ? { mode } : {},
|
|
1185
|
+
...profiles ? { profiles } : {},
|
|
1186
|
+
...layer ? { layer } : {},
|
|
1187
|
+
...frozen !== void 0 ? { frozen } : {}
|
|
1188
|
+
})
|
|
1189
|
+
});
|
|
1190
|
+
|
|
1191
|
+
// src/commands/pins.ts
|
|
1192
|
+
var import_zod13 = require("zod");
|
|
1193
|
+
var pinsCommand = define({
|
|
1194
|
+
name: "pins",
|
|
1195
|
+
tool: "kb_pins",
|
|
1196
|
+
usage: "pins",
|
|
1197
|
+
description: "Every pinned base across the manifest layers, each with its layer and whether it currently resolves to readable records. Reads the workspace manifests rather than any one base, like kb_context.",
|
|
1198
|
+
input: import_zod13.z.object({}),
|
|
1199
|
+
fromArgv: () => ({}),
|
|
1200
|
+
run: ({ store }) => listPins(store, process.cwd())
|
|
1201
|
+
});
|
|
1202
|
+
|
|
1203
|
+
// src/commands/query.ts
|
|
1204
|
+
var import_zod14 = require("zod");
|
|
1205
|
+
var queryCommand = define({
|
|
1206
|
+
name: "query",
|
|
1207
|
+
tool: "kb_query",
|
|
1208
|
+
usage: "query <text...>",
|
|
1209
|
+
description: "Search and return each match with its standing. Results are flagged, never filtered: a superseded record comes back alongside whatever replaced it, and a rejected one is marked as something explicitly not adopted. Prefer kb_load when the base fits its budget: on this package's measurements, a reader holding the whole base answered eight of nine questions whose wording appears in no record, where embedding search answered four. Never read record files directly \u2014 this tool (with kb_load and kb_trace) is the only supported way to read a base; a file read bypasses supersession resolution and returns replaced records as if current.",
|
|
1210
|
+
input: import_zod14.z.object({
|
|
1211
|
+
bundlePath,
|
|
1212
|
+
text: import_zod14.z.string().optional(),
|
|
1213
|
+
type: import_zod14.z.enum(KB_RECORD_TYPES).optional(),
|
|
1214
|
+
includeNonCurrent: import_zod14.z.boolean().optional()
|
|
1215
|
+
}),
|
|
1216
|
+
fromArgv: (argv, path) => ({
|
|
1217
|
+
bundlePath: path,
|
|
1218
|
+
text: argv.slice(1).join(" ").trim(),
|
|
1219
|
+
includeNonCurrent: true
|
|
1220
|
+
}),
|
|
1221
|
+
run: async ({ store }, { bundlePath: path, text, type, includeNonCurrent }) => (await store.query(path, text ?? "", {
|
|
1222
|
+
...type ? { type } : {},
|
|
1223
|
+
includeNonCurrent: includeNonCurrent === true
|
|
1224
|
+
})).map((hit) => ({
|
|
1225
|
+
conceptId: hit.record.conceptId,
|
|
1226
|
+
title: hit.record.frontmatter.title ?? null,
|
|
1227
|
+
description: hit.record.frontmatter.description ?? null,
|
|
1228
|
+
standing: hit.standing,
|
|
1229
|
+
supersededBy: hit.heads.map((head) => head.conceptId),
|
|
1230
|
+
warnings: hit.warnings,
|
|
1231
|
+
body: hit.record.body
|
|
1232
|
+
}))
|
|
1233
|
+
});
|
|
1234
|
+
|
|
1235
|
+
// src/commands/read-index.ts
|
|
1236
|
+
var import_zod15 = require("zod");
|
|
1237
|
+
var readIndexCommand = define({
|
|
1238
|
+
name: "index",
|
|
1239
|
+
tool: "kb_index",
|
|
1240
|
+
usage: "index",
|
|
1241
|
+
description: "The index, rebuilt if it disagrees with the records. One call gives the whole shape of the base: title, type, status, and description per record. The cheap re-orientation call after compaction or deep in a long session \u2014 a few hundred tokens; call it (or kb_context, when bases are pinned) first, then kb_load or fetch by concept id.",
|
|
1242
|
+
input: import_zod15.z.object({ bundlePath }),
|
|
1243
|
+
fromArgv: (_argv, path) => ({ bundlePath: path }),
|
|
1244
|
+
run: ({ store }, { bundlePath: path }) => store.readIndex(path)
|
|
1245
|
+
});
|
|
1246
|
+
|
|
1247
|
+
// src/commands/schema.ts
|
|
1248
|
+
var import_zod18 = require("zod");
|
|
1249
|
+
|
|
1250
|
+
// src/json-schema.ts
|
|
1251
|
+
var import_zod17 = require("zod");
|
|
1252
|
+
|
|
1253
|
+
// src/kb-log.ts
|
|
1254
|
+
var import_zod16 = require("zod");
|
|
1255
|
+
var LOG_FILE = "log.jsonl";
|
|
1256
|
+
var kbLogEntrySchema = import_zod16.z.object({
|
|
1257
|
+
at: import_zod16.z.string().min(1),
|
|
1258
|
+
by: import_zod16.z.string().min(1),
|
|
1259
|
+
operation: import_zod16.z.string().min(1),
|
|
1260
|
+
conceptId: import_zod16.z.string().min(1),
|
|
1261
|
+
/** Second concept id, where the operation relates two — supersession. */
|
|
1262
|
+
target: import_zod16.z.string().min(1).optional()
|
|
1263
|
+
}).strict();
|
|
1264
|
+
function renderLogEntry(entry) {
|
|
1265
|
+
return `${JSON.stringify(kbLogEntrySchema.parse(entry))}
|
|
1266
|
+
`;
|
|
1267
|
+
}
|
|
1268
|
+
function parseLog(raw) {
|
|
1269
|
+
const entries = [];
|
|
1270
|
+
const malformed = [];
|
|
1271
|
+
raw.split("\n").forEach((text, index) => {
|
|
1272
|
+
if (!text.trim()) return;
|
|
1273
|
+
let value;
|
|
1274
|
+
try {
|
|
1275
|
+
value = JSON.parse(text);
|
|
1276
|
+
} catch {
|
|
1277
|
+
malformed.push({ line: index + 1, text });
|
|
1278
|
+
return;
|
|
1279
|
+
}
|
|
1280
|
+
const parsed = kbLogEntrySchema.safeParse(value);
|
|
1281
|
+
if (!parsed.success) {
|
|
1282
|
+
malformed.push({ line: index + 1, text });
|
|
1283
|
+
return;
|
|
1284
|
+
}
|
|
1285
|
+
entries.push(parsed.data);
|
|
1286
|
+
});
|
|
1287
|
+
return { entries, malformed };
|
|
1288
|
+
}
|
|
1289
|
+
|
|
1290
|
+
// src/json-schema.ts
|
|
1291
|
+
function kbJsonSchemas() {
|
|
1292
|
+
return {
|
|
1293
|
+
recordFrontmatter: import_zod17.z.toJSONSchema(kbRecordFrontmatterSchema, {
|
|
1294
|
+
io: "input"
|
|
1295
|
+
}),
|
|
1296
|
+
composeInput: import_zod17.z.toJSONSchema(composeInputSchema, { io: "input" }),
|
|
1297
|
+
logEntry: import_zod17.z.toJSONSchema(kbLogEntrySchema, { io: "input" })
|
|
1298
|
+
};
|
|
1299
|
+
}
|
|
1300
|
+
|
|
1301
|
+
// src/commands/schema.ts
|
|
1302
|
+
var schemaCommand = define({
|
|
1303
|
+
name: "schema",
|
|
1304
|
+
tool: "kb_schema",
|
|
1305
|
+
usage: "schema",
|
|
1306
|
+
description: "JSON Schema for the frontmatter, the write input, and log entries \u2014 generated from the code that enforces them, so it cannot drift from what a write will accept.",
|
|
1307
|
+
input: import_zod18.z.object({}),
|
|
1308
|
+
fromArgv: () => ({}),
|
|
1309
|
+
run: () => Promise.resolve(kbJsonSchemas())
|
|
1310
|
+
});
|
|
1311
|
+
|
|
1312
|
+
// src/commands/status.ts
|
|
1313
|
+
var import_zod19 = require("zod");
|
|
1314
|
+
var statusCommand = define({
|
|
1315
|
+
name: "status",
|
|
1316
|
+
tool: "kb_status",
|
|
1317
|
+
usage: "status <concept-id> <status>",
|
|
1318
|
+
description: "Move a record's status, leaving everything else alone. Uses a compare-and-swap, so a concurrent change fails loudly rather than being overwritten.",
|
|
1319
|
+
input: import_zod19.z.object({
|
|
1320
|
+
bundlePath,
|
|
1321
|
+
conceptId,
|
|
1322
|
+
status: import_zod19.z.enum(KB_RECORD_STATUSES)
|
|
1323
|
+
}),
|
|
1324
|
+
fromArgv: (argv, path) => ({
|
|
1325
|
+
bundlePath: path,
|
|
1326
|
+
conceptId: argv[1],
|
|
1327
|
+
status: argv[2]
|
|
1328
|
+
}),
|
|
1329
|
+
run: async ({ store, actor }, { bundlePath: path, conceptId: id, status }) => {
|
|
1330
|
+
await assertBaseNotFrozen(process.cwd(), path);
|
|
1331
|
+
const record = await store.setStatus(path, id, status, actor);
|
|
1332
|
+
return { conceptId: record.conceptId, status };
|
|
1333
|
+
}
|
|
1334
|
+
});
|
|
1335
|
+
|
|
1336
|
+
// src/commands/supersede.ts
|
|
1337
|
+
var import_zod20 = require("zod");
|
|
1338
|
+
var supersedeCommand = define({
|
|
1339
|
+
name: "supersede",
|
|
1340
|
+
tool: "kb_supersede",
|
|
1341
|
+
usage: "supersede <concept-id> <replacement-id>",
|
|
1342
|
+
description: "Mark a record superseded by another, linking both directions. Use this rather than editing a record whose meaning changed \u2014 a record that quietly becomes something else invalidates every reference to it, and the earlier understanding is what a later trace needs.",
|
|
1343
|
+
input: import_zod20.z.object({ bundlePath, conceptId, replacementId: conceptId }),
|
|
1344
|
+
fromArgv: (argv, path) => ({
|
|
1345
|
+
bundlePath: path,
|
|
1346
|
+
conceptId: argv[1],
|
|
1347
|
+
replacementId: argv[2]
|
|
668
1348
|
}),
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
...depth ? { depth } : {}
|
|
688
|
-
})).map((step) => ({
|
|
689
|
-
conceptId: step.record.conceptId,
|
|
690
|
-
at: step.record.frontmatter.generated?.at ?? null,
|
|
691
|
-
status: step.record.frontmatter.strauss_status,
|
|
692
|
-
title: step.record.frontmatter.title ?? null,
|
|
693
|
-
depth: step.depth,
|
|
694
|
-
via: step.via,
|
|
695
|
-
body: step.record.body
|
|
696
|
-
}))
|
|
1349
|
+
run: async ({ store, actor }, { bundlePath: path, conceptId: id, replacementId }) => {
|
|
1350
|
+
await assertBaseNotFrozen(process.cwd(), path);
|
|
1351
|
+
await store.supersede(path, id, replacementId, actor);
|
|
1352
|
+
return { superseded: id, replacedBy: replacementId };
|
|
1353
|
+
}
|
|
1354
|
+
});
|
|
1355
|
+
|
|
1356
|
+
// src/commands/sync-instructions.ts
|
|
1357
|
+
var import_zod21 = require("zod");
|
|
1358
|
+
var syncInstructionsCommand = define({
|
|
1359
|
+
name: "sync-instructions",
|
|
1360
|
+
usage: "sync-instructions <file> [--profile NAME] [--budget N] [--full-under N]",
|
|
1361
|
+
description: "Idempotently plant the `context` block between sentinel comments in an instruction file (AGENTS.md, CLAUDE.md), creating the block when absent and leaving everything outside the sentinels alone. CLI-only: this is file plumbing for runtimes whose instruction files are re-read where their conversations are not, not an agent capability \u2014 the capability is kb_context.",
|
|
1362
|
+
input: import_zod21.z.object({
|
|
1363
|
+
file: import_zod21.z.string().min(1).describe("The instruction file to edit in place."),
|
|
1364
|
+
budgetTokens: import_zod21.z.number().int().positive().optional(),
|
|
1365
|
+
fullUnderTokens: import_zod21.z.number().int().positive().optional(),
|
|
1366
|
+
profile: import_zod21.z.string().optional()
|
|
697
1367
|
}),
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
1368
|
+
fromArgv: (argv) => {
|
|
1369
|
+
const budget = argvFlag(argv, "--budget");
|
|
1370
|
+
const fullUnder = argvFlag(argv, "--full-under");
|
|
1371
|
+
const profile = argvFlag(argv, "--profile");
|
|
1372
|
+
return {
|
|
1373
|
+
file: argv[1],
|
|
1374
|
+
...budget ? { budgetTokens: Number(budget) } : {},
|
|
1375
|
+
...fullUnder ? { fullUnderTokens: Number(fullUnder) } : {},
|
|
1376
|
+
...profile ? { profile } : {}
|
|
1377
|
+
};
|
|
1378
|
+
},
|
|
1379
|
+
run: async ({ store }, { file, budgetTokens, fullUnderTokens, profile }) => {
|
|
1380
|
+
const result = await buildContext(store, process.cwd(), {
|
|
1381
|
+
...budgetTokens ? { budgetTokens } : {},
|
|
1382
|
+
...fullUnderTokens ? { fullUnderTokens } : {},
|
|
1383
|
+
...profile ? { profile } : {},
|
|
1384
|
+
warn: (entry) => process.stderr.write(`${JSON.stringify(entry)}
|
|
1385
|
+
`)
|
|
1386
|
+
});
|
|
1387
|
+
return syncInstructions(file, result.block);
|
|
1388
|
+
}
|
|
1389
|
+
});
|
|
1390
|
+
|
|
1391
|
+
// src/commands/trace.ts
|
|
1392
|
+
var import_zod22 = require("zod");
|
|
1393
|
+
|
|
1394
|
+
// src/trace.ts
|
|
1395
|
+
var TRACE_EDGES = ["supersession", "anchor", "source"];
|
|
1396
|
+
function trace(seedId, bundle, options = {}) {
|
|
1397
|
+
const edges = options.edges?.length ? options.edges : TRACE_EDGES;
|
|
1398
|
+
const maxDepth = options.depth ?? 3;
|
|
1399
|
+
const byId = new Map(bundle.map((record) => [record.conceptId, record]));
|
|
1400
|
+
const seed = byId.get(seedId);
|
|
1401
|
+
if (!seed) return [];
|
|
1402
|
+
const reached = /* @__PURE__ */ new Map([
|
|
1403
|
+
[seedId, { record: seed, depth: 0, via: [] }]
|
|
1404
|
+
]);
|
|
1405
|
+
let frontier = [seed];
|
|
1406
|
+
for (let depth = 1; depth <= maxDepth && frontier.length; depth += 1) {
|
|
1407
|
+
const next = [];
|
|
1408
|
+
for (const from of frontier) {
|
|
1409
|
+
for (const edge of edges) {
|
|
1410
|
+
for (const record of neighbours(from, bundle, edge)) {
|
|
1411
|
+
const existing = reached.get(record.conceptId);
|
|
1412
|
+
if (existing) {
|
|
1413
|
+
if (existing.depth > 0 && !existing.via.includes(edge)) {
|
|
1414
|
+
existing.via.push(edge);
|
|
1415
|
+
}
|
|
1416
|
+
continue;
|
|
1417
|
+
}
|
|
1418
|
+
reached.set(record.conceptId, { record, depth, via: [edge] });
|
|
1419
|
+
next.push(record);
|
|
1420
|
+
}
|
|
1421
|
+
}
|
|
1422
|
+
}
|
|
1423
|
+
frontier = next;
|
|
1424
|
+
}
|
|
1425
|
+
return [...reached.values()].sort(byGeneratedAt);
|
|
1426
|
+
}
|
|
1427
|
+
function neighbours(from, bundle, edge) {
|
|
1428
|
+
switch (edge) {
|
|
1429
|
+
case "supersession":
|
|
1430
|
+
return bundle.filter(
|
|
1431
|
+
(candidate) => candidate.conceptId !== from.conceptId && (candidate.conceptId === from.frontmatter.strauss_superseded_by || from.frontmatter.strauss_supersedes?.includes(
|
|
1432
|
+
candidate.conceptId
|
|
1433
|
+
) || candidate.frontmatter.strauss_superseded_by === from.conceptId || candidate.frontmatter.strauss_supersedes?.includes(from.conceptId))
|
|
1434
|
+
);
|
|
1435
|
+
// The edge that answers "why is this code shaped this way": every record
|
|
1436
|
+
// attached to the same file or symbol, whatever its standing.
|
|
1437
|
+
case "anchor": {
|
|
1438
|
+
const mine = from.frontmatter.strauss_anchors ?? [];
|
|
1439
|
+
if (!mine.length) return [];
|
|
1440
|
+
return bundle.filter(
|
|
1441
|
+
(candidate) => candidate.conceptId !== from.conceptId && (candidate.frontmatter.strauss_anchors ?? []).some(
|
|
1442
|
+
(theirs) => mine.some((ours) => anchorsTouch(ours, theirs))
|
|
1443
|
+
)
|
|
1444
|
+
);
|
|
1445
|
+
}
|
|
1446
|
+
case "source": {
|
|
1447
|
+
const mine = new Set((from.frontmatter.sources ?? []).map((s) => s.id));
|
|
1448
|
+
if (!mine.size) return [];
|
|
1449
|
+
return bundle.filter(
|
|
1450
|
+
(candidate) => candidate.conceptId !== from.conceptId && (candidate.frontmatter.sources ?? []).some(
|
|
1451
|
+
(source) => mine.has(source.id)
|
|
1452
|
+
)
|
|
1453
|
+
);
|
|
1454
|
+
}
|
|
1455
|
+
}
|
|
1456
|
+
}
|
|
1457
|
+
function anchorsTouch(left, right) {
|
|
1458
|
+
if (left.file !== right.file) return false;
|
|
1459
|
+
if (!left.symbol || !right.symbol) return true;
|
|
1460
|
+
return left.symbol === right.symbol;
|
|
1461
|
+
}
|
|
1462
|
+
function byGeneratedAt(left, right) {
|
|
1463
|
+
const at = (step) => step.record.frontmatter.generated?.at ?? "";
|
|
1464
|
+
return at(left).localeCompare(at(right)) || left.depth - right.depth;
|
|
1465
|
+
}
|
|
1466
|
+
|
|
1467
|
+
// src/commands/trace.ts
|
|
1468
|
+
var traceCommand = define({
|
|
1469
|
+
name: "trace",
|
|
1470
|
+
tool: "kb_trace",
|
|
1471
|
+
usage: "trace <concept-id> [edges...]",
|
|
1472
|
+
description: 'How a position was arrived at, as a timeline ordered by when each record was written. Deliberately includes rejected, draft, and superseded records \u2014 in a history those are the content, not noise. Follows supersession, shared code anchors, and shared sources. Use when the question is "why is this the way it is" rather than "what do we hold now". This tool (with kb_load and kb_query) is the only supported way to read a base; a raw file read bypasses supersession resolution and returns replaced records as if current.',
|
|
1473
|
+
input: import_zod22.z.object({
|
|
1474
|
+
bundlePath,
|
|
1475
|
+
conceptId,
|
|
1476
|
+
edges: import_zod22.z.array(import_zod22.z.enum(TRACE_EDGES)).optional(),
|
|
1477
|
+
depth: import_zod22.z.number().int().positive().optional()
|
|
712
1478
|
}),
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
description: "The index, rebuilt if it disagrees with the records. One call gives the whole shape of the base: title, type, status, and description per record.",
|
|
718
|
-
input: import_zod6.z.object({ bundlePath }),
|
|
719
|
-
fromArgv: (_argv, path) => ({ bundlePath: path }),
|
|
720
|
-
run: ({ store }, { bundlePath: path }) => store.readIndex(path)
|
|
1479
|
+
fromArgv: (argv, path) => ({
|
|
1480
|
+
bundlePath: path,
|
|
1481
|
+
conceptId: argv[1],
|
|
1482
|
+
edges: argv.slice(2).filter((edge) => TRACE_EDGES.includes(edge))
|
|
721
1483
|
}),
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
1484
|
+
run: async ({ store }, { bundlePath: path, conceptId: id, edges, depth }) => (await store.trace(path, id, {
|
|
1485
|
+
...edges?.length ? { edges } : {},
|
|
1486
|
+
...depth ? { depth } : {}
|
|
1487
|
+
})).map((step) => ({
|
|
1488
|
+
conceptId: step.record.conceptId,
|
|
1489
|
+
at: step.record.frontmatter.generated?.at ?? null,
|
|
1490
|
+
status: step.record.frontmatter.strauss_status,
|
|
1491
|
+
title: step.record.frontmatter.title ?? null,
|
|
1492
|
+
depth: step.depth,
|
|
1493
|
+
via: step.via,
|
|
1494
|
+
body: step.record.body
|
|
1495
|
+
}))
|
|
1496
|
+
});
|
|
1497
|
+
|
|
1498
|
+
// src/commands/types.ts
|
|
1499
|
+
var import_zod23 = require("zod");
|
|
1500
|
+
var typesCommand = define({
|
|
1501
|
+
name: "types",
|
|
1502
|
+
tool: "kb_types",
|
|
1503
|
+
usage: "types",
|
|
1504
|
+
description: "The twelve record types with their purpose, body sections, and starting status. Read this before writing rather than guessing headings \u2014 a section the type does not define is rejected.",
|
|
1505
|
+
input: import_zod23.z.object({}),
|
|
1506
|
+
fromArgv: () => ({}),
|
|
1507
|
+
run: () => Promise.resolve(RECORD_TYPES)
|
|
1508
|
+
});
|
|
1509
|
+
|
|
1510
|
+
// src/commands/unpin.ts
|
|
1511
|
+
var import_zod24 = require("zod");
|
|
1512
|
+
var unpinCommand = define({
|
|
1513
|
+
name: "unpin",
|
|
1514
|
+
tool: "kb_unpin",
|
|
1515
|
+
usage: "unpin [bundle-path]",
|
|
1516
|
+
description: "Remove a base from every pin manifest layer that holds it \u2014 project, local, and user \u2014 because unpinned means gone, not still injected from another file. Reports which layers were touched.",
|
|
1517
|
+
input: import_zod24.z.object({ bundlePath }),
|
|
1518
|
+
fromArgv: (argv, path) => ({ bundlePath: argv[1] ?? path }),
|
|
1519
|
+
run: (_ctx, { bundlePath: path }) => unpinBase(process.cwd(), path)
|
|
1520
|
+
});
|
|
1521
|
+
|
|
1522
|
+
// src/commands/validate.ts
|
|
1523
|
+
var import_zod25 = require("zod");
|
|
1524
|
+
|
|
1525
|
+
// src/validate.ts
|
|
1526
|
+
function validateBundle(records) {
|
|
1527
|
+
const byId = new Map(records.map((record) => [record.conceptId, record]));
|
|
1528
|
+
const problems = [];
|
|
1529
|
+
const report = (check, conceptId2, note) => problems.push({ check, conceptId: conceptId2, note });
|
|
1530
|
+
for (const record of records) {
|
|
1531
|
+
const { conceptId: conceptId2, frontmatter: fm } = record;
|
|
1532
|
+
if (!isKbRecordType(fm.type)) {
|
|
1533
|
+
report("type", conceptId2, `unrecognised type "${fm.type}"`);
|
|
1534
|
+
}
|
|
1535
|
+
if (fm.strauss_status === "superseded") {
|
|
1536
|
+
const by = fm.strauss_superseded_by;
|
|
1537
|
+
if (!by) {
|
|
1538
|
+
report("superseded_by", conceptId2, "superseded with no replacement");
|
|
1539
|
+
} else if (!byId.has(by)) {
|
|
1540
|
+
report("superseded_by", conceptId2, `replacement ${by} is missing`);
|
|
1541
|
+
} else if (!byId.get(by)?.frontmatter.strauss_supersedes?.includes(conceptId2)) {
|
|
1542
|
+
report("backlink", by, `does not list ${conceptId2} in supersedes`);
|
|
1543
|
+
}
|
|
1544
|
+
}
|
|
1545
|
+
for (const old of fm.strauss_supersedes ?? []) {
|
|
1546
|
+
const previous = byId.get(old);
|
|
1547
|
+
if (!previous) {
|
|
1548
|
+
report("supersedes", conceptId2, `target ${old} is missing`);
|
|
1549
|
+
} else if (previous.frontmatter.strauss_status !== "superseded") {
|
|
1550
|
+
report("supersedes", conceptId2, `${old} is not marked superseded`);
|
|
1551
|
+
}
|
|
1552
|
+
}
|
|
1553
|
+
if (fm.strauss_assumption && fm.sources?.length) {
|
|
1554
|
+
report("assumption", conceptId2, "marked an assumption but cites sources");
|
|
1555
|
+
}
|
|
1556
|
+
}
|
|
1557
|
+
return problems;
|
|
1558
|
+
}
|
|
1559
|
+
|
|
1560
|
+
// src/commands/validate.ts
|
|
1561
|
+
var validateCommand = define({
|
|
1562
|
+
name: "validate",
|
|
1563
|
+
tool: "kb_validate",
|
|
1564
|
+
usage: "validate",
|
|
1565
|
+
description: "Check pointers no single record can see: supersession links that disagree between the two records, and assumptions that cite sources. Per-record shape is enforced on every read, so a problem here means someone edited a file by hand.",
|
|
1566
|
+
input: import_zod25.z.object({ bundlePath }),
|
|
1567
|
+
fromArgv: (_argv, path) => ({ bundlePath: path }),
|
|
1568
|
+
run: async ({ store }, { bundlePath: path }) => validateBundle(await store.list(path)),
|
|
1569
|
+
failsWhen: (result) => Array.isArray(result) && result.length > 0
|
|
1570
|
+
});
|
|
1571
|
+
|
|
1572
|
+
// src/commands/write.ts
|
|
1573
|
+
var import_zod26 = require("zod");
|
|
1574
|
+
var writeCommand = define({
|
|
1575
|
+
name: "write",
|
|
1576
|
+
tool: "kb_write",
|
|
1577
|
+
usage: "write <type> < record.json",
|
|
1578
|
+
description: [
|
|
1579
|
+
"Write one record. Search first \u2014 the same knowledge filed twice under different slugs is how a base rots, and a duplicate concept id is rejected rather than overwritten. Call kb_types for the sections each type accepts.",
|
|
1580
|
+
"",
|
|
1581
|
+
"Judgment the tool cannot enforce for you:",
|
|
1582
|
+
"- An unsourced claim is an `assumption` record with assumption: true, never a `fact` with a vague source. The distinction is what lets a later reader separate what was established from what was guessed.",
|
|
1583
|
+
"- When two records conflict, say so in a `risk`, an `open-question`, or a superseding `decision`. Quietly picking a winner destroys the disagreement, which is usually the useful part.",
|
|
1584
|
+
"- Prefer a new record over overloading an existing one, and keep each short. A record nobody finishes reading is not durable memory.",
|
|
1585
|
+
"- Records are never deleted; supersede instead, so the earlier reasoning stays inspectable."
|
|
1586
|
+
].join("\n"),
|
|
1587
|
+
input: import_zod26.z.object({
|
|
1588
|
+
bundlePath,
|
|
1589
|
+
type: import_zod26.z.enum(KB_RECORD_TYPES),
|
|
1590
|
+
input: composeInputSchema
|
|
730
1591
|
}),
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
description: "Check pointers no single record can see: supersession links that disagree between the two records, and assumptions that cite sources. Per-record shape is enforced on every read, so a problem here means someone edited a file by hand.",
|
|
736
|
-
input: import_zod6.z.object({ bundlePath }),
|
|
737
|
-
fromArgv: (_argv, path) => ({ bundlePath: path }),
|
|
738
|
-
run: async ({ store }, { bundlePath: path }) => validateBundle(await store.list(path)),
|
|
739
|
-
failsWhen: (result) => Array.isArray(result) && result.length > 0
|
|
1592
|
+
fromArgv: async (argv, path, stdin) => ({
|
|
1593
|
+
bundlePath: path,
|
|
1594
|
+
type: argv[1],
|
|
1595
|
+
input: JSON.parse(await stdin())
|
|
740
1596
|
}),
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
1597
|
+
run: async ({ store, actor, now }, { bundlePath: path, type, input }) => {
|
|
1598
|
+
await assertBaseNotFrozen(process.cwd(), path);
|
|
1599
|
+
const record = await store.write(
|
|
1600
|
+
path,
|
|
1601
|
+
composeRecord(type, input, actor, now()),
|
|
1602
|
+
actor
|
|
1603
|
+
);
|
|
1604
|
+
return { conceptId: record.conceptId };
|
|
1605
|
+
}
|
|
1606
|
+
});
|
|
1607
|
+
|
|
1608
|
+
// src/commands/write-decision.ts
|
|
1609
|
+
var import_zod27 = require("zod");
|
|
1610
|
+
var writeDecisionCommand = define({
|
|
1611
|
+
name: "write-decision",
|
|
1612
|
+
tool: "kb_write_decision",
|
|
1613
|
+
usage: "write-decision < decision.json",
|
|
1614
|
+
description: [
|
|
1615
|
+
"Write a decision. Takes `alternative` and `impact` as fields rather than free sections, because what was rejected is the part a later reader cannot reconstruct from the code \u2014 a heading is too easy to leave empty.",
|
|
1616
|
+
"",
|
|
1617
|
+
"What belongs in one:",
|
|
1618
|
+
'- Record a decision when a later reader would otherwise "simplify" the constraint away. If the diff already answers the question, there is nothing here to write.',
|
|
1619
|
+
"- `alternative` is what you turned down and why, not a list of everything considered.",
|
|
1620
|
+
"- A reference to material you read goes in `sources`; a reference to code goes in `anchors`; a reference to another record goes in `relatedConceptIds`."
|
|
1621
|
+
].join("\n"),
|
|
1622
|
+
input: import_zod27.z.object({ bundlePath, input: decisionInputSchema }),
|
|
1623
|
+
fromArgv: async (_argv, path, stdin) => ({
|
|
1624
|
+
bundlePath: path,
|
|
1625
|
+
input: JSON.parse(await stdin())
|
|
749
1626
|
}),
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
}
|
|
1627
|
+
run: async ({ store, actor, now }, { bundlePath: path, input }) => {
|
|
1628
|
+
await assertBaseNotFrozen(process.cwd(), path);
|
|
1629
|
+
const record = await store.write(
|
|
1630
|
+
path,
|
|
1631
|
+
composeDecisionRecord(input, actor, now()),
|
|
1632
|
+
actor
|
|
1633
|
+
);
|
|
1634
|
+
return { conceptId: record.conceptId };
|
|
1635
|
+
}
|
|
1636
|
+
});
|
|
1637
|
+
|
|
1638
|
+
// src/commands/index.ts
|
|
1639
|
+
var KB_COMMANDS = [
|
|
1640
|
+
writeCommand,
|
|
1641
|
+
writeDecisionCommand,
|
|
1642
|
+
noDecisionCommand,
|
|
1643
|
+
statusCommand,
|
|
1644
|
+
supersedeCommand,
|
|
1645
|
+
answerCommand,
|
|
1646
|
+
loadCommand,
|
|
1647
|
+
queryCommand,
|
|
1648
|
+
traceCommand,
|
|
1649
|
+
listCommand,
|
|
1650
|
+
readIndexCommand,
|
|
1651
|
+
logCommand,
|
|
1652
|
+
validateCommand,
|
|
1653
|
+
schemaCommand,
|
|
1654
|
+
pinCommand,
|
|
1655
|
+
unpinCommand,
|
|
1656
|
+
pinsCommand,
|
|
1657
|
+
contextCommand,
|
|
1658
|
+
syncInstructionsCommand,
|
|
1659
|
+
typesCommand
|
|
759
1660
|
];
|
|
760
1661
|
var KB_COMMANDS_BY_NAME = new Map(
|
|
761
1662
|
KB_COMMANDS.map((command) => [command.name, command])
|
|
@@ -763,8 +1664,8 @@ var KB_COMMANDS_BY_NAME = new Map(
|
|
|
763
1664
|
|
|
764
1665
|
// src/kb-store.ts
|
|
765
1666
|
var import_node_crypto = require("crypto");
|
|
766
|
-
var
|
|
767
|
-
var
|
|
1667
|
+
var import_promises4 = require("fs/promises");
|
|
1668
|
+
var import_node_path6 = require("path");
|
|
768
1669
|
|
|
769
1670
|
// src/markdown.ts
|
|
770
1671
|
var import_gray_matter = __toESM(require("gray-matter"), 1);
|
|
@@ -871,121 +1772,9 @@ var KbInvalidConceptIdError = class extends BaseError {
|
|
|
871
1772
|
}
|
|
872
1773
|
};
|
|
873
1774
|
|
|
874
|
-
// src/kb-index.ts
|
|
875
|
-
var INDEX_FILE = "INDEX.md";
|
|
876
|
-
var HEADING = "# KB Index";
|
|
877
|
-
function renderIndex(records) {
|
|
878
|
-
const lines = [...records].sort((left, right) => left.conceptId.localeCompare(right.conceptId)).map((record) => {
|
|
879
|
-
const { frontmatter: fm } = record;
|
|
880
|
-
const parts = [fm.type, fm.strauss_status];
|
|
881
|
-
if (fm.tags?.length) parts.push(`tags: ${fm.tags.join(", ")}`);
|
|
882
|
-
if (fm.description) parts.push(fm.description);
|
|
883
|
-
return `- [${fm.title ?? record.conceptId}](${record.conceptId}.md) \u2014 ${parts.join(" \xB7 ")}`;
|
|
884
|
-
});
|
|
885
|
-
return `${HEADING}
|
|
886
|
-
|
|
887
|
-
${lines.join("\n")}
|
|
888
|
-
`;
|
|
889
|
-
}
|
|
890
|
-
function indexIsStale(stored, expected) {
|
|
891
|
-
return stored !== expected;
|
|
892
|
-
}
|
|
893
|
-
|
|
894
|
-
// src/adjudicate.ts
|
|
895
|
-
var STANDING = {
|
|
896
|
-
accepted: "current",
|
|
897
|
-
resolved: "current",
|
|
898
|
-
draft: "unsettled",
|
|
899
|
-
proposed: "unsettled",
|
|
900
|
-
open: "open",
|
|
901
|
-
rejected: "rejected",
|
|
902
|
-
superseded: "superseded"
|
|
903
|
-
};
|
|
904
|
-
function adjudicate(hits, bundle, now = /* @__PURE__ */ new Date()) {
|
|
905
|
-
const byId = new Map(bundle.map((record) => [record.conceptId, record]));
|
|
906
|
-
return hits.map((record) => {
|
|
907
|
-
const status = record.frontmatter.strauss_status;
|
|
908
|
-
const warnings = [];
|
|
909
|
-
let heads = [];
|
|
910
|
-
if (status === "superseded") {
|
|
911
|
-
const resolved = resolveHeads(record, byId);
|
|
912
|
-
heads = resolved.heads;
|
|
913
|
-
warnings.push(...resolved.warnings);
|
|
914
|
-
if (heads.length) {
|
|
915
|
-
warnings.push({
|
|
916
|
-
kind: "superseded",
|
|
917
|
-
by: heads.map((head) => head.conceptId)
|
|
918
|
-
});
|
|
919
|
-
}
|
|
920
|
-
} else if (status === "rejected") {
|
|
921
|
-
warnings.push({ kind: "rejected" });
|
|
922
|
-
} else if (status === "draft" || status === "proposed") {
|
|
923
|
-
warnings.push({ kind: "unsettled", status });
|
|
924
|
-
} else if (status === "open") {
|
|
925
|
-
warnings.push({ kind: "unresolved-question" });
|
|
926
|
-
}
|
|
927
|
-
const staleAfter = record.frontmatter.stale_after;
|
|
928
|
-
if (staleAfter && Date.parse(staleAfter) < now.getTime()) {
|
|
929
|
-
warnings.push({ kind: "stale", staleAfter });
|
|
930
|
-
}
|
|
931
|
-
if (!record.frontmatter.verified?.length) {
|
|
932
|
-
warnings.push({ kind: "unverified" });
|
|
933
|
-
}
|
|
934
|
-
return { record, standing: STANDING[status], heads, warnings };
|
|
935
|
-
});
|
|
936
|
-
}
|
|
937
|
-
function resolveHeads(from, byId) {
|
|
938
|
-
const warnings = [];
|
|
939
|
-
const heads = /* @__PURE__ */ new Map();
|
|
940
|
-
const seen = /* @__PURE__ */ new Set([from.conceptId]);
|
|
941
|
-
const queue = [from];
|
|
942
|
-
while (queue.length) {
|
|
943
|
-
const current = queue.shift();
|
|
944
|
-
const next = successors(current, byId);
|
|
945
|
-
for (const missing of next.missing) {
|
|
946
|
-
warnings.push({ kind: "broken-chain", missing });
|
|
947
|
-
}
|
|
948
|
-
if (!next.records.length) {
|
|
949
|
-
if (current.conceptId !== from.conceptId)
|
|
950
|
-
heads.set(current.conceptId, current);
|
|
951
|
-
continue;
|
|
952
|
-
}
|
|
953
|
-
for (const record of next.records) {
|
|
954
|
-
if (seen.has(record.conceptId)) {
|
|
955
|
-
warnings.push({ kind: "chain-cycle", through: [...seen] });
|
|
956
|
-
continue;
|
|
957
|
-
}
|
|
958
|
-
seen.add(record.conceptId);
|
|
959
|
-
queue.push(record);
|
|
960
|
-
}
|
|
961
|
-
}
|
|
962
|
-
if (heads.size > 1) {
|
|
963
|
-
warnings.push({ kind: "forked-chain", heads: [...heads.keys()] });
|
|
964
|
-
}
|
|
965
|
-
return { heads: [...heads.values()], warnings };
|
|
966
|
-
}
|
|
967
|
-
function successors(record, byId) {
|
|
968
|
-
const ids = /* @__PURE__ */ new Set();
|
|
969
|
-
const forward = record.frontmatter.strauss_superseded_by;
|
|
970
|
-
if (forward) ids.add(forward);
|
|
971
|
-
for (const [id, candidate] of byId) {
|
|
972
|
-
if (candidate.frontmatter.strauss_supersedes?.includes(record.conceptId)) {
|
|
973
|
-
ids.add(id);
|
|
974
|
-
}
|
|
975
|
-
}
|
|
976
|
-
const records = [];
|
|
977
|
-
const missing = [];
|
|
978
|
-
for (const id of ids) {
|
|
979
|
-
const found = byId.get(id);
|
|
980
|
-
if (found) records.push(found);
|
|
981
|
-
else missing.push(id);
|
|
982
|
-
}
|
|
983
|
-
return { records, missing };
|
|
984
|
-
}
|
|
985
|
-
|
|
986
1775
|
// src/search-index.ts
|
|
987
|
-
var
|
|
988
|
-
var
|
|
1776
|
+
var import_promises3 = require("fs/promises");
|
|
1777
|
+
var import_node_path5 = require("path");
|
|
989
1778
|
var SEARCH_INDEX_FILE = ".index.sqlite";
|
|
990
1779
|
var COLLECTION = "kb";
|
|
991
1780
|
async function searchBase(bundlePath2, query, options = {}) {
|
|
@@ -994,7 +1783,7 @@ async function searchBase(bundlePath2, query, options = {}) {
|
|
|
994
1783
|
let store = null;
|
|
995
1784
|
try {
|
|
996
1785
|
store = await qmd.createStore({
|
|
997
|
-
dbPath: (0,
|
|
1786
|
+
dbPath: (0, import_node_path5.join)(bundlePath2, SEARCH_INDEX_FILE),
|
|
998
1787
|
config: {
|
|
999
1788
|
collections: {
|
|
1000
1789
|
[COLLECTION]: {
|
|
@@ -1029,13 +1818,13 @@ async function searchBase(bundlePath2, query, options = {}) {
|
|
|
1029
1818
|
}
|
|
1030
1819
|
}
|
|
1031
1820
|
async function isStale(bundlePath2) {
|
|
1032
|
-
const indexAt = await (0,
|
|
1821
|
+
const indexAt = await (0, import_promises3.stat)((0, import_node_path5.join)(bundlePath2, SEARCH_INDEX_FILE)).then((s) => s.mtimeMs).catch(() => 0);
|
|
1033
1822
|
if (!indexAt) return true;
|
|
1034
1823
|
const { readdir: readdir2 } = await import("fs/promises");
|
|
1035
1824
|
const names = await readdir2(bundlePath2).catch(() => []);
|
|
1036
1825
|
for (const name of names) {
|
|
1037
1826
|
if (!name.endsWith(".md") || name === INDEX_FILE) continue;
|
|
1038
|
-
const at = await (0,
|
|
1827
|
+
const at = await (0, import_promises3.stat)((0, import_node_path5.join)(bundlePath2, name)).then((s) => s.mtimeMs).catch(() => 0);
|
|
1039
1828
|
if (at > indexAt) return true;
|
|
1040
1829
|
}
|
|
1041
1830
|
return false;
|
|
@@ -1070,7 +1859,7 @@ async function loadQmd(logger) {
|
|
|
1070
1859
|
}
|
|
1071
1860
|
|
|
1072
1861
|
// src/kb-store.ts
|
|
1073
|
-
var KB_DIR = (0,
|
|
1862
|
+
var KB_DIR = (0, import_node_path6.join)(".strauss", "kb");
|
|
1074
1863
|
var STORE_OWNED = /* @__PURE__ */ new Set([INDEX_FILE, LOG_FILE, SEARCH_INDEX_FILE]);
|
|
1075
1864
|
var DEFAULT_LOAD_BUDGET = 25e3;
|
|
1076
1865
|
var KbStore = class {
|
|
@@ -1101,7 +1890,7 @@ var KbStore = class {
|
|
|
1101
1890
|
const conceptId2 = `${input.type}.${input.slug}`;
|
|
1102
1891
|
const root = this.root(bundlePath2);
|
|
1103
1892
|
const target = this.recordPath(bundlePath2, conceptId2);
|
|
1104
|
-
await (0,
|
|
1893
|
+
await (0, import_promises4.mkdir)(root, { recursive: true });
|
|
1105
1894
|
await this.publish(
|
|
1106
1895
|
target,
|
|
1107
1896
|
stringifyMarkdownWithFrontmatter(input.body, frontmatter),
|
|
@@ -1126,7 +1915,7 @@ var KbStore = class {
|
|
|
1126
1915
|
const target = this.recordPath(bundlePath2, conceptId2);
|
|
1127
1916
|
let raw;
|
|
1128
1917
|
try {
|
|
1129
|
-
raw = await (0,
|
|
1918
|
+
raw = await (0, import_promises4.readFile)(target, "utf8");
|
|
1130
1919
|
} catch {
|
|
1131
1920
|
return null;
|
|
1132
1921
|
}
|
|
@@ -1143,14 +1932,14 @@ var KbStore = class {
|
|
|
1143
1932
|
const root = this.root(bundlePath2);
|
|
1144
1933
|
let names;
|
|
1145
1934
|
try {
|
|
1146
|
-
names = await (0,
|
|
1935
|
+
names = await (0, import_promises4.readdir)(root);
|
|
1147
1936
|
} catch {
|
|
1148
1937
|
return [];
|
|
1149
1938
|
}
|
|
1150
1939
|
const wanted = names.sort().filter((name) => name.endsWith(".md") && !STORE_OWNED.has(name)).map((name) => ({ name, conceptId: name.slice(0, -".md".length) })).filter(({ conceptId: conceptId2 }) => !type || conceptId2.startsWith(`${type}.`));
|
|
1151
1940
|
const records = await Promise.all(
|
|
1152
1941
|
wanted.map(
|
|
1153
|
-
async ({ name, conceptId: conceptId2 }) => this.parse(conceptId2, await (0,
|
|
1942
|
+
async ({ name, conceptId: conceptId2 }) => this.parse(conceptId2, await (0, import_promises4.readFile)((0, import_node_path6.join)(root, name), "utf8"))
|
|
1154
1943
|
)
|
|
1155
1944
|
);
|
|
1156
1945
|
return records.filter((record) => record !== null);
|
|
@@ -1288,19 +2077,19 @@ ${answer}
|
|
|
1288
2077
|
const adjudicated = adjudicate(wanted, bundle);
|
|
1289
2078
|
const records = adjudicated.filter((hit) => hit.standing !== "superseded");
|
|
1290
2079
|
const superseded = adjudicated.filter((hit) => hit.standing === "superseded").map(stub);
|
|
1291
|
-
const
|
|
1292
|
-
if (
|
|
2080
|
+
const approxTokens2 = records.reduce((total, hit) => total + estimateTokens(hit.record), 0) + superseded.reduce((total, entry) => total + estimateStubTokens(entry), 0);
|
|
2081
|
+
if (approxTokens2 > budgetTokens) {
|
|
1293
2082
|
return {
|
|
1294
2083
|
loaded: false,
|
|
1295
2084
|
recordCount: wanted.length,
|
|
1296
|
-
approxTokens,
|
|
2085
|
+
approxTokens: approxTokens2,
|
|
1297
2086
|
budgetTokens
|
|
1298
2087
|
};
|
|
1299
2088
|
}
|
|
1300
2089
|
return {
|
|
1301
2090
|
loaded: true,
|
|
1302
2091
|
recordCount: wanted.length,
|
|
1303
|
-
approxTokens,
|
|
2092
|
+
approxTokens: approxTokens2,
|
|
1304
2093
|
budgetTokens,
|
|
1305
2094
|
records,
|
|
1306
2095
|
superseded
|
|
@@ -1320,11 +2109,11 @@ ${answer}
|
|
|
1320
2109
|
async readIndex(bundlePath2) {
|
|
1321
2110
|
const root = this.root(bundlePath2);
|
|
1322
2111
|
const expected = renderIndex(await this.list(bundlePath2));
|
|
1323
|
-
const stored = await (0,
|
|
2112
|
+
const stored = await (0, import_promises4.readFile)((0, import_node_path6.join)(root, INDEX_FILE), "utf8").catch(
|
|
1324
2113
|
() => null
|
|
1325
2114
|
);
|
|
1326
2115
|
if (indexIsStale(stored, expected)) {
|
|
1327
|
-
await this.publish((0,
|
|
2116
|
+
await this.publish((0, import_node_path6.join)(root, INDEX_FILE), expected, true, INDEX_FILE);
|
|
1328
2117
|
this.logger.info?.({
|
|
1329
2118
|
operation: "kb.index.repair",
|
|
1330
2119
|
bundlePath: root,
|
|
@@ -1341,8 +2130,8 @@ ${answer}
|
|
|
1341
2130
|
* knows which agent touched what. So a bad line is surfaced and left alone.
|
|
1342
2131
|
*/
|
|
1343
2132
|
async readLog(bundlePath2) {
|
|
1344
|
-
const raw = await (0,
|
|
1345
|
-
(0,
|
|
2133
|
+
const raw = await (0, import_promises4.readFile)(
|
|
2134
|
+
(0, import_node_path6.join)(this.root(bundlePath2), LOG_FILE),
|
|
1346
2135
|
"utf8"
|
|
1347
2136
|
).catch(() => "");
|
|
1348
2137
|
const result = parseLog(raw);
|
|
@@ -1357,14 +2146,14 @@ ${answer}
|
|
|
1357
2146
|
}
|
|
1358
2147
|
async mutate(bundlePath2, conceptId2, change, entry, changeBody = (body) => body) {
|
|
1359
2148
|
const target = this.recordPath(bundlePath2, conceptId2);
|
|
1360
|
-
const before = await (0,
|
|
2149
|
+
const before = await (0, import_promises4.readFile)(target, "utf8").catch(() => null);
|
|
1361
2150
|
if (before === null) throw new KbRecordNotFoundError(conceptId2);
|
|
1362
2151
|
const parsed = this.parse(conceptId2, before);
|
|
1363
2152
|
if (!parsed) throw new KbRecordNotFoundError(conceptId2);
|
|
1364
2153
|
const frontmatter = change(parsed.frontmatter);
|
|
1365
2154
|
const body = changeBody(parsed.body);
|
|
1366
2155
|
const contents = stringifyMarkdownWithFrontmatter(body, frontmatter);
|
|
1367
|
-
const witness = await (0,
|
|
2156
|
+
const witness = await (0, import_promises4.readFile)(target, "utf8").catch(() => null);
|
|
1368
2157
|
if (witness === null || digest(witness) !== digest(before)) {
|
|
1369
2158
|
throw new KbWriteConflictError(conceptId2);
|
|
1370
2159
|
}
|
|
@@ -1390,26 +2179,26 @@ ${answer}
|
|
|
1390
2179
|
*/
|
|
1391
2180
|
async publish(target, contents, overwrite, conceptId2) {
|
|
1392
2181
|
const staging = `${target}.${process.pid}.tmp`;
|
|
1393
|
-
await (0,
|
|
2182
|
+
await (0, import_promises4.writeFile)(staging, contents, "utf8");
|
|
1394
2183
|
try {
|
|
1395
2184
|
if (overwrite) {
|
|
1396
|
-
await (0,
|
|
2185
|
+
await (0, import_promises4.rename)(staging, target);
|
|
1397
2186
|
return;
|
|
1398
2187
|
}
|
|
1399
|
-
await (0,
|
|
2188
|
+
await (0, import_promises4.link)(staging, target);
|
|
1400
2189
|
} catch (error) {
|
|
1401
2190
|
if (error.code === "EEXIST") {
|
|
1402
2191
|
throw new KbRecordAlreadyExistsError(conceptId2);
|
|
1403
2192
|
}
|
|
1404
2193
|
throw error;
|
|
1405
2194
|
} finally {
|
|
1406
|
-
await (0,
|
|
2195
|
+
await (0, import_promises4.unlink)(staging).catch(() => void 0);
|
|
1407
2196
|
}
|
|
1408
2197
|
}
|
|
1409
2198
|
/** Appends one log line. Failing to log must not fail the mutation. */
|
|
1410
2199
|
async record(root, entry) {
|
|
1411
2200
|
const line = renderLogEntry({ at: (/* @__PURE__ */ new Date()).toISOString(), ...entry });
|
|
1412
|
-
await (0,
|
|
2201
|
+
await (0, import_promises4.appendFile)((0, import_node_path6.join)(root, LOG_FILE), line, "utf8").catch((error) => {
|
|
1413
2202
|
this.logger.warn?.({
|
|
1414
2203
|
operation: "kb.log.append",
|
|
1415
2204
|
outcome: "failed",
|
|
@@ -1435,18 +2224,18 @@ ${answer}
|
|
|
1435
2224
|
};
|
|
1436
2225
|
}
|
|
1437
2226
|
root(bundlePath2) {
|
|
1438
|
-
return (0,
|
|
2227
|
+
return (0, import_node_path6.resolve)(bundlePath2);
|
|
1439
2228
|
}
|
|
1440
2229
|
// Concept ids are `<type>.<slug>` and map to a single file directly under the
|
|
1441
2230
|
// bundle root; anything carrying a separator would escape it.
|
|
1442
2231
|
recordPath(bundlePath2, conceptId2) {
|
|
1443
|
-
if (conceptId2.includes(
|
|
2232
|
+
if (conceptId2.includes(import_node_path6.sep) || conceptId2.includes("/")) {
|
|
1444
2233
|
throw new KbInvalidConceptIdError(
|
|
1445
2234
|
"concept id must not contain a path separator",
|
|
1446
2235
|
{ conceptId: conceptId2 }
|
|
1447
2236
|
);
|
|
1448
2237
|
}
|
|
1449
|
-
return (0,
|
|
2238
|
+
return (0, import_node_path6.join)(this.root(bundlePath2), `${conceptId2}.md`);
|
|
1450
2239
|
}
|
|
1451
2240
|
};
|
|
1452
2241
|
function estimateTokens(record) {
|
|
@@ -1505,6 +2294,7 @@ async function runKbCli(argv) {
|
|
|
1505
2294
|
parsed.data
|
|
1506
2295
|
);
|
|
1507
2296
|
if (command.failsWhen?.(result)) process.exitCode = 1;
|
|
2297
|
+
if (result === "") return;
|
|
1508
2298
|
process.stdout.write(
|
|
1509
2299
|
typeof result === "string" ? result.endsWith("\n") ? result : `${result}
|
|
1510
2300
|
` : `${JSON.stringify(result, null, 2)}
|
|
@@ -1514,18 +2304,18 @@ async function runKbCli(argv) {
|
|
|
1514
2304
|
function takeBundle(argv) {
|
|
1515
2305
|
const at = argv.indexOf("--bundle");
|
|
1516
2306
|
if (at === -1) {
|
|
1517
|
-
return { bundle: (0,
|
|
2307
|
+
return { bundle: (0, import_node_path7.join)(process.cwd(), KB_DIR), rest: argv };
|
|
1518
2308
|
}
|
|
1519
2309
|
const bundle = argv[at + 1];
|
|
1520
2310
|
if (!bundle) die("--bundle requires a path");
|
|
1521
2311
|
return { bundle, rest: [...argv.slice(0, at), ...argv.slice(at + 2)] };
|
|
1522
2312
|
}
|
|
1523
2313
|
function readStdin() {
|
|
1524
|
-
return new Promise((
|
|
2314
|
+
return new Promise((resolve5, reject) => {
|
|
1525
2315
|
let text = "";
|
|
1526
2316
|
process.stdin.setEncoding("utf8");
|
|
1527
2317
|
process.stdin.on("data", (chunk) => text += chunk);
|
|
1528
|
-
process.stdin.on("end", () =>
|
|
2318
|
+
process.stdin.on("end", () => resolve5(text));
|
|
1529
2319
|
process.stdin.on("error", reject);
|
|
1530
2320
|
});
|
|
1531
2321
|
}
|