@narumitw/pi-sync-daemon 0.0.0-stage → 0.3.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 +22 -0
- package/LICENSE +21 -0
- package/README.md +91 -2
- package/dist/cli.d.ts +2 -0
- package/dist/cli.js +127 -0
- package/dist/client.d.ts +57 -0
- package/dist/client.js +534 -0
- package/dist/consent.d.ts +24 -0
- package/dist/consent.js +205 -0
- package/dist/document.d.ts +10 -0
- package/dist/document.js +6 -0
- package/dist/endpoint.d.ts +8 -0
- package/dist/endpoint.js +91 -0
- package/dist/engine/backends/backend-factory.d.ts +6 -0
- package/dist/engine/backends/backend-factory.js +16 -0
- package/dist/engine/backends/backend-identity.d.ts +3 -0
- package/dist/engine/backends/backend-identity.js +38 -0
- package/dist/engine/backends/backend-types.d.ts +70 -0
- package/dist/engine/backends/backend-types.js +1 -0
- package/dist/engine/backends/git/git-backend.d.ts +55 -0
- package/dist/engine/backends/git/git-backend.js +700 -0
- package/dist/engine/backends/git/git-config.d.ts +5 -0
- package/dist/engine/backends/git/git-config.js +118 -0
- package/dist/engine/backends/git/git-runner.d.ts +25 -0
- package/dist/engine/backends/git/git-runner.js +232 -0
- package/dist/engine/backends/git/git-storage.d.ts +39 -0
- package/dist/engine/backends/git/git-storage.js +226 -0
- package/dist/engine/backends/s3/s3-backend.d.ts +32 -0
- package/dist/engine/backends/s3/s3-backend.js +436 -0
- package/dist/engine/backends/s3/s3-client.d.ts +36 -0
- package/dist/engine/backends/s3/s3-client.js +352 -0
- package/dist/engine/backends/s3/s3-conditions.d.ts +4 -0
- package/dist/engine/backends/s3/s3-conditions.js +66 -0
- package/dist/engine/backends/snapshot-pointer.d.ts +6 -0
- package/dist/engine/backends/snapshot-pointer.js +22 -0
- package/dist/engine/backends/sync-backend.d.ts +71 -0
- package/dist/engine/backends/sync-backend.js +23 -0
- package/dist/engine/backends/webdav/webdav-backend.d.ts +34 -0
- package/dist/engine/backends/webdav/webdav-backend.js +534 -0
- package/dist/engine/backends/webdav/webdav-client.d.ts +36 -0
- package/dist/engine/backends/webdav/webdav-client.js +326 -0
- package/dist/engine/backends/webdav/webdav-config.d.ts +5 -0
- package/dist/engine/backends/webdav/webdav-config.js +73 -0
- package/dist/engine/commands/command-types.d.ts +31 -0
- package/dist/engine/commands/command-types.js +1 -0
- package/dist/engine/host.d.ts +70 -0
- package/dist/engine/host.js +16 -0
- package/dist/engine/paths.d.ts +9 -0
- package/dist/engine/paths.js +71 -0
- package/dist/engine/settings/config-errors.d.ts +1 -0
- package/dist/engine/settings/config-errors.js +4 -0
- package/dist/engine/settings/config-file.d.ts +33 -0
- package/dist/engine/settings/config-file.js +630 -0
- package/dist/engine/settings/config.d.ts +24 -0
- package/dist/engine/settings/config.js +200 -0
- package/dist/engine/settings/settings-management.d.ts +24 -0
- package/dist/engine/settings/settings-management.js +191 -0
- package/dist/engine/settings/settings-store.d.ts +8 -0
- package/dist/engine/settings/settings-store.js +41 -0
- package/dist/engine/settings/settings-types.d.ts +134 -0
- package/dist/engine/settings/settings-types.js +1 -0
- package/dist/engine/settings/settings-validation.d.ts +15 -0
- package/dist/engine/settings/settings-validation.js +356 -0
- package/dist/engine/snapshot/session-paths.d.ts +12 -0
- package/dist/engine/snapshot/session-paths.js +91 -0
- package/dist/engine/snapshot/snapshot-apply.d.ts +22 -0
- package/dist/engine/snapshot/snapshot-apply.js +262 -0
- package/dist/engine/snapshot/snapshot-case-replacement.d.ts +15 -0
- package/dist/engine/snapshot/snapshot-case-replacement.js +82 -0
- package/dist/engine/snapshot/snapshot-codec.d.ts +6 -0
- package/dist/engine/snapshot/snapshot-codec.js +53 -0
- package/dist/engine/snapshot/snapshot-exclude-safety.d.ts +7 -0
- package/dist/engine/snapshot/snapshot-exclude-safety.js +47 -0
- package/dist/engine/snapshot/snapshot-mutation-queues.d.ts +9 -0
- package/dist/engine/snapshot/snapshot-mutation-queues.js +133 -0
- package/dist/engine/snapshot/snapshot-paths.d.ts +1 -0
- package/dist/engine/snapshot/snapshot-paths.js +11 -0
- package/dist/engine/snapshot/snapshot-root-transition.d.ts +25 -0
- package/dist/engine/snapshot/snapshot-root-transition.js +122 -0
- package/dist/engine/snapshot/snapshot-transaction-directory.d.ts +2 -0
- package/dist/engine/snapshot/snapshot-transaction-directory.js +16 -0
- package/dist/engine/snapshot/snapshot-transaction-plan.d.ts +12 -0
- package/dist/engine/snapshot/snapshot-transaction-plan.js +68 -0
- package/dist/engine/snapshot/snapshot-transaction.d.ts +15 -0
- package/dist/engine/snapshot/snapshot-transaction.js +787 -0
- package/dist/engine/snapshot/snapshot-types.d.ts +48 -0
- package/dist/engine/snapshot/snapshot-types.js +1 -0
- package/dist/engine/snapshot/snapshot.d.ts +30 -0
- package/dist/engine/snapshot/snapshot.js +364 -0
- package/dist/engine/state/json-file.d.ts +6 -0
- package/dist/engine/state/json-file.js +49 -0
- package/dist/engine/state/lock-policy.d.ts +2 -0
- package/dist/engine/state/lock-policy.js +2 -0
- package/dist/engine/state/lock.d.ts +20 -0
- package/dist/engine/state/lock.js +259 -0
- package/dist/engine/state/lockfile-fs.d.ts +13 -0
- package/dist/engine/state/lockfile-fs.js +13 -0
- package/dist/engine/state/merge-baseline-store.d.ts +11 -0
- package/dist/engine/state/merge-baseline-store.js +187 -0
- package/dist/engine/state/mutation-directory-sync.d.ts +5 -0
- package/dist/engine/state/mutation-directory-sync.js +34 -0
- package/dist/engine/state/operation-availability.d.ts +49 -0
- package/dist/engine/state/operation-availability.js +40 -0
- package/dist/engine/state/state-directory.d.ts +14 -0
- package/dist/engine/state/state-directory.js +225 -0
- package/dist/engine/state/state-types.d.ts +27 -0
- package/dist/engine/state/state-types.js +1 -0
- package/dist/engine/state/sync-state-store.d.ts +11 -0
- package/dist/engine/state/sync-state-store.js +54 -0
- package/dist/engine/sync/agent-candidate.d.ts +44 -0
- package/dist/engine/sync/agent-candidate.js +207 -0
- package/dist/engine/sync/agent-plan.d.ts +8 -0
- package/dist/engine/sync/agent-plan.js +84 -0
- package/dist/engine/sync/agent-policy.d.ts +9 -0
- package/dist/engine/sync/agent-policy.js +10 -0
- package/dist/engine/sync/agent-refusal-retry.d.ts +4 -0
- package/dist/engine/sync/agent-refusal-retry.js +39 -0
- package/dist/engine/sync/agent-request.d.ts +14 -0
- package/dist/engine/sync/agent-request.js +13 -0
- package/dist/engine/sync/agent-runner.d.ts +6 -0
- package/dist/engine/sync/agent-runner.js +183 -0
- package/dist/engine/sync/agent-store.d.ts +7 -0
- package/dist/engine/sync/agent-store.js +177 -0
- package/dist/engine/sync/agent-sync.d.ts +4 -0
- package/dist/engine/sync/agent-sync.js +132 -0
- package/dist/engine/sync/apply-review.d.ts +8 -0
- package/dist/engine/sync/apply-review.js +51 -0
- package/dist/engine/sync/conflict-artifact-ownership.d.ts +4 -0
- package/dist/engine/sync/conflict-artifact-ownership.js +53 -0
- package/dist/engine/sync/conflict-artifacts.d.ts +32 -0
- package/dist/engine/sync/conflict-artifacts.js +158 -0
- package/dist/engine/sync/conflict-groups.d.ts +9 -0
- package/dist/engine/sync/conflict-groups.js +76 -0
- package/dist/engine/sync/conflict-preview.d.ts +4 -0
- package/dist/engine/sync/conflict-preview.js +53 -0
- package/dist/engine/sync/conflict-resolution.d.ts +13 -0
- package/dist/engine/sync/conflict-resolution.js +53 -0
- package/dist/engine/sync/conflict-retention.d.ts +5 -0
- package/dist/engine/sync/conflict-retention.js +61 -0
- package/dist/engine/sync/conflict-review.d.ts +4 -0
- package/dist/engine/sync/conflict-review.js +73 -0
- package/dist/engine/sync/content-conflicts.d.ts +10 -0
- package/dist/engine/sync/content-conflicts.js +70 -0
- package/dist/engine/sync/continuous-errors.d.ts +14 -0
- package/dist/engine/sync/continuous-errors.js +66 -0
- package/dist/engine/sync/continuous-lease.d.ts +9 -0
- package/dist/engine/sync/continuous-lease.js +206 -0
- package/dist/engine/sync/continuous-record.d.ts +31 -0
- package/dist/engine/sync/continuous-record.js +199 -0
- package/dist/engine/sync/continuous-storage.d.ts +14 -0
- package/dist/engine/sync/continuous-storage.js +59 -0
- package/dist/engine/sync/continuous-sync.d.ts +15 -0
- package/dist/engine/sync/continuous-sync.js +496 -0
- package/dist/engine/sync/continuous-watch.d.ts +3 -0
- package/dist/engine/sync/continuous-watch.js +65 -0
- package/dist/engine/sync/exclude-policy.d.ts +5 -0
- package/dist/engine/sync/exclude-policy.js +79 -0
- package/dist/engine/sync/field-migration.d.ts +5 -0
- package/dist/engine/sync/field-migration.js +20 -0
- package/dist/engine/sync/file-merge-planner.d.ts +36 -0
- package/dist/engine/sync/file-merge-planner.js +123 -0
- package/dist/engine/sync/initialization.d.ts +12 -0
- package/dist/engine/sync/initialization.js +75 -0
- package/dist/engine/sync/json-document.d.ts +29 -0
- package/dist/engine/sync/json-document.js +172 -0
- package/dist/engine/sync/local-field-policy.d.ts +2 -0
- package/dist/engine/sync/local-field-policy.js +26 -0
- package/dist/engine/sync/local-fields.d.ts +9 -0
- package/dist/engine/sync/local-fields.js +116 -0
- package/dist/engine/sync/merge-apply.d.ts +5 -0
- package/dist/engine/sync/merge-apply.js +386 -0
- package/dist/engine/sync/merge-journal.d.ts +31 -0
- package/dist/engine/sync/merge-journal.js +179 -0
- package/dist/engine/sync/merge-transfer-error.d.ts +3 -0
- package/dist/engine/sync/merge-transfer-error.js +64 -0
- package/dist/engine/sync/merged-sync.d.ts +6 -0
- package/dist/engine/sync/merged-sync.js +618 -0
- package/dist/engine/sync/mutation-owner.d.ts +2 -0
- package/dist/engine/sync/mutation-owner.js +18 -0
- package/dist/engine/sync/package-merge.d.ts +3 -0
- package/dist/engine/sync/package-merge.js +82 -0
- package/dist/engine/sync/partial-progress.d.ts +15 -0
- package/dist/engine/sync/partial-progress.js +39 -0
- package/dist/engine/sync/remote-snapshot.d.ts +28 -0
- package/dist/engine/sync/remote-snapshot.js +62 -0
- package/dist/engine/sync/resolver-stream.d.ts +4 -0
- package/dist/engine/sync/resolver-stream.js +34 -0
- package/dist/engine/sync/session-merge.d.ts +3 -0
- package/dist/engine/sync/session-merge.js +178 -0
- package/dist/engine/sync/session-push.d.ts +5 -0
- package/dist/engine/sync/session-push.js +32 -0
- package/dist/engine/sync/settings-conflicts.d.ts +8 -0
- package/dist/engine/sync/settings-conflicts.js +56 -0
- package/dist/engine/sync/settings-merge.d.ts +13 -0
- package/dist/engine/sync/settings-merge.js +93 -0
- package/dist/engine/sync/signals.d.ts +2 -0
- package/dist/engine/sync/signals.js +8 -0
- package/dist/engine/sync/startup-timings.d.ts +11 -0
- package/dist/engine/sync/startup-timings.js +44 -0
- package/dist/engine/sync/sync-decision.d.ts +35 -0
- package/dist/engine/sync/sync-decision.js +66 -0
- package/dist/engine/sync/sync-error-guidance.d.ts +2 -0
- package/dist/engine/sync/sync-error-guidance.js +55 -0
- package/dist/engine/sync/sync-errors.d.ts +9 -0
- package/dist/engine/sync/sync-errors.js +18 -0
- package/dist/engine/sync/sync-inspection.d.ts +26 -0
- package/dist/engine/sync/sync-inspection.js +40 -0
- package/dist/engine/sync/sync-loaders.d.ts +7 -0
- package/dist/engine/sync/sync-loaders.js +8 -0
- package/dist/engine/sync/sync-local.d.ts +5 -0
- package/dist/engine/sync/sync-local.js +40 -0
- package/dist/engine/sync/sync-mutations.d.ts +30 -0
- package/dist/engine/sync/sync-mutations.js +589 -0
- package/dist/engine/sync/sync-operations.d.ts +2 -0
- package/dist/engine/sync/sync-operations.js +3 -0
- package/dist/engine/sync/sync-policy.d.ts +85 -0
- package/dist/engine/sync/sync-policy.js +356 -0
- package/dist/engine/sync/sync-queries.d.ts +7 -0
- package/dist/engine/sync/sync-queries.js +195 -0
- package/dist/engine/sync/sync-state.d.ts +31 -0
- package/dist/engine/sync/sync-state.js +92 -0
- package/dist/engine/sync/text-merge.d.ts +19 -0
- package/dist/engine/sync/text-merge.js +124 -0
- package/dist/engine/types.d.ts +5 -0
- package/dist/engine/types.js +2 -0
- package/dist/engine/ui/merge-review.d.ts +2 -0
- package/dist/engine/ui/merge-review.js +7 -0
- package/dist/engine/ui/merged-sync-summary.d.ts +25 -0
- package/dist/engine/ui/merged-sync-summary.js +86 -0
- package/dist/engine/ui/sync-format.d.ts +13 -0
- package/dist/engine/ui/sync-format.js +150 -0
- package/dist/engine/ui/sync-status.d.ts +9 -0
- package/dist/engine/ui/sync-status.js +30 -0
- package/dist/engine/ui/terminal-text.d.ts +3 -0
- package/dist/engine/ui/terminal-text.js +9 -0
- package/dist/engine/ui/transfer-changes.d.ts +12 -0
- package/dist/engine/ui/transfer-changes.js +24 -0
- package/dist/engine/ui/transfer-review.d.ts +3 -0
- package/dist/engine/ui/transfer-review.js +19 -0
- package/dist/engine-service.d.ts +14 -0
- package/dist/engine-service.js +322 -0
- package/dist/identity.d.ts +20 -0
- package/dist/identity.js +133 -0
- package/dist/idle-exit.d.ts +17 -0
- package/dist/idle-exit.js +77 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.js +4 -0
- package/dist/lifecycle-log.d.ts +7 -0
- package/dist/lifecycle-log.js +53 -0
- package/dist/lifecycle.d.ts +2 -0
- package/dist/lifecycle.js +21 -0
- package/dist/model-signature.d.ts +2 -0
- package/dist/model-signature.js +7 -0
- package/dist/monitor.d.ts +14 -0
- package/dist/monitor.js +272 -0
- package/dist/participant-admission.d.ts +5 -0
- package/dist/participant-admission.js +30 -0
- package/dist/protocol.d.ts +102 -0
- package/dist/protocol.js +97 -0
- package/dist/resolver-host.d.ts +11 -0
- package/dist/resolver-host.js +78 -0
- package/dist/server.d.ts +24 -0
- package/dist/server.js +808 -0
- package/dist/service-host.d.ts +7 -0
- package/dist/service-host.js +174 -0
- package/docs/architecture.md +162 -0
- package/package.json +54 -4
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { compare, valid } from "semver";
|
|
2
|
+
import { jsonEqual } from "./json-document.js";
|
|
3
|
+
function entries(value) {
|
|
4
|
+
if (!Array.isArray(value))
|
|
5
|
+
return undefined;
|
|
6
|
+
const result = new Map();
|
|
7
|
+
for (const item of value) {
|
|
8
|
+
if (typeof item !== "string" && (!item || Array.isArray(item) || typeof item !== "object"))
|
|
9
|
+
return undefined;
|
|
10
|
+
const source = typeof item === "string" ? item : item.source;
|
|
11
|
+
if (typeof source !== "string")
|
|
12
|
+
return undefined;
|
|
13
|
+
const spec = source.startsWith("npm:") ? source.slice(4).trim() : undefined;
|
|
14
|
+
const match = spec?.match(/^(@?[^@]+(?:\/[^@]+)?)(?:@(.+))?$/u);
|
|
15
|
+
const key = match ? `npm:${match[1]}` : source;
|
|
16
|
+
if (result.has(key))
|
|
17
|
+
return undefined;
|
|
18
|
+
const options = typeof item === "string" ? null : Object.fromEntries(Object.entries(item).filter(([key]) => key !== "source"));
|
|
19
|
+
result.set(key, { value: item, options, version: match?.[2] });
|
|
20
|
+
}
|
|
21
|
+
return result;
|
|
22
|
+
}
|
|
23
|
+
/** Compare only explicit npm versions; do not resolve ranges, tags, Git refs or registry state. */
|
|
24
|
+
export function mergePackageVersions(base, local, remote) {
|
|
25
|
+
const ancestor = entries(base ?? []);
|
|
26
|
+
const left = entries(local);
|
|
27
|
+
const right = entries(remote);
|
|
28
|
+
if (!ancestor || !left || !right)
|
|
29
|
+
return undefined;
|
|
30
|
+
// Raw Git refs and local path aliases do not prove distinct package identities.
|
|
31
|
+
// Leave changes to those sources under the existing atomic field policy.
|
|
32
|
+
const otherEntries = (items) => [...items].filter(([key]) => !key.startsWith("npm:")).map(([, entry]) => entry.value);
|
|
33
|
+
if (!jsonEqual(otherEntries(ancestor), otherEntries(left)) || !jsonEqual(otherEntries(ancestor), otherEntries(right)))
|
|
34
|
+
return undefined;
|
|
35
|
+
// Package order affects resource precedence. Merge the ordered identity list
|
|
36
|
+
// atomically, then select versions without changing that order.
|
|
37
|
+
const baseKeys = [...ancestor.keys()];
|
|
38
|
+
const leftKeys = [...left.keys()];
|
|
39
|
+
const rightKeys = [...right.keys()];
|
|
40
|
+
const order = jsonEqual(leftKeys, rightKeys)
|
|
41
|
+
? leftKeys
|
|
42
|
+
: jsonEqual(leftKeys, baseKeys)
|
|
43
|
+
? rightKeys
|
|
44
|
+
: jsonEqual(rightKeys, baseKeys)
|
|
45
|
+
? leftKeys
|
|
46
|
+
: undefined;
|
|
47
|
+
if (!order)
|
|
48
|
+
return undefined;
|
|
49
|
+
const output = [];
|
|
50
|
+
for (const key of new Set([...order, ...baseKeys])) {
|
|
51
|
+
const a = ancestor.get(key);
|
|
52
|
+
const l = left.get(key);
|
|
53
|
+
const r = right.get(key);
|
|
54
|
+
let chosen;
|
|
55
|
+
if (l &&
|
|
56
|
+
r &&
|
|
57
|
+
key.startsWith("npm:") &&
|
|
58
|
+
l.version &&
|
|
59
|
+
r.version &&
|
|
60
|
+
valid(l.version) &&
|
|
61
|
+
valid(r.version) &&
|
|
62
|
+
jsonEqual(l.options, r.options)) {
|
|
63
|
+
const precedence = compare(l.version, r.version);
|
|
64
|
+
if (precedence !== 0)
|
|
65
|
+
chosen = precedence > 0 ? l : r;
|
|
66
|
+
}
|
|
67
|
+
// Build metadata and equivalent version spelling do not establish a higher version.
|
|
68
|
+
if (!chosen) {
|
|
69
|
+
if (jsonEqual(l?.value, r?.value))
|
|
70
|
+
chosen = l;
|
|
71
|
+
else if (jsonEqual(l?.value, a?.value))
|
|
72
|
+
chosen = r;
|
|
73
|
+
else if (jsonEqual(r?.value, a?.value))
|
|
74
|
+
chosen = l;
|
|
75
|
+
else
|
|
76
|
+
return undefined;
|
|
77
|
+
}
|
|
78
|
+
if (chosen)
|
|
79
|
+
output.push(chosen.value);
|
|
80
|
+
}
|
|
81
|
+
return output;
|
|
82
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { RemoteHead } from "../backends/sync-backend.js";
|
|
2
|
+
import type { AnySyncConfig } from "../settings/settings-types.js";
|
|
3
|
+
import type { Snapshot, SnapshotFile } from "../snapshot/snapshot-types.js";
|
|
4
|
+
import type { SyncState } from "../state/state-types.js";
|
|
5
|
+
import type { FileMergeDecision } from "./file-merge-planner.js";
|
|
6
|
+
/** Withholding preserves the complete local version, including explicit absence. */
|
|
7
|
+
export declare function acceptedMergeFiles(decisions: readonly FileMergeDecision[], localFiles: readonly SnapshotFile[], withheld: ReadonlySet<string>): SnapshotFile[];
|
|
8
|
+
export interface PartialProgress {
|
|
9
|
+
previous: SyncState;
|
|
10
|
+
groups: {
|
|
11
|
+
paths: string[];
|
|
12
|
+
artifact: string;
|
|
13
|
+
}[];
|
|
14
|
+
}
|
|
15
|
+
export declare function progressState(config: AnySyncConfig, head: RemoteHead, snapshot: Snapshot, progress: PartialProgress): SyncState;
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { fileHashMap } from "./sync-state.js";
|
|
2
|
+
/** Withholding preserves the complete local version, including explicit absence. */
|
|
3
|
+
export function acceptedMergeFiles(decisions, localFiles, withheld) {
|
|
4
|
+
const localByPath = new Map(localFiles.map((file) => [file.path, file]));
|
|
5
|
+
return decisions.flatMap((decision) => {
|
|
6
|
+
const file = withheld.has(decision.path)
|
|
7
|
+
? localByPath.get(decision.path)
|
|
8
|
+
: decision.kind === "accepted"
|
|
9
|
+
? decision.file
|
|
10
|
+
: undefined;
|
|
11
|
+
return file ? [file] : [];
|
|
12
|
+
});
|
|
13
|
+
}
|
|
14
|
+
export function progressState(config, head, snapshot, progress) {
|
|
15
|
+
const withheld = new Set(progress.groups.flatMap((group) => group.paths));
|
|
16
|
+
const current = fileHashMap(snapshot);
|
|
17
|
+
const hashes = Object.assign(Object.create(null), progress.previous.lastFileHashes);
|
|
18
|
+
for (const key of new Set([...Object.keys(hashes), ...Object.keys(current)])) {
|
|
19
|
+
if (withheld.has(key))
|
|
20
|
+
continue;
|
|
21
|
+
const value = current[key];
|
|
22
|
+
if (value === undefined)
|
|
23
|
+
delete hashes[key];
|
|
24
|
+
else
|
|
25
|
+
hashes[key] = value;
|
|
26
|
+
}
|
|
27
|
+
return {
|
|
28
|
+
...progress.previous,
|
|
29
|
+
version: 3,
|
|
30
|
+
profile: config.snapshotIdentity,
|
|
31
|
+
lastFileHashes: hashes,
|
|
32
|
+
include: [...config.include],
|
|
33
|
+
exclude: config.exclude === undefined ? undefined : [...config.exclude],
|
|
34
|
+
localFields: config.localFields,
|
|
35
|
+
lastObservedSnapshot: head.snapshotId,
|
|
36
|
+
lastObservedRevision: head.revision,
|
|
37
|
+
unresolved: progress.groups,
|
|
38
|
+
};
|
|
39
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type { RemoteHead, SyncBackend } from "../backends/sync-backend.js";
|
|
2
|
+
import type { AnySyncConfig } from "../settings/settings-types.js";
|
|
3
|
+
import type { Snapshot } from "../snapshot/snapshot-types.js";
|
|
4
|
+
import { type RemoteSelectionState } from "./sync-policy.js";
|
|
5
|
+
export declare function readSnapshotForHead(backend: SyncBackend, head: RemoteHead, signal?: AbortSignal): Promise<Snapshot>;
|
|
6
|
+
export declare function requireCompatibleRemoteSelection(config: AnySyncConfig, snapshot: Snapshot): void;
|
|
7
|
+
export declare function formatRemoteSelectionStatus(state: RemoteSelectionState | undefined): string;
|
|
8
|
+
export declare function readRemoteSnapshot(backend: SyncBackend, config: AnySyncConfig, signal?: AbortSignal, options?: {
|
|
9
|
+
allowSelectionDifference?: boolean;
|
|
10
|
+
}): Promise<{
|
|
11
|
+
head: undefined;
|
|
12
|
+
snapshot: undefined;
|
|
13
|
+
selectionState: undefined;
|
|
14
|
+
} | {
|
|
15
|
+
head: RemoteHead;
|
|
16
|
+
snapshot: {
|
|
17
|
+
id: string;
|
|
18
|
+
createdAt: string;
|
|
19
|
+
machine: string;
|
|
20
|
+
profile: string;
|
|
21
|
+
localFields?: string[];
|
|
22
|
+
version: number;
|
|
23
|
+
syncSessions: boolean | undefined;
|
|
24
|
+
selection: import("../snapshot/snapshot-types.js").SnapshotSelection;
|
|
25
|
+
files: import("../snapshot/snapshot-types.js").SnapshotFile[];
|
|
26
|
+
};
|
|
27
|
+
selectionState: RemoteSelectionState;
|
|
28
|
+
}>;
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { syncConfigReviewFingerprint } from "../settings/config.js";
|
|
2
|
+
import { filterSnapshotForConfigPolicy } from "../snapshot/snapshot.js";
|
|
3
|
+
import { safeTerminalText } from "../ui/terminal-text.js";
|
|
4
|
+
import { sameLocalFields, validatePortableSnapshot } from "./local-fields.js";
|
|
5
|
+
import { throwIfAborted } from "./signals.js";
|
|
6
|
+
import { inspectRemoteSelection, remoteSelectionMismatch, sameSnapshotSelection, } from "./sync-policy.js";
|
|
7
|
+
export async function readSnapshotForHead(backend, head, signal) {
|
|
8
|
+
const snapshot = await backend.readSnapshot(head.snapshotRef, signal);
|
|
9
|
+
throwIfAborted(signal);
|
|
10
|
+
validatePortableSnapshot(snapshot);
|
|
11
|
+
if (snapshot.id !== head.snapshotId) {
|
|
12
|
+
throw new Error(`Remote head ${head.snapshotId} resolved to unexpected snapshot ${snapshot.id}.`);
|
|
13
|
+
}
|
|
14
|
+
if (head.selection) {
|
|
15
|
+
if (!sameSnapshotSelection(head.selection, snapshot.selection)) {
|
|
16
|
+
throw new Error("Remote head selection does not match its immutable snapshot.");
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
return snapshot;
|
|
20
|
+
}
|
|
21
|
+
export function requireCompatibleRemoteSelection(config, snapshot) {
|
|
22
|
+
if (!sameLocalFields(config.localFields, snapshot.localFields))
|
|
23
|
+
throw new Error("Remote local-field policy differs; confirm a reviewed directional migration. Old history is not erased.");
|
|
24
|
+
const state = inspectRemoteSelection(config.include, snapshot, config.exclude);
|
|
25
|
+
if (state.kind === "different") {
|
|
26
|
+
throw remoteSelectionMismatch(config, state.include, syncConfigReviewFingerprint(config), state.exclude);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
export function formatRemoteSelectionStatus(state) {
|
|
30
|
+
if (!state)
|
|
31
|
+
return "remote included content: unavailable (remote is empty)";
|
|
32
|
+
if (state.kind === "same")
|
|
33
|
+
return "remote included content: matches this setup";
|
|
34
|
+
if (state.kind === "legacy") {
|
|
35
|
+
return `remote included content: unavailable in legacy snapshot (${state.discovered.length} path${state.discovered.length === 1 ? "" : "s"} discovered, partial)`;
|
|
36
|
+
}
|
|
37
|
+
return [
|
|
38
|
+
"remote included content: differs from this setup",
|
|
39
|
+
`remote-only selection: ${safeList(state.remoteOnly)}`,
|
|
40
|
+
`local-only selection: ${safeList(state.localOnly)}`,
|
|
41
|
+
`remote excluded paths: ${safeList(state.exclude ?? [])}`,
|
|
42
|
+
].join("\n");
|
|
43
|
+
}
|
|
44
|
+
function safeList(values) {
|
|
45
|
+
return values.length > 0 ? values.map(safeTerminalText).join(", ") : "none";
|
|
46
|
+
}
|
|
47
|
+
export async function readRemoteSnapshot(backend, config, signal, options = {}) {
|
|
48
|
+
const head = await backend.readHead(signal);
|
|
49
|
+
if (!head)
|
|
50
|
+
return { head: undefined, snapshot: undefined, selectionState: undefined };
|
|
51
|
+
const snapshot = await readSnapshotForHead(backend, head, signal);
|
|
52
|
+
const selectionState = inspectRemoteSelection(config.include, snapshot, config.exclude);
|
|
53
|
+
if (!options.allowSelectionDifference && selectionState.kind === "different") {
|
|
54
|
+
const configIdentity = syncConfigReviewFingerprint(config);
|
|
55
|
+
throw remoteSelectionMismatch(config, selectionState.include, configIdentity, selectionState.exclude);
|
|
56
|
+
}
|
|
57
|
+
return {
|
|
58
|
+
head,
|
|
59
|
+
snapshot: filterSnapshotForConfigPolicy(snapshot, config),
|
|
60
|
+
selectionState,
|
|
61
|
+
};
|
|
62
|
+
}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import { type Api, type Model } from "@earendil-works/pi-ai";
|
|
2
|
+
import type { EngineModelStream } from "../host.js";
|
|
3
|
+
/** Adapt only the public stream surface; do not leak SDK-private class identity to hosts. */
|
|
4
|
+
export declare function forwardResolverStream(upstream: EngineModelStream, model: Model<Api>): import("@earendil-works/pi-ai").AssistantMessageEventStream;
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { createAssistantMessageEventStream } from "@earendil-works/pi-ai";
|
|
2
|
+
/** Adapt only the public stream surface; do not leak SDK-private class identity to hosts. */
|
|
3
|
+
export function forwardResolverStream(upstream, model) {
|
|
4
|
+
const stream = createAssistantMessageEventStream();
|
|
5
|
+
void (async () => {
|
|
6
|
+
for await (const event of upstream)
|
|
7
|
+
stream.push(event);
|
|
8
|
+
stream.end(await upstream.result());
|
|
9
|
+
})().catch(() => {
|
|
10
|
+
stream.push({
|
|
11
|
+
type: "error",
|
|
12
|
+
reason: "error",
|
|
13
|
+
error: {
|
|
14
|
+
role: "assistant",
|
|
15
|
+
api: model.api,
|
|
16
|
+
provider: model.provider,
|
|
17
|
+
model: model.id,
|
|
18
|
+
content: [],
|
|
19
|
+
usage: {
|
|
20
|
+
input: 0,
|
|
21
|
+
output: 0,
|
|
22
|
+
cacheRead: 0,
|
|
23
|
+
cacheWrite: 0,
|
|
24
|
+
totalTokens: 0,
|
|
25
|
+
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },
|
|
26
|
+
},
|
|
27
|
+
stopReason: "error",
|
|
28
|
+
errorMessage: "Resolver transport failed.",
|
|
29
|
+
timestamp: Date.now(),
|
|
30
|
+
},
|
|
31
|
+
});
|
|
32
|
+
});
|
|
33
|
+
return stream;
|
|
34
|
+
}
|
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
import { CURRENT_SESSION_VERSION } from "@earendil-works/pi-coding-agent";
|
|
2
|
+
import { parseJsonObjectDocument } from "./json-document.js";
|
|
3
|
+
import { text } from "./text-merge.js";
|
|
4
|
+
// Public header IDs use Pi assertValidSessionId; generated entry IDs have a separate grammar.
|
|
5
|
+
const sessionId = (value) => typeof value === "string" && /^[A-Za-z0-9](?:[A-Za-z0-9._-]*[A-Za-z0-9])?$/u.test(value);
|
|
6
|
+
const id = (value) => typeof value === "string" && /^[a-zA-Z0-9_-]{1,128}$/u.test(value);
|
|
7
|
+
const content = (value, types = ["text", "image", "thinking", "toolCall"]) => typeof value === "string" ||
|
|
8
|
+
(Array.isArray(value) &&
|
|
9
|
+
value.every((block) => object(block) &&
|
|
10
|
+
types.includes(String(block.type)) &&
|
|
11
|
+
((block.type === "text" && typeof block.text === "string") ||
|
|
12
|
+
(block.type === "image" && typeof block.data === "string" && typeof block.mimeType === "string") ||
|
|
13
|
+
(block.type === "thinking" && typeof block.thinking === "string") ||
|
|
14
|
+
(block.type === "toolCall" &&
|
|
15
|
+
typeof block.id === "string" &&
|
|
16
|
+
typeof block.name === "string" &&
|
|
17
|
+
object(block.arguments)))));
|
|
18
|
+
const message = (value) => {
|
|
19
|
+
if (!object(value) || typeof value.timestamp !== "number" || !Number.isFinite(value.timestamp))
|
|
20
|
+
return false;
|
|
21
|
+
switch (value.role) {
|
|
22
|
+
case "system":
|
|
23
|
+
return (content(value.content, ["text"]) &&
|
|
24
|
+
(value.sections === undefined ||
|
|
25
|
+
(object(value.sections) &&
|
|
26
|
+
Object.values(value.sections).every((section) => section === null || typeof section === "string"))) &&
|
|
27
|
+
(value.toolsAdded === undefined ||
|
|
28
|
+
(Array.isArray(value.toolsAdded) &&
|
|
29
|
+
value.toolsAdded.every((tool) => object(tool) &&
|
|
30
|
+
typeof tool.name === "string" &&
|
|
31
|
+
typeof tool.description === "string" &&
|
|
32
|
+
object(tool.parameters)))) &&
|
|
33
|
+
(value.toolsRemoved === undefined ||
|
|
34
|
+
(Array.isArray(value.toolsRemoved) &&
|
|
35
|
+
value.toolsRemoved.every((tool) => object(tool) && typeof tool.name === "string"))));
|
|
36
|
+
case "user":
|
|
37
|
+
return content(value.content, ["text", "image"]);
|
|
38
|
+
case "assistant":
|
|
39
|
+
return (Array.isArray(value.content) &&
|
|
40
|
+
content(value.content, ["text", "thinking", "toolCall"]) &&
|
|
41
|
+
typeof value.api === "string" &&
|
|
42
|
+
typeof value.provider === "string" &&
|
|
43
|
+
typeof value.model === "string" &&
|
|
44
|
+
object(value.usage) &&
|
|
45
|
+
["pending", "stop", "length", "toolUse", "error", "aborted", "deferred"].includes(String(value.stopReason)));
|
|
46
|
+
case "toolResult":
|
|
47
|
+
return (typeof value.toolCallId === "string" &&
|
|
48
|
+
typeof value.toolName === "string" &&
|
|
49
|
+
Array.isArray(value.content) &&
|
|
50
|
+
content(value.content, ["text", "image"]) &&
|
|
51
|
+
typeof value.isError === "boolean");
|
|
52
|
+
case "custom":
|
|
53
|
+
return (typeof value.customType === "string" &&
|
|
54
|
+
content(value.content, ["text", "image"]) &&
|
|
55
|
+
typeof value.display === "boolean");
|
|
56
|
+
case "bashExecution":
|
|
57
|
+
return (typeof value.command === "string" &&
|
|
58
|
+
typeof value.output === "string" &&
|
|
59
|
+
typeof value.cancelled === "boolean" &&
|
|
60
|
+
typeof value.truncated === "boolean");
|
|
61
|
+
case "branchSummary":
|
|
62
|
+
return typeof value.summary === "string" && (value.fromId === null || typeof value.fromId === "string");
|
|
63
|
+
case "compactionSummary":
|
|
64
|
+
return (typeof value.summary === "string" &&
|
|
65
|
+
typeof value.tokensBefore === "number" &&
|
|
66
|
+
Number.isFinite(value.tokensBefore) &&
|
|
67
|
+
value.tokensBefore >= 0);
|
|
68
|
+
default:
|
|
69
|
+
return false;
|
|
70
|
+
}
|
|
71
|
+
};
|
|
72
|
+
const object = (value) => Boolean(value) && typeof value === "object" && !Array.isArray(value);
|
|
73
|
+
/** Stricter than Pi's repair-tolerant reader: never skip malformed lines or migrate a log. */
|
|
74
|
+
export function validateSession(bytes) {
|
|
75
|
+
if (CURRENT_SESSION_VERSION !== 3)
|
|
76
|
+
throw new Error("Unaudited Pi session format; require review.");
|
|
77
|
+
const raw = text(bytes);
|
|
78
|
+
if (!raw.endsWith("\n"))
|
|
79
|
+
throw new Error("Incomplete session tail.");
|
|
80
|
+
const rows = raw
|
|
81
|
+
.slice(0, -1)
|
|
82
|
+
.split("\n")
|
|
83
|
+
.map((line) => {
|
|
84
|
+
if (line.startsWith("\uFEFF"))
|
|
85
|
+
throw new Error("Unexpected session-record BOM; require review.");
|
|
86
|
+
return parseJsonObjectDocument(Buffer.from(line)).root.value;
|
|
87
|
+
});
|
|
88
|
+
if (rows.length > 16_384)
|
|
89
|
+
throw new Error("Session entry bound exceeded.");
|
|
90
|
+
const header = rows[0];
|
|
91
|
+
if (!object(header) ||
|
|
92
|
+
header.type !== "session" ||
|
|
93
|
+
header.version !== CURRENT_SESSION_VERSION ||
|
|
94
|
+
!sessionId(header.id) ||
|
|
95
|
+
typeof header.cwd !== "string" ||
|
|
96
|
+
typeof header.timestamp !== "string" ||
|
|
97
|
+
!Number.isFinite(Date.parse(header.timestamp)) ||
|
|
98
|
+
(header.parentSession !== undefined && typeof header.parentSession !== "string"))
|
|
99
|
+
throw new Error("Unsupported session header.");
|
|
100
|
+
const ids = new Set();
|
|
101
|
+
const known = (value) => id(value) && ids.has(value);
|
|
102
|
+
for (const row of rows.slice(1)) {
|
|
103
|
+
if (!object(row) ||
|
|
104
|
+
!id(row.id) ||
|
|
105
|
+
ids.has(row.id) ||
|
|
106
|
+
typeof row.timestamp !== "string" ||
|
|
107
|
+
!Number.isFinite(Date.parse(row.timestamp)) ||
|
|
108
|
+
(row.parentId !== null && !known(row.parentId)))
|
|
109
|
+
throw new Error("Invalid session graph.");
|
|
110
|
+
const strings = (...keys) => keys.every((key) => typeof row[key] === "string");
|
|
111
|
+
let valid = false;
|
|
112
|
+
switch (row.type) {
|
|
113
|
+
case "message":
|
|
114
|
+
valid = message(row.message);
|
|
115
|
+
break;
|
|
116
|
+
case "thinking_level_change":
|
|
117
|
+
valid = strings("thinkingLevel");
|
|
118
|
+
break;
|
|
119
|
+
case "model_change":
|
|
120
|
+
valid = strings("provider", "modelId");
|
|
121
|
+
break;
|
|
122
|
+
case "usage":
|
|
123
|
+
valid = strings("kind", "provider", "model") && object(row.usage);
|
|
124
|
+
break;
|
|
125
|
+
case "compaction":
|
|
126
|
+
valid =
|
|
127
|
+
strings("summary") &&
|
|
128
|
+
(known(row.firstKeptEntryId) || row.firstKeptEntryId === row.id) &&
|
|
129
|
+
(row.systemMessage === undefined ||
|
|
130
|
+
(object(row.systemMessage) && row.systemMessage.role === "system" && message(row.systemMessage))) &&
|
|
131
|
+
typeof row.tokensBefore === "number" &&
|
|
132
|
+
Number.isFinite(row.tokensBefore) &&
|
|
133
|
+
row.tokensBefore >= 0;
|
|
134
|
+
break;
|
|
135
|
+
case "branch_summary":
|
|
136
|
+
valid = strings("summary") && (known(row.fromId) || row.fromId === "root");
|
|
137
|
+
break;
|
|
138
|
+
case "custom":
|
|
139
|
+
valid = strings("customType");
|
|
140
|
+
break;
|
|
141
|
+
case "custom_message":
|
|
142
|
+
valid = strings("customType") && typeof row.display === "boolean" && content(row.content, ["text", "image"]);
|
|
143
|
+
break;
|
|
144
|
+
case "context_edit":
|
|
145
|
+
valid =
|
|
146
|
+
known(row.targetId) &&
|
|
147
|
+
(row.replacement === null || (object(row.replacement) && content(row.replacement.content)));
|
|
148
|
+
break;
|
|
149
|
+
case "label":
|
|
150
|
+
valid = known(row.targetId) && (row.label === undefined || typeof row.label === "string");
|
|
151
|
+
break;
|
|
152
|
+
case "session_info":
|
|
153
|
+
valid = row.name === undefined || typeof row.name === "string";
|
|
154
|
+
break;
|
|
155
|
+
}
|
|
156
|
+
if (!valid)
|
|
157
|
+
throw new Error("Unsupported session entry.");
|
|
158
|
+
ids.add(row.id);
|
|
159
|
+
}
|
|
160
|
+
return header.id;
|
|
161
|
+
}
|
|
162
|
+
export function mergeSession(base, local, remote) {
|
|
163
|
+
try {
|
|
164
|
+
const identity = validateSession(base);
|
|
165
|
+
if (validateSession(local) !== identity || validateSession(remote) !== identity)
|
|
166
|
+
return;
|
|
167
|
+
const prefix = (left, right) => right.length >= left.length && right.subarray(0, left.length).equals(left);
|
|
168
|
+
if (!prefix(base, local) || !prefix(base, remote))
|
|
169
|
+
return;
|
|
170
|
+
if (prefix(local, remote))
|
|
171
|
+
return remote;
|
|
172
|
+
if (prefix(remote, local))
|
|
173
|
+
return local;
|
|
174
|
+
}
|
|
175
|
+
catch {
|
|
176
|
+
return;
|
|
177
|
+
}
|
|
178
|
+
}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { CommandOptions } from "../commands/command-types.js";
|
|
2
|
+
import type { EngineContext } from "../host.js";
|
|
3
|
+
import type { SyncLoaders } from "./sync-loaders.js";
|
|
4
|
+
/** Existing legacy policy; service ownership/consent is established by its caller. */
|
|
5
|
+
export declare function pushSessionsOnShutdown(ctx: EngineContext, options: CommandOptions, loaders?: Pick<SyncLoaders, "operations">): Promise<void>;
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { loadConfig } from "../settings/config.js";
|
|
2
|
+
import { snapshotOptionsForContext } from "../snapshot/session-paths.js";
|
|
3
|
+
import { createSnapshot } from "../snapshot/snapshot.js";
|
|
4
|
+
import { withLock } from "../state/lock.js";
|
|
5
|
+
import { withStateDirectoryAccess } from "../state/state-directory.js";
|
|
6
|
+
import { readStateForConfig } from "../state/sync-state-store.js";
|
|
7
|
+
import { captureMutationOwner } from "./sync-local.js";
|
|
8
|
+
import { syncSessionsSelected } from "./sync-policy.js";
|
|
9
|
+
import { hasLocalChanges } from "./sync-state.js";
|
|
10
|
+
/** Existing legacy policy; service ownership/consent is established by its caller. */
|
|
11
|
+
export async function pushSessionsOnShutdown(ctx, options, loaders = { operations: () => import("./sync-operations.js") }) {
|
|
12
|
+
const validate = captureMutationOwner(ctx, options.signal);
|
|
13
|
+
const config = await loadConfig(options.setup);
|
|
14
|
+
validate();
|
|
15
|
+
if (!config.automatic || !syncSessionsSelected(config))
|
|
16
|
+
return;
|
|
17
|
+
await withStateDirectoryAccess(() => withLock("auto-session-push", async () => {
|
|
18
|
+
const state = await readStateForConfig(config);
|
|
19
|
+
validate();
|
|
20
|
+
const local = await createSnapshot(config.snapshotIdentity, {
|
|
21
|
+
...snapshotOptionsForContext(ctx, config),
|
|
22
|
+
signal: options.signal,
|
|
23
|
+
});
|
|
24
|
+
validate();
|
|
25
|
+
if (!hasLocalChanges(local, state, config))
|
|
26
|
+
return;
|
|
27
|
+
const operations = await loaders.operations();
|
|
28
|
+
validate();
|
|
29
|
+
await operations.push(ctx, { ...options, auto: true, yes: true, silent: true, reload: false }, { config, state, local });
|
|
30
|
+
validate();
|
|
31
|
+
}));
|
|
32
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { AnySyncConfig } from "../settings/settings-types.js";
|
|
2
|
+
import type { Snapshot } from "../snapshot/snapshot-types.js";
|
|
3
|
+
import type { SyncState } from "../state/state-types.js";
|
|
4
|
+
import type { FileMergePlan } from "./file-merge-planner.js";
|
|
5
|
+
export declare function resolveSettingsConflicts(config: AnySyncConfig, state: SyncState, local: Snapshot, remote: Snapshot, plan: FileMergePlan): Promise<{
|
|
6
|
+
plan: FileMergePlan;
|
|
7
|
+
fields: string[];
|
|
8
|
+
}>;
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import { readMergeAncestor } from "../state/merge-baseline-store.js";
|
|
3
|
+
import { mergeAcceptedSettingsPackages, mergeSettingsJson } from "./settings-merge.js";
|
|
4
|
+
export async function resolveSettingsConflicts(config, state, local, remote, plan) {
|
|
5
|
+
if (plan.kind !== "planned")
|
|
6
|
+
return { plan, fields: [] };
|
|
7
|
+
const fields = [];
|
|
8
|
+
const decisions = [...plan.decisions];
|
|
9
|
+
for (let index = 0; index < decisions.length; index++) {
|
|
10
|
+
const decision = decisions[index];
|
|
11
|
+
if (decision?.path !== "settings.json")
|
|
12
|
+
continue;
|
|
13
|
+
if (decision.kind === "conflict" ? decision.reason !== "both-changed" : !config.agentMode)
|
|
14
|
+
continue;
|
|
15
|
+
const left = local.files.find((file) => file.path === decision.path);
|
|
16
|
+
const right = remote.files.find((file) => file.path === decision.path);
|
|
17
|
+
if (!left || !right)
|
|
18
|
+
continue; // File deletion/addition is not a JSON field merge.
|
|
19
|
+
const ancestor = await readMergeAncestor(config, state, decision.path);
|
|
20
|
+
if (!ancestor)
|
|
21
|
+
continue;
|
|
22
|
+
const leftBytes = Buffer.from(left.contentBase64, "base64");
|
|
23
|
+
const rightBytes = Buffer.from(right.contentBase64, "base64");
|
|
24
|
+
let content;
|
|
25
|
+
if (decision.kind === "accepted") {
|
|
26
|
+
if (!decision.file)
|
|
27
|
+
continue;
|
|
28
|
+
const patched = mergeAcceptedSettingsPackages(ancestor, leftBytes, rightBytes, Buffer.from(decision.file.contentBase64, "base64"));
|
|
29
|
+
if (!patched)
|
|
30
|
+
continue;
|
|
31
|
+
content = patched;
|
|
32
|
+
}
|
|
33
|
+
else {
|
|
34
|
+
const merged = mergeSettingsJson(ancestor, leftBytes, rightBytes, undefined, Boolean(config.agentMode));
|
|
35
|
+
if (merged.kind === "review") {
|
|
36
|
+
fields.push(...merged.fields);
|
|
37
|
+
continue;
|
|
38
|
+
}
|
|
39
|
+
content = merged.content;
|
|
40
|
+
}
|
|
41
|
+
decisions[index] = {
|
|
42
|
+
kind: "accepted",
|
|
43
|
+
path: decision.path,
|
|
44
|
+
source: "merged",
|
|
45
|
+
file: {
|
|
46
|
+
path: decision.path,
|
|
47
|
+
contentBase64: content.toString("base64"),
|
|
48
|
+
sha256: createHash("sha256").update(content).digest("hex"),
|
|
49
|
+
},
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
return {
|
|
53
|
+
plan: { kind: "planned", decisions, conflicts: decisions.filter((decision) => decision.kind === "conflict") },
|
|
54
|
+
fields,
|
|
55
|
+
};
|
|
56
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export type SettingsMergeResult = {
|
|
2
|
+
kind: "merged";
|
|
3
|
+
content: Buffer;
|
|
4
|
+
} | {
|
|
5
|
+
kind: "review";
|
|
6
|
+
reason: "unsupported-format" | "field-conflict";
|
|
7
|
+
fields: string[];
|
|
8
|
+
};
|
|
9
|
+
export declare const COUPLED_FIELDS: readonly [readonly ["defaultProvider", "defaultModel"], readonly ["queueMode", "steeringMode"], readonly ["websockets", "transport"], readonly ["skills", "enableSkillCommands"], readonly ["enableAnalytics", "trackingId"]];
|
|
10
|
+
/** Accepted files need only a package override, not another full-document field merge. */
|
|
11
|
+
export declare function mergeAcceptedSettingsPackages(ancestor: Buffer, local: Buffer, remote: Buffer, accepted: Buffer): Buffer | undefined;
|
|
12
|
+
/** Only global settings.json; objects/arrays are atomic units until their invariants are proven. */
|
|
13
|
+
export declare function mergeSettingsJson(ancestor: Buffer, local: Buffer, remote: Buffer, selections?: ReadonlyMap<string, "local" | "remote">, preferHigherPackageVersions?: boolean): SettingsMergeResult;
|