stratagate-dsh 0.3.34 → 0.3.35

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
@@ -10442,6 +10442,65 @@ function parseJsonResponse(value, requiredKeys = []) {
10442
10442
  throw new ModelJsonResponseError(void 0, { response: value });
10443
10443
  }
10444
10444
 
10445
+ // src/extraction-batches.ts
10446
+ var ExtractionCapacityError = class extends Error {
10447
+ constructor(capacity) {
10448
+ super(`Event extraction ${capacity} capacity exceeded; a smaller source batch is required`);
10449
+ this.capacity = capacity;
10450
+ }
10451
+ capacity;
10452
+ };
10453
+ var CONTEXT_CHARS = 1200;
10454
+ function splitExtractionSlice(slice) {
10455
+ const messages = slice.messages;
10456
+ let left;
10457
+ let right;
10458
+ if (messages.length > 1) {
10459
+ const weights = messages.map((message) => estimateTokens(JSON.stringify(message)));
10460
+ const half = weights.reduce((sum, size2) => sum + size2, 0) / 2;
10461
+ let index = 1;
10462
+ let size = weights[0];
10463
+ while (index < messages.length - 1 && size < half) size += weights[index++];
10464
+ left = messages.slice(0, index);
10465
+ right = messages.slice(index);
10466
+ } else {
10467
+ const message = messages[0];
10468
+ if (!message) throw new Error("Event extraction has no source to split");
10469
+ const content = message.content + (message.toolCalls?.length ? `
10470
+ [Tool evidence, JSON]
10471
+ ${JSON.stringify(message.toolCalls)}` : "");
10472
+ if (content.length < 2) throw new Error("Event extraction cannot split this source any further");
10473
+ const middle = Math.floor(content.length / 2);
10474
+ const boundary = content.lastIndexOf("\n", middle);
10475
+ let cut = boundary >= middle / 2 ? boundary + 1 : middle;
10476
+ if (/[\uD800-\uDBFF]/.test(content[cut - 1])) cut += 1;
10477
+ if (cut >= content.length) throw new Error("Event extraction cannot split this source any further");
10478
+ const { toolCalls: _tools, ...base } = message;
10479
+ left = [{ ...base, content: content.slice(0, cut) }];
10480
+ right = [{ ...base, content: content.slice(cut) }];
10481
+ }
10482
+ const excerpt = (items2) => items2.map((message) => JSON.stringify(message)).join("\n");
10483
+ return [
10484
+ { messages: left, before: slice.before, after: excerpt(right).slice(0, CONTEXT_CHARS), depth: slice.depth + 1 },
10485
+ { messages: right, before: excerpt(left).slice(-CONTEXT_CHARS), after: slice.after, depth: slice.depth + 1 }
10486
+ ];
10487
+ }
10488
+ function mergeExtractionEvents(events, exactKey) {
10489
+ const unique3 = /* @__PURE__ */ new Map();
10490
+ for (const event of events) {
10491
+ const key = exactKey(event);
10492
+ const prior = unique3.get(key);
10493
+ if (!prior) {
10494
+ unique3.set(key, structuredClone(event));
10495
+ continue;
10496
+ }
10497
+ prior.sourceMessageIds = [.../* @__PURE__ */ new Set([...prior.sourceMessageIds, ...event.sourceMessageIds])];
10498
+ prior.tags = [.../* @__PURE__ */ new Set([...prior.tags ?? [], ...event.tags ?? []])].slice(0, 16);
10499
+ prior.quotes = [.../* @__PURE__ */ new Set([...prior.quotes ?? [], ...event.quotes ?? []])].slice(0, 12);
10500
+ }
10501
+ return [...unique3.values()];
10502
+ }
10503
+
10445
10504
  // src/llm.ts
10446
10505
  import { randomUUID as randomUUID5 } from "node:crypto";
10447
10506
 
@@ -10791,27 +10850,38 @@ function l2Neighbor(block) {
10791
10850
  l2Keypoints: block.l2Keypoints
10792
10851
  };
10793
10852
  }
