evolcore 0.0.41 → 0.0.42

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (190) hide show
  1. package/CHANGELOG.md +36 -0
  2. package/bin/codex-managed-hook.mjs +9 -5
  3. package/dist/agents/baseagent.js +156 -16
  4. package/dist/agents/claude-runner.js +10 -3
  5. package/dist/agents/codex/catalog.js +8 -4
  6. package/dist/agents/codex-runner.js +236 -59
  7. package/dist/agents/ecagent-runner.js +82 -13
  8. package/dist/aun/aid/agentmd.js +106 -27
  9. package/dist/aun/aid/client.js +1 -1
  10. package/dist/aun/aid/control-aid.js +65 -13
  11. package/dist/aun/aid/identity.js +70 -4
  12. package/dist/aun/aid/index.js +2 -2
  13. package/dist/aun/service-proxy.js +25 -1
  14. package/dist/channels/aun.js +242 -209
  15. package/dist/channels/dingtalk.js +1 -1
  16. package/dist/channels/feishu.js +1 -1
  17. package/dist/channels/qqbot.js +1 -1
  18. package/dist/channels/wechat.js +1 -1
  19. package/dist/channels/wecom.js +1 -1
  20. package/dist/cli/agent-command.js +183 -3
  21. package/dist/cli/agent-create.js +92 -86
  22. package/dist/cli/agent.js +136 -1
  23. package/dist/cli/aun-commands.js +137 -35
  24. package/dist/cli/bench.js +8 -2
  25. package/dist/cli/command-log.js +1 -1
  26. package/dist/cli/daemon-commands.js +235 -51
  27. package/dist/cli/debug-mode.js +1 -1
  28. package/dist/cli/directory.js +170 -0
  29. package/dist/cli/index.js +40 -30
  30. package/dist/cli/init-channel.js +4 -4
  31. package/dist/cli/init.js +47 -32
  32. package/dist/cli/managed-command-guard.js +4 -1
  33. package/dist/cli/model.js +27 -18
  34. package/dist/cli/pa-workspace-command.js +191 -0
  35. package/dist/cli/{contact.js → peer.js} +19 -25
  36. package/dist/cli/restart-monitor.js +11 -4
  37. package/dist/cli/thread-command.js +47 -18
  38. package/dist/cli/version.js +18 -18
  39. package/dist/config/access-policy.js +29 -12
  40. package/dist/config/boot-log.js +65 -10
  41. package/dist/config/builtin-roles.js +2 -4
  42. package/dist/config/config-field-policy.js +3 -0
  43. package/dist/config/config-manager.js +28 -20
  44. package/dist/config/{contact-bind-code.js → peer-bind-code.js} +6 -6
  45. package/dist/config/{contact-book-store.js → peer-book-store.js} +75 -75
  46. package/dist/config/{contact-book-v2-startup.js → peer-book-v2-startup.js} +2 -2
  47. package/dist/config/{contact-book.js → peer-book.js} +47 -47
  48. package/dist/config/peer-operation-service.js +132 -0
  49. package/dist/config/{contact-request-service.js → peer-request-service.js} +90 -90
  50. package/dist/config/peer-role-resolver.js +3 -3
  51. package/dist/config/providers-config.js +17 -8
  52. package/dist/config/providers.js +1 -1
  53. package/dist/config/schema-migration.js +336 -39
  54. package/dist/core/agent-application-service.js +8 -2
  55. package/dist/core/agent-create-status.js +4 -5
  56. package/dist/core/agent-ipc-registration.js +5 -4
  57. package/dist/core/agent-runtime.js +30 -17
  58. package/dist/core/auth/operation-authorizer.js +0 -24
  59. package/dist/core/auth/operation-catalog.js +4 -20
  60. package/dist/core/baseagent-loader.js +27 -3
  61. package/dist/core/bootstrap-service.js +5 -22
  62. package/dist/core/capability/capability-manager.js +52 -35
  63. package/dist/core/channel-loader.js +43 -2
  64. package/dist/core/channel-runtime.js +2 -2
  65. package/dist/core/command/agent-control.js +54 -18
  66. package/dist/core/command/command-handler.js +92 -72
  67. package/dist/core/command/connect-menu.js +35 -42
  68. package/dist/core/command/daemon-diagnostics.js +41 -5
  69. package/dist/core/command/data-root-error.js +10 -0
  70. package/dist/core/command/data-root-resolver.js +53 -13
  71. package/dist/core/command/menu-catalog.js +1 -0
  72. package/dist/core/command/menu-handler.js +163 -65
  73. package/dist/core/command/menu-protocol.js +52 -0
  74. package/dist/core/command/pa-workspace-calendar.js +223 -0
  75. package/dist/core/command/pa-workspace-store.js +459 -0
  76. package/dist/core/command/role-menu.js +2 -2
  77. package/dist/core/command/slash-gate.js +1 -1
  78. package/dist/core/command/slash-handler.js +57 -41
  79. package/dist/core/daemon-lifecycle.js +90 -31
  80. package/dist/core/evolagent.js +6 -10
  81. package/dist/core/handoff/runtime.js +11 -4
  82. package/dist/core/host-snapshot.js +208 -0
  83. package/dist/core/initialization/service.js +152 -20
  84. package/dist/core/ipc-registration.js +33 -4
  85. package/dist/core/memory/batch-meta.js +27 -0
  86. package/dist/core/memory/consolidation-ops-records.js +44 -1
  87. package/dist/core/memory/consolidation.js +169 -27
  88. package/dist/core/message/aun-ipc-sender.js +2 -8
  89. package/dist/core/message/im-renderer.js +56 -3
  90. package/dist/core/message/message-admission.js +121 -124
  91. package/dist/core/message/message-bridge.js +38 -54
  92. package/dist/core/message/message-queue.js +7 -0
  93. package/dist/core/message/outbound.js +34 -9
  94. package/dist/core/message/response-engine.js +154 -73
  95. package/dist/core/model/catalog-service.js +1 -1
  96. package/dist/core/model/config-scope.js +2 -2
  97. package/dist/core/model/model-catalog.js +5 -4
  98. package/dist/core/model/model-diagnostics.js +6 -5
  99. package/dist/core/model/model-fallback.js +3 -6
  100. package/dist/core/model/model-permission.js +1 -1
  101. package/dist/core/ownership/owner-resolver.js +9 -9
  102. package/dist/core/permission/approval-gateway.js +1 -1
  103. package/dist/core/permission/ec-command-parser.js +2 -2
  104. package/dist/core/permission/tool-canonical.js +96 -46
  105. package/dist/core/permission/tool-error-code.js +41 -0
  106. package/dist/core/permission/tool-policy.js +71 -47
  107. package/dist/core/role/runtime-policy.js +1 -1
  108. package/dist/core/session/session-idle-monitor.js +10 -0
  109. package/dist/core/session/session-manager.js +2 -4
  110. package/dist/core/session/session-title.js +36 -8
  111. package/dist/core/system-session/memory.js +2 -7
  112. package/dist/core/thread/command-parser.js +6 -2
  113. package/dist/core/thread/ipc-operation.js +51 -36
  114. package/dist/core/thread/thread-operation.js +29 -61
  115. package/dist/core/thread/thread-service.js +57 -13
  116. package/dist/directory/canonical-digest.js +54 -0
  117. package/dist/directory/owner-directory-admission.js +114 -43
  118. package/dist/directory/owner-directory-agentmd-sync.js +89 -0
  119. package/dist/directory/owner-directory-cache.js +847 -459
  120. package/dist/directory/owner-directory-markdown.js +272 -0
  121. package/dist/directory/owner-directory-query-operation.js +195 -0
  122. package/dist/directory/owner-directory-remark-operation.js +147 -0
  123. package/dist/directory/owner-directory-remarks.js +145 -0
  124. package/dist/directory/owner-directory-scope.js +55 -0
  125. package/dist/directory/owner-directory-search.js +148 -0
  126. package/dist/directory/owner-directory-store.js +227 -0
  127. package/dist/eck/kit-renderer.js +1 -0
  128. package/dist/ecweb/basic-settings-service.js +4 -1
  129. package/dist/ecweb/message-context-service.js +13 -9
  130. package/dist/index.js +195 -37
  131. package/dist/ipc.js +86 -22
  132. package/dist/paths.js +11 -8
  133. package/dist/product.js +2 -0
  134. package/dist/proxy/manager.js +13 -2
  135. package/dist/proxy/service-supervisor.js +8 -1
  136. package/dist/trigger/history.js +23 -0
  137. package/dist/trigger/scheduler.js +3 -0
  138. package/dist/utils/atomic-write.js +21 -0
  139. package/dist/utils/cross-platform.js +35 -0
  140. package/dist/utils/ecweb-supervisor.js +42 -0
  141. package/dist/utils/error-utils.js +2 -1
  142. package/dist/utils/evolcore-version.js +19 -0
  143. package/dist/utils/host-info.js +242 -5
  144. package/dist/utils/instance-registry.js +28 -6
  145. package/dist/utils/logger.js +59 -14
  146. package/dist/utils/machine-info.js +46 -0
  147. package/dist/utils/process-introspect.js +166 -2
  148. package/dist/utils/startup-trace.js +245 -0
  149. package/ecagent/dist/agent-loop.js +27 -13
  150. package/ecagent/dist/harness/agent-harness.js +3 -3
  151. package/ecagent/dist/harness/types.d.ts +1 -0
  152. package/ecagent/dist/types.d.ts +1 -0
  153. package/kits/docs/INDEX.md +1 -1
  154. package/kits/docs/aun/AUN/346/266/210/346/201/257/347/261/273/345/236/213/344/270/216/345/261/236/346/200/247.md +72 -0
  155. package/kits/docs/evolcore/INDEX.md +3 -2
  156. package/kits/docs/evolcore/agent-create.md +17 -27
  157. package/kits/docs/evolcore/agent.md +4 -1
  158. package/kits/docs/evolcore/aid.md +3 -1
  159. package/kits/docs/evolcore/config.md +16 -5
  160. package/kits/docs/evolcore/ctl.md +9 -0
  161. package/kits/docs/evolcore/ec-capabilities.md +19 -0
  162. package/kits/docs/evolcore/memory-consolidation-batches.md +79 -5
  163. package/kits/docs/evolcore/menu-exec-schema-commands.md +2 -1
  164. package/kits/docs/evolcore/menu-protocol.md +1 -0
  165. package/kits/docs/evolcore/model.md +13 -4
  166. package/kits/docs/evolcore/msg.md +1 -1
  167. package/kits/docs/evolcore/pa-workspace.md +51 -0
  168. package/kits/docs/evolcore/peer.md +73 -0
  169. package/kits/docs/evolcore/thread-chain.md +4 -1
  170. package/kits/eck_manifest.json +21 -0
  171. package/kits/rules/06-channel.md +1 -1
  172. package/kits/schemas/_meta.json +12 -1
  173. package/kits/schemas/agent-config.schema.16.json +6 -0
  174. package/kits/schemas/agent-config.schema.17.json +897 -0
  175. package/kits/schemas/daemon.schema.9.json +8 -1
  176. package/kits/skills/ec-agent-create/SKILL.md +2 -2
  177. package/kits/skills/ec-identity/SKILL.md +45 -27
  178. package/kits/skills/ec-service-proxy/SKILL.md +7 -2
  179. package/kits/skills/personal-assistant/references/03-data-flow-and-gaps.md +4 -5
  180. package/kits/skills/thread-ops/SKILL.md +11 -11
  181. package/kits/templates/bootstrap-welcome.md +3 -15
  182. package/kits/templates/message-fragments/item.md +1 -1
  183. package/kits/templates/system-fragments/bootstrap.md +24 -23
  184. package/kits/templates/system-fragments/commands.md +12 -5
  185. package/kits/templates/system-fragments/thread.md +18 -3
  186. package/package.json +6 -4
  187. package/dist/config/contact-operation-service.js +0 -235
  188. package/dist/directory/owner-directory-authorization.js +0 -309
  189. package/kits/docs/evolcore/contact.md +0 -78
  190. /package/dist/config/{contact-alias.js → peer-alias.js} +0 -0
