stratagate-dsh 0.3.40 → 0.3.42
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +732 -732
- package/CONTRIBUTING.md +99 -88
- package/CONTRIBUTING.zh-CN.md +90 -88
- package/LICENSE +21 -21
- package/README.md +510 -489
- package/README.zh-CN.md +514 -493
- package/benchmarks/locomo-conv26-r8-final.json +146 -146
- package/cordis.patch.yml +16 -16
- package/dist/client.d.ts +1 -1
- package/dist/client.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/plugin.js +134 -51
- package/dist/plugin.js.map +1 -1
- package/dist/repair-profile.js +0 -0
- package/dist/repair-profile.js.map +1 -1
- package/docs/ARCHITECTURE.md +282 -282
- package/docs/DSH.md +337 -337
- package/docs/DSH.zh-CN.md +325 -325
- package/docs/EVALUATION.md +189 -189
- package/docs/EXTERNAL_MEMORY_IMPORT.zh-CN.md +99 -99
- package/docs/MEMORY_TOPICS.zh-CN.md +51 -51
- package/docs/benchmarks/issue-128-windows-review.json +104 -104
- package/docs/benchmarks/issue-128-windows.json +103 -103
- package/docs/database-access.md +32 -32
- package/docs/development-validation.md +20 -0
- package/docs/dreaming-agent.md +69 -69
- package/docs/event-extractor.md +48 -48
- package/docs/feedback.md +50 -50
- package/docs/issue-128-lease-cache-review-followup.md +55 -55
- package/docs/issue-128-review-followup.md +58 -58
- package/docs/knowledge-graph.md +38 -38
- package/docs/npm-release.md +55 -53
- package/docs/surface-maintenance.md +36 -36
- package/docs/user-operation-priority.md +46 -39
- package/docs/validation/issue119-after.json +30 -0
- package/docs/validation/issue119-before.json +94 -0
- package/docs/validation/issue119.md +41 -41
- package/docs/validation/workspace-cleanup-20261020.md +36 -36
- package/package.json +256 -255
- package/packages/core/examples/basic.ts +87 -87
- package/packages/core/src/blocks.ts +442 -442
- package/packages/core/src/weights.ts +21 -21
- package/screenshots.json +8 -8
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/dsh-host-resolution.ts","../src/bootstrap.ts"],"sourcesContent":["import { existsSync, readFileSync } from 'node:fs'\
|
|
1
|
+
{"version":3,"sources":["../src/dsh-host-resolution.ts","../src/bootstrap.ts"],"sourcesContent":["import { existsSync, readFileSync } from 'node:fs'\nimport { createRequire, registerHooks } from 'node:module'\nimport { dirname, isAbsolute, join, relative } from 'node:path'\nimport { fileURLToPath, pathToFileURL } from 'node:url'\n\nconst HOST_PACKAGES = [\n '@deepseek-ai/cordis',\n '@deepseek-ai/dsh-agent-default-model',\n '@deepseek-ai/dsh-client-ui-conversation',\n '@deepseek-ai/dsh-llm',\n '@deepseek-ai/dsh-native-command',\n '@deepseek-ai/dsh-session',\n '@deepseek-ai/dsh-session-format',\n '@deepseek-ai/dsh-session-format-catalog',\n '@deepseek-ai/dsh-session-format-v0-to-v1',\n '@deepseek-ai/dsh-settings',\n '@deepseek-ai/dsh-system-prompt',\n '@deepseek-ai/dsh-tools',\n '@deepseek-ai/schemastery',\n] as const\n\nconst installedHooks = Symbol.for('stratagate.dsh-host-resolution.v1')\n\nfunction isWithin(path: string, root: string): boolean {\n const child = relative(root, path)\n return child === '' || (!child.startsWith('..') && !isAbsolute(child))\n}\n\nfunction profileDirectory(start: string): string | undefined {\n let directory = start\n for (;;) {\n const manifestPath = join(directory, 'package.json')\n if (existsSync(manifestPath)) {\n try {\n const manifest = JSON.parse(readFileSync(manifestPath, 'utf8')) as { dsh?: { profile?: unknown } }\n if (manifest.dsh?.profile && typeof manifest.dsh.profile === 'object') return directory\n } catch {}\n }\n const parent = dirname(directory)\n if (parent === directory) return undefined\n directory = parent\n }\n}\n\nfunction hostPackage(specifier: string): boolean {\n return HOST_PACKAGES.some((name) => specifier === name || specifier.startsWith(`${name}/`))\n}\n\n/**\n * Force only StrataGate's DSH imports through the installation-owned fallback\n * created by DSH. The hook neither deletes nor rewrites the profile's package\n * tree, and imports belonging to the host or other plugins are untouched.\n */\nexport function installDshHostResolution(moduleUrl: string): boolean {\n const packageDirectory = dirname(dirname(fileURLToPath(moduleUrl)))\n const profile = profileDirectory(packageDirectory)\n if (!profile) return false\n\n const globalState = globalThis as typeof globalThis & { [installedHooks]?: Set<string> }\n const installed = globalState[installedHooks] ??= new Set<string>()\n const key = `${profile}\\u0000${packageDirectory}`\n if (installed.has(key)) return true\n\n const fallbackModules = join(dirname(profile), 'node_modules')\n const hostRequire = createRequire(join(fallbackModules, '.stratagate-host-resolution.cjs'))\n registerHooks({\n resolve(specifier, context, nextResolve) {\n const parent = context.parentURL?.startsWith('file:') ? fileURLToPath(context.parentURL) : undefined\n if (!hostPackage(specifier) || !parent || !isWithin(parent, packageDirectory)) {\n return nextResolve(specifier, context)\n }\n try {\n return { url: pathToFileURL(hostRequire.resolve(specifier)).href, shortCircuit: true }\n } catch (cause) {\n if (specifier === '@deepseek-ai/dsh-session-format-v0-to-v1'\n || specifier === '@deepseek-ai/dsh-session-format-catalog') {\n return nextResolve(specifier, context)\n }\n throw new Error(\n `StrataGate cannot resolve host-provided DSH package ${JSON.stringify(specifier)} from ${fallbackModules}. `\n + 'Start StrataGate through the DSH profile launcher so the host can prepare its module fallback.',\n { cause },\n )\n }\n },\n })\n installed.add(key)\n return true\n}\n","import { installDshHostResolution } from './dsh-host-resolution.js'\n\n// DSH prepares its installation-owned module fallback before it imports a\n// profile bundle. Install the narrowly-scoped resolver first, then load the\n// implementation so stale profile-local peers cannot win ESM resolution.\ninstallDshHostResolution(import.meta.url)\n\n// This file and src/index.ts are separate tsup entries. The runtime path is\n// intentionally external so the implementation is instantiated only after\n// the resolver above is active.\n// @ts-expect-error dist/plugin.js is emitted from src/index.ts during build.\nconst plugin = await import('./plugin.js') as typeof import('./index.js')\n\nexport const name = plugin.name\nexport const inject = plugin.inject\nexport const STRATAGATE_SETTINGS_NAMESPACE = plugin.STRATAGATE_SETTINGS_NAMESPACE\nexport const Config = plugin.Config\nexport const apply = plugin.apply\nexport type PluginConfig = import('./index.js').PluginConfig\n"],"mappings":";AAAA,SAAS,YAAY,oBAAoB;AACzC,SAAS,eAAe,qBAAqB;AAC7C,SAAS,SAAS,YAAY,MAAM,gBAAgB;AACpD,SAAS,eAAe,qBAAqB;AAE7C,IAAM,gBAAgB;AAAA,EACpB;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF;AAEA,IAAM,iBAAiB,uBAAO,IAAI,mCAAmC;AAErE,SAAS,SAAS,MAAc,MAAuB;AACrD,QAAM,QAAQ,SAAS,MAAM,IAAI;AACjC,SAAO,UAAU,MAAO,CAAC,MAAM,WAAW,IAAI,KAAK,CAAC,WAAW,KAAK;AACtE;AAEA,SAAS,iBAAiB,OAAmC;AAC3D,MAAI,YAAY;AAChB,aAAS;AACP,UAAM,eAAe,KAAK,WAAW,cAAc;AACnD,QAAI,WAAW,YAAY,GAAG;AAC5B,UAAI;AACF,cAAM,WAAW,KAAK,MAAM,aAAa,cAAc,MAAM,CAAC;AAC9D,YAAI,SAAS,KAAK,WAAW,OAAO,SAAS,IAAI,YAAY,SAAU,QAAO;AAAA,MAChF,QAAQ;AAAA,MAAC;AAAA,IACX;AACA,UAAM,SAAS,QAAQ,SAAS;AAChC,QAAI,WAAW,UAAW,QAAO;AACjC,gBAAY;AAAA,EACd;AACF;AAEA,SAAS,YAAY,WAA4B;AAC/C,SAAO,cAAc,KAAK,CAACA,UAAS,cAAcA,SAAQ,UAAU,WAAW,GAAGA,KAAI,GAAG,CAAC;AAC5F;AAOO,SAAS,yBAAyB,WAA4B;AACnE,QAAM,mBAAmB,QAAQ,QAAQ,cAAc,SAAS,CAAC,CAAC;AAClE,QAAM,UAAU,iBAAiB,gBAAgB;AACjD,MAAI,CAAC,QAAS,QAAO;AAErB,QAAM,cAAc;AACpB,QAAM,YAAY,YAAY,cAAc,MAAM,oBAAI,IAAY;AAClE,QAAM,MAAM,GAAG,OAAO,KAAS,gBAAgB;AAC/C,MAAI,UAAU,IAAI,GAAG,EAAG,QAAO;AAE/B,QAAM,kBAAkB,KAAK,QAAQ,OAAO,GAAG,cAAc;AAC7D,QAAM,cAAc,cAAc,KAAK,iBAAiB,iCAAiC,CAAC;AAC1F,gBAAc;AAAA,IACZ,QAAQ,WAAW,SAAS,aAAa;AACvC,YAAM,SAAS,QAAQ,WAAW,WAAW,OAAO,IAAI,cAAc,QAAQ,SAAS,IAAI;AAC3F,UAAI,CAAC,YAAY,SAAS,KAAK,CAAC,UAAU,CAAC,SAAS,QAAQ,gBAAgB,GAAG;AAC7E,eAAO,YAAY,WAAW,OAAO;AAAA,MACvC;AACA,UAAI;AACF,eAAO,EAAE,KAAK,cAAc,YAAY,QAAQ,SAAS,CAAC,EAAE,MAAM,cAAc,KAAK;AAAA,MACvF,SAAS,OAAO;AACd,YAAI,cAAc,8CACb,cAAc,2CAA2C;AAC5D,iBAAO,YAAY,WAAW,OAAO;AAAA,QACvC;AACA,cAAM,IAAI;AAAA,UACR,uDAAuD,KAAK,UAAU,SAAS,CAAC,SAAS,eAAe;AAAA,UAExG,EAAE,MAAM;AAAA,QACV;AAAA,MACF;AAAA,IACF;AAAA,EACF,CAAC;AACD,YAAU,IAAI,GAAG;AACjB,SAAO;AACT;;;ACnFA,yBAAyB,YAAY,GAAG;AAMxC,IAAM,SAAS,MAAM,OAAO,aAAa;AAElC,IAAM,OAAO,OAAO;AACpB,IAAM,SAAS,OAAO;AACtB,IAAM,gCAAgC,OAAO;AAC7C,IAAM,SAAS,OAAO;AACtB,IAAM,QAAQ,OAAO;","names":["name"]}
|
package/dist/plugin.js
CHANGED
|
@@ -10529,6 +10529,7 @@ var STRATAGATE_MESSAGE_SOURCE_KIND = "plugin:stratagate-memory";
|
|
|
10529
10529
|
var DSH_RUNTIME_RANGE = "0.1.2-rc.1 || >=0.1.5-rc.2 <0.3.0-0";
|
|
10530
10530
|
var MODERN_API_RANGE = ">=0.1.7-rc.1";
|
|
10531
10531
|
var SESSION_FORMAT_RUNTIME_RANGE = ">=0.2.0-0";
|
|
10532
|
+
var DSH_022_VENDOR_RUNTIME_RANGE = ">=0.2.2-0 <0.3.0-0";
|
|
10532
10533
|
var SEMVER_OPTIONS = { includePrerelease: true };
|
|
10533
10534
|
function assertDshMethods(scope, value, methods) {
|
|
10534
10535
|
const target = value;
|
|
@@ -10579,13 +10580,14 @@ function classifyDshRuntime(packageVersions) {
|
|
|
10579
10580
|
const sameDshFamily = CORE_RUNTIME_PACKAGES.filter((name2) => name2.startsWith("@deepseek-ai/dsh-")).every((name2) => packageVersions[name2] === dshVersion);
|
|
10580
10581
|
const sameSessionFormatFamily = !satisfies(dshVersion, SESSION_FORMAT_RUNTIME_RANGE, SEMVER_OPTIONS) || SESSION_FORMAT_PACKAGES.every((name2) => packageVersions[name2] === dshVersion);
|
|
10581
10582
|
const modernApi = satisfies(dshVersion, MODERN_API_RANGE, SEMVER_OPTIONS);
|
|
10582
|
-
const
|
|
10583
|
+
const dsh022VendorFamily = satisfies(dshVersion, DSH_022_VENDOR_RUNTIME_RANGE, SEMVER_OPTIONS);
|
|
10584
|
+
const compatibleVendors = modernApi ? dsh022VendorFamily ? isCompatiblePatch(packageVersions["@deepseek-ai/cordis"], 4, 1, 0) && isCompatiblePatch(packageVersions["@deepseek-ai/schemastery"], 3, 19, 0) : isCompatiblePatch(packageVersions["@deepseek-ai/cordis"], 4, 0, 4) && isCompatiblePatch(packageVersions["@deepseek-ai/schemastery"], 3, 18, 4) : packageVersions["@deepseek-ai/cordis"] === "4.0.2" && packageVersions["@deepseek-ai/schemastery"] === "3.18.2";
|
|
10583
10585
|
if (satisfies(dshVersion, DSH_RUNTIME_RANGE, SEMVER_OPTIONS) && sameDshFamily && sameSessionFormatFamily && compatibleVendors) {
|
|
10584
10586
|
return { cliVersion: dshVersion === "0.1.5-rc.2" ? "0.1.5-rc.1" : dshVersion, packageVersions };
|
|
10585
10587
|
}
|
|
10586
10588
|
const found = RUNTIME_PACKAGES.map((name2) => `${name2}@${packageVersions[name2]}`).join(", ");
|
|
10587
10589
|
throw new Error(
|
|
10588
|
-
`StrataGate cannot start because this DSH profile resolves an unsupported or mixed core runtime. Resolved: ${found}. Supported DSH versions: ${DSH_RUNTIME_RANGE} (including prereleases). Core DSH packages must share one version; DSH 0.2.0 and later also require matching Session Format packages.
|
|
10590
|
+
`StrataGate cannot start because this DSH profile resolves an unsupported or mixed core runtime. Resolved: ${found}. Supported DSH versions: ${DSH_RUNTIME_RANGE} (including prereleases). Core DSH packages must share one version; DSH 0.2.0 and later also require matching Session Format packages. DSH 0.2.2 and later require Cordis 4.1.x from 4.1.0 and Schemastery 3.19.x from 3.19.0; earlier modern hosts require Cordis 4.0.x from 4.0.4 and Schemastery 3.18.x from 3.18.4, while older hosts use 4.0.2 and 3.18.2. Reinstall or update stratagate-dsh through \`dsh plugin --profile <name> add <package>\` so the host supplies its peers.`
|
|
10589
10591
|
);
|
|
10590
10592
|
}
|
|
10591
10593
|
function assertCompatibleDshRuntime() {
|
|
@@ -13729,6 +13731,7 @@ var DRAIN_BASE_BACKOFF_MS = 2e3;
|
|
|
13729
13731
|
var DRAIN_MAX_BACKOFF_MS = 6e4;
|
|
13730
13732
|
var BACKGROUND_WORKER_INITIAL_DELAY_MS = 250;
|
|
13731
13733
|
var BACKGROUND_WORKER_INTERVAL_MS = 3e3;
|
|
13734
|
+
var SURFACE_MAINTENANCE_MAX_ATTEMPTS = 3;
|
|
13732
13735
|
function graphProjectionCanRun(job, now = Date.now()) {
|
|
13733
13736
|
return job.attempts < DERIVATION_MAX_ATTEMPTS && (job.status === "pending" || job.status === "failed" && job.nextRetryAt != null && Date.parse(job.nextRetryAt) <= now);
|
|
13734
13737
|
}
|
|
@@ -14442,8 +14445,19 @@ var StrataGateRuntime = class _StrataGateRuntime {
|
|
|
14442
14445
|
this.scheduleSurfaceMaintenance(surface);
|
|
14443
14446
|
} else if (event.type === "request/context") {
|
|
14444
14447
|
const limit = surfaceCheckpointLimit(session);
|
|
14445
|
-
|
|
14446
|
-
|
|
14448
|
+
const capacityKey = surfaceCapacityKey(session);
|
|
14449
|
+
const limitChanged = surface.checkpointLimit !== limit;
|
|
14450
|
+
const capacityChanged = surface.capacityKey !== void 0 && surface.capacityKey !== capacityKey;
|
|
14451
|
+
surface.checkpointLimit = limit;
|
|
14452
|
+
surface.capacityKey = capacityKey;
|
|
14453
|
+
if (limitChanged || capacityChanged || surface.blockedAt !== void 0 && surface.blockedAt !== surfaceMaintenanceInput(surface)) {
|
|
14454
|
+
this.markSurfaceDirty(session, surface.memory);
|
|
14455
|
+
}
|
|
14456
|
+
} else if (event.type === "request/header") {
|
|
14457
|
+
const capacityKey = surfaceCapacityKey(session);
|
|
14458
|
+
const capacityChanged = surface.capacityKey !== void 0 && surface.capacityKey !== capacityKey;
|
|
14459
|
+
surface.capacityKey = capacityKey;
|
|
14460
|
+
if (capacityChanged || surface.blockedAt !== void 0 && surface.blockedAt !== surfaceMaintenanceInput(surface)) {
|
|
14447
14461
|
this.markSurfaceDirty(session, surface.memory);
|
|
14448
14462
|
}
|
|
14449
14463
|
} 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)) {
|
|
@@ -15115,18 +15129,9 @@ var StrataGateRuntime = class _StrataGateRuntime {
|
|
|
15115
15129
|
if (endTurn === null || hostCompactionActive(session)) return false;
|
|
15116
15130
|
const sourceEventSeqs = sources2 ?? sealedSurfaceSeqs(session, endTurn, block);
|
|
15117
15131
|
if (!sourceEventSeqs) return false;
|
|
15118
|
-
const start = sourceEventSeqs[0];
|
|
15119
|
-
const end = sourceEventSeqs.at(-1);
|
|
15120
|
-
if (start === void 0 || end === void 0) return false;
|
|
15121
15132
|
const selected = selectCompressedBlockSurface(session, sourceEventSeqs, block, context);
|
|
15122
15133
|
if (!selected) return false;
|
|
15123
|
-
session.
|
|
15124
|
-
content: [{ type: "text", text: selected }],
|
|
15125
|
-
source: dshMessageSource()
|
|
15126
|
-
}), {
|
|
15127
|
-
surfaceOp: dshReplaceSurfaceOp(start, end),
|
|
15128
|
-
sourceEventSeqs
|
|
15129
|
-
});
|
|
15134
|
+
appendBlockSurface(session, block.id, selected, sourceEventSeqs);
|
|
15130
15135
|
this.surfaceFlushPending.add(session);
|
|
15131
15136
|
levels.set(block.id, checkpointLevel(selected) ?? context.level);
|
|
15132
15137
|
return true;
|
|
@@ -15179,13 +15184,7 @@ var StrataGateRuntime = class _StrataGateRuntime {
|
|
|
15179
15184
|
);
|
|
15180
15185
|
if (!text3) continue;
|
|
15181
15186
|
if (node.text !== text3) {
|
|
15182
|
-
session.
|
|
15183
|
-
content: [{ type: "text", text: text3 }],
|
|
15184
|
-
source: dshMessageSource()
|
|
15185
|
-
}), {
|
|
15186
|
-
surfaceOp: dshReplaceSurfaceOp(node.seq, node.seq),
|
|
15187
|
-
sourceEventSeqs: [node.seq]
|
|
15188
|
-
});
|
|
15187
|
+
appendBlockSurface(session, block.id, text3, [node.seq]);
|
|
15189
15188
|
this.surfaceFlushPending.add(session);
|
|
15190
15189
|
changed = true;
|
|
15191
15190
|
}
|
|
@@ -15207,12 +15206,15 @@ var StrataGateRuntime = class _StrataGateRuntime {
|
|
|
15207
15206
|
const node = current.get(block.id);
|
|
15208
15207
|
const endTurn = node ? null : dshTurnAtBlockEnd(session, block);
|
|
15209
15208
|
const seqs = node ? [node.seq] : endTurn === null ? null : sealedSurfaceSeqs(session, endTurn, block);
|
|
15210
|
-
if (!seqs?.length
|
|
15209
|
+
if (!seqs?.length || !safeBlockSurfaceRange(session, block.id, seqs)) return {
|
|
15210
|
+
status: "blocked",
|
|
15211
|
+
reason: "source-unavailable",
|
|
15212
|
+
message: "\u8FD9\u6BB5\u5386\u53F2\u5DF2\u88AB\u6574\u7406\uFF0C\u6216\u5939\u6709\u9700\u8981\u4FDD\u7559\u7684\u7CFB\u7EDF\u3001\u5DE5\u5177\u6216\u63D2\u4EF6\u5185\u5BB9\uFF0C\u65E0\u6CD5\u5728\u8FD9\u91CC\u5207\u6362\u5C42\u7EA7\u3002\u53EF\u4EE5\u8BA9\u52A9\u624B\u67E5\u627E\u8FD9\u6BB5\u5386\u53F2\u3002"
|
|
15213
|
+
};
|
|
15211
15214
|
const relief = [];
|
|
15212
15215
|
const window = session.requestContext?.()?.contextWindow;
|
|
15213
15216
|
if (node?.text !== text3 && typeof window === "number" && Number.isInteger(window) && window > 0) {
|
|
15214
|
-
const
|
|
15215
|
-
const end = seqs.at(-1);
|
|
15217
|
+
const replaced = new Set(seqs);
|
|
15216
15218
|
let before = 0;
|
|
15217
15219
|
let removed = 0;
|
|
15218
15220
|
for (const seq of session.surface.nodes) {
|
|
@@ -15220,7 +15222,7 @@ var StrataGateRuntime = class _StrataGateRuntime {
|
|
|
15220
15222
|
const message = event && session.deriveEventMessage(event);
|
|
15221
15223
|
const tokens = message ? estimateTokens(JSON.stringify(message)) : 0;
|
|
15222
15224
|
before += tokens;
|
|
15223
|
-
if (seq
|
|
15225
|
+
if (replaced.has(seq)) removed += tokens;
|
|
15224
15226
|
}
|
|
15225
15227
|
const header = session.requestHeader?.();
|
|
15226
15228
|
const reserved = (header?.config.maxTokens ?? 0) + (header?.tools?.length ? estimateTokens(JSON.stringify(header.tools)) : 0);
|
|
@@ -15230,7 +15232,7 @@ var StrataGateRuntime = class _StrataGateRuntime {
|
|
|
15230
15232
|
const candidates = memory.listBlocks().filter((candidate) => candidate.id !== block.id && candidate.threadId === block.threadId && candidate.processingStatus === "ready" && candidate.lastLiftedBy !== "user").sort((a, b) => a.sequence - b.sequence);
|
|
15231
15233
|
for (const candidate of candidates) {
|
|
15232
15234
|
const checkpoint = current.get(candidate.id);
|
|
15233
|
-
if (!checkpoint ||
|
|
15235
|
+
if (!checkpoint || replaced.has(checkpoint.seq)) continue;
|
|
15234
15236
|
const level = checkpointLevel(checkpoint.text);
|
|
15235
15237
|
if (level === void 0 || level === 0) continue;
|
|
15236
15238
|
const original = this.cachedCheckpointTokens(session, checkpoint);
|
|
@@ -15290,22 +15292,13 @@ var StrataGateRuntime = class _StrataGateRuntime {
|
|
|
15290
15292
|
}
|
|
15291
15293
|
const { text: text3, seqs, relief } = plan;
|
|
15292
15294
|
for (const replacement of relief) {
|
|
15293
|
-
session.
|
|
15294
|
-
content: [{ type: "text", text: replacement.text }],
|
|
15295
|
-
source: dshMessageSource()
|
|
15296
|
-
}), { surfaceOp: dshReplaceSurfaceOp(replacement.seq, replacement.seq), sourceEventSeqs: [replacement.seq] });
|
|
15295
|
+
appendBlockSurface(session, replacement.id, replacement.text, [replacement.seq]);
|
|
15297
15296
|
levels.set(replacement.id, replacement.level);
|
|
15298
15297
|
this.surfaceFlushPending.add(session);
|
|
15299
15298
|
}
|
|
15300
15299
|
const node = currentBlockSurfaceMessages(session).get(block.id);
|
|
15301
15300
|
if (node?.text !== text3) {
|
|
15302
|
-
|
|
15303
|
-
const end = seqs.at(-1);
|
|
15304
|
-
if (start === void 0 || end === void 0) return false;
|
|
15305
|
-
session.append("user/message", createUserMessage2({
|
|
15306
|
-
content: [{ type: "text", text: text3 }],
|
|
15307
|
-
source: dshMessageSource()
|
|
15308
|
-
}), { surfaceOp: dshReplaceSurfaceOp(start, end), sourceEventSeqs: seqs });
|
|
15301
|
+
appendBlockSurface(session, block.id, text3, seqs);
|
|
15309
15302
|
this.surfaceFlushPending.add(session);
|
|
15310
15303
|
}
|
|
15311
15304
|
levels.set(block.id, context.level);
|
|
@@ -15357,13 +15350,19 @@ var StrataGateRuntime = class _StrataGateRuntime {
|
|
|
15357
15350
|
return tokens;
|
|
15358
15351
|
}
|
|
15359
15352
|
/** Finish deferred sealing or user expansion only after the target surface is applied. */
|
|
15360
|
-
async syncPendingRetrySurface(session, memory, userOnly = false) {
|
|
15353
|
+
async syncPendingRetrySurface(session, memory, userOnly = false, force = false) {
|
|
15354
|
+
if (!force && this.surfaceRetryBlocked(session, memory)) return;
|
|
15361
15355
|
const levels = /* @__PURE__ */ new Map();
|
|
15362
|
-
|
|
15363
|
-
|
|
15364
|
-
|
|
15365
|
-
|
|
15366
|
-
|
|
15356
|
+
try {
|
|
15357
|
+
const completed = this.applyPendingRetrySurface(session, memory, levels, userOnly);
|
|
15358
|
+
if (completed.length === 0) return;
|
|
15359
|
+
await this.commitSurfaceLevels(memory, levels);
|
|
15360
|
+
await this.flushSurface(session);
|
|
15361
|
+
this.completeSurfaceRequests(completed);
|
|
15362
|
+
} catch (error) {
|
|
15363
|
+
this.recordSurfaceFailure(session, memory, error);
|
|
15364
|
+
throw error;
|
|
15365
|
+
}
|
|
15367
15366
|
}
|
|
15368
15367
|
applyPendingRetrySurface(session, memory, levels, userOnly = false, recoverable) {
|
|
15369
15368
|
const namespace = this.namespaceFor(session);
|
|
@@ -15427,6 +15426,7 @@ var StrataGateRuntime = class _StrataGateRuntime {
|
|
|
15427
15426
|
/** Read-only in the common case; render only explicit lifts or oversized checkpoints. */
|
|
15428
15427
|
async syncRequestSurface(session, memory) {
|
|
15429
15428
|
if (hostCompactionActive(session)) return;
|
|
15429
|
+
if (this.surfaceRetryBlocked(session, memory)) return;
|
|
15430
15430
|
const current = currentBlockSurfaceMessages(session);
|
|
15431
15431
|
const limit = surfaceCheckpointLimit(session);
|
|
15432
15432
|
const repair = /* @__PURE__ */ new Set();
|
|
@@ -15455,20 +15455,61 @@ var StrataGateRuntime = class _StrataGateRuntime {
|
|
|
15455
15455
|
}
|
|
15456
15456
|
markSurfaceDirty(session, memory) {
|
|
15457
15457
|
if (this.closed) return;
|
|
15458
|
+
const state = this.ensureSurfaceMaintenanceState(session, memory);
|
|
15459
|
+
state.dirty = true;
|
|
15460
|
+
state.generation += 1;
|
|
15461
|
+
if (state.blockedAt !== void 0 && state.blockedAt !== surfaceMaintenanceInput(state)) {
|
|
15462
|
+
delete state.blockedAt;
|
|
15463
|
+
state.failures = 0;
|
|
15464
|
+
}
|
|
15465
|
+
this.scheduleSurfaceMaintenance(state);
|
|
15466
|
+
}
|
|
15467
|
+
ensureSurfaceMaintenanceState(session, memory) {
|
|
15458
15468
|
const key = String(session.id);
|
|
15459
15469
|
let state = this.surfaceMaintenance.get(key);
|
|
15460
15470
|
if (!state || state.session !== session || state.memory !== memory) {
|
|
15461
15471
|
if (state?.timer) clearTimeout(state.timer);
|
|
15462
15472
|
if (state) state.generation += 1;
|
|
15463
|
-
state = {
|
|
15473
|
+
state = {
|
|
15474
|
+
session,
|
|
15475
|
+
memory,
|
|
15476
|
+
dirty: true,
|
|
15477
|
+
generation: 0,
|
|
15478
|
+
busy: hostTurnActive(session),
|
|
15479
|
+
failures: 0,
|
|
15480
|
+
checkpointLimit: surfaceCheckpointLimit(session)
|
|
15481
|
+
};
|
|
15464
15482
|
this.surfaceMaintenance.set(key, state);
|
|
15465
15483
|
}
|
|
15484
|
+
return state;
|
|
15485
|
+
}
|
|
15486
|
+
clearSurfaceFailure(session, memory) {
|
|
15487
|
+
const state = this.surfaceMaintenance.get(String(session.id));
|
|
15488
|
+
if (state?.session !== session || state.memory !== memory) return;
|
|
15489
|
+
delete state.blockedAt;
|
|
15490
|
+
state.failures = 0;
|
|
15491
|
+
}
|
|
15492
|
+
surfaceRetryBlocked(session, memory) {
|
|
15493
|
+
const state = this.surfaceMaintenance.get(String(session.id));
|
|
15494
|
+
if (state?.session !== session || state.memory !== memory || state.blockedAt === void 0) return false;
|
|
15495
|
+
if (state.blockedAt !== surfaceMaintenanceInput(state)) {
|
|
15496
|
+
this.clearSurfaceFailure(session, memory);
|
|
15497
|
+
return false;
|
|
15498
|
+
}
|
|
15499
|
+
return true;
|
|
15500
|
+
}
|
|
15501
|
+
recordSurfaceFailure(session, memory, error) {
|
|
15502
|
+
const state = this.ensureSurfaceMaintenanceState(session, memory);
|
|
15466
15503
|
state.dirty = true;
|
|
15467
|
-
state.
|
|
15468
|
-
|
|
15504
|
+
state.failures += 1;
|
|
15505
|
+
if (surfaceFailureIsPermanent(error) || state.failures >= SURFACE_MAINTENANCE_MAX_ATTEMPTS) {
|
|
15506
|
+
state.blockedAt = surfaceMaintenanceInput(state);
|
|
15507
|
+
if (state.timer) clearTimeout(state.timer);
|
|
15508
|
+
state.timer = void 0;
|
|
15509
|
+
}
|
|
15469
15510
|
}
|
|
15470
15511
|
scheduleSurfaceMaintenance(state, delay2 = 25) {
|
|
15471
|
-
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;
|
|
15512
|
+
if (this.closed || this.surfaceMaintenance.get(String(state.session.id)) !== state || !state.dirty || state.blockedAt !== void 0 || state.busy || state.timer || state.run || this.surfaceAssemblyReaders.get(state.session)) return;
|
|
15472
15513
|
state.timer = setTimeout(() => {
|
|
15473
15514
|
state.timer = void 0;
|
|
15474
15515
|
void this.runSurfaceMaintenance(state.session);
|
|
@@ -15482,7 +15523,7 @@ var StrataGateRuntime = class _StrataGateRuntime {
|
|
|
15482
15523
|
if (state.run) return state.run;
|
|
15483
15524
|
if (state.timer) clearTimeout(state.timer);
|
|
15484
15525
|
state.timer = void 0;
|
|
15485
|
-
if (!state.dirty || state.busy || this.surfaceAssemblyReaders.get(session) || hostTurnActive(session) || hostCompactionActive(session)) return;
|
|
15526
|
+
if (!state.dirty || state.blockedAt !== void 0 || state.busy || this.surfaceAssemblyReaders.get(session) || hostTurnActive(session) || hostCompactionActive(session)) return;
|
|
15486
15527
|
const generation = state.generation;
|
|
15487
15528
|
const run = (async () => {
|
|
15488
15529
|
await this.drainTails.get(String(session.id));
|
|
@@ -15512,6 +15553,11 @@ var StrataGateRuntime = class _StrataGateRuntime {
|
|
|
15512
15553
|
if (error instanceof StorageConflictError) {
|
|
15513
15554
|
await state.memory.reloadFromStorage().catch((reloadError) => this.onIngestError(reloadError));
|
|
15514
15555
|
}
|
|
15556
|
+
if (state.generation !== generation) {
|
|
15557
|
+
state.failures = 0;
|
|
15558
|
+
} else if (surfaceFailureIsPermanent(error) || state.failures >= SURFACE_MAINTENANCE_MAX_ATTEMPTS) {
|
|
15559
|
+
state.blockedAt = surfaceMaintenanceInput(state);
|
|
15560
|
+
}
|
|
15515
15561
|
this.onIngestError(error);
|
|
15516
15562
|
}).finally(() => {
|
|
15517
15563
|
state.run = void 0;
|
|
@@ -16643,10 +16689,11 @@ var StrataGateRuntime = class _StrataGateRuntime {
|
|
|
16643
16689
|
if (plan.status === "blocked") throw Object.assign(new Error(plan.message), { code: "BLOCK_SELECTION_REJECTED" });
|
|
16644
16690
|
}
|
|
16645
16691
|
const result = await memory.expandBlock(id, target, "user");
|
|
16692
|
+
if (session && this.namespaceFor(session) === key) this.clearSurfaceFailure(session, memory);
|
|
16646
16693
|
this.pendingSurfaceSync.delete(`${key}\0${id}`);
|
|
16647
16694
|
this.queueSurfaceSync(key, id, "user-expansion");
|
|
16648
16695
|
if (session && this.namespaceFor(session) === key) {
|
|
16649
|
-
await this.syncPendingRetrySurface(session, memory);
|
|
16696
|
+
await this.syncPendingRetrySurface(session, memory, false, true);
|
|
16650
16697
|
}
|
|
16651
16698
|
return {
|
|
16652
16699
|
...result,
|
|
@@ -17233,8 +17280,9 @@ function sealedSurfaceSeqs(session, endTurn, block) {
|
|
|
17233
17280
|
start: open2.start,
|
|
17234
17281
|
end: event.seq,
|
|
17235
17282
|
nodes: currentSurface.filter((seq) => {
|
|
17236
|
-
if (seq > open2.start && seq < event.seq) return true;
|
|
17237
17283
|
const current = session.eventAt(seq);
|
|
17284
|
+
if (!replaceableBlockEvent(current, block.id)) return false;
|
|
17285
|
+
if (seq > open2.start && seq < event.seq) return true;
|
|
17238
17286
|
return current?.type === "tool/result" && current.surfaceOp !== "append" && current.sourceEventSeqs?.some((source) => source > open2.start && source < event.seq);
|
|
17239
17287
|
})
|
|
17240
17288
|
});
|
|
@@ -17258,7 +17306,42 @@ function sealedSurfaceSeqs(session, endTurn, block) {
|
|
|
17258
17306
|
const startIndex = currentSurface.indexOf(start);
|
|
17259
17307
|
const endIndex = currentSurface.indexOf(end);
|
|
17260
17308
|
if (startIndex < 0 || endIndex < startIndex) return null;
|
|
17261
|
-
|
|
17309
|
+
const range = currentSurface.slice(startIndex, endIndex + 1);
|
|
17310
|
+
return safeBlockSurfaceRange(session, block.id, range) ? range : null;
|
|
17311
|
+
}
|
|
17312
|
+
function replaceableBlockEvent(event, blockId) {
|
|
17313
|
+
if (event?.type === "assistant/message" || event?.type === "tool/result") return true;
|
|
17314
|
+
return event?.type === "user/message" && (event.data.source.kind === "user" || blockSurfaceMessage(event)?.id === blockId);
|
|
17315
|
+
}
|
|
17316
|
+
function safeBlockSurfaceRange(session, blockId, seqs) {
|
|
17317
|
+
if (seqs.length === 0) return false;
|
|
17318
|
+
const start = session.surface.nodes.indexOf(seqs[0]);
|
|
17319
|
+
return start >= 0 && seqs.every((seq, index) => session.surface.nodes[start + index] === seq && replaceableBlockEvent(session.eventAt(seq), blockId));
|
|
17320
|
+
}
|
|
17321
|
+
function appendBlockSurface(session, blockId, text3, seqs) {
|
|
17322
|
+
if (!safeBlockSurfaceRange(session, blockId, seqs)) {
|
|
17323
|
+
throw Object.assign(new Error("StrataGate surface replacement no longer owns its complete source range"), { retryable: false });
|
|
17324
|
+
}
|
|
17325
|
+
session.append("user/message", createUserMessage2({
|
|
17326
|
+
content: [{ type: "text", text: text3 }],
|
|
17327
|
+
source: dshMessageSource()
|
|
17328
|
+
}), {
|
|
17329
|
+
surfaceOp: dshReplaceSurfaceOp(seqs[0], seqs.at(-1)),
|
|
17330
|
+
sourceEventSeqs: seqs
|
|
17331
|
+
});
|
|
17332
|
+
}
|
|
17333
|
+
function surfaceMaintenanceInput(state) {
|
|
17334
|
+
return `${state.memory.storageRevision}:${surfaceCapacityKey(state.session)}:${state.session.surface?.nodes.join(",") ?? ""}`;
|
|
17335
|
+
}
|
|
17336
|
+
function surfaceFailureIsPermanent(error) {
|
|
17337
|
+
const seen = /* @__PURE__ */ new Set();
|
|
17338
|
+
for (let current = error; current && typeof current === "object" && !seen.has(current); ) {
|
|
17339
|
+
seen.add(current);
|
|
17340
|
+
const failure = current;
|
|
17341
|
+
if (failure.retryable === false || typeof failure.message === "string" && /^surface replace:/.test(failure.message)) return true;
|
|
17342
|
+
current = failure.cause;
|
|
17343
|
+
}
|
|
17344
|
+
return false;
|
|
17262
17345
|
}
|
|
17263
17346
|
function checkpointLevel(text3) {
|
|
17264
17347
|
const match = text3.match(/(?:^|\n)Block: [^|\n]+ \| Turns: \d+-\d+ \| Level: L([0-5])(?:\s|$)/mu);
|