evolcore 0.0.41-test.0 → 0.0.41-test.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +62 -0
- package/dist/agents/baseagent-availability.js +1 -1
- package/dist/agents/baseagent.js +88 -49
- package/dist/agents/claude-runner.js +177 -92
- package/dist/agents/codex-runner.js +146 -71
- package/dist/agents/ecagent-runner.js +1 -1
- package/dist/agents/gemini-runner.js +1 -1
- package/dist/aun/aid/agentmd.js +98 -11
- package/dist/aun/aid/client.js +1 -1
- package/dist/aun/aid/control-aid.js +26 -2
- package/dist/aun/aid/identity.js +91 -0
- package/dist/aun/aid/index.js +1 -1
- package/dist/aun/aid/managed-operation.js +1 -1
- package/dist/aun/aid/ownership-operation.js +23 -22
- package/dist/aun/group/fs-download.js +39 -0
- package/dist/aun/group/identity.js +36 -21
- package/dist/aun/group/operations.js +5 -0
- package/dist/aun/menu/client.js +27 -0
- package/dist/aun/msg/managed-operation.js +68 -7
- package/dist/aun/msg/p2p.js +8 -8
- package/dist/aun/service-proxy.js +53 -12
- package/dist/channels/aun.js +633 -32
- package/dist/channels/daemon.js +15 -1
- package/dist/cli/agent-command.js +5 -2
- package/dist/cli/agent-create.js +991 -233
- package/dist/cli/agent.js +49 -10
- package/dist/cli/aid-bind-command.js +48 -19
- package/dist/cli/aun-commands.js +48 -36
- package/dist/cli/daemon-commands.js +679 -697
- package/dist/cli/fs-command.js +64 -2
- package/dist/cli/index.js +72 -5
- package/dist/cli/init-channel.js +1 -1
- package/dist/cli/init.js +75 -1
- package/dist/cli/managed-command-guard.js +17 -1
- package/dist/cli/model.js +40 -40
- package/dist/cli/provider-command.js +57 -0
- package/dist/cli/proxy-command.js +92 -0
- package/dist/cli/restart-monitor.js +4 -2
- package/dist/cli/role.js +85 -0
- package/dist/cli/stats.js +10 -3
- package/dist/cli/task-context.js +19 -0
- package/dist/config/access-policy.js +7 -2
- package/dist/config/boot-log.js +39 -1
- package/dist/config/config-field-policy.js +17 -39
- package/dist/config/config-manager.js +183 -44
- package/dist/config/config-operation-service.js +5 -19
- package/dist/config/contact-book-store.js +47 -21
- package/dist/config/contact-book.js +18 -12
- package/dist/config/contact-operation-service.js +3 -4
- package/dist/config/contact-request-service.js +11 -0
- package/dist/config/daemon-services.js +11 -0
- package/dist/config/gateway-config.js +88 -594
- package/dist/config/owner-config.js +2 -2
- package/dist/config/peer-role-resolver.js +15 -12
- package/dist/config/provider-ipc-service.js +132 -0
- package/dist/config/provider-menu-request.js +58 -0
- package/dist/config/providers-config.js +12 -7
- package/dist/config/providers-import.js +3 -21
- package/dist/config/providers-management.js +78 -5
- package/dist/config/resolved-config-op.js +11 -1
- package/dist/config/role-command-service.js +166 -0
- package/dist/config/role-request-service.js +193 -0
- package/dist/config/schema-migration.js +1000 -25
- package/dist/config-store.js +89 -4
- package/dist/contracts/ecweb-instance.js +41 -0
- package/dist/contracts/ecweb-service-protocol.js +4 -1
- package/dist/core/agent-application-service.js +60 -11
- package/dist/core/agent-create-status.js +203 -0
- package/dist/core/agent-creation-claim.js +177 -0
- package/dist/core/agent-ipc-registration.js +39 -18
- package/dist/core/agent-runtime.js +194 -24
- package/dist/core/auth/agent-delegation.js +6 -3
- package/dist/core/auth/auth-gateway.js +67 -2
- package/dist/core/auth/authorization-audit.js +58 -11
- package/dist/core/auth/delegation-authorization.js +5 -2
- package/dist/core/auth/operation-authorizer.js +43 -6
- package/dist/core/auth/operation-catalog.js +45 -7
- package/dist/core/channel-loader.js +2 -0
- package/dist/core/command/agent-control.js +260 -21
- package/dist/core/command/agent-storage-menu.js +5 -3
- package/dist/core/command/cli-intent-parser.js +8 -3
- package/dist/core/command/command-handler.js +112 -154
- package/dist/core/command/connect-menu.js +11 -3
- package/dist/core/command/data-root-resolver.js +5 -4
- package/dist/core/command/memory-slash.js +16 -8
- package/dist/core/command/menu-catalog.js +10 -6
- package/dist/core/command/menu-handler.js +106 -73
- package/dist/core/command/menu-protocol.js +15 -1
- package/dist/core/command/provider-menu.js +46 -0
- package/dist/core/command/role-menu.js +147 -30
- package/dist/core/command/slash-handler.js +125 -255
- package/dist/core/daemon-control.js +26 -27
- package/dist/core/event-catalog.js +10 -0
- package/dist/core/evolagent-registry.js +7 -5
- package/dist/core/evolagent.js +10 -8
- package/dist/core/file-integrity/store.js +11 -0
- package/dist/core/handoff/ipc-registration.js +3 -0
- package/dist/core/inference/text-inference.js +31 -40
- package/dist/core/initialization/service.js +491 -0
- package/dist/core/ipc-registration.js +40 -24
- package/dist/core/memory/consolidation-context.js +51 -5
- package/dist/core/memory/consolidation-ops-records.js +96 -1
- package/dist/core/memory/consolidation-trigger.js +208 -191
- package/dist/core/memory/consolidation.js +333 -35
- package/dist/core/memory/index-manager.js +27 -5
- package/dist/core/memory/memory-admin.js +45 -5
- package/dist/core/memory/memory-writer.js +163 -18
- package/dist/core/memory/relation-idle-monitor.js +119 -58
- package/dist/core/memory/relation-sweep.js +55 -45
- package/dist/core/message/aun-ipc-sender.js +81 -69
- package/dist/core/message/im-renderer.js +25 -4
- package/dist/core/message/message-admission.js +89 -4
- package/dist/core/message/message-bridge.js +66 -21
- package/dist/core/message/response-engine.js +108 -35
- package/dist/core/model/catalog-service.js +5 -3
- package/dist/core/model/config-scope.js +29 -71
- package/dist/core/model/field-scope.js +1 -1
- package/dist/core/model/model-catalog.js +30 -41
- package/dist/core/model/model-diagnostics.js +4 -3
- package/dist/core/model/model-fallback.js +10 -7
- package/dist/core/model/model-permission.js +3 -2
- package/dist/core/ownership/owner-resolver.js +772 -158
- package/dist/core/permission/approval-gateway.js +103 -403
- package/dist/core/permission/ec-command-parser.js +62 -6
- package/dist/core/permission/index.js +1 -0
- package/dist/core/permission/permission-result.js +78 -0
- package/dist/core/permission/sandbox-runtime.js +18 -1
- package/dist/core/permission/tool-policy.js +21 -46
- package/dist/core/relation/peer-identity.js +64 -1
- package/dist/core/role/runtime-policy.js +72 -13
- package/dist/core/session/session-capsule.js +1 -1
- package/dist/core/session/session-idle-monitor.js +1 -1
- package/dist/core/session/session-manager.js +20 -0
- package/dist/core/session/session-renew.js +3 -3
- package/dist/core/skill/index-generator.js +15 -5
- package/dist/core/system-session/memory.js +43 -8
- package/dist/core/thread/thread-service.js +19 -6
- package/dist/directory/owner-directory-admission.js +95 -0
- package/dist/directory/owner-directory-authorization.js +309 -0
- package/dist/directory/owner-directory-cache.js +635 -0
- package/dist/eck/kit-renderer.js +2 -2
- package/dist/ecweb/basic-settings-service.js +33 -40
- package/dist/ecweb/init-menu.js +32 -0
- package/dist/ecweb/message-context-service.js +110 -0
- package/dist/index.js +93 -19
- package/dist/ipc.js +247 -29
- package/dist/paths.js +7 -0
- package/dist/proxy/command.js +122 -0
- package/dist/proxy/ipc-service.js +68 -0
- package/dist/proxy/manager.js +484 -0
- package/dist/proxy/service-supervisor.js +603 -0
- package/dist/response-system/engines/v1/proactive-flow.js +16 -6
- package/dist/trigger/authorization.js +27 -17
- package/dist/trigger/feedback.js +2 -3
- package/dist/trigger/ipc-service.js +105 -78
- package/dist/trigger/local-files.js +2 -1
- package/dist/trigger/manager.js +5 -3
- package/dist/trigger/scheduler.js +88 -6
- package/dist/utils/atomic-write.js +28 -0
- package/dist/utils/cross-platform.js +14 -0
- package/dist/utils/ecweb-supervisor.js +258 -83
- package/dist/utils/error-utils.js +26 -2
- package/dist/utils/logger.js +92 -6
- package/dist/utils/package-version-info.js +4 -2
- package/dist/utils/welcome.js +1 -1
- package/kits/docs/INDEX.md +1 -0
- package/kits/docs/aun/SDK.md +4 -1
- package/kits/docs/channels/aun.md +9 -0
- package/kits/docs/evolcore/INDEX.md +1 -0
- package/kits/docs/evolcore/agent.md +8 -1
- package/kits/docs/evolcore/aid.md +67 -17
- package/kits/docs/evolcore/config.md +28 -7
- package/kits/docs/evolcore/contact.md +3 -0
- package/kits/docs/evolcore/fs.md +11 -0
- package/kits/docs/evolcore/group.md +1 -1
- package/kits/docs/evolcore/memory-architecture.md +2 -2
- package/kits/docs/evolcore/memory-consolidation.md +32 -8
- package/kits/docs/evolcore/memory.md +1 -1
- package/kits/docs/evolcore/menu-protocol.md +4 -0
- package/kits/docs/evolcore/model.md +23 -20
- package/kits/docs/evolcore/msg.md +4 -7
- package/kits/docs/evolcore/provider.md +50 -0
- package/kits/docs/identity/AID_PROFILE_SPEC.md +52 -17
- package/kits/migrations/migrate-contact-book-v2.mjs +30 -11
- package/kits/rules/01-overview.md +3 -0
- package/kits/rules/04-relation.md +1 -1
- package/kits/schemas/_meta.json +37 -4
- package/kits/schemas/agent-config.schema.13.json +8 -3
- package/kits/schemas/agent-config.schema.16.json +891 -0
- package/kits/schemas/daemon.schema.9.json +366 -0
- package/kits/schemas/defaults.schema.5.json +406 -127
- package/kits/schemas/relation-config.schema.11.json +277 -0
- package/kits/schemas/role-config.schema.1.json +7 -7
- package/kits/skills/.switches.json +4 -0
- package/kits/skills/ec-agent-create/SKILL.md +3 -0
- package/kits/skills/ec-identity/SKILL.md +2 -2
- package/kits/skills/ec-service-proxy/SKILL.md +118 -0
- package/kits/skills/ec-skill/scripts/skill.mjs +6 -0
- package/kits/skills/personal-assistant/SKILL.md +176 -0
- package/kits/skills/personal-assistant/references/01-workspace-spec.md +333 -0
- package/kits/skills/personal-assistant/references/02-bootstrap.md +205 -0
- package/kits/skills/personal-assistant/references/03-data-flow-and-gaps.md +177 -0
- package/kits/skills/personal-assistant/references/04-frontend-contract.md +439 -0
- package/kits/skills/personal-assistant/references/05-directory-reference.md +292 -0
- package/kits/skills/personal-assistant/references/06-render-config.md +350 -0
- package/kits/skills/personal-assistant/schema/workspace.schema.json +85 -0
- package/kits/skills/personal-assistant/schema/workspace.v1.json +66 -0
- package/kits/skills/personal-assistant/scripts/archive.mjs +154 -0
- package/kits/skills/personal-assistant/scripts/check.mjs +114 -0
- package/kits/skills/personal-assistant/scripts/index-rebuild.mjs +252 -0
- package/kits/skills/personal-assistant/scripts/ingest.mjs +344 -0
- package/kits/skills/personal-assistant/scripts/init.mjs +270 -0
- package/kits/skills/personal-assistant/scripts/lib/apply.mjs +92 -0
- package/kits/skills/personal-assistant/scripts/lib/schema.mjs +96 -0
- package/kits/skills/personal-assistant/scripts/lib/time.mjs +28 -0
- package/kits/skills/personal-assistant/scripts/migrate.mjs +172 -0
- package/kits/skills/personal-assistant/scripts/search.mjs +172 -0
- package/kits/skills/personal-assistant/scripts/verify.mjs +196 -0
- package/kits/skills/personal-assistant/templates/INDEX.md.tpl +24 -0
- package/kits/skills/personal-assistant/templates/README.md.tpl +108 -0
- package/kits/skills/personal-assistant/triggers/ingest-daily.json +23 -0
- package/kits/skills/personal-assistant/triggers/memory-consolidate.json +23 -0
- package/kits/skills/personal-assistant/triggers/report-daily.json +23 -0
- package/kits/skills/personal-assistant/triggers/report-weekly.json +23 -0
- package/kits/skills/thread-ops/SKILL.md +5 -0
- package/kits/templates/memory-consolidation/forget.md +19 -6
- package/kits/templates/memory-consolidation/notes.md +1 -1
- package/kits/templates/memory-consolidation/profile.md +2 -2
- package/kits/templates/memory-consolidation/promote.md +20 -3
- package/kits/templates/roles/member.json +8 -1
- package/kits/templates/system-fragments/baseagent.md +2 -2
- package/kits/templates/system-fragments/memory.md +4 -4
- package/kits/templates/system-fragments/venue.md +1 -1
- package/package.json +7 -3
- package/dist/core/message/create-status.js +0 -96
|
@@ -22,7 +22,8 @@ import { logger } from '../../utils/logger.js';
|
|
|
22
22
|
import { atomicWriteText } from '../../utils/atomic-write.js';
|
|
23
23
|
import { appendOpsRecord, feedbackFilePath, readFeedbackText } from './consolidation-ops-records.js';
|
|
24
24
|
import { updateBatchMeta } from './batch-meta.js';
|
|
25
|
-
import { checkConsolidationCandidates, shouldUseMetaIndex } from './consolidation-trigger.js';
|
|
25
|
+
import { checkConsolidationCandidates, hasOwnerConsolidationCandidates, isConsolidatableRelation, judgeRelationMode, recordProfileAttempt, shouldUseMetaIndex } from './consolidation-trigger.js';
|
|
26
|
+
import { taskContextOfRoute } from './relation-idle-monitor.js';
|
|
26
27
|
/**
|
|
27
28
|
* 清理旧整理批次,全局保留最新的 10 个(不区分 aid 或 mode)。
|
|
28
29
|
* 启动新整理时调用;selfAid 参数保留供日志上下文用,不参与过滤。
|
|
@@ -215,6 +216,10 @@ export async function launchConsolidation(channel, params) {
|
|
|
215
216
|
const prompt = [
|
|
216
217
|
'你正在执行记忆整理任务。',
|
|
217
218
|
`本次整理模式:${mode}`,
|
|
219
|
+
// 整理会话自身的 peer 是 bg: 伪对端,不写明目标的话,读完上下文文件之前不知道在整理谁
|
|
220
|
+
effectiveCtx.peerKey
|
|
221
|
+
? `整理对象:${selfAid} 与 ${effectiveCtx.peerKey} 的关系`
|
|
222
|
+
: `整理对象:${selfAid} 的 ${params.scopeOverride ?? 'agent'} 层记忆`,
|
|
218
223
|
`整理上下文文件路径:${ctxFile.filePath}`,
|
|
219
224
|
'',
|
|
220
225
|
'请先读取上述上下文文件,再按照注入的整理规范完成本次整理。',
|
|
@@ -247,6 +252,7 @@ export async function launchConsolidation(channel, params) {
|
|
|
247
252
|
...(ownerAid ? { consolidationOwnerAid: ownerAid } : {}),
|
|
248
253
|
consolidationUsedThreadSnapshots: !!ctxFile.threadFiles?.length,
|
|
249
254
|
...(ctxFile.snapshotThroughTs ? { consolidationSnapshotThroughTs: ctxFile.snapshotThroughTs } : {}),
|
|
255
|
+
...(ctxFile.upgradeCandidateFiles ? { consolidationUpgradeCandidates: ctxFile.upgradeCandidateFiles } : {}),
|
|
250
256
|
...(effectiveCtx.peerKey ? { peerKey: effectiveCtx.peerKey } : {}),
|
|
251
257
|
...(feedbackOrigin ? { feedbackOrigin } : {}),
|
|
252
258
|
consolidationTriggeredBy: triggeredBy,
|
|
@@ -272,6 +278,10 @@ export async function launchConsolidation(channel, params) {
|
|
|
272
278
|
logger.info(`[Memory] 已拉起整理会话(mode=${mode},triggeredBy=${triggeredBy},baseagent=${baseagent},` +
|
|
273
279
|
`selfAid=${selfAid}${effectiveCtx.peerKey ? `,peerKey=${effectiveCtx.peerKey}` : ''}` +
|
|
274
280
|
`${executionSessionId ? `,session=${executionSessionId}` : ''})`);
|
|
281
|
+
// 画像退避:每次拉起都记一次尝试,定时检查据此限流(见 consolidation-trigger.ts)
|
|
282
|
+
if (mode === 'profile' && effectiveCtx.peerKey) {
|
|
283
|
+
recordProfileAttempt(path.join(resolveRoot(), 'agents', selfAid, 'relations', effectiveCtx.peerKey, 'memory'));
|
|
284
|
+
}
|
|
275
285
|
return {
|
|
276
286
|
triggerId,
|
|
277
287
|
contextFile: ctxFile.filePath,
|
|
@@ -313,6 +323,140 @@ function countClaimedNotes(memoryDir) {
|
|
|
313
323
|
return 0;
|
|
314
324
|
}
|
|
315
325
|
}
|
|
326
|
+
/**
|
|
327
|
+
* daemon 启动时调用:整理会话不跨进程存活,此刻所有 processing/ 都没有主人,一律归还 pending/。
|
|
328
|
+
* 返回仍有待整理 note 的关系及其最老 note 的 mtime,供关系计时补登(部署前就躺在 pending 里的、
|
|
329
|
+
* 计时状态丢失的 note 都没有时钟)。只扫真实对端的关系。
|
|
330
|
+
*/
|
|
331
|
+
export function recoverRelationPendingNotes(root = resolveRoot()) {
|
|
332
|
+
const out = [];
|
|
333
|
+
const agentsDir = path.join(root, 'agents');
|
|
334
|
+
let aids = [];
|
|
335
|
+
try {
|
|
336
|
+
aids = fs.readdirSync(agentsDir);
|
|
337
|
+
}
|
|
338
|
+
catch {
|
|
339
|
+
return out;
|
|
340
|
+
}
|
|
341
|
+
for (const selfAid of aids) {
|
|
342
|
+
const relationsDir = path.join(agentsDir, selfAid, 'relations');
|
|
343
|
+
let peerKeys = [];
|
|
344
|
+
try {
|
|
345
|
+
peerKeys = fs.readdirSync(relationsDir);
|
|
346
|
+
}
|
|
347
|
+
catch {
|
|
348
|
+
continue;
|
|
349
|
+
}
|
|
350
|
+
for (const peerKey of peerKeys) {
|
|
351
|
+
if (!isConsolidatableRelation(peerKey))
|
|
352
|
+
continue;
|
|
353
|
+
const memoryDir = path.join(relationsDir, peerKey, 'memory');
|
|
354
|
+
try {
|
|
355
|
+
if (fs.existsSync(path.join(memoryDir, 'processing'))) {
|
|
356
|
+
logger.warn(`[Memory] 启动时发现未收尾的 processing/,归还 pending/(aid=${selfAid},peerKey=${peerKey})`);
|
|
357
|
+
restorePendingNotes(memoryDir);
|
|
358
|
+
}
|
|
359
|
+
const pendingDir = path.join(memoryDir, 'pending');
|
|
360
|
+
if (!fs.existsSync(pendingDir))
|
|
361
|
+
continue;
|
|
362
|
+
let oldest;
|
|
363
|
+
for (const f of fs.readdirSync(pendingDir)) {
|
|
364
|
+
if (!f.endsWith('.md'))
|
|
365
|
+
continue;
|
|
366
|
+
const m = fs.statSync(path.join(pendingDir, f)).mtimeMs;
|
|
367
|
+
if (oldest === undefined || m < oldest)
|
|
368
|
+
oldest = m;
|
|
369
|
+
}
|
|
370
|
+
if (oldest !== undefined)
|
|
371
|
+
out.push({ selfAid, peerKey, oldestNoteAt: oldest });
|
|
372
|
+
}
|
|
373
|
+
catch (error) {
|
|
374
|
+
logger.warn(`[Memory] 启动时恢复 pending 失败(aid=${selfAid},peerKey=${peerKey}): ${error?.message}`);
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
return out;
|
|
379
|
+
}
|
|
380
|
+
/**
|
|
381
|
+
* 索引自愈:MEMORY.md 缺失(consolidated/ 里却有记忆),或索引列出的条目在 consolidated/ 里已不存在,就重建。
|
|
382
|
+
* 覆盖三层:每个真实关系、agent 自身(personal/memory)、owner 层。
|
|
383
|
+
*
|
|
384
|
+
* 旧版本收尾时曾把索引重建到伪关系目录里,留下「有记忆没索引」「索引指向已删文件」的关系,
|
|
385
|
+
* 主会话的 relation-memory 段注入的就是这份索引,坏了等于这个关系的记忆整体不可见。
|
|
386
|
+
* MEMORY.md 是派生文件,重建无副作用。dryRun 只报不改。
|
|
387
|
+
*/
|
|
388
|
+
export async function healMemoryIndexes(opts = {}) {
|
|
389
|
+
const root = opts.root ?? resolveRoot();
|
|
390
|
+
const { readIndexSlugs, rebuildIndex, refreshMemoryMeta, DEFAULT_INDEX_MAX_BYTES } = await import('./index-manager.js');
|
|
391
|
+
const targets = [];
|
|
392
|
+
const agentsDir = path.join(root, 'agents');
|
|
393
|
+
let aids = [];
|
|
394
|
+
try {
|
|
395
|
+
aids = fs.readdirSync(agentsDir);
|
|
396
|
+
}
|
|
397
|
+
catch { /* 没有 agent */ }
|
|
398
|
+
for (const selfAid of aids) {
|
|
399
|
+
targets.push({ memoryDir: path.join(agentsDir, selfAid, 'personal', 'memory'), scope: 'agent', selfAid });
|
|
400
|
+
let peerKeys = [];
|
|
401
|
+
try {
|
|
402
|
+
peerKeys = fs.readdirSync(path.join(agentsDir, selfAid, 'relations'));
|
|
403
|
+
}
|
|
404
|
+
catch {
|
|
405
|
+
continue;
|
|
406
|
+
}
|
|
407
|
+
for (const peerKey of peerKeys) {
|
|
408
|
+
if (!isConsolidatableRelation(peerKey))
|
|
409
|
+
continue;
|
|
410
|
+
targets.push({ memoryDir: path.join(agentsDir, selfAid, 'relations', peerKey, 'memory'), scope: 'relation', selfAid });
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
let owners = [];
|
|
414
|
+
try {
|
|
415
|
+
owners = fs.readdirSync(path.join(root, 'memory', 'owner'));
|
|
416
|
+
}
|
|
417
|
+
catch { /* 没有 owner 层 */ }
|
|
418
|
+
for (const ownerAid of owners) {
|
|
419
|
+
targets.push({ memoryDir: path.join(root, 'memory', 'owner', ownerAid), scope: 'owner' });
|
|
420
|
+
}
|
|
421
|
+
const healed = [];
|
|
422
|
+
for (const target of targets) {
|
|
423
|
+
const consolidatedDir = path.join(target.memoryDir, 'consolidated');
|
|
424
|
+
const indexPath = path.join(target.memoryDir, 'MEMORY.md');
|
|
425
|
+
let files;
|
|
426
|
+
try {
|
|
427
|
+
files = new Set(fs.readdirSync(consolidatedDir).filter(f => f.endsWith('.md')).map(f => f.slice(0, -3)));
|
|
428
|
+
}
|
|
429
|
+
catch {
|
|
430
|
+
continue;
|
|
431
|
+
}
|
|
432
|
+
const slugs = readIndexSlugs(indexPath);
|
|
433
|
+
let reason;
|
|
434
|
+
if (slugs === null) {
|
|
435
|
+
if (files.size > 0)
|
|
436
|
+
reason = `索引缺失(consolidated/ 有 ${files.size} 条)`;
|
|
437
|
+
}
|
|
438
|
+
else {
|
|
439
|
+
const dangling = slugs.filter(slug => !files.has(slug));
|
|
440
|
+
if (dangling.length > 0)
|
|
441
|
+
reason = `索引指向不存在的文件:${dangling.slice(0, 3).join(', ')}${dangling.length > 3 ? ' …' : ''}`;
|
|
442
|
+
}
|
|
443
|
+
if (!reason)
|
|
444
|
+
continue;
|
|
445
|
+
healed.push({ memoryDir: target.memoryDir, scope: target.scope, reason });
|
|
446
|
+
if (opts.dryRun)
|
|
447
|
+
continue;
|
|
448
|
+
try {
|
|
449
|
+
const maxBytes = (target.selfAid ? opts.indexMaxBytes?.(target.selfAid) : undefined) ?? DEFAULT_INDEX_MAX_BYTES;
|
|
450
|
+
await rebuildIndex(consolidatedDir, indexPath, target.scope, maxBytes);
|
|
451
|
+
refreshMemoryMeta(target.memoryDir);
|
|
452
|
+
logger.info(`[Memory] 索引自愈:已重建 ${indexPath}(${reason})`);
|
|
453
|
+
}
|
|
454
|
+
catch (error) {
|
|
455
|
+
logger.warn(`[Memory] 索引自愈失败(${indexPath}): ${error?.message}`);
|
|
456
|
+
}
|
|
457
|
+
}
|
|
458
|
+
return healed;
|
|
459
|
+
}
|
|
316
460
|
function restorePendingNotes(memoryDir) {
|
|
317
461
|
const processingDir = path.join(memoryDir, 'processing');
|
|
318
462
|
if (!fs.existsSync(processingDir))
|
|
@@ -426,25 +570,102 @@ export function recordConsolidationOutcome(contextFilePath, outcome) {
|
|
|
426
570
|
}
|
|
427
571
|
catch { /* ignore */ }
|
|
428
572
|
}
|
|
573
|
+
// ── agent 层升级标签的跨层触发 ──────────────────────────────────────────────
|
|
574
|
+
/**
|
|
575
|
+
* generalize 收尾后,agent 层还有 `to-promote` 就立即按 owner 关系拉起 promote。
|
|
576
|
+
*
|
|
577
|
+
* agent 级记忆不挂在任何关系下,而关系整理链按「某个关系空闲」推进,owner 关系可能很久
|
|
578
|
+
* 没活动,标签一直等不到评估。泛化收尾那一刻就知道有东西要提升,直接入队即可。
|
|
579
|
+
*
|
|
580
|
+
* 不防抖:同一 agent 的整理串行执行,收尾时本 agent 不会有别的 generalize 并发产出;
|
|
581
|
+
* 入队时本 agent 的槽位尚未释放,请求排在队列里,槽位一空就拉起。
|
|
582
|
+
*
|
|
583
|
+
* 跳过的情况:这次 generalize 本身是 owner 关系整理链的一步——链的下一步就是 promote,
|
|
584
|
+
* 且 promote 已计入 agent 层标签,再拉一次就重复了。
|
|
585
|
+
*/
|
|
586
|
+
export async function triggerAgentPromote(params) {
|
|
587
|
+
const { selfAid } = params;
|
|
588
|
+
try {
|
|
589
|
+
const ownerAid = params.ownerAid ?? getPrimaryOwner(selfAid);
|
|
590
|
+
if (!ownerAid)
|
|
591
|
+
return;
|
|
592
|
+
const agentConsolidated = path.join(agentDir(selfAid), 'personal', 'memory', 'consolidated');
|
|
593
|
+
const { countTaggedIn } = await import('./consolidation-trigger.js');
|
|
594
|
+
if (countTaggedIn(agentConsolidated, 'to-promote') === 0)
|
|
595
|
+
return;
|
|
596
|
+
// owner 关系本身可能还没有目录(没跟 owner 聊过),此时没有可挂靠的关系,留给每小时检查
|
|
597
|
+
const ownerPeerKey = findOwnerPeerKeyFor(selfAid, ownerAid);
|
|
598
|
+
if (!ownerPeerKey)
|
|
599
|
+
return;
|
|
600
|
+
if (params.inRelationSweep && params.sessionPeerKey === ownerPeerKey)
|
|
601
|
+
return;
|
|
602
|
+
const agent = params.getAgent(selfAid);
|
|
603
|
+
if (!agent?.baseagent)
|
|
604
|
+
return;
|
|
605
|
+
if (consolidationBlockedReason(selfAid))
|
|
606
|
+
return;
|
|
607
|
+
const result = await params.enqueueConsolidation({
|
|
608
|
+
taskCtx: { selfAid, peerKey: ownerPeerKey, baseagent: agent.baseagent },
|
|
609
|
+
mode: 'promote',
|
|
610
|
+
triggeredBy: 'relation-idle', // 静默:不对 owner 发反馈
|
|
611
|
+
ownerAid,
|
|
612
|
+
});
|
|
613
|
+
if (result)
|
|
614
|
+
logger.info(`[Memory] agent 层有 to-promote,已拉起 promote(aid=${selfAid},peerKey=${ownerPeerKey})`);
|
|
615
|
+
}
|
|
616
|
+
catch (e) {
|
|
617
|
+
logger.warn(`[Memory] agent 层 promote 触发失败(aid=${selfAid}): ${e?.message}`);
|
|
618
|
+
}
|
|
619
|
+
}
|
|
620
|
+
/** 找对端是 owner 的关系目录名(peerKey 形如 aun#<ownerAid>)。没聊过 owner 时返回 undefined。 */
|
|
621
|
+
function findOwnerPeerKeyFor(selfAid, ownerAid) {
|
|
622
|
+
const relationsDir = path.join(agentDir(selfAid), 'relations');
|
|
623
|
+
if (!fs.existsSync(relationsDir))
|
|
624
|
+
return undefined;
|
|
625
|
+
for (const name of fs.readdirSync(relationsDir)) {
|
|
626
|
+
const peerId = name.includes('#') ? name.slice(name.indexOf('#') + 1) : name;
|
|
627
|
+
// 群关系 peerKey 里 owner 不是对端本身,跳过
|
|
628
|
+
if (peerId === ownerAid)
|
|
629
|
+
return name;
|
|
630
|
+
}
|
|
631
|
+
return undefined;
|
|
632
|
+
}
|
|
429
633
|
/**
|
|
430
634
|
* 整理会话失败/中断时,依据 ops.jsonl 逐条回撤成功的写操作。
|
|
431
635
|
* 回撤顺序:倒序(最后写的先撤)。
|
|
432
636
|
*
|
|
433
637
|
* create → 删除写入的文件,若有 archivedFiles 将其还原回 consolidated/
|
|
434
|
-
* update →
|
|
638
|
+
* update → 恢复修改前的 tags、updatedAt 与 mtime
|
|
435
639
|
* archive → 将文件从 archivedTo 移回 sourceFile
|
|
640
|
+
*
|
|
641
|
+
* 每一步都过台账(见 memory-writer.ts 的 revert* 辅助函数)。写过遗忘日志的操作,
|
|
642
|
+
* 回撤时在同一份日志里补一行「撤销…」,日志才与磁盘对得上。
|
|
436
643
|
*/
|
|
437
644
|
export async function rollbackConsolidation(batchDir, selfAid) {
|
|
438
645
|
const { readRollbackRecords, appendOpsRecord } = await import('./consolidation-ops-records.js');
|
|
646
|
+
const { revertMemoryArchive, revertMemoryCreate, revertMemoryUpdate, sourceMemoryDirOf } = await import('./memory-writer.js');
|
|
647
|
+
const { appendForgetLog, slugOfMemoryFile } = await import('./consolidation-ops-records.js');
|
|
439
648
|
const entries = readRollbackRecords(batchDir);
|
|
440
649
|
let rolledBack = 0;
|
|
441
650
|
const errors = [];
|
|
651
|
+
const undoSource = `rollback:${path.basename(batchDir)}`;
|
|
652
|
+
const undoReason = '整理会话失败或中断,按批次回撤';
|
|
653
|
+
const logUndo = (memoryFile, action) => {
|
|
654
|
+
try {
|
|
655
|
+
appendForgetLog(sourceMemoryDirOf(memoryFile), {
|
|
656
|
+
action, slug: slugOfMemoryFile(memoryFile), reason: undoReason, source: undoSource,
|
|
657
|
+
});
|
|
658
|
+
}
|
|
659
|
+
catch (e) {
|
|
660
|
+
logger.warn(`[Memory] 回撤的遗忘日志未写入(${memoryFile}): ${e?.message}`);
|
|
661
|
+
}
|
|
662
|
+
};
|
|
442
663
|
for (const entry of entries) {
|
|
443
664
|
try {
|
|
444
665
|
if (entry.op === 'create') {
|
|
445
666
|
// 删除写入的文件
|
|
446
667
|
if (entry.filePath && fs.existsSync(entry.filePath)) {
|
|
447
|
-
|
|
668
|
+
revertMemoryCreate(entry.filePath);
|
|
448
669
|
}
|
|
449
670
|
// 还原被归档的旧文件
|
|
450
671
|
for (const af of (entry.archivedFiles ?? [])) {
|
|
@@ -452,30 +673,37 @@ export async function rollbackConsolidation(batchDir, selfAid) {
|
|
|
452
673
|
const dest = af.source.includes('/') || af.source.includes('\\')
|
|
453
674
|
? af.source
|
|
454
675
|
: path.join(path.dirname(af.archivedTo), path.basename(af.archivedTo));
|
|
455
|
-
|
|
456
|
-
|
|
676
|
+
// 旧版 ops 记录的 source 是 argv 原文,解析不出原路径时 dest 就是 archivedTo 本身
|
|
677
|
+
if (path.resolve(dest) === path.resolve(af.archivedTo))
|
|
678
|
+
continue;
|
|
679
|
+
revertMemoryArchive(af.archivedTo, dest);
|
|
680
|
+
if (entry.forgetLogged)
|
|
681
|
+
logUndo(dest, '撤销合并');
|
|
457
682
|
}
|
|
458
683
|
}
|
|
459
684
|
rolledBack++;
|
|
460
685
|
}
|
|
461
686
|
else if (entry.op === 'update') {
|
|
462
|
-
// 恢复修改前的 tags:读文件、替换 tags 行、重写(不重签名,仅文本恢复)
|
|
463
687
|
const target = entry.target;
|
|
464
688
|
if (target && fs.existsSync(target)) {
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
689
|
+
revertMemoryUpdate({
|
|
690
|
+
filePath: target,
|
|
691
|
+
prevTags: entry.prevTags ?? [],
|
|
692
|
+
prevUpdatedAt: entry.prevUpdatedAt,
|
|
693
|
+
prevMtimeMs: entry.prevMtimeMs,
|
|
694
|
+
selfAid,
|
|
695
|
+
});
|
|
696
|
+
if (entry.forgetLogged)
|
|
697
|
+
logUndo(target, '撤销续期');
|
|
471
698
|
rolledBack++;
|
|
472
699
|
}
|
|
473
700
|
}
|
|
474
701
|
else if (entry.op === 'archive') {
|
|
475
702
|
// 将文件从 archivedTo 移回 sourceFile
|
|
476
703
|
if (entry.archivedTo && entry.sourceFile && fs.existsSync(entry.archivedTo)) {
|
|
477
|
-
|
|
478
|
-
|
|
704
|
+
revertMemoryArchive(entry.archivedTo, entry.sourceFile);
|
|
705
|
+
if (entry.forgetLogged)
|
|
706
|
+
logUndo(entry.sourceFile, '撤销归档');
|
|
479
707
|
rolledBack++;
|
|
480
708
|
}
|
|
481
709
|
}
|
|
@@ -558,6 +786,33 @@ export function createConsolidationSessionFinalizer({ sessionManager, getAgent,
|
|
|
558
786
|
if (!success) {
|
|
559
787
|
await rollbackConsolidation(batchDir, session.selfAID);
|
|
560
788
|
}
|
|
789
|
+
// 升级标签兜底:模型评估完候选却没摘标签,关系整理链会按标签计数反复拉起同一轮
|
|
790
|
+
if (success && (meta.memoryMode === 'generalize' || meta.memoryMode === 'promote')
|
|
791
|
+
&& Array.isArray(meta.consolidationUpgradeCandidates)) {
|
|
792
|
+
try {
|
|
793
|
+
const { stripUpgradeTag } = await import('./memory-writer.js');
|
|
794
|
+
const { scopeOfMemoryDir } = await import('./consolidation-ops-records.js');
|
|
795
|
+
const tag = meta.memoryMode === 'generalize' ? 'to-generalize' : 'to-promote';
|
|
796
|
+
const stripped = stripUpgradeTag(meta.consolidationUpgradeCandidates, tag, session.selfAID);
|
|
797
|
+
for (const s of stripped) {
|
|
798
|
+
// 候选可能在关系层或 agent 层(promote 的两层来源),层从文件路径判定
|
|
799
|
+
const memoryDir = path.dirname(path.dirname(s.filePath));
|
|
800
|
+
appendOpsRecord(batchDir, {
|
|
801
|
+
ts: new Date().toISOString(), op: 'update', scope: scopeOfMemoryDir(memoryDir) ?? 'relation',
|
|
802
|
+
target: s.filePath,
|
|
803
|
+
tags: s.prevTags.filter(t => t !== tag), prevTags: s.prevTags,
|
|
804
|
+
memoryDir, ok: true,
|
|
805
|
+
reason: `收尾兜底:会话结束时候选仍带 ${tag},视为已评估`,
|
|
806
|
+
});
|
|
807
|
+
}
|
|
808
|
+
if (stripped.length > 0) {
|
|
809
|
+
logger.info(`[Memory] 收尾摘除 ${stripped.length} 条候选的 ${tag}(session=${sessionId})`);
|
|
810
|
+
}
|
|
811
|
+
}
|
|
812
|
+
catch (e) {
|
|
813
|
+
logger.warn(`[Memory] 升级标签兜底失败(session=${sessionId}): ${e?.message}`);
|
|
814
|
+
}
|
|
815
|
+
}
|
|
561
816
|
const completeResult = await onConsolidationComplete({
|
|
562
817
|
success,
|
|
563
818
|
batchDir,
|
|
@@ -617,7 +872,7 @@ export function createConsolidationSessionFinalizer({ sessionManager, getAgent,
|
|
|
617
872
|
const now = Date.now();
|
|
618
873
|
const affectedScopes = collectAffectedScopes(batchDir);
|
|
619
874
|
for (const [scope, memoryDir] of affectedScopes) {
|
|
620
|
-
//
|
|
875
|
+
// 关系层的到期审查由全局定时检查逐关系拉起,这里不重复拉会话
|
|
621
876
|
if (scope === 'relation')
|
|
622
877
|
continue;
|
|
623
878
|
const consolidatedDir = path.join(memoryDir, 'consolidated');
|
|
@@ -656,6 +911,18 @@ export function createConsolidationSessionFinalizer({ sessionManager, getAgent,
|
|
|
656
911
|
}
|
|
657
912
|
}
|
|
658
913
|
}
|
|
914
|
+
// agent 层升级标签的跨层触发:generalize 产出的 agent 级记忆打了 to-promote,
|
|
915
|
+
// 关系整理链(按关系空闲推进)等不到它,泛化收尾时直接拉起 promote。
|
|
916
|
+
if (success && meta.memoryMode === 'generalize' && session.selfAID) {
|
|
917
|
+
void triggerAgentPromote({
|
|
918
|
+
selfAid: session.selfAID,
|
|
919
|
+
ownerAid: meta.consolidationOwnerAid,
|
|
920
|
+
sessionPeerKey: typeof meta.peerKey === 'string' ? meta.peerKey : undefined,
|
|
921
|
+
inRelationSweep: typeof meta.relationSweepId === 'string',
|
|
922
|
+
enqueueConsolidation,
|
|
923
|
+
getAgent,
|
|
924
|
+
});
|
|
925
|
+
}
|
|
659
926
|
const agentSessionId = session.agentSessionId;
|
|
660
927
|
const projectPath = session.projectPath;
|
|
661
928
|
const baseagent = session.baseagent ?? 'claude';
|
|
@@ -865,6 +1132,8 @@ export class ConsolidationQueue {
|
|
|
865
1132
|
activeAgents = new Set();
|
|
866
1133
|
queues = new Map();
|
|
867
1134
|
activeSessions = new Map();
|
|
1135
|
+
/** 每个 agent 当前在跑的整理请求,供去重判断 */
|
|
1136
|
+
activeParams = new Map();
|
|
868
1137
|
timers = new Map();
|
|
869
1138
|
finalizer;
|
|
870
1139
|
launchFailedListener;
|
|
@@ -881,6 +1150,17 @@ export class ConsolidationQueue {
|
|
|
881
1150
|
setLaunchFailedListener(listener) {
|
|
882
1151
|
this.launchFailedListener = listener;
|
|
883
1152
|
}
|
|
1153
|
+
/** 同一 agent 是否已有同样的整理(同模式、同关系、同作用域)在排队或在跑。定时检查据此去重。 */
|
|
1154
|
+
isPendingOrActive(params) {
|
|
1155
|
+
const aid = params.taskCtx.selfAid;
|
|
1156
|
+
if (!aid)
|
|
1157
|
+
return false;
|
|
1158
|
+
const key = consolidationTaskKey(params);
|
|
1159
|
+
const active = this.activeParams.get(aid);
|
|
1160
|
+
if (active && consolidationTaskKey(active) === key)
|
|
1161
|
+
return true;
|
|
1162
|
+
return (this.queues.get(aid) ?? []).some(queued => consolidationTaskKey(queued) === key);
|
|
1163
|
+
}
|
|
884
1164
|
async enqueue(params) {
|
|
885
1165
|
const aid = params.taskCtx.selfAid;
|
|
886
1166
|
if (!aid)
|
|
@@ -919,6 +1199,7 @@ export class ConsolidationQueue {
|
|
|
919
1199
|
clearTimeout(timer);
|
|
920
1200
|
this.timers.delete(aid);
|
|
921
1201
|
this.activeSessions.delete(aid);
|
|
1202
|
+
this.activeParams.delete(aid);
|
|
922
1203
|
this.activeAgents.delete(aid);
|
|
923
1204
|
void this.drain();
|
|
924
1205
|
}
|
|
@@ -927,6 +1208,7 @@ export class ConsolidationQueue {
|
|
|
927
1208
|
if (!aid)
|
|
928
1209
|
return null;
|
|
929
1210
|
this.activeAgents.add(aid);
|
|
1211
|
+
this.activeParams.set(aid, params);
|
|
930
1212
|
try {
|
|
931
1213
|
const launched = await launchConsolidation(this.daemonChannel, params);
|
|
932
1214
|
if (launched.executionSessionId) {
|
|
@@ -956,6 +1238,7 @@ export class ConsolidationQueue {
|
|
|
956
1238
|
catch (error) {
|
|
957
1239
|
logger.warn(`[Memory] 拉起整理会话失败(aid=${aid},mode=${params.mode}): ${error?.message}`);
|
|
958
1240
|
this.activeAgents.delete(aid);
|
|
1241
|
+
this.activeParams.delete(aid);
|
|
959
1242
|
if (fromDrain && this.launchFailedListener) {
|
|
960
1243
|
try {
|
|
961
1244
|
this.launchFailedListener(params);
|
|
@@ -1002,7 +1285,11 @@ export function registerConsolidationLaunchExecutor({ ipcServer, enqueueConsolid
|
|
|
1002
1285
|
return result;
|
|
1003
1286
|
});
|
|
1004
1287
|
}
|
|
1005
|
-
|
|
1288
|
+
/** 整理请求的去重键:同 agent 下同模式、同关系、同作用域视为同一件事。 */
|
|
1289
|
+
function consolidationTaskKey(params) {
|
|
1290
|
+
return [params.mode, params.taskCtx.peerKey ?? '', params.scopeOverride ?? ''].join('|');
|
|
1291
|
+
}
|
|
1292
|
+
export function registerConsolidationCheckExecutor({ ipcServer, agentRegistry, enqueueConsolidation, getRelationRoute, isConsolidationPending, }) {
|
|
1006
1293
|
ipcServer.setConsolidationCheckExecutor(async ({ selfAid }) => {
|
|
1007
1294
|
const agent = agentRegistry.get(selfAid);
|
|
1008
1295
|
const agentConfig = agent?.config;
|
|
@@ -1011,8 +1298,6 @@ export function registerConsolidationCheckExecutor({ ipcServer, agentRegistry, e
|
|
|
1011
1298
|
if (consolidationBlockedReason(selfAid))
|
|
1012
1299
|
return { launched: [] };
|
|
1013
1300
|
recoverStaleConsolidationProcessing(selfAid);
|
|
1014
|
-
await ownerResolver.refresh(selfAid);
|
|
1015
|
-
const ownerAid = getPrimaryOwner(selfAid);
|
|
1016
1301
|
const agentCount = agentRegistry.runnableAgents().length;
|
|
1017
1302
|
const relationsDir = path.join(agentDir(selfAid), 'relations');
|
|
1018
1303
|
let relationCount = 0;
|
|
@@ -1025,34 +1310,43 @@ export function registerConsolidationCheckExecutor({ ipcServer, agentRegistry, e
|
|
|
1025
1310
|
// The relation directory is optional during initial bootstrap.
|
|
1026
1311
|
}
|
|
1027
1312
|
const useMetaIndex = shouldUseMetaIndex(agentCount, relationCount);
|
|
1313
|
+
const knownOwnerAid = ownerResolver.knownOwner(selfAid);
|
|
1028
1314
|
const candidates = checkConsolidationCandidates({
|
|
1029
|
-
selfAid,
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
|
|
1315
|
+
selfAid, config: { ...agentConfig?.memory, enableOwner: false }, useMetaIndex,
|
|
1316
|
+
});
|
|
1317
|
+
const ownerCandidates = hasOwnerConsolidationCandidates({
|
|
1318
|
+
selfAid, ownerAid: knownOwnerAid, config: agentConfig?.memory,
|
|
1033
1319
|
});
|
|
1320
|
+
if (candidates.length === 0 && !ownerCandidates)
|
|
1321
|
+
return { launched: [] };
|
|
1322
|
+
await ownerResolver.refresh(selfAid);
|
|
1323
|
+
const ownerAid = getPrimaryOwner(selfAid);
|
|
1324
|
+
if (ownerCandidates)
|
|
1325
|
+
candidates.push(...checkConsolidationCandidates({
|
|
1326
|
+
selfAid, ownerAid, config: agentConfig?.memory, useMetaIndex, ownerOnly: true,
|
|
1327
|
+
}));
|
|
1034
1328
|
const launched = [];
|
|
1035
1329
|
for (const candidate of candidates) {
|
|
1036
|
-
|
|
1037
|
-
requestRelationSweep(selfAid, candidate.peerKey);
|
|
1038
|
-
launched.push({ mode: `sweep:${candidate.mode}`, peerKey: candidate.peerKey });
|
|
1039
|
-
continue;
|
|
1040
|
-
}
|
|
1330
|
+
const route = candidate.peerKey ? getRelationRoute?.(selfAid, candidate.peerKey) : undefined;
|
|
1041
1331
|
const taskCtx = {
|
|
1042
1332
|
selfAid,
|
|
1043
1333
|
peerKey: candidate.peerKey,
|
|
1044
1334
|
baseagent: agent?.baseagent,
|
|
1045
1335
|
sessionId: undefined,
|
|
1336
|
+
...taskContextOfRoute(route),
|
|
1046
1337
|
};
|
|
1338
|
+
const params = {
|
|
1339
|
+
taskCtx,
|
|
1340
|
+
mode: candidate.mode,
|
|
1341
|
+
triggeredBy: 'periodic-trigger',
|
|
1342
|
+
ownerAid,
|
|
1343
|
+
forgetAfterDays: agentConfig?.memory?.forgetAfterDays,
|
|
1344
|
+
...(candidate.scope ? { scopeOverride: candidate.scope } : {}),
|
|
1345
|
+
};
|
|
1346
|
+
if (isConsolidationPending?.(params))
|
|
1347
|
+
continue;
|
|
1047
1348
|
try {
|
|
1048
|
-
const result = await enqueueConsolidation(
|
|
1049
|
-
taskCtx,
|
|
1050
|
-
mode: candidate.mode,
|
|
1051
|
-
triggeredBy: 'periodic-trigger',
|
|
1052
|
-
ownerAid,
|
|
1053
|
-
forgetAfterDays: agentConfig?.memory?.forgetAfterDays,
|
|
1054
|
-
...(candidate.scope ? { scopeOverride: candidate.scope } : {}),
|
|
1055
|
-
});
|
|
1349
|
+
const result = await enqueueConsolidation(params);
|
|
1056
1350
|
if (!result)
|
|
1057
1351
|
continue;
|
|
1058
1352
|
launched.push({ mode: candidate.mode, ...(candidate.peerKey ? { peerKey: candidate.peerKey } : {}) });
|
|
@@ -1080,6 +1374,10 @@ export function registerRelationConsolidationEvents({ eventBus, agentRegistry, r
|
|
|
1080
1374
|
const memoryConfig = agentRegistry.get(taskCtx.selfAid)?.config?.memory;
|
|
1081
1375
|
if (memoryConfig?.enableRelation === false)
|
|
1082
1376
|
return;
|
|
1377
|
+
if (judgeRelationMode({
|
|
1378
|
+
mode: 'notes', selfAid: taskCtx.selfAid, peerKey: taskCtx.peerKey, config: memoryConfig, force: true,
|
|
1379
|
+
}) !== 'run')
|
|
1380
|
+
return;
|
|
1083
1381
|
await ownerResolver.refresh(taskCtx.selfAid);
|
|
1084
1382
|
const ownerAid = getPrimaryOwner(taskCtx.selfAid);
|
|
1085
1383
|
await enqueueConsolidation({
|
|
@@ -22,9 +22,16 @@ import { getMemoryArray, getMemoryString, parseMemoryDocument } from './memory-d
|
|
|
22
22
|
import { registerFile, verifyFile } from '../file-integrity/store.js';
|
|
23
23
|
/** 索引容量上限(字节)。实际取值由调用方从 memory.indexMaxBytes 传入。 */
|
|
24
24
|
export const DEFAULT_INDEX_MAX_BYTES = 5 * 1024;
|
|
25
|
-
/** Fast metadata index for consolidation candidate scans. */
|
|
26
|
-
export const MEMORY_META_VERSION =
|
|
25
|
+
/** Fast metadata index for consolidation candidate scans. v2:画像判据改读 frontmatter 时间。 */
|
|
26
|
+
export const MEMORY_META_VERSION = 2;
|
|
27
27
|
export const MEMORY_META_FILENAME = '_meta.json';
|
|
28
|
+
/** frontmatter 里的 ISO 时间转毫秒;缺失或解析不了返回 null。 */
|
|
29
|
+
export function memoryTimeMs(value) {
|
|
30
|
+
if (!value)
|
|
31
|
+
return null;
|
|
32
|
+
const ms = Date.parse(value);
|
|
33
|
+
return Number.isFinite(ms) ? ms : null;
|
|
34
|
+
}
|
|
28
35
|
function metaPath(memoryDir) {
|
|
29
36
|
return path.join(memoryDir, MEMORY_META_FILENAME);
|
|
30
37
|
}
|
|
@@ -38,7 +45,8 @@ export function refreshMemoryMeta(memoryDir, now = Date.now()) {
|
|
|
38
45
|
hasToGeneralize: false,
|
|
39
46
|
hasToPromote: false,
|
|
40
47
|
profileSlug: null,
|
|
41
|
-
|
|
48
|
+
profileUpdatedAtMs: null,
|
|
49
|
+
newestCreatedAtMs: null,
|
|
42
50
|
oldestMtimeMs: null,
|
|
43
51
|
};
|
|
44
52
|
if (fs.existsSync(consolidatedDir)) {
|
|
@@ -58,14 +66,22 @@ export function refreshMemoryMeta(memoryDir, now = Date.now()) {
|
|
|
58
66
|
meta.count++;
|
|
59
67
|
if (meta.oldestMtimeMs === null || mtimeMs < meta.oldestMtimeMs)
|
|
60
68
|
meta.oldestMtimeMs = mtimeMs;
|
|
61
|
-
const
|
|
69
|
+
const document = parseMemoryDocument(content);
|
|
70
|
+
const tags = getMemoryArray(document, 'tags');
|
|
62
71
|
if (tags.includes('to-generalize'))
|
|
63
72
|
meta.hasToGeneralize = true;
|
|
64
73
|
if (tags.includes('to-promote'))
|
|
65
74
|
meta.hasToPromote = true;
|
|
66
75
|
if (tags.includes('profile')) {
|
|
67
76
|
meta.profileSlug = f.replace(/\.md$/, '');
|
|
68
|
-
meta.
|
|
77
|
+
meta.profileUpdatedAtMs = memoryTimeMs(getMemoryString(document, 'updatedAt'))
|
|
78
|
+
?? memoryTimeMs(getMemoryString(document, 'createdAt'));
|
|
79
|
+
}
|
|
80
|
+
else {
|
|
81
|
+
const createdAtMs = memoryTimeMs(getMemoryString(document, 'createdAt'));
|
|
82
|
+
if (createdAtMs !== null && (meta.newestCreatedAtMs === null || createdAtMs > meta.newestCreatedAtMs)) {
|
|
83
|
+
meta.newestCreatedAtMs = createdAtMs;
|
|
84
|
+
}
|
|
69
85
|
}
|
|
70
86
|
}
|
|
71
87
|
}
|
|
@@ -230,6 +246,12 @@ export function appendToIndex(indexPath, entry, scope, indexMaxBytes = DEFAULT_I
|
|
|
230
246
|
const kept = truncateToLimit(scope, filtered, indexMaxBytes);
|
|
231
247
|
writeIndexFile(indexPath, serializeIndex(scope, kept));
|
|
232
248
|
}
|
|
249
|
+
/** 索引里列出的 slug(三种历史格式都认);索引不存在时返回 null。 */
|
|
250
|
+
export function readIndexSlugs(indexPath) {
|
|
251
|
+
if (!fs.existsSync(indexPath))
|
|
252
|
+
return null;
|
|
253
|
+
return readIndexEntries(indexPath).map(e => e.slug);
|
|
254
|
+
}
|
|
233
255
|
function readIndexEntries(indexPath) {
|
|
234
256
|
if (!fs.existsSync(indexPath))
|
|
235
257
|
return [];
|