@nowcrew/daemon 0.6.93-beta.2 → 0.6.93-beta.4

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.
Files changed (46) hide show
  1. package/dist/cloud-report-upload.js +10 -1
  2. package/dist/execution-journal.js +1 -3
  3. package/dist/execution-protocol.js +1 -1
  4. package/dist/execution-runner.js +1 -3
  5. package/dist/local-execution-prompt.js +3 -8
  6. package/dist/local-executor.js +35 -64
  7. package/dist/local-memory/index-budget.js +3 -9
  8. package/dist/local-memory/inventory-cli.js +1 -23
  9. package/dist/local-memory/inventory-source.js +8 -15
  10. package/dist/local-memory/prompt.js +4 -20
  11. package/dist/local-memory/read-cli.js +77 -75
  12. package/dist/local-memory/rollout.js +8 -54
  13. package/dist/local-memory/runtime.js +13 -148
  14. package/dist/local-memory/write.js +19 -125
  15. package/dist/local-memory-diagnostics.js +2 -41
  16. package/dist/local-memory-telemetry.js +2 -19
  17. package/dist/machine-info.js +0 -3
  18. package/dist/marketplace-protocol.js +1 -1
  19. package/dist/marketplace-remote.js +1 -1
  20. package/dist/marketplace-runner.js +1 -1
  21. package/dist/memory-prune-diagnostics.js +4 -60
  22. package/dist/message-file-preview.js +1 -11
  23. package/dist/prompt.js +1 -5
  24. package/dist/runner.js +1 -1
  25. package/dist/serve.js +3 -36
  26. package/dist/token.js +0 -1
  27. package/dist/workspace-control-handler.js +3 -16
  28. package/dist/workspace.js +2 -10
  29. package/package.json +1 -1
  30. package/dist/local-memory/execution-workspace.js +0 -55
  31. package/dist/local-memory/full-index-search.js +0 -47
  32. package/dist/local-memory/initialization-identity.js +0 -23
  33. package/dist/local-memory/legacy-adapter.js +0 -233
  34. package/dist/local-memory/migration-access.js +0 -184
  35. package/dist/local-memory/migration-cli.js +0 -54
  36. package/dist/local-memory/migration-io.js +0 -199
  37. package/dist/local-memory/migration-plan.js +0 -283
  38. package/dist/local-memory/migration-schema.js +0 -55
  39. package/dist/local-memory/migration-store.js +0 -429
  40. package/dist/local-memory/migration-tool-entry.js +0 -21
  41. package/dist/local-memory/storage-format.js +0 -152
  42. package/dist/memory-migration-controller.js +0 -84
  43. package/dist/memory-migration-inspect.js +0 -75
  44. package/dist/memory-migration-journal.js +0 -14
  45. package/dist/memory-migration-protocol.js +0 -14
  46. package/dist/memory-migration-runtime.js +0 -305
@@ -6,15 +6,7 @@ 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
9
  const OUTPUT_BYTES = 12 * 1024;
14
- // A V2 page may carry coverage metadata for legacy sources. Reserve room for
15
- // that metadata so a mixed result never exceeds the command's output limit.
16
- const FULL_INDEX_RESULT_BYTES = 4 * 1024;
17
- const FULL_INDEX_LEGACY_SCAN_BYTES = OUTPUT_BYTES - 4 * 1024;
18
10
  const FILE_BYTES = 32 * 1024 * 1024;
19
11
  const READ_LIMIT_MAX = 4_000;
20
12
  const READ_LIMIT_SAFE = 2_000;
@@ -35,19 +27,11 @@ const Common = {
35
27
  ifHash: z.string().regex(/^[a-f0-9]{64}$/u).optional(),
36
28
  };