@@ -1,7 +1,9 @@
1
- import fs from 'node:fs';
2
1
  import path from 'node:path';
3
2
  import { agentDir } from '../paths.js';
3
+ import { existsAtomic } from '../utils/atomic-write.js';
4
4
  import { ownerResolver } from '../core/ownership/owner-resolver.js';
5
+ import { readExplicitAllowExternalContacts } from '../config/access-policy.js';
6
+ import { isInternalEntry, ownerRefAid } from './owner-directory-scope.js';
5
7
  import { OwnerDirectoryCache, OWNER_DIRECTORY_INITIAL_WAIT_MS, OWNER_DIRECTORY_SCOPES, } from './owner-directory-cache.js';
6
8
  const liveDirectories = new Map();
7
9
  // Coalesce negative lookups from unrelated senders as well as concurrent calls.
@@ -11,7 +13,52 @@ export function registerOwnerDirectoryAdmission(input) {
11
13
  const current = liveDirectories.get(input.selfAid);
12
14
  if (current?.cache === input.cache)
13
15
  return;
14
- liveDirectories.set(input.selfAid, { ...input, missAttemptAt: {} });
16
+ liveDirectories.set(input.selfAid, { ...input, missAttemptAt: new Map() });
17
+ }
18
+ /**
19
+ * 取某个 agent 当前注册着的缓存;没有注册过(channel 未连、纯身份)时返回 undefined。
20
+ * 命令路径用它来复用准入路径同一个实例,共享模块级单飞。
21
+ */
22
+ export function liveOwnerDirectoryCache(selfAid) {
23
+ const live = liveDirectories.get(selfAid);
24
+ if (!live)
25
+ return undefined;
26
+ if (!live.isCurrent()) {
27
+ liveDirectories.delete(selfAid);
28
+ return undefined;
29
+ }
30
+ return live.cache;
31
+ }
32
+ /**
33
+ * 触发一次有界同步:处理待办的 pending 标记(拉取 + 落盘),最多等
34
+ * `OWNER_DIRECTORY_INITIAL_WAIT_MS`。超时后同步继续在后台跑。
35
+ *
36
+ * 和准入路径共用同一个活缓存实例,因此也共用模块级单飞——不会因为一次查询
37
+ * 多发一轮网络请求。
38
+ */
39
+ export async function syncOwnerDirectoryForLookup(selfAid) {
40
+ const cache = liveOwnerDirectoryCache(selfAid);
41
+ if (!cache)
42
+ return false;
43
+ const scope = OWNER_DIRECTORY_SCOPES[0];
44
+ if (cache.scopeAccess(scope) !== 'authorized')
45
+ return false;
46
+ const before = cache.readSnapshot(scope)?.contentDigest;
47
+ // 把 pending 里带的通知 digest 一起传下去:与共享缓存一致时 0 次网络请求。
48
+ // 未命中的场景恰恰常发生在"通知刚到、共享缓存已拉过",不传等于白拉一次。
49
+ const hint = cache.readPendingUpdate()?.digests;
50
+ let timer;
51
+ try {
52
+ await Promise.race([
53
+ cache.pullAndApply(hint).catch(() => undefined),
54
+ new Promise(resolve => { timer = setTimeout(resolve, OWNER_DIRECTORY_INITIAL_WAIT_MS); }),
55
+ ]);
56
+ }
57
+ finally {
58
+ if (timer)
59
+ clearTimeout(timer);
60
+ }
61
+ return cache.readSnapshot(scope)?.contentDigest !== before;
15
62
  }
