nova-personal-agent-server 0.0.0-stage → 0.4.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/LICENSE +201 -0
- package/README.md +30 -2
- package/bin/novapersonal-server.mjs +4 -0
- package/package.json +61 -4
- package/resources/endpointing/volcengine-v1/LICENSE.silero-vad.txt +21 -0
- package/resources/endpointing/volcengine-v1/MANIFEST.json +31 -0
- package/resources/endpointing/volcengine-v1/silence-16k-s16le.pcm +0 -0
- package/resources/endpointing/volcengine-v1/speech-16k-s16le.pcm +0 -0
- package/resources/native/codex-sandbox-probe +0 -0
- package/resources/native/project-native/nova_project_native.node +0 -0
- package/resources/native-resources-v1.json +1 -0
- package/runtime/dist/eval/demos.js +156 -0
- package/runtime/dist/eval/fixture-host.js +385 -0
- package/runtime/dist/eval/fixtures.js +319 -0
- package/runtime/dist/eval/realtime/session-fixtures.js +474 -0
- package/runtime/dist/eval/scorecard.js +285 -0
- package/runtime/dist/eval/sim.js +43 -0
- package/runtime/dist/src/cascaded-text-provider.js +31 -0
- package/runtime/dist/src/cli.js +96 -0
- package/runtime/dist/src/composition/assembly.js +352 -0
- package/runtime/dist/src/composition/cascaded-realtime-assembly.js +455 -0
- package/runtime/dist/src/composition/production-composition.js +389 -0
- package/runtime/dist/src/composition/realtime-assembly.js +1330 -0
- package/runtime/dist/src/config/capability-registry.js +315 -0
- package/runtime/dist/src/config/cascaded-realtime-config.js +146 -0
- package/runtime/dist/src/config/coding-backends.js +8 -0
- package/runtime/dist/src/config/coding-profiles.js +98 -0
- package/runtime/dist/src/config/config.js +857 -0
- package/runtime/dist/src/config/diagnostics.js +139 -0
- package/runtime/dist/src/config/environment-contract.js +202 -0
- package/runtime/dist/src/config/self-hosted.js +23 -0
- package/runtime/dist/src/connectors/composio/client.js +195 -0
- package/runtime/dist/src/connectors/composio/google.js +208 -0
- package/runtime/dist/src/connectors/composio/index.js +334 -0
- package/runtime/dist/src/connectors/feishu/cli.js +220 -0
- package/runtime/dist/src/connectors/feishu/index.js +980 -0
- package/runtime/dist/src/connectors/macos/calendar.js +35 -0
- package/runtime/dist/src/connectors/macos/mail.js +32 -0
- package/runtime/dist/src/connectors/macos/reader.js +62 -0
- package/runtime/dist/src/conversation-voice-provider.js +41 -0
- package/runtime/dist/src/core/approval-port.js +9 -0
- package/runtime/dist/src/core/approval.js +1328 -0
- package/runtime/dist/src/core/calls.js +35 -0
- package/runtime/dist/src/core/camera-session.js +32 -0
- package/runtime/dist/src/core/causal-runtime.js +650 -0
- package/runtime/dist/src/core/clock.js +139 -0
- package/runtime/dist/src/core/context-view.js +156 -0
- package/runtime/dist/src/core/effects.js +62 -0
- package/runtime/dist/src/core/events.js +217 -0
- package/runtime/dist/src/core/executor-diagnostic.js +9 -0
- package/runtime/dist/src/core/ids.js +32 -0
- package/runtime/dist/src/core/media-store.js +104 -0
- package/runtime/dist/src/core/memory.js +296 -0
- package/runtime/dist/src/core/pending-decision.js +64 -0
- package/runtime/dist/src/core/ports.js +125 -0
- package/runtime/dist/src/core/runtime.js +1459 -0
- package/runtime/dist/src/core/schedule-tools.js +15 -0
- package/runtime/dist/src/core/sims.js +186 -0
- package/runtime/dist/src/core/slots.js +97 -0
- package/runtime/dist/src/core/source-processing-instructions.js +3 -0
- package/runtime/dist/src/core/source-processing-tools.js +12 -0
- package/runtime/dist/src/core/suggestions.js +143 -0
- package/runtime/dist/src/core/task-tools.js +22 -0
- package/runtime/dist/src/core/tool-schema.js +212 -0
- package/runtime/dist/src/core/trace.js +34 -0
- package/runtime/dist/src/core/work-tools.js +94 -0
- package/runtime/dist/src/desktop/desktop-camera-source.js +31 -0
- package/runtime/dist/src/desktop/desktop-camera.js +281 -0
- package/runtime/dist/src/desktop/desktop-control.js +140 -0
- package/runtime/dist/src/desktop/desktop-progress.js +162 -0
- package/runtime/dist/src/desktop/desktop-session.js +2106 -0
- package/runtime/dist/src/desktop/desktop-tasks.js +80 -0
- package/runtime/dist/src/desktop/desktop-wire.js +605 -0
- package/runtime/dist/src/desktop/phone-desktop-entry.js +17 -0
- package/runtime/dist/src/desktop/startup-error.js +18 -0
- package/runtime/dist/src/desktop/workbench-acceptance.js +262 -0
- package/runtime/dist/src/desktop-entry.js +106 -0
- package/runtime/dist/src/desktop.js +1092 -0
- package/runtime/dist/src/executors/acp/backends.js +105 -0
- package/runtime/dist/src/executors/acp/mcp.js +81 -0
- package/runtime/dist/src/executors/acp/preflight.js +14 -0
- package/runtime/dist/src/executors/acp/process-owner.js +112 -0
- package/runtime/dist/src/executors/acp/transport.js +760 -0
- package/runtime/dist/src/executors/acp/windows-launch.js +68 -0
- package/runtime/dist/src/executors/agent-controller.js +244 -0
- package/runtime/dist/src/executors/autoglm.js +376 -0
- package/runtime/dist/src/executors/browser-device.js +102 -0
- package/runtime/dist/src/executors/browser.js +61 -0
- package/runtime/dist/src/executors/camera.js +101 -0
- package/runtime/dist/src/executors/chromium-frame-source.js +173 -0
- package/runtime/dist/src/executors/codex/adapter-live.js +292 -0
- package/runtime/dist/src/executors/codex/adapter-project.js +1306 -0
- package/runtime/dist/src/executors/codex/app-server-schema.js +712 -0
- package/runtime/dist/src/executors/codex/app-server-transport.js +1798 -0
- package/runtime/dist/src/executors/codex/approval-protocol.js +439 -0
- package/runtime/dist/src/executors/codex/common.js +655 -0
- package/runtime/dist/src/executors/codex/contract.js +581 -0
- package/runtime/dist/src/executors/codex/controller.js +141 -0
- package/runtime/dist/src/executors/codex/credential-snapshot.js +195 -0
- package/runtime/dist/src/executors/codex/factory.js +463 -0
- package/runtime/dist/src/executors/codex/host-config.js +238 -0
- package/runtime/dist/src/executors/codex/host.js +17 -0
- package/runtime/dist/src/executors/codex/index.js +19 -0
- package/runtime/dist/src/executors/codex/launch-profile.js +37 -0
- package/runtime/dist/src/executors/codex/local-sessions.js +119 -0
- package/runtime/dist/src/executors/codex/managed-mcp.js +196 -0
- package/runtime/dist/src/executors/codex/process-owner.js +398 -0
- package/runtime/dist/src/executors/codex/production-host.js +887 -0
- package/runtime/dist/src/executors/codex/progress-instructions.js +10 -0
- package/runtime/dist/src/executors/codex/protocol.js +581 -0
- package/runtime/dist/src/executors/codex/safe-json.js +87 -0
- package/runtime/dist/src/executors/codex/session-title.js +61 -0
- package/runtime/dist/src/executors/codex/shared-home.js +29 -0
- package/runtime/dist/src/executors/codex/spawn-env.js +162 -0
- package/runtime/dist/src/executors/codex/turn-projection.js +435 -0
- package/runtime/dist/src/executors/codex/version.js +38 -0
- package/runtime/dist/src/executors/codex/windows-guardian.js +716 -0
- package/runtime/dist/src/executors/coding/intake-model.js +103 -0
- package/runtime/dist/src/executors/coding/intake.js +824 -0
- package/runtime/dist/src/executors/coding/json-completion.js +14 -0
- package/runtime/dist/src/executors/coding/target-resolution.js +61 -0
- package/runtime/dist/src/executors/coding/work-order.js +50 -0
- package/runtime/dist/src/executors/coding-executor.js +23 -0
- package/runtime/dist/src/executors/computer-backend.js +188 -0
- package/runtime/dist/src/executors/computer-device.js +187 -0
- package/runtime/dist/src/executors/computer-worker.js +125 -0
- package/runtime/dist/src/executors/computer.js +100 -0
- package/runtime/dist/src/executors/device-lock.js +127 -0
- package/runtime/dist/src/executors/frame-source.js +29 -0
- package/runtime/dist/src/executors/gui-evidence.js +30 -0
- package/runtime/dist/src/executors/gui-execution.js +237 -0
- package/runtime/dist/src/executors/gui-runner.js +88 -0
- package/runtime/dist/src/executors/host-executor-capability.js +12 -0
- package/runtime/dist/src/executors/index.js +13 -0
- package/runtime/dist/src/executors/mcp-client.js +198 -0
- package/runtime/dist/src/executors/mcp.js +210 -0
- package/runtime/dist/src/executors/mobile-android.js +180 -0
- package/runtime/dist/src/executors/mobile-ios.js +303 -0
- package/runtime/dist/src/executors/mobile.js +78 -0
- package/runtime/dist/src/executors/search-mcp.js +146 -0
- package/runtime/dist/src/executors/search.js +524 -0
- package/runtime/dist/src/executors/task-resources.js +38 -0
- package/runtime/dist/src/executors/vision/assess.js +296 -0
- package/runtime/dist/src/executors/vision/controller-core.js +373 -0
- package/runtime/dist/src/executors/vision/controller.js +164 -0
- package/runtime/dist/src/executors/vision/lifecycle.js +69 -0
- package/runtime/dist/src/executors/vision.js +5 -0
- package/runtime/dist/src/executors/watcher.js +718 -0
- package/runtime/dist/src/http/bounded-response.js +37 -0
- package/runtime/dist/src/index.js +56 -0
- package/runtime/dist/src/knowledge/assembly.js +60 -0
- package/runtime/dist/src/knowledge/documents.js +520 -0
- package/runtime/dist/src/knowledge/embeddings.js +201 -0
- package/runtime/dist/src/knowledge/mcp.js +218 -0
- package/runtime/dist/src/knowledge/parse-worker.js +122 -0
- package/runtime/dist/src/knowledge/references.js +51 -0
- package/runtime/dist/src/knowledge/service.js +526 -0
- package/runtime/dist/src/knowledge/store-client.js +185 -0
- package/runtime/dist/src/knowledge/store-worker.js +843 -0
- package/runtime/dist/src/knowledge/types.js +2 -0
- package/runtime/dist/src/mcp/http-server.js +177 -0
- package/runtime/dist/src/mem0/resource.js +35 -0
- package/runtime/dist/src/mem0/store-worker.js +330 -0
- package/runtime/dist/src/memory/blackboard-session.js +205 -0
- package/runtime/dist/src/memory/blackboard-store.js +194 -0
- package/runtime/dist/src/memory/blackboard-worker.js +369 -0
- package/runtime/dist/src/memory/entry.js +39 -0
- package/runtime/dist/src/memory/factory.js +20 -0
- package/runtime/dist/src/memory/personal-memory-inspection.js +18 -0
- package/runtime/dist/src/memory/personal-memory.js +10 -0
- package/runtime/dist/src/memory/prerecall.js +20 -0
- package/runtime/dist/src/memory/provenance.js +17 -0
- package/runtime/dist/src/memory/remote-personal-memory.js +194 -0
- package/runtime/dist/src/memory/retrieval.js +235 -0
- package/runtime/dist/src/memory/sensitivity.js +261 -0
- package/runtime/dist/src/memory-ledger/models.js +98 -0
- package/runtime/dist/src/memory-ledger/store-client.js +210 -0
- package/runtime/dist/src/memory-ledger/store-worker.js +130 -0
- package/runtime/dist/src/memory-ledger/store.js +597 -0
- package/runtime/dist/src/memory-substrate/candidates.js +16 -0
- package/runtime/dist/src/memory-substrate/consolidation.js +124 -0
- package/runtime/dist/src/memory-substrate/file-authority.js +111 -0
- package/runtime/dist/src/memory-substrate/life-operations.js +280 -0
- package/runtime/dist/src/memory-substrate/markdown-repository.js +610 -0
- package/runtime/dist/src/memory-substrate/migration.js +87 -0
- package/runtime/dist/src/memory-substrate/profile-source-input.js +9 -0
- package/runtime/dist/src/memory-substrate/purge.js +398 -0
- package/runtime/dist/src/memory-substrate/resolution.js +32 -0
- package/runtime/dist/src/memory-substrate/resource.js +867 -0
- package/runtime/dist/src/memory-substrate/retrieval.js +85 -0
- package/runtime/dist/src/memory-substrate/source-operations.js +306 -0
- package/runtime/dist/src/memory-substrate/source-processing.js +326 -0
- package/runtime/dist/src/memory-substrate/source-state.js +93 -0
- package/runtime/dist/src/memory-substrate/store.js +534 -0
- package/runtime/dist/src/memory-substrate/workspace-projections.js +106 -0
- package/runtime/dist/src/memory-substrate/workspace.js +21 -0
- package/runtime/dist/src/model/jev-client.js +25 -0
- package/runtime/dist/src/model/model-adapters.js +64 -0
- package/runtime/dist/src/model/model-gateway.js +399 -0
- package/runtime/dist/src/model/model-lane.js +34 -0
- package/runtime/dist/src/model/model-purpose.js +5 -0
- package/runtime/dist/src/model/personal-writer.js +361 -0
- package/runtime/dist/src/model/proactivity.js +72 -0
- package/runtime/dist/src/model/prompting.js +279 -0
- package/runtime/dist/src/model/task-verifier.js +217 -0
- package/runtime/dist/src/model/vision-capability.js +10 -0
- package/runtime/dist/src/news/feeds.js +114 -0
- package/runtime/dist/src/news/jev-ranking.js +25 -0
- package/runtime/dist/src/news/ranking.js +26 -0
- package/runtime/dist/src/news/service.js +262 -0
- package/runtime/dist/src/notifications/adapters/feishu.js +10 -0
- package/runtime/dist/src/notifications/adapters/nova.js +15 -0
- package/runtime/dist/src/notifications/content.js +15 -0
- package/runtime/dist/src/notifications/contracts.js +9 -0
- package/runtime/dist/src/notifications/feed.js +8 -0
- package/runtime/dist/src/notifications/service.js +412 -0
- package/runtime/dist/src/notifications/store.js +4 -0
- package/runtime/dist/src/personal-agent/app-context.js +113 -0
- package/runtime/dist/src/personal-agent/approval-scope.js +8 -0
- package/runtime/dist/src/personal-agent/coding-targets.js +82 -0
- package/runtime/dist/src/personal-agent/context-candidates.js +94 -0
- package/runtime/dist/src/personal-agent/contracts.js +32 -0
- package/runtime/dist/src/personal-agent/conversation-runtime.js +536 -0
- package/runtime/dist/src/personal-agent/conversations.js +187 -0
- package/runtime/dist/src/personal-agent/daily-brief.js +53 -0
- package/runtime/dist/src/personal-agent/host.js +2403 -0
- package/runtime/dist/src/personal-agent/intake-task.js +37 -0
- package/runtime/dist/src/personal-agent/life.js +260 -0
- package/runtime/dist/src/personal-agent/memory-overview.js +19 -0
- package/runtime/dist/src/personal-agent/profile-sources.js +34 -0
- package/runtime/dist/src/personal-agent/profile-warmup.js +244 -0
- package/runtime/dist/src/personal-agent/project-digests.js +242 -0
- package/runtime/dist/src/personal-agent/sampling.js +20 -0
- package/runtime/dist/src/personal-agent/schedule-agent.js +60 -0
- package/runtime/dist/src/personal-agent/schedule-api.js +61 -0
- package/runtime/dist/src/personal-agent/schedule-presets.js +13 -0
- package/runtime/dist/src/personal-agent/schedule-reference.js +3 -0
- package/runtime/dist/src/personal-agent/scheduled-response-scope.js +38 -0
- package/runtime/dist/src/personal-agent/scheduled-run-authority.js +44 -0
- package/runtime/dist/src/personal-agent/scheduled-tasks.js +280 -0
- package/runtime/dist/src/personal-agent/scheduled-time.js +99 -0
- package/runtime/dist/src/personal-agent/scheduled-tool-runtime.js +109 -0
- package/runtime/dist/src/personal-agent/source-priority.js +142 -0
- package/runtime/dist/src/personal-agent/source-processing-agent.js +158 -0
- package/runtime/dist/src/personal-agent/source-walk.js +25 -0
- package/runtime/dist/src/personal-agent/sources.js +1484 -0
- package/runtime/dist/src/personal-agent/store-lock-worker.js +17 -0
- package/runtime/dist/src/personal-agent/store.js +146 -0
- package/runtime/dist/src/personal-agent/task-loop.js +163 -0
- package/runtime/dist/src/personal-agent/tasks.js +920 -0
- package/runtime/dist/src/personal-agent/turn-deadline.js +55 -0
- package/runtime/dist/src/personal-agent/understanding.js +160 -0
- package/runtime/dist/src/personal-agent/workbench-context.js +239 -0
- package/runtime/dist/src/projects/confirmed-project-capability.js +60 -0
- package/runtime/dist/src/projects/host-paths.js +133 -0
- package/runtime/dist/src/projects/managed-workspace-maintenance.js +351 -0
- package/runtime/dist/src/projects/project-confirmation.js +486 -0
- package/runtime/dist/src/projects/project-native-resource.js +284 -0
- package/runtime/dist/src/projects/project-node-files.js +169 -0
- package/runtime/dist/src/projects/project-root-file.js +16 -0
- package/runtime/dist/src/projects/project-state.js +504 -0
- package/runtime/dist/src/projects/project-store-files.js +1271 -0
- package/runtime/dist/src/projects/project-store.js +1345 -0
- package/runtime/dist/src/realtime/aoq.js +141 -0
- package/runtime/dist/src/realtime/bridge.js +652 -0
- package/runtime/dist/src/realtime/cascaded/adapter.js +1669 -0
- package/runtime/dist/src/realtime/cascaded/ark-llm.js +219 -0
- package/runtime/dist/src/realtime/cascaded/chat-completions-llm.js +478 -0
- package/runtime/dist/src/realtime/cascaded/gemini-speech.js +232 -0
- package/runtime/dist/src/realtime/cascaded/llm.js +37 -0
- package/runtime/dist/src/realtime/cascaded/ports.js +2 -0
- package/runtime/dist/src/realtime/cascaded/provider.js +216 -0
- package/runtime/dist/src/realtime/cascaded/qwen-llm.js +7 -0
- package/runtime/dist/src/realtime/cascaded/self-hosted-asr.js +110 -0
- package/runtime/dist/src/realtime/cascaded/self-hosted-tts.js +137 -0
- package/runtime/dist/src/realtime/cascaded/transcribe.js +42 -0
- package/runtime/dist/src/realtime/coding-progress-narration.js +28 -0
- package/runtime/dist/src/realtime/confirmation-turn-isolation.js +586 -0
- package/runtime/dist/src/realtime/dictation.js +28 -0
- package/runtime/dist/src/realtime/evidence.js +276 -0
- package/runtime/dist/src/realtime/floor.js +43 -0
- package/runtime/dist/src/realtime/frontend-instructions.js +195 -0
- package/runtime/dist/src/realtime/gemini.js +517 -0
- package/runtime/dist/src/realtime/history.js +122 -0
- package/runtime/dist/src/realtime/host-delivery.js +1711 -0
- package/runtime/dist/src/realtime/integrated-wire-profile.js +128 -0
- package/runtime/dist/src/realtime/memory-board.js +105 -0
- package/runtime/dist/src/realtime/openai-wire-profile.js +46 -0
- package/runtime/dist/src/realtime/playback.js +242 -0
- package/runtime/dist/src/realtime/project-confirmation-flow.js +1428 -0
- package/runtime/dist/src/realtime/prompt-language.js +17 -0
- package/runtime/dist/src/realtime/protocol.js +428 -0
- package/runtime/dist/src/realtime/provider-projection.js +526 -0
- package/runtime/dist/src/realtime/provider-session.js +491 -0
- package/runtime/dist/src/realtime/qwen-transport.js +144 -0
- package/runtime/dist/src/realtime/qwen.js +1377 -0
- package/runtime/dist/src/realtime/recall.js +341 -0
- package/runtime/dist/src/realtime/service-ports.js +8 -0
- package/runtime/dist/src/realtime/service-state.js +259 -0
- package/runtime/dist/src/realtime/service.js +1726 -0
- package/runtime/dist/src/realtime/session-state.js +684 -0
- package/runtime/dist/src/realtime/session.js +1976 -0
- package/runtime/dist/src/realtime/speech-prep.js +63 -0
- package/runtime/dist/src/realtime/streaming-speech.js +120 -0
- package/runtime/dist/src/realtime/system-prompts.en.js +136 -0
- package/runtime/dist/src/realtime/telemetry.js +144 -0
- package/runtime/dist/src/realtime/tool-continuations.js +1510 -0
- package/runtime/dist/src/realtime/transport.js +8 -0
- package/runtime/dist/src/realtime/usage.js +55 -0
- package/runtime/dist/src/realtime/user-origin-binding.js +173 -0
- package/runtime/dist/src/realtime/volcengine/ark.js +428 -0
- package/runtime/dist/src/realtime/volcengine/asr.js +573 -0
- package/runtime/dist/src/realtime/volcengine/audio.js +39 -0
- package/runtime/dist/src/realtime/volcengine/endpointing-capability.js +834 -0
- package/runtime/dist/src/realtime/volcengine/livekit-endpointing.js +714 -0
- package/runtime/dist/src/realtime/volcengine/local-eot-executor.js +33 -0
- package/runtime/dist/src/realtime/volcengine/protocol.js +247 -0
- package/runtime/dist/src/realtime/volcengine/silence-endpointing.js +237 -0
- package/runtime/dist/src/realtime/volcengine/tts.js +444 -0
- package/runtime/dist/src/realtime/volcengine/websocket.js +293 -0
- package/runtime/dist/src/server/aoq-chat-server.js +497 -0
- package/runtime/dist/src/server/client-pairing.js +192 -0
- package/runtime/dist/src/server/client-protocol.js +87 -0
- package/runtime/dist/src/server/client-server.js +312 -0
- package/runtime/dist/src/server/server-config.js +72 -0
- package/runtime/dist/src/server-entry.js +135 -0
- package/runtime/dist/src/storage/bounded-json.js +63 -0
- package/runtime/dist/src/storage/native-file-lock.js +8 -0
- package/runtime/dist/src/storage/native-resource-snapshot.js +41 -0
- package/runtime/dist/src/storage/private-database.js +120 -0
- package/runtime/dist/src/text/canonical-json.js +60 -0
- package/runtime/dist/src/text/python-number.js +18 -0
- package/runtime/dist/src/text/python-text.js +92 -0
- package/runtime/dist/src/text/unicode-casefold.js +29 -0
- package/runtime/dist/src/text/unicode-normalize.js +138 -0
- package/runtime/dist/src/text/unicode-tables.js +210 -0
- package/runtime/dist/src/understanding/candidates.js +77 -0
- package/runtime/dist/src/understanding/fixture.js +7 -0
- package/runtime/dist/src/understanding/jev.js +20 -0
- package/runtime/dist/src/understanding/pipeline.js +38 -0
- package/runtime/dist/src/voicemem/store-client.js +476 -0
- package/runtime/dist/src/voicemem/store-worker.js +380 -0
- package/runtime/dist/src/voicemem/versioned-memory.js +170 -0
- package/runtime/scripts/pair-device.mjs +109 -0
- package/src/command.mjs +48 -0
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Binds an intake dispatch to a task and admits it. A task created here is cancelled when the dispatch is
|
|
3
|
+
* superseded or the runtime does not admit it; otherwise recovery would later run work the user was told
|
|
4
|
+
* did not start. An existing task (task_fence) is never cancelled here.
|
|
5
|
+
*/
|
|
6
|
+
export async function admitIntakeTask(tasks, input, wanted, admit) {
|
|
7
|
+
let created;
|
|
8
|
+
const abandon = async () => {
|
|
9
|
+
if (!created)
|
|
10
|
+
return;
|
|
11
|
+
const task = tasks.get(created);
|
|
12
|
+
await tasks.cancel('intake-abandon:' + input.intake_id, { task_id: task.id, control_revision: task.control_revision, goal_revision: task.goal_revision }, { kind: 'nova' }).catch(() => undefined);
|
|
13
|
+
};
|
|
14
|
+
try {
|
|
15
|
+
const task = input.task_fence ? tasks.get(input.task_fence.task_id) : await tasks.delegate('intake:' + input.intake_id, {
|
|
16
|
+
conversation_id: input.conversation_id, ...(input.conversation_generation === undefined ? {} : { conversation_generation: input.conversation_generation }),
|
|
17
|
+
goal: input.goal, acceptance: [...input.acceptance], origin_ref: input.origin_ref,
|
|
18
|
+
});
|
|
19
|
+
if (!input.task_fence)
|
|
20
|
+
created = task.id;
|
|
21
|
+
const grant = tasks.continuationContext(input.task_fence ?? { task_id: task.id, control_revision: task.control_revision, goal_revision: task.goal_revision });
|
|
22
|
+
await tasks.setRoute(grant.fence, input.route);
|
|
23
|
+
if (!wanted()) {
|
|
24
|
+
await abandon();
|
|
25
|
+
return 'superseded';
|
|
26
|
+
}
|
|
27
|
+
const admission = await admit(grant);
|
|
28
|
+
if (!admission.accepted)
|
|
29
|
+
await abandon();
|
|
30
|
+
return admission;
|
|
31
|
+
}
|
|
32
|
+
catch (error) {
|
|
33
|
+
await abandon();
|
|
34
|
+
throw error;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
//# sourceMappingURL=intake-task.js.map
|
|
@@ -0,0 +1,260 @@
|
|
|
1
|
+
import { sourceTagSchema } from '../memory/provenance.js';
|
|
2
|
+
import { z } from 'zod';
|
|
3
|
+
import { createHash } from 'node:crypto';
|
|
4
|
+
import { constants } from 'node:fs';
|
|
5
|
+
import { open } from 'node:fs/promises';
|
|
6
|
+
import { BoundedJsonStore } from '../storage/bounded-json.js';
|
|
7
|
+
export const newsArticleSchema = z.object({ article_id: z.string().min(1).max(512), source_id: z.string().min(1).max(256), url: z.string().url().max(4096).refine(value => ['https:', 'http:'].includes(new URL(value).protocol), 'news_web_url_required'), content_hash: z.string().min(1).max(256), title: z.string().max(300), summary: z.string().max(1500), published_at: z.string().nullable() }).strict();
|
|
8
|
+
const newsSourceSchema = newsArticleSchema.extend({ action: z.literal('user_conversion'), converted_at: z.string().datetime() });
|
|
9
|
+
const importanceSchema = z.enum(['normal', 'high']);
|
|
10
|
+
const fields = { importance: importanceSchema.optional(), sources: z.array(sourceTagSchema).max(256).optional(), provenance_refs: z.array(z.string().max(600)).max(256).optional(), auto_recorded: z.boolean().optional(), source_changed: z.boolean().optional(), news_source: newsSourceSchema.optional(), id: z.string(), version: z.number().int().nonnegative(), title: z.string().trim().min(1).max(200), note: z.string().max(4000), created_at: z.string(), updated_at: z.string() };
|
|
11
|
+
const todoSchema = z.object({ ...fields, status: z.enum(['open', 'doing', 'waiting', 'done', 'cancelled']), due: z.string().date().nullable(), goal_id: z.string().nullable(), idea_id: z.string().nullable() });
|
|
12
|
+
const ideaSchema = z.object({ ...fields, status: z.enum(['active', 'archived']), goal_id: z.string().nullable() });
|
|
13
|
+
const goalSchema = z.object({ ...fields, status: z.enum(['active', 'paused', 'completed', 'archived']), success_criteria: z.string().max(2000), idea_id: z.string().nullable() });
|
|
14
|
+
export const lifeStateSchema = z.object({ todos: z.array(todoSchema).max(1000), ideas: z.array(ideaSchema).max(1000), goals: z.array(goalSchema).max(200), profile: z.object({ about: z.string().max(5000), version: z.number().int().nonnegative() }), receipts: z.record(z.string(), z.object({ hash: z.string(), result: z.object({ id: z.string(), version: z.number() }) })) });
|
|
15
|
+
export const emptyLifeState = () => ({ todos: [], ideas: [], goals: [], profile: { about: '', version: 0 }, receipts: {} });
|
|
16
|
+
const kind = z.enum(['todo', 'idea', 'goal']);
|
|
17
|
+
export const lifeInputSchema = z.discriminatedUnion('op', [
|
|
18
|
+
z.object({ op: z.literal('from_news'), kind, title: z.string().trim().min(1).max(200), note: z.string().max(4000).default(''), article: newsArticleSchema }).strict(),
|
|
19
|
+
z.object({ op: z.literal('create'), kind, importance: importanceSchema.optional(), title: z.string().trim().min(1).max(200), note: z.string().max(4000).default(''), goal_id: z.string().nullable().optional(), due: z.string().date().nullable().optional(), success_criteria: z.string().max(2000).optional() }).strict(),
|
|
20
|
+
z.object({ op: z.literal('update'), kind, id: z.string(), expected_version: z.number().int().nonnegative(), importance: importanceSchema.optional(), title: z.string().trim().min(1).max(200).optional(), note: z.string().max(4000).optional(), status: z.string().optional(), goal_id: z.string().nullable().optional(), due: z.string().date().nullable().optional(), success_criteria: z.string().max(2000).optional() }).strict(),
|
|
21
|
+
z.object({ op: z.literal('convert'), id: z.string(), target: z.enum(['todo', 'goal']), expected_version: z.number().int().nonnegative() }).strict(),
|
|
22
|
+
z.object({ op: z.literal('undo_create'), id: z.string(), expected_version: z.literal(1) }).strict(),
|
|
23
|
+
z.object({ op: z.literal('profile'), about: z.string().max(5000), expected_version: z.number().int().nonnegative() }).strict(),
|
|
24
|
+
]);
|
|
25
|
+
const hash = (s) => createHash('sha256').update(s).digest('hex');
|
|
26
|
+
/** Shared domain transition; persistence and evidence admission belong to the backend. */
|
|
27
|
+
export function applyLifeMutation(state, raw, requestId, now = new Date().toISOString()) {
|
|
28
|
+
const p = lifeInputSchema.parse(raw), payload = hash(JSON.stringify(p)), next = structuredClone(state), prior = next.receipts[requestId];
|
|
29
|
+
if (prior) {
|
|
30
|
+
if (prior.hash !== payload)
|
|
31
|
+
throw Error('request_id_conflict');
|
|
32
|
+
return { state: next, result: prior.result };
|
|
33
|
+
}
|
|
34
|
+
let result;
|
|
35
|
+
const base = (title, note) => ({ ...('importance' in p && p.importance ? { importance: p.importance } : {}), id: hash(requestId), title, note, version: 1, created_at: now, updated_at: now });
|
|
36
|
+
if ('kind' in p) {
|
|
37
|
+
if (p.kind !== 'todo' && 'due' in p && p.due)
|
|
38
|
+
throw Error('due_only_for_todo');
|
|
39
|
+
if (p.kind !== 'goal' && 'success_criteria' in p && p.success_criteria)
|
|
40
|
+
throw Error('criteria_only_for_goal');
|
|
41
|
+
if (p.kind === 'goal' && 'goal_id' in p && p.goal_id)
|
|
42
|
+
throw Error('goal_cannot_parent');
|
|
43
|
+
}
|
|
44
|
+
if ('goal_id' in p && p.goal_id) {
|
|
45
|
+
const previous = p.op === 'update' ? (p.kind === 'todo' ? next.todos : next.ideas).find(r => r.id === p.id)?.goal_id : null;
|
|
46
|
+
if (!next.goals.some(g => g.id === p.goal_id && (g.status !== 'archived' || previous === p.goal_id)))
|
|
47
|
+
throw Error('goal_not_found');
|
|
48
|
+
}
|
|
49
|
+
if (p.op === 'undo_create') {
|
|
50
|
+
const old = next.todos.find(r => r.id === p.id);
|
|
51
|
+
if (!old)
|
|
52
|
+
throw Error('item_not_found');
|
|
53
|
+
if (old.version !== p.expected_version)
|
|
54
|
+
throw Error('version_conflict');
|
|
55
|
+
next.todos = next.todos.filter(r => r.id !== p.id);
|
|
56
|
+
result = { id: p.id, version: old.version };
|
|
57
|
+
}
|
|
58
|
+
else if (p.op === 'profile') {
|
|
59
|
+
if (next.profile.version !== p.expected_version)
|
|
60
|
+
throw Error('version_conflict');
|
|
61
|
+
next.profile = { about: p.about, version: p.expected_version + 1 };
|
|
62
|
+
result = { id: 'profile', version: next.profile.version };
|
|
63
|
+
}
|
|
64
|
+
else if (p.op === 'from_news') {
|
|
65
|
+
const rows = p.kind === 'todo' ? next.todos : p.kind === 'idea' ? next.ideas : next.goals;
|
|
66
|
+
const existing = rows.find(row => row.news_source?.article_id === p.article.article_id);
|
|
67
|
+
if (existing)
|
|
68
|
+
result = existing;
|
|
69
|
+
else {
|
|
70
|
+
const b = { ...base(p.title, p.note), news_source: { ...p.article, action: 'user_conversion', converted_at: now } };
|
|
71
|
+
if (p.kind === 'todo')
|
|
72
|
+
next.todos.push({ ...b, status: 'open', due: null, goal_id: null, idea_id: null });
|
|
73
|
+
if (p.kind === 'idea')
|
|
74
|
+
next.ideas.push({ ...b, status: 'active', goal_id: null });
|
|
75
|
+
if (p.kind === 'goal')
|
|
76
|
+
next.goals.push({ ...b, status: 'active', success_criteria: '', idea_id: null });
|
|
77
|
+
result = b;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
else if (p.op === 'create') {
|
|
81
|
+
const b = base(p.title, p.note);
|
|
82
|
+
if (p.kind === 'todo')
|
|
83
|
+
next.todos.push({ ...b, status: 'open', due: p.due ?? null, goal_id: p.goal_id ?? null, idea_id: null });
|
|
84
|
+
if (p.kind === 'idea')
|
|
85
|
+
next.ideas.push({ ...b, status: 'active', goal_id: p.goal_id ?? null });
|
|
86
|
+
if (p.kind === 'goal')
|
|
87
|
+
next.goals.push({ ...b, status: 'active', success_criteria: p.success_criteria ?? '', idea_id: null });
|
|
88
|
+
result = b;
|
|
89
|
+
}
|
|
90
|
+
else if (p.op === 'convert') {
|
|
91
|
+
const idea = next.ideas.find(i => i.id === p.id);
|
|
92
|
+
if (!idea)
|
|
93
|
+
throw Error('idea_not_found');
|
|
94
|
+
const existing = p.target === 'todo' ? next.todos.find(t => t.idea_id === p.id || (idea.news_source && t.news_source?.article_id === idea.news_source.article_id)) : next.goals.find(g => g.idea_id === p.id || (idea.news_source && g.news_source?.article_id === idea.news_source.article_id));
|
|
95
|
+
if (existing)
|
|
96
|
+
result = existing;
|
|
97
|
+
else {
|
|
98
|
+
if (idea.version !== p.expected_version)
|
|
99
|
+
throw Error('version_conflict');
|
|
100
|
+
if (idea.status === 'archived')
|
|
101
|
+
throw Error('idea_archived');
|
|
102
|
+
const b = { ...base(idea.title, idea.note), ...(idea.provenance_refs ? { provenance_refs: idea.provenance_refs } : {}), ...(idea.sources ? { sources: idea.sources } : {}), ...(idea.news_source ? { news_source: idea.news_source } : {}) };
|
|
103
|
+
if (p.target === 'todo')
|
|
104
|
+
next.todos.push({ ...b, status: 'open', due: null, goal_id: idea.goal_id, idea_id: idea.id });
|
|
105
|
+
else
|
|
106
|
+
next.goals.push({ ...b, status: 'active', success_criteria: '', idea_id: idea.id });
|
|
107
|
+
result = b;
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
else {
|
|
111
|
+
const rows = p.kind === 'todo' ? next.todos : p.kind === 'idea' ? next.ideas : next.goals;
|
|
112
|
+
const old = rows.find(r => r.id === p.id);
|
|
113
|
+
if (!old)
|
|
114
|
+
throw Error('item_not_found');
|
|
115
|
+
if (old.version !== p.expected_version)
|
|
116
|
+
throw Error('version_conflict');
|
|
117
|
+
const patch = Object.fromEntries(Object.entries(p).filter(([key]) => !['op', 'kind', 'id', 'expected_version'].includes(key)));
|
|
118
|
+
const candidate = { ...old, ...patch, version: old.version + 1, updated_at: now };
|
|
119
|
+
const parsed = p.kind === 'todo' ? todoSchema.strict().parse(candidate) : p.kind === 'idea' ? ideaSchema.strict().parse(candidate) : goalSchema.strict().parse(candidate);
|
|
120
|
+
Object.assign(old, parsed);
|
|
121
|
+
result = parsed;
|
|
122
|
+
}
|
|
123
|
+
const receipt = { id: result.id, version: result.version };
|
|
124
|
+
next.receipts[requestId] = { hash: payload, result: receipt };
|
|
125
|
+
for (const key of Object.keys(next.receipts).slice(0, -256))
|
|
126
|
+
delete next.receipts[key];
|
|
127
|
+
return { state: lifeStateSchema.parse(next), result: receipt };
|
|
128
|
+
}
|
|
129
|
+
// ponytail: bounded personal collections, linear link lookup; add indexes only beyond these small limits.
|
|
130
|
+
export class LifeService {
|
|
131
|
+
path;
|
|
132
|
+
changed;
|
|
133
|
+
backend;
|
|
134
|
+
#store;
|
|
135
|
+
#state = emptyLifeState();
|
|
136
|
+
#revision = 0;
|
|
137
|
+
#tail = Promise.resolve();
|
|
138
|
+
#backend;
|
|
139
|
+
#selectedAuthority = false;
|
|
140
|
+
constructor(path, changed = () => { }, backend) {
|
|
141
|
+
this.path = path;
|
|
142
|
+
this.changed = changed;
|
|
143
|
+
this.backend = backend;
|
|
144
|
+
this.#store = new BoundedJsonStore(path, lifeStateSchema, 8 * 1024 * 1024);
|
|
145
|
+
}
|
|
146
|
+
async open() {
|
|
147
|
+
this.#backend = typeof this.backend === 'function' ? this.backend() : this.backend;
|
|
148
|
+
if (this.#backend)
|
|
149
|
+
this.#selectedAuthority = true;
|
|
150
|
+
else if (this.#selectedAuthority)
|
|
151
|
+
throw Error('life_backend_unavailable');
|
|
152
|
+
if (!this.#backend) {
|
|
153
|
+
this.#state = await this.#store.read(this.#state);
|
|
154
|
+
return;
|
|
155
|
+
}
|
|
156
|
+
const current = await this.#backend.peek?.();
|
|
157
|
+
if (current) {
|
|
158
|
+
this.#state = lifeStateSchema.parse(current.state);
|
|
159
|
+
this.#revision = current.revision;
|
|
160
|
+
return;
|
|
161
|
+
}
|
|
162
|
+
// Migration input is read-only: do not create, chmod or rewrite the old file.
|
|
163
|
+
let legacy = emptyLifeState(), legacyRead = false;
|
|
164
|
+
try {
|
|
165
|
+
const file = await open(this.path, constants.O_RDONLY | constants.O_NOFOLLOW);
|
|
166
|
+
try {
|
|
167
|
+
if ((await file.stat()).size > 8 * 1024 * 1024)
|
|
168
|
+
throw Error('store_capacity');
|
|
169
|
+
const text = await file.readFile('utf8');
|
|
170
|
+
legacyRead = true;
|
|
171
|
+
if (text)
|
|
172
|
+
legacy = lifeStateSchema.parse(JSON.parse(text));
|
|
173
|
+
}
|
|
174
|
+
finally {
|
|
175
|
+
await file.close();
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
catch (error) {
|
|
179
|
+
if (error.code !== 'ENOENT')
|
|
180
|
+
throw error;
|
|
181
|
+
}
|
|
182
|
+
const loaded = await this.#backend.load(legacy, legacyRead ? { legacyPath: this.path } : undefined);
|
|
183
|
+
this.#state = lifeStateSchema.parse(loaded.state);
|
|
184
|
+
this.#revision = loaded.revision;
|
|
185
|
+
}
|
|
186
|
+
async close() { await this.#tail; }
|
|
187
|
+
async refresh() {
|
|
188
|
+
const run = this.#tail.then(async () => {
|
|
189
|
+
if (!this.#backend?.peek)
|
|
190
|
+
return;
|
|
191
|
+
const fresh = await this.#backend.peek();
|
|
192
|
+
if (!fresh)
|
|
193
|
+
throw Error('life_backend_unavailable');
|
|
194
|
+
if (fresh.revision === this.#revision && JSON.stringify(fresh.state) === JSON.stringify(this.#state))
|
|
195
|
+
return;
|
|
196
|
+
this.#state = lifeStateSchema.parse(fresh.state);
|
|
197
|
+
this.#revision = fresh.revision;
|
|
198
|
+
this.changed();
|
|
199
|
+
});
|
|
200
|
+
this.#tail = run.catch(() => { });
|
|
201
|
+
return run;
|
|
202
|
+
}
|
|
203
|
+
snapshot() { const state = structuredClone(this.#state); return { todos: state.todos.map(r => ({ ...r, kind: 'todo' })), ideas: state.ideas.map(r => ({ ...r, kind: 'idea' })), profile: state.profile, goals: state.goals.map(g => { const todos = state.todos.filter(t => t.goal_id === g.id && t.status !== 'cancelled'); return { ...g, kind: 'goal', progress: { done: todos.filter(t => t.status === 'done').length, total: todos.length } }; }) }; }
|
|
204
|
+
async completeTaskTodo(task) {
|
|
205
|
+
if (!task.todo_ref)
|
|
206
|
+
return 'synced';
|
|
207
|
+
// The Todo was delegated at revision zero; a changed scope needs explicit reconciliation.
|
|
208
|
+
if (task.goal_revision !== 0)
|
|
209
|
+
return 'conflict';
|
|
210
|
+
const receipt = 'task-complete:' + task.id + ':' + task.goal_revision;
|
|
211
|
+
await this.refresh();
|
|
212
|
+
// Receipt is checked before the captured-version/state guard, including a lost acknowledgement.
|
|
213
|
+
if (this.#state.receipts[receipt])
|
|
214
|
+
return 'synced';
|
|
215
|
+
const todo = this.#state.todos.find(item => item.id === task.todo_ref.id);
|
|
216
|
+
if (todo?.version !== task.todo_ref.version || todo.status === 'cancelled' || todo.status === 'done')
|
|
217
|
+
return 'conflict';
|
|
218
|
+
try {
|
|
219
|
+
await this.mutate({ op: 'update', kind: 'todo', id: task.todo_ref.id, expected_version: task.todo_ref.version, status: 'done' }, receipt);
|
|
220
|
+
return 'synced';
|
|
221
|
+
}
|
|
222
|
+
catch (error) {
|
|
223
|
+
if (error instanceof Error && ['version_conflict', 'item_not_found'].includes(error.message))
|
|
224
|
+
return 'conflict';
|
|
225
|
+
throw error;
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
mutate(raw, requestId, guard, provenance) {
|
|
229
|
+
const p = lifeInputSchema.parse(raw);
|
|
230
|
+
const run = this.#tail.then(async () => {
|
|
231
|
+
guard?.();
|
|
232
|
+
if (this.#backend) {
|
|
233
|
+
try {
|
|
234
|
+
const value = await this.#backend.mutate({ input: p, requestId, expectedRevision: this.#revision, ...(provenance ? { provenance } : {}) });
|
|
235
|
+
this.#state = lifeStateSchema.parse(value.state);
|
|
236
|
+
this.#revision = value.revision;
|
|
237
|
+
this.changed();
|
|
238
|
+
return value.result;
|
|
239
|
+
}
|
|
240
|
+
catch (error) {
|
|
241
|
+
if (error instanceof Error && error.message === 'version_conflict') {
|
|
242
|
+
const fresh = await this.#backend.load(emptyLifeState());
|
|
243
|
+
this.#state = lifeStateSchema.parse(fresh.state);
|
|
244
|
+
this.#revision = fresh.revision;
|
|
245
|
+
this.changed();
|
|
246
|
+
}
|
|
247
|
+
throw error;
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
const next = applyLifeMutation(this.#state, p, requestId);
|
|
251
|
+
await this.#store.write(next.state);
|
|
252
|
+
this.#state = next.state;
|
|
253
|
+
this.changed();
|
|
254
|
+
return next.result;
|
|
255
|
+
});
|
|
256
|
+
this.#tail = run.catch(() => { });
|
|
257
|
+
return run;
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
//# sourceMappingURL=life.js.map
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
import { versionSchema } from './contracts.js';
|
|
3
|
+
export const memoryOverviewSchema = z.object({
|
|
4
|
+
summary: z.string().trim().min(1).max(500),
|
|
5
|
+
sections: z.array(z.object({
|
|
6
|
+
title: z.string().trim().min(1).max(40),
|
|
7
|
+
summary: z.string().trim().min(1).max(500),
|
|
8
|
+
keywords: z.array(z.string().trim().min(1).max(40)).max(5),
|
|
9
|
+
refs: z.array(z.object({ entry_id: z.string().min(1).max(256), version: versionSchema }).strict()).min(1).max(100),
|
|
10
|
+
}).strict()).min(1).max(4),
|
|
11
|
+
}).strict();
|
|
12
|
+
export function validateMemoryOverview(value, entries) {
|
|
13
|
+
const parsed = memoryOverviewSchema.safeParse(value);
|
|
14
|
+
if (!parsed.success)
|
|
15
|
+
return null;
|
|
16
|
+
const active = new Map(entries.filter(entry => entry.status === 'active').map(entry => [entry.id, entry.version]));
|
|
17
|
+
return parsed.data.sections.every(section => section.refs.every(ref => active.get(ref.entry_id) === ref.version)) ? parsed.data : null;
|
|
18
|
+
}
|
|
19
|
+
//# sourceMappingURL=memory-overview.js.map
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { createHash } from 'node:crypto';
|
|
2
|
+
import { basename } from 'node:path';
|
|
3
|
+
import { eligibleDocument } from './context-candidates.js';
|
|
4
|
+
/** A small, varied set of active work material for the profile draft. */
|
|
5
|
+
export function selectProfileSources(entries) {
|
|
6
|
+
const groups = new Map();
|
|
7
|
+
for (const entry of entries) {
|
|
8
|
+
if (!('kind' in entry) || entry.kind !== 'file' || entry.priority < 1 || !entry.content.trim() || !eligibleDocument(entry.rel_path, entry.role, entry.hidden_prefix_depth))
|
|
9
|
+
continue;
|
|
10
|
+
const group = groups.get(entry.root) ?? [];
|
|
11
|
+
group.push(entry);
|
|
12
|
+
groups.set(entry.root, group);
|
|
13
|
+
}
|
|
14
|
+
const ranked = [...groups.entries()].map(([root, files]) => ({
|
|
15
|
+
root, priority: Math.max(...files.map(file => file.kind === 'file' ? file.priority : 0)),
|
|
16
|
+
recent: Math.max(...files.map(file => file.kind === 'file' ? file.mtime_ms : 0)),
|
|
17
|
+
files: files.sort((a, b) => (b.kind === 'file' ? b.mtime_ms : 0) - (a.kind === 'file' ? a.mtime_ms : 0)).slice(0, 2),
|
|
18
|
+
})).sort((a, b) => b.priority - a.priority || b.recent - a.recent || a.root.localeCompare(b.root)).slice(0, 12);
|
|
19
|
+
return ranked.flatMap(group => group.files.map(entry => {
|
|
20
|
+
if (entry.kind !== 'file')
|
|
21
|
+
throw Error('profile_source_kind');
|
|
22
|
+
return { id: entry.id, version: entry.version, content: entry.content, origin: 'inferred',
|
|
23
|
+
source: { project: basename(entry.root), document: basename(entry.rel_path) } };
|
|
24
|
+
}));
|
|
25
|
+
}
|
|
26
|
+
/** The user's own projects, as profile input. Each digest stands in for the files it cites; its version moves when its text does. */
|
|
27
|
+
export function profileInputsFromDigests(digests) {
|
|
28
|
+
return digests.filter(digest => digest.role === 'own').slice(0, 12).map(digest => {
|
|
29
|
+
const content = [digest.summary, digest.focus && '近期重点:' + digest.focus, digest.next_step && '下一步:' + digest.next_step].filter(Boolean).join(' ');
|
|
30
|
+
return { id: 'project:' + digest.project_key, version: createHash('sha256').update(digest.name + '\0' + content).digest('hex').slice(0, 16), content: digest.name + ':' + content,
|
|
31
|
+
origin: 'inferred', source: { project: digest.name, document: '' }, refs: digest.refs };
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
//# sourceMappingURL=profile-sources.js.map
|
|
@@ -0,0 +1,244 @@
|
|
|
1
|
+
import { createHash } from 'node:crypto';
|
|
2
|
+
import { z } from 'zod';
|
|
3
|
+
import { BoundedJsonStore } from '../storage/bounded-json.js';
|
|
4
|
+
import { versionSchema } from './contracts.js';
|
|
5
|
+
const refs = z.array(z.object({ entry_id: z.string().min(1).max(256), version: versionSchema }).strict()).min(1).max(8);
|
|
6
|
+
export const profileDraftSchema = z.object({
|
|
7
|
+
about: z.object({ text: z.string().trim().min(1).max(2600), refs: z.array(refs.element).min(1).max(64) }).strict().nullable(),
|
|
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
|
+
interests: z.array(z.object({ text: z.string().trim().min(1).max(100), refs }).strict()).max(8),
|
|
10
|
+
}).strict();
|
|
11
|
+
const diskSchema = z.object({ key: z.string(), draft: profileDraftSchema.nullable(), started_at: z.number().nonnegative().default(0), generated_at: z.number().nonnegative().default(0), stage: z.number().int().min(0).max(3).default(0) });
|
|
12
|
+
const SOURCE_STAGES = [5 * 60_000, 30 * 60_000, 4 * 60 * 60_000];
|
|
13
|
+
const inputKey = (entries) => entries.length ? createHash('sha256').update(JSON.stringify({ generation_version: 'sender-grounded-prose-v4', entries })).digest('hex') : '';
|
|
14
|
+
const factCount = (draft) => (draft.about ? 1 : 0) + draft.work.length + draft.interests.length;
|
|
15
|
+
const classify = (error, signal, controller) => {
|
|
16
|
+
if (controller.signal.aborted)
|
|
17
|
+
return 'aborted';
|
|
18
|
+
if (signal.aborted)
|
|
19
|
+
return 'timeout';
|
|
20
|
+
if (error instanceof z.ZodError)
|
|
21
|
+
return 'schema';
|
|
22
|
+
const message = error instanceof Error ? error.message : '';
|
|
23
|
+
return message === 'invalid_profile_evidence' ? 'evidence' : message === 'processing_consent_required' ? 'consent' : message === 'profile_persist_failed' ? 'persist' : 'provider';
|
|
24
|
+
};
|
|
25
|
+
/** Disposable suggestions only: never writes profile facts, news preferences, or consent. */
|
|
26
|
+
export class ProfileWarmup {
|
|
27
|
+
generate;
|
|
28
|
+
changed;
|
|
29
|
+
#store;
|
|
30
|
+
#cache = { key: '', draft: null, started_at: 0, generated_at: 0, stage: 0 };
|
|
31
|
+
#entries = [];
|
|
32
|
+
#eligible = [];
|
|
33
|
+
#key = '';
|
|
34
|
+
#opened = false;
|
|
35
|
+
#abort = new AbortController();
|
|
36
|
+
#run;
|
|
37
|
+
#runEntries = [];
|
|
38
|
+
#writes = Promise.resolve();
|
|
39
|
+
#timer;
|
|
40
|
+
#firstChangeAt = 0;
|
|
41
|
+
#lastRunAt = 0;
|
|
42
|
+
#failures = 0;
|
|
43
|
+
#failedKey = '';
|
|
44
|
+
#withdrawals = 0;
|
|
45
|
+
#withdrawnDuringRun = new Set();
|
|
46
|
+
#timeoutMs;
|
|
47
|
+
#backoffMs;
|
|
48
|
+
#minIntervalMs;
|
|
49
|
+
#report;
|
|
50
|
+
constructor(path, generate, changed, options = {}) {
|
|
51
|
+
this.generate = generate;
|
|
52
|
+
this.changed = changed;
|
|
53
|
+
this.#store = new BoundedJsonStore(path, diskSchema);
|
|
54
|
+
this.#report = options.report;
|
|
55
|
+
this.#timeoutMs = options.timeoutMs ?? 180_000;
|
|
56
|
+
this.#backoffMs = options.backoffMs ?? [30_000, 120_000, 600_000];
|
|
57
|
+
this.#minIntervalMs = options.minIntervalMs ?? 300_000;
|
|
58
|
+
}
|
|
59
|
+
async open() { this.#cache = await this.#store.read(this.#cache); this.#lastRunAt = this.#cache.generated_at; this.#opened = true; }
|
|
60
|
+
async close() { this.#opened = false; clearTimeout(this.#timer); this.#abort.abort(); await this.#run; await this.#writes; }
|
|
61
|
+
#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); }
|
|
63
|
+
// 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
|
+
#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
|
+
#prune(draft, entries) {
|
|
66
|
+
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) };
|
|
68
|
+
return factCount(pruned) ? pruned : null;
|
|
69
|
+
}
|
|
70
|
+
/** Only consent withdrawal and explicit clearing discard the draft wholesale; source churn goes through update(). */
|
|
71
|
+
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
|
+
snapshot() {
|
|
73
|
+
const draft = this.#cache.draft && this.#prune(this.#cache.draft, this.#eligible);
|
|
74
|
+
// A visible draft reads as ready even while a background refresh runs or fails; the renderer never blanks it.
|
|
75
|
+
const status = this.#run ? 'working' : draft ? 'ready' : this.#failures && this.#failedKey === this.#key ? 'failed' : 'idle';
|
|
76
|
+
return { status, 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
|
+
}
|
|
78
|
+
retainedEvidenceIds() {
|
|
79
|
+
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)))];
|
|
81
|
+
}
|
|
82
|
+
update(entries) {
|
|
83
|
+
const usable = entries.filter(e => e.version !== null && e.content.trim());
|
|
84
|
+
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)]
|
|
86
|
+
.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
|
+
const key = inputKey(selected);
|
|
88
|
+
if (key !== this.#key) {
|
|
89
|
+
this.#key = key;
|
|
90
|
+
this.#entries = selected;
|
|
91
|
+
// Nothing the running call can cite is still eligible, so its answer would be discarded anyway.
|
|
92
|
+
if (this.#run && !this.#runEntries.some(entry => this.#grounded({ entry_id: entry.id, version: entry.version }, this.#eligible)))
|
|
93
|
+
this.#abort.abort();
|
|
94
|
+
}
|
|
95
|
+
// Facts whose evidence is momentarily absent (a root still warming up, a file mid re-read) are hidden by snapshot(), not deleted.
|
|
96
|
+
this.#schedule();
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* Called after the user withdraws a source, with every source id that is still eligible (read synchronously, so a
|
|
100
|
+
* superseded memory refresh cannot leave stale eligibility). Facts it grounded leave disk as well as the view.
|
|
101
|
+
*/
|
|
102
|
+
async forgetUnavailable(eligibleSources) {
|
|
103
|
+
const withdrawn = this.#eligible.filter(entry => entry.source && !eligibleSources.has(entry.id));
|
|
104
|
+
for (const entry of [...withdrawn, ...this.#runEntries.filter(entry => entry.source && !eligibleSources.has(entry.id))])
|
|
105
|
+
this.#withdrawnDuringRun.add(entry.id);
|
|
106
|
+
this.#entries = this.#entries.filter(entry => !withdrawn.some(item => item.id === entry.id));
|
|
107
|
+
this.#eligible = this.#eligible.filter(entry => !withdrawn.includes(entry));
|
|
108
|
+
this.#key = inputKey(this.#entries);
|
|
109
|
+
this.#withdrawals++;
|
|
110
|
+
const draft = this.#cache.draft;
|
|
111
|
+
const pruned = draft && this.#prune(draft, this.#eligible);
|
|
112
|
+
if (draft && (!pruned || factCount(pruned) !== factCount(draft)))
|
|
113
|
+
this.#cache = { ...this.#cache, key: pruned ? this.#cache.key : '', draft: pruned };
|
|
114
|
+
// Queued behind any in-flight draft write, so the pruned cache is what stays on disk.
|
|
115
|
+
await this.#persist(this.#cache);
|
|
116
|
+
this.changed();
|
|
117
|
+
}
|
|
118
|
+
#schedule() {
|
|
119
|
+
clearTimeout(this.#timer);
|
|
120
|
+
this.#timer = undefined;
|
|
121
|
+
if (!this.#key || this.#run || !this.generate)
|
|
122
|
+
return;
|
|
123
|
+
const now = Date.now(), sourced = this.#entries.some(entry => entry.source);
|
|
124
|
+
if (this.#cache.key === this.#key && (!sourced || this.#cache.stage >= 3))
|
|
125
|
+
return;
|
|
126
|
+
if (sourced && !this.#firstChangeAt)
|
|
127
|
+
this.#firstChangeAt = now;
|
|
128
|
+
// Source churn never moves these deadlines; only successful drafts advance the ladder.
|
|
129
|
+
if (sourced && !this.#cache.started_at) {
|
|
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))
|
|
135
|
+
due = Math.max(due, this.#lastRunAt + this.#minIntervalMs);
|
|
136
|
+
if (this.#failures) {
|
|
137
|
+
// Retries back off; once the ladder is spent the same inputs wait for an explicit retry, while new inputs retry at the slowest step.
|
|
138
|
+
if (this.#failures > this.#backoffMs.length && this.#failedKey === this.#key)
|
|
139
|
+
return;
|
|
140
|
+
due = Math.max(due, this.#lastRunAt + this.#backoffMs[Math.min(this.#failures, this.#backoffMs.length) - 1]);
|
|
141
|
+
}
|
|
142
|
+
if (due <= now) {
|
|
143
|
+
void this.refresh();
|
|
144
|
+
return;
|
|
145
|
+
}
|
|
146
|
+
this.#timer = setTimeout(() => { this.#timer = undefined; this.#firstChangeAt = 0; void this.refresh(); }, due - now);
|
|
147
|
+
this.#timer.unref();
|
|
148
|
+
}
|
|
149
|
+
refresh(retry = false) {
|
|
150
|
+
clearTimeout(this.#timer);
|
|
151
|
+
this.#timer = undefined;
|
|
152
|
+
this.#firstChangeAt = 0;
|
|
153
|
+
if (this.#run)
|
|
154
|
+
return this.#run;
|
|
155
|
+
if (!this.#opened || !this.#key || !this.generate)
|
|
156
|
+
return Promise.resolve();
|
|
157
|
+
if (retry)
|
|
158
|
+
this.#failures = 0;
|
|
159
|
+
else if (this.#cache.key === this.#key && !(this.#cache.started_at && this.#cache.stage < 3 && Date.now() >= this.#cache.started_at + SOURCE_STAGES[this.#cache.stage]))
|
|
160
|
+
return Promise.resolve();
|
|
161
|
+
const key = this.#key, entries = structuredClone(this.#entries), controller = new AbortController(), started = Date.now();
|
|
162
|
+
this.#abort = controller;
|
|
163
|
+
this.#runEntries = entries;
|
|
164
|
+
this.#lastRunAt = started;
|
|
165
|
+
this.#withdrawnDuringRun = new Set();
|
|
166
|
+
const signal = AbortSignal.any([controller.signal, AbortSignal.timeout(this.#timeoutMs)]);
|
|
167
|
+
let outcome = { outcome: 'ok', latency_ms: 0, items: 0 };
|
|
168
|
+
const run = (async () => {
|
|
169
|
+
let cancel = () => { };
|
|
170
|
+
try {
|
|
171
|
+
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
|
+
cancel(); })]);
|
|
173
|
+
signal.throwIfAborted();
|
|
174
|
+
// Facts citing anything outside this call's inputs are invented; drop them and keep the grounded rest.
|
|
175
|
+
// Evidence withdrawn while the call ran stays withdrawn even if the same id (a re-digested project) is eligible again.
|
|
176
|
+
const draft = this.#prune(profileDraftSchema.parse(result), entries.filter(entry => !this.#withdrawnDuringRun.has(entry.id)));
|
|
177
|
+
if (!draft)
|
|
178
|
+
throw Error('invalid_profile_evidence');
|
|
179
|
+
outcome.items = factCount(draft);
|
|
180
|
+
const current = this.#prune(draft, this.#eligible);
|
|
181
|
+
// 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;
|
|
183
|
+
if (current && !current.about && kept)
|
|
184
|
+
current.about = kept;
|
|
185
|
+
if (current && this.#cache.draft) {
|
|
186
|
+
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
|
+
// A newly verified message draft may retire an old plan even when its original message still exists.
|
|
188
|
+
current.work = [...current.work, ...this.#cache.draft.work.filter(fact => unchanged(fact) && !current.work.some(item => item.title === fact.title))].slice(0, 6);
|
|
189
|
+
current.interests = [...current.interests, ...this.#cache.draft.interests.filter(fact => unchanged(fact) && !current.interests.some(item => item.text === fact.text))].slice(0, 8);
|
|
190
|
+
}
|
|
191
|
+
if (!this.#opened)
|
|
192
|
+
return;
|
|
193
|
+
if (!current) {
|
|
194
|
+
this.#failures = 0;
|
|
195
|
+
this.#failedKey = '';
|
|
196
|
+
return;
|
|
197
|
+
}
|
|
198
|
+
// 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, draft: current, started_at: this.#cache.started_at, generated_at: started, stage: this.#cache.started_at ? Math.max(this.#cache.stage, SOURCE_STAGES.filter(delay => started >= this.#cache.started_at + delay).length) : 0 }, withdrawals = this.#withdrawals;
|
|
200
|
+
this.#cache = cache;
|
|
201
|
+
try {
|
|
202
|
+
await this.#persist(cache);
|
|
203
|
+
}
|
|
204
|
+
catch {
|
|
205
|
+
// A rollback must not bring back facts withdrawn since the previous draft was captured.
|
|
206
|
+
if (this.#cache === cache) {
|
|
207
|
+
const kept = withdrawals === this.#withdrawals ? previous.draft : previous.draft && this.#prune(previous.draft, this.#eligible);
|
|
208
|
+
this.#cache = { ...previous, key: kept ? previous.key : '', draft: kept };
|
|
209
|
+
}
|
|
210
|
+
throw Error('profile_persist_failed');
|
|
211
|
+
}
|
|
212
|
+
this.#failures = 0;
|
|
213
|
+
this.#failedKey = '';
|
|
214
|
+
}
|
|
215
|
+
catch (error) {
|
|
216
|
+
outcome = { ...outcome, outcome: classify(error, signal, controller) };
|
|
217
|
+
if (this.#opened && outcome.outcome !== 'aborted') {
|
|
218
|
+
this.#failures++;
|
|
219
|
+
this.#failedKey = key;
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
finally {
|
|
223
|
+
signal.removeEventListener('abort', cancel);
|
|
224
|
+
outcome.latency_ms = Date.now() - started;
|
|
225
|
+
}
|
|
226
|
+
})();
|
|
227
|
+
this.#run = run;
|
|
228
|
+
this.changed();
|
|
229
|
+
void run.finally(() => {
|
|
230
|
+
this.#run = undefined;
|
|
231
|
+
this.#runEntries = [];
|
|
232
|
+
try {
|
|
233
|
+
this.#report?.(outcome);
|
|
234
|
+
}
|
|
235
|
+
catch { /* Reporting never changes the draft. */ }
|
|
236
|
+
if (!this.#opened)
|
|
237
|
+
return;
|
|
238
|
+
this.#schedule();
|
|
239
|
+
this.changed();
|
|
240
|
+
});
|
|
241
|
+
return run;
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
//# sourceMappingURL=profile-warmup.js.map
|