37
29
  const CommandSchema = z.discriminatedUnion("command", [
38
- z.object({
39
- command: z.literal("search-history"), ...Common,
40
- query: z.string().trim().min(1).max(256),
41
- limit: z.coerce.number().int().min(1).max(Number.MAX_SAFE_INTEGER).default(10),
42
- afterLine: z.coerce.number().int().min(0).max(Number.MAX_SAFE_INTEGER).default(0),
43
- cursor: z.string().min(1).max(4096).optional(),
44
- }).strict(),
45
30
  z.object({
46
31
  command: z.literal("search"), ...Common,
47
32
  query: z.string().trim().min(1).max(256),
48
33
  limit: z.coerce.number().int().min(1).max(Number.MAX_SAFE_INTEGER).default(10),
49
34
  afterLine: z.coerce.number().int().min(0).max(Number.MAX_SAFE_INTEGER).default(0),
50
- cursor: z.string().min(1).max(4096).optional(),
51
35
  }).strict(),
52
36
  z.object({
53
37
  command: z.literal("read"), ...Common,
@@ -69,8 +53,7 @@ const CommandSchema = z.discriminatedUnion("command", [
69
53
  }).strict(),
70
54
  ]);
71
55
  const USAGE = `只读记忆工具,默认输出 JSON,不修改任何记忆文件。
72
- crew-memory search --query <关键词> [--file MEMORY.md|notes/文件.md] [--limit 10] [--after-line 0] [--cursor <游标>]
73
- crew-memory search-history --query <关键词> [--limit 10] [--after-line 0] [--cursor <游标>] [--if-hash <sha256>]
56
+ crew-memory search --query <关键词> [--file MEMORY.md|notes/文件.md] [--limit 10] [--after-line 0]
74
57
  crew-memory read [--file MEMORY.md|notes/文件.md] [--offset 0] [--limit 2000]
75
58
  crew-memory search-pending --query <关键词> [--cursor <游标>] [--limit 10]
76
59
  crew-memory read-pending --id <候选ID> [--offset 0] [--limit 2000] [--if-hash <sha256>]
@@ -79,8 +62,7 @@ search 关键词按空白分隔,同一行须全部命中;返回完整文件
79
62
  读取的 offset/limit 按 Unicode 字符计数;用 nextOffset 继续,eof=true 表示结束。
80
63
  读取的 --limit 超出范围时,工具会自动降到 2000 并在结果中标记 warning;保留原文件或候选 ID 及 offset,按 nextOffset 分页,不要一次读完整文件。
81
64
  后续页可传 --if-hash <前一页的 sha256>,文件变化时停止,避免混读不同版本。
82
- schema=2 的 search 查询完整有效目录(active/pinned),按标题、摘要、触发词联合匹配并返回 ID、revision 和正文引用;search-history 显式查询历史/失效记录。预览未展示不代表记录不存在。
83
- 旧格式 search/search-history 及 read 读取原文;search-history 是显式 legacy fallback,不把旧内容当作已结构化 V2 记录;read 按指定文件和哈希分页,不扫描 pending。
65
+ search/read 读取 MEMORY.md 和 notes 原文,不过滤失效段落,也不扫描 pending。
84
66
  search-pending 只查因容量延期的 create 候选;有 nextCursor 就继续,即使本页 matches 为空。
85
67
  complete=true 才表示全部候选已检索完成且无遗漏;warnings 或非零退出码表示读取未完成,不能当作零命中。
86
68
  read-pending 只返回候选正文;offset 非零时必须传 --if-hash,update/supersede 候选不可读取。
@@ -125,6 +107,34 @@ async function readSource(home, file) {
125
107
  await handle.close();
126
108
  }
127
109
  }
110
+ function characterCount(text) {
111
+ let count = 0;
112
+ for (const _character of text)
113
+ count += 1;
114
+ return count;
115
+ }
116
+ function excerpt(text, limit) {
117
+ let value = "";
118
+ let count = 0;
119
+ for (const character of text) {
120
+ if (count++ >= limit)
121
+ return `${value}...`;
122
+ value += character;
123
+ }
124
+ return value;
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;
135
+ }
136
+ return text.length;
137
+ }
128
138
  function normalizeLimit(requestedLimit, max, safe) {
129
139
  if (requestedLimit <= max)
130
140
  return { limit: requestedLimit };
@@ -133,6 +143,50 @@ function normalizeLimit(requestedLimit, max, safe) {
133
143
  warning: { code: "memory_limit_clamped", requestedLimit, appliedLimit: safe, recovered: true },
134
144
  };
135
145
  }
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
+ }
136
190
  function readPage(text, source, command, warning) {
137
191
  const metadata = {
138
192
  ...source,
@@ -178,18 +232,6 @@ function writeResult(result, io) {
178
232
  io.out(output);
179
233
  }
180
234
  export async function runMemoryReadCommand(argv, io) {
181
- if (!io.env.CREW_HOME || argv.includes("--help") || argv.includes("-h"))
182
- return runMemoryReadUnlocked(argv, io);
183
- try {
184
- return await withMemoryAccess(io.env.CREW_HOME, () => runMemoryReadUnlocked(argv, io));
185
- }
186
- catch (error) {
187
- const reason = error instanceof Error && /^(?:local_)?memory_[a-z_]+$/u.test(error.message) ? error.message : "memory_migration_access_failed";
188
- io.err(JSON.stringify({ error: reason, message: "记忆读取受到迁移或状态保护,不能当作没有记忆。" }));
189
- return 1;
190
- }
191
- }
192
- async function runMemoryReadUnlocked(argv, io) {
193
235
  let command;
194
236
  try {
195
237
  const { values, positionals } = parseArgs({
@@ -222,7 +264,6 @@ async function runMemoryReadUnlocked(argv, io) {
222
264
  io.err(`memory_arguments_invalid: 参数不合法,记忆读取未完成;请按以下参数范围修正后重试,不要当作没有记忆。\n${USAGE}`);
223
265
  return 2;
224
266
  }
225
- const fileWasExplicit = argv.some((arg) => arg === "--file" || arg.startsWith("--file="));
226
267
  try {
227
268
  if (!io.env.CREW_HOME)
228
269
  throw new Error("memory_home_missing");
@@ -248,49 +289,10 @@ async function runMemoryReadUnlocked(argv, io) {
248
289
  const source = await readSource(io.env.CREW_HOME, command.file);
249
290
  if (command.ifHash !== undefined && command.ifHash !== source.sha256)
250
291
  throw new Error("memory_file_changed");
251
- const fullIndex = command.file === "MEMORY.md" && detectLocalMemoryStorageVersion(source.text) === 2;
252
- if ((command.command === "search" || command.command === "search-history") && fullIndex) {
253
- const normalized = normalizeLimit(command.limit, SEARCH_LIMIT_MAX, SEARCH_LIMIT_SAFE);
254
- const result = searchFullMemoryIndex(source.text, source.sha256, {
255
- ...command, limit: normalized.limit, history: command.command === "search-history",
256
- }, FULL_INDEX_RESULT_BYTES);
257
- const indexedFiles = new Set(["MEMORY.md", ...parseFullMemoryIndex(source.text).map((match) => match.file)]);
258
- const legacy = await searchLegacyMemorySources({ agentDir: io.env.CREW_HOME, query: command.query,
259
- limit: normalized.limit, outputBytes: FULL_INDEX_LEGACY_SCAN_BYTES,
260
- ...(command.cursor === undefined ? {} : { cursor: command.cursor }),
261
- ...(fileWasExplicit ? { files: [command.file], excludeFiles: indexedFiles } : { excludeFiles: indexedFiles }) });
262
- if (result.matches.length === 0 && (command.cursor === undefined || legacy.matchCount > 0)) {
263
- if (legacy.unindexedFiles.length > 0 && (legacy.matchCount > 0 || result.matchCount === 0)) {
264
- writeResult({ ...legacy, ...(normalized.warning ? { warning: normalized.warning,
265
- requestedLimit: normalized.warning.requestedLimit, limit: normalized.warning.appliedLimit } : {}) }, io);
266
- return 0;
267
- }
268
- }
269
- writeResult({ source: "v2", legacy: false, ...result,
270
- unindexedFiles: legacy.unindexedFiles,
271
- unindexedFileCount: legacy.unindexedFileCount,
272
- unindexedFilesTruncated: legacy.unindexedFilesTruncated,
273
- complete: result.complete && legacy.matchCount === 0 && legacy.complete,
274
- ...(legacy.matchCount === 0 ? {} : { unindexedMatchCount: legacy.matchCount }),
275
- ...(legacy.matchCount === 0 ? {} : {
276
- legacyHasMore: legacy.hasMore,
277
- legacyNextCursor: legacy.nextCursor,
278
- legacyNextAfterLine: legacy.nextAfterLine,
279
- v2AfterLine: result.matches.at(-1)?.line ?? result.nextAfterLine,
280
- }),
281
- ...(normalized.warning ? { warning: normalized.warning,
282
- requestedLimit: normalized.warning.requestedLimit, limit: normalized.warning.appliedLimit } : {}) }, io);
283
- return 0;
284
- }
285
- const result = command.command === "search" || command.command === "search-history"
286
- ? await (async () => {
292
+ const result = command.command === "search"
293
+ ? (() => {
287
294
  const normalized = normalizeLimit(command.limit, SEARCH_LIMIT_MAX, SEARCH_LIMIT_SAFE);
288
- const legacy = await searchLegacyMemorySources({ agentDir: io.env.CREW_HOME, query: command.query,
289
- limit: normalized.limit, outputBytes: OUTPUT_BYTES - 256, afterLine: command.afterLine,
290
- ...(command.cursor === undefined ? {} : { cursor: command.cursor }),
291
- ...(fileWasExplicit ? { files: [command.file] } : {}) });
292
- return { ...legacy, ...(normalized.warning ? { warning: normalized.warning,
293
- requestedLimit: normalized.warning.requestedLimit, limit: normalized.warning.appliedLimit } : {}) };
295
+ return searchSource(source.text, source.sha256, { ...command, limit: normalized.limit }, normalized.warning);
294
296
  })()
295
297
  : (() => {
296
298
  const normalized = normalizeLimit(command.limit, READ_LIMIT_MAX, READ_LIMIT_SAFE);
@@ -7,58 +7,30 @@ 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
10
  export const LOCAL_MEMORY_V2_DEFERRED_GUIDANCE = `
14
11
 
15
12
  ## 本轮本地记忆兼容状态
16
13
  开发环境已开放 V2,但该 Agent 的旧记忆尚未通过格式兼容检查,本轮未启用结构化 V2。
17
14
  本节覆盖前文和旧会话中直接改写共享记忆的要求:本轮 MEMORY.md 与 notes 只读,不得改写、迁移或清空。
18
15
  用户要求新增或更新长期规则时,内容和授权均只记录到本任务的 $CREW_TASK_LOG;保留日志,不要清空,不要宣称长期记忆已生效。旧资料兼容处理完成前,记忆收尾会明确报告需要处理。`;
19
- export const LOCAL_MEMORY_LEGACY_GUIDANCE = `
20
-
21
- ## 本轮本地记忆兼容状态
22
- 本 Agent 尚未完成 V2 格式转换,本轮继续原记忆流程。用户明确要求新增或修改长期规则时,可按原规则最小范围更新共享记忆;记忆收尾继续依据任务日志更新原 MEMORY.md 和 notes,确认成功后再清理日志。
23
- 本节覆盖旧会话中因等待 V2 转换而冻结共享记忆写入的要求,但不是新的用户写入授权;本任务如明确要求不更新记忆,继续遵守。
24
- 保留原身份、目标及无关资料,不自行迁移或重置文件,不宣称已启用 V2。`;
25
- export function reportLocalMemoryV2Deferred(executionId, handle, memoryPrune, legacyFallback = false) {
26
- dslog("local_memory.v2_deferred", legacyFallback ? "旧格式继续原记忆流程,转换成功后再启用V2" : "记忆格式异常,保留原资料和待处理日志", {
27
- execution_id: executionId, agent_handle: handle, reason: legacyFallback ? "legacy_compatible" : "requires_migration"
16
+ export function reportLocalMemoryV2Deferred(executionId, handle, memoryPrune) {
17
+ dslog("local_memory.v2_deferred", "旧记忆尚未通过兼容检查,保留原资料和待处理日志", {
18
+ execution_id: executionId, agent_handle: handle, reason: "requires_migration"
28
19
  });
29
- if (memoryPrune && !legacyFallback)
20
+ if (memoryPrune)
30
21
  throw new Error("local_memory_v2_requires_migration");
31
22
  }
32
23
  /** 只核对小型索引和其引用;不扫描全部历史任务、不迁移或生成新存储。 */
33
24
  export async function resolveLocalMemoryV2Rollout(input) {
34
25
  const environment = localMemoryV2Environment(input.serverUrl);
35
- if (input.selfTraining)
36
- return { enabled: false, deferred: false };
37
- // 已提交的目标格式独立于新的迁移资格;关闭名单不能退回不兼容writer。
38
- if (input.workspace.memory.includes("NOWWORK:MEMORY_INDEX:BEGIN schema=2")) {
39
- try {
40
- if (detectLocalMemoryStorageVersion(input.workspace.memory) !== 2)
41
- throw new Error("invalid_storage");
42
- parseFullMemoryIndex(input.workspace.memory);
43
- const root = await realpath(input.workspace.dir);
44
- const record = await readMemoryInventorySource(root, ".crew/memory/migration.v1.json", inventoryLimits.fileBytes);
45
- const state = MigrationStateSchema.parse(JSON.parse(record.content));
46
- if (state.phase !== "committed" || state.storageVersion !== 2
47
- || state.identity.agentId !== (input.agentId ?? input.handle)
48
- || state.identity.rootFingerprint !== createHash("sha256").update(root).digest("hex"))
49
- throw new Error("invalid_state");
50
- return { enabled: true, deferred: false };
51
- }
52
- catch {
53
- return { enabled: false, deferred: true };
54
- }
55
- }
56
- if (environment === "production")
26
+ if (input.selfTraining || environment === "production")
57
27
  return { enabled: false, deferred: false };
58
28
  if (environment === "custom")
59
29
  return { enabled: input.requested, deferred: false };
60
30
  const deferred = { enabled: false, deferred: true };
61
31
  const { workspace } = input;
32
+ if (Array.from(workspace.memory).length > LOCAL_MEMORY_TOTAL_CODE_POINT_CAP)
33
+ return deferred;
62
34
  try {
63
35
  const root = await realpath(workspace.dir);
64
36
  const notesStat = await lstat(join(root, "notes"));
@@ -70,26 +42,8 @@ export async function resolveLocalMemoryV2Rollout(input) {
70
42
  // 完整种子且无旧明细才可按空记忆接入;marker 不能替代这项核对。
71
43
  const seededRole = /^## Role\n([\s\S]*?)\n\n## Core Goals\n/mu.exec(memory.content)?.[1];
72
44
  if (seededRole !== undefined && memory.content === memorySeed(input.handle, seededRole)) {
73
- if (Array.from(memory.content).length <= LOCAL_MEMORY_TOTAL_CODE_POINT_CAP
74
- && (await readdir(join(root, "notes"))).length === 0)
75
- return { enabled: true, deferred: false };
45
+ return (await readdir(join(root, "notes"))).length === 0 ? { enabled: true, deferred: false } : deferred;
76
46
  }
77
- if (!/NOWWORK:MEMORY_INDEX:/u.test(memory.content)) {
78
- // 普通旧格式不是损坏V2。引用必须仍为根内可读文件,格式/容量例外保留原流程。
79
- const entries = parseLocalMemoryIndex(memory.content);
80
- // 已有受控ID或锚点的记录,不能因HTML标记丢失就退回旧writer。
81
- if (entries.some(entry => !entry.id.startsWith("legacy:") || entry.anchor !== null))
82
- return deferred;
83
- let remaining = inventoryLimits.totalBytes - memory.bytes;
84
- for (const file of new Set(entries.map(entry => entry.file))) {
85
- if (!file.startsWith("notes/"))
86
- return deferred;
87
- remaining -= (await readMemoryInventorySource(root, file, Math.min(remaining, inventoryLimits.fileBytes))).bytes;
88
- }
89
- return { enabled: false, deferred: true, legacyFallback: true };
90
- }
91
- if (Array.from(workspace.memory).length > LOCAL_MEMORY_TOTAL_CODE_POINT_CAP)
92
- return deferred;
93
47
  const generated = /<!-- NOWWORK:MEMORY_INDEX:BEGIN schema=1 -->[\s\S]*?<!-- NOWWORK:MEMORY_INDEX:END -->/u.exec(memory.content)?.[0];
94
48
  if (!generated || Array.from(generated).length > LOCAL_MEMORY_INDEX_CODE_POINT_CAP)
95
49
  return deferred;
@@ -2,12 +2,6 @@ import { createHash } from "node:crypto";
2
2
  import { constants } from "node:fs";
3
3
  import { lstat, mkdir, open, readFile, rm, writeFile } from "node:fs/promises";
4
4
  import { join } from "node:path";
5
- import { applyMemoryMigration, prepareMemoryMigration, recoverMemoryMigration } from "./migration-store.js";
6
- import { MigrationStateSchema } from "./migration-schema.js";
7
- import { detectLocalMemoryStorageVersion } from "./storage-format.js";
8
- import { isLocalMemoryInitializationIdentity, localMemoryInitializationIdentity } from "./initialization-identity.js";
9
- import { inventory } from "./migration-io.js";
10
- import { initializeExactWorkspaceSeed } from "./migration-plan.js";
11
5
  import { applyLocalMemoryResult, parseLocalMemoryResult } from "./write.js";
12
6
  import { verifyWorkLogSource } from "./work-log.js";
13
7
  export function isLocalMemoryV2Eligible(input) {
@@ -18,7 +12,7 @@ export function isLocalMemoryV2Eligible(input) {
18
12
  return input.enabledForHandle && input.memoryPrune;
19
13
  }
20
14
  const LOCAL_MEMORY_V2_MARKER = "v2-enabled";
21
- async function writeLocalMemoryV2Marker(agentDir) {
15
+ export async function initializeLocalMemoryV2(agentDir) {
22
16
  const crewDir = join(agentDir, ".crew");
23
17
  const memoryDir = join(crewDir, "memory");
24
18
  await mkdir(crewDir, { recursive: true });
@@ -37,122 +31,24 @@ async function writeLocalMemoryV2Marker(agentDir) {
37
31
  throw error;
38
32
  }
39
33
  }
40
- function initializedIntent(identity, state) {
41
- return {
42
- ...identity,
43
- kind: "server_write_intent",
44
- sourceDigest: state.sourceDigest,
45
- planDigest: state.planDigest,
46
- intentVersion: 1,
47
- // New-Agent initialization is derived from the exact pristine seed. Keep
48
- // the receipt deterministic so a crash between intent publication and the
49
- // mutable state update can be retried without creating a conflicting fact.
50
- confirmedAt: "1970-01-01T00:00:00.000Z",
51
- };
52
- }
53
- /**
54
- * Initialize a pristine new Agent through the same journaled schema2
55
- * publication path used by migration. The marker is written only after the
56
- * committed catalog and state have both been verified.
57
- */
58
- export async function initializeLocalMemoryV2(agentDir, agentId) {
59
- const identity = await localMemoryInitializationIdentity(agentDir, agentId);
60
- let state = await prepareMemoryMigration({
61
- agentDir,
62
- identity,
63
- serverHistory: { kind: "none", isNewAgent: true },
64
- });
65
- if (state.origin !== "initialized")
66
- throw new Error("local_memory_v2_initialization_source_invalid");
67
- if (state.phase === "prepared") {
68
- state = await applyMemoryMigration({
69
- agentDir,
70
- identity,
71
- intentReceipt: initializedIntent(identity, state),
72
- });
73
- }
74
- else if (state.phase === "applying" || state.phase === "repair_required") {
75
- state = await recoverMemoryMigration({
76
- agentDir,
77
- identity,
78
- intentReceipt: state.intentReceipt ?? initializedIntent(identity, state),
79
- stoppedReceipt: {
80
- kind: "migration_process_stopped",
81
- migrationId: identity.migrationId,
82
- machineId: identity.machineId,
83
- executionId: identity.executionId,
84
- ownerIdentity: identity.ownerIdentity,
85
- pid: null,
86
- stoppedAt: new Date().toISOString(),
87
- proof: "journal_reconciled",
88
- },
89
- cancelled: false,
90
- });
91
- }
92
- const memory = await readFile(join(agentDir, "MEMORY.md"), "utf8");
93
- if (state.phase !== "committed" || state.origin !== "initialized" || state.storageVersion !== 2
94
- || detectLocalMemoryStorageVersion(memory) !== 2) {
95
- throw new Error("local_memory_v2_initialization_incomplete");
96
- }
97
- await writeLocalMemoryV2Marker(agentDir);
98
- }
99
- export async function initializeLocalMemoryV2ForWorkspace(workspace, enabledForHandle, agentId) {
34
+ export async function initializeLocalMemoryV2ForWorkspace(workspace, enabledForHandle) {
100
35
  const initialized = workspace.localMemoryV2Initialized === true;
101
- if (initialized)
102
- return true;
103
- // Real workspaces always report this boolean. Keep partial/mock workspace
104
- // callers side-effect free instead of probing an unrelated filesystem path.
105
- if (workspace.localMemoryV2Initialized !== false)
106
- return false;
107
- let resumable = false;
36
+ if (!enabledForHandle || initialized || workspace.localMemoryV2Initialized !== false
37
+ || workspace.agentHomePreexisted === true || workspace.memorySeedCreated !== true)
38
+ return initialized;
108
39
  try {
109
- const state = MigrationStateSchema.parse(JSON.parse(await readFile(join(workspace.dir, ".crew", "memory", "migration.v1.json"), "utf8")));
110
- const identity = await localMemoryInitializationIdentity(workspace.dir, agentId);
111
- resumable = state.origin === "initialized"
112
- && state.phase !== "rolled_back"
113
- && isLocalMemoryInitializationIdentity(state.identity, identity);
114
- }
115
- catch (error) {
116
- if (error.code !== "ENOENT")
117
- throw error;
118
- try {
119
- const migrations = await lstat(join(workspace.dir, ".crew", "memory", "migrations"));
120
- if (!migrations.isDirectory() || migrations.isSymbolicLink()) {
121
- throw new Error("local_memory_v2_initialization_residue_invalid");
122
- }
123
- const identity = await localMemoryInitializationIdentity(workspace.dir, agentId);
124
- const operation = await lstat(join(workspace.dir, ".crew", "memory", "migrations", identity.migrationId));
125
- if (!operation.isDirectory() || operation.isSymbolicLink()) {
126
- throw new Error("local_memory_v2_initialization_residue_invalid");
127
- }
128
- const current = await inventory(workspace.dir);
129
- const memory = current.files.get("MEMORY.md");
130
- resumable = current.files.size === 1 && memory !== undefined
131
- && current.directories.every(path => path === "notes" || path === ".crew/memory/pending")
132
- && initializeExactWorkspaceSeed(memory) !== null;
133
- }
134
- catch (residueError) {
135
- if (residueError.code !== "ENOENT")
136
- throw residueError;
137
- }
40
+ await initializeLocalMemoryV2(workspace.dir);
41
+ return true;
138
42
  }
139
- const pristineFirstInitialization = enabledForHandle
140
- && workspace.localMemoryV2Initialized === false
141
- && workspace.agentHomePreexisted !== true
142
- && workspace.memorySeedCreated === true;
143
- if (!resumable && !pristineFirstInitialization)
43
+ catch {
144
44
  return false;
145
- await initializeLocalMemoryV2(workspace.dir, agentId);
146
- return true;
45
+ }
147
46
  }
148
47
  export function localMemoryResultPath(runDir, executionId) {
149
48
  const suffix = createHash("sha256").update(executionId, "utf8").digest("hex").slice(0, 32);
150
49
  return join(runDir, `.memory-result.v1-${suffix}.json`);
151
50
  }
152
- export function renderLocalMemoryResultInstructions(resultPath, storageVersion = 1) {
153
- const storagePolicy = storageVersion === 2
154
- ? "完整目录保存在 MEMORY.md,模型只看到有界预览;未展示记录仍然有效。涉及旧规则时先用 crew-memory search 查询完整目录,按当前 ID/revision 更新,不因预览未命中就重复新建。20条预览不是有效记录上限;完整存储超出资源上限会拒绝提交,不得自行删除或降级旧记录。已有 pending 仍是未应用候选,须重新核对授权、重复与版本后通过 changes 申请保存,不因迁移自动生效。"
155
- : "daemon 会校验、去重、限制 MEMORY.md 大小,超出索引容量的候选会保存在 pending 归档中。容量暂存的 create 可按需检索参考但不能覆盖现行规则;有空间的后续 proposed 提交可提升。未应用的 update/supersede 不进入普通召回,必须重新核对目标版本后处理。";
51
+ export function renderLocalMemoryResultInstructions(resultPath) {
156
52
  return `
157
53
 
158
54
  ## Structured memory prune (daemon-managed)
@@ -174,7 +70,7 @@ outcome 只允许 no_change、proposed、conflict、failed。新增、更新、
174
70
  \`\`\`
175
71
 
176
72
  每个 changes 项须保留示例中的全部字段。operation=create|update|supersede;kind=procedural|semantic|episodic|preference;priority=high|medium|low;detailCategory=preferences|project-rules|testing|debugging|git|incidents|lessons。create 的 memoryId/baseRevision/supersedesMemoryId 必须为 null;update 按现有索引填写 memoryId/baseRevision,supersedesMemoryId 为 null;supersede 填 supersedesMemoryId/baseRevision,memoryId 为 null。
177
- 只提交可复用、与本次任务明确相关的少量候选;不要写入凭证、token、隐私或内部控制标记。${storagePolicy}
73
+ 只提交可复用、与本次任务明确相关的少量候选;不要写入凭证、token、隐私或内部控制标记。daemon 会校验、去重、限制 MEMORY.md 大小,超出索引容量的候选会保存在 pending 归档中。容量暂存的 create 可按需检索参考但不能覆盖现行规则;有空间的后续 proposed 提交可提升。未应用的 update/supersede 不进入普通召回,必须重新核对目标版本后处理。
178
74
  写入候选文件只表示提交候选,不能提前宣称已经记住或规则已经更新;daemon 在本轮结束后才处理,候选持久化与进入有效索引也不是同一结果。本结构化轮次覆盖前文提前清理工作日志的要求:保留 $CREW_TASK_LOG,不要清空或删除;daemon 将在处理成功后清理,失败时保留日志和候选用于排查或重试。
179
75
  `;
180
76
  }
@@ -185,20 +81,6 @@ export function renderLocalMemoryV2WriteGuard() {
185
81
  ## V2 shared memory write safety
186
82
  本节覆盖前文关于普通任务直接更新共享记忆的例外规则。本 Agent 已启用结构化本地记忆 V2。普通任务不得直接编辑共享的 $CREW_HOME/MEMORY.md 或 $CREW_HOME/notes/ 文件;即使用户明确要求记住内容,也只把候选、用户授权和分类理由记录到本任务的 $CREW_TASK_LOG,留给任务结束后的 memory-prune 结构化收尾应用。只有 memory-prune 收尾提示明确要求写入 daemon 提供的 result 文件时,才按该协议提交候选。`;
187
83
  }
188
- async function localMemoryStorageFacts(agentDir) {
189
- const memory = await readFile(join(agentDir, "MEMORY.md"), "utf8");
190
- const storageVersion = detectLocalMemoryStorageVersion(memory);
191
- let committedState = false;
192
- try {
193
- const state = MigrationStateSchema.parse(JSON.parse(await readFile(join(agentDir, ".crew", "memory", "migration.v1.json"), "utf8")));
194
- committedState = state.phase === "committed" && state.storageVersion === 2;
195
- }
196
- catch (error) {
197
- if (error.code !== "ENOENT" && storageVersion === 2)
198
- throw error;
199
- }
200
- return { storageVersion, committedState };
201
- }
202
84
  function workLogFailure(error) {
203
85
  const reason = error instanceof Error ? error.message : "";
204
86
  return { status: "failed", error: reason === "local_memory_work_log_source_changed"
@@ -216,9 +98,6 @@ export async function commitLocalMemoryResultAfterExit(input) {
216
98
  return { status: "missing" };
217
99
  return { status: "failed", error: error instanceof Error ? error.message : "read_failed" };
218
100
  }
219
- let storageBefore;
220
- let storageAfter;
221
- let applied;
222
101
  try {
223
102
  const result = parseLocalMemoryResult(raw);
224
103
  if (result.outcome === "conflict" || result.outcome === "failed") {
@@ -232,23 +111,12 @@ export async function commitLocalMemoryResultAfterExit(input) {
232
111
  return workLogFailure(error);
233
112
  }
234
113
  }
235
- storageBefore = await localMemoryStorageFacts(input.agentDir);
236
- if (input.requiredStorageVersion === 2
237
- && (storageBefore.storageVersion !== 2 || !storageBefore.committedState)) {
238
- return { status: "failed", error: "local_memory_v2_state_invalid_before_commit" };
239
- }
240
- applied = await applyLocalMemoryResult({
114
+ await applyLocalMemoryResult({
241
115
  agentDir: input.agentDir,
242
116
  result,
243
117
  executionId: input.executionId,
244
118
  pruneTraceId: input.pruneTraceId,
245
- ...(input.writerCapabilities === undefined ? {} : { writerCapabilities: input.writerCapabilities }),
246
119
  });
247
- storageAfter = await localMemoryStorageFacts(input.agentDir);
248
- if (input.requiredStorageVersion === 2
249
- && (storageAfter.storageVersion !== 2 || !storageAfter.committedState)) {
250
- return { status: "failed", error: "local_memory_v2_state_invalid_after_commit" };
251
- }
252
120
  }
253
121
  catch (error) {
254
122
  return { status: "failed", error: error instanceof Error ? error.message : "apply_failed" };
@@ -279,8 +147,5 @@ export async function commitLocalMemoryResultAfterExit(input) {
279
147
  catch {
280
148
  return { status: "failed", error: "local_memory_result_cleanup_failed" };
281
149
  }
282
- return { status: "applied", storageVersionBefore: storageBefore.storageVersion,
283
- storageVersionAfter: storageAfter.storageVersion, committedState: storageAfter.committedState,
284
- createdIds: applied.createdIds, updatedIds: applied.updatedIds,
285
- statusChangedIds: applied.statusChangedIds, pendingIds: applied.pendingIds };
150
+ return { status: "applied" };
286
151
  }