@1agh/maude 2.1.0 → 2.1.1

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.
Files changed (100) hide show
  1. package/apps/studio/access/request.ts +1 -0
  2. package/apps/studio/actions/agents.ts +27 -0
  3. package/apps/studio/actions/defs/app.ts +28 -1
  4. package/apps/studio/actions/defs/sync.ts +4 -0
  5. package/apps/studio/actions.manifest.json +60 -2
  6. package/apps/studio/actions.parity.json +13 -1
  7. package/apps/studio/bin/_curl-local.mjs +7 -4
  8. package/apps/studio/client/app.jsx +16 -4
  9. package/apps/studio/client/home/HomeAccountsRow.jsx +221 -0
  10. package/apps/studio/client/home/HomeConnection.jsx +111 -0
  11. package/apps/studio/client/home/home-accounts-model.js +110 -1
  12. package/apps/studio/client/home/home-api.js +6 -2
  13. package/apps/studio/client/home/home-app.jsx +74 -12
  14. package/apps/studio/client/home/home-model.js +40 -1
  15. package/apps/studio/client/home/home-where-model.js +105 -0
  16. package/apps/studio/client/home/home.css +233 -1
  17. package/apps/studio/client/hooks/use-palette-and-panels.jsx +3 -1
  18. package/apps/studio/client/hooks/use-tabs.jsx +11 -4
  19. package/apps/studio/client/panels/TeamProjects.jsx +137 -134
  20. package/apps/studio/client/share-link.js +12 -0
  21. package/apps/studio/client/styles/_index.css +4 -0
  22. package/apps/studio/client/v2/diagnostics/diagnostics-host.jsx +1 -0
  23. package/apps/studio/client/v2/diagnostics/diagnostics-model.js +32 -2
  24. package/apps/studio/client/v2/diagnostics/diagnostics-sheet.jsx +35 -8
  25. package/apps/studio/client/v2/diagnostics/diagnostics.css +34 -0
  26. package/apps/studio/client/v2/diagnostics/sync-details/sync-details-model.js +94 -3
  27. package/apps/studio/client/v2/diagnostics/sync-details/sync-details.jsx +187 -4
  28. package/apps/studio/client/v2/link-server/LinkServerDialog.jsx +492 -0
  29. package/apps/studio/client/v2/link-server/LinkServerHost.jsx +42 -0
  30. package/apps/studio/client/v2/link-server/link-server-api.js +142 -0
  31. package/apps/studio/client/v2/link-server/link-server-model.js +438 -0
  32. package/apps/studio/client/v2/link-server/link-server.css +109 -0
  33. package/apps/studio/client/v2/onboarding/AdvancedSheets.jsx +31 -86
  34. package/apps/studio/client/v2/onboarding/onboarding-model.js +1 -18
  35. package/apps/studio/client/v2/search/search-keywords.js +3 -2
  36. package/apps/studio/client/v2/settings/account.jsx +4 -2
  37. package/apps/studio/client/v2/settings/advanced-section.jsx +245 -148
  38. package/apps/studio/client/v2/settings/connection-row.jsx +3 -59
  39. package/apps/studio/client/v2/settings/connections-section.jsx +118 -425
  40. package/apps/studio/client/v2/settings/general-section.jsx +138 -43
  41. package/apps/studio/client/v2/settings/project-sync-settings.jsx +4 -3
  42. package/apps/studio/client/v2/settings/reset-section.jsx +14 -3
  43. package/apps/studio/client/v2/settings/settings-card.jsx +142 -0
  44. package/apps/studio/client/v2/settings/settings-cards.js +220 -0
  45. package/apps/studio/client/v2/settings/settings-handlers.js +7 -6
  46. package/apps/studio/client/v2/settings/settings-model.js +91 -31
  47. package/apps/studio/client/v2/settings/settings-sheet.jsx +25 -6
  48. package/apps/studio/client/v2/settings/settings.css +374 -64
  49. package/apps/studio/client/v2/settings/this-project-card.jsx +730 -0
  50. package/apps/studio/client/v2/settings/this-project-model.js +172 -0
  51. package/apps/studio/client/v2/settings/use-connections.js +3 -2
  52. package/apps/studio/client/v2/settings/use-open-card.js +37 -0
  53. package/apps/studio/client/v2/shell/connection-state.js +197 -0
  54. package/apps/studio/client/v2/shell/menu-runner.js +7 -2
  55. package/apps/studio/client/v2/shell/project-account-bus.js +36 -0
  56. package/apps/studio/client/v2/shell/project-account-card.jsx +357 -148
  57. package/apps/studio/client/v2/shell/project-account-model.js +272 -149
  58. package/apps/studio/client/v2/shell/project-account-toast.js +66 -0
  59. package/apps/studio/client/v2/shell/project-account.css +237 -0
  60. package/apps/studio/client/v2/shell/share-cluster-host.jsx +166 -83
  61. package/apps/studio/client/v2/shell/shell-parity.js +3 -2
  62. package/apps/studio/client/v2/shell/shell-parity.json +40 -26
  63. package/apps/studio/client/v2/shell/use-account.js +2 -0
  64. package/apps/studio/client/v2/signin/use-signin.js +1 -1
  65. package/apps/studio/cloud/endpoints.ts +29 -4
  66. package/apps/studio/dist/client.bundle.js +834 -834
  67. package/apps/studio/dist/styles.css +1 -1
  68. package/apps/studio/home/accounts.ts +30 -11
  69. package/apps/studio/home/cloud.ts +6 -1
  70. package/apps/studio/home/hub-health.ts +76 -0
  71. package/apps/studio/home/routes.ts +9 -3
  72. package/apps/studio/http.ts +8 -0
  73. package/apps/studio/migrate/hub.ts +8 -1
  74. package/apps/studio/routes/hub-link-folder.ts +334 -0
  75. package/apps/studio/routes/hub-signin.ts +78 -1
  76. package/apps/studio/routes/index.ts +4 -0
  77. package/apps/studio/routes/sync.ts +22 -4
  78. package/apps/studio/shell/api-key.ts +11 -0
  79. package/apps/studio/sync/accepted-cold-start.ts +144 -8
  80. package/apps/studio/sync/action-stage.ts +18 -1
  81. package/apps/studio/sync/ctl-heal.ts +58 -0
  82. package/apps/studio/sync/file-ledger.ts +38 -4
  83. package/apps/studio/sync/file-plane.ts +226 -19
  84. package/apps/studio/sync/held-deletes.ts +86 -5
  85. package/apps/studio/sync/hub-link.ts +8 -0
  86. package/apps/studio/sync/index.ts +68 -4
  87. package/apps/studio/sync/link-consent.ts +22 -75
  88. package/apps/studio/sync/link-survey.ts +252 -0
  89. package/apps/studio/sync/local-kernel.ts +16 -1
  90. package/apps/studio/sync/seed-progress.ts +12 -0
  91. package/apps/studio/sync/source-recovery.ts +18 -2
  92. package/apps/studio/sync/status.ts +11 -1
  93. package/apps/studio/sync/writer-registry.ts +5 -0
  94. package/apps/studio/whats-new.json +18 -0
  95. package/cli/lib/bash-guard.mjs +19 -0
  96. package/cli/lib/design-help.gen.mjs +1 -1
  97. package/package.json +9 -8
  98. package/apps/studio/client/v2/onboarding/HubSheet.jsx +0 -212
  99. package/apps/studio/client/v2/shell/account-card-model.js +0 -114
  100. package/apps/studio/client/v2/shell/account-card.jsx +0 -189
