dsh-layers-hippocampus 1.9.4 → 1.9.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -4,6 +4,27 @@ All notable changes to `dsh-layers-hippocampus` are documented here.
4
4
  The format follows [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
5
5
  and the project adheres to [Semantic Versioning](https://semver.org/).
6
6
 
7
+ ## [1.9.5] — 2026-11-30
8
+
9
+ **恢复「到点真的缩小上下文」,但只允许缩一次。**
10
+ 用户的原话是:以前是**直接缩小上下文**的,甚至反复缩小。查证结果:v1.6.4 及更早每轮都委托宿主
11
+ `toolResultPruner`,被 v1.6.5(`66a3b93`)关掉,代码里至今留着当时的原话记录——
12
+ `"context drops from 10% to 7% over and over"`。而 1.9.4 又把折叠推迟到 85% 并禁止它,
13
+ 于是 60%~85% 之间什么都不缩。这两个极端都不要。
14
+
15
+ - **Added — `context.smartPrune.onceFoldAt`(默认 `0.65`,`0` 关闭)**。一个会话**只折叠一次**:
16
+ 占用到 65% 时执行一次 `smartPrune`,随后**立即强制交接**,把变小的那面交出去。
17
+ 之后本会话不再折叠,所以「反复缩小」不会重演。
18
+ - **Added — 折叠后交接**。`checkHandoff({force:true})`,结果记在 `runtime.lastOnceFoldHandoff`
19
+ (`ratio` / `onceFoldAt` / `folded` / `handoffActive` / `degraded`)。
20
+ - **Fixed — 守卫用错了测量**。1.9.4 的折叠门在**探针刷新之前**取 `hostProbes`,进程首个请求比率是 0,
21
+ 于是硬停与一次性折叠都可能根本不触发。现在门与一次性判定都基于**本请求刷新后的 `state`**。
22
+ - **Fixed — 被拒绝的折叠不得消耗一次性机会**。85% 硬停时折叠被拒,但一次性额度若在那一刻被「消费」,
23
+ 会话就永久失去它唯一一次变小的机会。现在额度只在 `foldGate.allowed` 为真时才会花掉。
24
+ - **Tests — 7 个新用例(`test/v195.oncefold.test.js`)**:`onceFoldAt` 默认值与 fail-fast;
25
+ 66% 折叠一次并立刻交接;同一会话第二次/第三次请求**不再改写表面**;
26
+ 50% 以下既不折叠也不交接;`onceFoldAt:0` 恢复 1.9.4 行为;
27
+ 86% 硬停时折叠被拒;**硬停不消耗一次性额度**(这条是回归测试,实现里真的漏过一次)。
7
28
  ## [1.9.4] — 2026-11-30
8
29
 
9
30
  **修「卸载外部压缩插件后 Handoff 再也不触发」,并实装短块聚合与极简状态。**
package/README.md CHANGED
@@ -7,7 +7,7 @@
7
7
 
8
8
  | | |
9
9
  | --- | --- |
10
- | 版本 | **1.9.4** |
10
+ | 版本 | **1.9.5** |
11
11
  | 包名 | `dsh-layers-hippocampus` |
12
12
  | 仓库 | <https://github.com/382quaii/dsh-Layers-Hippocampus> |
13
13
  | 许可证 | MIT |
@@ -296,7 +296,7 @@ clear_dropbox('D:/mem/sessions/abc');
296
296
  pnpm add dsh-layers-hippocampus
297
297
 
298
298
  # 或从 GitHub 指定 tag
299
- pnpm add "github:382quaii/dsh-Layers-Hippocampus#v1.9.4"
299
+ pnpm add "github:382quaii/dsh-Layers-Hippocampus#v1.9.5"
300
300
  ```
301
301
 
302
302
  ### 2. 挂到 profile
package/index.js CHANGED
@@ -80,7 +80,7 @@ export { resolveConfig, DEFAULT_CONFIG };
80
80
  * of letting a raw 404 reach the user.
81
81
  */
82
82
  export const CAPABILITIES = Object.freeze({
83
- version: '1.9.4',
83
+ version: '1.9.5',
84
84
  spaces: true,
85
85
  memories: true,
86
86
  transferConsent: true,
@@ -1258,44 +1258,6 @@ export function createRuntime({ ctx = null, config, options, clock, raw = null,
1258
1258
  // Our pointerization keeps head/tail plus a `deep_raw` recoverable pointer and
1259
1259
  // only fires on a genuinely oversized single result, so a healthy conversation
1260
1260
  // is never rewritten.
1261
- // ---- 0. the folding stop line (v1.9.4) --------------------------------
1262
- // Folding is fake relief: it shrinks the PROMPT without reducing what was said,
1263
- // so a conversation that folds on every request can reset pressure just before
1264
- // the handoff trigger and never hand over. At the hard stop folding is refused
1265
- // outright and the handoff is forced — enforced BEFORE any folder runs, because
1266
- // those folders are what would otherwise keep the ratio down.
1267
- const prePressure = memory.context.nativeState({
1268
- usedTokens: hostProbes.usedTokens ?? undefined,
1269
- reservedTokens: hostProbes.reservedTokens ?? undefined,
1270
- });
1271
- const foldGate = foldBlocked({ projectedRatio: prePressure.projectedRatio });
1272
- runtime.lastFoldGate = { ...foldGate, ratio: prePressure.projectedRatio, at: memory.clock.now() };
1273
- if (!foldGate.allowed) {
1274
- runtime.logger?.warn?.(
1275
- `hippocampus: [Handoff Guard] 投影占用 ${(prePressure.projectedRatio * 100).toFixed(1)}% >= ` +
1276
- `折叠硬阈值 ${(FOLD_HARD_STOP_RATIO * 100).toFixed(0)}%:本次请求禁止折叠,强制触发交接。`,
1277
- );
1278
- }
1279
-
1280
- const liveConfig = runtime.config?.context?.liveCompact ?? {};
1281
- if (liveConfig.enabled !== false && session && foldGate.allowed) {
1282
- try {
1283
- runtime.lastLiveCompact = {
1284
- source: 'hippocampus',
1285
- at: memory.clock.now(),
1286
- ...liveCompactSession(session, {
1287
- thresholdChars: liveConfig.thresholdChars ?? LIVE_COMPACT_DEFAULTS.thresholdChars,
1288
- headChars: liveConfig.headChars ?? LIVE_COMPACT_DEFAULTS.headChars,
1289
- tailChars: liveConfig.tailChars ?? LIVE_COMPACT_DEFAULTS.tailChars,
1290
- maxNodesPerRequest: liveConfig.maxNodesPerRequest ?? LIVE_COMPACT_DEFAULTS.maxNodesPerRequest,
1291
- logger: runtime.logger,
1292
- }),
1293
- };
1294
- } catch (error) {
1295
- runtime.logger?.warn?.(`hippocampus: live compact failed: ${asMemoryError(error).code}`);
1296
- }
1297
- }
1298
-
1299
1261
  // ---- 2. refresh the probes --------------------------------------------
1300
1262
  const provider = requestConfig?.provider ?? null;
1301
1263
  const model = requestConfig?.model ?? null;
@@ -1358,8 +1320,66 @@ export function createRuntime({ ctx = null, config, options, clock, raw = null,
1358
1320
  usedTokens: hostProbes.usedTokens ?? undefined,
1359
1321
  reservedTokens: hostProbes.reservedTokens ?? undefined,
1360
1322
  });
1323
+ // ---- gate: decided from the REFRESHED measurement (v1.9.5) ------------
1324
+ // The gate used to be computed from `hostProbes` BEFORE this request refreshed
1325
+ // them, so on the first request of a process the ratio was 0 and neither the
1326
+ // hard stop nor the one-shot could ever fire. Every ratio below now comes from
1327
+ // `state`, which is a measurement of THIS request.
1328
+ // Folding is fake relief: it shrinks the PROMPT without reducing what was said,
1329
+ // so a conversation that folds on every request can reset pressure just before
1330
+ // the handoff trigger and never hand over. At the hard stop folding is refused
1331
+ // outright and the handoff is forced — enforced BEFORE any folder runs, because
1332
+ // those folders are what would otherwise keep the ratio down.
1333
+ const gateRatio = Number.isFinite(state.projectedRatio) ? state.projectedRatio : 0;
1334
+ // ---- 0.1 the ONE-SHOT fold (v1.9.5) ------------------------------------
1335
+ // Folding exists so a conversation can actually get smaller — but folding on
1336
+ // every request is the "context drops from 10% to 7% over and over" bug that
1337
+ // v1.6.5 removed. The compromise: a session folds EXACTLY ONCE, at
1338
+ // `context.smartPrune.onceFoldAt`, and is handed off immediately afterwards.
1339
+ // After that this session never folds again, so the surface stops churning and
1340
+ // the handoff — not the folder — is what carries the conversation forward.
1341
+ const onceFoldAt = Number(runtime.config?.context?.smartPrune?.onceFoldAt ?? 0);
1342
+ const sessionKey = session?.id ?? memory.sessionUsage()?.sessionId ?? null;
1343
+ if (!runtime.onceFoldedSessions) runtime.onceFoldedSessions = new Set();
1344
+ const onceFoldDue =
1345
+ onceFoldAt > 0 &&
1346
+ Number.isFinite(gateRatio) &&
1347
+ gateRatio >= onceFoldAt &&
1348
+ sessionKey !== null &&
1349
+ !runtime.onceFoldedSessions.has(sessionKey);
1350
+ // The one-shot allowance is consumed below; the folder itself still respects the
1351
+ // hard stop, so ">= 85%" can never fold even when the one-shot is available.
1352
+ const foldGate = foldBlocked({ projectedRatio: gateRatio });
1353
+ runtime.lastFoldGate = { ...foldGate, ratio: gateRatio, at: memory.clock.now() };
1354
+ if (!foldGate.allowed) {
1355
+ runtime.logger?.warn?.(
1356
+ `hippocampus: [Handoff Guard] 投影占用 ${(gateRatio * 100).toFixed(1)}% >= ` +
1357
+ `折叠硬阈值 ${(FOLD_HARD_STOP_RATIO * 100).toFixed(0)}%:本次请求禁止折叠,强制触发交接。`,
1358
+ );
1359
+ }
1360
+
1361
+ const liveConfig = runtime.config?.context?.liveCompact ?? {};
1362
+ if (liveConfig.enabled !== false && session && foldGate.allowed) {
1363
+ try {
1364
+ runtime.lastLiveCompact = {
1365
+ source: 'hippocampus',
1366
+ at: memory.clock.now(),
1367
+ ...liveCompactSession(session, {
1368
+ thresholdChars: liveConfig.thresholdChars ?? LIVE_COMPACT_DEFAULTS.thresholdChars,
1369
+ headChars: liveConfig.headChars ?? LIVE_COMPACT_DEFAULTS.headChars,
1370
+ tailChars: liveConfig.tailChars ?? LIVE_COMPACT_DEFAULTS.tailChars,
1371
+ maxNodesPerRequest: liveConfig.maxNodesPerRequest ?? LIVE_COMPACT_DEFAULTS.maxNodesPerRequest,
1372
+ logger: runtime.logger,
1373
+ }),
1374
+ };
1375
+ } catch (error) {
1376
+ runtime.logger?.warn?.(`hippocampus: live compact failed: ${asMemoryError(error).code}`);
1377
+ }
1378
+ }
1379
+
1361
1380
  const pruneThreshold = Number.isFinite(pruneConfig.threshold) ? pruneConfig.threshold : 0.85;
1362
- if (pruneConfig.enabled !== false && session && foldGate.allowed && state.projectedRatio >= pruneThreshold) {
1381
+ const pruneAllowed = foldGate.allowed && (state.projectedRatio >= pruneThreshold || onceFoldDue);
1382
+ if (pruneConfig.enabled !== false && session && pruneAllowed) {
1363
1383
  try {
1364
1384
  runtime.lastSmartPrune = {
1365
1385
  source: 'hippocampus',
@@ -1427,6 +1447,36 @@ export function createRuntime({ ctx = null, config, options, clock, raw = null,
1427
1447
  /* advisory only: never block a request on marker upkeep */
1428
1448
  }
1429
1449
 
1450
+ // ---- 2.6 one-shot fold -> immediate handoff (v1.9.5) ------------------
1451
+ // The fold above made the conversation smaller ONCE; now hand it over, so the
1452
+ // smaller surface is what gets summarised and the next session starts clean.
1453
+ // The allowance may only be SPENT where folding was actually permitted. At the
1454
+ // hard stop the fold was refused, so consuming it here would silently steal the
1455
+ // session's single chance to shrink (caught by v195's "cannot be consumed" test).
1456
+ if (onceFoldDue && foldGate.allowed) {
1457
+ if (sessionKey !== null) runtime.onceFoldedSessions.add(sessionKey);
1458
+ try {
1459
+ const handed = await memory.checkHandoff({ now: memory.clock.now(), force: true });
1460
+ runtime.lastOnceFoldHandoff = {
1461
+ at: memory.clock.now(),
1462
+ ratio: gateRatio,
1463
+ onceFoldAt,
1464
+ folded: runtime.lastSmartPrune?.folded ?? 0,
1465
+ handoffActive: handed?.handoff?.active === true,
1466
+ degraded: handed?.handoff?.degraded === true,
1467
+ };
1468
+ } catch (error) {
1469
+ runtime.lastOnceFoldHandoff = {
1470
+ at: memory.clock.now(),
1471
+ ratio: gateRatio,
1472
+ onceFoldAt,
1473
+ folded: runtime.lastSmartPrune?.folded ?? 0,
1474
+ handoffActive: false,
1475
+ error: asMemoryError(error).code,
1476
+ };
1477
+ }
1478
+ }
1479
+
1430
1480
  // ---- 2.7 forced handoff at the folding hard stop (v1.9.4) -------------
1431
1481
  // Reaching the stop line means folding is no longer allowed AND the conversation
1432
1482
  // is at 85% of its window: hand off NOW instead of waiting for the 60% trigger,
@@ -1437,7 +1487,7 @@ export function createRuntime({ ctx = null, config, options, clock, raw = null,
1437
1487
  const forced = await memory.checkHandoff({ now: memory.clock.now(), force: true });
1438
1488
  runtime.lastForcedHandoff = {
1439
1489
  at: memory.clock.now(),
1440
- ratio: prePressure.projectedRatio,
1490
+ ratio: gateRatio,
1441
1491
  active: forced?.handoff?.active === true,
1442
1492
  degraded: forced?.handoff?.degraded === true,
1443
1493
  reason: forced?.forced === true ? 'hard-stop' : 'already-handled',
@@ -1445,7 +1495,7 @@ export function createRuntime({ ctx = null, config, options, clock, raw = null,
1445
1495
  } catch (error) {
1446
1496
  runtime.lastForcedHandoff = {
1447
1497
  at: memory.clock.now(),
1448
- ratio: prePressure.projectedRatio,
1498
+ ratio: gateRatio,
1449
1499
  active: false,
1450
1500
  error: asMemoryError(error).code,
1451
1501
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-layers-hippocampus",
3
- "version": "1.9.4",
3
+ "version": "1.9.5",
4
4
  "private": false,
5
5
  "description": "Layered long-term memory for DeepSeek Harness: append-only event log, rebuildable tag index, day diaries, independent semantic layer, L2.5 trend quarantine, offline knowledge graph and a Web UI panel.",
6
6
  "type": "module",
package/src/config.js CHANGED
@@ -294,7 +294,17 @@ export const DEFAULT_CONFIG = Object.freeze({
294
294
  // replacement for "hope the host compacts": at `threshold` of the REAL window the
295
295
  // oldest verbose turns are rewritten into `[System: …已折叠。原文 ID: ev_x]` pointers.
296
296
  // The log itself is untouched — only the surface the model reads.
297
- smartPrune: { enabled: true, threshold: 0.85, keepRecentTurns: 3, minChars: 1200, maxNodes: 8 },
297
+ // v1.9.5: ONE fold, then hand over. `onceFoldAt` is the point where a session
298
+ // folds ONCE (to actually get smaller) and is then handed off; 0 disables it.
299
+ // The hard stop (0.85) stays the ceiling where folding is refused outright.
300
+ smartPrune: {
301
+ enabled: true,
302
+ threshold: 0.85,
303
+ keepRecentTurns: 3,
304
+ minChars: 1200,
305
+ maxNodes: 8,
306
+ onceFoldAt: 0.65,
307
+ },
298
308
  // Security (v1.5.1): moving memories across spaces is NEVER an autonomous
299
309
  // decision. While true, the engine refuses a model-initiated transfer unless
300
310
  // the tool call carries `user_approved: true`; the UI route is the human
@@ -1020,6 +1030,13 @@ function buildResolvedConfig(raw = {}, { env = process.env } = {}) {
1020
1030
  'context.smartPrune.maxNodes',
1021
1031
  { min: 1, max: 64 },
1022
1032
  );
1033
+ // v1.9.5: the one-shot fold point. 0 means "never fold early" (the old 1.9.4
1034
+ // behaviour); anything in (0, 1] is a ratio.
1035
+ const smartPruneOnceFoldAt = requireNumberInRange(
1036
+ smartPruneIn.onceFoldAt === undefined ? defaults.context.smartPrune.onceFoldAt : smartPruneIn.onceFoldAt,
1037
+ 'context.smartPrune.onceFoldAt',
1038
+ { min: 0, max: 1 },
1039
+ );
1023
1040
  const liveCompactIn = contextIn.liveCompact ?? {};
1024
1041
  const liveCompactEnabled = requireBoolean(
1025
1042
  liveCompactIn.enabled === undefined ? defaults.context.liveCompact.enabled : liveCompactIn.enabled,
@@ -1712,6 +1729,7 @@ function buildResolvedConfig(raw = {}, { env = process.env } = {}) {
1712
1729
  smartPrune: {
1713
1730
  enabled: smartPruneEnabled,
1714
1731
  threshold: smartPruneThreshold,
1732
+ onceFoldAt: smartPruneOnceFoldAt,
1715
1733
  keepRecentTurns: smartPruneKeepRecentTurns,
1716
1734
  minChars: smartPruneMinChars,
1717
1735
  maxNodes: smartPruneMaxNodes,