dsh-live-compact 0.0.0-stage → 1.0.7
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 +212 -2
- package/bin/dsh-live-compact.js +26 -0
- package/cordis.patch.yml +18 -0
- package/gate.js +114 -0
- package/index.js +713 -0
- package/package.json +62 -4
- package/plan.js +1013 -0
- package/raw.js +162 -0
- package/shadow-search.js +134 -0
- package/shadow.js +251 -0
- package/shell.js +137 -0
- package/thunder.js +56 -0
package/raw.js
ADDED
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 工具原文。按 toolCallId 只写一次,写在官方修剪器换掉表面文本之前。
|
|
3
|
+
* Raw tool text, written once per toolCallId, before the official pruner replaces the surface.
|
|
4
|
+
*
|
|
5
|
+
* 已有文件必须对得上首次 meta 的哈希。对不上就当没有完整备份,不用当前内容给自己重算信任。
|
|
6
|
+
* An existing file must match the first meta hash. A mismatch is not a backup.
|
|
7
|
+
*/
|
|
8
|
+
import { createHash } from "node:crypto";
|
|
9
|
+
import { existsSync, mkdirSync, readFileSync, unlinkSync, writeFileSync } from "node:fs";
|
|
10
|
+
import { join } from "node:path";
|
|
11
|
+
|
|
12
|
+
export const PRUNE_MARKER = "[... tool result middle pruned ...]";
|
|
13
|
+
|
|
14
|
+
export function contentHash(text) {
|
|
15
|
+
const hash = createHash("sha256");
|
|
16
|
+
hash.update(typeof text === "string" ? Buffer.from(text, "utf8") : text);
|
|
17
|
+
return hash.digest("hex");
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export function rawName(toolCallId) {
|
|
21
|
+
return `raw-${createHash("sha256").update(String(toolCallId)).digest("hex").slice(0, 16)}.txt`;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function readExisting(file, toolCallId) {
|
|
25
|
+
let meta;
|
|
26
|
+
try {
|
|
27
|
+
meta = JSON.parse(readFileSync(`${file}.meta.json`, "utf8"));
|
|
28
|
+
} catch {
|
|
29
|
+
return { ok: false, reason: "missing-meta", toolCallId };
|
|
30
|
+
}
|
|
31
|
+
if (meta?.toolCallId !== toolCallId || typeof meta.sha256 !== "string" || !Number.isInteger(meta.bytes)) {
|
|
32
|
+
return { ok: false, reason: "bad-meta", toolCallId };
|
|
33
|
+
}
|
|
34
|
+
let stored;
|
|
35
|
+
try {
|
|
36
|
+
stored = readFileSync(file);
|
|
37
|
+
} catch {
|
|
38
|
+
return { ok: false, reason: "missing-file", toolCallId };
|
|
39
|
+
}
|
|
40
|
+
if (stored.length !== meta.bytes || contentHash(stored) !== meta.sha256 || stored.includes(PRUNE_MARKER)) {
|
|
41
|
+
return { ok: false, reason: "hash-mismatch", toolCallId };
|
|
42
|
+
}
|
|
43
|
+
return {
|
|
44
|
+
ok: true,
|
|
45
|
+
existed: true,
|
|
46
|
+
path: file,
|
|
47
|
+
bytes: stored.length,
|
|
48
|
+
sha256: meta.sha256,
|
|
49
|
+
toolCallId,
|
|
50
|
+
label: meta.label || "",
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export function verifiedRaw(record, readFile = readFileSync) {
|
|
55
|
+
const id = record?.toolCallId || "";
|
|
56
|
+
if (!record?.ok || !record.path || !record.sha256 || !id) return { ok: false, toolCallId: id };
|
|
57
|
+
let stored;
|
|
58
|
+
try {
|
|
59
|
+
stored = readFile(record.path);
|
|
60
|
+
} catch {
|
|
61
|
+
return { ok: false, toolCallId: id };
|
|
62
|
+
}
|
|
63
|
+
const bytes = typeof stored === "string" ? Buffer.from(stored, "utf8") : stored;
|
|
64
|
+
const text = typeof stored === "string" ? stored : stored.toString("utf8");
|
|
65
|
+
if (contentHash(bytes) !== record.sha256 || bytes.length !== record.bytes || text.includes(PRUNE_MARKER)) {
|
|
66
|
+
return { ok: false, toolCallId: id };
|
|
67
|
+
}
|
|
68
|
+
return {
|
|
69
|
+
ok: true,
|
|
70
|
+
text,
|
|
71
|
+
toolCallId: id,
|
|
72
|
+
path: record.path,
|
|
73
|
+
sha256: record.sha256,
|
|
74
|
+
bytes: bytes.length,
|
|
75
|
+
label: record.label || "",
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export function rawExcerptText(surface, record) {
|
|
80
|
+
const checked = record?.ok ? verifiedRaw(record) : null;
|
|
81
|
+
if (checked?.ok) return { text: checked.text, raw: checked };
|
|
82
|
+
const surfaceText = String(surface ?? "");
|
|
83
|
+
const id = record?.toolCallId || "";
|
|
84
|
+
if (record && surfaceText.includes(PRUNE_MARKER)) return { text: "", raw: id ? { ok: false, toolCallId: id } : undefined };
|
|
85
|
+
if (record) return { text: surfaceText, raw: { ok: false, toolCallId: id } };
|
|
86
|
+
return { text: surfaceText, raw: undefined };
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
// 同一个 toolCallId 只写一次。文件已在就核验 meta,不覆盖。
|
|
90
|
+
// 正文已经带修剪标记、又没有旧文件时,不把修剪后的文本当成原文。
|
|
91
|
+
// Write once per toolCallId. An existing file is verified, never overwritten.
|
|
92
|
+
// Pruned text with no earlier file is not stored as the original.
|
|
93
|
+
export function captureToolResult({ dir, toolCallId, text, turn, name, pathLabel }) {
|
|
94
|
+
if (!toolCallId) return { ok: false, reason: "missing-id", toolCallId: "" };
|
|
95
|
+
const body = String(text ?? "");
|
|
96
|
+
const file = join(dir, rawName(toolCallId));
|
|
97
|
+
if (existsSync(file)) return readExisting(file, toolCallId);
|
|
98
|
+
if (body.includes(PRUNE_MARKER)) return { ok: false, reason: "already-pruned", toolCallId };
|
|
99
|
+
try {
|
|
100
|
+
mkdirSync(dir, { recursive: true, mode: 0o700 });
|
|
101
|
+
const bytes = Buffer.from(body, "utf8");
|
|
102
|
+
const hash = contentHash(bytes);
|
|
103
|
+
const metaPath = `${file}.meta.json`;
|
|
104
|
+
writeFileSync(file, bytes, { flag: "wx", mode: 0o600 });
|
|
105
|
+
try {
|
|
106
|
+
writeFileSync(metaPath, JSON.stringify({
|
|
107
|
+
toolCallId,
|
|
108
|
+
bytes: bytes.length,
|
|
109
|
+
sha256: hash,
|
|
110
|
+
turn: turn ?? null,
|
|
111
|
+
name: name || "",
|
|
112
|
+
label: pathLabel || "",
|
|
113
|
+
}), { mode: 0o600 });
|
|
114
|
+
} catch (error) {
|
|
115
|
+
try {
|
|
116
|
+
unlinkSync(file);
|
|
117
|
+
} catch {}
|
|
118
|
+
throw error;
|
|
119
|
+
}
|
|
120
|
+
return { ok: true, existed: false, path: file, bytes: bytes.length, sha256: hash, toolCallId, label: pathLabel || "" };
|
|
121
|
+
} catch (error) {
|
|
122
|
+
if (error?.code === "EEXIST" && existsSync(file)) return readExisting(file, toolCallId);
|
|
123
|
+
return { ok: false, reason: "unwritable", toolCallId };
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
export function pointerFor(record, readFile = readFileSync) {
|
|
128
|
+
const id = record?.toolCallId || "";
|
|
129
|
+
if (!id) return "";
|
|
130
|
+
const checked = record?.ok ? verifiedRaw(record, readFile) : null;
|
|
131
|
+
if (!checked?.ok) return `无完整备份 [${id}]`;
|
|
132
|
+
const label = checked.label ? `${checked.label} ` : "";
|
|
133
|
+
return `${label}原文 ${checked.path} sha256:${checked.sha256.slice(0, 12)} ${checked.bytes}字节 [${id}]`;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
export function restoreToolText(message, record) {
|
|
137
|
+
const id = message?.toolCallId || message?.message?.toolCallId || "";
|
|
138
|
+
if (!id) return message;
|
|
139
|
+
const checked = verifiedRaw(record?.toolCallId ? record : { ...record, toolCallId: id });
|
|
140
|
+
if (!checked.ok) return message;
|
|
141
|
+
return {
|
|
142
|
+
...message,
|
|
143
|
+
role: message?.role || message?.message?.role || "tool",
|
|
144
|
+
content: checked.text,
|
|
145
|
+
toolCallId: id,
|
|
146
|
+
};
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
export function verifiedIds(records) {
|
|
150
|
+
const ids = new Set();
|
|
151
|
+
for (const record of records || []) {
|
|
152
|
+
if (record?.toolCallId && verifiedRaw(record).ok) ids.add(record.toolCallId);
|
|
153
|
+
}
|
|
154
|
+
return ids;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
export function runPruneAfterCapture(ids, captured, prune) {
|
|
158
|
+
for (const id of ids || []) {
|
|
159
|
+
if (!captured?.has?.(id)) throw new Error(`spine: raw backup missing before prune: ${id}`);
|
|
160
|
+
}
|
|
161
|
+
return prune();
|
|
162
|
+
}
|
package/shadow-search.js
ADDED
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
// 在 spine 的会话备份目录里检索原文片段。
|
|
2
|
+
//
|
|
3
|
+
// 背景:压缩把长段原文从上下文里移出、落到 shadow 目录,上下文只留摘要。
|
|
4
|
+
// 摘要容易丢掉具体坐标(文件路径、commit 短哈希、参数名),事后要把它们找回来时,
|
|
5
|
+
// 目前只能靠外部工具手敲 grep —— 而目录布局与文件命名是插件自己的约定,外部工具只能猜。
|
|
6
|
+
//
|
|
7
|
+
// 目录约定(1.0.6 起):备份按会话分目录,`sessionDir(root, key)` 给出某个会话的目录。
|
|
8
|
+
// 本模块**只读传入的那一个目录**,不递归、不跨会话、不推导路径;
|
|
9
|
+
// 需要「当前会话」语义时,调用方用 `root + sessionKey` 组合,由本模块调 `sessionDir` 定位。
|
|
10
|
+
//
|
|
11
|
+
// 设计取向(低耦合):
|
|
12
|
+
// · 目录一律由调用方给出(`dir`,或 `root` + `sessionKey`)
|
|
13
|
+
// · 文件识别复用本包已有的 `listPluginShadows`:哪些文件算备份、怎么命名,
|
|
14
|
+
// 由插件自己维护(本模块不另立一套过滤规则)
|
|
15
|
+
// · 返回值是纯数据,不含句柄、不读环境变量、不碰全局状态
|
|
16
|
+
|
|
17
|
+
import { readFileSync } from "node:fs";
|
|
18
|
+
import { resolve } from "node:path";
|
|
19
|
+
import { listPluginShadows, sessionDir } from "./shadow.js";
|
|
20
|
+
|
|
21
|
+
/** 单文件默认上限:超过就跳过,避免一次把几百 MB 读进内存。 */
|
|
22
|
+
const DEFAULT_MAX_FILE_BYTES = 8 * 1024 * 1024;
|
|
23
|
+
/** 默认最多返回多少条命中。 */
|
|
24
|
+
const DEFAULT_LIMIT = 50;
|
|
25
|
+
/** 备份文件首行是 `# spine-session <key>`:它是元信息,不参与匹配。 */
|
|
26
|
+
const SESSION_HEADER = /^# spine-session (\S+)\s*$/;
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* 在一个会话备份目录里检索文本。
|
|
30
|
+
*
|
|
31
|
+
* 目录只有两种给法,二选一:
|
|
32
|
+
* · `dir` —— 直接给会话备份目录(例如 `sessionDir(shadowRoot, key)` 的结果)
|
|
33
|
+
* · `root` + `sessionKey` —— 给 shadow 根和会话键,本模块按 `sessionDir` 定位到该会话目录
|
|
34
|
+
* 无论哪种,都**只扫描那一个目录**:不递归子目录、不扫父目录、不跨会话。
|
|
35
|
+
*
|
|
36
|
+
* @param {object} options 参数对象
|
|
37
|
+
* @param {string} [options.dir] 会话备份目录(与 root+sessionKey 二选一)
|
|
38
|
+
* @param {string} [options.root] shadow 根目录(配合 sessionKey 使用)
|
|
39
|
+
* @param {string} [options.sessionKey] 会话键(配合 root 使用)
|
|
40
|
+
* @param {string} options.pattern 关键词;`regex: true` 时按正则解释
|
|
41
|
+
* @param {boolean} [options.regex=false] 把 pattern 当正则
|
|
42
|
+
* @param {boolean} [options.ignoreCase=false] 忽略大小写
|
|
43
|
+
* @param {number} [options.limit=50] 最多返回多少条命中
|
|
44
|
+
* @param {number} [options.maxFileBytes=8388608] 单文件大小上限,超过则跳过
|
|
45
|
+
* @returns {{directory: string, scanned: number, skipped: number, matchedFiles: number, truncated: boolean, matches: Array<{file: string, sessionKey: string, line: number, text: string}>}}
|
|
46
|
+
* @throws {TypeError} 目录参数缺失、或 pattern 缺失时抛出
|
|
47
|
+
*/
|
|
48
|
+
export function searchShadow(options = {}) {
|
|
49
|
+
const directory = resolveTargetDirectory(options);
|
|
50
|
+
const pattern = options.pattern;
|
|
51
|
+
if (typeof pattern !== "string" || pattern === "") {
|
|
52
|
+
throw new TypeError("searchShadow: pattern is required (a keyword, or a regular expression when regex is true)");
|
|
53
|
+
}
|
|
54
|
+
const limit = Number.isFinite(options.limit) && options.limit > 0 ? Math.floor(options.limit) : DEFAULT_LIMIT;
|
|
55
|
+
const maxFileBytes = Number.isFinite(options.maxFileBytes) && options.maxFileBytes > 0
|
|
56
|
+
? Math.floor(options.maxFileBytes)
|
|
57
|
+
: DEFAULT_MAX_FILE_BYTES;
|
|
58
|
+
const matcher = options.regex === true
|
|
59
|
+
? new RegExp(pattern, options.ignoreCase === true ? "i" : "")
|
|
60
|
+
: null;
|
|
61
|
+
const needle = options.ignoreCase === true ? pattern.toLowerCase() : pattern;
|
|
62
|
+
|
|
63
|
+
const matches = [];
|
|
64
|
+
let scanned = 0;
|
|
65
|
+
let skipped = 0;
|
|
66
|
+
let matchedFiles = 0;
|
|
67
|
+
let truncated = false;
|
|
68
|
+
|
|
69
|
+
// 只列这一层:listPluginShadows 内部就是一次 readdir + 只收符合命名的普通文件,
|
|
70
|
+
// 因此天然不会递归进别的会话目录。
|
|
71
|
+
for (const file of listPluginShadows(directory)) {
|
|
72
|
+
if (file.size > maxFileBytes) {
|
|
73
|
+
skipped += 1;
|
|
74
|
+
continue;
|
|
75
|
+
}
|
|
76
|
+
let text;
|
|
77
|
+
try {
|
|
78
|
+
text = readFileSync(file.path, "utf8");
|
|
79
|
+
} catch {
|
|
80
|
+
skipped += 1;
|
|
81
|
+
continue;
|
|
82
|
+
}
|
|
83
|
+
scanned += 1;
|
|
84
|
+
let hitThisFile = false;
|
|
85
|
+
const lines = text.split("\n");
|
|
86
|
+
const headerMatch = SESSION_HEADER.exec(lines[0] ?? "");
|
|
87
|
+
const sessionKey = headerMatch === null ? file.sessionKey : headerMatch[1];
|
|
88
|
+
for (let index = 0; index < lines.length; index += 1) {
|
|
89
|
+
const line = lines[index];
|
|
90
|
+
if (SESSION_HEADER.test(line)) continue;
|
|
91
|
+
const hit = matcher === null
|
|
92
|
+
? (options.ignoreCase === true ? line.toLowerCase().includes(needle) : line.includes(needle))
|
|
93
|
+
: matcher.test(line);
|
|
94
|
+
if (!hit) continue;
|
|
95
|
+
hitThisFile = true;
|
|
96
|
+
matches.push({
|
|
97
|
+
file: file.path,
|
|
98
|
+
sessionKey,
|
|
99
|
+
line: index + 1,
|
|
100
|
+
text: line.length > 400 ? `${line.slice(0, 400)}…` : line,
|
|
101
|
+
});
|
|
102
|
+
if (matches.length >= limit) {
|
|
103
|
+
truncated = true;
|
|
104
|
+
break;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
if (hitThisFile) matchedFiles += 1;
|
|
108
|
+
if (truncated) break;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
return { directory, scanned, skipped, matchedFiles, truncated, matches };
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* 解析出「要扫描的那一个目录」。
|
|
116
|
+
*
|
|
117
|
+
* 这里刻意不接受「只给 root」:那会让调用方以为在搜全部会话,
|
|
118
|
+
* 而实际需求永远只针对某个会话。
|
|
119
|
+
*
|
|
120
|
+
* @param {object} options searchShadow 的入参
|
|
121
|
+
* @returns {string} 会话备份目录的绝对路径
|
|
122
|
+
* @throws {TypeError} 没有给出可用的目录参数时
|
|
123
|
+
*/
|
|
124
|
+
function resolveTargetDirectory(options) {
|
|
125
|
+
if (typeof options.dir === "string" && options.dir !== "") return resolve(options.dir);
|
|
126
|
+
if (typeof options.root === "string" && options.root !== "" && typeof options.sessionKey === "string" && options.sessionKey !== "") {
|
|
127
|
+
return resolve(sessionDir(options.root, options.sessionKey));
|
|
128
|
+
}
|
|
129
|
+
throw new TypeError(
|
|
130
|
+
"searchShadow: pass either dir (a session backup directory) or root + sessionKey; "
|
|
131
|
+
+ "searching a whole shadow root is not supported on purpose",
|
|
132
|
+
);
|
|
133
|
+
}
|
|
134
|
+
|
package/shadow.js
ADDED
|
@@ -0,0 +1,251 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 备份文件。按会话分目录,默认上限 256MB,快满时删最老的。
|
|
3
|
+
* Shadow files live in per-session directories. Default cap is 256MB; delete the oldest when nearly full.
|
|
4
|
+
*
|
|
5
|
+
* 删除顺序:根目录旧文件,死会话,当前会话的旧文件。
|
|
6
|
+
* 不删当前卡片指向的文件,也不删别的还活着的会话。ENOENT 当成已经删掉。
|
|
7
|
+
* Delete legacy root files, then dead sessions, then old files of the current session.
|
|
8
|
+
* Never delete the current card's file or another live session. ENOENT counts as already freed.
|
|
9
|
+
*/
|
|
10
|
+
import { randomBytes } from "node:crypto";
|
|
11
|
+
import { closeSync, openSync, readFileSync, readSync, readdirSync, renameSync, statSync, unlinkSync, writeFileSync } from "node:fs";
|
|
12
|
+
import { join, resolve } from "node:path";
|
|
13
|
+
|
|
14
|
+
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$/;
|
|
15
|
+
const SESSION_MARK = /^# spine-session (\S+)/;
|
|
16
|
+
|
|
17
|
+
export function isPluginShadowName(name) {
|
|
18
|
+
return PLUGIN_NAME.test(name);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export function sanitizeSessionKey(key) {
|
|
22
|
+
const safe = String(key || "default").replace(/[^A-Za-z0-9_-]/g, "");
|
|
23
|
+
return safe || "default";
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export function sessionDir(root, key) {
|
|
27
|
+
return join(root, sanitizeSessionKey(key));
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function ownerPid(dir) {
|
|
31
|
+
try {
|
|
32
|
+
const text = readFileSync(join(dir, "owner"), "utf8").trim();
|
|
33
|
+
const pid = Number(text);
|
|
34
|
+
return Number.isInteger(pid) && pid > 0 ? pid : 0;
|
|
35
|
+
} catch {
|
|
36
|
+
return 0;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function processAlive(pid) {
|
|
41
|
+
try {
|
|
42
|
+
process.kill(pid, 0);
|
|
43
|
+
return true;
|
|
44
|
+
} catch {
|
|
45
|
+
return false;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export function shadowFileBody(sessionKey, body) {
|
|
50
|
+
const key = String(sessionKey || "default").replace(/\s+/g, "");
|
|
51
|
+
return `# spine-session ${key}\n${body || ""}`;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export const STATS_NAME = "spine-stats.json";
|
|
55
|
+
|
|
56
|
+
export function readCutStats(dir) {
|
|
57
|
+
try {
|
|
58
|
+
const parsed = JSON.parse(readFileSync(join(dir, STATS_NAME), "utf8"));
|
|
59
|
+
return {
|
|
60
|
+
cuts: Number(parsed.cuts) || 0,
|
|
61
|
+
savedTokens: Number(parsed.savedTokens) || 0,
|
|
62
|
+
rewriteTokens: Number(parsed.rewriteTokens) || 0,
|
|
63
|
+
};
|
|
64
|
+
} catch {
|
|
65
|
+
return { cuts: 0, savedTokens: 0, rewriteTokens: 0 };
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export function recordCutStats(dir, { saved = 0, rewrite = 0 } = {}) {
|
|
70
|
+
if (!(saved > 0)) return readCutStats(dir);
|
|
71
|
+
try {
|
|
72
|
+
const file = join(dir, STATS_NAME);
|
|
73
|
+
const lock = join(dir, "spine-stats.lock");
|
|
74
|
+
const tmp = `${file}.${process.pid}-${randomBytes(8).toString("hex")}.tmp`;
|
|
75
|
+
let fd;
|
|
76
|
+
try {
|
|
77
|
+
fd = openSync(lock, "wx");
|
|
78
|
+
} catch {
|
|
79
|
+
return null;
|
|
80
|
+
}
|
|
81
|
+
try {
|
|
82
|
+
const prev = readCutStats(dir);
|
|
83
|
+
const locked = {
|
|
84
|
+
cuts: prev.cuts + 1,
|
|
85
|
+
savedTokens: prev.savedTokens + saved,
|
|
86
|
+
rewriteTokens: prev.rewriteTokens + rewrite,
|
|
87
|
+
updatedAt: new Date().toISOString(),
|
|
88
|
+
};
|
|
89
|
+
writeFileSync(tmp, JSON.stringify(locked));
|
|
90
|
+
renameSync(tmp, file);
|
|
91
|
+
return locked;
|
|
92
|
+
} finally {
|
|
93
|
+
try { unlinkSync(tmp); } catch {}
|
|
94
|
+
closeSync(fd);
|
|
95
|
+
try { unlinkSync(lock); } catch {}
|
|
96
|
+
}
|
|
97
|
+
} catch {
|
|
98
|
+
return null;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
export function shadowMaxBytes(env = process.env) {
|
|
103
|
+
const bytes = Number(env.SPINE_SHADOW_MAX_BYTES);
|
|
104
|
+
if (Number.isFinite(bytes) && bytes > 0) return Math.floor(bytes);
|
|
105
|
+
const mb = Number(env.SPINE_SHADOW_MAX_MB);
|
|
106
|
+
if (Number.isFinite(mb) && mb > 0) return Math.floor(mb * 1024 * 1024);
|
|
107
|
+
return 256 * 1024 * 1024;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
function readSessionKey(file) {
|
|
111
|
+
const fd = openSync(file, "r");
|
|
112
|
+
try {
|
|
113
|
+
const buf = Buffer.alloc(240);
|
|
114
|
+
const count = readSync(fd, buf, 0, buf.length, 0);
|
|
115
|
+
const line = buf.subarray(0, count).toString("utf8").split("\n")[0] || "";
|
|
116
|
+
return SESSION_MARK.exec(line)?.[1] || "";
|
|
117
|
+
} catch {
|
|
118
|
+
return "";
|
|
119
|
+
} finally {
|
|
120
|
+
closeSync(fd);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
export function listPluginShadows(dir) {
|
|
125
|
+
let names = [];
|
|
126
|
+
try {
|
|
127
|
+
names = readdirSync(dir);
|
|
128
|
+
} catch {
|
|
129
|
+
return [];
|
|
130
|
+
}
|
|
131
|
+
const files = [];
|
|
132
|
+
for (const name of names) {
|
|
133
|
+
if (!isPluginShadowName(name)) continue;
|
|
134
|
+
const path = resolve(dir, name);
|
|
135
|
+
try {
|
|
136
|
+
const info = statSync(path);
|
|
137
|
+
if (!info.isFile()) continue;
|
|
138
|
+
files.push({
|
|
139
|
+
name,
|
|
140
|
+
path,
|
|
141
|
+
size: info.size,
|
|
142
|
+
mtimeMs: info.mtimeMs,
|
|
143
|
+
sessionKey: readSessionKey(path),
|
|
144
|
+
});
|
|
145
|
+
} catch {}
|
|
146
|
+
}
|
|
147
|
+
return files;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
function byAge(left, right) {
|
|
151
|
+
return left.mtimeMs - right.mtimeMs || left.name.localeCompare(right.name);
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
export function listShadowEntries(dir, alive = processAlive) {
|
|
155
|
+
const entries = [];
|
|
156
|
+
let names = [];
|
|
157
|
+
try {
|
|
158
|
+
names = readdirSync(dir, { withFileTypes: true });
|
|
159
|
+
} catch {
|
|
160
|
+
return entries;
|
|
161
|
+
}
|
|
162
|
+
for (const entry of names) {
|
|
163
|
+
const path = resolve(dir, entry.name);
|
|
164
|
+
if (entry.isFile() && isPluginShadowName(entry.name)) {
|
|
165
|
+
try {
|
|
166
|
+
const info = statSync(path);
|
|
167
|
+
entries.push({
|
|
168
|
+
name: entry.name,
|
|
169
|
+
path,
|
|
170
|
+
size: info.size,
|
|
171
|
+
mtimeMs: info.mtimeMs,
|
|
172
|
+
sessionKey: "",
|
|
173
|
+
legacy: true,
|
|
174
|
+
live: false,
|
|
175
|
+
});
|
|
176
|
+
} catch {}
|
|
177
|
+
continue;
|
|
178
|
+
}
|
|
179
|
+
if (!entry.isDirectory()) continue;
|
|
180
|
+
const live = ownerPid(path) ? alive(ownerPid(path)) : false;
|
|
181
|
+
for (const file of listPluginShadows(path)) {
|
|
182
|
+
entries.push({ ...file, legacy: false, live, sessionKey: file.sessionKey || entry.name });
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
return entries;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
// 先删根目录旧文件,再删死会话,最后才删当前会话的旧文件。
|
|
189
|
+
// 当前卡片和这次引用的原文路径受保护。删不掉且仍然超限时返回 ok:false。
|
|
190
|
+
// Delete legacy root files, then dead sessions, then old files of this session.
|
|
191
|
+
// Protect the current card and cited raw paths. If unlink fails and the cap is still exceeded, ok is false.
|
|
192
|
+
export function rotateShadow({ dir, incomingBytes, protectPath, protectPaths = [], sessionKey, cap, unlink = unlinkSync, alive = processAlive }) {
|
|
193
|
+
const files = listShadowEntries(dir, alive);
|
|
194
|
+
const protect = new Set([protectPath, ...protectPaths].filter(Boolean).map((path) => resolve(path)));
|
|
195
|
+
const current = sanitizeSessionKey(sessionKey);
|
|
196
|
+
let used = files.reduce((sum, file) => sum + file.size, 0);
|
|
197
|
+
const low = Math.floor(cap * 0.8);
|
|
198
|
+
const oversize = incomingBytes > cap;
|
|
199
|
+
if (!oversize && used + incomingBytes <= cap) {
|
|
200
|
+
return { ok: true, deleted: 0, freed: 0, used, cap, oversize: false, failed: false };
|
|
201
|
+
}
|
|
202
|
+
const legacy = [];
|
|
203
|
+
const dead = [];
|
|
204
|
+
const mine = [];
|
|
205
|
+
for (const file of files) {
|
|
206
|
+
if (protect.has(file.path)) continue;
|
|
207
|
+
if (file.live && file.sessionKey !== current) continue;
|
|
208
|
+
if (file.legacy) legacy.push(file);
|
|
209
|
+
else if (file.sessionKey === current) mine.push(file);
|
|
210
|
+
else dead.push(file);
|
|
211
|
+
}
|
|
212
|
+
legacy.sort(byAge);
|
|
213
|
+
dead.sort(byAge);
|
|
214
|
+
mine.sort(byAge);
|
|
215
|
+
let deleted = 0;
|
|
216
|
+
let freed = 0;
|
|
217
|
+
let failed = false;
|
|
218
|
+
for (const file of [...legacy, ...dead, ...mine]) {
|
|
219
|
+
if (!oversize && used + incomingBytes <= low) break;
|
|
220
|
+
try {
|
|
221
|
+
unlink(file.path);
|
|
222
|
+
} catch (error) {
|
|
223
|
+
if (error?.code === "ENOENT") {
|
|
224
|
+
used -= file.size;
|
|
225
|
+
freed += file.size;
|
|
226
|
+
deleted += 1;
|
|
227
|
+
continue;
|
|
228
|
+
}
|
|
229
|
+
failed = true;
|
|
230
|
+
break;
|
|
231
|
+
}
|
|
232
|
+
used -= file.size;
|
|
233
|
+
freed += file.size;
|
|
234
|
+
deleted += 1;
|
|
235
|
+
if (file.name.startsWith("raw-")) {
|
|
236
|
+
try {
|
|
237
|
+
unlink(`${file.path}.meta.json`);
|
|
238
|
+
} catch {}
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
const fits = used + incomingBytes <= cap;
|
|
242
|
+
return {
|
|
243
|
+
ok: !failed && (oversize || fits),
|
|
244
|
+
deleted,
|
|
245
|
+
freed,
|
|
246
|
+
used,
|
|
247
|
+
cap,
|
|
248
|
+
oversize,
|
|
249
|
+
failed,
|
|
250
|
+
};
|
|
251
|
+
}
|
package/shell.js
ADDED
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 薄入口。这个文件必须能求值,引擎加载失败也不能把这里抛掉。
|
|
3
|
+
* Thin entry. This file must evaluate even when the engine cannot load.
|
|
4
|
+
* A throw here leaves stock disabled and /compact waiting.
|
|
5
|
+
*
|
|
6
|
+
* 配置预先检查失败时不构造引擎,也不再构造原版,避免报错被盖成「服务已被注册」。
|
|
7
|
+
* On a preflight failure, construct neither engine. Stock would register and hide the real error.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
let Engine = null;
|
|
11
|
+
let Stock = null;
|
|
12
|
+
let CompactionEngine = null;
|
|
13
|
+
let ManualCompactionError = null;
|
|
14
|
+
let loadError = null;
|
|
15
|
+
|
|
16
|
+
if (process.env.SPINE_ENGINE_FAIL === "import") {
|
|
17
|
+
loadError = new Error("spine: engine import refused");
|
|
18
|
+
} else {
|
|
19
|
+
try {
|
|
20
|
+
const engine = await import("./index.js");
|
|
21
|
+
Engine = engine.default;
|
|
22
|
+
} catch (error) {
|
|
23
|
+
loadError = error instanceof Error ? error : new Error(String(error));
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
try {
|
|
28
|
+
const compaction = await import("@deepseek-ai/dsh-compaction");
|
|
29
|
+
CompactionEngine = compaction.CompactionEngine;
|
|
30
|
+
ManualCompactionError = compaction.ManualCompactionError;
|
|
31
|
+
} catch (error) {
|
|
32
|
+
loadError ??= error instanceof Error ? error : new Error(String(error));
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
try {
|
|
36
|
+
const stock = await import("@deepseek-ai/dsh-compaction-basic");
|
|
37
|
+
Stock = stock.BasicCompactionEngine;
|
|
38
|
+
} catch (error) {
|
|
39
|
+
if (!loadError) loadError = error instanceof Error ? error : new Error(String(error));
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function reasonText(error) {
|
|
43
|
+
return error instanceof Error ? error.message : String(error);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export function preflightConfig(config = {}) {
|
|
47
|
+
if (!config || typeof config !== "object") return;
|
|
48
|
+
const threshold = config.thresholdRatio;
|
|
49
|
+
if (typeof threshold !== "number") return;
|
|
50
|
+
if (config.retainTokens !== undefined) return;
|
|
51
|
+
const ratio = config.retainRatio ?? 0.16;
|
|
52
|
+
if (ratio >= threshold) {
|
|
53
|
+
throw new Error(`BasicCompactionConfig: retainRatio (${ratio}) must be less than the resolved thresholdRatio (${threshold})`);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function releaseCompaction(ctx) {
|
|
58
|
+
const store = ctx?.reflect?.store;
|
|
59
|
+
if (!store) return false;
|
|
60
|
+
const keys = [...Object.getOwnPropertySymbols(store), ...Object.keys(store)];
|
|
61
|
+
for (const key of keys) {
|
|
62
|
+
if (store[key]?.name !== "compaction") continue;
|
|
63
|
+
delete store[key];
|
|
64
|
+
if (ctx.fiber?.store) delete ctx.fiber.store.compaction;
|
|
65
|
+
return true;
|
|
66
|
+
}
|
|
67
|
+
return false;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function visibleTakeover(ctx, reason) {
|
|
71
|
+
const line = `spine: ${MARK}。${reason}`;
|
|
72
|
+
console.error(line);
|
|
73
|
+
ctx?.logger?.warn?.(line);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
const MARK = "本会话未接管";
|
|
77
|
+
|
|
78
|
+
const UnavailableCompaction = CompactionEngine
|
|
79
|
+
? class extends CompactionEngine {
|
|
80
|
+
constructor(ctx, reason) {
|
|
81
|
+
super(ctx);
|
|
82
|
+
this.reason = reason;
|
|
83
|
+
ctx.logger?.warn?.(`spine: ${MARK}。压缩没有装上,这一刀没有执行。${reason}`);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
compactIfNeeded() {
|
|
87
|
+
return Promise.reject(new Error(`spine: ${MARK}。压缩没有装上,这一刀没有执行。${this.reason}`));
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
compactNow() {
|
|
91
|
+
const message = `spine: ${MARK}。压缩没有装上,这一刀没有执行。${this.reason}`;
|
|
92
|
+
if (ManualCompactionError) return Promise.reject(new ManualCompactionError("summary", message));
|
|
93
|
+
return Promise.reject(new Error(message));
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
: null;
|
|
97
|
+
|
|
98
|
+
export default class LiveCompactEntry {
|
|
99
|
+
static inject = ["llm", "tokenMeter", "sessions"];
|
|
100
|
+
static Config = Stock?.Config || Engine?.Config;
|
|
101
|
+
|
|
102
|
+
constructor(ctx, config) {
|
|
103
|
+
let cause = loadError;
|
|
104
|
+
let skipStock = process.env.SPINE_ENGINE_FAIL === "construct";
|
|
105
|
+
if (skipStock) cause = new Error("spine: constructor refused");
|
|
106
|
+
if (!loadError && !skipStock) {
|
|
107
|
+
try {
|
|
108
|
+
preflightConfig(config);
|
|
109
|
+
} catch (error) {
|
|
110
|
+
cause = error instanceof Error ? error : new Error(String(error));
|
|
111
|
+
skipStock = true;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
if (!loadError && !skipStock && Engine) {
|
|
115
|
+
try {
|
|
116
|
+
return new Engine(ctx, config);
|
|
117
|
+
} catch (error) {
|
|
118
|
+
cause = error instanceof Error ? error : new Error(String(error));
|
|
119
|
+
releaseCompaction(ctx);
|
|
120
|
+
skipStock = true;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
cause ??= new Error("spine: engine missing");
|
|
124
|
+
if (!skipStock && process.env.SPINE_STOCK_FAIL !== "1" && Stock) {
|
|
125
|
+
try {
|
|
126
|
+
visibleTakeover(ctx, `这一轮用原版压缩。${reasonText(cause)}`);
|
|
127
|
+
return new Stock(ctx, config);
|
|
128
|
+
} catch (error) {
|
|
129
|
+
cause = error instanceof Error ? error : new Error(String(error));
|
|
130
|
+
releaseCompaction(ctx);
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
visibleTakeover(ctx, reasonText(cause));
|
|
134
|
+
if (!UnavailableCompaction) throw new Error(`spine: ${MARK}。压缩接口没有装上。${reasonText(cause)}`);
|
|
135
|
+
return new UnavailableCompaction(ctx, reasonText(cause));
|
|
136
|
+
}
|
|
137
|
+
}
|