@evandu/super-pi-server 0.0.0-stage → 0.1.260
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/README.md +250 -3
- package/agents/code-auditor.md +64 -0
- package/agents/code-designer.md +64 -0
- package/agents/coder.md +24 -0
- package/agents/critic.md +24 -0
- package/agents/document-organizer.md +48 -0
- package/agents/patcher.md +27 -0
- package/agents/planner.md +30 -0
- package/agents/project-leader.md +28 -0
- package/agents/reviewer.md +27 -0
- package/agents/scout.md +27 -0
- package/agents/solution-designer.md +62 -0
- package/agents/super-pi-documents/index.ts +92 -0
- package/agents/super-pi-feishu/index.ts +88 -0
- package/agents/super-pi-knowledge/index.ts +70 -0
- package/agents/super-pi-managed-model/index.ts +9 -0
- package/agents/super-pi-memory/index.ts +75 -0
- package/agents/super-pi-native-tools/index.ts +1 -0
- package/agents/super-pi-task/core.ts +2262 -0
- package/agents/super-pi-task/index.ts +814 -0
- package/agents/super-pi-task/workflow-schema.ts +34 -0
- package/agents/super-pi-web/index.ts +32 -0
- package/agents/verifier.md +25 -0
- package/assets/fonts/LICENSE +92 -0
- package/assets/fonts/NotoSansSC-Bold.otf +0 -0
- package/assets/fonts/NotoSansSC-Regular.otf +0 -0
- package/assets/knowledge-model/README.md +37 -0
- package/assets/knowledge-model/Xenova/bge-small-zh-v1.5/config.json +31 -0
- package/assets/knowledge-model/Xenova/bge-small-zh-v1.5/onnx/model_quantized.onnx +0 -0
- package/assets/knowledge-model/Xenova/bge-small-zh-v1.5/special_tokens_map.json +7 -0
- package/assets/knowledge-model/Xenova/bge-small-zh-v1.5/tokenizer.json +21278 -0
- package/assets/knowledge-model/Xenova/bge-small-zh-v1.5/tokenizer_config.json +15 -0
- package/assets/knowledge-model/Xenova/bge-small-zh-v1.5/vocab.txt +21128 -0
- package/bin/npm-prefix.d.ts +1 -0
- package/bin/npm-prefix.js +10 -0
- package/bin/super-pi.js +349 -0
- package/dist/client/.super-pi-client-sources.json +82 -0
- package/dist/client/assets/CodeMirrorEditor-mi9rGUce.js +2 -0
- package/dist/client/assets/GitDiffEditor-CpoDDQrA.css +1 -0
- package/dist/client/assets/GitDiffEditor-FQBfGxOY.js +29 -0
- package/dist/client/assets/diff-ChtP43wD.js +1 -0
- package/dist/client/assets/dist-AW5lyI3I.js +1 -0
- package/dist/client/assets/dist-B3-a6uF0.js +7 -0
- package/dist/client/assets/dist-BGxkyh6e.js +1 -0
- package/dist/client/assets/dist-BJxfd7yb.js +11 -0
- package/dist/client/assets/dist-BRVQKlcp.js +1 -0
- package/dist/client/assets/dist-BV64A9Ji.js +1 -0
- package/dist/client/assets/dist-BXAOrsjq.js +1 -0
- package/dist/client/assets/dist-CKF2Jksy.js +1 -0
- package/dist/client/assets/dist-CX_Hm4GS.js +1 -0
- package/dist/client/assets/dist-Cus6_oaF.js +2 -0
- package/dist/client/assets/dist-Cx00EZKR.js +23 -0
- package/dist/client/assets/dist-D71y2EI1.js +1 -0
- package/dist/client/assets/dist-D978o95T.js +5 -0
- package/dist/client/assets/dist-Dc1MBGAG.js +1 -0
- package/dist/client/assets/dist-Dds-qLxY.js +9 -0
- package/dist/client/assets/dist-DeD9copn.js +1 -0
- package/dist/client/assets/dist-DuvGReu-.js +6 -0
- package/dist/client/assets/dist-Dvb0SqcK.js +2 -0
- package/dist/client/assets/dist-J6XWqRpB.js +3 -0
- package/dist/client/assets/dist-QzbOCAU6.js +1 -0
- package/dist/client/assets/dist-moiMRpw_.js +1 -0
- package/dist/client/assets/dist-scBkrwv-.js +2 -0
- package/dist/client/assets/dockerfile-BDePf5PZ.js +1 -0
- package/dist/client/assets/index-Cn7hdrDw.js +350 -0
- package/dist/client/assets/index-UfzqdECL.css +1 -0
- package/dist/client/assets/powershell-D8WpQPaX.js +1 -0
- package/dist/client/assets/properties-C357ku5U.js +1 -0
- package/dist/client/assets/rolldown-runtime-XKyaHtl9.js +1 -0
- package/dist/client/assets/ruby-ChfNwlCY.js +1 -0
- package/dist/client/assets/shell-DwuoZtxw.js +1 -0
- package/dist/client/assets/swift-DKB6_1j6.js +1 -0
- package/dist/client/assets/toml-BPTmHmyx.js +1 -0
- package/dist/client/favicon.ico +0 -0
- package/dist/client/favicon.png +0 -0
- package/dist/client/favicon.svg +10 -0
- package/dist/client/index.html +18 -0
- package/dist/extension.d.ts +15 -0
- package/dist/extension.js +126 -0
- package/dist/server/active-memory.d.ts +247 -0
- package/dist/server/active-memory.js +669 -0
- package/dist/server/agents.d.ts +245 -0
- package/dist/server/agents.js +701 -0
- package/dist/server/application/catalog-service.d.ts +98 -0
- package/dist/server/application/catalog-service.js +245 -0
- package/dist/server/application/conversation-service.d.ts +245 -0
- package/dist/server/application/conversation-service.js +654 -0
- package/dist/server/application/document-service.d.ts +68 -0
- package/dist/server/application/document-service.js +120 -0
- package/dist/server/application/extension-ui-service.d.ts +44 -0
- package/dist/server/application/extension-ui-service.js +135 -0
- package/dist/server/application/feishu-service.d.ts +179 -0
- package/dist/server/application/feishu-service.js +358 -0
- package/dist/server/application/group-chat-service.d.ts +75 -0
- package/dist/server/application/group-chat-service.js +953 -0
- package/dist/server/application/group-execution.d.ts +15 -0
- package/dist/server/application/group-execution.js +77 -0
- package/dist/server/application/project-service.d.ts +464 -0
- package/dist/server/application/project-service.js +2523 -0
- package/dist/server/application/provider-service.d.ts +174 -0
- package/dist/server/application/provider-service.js +510 -0
- package/dist/server/application/run-service.d.ts +547 -0
- package/dist/server/application/run-service.js +2683 -0
- package/dist/server/application/scheduled-task-service.d.ts +184 -0
- package/dist/server/application/scheduled-task-service.js +391 -0
- package/dist/server/application/session-orchestrator-service.d.ts +388 -0
- package/dist/server/application/session-orchestrator-service.js +1146 -0
- package/dist/server/application/session-reminder-service.d.ts +59 -0
- package/dist/server/application/session-reminder-service.js +141 -0
- package/dist/server/application/session-watchdog.d.ts +171 -0
- package/dist/server/application/session-watchdog.js +284 -0
- package/dist/server/application/workspace-service.d.ts +52 -0
- package/dist/server/application/workspace-service.js +186 -0
- package/dist/server/application/worktree-service.d.ts +13 -0
- package/dist/server/application/worktree-service.js +76 -0
- package/dist/server/browser-access.d.ts +34 -0
- package/dist/server/browser-access.js +89 -0
- package/dist/server/capability.d.ts +105 -0
- package/dist/server/capability.js +170 -0
- package/dist/server/command-handler.d.ts +93 -0
- package/dist/server/command-handler.js +540 -0
- package/dist/server/content-search.d.ts +16 -0
- package/dist/server/content-search.js +235 -0
- package/dist/server/conversation-usage.d.ts +22 -0
- package/dist/server/conversation-usage.js +43 -0
- package/dist/server/documents.d.ts +152 -0
- package/dist/server/documents.js +998 -0
- package/dist/server/engineering-learning.d.ts +30 -0
- package/dist/server/engineering-learning.js +106 -0
- package/dist/server/exec-service.d.ts +45 -0
- package/dist/server/exec-service.js +167 -0
- package/dist/server/explicit-file-open.d.ts +34 -0
- package/dist/server/explicit-file-open.js +105 -0
- package/dist/server/failure-text.d.ts +18 -0
- package/dist/server/failure-text.js +44 -0
- package/dist/server/feishu-cli.d.ts +223 -0
- package/dist/server/feishu-cli.js +464 -0
- package/dist/server/file-changes.d.ts +119 -0
- package/dist/server/file-changes.js +280 -0
- package/dist/server/file-diff.d.ts +41 -0
- package/dist/server/file-diff.js +97 -0
- package/dist/server/file-index.d.ts +100 -0
- package/dist/server/file-index.js +296 -0
- package/dist/server/file-name-index.d.ts +160 -0
- package/dist/server/file-name-index.js +411 -0
- package/dist/server/files.d.ts +269 -0
- package/dist/server/files.js +748 -0
- package/dist/server/git.d.ts +245 -0
- package/dist/server/git.js +903 -0
- package/dist/server/http-server.d.ts +45 -0
- package/dist/server/http-server.js +3830 -0
- package/dist/server/index.d.ts +1 -0
- package/dist/server/index.js +485 -0
- package/dist/server/inline-thinking.d.ts +90 -0
- package/dist/server/inline-thinking.js +181 -0
- package/dist/server/json-comments.d.ts +14 -0
- package/dist/server/json-comments.js +71 -0
- package/dist/server/knowledge-embedding-worker.d.ts +1 -0
- package/dist/server/knowledge-embedding-worker.js +27 -0
- package/dist/server/knowledge-vectors.d.ts +150 -0
- package/dist/server/knowledge-vectors.js +385 -0
- package/dist/server/knowledge.d.ts +273 -0
- package/dist/server/knowledge.js +848 -0
- package/dist/server/length-continuation.d.ts +83 -0
- package/dist/server/length-continuation.js +118 -0
- package/dist/server/logging.d.ts +32 -0
- package/dist/server/logging.js +185 -0
- package/dist/server/managed-http-tunnel.d.ts +6 -0
- package/dist/server/managed-http-tunnel.js +85 -0
- package/dist/server/managed-http.d.ts +4 -0
- package/dist/server/managed-http.js +85 -0
- package/dist/server/managed-model-adapter.d.ts +17 -0
- package/dist/server/managed-model-adapter.js +75 -0
- package/dist/server/managed-model-gateway.d.ts +49 -0
- package/dist/server/managed-model-gateway.js +73 -0
- package/dist/server/managed-servers.d.ts +55 -0
- package/dist/server/managed-servers.js +396 -0
- package/dist/server/managed-ssh.d.ts +26 -0
- package/dist/server/managed-ssh.js +73 -0
- package/dist/server/manager-vault.d.ts +11 -0
- package/dist/server/manager-vault.js +67 -0
- package/dist/server/memory-recall.d.ts +25 -0
- package/dist/server/memory-recall.js +45 -0
- package/dist/server/memory.d.ts +67 -0
- package/dist/server/memory.js +216 -0
- package/dist/server/message-media.d.ts +7 -0
- package/dist/server/message-media.js +54 -0
- package/dist/server/model-context-window.d.ts +186 -0
- package/dist/server/model-context-window.js +444 -0
- package/dist/server/model-filter.d.ts +23 -0
- package/dist/server/model-filter.js +48 -0
- package/dist/server/models-store.d.ts +36 -0
- package/dist/server/models-store.js +132 -0
- package/dist/server/native-tool-settings.d.ts +25 -0
- package/dist/server/native-tool-settings.js +41 -0
- package/dist/server/native-tools-extension.d.ts +2 -0
- package/dist/server/native-tools-extension.js +39 -0
- package/dist/server/native-tools-service.d.ts +39 -0
- package/dist/server/native-tools-service.js +240 -0
- package/dist/server/persistence/app-state-repository.d.ts +65 -0
- package/dist/server/persistence/app-state-repository.js +1 -0
- package/dist/server/persistence/json-state-repository.d.ts +14 -0
- package/dist/server/persistence/json-state-repository.js +43 -0
- package/dist/server/persistence/open-state-store.d.ts +3 -0
- package/dist/server/persistence/open-state-store.js +35 -0
- package/dist/server/persistence/rename.d.ts +22 -0
- package/dist/server/persistence/rename.js +44 -0
- package/dist/server/persistence/sqlite-state-repository.d.ts +12 -0
- package/dist/server/persistence/sqlite-state-repository.js +264 -0
- package/dist/server/pi-command.d.ts +94 -0
- package/dist/server/pi-command.js +265 -0
- package/dist/server/pi-native.d.ts +12 -0
- package/dist/server/pi-native.js +16 -0
- package/dist/server/pi-session-events.d.ts +158 -0
- package/dist/server/pi-session-events.js +373 -0
- package/dist/server/pi-session-pool.d.ts +233 -0
- package/dist/server/pi-session-pool.js +382 -0
- package/dist/server/pi-session.d.ts +642 -0
- package/dist/server/pi-session.js +655 -0
- package/dist/server/presentations/schema.d.ts +120 -0
- package/dist/server/presentations/schema.js +128 -0
- package/dist/server/provider-network.d.ts +16 -0
- package/dist/server/provider-network.js +106 -0
- package/dist/server/provider-proxy.d.ts +18 -0
- package/dist/server/provider-proxy.js +200 -0
- package/dist/server/providers.d.ts +333 -0
- package/dist/server/providers.js +989 -0
- package/dist/server/remote-bootstrap.d.ts +17 -0
- package/dist/server/remote-bootstrap.js +119 -0
- package/dist/server/rpc-channel.d.ts +131 -0
- package/dist/server/rpc-channel.js +135 -0
- package/dist/server/runtime.d.ts +1393 -0
- package/dist/server/runtime.js +3286 -0
- package/dist/server/scheduled-tasks.d.ts +101 -0
- package/dist/server/scheduled-tasks.js +138 -0
- package/dist/server/server-context.d.ts +119 -0
- package/dist/server/server-context.js +1 -0
- package/dist/server/session-cwd.d.ts +19 -0
- package/dist/server/session-cwd.js +58 -0
- package/dist/server/session-ledger.d.ts +67 -0
- package/dist/server/session-ledger.js +76 -0
- package/dist/server/session-memory-scope.d.ts +4 -0
- package/dist/server/session-memory-scope.js +30 -0
- package/dist/server/session-repository.d.ts +100 -0
- package/dist/server/session-repository.js +246 -0
- package/dist/server/session-tree-scope.d.ts +125 -0
- package/dist/server/session-tree-scope.js +337 -0
- package/dist/server/skills.d.ts +23 -0
- package/dist/server/skills.js +190 -0
- package/dist/server/store.d.ts +1062 -0
- package/dist/server/store.js +3305 -0
- package/dist/server/subagent-runs.d.ts +185 -0
- package/dist/server/subagent-runs.js +383 -0
- package/dist/server/subagents.d.ts +200 -0
- package/dist/server/subagents.js +619 -0
- package/dist/server/task-tool.d.ts +282 -0
- package/dist/server/task-tool.js +507 -0
- package/dist/server/tavily.d.ts +32 -0
- package/dist/server/tavily.js +136 -0
- package/dist/server/team-context.d.ts +12 -0
- package/dist/server/team-context.js +180 -0
- package/dist/server/terminal.d.ts +163 -0
- package/dist/server/terminal.js +557 -0
- package/dist/server/thinking-level-discovery.d.ts +38 -0
- package/dist/server/thinking-level-discovery.js +183 -0
- package/dist/server/time-zone.d.ts +20 -0
- package/dist/server/time-zone.js +32 -0
- package/dist/server/tool-calls.d.ts +130 -0
- package/dist/server/tool-calls.js +341 -0
- package/dist/server/tool-llm.d.ts +72 -0
- package/dist/server/tool-llm.js +146 -0
- package/dist/server/tool-registry.d.ts +129 -0
- package/dist/server/tool-registry.js +249 -0
- package/dist/server/tool-runner.d.ts +112 -0
- package/dist/server/tool-runner.js +306 -0
- package/dist/server/turn-failure.d.ts +48 -0
- package/dist/server/turn-failure.js +148 -0
- package/dist/server/types.d.ts +2315 -0
- package/dist/server/types.js +11 -0
- package/dist/server/update-status.d.ts +14 -0
- package/dist/server/update-status.js +69 -0
- package/dist/server/update.d.ts +46 -0
- package/dist/server/update.js +104 -0
- package/dist/server/upload.d.ts +35 -0
- package/dist/server/upload.js +177 -0
- package/dist/server/version.d.ts +33 -0
- package/dist/server/version.js +111 -0
- package/dist/server/web.d.ts +27 -0
- package/dist/server/web.js +183 -0
- package/dist/server/workflow-engine.d.ts +47 -0
- package/dist/server/workflow-engine.js +255 -0
- package/dist/server/workflow.d.ts +329 -0
- package/dist/server/workflow.js +469 -0
- package/dist/server/workspace-file-indexes.d.ts +2 -0
- package/dist/server/workspace-file-indexes.js +61 -0
- package/dist/server/workspace-file-operations.d.ts +36 -0
- package/dist/server/workspace-file-operations.js +281 -0
- package/dist/server/workspace-file-response.d.ts +7 -0
- package/dist/server/workspace-file-response.js +79 -0
- package/dist/server/ws-server.d.ts +149 -0
- package/dist/server/ws-server.js +640 -0
- package/dist/shared/agent-order.d.ts +44 -0
- package/dist/shared/agent-order.js +50 -0
- package/dist/shared/archive-decisions.d.ts +44 -0
- package/dist/shared/archive-decisions.js +98 -0
- package/dist/shared/background-task.d.ts +25 -0
- package/dist/shared/background-task.js +183 -0
- package/dist/shared/child-session-lock.d.ts +37 -0
- package/dist/shared/child-session-lock.js +59 -0
- package/dist/shared/content-search.d.ts +22 -0
- package/dist/shared/content-search.js +1 -0
- package/dist/shared/conversation-title.d.ts +4 -0
- package/dist/shared/conversation-title.js +4 -0
- package/dist/shared/file-mentions.d.ts +17 -0
- package/dist/shared/file-mentions.js +91 -0
- package/dist/shared/git-commit.d.ts +11 -0
- package/dist/shared/git-commit.js +1 -0
- package/dist/shared/git-graph.d.ts +42 -0
- package/dist/shared/git-graph.js +30 -0
- package/dist/shared/group-chat.d.ts +132 -0
- package/dist/shared/group-chat.js +6 -0
- package/dist/shared/managed-servers.d.ts +36 -0
- package/dist/shared/managed-servers.js +1 -0
- package/dist/shared/message-media.d.ts +12 -0
- package/dist/shared/message-media.js +27 -0
- package/dist/shared/model-window-label.d.ts +39 -0
- package/dist/shared/model-window-label.js +55 -0
- package/dist/shared/native-tools.d.ts +43 -0
- package/dist/shared/native-tools.js +1 -0
- package/dist/shared/pi-cache-warming.d.ts +9 -0
- package/dist/shared/pi-cache-warming.js +4 -0
- package/dist/shared/project-overview.d.ts +107 -0
- package/dist/shared/project-overview.js +92 -0
- package/dist/shared/project-presentation.d.ts +55 -0
- package/dist/shared/project-presentation.js +111 -0
- package/dist/shared/provider-network.d.ts +30 -0
- package/dist/shared/provider-network.js +1 -0
- package/dist/shared/session-reporting.d.ts +40 -0
- package/dist/shared/session-reporting.js +97 -0
- package/dist/shared/session-search.d.ts +14 -0
- package/dist/shared/session-search.js +1 -0
- package/dist/shared/team-context-view.d.ts +33 -0
- package/dist/shared/team-context-view.js +49 -0
- package/dist/shared/team-context.d.ts +59 -0
- package/dist/shared/team-context.js +3 -0
- package/dist/shared/terminal-protocol.d.ts +71 -0
- package/dist/shared/terminal-protocol.js +1 -0
- package/dist/shared/thinking-levels.d.ts +53 -0
- package/dist/shared/thinking-levels.js +85 -0
- package/dist/shared/tool-glyph.d.ts +7 -0
- package/dist/shared/tool-glyph.js +19 -0
- package/dist/shared/transcript-order.d.ts +7 -0
- package/dist/shared/transcript-order.js +12 -0
- package/dist/shared/update-status.d.ts +28 -0
- package/dist/shared/update-status.js +1 -0
- package/dist/shared/workspace-file-preview.d.ts +4 -0
- package/dist/shared/workspace-file-preview.js +28 -0
- package/package.json +450 -4
- package/scripts/diagnose.mjs +603 -0
- package/scripts/patch-brace-expansion.mjs +260 -0
- package/scripts/restart-server.mjs +104 -0
- package/skills/presentation/SKILL.md +30 -0
- package/skills/presentation/references/data.md +10 -0
- package/skills/presentation/references/quality.md +12 -0
- package/skills/presentation/references/story.md +12 -0
- package/skills/presentation/references/templates-and-editing.md +11 -0
- package/skills/presentation/references/visual-system.md +10 -0
- package/skills/super-pi-agent-development/SKILL.md +25 -0
- package/skills/super-pi-development/SKILL.md +31 -0
- package/skills/super-pi-tool-development/SKILL.md +39 -0
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import type { AppState, Conversation, Message, SubagentRun } from "../types.js";
|
|
2
|
+
/** A durable atomic update to a single streaming entity. */
|
|
3
|
+
export type StateTransaction = {
|
|
4
|
+
kind: "message.replace";
|
|
5
|
+
conversationId: string;
|
|
6
|
+
message: Message;
|
|
7
|
+
updatedAt: string;
|
|
8
|
+
} | {
|
|
9
|
+
kind: "subagent.replace";
|
|
10
|
+
run: SubagentRun;
|
|
11
|
+
} | {
|
|
12
|
+
kind: "collections.replace";
|
|
13
|
+
values: Partial<Pick<AppState, "workflows" | "backgroundTasks" | "scheduledTasks" | "projects">>;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Removes one collection row entirely.
|
|
17
|
+
*
|
|
18
|
+
* It exists because "this collection is empty" is stored as the ABSENCE of the key, not as an empty
|
|
19
|
+
* array: a fresh install must not claim the user considered a schedule and found none. A repository
|
|
20
|
+
* whose `collections.replace` can only write rows therefore cannot express "there are none" - it can
|
|
21
|
+
* only write `[]`, which is a different claim.
|
|
22
|
+
*/
|
|
23
|
+
| {
|
|
24
|
+
kind: "collections.remove";
|
|
25
|
+
name: "workflows" | "backgroundTasks" | "scheduledTasks" | "projects";
|
|
26
|
+
} | {
|
|
27
|
+
kind: "conversation.metadata";
|
|
28
|
+
conversationId: string;
|
|
29
|
+
metadata: Omit<AppState["conversations"][number], "messages">;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* A rewrite of one conversation's whole message array, for the changes that are about the ARRAY rather
|
|
33
|
+
* than about one message's content: a waiting message being added, claimed, released, reordered,
|
|
34
|
+
* edited or withdrawn, and an assistant message being spliced in after the message it answers.
|
|
35
|
+
*
|
|
36
|
+
* It exists because of where the waiting list moved. Those used to be `collections.replace` writes
|
|
37
|
+
* against a `queuedMessages` array that sat outside every conversation, so they never touched the
|
|
38
|
+
* transcript - which is precisely why a steered message could be delivered to the model and leave no
|
|
39
|
+
* trace in the conversation the user was reading. A waiting message IS a message now, so the write is
|
|
40
|
+
* about messages, and it is still its own kind because the array is rewritten as a unit while
|
|
41
|
+
* `message.replace` deliberately never sees a list.
|
|
42
|
+
*
|
|
43
|
+
* `updatedAt` travels WITH the array because opening a turn changes it, and this is the only write that
|
|
44
|
+
* could record that. Leaving it out and clearing the timestamp overlay instead - the shape
|
|
45
|
+
* `conversation.metadata` uses, where the metadata is the payload - would make a reopened conversation
|
|
46
|
+
* report the timestamp of whatever the base snapshot happened to hold, which is a value the server
|
|
47
|
+
* already decided was wrong.
|
|
48
|
+
*/
|
|
49
|
+
| {
|
|
50
|
+
kind: "conversation.messages";
|
|
51
|
+
conversationId: string;
|
|
52
|
+
messages: Message[];
|
|
53
|
+
updatedAt: string;
|
|
54
|
+
acceptedSendId?: string;
|
|
55
|
+
metadata?: Omit<Conversation, "messages">;
|
|
56
|
+
};
|
|
57
|
+
/** Durable backing store for the application's in-memory state snapshot. */
|
|
58
|
+
export interface AppStateRepository {
|
|
59
|
+
load(): Promise<AppState>;
|
|
60
|
+
save(state: AppState): Promise<void>;
|
|
61
|
+
/** Optional incremental path. Implementations without it retain full snapshot saves. */
|
|
62
|
+
commit?(transaction: StateTransaction): Promise<void>;
|
|
63
|
+
flush(): Promise<void>;
|
|
64
|
+
close?(): void;
|
|
65
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { AppState } from "../types.js";
|
|
2
|
+
import type { AppStateRepository } from "./app-state-repository.js";
|
|
3
|
+
/** Compatible, atomic state.json repository; the replacement seam for SQLite. */
|
|
4
|
+
export declare class JsonStateRepository implements AppStateRepository {
|
|
5
|
+
readonly filePath: string;
|
|
6
|
+
private writeQueue;
|
|
7
|
+
/** The error of the most recent FAILED write, cleared by the next successful one. Read by `flush`. */
|
|
8
|
+
private lastWriteError;
|
|
9
|
+
constructor(filePath: string);
|
|
10
|
+
load(): Promise<AppState>;
|
|
11
|
+
save(state: AppState): Promise<void>;
|
|
12
|
+
flush(): Promise<void>;
|
|
13
|
+
}
|
|
14
|
+
export declare const defaultJsonStatePath: (dataDir: string) => string;
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { mkdir, readFile, writeFile } from "node:fs/promises";
|
|
2
|
+
import { replaceFile } from "./rename.js";
|
|
3
|
+
import { dirname, join } from "node:path";
|
|
4
|
+
/** Compatible, atomic state.json repository; the replacement seam for SQLite. */
|
|
5
|
+
export class JsonStateRepository {
|
|
6
|
+
filePath;
|
|
7
|
+
writeQueue = Promise.resolve();
|
|
8
|
+
/** The error of the most recent FAILED write, cleared by the next successful one. Read by `flush`. */
|
|
9
|
+
lastWriteError;
|
|
10
|
+
constructor(filePath) {
|
|
11
|
+
this.filePath = filePath;
|
|
12
|
+
}
|
|
13
|
+
async load() { return JSON.parse(await readFile(this.filePath, "utf8")); }
|
|
14
|
+
async save(state) {
|
|
15
|
+
const write = this.writeQueue.then(async () => {
|
|
16
|
+
try {
|
|
17
|
+
await mkdir(dirname(this.filePath), { recursive: true });
|
|
18
|
+
const temporary = `${this.filePath}.tmp`;
|
|
19
|
+
await writeFile(temporary, `${JSON.stringify(state, null, 2)}\n`, "utf8");
|
|
20
|
+
await replaceFile(temporary, this.filePath);
|
|
21
|
+
this.lastWriteError = undefined;
|
|
22
|
+
}
|
|
23
|
+
catch (error) {
|
|
24
|
+
this.lastWriteError = error;
|
|
25
|
+
throw error;
|
|
26
|
+
}
|
|
27
|
+
});
|
|
28
|
+
// The queue keeps swallowing: one failed write must not poison every write queued behind it.
|
|
29
|
+
// The failure is remembered above, and `flush` is what reports it.
|
|
30
|
+
this.writeQueue = write.catch(() => { });
|
|
31
|
+
await write;
|
|
32
|
+
}
|
|
33
|
+
async flush() {
|
|
34
|
+
await this.writeQueue;
|
|
35
|
+
if (this.lastWriteError !== undefined) {
|
|
36
|
+
const error = this.lastWriteError;
|
|
37
|
+
this.lastWriteError = undefined;
|
|
38
|
+
console.error(`Super Pi: the last state write to ${this.filePath} failed (${error instanceof Error ? error.message : String(error)}) - the state on disk may be older than the state in memory.`);
|
|
39
|
+
throw error;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
export const defaultJsonStatePath = (dataDir) => join(dataDir, "state.json");
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { mkdir } from "node:fs/promises";
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
import { Store, defaultStorePath } from "../store.js";
|
|
4
|
+
import { JsonStateRepository } from "./json-state-repository.js";
|
|
5
|
+
import { SqliteStateRepository } from "./sqlite-state-repository.js";
|
|
6
|
+
/** Opens the default SQLite store, importing legacy JSON only into an empty DB. */
|
|
7
|
+
export async function openStateStore(dataDir, backend = "sqlite") {
|
|
8
|
+
if (backend === "json")
|
|
9
|
+
return Store.open(defaultStorePath(dataDir));
|
|
10
|
+
if (backend !== "sqlite")
|
|
11
|
+
throw new Error(`Unsupported persistence backend: ${backend}`);
|
|
12
|
+
await mkdir(dataDir, { recursive: true });
|
|
13
|
+
const repository = new SqliteStateRepository(join(dataDir, "state.sqlite"));
|
|
14
|
+
try {
|
|
15
|
+
try {
|
|
16
|
+
await repository.load();
|
|
17
|
+
}
|
|
18
|
+
catch (error) {
|
|
19
|
+
if (error.code !== "ENOENT")
|
|
20
|
+
throw error;
|
|
21
|
+
try {
|
|
22
|
+
await repository.save(await new JsonStateRepository(defaultStorePath(dataDir)).load());
|
|
23
|
+
}
|
|
24
|
+
catch (importError) {
|
|
25
|
+
if (importError.code !== "ENOENT")
|
|
26
|
+
throw importError;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
return await Store.openRepository(repository);
|
|
30
|
+
}
|
|
31
|
+
catch (error) {
|
|
32
|
+
repository.close();
|
|
33
|
+
throw error;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { rename } from "node:fs/promises";
|
|
2
|
+
/**
|
|
3
|
+
* Renames `from` over `to`, retrying the WINDOWS SHARING VIOLATION.
|
|
4
|
+
*
|
|
5
|
+
* WHY THIS EXISTS, because the failure it fixes was a real defect and not a test artefact. On Windows a
|
|
6
|
+
* rename onto a file that ANY handle has open fails with EPERM/EACCES: the destination must be held with
|
|
7
|
+
* FILE_SHARE_DELETE, and a plain reader does not do that. Node's own `readFile` is such a reader, and so
|
|
8
|
+
* is every indexer, virus scanner and sync client on the machine. So `persist()` threw EPERM, the caller's
|
|
9
|
+
* mutation stayed only in memory, and the save was LOST - the settings dialog had already closed on send,
|
|
10
|
+
* so the person who pressed 保存 was told nothing and kept a default that had never been saved.
|
|
11
|
+
* scripts/workspace-settings-browser.test.mjs reproduced it at roughly 1 save in 5, and every occurrence
|
|
12
|
+
* read as "the save never arrived" because the refusal travelled to the browser as an error event the
|
|
13
|
+
* suite did not read, so a refused write and a slow write were indistinguishable for a whole day.
|
|
14
|
+
*
|
|
15
|
+
* MEASURED, so the bound is honest rather than hopeful: renaming onto a held-open `state.json` fails on
|
|
16
|
+
* the first attempt 40 times out of 40 and clears within one 5ms tick of the reader letting go (worst
|
|
17
|
+
* case 11 attempts). The lock lasts exactly as long as somebody is READING, which is why a bounded
|
|
18
|
+
* retry is the fix and not a longer sleep: the transient case is short, and a genuinely stuck file is
|
|
19
|
+
* not a sharing violation. A file that stays contended past the bound still throws, which is the honest
|
|
20
|
+
* outcome - the store reports a failed write instead of pretending it saved.
|
|
21
|
+
*/
|
|
22
|
+
export declare function replaceFile(from: string, to: string, move?: typeof rename): Promise<void>;
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { rename } from "node:fs/promises";
|
|
2
|
+
/**
|
|
3
|
+
* How long `replaceFile` keeps retrying a rename that lost a race with a reader. Two seconds is two
|
|
4
|
+
* orders of magnitude past the measured sub-50ms release, so it exists to absorb a loaded machine and a
|
|
5
|
+
* virus scanner mid-scan, not to wait out a lock that will never clear.
|
|
6
|
+
*/
|
|
7
|
+
const RENAME_RETRY_MS = 2_000;
|
|
8
|
+
const RENAME_RETRY_STEP_MS = 5;
|
|
9
|
+
/**
|
|
10
|
+
* Renames `from` over `to`, retrying the WINDOWS SHARING VIOLATION.
|
|
11
|
+
*
|
|
12
|
+
* WHY THIS EXISTS, because the failure it fixes was a real defect and not a test artefact. On Windows a
|
|
13
|
+
* rename onto a file that ANY handle has open fails with EPERM/EACCES: the destination must be held with
|
|
14
|
+
* FILE_SHARE_DELETE, and a plain reader does not do that. Node's own `readFile` is such a reader, and so
|
|
15
|
+
* is every indexer, virus scanner and sync client on the machine. So `persist()` threw EPERM, the caller's
|
|
16
|
+
* mutation stayed only in memory, and the save was LOST - the settings dialog had already closed on send,
|
|
17
|
+
* so the person who pressed 保存 was told nothing and kept a default that had never been saved.
|
|
18
|
+
* scripts/workspace-settings-browser.test.mjs reproduced it at roughly 1 save in 5, and every occurrence
|
|
19
|
+
* read as "the save never arrived" because the refusal travelled to the browser as an error event the
|
|
20
|
+
* suite did not read, so a refused write and a slow write were indistinguishable for a whole day.
|
|
21
|
+
*
|
|
22
|
+
* MEASURED, so the bound is honest rather than hopeful: renaming onto a held-open `state.json` fails on
|
|
23
|
+
* the first attempt 40 times out of 40 and clears within one 5ms tick of the reader letting go (worst
|
|
24
|
+
* case 11 attempts). The lock lasts exactly as long as somebody is READING, which is why a bounded
|
|
25
|
+
* retry is the fix and not a longer sleep: the transient case is short, and a genuinely stuck file is
|
|
26
|
+
* not a sharing violation. A file that stays contended past the bound still throws, which is the honest
|
|
27
|
+
* outcome - the store reports a failed write instead of pretending it saved.
|
|
28
|
+
*/
|
|
29
|
+
export async function replaceFile(from, to, move = rename) {
|
|
30
|
+
const deadline = Date.now() + RENAME_RETRY_MS;
|
|
31
|
+
for (;;) {
|
|
32
|
+
try {
|
|
33
|
+
await move(from, to);
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
catch (error) {
|
|
37
|
+
const code = error.code;
|
|
38
|
+
const contended = code === "EPERM" || code === "EACCES" || code === "EBUSY";
|
|
39
|
+
if (!contended || Date.now() >= deadline)
|
|
40
|
+
throw error;
|
|
41
|
+
await new Promise((resolve) => setTimeout(resolve, RENAME_RETRY_STEP_MS));
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { AppState } from "../types.js";
|
|
2
|
+
import type { AppStateRepository, StateTransaction } from "./app-state-repository.js";
|
|
3
|
+
export declare class SqliteStateRepository implements AppStateRepository {
|
|
4
|
+
private readonly db;
|
|
5
|
+
constructor(path: string);
|
|
6
|
+
load(): Promise<AppState>;
|
|
7
|
+
save(state: AppState): Promise<void>;
|
|
8
|
+
commit(transaction: StateTransaction): Promise<void>;
|
|
9
|
+
flush(): Promise<void>;
|
|
10
|
+
close(): void;
|
|
11
|
+
private atomic;
|
|
12
|
+
}
|
|
@@ -0,0 +1,264 @@
|
|
|
1
|
+
import { DatabaseSync } from "node:sqlite";
|
|
2
|
+
/** SQLite snapshot adapter with row-level writes for streamed messages and runs. */
|
|
3
|
+
/**
|
|
4
|
+
* THE ROW THAT SAYS "THIS CONVERSATION'S ARRAY IS EMPTY".
|
|
5
|
+
*
|
|
6
|
+
* A missing row in `message_order` used to mean two different things: "this conversation was never
|
|
7
|
+
* written incrementally" AND "this conversation's array was emptied". The loader could only read the
|
|
8
|
+
* first one, so a conversation whose LAST message was withdrawn fell back to the base snapshot - and
|
|
9
|
+
* the withdrawn message came back, carrying `delivery: "waiting"`, to be handed to the model by the
|
|
10
|
+
* next turn. A withdrawal is the last act of "take back what I have not sent yet", and it did not
|
|
11
|
+
* survive a restart (ledger TEST-041).
|
|
12
|
+
*
|
|
13
|
+
* A sentinel row says the difference. It is marked by a NEGATIVE POSITION - a real message's
|
|
14
|
+
* position is never negative - so it is never mistaken for one, and the loader reads it as "empty"
|
|
15
|
+
* rather than as "not written". (The marker is the position and NOT the id on purpose: a NUL in the
|
|
16
|
+
* id would be truncated by SQLite's TEXT handling, and the row would stop being recognisable.)
|
|
17
|
+
*/
|
|
18
|
+
const EMPTY_ARRAY_POSITION = -1;
|
|
19
|
+
/** The id that goes with the sentinel row. It never becomes a message: the loader drops every row
|
|
20
|
+
* whose position is negative, and this row's position IS negative. */
|
|
21
|
+
const EMPTY_ARRAY_SENTINEL_ID = "empty-array";
|
|
22
|
+
export class SqliteStateRepository {
|
|
23
|
+
db;
|
|
24
|
+
constructor(path) {
|
|
25
|
+
this.db = new DatabaseSync(path);
|
|
26
|
+
this.db.exec(`PRAGMA journal_mode=WAL; PRAGMA synchronous=FULL;
|
|
27
|
+
CREATE TABLE IF NOT EXISTS state (id INTEGER PRIMARY KEY CHECK(id=1), json TEXT NOT NULL);
|
|
28
|
+
CREATE TABLE IF NOT EXISTS messages (conversation TEXT NOT NULL, id TEXT NOT NULL, json TEXT NOT NULL, PRIMARY KEY(conversation,id));
|
|
29
|
+
/**
|
|
30
|
+
* Explicit transcript ORDER for a conversation, written by the conversation.messages transaction.
|
|
31
|
+
* Every other write path leaves message order in the base snapshot's array, which is fine as long as
|
|
32
|
+
* nothing ever REORDERS or REMOVES a message - and now something does: a waiting message can be
|
|
33
|
+
* moved within the group, or withdrawn, and both change which messages exist and in what sequence.
|
|
34
|
+
* Neither can be expressed by upserting a row per message id.
|
|
35
|
+
*
|
|
36
|
+
* It is AUTHORITATIVE once present, not a hint: load rebuilds that conversation's whole array from
|
|
37
|
+
* these rows rather than from the base json, because a withdrawn message is precisely one the base
|
|
38
|
+
* snapshot still contains and must NOT bring back. A conversation with no rows here is untouched and
|
|
39
|
+
* keeps reading its array from the snapshot, so this adds a code path rather than replacing one.
|
|
40
|
+
*/
|
|
41
|
+
CREATE TABLE IF NOT EXISTS message_order (conversation TEXT NOT NULL, id TEXT NOT NULL, position INTEGER NOT NULL, json TEXT NOT NULL, PRIMARY KEY(conversation,id));
|
|
42
|
+
CREATE TABLE IF NOT EXISTS timestamps (conversation TEXT PRIMARY KEY, stamp TEXT NOT NULL);
|
|
43
|
+
CREATE TABLE IF NOT EXISTS collections (name TEXT PRIMARY KEY, json TEXT NOT NULL);
|
|
44
|
+
CREATE TABLE IF NOT EXISTS metadata (conversation TEXT PRIMARY KEY, json TEXT NOT NULL);
|
|
45
|
+
CREATE TABLE IF NOT EXISTS send_receipts (conversation TEXT NOT NULL, id TEXT NOT NULL, PRIMARY KEY(conversation,id));
|
|
46
|
+
CREATE TABLE IF NOT EXISTS runs (id TEXT PRIMARY KEY, json TEXT NOT NULL);`);
|
|
47
|
+
}
|
|
48
|
+
async load() {
|
|
49
|
+
const row = this.db.prepare("SELECT json FROM state WHERE id=1").get();
|
|
50
|
+
if (!row)
|
|
51
|
+
throw Object.assign(new Error("State has not been initialized"), { code: "ENOENT" });
|
|
52
|
+
const state = JSON.parse(String(row.json));
|
|
53
|
+
for (const overlay of this.db.prepare("SELECT name,json FROM collections").all()) {
|
|
54
|
+
Object.assign(state, { [String(overlay.name)]: JSON.parse(String(overlay.json)) });
|
|
55
|
+
}
|
|
56
|
+
const messages = this.db.prepare("SELECT json FROM messages WHERE conversation=?");
|
|
57
|
+
if (!Array.isArray(state.conversations))
|
|
58
|
+
state.conversations = [];
|
|
59
|
+
for (const conversation of state.conversations) {
|
|
60
|
+
const baseReceipts = conversation.acceptedSendIds ?? [];
|
|
61
|
+
const metadata = this.db.prepare("SELECT json FROM metadata WHERE conversation=?").get(conversation.id);
|
|
62
|
+
if (metadata) {
|
|
63
|
+
const messages = conversation.messages;
|
|
64
|
+
for (const key of Object.keys(conversation))
|
|
65
|
+
Reflect.deleteProperty(conversation, key);
|
|
66
|
+
Object.assign(conversation, JSON.parse(String(metadata.json)), { messages });
|
|
67
|
+
}
|
|
68
|
+
const overrides = new Map(messages.all(conversation.id).map(r => {
|
|
69
|
+
const message = JSON.parse(String(r.json));
|
|
70
|
+
return [message.id, message];
|
|
71
|
+
}));
|
|
72
|
+
// An authoritative order row set means this conversation's transcript is rebuilt from it, rather
|
|
73
|
+
// than from the base snapshot's array. That is what makes a WITHDRAWN message stay withdrawn: the
|
|
74
|
+
// snapshot still lists it, and reading the array back would resurrect words the user took back.
|
|
75
|
+
//
|
|
76
|
+
// THE CONTENT COMES FROM THE `messages` TABLE WHEN IT HAS THE ROW, and only the ORDER comes from
|
|
77
|
+
// here. `message.replace` (the 50ms streaming checkpoint) writes that table alone, so preferring
|
|
78
|
+
// this table's copy of the JSON would reload a message at whatever content it had when the order
|
|
79
|
+
// was last rewritten - the placeholder an assistant message was born with, not what it streamed.
|
|
80
|
+
const ordered = this.db.prepare("SELECT id,position,json FROM message_order WHERE conversation=? ORDER BY position").all(conversation.id);
|
|
81
|
+
if (ordered.length) {
|
|
82
|
+
// Rows EXIST, so this conversation was written incrementally and these rows are authoritative -
|
|
83
|
+
// including the sentinel, which says the array is empty. The sentinel is not a message and
|
|
84
|
+
// never becomes one.
|
|
85
|
+
conversation.messages = ordered
|
|
86
|
+
.filter((row) => Number(row.position) >= 0)
|
|
87
|
+
.map((row) => overrides.get(String(row.id)) ?? JSON.parse(String(row.json)));
|
|
88
|
+
}
|
|
89
|
+
else {
|
|
90
|
+
if (!Array.isArray(conversation.messages))
|
|
91
|
+
conversation.messages = [];
|
|
92
|
+
conversation.messages = conversation.messages.map(message => overrides.get(message.id) ?? message);
|
|
93
|
+
}
|
|
94
|
+
// Timestamp overlays are stored beside the base conversation metadata.
|
|
95
|
+
const stamp = this.db.prepare("SELECT stamp FROM timestamps WHERE conversation=?").get(conversation.id);
|
|
96
|
+
if (stamp?.stamp)
|
|
97
|
+
conversation.updatedAt = String(stamp.stamp);
|
|
98
|
+
const receipts = this.db.prepare("SELECT id FROM send_receipts WHERE conversation=?").all(conversation.id);
|
|
99
|
+
if (baseReceipts.length || receipts.length) {
|
|
100
|
+
conversation.acceptedSendIds = [...new Set([...baseReceipts, ...(conversation.acceptedSendIds ?? []), ...receipts.map(item => String(item.id))])];
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
const runs = new Map(this.db.prepare("SELECT id,json FROM runs").all().map(r => [String(r.id), JSON.parse(String(r.json))]));
|
|
104
|
+
/**
|
|
105
|
+
* WHY THESE TWO ARE RE-CHECKED HERE, AND THE UPGRADE THEY PROTECT.
|
|
106
|
+
*
|
|
107
|
+
* `state.json` is the LEGACY store, and `openStateStore` imports one into an empty database verbatim.
|
|
108
|
+
* A state written by a Super Pi older than sub-agent dispatch has no `subagentRuns` key at all, so
|
|
109
|
+
* `state.subagentRuns.map` threw `Cannot read properties of undefined (reading 'map')` and the server
|
|
110
|
+
* exited 1 on its very first `load` - before the HTTP listener existed, so there was no page, no
|
|
111
|
+
* error in the UI, and nothing to retry. An upgrade that cannot start is the worst failure this
|
|
112
|
+
* project can ship.
|
|
113
|
+
*
|
|
114
|
+
* The same applies to `conversations` just above, for a state written before conversations existed.
|
|
115
|
+
* Both are defaulted to `[]` rather than repaired, because an absent collection is genuinely empty:
|
|
116
|
+
* there is nothing to invent, and `Store.openRepository` already reconciles a real (if empty) list.
|
|
117
|
+
*
|
|
118
|
+
* NOTE THAT NO BROWSER TEST COULD HAVE CAUGHT THIS. `scripts/run-check.mjs` runs the whole browser
|
|
119
|
+
* batch with `SUPER_PI_PERSISTENCE=json`, so every browser suite takes the JSON repository and the
|
|
120
|
+
* SQLite import path is never executed by the suite. It was found by running one of those suites
|
|
121
|
+
* standalone, which is the only way the SQLite default is ever used.
|
|
122
|
+
*/
|
|
123
|
+
if (!Array.isArray(state.subagentRuns))
|
|
124
|
+
state.subagentRuns = [];
|
|
125
|
+
state.subagentRuns = state.subagentRuns.map(run => runs.get(run.id) ?? run);
|
|
126
|
+
return state;
|
|
127
|
+
}
|
|
128
|
+
async save(state) {
|
|
129
|
+
this.atomic(() => {
|
|
130
|
+
this.db.prepare("INSERT INTO state VALUES(1,?) ON CONFLICT(id) DO UPDATE SET json=excluded.json").run(JSON.stringify(state));
|
|
131
|
+
this.db.exec("DELETE FROM messages; DELETE FROM message_order; DELETE FROM runs; DELETE FROM timestamps; DELETE FROM collections; DELETE FROM metadata; DELETE FROM send_receipts;");
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
async commit(transaction) {
|
|
135
|
+
this.atomic(() => {
|
|
136
|
+
if (transaction.kind === "message.replace") {
|
|
137
|
+
this.db.prepare("INSERT INTO messages VALUES(?,?,?) ON CONFLICT(conversation,id) DO UPDATE SET json=excluded.json")
|
|
138
|
+
.run(transaction.conversationId, transaction.message.id, JSON.stringify(transaction.message));
|
|
139
|
+
this.db.prepare("INSERT INTO timestamps VALUES(?,?) ON CONFLICT(conversation) DO UPDATE SET stamp=excluded.stamp")
|
|
140
|
+
.run(transaction.conversationId, transaction.updatedAt);
|
|
141
|
+
}
|
|
142
|
+
else if (transaction.kind === "subagent.replace") {
|
|
143
|
+
this.db.prepare("INSERT INTO runs VALUES(?,?) ON CONFLICT(id) DO UPDATE SET json=excluded.json")
|
|
144
|
+
.run(transaction.run.id, JSON.stringify(transaction.run));
|
|
145
|
+
}
|
|
146
|
+
else if (transaction.kind === "collections.replace") {
|
|
147
|
+
const write = this.db.prepare("INSERT INTO collections VALUES(?,?) ON CONFLICT(name) DO UPDATE SET json=excluded.json");
|
|
148
|
+
for (const [name, value] of Object.entries(transaction.values))
|
|
149
|
+
write.run(name, JSON.stringify(value));
|
|
150
|
+
}
|
|
151
|
+
else if (transaction.kind === "collections.remove") {
|
|
152
|
+
this.db.prepare("DELETE FROM collections WHERE name=?").run(transaction.name);
|
|
153
|
+
// AND THE BASE SNAPSHOT MUST STOP CARRYING IT TOO. Deleting only the overlay row was not
|
|
154
|
+
// enough: this loader treats an overlay as a PURE INCREMENT - "no row for this name" means
|
|
155
|
+
// "read it from the base snapshot" - so the base's stale array came back on the next load,
|
|
156
|
+
// and a scheduled task the user had deleted was resurrected and ran again on its old
|
|
157
|
+
// schedule (ledger TEST-038). "This collection is empty" is stored as the ABSENCE of the key
|
|
158
|
+
// (app-state-repository.ts), so the base has to lose the key as well - the row's deletion
|
|
159
|
+
// alone cannot express it, because the base is where the value would come from.
|
|
160
|
+
//
|
|
161
|
+
// The json backend has no incremental path and falls back to a whole-state save, which is
|
|
162
|
+
// why it was always right; that asymmetry is what makes this a defect and not a choice.
|
|
163
|
+
const base = this.db.prepare("SELECT json FROM state WHERE id=1").get();
|
|
164
|
+
if (base) {
|
|
165
|
+
const parsed = JSON.parse(String(base.json));
|
|
166
|
+
if (transaction.name in parsed) {
|
|
167
|
+
delete parsed[transaction.name];
|
|
168
|
+
this.db.prepare("UPDATE state SET json=? WHERE id=1").run(JSON.stringify(parsed));
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
else if (transaction.kind === "conversation.messages") {
|
|
173
|
+
// A DIFF, NOT A REWRITE. This used to DELETE the conversation's rows and re-INSERT every
|
|
174
|
+
// message - stringified twice, once per table - on every single send, under
|
|
175
|
+
// `synchronous=FULL`. That is O(transcript) work in front of the broadcast that tells the
|
|
176
|
+
// browser a turn has started, so a long conversation showed 「就绪」 for seconds after the
|
|
177
|
+
// click. A send changes ONE message (the appended user row); the rest are already on disk,
|
|
178
|
+
// byte for byte.
|
|
179
|
+
//
|
|
180
|
+
// THE ORDER TABLE IS STILL AUTHORITATIVE FOR ORDER, and its presence is what makes a
|
|
181
|
+
// withdrawn message stay withdrawn (the base snapshot still lists it). When it has no rows
|
|
182
|
+
// yet, the base array is authoritative - that is the `load` contract - so this falls back to
|
|
183
|
+
// the full write ONCE to materialise the order, and every later write is a diff.
|
|
184
|
+
const conversationId = transaction.conversationId;
|
|
185
|
+
if (transaction.acceptedSendId) {
|
|
186
|
+
this.db.prepare("INSERT OR IGNORE INTO send_receipts VALUES(?,?)").run(conversationId, transaction.acceptedSendId);
|
|
187
|
+
}
|
|
188
|
+
if (transaction.metadata) {
|
|
189
|
+
this.db.prepare("INSERT INTO metadata VALUES(?,?) ON CONFLICT(conversation) DO UPDATE SET json=excluded.json")
|
|
190
|
+
.run(conversationId, JSON.stringify(transaction.metadata));
|
|
191
|
+
}
|
|
192
|
+
const stored = new Map(this.db.prepare("SELECT id,json FROM messages WHERE conversation=?").all(conversationId)
|
|
193
|
+
.map((row) => [String(row.id), String(row.json)]));
|
|
194
|
+
const ordered = this.db.prepare("SELECT id,position FROM message_order WHERE conversation=? ORDER BY position").all(conversationId);
|
|
195
|
+
const upsertMessage = this.db.prepare("INSERT INTO messages VALUES(?,?,?) ON CONFLICT(conversation,id) DO UPDATE SET json=excluded.json");
|
|
196
|
+
if (ordered.length === 0) {
|
|
197
|
+
const insertOrdered = this.db.prepare("INSERT INTO message_order VALUES(?,?,?,?)");
|
|
198
|
+
transaction.messages.forEach((message, position) => {
|
|
199
|
+
const json = JSON.stringify(message);
|
|
200
|
+
upsertMessage.run(conversationId, message.id, json);
|
|
201
|
+
insertOrdered.run(conversationId, message.id, position, json);
|
|
202
|
+
});
|
|
203
|
+
// An EMPTY ARRAY MUST BE WRITTEN DOWN, or "no rows" will read as "never written" and the
|
|
204
|
+
// loader will fall back to the base snapshot - which still holds the message the person
|
|
205
|
+
// just withdrew (ledger TEST-041).
|
|
206
|
+
if (transaction.messages.length === 0) {
|
|
207
|
+
insertOrdered.run(conversationId, EMPTY_ARRAY_SENTINEL_ID, EMPTY_ARRAY_POSITION, "[]");
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
else {
|
|
211
|
+
const nextIds = new Set(transaction.messages.map((message) => message.id));
|
|
212
|
+
const deleteMessage = this.db.prepare("DELETE FROM messages WHERE conversation=? AND id=?");
|
|
213
|
+
const deleteOrder = this.db.prepare("DELETE FROM message_order WHERE conversation=? AND id=?");
|
|
214
|
+
for (const row of ordered) {
|
|
215
|
+
if (nextIds.has(String(row.id)))
|
|
216
|
+
continue;
|
|
217
|
+
deleteMessage.run(conversationId, row.id);
|
|
218
|
+
deleteOrder.run(conversationId, row.id);
|
|
219
|
+
}
|
|
220
|
+
const upsertOrder = this.db.prepare("INSERT INTO message_order VALUES(?,?,?,?) ON CONFLICT(conversation,id) DO UPDATE SET position=excluded.position, json=excluded.json");
|
|
221
|
+
const positions = new Map(ordered.map((row) => [String(row.id), Number(row.position)]));
|
|
222
|
+
transaction.messages.forEach((message, position) => {
|
|
223
|
+
const json = JSON.stringify(message);
|
|
224
|
+
if (stored.get(message.id) !== json)
|
|
225
|
+
upsertMessage.run(conversationId, message.id, json);
|
|
226
|
+
// An unchanged message at an unchanged position needs NO write at all. A pure append
|
|
227
|
+
// therefore costs exactly one message row plus one order row.
|
|
228
|
+
if (positions.get(message.id) !== position)
|
|
229
|
+
upsertOrder.run(conversationId, message.id, position, json);
|
|
230
|
+
});
|
|
231
|
+
// The LAST MESSAGE GONE leaves the order table with no rows, and no rows read as "never
|
|
232
|
+
// written" - so the loader would fall back to the base snapshot and hand the withdrawn
|
|
233
|
+
// message back to the model. The sentinel says the array is empty (ledger TEST-041).
|
|
234
|
+
if (transaction.messages.length === 0) {
|
|
235
|
+
upsertOrder.run(conversationId, EMPTY_ARRAY_SENTINEL_ID, EMPTY_ARRAY_POSITION, "[]");
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
// The stamp is WRITTEN, not cleared. Opening a turn moves `updatedAt`, and this is the only write
|
|
239
|
+
// that can record it; clearing the overlay instead would leave the reopened conversation reporting
|
|
240
|
+
// whatever the base snapshot held, which is the value the server had already decided was wrong.
|
|
241
|
+
this.db.prepare("INSERT INTO timestamps VALUES(?,?) ON CONFLICT(conversation) DO UPDATE SET stamp=excluded.stamp")
|
|
242
|
+
.run(transaction.conversationId, transaction.updatedAt);
|
|
243
|
+
}
|
|
244
|
+
else {
|
|
245
|
+
this.db.prepare("INSERT INTO metadata VALUES(?,?) ON CONFLICT(conversation) DO UPDATE SET json=excluded.json")
|
|
246
|
+
.run(transaction.conversationId, JSON.stringify(transaction.metadata));
|
|
247
|
+
this.db.prepare("DELETE FROM timestamps WHERE conversation=?").run(transaction.conversationId);
|
|
248
|
+
}
|
|
249
|
+
});
|
|
250
|
+
}
|
|
251
|
+
async flush() { }
|
|
252
|
+
close() { this.db.close(); }
|
|
253
|
+
atomic(work) {
|
|
254
|
+
this.db.exec("BEGIN IMMEDIATE");
|
|
255
|
+
try {
|
|
256
|
+
work();
|
|
257
|
+
this.db.exec("COMMIT");
|
|
258
|
+
}
|
|
259
|
+
catch (error) {
|
|
260
|
+
this.db.exec("ROLLBACK");
|
|
261
|
+
throw error;
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
}
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Where Pi is, or a plain statement that it is not here.
|
|
3
|
+
*
|
|
4
|
+
* WHY THIS IS NOT `process.env.SUPER_PI_PI_COMMAND ?? "pi.cmd"`. npm installs global CLIs into a
|
|
5
|
+
* directory that is NOT on many shells' PATH - on Windows `%APPDATA%\npm` is missing from a cron or
|
|
6
|
+
* service shell by default - so a bare `pi.cmd` spawn fails there while succeeding in an interactive
|
|
7
|
+
* shell. Same commit, same machine, opposite `npm run check` verdicts, and the failing line was
|
|
8
|
+
* cmd.exe's own message in the console's code page (`'pi.cmd' 不是内部或外部命令`), which reads as an
|
|
9
|
+
* encoding bug instead of a PATH one. Both the production spawn and the real-Pi probe resolve through
|
|
10
|
+
* this one function so the test cannot verify a resolution production does not use.
|
|
11
|
+
*
|
|
12
|
+
* "Not installed" and "installed but disagreeing" are DIFFERENT facts with different verdicts: the
|
|
13
|
+
* first is the environment, the second is the product. `found: false` is the first, and callers report
|
|
14
|
+
* it as a skip with a reason rather than as a product failure.
|
|
15
|
+
*/
|
|
16
|
+
export type PiCommandResolution = {
|
|
17
|
+
found: true;
|
|
18
|
+
command: string;
|
|
19
|
+
via: string; /** Whether the command was named explicitly rather than found. */
|
|
20
|
+
explicit: boolean;
|
|
21
|
+
} | {
|
|
22
|
+
found: false;
|
|
23
|
+
reason: string;
|
|
24
|
+
searched: string[]; /** Whether a command was NAMED and did not resolve. A named Pi is a statement about which Pi runs a turn, and it stays true when the file is missing. */
|
|
25
|
+
explicit?: boolean;
|
|
26
|
+
};
|
|
27
|
+
/** Overridable so the resolution can be exercised for both platforms and both outcomes without a Pi install. */
|
|
28
|
+
export interface PiCommandOptions {
|
|
29
|
+
/** An explicit command (the runtime constructor's second argument). Wins over the environment. */
|
|
30
|
+
command?: string | undefined;
|
|
31
|
+
env?: Record<string, string | undefined>;
|
|
32
|
+
platform?: NodeJS.Platform;
|
|
33
|
+
exists?: (path: string) => boolean;
|
|
34
|
+
/**
|
|
35
|
+
* Where the "copy of Pi that came with Super Pi" is looked for, and how it is found.
|
|
36
|
+
*
|
|
37
|
+
* INJECTED, and the reason is the same reason `exists` is: the bundled lookup walks the REAL
|
|
38
|
+
* filesystem, so a test that asserts a resolution RULE ("PATH wins", "the global directory is
|
|
39
|
+
* searched") would otherwise be answered by whatever happens to be installed on the machine running
|
|
40
|
+
* the test. The rule being asserted has to be the only thing that decides the answer.
|
|
41
|
+
*
|
|
42
|
+
* The default walks up from this module's own location, which is the tree Super Pi was loaded from.
|
|
43
|
+
*/
|
|
44
|
+
bundled?: (exists: (path: string) => boolean, platform: NodeJS.Platform) => string | undefined;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Resolves the command that starts Pi: the explicit override, then PATH, then npm's global bin
|
|
48
|
+
* directories, and only then a report of every place it looked.
|
|
49
|
+
*
|
|
50
|
+
* An explicit override that does not exist is REPORTED rather than handed to the spawn, because a
|
|
51
|
+
* silent fallback would make a typo in `SUPER_PI_PI_COMMAND` look like a working Pi somewhere else.
|
|
52
|
+
*/
|
|
53
|
+
export declare function resolvePiCommand(options?: PiCommandOptions): PiCommandResolution;
|
|
54
|
+
/**
|
|
55
|
+
* The `pi` executable of the Pi package Super Pi depends on, if it is installed next to Super Pi.
|
|
56
|
+
*
|
|
57
|
+
* npm puts a dependency's binaries in `node_modules/.bin`, as a shell script on POSIX and as a `.cmd`
|
|
58
|
+
* shim on Windows. Two layouts have to be covered, because Super Pi runs in both:
|
|
59
|
+
*
|
|
60
|
+
* - INSTALLED: `<node_modules>/@evandu/super-pi/dist/server/pi-command.js`, and the shim is at
|
|
61
|
+
* `<node_modules>/.bin/pi` - the `.bin` of the tree that HOLDS Super Pi, two levels up.
|
|
62
|
+
* - A CHECKOUT: `<repo>/dist/server/pi-command.js`, and the shim is at `<repo>/node_modules/.bin/pi`
|
|
63
|
+
* - the `.bin` of Super Pi's OWN tree.
|
|
64
|
+
*
|
|
65
|
+
* So every level of the walk checks both `<dir>/.bin` and `<dir>/node_modules/.bin`, and the walk
|
|
66
|
+
* starts at this module's own location rather than at `process.cwd()`: the server is started from
|
|
67
|
+
* wherever the user happened to be, and the answer must not depend on that.
|
|
68
|
+
*/
|
|
69
|
+
export declare function bundledPiCommand(exists?: (path: string) => boolean, platform?: NodeJS.Platform): string | undefined;
|
|
70
|
+
/**
|
|
71
|
+
* Decodes a child process's output, falling back to the console code page when it is not UTF-8.
|
|
72
|
+
*
|
|
73
|
+
* A Windows `.cmd` shim runs under cmd.exe, which writes its own diagnostics in the console's code
|
|
74
|
+
* page - GBK on a Chinese Windows - so `chunk.toString()` renders `'pi.cmd' 不是内部或外部命令` as
|
|
75
|
+
* `'pi.cmd' ??????`. Reading those bytes as UTF-8 fatal first and retrying as GBK is what keeps the
|
|
76
|
+
* message readable, and therefore what keeps the next reader from mistaking a PATH problem for an
|
|
77
|
+
* encoding one.
|
|
78
|
+
*/
|
|
79
|
+
export declare function decodeProcessOutput(chunks: Iterable<Buffer>, fallbackCodePage?: string): string;
|
|
80
|
+
/**
|
|
81
|
+
* True when the text says the command itself was not found, which is a PATH or install problem rather
|
|
82
|
+
* than a rejection by Pi - the two are indistinguishable once the output is garbled, so the diagnosis
|
|
83
|
+
* is made on the decoded text rather than guessed from an exit code.
|
|
84
|
+
*/
|
|
85
|
+
export declare function looksLikeCommandNotFound(text: string): boolean;
|
|
86
|
+
/**
|
|
87
|
+
* Quotes a resolved command for `shell: true` spawns on Windows.
|
|
88
|
+
*
|
|
89
|
+
* npm's global bin directory can sit under a path with spaces (`C:\Program Files\nodejs`), and node
|
|
90
|
+
* joins a shell command out of argv WITHOUT quoting it, so an unquoted resolved path fails to launch
|
|
91
|
+
* on exactly the machine where PATH lookup did not already find Pi. A command with no spaces - the
|
|
92
|
+
* common case, and every fake launcher the tests use - is returned untouched.
|
|
93
|
+
*/
|
|
94
|
+
export declare function shellQuote(command: string, platform?: NodeJS.Platform): string;
|