pi-braid 0.43.5 → 0.43.7
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/CHANGELOG.md +95 -7
- package/build-info.json +4 -4
- package/dist/extensions/core/prompt-builder/surface-discipline-section.d.ts +1 -1
- package/dist/extensions/core/prompt-builder/surface-discipline-section.js +1 -1
- package/dist/extensions/core/skills-tool/applicability.d.ts +134 -0
- package/dist/extensions/core/skills-tool/applicability.js +161 -0
- package/dist/extensions/core/skills-tool/auto-inject.d.ts +10 -0
- package/dist/extensions/core/skills-tool/auto-inject.js +216 -1
- package/dist/extensions/core/skills-tool/index.js +64 -3
- package/dist/extensions/core/skills-tool/loading-guidance.js +2 -1
- package/dist/extensions/core/skills-tool/session-records.d.ts +297 -8
- package/dist/extensions/core/skills-tool/session-records.js +509 -5
- package/dist/extensions/core/skills-tool/skill-families.d.ts +60 -0
- package/dist/extensions/core/skills-tool/skill-families.js +105 -0
- package/dist/extensions/orchestrator/index.js +11 -1
- package/docs/LOCAL_DEV.md +1 -1
- package/package.json +1 -1
- package/src/extensions/core/AGENTS.md +1 -0
- package/src/extensions/core/prompt-builder/surface-discipline-section.ts +1 -1
- package/src/extensions/core/skills-tool/applicability.ts +220 -0
- package/src/extensions/core/skills-tool/auto-inject.ts +274 -0
- package/src/extensions/core/skills-tool/index.ts +75 -2
- package/src/extensions/core/skills-tool/loading-guidance.ts +2 -1
- package/src/extensions/core/skills-tool/session-records.ts +740 -5
- package/src/extensions/core/skills-tool/skill-families.ts +112 -0
- package/src/extensions/orchestrator/index.ts +13 -1
|
@@ -1,8 +1,18 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Durable per-session skill
|
|
2
|
+
* Durable per-session skill records + the monthly adoption rollup.
|
|
3
3
|
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
4
|
+
* The store is one append-only JSONL file with three record kinds, told apart
|
|
5
|
+
* by `kind`:
|
|
6
|
+
*
|
|
7
|
+
* (absent) an ADOPTION record, the older shape:
|
|
8
|
+
* { sessionId, role, month, hadSkillLoad, updatedAt }
|
|
9
|
+
* "applicability" an APPLICABILITY record: the session's skill verdicts
|
|
10
|
+
* (see ApplicabilityRecord)
|
|
11
|
+
* "obligation" an OBLIGATION record: one skill's legs in one session
|
|
12
|
+
* (see ObligationLegRecord)
|
|
13
|
+
*
|
|
14
|
+
* An adoption record exists for every session that reaches an agent turn, so
|
|
15
|
+
* every such session gets ONE logical record (the rollup denominator);
|
|
6
16
|
* `role` carries the session's profile name from the braid config (see
|
|
7
17
|
* lib/config/session-profiles.ts), or the neutral default when no profile
|
|
8
18
|
* matches; `month` is the local-time "YYYY-MM" of the write, and
|
|
@@ -22,6 +32,13 @@
|
|
|
22
32
|
* filesystems; the tolerant reader skips unparseable lines, the window is
|
|
23
33
|
* a single small write, and this is accepted as best-effort telemetry.
|
|
24
34
|
*
|
|
35
|
+
* Each reader reads its own kind and ignores the others. The adoption reader
|
|
36
|
+
* is fail-closed on an unknown `kind`, so a record type added later cannot
|
|
37
|
+
* enter the adoption numbers by looking enough like one; and a build that
|
|
38
|
+
* predates the applicability kind reads those lines as adoption records of
|
|
39
|
+
* the same session, which folds to the same numbers because it carries no
|
|
40
|
+
* new sessions and never sets `hadSkillLoad`.
|
|
41
|
+
*
|
|
25
42
|
* Back-compat: records written before profiles existed carry a role from
|
|
26
43
|
* the retired fixed set; those values pass the profile-name gate
|
|
27
44
|
* unchanged, so older buckets keep folding under their original names.
|
|
@@ -34,6 +51,7 @@ import { createLogger } from "../../../lib/core/logger.js";
|
|
|
34
51
|
import { getExtensionState } from "../../../lib/state/extension-state.js";
|
|
35
52
|
import { getSkillsSessionsFilePath } from "../../../lib/config/braid-paths.js";
|
|
36
53
|
import { NEUTRAL_SESSION_PROFILE, normalizeProfileName, resolveSessionProfileForCwd, } from "../../../lib/config/session-profiles.js";
|
|
54
|
+
import { MAX_APPLICABILITY_EVIDENCE } from "./applicability.js";
|
|
37
55
|
const log = createLogger("skills-tool");
|
|
38
56
|
/** Local-time calendar month of a date, "YYYY-MM". */
|
|
39
57
|
export function monthOf(date) {
|
|
@@ -45,6 +63,10 @@ function normalizeRecord(raw) {
|
|
|
45
63
|
if (!raw || typeof raw !== "object" || Array.isArray(raw))
|
|
46
64
|
return undefined;
|
|
47
65
|
const r = raw;
|
|
66
|
+
// One file, several record kinds: the adoption reader accepts its own kind
|
|
67
|
+
// (no `kind`, or an explicit "adoption") and refuses anything else.
|
|
68
|
+
if (r.kind !== undefined && r.kind !== "adoption")
|
|
69
|
+
return undefined;
|
|
48
70
|
if (typeof r.sessionId !== "string" || !r.sessionId)
|
|
49
71
|
return undefined;
|
|
50
72
|
if (typeof r.month !== "string" || !/^\d{4}-\d{2}$/.test(r.month))
|
|
@@ -108,11 +130,11 @@ export function readSessionRecords(storePath) {
|
|
|
108
130
|
return out;
|
|
109
131
|
}
|
|
110
132
|
/**
|
|
111
|
-
* Append one record. Best-effort: every error is swallowed (this runs on
|
|
133
|
+
* Append one record line. Best-effort: every error is swallowed (this runs on
|
|
112
134
|
* the agent-loop and tool-result paths — a telemetry failure must never
|
|
113
135
|
* break a turn). Returns true when the line landed on disk.
|
|
114
136
|
*/
|
|
115
|
-
|
|
137
|
+
function appendLine(record, storePath) {
|
|
116
138
|
try {
|
|
117
139
|
mkdirSync(dirname(storePath), { recursive: true });
|
|
118
140
|
appendFileSync(storePath, `${JSON.stringify(record)}\n`, "utf-8");
|
|
@@ -123,6 +145,10 @@ export function appendSessionRecord(record, storePath) {
|
|
|
123
145
|
return false;
|
|
124
146
|
}
|
|
125
147
|
}
|
|
148
|
+
/** Append one adoption record (see appendLine for the failure contract). */
|
|
149
|
+
export function appendSessionRecord(record, storePath) {
|
|
150
|
+
return appendLine(record, storePath);
|
|
151
|
+
}
|
|
126
152
|
/** Warned once per process about a refused adoption-record append. */
|
|
127
153
|
let _warnedRecordFailure = false;
|
|
128
154
|
function reportRecordFailure(err) {
|
|
@@ -206,6 +232,484 @@ export function recordSessionSkillLoad(ctx, storePath) {
|
|
|
206
232
|
reportRecordFailure(err);
|
|
207
233
|
}
|
|
208
234
|
}
|
|
235
|
+
/** One evidence row, normalizing an unknown source to "no source recorded". */
|
|
236
|
+
function normalizeEvidence(raw) {
|
|
237
|
+
if (!raw || typeof raw !== "object" || Array.isArray(raw))
|
|
238
|
+
return undefined;
|
|
239
|
+
const e = raw;
|
|
240
|
+
if (typeof e.phrase !== "string" || !e.phrase)
|
|
241
|
+
return undefined;
|
|
242
|
+
return e.source === "frontmatter" || e.source === "heading" || e.source === "description"
|
|
243
|
+
? { phrase: e.phrase, source: e.source }
|
|
244
|
+
: { phrase: e.phrase };
|
|
245
|
+
}
|
|
246
|
+
/**
|
|
247
|
+
* Normalize one raw applicability line; undefined when the line is not one.
|
|
248
|
+
* Field junk is coerced to a safe default rather than trusted, matching the
|
|
249
|
+
* adoption reader's posture.
|
|
250
|
+
*/
|
|
251
|
+
function normalizeApplicabilityRecord(raw) {
|
|
252
|
+
if (!raw || typeof raw !== "object" || Array.isArray(raw))
|
|
253
|
+
return undefined;
|
|
254
|
+
const r = raw;
|
|
255
|
+
if (r.kind !== "applicability")
|
|
256
|
+
return undefined;
|
|
257
|
+
if (typeof r.sessionId !== "string" || !r.sessionId)
|
|
258
|
+
return undefined;
|
|
259
|
+
if (typeof r.month !== "string" || !/^\d{4}-\d{2}$/.test(r.month))
|
|
260
|
+
return undefined;
|
|
261
|
+
const base = {
|
|
262
|
+
kind: "applicability",
|
|
263
|
+
sessionId: r.sessionId,
|
|
264
|
+
role: normalizeProfileName(r.role) ?? NEUTRAL_SESSION_PROFILE,
|
|
265
|
+
month: r.month,
|
|
266
|
+
updatedAt: typeof r.updatedAt === "string" ? r.updatedAt : "",
|
|
267
|
+
};
|
|
268
|
+
if (typeof r.skill === "string" && r.skill) {
|
|
269
|
+
if (r.applicability !== "read" && r.applicability !== "ignored")
|
|
270
|
+
return undefined;
|
|
271
|
+
const evidence = [];
|
|
272
|
+
for (const item of Array.isArray(r.evidence) ? r.evidence : []) {
|
|
273
|
+
const normalized = normalizeEvidence(item);
|
|
274
|
+
if (normalized)
|
|
275
|
+
evidence.push(normalized);
|
|
276
|
+
}
|
|
277
|
+
return { ...base, skill: r.skill, applicability: r.applicability, evidence };
|
|
278
|
+
}
|
|
279
|
+
const count = (value) => typeof value === "number" && Number.isFinite(value) && value >= 0 ? value : 0;
|
|
280
|
+
return {
|
|
281
|
+
...base,
|
|
282
|
+
taskChars: count(r.taskChars),
|
|
283
|
+
evaluated: count(r.evaluated),
|
|
284
|
+
undeclared: (Array.isArray(r.undeclared) ? r.undeclared : []).filter((name) => typeof name === "string" && name !== ""),
|
|
285
|
+
};
|
|
286
|
+
}
|
|
287
|
+
/**
|
|
288
|
+
* Read the applicability records from the store. Missing or unreadable file →
|
|
289
|
+
* empty (never throws); non-applicability and corrupt lines are skipped.
|
|
290
|
+
*/
|
|
291
|
+
export function readApplicabilityRecords(storePath) {
|
|
292
|
+
let text;
|
|
293
|
+
try {
|
|
294
|
+
text = readFileSync(storePath, "utf-8");
|
|
295
|
+
}
|
|
296
|
+
catch {
|
|
297
|
+
return []; // missing or unreadable → no records
|
|
298
|
+
}
|
|
299
|
+
const out = [];
|
|
300
|
+
for (const line of text.split("\n")) {
|
|
301
|
+
const trimmed = line.trim();
|
|
302
|
+
if (!trimmed)
|
|
303
|
+
continue;
|
|
304
|
+
try {
|
|
305
|
+
const rec = normalizeApplicabilityRecord(JSON.parse(trimmed));
|
|
306
|
+
if (rec)
|
|
307
|
+
out.push(rec);
|
|
308
|
+
}
|
|
309
|
+
catch {
|
|
310
|
+
// torn or corrupt line — skip
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
return out;
|
|
314
|
+
}
|
|
315
|
+
/**
|
|
316
|
+
* Merge two verdicts for the same skill: `read` beats `ignored` (a read is
|
|
317
|
+
* evidence the skill's content reached the session, whatever the turn order
|
|
318
|
+
* was), and the evidence unions by (phrase, source), bounded like every other
|
|
319
|
+
* evidence surface. Pure.
|
|
320
|
+
*/
|
|
321
|
+
function mergeVerdict(current, next) {
|
|
322
|
+
if (!current)
|
|
323
|
+
return next;
|
|
324
|
+
const byPhrase = new Map();
|
|
325
|
+
for (const evidence of [...current.evidence, ...next.evidence]) {
|
|
326
|
+
const key = `${evidence.phrase}\u0000${evidence.source ?? ""}`;
|
|
327
|
+
if (!byPhrase.has(key) && byPhrase.size < MAX_APPLICABILITY_EVIDENCE) {
|
|
328
|
+
byPhrase.set(key, evidence);
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
return {
|
|
332
|
+
name: next.name,
|
|
333
|
+
verdict: current.verdict === "read" || next.verdict === "read" ? "read" : "ignored",
|
|
334
|
+
evidence: [...byPhrase.values()],
|
|
335
|
+
};
|
|
336
|
+
}
|
|
337
|
+
/**
|
|
338
|
+
* Fold the store into one logical record per sessionId: the newest census wins
|
|
339
|
+
* the session facts, verdict rows union across writes with `read` beating
|
|
340
|
+
* `ignored`. Pure. The fold is what makes a torn, duplicated or out-of-order
|
|
341
|
+
* append harmless: the record set, not the line order, decides a verdict.
|
|
342
|
+
*/
|
|
343
|
+
export function foldApplicabilityRecords(records) {
|
|
344
|
+
const byId = new Map();
|
|
345
|
+
const empty = (rec) => ({
|
|
346
|
+
sessionId: rec.sessionId,
|
|
347
|
+
role: rec.role,
|
|
348
|
+
month: rec.month,
|
|
349
|
+
updatedAt: rec.updatedAt,
|
|
350
|
+
taskChars: 0,
|
|
351
|
+
evaluated: 0,
|
|
352
|
+
undeclared: [],
|
|
353
|
+
verdicts: [],
|
|
354
|
+
classes: { read: 0, ignored: 0, notApplicable: 0 },
|
|
355
|
+
});
|
|
356
|
+
for (const rec of records) {
|
|
357
|
+
const prev = byId.get(rec.sessionId) ?? empty(rec);
|
|
358
|
+
// Session facts come from the newest CENSUS row; verdict rows carry no
|
|
359
|
+
// census of their own and must not erase it.
|
|
360
|
+
const newer = rec.updatedAt >= prev.updatedAt;
|
|
361
|
+
const verdicts = new Map(prev.verdicts.map((v) => [v.name, v]));
|
|
362
|
+
if ("skill" in rec) {
|
|
363
|
+
verdicts.set(rec.skill, mergeVerdict(verdicts.get(rec.skill), {
|
|
364
|
+
name: rec.skill,
|
|
365
|
+
verdict: rec.applicability,
|
|
366
|
+
evidence: rec.evidence,
|
|
367
|
+
}));
|
|
368
|
+
}
|
|
369
|
+
const census = "evaluated" in rec ? (newer ? rec : undefined) : undefined;
|
|
370
|
+
byId.set(rec.sessionId, {
|
|
371
|
+
sessionId: rec.sessionId,
|
|
372
|
+
role: newer ? rec.role : prev.role,
|
|
373
|
+
month: newer ? rec.month : prev.month,
|
|
374
|
+
updatedAt: newer ? rec.updatedAt : prev.updatedAt,
|
|
375
|
+
taskChars: census ? census.taskChars : prev.taskChars,
|
|
376
|
+
evaluated: census ? census.evaluated : prev.evaluated,
|
|
377
|
+
undeclared: census ? census.undeclared : prev.undeclared,
|
|
378
|
+
verdicts: [...verdicts.values()],
|
|
379
|
+
classes: { read: 0, ignored: 0, notApplicable: 0 },
|
|
380
|
+
});
|
|
381
|
+
}
|
|
382
|
+
for (const rec of byId.values()) {
|
|
383
|
+
const read = rec.verdicts.filter((v) => v.verdict === "read").length;
|
|
384
|
+
const ignored = rec.verdicts.length - read;
|
|
385
|
+
rec.classes = { read, ignored, notApplicable: Math.max(0, rec.evaluated - read - ignored) };
|
|
386
|
+
}
|
|
387
|
+
return [...byId.values()];
|
|
388
|
+
}
|
|
389
|
+
/** Resolve the session identity a record needs, or undefined for a fake ctx. */
|
|
390
|
+
function applicabilityBase(ctx) {
|
|
391
|
+
const sess = resolveSession(ctx);
|
|
392
|
+
if (!sess)
|
|
393
|
+
return undefined;
|
|
394
|
+
return {
|
|
395
|
+
sessionId: sess.sessionId,
|
|
396
|
+
role: getSessionProfile(ctx).name,
|
|
397
|
+
month: monthOf(new Date()),
|
|
398
|
+
updatedAt: new Date().toISOString(),
|
|
399
|
+
};
|
|
400
|
+
}
|
|
401
|
+
/**
|
|
402
|
+
* Append the session's census row. Best-effort like every other writer here: a
|
|
403
|
+
* false return means the line did not land, never a thrown turn.
|
|
404
|
+
*/
|
|
405
|
+
export function recordApplicabilityCensus(ctx, census, storePath) {
|
|
406
|
+
try {
|
|
407
|
+
const base = applicabilityBase(ctx);
|
|
408
|
+
if (!base)
|
|
409
|
+
return false;
|
|
410
|
+
return appendLine({ kind: "applicability", ...base, ...census }, storePath ?? getSkillsSessionsFilePath());
|
|
411
|
+
}
|
|
412
|
+
catch (err) {
|
|
413
|
+
reportRecordFailure(err);
|
|
414
|
+
return false;
|
|
415
|
+
}
|
|
416
|
+
}
|
|
417
|
+
/**
|
|
418
|
+
* Append one skill's verdict row. The same function writes the `read` upgrade:
|
|
419
|
+
* the fold decides, so no line is ever rewritten.
|
|
420
|
+
*/
|
|
421
|
+
export function recordApplicabilityVerdict(ctx, verdict, storePath) {
|
|
422
|
+
try {
|
|
423
|
+
const base = applicabilityBase(ctx);
|
|
424
|
+
if (!base)
|
|
425
|
+
return false;
|
|
426
|
+
return appendLine({ kind: "applicability", ...base, ...verdict }, storePath ?? getSkillsSessionsFilePath());
|
|
427
|
+
}
|
|
428
|
+
catch (err) {
|
|
429
|
+
reportRecordFailure(err);
|
|
430
|
+
return false;
|
|
431
|
+
}
|
|
432
|
+
}
|
|
433
|
+
/**
|
|
434
|
+
* The folded applicability record for the context's own session, or undefined
|
|
435
|
+
* when the session wrote none. Reader: a refused path or a torn store is a
|
|
436
|
+
* miss, not a throw.
|
|
437
|
+
*/
|
|
438
|
+
export function getSessionApplicability(ctx, options = {}) {
|
|
439
|
+
try {
|
|
440
|
+
const sess = resolveSession(ctx);
|
|
441
|
+
if (!sess)
|
|
442
|
+
return undefined;
|
|
443
|
+
const storePath = options.storePath ?? getSkillsSessionsFilePath();
|
|
444
|
+
return foldApplicabilityRecords(readApplicabilityRecords(storePath)).find((rec) => rec.sessionId === sess.sessionId);
|
|
445
|
+
}
|
|
446
|
+
catch (err) {
|
|
447
|
+
reportRecordFailure(err);
|
|
448
|
+
return undefined;
|
|
449
|
+
}
|
|
450
|
+
}
|
|
451
|
+
/** Normalize a parsed JSON line into an obligation record, or null. */
|
|
452
|
+
export function normalizeObligationRecord(raw) {
|
|
453
|
+
if (!raw || typeof raw !== "object")
|
|
454
|
+
return null;
|
|
455
|
+
const r = raw;
|
|
456
|
+
if (r.kind !== "obligation")
|
|
457
|
+
return null;
|
|
458
|
+
if (typeof r.sessionId !== "string" || typeof r.skill !== "string" || !r.skill.trim()) {
|
|
459
|
+
return null;
|
|
460
|
+
}
|
|
461
|
+
return {
|
|
462
|
+
kind: "obligation",
|
|
463
|
+
sessionId: r.sessionId,
|
|
464
|
+
role: typeof r.role === "string" ? r.role : NEUTRAL_SESSION_PROFILE,
|
|
465
|
+
month: typeof r.month === "string" ? r.month : "",
|
|
466
|
+
updatedAt: typeof r.updatedAt === "string" ? r.updatedAt : "",
|
|
467
|
+
skill: r.skill,
|
|
468
|
+
suggested: r.suggested === true,
|
|
469
|
+
listed: r.listed === true,
|
|
470
|
+
read: r.read === true,
|
|
471
|
+
called: r.called === true,
|
|
472
|
+
applied: r.applied === true,
|
|
473
|
+
};
|
|
474
|
+
}
|
|
475
|
+
/**
|
|
476
|
+
* Read the obligation records from the store. Missing or unreadable file →
|
|
477
|
+
* empty (never throws); other kinds and corrupt lines are skipped.
|
|
478
|
+
*/
|
|
479
|
+
export function readObligationRecords(storePath = getSkillsSessionsFilePath()) {
|
|
480
|
+
let text;
|
|
481
|
+
try {
|
|
482
|
+
text = readFileSync(storePath, "utf-8");
|
|
483
|
+
}
|
|
484
|
+
catch {
|
|
485
|
+
return []; // missing or unreadable → no records
|
|
486
|
+
}
|
|
487
|
+
const out = [];
|
|
488
|
+
for (const line of text.split("\n")) {
|
|
489
|
+
const trimmed = line.trim();
|
|
490
|
+
if (!trimmed)
|
|
491
|
+
continue;
|
|
492
|
+
try {
|
|
493
|
+
const rec = normalizeObligationRecord(JSON.parse(trimmed));
|
|
494
|
+
if (rec)
|
|
495
|
+
out.push(rec);
|
|
496
|
+
}
|
|
497
|
+
catch {
|
|
498
|
+
// torn or corrupt line — skip
|
|
499
|
+
}
|
|
500
|
+
}
|
|
501
|
+
return out;
|
|
502
|
+
}
|
|
503
|
+
/**
|
|
504
|
+
* Fold a session's obligation rows into one record: the five legs OR across
|
|
505
|
+
* every row, so a row that saw one leg never erases a leg a later row did not
|
|
506
|
+
* have. `called`/`applied` are written once per skill, so the OR is exact
|
|
507
|
+
* rather than convenient. A torn or duplicated row folds; a malformed one is
|
|
508
|
+
* dropped at normalize, before this sees it.
|
|
509
|
+
*/
|
|
510
|
+
export function foldObligationRecords(records) {
|
|
511
|
+
const bySession = new Map();
|
|
512
|
+
for (const rec of records) {
|
|
513
|
+
let session = bySession.get(rec.sessionId);
|
|
514
|
+
if (!session) {
|
|
515
|
+
session = {
|
|
516
|
+
sessionId: rec.sessionId,
|
|
517
|
+
role: rec.role,
|
|
518
|
+
month: rec.month,
|
|
519
|
+
updatedAt: rec.updatedAt,
|
|
520
|
+
skills: [],
|
|
521
|
+
};
|
|
522
|
+
bySession.set(rec.sessionId, session);
|
|
523
|
+
}
|
|
524
|
+
else if (rec.updatedAt >= session.updatedAt) {
|
|
525
|
+
session.role = rec.role;
|
|
526
|
+
session.month = rec.month;
|
|
527
|
+
session.updatedAt = rec.updatedAt;
|
|
528
|
+
}
|
|
529
|
+
const held = session.skills.find((legs) => legs.skill === rec.skill);
|
|
530
|
+
if (!held) {
|
|
531
|
+
session.skills.push({
|
|
532
|
+
skill: rec.skill,
|
|
533
|
+
suggested: rec.suggested,
|
|
534
|
+
listed: rec.listed,
|
|
535
|
+
read: rec.read,
|
|
536
|
+
called: rec.called,
|
|
537
|
+
applied: rec.applied,
|
|
538
|
+
});
|
|
539
|
+
continue;
|
|
540
|
+
}
|
|
541
|
+
held.suggested = held.suggested || rec.suggested;
|
|
542
|
+
held.listed = held.listed || rec.listed;
|
|
543
|
+
held.read = held.read || rec.read;
|
|
544
|
+
held.called = held.called || rec.called;
|
|
545
|
+
held.applied = held.applied || rec.applied;
|
|
546
|
+
}
|
|
547
|
+
return [...bySession.values()];
|
|
548
|
+
}
|
|
549
|
+
/**
|
|
550
|
+
* Append one skill's legs. Best-effort like the sibling writer: a refused or
|
|
551
|
+
* missing store returns false so the caller can retry on a later turn.
|
|
552
|
+
*/
|
|
553
|
+
export function recordObligationLegs(ctx, legs, storePath) {
|
|
554
|
+
try {
|
|
555
|
+
const base = applicabilityBase(ctx);
|
|
556
|
+
if (!base)
|
|
557
|
+
return false;
|
|
558
|
+
const record = {
|
|
559
|
+
kind: "obligation",
|
|
560
|
+
...base,
|
|
561
|
+
skill: legs.skill,
|
|
562
|
+
suggested: legs.suggested,
|
|
563
|
+
listed: legs.listed,
|
|
564
|
+
read: legs.read,
|
|
565
|
+
called: legs.called,
|
|
566
|
+
applied: legs.applied,
|
|
567
|
+
};
|
|
568
|
+
return appendLine(record, storePath ?? getSkillsSessionsFilePath());
|
|
569
|
+
}
|
|
570
|
+
catch (err) {
|
|
571
|
+
reportRecordFailure(err);
|
|
572
|
+
return false;
|
|
573
|
+
}
|
|
574
|
+
}
|
|
575
|
+
/** The current session's obligation rows, folded. Missing store → undefined. */
|
|
576
|
+
export function getSessionObligation(ctx, options = {}) {
|
|
577
|
+
try {
|
|
578
|
+
const sess = resolveSession(ctx);
|
|
579
|
+
if (!sess)
|
|
580
|
+
return undefined;
|
|
581
|
+
const storePath = options.storePath ?? getSkillsSessionsFilePath();
|
|
582
|
+
return foldObligationRecords(readObligationRecords(storePath)).find((rec) => rec.sessionId === sess.sessionId);
|
|
583
|
+
}
|
|
584
|
+
catch (err) {
|
|
585
|
+
reportRecordFailure(err);
|
|
586
|
+
return undefined;
|
|
587
|
+
}
|
|
588
|
+
}
|
|
589
|
+
/**
|
|
590
|
+
* An obligation is satisfied when the body was read AND, if the session ever
|
|
591
|
+
* called into a family the skill declares, that the first call came after the
|
|
592
|
+
* read. Without a declared family the second clause is vacuous: reading the
|
|
593
|
+
* body is the whole obligation.
|
|
594
|
+
*/
|
|
595
|
+
export function obligationSatisfied(legs) {
|
|
596
|
+
return legs.read && (!legs.called || legs.applied);
|
|
597
|
+
}
|
|
598
|
+
/**
|
|
599
|
+
* Judge one session against its obligation rows. Pure.
|
|
600
|
+
*
|
|
601
|
+
* A skill is an obligation when the applicability fold carries a verdict for
|
|
602
|
+
* it: the previous record's question was whether the skill applied, and this
|
|
603
|
+
* one takes that answer as given. An obligation is decided by this record's
|
|
604
|
+
* legs alone, which is why the two kinds can be folded independently and
|
|
605
|
+
* joined on (sessionId, skill) without either re-reading the other.
|
|
606
|
+
*/
|
|
607
|
+
export function judgeSessionObligations(applicable, obligation) {
|
|
608
|
+
if (!applicable)
|
|
609
|
+
return undefined;
|
|
610
|
+
const applied = new Set(applicable.verdicts.map((verdict) => verdict.name));
|
|
611
|
+
const skills = (obligation?.skills ?? []).map((legs) => ({
|
|
612
|
+
...legs,
|
|
613
|
+
satisfied: applied.has(legs.skill) ? obligationSatisfied(legs) : null,
|
|
614
|
+
}));
|
|
615
|
+
// An applicable skill with no leg row still owes the read: a session that was
|
|
616
|
+
// never offered, listed, or read the body it owed is the ignored population,
|
|
617
|
+
// and dropping it from the denominator would report only the sessions that
|
|
618
|
+
// happened to be instrumented.
|
|
619
|
+
const seen = new Set(skills.map((legs) => legs.skill));
|
|
620
|
+
for (const skill of applied) {
|
|
621
|
+
if (seen.has(skill))
|
|
622
|
+
continue;
|
|
623
|
+
skills.push({
|
|
624
|
+
skill,
|
|
625
|
+
suggested: false,
|
|
626
|
+
listed: false,
|
|
627
|
+
read: false,
|
|
628
|
+
called: false,
|
|
629
|
+
applied: false,
|
|
630
|
+
satisfied: false,
|
|
631
|
+
});
|
|
632
|
+
}
|
|
633
|
+
if (!applicable.verdicts.length) {
|
|
634
|
+
return { skills, allSatisfied: false, obligations: 0 };
|
|
635
|
+
}
|
|
636
|
+
const obligations = skills.filter((legs) => legs.satisfied !== null);
|
|
637
|
+
return {
|
|
638
|
+
skills,
|
|
639
|
+
obligations: obligations.length,
|
|
640
|
+
allSatisfied: obligations.every((legs) => legs.satisfied === true),
|
|
641
|
+
};
|
|
642
|
+
}
|
|
643
|
+
/**
|
|
644
|
+
* The design's own decision rule: a gap this small makes the rate a duplicate
|
|
645
|
+
* of the read rate, and a duplicate should be deleted rather than shipped.
|
|
646
|
+
*/
|
|
647
|
+
export const FALSIFIER_GAP_POINTS = 5;
|
|
648
|
+
/** Compute both rates over one store's folds. Pure. */
|
|
649
|
+
export function computeObligationRate(sessions, obligations) {
|
|
650
|
+
const bySession = new Map(obligations.map((rec) => [rec.sessionId, rec]));
|
|
651
|
+
let population = 0;
|
|
652
|
+
let allSatisfied = 0;
|
|
653
|
+
let withRead = 0;
|
|
654
|
+
let obligationsSeen = 0;
|
|
655
|
+
let unsatisfied = 0;
|
|
656
|
+
let uninstrumented = 0;
|
|
657
|
+
for (const session of sessions) {
|
|
658
|
+
if (!session.verdicts.length)
|
|
659
|
+
continue;
|
|
660
|
+
const legs = bySession.get(session.sessionId);
|
|
661
|
+
if (!legs || !legs.skills.length) {
|
|
662
|
+
uninstrumented++;
|
|
663
|
+
continue;
|
|
664
|
+
}
|
|
665
|
+
const judgement = judgeSessionObligations(session, legs);
|
|
666
|
+
if (!judgement)
|
|
667
|
+
continue;
|
|
668
|
+
population++;
|
|
669
|
+
obligationsSeen += judgement.obligations;
|
|
670
|
+
if (judgement.allSatisfied)
|
|
671
|
+
allSatisfied++;
|
|
672
|
+
if (legs.skills.some((row) => row.read))
|
|
673
|
+
withRead++;
|
|
674
|
+
unsatisfied += judgement.skills.filter((row) => row.satisfied === false).length;
|
|
675
|
+
}
|
|
676
|
+
const allSatisfiedRate = population ? round4(allSatisfied / population) : 0;
|
|
677
|
+
const readRate = population ? round4(withRead / population) : 0;
|
|
678
|
+
const gapPoints = Math.round((allSatisfiedRate - readRate) * 10000) / 100;
|
|
679
|
+
return {
|
|
680
|
+
sessions: population,
|
|
681
|
+
allSatisfied,
|
|
682
|
+
allSatisfiedRate,
|
|
683
|
+
withRead,
|
|
684
|
+
readRate,
|
|
685
|
+
gapPoints,
|
|
686
|
+
obligations: obligationsSeen,
|
|
687
|
+
unsatisfied,
|
|
688
|
+
discriminating: Math.abs(gapPoints) >= FALSIFIER_GAP_POINTS,
|
|
689
|
+
uninstrumented,
|
|
690
|
+
};
|
|
691
|
+
}
|
|
692
|
+
function round4(n) {
|
|
693
|
+
return Math.round(n * 10000) / 10000;
|
|
694
|
+
}
|
|
695
|
+
/**
|
|
696
|
+
* The store-wide rate, or undefined when the store carries no obligation row
|
|
697
|
+
* at all. Undefined is the absent record: a reader of a store this build has
|
|
698
|
+
* not written to sees the payload it always saw, byte for byte.
|
|
699
|
+
*/
|
|
700
|
+
export function getObligationRate(options = {}) {
|
|
701
|
+
try {
|
|
702
|
+
const storePath = options.storePath ?? getSkillsSessionsFilePath();
|
|
703
|
+
const obligationRecords = readObligationRecords(storePath);
|
|
704
|
+
if (!obligationRecords.length)
|
|
705
|
+
return undefined;
|
|
706
|
+
return computeObligationRate(foldApplicabilityRecords(readApplicabilityRecords(storePath)), foldObligationRecords(obligationRecords));
|
|
707
|
+
}
|
|
708
|
+
catch (err) {
|
|
709
|
+
reportRecordFailure(err);
|
|
710
|
+
return undefined;
|
|
711
|
+
}
|
|
712
|
+
}
|
|
209
713
|
/**
|
|
210
714
|
* Compute the monthly adoption rollup for the current + previous 2
|
|
211
715
|
* months, keyed by session profile name. Pure (given records + now).
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The tool families a skill declares it governs.
|
|
3
|
+
*
|
|
4
|
+
* A trigger phrase says WHEN to open a skill file; nothing in the file says
|
|
5
|
+
* what the file's advice is then used FOR. `governs:` closes that: the tool
|
|
6
|
+
* families whose calls the body is written to direct. A family is the tool
|
|
7
|
+
* name's first underscore-delimited segment (`search_documents` → `search`),
|
|
8
|
+
* the same token `loading-guidance.ts` pairs with a skill name in the
|
|
9
|
+
* mechanical trigger, so "before the first call in the family" in the prompt
|
|
10
|
+
* and "a call in a declared family" in the record cannot mean two things.
|
|
11
|
+
*
|
|
12
|
+
* The declaration is authored data in the skill's own frontmatter — pi's
|
|
13
|
+
* parser already returns it, and the pack's authoring contract already admits
|
|
14
|
+
* any YAML-valid extra key. No new file format and no new field parser is
|
|
15
|
+
* needed, which is why this module only reads.
|
|
16
|
+
*
|
|
17
|
+
* Most skills govern nothing: advice about prose, a palette or a checklist
|
|
18
|
+
* directs no tool, and a file without the key is read as declaring no family.
|
|
19
|
+
* The absence is a fact, not a gap — an obligation that names no family is
|
|
20
|
+
* satisfied by the read alone.
|
|
21
|
+
*
|
|
22
|
+
* A declared family may not be the detector's own tool (`read`): a call to it
|
|
23
|
+
* IS the read leg, so counting it as an applied call would let an obligation
|
|
24
|
+
* satisfy itself. The parser drops such an entry; the shipped pack's contract
|
|
25
|
+
* refuses it by name so an author sees the problem before a release.
|
|
26
|
+
*
|
|
27
|
+
* Nothing here names a skill, a package or a path. Reads are memoized by file
|
|
28
|
+
* mtime + size, so a turn pays one stat per skill file and a parse only when
|
|
29
|
+
* a file changed.
|
|
30
|
+
*/
|
|
31
|
+
/**
|
|
32
|
+
* Max families read from one declaration. A body that directs more than four
|
|
33
|
+
* tool families is not one skill's job, and the record's per-session rows are
|
|
34
|
+
* bounded by the same number. The pack's contract mirrors this cap.
|
|
35
|
+
*/
|
|
36
|
+
export declare const MAX_GOVERNED_FAMILIES = 4;
|
|
37
|
+
/** The token shape a declaration must use: a tool name's first segment. */
|
|
38
|
+
export declare const GOVERNED_FAMILY_SHAPE: RegExp;
|
|
39
|
+
/**
|
|
40
|
+
* The tool whose call is the read leg itself. Never a declarable family: see
|
|
41
|
+
* the module header.
|
|
42
|
+
*/
|
|
43
|
+
export declare const DETECTOR_TOOL = "read";
|
|
44
|
+
/** The family a tool name belongs to: its first underscore-delimited segment. */
|
|
45
|
+
export declare function toolFamilyToken(toolName: string): string;
|
|
46
|
+
/**
|
|
47
|
+
* Normalize a frontmatter `governs` value into family tokens. Accepts a YAML
|
|
48
|
+
* list or a comma-separated string, lowercases, drops malformed, detector, and
|
|
49
|
+
* surplus entries, and dedupes in authored order. Pure.
|
|
50
|
+
*/
|
|
51
|
+
export declare function parseGovernedFamilies(value: unknown): string[];
|
|
52
|
+
/**
|
|
53
|
+
* Read the families a skill file declares. Missing or unreadable file, or
|
|
54
|
+
* malformed frontmatter, yields an empty list (a declaration that cannot be
|
|
55
|
+
* read is not a declaration). Memoized: an unchanged file never re-reads.
|
|
56
|
+
*/
|
|
57
|
+
export declare function readGovernedFamilies(filePath: string): string[];
|
|
58
|
+
/** Test seam: forget every memoized read. */
|
|
59
|
+
export declare function clearGovernedFamilyCache(): void;
|
|
60
|
+
//# sourceMappingURL=skill-families.d.ts.map
|