evolcore 0.0.43-test.5 → 0.0.43-test.7
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 +95 -0
- package/bin/ecmsg.js +281 -81
- package/dist/agents/baseagent-availability.js +20 -5
- package/dist/agents/baseagent.js +2 -2
- package/dist/agents/claude-runner.js +19 -4
- package/dist/agents/codex/app-server-client.js +36 -3
- package/dist/agents/codex-runner.js +75 -8
- package/dist/aun/group/invite-service.js +3 -2
- package/dist/aun/msg/p2p.js +6 -1
- package/dist/aun/service-proxy.js +18 -6
- package/dist/channels/aun.js +306 -30
- package/dist/cli/agent-create.js +18 -8
- package/dist/cli/aun-commands.js +9 -1
- package/dist/cli/daemon-commands.js +34 -65
- package/dist/cli/daemon-lifecycle-client.js +61 -0
- package/dist/cli/ecmsg-gateway.js +38 -3
- package/dist/cli/events-command.js +73 -0
- package/dist/cli/index.js +31 -14
- package/dist/cli/init.js +75 -0
- package/dist/cli/invoke-command.js +182 -0
- package/dist/cli/managed-command-guard.js +6 -1
- package/dist/cli/provider-command.js +1 -1
- package/dist/cli/restart-monitor.js +67 -50
- package/dist/cli/services-command.js +111 -0
- package/dist/cli/task-context.js +38 -1
- package/dist/cli/team-workspace-command.js +134 -0
- package/dist/cli/thread-command.js +6 -2
- package/dist/cli/watch-logs.js +6 -4
- package/dist/cli/watch-msg.js +1 -1
- package/dist/config/boot-log.js +6 -4
- package/dist/config/builtin-roles.js +2 -2
- package/dist/config/config-field-policy.js +2 -2
- package/dist/config/config-manager.js +1 -8
- package/dist/config/invoke-ipc-service.js +118 -0
- package/dist/config/peer-role-resolver.js +1 -1
- package/dist/config/provider-identity-migration.js +92 -17
- package/dist/config/provider-local-sync.js +2 -0
- package/dist/config/providers-config.js +13 -0
- package/dist/config/providers-management.js +100 -10
- package/dist/config/role-service.js +8 -8
- package/dist/config/role-store.js +22 -22
- package/dist/config/schema-migration.js +86 -88
- package/dist/config/snapshot.js +70 -41
- package/dist/config-store.js +2 -1
- package/dist/core/agent-reload-coordinator.js +6 -4
- package/dist/core/agent-runtime.js +146 -15
- package/dist/core/auth/delegation-authorization.js +66 -0
- package/dist/core/auth/operation-catalog.js +20 -0
- package/dist/core/baseagent-loader.js +75 -42
- package/dist/core/channel-loader.js +45 -13
- package/dist/core/channel-runtime.js +116 -45
- package/dist/core/command/agent-control.js +1 -1
- package/dist/core/command/agent-storage-menu.js +1 -1
- package/dist/core/command/chatmode-config.js +2 -36
- package/dist/core/command/command-handler.js +58 -16
- package/dist/core/command/data-root-resolver.js +32 -23
- package/dist/core/command/eventcenter-menu.js +338 -0
- package/dist/core/command/menu-catalog.js +8 -0
- package/dist/core/command/menu-handler.js +206 -87
- package/dist/core/command/menu-protocol.js +142 -22
- package/dist/core/command/menu-query-batch.js +194 -0
- package/dist/core/command/role-menu.js +27 -15
- package/dist/core/command/slash-gate.js +1 -1
- package/dist/core/command/slash-handler.js +34 -42
- package/dist/core/command/team-workspace-bootstrap.js +350 -0
- package/dist/core/command/{pa-workspace-calendar.js → team-workspace-calendar.js} +7 -7
- package/dist/core/command/team-workspace-initializer.js +2 -0
- package/dist/core/command/{pa-workspace-items.js → team-workspace-items.js} +83 -68
- package/dist/core/command/team-workspace-job-store.js +88 -0
- package/dist/core/command/team-workspace-management-service.js +2 -0
- package/dist/core/command/team-workspace-management-store.js +96 -0
- package/dist/core/command/team-workspace-management.js +265 -0
- package/dist/core/command/team-workspace-members-menu.js +88 -0
- package/dist/core/command/team-workspace-store.js +612 -0
- package/dist/core/command/team-workspace-team.js +193 -0
- package/dist/core/daemon-control.js +45 -15
- package/dist/core/daemon-lifecycle.js +478 -75
- package/dist/core/handoff/dispatcher.js +9 -3
- package/dist/core/handoff/runtime.js +130 -42
- package/dist/core/handoff/store.js +190 -2
- package/dist/core/handoff/types.js +5 -0
- package/dist/core/initialization/service.js +4 -4
- package/dist/core/invoke/executors/index.js +13 -0
- package/dist/core/invoke/executors/media.js +283 -0
- package/dist/core/invoke/executors/text.js +198 -0
- package/dist/core/invoke/invoke-service.js +157 -0
- package/dist/core/invoke/style-router.js +48 -0
- package/dist/core/invoke/types.js +19 -0
- package/dist/core/ipc-registration.js +51 -11
- package/dist/core/lifecycle-notice.js +10 -4
- package/dist/core/memory/consolidation.js +7 -5
- package/dist/core/message/aun-ipc-sender.js +149 -19
- package/dist/core/message/im-renderer.js +28 -84
- package/dist/core/message/message-bridge.js +57 -4
- package/dist/core/message/message-queue.js +46 -4
- package/dist/core/message/message-utils.js +10 -2
- package/dist/core/message/peer-mode.js +7 -34
- package/dist/core/message/pending-hints.js +4 -10
- package/dist/core/message/response-engine.js +163 -52
- package/dist/core/message/send-options.js +7 -4
- package/dist/core/message/send-routing.js +55 -7
- package/dist/core/ownership/owner-resolver.js +8 -0
- package/dist/core/permission/ec-command-parser.js +14 -3
- package/dist/core/permission/protected-paths.js +12 -2
- package/dist/core/permission/shell-environment.js +9 -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-file-health.js +243 -0
- package/dist/core/session/session-fs-store.js +163 -3
- package/dist/core/session/session-idle-monitor.js +9 -1
- package/dist/core/session/session-manager.js +36 -2
- package/dist/core/task/task-recovery-service.js +13 -2
- package/dist/core/thread/command-parser.js +9 -2
- package/dist/core/thread/thread-operation.js +21 -2
- package/dist/core/thread/thread-route.js +13 -2
- package/dist/directory/owner-directory-admission.js +42 -30
- package/dist/directory/owner-directory-cache.js +143 -126
- package/dist/directory/owner-directory-query-operation.js +9 -8
- package/dist/eck/kit-renderer.js +7 -5
- package/dist/ecweb/basic-settings-service.js +1 -1
- package/dist/ecweb/message-context-service.js +0 -4
- package/dist/ecweb/runtime/lifecycle.js +14 -8
- package/dist/ecweb/runtime/supervisor.js +62 -30
- package/dist/event-center/admission-materializer.js +114 -0
- package/dist/event-center/bus-gate.js +136 -0
- package/dist/event-center/bus-host.js +290 -0
- package/dist/event-center/bus-resolver.js +120 -0
- package/dist/event-center/center-config.js +100 -0
- package/dist/event-center/client-factory.js +50 -0
- package/dist/event-center/daemon.js +45 -0
- package/dist/event-center/directory-source.js +53 -0
- package/dist/event-center/dispatcher.js +157 -0
- package/dist/event-center/event-log.js +110 -0
- package/dist/event-center/index-generator.js +63 -0
- package/dist/event-center/ipc-operation.js +308 -0
- package/dist/event-center/ledger.js +52 -0
- package/dist/event-center/pack-loader.js +202 -0
- package/dist/event-center/pack-manager.js +85 -0
- package/dist/event-center/registry.js +238 -0
- package/dist/event-center/runtime.js +641 -0
- package/dist/event-center/script-runner.js +356 -0
- package/dist/event-center/types.js +35 -0
- package/dist/event-center/validation.js +413 -0
- package/dist/index.js +751 -268
- package/dist/ipc.js +66 -12
- package/dist/paths.js +20 -0
- package/dist/product.js +14 -0
- package/dist/proxy/manager.js +43 -18
- package/dist/proxy/service-supervisor.js +78 -35
- package/dist/response-system/engines/v1/index.js +0 -1
- package/dist/response-system/modes/single-session/index.js +1 -1
- package/dist/services/dispatcher.js +100 -0
- package/dist/services/idempotency.js +90 -0
- package/dist/services/identity.js +52 -0
- package/dist/services/index-generator.js +70 -0
- package/dist/services/index.js +449 -0
- package/dist/services/ipc-operation.js +458 -0
- package/dist/services/ledger.js +52 -0
- package/dist/services/message-log.js +112 -0
- package/dist/services/pack-manager.js +204 -0
- package/dist/services/reply.js +55 -0
- package/dist/services/router.js +95 -0
- package/dist/services/script-runner.js +422 -0
- package/dist/services/service-payload.js +70 -0
- package/dist/services/types.js +18 -0
- package/dist/services/validation.js +297 -0
- package/dist/trigger/application-service.js +21 -1
- package/dist/trigger/audit.js +19 -4
- package/dist/trigger/authorization.js +14 -5
- package/dist/trigger/event-source.js +1 -1
- package/dist/trigger/history.js +176 -7
- package/dist/trigger/ipc-service.js +3 -1
- package/dist/trigger/manager.js +1 -3
- package/dist/trigger/scheduler.js +78 -5
- package/dist/trigger/script-sandbox.js +118 -22
- package/dist/utils/cross-platform.js +175 -13
- package/dist/utils/evolcore-version.js +69 -27
- package/dist/utils/host-info.js +11 -8
- package/dist/utils/instance-registry.js +19 -13
- package/dist/utils/npm-ops.js +48 -0
- package/dist/utils/process-introspect.js +109 -10
- package/dist/utils/startup-trace.js +222 -44
- package/dist/utils/windows-shell-trust.js +80 -6
- package/kits/docs/INDEX.md +2 -2
- package/kits/docs/aun/ABILITY_CALLING.md +10 -17
- package/kits/docs/context-assembly.md +1 -1
- package/kits/docs/evolcore/INDEX.md +5 -2
- package/kits/docs/evolcore/ability.md +4 -0
- package/kits/docs/evolcore/aid.md +1 -1
- package/kits/docs/evolcore/ctl.md +2 -1
- package/kits/docs/evolcore/event-center.md +207 -0
- package/kits/docs/evolcore/file-navigation.md +10 -264
- package/kits/docs/evolcore/fs.md +3 -2
- package/kits/docs/evolcore/group.md +2 -2
- package/kits/docs/evolcore/memory-architecture.md +2 -0
- package/kits/docs/evolcore/menu-exec-schema-commands.md +1 -1
- package/kits/docs/evolcore/menu-protocol.md +21 -0
- package/kits/docs/evolcore/msg.md +27 -8
- package/kits/docs/evolcore/provider.md +23 -3
- package/kits/docs/evolcore/response.md +5 -5
- package/kits/docs/evolcore/services.md +277 -0
- package/kits/docs/evolcore/task.md +2 -0
- package/kits/docs/evolcore/team-workspace.md +41 -0
- package/kits/eck_manifest.json +45 -7
- package/kits/eck_message_manifest.json +3 -3
- package/kits/rules/02-navigation.md +10 -1
- package/kits/rules/06-channel.md +5 -6
- package/kits/schemas/_meta.json +17 -6
- package/kits/schemas/agent-config.schema.19.json +5 -7
- package/kits/schemas/agent-config.schema.20.json +936 -0
- 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/relation-config.schema.12.json +2 -3
- package/kits/schemas/role-config.schema.2.json +4 -4
- 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/.switches.json +1 -5
- package/kits/skills/ability/SKILL.md +56 -0
- package/kits/skills/ability/references/calling.md +56 -0
- package/kits/skills/{ec-ability → ability}/references/contract-checklist.md +1 -1
- package/kits/skills/{ec-ability → ability}/references/development.md +9 -33
- package/kits/skills/{ec-ability → ability}/references/management.md +2 -2
- package/kits/skills/{ec-ability → ability}/references/recovery.md +1 -1
- package/kits/skills/{ec-ability → ability}/references/runtime-protocol.md +2 -0
- package/kits/skills/ec-agent-create/SKILL.md +0 -1
- 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 +34 -5
- package/kits/skills/ec-trigger/SKILL.md +4 -4
- package/kits/skills/personal-assistant/SKILL.md +6 -6
- package/kits/skills/personal-assistant/references/02-bootstrap.md +4 -4
- package/kits/skills/personal-assistant/scripts/clean.mjs +4 -4
- package/kits/skills/personal-assistant/scripts/init.mjs +2 -2
- package/kits/skills/personal-assistant/scripts/pa.mjs +4 -4
- package/kits/skills/thread-ops/SKILL.md +10 -10
- package/kits/templates/system-fragments/channel.md +3 -3
- package/kits/templates/system-fragments/commands.md +9 -7
- package/kits/templates/system-fragments/reply.md +2 -6
- package/kits/templates/system-fragments/session.md +1 -1
- package/kits/templates/system-fragments/thread.md +11 -9
- package/package.json +5 -6
- package/dist/cli/pa-workspace-command.js +0 -225
- package/dist/core/command/pa-workspace-store.js +0 -468
- package/dist/core/relation/pa-team.js +0 -132
- package/kits/docs/evolcore/pa-workspace.md +0 -51
- package/kits/skills/aid-ability/SKILL.md +0 -244
- package/kits/skills/ec-ability/SKILL.md +0 -67
- package/kits/skills/ec-ability/references/workflows.md +0 -15
|
@@ -0,0 +1,297 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* service.json / router.json 校验。
|
|
3
|
+
* 约束 1:1 映射 docs/services/schema/{service,router}.schema.json;Schema 为权威,
|
|
4
|
+
* 两者不一致改这里并同步 Schema。手写校验器(不引 ajv),失败逐条报错。
|
|
5
|
+
*/
|
|
6
|
+
import { isListenableType } from './service-payload.js';
|
|
7
|
+
export const SCRIPT_EXTENSIONS = ['mjs', 'cjs', 'js', 'py', 'sh', 'ps1'];
|
|
8
|
+
export const RUNTIME_WHITELIST = ['node', 'python', 'bash', 'powershell'];
|
|
9
|
+
const TIMEOUT_MIN = 1000;
|
|
10
|
+
const TIMEOUT_MAX = 300_000;
|
|
11
|
+
const TIMEOUT_DEFAULT = 30_000;
|
|
12
|
+
const QUOTA_DEFAULT_BUILTIN = { perMinute: 20, perHour: 200 };
|
|
13
|
+
const QUOTA_DEFAULT_SERVICE = { perMinute: 50, perHour: 1000 };
|
|
14
|
+
function isRecord(value) {
|
|
15
|
+
return !!value && typeof value === 'object' && !Array.isArray(value);
|
|
16
|
+
}
|
|
17
|
+
function optionalString(value, field, errors, max) {
|
|
18
|
+
if (value === undefined)
|
|
19
|
+
return undefined;
|
|
20
|
+
if (typeof value !== 'string') {
|
|
21
|
+
errors.push(`${field} must be a string`);
|
|
22
|
+
return undefined;
|
|
23
|
+
}
|
|
24
|
+
if (max !== undefined && value.length > max)
|
|
25
|
+
errors.push(`${field} is too long (max ${max})`);
|
|
26
|
+
return value;
|
|
27
|
+
}
|
|
28
|
+
function intInRange(value, field, min, max, errors) {
|
|
29
|
+
if (typeof value !== 'number' || !Number.isInteger(value) || value < min || value > max) {
|
|
30
|
+
errors.push(`${field} must be an integer between ${min} and ${max}`);
|
|
31
|
+
return undefined;
|
|
32
|
+
}
|
|
33
|
+
return value;
|
|
34
|
+
}
|
|
35
|
+
/** requires 的值:单个字符串、非空字符串数组,或 "*"。 */
|
|
36
|
+
function validateScopeValue(value, field, errors) {
|
|
37
|
+
if (typeof value === 'string') {
|
|
38
|
+
if (!value)
|
|
39
|
+
errors.push(`${field} must not be empty`);
|
|
40
|
+
return value;
|
|
41
|
+
}
|
|
42
|
+
if (Array.isArray(value)) {
|
|
43
|
+
if (value.length === 0) {
|
|
44
|
+
errors.push(`${field} must not be an empty array`);
|
|
45
|
+
return undefined;
|
|
46
|
+
}
|
|
47
|
+
if (value.some(item => typeof item !== 'string' || !item)) {
|
|
48
|
+
errors.push(`${field} must be an array of non-empty strings`);
|
|
49
|
+
return undefined;
|
|
50
|
+
}
|
|
51
|
+
return value;
|
|
52
|
+
}
|
|
53
|
+
errors.push(`${field} must be a string, a string array, or "*"`);
|
|
54
|
+
return undefined;
|
|
55
|
+
}
|
|
56
|
+
function validateValueAgainst(value, scope) {
|
|
57
|
+
if (scope === '*')
|
|
58
|
+
return true;
|
|
59
|
+
if (Array.isArray(scope))
|
|
60
|
+
return scope.includes(value);
|
|
61
|
+
return scope === value;
|
|
62
|
+
}
|
|
63
|
+
/** requires 判定:本机名片 type / exttype 是否满足。 */
|
|
64
|
+
export function requiresSatisfied(requires, identity) {
|
|
65
|
+
return validateValueAgainst(identity.type, requires.type)
|
|
66
|
+
&& validateValueAgainst(identity.exttype, requires.exttype);
|
|
67
|
+
}
|
|
68
|
+
/** handler.script 结构校验(存在性/越界由 pack-manager 做文件系统校验)。 */
|
|
69
|
+
function validateScriptPath(script, errors) {
|
|
70
|
+
if (typeof script !== 'string' || !script) {
|
|
71
|
+
errors.push('handler.script is required');
|
|
72
|
+
return undefined;
|
|
73
|
+
}
|
|
74
|
+
if (script.startsWith('/') || /^[a-zA-Z]:[\\/]/.test(script)) {
|
|
75
|
+
errors.push(`handler.script must be a relative path: ${script}`);
|
|
76
|
+
return undefined;
|
|
77
|
+
}
|
|
78
|
+
if (script.includes('..')) {
|
|
79
|
+
errors.push(`handler.script must not contain '..': ${script}`);
|
|
80
|
+
return undefined;
|
|
81
|
+
}
|
|
82
|
+
if (script.includes('\\')) {
|
|
83
|
+
errors.push(`handler.script must use forward slashes: ${script}`);
|
|
84
|
+
return undefined;
|
|
85
|
+
}
|
|
86
|
+
if (!script.startsWith('scripts/')) {
|
|
87
|
+
errors.push(`handler.script must be under scripts/: ${script}`);
|
|
88
|
+
return undefined;
|
|
89
|
+
}
|
|
90
|
+
const ext = script.split('.').pop() ?? '';
|
|
91
|
+
if (!SCRIPT_EXTENSIONS.includes(ext)) {
|
|
92
|
+
errors.push(`handler.script extension must be one of ${SCRIPT_EXTENSIONS.join('/')}: ${script}`);
|
|
93
|
+
return undefined;
|
|
94
|
+
}
|
|
95
|
+
return script;
|
|
96
|
+
}
|
|
97
|
+
function validateHandler(raw, errors) {
|
|
98
|
+
if (!isRecord(raw)) {
|
|
99
|
+
errors.push('listen[].handler must be an object');
|
|
100
|
+
return undefined;
|
|
101
|
+
}
|
|
102
|
+
const extra = Object.keys(raw).filter(key => !['script', 'runtime', 'args', 'timeoutMs'].includes(key));
|
|
103
|
+
if (extra.length)
|
|
104
|
+
errors.push(`listen[].handler has unknown fields: ${extra.join(', ')}`);
|
|
105
|
+
const script = validateScriptPath(raw.script, errors);
|
|
106
|
+
if (raw.runtime !== undefined && !RUNTIME_WHITELIST.includes(raw.runtime)) {
|
|
107
|
+
errors.push(`listen[].handler.runtime must be one of ${RUNTIME_WHITELIST.join('/')}`);
|
|
108
|
+
}
|
|
109
|
+
if (raw.args !== undefined && (!Array.isArray(raw.args) || raw.args.some(item => typeof item !== 'string'))) {
|
|
110
|
+
errors.push('listen[].handler.args must be an array of strings');
|
|
111
|
+
}
|
|
112
|
+
const timeoutMs = raw.timeoutMs === undefined
|
|
113
|
+
? TIMEOUT_DEFAULT
|
|
114
|
+
: intInRange(raw.timeoutMs, 'listen[].handler.timeoutMs', TIMEOUT_MIN, TIMEOUT_MAX, errors);
|
|
115
|
+
if (script === undefined)
|
|
116
|
+
return undefined;
|
|
117
|
+
return {
|
|
118
|
+
script,
|
|
119
|
+
...(raw.runtime !== undefined ? { runtime: raw.runtime } : {}),
|
|
120
|
+
...(Array.isArray(raw.args) ? { args: raw.args } : {}),
|
|
121
|
+
timeoutMs: timeoutMs ?? TIMEOUT_DEFAULT,
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
function validateListenEntry(raw, errors) {
|
|
125
|
+
if (!isRecord(raw)) {
|
|
126
|
+
errors.push('listen[] entries must be objects');
|
|
127
|
+
return undefined;
|
|
128
|
+
}
|
|
129
|
+
const extra = Object.keys(raw).filter(key => !['type', 'handler', 'filter'].includes(key));
|
|
130
|
+
if (extra.length)
|
|
131
|
+
errors.push(`listen entry has unknown fields: ${extra.join(', ')}`);
|
|
132
|
+
if (typeof raw.type !== 'string' || !isListenableType(raw.type)) {
|
|
133
|
+
errors.push(`listen.type must be services[.<seg>[.<seg>]] or services[.<seg>].*, and must not end with .result: ${String(raw.type)}`);
|
|
134
|
+
}
|
|
135
|
+
if (raw.filter !== undefined && !isRecord(raw.filter))
|
|
136
|
+
errors.push('listen[].filter must be an object');
|
|
137
|
+
const handler = validateHandler(raw.handler, errors);
|
|
138
|
+
if (typeof raw.type !== 'string' || !isListenableType(raw.type) || handler === undefined)
|
|
139
|
+
return undefined;
|
|
140
|
+
return {
|
|
141
|
+
type: raw.type,
|
|
142
|
+
...(isRecord(raw.filter) ? { filter: raw.filter } : {}),
|
|
143
|
+
handler,
|
|
144
|
+
};
|
|
145
|
+
}
|
|
146
|
+
function validateRequires(raw, errors) {
|
|
147
|
+
if (!isRecord(raw)) {
|
|
148
|
+
errors.push('requires is required and must be an object');
|
|
149
|
+
return undefined;
|
|
150
|
+
}
|
|
151
|
+
const extra = Object.keys(raw).filter(key => !['type', 'exttype'].includes(key));
|
|
152
|
+
if (extra.length)
|
|
153
|
+
errors.push(`requires has unknown fields: ${extra.join(', ')}`);
|
|
154
|
+
if (raw.type === undefined)
|
|
155
|
+
errors.push('requires.type is required');
|
|
156
|
+
if (raw.exttype === undefined)
|
|
157
|
+
errors.push('requires.exttype is required');
|
|
158
|
+
const type = raw.type === undefined ? undefined : validateScopeValue(raw.type, 'requires.type', errors);
|
|
159
|
+
const exttype = raw.exttype === undefined ? undefined : validateScopeValue(raw.exttype, 'requires.exttype', errors);
|
|
160
|
+
if (type === undefined || exttype === undefined)
|
|
161
|
+
return undefined;
|
|
162
|
+
return { type, exttype };
|
|
163
|
+
}
|
|
164
|
+
function validateReplyPolicy(raw, errors) {
|
|
165
|
+
if (raw === undefined)
|
|
166
|
+
return { allowed: false, payloadTypes: ['service-result'] };
|
|
167
|
+
if (!isRecord(raw)) {
|
|
168
|
+
errors.push('reply must be an object');
|
|
169
|
+
return { allowed: false, payloadTypes: ['service-result'] };
|
|
170
|
+
}
|
|
171
|
+
const extra = Object.keys(raw).filter(key => !['allowed', 'payloadTypes'].includes(key));
|
|
172
|
+
if (extra.length)
|
|
173
|
+
errors.push(`reply has unknown fields: ${extra.join(', ')}`);
|
|
174
|
+
if (raw.allowed !== undefined && typeof raw.allowed !== 'boolean')
|
|
175
|
+
errors.push('reply.allowed must be a boolean');
|
|
176
|
+
let payloadTypes = ['service-result'];
|
|
177
|
+
if (raw.payloadTypes !== undefined) {
|
|
178
|
+
if (!Array.isArray(raw.payloadTypes))
|
|
179
|
+
errors.push('reply.payloadTypes must be an array');
|
|
180
|
+
else if (raw.payloadTypes.some(item => item !== 'service-result')) {
|
|
181
|
+
errors.push('reply.payloadTypes only accepts "service-result"');
|
|
182
|
+
}
|
|
183
|
+
else
|
|
184
|
+
payloadTypes = ['service-result'];
|
|
185
|
+
}
|
|
186
|
+
return { allowed: raw.allowed === true, payloadTypes };
|
|
187
|
+
}
|
|
188
|
+
/** 校验 service.json 原始对象(不做文件系统检查)。 */
|
|
189
|
+
export function validateServiceJson(raw) {
|
|
190
|
+
const errors = [];
|
|
191
|
+
if (!isRecord(raw))
|
|
192
|
+
return { ok: false, errors: ['service.json must be a JSON object'] };
|
|
193
|
+
const extra = Object.keys(raw).filter(key => !['v', 'name', 'description', 'enabled', 'requires', 'listen', 'unmatched', 'reply'].includes(key));
|
|
194
|
+
if (extra.length)
|
|
195
|
+
errors.push(`unknown top-level fields: ${extra.join(', ')}`);
|
|
196
|
+
if (raw.v !== 1)
|
|
197
|
+
errors.push('v must be 1');
|
|
198
|
+
if (typeof raw.name !== 'string' || !raw.name || raw.name.length > 64) {
|
|
199
|
+
errors.push('name must be a non-empty string (max 64)');
|
|
200
|
+
}
|
|
201
|
+
const description = optionalString(raw.description, 'description', errors, 200);
|
|
202
|
+
if (raw.enabled !== undefined && typeof raw.enabled !== 'boolean')
|
|
203
|
+
errors.push('enabled must be a boolean');
|
|
204
|
+
if (raw.unmatched !== undefined && raw.unmatched !== 'silent' && raw.unmatched !== 'reject') {
|
|
205
|
+
errors.push('unmatched must be "silent" or "reject"');
|
|
206
|
+
}
|
|
207
|
+
const requires = validateRequires(raw.requires, errors);
|
|
208
|
+
const listen = [];
|
|
209
|
+
if (raw.listen === undefined)
|
|
210
|
+
errors.push('listen is required');
|
|
211
|
+
else if (!Array.isArray(raw.listen))
|
|
212
|
+
errors.push('listen must be an array');
|
|
213
|
+
else if (raw.listen.length === 0)
|
|
214
|
+
errors.push('listen must have at least one entry');
|
|
215
|
+
else {
|
|
216
|
+
for (const item of raw.listen) {
|
|
217
|
+
const entry = validateListenEntry(item, errors);
|
|
218
|
+
if (entry)
|
|
219
|
+
listen.push(entry);
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
// 包内 type 唯一(含通配原文,同字面重复)。
|
|
223
|
+
const seen = new Set();
|
|
224
|
+
for (const entry of listen) {
|
|
225
|
+
if (seen.has(entry.type))
|
|
226
|
+
errors.push(`duplicate listen.type: ${entry.type}`);
|
|
227
|
+
else
|
|
228
|
+
seen.add(entry.type);
|
|
229
|
+
}
|
|
230
|
+
const reply = validateReplyPolicy(raw.reply, errors);
|
|
231
|
+
if (errors.length)
|
|
232
|
+
return { ok: false, errors };
|
|
233
|
+
return {
|
|
234
|
+
ok: true,
|
|
235
|
+
errors: [],
|
|
236
|
+
value: {
|
|
237
|
+
name: raw.name,
|
|
238
|
+
...(description !== undefined ? { description } : {}),
|
|
239
|
+
enabled: raw.enabled !== false,
|
|
240
|
+
requires: requires,
|
|
241
|
+
listen,
|
|
242
|
+
unmatched: raw.unmatched ?? 'silent',
|
|
243
|
+
reply,
|
|
244
|
+
},
|
|
245
|
+
};
|
|
246
|
+
}
|
|
247
|
+
function validateQuota(raw, field, fallback, errors) {
|
|
248
|
+
if (raw === undefined)
|
|
249
|
+
return { ...fallback };
|
|
250
|
+
if (!isRecord(raw)) {
|
|
251
|
+
errors.push(`${field} must be an object`);
|
|
252
|
+
return { ...fallback };
|
|
253
|
+
}
|
|
254
|
+
const extra = Object.keys(raw).filter(key => !['perMinute', 'perHour'].includes(key));
|
|
255
|
+
if (extra.length)
|
|
256
|
+
errors.push(`${field} has unknown fields: ${extra.join(', ')}`);
|
|
257
|
+
const perMinute = intInRange(raw.perMinute, `${field}.perMinute`, 1, Number.MAX_SAFE_INTEGER, errors);
|
|
258
|
+
const perHour = intInRange(raw.perHour, `${field}.perHour`, 1, Number.MAX_SAFE_INTEGER, errors);
|
|
259
|
+
return { perMinute: perMinute ?? fallback.perMinute, perHour: perHour ?? fallback.perHour };
|
|
260
|
+
}
|
|
261
|
+
/** 校验 router.json 原始对象。 */
|
|
262
|
+
export function validateRouterConfig(raw) {
|
|
263
|
+
const errors = [];
|
|
264
|
+
if (!isRecord(raw))
|
|
265
|
+
return { ok: false, errors: ['router.json must be a JSON object'] };
|
|
266
|
+
const extra = Object.keys(raw).filter(key => !['v', 'enabled', 'rateLimit'].includes(key));
|
|
267
|
+
if (extra.length)
|
|
268
|
+
errors.push(`unknown top-level fields: ${extra.join(', ')}`);
|
|
269
|
+
if (raw.v !== 1)
|
|
270
|
+
errors.push('v must be 1');
|
|
271
|
+
if (raw.enabled !== undefined && typeof raw.enabled !== 'boolean')
|
|
272
|
+
errors.push('enabled must be a boolean');
|
|
273
|
+
let rateLimit = {
|
|
274
|
+
builtin: { ...QUOTA_DEFAULT_BUILTIN },
|
|
275
|
+
service: { ...QUOTA_DEFAULT_SERVICE },
|
|
276
|
+
};
|
|
277
|
+
if (raw.rateLimit !== undefined) {
|
|
278
|
+
if (!isRecord(raw.rateLimit))
|
|
279
|
+
errors.push('rateLimit must be an object');
|
|
280
|
+
else {
|
|
281
|
+
const rateExtra = Object.keys(raw.rateLimit).filter(key => !['builtin', 'service'].includes(key));
|
|
282
|
+
if (rateExtra.length)
|
|
283
|
+
errors.push(`rateLimit has unknown fields: ${rateExtra.join(', ')}`);
|
|
284
|
+
rateLimit = {
|
|
285
|
+
builtin: validateQuota(raw.rateLimit.builtin, 'rateLimit.builtin', QUOTA_DEFAULT_BUILTIN, errors),
|
|
286
|
+
service: validateQuota(raw.rateLimit.service, 'rateLimit.service', QUOTA_DEFAULT_SERVICE, errors),
|
|
287
|
+
};
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
if (errors.length)
|
|
291
|
+
return { ok: false, errors };
|
|
292
|
+
return {
|
|
293
|
+
ok: true,
|
|
294
|
+
errors: [],
|
|
295
|
+
value: { v: 1, enabled: raw.enabled === true, rateLimit },
|
|
296
|
+
};
|
|
297
|
+
}
|
|
@@ -9,6 +9,20 @@ import { logger } from '../utils/logger.js';
|
|
|
9
9
|
export class TriggerSchedulerRegistry {
|
|
10
10
|
schedulers = new Map();
|
|
11
11
|
starts = new Map();
|
|
12
|
+
listeners = [];
|
|
13
|
+
addLifecycleListener(listener) {
|
|
14
|
+
this.listeners.push(listener);
|
|
15
|
+
}
|
|
16
|
+
async notify(action) {
|
|
17
|
+
for (const listener of this.listeners) {
|
|
18
|
+
try {
|
|
19
|
+
await action(listener);
|
|
20
|
+
}
|
|
21
|
+
catch (error) {
|
|
22
|
+
logger.error(`[Trigger] lifecycle listener failed: ${error instanceof Error ? error.message : String(error)}`);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
}
|
|
12
26
|
get(agentAid) {
|
|
13
27
|
return this.schedulers.get(agentAid);
|
|
14
28
|
}
|
|
@@ -36,7 +50,11 @@ export class TriggerSchedulerRegistry {
|
|
|
36
50
|
}
|
|
37
51
|
catch (error) {
|
|
38
52
|
logger.error(`[Trigger] Scheduler init failed for ${agentAid}: ${error instanceof Error ? error.message : String(error)}`);
|
|
53
|
+
// Propagate failure to admission/readiness and allow ensureStarted to retry.
|
|
54
|
+
throw error;
|
|
39
55
|
}
|
|
56
|
+
// 附属服务不阻塞调度器启动。
|
|
57
|
+
void this.notify(listener => listener.started?.(agentAid));
|
|
40
58
|
}
|
|
41
59
|
async ensureStarted(agentAid, start) {
|
|
42
60
|
const existing = this.starts.get(agentAid);
|
|
@@ -54,6 +72,7 @@ export class TriggerSchedulerRegistry {
|
|
|
54
72
|
}
|
|
55
73
|
}
|
|
56
74
|
async remove(agentAid) {
|
|
75
|
+
await this.notify(listener => listener.removed?.(agentAid));
|
|
57
76
|
await this.schedulers.get(agentAid)?.stop();
|
|
58
77
|
this.schedulers.delete(agentAid);
|
|
59
78
|
this.starts.delete(agentAid);
|
|
@@ -67,6 +86,7 @@ export class TriggerSchedulerRegistry {
|
|
|
67
86
|
await Promise.all([...this.schedulers.values()].map(scheduler => scheduler.waitForIdle(timeoutMs)));
|
|
68
87
|
}
|
|
69
88
|
async suspendForDaemonExit() {
|
|
89
|
+
await this.notify(listener => listener.suspending?.());
|
|
70
90
|
await Promise.all([...this.schedulers.values()].map(scheduler => scheduler.suspendForDaemonExit()));
|
|
71
91
|
}
|
|
72
92
|
}
|
|
@@ -108,7 +128,7 @@ export function buildTriggerScheduler(owner, dependencies, options = {}) {
|
|
|
108
128
|
removeConsolidationCheckTrigger(manager);
|
|
109
129
|
}
|
|
110
130
|
const state = new TriggerRunStateStore(manager);
|
|
111
|
-
const agentTriggerAudit = dependencies.triggerAudit.withHistory(manager.history);
|
|
131
|
+
const agentTriggerAudit = dependencies.triggerAudit.withHistory(manager.history, { deferHistoryImport: true });
|
|
112
132
|
const dispatcher = new TriggerFeedbackDispatcher({
|
|
113
133
|
getChannel: dependencies.getTriggerChannel,
|
|
114
134
|
sessionManager: dependencies.sessionManager,
|
package/dist/trigger/audit.js
CHANGED
|
@@ -6,10 +6,14 @@ export class TriggerAuditLogger {
|
|
|
6
6
|
history;
|
|
7
7
|
writer;
|
|
8
8
|
logDir;
|
|
9
|
-
|
|
9
|
+
historyReady;
|
|
10
|
+
constructor(logDir = resolvePaths().logs, history, writer, options = {}) {
|
|
10
11
|
this.history = history;
|
|
11
12
|
this.logDir = logDir;
|
|
12
|
-
|
|
13
|
+
if (!options.deferHistoryImport) {
|
|
14
|
+
this.history?.importAuditLogs(logDir);
|
|
15
|
+
this.historyReady = Promise.resolve();
|
|
16
|
+
}
|
|
13
17
|
this.writer = writer ?? new LogWriter({
|
|
14
18
|
baseName: 'trigger-runs',
|
|
15
19
|
logDir,
|
|
@@ -17,8 +21,19 @@ export class TriggerAuditLogger {
|
|
|
17
21
|
retention: { days: 7 },
|
|
18
22
|
});
|
|
19
23
|
}
|
|
20
|
-
withHistory(history) {
|
|
21
|
-
return new TriggerAuditLogger(this.logDir, history, this.writer);
|
|
24
|
+
withHistory(history, options = {}) {
|
|
25
|
+
return new TriggerAuditLogger(this.logDir, history, this.writer, options);
|
|
26
|
+
}
|
|
27
|
+
initializeHistory() {
|
|
28
|
+
return this.historyReady ??= (async () => {
|
|
29
|
+
try {
|
|
30
|
+
await this.history?.importAuditLogsAsync(this.logDir);
|
|
31
|
+
}
|
|
32
|
+
catch (error) {
|
|
33
|
+
this.historyReady = undefined;
|
|
34
|
+
throw error;
|
|
35
|
+
}
|
|
36
|
+
})();
|
|
22
37
|
}
|
|
23
38
|
write(record) {
|
|
24
39
|
this.writer.write(JSON.stringify(record));
|
|
@@ -2,7 +2,7 @@ import crypto from 'crypto';
|
|
|
2
2
|
import { eventPatternIncludesInternal } from '../core/event-catalog.js';
|
|
3
3
|
import { trustedExecutionIdentityInput } from '../core/auth/agent-delegation.js';
|
|
4
4
|
import { authorizeOperation, buildAuthSubject } from '../core/auth/auth-gateway.js';
|
|
5
|
-
import { refreshOwnershipForAuthorization } from '../core/ownership/owner-resolver.js';
|
|
5
|
+
import { ownerResolver, refreshOwnershipForAuthorization } from '../core/ownership/owner-resolver.js';
|
|
6
6
|
import { isManagementRole } from '../config/builtin-roles.js';
|
|
7
7
|
import { checkRoleAccess, resolvePeerRoleDetail, roleToSessionIdentity } from '../config/peer-role-resolver.js';
|
|
8
8
|
import { validateModelSelectionForRole } from '../core/model/model-permission.js';
|
|
@@ -215,9 +215,21 @@ export async function authorizeTriggerExecution(definition, dependencies) {
|
|
|
215
215
|
&& definition.labels.originAuthorization !== 'creator-session-v1') {
|
|
216
216
|
return { allowed: false, reason: 'legacy cross-thread review creator is unverified; recreate the Trigger from its source conversation' };
|
|
217
217
|
}
|
|
218
|
-
|
|
218
|
+
const origin = definition.origin;
|
|
219
|
+
if (!origin?.peerId)
|
|
220
|
+
return { allowed: false, reason: 'trigger origin peer is missing' };
|
|
221
|
+
if (definition.execution.permissionMode === 'fullaccess' && !definition.authorizedBy) {
|
|
222
|
+
return { allowed: false, reason: 'fullaccess Trigger approval provenance is missing' };
|
|
223
|
+
}
|
|
224
|
+
const ownership = await refreshOwnershipForAuthorization(definition.agentAid, {
|
|
219
225
|
allowTransient: true, includeDaemon: definition.execution.permissionMode === 'fullaccess',
|
|
220
226
|
});
|
|
227
|
+
const originType = origin.channelType || tryParseChannelKey(origin.channelKey)?.type;
|
|
228
|
+
const requiresDaemonOwner = definition.execution.permissionMode === 'fullaccess';
|
|
229
|
+
if (ownership.some(view => view.consensusStatus === 'unavailable'
|
|
230
|
+
&& (requiresDaemonOwner ? view.subjectAid === ownerResolver.getDaemonAid() : originType === 'aun'))) {
|
|
231
|
+
return { allowed: false, retryable: true, reason: 'Ownership authorization evidence is temporarily unavailable' };
|
|
232
|
+
}
|
|
221
233
|
if (definition.execution.permissionMode === 'fullaccess') {
|
|
222
234
|
if (!definition.authorizedBy) {
|
|
223
235
|
return { allowed: false, reason: 'fullaccess Trigger approval provenance is missing' };
|
|
@@ -227,9 +239,6 @@ export async function authorizeTriggerExecution(definition, dependencies) {
|
|
|
227
239
|
return { allowed: false, reason: 'fullaccess Trigger approver is no longer a current daemon-owner' };
|
|
228
240
|
}
|
|
229
241
|
}
|
|
230
|
-
const origin = definition.origin;
|
|
231
|
-
if (!origin?.peerId)
|
|
232
|
-
return { allowed: false, reason: 'trigger origin peer is missing' };
|
|
233
242
|
if (definition.execution.permissionMode === 'fullaccess') {
|
|
234
243
|
return { allowed: true, scope: 'daemon', permissionMode: 'fullaccess' };
|
|
235
244
|
}
|
|
@@ -99,7 +99,7 @@ function matchPattern(eventType, pattern) {
|
|
|
99
99
|
return eventType.startsWith(pattern.slice(0, -1));
|
|
100
100
|
return false;
|
|
101
101
|
}
|
|
102
|
-
function matchFilter(event, filter) {
|
|
102
|
+
export function matchFilter(event, filter) {
|
|
103
103
|
if (!filter.match)
|
|
104
104
|
return true;
|
|
105
105
|
for (const [path, expected] of Object.entries(filter.match)) {
|
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
|
+
}
|