evolcore 0.0.43-test.6 → 0.0.43-test.8
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 +188 -58
- package/dist/agent-template/jobs.js +62 -0
- package/dist/agent-template/runtime.js +214 -0
- package/dist/agent-template/service.js +465 -0
- package/dist/agent-template/tool-runner.js +45 -0
- package/dist/agent-template/types.js +5 -0
- package/dist/agents/baseagent-availability.js +1 -1
- package/dist/agents/claude-runner.js +1 -1
- package/dist/agents/codex/app-server-client.js +22 -0
- package/dist/agents/codex-runner.js +69 -2
- package/dist/aun/aid/agentmd.js +56 -1
- package/dist/aun/aid/client.js +3 -1
- package/dist/aun/menu/client.js +3 -2
- package/dist/aun/msg/p2p.js +6 -1
- package/dist/channels/aun.js +277 -49
- package/dist/cli/agent-command.js +5 -0
- package/dist/cli/agent-create.js +7 -3
- package/dist/cli/agent-template.js +124 -0
- package/dist/cli/aun-commands.js +2 -2
- package/dist/cli/ctl-command.js +2 -1
- package/dist/cli/daemon-commands.js +11 -66
- package/dist/cli/daemon-lifecycle-client.js +61 -0
- package/dist/cli/ecmsg-gateway.js +35 -2
- package/dist/cli/events-command.js +109 -0
- package/dist/cli/index.js +60 -14
- package/dist/cli/invoke-command.js +182 -0
- package/dist/cli/managed-command-guard.js +6 -1
- package/dist/cli/net-check.js +1 -1
- package/dist/cli/restart-monitor.js +160 -83
- package/dist/cli/services-command.js +137 -0
- package/dist/cli/team-workspace-command.js +134 -0
- package/dist/cli/thread-command.js +103 -13
- package/dist/cli/watch-msg.js +1 -1
- package/dist/config/boot-log.js +8 -8
- package/dist/config/config-field-policy.js +2 -2
- package/dist/config/config-manager.js +0 -7
- package/dist/config/invoke-ipc-service.js +118 -0
- package/dist/config/schema-migration.js +45 -15
- package/dist/config-store.js +2 -1
- package/dist/core/agent-ipc-registration.js +25 -4
- package/dist/core/agent-reload-coordinator.js +6 -4
- package/dist/core/agent-runtime.js +106 -10
- package/dist/core/auth/delegation-authorization.js +66 -0
- package/dist/core/auth/operation-authorizer.js +2 -0
- package/dist/core/auth/operation-catalog.js +29 -0
- package/dist/core/baseagent-loader.js +60 -130
- package/dist/core/channel-loader.js +10 -1
- package/dist/core/channel-runtime.js +20 -12
- package/dist/core/command/agent-control.js +12 -2
- package/dist/core/command/agent-storage-menu.js +1 -1
- package/dist/core/command/agent-template-menu.js +147 -0
- package/dist/core/command/chatmode-config.js +2 -36
- package/dist/core/command/command-handler.js +80 -17
- package/dist/core/command/data-root-resolver.js +32 -23
- package/dist/core/command/eventcenter-menu.js +468 -0
- package/dist/core/command/menu-catalog.js +15 -0
- package/dist/core/command/menu-handler.js +216 -90
- package/dist/core/command/menu-protocol.js +71 -13
- package/dist/core/command/menu-query-batch.js +194 -0
- 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 +22 -5
- package/dist/core/daemon-lifecycle.js +323 -191
- package/dist/core/handoff/runtime.js +45 -13
- 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 +50 -7
- package/dist/core/legacy-rename.js +41 -0
- package/dist/core/memory/consolidation.js +7 -5
- package/dist/core/message/aun-ipc-sender.js +252 -17
- package/dist/core/message/im-renderer.js +116 -85
- package/dist/core/message/message-bridge.js +4 -0
- package/dist/core/message/message-queue.js +66 -15
- 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 +29 -5
- package/dist/core/message/response-engine.js +194 -61
- package/dist/core/message/send-options.js +7 -4
- package/dist/core/message/send-routing.js +50 -5
- package/dist/core/ownership/owner-resolver.js +7 -0
- package/dist/core/pack-auth.js +94 -0
- package/dist/core/pack-install.js +194 -0
- package/dist/core/permission/ec-command-parser.js +51 -17
- package/dist/core/permission/protected-paths.js +12 -2
- package/dist/core/permission/shell-environment.js +9 -0
- package/dist/core/rate-window.js +48 -0
- package/dist/core/relation/peer-identity.js +28 -0
- package/dist/core/session/session-file-health.js +243 -0
- package/dist/core/session/session-fs-store.js +67 -28
- package/dist/core/session/session-idle-monitor.js +59 -19
- package/dist/core/session/session-manager.js +13 -4
- 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/core/thread/thread-service.js +52 -3
- package/dist/directory/owner-directory-admission.js +40 -53
- package/dist/directory/owner-directory-cache.js +89 -113
- package/dist/directory/owner-directory-query-operation.js +8 -10
- package/dist/eck/kit-renderer.js +7 -5
- package/dist/eck/message-renderer.js +2 -0
- package/dist/ecweb/basic-settings-service.js +1 -1
- package/dist/ecweb/message-context-service.js +0 -4
- package/dist/ecweb/runtime/lifecycle.js +6 -2
- package/dist/ecweb/runtime/supervisor.js +19 -4
- package/dist/event-center/admission-materializer.js +124 -0
- package/dist/event-center/bus-gate.js +154 -0
- package/dist/event-center/bus-host.js +307 -0
- package/dist/event-center/bus-resolver.js +120 -0
- package/dist/event-center/center-config.js +127 -0
- package/dist/event-center/daemon.js +44 -0
- package/dist/event-center/directory-source.js +55 -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 +73 -0
- package/dist/event-center/ipc-operation.js +423 -0
- package/dist/event-center/ledger.js +52 -0
- package/dist/event-center/pack-loader.js +207 -0
- package/dist/event-center/pack-manager.js +85 -0
- package/dist/event-center/registry.js +463 -0
- package/dist/event-center/runtime.js +837 -0
- package/dist/event-center/script-runner.js +356 -0
- package/dist/event-center/types.js +51 -0
- package/dist/event-center/validation.js +435 -0
- package/dist/index.js +424 -135
- package/dist/ipc.js +106 -12
- package/dist/paths.js +20 -0
- package/dist/product.js +14 -0
- package/dist/proxy/manager.js +5 -1
- package/dist/proxy/service-supervisor.js +13 -4
- package/dist/response-system/engines/v1/index.js +0 -1
- package/dist/response-system/engines/v1/proactive-flow.js +4 -2
- 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 +550 -0
- package/dist/services/ledger.js +52 -0
- package/dist/services/message-log.js +112 -0
- package/dist/services/pack-manager.js +215 -0
- package/dist/services/reply.js +55 -0
- package/dist/services/router.js +98 -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 +18 -0
- package/dist/trigger/event-source.js +1 -1
- package/dist/trigger/ipc-service.js +3 -1
- package/dist/trigger/scheduler.js +38 -0
- package/dist/trigger/script-sandbox.js +170 -24
- package/dist/utils/cross-platform.js +24 -10
- package/dist/utils/evolcore-version.js +69 -27
- package/dist/utils/instance-registry.js +19 -13
- package/dist/utils/npm-ops.js +2 -1
- package/dist/utils/package-version-info.js +6 -3
- package/dist/utils/process-introspect.js +45 -9
- package/dist/utils/startup-trace.js +74 -37
- package/dist/utils/windows-shell-trust.js +80 -6
- package/kits/docs/INDEX.md +2 -2
- package/kits/docs/aun/ABILITY_CALLING.md +9 -15
- package/kits/docs/context-assembly.md +1 -1
- package/kits/docs/evolcore/INDEX.md +3 -1
- package/kits/docs/evolcore/ability.md +4 -0
- package/kits/docs/evolcore/agent.md +22 -1
- package/kits/docs/evolcore/aid.md +1 -1
- package/kits/docs/evolcore/event-center.md +227 -0
- package/kits/docs/evolcore/file-navigation.md +10 -264
- package/kits/docs/evolcore/fs.md +5 -3
- package/kits/docs/evolcore/group.md +1 -1
- package/kits/docs/evolcore/memory-architecture.md +2 -0
- package/kits/docs/evolcore/menu-protocol.md +28 -1
- package/kits/docs/evolcore/msg.md +21 -9
- package/kits/docs/evolcore/provider.md +3 -1
- package/kits/docs/evolcore/response.md +5 -5
- package/kits/docs/evolcore/services.md +294 -0
- package/kits/docs/evolcore/task.md +2 -0
- package/kits/docs/evolcore/team-workspace.md +41 -0
- package/kits/docs/evolcore/thread-chain.md +15 -3
- package/kits/eck_manifest.json +26 -0
- package/kits/rules/02-navigation.md +10 -1
- package/kits/rules/06-channel.md +1 -0
- 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/relation-config.schema.12.json +2 -3
- package/kits/schemas/role-config.schema.2.json +4 -4
- 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 +5 -8
- 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/message-fragments/item.md +1 -1
- package/kits/templates/system-fragments/channel.md +3 -3
- package/kits/templates/system-fragments/commands.md +5 -3
- package/kits/templates/system-fragments/reply.md +1 -5
- package/kits/templates/system-fragments/thread.md +13 -10
- package/package.json +5 -6
- package/scripts/agent-template/index.mjs +40 -0
- package/scripts/agent-template/inspect.mjs +240 -0
- package/scripts/agent-template/overlay.mjs +71 -0
- package/scripts/agent-template/pack.mjs +169 -0
- package/scripts/agent-template/shared/common.mjs +235 -0
- package/scripts/agent-template/shared/evolcore.mjs +75 -0
- package/scripts/agent-template/shared/plan.mjs +129 -0
- package/scripts/agent-template/shared/team-schema.json +70 -0
- package/scripts/agent-template/shared/template-contract.mjs +154 -0
- package/scripts/agent-template/shared/template-package.mjs +265 -0
- package/scripts/agent-template/shared/template-runtime.mjs +80 -0
- package/scripts/agent-template/shared/template.mjs +142 -0
- 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,612 @@
|
|
|
1
|
+
import crypto from 'node:crypto';
|
|
2
|
+
import fs from 'node:fs';
|
|
3
|
+
import path from 'node:path';
|
|
4
|
+
import { agentDataDir, agentOutputDir } from '../../paths.js';
|
|
5
|
+
import { isValidAid } from '../../aun/aid/validation.js';
|
|
6
|
+
import { atomicReplaceFile } from '../../utils/atomic-write.js';
|
|
7
|
+
export class DataRootError extends Error {
|
|
8
|
+
code;
|
|
9
|
+
data;
|
|
10
|
+
constructor(code, message, data) {
|
|
11
|
+
super(message);
|
|
12
|
+
this.name = 'DataRootError';
|
|
13
|
+
this.code = code;
|
|
14
|
+
this.data = data;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
/** Team Workspace is the active binding; startup may explicitly import PA registration once. */
|
|
18
|
+
const STATE_FILE = 'team-workspace-state.json';
|
|
19
|
+
const MAX_ENTRIES = 200;
|
|
20
|
+
const MAX_PATH_LENGTH = 2048;
|
|
21
|
+
const lockDelay = new Int32Array(new SharedArrayBuffer(4));
|
|
22
|
+
const DEFAULT_NAVIGATION_ENTRIES = [
|
|
23
|
+
{ id: 'capture', title: '输入资料', path: 'capture', kind: 'directory', order: 10, icon: 'folder' },
|
|
24
|
+
{ id: 'output', title: '团队产物', path: 'output', kind: 'directory', order: 20, icon: 'document' },
|
|
25
|
+
];
|
|
26
|
+
function statePath(agentAid) {
|
|
27
|
+
return path.join(agentDataDir(agentAid), STATE_FILE);
|
|
28
|
+
}
|
|
29
|
+
function fail(code, message, data) {
|
|
30
|
+
throw new DataRootError(code, message, data);
|
|
31
|
+
}
|
|
32
|
+
function canonicalDirectory(value) {
|
|
33
|
+
if (typeof value !== 'string' || !value.trim() || !path.isAbsolute(value)) {
|
|
34
|
+
return fail('INVALID_ARGUMENT', 'workspace rootPath must be an absolute path');
|
|
35
|
+
}
|
|
36
|
+
let real;
|
|
37
|
+
try {
|
|
38
|
+
real = fs.realpathSync(value);
|
|
39
|
+
}
|
|
40
|
+
catch {
|
|
41
|
+
return fail('ROOT_NOT_FOUND', 'Team workspace does not exist');
|
|
42
|
+
}
|
|
43
|
+
try {
|
|
44
|
+
if (!fs.statSync(real).isDirectory())
|
|
45
|
+
return fail('ROOT_NOT_FOUND', 'Team workspace is not a directory');
|
|
46
|
+
}
|
|
47
|
+
catch {
|
|
48
|
+
return fail('ROOT_NOT_FOUND', 'Team workspace does not exist');
|
|
49
|
+
}
|
|
50
|
+
return real;
|
|
51
|
+
}
|
|
52
|
+
function isInside(root, candidate) {
|
|
53
|
+
const relative = path.relative(root, candidate);
|
|
54
|
+
return relative === '' || (!relative.startsWith(`..${path.sep}`) && relative !== '..' && !path.isAbsolute(relative));
|
|
55
|
+
}
|
|
56
|
+
/** Paths which can contain runtime state are never user-facing content. */
|
|
57
|
+
export function isTeamHiddenPath(relativePath) {
|
|
58
|
+
const parts = relativePath.replaceAll('\\', '/').split('/').filter(Boolean);
|
|
59
|
+
if (parts.some(part => part === '.' || part.startsWith('.')))
|
|
60
|
+
return true;
|
|
61
|
+
for (let i = 0; i + 1 < parts.length; i++) {
|
|
62
|
+
if (parts[i].toLowerCase() === 'capture' && parts[i + 1].toLowerCase() === '_manifest')
|
|
63
|
+
return true;
|
|
64
|
+
}
|
|
65
|
+
return false;
|
|
66
|
+
}
|
|
67
|
+
function normalizeRelative(value, allowRoot = false) {
|
|
68
|
+
if (typeof value !== 'string' || !value.trim() || value.length > MAX_PATH_LENGTH) {
|
|
69
|
+
return fail('INVALID_ROOT_PATH', 'A Team workspace path is required');
|
|
70
|
+
}
|
|
71
|
+
const raw = value.trim().replaceAll('\\', '/');
|
|
72
|
+
if (raw.startsWith('/') || path.isAbsolute(raw) || path.win32.isAbsolute(raw)
|
|
73
|
+
|| raw.includes(':') || raw.includes('\0') || raw.split('/').includes('..')) {
|
|
74
|
+
return fail('INVALID_ROOT_PATH', 'Team workspace paths must be relative and cannot contain traversal');
|
|
75
|
+
}
|
|
76
|
+
const normalized = raw.split('/').filter(part => part && part !== '.').join('/');
|
|
77
|
+
if ((!normalized && !allowRoot) || isTeamHiddenPath(normalized))
|
|
78
|
+
return fail('INVALID_ROOT_PATH', 'Team workspace path is not user-facing content');
|
|
79
|
+
return normalized || '.';
|
|
80
|
+
}
|
|
81
|
+
function entryId(relativePath) {
|
|
82
|
+
return `team-${crypto.createHash('sha256').update(relativePath).digest('hex').slice(0, 16)}`;
|
|
83
|
+
}
|
|
84
|
+
function resolveEntryPath(rootPath, outputRootPath, relativePath) {
|
|
85
|
+
if (outputRootPath && (relativePath === 'output' || relativePath.startsWith('output/'))) {
|
|
86
|
+
const suffix = relativePath === 'output' ? '.' : relativePath.slice('output/'.length);
|
|
87
|
+
return path.resolve(outputRootPath, suffix);
|
|
88
|
+
}
|
|
89
|
+
return path.resolve(rootPath, relativePath);
|
|
90
|
+
}
|
|
91
|
+
function validateEntry(entry, rootPath, outputRootPath, index) {
|
|
92
|
+
if (!entry || typeof entry !== 'object' || Array.isArray(entry))
|
|
93
|
+
return fail('INVALID_ARGUMENT', `entries[${index}] must be an object`);
|
|
94
|
+
const value = entry;
|
|
95
|
+
const relativePath = normalizeRelative(value.path);
|
|
96
|
+
const title = typeof value.title === 'string' && value.title.trim() ? value.title.trim() : path.posix.basename(relativePath);
|
|
97
|
+
if (title.length > 200)
|
|
98
|
+
return fail('INVALID_ARGUMENT', `entries[${index}].title is too long`);
|
|
99
|
+
const requestedKind = value.kind;
|
|
100
|
+
if (requestedKind !== 'file' && requestedKind !== 'directory')
|
|
101
|
+
return fail('INVALID_ARGUMENT', `entries[${index}].kind is invalid`);
|
|
102
|
+
if (value.availability !== undefined && value.availability !== 'available' && value.availability !== 'unavailable') {
|
|
103
|
+
return fail('INVALID_ARGUMENT', `entries[${index}].availability is invalid`);
|
|
104
|
+
}
|
|
105
|
+
const unavailable = value.availability === 'unavailable';
|
|
106
|
+
const reason = typeof value.reason === 'string' && value.reason.trim() ? value.reason.trim().slice(0, 500) : undefined;
|
|
107
|
+
if (unavailable && !reason)
|
|
108
|
+
return fail('INVALID_ARGUMENT', `entries[${index}].reason is required for unavailable entries`);
|
|
109
|
+
const order = Number.isSafeInteger(value.order) ? Number(value.order) : index;
|
|
110
|
+
if (order < 0 || order > 1_000_000)
|
|
111
|
+
return fail('INVALID_ARGUMENT', `entries[${index}].order is invalid`);
|
|
112
|
+
const id = typeof value.id === 'string' && value.id.trim() ? value.id.trim() : entryId(relativePath);
|
|
113
|
+
if (id.length > 200)
|
|
114
|
+
return fail('INVALID_ARGUMENT', `entries[${index}].id is too long`);
|
|
115
|
+
const icon = typeof value.icon === 'string' && value.icon.trim() ? value.icon.trim().slice(0, 100) : undefined;
|
|
116
|
+
const tag = typeof value.tag === 'string' && value.tag.trim() ? value.tag.trim().slice(0, 100) : undefined;
|
|
117
|
+
const metadata = value.metadata && typeof value.metadata === 'object' && !Array.isArray(value.metadata)
|
|
118
|
+
? value.metadata : undefined;
|
|
119
|
+
if (value.metadata !== undefined && !metadata)
|
|
120
|
+
return fail('INVALID_ARGUMENT', `entries[${index}].metadata must be an object`);
|
|
121
|
+
const candidate = resolveEntryPath(rootPath, outputRootPath, relativePath);
|
|
122
|
+
const boundary = outputRootPath && (relativePath === 'output' || relativePath.startsWith('output/'))
|
|
123
|
+
? outputRootPath : rootPath;
|
|
124
|
+
let realPath;
|
|
125
|
+
let stat;
|
|
126
|
+
try {
|
|
127
|
+
realPath = fs.realpathSync(candidate);
|
|
128
|
+
stat = fs.statSync(realPath);
|
|
129
|
+
}
|
|
130
|
+
catch {
|
|
131
|
+
if (!unavailable)
|
|
132
|
+
return fail('NOT_FOUND', `Team workspace entry does not exist: ${relativePath}`);
|
|
133
|
+
return {
|
|
134
|
+
id,
|
|
135
|
+
title,
|
|
136
|
+
path: relativePath,
|
|
137
|
+
kind: requestedKind,
|
|
138
|
+
order,
|
|
139
|
+
availability: 'unavailable',
|
|
140
|
+
reason,
|
|
141
|
+
...(icon ? { icon } : {}),
|
|
142
|
+
...(tag ? { tag } : {}),
|
|
143
|
+
...(metadata ? { metadata } : {}),
|
|
144
|
+
};
|
|
145
|
+
}
|
|
146
|
+
if (!isInside(boundary, realPath))
|
|
147
|
+
return fail('INVALID_ROOT_PATH', 'Team workspace entry escapes its registered root');
|
|
148
|
+
const kind = stat.isDirectory() ? 'directory' : stat.isFile() ? 'file' : undefined;
|
|
149
|
+
if (!kind)
|
|
150
|
+
return fail('NOT_SUPPORTED', `Team workspace entry is not a regular file or directory: ${relativePath}`);
|
|
151
|
+
if (requestedKind !== kind)
|
|
152
|
+
return fail('INVALID_ARGUMENT', `entries[${index}].kind does not match the target`);
|
|
153
|
+
const result = { id, title, path: relativePath, kind, order, availability: 'available' };
|
|
154
|
+
if (icon)
|
|
155
|
+
result.icon = icon;
|
|
156
|
+
if (tag)
|
|
157
|
+
result.tag = tag;
|
|
158
|
+
if (metadata)
|
|
159
|
+
result.metadata = metadata;
|
|
160
|
+
return result;
|
|
161
|
+
}
|
|
162
|
+
function sortEntries(entries) {
|
|
163
|
+
return [...entries].sort((a, b) => a.order - b.order || a.title.localeCompare(b.title) || a.path.localeCompare(b.path));
|
|
164
|
+
}
|
|
165
|
+
function defaultEntries(rootPath, outputRootPath) {
|
|
166
|
+
return DEFAULT_NAVIGATION_ENTRIES.map(entry => {
|
|
167
|
+
try {
|
|
168
|
+
const base = entry.path === 'output' && outputRootPath ? outputRootPath : rootPath;
|
|
169
|
+
const realPath = fs.realpathSync(resolveEntryPath(rootPath, outputRootPath, entry.path));
|
|
170
|
+
if (!isInside(base, realPath) || !fs.statSync(realPath).isDirectory()) {
|
|
171
|
+
return { ...entry, availability: 'unavailable', reason: '栏目暂不可用' };
|
|
172
|
+
}
|
|
173
|
+
return { ...entry, availability: 'available' };
|
|
174
|
+
}
|
|
175
|
+
catch {
|
|
176
|
+
return { ...entry, availability: 'unavailable', reason: '栏目尚未创建' };
|
|
177
|
+
}
|
|
178
|
+
});
|
|
179
|
+
}
|
|
180
|
+
export class TeamWorkspaceStore {
|
|
181
|
+
read(agentAid) {
|
|
182
|
+
if (!isValidAid(agentAid))
|
|
183
|
+
return null;
|
|
184
|
+
let value;
|
|
185
|
+
try {
|
|
186
|
+
value = JSON.parse(fs.readFileSync(statePath(agentAid), 'utf8'));
|
|
187
|
+
}
|
|
188
|
+
catch {
|
|
189
|
+
return null;
|
|
190
|
+
}
|
|
191
|
+
if (!value || typeof value !== 'object' || Array.isArray(value))
|
|
192
|
+
return null;
|
|
193
|
+
const state = value;
|
|
194
|
+
if (state.version !== 1 || typeof state.workspaceId !== 'string' || !state.workspaceId
|
|
195
|
+
|| typeof state.ownerAid !== 'string' || typeof state.rootPath !== 'string'
|
|
196
|
+
|| !Number.isSafeInteger(state.navigationRevision) || !Array.isArray(state.entries))
|
|
197
|
+
return null;
|
|
198
|
+
const navigationRevision = state.navigationRevision;
|
|
199
|
+
return {
|
|
200
|
+
version: 1,
|
|
201
|
+
workspaceId: state.workspaceId,
|
|
202
|
+
ownerAid: state.ownerAid,
|
|
203
|
+
rootPath: state.rootPath,
|
|
204
|
+
...(typeof state.outputRootPath === 'string' ? { outputRootPath: state.outputRootPath } : {}),
|
|
205
|
+
navigationRevision,
|
|
206
|
+
entries: sortEntries(state.entries),
|
|
207
|
+
};
|
|
208
|
+
}
|
|
209
|
+
bind(agentAid, ownerAid, rootPath, outputRootPath) {
|
|
210
|
+
if (!isValidAid(agentAid) || !isValidAid(ownerAid))
|
|
211
|
+
return fail('INVALID_ARGUMENT', 'agentAid and ownerAid must be valid AIDs');
|
|
212
|
+
const canonical = canonicalDirectory(rootPath);
|
|
213
|
+
const canonicalOutput = outputRootPath ? canonicalDirectory(outputRootPath) : undefined;
|
|
214
|
+
if (canonicalOutput && !this.samePath(canonicalOutput, path.resolve(agentOutputDir(agentAid)))) {
|
|
215
|
+
return fail('INVALID_ROOT_PATH', 'Agent output root must be its Agent-owned output directory');
|
|
216
|
+
}
|
|
217
|
+
return this.withLock(agentAid, () => {
|
|
218
|
+
const current = this.read(agentAid);
|
|
219
|
+
if (current && current.ownerAid === ownerAid && this.samePath(current.rootPath, canonical)) {
|
|
220
|
+
if (!canonicalOutput || current.outputRootPath && this.samePath(current.outputRootPath, canonicalOutput))
|
|
221
|
+
return current;
|
|
222
|
+
const next = { ...current, outputRootPath: canonicalOutput };
|
|
223
|
+
this.write(agentAid, next);
|
|
224
|
+
return next;
|
|
225
|
+
}
|
|
226
|
+
const state = {
|
|
227
|
+
version: 1,
|
|
228
|
+
workspaceId: `team1_${crypto.randomBytes(18).toString('base64url')}`,
|
|
229
|
+
ownerAid,
|
|
230
|
+
rootPath: canonical,
|
|
231
|
+
...(canonicalOutput ? { outputRootPath: canonicalOutput } : {}),
|
|
232
|
+
navigationRevision: 1,
|
|
233
|
+
entries: defaultEntries(canonical, canonicalOutput),
|
|
234
|
+
};
|
|
235
|
+
this.write(agentAid, state);
|
|
236
|
+
return state;
|
|
237
|
+
});
|
|
238
|
+
}
|
|
239
|
+
rebindRoot(agentAid, ownerAid, rootPath) {
|
|
240
|
+
if (!isValidAid(agentAid) || !isValidAid(ownerAid))
|
|
241
|
+
return fail('INVALID_ARGUMENT', 'agentAid and ownerAid must be valid AIDs');
|
|
242
|
+
const canonical = canonicalDirectory(rootPath);
|
|
243
|
+
return this.withLock(agentAid, () => {
|
|
244
|
+
const current = this.read(agentAid);
|
|
245
|
+
if (!current)
|
|
246
|
+
return fail('NOT_CONFIGURED', 'Team workspace is not configured');
|
|
247
|
+
if (current.ownerAid !== ownerAid)
|
|
248
|
+
return fail('OWNER_UNAVAILABLE', 'Workspace binding belongs to another Owner');
|
|
249
|
+
if (this.samePath(current.rootPath, canonical))
|
|
250
|
+
return current;
|
|
251
|
+
const next = { ...current, rootPath: canonical };
|
|
252
|
+
this.write(agentAid, next);
|
|
253
|
+
return next;
|
|
254
|
+
});
|
|
255
|
+
}
|
|
256
|
+
updateNavigation(agentAid, expectedNavigationRevision, entries) {
|
|
257
|
+
return this.withLock(agentAid, () => {
|
|
258
|
+
const state = this.require(agentAid);
|
|
259
|
+
if (!Number.isSafeInteger(expectedNavigationRevision))
|
|
260
|
+
return fail('INVALID_ARGUMENT', 'expectedNavigationRevision must be an integer');
|
|
261
|
+
if (expectedNavigationRevision !== state.navigationRevision)
|
|
262
|
+
return fail('CONFLICT', 'Navigation revision is stale', { expectedNavigationRevision, navigationRevision: state.navigationRevision });
|
|
263
|
+
if (!Array.isArray(entries) || entries.length > MAX_ENTRIES)
|
|
264
|
+
return fail('INVALID_ARGUMENT', `entries must contain at most ${MAX_ENTRIES} items`);
|
|
265
|
+
const normalized = sortEntries(entries.map((entry, index) => validateEntry(entry, this.root(state), this.outputRoot(state), index)));
|
|
266
|
+
const ids = new Set();
|
|
267
|
+
const paths = new Set();
|
|
268
|
+
for (const entry of normalized) {
|
|
269
|
+
if (ids.has(entry.id))
|
|
270
|
+
return fail('INVALID_ARGUMENT', `Duplicate navigation entry id: ${entry.id}`);
|
|
271
|
+
if (paths.has(entry.path))
|
|
272
|
+
return fail('INVALID_ARGUMENT', `Duplicate navigation entry path: ${entry.path}`);
|
|
273
|
+
ids.add(entry.id);
|
|
274
|
+
paths.add(entry.path);
|
|
275
|
+
}
|
|
276
|
+
const next = { ...state, navigationRevision: state.navigationRevision + 1, entries: normalized };
|
|
277
|
+
this.write(agentAid, next);
|
|
278
|
+
return next;
|
|
279
|
+
});
|
|
280
|
+
}
|
|
281
|
+
/** Merge entries by id. This is the agent-friendly default for publishing one artifact. */
|
|
282
|
+
upsertNavigation(agentAid, expectedNavigationRevision, entries) {
|
|
283
|
+
return this.withLock(agentAid, () => {
|
|
284
|
+
const state = this.require(agentAid);
|
|
285
|
+
this.assertRevision(state, expectedNavigationRevision);
|
|
286
|
+
if (!Array.isArray(entries) || entries.length === 0 || entries.length > MAX_ENTRIES) {
|
|
287
|
+
return fail('INVALID_ARGUMENT', `entries must contain 1 to ${MAX_ENTRIES} items`);
|
|
288
|
+
}
|
|
289
|
+
const incoming = entries.map((entry, index) => validateEntry(entry, this.root(state), this.outputRoot(state), index));
|
|
290
|
+
const merged = new Map(state.entries.map(entry => [entry.id, entry]));
|
|
291
|
+
for (const entry of incoming)
|
|
292
|
+
merged.set(entry.id, entry);
|
|
293
|
+
const normalized = sortEntries([...merged.values()]);
|
|
294
|
+
this.assertUniqueEntries(normalized);
|
|
295
|
+
const next = { ...state, navigationRevision: state.navigationRevision + 1, entries: normalized };
|
|
296
|
+
this.write(agentAid, next);
|
|
297
|
+
return next;
|
|
298
|
+
});
|
|
299
|
+
}
|
|
300
|
+
/** Remove entries explicitly by id; omission is never interpreted as deletion. */
|
|
301
|
+
removeNavigation(agentAid, expectedNavigationRevision, ids) {
|
|
302
|
+
return this.withLock(agentAid, () => {
|
|
303
|
+
const state = this.require(agentAid);
|
|
304
|
+
this.assertRevision(state, expectedNavigationRevision);
|
|
305
|
+
if (!Array.isArray(ids) || ids.length === 0 || ids.some(id => typeof id !== 'string' || !id.trim())) {
|
|
306
|
+
return fail('INVALID_ARGUMENT', 'ids must be a non-empty array of entry ids');
|
|
307
|
+
}
|
|
308
|
+
const remove = new Set(ids.map(id => id.trim()));
|
|
309
|
+
if (remove.size !== ids.length)
|
|
310
|
+
return fail('INVALID_ARGUMENT', 'ids cannot contain duplicates');
|
|
311
|
+
const nextEntries = state.entries.filter(entry => !remove.has(entry.id));
|
|
312
|
+
if (state.entries.length - nextEntries.length !== remove.size)
|
|
313
|
+
return fail('NOT_FOUND', 'One or more navigation entry ids were not found');
|
|
314
|
+
const next = { ...state, navigationRevision: state.navigationRevision + 1, entries: nextEntries };
|
|
315
|
+
this.write(agentAid, next);
|
|
316
|
+
return next;
|
|
317
|
+
});
|
|
318
|
+
}
|
|
319
|
+
assertRevision(state, expectedNavigationRevision) {
|
|
320
|
+
if (!Number.isSafeInteger(expectedNavigationRevision))
|
|
321
|
+
return fail('INVALID_ARGUMENT', 'expectedNavigationRevision must be an integer');
|
|
322
|
+
if (expectedNavigationRevision !== state.navigationRevision)
|
|
323
|
+
return fail('CONFLICT', 'Navigation revision is stale', { expectedNavigationRevision, navigationRevision: state.navigationRevision });
|
|
324
|
+
}
|
|
325
|
+
assertUniqueEntries(entries) {
|
|
326
|
+
const ids = new Set();
|
|
327
|
+
const paths = new Set();
|
|
328
|
+
for (const entry of entries) {
|
|
329
|
+
if (ids.has(entry.id))
|
|
330
|
+
return fail('INVALID_ARGUMENT', `Duplicate navigation entry id: ${entry.id}`);
|
|
331
|
+
if (paths.has(entry.path))
|
|
332
|
+
return fail('INVALID_ARGUMENT', `Duplicate navigation entry path: ${entry.path}`);
|
|
333
|
+
ids.add(entry.id);
|
|
334
|
+
paths.add(entry.path);
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
require(agentAid) {
|
|
338
|
+
const state = this.read(agentAid);
|
|
339
|
+
if (!state)
|
|
340
|
+
return fail('NOT_CONFIGURED', 'Team workspace is not configured');
|
|
341
|
+
try {
|
|
342
|
+
if (!fs.statSync(this.root(state)).isDirectory())
|
|
343
|
+
throw new Error();
|
|
344
|
+
}
|
|
345
|
+
catch {
|
|
346
|
+
return fail('WORKSPACE_UNAVAILABLE', 'Team workspace is unavailable');
|
|
347
|
+
}
|
|
348
|
+
if (state.outputRootPath) {
|
|
349
|
+
try {
|
|
350
|
+
const output = canonicalDirectory(state.outputRootPath);
|
|
351
|
+
if (!this.samePath(output, path.resolve(agentOutputDir(agentAid))))
|
|
352
|
+
throw new Error();
|
|
353
|
+
}
|
|
354
|
+
catch {
|
|
355
|
+
return fail('WORKSPACE_UNAVAILABLE', 'Agent output directory is unavailable');
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
return state;
|
|
359
|
+
}
|
|
360
|
+
/** Preserve active registrations; an untouched default may adopt the old PA columns. */
|
|
361
|
+
importPersonalAssistantRegistration(agentAid, ownerAid) {
|
|
362
|
+
if (!isValidAid(agentAid) || !isValidAid(ownerAid))
|
|
363
|
+
return fail('INVALID_ARGUMENT', 'Invalid Agent or Owner');
|
|
364
|
+
return this.withLock(agentAid, () => {
|
|
365
|
+
const current = this.read(agentAid);
|
|
366
|
+
if (current && (current.ownerAid !== ownerAid || current.navigationRevision !== 1
|
|
367
|
+
|| current.entries.length !== DEFAULT_NAVIGATION_ENTRIES.length
|
|
368
|
+
|| current.entries.some((entry, index) => {
|
|
369
|
+
const preset = DEFAULT_NAVIGATION_ENTRIES[index];
|
|
370
|
+
return entry.id !== preset.id || entry.title !== preset.title || entry.path !== preset.path
|
|
371
|
+
|| entry.kind !== preset.kind || entry.order !== preset.order || entry.icon !== preset.icon
|
|
372
|
+
|| entry.tag !== undefined || entry.metadata !== undefined;
|
|
373
|
+
})))
|
|
374
|
+
return false;
|
|
375
|
+
const legacyPath = path.join(agentDataDir(agentAid), 'pa-workspace-state.json');
|
|
376
|
+
if (!fs.existsSync(legacyPath))
|
|
377
|
+
return false;
|
|
378
|
+
let state;
|
|
379
|
+
try {
|
|
380
|
+
state = JSON.parse(fs.readFileSync(legacyPath, 'utf8'));
|
|
381
|
+
}
|
|
382
|
+
catch {
|
|
383
|
+
return fail('WORKSPACE_MIGRATION_CONFLICT', 'The previous PA registration is invalid; it was not replaced');
|
|
384
|
+
}
|
|
385
|
+
if (state.version !== 1 || typeof state.workspaceId !== 'string' || !state.workspaceId
|
|
386
|
+
|| state.ownerAid !== ownerAid || !Number.isSafeInteger(state.navigationRevision) || state.navigationRevision < 1
|
|
387
|
+
|| !Array.isArray(state.entries) || state.entries.length > MAX_ENTRIES) {
|
|
388
|
+
return fail('WORKSPACE_MIGRATION_CONFLICT', 'The previous PA registration does not match the current Owner');
|
|
389
|
+
}
|
|
390
|
+
const rootPath = canonicalDirectory(state.rootPath);
|
|
391
|
+
if (current && !this.samePath(current.rootPath, rootPath))
|
|
392
|
+
return false;
|
|
393
|
+
const entries = state.entries.map((entry, index) => validateEntry(entry, rootPath, current?.outputRootPath, index));
|
|
394
|
+
this.assertUniqueEntries(entries);
|
|
395
|
+
if (current && JSON.stringify(current.entries) === JSON.stringify(sortEntries(entries)))
|
|
396
|
+
return false;
|
|
397
|
+
this.write(agentAid, {
|
|
398
|
+
version: 1, workspaceId: current?.workspaceId ?? state.workspaceId, ownerAid, rootPath,
|
|
399
|
+
...(current?.outputRootPath ? { outputRootPath: current.outputRootPath } : {}),
|
|
400
|
+
navigationRevision: current ? Math.max(current.navigationRevision, state.navigationRevision) + 1 : state.navigationRevision,
|
|
401
|
+
entries: sortEntries(entries),
|
|
402
|
+
});
|
|
403
|
+
return true;
|
|
404
|
+
});
|
|
405
|
+
}
|
|
406
|
+
root(state) { return canonicalDirectory(state.rootPath); }
|
|
407
|
+
outputRoot(state) {
|
|
408
|
+
return state.outputRootPath ? canonicalDirectory(state.outputRootPath) : undefined;
|
|
409
|
+
}
|
|
410
|
+
resolvePath(state, relativePath) {
|
|
411
|
+
const normalized = normalizeRelative(relativePath, true);
|
|
412
|
+
const workspaceRoot = this.root(state);
|
|
413
|
+
const outputRoot = this.outputRoot(state);
|
|
414
|
+
const isOutputPath = !!outputRoot && (normalized === 'output' || normalized.startsWith('output/'));
|
|
415
|
+
const root = isOutputPath ? outputRoot : workspaceRoot;
|
|
416
|
+
const suffix = isOutputPath ? normalized === 'output' ? '.' : normalized.slice('output/'.length) : normalized;
|
|
417
|
+
const candidate = path.resolve(root, suffix);
|
|
418
|
+
let realPath;
|
|
419
|
+
let stat;
|
|
420
|
+
try {
|
|
421
|
+
realPath = fs.realpathSync(candidate);
|
|
422
|
+
stat = fs.statSync(realPath);
|
|
423
|
+
}
|
|
424
|
+
catch {
|
|
425
|
+
return fail('NOT_FOUND', 'File or directory does not exist');
|
|
426
|
+
}
|
|
427
|
+
if (!isInside(root, realPath))
|
|
428
|
+
return fail('INVALID_ROOT_PATH', 'Path escapes the Team workspace');
|
|
429
|
+
const resolvedRelative = path.relative(root, realPath).split(path.sep).join('/') || '.';
|
|
430
|
+
if (resolvedRelative !== '.' && isTeamHiddenPath(resolvedRelative))
|
|
431
|
+
return fail('INVALID_ROOT_PATH', 'Team workspace path is not user-facing content');
|
|
432
|
+
const logicalPath = isOutputPath
|
|
433
|
+
? resolvedRelative === '.' ? 'output' : `output/${resolvedRelative}`
|
|
434
|
+
: resolvedRelative;
|
|
435
|
+
return { realPath, relativePath: logicalPath, stat };
|
|
436
|
+
}
|
|
437
|
+
entryAvailability(state, entry) {
|
|
438
|
+
try {
|
|
439
|
+
const resolved = this.resolvePath(state, entry.path);
|
|
440
|
+
const actualKind = resolved.stat.isDirectory() ? 'directory' : resolved.stat.isFile() ? 'file' : undefined;
|
|
441
|
+
if (actualKind !== entry.kind)
|
|
442
|
+
return { ...entry, availability: 'unavailable', reason: '入口类型已变化' };
|
|
443
|
+
const { reason: _reason, ...available } = entry;
|
|
444
|
+
return { ...available, availability: 'available' };
|
|
445
|
+
}
|
|
446
|
+
catch (error) {
|
|
447
|
+
const reason = error instanceof DataRootError && error.code === 'NOT_FOUND'
|
|
448
|
+
? entry.reason === '栏目尚未创建' ? entry.reason : '栏目尚未创建或内容已移动'
|
|
449
|
+
: '入口不可用';
|
|
450
|
+
return { ...entry, availability: 'unavailable', reason };
|
|
451
|
+
}
|
|
452
|
+
}
|
|
453
|
+
navigation(state) {
|
|
454
|
+
return {
|
|
455
|
+
workspaceId: state.workspaceId,
|
|
456
|
+
navigationRevision: state.navigationRevision,
|
|
457
|
+
title: 'Team Workspace',
|
|
458
|
+
entries: sortEntries(state.entries).filter(entry => !isTeamHiddenPath(entry.path)),
|
|
459
|
+
};
|
|
460
|
+
}
|
|
461
|
+
readNavigation(agentAid) {
|
|
462
|
+
return this.withLock(agentAid, () => {
|
|
463
|
+
const state = this.require(agentAid);
|
|
464
|
+
const entries = sortEntries(state.entries)
|
|
465
|
+
.filter(entry => !isTeamHiddenPath(entry.path))
|
|
466
|
+
.map(entry => this.entryAvailability(state, entry));
|
|
467
|
+
const changed = JSON.stringify(entries) !== JSON.stringify(state.entries);
|
|
468
|
+
const next = changed ? { ...state, entries } : state;
|
|
469
|
+
if (changed)
|
|
470
|
+
this.write(agentAid, next);
|
|
471
|
+
return this.navigation(next);
|
|
472
|
+
});
|
|
473
|
+
}
|
|
474
|
+
calendarArtifacts(state, navigation, from, to, limit = 500) {
|
|
475
|
+
const pending = [];
|
|
476
|
+
const items = [];
|
|
477
|
+
const visitedPaths = new Set();
|
|
478
|
+
let visited = 0;
|
|
479
|
+
let truncated = false;
|
|
480
|
+
for (const entry of navigation.entries) {
|
|
481
|
+
if (entry.availability === 'available')
|
|
482
|
+
pending.push({ path: entry.path, kind: entry.kind });
|
|
483
|
+
}
|
|
484
|
+
while (pending.length > 0 && !truncated) {
|
|
485
|
+
const current = pending.pop();
|
|
486
|
+
if (visitedPaths.has(current.path))
|
|
487
|
+
continue;
|
|
488
|
+
visitedPaths.add(current.path);
|
|
489
|
+
let resolved;
|
|
490
|
+
try {
|
|
491
|
+
resolved = this.resolvePath(state, current.path);
|
|
492
|
+
}
|
|
493
|
+
catch {
|
|
494
|
+
truncated = true;
|
|
495
|
+
continue;
|
|
496
|
+
}
|
|
497
|
+
if (current.kind === 'file') {
|
|
498
|
+
if (!resolved.stat.isFile())
|
|
499
|
+
continue;
|
|
500
|
+
const at = Math.floor(resolved.stat.mtimeMs);
|
|
501
|
+
if (at >= from && at < to) {
|
|
502
|
+
items.push({ path: resolved.relativePath, title: path.posix.basename(resolved.relativePath), at });
|
|
503
|
+
}
|
|
504
|
+
continue;
|
|
505
|
+
}
|
|
506
|
+
if (!resolved.stat.isDirectory())
|
|
507
|
+
continue;
|
|
508
|
+
let entries;
|
|
509
|
+
try {
|
|
510
|
+
entries = fs.readdirSync(resolved.realPath, { withFileTypes: true });
|
|
511
|
+
}
|
|
512
|
+
catch {
|
|
513
|
+
truncated = true;
|
|
514
|
+
continue;
|
|
515
|
+
}
|
|
516
|
+
entries.sort((a, b) => a.name.localeCompare(b.name));
|
|
517
|
+
for (const entry of entries) {
|
|
518
|
+
const relativePath = `${resolved.relativePath}/${entry.name}`;
|
|
519
|
+
if (isTeamHiddenPath(relativePath))
|
|
520
|
+
continue;
|
|
521
|
+
if (++visited > 20_000) {
|
|
522
|
+
truncated = true;
|
|
523
|
+
break;
|
|
524
|
+
}
|
|
525
|
+
if (entry.isDirectory())
|
|
526
|
+
pending.push({ path: relativePath, kind: 'directory' });
|
|
527
|
+
else if (entry.isFile())
|
|
528
|
+
pending.push({ path: relativePath, kind: 'file' });
|
|
529
|
+
}
|
|
530
|
+
}
|
|
531
|
+
items.sort((a, b) => a.at - b.at || a.path.localeCompare(b.path));
|
|
532
|
+
if (items.length > limit) {
|
|
533
|
+
items.splice(0, items.length - limit);
|
|
534
|
+
truncated = true;
|
|
535
|
+
}
|
|
536
|
+
return { items, truncated };
|
|
537
|
+
}
|
|
538
|
+
write(agentAid, state) {
|
|
539
|
+
const destination = statePath(agentAid);
|
|
540
|
+
fs.mkdirSync(path.dirname(destination), { recursive: true });
|
|
541
|
+
const temporary = `${destination}.${process.pid}.${crypto.randomBytes(6).toString('hex')}.tmp`;
|
|
542
|
+
fs.writeFileSync(temporary, `${JSON.stringify(state, null, 2)}\n`, { encoding: 'utf8', flag: 'wx', mode: 0o600 });
|
|
543
|
+
try {
|
|
544
|
+
atomicReplaceFile(temporary, destination);
|
|
545
|
+
}
|
|
546
|
+
catch (error) {
|
|
547
|
+
try {
|
|
548
|
+
fs.rmSync(temporary, { force: true });
|
|
549
|
+
}
|
|
550
|
+
catch { }
|
|
551
|
+
throw error;
|
|
552
|
+
}
|
|
553
|
+
}
|
|
554
|
+
withLock(agentAid, action) {
|
|
555
|
+
const directory = agentDataDir(agentAid);
|
|
556
|
+
fs.mkdirSync(directory, { recursive: true });
|
|
557
|
+
const lockPath = path.join(directory, `${STATE_FILE}.lock`);
|
|
558
|
+
let fd;
|
|
559
|
+
const deadline = Date.now() + 10_000;
|
|
560
|
+
while (fd === undefined) {
|
|
561
|
+
try {
|
|
562
|
+
fd = fs.openSync(lockPath, 'wx', 0o600);
|
|
563
|
+
}
|
|
564
|
+
catch (error) {
|
|
565
|
+
if (error.code !== 'EEXIST')
|
|
566
|
+
throw error;
|
|
567
|
+
try {
|
|
568
|
+
if (Date.now() - fs.statSync(lockPath).mtimeMs > 120_000)
|
|
569
|
+
fs.rmSync(lockPath, { force: true });
|
|
570
|
+
}
|
|
571
|
+
catch { /* another process may have released it */ }
|
|
572
|
+
if (Date.now() >= deadline)
|
|
573
|
+
return fail('CONFLICT', 'Team workspace is being updated; retry shortly');
|
|
574
|
+
Atomics.wait(lockDelay, 0, 0, 20);
|
|
575
|
+
}
|
|
576
|
+
}
|
|
577
|
+
try {
|
|
578
|
+
return action();
|
|
579
|
+
}
|
|
580
|
+
finally {
|
|
581
|
+
fs.closeSync(fd);
|
|
582
|
+
fs.rmSync(lockPath, { force: true });
|
|
583
|
+
}
|
|
584
|
+
}
|
|
585
|
+
samePath(a, b) {
|
|
586
|
+
try {
|
|
587
|
+
return fs.realpathSync(a) === b;
|
|
588
|
+
}
|
|
589
|
+
catch {
|
|
590
|
+
return path.resolve(a) === b;
|
|
591
|
+
}
|
|
592
|
+
}
|
|
593
|
+
}
|
|
594
|
+
export const teamWorkspaceStore = new TeamWorkspaceStore();
|
|
595
|
+
/** Explicit Core-side Team Workspace binding hook. */
|
|
596
|
+
export function bindTeamWorkspace(agentAid, ownerAid, rootPath) {
|
|
597
|
+
return teamWorkspaceStore.bind(agentAid, ownerAid, rootPath);
|
|
598
|
+
}
|
|
599
|
+
/** Core-side navigation read capability. */
|
|
600
|
+
export function readTeamWorkspaceNavigation(agentAid) {
|
|
601
|
+
return teamWorkspaceStore.readNavigation(agentAid);
|
|
602
|
+
}
|
|
603
|
+
/** Core-side full-replacement operation for deliberate navigation resets. */
|
|
604
|
+
export function updateTeamWorkspaceNavigation(agentAid, expectedNavigationRevision, entries) {
|
|
605
|
+
return teamWorkspaceStore.navigation(teamWorkspaceStore.updateNavigation(agentAid, expectedNavigationRevision, entries));
|
|
606
|
+
}
|
|
607
|
+
export function upsertTeamWorkspaceNavigation(aid, expectedRevision, entries) {
|
|
608
|
+
return teamWorkspaceStore.navigation(teamWorkspaceStore.upsertNavigation(aid, expectedRevision, entries));
|
|
609
|
+
}
|
|
610
|
+
export function removeTeamWorkspaceNavigation(aid, expectedRevision, ids) {
|
|
611
|
+
return teamWorkspaceStore.navigation(teamWorkspaceStore.removeNavigation(aid, expectedRevision, ids));
|
|
612
|
+
}
|