10794
- function extractorPayload(context) {
10795
- return {
10853
+ function extractorPayload(context, slice) {
10854
+ const payload = {
10796
10855
  target: {
10797
10856
  blockId: context.target.id,
10798
10857
  sequence: context.target.sequence,
10799
10858
  startTurn: context.target.startTurn,
10800
10859
  endTurn: context.target.endTurn,
10801
10860
  createdAt: context.target.createdAt,
10802
- messages: buildMemoryDerivationMessages(context.target.l5Raw)
10861
+ messages: slice.messages,
10862
+ ...slice.depth ? { fragment: { depth: slice.depth, beforeContextOnly: slice.before, afterContextOnly: slice.after } } : {}
10803
10863
  },
10804
10864
  neighbors: {
10805
10865
  previous: l2Neighbor(context.previous),
10806
10866
  next: l2Neighbor(context.next)
10807
10867
  },
10808
- allowedSourceMessageIds: context.target.l5Raw.map((message) => message.id),
10809
- timeline: context.timeline
10868
+ allowedSourceMessageIds: [...new Set(slice.messages.map((message) => message.id))],
10869
+ timeline: [...context.timeline]
10810
10870
  };
10871
+ if (estimateTokens(JSON.stringify(payload.neighbors)) > 3e3) {
10872
+ payload.neighbors = { previous: null, next: null };
10873
+ }
10874
+ while (payload.timeline.length && estimateTokens(JSON.stringify({ neighbors: payload.neighbors, timeline: payload.timeline })) > 3e3) payload.timeline.pop();
10875
+ return payload;
10811
10876
  }
10812
10877
  var JSON_RESPONSE_ATTEMPTS = 2;
10813
10878
  var JSON_RETRY_INSTRUCTION = "Your previous response did not make one valid call to the requested tool. Do not spend output on analysis or reasoning. Immediately call that tool exactly once with complete arguments. Do not return an answer as text or markdown.";
10814
10879
  var DEFAULT_STRUCTURED_TIMEOUT_MS = 12e4;
10880
+ var EXTRACTION_OUTPUT_TOKENS = 8192;
10881
+ var EXTRACTION_INPUT_TOKENS = 12e3;
10882
+ var EXTRACTION_MAX_CALLS = 32;
10883
+ var EXTRACTION_MAX_DEPTH = 8;
10884
+ var EXTRACTION_MAX_RESERVED_TOKENS = 262144;
10815
10885
  var STRUCTURED_FIELDS = {
10816
10886
  summarizer: ["l0Title", "l0Tags", "l1Summary", "l2Keypoints", "shouldExtract"],
10817
10887
  extractor: ["shouldExtract", "reason", "events"],
@@ -11345,6 +11415,7 @@ var DshModelBridge = class {
11345
11415
  ctx;
11346
11416
  config;
11347
11417
  liveStructuredReasoningEffort;
11418
+ extractionBudgets = new AsyncLocalStorage();
11348
11419
  diagnosticScopes = new AsyncLocalStorage();
11349
11420
  diagnostics;
11350
11421
  diagnosticNamespace = () => "@shared";
@@ -11424,7 +11495,40 @@ Choose exactly one action using stratagate_dreaming_step. args is an object. Fin
11424
11495
  };
11425
11496
  };
11426
11497
  extractor = async (context) => {
11427
- const validMessageIds = new Set(context.target.l5Raw.map((message) => message.id));
11498
+ const budget = { calls: 0, reservedTokens: 0, deadline: Date.now() + Math.min(
11499
+ 36e4,
11500
+ 3 * (this.config.structuredTaskTimeoutMs ?? DEFAULT_STRUCTURED_TIMEOUT_MS)
11501
+ ) };
11502
+ return this.extractionBudgets.run(budget, async () => {
11503
+ const exactKeys = /* @__PURE__ */ new WeakMap();
11504
+ const visit = async (slice) => {
11505
+ this.assertExtractionBudget();
11506
+ try {
11507
+ return (await this.extractSlice(context, slice, exactKeys)).events;
11508
+ } catch (error) {
11509
+ if (!(error instanceof ExtractionCapacityError)) throw error;
11510
+ if (slice.depth >= EXTRACTION_MAX_DEPTH) throw new Error("Event extraction split depth exhausted; original Block remains pending", { cause: error });
11511
+ const [left, right] = splitExtractionSlice(slice);
11512
+ return [...await visit(left), ...await visit(right)];
11513
+ }
11514
+ };
11515
+ const events = mergeExtractionEvents(await visit({
11516
+ messages: buildMemoryDerivationMessages(context.target.l5Raw),
11517
+ before: "",
11518
+ after: "",
11519
+ depth: 0
11520
+ }), (event) => exactKeys.get(event));
11521
+ return { shouldExtract: events.length > 0, reason: events.length ? "Durable evidence extracted." : "No durable evidence.", events };
11522
+ });
11523
+ };
11524
+ assertExtractionBudget() {
11525
+ const budget = this.extractionBudgets.getStore();
11526
+ if (budget && (budget.calls >= EXTRACTION_MAX_CALLS || Date.now() >= budget.deadline)) {
11527
+ throw new Error("Event extraction shared call/time budget exhausted; original Block remains pending");
11528
+ }
11529
+ }
11530
+ async extractSlice(context, slice, exactKeys) {
11531
+ const validMessageIds = new Set(slice.messages.map((message) => message.id));
11428
11532
  const raw = object(await this.callStructured(
11429
11533
  "extractor",
11430
11534
  `You are the StrataGate Event Extractor. Make the final extraction decision independently of the Summarizer's high-recall pre-screen. Extract durable, evidence-backed events from target.messages, then call ${STRUCTURED_TOOLS.extractor.name} exactly once. If none qualifies, set shouldExtract=false and events=[]; give a brief reason, not an Event body.
@@ -11445,10 +11549,21 @@ Preserve source certainty and attribution: distinguish a user statement or decis
11445
11549
 
11446
11550
  target.messages is a provenance-preserving derivation view of the target Block: message ids and conversational text are retained, while tool code and oversized tool payloads may be marked compacted. Use only retained tool names, evidence summaries, and excerpts; do not invent omitted details. Only target.messages may supply new facts, exact quotes, and sourceMessageIds. neighbors.previous and neighbors.next are context-only L2 summaries; timeline only helps identify historical relationships. Neither neighbors nor timeline nor an assistant recap of older memory can create a new Event. Require new human input or a new observable task/tool outcome from target.messages. Every sourceMessageIds entry must exactly match allowedSourceMessageIds and directly support the Event.
11447
11551
 
11552
+ When target.fragment is present, messages are a consecutive source slice, possibly part of one original message. beforeContextOnly and afterContextOnly are adjacent excerpts for resolving references, qualifiers and corrections only, never additional factual evidence or quote sources. Do not infer a completed sentence or tool result from a cut fragment. Preserve corrections, cancellations and uncertainty visible in the source; do not lower admission or merge independent facts to fit the output budget.
11553
+
11448
11554
  Use project scope for repository decisions, user scope for stable preferences/identity, and session scope for temporary task state. temporal.eventType must use exactly one stable value: decision, release, task_completed, plan, change, cancellation, incident, meeting, collaboration, migration, or other. temporal.participants contains canonical entity names. Use ISO-8601 timestamps with the explicit +08:00 offset in temporal fields. Keep happened time separate from mentionedAt; when happened time is unknown omit it and set precision/basis to unknown. Do not return the result as text.`,
11449
- extractorPayload(context)
11555
+ extractorPayload(context, slice),
11556
+ (value) => {
11557
+ const result = object(value);
11558
+ if (result.shouldExtract !== true) return;
11559
+ const events2 = Array.isArray(result.events) ? result.events : [];
11560
+ if (!events2.length || events2.some((candidate) => {
11561
+ const event = object(candidate);
11562
+ const ids2 = strings2(event.sourceMessageIds);
11563
+ return !text2(event.title) || !text2(event.summary) || !ids2.length || ids2.some((id) => !validMessageIds.has(id));
11564
+ })) throw new Error("Extraction requested events but returned empty/invalid evidence or source IDs; correct every event using allowedSourceMessageIds");
11565
+ }
11450
11566
  ));
11451
- const exactKeys = /* @__PURE__ */ new WeakMap();
11452
11567
  const candidates = (Array.isArray(raw.events) ? raw.events : []).map((candidate) => {
11453
11568
  const item = object(candidate);
11454
11569
  const sourceMessageIds = strings2(item.sourceMessageIds).filter((id) => validMessageIds.has(id));
@@ -11490,7 +11605,7 @@ Use project scope for repository decisions, user scope for stable preferences/id
11490
11605
  reason: text2(raw.reason) || (shouldExtract ? "Durable evidence extracted." : "No durable evidence."),
11491
11606
  events: shouldExtract ? events : []
11492
11607
  };
11493
- };
11608
+ }
11494
11609
  projector = async (context) => {
11495
11610
  const eventIds = new Set(context.events.map((event) => event.id));
11496
11611
  const raw = object(await this.callStructured(
@@ -11709,6 +11824,7 @@ Use project scope for repository decisions, user scope for stable preferences/id
11709
11824
  });
11710
11825
  return result;
11711
11826
  } catch (error) {
11827
+ if (error instanceof ExtractionCapacityError && !scope.calls) throw error;
11712
11828
  this.diagnostics?.record(scope, {
11713
11829
  operations: 1,
11714
11830
  operationsSucceeded: 0,
@@ -11776,7 +11892,7 @@ ${JSON_RETRY_INSTRUCTION}${graphRetryFeedback}${topicRetryFeedback}`,
11776
11892
  type: "function",
11777
11893
  function: { name: STRUCTURED_TOOLS[kind].name }
11778
11894
  },
11779
- maxTokens: kind === "dreamingStep" ? TOPIC_OUTPUT_TOKEN_BUDGET : kind === "profileMaintenance" ? Math.max(this.config.maxOutputTokens, Math.min(12e3, 512 + 2 * Array.from(JSON.stringify(payload)).length)) : isTopic ? TOPIC_OUTPUT_TOKEN_BUDGET : isGraph ? GRAPH_OUTPUT_TOKEN_BUDGET : this.config.maxOutputTokens,
11895
+ maxTokens: kind === "dreamingStep" ? TOPIC_OUTPUT_TOKEN_BUDGET : kind === "profileMaintenance" ? Math.max(this.config.maxOutputTokens, Math.min(12e3, 512 + 2 * Array.from(JSON.stringify(payload)).length)) : isTopic ? TOPIC_OUTPUT_TOKEN_BUDGET : isGraph ? GRAPH_OUTPUT_TOKEN_BUDGET : kind === "extractor" ? Math.max(EXTRACTION_OUTPUT_TOKENS, this.config.maxOutputTokens) : this.config.maxOutputTokens,
11780
11896
  sessionId: execution.sessionId,
11781
11897
  purpose: "compaction"
11782
11898
  };
@@ -11800,6 +11916,15 @@ ${JSON_RETRY_INSTRUCTION}${graphRetryFeedback}${topicRetryFeedback}`,
11800
11916
  outputTokenBudget: request.maxTokens
11801
11917
  }) }] }];
