stratagate-dsh 0.3.29 → 0.3.31

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/dist/plugin.js CHANGED
@@ -9,6 +9,7 @@ var StructuredReasoningEffortSettings = z.object({
9
9
  structuredReasoningEffort: z.union(["auto", "force-off"]).default("auto").description("\u8BB0\u5FC6\u5904\u7406\u7ED3\u6784\u5316\u8C03\u7528\u7684\u63A8\u7406\u6863\u4F4D\u7B56\u7565").comment("auto\uFF1A\u4EC5\u5728\u6A21\u578B\u660E\u786E\u652F\u6301 off \u65F6\u4F7F\u7528\uFF1Bforce-off\uFF1A\u4F18\u5148\u4F7F\u7528 off\uFF0C\u4E0D\u652F\u6301\u6216\u80FD\u529B\u68C0\u67E5\u5931\u8D25\u65F6\u5B89\u5168\u964D\u7EA7\uFF0C\u5E76\u5BF9\u540C\u4E00\u6A21\u578B\u53EA\u544A\u8B66\u4E00\u6B21\u3002"),
10
10
  showStrataGateStatus: z.boolean().default(true).description("\u663E\u793A StrataGate \u72B6\u6001\u63D0\u793A").comment("\u5173\u95ED\u540E\u9690\u85CF\u804A\u5929\u754C\u9762\u4E2D\u7684 StrataGate \u72B6\u6001\u4FE1\u606F\uFF1B\u8BB0\u5FC6\u3001\u68C0\u7D22\u548C\u540E\u53F0\u5904\u7406\u4E0D\u53D7\u5F71\u54CD\u3002"),
11
11
  showShortTermStatus: z.boolean().default(true).description("\u663E\u793A\u77ED\u671F\u8BB0\u5FC6\u5757\u72B6\u6001").comment("\u63A7\u5236\u77ED\u671F\u8BB0\u5FC6\u5757\u8FDB\u5EA6\u3001Block \u5C01\u5B58\u4E0E\u6574\u7406\u72B6\u6001\u7684\u804A\u5929\u5185\u63D0\u793A\uFF1B\u540E\u53F0\u5904\u7406\u4E0D\u53D7\u5F71\u54CD\u3002"),
12
+ showSidebarEntry: z.boolean().default(false).description("\u5728\u5DE6\u4FA7\u680F\u663E\u793A StrataGate").comment("\u4EC5\u63A7\u5236\u5DE6\u4FA7\u680F\u5165\u53E3\uFF1B\u5173\u95ED\u540E\u4ECD\u53EF\u4ECE\u8BBE\u7F6E\u6253\u5F00\uFF0C\u8BB0\u5FC6\u3001\u68C0\u7D22\u548C\u540E\u53F0\u5904\u7406\u4E0D\u53D7\u5F71\u54CD\u3002"),
12
13
  showRetrievalStatus: z.boolean().default(true).description("\u663E\u793A\u8BB0\u5FC6\u68C0\u7D22\u72B6\u6001").comment("\u63A7\u5236\u68C0\u7D22\u6B21\u6570\u3001\u8FD4\u56DE\u6570\u91CF\u548C\u8BB0\u5FC6\u91C7\u7528\u4FE1\u606F\u7684\u804A\u5929\u5185\u63D0\u793A\uFF1B\u68C0\u7D22\u4E0E\u91C7\u7528\u4E0D\u53D7\u5F71\u54CD\u3002")
13
14
  });
