@fgv/ts-agent-memory 5.1.0-37 → 5.1.0-39
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/.rush/temp/{6dfc331ddf74dc2db0d4483d0c96ea971f351c83.tar.log → b82cf6bdece20481260e6bab946179eeec9d7b46.tar.log} +116 -2
- package/.rush/temp/chunked-rush-logs/ts-agent-memory.build.chunks.jsonl +4 -4
- package/.rush/temp/operation/build/all.log +4 -4
- package/.rush/temp/operation/build/log-chunks.jsonl +4 -4
- package/.rush/temp/operation/build/state.json +1 -1
- package/.rush/temp/shrinkwrap-deps.json +222 -221
- package/config/jest.config.json +1 -1
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -1
- package/dist/packlets/ingest/cycleGuard.js +111 -0
- package/dist/packlets/ingest/cycleGuard.js.map +1 -0
- package/dist/packlets/ingest/hostStages.js +6 -0
- package/dist/packlets/ingest/hostStages.js.map +1 -0
- package/dist/packlets/ingest/index.js +9 -0
- package/dist/packlets/ingest/index.js.map +1 -0
- package/dist/packlets/ingest/model.js +6 -0
- package/dist/packlets/ingest/model.js.map +1 -0
- package/dist/packlets/ingest/orchestrator.js +438 -0
- package/dist/packlets/ingest/orchestrator.js.map +1 -0
- package/dist/packlets/retrieve/index.js +1 -0
- package/dist/packlets/retrieve/index.js.map +1 -1
- package/dist/packlets/retrieve/temporalRetrievers.js +172 -0
- package/dist/packlets/retrieve/temporalRetrievers.js.map +1 -0
- package/dist/packlets/store/fileTreeMemoryStore.js +323 -41
- package/dist/packlets/store/fileTreeMemoryStore.js.map +1 -1
- package/dist/packlets/tools/index.js +6 -0
- package/dist/packlets/tools/index.js.map +1 -0
- package/dist/packlets/tools/memoryTools.js +337 -0
- package/dist/packlets/tools/memoryTools.js.map +1 -0
- package/dist/packlets/types/identityCodec.js +115 -0
- package/dist/packlets/types/identityCodec.js.map +1 -1
- package/dist/packlets/types/index.js +1 -0
- package/dist/packlets/types/index.js.map +1 -1
- package/dist/packlets/types/temporal.js +85 -0
- package/dist/packlets/types/temporal.js.map +1 -0
- package/dist/packlets/types/writePolicy.js +96 -0
- package/dist/packlets/types/writePolicy.js.map +1 -1
- package/dist/test/unit/converters/antagonistRoundTrip.test.js +95 -0
- package/dist/test/unit/converters/antagonistRoundTrip.test.js.map +1 -0
- package/dist/test/unit/ingest/antagonistCycleAndParity.test.js +273 -0
- package/dist/test/unit/ingest/antagonistCycleAndParity.test.js.map +1 -0
- package/dist/test/unit/ingest/cycleGuard.test.js +54 -0
- package/dist/test/unit/ingest/cycleGuard.test.js.map +1 -0
- package/dist/test/unit/ingest/orchestrator.test.js +913 -0
- package/dist/test/unit/ingest/orchestrator.test.js.map +1 -0
- package/dist/test/unit/retrieve/temporalRetrievers.test.js +182 -0
- package/dist/test/unit/retrieve/temporalRetrievers.test.js.map +1 -0
- package/dist/test/unit/store/antagonistTemporalBoundary.test.js +120 -0
- package/dist/test/unit/store/antagonistTemporalBoundary.test.js.map +1 -0
- package/dist/test/unit/store/fileTreeMemoryStore.test.js +74 -7
- package/dist/test/unit/store/fileTreeMemoryStore.test.js.map +1 -1
- package/dist/test/unit/store/temporalStore.test.js +398 -0
- package/dist/test/unit/store/temporalStore.test.js.map +1 -0
- package/dist/test/unit/tools/memoryTools.test.js +572 -0
- package/dist/test/unit/tools/memoryTools.test.js.map +1 -0
- package/dist/test/unit/types/temporalCodec.test.js +203 -0
- package/dist/test/unit/types/temporalCodec.test.js.map +1 -0
- package/dist/test/unit/types/temporalPolicy.test.js +62 -0
- package/dist/test/unit/types/temporalPolicy.test.js.map +1 -0
- package/dist/ts-agent-memory.d.ts +1042 -10
- package/dist/tsdoc-metadata.json +1 -1
- package/etc/ts-agent-memory.api.md +284 -0
- package/lib/index.d.ts +2 -0
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +2 -0
- package/lib/index.js.map +1 -1
- package/lib/packlets/ingest/cycleGuard.d.ts +41 -0
- package/lib/packlets/ingest/cycleGuard.d.ts.map +1 -0
- package/lib/packlets/ingest/cycleGuard.js +115 -0
- package/lib/packlets/ingest/cycleGuard.js.map +1 -0
- package/lib/packlets/ingest/hostStages.d.ts +88 -0
- package/lib/packlets/ingest/hostStages.d.ts.map +1 -0
- package/lib/packlets/ingest/hostStages.js +7 -0
- package/lib/packlets/ingest/hostStages.js.map +1 -0
- package/lib/packlets/ingest/index.d.ts +5 -0
- package/lib/packlets/ingest/index.d.ts.map +1 -0
- package/lib/packlets/ingest/index.js +25 -0
- package/lib/packlets/ingest/index.js.map +1 -0
- package/lib/packlets/ingest/model.d.ts +177 -0
- package/lib/packlets/ingest/model.d.ts.map +1 -0
- package/lib/packlets/ingest/model.js +7 -0
- package/lib/packlets/ingest/model.js.map +1 -0
- package/lib/packlets/ingest/orchestrator.d.ts +206 -0
- package/lib/packlets/ingest/orchestrator.d.ts.map +1 -0
- package/lib/packlets/ingest/orchestrator.js +442 -0
- package/lib/packlets/ingest/orchestrator.js.map +1 -0
- package/lib/packlets/retrieve/index.d.ts +1 -0
- package/lib/packlets/retrieve/index.d.ts.map +1 -1
- package/lib/packlets/retrieve/index.js +1 -0
- package/lib/packlets/retrieve/index.js.map +1 -1
- package/lib/packlets/retrieve/temporalRetrievers.d.ts +78 -0
- package/lib/packlets/retrieve/temporalRetrievers.d.ts.map +1 -0
- package/lib/packlets/retrieve/temporalRetrievers.js +178 -0
- package/lib/packlets/retrieve/temporalRetrievers.js.map +1 -0
- package/lib/packlets/store/fileTreeMemoryStore.d.ts +118 -9
- package/lib/packlets/store/fileTreeMemoryStore.d.ts.map +1 -1
- package/lib/packlets/store/fileTreeMemoryStore.js +322 -40
- package/lib/packlets/store/fileTreeMemoryStore.js.map +1 -1
- package/lib/packlets/tools/index.d.ts +2 -0
- package/lib/packlets/tools/index.d.ts.map +1 -0
- package/lib/packlets/tools/index.js +22 -0
- package/lib/packlets/tools/index.js.map +1 -0
- package/lib/packlets/tools/memoryTools.d.ts +139 -0
- package/lib/packlets/tools/memoryTools.d.ts.map +1 -0
- package/lib/packlets/tools/memoryTools.js +341 -0
- package/lib/packlets/tools/memoryTools.js.map +1 -0
- package/lib/packlets/types/identityCodec.d.ts +86 -0
- package/lib/packlets/types/identityCodec.d.ts.map +1 -1
- package/lib/packlets/types/identityCodec.js +118 -1
- package/lib/packlets/types/identityCodec.js.map +1 -1
- package/lib/packlets/types/index.d.ts +1 -0
- package/lib/packlets/types/index.d.ts.map +1 -1
- package/lib/packlets/types/index.js +1 -0
- package/lib/packlets/types/index.js.map +1 -1
- package/lib/packlets/types/temporal.d.ts +40 -0
- package/lib/packlets/types/temporal.d.ts.map +1 -0
- package/lib/packlets/types/temporal.js +92 -0
- package/lib/packlets/types/temporal.js.map +1 -0
- package/lib/packlets/types/writePolicy.d.ts +49 -0
- package/lib/packlets/types/writePolicy.d.ts.map +1 -1
- package/lib/packlets/types/writePolicy.js +98 -1
- package/lib/packlets/types/writePolicy.js.map +1 -1
- package/lib/test/unit/converters/antagonistRoundTrip.test.d.ts +10 -0
- package/lib/test/unit/converters/antagonistRoundTrip.test.d.ts.map +1 -0
- package/lib/test/unit/converters/antagonistRoundTrip.test.js +97 -0
- package/lib/test/unit/converters/antagonistRoundTrip.test.js.map +1 -0
- package/lib/test/unit/ingest/antagonistCycleAndParity.test.d.ts +9 -0
- package/lib/test/unit/ingest/antagonistCycleAndParity.test.d.ts.map +1 -0
- package/lib/test/unit/ingest/antagonistCycleAndParity.test.js +275 -0
- package/lib/test/unit/ingest/antagonistCycleAndParity.test.js.map +1 -0
- package/lib/test/unit/ingest/cycleGuard.test.d.ts +2 -0
- package/lib/test/unit/ingest/cycleGuard.test.d.ts.map +1 -0
- package/lib/test/unit/ingest/cycleGuard.test.js +56 -0
- package/lib/test/unit/ingest/cycleGuard.test.js.map +1 -0
- package/lib/test/unit/ingest/orchestrator.test.d.ts +2 -0
- package/lib/test/unit/ingest/orchestrator.test.d.ts.map +1 -0
- package/lib/test/unit/ingest/orchestrator.test.js +915 -0
- package/lib/test/unit/ingest/orchestrator.test.js.map +1 -0
- package/lib/test/unit/retrieve/temporalRetrievers.test.d.ts +2 -0
- package/lib/test/unit/retrieve/temporalRetrievers.test.d.ts.map +1 -0
- package/lib/test/unit/retrieve/temporalRetrievers.test.js +184 -0
- package/lib/test/unit/retrieve/temporalRetrievers.test.js.map +1 -0
- package/lib/test/unit/store/antagonistTemporalBoundary.test.d.ts +9 -0
- package/lib/test/unit/store/antagonistTemporalBoundary.test.d.ts.map +1 -0
- package/lib/test/unit/store/antagonistTemporalBoundary.test.js +122 -0
- package/lib/test/unit/store/antagonistTemporalBoundary.test.js.map +1 -0
- package/lib/test/unit/store/fileTreeMemoryStore.test.js +74 -7
- package/lib/test/unit/store/fileTreeMemoryStore.test.js.map +1 -1
- package/lib/test/unit/store/temporalStore.test.d.ts +2 -0
- package/lib/test/unit/store/temporalStore.test.d.ts.map +1 -0
- package/lib/test/unit/store/temporalStore.test.js +400 -0
- package/lib/test/unit/store/temporalStore.test.js.map +1 -0
- package/lib/test/unit/tools/memoryTools.test.d.ts +2 -0
- package/lib/test/unit/tools/memoryTools.test.d.ts.map +1 -0
- package/lib/test/unit/tools/memoryTools.test.js +574 -0
- package/lib/test/unit/tools/memoryTools.test.js.map +1 -0
- package/lib/test/unit/types/temporalCodec.test.d.ts +2 -0
- package/lib/test/unit/types/temporalCodec.test.d.ts.map +1 -0
- package/lib/test/unit/types/temporalCodec.test.js +205 -0
- package/lib/test/unit/types/temporalCodec.test.js.map +1 -0
- package/lib/test/unit/types/temporalPolicy.test.d.ts +2 -0
- package/lib/test/unit/types/temporalPolicy.test.d.ts.map +1 -0
- package/lib/test/unit/types/temporalPolicy.test.js +64 -0
- package/lib/test/unit/types/temporalPolicy.test.js.map +1 -0
- package/package.json +7 -7
- package/rush-logs/ts-agent-memory.build.cache.log +1 -1
- package/rush-logs/ts-agent-memory.build.log +4 -4
- package/src/index.ts +2 -0
- package/src/packlets/ingest/cycleGuard.ts +142 -0
- package/src/packlets/ingest/hostStages.ts +111 -0
- package/src/packlets/ingest/index.ts +9 -0
- package/src/packlets/ingest/model.ts +184 -0
- package/src/packlets/ingest/orchestrator.ts +797 -0
- package/src/packlets/retrieve/index.ts +1 -0
- package/src/packlets/retrieve/temporalRetrievers.ts +210 -0
- package/src/packlets/store/fileTreeMemoryStore.ts +460 -66
- package/src/packlets/tools/index.ts +6 -0
- package/src/packlets/tools/memoryTools.ts +579 -0
- package/src/packlets/types/identityCodec.ts +184 -0
- package/src/packlets/types/index.ts +1 -0
- package/src/packlets/types/temporal.ts +96 -0
- package/src/packlets/types/writePolicy.ts +127 -0
- package/src/test/unit/converters/antagonistRoundTrip.test.ts +110 -0
- package/src/test/unit/ingest/antagonistCycleAndParity.test.ts +362 -0
- package/src/test/unit/ingest/cycleGuard.test.ts +68 -0
- package/src/test/unit/ingest/orchestrator.test.ts +1158 -0
- package/src/test/unit/retrieve/temporalRetrievers.test.ts +226 -0
- package/src/test/unit/store/antagonistTemporalBoundary.test.ts +158 -0
- package/src/test/unit/store/fileTreeMemoryStore.test.ts +98 -7
- package/src/test/unit/store/temporalStore.test.ts +469 -0
- package/src/test/unit/tools/memoryTools.test.ts +771 -0
- package/src/test/unit/types/temporalCodec.test.ts +259 -0
- package/src/test/unit/types/temporalPolicy.test.ts +96 -0
- package/temp/build/lint/_eslint-5eVG3S6w.json +85 -9
- package/temp/build/typescript/ts_8nwakTlr.json +1 -1
- package/temp/ts-agent-memory.api.json +11984 -6314
- package/temp/ts-agent-memory.api.md +284 -0
|
@@ -4,11 +4,128 @@
|
|
|
4
4
|
* SPDX-License-Identifier: MIT
|
|
5
5
|
*/
|
|
6
6
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
|
-
exports.MtmIdentityCodec = exports.LtmIdentityCodec = exports.KnowledgeIdentityCodec = void 0;
|
|
7
|
+
exports.MtmIdentityCodec = exports.LtmIdentityCodec = exports.KnowledgeIdentityCodec = exports.TemporalIdentityCodec = void 0;
|
|
8
|
+
exports.isTemporalIdentityCodec = isTemporalIdentityCodec;
|
|
8
9
|
const ts_utils_1 = require("@fgv/ts-utils");
|
|
9
10
|
const ids_1 = require("./ids");
|
|
10
11
|
const filenameSafety_1 = require("./filenameSafety");
|
|
11
12
|
/**
|
|
13
|
+
* Narrow an {@link IIdentityCodec} to {@link ITemporalIdentityCodec} by probing
|
|
14
|
+
* for the versioned methods. Used by the store when an `encode` result reports
|
|
15
|
+
* `isVersioned: true`.
|
|
16
|
+
* @public
|
|
17
|
+
*/
|
|
18
|
+
function isTemporalIdentityCodec(codec) {
|
|
19
|
+
const candidate = codec;
|
|
20
|
+
return typeof candidate.encodeVersion === 'function' && typeof candidate.decodeVersion === 'function';
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Identity codec for a versioned (temporal) kind family, resolving OQ-11 to the
|
|
24
|
+
* subtree-per-entity layout: every version of an entity is a distinct file under
|
|
25
|
+
* a per-entity subtree.
|
|
26
|
+
*
|
|
27
|
+
* @remarks
|
|
28
|
+
* - `encode`: scope = `<baseScope>/entities/<entityId>`, idStem = `<entityId>`,
|
|
29
|
+
* `isVersioned = true`. The idStem is the stable entity prefix; the store forms
|
|
30
|
+
* each version's filename via {@link TemporalIdentityCodec.encodeVersion}.
|
|
31
|
+
* - `encodeVersion`: version stem = `<entityId>-v<seq>`.
|
|
32
|
+
* - `decode` / `decodeVersion`: recover `entityId` (and `seq`) from a
|
|
33
|
+
* `(subtree scope, version stem)` pair; the scope is authoritative for the
|
|
34
|
+
* `entityId`.
|
|
35
|
+
* - Escaping: `baseScope` and `entityId` must each match the POSIX portable
|
|
36
|
+
* filename set (they become path segments); `seq` is a non-negative integer.
|
|
37
|
+
* - Layout: `vault/<baseScope>/entities/<entityId>/<entityId>-v<seq>.md`.
|
|
38
|
+
* @public
|
|
39
|
+
*/
|
|
40
|
+
class TemporalIdentityCodec {
|
|
41
|
+
constructor(baseScope) {
|
|
42
|
+
this.baseScope = baseScope;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Family-convention factory. Validates that `baseScope` is a single portable
|
|
46
|
+
* filename segment (it becomes the top-level path component).
|
|
47
|
+
*/
|
|
48
|
+
static create(baseScope) {
|
|
49
|
+
return (0, filenameSafety_1.assertPortableFilenameStem)(baseScope)
|
|
50
|
+
.withErrorFormat((msg) => `temporal codec: baseScope '${baseScope}': ${msg}`)
|
|
51
|
+
.onSuccess(() => (0, ts_utils_1.succeed)(new TemporalIdentityCodec(baseScope)));
|
|
52
|
+
}
|
|
53
|
+
/** {@inheritDoc IIdentityCodec.encode} */
|
|
54
|
+
encode(entityId) {
|
|
55
|
+
return (0, filenameSafety_1.assertPortableFilenameStem)(entityId)
|
|
56
|
+
.withErrorFormat((msg) => `temporal codec: entityId '${entityId}': ${msg}`)
|
|
57
|
+
.onSuccess((stem) => (0, ts_utils_1.succeed)({
|
|
58
|
+
scope: `${this.baseScope}/${TemporalIdentityCodec.entitiesSegment}/${stem}`,
|
|
59
|
+
idStem: stem,
|
|
60
|
+
isVersioned: true
|
|
61
|
+
}));
|
|
62
|
+
}
|
|
63
|
+
/** {@inheritDoc ITemporalIdentityCodec.encodeVersion} */
|
|
64
|
+
encodeVersion(entityId, seq) {
|
|
65
|
+
return (0, filenameSafety_1.assertPortableFilenameStem)(entityId)
|
|
66
|
+
.withErrorFormat((msg) => `temporal codec: entityId '${entityId}': ${msg}`)
|
|
67
|
+
.onSuccess((stem) => {
|
|
68
|
+
if (!Number.isInteger(seq) || seq < 0) {
|
|
69
|
+
return (0, ts_utils_1.fail)(`temporal codec: version seq '${seq}' must be a non-negative integer`);
|
|
70
|
+
}
|
|
71
|
+
return (0, ts_utils_1.succeed)(`${stem}${TemporalIdentityCodec.versionInfix}${seq}`);
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
/** {@inheritDoc IIdentityCodec.decode} */
|
|
75
|
+
decode(scope, encodedStem) {
|
|
76
|
+
return this.decodeVersion(scope, encodedStem).onSuccess((addr) => ids_1.Convert.entityId.convert(addr.entityId));
|
|
77
|
+
}
|
|
78
|
+
/** {@inheritDoc ITemporalIdentityCodec.decodeVersion} */
|
|
79
|
+
decodeVersion(scope, stem) {
|
|
80
|
+
return this._entityIdFromScope(scope).onSuccess((entityId) => {
|
|
81
|
+
const prefix = `${entityId}${TemporalIdentityCodec.versionInfix}`;
|
|
82
|
+
if (!stem.startsWith(prefix)) {
|
|
83
|
+
return (0, ts_utils_1.fail)(`temporal codec: version stem '${stem}' must begin with '${prefix}' (from scope '${scope}')`);
|
|
84
|
+
}
|
|
85
|
+
const seqText = stem.slice(prefix.length);
|
|
86
|
+
if (!TemporalIdentityCodec._versionSeqRe.test(seqText)) {
|
|
87
|
+
return (0, ts_utils_1.fail)(`temporal codec: version stem '${stem}' has a non-integer version suffix '${seqText}'`);
|
|
88
|
+
}
|
|
89
|
+
// The regex admits digit strings of unbounded length; `parseInt` would silently
|
|
90
|
+
// lose precision past MAX_SAFE_INTEGER, decoding a corrupt/tampered filename to a
|
|
91
|
+
// plausible-but-wrong `seq` that drives version ordering. Reject rather than corrupt.
|
|
92
|
+
const seq = Number.parseInt(seqText, 10);
|
|
93
|
+
if (!Number.isSafeInteger(seq)) {
|
|
94
|
+
return (0, ts_utils_1.fail)(`temporal codec: version stem '${stem}' has a version suffix '${seqText}' outside the safe integer range`);
|
|
95
|
+
}
|
|
96
|
+
return ids_1.Convert.entityId.convert(entityId).onSuccess((branded) => (0, ts_utils_1.succeed)({ entityId: branded, seq }));
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
/** {@inheritDoc IIdentityCodec.verifyRoundTrip} */
|
|
100
|
+
verifyRoundTrip(scope, stem) {
|
|
101
|
+
return this.decodeVersion(scope, stem).onSuccess((addr) => this.encode(addr.entityId).onSuccess((encoded) => this.encodeVersion(addr.entityId, addr.seq).onSuccess((reStem) => {
|
|
102
|
+
if (encoded.scope !== scope || reStem !== stem) {
|
|
103
|
+
return (0, ts_utils_1.fail)(`temporal codec: round-trip mismatch for scope '${scope}' stem '${stem}' (re-encoded to scope '${encoded.scope}' stem '${reStem}')`);
|
|
104
|
+
}
|
|
105
|
+
return (0, ts_utils_1.succeed)(true);
|
|
106
|
+
})));
|
|
107
|
+
}
|
|
108
|
+
/** Validate and extract the `entityId` from a `<baseScope>/entities/<entityId>` scope. */
|
|
109
|
+
_entityIdFromScope(scope) {
|
|
110
|
+
const segments = scope.split('/');
|
|
111
|
+
if (segments.length !== 3 ||
|
|
112
|
+
segments[0] !== this.baseScope ||
|
|
113
|
+
segments[1] !== TemporalIdentityCodec.entitiesSegment) {
|
|
114
|
+
return (0, ts_utils_1.fail)(`temporal codec: scope '${scope}' must be '${this.baseScope}/${TemporalIdentityCodec.entitiesSegment}/<entityId>'`);
|
|
115
|
+
}
|
|
116
|
+
const entityId = segments[2];
|
|
117
|
+
return (0, filenameSafety_1.assertPortableFilenameStem)(entityId)
|
|
118
|
+
.withErrorFormat((msg) => `temporal codec: entityId '${entityId}': ${msg}`)
|
|
119
|
+
.onSuccess(() => (0, ts_utils_1.succeed)(entityId));
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
exports.TemporalIdentityCodec = TemporalIdentityCodec;
|
|
123
|
+
/** The fixed subtree segment separating an entity's versions from its scope. */
|
|
124
|
+
TemporalIdentityCodec.entitiesSegment = 'entities';
|
|
125
|
+
/** The version-stem infix: `<entityId>` + this + `<seq>`. */
|
|
126
|
+
TemporalIdentityCodec.versionInfix = '-v';
|
|
127
|
+
/** A non-negative integer string (the version seq). */
|
|
128
|
+
TemporalIdentityCodec._versionSeqRe = /^\d+$/;
|
|
12
129
|
/**
|
|
13
130
|
* Identity codec for the knowledge kind family. A knowledge entity is keyed
|
|
14
131
|
* by its consumer-supplied `docId`, which is used verbatim as the filename
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"identityCodec.js","sourceRoot":"","sources":["../../../src/packlets/types/identityCodec.ts"],"names":[],"mappings":";AAAA;;;GAGG;;;AAEH,4CAAsD;AACtD,+BAA0D;AAC1D,qDAA8D;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AC9C,CAAC;IAED,yEAAyE;IACjE,MAAM,CAAC,kBAAkB,CAAC,IAAY;QAC5C,IAAI,CAAC,IAAI,CAAC,UAAU,CAAC,gBAAgB,CAAC,cAAc,CAAC,EAAE,CAAC;YACtD,OAAO,IAAA,eAAI,EAAC,oBAAoB,IAAI,sBAAsB,gBAAgB,CAAC,cAAc,GAAG,CAAC,CAAC;QAChG,CAAC;QACD,OAAO,gBAAgB,CAAC,gBAAgB,CAAC,IAAI,CAAC,KAAK,CAAC,gBAAgB,CAAC,cAAc,CAAC,MAAM,CAAC,CAAC,CAAC;IAC/F,CAAC;IAED,kEAAkE;IAC1D,MAAM,CAAC,gBAAgB,CAAC,SAAiB;QAC/C,IAAI,CAAC,gBAAgB,CAAC,YAAY,CAAC,IAAI,CAAC,SAAS,CAAC,EAAE,CAAC;YACnD,OAAO,IAAA,eAAI,EAAC,yBAAyB,SAAS,kCAAkC,CAAC,CAAC;QACpF,CAAC;QACD,OAAO,IAAA,kBAAO,EAAC,SAAS,CAAC,CAAC;IAC5B,CAAC;;AAjFH,4CAkFC;AAjFC,uEAAuE;AAChD,iCAAgB,GAAW,eAAe,CAAC;AAClE,wDAAwD;AACjC,+BAAc,GAAW,OAAO,CAAC;AAExD,0EAA0E;AAClD,6BAAY,GAAW,OAAO,CAAC","sourcesContent":["/*\n * Copyright (c) 2026 Erik Fortune\n * SPDX-License-Identifier: MIT\n */\n\nimport { Result, fail, succeed } from '@fgv/ts-utils';\nimport { Convert, EntityId, MemoryScopeKey } from './ids';\nimport { assertPortableFilenameStem } from './filenameSafety';\n\n/**\n * The FileTree storage address an {@link IIdentityCodec} maps a domain key to.\n * @public\n */\nexport interface IIdentityCodecResult {\n /** Scope path segment (may be multi-level, e.g. `conversations/<id>`). */\n readonly scope: MemoryScopeKey;\n /** Filename stem (the part before `.md`). Filename-safe after encoding. */\n readonly idStem: string;\n /**\n * Whether this kind uses a versioned layout (temporal: multiple files per\n * entity) vs. a flat layout (one file per entity). Non-temporal = always\n * `false`.\n */\n readonly isVersioned: boolean;\n}\n\n/**\n * Maps a consumer-supplied domain key ⇄ a FileTree storage address. Injected\n * per kind so the store never touches raw domain keys: the codec owns all\n * filename escaping and the flat-vs-versioned layout dispatch.\n * @public\n */\nexport interface IIdentityCodec {\n /**\n * Encode a consumer-supplied entity id to a FileTree address. Deterministic\n * and pure — no I/O.\n */\n encode(entityId: EntityId): Result<IIdentityCodecResult>;\n\n /**\n * Decode a FileTree address back to the original {@link EntityId}. The exact\n * inverse of {@link IIdentityCodec.encode | encode} for non-versioned kinds.\n */\n decode(scope: MemoryScopeKey, encodedStem: string): Result<EntityId>;\n\n /**\n * Assert that `encode(decode(scope, stem)).idStem === stem`. Used by the\n * store's `verifyFilenameId` check on load.\n */\n verifyRoundTrip(scope: MemoryScopeKey, stem: string): Result<true>;\n}\n\n/**\n/**\n * Identity codec for the knowledge kind family. A knowledge entity is keyed\n * by its consumer-supplied `docId`, which is used verbatim as the filename\n * stem under the flat `knowledge` scope.\n *\n * @remarks\n * - `encode`: scope = `knowledge`, idStem = `docId`, `isVersioned = false`.\n * - `decode`: brands the stem back to an {@link EntityId}.\n * - Escaping: the `docId` must match the POSIX portable filename set.\n * - Layout: `vault/knowledge/<docId>.md`.\n * @public\n */\nexport class KnowledgeIdentityCodec implements IIdentityCodec {\n /** The fixed scope for every knowledge entity. */\n public static readonly scope: MemoryScopeKey = 'knowledge' as MemoryScopeKey;\n\n /** {@inheritDoc IIdentityCodec.encode} */\n public encode(entityId: EntityId): Result<IIdentityCodecResult> {\n return assertPortableFilenameStem(entityId).onSuccess((idStem) =>\n succeed({ scope: KnowledgeIdentityCodec.scope, idStem, isVersioned: false })\n );\n }\n\n /** {@inheritDoc IIdentityCodec.decode} */\n public decode(scope: MemoryScopeKey, encodedStem: string): Result<EntityId> {\n if (scope !== KnowledgeIdentityCodec.scope) {\n return fail(\n `knowledge codec: scope '${scope}' does not match expected scope '${KnowledgeIdentityCodec.scope}'`\n );\n }\n return assertPortableFilenameStem(encodedStem).onSuccess((stem) => Convert.entityId.convert(stem));\n }\n\n /** {@inheritDoc IIdentityCodec.verifyRoundTrip} */\n public verifyRoundTrip(scope: MemoryScopeKey, stem: string): Result<true> {\n return this.decode(scope, stem)\n .onSuccess((entityId) => this.encode(entityId))\n .onSuccess((encoded) => {\n /* c8 ignore start -- defensive: for the identity (knowledge) codec, encode(decode(stem)).idStem always equals stem when both succeed; the guard exists for the non-identity codecs (LTM/MTM, Phase C) that reuse this contract */\n if (encoded.idStem !== stem) {\n return fail(\n `knowledge codec: round-trip mismatch for stem '${stem}' (re-encoded to '${encoded.idStem}')`\n );\n }\n /* c8 ignore stop */\n return succeed(true);\n });\n }\n}\n\n/**\n * Identity codec for the long-term-memory (LTM) kind family. An LTM entity is\n * keyed by its `conversationId`, used verbatim as the filename stem under the\n * flat `conversations` scope.\n *\n * @remarks\n * - `encode`: scope = `conversations`, idStem = `conversationId`, `isVersioned = false`.\n * - `decode`: brands the stem back to an {@link EntityId}.\n * - Escaping: the `conversationId` must match the POSIX portable filename set.\n * - Layout: `vault/conversations/<conversationId>.md`.\n *\n * An LTM file (`conversations/<id>.md`) and the MTM subtree\n * (`conversations/<id>/turn-N.md`) coexist — a file and a same-named directory\n * are independent on every supported FileTree backend.\n * @public\n */\nexport class LtmIdentityCodec implements IIdentityCodec {\n /** The fixed scope for every LTM entity. */\n public static readonly scope: MemoryScopeKey = 'conversations' as MemoryScopeKey;\n\n /** {@inheritDoc IIdentityCodec.encode} */\n public encode(entityId: EntityId): Result<IIdentityCodecResult> {\n return assertPortableFilenameStem(entityId).onSuccess((idStem) =>\n succeed({ scope: LtmIdentityCodec.scope, idStem, isVersioned: false })\n );\n }\n\n /** {@inheritDoc IIdentityCodec.decode} */\n public decode(scope: MemoryScopeKey, encodedStem: string): Result<EntityId> {\n if (scope !== LtmIdentityCodec.scope) {\n return fail(`LTM codec: scope '${scope}' does not match expected scope '${LtmIdentityCodec.scope}'`);\n }\n return assertPortableFilenameStem(encodedStem).onSuccess((stem) => Convert.entityId.convert(stem));\n }\n\n /** {@inheritDoc IIdentityCodec.verifyRoundTrip} */\n public verifyRoundTrip(scope: MemoryScopeKey, stem: string): Result<true> {\n return this.decode(scope, stem)\n .onSuccess((entityId) => this.encode(entityId))\n .onSuccess((encoded) => {\n /* c8 ignore start -- defensive: LTM encode(decode(stem)).idStem always equals stem when both succeed (identity mapping); guard preserves the contract for direct callers */\n if (encoded.idStem !== stem) {\n return fail(\n `LTM codec: round-trip mismatch for stem '${stem}' (re-encoded to '${encoded.idStem}')`\n );\n }\n /* c8 ignore stop */\n return succeed(true);\n });\n }\n}\n\n/**\n * Identity codec for the medium-term-memory (MTM) kind family. An MTM entity is\n * keyed by the colon-composite `<conversationId>:<turnIndex>` and maps into a\n * per-conversation subtree.\n *\n * @remarks\n * - `encode`: splits the entity id on `:`; scope =\n * `conversations/<conversationId>` (multi-segment), idStem = `turn-<turnIndex>`,\n * `isVersioned = false`.\n * - `decode`: reverses scope `conversations/<id>` + stem `turn-<N>` to the\n * composite `<id>:<N>`.\n * - Escaping: `conversationId` must match the POSIX portable filename set (so it\n * contains no `/` or `:`); `turnIndex` must be a non-negative integer string,\n * preserved verbatim so the round-trip is exact.\n * - Layout: `vault/conversations/<conversationId>/turn-<N>.md`. The `/` in the\n * scope is handled by the store's multi-segment scope resolver.\n *\n * **Verbatim turn index (caller-canonicalization note).** The turn index is\n * preserved exactly — `conv-1:7` and `conv-1:007` are DISTINCT entities mapping\n * to distinct files (`turn-7.md` / `turn-007.md`). Verbatim preservation is what\n * makes the round-trip exact, but it means a caller that formats turn indices\n * inconsistently (some zero-padded, some not) will silently mint separate\n * entities. Callers should canonicalize to one form (e.g. no leading zeros).\n * @public\n */\nexport class MtmIdentityCodec implements IIdentityCodec {\n /** The top-level scope segment under which every MTM subtree lives. */\n public static readonly rootScopeSegment: string = 'conversations';\n /** The fixed filename-stem prefix for a turn record. */\n public static readonly turnStemPrefix: string = 'turn-';\n\n /** A non-negative integer string (the turn index), preserved verbatim. */\n private static readonly _turnIndexRe: RegExp = /^\\d+$/;\n\n /** {@inheritDoc IIdentityCodec.encode} */\n public encode(entityId: EntityId): Result<IIdentityCodecResult> {\n const parts: string[] = entityId.split(':');\n if (parts.length !== 2) {\n return fail(`MTM codec: entity id '${entityId}' must be a '<conversationId>:<turnIndex>' composite`);\n }\n const [conversationId, turnIndex] = parts;\n return assertPortableFilenameStem(conversationId)\n .withErrorFormat((msg) => `MTM codec: conversationId '${conversationId}': ${msg}`)\n .onSuccess(() => MtmIdentityCodec._assertTurnIndex(turnIndex))\n .onSuccess(() =>\n succeed({\n scope: `${MtmIdentityCodec.rootScopeSegment}/${conversationId}` as MemoryScopeKey,\n idStem: `${MtmIdentityCodec.turnStemPrefix}${turnIndex}`,\n isVersioned: false\n })\n );\n }\n\n /** {@inheritDoc IIdentityCodec.decode} */\n public decode(scope: MemoryScopeKey, encodedStem: string): Result<EntityId> {\n return MtmIdentityCodec._conversationIdFromScope(scope).onSuccess((conversationId) =>\n MtmIdentityCodec._turnIndexFromStem(encodedStem).onSuccess((turnIndex) =>\n Convert.entityId.convert(`${conversationId}:${turnIndex}`)\n )\n );\n }\n\n /** {@inheritDoc IIdentityCodec.verifyRoundTrip} */\n public verifyRoundTrip(scope: MemoryScopeKey, stem: string): Result<true> {\n return this.decode(scope, stem)\n .onSuccess((entityId) => this.encode(entityId))\n .onSuccess((encoded) => {\n /* c8 ignore start -- defensive: MTM encode(decode(scope, stem)) reproduces the same scope+stem whenever both succeed (no normalization); guard preserves the contract for direct callers */\n if (encoded.idStem !== stem || encoded.scope !== scope) {\n return fail(\n `MTM codec: round-trip mismatch for scope '${scope}' stem '${stem}' (re-encoded to scope '${encoded.scope}' stem '${encoded.idStem}')`\n );\n }\n /* c8 ignore stop */\n return succeed(true);\n });\n }\n\n /** Validate and extract the `conversationId` from a `conversations/<id>` scope. */\n private static _conversationIdFromScope(scope: MemoryScopeKey): Result<string> {\n const segments: string[] = scope.split('/');\n if (segments.length !== 2 || segments[0] !== MtmIdentityCodec.rootScopeSegment) {\n return fail(\n `MTM codec: scope '${scope}' must be '${MtmIdentityCodec.rootScopeSegment}/<conversationId>'`\n );\n }\n const conversationId: string = segments[1];\n return assertPortableFilenameStem(conversationId)\n .withErrorFormat((msg) => `MTM codec: conversationId '${conversationId}': ${msg}`)\n .onSuccess(() => succeed(conversationId));\n }\n\n /** Validate and extract the turn-index digits from a `turn-<N>` stem. */\n private static _turnIndexFromStem(stem: string): Result<string> {\n if (!stem.startsWith(MtmIdentityCodec.turnStemPrefix)) {\n return fail(`MTM codec: stem '${stem}' must begin with '${MtmIdentityCodec.turnStemPrefix}'`);\n }\n return MtmIdentityCodec._assertTurnIndex(stem.slice(MtmIdentityCodec.turnStemPrefix.length));\n }\n\n /** Assert a string is a non-negative integer (the turn index). */\n private static _assertTurnIndex(turnIndex: string): Result<string> {\n if (!MtmIdentityCodec._turnIndexRe.test(turnIndex)) {\n return fail(`MTM codec: turnIndex '${turnIndex}' must be a non-negative integer`);\n }\n return succeed(turnIndex);\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"identityCodec.js","sourceRoot":"","sources":["../../../src/packlets/types/identityCodec.ts"],"names":[],"mappings":";AAAA;;;GAGG;;;AA4FH,0DAGC;AA7FD,4CAAsD;AACtD,+BAA0D;AAC1D,qDAA8D;AAkF9D;;;;;GAKG;AACH,SAAgB,uBAAuB,CAAC,KAAqB;IAC3D,MAAM,SAAS,GAAoC,KAAwC,CAAC;IAC5F,OAAO,OAAO,SAAS,CAAC,aAAa,KAAK,UAAU,IAAI,OAAO,SAAS,CAAC,aAAa,KAAK,UAAU,CAAC;AACxG,CAAC;AAED;;;;;;;;;;;;;;;;;GAiBG;AACH,MAAa,qBAAqB;IAYhC,YAAoB,SAAiB;QACnC,IAAI,CAAC,SAAS,GAAG,SAAS,CAAC;IAC7B,CAAC;IAED;;;OAGG;IACI,MAAM,CAAC,MAAM,CAAC,SAAiB;QACpC,OAAO,IAAA,2CAA0B,EAAC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identifier: MIT\n */\n\nimport { Result, fail, succeed } from '@fgv/ts-utils';\nimport { Convert, EntityId, MemoryScopeKey } from './ids';\nimport { assertPortableFilenameStem } from './filenameSafety';\n\n/**\n * The FileTree storage address an {@link IIdentityCodec} maps a domain key to.\n * @public\n */\nexport interface IIdentityCodecResult {\n /** Scope path segment (may be multi-level, e.g. `conversations/<id>`). */\n readonly scope: MemoryScopeKey;\n /** Filename stem (the part before `.md`). Filename-safe after encoding. */\n readonly idStem: string;\n /**\n * Whether this kind uses a versioned layout (temporal: multiple files per\n * entity) vs. a flat layout (one file per entity). Non-temporal = always\n * `false`.\n */\n readonly isVersioned: boolean;\n}\n\n/**\n * Maps a consumer-supplied domain key ⇄ a FileTree storage address. Injected\n * per kind so the store never touches raw domain keys: the codec owns all\n * filename escaping and the flat-vs-versioned layout dispatch.\n * @public\n */\nexport interface IIdentityCodec {\n /**\n * Encode a consumer-supplied entity id to a FileTree address. Deterministic\n * and pure — no I/O.\n */\n encode(entityId: EntityId): Result<IIdentityCodecResult>;\n\n /**\n * Decode a FileTree address back to the original {@link EntityId}. The exact\n * inverse of {@link IIdentityCodec.encode | encode} for non-versioned kinds.\n */\n decode(scope: MemoryScopeKey, encodedStem: string): Result<EntityId>;\n\n /**\n * Assert that `encode(decode(scope, stem)).idStem === stem`. Used by the\n * store's `verifyFilenameId` check on load.\n */\n verifyRoundTrip(scope: MemoryScopeKey, stem: string): Result<true>;\n}\n\n/**\n * The `(entityId, version seq)` an {@link ITemporalIdentityCodec.decodeVersion}\n * recovers from a version file address.\n * @public\n */\nexport interface ITemporalVersionAddress {\n /** The stable consumer-supplied domain key. */\n readonly entityId: EntityId;\n /** The version's monotonic `seq` (the `v<seq>` component of the file stem). */\n readonly seq: number;\n}\n\n/**\n * Additive extension of {@link IIdentityCodec} for versioned (temporal) kinds. A\n * codec whose {@link IIdentityCodec.encode | encode} reports `isVersioned: true`\n * implements this so the store can form and parse per-version filenames without\n * knowing the layout. Non-versioned codecs do NOT implement it (probe with\n * {@link isTemporalIdentityCodec}).\n * @public\n */\nexport interface ITemporalIdentityCodec extends IIdentityCodec {\n /**\n * Form the version filename stem for a specific `(entityId, seq)`. The store\n * appends the extension and writes it under the entity subtree returned by\n * {@link IIdentityCodec.encode | encode}.\n */\n encodeVersion(entityId: EntityId, seq: number): Result<string>;\n\n /**\n * Parse a `(subtree scope, version stem)` back to its\n * {@link ITemporalVersionAddress}. The subtree scope is authoritative for the\n * `entityId`, disambiguating a stem whose `entityId` itself ends in\n * `-v<digits>`.\n */\n decodeVersion(scope: MemoryScopeKey, stem: string): Result<ITemporalVersionAddress>;\n}\n\n/**\n * Narrow an {@link IIdentityCodec} to {@link ITemporalIdentityCodec} by probing\n * for the versioned methods. Used by the store when an `encode` result reports\n * `isVersioned: true`.\n * @public\n */\nexport function isTemporalIdentityCodec(codec: IIdentityCodec): codec is ITemporalIdentityCodec {\n const candidate: Partial<ITemporalIdentityCodec> = codec as Partial<ITemporalIdentityCodec>;\n return typeof candidate.encodeVersion === 'function' && typeof candidate.decodeVersion === 'function';\n}\n\n/**\n * Identity codec for a versioned (temporal) kind family, resolving OQ-11 to the\n * subtree-per-entity layout: every version of an entity is a distinct file under\n * a per-entity subtree.\n *\n * @remarks\n * - `encode`: scope = `<baseScope>/entities/<entityId>`, idStem = `<entityId>`,\n * `isVersioned = true`. The idStem is the stable entity prefix; the store forms\n * each version's filename via {@link TemporalIdentityCodec.encodeVersion}.\n * - `encodeVersion`: version stem = `<entityId>-v<seq>`.\n * - `decode` / `decodeVersion`: recover `entityId` (and `seq`) from a\n * `(subtree scope, version stem)` pair; the scope is authoritative for the\n * `entityId`.\n * - Escaping: `baseScope` and `entityId` must each match the POSIX portable\n * filename set (they become path segments); `seq` is a non-negative integer.\n * - Layout: `vault/<baseScope>/entities/<entityId>/<entityId>-v<seq>.md`.\n * @public\n */\nexport class TemporalIdentityCodec implements ITemporalIdentityCodec {\n /** The fixed subtree segment separating an entity's versions from its scope. */\n public static readonly entitiesSegment: string = 'entities';\n /** The version-stem infix: `<entityId>` + this + `<seq>`. */\n public static readonly versionInfix: string = '-v';\n\n /** A non-negative integer string (the version seq). */\n private static readonly _versionSeqRe: RegExp = /^\\d+$/;\n\n /** The base scope segment this codec's entities live under. */\n public readonly baseScope: string;\n\n private constructor(baseScope: string) {\n this.baseScope = baseScope;\n }\n\n /**\n * Family-convention factory. Validates that `baseScope` is a single portable\n * filename segment (it becomes the top-level path component).\n */\n public static create(baseScope: string): Result<TemporalIdentityCodec> {\n return assertPortableFilenameStem(baseScope)\n .withErrorFormat((msg) => `temporal codec: baseScope '${baseScope}': ${msg}`)\n .onSuccess(() => succeed(new TemporalIdentityCodec(baseScope)));\n }\n\n /** {@inheritDoc IIdentityCodec.encode} */\n public encode(entityId: EntityId): Result<IIdentityCodecResult> {\n return assertPortableFilenameStem(entityId)\n .withErrorFormat((msg) => `temporal codec: entityId '${entityId}': ${msg}`)\n .onSuccess((stem) =>\n succeed({\n scope: `${this.baseScope}/${TemporalIdentityCodec.entitiesSegment}/${stem}` as MemoryScopeKey,\n idStem: stem,\n isVersioned: true\n })\n );\n }\n\n /** {@inheritDoc ITemporalIdentityCodec.encodeVersion} */\n public encodeVersion(entityId: EntityId, seq: number): Result<string> {\n return assertPortableFilenameStem(entityId)\n .withErrorFormat((msg) => `temporal codec: entityId '${entityId}': ${msg}`)\n .onSuccess((stem) => {\n if (!Number.isInteger(seq) || seq < 0) {\n return fail(`temporal codec: version seq '${seq}' must be a non-negative integer`);\n }\n return succeed(`${stem}${TemporalIdentityCodec.versionInfix}${seq}`);\n });\n }\n\n /** {@inheritDoc IIdentityCodec.decode} */\n public decode(scope: MemoryScopeKey, encodedStem: string): Result<EntityId> {\n return this.decodeVersion(scope, encodedStem).onSuccess((addr) =>\n Convert.entityId.convert(addr.entityId)\n );\n }\n\n /** {@inheritDoc ITemporalIdentityCodec.decodeVersion} */\n public decodeVersion(scope: MemoryScopeKey, stem: string): Result<ITemporalVersionAddress> {\n return this._entityIdFromScope(scope).onSuccess((entityId) => {\n const prefix: string = `${entityId}${TemporalIdentityCodec.versionInfix}`;\n if (!stem.startsWith(prefix)) {\n return fail(\n `temporal codec: version stem '${stem}' must begin with '${prefix}' (from scope '${scope}')`\n );\n }\n const seqText: string = stem.slice(prefix.length);\n if (!TemporalIdentityCodec._versionSeqRe.test(seqText)) {\n return fail(`temporal codec: version stem '${stem}' has a non-integer version suffix '${seqText}'`);\n }\n // The regex admits digit strings of unbounded length; `parseInt` would silently\n // lose precision past MAX_SAFE_INTEGER, decoding a corrupt/tampered filename to a\n // plausible-but-wrong `seq` that drives version ordering. Reject rather than corrupt.\n const seq: number = Number.parseInt(seqText, 10);\n if (!Number.isSafeInteger(seq)) {\n return fail(\n `temporal codec: version stem '${stem}' has a version suffix '${seqText}' outside the safe integer range`\n );\n }\n return Convert.entityId.convert(entityId).onSuccess((branded) => succeed({ entityId: branded, seq }));\n });\n }\n\n /** {@inheritDoc IIdentityCodec.verifyRoundTrip} */\n public verifyRoundTrip(scope: MemoryScopeKey, stem: string): Result<true> {\n return this.decodeVersion(scope, stem).onSuccess((addr) =>\n this.encode(addr.entityId).onSuccess((encoded) =>\n this.encodeVersion(addr.entityId, addr.seq).onSuccess((reStem) => {\n if (encoded.scope !== scope || reStem !== stem) {\n return fail(\n `temporal codec: round-trip mismatch for scope '${scope}' stem '${stem}' (re-encoded to scope '${encoded.scope}' stem '${reStem}')`\n );\n }\n return succeed(true);\n })\n )\n );\n }\n\n /** Validate and extract the `entityId` from a `<baseScope>/entities/<entityId>` scope. */\n private _entityIdFromScope(scope: MemoryScopeKey): Result<string> {\n const segments: string[] = scope.split('/');\n if (\n segments.length !== 3 ||\n segments[0] !== this.baseScope ||\n segments[1] !== TemporalIdentityCodec.entitiesSegment\n ) {\n return fail(\n `temporal codec: scope '${scope}' must be '${this.baseScope}/${TemporalIdentityCodec.entitiesSegment}/<entityId>'`\n );\n }\n const entityId: string = segments[2];\n return assertPortableFilenameStem(entityId)\n .withErrorFormat((msg) => `temporal codec: entityId '${entityId}': ${msg}`)\n .onSuccess(() => succeed(entityId));\n }\n}\n\n/**\n * Identity codec for the knowledge kind family. A knowledge entity is keyed\n * by its consumer-supplied `docId`, which is used verbatim as the filename\n * stem under the flat `knowledge` scope.\n *\n * @remarks\n * - `encode`: scope = `knowledge`, idStem = `docId`, `isVersioned = false`.\n * - `decode`: brands the stem back to an {@link EntityId}.\n * - Escaping: the `docId` must match the POSIX portable filename set.\n * - Layout: `vault/knowledge/<docId>.md`.\n * @public\n */\nexport class KnowledgeIdentityCodec implements IIdentityCodec {\n /** The fixed scope for every knowledge entity. */\n public static readonly scope: MemoryScopeKey = 'knowledge' as MemoryScopeKey;\n\n /** {@inheritDoc IIdentityCodec.encode} */\n public encode(entityId: EntityId): Result<IIdentityCodecResult> {\n return assertPortableFilenameStem(entityId).onSuccess((idStem) =>\n succeed({ scope: KnowledgeIdentityCodec.scope, idStem, isVersioned: false })\n );\n }\n\n /** {@inheritDoc IIdentityCodec.decode} */\n public decode(scope: MemoryScopeKey, encodedStem: string): Result<EntityId> {\n if (scope !== KnowledgeIdentityCodec.scope) {\n return fail(\n `knowledge codec: scope '${scope}' does not match expected scope '${KnowledgeIdentityCodec.scope}'`\n );\n }\n return assertPortableFilenameStem(encodedStem).onSuccess((stem) => Convert.entityId.convert(stem));\n }\n\n /** {@inheritDoc IIdentityCodec.verifyRoundTrip} */\n public verifyRoundTrip(scope: MemoryScopeKey, stem: string): Result<true> {\n return this.decode(scope, stem)\n .onSuccess((entityId) => this.encode(entityId))\n .onSuccess((encoded) => {\n /* c8 ignore start -- defensive: for the identity (knowledge) codec, encode(decode(stem)).idStem always equals stem when both succeed; the guard exists for the non-identity codecs (LTM/MTM, Phase C) that reuse this contract */\n if (encoded.idStem !== stem) {\n return fail(\n `knowledge codec: round-trip mismatch for stem '${stem}' (re-encoded to '${encoded.idStem}')`\n );\n }\n /* c8 ignore stop */\n return succeed(true);\n });\n }\n}\n\n/**\n * Identity codec for the long-term-memory (LTM) kind family. An LTM entity is\n * keyed by its `conversationId`, used verbatim as the filename stem under the\n * flat `conversations` scope.\n *\n * @remarks\n * - `encode`: scope = `conversations`, idStem = `conversationId`, `isVersioned = false`.\n * - `decode`: brands the stem back to an {@link EntityId}.\n * - Escaping: the `conversationId` must match the POSIX portable filename set.\n * - Layout: `vault/conversations/<conversationId>.md`.\n *\n * An LTM file (`conversations/<id>.md`) and the MTM subtree\n * (`conversations/<id>/turn-N.md`) coexist — a file and a same-named directory\n * are independent on every supported FileTree backend.\n * @public\n */\nexport class LtmIdentityCodec implements IIdentityCodec {\n /** The fixed scope for every LTM entity. */\n public static readonly scope: MemoryScopeKey = 'conversations' as MemoryScopeKey;\n\n /** {@inheritDoc IIdentityCodec.encode} */\n public encode(entityId: EntityId): Result<IIdentityCodecResult> {\n return assertPortableFilenameStem(entityId).onSuccess((idStem) =>\n succeed({ scope: LtmIdentityCodec.scope, idStem, isVersioned: false })\n );\n }\n\n /** {@inheritDoc IIdentityCodec.decode} */\n public decode(scope: MemoryScopeKey, encodedStem: string): Result<EntityId> {\n if (scope !== LtmIdentityCodec.scope) {\n return fail(`LTM codec: scope '${scope}' does not match expected scope '${LtmIdentityCodec.scope}'`);\n }\n return assertPortableFilenameStem(encodedStem).onSuccess((stem) => Convert.entityId.convert(stem));\n }\n\n /** {@inheritDoc IIdentityCodec.verifyRoundTrip} */\n public verifyRoundTrip(scope: MemoryScopeKey, stem: string): Result<true> {\n return this.decode(scope, stem)\n .onSuccess((entityId) => this.encode(entityId))\n .onSuccess((encoded) => {\n /* c8 ignore start -- defensive: LTM encode(decode(stem)).idStem always equals stem when both succeed (identity mapping); guard preserves the contract for direct callers */\n if (encoded.idStem !== stem) {\n return fail(\n `LTM codec: round-trip mismatch for stem '${stem}' (re-encoded to '${encoded.idStem}')`\n );\n }\n /* c8 ignore stop */\n return succeed(true);\n });\n }\n}\n\n/**\n * Identity codec for the medium-term-memory (MTM) kind family. An MTM entity is\n * keyed by the colon-composite `<conversationId>:<turnIndex>` and maps into a\n * per-conversation subtree.\n *\n * @remarks\n * - `encode`: splits the entity id on `:`; scope =\n * `conversations/<conversationId>` (multi-segment), idStem = `turn-<turnIndex>`,\n * `isVersioned = false`.\n * - `decode`: reverses scope `conversations/<id>` + stem `turn-<N>` to the\n * composite `<id>:<N>`.\n * - Escaping: `conversationId` must match the POSIX portable filename set (so it\n * contains no `/` or `:`); `turnIndex` must be a non-negative integer string,\n * preserved verbatim so the round-trip is exact.\n * - Layout: `vault/conversations/<conversationId>/turn-<N>.md`. The `/` in the\n * scope is handled by the store's multi-segment scope resolver.\n *\n * **Verbatim turn index (caller-canonicalization note).** The turn index is\n * preserved exactly — `conv-1:7` and `conv-1:007` are DISTINCT entities mapping\n * to distinct files (`turn-7.md` / `turn-007.md`). Verbatim preservation is what\n * makes the round-trip exact, but it means a caller that formats turn indices\n * inconsistently (some zero-padded, some not) will silently mint separate\n * entities. Callers should canonicalize to one form (e.g. no leading zeros).\n * @public\n */\nexport class MtmIdentityCodec implements IIdentityCodec {\n /** The top-level scope segment under which every MTM subtree lives. */\n public static readonly rootScopeSegment: string = 'conversations';\n /** The fixed filename-stem prefix for a turn record. */\n public static readonly turnStemPrefix: string = 'turn-';\n\n /** A non-negative integer string (the turn index), preserved verbatim. */\n private static readonly _turnIndexRe: RegExp = /^\\d+$/;\n\n /** {@inheritDoc IIdentityCodec.encode} */\n public encode(entityId: EntityId): Result<IIdentityCodecResult> {\n const parts: string[] = entityId.split(':');\n if (parts.length !== 2) {\n return fail(`MTM codec: entity id '${entityId}' must be a '<conversationId>:<turnIndex>' composite`);\n }\n const [conversationId, turnIndex] = parts;\n return assertPortableFilenameStem(conversationId)\n .withErrorFormat((msg) => `MTM codec: conversationId '${conversationId}': ${msg}`)\n .onSuccess(() => MtmIdentityCodec._assertTurnIndex(turnIndex))\n .onSuccess(() =>\n succeed({\n scope: `${MtmIdentityCodec.rootScopeSegment}/${conversationId}` as MemoryScopeKey,\n idStem: `${MtmIdentityCodec.turnStemPrefix}${turnIndex}`,\n isVersioned: false\n })\n );\n }\n\n /** {@inheritDoc IIdentityCodec.decode} */\n public decode(scope: MemoryScopeKey, encodedStem: string): Result<EntityId> {\n return MtmIdentityCodec._conversationIdFromScope(scope).onSuccess((conversationId) =>\n MtmIdentityCodec._turnIndexFromStem(encodedStem).onSuccess((turnIndex) =>\n Convert.entityId.convert(`${conversationId}:${turnIndex}`)\n )\n );\n }\n\n /** {@inheritDoc IIdentityCodec.verifyRoundTrip} */\n public verifyRoundTrip(scope: MemoryScopeKey, stem: string): Result<true> {\n return this.decode(scope, stem)\n .onSuccess((entityId) => this.encode(entityId))\n .onSuccess((encoded) => {\n /* c8 ignore start -- defensive: MTM encode(decode(scope, stem)) reproduces the same scope+stem whenever both succeed (no normalization); guard preserves the contract for direct callers */\n if (encoded.idStem !== stem || encoded.scope !== scope) {\n return fail(\n `MTM codec: round-trip mismatch for scope '${scope}' stem '${stem}' (re-encoded to scope '${encoded.scope}' stem '${encoded.idStem}')`\n );\n }\n /* c8 ignore stop */\n return succeed(true);\n });\n }\n\n /** Validate and extract the `conversationId` from a `conversations/<id>` scope. */\n private static _conversationIdFromScope(scope: MemoryScopeKey): Result<string> {\n const segments: string[] = scope.split('/');\n if (segments.length !== 2 || segments[0] !== MtmIdentityCodec.rootScopeSegment) {\n return fail(\n `MTM codec: scope '${scope}' must be '${MtmIdentityCodec.rootScopeSegment}/<conversationId>'`\n );\n }\n const conversationId: string = segments[1];\n return assertPortableFilenameStem(conversationId)\n .withErrorFormat((msg) => `MTM codec: conversationId '${conversationId}': ${msg}`)\n .onSuccess(() => succeed(conversationId));\n }\n\n /** Validate and extract the turn-index digits from a `turn-<N>` stem. */\n private static _turnIndexFromStem(stem: string): Result<string> {\n if (!stem.startsWith(MtmIdentityCodec.turnStemPrefix)) {\n return fail(`MTM codec: stem '${stem}' must begin with '${MtmIdentityCodec.turnStemPrefix}'`);\n }\n return MtmIdentityCodec._assertTurnIndex(stem.slice(MtmIdentityCodec.turnStemPrefix.length));\n }\n\n /** Assert a string is a non-negative integer (the turn index). */\n private static _assertTurnIndex(turnIndex: string): Result<string> {\n if (!MtmIdentityCodec._turnIndexRe.test(turnIndex)) {\n return fail(`MTM codec: turnIndex '${turnIndex}' must be a non-negative integer`);\n }\n return succeed(turnIndex);\n }\n}\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/packlets/types/index.ts"],"names":[],"mappings":"AAKA,cAAc,OAAO,CAAC;AACtB,cAAc,YAAY,CAAC;AAC3B,cAAc,kBAAkB,CAAC;AACjC,cAAc,iBAAiB,CAAC;AAChC,cAAc,eAAe,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/packlets/types/index.ts"],"names":[],"mappings":"AAKA,cAAc,OAAO,CAAC;AACtB,cAAc,YAAY,CAAC;AAC3B,cAAc,kBAAkB,CAAC;AACjC,cAAc,iBAAiB,CAAC;AAChC,cAAc,YAAY,CAAC;AAC3B,cAAc,eAAe,CAAC"}
|
|
@@ -22,5 +22,6 @@ __exportStar(require("./ids"), exports);
|
|
|
22
22
|
__exportStar(require("./envelope"), exports);
|
|
23
23
|
__exportStar(require("./filenameSafety"), exports);
|
|
24
24
|
__exportStar(require("./identityCodec"), exports);
|
|
25
|
+
__exportStar(require("./temporal"), exports);
|
|
25
26
|
__exportStar(require("./writePolicy"), exports);
|
|
26
27
|
//# sourceMappingURL=index.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/packlets/types/index.ts"],"names":[],"mappings":";AAAA;;;GAGG;;;;;;;;;;;;;;;;AAEH,wCAAsB;AACtB,6CAA2B;AAC3B,mDAAiC;AACjC,kDAAgC;AAChC,gDAA8B","sourcesContent":["/*\n * Copyright (c) 2026 Erik Fortune\n * SPDX-License-Identifier: MIT\n */\n\nexport * from './ids';\nexport * from './envelope';\nexport * from './filenameSafety';\nexport * from './identityCodec';\nexport * from './writePolicy';\n"]}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/packlets/types/index.ts"],"names":[],"mappings":";AAAA;;;GAGG;;;;;;;;;;;;;;;;AAEH,wCAAsB;AACtB,6CAA2B;AAC3B,mDAAiC;AACjC,kDAAgC;AAChC,6CAA2B;AAC3B,gDAA8B","sourcesContent":["/*\n * Copyright (c) 2026 Erik Fortune\n * SPDX-License-Identifier: MIT\n */\n\nexport * from './ids';\nexport * from './envelope';\nexport * from './filenameSafety';\nexport * from './identityCodec';\nexport * from './temporal';\nexport * from './writePolicy';\n"]}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { IMemoryRecord } from './envelope';
|
|
2
|
+
/**
|
|
3
|
+
* Whether a record participates in the versioned (temporal) layout. A temporal
|
|
4
|
+
* record always carries a {@link ITemporalBlock | temporal} block (the store
|
|
5
|
+
* stamps `valid_at` on every versioned write); an atemporal record never does,
|
|
6
|
+
* so presence of `temporal` is the discriminator. Independent of `id`/`entityId`
|
|
7
|
+
* divergence (MTM is flat yet has `entityId !== id`).
|
|
8
|
+
* @public
|
|
9
|
+
*/
|
|
10
|
+
export declare function isTemporalRecord(record: IMemoryRecord<unknown>): boolean;
|
|
11
|
+
/**
|
|
12
|
+
* Whether a temporal record is a *current* version — its `temporal.invalid_at`
|
|
13
|
+
* is `null` or absent (the still-valid sentinel). A non-temporal record is never
|
|
14
|
+
* current in this sense (returns `false`).
|
|
15
|
+
* @public
|
|
16
|
+
*/
|
|
17
|
+
export declare function isVersionCurrent(record: IMemoryRecord<unknown>): boolean;
|
|
18
|
+
/**
|
|
19
|
+
* Whether a temporal record's validity interval contains `asOf` (epoch ms):
|
|
20
|
+
* `valid_at <= asOf` and (`invalid_at` is null/absent OR `asOf < invalid_at`).
|
|
21
|
+
* The version's `valid_at` defaults to its `created` when absent; a non-temporal
|
|
22
|
+
* record is never "valid at" a point (returns `false`).
|
|
23
|
+
* @public
|
|
24
|
+
*/
|
|
25
|
+
export declare function isVersionValidAt(record: IMemoryRecord<unknown>, asOf: number): boolean;
|
|
26
|
+
/**
|
|
27
|
+
* Select the current version from a set of an entity's versions: the newest
|
|
28
|
+
* (highest `seq`) version whose `invalid_at` is null/absent. `undefined` when the
|
|
29
|
+
* entity has no current version (fully invalidated / soft-deleted, or empty).
|
|
30
|
+
* @public
|
|
31
|
+
*/
|
|
32
|
+
export declare function selectCurrentVersion(versions: ReadonlyArray<IMemoryRecord<unknown>>): IMemoryRecord<unknown> | undefined;
|
|
33
|
+
/**
|
|
34
|
+
* Select the version of an entity valid at `asOf` (epoch ms): the newest
|
|
35
|
+
* (highest `seq`) version whose validity interval contains `asOf`. `undefined`
|
|
36
|
+
* when no version was valid at that instant.
|
|
37
|
+
* @public
|
|
38
|
+
*/
|
|
39
|
+
export declare function selectVersionAsOf(versions: ReadonlyArray<IMemoryRecord<unknown>>, asOf: number): IMemoryRecord<unknown> | undefined;
|
|
40
|
+
//# sourceMappingURL=temporal.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"temporal.d.ts","sourceRoot":"","sources":["../../../src/packlets/types/temporal.ts"],"names":[],"mappings":"AAKA,OAAO,EAAE,aAAa,EAAE,MAAM,YAAY,CAAC;AAE3C;;;;;;;GAOG;AACH,wBAAgB,gBAAgB,CAAC,MAAM,EAAE,aAAa,CAAC,OAAO,CAAC,GAAG,OAAO,CAExE;AAED;;;;;GAKG;AACH,wBAAgB,gBAAgB,CAAC,MAAM,EAAE,aAAa,CAAC,OAAO,CAAC,GAAG,OAAO,CAMxE;AAED;;;;;;GAMG;AACH,wBAAgB,gBAAgB,CAAC,MAAM,EAAE,aAAa,CAAC,OAAO,CAAC,EAAE,IAAI,EAAE,MAAM,GAAG,OAAO,CActF;AAkBD;;;;;GAKG;AACH,wBAAgB,oBAAoB,CAClC,QAAQ,EAAE,aAAa,CAAC,aAAa,CAAC,OAAO,CAAC,CAAC,GAC9C,aAAa,CAAC,OAAO,CAAC,GAAG,SAAS,CAEpC;AAED;;;;;GAKG;AACH,wBAAgB,iBAAiB,CAC/B,QAAQ,EAAE,aAAa,CAAC,aAAa,CAAC,OAAO,CAAC,CAAC,EAC/C,IAAI,EAAE,MAAM,GACX,aAAa,CAAC,OAAO,CAAC,GAAG,SAAS,CAEpC"}
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/*
|
|
3
|
+
* Copyright (c) 2026 Erik Fortune
|
|
4
|
+
* SPDX-License-Identifier: MIT
|
|
5
|
+
*/
|
|
6
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
|
+
exports.isTemporalRecord = isTemporalRecord;
|
|
8
|
+
exports.isVersionCurrent = isVersionCurrent;
|
|
9
|
+
exports.isVersionValidAt = isVersionValidAt;
|
|
10
|
+
exports.selectCurrentVersion = selectCurrentVersion;
|
|
11
|
+
exports.selectVersionAsOf = selectVersionAsOf;
|
|
12
|
+
/**
|
|
13
|
+
* Whether a record participates in the versioned (temporal) layout. A temporal
|
|
14
|
+
* record always carries a {@link ITemporalBlock | temporal} block (the store
|
|
15
|
+
* stamps `valid_at` on every versioned write); an atemporal record never does,
|
|
16
|
+
* so presence of `temporal` is the discriminator. Independent of `id`/`entityId`
|
|
17
|
+
* divergence (MTM is flat yet has `entityId !== id`).
|
|
18
|
+
* @public
|
|
19
|
+
*/
|
|
20
|
+
function isTemporalRecord(record) {
|
|
21
|
+
return record.envelope.temporal !== undefined;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Whether a temporal record is a *current* version — its `temporal.invalid_at`
|
|
25
|
+
* is `null` or absent (the still-valid sentinel). A non-temporal record is never
|
|
26
|
+
* current in this sense (returns `false`).
|
|
27
|
+
* @public
|
|
28
|
+
*/
|
|
29
|
+
function isVersionCurrent(record) {
|
|
30
|
+
const temporal = record.envelope.temporal;
|
|
31
|
+
if (temporal === undefined) {
|
|
32
|
+
return false;
|
|
33
|
+
}
|
|
34
|
+
return temporal.invalid_at === null || temporal.invalid_at === undefined;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Whether a temporal record's validity interval contains `asOf` (epoch ms):
|
|
38
|
+
* `valid_at <= asOf` and (`invalid_at` is null/absent OR `asOf < invalid_at`).
|
|
39
|
+
* The version's `valid_at` defaults to its `created` when absent; a non-temporal
|
|
40
|
+
* record is never "valid at" a point (returns `false`).
|
|
41
|
+
* @public
|
|
42
|
+
*/
|
|
43
|
+
function isVersionValidAt(record, asOf) {
|
|
44
|
+
var _a;
|
|
45
|
+
const temporal = record.envelope.temporal;
|
|
46
|
+
if (temporal === undefined) {
|
|
47
|
+
return false;
|
|
48
|
+
}
|
|
49
|
+
const start = (_a = temporal.valid_at) !== null && _a !== void 0 ? _a : record.envelope.created;
|
|
50
|
+
if (start > asOf) {
|
|
51
|
+
return false;
|
|
52
|
+
}
|
|
53
|
+
const end = temporal.invalid_at;
|
|
54
|
+
if (end === null || end === undefined) {
|
|
55
|
+
return true;
|
|
56
|
+
}
|
|
57
|
+
return asOf < end;
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* The version with the highest `seq` among `candidates` (undefined when empty).
|
|
61
|
+
* `seq` is the store's monotonic write counter, so highest `seq` is the newest
|
|
62
|
+
* version. Shared tiebreak for {@link selectCurrentVersion} /
|
|
63
|
+
* {@link selectVersionAsOf}.
|
|
64
|
+
*/
|
|
65
|
+
function highestSeq(candidates) {
|
|
66
|
+
let best;
|
|
67
|
+
for (const candidate of candidates) {
|
|
68
|
+
if (best === undefined || candidate.envelope.seq > best.envelope.seq) {
|
|
69
|
+
best = candidate;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
return best;
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Select the current version from a set of an entity's versions: the newest
|
|
76
|
+
* (highest `seq`) version whose `invalid_at` is null/absent. `undefined` when the
|
|
77
|
+
* entity has no current version (fully invalidated / soft-deleted, or empty).
|
|
78
|
+
* @public
|
|
79
|
+
*/
|
|
80
|
+
function selectCurrentVersion(versions) {
|
|
81
|
+
return highestSeq(versions.filter(isVersionCurrent));
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Select the version of an entity valid at `asOf` (epoch ms): the newest
|
|
85
|
+
* (highest `seq`) version whose validity interval contains `asOf`. `undefined`
|
|
86
|
+
* when no version was valid at that instant.
|
|
87
|
+
* @public
|
|
88
|
+
*/
|
|
89
|
+
function selectVersionAsOf(versions, asOf) {
|
|
90
|
+
return highestSeq(versions.filter((version) => isVersionValidAt(version, asOf)));
|
|
91
|
+
}
|
|
92
|
+
//# sourceMappingURL=temporal.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"temporal.js","sourceRoot":"","sources":["../../../src/packlets/types/temporal.ts"],"names":[],"mappings":";AAAA;;;GAGG;;AAYH,4CAEC;AAQD,4CAMC;AASD,4CAcC;AAwBD,oDAIC;AAQD,8CAKC;AAxFD;;;;;;;GAOG;AACH,SAAgB,gBAAgB,CAAC,MAA8B;IAC7D,OAAO,MAAM,CAAC,QAAQ,CAAC,QAAQ,KAAK,SAAS,CAAC;AAChD,CAAC;AAED;;;;;GAKG;AACH,SAAgB,gBAAgB,CAAC,MAA8B;IAC7D,MAAM,QAAQ,GAAmD,MAAM,CAAC,QAAQ,CAAC,QAAQ,CAAC;IAC1F,IAAI,QAAQ,KAAK,SAAS,EAAE,CAAC;QAC3B,OAAO,KAAK,CAAC;IACf,CAAC;IACD,OAAO,QAAQ,CAAC,UAAU,KAAK,IAAI,IAAI,QAAQ,CAAC,UAAU,KAAK,SAAS,CAAC;AAC3E,CAAC;AAED;;;;;;GAMG;AACH,SAAgB,gBAAgB,CAAC,MAA8B,EAAE,IAAY;;IAC3E,MAAM,QAAQ,GAAmD,MAAM,CAAC,QAAQ,CAAC,QAAQ,CAAC;IAC1F,IAAI,QAAQ,KAAK,SAAS,EAAE,CAAC;QAC3B,OAAO,KAAK,CAAC;IACf,CAAC;IACD,MAAM,KAAK,GAAW,MAAA,QAAQ,CAAC,QAAQ,mCAAI,MAAM,CAAC,QAAQ,CAAC,OAAO,CAAC;IACnE,IAAI,KAAK,GAAG,IAAI,EAAE,CAAC;QACjB,OAAO,KAAK,CAAC;IACf,CAAC;IACD,MAAM,GAAG,GAA8B,QAAQ,CAAC,UAAU,CAAC;IAC3D,IAAI,GAAG,KAAK,IAAI,IAAI,GAAG,KAAK,SAAS,EAAE,CAAC;QACtC,OAAO,IAAI,CAAC;IACd,CAAC;IACD,OAAO,IAAI,GAAG,GAAG,CAAC;AACpB,CAAC;AAED;;;;;GAKG;AACH,SAAS,UAAU,CAAC,UAAiD;IACnE,IAAI,IAAwC,CAAC;IAC7C,KAAK,MAAM,SAAS,IAAI,UAAU,EAAE,CAAC;QACnC,IAAI,IAAI,KAAK,SAAS,IAAI,SAAS,CAAC,QAAQ,CAAC,GAAG,GAAG,IAAI,CAAC,QAAQ,CAAC,GAAG,EAAE,CAAC;YACrE,IAAI,GAAG,SAAS,CAAC;QACnB,CAAC;IACH,CAAC;IACD,OAAO,IAAI,CAAC;AACd,CAAC;AAED;;;;;GAKG;AACH,SAAgB,oBAAoB,CAClC,QAA+C;IAE/C,OAAO,UAAU,CAAC,QAAQ,CAAC,MAAM,CAAC,gBAAgB,CAAC,CAAC,CAAC;AACvD,CAAC;AAED;;;;;GAKG;AACH,SAAgB,iBAAiB,CAC/B,QAA+C,EAC/C,IAAY;IAEZ,OAAO,UAAU,CAAC,QAAQ,CAAC,MAAM,CAAC,CAAC,OAAO,EAAE,EAAE,CAAC,gBAAgB,CAAC,OAAO,EAAE,IAAI,CAAC,CAAC,CAAC,CAAC;AACnF,CAAC","sourcesContent":["/*\n * Copyright (c) 2026 Erik Fortune\n * SPDX-License-Identifier: MIT\n */\n\nimport { IMemoryRecord } from './envelope';\n\n/**\n * Whether a record participates in the versioned (temporal) layout. A temporal\n * record always carries a {@link ITemporalBlock | temporal} block (the store\n * stamps `valid_at` on every versioned write); an atemporal record never does,\n * so presence of `temporal` is the discriminator. Independent of `id`/`entityId`\n * divergence (MTM is flat yet has `entityId !== id`).\n * @public\n */\nexport function isTemporalRecord(record: IMemoryRecord<unknown>): boolean {\n return record.envelope.temporal !== undefined;\n}\n\n/**\n * Whether a temporal record is a *current* version — its `temporal.invalid_at`\n * is `null` or absent (the still-valid sentinel). A non-temporal record is never\n * current in this sense (returns `false`).\n * @public\n */\nexport function isVersionCurrent(record: IMemoryRecord<unknown>): boolean {\n const temporal: IMemoryRecord<unknown>['envelope']['temporal'] = record.envelope.temporal;\n if (temporal === undefined) {\n return false;\n }\n return temporal.invalid_at === null || temporal.invalid_at === undefined;\n}\n\n/**\n * Whether a temporal record's validity interval contains `asOf` (epoch ms):\n * `valid_at <= asOf` and (`invalid_at` is null/absent OR `asOf < invalid_at`).\n * The version's `valid_at` defaults to its `created` when absent; a non-temporal\n * record is never \"valid at\" a point (returns `false`).\n * @public\n */\nexport function isVersionValidAt(record: IMemoryRecord<unknown>, asOf: number): boolean {\n const temporal: IMemoryRecord<unknown>['envelope']['temporal'] = record.envelope.temporal;\n if (temporal === undefined) {\n return false;\n }\n const start: number = temporal.valid_at ?? record.envelope.created;\n if (start > asOf) {\n return false;\n }\n const end: number | null | undefined = temporal.invalid_at;\n if (end === null || end === undefined) {\n return true;\n }\n return asOf < end;\n}\n\n/**\n * The version with the highest `seq` among `candidates` (undefined when empty).\n * `seq` is the store's monotonic write counter, so highest `seq` is the newest\n * version. Shared tiebreak for {@link selectCurrentVersion} /\n * {@link selectVersionAsOf}.\n */\nfunction highestSeq(candidates: ReadonlyArray<IMemoryRecord<unknown>>): IMemoryRecord<unknown> | undefined {\n let best: IMemoryRecord<unknown> | undefined;\n for (const candidate of candidates) {\n if (best === undefined || candidate.envelope.seq > best.envelope.seq) {\n best = candidate;\n }\n }\n return best;\n}\n\n/**\n * Select the current version from a set of an entity's versions: the newest\n * (highest `seq`) version whose `invalid_at` is null/absent. `undefined` when the\n * entity has no current version (fully invalidated / soft-deleted, or empty).\n * @public\n */\nexport function selectCurrentVersion(\n versions: ReadonlyArray<IMemoryRecord<unknown>>\n): IMemoryRecord<unknown> | undefined {\n return highestSeq(versions.filter(isVersionCurrent));\n}\n\n/**\n * Select the version of an entity valid at `asOf` (epoch ms): the newest\n * (highest `seq`) version whose validity interval contains `asOf`. `undefined`\n * when no version was valid at that instant.\n * @public\n */\nexport function selectVersionAsOf(\n versions: ReadonlyArray<IMemoryRecord<unknown>>,\n asOf: number\n): IMemoryRecord<unknown> | undefined {\n return highestSeq(versions.filter((version) => isVersionValidAt(version, asOf)));\n}\n"]}
|
|
@@ -210,4 +210,53 @@ export declare class MemoryCapCullPolicy implements IWritePolicy {
|
|
|
210
210
|
*/
|
|
211
211
|
private _rebuild;
|
|
212
212
|
}
|
|
213
|
+
/**
|
|
214
|
+
* Write policy for a versioned (temporal) kind family, implementing
|
|
215
|
+
* invalidate-don't-delete. Admission always accepts — history is retained, never
|
|
216
|
+
* culled — and updates apply the same RFC-7386 merge patch as
|
|
217
|
+
* {@link KnowledgeLwwPolicy}, restricted to the temporal mutable surface.
|
|
218
|
+
*
|
|
219
|
+
* @remarks
|
|
220
|
+
* The policy does NOT perform the version file writes or set `invalid_at` — that
|
|
221
|
+
* is the store's versioned write branch, driven by the kind's
|
|
222
|
+
* {@link ITemporalIdentityCodec}. The policy's role is limited to admission and
|
|
223
|
+
* the merge that forms the **new version's** content from the **current**
|
|
224
|
+
* version plus the incoming patch (the merge-patch-under-versioning contract).
|
|
225
|
+
*
|
|
226
|
+
* - **Dedup scope.** `'entity'` — an identical re-put of the current content is a
|
|
227
|
+
* no-op (the store compares the incoming content hash against the current
|
|
228
|
+
* version), so identical writes do not spawn redundant versions.
|
|
229
|
+
* - **Mutable surface.** `body` + the envelope metadata a consumer may revise
|
|
230
|
+
* (`tags` / `links` / `provenance` / `embeddingRef`). `temporal` is NOT mutable
|
|
231
|
+
* here — `valid_at` / `invalid_at` are set by the store's versioned branch.
|
|
232
|
+
* @public
|
|
233
|
+
*/
|
|
234
|
+
export declare class TemporalVersionedPolicy implements IWritePolicy {
|
|
235
|
+
/** The temporal mutable surface (mirrors {@link KnowledgeLwwPolicy}). */
|
|
236
|
+
readonly mutableFields: ReadonlyArray<string>;
|
|
237
|
+
/** Versioned kinds dedup per-entity against the current version (see the class remarks). */
|
|
238
|
+
readonly dedupScope: DedupScope;
|
|
239
|
+
/** Deep-clones the mutable view without RFC-7386 null-deletion semantics. */
|
|
240
|
+
private readonly _cloneEditor;
|
|
241
|
+
/** Applies the RFC-7386 merge patch. */
|
|
242
|
+
private readonly _mergeEditor;
|
|
243
|
+
private constructor();
|
|
244
|
+
/**
|
|
245
|
+
* Family-convention factory. Constructs the shared `JsonEditor` instances (one
|
|
246
|
+
* for cloning, one for the RFC-7386 merge), rules disabled — the same merge
|
|
247
|
+
* config as the shipped policies.
|
|
248
|
+
*/
|
|
249
|
+
static create(): Result<TemporalVersionedPolicy>;
|
|
250
|
+
/** {@inheritDoc IWritePolicy.admit} */
|
|
251
|
+
admit(__incoming: IMemoryRecord<unknown>, __existing: ReadonlyArray<IMemoryRecord<unknown>>): Result<AdmissionDecision>;
|
|
252
|
+
/** {@inheritDoc IWritePolicy.applyUpdate} */
|
|
253
|
+
applyUpdate(existing: IMemoryRecord<unknown>, patch: Record<string, unknown>): Result<IMemoryRecord<unknown>>;
|
|
254
|
+
/**
|
|
255
|
+
* Reassemble a record from the merged mutable view. `body` / `tags` / `links` /
|
|
256
|
+
* `provenance` are required and may not be deleted by a patch; `embeddingRef`,
|
|
257
|
+
* when dropped by the merge, is restored as `undefined` (absent) — the same
|
|
258
|
+
* hash-stable semantics as {@link KnowledgeLwwPolicy}.
|
|
259
|
+
*/
|
|
260
|
+
private _rebuild;
|
|
261
|
+
}
|
|
213
262
|
//# sourceMappingURL=writePolicy.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"writePolicy.d.ts","sourceRoot":"","sources":["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;;;GAoBG;AACH,qBAAa,uBAAwB,YAAW,YAAY;IAC1D,yEAAyE;IACzE,SAAgB,aAAa,EAAE,aAAa,CAAC,MAAM,CAAC,CAMlD;IAEF,4FAA4F;IAC5F,SAAgB,UAAU,EAAE,UAAU,CAAY;IAElD,6EAA6E;IAC7E,OAAO,CAAC,QAAQ,CAAC,YAAY,CAAa;IAC1C,wCAAwC;IACxC,OAAO,CAAC,QAAQ,CAAC,YAAY,CAAa;IAE1C,OAAO;IAKP;;;;OAIG;WACW,MAAM,IAAI,MAAM,CAAC,uBAAuB,CAAC;IAQvD,uCAAuC;IAChC,KAAK,CACV,UAAU,EAAE,aAAa,CAAC,OAAO,CAAC,EAClC,UAAU,EAAE,aAAa,CAAC,aAAa,CAAC,OAAO,CAAC,CAAC,GAChD,MAAM,CAAC,iBAAiB,CAAC;IAM5B,6CAA6C;IACtC,WAAW,CAChB,QAAQ,EAAE,aAAa,CAAC,OAAO,CAAC,EAChC,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAC7B,MAAM,CAAC,aAAa,CAAC,OAAO,CAAC,CAAC;IA6BjC;;;;;OAKG;IACH,OAAO,CAAC,QAAQ;CAmBjB"}
|
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
* SPDX-License-Identifier: MIT
|
|
5
5
|
*/
|
|
6
6
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
|
-
exports.MemoryCapCullPolicy = exports.KnowledgeLwwPolicy = exports.DEFAULT_DEDUP_SCOPE = void 0;
|
|
7
|
+
exports.TemporalVersionedPolicy = exports.MemoryCapCullPolicy = exports.KnowledgeLwwPolicy = exports.DEFAULT_DEDUP_SCOPE = void 0;
|
|
8
8
|
const ts_utils_1 = require("@fgv/ts-utils");
|
|
9
9
|
const ts_json_1 = require("@fgv/ts-json");
|
|
10
10
|
/**
|
|
@@ -265,4 +265,101 @@ class MemoryCapCullPolicy {
|
|
|
265
265
|
}
|
|
266
266
|
}
|
|
267
267
|
exports.MemoryCapCullPolicy = MemoryCapCullPolicy;
|
|
268
|
+
/**
|
|
269
|
+
* Write policy for a versioned (temporal) kind family, implementing
|
|
270
|
+
* invalidate-don't-delete. Admission always accepts — history is retained, never
|
|
271
|
+
* culled — and updates apply the same RFC-7386 merge patch as
|
|
272
|
+
* {@link KnowledgeLwwPolicy}, restricted to the temporal mutable surface.
|
|
273
|
+
*
|
|
274
|
+
* @remarks
|
|
275
|
+
* The policy does NOT perform the version file writes or set `invalid_at` — that
|
|
276
|
+
* is the store's versioned write branch, driven by the kind's
|
|
277
|
+
* {@link ITemporalIdentityCodec}. The policy's role is limited to admission and
|
|
278
|
+
* the merge that forms the **new version's** content from the **current**
|
|
279
|
+
* version plus the incoming patch (the merge-patch-under-versioning contract).
|
|
280
|
+
*
|
|
281
|
+
* - **Dedup scope.** `'entity'` — an identical re-put of the current content is a
|
|
282
|
+
* no-op (the store compares the incoming content hash against the current
|
|
283
|
+
* version), so identical writes do not spawn redundant versions.
|
|
284
|
+
* - **Mutable surface.** `body` + the envelope metadata a consumer may revise
|
|
285
|
+
* (`tags` / `links` / `provenance` / `embeddingRef`). `temporal` is NOT mutable
|
|
286
|
+
* here — `valid_at` / `invalid_at` are set by the store's versioned branch.
|
|
287
|
+
* @public
|
|
288
|
+
*/
|
|
289
|
+
class TemporalVersionedPolicy {
|
|
290
|
+
constructor(cloneEditor, mergeEditor) {
|
|
291
|
+
/** The temporal mutable surface (mirrors {@link KnowledgeLwwPolicy}). */
|
|
292
|
+
this.mutableFields = [
|
|
293
|
+
'body',
|
|
294
|
+
'tags',
|
|
295
|
+
'links',
|
|
296
|
+
'provenance',
|
|
297
|
+
'embeddingRef'
|
|
298
|
+
];
|
|
299
|
+
/** Versioned kinds dedup per-entity against the current version (see the class remarks). */
|
|
300
|
+
this.dedupScope = 'entity';
|
|
301
|
+
this._cloneEditor = cloneEditor;
|
|
302
|
+
this._mergeEditor = mergeEditor;
|
|
303
|
+
}
|
|
304
|
+
/**
|
|
305
|
+
* Family-convention factory. Constructs the shared `JsonEditor` instances (one
|
|
306
|
+
* for cloning, one for the RFC-7386 merge), rules disabled — the same merge
|
|
307
|
+
* config as the shipped policies.
|
|
308
|
+
*/
|
|
309
|
+
static create() {
|
|
310
|
+
return ts_json_1.JsonEditor.create({}, []).onSuccess((cloneEditor) => ts_json_1.JsonEditor.create(MERGE_PATCH_OPTIONS, []).onSuccess((mergeEditor) => (0, ts_utils_1.succeed)(new TemporalVersionedPolicy(cloneEditor, mergeEditor))));
|
|
311
|
+
}
|
|
312
|
+
/** {@inheritDoc IWritePolicy.admit} */
|
|
313
|
+
admit(__incoming, __existing) {
|
|
314
|
+
// Invalidate-don't-delete: always accept. Superseded versions are retained
|
|
315
|
+
// (invalidated), never culled.
|
|
316
|
+
return (0, ts_utils_1.succeed)({ decision: 'accept' });
|
|
317
|
+
}
|
|
318
|
+
/** {@inheritDoc IWritePolicy.applyUpdate} */
|
|
319
|
+
applyUpdate(existing, patch) {
|
|
320
|
+
// Project the mutable fields into a single record-level view, each sourced
|
|
321
|
+
// from its canonical location. `embeddingRef` is omitted when `undefined`.
|
|
322
|
+
const view = {
|
|
323
|
+
body: existing.body,
|
|
324
|
+
tags: existing.envelope.tags,
|
|
325
|
+
links: existing.envelope.links,
|
|
326
|
+
provenance: existing.envelope.provenance
|
|
327
|
+
};
|
|
328
|
+
if (existing.envelope.embeddingRef !== undefined) {
|
|
329
|
+
view.embeddingRef = existing.envelope.embeddingRef;
|
|
330
|
+
}
|
|
331
|
+
// Restrict the incoming patch to the declared mutable fields.
|
|
332
|
+
const scopedPatch = {};
|
|
333
|
+
for (const field of this.mutableFields) {
|
|
334
|
+
if (field in patch) {
|
|
335
|
+
scopedPatch[field] = patch[field];
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
// Clone the view (no null-deletion), then apply the RFC-7386 merge patch onto
|
|
339
|
+
// the clone so the current version is never mutated in place.
|
|
340
|
+
return this._cloneEditor
|
|
341
|
+
.mergeObjectInPlace({}, view)
|
|
342
|
+
.onSuccess((clone) => this._mergeEditor.mergeObjectInPlace(clone, scopedPatch))
|
|
343
|
+
.onSuccess((merged) => this._rebuild(existing, merged));
|
|
344
|
+
}
|
|
345
|
+
/**
|
|
346
|
+
* Reassemble a record from the merged mutable view. `body` / `tags` / `links` /
|
|
347
|
+
* `provenance` are required and may not be deleted by a patch; `embeddingRef`,
|
|
348
|
+
* when dropped by the merge, is restored as `undefined` (absent) — the same
|
|
349
|
+
* hash-stable semantics as {@link KnowledgeLwwPolicy}.
|
|
350
|
+
*/
|
|
351
|
+
_rebuild(existing, merged) {
|
|
352
|
+
const required = ['body', 'tags', 'links', 'provenance'];
|
|
353
|
+
const missing = required.filter((field) => !(field in merged));
|
|
354
|
+
if (missing.length > 0) {
|
|
355
|
+
return (0, ts_utils_1.fail)(`temporal versioned: merge patch may not delete required field(s): ${missing.join(', ')}`);
|
|
356
|
+
}
|
|
357
|
+
// The merged values are JSON projections of the already-validated typed
|
|
358
|
+
// record; restore the domain types (structural restorations, not fresh
|
|
359
|
+
// untrusted input — mirrors KnowledgeLwwPolicy._rebuild).
|
|
360
|
+
const envelope = Object.assign(Object.assign({}, existing.envelope), { tags: merged.tags, links: merged.links, provenance: merged.provenance, embeddingRef: 'embeddingRef' in merged ? merged.embeddingRef : undefined });
|
|
361
|
+
return (0, ts_utils_1.succeed)({ envelope, body: merged.body });
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
exports.TemporalVersionedPolicy = TemporalVersionedPolicy;
|
|
268
365
|
//# sourceMappingURL=writePolicy.js.map
|