11802
11918
  }
11919
+ if (kind === "extractor") {
11920
+ const window = this.contextWindows.get(routeKey) ?? 32768;
11921
+ const input = estimateTokens(JSON.stringify({ system: request.system, tools: request.tools, messages: request.messages }));
11922
+ const reserve = Math.min(request.maxTokens, Math.max(256, Math.floor(window / 4)));
11923
+ if (input + estimateTokens(JSON_RETRY_INSTRUCTION) > Math.min(EXTRACTION_INPUT_TOKENS, window - reserve - 1024)) {
11924
+ throw new ExtractionCapacityError("input");
11925
+ }
11926
+ request.maxTokens = Math.min(request.maxTokens, Math.floor(window - input - 1024));
11927
+ }
11803
11928
  if (isTopic) {
11804
11929
  const estimateInput = () => estimateTokens(JSON.stringify({ system: request.system, tools: request.tools, messages: request.messages }));
11805
11930
  let inputTokens = estimateInput();
@@ -11824,6 +11949,18 @@ ${JSON_RETRY_INSTRUCTION}${graphRetryFeedback}${topicRetryFeedback}`,
11824
11949
  });
11825
11950
  }
11826
11951
  try {
11952
+ if (kind === "extractor") {
11953
+ this.assertExtractionBudget();
11954
+ const budget = this.extractionBudgets.getStore();
11955
+ if (budget) {
11956
+ const reservation = estimateTokens(JSON.stringify({ system: request.system, tools: request.tools, messages: request.messages })) + request.maxTokens;
11957
+ if (budget.reservedTokens + reservation > EXTRACTION_MAX_RESERVED_TOKENS) {
11958
+ throw new Error("Event extraction shared token budget exhausted; original Block remains pending");
11959
+ }
11960
+ budget.calls += 1;
11961
+ budget.reservedTokens += reservation;
11962
+ }
11963
+ }
11827
11964
  await this.consumeStructuredStream(request, assembler);
11828
11965
  } catch (error) {
11829
11966
  if (!noAdapterRetried && isNoAdapter(error)) {
@@ -11847,6 +11984,7 @@ ${JSON_RETRY_INSTRUCTION}${graphRetryFeedback}${topicRetryFeedback}`,
11847
11984
  }