14
15
  function volatileIfSupported(schema) {
@@ -32,7 +33,8 @@ var Config = z.object({
32
33
  structuredReasoningEffort: volatileIfSupported(z.union(["auto", "force-off"]).default("auto")),
33
34
  showStrataGateStatus: volatileIfSupported(z.boolean().default(true)),
34
35
  showShortTermStatus: volatileIfSupported(z.boolean().default(true)),
35
- showRetrievalStatus: volatileIfSupported(z.boolean().default(true))
36
+ showRetrievalStatus: volatileIfSupported(z.boolean().default(true)),
37
+ showSidebarEntry: volatileIfSupported(z.boolean().default(false))
36
38
  });
37
39
  function liveConfigValue(value) {
38
40
  return value && typeof value === "object" && "get" in value && typeof value.get === "function" ? value.get() : value;
@@ -68,7 +70,8 @@ function resolveConfig(config) {
68
70
  structuredReasoningEffort: liveConfigValue(config.structuredReasoningEffort) ?? "auto",
69
71
  showStrataGateStatus: liveConfigValue(config.showStrataGateStatus) ?? true,
70
72
  showShortTermStatus: liveConfigValue(config.showShortTermStatus) ?? true,
71
- showRetrievalStatus: liveConfigValue(config.showRetrievalStatus) ?? true
73
+ showRetrievalStatus: liveConfigValue(config.showRetrievalStatus) ?? true,
74
+ showSidebarEntry: liveConfigValue(config.showSidebarEntry) ?? false
72
75
  };
73
76
  }
74
77
 
@@ -79,6 +82,29 @@ import { AsyncLocalStorage } from "node:async_hooks";
79
82
  var DEFAULT_BLOCK_TURN_SIZE = 12;
80
83
  var BLOCK_MAX_LEVEL = 5;
81
84
  var BLOCK_DECAY_LAMBDA = 0.3;
85
+ function buildBlockTimeline(blocks) {
86
+ const threads = /* @__PURE__ */ new Map();
87
+ for (const block of blocks) {
88
+ const thread = threads.get(block.threadId) ?? [];
89
+ thread.push(block);
90
+ threads.set(block.threadId, thread);
91
+ }
92
+ const positions = /* @__PURE__ */ new Map();
93
+ for (const thread of threads.values()) {
94
+ thread.sort((a, b) => a.sequence - b.sequence);
95
+ thread.forEach((block, index) => positions.set(block.id, {
96
+ blockPosition: index + 1,
97
+ latestBlockPosition: thread.length
98
+ }));
99
+ }
100
+ return positions;
101
+ }
102
+ function getBlockDecayView(block, position, lambda = BLOCK_DECAY_LAMBDA) {
103
+ return {
104
+ age: Math.max(0, position.latestBlockPosition - position.blockPosition),
105
+ level: getDecayedBlockLevel(block.pointerAnchorLevel, block.pointerAnchorBlockPosition, position.latestBlockPosition, lambda)
106
+ };
107
+ }
82
108
  var FILLER_ONLY = /* @__PURE__ */ new Set([
83
109
  "ok",
84
110
  "okay",
@@ -1873,7 +1899,7 @@ ${lines.join("\n")}`;
1873
1899
  }
1874
1900
 
1875
1901
  // packages/core/src/storage.ts
1876
- var STRATAGATE_STORAGE_SCHEMA_VERSION = 13;
1902
+ var STRATAGATE_STORAGE_SCHEMA_VERSION = 14;
1877
1903
  var KNOWLEDGE_GRAPH_PROJECTOR_VERSION = 1;
1878
1904
  var DERIVATION_MAX_ATTEMPTS = 3;
1879
1905
  var DERIVATION_LEASE_MS = 5 * 6e4;
@@ -2035,7 +2061,7 @@ function normalizeSnapshot(value) {
2035
2061
  schemaVersion: STRATAGATE_STORAGE_SCHEMA_VERSION,
2036
2062
  agentEvents: []
2037
2063
  };
2038
- } else if (schemaVersion === 12) {
2064
+ } else if (schemaVersion === 12 || schemaVersion === 13) {
2039
2065
  snapshot = { ...structuredClone(value), schemaVersion: STRATAGATE_STORAGE_SCHEMA_VERSION };
2040
2066
  } else if (schemaVersion === STRATAGATE_STORAGE_SCHEMA_VERSION) {
2041
2067
  snapshot = structuredClone(value);
@@ -2093,8 +2119,8 @@ function normalizeSnapshot(value) {
2093
2119
  if (!Number.isSafeInteger(block.pointerAnchorBlockPosition) || block.pointerAnchorBlockPosition < 1) {
2094
2120
  throw new TypeError("Invalid StrataGate snapshot: pointerAnchorBlockPosition must be a positive integer");
2095
2121
  }
2096
- if (block.surfaceCurrentLevel === void 0) block.surfaceCurrentLevel = null;
2097
- if (block.surfaceCurrentLevel != null && (!Number.isSafeInteger(block.surfaceCurrentLevel) || block.surfaceCurrentLevel < 0 || block.surfaceCurrentLevel > 5)) {
2122
+ if (block.surfaceCurrentLevel === void 0) block.surfaceCurrentLevel = block.pointerCurrentLevel;
2123
+ if (!Number.isSafeInteger(block.surfaceCurrentLevel) || block.surfaceCurrentLevel < 0 || block.surfaceCurrentLevel > 5) {
2098
2124
  throw new TypeError("Invalid StrataGate snapshot: surfaceCurrentLevel must be a Block level");
2099
2125
  }
2100
2126
  if (block.lastLiftedBy !== null && block.lastLiftedBy !== "user" && block.lastLiftedBy !== "agent") {
@@ -2104,6 +2130,22 @@ function normalizeSnapshot(value) {
2104
2130
  if (snapshot.successfulModelResponses.length > 5) {
2105
2131
  snapshot.successfulModelResponses = snapshot.successfulModelResponses.slice(-5);
2106
2132
  }
2133
+ if (schemaVersion !== STRATAGATE_STORAGE_SCHEMA_VERSION) {
2134
+ const pendingCounts = /* @__PURE__ */ new Map();
2135
+ for (const block of snapshot.blocks) {
2136
+ if (block.processingStatus === "pending") {
2137
+ pendingCounts.set(block.threadId, (pendingCounts.get(block.threadId) ?? 0) + 1);
2138
+ }
2139
+ }
2140
+ for (const block of snapshot.blocks) {
2141
+ if (block.processingStatus === "ready") {
2142
+ block.pointerAnchorBlockPosition += pendingCounts.get(block.threadId) ?? 0;
2143
+ if (!Number.isSafeInteger(block.pointerAnchorBlockPosition)) {
2144
+ throw new TypeError("Invalid StrataGate snapshot: migrated Block anchor exceeds safe integer range");
2145
+ }
2146
+ }
2147
+ }
2148
+ }
2107
2149
  return snapshot;
2108
2150
  }
2109
2151
  function assertValidSnapshot(value) {
@@ -3743,7 +3785,7 @@ CREATE TABLE IF NOT EXISTS blocks (
3743
3785
  l3_condensed TEXT NOT NULL,
3744
3786
  l4_readable TEXT NOT NULL,
3745
3787
  pointer_current_level INTEGER NOT NULL,
3746
- surface_current_level INTEGER CHECK (surface_current_level BETWEEN 0 AND 5),
3788
+ surface_current_level INTEGER NOT NULL DEFAULT 5,
3747
3789
  pointer_anchor_level INTEGER NOT NULL,
3748
3790
  pointer_anchor_block_position INTEGER NOT NULL,
3749
3791
  last_lifted_at TEXT,
@@ -4146,7 +4188,7 @@ var SqliteStorage = class _SqliteStorage {
4146
4188
  FROM memory_spaces WHERE namespace = ?
4147
4189
  `).get(key);
4148
4190
  if (!space) return null;
4149
- if (space.schema_version !== STRATAGATE_STORAGE_SCHEMA_VERSION && !(this.readonly && space.schema_version === 12)) {
4191
+ if (space.schema_version !== STRATAGATE_STORAGE_SCHEMA_VERSION && !(this.readonly && [12, 13].includes(space.schema_version))) {
4150
4192
  throw new Error(`Unsupported stored StrataGate schema: ${space.schema_version}`);
4151
4193
  }
4152
4194
  const messageRows = this.database.prepare(`
@@ -4193,7 +4235,7 @@ var SqliteStorage = class _SqliteStorage {
4193
4235
  l4Readable: row.l4_readable,
4194
4236
  l5Raw: messagesByBlock.get(row.id) ?? [],
4195
4237
  pointerCurrentLevel: row.pointer_current_level,
4196
- surfaceCurrentLevel: row.surface_current_level == null ? null : row.surface_current_level,
4238
+ surfaceCurrentLevel: row.surface_current_level ?? row.pointer_current_level,
4197
4239
  pointerAnchorLevel: row.pointer_anchor_level,
4198
4240
  pointerAnchorBlockPosition: row.pointer_anchor_block_position,
4199
4241
  lastLiftedAt: row.last_lifted_at,
@@ -4388,7 +4430,7 @@ var SqliteStorage = class _SqliteStorage {
4388
4430
  const hasTopics = this.database.prepare("SELECT 1 FROM sqlite_master WHERE type = 'table' AND name = 'memory_topic_state'").get();
4389
4431
  const topicState = hasTopics ? this.database.prepare("SELECT state_json FROM memory_topic_state WHERE namespace = ?").get(key) : void 0;
4390
4432
  const snapshot = {
4391
- schemaVersion: STRATAGATE_STORAGE_SCHEMA_VERSION,
4433
+ schemaVersion: space.schema_version,
4392
4434
  currentTurn: space.current_turn,
4393
4435
  blockTurnSize: space.block_turn_size,
4394
4436
  blockDecayLambda: space.block_decay_lambda,
@@ -4677,7 +4719,7 @@ var SqliteStorage = class _SqliteStorage {
4677
4719
  block.l3Condensed,
4678
4720
  block.l4Readable,
4679
4721
  block.pointerCurrentLevel,
4680
- block.surfaceCurrentLevel ?? null,
4722
+ block.surfaceCurrentLevel ?? block.pointerCurrentLevel,
4681
4723
  block.pointerAnchorLevel,
4682
4724
  block.pointerAnchorBlockPosition,
4683
4725
  block.lastLiftedAt,
@@ -5475,7 +5517,8 @@ var SqliteStorage = class _SqliteStorage {
5475
5517
  this.database.exec("ALTER TABLE blocks ADD COLUMN processing_status TEXT NOT NULL DEFAULT 'ready' CHECK (processing_status IN ('pending', 'ready'))");
5476
5518
  }
5477
5519
  if (!blockColumns.some(({ name: name2 }) => name2 === "surface_current_level")) {
5478
- this.database.exec("ALTER TABLE blocks ADD COLUMN surface_current_level INTEGER CHECK (surface_current_level BETWEEN 0 AND 5)");
5520
+ this.database.exec("ALTER TABLE blocks ADD COLUMN surface_current_level INTEGER NOT NULL DEFAULT 5");
5521
+ this.database.exec("UPDATE blocks SET surface_current_level = pointer_current_level");
5479
5522
  }
5480
5523
  const eventColumns = this.database.prepare("PRAGMA table_info('events')").all();
5481
5524
  if (!eventColumns.some(({ name: name2 }) => name2 === "formed_turn")) {
@@ -5509,6 +5552,27 @@ var SqliteStorage = class _SqliteStorage {
5509
5552
  this.rebuildLegacyElementSourceForeignKeys();
5510
5553
  this.enableRawSearchFts();
5511
5554
  this.database.prepare("INSERT INTO stratagate_graph_policy_migrations (id, completed_at) VALUES (1, ?) ON CONFLICT (id) DO NOTHING").run(nowUtc8());
5555
+ this.database.exec(`
5556
+ WITH pending_counts AS MATERIALIZED (
5557
+ SELECT namespace, thread_id, COUNT(*) AS count FROM blocks
5558
+ WHERE processing_status = 'pending' GROUP BY namespace, thread_id
5559
+ )
5560
+ UPDATE blocks AS target
5561
+ SET pointer_anchor_block_position = pointer_anchor_block_position + (
5562
+ SELECT pending.count FROM pending_counts AS pending
5563
+ WHERE pending.namespace = target.namespace
5564
+ AND pending.thread_id IS target.thread_id
5565
+ )
5566
+ WHERE target.processing_status = 'ready'
5567
+ AND EXISTS (
5568
+ SELECT 1 FROM pending_counts AS pending
5569
+ WHERE pending.namespace = target.namespace AND pending.thread_id IS target.thread_id
5570
+ )
5571
+ AND EXISTS (
5572
+ SELECT 1 FROM memory_spaces AS space
5573
+ WHERE space.namespace = target.namespace AND space.schema_version < 14
5574
+ )
5575
+ `);
5512
5576
  this.database.prepare("UPDATE memory_spaces SET schema_version = ? WHERE schema_version < ?").run(STRATAGATE_STORAGE_SCHEMA_VERSION, STRATAGATE_STORAGE_SCHEMA_VERSION);
5513
5577
  this.database.exec(`PRAGMA user_version = ${STRATAGATE_STORAGE_SCHEMA_VERSION}`);
5514
5578
  });
@@ -5521,7 +5585,8 @@ var SqliteStorage = class _SqliteStorage {
5521
5585
  if (!this.hasColumn("memory_spaces", "graph_epoch")) this.database.exec("ALTER TABLE memory_spaces ADD COLUMN graph_epoch INTEGER NOT NULL DEFAULT 0");
5522
5586
  if (!this.hasColumn("memory_spaces", "graph_approved_event_ids_json")) this.database.exec("ALTER TABLE memory_spaces ADD COLUMN graph_approved_event_ids_json TEXT NOT NULL DEFAULT '[]'");
5523
5587
  if (!this.hasColumn("blocks", "surface_current_level")) {
5524
- this.database.exec("ALTER TABLE blocks ADD COLUMN surface_current_level INTEGER CHECK (surface_current_level BETWEEN 0 AND 5)");
5588
+ this.database.exec("ALTER TABLE blocks ADD COLUMN surface_current_level INTEGER NOT NULL DEFAULT 5");
5589
+ this.database.exec("UPDATE blocks SET surface_current_level = pointer_current_level");
5525
5590
  }
5526
5591
  this.database.prepare("INSERT INTO stratagate_graph_policy_migrations (id, completed_at) VALUES (1, ?) ON CONFLICT (id) DO NOTHING").run(nowUtc8());
5527
5592
  this.database.exec(THREAD_INDEXES);
@@ -5736,7 +5801,7 @@ var SqliteStorage = class _SqliteStorage {
5736
5801
  }
5737
5802
  assertSchemaVersion() {
5738
5803
  const version = this.userVersion();
5739
- if (version !== STRATAGATE_STORAGE_SCHEMA_VERSION && !(this.readonly && version === 12)) {
5804
+ if (version !== STRATAGATE_STORAGE_SCHEMA_VERSION && !(this.readonly && [12, 13].includes(version))) {
5740
5805
  throw new Error(`Unsupported SQLite schema version: ${version}`);
5741
5806
  }
5742
5807
  }
@@ -7327,19 +7392,11 @@ var StrataGate = class _StrataGate {
7327
7392
  * conversation; omitting it intentionally returns every thread in this
7328
7393
  * StrataGate namespace (never another namespace).
7329
7394
  */
7330
- getBlockContext(threadId) {
7395
+ getBlockContext(threadId, blockIds) {
7396
+ const timeline = buildBlockTimeline(this.blocks);
7331
7397
  const blocks = threadId === void 0 ? this.blocks.filter((block) => block.processingStatus === "ready") : this.blocks.filter((block) => block.threadId === threadId && block.processingStatus === "ready");
7332
- return blocks.map((block) => {
7333
- const threadBlocks = this.threadBlocks(block.threadId).filter((candidate) => candidate.processingStatus === "ready");
7334
- const latestBlockPosition = threadBlocks.length;
7335
- const blockPosition = threadBlocks.indexOf(block) + 1;
7336
- const age = Math.max(0, latestBlockPosition - blockPosition);
7337
- const level = getDecayedBlockLevel(
7338
- block.pointerAnchorLevel,
7339
- block.pointerAnchorBlockPosition,
7340
- latestBlockPosition,
7341
- this.blockDecayLambda
7342
- );
7398
+ return blocks.filter((block) => !blockIds || blockIds.has(block.id)).map((block) => {
7399
+ const { age, level } = getBlockDecayView(block, timeline.get(block.id), this.blockDecayLambda);
7343
7400
  block.pointerCurrentLevel = level;
7344
7401
  return {
7345
7402
  id: block.id,
@@ -7354,13 +7411,27 @@ var StrataGate = class _StrataGate {
7354
7411
  });
7355
7412
  }
7356
7413
  async setBlockSurfaceLevel(id, level) {
7357
- if (level !== null && (!Number.isInteger(level) || level < 0 || level > 5)) throw new TypeError("Invalid surface level");
7358
- if (this.blocks.find((block) => block.id === id)?.surfaceCurrentLevel === level) return;
7359
- await this.commitMutation(() => {
7360
- const block = this.blocks.find((candidate) => candidate.id === id);
7361
- if (!block) throw new Error(`Unknown block: ${id}`);
7362
- if (block.processingStatus !== "ready") throw new Error(`Block ${id} is not ready for surface tracking`);
7363
- block.surfaceCurrentLevel = level;
7414
+ await this.setBlockSurfaceLevels([{ id, level }]);
7415
+ }
7416
+ /** Record a whole surface pass once; unchanged values never create a snapshot or revision. */
7417
+ async setBlockSurfaceLevels(updates) {
7418
+ const levels = new Map(updates.map(({ id, level }) => [id, level]));
7419
+ if (levels.size === 0) return 0;
7420
+ return this.withMutationQueue(async () => {
7421
+ const byId = new Map(this.blocks.map((block) => [block.id, block]));
7422
+ const changed = [];
7423
+ for (const [id, level] of levels) {
7424
+ if (!Number.isInteger(level) || level < 0 || level > 5) throw new TypeError(`Invalid block surface level: ${level}`);
7425
+ const block = byId.get(id);
7426
+ if (!block) throw new Error(`Unknown block: ${id}`);
7427
+ if (block.processingStatus !== "ready") throw new Error(`Block ${id} is not ready for surface tracking`);
7428
+ if (block.surfaceCurrentLevel !== level) changed.push({ block, level });
7429
+ }
7430
+ if (changed.length === 0) return 0;
7431
+ return this.applyMutation(() => {
7432
+ for (const { block, level } of changed) block.surfaceCurrentLevel = level;
7433
+ return changed.length;
7434
+ });
7364
7435
  });
7365
7436
  }
7366
7437
  async expandBlock(id, target = "next", source = "agent") {
@@ -7368,26 +7439,19 @@ var StrataGate = class _StrataGate {
7368
7439
  const block = this.blocks.find((candidate) => candidate.id === id);
7369
7440
  if (!block) throw new Error(`Unknown block: ${id}`);
7370
7441
  if (block.processingStatus !== "ready") throw new Error(`Block ${id} is not ready for decay or expansion`);
7371
- const readyBlocks = this.threadBlocks(block.threadId).filter((candidate) => candidate.processingStatus === "ready");
7372
- const latestBlockPosition = readyBlocks.length;
7373
- const blockPosition = readyBlocks.indexOf(block) + 1;
7374
- const current = getDecayedBlockLevel(
7375
- block.pointerAnchorLevel,
7376
- block.pointerAnchorBlockPosition,
7377
- latestBlockPosition,
7378
- this.blockDecayLambda
7379
- );
7442
+ const position = buildBlockTimeline(this.threadBlocks(block.threadId)).get(block.id);
7443
+ const { age, level: current } = getBlockDecayView(block, position, this.blockDecayLambda);
7380
7444
  const level = normalizeBlockLevel(target, current);
7381
7445
  block.pointerCurrentLevel = level;
7382
7446
  block.pointerAnchorLevel = level;
7383
- block.pointerAnchorBlockPosition = latestBlockPosition;
7447
+ block.pointerAnchorBlockPosition = position.latestBlockPosition;
7384
7448
  block.lastLiftedAt = toUtc8Iso(this.now());
7385
7449
  block.lastLiftedBy = source;
7386
7450
  return {
7387
7451
  id: block.id,
7388
7452
  ...block.threadId ? { threadId: block.threadId } : {},
7389
7453
  turnRange: [block.startTurn, block.endTurn],
7390
- age: Math.max(0, latestBlockPosition - blockPosition),
7454
+ age,
7391
7455
  level,
7392
7456
  ...block.surfaceCurrentLevel === void 0 ? {} : { surfaceLevel: block.surfaceCurrentLevel },
7393
7457
  label: blockLevelLabel(level),
@@ -7684,7 +7748,7 @@ ${duplicate.summary}` : duplicate.summary;
7684
7748
  processingStatus: "ready",
7685
7749
  ...deterministicBlockLayers([message]),
7686
7750
  pointerCurrentLevel: 5,
7687
- surfaceCurrentLevel: null,
7751
+ surfaceCurrentLevel: 5,
7688
7752
  pointerAnchorLevel: 5,
7689
7753
  pointerAnchorBlockPosition: 1,
7690
7754
  lastLiftedAt: null,
@@ -7722,7 +7786,7 @@ ${duplicate.summary}` : duplicate.summary;
7722
7786
  processingStatus: "ready",
7723
7787
  ...deterministicBlockLayers([message]),
7724
7788
  pointerCurrentLevel: 5,
7725
- surfaceCurrentLevel: null,
7789
+ surfaceCurrentLevel: 5,
7726
7790
  pointerAnchorLevel: 5,
7727
7791
  pointerAnchorBlockPosition: 1,
7728
7792
  lastLiftedAt: null,
@@ -8439,7 +8503,7 @@ ${duplicate.summary}` : duplicate.summary;
8439
8503
  processingStatus: "pending",
8440
8504
  ...deterministic,
8441
8505
  pointerCurrentLevel: 5,
8442
- surfaceCurrentLevel: null,
8506
+ surfaceCurrentLevel: 5,
8443
8507
  pointerAnchorLevel: 5,
8444
8508
  pointerAnchorBlockPosition: blockPosition,
8445
8509
  lastLiftedAt: null,
@@ -8556,11 +8620,9 @@ ${duplicate.summary}` : duplicate.summary;
8556
8620
  if (!block.l0Title || !block.l0Tags || !block.l1Summary || !block.l2Keypoints || typeof block.shouldExtract !== "boolean") {
8557
8621
  throw new Error(`Block ${block.id} cannot become ready without validated L0-L2 layers`);
8558
8622
  }
8559
- const ready = this.threadBlocks(block.threadId).filter((candidate) => candidate.processingStatus === "ready");
8560
8623
  block.processingStatus = "ready";
8561
- block.pointerCurrentLevel = 5;
8562
- block.pointerAnchorLevel = 5;
8563
- block.pointerAnchorBlockPosition = ready.length + 1;
8624
+ const position = buildBlockTimeline(this.threadBlocks(block.threadId)).get(block.id);
8625
+ block.pointerCurrentLevel = getBlockDecayView(block, position, this.blockDecayLambda).level;
8564
8626
  }
8565
8627
  async extractEligibleBlock(options = {}) {
8566
8628
  if (!this.extractor) return null;
@@ -8804,6 +8866,9 @@ ${duplicate.summary}` : duplicate.summary;
8804
8866
  }
8805
8867
  }
8806
8868
  async commitMutation(mutation, graphEpoch) {
8869
+ return this.withMutationQueue(() => this.applyMutation(mutation, graphEpoch));
8870
+ }
8871
+ async withMutationQueue(operation) {
8807
8872
  if (this.storage?.readonly) throw new Error("Cannot mutate read-only StrataGate storage.");
8808
8873
  const previous = this.mutationQueue;
8809
8874
  let release;
@@ -8811,14 +8876,18 @@ ${duplicate.summary}` : duplicate.summary;
8811
8876
  release = resolve4;
8812
8877
  });
8813
8878
  await previous;
8879
+ try {
8880
+ return await operation();
8881
+ } finally {
8882
+ release();
8883
+ }
8884
+ }
8885
+ /** The caller owns mutationQueue, including any no-op preflight. */
8886
+ async applyMutation(mutation, graphEpoch) {
8814
8887
  if (!this.storage) {
8815
- try {
8816
- const result = await mutation();
8817
- this.topicDirectory.synchronize(this.listAllEvents(), toUtc8Iso(this.now()));
8818
- return result;
8819
- } finally {
8820
- release();
8821
- }
8888
+ const result = await mutation();
8889
+ this.topicDirectory.synchronize(this.listAllEvents(), toUtc8Iso(this.now()));
8890
+ return result;
8822
8891
  }
8823
8892
  const before = this.exportSnapshot();
8824
8893
  const beforeRevision = this.revision;
@@ -8837,8 +8906,6 @@ ${duplicate.summary}` : duplicate.summary;
8837
8906
  this.pendingRawDeletes.clear();
8838
8907
  for (const id of beforeRawDeletes) this.pendingRawDeletes.add(id);
8839
8908
  throw error;
8840
- } finally {
8841
- release();
8842
8909
  }
8843
8910
  }
8844
8911
  async persist(before, graphEpoch) {
@@ -12345,6 +12412,11 @@ var StrataGateRuntime = class {
12345
12412
  derivationRuns = /* @__PURE__ */ new Map();
12346
12413
  knownSessions = /* @__PURE__ */ new Map();
12347
12414
  pendingSurfaceSync = /* @__PURE__ */ new Map();
12415
+ surfaceMaintenance = /* @__PURE__ */ new Map();
12416
+ surfaceMaintenanceRuns = /* @__PURE__ */ new Set();
12417
+ surfaceFlushPending = /* @__PURE__ */ new WeakSet();
12418
+ checkpointTokenCache = /* @__PURE__ */ new WeakMap();
12419
+ surfaceAssemblyReaders = /* @__PURE__ */ new WeakMap();
12348
12420
  adminSnapshotCache = /* @__PURE__ */ new Map();
12349
12421
  externalImportRuns = /* @__PURE__ */ new Map();
12350
12422
  dreamingRuns = /* @__PURE__ */ new Map();
@@ -12784,14 +12856,14 @@ var StrataGateRuntime = class {
12784
12856
  const sessionsToSync = /* @__PURE__ */ new Set();
12785
12857
  for (const block of resumed.readyBlocks) {
12786
12858
  if (!block.threadId) continue;
12787
- this.pendingSurfaceSync.set(
12788
- `${namespace}\0${block.id}`,
12789
- { namespace, blockId: block.id }
12790
- );
12859
+ this.queueSurfaceSync(namespace, block.id);
12791
12860
  const session = this.knownSessions.get(block.threadId)?.deref();
12792
12861
  if (session && this.namespaceFor(session) === namespace) sessionsToSync.add(session);
12793
12862
  }
12794
- for (const session of sessionsToSync) await this.syncPendingRetrySurface(session, memory);
12863
+ for (const session of sessionsToSync) {
12864
+ const opening = this.spaces.get(namespace);
12865
+ if (opening) this.markSurfaceDirty(session, await opening);
12866
+ }
12795
12867
  } finally {
12796
12868
  if (owned) await memory.close();
12797
12869
  }
@@ -12846,6 +12918,26 @@ var StrataGateRuntime = class {
12846
12918
  if (this.isDreamingSession(session)) return;
12847
12919
  if (this.closed) return;
12848
12920
  if (!this.config.ingestSubagents && session.header.origin === "subagent") return;
12921
+ const surface = this.surfaceMaintenance.get(String(session.id));
12922
+ if (surface?.session === session) {
12923
+ if (event.type === "turn/start") {
12924
+ surface.busy = true;
12925
+ surface.generation += 1;
12926
+ if (surface.timer) clearTimeout(surface.timer);
12927
+ surface.timer = void 0;
12928
+ } else if (event.type === "turn/end") {
12929
+ surface.busy = false;
12930
+ this.scheduleSurfaceMaintenance(surface);
12931
+ } else if (event.type === "request/context") {
12932
+ const limit = surfaceCheckpointLimit(session);
12933
+ if (surface.checkpointLimit !== limit) {
12934
+ surface.checkpointLimit = limit;
12935
+ this.markSurfaceDirty(session, surface.memory);
12936
+ }
12937
+ } else if (String(event.type).startsWith("compaction/") || event.type === "session/end-seed" || event.type === "user/message" && event.surfaceOp !== void 0 && event.surfaceOp !== "append" && !isStrataGateMessageSource(event.data.source)) {
12938
+ this.markSurfaceDirty(session, surface.memory);
12939
+ }
12940
+ }
12849
12941
  const turn = this.folder.accept(session, event);
12850
12942
  if (!turn) return;
12851
12943
  const key = String(session.id);
@@ -12920,6 +13012,7 @@ var StrataGateRuntime = class {
12920
13012
  for (const turn of turns) {
12921
13013
  const result = await memory.appendTurn(turn, { deferDerivation: true });
12922
13014
  completed += 1;
13015
+ if (result.sealedBlock) this.markSurfaceDirty(session, memory);
12923
13016
  if (result.sealedBlock) this.scheduleBlockDerivation(session, memory);
12924
13017
  }
12925
13018
  } catch (error) {
@@ -13083,8 +13176,7 @@ var StrataGateRuntime = class {
13083
13176
  await this.flush();
13084
13177
  const memory = await this.space(session);
13085
13178
  const threadId = String(session.id);
13086
- const snapshot = this.observeBlockSurface(this.namespaceFor(session), memory.exportSnapshot());
13087
- const actualLevels = new Map(snapshot.blocks.map((block) => [block.id, block.surfaceCurrentLevel ?? null]));
13179
+ const snapshot = memory.exportSnapshot();
13088
13180
  const namespaceBlockCount = snapshot.blocks.length;
13089
13181
  const namespaceThreadIds = [...new Set(snapshot.blocks.map((block) => block.threadId).filter((value) => Boolean(value)))];
13090
13182
  const openTailCount = snapshot.openTail.filter((message) => scope === "namespace" || message.threadId === threadId).length;
@@ -13092,11 +13184,11 @@ var StrataGateRuntime = class {
13092
13184
  const offset = Math.max(0, Math.floor(Number.isFinite(options.offset) ? options.offset : 0));
13093
13185
  const limit = Math.max(1, Math.min(20, Math.floor(Number.isFinite(options.limit) ? options.limit : 12)));
13094
13186
  const blockTitles = new Map(memory.listBlocks().map((block) => [block.id, blockCitationTitle(block)]));
13095
- const results = matches.slice(offset, offset + limit).map(({ id, turnRange, level }) => ({
13187
+ const results = matches.slice(offset, offset + limit).map(({ id, turnRange, level, surfaceLevel }) => ({
13096
13188
  id,
13097
13189
  title: compactText2(blockTitles.get(id) ?? "Block", 240),
13098
13190
  turnRange,
13099
- level: actualLevels.get(id) ?? level,
13191
+ level: surfaceLevel ?? level,
13100
13192
  targetLevel: level
13101
13193
  }));
13102
13194
  const emptyReason = matches.length > 0 ? null : namespaceBlockCount === 0 ? openTailCount > 0 ? "open_tail_pending" : "no_blocks_in_namespace" : openTailCount > 0 ? "open_tail_pending" : "blocks_exist_in_other_threads";
@@ -13119,7 +13211,7 @@ var StrataGateRuntime = class {
13119
13211
  target: {
13120
13212
  eventIds: [],
13121
13213
  elementIds: [],
13122
- citation: citation("block", result.id, blockTitles.get(result.id) ?? "Block", `block:${result.id}:level:${result.level}`, "blockId", result.level === null ? {} : { level: result.level })
13214
+ citation: citation("block", result.id, blockTitles.get(result.id) ?? "Block", `block:${result.id}:level:${result.level}`, "blockId", { level: result.level })
13123
13215
  }
13124
13216
  })), results, status);
13125
13217
  }
@@ -13320,14 +13412,21 @@ var StrataGateRuntime = class {
13320
13412
  if (error !== void 0) throw error;
13321
13413
  }
13322
13414
  async buildAutoContext(session) {
13415
+ this.surfaceAssemblyReaders.set(session, (this.surfaceAssemblyReaders.get(session) ?? 0) + 1);
13416
+ try {
13417
+ return await this.assembleAutoContext(session);
13418
+ } finally {
13419
+ this.surfaceAssemblyReaders.set(session, (this.surfaceAssemblyReaders.get(session) ?? 1) - 1);
13420
+ const state = this.surfaceMaintenance.get(String(session.id));
13421
+ if (state?.session === session) this.scheduleSurfaceMaintenance(state);
13422
+ }
13423
+ }
13424
+ async assembleAutoContext(session) {
13323
13425
  await this.flush();
13324
13426
  const memory = await this.space(session);
13325
13427
  await this.refreshExternalImportMemory(this.namespaceFor(session), memory);
13326
13428
  const threadId = String(session.id);
13327
- const blockContexts = memory.getBlockContext(threadId);
13328
- if (await this.syncDecayedBlockSurface(session, memory, blockContexts)) {
13329
- await this.flushNativeSession(session);
13330
- }
13429
+ await this.syncRequestSurface(session, memory);
13331
13430
  const openTail = memory.listOpenTail(threadId);
13332
13431
  const activationQuery = [currentUserMessage(session), renderMessages(recentTurns(openTail, 2))].filter(Boolean).join("\n\n");
13333
13432
  const eventHits = activationQuery ? await memory.searchEvents(activationQuery, {
@@ -13370,24 +13469,15 @@ var StrataGateRuntime = class {
13370
13469
  * events remain in the append-only log for transcript/evidence provenance,
13371
13470
  * while deriveMessages() sees only this compressed checkpoint.
13372
13471
  */
13373
- surfaceSyncQueues = /* @__PURE__ */ new WeakMap();
13374
- async withSurfaceSync(session, operation) {
13375
- const previous = this.surfaceSyncQueues.get(session) ?? Promise.resolve();
13376
- const pending = previous.catch(() => {
13377
- }).then(operation);
13378
- this.surfaceSyncQueues.set(session, pending);
13379
- try {
13380
- return await pending;
13381
- } finally {
13382
- if (this.surfaceSyncQueues.get(session) === pending) this.surfaceSyncQueues.delete(session);
13383
- }
13384
- }
13385
- replaceSealedSurface(session, memory, block, context, endTurn) {
13386
- return this.withSurfaceSync(session, () => this.replaceSealedSurfaceUnlocked(session, memory, block, context, endTurn));
13472
+ async replaceSealedSurface(session, memory, block, context, endTurn) {
13473
+ const levels = /* @__PURE__ */ new Map();
13474
+ const changed = this.applySealedSurface(session, block, context, endTurn, levels);
13475
+ await this.commitSurfaceLevels(memory, levels);
13476
+ return changed;
13387
13477
  }
13388
- async replaceSealedSurfaceUnlocked(session, memory, block, context, endTurn) {
13478
+ applySealedSurface(session, block, context, endTurn, levels, sources2) {
13389
13479
  if (endTurn === null || hostCompactionActive(session)) return false;
13390
- const sourceEventSeqs = sealedSurfaceSeqs(session, endTurn, block);
13480
+ const sourceEventSeqs = sources2 ?? sealedSurfaceSeqs(session, endTurn, block);
13391
13481
  if (!sourceEventSeqs) return false;
13392
13482
  const start = sourceEventSeqs[0];
13393
13483
  const end = sourceEventSeqs.at(-1);
@@ -13401,94 +13491,263 @@ var StrataGateRuntime = class {
13401
13491
  surfaceOp: dshReplaceSurfaceOp(start, end),
13402
13492
  sourceEventSeqs
13403
13493
  });
13404
- await this.flushNativeSession(session);
13405
- const actual = currentBlockSurfaceMessages(session).get(block.id);
13406
- await memory.setBlockSurfaceLevel(block.id, actual ? checkpointLevel(actual.text) : null);
13494
+ this.surfaceFlushPending.add(session);
13495
+ levels.set(block.id, checkpointLevel(selected) ?? context.level);
13407
13496
  return true;
13408
13497
  }
13409
13498
  /** Keep each native Block checkpoint synchronized with its current decay pointer. */
13410
- syncDecayedBlockSurface(session, memory, contexts) {
13411
- return this.withSurfaceSync(session, () => this.syncDecayedBlockSurfaceUnlocked(session, memory, contexts));
13499
+ async syncDecayedBlockSurface(session, memory, contexts) {
13500
+ const levels = /* @__PURE__ */ new Map();
13501
+ const changed = this.applyDecayedBlockSurface(session, memory, contexts, levels);
13502
+ await this.commitSurfaceLevels(memory, levels);
13503
+ return changed;
13412
13504
  }
13413
- async syncDecayedBlockSurfaceUnlocked(session, memory, contexts) {
13414
- if (!session.surface || typeof session.deriveEventMessage !== "function" || hostCompactionActive(session)) return false;
13505
+ applyDecayedBlockSurface(session, memory, contexts, levels) {
13506
+ if (hostCompactionActive(session)) return false;
13507
+ const current = currentBlockSurfaceMessages(session);
13415
13508
  const blocks = new Map(memory.listBlocks().map((block) => [block.id, block]));
13416
13509
  const checkpointLimit = surfaceCheckpointLimit(session);
13417
13510
  let changed = false;
13418
13511
  for (const context of contexts) {
13419
- if (hostCompactionActive(session)) break;
13512
+ const node = current.get(context.id);
13513
+ if (!node) continue;
13514
+ const currentLevel = checkpointLevel(node.text) ?? -1;
13515
+ const currentTokens = this.cachedCheckpointTokens(session, node);
13516
+ const sourceTokens = originalCheckpointSourceTokens(session, node.seq);
13517
+ const violatesLimit = currentTokens > checkpointLimit || sourceTokens !== null && !worthwhileSurfaceReduction(sourceTokens, currentTokens);
13420
13518
  const block = blocks.get(context.id);
13421
13519
  if (!block) continue;
13422
- const node = currentBlockSurfaceMessages(session).get(context.id);
13423
- if (!node) {
13424
- const endTurn = dshTurnAtBlockEnd(session, block);
13425
- const native = endTurn === null ? null : sealedSurfaceSeqs(session, endTurn, block);
13426
- if (native) {
13427
- await memory.setBlockSurfaceLevel(context.id, 5);
13428
- changed = await this.replaceSealedSurfaceUnlocked(session, memory, block, context, endTurn) || changed;
13429
- } else {
13430
- await memory.setBlockSurfaceLevel(context.id, null);
13431
- }
13520
+ if (currentLevel >= 0) levels.set(context.id, currentLevel);
13521
+ const userLift = block.lastLiftedBy === "user" && currentLevel >= 0 && context.level > currentLevel;
13522
+ if (userLift) {
13523
+ changed = this.applyExpandedSurface(session, block, context, levels) || changed;
13432
13524
  continue;
13433
13525
  }
13434
- const currentLevel = checkpointLevel(node.text);
13435
- if (block.surfaceCurrentLevel !== currentLevel) {
13436
- await this.flushNativeSession(session);
13437
- const observed = currentBlockSurfaceMessages(session).get(context.id);
13438
- await memory.setBlockSurfaceLevel(context.id, observed ? checkpointLevel(observed.text) : null);
13439
- if (observed?.seq !== node.seq) continue;
13440
- }
13441
- if (hostCompactionActive(session)) continue;
13442
- if (currentLevel !== null && context.level >= currentLevel) continue;
13526
+ if (context.level >= currentLevel && currentLevel >= 0 && !violatesLimit) continue;
13527
+ const highestLevel = currentLevel >= 0 ? Math.min(context.level, currentLevel) : context.level;
13443
13528
  const text3 = selectCompressedBlockSurfaceText(
13444
13529
  node.text,
13445
13530
  context,
13446
13531
  block,
13447
- context.level,
13448
- originalCheckpointSourceTokens(session, node.seq),
13532
+ highestLevel,
13533
+ sourceTokens,
13449
13534
  checkpointLimit
13450
13535
  );
13451
- if (!text3 || node.text === text3) continue;
13536
+ if (!text3) continue;
13537
+ if (node.text !== text3) {
13538
+ session.append("user/message", createUserMessage2({
13539
+ content: [{ type: "text", text: text3 }],
13540
+ source: dshMessageSource()
13541
+ }), {
13542
+ surfaceOp: dshReplaceSurfaceOp(node.seq, node.seq),
13543
+ sourceEventSeqs: [node.seq]
13544
+ });
13545
+ this.surfaceFlushPending.add(session);
13546
+ changed = true;
13547
+ }
13548
+ levels.set(context.id, checkpointLevel(text3) ?? currentLevel);
13549
+ }
13550
+ return changed;
13551
+ }
13552
+ queueSurfaceSync(namespace, blockId, reason) {
13553
+ const key = `${namespace}\0${blockId}`;
13554
+ if (!reason && this.pendingSurfaceSync.has(key)) return;
13555
+ this.pendingSurfaceSync.set(key, { namespace, blockId, ...reason ? { reason } : {} });
13556
+ }
13557
+ /** Apply an explicit user request without the automatic decay size-reduction gate. */
13558
+ applyExpandedSurface(session, block, context, levels) {
13559
+ if (hostCompactionActive(session) || block.threadId !== String(session.id)) return false;
13560
+ const text3 = renderBlockSurfaceMessage(context);
13561
+ if (surfaceCheckpointTokens(text3) > surfaceCheckpointLimit(session)) return false;
13562
+ const node = currentBlockSurfaceMessages(session).get(block.id);
13563
+ if (node?.text !== text3) {
13564
+ const endTurn = node ? null : dshTurnAtBlockEnd(session, block);
13565
+ const seqs = node ? [node.seq] : endTurn === null ? null : sealedSurfaceSeqs(session, endTurn, block);
13566
+ if (!seqs) return false;
13567
+ const start = seqs[0];
13568
+ const end = seqs.at(-1);
13569
+ if (start === void 0 || end === void 0) return false;
13452
13570
  session.append("user/message", createUserMessage2({
13453
13571
  content: [{ type: "text", text: text3 }],
13454
13572
  source: dshMessageSource()
13455
- }), {
13456
- surfaceOp: dshReplaceSurfaceOp(node.seq, node.seq),
13457
- sourceEventSeqs: [node.seq]
13458
- });
13459
- await this.flushNativeSession(session);
13460
- const actual = currentBlockSurfaceMessages(session).get(context.id);
13461
- await memory.setBlockSurfaceLevel(context.id, actual ? checkpointLevel(actual.text) : null);
13462
- changed = true;
13573
+ }), { surfaceOp: dshReplaceSurfaceOp(start, end), sourceEventSeqs: seqs });
13574
+ this.surfaceFlushPending.add(session);
13463
13575
  }
13464
- return changed;
13576
+ levels.set(block.id, context.level);
13577
+ return true;
13465
13578
  }
13466
- /** Finish native-surface replacement when an admin retry ran without the target session loaded. */
13467
- async syncPendingRetrySurface(session, memory) {
13468
- if (hostCompactionActive(session)) return;
13579
+ async commitSurfaceLevels(memory, levels) {
13580
+ if (levels.size > 0) await memory.setBlockSurfaceLevels([...levels].map(([id, level]) => ({ id, level })));
13581
+ }
13582
+ async flushSurface(session) {
13583
+ if (!this.surfaceFlushPending.has(session)) return;
13584
+ const seq = session.seq;
13585
+ await this.flushNativeSession(session);
13586
+ if (session.seq === seq) this.surfaceFlushPending.delete(session);
13587
+ }
13588
+ cachedCheckpointTokens(session, node) {
13589
+ let cache = this.checkpointTokenCache.get(session);
13590
+ if (!cache) {
13591
+ cache = /* @__PURE__ */ new Map();
13592
+ this.checkpointTokenCache.set(session, cache);
13593
+ }
13594
+ let tokens = cache.get(node.seq);
13595
+ if (tokens === void 0) {
13596
+ tokens = surfaceCheckpointTokens(node.text);
13597
+ cache.set(node.seq, tokens);
13598
+ }
13599
+ return tokens;
13600
+ }
13601
+ /** Finish deferred sealing or user expansion only after the target surface is applied. */
13602
+ async syncPendingRetrySurface(session, memory, userOnly = false) {
13603
+ const levels = /* @__PURE__ */ new Map();
13604
+ const completed = this.applyPendingRetrySurface(session, memory, levels, userOnly);
13605
+ if (completed.length === 0) return;
13606
+ await this.commitSurfaceLevels(memory, levels);
13607
+ await this.flushSurface(session);
13608
+ this.completeSurfaceRequests(completed);
13609
+ }
13610
+ applyPendingRetrySurface(session, memory, levels, userOnly = false, recoverable) {
13611
+ if (hostCompactionActive(session)) return [];
13469
13612
  const namespace = this.namespaceFor(session);
13470
13613
  const threadId = String(session.id);
13471
- const pending = [...this.pendingSurfaceSync.entries()].filter(([, item]) => item.namespace === namespace);
13472
- if (pending.length === 0) return;
13473
- const contexts = new Map(memory.getBlockContext(threadId).map((context) => [context.id, context]));
13614
+ let pending = [...this.pendingSurfaceSync.entries()].filter(([, item]) => item.namespace === namespace && (!userOnly || item.reason === "user-expansion"));
13615
+ if (pending.length === 0) return [];
13474
13616
  const existing = currentBlockSurfaceMessages(session);
13617
+ const blocks = new Map(memory.listBlocks().filter((block) => block.threadId === threadId).map((block) => [block.id, block]));
13618
+ const automatic = pending.flatMap(([, item]) => {
13619
+ const block = blocks.get(item.blockId);
13620
+ return !item.reason && !existing.has(item.blockId) && block?.processingStatus === "ready" ? [block] : [];
13621
+ });
13622
+ const recovery = recoverable ?? recoverableSealedSurfaceSources(session, automatic);
13623
+ pending = pending.filter(([key, item]) => {
13624
+ const block = blocks.get(item.blockId);
13625
+ if (item.reason || existing.has(item.blockId) || block?.processingStatus !== "ready" || recovery.sources.has(item.blockId)) return true;
13626
+ if (recovery.retired.has(item.blockId) && this.pendingSurfaceSync.get(key) === item) this.pendingSurfaceSync.delete(key);
13627
+ return false;
13628
+ });
13629
+ if (pending.length === 0) return [];
13630
+ const contexts = new Map(memory.getBlockContext(threadId, new Set(pending.map(([, item]) => item.blockId))).map((context) => [context.id, context]));
13475
13631
  let changed = false;
13476
- for (const [key, { blockId }] of pending) {
13477
- if (existing.has(blockId)) {
13478
- this.pendingSurfaceSync.delete(key);
13479
- continue;
13480
- }
13481
- const block = memory.listBlocks().find((candidate) => candidate.id === blockId && candidate.threadId === threadId);
13632
+ const completed = [];
13633
+ for (const [key, item] of pending) {
13634
+ const { blockId } = item;
13635
+ const block = blocks.get(blockId);
13482
13636
  const context = contexts.get(blockId);
13483
13637
  if (!block || block.processingStatus !== "ready" || !context) continue;
13484
- try {
13485
- changed = await this.replaceSealedSurface(session, memory, block, context, dshTurnAtBlockEnd(session, block)) || changed;
13486
- this.pendingSurfaceSync.delete(key);
13487
- } catch (error) {
13488
- this.onIngestError(error);
13638
+ if (item.reason === "user-expansion") {
13639
+ if (!this.applyExpandedSurface(session, block, context, levels)) continue;
13640
+ changed = true;
13641
+ } else if (!existing.has(blockId)) {
13642
+ const source = recovery.sources.get(blockId);
13643
+ if (!this.applySealedSurface(session, block, context, source.endTurn, levels, source.seqs)) continue;
13644
+ changed = true;
13645
+ } else {
13646
+ const level = checkpointLevel(existing.get(blockId).text);
13647
+ if (level !== void 0) levels.set(blockId, level);
13489
13648
  }
13649
+ completed.push([key, item]);
13490
13650
  }
13491
- if (changed) await this.flushNativeSession(session);
13651
+ if (changed) this.surfaceFlushPending.add(session);
13652
+ return completed;
13653
+ }
13654
+ completeSurfaceRequests(completed) {
13655
+ for (const [key, item] of completed) {
13656
+ if (this.pendingSurfaceSync.get(key) === item) this.pendingSurfaceSync.delete(key);
13657
+ }
13658
+ }
13659
+ /** Read-only in the common case; render only explicit lifts or oversized checkpoints. */
13660
+ async syncRequestSurface(session, memory) {
13661
+ if (hostCompactionActive(session)) return;
13662
+ const current = currentBlockSurfaceMessages(session);
13663
+ const limit = surfaceCheckpointLimit(session);
13664
+ const repair = /* @__PURE__ */ new Set();
13665
+ const namespace = this.namespaceFor(session);
13666
+ for (const block of memory.listBlocks()) {
13667
+ if (block.threadId !== String(session.id) || block.processingStatus !== "ready") continue;
13668
+ const node = current.get(block.id);
13669
+ if (node && this.cachedCheckpointTokens(session, node) > limit) repair.add(block.id);
13670
+ if (block.lastLiftedBy === "user" && (!node || block.pointerCurrentLevel > (checkpointLevel(node.text) ?? 5))) {
13671
+ this.queueSurfaceSync(namespace, block.id, "user-expansion");
13672
+ }
13673
+ }
13674
+ const cache = this.checkpointTokenCache.get(session);
13675
+ if (cache && cache.size > current.size + 16) {
13676
+ const live2 = new Set([...current.values()].map(({ seq }) => seq));
13677
+ for (const seq of cache.keys()) if (!live2.has(seq)) cache.delete(seq);
13678
+ }
13679
+ await this.syncPendingRetrySurface(session, memory, true);
13680
+ if (repair.size > 0) {
13681
+ await this.syncDecayedBlockSurface(session, memory, memory.getBlockContext(String(session.id), repair));
13682
+ await this.flushSurface(session);
13683
+ }
13684
+ }
13685
+ markSurfaceDirty(session, memory) {
13686
+ if (this.closed) return;
13687
+ const key = String(session.id);
13688
+ let state = this.surfaceMaintenance.get(key);
13689
+ if (!state || state.session !== session || state.memory !== memory) {
13690
+ if (state?.timer) clearTimeout(state.timer);
13691
+ if (state) state.generation += 1;
13692
+ state = { session, memory, dirty: true, generation: 0, busy: hostTurnActive(session), failures: 0, checkpointLimit: surfaceCheckpointLimit(session) };
13693
+ this.surfaceMaintenance.set(key, state);
13694
+ }
13695
+ state.dirty = true;
13696
+ state.generation += 1;
13697
+ this.scheduleSurfaceMaintenance(state);
13698
+ }
13699
+ scheduleSurfaceMaintenance(state, delay2 = 25) {
13700
+ if (this.closed || this.surfaceMaintenance.get(String(state.session.id)) !== state || !state.dirty || state.busy || state.timer || state.run || this.surfaceAssemblyReaders.get(state.session)) return;
13701
+ state.timer = setTimeout(() => {
13702
+ state.timer = void 0;
13703
+ void this.runSurfaceMaintenance(state.session);
13704
+ }, delay2);
13705
+ state.timer.unref?.();
13706
+ }
13707
+ /** Separate from ingestion: flush() never waits for automatic surface maintenance. */
13708
+ async runSurfaceMaintenance(session) {
13709
+ const state = this.surfaceMaintenance.get(String(session.id));
13710
+ if (!state || state.session !== session || this.closed) return;
13711
+ if (state.run) return state.run;
13712
+ if (state.timer) clearTimeout(state.timer);
13713
+ state.timer = void 0;
13714
+ if (!state.dirty || state.busy || this.surfaceAssemblyReaders.get(session) || hostTurnActive(session) || hostCompactionActive(session)) return;
13715
+ const generation = state.generation;
13716
+ const run = (async () => {
13717
+ await this.drainTails.get(String(session.id));
13718
+ if (this.closed || this.surfaceMaintenance.get(String(session.id)) !== state || state.busy || this.surfaceAssemblyReaders.get(session) || state.generation !== generation || hostTurnActive(session) || hostCompactionActive(session)) return;
13719
+ const levels = /* @__PURE__ */ new Map();
13720
+ const current = currentBlockSurfaceMessages(session);
13721
+ const missing = state.memory.listBlocks().filter((block) => block.threadId === String(session.id) && block.processingStatus === "ready" && !current.has(block.id));
13722
+ const recoverable = recoverableSealedSurfaceSources(session, missing);
13723
+ for (const block of missing) {
13724
+ if (block.lastLiftedBy === "user" || recoverable.sources.has(block.id)) {
13725
+ this.queueSurfaceSync(this.namespaceFor(session), block.id, block.lastLiftedBy === "user" ? "user-expansion" : void 0);
13726
+ }
13727
+ }
13728
+ const completed = this.applyPendingRetrySurface(session, state.memory, levels, false, recoverable);
13729
+ const live2 = new Set(currentBlockSurfaceMessages(session).keys());
13730
+ if (live2.size > 0) this.applyDecayedBlockSurface(session, state.memory, state.memory.getBlockContext(String(session.id), live2), levels);
13731
+ await this.commitSurfaceLevels(state.memory, levels);
13732
+ await this.flushSurface(session);
13733
+ this.completeSurfaceRequests(completed);
13734
+ if (state.generation === generation) state.dirty = false;
13735
+ state.failures = 0;
13736
+ })().catch(async (error) => {
13737
+ state.failures += 1;
13738
+ state.dirty = true;
13739
+ if (error instanceof StorageConflictError) {
13740
+ await state.memory.reloadFromStorage().catch((reloadError) => this.onIngestError(reloadError));
13741
+ }
13742
+ this.onIngestError(error);
13743
+ }).finally(() => {
13744
+ state.run = void 0;
13745
+ this.surfaceMaintenanceRuns.delete(run);
13746
+ if (!hostCompactionActive(session)) this.scheduleSurfaceMaintenance(state, state.failures > 0 ? Math.min(3e4, 1e3 * 2 ** Math.min(state.failures - 1, 5)) : 25);
13747
+ });
13748
+ state.run = run;
13749
+ this.surfaceMaintenanceRuns.add(run);
13750
+ return run;
13492
13751
  }
13493
13752
  // Keep the ingestion error for callers that explicitly require a flushed run.
13494
13753
  async settleIngestion() {
@@ -13512,6 +13771,7 @@ var StrataGateRuntime = class {
13512
13771
  for (const controller of this.dreamingControllers.values()) controller.abort(new Error("\u5BBF\u4E3B\u5DF2\u5173\u95ED\uFF0C\u4EFB\u52A1\u4FDD\u7559\u4E3A\u6682\u505C"));
13513
13772
  this.disposeAdaptersUpdated();
13514
13773
  this.disposeWorkChanged();
13774
+ for (const state of this.surfaceMaintenance.values()) if (state.timer) clearTimeout(state.timer);
13515
13775
  if (this.backgroundWorkerTimer) clearTimeout(this.backgroundWorkerTimer);
13516
13776
  this.backgroundWorkerTimer = void 0;
13517
13777
  for (const timer of this.migrationTimers.values()) clearTimeout(timer);
@@ -13534,6 +13794,8 @@ var StrataGateRuntime = class {
13534
13794
  this.duplicateMaintenance?.close();
13535
13795
  await Promise.allSettled(this.migrationRuns.values());
13536
13796
  await Promise.allSettled(this.derivationRuns.values());
13797
+ await Promise.allSettled(this.surfaceMaintenanceRuns);
13798
+ this.surfaceMaintenance.clear();
13537
13799
  await Promise.allSettled(this.externalImportRuns.values());
13538
13800
  await Promise.allSettled(this.dreamingRuns.values());
13539
13801
  await this.dreamingHost?.close();
@@ -13775,24 +14037,6 @@ var StrataGateRuntime = class {
13775
14037
  await storage.close();
13776
14038
  }
13777
14039
  }
13778
- /** Read-only live-session overlay: a cached database pointer is never proof of an injected layer. */
13779
- observeBlockSurface(namespace, snapshot) {
13780
- const sessions = /* @__PURE__ */ new Map();
13781
- for (const [id, reference] of this.knownSessions) {
13782
- const session = reference.deref();
13783
- if (session?.surface && this.namespaceFor(session) === namespace) sessions.set(id, session);
13784
- }
13785
- if (sessions.size === 0) return snapshot;
13786
- const checkpoints = new Map([...sessions].map(([id, session]) => [id, currentBlockSurfaceMessages(session)]));
13787
- return { ...snapshot, blocks: snapshot.blocks.map((block) => {
13788
- const session = block.threadId ? sessions.get(block.threadId) : void 0;
13789
- if (!session || block.processingStatus !== "ready") return block;
13790
- const checkpoint = checkpoints.get(block.threadId)?.get(block.id);
13791
- const endTurn = checkpoint ? null : dshTurnAtBlockEnd(session, block);
13792
- const surfaceCurrentLevel = checkpoint ? checkpointLevel(checkpoint.text) : endTurn !== null && sealedSurfaceSeqs(session, endTurn, block) ? 5 : null;
13793
- return { ...block, surfaceCurrentLevel };
13794
- }) };
13795
- }
13796
14040
  async adminSnapshotEntries() {
13797
14041
  if (this.config.database === ":memory:" || !existsSync2(this.config.database)) return [];
13798
14042
  const storage = new SqliteStorage({ filename: this.config.database, readonly: true });
@@ -13825,7 +14069,7 @@ var StrataGateRuntime = class {
13825
14069
  for (const namespace of this.adminSnapshotCache.keys()) {
13826
14070
  if (!activeNamespaces.has(namespace)) this.adminSnapshotCache.delete(namespace);
13827
14071
  }
13828
- return entries.map((entry) => ({ ...entry, snapshot: this.observeBlockSurface(entry.namespace, entry.snapshot) }));
14072
+ return entries;
13829
14073
  } finally {
13830
14074
  await storage.close();
13831
14075
  }
@@ -13865,22 +14109,22 @@ var StrataGateRuntime = class {
13865
14109
  await this.refreshExternalImportMemory(key, memory);
13866
14110
  const revision = memory.storageRevision;
13867
14111
  const cached = this.adminSnapshotCache.get(key);
13868
- if (cached?.revision === revision) return this.observeBlockSurface(key, cached.snapshot);
14112
+ if (cached?.revision === revision) return cached.snapshot;
13869
14113
  const entry = { namespace: key, revision, snapshot: memory.exportSnapshot() };
13870
14114
  this.adminSnapshotCache.set(key, entry);
13871
- return this.observeBlockSurface(key, entry.snapshot);
14115
+ return entry.snapshot;
13872
14116
  }
13873
14117
  const storage = new SqliteStorage({ filename: this.config.database, readonly: true });
13874
14118
  try {
13875
14119
  const head = storage.listNamespaceRevisions().find(({ namespace: namespace2 }) => namespace2 === key);
13876
14120
  if (!head) return null;
13877
14121
  const cached = this.adminSnapshotCache.get(key);
13878
- if (cached?.revision === head.revision) return this.observeBlockSurface(key, cached.snapshot);
14122
+ if (cached?.revision === head.revision) return cached.snapshot;
13879
14123
  const loaded = await storage.load(key);
13880
14124
  if (!loaded) return null;
13881
14125
  const entry = { namespace: key, revision: loaded.revision, snapshot: loaded.snapshot };
13882
14126
  this.adminSnapshotCache.set(key, entry);
13883
- return this.observeBlockSurface(key, entry.snapshot);
14127
+ return entry.snapshot;
13884
14128
  } finally {
13885
14129
  await storage.close();
13886
14130
  }
@@ -14393,27 +14637,22 @@ var StrataGateRuntime = class {
14393
14637
  const update = this.settingsTail.catch(() => {
14394
14638
  }).then(async () => {
14395
14639
  await this.flush();
14396
- const active = this.spaces.get(key);
14397
- if (active) return (await active).expandBlock(id, target, "user");
14398
- if (this.config.database === ":memory:" || !existsSync2(this.config.database)) {
14399
- throw new Error(`Unknown StrataGate namespace: ${key}`);
14400
- }
14401
- const memory = await StrataGate.open({
14402
- database: this.config.database,
14403
- namespace: key,
14404
- graphPolicy: () => this.graphPolicyFor(key),
14405
- blockTurnSize: this.blockTurnSize,
14406
- blockDecayLambda: this.blockDecayLambda,
14407
- summarizer: this.models.summarizer,
14408
- extractor: this.models.extractor,
14409
- ...typeof this.models.eventDuplicateDecider === "function" ? { eventDuplicateDecider: this.models.eventDuplicateDecider } : {},
14410
- graphProjector: this.models.graphProjector,
14411
- disableElementProjection: true
14412
- });
14640
+ const { memory, owned } = await this.openAdminMemory(key);
14413
14641
  try {
14414
- return await memory.expandBlock(id, target, "user");
14642
+ const result = await memory.expandBlock(id, target, "user");
14643
+ if (result.threadId) {
14644
+ this.queueSurfaceSync(key, id, "user-expansion");
14645
+ }
14646
+ const session = result.threadId ? this.knownSessions.get(result.threadId)?.deref() : void 0;
14647
+ if (session && this.namespaceFor(session) === key) {
14648
+ await this.syncPendingRetrySurface(session, memory);
14649
+ if (this.pendingSurfaceSync.has(`${key}\0${id}`)) {
14650
+ throw new Error(`\u65E0\u6CD5\u5C06 Block ${id} \u7684\u5F53\u524D\u4F1A\u8BDD\u4E0A\u4E0B\u6587\u5C55\u5F00\u5230 L${result.level}\uFF1B\u76EE\u6807\u5185\u5BB9\u8D85\u8FC7\u5F53\u524D\u4E0A\u4E0B\u6587\u5BB9\u91CF\u6216\u4F1A\u8BDD\u6B63\u5728\u6574\u7406`);
14651
+ }
14652
+ }
14653
+ return { ...result, surfaceLevel: memory.listBlocks().find(({ id: blockId }) => blockId === id)?.surfaceCurrentLevel };
14415
14654
  } finally {
14416
- await memory.close();
14655
+ if (owned) await memory.close();
14417
14656
  }
14418
14657
  });
14419
14658
  this.settingsTail = update.then(() => {
@@ -14457,7 +14696,7 @@ var StrataGateRuntime = class {
14457
14696
  await this.persistSuccessfulResponses(memory);
14458
14697
  const currentBlock = blockId ? memory.listBlocks().find(({ id: candidateId }) => candidateId === blockId) : void 0;
14459
14698
  if (kind !== "graph-projection" && currentBlock?.processingStatus === "ready" && currentBlock.threadId) {
14460
- this.pendingSurfaceSync.set(`${key}\0${currentBlock.id}`, { namespace: key, blockId: currentBlock.id });
14699
+ this.queueSurfaceSync(key, currentBlock.id);
14461
14700
  if (session && this.namespaceFor(session) === key) await this.syncPendingRetrySurface(session, memory);
14462
14701
  }
14463
14702
  const job = kind === "block-summary" ? memory.listSummaryJobs().find(({ blockId: candidateId }) => candidateId === jobId) : kind === "event-extraction" ? memory.listExtractionJobs().find(({ blockId: candidateId }) => candidateId === jobId) : memory.listGraphProjectionJobs().find((candidate) => graphJobIdsBeforeRetry !== void 0 && !graphJobIdsBeforeRetry.has(candidate.id)) ?? memory.listGraphProjectionJobs().find(({ id: candidateId }) => candidateId === jobId);
@@ -14527,6 +14766,7 @@ var StrataGateRuntime = class {
14527
14766
  for (const [namespace, opening] of this.spaces) {
14528
14767
  const memory = await opening;
14529
14768
  await memory.setBlockDecayLambda(value);
14769
+ for (const state of this.surfaceMaintenance.values()) if (state.memory === memory) this.markSurfaceDirty(state.session, memory);
14530
14770
  openNamespaces.add(namespace);
14531
14771
  }
14532
14772
  if (this.config.database !== ":memory:" && existsSync2(this.config.database)) {
@@ -14596,15 +14836,12 @@ var StrataGateRuntime = class {
14596
14836
  try {
14597
14837
  try {
14598
14838
  await memory2.resumePendingWork({ deferDerivation: true, threadId: String(session.id) });
14599
- const contexts = memory2.getBlockContext(String(session.id));
14600
- if (await this.syncDecayedBlockSurface(session, memory2, contexts)) {
14601
- await this.flushNativeSession(session);
14602
- }
14603
14839
  } finally {
14604
14840
  await this.persistSuccessfulResponses(memory2);
14605
14841
  }
14606
14842
  this.scheduleGraphMigration(session, memory2);
14607
14843
  this.scheduleBlockDerivation(session, memory2);
14844
+ this.markSurfaceDirty(session, memory2);
14608
14845
  return memory2;
14609
14846
  } catch (error) {
14610
14847
  await memory2.close().catch(() => {
@@ -14618,7 +14855,8 @@ var StrataGateRuntime = class {
14618
14855
  });
14619
14856
  }
14620
14857
  const memory = await opening;
14621
- await this.syncPendingRetrySurface(session, memory);
14858
+ if (this.surfaceMaintenance.get(String(session.id))?.session !== session) this.markSurfaceDirty(session, memory);
14859
+ await this.syncPendingRetrySurface(session, memory, true);
14622
14860
  return memory;
14623
14861
  }
14624
14862
  async searchGraph(session, query, limit = 8) {
@@ -14700,23 +14938,11 @@ var StrataGateRuntime = class {
14700
14938
  const run = this.models.run(session, () => memory.resumePendingWork({ threadId })).then(async (resumed) => {
14701
14939
  await this.persistSuccessfulResponses(memory);
14702
14940
  this.noteNewCoreJobFailures(session, failedBefore, memory);
14703
- const contexts = memory.getBlockContext(threadId);
14704
- let changed = false;
14705
14941
  for (const block of resumed.readyBlocks) {
14706
14942
  if (block.threadId !== threadId) continue;
14707
- const context = contexts.find(({ id }) => id === block.id);
14708
- if (!context) throw new Error(`Missing context for ready StrataGate block ${block.id}`);
14709
- if (hostCompactionActive(session)) {
14710
- this.pendingSurfaceSync.set(`${this.namespaceFor(session)}\0${block.id}`, {
14711
- namespace: this.namespaceFor(session),
14712
- blockId: block.id
14713
- });
14714
- continue;
14715
- }
14716
- changed = await this.replaceSealedSurface(session, memory, block, context, dshTurnAtBlockEnd(session, block)) || changed;
14943
+ this.queueSurfaceSync(this.namespaceFor(session), block.id);
14717
14944
  }
14718
- changed = await this.syncDecayedBlockSurface(session, memory, contexts) || changed;
14719
- if (changed) await this.flushNativeSession(session);
14945
+ if (resumed.readyBlocks.some((block) => block.threadId === threadId)) this.markSurfaceDirty(session, memory);
14720
14946
  }).catch(async (error) => {
14721
14947
  retryDelay = BACKGROUND_WORKER_INTERVAL_MS;
14722
14948
  if (error instanceof StorageConflictError) {
@@ -14936,6 +15162,55 @@ function dshTurnAtBlockEnd(session, block) {
14936
15162
  }
14937
15163
  return null;
14938
15164
  }
15165
+ function recoverableSealedSurfaceSources(session, blocks) {
15166
+ const result = { sources: /* @__PURE__ */ new Map(), retired: /* @__PURE__ */ new Set() };
15167
+ if (blocks.length === 0 || !session.surface?.nodes) return result;
15168
+ const visible2 = new Set(session.surface.nodes);
15169
+ for (const seq of session.surface.nodes) {
15170
+ const event = session.eventAt(seq);
15171
+ if (event?.type === "tool/result" && event.surfaceOp !== "append") {
15172
+ for (const source of event.sourceEventSeqs ?? []) visible2.add(source);
15173
+ }
15174
+ }
15175
+ const ends = /* @__PURE__ */ new Map();
15176
+ const visibleEnds = /* @__PURE__ */ new Set();
15177
+ const compactedBlocks = /* @__PURE__ */ new Set();
15178
+ let open2;
15179
+ for (const event of session.snapshotEvents()) {
15180
+ const hostEvent = event;
15181
+ if (hostEvent.type === "compaction/summary") {
15182
+ for (const seq of hostEvent.data.shadowedSeqs ?? []) {
15183
+ const checkpoint = blockSurfaceMessage(session.eventAt(seq));
15184
+ if (checkpoint) compactedBlocks.add(checkpoint.id);
15185
+ }
15186
+ }
15187
+ if (event.type === "turn/start") open2 = { turn: event.data.turn, visible: false };
15188
+ else if (event.type === "turn/end") {
15189
+ if (open2?.turn === event.data.turn) {
15190
+ ends.set(event.time, event.data.turn);
15191
+ if (open2.visible) visibleEnds.add(event.time);
15192
+ }
15193
+ open2 = void 0;
15194
+ } else if (open2 && visible2.has(event.seq)) open2.visible = true;
15195
+ }
15196
+ for (const block of blocks) {
15197
+ if (compactedBlocks.has(block.id)) {
15198
+ result.retired.add(block.id);
15199
+ continue;
15200
+ }
15201
+ const endTime = Date.parse(block.createdAt);
15202
+ const endTurn = ends.get(endTime);
15203
+ if (endTurn === void 0) continue;
15204
+ if (!visibleEnds.has(endTime)) {
15205
+ result.retired.add(block.id);
15206
+ continue;
15207
+ }
15208
+ const seqs = sealedSurfaceSeqs(session, endTurn, block);
15209
+ if (seqs?.length) result.sources.set(block.id, { endTurn, seqs });
15210
+ else result.retired.add(block.id);
15211
+ }
15212
+ return result;
15213
+ }
14939
15214
  function sealedSurfaceSeqs(session, endTurn, block) {
14940
15215
  const currentSurface = [...session.surface.nodes];
14941
15216
  const completed = [];
@@ -14983,6 +15258,18 @@ function sealedSurfaceSeqs(session, endTurn, block) {
14983
15258
  if (startIndex < 0 || endIndex < startIndex) return null;
14984
15259
  return currentSurface.slice(startIndex, endIndex + 1);
14985
15260
  }
15261
+ function checkpointLevel(text3) {
15262
+ const match = text3.match(/(?:^|\n)Block: [^|\n]+ \| Turns: \d+-\d+ \| Level: L([0-5])(?:\s|$)/mu);
15263
+ return match ? Number(match[1]) : void 0;
15264
+ }
15265
+ function hostTurnActive(session) {
15266
+ for (let seq = session.seq - 1; seq >= 0; seq -= 1) {
15267
+ const event = session.eventAt(seq);
15268
+ if (event?.type === "turn/start") return true;
15269
+ if (event?.type === "turn/end" || event?.type === "session/end-seed") return false;
15270
+ }
15271
+ return false;
15272
+ }
14986
15273
  function hostCompactionActive(session) {
14987
15274
  let lastBoundary;
14988
15275
  let lastSeed;
@@ -15035,36 +15322,63 @@ function worthwhileSurfaceReduction(before, after) {
15035
15322
  }
15036
15323
  function selectCompressedBlockSurfaceText(currentText, context, block, highestLevel, sourceTokens, checkpointLimit) {
15037
15324
  const before = surfaceCheckpointTokens(currentText);
15038
- const candidate = renderBlockSurfaceMessage({
15039
- ...context,
15040
- level: highestLevel,
15041
- label: blockLevelLabel(highestLevel),
15042
- content: highestLevel === context.level ? context.content : renderBlock(block, highestLevel)
15043
- });
15044
- const tokens = surfaceCheckpointTokens(candidate);
15045
- return tokens <= checkpointLimit && worthwhileSurfaceReduction(before, tokens) && (sourceTokens === null || worthwhileSurfaceReduction(sourceTokens, tokens)) ? candidate : null;
15325
+ const level = highestLevel;
15326
+ const renderCandidate = (candidateLevel) => {
15327
+ const text3 = renderBlockSurfaceMessage({
15328
+ ...context,
15329
+ level: candidateLevel,
15330
+ label: blockLevelLabel(candidateLevel),
15331
+ content: candidateLevel === context.level ? context.content : renderBlock(block, candidateLevel)
15332
+ });
15333
+ return { text: text3, tokens: surfaceCheckpointTokens(text3) };
15334
+ };
15335
+ const target = renderCandidate(level);
15336
+ if (target.tokens <= checkpointLimit) {
15337
+ if (worthwhileSurfaceReduction(before, target.tokens) && (sourceTokens === null || worthwhileSurfaceReduction(sourceTokens, target.tokens))) return target.text;
15338
+ return null;
15339
+ }
15340
+ for (let candidateLevel = level - 1; candidateLevel >= 0; candidateLevel -= 1) {
15341
+ const candidate = renderCandidate(candidateLevel);
15342
+ if (candidate.tokens > checkpointLimit) continue;
15343
+ if (worthwhileSurfaceReduction(before, candidate.tokens) && (sourceTokens === null || worthwhileSurfaceReduction(sourceTokens, candidate.tokens))) return candidate.text;
15344
+ }
15345
+ return null;
15046
15346
  }
15047
- function selectCompressedBlockSurface(session, sourceSeqs, _block, context) {
15347
+ function selectCompressedBlockSurface(session, sourceSeqs, block, context) {
15048
15348
  const before = sourceSeqs.reduce((sum, seq) => {
15049
15349
  const event = session.eventAt(seq);
15050
15350
  const message = event && session.deriveEventMessage(event);
15051
15351
  return sum + (message ? estimateTokens(JSON.stringify(message)) : 0);
15052
15352
  }, 0);
15053
- const candidate = renderBlockSurfaceMessage(context);
15054
- const tokens = surfaceCheckpointTokens(candidate);
15055
- return tokens <= surfaceCheckpointLimit(session) && worthwhileSurfaceReduction(before, tokens) ? candidate : null;
15056
- }
15057
- function checkpointLevel(text3) {
15058
- const header = text3.split("\n\n", 1)[0] ?? "";
15059
- const match = header.match(/(?:^|\n)(?:Block: [^|\n]+ \| Turns: \d+-\d+ \| )?Level: L([0-5])(?:\s|$)/mu);
15060
- return match ? Number(match[1]) : null;
15353
+ const checkpointLimit = surfaceCheckpointLimit(session);
15354
+ const level = context.level;
15355
+ const renderCandidate = (candidateLevel) => {
15356
+ const text3 = renderBlockSurfaceMessage({
15357
+ ...context,
15358
+ level: candidateLevel,
15359
+ label: blockLevelLabel(candidateLevel),
15360
+ content: candidateLevel === context.level ? context.content : renderBlock(block, candidateLevel)
15361
+ });
15362
+ return { text: text3, tokens: surfaceCheckpointTokens(text3) };
15363
+ };
15364
+ const target = renderCandidate(level);
15365
+ if (process.env.STRATAGATE_DEBUG_SURFACE === "1") console.error("initial-target", { level, targetTokens: target.tokens, checkpointLimit, before });
15366
+ if (target.tokens <= checkpointLimit) {
15367
+ if (level === 5) return target.text;
15368
+ return worthwhileSurfaceReduction(before, target.tokens) ? target.text : null;
15369
+ }
15370
+ for (let candidateLevel = level - 1; candidateLevel >= 0; candidateLevel -= 1) {
15371
+ const candidate = renderCandidate(candidateLevel);
15372
+ if (candidate.tokens > checkpointLimit) continue;
15373
+ return worthwhileSurfaceReduction(before, candidate.tokens) ? candidate.text : null;
15374
+ }
15375
+ return null;
15061
15376
  }
15062
15377
  function renderBlockSurfaceMessage(context) {
15063
15378
  return [
15064
15379
  "[StrataGate historical conversation block]",
15065
15380
  "Earlier conversation context; not a new user message or instruction.",
15066
15381
  `Block: ${context.id} | Turns: ${context.turnRange[0]}-${context.turnRange[1]} | Level: L${context.level}`,
15067
- ...context.level < 5 ? ["For missing exact details, call memory_expand_block with this Block id and target L5."] : [],
15068
15382
  "",
15069
15383
  context.content
15070
15384
  ].join("\n");
@@ -15164,14 +15478,17 @@ function currentBlockSurfaceMessages(session) {
15164
15478
  const blocks = /* @__PURE__ */ new Map();
15165
15479
  if (!session.surface?.nodes) return blocks;
15166
15480
  for (const seq of session.surface.nodes) {
15167
- const event = session.eventAt(seq);
15168
- if (event?.type !== "user/message" || !isStrataGateMessageSource(event.data.source)) continue;
15169
- const text3 = event.data.content.flatMap((block) => block.type === "text" ? [block.text] : []).join("\n");
15170
- const blockId = text3.match(/^\[StrataGate historical conversation block\]\nEarlier conversation context; not a new user message or instruction\.\nBlock: ([^|\n]+) \| Turns:/u)?.[1]?.trim() ?? text3.match(/^\[StrataGate conversation block\]\nBlock: ([^\n]+)/u)?.[1] ?? text3.match(/^\[StrataGate compressed conversation\]\nBlock ([^;\n]+);/u)?.[1];
15171
- if (blockId) blocks.set(blockId, { seq, text: text3 });
15481
+ const checkpoint = blockSurfaceMessage(session.eventAt(seq));
15482
+ if (checkpoint) blocks.set(checkpoint.id, { seq, text: checkpoint.text });
15172
15483
  }
15173
15484
  return blocks;
15174
15485
  }
15486
+ function blockSurfaceMessage(event) {
15487
+ if (event?.type !== "user/message" || !isStrataGateMessageSource(event.data.source)) return void 0;
15488
+ const text3 = event.data.content.flatMap((block) => block.type === "text" ? [block.text] : []).join("\n");
15489
+ const blockId = text3.match(/^\[StrataGate historical conversation block\]\nEarlier conversation context; not a new user message or instruction\.\nBlock: ([^|\n]+) \| Turns:/u)?.[1]?.trim() ?? text3.match(/^\[StrataGate conversation block\]\nBlock: ([^\n]+)/u)?.[1] ?? text3.match(/^\[StrataGate compressed conversation\]\nBlock ([^;\n]+);/u)?.[1];
15490
+ return blockId ? { id: blockId, text: text3 } : void 0;
15491
+ }
15175
15492
  function activatedEvents(memory, relevance) {
15176
15493
  const allowed = new Map(relevance.map(({ event }) => [event.id, event]));
15177
15494
  for (const event of memory.listAllEvents()) {
@@ -15834,7 +16151,7 @@ function registerMemoryTools(ctx, runtime) {
15834
16151
  }));
15835
16152
  ctx.tools.register(defineTool2({
15836
16153
  name: "memory_get_blocks",
15837
- description: "This tool is provided by the StrataGate plugin. Browse a paginated block directory containing id, title, turnRange, the layer actually injected into the model, and the separate decay targetLevel. Use memory_expand_block for source content. Defaults to blocks with the active threadId only; use scope=namespace to browse every thread and legacy unthreaded blocks in the current namespace. Reports counts, offset, limit, nextOffset, and emptyReason; use nextOffset to continue.",
16154
+ description: "This tool is provided by the StrataGate plugin. Browse a paginated block directory containing id, title, turnRange, the layer actually injected into the model, and targetLevel when the decay target differs. Use memory_expand_block for source content. Defaults to blocks with the active threadId only; use scope=namespace to browse every thread and legacy unthreaded blocks in the current namespace. Reports counts, offset, limit, nextOffset, and emptyReason; use nextOffset to continue.",
15838
16155
  parameters: {
15839
16156
  scope: { type: "string", enum: ["session", "namespace"], description: "Query range. Defaults to session to preserve existing isolation behavior." },
15840
16157
  offset: { type: "integer", description: "\u4ECE\u7B2C\u51E0\u9879\u5F00\u59CB\uFF0C\u4F7F\u7528\u8FD4\u56DE\u7684 nextOffset \u7EE7\u7EED\u3002" },
@@ -15874,7 +16191,11 @@ function registerMemoryTools(ctx, runtime) {
15874
16191
  parameters: {
15875
16192
  batch_id: { type: "string", description: "The batchId returned by the retrieval to assess. Omit only in a strictly sequential flow." },
15876
16193
  verdict: { type: "string", enum: ["sufficient", "partial", "wrong"], required: true },
15877
- evidence_refs: { type: "array", items: { type: "string" }, required: true },
16194
+ evidence_refs: {
16195
+ oneOf: [{ type: "array", items: { type: "string" } }, { type: "null" }],
16196
+ required: true,
16197
+ description: "Evidence refs adopted from this batch. Use [] when none are adopted; null is also accepted as []."
16198
+ },
15878
16199
  fit: { type: "string", required: true },
15879
16200
  missing: { type: "string", required: true },
15880
16201
  next_strategy: {
@@ -15884,7 +16205,7 @@ function registerMemoryTools(ctx, runtime) {
15884
16205
  }
15885
16206
  },
15886
16207
  output: jsonOutput,
15887
- execute: async (args, exec) => runtime.assess(sessionOf(exec), args, args.batch_id)
16208
+ execute: async (args, exec) => runtime.assess(sessionOf(exec), { ...args, evidence_refs: args.evidence_refs ?? [] }, args.batch_id)
15888
16209
  }));
15889
16210
  assessmentGraphEnabled = enabled;
15890
16211
  };
@@ -15894,13 +16215,17 @@ function registerMemoryTools(ctx, runtime) {
15894
16215
  description: "This tool is provided by the StrataGate plugin. Close one StrataGate retrieval batch BEFORE outputting the final user answer. Complete memory_assess / memory_record_use as pre-answer evidence processing for ALL batches, then generate the final user answer. Pass its batch_id and exactly the evidenceRefs from that batch actually adopted for the answer, or [] when none are used. Non-empty refs require that batch's sufficient assessment. Never write the answer first and record usage afterwards. Do not turn this tool's internal receipt or batch status into a user-facing answer. The host renders successful selections as answer-tail citations, so do not write a manual citation list. Omitting batch_id selects the latest batch for sequential compatibility.",
15895
16216
  parameters: {
15896
16217
  batch_id: { type: "string", description: "The batchId to close. Omit only in a strictly sequential flow." },
15897
- evidence_refs: { type: "array", items: { type: "string" }, required: true }
16218
+ evidence_refs: {
16219
+ oneOf: [{ type: "array", items: { type: "string" } }, { type: "null" }],
16220
+ required: true,
16221
+ description: "Evidence refs actually used in the answer. Use [] when none are used; null is also accepted as []."
16222
+ }
15898
16223
  },
15899
16224
  output: jsonOutput,
15900
16225
  execute: async (args, exec) => runtime.recordUse(
15901
16226
  sessionOf(exec),
15902
16227
  String(exec.callId),
15903
- args.evidence_refs,
16228
+ args.evidence_refs ?? [],
15904
16229
  args.batch_id
15905
16230
  )
15906
16231
  }));
@@ -17120,7 +17445,12 @@ async function memories(runtime, url) {
17120
17445
  const requestedThreadId = url.searchParams.get("threadId")?.trim() ?? "";
17121
17446
  const activeThreadId = requestedThreadId || conversations[0]?.id || null;
17122
17447
  const scopedBlocks = activeThreadId ? recovered.blocks.filter((block) => block.threadId === activeThreadId) : [];
17123
- const readySources = snapshot.blocks.filter((block) => block.processingStatus === "ready");
17448
+ const timeline = buildBlockTimeline(scopedBlocks.map((block) => ({
17449
+ id: block.id,
17450
+ threadId: block.threadId,
17451
+ sequence: block.source.sequence
17452
+ })));
17453
+ const sourceTimeline = buildBlockTimeline(snapshot.blocks);
17124
17454
  values = scopedBlocks.map((block) => {
17125
17455
  const source = block.source;
17126
17456
  const layers = virtualBlockLayers(block);
@@ -17138,15 +17468,13 @@ async function memories(runtime, url) {
17138
17468
  const summaryState = summary ? derivationJobState(summary, summary.status === "succeeded") : null;
17139
17469
  const extractionState = extraction ? derivationJobState(extraction, extraction.status === "succeeded" || extraction.status === "skipped") : null;
17140
17470
  const status = summaryState === "terminal-failed" ? "failed" : summaryState === "processing" || summaryState === "retryable" || !summary && source.processingStatus === "pending" ? "processing" : extractionState === "terminal-failed" || failedProjection ? "failed" : extractionState === "processing" || extractionState === "retryable" ? "processing" : extraction?.status === "succeeded" || extraction?.status === "skipped" ? pendingProjection ? "processing" : "organized" : needsExtraction ? "waiting" : "organized";
17141
- const blockPosition = scopedBlocks.findIndex(({ id }) => id === block.id) + 1;
17142
- const latestBlockPosition = source.threadId ? readySources.filter((block2) => block2.threadId === source.threadId).length : scopedBlocks.filter((block2) => block2.source.processingStatus === "ready").length;
17143
- const targetLevel = getDecayedBlockLevel(
17144
- source.pointerAnchorLevel,
17145
- source.threadId ? source.pointerAnchorBlockPosition : Math.min(source.pointerAnchorBlockPosition, blockPosition),
17146
- latestBlockPosition,
17147
- snapshot.blockDecayLambda
17148
- );
17149
- const currentLevel = source.surfaceCurrentLevel ?? source.pointerCurrentLevel;
17471
+ const position = block.virtual ? timeline.get(block.id) : sourceTimeline.get(source.id);
17472
+ const { blockPosition } = position;
17473
+ const { age, level: targetLevel } = getBlockDecayView({
17474
+ pointerAnchorLevel: source.pointerAnchorLevel,
17475
+ pointerAnchorBlockPosition: block.virtual ? Math.min(source.pointerAnchorBlockPosition, blockPosition) : source.pointerAnchorBlockPosition
17476
+ }, position, snapshot.blockDecayLambda);
17477
+ const currentLevel = source.surfaceCurrentLevel ?? targetLevel;
17150
17478
  const currentMetrics = layerTokens.find(({ level }) => level === currentLevel);
17151
17479
  const l5Tokens = layerTokens.find(({ level }) => level === 5)?.tokens ?? 0;
17152
17480
  return {
@@ -17162,12 +17490,11 @@ async function memories(runtime, url) {
17162
17490
  keypoints: source.l2Keypoints,
17163
17491
  currentLevel,
17164
17492
  targetLevel,
17165
- currentTokens: currentMetrics?.tokens ?? null,
17493
+ currentTokens: currentMetrics?.tokens ?? 0,
17166
17494
  l5Tokens,
17167
- compressionPercent: currentMetrics?.percentOfL5 ?? null,
17168
- surfaceLevelSource: source.surfaceCurrentLevel == null ? "legacy-estimate" : "observed",
17495
+ compressionPercent: currentMetrics?.percentOfL5 ?? (currentLevel === 5 ? 100 : 0),
17169
17496
  layerTokens,
17170
- distanceFromLatest: Math.max(0, latestBlockPosition - blockPosition),
17497
+ distanceFromLatest: age,
17171
17498
  expansionSource: source.lastLiftedAt ? source.lastLiftedBy ?? "legacy" : null,
17172
17499
  lastLiftedAt: source.lastLiftedAt,
17173
17500
  sourceMessages: block.messages.length,
@@ -17419,8 +17746,7 @@ async function dashboard(runtime, url, ifNoneMatch) {
17419
17746
  const revisionKey = entries.map(({ namespace, revision }) => `${namespace}:${revision}`).join("|");
17420
17747
  const agentMemoryWeight = runtime.adminAgentMemoryRetrievalWeight?.() ?? 1;
17421
17748
  const duplicateKey = JSON.stringify([entries.map(({ namespace }) => runtime.adminDuplicateRepairStatus?.(namespace) ?? null), runtime.adminGraphSettings?.()]);
17422
- const surfaceKey = JSON.stringify(entries.map(({ snapshot }) => snapshot.blocks.map(({ id, surfaceCurrentLevel }) => [id, surfaceCurrentLevel ?? null])));
17423
- const etag = `"${createHash6("sha256").update(`${STRATAGATE_DSH_VERSION}\0${TOPIC_DIRECTORY_VIEW_VERSION}\0${surfaceKey}\0${revisionKey}\0${selected?.namespace ?? ""}\0${threadId}\0${agentMemoryWeight}\0${duplicateKey}`).digest("base64url").slice(0, 24)}"`;
17749
+ const etag = `"${createHash6("sha256").update(`${STRATAGATE_DSH_VERSION}\0${TOPIC_DIRECTORY_VIEW_VERSION}\0${revisionKey}\0${selected?.namespace ?? ""}\0${threadId}\0${agentMemoryWeight}\0${duplicateKey}`).digest("base64url").slice(0, 24)}"`;
17424
17750
  if (ifNoneMatch.split(",").map((value) => value.trim()).includes(etag)) return { etag, notModified: true };
17425
17751
  const overviewValue = await overview(runtime, entries);
17426
17752
  if (!selected) {
@@ -18124,7 +18450,8 @@ async function apply(ctx, config) {
18124
18450
  structuredReasoningEffort: resolved.structuredReasoningEffort ?? "auto",
18125
18451
  showStrataGateStatus: resolved.showStrataGateStatus ?? true,
18126
18452
  showShortTermStatus: resolved.showShortTermStatus ?? true,
18127
- showRetrievalStatus: resolved.showRetrievalStatus ?? true
18453
+ showRetrievalStatus: resolved.showRetrievalStatus ?? true,
18454
+ showSidebarEntry: resolved.showSidebarEntry ?? false
18128
18455
  };
18129
18456
  let effortSource = () => effortEntry;
18130
18457
  ctx.inject(["settings"], (settingsCtx) => {