evolcore 0.0.45 → 0.0.46

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 (204) hide show
  1. package/CHANGELOG.md +75 -0
  2. package/bin/ecmsg.js +33 -23
  3. package/dist/ability/application-service.js +1 -2
  4. package/dist/ability/package.js +28 -0
  5. package/dist/agent-template/jobs.js +158 -36
  6. package/dist/agent-template/runtime.js +401 -34
  7. package/dist/agent-template/service.js +764 -369
  8. package/dist/agent-template/tool-runner.js +35 -17
  9. package/dist/agent-template/types.js +7 -4
  10. package/dist/agents/baseagent.js +10 -9
  11. package/dist/agents/claude-runner.js +13 -5
  12. package/dist/agents/codex/app-server-client.js +2 -4
  13. package/dist/agents/codex/diagnostics.js +18 -1
  14. package/dist/agents/codex-runner.js +3 -1
  15. package/dist/agents/ecagent-runner.js +3 -1
  16. package/dist/agents/gemini-runner.js +2 -1
  17. package/dist/aun/menu/client.js +142 -182
  18. package/dist/aun/rpc/client.js +33 -17
  19. package/dist/aun/service-proxy.js +6 -23
  20. package/dist/channels/aun.js +656 -112
  21. package/dist/cli/agent-create.js +37 -10
  22. package/dist/cli/agent-template.js +105 -84
  23. package/dist/cli/agent.js +31 -18
  24. package/dist/cli/daemon-commands.js +15 -17
  25. package/dist/cli/daemon-lifecycle-client.js +40 -18
  26. package/dist/cli/directory.js +12 -4
  27. package/dist/cli/ecmsg-gateway.js +5 -2
  28. package/dist/cli/events-command.js +2 -1
  29. package/dist/cli/init.js +50 -20
  30. package/dist/cli/integrity-command.js +2 -0
  31. package/dist/cli/managed-command-guard.js +20 -1
  32. package/dist/cli/menu-client.js +7 -5
  33. package/dist/cli/restart-monitor.js +33 -20
  34. package/dist/cli/services-command.js +28 -5
  35. package/dist/config/config-field-policy.js +24 -0
  36. package/dist/config/config-manager.js +20 -2
  37. package/dist/config/peer-book-store.js +152 -120
  38. package/dist/config/peer-role-resolver.js +24 -4
  39. package/dist/config/provider-identity-migration.js +1 -1
  40. package/dist/config/provider-identity.js +10 -4
  41. package/dist/config/provider-ipc-service.js +10 -2
  42. package/dist/config/provider-local-sync.js +2 -2
  43. package/dist/config/providers-config.js +95 -10
  44. package/dist/config/providers-import.js +4 -6
  45. package/dist/config/providers.js +14 -3
  46. package/dist/config/role-store.js +1 -1
  47. package/dist/config-store.js +9 -1
  48. package/dist/core/agent-application-service.js +56 -29
  49. package/dist/core/agent-create-status.js +3 -1
  50. package/dist/core/agent-creation-claim.js +1 -0
  51. package/dist/core/agent-ipc-registration.js +2 -2
  52. package/dist/core/agent-reload-coordinator.js +38 -1
  53. package/dist/core/agent-runtime.js +85 -38
  54. package/dist/core/auth/agent-delegation.js +5 -0
  55. package/dist/core/auth/operation-authorizer.js +1 -1
  56. package/dist/core/auth/operation-catalog.js +3 -3
  57. package/dist/core/bootstrap-service.js +3 -0
  58. package/dist/core/capability/capability-manager.js +3 -1
  59. package/dist/core/channel-loader.js +29 -3
  60. package/dist/core/channel-runtime.js +1 -1
  61. package/dist/core/command/agent-control.js +6 -5
  62. package/dist/core/command/agent-template-menu.js +32 -97
  63. package/dist/core/command/eventcenter-menu.js +61 -13
  64. package/dist/core/command/menu-catalog.js +11 -4
  65. package/dist/core/command/menu-handler.js +35 -8
  66. package/dist/core/command/menu-protocol.js +8 -16
  67. package/dist/core/command/skill-slash.js +6 -2
  68. package/dist/core/command/team-workspace-bootstrap.js +1 -2
  69. package/dist/core/command/team-workspace-management-store.js +83 -0
  70. package/dist/core/command/team-workspace-management.js +1 -2
  71. package/dist/core/command/team-workspace-team.js +26 -3
  72. package/dist/core/daemon-control.js +3 -4
  73. package/dist/core/daemon-lifecycle.js +146 -110
  74. package/dist/core/evolagent-registry.js +42 -0
  75. package/dist/core/evolagent.js +2 -1
  76. package/dist/core/file-integrity/read.js +9 -16
  77. package/dist/core/file-integrity/store.js +94 -34
  78. package/dist/core/handoff/dispatcher.js +33 -7
  79. package/dist/core/handoff/runtime.js +8 -0
  80. package/dist/core/inference/text-inference.js +12 -2
  81. package/dist/core/invoke/invoke-service.js +24 -3
  82. package/dist/core/ipc-registration.js +3 -0
  83. package/dist/core/message/aun-ipc-sender.js +109 -7
  84. package/dist/core/message/contact-admission.js +6 -2
  85. package/dist/core/message/message-bridge.js +13 -0
  86. package/dist/core/message/message-queue.js +131 -30
  87. package/dist/core/message/pending-hints.js +8 -0
  88. package/dist/core/message/response-engine.js +48 -2
  89. package/dist/core/message/send-routing.js +3 -12
  90. package/dist/core/model/catalog-service.js +13 -0
  91. package/dist/core/model/model-catalog.js +57 -18
  92. package/dist/core/pack-auth.js +163 -31
  93. package/dist/core/pack-install.js +42 -1
  94. package/dist/{services/ledger.js → core/pack-ledger.js} +17 -6
  95. package/dist/core/permission/ec-command-parser.js +3 -1
  96. package/dist/core/permission/shell-environment.js +23 -0
  97. package/dist/core/rate-window.js +6 -1
  98. package/dist/core/session/session-idle-monitor.js +0 -1
  99. package/dist/core/session/session-manager.js +14 -0
  100. package/dist/core/session/session-title.js +26 -6
  101. package/dist/core/skill/index-generator.js +3 -4
  102. package/dist/core/thread/thread-service.js +0 -1
  103. package/dist/core/thread/topic-id.js +8 -0
  104. package/dist/directory/owner-directory-admission.js +17 -0
  105. package/dist/directory/owner-directory-cache.js +3 -1
  106. package/dist/directory/owner-directory-markdown.js +145 -23
  107. package/dist/directory/owner-directory-policy.js +23 -0
  108. package/dist/directory/owner-directory-query-operation.js +20 -6
  109. package/dist/directory/owner-directory-search.js +47 -0
  110. package/dist/eck/message-renderer.js +171 -32
  111. package/dist/ecweb/basic-settings-service.js +7 -3
  112. package/dist/ecweb/providers-service.js +17 -10
  113. package/dist/ecweb/runtime/lifecycle.js +1 -1
  114. package/dist/ecweb/runtime/supervisor.js +77 -35
  115. package/dist/event-center/admission-materializer.js +1 -1
  116. package/dist/event-center/bus-gate.js +48 -4
  117. package/dist/event-center/bus-host.js +3 -3
  118. package/dist/event-center/center-config.js +57 -28
  119. package/dist/event-center/dispatcher.js +8 -0
  120. package/dist/event-center/event-log.js +69 -27
  121. package/dist/event-center/index-generator.js +6 -1
  122. package/dist/event-center/ipc-operation.js +66 -25
  123. package/dist/event-center/pack-loader.js +12 -55
  124. package/dist/event-center/pack-manager.js +5 -3
  125. package/dist/event-center/registry.js +48 -52
  126. package/dist/event-center/runtime.js +219 -81
  127. package/dist/event-center/script-runner.js +18 -7
  128. package/dist/event-center/types.js +9 -1
  129. package/dist/event-center/validation.js +33 -4
  130. package/dist/index.js +96 -42
  131. package/dist/ipc.js +33 -7
  132. package/dist/product.js +1 -0
  133. package/dist/proxy/manager.js +52 -11
  134. package/dist/proxy/service-supervisor.js +265 -67
  135. package/dist/services/dispatcher.js +26 -3
  136. package/dist/services/idempotency.js +128 -0
  137. package/dist/services/identity.js +3 -27
  138. package/dist/services/index-generator.js +5 -1
  139. package/dist/services/index.js +244 -45
  140. package/dist/services/ipc-operation.js +164 -20
  141. package/dist/services/pack-manager.js +37 -9
  142. package/dist/services/router.js +100 -31
  143. package/dist/services/script-runner.js +15 -4
  144. package/dist/services/service-payload.js +50 -2
  145. package/dist/services/validation.js +107 -3
  146. package/dist/trigger/scheduler.js +14 -4
  147. package/dist/trigger/script-sandbox.js +39 -2
  148. package/dist/utils/cross-platform.js +35 -11
  149. package/dist/utils/error-utils.js +13 -1
  150. package/dist/utils/instance-registry.js +33 -35
  151. package/dist/utils/process-introspect.js +35 -20
  152. package/dist/utils/project-path.js +47 -3
  153. package/dist/utils/windows-shell-trust.js +174 -68
  154. package/kits/docs/context-assembly.md +3 -3
  155. package/kits/docs/evolcore/agent.md +28 -10
  156. package/kits/docs/evolcore/config.md +22 -0
  157. package/kits/docs/evolcore/directory.md +46 -12
  158. package/kits/docs/evolcore/event-center.md +6 -6
  159. package/kits/docs/evolcore/group-collaboration.md +1 -1
  160. package/kits/docs/evolcore/menu-client.md +7 -5
  161. package/kits/docs/evolcore/menu-protocol.md +5 -3
  162. package/kits/docs/evolcore/msg.md +10 -3
  163. package/kits/docs/evolcore/provider.md +7 -3
  164. package/kits/docs/evolcore/services.md +145 -12
  165. package/kits/docs/evolcore/thread-chain.md +17 -9
  166. package/kits/docs/prompt-loading-architecture.md +4 -4
  167. package/kits/eck_message_manifest.json +46 -3
  168. package/kits/rules/04-relation.md +10 -2
  169. package/kits/rules/06-channel.md +2 -1
  170. package/kits/schemas/providers.schema.2.json +1 -0
  171. package/kits/skills/ec-group/SKILL.md +4 -0
  172. package/kits/skills/ec-skill/scripts/cli.mjs +1 -1
  173. package/kits/skills/ec-skill/scripts/skill.mjs +13 -2
  174. package/kits/skills/thread-ops/SKILL.md +8 -5
  175. package/kits/templates/message-fragments/group-batch-item.md +3 -0
  176. package/kits/templates/message-fragments/group-batch.md +1 -0
  177. package/kits/templates/message-fragments/item-group.md +4 -0
  178. package/kits/templates/message-fragments/item-private.md +4 -0
  179. package/kits/templates/message-fragments/thread-context.md +1 -1
  180. package/kits/templates/system-fragments/commands.md +5 -5
  181. package/kits/templates/system-fragments/managed-runtime.md +1 -1
  182. package/kits/templates/system-fragments/relation.md +11 -7
  183. package/package.json +3 -2
  184. package/scripts/agent-template/index.mjs +9 -4
  185. package/scripts/agent-template/inspect.mjs +16 -9
  186. package/scripts/agent-template/shared/plan.mjs +3 -33
  187. package/scripts/agent-template/shared/template-contract.mjs +186 -3
  188. package/scripts/agent-template/shared/template-package.mjs +119 -10
  189. package/scripts/agent-template/shared/template-runtime.mjs +49 -6
  190. package/scripts/agent-template/shared/template.mjs +5 -5
  191. package/scripts/agent-template/team-pack.mjs +245 -0
  192. package/scripts/agent-template/team-preview.mjs +111 -0
  193. package/scripts/aid-batch/identity-batch.mjs +143 -0
  194. package/dist/ability/versions.js +0 -32
  195. package/dist/aun/model-headers.js +0 -17
  196. package/dist/channels/builtin-plugins.js +0 -19
  197. package/dist/core/command/team-workspace-initializer.js +0 -2
  198. package/dist/core/command/team-workspace-job-store.js +0 -88
  199. package/dist/core/command/team-workspace-management-service.js +0 -2
  200. package/dist/core/contact-bind-registry.js +0 -27
  201. package/dist/event-center/ledger.js +0 -52
  202. package/dist/services/reply.js +0 -55
  203. package/kits/templates/message-fragments/item.md +0 -3
  204. package/scripts/agent-template/pack.mjs +0 -169
