evolcore 0.0.41 → 0.0.42
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 +36 -0
- package/bin/codex-managed-hook.mjs +9 -5
- package/dist/agents/baseagent.js +156 -16
- package/dist/agents/claude-runner.js +10 -3
- package/dist/agents/codex/catalog.js +8 -4
- package/dist/agents/codex-runner.js +236 -59
- package/dist/agents/ecagent-runner.js +82 -13
- package/dist/aun/aid/agentmd.js +106 -27
- package/dist/aun/aid/client.js +1 -1
- package/dist/aun/aid/control-aid.js +65 -13
- package/dist/aun/aid/identity.js +70 -4
- package/dist/aun/aid/index.js +2 -2
- package/dist/aun/service-proxy.js +25 -1
- package/dist/channels/aun.js +242 -209
- package/dist/channels/dingtalk.js +1 -1
- package/dist/channels/feishu.js +1 -1
- package/dist/channels/qqbot.js +1 -1
- package/dist/channels/wechat.js +1 -1
- package/dist/channels/wecom.js +1 -1
- package/dist/cli/agent-command.js +183 -3
- package/dist/cli/agent-create.js +92 -86
- package/dist/cli/agent.js +136 -1
- package/dist/cli/aun-commands.js +137 -35
- package/dist/cli/bench.js +8 -2
- package/dist/cli/command-log.js +1 -1
- package/dist/cli/daemon-commands.js +235 -51
- package/dist/cli/debug-mode.js +1 -1
- package/dist/cli/directory.js +170 -0
- package/dist/cli/index.js +40 -30
- package/dist/cli/init-channel.js +4 -4
- package/dist/cli/init.js +47 -32
- package/dist/cli/managed-command-guard.js +4 -1
- package/dist/cli/model.js +27 -18
- package/dist/cli/pa-workspace-command.js +191 -0
- package/dist/cli/{contact.js → peer.js} +19 -25
- package/dist/cli/restart-monitor.js +11 -4
- package/dist/cli/thread-command.js +47 -18
- package/dist/cli/version.js +18 -18
- package/dist/config/access-policy.js +29 -12
- package/dist/config/boot-log.js +65 -10
- package/dist/config/builtin-roles.js +2 -4
- package/dist/config/config-field-policy.js +3 -0
- package/dist/config/config-manager.js +28 -20
- package/dist/config/{contact-bind-code.js → peer-bind-code.js} +6 -6
- package/dist/config/{contact-book-store.js → peer-book-store.js} +75 -75
- package/dist/config/{contact-book-v2-startup.js → peer-book-v2-startup.js} +2 -2
- package/dist/config/{contact-book.js → peer-book.js} +47 -47
- package/dist/config/peer-operation-service.js +132 -0
- package/dist/config/{contact-request-service.js → peer-request-service.js} +90 -90
- package/dist/config/peer-role-resolver.js +3 -3
- package/dist/config/providers-config.js +17 -8
- package/dist/config/providers.js +1 -1
- package/dist/config/schema-migration.js +336 -39
- package/dist/core/agent-application-service.js +8 -2
- package/dist/core/agent-create-status.js +4 -5
- package/dist/core/agent-ipc-registration.js +5 -4
- package/dist/core/agent-runtime.js +30 -17
- package/dist/core/auth/operation-authorizer.js +0 -24
- package/dist/core/auth/operation-catalog.js +4 -20
- package/dist/core/baseagent-loader.js +27 -3
- package/dist/core/bootstrap-service.js +5 -22
- package/dist/core/capability/capability-manager.js +52 -35
- package/dist/core/channel-loader.js +43 -2
- package/dist/core/channel-runtime.js +2 -2
- package/dist/core/command/agent-control.js +54 -18
- package/dist/core/command/command-handler.js +92 -72
- package/dist/core/command/connect-menu.js +35 -42
- package/dist/core/command/daemon-diagnostics.js +41 -5
- package/dist/core/command/data-root-error.js +10 -0
- package/dist/core/command/data-root-resolver.js +53 -13
- package/dist/core/command/menu-catalog.js +1 -0
- package/dist/core/command/menu-handler.js +163 -65
- package/dist/core/command/menu-protocol.js +52 -0
- package/dist/core/command/pa-workspace-calendar.js +223 -0
- package/dist/core/command/pa-workspace-store.js +459 -0
- package/dist/core/command/role-menu.js +2 -2
- package/dist/core/command/slash-gate.js +1 -1
- package/dist/core/command/slash-handler.js +57 -41
- package/dist/core/daemon-lifecycle.js +90 -31
- package/dist/core/evolagent.js +6 -10
- package/dist/core/handoff/runtime.js +11 -4
- package/dist/core/host-snapshot.js +208 -0
- package/dist/core/initialization/service.js +152 -20
- package/dist/core/ipc-registration.js +33 -4
- package/dist/core/memory/batch-meta.js +27 -0
- package/dist/core/memory/consolidation-ops-records.js +44 -1
- package/dist/core/memory/consolidation.js +169 -27
- package/dist/core/message/aun-ipc-sender.js +2 -8
- package/dist/core/message/im-renderer.js +56 -3
- package/dist/core/message/message-admission.js +121 -124
- package/dist/core/message/message-bridge.js +38 -54
- package/dist/core/message/message-queue.js +7 -0
- package/dist/core/message/outbound.js +34 -9
- package/dist/core/message/response-engine.js +154 -73
- package/dist/core/model/catalog-service.js +1 -1
- package/dist/core/model/config-scope.js +2 -2
- package/dist/core/model/model-catalog.js +5 -4
- package/dist/core/model/model-diagnostics.js +6 -5
- package/dist/core/model/model-fallback.js +3 -6
- package/dist/core/model/model-permission.js +1 -1
- package/dist/core/ownership/owner-resolver.js +9 -9
- package/dist/core/permission/approval-gateway.js +1 -1
- package/dist/core/permission/ec-command-parser.js +2 -2
- package/dist/core/permission/tool-canonical.js +96 -46
- package/dist/core/permission/tool-error-code.js +41 -0
- package/dist/core/permission/tool-policy.js +71 -47
- package/dist/core/role/runtime-policy.js +1 -1
- package/dist/core/session/session-idle-monitor.js +10 -0
- package/dist/core/session/session-manager.js +2 -4
- package/dist/core/session/session-title.js +36 -8
- package/dist/core/system-session/memory.js +2 -7
- package/dist/core/thread/command-parser.js +6 -2
- package/dist/core/thread/ipc-operation.js +51 -36
- package/dist/core/thread/thread-operation.js +29 -61
- package/dist/core/thread/thread-service.js +57 -13
- package/dist/directory/canonical-digest.js +54 -0
- package/dist/directory/owner-directory-admission.js +114 -43
- package/dist/directory/owner-directory-agentmd-sync.js +89 -0
- package/dist/directory/owner-directory-cache.js +847 -459
- package/dist/directory/owner-directory-markdown.js +272 -0
- package/dist/directory/owner-directory-query-operation.js +195 -0
- package/dist/directory/owner-directory-remark-operation.js +147 -0
- package/dist/directory/owner-directory-remarks.js +145 -0
- package/dist/directory/owner-directory-scope.js +55 -0
- package/dist/directory/owner-directory-search.js +148 -0
- package/dist/directory/owner-directory-store.js +227 -0
- package/dist/eck/kit-renderer.js +1 -0
- package/dist/ecweb/basic-settings-service.js +4 -1
- package/dist/ecweb/message-context-service.js +13 -9
- package/dist/index.js +195 -37
- package/dist/ipc.js +86 -22
- package/dist/paths.js +11 -8
- package/dist/product.js +2 -0
- package/dist/proxy/manager.js +13 -2
- package/dist/proxy/service-supervisor.js +8 -1
- package/dist/trigger/history.js +23 -0
- package/dist/trigger/scheduler.js +3 -0
- package/dist/utils/atomic-write.js +21 -0
- package/dist/utils/cross-platform.js +35 -0
- package/dist/utils/ecweb-supervisor.js +42 -0
- package/dist/utils/error-utils.js +2 -1
- package/dist/utils/evolcore-version.js +19 -0
- package/dist/utils/host-info.js +242 -5
- package/dist/utils/instance-registry.js +28 -6
- package/dist/utils/logger.js +59 -14
- package/dist/utils/machine-info.js +46 -0
- package/dist/utils/process-introspect.js +166 -2
- package/dist/utils/startup-trace.js +245 -0
- package/ecagent/dist/agent-loop.js +27 -13
- package/ecagent/dist/harness/agent-harness.js +3 -3
- package/ecagent/dist/harness/types.d.ts +1 -0
- package/ecagent/dist/types.d.ts +1 -0
- package/kits/docs/INDEX.md +1 -1
- package/kits/docs/aun/AUN/346/266/210/346/201/257/347/261/273/345/236/213/344/270/216/345/261/236/346/200/247.md +72 -0
- package/kits/docs/evolcore/INDEX.md +3 -2
- package/kits/docs/evolcore/agent-create.md +17 -27
- package/kits/docs/evolcore/agent.md +4 -1
- package/kits/docs/evolcore/aid.md +3 -1
- package/kits/docs/evolcore/config.md +16 -5
- package/kits/docs/evolcore/ctl.md +9 -0
- package/kits/docs/evolcore/ec-capabilities.md +19 -0
- package/kits/docs/evolcore/memory-consolidation-batches.md +79 -5
- package/kits/docs/evolcore/menu-exec-schema-commands.md +2 -1
- package/kits/docs/evolcore/menu-protocol.md +1 -0
- package/kits/docs/evolcore/model.md +13 -4
- package/kits/docs/evolcore/msg.md +1 -1
- package/kits/docs/evolcore/pa-workspace.md +51 -0
- package/kits/docs/evolcore/peer.md +73 -0
- package/kits/docs/evolcore/thread-chain.md +4 -1
- package/kits/eck_manifest.json +21 -0
- package/kits/rules/06-channel.md +1 -1
- package/kits/schemas/_meta.json +12 -1
- package/kits/schemas/agent-config.schema.16.json +6 -0
- package/kits/schemas/agent-config.schema.17.json +897 -0
- package/kits/schemas/daemon.schema.9.json +8 -1
- package/kits/skills/ec-agent-create/SKILL.md +2 -2
- package/kits/skills/ec-identity/SKILL.md +45 -27
- package/kits/skills/ec-service-proxy/SKILL.md +7 -2
- package/kits/skills/personal-assistant/references/03-data-flow-and-gaps.md +4 -5
- package/kits/skills/thread-ops/SKILL.md +11 -11
- package/kits/templates/bootstrap-welcome.md +3 -15
- package/kits/templates/message-fragments/item.md +1 -1
- package/kits/templates/system-fragments/bootstrap.md +24 -23
- package/kits/templates/system-fragments/commands.md +12 -5
- package/kits/templates/system-fragments/thread.md +18 -3
- package/package.json +6 -4
- package/dist/config/contact-operation-service.js +0 -235
- package/dist/directory/owner-directory-authorization.js +0 -309
- package/kits/docs/evolcore/contact.md +0 -78
- /package/dist/config/{contact-alias.js → peer-alias.js} +0 -0
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
import fs from 'node:fs';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import { DataRootError } from './data-root-error.js';
|
|
4
|
+
import { isPaHiddenPath, paWorkspaceStore } from './pa-workspace-store.js';
|
|
5
|
+
const MAX_RANGE_DAYS = 62;
|
|
6
|
+
const MAX_FILES = 500;
|
|
7
|
+
const MAX_RUNS_PER_TRIGGER = 200;
|
|
8
|
+
function invalid(message) {
|
|
9
|
+
throw new DataRootError('INVALID_ARGUMENT', message);
|
|
10
|
+
}
|
|
11
|
+
function dateValue(value, fallback) {
|
|
12
|
+
if (value === undefined || value === null || value === '')
|
|
13
|
+
return fallback;
|
|
14
|
+
if (typeof value !== 'string' || !/^\d{4}-\d{2}-\d{2}$/.test(value)) {
|
|
15
|
+
return invalid('calendar dates must use YYYY-MM-DD');
|
|
16
|
+
}
|
|
17
|
+
const parsed = Date.parse(`${value}T00:00:00Z`);
|
|
18
|
+
if (!Number.isFinite(parsed) || new Date(parsed).toISOString().slice(0, 10) !== value) {
|
|
19
|
+
return invalid('calendar date is invalid');
|
|
20
|
+
}
|
|
21
|
+
return value;
|
|
22
|
+
}
|
|
23
|
+
function todayInTimezone(timezone) {
|
|
24
|
+
return dateForTimestamp(Date.now(), timezone);
|
|
25
|
+
}
|
|
26
|
+
function resolveRange(args, timezone) {
|
|
27
|
+
const today = todayInTimezone(timezone);
|
|
28
|
+
const fallbackFrom = shiftDate(today, -31);
|
|
29
|
+
const fallbackTo = shiftDate(today, 31);
|
|
30
|
+
const from = dateValue(args?.from, fallbackFrom);
|
|
31
|
+
const to = dateValue(args?.to, fallbackTo);
|
|
32
|
+
const first = Date.parse(`${from}T00:00:00Z`);
|
|
33
|
+
const last = Date.parse(`${to}T00:00:00Z`);
|
|
34
|
+
if (last < first)
|
|
35
|
+
return invalid('calendar to must be on or after from');
|
|
36
|
+
const days = Math.floor((last - first) / 86_400_000) + 1;
|
|
37
|
+
if (days > MAX_RANGE_DAYS)
|
|
38
|
+
return invalid(`calendar range cannot exceed ${MAX_RANGE_DAYS} days`);
|
|
39
|
+
return { from, to, first, last };
|
|
40
|
+
}
|
|
41
|
+
function shiftDate(value, days) {
|
|
42
|
+
const timestamp = Date.parse(`${value}T00:00:00Z`) + days * 86_400_000;
|
|
43
|
+
return new Date(timestamp).toISOString().slice(0, 10);
|
|
44
|
+
}
|
|
45
|
+
function resolveTimezone(value) {
|
|
46
|
+
const timezone = value === undefined || value === null || value === ''
|
|
47
|
+
? Intl.DateTimeFormat().resolvedOptions().timeZone || 'UTC'
|
|
48
|
+
: String(value).trim();
|
|
49
|
+
if (!timezone || timezone.length > 100)
|
|
50
|
+
return invalid('calendar timezone is invalid');
|
|
51
|
+
try {
|
|
52
|
+
new Intl.DateTimeFormat('en-US', { timeZone: timezone }).format();
|
|
53
|
+
}
|
|
54
|
+
catch {
|
|
55
|
+
return invalid('calendar timezone is invalid');
|
|
56
|
+
}
|
|
57
|
+
return timezone;
|
|
58
|
+
}
|
|
59
|
+
function dateForTimestamp(timestamp, timezone) {
|
|
60
|
+
const parts = new Intl.DateTimeFormat('en-US', {
|
|
61
|
+
timeZone: timezone,
|
|
62
|
+
year: 'numeric',
|
|
63
|
+
month: '2-digit',
|
|
64
|
+
day: '2-digit',
|
|
65
|
+
}).formatToParts(new Date(timestamp));
|
|
66
|
+
const values = new Map(parts.map(part => [part.type, part.value]));
|
|
67
|
+
return `${values.get('year')}-${values.get('month')}-${values.get('day')}`;
|
|
68
|
+
}
|
|
69
|
+
function inRange(timestamp, range, timezone) {
|
|
70
|
+
if (!Number.isFinite(timestamp) || timestamp <= 0)
|
|
71
|
+
return false;
|
|
72
|
+
const date = dateForTimestamp(timestamp, timezone);
|
|
73
|
+
return date >= range.from && date <= range.to;
|
|
74
|
+
}
|
|
75
|
+
function addItem(days, item, range, timezone) {
|
|
76
|
+
if (!inRange(item.at, range, timezone))
|
|
77
|
+
return;
|
|
78
|
+
const date = dateForTimestamp(item.at, timezone);
|
|
79
|
+
const values = days.get(date) ?? [];
|
|
80
|
+
values.push(item);
|
|
81
|
+
days.set(date, values);
|
|
82
|
+
}
|
|
83
|
+
function relativeChild(parent, child) {
|
|
84
|
+
return parent === '.' ? child : `${parent}/${child}`;
|
|
85
|
+
}
|
|
86
|
+
function collectFiles(state, navigation) {
|
|
87
|
+
const files = [];
|
|
88
|
+
const seen = new Set();
|
|
89
|
+
let truncated = false;
|
|
90
|
+
const visit = (entry, relativePath, rootEntryId) => {
|
|
91
|
+
if (files.length >= MAX_FILES) {
|
|
92
|
+
truncated = true;
|
|
93
|
+
return;
|
|
94
|
+
}
|
|
95
|
+
if (seen.has(relativePath) || isPaHiddenPath(relativePath))
|
|
96
|
+
return;
|
|
97
|
+
seen.add(relativePath);
|
|
98
|
+
let resolved;
|
|
99
|
+
try {
|
|
100
|
+
resolved = paWorkspaceStore.resolvePath(state, relativePath);
|
|
101
|
+
}
|
|
102
|
+
catch {
|
|
103
|
+
return;
|
|
104
|
+
}
|
|
105
|
+
if (resolved.stat.isFile()) {
|
|
106
|
+
if (resolved.stat.mtimeMs > 0)
|
|
107
|
+
files.push({
|
|
108
|
+
entry: { ...entry, id: rootEntryId, title: path.posix.basename(resolved.relativePath) },
|
|
109
|
+
relativePath: resolved.relativePath,
|
|
110
|
+
mtime: resolved.stat.mtimeMs,
|
|
111
|
+
});
|
|
112
|
+
return;
|
|
113
|
+
}
|
|
114
|
+
if (!resolved.stat.isDirectory())
|
|
115
|
+
return;
|
|
116
|
+
let names;
|
|
117
|
+
try {
|
|
118
|
+
names = fs.readdirSync(resolved.realPath).sort((a, b) => a.localeCompare(b));
|
|
119
|
+
}
|
|
120
|
+
catch {
|
|
121
|
+
return;
|
|
122
|
+
}
|
|
123
|
+
for (const name of names) {
|
|
124
|
+
if (isPaHiddenPath(relativeChild(resolved.relativePath, name)))
|
|
125
|
+
continue;
|
|
126
|
+
const childPath = relativeChild(resolved.relativePath, name);
|
|
127
|
+
visit({ ...entry, title: name, path: childPath, kind: 'file' }, childPath, rootEntryId);
|
|
128
|
+
if (files.length >= MAX_FILES) {
|
|
129
|
+
truncated = true;
|
|
130
|
+
return;
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
};
|
|
134
|
+
for (const entry of navigation.entries) {
|
|
135
|
+
if (entry.availability === 'unavailable' || isPaHiddenPath(entry.path))
|
|
136
|
+
continue;
|
|
137
|
+
visit(entry, entry.path, entry.id);
|
|
138
|
+
if (files.length >= MAX_FILES) {
|
|
139
|
+
truncated = true;
|
|
140
|
+
break;
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
return { files, truncated };
|
|
144
|
+
}
|
|
145
|
+
export function queryPaWorkspaceCalendar({ state, navigation, scheduler, args }) {
|
|
146
|
+
const timezone = resolveTimezone(args?.timezone);
|
|
147
|
+
const range = resolveRange(args, timezone);
|
|
148
|
+
const byDate = new Map();
|
|
149
|
+
const collected = collectFiles(state, navigation);
|
|
150
|
+
for (const file of collected.files) {
|
|
151
|
+
addItem(byDate, {
|
|
152
|
+
id: `artifact:${navigation.workspaceId}:${file.relativePath}`,
|
|
153
|
+
kind: 'artifact',
|
|
154
|
+
title: file.entry.title,
|
|
155
|
+
at: file.mtime,
|
|
156
|
+
entryId: file.entry.id,
|
|
157
|
+
path: file.relativePath,
|
|
158
|
+
}, range, timezone);
|
|
159
|
+
}
|
|
160
|
+
let triggerCount = 0;
|
|
161
|
+
if (scheduler) {
|
|
162
|
+
const definitions = scheduler.list({ all: true });
|
|
163
|
+
const items = scheduler.listItems(definitions);
|
|
164
|
+
triggerCount = items.length;
|
|
165
|
+
const runsById = new Set();
|
|
166
|
+
for (const item of items) {
|
|
167
|
+
if (item.enabled && item.nextFireAt)
|
|
168
|
+
addItem(byDate, {
|
|
169
|
+
id: `trigger:${item.id}:${item.nextFireAt}`,
|
|
170
|
+
kind: 'trigger',
|
|
171
|
+
title: item.name,
|
|
172
|
+
at: item.nextFireAt,
|
|
173
|
+
status: item.enabled ? 'planned' : 'disabled',
|
|
174
|
+
triggerId: item.id,
|
|
175
|
+
}, range, timezone);
|
|
176
|
+
const recentRuns = scheduler.recentRuns(item.id, MAX_RUNS_PER_TRIGGER);
|
|
177
|
+
const hasRecentLastRun = recentRuns.some(run => (run.finishedAt || run.startedAt) === item.lastFiredAt ||
|
|
178
|
+
run.startedAt === item.lastFiredAt);
|
|
179
|
+
if (item.lastFiredAt && !hasRecentLastRun)
|
|
180
|
+
addItem(byDate, {
|
|
181
|
+
id: `execution:${item.id}:${item.lastFiredAt}`,
|
|
182
|
+
kind: 'execution',
|
|
183
|
+
title: item.name,
|
|
184
|
+
at: item.lastFiredAt,
|
|
185
|
+
status: item.lastStatus ?? item.lastResult ?? 'completed',
|
|
186
|
+
triggerId: item.id,
|
|
187
|
+
}, range, timezone);
|
|
188
|
+
for (const run of recentRuns) {
|
|
189
|
+
if (runsById.has(run.runId))
|
|
190
|
+
continue;
|
|
191
|
+
runsById.add(run.runId);
|
|
192
|
+
addItem(byDate, {
|
|
193
|
+
id: `execution:${item.id}:${run.runId}`,
|
|
194
|
+
kind: 'execution',
|
|
195
|
+
title: item.name,
|
|
196
|
+
at: run.finishedAt || run.startedAt,
|
|
197
|
+
status: run.status,
|
|
198
|
+
triggerId: item.id,
|
|
199
|
+
runId: run.runId,
|
|
200
|
+
}, range, timezone);
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
const days = [...byDate.entries()]
|
|
205
|
+
.sort(([a], [b]) => a.localeCompare(b))
|
|
206
|
+
.map(([date, items]) => ({
|
|
207
|
+
date,
|
|
208
|
+
items: items.sort((a, b) => a.at - b.at || a.title.localeCompare(b.title)),
|
|
209
|
+
}));
|
|
210
|
+
return {
|
|
211
|
+
protocol: 'evol.pa-workspace',
|
|
212
|
+
protocolVersion: 1,
|
|
213
|
+
view: 'calendar',
|
|
214
|
+
timezone,
|
|
215
|
+
from: range.from,
|
|
216
|
+
to: range.to,
|
|
217
|
+
days,
|
|
218
|
+
sources: {
|
|
219
|
+
artifacts: { status: 'available', fileCount: collected.files.length, truncated: collected.truncated },
|
|
220
|
+
triggers: { status: scheduler ? 'available' : 'unavailable', triggerCount },
|
|
221
|
+
},
|
|
222
|
+
};
|
|
223
|
+
}
|
|
@@ -0,0 +1,459 @@
|
|
|
1
|
+
import crypto from 'node:crypto';
|
|
2
|
+
import fs from 'node:fs';
|
|
3
|
+
import path from 'node:path';
|
|
4
|
+
import { agentDataDir } from '../../paths.js';
|
|
5
|
+
import { isValidAid } from '../../aun/aid/validation.js';
|
|
6
|
+
import { atomicReplaceFile } from '../../utils/atomic-write.js';
|
|
7
|
+
import { DataRootError } from './data-root-error.js';
|
|
8
|
+
const STATE_FILE = 'pa-workspace-state.json';
|
|
9
|
+
const MAX_ENTRIES = 200;
|
|
10
|
+
const MAX_PATH_LENGTH = 2048;
|
|
11
|
+
const lockDelay = new Int32Array(new SharedArrayBuffer(4));
|
|
12
|
+
const DEFAULT_NAVIGATION_ENTRIES = [
|
|
13
|
+
{ id: 'reports', title: '报告', path: 'output/reports', kind: 'directory', order: 10, icon: 'report' },
|
|
14
|
+
{ id: 'tasks', title: '任务', path: 'output/tasks', kind: 'directory', order: 20, icon: 'task' },
|
|
15
|
+
{ id: 'knowledge', title: '沉淀', path: 'output/knowledge', kind: 'directory', order: 30, icon: 'memory' },
|
|
16
|
+
{ id: 'conversations', title: '对话档案', path: 'capture/conversation', kind: 'directory', order: 40, icon: 'conversation' },
|
|
17
|
+
{ id: 'notes', title: '笔记', path: 'capture/notes', kind: 'directory', order: 50, icon: 'notes' },
|
|
18
|
+
{ id: 'links', title: '链接收藏', path: 'capture/links', kind: 'directory', order: 60, icon: 'links' },
|
|
19
|
+
];
|
|
20
|
+
const LEGACY_AUTO_ENTRY_PATHS = new Set(['capture', 'output', 'README.md', 'INDEX.md']);
|
|
21
|
+
function statePath(paAid) {
|
|
22
|
+
return path.join(agentDataDir(paAid), STATE_FILE);
|
|
23
|
+
}
|
|
24
|
+
function fail(code, message, data) {
|
|
25
|
+
throw new DataRootError(code, message, data);
|
|
26
|
+
}
|
|
27
|
+
function canonicalDirectory(value) {
|
|
28
|
+
if (typeof value !== 'string' || !value.trim() || !path.isAbsolute(value)) {
|
|
29
|
+
return fail('INVALID_ARGUMENT', 'workspace rootPath must be an absolute path');
|
|
30
|
+
}
|
|
31
|
+
let real;
|
|
32
|
+
try {
|
|
33
|
+
real = fs.realpathSync(value);
|
|
34
|
+
}
|
|
35
|
+
catch {
|
|
36
|
+
return fail('ROOT_NOT_FOUND', 'PA workspace does not exist');
|
|
37
|
+
}
|
|
38
|
+
try {
|
|
39
|
+
if (!fs.statSync(real).isDirectory())
|
|
40
|
+
return fail('ROOT_NOT_FOUND', 'PA workspace is not a directory');
|
|
41
|
+
}
|
|
42
|
+
catch {
|
|
43
|
+
return fail('ROOT_NOT_FOUND', 'PA workspace does not exist');
|
|
44
|
+
}
|
|
45
|
+
return real;
|
|
46
|
+
}
|
|
47
|
+
function isInside(root, candidate) {
|
|
48
|
+
const relative = path.relative(root, candidate);
|
|
49
|
+
return relative === '' || (!relative.startsWith(`..${path.sep}`) && relative !== '..' && !path.isAbsolute(relative));
|
|
50
|
+
}
|
|
51
|
+
/** Paths which can contain runtime state are never PA user-facing content. */
|
|
52
|
+
export function isPaHiddenPath(relativePath) {
|
|
53
|
+
const parts = relativePath.replaceAll('\\', '/').split('/').filter(Boolean);
|
|
54
|
+
if (parts.some(part => part === '.' || part.startsWith('.')))
|
|
55
|
+
return true;
|
|
56
|
+
for (let i = 0; i + 1 < parts.length; i++) {
|
|
57
|
+
if (parts[i].toLowerCase() === 'capture' && parts[i + 1].toLowerCase() === '_manifest')
|
|
58
|
+
return true;
|
|
59
|
+
}
|
|
60
|
+
return false;
|
|
61
|
+
}
|
|
62
|
+
function normalizeRelative(value, allowRoot = false) {
|
|
63
|
+
if (typeof value !== 'string' || !value.trim() || value.length > MAX_PATH_LENGTH) {
|
|
64
|
+
return fail('INVALID_ROOT_PATH', 'A PA workspace path is required');
|
|
65
|
+
}
|
|
66
|
+
const raw = value.trim().replaceAll('\\', '/');
|
|
67
|
+
if (raw.startsWith('/') || path.isAbsolute(raw) || path.win32.isAbsolute(raw)
|
|
68
|
+
|| raw.includes(':') || raw.includes('\0') || raw.split('/').includes('..')) {
|
|
69
|
+
return fail('INVALID_ROOT_PATH', 'PA workspace paths must be relative and cannot contain traversal');
|
|
70
|
+
}
|
|
71
|
+
const normalized = raw.split('/').filter(part => part && part !== '.').join('/');
|
|
72
|
+
if ((!normalized && !allowRoot) || isPaHiddenPath(normalized))
|
|
73
|
+
return fail('INVALID_ROOT_PATH', 'PA workspace path is not user-facing content');
|
|
74
|
+
return normalized || '.';
|
|
75
|
+
}
|
|
76
|
+
function entryId(relativePath) {
|
|
77
|
+
return `pa-${crypto.createHash('sha256').update(relativePath).digest('hex').slice(0, 16)}`;
|
|
78
|
+
}
|
|
79
|
+
function validateEntry(entry, rootPath, index) {
|
|
80
|
+
if (!entry || typeof entry !== 'object' || Array.isArray(entry))
|
|
81
|
+
return fail('INVALID_ARGUMENT', `entries[${index}] must be an object`);
|
|
82
|
+
const value = entry;
|
|
83
|
+
const relativePath = normalizeRelative(value.path);
|
|
84
|
+
const title = typeof value.title === 'string' && value.title.trim() ? value.title.trim() : path.posix.basename(relativePath);
|
|
85
|
+
if (title.length > 200)
|
|
86
|
+
return fail('INVALID_ARGUMENT', `entries[${index}].title is too long`);
|
|
87
|
+
const requestedKind = value.kind;
|
|
88
|
+
if (requestedKind !== 'file' && requestedKind !== 'directory')
|
|
89
|
+
return fail('INVALID_ARGUMENT', `entries[${index}].kind is invalid`);
|
|
90
|
+
if (value.availability !== undefined && value.availability !== 'available' && value.availability !== 'unavailable') {
|
|
91
|
+
return fail('INVALID_ARGUMENT', `entries[${index}].availability is invalid`);
|
|
92
|
+
}
|
|
93
|
+
const unavailable = value.availability === 'unavailable';
|
|
94
|
+
const reason = typeof value.reason === 'string' && value.reason.trim() ? value.reason.trim().slice(0, 500) : undefined;
|
|
95
|
+
if (unavailable && !reason)
|
|
96
|
+
return fail('INVALID_ARGUMENT', `entries[${index}].reason is required for unavailable entries`);
|
|
97
|
+
const order = Number.isSafeInteger(value.order) ? Number(value.order) : index;
|
|
98
|
+
if (order < 0 || order > 1_000_000)
|
|
99
|
+
return fail('INVALID_ARGUMENT', `entries[${index}].order is invalid`);
|
|
100
|
+
const id = typeof value.id === 'string' && value.id.trim() ? value.id.trim() : entryId(relativePath);
|
|
101
|
+
if (id.length > 200)
|
|
102
|
+
return fail('INVALID_ARGUMENT', `entries[${index}].id is too long`);
|
|
103
|
+
const icon = typeof value.icon === 'string' && value.icon.trim() ? value.icon.trim().slice(0, 100) : undefined;
|
|
104
|
+
const candidate = path.resolve(rootPath, relativePath);
|
|
105
|
+
let realPath;
|
|
106
|
+
let stat;
|
|
107
|
+
try {
|
|
108
|
+
realPath = fs.realpathSync(candidate);
|
|
109
|
+
stat = fs.statSync(realPath);
|
|
110
|
+
}
|
|
111
|
+
catch {
|
|
112
|
+
if (!unavailable)
|
|
113
|
+
return fail('NOT_FOUND', `PA workspace entry does not exist: ${relativePath}`);
|
|
114
|
+
return {
|
|
115
|
+
id,
|
|
116
|
+
title,
|
|
117
|
+
path: relativePath,
|
|
118
|
+
kind: requestedKind,
|
|
119
|
+
order,
|
|
120
|
+
availability: 'unavailable',
|
|
121
|
+
reason,
|
|
122
|
+
...(icon ? { icon } : {}),
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
if (!isInside(rootPath, realPath))
|
|
126
|
+
return fail('INVALID_ROOT_PATH', 'PA workspace entry escapes the workspace');
|
|
127
|
+
const kind = stat.isDirectory() ? 'directory' : stat.isFile() ? 'file' : undefined;
|
|
128
|
+
if (!kind)
|
|
129
|
+
return fail('NOT_SUPPORTED', `PA workspace entry is not a regular file or directory: ${relativePath}`);
|
|
130
|
+
if (requestedKind !== kind)
|
|
131
|
+
return fail('INVALID_ARGUMENT', `entries[${index}].kind does not match the target`);
|
|
132
|
+
const result = { id, title, path: relativePath, kind, order, availability: 'available' };
|
|
133
|
+
if (icon)
|
|
134
|
+
result.icon = icon;
|
|
135
|
+
return result;
|
|
136
|
+
}
|
|
137
|
+
function sortEntries(entries) {
|
|
138
|
+
return [...entries].sort((a, b) => a.order - b.order || a.title.localeCompare(b.title) || a.path.localeCompare(b.path));
|
|
139
|
+
}
|
|
140
|
+
function defaultEntries(rootPath) {
|
|
141
|
+
return DEFAULT_NAVIGATION_ENTRIES.map(entry => {
|
|
142
|
+
try {
|
|
143
|
+
const realPath = fs.realpathSync(path.resolve(rootPath, entry.path));
|
|
144
|
+
if (!isInside(rootPath, realPath) || !fs.statSync(realPath).isDirectory()) {
|
|
145
|
+
return { ...entry, availability: 'unavailable', reason: '栏目暂不可用' };
|
|
146
|
+
}
|
|
147
|
+
return { ...entry, availability: 'available' };
|
|
148
|
+
}
|
|
149
|
+
catch {
|
|
150
|
+
return { ...entry, availability: 'unavailable', reason: '栏目尚未创建' };
|
|
151
|
+
}
|
|
152
|
+
});
|
|
153
|
+
}
|
|
154
|
+
function migratedDefaultEntries(state, rootPath) {
|
|
155
|
+
const defaults = defaultEntries(rootPath);
|
|
156
|
+
const defaultsByPath = new Map(defaults.map(entry => [entry.path, entry]));
|
|
157
|
+
const existing = state.navigationRevision === 1
|
|
158
|
+
? state.entries.filter(entry => !LEGACY_AUTO_ENTRY_PATHS.has(entry.path))
|
|
159
|
+
: state.entries;
|
|
160
|
+
const migrated = existing.map(entry => {
|
|
161
|
+
const preset = defaultsByPath.get(entry.path);
|
|
162
|
+
return state.navigationRevision === 1 && preset ? preset : entry;
|
|
163
|
+
});
|
|
164
|
+
const paths = new Set(migrated.map(entry => entry.path));
|
|
165
|
+
const ids = new Set(migrated.map(entry => entry.id));
|
|
166
|
+
const additions = defaults.filter(entry => !paths.has(entry.path)).map(entry => {
|
|
167
|
+
let id = entry.id;
|
|
168
|
+
let suffix = 1;
|
|
169
|
+
while (ids.has(id))
|
|
170
|
+
id = `${entry.id}-preset-${suffix++}`;
|
|
171
|
+
ids.add(id);
|
|
172
|
+
return { ...entry, id };
|
|
173
|
+
});
|
|
174
|
+
return sortEntries([...migrated, ...additions]);
|
|
175
|
+
}
|
|
176
|
+
export class PaWorkspaceStore {
|
|
177
|
+
read(paAid) {
|
|
178
|
+
if (!isValidAid(paAid))
|
|
179
|
+
return null;
|
|
180
|
+
let value;
|
|
181
|
+
try {
|
|
182
|
+
value = JSON.parse(fs.readFileSync(statePath(paAid), 'utf8'));
|
|
183
|
+
}
|
|
184
|
+
catch {
|
|
185
|
+
return null;
|
|
186
|
+
}
|
|
187
|
+
if (!value || typeof value !== 'object' || Array.isArray(value))
|
|
188
|
+
return null;
|
|
189
|
+
const state = value;
|
|
190
|
+
if (state.version !== 1 || typeof state.workspaceId !== 'string' || !state.workspaceId
|
|
191
|
+
|| typeof state.ownerAid !== 'string' || typeof state.rootPath !== 'string'
|
|
192
|
+
|| !Number.isSafeInteger(state.navigationRevision) || !Array.isArray(state.entries))
|
|
193
|
+
return null;
|
|
194
|
+
const navigationRevision = state.navigationRevision;
|
|
195
|
+
return {
|
|
196
|
+
version: 1,
|
|
197
|
+
workspaceId: state.workspaceId,
|
|
198
|
+
ownerAid: state.ownerAid,
|
|
199
|
+
rootPath: state.rootPath,
|
|
200
|
+
navigationRevision,
|
|
201
|
+
entries: sortEntries(state.entries),
|
|
202
|
+
defaultNavigationSeeded: state.defaultNavigationSeeded === true,
|
|
203
|
+
};
|
|
204
|
+
}
|
|
205
|
+
bind(paAid, ownerAid, rootPath) {
|
|
206
|
+
if (!isValidAid(paAid) || !isValidAid(ownerAid))
|
|
207
|
+
return fail('INVALID_ARGUMENT', 'paAid and ownerAid must be valid AIDs');
|
|
208
|
+
const canonical = canonicalDirectory(rootPath);
|
|
209
|
+
return this.withLock(paAid, () => {
|
|
210
|
+
const current = this.read(paAid);
|
|
211
|
+
if (current && current.ownerAid === ownerAid && this.samePath(current.rootPath, canonical))
|
|
212
|
+
return current;
|
|
213
|
+
const state = {
|
|
214
|
+
version: 1,
|
|
215
|
+
workspaceId: `paw1_${crypto.randomBytes(18).toString('base64url')}`,
|
|
216
|
+
ownerAid,
|
|
217
|
+
rootPath: canonical,
|
|
218
|
+
navigationRevision: 1,
|
|
219
|
+
entries: defaultEntries(canonical),
|
|
220
|
+
defaultNavigationSeeded: true,
|
|
221
|
+
};
|
|
222
|
+
this.write(paAid, state);
|
|
223
|
+
return state;
|
|
224
|
+
});
|
|
225
|
+
}
|
|
226
|
+
updateNavigation(paAid, expectedNavigationRevision, entries) {
|
|
227
|
+
return this.withLock(paAid, () => {
|
|
228
|
+
const state = this.require(paAid);
|
|
229
|
+
if (!Number.isSafeInteger(expectedNavigationRevision))
|
|
230
|
+
return fail('INVALID_ARGUMENT', 'expectedNavigationRevision must be an integer');
|
|
231
|
+
if (expectedNavigationRevision !== state.navigationRevision)
|
|
232
|
+
return fail('CONFLICT', 'Navigation revision is stale', { expectedNavigationRevision, navigationRevision: state.navigationRevision });
|
|
233
|
+
if (!Array.isArray(entries) || entries.length > MAX_ENTRIES)
|
|
234
|
+
return fail('INVALID_ARGUMENT', `entries must contain at most ${MAX_ENTRIES} items`);
|
|
235
|
+
const normalized = sortEntries(entries.map((entry, index) => validateEntry(entry, this.root(state), index)));
|
|
236
|
+
const ids = new Set();
|
|
237
|
+
const paths = new Set();
|
|
238
|
+
for (const entry of normalized) {
|
|
239
|
+
if (ids.has(entry.id))
|
|
240
|
+
return fail('INVALID_ARGUMENT', `Duplicate navigation entry id: ${entry.id}`);
|
|
241
|
+
if (paths.has(entry.path))
|
|
242
|
+
return fail('INVALID_ARGUMENT', `Duplicate navigation entry path: ${entry.path}`);
|
|
243
|
+
ids.add(entry.id);
|
|
244
|
+
paths.add(entry.path);
|
|
245
|
+
}
|
|
246
|
+
const next = { ...state, navigationRevision: state.navigationRevision + 1, entries: normalized, defaultNavigationSeeded: true };
|
|
247
|
+
this.write(paAid, next);
|
|
248
|
+
return next;
|
|
249
|
+
});
|
|
250
|
+
}
|
|
251
|
+
require(paAid) {
|
|
252
|
+
const state = this.read(paAid);
|
|
253
|
+
if (!state)
|
|
254
|
+
return fail('NOT_CONFIGURED', 'PA workspace is not configured');
|
|
255
|
+
try {
|
|
256
|
+
if (!fs.statSync(this.root(state)).isDirectory())
|
|
257
|
+
throw new Error();
|
|
258
|
+
}
|
|
259
|
+
catch {
|
|
260
|
+
return fail('WORKSPACE_UNAVAILABLE', 'PA workspace is unavailable');
|
|
261
|
+
}
|
|
262
|
+
return state;
|
|
263
|
+
}
|
|
264
|
+
root(state) { return canonicalDirectory(state.rootPath); }
|
|
265
|
+
resolvePath(state, relativePath) {
|
|
266
|
+
const normalized = normalizeRelative(relativePath, true);
|
|
267
|
+
const root = this.root(state);
|
|
268
|
+
const candidate = path.resolve(root, normalized);
|
|
269
|
+
let realPath;
|
|
270
|
+
let stat;
|
|
271
|
+
try {
|
|
272
|
+
realPath = fs.realpathSync(candidate);
|
|
273
|
+
stat = fs.statSync(realPath);
|
|
274
|
+
}
|
|
275
|
+
catch {
|
|
276
|
+
return fail('NOT_FOUND', 'File or directory does not exist');
|
|
277
|
+
}
|
|
278
|
+
if (!isInside(root, realPath))
|
|
279
|
+
return fail('INVALID_ROOT_PATH', 'Path escapes the PA workspace');
|
|
280
|
+
const relative = path.relative(root, realPath).split(path.sep).join('/') || '.';
|
|
281
|
+
if (relative !== '.' && isPaHiddenPath(relative))
|
|
282
|
+
return fail('INVALID_ROOT_PATH', 'PA workspace path is not user-facing content');
|
|
283
|
+
return { realPath, relativePath: relative, stat };
|
|
284
|
+
}
|
|
285
|
+
entryAvailability(state, entry) {
|
|
286
|
+
try {
|
|
287
|
+
const resolved = this.resolvePath(state, entry.path);
|
|
288
|
+
const actualKind = resolved.stat.isDirectory() ? 'directory' : resolved.stat.isFile() ? 'file' : undefined;
|
|
289
|
+
if (actualKind !== entry.kind)
|
|
290
|
+
return { ...entry, availability: 'unavailable', reason: '入口类型已变化' };
|
|
291
|
+
const { reason: _reason, ...available } = entry;
|
|
292
|
+
return { ...available, availability: 'available' };
|
|
293
|
+
}
|
|
294
|
+
catch (error) {
|
|
295
|
+
const reason = error instanceof DataRootError && error.code === 'NOT_FOUND'
|
|
296
|
+
? entry.reason === '栏目尚未创建' ? entry.reason : '栏目尚未创建或内容已移动'
|
|
297
|
+
: '入口不可用';
|
|
298
|
+
return { ...entry, availability: 'unavailable', reason };
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
navigation(state) {
|
|
302
|
+
return {
|
|
303
|
+
workspaceId: state.workspaceId,
|
|
304
|
+
navigationRevision: state.navigationRevision,
|
|
305
|
+
title: '个人助理',
|
|
306
|
+
entries: sortEntries(state.entries).filter(entry => !isPaHiddenPath(entry.path)),
|
|
307
|
+
};
|
|
308
|
+
}
|
|
309
|
+
readNavigation(paAid) {
|
|
310
|
+
return this.withLock(paAid, () => {
|
|
311
|
+
const state = this.require(paAid);
|
|
312
|
+
const baseEntries = state.defaultNavigationSeeded
|
|
313
|
+
? sortEntries(state.entries)
|
|
314
|
+
: migratedDefaultEntries(state, this.root(state));
|
|
315
|
+
const entries = baseEntries
|
|
316
|
+
.filter(entry => !isPaHiddenPath(entry.path))
|
|
317
|
+
.map(entry => this.entryAvailability(state, entry));
|
|
318
|
+
const changed = JSON.stringify(entries) !== JSON.stringify(state.entries);
|
|
319
|
+
const next = changed || !state.defaultNavigationSeeded
|
|
320
|
+
? { ...state, navigationRevision: state.navigationRevision + (changed ? 1 : 0), entries, defaultNavigationSeeded: true }
|
|
321
|
+
: state;
|
|
322
|
+
if (changed || !state.defaultNavigationSeeded)
|
|
323
|
+
this.write(paAid, next);
|
|
324
|
+
return this.navigation(next);
|
|
325
|
+
});
|
|
326
|
+
}
|
|
327
|
+
calendarArtifacts(state, navigation, from, to, limit = 500) {
|
|
328
|
+
const pending = [];
|
|
329
|
+
const items = [];
|
|
330
|
+
const visitedPaths = new Set();
|
|
331
|
+
let visited = 0;
|
|
332
|
+
let truncated = false;
|
|
333
|
+
for (const entry of navigation.entries) {
|
|
334
|
+
if (entry.availability === 'available')
|
|
335
|
+
pending.push({ path: entry.path, kind: entry.kind });
|
|
336
|
+
}
|
|
337
|
+
while (pending.length > 0 && !truncated) {
|
|
338
|
+
const current = pending.pop();
|
|
339
|
+
if (visitedPaths.has(current.path))
|
|
340
|
+
continue;
|
|
341
|
+
visitedPaths.add(current.path);
|
|
342
|
+
let resolved;
|
|
343
|
+
try {
|
|
344
|
+
resolved = this.resolvePath(state, current.path);
|
|
345
|
+
}
|
|
346
|
+
catch {
|
|
347
|
+
truncated = true;
|
|
348
|
+
continue;
|
|
349
|
+
}
|
|
350
|
+
if (current.kind === 'file') {
|
|
351
|
+
if (!resolved.stat.isFile())
|
|
352
|
+
continue;
|
|
353
|
+
const at = Math.floor(resolved.stat.mtimeMs);
|
|
354
|
+
if (at >= from && at < to) {
|
|
355
|
+
items.push({ path: resolved.relativePath, title: path.posix.basename(resolved.relativePath), at });
|
|
356
|
+
}
|
|
357
|
+
continue;
|
|
358
|
+
}
|
|
359
|
+
if (!resolved.stat.isDirectory())
|
|
360
|
+
continue;
|
|
361
|
+
let entries;
|
|
362
|
+
try {
|
|
363
|
+
entries = fs.readdirSync(resolved.realPath, { withFileTypes: true });
|
|
364
|
+
}
|
|
365
|
+
catch {
|
|
366
|
+
truncated = true;
|
|
367
|
+
continue;
|
|
368
|
+
}
|
|
369
|
+
entries.sort((a, b) => a.name.localeCompare(b.name));
|
|
370
|
+
for (const entry of entries) {
|
|
371
|
+
const relativePath = `${resolved.relativePath}/${entry.name}`;
|
|
372
|
+
if (isPaHiddenPath(relativePath))
|
|
373
|
+
continue;
|
|
374
|
+
if (++visited > 20_000) {
|
|
375
|
+
truncated = true;
|
|
376
|
+
break;
|
|
377
|
+
}
|
|
378
|
+
if (entry.isDirectory())
|
|
379
|
+
pending.push({ path: relativePath, kind: 'directory' });
|
|
380
|
+
else if (entry.isFile())
|
|
381
|
+
pending.push({ path: relativePath, kind: 'file' });
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
items.sort((a, b) => a.at - b.at || a.path.localeCompare(b.path));
|
|
385
|
+
if (items.length > limit) {
|
|
386
|
+
items.splice(0, items.length - limit);
|
|
387
|
+
truncated = true;
|
|
388
|
+
}
|
|
389
|
+
return { items, truncated };
|
|
390
|
+
}
|
|
391
|
+
write(paAid, state) {
|
|
392
|
+
const destination = statePath(paAid);
|
|
393
|
+
fs.mkdirSync(path.dirname(destination), { recursive: true });
|
|
394
|
+
const temporary = `${destination}.${process.pid}.${crypto.randomBytes(6).toString('hex')}.tmp`;
|
|
395
|
+
fs.writeFileSync(temporary, `${JSON.stringify(state, null, 2)}\n`, { encoding: 'utf8', flag: 'wx', mode: 0o600 });
|
|
396
|
+
try {
|
|
397
|
+
atomicReplaceFile(temporary, destination);
|
|
398
|
+
}
|
|
399
|
+
catch (error) {
|
|
400
|
+
try {
|
|
401
|
+
fs.rmSync(temporary, { force: true });
|
|
402
|
+
}
|
|
403
|
+
catch { }
|
|
404
|
+
throw error;
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
withLock(paAid, action) {
|
|
408
|
+
const directory = agentDataDir(paAid);
|
|
409
|
+
fs.mkdirSync(directory, { recursive: true });
|
|
410
|
+
const lockPath = path.join(directory, `${STATE_FILE}.lock`);
|
|
411
|
+
let fd;
|
|
412
|
+
const deadline = Date.now() + 10_000;
|
|
413
|
+
while (fd === undefined) {
|
|
414
|
+
try {
|
|
415
|
+
fd = fs.openSync(lockPath, 'wx', 0o600);
|
|
416
|
+
}
|
|
417
|
+
catch (error) {
|
|
418
|
+
if (error.code !== 'EEXIST')
|
|
419
|
+
throw error;
|
|
420
|
+
try {
|
|
421
|
+
if (Date.now() - fs.statSync(lockPath).mtimeMs > 120_000)
|
|
422
|
+
fs.rmSync(lockPath, { force: true });
|
|
423
|
+
}
|
|
424
|
+
catch { /* another process may have released it */ }
|
|
425
|
+
if (Date.now() >= deadline)
|
|
426
|
+
return fail('CONFLICT', 'PA workspace is being updated; retry shortly');
|
|
427
|
+
Atomics.wait(lockDelay, 0, 0, 20);
|
|
428
|
+
}
|
|
429
|
+
}
|
|
430
|
+
try {
|
|
431
|
+
return action();
|
|
432
|
+
}
|
|
433
|
+
finally {
|
|
434
|
+
fs.closeSync(fd);
|
|
435
|
+
fs.rmSync(lockPath, { force: true });
|
|
436
|
+
}
|
|
437
|
+
}
|
|
438
|
+
samePath(a, b) {
|
|
439
|
+
try {
|
|
440
|
+
return fs.realpathSync(a) === b;
|
|
441
|
+
}
|
|
442
|
+
catch {
|
|
443
|
+
return path.resolve(a) === b;
|
|
444
|
+
}
|
|
445
|
+
}
|
|
446
|
+
}
|
|
447
|
+
export const paWorkspaceStore = new PaWorkspaceStore();
|
|
448
|
+
/** Explicit Core-side binding hook for PA initialization/--workspace callers. */
|
|
449
|
+
export function bindPaWorkspace(paAid, ownerAid, rootPath) {
|
|
450
|
+
return paWorkspaceStore.bind(paAid, ownerAid, rootPath);
|
|
451
|
+
}
|
|
452
|
+
/** Core-side capability used by PA initialization and conversation tooling. */
|
|
453
|
+
export function readPaWorkspaceNavigation(paAid) {
|
|
454
|
+
return paWorkspaceStore.readNavigation(paAid);
|
|
455
|
+
}
|
|
456
|
+
/** Core-side capability used by PA conversation tooling; App has no write route. */
|
|
457
|
+
export function updatePaWorkspaceNavigation(paAid, expectedNavigationRevision, entries) {
|
|
458
|
+
return paWorkspaceStore.navigation(paWorkspaceStore.updateNavigation(paAid, expectedNavigationRevision, entries));
|
|
459
|
+
}
|