@@ -308,6 +308,13 @@ export const FIRST_ANCHOR_STORM_LIMIT = 10;
308
308
  * into everyone's; inbound turns a hostile or broken hub into a local wipe.
309
309
  */
310
310
  export const DELETE_BREAKER_MAX = 10;
311
+
312
+ /** A door's 409 that names a tombstone (`deleted` + `seq`, 2.1.1 T9). Hub-supplied: shape-checked. */
313
+ function tombstoneOf(body: Record<string, unknown> | null | undefined): { tombstoneSeq?: number } {
314
+ return body?.deleted === true && Number.isInteger(body.seq) && (body.seq as number) > 0
315
+ ? { tombstoneSeq: body.seq as number }
316
+ : {};
317
+ }
311
318
  export const DELETE_BREAKER_MAX_FRACTION = 0.25;
312
319
 
313
320
  /**
@@ -361,6 +368,9 @@ export const UNREACHABLE_HOLD_MS = 30_000;
361
368
  */
362
369
  export const MAX_DORUCEKA_ROWS = 300;
363
370
 
371
+ /** How many conflict copies one payload names (the rest are in the folder, and in the counts). */
372
+ export const MAX_CONFLICT_COPIES = 50;
373
+
364
374
  /** The last delete-hold we logged, so the same hold is not re-announced every
365
375
  * pass. Observed 47 times in a 72-line log, burying everything else. */
366
376
  let lastDeleteHoldKey = '';