@@ -46,7 +46,7 @@ function readInstanceRecords(name) {
46
46
  || record.pid !== Number(match[1])
47
47
  || typeof record.startedAt !== 'number'
48
48
  || !Number.isFinite(record.startedAt)
49
- || record.startedAt <= 0
49
+ || record.startedAt < 0
50
50
  || !Number.isInteger(record.port))
51
51
  continue;
52
52
  output.push({
@@ -94,6 +94,8 @@ async function checkHealth(service, pid) {
94
94
  const response = await fetch('http://127.0.0.1:' + service.port + (health.path ?? '/'), {
95
95
  signal: AbortSignal.timeout(timeoutMs),
96
96
  });
97
+ // Status-only probes must release bodies that may continue streaming.
98
+ await response.body?.cancel().catch(() => { });
97
99
  return response.ok;
98
100
  }
99
101
  catch {
@@ -108,6 +110,7 @@ export class ServiceSupervisor {
108
110
  desired = new Map();
109
111
  operations = new Map();
110
112
  stopped = false;
113
+ stopPromise;
111
114
  logLifecycle(active, event, details = {}) {
112
115
  const record = {
113
116
  ts: new Date().toISOString(), event, service: active.config.name,
@@ -140,6 +143,8 @@ export class ServiceSupervisor {
140
143
  this.log = log;
141
144
  }
142
145
  async start(services) {
146
+ if (this.stopPromise)
147
+ await this.stopPromise;
143
148
  this.stopped = false;
144
149
  await this.reconcile(services);
145
150
  }
@@ -147,9 +152,31 @@ export class ServiceSupervisor {
147
152
  if (this.stopped)
148
153
  return;
149
154
  const next = new Map(services.map(service => [service.name, service]));
155
+ this.desired.clear();
156
+ for (const [name, service] of next)
157
+ this.desired.set(name, service);
158
+ const applying = [];
159
+ // Invalidate obsolete readiness before queueing configuration work. An
160
+ // explicit start may be holding that queue until its health deadline.
161
+ for (const [name, active] of this.active) {
162
+ const desired = next.get(name);
163
+ if (active.startPromise && (!desired || desired.enabled !== true
164
+ || serviceRuntimeMode(desired) !== serviceRuntimeMode(active.config)
165
+ || serviceConfigHash(desired) !== active.configHash)) {
166
+ active.intentionalStop = true;
167
+ active.generation += 1;
168
+ }
169
+ }
150
170
  for (const [name, active] of this.active) {
151
- await this.queueOperation(name, async () => {
152
- if (this.active.get(name) !== active)
171
+ const desired = next.get(name);
172
+ // An unchanged entry has no lifecycle mutation to serialize. Do not
173
+ // make the whole configuration apply join its explicit readiness wait.
174
+ if (desired?.enabled === true
175
+ && !(name === 'ecweb' && serviceRuntimeMode(desired) === 'managed')
176
+ && stableHash(desired) === stableHash(active.config))
177
+ continue;
178
+ applying.push(this.queueOperation(name, async () => {
179
+ if (this.stopped || this.active.get(name) !== active)
153
180
  return;
154
181
  const desired = next.get(name);
155
182
  if (!desired || desired.enabled !== true
@@ -187,38 +214,57 @@ export class ServiceSupervisor {
187
214
  this.scheduleHealthCheck(active);
188
215
  }
189
216
  }
190
- });
217
+ }));
191
218
  }
192
- this.desired.clear();
193
- for (const service of services)
194
- this.desired.set(service.name, service);
195
219
  for (const service of services) {
196
- if (service.enabled !== true || this.active.has(service.name))
220
+ if (service.enabled !== true)
197
221
  continue;
198
222
  if (service.name === 'ecweb' && serviceRuntimeMode(service) === 'managed')
199
223
  continue;
200
- if (serviceRuntimeMode(service) === 'external') {
201
- const active = {
202
- config: service, process: null, pid: 0, startedAt: 0, recordPath: '',
203
- configHash: serviceConfigHash(service), generation: 1, state: service.port ? 'starting' : 'unknown',
204
- failureCount: 0, restartCount: 0, failures: [], healthFailures: 0, healthCheckInFlight: false,
205
- intentionalStop: false, stopping: false,
206
- };
207
- this.active.set(service.name, active);
208
- void this.monitorHealth(active).finally(() => this.scheduleHealthCheck(active));
224
+ const existing = this.active.get(service.name);
225
+ if (existing && stableHash(service) === stableHash(existing.config))
209
226
  continue;
210
- }
211
- const active = await this.adoptOrCreate(service);
212
- if (this.stopped)
213
- return;
214
- this.active.set(service.name, active);
215
- if (active.state !== 'ready') {
216
- void this.queueOperation(service.name, () => this.startEntry(active)).catch(() => { });
217
- }
218
- else if (!active.healthy) {
219
- void this.verifyAdopted(active);
220
- }
227
+ // Adoption also mutates the lifecycle. Serialize it with explicit
228
+ // start/stop so a late identity result cannot replace their instance.
229
+ applying.push(this.queueOperation(service.name, async () => {
230
+ if (this.stopped || this.active.has(service.name) || this.desired.get(service.name) !== service)
231
+ return;
232
+ if (serviceRuntimeMode(service) === 'external') {
233
+ const active = {
234
+ config: service, process: null, pid: 0, startedAt: 0, recordPath: '',
235
+ configHash: serviceConfigHash(service), generation: 1, state: service.port ? 'starting' : 'unknown',
236
+ failureCount: 0, restartCount: 0, failures: [], healthFailures: 0, healthCheckInFlight: false,
237
+ intentionalStop: false, stopping: false,
238
+ };
239
+ this.active.set(service.name, active);
240
+ void this.monitorHealth(active).finally(() => this.scheduleHealthCheck(active));
241
+ return;
242
+ }
243
+ const active = await this.adoptOrCreate(service);
244
+ if (this.stopped || this.desired.get(service.name) !== service)
245
+ return;
246
+ this.active.set(service.name, active);
247
+ if (active.state !== 'ready') {
248
+ void this.queueOperation(service.name, async () => {
249
+ if (this.stopped || this.active.get(service.name) !== active || active.intentionalStop)
250
+ return;
251
+ // Keep readiness outside the admission queue. startPromise still
252
+ // joins explicit starts, while configuration and stop can proceed.
253
+ void this.startEntry(active).catch(() => { });
254
+ }).catch(() => { });
255
+ }
256
+ else if (!active.healthy) {
257
+ void this.verifyAdopted(active);
258
+ }
259
+ }));
221
260
  }
261
+ // A readiness wait for one service must not hold up another service's
262
+ // configuration. Each service keeps its own operation order. Join every
263
+ // dispatched operation before surfacing an error to the configuration queue.
264
+ const results = await Promise.allSettled(applying);
265
+ const failure = results.find(result => result.status === 'rejected');
266
+ if (failure?.status === 'rejected')
267
+ throw failure.reason;
222
268
  }
223
269
  async reload(name) {
224
270
  return this.restartService(name);
@@ -246,8 +292,12 @@ export class ServiceSupervisor {
246
292
  let active = this.active.get(name);
247
293
  if (!active) {
248
294
  active = await this.adoptOrCreate(service);
249
- if (this.stopped)
295
+ const current = this.desired.get(name);
296
+ if (this.stopped || !current || current.enabled !== true
297
+ || current.runtime?.mode !== 'managed' || serviceConfigHash(current) !== active.configHash) {
250
298
  throw Object.assign(new Error('service start cancelled: ' + name), { code: 'SERVICE_START_CANCELLED' });
299
+ }
300
+ active.config = current;
251
301
  this.active.set(name, active);
252
302
  }
253
303
  await this.startEntry(active);
@@ -284,16 +334,30 @@ export class ServiceSupervisor {
284
334
  .map(active => this.snapshotOne(active))
285
335
  .sort((a, b) => a.name.localeCompare(b.name));
286
336
  }
287
- async stop() {
337
+ stop() {
338
+ if (this.stopPromise)
339
+ return this.stopPromise;
288
340
  if (this.stopped)
289
- return;
341
+ return Promise.resolve();
290
342
  this.stopped = true;
291
- for (const active of [...this.active.values()]) {
343
+ // Services have independent lifecycles. Request every stop before waiting
344
+ // for exits, and join all cleanups even if one observation fails.
345
+ const cleanups = [...this.active.values()].map(async (active) => {
292
346
  const stopped = await this.stopEntry(active, true);
293
347
  await active.startPromise?.catch(() => { });
294
348
  if (stopped)
295
349
  this.active.delete(active.config.name);
296
- }
350
+ });
351
+ const promise = Promise.allSettled(cleanups).then(results => {
352
+ const failure = results.find(result => result.status === 'rejected');
353
+ if (failure?.status === 'rejected')
354
+ throw failure.reason;
355
+ }).finally(() => {
356
+ if (this.stopPromise === promise)
357
+ this.stopPromise = undefined;
358
+ });
359
+ this.stopPromise = promise;
360
+ return promise;
297
361
  }
298
362
  async clearFailure(name) {
299
363
  return this.queueOperation(name, async () => {
@@ -315,17 +379,19 @@ export class ServiceSupervisor {
315
379
  }
316
380
  async verifyAdopted(active) {
317
381
  const generation = active.generation;
318
- if (active.state !== 'starting' || !active.pid || !(await platform.isProcessRunningAsync(active.pid)))
382
+ const pid = active.pid;
383
+ if (active.state !== 'starting' || !pid || !(await platform.isProcessRunningAsync(pid)))
319
384
  return;
320
385
  if (generation !== active.generation || active.stopping || this.stopped)
321
386
  return;
322
- const healthy = await checkHealth(active.config, active.pid);
387
+ const healthy = await this.checkManagedHealth(active) && active.pid === pid;
323
388
  if (generation !== active.generation || active.stopping || this.stopped)
324
389
  return;
325
390
  active.checkedAt = Date.now();
326
391
  active.healthy = healthy;
327
392
  active.state = healthy ? 'ready' : 'failed';
328
393
  if (healthy) {
394
+ this.cancelScheduledRestart(active);
329
395
  if (active.recordPath) {
330
396
  atomicWriteJson(active.recordPath, {
331
397
  owner: 'evolcore-service-supervisor',
@@ -347,13 +413,22 @@ export class ServiceSupervisor {
347
413
  const hash = serviceConfigHash(service);
348
414
  const records = readInstanceRecords(service.name);
349
415
  const liveRecords = [];
416
+ const unverifiedRecords = [];
350
417
  for (const record of records) {
351
- if ((await platform.isProcessRunningAsync(record.pid))
352
- && startTimeMatches(record.startedAt, await getProcessStartTimeAsync(record.pid)))
418
+ if (!(await platform.isProcessRunningAsync(record.pid)))
419
+ continue;
420
+ const startedAt = await getProcessStartTimeAsync(record.pid);
421
+ // A live record without a verified identity still blocks replacement.
422
+ // In particular, startedAt=0 from an interrupted spawn is not stale.
423
+ if (record.startedAt === 0 || startedAt === null)
424
+ unverifiedRecords.push(record);
425
+ else if (startTimeMatches(record.startedAt, startedAt))
353
426
  liveRecords.push(record);
354
427
  }
355
428
  liveRecords.sort((a, b) => b.startedAt - a.startedAt);
356
- const adopted = liveRecords.find(record => record.configHash === hash);
429
+ unverifiedRecords.sort((a, b) => b.startedAt - a.startedAt);
430
+ const adopted = liveRecords.find(record => record.configHash === hash)
431
+ ?? unverifiedRecords.find(record => record.configHash === hash);
357
432
  if (adopted) {
358
433
  return {
359
434
  config: service, process: null, pid: adopted.pid, startedAt: adopted.startedAt,
@@ -363,7 +438,7 @@ export class ServiceSupervisor {
363
438
  intentionalStop: false, stopping: false, adoptionAllowed: true,
364
439
  };
365
440
  }
366
- const staleConfig = liveRecords[0];
441
+ const staleConfig = unverifiedRecords[0] ?? liveRecords[0];
367
442
  if (staleConfig) {
368
443
  this.log.warn('[ServiceSupervisor] managed process config changed; replacing recorded process after exit confirmation: service=' + service.name + ' pid=' + staleConfig.pid);
369
444
  return {
@@ -424,6 +499,8 @@ export class ServiceSupervisor {
424
499
  this.scheduleRestart(active);
425
500
  }
426
501
  }
502
+ if (active.lastErrorCode === 'PROCESS_IDENTITY_UNVERIFIED')
503
+ this.scheduleHealthCheck(active);
427
504
  throw error;
428
505
  }).finally(() => {
429
506
  if (active.startPromise === promise)
@@ -440,8 +517,9 @@ export class ServiceSupervisor {
440
517
  if (generation !== active.generation || this.stopped || active.stopping || active.intentionalStop)
441
518
  return;
442
519
  if (running) {
443
- if (active.adoptionAllowed !== false && startTimeMatches(active.startedAt, await getProcessStartTimeAsync(active.pid))) {
444
- const healthy = await checkHealth(active.config, active.pid);
520
+ if (active.adoptionAllowed !== false) {
521
+ const pid = active.pid;
522
+ const healthy = await this.checkManagedHealth(active) && active.pid === pid;
445
523
  if (generation !== active.generation || this.stopped || active.intentionalStop) {
446
524
  throw Object.assign(new Error('service start cancelled: ' + active.config.name), { code: 'SERVICE_START_CANCELLED' });
447
525
  }
@@ -449,6 +527,8 @@ export class ServiceSupervisor {
449
527
  active.healthy = healthy;
450
528
  if (healthy) {
451
529
  active.state = 'ready';
530
+ this.cancelScheduledRestart(active);
531
+ active.healthFailures = 0;
452
532
  active.lastError = undefined;
453
533
  active.lastErrorCode = undefined;
454
534
  if (active.recordPath) {
@@ -464,7 +544,7 @@ export class ServiceSupervisor {
464
544
  }
465
545
  }
466
546
  if (!await this.stopEntry(active, false)) {
467
- throw Object.assign(new Error('managed process did not exit; refusing to start a replacement: ' + active.config.name), { code: 'PROCESS_STOP_FAILED' });
547
+ throw Object.assign(new Error(active.lastError ?? 'managed process did not exit; refusing to start a replacement: ' + active.config.name), { code: active.lastErrorCode ?? 'PROCESS_STOP_FAILED' });
468
548
  }
469
549
  }
470
550
  if (active.config.port) {
@@ -575,6 +655,14 @@ export class ServiceSupervisor {
575
655
  });
576
656
  child.unref();
577
657
  const identity = active.identityPromise = getProcessStartTimeAsync(pid);
658
+ // Preserve ownership even when shutdown overtakes the first identity
659
+ // query. Unknown identity is a blocking registration, never readiness.
660
+ fs.mkdirSync(resolvePaths().instanceDir, { recursive: true });
661
+ atomicWriteJson(active.recordPath, {
662
+ owner: 'evolcore-service-supervisor', pid, startedAt: 0,
663
+ port: active.config.port ?? 0, configHash: active.configHash,
664
+ state: 'starting', attemptId: active.attemptId,
665
+ });
578
666
  const startedAt = await identity;
579
667
  if (active.identityPromise === identity)
580
668
  active.identityPromise = undefined;
@@ -615,6 +703,7 @@ export class ServiceSupervisor {
615
703
  attemptId: active.attemptId,
616
704
  });
617
705
  active.state = 'ready';
706
+ this.cancelScheduledRestart(active);
618
707
  this.scheduleHealthCheck(active);
619
708
  this.log.info('[ServiceSupervisor] ready service=' + active.config.name + ' pid=' + pid);
620
709
  this.logLifecycle(active, 'service.ready');
@@ -664,7 +753,7 @@ export class ServiceSupervisor {
664
753
  active.state = 'reloading';
665
754
  this.log.info('[ServiceSupervisor] restart service=' + active.config.name + ' reason=' + reason);
666
755
  if (!await this.stopEntry(active, false)) {
667
- throw Object.assign(new Error('managed process did not exit; refusing to start a replacement: ' + active.config.name), { code: 'PROCESS_STOP_FAILED' });
756
+ throw Object.assign(new Error(active.lastError ?? 'managed process did not exit; refusing to start a replacement: ' + active.config.name), { code: active.lastErrorCode ?? 'PROCESS_STOP_FAILED' });
668
757
  }
669
758
  await active.startPromise?.catch(() => { });
670
759
  active.stopping = false;
@@ -678,6 +767,9 @@ export class ServiceSupervisor {
678
767
  const promise = this.runStopEntry(active, intentional).finally(() => {
679
768
  if (active.stopPromise === promise)
680
769
  active.stopPromise = undefined;
770
+ // A failed replacement still owns a live process. Keep observing it;
771
+ // explicit stops and shutdown remain excluded by scheduleHealthCheck.
772
+ this.scheduleHealthCheck(active);
681
773
  });
682
774
  active.stopPromise = promise;
683
775
  return promise;
@@ -696,8 +788,8 @@ export class ServiceSupervisor {
696
788
  const pid = active.pid;
697
789
  const expectedStartedAt = active.startedAt || await active.identityPromise || 0;
698
790
  if (pid > 0 && (await platform.isProcessRunningAsync(pid))) {
699
- const actualStartedAt = await getProcessStartTimeAsync(pid);
700
- if (actualStartedAt === null && (await platform.isProcessRunningAsync(pid))) {
791
+ const actualStartedAt = await getProcessStartTimeAsync(pid, { fresh: true });
792
+ if ((expectedStartedAt <= 0 || actualStartedAt === null) && (await platform.isProcessRunningAsync(pid))) {
701
793
  active.state = 'failed';
702
794
  active.lastError = 'cannot verify managed process identity; refusing to stop or replace it (pid=' + pid + ')';
703
795
  active.lastErrorCode = 'PROCESS_IDENTITY_UNVERIFIED';
@@ -711,14 +803,14 @@ export class ServiceSupervisor {
711
803
  }
712
804
  catch { }
713
805
  let exited = await platform.waitForProcessExit(pid, active.config.runtime?.stopTimeoutMs ?? 10000);
714
- if (!exited && startTimeMatches(expectedStartedAt, await getProcessStartTimeAsync(pid))) {
806
+ if (!exited && startTimeMatches(expectedStartedAt, await getProcessStartTimeAsync(pid, { fresh: true }))) {
715
807
  try {
716
808
  platform.killProcessTree(pid, true);
717
809
  }
718
810
  catch { }
719
811
  exited = await platform.waitForProcessExit(pid, 5000);
720
812
  }
721
- const remainingStartedAt = !exited && (await platform.isProcessRunningAsync(pid)) ? await getProcessStartTimeAsync(pid) : undefined;
813
+ const remainingStartedAt = !exited && (await platform.isProcessRunningAsync(pid)) ? await getProcessStartTimeAsync(pid, { fresh: true }) : undefined;
722
814
  if (remainingStartedAt === null || (remainingStartedAt !== undefined && startTimeMatches(expectedStartedAt, remainingStartedAt))) {
723
815
  active.state = 'failed';
724
816
  active.lastError = 'process did not exit after graceful and forced termination (pid=' + pid + ')';
@@ -748,6 +840,24 @@ export class ServiceSupervisor {
748
840
  this.logLifecycle(active, 'service.stopped', { pid });
749
841
  return true;
750
842
  }
843
+ cancelScheduledRestart(active) {
844
+ if (active.restartTimer)
845
+ clearTimeout(active.restartTimer);
846
+ // Clearing the handle also invalidates an already queued timer callback.
847
+ active.restartTimer = undefined;
848
+ }
849
+ hasRecoveryBudget(active) {
850
+ const now = Date.now();
851
+ active.failures = active.failures.filter(timestamp => now - timestamp <= SERVICE_RESTART_WINDOW_MS);
852
+ active.failureCount = active.failures.length;
853
+ if (active.failures.length < SERVICE_RESTART_MAX_ATTEMPTS)
854
+ return true;
855
+ active.retryExhausted = true;
856
+ active.state = 'failed';
857
+ this.log.error('[ServiceSupervisor] restart limit reached service=' + active.config.name);
858
+ this.logLifecycle(active, 'service.recovery.exhausted', { cause: active.lastError, code: active.lastErrorCode });
859
+ return false;
860
+ }
751
861
  scheduleRestart(active) {
752
862
  if (this.stopped || active.intentionalStop || active.restartTimer || active.config.enabled !== true
753
863
  || active.config.runtime?.mode !== 'managed')
@@ -760,27 +870,50 @@ export class ServiceSupervisor {
760
870
  return;
761
871
  if (active.retryExhausted)
762
872
  return;
763
- const now = Date.now();
764
- active.failures = active.failures.filter(timestamp => now - timestamp <= SERVICE_RESTART_WINDOW_MS);
765
- if (active.failures.length >= SERVICE_RESTART_MAX_ATTEMPTS) {
766
- active.retryExhausted = true;
767
- active.state = 'failed';
768
- this.log.error('[ServiceSupervisor] restart limit reached service=' + active.config.name);
769
- this.logLifecycle(active, 'service.recovery.exhausted', { cause: active.lastError, code: active.lastErrorCode });
873
+ if (!this.hasRecoveryBudget(active))
770
874
  return;
771
- }
772
- active.failures.push(now);
773
875
  const base = restart?.backoffMs ?? SERVICE_RESTART_BACKOFF_MS;
774
- const delay = base * (2 ** (active.failures.length - 1));
876
+ const delay = base * (2 ** active.failures.length);
877
+ const generation = active.generation;
775
878
  active.state = 'backoff';
776
- active.failureCount = active.failures.length;
777
- active.restartTimer = setTimeout(() => {
778
- active.restartTimer = undefined;
779
- active.restartCount += 1;
879
+ const timer = active.restartTimer = setTimeout(() => {
880
+ // Keep the handle while queued so successful starts and policy changes
881
+ // can cancel this attempt even after the timeout has fired.
780
882
  void this.queueOperation(active.config.name, async () => {
781
- if (this.stopped || active.intentionalStop || this.active.get(active.config.name) !== active)
782
- return;
783
- await this.restartEntry(active, 'automatic recovery');
883
+ const valid = () => !this.stopped && !active.intentionalStop && !active.stopping
884
+ && this.active.get(active.config.name) === active && active.generation === generation
885
+ && active.restartTimer === timer && (active.state === 'backoff' || active.state === 'failed')
886
+ && active.config.enabled === true && active.config.runtime?.restart?.enabled !== false;
887
+ try {
888
+ if (!valid())
889
+ return;
890
+ const pid = active.pid;
891
+ const healthy = await this.checkManagedHealth(active);
892
+ if (!valid())
893
+ return;
894
+ if (healthy && active.pid === pid) {
895
+ active.state = 'ready';
896
+ active.healthy = true;
897
+ active.checkedAt = Date.now();
898
+ active.healthFailures = 0;
899
+ active.lastError = undefined;
900
+ active.lastErrorCode = undefined;
901
+ this.cancelScheduledRestart(active);
902
+ this.scheduleHealthCheck(active);
903
+ return;
904
+ }
905
+ if (!this.hasRecoveryBudget(active))
906
+ return;
907
+ this.cancelScheduledRestart(active);
908
+ active.failures.push(Date.now());
909
+ active.failureCount = active.failures.length;
910
+ active.restartCount += 1;
911
+ await this.restartEntry(active, 'automatic recovery');
912
+ }
913
+ finally {
914
+ if (active.restartTimer === timer)
915
+ active.restartTimer = undefined;
916
+ }
784
917
  }).catch(error => this.log.warn('[ServiceSupervisor] automatic recovery failed service=' + active.config.name + ': ' + String(error)));
785
918
  }, delay);
786
919
  active.restartTimer.unref?.();
@@ -832,18 +965,71 @@ export class ServiceSupervisor {
832
965
  }
833
966
  return;
834
967
  }
835
- const healthy = (serviceRuntimeMode(active.config) === 'external'
836
- || startTimeMatches(active.startedAt, await getProcessStartTimeAsync(active.pid)))
837
- && await checkHealth(active.config, active.pid);
968
+ const pid = active.pid;
969
+ let identityVerified = false;
970
+ const identityBlocked = active.lastErrorCode === 'PROCESS_IDENTITY_UNVERIFIED';
971
+ const stopBlocked = active.lastErrorCode === 'PROCESS_STOP_FAILED';
972
+ if (serviceRuntimeMode(active.config) === 'managed' && (identityBlocked || stopBlocked)) {
973
+ const running = await platform.isProcessRunningAsync(pid);
974
+ if (generation !== active.generation || active.pid !== pid || active.stopping || this.stopped)
975
+ return;
976
+ const actualStartedAt = running && active.startedAt > 0 ? await getProcessStartTimeAsync(pid) : null;
977
+ if (generation !== active.generation || active.pid !== pid || active.stopping || this.stopped)
978
+ return;
979
+ identityVerified = actualStartedAt !== null && startTimeMatches(active.startedAt, actualStartedAt);
980
+ const originalExited = !running || (actualStartedAt !== null && !startTimeMatches(active.startedAt, actualStartedAt));
981
+ // Captured children report exit themselves. Recreated supervisors
982
+ // only have the registration, so keep observing it until exit is known.
983
+ if (originalExited && !active.process) {
984
+ if (active.recordPath) {
985
+ try {
986
+ fs.unlinkSync(active.recordPath);
987
+ }
988
+ catch { }
989
+ }
990
+ active.pid = 0;
991
+ active.startedAt = 0;
992
+ active.healthy = false;
993
+ active.lastError = 'recorded process exited while replacement was blocked';
994
+ active.lastErrorCode = 'PROCESS_EXITED';
995
+ active.state = 'failed';
996
+ this.scheduleRestart(active);
997
+ return;
998
+ }
999
+ if (active.adoptionAllowed === false && identityVerified && identityBlocked) {
1000
+ // The old identity is known again, but its command is obsolete.
1001
+ // Health alone must not accept it as the desired configuration.
1002
+ active.lastError = 'recorded service configuration requires replacement';
1003
+ active.lastErrorCode = undefined;
1004
+ this.scheduleRestart(active);
1005
+ return;
1006
+ }
1007
+ }
1008
+ const healthy = serviceRuntimeMode(active.config) === 'external'
1009
+ ? await checkHealth(active.config, pid)
1010
+ : await this.checkManagedHealth(active) && active.pid === pid;
838
1011
  if (generation !== active.generation || active.stopping || this.stopped)
839
1012
  return;
840
1013
  active.healthy = healthy;
841
1014
  active.checkedAt = Date.now();
842
1015
  if (active.healthy) {
1016
+ if (identityBlocked || stopBlocked) {
1017
+ if (active.recordPath) {
1018
+ atomicWriteJson(active.recordPath, {
1019
+ owner: 'evolcore-service-supervisor', pid: active.pid, startedAt: active.startedAt,
1020
+ port: active.config.port ?? 0, configHash: active.configHash, state: 'ready',
1021
+ });
1022
+ }
1023
+ active.lastError = undefined;
1024
+ active.lastErrorCode = undefined;
1025
+ }
1026
+ this.cancelScheduledRestart(active);
843
1027
  active.healthFailures = 0;
844
1028
  active.state = 'ready';
845
1029
  return;
846
1030
  }
1031
+ if (identityVerified && identityBlocked)
1032
+ active.lastErrorCode = undefined;
847
1033
  active.healthFailures += 1;
848
1034
  const threshold = active.config.health?.failureThreshold ?? 3;
849
1035
  if (active.healthFailures < threshold)
@@ -857,6 +1043,18 @@ export class ServiceSupervisor {
857
1043
  active.healthCheckInFlight = false;
858
1044
  }
859
1045
  }
1046
+ async checkManagedHealth(active) {
1047
+ const { pid, startedAt, config } = active;
1048
+ if (pid <= 0 || startedAt <= 0 || active.adoptionAllowed === false
1049
+ || !startTimeMatches(startedAt, await getProcessStartTimeAsync(pid)))
1050
+ return false;
1051
+ if (!await checkHealth(config, pid))
1052
+ return false;
1053
+ // A successful HTTP response may arrive after process exit or PID reuse.
1054
+ // Verify the same OS process again before accepting it as healthy.
1055
+ const actualStartedAt = await getProcessStartTimeAsync(pid);
1056
+ return await platform.isProcessRunningAsync(pid) && startTimeMatches(startedAt, actualStartedAt);
1057
+ }
860
1058
  snapshotOne(active) {
861
1059
  const hasProbe = !!active.config.port;
862
1060
  const healthy = hasProbe && active.state !== 'disabled' ? active.healthy ?? null : null;
@@ -26,6 +26,7 @@ export class ServiceDispatcher {
26
26
  if (this.closed) {
27
27
  return {
28
28
  outcome: 'error',
29
+ cmd: handler.script,
29
30
  exitCode: 0,
30
31
  durationMs: 0,
31
32
  stdoutPreview: '',
@@ -37,6 +38,7 @@ export class ServiceDispatcher {
37
38
  if (pending >= this.limits.queueSize) {
38
39
  return {
39
40
  outcome: 'error',
41
+ cmd: handler.script,
40
42
  exitCode: 0,
41
43
  durationMs: 0,
42
44
  stdoutPreview: '',
@@ -46,7 +48,22 @@ export class ServiceDispatcher {
46
48
  }
47
49
  this.pendingByPack.set(pack.id, pending + 1);
48
50
  try {
49
- await this.acquire();
51
+ try {
52
+ await this.acquire();
53
+ }
54
+ catch {
55
+ // 排队期间被 `close()` 唤醒:必须在拿到槽位后重新判一次,否则等待者会被
56
+ // 唤醒后照常跑脚本——`close()` 的语义就落空了。
57
+ return {
58
+ outcome: 'error',
59
+ cmd: handler.script,
60
+ exitCode: 0,
61
+ durationMs: 0,
62
+ stdoutPreview: '',
63
+ stderrPreview: '',
64
+ error: { code: 'DISPATCHER_CLOSED', message: 'services dispatcher is shutting down' },
65
+ };
66
+ }
50
67
  try {
51
68
  return await this.scriptRunner.run(pack, handler, message);
52
69
  }
@@ -54,6 +71,7 @@ export class ServiceDispatcher {
54
71
  logger.warn(`[Services] ${pack.id} handler for ${message.type} crashed: ${error instanceof Error ? error.message : String(error)}`);
55
72
  return {
56
73
  outcome: 'error',
74
+ cmd: handler.script,
57
75
  exitCode: 0,
58
76
  durationMs: 0,
59
77
  stdoutPreview: '',
@@ -89,12 +107,17 @@ export class ServiceDispatcher {
89
107
  else
90
108
  this.running = Math.max(0, this.running - 1);
91
109
  }
92
- /** 关闭:拒绝新任务,唤醒所有等待者让其按 DISPATCHER_CLOSED 返回。 */
110
+ /**
111
+ * 关闭:拒绝新任务,并让所有排队中的等待者以 `DISPATCHER_CLOSED` 收场。
112
+ *
113
+ * 等待者必须被 reject 而不是 resolve:`resolve` 会让 `acquire()` 当成"拿到槽位",
114
+ * 等待者醒来后照常跑脚本,等于关停失效。
115
+ */
93
116
  close() {
94
117
  this.closed = true;
95
118
  this.pendingByPack.clear();
96
119
  for (const waiter of this.waiters.splice(0)) {
97
- waiter.resolve();
120
+ waiter.reject(new Error('DISPATCHER_CLOSED'));
98
121
  }
99
122
  }
100
123
  }