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
package/dist/trigger/history.js
CHANGED
|
@@ -1,18 +1,110 @@
|
|
|
1
1
|
import crypto from 'crypto';
|
|
2
2
|
import fs from 'fs';
|
|
3
3
|
import path from 'path';
|
|
4
|
+
import { createInterface } from 'node:readline';
|
|
5
|
+
import { setImmediate as yieldToIo } from 'node:timers/promises';
|
|
4
6
|
import { isManagedSessionRuntimeDir, SESSION_RUNTIME_DIR_ENV } from '../cli/task-context.js';
|
|
5
7
|
import { findMessageLogOperationMessageId } from '../core/message/message-log.js';
|
|
6
8
|
import { chatDirPath } from '../core/session/session-fs-store.js';
|
|
7
9
|
import { resolvePaths } from '../paths.js';
|
|
8
10
|
import { definitionRevision } from './validation.js';
|
|
9
11
|
import * as aunOutbox from '../aun/outbox.js';
|
|
12
|
+
// Share streaming scans, without retaining the contents of the audit logs.
|
|
13
|
+
// Callers arriving after a pass starts join the next pass so no prefix is lost.
|
|
14
|
+
const sharedAuditScans = new Map();
|
|
15
|
+
function importSharedAuditLogs(logDir, agentAid, ready, accept) {
|
|
16
|
+
const directory = path.resolve(logDir);
|
|
17
|
+
const key = process.platform === 'win32' ? directory.toLowerCase() : directory;
|
|
18
|
+
const batch = sharedAuditScans.get(key) ?? { pending: [], running: false };
|
|
19
|
+
sharedAuditScans.set(key, batch);
|
|
20
|
+
const result = new Promise((resolve, reject) => {
|
|
21
|
+
const request = { agentAid, accept, resolve, reject, ready };
|
|
22
|
+
request.ready = ready.catch(error => { request.failed = true; reject(error); });
|
|
23
|
+
batch.pending.push(request);
|
|
24
|
+
});
|
|
25
|
+
const schedule = () => {
|
|
26
|
+
if (batch.running)
|
|
27
|
+
return;
|
|
28
|
+
batch.running = true;
|
|
29
|
+
setImmediate(() => {
|
|
30
|
+
const requests = batch.pending.splice(0);
|
|
31
|
+
void scanSharedAuditLogs(directory, requests).finally(() => {
|
|
32
|
+
batch.running = false;
|
|
33
|
+
if (batch.pending.length)
|
|
34
|
+
schedule();
|
|
35
|
+
else
|
|
36
|
+
sharedAuditScans.delete(key);
|
|
37
|
+
});
|
|
38
|
+
});
|
|
39
|
+
};
|
|
40
|
+
schedule();
|
|
41
|
+
return result;
|
|
42
|
+
}
|
|
43
|
+
async function scanSharedAuditLogs(logDir, requests) {
|
|
44
|
+
// Registration happens before local history I/O, so simultaneous startup
|
|
45
|
+
// callers share a pass even when their history callbacks arrive out of order.
|
|
46
|
+
await Promise.all(requests.map(request => request.ready));
|
|
47
|
+
const byAgent = new Map();
|
|
48
|
+
for (const request of requests) {
|
|
49
|
+
const matching = byAgent.get(request.agentAid) ?? [];
|
|
50
|
+
matching.push(request);
|
|
51
|
+
byAgent.set(request.agentAid, matching);
|
|
52
|
+
}
|
|
53
|
+
try {
|
|
54
|
+
let entries;
|
|
55
|
+
try {
|
|
56
|
+
entries = await fs.promises.readdir(logDir);
|
|
57
|
+
}
|
|
58
|
+
catch {
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
for (const file of auditLogFiles(logDir, entries)) {
|
|
62
|
+
for await (const line of readLinesCooperatively(file)) {
|
|
63
|
+
let audit;
|
|
64
|
+
try {
|
|
65
|
+
audit = JSON.parse(line);
|
|
66
|
+
}
|
|
67
|
+
catch {
|
|
68
|
+
continue;
|
|
69
|
+
}
|
|
70
|
+
if (!audit?.runId || audit.status === 'dry-run')
|
|
71
|
+
continue;
|
|
72
|
+
for (const request of byAgent.get(audit.agentAid) ?? []) {
|
|
73
|
+
if (request.failed)
|
|
74
|
+
continue;
|
|
75
|
+
try {
|
|
76
|
+
request.accept(audit, path.basename(file));
|
|
77
|
+
}
|
|
78
|
+
catch (error) {
|
|
79
|
+
request.failed = true;
|
|
80
|
+
request.reject(error);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
catch (error) {
|
|
87
|
+
for (const request of requests) {
|
|
88
|
+
if (!request.failed) {
|
|
89
|
+
request.failed = true;
|
|
90
|
+
request.reject(error);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
finally {
|
|
95
|
+
for (const request of requests)
|
|
96
|
+
if (!request.failed)
|
|
97
|
+
request.resolve();
|
|
98
|
+
}
|
|
99
|
+
}
|
|
10
100
|
export class TriggerHistoryStore {
|
|
11
101
|
rootDir;
|
|
12
102
|
agentAid;
|
|
13
103
|
file;
|
|
14
104
|
fallbackFile;
|
|
15
105
|
runSummaryCache = null;
|
|
106
|
+
auditImport;
|
|
107
|
+
importingRunIds;
|
|
16
108
|
constructor(rootDir, agentAid) {
|
|
17
109
|
this.rootDir = rootDir;
|
|
18
110
|
this.agentAid = agentAid;
|
|
@@ -58,6 +150,8 @@ export class TriggerHistoryStore {
|
|
|
58
150
|
importedFrom,
|
|
59
151
|
};
|
|
60
152
|
this.append(event);
|
|
153
|
+
if (audit.agentAid === this.agentAid)
|
|
154
|
+
this.importingRunIds?.add(audit.runId);
|
|
61
155
|
return event;
|
|
62
156
|
}
|
|
63
157
|
events(triggerId, limit = 100) {
|
|
@@ -226,17 +320,36 @@ export class TriggerHistoryStore {
|
|
|
226
320
|
});
|
|
227
321
|
}
|
|
228
322
|
ensureDefinitionSnapshot(definition) {
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
323
|
+
this.ensureDefinitionSnapshots([definition]);
|
|
324
|
+
}
|
|
325
|
+
ensureDefinitionSnapshots(definitions) {
|
|
326
|
+
const pending = new Map(definitions.map(definition => [definition.id, {
|
|
327
|
+
definition, revision: definitionRevision(definition),
|
|
328
|
+
}]));
|
|
329
|
+
if (pending.size === 0)
|
|
330
|
+
return;
|
|
331
|
+
// Snapshot migration needs revisions only, never delivery hydration. Read
|
|
332
|
+
// history once for the entire Agent instead of once per definition.
|
|
333
|
+
for (const line of this.readLines()) {
|
|
334
|
+
const event = parseHistoryEvent(line, this.agentAid);
|
|
335
|
+
if (!event || event.agentAid !== this.agentAid || !isDefinitionEvent(event))
|
|
336
|
+
continue;
|
|
337
|
+
if (pending.get(event.triggerId)?.revision === event.revision)
|
|
338
|
+
pending.delete(event.triggerId);
|
|
339
|
+
if (pending.size === 0)
|
|
232
340
|
return;
|
|
233
341
|
}
|
|
234
|
-
|
|
342
|
+
for (const { definition } of pending.values()) {
|
|
343
|
+
this.recordDefinition('trigger.created', definition, { reason: 'migration_snapshot' });
|
|
344
|
+
}
|
|
235
345
|
}
|
|
236
346
|
importAuditLogs(logDir) {
|
|
237
|
-
const knownRuns = new Set(
|
|
238
|
-
|
|
239
|
-
|
|
347
|
+
const knownRuns = new Set();
|
|
348
|
+
for (const line of this.readLines()) {
|
|
349
|
+
const event = parseHistoryEvent(line, this.agentAid);
|
|
350
|
+
if (event && event.agentAid === this.agentAid && isRunEvent(event))
|
|
351
|
+
knownRuns.add(event.audit.runId);
|
|
352
|
+
}
|
|
240
353
|
let imported = 0;
|
|
241
354
|
for (const file of listAuditLogFiles(logDir)) {
|
|
242
355
|
let lines;
|
|
@@ -267,6 +380,39 @@ export class TriggerHistoryStore {
|
|
|
267
380
|
}
|
|
268
381
|
return imported;
|
|
269
382
|
}
|
|
383
|
+
/** Daemon startup imports yield during both history and legacy audit scans. */
|
|
384
|
+
importAuditLogsAsync(logDir) {
|
|
385
|
+
if (this.auditImport)
|
|
386
|
+
return this.auditImport;
|
|
387
|
+
const knownRuns = new Set();
|
|
388
|
+
// A live audit written while scanning must not be imported a second time.
|
|
389
|
+
this.importingRunIds = knownRuns;
|
|
390
|
+
this.auditImport = this.importAuditLogsCooperatively(logDir, knownRuns).finally(() => {
|
|
391
|
+
this.auditImport = undefined;
|
|
392
|
+
this.importingRunIds = undefined;
|
|
393
|
+
});
|
|
394
|
+
return this.auditImport;
|
|
395
|
+
}
|
|
396
|
+
async importAuditLogsCooperatively(logDir, knownRuns) {
|
|
397
|
+
const ready = (async () => {
|
|
398
|
+
for (const file of [this.file, this.fallbackFile].filter((value) => !!value)) {
|
|
399
|
+
for await (const line of readLinesCooperatively(file)) {
|
|
400
|
+
const event = parseHistoryEvent(line, this.agentAid);
|
|
401
|
+
if (event && event.agentAid === this.agentAid && isRunEvent(event))
|
|
402
|
+
knownRuns.add(event.audit.runId);
|
|
403
|
+
}
|
|
404
|
+
}
|
|
405
|
+
})();
|
|
406
|
+
let imported = 0;
|
|
407
|
+
await importSharedAuditLogs(logDir, this.agentAid, ready, (audit, file) => {
|
|
408
|
+
if (knownRuns.has(audit.runId))
|
|
409
|
+
return;
|
|
410
|
+
this.recordRun(audit, file);
|
|
411
|
+
knownRuns.add(audit.runId);
|
|
412
|
+
imported++;
|
|
413
|
+
});
|
|
414
|
+
return imported;
|
|
415
|
+
}
|
|
270
416
|
append(event) {
|
|
271
417
|
const line = `${JSON.stringify(event)}\n`;
|
|
272
418
|
try {
|
|
@@ -453,8 +599,31 @@ function listAuditLogFiles(logDir) {
|
|
|
453
599
|
catch {
|
|
454
600
|
return [];
|
|
455
601
|
}
|
|
602
|
+
return auditLogFiles(logDir, entries);
|
|
603
|
+
}
|
|
604
|
+
function auditLogFiles(logDir, entries) {
|
|
456
605
|
return entries
|
|
457
606
|
.filter(file => file === 'trigger-runs.log' || /^trigger-runs-\d{8}\.log$/.test(file))
|
|
458
607
|
.sort()
|
|
459
608
|
.map(file => path.join(logDir, file));
|
|
460
609
|
}
|
|
610
|
+
async function* readLinesCooperatively(file) {
|
|
611
|
+
const input = fs.createReadStream(file, { encoding: 'utf8' });
|
|
612
|
+
const lines = createInterface({ input, crlfDelay: Infinity });
|
|
613
|
+
let count = 0;
|
|
614
|
+
try {
|
|
615
|
+
for await (const line of lines) {
|
|
616
|
+
if (line)
|
|
617
|
+
yield line;
|
|
618
|
+
if (++count % 128 === 0)
|
|
619
|
+
await yieldToIo();
|
|
620
|
+
}
|
|
621
|
+
}
|
|
622
|
+
catch {
|
|
623
|
+
// Preserve the synchronous import's tolerance of missing/inaccessible logs.
|
|
624
|
+
}
|
|
625
|
+
finally {
|
|
626
|
+
lines.close();
|
|
627
|
+
input.destroy();
|
|
628
|
+
}
|
|
629
|
+
}
|
package/dist/trigger/manager.js
CHANGED
|
@@ -15,9 +15,7 @@ export class TriggerDefinitionManager {
|
|
|
15
15
|
fs.mkdirSync(this.rootDir, { recursive: true });
|
|
16
16
|
assertRealDirectory(this.rootDir, 'Trigger root');
|
|
17
17
|
this.history = new TriggerHistoryStore(rootDir, agentAid);
|
|
18
|
-
|
|
19
|
-
this.history.ensureDefinitionSnapshot(definition);
|
|
20
|
-
}
|
|
18
|
+
this.history.ensureDefinitionSnapshots(this.list({ all: true }));
|
|
21
19
|
}
|
|
22
20
|
list(opts = {}) {
|
|
23
21
|
const definitions = [];
|
|
@@ -4,7 +4,7 @@ import { CronExpressionParser } from 'cron-parser';
|
|
|
4
4
|
import { logger } from '../utils/logger.js';
|
|
5
5
|
import { isNoopText } from '../core/message/message-utils.js';
|
|
6
6
|
import { matchTriggerEvent, TriggerEventSource } from './event-source.js';
|
|
7
|
-
import { definitionRevision, executionTimeoutMs, parseDurationMs, previewText, renderTemplate, resolveScriptPath, sha256, validateConcurrencyCombination, } from './validation.js';
|
|
7
|
+
import { definitionRevision, executionTimeoutMs, parseDurationMs, previewText, renderTemplate, sessionIdlePromptOverride, resolveScriptPath, sha256, validateConcurrencyCombination, } from './validation.js';
|
|
8
8
|
import { TargetSessionLockRegistry } from './session-lock.js';
|
|
9
9
|
import { enterTrigger, formatTriggerPath } from '../core/causation/context.js';
|
|
10
10
|
import { createRootCausation } from '../core/causation/context.js';
|
|
@@ -33,12 +33,14 @@ export class TriggerRuntimeScheduler {
|
|
|
33
33
|
eventBus;
|
|
34
34
|
targetSessionLocks;
|
|
35
35
|
timers = new Map();
|
|
36
|
+
authorizationRetries = new Map();
|
|
36
37
|
running = new Map();
|
|
37
38
|
runStartLocks = new Map();
|
|
38
39
|
recoveredOneShotIds = new Set();
|
|
39
40
|
eventSource;
|
|
40
41
|
initialized = false;
|
|
41
42
|
stopping = false;
|
|
43
|
+
lifecycleGeneration = 0;
|
|
42
44
|
constructor(manager, state, audit, scriptExecutor, feedback, daemonChannel, runtime, eventBus, targetSessionLocks = new TargetSessionLockRegistry()) {
|
|
43
45
|
this.manager = manager;
|
|
44
46
|
this.state = state;
|
|
@@ -56,10 +58,19 @@ export class TriggerRuntimeScheduler {
|
|
|
56
58
|
}
|
|
57
59
|
}
|
|
58
60
|
async init() {
|
|
59
|
-
|
|
61
|
+
const stopped = this.stop();
|
|
62
|
+
const generation = this.lifecycleGeneration;
|
|
63
|
+
await stopped;
|
|
64
|
+
if (generation !== this.lifecycleGeneration)
|
|
65
|
+
return;
|
|
60
66
|
this.stopping = false;
|
|
61
67
|
this.recoveredOneShotIds.clear();
|
|
68
|
+
await this.audit.initializeHistory?.();
|
|
69
|
+
if (this.stopping || generation !== this.lifecycleGeneration)
|
|
70
|
+
return;
|
|
62
71
|
await this.recoverOpenRuns();
|
|
72
|
+
if (this.stopping || generation !== this.lifecycleGeneration)
|
|
73
|
+
return;
|
|
63
74
|
for (const definition of this.manager.list()) {
|
|
64
75
|
if (this.recoveredOneShotIds.has(definition.id))
|
|
65
76
|
continue;
|
|
@@ -70,7 +81,9 @@ export class TriggerRuntimeScheduler {
|
|
|
70
81
|
logger.info(`[Trigger] scheduler initialized for ${this.manager.agentAid}`);
|
|
71
82
|
}
|
|
72
83
|
async stop() {
|
|
84
|
+
this.lifecycleGeneration++;
|
|
73
85
|
this.stopping = true;
|
|
86
|
+
this.clearAuthorizationRetries();
|
|
74
87
|
for (const timer of this.timers.values())
|
|
75
88
|
clearTimeout(timer);
|
|
76
89
|
this.timers.clear();
|
|
@@ -93,7 +106,9 @@ export class TriggerRuntimeScheduler {
|
|
|
93
106
|
* Explicit scheduler replacement/unload must continue to use stop().
|
|
94
107
|
*/
|
|
95
108
|
async suspendForDaemonExit() {
|
|
109
|
+
this.lifecycleGeneration++;
|
|
96
110
|
this.stopping = true;
|
|
111
|
+
this.clearAuthorizationRetries();
|
|
97
112
|
for (const timer of this.timers.values())
|
|
98
113
|
clearTimeout(timer);
|
|
99
114
|
this.timers.clear();
|
|
@@ -431,7 +446,8 @@ export class TriggerRuntimeScheduler {
|
|
|
431
446
|
return;
|
|
432
447
|
}
|
|
433
448
|
const result = await runPromise;
|
|
434
|
-
if (result.
|
|
449
|
+
if (result.reason === 'authorization_unavailable'
|
|
450
|
+
|| (result.status === 'running' && result.reason === 'daemon_restart_resume_pending'))
|
|
435
451
|
return;
|
|
436
452
|
const fresh = this.manager.get(triggerId);
|
|
437
453
|
if (fresh?.enabled && definitionRevision(fresh) === firedRevision) {
|
|
@@ -471,6 +487,24 @@ export class TriggerRuntimeScheduler {
|
|
|
471
487
|
return { ok: false, runId, triggerId: definition.id, status: 'failed', reason: 'invalid_execution_configuration', audit, error: message };
|
|
472
488
|
}
|
|
473
489
|
const executionAuthorization = await this.runtime.authorizeExecution?.(definition);
|
|
490
|
+
if (executionAuthorization?.retryable && !executionAuthorization.allowed) {
|
|
491
|
+
if (!payload.dryRun)
|
|
492
|
+
this.deferAuthorization(definition, definition.id, async (current) => {
|
|
493
|
+
const result = await this.startRun(current, payload);
|
|
494
|
+
if (payload.scheduledAt !== undefined && this.isOneShot(current.source) && result.reason !== 'authorization_unavailable'
|
|
495
|
+
&& result.reason !== 'stopped' && this.manager.get(current.id)?.enabled
|
|
496
|
+
&& definitionRevision(this.manager.get(current.id)) === definitionRevision(current)) {
|
|
497
|
+
this.setEnabled(current.id, false);
|
|
498
|
+
}
|
|
499
|
+
});
|
|
500
|
+
return { ok: false, runId, triggerId: definition.id, status: 'skipped', reason: 'authorization_unavailable' };
|
|
501
|
+
}
|
|
502
|
+
if (!payload.dryRun) {
|
|
503
|
+
const pending = this.authorizationRetries.get(definition.id);
|
|
504
|
+
if (pending)
|
|
505
|
+
clearTimeout(pending);
|
|
506
|
+
this.authorizationRetries.delete(definition.id);
|
|
507
|
+
}
|
|
474
508
|
if (executionAuthorization && !executionAuthorization.allowed) {
|
|
475
509
|
const reason = executionAuthorization.reason || 'execution_authorization_denied';
|
|
476
510
|
const failureNotice = payload.dryRun
|
|
@@ -843,9 +877,17 @@ export class TriggerRuntimeScheduler {
|
|
|
843
877
|
}
|
|
844
878
|
async runRecordedExecutionAttempt(definition, runId, firedAt, sourcePayload, signal, dryRun, causation, attempt, previousAttempt) {
|
|
845
879
|
const attemptId = `${runId}:attempt:${attempt}`;
|
|
846
|
-
|
|
880
|
+
let executionAuthorization = await this.runtime.authorizeExecution?.(definition);
|
|
881
|
+
while (executionAuthorization?.retryable && !executionAuthorization.allowed && !dryRun) {
|
|
882
|
+
if (!await sleep(5_000, signal))
|
|
883
|
+
throw new Error('Trigger authorization wait cancelled');
|
|
884
|
+
executionAuthorization = await this.runtime.authorizeExecution?.(definition);
|
|
885
|
+
}
|
|
886
|
+
if (signal.aborted)
|
|
887
|
+
throw new Error('Trigger execution cancelled');
|
|
847
888
|
if (executionAuthorization && !executionAuthorization.allowed) {
|
|
848
|
-
const
|
|
889
|
+
const reason = executionAuthorization.retryable ? 'authorization_unavailable' : 'authorization_denied';
|
|
890
|
+
const reply = errorReply(runId, 0, reason, executionAuthorization.reason || 'Trigger execution authorization denied');
|
|
849
891
|
const execution = {
|
|
850
892
|
script: null,
|
|
851
893
|
processing: { mode: definition.execution.type },
|
|
@@ -859,7 +901,7 @@ export class TriggerRuntimeScheduler {
|
|
|
859
901
|
});
|
|
860
902
|
this.state.completeExecutionAttempt?.(definition.id, runId, attemptId, {
|
|
861
903
|
status: 'failed',
|
|
862
|
-
error: { code:
|
|
904
|
+
error: { code: reason, message: reply.error?.text || 'Trigger execution authorization denied' },
|
|
863
905
|
});
|
|
864
906
|
}
|
|
865
907
|
return execution;
|
|
@@ -936,7 +978,7 @@ export class TriggerRuntimeScheduler {
|
|
|
936
978
|
: {}),
|
|
937
979
|
},
|
|
938
980
|
};
|
|
939
|
-
const originalPrompt = renderTemplate(definition.execution.prompt ?? '', {
|
|
981
|
+
const originalPrompt = sessionIdlePromptOverride(sourcePayload) ?? renderTemplate(definition.execution.prompt ?? '', {
|
|
940
982
|
trigger: triggerView,
|
|
941
983
|
timestamp: firedAt,
|
|
942
984
|
event: sourcePayload,
|
|
@@ -1161,10 +1203,41 @@ export class TriggerRuntimeScheduler {
|
|
|
1161
1203
|
}
|
|
1162
1204
|
}
|
|
1163
1205
|
}
|
|
1206
|
+
clearAuthorizationRetries() {
|
|
1207
|
+
for (const timer of this.authorizationRetries.values())
|
|
1208
|
+
clearTimeout(timer);
|
|
1209
|
+
this.authorizationRetries.clear();
|
|
1210
|
+
}
|
|
1211
|
+
deferAuthorization(definition, key, retry) {
|
|
1212
|
+
if (this.stopping || this.authorizationRetries.has(key))
|
|
1213
|
+
return;
|
|
1214
|
+
const revision = definitionRevision(definition);
|
|
1215
|
+
logger.info(`[Trigger] waiting for authorization evidence: trigger=${definition.id}`);
|
|
1216
|
+
const timer = setTimeout(() => {
|
|
1217
|
+
this.authorizationRetries.delete(key);
|
|
1218
|
+
const current = this.manager.get(definition.id);
|
|
1219
|
+
if (this.stopping || !current?.enabled || definitionRevision(current) !== revision)
|
|
1220
|
+
return;
|
|
1221
|
+
void retry(current).catch(error => logger.warn(`[Trigger] deferred authorization failed: ${definition.id}: ${error}`));
|
|
1222
|
+
}, 5_000);
|
|
1223
|
+
timer.unref?.();
|
|
1224
|
+
this.authorizationRetries.set(key, timer);
|
|
1225
|
+
}
|
|
1164
1226
|
async reclaimOpenRun(definition, run) {
|
|
1165
1227
|
if (!definition.enabled || !this.isResumableCheckpoint(definition, run))
|
|
1166
1228
|
return false;
|
|
1167
1229
|
const executionAuthorization = await this.runtime.authorizeExecution?.(definition);
|
|
1230
|
+
if (executionAuthorization?.retryable && !executionAuthorization.allowed) {
|
|
1231
|
+
this.deferAuthorization(definition, run.runId, async (current) => {
|
|
1232
|
+
if (!this.state.list(current.id).some(active => active.runId === run.runId))
|
|
1233
|
+
return;
|
|
1234
|
+
if (!await this.reclaimOpenRun(current, run))
|
|
1235
|
+
await this.closeOpenRunAfterRestart(current, run);
|
|
1236
|
+
});
|
|
1237
|
+
if (this.isOneShot(definition.source))
|
|
1238
|
+
this.recoveredOneShotIds.add(definition.id);
|
|
1239
|
+
return true;
|
|
1240
|
+
}
|
|
1168
1241
|
if (executionAuthorization && !executionAuthorization.allowed) {
|
|
1169
1242
|
const reason = executionAuthorization.reason || 'Trigger execution authorization denied';
|
|
1170
1243
|
const notice = await this.disableAndNotifyAuthorizationDenied(definition, run.runId, run.source?.firedAt ?? Date.now(), reason, createRootCausation());
|
|
@@ -1266,7 +1339,7 @@ export class TriggerRuntimeScheduler {
|
|
|
1266
1339
|
let anomalies = [];
|
|
1267
1340
|
let incomplete;
|
|
1268
1341
|
try {
|
|
1269
|
-
const originalPrompt = renderTemplate(definition.execution.prompt ?? '', {
|
|
1342
|
+
const originalPrompt = sessionIdlePromptOverride(source.payload) ?? renderTemplate(definition.execution.prompt ?? '', {
|
|
1270
1343
|
trigger: definition,
|
|
1271
1344
|
timestamp: source.firedAt,
|
|
1272
1345
|
event: source.payload,
|
|
@@ -157,7 +157,7 @@ export function seedTaskRecoveryTrigger(manager, owner) {
|
|
|
157
157
|
atomicWriteJson(manager.definitionPath(definition.id), definition);
|
|
158
158
|
fs.rmSync(manager.activePath(definition.id), { force: true });
|
|
159
159
|
}
|
|
160
|
-
const
|
|
160
|
+
const DEFAULT_SESSION_IDLE_PROMPT = `本会话已闲置一段时间。请依据你的职责执行一次例行巡检:
|
|
161
161
|
1. 检查当前会话中未完成却停滞的事项。私聊检查自己承诺或正在推进的任务;群聊还需检查成员长时间无进展、被点名未回应、承诺后失联等情况。有,则在既有授权和职责内推进一次,或向相关成员催促一次并说明停滞事项,随后即止;无,则不做任何动作。不要自行新增任务,正常推进或无待办时保持沉默。
|
|
162
162
|
2. 巡检优先基于已有上下文判断,确需补充背景再查询历史,并遵守会话自身的发送规范。
|
|
163
163
|
3. 判定无需任何对外动作时,本轮只输出 [[NOOP]],不发送任何消息、不发布巡检结论。注意:最终对外输出必须是且仅是 [[NOOP]] 这一行——不要在输出它之前先发送任何说明文字或巡检判定,判定过程仅在内部完成。
|
|
@@ -188,7 +188,7 @@ export function seedSessionIdleCheckTrigger(manager, owner) {
|
|
|
188
188
|
},
|
|
189
189
|
execution: {
|
|
190
190
|
type: 'target_session',
|
|
191
|
-
prompt:
|
|
191
|
+
prompt: DEFAULT_SESSION_IDLE_PROMPT,
|
|
192
192
|
onError: 'retry',
|
|
193
193
|
noopSentinel: '[[NOOP]]',
|
|
194
194
|
},
|
|
@@ -9,6 +9,10 @@ const MAX_EXECUTION_TIMEOUT_MS = 2_147_483_647;
|
|
|
9
9
|
export const TRIGGER_SCHEMA_VERSION = 3.1;
|
|
10
10
|
const LEGACY_TRIGGER_SCHEMA_VERSION = 4;
|
|
11
11
|
const DEFAULT_NOOP_SENTINEL = '[[NOOP]]';
|
|
12
|
+
export function sessionIdlePromptOverride(event) {
|
|
13
|
+
return event?.type === 'session:idle' && typeof event.loopOnceRequestId === 'string'
|
|
14
|
+
&& typeof event.loopPrompt === 'string' ? event.loopPrompt : undefined;
|
|
15
|
+
}
|
|
12
16
|
/** Only a leading sentinel controls delivery; later mentions are ordinary prose. */
|
|
13
17
|
export function isTriggerNoopText(text, sentinel) {
|
|
14
18
|
return !!sentinel && text.trimStart().startsWith(sentinel);
|
package/dist/utils/autostart.js
CHANGED
|
@@ -7,9 +7,9 @@ export function autostartInstalled() {
|
|
|
7
7
|
return macosAutostartInstalled();
|
|
8
8
|
return false;
|
|
9
9
|
}
|
|
10
|
-
export function configureAutostart(enabled, runtimeRoot) {
|
|
10
|
+
export function configureAutostart(enabled, runtimeRoot, options = {}) {
|
|
11
11
|
if (process.platform === 'win32')
|
|
12
|
-
return configureWindowsAutostart(enabled, runtimeRoot);
|
|
12
|
+
return configureWindowsAutostart(enabled, runtimeRoot, options);
|
|
13
13
|
if (process.platform === 'darwin')
|
|
14
14
|
return configureMacosAutostart(enabled, runtimeRoot);
|
|
15
15
|
return {
|