16
63
  /** Cache misses get one bounded synchronization attempt before admission, using the live channel. */
17
64
  export async function prepareOwnerDirectoryAdmission(selfAid, aid) {
@@ -24,37 +71,23 @@ export async function prepareOwnerDirectoryAdmission(selfAid, aid) {
24
71
  }
25
72
  if (ownerResolver.owner(selfAid) !== live.ownerAid)
26
73
  return;
27
- const agents = live.cache.readSnapshot('agents');
28
- if (agents?.entries.some(entry => entry.aid === aid)) {
29
- // The higher-priority Agent category is already known. Only its stale refresh may run.
30
- void live.cache.lookupByAid('agents', aid).catch(() => { });
74
+ const scope = OWNER_DIRECTORY_SCOPES[0];
75
+ if (live.cache.scopeAccess(scope) !== 'authorized')
31
76
  return;
32
- }
33
- const contacts = live.cache.readSnapshot('contacts');
34
- if (contacts?.entries.some(entry => entry.aid === aid)
35
- && (agents || live.cache.scopeAccess('agents') === 'not_authorized')) {
36
- void live.cache.refreshIfNeeded().catch(() => { });
77
+ const found = live.cache.readSnapshot(scope)?.entries.some(entry => entry.aid === aid) ?? false;
78
+ if (found) {
79
+ // 已知成员:只允许一次后台刷新,不阻塞本条消息的处理。
80
+ void live.cache.lookupByAid(scope, aid).catch(() => { });
37
81
  return;
38
82
  }
39
- const tasks = [];
40
- for (const scope of OWNER_DIRECTORY_SCOPES) {
41
- if (live.cache.scopeAccess(scope) !== 'authorized')
42
- continue;
43
- const found = live.cache.readSnapshot(scope)?.entries.some(entry => entry.aid === aid);
44
- const now = Date.now();
45
- if (!found) {
46
- if (now - (live.missAttemptAt[scope] ?? 0) < MISS_REFRESH_INTERVAL_MS)
47
- continue;
48
- live.missAttemptAt[scope] = now;
49
- }
50
- tasks.push(live.cache.lookupByAid(scope, aid));
51
- }
52
- if (tasks.length === 0)
83
+ const now = Date.now();
84
+ if (now - (live.missAttemptAt.get(aid) ?? 0) < MISS_REFRESH_INTERVAL_MS)
53
85
  return;
86
+ live.missAttemptAt.set(aid, now);
54
87
  let timer;
55
88
  try {
56
89
  await Promise.race([
57
- Promise.allSettled(tasks),
90
+ live.cache.lookupByAid(scope, aid).catch(() => undefined),
58
91
  new Promise(resolve => { timer = setTimeout(resolve, OWNER_DIRECTORY_INITIAL_WAIT_MS); }),
59
92
  ]);
60
93
  }
@@ -63,33 +96,71 @@ export async function prepareOwnerDirectoryAdmission(selfAid, aid) {
63
96
  clearTimeout(timer);
64
97
  }
65
98
  }
66
- /** Local-only classification. A stale or changed Owner can never authorize an old snapshot. */
99
+ /**
100
+ * 外部联系开关的最终值:**显式配置 > 个人助理判据 > false**。
101
+ *
102
+ * 个人助理要联系 Owner 之外的人,所以对自己默认打开;其余 agent 默认关闭,
103
+ * 只有明确有外部业务需求时才由 Owner 打开。准入、注入、查询三处共用这一个判据。
104
+ */
105
+ export function resolveAllowExternalContacts(selfAid, snapshot,
106
+ /** 显式配置;缺省时从配置文件读。测试用来固定优先级。 */
107
+ explicit = readExplicitAllowExternalContacts(selfAid)) {
108
+ if (explicit !== undefined)
109
+ return explicit;
110
+ return !!snapshot?.personalAssistant?.aid && snapshot.personalAssistant.aid === selfAid;
111
+ }
112
+ /**
113
+ * 本地判定一条 AID 在名录中的身份。只读本地快照,不发起网络请求。
114
+ *
115
+ * 归属内外看条目的 `owner_aid`:等于根 `owner_aid` 是内部,不等是外部。
116
+ * 主体类型看 `type`:`agent` 与 `human` 走会话,其余(host / device / service)
117
+ * 不发消息、按 ability 调用。
118
+ *
119
+ * 返回 undefined 表示这个 Agent 从未接入名录(无授权也无快照),调用方应保持
120
+ * 旧的准入行为。
121
+ */
67
122
  export function readOwnerDirectoryMembership(selfAid, aid) {
68
123
  const directory = path.join(agentDir(selfAid), 'directory');
69
- const configured = ['authorization.json', 'authorization.json_', 'agents.json', 'contacts.json']
70
- .some(name => fs.existsSync(path.join(directory, name)));
124
+ // "从未接入名录"与"接入过但当前不可用"是两种结论,判据是盘上有没有痕迹。
125
+ // 原子写的热备名(`*.json_`)由 existsAtomic 负责,业务层不关心后缀。
126
+ const configured = existsAtomic(path.join(directory, 'authorization.json'))
127
+ || existsAtomic(path.join(directory, 'contacts.json'));
71
128
  if (!configured)
72
129
  return undefined;
73
- const unknown = { agents: 'unavailable', contacts: 'unavailable' };
74
130
  const ownerAid = ownerResolver.owner(selfAid);
75
131
  if (!ownerAid)
76
- return unknown;
132
+ return { category: 'unavailable' };
77
133
  try {
78
134
  const cache = new OwnerDirectoryCache({ selfAid, ownerAid, directory });
79
- const membership = { ...unknown };
80
- for (const scope of OWNER_DIRECTORY_SCOPES) {
81
- const access = cache.scopeAccess(scope);
82
- if (access === 'not_authorized')
83
- membership[scope] = 'not_authorized';
84
- else if (access === 'authorized') {
85
- const snapshot = cache.readSnapshot(scope);
86
- if (snapshot)
87
- membership[scope] = snapshot.entries.some(entry => entry.aid === aid) ? 'found' : 'not_found';
88
- }
135
+ const scope = OWNER_DIRECTORY_SCOPES[0];
136
+ if (cache.scopeAccess(scope) !== 'authorized')
137
+ return { category: 'unavailable' };
138
+ const snapshot = cache.readSnapshot(scope);
139
+ if (!snapshot)
140
+ return { category: 'unavailable' };
141
+ const effectiveAllowExternalContacts = resolveAllowExternalContacts(selfAid, snapshot);
142
+ if (aid === ownerAid || snapshot.owner?.aid === aid) {
143
+ return { category: 'owner', internal: true, effectiveAllowExternalContacts };
144
+ }
145
+ if (snapshot.personalAssistant?.aid === aid) {
146
+ return { category: 'assistant', internal: true, effectiveAllowExternalContacts };
89
147
  }
90
- return membership;
148
+ if (aid === selfAid)
149
+ return { category: 'agent', internal: true, effectiveAllowExternalContacts };
150
+ const entry = snapshot.entries.find(item => item.aid === aid);
151
+ if (!entry)
152
+ return { category: 'not_found', effectiveAllowExternalContacts };
153
+ // 内外判据只有一个来源:Owner 身份块里的 aid;缺失时回落到根 owner_aid(协议要求两者相等)。
154
+ const ownerRef = ownerRefAid(snapshot, ownerAid);
155
+ const internal = isInternalEntry(entry, ownerRef);
156
+ const type = (entry.type ?? '').toLowerCase();
157
+ if (type === 'agent')
158
+ return { category: 'agent', internal, entry, effectiveAllowExternalContacts };
159
+ if (type === 'human')
160
+ return { category: 'human', internal, entry, effectiveAllowExternalContacts };
161
+ return { category: 'other', internal, entry, effectiveAllowExternalContacts };
91
162
  }
92
163
  catch {
93
- return unknown;
164
+ return { category: 'unavailable' };
94
165
  }
95
166
  }
@@ -0,0 +1,89 @@
1
+ import crypto from 'node:crypto';
2
+ import fs from 'node:fs';
3
+ import path from 'node:path';
4
+ import { aidLocalDir } from '../paths.js';
5
+ const DEFAULT_CONCURRENCY = 4;
6
+ /**
7
+ * 名录更新后按差集触发 agent.md 刷新(ttlDays=0,绕过 TTL 直接 HEAD)。
8
+ * 不再做定时巡检——名录里不通信的成员靠名录 diff 主动触发,
9
+ * 正在通信的成员靠 SDK 的 ETag 观察机制(agentmd.changed → refreshAid)。
10
+ *
11
+ * 本机持有私钥的身份不下载:它的本地 agent.md 可能是待 `ec aid agentmd put` 的手改稿。
12
+ */
13
+ export class OwnerDirectoryAgentMdSync {
14
+ options;
15
+ concurrency;
16
+ inFlight = new Map();
17
+ inFlightTtl = new Map();
18
+ constructor(options) {
19
+ this.options = options;
20
+ this.concurrency = Math.max(1, options.concurrency ?? DEFAULT_CONCURRENCY);
21
+ }
22
+ /** 观察到名录成员 ETag 变化:立即向远端确认并下载。非成员或连接已失效返回 undefined。 */
23
+ refreshAid(aid) {
24
+ if (!this.options.isCurrent())
25
+ return undefined;
26
+ return this.syncOne(aid, 0);
27
+ }
28
+ /**
29
+ * 名录 diff 产生的一批 AID 需要刷新。并发执行,fire-and-forget。
30
+ * 进程内去重由 SDK 保证:checkAgentMd 命中本地缓存则直接返回。
31
+ */
32
+ refreshAids(aids) {
33
+ return Promise.all(aids.map(aid => this.syncOne(aid, 0)));
34
+ }
35
+ syncOne(aid, ttlDays) {
36
+ const existing = this.inFlight.get(aid);
37
+ const existingTtl = this.inFlightTtl.get(aid);
38
+ // Reuse in-flight when: same or lower ttl (sweep→refresh can reuse sweep;
39
+ // refresh→refresh always reuse; refresh→sweep can reuse refresh)
40
+ if (existing && existingTtl !== undefined && ttlDays <= existingTtl)
41
+ return existing;
42
+ const task = (existing ? existing.then(() => this.checkAndDownload(aid, ttlDays)) : this.checkAndDownload(aid, ttlDays))
43
+ .finally(() => {
44
+ if (this.inFlight.get(aid) === task) {
45
+ this.inFlight.delete(aid);
46
+ this.inFlightTtl.delete(aid);
47
+ }
48
+ });
49
+ this.inFlight.set(aid, task);
50
+ this.inFlightTtl.set(aid, ttlDays);
51
+ return task;
52
+ }
53
+ async checkAndDownload(aid, ttlDays) {
54
+ try {
55
+ if (fs.existsSync(path.join(aidLocalDir(aid, this.options.aunPath), 'private'))) {
56
+ return { aid, status: 'local-identity' };
57
+ }
58
+ const store = this.options.store();
59
+ if (!store)
60
+ return { aid, status: 'error', error: 'AID store unavailable' };
61
+ const checked = await store.checkAgentMd(aid, ttlDays);
62
+ if (!checked.ok)
63
+ return { aid, status: 'error', error: checked.error.message };
64
+ const check = checked.data;
65
+ if (!check.remote_found)
66
+ return { aid, status: 'missing' };
67
+ if (check.local_found && !check.needs_update) {
68
+ return { aid, status: 'unchanged', ...(check.verify_status ? { verifyStatus: check.verify_status } : {}) };
69
+ }
70
+ const fetched = await store.downloadAgentMd(aid);
71
+ if (!fetched.ok)
72
+ return { aid, status: 'error', error: fetched.error.message };
73
+ const verifyStatus = fetched.data.verification?.status;
74
+ const changed = contentEtag(fetched.data.content) !== check.local_etag;
75
+ if (changed)
76
+ this.options.onUpdated?.(aid);
77
+ return { aid, status: changed ? 'updated' : 'unchanged', ...(verifyStatus ? { verifyStatus } : {}) };
78
+ }
79
+ catch (error) {
80
+ return { aid, status: 'error', error: errorText(error) };
81
+ }
82
+ }
83
+ }
84
+ function contentEtag(content) {
85
+ return `"${crypto.createHash('sha256').update(content, 'utf8').digest('hex')}"`;
86
+ }
87
+ function errorText(error) {
88
+ return error instanceof Error ? error.message : String(error);
89
+ }