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
|
@@ -212,6 +212,7 @@ function scanCandidates(consolidatedDir, pick) {
|
|
|
212
212
|
relPath: `consolidated/${f}`,
|
|
213
213
|
description: readFmValue(content, 'description') ?? f.replace(/\.md$/, ''),
|
|
214
214
|
tags,
|
|
215
|
+
absPath: fp,
|
|
215
216
|
});
|
|
216
217
|
}
|
|
217
218
|
out.sort((a, b) => a.relPath.localeCompare(b.relPath));
|
|
@@ -225,7 +226,11 @@ function scanCandidates(consolidatedDir, pick) {
|
|
|
225
226
|
function withPeerPrefix(candidates, peerKey) {
|
|
226
227
|
if (!peerKey)
|
|
227
228
|
return candidates;
|
|
228
|
-
return candidates.map(c => ({ ...c, relPath: `${peerKey}/${c.relPath}
|
|
229
|
+
return candidates.map(c => ({ ...c, relPath: `${peerKey}/${c.relPath}`, sourceScope: 'relation' }));
|
|
230
|
+
}
|
|
231
|
+
/** agent 层候选加 `agent/` 前缀(promote 的第二来源层)。 */
|
|
232
|
+
function withAgentPrefix(candidates) {
|
|
233
|
+
return candidates.map(c => ({ ...c, relPath: `agent/${c.relPath}`, sourceScope: 'agent' }));
|
|
229
234
|
}
|
|
230
235
|
/** 按模式填充候选清单。 */
|
|
231
236
|
function fillCandidates(ctx, mode, forgetAfterDays, sourcePeerKey) {
|
|
@@ -234,7 +239,13 @@ function fillCandidates(ctx, mode, forgetAfterDays, sourcePeerKey) {
|
|
|
234
239
|
ctx.candidates = withPeerPrefix(scanCandidates(source, ({ tags }) => tags.includes('to-generalize')), sourcePeerKey);
|
|
235
240
|
}
|
|
236
241
|
else if (mode === 'promote') {
|
|
237
|
-
|
|
242
|
+
// 两层来源:关系层(候选本来的家)与 agent 层。
|
|
243
|
+
// generalize 手册要求"泛化出来、同时是 owner 规矩"的记忆在产出时打 to-promote,
|
|
244
|
+
// 只扫关系层的话这些标签永远不会被读到,规矩永远卡在 agent 级。
|
|
245
|
+
const relation = withPeerPrefix(scanCandidates(source, ({ tags }) => tags.includes('to-promote')), sourcePeerKey);
|
|
246
|
+
const agentDir = memoryDirOf('agent', ctx.selfAid, undefined, undefined);
|
|
247
|
+
const agent = withAgentPrefix(scanCandidates(path.join(agentDir, 'consolidated'), ({ tags }) => tags.includes('to-promote')));
|
|
248
|
+
ctx.candidates = [...relation, ...agent];
|
|
238
249
|
}
|
|
239
250
|
else if (mode === 'profile') {
|
|
240
251
|
ctx.candidates = scanCandidates(source, () => true);
|
|
@@ -314,13 +325,42 @@ function renderScopeSection(ctx, mode, threadFiles) {
|
|
|
314
325
|
// 其他模式:直接列出候选
|
|
315
326
|
lines.push(`**候选记忆(${ctx.candidates.length} 条)** —— 只列路径与描述,按需读正文:`);
|
|
316
327
|
lines.push('');
|
|
328
|
+
if ((mode === 'generalize' || mode === 'promote') && ctx.candidates.length > 0) {
|
|
329
|
+
// 候选在关系层 / agent 层,本模式写作用域在产出层:不带前缀会被解析成产出层同名文件
|
|
330
|
+
const tag = mode === 'generalize' ? 'to-generalize' : 'to-promote';
|
|
331
|
+
const relationPrefix = ctx.peerKey ?? '<peerKey>';
|
|
332
|
+
const hasAgent = ctx.candidates.some(c => c.sourceScope === 'agent');
|
|
333
|
+
const ownLayer = ctx.scope === 'agent'
|
|
334
|
+
? '本模式写作用域就是 agent 层,**裸 `consolidated/<文件名>` 会改到 agent 层同名文件**'
|
|
335
|
+
: `本模式写作用域是 ${ctx.scope} 层,**裸 \`consolidated/<文件名>\` 会改到 ${ctx.scope} 层同名文件**`;
|
|
336
|
+
lines.push(`> 摘掉 \`${tag}\` 时,\`ec memory update\` 的路径必须原样抄上面的候选路径:`
|
|
337
|
+
+ `关系层候选带 \`${relationPrefix}/\` 前缀`
|
|
338
|
+
+ (hasAgent ? ',agent 层候选带 `agent/` 前缀' : '')
|
|
339
|
+
+ `。${ownLayer},候选的标签不会被摘掉。`);
|
|
340
|
+
lines.push('');
|
|
341
|
+
}
|
|
317
342
|
if (ctx.candidates.length === 0) {
|
|
318
343
|
lines.push('(无候选)');
|
|
319
344
|
}
|
|
320
345
|
else {
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
346
|
+
// promote 有两层来源:分开列,避免把 agent 级记忆当成关系级去处理
|
|
347
|
+
const groups = mode === 'promote'
|
|
348
|
+
? [
|
|
349
|
+
{ title: '关系层', items: ctx.candidates.filter(c => c.sourceScope !== 'agent') },
|
|
350
|
+
{ title: 'agent 层', items: ctx.candidates.filter(c => c.sourceScope === 'agent') },
|
|
351
|
+
].filter(g => g.items.length > 0)
|
|
352
|
+
: [{ title: '', items: ctx.candidates }];
|
|
353
|
+
for (const g of groups) {
|
|
354
|
+
if (g.title) {
|
|
355
|
+
lines.push(`**${g.title}(${g.items.length} 条)**`);
|
|
356
|
+
lines.push('');
|
|
357
|
+
}
|
|
358
|
+
for (const c of g.items) {
|
|
359
|
+
const tags = c.tags.length > 0 ? ` [${c.tags.join(', ')}]` : '';
|
|
360
|
+
lines.push(`- \`${c.relPath}\`${tags} — ${c.description}`);
|
|
361
|
+
}
|
|
362
|
+
if (g.title)
|
|
363
|
+
lines.push('');
|
|
324
364
|
}
|
|
325
365
|
}
|
|
326
366
|
}
|
|
@@ -624,7 +664,13 @@ export function serializeConsolidationContext(input) {
|
|
|
624
664
|
startedAt: now.toISOString(),
|
|
625
665
|
status: 'running',
|
|
626
666
|
});
|
|
667
|
+
// generalize / promote 的候选绝对路径:收尾时据此兜底摘掉没被处理的升级标签
|
|
668
|
+
const upgradeCandidateFiles = (input.mode === 'generalize' || input.mode === 'promote')
|
|
669
|
+
? scopes.flatMap(s => (s.candidates ?? []).map(c => c.absPath
|
|
670
|
+
?? path.join(s.candidateSourceDir ?? s.consolidatedDir, path.basename(c.relPath))))
|
|
671
|
+
: undefined;
|
|
627
672
|
return { filePath: contextFilePath, batchDir, contextFilePath, scopes, historyJsonlPath, threadFiles, chatDir,
|
|
673
|
+
...(upgradeCandidateFiles?.length ? { upgradeCandidateFiles } : {}),
|
|
628
674
|
...(threadFiles?.length ? { snapshotThroughTs: now.getTime() } : {}) };
|
|
629
675
|
}
|
|
630
676
|
/** 会话结束后回填本次产出,追加到任务书末尾。 */
|
|
@@ -5,11 +5,13 @@
|
|
|
5
5
|
* - session-start / session-end:会话级别的开始/结束记录
|
|
6
6
|
* - create / update / archive:每次写操作的结果
|
|
7
7
|
* - summary:会话结束时的汇总行
|
|
8
|
+
* - FORGET-LOG.md:按记忆作用域保留遗忘、手动归档与回撤记录
|
|
8
9
|
*
|
|
9
10
|
* session-end 写在 onConsolidationComplete 里,失败时可依据 ops.jsonl 逐条回撤。
|
|
10
11
|
*/
|
|
11
12
|
import fs from 'fs';
|
|
12
13
|
import path from 'path';
|
|
14
|
+
import { logger } from '../../utils/logger.js';
|
|
13
15
|
/** 向 ops 记录文件追加一条记录。失败静默,不影响主流程。 */
|
|
14
16
|
export function appendOpsRecord(batchDir, entry) {
|
|
15
17
|
try {
|
|
@@ -118,7 +120,7 @@ export function collectAffectedScopes(batchDir) {
|
|
|
118
120
|
return [...result].map(([memoryDir, scope]) => [scope, memoryDir]);
|
|
119
121
|
}
|
|
120
122
|
/** 从 memory 根目录路径推断作用域;不是标准布局时返回 undefined。 */
|
|
121
|
-
function scopeOfMemoryDir(memoryDir) {
|
|
123
|
+
export function scopeOfMemoryDir(memoryDir) {
|
|
122
124
|
const parts = path.resolve(memoryDir).split(path.sep);
|
|
123
125
|
const last = parts.length - 1;
|
|
124
126
|
// .../agents/<selfAid>/relations/<peerKey>/memory
|
|
@@ -187,3 +189,96 @@ export function readContextBytes(batchDir) {
|
|
|
187
189
|
return 0;
|
|
188
190
|
}
|
|
189
191
|
}
|
|
192
|
+
/**
|
|
193
|
+
* 遗忘日志 FORGET-LOG.md(审计记录,不是记忆)
|
|
194
|
+
*
|
|
195
|
+
* 遗忘机制每动一条记忆,就在**该记忆所在层**的 `archive/FORGET-LOG.md` 追加一行:
|
|
196
|
+
* 时间、动作、记忆、理由、来源。理由是针对这一条记忆的,不是针对一次整理的
|
|
197
|
+
* (一次整理的总结是 feedback,记录在上面的 session-end 中)。
|
|
198
|
+
*
|
|
199
|
+
* 只记遗忘:forget 会话里的续期 / 归档 / 合并归档、它们的回撤,以及会话外的
|
|
200
|
+
* `/memory archive` / `/memory restore`。其他模式 `--replaces` 产生的归档不记——
|
|
201
|
+
* 那是记忆产生与泛化的过程,理由在 ops.jsonl。长期记忆的推迟审查(deferCandidate)
|
|
202
|
+
* 只刷 mtime,也不记。
|
|
203
|
+
*
|
|
204
|
+
* 放在 archive/ 下:archive/ 不在任何索引与召回路径上(rebuildIndex 只扫 consolidated/,
|
|
205
|
+
* `/memory list --archived` 只扫 archive/consolidated/),整理会话的读取范围也不含它。
|
|
206
|
+
* 只追加、不轮转,不随整理批次目录清理。只由 daemon 写,模型只能经 ec memory 命令间接产生行。
|
|
207
|
+
*/
|
|
208
|
+
/** 审计日志,不是记忆,不进索引。路径相对 memory 根目录。 */
|
|
209
|
+
export const FORGET_LOG_RELPATH = path.join('archive', 'FORGET-LOG.md');
|
|
210
|
+
/** 理由上限(按码点计)。超长拒绝而不截断:截断会丢意思,让模型自己缩短。 */
|
|
211
|
+
export const FORGET_REASON_MAX_CHARS = 80;
|
|
212
|
+
const HEADER = [
|
|
213
|
+
'# 遗忘日志',
|
|
214
|
+
'',
|
|
215
|
+
'> 审计记录,不是记忆,不进索引。由 daemon 追加,请勿手改。',
|
|
216
|
+
'',
|
|
217
|
+
'| 时间 | 动作 | 记忆 | 理由 | 来源 |',
|
|
218
|
+
'|---|---|---|---|---|',
|
|
219
|
+
'',
|
|
220
|
+
].join('\n');
|
|
221
|
+
export function forgetLogPath(memoryDir) {
|
|
222
|
+
return path.join(memoryDir, FORGET_LOG_RELPATH);
|
|
223
|
+
}
|
|
224
|
+
/** 理由归一:去首尾空白,换行与连续空白压成一个空格。 */
|
|
225
|
+
export function normalizeForgetReason(raw) {
|
|
226
|
+
return (raw ?? '').replace(/\s+/g, ' ').trim();
|
|
227
|
+
}
|
|
228
|
+
/** 校验理由:必填、≤80 字。返回归一后的理由,或给模型看的错误说明。 */
|
|
229
|
+
export function checkForgetReason(raw) {
|
|
230
|
+
const reason = normalizeForgetReason(raw);
|
|
231
|
+
if (!reason) {
|
|
232
|
+
return { ok: false, error: "forget 模式每条操作都要带 --reason '<理由>',说明这一条记忆为什么续期 / 归档 / 合并" };
|
|
233
|
+
}
|
|
234
|
+
const len = [...reason].length;
|
|
235
|
+
if (len > FORGET_REASON_MAX_CHARS) {
|
|
236
|
+
return { ok: false, error: `--reason 不能超过 ${FORGET_REASON_MAX_CHARS} 字(当前 ${len} 字),请缩短后重试` };
|
|
237
|
+
}
|
|
238
|
+
return { ok: true, reason };
|
|
239
|
+
}
|
|
240
|
+
/** 记忆文件路径 → slug。归档目标可能带时间戳后缀,调用方应传源路径。 */
|
|
241
|
+
export function slugOfMemoryFile(filePath) {
|
|
242
|
+
return path.basename(filePath).replace(/\.md$/, '');
|
|
243
|
+
}
|
|
244
|
+
/** 来源列:整理会话用批次目录名(带 agent AID,owner 层多 agent 共用时靠它区分)。 */
|
|
245
|
+
export function batchSourceLabel(batchDir) {
|
|
246
|
+
return batchDir ? path.basename(batchDir) : 'forget';
|
|
247
|
+
}
|
|
248
|
+
function cell(value) {
|
|
249
|
+
return normalizeForgetReason(value).replace(/\|/g, '\\|');
|
|
250
|
+
}
|
|
251
|
+
function formatLocalTime(d) {
|
|
252
|
+
const pad = (n) => String(n).padStart(2, '0');
|
|
253
|
+
const offsetMin = -d.getTimezoneOffset();
|
|
254
|
+
const sign = offsetMin >= 0 ? '+' : '-';
|
|
255
|
+
const abs = Math.abs(offsetMin);
|
|
256
|
+
return `${d.getFullYear()}-${pad(d.getMonth() + 1)}-${pad(d.getDate())} `
|
|
257
|
+
+ `${pad(d.getHours())}:${pad(d.getMinutes())}:${pad(d.getSeconds())} `
|
|
258
|
+
+ `${sign}${pad(Math.floor(abs / 60))}:${pad(abs % 60)}`;
|
|
259
|
+
}
|
|
260
|
+
/**
|
|
261
|
+
* 追加一行。文件不存在时先写表头。
|
|
262
|
+
* 失败只告警不抛:文件操作已经成功,不能因为日志写不进去让调用方以为操作失败。
|
|
263
|
+
* 返回是否写入成功,回撤据此决定要不要补「撤销」行。
|
|
264
|
+
*/
|
|
265
|
+
export function appendForgetLog(memoryDir, row) {
|
|
266
|
+
const line = `| ${formatLocalTime(row.now ?? new Date())} | ${cell(row.action)} | ${cell(row.slug)} | ${cell(row.reason)} | ${cell(row.source)} |\n`;
|
|
267
|
+
const filePath = forgetLogPath(memoryDir);
|
|
268
|
+
try {
|
|
269
|
+
fs.mkdirSync(path.dirname(filePath), { recursive: true });
|
|
270
|
+
try {
|
|
271
|
+
fs.writeFileSync(filePath, HEADER, { encoding: 'utf-8', flag: 'wx' });
|
|
272
|
+
}
|
|
273
|
+
catch (e) {
|
|
274
|
+
if (e?.code !== 'EEXIST')
|
|
275
|
+
throw e;
|
|
276
|
+
}
|
|
277
|
+
fs.appendFileSync(filePath, line, 'utf-8');
|
|
278
|
+
return true;
|
|
279
|
+
}
|
|
280
|
+
catch (e) {
|
|
281
|
+
logger.warn(`[Memory] 遗忘日志写入失败(${filePath}): ${e?.message};未记入的行: ${line.trim()}`);
|
|
282
|
+
return false;
|
|
283
|
+
}
|
|
284
|
+
}
|