dsh-live-compact 0.0.0-stage → 1.0.6
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/LICENSE +21 -0
- package/README.md +166 -2
- package/bin/dsh-live-compact.js +14 -0
- package/cordis.patch.yml +18 -0
- package/gate.js +114 -0
- package/index.js +633 -0
- package/package.json +60 -4
- package/plan.js +786 -0
- package/raw.js +151 -0
- package/shadow.js +189 -0
- package/shell.js +131 -0
- package/thunder.js +50 -0
package/raw.js
ADDED
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import { existsSync, mkdirSync, readFileSync, unlinkSync, writeFileSync } from "node:fs";
|
|
3
|
+
import { join } from "node:path";
|
|
4
|
+
|
|
5
|
+
export const PRUNE_MARKER = "[... tool result middle pruned ...]";
|
|
6
|
+
|
|
7
|
+
export function contentHash(text) {
|
|
8
|
+
const hash = createHash("sha256");
|
|
9
|
+
hash.update(typeof text === "string" ? Buffer.from(text, "utf8") : text);
|
|
10
|
+
return hash.digest("hex");
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export function rawName(toolCallId) {
|
|
14
|
+
return `raw-${createHash("sha256").update(String(toolCallId)).digest("hex").slice(0, 16)}.txt`;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
function readExisting(file, toolCallId) {
|
|
18
|
+
let meta;
|
|
19
|
+
try {
|
|
20
|
+
meta = JSON.parse(readFileSync(`${file}.meta.json`, "utf8"));
|
|
21
|
+
} catch {
|
|
22
|
+
return { ok: false, reason: "missing-meta", toolCallId };
|
|
23
|
+
}
|
|
24
|
+
if (meta?.toolCallId !== toolCallId || typeof meta.sha256 !== "string" || !Number.isInteger(meta.bytes)) {
|
|
25
|
+
return { ok: false, reason: "bad-meta", toolCallId };
|
|
26
|
+
}
|
|
27
|
+
let stored;
|
|
28
|
+
try {
|
|
29
|
+
stored = readFileSync(file);
|
|
30
|
+
} catch {
|
|
31
|
+
return { ok: false, reason: "missing-file", toolCallId };
|
|
32
|
+
}
|
|
33
|
+
if (stored.length !== meta.bytes || contentHash(stored) !== meta.sha256 || stored.includes(PRUNE_MARKER)) {
|
|
34
|
+
return { ok: false, reason: "hash-mismatch", toolCallId };
|
|
35
|
+
}
|
|
36
|
+
return {
|
|
37
|
+
ok: true,
|
|
38
|
+
existed: true,
|
|
39
|
+
path: file,
|
|
40
|
+
bytes: stored.length,
|
|
41
|
+
sha256: meta.sha256,
|
|
42
|
+
toolCallId,
|
|
43
|
+
label: meta.label || "",
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export function verifiedRaw(record, readFile = readFileSync) {
|
|
48
|
+
const id = record?.toolCallId || "";
|
|
49
|
+
if (!record?.ok || !record.path || !record.sha256 || !id) return { ok: false, toolCallId: id };
|
|
50
|
+
let stored;
|
|
51
|
+
try {
|
|
52
|
+
stored = readFile(record.path);
|
|
53
|
+
} catch {
|
|
54
|
+
return { ok: false, toolCallId: id };
|
|
55
|
+
}
|
|
56
|
+
const bytes = typeof stored === "string" ? Buffer.from(stored, "utf8") : stored;
|
|
57
|
+
const text = typeof stored === "string" ? stored : stored.toString("utf8");
|
|
58
|
+
if (contentHash(bytes) !== record.sha256 || bytes.length !== record.bytes || text.includes(PRUNE_MARKER)) {
|
|
59
|
+
return { ok: false, toolCallId: id };
|
|
60
|
+
}
|
|
61
|
+
return {
|
|
62
|
+
ok: true,
|
|
63
|
+
text,
|
|
64
|
+
toolCallId: id,
|
|
65
|
+
path: record.path,
|
|
66
|
+
sha256: record.sha256,
|
|
67
|
+
bytes: bytes.length,
|
|
68
|
+
label: record.label || "",
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export function rawExcerptText(surface, record) {
|
|
73
|
+
const checked = record?.ok ? verifiedRaw(record) : null;
|
|
74
|
+
if (checked?.ok) return { text: checked.text, raw: checked };
|
|
75
|
+
const surfaceText = String(surface ?? "");
|
|
76
|
+
const id = record?.toolCallId || "";
|
|
77
|
+
if (record && surfaceText.includes(PRUNE_MARKER)) return { text: "", raw: id ? { ok: false, toolCallId: id } : undefined };
|
|
78
|
+
if (record) return { text: surfaceText, raw: { ok: false, toolCallId: id } };
|
|
79
|
+
return { text: surfaceText, raw: undefined };
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export function captureToolResult({ dir, toolCallId, text, turn, name, pathLabel }) {
|
|
83
|
+
if (!toolCallId) return { ok: false, reason: "missing-id", toolCallId: "" };
|
|
84
|
+
const body = String(text ?? "");
|
|
85
|
+
const file = join(dir, rawName(toolCallId));
|
|
86
|
+
if (existsSync(file)) return readExisting(file, toolCallId);
|
|
87
|
+
if (body.includes(PRUNE_MARKER)) return { ok: false, reason: "already-pruned", toolCallId };
|
|
88
|
+
try {
|
|
89
|
+
mkdirSync(dir, { recursive: true, mode: 0o700 });
|
|
90
|
+
const bytes = Buffer.from(body, "utf8");
|
|
91
|
+
const hash = contentHash(bytes);
|
|
92
|
+
const metaPath = `${file}.meta.json`;
|
|
93
|
+
writeFileSync(file, bytes, { flag: "wx", mode: 0o600 });
|
|
94
|
+
try {
|
|
95
|
+
writeFileSync(metaPath, JSON.stringify({
|
|
96
|
+
toolCallId,
|
|
97
|
+
bytes: bytes.length,
|
|
98
|
+
sha256: hash,
|
|
99
|
+
turn: turn ?? null,
|
|
100
|
+
name: name || "",
|
|
101
|
+
label: pathLabel || "",
|
|
102
|
+
}), { mode: 0o600 });
|
|
103
|
+
} catch (error) {
|
|
104
|
+
try {
|
|
105
|
+
unlinkSync(file);
|
|
106
|
+
} catch {}
|
|
107
|
+
throw error;
|
|
108
|
+
}
|
|
109
|
+
return { ok: true, existed: false, path: file, bytes: bytes.length, sha256: hash, toolCallId, label: pathLabel || "" };
|
|
110
|
+
} catch (error) {
|
|
111
|
+
if (error?.code === "EEXIST" && existsSync(file)) return readExisting(file, toolCallId);
|
|
112
|
+
return { ok: false, reason: "unwritable", toolCallId };
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
export function pointerFor(record, readFile = readFileSync) {
|
|
117
|
+
const id = record?.toolCallId || "";
|
|
118
|
+
if (!id) return "";
|
|
119
|
+
const checked = record?.ok ? verifiedRaw(record, readFile) : null;
|
|
120
|
+
if (!checked?.ok) return `无完整备份 [${id}]`;
|
|
121
|
+
const label = checked.label ? `${checked.label} ` : "";
|
|
122
|
+
return `${label}原文 ${checked.path} sha256:${checked.sha256.slice(0, 12)} ${checked.bytes}字节 [${id}]`;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
export function restoreToolText(message, record) {
|
|
126
|
+
const id = message?.toolCallId || message?.message?.toolCallId || "";
|
|
127
|
+
if (!id) return message;
|
|
128
|
+
const checked = verifiedRaw(record?.toolCallId ? record : { ...record, toolCallId: id });
|
|
129
|
+
if (!checked.ok) return message;
|
|
130
|
+
return {
|
|
131
|
+
...message,
|
|
132
|
+
role: message?.role || message?.message?.role || "tool",
|
|
133
|
+
content: checked.text,
|
|
134
|
+
toolCallId: id,
|
|
135
|
+
};
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
export function verifiedIds(records) {
|
|
139
|
+
const ids = new Set();
|
|
140
|
+
for (const record of records || []) {
|
|
141
|
+
if (record?.toolCallId && verifiedRaw(record).ok) ids.add(record.toolCallId);
|
|
142
|
+
}
|
|
143
|
+
return ids;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
export function runPruneAfterCapture(ids, captured, prune) {
|
|
147
|
+
for (const id of ids || []) {
|
|
148
|
+
if (!captured?.has?.(id)) throw new Error(`spine: raw backup missing before prune: ${id}`);
|
|
149
|
+
}
|
|
150
|
+
return prune();
|
|
151
|
+
}
|
package/shadow.js
ADDED
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
import { closeSync, openSync, readFileSync, readSync, readdirSync, statSync, unlinkSync } from "node:fs";
|
|
2
|
+
import { join, resolve } from "node:path";
|
|
3
|
+
|
|
4
|
+
const PLUGIN_NAME = /^(?:(?:cut|official|shadow)-\d+-[0-9a-f]{8}|raw-[0-9a-f]{16}|\d+-[0-9a-f]{8}-[0-9a-f]{8}|\d+-[0-9a-f]{8,16})\.txt$/;
|
|
5
|
+
const SESSION_MARK = /^# spine-session (\S+)/;
|
|
6
|
+
|
|
7
|
+
export function isPluginShadowName(name) {
|
|
8
|
+
return PLUGIN_NAME.test(name);
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export function sanitizeSessionKey(key) {
|
|
12
|
+
const safe = String(key || "default").replace(/[^A-Za-z0-9_-]/g, "");
|
|
13
|
+
return safe || "default";
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export function sessionDir(root, key) {
|
|
17
|
+
return join(root, sanitizeSessionKey(key));
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function ownerPid(dir) {
|
|
21
|
+
try {
|
|
22
|
+
const text = readFileSync(join(dir, "owner"), "utf8").trim();
|
|
23
|
+
const pid = Number(text);
|
|
24
|
+
return Number.isInteger(pid) && pid > 0 ? pid : 0;
|
|
25
|
+
} catch {
|
|
26
|
+
return 0;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function processAlive(pid) {
|
|
31
|
+
try {
|
|
32
|
+
process.kill(pid, 0);
|
|
33
|
+
return true;
|
|
34
|
+
} catch {
|
|
35
|
+
return false;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export function shadowFileBody(sessionKey, body) {
|
|
40
|
+
const key = String(sessionKey || "default").replace(/\s+/g, "");
|
|
41
|
+
return `# spine-session ${key}\n${body || ""}`;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export function shadowMaxBytes(env = process.env) {
|
|
45
|
+
const bytes = Number(env.SPINE_SHADOW_MAX_BYTES);
|
|
46
|
+
if (Number.isFinite(bytes) && bytes > 0) return Math.floor(bytes);
|
|
47
|
+
const mb = Number(env.SPINE_SHADOW_MAX_MB);
|
|
48
|
+
if (Number.isFinite(mb) && mb > 0) return Math.floor(mb * 1024 * 1024);
|
|
49
|
+
return 128 * 1024 * 1024;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function readSessionKey(file) {
|
|
53
|
+
const fd = openSync(file, "r");
|
|
54
|
+
try {
|
|
55
|
+
const buf = Buffer.alloc(240);
|
|
56
|
+
const count = readSync(fd, buf, 0, buf.length, 0);
|
|
57
|
+
const line = buf.subarray(0, count).toString("utf8").split("\n")[0] || "";
|
|
58
|
+
return SESSION_MARK.exec(line)?.[1] || "";
|
|
59
|
+
} catch {
|
|
60
|
+
return "";
|
|
61
|
+
} finally {
|
|
62
|
+
closeSync(fd);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export function listPluginShadows(dir) {
|
|
67
|
+
let names = [];
|
|
68
|
+
try {
|
|
69
|
+
names = readdirSync(dir);
|
|
70
|
+
} catch {
|
|
71
|
+
return [];
|
|
72
|
+
}
|
|
73
|
+
const files = [];
|
|
74
|
+
for (const name of names) {
|
|
75
|
+
if (!isPluginShadowName(name)) continue;
|
|
76
|
+
const path = resolve(dir, name);
|
|
77
|
+
try {
|
|
78
|
+
const info = statSync(path);
|
|
79
|
+
if (!info.isFile()) continue;
|
|
80
|
+
files.push({
|
|
81
|
+
name,
|
|
82
|
+
path,
|
|
83
|
+
size: info.size,
|
|
84
|
+
mtimeMs: info.mtimeMs,
|
|
85
|
+
sessionKey: readSessionKey(path),
|
|
86
|
+
});
|
|
87
|
+
} catch {}
|
|
88
|
+
}
|
|
89
|
+
return files;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
function byAge(left, right) {
|
|
93
|
+
return left.mtimeMs - right.mtimeMs || left.name.localeCompare(right.name);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
export function listShadowEntries(dir, alive = processAlive) {
|
|
97
|
+
const entries = [];
|
|
98
|
+
let names = [];
|
|
99
|
+
try {
|
|
100
|
+
names = readdirSync(dir, { withFileTypes: true });
|
|
101
|
+
} catch {
|
|
102
|
+
return entries;
|
|
103
|
+
}
|
|
104
|
+
for (const entry of names) {
|
|
105
|
+
const path = resolve(dir, entry.name);
|
|
106
|
+
if (entry.isFile() && isPluginShadowName(entry.name)) {
|
|
107
|
+
try {
|
|
108
|
+
const info = statSync(path);
|
|
109
|
+
entries.push({
|
|
110
|
+
name: entry.name,
|
|
111
|
+
path,
|
|
112
|
+
size: info.size,
|
|
113
|
+
mtimeMs: info.mtimeMs,
|
|
114
|
+
sessionKey: "",
|
|
115
|
+
legacy: true,
|
|
116
|
+
live: false,
|
|
117
|
+
});
|
|
118
|
+
} catch {}
|
|
119
|
+
continue;
|
|
120
|
+
}
|
|
121
|
+
if (!entry.isDirectory()) continue;
|
|
122
|
+
const live = ownerPid(path) ? alive(ownerPid(path)) : false;
|
|
123
|
+
for (const file of listPluginShadows(path)) {
|
|
124
|
+
entries.push({ ...file, legacy: false, live, sessionKey: file.sessionKey || entry.name });
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
return entries;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
export function rotateShadow({ dir, incomingBytes, protectPath, protectPaths = [], sessionKey, cap, unlink = unlinkSync, alive = processAlive }) {
|
|
131
|
+
const files = listShadowEntries(dir, alive);
|
|
132
|
+
const protect = new Set([protectPath, ...protectPaths].filter(Boolean).map((path) => resolve(path)));
|
|
133
|
+
const current = sanitizeSessionKey(sessionKey);
|
|
134
|
+
let used = files.reduce((sum, file) => sum + file.size, 0);
|
|
135
|
+
const low = Math.floor(cap * 0.8);
|
|
136
|
+
const oversize = incomingBytes > cap;
|
|
137
|
+
if (!oversize && used + incomingBytes <= cap) {
|
|
138
|
+
return { ok: true, deleted: 0, freed: 0, used, cap, oversize: false, failed: false };
|
|
139
|
+
}
|
|
140
|
+
const legacy = [];
|
|
141
|
+
const dead = [];
|
|
142
|
+
const mine = [];
|
|
143
|
+
for (const file of files) {
|
|
144
|
+
if (protect.has(file.path)) continue;
|
|
145
|
+
if (file.live && file.sessionKey !== current) continue;
|
|
146
|
+
if (file.legacy) legacy.push(file);
|
|
147
|
+
else if (file.sessionKey === current) mine.push(file);
|
|
148
|
+
else dead.push(file);
|
|
149
|
+
}
|
|
150
|
+
legacy.sort(byAge);
|
|
151
|
+
dead.sort(byAge);
|
|
152
|
+
mine.sort(byAge);
|
|
153
|
+
let deleted = 0;
|
|
154
|
+
let freed = 0;
|
|
155
|
+
let failed = false;
|
|
156
|
+
for (const file of [...legacy, ...dead, ...mine]) {
|
|
157
|
+
if (!oversize && used + incomingBytes <= low) break;
|
|
158
|
+
try {
|
|
159
|
+
unlink(file.path);
|
|
160
|
+
} catch (error) {
|
|
161
|
+
if (error?.code === "ENOENT") {
|
|
162
|
+
used -= file.size;
|
|
163
|
+
freed += file.size;
|
|
164
|
+
deleted += 1;
|
|
165
|
+
continue;
|
|
166
|
+
}
|
|
167
|
+
failed = true;
|
|
168
|
+
break;
|
|
169
|
+
}
|
|
170
|
+
used -= file.size;
|
|
171
|
+
freed += file.size;
|
|
172
|
+
deleted += 1;
|
|
173
|
+
if (file.name.startsWith("raw-")) {
|
|
174
|
+
try {
|
|
175
|
+
unlink(`${file.path}.meta.json`);
|
|
176
|
+
} catch {}
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
const fits = used + incomingBytes <= cap;
|
|
180
|
+
return {
|
|
181
|
+
ok: !failed && (oversize || fits),
|
|
182
|
+
deleted,
|
|
183
|
+
freed,
|
|
184
|
+
used,
|
|
185
|
+
cap,
|
|
186
|
+
oversize,
|
|
187
|
+
failed,
|
|
188
|
+
};
|
|
189
|
+
}
|
package/shell.js
ADDED
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
// Thin entry. This file must evaluate even when the engine cannot load.
|
|
2
|
+
// A throw here leaves stock disabled and /compact waiting.
|
|
3
|
+
|
|
4
|
+
let Engine = null;
|
|
5
|
+
let Stock = null;
|
|
6
|
+
let CompactionEngine = null;
|
|
7
|
+
let ManualCompactionError = null;
|
|
8
|
+
let loadError = null;
|
|
9
|
+
|
|
10
|
+
if (process.env.SPINE_ENGINE_FAIL === "import") {
|
|
11
|
+
loadError = new Error("spine: engine import refused");
|
|
12
|
+
} else {
|
|
13
|
+
try {
|
|
14
|
+
const engine = await import("./index.js");
|
|
15
|
+
Engine = engine.default;
|
|
16
|
+
} catch (error) {
|
|
17
|
+
loadError = error instanceof Error ? error : new Error(String(error));
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
try {
|
|
22
|
+
const compaction = await import("@deepseek-ai/dsh-compaction");
|
|
23
|
+
CompactionEngine = compaction.CompactionEngine;
|
|
24
|
+
ManualCompactionError = compaction.ManualCompactionError;
|
|
25
|
+
} catch (error) {
|
|
26
|
+
loadError ??= error instanceof Error ? error : new Error(String(error));
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
try {
|
|
30
|
+
const stock = await import("@deepseek-ai/dsh-compaction-basic");
|
|
31
|
+
Stock = stock.BasicCompactionEngine;
|
|
32
|
+
} catch (error) {
|
|
33
|
+
if (!loadError) loadError = error instanceof Error ? error : new Error(String(error));
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function reasonText(error) {
|
|
37
|
+
return error instanceof Error ? error.message : String(error);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export function preflightConfig(config = {}) {
|
|
41
|
+
if (!config || typeof config !== "object") return;
|
|
42
|
+
const threshold = config.thresholdRatio;
|
|
43
|
+
if (typeof threshold !== "number") return;
|
|
44
|
+
if (config.retainTokens !== undefined) return;
|
|
45
|
+
const ratio = config.retainRatio ?? 0.16;
|
|
46
|
+
if (ratio >= threshold) {
|
|
47
|
+
throw new Error(`BasicCompactionConfig: retainRatio (${ratio}) must be less than the resolved thresholdRatio (${threshold})`);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function releaseCompaction(ctx) {
|
|
52
|
+
const store = ctx?.reflect?.store;
|
|
53
|
+
if (!store) return false;
|
|
54
|
+
const keys = [...Object.getOwnPropertySymbols(store), ...Object.keys(store)];
|
|
55
|
+
for (const key of keys) {
|
|
56
|
+
if (store[key]?.name !== "compaction") continue;
|
|
57
|
+
delete store[key];
|
|
58
|
+
if (ctx.fiber?.store) delete ctx.fiber.store.compaction;
|
|
59
|
+
return true;
|
|
60
|
+
}
|
|
61
|
+
return false;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function visibleTakeover(ctx, reason) {
|
|
65
|
+
const line = `spine: ${MARK}。${reason}`;
|
|
66
|
+
console.error(line);
|
|
67
|
+
ctx?.logger?.warn?.(line);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
const MARK = "本会话未接管";
|
|
71
|
+
|
|
72
|
+
const UnavailableCompaction = CompactionEngine
|
|
73
|
+
? class extends CompactionEngine {
|
|
74
|
+
constructor(ctx, reason) {
|
|
75
|
+
super(ctx);
|
|
76
|
+
this.reason = reason;
|
|
77
|
+
ctx.logger?.warn?.(`spine: ${MARK}。压缩没有装上,这一刀没有执行。${reason}`);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
compactIfNeeded() {
|
|
81
|
+
return Promise.reject(new Error(`spine: ${MARK}。压缩没有装上,这一刀没有执行。${this.reason}`));
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
compactNow() {
|
|
85
|
+
const message = `spine: ${MARK}。压缩没有装上,这一刀没有执行。${this.reason}`;
|
|
86
|
+
if (ManualCompactionError) return Promise.reject(new ManualCompactionError("summary", message));
|
|
87
|
+
return Promise.reject(new Error(message));
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
: null;
|
|
91
|
+
|
|
92
|
+
export default class LiveCompactEntry {
|
|
93
|
+
static inject = ["llm", "tokenMeter", "sessions"];
|
|
94
|
+
static Config = Stock?.Config || Engine?.Config;
|
|
95
|
+
|
|
96
|
+
constructor(ctx, config) {
|
|
97
|
+
let cause = loadError;
|
|
98
|
+
let skipStock = process.env.SPINE_ENGINE_FAIL === "construct";
|
|
99
|
+
if (skipStock) cause = new Error("spine: constructor refused");
|
|
100
|
+
if (!loadError && !skipStock) {
|
|
101
|
+
try {
|
|
102
|
+
preflightConfig(config);
|
|
103
|
+
} catch (error) {
|
|
104
|
+
cause = error instanceof Error ? error : new Error(String(error));
|
|
105
|
+
skipStock = true;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
if (!loadError && !skipStock && Engine) {
|
|
109
|
+
try {
|
|
110
|
+
return new Engine(ctx, config);
|
|
111
|
+
} catch (error) {
|
|
112
|
+
cause = error instanceof Error ? error : new Error(String(error));
|
|
113
|
+
releaseCompaction(ctx);
|
|
114
|
+
skipStock = true;
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
cause ??= new Error("spine: engine missing");
|
|
118
|
+
if (!skipStock && process.env.SPINE_STOCK_FAIL !== "1" && Stock) {
|
|
119
|
+
try {
|
|
120
|
+
visibleTakeover(ctx, `这一轮用原版压缩。${reasonText(cause)}`);
|
|
121
|
+
return new Stock(ctx, config);
|
|
122
|
+
} catch (error) {
|
|
123
|
+
cause = error instanceof Error ? error : new Error(String(error));
|
|
124
|
+
releaseCompaction(ctx);
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
visibleTakeover(ctx, reasonText(cause));
|
|
128
|
+
if (!UnavailableCompaction) throw new Error(`spine: ${MARK}。压缩接口没有装上。${reasonText(cause)}`);
|
|
129
|
+
return new UnavailableCompaction(ctx, reasonText(cause));
|
|
130
|
+
}
|
|
131
|
+
}
|
package/thunder.js
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
// Next-round thunder path. Off unless SPINE_THUNDER=1.
|
|
2
|
+
// One model read only. Never try a full attach and then send less.
|
|
3
|
+
|
|
4
|
+
const PROSE_TOKEN_CAP = 32000;
|
|
5
|
+
|
|
6
|
+
export function sharedPrefixLength(previous, next) {
|
|
7
|
+
if (!previous || !next) return 0;
|
|
8
|
+
const limit = Math.min(previous.length, next.length);
|
|
9
|
+
let index = 0;
|
|
10
|
+
const step = 64;
|
|
11
|
+
while (index + step <= limit && previous.slice(index, index + step) === next.slice(index, index + step)) index += step;
|
|
12
|
+
while (index < limit && previous[index] === next[index]) index += 1;
|
|
13
|
+
return index;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export function decideThunder({ previous, next, modelKind, prefixTokens, attachMaxTokens = 80000 }) {
|
|
17
|
+
if (!previous || !next) return "send-less";
|
|
18
|
+
const shared = sharedPrefixLength(previous, next);
|
|
19
|
+
const ratio = shared / Math.max(previous.length, 1);
|
|
20
|
+
if (ratio < 0.9) return "send-less";
|
|
21
|
+
if (modelKind === "27b" && (prefixTokens || 0) > attachMaxTokens) return "send-less";
|
|
22
|
+
return "attach";
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function estimateTokens(text) {
|
|
26
|
+
const cjk = (text.match(/[\u4e00-\u9fff]/g) || []).length;
|
|
27
|
+
return Math.ceil(cjk / 1.5 + (text.length - cjk) / 4);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export function buildSendLess({ taskLine, uncovered, facts, proseTokens = PROSE_TOKEN_CAP }) {
|
|
31
|
+
const kept = (facts || []).filter(Boolean);
|
|
32
|
+
let prose = uncovered || "";
|
|
33
|
+
if (estimateTokens(prose) > proseTokens) {
|
|
34
|
+
let low = 0;
|
|
35
|
+
let high = prose.length;
|
|
36
|
+
while (low < high) {
|
|
37
|
+
const mid = Math.ceil((low + high) / 2);
|
|
38
|
+
if (estimateTokens(prose.slice(0, mid)) <= proseTokens) low = mid;
|
|
39
|
+
else high = mid - 1;
|
|
40
|
+
}
|
|
41
|
+
prose = prose.slice(0, low);
|
|
42
|
+
}
|
|
43
|
+
return [
|
|
44
|
+
taskLine || "Continue the current task.",
|
|
45
|
+
"Facts already copied locally:",
|
|
46
|
+
...(kept.length ? kept.map((fact) => `- ${fact}`) : ["- (none)"]),
|
|
47
|
+
"Uncovered prose to summarize:",
|
|
48
|
+
prose || "(none)",
|
|
49
|
+
].join("\n");
|
|
50
|
+
}
|