stratagate-dsh 0.3.42 → 0.3.43

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
@@ -431,6 +431,28 @@ function formatRawTranscript(messages) {
431
431
  ...(message.toolCalls ?? []).map(renderRawToolTrace)
432
432
  ]).join("\n\n");
433
433
  }
434
+ var ATTACHMENT_REFERENCE_PREFIX = "[StrataGate attachment] ";
435
+ function attachmentReferenceLines(value) {
436
+ return value.split(/\r?\n/u).filter((line) => line.startsWith(ATTACHMENT_REFERENCE_PREFIX));
437
+ }
438
+ function attachmentReferences(messages) {
439
+ const lines = [];
440
+ for (const message of messages) {
441
+ lines.push(...attachmentReferenceLines(message.content));
442
+ for (const trace of message.toolCalls ?? []) {
443
+ if (typeof trace.result === "string") lines.push(...attachmentReferenceLines(trace.result));
444
+ }
445
+ }
446
+ return [...new Set(lines)];
447
+ }
448
+ function preserveAttachmentReferences(content, messages) {
449
+ const missing2 = attachmentReferences(messages).filter((reference) => !content.includes(reference));
450
+ if (missing2.length === 0) return content;
451
+ return `${content}
452
+
453
+ Attachments retained in source:
454
+ ${missing2.join("\n")}`;
455
+ }
434
456
  function estimateTokens(value) {
435
457
  let tokens = 0;
436
458
  let asciiRun = 0;
@@ -6545,18 +6567,28 @@ function renderBlock(block, level) {
6545
6567
  if (block.processingStatus !== "ready" || !block.l0Title || !block.l0Tags || !block.l1Summary || !block.l2Keypoints) {
6546
6568
  throw new Error(`Block ${block.id} is not ready for rendering`);
6547
6569
  }
6548
- if (level === 0) return `${block.l0Title}
6570
+ let body;
6571
+ if (level === 0) body = `${block.l0Title}
6549
6572
  Tags: ${block.l0Tags.join(", ") || "none"}`;
6550
- if (level === 1) return block.l1Summary;
6551
- if (level === 2) return block.l2Keypoints.map((point) => `- ${point}`).join("\n") || block.l1Summary;
6552
- const deterministic = deterministicBlockLayers(block.l5Raw);
6553
- if (level === 3) return deterministic.l3Condensed;
6554
- if (level === 4) return deterministic.l4Readable;
6555
- return formatRawTranscript(block.l5Raw);
6573
+ else if (level === 1) body = block.l1Summary;
6574
+ else if (level === 2) body = block.l2Keypoints.map((point) => `- ${point}`).join("\n") || block.l1Summary;
6575
+ else {
6576
+ const deterministic = deterministicBlockLayers(block.l5Raw);
6577
+ body = level === 3 ? deterministic.l3Condensed : level === 4 ? deterministic.l4Readable : formatRawTranscript(block.l5Raw);
6578
+ }
6579
+ return preserveAttachmentReferences(body, block.l5Raw);
6556
6580
  }
6557
6581
  function sameIds(left, right) {
6558
6582
  return left.length === right.length && left.every((id, index) => id === right[index]);
6559
6583
  }
6584
+ function userTurnHasAssistant(messages, userIndex) {
6585
+ for (let index = userIndex + 1; index < messages.length; index += 1) {
6586
+ const role = messages[index].role;
6587
+ if (role === "user") return false;
6588
+ if (role === "assistant") return true;
6589
+ }
6590
+ return false;
6591
+ }
6560
6592
  function errorMessage(error) {
6561
6593
  if (error && typeof error === "object" && "fullMessage" in error) {
6562
6594
  const fullMessage = error.fullMessage;
@@ -7281,8 +7313,16 @@ var StrataGate = class _StrataGate {
7281
7313
  throw new TypeError("Turn threadId must not be empty");
7282
7314
  }
7283
7315
  const createdAt = toUtc8Iso(input.createdAt ?? this.now());
7284
- const userMessage = {
7285
- id: this.idFactory("msg"),
7316
+ const requestedUserMessageId = input.userMessageId?.trim();
7317
+ if (input.userMessageId !== void 0 && !requestedUserMessageId) {
7318
+ throw new TypeError("Turn userMessageId must not be empty");
7319
+ }
7320
+ const existingUserMessage = requestedUserMessageId ? this.openTail.find((message) => message.id === requestedUserMessageId && message.role === "user") : void 0;
7321
+ if (existingUserMessage && (existingUserMessage.content !== input.user || existingUserMessage.threadId !== threadId)) {
7322
+ throw new Error(`Reserved user message ${requestedUserMessageId} does not match the appended turn`);
7323
+ }
7324
+ const userMessage = existingUserMessage ?? {
7325
+ id: requestedUserMessageId ?? this.idFactory("msg"),
7286
7326
  role: "user",
7287
7327
  content: input.user,
7288
7328
  createdAt,
@@ -7297,30 +7337,42 @@ var StrataGate = class _StrataGate {
7297
7337
  ...threadId ? { threadId } : {},
7298
7338
  ...input.assistantToolCalls ? { toolCalls: structuredClone(input.assistantToolCalls) } : {}
7299
7339
  };
7340
+ const messagesToAppend = existingUserMessage ? [assistantMessage] : [userMessage, assistantMessage];
7341
+ const placeTurnMessages = (tail, capture) => {
7342
+ const assistant = capture ? captureRawMessage(assistantMessage) : assistantMessage;
7343
+ if (!existingUserMessage) {
7344
+ tail.push(capture ? captureRawMessage(userMessage) : userMessage, assistant);
7345
+ return;
7346
+ }
7347
+ const index = tail.findIndex((message) => message.id === existingUserMessage.id);
7348
+ if (index < 0) tail.push(assistant);
7349
+ else tail.splice(index + 1, 0, assistant);
7350
+ };
7300
7351
  const appendInMemory = () => {
7301
7352
  if (receiptId && this.ingestionReceipts.has(receiptId)) return false;
7302
7353
  this.currentTurn += 1;
7303
- this.openTail.push(userMessage, assistantMessage);
7354
+ placeTurnMessages(this.openTail, false);
7304
7355
  if (receiptId) this.ingestionReceipts.set(receiptId, { id: receiptId, createdAt });
7305
7356
  return true;
7306
7357
  };
7307
7358
  const appended = this.storage?.commitRecords ? await this.withMutationQueue(async () => {
7308
- if (this.hasPendingRecordChanges()) return this.applyMutation(appendInMemory);
7359
+ const reservedIsLast = !existingUserMessage || this.openTail.at(-1)?.id === existingUserMessage.id;
7360
+ if (this.hasPendingRecordChanges() || !reservedIsLast) return this.applyMutation(appendInMemory);
7309
7361
  if (receiptId && this.ingestionReceipts.has(receiptId)) return false;
7310
7362
  const receipt = receiptId ? { id: receiptId, createdAt } : void 0;
7311
7363
  const committed = await this.storage.commitRecords(this.namespace, this.revision, {
7312
7364
  kind: "append",
7313
- messages: [userMessage, assistantMessage],
7365
+ messages: messagesToAppend,
7314
7366
  updatedAt: createdAt,
7315
7367
  ...receipt ? { receipt } : {}
7316
7368
  });
7317
7369
  if (committed.changed) {
7318
7370
  this.currentTurn += 1;
7319
- this.openTail.push(userMessage, assistantMessage);
7371
+ placeTurnMessages(this.openTail, false);
7320
7372
  if (receipt) this.ingestionReceipts.set(receipt.id, receipt);
7321
7373
  const baseline = this.committedMetadata;
7322
7374
  baseline.currentTurn = this.currentTurn;
7323
- baseline.openTail.push(captureRawMessage(userMessage), captureRawMessage(assistantMessage));
7375
+ placeTurnMessages(baseline.openTail, true);
7324
7376
  if (receipt) baseline.ingestionReceipts.push(structuredClone(receipt));
7325
7377
  }
7326
7378
  this.revision = committed.revision;
@@ -7330,7 +7382,7 @@ var StrataGate = class _StrataGate {
7330
7382
  if (options.deferProcessing === true) {
7331
7383
  return { sealedBlock: null, readyBlocks: [], extractedEvents: [], projectedElements: [] };
7332
7384
  }
7333
- if (this.threadOpenTail(threadId).filter((message) => message.role === "user").length < this.blockTurnSize) {
7385
+ if (this.completeOpenUserCount(threadId) < this.blockTurnSize) {
7334
7386
  const projectedElements2 = await this.projectEligibleElements() ?? [];
7335
7387
  await this.projectEligibleGraph();
7336
7388
  return { sealedBlock: null, readyBlocks: [], extractedEvents: [], projectedElements: projectedElements2 };
@@ -7346,6 +7398,33 @@ var StrataGate = class _StrataGate {
7346
7398
  await this.projectEligibleGraph();
7347
7399
  return { sealedBlock, readyBlocks, extractedEvents, projectedElements };
7348
7400
  }
7401
+ /**
7402
+ * Persist the current user message before a tool call needs to cite it.
7403
+ * The message stays in the open tail but does not count as a sealable turn
7404
+ * until appendTurn stores its assistant reply immediately after it.
7405
+ * That call reuses this message by userMessageId and counts the turn once.
7406
+ */
7407
+ async reserveUserMessage(input) {
7408
+ const id = input.id.trim();
7409
+ if (!id) throw new TypeError("Reserved user message id must not be empty");
7410
+ const content = input.content;
7411
+ if (!content.trim()) throw new TypeError("Reserved user message content must not be empty");
7412
+ const threadId = input.threadId?.trim();
7413
+ if (input.threadId !== void 0 && !threadId) throw new TypeError("Reserved user message threadId must not be empty");
7414
+ const createdAt = toUtc8Iso(input.createdAt ?? this.now());
7415
+ return this.commitMutation(() => {
7416
+ const existing = this.openTail.find((message2) => message2.id === id);
7417
+ if (existing) {
7418
+ if (existing.role !== "user" || existing.content !== content || existing.threadId !== threadId) {
7419
+ throw new Error(`Reserved user message ${id} already exists with different content`);
7420
+ }
7421
+ return existing;
7422
+ }
7423
+ const message = { id, role: "user", content, createdAt, ...threadId ? { threadId } : {} };
7424
+ this.openTail.push(message);
7425
+ return message;
7426
+ });
7427
+ }
7349
7428
  async resumePendingWork(options = {}) {
7350
7429
  const sealedBlocks = [];
7351
7430
  const readyBlocks = [];
@@ -8863,7 +8942,15 @@ ${duplicate.summary}` : duplicate.summary;
8863
8942
  resolveAgentEventProvenance(candidate, threadId, now) {
8864
8943
  const real = threadId ? this.listOpenTail(threadId).filter((message) => message.role === "user" || message.role === "assistant") : [];
8865
8944
  if (real.length > 0) {
8866
- return { messageIds: real.slice(-AGENT_EVENT_PROVENANCE_LIMIT).map(({ id }) => id), synthetic: false };
8945
+ let latestUser = -1;
8946
+ for (let index = real.length - 1; index >= 0; index -= 1) {
8947
+ if (real[index].role === "user") {
8948
+ latestUser = index;
8949
+ break;
8950
+ }
8951
+ }
8952
+ const currentTurn = latestUser >= 0 ? real.slice(latestUser) : real;
8953
+ return { messageIds: currentTurn.slice(-AGENT_EVENT_PROVENANCE_LIMIT).map(({ id }) => id), synthetic: false };
8867
8954
  }
8868
8955
  const sealed = threadId ? this.blocks.filter((block) => block.threadId === threadId && block.l5Raw.length > 0) : [];
8869
8956
  const last = sealed.at(-1);
@@ -9339,6 +9426,15 @@ ${duplicate.summary}` : duplicate.summary;
9339
9426
  threadOpenTail(threadId) {
9340
9427
  return this.openTail.filter((message) => message.threadId === threadId);
9341
9428
  }
9429
+ /** User messages that already have their assistant reply in this thread. */
9430
+ completeOpenUserCount(threadId) {
9431
+ const messages = this.threadOpenTail(threadId);
9432
+ let count = 0;
9433
+ for (let index = 0; index < messages.length; index += 1) {
9434
+ if (messages[index].role === "user" && userTurnHasAssistant(messages, index)) count += 1;
9435
+ }
9436
+ return count;
9437
+ }
9342
9438
  threadBlocks(threadId) {
9343
9439
  return this.blocks.filter((block) => block.threadId === threadId);
9344
9440
  }
@@ -9351,16 +9447,11 @@ ${duplicate.summary}` : duplicate.summary;
9351
9447
  return this.nextSealableThread() !== null;
9352
9448
  }
9353
9449
  nextSealableThread() {
9354
- const counts = [];
9450
+ const seen = /* @__PURE__ */ new Set();
9355
9451
  for (const message of this.openTail) {
9356
- if (message.role !== "user") continue;
9357
- let entry = counts.find((candidate) => candidate.threadId === message.threadId);
9358
- if (!entry) {
9359
- entry = { threadId: message.threadId, users: 0 };
9360
- counts.push(entry);
9361
- }
9362
- entry.users += 1;
9363
- if (entry.users >= this.blockTurnSize) return { threadId: entry.threadId };
9452
+ if (seen.has(message.threadId)) continue;
9453
+ seen.add(message.threadId);
9454
+ if (this.completeOpenUserCount(message.threadId) >= this.blockTurnSize) return { threadId: message.threadId };
9364
9455
  }
9365
9456
  return null;
9366
9457
  }
@@ -9371,9 +9462,10 @@ ${duplicate.summary}` : duplicate.summary;
9371
9462
  pendingBlockMessages(threadId) {
9372
9463
  const messages = this.threadOpenTail(threadId);
9373
9464
  let users = 0;
9374
- let end = messages.length;
9465
+ let end = 0;
9375
9466
  for (const [index, message] of messages.entries()) {
9376
9467
  if (message.role !== "user") continue;
9468
+ if (!userTurnHasAssistant(messages, index)) break;
9377
9469
  users += 1;
9378
9470
  if (users !== this.blockTurnSize) continue;
9379
9471
  const nextUserOffset = messages.slice(index + 1).findIndex((candidate) => candidate.role === "user");
@@ -13139,6 +13231,24 @@ async function listEndpointModels(url, key) {
13139
13231
  }
13140
13232
 
13141
13233
  // src/fold.ts
13234
+ function renderAttachment(block) {
13235
+ const payload = block.type === "image" ? {
13236
+ type: "image",
13237
+ ...block.attachment.name ? { name: block.attachment.name } : {},
13238
+ attachmentId: String(block.attachment.attachmentId),
13239
+ mediaType: block.attachment.mediaType,
13240
+ bytes: block.attachment.bytes,
13241
+ width: block.attachment.width,
13242
+ height: block.attachment.height,
13243
+ ...block.attachment.originalDimensions ? { originalDimensions: block.attachment.originalDimensions } : {}
13244
+ } : {
13245
+ type: "file",
13246
+ name: block.attachment.name,
13247
+ attachmentId: String(block.attachment.attachmentId),
13248
+ bytes: block.attachment.bytes
13249
+ };
13250
+ return `[StrataGate attachment] ${JSON.stringify(payload)}`;
13251
+ }
13142
13252
  function renderBlocks(blocks) {
13143
13253
  const output = [];
13144
13254
  for (const block of blocks) {
@@ -13149,7 +13259,8 @@ function renderBlocks(blocks) {
13149
13259
  case "reasoning":
13150
13260
  break;
13151
13261
  case "image":
13152
- output.push("[image]");
13262
+ case "file":
13263
+ output.push(renderAttachment(block));
13153
13264
  break;
13154
13265
  case "tool-call":
13155
13266
  break;
@@ -13232,6 +13343,7 @@ var TurnFolder = class {
13232
13343
  user: pending.user.join("\n\n"),
13233
13344
  assistant: pending.assistant.join("\n\n") || reasonLabel(event.data.reason),
13234
13345
  threadId: sessionId,
13346
+ userMessageId: pending.userMessageId,
13235
13347
  assistantToolCalls: [...pending.tools.values()],
13236
13348
  createdAt: toUtc8Iso(event.time),
13237
13349
  receiptId: `dsh:${sessionId}:turn:${event.data.turn}`,
@@ -13250,7 +13362,14 @@ var TurnFolder = class {
13250
13362
  }
13251
13363
  let pending = byTurn.get(turn);
13252
13364
  if (!pending) {
13253
- pending = { turn, user: [], assistant: [], tools: /* @__PURE__ */ new Map(), ignoredTools: /* @__PURE__ */ new Set() };
13365
+ pending = {
13366
+ turn,
13367
+ userMessageId: `dsh:${sessionId}:turn:${turn}:user`,
13368
+ user: [],
13369
+ assistant: [],
13370
+ tools: /* @__PURE__ */ new Map(),
13371
+ ignoredTools: /* @__PURE__ */ new Set()
13372
+ };
13254
13373
  byTurn.set(turn, pending);
13255
13374
  }
13256
13375
  return pending;
@@ -15742,6 +15861,8 @@ var StrataGateRuntime = class _StrataGateRuntime {
15742
15861
  throw new Error("StrataGate agent memory is disabled by configuration (agentMemoryEnabled=false).");
15743
15862
  }
15744
15863
  const memory = await this.space(session);
15864
+ const current = currentTurnUserInput(session);
15865
+ if (current) await memory.reserveUserMessage(current);
15745
15866
  const hasDecider = typeof this.models.externalMemoryDecider === "function";
15746
15867
  const deciderBudgetMs = Math.min(this.config.structuredTaskTimeoutMs ?? 12e4, 3e4);
15747
15868
  const result = await this.models.run(session, () => memory.recordAgentEvent({
@@ -17161,31 +17282,40 @@ var StrataGateRuntime = class _StrataGateRuntime {
17161
17282
  ].join(" ");
17162
17283
  }
17163
17284
  };
17285
+ function currentTurnUserInput(session) {
17286
+ const events = session.snapshotEvents();
17287
+ let start = -1;
17288
+ let turn;
17289
+ for (let index = events.length - 1; index >= 0; index -= 1) {
17290
+ const event = events[index];
17291
+ if (event?.type === "turn/start") {
17292
+ start = index;
17293
+ turn = event.data.turn;
17294
+ break;
17295
+ }
17296
+ if (event?.type === "turn/end" || event?.type === "session/end-seed") return null;
17297
+ }
17298
+ if (start < 0 || turn === void 0) return null;
17299
+ const messages = events.slice(start + 1).filter((event) => event.type === "user/message" && event.data.source.kind === "user");
17300
+ const content = messages.map((event) => renderBlocks(event.data.content)).filter(Boolean).join("\n\n").trim();
17301
+ if (!content) return null;
17302
+ const firstTime = messages[0]?.time;
17303
+ return {
17304
+ id: `dsh:${String(session.id)}:turn:${turn}:user`,
17305
+ content,
17306
+ threadId: String(session.id),
17307
+ ...typeof firstTime === "number" && Number.isFinite(firstTime) ? { createdAt: new Date(firstTime).toISOString() } : {}
17308
+ };
17309
+ }
17164
17310
  function currentUserMessage(session) {
17165
17311
  const messages = typeof session.deriveMessages === "function" ? session.deriveMessages() : [];
17166
17312
  for (let index = messages.length - 1; index >= 0; index -= 1) {
17167
17313
  const message = messages[index];
17168
17314
  if (message?.role !== "user" || message.source.kind !== "user") continue;
17169
- return renderContent(message.content);
17315
+ return renderBlocks(message.content);
17170
17316
  }
17171
17317
  return "";
17172
17318
  }
17173
- function renderContent(content) {
17174
- const output = [];
17175
- for (const block of content) {
17176
- if (block.type === "text" && typeof block.text === "string" && block.text.trim()) {
17177
- output.push(block.text.trim());
17178
- } else if (block.type === "image") {
17179
- output.push("[image]");
17180
- } else {
17181
- const legacy = block;
17182
- if (legacy.type === "tool-result" && Array.isArray(legacy.content)) {
17183
- output.push(renderContent(legacy.content));
17184
- }
17185
- }
17186
- }
17187
- return output.filter(Boolean).join("\n");
17188
- }
17189
17319
  function recentTurns(messages, count) {
17190
17320
  let remaining = count;
17191
17321
  for (let index = messages.length - 1; index >= 0; index -= 1) {
@@ -17516,6 +17646,7 @@ function compactEvent2(event, score) {
17516
17646
  temporal: compactTemporal(event),
17517
17647
  ...event.sourceBlockId !== void 0 ? { sourceBlockId: event.sourceBlockId } : {},
17518
17648
  status: event.status,
17649
+ ...event.supersededBy ? { supersededBy: event.supersededBy } : {},
17519
17650
  scope: event.scope,
17520
17651
  criticality: event.criticality,
17521
17652
  ...agentRecorded ? { source: "agent-recorded" } : {},
@@ -17649,9 +17780,12 @@ function renderActivatedMemory(events, graphResults) {
17649
17780
  id: event.id,
17650
17781
  title: event.title,
17651
17782
  summary: event.summary,
17783
+ status: event.status,
17784
+ supersededBy: event.supersededBy,
17785
+ scope: event.scope,
17652
17786
  happenedStart: event.temporal.happenedStart,
17653
17787
  happenedEnd: event.temporal.happenedEnd,
17654
- temporal: { status: event.temporal.status }
17788
+ temporal: event.temporal
17655
17789
  });
17656
17790
  const cost = estimateTokens(`
17657
17791
  Events:
@@ -18605,19 +18739,19 @@ function blockLayers(block) {
18605
18739
  const deterministic = deterministicBlockLayers(block.l5Raw);
18606
18740
  if (block.processingStatus !== "ready" || !block.l0Title || !block.l0Tags || !block.l1Summary || !block.l2Keypoints) {
18607
18741
  return withLayerMetrics([
18608
- { level: 3, content: deterministic.l3Condensed },
18609
- { level: 4, content: deterministic.l4Readable },
18610
- { level: 5, content: formatRawTranscript(block.l5Raw) }
18742
+ { level: 3, content: preserveAttachmentReferences(deterministic.l3Condensed, block.l5Raw) },
18743
+ { level: 4, content: preserveAttachmentReferences(deterministic.l4Readable, block.l5Raw) },
18744
+ { level: 5, content: preserveAttachmentReferences(formatRawTranscript(block.l5Raw), block.l5Raw) }
18611
18745
  ]);
18612
18746
  }
18613
18747
  return withLayerMetrics([
18614
- { level: 0, content: `${block.l0Title}
18615
- Tags: ${block.l0Tags.join(", ") || "none"}` },
18616
- { level: 1, content: block.l1Summary || block.l0Title },
18617
- { level: 2, content: block.l2Keypoints.map((point) => `- ${point}`).join("\n") || block.l1Summary || block.l0Title },
18618
- { level: 3, content: deterministic.l3Condensed || block.l2Keypoints.join("\n") || block.l1Summary },
18619
- { level: 4, content: deterministic.l4Readable || deterministic.l3Condensed },
18620
- { level: 5, content: formatRawTranscript(block.l5Raw) }
18748
+ { level: 0, content: preserveAttachmentReferences(`${block.l0Title}
18749
+ Tags: ${block.l0Tags.join(", ") || "none"}`, block.l5Raw) },
18750
+ { level: 1, content: preserveAttachmentReferences(block.l1Summary || block.l0Title, block.l5Raw) },
18751
+ { level: 2, content: preserveAttachmentReferences(block.l2Keypoints.map((point) => `- ${point}`).join("\n") || block.l1Summary || block.l0Title, block.l5Raw) },
18752
+ { level: 3, content: preserveAttachmentReferences(deterministic.l3Condensed || block.l2Keypoints.join("\n") || block.l1Summary, block.l5Raw) },
18753
+ { level: 4, content: preserveAttachmentReferences(deterministic.l4Readable || deterministic.l3Condensed, block.l5Raw) },
18754
+ { level: 5, content: preserveAttachmentReferences(formatRawTranscript(block.l5Raw), block.l5Raw) }
18621
18755
  ]);
18622
18756
  }
18623
18757
  function sampledEventWeight(event, turn, lastAdoptedTurn, mentionCount) {