@lmzhen/dsh-evolution-core 0.53.0 → 0.55.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/{extract-plan-D4zO7dmD.js → extract-plan-ihQznLVT.js} +2 -1
- package/lib/extract.js +7 -7
- package/lib/index.js +795 -546
- package/lib/{limits-TD4Pv92Q.js → limits-DBg3UMkw.js} +11 -6
- package/lib/{skill-history-CZp8vhyL.js → mutation-storage-adapter-BM7mIJKp.js} +4 -647
- package/lib/mutations-1wLISSrY.js +592 -0
- package/lib/policy.js +9 -3
- package/lib/{quality-BLc6UQQb.js → quality-DNUVRbUm.js} +1 -1
- package/lib/{skill-health-BIpiPvw6.js → review-context-CXKwGGd8.js} +124 -183
- package/lib/runtime/plugin.js +92 -1
- package/lib/{session-projection-ByNxQi9O.js → session-projection-CbIKvDfJ.js} +3 -2
- package/lib/skill-history-B-DyIedq.js +646 -0
- package/lib/skills.js +12 -8
- package/lib/text-metrics-CyJhxWBs.js +132 -0
- package/lib/types/constants.d.ts +10 -5
- package/lib/types/constants.js +10 -5
- package/lib/types/drift-signals.d.ts +1 -4
- package/lib/types/drift-signals.js +14 -23
- package/lib/types/extract-request.js +3 -2
- package/lib/types/frontmatter.js +12 -8
- package/lib/types/index.d.ts +2 -1
- package/lib/types/index.js +2 -1
- package/lib/types/memory-store.d.ts +32 -6
- package/lib/types/memory-store.js +86 -30
- package/lib/types/mutation-unit.js +2 -1
- package/lib/types/params.js +6 -3
- package/lib/types/policy.d.ts +5 -0
- package/lib/types/policy.js +8 -2
- package/lib/types/prompts.d.ts +1 -1
- package/lib/types/prompts.js +1 -1
- package/lib/types/runtime/coherence.d.ts +27 -0
- package/lib/types/runtime/coherence.js +115 -0
- package/lib/types/runtime/plugin.js +2 -0
- package/lib/types/signals.js +3 -2
- package/lib/types/skill-health.d.ts +8 -5
- package/lib/types/skill-health.js +27 -28
- package/lib/types/skill-history.d.ts +2 -2
- package/lib/types/skill-history.js +7 -7
- package/lib/types/skill-lint.d.ts +1 -1
- package/lib/types/skill-lint.js +4 -4
- package/lib/types/skill-stats.d.ts +3 -1
- package/lib/types/skill-stats.js +2 -0
- package/lib/types/skill-store.js +2 -1
- package/lib/types/text-budgets.d.ts +150 -0
- package/lib/types/text-budgets.js +65 -0
- package/lib/types/text-metrics.d.ts +88 -0
- package/lib/types/text-metrics.js +131 -0
- package/lib/{usage-B1qveKi9.js → usage-e34VZPmK.js} +2 -515
- package/lib/write.js +3 -2
- package/package.json +1 -1
- package/lib/mutations-DQXddSJd.js +0 -75
- package/lib/types/cost.d.ts +0 -63
- package/lib/types/cost.js +0 -67
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { a as isProcessAlive,
|
|
2
|
-
import { r as contentHash } from "./mutations-
|
|
1
|
+
import { a as isProcessAlive, c as transactIo, i as isCommittedWarning, t as TransactionNotInvokedError } from "./io-GxghO3Ip.js";
|
|
2
|
+
import { l as exceedsContentLimit, r as contentHash } from "./mutations-1wLISSrY.js";
|
|
3
3
|
import { basename, dirname, isAbsolute, join, relative } from "node:path";
|
|
4
4
|
import { createHash, randomUUID } from "node:crypto";
|
|
5
5
|
//#region lib/types/write-kernel.js
|
|
@@ -305,7 +305,7 @@ function validateMutationRequest(request, limits) {
|
|
|
305
305
|
});
|
|
306
306
|
continue;
|
|
307
307
|
}
|
|
308
|
-
if (basename(target) === "SKILL.md" && op.content
|
|
308
|
+
if (basename(target) === "SKILL.md" && exceedsContentLimit(op.content, limits.maxSkillContentChars)) {
|
|
309
309
|
errors.push({
|
|
310
310
|
code: "skill-too-large",
|
|
311
311
|
message: "SKILL.md exceeds " + limits.maxSkillContentChars + " characters",
|
|
@@ -1263,647 +1263,4 @@ function unitJournalPort(io, home) {
|
|
|
1263
1263
|
} };
|
|
1264
1264
|
}
|
|
1265
1265
|
//#endregion
|
|
1266
|
-
|
|
1267
|
-
/**
|
|
1268
|
-
* Skill content history: content-addressed blobs plus a per-skill version index.
|
|
1269
|
-
*
|
|
1270
|
-
* Why it exists: `.mutations.json` records before/after HASHES and calls every automated edit
|
|
1271
|
-
* "reviewable and replayable" — but a hash cannot be replayed. With no content store, the only way
|
|
1272
|
-
* back was a whole-tree snapshot (`.backups`, keep=5) or `.archive` (removed skills only), so
|
|
1273
|
-
* reverting one skill cost the collateral of every other change in that window. This module keeps
|
|
1274
|
-
* the content, keyed by skill and version.
|
|
1275
|
-
*
|
|
1276
|
-
* Invariants (design `dsh-evolution-skill-history-design.md` §1):
|
|
1277
|
-
* - HISTORY IS HISTORY: the skill tree stays the single truth for "current content"; nothing here
|
|
1278
|
-
* feeds a judgment — callers read back content and version NUMBERS only.
|
|
1279
|
-
* - BEST-EFFORT, like the mutation audit: a failed history write warns and never fails the write
|
|
1280
|
-
* that already landed.
|
|
1281
|
-
* - Blobs are content-addressed, so identical bodies are stored once (across skills too).
|
|
1282
|
-
* - Versions are numbered by `v` (max+1), NOT by the clock: a moved system clock must not reorder
|
|
1283
|
-
* history (KiroCrew's `v<N>` choice, independently arrived at). Numbering and the index read-modify-write run
|
|
1284
|
-
* under the INDEX's own io lock, so two writers of one skill cannot mint the same number.
|
|
1285
|
-
* - The array's ORDER is append order, and the index is appended after the content lock is released, so two
|
|
1286
|
-
* writers of ONE skill can interleave their appends. Each entry therefore carries `beforeHash` — the content
|
|
1287
|
-
* it replaced, read under that writer's lock — and that link, not the position, is what `undo` follows
|
|
1288
|
-
* ({@link orderVersions} rebuilds the content order for display when the chain is complete).
|
|
1289
|
-
*
|
|
1290
|
-
* NOT here: blob garbage collection. Trimming drops INDEX entries; orphan blobs stay until a
|
|
1291
|
-
* sweeper collects them, because a reference count would have to span every skill's index and that
|
|
1292
|
-
* belongs to the curator's whole-library pass, not to the write path.
|
|
1293
|
-
* @module @lmzhen/dsh-evolution-core/skill-history
|
|
1294
|
-
*/
|
|
1295
|
-
/** Root-level history directory. Dot-prefixed so skill discovery skips it. */
|
|
1296
|
-
const HISTORY_DIR = ".history";
|
|
1297
|
-
/** Versioned index shape, so a future field can migrate without guessing. */
|
|
1298
|
-
const HISTORY_INDEX_VERSION = 1;
|
|
1299
|
-
/** The action label for a predecessor the index had never seen (content found on disk before this
|
|
1300
|
-
* feature existed, or a hand-edited tree): it says "this is what was there", not what wrote it. */
|
|
1301
|
-
const BASELINE_ACTION = "baseline";
|
|
1302
|
-
/** Default hard cap for a change summary, counted in Unicode code points. */
|
|
1303
|
-
const DEFAULT_SKILL_WRITE_SUMMARY_CHARS = 50;
|
|
1304
|
-
/** Count a summary the same way the write gate does (code points, not UTF-16 units). */
|
|
1305
|
-
function summaryChars(text) {
|
|
1306
|
-
return [...text].length;
|
|
1307
|
-
}
|
|
1308
|
-
/** Normalize a model-supplied summary: single line, trimmed, non-empty, within the hard cap. */
|
|
1309
|
-
function normalizeChangeSummary(value, cap = 50) {
|
|
1310
|
-
if (typeof value !== "string") return {
|
|
1311
|
-
ok: false,
|
|
1312
|
-
reason: "missing",
|
|
1313
|
-
length: 0
|
|
1314
|
-
};
|
|
1315
|
-
const text = value.replace(/\s+/g, " ").trim();
|
|
1316
|
-
if (text === "") return {
|
|
1317
|
-
ok: false,
|
|
1318
|
-
reason: "missing",
|
|
1319
|
-
length: 0
|
|
1320
|
-
};
|
|
1321
|
-
if (value.includes("\n") || value.includes("\r")) return {
|
|
1322
|
-
ok: false,
|
|
1323
|
-
reason: "multiline",
|
|
1324
|
-
length: summaryChars(text)
|
|
1325
|
-
};
|
|
1326
|
-
const length = summaryChars(text);
|
|
1327
|
-
return length <= cap ? {
|
|
1328
|
-
ok: true,
|
|
1329
|
-
text
|
|
1330
|
-
} : {
|
|
1331
|
-
ok: false,
|
|
1332
|
-
reason: "length",
|
|
1333
|
-
length
|
|
1334
|
-
};
|
|
1335
|
-
}
|
|
1336
|
-
/** Clip a host-generated summary to the same hard cap; host facts are not rejected. */
|
|
1337
|
-
function clipChangeSummary(text, cap = 50) {
|
|
1338
|
-
const line = text.replace(/\s+/g, " ").trim();
|
|
1339
|
-
const chars = [...line];
|
|
1340
|
-
return chars.length <= cap ? line : chars.slice(0, Math.max(1, cap - 1)).join("") + "…";
|
|
1341
|
-
}
|
|
1342
|
-
/** The root-level history directory. */
|
|
1343
|
-
function historyRoot(root) {
|
|
1344
|
-
return join(root, HISTORY_DIR);
|
|
1345
|
-
}
|
|
1346
|
-
/** Content-addressed blob path: identical bodies share one file, across skills too. */
|
|
1347
|
-
function blobPath(root, hash) {
|
|
1348
|
-
return join(historyRoot(root), "blobs", hash);
|
|
1349
|
-
}
|
|
1350
|
-
/** One skill's index file. */
|
|
1351
|
-
function historyIndexFile(root, name) {
|
|
1352
|
-
return join(historyRoot(root), "skills", name, "index.json");
|
|
1353
|
-
}
|
|
1354
|
-
/**
|
|
1355
|
-
* The versions in CONTENT order (oldest first), rebuilt from the chain links, for display.
|
|
1356
|
-
*
|
|
1357
|
-
* The stored array is append order, which interleaves when two writers of one skill audit out of
|
|
1358
|
-
* order; the links say what actually replaced what. The rebuild runs only when it can account for
|
|
1359
|
-
* EVERY entry (exactly one start and a forward link for each step) — otherwise the stored order is
|
|
1360
|
-
* returned unchanged, because a partial reconstruction would be a worse answer than the honest one.
|
|
1361
|
-
* @param versions - the stored index, oldest first as recorded.
|
|
1362
|
-
* @returns the same entries, in content order when the chain is complete.
|
|
1363
|
-
*/
|
|
1364
|
-
function orderVersions(versions) {
|
|
1365
|
-
if (versions.length < 2) return [...versions];
|
|
1366
|
-
const replaced = /* @__PURE__ */ new Set();
|
|
1367
|
-
for (const entry of versions) if (entry.beforeHash !== void 0) replaced.add(entry.beforeHash);
|
|
1368
|
-
const ends = versions.filter((entry) => !replaced.has(entry.hash));
|
|
1369
|
-
if (ends.length !== 1) return [...versions];
|
|
1370
|
-
const used = /* @__PURE__ */ new Set();
|
|
1371
|
-
const backward = [];
|
|
1372
|
-
let cursor = ends[0];
|
|
1373
|
-
while (cursor !== void 0 && !used.has(cursor)) {
|
|
1374
|
-
used.add(cursor);
|
|
1375
|
-
backward.push(cursor);
|
|
1376
|
-
const before = cursor.beforeHash;
|
|
1377
|
-
cursor = before === void 0 ? void 0 : [...versions].reverse().find((entry) => !used.has(entry) && entry.hash === before);
|
|
1378
|
-
}
|
|
1379
|
-
return backward.length === versions.length ? backward.reverse() : [...versions];
|
|
1380
|
-
}
|
|
1381
|
-
/** The actions that record a SUPPORT FILE's bytes (they share the skill's version index). */
|
|
1382
|
-
const SUPPORT_ACTIONS = ["write_file", "remove_file"];
|
|
1383
|
-
/** The actions that record the skill's own SKILL.md body. */
|
|
1384
|
-
const CONTENT_ACTIONS = [
|
|
1385
|
-
"create",
|
|
1386
|
-
"update",
|
|
1387
|
-
"patch",
|
|
1388
|
-
"consolidate",
|
|
1389
|
-
"restructure",
|
|
1390
|
-
"extract",
|
|
1391
|
-
"restore",
|
|
1392
|
-
"merge"
|
|
1393
|
-
];
|
|
1394
|
-
/**
|
|
1395
|
-
* Which artifact one recorded version holds, from the action that produced it.
|
|
1396
|
-
* @param action - the entry's action label.
|
|
1397
|
-
* @returns the artifact class; `other` when the label does not say.
|
|
1398
|
-
*/
|
|
1399
|
-
function versionTarget(action) {
|
|
1400
|
-
if (SUPPORT_ACTIONS.includes(action)) return "support";
|
|
1401
|
-
if (CONTENT_ACTIONS.includes(action)) return "content";
|
|
1402
|
-
return "other";
|
|
1403
|
-
}
|
|
1404
|
-
/**
|
|
1405
|
-
* Which artifact one INDEX ENTRY holds.
|
|
1406
|
-
*
|
|
1407
|
-
* {@link versionTarget} reads the ACTION, which is enough for the entries a write mints under its own
|
|
1408
|
-
* label and is NOT enough for a {@link BASELINE_ACTION}: a baseline minted for a support write holds
|
|
1409
|
-
* that FILE's previous bytes while its action says only "this is what was there". Two facts recover
|
|
1410
|
-
* the artifact:
|
|
1411
|
-
*
|
|
1412
|
-
* - `path` is written exactly when the bytes come from a support file, on BOTH entries one support
|
|
1413
|
-
* write can mint, so it decides on its own;
|
|
1414
|
-
* - an entry recorded before `path` existed has none, and the link is the evidence left: a support
|
|
1415
|
-
* write names the content it replaced through `beforeHash`, so a baseline that a support write
|
|
1416
|
-
* names as its predecessor holds that file's bytes.
|
|
1417
|
-
*
|
|
1418
|
-
* The inference runs only for a baseline. Every other entry keeps the action's verdict, which is also
|
|
1419
|
-
* the fail-closed one for an action this module does not know.
|
|
1420
|
-
* @param entry - one entry of a skill's index.
|
|
1421
|
-
* @param index - the index the entry came from: the legacy link is only readable across the whole
|
|
1422
|
-
* index. Omitting it answers from `path` and the action alone.
|
|
1423
|
-
* @returns the artifact class; `other` when nothing attributes the bytes.
|
|
1424
|
-
*/
|
|
1425
|
-
function entryTarget(entry, index = []) {
|
|
1426
|
-
if (entry.path !== void 0) return "support";
|
|
1427
|
-
if (entry.action !== "baseline") return versionTarget(entry.action);
|
|
1428
|
-
return index.some((candidate) => SUPPORT_ACTIONS.includes(candidate.action) && candidate.beforeHash === entry.hash) ? "support" : "other";
|
|
1429
|
-
}
|
|
1430
|
-
/**
|
|
1431
|
-
* Split one index into the skill body's chain and the support files' versions.
|
|
1432
|
-
*
|
|
1433
|
-
* Both share ONE index, so a skill that ever wrote a support file has two chains and
|
|
1434
|
-
* {@link orderVersions} cannot account for every entry (it falls back to the stored order). Splitting
|
|
1435
|
-
* first lets each group rebuild its own order, and keeps the body's chain intact by leaving every
|
|
1436
|
-
* entry whose artifact nothing attributes in it — a `baseline` predecessor is usually the body's
|
|
1437
|
-
* first link, and only the support-file baselines {@link entryTarget} recovers leave it.
|
|
1438
|
-
* @param versions - the stored index, oldest first as recorded.
|
|
1439
|
-
* @returns the two groups, each in content order when its chain is complete.
|
|
1440
|
-
*/
|
|
1441
|
-
function partitionVersions(versions) {
|
|
1442
|
-
const content = [];
|
|
1443
|
-
const support = [];
|
|
1444
|
-
for (const entry of versions) if (entryTarget(entry, versions) === "support") support.push(entry);
|
|
1445
|
-
else content.push(entry);
|
|
1446
|
-
return {
|
|
1447
|
-
content: orderVersions(content),
|
|
1448
|
-
support: orderVersions(support)
|
|
1449
|
-
};
|
|
1450
|
-
}
|
|
1451
|
-
/**
|
|
1452
|
-
* When this skill's content last changed: the newest `at` across BOTH chains, or null when the index
|
|
1453
|
-
* holds nothing.
|
|
1454
|
-
*
|
|
1455
|
-
* The stored array is APPEND order and two writers of one skill can interleave, so the answer is the
|
|
1456
|
-
* maximum timestamp rather than the tail. Every `at` this family writes is the same ISO-8601 UTC
|
|
1457
|
-
* form, so comparing the strings is comparing the instants.
|
|
1458
|
-
* @param versions - every entry the index holds, in any order.
|
|
1459
|
-
* @returns the newest timestamp, or null when there is none to report.
|
|
1460
|
-
*/
|
|
1461
|
-
function latestVersionAt(versions) {
|
|
1462
|
-
let newest = null;
|
|
1463
|
-
for (const entry of versions) if (newest === null || entry.at > newest) newest = entry.at;
|
|
1464
|
-
return newest;
|
|
1465
|
-
}
|
|
1466
|
-
/** Keep nothing less than one version, whatever the deployment asks for. */
|
|
1467
|
-
function clampKeep(keep) {
|
|
1468
|
-
return Number.isFinite(keep) ? Math.max(1, Math.floor(keep)) : 1;
|
|
1469
|
-
}
|
|
1470
|
-
/** The next version number: max+1, never a timestamp. */
|
|
1471
|
-
function nextVersion(versions) {
|
|
1472
|
-
let max = 0;
|
|
1473
|
-
for (const entry of versions) if (entry.v > max) max = entry.v;
|
|
1474
|
-
return max + 1;
|
|
1475
|
-
}
|
|
1476
|
-
/** The last entry carrying this hash, or undefined (a hash may repeat after an undo). */
|
|
1477
|
-
function lastVersionOf(versions, hash) {
|
|
1478
|
-
for (let index = versions.length - 1; index >= 0; index -= 1) {
|
|
1479
|
-
const entry = versions[index];
|
|
1480
|
-
if (entry !== void 0 && entry.hash === hash) return entry.v;
|
|
1481
|
-
}
|
|
1482
|
-
}
|
|
1483
|
-
/** One entry as this reader accepts it, or null when the entry cannot be understood. */
|
|
1484
|
-
function readVersionEntry(entry) {
|
|
1485
|
-
if (typeof entry !== "object" || entry === null) return null;
|
|
1486
|
-
const candidate = entry;
|
|
1487
|
-
if (typeof candidate.v !== "number" || !Number.isInteger(candidate.v) || candidate.v <= 0) return null;
|
|
1488
|
-
if (typeof candidate.hash !== "string" || candidate.hash === "") return null;
|
|
1489
|
-
if (typeof candidate.at !== "string") return null;
|
|
1490
|
-
if (typeof candidate.action !== "string") return null;
|
|
1491
|
-
if (typeof candidate.chars !== "number") return null;
|
|
1492
|
-
const summary = typeof candidate.summary === "string" && candidate.summary !== "" ? candidate.summary : void 0;
|
|
1493
|
-
const summarySource = candidate.summarySource === "model" || candidate.summarySource === "host" || candidate.summarySource === "fallback" ? candidate.summarySource : void 0;
|
|
1494
|
-
const path = typeof candidate.path === "string" && candidate.path !== "" ? candidate.path : void 0;
|
|
1495
|
-
const unitId = typeof candidate.unitId === "string" && candidate.unitId !== "" ? candidate.unitId : void 0;
|
|
1496
|
-
const removed = candidate.removed === true ? true : void 0;
|
|
1497
|
-
const unitHead = candidate.unitHead;
|
|
1498
|
-
const base = {
|
|
1499
|
-
v: candidate.v,
|
|
1500
|
-
at: candidate.at,
|
|
1501
|
-
action: candidate.action,
|
|
1502
|
-
hash: candidate.hash,
|
|
1503
|
-
chars: candidate.chars,
|
|
1504
|
-
...summary === void 0 ? {} : { summary },
|
|
1505
|
-
...summarySource === void 0 ? {} : { summarySource },
|
|
1506
|
-
...path === void 0 ? {} : { path },
|
|
1507
|
-
...unitId === void 0 ? {} : { unitId },
|
|
1508
|
-
...removed === void 0 ? {} : { removed },
|
|
1509
|
-
...unitHead === void 0 ? {} : { unitHead }
|
|
1510
|
-
};
|
|
1511
|
-
return typeof candidate.beforeHash === "string" && candidate.beforeHash !== "" ? {
|
|
1512
|
-
...base,
|
|
1513
|
-
beforeHash: candidate.beforeHash
|
|
1514
|
-
} : base;
|
|
1515
|
-
}
|
|
1516
|
-
/**
|
|
1517
|
-
* Read one index body for a RECORDER: "understood", "no file", or "cannot be understood".
|
|
1518
|
-
* A file whose shape is right but which carries an entry this reader would have to drop counts as
|
|
1519
|
-
* unreadable — dropping it silently would be the overwrite the audit posture forbids.
|
|
1520
|
-
* @param raw - the index file's bytes, or null when absent.
|
|
1521
|
-
* @returns the three-state answer.
|
|
1522
|
-
*/
|
|
1523
|
-
function readHistoryIndex(raw) {
|
|
1524
|
-
if (raw === null) return { kind: "absent" };
|
|
1525
|
-
if (raw.trim() === "") return { kind: "unreadable" };
|
|
1526
|
-
let parsed;
|
|
1527
|
-
try {
|
|
1528
|
-
parsed = JSON.parse(raw);
|
|
1529
|
-
} catch {
|
|
1530
|
-
return { kind: "unreadable" };
|
|
1531
|
-
}
|
|
1532
|
-
if (typeof parsed !== "object" || parsed === null) return { kind: "unreadable" };
|
|
1533
|
-
const holder = parsed;
|
|
1534
|
-
if (typeof holder.version === "number" && holder.version > 1) return { kind: "unreadable" };
|
|
1535
|
-
if (!Array.isArray(holder.versions)) return { kind: "unreadable" };
|
|
1536
|
-
const versions = [];
|
|
1537
|
-
for (const entry of holder.versions) {
|
|
1538
|
-
const version = readVersionEntry(entry);
|
|
1539
|
-
if (version === null) return { kind: "unreadable" };
|
|
1540
|
-
versions.push(version);
|
|
1541
|
-
}
|
|
1542
|
-
return {
|
|
1543
|
-
kind: "ok",
|
|
1544
|
-
versions
|
|
1545
|
-
};
|
|
1546
|
-
}
|
|
1547
|
-
/**
|
|
1548
|
-
* Parse one index body for a READER. Malformed content, a foreign shape, or a version NEWER than this
|
|
1549
|
-
* reader all read as empty — the audit posture: never guess. A recorder must use
|
|
1550
|
-
* {@link readHistoryIndex} instead, because it has to PRESERVE bytes it cannot understand.
|
|
1551
|
-
* @param raw - the index file's bytes, or null when absent.
|
|
1552
|
-
* @returns the versions, oldest first.
|
|
1553
|
-
*/
|
|
1554
|
-
function parseHistoryIndex(raw) {
|
|
1555
|
-
const state = readHistoryIndex(raw);
|
|
1556
|
-
return state.kind === "ok" ? state.versions : [];
|
|
1557
|
-
}
|
|
1558
|
-
/**
|
|
1559
|
-
* The index after this write, and the version numbers it produced. Pure, so the versioning rules
|
|
1560
|
-
* are testable without io.
|
|
1561
|
-
*
|
|
1562
|
-
* Two rules matter here: a predecessor the index has never seen gets a BASELINE entry (so the state
|
|
1563
|
-
* before this mutation is recoverable even for a skill that predates the feature), and re-writing
|
|
1564
|
-
* identical content mints NO new version (the library's no-op updates must not grow history).
|
|
1565
|
-
* @param versions - the current index, oldest first.
|
|
1566
|
-
* @param input - the write being recorded.
|
|
1567
|
-
* @param keep - how many versions to retain per skill.
|
|
1568
|
-
* @returns the trimmed index and the recorded numbers.
|
|
1569
|
-
*/
|
|
1570
|
-
function nextHistoryIndex(versions, input, keep) {
|
|
1571
|
-
const beforeHash = input.before === null ? null : contentHash(input.before);
|
|
1572
|
-
const afterHash = input.after === null ? null : contentHash(input.after);
|
|
1573
|
-
const grown = [...versions];
|
|
1574
|
-
const pathField = input.path === void 0 ? {} : { path: input.path };
|
|
1575
|
-
if (beforeHash !== null && !grown.some((entry) => entry.hash === beforeHash)) grown.push({
|
|
1576
|
-
v: nextVersion(grown),
|
|
1577
|
-
at: input.at,
|
|
1578
|
-
action: BASELINE_ACTION,
|
|
1579
|
-
hash: beforeHash,
|
|
1580
|
-
chars: input.before?.length ?? 0,
|
|
1581
|
-
...pathField,
|
|
1582
|
-
...input.unitId === void 0 ? {} : { unitId: input.unitId }
|
|
1583
|
-
});
|
|
1584
|
-
const removalHash = afterHash === null ? beforeHash : null;
|
|
1585
|
-
if (afterHash !== null) {
|
|
1586
|
-
if (grown[grown.length - 1]?.hash !== afterHash) grown.push({
|
|
1587
|
-
...input.unitId === void 0 ? {} : { unitId: input.unitId },
|
|
1588
|
-
...input.unitHead === void 0 ? {} : { unitHead: input.unitHead },
|
|
1589
|
-
v: nextVersion(grown),
|
|
1590
|
-
at: input.at,
|
|
1591
|
-
action: input.action,
|
|
1592
|
-
hash: afterHash,
|
|
1593
|
-
chars: input.after?.length ?? 0,
|
|
1594
|
-
...pathField,
|
|
1595
|
-
...input.summary === void 0 ? {} : { summary: input.summary },
|
|
1596
|
-
...input.summarySource === void 0 ? {} : { summarySource: input.summarySource },
|
|
1597
|
-
...beforeHash === null ? {} : { beforeHash }
|
|
1598
|
-
});
|
|
1599
|
-
}
|
|
1600
|
-
if (removalHash !== null) {
|
|
1601
|
-
const tail = grown[grown.length - 1];
|
|
1602
|
-
if (tail?.hash !== removalHash || tail.action !== input.action) grown.push({
|
|
1603
|
-
v: nextVersion(grown),
|
|
1604
|
-
at: input.at,
|
|
1605
|
-
action: input.action,
|
|
1606
|
-
hash: removalHash,
|
|
1607
|
-
chars: input.before?.length ?? 0,
|
|
1608
|
-
removed: true,
|
|
1609
|
-
...pathField,
|
|
1610
|
-
...input.unitId === void 0 ? {} : { unitId: input.unitId },
|
|
1611
|
-
...input.unitHead === void 0 ? {} : { unitHead: input.unitHead },
|
|
1612
|
-
...input.summary === void 0 ? {} : { summary: input.summary },
|
|
1613
|
-
...input.summarySource === void 0 ? {} : { summarySource: input.summarySource }
|
|
1614
|
-
});
|
|
1615
|
-
}
|
|
1616
|
-
const budget = clampKeep(keep);
|
|
1617
|
-
const groups = [];
|
|
1618
|
-
const byUnit = /* @__PURE__ */ new Map();
|
|
1619
|
-
for (const entry of grown) {
|
|
1620
|
-
const unit = entry.unitId;
|
|
1621
|
-
if (unit === void 0) {
|
|
1622
|
-
groups.push([entry]);
|
|
1623
|
-
continue;
|
|
1624
|
-
}
|
|
1625
|
-
const found = byUnit.get(unit);
|
|
1626
|
-
if (found !== void 0) {
|
|
1627
|
-
found.push(entry);
|
|
1628
|
-
continue;
|
|
1629
|
-
}
|
|
1630
|
-
const made = [entry];
|
|
1631
|
-
byUnit.set(unit, made);
|
|
1632
|
-
groups.push(made);
|
|
1633
|
-
}
|
|
1634
|
-
const keptIndexes = /* @__PURE__ */ new Set();
|
|
1635
|
-
let keptCount = 0;
|
|
1636
|
-
for (let i = groups.length - 1; i >= 0; i -= 1) {
|
|
1637
|
-
const group = groups[i];
|
|
1638
|
-
if (group === void 0) continue;
|
|
1639
|
-
if (keptCount > 0 && keptCount + group.length > budget) break;
|
|
1640
|
-
for (const entry of group) keptIndexes.add(grown.indexOf(entry));
|
|
1641
|
-
keptCount += group.length;
|
|
1642
|
-
}
|
|
1643
|
-
const trimmed = grown.filter((_, index) => keptIndexes.has(index));
|
|
1644
|
-
const recorded = {};
|
|
1645
|
-
if (beforeHash !== null) {
|
|
1646
|
-
const v = lastVersionOf(trimmed, beforeHash);
|
|
1647
|
-
if (v !== void 0) recorded.beforeVersion = v;
|
|
1648
|
-
}
|
|
1649
|
-
if (afterHash !== null) {
|
|
1650
|
-
const v = lastVersionOf(trimmed, afterHash);
|
|
1651
|
-
if (v !== void 0) recorded.afterVersion = v;
|
|
1652
|
-
}
|
|
1653
|
-
return {
|
|
1654
|
-
versions: trimmed,
|
|
1655
|
-
recorded
|
|
1656
|
-
};
|
|
1657
|
-
}
|
|
1658
|
-
/** Shrink level at which the retention line speaks: a body that keeps less than this share of the
|
|
1659
|
-
* previous one is a rewrite, not an edit. */
|
|
1660
|
-
const RETENTION_FEEDBACK_KEEP_RATIO = .5;
|
|
1661
|
-
/** Floor on the REPLACED body: below this a ratio is noise (a 40-character skill cut in half is not
|
|
1662
|
-
* news), so the line stays off for small skills. */
|
|
1663
|
-
const RETENTION_FEEDBACK_MIN_CHARS = 200;
|
|
1664
|
-
/**
|
|
1665
|
-
* The anti-overwrite feedback line (design §4, item 3): says how much of the previous body a
|
|
1666
|
-
* replacement kept, so a destructive rewrite is visible in the same message that reports the write.
|
|
1667
|
-
*
|
|
1668
|
-
* Computed where the replaced bytes are still in hand — the write path's own in-lock read — instead of
|
|
1669
|
-
* re-reading the file from a caller (that second read would race the writers this library serializes).
|
|
1670
|
-
* It is FEEDBACK, never a gate: no ratio refuses a write, and the function owns no configuration
|
|
1671
|
-
* (a toggle nobody reads would be configuration that does nothing).
|
|
1672
|
-
*
|
|
1673
|
-
* @param input - the write being described.
|
|
1674
|
-
* @returns the line, or null when the write is not a significant whole-body replacement.
|
|
1675
|
-
*/
|
|
1676
|
-
function contentRetentionFeedback(input) {
|
|
1677
|
-
if (input.action !== "update") return null;
|
|
1678
|
-
const before = input.before;
|
|
1679
|
-
const after = input.after;
|
|
1680
|
-
if (before === null || after === null) return null;
|
|
1681
|
-
if (before.length < 200) return null;
|
|
1682
|
-
if (after.length >= before.length * .5) return null;
|
|
1683
|
-
return `新内容保留了上一版正文的 ${Math.round(after.length / before.length * 100)}%(${after.length}/${before.length} 字符);被替换的版本已保留在该技能的历史里。`;
|
|
1684
|
-
}
|
|
1685
|
-
/**
|
|
1686
|
-
* Read one skill's versions, oldest first.
|
|
1687
|
-
* @param root - the skills root.
|
|
1688
|
-
* @param io - the io seam.
|
|
1689
|
-
* @param name - the skill name.
|
|
1690
|
-
* @returns the versions; an absent or unreadable index reads as empty (never a throw).
|
|
1691
|
-
*/
|
|
1692
|
-
async function loadVersions(root, io, name) {
|
|
1693
|
-
const file = historyIndexFile(root, name);
|
|
1694
|
-
const probe = await probeText(io, file);
|
|
1695
|
-
if (isUnknown(probe)) {
|
|
1696
|
-
console.warn(`skill-store: ${file} could not be read (${probe.reason}); reporting no versions for "${name}" — the bytes are untouched`);
|
|
1697
|
-
return [];
|
|
1698
|
-
}
|
|
1699
|
-
return parseHistoryIndex(isPresent(probe) ? probe.value : null);
|
|
1700
|
-
}
|
|
1701
|
-
/**
|
|
1702
|
-
* One version's content.
|
|
1703
|
-
* @param root - the skills root.
|
|
1704
|
-
* @param io - the io seam.
|
|
1705
|
-
* @param name - the skill name.
|
|
1706
|
-
* @param v - the version number.
|
|
1707
|
-
* @returns the content, or null when the version or its blob cannot be read (never '').
|
|
1708
|
-
*/
|
|
1709
|
-
async function loadVersionContent(root, io, name, v) {
|
|
1710
|
-
const entry = (await loadVersions(root, io, name)).find((candidate) => candidate.v === v);
|
|
1711
|
-
if (entry === void 0) return null;
|
|
1712
|
-
const probe = await probeText(io, blobPath(root, entry.hash));
|
|
1713
|
-
if (isUnknown(probe)) {
|
|
1714
|
-
console.warn(`skill-store: version ${v} of "${name}" could not be read (${probe.reason}); reporting no content`);
|
|
1715
|
-
return null;
|
|
1716
|
-
}
|
|
1717
|
-
return isPresent(probe) ? probe.value : null;
|
|
1718
|
-
}
|
|
1719
|
-
async function recordVersions(root, io, input, keep, fence) {
|
|
1720
|
-
if (input.before === null && input.after === null) return null;
|
|
1721
|
-
for (const side of [input.before, input.after]) {
|
|
1722
|
-
if (side === null) continue;
|
|
1723
|
-
const path = blobPath(root, contentHash(side));
|
|
1724
|
-
let blobPresent;
|
|
1725
|
-
try {
|
|
1726
|
-
blobPresent = await io.exists(path);
|
|
1727
|
-
} catch (error) {
|
|
1728
|
-
blobPresent = false;
|
|
1729
|
-
console.warn(`skill-store: could not check whether the history blob ${path} exists (${error instanceof Error ? error.message : String(error)}); writing it — content-addressed bytes are the same file`);
|
|
1730
|
-
}
|
|
1731
|
-
if (!blobPresent) await io.writeText(path, side);
|
|
1732
|
-
}
|
|
1733
|
-
const file = historyIndexFile(root, input.skillName);
|
|
1734
|
-
const fileProbe = await probeText(io, file);
|
|
1735
|
-
if (isUnknown(fileProbe)) {
|
|
1736
|
-
const quarantine = `${file}.corrupt`;
|
|
1737
|
-
try {
|
|
1738
|
-
await io.copy(file, quarantine);
|
|
1739
|
-
} catch {}
|
|
1740
|
-
console.warn(`skill-store: ${file} could not be read (${fileProbe.reason}); its bytes were copied to ${quarantine} where possible and the version record for this write was abandoned — the mutation stands and the file is untouched`);
|
|
1741
|
-
return null;
|
|
1742
|
-
}
|
|
1743
|
-
const probe = readHistoryIndex(isPresent(fileProbe) ? fileProbe.value : null);
|
|
1744
|
-
if (probe.kind === "unreadable") {
|
|
1745
|
-
const quarantine = `${file}.corrupt`;
|
|
1746
|
-
await io.copy(file, quarantine);
|
|
1747
|
-
console.warn(`skill-store: ${file} could not be understood (malformed, foreign, or written by a newer reader); its bytes were copied to ${quarantine} and a fresh index starts from this write`);
|
|
1748
|
-
}
|
|
1749
|
-
const recorded = {};
|
|
1750
|
-
let raceRefused = false;
|
|
1751
|
-
const write = async () => {
|
|
1752
|
-
await transactIo(io, file, (current) => {
|
|
1753
|
-
const state = readHistoryIndex(current);
|
|
1754
|
-
if (state.kind === "unreadable" && probe.kind !== "unreadable") {
|
|
1755
|
-
raceRefused = true;
|
|
1756
|
-
return current;
|
|
1757
|
-
}
|
|
1758
|
-
const { versions, recorded: numbers } = nextHistoryIndex(state.kind === "ok" ? state.versions : [], input, keep);
|
|
1759
|
-
recorded.beforeVersion = numbers.beforeVersion;
|
|
1760
|
-
recorded.afterVersion = numbers.afterVersion;
|
|
1761
|
-
return JSON.stringify({
|
|
1762
|
-
version: 1,
|
|
1763
|
-
versions
|
|
1764
|
-
}, null, 2);
|
|
1765
|
-
});
|
|
1766
|
-
};
|
|
1767
|
-
if (fence !== void 0) await fence.run(write);
|
|
1768
|
-
else await write();
|
|
1769
|
-
if (raceRefused) {
|
|
1770
|
-
const quarantine = `${file}.corrupt`;
|
|
1771
|
-
try {
|
|
1772
|
-
await io.copy(file, quarantine);
|
|
1773
|
-
} catch {}
|
|
1774
|
-
console.warn(`skill-store: ${file} became unreadable between the probe and the locked read; its bytes were copied to ${quarantine} where possible and the version record for this write was abandoned — nothing was overwritten`);
|
|
1775
|
-
return null;
|
|
1776
|
-
}
|
|
1777
|
-
return recorded;
|
|
1778
|
-
}
|
|
1779
|
-
/**
|
|
1780
|
-
* Classify one version action for display. Fail-closed like {@link versionTarget}: an action this
|
|
1781
|
-
* module does not know reads as `other` rather than being guessed, and `tests/skill-history.spec.ts`
|
|
1782
|
-
* pins every action the library can write, so a new one cannot land unclassified.
|
|
1783
|
-
* @param action - the entry’s action label.
|
|
1784
|
-
* @returns the kind the faces switch on.
|
|
1785
|
-
*/
|
|
1786
|
-
function versionActionKind(action) {
|
|
1787
|
-
switch (action) {
|
|
1788
|
-
case BASELINE_ACTION: return "baseline";
|
|
1789
|
-
case "create": return "create";
|
|
1790
|
-
case "patch":
|
|
1791
|
-
case "edit": return "patch";
|
|
1792
|
-
case "update": return "update";
|
|
1793
|
-
case "restore": return "restore";
|
|
1794
|
-
case "delete": return "delete";
|
|
1795
|
-
case "archive": return "archive";
|
|
1796
|
-
case "consolidate": return "consolidate";
|
|
1797
|
-
case "restructure": return "restructure";
|
|
1798
|
-
case "extract": return "extract";
|
|
1799
|
-
case "merge": return "merge";
|
|
1800
|
-
case "write_file": return "support-write";
|
|
1801
|
-
case "remove_file": return "support-remove";
|
|
1802
|
-
default: return "other";
|
|
1803
|
-
}
|
|
1804
|
-
}
|
|
1805
|
-
/** Lines one changed region may carry before it is windowed and flagged. */
|
|
1806
|
-
const TEXT_DIFF_MAX_LINES = 40;
|
|
1807
|
-
/** Characters one windowed side may carry, so a single enormous line cannot travel whole. */
|
|
1808
|
-
const TEXT_DIFF_MAX_CHARS = 4e3;
|
|
1809
|
-
/**
|
|
1810
|
-
* The facts of one whole-body replacement: how many lines the changed region gained and lost, and
|
|
1811
|
-
* that region itself as one hunk.
|
|
1812
|
-
*
|
|
1813
|
-
* The algorithm is the honest one for "what did this edit touch": common leading and trailing lines
|
|
1814
|
-
* are trimmed away and what remains is ONE contiguous region. It is not a minimal edit script — a
|
|
1815
|
-
* body edited in two distant places reports their span as one region — and that limitation is the
|
|
1816
|
-
* reason the faces show the region as context rather than as a line-by-line proof.
|
|
1817
|
-
* @param before - the body the write replaced.
|
|
1818
|
-
* @param after - the body the write stored.
|
|
1819
|
-
* @param path - the path the hunk names (the platform’s diff card requires one).
|
|
1820
|
-
* @param maxLines - the window a region’s text is capped to.
|
|
1821
|
-
* @returns the counts, the windowed hunk, and whether the window cut anything.
|
|
1822
|
-
*/
|
|
1823
|
-
function textDiffFacts(before, after, path, maxLines = 40) {
|
|
1824
|
-
const oldLines = before === "" ? [] : before.split("\n");
|
|
1825
|
-
const newLines = after === "" ? [] : after.split("\n");
|
|
1826
|
-
let head = 0;
|
|
1827
|
-
while (head < oldLines.length && head < newLines.length && oldLines[head] === newLines[head]) head += 1;
|
|
1828
|
-
let tail = 0;
|
|
1829
|
-
while (tail < oldLines.length - head && tail < newLines.length - head && oldLines[oldLines.length - 1 - tail] === newLines[newLines.length - 1 - tail]) tail += 1;
|
|
1830
|
-
const oldRegion = oldLines.slice(head, oldLines.length - tail);
|
|
1831
|
-
const newRegion = newLines.slice(head, newLines.length - tail);
|
|
1832
|
-
if (oldRegion.length === 0 && newRegion.length === 0) return {
|
|
1833
|
-
linesAdded: 0,
|
|
1834
|
-
linesRemoved: 0,
|
|
1835
|
-
hunks: [],
|
|
1836
|
-
truncated: false
|
|
1837
|
-
};
|
|
1838
|
-
const window = (lines) => {
|
|
1839
|
-
const joined = lines.slice(0, maxLines).join("\n");
|
|
1840
|
-
if (joined.length <= 4e3) return {
|
|
1841
|
-
text: joined,
|
|
1842
|
-
cut: lines.length > maxLines
|
|
1843
|
-
};
|
|
1844
|
-
return {
|
|
1845
|
-
text: joined.slice(0, TEXT_DIFF_MAX_CHARS),
|
|
1846
|
-
cut: true
|
|
1847
|
-
};
|
|
1848
|
-
};
|
|
1849
|
-
const oldWindow = window(oldRegion);
|
|
1850
|
-
const newWindow = window(newRegion);
|
|
1851
|
-
return {
|
|
1852
|
-
linesAdded: newRegion.length,
|
|
1853
|
-
linesRemoved: oldRegion.length,
|
|
1854
|
-
hunks: [{
|
|
1855
|
-
path,
|
|
1856
|
-
oldText: oldWindow.text,
|
|
1857
|
-
newText: newWindow.text
|
|
1858
|
-
}],
|
|
1859
|
-
truncated: oldWindow.cut || newWindow.cut
|
|
1860
|
-
};
|
|
1861
|
-
}
|
|
1862
|
-
/**
|
|
1863
|
-
* How long ago an ISO timestamp was, as a bucket plus a count — never as a sentence.
|
|
1864
|
-
*
|
|
1865
|
-
* The shape deliberately mirrors the platform’s `relativeTime` (`ui-primitives`), so a face that later
|
|
1866
|
-
* may import the platform helper swaps one call and keeps its dictionary; the WORDS live in the
|
|
1867
|
-
* faces (the panel’s locale dictionary, the command surface’s English), which is why this module
|
|
1868
|
-
* returns numbers.
|
|
1869
|
-
* @param at - an ISO timestamp, or any string `Date.parse` understands.
|
|
1870
|
-
* @param nowMs - the reference instant.
|
|
1871
|
-
* @returns the bucket and its count; an unparsable timestamp reads as `now` with 0.
|
|
1872
|
-
*/
|
|
1873
|
-
function elapsedSince(at, nowMs) {
|
|
1874
|
-
const then = Date.parse(at);
|
|
1875
|
-
if (Number.isNaN(then)) return {
|
|
1876
|
-
unit: "now",
|
|
1877
|
-
n: 0
|
|
1878
|
-
};
|
|
1879
|
-
const minutes = Math.floor(Math.max(0, nowMs - then) / 6e4);
|
|
1880
|
-
if (minutes < 1) return {
|
|
1881
|
-
unit: "now",
|
|
1882
|
-
n: 0
|
|
1883
|
-
};
|
|
1884
|
-
if (minutes < 60) return {
|
|
1885
|
-
unit: "minutes",
|
|
1886
|
-
n: minutes
|
|
1887
|
-
};
|
|
1888
|
-
const hours = Math.floor(minutes / 60);
|
|
1889
|
-
if (hours < 24) return {
|
|
1890
|
-
unit: "hours",
|
|
1891
|
-
n: hours
|
|
1892
|
-
};
|
|
1893
|
-
const days = Math.floor(hours / 24);
|
|
1894
|
-
if (days < 30) return {
|
|
1895
|
-
unit: "days",
|
|
1896
|
-
n: days
|
|
1897
|
-
};
|
|
1898
|
-
const months = Math.floor(days / 30);
|
|
1899
|
-
if (months < 12) return {
|
|
1900
|
-
unit: "months",
|
|
1901
|
-
n: months
|
|
1902
|
-
};
|
|
1903
|
-
return {
|
|
1904
|
-
unit: "years",
|
|
1905
|
-
n: Math.max(1, Math.floor(days / 365))
|
|
1906
|
-
};
|
|
1907
|
-
}
|
|
1908
|
-
//#endregion
|
|
1909
|
-
export { writeUnitReceipt as $, pruneTreeChangeUnits as A, UNIT_STORAGE_SCHEMA_VERSION as B, partitionVersions as C, textDiffFacts as D, summaryChars as E, assertUnitJournalTransition as F, unitEvidencePath as G, readUnitIndex as H, readUnitJournal as I, unitSnapshotPath as J, unitIndexPath as K, unitJournalPath as L, unitSnapshotPort as M, UNIT_JOURNAL_PHASES as N, versionActionKind as O, UNIT_JOURNAL_SCHEMA_VERSION as P, writeUnitEvidence as Q, unitJournalPhaseRank as R, parseHistoryIndex as S, recordVersions as T, readUnitReceipt as U, readUnitEvidence as V, readUnitSnapshot as W, unitStorageKey as X, unitSnapshotsDir as Y, upsertUnitIndex as Z, loadVersionContent as _, RETENTION_FEEDBACK_KEEP_RATIO as a, normalizeChangeSummary as b, TEXT_DIFF_MAX_LINES as c, contentRetentionFeedback as d, writeUnitSnapshot as et, elapsedSince as f, latestVersionAt as g, historyRoot as h, HISTORY_INDEX_VERSION as i, WriteKernel as it, unitJournalPort as j, versionTarget as k, blobPath as l, historyIndexFile as m, DEFAULT_SKILL_WRITE_SUMMARY_CHARS as n, fileMutationLock as nt, RETENTION_FEEDBACK_MIN_CHARS as o, entryTarget as p, unitReceiptPath as q, HISTORY_DIR as r, validateMutationRequest as rt, TEXT_DIFF_MAX_CHARS as s, BASELINE_ACTION as t, MutationUnit as tt, clipChangeSummary as u, loadVersions as v, readHistoryIndex as w, orderVersions as x, nextHistoryIndex as y, writeUnitJournal as z };
|
|
1266
|
+
export { writeUnitEvidence as C, fileMutationLock as D, MutationUnit as E, validateMutationRequest as O, upsertUnitIndex as S, writeUnitSnapshot as T, unitIndexPath as _, UNIT_JOURNAL_SCHEMA_VERSION as a, unitSnapshotsDir as b, unitJournalPath as c, UNIT_STORAGE_SCHEMA_VERSION as d, readUnitEvidence as f, unitEvidencePath as g, readUnitSnapshot as h, UNIT_JOURNAL_PHASES as i, WriteKernel as k, unitJournalPhaseRank as l, readUnitReceipt as m, unitJournalPort as n, assertUnitJournalTransition as o, readUnitIndex as p, unitSnapshotPort as r, readUnitJournal as s, pruneTreeChangeUnits as t, writeUnitJournal as u, unitReceiptPath as v, writeUnitReceipt as w, unitStorageKey as x, unitSnapshotPath as y };
|