stratagate-dsh 0.3.39 → 0.3.41
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 -731
- 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 +3 -2
- package/dist/index.js.map +1 -1
- package/dist/plugin.js +137 -52
- 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 -319
- 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/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 +2 -1
- 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,S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|
13732
|
+
var SURFACE_MAINTENANCE_MAX_ATTEMPTS = 3;
|
|
13732
13733
|
function graphProjectionCanRun(job, now = Date.now()) {
|
|
13733
13734
|
return job.attempts < DERIVATION_MAX_ATTEMPTS && (job.status === "pending" || job.status === "failed" && job.nextRetryAt != null && Date.parse(job.nextRetryAt) <= now);
|
|
13734
13735
|
}
|
|
@@ -14442,8 +14443,19 @@ var StrataGateRuntime = class _StrataGateRuntime {
|
|
|
14442
14443
|
this.scheduleSurfaceMaintenance(surface);
|
|
14443
14444
|
} else if (event.type === "request/context") {
|
|
14444
14445
|
const limit = surfaceCheckpointLimit(session);
|
|
14445
|
-
|
|
14446
|
-
|
|
14446
|
+
const capacityKey = surfaceCapacityKey(session);
|
|
14447
|
+
const limitChanged = surface.checkpointLimit !== limit;
|
|
14448
|
+
const capacityChanged = surface.capacityKey !== void 0 && surface.capacityKey !== capacityKey;
|
|
14449
|
+
surface.checkpointLimit = limit;
|
|
14450
|
+
surface.capacityKey = capacityKey;
|
|
14451
|
+
if (limitChanged || capacityChanged || surface.blockedAt !== void 0 && surface.blockedAt !== surfaceMaintenanceInput(surface)) {
|
|
14452
|
+
this.markSurfaceDirty(session, surface.memory);
|
|
14453
|
+
}
|
|
14454
|
+
} else if (event.type === "request/header") {
|
|
14455
|
+
const capacityKey = surfaceCapacityKey(session);
|
|
14456
|
+
const capacityChanged = surface.capacityKey !== void 0 && surface.capacityKey !== capacityKey;
|
|
14457
|
+
surface.capacityKey = capacityKey;
|
|
14458
|
+
if (capacityChanged || surface.blockedAt !== void 0 && surface.blockedAt !== surfaceMaintenanceInput(surface)) {
|
|
14447
14459
|
this.markSurfaceDirty(session, surface.memory);
|
|
14448
14460
|
}
|
|
14449
14461
|
} 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 +15127,9 @@ var StrataGateRuntime = class _StrataGateRuntime {
|
|
|
15115
15127
|
if (endTurn === null || hostCompactionActive(session)) return false;
|
|
15116
15128
|
const sourceEventSeqs = sources2 ?? sealedSurfaceSeqs(session, endTurn, block);
|
|
15117
15129
|
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
15130
|
const selected = selectCompressedBlockSurface(session, sourceEventSeqs, block, context);
|
|
15122
15131
|
if (!selected) return false;
|
|
15123
|
-
session.
|
|
15124
|
-
content: [{ type: "text", text: selected }],
|
|
15125
|
-
source: dshMessageSource()
|
|
15126
|
-
}), {
|
|
15127
|
-
surfaceOp: dshReplaceSurfaceOp(start, end),
|
|
15128
|
-
sourceEventSeqs
|
|
15129
|
-
});
|
|
15132
|
+
appendBlockSurface(session, block.id, selected, sourceEventSeqs);
|
|
15130
15133
|
this.surfaceFlushPending.add(session);
|
|
15131
15134
|
levels.set(block.id, checkpointLevel(selected) ?? context.level);
|
|
15132
15135
|
return true;
|
|
@@ -15179,13 +15182,7 @@ var StrataGateRuntime = class _StrataGateRuntime {
|
|
|
15179
15182
|
);
|
|
15180
15183
|
if (!text3) continue;
|
|
15181
15184
|
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
|
-
});
|
|
15185
|
+
appendBlockSurface(session, block.id, text3, [node.seq]);
|
|
15189
15186
|
this.surfaceFlushPending.add(session);
|
|
15190
15187
|
changed = true;
|
|
15191
15188
|
}
|
|
@@ -15207,12 +15204,15 @@ var StrataGateRuntime = class _StrataGateRuntime {
|
|
|
15207
15204
|
const node = current.get(block.id);
|
|
15208
15205
|
const endTurn = node ? null : dshTurnAtBlockEnd(session, block);
|
|
15209
15206
|
const seqs = node ? [node.seq] : endTurn === null ? null : sealedSurfaceSeqs(session, endTurn, block);
|
|
15210
|
-
if (!seqs?.length
|
|
15207
|
+
if (!seqs?.length || !safeBlockSurfaceRange(session, block.id, seqs)) return {
|
|
15208
|
+
status: "blocked",
|
|
15209
|
+
reason: "source-unavailable",
|
|
15210
|
+
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"
|
|
15211
|
+
};
|
|
15211
15212
|
const relief = [];
|
|
15212
15213
|
const window = session.requestContext?.()?.contextWindow;
|
|
15213
15214
|
if (node?.text !== text3 && typeof window === "number" && Number.isInteger(window) && window > 0) {
|
|
15214
|
-
const
|
|
15215
|
-
const end = seqs.at(-1);
|
|
15215
|
+
const replaced = new Set(seqs);
|
|
15216
15216
|
let before = 0;
|
|
15217
15217
|
let removed = 0;
|
|
15218
15218
|
for (const seq of session.surface.nodes) {
|
|
@@ -15220,7 +15220,7 @@ var StrataGateRuntime = class _StrataGateRuntime {
|
|
|
15220
15220
|
const message = event && session.deriveEventMessage(event);
|
|
15221
15221
|
const tokens = message ? estimateTokens(JSON.stringify(message)) : 0;
|
|
15222
15222
|
before += tokens;
|
|
15223
|
-
if (seq
|
|
15223
|
+
if (replaced.has(seq)) removed += tokens;
|
|
15224
15224
|
}
|
|
15225
15225
|
const header = session.requestHeader?.();
|
|
15226
15226
|
const reserved = (header?.config.maxTokens ?? 0) + (header?.tools?.length ? estimateTokens(JSON.stringify(header.tools)) : 0);
|
|
@@ -15230,7 +15230,7 @@ var StrataGateRuntime = class _StrataGateRuntime {
|
|
|
15230
15230
|
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
15231
|
for (const candidate of candidates) {
|
|
15232
15232
|
const checkpoint = current.get(candidate.id);
|
|
15233
|
-
if (!checkpoint ||
|
|
15233
|
+
if (!checkpoint || replaced.has(checkpoint.seq)) continue;
|
|
15234
15234
|
const level = checkpointLevel(checkpoint.text);
|
|
15235
15235
|
if (level === void 0 || level === 0) continue;
|
|
15236
15236
|
const original = this.cachedCheckpointTokens(session, checkpoint);
|
|
@@ -15290,22 +15290,13 @@ var StrataGateRuntime = class _StrataGateRuntime {
|
|
|
15290
15290
|
}
|
|
15291
15291
|
const { text: text3, seqs, relief } = plan;
|
|
15292
15292
|
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] });
|
|
15293
|
+
appendBlockSurface(session, replacement.id, replacement.text, [replacement.seq]);
|
|
15297
15294
|
levels.set(replacement.id, replacement.level);
|
|
15298
15295
|
this.surfaceFlushPending.add(session);
|
|
15299
15296
|
}
|
|
15300
15297
|
const node = currentBlockSurfaceMessages(session).get(block.id);
|
|
15301
15298
|
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 });
|
|
15299
|
+
appendBlockSurface(session, block.id, text3, seqs);
|
|
15309
15300
|
this.surfaceFlushPending.add(session);
|
|
15310
15301
|
}
|
|
15311
15302
|
levels.set(block.id, context.level);
|
|
@@ -15357,13 +15348,19 @@ var StrataGateRuntime = class _StrataGateRuntime {
|
|
|
15357
15348
|
return tokens;
|
|
15358
15349
|
}
|
|
15359
15350
|
/** Finish deferred sealing or user expansion only after the target surface is applied. */
|
|
15360
|
-
async syncPendingRetrySurface(session, memory, userOnly = false) {
|
|
15351
|
+
async syncPendingRetrySurface(session, memory, userOnly = false, force = false) {
|
|
15352
|
+
if (!force && this.surfaceRetryBlocked(session, memory)) return;
|
|
15361
15353
|
const levels = /* @__PURE__ */ new Map();
|
|
15362
|
-
|
|
15363
|
-
|
|
15364
|
-
|
|
15365
|
-
|
|
15366
|
-
|
|
15354
|
+
try {
|
|
15355
|
+
const completed = this.applyPendingRetrySurface(session, memory, levels, userOnly);
|
|
15356
|
+
if (completed.length === 0) return;
|
|
15357
|
+
await this.commitSurfaceLevels(memory, levels);
|
|
15358
|
+
await this.flushSurface(session);
|
|
15359
|
+
this.completeSurfaceRequests(completed);
|
|
15360
|
+
} catch (error) {
|
|
15361
|
+
this.recordSurfaceFailure(session, memory, error);
|
|
15362
|
+
throw error;
|
|
15363
|
+
}
|
|
15367
15364
|
}
|
|
15368
15365
|
applyPendingRetrySurface(session, memory, levels, userOnly = false, recoverable) {
|
|
15369
15366
|
const namespace = this.namespaceFor(session);
|
|
@@ -15427,6 +15424,7 @@ var StrataGateRuntime = class _StrataGateRuntime {
|
|
|
15427
15424
|
/** Read-only in the common case; render only explicit lifts or oversized checkpoints. */
|
|
15428
15425
|
async syncRequestSurface(session, memory) {
|
|
15429
15426
|
if (hostCompactionActive(session)) return;
|
|
15427
|
+
if (this.surfaceRetryBlocked(session, memory)) return;
|
|
15430
15428
|
const current = currentBlockSurfaceMessages(session);
|
|
15431
15429
|
const limit = surfaceCheckpointLimit(session);
|
|
15432
15430
|
const repair = /* @__PURE__ */ new Set();
|
|
@@ -15455,20 +15453,61 @@ var StrataGateRuntime = class _StrataGateRuntime {
|
|
|
15455
15453
|
}
|
|
15456
15454
|
markSurfaceDirty(session, memory) {
|
|
15457
15455
|
if (this.closed) return;
|
|
15456
|
+
const state = this.ensureSurfaceMaintenanceState(session, memory);
|
|
15457
|
+
state.dirty = true;
|
|
15458
|
+
state.generation += 1;
|
|
15459
|
+
if (state.blockedAt !== void 0 && state.blockedAt !== surfaceMaintenanceInput(state)) {
|
|
15460
|
+
delete state.blockedAt;
|
|
15461
|
+
state.failures = 0;
|
|
15462
|
+
}
|
|
15463
|
+
this.scheduleSurfaceMaintenance(state);
|
|
15464
|
+
}
|
|
15465
|
+
ensureSurfaceMaintenanceState(session, memory) {
|
|
15458
15466
|
const key = String(session.id);
|
|
15459
15467
|
let state = this.surfaceMaintenance.get(key);
|
|
15460
15468
|
if (!state || state.session !== session || state.memory !== memory) {
|
|
15461
15469
|
if (state?.timer) clearTimeout(state.timer);
|
|
15462
15470
|
if (state) state.generation += 1;
|
|
15463
|
-
state = {
|
|
15471
|
+
state = {
|
|
15472
|
+
session,
|
|
15473
|
+
memory,
|
|
15474
|
+
dirty: true,
|
|
15475
|
+
generation: 0,
|
|
15476
|
+
busy: hostTurnActive(session),
|
|
15477
|
+
failures: 0,
|
|
15478
|
+
checkpointLimit: surfaceCheckpointLimit(session)
|
|
15479
|
+
};
|
|
15464
15480
|
this.surfaceMaintenance.set(key, state);
|
|
15465
15481
|
}
|
|
15482
|
+
return state;
|
|
15483
|
+
}
|
|
15484
|
+
clearSurfaceFailure(session, memory) {
|
|
15485
|
+
const state = this.surfaceMaintenance.get(String(session.id));
|
|
15486
|
+
if (state?.session !== session || state.memory !== memory) return;
|
|
15487
|
+
delete state.blockedAt;
|
|
15488
|
+
state.failures = 0;
|
|
15489
|
+
}
|
|
15490
|
+
surfaceRetryBlocked(session, memory) {
|
|
15491
|
+
const state = this.surfaceMaintenance.get(String(session.id));
|
|
15492
|
+
if (state?.session !== session || state.memory !== memory || state.blockedAt === void 0) return false;
|
|
15493
|
+
if (state.blockedAt !== surfaceMaintenanceInput(state)) {
|
|
15494
|
+
this.clearSurfaceFailure(session, memory);
|
|
15495
|
+
return false;
|
|
15496
|
+
}
|
|
15497
|
+
return true;
|
|
15498
|
+
}
|
|
15499
|
+
recordSurfaceFailure(session, memory, error) {
|
|
15500
|
+
const state = this.ensureSurfaceMaintenanceState(session, memory);
|
|
15466
15501
|
state.dirty = true;
|
|
15467
|
-
state.
|
|
15468
|
-
|
|
15502
|
+
state.failures += 1;
|
|
15503
|
+
if (surfaceFailureIsPermanent(error) || state.failures >= SURFACE_MAINTENANCE_MAX_ATTEMPTS) {
|
|
15504
|
+
state.blockedAt = surfaceMaintenanceInput(state);
|
|
15505
|
+
if (state.timer) clearTimeout(state.timer);
|
|
15506
|
+
state.timer = void 0;
|
|
15507
|
+
}
|
|
15469
15508
|
}
|
|
15470
15509
|
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;
|
|
15510
|
+
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
15511
|
state.timer = setTimeout(() => {
|
|
15473
15512
|
state.timer = void 0;
|
|
15474
15513
|
void this.runSurfaceMaintenance(state.session);
|
|
@@ -15482,7 +15521,7 @@ var StrataGateRuntime = class _StrataGateRuntime {
|
|
|
15482
15521
|
if (state.run) return state.run;
|
|
15483
15522
|
if (state.timer) clearTimeout(state.timer);
|
|
15484
15523
|
state.timer = void 0;
|
|
15485
|
-
if (!state.dirty || state.busy || this.surfaceAssemblyReaders.get(session) || hostTurnActive(session) || hostCompactionActive(session)) return;
|
|
15524
|
+
if (!state.dirty || state.blockedAt !== void 0 || state.busy || this.surfaceAssemblyReaders.get(session) || hostTurnActive(session) || hostCompactionActive(session)) return;
|
|
15486
15525
|
const generation = state.generation;
|
|
15487
15526
|
const run = (async () => {
|
|
15488
15527
|
await this.drainTails.get(String(session.id));
|
|
@@ -15512,6 +15551,11 @@ var StrataGateRuntime = class _StrataGateRuntime {
|
|
|
15512
15551
|
if (error instanceof StorageConflictError) {
|
|
15513
15552
|
await state.memory.reloadFromStorage().catch((reloadError) => this.onIngestError(reloadError));
|
|
15514
15553
|
}
|
|
15554
|
+
if (state.generation !== generation) {
|
|
15555
|
+
state.failures = 0;
|
|
15556
|
+
} else if (surfaceFailureIsPermanent(error) || state.failures >= SURFACE_MAINTENANCE_MAX_ATTEMPTS) {
|
|
15557
|
+
state.blockedAt = surfaceMaintenanceInput(state);
|
|
15558
|
+
}
|
|
15515
15559
|
this.onIngestError(error);
|
|
15516
15560
|
}).finally(() => {
|
|
15517
15561
|
state.run = void 0;
|
|
@@ -16495,6 +16539,7 @@ var StrataGateRuntime = class _StrataGateRuntime {
|
|
|
16495
16539
|
/** Persist the supplied embedding/rerank settings; omitted keys keep their value. */
|
|
16496
16540
|
writeVectorSettings(values) {
|
|
16497
16541
|
if (this.config.database === ":memory:") return;
|
|
16542
|
+
this.profileStore();
|
|
16498
16543
|
const database = new DatabaseSync5(this.config.database);
|
|
16499
16544
|
try {
|
|
16500
16545
|
database.exec("PRAGMA busy_timeout = 5000");
|
|
@@ -16642,10 +16687,11 @@ var StrataGateRuntime = class _StrataGateRuntime {
|
|
|
16642
16687
|
if (plan.status === "blocked") throw Object.assign(new Error(plan.message), { code: "BLOCK_SELECTION_REJECTED" });
|
|
16643
16688
|
}
|
|
16644
16689
|
const result = await memory.expandBlock(id, target, "user");
|
|
16690
|
+
if (session && this.namespaceFor(session) === key) this.clearSurfaceFailure(session, memory);
|
|
16645
16691
|
this.pendingSurfaceSync.delete(`${key}\0${id}`);
|
|
16646
16692
|
this.queueSurfaceSync(key, id, "user-expansion");
|
|
16647
16693
|
if (session && this.namespaceFor(session) === key) {
|
|
16648
|
-
await this.syncPendingRetrySurface(session, memory);
|
|
16694
|
+
await this.syncPendingRetrySurface(session, memory, false, true);
|
|
16649
16695
|
}
|
|
16650
16696
|
return {
|
|
16651
16697
|
...result,
|
|
@@ -17232,8 +17278,9 @@ function sealedSurfaceSeqs(session, endTurn, block) {
|
|
|
17232
17278
|
start: open2.start,
|
|
17233
17279
|
end: event.seq,
|
|
17234
17280
|
nodes: currentSurface.filter((seq) => {
|
|
17235
|
-
if (seq > open2.start && seq < event.seq) return true;
|
|
17236
17281
|
const current = session.eventAt(seq);
|
|
17282
|
+
if (!replaceableBlockEvent(current, block.id)) return false;
|
|
17283
|
+
if (seq > open2.start && seq < event.seq) return true;
|
|
17237
17284
|
return current?.type === "tool/result" && current.surfaceOp !== "append" && current.sourceEventSeqs?.some((source) => source > open2.start && source < event.seq);
|
|
17238
17285
|
})
|
|
17239
17286
|
});
|
|
@@ -17257,7 +17304,42 @@ function sealedSurfaceSeqs(session, endTurn, block) {
|
|
|
17257
17304
|
const startIndex = currentSurface.indexOf(start);
|
|
17258
17305
|
const endIndex = currentSurface.indexOf(end);
|
|
17259
17306
|
if (startIndex < 0 || endIndex < startIndex) return null;
|
|
17260
|
-
|
|
17307
|
+
const range = currentSurface.slice(startIndex, endIndex + 1);
|
|
17308
|
+
return safeBlockSurfaceRange(session, block.id, range) ? range : null;
|
|
17309
|
+
}
|
|
17310
|
+
function replaceableBlockEvent(event, blockId) {
|
|
17311
|
+
if (event?.type === "assistant/message" || event?.type === "tool/result") return true;
|
|
17312
|
+
return event?.type === "user/message" && (event.data.source.kind === "user" || blockSurfaceMessage(event)?.id === blockId);
|
|
17313
|
+
}
|
|
17314
|
+
function safeBlockSurfaceRange(session, blockId, seqs) {
|
|
17315
|
+
if (seqs.length === 0) return false;
|
|
17316
|
+
const start = session.surface.nodes.indexOf(seqs[0]);
|
|
17317
|
+
return start >= 0 && seqs.every((seq, index) => session.surface.nodes[start + index] === seq && replaceableBlockEvent(session.eventAt(seq), blockId));
|
|
17318
|
+
}
|
|
17319
|
+
function appendBlockSurface(session, blockId, text3, seqs) {
|
|
17320
|
+
if (!safeBlockSurfaceRange(session, blockId, seqs)) {
|
|
17321
|
+
throw Object.assign(new Error("StrataGate surface replacement no longer owns its complete source range"), { retryable: false });
|
|
17322
|
+
}
|
|
17323
|
+
session.append("user/message", createUserMessage2({
|
|
17324
|
+
content: [{ type: "text", text: text3 }],
|
|
17325
|
+
source: dshMessageSource()
|
|
17326
|
+
}), {
|
|
17327
|
+
surfaceOp: dshReplaceSurfaceOp(seqs[0], seqs.at(-1)),
|
|
17328
|
+
sourceEventSeqs: seqs
|
|
17329
|
+
});
|
|
17330
|
+
}
|
|
17331
|
+
function surfaceMaintenanceInput(state) {
|
|
17332
|
+
return `${state.memory.storageRevision}:${surfaceCapacityKey(state.session)}:${state.session.surface?.nodes.join(",") ?? ""}`;
|
|
17333
|
+
}
|
|
17334
|
+
function surfaceFailureIsPermanent(error) {
|
|
17335
|
+
const seen = /* @__PURE__ */ new Set();
|
|
17336
|
+
for (let current = error; current && typeof current === "object" && !seen.has(current); ) {
|
|
17337
|
+
seen.add(current);
|
|
17338
|
+
const failure = current;
|
|
17339
|
+
if (failure.retryable === false || typeof failure.message === "string" && /^surface replace:/.test(failure.message)) return true;
|
|
17340
|
+
current = failure.cause;
|
|
17341
|
+
}
|
|
17342
|
+
return false;
|
|
17261
17343
|
}
|
|
17262
17344
|
function checkpointLevel(text3) {
|
|
17263
17345
|
const match = text3.match(/(?:^|\n)Block: [^|\n]+ \| Turns: \d+-\d+ \| Level: L([0-5])(?:\s|$)/mu);
|
|
@@ -20072,12 +20154,15 @@ async function handleAdminRequest(runtime, req, res) {
|
|
|
20072
20154
|
if (req.method !== "PATCH") throw new AdminHttpError(405, "StrataGate settings require PATCH");
|
|
20073
20155
|
sendJson(res, 200, await updateSettings(runtime, url, req));
|
|
20074
20156
|
} else if (path === "/api/stratagate/models") {
|
|
20075
|
-
if (req.method === "GET" &&
|
|
20157
|
+
if (req.method === "GET" && !url.searchParams.has("kind")) {
|
|
20158
|
+
if (url.searchParams.has("url") || url.searchParams.has("key")) {
|
|
20159
|
+
throw new AdminHttpError(400, "Model credentials must be sent in a JSON body; credentials in the URL are not accepted");
|
|
20160
|
+
}
|
|
20076
20161
|
sendJson(res, 200, await runtime.adminMemoryModelCatalog(url.searchParams.get("provider") || void 0));
|
|
20077
20162
|
return;
|
|
20078
20163
|
}
|
|
20079
|
-
if (req.method === "POST" &&
|
|
20080
|
-
throw new AdminHttpError(405, "Memory models require GET");
|
|
20164
|
+
if (req.method === "POST" && req.body === void 0 && typeof req[Symbol.asyncIterator] !== "function") {
|
|
20165
|
+
throw new AdminHttpError(405, "Memory models require GET; embedding/rerank listing requires a JSON body");
|
|
20081
20166
|
}
|
|
20082
20167
|
if (req.method !== "GET" && req.method !== "POST") throw new AdminHttpError(405, "StrataGate model listing requires GET or POST");
|
|
20083
20168
|
if (req.method === "GET" && (url.searchParams.has("url") || url.searchParams.has("key"))) {
|