stratagate-dsh 0.3.44 → 0.3.45

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
@@ -653,8 +653,8 @@ function effectiveGraphNodeView(node, edges, events) {
653
653
  if ((record3.status === "active" || record3.status === "disputed") && activeSources.length > 0) {
654
654
  return { current: { ...record3, sourceEventIds: activeSources } };
655
655
  }
656
- if (visibleSources.length > 0 && record3.status === "superseded") {
657
- return { historical: { ...record3, sourceEventIds: visibleSources } };
656
+ if (visibleSources.length > 0 && (record3.status === "superseded" || activeSources.length === 0)) {
657
+ return { historical: { ...record3, status: "superseded", sourceEventIds: visibleSources } };
658
658
  }
659
659
  return {};
660
660
  };
@@ -7319,7 +7319,7 @@ var StrataGate = class _StrataGate {
7319
7319
  throw new TypeError("Turn userMessageId must not be empty");
7320
7320
  }
7321
7321
  const existingUserMessage = requestedUserMessageId ? this.openTail.find((message) => message.id === requestedUserMessageId && message.role === "user") : void 0;
7322
- if (existingUserMessage && (existingUserMessage.content !== input.user || existingUserMessage.threadId !== threadId)) {
7322
+ if (existingUserMessage && existingUserMessage.threadId !== threadId) {
7323
7323
  throw new Error(`Reserved user message ${requestedUserMessageId} does not match the appended turn`);
7324
7324
  }
7325
7325
  const userMessage = existingUserMessage ?? {
@@ -7349,8 +7349,13 @@ var StrataGate = class _StrataGate {
7349
7349
  if (index < 0) tail.push(assistant);
7350
7350
  else tail.splice(index + 1, 0, assistant);
7351
7351
  };
7352
+ const reservedContentChanged = existingUserMessage !== void 0 && existingUserMessage.content !== input.user;
7352
7353
  const appendInMemory = () => {
7353
7354
  if (receiptId && this.ingestionReceipts.has(receiptId)) return false;
7355
+ if (existingUserMessage) {
7356
+ existingUserMessage.content = input.user;
7357
+ if (input.userToolCalls !== void 0) existingUserMessage.toolCalls = structuredClone(input.userToolCalls);
7358
+ }
7354
7359
  this.currentTurn += 1;
7355
7360
  placeTurnMessages(this.openTail, false);
7356
7361
  if (receiptId) this.ingestionReceipts.set(receiptId, { id: receiptId, createdAt });
@@ -7358,7 +7363,7 @@ var StrataGate = class _StrataGate {
7358
7363
  };
7359
7364
  const appended = this.storage?.commitRecords ? await this.withMutationQueue(async () => {
7360
7365
  const reservedIsLast = !existingUserMessage || this.openTail.at(-1)?.id === existingUserMessage.id;
7361
- if (this.hasPendingRecordChanges() || !reservedIsLast) return this.applyMutation(appendInMemory);
7366
+ if (this.hasPendingRecordChanges() || !reservedIsLast || reservedContentChanged) return this.applyMutation(appendInMemory);
7362
7367
  if (receiptId && this.ingestionReceipts.has(receiptId)) return false;
7363
7368
  const receipt = receiptId ? { id: receiptId, createdAt } : void 0;
7364
7369
  const committed = await this.storage.commitRecords(this.namespace, this.revision, {
@@ -9463,17 +9468,18 @@ ${duplicate.summary}` : duplicate.summary;
9463
9468
  pendingBlockMessages(threadId) {
9464
9469
  const messages = this.threadOpenTail(threadId);
9465
9470
  let users = 0;
9466
- let end = 0;
9471
+ const selected = [];
9467
9472
  for (const [index, message] of messages.entries()) {
9468
9473
  if (message.role !== "user") continue;
9469
- if (!userTurnHasAssistant(messages, index)) break;
9474
+ if (!userTurnHasAssistant(messages, index)) continue;
9470
9475
  users += 1;
9471
- if (users !== this.blockTurnSize) continue;
9472
9476
  const nextUserOffset = messages.slice(index + 1).findIndex((candidate) => candidate.role === "user");
9473
- end = nextUserOffset === -1 ? messages.length : index + 1 + nextUserOffset;
9477
+ const end = nextUserOffset === -1 ? messages.length : index + 1 + nextUserOffset;
9478
+ selected.push(...messages.slice(index, end));
9479
+ if (users !== this.blockTurnSize) continue;
9474
9480
  break;
9475
9481
  }
9476
- return messages.slice(0, end);
9482
+ return selected;
9477
9483
  }
9478
9484
  async sealOpenTail(threadId) {
9479
9485
  const raw = this.pendingBlockMessages(threadId);
@@ -17352,6 +17358,8 @@ function renderMessages(messages) {
17352
17358
  }).join("\n\n");
17353
17359
  }
17354
17360
  function dshTurnAtBlockEnd(session, block) {
17361
+ const sourceTurns = sourceTurnNumbersForBlock(block);
17362
+ if (sourceTurns.length > 0) return sourceTurns.at(-1) ?? null;
17355
17363
  const blockEnd = Date.parse(block.createdAt);
17356
17364
  const events = session.snapshotEvents();
17357
17365
  for (let index = events.length - 1; index >= 0; index -= 1) {
@@ -17372,6 +17380,7 @@ function recoverableSealedSurfaceSources(session, blocks) {
17372
17380
  }
17373
17381
  const ends = /* @__PURE__ */ new Map();
17374
17382
  const visibleEnds = /* @__PURE__ */ new Set();
17383
+ const visibleTurns = /* @__PURE__ */ new Set();
17375
17384
  const compactedBlocks = /* @__PURE__ */ new Set();
17376
17385
  let open2;
17377
17386
  for (const event of session.snapshotEvents()) {
@@ -17387,6 +17396,7 @@ function recoverableSealedSurfaceSources(session, blocks) {
17387
17396
  if (open2?.turn === event.data.turn) {
17388
17397
  ends.set(event.time, event.data.turn);
17389
17398
  if (open2.visible) visibleEnds.add(event.time);
17399
+ if (open2.visible) visibleTurns.add(event.data.turn);
17390
17400
  }
17391
17401
  open2 = void 0;
17392
17402
  } else if (open2 && visible2.has(event.seq)) open2.visible = true;
@@ -17396,10 +17406,11 @@ function recoverableSealedSurfaceSources(session, blocks) {
17396
17406
  result.retired.add(block.id);
17397
17407
  continue;
17398
17408
  }
17409
+ const sourceTurns = sourceTurnNumbersForBlock(block);
17399
17410
  const endTime = Date.parse(block.createdAt);
17400
- const endTurn = ends.get(endTime);
17411
+ const endTurn = sourceTurns.at(-1) ?? ends.get(endTime);
17401
17412
  if (endTurn === void 0) continue;
17402
- if (!visibleEnds.has(endTime)) {
17413
+ if (sourceTurns.length > 0 ? !visibleTurns.has(endTurn) : !visibleEnds.has(endTime)) {
17403
17414
  result.retired.add(block.id);
17404
17415
  continue;
17405
17416
  }
@@ -17414,6 +17425,8 @@ function sealedSurfaceSeqs(session, endTurn, block) {
17414
17425
  const completed = [];
17415
17426
  let open2;
17416
17427
  const events = session.snapshotEvents();
17428
+ const sourceTurnNumbers = sourceTurnNumbersForBlock(block);
17429
+ const sourceTurnSet = new Set(sourceTurnNumbers);
17417
17430
  for (const event of events) {
17418
17431
  if (event.type === "turn/start") {
17419
17432
  open2 = { turn: event.data.turn, start: event.seq };
@@ -17422,7 +17435,7 @@ function sealedSurfaceSeqs(session, endTurn, block) {
17422
17435
  if (event.type !== "turn/end" || !open2 || event.data.turn !== open2.turn) continue;
17423
17436
  const turnEvents = events.slice(open2.start + 1, event.seq);
17424
17437
  const hasHumanMessage = turnEvents.some((candidate) => candidate.type === "user/message" && candidate.data.source.kind === "user");
17425
- if (hasHumanMessage && event.data.turn <= endTurn) {
17438
+ if (hasHumanMessage && (sourceTurnSet.size > 0 ? sourceTurnSet.has(event.data.turn) : event.data.turn <= endTurn)) {
17426
17439
  completed.push({
17427
17440
  turn: event.data.turn,
17428
17441
  endTime: event.time,
@@ -17439,10 +17452,9 @@ function sealedSurfaceSeqs(session, endTurn, block) {
17439
17452
  }
17440
17453
  open2 = void 0;
17441
17454
  }
17442
- const sourceTimes = block.l5Raw.filter((message) => message.role === "user").map((message) => Date.parse(message.createdAt));
17443
17455
  const turnCount = block.endTurn - block.startTurn + 1;
17444
- const selected = completed.slice(-turnCount);
17445
- if (selected.length !== turnCount || selected.at(-1)?.turn !== endTurn || sourceTimes.length !== turnCount || selected.some((turn, index) => turn.endTime !== sourceTimes[index])) return null;
17456
+ const selected = sourceTurnNumbers.length === turnCount ? sourceTurnNumbers.flatMap((turn) => completed.find((candidate) => candidate.turn === turn) ?? []) : completed.slice(-turnCount);
17457
+ if (selected.length !== turnCount || selected.at(-1)?.turn !== (sourceTurnNumbers.at(-1) ?? endTurn) || sourceTurnNumbers.length !== turnCount && selected.some((turn, index) => turn.endTime !== Date.parse(block.l5Raw.filter((message) => message.role === "user")[index].createdAt))) return null;
17446
17458
  const firstTurn = selected[0];
17447
17459
  const lastTurn = selected.at(-1);
17448
17460
  if (events.some((event) => {
@@ -17473,9 +17485,22 @@ function turnAtSurfaceSeq(session, seq) {
17473
17485
  return turn;
17474
17486
  }
17475
17487
  function sourceTurnsForBlock(session, block) {
17488
+ const stableTurns = sourceTurnNumbersForBlock(block);
17489
+ if (stableTurns.length > 0) return new Set(stableTurns);
17476
17490
  const sourceTimes = new Set(block.l5Raw.filter((message) => message.role === "user").map((message) => Date.parse(message.createdAt)));
17477
17491
  return new Set(session.snapshotEvents().filter((event) => event.type === "turn/end" && sourceTimes.has(event.time)).map((event) => event.data.turn));
17478
17492
  }
17493
+ function sourceTurnNumbersForBlock(block) {
17494
+ const turns = [];
17495
+ for (const message of block.l5Raw) {
17496
+ if (message.role !== "user") continue;
17497
+ const match = message.id.match(/:turn:(\d+):user$/u);
17498
+ if (!match) continue;
17499
+ const turn = Number(match[1]);
17500
+ if (Number.isSafeInteger(turn) && !turns.includes(turn)) turns.push(turn);
17501
+ }
17502
+ return turns;
17503
+ }
17479
17504
  function surfaceEventTurns(session, event) {
17480
17505
  if (event.surfaceOp !== "append" && event.sourceEventSeqs?.length) {
17481
17506
  const sourceTurns = event.sourceEventSeqs.flatMap((source) => {
@@ -17516,13 +17541,29 @@ function appendBlockSurface(session, block, text3, seqs) {
17516
17541
  throw Object.assign(new Error("StrataGate surface replacement no longer owns its complete source range"), { retryable: false });
17517
17542
  }
17518
17543
  session.append("user/message", createUserMessage2({
17519
- content: [{ type: "text", text: text3 }],
17544
+ content: [{ type: "text", text: text3 }, ...sourceAttachmentBlocks(session, seqs)],
17520
17545
  source: dshMessageSource()
17521
17546
  }), {
17522
17547
  surfaceOp: dshReplaceSurfaceOp(seqs[0], seqs.at(-1)),
17523
17548
  sourceEventSeqs: seqs
17524
17549
  });
17525
17550
  }
17551
+ function sourceAttachmentBlocks(session, seqs) {
17552
+ const attachments = [];
17553
+ const seen = /* @__PURE__ */ new Set();
17554
+ for (const seq of seqs) {
17555
+ const event = session.eventAt(seq);
17556
+ if (event?.type !== "user/message") continue;
17557
+ for (const block of event.data.content) {
17558
+ if (block.type !== "image" && block.type !== "file") continue;
17559
+ const key = `${block.type}:${String(block.attachment.attachmentId)}`;
17560
+ if (seen.has(key)) continue;
17561
+ seen.add(key);
17562
+ attachments.push(structuredClone(block));
17563
+ }
17564
+ }
17565
+ return attachments;
17566
+ }
17526
17567
  function surfaceMaintenanceInput(state) {
17527
17568
  return `${state.memory.storageRevision}:${surfaceCapacityKey(state.session)}:${state.session.surface?.nodes.join(",") ?? ""}`;
17528
17569
  }