[Init] Initial commit - NetMesh terminal manager
Some checks failed
build-packages / resolve bundled mosh-client (push) Has been cancelled
build-packages / resolve bundled et-client (push) Has been cancelled
build-packages / build-macos (push) Has been cancelled
build-packages / build-windows (push) Has been cancelled
build-packages / build-linux-x64 (push) Has been cancelled
build-packages / build-linux-arm64 (push) Has been cancelled
build-packages / release (push) Has been cancelled
build-packages / update Nix release metadata (push) Has been cancelled
build-packages / bump homebrew tap (push) Has been cancelled
test / lint-and-test (push) Has been cancelled
AI automation / Route event (push) Has been cancelled
AI automation / Hand reopened issue to maintainers (push) Has been cancelled
AI automation / Clean source issue state (push) Has been cancelled
AI automation / Reconcile handoffs (push) Has been cancelled
AI automation / Classify issue (push) Has been cancelled
AI automation / Claude Code smoke (push) Has been cancelled
AI automation / Review issue follow-up (push) Has been cancelled
AI automation / Publish issue follow-up (push) Has been cancelled
AI automation / Implement with Claude Code (push) Has been cancelled
AI automation / Publish implement PR (push) Has been cancelled
AI automation / Continue queued issue comments (push) Has been cancelled
AI automation / Codex review loop (push) Has been cancelled
AI automation / Publish Codex fix (push) Has been cancelled
AI automation / Clear Codex dispatch marker (push) Has been cancelled
AI automation / Own PR re-request Codex (push) Has been cancelled
AI automation / External PR re-request Codex (push) Has been cancelled
AI automation / Poll Codex reaction / retry (push) Has been cancelled
build-et-binaries / build-linux-x64 (push) Has been cancelled
build-et-binaries / build-linux-arm64 (push) Has been cancelled
build-et-binaries / build-macos-universal (push) Has been cancelled
build-et-binaries / build-windows-x64 (push) Has been cancelled
build-et-binaries / release (push) Has been cancelled
Some checks failed
build-packages / resolve bundled mosh-client (push) Has been cancelled
build-packages / resolve bundled et-client (push) Has been cancelled
build-packages / build-macos (push) Has been cancelled
build-packages / build-windows (push) Has been cancelled
build-packages / build-linux-x64 (push) Has been cancelled
build-packages / build-linux-arm64 (push) Has been cancelled
build-packages / release (push) Has been cancelled
build-packages / update Nix release metadata (push) Has been cancelled
build-packages / bump homebrew tap (push) Has been cancelled
test / lint-and-test (push) Has been cancelled
AI automation / Route event (push) Has been cancelled
AI automation / Hand reopened issue to maintainers (push) Has been cancelled
AI automation / Clean source issue state (push) Has been cancelled
AI automation / Reconcile handoffs (push) Has been cancelled
AI automation / Classify issue (push) Has been cancelled
AI automation / Claude Code smoke (push) Has been cancelled
AI automation / Review issue follow-up (push) Has been cancelled
AI automation / Publish issue follow-up (push) Has been cancelled
AI automation / Implement with Claude Code (push) Has been cancelled
AI automation / Publish implement PR (push) Has been cancelled
AI automation / Continue queued issue comments (push) Has been cancelled
AI automation / Codex review loop (push) Has been cancelled
AI automation / Publish Codex fix (push) Has been cancelled
AI automation / Clear Codex dispatch marker (push) Has been cancelled
AI automation / Own PR re-request Codex (push) Has been cancelled
AI automation / External PR re-request Codex (push) Has been cancelled
AI automation / Poll Codex reaction / retry (push) Has been cancelled
build-et-binaries / build-linux-x64 (push) Has been cancelled
build-et-binaries / build-linux-arm64 (push) Has been cancelled
build-et-binaries / build-macos-universal (push) Has been cancelled
build-et-binaries / build-windows-x64 (push) Has been cancelled
build-et-binaries / release (push) Has been cancelled
This commit is contained in:
98
domain/convergentSync/clock.ts
Normal file
98
domain/convergentSync/clock.ts
Normal file
@@ -0,0 +1,98 @@
|
||||
import type {
|
||||
Dot,
|
||||
HybridLogicalClock,
|
||||
VersionVector,
|
||||
} from './types';
|
||||
import { ConvergentSyncInvariantError } from './types';
|
||||
import { getOwnRecordValue, setOwnRecordValue } from './record';
|
||||
|
||||
export function compareStrings(left: string, right: string): number {
|
||||
if (left < right) return -1;
|
||||
if (left > right) return 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
export function compareDots(left: Dot, right: Dot): number {
|
||||
const deviceOrder = compareStrings(left.deviceId, right.deviceId);
|
||||
return deviceOrder !== 0 ? deviceOrder : left.counter - right.counter;
|
||||
}
|
||||
|
||||
export function dotKey(dot: Dot): string {
|
||||
return `${dot.deviceId}:${dot.counter}`;
|
||||
}
|
||||
|
||||
export function observesDot(vector: VersionVector, dot: Dot): boolean {
|
||||
return (getOwnRecordValue(vector, dot.deviceId) ?? 0) >= dot.counter;
|
||||
}
|
||||
|
||||
export function mergeVersionVectors(
|
||||
left: VersionVector,
|
||||
right: VersionVector,
|
||||
): VersionVector {
|
||||
const merged: VersionVector = {};
|
||||
const deviceIds = new Set([...Object.keys(left), ...Object.keys(right)]);
|
||||
for (const deviceId of [...deviceIds].sort()) {
|
||||
const counter = Math.max(
|
||||
getOwnRecordValue(left, deviceId) ?? 0,
|
||||
getOwnRecordValue(right, deviceId) ?? 0,
|
||||
);
|
||||
if (counter > 0) setOwnRecordValue(merged, deviceId, counter);
|
||||
}
|
||||
return merged;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true when `candidate` has observed every write represented by
|
||||
* `expected`. Extra counters in `candidate` are allowed: they represent a
|
||||
* remote superset that must be joined and propagated, not a failed write.
|
||||
*/
|
||||
export function versionVectorDominates(
|
||||
candidate: VersionVector,
|
||||
expected: VersionVector,
|
||||
): boolean {
|
||||
return Object.keys(expected).every(
|
||||
(deviceId) => (getOwnRecordValue(candidate, deviceId) ?? 0)
|
||||
>= (getOwnRecordValue(expected, deviceId) ?? 0),
|
||||
);
|
||||
}
|
||||
|
||||
export function versionVectorsEqual(
|
||||
left: VersionVector,
|
||||
right: VersionVector,
|
||||
): boolean {
|
||||
return versionVectorDominates(left, right) && versionVectorDominates(right, left);
|
||||
}
|
||||
|
||||
export function compareHybridLogicalClocks(
|
||||
left: HybridLogicalClock,
|
||||
right: HybridLogicalClock,
|
||||
): number {
|
||||
if (left.wallTime !== right.wallTime) return left.wallTime - right.wallTime;
|
||||
return left.logical - right.logical;
|
||||
}
|
||||
|
||||
export function maxHybridLogicalClock(
|
||||
left: HybridLogicalClock,
|
||||
right: HybridLogicalClock,
|
||||
): HybridLogicalClock {
|
||||
return compareHybridLogicalClocks(left, right) >= 0
|
||||
? { ...left }
|
||||
: { ...right };
|
||||
}
|
||||
|
||||
export function tickHybridLogicalClock(
|
||||
current: HybridLogicalClock,
|
||||
now: number,
|
||||
): HybridLogicalClock {
|
||||
const safeNow = Number.isFinite(now) ? Math.max(0, Math.floor(now)) : 0;
|
||||
if (!Number.isSafeInteger(safeNow)) {
|
||||
throw new ConvergentSyncInvariantError('Hybrid logical clock wall time is out of range');
|
||||
}
|
||||
if (safeNow > current.wallTime) {
|
||||
return { wallTime: safeNow, logical: 0 };
|
||||
}
|
||||
if (current.logical >= Number.MAX_SAFE_INTEGER) {
|
||||
throw new ConvergentSyncInvariantError('Hybrid logical clock counter exhausted');
|
||||
}
|
||||
return { wallTime: current.wallTime, logical: current.logical + 1 };
|
||||
}
|
||||
Reference in New Issue
Block a user