@rigxyz/tapd 0.6.7 → 0.6.8

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/dist/index.d.ts CHANGED
@@ -443,11 +443,18 @@ declare function revoke(opts: {
443
443
  /**
444
444
  * Apply remote change events to the local filesystem.
445
445
  *
446
- * Phase 2 keeps this simple: last-write-wins, no conflict materialization
447
- * (Phase 4 adds it). Echo suppression is handled via the state DB:
448
- * if the remote event's hash matches our last_seen_hash for the path,
449
- * we know it's our own write coming back through the poll loop and skip
450
- * the download.
446
+ * Echo suppression is handled via the state DB: an event at or before the
447
+ * path's last_applied_change_id (our own push coming back, or a version
448
+ * already superseded here), or whose hash matches our last_seen_hash, is
449
+ * skipped without a download.
450
+ *
451
+ * A change made on both sides is first three-way merged (merge.ts) against
452
+ * the version this device last synced; a clean merge becomes the file and
453
+ * is uploaded on top of the remote change. Otherwise the remote stays the
454
+ * file and the local edit moves to a `.conflict-from.<this device>.<chg>`
455
+ * copy — a local copy that hasn't seen a newer remote version must never
456
+ * replace it (the 2026-09-30 clear-harbor incident). Identical bytes on
457
+ * both sides are not a conflict.
451
458
  *
452
459
  * Materialization is atomic: download to `<file>.tap-tmp.<rand>` then
453
460
  * `rename` into place. Atomic on POSIX; on Windows, `fs.rename` falls
@@ -479,8 +486,8 @@ declare function conflictSidecarPath(path: string, ev: {
479
486
  * Apply mode.
480
487
  *
481
488
  * - `primary`: the local copy has write authority. Local-divergence
482
- * guards protect unpushed edits (sidecars, keep-on-delete, refuse to
483
- * clobber a dir) on the assumption scanAndPush carries them upstream.
489
+ * guards protect unpushed edits (conflict copies, keep-on-delete,
490
+ * refuse to clobber a dir).
484
491
  * - `mirror`: a read-only import of another binding. The mount has no
485
492
  * local authority, so **remote always wins** across every event type —
486
493
  * every divergence guard flips to remote-authoritative. There is no
@@ -502,8 +509,8 @@ type ApplyResult = {
502
509
  errors: ReadonlyArray<ApplyEventError>;
503
510
  };
504
511
  /**
505
- * One iteration of the apply loop: poll `GET /changes?after=<cursor>`,
506
- * materialize each event, update the cursor.
512
+ * One iteration of the apply loop: page through `GET /changes?after=<cursor>`
513
+ * until caught up, materialize each event, update the cursor.
507
514
  *
508
515
  * Per-event errors (hash mismatch, file/directory collision, disk full,
509
516
  * etc.) are caught, logged, and recorded to the state DB — the cursor
@@ -521,6 +528,62 @@ declare function applyOnce(opts: {
521
528
  mode?: ApplyMode;
522
529
  }): Promise<ApplyResult>;
523
530
 
531
+ /**
532
+ * Line-based three-way merge for a file changed on both sides (layer 0:
533
+ * deterministic, no AI). The applier tries it before falling back to a
534
+ * conflict copy: base = the version this device last synced, ours = the
535
+ * local file, theirs = the remote version being applied.
536
+ *
537
+ * The merge itself is node-diff3's `diff3Merge` (overlapping or touching
538
+ * hunks are a conflict; the same change on both sides is not). Two things
539
+ * are layered around it:
540
+ *
541
+ * - Only text it can't corrupt: valid UTF-8, no NUL, ≤ 1 MB per side, and
542
+ * not a format where a clean line merge can still be wrong (TOML, YAML,
543
+ * lockfiles, CSV/TSV). A merged `.json` must still parse.
544
+ * - Bounded work. node-diff3's LCS slows down sharply on many repeated
545
+ * lines (a 5,000-line doc that is one-third blank lines takes seconds),
546
+ * and this runs on the daemon's event loop. So the lines all three
547
+ * versions share at the start and end are set aside, the rest is cut at
548
+ * lines that occur exactly once in every version and in the same order
549
+ * (a merge can't move them), and each piece is merged on its own. Text
550
+ * still too repetitive to merge quickly gives up — a conflict copy,
551
+ * never a hang.
552
+ *
553
+ * Line endings: if every version uses one style, lines are compared
554
+ * without it and the result takes the style a side switched to (ours if
555
+ * both did), else the base's. A file mixing styles is merged as raw lines.
556
+ * The trailing newline follows the same rule.
557
+ */
558
+
559
+ /**
560
+ * One auto-merge: this device's edit to `path` and another member's change
561
+ * `theirsChangeId`, both made on top of `baseChangeId`. Successive merges
562
+ * into an edit not yet uploaded update one record (`theirsChangeId` is
563
+ * then the newest remote change merged in). `changeId` is the upload that
564
+ * carried the merged file, once it went up.
565
+ */
566
+ type MergeRecord = {
567
+ path: string;
568
+ at: string;
569
+ baseChangeId: string | null;
570
+ theirsChangeId: string;
571
+ theirsDeviceId: string | null;
572
+ /** The local bytes before the merge (not stored on the relay). */
573
+ oursHash: string;
574
+ resultHash: string;
575
+ changeId?: string;
576
+ };
577
+ declare function readMerges(stateDb: StateDb): MergeRecord[];
578
+ type MergeResult = {
579
+ ok: true;
580
+ bytes: Buffer;
581
+ } | {
582
+ ok: false;
583
+ reason: string;
584
+ };
585
+ declare function mergeText(path: string, base: Buffer, ours: Buffer, theirs: Buffer): MergeResult;
586
+
524
587
  /**
525
588
  * Claude Code context provider (Context Layer § 4, docs/context-layer-design-doc.md).
526
589
  *
@@ -627,6 +690,8 @@ declare function scanAndPush(opts: {
627
690
  * map and the batch simply carries no intent.
628
691
  */
629
692
  resolveIntents?: (changedPaths: ReadonlyArray<string>) => Promise<Map<string, IntentAssignment>>;
693
+ /** Internal: this is the re-scan that uploads merges a stale_base refusal produced. */
694
+ afterMerge?: boolean;
630
695
  }): Promise<ScanAndPushResult>;
