nova-personal-agent-server 0.4.1 → 1.0.2
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/LICENSE +674 -201
- package/README.md +10 -0
- package/bin/nova-space-host.mjs +4 -0
- package/node_modules/@nova-personal-agent/relay/dist/src/cli.js +71 -0
- package/node_modules/@nova-personal-agent/relay/dist/src/client.js +340 -0
- package/node_modules/@nova-personal-agent/relay/dist/src/crypto.js +167 -0
- package/node_modules/@nova-personal-agent/relay/dist/src/live.js +169 -0
- package/node_modules/@nova-personal-agent/relay/dist/src/mailbox.js +217 -0
- package/node_modules/@nova-personal-agent/relay/dist/src/media.js +320 -0
- package/node_modules/@nova-personal-agent/relay/dist/src/pairing.js +13 -0
- package/node_modules/@nova-personal-agent/relay/dist/src/protocol.js +50 -0
- package/node_modules/@nova-personal-agent/relay/dist/src/security.js +305 -0
- package/node_modules/@nova-personal-agent/relay/dist/src/server.js +477 -0
- package/node_modules/@nova-personal-agent/relay/dist/src/source-address.js +58 -0
- package/node_modules/@nova-personal-agent/relay/package.json +1 -0
- package/package.json +11 -4
- package/runtime/dist/src/cascaded-text-provider.js +8 -3
- package/runtime/dist/src/composition/assembly.js +133 -35
- package/runtime/dist/src/composition/cascaded-realtime-assembly.js +114 -59
- package/runtime/dist/src/composition/production-composition.js +57 -21
- package/runtime/dist/src/composition/realtime-assembly.js +160 -52
- package/runtime/dist/src/composition/soul-context.js +22 -0
- package/runtime/dist/src/config/capability-registry.js +48 -3
- package/runtime/dist/src/config/cascaded-realtime-config.js +23 -10
- package/runtime/dist/src/config/config.js +541 -128
- package/runtime/dist/src/config/credential-rotation.js +26 -0
- package/runtime/dist/src/config/data-root.js +764 -0
- package/runtime/dist/src/config/diagnostics.js +3 -1
- package/runtime/dist/src/config/environment-contract.js +190 -188
- package/runtime/dist/src/config/model-routes.js +140 -0
- package/runtime/dist/src/config/voice-credentials.js +55 -0
- package/runtime/dist/src/connectors/composio/index.js +9 -0
- package/runtime/dist/src/connectors/feishu/index.js +258 -44
- package/runtime/dist/src/connectors/macos/mail.js +7 -3
- package/runtime/dist/src/connectors/macos/reader.js +3 -1
- package/runtime/dist/src/conversation-voice-provider.js +5 -2
- package/runtime/dist/src/core/causal-runtime.js +38 -3
- package/runtime/dist/src/core/context-view.js +7 -0
- package/runtime/dist/src/core/guards.js +32 -0
- package/runtime/dist/src/core/runtime.js +3 -0
- package/runtime/dist/src/core/schedule-tools.js +2 -2
- package/runtime/dist/src/core/source-processing-instructions.js +2 -2
- package/runtime/dist/src/core/source-processing-tools.js +3 -1
- package/runtime/dist/src/core/tool-schema.js +42 -7
- package/runtime/dist/src/desktop/desktop-camera.js +3 -5
- package/runtime/dist/src/desktop/desktop-control.js +41 -8
- package/runtime/dist/src/desktop/desktop-session.js +90 -19
- package/runtime/dist/src/desktop/desktop-wire.js +5 -7
- package/runtime/dist/src/desktop/startup-error.js +18 -16
- package/runtime/dist/src/desktop/workbench-acceptance.js +52 -14
- package/runtime/dist/src/desktop-entry.js +6 -2
- package/runtime/dist/src/desktop.js +5 -1
- package/runtime/dist/src/device/attempt.js +65 -0
- package/runtime/dist/src/device/authority.js +159 -0
- package/runtime/dist/src/device/gateway.js +87 -0
- package/runtime/dist/src/device/ledger.js +91 -0
- package/runtime/dist/src/device/ownership.js +92 -0
- package/runtime/dist/src/device/port.js +2 -0
- package/runtime/dist/src/device/schedule-ownership.js +103 -0
- package/runtime/dist/src/executors/acp/transport.js +2 -0
- package/runtime/dist/src/executors/browser.js +4 -3
- package/runtime/dist/src/executors/codex/adapter-project.js +58 -12
- package/runtime/dist/src/executors/codex/app-server-schema.js +12 -21
- package/runtime/dist/src/executors/codex/app-server-transport.js +32 -18
- package/runtime/dist/src/executors/codex/common.js +1 -1
- package/runtime/dist/src/executors/codex/contract.js +12 -16
- package/runtime/dist/src/executors/codex/credential-snapshot.js +1 -9
- package/runtime/dist/src/executors/codex/factory.js +54 -32
- package/runtime/dist/src/executors/codex/host-config.js +69 -20
- package/runtime/dist/src/executors/codex/launch-profile.js +3 -2
- package/runtime/dist/src/executors/codex/local-provider-credential.js +39 -0
- package/runtime/dist/src/executors/codex/process-owner.js +4 -6
- package/runtime/dist/src/executors/codex/production-host.js +33 -34
- package/runtime/dist/src/executors/codex/protocol.js +9 -14
- package/runtime/dist/src/executors/codex/session-title.js +6 -8
- package/runtime/dist/src/executors/codex/spawn-env.js +27 -6
- package/runtime/dist/src/executors/codex/turn-projection.js +12 -17
- package/runtime/dist/src/executors/codex/windows-guardian.js +4 -6
- package/runtime/dist/src/executors/coding/intake.js +12 -7
- package/runtime/dist/src/executors/computer-backend.js +18 -10
- package/runtime/dist/src/executors/computer-config.js +32 -0
- package/runtime/dist/src/executors/computer-device.js +68 -16
- package/runtime/dist/src/executors/computer-worker.js +18 -6
- package/runtime/dist/src/executors/computer.js +29 -56
- package/runtime/dist/src/executors/device-lock.js +14 -5
- package/runtime/dist/src/executors/gui-execution.js +2 -0
- package/runtime/dist/src/executors/index.js +0 -1
- package/runtime/dist/src/executors/mobile.js +9 -8
- package/runtime/dist/src/executors/native-process.js +4 -0
- package/runtime/dist/src/executors/search.js +3 -5
- package/runtime/dist/src/executors/task-resources.js +21 -0
- package/runtime/dist/src/executors/watcher.js +2 -4
- package/runtime/dist/src/experience/host.js +179 -0
- package/runtime/dist/src/index.js +2 -0
- package/runtime/dist/src/knowledge/assembly.js +3 -4
- package/runtime/dist/src/knowledge/embeddings.js +50 -34
- package/runtime/dist/src/knowledge/service.js +1 -0
- package/runtime/dist/src/knowledge/store-client.js +1 -6
- package/runtime/dist/src/knowledge/store-worker.js +1 -3
- package/runtime/dist/src/mem0/resource.js +25 -0
- package/runtime/dist/src/mem0/store-worker.js +234 -21
- package/runtime/dist/src/memory/consumer.js +31 -0
- package/runtime/dist/src/memory/engine-worker.js +210 -0
- package/runtime/dist/src/memory/entry.js +8 -1
- package/runtime/dist/src/memory/explicit-memory.js +20 -0
- package/runtime/dist/src/memory/factory.js +30 -15
- package/runtime/dist/src/memory/grounded-voicemem.js +10 -0
- package/runtime/dist/src/memory/ledger-recall-engine.js +86 -0
- package/runtime/dist/src/memory/prerecall-port.js +27 -0
- package/runtime/dist/src/memory/prerecall.js +1 -1
- package/runtime/dist/src/memory/production-engine.js +151 -0
- package/runtime/dist/src/memory/provenance.js +1 -0
- package/runtime/dist/src/memory/remote-personal-memory.js +3 -1
- package/runtime/dist/src/memory/retrieval.js +107 -22
- package/runtime/dist/src/memory/sensitivity.js +6 -2
- package/runtime/dist/src/memory/source-retrieval.js +11 -0
- package/runtime/dist/src/memory/tool-capabilities.js +46 -0
- package/runtime/dist/src/memory/voicemem-sdk/LICENSE +202 -0
- package/runtime/dist/src/memory/voicemem-sdk/NOTICE.md +97 -0
- package/runtime/dist/src/memory/voicemem-sdk/PATCH-NOTICE.md +23 -0
- package/runtime/dist/src/memory/voicemem-sdk/archive.js +12 -0
- package/runtime/dist/src/memory/voicemem-sdk/audio.js +111 -0
- package/runtime/dist/src/memory/voicemem-sdk/barge-in.js +59 -0
- package/runtime/dist/src/memory/voicemem-sdk/checkpoint.js +56 -0
- package/runtime/dist/src/memory/voicemem-sdk/cleanup.js +139 -0
- package/runtime/dist/src/memory/voicemem-sdk/dashscope-asr.js +254 -0
- package/runtime/dist/src/memory/voicemem-sdk/dashscope-tts.js +247 -0
- package/runtime/dist/src/memory/voicemem-sdk/evaluation.js +330 -0
- package/runtime/dist/src/memory/voicemem-sdk/extraction.js +220 -0
- package/runtime/dist/src/memory/voicemem-sdk/finetune-data.js +115 -0
- package/runtime/dist/src/memory/voicemem-sdk/finetune.js +170 -0
- package/runtime/dist/src/memory/voicemem-sdk/grounded-reply-preferences.js +81 -0
- package/runtime/dist/src/memory/voicemem-sdk/index.js +22 -0
- package/runtime/dist/src/memory/voicemem-sdk/local-embedding.js +164 -0
- package/runtime/dist/src/memory/voicemem-sdk/local-runtime.js +53 -0
- package/runtime/dist/src/memory/voicemem-sdk/local-speaker.js +240 -0
- package/runtime/dist/src/memory/voicemem-sdk/memory.js +767 -0
- package/runtime/dist/src/memory/voicemem-sdk/model-json.js +14 -0
- package/runtime/dist/src/memory/voicemem-sdk/personamem.js +209 -0
- package/runtime/dist/src/memory/voicemem-sdk/prompts/addenda.js +215 -0
- package/runtime/dist/src/memory/voicemem-sdk/prompts/additive-extraction.js +478 -0
- package/runtime/dist/src/memory/voicemem-sdk/prompts/index.js +32 -0
- package/runtime/dist/src/memory/voicemem-sdk/prompts/update-memory.js +152 -0
- package/runtime/dist/src/memory/voicemem-sdk/providers.js +325 -0
- package/runtime/dist/src/memory/voicemem-sdk/ranking.js +65 -0
- package/runtime/dist/src/memory/voicemem-sdk/reaction.js +33 -0
- package/runtime/dist/src/memory/voicemem-sdk/realtime.js +215 -0
- package/runtime/dist/src/memory/voicemem-sdk/reply.js +68 -0
- package/runtime/dist/src/memory/voicemem-sdk/routing.js +209 -0
- package/runtime/dist/src/memory/voicemem-sdk/slots.js +61 -0
- package/runtime/dist/src/memory/voicemem-sdk/speaker-bindings.js +120 -0
- package/runtime/dist/src/memory/voicemem-sdk/speaker-profiles.js +327 -0
- package/runtime/dist/src/memory/voicemem-sdk/store.js +577 -0
- package/runtime/dist/src/memory/voicemem-sdk/stream.js +317 -0
- package/runtime/dist/src/memory/voicemem-sdk/subgraph.js +113 -0
- package/runtime/dist/src/memory/voicemem-sdk/summaries.js +56 -0
- package/runtime/dist/src/memory/voicemem-sdk/time.js +157 -0
- package/runtime/dist/src/memory/voicemem-sdk/types.js +2 -0
- package/runtime/dist/src/memory-ledger/store-client.js +235 -37
- package/runtime/dist/src/memory-ledger/store-worker.js +8 -5
- package/runtime/dist/src/memory-ledger/store.js +91 -1
- package/runtime/dist/src/memory-substrate/consolidation.js +23 -7
- package/runtime/dist/src/memory-substrate/diary.js +27 -0
- package/runtime/dist/src/memory-substrate/engine-outbox.js +107 -0
- package/runtime/dist/src/memory-substrate/explicit-memory.js +112 -0
- package/runtime/dist/src/memory-substrate/fact-fidelity.js +68 -0
- package/runtime/dist/src/memory-substrate/feishu-consent.js +41 -0
- package/runtime/dist/src/memory-substrate/file-authority.js +29 -6
- package/runtime/dist/src/memory-substrate/life-operations.js +35 -9
- package/runtime/dist/src/memory-substrate/markdown-repository.js +271 -65
- package/runtime/dist/src/memory-substrate/memory-documents.js +96 -0
- package/runtime/dist/src/memory-substrate/migration.js +2 -0
- package/runtime/dist/src/memory-substrate/purge.js +12 -2
- package/runtime/dist/src/memory-substrate/resource.js +514 -129
- package/runtime/dist/src/memory-substrate/retrieval.js +8 -5
- package/runtime/dist/src/memory-substrate/source-operations.js +9 -1
- package/runtime/dist/src/memory-substrate/source-processing.js +436 -40
- package/runtime/dist/src/memory-substrate/source-retrieval.js +28 -0
- package/runtime/dist/src/memory-substrate/source-state.js +8 -0
- package/runtime/dist/src/memory-substrate/space-source-input.js +90 -0
- package/runtime/dist/src/memory-substrate/store.js +125 -23
- package/runtime/dist/src/model/goal-checkin-writer.js +72 -0
- package/runtime/dist/src/model/goal-writer.js +80 -0
- package/runtime/dist/src/model/jev-client.js +24 -13
- package/runtime/dist/src/model/model-adapters.js +2 -3
- package/runtime/dist/src/model/model-catalog.js +80 -0
- package/runtime/dist/src/model/model-gateway.js +47 -18
- package/runtime/dist/src/model/model-purpose.js +3 -4
- package/runtime/dist/src/model/personal-writer.js +536 -44
- package/runtime/dist/src/model/proactivity.js +50 -6
- package/runtime/dist/src/model/responses-model-gateway.js +59 -0
- package/runtime/dist/src/model/task-verifier.js +3 -3
- package/runtime/dist/src/model/timeline-anchors.js +77 -0
- package/runtime/dist/src/model/timeline-perspective.js +13 -0
- package/runtime/dist/src/model/timeline-selection.js +182 -0
- package/runtime/dist/src/model/understanding-writer.js +27 -0
- package/runtime/dist/src/model/vision-capability.js +3 -1
- package/runtime/dist/src/model/workbench-writer.js +96 -0
- package/runtime/dist/src/news/feeds.js +6 -10
- package/runtime/dist/src/news/interests.js +23 -0
- package/runtime/dist/src/news/service.js +87 -31
- package/runtime/dist/src/news/sources.json +116 -0
- package/runtime/dist/src/notifications/adapters/space-relay.js +4 -0
- package/runtime/dist/src/notifications/content.js +41 -0
- package/runtime/dist/src/notifications/contracts.js +4 -2
- package/runtime/dist/src/notifications/feed.js +6 -2
- package/runtime/dist/src/notifications/service.js +27 -14
- package/runtime/dist/src/notifications/store.js +3 -1
- package/runtime/dist/src/personal-agent/app-context.js +39 -4
- package/runtime/dist/src/personal-agent/chat-failure.js +37 -0
- package/runtime/dist/src/personal-agent/computer-use/contract.js +42 -0
- package/runtime/dist/src/personal-agent/computer-use/session.js +280 -0
- package/runtime/dist/src/personal-agent/computer-use/skill.js +18 -0
- package/runtime/dist/src/personal-agent/computer-use/tools.js +15 -0
- package/runtime/dist/src/personal-agent/context-candidates.js +9 -13
- package/runtime/dist/src/personal-agent/context-dependencies.js +28 -0
- package/runtime/dist/src/personal-agent/contracts.js +24 -4
- package/runtime/dist/src/personal-agent/conversation-runtime.js +440 -42
- package/runtime/dist/src/personal-agent/conversation-tool-bridge.js +97 -0
- package/runtime/dist/src/personal-agent/conversation-tool-policy.js +22 -0
- package/runtime/dist/src/personal-agent/conversations.js +71 -15
- package/runtime/dist/src/personal-agent/daily-brief.js +5 -0
- package/runtime/dist/src/personal-agent/daily-workbench.js +558 -0
- package/runtime/dist/src/personal-agent/discovery-window.js +36 -0
- package/runtime/dist/src/personal-agent/goal-checkin.js +53 -0
- package/runtime/dist/src/personal-agent/host.js +2379 -223
- package/runtime/dist/src/personal-agent/identity-context.js +33 -0
- package/runtime/dist/src/personal-agent/life.js +129 -11
- package/runtime/dist/src/personal-agent/local-shell.js +181 -0
- package/runtime/dist/src/personal-agent/local-tools.js +234 -0
- package/runtime/dist/src/personal-agent/preferred-name-sync.js +35 -0
- package/runtime/dist/src/personal-agent/profile-warmup.js +100 -39
- package/runtime/dist/src/personal-agent/project-digests.js +7 -3
- package/runtime/dist/src/personal-agent/project-history.js +36 -0
- package/runtime/dist/src/personal-agent/reading-copies.js +183 -0
- package/runtime/dist/src/personal-agent/scheduled-tasks.js +50 -13
- package/runtime/dist/src/personal-agent/scheduled-tool-runtime.js +26 -10
- package/runtime/dist/src/personal-agent/source-processing-agent.js +239 -17
- package/runtime/dist/src/personal-agent/sources.js +9 -1
- package/runtime/dist/src/personal-agent/store.js +124 -14
- package/runtime/dist/src/personal-agent/task-loop.js +3 -0
- package/runtime/dist/src/personal-agent/tasks.js +16 -4
- package/runtime/dist/src/personal-agent/timeline-brief.js +462 -0
- package/runtime/dist/src/personal-agent/timeline-cache.js +191 -0
- package/runtime/dist/src/personal-agent/timeline-cluster.js +30 -0
- package/runtime/dist/src/personal-agent/timeline-inputs.js +47 -0
- package/runtime/dist/src/personal-agent/timeline-time.js +60 -0
- package/runtime/dist/src/personal-agent/understanding-products.js +49 -0
- package/runtime/dist/src/personal-agent/understanding.js +7 -22
- package/runtime/dist/src/personal-agent/workbench-context.js +17 -21
- package/runtime/dist/src/personal-agent/workbench-copy.js +124 -0
- package/runtime/dist/src/personal-agent/workbench-evidence.js +77 -0
- package/runtime/dist/src/personal-agent/workbench-instructions.js +21 -0
- package/runtime/dist/src/personal-agent/workbench-runtime.js +1006 -0
- package/runtime/dist/src/personal-agent/workbench-shell-class.js +304 -0
- package/runtime/dist/src/personal-agent/workbench-store.js +292 -0
- package/runtime/dist/src/personal-agent/workbench-temporal.js +66 -0
- package/runtime/dist/src/projects/project-native-resource.js +10 -8
- package/runtime/dist/src/projects/project-store.js +36 -4
- package/runtime/dist/src/realtime/bridge.js +77 -8
- package/runtime/dist/src/realtime/capabilities.js +19 -0
- package/runtime/dist/src/realtime/cascaded/adapter.js +180 -40
- package/runtime/dist/src/realtime/cascaded/ark-llm.js +150 -24
- package/runtime/dist/src/realtime/cascaded/chat-completions-llm.js +243 -83
- package/runtime/dist/src/realtime/cascaded/llm-stream-limits.js +18 -0
- package/runtime/dist/src/realtime/cascaded/llm.js +96 -0
- package/runtime/dist/src/realtime/cascaded/openai-compatible-speech.js +251 -0
- package/runtime/dist/src/realtime/cascaded/provider.js +8 -0
- package/runtime/dist/src/realtime/evidence.js +1 -1
- package/runtime/dist/src/realtime/frontend-instructions.js +76 -55
- package/runtime/dist/src/realtime/gemini.js +14 -3
- package/runtime/dist/src/realtime/host-delivery.js +37 -4
- package/runtime/dist/src/realtime/protocol.js +18 -3
- package/runtime/dist/src/realtime/provider-projection.js +4 -1
- package/runtime/dist/src/realtime/provider-session.js +73 -16
- package/runtime/dist/src/realtime/qwen-transport.js +35 -4
- package/runtime/dist/src/realtime/qwen.js +144 -9
- package/runtime/dist/src/realtime/service-state.js +0 -1
- package/runtime/dist/src/realtime/service.js +93 -7
- package/runtime/dist/src/realtime/session.js +39 -32
- package/runtime/dist/src/realtime/sglang-native.js +214 -0
- package/runtime/dist/src/realtime/system-prompts.en.js +14 -2
- package/runtime/dist/src/realtime/telemetry.js +6 -3
- package/runtime/dist/src/realtime/tool-continuations.js +84 -7
- package/runtime/dist/src/realtime/transport.js +17 -0
- package/runtime/dist/src/realtime/usage.js +33 -17
- package/runtime/dist/src/realtime/volcengine/ark.js +40 -15
- package/runtime/dist/src/realtime/volcengine/asr.js +2 -1
- package/runtime/dist/src/realtime/volcengine/silence-endpointing.js +1 -7
- package/runtime/dist/src/realtime/volcengine/tts.js +2 -1
- package/runtime/dist/src/server/aoq-chat-server.js +1 -1
- package/runtime/dist/src/server/client-server.js +21 -0
- package/runtime/dist/src/server-entry.js +3 -0
- package/runtime/dist/src/space-host/admin-socket.js +115 -0
- package/runtime/dist/src/space-host/audio-owner.js +44 -0
- package/runtime/dist/src/space-host/cli.js +195 -0
- package/runtime/dist/src/space-host/config.js +247 -0
- package/runtime/dist/src/space-host/context-input.js +12 -0
- package/runtime/dist/src/space-host/contracts.js +10 -0
- package/runtime/dist/src/space-host/host-admin.js +114 -0
- package/runtime/dist/src/space-host/key-delivery.js +28 -0
- package/runtime/dist/src/space-host/mobile-tools.js +9 -0
- package/runtime/dist/src/space-host/model-settings.js +213 -0
- package/runtime/dist/src/space-host/projection.js +37 -0
- package/runtime/dist/src/space-host/push.js +137 -0
- package/runtime/dist/src/space-host/relay-transport.js +55 -0
- package/runtime/dist/src/space-host/service.js +841 -0
- package/runtime/dist/src/space-host/voice-crypto.js +240 -0
- package/runtime/dist/src/space-host/voice.js +600 -0
- package/runtime/dist/src/space-host/windows-secret-key.js +82 -0
- package/runtime/dist/src/storage/private-database.js +7 -10
- package/runtime/dist/src/text/unicode-normalize.js +5 -1
- package/runtime/dist/src/understanding/candidates.js +8 -10
- package/runtime/dist/src/understanding/fixture.js +1 -1
- package/runtime/dist/src/understanding/pipeline.js +4 -4
- package/runtime/dist/src/usage/context.js +23 -0
- package/runtime/dist/src/usage/index.js +4 -0
- package/runtime/dist/src/usage/ledger-worker.js +90 -0
- package/runtime/dist/src/usage/ledger.js +74 -0
- package/runtime/dist/src/usage/model-call.js +63 -0
- package/runtime/dist/src/usage/pricing.js +68 -0
- package/runtime/dist/src/usage/summary.js +69 -0
- package/runtime/dist/src/voicemem/store-client.js +104 -21
- package/runtime/dist/src/voicemem/store-worker.js +5 -8
- package/runtime/dist/src/voicemem/versioned-memory.js +1 -1
- package/src/command.mjs +8 -1
- package/runtime/dist/src/executors/autoglm.js +0 -376
|
@@ -0,0 +1,234 @@
|
|
|
1
|
+
import { randomUUID } from 'node:crypto';
|
|
2
|
+
import { lstat, mkdir, open, opendir, realpath, rename, stat, unlink } from 'node:fs/promises';
|
|
3
|
+
import { dirname, isAbsolute, join, posix, win32 } from 'node:path';
|
|
4
|
+
import { z } from 'zod';
|
|
5
|
+
import { executeLocalShell } from './local-shell.js';
|
|
6
|
+
export const LOCAL_OUTPUT_BYTES = 64 * 1024;
|
|
7
|
+
export const LOCAL_FILE_BYTES = 1024 * 1024;
|
|
8
|
+
export { projectResourceKey } from '../executors/task-resources.js';
|
|
9
|
+
const pathArgument = z.string().min(1).max(32768).refine(path => !path.includes('\0'), 'invalid_path');
|
|
10
|
+
export const localToolArguments = {
|
|
11
|
+
file_read: z.object({ path: pathArgument, offset: z.number().int().min(1).default(1), byte_offset: z.number().int().nonnegative().safe().optional(), limit: z.number().int().min(1).max(2000).default(2000) }).strict(),
|
|
12
|
+
file_write: z.object({ path: pathArgument, content: z.string().refine(text => Buffer.byteLength(text) <= LOCAL_FILE_BYTES, 'file_too_large') }).strict(),
|
|
13
|
+
file_edit: z.object({ path: pathArgument, old_string: z.string().min(1), new_string: z.string(), replace_all: z.boolean().default(false) }).strict(),
|
|
14
|
+
shell_execute: z.object({ command: z.string().min(1).max(LOCAL_FILE_BYTES).refine(text => !text.includes('\0')), cwd: pathArgument.optional(), timeout: z.number().int().min(1).max(3600).default(60) }).strict(),
|
|
15
|
+
};
|
|
16
|
+
const descriptions = {
|
|
17
|
+
file_read: 'Read a UTF-8 file or list a directory. Paths are relative to the selected project or absolute. offset is 1-based; limit is at most 2000. Text is limited to 64 KiB; binary files return metadata. For truncated files pass next_byte_offset as byte_offset (a zero-based UTF-8 byte cursor that overrides offset); next_offset alone may reread a partial line. Directory pagination uses next_offset only. Follow truncation and pagination before claiming complete coverage.',
|
|
18
|
+
file_write: 'Atomically create or replace a UTF-8 file, creating parent directories. Maximum content is 1 MiB. Read existing files first. The host may request user approval before writing.',
|
|
19
|
+
file_edit: 'Exactly replace old_string with new_string in a UTF-8 file (at most 1 MiB). Require one match unless replace_all=true. Preserve exact line endings. Read first; no match or ambiguous matches make no changes. The host may request approval.',
|
|
20
|
+
shell_execute: 'Execute a native shell command in the selected project or cwd. macOS uses zsh; Windows uses Windows PowerShell. timeout is 1–3600 seconds (default 60). Read stdout, stderr, exit_code and status; nonzero exit is failure. Output is limited to 64 KiB. A stopped or unknown operation must be checked before retrying.',
|
|
21
|
+
};
|
|
22
|
+
export const LOCAL_TOOLS = Object.entries(localToolArguments).map(([name, schema]) => ({ name, description: descriptions[name], parameters: JSON.parse(JSON.stringify(z.toJSONSchema(schema))) }));
|
|
23
|
+
export function isLocalTool(name) { return Object.hasOwn(localToolArguments, name); }
|
|
24
|
+
export function localPath(project, path, platform = process.platform) {
|
|
25
|
+
if (!path || path.includes('\0'))
|
|
26
|
+
throw Error('invalid_path');
|
|
27
|
+
if (platform === 'win32') {
|
|
28
|
+
// C:foo and \foo depend on ambient drive state and are never accepted.
|
|
29
|
+
if (/^[a-z]:[^\\/]/iu.test(path) || /^[a-z]:$/iu.test(path) || /^[\\/](?![\\/])/u.test(path))
|
|
30
|
+
throw Error('ambiguous_windows_path');
|
|
31
|
+
return win32.resolve(project, path);
|
|
32
|
+
}
|
|
33
|
+
return posix.resolve(project, path);
|
|
34
|
+
}
|
|
35
|
+
/** True when the path resolves, after following existing links, outside the project. The host asks before such reads. */
|
|
36
|
+
export async function localPathEscapesProject(project, path, platform = process.platform) {
|
|
37
|
+
const target = localPath(project, path, platform), flavor = platform === 'win32' ? win32 : posix;
|
|
38
|
+
const real = await realpath(target).catch(async () => {
|
|
39
|
+
// A missing leaf cannot be a link; judge its nearest existing parent so `link/new.txt` is still seen through `link`.
|
|
40
|
+
const parent = flavor.dirname(target);
|
|
41
|
+
return parent === target ? target : flavor.join(await realpath(parent).catch(() => parent), flavor.basename(target));
|
|
42
|
+
});
|
|
43
|
+
const relative = flavor.relative(project, real);
|
|
44
|
+
return relative === '' ? false : relative === '..' || relative.startsWith('..' + flavor.sep) || flavor.isAbsolute(relative);
|
|
45
|
+
}
|
|
46
|
+
export async function canonicalProject(path) {
|
|
47
|
+
if (!isAbsolute(path) || path.includes('\0'))
|
|
48
|
+
throw Error('invalid_project_directory');
|
|
49
|
+
const canonical = await realpath(localPath(dirname(path), path));
|
|
50
|
+
if (!(await stat(canonical)).isDirectory())
|
|
51
|
+
throw Error('invalid_project_directory');
|
|
52
|
+
return canonical;
|
|
53
|
+
}
|
|
54
|
+
function decode(bytes) { return new TextDecoder('utf-8', { fatal: true, ignoreBOM: true }).decode(bytes); }
|
|
55
|
+
async function readText(path) {
|
|
56
|
+
const file = await open(path, 'r');
|
|
57
|
+
try {
|
|
58
|
+
const info = await file.stat();
|
|
59
|
+
if (!info.isFile())
|
|
60
|
+
throw Error('not_a_file');
|
|
61
|
+
if (info.size > LOCAL_FILE_BYTES)
|
|
62
|
+
throw Error('file_too_large');
|
|
63
|
+
const bytes = await file.readFile();
|
|
64
|
+
if (bytes.length > LOCAL_FILE_BYTES || bytes.includes(0))
|
|
65
|
+
throw Error('not_utf8_text');
|
|
66
|
+
return { text: decode(bytes), mode: info.mode };
|
|
67
|
+
}
|
|
68
|
+
finally {
|
|
69
|
+
await file.close();
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
/** Same-directory rename, with a flushed temporary file. A failure never removes the old file. */
|
|
73
|
+
export async function atomicLocalWrite(path, content, signal, mode) {
|
|
74
|
+
if (Buffer.byteLength(content) > LOCAL_FILE_BYTES)
|
|
75
|
+
throw Error('file_too_large');
|
|
76
|
+
signal.throwIfAborted();
|
|
77
|
+
await mkdir(dirname(path), { recursive: true });
|
|
78
|
+
signal.throwIfAborted();
|
|
79
|
+
const temp = join(dirname(path), '.nova-' + randomUUID() + '.tmp'), file = await open(temp, 'wx', mode === undefined ? 0o666 : mode & 0o777);
|
|
80
|
+
try {
|
|
81
|
+
await file.writeFile(content, 'utf8');
|
|
82
|
+
if (mode !== undefined)
|
|
83
|
+
await file.chmod(mode & 0o777);
|
|
84
|
+
await file.sync();
|
|
85
|
+
await file.close();
|
|
86
|
+
signal.throwIfAborted();
|
|
87
|
+
await rename(temp, path);
|
|
88
|
+
}
|
|
89
|
+
finally {
|
|
90
|
+
await file.close().catch(() => { });
|
|
91
|
+
await unlink(temp).catch(() => { });
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
async function fileRead(path, offset, limit, signal, byteOffset) {
|
|
95
|
+
const info = await stat(path);
|
|
96
|
+
if (info.isDirectory()) {
|
|
97
|
+
if (byteOffset !== undefined)
|
|
98
|
+
throw Error('byte_offset_requires_file');
|
|
99
|
+
const entries = [];
|
|
100
|
+
let index = 0, bytes = 0, truncated = false;
|
|
101
|
+
for await (const entry of await opendir(path)) {
|
|
102
|
+
signal.throwIfAborted();
|
|
103
|
+
index++;
|
|
104
|
+
if (index < offset)
|
|
105
|
+
continue;
|
|
106
|
+
const item = { name: entry.name, type: entry.isDirectory() ? 'directory' : entry.isSymbolicLink() ? 'link' : entry.isFile() ? 'file' : 'other' }, size = Buffer.byteLength(JSON.stringify(item));
|
|
107
|
+
if (entries.length === limit || bytes + size > LOCAL_OUTPUT_BYTES) {
|
|
108
|
+
truncated = true;
|
|
109
|
+
break;
|
|
110
|
+
}
|
|
111
|
+
entries.push(item);
|
|
112
|
+
bytes += size;
|
|
113
|
+
}
|
|
114
|
+
return { path, type: 'directory', entries, offset, next_offset: truncated ? offset + entries.length : null, truncated };
|
|
115
|
+
}
|
|
116
|
+
if (!info.isFile())
|
|
117
|
+
throw Error('not_a_regular_file');
|
|
118
|
+
if (byteOffset !== undefined && byteOffset > info.size)
|
|
119
|
+
throw Error('invalid_byte_offset');
|
|
120
|
+
const file = await open(path, 'r');
|
|
121
|
+
try {
|
|
122
|
+
const buffer = Buffer.alloc(8192), decoder = new TextDecoder('utf-8', { fatal: true, ignoreBOM: true });
|
|
123
|
+
let line = 1, count = 0, content = '', partial = '', bytes = 0, position = 0, startLine = offset, truncated = false, binary = false;
|
|
124
|
+
outer: while (true) {
|
|
125
|
+
signal.throwIfAborted();
|
|
126
|
+
const { bytesRead } = await file.read(buffer, 0, buffer.length, null);
|
|
127
|
+
let chunk;
|
|
128
|
+
try {
|
|
129
|
+
const data = buffer.subarray(0, bytesRead);
|
|
130
|
+
if (data.includes(0)) {
|
|
131
|
+
binary = true;
|
|
132
|
+
break;
|
|
133
|
+
}
|
|
134
|
+
chunk = decoder.decode(data, { stream: bytesRead !== 0 });
|
|
135
|
+
}
|
|
136
|
+
catch {
|
|
137
|
+
binary = true;
|
|
138
|
+
break;
|
|
139
|
+
}
|
|
140
|
+
for (const char of chunk) {
|
|
141
|
+
const n = Buffer.byteLength(char);
|
|
142
|
+
if (byteOffset !== undefined && position < byteOffset && position + n > byteOffset)
|
|
143
|
+
throw Error('invalid_byte_offset');
|
|
144
|
+
const selected = byteOffset === undefined ? line >= offset : position >= byteOffset;
|
|
145
|
+
if (byteOffset === position)
|
|
146
|
+
startLine = line;
|
|
147
|
+
if (selected) {
|
|
148
|
+
if (count >= limit || bytes + n > LOCAL_OUTPUT_BYTES) {
|
|
149
|
+
truncated = true;
|
|
150
|
+
break outer;
|
|
151
|
+
}
|
|
152
|
+
partial += char;
|
|
153
|
+
bytes += n;
|
|
154
|
+
}
|
|
155
|
+
position += n;
|
|
156
|
+
if (char === '\n') {
|
|
157
|
+
if (selected) {
|
|
158
|
+
content += partial;
|
|
159
|
+
partial = '';
|
|
160
|
+
count++;
|
|
161
|
+
}
|
|
162
|
+
line++;
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
if (bytesRead === 0)
|
|
166
|
+
break;
|
|
167
|
+
}
|
|
168
|
+
content += partial;
|
|
169
|
+
if (partial)
|
|
170
|
+
count++;
|
|
171
|
+
if (binary)
|
|
172
|
+
return { path, type: 'binary', size: info.size };
|
|
173
|
+
return { path, type: 'file', size: info.size, content, offset: startLine, lines: count, truncated, next_offset: truncated ? line : null, next_byte_offset: truncated ? position : null };
|
|
174
|
+
}
|
|
175
|
+
finally {
|
|
176
|
+
await file.close();
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
export async function executeLocalTool(name, args, project, signal, shell = {}) {
|
|
180
|
+
signal.throwIfAborted();
|
|
181
|
+
if (name === 'shell_execute') {
|
|
182
|
+
const q = localToolArguments.shell_execute.parse(args);
|
|
183
|
+
return executeLocalShell({ ...q, cwd: localPath(project, q.cwd ?? project) }, signal, shell);
|
|
184
|
+
}
|
|
185
|
+
if (name === 'file_read') {
|
|
186
|
+
const q = localToolArguments.file_read.parse(args);
|
|
187
|
+
return fileRead(localPath(project, q.path), q.offset, q.limit, signal, q.byte_offset);
|
|
188
|
+
}
|
|
189
|
+
if (name === 'file_write') {
|
|
190
|
+
const q = localToolArguments.file_write.parse(args), requested = localPath(project, q.path);
|
|
191
|
+
// Follow an existing link instead of replacing the link itself.
|
|
192
|
+
const path = await realpath(requested).catch(async (error) => {
|
|
193
|
+
if (error.code !== 'ENOENT')
|
|
194
|
+
throw error;
|
|
195
|
+
const entry = await lstat(requested).catch(cause => { if (cause.code === 'ENOENT')
|
|
196
|
+
return null; throw cause; });
|
|
197
|
+
if (entry?.isSymbolicLink())
|
|
198
|
+
throw Error('dangling_symlink');
|
|
199
|
+
return requested;
|
|
200
|
+
});
|
|
201
|
+
let old;
|
|
202
|
+
try {
|
|
203
|
+
const file = await open(path, 'r');
|
|
204
|
+
try {
|
|
205
|
+
const info = await file.stat();
|
|
206
|
+
if (!info.isFile())
|
|
207
|
+
throw Error('not_a_file');
|
|
208
|
+
const header = Buffer.alloc(3);
|
|
209
|
+
await file.read(header, 0, 3, 0);
|
|
210
|
+
old = { mode: info.mode, bom: header.equals(Buffer.from([0xef, 0xbb, 0xbf])) };
|
|
211
|
+
}
|
|
212
|
+
finally {
|
|
213
|
+
await file.close();
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
catch (error) {
|
|
217
|
+
if (error.code !== 'ENOENT')
|
|
218
|
+
throw error;
|
|
219
|
+
}
|
|
220
|
+
const content = old?.bom && !q.content.startsWith('\ufeff') ? '\ufeff' + q.content : q.content;
|
|
221
|
+
await atomicLocalWrite(path, content, signal, old?.mode);
|
|
222
|
+
return { path, written_bytes: Buffer.byteLength(content), status: 'completed' };
|
|
223
|
+
}
|
|
224
|
+
const q = localToolArguments.file_edit.parse(args), path = await realpath(localPath(project, q.path)), old = await readText(path);
|
|
225
|
+
const matches = old.text.split(q.old_string).length - 1;
|
|
226
|
+
if (matches === 0)
|
|
227
|
+
throw Error('no_match');
|
|
228
|
+
if (matches > 1 && !q.replace_all)
|
|
229
|
+
throw Error('multiple_matches');
|
|
230
|
+
const content = q.replace_all ? old.text.split(q.old_string).join(q.new_string) : old.text.replace(q.old_string, () => q.new_string);
|
|
231
|
+
await atomicLocalWrite(path, content, signal, old.mode);
|
|
232
|
+
return { path, replacements: q.replace_all ? matches : 1, status: 'completed' };
|
|
233
|
+
}
|
|
234
|
+
//# sourceMappingURL=local-tools.js.map
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/** The authorized memory slot is durable authority; settings is its local display mirror.
|
|
2
|
+
* undefined means this provider cannot inspect names, null means the name was forgotten.
|
|
3
|
+
* Call before identity injection and after explicit memory admission. A failed mirror
|
|
4
|
+
* propagates to the caller and is repaired by the same read on the next attempt/restart.
|
|
5
|
+
*/
|
|
6
|
+
export function createPreferredNameSync(options) {
|
|
7
|
+
let tail = Promise.resolve();
|
|
8
|
+
const serial = (operation) => {
|
|
9
|
+
const run = tail.then(operation);
|
|
10
|
+
tail = run.catch(() => { });
|
|
11
|
+
return run;
|
|
12
|
+
};
|
|
13
|
+
const synchronize = async () => {
|
|
14
|
+
for (;;) {
|
|
15
|
+
const stored = await options.readMemory();
|
|
16
|
+
if (stored === undefined)
|
|
17
|
+
return;
|
|
18
|
+
const name = stored ?? '';
|
|
19
|
+
if (name === options.readSettings())
|
|
20
|
+
return;
|
|
21
|
+
await options.saveSettings(name);
|
|
22
|
+
// An admitted chat rename may race a settings write. Re-read authorized
|
|
23
|
+
// current memory rather than applying the older tool argument or receipt.
|
|
24
|
+
}
|
|
25
|
+
};
|
|
26
|
+
return {
|
|
27
|
+
synchronize: () => serial(synchronize),
|
|
28
|
+
commitMemory: (write) => serial(async () => {
|
|
29
|
+
const result = await write();
|
|
30
|
+
await synchronize();
|
|
31
|
+
return result;
|
|
32
|
+
}),
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
//# sourceMappingURL=preferred-name-sync.js.map
|
|
@@ -7,10 +7,15 @@ export const profileDraftSchema = z.object({
|
|
|
7
7
|
about: z.object({ text: z.string().trim().min(1).max(2600), refs: z.array(refs.element).min(1).max(64) }).strict().nullable(),
|
|
8
8
|
work: z.array(z.object({ title: z.string().trim().min(1).max(80), text: z.string().trim().min(1).max(500), refs }).strict()).max(6).default([]),
|
|
9
9
|
interests: z.array(z.object({ text: z.string().trim().min(1).max(100), refs }).strict()).max(8),
|
|
10
|
+
/** Short topic labels from the writer. Optional so profiles stored before keywords existed still parse. */
|
|
11
|
+
keywords: z.array(z.object({ text: z.string().trim().min(2).max(24), refs }).strict()).max(6).optional(),
|
|
10
12
|
}).strict();
|
|
11
|
-
|
|
12
|
-
const
|
|
13
|
-
const
|
|
13
|
+
/** Raise this whenever the way a profile is generated changes: drafts written by an older pipeline are then replaced whole instead of being mixed into the new one. */
|
|
14
|
+
export const PROFILE_GENERATION_VERSION = 'understanding-v10';
|
|
15
|
+
const diskSchema = z.object({ key: z.string(), generation_version: z.string().default(''), participants: z.boolean().default(false), news_cleanup_pending: z.boolean().default(false), draft: profileDraftSchema.nullable(), started_at: z.number().nonnegative().default(0), generated_at: z.number().nonnegative().default(0), last_attempt_at: z.string().nullable().default(null), stage: z.number().int().min(0).max(3).default(0), automatic_day: z.string().regex(/^$|^legacy$|^\d{4}-\d{2}-\d{2}$/).default('legacy') });
|
|
16
|
+
const localDay = (at) => { const date = new Date(at); return [date.getFullYear(), String(date.getMonth() + 1).padStart(2, '0'), String(date.getDate()).padStart(2, '0')].join('-'); };
|
|
17
|
+
const nextDay = (at) => { const date = new Date(at); date.setHours(24, 0, 0, 0); return date.getTime(); };
|
|
18
|
+
const inputKey = (entries) => entries.length ? createHash('sha256').update(JSON.stringify({ generation_version: PROFILE_GENERATION_VERSION, entries })).digest('hex') : '';
|
|
14
19
|
const factCount = (draft) => (draft.about ? 1 : 0) + draft.work.length + draft.interests.length;
|
|
15
20
|
const classify = (error, signal, controller) => {
|
|
16
21
|
if (controller.signal.aborted)
|
|
@@ -26,8 +31,10 @@ const classify = (error, signal, controller) => {
|
|
|
26
31
|
export class ProfileWarmup {
|
|
27
32
|
generate;
|
|
28
33
|
changed;
|
|
34
|
+
#openError = false;
|
|
35
|
+
#deferGeneration = false;
|
|
29
36
|
#store;
|
|
30
|
-
#cache = { key: '', draft: null, started_at: 0, generated_at: 0, stage: 0 };
|
|
37
|
+
#cache = { key: '', generation_version: PROFILE_GENERATION_VERSION, participants: false, news_cleanup_pending: false, draft: null, started_at: 0, generated_at: 0, last_attempt_at: null, stage: 0, automatic_day: '' };
|
|
31
38
|
#entries = [];
|
|
32
39
|
#eligible = [];
|
|
33
40
|
#key = '';
|
|
@@ -36,6 +43,7 @@ export class ProfileWarmup {
|
|
|
36
43
|
#run;
|
|
37
44
|
#runEntries = [];
|
|
38
45
|
#writes = Promise.resolve();
|
|
46
|
+
#stale = false;
|
|
39
47
|
#timer;
|
|
40
48
|
#firstChangeAt = 0;
|
|
41
49
|
#lastRunAt = 0;
|
|
@@ -43,8 +51,8 @@ export class ProfileWarmup {
|
|
|
43
51
|
#failedKey = '';
|
|
44
52
|
#withdrawals = 0;
|
|
45
53
|
#withdrawnDuringRun = new Set();
|
|
54
|
+
#participants;
|
|
46
55
|
#timeoutMs;
|
|
47
|
-
#backoffMs;
|
|
48
56
|
#minIntervalMs;
|
|
49
57
|
#report;
|
|
50
58
|
constructor(path, generate, changed, options = {}) {
|
|
@@ -52,37 +60,82 @@ export class ProfileWarmup {
|
|
|
52
60
|
this.changed = changed;
|
|
53
61
|
this.#store = new BoundedJsonStore(path, diskSchema);
|
|
54
62
|
this.#report = options.report;
|
|
63
|
+
this.#participants = options.participants === true;
|
|
55
64
|
this.#timeoutMs = options.timeoutMs ?? 180_000;
|
|
56
|
-
this.#backoffMs = options.backoffMs ?? [30_000, 120_000, 600_000];
|
|
57
65
|
this.#minIntervalMs = options.minIntervalMs ?? 300_000;
|
|
58
66
|
}
|
|
59
|
-
async open(
|
|
67
|
+
async open({ deferGeneration = false } = {}) {
|
|
68
|
+
this.#deferGeneration = deferGeneration;
|
|
69
|
+
try {
|
|
70
|
+
this.#cache = await this.#store.read(this.#cache);
|
|
71
|
+
this.#openError = false;
|
|
72
|
+
}
|
|
73
|
+
catch {
|
|
74
|
+
// This is a disposable suggestion cache. Unknown quota fails closed; explicit retry may repair it.
|
|
75
|
+
this.#openError = true;
|
|
76
|
+
this.#cache = { ...this.#cache, automatic_day: localDay(Date.now()) };
|
|
77
|
+
}
|
|
78
|
+
// A draft that was written while other people's messages were read cannot outlive that switch: facts grounded in them leave disk with it.
|
|
79
|
+
if (this.#cache.participants && !this.#participants) {
|
|
80
|
+
this.#cache = { ...this.#cache, key: '', draft: null, participants: false, news_cleanup_pending: true, started_at: 0, generated_at: 0, stage: 0 };
|
|
81
|
+
await this.#persist(this.#cache);
|
|
82
|
+
}
|
|
83
|
+
if (this.#cache.automatic_day === 'legacy')
|
|
84
|
+
this.#cache.automatic_day = this.#cache.generated_at ? localDay(this.#cache.generated_at) : '';
|
|
85
|
+
this.#lastRunAt = this.#cache.generated_at;
|
|
86
|
+
// A draft written by an older pipeline stays visible until the new pipeline replaces it whole; with no draft there is nothing to replace, so the ordinary schedule applies.
|
|
87
|
+
this.#stale = this.#cache.draft !== null && this.#cache.generation_version !== PROFILE_GENERATION_VERSION;
|
|
88
|
+
this.#opened = true;
|
|
89
|
+
}
|
|
90
|
+
/** Shared by automatic warmup and source-processing composition; claim before any model call. */
|
|
91
|
+
async claimAutomaticUpdate() {
|
|
92
|
+
if (!this.#opened || this.#deferGeneration || this.#cache.automatic_day >= localDay(Date.now()))
|
|
93
|
+
return false;
|
|
94
|
+
this.#cache = { ...this.#cache, automatic_day: localDay(Date.now()) };
|
|
95
|
+
try {
|
|
96
|
+
await this.#persist(this.#cache);
|
|
97
|
+
}
|
|
98
|
+
catch {
|
|
99
|
+
this.#openError = true;
|
|
100
|
+
this.changed();
|
|
101
|
+
throw Error('profile_persist_failed');
|
|
102
|
+
}
|
|
103
|
+
this.changed();
|
|
104
|
+
return this.#opened;
|
|
105
|
+
}
|
|
106
|
+
startGeneration() { if (!this.#opened)
|
|
107
|
+
return; this.#deferGeneration = false; this.#schedule(); }
|
|
60
108
|
async close() { this.#opened = false; clearTimeout(this.#timer); this.#abort.abort(); await this.#run; await this.#writes; }
|
|
61
109
|
#persist(cache) { const write = this.#writes.then(() => this.#store.write(cache)); this.#writes = write.catch(() => { }); return write; }
|
|
62
|
-
async clear() { this.invalidate(); this.#cache = { key: '', draft: null, started_at: 0, generated_at: 0, stage: 0 }; await this.#run; await this.#persist(this.#cache); }
|
|
110
|
+
async clear() { this.invalidate(); this.#cache = { key: '', generation_version: PROFILE_GENERATION_VERSION, participants: false, news_cleanup_pending: this.#cache.news_cleanup_pending, draft: null, started_at: 0, generated_at: 0, last_attempt_at: this.#cache.last_attempt_at, stage: 0, automatic_day: this.#cache.automatic_day }; await this.#run; await this.#persist(this.#cache); }
|
|
63
111
|
// A source file keeps its facts while it is re-read (its version moves); a stated memory must match exactly because a correction replaces it.
|
|
64
112
|
#grounded(ref, entries) { return entries.some(e => e.id === ref.entry_id && (e.source && !e.source.kind ? true : (e.source !== undefined || e.origin === 'stated') && e.version === ref.version)); }
|
|
65
113
|
#prune(draft, entries) {
|
|
66
114
|
const keep = (fact) => fact.refs.every(ref => this.#grounded(ref, entries));
|
|
67
|
-
const pruned = { about: draft.about && keep(draft.about) ? draft.about : null, work: draft.work.filter(keep), interests: draft.interests.filter(keep) };
|
|
115
|
+
const pruned = { about: draft.about && keep(draft.about) ? draft.about : null, work: draft.work.filter(keep), interests: draft.interests.filter(keep), ...(draft.keywords ? { keywords: draft.keywords.filter(keep) } : {}) };
|
|
68
116
|
return factCount(pruned) ? pruned : null;
|
|
69
117
|
}
|
|
70
118
|
/** Only consent withdrawal and explicit clearing discard the draft wholesale; source churn goes through update(). */
|
|
71
119
|
invalidate() { clearTimeout(this.#timer); this.#timer = undefined; this.#firstChangeAt = 0; this.#abort.abort(); this.#entries = []; this.#eligible = []; this.#key = ''; this.#failures = 0; this.#failedKey = ''; }
|
|
72
120
|
snapshot() {
|
|
73
121
|
const draft = this.#cache.draft && this.#prune(this.#cache.draft, this.#eligible);
|
|
74
|
-
//
|
|
75
|
-
const
|
|
76
|
-
|
|
122
|
+
// Refresh status is independent of the usable draft; failures keep grounded content visible.
|
|
123
|
+
const queued = !this.#run && !this.#openError && !(this.#failures && this.#failedKey === this.#key) && (this.#timer !== undefined || this.#deferGeneration && !!this.#key && this.#cache.key !== this.#key);
|
|
124
|
+
const status = this.#run ? 'working' : this.#openError || this.#failures && this.#failedKey === this.#key ? 'failed' : draft ? 'ready' : queued ? 'queued' : 'idle';
|
|
125
|
+
return { status, automatic_available: this.#cache.automatic_day < localDay(Date.now()), last_attempt_at: this.#cache.last_attempt_at, draft: draft ? structuredClone(draft) : null, sources: this.#entries.map(e => ({ id: e.id, version: e.version, label: e.source ? e.source.document ? `${e.source.project}/${e.source.document}` : e.source.project : '你提供的信息' })) };
|
|
77
126
|
}
|
|
127
|
+
/** Set on disk together with the purge of a draft written with other people's messages, and kept until the caller confirms that what was derived from the draft is gone too. */
|
|
128
|
+
newsCleanupPending() { return this.#cache.news_cleanup_pending; }
|
|
129
|
+
async confirmNewsCleanup() { if (!this.#cache.news_cleanup_pending)
|
|
130
|
+
return; this.#cache = { ...this.#cache, news_cleanup_pending: false }; await this.#persist(this.#cache); }
|
|
78
131
|
retainedEvidenceIds() {
|
|
79
132
|
const draft = this.#cache.draft;
|
|
80
|
-
return [...new Set([...(draft?.about?.refs ?? []), ...(draft?.work.flatMap(item => item.refs) ?? []), ...(draft?.interests.flatMap(item => item.refs) ?? [])].map(ref => ref.entry_id).concat(this.#runEntries.filter(input => input.source?.kind).map(input => input.id)))];
|
|
133
|
+
return [...new Set([...(draft?.about?.refs ?? []), ...(draft?.work.flatMap(item => item.refs) ?? []), ...(draft?.interests.flatMap(item => item.refs) ?? []), ...(draft?.keywords?.flatMap(item => item.refs) ?? [])].map(ref => ref.entry_id).concat(this.#runEntries.filter(input => input.source?.kind).map(input => input.id)))];
|
|
81
134
|
}
|
|
82
135
|
update(entries) {
|
|
83
136
|
const usable = entries.filter(e => e.version !== null && e.content.trim());
|
|
84
137
|
this.#eligible = [...usable];
|
|
85
|
-
const selected = [...usable.filter(e => !e.source).slice(-16), ...usable.filter(e => e.source?.kind).slice(0, 96), ...usable.filter(e => e.source && !e.source.kind).slice(0, 24)]
|
|
138
|
+
const selected = [...usable.filter(e => !e.source).slice(-16), ...usable.filter(e => e.source?.kind && e.source.relation !== 'participant').slice(0, 96), ...(this.#participants ? usable.filter(e => e.source?.relation === 'participant').slice(0, 32) : []), ...usable.filter(e => e.source && !e.source.kind).slice(0, 24)]
|
|
86
139
|
.map(e => ({ id: e.id, version: e.version, content: e.content.slice(0, 1500), origin: e.origin, ...(e.observed_at ? { observed_at: e.observed_at } : {}), ...(e.source ? { source: e.source } : {}) })).sort((a, b) => a.id.localeCompare(b.id));
|
|
87
140
|
const key = inputKey(selected);
|
|
88
141
|
if (key !== this.#key) {
|
|
@@ -118,56 +171,62 @@ export class ProfileWarmup {
|
|
|
118
171
|
#schedule() {
|
|
119
172
|
clearTimeout(this.#timer);
|
|
120
173
|
this.#timer = undefined;
|
|
121
|
-
if (!this.#key || this.#run || !this.generate)
|
|
174
|
+
if (this.#deferGeneration || !this.#opened || !this.#key || this.#run || !this.generate)
|
|
122
175
|
return;
|
|
123
|
-
|
|
124
|
-
if (this.#cache.key === this.#key && (!sourced || this.#cache.stage >= 3))
|
|
176
|
+
if (!this.#stale && this.#cache.key === this.#key)
|
|
125
177
|
return;
|
|
126
|
-
|
|
178
|
+
const now = Date.now(), sourced = this.#entries.some(entry => entry.source);
|
|
179
|
+
if (!this.#firstChangeAt)
|
|
127
180
|
this.#firstChangeAt = now;
|
|
128
|
-
|
|
129
|
-
if (
|
|
130
|
-
this.#cache.started_at = now;
|
|
131
|
-
void this.#persist({ ...this.#cache }).catch(() => { });
|
|
132
|
-
}
|
|
133
|
-
let due = sourced ? this.#cache.stage < 3 ? this.#cache.started_at + SOURCE_STAGES[this.#cache.stage] : this.#lastRunAt + SOURCE_STAGES[2] : now;
|
|
134
|
-
if (this.#cache.draft && (!sourced || this.#minIntervalMs !== 300_000))
|
|
181
|
+
let due = this.#stale ? now : sourced ? this.#firstChangeAt + 5 * 60_000 : now;
|
|
182
|
+
if (this.#cache.draft)
|
|
135
183
|
due = Math.max(due, this.#lastRunAt + this.#minIntervalMs);
|
|
136
|
-
if (this.#
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
due = Math.max(due,
|
|
141
|
-
}
|
|
184
|
+
if (this.#stale)
|
|
185
|
+
due = now;
|
|
186
|
+
// Count attempts, including failures; clock rollback and restart cannot grant another turn.
|
|
187
|
+
if (this.#cache.automatic_day >= localDay(now))
|
|
188
|
+
due = Math.max(due, nextDay(now));
|
|
142
189
|
if (due <= now) {
|
|
143
|
-
void this.refresh();
|
|
190
|
+
void this.refresh(false, true);
|
|
144
191
|
return;
|
|
145
192
|
}
|
|
146
|
-
this.#timer = setTimeout(() => { this.#timer = undefined;
|
|
193
|
+
this.#timer = setTimeout(() => { this.#timer = undefined; void this.refresh(false, true); }, due - now);
|
|
147
194
|
this.#timer.unref();
|
|
148
195
|
}
|
|
149
|
-
refresh(retry = false) {
|
|
196
|
+
refresh(retry = false, automatic = false) {
|
|
150
197
|
clearTimeout(this.#timer);
|
|
151
198
|
this.#timer = undefined;
|
|
152
199
|
this.#firstChangeAt = 0;
|
|
153
200
|
if (this.#run)
|
|
154
201
|
return this.#run;
|
|
155
|
-
if (!this.#opened || !this.#key || !this.generate)
|
|
202
|
+
if (this.#deferGeneration || !this.#opened || !this.#key || !this.generate)
|
|
156
203
|
return Promise.resolve();
|
|
204
|
+
if (automatic && this.#cache.automatic_day >= localDay(Date.now())) {
|
|
205
|
+
this.#schedule();
|
|
206
|
+
return Promise.resolve();
|
|
207
|
+
}
|
|
157
208
|
if (retry)
|
|
158
209
|
this.#failures = 0;
|
|
159
|
-
else if (
|
|
210
|
+
else if (!this.#stale && this.#cache.key === this.#key)
|
|
160
211
|
return Promise.resolve();
|
|
212
|
+
// An explicit refresh, or the first one after the pipeline changed, replaces the draft whole: nothing the previous pipeline wrote is carried into the new draft.
|
|
213
|
+
const whole = retry || this.#stale;
|
|
161
214
|
const key = this.#key, entries = structuredClone(this.#entries), controller = new AbortController(), started = Date.now();
|
|
162
215
|
this.#abort = controller;
|
|
163
216
|
this.#runEntries = entries;
|
|
164
217
|
this.#lastRunAt = started;
|
|
165
218
|
this.#withdrawnDuringRun = new Set();
|
|
219
|
+
// Durable attempt time: a failed or interrupted refresh is still visible as an attempt, never as a success.
|
|
220
|
+
this.#cache = { ...this.#cache, last_attempt_at: new Date(started).toISOString() };
|
|
221
|
+
void this.#persist(this.#cache).catch(() => undefined);
|
|
166
222
|
const signal = AbortSignal.any([controller.signal, AbortSignal.timeout(this.#timeoutMs)]);
|
|
167
223
|
let outcome = { outcome: 'ok', latency_ms: 0, items: 0 };
|
|
168
224
|
const run = (async () => {
|
|
169
225
|
let cancel = () => { };
|
|
170
226
|
try {
|
|
227
|
+
if (automatic && !await this.claimAutomaticUpdate())
|
|
228
|
+
return;
|
|
229
|
+
signal.throwIfAborted();
|
|
171
230
|
const result = await Promise.race([this.generate(entries, signal), new Promise((_, reject) => { cancel = () => reject(Error('warmup_aborted')); signal.addEventListener('abort', cancel, { once: true }); if (signal.aborted)
|
|
172
231
|
cancel(); })]);
|
|
173
232
|
signal.throwIfAborted();
|
|
@@ -179,10 +238,10 @@ export class ProfileWarmup {
|
|
|
179
238
|
outcome.items = factCount(draft);
|
|
180
239
|
const current = this.#prune(draft, this.#eligible);
|
|
181
240
|
// A reply that leaves about out keeps the previous one only while every entry it cites is unchanged; a re-read source may now say something else.
|
|
182
|
-
const previousAbout = this.#cache.draft?.about, kept = previousAbout?.refs.every(ref => !this.#withdrawnDuringRun.has(ref.entry_id) && this.#eligible.some(e => e.id === ref.entry_id && e.version === ref.version && !e.source?.kind)) ? previousAbout : null;
|
|
241
|
+
const previousAbout = this.#cache.draft?.about, kept = !whole && previousAbout?.refs.every(ref => !this.#withdrawnDuringRun.has(ref.entry_id) && this.#eligible.some(e => e.id === ref.entry_id && e.version === ref.version && !e.source?.kind)) ? previousAbout : null;
|
|
183
242
|
if (current && !current.about && kept)
|
|
184
243
|
current.about = kept;
|
|
185
|
-
if (current && this.#cache.draft) {
|
|
244
|
+
if (current && this.#cache.draft && !whole) {
|
|
186
245
|
const unchanged = (fact) => fact.refs.every(ref => !this.#withdrawnDuringRun.has(ref.entry_id) && this.#eligible.some(e => e.id === ref.entry_id && e.version === ref.version && !e.source?.kind));
|
|
187
246
|
// A newly verified message draft may retire an old plan even when its original message still exists.
|
|
188
247
|
current.work = [...current.work, ...this.#cache.draft.work.filter(fact => unchanged(fact) && !current.work.some(item => item.title === fact.title))].slice(0, 6);
|
|
@@ -196,7 +255,7 @@ export class ProfileWarmup {
|
|
|
196
255
|
return;
|
|
197
256
|
}
|
|
198
257
|
// Installed before the write so a withdrawal arriving mid-write prunes this draft and queues its own write after it.
|
|
199
|
-
const previous = this.#cache, cache = { key,
|
|
258
|
+
const previous = this.#cache, cache = { key, generation_version: PROFILE_GENERATION_VERSION, participants: this.#participants, news_cleanup_pending: this.#cache.news_cleanup_pending, draft: current, started_at: this.#cache.started_at, generated_at: started, last_attempt_at: this.#cache.last_attempt_at, stage: 3, automatic_day: this.#cache.automatic_day }, withdrawals = this.#withdrawals;
|
|
200
259
|
this.#cache = cache;
|
|
201
260
|
try {
|
|
202
261
|
await this.#persist(cache);
|
|
@@ -211,6 +270,8 @@ export class ProfileWarmup {
|
|
|
211
270
|
}
|
|
212
271
|
this.#failures = 0;
|
|
213
272
|
this.#failedKey = '';
|
|
273
|
+
this.#stale = false;
|
|
274
|
+
this.#openError = false;
|
|
214
275
|
}
|
|
215
276
|
catch (error) {
|
|
216
277
|
outcome = { ...outcome, outcome: classify(error, signal, controller) };
|
|
@@ -25,6 +25,7 @@ export class ProjectDigests {
|
|
|
25
25
|
generate;
|
|
26
26
|
changed;
|
|
27
27
|
options;
|
|
28
|
+
#deferGeneration = false;
|
|
28
29
|
#store;
|
|
29
30
|
#disk = { projects: {} };
|
|
30
31
|
#opened = false;
|
|
@@ -55,12 +56,15 @@ export class ProjectDigests {
|
|
|
55
56
|
this.#timeoutMs = options.timeoutMs ?? 60_000;
|
|
56
57
|
this.#now = options.now ?? Date.now;
|
|
57
58
|
}
|
|
58
|
-
async open() {
|
|
59
|
+
async open({ deferGeneration = false } = {}) {
|
|
60
|
+
this.#deferGeneration = deferGeneration;
|
|
59
61
|
const disk = await this.#store.read(this.#disk);
|
|
60
62
|
// A record that does not list what it read cannot be retired on withdrawal; recompute it instead.
|
|
61
63
|
this.#disk = { projects: Object.fromEntries(Object.entries(disk.projects).filter(([, hit]) => hit.inputs.length)) };
|
|
62
64
|
this.#opened = true;
|
|
63
65
|
}
|
|
66
|
+
startGeneration() { if (!this.#opened)
|
|
67
|
+
return; this.#deferGeneration = false; this.#schedule(); }
|
|
64
68
|
async close() { this.#opened = false; clearTimeout(this.#timer); this.#abort.abort(); await this.#run; await this.#writes; }
|
|
65
69
|
async clear() { clearTimeout(this.#timer); this.#abort.abort(); await this.#run; this.#projects.clear(); this.#failures.clear(); this.#blocked.clear(); this.#disk = { projects: {} }; await this.#persist(); }
|
|
66
70
|
#persist() { const snapshot = structuredClone(this.#disk), write = this.#writes.then(() => this.#store.write(snapshot)); this.#writes = write.catch(() => { }); return write; }
|
|
@@ -139,7 +143,7 @@ export class ProjectDigests {
|
|
|
139
143
|
#schedule() {
|
|
140
144
|
clearTimeout(this.#timer);
|
|
141
145
|
this.#timer = undefined;
|
|
142
|
-
if (!this.#opened || !this.generate || this.#run)
|
|
146
|
+
if (this.#deferGeneration || !this.#opened || !this.generate || this.#run)
|
|
143
147
|
return;
|
|
144
148
|
const due = this.#due();
|
|
145
149
|
if (!due.length)
|
|
@@ -157,7 +161,7 @@ export class ProjectDigests {
|
|
|
157
161
|
this.#timer = undefined;
|
|
158
162
|
if (this.#run)
|
|
159
163
|
return this.#run;
|
|
160
|
-
if (!this.#opened || !this.generate)
|
|
164
|
+
if (this.#deferGeneration || !this.#opened || !this.generate)
|
|
161
165
|
return Promise.resolve();
|
|
162
166
|
const now = this.#now();
|
|
163
167
|
this.#spent = this.#spent.filter(at => now - at < 3_600_000);
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { execFile } from 'node:child_process';
|
|
2
|
+
import { promisify } from 'node:util';
|
|
3
|
+
import { realpath } from 'node:fs/promises';
|
|
4
|
+
import { basename, dirname, resolve } from 'node:path';
|
|
5
|
+
import { createHash } from 'node:crypto';
|
|
6
|
+
import { SensitiveContentPolicy } from '../memory/sensitivity.js';
|
|
7
|
+
const run = promisify(execFile);
|
|
8
|
+
const hash = (text) => createHash('sha256').update(text).digest('hex').slice(0, 24);
|
|
9
|
+
const git = async (root, args) => { const { stdout } = await run('git', ['--no-optional-locks', '-C', root, ...args], { timeout: 3000, maxBuffer: 64000 }); return stdout.trim(); };
|
|
10
|
+
/** Paths are supplied by the authorized source registry, never by model arguments. No fetch, checkout or writes. */
|
|
11
|
+
export async function inspectProject(root) {
|
|
12
|
+
const path = await realpath(root).catch(() => resolve(root)), fallback = { id: hash(path), name: basename(path), worktree_alias: basename(path), git: false, head: null, commits: [], unmerged: [], comparison: null, dirty: false, history_available: false };
|
|
13
|
+
try {
|
|
14
|
+
const common = await realpath(resolve(path, await git(path, ['rev-parse', '--git-common-dir'])));
|
|
15
|
+
const primary = basename(common) === '.git' ? dirname(common) : path, head = await git(path, ['rev-parse', 'HEAD']);
|
|
16
|
+
const identity = await git(path, ['var', 'GIT_AUTHOR_IDENT']).catch(() => '');
|
|
17
|
+
const email = /<([^>]+)>/u.exec(identity)?.[1]?.toLowerCase();
|
|
18
|
+
const parse = (text) => text.split('\n').filter(Boolean).map(line => { const [id, at, author, mail, ...subject] = line.split('\t'); return { id: id, at: at, author: author, self: !!email && email === mail?.toLowerCase(), subject: subject.join(' ').slice(0, 240) }; });
|
|
19
|
+
const args = ['--format=%H%x09%aI%x09%an%x09%ae%x09%s', '-30'];
|
|
20
|
+
const commits = parse(await git(path, ['log', ...args])), primaryHead = await git(primary, ['rev-parse', 'HEAD']).catch(() => head);
|
|
21
|
+
const unmerged = primaryHead === head ? [] : parse(await git(path, ['log', ...args, primaryHead + '..' + head]));
|
|
22
|
+
return { ...fallback, id: hash(common), name: basename(primary), git: true, head, commits, unmerged, comparison: primaryHead, dirty: !!await git(path, ['status', '--porcelain', '--untracked-files=no']), history_available: true };
|
|
23
|
+
}
|
|
24
|
+
catch {
|
|
25
|
+
return fallback;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
export async function inspectProjectDiff(root, commit, files = []) {
|
|
29
|
+
if (!/^[a-f0-9]{40}$/u.test(commit))
|
|
30
|
+
throw Error('invalid_project_commit');
|
|
31
|
+
await git(root, ['merge-base', '--is-ancestor', commit, 'HEAD']);
|
|
32
|
+
const patch = await git(root, ['show', '--format=medium', '--no-ext-diff', '--no-textconv', '--stat', '--patch', '--unified=2', commit, ...(files.length ? ['--', ...files] : [])]);
|
|
33
|
+
const safe = new SensitiveContentPolicy().scrub('profile', patch.slice(0, 8000));
|
|
34
|
+
return { commit, text: safe.kind === 'clean' ? patch.slice(0, 8000) : safe.kind === 'redacted' ? safe.value : '', truncated: patch.length > 8000 };
|
|
35
|
+
}
|
|
36
|
+
//# sourceMappingURL=project-history.js.map
|