evolcore 0.0.43-test.4 → 0.0.43-test.6
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 +93 -0
- package/bin/ecmsg.js +437 -91
- package/dist/agents/baseagent-availability.js +19 -4
- package/dist/agents/baseagent.js +2 -2
- package/dist/agents/claude-runner.js +19 -4
- package/dist/agents/codex/app-server-client.js +14 -3
- package/dist/agents/codex-runner.js +18 -23
- package/dist/aun/aid/agentmd.js +25 -0
- package/dist/aun/aid/control-aid.js +15 -0
- package/dist/aun/aid/identity.js +8 -0
- package/dist/aun/aid/ownership-operation.js +12 -3
- package/dist/aun/group/invite-service.js +3 -2
- package/dist/aun/service-proxy.js +18 -6
- package/dist/channels/aun.js +99 -23
- package/dist/channels/daemon.js +2 -2
- package/dist/cli/agent-create.js +19 -7
- package/dist/cli/aid-bind-command.js +3 -0
- package/dist/cli/aun-commands.js +19 -4
- package/dist/cli/code-stats.js +451 -213
- package/dist/cli/ctl-command.js +1 -3
- package/dist/cli/daemon-commands.js +42 -4
- package/dist/cli/directory.js +3 -1
- package/dist/cli/ecmsg-gateway.js +206 -0
- package/dist/cli/index.js +6 -0
- package/dist/cli/init.js +120 -13
- package/dist/cli/provider-command.js +82 -5
- package/dist/cli/restart-monitor.js +35 -24
- package/dist/cli/stats.js +8 -1
- package/dist/cli/task-context.js +41 -1
- package/dist/cli/watch-logs.js +6 -4
- package/dist/config/boot-log.js +16 -3
- package/dist/config/builtin-roles.js +2 -2
- package/dist/config/config-manager.js +5 -14
- package/dist/config/daemon-services.js +1 -0
- package/dist/config/peer-role-resolver.js +6 -6
- package/dist/config/provider-identity-migration.js +277 -0
- package/dist/config/provider-identity.js +37 -0
- package/dist/config/provider-ipc-service.js +40 -3
- package/dist/config/provider-local-sync.js +155 -0
- package/dist/config/providers-config.js +45 -4
- package/dist/config/providers-management.js +242 -76
- package/dist/config/providers.js +4 -1
- package/dist/config/role-service.js +8 -8
- package/dist/config/role-store.js +22 -22
- package/dist/config/schema-migration.js +143 -86
- package/dist/config/snapshot.js +70 -41
- package/dist/core/agent-runtime.js +48 -9
- package/dist/core/auth/auth-gateway.js +5 -3
- package/dist/core/auth/delegation-diagnostics.js +6 -2
- package/dist/core/auth/operation-authorizer.js +3 -1
- package/dist/core/auth/operation-catalog.js +6 -4
- package/dist/core/baseagent-loader.js +114 -11
- package/dist/core/channel-loader.js +36 -13
- package/dist/core/channel-runtime.js +112 -42
- package/dist/core/command/cli-intent-parser.js +2 -1
- package/dist/core/command/command-handler.js +16 -11
- package/dist/core/command/menu-handler.js +8 -3
- package/dist/core/command/menu-protocol.js +80 -7
- package/dist/core/command/provider-menu.js +4 -2
- package/dist/core/command/role-menu.js +29 -17
- package/dist/core/command/slash-handler.js +52 -16
- package/dist/core/daemon-control.js +42 -15
- package/dist/core/daemon-lifecycle.js +411 -67
- package/dist/core/handoff/dispatcher.js +9 -3
- package/dist/core/handoff/runtime.js +139 -45
- package/dist/core/handoff/store.js +190 -2
- package/dist/core/handoff/types.js +5 -0
- package/dist/core/initialization/service.js +7 -6
- package/dist/core/ipc-registration.js +5 -4
- package/dist/core/lifecycle-notice.js +10 -4
- package/dist/core/message/aun-ipc-sender.js +83 -5
- package/dist/core/message/message-admission.js +2 -0
- package/dist/core/message/message-bridge.js +53 -4
- package/dist/core/message/message-queue.js +46 -4
- package/dist/core/message/pending-hints.js +4 -10
- package/dist/core/message/response-engine.js +33 -25
- package/dist/core/message/send-options.js +40 -0
- package/dist/core/message/send-routing.js +320 -0
- package/dist/core/ownership/owner-resolver.js +112 -8
- package/dist/core/permission/ec-command-parser.js +159 -16
- package/dist/core/permission/readonly-shell-query.js +66 -7
- package/dist/core/permission/tool-error-code.js +13 -1
- package/dist/core/permission/tool-policy.js +74 -20
- package/dist/core/relation/same-owner-proof.js +131 -0
- package/dist/core/session/adapters/claude-session-file-adapter.js +24 -0
- package/dist/core/session/adapters/codex-session-file-adapter.js +80 -0
- package/dist/core/session/adapters/ecagent-session-file-adapter.js +22 -0
- package/dist/core/session/adapters/gemini-session-file-adapter.js +39 -0
- package/dist/core/session/session-fs-store.js +164 -3
- package/dist/core/session/session-idle-monitor.js +103 -17
- package/dist/core/session/session-manager.js +105 -18
- package/dist/core/thread/thread-route.js +5 -2
- package/dist/directory/owner-directory-admission.js +26 -1
- package/dist/directory/owner-directory-cache.js +739 -109
- package/dist/directory/owner-directory-markdown.js +146 -36
- package/dist/directory/owner-directory-query-operation.js +6 -3
- package/dist/directory/owner-directory-scope.js +12 -0
- package/dist/directory/owner-directory-search.js +5 -1
- package/dist/directory/owner-directory-store.js +28 -0
- package/dist/ecweb/basic-settings-service.js +15 -1
- package/dist/ecweb/message-context-service.js +2 -2
- package/dist/ecweb/providers-service.js +3 -1
- package/dist/ecweb/runtime/lifecycle.js +26 -15
- package/dist/ecweb/runtime/supervisor.js +120 -51
- package/dist/index.js +551 -250
- package/dist/paths.js +2 -1
- package/dist/proxy/manager.js +40 -17
- package/dist/proxy/service-supervisor.js +67 -33
- package/dist/response-system/engines/v1/proactive-flow.js +4 -0
- package/dist/trigger/application-service.js +4 -1
- package/dist/trigger/audit.js +19 -4
- package/dist/trigger/authorization.js +14 -5
- package/dist/trigger/event-source.js +4 -0
- package/dist/trigger/feedback.js +26 -2
- package/dist/trigger/history.js +176 -7
- package/dist/trigger/manager.js +1 -3
- package/dist/trigger/scheduler.js +81 -8
- package/dist/trigger/system-definitions.js +2 -2
- package/dist/trigger/validation.js +4 -0
- package/dist/utils/autostart.js +2 -2
- package/dist/utils/cross-platform.js +188 -12
- package/dist/utils/host-info.js +11 -8
- package/dist/utils/npm-ops.js +48 -0
- package/dist/utils/process-introspect.js +64 -1
- package/dist/utils/public-suffix.js +5811 -0
- package/dist/utils/startup-trace.js +160 -19
- package/dist/utils/windows-autostart.js +41 -7
- package/ecagent/dist/harness/env/nodejs.js +46 -2
- package/kits/docs/aun/ABILITY_CALLING.md +6 -6
- package/kits/docs/channels/aun.md +1 -1
- package/kits/docs/channels/feishu.md +2 -2
- package/kits/docs/context-assembly.md +20 -4
- package/kits/docs/evolcore/INDEX.md +5 -3
- package/kits/docs/evolcore/agent.md +2 -2
- package/kits/docs/evolcore/aid.md +8 -2
- package/kits/docs/evolcore/ctl.md +15 -5
- package/kits/docs/evolcore/directory.md +48 -15
- package/kits/docs/evolcore/group.md +8 -8
- package/kits/docs/evolcore/memory-consolidation.md +1 -1
- package/kits/docs/evolcore/menu-exec-schema-commands.md +1 -1
- package/kits/docs/evolcore/menu-protocol.md +32 -3
- package/kits/docs/evolcore/msg.md +38 -30
- package/kits/docs/evolcore/provider.md +57 -5
- package/kits/docs/evolcore/response.md +2 -2
- package/kits/docs/evolcore/thread-chain.md +11 -13
- package/kits/docs/evolcore/tool-authorization.md +17 -0
- package/kits/docs/evolcore/trigger.md +2 -2
- package/kits/docs/identity/ROLE_DETAIL.md +1 -1
- package/kits/docs/prompt-loading-architecture.md +30 -20
- package/kits/eck_manifest.auxiliary.json +5 -5
- package/kits/eck_manifest.json +44 -32
- package/kits/eck_manifest.memory.json +18 -18
- package/kits/eck_message_manifest.json +3 -3
- package/kits/rules/01-overview.md +1 -1
- package/kits/rules/06-channel.md +7 -8
- package/kits/schemas/_meta.json +7 -1
- package/kits/schemas/daemon.schema.1.json +5 -0
- package/kits/schemas/daemon.schema.2.json +5 -0
- package/kits/schemas/daemon.schema.3.json +2 -1
- package/kits/schemas/daemon.schema.4.json +2 -1
- package/kits/schemas/daemon.schema.5.json +2 -1
- package/kits/schemas/daemon.schema.6.json +1 -1
- package/kits/schemas/daemon.schema.7.json +1 -1
- package/kits/schemas/daemon.schema.8.json +2 -1
- package/kits/schemas/daemon.schema.9.json +2 -1
- package/kits/schemas/providers.schema.2.json +74 -0
- package/kits/schemas/role-registry.schema.1.json +2 -2
- package/kits/schemas/single-session.schema.1.json +1 -1
- package/kits/schemas/single-session.schema.2.json +1 -1
- package/kits/schemas/single-session.schema.3.json +1 -1
- package/kits/skills/aid-ability/SKILL.md +7 -7
- package/kits/skills/ec-agent-create/SKILL.md +1 -0
- package/kits/skills/ec-group/SKILL.md +8 -5
- package/kits/skills/ec-group-collaboration/SKILL.md +7 -7
- package/kits/skills/ec-service-proxy/SKILL.md +3 -10
- package/kits/skills/ec-trigger/SKILL.md +9 -9
- package/kits/skills/ec-trigger/references/examples.md +1 -1
- package/kits/skills/thread-ops/SKILL.md +10 -10
- package/kits/templates/system-fragments/channel.md +5 -5
- package/kits/templates/system-fragments/commands.md +7 -6
- package/kits/templates/system-fragments/directory-contacts.md +10 -3
- package/kits/templates/system-fragments/personal-assistant.md +22 -3
- package/kits/templates/system-fragments/reply.md +48 -28
- package/kits/templates/system-fragments/session.md +3 -10
- package/kits/templates/system-fragments/thread.md +5 -5
- package/package.json +4 -4
|
@@ -45,7 +45,15 @@ export const OWNER_DIRECTORY_APPLY_WAIT_MS = 300;
|
|
|
45
45
|
* 一次网络抖动就让名录停在旧版本直到 daemon 重启,App 那边却以为已经同步成功。
|
|
46
46
|
* 退避按时间而不是按次数,既不会造成拉取风暴,也不会永久放弃。
|
|
47
47
|
*/
|
|
48
|
-
export const OWNER_DIRECTORY_PULL_BACKOFF_MS = [30_000, 120_000,
|
|
48
|
+
export const OWNER_DIRECTORY_PULL_BACKOFF_MS = [15_000, 30_000, 60_000, 120_000, 240_000, 480_000, 960_000, 1_800_000, 3_600_000];
|
|
49
|
+
/**
|
|
50
|
+
* 拉取前的防抖窗口。
|
|
51
|
+
*
|
|
52
|
+
* 只在**真正拿锁的那个 agent** 身上生效:Owner 侧一次改动会逐个通知所有 agent,
|
|
53
|
+
* 通知之间有几百毫秒到几秒的间隔。先到的那一个等满这个窗口再发请求,就能把
|
|
54
|
+
* 同一批通知合并成一次网络请求;后到的直接命中同一个 in-flight Promise,不等待。
|
|
55
|
+
*/
|
|
56
|
+
export const OWNER_DIRECTORY_PULL_DEBOUNCE_MS = 15_000;
|
|
49
57
|
/** 共享缓存下保存"最近一次拉到但还没通过校验"的原始字节的子目录。 */
|
|
50
58
|
const RAW_DIR = 'raw';
|
|
51
59
|
/** 拉取失败记录文件名,与共享缓存同目录。 */
|
|
@@ -65,6 +73,38 @@ const LANDING_MAX_BYTES = 1024 * 1024;
|
|
|
65
73
|
const LANDING_DIFF_MAX_ENTRIES = 20;
|
|
66
74
|
/** 连续失败到这个次数就把 WARN 升级成 ERROR,避免"没人看见的一行 WARN"。 */
|
|
67
75
|
const FAILURE_ESCALATE_AFTER = 3;
|
|
76
|
+
/**
|
|
77
|
+
* agent 级动作日志:`<agent 目录>/directory/actions.jsonl`。
|
|
78
|
+
*
|
|
79
|
+
* 共享区的 `landing.jsonl` 只回答"远端内容什么时候到了共享区",回答不了
|
|
80
|
+
* "**这个** agent 收到通知后做了什么"。两者分开记,因为一次 owner 级更新会
|
|
81
|
+
* 触发 N 个 agent 各自不同的动作(落地/跳过/失败/等退避),共享区那一份
|
|
82
|
+
* 记不下也看不清。
|
|
83
|
+
*
|
|
84
|
+
* 仍然只是诊断设施:写失败只 warn,绝不影响同步。
|
|
85
|
+
*/
|
|
86
|
+
const ACTIONS_FILE = 'actions.jsonl';
|
|
87
|
+
const ACTIONS_KEEP_LINES = 500;
|
|
88
|
+
const ACTIONS_MAX_BYTES = 512 * 1024;
|
|
89
|
+
/**
|
|
90
|
+
* 落地镜像元数据:`<agent 目录>/directory/<scope>.meta.json`。
|
|
91
|
+
*
|
|
92
|
+
* 只回答一件事:**本 agent 落地的是共享区的哪一版**。内容是共享区那份文件的
|
|
93
|
+
* 时间戳、大小和摘要;它自己的 mtime 也设成共享区的 mtime。
|
|
94
|
+
*
|
|
95
|
+
* 热路径(每条要进模型的消息)只做两次 `stat` 比较:
|
|
96
|
+
*
|
|
97
|
+
* stat(共享区/contacts.json).mtimeMs vs stat(agent 区/contacts.meta.json).mtimeMs
|
|
98
|
+
*
|
|
99
|
+
* 不等就拷一次,然后重写这个元数据文件并重算 directory.md。
|
|
100
|
+
*
|
|
101
|
+
* 为什么用独立元数据文件而不是直接比 `contacts.json`:两个文件的**格式不同**
|
|
102
|
+
* ——共享区存远端原始字节,agent 区存重新序列化的信封(`{schemaVersion, ...}`),
|
|
103
|
+
* 长度天然不等,大小没法直接比;而 agent 区那个文件的 mtime 也不该单独挪动,
|
|
104
|
+
* 它会作为快照自身的时间被别人读。用一个只承载"我同步到哪一版"的文件来镜像,
|
|
105
|
+
* 两个语义都干净。
|
|
106
|
+
*/
|
|
107
|
+
const SYNC_STAMP_SUFFIX = '.meta.json';
|
|
68
108
|
const MAX_DIRECTORY_ENTRIES = 100_000;
|
|
69
109
|
const SNAPSHOT_ID_RE = /^[A-Za-z0-9_-]{1,128}$/;
|
|
70
110
|
const SHA256_RE = /^[a-f0-9]{64}$/;
|
|
@@ -78,8 +118,119 @@ export function parseOwnerDirectoryGrantAbilityResponse(value) {
|
|
|
78
118
|
return undefined;
|
|
79
119
|
return value.result;
|
|
80
120
|
}
|
|
81
|
-
/**
|
|
82
|
-
|
|
121
|
+
/**
|
|
122
|
+
* 共享缓存的唯一维护者,**一个 owner 一个**。
|
|
123
|
+
*
|
|
124
|
+
* 拉取不再是「谁先到谁拉」的 agent 行为:各 agent 收到 `owner.directory.changed`
|
|
125
|
+
* 只向这里报告,由协调者统一去重、防抖、退避、重试。理由是 agent 是短命的
|
|
126
|
+
* (可能被停止/重建),而「共享区始终维护到最新」是 daemon 级的持久责任——
|
|
127
|
+
* 把重试状态挂在某个 agent 实例上,那个 agent 一停,整条同步链就断了。
|
|
128
|
+
*
|
|
129
|
+
* 协调者只做**网络与共享区**这一层;从共享区到各 agent 目录的落地仍由各
|
|
130
|
+
* agent 自己做(本地写,互不影响)。
|
|
131
|
+
*
|
|
132
|
+
* 方案 3:协调者自己不做网络请求,而是挑一个当前持有有效授权的 agent 作为
|
|
133
|
+
* 执行者代为发起(授权凭证是 per-agent 的,协调者没有身份)。
|
|
134
|
+
*/
|
|
135
|
+
class OwnerDirectoryCoordinator {
|
|
136
|
+
ownerAid;
|
|
137
|
+
/** 每个 owner 下活着的 cache 实例,既是广播对象也是执行者候选池。 */
|
|
138
|
+
caches = new Set();
|
|
139
|
+
/** 进行中的拉取。协调者级单飞:一个 owner 同时最多一次网络请求。 */
|
|
140
|
+
pulling;
|
|
141
|
+
/** 最近一次收到的通知。拉取成功后拿它判断是否已经收敛。 */
|
|
142
|
+
latestNotice;
|
|
143
|
+
/** 共享区当前代号;`undefined` 表示本进程还没成功落过盘。 */
|
|
144
|
+
epoch;
|
|
145
|
+
/** 连续失败次数与时间点,决定下一档退避。挂在协调者上,不随 agent 生死。 */
|
|
146
|
+
failures = 0;
|
|
147
|
+
lastFailureAtMs;
|
|
148
|
+
/** 当前这一轮拉取的序号。每发起一次协调拉取自增,用于失败去重。 */
|
|
149
|
+
pullGeneration = 0;
|
|
150
|
+
/** 已经计过失败的那一轮序号;同一轮无论几个 agent 报告都只记一次。 */
|
|
151
|
+
countedFailureGeneration;
|
|
152
|
+
constructor(ownerAid) {
|
|
153
|
+
this.ownerAid = ownerAid;
|
|
154
|
+
}
|
|
155
|
+
/**
|
|
156
|
+
* 记一次拉取失败,**按轮次去重**。
|
|
157
|
+
*
|
|
158
|
+
* 同一轮拉取的 Promise 被 N 个 agent 共用,失败时 N 个调用方都会进 catch。
|
|
159
|
+
* 若各自把 `failures` 加一,一次网络故障就把退避推到封顶(实测 13 个 agent
|
|
160
|
+
* 在 3 毫秒内把 15 秒档推到 1 小时)。失败是**协调者这一轮**的属性,不是
|
|
161
|
+
* 每个观测者的属性,所以只认第一次报告。
|
|
162
|
+
*/
|
|
163
|
+
recordFailure(now, generation) {
|
|
164
|
+
if (generation === this.countedFailureGeneration)
|
|
165
|
+
return;
|
|
166
|
+
this.countedFailureGeneration = generation;
|
|
167
|
+
this.failures++;
|
|
168
|
+
this.lastFailureAtMs = now;
|
|
169
|
+
}
|
|
170
|
+
/** 距离退避窗口结束还有多久;0 表示可以立刻拉取。 */
|
|
171
|
+
backoffRemainingMs(now) {
|
|
172
|
+
if (this.failures === 0 || this.lastFailureAtMs === undefined)
|
|
173
|
+
return 0;
|
|
174
|
+
const window = OWNER_DIRECTORY_PULL_BACKOFF_MS[Math.min(this.failures, OWNER_DIRECTORY_PULL_BACKOFF_MS.length) - 1];
|
|
175
|
+
return Math.max(0, this.lastFailureAtMs + window - now);
|
|
176
|
+
}
|
|
177
|
+
clearFailures() {
|
|
178
|
+
this.failures = 0;
|
|
179
|
+
this.lastFailureAtMs = undefined;
|
|
180
|
+
this.countedFailureGeneration = undefined;
|
|
181
|
+
}
|
|
182
|
+
/**
|
|
183
|
+
* 挑一个当前可用的执行者。
|
|
184
|
+
*
|
|
185
|
+
* 判据是「实例还活着 + 它有授权」——授权凭证是 per-agent 的,协调者没有
|
|
186
|
+
* 自己的身份,所以必须借一个。优先选最近活跃的,避免挑到一个正在停止中的。
|
|
187
|
+
* 被远端拒过的实例排在后面:它多半还会被拒,但全员都被拒时仍要有人去试,
|
|
188
|
+
* 否则名录永远不更新。被拒只影响这里,不影响任何一方能不能用名录。
|
|
189
|
+
*/
|
|
190
|
+
pickExecutor(preferred) {
|
|
191
|
+
// 优先用发起方自己:它一定想拿到这份数据,结果也直接回给它。
|
|
192
|
+
// 只有它自身不可用(授权失效/已退役/刚被远端拒过)时才退到别人,避免出现
|
|
193
|
+
// 「A 触发、却被 B 的代拉结果覆盖」这种观测不到的错配。
|
|
194
|
+
const usable = (cache) => !cache.isRetired() && cache.scopeAccessUnavailableReason() === undefined;
|
|
195
|
+
if (preferred && usable(preferred) && !preferred.pullBlocked())
|
|
196
|
+
return preferred;
|
|
197
|
+
for (const cache of this.caches) {
|
|
198
|
+
if (cache.isRetired()) {
|
|
199
|
+
this.caches.delete(cache);
|
|
200
|
+
continue;
|
|
201
|
+
}
|
|
202
|
+
if (usable(cache) && !cache.pullBlocked())
|
|
203
|
+
return cache;
|
|
204
|
+
}
|
|
205
|
+
if (preferred && usable(preferred))
|
|
206
|
+
return preferred;
|
|
207
|
+
for (const cache of this.caches) {
|
|
208
|
+
if (usable(cache))
|
|
209
|
+
return cache;
|
|
210
|
+
}
|
|
211
|
+
return undefined;
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
/** daemon 级协调者表:ownerAid → 协调者。 */
|
|
215
|
+
const _coordinators = new Map();
|
|
216
|
+
function coordinatorFor(ownerAid) {
|
|
217
|
+
let found = _coordinators.get(ownerAid);
|
|
218
|
+
if (!found) {
|
|
219
|
+
found = new OwnerDirectoryCoordinator(ownerAid);
|
|
220
|
+
_coordinators.set(ownerAid, found);
|
|
221
|
+
}
|
|
222
|
+
return found;
|
|
223
|
+
}
|
|
224
|
+
/**
|
|
225
|
+
* 测试专用:清空协调者表。
|
|
226
|
+
*
|
|
227
|
+
* 协调者按 ownerAid 常驻模块内存,失败计数和代号都会跨用例泄漏——同一个
|
|
228
|
+
* ownerAid 下前一个用例制造的退避会让后面的用例直接不发请求。生产环境没有
|
|
229
|
+
* 清空需求(进程重启就是清空)。
|
|
230
|
+
*/
|
|
231
|
+
export function _resetOwnerDirectoryCoordinators() {
|
|
232
|
+
_coordinators.clear();
|
|
233
|
+
}
|
|
83
234
|
/**
|
|
84
235
|
* 本进程已做过"启动时无快照检查"的 agent 目录。
|
|
85
236
|
*
|
|
@@ -110,10 +261,14 @@ export class OwnerDirectoryCache {
|
|
|
110
261
|
* "多轮消息同时撞上同一份新共享缓存"的窗口。
|
|
111
262
|
*/
|
|
112
263
|
applyInFlight;
|
|
113
|
-
/**
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
264
|
+
/**
|
|
265
|
+
* 本实例已落地到共享缓存的哪一代。
|
|
266
|
+
*
|
|
267
|
+
* 与协调者的 `epoch` 比对构成入站消息热路径的全部判断——两边都是内存里的
|
|
268
|
+
* 字符串/数字,不碰磁盘。`undefined` 表示还没有对过账(刚启动或刚重建实例),
|
|
269
|
+
* 这时读一次盘确认,之后就只比内存。
|
|
270
|
+
*/
|
|
271
|
+
appliedGeneration;
|
|
117
272
|
stateWriteTail = Promise.resolve();
|
|
118
273
|
/**
|
|
119
274
|
* 本 agent 快照的进程内缓存,`null` 表示"读过了,磁盘上没有可用快照"。
|
|
@@ -134,6 +289,110 @@ export class OwnerDirectoryCache {
|
|
|
134
289
|
throw new TypeError('owner directory cache path must be absolute');
|
|
135
290
|
}
|
|
136
291
|
this.now = options.now ?? Date.now;
|
|
292
|
+
// 登记进 owner 的协调者:既是广播对象,也是「谁来代为拉取」的候选池。
|
|
293
|
+
// 实例被回收(isCurrent 为假)时在广播/挑执行者时顺手剔除,不需要显式注销。
|
|
294
|
+
coordinatorFor(options.ownerAid).caches.add(this);
|
|
295
|
+
}
|
|
296
|
+
/**
|
|
297
|
+
* 共享缓存是否已经比本实例落地的那一代更新。
|
|
298
|
+
*
|
|
299
|
+
* **纯内存,不读盘**:这是入站消息热路径上的唯一判断。代号只在
|
|
300
|
+
* `_writeSharedCache` 成功后推进,所以「共享区变了但还没被本实例落地」
|
|
301
|
+
* 一定能被看到,「共享区没变」则一定不触发 IO。
|
|
302
|
+
*/
|
|
303
|
+
/** 实例所属的 agent AID;协调者调度和日志用它,不必暴露整个 options。 */
|
|
304
|
+
get selfAid() { return this.options.selfAid; }
|
|
305
|
+
/** 运行时是否已经退役(连接断开、关系重建、daemon 停机中)。 */
|
|
306
|
+
isRetired() { return this.options.isCurrent?.() === false; }
|
|
307
|
+
hasPendingSharedUpdate() {
|
|
308
|
+
for (const scope of OWNER_DIRECTORY_SCOPES) {
|
|
309
|
+
if (this._sharedNewerThanLocal(scope))
|
|
310
|
+
return true;
|
|
311
|
+
}
|
|
312
|
+
return false;
|
|
313
|
+
}
|
|
314
|
+
/**
|
|
315
|
+
* 共享区那一版是否还没被本 agent 落地。
|
|
316
|
+
*
|
|
317
|
+
* 判据是**两个文件的 mtime**:共享区的 `<scope>.json` 与 agent 区的
|
|
318
|
+
* `<scope>.meta.json`(落地镜像)。后者每次落地时被回拨成前者的 mtime,
|
|
319
|
+
* 所以「同步过」= 两个时间戳相等。
|
|
320
|
+
*
|
|
321
|
+
* - 每次调用只有两次 `stat`,够便宜到每条消息都做。
|
|
322
|
+
* - 不解析 31KB JSON、不算摘要、不看内存代号。
|
|
323
|
+
* - 进程重启、内存状态丢失、协调者被清空都不影响判断——磁盘就是事实。
|
|
324
|
+
*
|
|
325
|
+
* 元数据缺失(老版本升上来、或从未落地过)算「需要落地」,这一次会把
|
|
326
|
+
* 元数据补出来,之后就走纯 stat 的比较。
|
|
327
|
+
*/
|
|
328
|
+
_sharedNewerThanLocal(scope) {
|
|
329
|
+
let shared;
|
|
330
|
+
try {
|
|
331
|
+
shared = fs.statSync(this._sharedCachePath(scope));
|
|
332
|
+
}
|
|
333
|
+
// 共享区还没有内容:没东西可拷。返回 false 让调用方走「立刻返回」那条路,
|
|
334
|
+
// 而不是退回网络——这里的职责只是本机拷贝,拉取是另一回事。
|
|
335
|
+
catch {
|
|
336
|
+
return false;
|
|
337
|
+
}
|
|
338
|
+
let stamp;
|
|
339
|
+
try {
|
|
340
|
+
stamp = fs.statSync(this.syncStampPath(scope));
|
|
341
|
+
}
|
|
342
|
+
catch {
|
|
343
|
+
return true;
|
|
344
|
+
} // 还没落地过(或元数据丢了),拷一次补出来
|
|
345
|
+
return shared.mtimeMs !== stamp.mtimeMs;
|
|
346
|
+
}
|
|
347
|
+
/** agent 区的落地镜像元数据路径。 */
|
|
348
|
+
syncStampPath(scope) {
|
|
349
|
+
return path.join(this.options.directory, `${scope}${SYNC_STAMP_SUFFIX}`);
|
|
350
|
+
}
|
|
351
|
+
/**
|
|
352
|
+
* 写落地镜像元数据,并把它的 mtime 回拨成共享区的 mtime。
|
|
353
|
+
*
|
|
354
|
+
* 内容是给人排查用的(共享区那一版的时间戳、大小、摘要、落地时刻);
|
|
355
|
+
* **mtime 是给热路径用的**——下一次进来只 stat 它,不读内容。
|
|
356
|
+
*/
|
|
357
|
+
_writeSyncStamp(scope, file) {
|
|
358
|
+
const sharedPath = this._sharedCachePath(scope);
|
|
359
|
+
// 时间戳与大小一律从 `stat` 拿,不依赖解析结果:热路径比较的正是文件系统上
|
|
360
|
+
// 的真实 mtime,而这里经常在手头没有解析产物的场合被调用(摘要命中的短路
|
|
361
|
+
// 分支),重新解析 31KB 会把热路径的开销又加回来。
|
|
362
|
+
let sharedStat;
|
|
363
|
+
try {
|
|
364
|
+
sharedStat = fs.statSync(sharedPath);
|
|
365
|
+
}
|
|
366
|
+
catch {
|
|
367
|
+
return;
|
|
368
|
+
} // 共享区没有文件就没什么可镜像
|
|
369
|
+
// 摘要与 sha256 是给人看的:有解析产物就用它的,没有就用本地快照已有的值
|
|
370
|
+
// (走到这里说明两边内容一致,取哪个都是同一个)。
|
|
371
|
+
const contentDigest = file?.contentDigest ?? this.readCachedSnapshot(scope)?.contentDigest;
|
|
372
|
+
try {
|
|
373
|
+
atomicWriteJson(this.syncStampPath(scope), {
|
|
374
|
+
schemaVersion: 1,
|
|
375
|
+
ownerAid: this.options.ownerAid,
|
|
376
|
+
granteeAid: this.options.selfAid,
|
|
377
|
+
scope,
|
|
378
|
+
/** 共享区那一版的文件时间戳:热路径比较的就是它的 mtime。 */
|
|
379
|
+
sharedMtimeMs: sharedStat.mtimeMs,
|
|
380
|
+
/** 共享区文件字节数。 */
|
|
381
|
+
sharedBytes: sharedStat.size,
|
|
382
|
+
...(contentDigest ? { contentDigest } : {}),
|
|
383
|
+
...(file?.sha256 ? { sha256: file.sha256 } : {}),
|
|
384
|
+
...(file?.updatedAtMs !== undefined ? { updatedAtMs: file.updatedAtMs } : {}),
|
|
385
|
+
/** 共享区文档声明的更新时间(有解析产物时才有)。 */
|
|
386
|
+
...(file?.storageUpdatedAtMs !== undefined ? { storageUpdatedAtMs: file.storageUpdatedAtMs } : {}),
|
|
387
|
+
/** 本机落地时刻,与 mtime 语义分开。 */
|
|
388
|
+
appliedAtMs: this.now(),
|
|
389
|
+
});
|
|
390
|
+
// 必须在写盘之后设:写入本身会把 mtime 刷成本机当前时刻。
|
|
391
|
+
applyStorageMtime(this.syncStampPath(scope), sharedStat.mtimeMs);
|
|
392
|
+
}
|
|
393
|
+
catch (error) {
|
|
394
|
+
logger.warn(`[OwnerDirectoryCache] Failed to write sync stamp for ${this.options.selfAid}/${scope}: ${errorText(error)}`);
|
|
395
|
+
}
|
|
137
396
|
}
|
|
138
397
|
// ── Authorization ───────────────────────────────────────────────────────────
|
|
139
398
|
/** A retired runtime must never mutate the current relation's shared files. */
|
|
@@ -210,13 +469,22 @@ export class OwnerDirectoryCache {
|
|
|
210
469
|
fs.chmodSync(this.authorizationPath(), 0o600);
|
|
211
470
|
this.clearPullFailures();
|
|
212
471
|
logger.info(`[OwnerDirectoryCache] Authorization saved: status=${result.status} expiresAt=${grant.expiresAt}`);
|
|
472
|
+
this._logAction({
|
|
473
|
+
action: 'auth',
|
|
474
|
+
generation: this.appliedGeneration,
|
|
475
|
+
detail: `授权保存 status=${result.status} expiresAt=${grant.expiresAt}`,
|
|
476
|
+
});
|
|
213
477
|
void this.updateSyncState(state => ({ ...state, blockedScopes: [] })).then(() => this.refreshMarkdown());
|
|
214
478
|
}
|
|
215
479
|
/**
|
|
216
|
-
*
|
|
480
|
+
* 是否具备使用名录的条件。
|
|
217
481
|
*
|
|
218
|
-
*
|
|
219
|
-
*
|
|
482
|
+
* 只看授权本身:有没有授权、有没有过期。**不按 scope 区分**——名录只有一份文件,
|
|
483
|
+
* 能不能读由 storage 的 ACL 决定,不在这里重复判一次。
|
|
484
|
+
*
|
|
485
|
+
* **拉取被拒不算不可用**:某次拉取被远端拒绝只影响「谁来拉」(见 `pullBlocked`),
|
|
486
|
+
* 不影响准入、查询和本地读取。数据由 owner 级共享缓存供给,别的 agent 拉到一样能用;
|
|
487
|
+
* 把自己锁死既救不了远端权限,也只会让本 agent 既发不出也收不进。
|
|
220
488
|
*/
|
|
221
489
|
scopeAccess(scope) {
|
|
222
490
|
return this.scopeAccessUnavailableReason(scope) ? 'unavailable' : 'authorized';
|
|
@@ -227,10 +495,17 @@ export class OwnerDirectoryCache {
|
|
|
227
495
|
return 'authorization_missing_or_invalid';
|
|
228
496
|
if (grant.expiresAt > 0 && grant.expiresAt <= this.now())
|
|
229
497
|
return 'authorization_expired';
|
|
230
|
-
if (this.readSyncState().blockedScopes.length > 0)
|
|
231
|
-
return 'scope_blocked';
|
|
232
498
|
return undefined;
|
|
233
499
|
}
|
|
500
|
+
/**
|
|
501
|
+
* 本实例最近一次拉取是否被远端拒绝过。
|
|
502
|
+
*
|
|
503
|
+
* 只用于挑选执行者:被拒过一次的实例大概率还会被拒,优先让别人去拉。
|
|
504
|
+
* 它不是准入判据,也不删本地快照——拉不动不等于用不了。
|
|
505
|
+
*/
|
|
506
|
+
pullBlocked(scope = OWNER_DIRECTORY_SCOPES[0]) {
|
|
507
|
+
return this.readSyncState().blockedScopes.includes(scope);
|
|
508
|
+
}
|
|
234
509
|
// ── Pending update ──────────────────────────────────────────────────────────
|
|
235
510
|
writePendingUpdate(update) {
|
|
236
511
|
if (!this.canMutate())
|
|
@@ -256,23 +531,51 @@ export class OwnerDirectoryCache {
|
|
|
256
531
|
catch { /* ignore */ }
|
|
257
532
|
}
|
|
258
533
|
// ── handleOwnerChangedEvent ────────────────────────────────────────────────
|
|
259
|
-
async handleOwnerChangedEvent(senderAid, digests) {
|
|
534
|
+
async handleOwnerChangedEvent(senderAid, digests, published) {
|
|
260
535
|
if (senderAid !== this.options.ownerAid) {
|
|
261
536
|
throw new OwnerDirectoryCacheError('UNTRUSTED_OWNER_EVENT', `directory change sender is not the authorized Owner: sender=${senderAid} expected=${this.options.ownerAid}`);
|
|
262
537
|
}
|
|
263
|
-
//
|
|
264
|
-
//
|
|
265
|
-
|
|
538
|
+
// 报到协调者:这是「agent 只负责报告、daemon 统一维护共享区」的入口。
|
|
539
|
+
// 协调者保留**最新的**一份通知;拉取成功的判据是「拉到的东西不比它旧」,
|
|
540
|
+
// 而不是「追上它声明的那个 digest」——声明值可能已经是过期快照。
|
|
541
|
+
const coordinator = coordinatorFor(this.options.ownerAid);
|
|
542
|
+
const contentDigest = digests?.[OWNER_DIRECTORY_SCOPES[0]];
|
|
543
|
+
if (published?.updatedAtMs !== undefined && coordinator.latestNotice?.updatedAtMs !== undefined
|
|
544
|
+
&& published.updatedAtMs < coordinator.latestNotice.updatedAtMs) {
|
|
545
|
+
// 乱序到达的旧通知:不覆盖更新的那条,也不触发新一轮拉取。
|
|
546
|
+
logger.debug(`[OwnerDirectoryCache] Stale change notice ignored for ${this.options.ownerAid}`
|
|
547
|
+
+ ` (${published.updatedAtMs} < ${coordinator.latestNotice.updatedAtMs})`);
|
|
548
|
+
return;
|
|
549
|
+
}
|
|
550
|
+
coordinator.latestNotice = {
|
|
551
|
+
operationId: `notification-${randomUUID()}`,
|
|
552
|
+
...(published?.updatedAtMs !== undefined ? { updatedAtMs: published.updatedAtMs } : {}),
|
|
553
|
+
...(contentDigest ? { contentDigest } : {}),
|
|
554
|
+
receivedAtMs: this.now(),
|
|
555
|
+
};
|
|
556
|
+
// 新通知意味着新的变更意图:退避归零,立刻给一次机会。
|
|
557
|
+
coordinator.clearFailures();
|
|
266
558
|
this.writePendingUpdate({
|
|
267
559
|
ownerAid: this.options.ownerAid,
|
|
268
|
-
operationId:
|
|
560
|
+
operationId: coordinator.latestNotice.operationId,
|
|
269
561
|
digests: digests ?? {},
|
|
270
562
|
markedAtMs: this.now(),
|
|
271
563
|
});
|
|
272
564
|
const hint = Object.entries(digests ?? {})
|
|
273
565
|
.map(([scope, digest]) => `${scope}=${String(digest).slice(0, 8)}`).join(',');
|
|
274
566
|
logger.info(`[OwnerDirectoryCache] Pending written for ${this.options.ownerAid}`
|
|
275
|
-
+ ` sender=${senderAid} digests=${hint || 'none'}`
|
|
567
|
+
+ ` sender=${senderAid} digests=${hint || 'none'}`
|
|
568
|
+
+ (published?.updatedAtMs !== undefined ? ` updatedAtMs=${published.updatedAtMs}` : ''));
|
|
569
|
+
this._logAction({
|
|
570
|
+
action: 'notice',
|
|
571
|
+
trigger: 'notification',
|
|
572
|
+
operationId: coordinator.latestNotice.operationId,
|
|
573
|
+
digest: contentDigest?.slice(0, 16),
|
|
574
|
+
generation: this.appliedGeneration,
|
|
575
|
+
sharedGeneration: coordinator.epoch?.generation,
|
|
576
|
+
detail: `通知到达 sender=${senderAid}`
|
|
577
|
+
+ (published?.updatedAtMs !== undefined ? ` updatedAtMs=${published.updatedAtMs}` : ''),
|
|
578
|
+
});
|
|
276
579
|
}
|
|
277
580
|
/** 授权被明确撤销或拒绝时,清掉本地快照,避免继续拿旧数据做准入判断。 */
|
|
278
581
|
revokeAuthorization(status) {
|
|
@@ -281,8 +584,20 @@ export class OwnerDirectoryCache {
|
|
|
281
584
|
this.clearPullFailures();
|
|
282
585
|
atomicRemove(this.authorizationPath());
|
|
283
586
|
atomicRemove(this.cachePath(OWNER_DIRECTORY_SCOPES[0]));
|
|
587
|
+
// 落地镜像一并删掉:留着它会让"已同步到共享区某一版"的结论在授权恢复后
|
|
588
|
+
// 依然成立,热路径据此跳过本该重做的一次落地。
|
|
589
|
+
for (const scope of OWNER_DIRECTORY_SCOPES)
|
|
590
|
+
atomicRemove(this.syncStampPath(scope));
|
|
284
591
|
this.snapshotCache.clear();
|
|
285
592
|
logger.info(`[OwnerDirectoryCache] Authorization revoked (${status}) for ${this.options.ownerAid}`);
|
|
593
|
+
// 快照已删,本地就不再对应任何一代共享内容。不清掉这个标记,
|
|
594
|
+
// 热路径会以为"已经落地到当前代号",从此再也不去读共享区。
|
|
595
|
+
this.appliedGeneration = undefined;
|
|
596
|
+
this._logAction({
|
|
597
|
+
action: 'auth',
|
|
598
|
+
generation: undefined,
|
|
599
|
+
detail: `授权撤销 status=${status},已删除本地快照`,
|
|
600
|
+
});
|
|
286
601
|
this.refreshMarkdown();
|
|
287
602
|
}
|
|
288
603
|
/**
|
|
@@ -354,29 +669,58 @@ export class OwnerDirectoryCache {
|
|
|
354
669
|
if (!pending)
|
|
355
670
|
return;
|
|
356
671
|
// 退避窗口内不发请求:pending 留着,到点自动重试。见 OWNER_DIRECTORY_PULL_BACKOFF_MS。
|
|
357
|
-
|
|
672
|
+
// 读数来自协调者而不是本实例:退避是 owner 级状态,换一个 agent 来触发也要
|
|
673
|
+
// 看到同一份进度,否则「一直失败的那个恰好被停了」会让重试从头开始。
|
|
674
|
+
const coordinator = coordinatorFor(this.options.ownerAid);
|
|
675
|
+
const wait = coordinator.backoffRemainingMs(this.now());
|
|
358
676
|
if (wait > 0) {
|
|
359
|
-
logger.debug(`[OwnerDirectoryCache] Pull backed off for ${wait}ms (${
|
|
677
|
+
logger.debug(`[OwnerDirectoryCache] Pull backed off for ${wait}ms (${coordinator.failures} failures) for ${this.options.ownerAid}`);
|
|
360
678
|
return;
|
|
361
679
|
}
|
|
362
|
-
logger.info(`[OwnerDirectoryCache] Processing pending for ${this.options.ownerAid}, op=${pending.operationId}, attempt=${
|
|
680
|
+
logger.info(`[OwnerDirectoryCache] Processing pending for ${this.options.ownerAid}, op=${pending.operationId}, attempt=${coordinator.failures + 1}`);
|
|
363
681
|
try {
|
|
364
682
|
const fetched = await this.fetchToSharedCache(pending.digests,
|
|
365
683
|
// 调用方知道得更准(grant 之后的首次拉取、启动补拉);否则按标记来源推断。
|
|
366
684
|
trigger ?? (pending.operationId.startsWith('notification-') ? 'notification' : 'startup'), pending.operationId);
|
|
367
685
|
if (this.options.relationVersion !== undefined)
|
|
368
686
|
await this._applyAll(fetched);
|
|
369
|
-
|
|
687
|
+
// 收敛判据:拉到的这一版不比最近一条通知旧,就算追平了。
|
|
688
|
+
// 不用「digest 必须等于通知声明的值」——声明值可能已经是过期快照,
|
|
689
|
+
// 那样会一直追不上;这里只要求「不落后于最新一次通知」。
|
|
690
|
+
// 判据必须在 clearFailures 之前:拉到了旧版本属于「还没收敛」,要保留
|
|
691
|
+
// 失败计数让退避继续升级,否则每轮都被清零、永远停在最短档。
|
|
692
|
+
const latest = coordinator.latestNotice;
|
|
693
|
+
const fetchedUpdatedAt = fetched[OWNER_DIRECTORY_SCOPES[0]]?.updatedAtMs;
|
|
694
|
+
if (latest?.updatedAtMs !== undefined && fetchedUpdatedAt !== undefined
|
|
695
|
+
&& fetchedUpdatedAt < latest.updatedAtMs) {
|
|
696
|
+
coordinator.recordFailure(this.now(), coordinator.pullGeneration);
|
|
697
|
+
logger.info(`[OwnerDirectoryCache] Pulled ${fetchedUpdatedAt} is older than notice ${latest.updatedAtMs}`
|
|
698
|
+
+ ` for ${this.options.ownerAid} (${coordinator.failures} in a row), will retry`);
|
|
699
|
+
return; // 保留 pending,等下一轮
|
|
700
|
+
}
|
|
701
|
+
coordinator.clearFailures();
|
|
370
702
|
this.clearPendingUnlessSuperseded(pending);
|
|
371
703
|
}
|
|
372
704
|
catch (error) {
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
const
|
|
705
|
+
// 一次协调者拉取的失败只记一次:13 个 agent 同时 await 同一个 Promise,
|
|
706
|
+
// 若各自 ++,一次网络故障就会把退避直接推到封顶。
|
|
707
|
+
coordinator.recordFailure(this.now(), coordinator.pullGeneration);
|
|
708
|
+
const failures = coordinator.failures;
|
|
709
|
+
this._logAction({
|
|
710
|
+
action: 'error',
|
|
711
|
+
trigger: trigger ?? (pending.operationId.startsWith('notification-') ? 'notification' : 'startup'),
|
|
712
|
+
operationId: pending.operationId,
|
|
713
|
+
generation: this.appliedGeneration,
|
|
714
|
+
sharedGeneration: coordinator.epoch?.generation,
|
|
715
|
+
error: errorText(error),
|
|
716
|
+
detail: `拉取失败(连续第 ${failures} 次,下一档退避 `
|
|
717
|
+
+ `${OWNER_DIRECTORY_PULL_BACKOFF_MS[Math.min(failures, OWNER_DIRECTORY_PULL_BACKOFF_MS.length) - 1]}ms)`,
|
|
718
|
+
});
|
|
719
|
+
const next = OWNER_DIRECTORY_PULL_BACKOFF_MS[Math.min(failures, OWNER_DIRECTORY_PULL_BACKOFF_MS.length) - 1];
|
|
720
|
+
const message = `[OwnerDirectoryCache] Pending pull failed (${failures} in a row, next retry in ${next}ms): ${errorText(error)}`;
|
|
377
721
|
// 连续失败不是抖动:升级日志级别,让"名录一直没更新"能被发现,
|
|
378
722
|
// 而不是翻两小时前的 WARN 才知道。
|
|
379
|
-
if (
|
|
723
|
+
if (failures >= FAILURE_ESCALATE_AFTER)
|
|
380
724
|
logger.error(message);
|
|
381
725
|
else
|
|
382
726
|
logger.warn(message);
|
|
@@ -388,7 +732,7 @@ export class OwnerDirectoryCache {
|
|
|
388
732
|
...state.files[scope],
|
|
389
733
|
status: 'error',
|
|
390
734
|
error: errorText(error),
|
|
391
|
-
failureCount:
|
|
735
|
+
failureCount: failures,
|
|
392
736
|
}])),
|
|
393
737
|
},
|
|
394
738
|
})).catch(() => { });
|
|
@@ -408,67 +752,50 @@ export class OwnerDirectoryCache {
|
|
|
408
752
|
}
|
|
409
753
|
this.clearPendingUpdate();
|
|
410
754
|
}
|
|
411
|
-
/**
|
|
412
|
-
pullBackoffRemainingMs() {
|
|
413
|
-
if (this.pullFailures === 0 || this.lastPullFailureAt === undefined)
|
|
414
|
-
return 0;
|
|
415
|
-
const window = OWNER_DIRECTORY_PULL_BACKOFF_MS[Math.min(this.pullFailures, OWNER_DIRECTORY_PULL_BACKOFF_MS.length) - 1];
|
|
416
|
-
return Math.max(0, this.lastPullFailureAt + window - this.now());
|
|
417
|
-
}
|
|
755
|
+
/** 退避状态在协调者上(owner 级),本实例只转发。 */
|
|
418
756
|
clearPullFailures() {
|
|
419
|
-
this.
|
|
420
|
-
this.lastPullFailureAt = undefined;
|
|
757
|
+
coordinatorFor(this.options.ownerAid).clearFailures();
|
|
421
758
|
}
|
|
422
759
|
// ── 阶段一 / 阶段二 / 组合 ─────────────────────────────────────────────────
|
|
423
760
|
/**
|
|
424
761
|
* 阶段一:把 Owner 名录拉到**共享缓存**,不碰任何 agent 目录。
|
|
425
762
|
*
|
|
426
|
-
*
|
|
427
|
-
*
|
|
428
|
-
*
|
|
429
|
-
* - 否则模块级单飞 get_object:同进程内并发调用共用同一个 Promise
|
|
763
|
+
* 网络请求由 owner 级协调者统一调度:本实例只是「报告需求 + 成为可能的
|
|
764
|
+
* 执行者」,不做去重、不持有退避状态。这样换哪一个 agent 来干都不影响
|
|
765
|
+
* 共享区能不能被维护到最新——重试状态在协调者身上,不随 agent 生死。
|
|
430
766
|
*
|
|
431
|
-
*
|
|
432
|
-
*
|
|
767
|
+
* - 协调者的代号已经等于通知 digest → 0 次网络,直接读共享区
|
|
768
|
+
* - 有人在拉 → 等同一个 Promise,不重复发请求
|
|
769
|
+
* - 没人拉 → 协调者挑一个持有效授权的实例代为拉取
|
|
433
770
|
*/
|
|
434
771
|
async fetchToSharedCache(digests, trigger = 'unspecified', operationId) {
|
|
435
|
-
|
|
436
|
-
const
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
const
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
const existing = _fetchInFlight.get(flightKey);
|
|
451
|
-
const outcome = existing
|
|
452
|
-
? await existing
|
|
453
|
-
: await (() => {
|
|
454
|
-
const startedAt = this.now();
|
|
455
|
-
const fetchPromise = this._doFetch().then(result => {
|
|
456
|
-
// 关系级共享缓存由「拉取方替其他 agent 写」;单 owner 目录只写自己的。
|
|
457
|
-
// 两条分支都要带 trigger / operationId,落地记录才回答得出"这次是谁触发的"。
|
|
458
|
-
if (this.options.relationVersion === undefined && (!this.options.isCurrent || this.options.isCurrent())) {
|
|
459
|
-
this._writeSharedCache(result.data, trigger, operationId, startedAt);
|
|
460
|
-
}
|
|
461
|
-
return result;
|
|
462
|
-
}).finally(() => {
|
|
463
|
-
if (_fetchInFlight.get(flightKey) === fetchPromise) {
|
|
464
|
-
_fetchInFlight.delete(flightKey);
|
|
465
|
-
}
|
|
772
|
+
const coordinator = coordinatorFor(this.options.ownerAid);
|
|
773
|
+
const hintDigest = digests?.[OWNER_DIRECTORY_SCOPES[0]];
|
|
774
|
+
// ① 共享区已经是最新 → 0 次网络。判据优先用内存代号,命中不了才读盘。
|
|
775
|
+
if (hintDigest && coordinator.epoch?.contentDigest === hintDigest) {
|
|
776
|
+
logger.debug(`[OwnerDirectoryCache] Shared cache already at ${hintDigest.slice(0, 8)} for ${this.options.ownerAid}, skipping fetch`);
|
|
777
|
+
const cached = this._readSharedCache();
|
|
778
|
+
if (OWNER_DIRECTORY_SCOPES.every(scope => cached[scope])) {
|
|
779
|
+
this._logAction({
|
|
780
|
+
action: 'skip',
|
|
781
|
+
trigger,
|
|
782
|
+
operationId,
|
|
783
|
+
digest: hintDigest.slice(0, 16),
|
|
784
|
+
generation: this.appliedGeneration,
|
|
785
|
+
sharedGeneration: coordinator.epoch?.generation,
|
|
786
|
+
detail: '共享区已等于通知 digest,未发网络请求',
|
|
466
787
|
});
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
788
|
+
return cached;
|
|
789
|
+
}
|
|
790
|
+
// 共享区读不出来(被别的进程原子替换、文件损坏)就走网络,不抛错——
|
|
791
|
+
// 抛错会让所有 agent 一起进退避,比各自拉一次更糟。
|
|
792
|
+
}
|
|
793
|
+
// ② 协调者级单飞:一个 owner 同时最多一次网络请求。
|
|
794
|
+
const outcome = coordinator.pulling
|
|
795
|
+
? await coordinator.pulling
|
|
796
|
+
: await this._startCoordinatedPull(coordinator, trigger, operationId, hintDigest);
|
|
470
797
|
// 远端明确拒绝:作废本地快照并标记 blocked,重试没有意义。逐个 scope 处理,
|
|
471
|
-
// 因为一次拉取里可能多个 scope
|
|
798
|
+
// 因为一次拉取里可能多个 scope 都被拒;共用同一个 Promise 的调用方也各自执行这一步。
|
|
472
799
|
for (const scope of outcome.denied) {
|
|
473
800
|
if (this.scopeAccess(scope) === 'authorized')
|
|
474
801
|
await this.blockScope(scope);
|
|
@@ -476,6 +803,136 @@ export class OwnerDirectoryCache {
|
|
|
476
803
|
const complete = this._assertComplete(outcome.data, outcome.failures);
|
|
477
804
|
return complete;
|
|
478
805
|
}
|
|
806
|
+
/**
|
|
807
|
+
* 由协调者发起一次拉取。
|
|
808
|
+
*
|
|
809
|
+
* 挑执行者的原因见 `OwnerDirectoryCoordinator`:授权凭证是 per-agent 的,
|
|
810
|
+
* 协调者没有自己的身份,必须借一个当前有效授权的实例。
|
|
811
|
+
*/
|
|
812
|
+
_startCoordinatedPull(coordinator, trigger, operationId, hintDigest) {
|
|
813
|
+
const startedAt = this.now();
|
|
814
|
+
// 新一轮拉取:失败去重按这个序号算,同一轮的多个观测者只记一次。
|
|
815
|
+
coordinator.pullGeneration++;
|
|
816
|
+
const pulling = (async () => {
|
|
817
|
+
// 防抖:把 Owner 侧「逐个通知」的同一批变更合并成一次请求。
|
|
818
|
+
// grant 例外:用户在界面上等结果,防抖没有意义。
|
|
819
|
+
await this._debounceBeforePull(startedAt, trigger);
|
|
820
|
+
// 防抖期间可能已经有人把共享区拉新了,重新对一次代号,避免白跑一趟。
|
|
821
|
+
if (hintDigest && coordinator.epoch?.contentDigest === hintDigest) {
|
|
822
|
+
const cached = this._readSharedCache();
|
|
823
|
+
if (OWNER_DIRECTORY_SCOPES.every(scope => cached[scope])) {
|
|
824
|
+
return { data: cached, denied: [], failures: {} };
|
|
825
|
+
}
|
|
826
|
+
}
|
|
827
|
+
const executor = coordinator.pickExecutor(this);
|
|
828
|
+
if (!executor) {
|
|
829
|
+
throw new OwnerDirectoryCacheError('NOT_AUTHORIZED', 'no agent with a valid owner directory authorization to pull for');
|
|
830
|
+
}
|
|
831
|
+
const result = await executor._doFetch();
|
|
832
|
+
const written = executor._commitSharedCache(result.data, trigger, operationId, startedAt);
|
|
833
|
+
if (written) {
|
|
834
|
+
// 执行者**自己**先同步落地:`onSnapshotApplying` 这类前置校验失败必须
|
|
835
|
+
// 抛回给它自己的调用方("授权失效没持久化成功就不能发布快照")。
|
|
836
|
+
// 广播是 fire-and-forget,放在这里会把失败吞成一行 warn。
|
|
837
|
+
await executor.applyFromSharedCache();
|
|
838
|
+
coordinator.clearFailures();
|
|
839
|
+
executor._logAction({
|
|
840
|
+
action: 'pull',
|
|
841
|
+
trigger,
|
|
842
|
+
operationId,
|
|
843
|
+
digest: result.data[OWNER_DIRECTORY_SCOPES[0]]?.contentDigest?.slice(0, 16),
|
|
844
|
+
sharedGeneration: coordinator.epoch?.generation,
|
|
845
|
+
detail: `拉取成功,执行者=${executor.selfAid},已写共享区并广播`,
|
|
846
|
+
});
|
|
847
|
+
executor._broadcastSharedUpdate();
|
|
848
|
+
}
|
|
849
|
+
return result;
|
|
850
|
+
})().finally(() => {
|
|
851
|
+
if (coordinator.pulling === pulling)
|
|
852
|
+
coordinator.pulling = undefined;
|
|
853
|
+
});
|
|
854
|
+
coordinator.pulling = pulling;
|
|
855
|
+
return pulling;
|
|
856
|
+
}
|
|
857
|
+
/**
|
|
858
|
+
* 拉取前的防抖等待。
|
|
859
|
+
*
|
|
860
|
+
* 只在**协调者当前这一轮拉取**上执行,所以整个进程里最多同时有一个窗口在走。
|
|
861
|
+
* 窗口内到达的通知会更新协调者的 `latestNotice`,但拉取本身已经排上,不必再等
|
|
862
|
+
* 一轮——`processPendingIfNeeded` 的循环会在拉完后看到新的 operationId 再补一次。
|
|
863
|
+
*
|
|
864
|
+
* 授权刚下发(grant)不等待:用户在界面上等结果,防抖没有意义。
|
|
865
|
+
* 判据用 trigger 而不是 operationId:通知和 grant 两条路径的 operationId
|
|
866
|
+
* 都是 `notification-` 前缀,按前缀判断区分不开。
|
|
867
|
+
*/
|
|
868
|
+
async _debounceBeforePull(startedAt, trigger) {
|
|
869
|
+
if (trigger === 'grant')
|
|
870
|
+
return;
|
|
871
|
+
const debounceMs = this.options.pullDebounceMs ?? OWNER_DIRECTORY_PULL_DEBOUNCE_MS;
|
|
872
|
+
if (debounceMs <= 0)
|
|
873
|
+
return;
|
|
874
|
+
const elapsed = this.now() - startedAt;
|
|
875
|
+
const wait = debounceMs - elapsed;
|
|
876
|
+
if (wait <= 0)
|
|
877
|
+
return;
|
|
878
|
+
logger.debug(`[OwnerDirectoryCache] Debouncing ${trigger} pull for ${wait}ms (owner=${this.options.ownerAid})`);
|
|
879
|
+
await new Promise(resolve => {
|
|
880
|
+
const timer = setTimeout(resolve, wait);
|
|
881
|
+
timer.unref?.();
|
|
882
|
+
});
|
|
883
|
+
}
|
|
884
|
+
/**
|
|
885
|
+
* 写共享缓存并推进版本代号。
|
|
886
|
+
*
|
|
887
|
+
* 写盘成功才推进代号:失败时不前进,各 agent 比对时仍看到旧代号,
|
|
888
|
+
* 下一条消息或下一次通知会自然重试。返回是否真正写成功。
|
|
889
|
+
*/
|
|
890
|
+
_commitSharedCache(data, trigger, operationId, startedAt) {
|
|
891
|
+
// 退役的运行时不能替当前关系写共享文件。
|
|
892
|
+
if (this.options.isCurrent?.() === false)
|
|
893
|
+
return false;
|
|
894
|
+
const ok = this._writeSharedCache(data, trigger, operationId, startedAt);
|
|
895
|
+
if (!ok)
|
|
896
|
+
return false;
|
|
897
|
+
const digest = data[OWNER_DIRECTORY_SCOPES[0]]?.contentDigest;
|
|
898
|
+
if (!digest)
|
|
899
|
+
return false;
|
|
900
|
+
const coordinator = coordinatorFor(this.options.ownerAid);
|
|
901
|
+
coordinator.epoch = {
|
|
902
|
+
generation: (coordinator.epoch?.generation ?? 0) + 1,
|
|
903
|
+
contentDigest: digest,
|
|
904
|
+
fetchedAtMs: this.now(),
|
|
905
|
+
};
|
|
906
|
+
return true;
|
|
907
|
+
}
|
|
908
|
+
/**
|
|
909
|
+
* 共享区更新后,让本进程里所有 agent 立刻落地。
|
|
910
|
+
*
|
|
911
|
+
* 这是「不做防抖也简单」的关键一步:拉取方替所有人把网络请求做完,落地
|
|
912
|
+
* 就各自并发写本机文件(一次 31KB),不需要等入站消息,也不需要判断谁是 PA。
|
|
913
|
+
*
|
|
914
|
+
* 每个实例独立 try/catch:某个 agent 的落地失败不能影响其他 agent。
|
|
915
|
+
* 已退役的实例在这里剔除,省掉显式注销。
|
|
916
|
+
*/
|
|
917
|
+
_broadcastSharedUpdate() {
|
|
918
|
+
const coordinator = coordinatorFor(this.options.ownerAid);
|
|
919
|
+
for (const cache of [...coordinator.caches]) {
|
|
920
|
+
if (cache.isRetired()) {
|
|
921
|
+
coordinator.caches.delete(cache);
|
|
922
|
+
continue;
|
|
923
|
+
}
|
|
924
|
+
// 执行者已经自己落地过了(见 `_startCoordinatedPull`)。跳过它是为了让它的
|
|
925
|
+
// 前置校验失败能抛回调用方——否则会变成"别人先把快照写进去,它再一看
|
|
926
|
+
// 摘要相同就跳过",错误被悄悄吞掉。
|
|
927
|
+
if (cache === this)
|
|
928
|
+
continue;
|
|
929
|
+
if (!cache.hasPendingSharedUpdate())
|
|
930
|
+
continue;
|
|
931
|
+
void cache.applyFromSharedCache().catch(error => {
|
|
932
|
+
logger.warn(`[OwnerDirectoryCache] Broadcast apply failed for ${cache.selfAid}: ${errorText(error)}`);
|
|
933
|
+
});
|
|
934
|
+
}
|
|
935
|
+
}
|
|
479
936
|
/**
|
|
480
937
|
* 已授权范围一个都没拿到,就不算拉取成功。
|
|
481
938
|
*
|
|
@@ -514,22 +971,44 @@ export class OwnerDirectoryCache {
|
|
|
514
971
|
return promise;
|
|
515
972
|
}
|
|
516
973
|
async _applyFromSharedCacheOnce() {
|
|
517
|
-
//
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
//
|
|
521
|
-
|
|
522
|
-
//
|
|
974
|
+
// 热路径的「要不要干活」由调用方(`applyFromSharedCacheWithin` →
|
|
975
|
+
// `hasPendingSharedUpdate` 的两次 stat)先判定,这里不再按内存代号早退:
|
|
976
|
+
// 代号与文件时间戳是两套判据,任意一套抢在前面 return 都会让另一套
|
|
977
|
+
// 永远等不到对齐的机会。
|
|
978
|
+
const epoch = coordinatorFor(this.options.ownerAid).epoch;
|
|
979
|
+
// 内容摘要与本地快照一致:不必重写快照,但**镜像必须对齐**。
|
|
980
|
+
// 热路径判据是「共享区 mtime 与镜像 mtime 相等」,这里不对齐的话,
|
|
981
|
+
// 共享区每次重新发布同一份内容都会让每条消息都被判成"有新内容",
|
|
982
|
+
// 反复走一遍本函数。
|
|
523
983
|
if (OWNER_DIRECTORY_SCOPES.every(scope => this._isSharedCacheApplied(scope))) {
|
|
984
|
+
// 只 stat + 写镜像,不解析共享区文档——那正是这个短路要省掉的成本。
|
|
985
|
+
for (const scope of OWNER_DIRECTORY_SCOPES)
|
|
986
|
+
this._writeSyncStamp(scope);
|
|
524
987
|
this.refreshMarkdown();
|
|
525
988
|
return true;
|
|
526
989
|
}
|
|
527
990
|
const fetched = this._readSharedCache();
|
|
528
991
|
if (OWNER_DIRECTORY_SCOPES.every(scope => !fetched[scope])) {
|
|
529
992
|
logger.debug(`[OwnerDirectoryCache] Shared cache empty for ${this.options.ownerAid}, nothing to apply`);
|
|
993
|
+
this._logAction({
|
|
994
|
+
action: 'noop',
|
|
995
|
+
generation: this.appliedGeneration,
|
|
996
|
+
sharedGeneration: epoch?.generation,
|
|
997
|
+
detail: '共享区没有可用字节(首次启动或文件损坏)',
|
|
998
|
+
});
|
|
530
999
|
return false;
|
|
531
1000
|
}
|
|
532
1001
|
await this._applyAll(fetched);
|
|
1002
|
+
const applied = coordinatorFor(this.options.ownerAid).epoch;
|
|
1003
|
+
if (applied)
|
|
1004
|
+
this.appliedGeneration = applied.generation;
|
|
1005
|
+
this._logAction({
|
|
1006
|
+
action: 'apply',
|
|
1007
|
+
digest: fetched[OWNER_DIRECTORY_SCOPES[0]]?.contentDigest?.slice(0, 16),
|
|
1008
|
+
generation: this.appliedGeneration,
|
|
1009
|
+
sharedGeneration: applied?.generation,
|
|
1010
|
+
detail: 'shared → agent 目录',
|
|
1011
|
+
});
|
|
533
1012
|
return true;
|
|
534
1013
|
}
|
|
535
1014
|
/**
|
|
@@ -540,10 +1019,16 @@ export class OwnerDirectoryCache {
|
|
|
540
1019
|
* 整段丢掉名录(尤其是 directory.md 此前根本不存在的时候)。
|
|
541
1020
|
*
|
|
542
1021
|
* 共享缓存为空(owner 级目录还没拉到过)时立刻返回 false,不等超时。
|
|
1022
|
+
* 共享区没有新版本时同理立刻返回——纯内存比较,不产生任何 IO。
|
|
543
1023
|
*/
|
|
544
1024
|
async applyFromSharedCacheWithin(timeoutMs) {
|
|
1025
|
+
// 共享区根本没有内容(从没拉成功过)时立刻返回 false,不等超时也不发网络。
|
|
1026
|
+
// 必须先判这一步:`hasPendingSharedUpdate()` 在共享区缺失时也会返回 false,
|
|
1027
|
+
// 但那个 false 的含义是「没有新内容」,不是「什么都不用做」。
|
|
545
1028
|
if (!OWNER_DIRECTORY_SCOPES.some(scope => this._sharedCacheExists(scope)))
|
|
546
1029
|
return false;
|
|
1030
|
+
if (!this.hasPendingSharedUpdate())
|
|
1031
|
+
return true;
|
|
547
1032
|
let timer;
|
|
548
1033
|
try {
|
|
549
1034
|
return await Promise.race([
|
|
@@ -598,17 +1083,23 @@ export class OwnerDirectoryCache {
|
|
|
598
1083
|
async _applyAll(fetched) {
|
|
599
1084
|
const apply = async () => {
|
|
600
1085
|
for (const scope of OWNER_DIRECTORY_SCOPES) {
|
|
601
|
-
if (this.scopeAccess(scope) !== 'authorized')
|
|
602
|
-
continue;
|
|
603
1086
|
const file = fetched[scope];
|
|
604
1087
|
if (!file)
|
|
605
1088
|
continue;
|
|
1089
|
+
// **不判授权**:这一步是共享区 → agent 区的本地文件拷贝,不发网络请求、
|
|
1090
|
+
// 不降级、不涉及远端 ACL。授权只管"能不能从远端拉",两个环节分开。
|
|
606
1091
|
await this._applyFile(scope, file);
|
|
607
1092
|
}
|
|
608
1093
|
};
|
|
609
1094
|
const next = this.applyTail.then(apply);
|
|
610
1095
|
this.applyTail = next.catch(() => { });
|
|
611
1096
|
await next;
|
|
1097
|
+
// 对账:落地完后认下当前代号,热路径从此只比内存。
|
|
1098
|
+
// 放在这里而不是各调用方,是因为落地有三条入口(拉取后、共享区读回、
|
|
1099
|
+
// 广播),漏掉任何一条都会让热路径反复读盘。
|
|
1100
|
+
const epoch = coordinatorFor(this.options.ownerAid).epoch;
|
|
1101
|
+
if (epoch)
|
|
1102
|
+
this.appliedGeneration = epoch.generation;
|
|
612
1103
|
}
|
|
613
1104
|
// ── 共享缓存($EVOLCORE_HOME/data/owner-directory/<ownerAid>/) ────────────
|
|
614
1105
|
_sharedCacheDir() {
|
|
@@ -700,6 +1191,7 @@ export class OwnerDirectoryCache {
|
|
|
700
1191
|
try {
|
|
701
1192
|
const dir = this._sharedCacheDir();
|
|
702
1193
|
fs.mkdirSync(dir, { recursive: true, mode: 0o700 });
|
|
1194
|
+
let wrote = false;
|
|
703
1195
|
for (const scope of OWNER_DIRECTORY_SCOPES) {
|
|
704
1196
|
const file = data[scope];
|
|
705
1197
|
if (!file)
|
|
@@ -710,20 +1202,15 @@ export class OwnerDirectoryCache {
|
|
|
710
1202
|
atomicWriteBytes(target, file.bytes);
|
|
711
1203
|
fs.chmodSync(target, 0o600);
|
|
712
1204
|
// mtime 必须在 rename 之后设:写入本身会把时间戳刷成本机当前时刻。
|
|
713
|
-
|
|
714
|
-
const seconds = file.storageUpdatedAtMs / 1000;
|
|
715
|
-
try {
|
|
716
|
-
fs.utimesSync(target, seconds, seconds);
|
|
717
|
-
}
|
|
718
|
-
catch (error) {
|
|
719
|
-
logger.debug(`[OwnerDirectoryCache] utimes ${scope} failed: ${errorText(error)}`);
|
|
720
|
-
}
|
|
721
|
-
}
|
|
1205
|
+
applyStorageMtime(target, file.storageUpdatedAtMs);
|
|
722
1206
|
this._recordLanding(scope, file, prev, trigger, operationId, startedAt);
|
|
1207
|
+
wrote = true;
|
|
723
1208
|
}
|
|
1209
|
+
return wrote;
|
|
724
1210
|
}
|
|
725
1211
|
catch (error) {
|
|
726
1212
|
logger.warn(`[OwnerDirectoryCache] Failed to write shared cache: ${errorText(error)}`);
|
|
1213
|
+
return false;
|
|
727
1214
|
}
|
|
728
1215
|
}
|
|
729
1216
|
/** 读旧共享缓存;不存在或已损坏都返回 undefined,不影响本次落地。 */
|
|
@@ -788,6 +1275,41 @@ export class OwnerDirectoryCache {
|
|
|
788
1275
|
}
|
|
789
1276
|
}
|
|
790
1277
|
/** 追加并滚动 `landing.jsonl`;与 `failures.jsonl` 同一套行数/字节双上限。 */
|
|
1278
|
+
/**
|
|
1279
|
+
* 记一条 agent 级动作日志。
|
|
1280
|
+
*
|
|
1281
|
+
* 与共享区 `landing.jsonl` 的分工:那一份记"远端内容什么时候到了共享区",
|
|
1282
|
+
* 这一份记"**本 agent** 收到通知之后做了什么"。排查「某个 agent 名录不更新」
|
|
1283
|
+
* 时,先看这里有没有 `notice`、再看它后面跟的是 `skip`(以为已最新,可能是
|
|
1284
|
+
* 代号错了)还是 `apply`(真写了盘)还是 `error`(失败原因)。
|
|
1285
|
+
*
|
|
1286
|
+
* 写失败只 warn:诊断设施本身的故障不能影响同步。
|
|
1287
|
+
*/
|
|
1288
|
+
_logAction(record) {
|
|
1289
|
+
try {
|
|
1290
|
+
const dir = this.options.directory;
|
|
1291
|
+
fs.mkdirSync(dir, { recursive: true, mode: 0o700 });
|
|
1292
|
+
const target = path.join(dir, ACTIONS_FILE);
|
|
1293
|
+
const previous = fs.existsSync(target) ? fs.readFileSync(target, 'utf8') : '';
|
|
1294
|
+
const lines = previous.split('\n').filter(Boolean);
|
|
1295
|
+
lines.push(JSON.stringify({
|
|
1296
|
+
at: this.now(),
|
|
1297
|
+
scope: record.scope ?? OWNER_DIRECTORY_SCOPES[0],
|
|
1298
|
+
...record,
|
|
1299
|
+
}));
|
|
1300
|
+
const kept = lines.slice(-ACTIONS_KEEP_LINES);
|
|
1301
|
+
while (kept.length > 1 && Buffer.byteLength(kept.join('\n'), 'utf8') > ACTIONS_MAX_BYTES)
|
|
1302
|
+
kept.shift();
|
|
1303
|
+
atomicWriteText(target, `${kept.join('\n')}\n`);
|
|
1304
|
+
try {
|
|
1305
|
+
fs.chmodSync(target, 0o600);
|
|
1306
|
+
}
|
|
1307
|
+
catch { /* Windows 上可能不支持 */ }
|
|
1308
|
+
}
|
|
1309
|
+
catch (error) {
|
|
1310
|
+
logger.warn(`[OwnerDirectoryCache] Failed to append action log for ${this.options.selfAid}: ${errorText(error)}`);
|
|
1311
|
+
}
|
|
1312
|
+
}
|
|
791
1313
|
_appendLandingLine(record) {
|
|
792
1314
|
const target = path.join(this._sharedCacheDir(), LANDING_FILE);
|
|
793
1315
|
const previous = fs.existsSync(target) ? fs.readFileSync(target, 'utf8') : '';
|
|
@@ -846,10 +1368,13 @@ export class OwnerDirectoryCache {
|
|
|
846
1368
|
const key = `data/directories/v2/${scope}.json`;
|
|
847
1369
|
const { bytes, metadata } = await readAuthoritativePersonalFsObject(this.options.client, `/${key}`, this.options.ownerAid, OWNER_DIRECTORY_MAX_OBJECT_BYTES);
|
|
848
1370
|
const sha256 = typeof metadata.sha256 === 'string' ? metadata.sha256.toLowerCase() : '';
|
|
1371
|
+
// storage 侧这份对象的修改时间:raw 副本、共享缓存、agent 本地快照三处的
|
|
1372
|
+
// 文件时间都统一用它,让任何一个文件 `stat` 出来都是"远端这版什么时候的"。
|
|
1373
|
+
const storageUpdatedAtMs = storageMtimeMs(metadata);
|
|
849
1374
|
// 网络这一层已经拿回字节:先落盘留证,再谈内容合不合格。
|
|
850
1375
|
// 校验失败时下游只看得到一行 WARN,靠这份原始字节才能判断"拉到了、
|
|
851
1376
|
// 只是内容不对",而不是"拉不到"。
|
|
852
|
-
this._writeRawFetch(scope, bytes, sha256);
|
|
1377
|
+
this._writeRawFetch(scope, bytes, sha256, storageUpdatedAtMs);
|
|
853
1378
|
if (!SHA256_RE.test(sha256))
|
|
854
1379
|
throw new OwnerDirectoryCacheError('INVALID_DIGEST', `bad sha256 for ${scope}`);
|
|
855
1380
|
let text = new TextDecoder('utf-8', { fatal: true }).decode(bytes);
|
|
@@ -858,9 +1383,13 @@ export class OwnerDirectoryCache {
|
|
|
858
1383
|
const parsed = JSON.parse(text);
|
|
859
1384
|
// 落盘字节保持原样(含 BOM 都留着),解析才剥——共享缓存必须与远端逐字节一致。
|
|
860
1385
|
result[scope] = this.parseDirectoryObject(parsed, bytes, sha256, scope, {
|
|
861
|
-
storageUpdatedAtMs:
|
|
1386
|
+
storageUpdatedAtMs: storageUpdatedAtMs ?? numberField(parsed, 'updated_at_ms'),
|
|
862
1387
|
});
|
|
863
1388
|
logger.info(`[OwnerDirectoryCache] Fetched ${scope}: total=${result[scope].total} digest=${result[scope].contentDigest?.slice(0, 8) ?? 'none'}`);
|
|
1389
|
+
// 这次拉通了,撤掉之前的拉取标记(写盘失败不影响本次结果)。
|
|
1390
|
+
if (this.pullBlocked(scope)) {
|
|
1391
|
+
await this.clearScopeBlock(scope).catch(() => { });
|
|
1392
|
+
}
|
|
864
1393
|
}
|
|
865
1394
|
catch (error) {
|
|
866
1395
|
const normalized = toDirectoryError(error);
|
|
@@ -884,15 +1413,24 @@ export class OwnerDirectoryCache {
|
|
|
884
1413
|
* 这一份**不参与**任何读取路径,只为排查——"到底拉到了什么"必须能从磁盘
|
|
885
1414
|
* 直接看到,而不是只能从日志推断。所以它在校验之前写,失败也保留。
|
|
886
1415
|
*/
|
|
887
|
-
_writeRawFetch(scope, bytes, sha256) {
|
|
1416
|
+
_writeRawFetch(scope, bytes, sha256, storageUpdatedAtMs) {
|
|
888
1417
|
try {
|
|
889
1418
|
const dir = path.join(this._sharedCacheDir(), RAW_DIR);
|
|
890
1419
|
fs.mkdirSync(dir, { recursive: true, mode: 0o700 });
|
|
891
1420
|
const target = path.join(dir, `${scope}.json`);
|
|
892
1421
|
atomicWriteBytes(target, bytes);
|
|
893
1422
|
fs.chmodSync(target, 0o600);
|
|
1423
|
+
// mtime 回拨到 storage 的修改时间:这份字节代表的是远端那一版,
|
|
1424
|
+
// 文件时间就该回答"远端这版是什么时候的",而不是"我什么时候拷下来"。
|
|
1425
|
+
// 拉取时刻单独记在 meta.fetchedAt,两个语义分开存。
|
|
1426
|
+
applyStorageMtime(target, storageUpdatedAtMs);
|
|
894
1427
|
const meta = path.join(dir, `${scope}.meta.json`);
|
|
895
|
-
atomicWriteJson(meta, {
|
|
1428
|
+
atomicWriteJson(meta, {
|
|
1429
|
+
sha256,
|
|
1430
|
+
bytes: bytes.byteLength,
|
|
1431
|
+
fetchedAt: this.now(),
|
|
1432
|
+
...(storageUpdatedAtMs !== undefined ? { storageUpdatedAtMs } : {}),
|
|
1433
|
+
});
|
|
896
1434
|
fs.chmodSync(meta, 0o600);
|
|
897
1435
|
}
|
|
898
1436
|
catch (error) {
|
|
@@ -1086,6 +1624,8 @@ export class OwnerDirectoryCache {
|
|
|
1086
1624
|
if (recomputed !== declared) {
|
|
1087
1625
|
logger.warn(`[OwnerDirectoryCache] ${scope}.json content_digest mismatch (accepted, server authoritative): declared=${declared.slice(0, 12)} recomputed=${recomputed.slice(0, 12)}`);
|
|
1088
1626
|
}
|
|
1627
|
+
// 解析 open_pa_context:PA Team 成员 AID 列表
|
|
1628
|
+
const openPaContext = parseOpenPaContext(parsed.open_pa_context, seen, scope);
|
|
1089
1629
|
return {
|
|
1090
1630
|
raw: parsed, bytes, sha256, ownerAid: this.options.ownerAid, scope, schemaVersion: schemaVersion,
|
|
1091
1631
|
contentDigest: declared,
|
|
@@ -1098,6 +1638,7 @@ export class OwnerDirectoryCache {
|
|
|
1098
1638
|
blockedAids: blockedAids,
|
|
1099
1639
|
owner: parseIdentityField(parsed.owner),
|
|
1100
1640
|
personalAssistant: parseIdentityField(parsed.personal_assistant),
|
|
1641
|
+
openPaContext,
|
|
1101
1642
|
};
|
|
1102
1643
|
}
|
|
1103
1644
|
// ── Internal: applyFile ──────────────────────────────────────────────────────
|
|
@@ -1108,6 +1649,10 @@ export class OwnerDirectoryCache {
|
|
|
1108
1649
|
const localDigest = prev?.contentDigest;
|
|
1109
1650
|
if (file.contentDigest && localDigest && file.contentDigest === localDigest) {
|
|
1110
1651
|
logger.debug(`[OwnerDirectoryCache] ${scope} content_digest matches local cache, skipping write`);
|
|
1652
|
+
// 内容一样,但**镜像元数据必须刷新**:热路径比的是"共享区"与"落地镜像"
|
|
1653
|
+
// 两个 mtime,不刷新的话共享区每次更新(哪怕内容相同)都会被当成新内容,
|
|
1654
|
+
// 每条消息重走一遍解析。
|
|
1655
|
+
this._writeSyncStamp(scope, file);
|
|
1111
1656
|
// 内容没变但渲染规则可能变了(分组规则、备注、外部开关),重算一次。
|
|
1112
1657
|
// refreshMarkdown 自己会比对字节,内容相同就不落盘。
|
|
1113
1658
|
this.refreshMarkdown();
|
|
@@ -1125,7 +1670,10 @@ export class OwnerDirectoryCache {
|
|
|
1125
1670
|
const snapshot = {
|
|
1126
1671
|
sourceRevision: file.raw.source_revision,
|
|
1127
1672
|
...(this.options.relationVersion !== undefined ? { relationVersion: this.options.relationVersion } : {}),
|
|
1128
|
-
|
|
1673
|
+
// `target_type` 的 aid 与 group 都要收:被拉黑的群同样是"不该出现在名录里、
|
|
1674
|
+
// 也不该被主动联系"的主体,只留 aid 会让群的拉黑在渲染层失效。
|
|
1675
|
+
blockedAids: file.blockedAids
|
|
1676
|
+
.filter(value => isRecord(value) && (value.target_type === 'aid' || value.target_type === 'group'))
|
|
1129
1677
|
.map(value => value.target_id),
|
|
1130
1678
|
ownerAid: this.options.ownerAid,
|
|
1131
1679
|
granteeAid: this.options.selfAid,
|
|
@@ -1138,6 +1686,7 @@ export class OwnerDirectoryCache {
|
|
|
1138
1686
|
entries,
|
|
1139
1687
|
...(file.owner ? { owner: file.owner } : {}),
|
|
1140
1688
|
...(file.personalAssistant ? { personalAssistant: file.personalAssistant } : {}),
|
|
1689
|
+
...(file.openPaContext ? { openPaContext: file.openPaContext } : {}),
|
|
1141
1690
|
};
|
|
1142
1691
|
this.options.onSnapshotApplying?.(snapshot);
|
|
1143
1692
|
if (!this.canMutate())
|
|
@@ -1155,6 +1704,11 @@ export class OwnerDirectoryCache {
|
|
|
1155
1704
|
fs.mkdirSync(this.options.directory, { recursive: true, mode: 0o700 });
|
|
1156
1705
|
atomicWriteJson(this.cachePath(scope), stored);
|
|
1157
1706
|
fs.chmodSync(this.cachePath(scope), 0o600);
|
|
1707
|
+
// 与共享缓存、raw 副本同一个口径:文件时间回答"远端这版什么时候的",
|
|
1708
|
+
// 本机落地时刻记在快照的 fetchedAt 里。必须在 rename 之后设。
|
|
1709
|
+
applyStorageMtime(this.cachePath(scope), file.storageUpdatedAtMs);
|
|
1710
|
+
// 落地镜像:记下"本 agent 已同步到共享区的哪一版"。热路径只 stat 它。
|
|
1711
|
+
this._writeSyncStamp(scope, file);
|
|
1158
1712
|
this.snapshotCache.set(scope, { snapshot, stamp: this.cacheStamp(scope) });
|
|
1159
1713
|
// agents.json 已并入 contacts.json:首次成功应用后清掉遗留文件。必须连热备
|
|
1160
1714
|
// 一起删,否则 atomicRead 会把 `agents.json_` 当成中断的 rename 恢复回来。
|
|
@@ -1199,8 +1753,8 @@ export class OwnerDirectoryCache {
|
|
|
1199
1753
|
}
|
|
1200
1754
|
// ── Internal: readCachedSnapshot ────────────────────────────────────────────
|
|
1201
1755
|
readCachedSnapshot(scope) {
|
|
1202
|
-
|
|
1203
|
-
|
|
1756
|
+
// 不判授权:本地文件既然在盘上,就该读得出来。授权是远端读取的事,
|
|
1757
|
+
// 由准入层(`owner-directory-admission`)自己按需判断。
|
|
1204
1758
|
const stamp = this.cacheStamp(scope);
|
|
1205
1759
|
const cached = this.snapshotCache.get(scope);
|
|
1206
1760
|
if (cached !== undefined && cached.stamp === stamp)
|
|
@@ -1239,6 +1793,10 @@ export class OwnerDirectoryCache {
|
|
|
1239
1793
|
...(isRecord(raw.personalAssistant)
|
|
1240
1794
|
? { personalAssistant: parseIdentityField(raw.personalAssistant) }
|
|
1241
1795
|
: isRecord(raw.personal_assistant) ? { personalAssistant: parseIdentityField(raw.personal_assistant) } : {}),
|
|
1796
|
+
// `open_pa_context` 同样两种拼写都认:本地快照写 camelCase,远端文档是 snake_case。
|
|
1797
|
+
...(Array.isArray(raw.openPaContext) || Array.isArray(raw.open_pa_context)
|
|
1798
|
+
? { openPaContext: (raw.openPaContext ?? raw.open_pa_context) }
|
|
1799
|
+
: {}),
|
|
1242
1800
|
});
|
|
1243
1801
|
}
|
|
1244
1802
|
catch {
|
|
@@ -1260,17 +1818,35 @@ export class OwnerDirectoryCache {
|
|
|
1260
1818
|
}
|
|
1261
1819
|
}
|
|
1262
1820
|
// ── Internal: authorization / sync state / paths ────────────────────────────
|
|
1821
|
+
/**
|
|
1822
|
+
* 记下「这个实例拉不动这份对象」。
|
|
1823
|
+
*
|
|
1824
|
+
* 只写 `blockedScopes`,供 `pickExecutor` 换个执行者。**不删本地快照、
|
|
1825
|
+
* 不改文件状态、不影响准入**:快照还在、内容由共享缓存供给,删了只会让本
|
|
1826
|
+
* agent 在远端权限修好之前既发不出也收不进。真正撤权走 `revokeAuthorization`。
|
|
1827
|
+
*/
|
|
1263
1828
|
async blockScope(scope) {
|
|
1264
1829
|
if (!this.canMutate())
|
|
1265
1830
|
return;
|
|
1266
|
-
|
|
1267
|
-
|
|
1831
|
+
if (this.readSyncState().blockedScopes.includes(scope))
|
|
1832
|
+
return;
|
|
1268
1833
|
await this.updateSyncState(state => ({
|
|
1269
1834
|
...state,
|
|
1270
1835
|
blockedScopes: [...new Set([...state.blockedScopes, scope])],
|
|
1271
|
-
files: { ...state.files, [scope]: { ...state.files[scope], status: 'blocked', error: 'remote storage denied access' } },
|
|
1272
1836
|
}));
|
|
1273
|
-
this.
|
|
1837
|
+
logger.info(`[OwnerDirectoryCache] Marked ${scope} as pull-blocked for ${this.options.selfAid} (other agents keep pulling)`);
|
|
1838
|
+
}
|
|
1839
|
+
/** 本实例重新拉到了:解除拉取标记,恢复执行者候选资格。 */
|
|
1840
|
+
async clearScopeBlock(scope) {
|
|
1841
|
+
if (!this.canMutate())
|
|
1842
|
+
return;
|
|
1843
|
+
if (!this.readSyncState().blockedScopes.includes(scope))
|
|
1844
|
+
return;
|
|
1845
|
+
await this.updateSyncState(state => ({
|
|
1846
|
+
...state,
|
|
1847
|
+
blockedScopes: state.blockedScopes.filter(item => item !== scope),
|
|
1848
|
+
}));
|
|
1849
|
+
logger.info(`[OwnerDirectoryCache] Cleared pull-block for ${scope} on ${this.options.selfAid}`);
|
|
1274
1850
|
}
|
|
1275
1851
|
async updateFileState(scope, update) {
|
|
1276
1852
|
await this.updateSyncState(state => ({
|
|
@@ -1497,6 +2073,39 @@ function parseIdentityField(value) {
|
|
|
1497
2073
|
...(Number.isSafeInteger(value.agent_md_signed_at) ? { agent_md_signed_at: value.agent_md_signed_at } : {}),
|
|
1498
2074
|
};
|
|
1499
2075
|
}
|
|
2076
|
+
/**
|
|
2077
|
+
* 解析 open_pa_context:PA Team 成员 AID 列表。
|
|
2078
|
+
*
|
|
2079
|
+
* 校验规则:
|
|
2080
|
+
* - 必须是字符串数组
|
|
2081
|
+
* - 每个元素必须是合法 AID
|
|
2082
|
+
* - 去重
|
|
2083
|
+
* - 过滤掉不在 entries 里的悬空 AID(记 warn)
|
|
2084
|
+
*/
|
|
2085
|
+
function parseOpenPaContext(value, validAids, scope) {
|
|
2086
|
+
if (!Array.isArray(value))
|
|
2087
|
+
return undefined;
|
|
2088
|
+
const seen = new Set();
|
|
2089
|
+
const result = [];
|
|
2090
|
+
for (const item of value) {
|
|
2091
|
+
if (typeof item !== 'string' || !item.trim())
|
|
2092
|
+
continue;
|
|
2093
|
+
const aid = item.trim();
|
|
2094
|
+
if (!isValidAid(aid)) {
|
|
2095
|
+
logger.warn(`[OwnerDirectoryCache] ${scope}.json open_pa_context contains invalid AID: ${aid}`);
|
|
2096
|
+
continue;
|
|
2097
|
+
}
|
|
2098
|
+
if (seen.has(aid))
|
|
2099
|
+
continue;
|
|
2100
|
+
seen.add(aid);
|
|
2101
|
+
if (!validAids.has(aid)) {
|
|
2102
|
+
logger.warn(`[OwnerDirectoryCache] ${scope}.json open_pa_context contains AID not in entries: ${aid}`);
|
|
2103
|
+
continue;
|
|
2104
|
+
}
|
|
2105
|
+
result.push(aid);
|
|
2106
|
+
}
|
|
2107
|
+
return result.length > 0 ? result : undefined;
|
|
2108
|
+
}
|
|
1500
2109
|
/**
|
|
1501
2110
|
* 把远端条目收敛成唯一的内部形状。
|
|
1502
2111
|
*
|
|
@@ -1766,9 +2375,9 @@ function isPermissionDenied(error) {
|
|
|
1766
2375
|
/**
|
|
1767
2376
|
* 把"远端明确拒绝"的各种错误形态归一成 `PERMISSION_DENIED`。
|
|
1768
2377
|
*
|
|
1769
|
-
* `blockScope`
|
|
1770
|
-
*
|
|
1771
|
-
*
|
|
2378
|
+
* 归一的用途只是把"这次拉不动"和"网络抖动"分开:前者让 `blockScope` 换个人去拉,
|
|
2379
|
+
* 后者走普通退避重试。两者都**不影响本 agent 能不能用名录**——撤权由
|
|
2380
|
+
* `revokeAuthorization` 单独表达,不靠一次拉取失败推断。
|
|
1772
2381
|
*
|
|
1773
2382
|
* 形态来源:
|
|
1774
2383
|
* - AUN RPC 拒绝:`code` 为 `-32004` / `403` / `4030`,或错误消息里带这些词
|
|
@@ -1850,6 +2459,27 @@ function mtimeOf(filePath) {
|
|
|
1850
2459
|
return undefined;
|
|
1851
2460
|
}
|
|
1852
2461
|
}
|
|
2462
|
+
/**
|
|
2463
|
+
* 把文件 mtime 回拨到 storage 的修改时间。
|
|
2464
|
+
*
|
|
2465
|
+
* 名录相关的每一份本地副本(raw 诊断副本、共享缓存、agent 本地快照)都代表
|
|
2466
|
+
* 远端同一个版本,文件时间就该统一回答"远端这版是什么时候的"。本机的拉取/
|
|
2467
|
+
* 落地时刻另记在 `fetchedAt` 里,两者不要混用。
|
|
2468
|
+
*
|
|
2469
|
+
* 必须在 rename 之后调用:写盘本身会把时间戳刷成本机当前时刻。取不到远端
|
|
2470
|
+
* 时间就保持本地写入时间,不做猜测。
|
|
2471
|
+
*/
|
|
2472
|
+
function applyStorageMtime(filePath, storageUpdatedAtMs) {
|
|
2473
|
+
if (storageUpdatedAtMs === undefined || !Number.isFinite(storageUpdatedAtMs) || storageUpdatedAtMs <= 0)
|
|
2474
|
+
return;
|
|
2475
|
+
const seconds = storageUpdatedAtMs / 1000;
|
|
2476
|
+
try {
|
|
2477
|
+
fs.utimesSync(filePath, seconds, seconds);
|
|
2478
|
+
}
|
|
2479
|
+
catch (error) {
|
|
2480
|
+
logger.debug(`[OwnerDirectoryCache] utimes ${filePath} failed: ${errorText(error)}`);
|
|
2481
|
+
}
|
|
2482
|
+
}
|
|
1853
2483
|
function errorText(error) {
|
|
1854
2484
|
return error instanceof Error ? error.message : String(error);
|
|
1855
2485
|
}
|