631
696
 
632
697
  /**
@@ -774,6 +839,19 @@ type SubscriptionHandle = {
774
839
  stop(): Promise<void>;
775
840
  /** Force one apply cycle now (through the mutex). */
776
841
  applyNow(): Promise<ApplyResult>;
842
+ /**
843
+ * Apply every remote change, then run `fn` before any other apply
844
+ * starts — so an upload only ever goes out from a tree that has seen the
845
+ * relay's latest. `{ ok: false }` (fn not run) when the changes couldn't
846
+ * be fetched.
847
+ */
848
+ catchUpThen<T>(fn: () => Promise<T>): Promise<{
849
+ ok: true;
850
+ value: T;
851
+ } | {
852
+ ok: false;
853
+ error: unknown;
854
+ }>;
777
855
  /** Underlying state DB. Caller MUST NOT close it — stop() does. */
778
856
  stateDb: StateDb;
779
857
  };
@@ -946,6 +1024,12 @@ declare function subscribe(opts: SseSubscriberOptions): SseSubscriber;
946
1024
  * subscription.ts; this file wires the pieces together and owns the
947
1025
  * primary writer.
948
1026
  *
1027
+ * Every upload goes through the primary subscription's catchUpThen: remote
1028
+ * changes are applied first, under the same lock. When they can't be
1029
+ * fetched (relay down, offline) nothing is uploaded — a copy that hasn't
1030
+ * seen the latest remote changes must not be pushed over them — and the
1031
+ * push is retried.
1032
+ *
949
1033
  * Critical invariant: import mount points are HARD-excluded from the
950
1034
  * primary writer (watcher `ignored` + walker `excludePaths`), so a
951
1035
  * mirrored subtree is never re-uploaded into the primary's own binding
@@ -1014,4 +1098,4 @@ type DaemonHandle = {
1014
1098
  };
1015
1099
  declare function start(opts: StartOptions): Promise<DaemonHandle>;
1016
1100
 