@@ -374,6 +384,7 @@ function deliveryRank(state: DeliveryState): number {
374
384
  return 1;
375
385
  case 'stuck':
376
386
  case 'delete-held':
387
+ case 'delete-in-held':
377
388
  return 2;
378
389
  case 'referenced-but-unoffered':
379
390
  return 3;
@@ -478,6 +489,8 @@ export interface FilePlaneResult {
478
489
  count: number;
479
490
  paths: string[];
480
491
  folders?: HeldDeleteFolder[];
492
+ /** 2.1.1 T8 / N4 — `heldSetDigest` of EVERY path an answer would cover (the list above is capped) */
493
+ digest?: string;
481
494
  };
482
495
  /** Consecutive `reanchor` answers tripped the storm limit; the pass held. */
483
496
  reanchorHeld?: true;
@@ -764,6 +777,13 @@ export const DELETE_HELD_REASON =
764
777
  export const HUB_ONLY_REASON =
765
778
  'deleted on this Mac and kept on the hub — it is not downloaded again';
766
779
 
780
+ /** 2.1.1 T13 — the ledger reason for a `delete-in-held` row. */
781
+ export const DELETE_IN_HELD_REASON =
782
+ 'the project deleted this, as part of a batch too large to remove here without asking';
783
+ /** 2.1.1 T13 — the ledger reason for a `kept-here` row. */
784
+ export const KEPT_HERE_REASON =
785
+ 'the project deleted this and you kept this Mac’s copy — it is not removed here or sent back';
786
+
767
787
  /** One folder of held deletes, for the Sync details row. */
768
788
  export interface HeldDeleteFolder {
769
789
  /** Design-root-relative folder (`''` = the design root itself). */
@@ -798,6 +818,28 @@ export function summarizeHeldDeletes(rows: Readonly<Record<string, LedgerRow>>):
798
818
  return { count: paths.length, paths, folders };
799
819
  }
800
820
 
821
+ /** 2.1.1 T13 — every `delete-in-held` row, grouped like the outbound ones (pure over the rows). */
822
+ export function summarizeHeldIncoming(
823
+ rows: Readonly<Record<string, LedgerRow>>,
824
+ skip?: ReadonlySet<string>
825
+ ): ReturnType<typeof summarizeHeldDeletes> {
826
+ const waiting: Record<string, LedgerRow> = {};
827
+ for (const [rel, row] of Object.entries(rows))
828
+ if (row.state === 'delete-in-held' && !skip?.has(rel))
829
+ waiting[rel] = { ...row, state: 'delete-held' };
830
+ return summarizeHeldDeletes(waiting);
831
+ }
832
+
833
+ /**
834
+ * 2.1.1 T8 — a fingerprint of a held set (sorted paths). Sync details shows a held batch with it and
835
+ * the person's answer carries it back, so an answer applies only to the set that was on screen.
836
+ */
837
+ export function heldSetDigest(paths: readonly string[]): string {
838
+ const h = createHash('sha256');
839
+ for (const p of [...paths].sort()) h.update(p).update('\n');
840
+ return h.digest('hex').slice(0, 32);
841
+ }
842
+
801
843
  /** Does `rel` sit in `folder` (or below it)? `null` folder = every held row. */
802
844
  export function inHeldFolder(rel: string, folder: string | null): boolean {
803
845
  if (folder === null) return true;
@@ -932,7 +974,8 @@ export async function scanLocalFilesAsync(
932
974
  /** Walk the design root (stat only, no reads) and keep what the classifier admits. */
933
975
  function walkAndClassify(
934
976
  designRoot: string,
935
- canvasGroups?: readonly CanvasGroupLike[]
977
+ canvasGroups?: readonly CanvasGroupLike[],
978
+ all = false
936
979
  ): { f: WalkedFile; cls: FileClass }[] {
937
980
  const found: WalkedFile[] = [];
938
981
 
@@ -975,11 +1018,28 @@ function walkAndClassify(
975
1018
  const out: { f: WalkedFile; cls: FileClass }[] = [];
976
1019
  for (const f of found) {
977
1020
  const cls = classifyProjectFile(f.rel, opts);
978
- if (isFilePlaneClass(cls)) out.push({ f, cls });
1021
+ if (all || isFilePlaneClass(cls)) out.push({ f, cls });
979
1022
  }
980
1023
  return out;
981
1024
  }
982
1025
 
1026
+ /**
1027
+ * 2.1.1 — every file the walk sees, with its class (`canvas-owned` included): the link survey
1028
+ * (sync/link-survey.ts) counts canvases and files with the SAME walk and classifier the plane uses,
1029
+ * so "only on this Mac" can never name a file the plane would not send.
1030
+ */
1031
+ export function walkClassifiedAll(
1032
+ designRoot: string,
1033
+ canvasGroups?: readonly CanvasGroupLike[]
1034
+ ): { rel: string; abs: string; size: number; cls: FileClass }[] {
1035
+ return walkAndClassify(designRoot, canvasGroups, true).map(({ f, cls }) => ({
1036
+ rel: f.rel,
1037
+ abs: f.abs,
1038
+ size: f.size,
1039
+ cls,
1040
+ }));
1041
+ }
1042
+
983
1043
  /**
984
1044
  * Fold journal entries into "what the hub currently holds", newest row wins.
985
1045
  */
@@ -1000,6 +1060,11 @@ export interface FilePlane {
1000
1060
  doruceka(): Record<string, DeliveryState>;
1001
1061
  /** How many rows the ledger holds, before the doručenka's cap. */
1002
1062
  dorucekaTotal(): number;
1063
+ /**
1064
+ * 2.1.1 T14 — files in conflict whose copy of this Mac's version still sits beside them: file →
1065
+ * copy (both designRoot-relative). What Sync details names; a row clears when the copy is gone.
1066
+ */
1067
+ conflictCopies(): Record<string, string>;
1003
1068
  /**
1004
1069
  * The media this machine cannot place, in BYTES as well as files — T29.
1005
1070
  * `null` when nothing is blocked, so a healthy project carries no field.
@@ -1021,6 +1086,20 @@ export interface FilePlane {
1021
1086
  answerHeldDeletes(how: 'send' | 'purging' | 'keep', folder: string | null): string[];
1022
1087
  /** The purge for these paths ended (done or not): held again unless the hub deleted them. */
1023
1088
  endPurge(rels: readonly string[]): void;
1089
+ /** 2.1.1 T13 — the held INCOMING deletes (the project removed these; this Mac still has them). */
1090
+ heldIncomingDeletes(): ReturnType<typeof summarizeHeldDeletes>;
1091
+ /**
1092
+ * 2.1.1 T13 — the person's answer to held incoming deletes (`folder` null = all). `remove`: the
1093
+ * next pass quarantines them into `_trash/` in one go, past the breaker. `keep`: they stay on this
1094
+ * Mac and are neither removed nor sent back. Returns the paths the answer covered.
1095
+ * 2.1.1 T8 — `digest` is `heldSetDigest` of the set the person was shown: when the held set has
1096
+ * changed since (it grew, a row settled), nothing is applied and the answer is `null`.
1097
+ */
1098
+ answerIncomingDeletes(
1099
+ how: 'remove' | 'keep',
1100
+ folder: string | null,
1101
+ digest: string
1102
+ ): string[] | null;
1024
1103
  }
1025
1104
 
1026
1105
  export function createFilePlane(opts: FilePlaneOptions): FilePlane {
@@ -1039,6 +1118,8 @@ export function createFilePlane(opts: FilePlaneOptions): FilePlane {
1039
1118
  const confirmedDeletes = new Set<string>();
1040
1119
  /** Issue #207 (7b) — held deletes an owner purge is deleting on the hub right now. */
1041
1120
  const purgingDeletes = new Set<string>();
1121
+ /** 2.1.1 T13 — held incoming deletes the person said to remove here (in memory, like the above). */
1122
+ const acceptedIncoming = new Set<string>();
1042
1123
  const readPropagateDeletes = (): boolean =>
1043
1124
  (typeof opts.propagateDeletes === 'function'
1044
1125
  ? opts.propagateDeletes()
@@ -1114,7 +1195,10 @@ export function createFilePlane(opts: FilePlaneOptions): FilePlane {
1114
1195
  * Capped at one per `REANCHOR_HOLD_RECOVERY_MS`, like the re-anchor storm,
1115
1196
  * so a hub answering `conflict` to every push cannot farm full reads.
1116
1197
  */
1117
- let fullReadOwed = Object.values(ledger.rows()).some((r) => r.state === 'conflict');
1198
+ // 2.1.1 T14 — a conflict that already parked its copy owes nothing: it stays in `conflict` on purpose
1199
+ let fullReadOwed = Object.values(ledger.rows()).some(
1200
+ (r) => r.state === 'conflict' && !r.conflictCopy
1201
+ );
1118
1202
  let lastOwedFullReadAt = Number.NEGATIVE_INFINITY;
1119
1203
 
1120
1204
  /**
@@ -1809,10 +1893,12 @@ export function createFilePlane(opts: FilePlaneOptions): FilePlane {
1809
1893
  */
1810
1894
  async function push(
1811
1895
  local: SyncableLocalFile,
1812
- expect: string | null
1896
+ expect: string | null,
1897
+ /** The tombstone seq this push decides over (2.1.1 T9) — the door asks it of an owner purge. */
1898
+ expectSeq?: number
1813
1899
  ): Promise<
1814
1900
  | { ok: true; seq: number | null }
1815
- | { ok: false; conflict: true; current: string | null }
1901
+ | { ok: false; conflict: true; current: string | null; tombstoneSeq?: number }
1816
1902
  | {
1817
1903
  ok: false;
1818
1904
  conflict?: false;
@@ -1860,7 +1946,7 @@ export function createFilePlane(opts: FilePlaneOptions): FilePlane {
1860
1946
  };
1861
1947
  }
1862
1948
  // T18 — past one request: a resumable session, in verified parts.
1863
- if (local.size > singlePutCeiling()) return pushSession(local, expect);
1949
+ if (local.size > singlePutCeiling()) return pushSession(local, expect, expectSeq);
1864
1950
  // ISSUE #207 — A LARGE BODY STREAMS FROM DISK. Handing fetch the whole file
1865
1951
  // as one buffer cost TWICE the file in memory over TLS (measured: 532 MB of
1866
1952
  // RSS for a 256 MB body; a `Bun.file` body is no better there), copied on
@@ -1906,6 +1992,7 @@ export function createFilePlane(opts: FilePlaneOptions): FilePlane {
1906
1992
  'content-type': 'application/octet-stream',
1907
1993
  'x-maude-content-sha256': local.hash,
1908
1994
  'x-maude-expect-hash': expect ?? 'none',
1995
+ ...(expectSeq !== undefined ? { 'x-maude-expect-seq': String(expectSeq) } : {}),
1909
1996
  // A stream has no length of its own; the door and every proxy in
1910
1997
  // front of it see the same one a buffer would have carried.
1911
1998
  'content-length': String(local.size),
@@ -1915,11 +2002,12 @@ export function createFilePlane(opts: FilePlaneOptions): FilePlane {
1915
2002
  }
1916
2003
  );
1917
2004
  if (res.status === 409) {
1918
- const body = (await res.json().catch(() => ({}))) as { current?: unknown };
2005
+ const body = (await res.json().catch(() => ({}))) as Record<string, unknown>;
1919
2006
  return {
1920
2007
  ok: false,
1921
2008
  conflict: true,
1922
2009
  current: typeof body?.current === 'string' ? body.current : null,
2010
+ ...tombstoneOf(body),
1923
2011
  };
1924
2012
  }
1925
2013
  if (!res.ok) return await refusal(res, { final: true });
@@ -1941,7 +2029,8 @@ export function createFilePlane(opts: FilePlaneOptions): FilePlane {
1941
2029
  */
1942
2030
  async function pushSession(
1943
2031
  local: SyncableLocalFile,
1944
- expect: string | null
2032
+ expect: string | null,
2033
+ expectSeq?: number
1945
2034
  ): Promise<Awaited<ReturnType<typeof push>>> {
1946
2035
  const json = async (res: Response) =>
1947
2036
  (await res.json().catch(() => ({}))) as Record<string, unknown>;
@@ -1957,6 +2046,7 @@ export function createFilePlane(opts: FilePlaneOptions): FilePlane {
1957
2046
  size: local.size,
1958
2047
  sha256: local.hash,
1959
2048
  expectHash: expect ?? 'none',
2049
+ ...(expectSeq !== undefined ? { expectSeq } : {}),
1960
2050
  }),
1961
2051
  signal: AbortSignal.timeout(GET_TIMEOUT_MS),
1962
2052
  });
@@ -2020,6 +2110,7 @@ export function createFilePlane(opts: FilePlaneOptions): FilePlane {
2020
2110
  ok: false,
2021
2111
  conflict: true,
2022
2112
  current: typeof body.current === 'string' ? body.current : null,
2113
+ ...tombstoneOf(body),
2023
2114
  };
2024
2115
  }
2025
2116
  if (!done.ok) return await refusal(done);
@@ -2400,7 +2491,7 @@ export function createFilePlane(opts: FilePlaneOptions): FilePlane {
2400
2491
  delta.delete(rel);
2401
2492
  continue;
2402
2493
  }
2403
- ledger.noteRemote(rel, row.deleted ? null : row.sha256, row.seq);
2494
+ ledger.noteRemote(rel, row.deleted ? null : row.sha256, row.seq, row.deleted === true);
2404
2495
  }
2405
2496
  // Only a COMPLETE full read may say "the hub does not have this". A
2406
2497
  // truncated one is silent about every path past its last page, and
@@ -2502,12 +2593,16 @@ export function createFilePlane(opts: FilePlaneOptions): FilePlane {
2502
2593
  continue;
2503
2594
  }
2504
2595
  }
2596
+ // THE HUB'S LATEST WORD IS A TOMBSTONE — said on this page, or on an earlier one this
2597
+ // machine did not act on (2.1.1 T9: the inbound breaker held it). Remembered by the ledger,
2598
+ // so a held delete stays a delete instead of reading as "the hub lost it" next pass.
2599
+ const tombstoned = row ? row.deleted === true : ledger.remoteTombstoned(rel);
2505
2600
  // A DELETE IS A TRANSFER TOO. The code-module gate below only sees a
2506
2601
  // non-null remote, so a tombstone walked past it: an ancestor this peer
2507
2602
  // recorded (by push, or by agreement) was all `tombstone-agreed` needed
2508
2603
  // to quarantine a code module on the word of a hub that may not move
2509
2604
  // one. Refuse it here, reported, with the file and its row untouched.
2510
- if (row?.deleted && here && !mayReceiveCode()) {
2605
+ if (tombstoned && here && !mayReceiveCode()) {
2511
2606
  const cls = classifyProjectFile(rel, {
2512
2607
  canvasGroups: opts.canvasGroups,
2513
2608
  hasFile: (r) => local.has(r) || existsSync(path.join(designRoot, r)),
@@ -2679,6 +2774,18 @@ export function createFilePlane(opts: FilePlaneOptions): FilePlane {
2679
2774
  ) {
2680
2775
  continue;
2681
2776
  }
2777
+ // 2.1.1 T13 — KEPT HERE: the project deleted it and the person kept this Mac's copy. While the
2778
+ // file is still the bytes they kept and the project's word is still the delete, nothing is
2779
+ // decided: not removed here, not pushed back over the tombstone. An edit here, or a teammate
2780
+ // recreating the file, is a new decision.
2781
+ if (
2782
+ here &&
2783
+ remoteHash === null &&
2784
+ kept?.state === 'kept-here' &&
2785
+ here.hash === (kept.syncedHash ?? kept.lastSeenHash ?? null)
2786
+ ) {
2787
+ continue;
2788
+ }
2682
2789
 
2683
2790
  // GONE FROM HERE, AND THE HUB HOLDS EXACTLY WHAT THIS MAC LAST SAW (issue #207). No recorded
2684
2791
  // ancestor — the upload landed while its answer was lost (a timeout on a slow link), or the
@@ -2688,7 +2795,7 @@ export function createFilePlane(opts: FilePlaneOptions): FilePlane {
2688
2795
  // this decision — the ledger's ancestor moves only after bytes land (`adoptAfter`).
2689
2796
  const ancestor =
2690
2797
  ledger.ancestorOf(rel) ??
2691
- (!here && remoteHash !== null && !row?.deleted && kept?.lastSeenHash === remoteHash
2798
+ (!here && remoteHash !== null && !tombstoned && kept?.lastSeenHash === remoteHash
2692
2799
  ? remoteHash
2693
2800
  : null);
2694
2801
  const state: FileState = {
@@ -2696,7 +2803,7 @@ export function createFilePlane(opts: FilePlaneOptions): FilePlane {
2696
2803
  local: here?.hash ?? null,
2697
2804
  remote: remoteHash,
2698
2805
  ancestor,
2699
- ...(row?.deleted ? { remoteTombstone: true } : {}),
2806
+ ...(tombstoned ? { remoteTombstone: true } : {}),
2700
2807
  ...(degraded ? { epochChanged: true } : {}),
2701
2808
  ...(remoteHash && ledger.outboxHas(remoteHash) ? { selfInFlight: true } : {}),
2702
2809
  ...(propagateDeletes ? { propagateDeletes: true } : {}),
@@ -2714,6 +2821,16 @@ export function createFilePlane(opts: FilePlaneOptions): FilePlane {
2714
2821
  continue;
2715
2822
  }
2716
2823
  if (decision.action === 'noop' && !decision.parkRemote) {
2824
+ // 2.1.1 T14 — A FILE IN CONFLICT STAYS IN CONFLICT WHILE ITS COPY IS BESIDE IT. The pass that
2825
+ // parked this Mac's version also moved the ancestor to the server's bytes, so the next pass
2826
+ // reads the pair as agreement and `adoptAfter` below used to turn the row `on-hub` — the
2827
+ // conflict vanished from Sync details with the copy still on disk and nothing said where.
2828
+ // The row clears when the copy is gone (the person kept one and removed the other) or this
2829
+ // path changes again; the bytes themselves have nothing left to move.
2830
+ if (here && keepsConflictCopy(kept, here.hash)) {
2831
+ out.synced += 1;
2832
+ continue;
2833
+ }
2717
2834
  if (decision.adoptAncestor && here) {
2718
2835
  // Record agreement so the next pass is a stat and nothing more.
2719
2836
  //
@@ -2746,6 +2863,7 @@ export function createFilePlane(opts: FilePlaneOptions): FilePlane {
2746
2863
  ledger.forget(rel);
2747
2864
  purgingDeletes.delete(rel);
2748
2865
  confirmedDeletes.delete(rel);
2866
+ acceptedIncoming.delete(rel);
2749
2867
  } else if (
2750
2868
  !here &&
2751
2869
  remoteHash !== null &&
@@ -2802,7 +2920,11 @@ export function createFilePlane(opts: FilePlaneOptions): FilePlane {
2802
2920
  // gates it). One still waiting for an answer keeps waiting, and a new local delete joins its
2803
2921
  // question rather than slipping out under the limit beside it.
2804
2922
  const hits = work.filter(
2805
- (w) => w.decision.action === action && !(direction === 'out' && confirmedDeletes.has(w.rel))
2923
+ (w) =>
2924
+ w.decision.action === action &&
2925
+ !(direction === 'out' && confirmedDeletes.has(w.rel)) &&
2926
+ // 2.1.1 T13 — an incoming delete the person said to remove here goes through.
2927
+ !(direction === 'in' && acceptedIncoming.has(w.rel))
2806
2928
  );
2807
2929
  const asking =
2808
2930
  direction === 'out' &&
@@ -2830,10 +2952,9 @@ export function createFilePlane(opts: FilePlaneOptions): FilePlane {
2830
2952
  if (ledger.row(w.rel)?.state !== 'delete-held') {
2831
2953
  ledger.setState(w.rel, 'delete-held', { reason: DELETE_HELD_REASON });
2832
2954
  }
2833
- } else {
2834
- ledger.setState(w.rel, 'conflict', {
2835
- reason: 'the project wants this removed, as part of a batch too large to apply unasked',
2836
- });
2955
+ } else if (ledger.row(w.rel)?.state !== 'delete-in-held') {
2956
+ // Its own state (T13): `conflict` read as a both-sides edit and offered no answer.
2957
+ ledger.setState(w.rel, 'delete-in-held', { reason: DELETE_IN_HELD_REASON });
2837
2958
  }
2838
2959
  }
2839
2960
  if (direction === 'out') {
@@ -2843,9 +2964,21 @@ export function createFilePlane(opts: FilePlaneOptions): FilePlane {
2843
2964
  count: all.count,
2844
2965
  paths: all.paths.slice(0, 200),
2845
2966
  folders: all.folders.slice(0, 20),
2967
+ // 2.1.1 N4 — the set an answer would cover (`heldDeletes()`: not one already answered)
2968
+ digest: heldSetDigest(
2969
+ all.paths.filter((r) => !purgingDeletes.has(r) && !confirmedDeletes.has(r))
2970
+ ),
2846
2971
  };
2847
2972
  } else {
2848
- out.deleteHeld = { direction, count: paths.length, paths: paths.slice(0, 200) };
2973
+ // Every row still waiting for the person's answer, not just this pass's hits (T13).
2974
+ const all = summarizeHeldIncoming(ledger.rows(), acceptedIncoming);
2975
+ out.deleteHeld = {
2976
+ direction,
2977
+ count: all.count,
2978
+ paths: all.paths.slice(0, 200),
2979
+ folders: all.folders.slice(0, 20),
2980
+ digest: heldSetDigest(all.paths),
2981
+ };
2849
2982
  }
2850
2983
  work = work.filter((w) => !hits.includes(w));
2851
2984
  }
@@ -3020,6 +3153,21 @@ export function createFilePlane(opts: FilePlaneOptions): FilePlane {
3020
3153
  while (inFlight.size > 0) await Promise.race(inFlight);
3021
3154
  };
3022
3155
 
3156
+ // 2.1.1 T13 — AN ACCEPTED INCOMING BATCH IS ONE PASS. Filing a file into `_trash/` is a local
3157
+ // rename: no request, no bytes, no hub to protect. The per-pass file ceiling exists for
3158
+ // downloads and uploads, and would stretch a person's "Remove here too" over a dozen passes.
3159
+ if (acceptedIncoming.size > 0) {
3160
+ const now1 = work.filter(
3161
+ (w) => w.decision.action === 'quarantine' && acceptedIncoming.has(w.rel)
3162
+ );
3163
+ if (now1.length > 0) {
3164
+ const gone = new Set(now1);
3165
+ work = work.filter((w) => !gone.has(w));
3166
+ for (const item of now1) await run(item);
3167
+ moved = 0;
3168
+ }
3169
+ }
3170
+
3023
3171
  for (const item of work) {
3024
3172
  // TWO CEILINGS, because they bound different things and only one of them
3025
3173
  // survives a bad day. `moved` bounds what LANDS — disk writes, parked
@@ -3324,8 +3472,16 @@ export function createFilePlane(opts: FilePlaneOptions): FilePlane {
3324
3472
  }
3325
3473
  ledger.setState(rel, 'pushing');
3326
3474
  const expect = decision.action === 'revive' ? null : remoteHash;
3327
- const res = await push(here, expect);
3475
+ // Over a tombstone this machine knows of, say which one (2.1.1 T9).
3476
+ const overTombstone = ledger.remoteTombstoned(rel);
3477
+ const res = await push(
3478
+ here,
3479
+ expect,
3480
+ overTombstone ? ledger.row(rel)?.remoteSeq : undefined
3481
+ );
3328
3482
  if (res.ok) {
3483
+ // The hub holds these bytes now; the tombstone this push decided over is history.
3484
+ if (overTombstone) ledger.noteRemote(rel, here.hash, res.seq ?? undefined);
3329
3485
  await ledger.adoptAfter(rel, here.hash, () => {}, {
3330
3486
  ...(res.seq !== null ? { remoteSeq: res.seq } : {}),
3331
3487
  size: here.size,
@@ -3355,6 +3511,18 @@ export function createFilePlane(opts: FilePlaneOptions): FilePlane {
3355
3511
  out.synced += 1;
3356
3512
  return true;
3357
3513
  }
3514
+ if (res.conflict && res.current === null && res.tombstoneSeq !== undefined) {
3515
+ // THE PATH IS TOMBSTONED, and this push did not decide over that tombstone (an owner
3516
+ // purge asks it be named — 2.1.1 T9). Remember it, so the next pass decides against the
3517
+ // delete (agreed → quarantine, held by the breaker; changed here → revive naming it)
3518
+ // instead of offering the same stale write again.
3519
+ ledger.noteRemote(rel, null, res.tombstoneSeq, true);
3520
+ ledger.setState(rel, 'conflict', {
3521
+ reason: 'the project deleted this file since this Mac last looked — deciding again',
3522
+ });
3523
+ out.conflicts.push({ rel, copy: null });
3524
+ return true;
3525
+ }
3358
3526
  if (res.conflict) {
3359
3527
  // The hub moved under us. Do NOT retry blindly — re-decide next
3360
3528
  // pass against what it actually holds now, which is exactly what a
@@ -3483,6 +3651,7 @@ export function createFilePlane(opts: FilePlaneOptions): FilePlane {
3483
3651
  return false;
3484
3652
  }
3485
3653
  ledger.forget(rel);
3654
+ acceptedIncoming.delete(rel);
3486
3655
  out.deleted.push({ rel, parked });
3487
3656
  return true;
3488
3657
  }
@@ -3524,6 +3693,13 @@ export function createFilePlane(opts: FilePlaneOptions): FilePlane {
3524
3693
  }
3525
3694
  }
3526
3695
 
3696
+ /** A row whose conflict copy still sits beside it and whose bytes are the ones the conflict settled on. */
3697
+ function keepsConflictCopy(kept: LedgerRow | null | undefined, hereHash: string): boolean {
3698
+ if (!kept || kept.state !== 'conflict' || !kept.conflictCopy) return false;
3699
+ if (kept.syncedHash !== hereHash) return false;
3700
+ return existsSync(path.join(designRoot, kept.conflictCopy));
3701
+ }
3702
+
3527
3703
  function restat(rel: string): void {
3528
3704
  try {
3529
3705
  const st = statSync(path.join(designRoot, rel));
@@ -3565,6 +3741,23 @@ export function createFilePlane(opts: FilePlaneOptions): FilePlane {
3565
3741
  dorucekaTotal() {
3566
3742
  return Object.keys(ledger.rows()).length;
3567
3743
  },
3744
+ conflictCopies() {
3745
+ // 2.1.1 T14 — the file → the copy of this Mac's version parked beside it, for every row still
3746
+ // in conflict. Bounded like the doručenka, and only copies that are still on disk.
3747
+ const out: Record<string, string> = {};
3748
+ for (const [rel, row] of Object.entries(ledger.rows()).sort((a, b) =>
3749
+ a[0].localeCompare(b[0])
3750
+ )) {
3751
+ if (Object.keys(out).length >= MAX_CONFLICT_COPIES) break;
3752
+ if (
3753
+ row.state === 'conflict' &&
3754
+ row.conflictCopy &&
3755
+ existsSync(path.join(designRoot, row.conflictCopy))
3756
+ )
3757
+ out[rel] = row.conflictCopy;
3758
+ }
3759
+ return out;
3760
+ },
3568
3761
  heldDeletes() {
3569
3762
  // What still waits for an answer: an answered row (being sent, or purged) is not asked again.
3570
3763
  const waiting: Record<string, LedgerRow> = {};
@@ -3586,6 +3779,20 @@ export function createFilePlane(opts: FilePlaneOptions): FilePlane {
3586
3779
  endPurge(rels) {
3587
3780
  for (const rel of rels) purgingDeletes.delete(rel);
3588
3781
  },
3782
+ heldIncomingDeletes() {
3783
+ return summarizeHeldIncoming(ledger.rows(), acceptedIncoming);
3784
+ },
3785
+ answerIncomingDeletes(how, folder, digest) {
3786
+ const all = summarizeHeldIncoming(ledger.rows(), acceptedIncoming).paths;
3787
+ // 2.1.1 T8 — the answer covers exactly the set the person saw, or nothing
3788
+ if (digest !== heldSetDigest(all)) return null;
3789
+ const rels = all.filter((r) => inHeldFolder(r, folder));
3790
+ for (const rel of rels) {
3791
+ if (how === 'remove') acceptedIncoming.add(rel);
3792
+ else ledger.setState(rel, 'kept-here', { reason: KEPT_HERE_REASON });
3793
+ }
3794
+ return rels;
3795
+ },
3589
3796
  /**
3590
3797
  * HOW MUCH MEDIA IS NOT HERE — plan T29.
3591
3798
  *
@@ -15,8 +15,12 @@
15
15
  //
16
16
  // Which path to TRY comes from this Mac's stored role and its own record of deletes sent; the hub
17
17
  // decides the rest (a member's door 429, an owner check on the purge). Nothing here is a hub claim.
18
+ //
19
+ // 2.1.1 N4 — both answers name the batch the person was shown (`heldSetDigest` of the waiting set,
20
+ // carried in the pass status): a set that changed since (more local deletes joined it) answers
21
+ // `changed` and nothing is sent — the incoming answer's T8 rule.
18
22
 
19
- import { inHeldFolder } from './file-plane.ts';
23
+ import { heldSetDigest, inHeldFolder } from './file-plane.ts';
20
24
 
21
25
  /** The hub door's delete window — apps/hub/src/journal.mjs `DELETE_BUDGET_PER_WINDOW`. */
22
26
  export const DOOR_DELETES_MEMBER = 25;
@@ -32,6 +36,12 @@ export interface HeldDeletePlane {
32
36
  heldDeletes(): { count: number; paths: string[]; folders: { folder: string; count: number }[] };
33
37
  answerHeldDeletes(how: 'send' | 'purging' | 'keep', folder: string | null): string[];
34
38
  endPurge(rels: readonly string[]): void;
39
+ /** 2.1.1 T13 — the held INCOMING deletes (the project removed these; this Mac still has them). */
40
+ answerIncomingDeletes(
41
+ how: 'remove' | 'keep',
42
+ folder: string | null,
43
+ digest: string
44
+ ): string[] | null;
35
45
  }
36
46
 
37
47
  export interface HeldDeletesDeps {
@@ -57,16 +67,32 @@ export interface HeldDeletesView {
57
67
  }
58
68
 
59
69
  export type HeldDeleteAnswer =
60
- | { ok: true; how: 'door' | 'purge' | 'keep'; count: number }
70
+ | { ok: true; how: 'door' | 'purge' | 'keep' | 'removed' | 'kept-here'; count: number }
61
71
  | {
62
72
  ok: false;
63
- reason: 'nothing-held' | 'not-owner' | 'busy' | 'not-linked';
73
+ reason: 'nothing-held' | 'not-owner' | 'busy' | 'not-linked' | 'changed';
64
74
  detail: string;
65
75
  };
66
76
 
67
77
  export interface HeldDeletesControl {
68
78
  view(): HeldDeletesView;
69
- answer(answer: 'delete' | 'keep', folder: string | null): Promise<HeldDeleteAnswer>;
79
+ /** 2.1.1 N4 — `digest` names the set the person was shown; a stale or missing one answers `changed`. */
80
+ answer(
81
+ answer: 'delete' | 'keep',
82
+ folder: string | null,
83
+ digest: string
84
+ ): Promise<HeldDeleteAnswer>;
85
+ /**
86
+ * 2.1.1 T13 — the answer to a batch the PROJECT deleted, held here. `remove`: filed into `_trash/` on
87
+ * the next pass, all at once (recoverable). `keep`: stays on this Mac, never sent back. Local to
88
+ * this Mac either way, so no role or hub decides it. 2.1.1 T8 — `digest` names the set the person
89
+ * was shown; a set that changed since answers `changed` and nothing is applied.
90
+ */
91
+ answerIncoming(
92
+ answer: 'remove' | 'keep',
93
+ folder: string | null,
94
+ digest: string
95
+ ): Promise<HeldDeleteAnswer>;
70
96
  /** Test seam: the running purge, if any. */
71
97
  idle(): Promise<void>;
72
98
  }
@@ -169,7 +195,7 @@ export function createHeldDeletes(deps: HeldDeletesDeps): HeldDeletesControl {
169
195
  purge: purge ? { ...purge } : null,
170
196
  };
171
197
  },
172
- async answer(answer, folder) {
198
+ async answer(answer, folder, digest) {
173
199
  const plane = deps.plane();
174
200
  if (!plane) {
175
201
  return {
@@ -178,6 +204,9 @@ export function createHeldDeletes(deps: HeldDeletesDeps): HeldDeletesControl {
178
204
  detail: 'This project isn’t syncing with a hub right now.',
179
205
  };
180
206
  }
207
+ // 2.1.1 N4 — the answer covers exactly the waiting set that was on screen, or nothing
208
+ if (digest !== heldSetDigest(plane.heldDeletes().paths))
209
+ return listChanged(LIST_CHANGED_OUT_DETAIL);
181
210
  if (answer === 'keep') {
182
211
  const kept = plane.answerHeldDeletes('keep', folder);
183
212
  if (kept.length === 0) return nothingHeld();
@@ -207,12 +236,52 @@ export function createHeldDeletes(deps: HeldDeletesDeps): HeldDeletesControl {
207
236
  });
208
237
  return { ok: true, how: 'purge', count: rels.length };
209
238
  },
239
+ async answerIncoming(answer, folder, digest) {
240
+ const plane = deps.plane();
241
+ if (!plane) {
242
+ return {
243
+ ok: false,
244
+ reason: 'not-linked',
245
+ detail: 'This project isn’t syncing with a hub right now.',
246
+ };
247
+ }
248
+ const rels = plane.answerIncomingDeletes(answer, folder, digest);
249
+ if (rels === null) return listChanged();
250
+ if (rels.length === 0) {
251
+ return {
252
+ ok: false,
253
+ reason: 'nothing-held',
254
+ detail: 'Nothing here is waiting to be removed.',
255
+ };
256
+ }
257
+ // The removal applies on the very next pass — start it now rather than at the next tick.
258
+ void Promise.resolve(deps.runPass()).catch(() => {});
259
+ return { ok: true, how: answer === 'remove' ? 'removed' : 'kept-here', count: rels.length };
260
+ },
210
261
  async idle() {
211
262
  await job;
212
263
  },
213
264
  };
214
265
  }
215
266
 
267
+ /** 2.1.1 T8 — the held set is not the one the person answered about. */
268
+ export const LIST_CHANGED_DETAIL = 'The list changed. Nothing was removed — look at it again.';
269
+ /** 2.1.1 N4 — the outbound twin: nothing was deleted on the hub. */
270
+ export const LIST_CHANGED_OUT_DETAIL = 'The list changed. Nothing was deleted — look at it again.';
271
+ function listChanged(detail = LIST_CHANGED_DETAIL): HeldDeleteAnswer {
272
+ return { ok: false, reason: 'changed', detail };
273
+ }
274
+
275
+ /** A folder name from the project for a sentence: no control or formatting characters (a bidi
276
+ * override would reorder the words around it), one line, capped — the folder list's own cleanup. */
277
+ export function cleanFolderName(raw: string): string {
278
+ const s = String(raw ?? '')
279
+ .replace(/[\p{Cc}\p{Cf}]/gu, '')
280
+ .replace(/\s+/g, ' ')
281
+ .trim();
282
+ return s.length > 120 ? `${s.slice(0, 120)}…` : s;
283
+ }
284
+
216
285
  function nothingHeld(): HeldDeleteAnswer {
217
286
  return { ok: false, reason: 'nothing-held', detail: 'Nothing here is waiting to be deleted.' };
218
287
  }
@@ -228,3 +297,15 @@ export function heldDeleteDetail(count: number, folders: { folder: string; count
228
297
  const files = count === 1 ? '1 file' : `${thousands(count)} files`;
229
298
  return `${files} deleted on this Mac${where} — more than sync deletes on the hub without asking. They are still on the hub. Delete them on the hub too, or keep them there.`;
230
299
  }
300
+
301
+ /** Sync details' sentence for the held INCOMING row (CONTRACT §4: what happened · what is safe · the verbs). */
302
+ export function heldIncomingDetail(count: number, folders: { folder: string; count: number }[]) {
303
+ const where =
304
+ folders.length === 1
305
+ ? ` in ${cleanFolderName(folders[0]?.folder ?? '') || 'the project folder'}`
306
+ : folders.length > 1
307
+ ? ` in ${thousands(folders.length)} folders`
308
+ : '';
309
+ const files = count === 1 ? '1 file' : `${thousands(count)} files`;
310
+ return `${files} deleted by the project${where} — more than sync removes here without asking. They are still on this Mac. Remove them here too and they go to the trash, or keep them here.`;
311
+ }
@@ -77,6 +77,11 @@ export async function linkHub(body: unknown): Promise<HubLinkResult> {
77
77
  }
78
78
  if (parsed.protocol !== 'http:' && parsed.protocol !== 'https:')
79
79
  return bad('The hub address must start with http:// or https://.');
80
+ // 2.1.1 T8 — the same grammar as every other door (home/hub.ts `hubAddress`): a credential is never
81
+ // stored for cleartext http to a remote host; http stays for a loopback dev server.
82
+ if (parsed.protocol === 'http:' && !isLoopbackHostname(parsed.hostname))
83
+ return bad('The hub address must start with https://.');
84
+ if (parsed.username || parsed.password) return bad("That doesn't look like a valid hub address.");
80
85
 
81
86
  const token = b.token.trim();
82
87
 
@@ -97,6 +102,9 @@ export async function linkHub(body: unknown): Promise<HubLinkResult> {
97
102
  };
98
103
  }
99
104
 
105
+ const isLoopbackHostname = (h: string) =>
106
+ h === 'localhost' || h === '[::1]' || /^127(?:\.\d{1,3}){3}$/.test(h);
107
+
100
108
  function bad(error: string): HubLinkResult {
101
109
  return { status: 400, json: { ok: false, error } };
102
110
  }