@nowcrew/daemon 0.6.99 → 0.6.100-beta.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cloud-report-upload.js +1 -10
- package/dist/computer-main.js +0 -0
- package/dist/console.js +17 -2
- package/dist/execution-journal.js +3 -1
- package/dist/execution-protocol.js +1 -1
- package/dist/execution-runner.js +4 -1
- package/dist/local-execution-prompt.js +8 -3
- package/dist/local-executor.js +78 -55
- package/dist/local-memory/commit-telemetry.js +30 -0
- package/dist/local-memory/execution-workspace.js +62 -0
- package/dist/local-memory/full-index-search.js +47 -0
- package/dist/local-memory/index-budget.js +9 -3
- package/dist/local-memory/initialization-identity.js +36 -0
- package/dist/local-memory/inventory-cli.js +52 -8
- package/dist/local-memory/inventory-source.js +15 -8
- package/dist/local-memory/inventory-v2.js +362 -0
- package/dist/local-memory/legacy-adapter.js +233 -0
- package/dist/local-memory/migration-access.js +242 -0
- package/dist/local-memory/migration-cli.js +54 -0
- package/dist/local-memory/migration-io.js +199 -0
- package/dist/local-memory/migration-pending.js +9 -0
- package/dist/local-memory/migration-plan.js +432 -0
- package/dist/local-memory/migration-schema.js +55 -0
- package/dist/local-memory/migration-state-invariants.js +65 -0
- package/dist/local-memory/migration-store.js +414 -0
- package/dist/local-memory/migration-tool-entry.js +21 -0
- package/dist/local-memory/pending-query.js +5 -2
- package/dist/local-memory/pending-schema.js +1 -1
- package/dist/local-memory/pending.js +16 -2
- package/dist/local-memory/prompt.js +21 -5
- package/dist/local-memory/read-cli.js +98 -76
- package/dist/local-memory/release-artifact-guard.js +55 -0
- package/dist/local-memory/rollout.js +79 -11
- package/dist/local-memory/runtime.js +191 -17
- package/dist/local-memory/source-path.js +13 -0
- package/dist/local-memory/storage-format.js +249 -0
- package/dist/local-memory/write.js +190 -21
- package/dist/local-memory/writer-mode.js +13 -0
- package/dist/local-memory-diagnostics.js +96 -15
- package/dist/local-memory-telemetry.js +20 -3
- package/dist/machine-info.js +6 -1
- package/dist/main.js +0 -0
- package/dist/marketplace-controller.js +21 -8
- package/dist/marketplace-maintenance-bridge.js +79 -0
- package/dist/marketplace-maintenance-protocol.js +40 -0
- package/dist/marketplace-protocol.js +1 -1
- package/dist/marketplace-remote.js +1 -1
- package/dist/marketplace-runner.js +1 -1
- package/dist/marketplace-serve.js +4 -2
- package/dist/memory-migration-controller.js +89 -0
- package/dist/memory-migration-inspect.js +108 -0
- package/dist/memory-migration-journal.js +14 -0
- package/dist/memory-migration-protocol.js +14 -0
- package/dist/memory-migration-runtime.js +305 -0
- package/dist/memory-prune-diagnostics.js +74 -4
- package/dist/message-file-preview.js +11 -1
- package/dist/prompt.js +9 -17
- package/dist/runner.js +1 -1
- package/dist/runtimes/claude-thread-runner.js +25 -4
- package/dist/runtimes/pi-runner.js +1 -0
- package/dist/runtimes/pi.js +54 -20
- package/dist/serve.js +39 -4
- package/dist/token.js +1 -0
- package/dist/workspace-control-handler.js +16 -3
- package/dist/workspace.js +13 -2
- package/package.json +9 -10
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { dslog } from "../slog.js";
|
|
2
2
|
import { renderPendingMemoryContext, searchPendingMemoryCandidates } from "./pending.js";
|
|
3
3
|
import { renderLocalMemoryResultInstructions, renderLocalMemoryV2WriteGuard } from "./runtime.js";
|
|
4
|
-
import { LOCAL_MEMORY_V2_DEFERRED_GUIDANCE } from "./rollout.js";
|
|
5
|
-
const PENDING_READ_GUIDANCE = "\n\nV2新增资料预览未命中不代表没有资料;先查有效索引,必要时用 crew-memory search-pending --query '关键词',按 nextCursor 续查;用 read-pending --id ID
|
|
4
|
+
import { LOCAL_MEMORY_V2_DEFERRED_GUIDANCE, LOCAL_MEMORY_LEGACY_GUIDANCE } from "./rollout.js";
|
|
5
|
+
const PENDING_READ_GUIDANCE = "\n\nV2新增资料预览未命中不代表没有资料;先查有效索引,必要时用 crew-memory search-pending --query '关键词',按 nextCursor 续查;用 read-pending --id ID 分页读正文。容量暂存或迁移延期资料不能覆盖现行规则,未应用修改不作为记忆;迁移延期候选必须等 reviewed migration 后再按当前 ID/revision/hash 处理。用 crew-memory --help 查看参数。\n";
|
|
6
6
|
export async function prepareLocalMemoryPrompt(input) {
|
|
7
7
|
let pending = "";
|
|
8
8
|
if (input.enabled) {
|
|
@@ -22,10 +22,11 @@ export async function prepareLocalMemoryPrompt(input) {
|
|
|
22
22
|
}
|
|
23
23
|
}
|
|
24
24
|
const original = input.renderBase(0);
|
|
25
|
-
const guard = input.deferred ? LOCAL_MEMORY_V2_DEFERRED_GUIDANCE
|
|
25
|
+
const guard = input.legacyFallback ? LOCAL_MEMORY_LEGACY_GUIDANCE : input.deferred ? LOCAL_MEMORY_V2_DEFERRED_GUIDANCE
|
|
26
26
|
: input.strictRollout && input.enabled && input.resultPath === null ? renderLocalMemoryV2WriteGuard() : "";
|
|
27
|
-
const structured = input.resultPath === null ? "" : renderLocalMemoryResultInstructions(input.resultPath);
|
|
27
|
+
const structured = input.resultPath === null ? "" : renderLocalMemoryResultInstructions(input.resultPath, input.storageVersion, input.migrationDeferred === true);
|
|
28
28
|
const guidance = input.enabled ? PENDING_READ_GUIDANCE : "";
|
|
29
|
+
let requiredMemoryBudgetError;
|
|
29
30
|
// 短命令指引放在当前轮,独立于可舍弃预览;收尾契约仍保持系统与当前轮两份。
|
|
30
31
|
for (const choice of [
|
|
31
32
|
{ suffix: pending + guard + structured, wake: guard + structured + guidance },
|
|
@@ -33,11 +34,26 @@ export async function prepareLocalMemoryPrompt(input) {
|
|
|
33
34
|
{ suffix: guard + structured, wake: guard + structured },
|
|
34
35
|
]) {
|
|
35
36
|
const reserved = Buffer.byteLength(choice.suffix + choice.wake, "utf8");
|
|
36
|
-
|
|
37
|
+
let base;
|
|
38
|
+
try {
|
|
39
|
+
base = reserved === 0 ? original : input.renderBase(reserved);
|
|
40
|
+
}
|
|
41
|
+
catch (error) {
|
|
42
|
+
// 可选候选先让出预算;最后仍不能容纳身份/置顶时必须拒绝,不能省略必需记忆。
|
|
43
|
+
if (input.storageVersion === 2 && error instanceof Error
|
|
44
|
+
&& ["local_memory_identity_budget_exceeded", "local_memory_pinned_budget_exceeded"].includes(error.message)) {
|
|
45
|
+
requiredMemoryBudgetError = error;
|
|
46
|
+
continue;
|
|
47
|
+
}
|
|
48
|
+
throw error;
|
|
49
|
+
}
|
|
50
|
+
const systemPrompt = base + choice.suffix;
|
|
37
51
|
if (input.maxBytes === undefined || Buffer.byteLength(systemPrompt + choice.wake, "utf8") <= input.maxBytes) {
|
|
38
52
|
return { systemPrompt, resultPath: input.resultPath, wakePromptSuffix: choice.wake };
|
|
39
53
|
}
|
|
40
54
|
}
|
|
55
|
+
if (requiredMemoryBudgetError)
|
|
56
|
+
throw requiredMemoryBudgetError;
|
|
41
57
|
if (input.strictRollout)
|
|
42
58
|
throw new Error("local_memory_v2_prompt_over_budget");
|
|
43
59
|
// 强制基础指令与契约本身仍无法共存时,保留既有非结构化兼容回退,不裁基础指令。
|
|
@@ -6,7 +6,18 @@ import { pathToFileURL } from "node:url";
|
|
|
6
6
|
import { parseArgs, TextDecoder } from "node:util";
|
|
7
7
|
import { z } from "zod";
|
|
8
8
|
import { readPendingMemoryBody, searchPendingMemoryPage } from "./pending-query.js";
|
|
9
|
+
import { detectLocalMemoryStorageVersion, parseFullMemoryIndex } from "./storage-format.js";
|
|
10
|
+
import { searchFullMemoryIndex } from "./full-index-search.js";
|
|
11
|
+
import { searchLegacyMemorySources } from "./legacy-adapter.js";
|
|
12
|
+
import { withMemoryAccess } from "./migration-access.js";
|
|
13
|
+
import { readMemoryInventorySource, scanMemorySources } from "./inventory-source.js";
|
|
14
|
+
import { MigrationStateSchema } from "./migration-schema.js";
|
|
15
|
+
import { findControlledPreservedSourcePaths } from "./inventory-v2.js";
|
|
9
16
|
const OUTPUT_BYTES = 12 * 1024;
|
|
17
|
+
// A V2 page may carry coverage metadata for legacy sources. Reserve room for
|
|
18
|
+
// that metadata so a mixed result never exceeds the command's output limit.
|
|
19
|
+
const FULL_INDEX_RESULT_BYTES = 4 * 1024;
|
|
20
|
+
const FULL_INDEX_LEGACY_SCAN_BYTES = OUTPUT_BYTES - 4 * 1024;
|
|
10
21
|
const FILE_BYTES = 32 * 1024 * 1024;
|
|
11
22
|
const READ_LIMIT_MAX = 4_000;
|
|
12
23
|
const READ_LIMIT_SAFE = 2_000;
|
|
@@ -27,11 +38,19 @@ const Common = {
|
|
|
27
38
|
ifHash: z.string().regex(/^[a-f0-9]{64}$/u).optional(),
|
|
28
39
|
};
|
|
29
40
|
const CommandSchema = z.discriminatedUnion("command", [
|
|
41
|
+
z.object({
|
|
42
|
+
command: z.literal("search-history"), ...Common,
|
|
43
|
+
query: z.string().trim().min(1).max(256),
|
|
44
|
+
limit: z.coerce.number().int().min(1).max(Number.MAX_SAFE_INTEGER).default(10),
|
|
45
|
+
afterLine: z.coerce.number().int().min(0).max(Number.MAX_SAFE_INTEGER).default(0),
|
|
46
|
+
cursor: z.string().min(1).max(4096).optional(),
|
|
47
|
+
}).strict(),
|
|
30
48
|
z.object({
|
|
31
49
|
command: z.literal("search"), ...Common,
|
|
32
50
|
query: z.string().trim().min(1).max(256),
|
|
33
51
|
limit: z.coerce.number().int().min(1).max(Number.MAX_SAFE_INTEGER).default(10),
|
|
34
52
|
afterLine: z.coerce.number().int().min(0).max(Number.MAX_SAFE_INTEGER).default(0),
|
|
53
|
+
cursor: z.string().min(1).max(4096).optional(),
|
|
35
54
|
}).strict(),
|
|
36
55
|
z.object({
|
|
37
56
|
command: z.literal("read"), ...Common,
|
|
@@ -53,7 +72,8 @@ const CommandSchema = z.discriminatedUnion("command", [
|
|
|
53
72
|
}).strict(),
|
|
54
73
|
]);
|
|
55
74
|
const USAGE = `只读记忆工具,默认输出 JSON,不修改任何记忆文件。
|
|
56
|
-
crew-memory search --query <关键词> [--file MEMORY.md|notes/文件.md] [--limit 10] [--after-line 0]
|
|
75
|
+
crew-memory search --query <关键词> [--file MEMORY.md|notes/文件.md] [--limit 10] [--after-line 0] [--cursor <游标>]
|
|
76
|
+
crew-memory search-history --query <关键词> [--limit 10] [--after-line 0] [--cursor <游标>] [--if-hash <sha256>]
|
|
57
77
|
crew-memory read [--file MEMORY.md|notes/文件.md] [--offset 0] [--limit 2000]
|
|
58
78
|
crew-memory search-pending --query <关键词> [--cursor <游标>] [--limit 10]
|
|
59
79
|
crew-memory read-pending --id <候选ID> [--offset 0] [--limit 2000] [--if-hash <sha256>]
|
|
@@ -62,11 +82,12 @@ search 关键词按空白分隔,同一行须全部命中;返回完整文件
|
|
|
62
82
|
读取的 offset/limit 按 Unicode 字符计数;用 nextOffset 继续,eof=true 表示结束。
|
|
63
83
|
读取的 --limit 超出范围时,工具会自动降到 2000 并在结果中标记 warning;保留原文件或候选 ID 及 offset,按 nextOffset 分页,不要一次读完整文件。
|
|
64
84
|
后续页可传 --if-hash <前一页的 sha256>,文件变化时停止,避免混读不同版本。
|
|
65
|
-
|
|
66
|
-
search-
|
|
85
|
+
schema=2 的 search 查询完整有效目录(active/pinned),按标题、摘要、触发词联合匹配并返回 ID、revision 和正文引用;search-history 显式查询历史/失效记录。预览未展示不代表记录不存在。
|
|
86
|
+
旧格式 search/search-history 及 read 读取原文;search-history 是显式 legacy fallback,不把旧内容当作已结构化 V2 记录;read 按指定文件和哈希分页,不扫描 pending。
|
|
87
|
+
search-pending 只查容量延期或迁移延期的 create 候选;有 nextCursor 就继续,即使本页 matches 为空。
|
|
67
88
|
complete=true 才表示全部候选已检索完成且无遗漏;warnings 或非零退出码表示读取未完成,不能当作零命中。
|
|
68
89
|
read-pending 只返回候选正文;offset 非零时必须传 --if-hash,update/supersede 候选不可读取。
|
|
69
|
-
|
|
90
|
+
候选只供相关问题参考,未生效,不能覆盖有效规则;先读取当前有效索引与明细。迁移延期候选会标记为 migration_deferred,必须等 reviewed migration 后再按当前 ID/revision/hash 处理。`;
|
|
70
91
|
function inside(root, path) {
|
|
71
92
|
const value = relative(root, path);
|
|
72
93
|
return value !== ".." && !value.startsWith(`..${sep}`) && !isAbsolute(value);
|
|
@@ -107,33 +128,21 @@ async function readSource(home, file) {
|
|
|
107
128
|
await handle.close();
|
|
108
129
|
}
|
|
109
130
|
}
|
|
110
|
-
function
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
return `${value}...`;
|
|
122
|
-
value += character;
|
|
131
|
+
async function readControlledPreservedSourcePaths(home, memory) {
|
|
132
|
+
try {
|
|
133
|
+
const root = await realpath(home);
|
|
134
|
+
const stateSource = await readMemoryInventorySource(root, ".crew/memory/migration.v1.json", FILE_BYTES);
|
|
135
|
+
const state = MigrationStateSchema.parse(JSON.parse(stateSource.content));
|
|
136
|
+
if (state.phase !== "committed" || state.storageVersion !== 2)
|
|
137
|
+
return new Set();
|
|
138
|
+
const scan = await scanMemorySources(root, { includeOperationalFiles: false });
|
|
139
|
+
const sourceHashes = new Map(state.manifest.files
|
|
140
|
+
.flatMap(file => file.before.exists ? [[file.path, file.before.sha256]] : []));
|
|
141
|
+
return new Set(findControlledPreservedSourcePaths(parseFullMemoryIndex(memory), scan.sources, state.manifest.recordMappings, sourceHashes));
|
|
123
142
|
}
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
function originalLowercaseIndex(text, index) {
|
|
127
|
-
let originalIndex = 0;
|
|
128
|
-
let lowerIndex = 0;
|
|
129
|
-
for (const character of text) {
|
|
130
|
-
// 逐字符只累计小写长度;匹配仍用整行小写,保留长度不变的词尾 sigma 上下文规则。
|
|
131
|
-
lowerIndex += character.toLowerCase().length;
|
|
132
|
-
if (index < lowerIndex)
|
|
133
|
-
return originalIndex;
|
|
134
|
-
originalIndex += character.length;
|
|
143
|
+
catch {
|
|
144
|
+
return new Set();
|
|
135
145
|
}
|
|
136
|
-
return text.length;
|
|
137
146
|
}
|
|
138
147
|
function normalizeLimit(requestedLimit, max, safe) {
|
|
139
148
|
if (requestedLimit <= max)
|
|
@@ -143,50 +152,6 @@ function normalizeLimit(requestedLimit, max, safe) {
|
|
|
143
152
|
warning: { code: "memory_limit_clamped", requestedLimit, appliedLimit: safe, recovered: true },
|
|
144
153
|
};
|
|
145
154
|
}
|
|
146
|
-
function searchSource(text, sha256, command, warning) {
|
|
147
|
-
const terms = command.query.toLowerCase().split(/\s+/u);
|
|
148
|
-
const matches = [];
|
|
149
|
-
let line = 0;
|
|
150
|
-
let offset = 0;
|
|
151
|
-
let heading = "";
|
|
152
|
-
let matchCount = 0;
|
|
153
|
-
let hasMore = false;
|
|
154
|
-
let remaining = OUTPUT_BYTES - Buffer.byteLength(JSON.stringify({ file: command.file, sha256 })) - 512;
|
|
155
|
-
for (const match of text.matchAll(/[^\n]*(?:\n|$)/gu)) {
|
|
156
|
-
if (match[0] === "")
|
|
157
|
-
break;
|
|
158
|
-
line += 1;
|
|
159
|
-
const raw = match[0];
|
|
160
|
-
const content = raw.replace(/\r?\n$/u, "");
|
|
161
|
-
const title = /^#{1,6}\s+(.+?)\s*$/u.exec(content)?.[1];
|
|
162
|
-
if (title !== undefined)
|
|
163
|
-
heading = excerpt(title, 96);
|
|
164
|
-
const lower = content.toLowerCase();
|
|
165
|
-
if (terms.every((term) => lower.includes(term))) {
|
|
166
|
-
matchCount += 1;
|
|
167
|
-
if (line > command.afterLine) {
|
|
168
|
-
let start = Math.max(0, originalLowercaseIndex(content, lower.indexOf(terms[0])) - 60);
|
|
169
|
-
if (start > 0 && /[\uDC00-\uDFFF]/u.test(content[start]))
|
|
170
|
-
start -= 1;
|
|
171
|
-
const item = { line, offset, heading, text: `${start > 0 ? "..." : ""}${excerpt(content.slice(start), 240)}` };
|
|
172
|
-
const bytes = Buffer.byteLength(JSON.stringify(item), "utf8") + 1;
|
|
173
|
-
if (hasMore || matches.length >= command.limit || bytes > remaining) {
|
|
174
|
-
hasMore = true;
|
|
175
|
-
}
|
|
176
|
-
else {
|
|
177
|
-
matches.push(item);
|
|
178
|
-
remaining -= bytes;
|
|
179
|
-
}
|
|
180
|
-
}
|
|
181
|
-
}
|
|
182
|
-
offset += characterCount(raw);
|
|
183
|
-
}
|
|
184
|
-
return {
|
|
185
|
-
file: command.file, sha256, matches, matchCount, hasMore,
|
|
186
|
-
nextAfterLine: hasMore ? matches.at(-1)?.line ?? null : null,
|
|
187
|
-
...(warning === undefined ? {} : { warning, requestedLimit: warning.requestedLimit, limit: warning.appliedLimit }),
|
|
188
|
-
};
|
|
189
|
-
}
|
|
190
155
|
function readPage(text, source, command, warning) {
|
|
191
156
|
const metadata = {
|
|
192
157
|
...source,
|
|
@@ -232,6 +197,19 @@ function writeResult(result, io) {
|
|
|
232
197
|
io.out(output);
|
|
233
198
|
}
|
|
234
199
|
export async function runMemoryReadCommand(argv, io) {
|
|
200
|
+
if (!io.env.CREW_HOME || argv.includes("--help") || argv.includes("-h"))
|
|
201
|
+
return runMemoryReadUnlocked(argv, io);
|
|
202
|
+
const pendingOnly = argv[0] === "search-pending" || argv[0] === "read-pending";
|
|
203
|
+
try {
|
|
204
|
+
return await withMemoryAccess(io.env.CREW_HOME, () => runMemoryReadUnlocked(argv, io), pendingOnly ? { pendingOnly: true } : {});
|
|
205
|
+
}
|
|
206
|
+
catch (error) {
|
|
207
|
+
const reason = error instanceof Error && /^(?:local_)?memory_[a-z_]+$/u.test(error.message) ? error.message : "memory_migration_access_failed";
|
|
208
|
+
io.err(JSON.stringify({ error: reason, message: "记忆读取受到迁移或状态保护,不能当作没有记忆。" }));
|
|
209
|
+
return 1;
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
async function runMemoryReadUnlocked(argv, io) {
|
|
235
213
|
let command;
|
|
236
214
|
try {
|
|
237
215
|
const { values, positionals } = parseArgs({
|
|
@@ -264,6 +242,7 @@ export async function runMemoryReadCommand(argv, io) {
|
|
|
264
242
|
io.err(`memory_arguments_invalid: 参数不合法,记忆读取未完成;请按以下参数范围修正后重试,不要当作没有记忆。\n${USAGE}`);
|
|
265
243
|
return 2;
|
|
266
244
|
}
|
|
245
|
+
const fileWasExplicit = argv.some((arg) => arg === "--file" || arg.startsWith("--file="));
|
|
267
246
|
try {
|
|
268
247
|
if (!io.env.CREW_HOME)
|
|
269
248
|
throw new Error("memory_home_missing");
|
|
@@ -289,10 +268,53 @@ export async function runMemoryReadCommand(argv, io) {
|
|
|
289
268
|
const source = await readSource(io.env.CREW_HOME, command.file);
|
|
290
269
|
if (command.ifHash !== undefined && command.ifHash !== source.sha256)
|
|
291
270
|
throw new Error("memory_file_changed");
|
|
292
|
-
const
|
|
293
|
-
|
|
271
|
+
const fullIndex = command.file === "MEMORY.md" && detectLocalMemoryStorageVersion(source.text) === 2;
|
|
272
|
+
if ((command.command === "search" || command.command === "search-history") && fullIndex) {
|
|
273
|
+
const normalized = normalizeLimit(command.limit, SEARCH_LIMIT_MAX, SEARCH_LIMIT_SAFE);
|
|
274
|
+
const result = searchFullMemoryIndex(source.text, source.sha256, {
|
|
275
|
+
...command, limit: normalized.limit, history: command.command === "search-history",
|
|
276
|
+
}, FULL_INDEX_RESULT_BYTES);
|
|
277
|
+
const indexedFiles = new Set(["MEMORY.md", ...parseFullMemoryIndex(source.text).map((match) => match.file)]);
|
|
278
|
+
const preservedSourceFiles = await readControlledPreservedSourcePaths(io.env.CREW_HOME, source.text);
|
|
279
|
+
const legacyExcludedFiles = fileWasExplicit
|
|
280
|
+
? indexedFiles
|
|
281
|
+
: new Set([...indexedFiles, ...preservedSourceFiles]);
|
|
282
|
+
const legacy = await searchLegacyMemorySources({ agentDir: io.env.CREW_HOME, query: command.query,
|
|
283
|
+
limit: normalized.limit, outputBytes: FULL_INDEX_LEGACY_SCAN_BYTES,
|
|
284
|
+
...(command.cursor === undefined ? {} : { cursor: command.cursor }),
|
|
285
|
+
...(fileWasExplicit ? { files: [command.file], excludeFiles: legacyExcludedFiles } : { excludeFiles: legacyExcludedFiles }) });
|
|
286
|
+
if (result.matches.length === 0 && (command.cursor === undefined || legacy.matchCount > 0)) {
|
|
287
|
+
if (legacy.unindexedFiles.length > 0 && (legacy.matchCount > 0 || result.matchCount === 0)) {
|
|
288
|
+
writeResult({ ...legacy, ...(normalized.warning ? { warning: normalized.warning,
|
|
289
|
+
requestedLimit: normalized.warning.requestedLimit, limit: normalized.warning.appliedLimit } : {}) }, io);
|
|
290
|
+
return 0;
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
writeResult({ source: "v2", legacy: false, ...result,
|
|
294
|
+
unindexedFiles: legacy.unindexedFiles,
|
|
295
|
+
unindexedFileCount: legacy.unindexedFileCount,
|
|
296
|
+
unindexedFilesTruncated: legacy.unindexedFilesTruncated,
|
|
297
|
+
complete: result.complete && legacy.matchCount === 0 && legacy.complete,
|
|
298
|
+
...(legacy.matchCount === 0 ? {} : { unindexedMatchCount: legacy.matchCount }),
|
|
299
|
+
...(legacy.matchCount === 0 ? {} : {
|
|
300
|
+
legacyHasMore: legacy.hasMore,
|
|
301
|
+
legacyNextCursor: legacy.nextCursor,
|
|
302
|
+
legacyNextAfterLine: legacy.nextAfterLine,
|
|
303
|
+
v2AfterLine: result.matches.at(-1)?.line ?? result.nextAfterLine,
|
|
304
|
+
}),
|
|
305
|
+
...(normalized.warning ? { warning: normalized.warning,
|
|
306
|
+
requestedLimit: normalized.warning.requestedLimit, limit: normalized.warning.appliedLimit } : {}) }, io);
|
|
307
|
+
return 0;
|
|
308
|
+
}
|
|
309
|
+
const result = command.command === "search" || command.command === "search-history"
|
|
310
|
+
? await (async () => {
|
|
294
311
|
const normalized = normalizeLimit(command.limit, SEARCH_LIMIT_MAX, SEARCH_LIMIT_SAFE);
|
|
295
|
-
|
|
312
|
+
const legacy = await searchLegacyMemorySources({ agentDir: io.env.CREW_HOME, query: command.query,
|
|
313
|
+
limit: normalized.limit, outputBytes: OUTPUT_BYTES - 256, afterLine: command.afterLine,
|
|
314
|
+
...(command.cursor === undefined ? {} : { cursor: command.cursor }),
|
|
315
|
+
...(fileWasExplicit ? { files: [command.file] } : {}) });
|
|
316
|
+
return { ...legacy, ...(normalized.warning ? { warning: normalized.warning,
|
|
317
|
+
requestedLimit: normalized.warning.requestedLimit, limit: normalized.warning.appliedLimit } : {}) };
|
|
296
318
|
})()
|
|
297
319
|
: (() => {
|
|
298
320
|
const normalized = normalizeLimit(command.limit, READ_LIMIT_MAX, READ_LIMIT_SAFE);
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { randomUUID } from "node:crypto";
|
|
2
|
+
import { writeFile } from "node:fs/promises";
|
|
3
|
+
import { join, resolve } from "node:path";
|
|
4
|
+
import { boundedDiagnosticJsonArray } from "../diagnostic-json.js";
|
|
5
|
+
import { dslog } from "../slog.js";
|
|
6
|
+
import { diffMemoryPruneNotes, releaseArtifactNoteChanges, } from "../memory-prune-diagnostics.js";
|
|
7
|
+
const trustedGuardTokens = new Map();
|
|
8
|
+
/**
|
|
9
|
+
* Keep an artifact detection sticky across retries in this daemon process. The
|
|
10
|
+
* marker is audit evidence only; its contents are checked against a token held
|
|
11
|
+
* in daemon memory so a model cannot pre-create the file and cause a denial
|
|
12
|
+
* of service. Cross-restart reconciliation belongs to the future task-level
|
|
13
|
+
* artifact receipt design.
|
|
14
|
+
*/
|
|
15
|
+
export async function checkReleaseArtifactGuard(input) {
|
|
16
|
+
const guardPath = join(input.runDir, ".memory-release-artifact-detected");
|
|
17
|
+
const guardKey = `${resolve(input.runDir)}:${input.scopeKey ?? "unknown"}`;
|
|
18
|
+
if (trustedGuardTokens.has(guardKey))
|
|
19
|
+
return { blocked: true, guardPath, changes: [], reason: "sticky_guard" };
|
|
20
|
+
const diff = diffMemoryPruneNotes(input.before.notes_manifest, input.after.notes_manifest);
|
|
21
|
+
if (!diff.evidence_complete || diff.truncated) {
|
|
22
|
+
return { blocked: true, guardPath, changes: [], reason: "evidence_incomplete" };
|
|
23
|
+
}
|
|
24
|
+
const changes = releaseArtifactNoteChanges(diff);
|
|
25
|
+
if (changes.length === 0)
|
|
26
|
+
return { blocked: false, guardPath, changes, reason: null };
|
|
27
|
+
const token = randomUUID();
|
|
28
|
+
trustedGuardTokens.set(guardKey, token);
|
|
29
|
+
await writeFile(guardPath, `release_artifact_guard_v1\n${token}\n`, { encoding: "utf8", mode: 0o600, flag: "wx" })
|
|
30
|
+
.catch((error) => {
|
|
31
|
+
if (error.code !== "EEXIST")
|
|
32
|
+
throw error;
|
|
33
|
+
});
|
|
34
|
+
return { blocked: true, guardPath, changes, reason: "release_artifact" };
|
|
35
|
+
}
|
|
36
|
+
export async function checkReleaseArtifactGuardBeforeCommit(input) {
|
|
37
|
+
const guardPath = join(input.runDir, ".memory-release-artifact-detected");
|
|
38
|
+
if (input.before === null)
|
|
39
|
+
return { blocked: true, guardPath, changes: [], reason: "evidence_unavailable" };
|
|
40
|
+
return checkReleaseArtifactGuard({ runDir: input.runDir,
|
|
41
|
+
...(input.scopeKey === undefined ? {} : { scopeKey: input.scopeKey }),
|
|
42
|
+
before: input.before, after: await input.inspectAfter() });
|
|
43
|
+
}
|
|
44
|
+
export function throwReleaseArtifactGuardFailure(input) {
|
|
45
|
+
const paths = boundedDiagnosticJsonArray(input.result.changes.map((change) => change.relative_path));
|
|
46
|
+
dslog("memory_prune.release_artifact_detected", "检测到发布证据或不完整文件证据,停止提交并保留工作日志", {
|
|
47
|
+
level: "ERROR", execution_id: input.executionId, agent_handle: input.agentHandle, task_key: input.taskKey,
|
|
48
|
+
prune_trace_id: input.pruneTraceId, release_count: input.result.changes.length, release_paths: paths.json,
|
|
49
|
+
release_paths_truncated: paths.truncated, manual_action_required: true, artifact_cleanup: "not_attempted"
|
|
50
|
+
});
|
|
51
|
+
throw new Error(input.result.reason === "evidence_incomplete"
|
|
52
|
+
? "local_memory_release_artifact_evidence_incomplete"
|
|
53
|
+
: input.result.reason === "evidence_unavailable"
|
|
54
|
+
? "local_memory_release_artifact_evidence_unavailable" : "local_memory_release_artifact_detected");
|
|
55
|
+
}
|
|
@@ -7,30 +7,80 @@ import { inventoryLimits, readMemoryInventorySource } from "./inventory-source.j
|
|
|
7
7
|
import { previewMemoryInventory } from "./inventory-preview.js";
|
|
8
8
|
import { parseLocalMemoryIndex } from "./write.js";
|
|
9
9
|
import { dslog } from "../slog.js";
|
|
10
|
+
import { createHash } from "node:crypto";
|
|
11
|
+
import { detectLocalMemoryStorageVersion, parseFullMemoryIndex } from "./storage-format.js";
|
|
12
|
+
import { MigrationStateSchema } from "./migration-schema.js";
|
|
13
|
+
import { assertMigrationStateIntegrity } from "./migration-state-invariants.js";
|
|
14
|
+
const TRANSIENT_INSPECTION_ERRORS = new Set([
|
|
15
|
+
"EACCES", "EPERM", "EIO", "EBUSY", "EAGAIN", "EINTR", "ESTALE", "EMFILE", "ENFILE", "ENOMEM", "ETIMEDOUT",
|
|
16
|
+
"source_changed", "memory_migration_access_busy",
|
|
17
|
+
]);
|
|
18
|
+
export function localMemoryRolloutFailure(error) {
|
|
19
|
+
const code = error?.code;
|
|
20
|
+
const reason = error instanceof Error ? error.message : "";
|
|
21
|
+
return (typeof code === "string" && TRANSIENT_INSPECTION_ERRORS.has(code))
|
|
22
|
+
|| TRANSIENT_INSPECTION_ERRORS.has(reason)
|
|
23
|
+
? "inspection_failed"
|
|
24
|
+
: "requires_migration";
|
|
25
|
+
}
|
|
10
26
|
export const LOCAL_MEMORY_V2_DEFERRED_GUIDANCE = `
|
|
11
27
|
|
|
12
28
|
## 本轮本地记忆兼容状态
|
|
13
29
|
开发环境已开放 V2,但该 Agent 的旧记忆尚未通过格式兼容检查,本轮未启用结构化 V2。
|
|
14
30
|
本节覆盖前文和旧会话中直接改写共享记忆的要求:本轮 MEMORY.md 与 notes 只读,不得改写、迁移或清空。
|
|
15
31
|
用户要求新增或更新长期规则时,内容和授权均只记录到本任务的 $CREW_TASK_LOG;保留日志,不要清空,不要宣称长期记忆已生效。旧资料兼容处理完成前,记忆收尾会明确报告需要处理。`;
|
|
16
|
-
export
|
|
17
|
-
|
|
18
|
-
|
|
32
|
+
export const LOCAL_MEMORY_LEGACY_GUIDANCE = `
|
|
33
|
+
|
|
34
|
+
## 本轮本地记忆兼容状态
|
|
35
|
+
本 Agent 尚未完成 V2 格式转换,本轮继续原记忆流程。用户明确要求新增或修改长期规则时,可按原规则最小范围更新共享记忆;记忆收尾继续依据任务日志更新原 MEMORY.md 和 notes,确认成功后再清理日志。
|
|
36
|
+
本节覆盖旧会话中因等待 V2 转换而冻结共享记忆写入的要求,但不是新的用户写入授权;本任务如明确要求不更新记忆,继续遵守。
|
|
37
|
+
保留原身份、目标及无关资料,不自行迁移或重置文件,不宣称已启用 V2。`;
|
|
38
|
+
export function reportLocalMemoryV2Deferred(executionId, handle, memoryPrune, legacyFallback = false, failure = "requires_migration", allowPendingOnly = false) {
|
|
39
|
+
dslog("local_memory.v2_deferred", allowPendingOnly
|
|
40
|
+
? "迁移尚未完成,新增候选隔离保存到 pending"
|
|
41
|
+
: legacyFallback ? "旧格式继续原记忆流程,转换成功后再启用V2" : "记忆格式异常,保留原资料和待处理日志", {
|
|
42
|
+
execution_id: executionId,
|
|
43
|
+
agent_handle: handle,
|
|
44
|
+
reason: legacyFallback ? "legacy_compatible" : failure,
|
|
45
|
+
writer_mode: allowPendingOnly ? "pending_only" : legacyFallback ? "legacy" : "blocked",
|
|
46
|
+
...(allowPendingOnly ? { pending_reason: "legacy_requires_migration" } : {}),
|
|
19
47
|
});
|
|
20
|
-
if (memoryPrune)
|
|
21
|
-
throw new Error("local_memory_v2_requires_migration");
|
|
48
|
+
if (memoryPrune && !legacyFallback && !allowPendingOnly) {
|
|
49
|
+
throw new Error(failure === "inspection_failed" ? "local_memory_inspection_failed" : "local_memory_v2_requires_migration");
|
|
50
|
+
}
|
|
22
51
|
}
|
|
23
52
|
/** 只核对小型索引和其引用;不扫描全部历史任务、不迁移或生成新存储。 */
|
|
24
53
|
export async function resolveLocalMemoryV2Rollout(input) {
|
|
25
54
|
const environment = localMemoryV2Environment(input.serverUrl);
|
|
26
|
-
if (input.selfTraining
|
|
55
|
+
if (input.selfTraining)
|
|
56
|
+
return { enabled: false, deferred: false };
|
|
57
|
+
// 已提交的目标格式独立于新的迁移资格;关闭名单不能退回不兼容writer。
|
|
58
|
+
if (input.workspace.memory.includes("NOWWORK:MEMORY_INDEX:BEGIN schema=2")) {
|
|
59
|
+
try {
|
|
60
|
+
if (detectLocalMemoryStorageVersion(input.workspace.memory) !== 2)
|
|
61
|
+
throw new Error("invalid_storage");
|
|
62
|
+
parseFullMemoryIndex(input.workspace.memory);
|
|
63
|
+
const root = await realpath(input.workspace.dir);
|
|
64
|
+
const record = await readMemoryInventorySource(root, ".crew/memory/migration.v1.json", inventoryLimits.fileBytes);
|
|
65
|
+
const state = MigrationStateSchema.parse(JSON.parse(record.content));
|
|
66
|
+
assertMigrationStateIntegrity(state, state.identity);
|
|
67
|
+
if (state.phase !== "committed" || state.storageVersion !== 2
|
|
68
|
+
|| state.identity.agentId !== (input.agentId ?? input.handle)
|
|
69
|
+
|| state.identity.rootFingerprint !== createHash("sha256").update(root).digest("hex")
|
|
70
|
+
|| state.identity.ownerIdentity !== String((await lstat(root)).uid))
|
|
71
|
+
throw new Error("invalid_state");
|
|
72
|
+
return { enabled: true, deferred: false };
|
|
73
|
+
}
|
|
74
|
+
catch (error) {
|
|
75
|
+
return { enabled: false, deferred: true, failure: localMemoryRolloutFailure(error) };
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
if (environment === "production")
|
|
27
79
|
return { enabled: false, deferred: false };
|
|
28
80
|
if (environment === "custom")
|
|
29
81
|
return { enabled: input.requested, deferred: false };
|
|
30
82
|
const deferred = { enabled: false, deferred: true };
|
|
31
83
|
const { workspace } = input;
|
|
32
|
-
if (Array.from(workspace.memory).length > LOCAL_MEMORY_TOTAL_CODE_POINT_CAP)
|
|
33
|
-
return deferred;
|
|
34
84
|
try {
|
|
35
85
|
const root = await realpath(workspace.dir);
|
|
36
86
|
const notesStat = await lstat(join(root, "notes"));
|
|
@@ -42,8 +92,26 @@ export async function resolveLocalMemoryV2Rollout(input) {
|
|
|
42
92
|
// 完整种子且无旧明细才可按空记忆接入;marker 不能替代这项核对。
|
|
43
93
|
const seededRole = /^## Role\n([\s\S]*?)\n\n## Core Goals\n/mu.exec(memory.content)?.[1];
|
|
44
94
|
if (seededRole !== undefined && memory.content === memorySeed(input.handle, seededRole)) {
|
|
45
|
-
|
|
95
|
+
if (Array.from(memory.content).length <= LOCAL_MEMORY_TOTAL_CODE_POINT_CAP
|
|
96
|
+
&& (await readdir(join(root, "notes"))).length === 0)
|
|
97
|
+
return { enabled: true, deferred: false };
|
|
46
98
|
}
|
|
99
|
+
if (!/NOWWORK:MEMORY_INDEX:/u.test(memory.content)) {
|
|
100
|
+
// 普通旧格式不是损坏V2。引用必须仍为根内可读文件,格式/容量例外保留原流程。
|
|
101
|
+
const entries = parseLocalMemoryIndex(memory.content);
|
|
102
|
+
// 已有受控ID或锚点的记录,不能因HTML标记丢失就退回旧writer。
|
|
103
|
+
if (entries.some(entry => !entry.id.startsWith("legacy:") || entry.anchor !== null))
|
|
104
|
+
return deferred;
|
|
105
|
+
let remaining = inventoryLimits.totalBytes - memory.bytes;
|
|
106
|
+
for (const file of new Set(entries.map(entry => entry.file))) {
|
|
107
|
+
if (!file.startsWith("notes/"))
|
|
108
|
+
return deferred;
|
|
109
|
+
remaining -= (await readMemoryInventorySource(root, file, Math.min(remaining, inventoryLimits.fileBytes))).bytes;
|
|
110
|
+
}
|
|
111
|
+
return { enabled: false, deferred: true, legacyFallback: true };
|
|
112
|
+
}
|
|
113
|
+
if (Array.from(workspace.memory).length > LOCAL_MEMORY_TOTAL_CODE_POINT_CAP)
|
|
114
|
+
return deferred;
|
|
47
115
|
const generated = /<!-- NOWWORK:MEMORY_INDEX:BEGIN schema=1 -->[\s\S]*?<!-- NOWWORK:MEMORY_INDEX:END -->/u.exec(memory.content)?.[0];
|
|
48
116
|
if (!generated || Array.from(generated).length > LOCAL_MEMORY_INDEX_CODE_POINT_CAP)
|
|
49
117
|
return deferred;
|
|
@@ -76,7 +144,7 @@ export async function resolveLocalMemoryV2Rollout(input) {
|
|
|
76
144
|
}
|
|
77
145
|
return { enabled: true, deferred: false };
|
|
78
146
|
}
|
|
79
|
-
catch {
|
|
80
|
-
return deferred;
|
|
147
|
+
catch (error) {
|
|
148
|
+
return { ...deferred, failure: localMemoryRolloutFailure(error) };
|
|
81
149
|
}
|
|
82
150
|
}
|