evolcore 0.0.42 → 0.0.43-test.0
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 +29 -0
- package/bin/ec-safe-output.js +4 -1
- package/dist/ability/application-service.js +882 -0
- package/dist/ability/authorization.js +70 -0
- package/dist/ability/command-contract.js +105 -0
- package/dist/ability/daemon.js +137 -0
- package/dist/ability/development.js +450 -0
- package/dist/ability/environment.js +75 -0
- package/dist/ability/interop-v1.js +547 -0
- package/dist/ability/management.js +79 -0
- package/dist/ability/package.js +264 -0
- package/dist/ability/provider.js +73 -0
- package/dist/ability/publication.js +107 -0
- package/dist/ability/resources.js +106 -0
- package/dist/ability/runtime.js +212 -0
- package/dist/ability/secrets.js +47 -0
- package/dist/ability/store.js +112 -0
- package/dist/ability/toolchain.js +69 -0
- package/dist/ability/types.js +14 -0
- package/dist/ability/versions.js +32 -0
- package/dist/agents/baseagent.js +2 -2
- package/dist/agents/claude-runner.js +4 -0
- package/dist/agents/codex-runner.js +81 -17
- package/dist/agents/ecagent-plugin-lazy.js +41 -0
- package/dist/agents/ecagent-runner.js +18 -11
- package/dist/aun/ability-device-routing.js +12 -1
- package/dist/aun/aid/agentmd.js +2 -2
- package/dist/aun/aid/identity.js +45 -0
- package/dist/aun/aid/index.js +1 -0
- package/dist/aun/aid/managed-operation.js +73 -2
- package/dist/channels/aun.js +64 -18
- package/dist/channels/builtin-plugins.js +14 -0
- package/dist/channels/dingtalk.js +78 -26
- package/dist/channels/feishu.js +89 -14
- package/dist/channels/qqbot.js +84 -19
- package/dist/channels/wechat.js +77 -19
- package/dist/channels/wecom.js +67 -22
- package/dist/cli/ability-command.js +92 -0
- package/dist/cli/aun-commands.js +124 -85
- package/dist/cli/daemon-commands.js +37 -85
- package/dist/cli/index.js +14 -0
- package/dist/cli/init.js +11 -13
- package/dist/cli/managed-command-guard.js +6 -0
- package/dist/cli/model.js +101 -104
- package/dist/cli/provider-command.js +6 -4
- package/dist/cli/restart-monitor.js +52 -11
- package/dist/cli/upgrade-command.js +102 -0
- package/dist/cli/version.js +1 -1
- package/dist/config/access-policy.js +4 -2
- package/dist/config/builtin-roles.js +1 -0
- package/dist/config/config-field-policy.js +4 -0
- package/dist/config/config-manager.js +7 -0
- package/dist/config/provider-ipc-service.js +10 -0
- package/dist/config/providers-config.js +12 -3
- package/dist/config/providers-management.js +45 -9
- package/dist/config/schema-migration.js +40 -22
- package/dist/contracts/ecweb-service-protocol.js +2 -1
- package/dist/core/auth/agent-delegation.js +21 -5
- package/dist/core/auth/auth-gateway.js +31 -9
- package/dist/core/auth/authorization-audit.js +52 -4
- package/dist/core/auth/delegation-authorization.js +140 -4
- package/dist/core/auth/operation-authorizer.js +114 -13
- package/dist/core/auth/operation-catalog.js +118 -4
- package/dist/core/baseagent-loader.js +14 -1
- package/dist/core/channel-loader.js +47 -9
- package/dist/core/channel-runtime.js +1 -1
- package/dist/core/command/agent-storage-menu.js +1 -1
- package/dist/core/command/cli-intent-parser.js +26 -13
- package/dist/core/command/command-handler.js +232 -76
- package/dist/core/command/connect-menu.js +10 -4
- package/dist/core/command/daemon-diagnostics.js +3 -3
- package/dist/core/command/data-root-resolver.js +2 -3
- package/dist/core/command/menu-catalog.js +10 -3
- package/dist/core/command/menu-handler.js +321 -46
- package/dist/core/command/menu-protocol.js +53 -7
- package/dist/core/command/pa-workspace-calendar.js +1 -2
- package/dist/core/command/pa-workspace-items.js +486 -0
- package/dist/core/command/pa-workspace-store.js +10 -1
- package/dist/core/command/slash-gate.js +2 -2
- package/dist/core/command/slash-handler.js +67 -25
- package/dist/core/contact-bind-registry.js +27 -0
- package/dist/core/daemon-control.js +3 -3
- package/dist/core/daemon-lifecycle.js +2 -1
- package/dist/core/event-bus.js +9 -2
- package/dist/core/handoff/ipc-registration.js +1 -0
- package/dist/core/host-snapshot.js +1 -2
- package/dist/core/initialization/service.js +32 -11
- package/dist/core/ipc-registration.js +18 -5
- package/dist/core/memory/consolidation-context.js +2 -2
- package/dist/core/message/aun-request-registry.js +99 -18
- package/dist/core/message/im-renderer.js +16 -11
- package/dist/core/message/message-admission.js +25 -20
- package/dist/core/message/message-bridge.js +21 -47
- package/dist/core/message/message-queue.js +3 -0
- package/dist/core/message/response-engine.js +7 -5
- package/dist/core/model/catalog-service.js +1 -1
- package/dist/core/permission/ec-command-parser.js +41 -9
- package/dist/core/permission/permission-result.js +1 -0
- package/dist/core/permission/protected-paths.js +25 -14
- package/dist/core/permission/tool-policy.js +108 -107
- package/dist/core/plugin-lifecycle.js +133 -0
- package/dist/core/runner-reload-transaction.js +4 -1
- package/dist/core/system-session/memory.js +16 -3
- package/dist/core/task/task-activity-store.js +50 -0
- package/dist/core/task/task-image-resources.js +140 -0
- package/dist/core/thread/ipc-operation.js +1 -1
- package/dist/core/thread/thread-service.js +0 -1
- package/dist/directory/canonical-digest.js +5 -3
- package/dist/directory/owner-directory-admission.js +22 -2
- package/dist/directory/owner-directory-cache.js +394 -60
- package/dist/directory/owner-directory-markdown.js +29 -2
- package/dist/directory/owner-directory-scope.js +35 -16
- package/dist/ecweb/basic-settings-service.js +4 -1
- package/dist/ecweb/pairing.js +41 -0
- package/dist/ecweb/providers-service.js +8 -1
- package/dist/{utils/ecweb-utils.js → ecweb/runtime/launch.js} +3 -27
- package/dist/ecweb/runtime/lifecycle.js +184 -0
- package/dist/{utils/ecweb-supervisor.js → ecweb/runtime/supervisor.js} +32 -27
- package/dist/index.js +34 -25
- package/dist/ipc.js +124 -7
- package/dist/response-system/engines/v1/proactive-flow.js +1 -1
- package/dist/trigger/authorization.js +2 -2
- package/dist/utils/atomic-write.js +24 -0
- package/dist/utils/avatar-upload.js +3 -1
- package/dist/utils/cross-platform.js +31 -2
- package/dist/utils/host-info.js +36 -0
- package/dist/utils/instance-registry.js +38 -11
- package/dist/utils/logger.js +9 -0
- package/dist/utils/{process-tree-stats.js → process-tree.js} +29 -0
- package/dist/utils/tool-summary.js +9 -1
- package/dist/utils/upgrade-jobs.js +96 -15
- package/dist/utils/windows-autostart.js +1 -1
- package/ecagent/README.md +19 -0
- package/ecagent/dist/harness/agent-harness.js +9 -0
- package/ecagent/dist/harness/env/nodejs.js +71 -11
- package/ecagent/dist/harness/env/windows-shell.d.ts +7 -0
- package/ecagent/dist/harness/env/windows-shell.js +132 -0
- package/kits/docs/aun/ABILITY_CALLING.md +38 -9
- package/kits/docs/evolcore/INDEX.md +5 -1
- package/kits/docs/evolcore/ability.md +181 -0
- package/kits/docs/evolcore/aid.md +20 -0
- package/kits/docs/evolcore/config.md +10 -6
- package/kits/docs/evolcore/menu-protocol.md +40 -1
- package/kits/docs/evolcore/provider.md +2 -2
- package/kits/docs/evolcore/tool-authorization.md +270 -0
- package/kits/runtime/ability-job.ps1 +41 -0
- package/kits/runtime/ability-runner.mjs +67 -0
- package/kits/runtime/ability-secret.ps1 +59 -0
- package/kits/runtime/ability-templates/go/main.go +59 -0
- package/kits/runtime/ability-templates/java/Json.java +67 -0
- package/kits/runtime/ability-templates/java/Runner.java +42 -0
- package/kits/runtime/ability-templates/python/runner.py +48 -0
- package/kits/runtime/ability-templates/rust/json.rs +87 -0
- package/kits/runtime/ability-templates/rust/runner.rs +35 -0
- package/kits/schemas/_meta.json +12 -1
- package/kits/schemas/ability-plugin.schema.1.json +107 -0
- package/kits/schemas/ability-project.schema.1.json +31 -0
- package/kits/schemas/agent-config.schema.16.json +20 -0
- package/kits/schemas/agent-config.schema.17.json +20 -0
- package/kits/schemas/agent-config.schema.18.json +917 -0
- package/kits/skills/.switches.json +4 -0
- package/kits/skills/aid-ability/SKILL.md +5 -4
- package/kits/skills/ec-ability/SKILL.md +67 -0
- package/kits/skills/ec-ability/references/contract-checklist.md +37 -0
- package/kits/skills/ec-ability/references/development.md +104 -0
- package/kits/skills/ec-ability/references/management.md +117 -0
- package/kits/skills/ec-ability/references/recovery.md +27 -0
- package/kits/skills/ec-ability/references/runtime-protocol.md +109 -0
- package/kits/skills/ec-ability/references/workflows.md +15 -0
- package/kits/skills/ec-service-proxy/SKILL.md +12 -0
- package/kits/skills/personal-assistant/SKILL.md +240 -6
- package/kits/skills/personal-assistant/references/02-bootstrap.md +78 -4
- package/kits/skills/personal-assistant/scripts/clean.mjs +397 -0
- package/kits/skills/personal-assistant/scripts/init.mjs +113 -25
- package/kits/skills/personal-assistant/scripts/lock.mjs +189 -0
- package/kits/skills/personal-assistant/scripts/migrate.mjs +35 -10
- package/kits/skills/personal-assistant/scripts/verify.mjs +2 -1
- package/kits/skills/personal-assistant/templates/persona-section.md +54 -0
- package/kits/skills/personal-assistant/triggers/ingest-daily.json +1 -1
- package/kits/templates/memory-consolidation/notes.md +1 -1
- package/kits/templates/system-fragments/channel.md +2 -1
- package/kits/templates/system-fragments/commands.md +1 -0
- package/package.json +4 -2
- package/dist/core/command/data-root-error.js +0 -10
- package/dist/utils/machine-info.js +0 -46
- package/dist/utils/process-tree-worker.js +0 -31
- package/dist/utils/windows-output.js +0 -36
|
@@ -7,6 +7,7 @@ import { logger } from '../../utils/logger.js';
|
|
|
7
7
|
import { validateIdleSeconds } from '../session/session-idle-monitor.js';
|
|
8
8
|
import { parseFileSearchArgs } from './file-search.js';
|
|
9
9
|
import { validateProviderMenuRequest } from '../../config/provider-menu-request.js';
|
|
10
|
+
import { validatePaWorkspaceItemsArgs } from './pa-workspace-items.js';
|
|
10
11
|
export const MENU_REQUEST_TYPES = new Set([
|
|
11
12
|
'menu.token.request',
|
|
12
13
|
'menu.list',
|
|
@@ -351,11 +352,22 @@ export function validateMenuRequest(request) {
|
|
|
351
352
|
if (type !== 'menu.query')
|
|
352
353
|
return { code: 'NOT_SUPPORTED', message: 'pa-workspace only supports menu.query' };
|
|
353
354
|
const args = request.args;
|
|
354
|
-
const unknown = Object.keys(args ?? {}).find(key => !['view', 'protocolVersion', 'context', 'from', 'to', 'timezone'].includes(key));
|
|
355
|
+
const unknown = Object.keys(args ?? {}).find(key => !['view', 'protocolVersion', 'context', 'from', 'to', 'timezone', 'path', 'offset', 'limit', 'indexRevision'].includes(key));
|
|
355
356
|
if (unknown)
|
|
356
357
|
return { code: 'INVALID_ARGUMENT', message: `pa-workspace does not accept args.${unknown}` };
|
|
357
|
-
if (args?.view !== '
|
|
358
|
-
return { code: 'INVALID_ARGUMENT', message: 'pa-workspace requires
|
|
358
|
+
if (typeof args?.view !== 'string' || !['navigation', 'calendar', 'items'].includes(args.view))
|
|
359
|
+
return { code: 'INVALID_ARGUMENT', message: 'pa-workspace requires navigation, calendar or items view' };
|
|
360
|
+
if (args?.view !== 'items' && ['path', 'offset', 'limit', 'indexRevision'].some(key => args?.[key] !== undefined)) {
|
|
361
|
+
return { code: 'INVALID_ARGUMENT', message: 'items arguments require items view' };
|
|
362
|
+
}
|
|
363
|
+
if (args?.view === 'items') {
|
|
364
|
+
try {
|
|
365
|
+
validatePaWorkspaceItemsArgs(args);
|
|
366
|
+
}
|
|
367
|
+
catch (error) {
|
|
368
|
+
return { code: 'INVALID_ARGUMENT', message: error instanceof Error ? error.message : 'Invalid items arguments' };
|
|
369
|
+
}
|
|
370
|
+
}
|
|
359
371
|
if (args?.view !== 'calendar' && (args?.from !== undefined || args?.to !== undefined || args?.timezone !== undefined)) {
|
|
360
372
|
return { code: 'INVALID_ARGUMENT', message: 'from, to and timezone are only valid for calendar view' };
|
|
361
373
|
}
|
|
@@ -466,6 +478,28 @@ export function validateMenuRequest(request) {
|
|
|
466
478
|
return { code: 'INVALID_ARGUMENT', message: 'File fetch requires args.path' };
|
|
467
479
|
}
|
|
468
480
|
}
|
|
481
|
+
const isFileRequest = request.name === 'file' || firstCommandToken(request.cmd) === '/file';
|
|
482
|
+
if (type === 'menu.action' && isFileRequest
|
|
483
|
+
&& (request.action === 'move' || request.action === 'delete')) {
|
|
484
|
+
const args = request.args;
|
|
485
|
+
const allowed = request.action === 'move'
|
|
486
|
+
? ['path', 'destination', 'context']
|
|
487
|
+
: ['path', 'mtime', 'context'];
|
|
488
|
+
const unknown = Object.keys(args ?? {}).find(key => !allowed.includes(key));
|
|
489
|
+
if (unknown)
|
|
490
|
+
return { code: 'INVALID_ARGUMENT', message: `file.${request.action} does not accept args.${unknown}` };
|
|
491
|
+
if (typeof args?.path !== 'string' || !args.path.trim()) {
|
|
492
|
+
return { code: 'INVALID_ARGUMENT', message: `File ${request.action} requires args.path` };
|
|
493
|
+
}
|
|
494
|
+
if (request.action === 'move'
|
|
495
|
+
&& (typeof args.destination !== 'string' || !args.destination.trim())) {
|
|
496
|
+
return { code: 'INVALID_ARGUMENT', message: 'File move requires args.destination' };
|
|
497
|
+
}
|
|
498
|
+
if (request.action === 'delete'
|
|
499
|
+
&& (typeof args.mtime !== 'number' || !Number.isFinite(args.mtime) || args.mtime < 0)) {
|
|
500
|
+
return { code: 'INVALID_ARGUMENT', message: 'File delete requires a non-negative numeric args.mtime' };
|
|
501
|
+
}
|
|
502
|
+
}
|
|
469
503
|
if (type === 'menu.action' && (request.name === 'file' || request.cmd === '/file') && request.action === 'search') {
|
|
470
504
|
try {
|
|
471
505
|
parseFileSearchArgs(request.args);
|
|
@@ -487,17 +521,19 @@ export function validateTaskActivityQueryArgs(value) {
|
|
|
487
521
|
return { code: 'INVALID_ARGUMENT', message: 'task query requires args' };
|
|
488
522
|
}
|
|
489
523
|
const args = value;
|
|
490
|
-
if (args.view !== 'activities' && args.view !== 'activity') {
|
|
491
|
-
return { code: 'INVALID_ARGUMENT', message: 'task query requires args.view=activities or
|
|
524
|
+
if (args.view !== 'activities' && args.view !== 'activity' && args.view !== 'resource') {
|
|
525
|
+
return { code: 'INVALID_ARGUMENT', message: 'task query requires args.view=activities, activity or resource' };
|
|
492
526
|
}
|
|
493
|
-
const allowed = args.view === 'activities' ? ['view', 'taskId', 'offset', 'limit']
|
|
527
|
+
const allowed = args.view === 'activities' ? ['view', 'taskId', 'offset', 'limit']
|
|
528
|
+
: args.view === 'resource' ? ['view', 'taskId', 'activityId', 'resourceId', 'offset']
|
|
529
|
+
: ['view', 'taskId', 'activityId'];
|
|
494
530
|
if (Object.keys(args).some(key => !allowed.includes(key))) {
|
|
495
531
|
return { code: 'INVALID_ARGUMENT', message: 'unsupported task query argument' };
|
|
496
532
|
}
|
|
497
533
|
if (typeof args.taskId !== 'string' || !/^task-[A-Za-z0-9._:-]+$/.test(args.taskId)) {
|
|
498
534
|
return { code: 'INVALID_ARGUMENT', message: 'invalid taskId' };
|
|
499
535
|
}
|
|
500
|
-
if (args.view === 'activity') {
|
|
536
|
+
if (args.view === 'activity' || args.view === 'resource') {
|
|
501
537
|
if (typeof args.activityId !== 'string' || !/^activity-[A-Za-z0-9._:-]+$/.test(args.activityId)) {
|
|
502
538
|
return { code: 'INVALID_ARGUMENT', message: 'invalid activityId' };
|
|
503
539
|
}
|
|
@@ -506,6 +542,14 @@ export function validateTaskActivityQueryArgs(value) {
|
|
|
506
542
|
|| (args.limit !== undefined && (!Number.isSafeInteger(args.limit) || args.limit < 1 || args.limit > 100))) {
|
|
507
543
|
return { code: 'INVALID_ARGUMENT', message: 'offset must be a non-negative integer and limit must be between 1 and 100' };
|
|
508
544
|
}
|
|
545
|
+
if (args.view === 'resource') {
|
|
546
|
+
if (typeof args.resourceId !== 'string' || !/^resource-[A-Za-z0-9._:-]+$/.test(args.resourceId)) {
|
|
547
|
+
return { code: 'INVALID_ARGUMENT', message: 'invalid resourceId' };
|
|
548
|
+
}
|
|
549
|
+
if (args.offset !== undefined && (!Number.isSafeInteger(args.offset) || args.offset < 0)) {
|
|
550
|
+
return { code: 'INVALID_ARGUMENT', message: 'resource offset must be a non-negative integer' };
|
|
551
|
+
}
|
|
552
|
+
}
|
|
509
553
|
return null;
|
|
510
554
|
}
|
|
511
555
|
export function menuSuccess(header, data) {
|
|
@@ -560,12 +604,14 @@ const STABLE_CODES = new Set([
|
|
|
560
604
|
'INVALID_ARGUMENT', 'NOT_ALLOWED', 'NOT_SUPPORTED', 'UNSUPPORTED',
|
|
561
605
|
'METHOD_NOT_FOUND', 'NOT_FOUND', 'CONFLICT', 'EXPIRED', 'EXECUTION_TIMEOUT',
|
|
562
606
|
'TEMPORARILY_UNAVAILABLE', 'INTERNAL_ERROR', 'MISSING_SCOPE', 'UPGRADE_REQUIRED',
|
|
607
|
+
'NO_ACTIVE_UPDATE',
|
|
563
608
|
'MENU_TOKEN_REQUIRED', 'MENU_TOKEN_REJECTED', 'MENU_TOKEN_ENCRYPTION_REQUIRED',
|
|
564
609
|
'INVALID_CONTEXT', 'SCHEMA_VERSION_UNSUPPORTED', 'DEPENDENCY_UNAVAILABLE',
|
|
565
610
|
'RATE_LIMITED', 'CATALOG_STALE', 'DATA_MIGRATION_REQUIRED',
|
|
566
611
|
'INVALID_CLIENT', 'REMOTE_ACCESS_TOKEN_INVALID', 'ECWEB_TOKEN_INVALID', 'CLIENT_BUSY', 'INVALID_RESPONSE', 'TIMEOUT',
|
|
567
612
|
'RESPONSE_TOO_LARGE',
|
|
568
613
|
'ACTIVITY_NOT_FOUND', 'ACTIVITY_EXPIRED',
|
|
614
|
+
'RESOURCE_NOT_FOUND', 'RESOURCE_EXPIRED', 'RESOURCE_CORRUPT', 'INVALID_OFFSET',
|
|
569
615
|
'TOPIC_SESSION_NOT_FOUND', 'TOPIC_SESSION_LOOKUP_FAILED',
|
|
570
616
|
'LOOP_TRIGGER_NOT_FOUND', 'LOOP_TRIGGER_DISABLED', 'LOOP_TRIGGER_INVALID',
|
|
571
617
|
'ROOT_NOT_FOUND', 'ROOT_FORBIDDEN', 'ROOT_EXPIRED', 'ROOT_SCOPE_MISMATCH', 'ROOT_PEER_MISMATCH',
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import fs from 'node:fs';
|
|
2
2
|
import path from 'node:path';
|
|
3
|
-
import { DataRootError } from './
|
|
4
|
-
import { isPaHiddenPath, paWorkspaceStore } from './pa-workspace-store.js';
|
|
3
|
+
import { DataRootError, isPaHiddenPath, paWorkspaceStore } from './pa-workspace-store.js';
|
|
5
4
|
const MAX_RANGE_DAYS = 62;
|
|
6
5
|
const MAX_FILES = 500;
|
|
7
6
|
const MAX_RUNS_PER_TRIGGER = 200;
|
|
@@ -0,0 +1,486 @@
|
|
|
1
|
+
import crypto from 'node:crypto';
|
|
2
|
+
import fs from 'node:fs';
|
|
3
|
+
import path from 'node:path';
|
|
4
|
+
import { watch } from 'chokidar';
|
|
5
|
+
import { parseDocument } from 'yaml';
|
|
6
|
+
import { agentDataDir } from '../../paths.js';
|
|
7
|
+
import { atomicWriteText } from '../../utils/atomic-write.js';
|
|
8
|
+
import { DataRootError, isPaHiddenPath, paWorkspaceStore } from './pa-workspace-store.js';
|
|
9
|
+
const MAX_FILES = 10_000;
|
|
10
|
+
const MAX_VISITED = 20_000;
|
|
11
|
+
const MAX_PREVIEW_BYTES = 4096;
|
|
12
|
+
const MAX_RESPONSE_BYTES = 48 * 1024;
|
|
13
|
+
const HEAD_BYTES = 16 * 1024;
|
|
14
|
+
const TAIL_BYTES = 2048;
|
|
15
|
+
const RECONCILE_MS = 60_000;
|
|
16
|
+
const IDLE_MS = 10 * 60_000;
|
|
17
|
+
const FIELDS = ['title', 'summary', 'type', 'id', 'status', 'horizon', 'priority', 'date',
|
|
18
|
+
'period', 'report_type', 'due_at', 'completed_at', 'owner_note', 'source', 'stale_checks',
|
|
19
|
+
'memory_kind', 'superseded_by', 'url', 'peer', 'thread_id', 'count', 'tags'];
|
|
20
|
+
export function validatePaWorkspaceItemsArgs(args) {
|
|
21
|
+
if (typeof args.path !== 'string' || !args.path || args.path.length > 2048
|
|
22
|
+
|| args.path.includes('\\') || args.path.includes(':') || /[\x00-\x1f\x7f]/.test(args.path)
|
|
23
|
+
|| args.path.split('/').some(part => !part || part.startsWith('.')) || isPaHiddenPath(args.path)) {
|
|
24
|
+
throw new DataRootError('INVALID_ARGUMENT', 'items requires a user-facing relative path');
|
|
25
|
+
}
|
|
26
|
+
for (const [key, min, max] of [['offset', 0, MAX_FILES], ['limit', 1, 20]]) {
|
|
27
|
+
if (args[key] !== undefined && (!Number.isSafeInteger(args[key]) || Number(args[key]) < min || Number(args[key]) > max)) {
|
|
28
|
+
throw new DataRootError('INVALID_ARGUMENT', `items ${key} is out of range`);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
if (args.indexRevision !== undefined && (typeof args.indexRevision !== 'string'
|
|
32
|
+
|| !/^[a-f0-9]{24}$/.test(args.indexRevision))) {
|
|
33
|
+
throw new DataRootError('INVALID_ARGUMENT', 'items indexRevision is invalid');
|
|
34
|
+
}
|
|
35
|
+
if (Number(args.offset ?? 0) > 0 && args.indexRevision === undefined) {
|
|
36
|
+
throw new DataRootError('INVALID_ARGUMENT', 'items pagination requires indexRevision');
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
function signature(stat) {
|
|
40
|
+
return `${stat.dev}:${stat.ino}:${stat.size}:${stat.mtimeMs}:${stat.ctimeMs}`;
|
|
41
|
+
}
|
|
42
|
+
function within(parent, child) {
|
|
43
|
+
return child === parent || child.startsWith(`${parent}/`);
|
|
44
|
+
}
|
|
45
|
+
function published(state, relative) {
|
|
46
|
+
return relative.length <= 2048 && !/[\\:\x00-\x1f\x7f]/.test(relative)
|
|
47
|
+
&& !isPaHiddenPath(relative) && state.entries.some(entry => entry.availability !== 'unavailable'
|
|
48
|
+
&& (entry.path === relative || entry.kind === 'directory' && within(entry.path, relative)));
|
|
49
|
+
}
|
|
50
|
+
function clip(value, bytes) {
|
|
51
|
+
const buffer = Buffer.from(value);
|
|
52
|
+
if (buffer.length <= bytes)
|
|
53
|
+
return value;
|
|
54
|
+
return new TextDecoder('utf-8', { fatal: false }).decode(buffer.subarray(0, bytes)).replace(/\uFFFD$/, '');
|
|
55
|
+
}
|
|
56
|
+
/** Use existing App YAML parsers; do not introduce a second set of card models. */
|
|
57
|
+
async function preview(realPath, size) {
|
|
58
|
+
const file = await fs.promises.open(realPath, 'r');
|
|
59
|
+
try {
|
|
60
|
+
const head = Buffer.alloc(Math.min(size, HEAD_BYTES));
|
|
61
|
+
const { bytesRead } = await file.read(head, 0, head.length, 0);
|
|
62
|
+
let body = head.subarray(0, bytesRead).toString('utf8').replace(/^\uFEFF/, '');
|
|
63
|
+
const fields = {};
|
|
64
|
+
if (/^---[ \t]*\r?\n/.test(body)) {
|
|
65
|
+
const header = /^---[ \t]*\r?\n([\s\S]*?)^(?:---|\.\.\.)[ \t]*(?:\r?\n|$)/m.exec(body);
|
|
66
|
+
if (!header)
|
|
67
|
+
throw new Error('Incomplete frontmatter');
|
|
68
|
+
body = body.slice(header[0].length);
|
|
69
|
+
const doc = parseDocument(header[1], { strict: true, uniqueKeys: true });
|
|
70
|
+
if (doc.errors.length)
|
|
71
|
+
throw new Error('Invalid frontmatter');
|
|
72
|
+
const data = doc.toJS({ maxAliasCount: 0 });
|
|
73
|
+
if (data && typeof data === 'object' && !Array.isArray(data)) {
|
|
74
|
+
for (const key of FIELDS) {
|
|
75
|
+
const value = data[key];
|
|
76
|
+
if (typeof value === 'string')
|
|
77
|
+
fields[key] = clip(value, key === 'summary' ? 768 : 256);
|
|
78
|
+
else if (typeof value === 'number' && Number.isFinite(value))
|
|
79
|
+
fields[key] = value;
|
|
80
|
+
else if (key === 'tags' && Array.isArray(value))
|
|
81
|
+
fields[key] = value.filter(v => typeof v === 'string').slice(0, 12).map(v => clip(v, 64));
|
|
82
|
+
if (Buffer.byteLength(JSON.stringify(fields)) > 2400)
|
|
83
|
+
delete fields[key];
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
// Preserve a real heading and body prefix for legacy documents. Transcripts
|
|
88
|
+
// use a real final message marker when one fits in the bounded tail read.
|
|
89
|
+
let snippet = clip(body, 1400);
|
|
90
|
+
if (path.basename(realPath).toLowerCase() === 'transcript.md' && size > HEAD_BYTES) {
|
|
91
|
+
const tail = Buffer.alloc(Math.min(size, TAIL_BYTES));
|
|
92
|
+
const end = await file.read(tail, 0, tail.length, size - tail.length);
|
|
93
|
+
const text = tail.subarray(0, end.bytesRead).toString('utf8');
|
|
94
|
+
const markers = [...text.matchAll(/^\*\*([^\n]+?)\*\*[ \t]*[←→][ \t]*`[^`]*`[ \t]*\r?$/gm)];
|
|
95
|
+
const last = markers.at(-1);
|
|
96
|
+
const heading = /^#[ \t]+.*$/m.exec(body)?.[0] ?? '';
|
|
97
|
+
snippet = last ? `${heading}\n\n${clip(text.slice(last.index), 1400)}` : heading;
|
|
98
|
+
}
|
|
99
|
+
return clip(`---\n${JSON.stringify(fields)}\n---\n${snippet}`, MAX_PREVIEW_BYTES);
|
|
100
|
+
}
|
|
101
|
+
finally {
|
|
102
|
+
await file.close();
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
/** Derived private cache. No Skill files, workspace content or directory layouts are modified. */
|
|
106
|
+
export class PaWorkspaceItemsIndex {
|
|
107
|
+
store;
|
|
108
|
+
indexes = new Map();
|
|
109
|
+
constructor(store = paWorkspaceStore) {
|
|
110
|
+
this.store = store;
|
|
111
|
+
}
|
|
112
|
+
warm(paAid, state) {
|
|
113
|
+
// Navigation must not wait for cache JSON parsing or filesystem discovery.
|
|
114
|
+
setImmediate(() => {
|
|
115
|
+
try {
|
|
116
|
+
this.assertCurrent(paAid, state);
|
|
117
|
+
const index = this.get(paAid, state);
|
|
118
|
+
void this.refresh(index).then(async () => {
|
|
119
|
+
// Only the first page of each entry needs to be ready before a click.
|
|
120
|
+
const firstPages = state.entries.flatMap(entry => index.sorted.filter(item => within(entry.path, item.path)).slice(0, 20));
|
|
121
|
+
if (await this.prepare(index, firstPages))
|
|
122
|
+
this.persist(index);
|
|
123
|
+
}).catch(() => { index.rescan = true; });
|
|
124
|
+
}
|
|
125
|
+
catch { /* Binding may have changed since the navigation response. */ }
|
|
126
|
+
}).unref();
|
|
127
|
+
}
|
|
128
|
+
assertCurrent(paAid, state) {
|
|
129
|
+
const current = this.store.require(paAid);
|
|
130
|
+
if (current.workspaceId !== state.workspaceId || current.ownerAid !== state.ownerAid
|
|
131
|
+
|| current.rootPath !== state.rootPath || current.navigationRevision !== state.navigationRevision) {
|
|
132
|
+
throw new DataRootError('WORKSPACE_CHANGED', 'PA workspace changed during query');
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
async query(paAid, state, args) {
|
|
136
|
+
validatePaWorkspaceItemsArgs(args);
|
|
137
|
+
this.assertCurrent(paAid, state);
|
|
138
|
+
const requestedPath = args.path;
|
|
139
|
+
if (!published(state, requestedPath))
|
|
140
|
+
throw new DataRootError('FORBIDDEN', 'Path is not published in PA navigation');
|
|
141
|
+
this.store.resolvePath(state, requestedPath);
|
|
142
|
+
const index = this.get(paAid, state);
|
|
143
|
+
await this.refresh(index);
|
|
144
|
+
this.assertCurrent(paAid, state);
|
|
145
|
+
if (args.indexRevision !== undefined && args.indexRevision !== index.revision) {
|
|
146
|
+
throw new DataRootError('INDEX_CHANGED', 'PA items changed; restart pagination');
|
|
147
|
+
}
|
|
148
|
+
const snapshotRevision = index.revision;
|
|
149
|
+
const matching = index.sorted.filter(item => within(requestedPath, item.path));
|
|
150
|
+
const offset = Number(args.offset ?? 0);
|
|
151
|
+
const limit = Number(args.limit ?? 20);
|
|
152
|
+
const window = matching.slice(offset, offset + limit);
|
|
153
|
+
const parsed = await this.prepare(index, window);
|
|
154
|
+
this.assertCurrent(paAid, state);
|
|
155
|
+
if (index.closed || snapshotRevision !== index.revision) {
|
|
156
|
+
throw new DataRootError('INDEX_CHANGED', 'PA items changed; restart pagination');
|
|
157
|
+
}
|
|
158
|
+
// Revalidate only the returned page, including canonical paths, before any
|
|
159
|
+
// cached private text leaves Core. A missed watcher event cannot leak it.
|
|
160
|
+
for (const item of window) {
|
|
161
|
+
try {
|
|
162
|
+
const resolved = this.store.resolvePath(state, item.path);
|
|
163
|
+
if (!resolved.stat.isFile() || resolved.realPath !== item.realPath || signature(resolved.stat) !== item.signature)
|
|
164
|
+
throw new Error('Changed');
|
|
165
|
+
}
|
|
166
|
+
catch {
|
|
167
|
+
index.dirty.add(item.path);
|
|
168
|
+
await this.refresh(index);
|
|
169
|
+
throw new DataRootError('INDEX_CHANGED', 'PA items changed; restart pagination');
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
if (offset > matching.length)
|
|
173
|
+
throw new DataRootError('INVALID_ARGUMENT', 'items offset exceeds total');
|
|
174
|
+
if (parsed)
|
|
175
|
+
this.persist(index);
|
|
176
|
+
const next = offset + window.length;
|
|
177
|
+
const response = {
|
|
178
|
+
protocol: 'evol.pa-workspace', protocolVersion: 1, view: 'items',
|
|
179
|
+
workspaceId: state.workspaceId, navigationRevision: state.navigationRevision,
|
|
180
|
+
indexRevision: index.revision, path: requestedPath, offset,
|
|
181
|
+
total: matching.length, nextOffset: next < matching.length ? next : null,
|
|
182
|
+
hasMore: next < matching.length, truncated: index.truncated,
|
|
183
|
+
items: window.map(({ signature: _signature, realPath: _realPath, previewLoaded: _loaded, ...item }) => item),
|
|
184
|
+
};
|
|
185
|
+
// JSON escaping and long logical paths also consume transport space. A page
|
|
186
|
+
// can contain fewer than limit; the cursor always reflects actual entries.
|
|
187
|
+
while (response.items.length > 1 && Buffer.byteLength(JSON.stringify(response)) > MAX_RESPONSE_BYTES) {
|
|
188
|
+
response.items.pop();
|
|
189
|
+
response.nextOffset = offset + response.items.length;
|
|
190
|
+
response.hasMore = true;
|
|
191
|
+
}
|
|
192
|
+
return response;
|
|
193
|
+
}
|
|
194
|
+
get(paAid, state) {
|
|
195
|
+
const key = crypto.createHash('sha256').update(JSON.stringify([paAid, state.ownerAid,
|
|
196
|
+
state.workspaceId, this.store.root(state), state.navigationRevision, state.entries])).digest('hex');
|
|
197
|
+
let index = this.indexes.get(paAid);
|
|
198
|
+
if (!index || index.key !== key) {
|
|
199
|
+
if (index)
|
|
200
|
+
this.release(index);
|
|
201
|
+
index = { key, paAid, state, revision: crypto.randomBytes(12).toString('hex'), items: new Map(),
|
|
202
|
+
sorted: [], truncated: false, scannedAt: 0, dirty: new Set(), rescan: true, closed: false,
|
|
203
|
+
previewRequests: new Map(), previewQueue: [], activePreviews: 0 };
|
|
204
|
+
this.indexes.set(paAid, index);
|
|
205
|
+
// Disk is only a parse memo. Always reconcile directories before serving
|
|
206
|
+
// it after restart; navigationRevision does not version file contents.
|
|
207
|
+
try {
|
|
208
|
+
const cachePath = this.cachePath(index);
|
|
209
|
+
if (fs.statSync(cachePath).size <= 48 * 1024 * 1024) {
|
|
210
|
+
const saved = JSON.parse(fs.readFileSync(cachePath, 'utf8'));
|
|
211
|
+
if (saved.version === 1 && saved.key === key && Array.isArray(saved.items)) {
|
|
212
|
+
for (const item of saved.items.slice(0, MAX_FILES)) {
|
|
213
|
+
if (item && typeof item.path === 'string' && published(state, item.path)
|
|
214
|
+
&& typeof item.signature === 'string' && typeof item.realPath === 'string'
|
|
215
|
+
&& typeof item.previewLoaded === 'boolean'
|
|
216
|
+
&& item.type === 'file' && item.name === path.posix.basename(item.path)
|
|
217
|
+
&& Number.isSafeInteger(item.size) && item.size >= 0 && Number.isSafeInteger(item.mtime)
|
|
218
|
+
&& ['ready', 'unavailable', 'unsupported'].includes(item.previewStatus)
|
|
219
|
+
&& (item.previewStatus !== 'ready' || typeof item.previewSource === 'string')
|
|
220
|
+
&& (item.previewSource === undefined || typeof item.previewSource === 'string' && Buffer.byteLength(item.previewSource) <= MAX_PREVIEW_BYTES)) {
|
|
221
|
+
index.items.set(item.path, item);
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
catch { /* A derived cache is disposable. */ }
|
|
228
|
+
this.observe(index);
|
|
229
|
+
}
|
|
230
|
+
clearTimeout(index.idle);
|
|
231
|
+
index.idle = setTimeout(() => {
|
|
232
|
+
this.release(index);
|
|
233
|
+
if (this.indexes.get(paAid) === index)
|
|
234
|
+
this.indexes.delete(paAid);
|
|
235
|
+
}, IDLE_MS);
|
|
236
|
+
index.idle.unref();
|
|
237
|
+
// Bound watcher count even on a host with many PAs.
|
|
238
|
+
if (this.indexes.size > 16) {
|
|
239
|
+
const oldest = [...this.indexes.values()].find(candidate => candidate !== index && !candidate.pending);
|
|
240
|
+
if (oldest) {
|
|
241
|
+
this.release(oldest);
|
|
242
|
+
this.indexes.delete(oldest.paAid);
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
return index;
|
|
246
|
+
}
|
|
247
|
+
cachePath(index) { return path.join(agentDataDir(index.paAid), 'pa-workspace-items-v1.json'); }
|
|
248
|
+
persist(index) {
|
|
249
|
+
if (index.closed || this.indexes.get(index.paAid) !== index)
|
|
250
|
+
return;
|
|
251
|
+
try {
|
|
252
|
+
atomicWriteText(this.cachePath(index), JSON.stringify({ version: 1, key: index.key, items: index.sorted }));
|
|
253
|
+
}
|
|
254
|
+
catch { /* The disposable disk cache must not prevent in-memory reads. */ }
|
|
255
|
+
}
|
|
256
|
+
async prepare(index, items) {
|
|
257
|
+
const needed = items.filter(item => !item.previewLoaded);
|
|
258
|
+
await Promise.all(needed.map(item => {
|
|
259
|
+
const key = `${item.path}:${item.signature}`;
|
|
260
|
+
const existing = index.previewRequests.get(key);
|
|
261
|
+
if (existing)
|
|
262
|
+
return existing;
|
|
263
|
+
const request = new Promise((resolve, reject) => {
|
|
264
|
+
index.previewQueue.push(() => {
|
|
265
|
+
index.activePreviews++;
|
|
266
|
+
void (async () => {
|
|
267
|
+
if (index.closed)
|
|
268
|
+
throw new DataRootError('WORKSPACE_CHANGED', 'PA workspace index was replaced');
|
|
269
|
+
let resolved;
|
|
270
|
+
try {
|
|
271
|
+
resolved = this.store.resolvePath(index.state, item.path);
|
|
272
|
+
}
|
|
273
|
+
catch {
|
|
274
|
+
index.dirty.add(item.path);
|
|
275
|
+
throw new DataRootError('INDEX_CHANGED', 'PA file changed during preview read');
|
|
276
|
+
}
|
|
277
|
+
if (resolved.realPath !== item.realPath || signature(resolved.stat) !== item.signature) {
|
|
278
|
+
index.dirty.add(item.path);
|
|
279
|
+
throw new DataRootError('INDEX_CHANGED', 'PA file changed during preview read');
|
|
280
|
+
}
|
|
281
|
+
try {
|
|
282
|
+
item.previewSource = await preview(resolved.realPath, item.size);
|
|
283
|
+
item.previewStatus = 'ready';
|
|
284
|
+
}
|
|
285
|
+
catch {
|
|
286
|
+
item.previewStatus = 'unavailable';
|
|
287
|
+
}
|
|
288
|
+
item.previewLoaded = true;
|
|
289
|
+
})().then(resolve, reject).finally(() => {
|
|
290
|
+
index.activePreviews--;
|
|
291
|
+
index.previewRequests.delete(key);
|
|
292
|
+
this.pumpPreviews(index);
|
|
293
|
+
});
|
|
294
|
+
});
|
|
295
|
+
});
|
|
296
|
+
index.previewRequests.set(key, request);
|
|
297
|
+
this.pumpPreviews(index);
|
|
298
|
+
return request;
|
|
299
|
+
}));
|
|
300
|
+
return needed.length > 0;
|
|
301
|
+
}
|
|
302
|
+
pumpPreviews(index) {
|
|
303
|
+
while (index.activePreviews < 8 && index.previewQueue.length)
|
|
304
|
+
index.previewQueue.shift()();
|
|
305
|
+
}
|
|
306
|
+
observe(index) {
|
|
307
|
+
const root = this.store.root(index.state);
|
|
308
|
+
const allowed = index.state.entries.filter(entry => entry.availability !== 'unavailable');
|
|
309
|
+
const watcher = watch(root, {
|
|
310
|
+
persistent: false, ignoreInitial: true, followSymlinks: false,
|
|
311
|
+
ignored: absolute => {
|
|
312
|
+
const relative = path.relative(root, absolute).split(path.sep).join('/');
|
|
313
|
+
return relative !== '' && (isPaHiddenPath(relative) || !allowed.some(entry => within(relative, entry.path) || entry.kind === 'directory' && within(entry.path, relative)));
|
|
314
|
+
},
|
|
315
|
+
});
|
|
316
|
+
index.watcher = watcher;
|
|
317
|
+
watcher.on('all', (event, absolute) => {
|
|
318
|
+
if (index.closed)
|
|
319
|
+
return;
|
|
320
|
+
const relative = path.relative(root, absolute).split(path.sep).join('/');
|
|
321
|
+
if (event === 'addDir' || event === 'unlinkDir')
|
|
322
|
+
index.rescan = true;
|
|
323
|
+
else if (published(index.state, relative))
|
|
324
|
+
index.dirty.add(relative);
|
|
325
|
+
clearTimeout(index.timer);
|
|
326
|
+
index.timer = setTimeout(() => { void this.refresh(index).catch(() => { index.rescan = true; }); }, 75);
|
|
327
|
+
index.timer.unref();
|
|
328
|
+
});
|
|
329
|
+
watcher.on('ready', () => { index.rescan = true; });
|
|
330
|
+
watcher.on('error', () => { index.rescan = true; });
|
|
331
|
+
}
|
|
332
|
+
refresh(index) {
|
|
333
|
+
try {
|
|
334
|
+
if (index.closed)
|
|
335
|
+
throw new DataRootError('WORKSPACE_CHANGED', 'PA workspace index was replaced');
|
|
336
|
+
this.assertCurrent(index.paAid, index.state);
|
|
337
|
+
}
|
|
338
|
+
catch (error) {
|
|
339
|
+
this.release(index);
|
|
340
|
+
if (this.indexes.get(index.paAid) === index)
|
|
341
|
+
this.indexes.delete(index.paAid);
|
|
342
|
+
return Promise.reject(error);
|
|
343
|
+
}
|
|
344
|
+
if (index.pending)
|
|
345
|
+
return index.pending;
|
|
346
|
+
if (!index.rescan && !index.dirty.size && Date.now() - index.scannedAt < RECONCILE_MS)
|
|
347
|
+
return Promise.resolve();
|
|
348
|
+
index.pending = (async () => {
|
|
349
|
+
// Changes observed during an async scan must not be cleared by that scan.
|
|
350
|
+
for (let pass = 0; pass < 3; pass++) {
|
|
351
|
+
await this.update(index);
|
|
352
|
+
if (!index.rescan && !index.dirty.size)
|
|
353
|
+
return;
|
|
354
|
+
}
|
|
355
|
+
throw new DataRootError('INDEX_CHANGED', 'PA items are changing; retry the first page');
|
|
356
|
+
})().catch(error => { index.rescan = true; throw error; })
|
|
357
|
+
.finally(() => { index.pending = undefined; });
|
|
358
|
+
return index.pending;
|
|
359
|
+
}
|
|
360
|
+
async read(index, relative) {
|
|
361
|
+
if (index.closed || !published(index.state, relative))
|
|
362
|
+
return undefined;
|
|
363
|
+
let resolved;
|
|
364
|
+
try {
|
|
365
|
+
resolved = this.store.resolvePath(index.state, relative);
|
|
366
|
+
}
|
|
367
|
+
catch (error) {
|
|
368
|
+
if (error instanceof DataRootError && ['NOT_FOUND', 'INVALID_ROOT_PATH'].includes(error.code))
|
|
369
|
+
return undefined;
|
|
370
|
+
throw error;
|
|
371
|
+
}
|
|
372
|
+
if (!resolved.stat.isFile())
|
|
373
|
+
return undefined;
|
|
374
|
+
const previous = index.items.get(relative);
|
|
375
|
+
const stamp = signature(resolved.stat);
|
|
376
|
+
if (previous?.signature === stamp && previous.realPath === resolved.realPath)
|
|
377
|
+
return previous;
|
|
378
|
+
const markdown = /\.(md|markdown)$/i.test(relative);
|
|
379
|
+
const item = { path: relative, name: path.posix.basename(relative), type: 'file',
|
|
380
|
+
size: resolved.stat.size, mtime: Math.floor(resolved.stat.mtimeMs), signature: stamp,
|
|
381
|
+
realPath: resolved.realPath, previewStatus: markdown ? 'unavailable' : 'unsupported', previewLoaded: !markdown };
|
|
382
|
+
return item;
|
|
383
|
+
}
|
|
384
|
+
async update(index) {
|
|
385
|
+
const full = index.rescan || Date.now() - index.scannedAt >= RECONCILE_MS;
|
|
386
|
+
index.rescan = false;
|
|
387
|
+
const dirty = [...index.dirty];
|
|
388
|
+
index.dirty.clear();
|
|
389
|
+
const next = full ? new Map() : new Map(index.items);
|
|
390
|
+
let truncated = full ? false : index.truncated;
|
|
391
|
+
const files = [];
|
|
392
|
+
if (full) {
|
|
393
|
+
const pending = index.state.entries.filter(entry => entry.availability !== 'unavailable').map(entry => entry.path);
|
|
394
|
+
const visited = new Set();
|
|
395
|
+
let count = 0;
|
|
396
|
+
while (pending.length && !index.closed) {
|
|
397
|
+
const relative = pending.pop();
|
|
398
|
+
if (++count > MAX_VISITED || files.length >= MAX_FILES) {
|
|
399
|
+
truncated = true;
|
|
400
|
+
break;
|
|
401
|
+
}
|
|
402
|
+
let resolved;
|
|
403
|
+
try {
|
|
404
|
+
resolved = this.store.resolvePath(index.state, relative);
|
|
405
|
+
}
|
|
406
|
+
catch (error) {
|
|
407
|
+
if (error instanceof DataRootError && ['NOT_FOUND', 'INVALID_ROOT_PATH'].includes(error.code))
|
|
408
|
+
continue;
|
|
409
|
+
throw error;
|
|
410
|
+
}
|
|
411
|
+
if (visited.has(resolved.realPath))
|
|
412
|
+
continue;
|
|
413
|
+
visited.add(resolved.realPath);
|
|
414
|
+
if (resolved.stat.isFile()) {
|
|
415
|
+
files.push(relative);
|
|
416
|
+
continue;
|
|
417
|
+
}
|
|
418
|
+
if (!resolved.stat.isDirectory())
|
|
419
|
+
continue;
|
|
420
|
+
const children = await fs.promises.readdir(resolved.realPath, { withFileTypes: true });
|
|
421
|
+
children.sort((a, b) => a.name < b.name ? 1 : a.name > b.name ? -1 : 0);
|
|
422
|
+
for (const child of children) {
|
|
423
|
+
const candidate = `${relative}/${child.name}`;
|
|
424
|
+
if (!published(index.state, candidate))
|
|
425
|
+
continue;
|
|
426
|
+
if (child.isFile()) {
|
|
427
|
+
if (files.length >= MAX_FILES) {
|
|
428
|
+
truncated = true;
|
|
429
|
+
break;
|
|
430
|
+
}
|
|
431
|
+
files.push(candidate);
|
|
432
|
+
}
|
|
433
|
+
else if (child.isDirectory())
|
|
434
|
+
pending.push(candidate);
|
|
435
|
+
}
|
|
436
|
+
}
|
|
437
|
+
}
|
|
438
|
+
else
|
|
439
|
+
files.push(...dirty);
|
|
440
|
+
let cursor = 0;
|
|
441
|
+
await Promise.all(Array.from({ length: Math.min(8, files.length) }, async () => {
|
|
442
|
+
while (cursor < files.length && !index.closed) {
|
|
443
|
+
const relative = files[cursor++];
|
|
444
|
+
const item = await this.read(index, relative);
|
|
445
|
+
if (item)
|
|
446
|
+
next.set(relative, item);
|
|
447
|
+
else
|
|
448
|
+
next.delete(relative);
|
|
449
|
+
}
|
|
450
|
+
}));
|
|
451
|
+
if (index.closed)
|
|
452
|
+
return;
|
|
453
|
+
if (next.size > MAX_FILES) {
|
|
454
|
+
truncated = true;
|
|
455
|
+
for (const relative of [...next.keys()].slice(MAX_FILES))
|
|
456
|
+
next.delete(relative);
|
|
457
|
+
}
|
|
458
|
+
const changed = next.size !== index.items.size || [...next].some(([relative, item]) => index.items.get(relative) !== item)
|
|
459
|
+
|| index.truncated !== truncated;
|
|
460
|
+
index.items = next;
|
|
461
|
+
index.sorted = [...next.values()].sort((a, b) => b.mtime - a.mtime || (a.path < b.path ? -1 : a.path > b.path ? 1 : 0));
|
|
462
|
+
index.truncated = truncated;
|
|
463
|
+
if (full)
|
|
464
|
+
index.scannedAt = Date.now();
|
|
465
|
+
if (changed)
|
|
466
|
+
index.revision = crypto.randomBytes(12).toString('hex');
|
|
467
|
+
if (changed || full)
|
|
468
|
+
this.persist(index);
|
|
469
|
+
}
|
|
470
|
+
release(index) {
|
|
471
|
+
index.closed = true;
|
|
472
|
+
clearTimeout(index.timer);
|
|
473
|
+
clearTimeout(index.idle);
|
|
474
|
+
void index.watcher?.close();
|
|
475
|
+
}
|
|
476
|
+
async close() {
|
|
477
|
+
const indexes = [...this.indexes.values()];
|
|
478
|
+
for (const index of indexes)
|
|
479
|
+
this.release(index);
|
|
480
|
+
this.indexes.clear();
|
|
481
|
+
await Promise.all(indexes.map(index => index.pending?.catch(() => { })));
|
|
482
|
+
await Promise.all(indexes.flatMap(index => [...index.previewRequests.values()].map(request => request.catch(() => { }))));
|
|
483
|
+
await Promise.all(indexes.map(index => index.watcher?.close()));
|
|
484
|
+
}
|
|
485
|
+
}
|
|
486
|
+
export const paWorkspaceItemsIndex = new PaWorkspaceItemsIndex();
|
|
@@ -4,7 +4,16 @@ import path from 'node:path';
|
|
|
4
4
|
import { agentDataDir } from '../../paths.js';
|
|
5
5
|
import { isValidAid } from '../../aun/aid/validation.js';
|
|
6
6
|
import { atomicReplaceFile } from '../../utils/atomic-write.js';
|
|
7
|
-
|
|
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
|
+
}
|
|
8
17
|
const STATE_FILE = 'pa-workspace-state.json';
|
|
9
18
|
const MAX_ENTRIES = 200;
|
|
10
19
|
const MAX_PATH_LENGTH = 2048;
|
|
@@ -43,7 +43,7 @@ export function probeSessionActivity(evidence, probes) {
|
|
|
43
43
|
return evaluateSessionActivity(collected);
|
|
44
44
|
}
|
|
45
45
|
// 支持的命令列表
|
|
46
|
-
const commands = ['/new', '/pwd', '/help', '/evolhelp', '/status', '/restart', '/reload', '/model', '/effort', '/baseagent', '/slist', '/session', '/rename', '/stop', '/pause', '/compact', '/renew', '/repair', '/fork', '/del', '/perm', '/file', '/check', '/rewind', '/activity', '/observable', '/chatmode', '/mention', '/loop', '/fa', '/fullaccess', '/ask', '/resume', '/aid', '/rpc', '/storage', '/agent', '/trigger', '/upgrade', '/pair', '/memory', '/skill', '/thread'];
|
|
46
|
+
const commands = ['/new', '/pwd', '/help', '/evolhelp', '/status', '/restart', '/reload', '/model', '/effort', '/baseagent', '/slist', '/session', '/rename', '/stop', '/pause', '/compact', '/renew', '/repair', '/fork', '/del', '/perm', '/file', '/check', '/rewind', '/activity', '/observable', '/chatmode', '/mention', '/loop', '/fa', '/fullaccess', '/ask', '/resume', '/aid', '/rpc', '/storage', '/agent', '/trigger', '/upgrade', '/update', '/pair', '/memory', '/skill', '/thread'];
|
|
47
47
|
const deprecatedCommands = ['/clear'];
|
|
48
48
|
commands.push('/diag');
|
|
49
49
|
const exactBoundaryCommands = new Set(['/pause', '/resume', '/stop', '/pair', '/diag']);
|
|
@@ -114,7 +114,7 @@ export function guardGroupSlashCommand(content, chatType) {
|
|
|
114
114
|
};
|
|
115
115
|
}
|
|
116
116
|
// 命令快速路径前缀(所有命令都不进入消息队列)
|
|
117
|
-
const quickCommandPrefixes = ['/new', '/pwd', '/help', '/evolhelp', '/status', '/restart', '/reload', '/model', '/effort', '/baseagent', '/slist', '/session', '/rename', '/repair', '/fork', '/stop', '/pause', '/clear', '/compact', '/renew', '/del', '/perm', '/file', '/check', '/s ', '/name', '/rewind', '/rw', '/rw ', '/activity', '/observable', '/chatmode', '/mention', '/loop', '/fa', '/fullaccess', '/ask', '/resume', '/base ', '/aid', '/rpc', '/storage', '/agent', '/trigger', '/upgrade', '/pair', '/memory', '/skill', '/thread'];
|
|
117
|
+
const quickCommandPrefixes = ['/new', '/pwd', '/help', '/evolhelp', '/status', '/restart', '/reload', '/model', '/effort', '/baseagent', '/slist', '/session', '/rename', '/repair', '/fork', '/stop', '/pause', '/clear', '/compact', '/renew', '/del', '/perm', '/file', '/check', '/s ', '/name', '/rewind', '/rw', '/rw ', '/activity', '/observable', '/chatmode', '/mention', '/loop', '/fa', '/fullaccess', '/ask', '/resume', '/base ', '/aid', '/rpc', '/storage', '/agent', '/trigger', '/upgrade', '/update', '/pair', '/memory', '/skill', '/thread'];
|
|
118
118
|
quickCommandPrefixes.push('/diag');
|
|
119
119
|
/**
|
|
120
120
|
* 计算两个字符串的 Levenshtein 距离(编辑距离)
|