dsh-engram 0.4.0 → 0.5.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +142 -17
- package/README.zh.md +110 -15
- package/docs/PROPOSAL-resultpack.zh.md +7 -3
- package/docs/RELEASE-NOTES-v0.5.0.md +58 -0
- package/docs/RELEASE-NOTES-v0.5.1.md +54 -0
- package/lib/api.js +19 -6
- package/lib/boundary-prune.js +115 -14
- package/lib/client.js +253 -3
- package/lib/index-block.js +27 -5
- package/lib/index.js +181 -5
- package/lib/pack-telemetry.js +115 -0
- package/lib/preference.js +276 -0
- package/lib/result-pack.js +276 -0
- package/lib/session-cjk.js +2 -2
- package/lib/settings.js +115 -31
- package/lib/store.js +1 -0
- package/lib/tools.js +2 -1
- package/lib/usage.js +47 -4
- package/package.json +8 -7
- package/scripts/prepublish-check.mjs +7 -4
package/lib/index-block.js
CHANGED
|
@@ -20,6 +20,7 @@ export function prefixFor(kind) {
|
|
|
20
20
|
case "insight": return "I";
|
|
21
21
|
case "handoff": return "H";
|
|
22
22
|
case "task": return "T";
|
|
23
|
+
case "preference": return "R";
|
|
23
24
|
default: return "F";
|
|
24
25
|
}
|
|
25
26
|
}
|
|
@@ -65,14 +66,24 @@ export function selectIndexMemories(store, workspace, config) {
|
|
|
65
66
|
m.kind === "task"),
|
|
66
67
|
)
|
|
67
68
|
.sort((a, b) => {
|
|
68
|
-
|
|
69
|
-
|
|
69
|
+
// User preferences are standing rules — rank them above everything
|
|
70
|
+
// (still newest-first within the group). Proven procedures follow.
|
|
71
|
+
const ap = kindRank(a, config) ? 0 : 1;
|
|
72
|
+
const bp = kindRank(b, config) ? 0 : 1;
|
|
70
73
|
if (ap !== bp) return ap - bp;
|
|
74
|
+
const ap2 = isProvenProcedure(a, config) ? 0 : 1;
|
|
75
|
+
const bp2 = isProvenProcedure(b, config) ? 0 : 1;
|
|
76
|
+
if (ap2 !== bp2) return ap2 - bp2;
|
|
71
77
|
return byRecency(a, b);
|
|
72
78
|
})
|
|
73
79
|
.slice(0, config.indexMaxLines);
|
|
74
80
|
}
|
|
75
81
|
|
|
82
|
+
/** Preference memories outrank everything in the [ENGRAM] index block. */
|
|
83
|
+
function kindRank(m, config) {
|
|
84
|
+
return (m?.kind ?? "") === "preference" && (m.signal ?? 0.5) >= (config?.minIndexSignal ?? 0.4);
|
|
85
|
+
}
|
|
86
|
+
|
|
76
87
|
function memoryLine(m, config) {
|
|
77
88
|
const px = prefixFor(m.kind);
|
|
78
89
|
const mark = isProvenProcedure(m, config) ? `${px}✓` : px;
|
|
@@ -199,14 +210,25 @@ export function renderStartRecall(store, workspace, config = {}, opts = {}) {
|
|
|
199
210
|
const lines = [];
|
|
200
211
|
const source = query === null ? "newest memory" : "recall";
|
|
201
212
|
lines.push(`[RECALL] ${source} · ${fresh.length} hit(s)${query ? ` · first msg: ${escapeLt(truncate(query, 40))}` : " · no user text yet"}`);
|
|
213
|
+
// UAP (read-path activation): when a hit's oneline is truncated, say so —
|
|
214
|
+
// the model should know the full record is one `engram_detail` call away
|
|
215
|
+
// instead of assuming the 140-char preview is the whole memory. The trailing
|
|
216
|
+
// guidance then points at a concrete truncated id (or the top hit).
|
|
217
|
+
let truncatedId = null;
|
|
202
218
|
for (const m of fresh) {
|
|
203
219
|
const px = prefixFor(m.kind);
|
|
204
220
|
const mark = isProvenProcedure(m, config) ? `${px}✓` : px;
|
|
205
221
|
const date = fmtDate(m.createdAt ?? m.updatedAt);
|
|
206
|
-
const
|
|
207
|
-
|
|
222
|
+
const full = String(m.text).replace(/\s*\n+\s*/g, " ").trim();
|
|
223
|
+
const truncated = [...full].length > 140;
|
|
224
|
+
const text = truncate(full, 140);
|
|
225
|
+
if (truncated && truncatedId === null) truncatedId = shortId(m.id);
|
|
226
|
+
lines.push(`- [${mark}] ${date} ${escapeLt(text)} #${shortId(m.id)}${truncated ? " …" : ""}${(m.hits ?? 0) > 0 ? ` ×${m.hits}` : ""}`);
|
|
208
227
|
}
|
|
209
|
-
|
|
228
|
+
const drill = truncatedId !== null
|
|
229
|
+
? `context: use above · #${truncatedId} truncated — engram_detail ${truncatedId} (full) · engram_recall <query>`
|
|
230
|
+
: `context: use above · engram_detail <id> · engram_recall <query>`;
|
|
231
|
+
lines.push(drill);
|
|
210
232
|
return enforceStartRecallCap(lines, maxChars);
|
|
211
233
|
}
|
|
212
234
|
|
package/lib/index.js
CHANGED
|
@@ -23,11 +23,14 @@ import { openEngramDomain } from "./store.js";
|
|
|
23
23
|
import { registerTools } from "./tools.js";
|
|
24
24
|
import { makeCaptureHandler } from "./capture.js";
|
|
25
25
|
import { installGoalCapture } from "./goal-capture.js";
|
|
26
|
+
import { installPreferenceCapture } from "./preference.js";
|
|
26
27
|
import { installBoundaryPrune } from "./boundary-prune.js";
|
|
28
|
+
import { resultPackDir, archivePrunedResults, resultRecallTool, sweepExpired } from "./result-pack.js";
|
|
29
|
+
import { recordBoundaryPruneFire, recordRecallInjection, readPackTelemetry, engramHome } from "./pack-telemetry.js";
|
|
27
30
|
import { renderIndex, renderStartRecall, firstUserMessageText, renderEsr, esrMethodology, buildEsrSnapshot, buildFrozenEsrBlock } from "./index-block.js";
|
|
28
31
|
import { makeTriggerRecorder } from "./trigger.js";
|
|
29
32
|
import { pendingTodosFromSession, sinkPendingTodos } from "./todo-sink.js";
|
|
30
|
-
import { installEngramSettings } from "./settings.js";
|
|
33
|
+
import { installEngramSettings, makeEngineConfigSchema, SETTINGS_KEYS } from "./settings.js";
|
|
31
34
|
import { makeEngramRoutes } from "./api.js";
|
|
32
35
|
import { collectPointerContext, mountCompactionEngine } from "./context-gc.js";
|
|
33
36
|
import { provisionWebCompaction } from "./web-provision.js";
|
|
@@ -68,6 +71,11 @@ const DEFAULTS = {
|
|
|
68
71
|
autoRecallOnStart: true,
|
|
69
72
|
autoRecallLimit: 3,
|
|
70
73
|
autoRecallMaxChars: 700,
|
|
74
|
+
// user-correction / preference learning (zero-LLM, auto-captured from
|
|
75
|
+
// user/message events; prefer/avoid/switch-to cues). Store as `preference`
|
|
76
|
+
// memories that outrank the [ENGRAM] index.
|
|
77
|
+
autoCapturePreferences: true,
|
|
78
|
+
preferenceCapturePerSession: 8,
|
|
71
79
|
// [ENGRAM] index budget (token discipline)
|
|
72
80
|
indexMaxLines: 12,
|
|
73
81
|
indexMaxChars: 700,
|
|
@@ -144,23 +152,110 @@ const DEFAULTS = {
|
|
|
144
152
|
// least this many more requests (empirical workspace prior, see
|
|
145
153
|
// lib/boundary-prune.js). 50 = L0 sweep optimum.
|
|
146
154
|
boundaryPruneMinRemaining: 50,
|
|
155
|
+
// Threshold override (ResultPack L0 "T2048 landing"): the host pruner is
|
|
156
|
+
// fixed at 8192; set this to a lower budget to prune earlier at boundaries
|
|
157
|
+
// (L0 says T2048 has the highest net savings +61.6Mc, gated only by the
|
|
158
|
+
// L2 archive requirement — now satisfied). 0/absent → host threshold.
|
|
159
|
+
boundaryPruneThresholdChars: 0,
|
|
160
|
+
// Edges retained by the override pruner (unless set, host's 4096/1024).
|
|
161
|
+
boundaryPruneHeadChars: 0,
|
|
162
|
+
boundaryPruneTailChars: 0,
|
|
163
|
+
// Request-count boundary (SoL-Pi "step idle", default OFF): fire at most once
|
|
164
|
+
// per N requests even in sessions with no todo/goal boundary (~62% of the L0
|
|
165
|
+
// corpus never fires the event boundaries). Still under the repayment gate.
|
|
166
|
+
// 0 → event boundaries only.
|
|
167
|
+
boundaryPruneIdleCadence: 0,
|
|
168
|
+
// ResultPack L2 (PROPOSAL-resultpack §L2): archive the middles L1 prunes
|
|
169
|
+
// off the surface (~/.dsh/engram/resultpack/<callId>.json, redacted) and
|
|
170
|
+
// append a `[middle archived: result_recall(<callId>) 可取]` handle tail to
|
|
171
|
+
// each pruned node; register `result_recall(callId, offset?)` for paged
|
|
172
|
+
// recall — the full ObservationPack pattern (SoL-Pi mech ③; paired
|
|
173
|
+
// EdgeBench −23.58%). Requires `boundaryPrune: true` to have anything to
|
|
174
|
+
// archive (L2 without L1 is a no-op with a log line). Default OFF (C13).
|
|
175
|
+
resultPack: false,
|
|
176
|
+
// result_recall page budget (chars per page). 4000 fits a typical model
|
|
177
|
+
// context band without re-materializing huge results whole.
|
|
178
|
+
resultPackPageChars: 4000,
|
|
179
|
+
// Retain archived middles this many days (mtime sweep, same GC cadence).
|
|
180
|
+
resultPackRetentionDays: 7,
|
|
147
181
|
};
|
|
148
182
|
|
|
183
|
+
/**
|
|
184
|
+
* Cordis reads `plugin.Config` off the module: DSH ≥0.1.7 validates the
|
|
185
|
+
* `engram` profile row against it AND projects the settings form from it —
|
|
186
|
+
* the curated SETTINGS_KEYS are `.volatile()`, so the settings service exposes
|
|
187
|
+
* exactly the GUI card's knobs (entry id `engram`), while every other DEFAULTS
|
|
188
|
+
* key stays a plain declared field so profile-patch config survives validation.
|
|
189
|
+
*/
|
|
190
|
+
export const Config = makeEngineConfigSchema(DEFAULTS);
|
|
191
|
+
|
|
149
192
|
export function resolveConfig(config) {
|
|
150
193
|
const src = config && typeof config === "object" ? config : {};
|
|
151
194
|
const out = { ...DEFAULTS };
|
|
152
195
|
for (const key of Object.keys(DEFAULTS)) {
|
|
153
|
-
if (src[key] !== void 0)
|
|
196
|
+
if (src[key] !== void 0) {
|
|
197
|
+
// A volatile reference may hold a nullish value (e.g. a profile patch
|
|
198
|
+
// listed a knob as null, or schemastery's null-default sentinel); the
|
|
199
|
+
// `??` keeps the DEFAULTS value in that case instead of nulling out.
|
|
200
|
+
out[key] = unwrapValue(src[key]) ?? DEFAULTS[key];
|
|
201
|
+
}
|
|
154
202
|
}
|
|
155
203
|
return out;
|
|
156
204
|
}
|
|
157
205
|
|
|
206
|
+
/** Unwrap a volatile config reference (DSH ≥0.1.7 settings) to its plain value. */
|
|
207
|
+
function unwrapValue(value) {
|
|
208
|
+
return value !== null && typeof value === "object" && typeof value.get === "function" ? value.get() : value;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
/**
|
|
212
|
+
* Collect the volatile config references for the given keys from a plugin
|
|
213
|
+
* config object (DSH ≥0.1.7 validates `Config` into volatile references that
|
|
214
|
+
* the loader commits into IN PLACE on settings-card edits). Plain values are
|
|
215
|
+
* skipped, so this is a no-op on legacy DSH.
|
|
216
|
+
*/
|
|
217
|
+
export function collectVolatileRefs(config, keys) {
|
|
218
|
+
const refs = {};
|
|
219
|
+
if (config !== null && typeof config === "object") {
|
|
220
|
+
for (const key of keys) {
|
|
221
|
+
const ref = config[key];
|
|
222
|
+
if (ref !== null && typeof ref === "object" && typeof ref.get === "function") refs[key] = ref;
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
return refs;
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
/**
|
|
229
|
+
* Re-read volatile references into a resolved config (`target`, mutated in
|
|
230
|
+
* place): the loader has committed a settings-card edit into the same refs and
|
|
231
|
+
* emitted `loader/volatile-update`. Nullish refs fall back to DEFAULTS (the
|
|
232
|
+
* schemastery null-default sentinel yields an undefined ref value). Returns
|
|
233
|
+
* the keys whose value actually changed.
|
|
234
|
+
*/
|
|
235
|
+
export function syncVolatileRefs(refs, target) {
|
|
236
|
+
const changed = [];
|
|
237
|
+
for (const key of Object.keys(refs)) {
|
|
238
|
+
const next = unwrapValue(refs[key]) ?? DEFAULTS[key];
|
|
239
|
+
if (next !== target[key]) {
|
|
240
|
+
target[key] = next;
|
|
241
|
+
changed.push(key);
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
return changed;
|
|
245
|
+
}
|
|
246
|
+
|
|
158
247
|
/**
|
|
159
248
|
* Mount the plugin. `ctx` is the scoped cordis context; `config` the optional
|
|
160
249
|
* resolved plugin config (plain object from the profile patch).
|
|
161
250
|
*/
|
|
162
251
|
export function apply(ctx, config) {
|
|
163
252
|
const resolved = resolveConfig(config);
|
|
253
|
+
// DSH ≥0.1.7 / 0.2.x settings model: the GUI card edits the `engram`
|
|
254
|
+
// entry's VOLATILE Config fields. The loader commits those into the same
|
|
255
|
+
// references (in place) and emits `loader/volatile-update` — hold the refs
|
|
256
|
+
// so a card edit re-syncs `resolved` live for new sessions (blocks already
|
|
257
|
+
// frozen for a session stay frozen — the plugin's documented contract).
|
|
258
|
+
const volatileRefs = collectVolatileRefs(config, SETTINGS_KEYS);
|
|
164
259
|
if (!resolved.enabled) return;
|
|
165
260
|
|
|
166
261
|
const log = ctx.logger ? ctx.logger("engram") : undefined;
|
|
@@ -220,7 +315,10 @@ export function apply(ctx, config) {
|
|
|
220
315
|
sessionQuery,
|
|
221
316
|
log,
|
|
222
317
|
/** Cumulative auto-capture totals (GUI overview observability). */
|
|
223
|
-
captureStats: { total: 0, git: 0, file: 0, error: 0 },
|
|
318
|
+
captureStats: { total: 0, git: 0, file: 0, error: 0, preference: 0 },
|
|
319
|
+
/** SoL-Pi activation telemetry (M15/C13): durable boundary-prune fires +
|
|
320
|
+
* [RECALL] injection funnel, read at request time (readPackTelemetry). */
|
|
321
|
+
packTelemetry: () => readPackTelemetry({ home: engramHome() }),
|
|
224
322
|
/** Last GC sweep totals (GUI overview observability). */
|
|
225
323
|
gcStats: { lastRun: 0, archivedMemories: 0, archivedTasks: 0, removedLinks: 0 },
|
|
226
324
|
/** Live Context GC status (API overview + durable status file). */
|
|
@@ -264,6 +362,11 @@ export function apply(ctx, config) {
|
|
|
264
362
|
if (report.archivedMemories.length + report.archivedTasks.length + report.removedLinks.length > 0) {
|
|
265
363
|
log?.info?.(`engram gc: ${report.archivedMemories.length} memories archived, ${report.archivedTasks.length} tasks, ${report.removedLinks.length} links removed`);
|
|
266
364
|
}
|
|
365
|
+
// ResultPack L2 archive sweep rides the same GC cadence (mtime-based).
|
|
366
|
+
if (resolved.resultPack === true) {
|
|
367
|
+
const removed = sweepExpired(resultPackDir(), resolved.resultPackRetentionDays ?? 7);
|
|
368
|
+
if (removed > 0) log?.info?.(`engram gc: ${removed} expired result-pack archives removed`);
|
|
369
|
+
}
|
|
267
370
|
} catch (error) {
|
|
268
371
|
log?.warn?.(`engram gc sweep failed: ${String(error)}`);
|
|
269
372
|
}
|
|
@@ -327,7 +430,18 @@ export function apply(ctx, config) {
|
|
|
327
430
|
pendingRecall = true;
|
|
328
431
|
} else {
|
|
329
432
|
const recall = renderStartRecall(opened, workspace, resolved, { query, allowRecencyFallback: false });
|
|
330
|
-
|
|
433
|
+
if (recall.length > 0) {
|
|
434
|
+
// Activation funnel, injection leg: a [RECALL] block actually made
|
|
435
|
+
// it into a session. Counted here (the block only materializes when
|
|
436
|
+
// the render produced hits), so /stats can close the loop with
|
|
437
|
+
// engram_recall/engram_detail tool calls from the session logs.
|
|
438
|
+
recordRecallInjection({
|
|
439
|
+
home: engramHome(),
|
|
440
|
+
workspace,
|
|
441
|
+
hits: (recall.match(/- \[/g) ?? []).length,
|
|
442
|
+
});
|
|
443
|
+
block = `${block}\n${recall}`.trim();
|
|
444
|
+
}
|
|
331
445
|
}
|
|
332
446
|
}
|
|
333
447
|
} catch (error) {
|
|
@@ -392,6 +506,17 @@ export function apply(ctx, config) {
|
|
|
392
506
|
ctx.effect(() => {
|
|
393
507
|
const disposers = [];
|
|
394
508
|
|
|
509
|
+
// Settings card edits (volatile Config fields) hot-commit into the refs
|
|
510
|
+
// in place; re-read them so `resolved` reflects new values for new
|
|
511
|
+
// sessions without a profile restart. Legacy DSH never emits this event
|
|
512
|
+
// and leaves `volatileRefs` empty, so the listener is inert there.
|
|
513
|
+
if (Object.keys(volatileRefs).length > 0) {
|
|
514
|
+
const disposeVolatile = ctx.on?.("loader/volatile-update", () => {
|
|
515
|
+
syncVolatileRefs(volatileRefs, resolved);
|
|
516
|
+
});
|
|
517
|
+
if (disposeVolatile !== void 0) disposers.push(disposeVolatile);
|
|
518
|
+
}
|
|
519
|
+
|
|
395
520
|
// tools
|
|
396
521
|
disposers.push(registerTools(ctx, service));
|
|
397
522
|
|
|
@@ -622,11 +747,62 @@ export function apply(ctx, config) {
|
|
|
622
747
|
const disposeGoal = installGoalCapture(ctx, captureStore, log);
|
|
623
748
|
disposers.push(disposeGoal);
|
|
624
749
|
|
|
750
|
+
// User-correction / preference learning (zero-LLM): `user/message`
|
|
751
|
+
// events carrying explicit prefer/avoid/switch-to cues auto-sediment as
|
|
752
|
+
// `preference` memories (correx / PAHF / Mem0 preference-extraction
|
|
753
|
+
// pattern, distilled to pure rules). No model call.
|
|
754
|
+
const disposePreference = installPreferenceCapture(ctx, captureStore, resolved, log, service.captureStats);
|
|
755
|
+
disposers.push(disposePreference);
|
|
756
|
+
|
|
625
757
|
// BoundaryPrune (PROPOSAL-resultpack L1, default OFF): prune
|
|
626
758
|
// over-threshold tool results at todo/goal subtask boundaries through
|
|
627
759
|
// DSH's own deterministic toolResultPruner, gated by a repayment clock.
|
|
628
|
-
const
|
|
760
|
+
const l2Dir = resultPackDir();
|
|
761
|
+
const disposeBoundaryPrune = installBoundaryPrune(ctx, resolved, log, {
|
|
762
|
+
// ResultPack L2 hook: archive the pruned middles (redacted) + append
|
|
763
|
+
// a handle tail on each pruned node. Fail-contained in result-pack.js.
|
|
764
|
+
// Also records durable activation telemetry (SoL-Pi M15/C13) —
|
|
765
|
+
// independent of `resultPack`: /stats must be able to prove the L1
|
|
766
|
+
// mechanism actually fires on the live profile.
|
|
767
|
+
onPruned: (session, report, boundary) => {
|
|
768
|
+
recordBoundaryPruneFire({
|
|
769
|
+
home: engramHome(),
|
|
770
|
+
boundary,
|
|
771
|
+
report,
|
|
772
|
+
workspace: wk(session?.header?.cwd),
|
|
773
|
+
});
|
|
774
|
+
if (resolved.resultPack !== true) return;
|
|
775
|
+
const workspace = wk(session?.header?.cwd);
|
|
776
|
+
archivePrunedResults({
|
|
777
|
+
dir: l2Dir,
|
|
778
|
+
session,
|
|
779
|
+
report,
|
|
780
|
+
workspace,
|
|
781
|
+
log,
|
|
782
|
+
});
|
|
783
|
+
},
|
|
784
|
+
});
|
|
629
785
|
disposers.push(disposeBoundaryPrune);
|
|
786
|
+
|
|
787
|
+
// ResultPack L2 tool (default OFF with L1): `result_recall(callId, offset?)`
|
|
788
|
+
// pages back an archived middle. Independent of the memory domain — the
|
|
789
|
+
// archive lives on the filesystem. Registers only when enabled; without
|
|
790
|
+
// boundayPrune there is nothing to archive, so it self-disables.
|
|
791
|
+
if (resolved.resultPack === true) {
|
|
792
|
+
if (resolved.boundaryPrune !== true) {
|
|
793
|
+
log?.warn?.("engram result-pack: requires boundaryPrune: true to prune middles — result_recall not registered");
|
|
794
|
+
} else {
|
|
795
|
+
const pageChars = Number.isFinite(resolved.resultPackPageChars)
|
|
796
|
+
? resolved.resultPackPageChars
|
|
797
|
+
: 4000;
|
|
798
|
+
disposers.push(
|
|
799
|
+
ctx.effect(() => {
|
|
800
|
+
ctx.tools.register(resultRecallTool(l2Dir, pageChars));
|
|
801
|
+
log?.info?.(`engram result-pack enabled: result_recall(callId, offset) registered; archive dir ${l2Dir}`);
|
|
802
|
+
}),
|
|
803
|
+
);
|
|
804
|
+
}
|
|
805
|
+
}
|
|
630
806
|
}
|
|
631
807
|
|
|
632
808
|
// memory GC scheduling: one sweep after the store is reachable, then on
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Activation telemetry for the SoL-Pi mechanisms (dsh-engram).
|
|
3
|
+
*
|
|
4
|
+
* SoL-Pi methodology pair rule: M15 "mechanisms must prove activation" and
|
|
5
|
+
* C13 "dormant mechanisms are off by default" — a mechanism that cannot be
|
|
6
|
+
* observed activating is treated as dormant. This module gives BoundaryPrune
|
|
7
|
+
* (L1) + ResultPack (L2) an observable, durable activation record:
|
|
8
|
+
*
|
|
9
|
+
* - `recordBoundaryPruneFire` is called on every real prune (fires > 0) —
|
|
10
|
+
* durable JSON under the engram home, cumulative across restarts, so
|
|
11
|
+
* /stats and the GUI overview can prove the mechanism actually fires on
|
|
12
|
+
* the live profile (not just in the simulation).
|
|
13
|
+
* - `readBoundaryPruneStats` / `readRecallInjections` are pure readers.
|
|
14
|
+
*
|
|
15
|
+
* Zero LLM, pure deterministic bookkeeping. Two files today:
|
|
16
|
+
* ~/.dsh/engram/boundary-prune.status.json
|
|
17
|
+
* ~/.dsh/engram/recall-injections.json
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
import fs from "node:fs";
|
|
21
|
+
import os from "node:os";
|
|
22
|
+
import path from "node:path";
|
|
23
|
+
|
|
24
|
+
/** Default engram home (mirrors result-pack.js). */
|
|
25
|
+
export function engramHome(home = path.join(os.homedir(), ".dsh", "engram")) {
|
|
26
|
+
return home;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function statusFile(home, name) {
|
|
30
|
+
return path.join(home, name);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** Atomic-ish write: tmp + rename, so a crash never leaves a torn file. */
|
|
34
|
+
function writeJsonAtomic(file, payload) {
|
|
35
|
+
try {
|
|
36
|
+
fs.mkdirSync(path.dirname(file), { recursive: true });
|
|
37
|
+
const tmp = `${file}.${process.pid}.tmp`;
|
|
38
|
+
fs.writeFileSync(tmp, `${JSON.stringify(payload)}\n`, "utf8");
|
|
39
|
+
fs.renameSync(tmp, file);
|
|
40
|
+
} catch {
|
|
41
|
+
// best-effort telemetry — never throw into the prune pipeline
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function readJson(file) {
|
|
46
|
+
try {
|
|
47
|
+
return JSON.parse(fs.readFileSync(file, "utf8"));
|
|
48
|
+
} catch {
|
|
49
|
+
return null;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Record one BoundaryPrune fire (a real prune happened) into durable state.
|
|
55
|
+
* `report` is the pruner's pruneSession result. Returns the new cumulative
|
|
56
|
+
* stats object (or null when writing failed).
|
|
57
|
+
*/
|
|
58
|
+
export function recordBoundaryPruneFire({ home, boundary, report, workspace, now = Date.now() } = {}) {
|
|
59
|
+
const prev = readBoundaryPruneStats({ home }) ?? {};
|
|
60
|
+
const pruned = Array.isArray(report?.pruned) ? report.pruned.length : 0;
|
|
61
|
+
const chars = Number.isFinite(report?.charsRemoved) ? report.charsRemoved : 0;
|
|
62
|
+
const kind = boundary?.kind ?? "unknown";
|
|
63
|
+
const byKind = { ...(prev.byKind ?? {}) };
|
|
64
|
+
byKind[kind] = (byKind[kind] ?? 0) + 1;
|
|
65
|
+
const next = {
|
|
66
|
+
version: 1,
|
|
67
|
+
fires: (prev.fires ?? 0) + 1,
|
|
68
|
+
prunedResults: (prev.prunedResults ?? 0) + pruned,
|
|
69
|
+
charsRemoved: (prev.charsRemoved ?? 0) + chars,
|
|
70
|
+
uses: (prev.uses ?? 0) + 1, // legacy alias kept for GUI tolerance
|
|
71
|
+
lastFire: now,
|
|
72
|
+
lastBoundary: kind,
|
|
73
|
+
lastWorkspace: workspace ?? prev.lastWorkspace ?? null,
|
|
74
|
+
byKind,
|
|
75
|
+
};
|
|
76
|
+
writeJsonAtomic(statusFile(home, "boundary-prune.status.json"), next);
|
|
77
|
+
return next;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/** Read the cumulative BoundaryPrune activation record (never throws). */
|
|
81
|
+
export function readBoundaryPruneStats({ home } = {}) {
|
|
82
|
+
return readJson(statusFile(home, "boundary-prune.status.json"));
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Record one [RECALL] injection (a session-start recall block was actually
|
|
87
|
+
* appended to a session) for the activation funnel: injections → tool
|
|
88
|
+
* queries → hits → detail-follows.
|
|
89
|
+
*/
|
|
90
|
+
export function recordRecallInjection({ home, workspace, hits, now = Date.now() } = {}) {
|
|
91
|
+
const prev = readRecallInjections({ home }) ?? { version: 1, injections: 0, hitInjections: 0 };
|
|
92
|
+
const next = {
|
|
93
|
+
version: 1,
|
|
94
|
+
injections: (prev.injections ?? 0) + 1,
|
|
95
|
+
hitInjections: (prev.hitInjections ?? 0) + (hits > 0 ? 1 : 0),
|
|
96
|
+
hitsInjected: (prev.hitsInjected ?? 0) + (hits > 0 ? hits : 0),
|
|
97
|
+
lastInjection: now,
|
|
98
|
+
lastWorkspace: workspace ?? prev.lastWorkspace ?? null,
|
|
99
|
+
};
|
|
100
|
+
writeJsonAtomic(statusFile(home, "recall-injections.json"), next);
|
|
101
|
+
return next;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/** Read the cumulative [RECALL] injection record (never throws). */
|
|
105
|
+
export function readRecallInjections({ home } = {}) {
|
|
106
|
+
return readJson(statusFile(home, "recall-injections.json"));
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/** Aggregate both telemetry files into one summary for /stats + overview. */
|
|
110
|
+
export function readPackTelemetry({ home } = {}) {
|
|
111
|
+
return {
|
|
112
|
+
boundaryPrune: readBoundaryPruneStats({ home }),
|
|
113
|
+
recall: readRecallInjections({ home }),
|
|
114
|
+
};
|
|
115
|
+
}
|