evolcore 0.0.40 → 0.0.41-test.1
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 +94 -1
- package/bin/codex-managed-hook.mjs +11 -0
- package/bin/ec-safe-output.js +11 -1
- package/dist/agents/baseagent-availability.js +1 -1
- package/dist/agents/baseagent.js +20 -43
- package/dist/agents/claude-runner.js +197 -197
- package/dist/agents/codex-runner.js +535 -259
- package/dist/agents/ecagent-runner.js +31 -12
- package/dist/agents/gemini-runner.js +27 -7
- package/dist/agents/runner-types.js +11 -3
- package/dist/aun/ability-device-routing.js +26 -0
- package/dist/aun/aid/client.js +1 -1
- package/dist/aun/group/fs-download.js +39 -0
- package/dist/aun/group/identity.js +36 -13
- package/dist/aun/group/operations.js +5 -0
- package/dist/aun/msg/managed-operation.js +72 -11
- package/dist/aun/msg/p2p.js +32 -7
- package/dist/aun/service-proxy.js +36 -12
- package/dist/aun/storage/download.js +30 -0
- package/dist/aun/storage/index.js +14 -6
- package/dist/channels/aun.js +367 -24
- package/dist/channels/daemon.js +39 -5
- package/dist/cli/agent-command.js +2 -0
- package/dist/cli/agent-create.js +98 -19
- package/dist/cli/agent.js +10 -3
- package/dist/cli/aun-commands.js +85 -41
- package/dist/cli/daemon-commands.js +678 -688
- package/dist/cli/fs-command.js +64 -2
- package/dist/cli/index.js +48 -4
- package/dist/cli/managed-command-guard.js +4 -0
- package/dist/cli/model.js +40 -40
- package/dist/cli/proxy-command.js +92 -0
- package/dist/cli/restart-monitor.js +17 -0
- package/dist/cli/role.js +85 -0
- package/dist/cli/session-command.js +128 -13
- package/dist/cli/stats.js +60 -4
- package/dist/cli/trigger-command.js +2 -2
- package/dist/config/access-policy.js +8 -1
- package/dist/config/boot-log.js +38 -1
- package/dist/config/config-field-policy.js +5 -32
- package/dist/config/config-manager.js +46 -39
- package/dist/config/config-operation-service.js +8 -11
- package/dist/config/contact-book-store.js +47 -21
- package/dist/config/contact-book.js +18 -12
- package/dist/config/contact-operation-service.js +3 -4
- package/dist/config/contact-request-service.js +11 -0
- package/dist/config/daemon-services.js +11 -0
- package/dist/config/gateway-config.js +89 -595
- package/dist/config/peer-role-resolver.js +15 -11
- package/dist/config/providers-config.js +12 -7
- package/dist/config/providers-import.js +3 -21
- package/dist/config/resolved-config-op.js +3 -0
- package/dist/config/role-command-service.js +166 -0
- package/dist/config/role-request-service.js +193 -0
- package/dist/config/schema-migration.js +686 -25
- package/dist/config-store.js +79 -3
- package/dist/contracts/ecweb-instance.js +41 -0
- package/dist/contracts/ecweb-service-protocol.js +3 -1
- package/dist/core/agent-application-service.js +11 -1
- package/dist/core/agent-ipc-registration.js +4 -0
- package/dist/core/agent-runtime.js +1 -1
- package/dist/core/auth/agent-delegation.js +82 -6
- package/dist/core/auth/auth-gateway.js +1 -1
- package/dist/core/auth/authorization-audit.js +37 -9
- package/dist/core/auth/delegation-authorization.js +3 -1
- package/dist/core/auth/operation-authorizer.js +15 -0
- package/dist/core/auth/operation-catalog.js +39 -6
- package/dist/core/command/agent-control.js +94 -19
- package/dist/core/command/agent-storage-menu.js +5 -3
- package/dist/core/command/cli-intent-parser.js +2 -1
- package/dist/core/command/command-handler.js +105 -145
- package/dist/core/command/connect-menu.js +31 -2
- package/dist/core/command/data-root-resolver.js +5 -4
- package/dist/core/command/memory-slash.js +16 -8
- package/dist/core/command/menu-catalog.js +6 -6
- package/dist/core/command/menu-handler.js +87 -163
- package/dist/core/command/menu-protocol.js +9 -2
- package/dist/core/command/role-menu.js +148 -30
- package/dist/core/command/slash-gate.js +13 -10
- package/dist/core/command/slash-handler.js +157 -352
- package/dist/core/daemon-control.js +8 -14
- package/dist/core/event-catalog.js +15 -0
- package/dist/core/evolagent-registry.js +4 -4
- package/dist/core/evolagent.js +2 -2
- package/dist/core/inference/text-inference.js +31 -40
- package/dist/core/ipc-registration.js +24 -3
- package/dist/core/memory/consolidation-context.js +51 -5
- package/dist/core/memory/consolidation-ops-records.js +1 -1
- package/dist/core/memory/consolidation-trigger.js +190 -190
- package/dist/core/memory/consolidation.js +316 -29
- package/dist/core/memory/forget-log.js +96 -0
- package/dist/core/memory/index-manager.js +27 -5
- package/dist/core/memory/memory-admin.js +45 -5
- package/dist/core/memory/memory-writer.js +163 -18
- package/dist/core/memory/relation-idle-monitor.js +119 -58
- package/dist/core/memory/relation-sweep.js +44 -39
- package/dist/core/message/aun-ipc-sender.js +34 -7
- package/dist/core/message/aun-request-registry.js +97 -3
- package/dist/core/message/im-renderer.js +11 -4
- package/dist/core/message/message-admission.js +97 -8
- package/dist/core/message/message-bridge.js +71 -24
- package/dist/core/message/message-log.js +55 -0
- package/dist/core/message/message-queue.js +5 -5
- package/dist/core/message/response-engine.js +387 -144
- package/dist/core/model/catalog-service.js +4 -2
- package/dist/core/model/config-scope.js +24 -64
- package/dist/core/model/field-scope.js +1 -1
- package/dist/core/model/model-catalog.js +37 -47
- package/dist/core/model/model-diagnostics.js +4 -3
- package/dist/core/model/model-fallback.js +10 -7
- package/dist/core/model/model-permission.js +3 -2
- package/dist/core/ownership/owner-resolver.js +25 -23
- package/dist/core/permission/ec-command-parser.js +138 -5
- package/dist/core/permission/sandbox-runtime.js +18 -1
- package/dist/core/permission/tool-policy.js +42 -10
- package/dist/core/relation/peer-identity.js +64 -1
- package/dist/core/role/runtime-policy.js +72 -13
- package/dist/core/session/session-capsule.js +197 -0
- package/dist/core/session/session-idle-monitor.js +1 -1
- package/dist/core/session/session-manager.js +168 -14
- package/dist/core/session/session-mapper.js +2 -0
- package/dist/core/session/session-renew.js +3 -3
- package/dist/core/skill/index-generator.js +15 -5
- package/dist/core/system-session/memory.js +6 -1
- package/dist/core/thread/thread-service.js +19 -6
- package/dist/directory/owner-directory-admission.js +95 -0
- package/dist/directory/owner-directory-authorization.js +309 -0
- package/dist/directory/owner-directory-cache.js +635 -0
- package/dist/eck/kit-renderer.js +2 -2
- package/dist/ecweb/basic-settings-service.js +33 -40
- package/dist/ecweb/message-context-service.js +110 -0
- package/dist/index.js +79 -17
- package/dist/ipc.js +189 -17
- package/dist/paths.js +4 -0
- package/dist/proxy/command.js +122 -0
- package/dist/proxy/ipc-service.js +68 -0
- package/dist/proxy/manager.js +485 -0
- package/dist/proxy/service-supervisor.js +603 -0
- package/dist/response-system/engines/v1/proactive-flow.js +16 -6
- package/dist/stats/budget.js +1 -1
- package/dist/stats/db.js +15 -0
- package/dist/stats/index.js +2 -2
- package/dist/stats/price-resolver.js +304 -5
- package/dist/stats/query.js +82 -3
- package/dist/stats/writer.js +38 -5
- package/dist/trigger/feedback.js +18 -7
- package/dist/trigger/manager.js +20 -3
- package/dist/trigger/parser.js +6 -3
- package/dist/trigger/patch.js +1 -1
- package/dist/trigger/system-definitions.js +43 -7
- package/dist/trigger/validation.js +13 -5
- package/dist/utils/cross-platform.js +14 -0
- package/dist/utils/ecweb-supervisor.js +258 -83
- package/dist/utils/error-utils.js +16 -2
- package/dist/utils/logger.js +24 -5
- package/dist/utils/npm-ops.js +2 -1
- package/dist/utils/package-version-info.js +5 -3
- package/dist/utils/upgrade-jobs.js +45 -5
- package/kits/docs/INDEX.md +1 -1
- package/kits/docs/aun/ABILITY_CALLING.md +20 -2
- package/kits/docs/channels/aun.md +9 -0
- package/kits/docs/evolcore/INDEX.md +2 -1
- package/kits/docs/evolcore/agent-create.md +3 -2
- package/kits/docs/evolcore/agent.md +1 -1
- package/kits/docs/evolcore/config.md +18 -7
- package/kits/docs/evolcore/contact.md +3 -0
- package/kits/docs/evolcore/event.md +1 -0
- package/kits/docs/evolcore/fs.md +16 -0
- package/kits/docs/evolcore/memory-architecture.md +2 -2
- package/kits/docs/evolcore/memory-consolidation.md +32 -8
- package/kits/docs/evolcore/memory.md +1 -1
- package/kits/docs/evolcore/menu-protocol.md +36 -0
- package/kits/docs/evolcore/model.md +23 -20
- package/kits/docs/evolcore/msg.md +21 -8
- package/kits/docs/evolcore/session.md +14 -0
- package/kits/migrations/migrate-contact-book-v2.mjs +30 -11
- package/kits/schemas/_meta.json +37 -4
- package/kits/schemas/agent-config.schema.13.json +23 -3
- package/kits/schemas/agent-config.schema.16.json +891 -0
- package/kits/schemas/daemon.schema.9.json +366 -0
- package/kits/schemas/defaults.schema.5.json +406 -127
- package/kits/schemas/relation-config.schema.11.json +260 -0
- package/kits/schemas/role-config.schema.1.json +7 -7
- package/kits/skills/.switches.json +3 -7
- package/kits/skills/aid-ability/SKILL.md +35 -6
- package/kits/skills/ec-service-proxy/SKILL.md +118 -0
- package/kits/skills/ec-trigger/SKILL.md +13 -5
- package/kits/skills/personal-assistant/SKILL.md +176 -0
- package/kits/skills/personal-assistant/references/01-workspace-spec.md +333 -0
- package/kits/skills/personal-assistant/references/02-bootstrap.md +205 -0
- package/kits/skills/personal-assistant/references/03-data-flow-and-gaps.md +177 -0
- package/kits/skills/personal-assistant/references/04-frontend-contract.md +439 -0
- package/kits/skills/personal-assistant/references/05-directory-reference.md +292 -0
- package/kits/skills/personal-assistant/references/06-render-config.md +350 -0
- package/kits/skills/personal-assistant/schema/workspace.schema.json +85 -0
- package/kits/skills/personal-assistant/schema/workspace.v1.json +66 -0
- package/kits/skills/personal-assistant/scripts/archive.mjs +154 -0
- package/kits/skills/personal-assistant/scripts/check.mjs +114 -0
- package/kits/skills/personal-assistant/scripts/index-rebuild.mjs +252 -0
- package/kits/skills/personal-assistant/scripts/ingest.mjs +344 -0
- package/kits/skills/personal-assistant/scripts/init.mjs +270 -0
- package/kits/skills/personal-assistant/scripts/lib/apply.mjs +92 -0
- package/kits/skills/personal-assistant/scripts/lib/schema.mjs +96 -0
- package/kits/skills/personal-assistant/scripts/lib/time.mjs +28 -0
- package/kits/skills/personal-assistant/scripts/migrate.mjs +172 -0
- package/kits/skills/personal-assistant/scripts/search.mjs +172 -0
- package/kits/skills/personal-assistant/scripts/verify.mjs +196 -0
- package/kits/skills/personal-assistant/templates/INDEX.md.tpl +24 -0
- package/kits/skills/personal-assistant/templates/README.md.tpl +108 -0
- package/kits/skills/personal-assistant/triggers/ingest-daily.json +23 -0
- package/kits/skills/personal-assistant/triggers/memory-consolidate.json +23 -0
- package/kits/skills/personal-assistant/triggers/report-daily.json +23 -0
- package/kits/skills/personal-assistant/triggers/report-weekly.json +23 -0
- package/kits/templates/memory-consolidation/forget.md +19 -6
- package/kits/templates/memory-consolidation/notes.md +1 -1
- package/kits/templates/memory-consolidation/profile.md +2 -2
- package/kits/templates/memory-consolidation/promote.md +20 -3
- package/kits/templates/roles/member.json +8 -1
- package/kits/templates/session-capsule/recovery.md +5 -0
- package/kits/templates/session-capsule/renew.md +5 -0
- package/kits/templates/system-fragments/baseagent.md +2 -2
- package/kits/templates/system-fragments/bootstrap.md +6 -6
- package/kits/templates/system-fragments/channel.md +1 -1
- package/kits/templates/system-fragments/memory.md +4 -4
- package/package.json +7 -3
- package/dist/stats/billing.js +0 -215
|
@@ -0,0 +1,635 @@
|
|
|
1
|
+
import fs from 'node:fs';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import { isValidAid } from '../aun/aid/validation.js';
|
|
4
|
+
import { readAuthoritativePersonalFsObject } from '../aun/group/fs-download.js';
|
|
5
|
+
import { atomicReadJson, atomicWriteJson } from '../utils/atomic-write.js';
|
|
6
|
+
import { logger } from '../utils/logger.js';
|
|
7
|
+
export const OWNER_DIRECTORY_SCOPES = ['contacts', 'agents'];
|
|
8
|
+
export const OWNER_DIRECTORY_MAX_OBJECT_BYTES = 24 * 1024;
|
|
9
|
+
export const OWNER_DIRECTORY_STALE_AFTER_MS = 30 * 24 * 60 * 60_000;
|
|
10
|
+
export const OWNER_DIRECTORY_RETRY_BACKOFF_MS = 60 * 60_000;
|
|
11
|
+
export const OWNER_DIRECTORY_INITIAL_WAIT_MS = 3_000;
|
|
12
|
+
const MAX_DIRECTORY_ENTRIES = 100_000;
|
|
13
|
+
const MAX_SNAPSHOT_OBJECTS = 2_000;
|
|
14
|
+
const MAX_INDEX_SEGMENTS = 1_000;
|
|
15
|
+
const MAX_PAGES_PER_INDEX = 50;
|
|
16
|
+
const SNAPSHOT_ID_RE = /^[A-Za-z0-9_-]{1,128}$/;
|
|
17
|
+
const SHA256_RE = /^[a-f0-9]{64}$/;
|
|
18
|
+
const GRANT_STATUSES = new Set([
|
|
19
|
+
'active', 'not_authorized', 'expired', 'revoked', 'revoke_pending', 'sync_pending',
|
|
20
|
+
]);
|
|
21
|
+
export function parseOwnerDirectoryGrantAbilityResponse(value) {
|
|
22
|
+
if (!isRecord(value) || value.type !== 'ability' || value.role !== 'result'
|
|
23
|
+
|| value.ability_path !== 'directory/access' || value.definition_id !== 'evol-directory-access-v1'
|
|
24
|
+
|| value.ok !== true || value.execution_state !== 'completed' || !isRecord(value.result))
|
|
25
|
+
return undefined;
|
|
26
|
+
return value.result;
|
|
27
|
+
}
|
|
28
|
+
export class OwnerDirectoryCacheError extends Error {
|
|
29
|
+
code;
|
|
30
|
+
constructor(code, message) {
|
|
31
|
+
super(message);
|
|
32
|
+
this.code = code;
|
|
33
|
+
this.name = 'OwnerDirectoryCacheError';
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
/** Per-Agent durable snapshots; ECK and prompt rendering are intentionally outside this service. */
|
|
37
|
+
export class OwnerDirectoryCache {
|
|
38
|
+
options;
|
|
39
|
+
now;
|
|
40
|
+
initialWaitMs;
|
|
41
|
+
staleAfterMs;
|
|
42
|
+
retryBackoffMs;
|
|
43
|
+
inFlight = new Map();
|
|
44
|
+
stateWriteTail = Promise.resolve();
|
|
45
|
+
constructor(options) {
|
|
46
|
+
this.options = options;
|
|
47
|
+
if (!isValidAid(options.selfAid) || !isValidAid(options.ownerAid)) {
|
|
48
|
+
throw new TypeError('owner directory cache requires valid self and owner AIDs');
|
|
49
|
+
}
|
|
50
|
+
if (!options.directory || !path.isAbsolute(options.directory)) {
|
|
51
|
+
throw new TypeError('owner directory cache path must be absolute');
|
|
52
|
+
}
|
|
53
|
+
this.now = options.now ?? Date.now;
|
|
54
|
+
this.initialWaitMs = options.initialWaitMs ?? OWNER_DIRECTORY_INITIAL_WAIT_MS;
|
|
55
|
+
this.staleAfterMs = options.staleAfterMs ?? OWNER_DIRECTORY_STALE_AFTER_MS;
|
|
56
|
+
this.retryBackoffMs = options.retryBackoffMs ?? OWNER_DIRECTORY_RETRY_BACKOFF_MS;
|
|
57
|
+
}
|
|
58
|
+
/** Persist a directory/access result only after validating its subject and active scopes. */
|
|
59
|
+
saveAuthorization(result) {
|
|
60
|
+
logger.info(`[OwnerDirectoryCache] Saving authorization for Owner ${this.options.ownerAid}, status: ${result.status}`);
|
|
61
|
+
if (!isRecord(result) || result.owner_aid !== this.options.ownerAid
|
|
62
|
+
|| result.grantee_aid !== this.options.selfAid || !GRANT_STATUSES.has(result.status)) {
|
|
63
|
+
logger.error(`[OwnerDirectoryCache] Authorization validation failed`);
|
|
64
|
+
throw new OwnerDirectoryCacheError('INVALID_GRANT', 'directory/access result identity or status is invalid');
|
|
65
|
+
}
|
|
66
|
+
const rawScopes = result.active_scopes ?? [];
|
|
67
|
+
if (!Array.isArray(rawScopes) || rawScopes.some(scope => !isScope(scope))) {
|
|
68
|
+
throw new OwnerDirectoryCacheError('INVALID_GRANT', 'directory/access active_scopes is invalid');
|
|
69
|
+
}
|
|
70
|
+
const activeScopes = [...new Set(rawScopes)];
|
|
71
|
+
const expiry = isRecord(result.expires_at_by_scope) ? result.expires_at_by_scope : {};
|
|
72
|
+
const expiresAtByScope = {};
|
|
73
|
+
for (const scope of activeScopes) {
|
|
74
|
+
const expiresAt = expiry[scope];
|
|
75
|
+
if (!Number.isSafeInteger(expiresAt) || expiresAt < 0) {
|
|
76
|
+
throw new OwnerDirectoryCacheError('INVALID_GRANT', `directory/access has invalid expiry for ${scope}`);
|
|
77
|
+
}
|
|
78
|
+
expiresAtByScope[scope] = expiresAt;
|
|
79
|
+
}
|
|
80
|
+
const refs = isRecord(result.manifest_refs) ? result.manifest_refs : {};
|
|
81
|
+
const manifestRefs = {};
|
|
82
|
+
for (const scope of activeScopes) {
|
|
83
|
+
if (refs[scope] !== undefined) {
|
|
84
|
+
const serialized = JSON.stringify(refs[scope]);
|
|
85
|
+
if (!serialized || Buffer.byteLength(serialized, 'utf8') > OWNER_DIRECTORY_MAX_OBJECT_BYTES) {
|
|
86
|
+
throw new OwnerDirectoryCacheError('INVALID_GRANT', `directory/access manifest ref for ${scope} is invalid`);
|
|
87
|
+
}
|
|
88
|
+
manifestRefs[scope] = JSON.parse(serialized);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
const effectiveScopes = ['active', 'sync_pending', 'revoke_pending'].includes(result.status)
|
|
92
|
+
? activeScopes
|
|
93
|
+
: [];
|
|
94
|
+
if (result.status === 'active' && effectiveScopes.length === 0) {
|
|
95
|
+
throw new OwnerDirectoryCacheError('INVALID_GRANT', 'active directory/access result has no active scopes');
|
|
96
|
+
}
|
|
97
|
+
const grant = {
|
|
98
|
+
schemaVersion: 1,
|
|
99
|
+
status: result.status,
|
|
100
|
+
ownerAid: result.owner_aid,
|
|
101
|
+
granteeAid: result.grantee_aid,
|
|
102
|
+
activeScopes: effectiveScopes,
|
|
103
|
+
knownScopes: [...new Set([...(this.loadGrant()?.knownScopes ?? []), ...activeScopes])],
|
|
104
|
+
expiresAtByScope,
|
|
105
|
+
manifestRefs,
|
|
106
|
+
storedAt: this.now(),
|
|
107
|
+
};
|
|
108
|
+
logger.info(`[OwnerDirectoryCache] Persisting authorization: activeScopes=${JSON.stringify(effectiveScopes)}, path=${this.authorizationPath()}`);
|
|
109
|
+
fs.mkdirSync(this.options.directory, { recursive: true, mode: 0o700 });
|
|
110
|
+
atomicWriteJson(this.authorizationPath(), grant);
|
|
111
|
+
fs.chmodSync(this.authorizationPath(), 0o600);
|
|
112
|
+
logger.info(`[OwnerDirectoryCache] Authorization saved successfully`);
|
|
113
|
+
void this.updateSyncState(state => ({
|
|
114
|
+
...state,
|
|
115
|
+
blockedScopes: state.blockedScopes.filter(scope => !effectiveScopes.includes(scope)),
|
|
116
|
+
}));
|
|
117
|
+
}
|
|
118
|
+
/** At startup, wait at most three seconds for first snapshots; stale refreshes stay in background. */
|
|
119
|
+
async refreshIfNeeded() {
|
|
120
|
+
const grant = this.loadGrant();
|
|
121
|
+
logger.info(`[OwnerDirectoryCache] refreshIfNeeded called for ${this.options.ownerAid}, grant exists: ${!!grant}`);
|
|
122
|
+
if (!grant) {
|
|
123
|
+
logger.warn(`[OwnerDirectoryCache] No authorization grant found, cannot refresh`);
|
|
124
|
+
return {};
|
|
125
|
+
}
|
|
126
|
+
logger.info(`[OwnerDirectoryCache] Active scopes: ${JSON.stringify(grant.activeScopes)}`);
|
|
127
|
+
const firstSyncs = [];
|
|
128
|
+
const result = {};
|
|
129
|
+
for (const scope of grant.activeScopes) {
|
|
130
|
+
if (!this.isAuthorized(grant, scope)) {
|
|
131
|
+
logger.debug(`[OwnerDirectoryCache] Scope ${scope} not authorized, skipping`);
|
|
132
|
+
continue;
|
|
133
|
+
}
|
|
134
|
+
const snapshot = this.readCachedSnapshot(scope);
|
|
135
|
+
if (!snapshot) {
|
|
136
|
+
logger.info(`[OwnerDirectoryCache] No cached snapshot for ${scope}, triggering first sync`);
|
|
137
|
+
firstSyncs.push(this.sync(scope).catch(error => {
|
|
138
|
+
logger.error(`[OwnerDirectoryCache] First sync failed for ${scope}: ${error}`);
|
|
139
|
+
this.options.onError?.(scope, error);
|
|
140
|
+
}));
|
|
141
|
+
continue;
|
|
142
|
+
}
|
|
143
|
+
logger.info(`[OwnerDirectoryCache] Found cached snapshot for ${scope}, total entries: ${snapshot.total}`);
|
|
144
|
+
result[scope] = snapshot;
|
|
145
|
+
if (this.isStale(scope)) {
|
|
146
|
+
logger.info(`[OwnerDirectoryCache] Snapshot for ${scope} is stale, triggering background refresh`);
|
|
147
|
+
this.startAutomaticSync(scope);
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
if (firstSyncs.length > 0) {
|
|
151
|
+
await waitAtMost(Promise.all(firstSyncs), this.initialWaitMs);
|
|
152
|
+
for (const scope of grant.activeScopes) {
|
|
153
|
+
if (this.isAuthorized(grant, scope)) {
|
|
154
|
+
const snapshot = this.readCachedSnapshot(scope);
|
|
155
|
+
if (snapshot)
|
|
156
|
+
result[scope] = snapshot;
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
return result;
|
|
161
|
+
}
|
|
162
|
+
/** Cache hits are local. A miss forces a complete remote sync before reporting not_found. */
|
|
163
|
+
async lookupByAid(scope, aid) {
|
|
164
|
+
if (!isScope(scope) || !isValidAid(aid)) {
|
|
165
|
+
return { status: 'unavailable', error: new OwnerDirectoryCacheError('INVALID_QUERY', 'scope or AID is invalid') };
|
|
166
|
+
}
|
|
167
|
+
logger.info(`[OwnerDirectoryCache] Looking up AID ${aid} in scope ${scope}`);
|
|
168
|
+
const current = this.readCachedSnapshot(scope);
|
|
169
|
+
const cachedEntry = current?.entries.find(entry => entry.aid === aid);
|
|
170
|
+
if (current && cachedEntry) {
|
|
171
|
+
logger.info(`[OwnerDirectoryCache] Found ${aid} in cached snapshot, name: ${cachedEntry.name}`);
|
|
172
|
+
if (this.isStale(scope))
|
|
173
|
+
this.startAutomaticSync(scope);
|
|
174
|
+
return { status: 'found', entry: cachedEntry, snapshot: current };
|
|
175
|
+
}
|
|
176
|
+
logger.info(`[OwnerDirectoryCache] AID ${aid} not in cache, forcing sync`);
|
|
177
|
+
try {
|
|
178
|
+
const refreshed = await this.sync(scope);
|
|
179
|
+
const entry = refreshed.entries.find(item => item.aid === aid);
|
|
180
|
+
if (entry) {
|
|
181
|
+
logger.info(`[OwnerDirectoryCache] Found ${aid} after sync, name: ${entry.name}`);
|
|
182
|
+
}
|
|
183
|
+
else {
|
|
184
|
+
logger.info(`[OwnerDirectoryCache] AID ${aid} not found in Owner directory ${scope}`);
|
|
185
|
+
}
|
|
186
|
+
return entry ? { status: 'found', entry, snapshot: refreshed } : { status: 'not_found', snapshot: refreshed };
|
|
187
|
+
}
|
|
188
|
+
catch (error) {
|
|
189
|
+
logger.error(`[OwnerDirectoryCache] Lookup failed for ${aid}: ${errorText(error)}`);
|
|
190
|
+
return { status: 'unavailable', error };
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
/** Called by an authenticated Owner-directory change handler; event data itself is ignored. */
|
|
194
|
+
async handleOwnerChangedEvent(senderAid) {
|
|
195
|
+
if (senderAid !== this.options.ownerAid) {
|
|
196
|
+
throw new OwnerDirectoryCacheError('UNTRUSTED_OWNER_EVENT', 'directory change sender is not the authorized Owner');
|
|
197
|
+
}
|
|
198
|
+
const grant = this.loadGrant();
|
|
199
|
+
if (!grant)
|
|
200
|
+
return {};
|
|
201
|
+
const scopes = grant.activeScopes.filter(scope => this.isAuthorized(grant, scope));
|
|
202
|
+
const results = await Promise.allSettled(scopes.map(async (scope) => [scope, await this.sync(scope)]));
|
|
203
|
+
return Object.fromEntries(results
|
|
204
|
+
.filter((result) => result.status === 'fulfilled')
|
|
205
|
+
.map(result => result.value));
|
|
206
|
+
}
|
|
207
|
+
async sync(scope) {
|
|
208
|
+
if (!isScope(scope))
|
|
209
|
+
throw new OwnerDirectoryCacheError('INVALID_SCOPE', 'unknown Owner directory scope');
|
|
210
|
+
const current = this.inFlight.get(scope);
|
|
211
|
+
if (current) {
|
|
212
|
+
logger.debug(`[OwnerDirectoryCache] Sync already in flight for ${scope}`);
|
|
213
|
+
return current;
|
|
214
|
+
}
|
|
215
|
+
const grant = this.loadGrant();
|
|
216
|
+
if (!grant || !this.isAuthorized(grant, scope)) {
|
|
217
|
+
logger.error(`[OwnerDirectoryCache] Not authorized to sync ${scope}`);
|
|
218
|
+
throw new OwnerDirectoryCacheError('NOT_AUTHORIZED', `Owner directory scope ${scope} is not active`);
|
|
219
|
+
}
|
|
220
|
+
logger.info(`[OwnerDirectoryCache] Starting sync for ${scope}`);
|
|
221
|
+
const task = this.runSync(scope, grant).finally(() => {
|
|
222
|
+
if (this.inFlight.get(scope) === task)
|
|
223
|
+
this.inFlight.delete(scope);
|
|
224
|
+
});
|
|
225
|
+
this.inFlight.set(scope, task);
|
|
226
|
+
return task;
|
|
227
|
+
}
|
|
228
|
+
readSnapshot(scope) {
|
|
229
|
+
if (!isScope(scope))
|
|
230
|
+
return undefined;
|
|
231
|
+
return this.readCachedSnapshot(scope);
|
|
232
|
+
}
|
|
233
|
+
/** A scope never granted is optional; an expired/revoked/blocked scope is unavailable. */
|
|
234
|
+
scopeAccess(scope) {
|
|
235
|
+
const grant = this.loadGrant();
|
|
236
|
+
if (!grant || !['active', 'sync_pending', 'revoke_pending'].includes(grant.status))
|
|
237
|
+
return 'unavailable';
|
|
238
|
+
if (!grant.activeScopes.includes(scope))
|
|
239
|
+
return grant.knownScopes.includes(scope) ? 'unavailable' : 'not_authorized';
|
|
240
|
+
if (!this.isAuthorized(grant, scope) || this.readSyncState().blockedScopes.includes(scope))
|
|
241
|
+
return 'unavailable';
|
|
242
|
+
return 'authorized';
|
|
243
|
+
}
|
|
244
|
+
async runSync(scope, grant) {
|
|
245
|
+
const attemptedAt = this.now();
|
|
246
|
+
logger.info(`[OwnerDirectoryCache] runSync starting for ${scope}`);
|
|
247
|
+
await this.updateFileState(scope, { lastAttemptAt: attemptedAt, status: 'syncing', error: undefined });
|
|
248
|
+
try {
|
|
249
|
+
logger.info(`[OwnerDirectoryCache] Fetching complete snapshot for ${scope} from Owner storage`);
|
|
250
|
+
const snapshot = await this.fetchCompleteSnapshot(scope);
|
|
251
|
+
logger.info(`[OwnerDirectoryCache] Snapshot fetched successfully for ${scope}, entries: ${snapshot.total}, snapshotId: ${snapshot.snapshotId}`);
|
|
252
|
+
const stored = { schemaVersion: 1, ...snapshot };
|
|
253
|
+
fs.mkdirSync(this.options.directory, { recursive: true, mode: 0o700 });
|
|
254
|
+
atomicWriteJson(this.cachePath(scope), stored);
|
|
255
|
+
fs.chmodSync(this.cachePath(scope), 0o600);
|
|
256
|
+
logger.info(`[OwnerDirectoryCache] Snapshot saved to ${this.cachePath(scope)}`);
|
|
257
|
+
await this.updateFileState(scope, {
|
|
258
|
+
lastAttemptAt: attemptedAt,
|
|
259
|
+
lastSuccessAt: snapshot.fetchedAt,
|
|
260
|
+
snapshotId: snapshot.snapshotId,
|
|
261
|
+
status: 'ready',
|
|
262
|
+
error: undefined,
|
|
263
|
+
});
|
|
264
|
+
return snapshot;
|
|
265
|
+
}
|
|
266
|
+
catch (error) {
|
|
267
|
+
logger.error(`[OwnerDirectoryCache] Sync failed for ${scope}: ${errorText(error)}, code: ${errorCode(error)}`);
|
|
268
|
+
if (isPermissionDenied(error)) {
|
|
269
|
+
logger.warn(`[OwnerDirectoryCache] Permission denied for ${scope}, blocking scope`);
|
|
270
|
+
await this.blockScope(scope);
|
|
271
|
+
}
|
|
272
|
+
else {
|
|
273
|
+
await this.updateFileState(scope, {
|
|
274
|
+
lastAttemptAt: attemptedAt,
|
|
275
|
+
status: 'error',
|
|
276
|
+
error: errorText(error),
|
|
277
|
+
});
|
|
278
|
+
}
|
|
279
|
+
this.options.onError?.(scope, error);
|
|
280
|
+
throw error;
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
async fetchCompleteSnapshot(scope) {
|
|
284
|
+
const base = `data/directories/v1/${scope}`;
|
|
285
|
+
let lastError;
|
|
286
|
+
for (let restart = 0; restart < 2; restart++) {
|
|
287
|
+
const manifestObject = await this.readRemoteJson(`${base}/manifest.json`);
|
|
288
|
+
const manifest = parseManifest(manifestObject.value, this.options.ownerAid, scope);
|
|
289
|
+
try {
|
|
290
|
+
const entries = [];
|
|
291
|
+
const seenAids = new Set();
|
|
292
|
+
const visitedIndexes = new Set();
|
|
293
|
+
let indexRef = manifest.index_ref;
|
|
294
|
+
let indexSegments = 0;
|
|
295
|
+
let pageCount = 0;
|
|
296
|
+
while (indexRef) {
|
|
297
|
+
if (++indexSegments > MAX_INDEX_SEGMENTS || visitedIndexes.has(indexRef.object_key)) {
|
|
298
|
+
throw snapshotChanged('index chain loops or exceeds the page limit');
|
|
299
|
+
}
|
|
300
|
+
visitedIndexes.add(indexRef.object_key);
|
|
301
|
+
assertSnapshotPath(indexRef.object_key, scope, manifest.snapshot_id);
|
|
302
|
+
const indexObject = await this.readRemoteJson(indexRef.object_key, indexRef.sha256);
|
|
303
|
+
const index = parseIndex(indexObject.value, this.options.ownerAid, scope, manifest.snapshot_id);
|
|
304
|
+
if (pageCount + index.pages.length > MAX_SNAPSHOT_OBJECTS) {
|
|
305
|
+
throw snapshotChanged('snapshot exceeds the local object limit');
|
|
306
|
+
}
|
|
307
|
+
pageCount += index.pages.length;
|
|
308
|
+
for (const pageRef of index.pages) {
|
|
309
|
+
assertSnapshotPath(pageRef.object_key, scope, manifest.snapshot_id);
|
|
310
|
+
const pageObject = await this.readRemoteJson(pageRef.object_key, pageRef.sha256);
|
|
311
|
+
const pageEntries = parsePage(pageObject.value, this.options.ownerAid, scope, manifest.snapshot_id);
|
|
312
|
+
if (pageEntries.length !== pageRef.count)
|
|
313
|
+
throw snapshotChanged('page entry count does not match its index');
|
|
314
|
+
for (const rawEntry of pageEntries) {
|
|
315
|
+
const entry = normalizeEntry(scope, rawEntry);
|
|
316
|
+
if (seenAids.has(entry.aid))
|
|
317
|
+
throw snapshotChanged('duplicate AID in Owner directory snapshot');
|
|
318
|
+
seenAids.add(entry.aid);
|
|
319
|
+
entries.push(entry);
|
|
320
|
+
if (entries.length > MAX_DIRECTORY_ENTRIES)
|
|
321
|
+
throw snapshotChanged('directory exceeds the local entry limit');
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
indexRef = index.next_ref;
|
|
325
|
+
}
|
|
326
|
+
if (entries.length !== manifest.total)
|
|
327
|
+
throw snapshotChanged('manifest total does not match downloaded pages');
|
|
328
|
+
return {
|
|
329
|
+
ownerAid: this.options.ownerAid,
|
|
330
|
+
granteeAid: this.options.selfAid,
|
|
331
|
+
scope,
|
|
332
|
+
snapshotId: manifest.snapshot_id,
|
|
333
|
+
total: entries.length,
|
|
334
|
+
fetchedAt: this.now(),
|
|
335
|
+
...(manifest.synced_at_ms !== undefined ? { syncedAtMs: manifest.synced_at_ms } : {}),
|
|
336
|
+
...(manifest.source_observed_at_ms !== undefined ? { sourceObservedAtMs: manifest.source_observed_at_ms } : {}),
|
|
337
|
+
entries,
|
|
338
|
+
};
|
|
339
|
+
}
|
|
340
|
+
catch (error) {
|
|
341
|
+
lastError = error;
|
|
342
|
+
if (restart === 0 && isSnapshotRestartable(error))
|
|
343
|
+
continue;
|
|
344
|
+
throw error;
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
throw lastError ?? snapshotChanged('snapshot could not be read');
|
|
348
|
+
}
|
|
349
|
+
async readRemoteJson(objectKey, expectedSha256) {
|
|
350
|
+
if (!this.options.client)
|
|
351
|
+
throw new OwnerDirectoryCacheError('CLIENT_UNAVAILABLE', 'Owner directory synchronization requires a connected AUN client');
|
|
352
|
+
assertCanonicalObjectKey(objectKey);
|
|
353
|
+
const { bytes, metadata } = await readAuthoritativePersonalFsObject(this.options.client, `/${objectKey}`, this.options.ownerAid, OWNER_DIRECTORY_MAX_OBJECT_BYTES);
|
|
354
|
+
const sha256 = typeof metadata.sha256 === 'string' ? metadata.sha256.toLowerCase() : '';
|
|
355
|
+
if (!SHA256_RE.test(sha256) || (expectedSha256 && sha256 !== expectedSha256)) {
|
|
356
|
+
throw snapshotChanged(`object digest mismatch: ${objectKey}`);
|
|
357
|
+
}
|
|
358
|
+
let parsed;
|
|
359
|
+
try {
|
|
360
|
+
let text = new TextDecoder('utf-8', { fatal: true }).decode(bytes);
|
|
361
|
+
if (text.charCodeAt(0) === 0xfeff)
|
|
362
|
+
text = text.slice(1);
|
|
363
|
+
parsed = JSON.parse(text);
|
|
364
|
+
}
|
|
365
|
+
catch {
|
|
366
|
+
throw snapshotChanged(`object is not valid UTF-8 JSON: ${objectKey}`);
|
|
367
|
+
}
|
|
368
|
+
if (!isRecord(parsed))
|
|
369
|
+
throw snapshotChanged(`object is not a JSON object: ${objectKey}`);
|
|
370
|
+
return { value: parsed, sha256 };
|
|
371
|
+
}
|
|
372
|
+
readCachedSnapshot(scope) {
|
|
373
|
+
const grant = this.loadGrant();
|
|
374
|
+
if (!grant || !this.isAuthorized(grant, scope))
|
|
375
|
+
return undefined;
|
|
376
|
+
const state = this.readSyncState();
|
|
377
|
+
if (state.blockedScopes.includes(scope))
|
|
378
|
+
return undefined;
|
|
379
|
+
const raw = atomicReadJson(this.cachePath(scope));
|
|
380
|
+
if (!isRecord(raw) || raw.schemaVersion !== 1 || raw.ownerAid !== this.options.ownerAid
|
|
381
|
+
|| raw.granteeAid !== this.options.selfAid || raw.scope !== scope
|
|
382
|
+
|| typeof raw.snapshotId !== 'string' || !SNAPSHOT_ID_RE.test(raw.snapshotId)
|
|
383
|
+
|| !Number.isSafeInteger(raw.fetchedAt) || !Number.isSafeInteger(raw.total)
|
|
384
|
+
|| !Array.isArray(raw.entries) || raw.entries.length !== raw.total)
|
|
385
|
+
return undefined;
|
|
386
|
+
try {
|
|
387
|
+
const entries = raw.entries.map(entry => normalizeEntry(scope, entry));
|
|
388
|
+
const aids = new Set(entries.map(entry => entry.aid));
|
|
389
|
+
if (aids.size !== entries.length || entries.length > MAX_DIRECTORY_ENTRIES)
|
|
390
|
+
return undefined;
|
|
391
|
+
return {
|
|
392
|
+
ownerAid: this.options.ownerAid,
|
|
393
|
+
granteeAid: this.options.selfAid,
|
|
394
|
+
scope,
|
|
395
|
+
snapshotId: raw.snapshotId,
|
|
396
|
+
total: entries.length,
|
|
397
|
+
fetchedAt: raw.fetchedAt,
|
|
398
|
+
...(Number.isSafeInteger(raw.syncedAtMs) ? { syncedAtMs: raw.syncedAtMs } : {}),
|
|
399
|
+
...(Number.isSafeInteger(raw.sourceObservedAtMs) ? { sourceObservedAtMs: raw.sourceObservedAtMs } : {}),
|
|
400
|
+
entries,
|
|
401
|
+
};
|
|
402
|
+
}
|
|
403
|
+
catch {
|
|
404
|
+
return undefined;
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
isAuthorized(grant, scope) {
|
|
408
|
+
if (!grant.activeScopes.includes(scope))
|
|
409
|
+
return false;
|
|
410
|
+
const expiresAt = grant.expiresAtByScope[scope];
|
|
411
|
+
return typeof expiresAt === 'number' && (expiresAt === 0 || expiresAt > this.now());
|
|
412
|
+
}
|
|
413
|
+
isStale(scope) {
|
|
414
|
+
const state = this.readSyncState();
|
|
415
|
+
const lastSuccessAt = state.files[scope]?.lastSuccessAt;
|
|
416
|
+
return typeof lastSuccessAt !== 'number' || this.now() - lastSuccessAt >= this.staleAfterMs;
|
|
417
|
+
}
|
|
418
|
+
startAutomaticSync(scope) {
|
|
419
|
+
const state = this.readSyncState();
|
|
420
|
+
const lastAttemptAt = state.files[scope]?.lastAttemptAt;
|
|
421
|
+
if (typeof lastAttemptAt === 'number' && this.now() - lastAttemptAt < this.retryBackoffMs)
|
|
422
|
+
return;
|
|
423
|
+
void this.sync(scope).catch(() => { });
|
|
424
|
+
}
|
|
425
|
+
async blockScope(scope) {
|
|
426
|
+
fs.rmSync(this.cachePath(scope), { force: true });
|
|
427
|
+
await this.updateSyncState(state => ({
|
|
428
|
+
...state,
|
|
429
|
+
blockedScopes: [...new Set([...state.blockedScopes, scope])],
|
|
430
|
+
files: { ...state.files, [scope]: { ...state.files[scope], status: 'blocked', error: 'remote storage denied access' } },
|
|
431
|
+
}));
|
|
432
|
+
}
|
|
433
|
+
async updateFileState(scope, update) {
|
|
434
|
+
await this.updateSyncState(state => ({
|
|
435
|
+
...state,
|
|
436
|
+
files: { ...state.files, [scope]: { ...state.files[scope], ...update } },
|
|
437
|
+
}));
|
|
438
|
+
}
|
|
439
|
+
async updateSyncState(transform) {
|
|
440
|
+
const run = this.stateWriteTail.then(() => {
|
|
441
|
+
const state = transform(this.readSyncState());
|
|
442
|
+
fs.mkdirSync(this.options.directory, { recursive: true, mode: 0o700 });
|
|
443
|
+
atomicWriteJson(this.syncStatePath(), state);
|
|
444
|
+
fs.chmodSync(this.syncStatePath(), 0o600);
|
|
445
|
+
});
|
|
446
|
+
this.stateWriteTail = run.catch(() => { });
|
|
447
|
+
await run;
|
|
448
|
+
}
|
|
449
|
+
readSyncState() {
|
|
450
|
+
const raw = atomicReadJson(this.syncStatePath());
|
|
451
|
+
if (!isRecord(raw) || raw.schemaVersion !== 1 || raw.ownerAid !== this.options.ownerAid
|
|
452
|
+
|| raw.granteeAid !== this.options.selfAid || !isRecord(raw.files)) {
|
|
453
|
+
return { schemaVersion: 1, ownerAid: this.options.ownerAid, granteeAid: this.options.selfAid, files: {}, blockedScopes: [] };
|
|
454
|
+
}
|
|
455
|
+
return {
|
|
456
|
+
schemaVersion: 1,
|
|
457
|
+
ownerAid: this.options.ownerAid,
|
|
458
|
+
granteeAid: this.options.selfAid,
|
|
459
|
+
files: raw.files,
|
|
460
|
+
blockedScopes: Array.isArray(raw.blockedScopes) ? raw.blockedScopes.filter(isScope) : [],
|
|
461
|
+
};
|
|
462
|
+
}
|
|
463
|
+
loadGrant() {
|
|
464
|
+
const raw = atomicReadJson(this.authorizationPath());
|
|
465
|
+
if (!isRecord(raw) || raw.schemaVersion !== 1 || raw.ownerAid !== this.options.ownerAid
|
|
466
|
+
|| raw.granteeAid !== this.options.selfAid || typeof raw.status !== 'string'
|
|
467
|
+
|| !GRANT_STATUSES.has(raw.status) || !Array.isArray(raw.activeScopes)
|
|
468
|
+
|| raw.activeScopes.some(scope => !isScope(scope)) || !isRecord(raw.expiresAtByScope))
|
|
469
|
+
return undefined;
|
|
470
|
+
const activeScopes = raw.activeScopes;
|
|
471
|
+
const knownScopes = [...new Set([
|
|
472
|
+
...activeScopes,
|
|
473
|
+
...(Array.isArray(raw.knownScopes) ? raw.knownScopes.filter(isScope) : []),
|
|
474
|
+
...OWNER_DIRECTORY_SCOPES.filter(scope => fs.existsSync(this.cachePath(scope)) || fs.existsSync(`${this.cachePath(scope)}_`)),
|
|
475
|
+
])];
|
|
476
|
+
const expiresAtByScope = {};
|
|
477
|
+
for (const scope of activeScopes) {
|
|
478
|
+
const expiresAt = raw.expiresAtByScope[scope];
|
|
479
|
+
if (!Number.isSafeInteger(expiresAt) || expiresAt < 0)
|
|
480
|
+
return undefined;
|
|
481
|
+
expiresAtByScope[scope] = expiresAt;
|
|
482
|
+
}
|
|
483
|
+
return {
|
|
484
|
+
schemaVersion: 1,
|
|
485
|
+
status: raw.status,
|
|
486
|
+
ownerAid: this.options.ownerAid,
|
|
487
|
+
granteeAid: this.options.selfAid,
|
|
488
|
+
activeScopes,
|
|
489
|
+
knownScopes,
|
|
490
|
+
expiresAtByScope,
|
|
491
|
+
manifestRefs: isRecord(raw.manifestRefs) ? raw.manifestRefs : {},
|
|
492
|
+
storedAt: Number.isSafeInteger(raw.storedAt) ? raw.storedAt : 0,
|
|
493
|
+
};
|
|
494
|
+
}
|
|
495
|
+
authorizationPath() { return path.join(this.options.directory, 'authorization.json'); }
|
|
496
|
+
syncStatePath() { return path.join(this.options.directory, 'sync-state.json'); }
|
|
497
|
+
cachePath(scope) { return path.join(this.options.directory, `${scope}.json`); }
|
|
498
|
+
}
|
|
499
|
+
function parseManifest(value, ownerAid, scope) {
|
|
500
|
+
if (value.schema_version !== 1 || value.owner_aid !== ownerAid || value.dataset !== scope
|
|
501
|
+
|| value.complete !== true || typeof value.snapshot_id !== 'string' || !SNAPSHOT_ID_RE.test(value.snapshot_id)
|
|
502
|
+
|| !Number.isSafeInteger(value.total) || value.total < 0 || value.total > MAX_DIRECTORY_ENTRIES) {
|
|
503
|
+
throw snapshotChanged('manifest identity, schema, completeness or total is invalid');
|
|
504
|
+
}
|
|
505
|
+
const rawIndexRef = typeof value.index_ref === 'string'
|
|
506
|
+
? { object_key: value.index_ref, sha256: value.index_sha256 }
|
|
507
|
+
: value.index_ref;
|
|
508
|
+
const indexRef = parseRef(rawIndexRef);
|
|
509
|
+
assertSnapshotPath(indexRef.object_key, scope, value.snapshot_id);
|
|
510
|
+
return {
|
|
511
|
+
schema_version: 1,
|
|
512
|
+
owner_aid: ownerAid,
|
|
513
|
+
dataset: scope,
|
|
514
|
+
complete: true,
|
|
515
|
+
snapshot_id: value.snapshot_id,
|
|
516
|
+
total: value.total,
|
|
517
|
+
index_ref: indexRef,
|
|
518
|
+
...(optionalTimestamp(value.synced_at_ms) !== undefined ? { synced_at_ms: optionalTimestamp(value.synced_at_ms) } : {}),
|
|
519
|
+
...(optionalTimestamp(value.source_observed_at_ms) !== undefined ? { source_observed_at_ms: optionalTimestamp(value.source_observed_at_ms) } : {}),
|
|
520
|
+
};
|
|
521
|
+
}
|
|
522
|
+
function parseIndex(value, ownerAid, scope, snapshotId) {
|
|
523
|
+
if (value.schema_version !== 1 || value.owner_aid !== ownerAid || value.dataset !== scope || value.snapshot_id !== snapshotId
|
|
524
|
+
|| !Array.isArray(value.pages) || value.pages.length > MAX_PAGES_PER_INDEX) {
|
|
525
|
+
throw snapshotChanged('index identity, schema or page count is invalid');
|
|
526
|
+
}
|
|
527
|
+
const pages = value.pages.map(item => {
|
|
528
|
+
if (!isRecord(item) || !Number.isSafeInteger(item.count) || item.count < 0) {
|
|
529
|
+
throw snapshotChanged('index page count is invalid');
|
|
530
|
+
}
|
|
531
|
+
return { ...parseRef(item), count: item.count };
|
|
532
|
+
});
|
|
533
|
+
const nextRef = value.next_ref === undefined || value.next_ref === null ? undefined : parseRef(value.next_ref);
|
|
534
|
+
if (pages.length === 0 && nextRef)
|
|
535
|
+
throw snapshotChanged('empty index segment cannot point to another segment');
|
|
536
|
+
return { pages, ...(nextRef ? { next_ref: nextRef } : {}) };
|
|
537
|
+
}
|
|
538
|
+
function parsePage(value, ownerAid, scope, snapshotId) {
|
|
539
|
+
if (value.schema_version !== 1 || value.owner_aid !== ownerAid || value.dataset !== scope
|
|
540
|
+
|| value.snapshot_id !== snapshotId || !Array.isArray(value.entries)) {
|
|
541
|
+
throw snapshotChanged('page identity, schema or entries are invalid');
|
|
542
|
+
}
|
|
543
|
+
return value.entries;
|
|
544
|
+
}
|
|
545
|
+
function parseRef(value) {
|
|
546
|
+
if (!isRecord(value) || typeof value.object_key !== 'string' || typeof value.sha256 !== 'string'
|
|
547
|
+
|| !SHA256_RE.test(value.sha256))
|
|
548
|
+
throw snapshotChanged('snapshot object reference is invalid');
|
|
549
|
+
assertCanonicalObjectKey(value.object_key);
|
|
550
|
+
return { object_key: value.object_key, sha256: value.sha256 };
|
|
551
|
+
}
|
|
552
|
+
function normalizeEntry(scope, value) {
|
|
553
|
+
if (!isRecord(value) || typeof value.aid !== 'string' || !isValidAid(value.aid)
|
|
554
|
+
|| typeof value.name !== 'string' || value.name.length > 256) {
|
|
555
|
+
throw snapshotChanged(`${scope} entry identity or name is invalid`);
|
|
556
|
+
}
|
|
557
|
+
if (scope === 'contacts') {
|
|
558
|
+
if (value.remark !== undefined && (typeof value.remark !== 'string' || value.remark.length > 512)) {
|
|
559
|
+
throw snapshotChanged('contacts entry remark is invalid');
|
|
560
|
+
}
|
|
561
|
+
return { aid: value.aid, name: value.name, ...(typeof value.remark === 'string' ? { remark: value.remark } : {}) };
|
|
562
|
+
}
|
|
563
|
+
for (const key of ['type', 'description', 'ownership_status']) {
|
|
564
|
+
const max = key === 'description' ? 4096 : 128;
|
|
565
|
+
if (value[key] !== undefined && (typeof value[key] !== 'string' || value[key].length > max)) {
|
|
566
|
+
throw snapshotChanged(`agents entry ${key} is invalid`);
|
|
567
|
+
}
|
|
568
|
+
}
|
|
569
|
+
if (value.ownership_checked_at_ms !== undefined
|
|
570
|
+
&& (!Number.isSafeInteger(value.ownership_checked_at_ms) || value.ownership_checked_at_ms < 0)) {
|
|
571
|
+
throw snapshotChanged('agents entry ownership_checked_at_ms is invalid');
|
|
572
|
+
}
|
|
573
|
+
return {
|
|
574
|
+
aid: value.aid,
|
|
575
|
+
name: value.name,
|
|
576
|
+
...(typeof value.type === 'string' ? { type: value.type } : {}),
|
|
577
|
+
...(typeof value.description === 'string' ? { description: value.description } : {}),
|
|
578
|
+
...(typeof value.ownership_status === 'string' ? { ownership_status: value.ownership_status } : {}),
|
|
579
|
+
...(typeof value.ownership_checked_at_ms === 'number' ? { ownership_checked_at_ms: value.ownership_checked_at_ms } : {}),
|
|
580
|
+
};
|
|
581
|
+
}
|
|
582
|
+
function assertSnapshotPath(key, scope, snapshotId) {
|
|
583
|
+
const prefix = `data/directories/v1/${scope}/snapshots/${snapshotId}/`;
|
|
584
|
+
if (!key.startsWith(prefix) || key.slice(prefix.length).length === 0) {
|
|
585
|
+
throw snapshotChanged('snapshot reference escapes its Owner/scope/snapshot directory');
|
|
586
|
+
}
|
|
587
|
+
}
|
|
588
|
+
function assertCanonicalObjectKey(key) {
|
|
589
|
+
if (!key || key.startsWith('/') || key.endsWith('/') || key.includes('\\') || /[\u0000-\u001f\u007f]/.test(key)
|
|
590
|
+
|| key.split('/').some(part => !part || part === '.' || part === '..')) {
|
|
591
|
+
throw snapshotChanged('snapshot object key is not canonical');
|
|
592
|
+
}
|
|
593
|
+
}
|
|
594
|
+
function optionalTimestamp(value) {
|
|
595
|
+
return Number.isSafeInteger(value) && value >= 0 ? value : undefined;
|
|
596
|
+
}
|
|
597
|
+
function isSnapshotRestartable(error) {
|
|
598
|
+
if (error instanceof OwnerDirectoryCacheError)
|
|
599
|
+
return error.code === 'SNAPSHOT_CHANGED';
|
|
600
|
+
const code = errorCode(error).toUpperCase();
|
|
601
|
+
return code === 'FS_NOT_FOUND' || code === 'NOT_FOUND' || code === 'ENOENT'
|
|
602
|
+
|| code === '-32008' || code === '404' || code === '4040' || code === 'SNAPSHOT_CHANGED'
|
|
603
|
+
|| (code === '-32602' && /not found|no such|不存在/i.test(errorText(error)));
|
|
604
|
+
}
|
|
605
|
+
function isPermissionDenied(error) {
|
|
606
|
+
const code = errorCode(error).toUpperCase();
|
|
607
|
+
return code === 'PERMISSION_DENIED' || code === 'STORAGE_ACCESS_DENIED' || code === 'ACCESS_DENIED'
|
|
608
|
+
|| code === 'EACCES' || code === '-32004' || code === '403' || code === '4030';
|
|
609
|
+
}
|
|
610
|
+
function errorCode(error) {
|
|
611
|
+
return error && typeof error === 'object' && 'code' in error ? String(error.code) : '';
|
|
612
|
+
}
|
|
613
|
+
function errorText(error) {
|
|
614
|
+
const text = error instanceof Error ? error.message : String(error);
|
|
615
|
+
return text.slice(0, 500);
|
|
616
|
+
}
|
|
617
|
+
function snapshotChanged(message) {
|
|
618
|
+
return new OwnerDirectoryCacheError('SNAPSHOT_CHANGED', message);
|
|
619
|
+
}
|
|
620
|
+
function isScope(value) {
|
|
621
|
+
return value === 'contacts' || value === 'agents';
|
|
622
|
+
}
|
|
623
|
+
function isRecord(value) {
|
|
624
|
+
return value !== null && typeof value === 'object' && !Array.isArray(value);
|
|
625
|
+
}
|
|
626
|
+
async function waitAtMost(promise, ms) {
|
|
627
|
+
let timer;
|
|
628
|
+
try {
|
|
629
|
+
await Promise.race([promise, new Promise(resolve => { timer = setTimeout(resolve, ms); })]);
|
|
630
|
+
}
|
|
631
|
+
finally {
|
|
632
|
+
if (timer)
|
|
633
|
+
clearTimeout(timer);
|
|
634
|
+
}
|
|
635
|
+
}
|
package/dist/eck/kit-renderer.js
CHANGED
|
@@ -282,8 +282,8 @@ const PARAM_DESCRIPTIONS = {
|
|
|
282
282
|
evolcoreMode: 'evolcore 运行模式(dev=源码仓库可直接修改 | install=全局安装包只读)',
|
|
283
283
|
baseAgent: 'base agent 规范值(claude/codex/gemini/ecagent)',
|
|
284
284
|
baseAgentName: 'base agent 显示名',
|
|
285
|
-
baseAgentModel: 'base agent
|
|
286
|
-
effectiveModel: '
|
|
285
|
+
baseAgentModel: 'base agent 引擎实例持有的模型(参考值;实际调用以 effectiveModel 为准)',
|
|
286
|
+
effectiveModel: '当前实际生效模型(Agent 级配置经角色约束后的结果)',
|
|
287
287
|
modelFallbackActive: 'evolcore 配置的模型不可用,当前正在使用降级模型',
|
|
288
288
|
modelFallbackModel: '当前降级使用的 base agent 模型名',
|
|
289
289
|
agentSessionId: 'base agent 会话 ID',
|