@narumitw/pi-sync 0.61.0 → 0.63.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +25 -0
- package/README.md +46 -25
- package/dist/chunks/{chunk-277VHM2P.ts → chunk-27F5W7DX.ts} +7 -3
- package/dist/chunks/chunk-27F5W7DX.ts.map +7 -0
- package/dist/chunks/{chunk-ZIL6PW5L.ts → chunk-6YKOHBSC.ts} +2 -2
- package/dist/chunks/{chunk-OA4NZLSL.ts → chunk-APZVJAEW.ts} +4 -3
- package/dist/chunks/chunk-CKUGCBHP.ts +114 -0
- package/dist/chunks/chunk-CKUGCBHP.ts.map +7 -0
- package/dist/chunks/chunk-EJDDRS6O.ts +17 -0
- package/dist/chunks/chunk-EJDDRS6O.ts.map +7 -0
- package/dist/chunks/{chunk-JVFCVPPD.ts → chunk-EXDWEFIJ.ts} +87 -147
- package/dist/chunks/chunk-EXDWEFIJ.ts.map +7 -0
- package/dist/chunks/{chunk-4SR3ZYHA.ts → chunk-GELYN4UC.ts} +2 -2
- package/dist/chunks/{chunk-2ZGV5KNC.ts → chunk-HSCAFOT5.ts} +15 -12
- package/dist/chunks/chunk-HSCAFOT5.ts.map +7 -0
- package/dist/chunks/{chunk-YQSFJLJ3.ts → chunk-IBXEM35O.ts} +4 -3
- package/dist/chunks/{chunk-2JHAN3ZO.ts → chunk-JREGWM2B.ts} +3 -3
- package/dist/chunks/chunk-MANNB4WZ.ts +36 -0
- package/dist/chunks/chunk-MANNB4WZ.ts.map +7 -0
- package/dist/chunks/{chunk-EMRFVLZM.ts → chunk-NXWHPVXR.ts} +2 -2
- package/dist/chunks/{chunk-SMVKMYLK.ts → chunk-P4GI7LTT.ts} +2 -2
- package/dist/chunks/{chunk-ZVGRZD4C.ts → chunk-R3SAMXB6.ts} +2 -2
- package/dist/chunks/{chunk-4KTJLJSJ.ts → chunk-S6FUUWV5.ts} +148 -21
- package/dist/chunks/chunk-S6FUUWV5.ts.map +7 -0
- package/dist/chunks/{chunk-PJMM75W5.ts → chunk-SXCH4Q5O.ts} +41 -23
- package/dist/chunks/chunk-SXCH4Q5O.ts.map +7 -0
- package/dist/chunks/{chunk-RNIEVDTR.ts → chunk-T3LABNHF.ts} +2 -2
- package/dist/chunks/{chunk-E3YLRLJM.ts → chunk-UGS6RYDZ.ts} +14 -12
- package/dist/chunks/chunk-UGS6RYDZ.ts.map +7 -0
- package/dist/chunks/{chunk-S3R3W3KF.ts → chunk-UMPA5S7X.ts} +2 -2
- package/dist/chunks/{chunk-EMVZL4PH.ts → chunk-V76KCWHO.ts} +7 -5
- package/dist/chunks/{chunk-EMVZL4PH.ts.map → chunk-V76KCWHO.ts.map} +1 -1
- package/dist/chunks/{chunk-S7F4FOAS.ts → chunk-VQ47JBLF.ts} +5 -5
- package/dist/chunks/{chunk-WP2M67XZ.ts → chunk-WBIOSK76.ts} +2 -2
- package/dist/chunks/chunk-WFBHWYI6.ts +18 -0
- package/dist/chunks/chunk-WFBHWYI6.ts.map +7 -0
- package/dist/chunks/{chunk-HEB6YT64.ts → chunk-ZO52JM4H.ts} +2 -2
- package/dist/chunks/{command-handler-K4XJOGYC.ts → command-handler-VINMQ7ZM.ts} +26 -43
- package/dist/chunks/command-handler-VINMQ7ZM.ts.map +7 -0
- package/dist/chunks/{conflict-review-IGKY2TOJ.ts → conflict-review-PCDFRIVB.ts} +4 -4
- package/dist/chunks/{continuous-record-3XDXDOMV.ts → continuous-record-3SKJ6I3V.ts} +2 -2
- package/dist/chunks/{continuous-storage-HLKCHF6Z.ts → continuous-storage-3EU6I7Z3.ts} +2 -2
- package/dist/chunks/{file-selection-MGGVOKM7.ts → file-selection-KID23GAD.ts} +21 -14
- package/dist/chunks/file-selection-KID23GAD.ts.map +7 -0
- package/dist/chunks/manager-result-dispatcher-GDB74IOP.ts +20 -0
- package/dist/chunks/{manager-ui-KYQ5AZJS.ts → manager-ui-YHDECYKI.ts} +454 -167
- package/dist/chunks/manager-ui-YHDECYKI.ts.map +7 -0
- package/dist/chunks/remote-selection-ui-GPITNEBF.ts +19 -0
- package/dist/chunks/{setup-switch-D6K6PBPC.ts → setup-switch-L54X5U5I.ts} +7 -7
- package/dist/chunks/{setup-wizard-4DYUDHJR.ts → setup-wizard-YO4MCPUF.ts} +15 -11
- package/dist/chunks/{setup-wizard-4DYUDHJR.ts.map → setup-wizard-YO4MCPUF.ts.map} +1 -1
- package/dist/chunks/{snapshot-5HY3M2NL.ts → snapshot-ZUJJZCFW.ts} +2 -2
- package/dist/chunks/{snapshot-transaction-directory-OU2D7R7A.ts → snapshot-transaction-directory-F46JWRAU.ts} +2 -2
- package/dist/chunks/{state-directory-XXHU73Z3.ts → state-directory-CMAIF34J.ts} +2 -2
- package/dist/chunks/{sync-inspection-J3CRBORL.ts → sync-inspection-H2WX5SUT.ts} +5 -5
- package/dist/chunks/{sync-operations-BVWYYKH3.ts → sync-operations-2MZAYHL2.ts} +13 -9
- package/dist/chunks/sync-operations-2MZAYHL2.ts.map +7 -0
- package/dist/chunks/{sync-state-2LMIXJ2C.ts → sync-state-QIT64TKV.ts} +2 -2
- package/dist/daemon/LICENSE +21 -0
- package/dist/daemon/dist/build-version.js +8 -0
- package/dist/daemon/dist/child-sdk.mjs +17 -0
- package/dist/daemon/dist/cli.js +169 -0
- package/dist/daemon/dist/client.js +909 -0
- package/dist/daemon/dist/consent.js +383 -0
- package/dist/daemon/dist/document.js +6 -0
- package/dist/daemon/dist/endpoint.js +91 -0
- package/dist/daemon/dist/engine/backends/backend-factory.js +16 -0
- package/dist/daemon/dist/engine/backends/backend-identity.js +38 -0
- package/dist/daemon/dist/engine/backends/backend-types.js +1 -0
- package/dist/daemon/dist/engine/backends/git/git-backend.js +746 -0
- package/dist/daemon/dist/engine/backends/git/git-config.js +118 -0
- package/dist/daemon/dist/engine/backends/git/git-runner.js +232 -0
- package/dist/daemon/dist/engine/backends/git/git-storage.js +252 -0
- package/dist/daemon/dist/engine/backends/s3/s3-backend.js +444 -0
- package/dist/daemon/dist/engine/backends/s3/s3-client.js +352 -0
- package/dist/daemon/dist/engine/backends/s3/s3-conditions.js +66 -0
- package/dist/daemon/dist/engine/backends/snapshot-pointer.js +22 -0
- package/dist/daemon/dist/engine/backends/sync-backend.js +23 -0
- package/dist/daemon/dist/engine/backends/webdav/webdav-backend.js +542 -0
- package/dist/daemon/dist/engine/backends/webdav/webdav-client.js +326 -0
- package/dist/daemon/dist/engine/backends/webdav/webdav-config.js +73 -0
- package/dist/daemon/dist/engine/commands/command-types.js +1 -0
- package/dist/daemon/dist/engine/host.js +16 -0
- package/dist/daemon/dist/engine/ignore/effective-ignore-policy.js +33 -0
- package/dist/daemon/dist/engine/ignore/ignore-edit.js +102 -0
- package/dist/daemon/dist/engine/ignore/ignore-initialization.js +252 -0
- package/dist/daemon/dist/engine/ignore/ignore-migration-prompt.js +139 -0
- package/dist/daemon/dist/engine/ignore/ignore-settings.js +393 -0
- package/dist/daemon/dist/engine/ignore/ignore-source.js +250 -0
- package/dist/daemon/dist/engine/paths.js +71 -0
- package/dist/daemon/dist/engine/settings/config-errors.js +4 -0
- package/dist/daemon/dist/engine/settings/config-file.js +736 -0
- package/dist/daemon/dist/engine/settings/config.js +344 -0
- package/dist/daemon/dist/engine/settings/settings-management.js +199 -0
- package/dist/daemon/dist/engine/settings/settings-store.js +41 -0
- package/dist/daemon/dist/engine/settings/settings-types.js +1 -0
- package/dist/daemon/dist/engine/settings/settings-validation.js +384 -0
- package/dist/daemon/dist/engine/snapshot/session-paths.js +165 -0
- package/dist/daemon/dist/engine/snapshot/snapshot-apply.js +348 -0
- package/dist/daemon/dist/engine/snapshot/snapshot-bounds.js +20 -0
- package/dist/daemon/dist/engine/snapshot/snapshot-case-replacement.js +82 -0
- package/dist/daemon/dist/engine/snapshot/snapshot-codec.js +61 -0
- package/dist/daemon/dist/engine/snapshot/snapshot-entry-projection.js +78 -0
- package/dist/daemon/dist/engine/snapshot/snapshot-exclude-safety.js +262 -0
- package/dist/daemon/dist/engine/snapshot/snapshot-mutation-queues.js +133 -0
- package/dist/daemon/dist/engine/snapshot/snapshot-paths.js +11 -0
- package/dist/daemon/dist/engine/snapshot/snapshot-payload.js +10 -0
- package/dist/daemon/dist/engine/snapshot/snapshot-reviewed-preimages.js +60 -0
- package/dist/daemon/dist/engine/snapshot/snapshot-root-transition.js +138 -0
- package/dist/daemon/dist/engine/snapshot/snapshot-transaction-directory.js +16 -0
- package/dist/daemon/dist/engine/snapshot/snapshot-transaction-images.js +387 -0
- package/dist/daemon/dist/engine/snapshot/snapshot-transaction-plan.js +68 -0
- package/dist/daemon/dist/engine/snapshot/snapshot-transaction-policy.js +296 -0
- package/dist/daemon/dist/engine/snapshot/snapshot-transaction-stage.js +303 -0
- package/dist/daemon/dist/engine/snapshot/snapshot-transaction-verification.js +78 -0
- package/dist/daemon/dist/engine/snapshot/snapshot-transaction.js +855 -0
- package/dist/daemon/dist/engine/snapshot/snapshot-types.js +9 -0
- package/dist/daemon/dist/engine/snapshot/snapshot.js +678 -0
- package/dist/daemon/dist/engine/state/json-file.js +49 -0
- package/dist/daemon/dist/engine/state/lock-policy.js +2 -0
- package/dist/daemon/dist/engine/state/lock.js +259 -0
- package/dist/daemon/dist/engine/state/lockfile-fs.js +13 -0
- package/dist/daemon/dist/engine/state/merge-baseline-store.js +198 -0
- package/dist/daemon/dist/engine/state/mutation-directory-sync.js +34 -0
- package/dist/daemon/dist/engine/state/operation-availability.js +40 -0
- package/dist/daemon/dist/engine/state/state-directory.js +227 -0
- package/dist/daemon/dist/engine/state/state-types.js +1 -0
- package/dist/daemon/dist/engine/state/sync-state-store.js +54 -0
- package/dist/daemon/dist/engine/sync/agent-candidate.js +207 -0
- package/dist/daemon/dist/engine/sync/agent-plan.js +85 -0
- package/dist/daemon/dist/engine/sync/agent-policy.js +10 -0
- package/dist/daemon/dist/engine/sync/agent-refusal-retry.js +39 -0
- package/dist/daemon/dist/engine/sync/agent-request.js +13 -0
- package/dist/daemon/dist/engine/sync/agent-runner.js +183 -0
- package/dist/daemon/dist/engine/sync/agent-store.js +177 -0
- package/dist/daemon/dist/engine/sync/agent-sync.js +139 -0
- package/dist/daemon/dist/engine/sync/apply-review.js +52 -0
- package/dist/daemon/dist/engine/sync/conflict-artifact-ownership.js +53 -0
- package/dist/daemon/dist/engine/sync/conflict-artifacts.js +158 -0
- package/dist/daemon/dist/engine/sync/conflict-groups.js +76 -0
- package/dist/daemon/dist/engine/sync/conflict-preview.js +53 -0
- package/dist/daemon/dist/engine/sync/conflict-resolution.js +53 -0
- package/dist/daemon/dist/engine/sync/conflict-retention.js +61 -0
- package/dist/daemon/dist/engine/sync/conflict-review.js +73 -0
- package/dist/daemon/dist/engine/sync/content-conflicts.js +70 -0
- package/dist/daemon/dist/engine/sync/continuous-errors.js +66 -0
- package/dist/daemon/dist/engine/sync/continuous-lease.js +206 -0
- package/dist/daemon/dist/engine/sync/continuous-record.js +199 -0
- package/dist/daemon/dist/engine/sync/continuous-storage.js +59 -0
- package/dist/daemon/dist/engine/sync/continuous-sync.js +538 -0
- package/dist/daemon/dist/engine/sync/continuous-watch.js +140 -0
- package/dist/daemon/dist/engine/sync/exclude-policy.js +86 -0
- package/dist/daemon/dist/engine/sync/field-migration.js +20 -0
- package/dist/daemon/dist/engine/sync/file-merge-planner.js +123 -0
- package/dist/daemon/dist/engine/sync/initialization.js +80 -0
- package/dist/daemon/dist/engine/sync/json-document.js +172 -0
- package/dist/daemon/dist/engine/sync/local-field-policy.js +26 -0
- package/dist/daemon/dist/engine/sync/local-fields.js +196 -0
- package/dist/daemon/dist/engine/sync/merge-acceptance.js +29 -0
- package/dist/daemon/dist/engine/sync/merge-apply.js +532 -0
- package/dist/daemon/dist/engine/sync/merge-journal.js +314 -0
- package/dist/daemon/dist/engine/sync/merge-transfer-error.js +64 -0
- package/dist/daemon/dist/engine/sync/merged-sync.js +666 -0
- package/dist/daemon/dist/engine/sync/mutation-owner.js +18 -0
- package/dist/daemon/dist/engine/sync/package-merge.js +82 -0
- package/dist/daemon/dist/engine/sync/partial-progress.js +41 -0
- package/dist/daemon/dist/engine/sync/remote-snapshot.js +119 -0
- package/dist/daemon/dist/engine/sync/resolver-stream.js +34 -0
- package/dist/daemon/dist/engine/sync/session-merge.js +178 -0
- package/dist/daemon/dist/engine/sync/session-push.js +47 -0
- package/dist/daemon/dist/engine/sync/settings-conflicts.js +56 -0
- package/dist/daemon/dist/engine/sync/settings-merge.js +93 -0
- package/dist/daemon/dist/engine/sync/signals.js +8 -0
- package/dist/daemon/dist/engine/sync/startup-timings.js +44 -0
- package/dist/daemon/dist/engine/sync/sync-decision.js +81 -0
- package/dist/daemon/dist/engine/sync/sync-error-guidance.js +55 -0
- package/dist/daemon/dist/engine/sync/sync-errors.js +18 -0
- package/dist/daemon/dist/engine/sync/sync-inspection.js +42 -0
- package/dist/daemon/dist/engine/sync/sync-loaders.js +8 -0
- package/dist/daemon/dist/engine/sync/sync-local.js +40 -0
- package/dist/daemon/dist/engine/sync/sync-mutations.js +754 -0
- package/dist/daemon/dist/engine/sync/sync-operations.js +3 -0
- package/dist/daemon/dist/engine/sync/sync-policy.js +646 -0
- package/dist/daemon/dist/engine/sync/sync-queries.js +212 -0
- package/dist/daemon/dist/engine/sync/sync-state.js +124 -0
- package/dist/daemon/dist/engine/sync/text-merge.js +124 -0
- package/dist/daemon/dist/engine/sync/transfer-freshness.js +82 -0
- package/dist/daemon/dist/engine/types.js +2 -0
- package/dist/daemon/dist/engine/ui/merge-review.js +7 -0
- package/dist/daemon/dist/engine/ui/merged-sync-summary.js +86 -0
- package/dist/daemon/dist/engine/ui/sync-format.js +165 -0
- package/dist/daemon/dist/engine/ui/sync-status.js +30 -0
- package/dist/daemon/dist/engine/ui/terminal-text.js +9 -0
- package/dist/daemon/dist/engine/ui/transfer-changes.js +24 -0
- package/dist/daemon/dist/engine/ui/transfer-review.js +19 -0
- package/dist/daemon/dist/engine-service.js +441 -0
- package/dist/daemon/dist/identity.js +241 -0
- package/dist/daemon/dist/idle-exit.js +77 -0
- package/dist/daemon/dist/index.js +4 -0
- package/dist/daemon/dist/lifecycle-log.js +53 -0
- package/dist/daemon/dist/lifecycle.js +21 -0
- package/dist/daemon/dist/model-signature.js +7 -0
- package/dist/daemon/dist/monitor.js +324 -0
- package/dist/daemon/dist/participant-admission.js +30 -0
- package/dist/daemon/dist/protocol.js +104 -0
- package/dist/daemon/dist/resolver-host.js +78 -0
- package/dist/daemon/dist/server.js +994 -0
- package/dist/daemon/dist/service-host.js +175 -0
- package/dist/daemon/dist/upgrade-repair.js +55 -0
- package/dist/daemon/package.json +3 -0
- package/dist/index.ts +40 -37
- package/dist/index.ts.map +4 -4
- package/dist/package.json +6 -0
- package/docs/daemon-migration.md +27 -16
- package/package.json +9 -3
- package/skills/configure-pi-sync/SKILL.md +24 -46
- package/skills/configure-pi-sync/references/configuration.md +62 -22
- package/src/commands/command-execution.ts +13 -9
- package/src/commands/command-types.ts +1 -0
- package/src/daemon/pi-client.ts +4 -2
- package/src/settings/config.ts +2 -0
- package/src/sync/automatic-sync.ts +9 -2
- package/src/sync/setup-switch.ts +38 -15
- package/src/sync/startup-check.ts +7 -4
- package/src/sync/sync-operations.ts +18 -10
- package/src/ui/excluded-paths-ui.ts +6 -3
- package/src/ui/file-selection.ts +7 -2
- package/src/ui/ignore-file-guidance.ts +7 -0
- package/src/ui/ignore-file-ui.ts +97 -0
- package/src/ui/ignore-migration-prompt.ts +65 -0
- package/src/ui/manager-state.ts +40 -14
- package/src/ui/manager-ui.ts +41 -19
- package/src/ui/remote-selection-ui.ts +88 -28
- package/src/ui/settings-review.ts +26 -0
- package/src/ui/settings-ui.ts +378 -224
- package/src/ui/setup/setup-actions.ts +16 -4
- package/src/ui/setup/setup-prompts.ts +55 -9
- package/src/ui/setup/setup-switcher.ts +24 -11
- package/src/ui/storage-connections-ui.ts +50 -20
- package/src/ui/sync-setups-ui.ts +65 -30
- package/dist/chunks/chunk-277VHM2P.ts.map +0 -7
- package/dist/chunks/chunk-2ZGV5KNC.ts.map +0 -7
- package/dist/chunks/chunk-4KTJLJSJ.ts.map +0 -7
- package/dist/chunks/chunk-E3YLRLJM.ts.map +0 -7
- package/dist/chunks/chunk-ECYCNZOE.ts +0 -12
- package/dist/chunks/chunk-ECYCNZOE.ts.map +0 -7
- package/dist/chunks/chunk-JVFCVPPD.ts.map +0 -7
- package/dist/chunks/chunk-PJMM75W5.ts.map +0 -7
- package/dist/chunks/command-handler-K4XJOGYC.ts.map +0 -7
- package/dist/chunks/file-selection-MGGVOKM7.ts.map +0 -7
- package/dist/chunks/manager-result-dispatcher-4VJVDEUD.ts +0 -19
- package/dist/chunks/manager-ui-KYQ5AZJS.ts.map +0 -7
- package/dist/chunks/remote-selection-ui-W3KOZM3W.ts +0 -18
- package/dist/chunks/sync-operations-BVWYYKH3.ts.map +0 -7
- /package/dist/chunks/{chunk-ZIL6PW5L.ts.map → chunk-6YKOHBSC.ts.map} +0 -0
- /package/dist/chunks/{chunk-OA4NZLSL.ts.map → chunk-APZVJAEW.ts.map} +0 -0
- /package/dist/chunks/{chunk-4SR3ZYHA.ts.map → chunk-GELYN4UC.ts.map} +0 -0
- /package/dist/chunks/{chunk-YQSFJLJ3.ts.map → chunk-IBXEM35O.ts.map} +0 -0
- /package/dist/chunks/{chunk-2JHAN3ZO.ts.map → chunk-JREGWM2B.ts.map} +0 -0
- /package/dist/chunks/{chunk-EMRFVLZM.ts.map → chunk-NXWHPVXR.ts.map} +0 -0
- /package/dist/chunks/{chunk-SMVKMYLK.ts.map → chunk-P4GI7LTT.ts.map} +0 -0
- /package/dist/chunks/{chunk-ZVGRZD4C.ts.map → chunk-R3SAMXB6.ts.map} +0 -0
- /package/dist/chunks/{chunk-RNIEVDTR.ts.map → chunk-T3LABNHF.ts.map} +0 -0
- /package/dist/chunks/{chunk-S3R3W3KF.ts.map → chunk-UMPA5S7X.ts.map} +0 -0
- /package/dist/chunks/{chunk-S7F4FOAS.ts.map → chunk-VQ47JBLF.ts.map} +0 -0
- /package/dist/chunks/{chunk-WP2M67XZ.ts.map → chunk-WBIOSK76.ts.map} +0 -0
- /package/dist/chunks/{chunk-HEB6YT64.ts.map → chunk-ZO52JM4H.ts.map} +0 -0
- /package/dist/chunks/{conflict-review-IGKY2TOJ.ts.map → conflict-review-PCDFRIVB.ts.map} +0 -0
- /package/dist/chunks/{continuous-record-3XDXDOMV.ts.map → continuous-record-3SKJ6I3V.ts.map} +0 -0
- /package/dist/chunks/{continuous-storage-HLKCHF6Z.ts.map → continuous-storage-3EU6I7Z3.ts.map} +0 -0
- /package/dist/chunks/{manager-result-dispatcher-4VJVDEUD.ts.map → manager-result-dispatcher-GDB74IOP.ts.map} +0 -0
- /package/dist/chunks/{remote-selection-ui-W3KOZM3W.ts.map → remote-selection-ui-GPITNEBF.ts.map} +0 -0
- /package/dist/chunks/{setup-switch-D6K6PBPC.ts.map → setup-switch-L54X5U5I.ts.map} +0 -0
- /package/dist/chunks/{snapshot-5HY3M2NL.ts.map → snapshot-ZUJJZCFW.ts.map} +0 -0
- /package/dist/chunks/{snapshot-transaction-directory-OU2D7R7A.ts.map → snapshot-transaction-directory-F46JWRAU.ts.map} +0 -0
- /package/dist/chunks/{state-directory-XXHU73Z3.ts.map → state-directory-CMAIF34J.ts.map} +0 -0
- /package/dist/chunks/{sync-inspection-J3CRBORL.ts.map → sync-inspection-H2WX5SUT.ts.map} +0 -0
- /package/dist/chunks/{sync-state-2LMIXJ2C.ts.map → sync-state-QIT64TKV.ts.map} +0 -0
|
@@ -0,0 +1,324 @@
|
|
|
1
|
+
import { randomUUID } from "node:crypto";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { automaticPolicyAuthorized, ConsentPublicationError, consentFingerprint, participantBinding, readConsent, revokeChangedBindings, } from "./consent.js";
|
|
4
|
+
import { loadConfigForCheck, syncCheckConfigFingerprint } from "./engine/settings/config.js";
|
|
5
|
+
import { createEngineLoaders } from "./engine/sync/sync-loaders.js";
|
|
6
|
+
import { createServiceContext } from "./service-host.js";
|
|
7
|
+
export function createMonitor(root, host) {
|
|
8
|
+
let server;
|
|
9
|
+
let task;
|
|
10
|
+
let transition = Promise.resolve();
|
|
11
|
+
let closed = false;
|
|
12
|
+
let authorizationFailed = false;
|
|
13
|
+
let scheduled = false;
|
|
14
|
+
let settingsGeneration = 0;
|
|
15
|
+
let blockedParticipants;
|
|
16
|
+
const participatingSessions = () => JSON.stringify(server
|
|
17
|
+
?.participants()
|
|
18
|
+
.map(({ id, generation }) => `${id}:${generation}`)
|
|
19
|
+
.sort() ?? []);
|
|
20
|
+
const policyNeedsReview = () => blockedParticipants === participatingSessions();
|
|
21
|
+
let monitoredPolicy;
|
|
22
|
+
const retries = new Map();
|
|
23
|
+
const retryOwner = (peer) => JSON.stringify([peer.generation, peer.cwd, peer.sessionId, peer.sessionFile, peer.sessionDir, peer.model]);
|
|
24
|
+
let epoch;
|
|
25
|
+
const timingOwners = new WeakMap();
|
|
26
|
+
const loaders = createEngineLoaders();
|
|
27
|
+
let continuous;
|
|
28
|
+
function schedule() {
|
|
29
|
+
if (closed || scheduled || !server)
|
|
30
|
+
return;
|
|
31
|
+
scheduled = true;
|
|
32
|
+
setImmediate(() => {
|
|
33
|
+
scheduled = false;
|
|
34
|
+
transition = transition.then(start).catch(() => { });
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
function blockAutomatic() {
|
|
38
|
+
authorizationFailed = true;
|
|
39
|
+
retries.clear();
|
|
40
|
+
task?.controller.abort(new DOMException("Daemon safety barrier", "AbortError"));
|
|
41
|
+
}
|
|
42
|
+
async function stop() {
|
|
43
|
+
const current = task;
|
|
44
|
+
if (!current)
|
|
45
|
+
return;
|
|
46
|
+
task = undefined;
|
|
47
|
+
current.controller.abort(new DOMException("Daemon monitoring interrupted", "AbortError"));
|
|
48
|
+
await current.continuous.stop();
|
|
49
|
+
await current.promise;
|
|
50
|
+
}
|
|
51
|
+
function unattendedGrant(config) {
|
|
52
|
+
if (!config.continuous || !config.agentMode || !automaticPolicyAuthorized(root, config))
|
|
53
|
+
return undefined;
|
|
54
|
+
const saved = readConsent(root)?.setups[config.setupName];
|
|
55
|
+
return saved?.afterExit && saved.fingerprint === consentFingerprint(root, config) ? saved : undefined;
|
|
56
|
+
}
|
|
57
|
+
async function lifetime() {
|
|
58
|
+
if (closed || authorizationFailed)
|
|
59
|
+
return "pause";
|
|
60
|
+
try {
|
|
61
|
+
const consent = readConsent(root);
|
|
62
|
+
if (!consent || !Object.values(consent.setups).some((saved) => saved.afterExit))
|
|
63
|
+
return "exit";
|
|
64
|
+
const config = await loadConfigForCheck();
|
|
65
|
+
if (closed || authorizationFailed)
|
|
66
|
+
return "pause";
|
|
67
|
+
return !policyNeedsReview() && unattendedGrant(config) ? "keep" : "exit";
|
|
68
|
+
}
|
|
69
|
+
catch {
|
|
70
|
+
// Invalid evidence is not an implicit revocation or permission to discard a safety barrier.
|
|
71
|
+
return "pause";
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
async function start() {
|
|
75
|
+
const service = server;
|
|
76
|
+
if (!service)
|
|
77
|
+
return;
|
|
78
|
+
if (closed || authorizationFailed || policyNeedsReview() || task || !service.isIdle())
|
|
79
|
+
return;
|
|
80
|
+
const generation = settingsGeneration;
|
|
81
|
+
const participants = service.participantGeneration?.();
|
|
82
|
+
const participantsToken = participantBinding(service);
|
|
83
|
+
const consentToken = JSON.stringify(readConsent(root));
|
|
84
|
+
const sameAuthorization = () => !closed &&
|
|
85
|
+
!authorizationFailed &&
|
|
86
|
+
!policyNeedsReview() &&
|
|
87
|
+
server === service &&
|
|
88
|
+
generation === settingsGeneration &&
|
|
89
|
+
participants === service.participantGeneration?.() &&
|
|
90
|
+
participantsToken === participantBinding(service) &&
|
|
91
|
+
consentToken === JSON.stringify(readConsent(root));
|
|
92
|
+
const available = () => !task && service.isIdle() && sameAuthorization();
|
|
93
|
+
if (!consentToken || !available())
|
|
94
|
+
return;
|
|
95
|
+
const config = await loadConfigForCheck();
|
|
96
|
+
if (!available() || !automaticPolicyAuthorized(root, config))
|
|
97
|
+
return;
|
|
98
|
+
if (!config.continuous || !config.agentMode) {
|
|
99
|
+
retries.clear();
|
|
100
|
+
return;
|
|
101
|
+
}
|
|
102
|
+
const { pendingTransactionEntries } = await import("./engine/snapshot/snapshot-transaction-directory.js");
|
|
103
|
+
if ((await pendingTransactionEntries()).some((entry) => entry.isDirectory()) || !available())
|
|
104
|
+
return;
|
|
105
|
+
const peers = service.participants();
|
|
106
|
+
if (peers.some((peer) => !peer.idle || peer.ready !== true))
|
|
107
|
+
return;
|
|
108
|
+
const fingerprint = syncCheckConfigFingerprint(config);
|
|
109
|
+
if (config.reservedIgnoreNames !== undefined && monitoredPolicy !== undefined && monitoredPolicy !== fingerprint)
|
|
110
|
+
return;
|
|
111
|
+
const authorized = unattendedGrant(config);
|
|
112
|
+
const selected = peers.toSorted((a, b) => a.id.localeCompare(b.id))[0];
|
|
113
|
+
if (!selected && !authorized)
|
|
114
|
+
return;
|
|
115
|
+
if (!continuous) {
|
|
116
|
+
const { createContinuousSync } = await import("./engine/sync/continuous-sync.js");
|
|
117
|
+
if (!available())
|
|
118
|
+
return;
|
|
119
|
+
continuous = createContinuousSync(loaders, undefined, (ctx, report) => timingOwners.get(ctx)?.emit("timings", { report }));
|
|
120
|
+
// Re-enter every eligibility branch after importing the full engine graph.
|
|
121
|
+
return start();
|
|
122
|
+
}
|
|
123
|
+
const activeContinuous = continuous;
|
|
124
|
+
const binding = selected?.model ?? authorized?.model;
|
|
125
|
+
const sessionDir = selected ? selected.sessionDir : authorized?.sessionDir;
|
|
126
|
+
const cwd = selected?.cwd ?? authorized?.cwd ?? root;
|
|
127
|
+
const key = peers
|
|
128
|
+
.map((peer) => `${peer.id}:${peer.generation}`)
|
|
129
|
+
.sort()
|
|
130
|
+
.join(",") || "unattended";
|
|
131
|
+
if (epoch?.key !== key) {
|
|
132
|
+
epoch?.controller.abort(new DOMException("Participating sessions changed", "AbortError"));
|
|
133
|
+
const location = { sessionDir, cwd };
|
|
134
|
+
const manager = {
|
|
135
|
+
getSessionId: () => key,
|
|
136
|
+
getCwd: () => location.cwd,
|
|
137
|
+
getSessionDir: () => location.sessionDir ?? path.join(root, "sessions"),
|
|
138
|
+
usesDefaultSessionDir: () => location.sessionDir === undefined,
|
|
139
|
+
};
|
|
140
|
+
epoch = { key, controller: new AbortController(), manager, location, debug: selected?.startupDebug ?? false };
|
|
141
|
+
}
|
|
142
|
+
epoch.location.sessionDir = sessionDir;
|
|
143
|
+
epoch.location.cwd = cwd;
|
|
144
|
+
const controller = new AbortController();
|
|
145
|
+
const original = service.backgroundExecution(controller.signal);
|
|
146
|
+
const stableFacts = {
|
|
147
|
+
id: randomUUID(),
|
|
148
|
+
generation: randomUUID(),
|
|
149
|
+
pid: process.pid,
|
|
150
|
+
cwd,
|
|
151
|
+
mode: "rpc",
|
|
152
|
+
hasUI: false,
|
|
153
|
+
idle: true,
|
|
154
|
+
sessionDir,
|
|
155
|
+
model: binding,
|
|
156
|
+
};
|
|
157
|
+
const execution = { ...original, facts: () => stableFacts };
|
|
158
|
+
const allowed = (currentConfig = config) => {
|
|
159
|
+
if (controller.signal.aborted ||
|
|
160
|
+
!sameAuthorization() ||
|
|
161
|
+
syncCheckConfigFingerprint(currentConfig) !== fingerprint)
|
|
162
|
+
return false;
|
|
163
|
+
const current = service.participants();
|
|
164
|
+
if (current.length)
|
|
165
|
+
return current.every((peer) => peer.idle &&
|
|
166
|
+
peer.ready === true &&
|
|
167
|
+
JSON.stringify(peer.model) === JSON.stringify(binding) &&
|
|
168
|
+
peer.sessionDir === sessionDir &&
|
|
169
|
+
peer.cwd === cwd);
|
|
170
|
+
const saved = unattendedGrant(currentConfig);
|
|
171
|
+
return Boolean(saved &&
|
|
172
|
+
JSON.stringify(saved.model) === JSON.stringify(binding) &&
|
|
173
|
+
saved.sessionDir === sessionDir &&
|
|
174
|
+
(saved.cwd ?? root) === cwd);
|
|
175
|
+
};
|
|
176
|
+
const ctx = createServiceContext(execution);
|
|
177
|
+
ctx.reload = undefined;
|
|
178
|
+
ctx.sessionManager = epoch.manager;
|
|
179
|
+
Object.defineProperty(ctx, "hasUI", {
|
|
180
|
+
get: () => service.participants().some((peer) => peer.hasUI && peer.ready === true),
|
|
181
|
+
});
|
|
182
|
+
timingOwners.set(ctx, execution);
|
|
183
|
+
ctx.backgroundAuthorized = allowed;
|
|
184
|
+
ctx.isIdle = () => Boolean(service.isIdle() && allowed());
|
|
185
|
+
const refresh = async () => {
|
|
186
|
+
execution.validate();
|
|
187
|
+
if (!allowed(await loadConfigForCheck(undefined, controller.signal)))
|
|
188
|
+
throw new Error("Daemon automatic authorization changed.");
|
|
189
|
+
execution.validate();
|
|
190
|
+
};
|
|
191
|
+
const sessionSignal = epoch.controller.signal;
|
|
192
|
+
const debug = epoch.debug;
|
|
193
|
+
let finish;
|
|
194
|
+
const promise = new Promise((resolve) => {
|
|
195
|
+
finish = resolve;
|
|
196
|
+
});
|
|
197
|
+
const current = { controller, continuous: activeContinuous, promise };
|
|
198
|
+
task = current;
|
|
199
|
+
void host
|
|
200
|
+
.run(execution, async () => {
|
|
201
|
+
const { prepareResolverHost } = await import("./resolver-host.js");
|
|
202
|
+
await refresh();
|
|
203
|
+
const resolver = await prepareResolverHost(root, binding, controller.signal);
|
|
204
|
+
try {
|
|
205
|
+
await refresh();
|
|
206
|
+
if (config.reservedIgnoreNames !== undefined)
|
|
207
|
+
monitoredPolicy = fingerprint;
|
|
208
|
+
ctx.model = resolver.model;
|
|
209
|
+
ctx.modelUnavailableReason = resolver.unavailable;
|
|
210
|
+
ctx.modelRegistry = {
|
|
211
|
+
streamSimple: (model, context, options) => {
|
|
212
|
+
execution.validate();
|
|
213
|
+
if (!allowed())
|
|
214
|
+
throw new Error("Daemon authorization changed before model dispatch.");
|
|
215
|
+
return resolver.registry.streamSimple(model, context, options);
|
|
216
|
+
},
|
|
217
|
+
};
|
|
218
|
+
ctx.validateModel = async () => {
|
|
219
|
+
await refresh();
|
|
220
|
+
for (const peer of peers) {
|
|
221
|
+
const fresh = await execution.request("model-binding", { background: true }, peer.id);
|
|
222
|
+
if (JSON.stringify(fresh ?? undefined) !== JSON.stringify(binding))
|
|
223
|
+
throw new Error("Participating model changed before dispatch.");
|
|
224
|
+
await refresh();
|
|
225
|
+
}
|
|
226
|
+
};
|
|
227
|
+
const explicitRetry = service.participants().some((peer) => {
|
|
228
|
+
const retry = retries.get(peer.id);
|
|
229
|
+
return retry?.fingerprint === fingerprint && retry.owner === retryOwner(peer) && !retry.signal.aborted;
|
|
230
|
+
});
|
|
231
|
+
retries.clear();
|
|
232
|
+
activeContinuous.start(ctx, sessionSignal, explicitRetry ? config.setupName : false, debug, async (run) => {
|
|
233
|
+
await refresh();
|
|
234
|
+
const attempt = new AbortController();
|
|
235
|
+
const owner = service.backgroundExecution(AbortSignal.any([attempt.signal, controller.signal]));
|
|
236
|
+
try {
|
|
237
|
+
return await host.run({ ...owner, facts: execution.facts }, () => run(owner.committed));
|
|
238
|
+
}
|
|
239
|
+
finally {
|
|
240
|
+
attempt.abort();
|
|
241
|
+
}
|
|
242
|
+
});
|
|
243
|
+
await new Promise((resolve) => {
|
|
244
|
+
if (controller.signal.aborted)
|
|
245
|
+
resolve();
|
|
246
|
+
else
|
|
247
|
+
controller.signal.addEventListener("abort", () => resolve(), { once: true });
|
|
248
|
+
});
|
|
249
|
+
await activeContinuous.stop();
|
|
250
|
+
}
|
|
251
|
+
finally {
|
|
252
|
+
await resolver.settle();
|
|
253
|
+
}
|
|
254
|
+
})
|
|
255
|
+
.catch(() => { })
|
|
256
|
+
.finally(() => {
|
|
257
|
+
controller.abort();
|
|
258
|
+
if (task === current)
|
|
259
|
+
task = undefined;
|
|
260
|
+
finish();
|
|
261
|
+
});
|
|
262
|
+
}
|
|
263
|
+
return {
|
|
264
|
+
bind(value) {
|
|
265
|
+
server = value;
|
|
266
|
+
schedule();
|
|
267
|
+
},
|
|
268
|
+
schedule,
|
|
269
|
+
lifetime,
|
|
270
|
+
blockAutomatic,
|
|
271
|
+
explicitSync(config, execution) {
|
|
272
|
+
const peer = execution.facts();
|
|
273
|
+
if (closed || !peer)
|
|
274
|
+
return;
|
|
275
|
+
const fingerprint = syncCheckConfigFingerprint(config);
|
|
276
|
+
retries.set(peer.id, { fingerprint, owner: retryOwner(peer), signal: execution.signal });
|
|
277
|
+
},
|
|
278
|
+
reviewedPolicy(config) {
|
|
279
|
+
blockedParticipants = undefined;
|
|
280
|
+
monitoredPolicy = config.reservedIgnoreNames === undefined ? undefined : syncCheckConfigFingerprint(config);
|
|
281
|
+
},
|
|
282
|
+
settingsCommitted(blockNewPolicy) {
|
|
283
|
+
settingsGeneration++;
|
|
284
|
+
retries.clear();
|
|
285
|
+
if (blockNewPolicy)
|
|
286
|
+
blockedParticipants = participatingSessions();
|
|
287
|
+
task?.controller.abort(new DOMException("Sync settings committed", "AbortError"));
|
|
288
|
+
},
|
|
289
|
+
async pause() {
|
|
290
|
+
transition = transition.then(stop);
|
|
291
|
+
await transition;
|
|
292
|
+
},
|
|
293
|
+
async factsChanged() {
|
|
294
|
+
let failure;
|
|
295
|
+
try {
|
|
296
|
+
if (server) {
|
|
297
|
+
const peers = server.participants();
|
|
298
|
+
for (const [id, retry] of retries)
|
|
299
|
+
if (retry.signal.aborted || !peers.some((peer) => peer.id === id && retryOwner(peer) === retry.owner))
|
|
300
|
+
retries.delete(id);
|
|
301
|
+
revokeChangedBindings(root, peers);
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
catch (error) {
|
|
305
|
+
blockAutomatic();
|
|
306
|
+
// Recovery barriers must not prevent passive clients from registering protection.
|
|
307
|
+
if (error instanceof ConsentPublicationError)
|
|
308
|
+
failure = error;
|
|
309
|
+
}
|
|
310
|
+
transition = transition.then(stop);
|
|
311
|
+
await transition;
|
|
312
|
+
if (failure)
|
|
313
|
+
throw new Error("Daemon authorization could not be saved. Automatic work is blocked for this instance; repair and revoke permission before an explicit restart.", { cause: failure });
|
|
314
|
+
schedule();
|
|
315
|
+
},
|
|
316
|
+
async close() {
|
|
317
|
+
closed = true;
|
|
318
|
+
retries.clear();
|
|
319
|
+
transition = transition.then(stop);
|
|
320
|
+
await transition;
|
|
321
|
+
epoch?.controller.abort();
|
|
322
|
+
},
|
|
323
|
+
};
|
|
324
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/** Keep client publication outside complete mutation-queue reservation lifetimes. */
|
|
2
|
+
export class ParticipantAdmission {
|
|
3
|
+
tail = Promise.resolve();
|
|
4
|
+
async run(action, signal) {
|
|
5
|
+
signal?.throwIfAborted();
|
|
6
|
+
const previous = this.tail;
|
|
7
|
+
let release;
|
|
8
|
+
const held = new Promise((resolve) => {
|
|
9
|
+
release = resolve;
|
|
10
|
+
});
|
|
11
|
+
this.tail = previous.then(() => held);
|
|
12
|
+
let abort;
|
|
13
|
+
try {
|
|
14
|
+
await new Promise((resolve, reject) => {
|
|
15
|
+
void previous.then(resolve);
|
|
16
|
+
abort = () => reject(signal?.reason);
|
|
17
|
+
signal?.addEventListener("abort", abort, { once: true });
|
|
18
|
+
if (signal?.aborted)
|
|
19
|
+
abort();
|
|
20
|
+
});
|
|
21
|
+
signal?.throwIfAborted();
|
|
22
|
+
return await action();
|
|
23
|
+
}
|
|
24
|
+
finally {
|
|
25
|
+
if (abort)
|
|
26
|
+
signal?.removeEventListener("abort", abort);
|
|
27
|
+
release();
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
}
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
export const PROTOCOL_VERSION = 1;
|
|
4
|
+
/** Authenticated idle shutdown after work is drained; callers may wait for this owner to exit. */
|
|
5
|
+
export class DaemonStoppingError extends Error {
|
|
6
|
+
name = "DaemonStoppingError";
|
|
7
|
+
constructor() {
|
|
8
|
+
super("Daemon is closing after its idle grace period; wait for owner exit before reconnecting.");
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
/** This owner expired this client's heartbeat; a normal facts update cannot restore participation. */
|
|
12
|
+
export class ClientContactExpiredError extends Error {
|
|
13
|
+
name = "ClientContactExpiredError";
|
|
14
|
+
constructor() {
|
|
15
|
+
super("Client contact expired; resume the same session before publishing facts.");
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
export const MAX_MESSAGE_BYTES = 4 * 1024 * 1024;
|
|
19
|
+
export const REVIEW_PAGE_CHARS = 64 * 1024;
|
|
20
|
+
export const MAX_REVIEW_BYTES = 16 * 1024 * 1024;
|
|
21
|
+
export const MAX_SETTINGS_BYTES = 512 * 1024;
|
|
22
|
+
export function validateHandoff(value) {
|
|
23
|
+
if (!record(value) ||
|
|
24
|
+
!["replace", "exit"].includes(String(value.kind)) ||
|
|
25
|
+
["file", "sessionId", "cwd"].some((key) => value[key] !== undefined &&
|
|
26
|
+
(typeof value[key] !== "string" || value[key].length > 32768 || value[key].includes("\0"))) ||
|
|
27
|
+
["file", "cwd"].some((key) => typeof value[key] === "string" && !path.isAbsolute(value[key])))
|
|
28
|
+
throw new Error("Invalid client handoff.");
|
|
29
|
+
}
|
|
30
|
+
export function record(value) {
|
|
31
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
32
|
+
}
|
|
33
|
+
export function operationPrefix(identity) {
|
|
34
|
+
return `${createHash("sha256").update(identity.instance).digest("hex").slice(0, 16)}-`;
|
|
35
|
+
}
|
|
36
|
+
export function validId(value) {
|
|
37
|
+
return typeof value === "string" && /^[a-f0-9-]{16,64}$/u.test(value);
|
|
38
|
+
}
|
|
39
|
+
export function validateFacts(value) {
|
|
40
|
+
if (!record(value) ||
|
|
41
|
+
Object.keys(value).some((key) => ![
|
|
42
|
+
"id",
|
|
43
|
+
"pid",
|
|
44
|
+
"generation",
|
|
45
|
+
"cwd",
|
|
46
|
+
"mode",
|
|
47
|
+
"hasUI",
|
|
48
|
+
"idle",
|
|
49
|
+
"ready",
|
|
50
|
+
"startupDebug",
|
|
51
|
+
"sessionFile",
|
|
52
|
+
"sessionId",
|
|
53
|
+
"sessionDir",
|
|
54
|
+
"model",
|
|
55
|
+
].includes(key)) ||
|
|
56
|
+
!validId(value.id) ||
|
|
57
|
+
!validId(value.generation) ||
|
|
58
|
+
!Number.isSafeInteger(value.pid) ||
|
|
59
|
+
Number(value.pid) < 1 ||
|
|
60
|
+
Number(value.pid) > 2_147_483_647 ||
|
|
61
|
+
typeof value.cwd !== "string" ||
|
|
62
|
+
value.cwd.length > 32768 ||
|
|
63
|
+
value.cwd.includes("\0") ||
|
|
64
|
+
!path.isAbsolute(value.cwd) ||
|
|
65
|
+
!["tui", "rpc", "json", "print"].includes(String(value.mode)) ||
|
|
66
|
+
typeof value.hasUI !== "boolean" ||
|
|
67
|
+
typeof value.idle !== "boolean" ||
|
|
68
|
+
(value.ready !== undefined && typeof value.ready !== "boolean") ||
|
|
69
|
+
(value.startupDebug !== undefined && typeof value.startupDebug !== "boolean") ||
|
|
70
|
+
["sessionFile", "sessionId", "sessionDir"].some((key) => value[key] !== undefined &&
|
|
71
|
+
(typeof value[key] !== "string" || value[key].length > 32768 || value[key].includes("\0"))) ||
|
|
72
|
+
["sessionFile", "sessionDir"].some((key) => typeof value[key] === "string" && !path.isAbsolute(value[key])) ||
|
|
73
|
+
(value.model !== undefined &&
|
|
74
|
+
(!record(value.model) ||
|
|
75
|
+
Object.keys(value.model).some((key) => !["provider", "id", "signature", "authSource"].includes(key)) ||
|
|
76
|
+
typeof value.model.provider !== "string" ||
|
|
77
|
+
!value.model.provider.length ||
|
|
78
|
+
value.model.provider.length > 256 ||
|
|
79
|
+
typeof value.model.id !== "string" ||
|
|
80
|
+
!value.model.id.length ||
|
|
81
|
+
value.model.id.length > 4096 ||
|
|
82
|
+
typeof value.model.signature !== "string" ||
|
|
83
|
+
!/^[a-f0-9]{64}$/u.test(value.model.signature) ||
|
|
84
|
+
!["stored", "environment", "models_json_key", "models_json_command"].includes(String(value.model.authSource))))) {
|
|
85
|
+
throw new Error("Invalid client facts.");
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
export function remoteError(error) {
|
|
89
|
+
if (!(error instanceof Error))
|
|
90
|
+
return { name: "Error", message: "Synchronization operation failed." };
|
|
91
|
+
const properties = Object.fromEntries(Object.entries(error).filter(([key]) => ["code", "decision", "status", "phase", "snapshotId", "backup", "publication", "snapshotRef"].includes(key)));
|
|
92
|
+
const result = { name: error.name, message: error.message.slice(0, 4096), properties };
|
|
93
|
+
try {
|
|
94
|
+
if (Buffer.byteLength(JSON.stringify(result)) <= MAX_MESSAGE_BYTES / 4)
|
|
95
|
+
return result;
|
|
96
|
+
}
|
|
97
|
+
catch {
|
|
98
|
+
/* Opaque/unserializable details must not escape. */
|
|
99
|
+
}
|
|
100
|
+
return {
|
|
101
|
+
name: "Error",
|
|
102
|
+
message: "Operation details exceed the local API limit; inspect retained recovery evidence. No truncated review was approved.",
|
|
103
|
+
};
|
|
104
|
+
}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import path from "node:path";
|
|
2
|
+
import { modelSignature } from "./model-signature.js";
|
|
3
|
+
const DIFFERENT = "The selected model is unavailable: its definition or auth source differs in the standalone daemon. No fallback model was selected. Align the host and daemon Pi model catalogs, or select a model with the same definition and auth source on both.";
|
|
4
|
+
/** No resource discovery or extension loading; credential work starts only for a resolver request. */
|
|
5
|
+
export async function prepareResolverHost(root, binding, signal) {
|
|
6
|
+
const unavailable = (reason) => ({
|
|
7
|
+
unavailable: reason,
|
|
8
|
+
registry: {
|
|
9
|
+
streamSimple: () => {
|
|
10
|
+
throw new Error(reason);
|
|
11
|
+
},
|
|
12
|
+
},
|
|
13
|
+
settle: async () => { },
|
|
14
|
+
});
|
|
15
|
+
signal?.throwIfAborted();
|
|
16
|
+
if (!binding)
|
|
17
|
+
return unavailable("No independently available daemon resolver model is authorized; parent-only runtime providers are unavailable.");
|
|
18
|
+
const [{ ModelRuntime, ModelRegistry }, { InMemoryCredentialStore }] = await Promise.all([
|
|
19
|
+
import("@earendil-works/pi-coding-agent"),
|
|
20
|
+
import("@earendil-works/pi-ai"),
|
|
21
|
+
]);
|
|
22
|
+
signal?.throwIfAborted();
|
|
23
|
+
// Read public static definitions without touching auth.json, refreshing OAuth,
|
|
24
|
+
// evaluating credential commands, or discovering catalogs over the network.
|
|
25
|
+
const metadata = await ModelRuntime.create({
|
|
26
|
+
credentials: new InMemoryCredentialStore(),
|
|
27
|
+
modelsPath: path.join(root, "models.json"),
|
|
28
|
+
refreshOnCreate: false,
|
|
29
|
+
});
|
|
30
|
+
signal?.throwIfAborted();
|
|
31
|
+
const model = metadata.getModel(binding.provider, binding.id);
|
|
32
|
+
if (metadata.getError() || !model || modelSignature(model) !== binding.signature)
|
|
33
|
+
return unavailable(DIFFERENT);
|
|
34
|
+
let runtime;
|
|
35
|
+
let loading;
|
|
36
|
+
const load = (requestSignal) => (loading ??= (async () => {
|
|
37
|
+
requestSignal?.throwIfAborted();
|
|
38
|
+
runtime = await ModelRuntime.create({
|
|
39
|
+
authPath: path.join(root, "auth.json"),
|
|
40
|
+
modelsPath: path.join(root, "models.json"),
|
|
41
|
+
refreshOnCreate: false,
|
|
42
|
+
});
|
|
43
|
+
requestSignal?.throwIfAborted();
|
|
44
|
+
if (runtime.getError() || modelSignature(runtime.getModel(binding.provider, binding.id)) !== binding.signature)
|
|
45
|
+
throw new Error(DIFFERENT);
|
|
46
|
+
await runtime.refresh({ allowNetwork: false, providers: [binding.provider], signal: requestSignal });
|
|
47
|
+
requestSignal?.throwIfAborted();
|
|
48
|
+
const actual = runtime.getModel(binding.provider, binding.id);
|
|
49
|
+
if (runtime.getError() ||
|
|
50
|
+
!actual ||
|
|
51
|
+
modelSignature(actual) !== binding.signature ||
|
|
52
|
+
runtime.getProviderAuthStatus(binding.provider).source !== binding.authSource)
|
|
53
|
+
throw new Error(DIFFERENT);
|
|
54
|
+
return { registry: new ModelRegistry(runtime), model: actual };
|
|
55
|
+
})());
|
|
56
|
+
return {
|
|
57
|
+
model,
|
|
58
|
+
registry: {
|
|
59
|
+
streamSimple(_model, context, options) {
|
|
60
|
+
let upstream;
|
|
61
|
+
const get = () => (upstream ??= load(options?.signal ?? signal).then((host) => host.registry.streamSimple(host.model, context, options)));
|
|
62
|
+
return {
|
|
63
|
+
async *[Symbol.asyncIterator]() {
|
|
64
|
+
for await (const event of await get())
|
|
65
|
+
yield event;
|
|
66
|
+
},
|
|
67
|
+
async result() {
|
|
68
|
+
return (await get()).result();
|
|
69
|
+
},
|
|
70
|
+
};
|
|
71
|
+
},
|
|
72
|
+
},
|
|
73
|
+
async settle() {
|
|
74
|
+
await loading?.catch(() => { });
|
|
75
|
+
await runtime?.settleCredentialWrites();
|
|
76
|
+
},
|
|
77
|
+
};
|
|
78
|
+
}
|