@jjh32456/dsh-longlive 1.6.6 → 1.6.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +4 -0
- package/package.json +1 -1
- package/src/compaction/engine-core.js +144 -97
- package/test/hot-truncate.test.mjs +113 -0
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,7 @@
|
|
|
1
|
+
## 1.6.7
|
|
2
|
+
|
|
3
|
+
- 热截断加固:turn===0 / 无 turn 记录的组不再永久屏障(含 system/developer 指令的原地保留,只含普通消息的随旧前缀整组归档);中段缺 turn/end 的轮只有最后启动的当前轮不可切;新增 onNoPlan 拒绝原因通道,切不了会写 'hot-truncate: 未切(reason)',杜绝静默零进展。
|
|
4
|
+
|
|
1
5
|
## 1.6.6
|
|
2
6
|
|
|
3
7
|
- 热截断修复:去掉 skipPast 永久钉死(aborted/interrupted 轮只要有 turn/end 就整轮可切);段首 system/message 永不进切段,中途注入的 system 副本随旧前缀归档。修复后真实会话一次可切 35 个 closed 轮(约 46 万 tokens),上下文回到最近 4 轮量级。
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jjh32456/dsh-longlive",
|
|
3
|
-
"version": "1.6.
|
|
3
|
+
"version": "1.6.7",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "久视(longlive):一个磁盘上的共享大脑,让每个会话都是同一个你——记忆卡、状态卡、开机卡让新会话接上同一段关系与未完成的事;配套会话卫生(热截断/备份/D2/D6 裁剪)与记忆工具(巩固/召回/程序性记忆)。1.6.4 修复记忆根可移植性(默认根 = <DSH home>/longlive-memory,一处配置贯穿全部落盘),并同步面板 0.2.5 文案与 G1-G4 修复。",
|
|
6
6
|
"keywords": [
|
|
@@ -342,21 +342,31 @@ function roughPointerTokens(text) {
|
|
|
342
342
|
}
|
|
343
343
|
|
|
344
344
|
/**
|
|
345
|
-
* Select the oldest whole
|
|
346
|
-
*
|
|
345
|
+
* Select the oldest whole closed turns that sit outside the retention window.
|
|
346
|
+
* Returns null when no safe cut exists; every such refusal reports a structured
|
|
347
|
+
* reason through `helpers.onNoPlan` so a silently disabled hot truncate is
|
|
348
|
+
* observable (the engine logs it once per reason).
|
|
347
349
|
*
|
|
348
|
-
* The
|
|
349
|
-
*
|
|
350
|
-
*
|
|
351
|
-
*
|
|
352
|
-
*
|
|
353
|
-
*
|
|
354
|
-
* same cut, which keeps exactly one pointer.
|
|
350
|
+
* The leading run of `system/message` / `developer/message` nodes (the real
|
|
351
|
+
* system prompt at host-protected position 0) is never moved. A `system/message`
|
|
352
|
+
* re-injected by a host restart further inside the surface is an archived copy
|
|
353
|
+
* of that same prompt (host-tolerated inside a replacement range). Checkpoint
|
|
354
|
+
* nodes at the head of the span are folded into the same cut, which keeps
|
|
355
|
+
* exactly one pointer.
|
|
355
356
|
*/
|
|
356
357
|
export function selectCutPlan(session, options, measurement, helpers = {}) {
|
|
357
358
|
const table = helpers.table ?? turnTable(session);
|
|
358
359
|
const groups = surfaceGroups(session, table, helpers.isCheckpointSource);
|
|
359
|
-
|
|
360
|
+
const onNoPlan = typeof helpers.onNoPlan === 'function' ? helpers.onNoPlan : null;
|
|
361
|
+
const noPlan = (reason, detail) => {
|
|
362
|
+
try {
|
|
363
|
+
onNoPlan?.({ reason, detail: detail === undefined ? null : detail });
|
|
364
|
+
} catch {
|
|
365
|
+
// Diagnostics never break a plan.
|
|
366
|
+
}
|
|
367
|
+
return null;
|
|
368
|
+
};
|
|
369
|
+
if (groups.length === 0) return noPlan('no-groups');
|
|
360
370
|
const tokens = new Map();
|
|
361
371
|
for (const node of measurement?.nodes ?? []) {
|
|
362
372
|
if (node && typeof node.seq === 'number' && Number.isFinite(node.tokens)) tokens.set(node.seq, node.tokens);
|
|
@@ -386,29 +396,37 @@ export function selectCutPlan(session, options, measurement, helpers = {}) {
|
|
|
386
396
|
break;
|
|
387
397
|
}
|
|
388
398
|
}
|
|
389
|
-
if (boundary <= 0) return
|
|
399
|
+
if (boundary <= 0) return noPlan('not-enough-turns', { groups: groups.length });
|
|
390
400
|
|
|
391
401
|
const surfaceNodes = session?.surface?.nodes ?? [];
|
|
392
|
-
if (surfaceNodes.length === 0) return
|
|
402
|
+
if (surfaceNodes.length === 0) return noPlan('no-surface');
|
|
393
403
|
|
|
394
|
-
// 2026-11-06 1.6.
|
|
404
|
+
// 2026-11-06 1.6.7 hardening — no permanent barrier, no silent zero progress.
|
|
395
405
|
//
|
|
396
|
-
// 1.6.
|
|
397
|
-
//
|
|
398
|
-
//
|
|
399
|
-
//
|
|
400
|
-
//
|
|
401
|
-
//
|
|
402
|
-
//
|
|
403
|
-
//
|
|
404
|
-
//
|
|
406
|
+
// 1.6.6 removed the aborted-turn freeze but treated every group without a
|
|
407
|
+
// usable turn record as an immovable open group and stopped the span before
|
|
408
|
+
// it. Three surface shapes then made the planner return null forever even
|
|
409
|
+
// though whole closed turns older than the retention window existed:
|
|
410
|
+
// * a log truncated mid-turn leaves orphan messages at the surface head
|
|
411
|
+
// (no turn/start) — a turn===0 group at the head (red team E1);
|
|
412
|
+
// * the host appends a user/message between turns — a turn===0 group in
|
|
413
|
+
// the middle (red team E3);
|
|
414
|
+
// * a turn whose turn/end was lost by log truncation — turn N has no
|
|
415
|
+
// endSeq but later turns exist (red team E2).
|
|
405
416
|
//
|
|
406
|
-
//
|
|
407
|
-
//
|
|
408
|
-
//
|
|
409
|
-
//
|
|
410
|
-
//
|
|
411
|
-
//
|
|
417
|
+
// Rules now:
|
|
418
|
+
// * the whole LEADING run of system/message + developer/message nodes is
|
|
419
|
+
// never moved (head instructions; host-protected position 0);
|
|
420
|
+
// * a turn===0 group that carries a system/developer node is a head
|
|
421
|
+
// instruction zone and stays in place; a turn===0 group of plain messages
|
|
422
|
+
// is an orphan prefix and is archived with the cut (recoverable);
|
|
423
|
+
// * a group whose turn has no turn/end is the CURRENT turn only when it is
|
|
424
|
+
// the last started turn; an earlier turn without turn/end is abandoned
|
|
425
|
+
// (the log was truncated inside it) and is cuttable, archived through an
|
|
426
|
+
// inferred range up to the next turn/start;
|
|
427
|
+
// * the planner skips a blocked head barrier and cuts the next run of
|
|
428
|
+
// wholly-cuttable groups; if nothing is cuttable it reports the reason
|
|
429
|
+
// through onNoPlan instead of returning null silently.
|
|
412
430
|
const groupStart = new Array(groups.length);
|
|
413
431
|
const groupEnd = new Array(groups.length);
|
|
414
432
|
const groupOfPosition = new Array(surfaceNodes.length);
|
|
@@ -420,99 +438,118 @@ export function selectCutPlan(session, options, measurement, helpers = {}) {
|
|
|
420
438
|
groupEnd[index] = cursor - 1;
|
|
421
439
|
}
|
|
422
440
|
}
|
|
441
|
+
const typeOfSeq = (seq) => table.bySeq.get(seq)?.type;
|
|
442
|
+
const isInstructionSeq = (seq) => typeOfSeq(seq) === 'system/message' || typeOfSeq(seq) === 'developer/message';
|
|
443
|
+
const protectedSeqs = new Set();
|
|
444
|
+
{
|
|
445
|
+
let position = 0;
|
|
446
|
+
while (position < surfaceNodes.length && isInstructionSeq(surfaceNodes[position])) {
|
|
447
|
+
protectedSeqs.add(surfaceNodes[position]);
|
|
448
|
+
position += 1;
|
|
449
|
+
}
|
|
450
|
+
}
|
|
451
|
+
const lastStarted = table.turns.length > 0 ? table.turns[table.turns.length - 1] : null;
|
|
423
452
|
const turnInfoOf = (group) => (group.kind === 'turn' && group.turn !== 0 ? table.byNumber.get(group.turn) : undefined);
|
|
424
|
-
const
|
|
453
|
+
const isCurrentTurnGroup = (group) => {
|
|
454
|
+
if (group.kind !== 'turn' || group.turn === 0) return false;
|
|
455
|
+
const info = table.byNumber.get(group.turn);
|
|
456
|
+
if (info === undefined) return true;
|
|
457
|
+
return info.endSeq === null && lastStarted !== null && info.turn === lastStarted.turn;
|
|
458
|
+
};
|
|
459
|
+
const isCuttableGroup = (group) => {
|
|
460
|
+
if (group === undefined) return false;
|
|
461
|
+
if (group.kind === 'checkpoint') return true;
|
|
425
462
|
if (group.kind !== 'turn') return false;
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
463
|
+
// A turn===0 group is either an orphan head zone (contains instructions) or
|
|
464
|
+
// a stray message block between turns; only the latter may be archived.
|
|
465
|
+
if (group.turn === 0) return !group.seqs.some((seq) => isInstructionSeq(seq));
|
|
466
|
+
const info = table.byNumber.get(group.turn);
|
|
467
|
+
if (info === undefined) return false;
|
|
468
|
+
return !isCurrentTurnGroup(group);
|
|
469
|
+
};
|
|
470
|
+
// An abandoned turn (no turn/end, but a later turn/start exists) never got a
|
|
471
|
+
// closing event; the archive range is inferred up to the next turn/start so
|
|
472
|
+
// every one of its original events stays recoverable.
|
|
473
|
+
const resolvedEndSeq = (group) => {
|
|
474
|
+
const info = table.byNumber.get(group.turn);
|
|
475
|
+
const lastSurfaceSeq = group.seqs[group.seqs.length - 1];
|
|
476
|
+
if (info === undefined || !Number.isInteger(info.startSeq)) return lastSurfaceSeq;
|
|
477
|
+
if (Number.isInteger(info.endSeq)) return info.endSeq;
|
|
478
|
+
const next = table.turns.find((entry) => Number.isInteger(entry.startSeq) && entry.startSeq > info.startSeq);
|
|
479
|
+
if (next === undefined) return lastSurfaceSeq;
|
|
480
|
+
return Math.max(info.startSeq, next.startSeq - 1);
|
|
429
481
|
};
|
|
430
482
|
|
|
431
|
-
// The
|
|
432
|
-
//
|
|
433
|
-
//
|
|
483
|
+
// The span starts after the protected head run and on a user/assistant node
|
|
484
|
+
// (the pointer replaces one); a system copy skipped at the head of a turn
|
|
485
|
+
// stays outside the span exactly like 1.6.6 — it is never moved out.
|
|
434
486
|
let startIndex = 0;
|
|
435
|
-
while (startIndex < surfaceNodes.length
|
|
436
|
-
&& table.bySeq.get(surfaceNodes[startIndex])?.type === 'system/message') {
|
|
437
|
-
startIndex += 1;
|
|
438
|
-
}
|
|
439
|
-
// The pointer replaces a user/assistant message, so the first cut node must
|
|
440
|
-
// be one. A leading developer node is kept in place instead of cut.
|
|
487
|
+
while (startIndex < surfaceNodes.length && protectedSeqs.has(surfaceNodes[startIndex])) startIndex += 1;
|
|
441
488
|
while (startIndex < surfaceNodes.length) {
|
|
442
|
-
const type =
|
|
489
|
+
const type = typeOfSeq(surfaceNodes[startIndex]);
|
|
443
490
|
if (type === 'user/message' || type === 'assistant/message') break;
|
|
444
491
|
startIndex += 1;
|
|
445
492
|
}
|
|
446
|
-
if (startIndex >= surfaceNodes.length) return
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
//
|
|
450
|
-
//
|
|
451
|
-
//
|
|
452
|
-
let
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
const type = table.bySeq.get(surfaceNodes[startIndex])?.type;
|
|
462
|
-
if (type === 'user/message' || type === 'assistant/message') break;
|
|
463
|
-
startIndex += 1;
|
|
493
|
+
if (startIndex >= surfaceNodes.length) return noPlan('no-span-start');
|
|
494
|
+
const firstGroup = groupOfPosition[startIndex];
|
|
495
|
+
|
|
496
|
+
// Earliest run of consecutive cuttable groups that lies entirely before the
|
|
497
|
+
// retention boundary and holds at least one real closed turn. Runs made only
|
|
498
|
+
// of orphan messages/pointers are not worth a surface rewrite: skip them.
|
|
499
|
+
let chosenStart = -1;
|
|
500
|
+
let chosenEnd = -1;
|
|
501
|
+
for (let index = firstGroup; index < boundary; index += 1) {
|
|
502
|
+
if (!isCuttableGroup(groups[index])) continue;
|
|
503
|
+
let runEnd = index;
|
|
504
|
+
while (runEnd + 1 < boundary && isCuttableGroup(groups[runEnd + 1])) runEnd += 1;
|
|
505
|
+
let realTurns = 0;
|
|
506
|
+
for (let cursor = index; cursor <= runEnd; cursor += 1) {
|
|
507
|
+
if (groups[cursor].kind === 'turn' && groups[cursor].turn !== 0) realTurns += 1;
|
|
464
508
|
}
|
|
465
|
-
if (
|
|
466
|
-
|
|
509
|
+
if (realTurns > 0) { chosenStart = index; chosenEnd = runEnd; break; }
|
|
510
|
+
index = runEnd;
|
|
467
511
|
}
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
let endGroup = boundary - 1;
|
|
475
|
-
let blockedAt = -1;
|
|
476
|
-
for (let index = startGroup; index <= endGroup; index += 1) {
|
|
477
|
-
if (isOpenGroup(groups[index])) { blockedAt = index; break; }
|
|
512
|
+
if (chosenStart < 0) {
|
|
513
|
+
return noPlan('blocked-prefix', {
|
|
514
|
+
firstGroup,
|
|
515
|
+
boundary,
|
|
516
|
+
blocked: groups.slice(firstGroup, boundary).map((group) => `${group.kind}:${group.turn}:${group.completed}`),
|
|
517
|
+
});
|
|
478
518
|
}
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
519
|
+
let spanStart = Math.max(startIndex, groupStart[chosenStart]);
|
|
520
|
+
const endIndex = groupEnd[chosenEnd];
|
|
521
|
+
if (endIndex < spanStart) return noPlan('empty-span', { chosenStart, chosenEnd });
|
|
482
522
|
|
|
483
523
|
// A system/message node *inside* the span is a host-restart re-injection of
|
|
484
|
-
// the same prompt
|
|
485
|
-
//
|
|
486
|
-
|
|
487
|
-
// injected copy each) outside every future cut, so the context could never
|
|
488
|
-
// reach the retention window. The host tolerates later system nodes inside a
|
|
489
|
-
// replacement range, so an injected copy is archived together with the old
|
|
490
|
-
// prefix it belongs to. Nodes before the span start are never touched.
|
|
491
|
-
const shadowedSeqs = surfaceNodes.slice(startIndex, endIndex + 1);
|
|
524
|
+
// the same prompt; the host tolerates it inside a replacement range and it is
|
|
525
|
+
// archived together with the old prefix (approved 1.6.6 behaviour).
|
|
526
|
+
const shadowedSeqs = surfaceNodes.slice(spanStart, endIndex + 1);
|
|
492
527
|
const shadowedSet = new Set(shadowedSeqs);
|
|
493
528
|
const touchedGroups = groups.filter((group) => group.seqs.some((seq) => shadowedSet.has(seq)));
|
|
494
|
-
if (touchedGroups.some((group) =>
|
|
529
|
+
if (touchedGroups.some((group) => isCurrentTurnGroup(group))) return noPlan('current-turn-touched');
|
|
530
|
+
// No half turn: every touched turn group must be fully shadowed. The only
|
|
531
|
+
// allowed exception is a system/developer copy skipped at the head of the
|
|
532
|
+
// first group (it stays on the surface, never half-cut).
|
|
495
533
|
for (const group of touchedGroups) {
|
|
496
534
|
if (group.kind !== 'turn') continue;
|
|
497
535
|
const missing = group.seqs.filter((seq) => !shadowedSet.has(seq));
|
|
498
|
-
if (missing.some((seq) =>
|
|
536
|
+
if (missing.some((seq) => !isInstructionSeq(seq))) return noPlan('split-group', { turn: group.turn });
|
|
499
537
|
}
|
|
500
538
|
const cutTurnGroups = touchedGroups.filter((group) => {
|
|
501
539
|
if (group.kind !== 'turn' || group.turn === 0) return false;
|
|
502
|
-
|
|
503
|
-
return info !== undefined && info.endSeq !== null;
|
|
540
|
+
return !isCurrentTurnGroup(group);
|
|
504
541
|
});
|
|
505
|
-
if (cutTurnGroups.length === 0) return
|
|
542
|
+
if (cutTurnGroups.length === 0) return noPlan('no-turns', { chosenStart, chosenEnd });
|
|
506
543
|
|
|
507
|
-
// Archive every original event of the cut turns, plus any checkpoint node
|
|
508
|
-
// that positionally lived inside the replaced span
|
|
544
|
+
// Archive every original event of the cut turns, plus any checkpoint node and
|
|
545
|
+
// orphan (turn===0) message that positionally lived inside the replaced span.
|
|
509
546
|
const archivedSeqs = new Set();
|
|
510
547
|
const archiveEvents = [];
|
|
511
548
|
for (const group of cutTurnGroups) {
|
|
512
|
-
const
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
for (let seq = startSeq; seq <= endSeq; seq += 1) {
|
|
549
|
+
const info = table.byNumber.get(group.turn);
|
|
550
|
+
if (info === undefined || !Number.isInteger(info.startSeq)) continue;
|
|
551
|
+
const endSeq = resolvedEndSeq(group);
|
|
552
|
+
for (let seq = info.startSeq; seq <= endSeq; seq += 1) {
|
|
516
553
|
const event = table.bySeq.get(seq);
|
|
517
554
|
if (event === undefined || archivedSeqs.has(seq)) continue;
|
|
518
555
|
archivedSeqs.add(seq);
|
|
@@ -536,13 +573,13 @@ export function selectCutPlan(session, options, measurement, helpers = {}) {
|
|
|
536
573
|
firstSeq: seqs[0],
|
|
537
574
|
lastSeq: seqs[seqs.length - 1],
|
|
538
575
|
startSeq: info?.startSeq ?? seqs[0],
|
|
539
|
-
endSeq:
|
|
576
|
+
endSeq: resolvedEndSeq(group),
|
|
540
577
|
surfaceEvents: seqs.length,
|
|
541
578
|
tokens: seqs.reduce((total, seq) => total + prices.get(seq), 0),
|
|
542
579
|
};
|
|
543
580
|
});
|
|
544
581
|
const shadowedTokenCount = shadowedSeqs.reduce((total, seq) => total + prices.get(seq), 0);
|
|
545
|
-
if (shadowedTokenCount <= 0) return
|
|
582
|
+
if (shadowedTokenCount <= 0) return noPlan('zero-token');
|
|
546
583
|
|
|
547
584
|
const lastTouchedIndex = groups.indexOf(touchedGroups[touchedGroups.length - 1]);
|
|
548
585
|
const retainedGroups = groups.slice(lastTouchedIndex + 1);
|
|
@@ -569,7 +606,9 @@ export function selectCutPlan(session, options, measurement, helpers = {}) {
|
|
|
569
606
|
retainedTurns,
|
|
570
607
|
});
|
|
571
608
|
const pointerTokens = roughPointerTokens(previewText);
|
|
572
|
-
if (shadowedTokenCount < pointerTokens + options.minCutTokens)
|
|
609
|
+
if (shadowedTokenCount < pointerTokens + options.minCutTokens) {
|
|
610
|
+
return noPlan('pointer-too-large', { shadowedTokenCount, pointerTokens });
|
|
611
|
+
}
|
|
573
612
|
|
|
574
613
|
return {
|
|
575
614
|
start: shadowedSeqs[0],
|
|
@@ -895,6 +934,14 @@ export function createCutEngine(deps) {
|
|
|
895
934
|
return selectCutPlan(session, this.options, measurement, {
|
|
896
935
|
table,
|
|
897
936
|
isCheckpointSource: isCompactCheckpointSource,
|
|
937
|
+
// "cut, or say why not": a refusal that would otherwise be silent is
|
|
938
|
+
// logged once per reason (warnOnce) so a permanently disabled hot
|
|
939
|
+
// truncate is always observable in the host log.
|
|
940
|
+
onNoPlan: (info) => {
|
|
941
|
+
const reason = String(info?.reason ?? 'unknown');
|
|
942
|
+
const detail = info?.detail === undefined || info?.detail === null ? '' : ` ${JSON.stringify(info.detail)}`;
|
|
943
|
+
this.warnOnce(`no-plan:${reason}`, `hot-truncate: 未切(${reason})${detail}`);
|
|
944
|
+
},
|
|
898
945
|
});
|
|
899
946
|
}
|
|
900
947
|
|
|
@@ -559,6 +559,119 @@ test('H30b real-session shape: head system + mid-surface injected system + abort
|
|
|
559
559
|
assert.equal(session.surface.nodes.includes(injected.seq), false, 'injected copy left the surface');
|
|
560
560
|
});
|
|
561
561
|
|
|
562
|
+
// ---------------------------------------------------------------------------
|
|
563
|
+
// 1.6.7 hardening: barriers must never disable hot truncate silently
|
|
564
|
+
// ---------------------------------------------------------------------------
|
|
565
|
+
|
|
566
|
+
/** Append a message-shaped event that belongs to no turn (no turn/start). */
|
|
567
|
+
function orphanMessage(session, type, id, text) {
|
|
568
|
+
const data = type === 'assistant/message'
|
|
569
|
+
? { message: { id, role: 'assistant', content: [{ type: 'text', text }] } }
|
|
570
|
+
: { role: type === 'system/message' ? 'system' : 'user', id, content: [{ type: 'text', text }] };
|
|
571
|
+
return session.append(type, data, { surfaceOp: 'append' });
|
|
572
|
+
}
|
|
573
|
+
|
|
574
|
+
test('H35 head truncated mid-turn (orphan messages before the system prompt) still cuts', async () => {
|
|
575
|
+
// Red team E1: raw log truncation drops the head turn/start, leaving orphan
|
|
576
|
+
// user/assistant surface nodes before the re-appended system prompt. 1.6.6
|
|
577
|
+
// treated the turn===0 group as open => plan null forever. Now the orphan
|
|
578
|
+
// block/head instructions are kept in place and the closed turns after them
|
|
579
|
+
// are cut (red team E1: null -> [1,2]).
|
|
580
|
+
const session = new MockSession();
|
|
581
|
+
const orphanU = orphanMessage(session, 'user/message', 'orphan-u', 'orphan-mid-turn');
|
|
582
|
+
orphanMessage(session, 'assistant/message', 'orphan-a', 'orphan answer');
|
|
583
|
+
const headSystem = orphanMessage(session, 'system/message', 'head-sys', 'HEAD-SYSTEM');
|
|
584
|
+
for (let turn = 1; turn <= 7; turn += 1) {
|
|
585
|
+
if (turn === 7) addTurn(session, turn, { open: true });
|
|
586
|
+
else addTurn(session, turn, { completed: true });
|
|
587
|
+
}
|
|
588
|
+
const dir = caseDir('archive');
|
|
589
|
+
const { engine } = engineFor({ archiveDir: dir, perEvent: 5000, retainTurns: 4, retainTokens: 0, autoMinTokens: 0 });
|
|
590
|
+
const result = await engine.compactIfNeeded(makeAgent(session), 'pressure', new AbortController().signal);
|
|
591
|
+
assert.ok(result, 'the head barrier must not disable the planner (E1 was null before)');
|
|
592
|
+
const file = fs.readdirSync(dir).find((name) => name.endsWith('.jsonl'));
|
|
593
|
+
const parsed = readArchive(path.join(dir, file));
|
|
594
|
+
assert.deepEqual(parsed.header.cutTurns.map((entry) => entry.turn), [1, 2]);
|
|
595
|
+
assert.equal(result.shadowedSeqs.includes(headSystem.seq), false, 'head system instruction is never cut');
|
|
596
|
+
assert.equal(session.surface.nodes.includes(headSystem.seq), true, 'head system instruction stays on the surface');
|
|
597
|
+
assert.equal(session.surface.nodes.includes(orphanU.seq), true, 'orphan head block stays in place');
|
|
598
|
+
});
|
|
599
|
+
|
|
600
|
+
test('H36 a stray user/message between turns does not pin the prefix (red team E3)', async () => {
|
|
601
|
+
const session = new MockSession();
|
|
602
|
+
const headSystem = orphanMessage(session, 'system/message', 'head-sys', 'HEAD');
|
|
603
|
+
addTurn(session, 1, { completed: true });
|
|
604
|
+
const stray = session.append('user/message', {
|
|
605
|
+
id: 'between-turns', role: 'user', content: [{ type: 'text', text: 'between-turns' }],
|
|
606
|
+
}, { surfaceOp: 'append' });
|
|
607
|
+
for (let turn = 2; turn <= 7; turn += 1) {
|
|
608
|
+
if (turn === 7) addTurn(session, turn, { open: true });
|
|
609
|
+
else addTurn(session, turn, { completed: true });
|
|
610
|
+
}
|
|
611
|
+
const dir = caseDir('archive');
|
|
612
|
+
const { engine } = engineFor({ archiveDir: dir, perEvent: 5000, retainTurns: 4, retainTokens: 0, autoMinTokens: 0 });
|
|
613
|
+
const result = await engine.compactIfNeeded(makeAgent(session), 'pressure', new AbortController().signal);
|
|
614
|
+
assert.ok(result, 'the stray turn-0 group must not freeze the cut (E3 cut only [1] before)');
|
|
615
|
+
const file = fs.readdirSync(dir).find((name) => name.endsWith('.jsonl'));
|
|
616
|
+
const parsed = readArchive(path.join(dir, file));
|
|
617
|
+
assert.deepEqual(parsed.header.cutTurns.map((entry) => entry.turn), [1, 2]);
|
|
618
|
+
assert.equal(result.shadowedSeqs.includes(stray.seq), true, 'the stray message is inside the cut span');
|
|
619
|
+
assert.equal(session.surface.nodes.includes(stray.seq), false, 'the stray message left the surface');
|
|
620
|
+
assert.equal(session.surface.nodes.includes(headSystem.seq), true, 'the head system instruction stays');
|
|
621
|
+
assert.equal(parsed.records.some((record) => record.seq === stray.seq), true, 'the stray message is recoverable from the archive');
|
|
622
|
+
});
|
|
623
|
+
|
|
624
|
+
test('H37 a mid-log turn missing turn/end does not block later cuts (red team E2)', async () => {
|
|
625
|
+
const session = new MockSession();
|
|
626
|
+
orphanMessage(session, 'system/message', 'head-sys', 'HEAD');
|
|
627
|
+
addTurn(session, 1, { completed: true });
|
|
628
|
+
const abandonedStart = session.events.length;
|
|
629
|
+
addTurn(session, 2, { open: true }); // turn/end lost by log truncation
|
|
630
|
+
const abandonedEnd = session.events.length - 1;
|
|
631
|
+
for (let turn = 3; turn <= 6; turn += 1) addTurn(session, turn, { completed: true });
|
|
632
|
+
addTurn(session, 7, { open: true }); // the real current turn
|
|
633
|
+
const currentUser = session.events.find((event) => event.type === 'user/message' && event.data.id === 'user-7');
|
|
634
|
+
const dir = caseDir('archive');
|
|
635
|
+
const { engine } = engineFor({ archiveDir: dir, perEvent: 5000, retainTurns: 4, retainTokens: 0, autoMinTokens: 0 });
|
|
636
|
+
const result = await engine.compactIfNeeded(makeAgent(session), 'pressure', new AbortController().signal);
|
|
637
|
+
assert.ok(result, 'an abandoned mid-log unclosed turn must not freeze the planner');
|
|
638
|
+
const file = fs.readdirSync(dir).find((name) => name.endsWith('.jsonl'));
|
|
639
|
+
const parsed = readArchive(path.join(dir, file));
|
|
640
|
+
assert.deepEqual(parsed.header.cutTurns.map((entry) => entry.turn), [1, 2], 'turns before the retention window are cut');
|
|
641
|
+
const abandoned = parsed.header.cutTurns.find((entry) => entry.turn === 2);
|
|
642
|
+
assert.ok(abandoned.endSeq > abandoned.startSeq, 'abandoned turn archive range is inferred, not empty');
|
|
643
|
+
assert.equal(parsed.records.some((record) => record.turn === 2), true, 'abandoned turn events are recoverable');
|
|
644
|
+
assert.equal(session.surface.nodes.includes(currentUser.seq), true, 'the current open turn is never cut');
|
|
645
|
+
assert.equal(session.surface.nodes.some((seq) => seq >= abandonedStart && seq <= abandonedEnd), false, 'the abandoned turn left the surface');
|
|
646
|
+
assert.equal(result.shadowedSeqs.includes(currentUser.seq), false);
|
|
647
|
+
});
|
|
648
|
+
|
|
649
|
+
test('H38 "cut or say why": a blocked prefix logs a structured refusal instead of silence', async () => {
|
|
650
|
+
// Normal case: not enough completed turns yet.
|
|
651
|
+
const small = new MockSession();
|
|
652
|
+
addTurn(small, 1, { completed: true });
|
|
653
|
+
addTurn(small, 2, { completed: false });
|
|
654
|
+
addTurn(small, 3, { open: true });
|
|
655
|
+
const { engine, ctx } = engineFor({ archiveDir: caseDir('archive'), perEvent: 5000, retainTurns: 4, retainTokens: 0, autoMinTokens: 0 });
|
|
656
|
+
const none = await engine.compactIfNeeded(makeAgent(small), 'pressure', new AbortController().signal);
|
|
657
|
+
assert.equal(none, null);
|
|
658
|
+
assert.match(ctx.text('warn'), /未切(not-enough-turns)/);
|
|
659
|
+
|
|
660
|
+
// Blocked prefix: the only groups older than the boundary are a turn===0
|
|
661
|
+
// system zone, so there is nothing whole to cut. The reason must be visible.
|
|
662
|
+
const blocked = new MockSession();
|
|
663
|
+
orphanMessage(blocked, 'system/message', 'head-sys', 'HEAD');
|
|
664
|
+
for (let turn = 1; turn <= 5; turn += 1) {
|
|
665
|
+
if (turn === 5) addTurn(blocked, turn, { open: true });
|
|
666
|
+
else addTurn(blocked, turn, { completed: true });
|
|
667
|
+
}
|
|
668
|
+
const second = engineFor({ archiveDir: caseDir('archive'), perEvent: 5000, retainTurns: 4, retainTokens: 0, autoMinTokens: 0 });
|
|
669
|
+
const blockedResult = await second.engine.compactIfNeeded(makeAgent(blocked), 'pressure', new AbortController().signal);
|
|
670
|
+
assert.equal(blockedResult, null, 'nothing cuttable yet');
|
|
671
|
+
assert.match(second.ctx.text('warn'), /未切(blocked-prefix)/);
|
|
672
|
+
assert.match(second.ctx.text('warn'), /head-sys|blocked-prefix/);
|
|
673
|
+
});
|
|
674
|
+
|
|
562
675
|
test('H4 archive: atomic JSONL, exact event count, sha256, append-only', async () => {
|
|
563
676
|
const dir = caseDir('archive');
|
|
564
677
|
const session = buildSession(9, { openLast: false });
|