11848
11985
  assertActive?.();
11849
11986
  const finish = assembler.finish;
11987
+ if (kind === "extractor" && finish.kind === "max-tokens") throw new ExtractionCapacityError("output");
11850
11988
  if (isTopic) topicDiagnostics.finishReason = finish.kind;
11851
11989
  if (finish.kind === "error" || finish.kind === "aborted") {
11852
11990
  const failure = new Error(`StrataGate model call failed [${finish.failure.code}]: ${finish.failure.message}`);
@@ -12038,7 +12176,10 @@ Correct the previous Topic output using these validation errors: ${JSON.stringif
12038
12176
  this.ctx.logger.warn(`stratagate-memory ${message}`);
12039
12177
  }
12040
12178
  async consumeStructuredStream(request, assembler) {
12041
- const timeoutMs = this.config.structuredTaskTimeoutMs ?? DEFAULT_STRUCTURED_TIMEOUT_MS;
12179
+ const timeoutMs = Math.max(1, Math.min(
12180
+ this.config.structuredTaskTimeoutMs ?? DEFAULT_STRUCTURED_TIMEOUT_MS,
12181
+ (this.extractionBudgets.getStore()?.deadline ?? Infinity) - Date.now()
12182
+ ));
12042
12183
  const controller = new AbortController();
12043
12184
  const externalSignal = this.sessions.getStore()?.signal;
12044
12185
  const aborted = new Promise((_resolve, reject) => {