1017
- export { type AddImportOptions, type AddImportResult, AlreadyInitializedError, AlreadyJoinedError, type ApplyMode, type ApplyResult, BINDING_CONFIG_PATH, BUILTIN_IGNORE_LINES, type BindingConfig, type DaemonHandle, type ImportHandle, type ImportMountFile, type ImportSpec, ImportTokenScopeError, type ImportValidationError, ImportValidationFailed, type InitOptions, type InitResult, InvalidInviteUrlError, type JoinOptions, type JoinResult, type LocalPathState, type MintInviteOptions, NotInitializedError, type NotSyncedFile, RelayClient, type RelayClientOptions, RelayError, type RemoveImportResult, STATE_KEY_CONFLICT_COUNT, type ScanAndPushResult, type SseSubscriber, type SseSubscriberOptions, type StartOptions, type StartSubscriptionOptions, type StateDb, type SubscriptionConfig, type SubscriptionHandle, type WalkFile, type WalkResult, type WalkWarning, addImport, applyOnce, bindingConfigFile, conflictSidecarPath, createBinding, hashFile, importMountFile, importStateDbPath, importsDir, init, isAlwaysExcluded, join, list as listInvites, loadIgnore, loadImportMounts, mint as mintInvite, normalizeImportPath, openStateDb, parseInviteUrl, primaryStateDbPath, readBindingConfig, readImportMount, readNotSynced, removeImport, revoke as revokeInvite, scanAndPush, subscribe as sseSubscribe, start, startSubscription, validateImportName, validateImportPath, validateImports, walk, writeBindingConfig, writeImportMount };
1101
+ export { type AddImportOptions, type AddImportResult, AlreadyInitializedError, AlreadyJoinedError, type ApplyMode, type ApplyResult, BINDING_CONFIG_PATH, BUILTIN_IGNORE_LINES, type BindingConfig, type DaemonHandle, type ImportHandle, type ImportMountFile, type ImportSpec, ImportTokenScopeError, type ImportValidationError, ImportValidationFailed, type InitOptions, type InitResult, InvalidInviteUrlError, type JoinOptions, type JoinResult, type LocalPathState, type MergeRecord, type MintInviteOptions, NotInitializedError, type NotSyncedFile, RelayClient, type RelayClientOptions, RelayError, type RemoveImportResult, STATE_KEY_CONFLICT_COUNT, type ScanAndPushResult, type SseSubscriber, type SseSubscriberOptions, type StartOptions, type StartSubscriptionOptions, type StateDb, type SubscriptionConfig, type SubscriptionHandle, type WalkFile, type WalkResult, type WalkWarning, addImport, applyOnce, bindingConfigFile, conflictSidecarPath, createBinding, hashFile, importMountFile, importStateDbPath, importsDir, init, isAlwaysExcluded, join, list as listInvites, loadIgnore, loadImportMounts, mergeText, mint as mintInvite, normalizeImportPath, openStateDb, parseInviteUrl, primaryStateDbPath, readBindingConfig, readImportMount, readMerges, readNotSynced, removeImport, revoke as revokeInvite, scanAndPush, subscribe as sseSubscribe, start, startSubscription, validateImportName, validateImportPath, validateImports, walk, writeBindingConfig, writeImportMount };
package/dist/index.js CHANGED
@@ -26,6 +26,7 @@ import {
26
26
  list,
27
27
  loadIgnore,
28
28
  loadImportMounts,
29
+ mergeText,
29
30
  mint,
30
31
  normalizeImportPath,
31
32
  openStateDb,
@@ -33,6 +34,7 @@ import {
33
34
  primaryStateDbPath,
34
35
  readBindingConfig,
35
36
  readImportMount,
37
+ readMerges,
36
38
  readNotSynced,
37
39
  removeImport,
38
40
  revoke,
@@ -46,7 +48,7 @@ import {
46
48
  walk,
47
49
  writeBindingConfig,
48
50
  writeImportMount
49
- } from "./chunk-32K6ANXS.js";
51
+ } from "./chunk-S5AVBVLP.js";
50
52
  export {
51
53
  AlreadyInitializedError,
52
54
  AlreadyJoinedError,
@@ -74,6 +76,7 @@ export {
74
76
  list as listInvites,
75
77
  loadIgnore,
76
78
  loadImportMounts,
79
+ mergeText,
77
80
  mint as mintInvite,
78
81
  normalizeImportPath,
79
82
  openStateDb,
@@ -81,6 +84,7 @@ export {
81
84
  primaryStateDbPath,
82
85
  readBindingConfig,
83
86
  readImportMount,
87
+ readMerges,
84
88
  readNotSynced,
85
89
  removeImport,
86
90
  revoke as revokeInvite,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rigxyz/tapd",
3
- "version": "0.6.7",
3
+ "version": "0.6.8",
4
4
  "description": "Local daemon for the hosted Tap relay — watches files, hashes content, uploads/downloads objects, applies remote changes. Pairs with @rigxyz/cli for shared-rig sync.",
5
5
  "license": "MIT",
6
6
  "author": "Dylan Bourgeois <dtsbourg@gmail.com>",
@@ -40,7 +40,8 @@
40
40
  "dependencies": {
41
41
  "chokidar": "^4.0.1",
42
42
  "ignore": "^5.3.0",
43
- "kleur": "^4.1.5"
43
+ "kleur": "^4.1.5",
44
+ "node-diff3": "3.2.1"
44
45
  },
45
46
  "devDependencies": {
46
47
  "@tap/core": "workspace:*",