@estiva-app/interop 0.53.0 → 0.55.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.
@@ -863,7 +863,14 @@ const BARE_FILE_MANIFEST = {
863
863
  widget: 'card',
864
864
  slots: {
865
865
  title: { tag: 'title', fold: 'title' },
866
- body: { field: 'content' },
866
+ /*
867
+ The text is the `description` fold, seeded from `content` (SPEC §6.7,
868
+ Leaf 1): anyone in the Folder saves it with the `describe` action
869
+ below, the author included — a republished root would lose silently
870
+ to an older change. Ship's body slot is the same fold, minus the
871
+ `tag`: a `30840` carries its text in `content`, never in a tag.
872
+ */
873
+ body: { fold: 'description', field: 'content' },
867
874
  // A bare file may hold bare files — a sub-topic under a topic. Children
868
875
  // of *other* kinds are found the other way round, by their own `a`
869
876
  // (`parentRefOf` below), which is FOL-4's generic direction; this slot
@@ -898,8 +905,8 @@ const BARE_FILE_MANIFEST = {
898
905
  },
899
906
  },
900
907
  /*
901
- Four actions, and the manifest is the only place a consumer learns of any
902
- of them (FOL-33; `move` from FOL-4). A Peek page offering Rename and Delete on a bare file and
908
+ The manifest is the only place a consumer learns of any of these actions
909
+ (FOL-33; `move` from FOL-4, `describe` from Leaf 1). A Peek page offering Rename and Delete on a bare file and
903
910
  nothing on a Ship issue does so because *this* list says `rename` and
904
911
  `delete` and Ship's does not — never because the kind is 30840. The rule is
905
912
  the one FOL-31 set for `comment`: a control is drawn from a declaration.
@@ -934,6 +941,24 @@ const BARE_FILE_MANIFEST = {
934
941
  emits: { kind: 1851, field: 'title' },
935
942
  input: { type: 'string' },
936
943
  },
944
+ /*
945
+ `describe` replaces the file's text with a whole §13.3 block document —
946
+ a snapshot, last save wins (SPEC §6.7, decided 2026-10-13; live merging
947
+ between open windows is a later layer). The shape of Ship's
948
+ `describe-issue`, so a consumer that edits one edits both, and finds it
949
+ by `emits.field` rather than by this id. Every save is a full copy, so a
950
+ writer saves on leave, never per keystroke.
951
+ */
952
+ {
953
+ id: 'describe',
954
+ label: 'Edit',
955
+ description: "Replace this file's text with new text. A change event, so anyone in the Folder may, and every app " +
956
+ 'reading the file folds it over the text it was made with; the later save wins.',
957
+ effect: 'writes',
958
+ appliesTo: [String(KIND_BARE_FILE)],
959
+ emits: { kind: 1851, field: 'description' },
960
+ input: { type: 'string', format: BLOCK_DOCUMENT_FORMAT },
961
+ },
937
962
  /*
938
963
  `move` is the `parent` field SPEC §6.7 already defines, declared so a
939
964
  consumer draws the control from here as it draws Rename (FOL-4). The value
@@ -974,7 +999,13 @@ const BARE_FILE_MANIFEST = {
974
999
  'an agent that wrote it, may — and its comments stay as separate events.',
975
1000
  effect: 'destructive',
976
1001
  appliesTo: [String(KIND_BARE_FILE)],
977
- emits: { kind: KIND_DELETION },
1002
+ /*
1003
+ `listed`, as `create-file` on a Folder is (INT-12): a file put in a
1004
+ Folder's state by a declaration is taken out of it by one. Without it a
1005
+ consumer following declarations deleted the file and left its row in
1006
+ every Folder that listed it — FOL-48's failure.
1007
+ */
1008
+ emits: { kind: KIND_DELETION, listed: true },
978
1009
  },
979
1010
  ],
980
1011
  vocabularies: {
@@ -988,6 +1019,52 @@ const BARE_FILE_MANIFEST = {
988
1019
  function bareFileManifest() {
989
1020
  return { manifest: BARE_FILE_MANIFEST, address: BARE_FILE_MANIFEST_ADDRESS, viaRecommendation: false };
990
1021
  }
1022
+ /**
1023
+ * What a Folder's built-in actions are declared by — SPEC §3.2, as
1024
+ * {@link BARE_FILE_MANIFEST_ADDRESS} is for the bare file. The
1025
+ * `declaredBy.address` of an action no app declared.
1026
+ */
1027
+ export const FOLDER_ACTIONS_ADDRESS = 'spec:3.2';
1028
+ /**
1029
+ * A Folder's built-in actions, offered beside the ones apps declare on it
1030
+ * (INT-12).
1031
+ *
1032
+ * A Folder is owned by no app (SPEC §3.2), so what every app may do to one
1033
+ * cannot sit on any `kind:31990` — a manifest claiming `39000` is what §3.2
1034
+ * forbids, and a creation declared there reaches a consumer only through that
1035
+ * claim. Putting a new file in a Folder is the first: a bare file with the
1036
+ * Folder in `h` (§6.7 — never `buzz-channel`, so a private Folder's titles stay
1037
+ * its own), then the `kind:1852` `add` that lists it (§3.3), which `listed`
1038
+ * makes the runtime's job as it is for Ship's `add-project`.
1039
+ *
1040
+ * It travels the cross-app path ({@link borrowedFrom}) rather than the owner's,
1041
+ * so it carries `declaredBy` naming this section, and
1042
+ * {@link resolveActingManifest} hands it back by that address. The projection
1043
+ * is the bare file's own, so the created object is drawn the way every other
1044
+ * bare file is. Each consumer offers it only if it may sign `30840` and `1852`
1045
+ * (§3.3).
1046
+ */
1047
+ const FOLDER_CREATOR = {
1048
+ manifest: {
1049
+ name: 'Folder',
1050
+ about: 'What every app may do in a Folder, whichever app made it.',
1051
+ projections: { [String(KIND_BARE_FILE)]: BARE_FILE_MANIFEST.projections[String(KIND_BARE_FILE)] },
1052
+ actions: [
1053
+ {
1054
+ id: 'create-file',
1055
+ label: 'New file',
1056
+ description: 'Put a new file in this Folder: a title, and nothing else yet. Everyone who can read the Folder can read ' +
1057
+ 'it, and the Folder lists it straight away. The app opening files calls it by its own word — Peek says topic.',
1058
+ effect: 'writes',
1059
+ appliesTo: String(KIND_CHANNEL_METADATA),
1060
+ emits: { kind: KIND_BARE_FILE, listed: true },
1061
+ input: { type: 'object', properties: { title: { type: 'string' } }, required: ['title'] },
1062
+ },
1063
+ ],
1064
+ },
1065
+ address: FOLDER_ACTIONS_ADDRESS,
1066
+ viaRecommendation: false,
1067
+ };
991
1068
  /**
992
1069
  * The built-in manifest, named by the app that opens it — PRO-24.
993
1070
  *
@@ -1316,17 +1393,27 @@ function creatorsOrNone(kind, query, cache) {
1316
1393
  * creates here is the owner's to offer, so another manifest's claim to create
1317
1394
  * it — a staging copy of the same app, most likely — is dropped rather than
1318
1395
  * drawn as a second row.
1396
+ *
1397
+ * **A Folder's built-in creations come first and are never dropped**
1398
+ * ({@link FOLDER_CREATOR}, INT-12): no app owns a Folder (SPEC §3.2), so no
1399
+ * app's claim to create the same kind there outranks the specification's —
1400
+ * a manifest declaring a bare file on a Folder is the second row, not this.
1319
1401
  */
1320
1402
  function borrowedFrom(kind, owner, creators) {
1403
+ const builtIn = kind === KIND_CHANNEL_METADATA ? [FOLDER_CREATOR] : [];
1404
+ const builtInMakes = new Set(builtIn.flatMap((creator) => (creator.manifest.actions ?? []).map((a) => a.emits.kind)));
1321
1405
  const ownerMakes = new Set((owner.manifest.actions ?? [])
1322
1406
  .filter((action) => isCreationAction(action) && asArray(action.appliesTo).includes(String(kind)))
1323
1407
  .map((action) => action.emits.kind));
1324
- return creators.flatMap((creator) => {
1325
- if (creator.address === owner.address)
1326
- return [];
1327
- const actions = (creator.manifest.actions ?? []).filter((action) => !ownerMakes.has(action.emits.kind));
1328
- return actions.length ? [{ ...creator, manifest: { ...creator.manifest, actions } }] : [];
1329
- });
1408
+ return [
1409
+ ...builtIn,
1410
+ ...creators.flatMap((creator) => {
1411
+ if (creator.address === owner.address)
1412
+ return [];
1413
+ const actions = (creator.manifest.actions ?? []).filter((action) => !ownerMakes.has(action.emits.kind) && !builtInMakes.has(action.emits.kind));
1414
+ return actions.length ? [{ ...creator, manifest: { ...creator.manifest, actions } }] : [];
1415
+ }),
1416
+ ];
1330
1417
  }
1331
1418
  /** Every borrowed creation, resolved as the owner's actions are, with its declaring app named. */
1332
1419
  function borrowedActions(kind, owner, creators) {
@@ -1350,6 +1437,9 @@ export async function resolveActingManifest(pointer, declaredBy, query, cache) {
1350
1437
  const owner = await resolveManifest(pointer, query, cache);
1351
1438
  if (!owner || !declaredBy || declaredBy === owner.address)
1352
1439
  return owner;
1440
+ // A Folder's built-in needs no sweep, so a refused one must not cost it (INT-12).
1441
+ if (pointer.kind === KIND_CHANNEL_METADATA && declaredBy === FOLDER_ACTIONS_ADDRESS)
1442
+ return FOLDER_CREATOR;
1353
1443
  const creators = await creatorsOn(pointer.kind, query, cache);
1354
1444
  return borrowedFrom(pointer.kind, owner, creators).find((creator) => creator.address === declaredBy) ?? null;
1355
1445
  }
@@ -1428,9 +1518,65 @@ import { contentFormatOf } from '@estiva-app/protocol';
1428
1518
  * and a consumer reads `slots.body` to find it.
1429
1519
  */
1430
1520
  export const BODY_SLOT = 'body';
1431
- function foldChanges(changes, rule) {
1521
+ /**
1522
+ * Does this change sit in the Folder its target is filed in — SPEC §6.2.
1523
+ *
1524
+ * The relay checks a change only against its own `h`: the signer must be a
1525
+ * member of *that* Folder. Nothing on the relay ties it to the target's Folder,
1526
+ * so before this a member of any Folder could rename, move, archive or rewrite
1527
+ * a file in a Folder they cannot read, and every reader who could see the
1528
+ * change folded it in. Probed on production 2026-10-13: QA-2's `description`
1529
+ * change to QA-1's page, signed with another Folder's `h`, was accepted. So
1530
+ * the reader ignores it, for every kind (Miky, 2026-10-13).
1531
+ *
1532
+ * The target's Folder is any placement its root carries — `h`, or Ship's
1533
+ * `buzz-channel` (§7.3), whichever the root has; about half of Ship's projects
1534
+ * carry one and half the other — or, for a `kind:39000`, the Folder itself.
1535
+ * A change's own Folder is read the same way. Measured before this shipped:
1536
+ * of 2,790 changes whose roots were readable, none would be dropped but two
1537
+ * duplicated project changes and four Folder archives `folderArchiveOf`
1538
+ * already ignored.
1539
+ *
1540
+ * A change's Folder is its `h` alone: that is the tag the relay checks the
1541
+ * signer's membership against, so a `buzz-channel` on a change proves nothing.
1542
+ *
1543
+ * Without the root there is nothing to compare, so every change counts: the
1544
+ * caller that has only an address (an archived parent read by address) is no
1545
+ * worse than before. Nor does a root that names no Folder (an early topic,
1546
+ * from when `h` was SHOULD) — it has no Folder to be outside of.
1547
+ *
1548
+ * Built once per root, and asked once per change.
1549
+ */
1550
+ function targetFolders(root) {
1551
+ if (!root)
1552
+ return undefined;
1553
+ const folders = new Set(PLACEMENTS.flatMap((name) => root.tags.filter((t) => t[0] === name && t[1]).map((t) => t[1])));
1554
+ if (root.kind === KIND_CHANNEL_METADATA) {
1555
+ const own = tagValue(root, 'd');
1556
+ if (own)
1557
+ folders.add(own);
1558
+ }
1559
+ return folders.size ? folders : undefined;
1560
+ }
1561
+ function inTargetFolder(change, folders) {
1562
+ if (!folders)
1563
+ return true;
1564
+ // The first `h` with a value, as the relay reads it: an empty `["h"]` ahead
1565
+ // of a real one must not make this read a different Folder from the one the
1566
+ // relay checked the signer against.
1567
+ const own = change.tags.find((t) => t[0] === 'h' && t[1])?.[1];
1568
+ return own !== undefined && folders.has(own);
1569
+ }
1570
+ /**
1571
+ * Fold `changes` under `rule`, ignoring any not in `root`'s Folder
1572
+ * ({@link targetFolders}). `root` is required so every caller decides: pass
1573
+ * `undefined` only where the root genuinely is not at hand, and the newest
1574
+ * version where several are.
1575
+ */
1576
+ function foldChanges(changes, rule, root) {
1432
1577
  const fields = {};
1433
- for (const change of [...changes].sort(byOrder)) {
1578
+ const folders = targetFolders(root);
1579
+ for (const change of [...changes].filter((c) => inTargetFolder(c, folders)).sort(byOrder)) {
1434
1580
  const field = tagValue(change, rule.fieldTag);
1435
1581
  const value = tagValue(change, rule.valueTag);
1436
1582
  if (!field || value === undefined)
@@ -1453,11 +1599,11 @@ function foldChanges(changes, rule) {
1453
1599
  * the **first** target tag, as `archiveOf` reads it. Empty when the app declares
1454
1600
  * no change events — `foldRuleOf`'s substitute is never asked for.
1455
1601
  */
1456
- function foldOf(events, address, manifest) {
1602
+ function foldOf(events, address, manifest, root) {
1457
1603
  const records = foldRuleOf(manifest);
1458
1604
  return foldChanges(manifest.records
1459
1605
  ? events.filter((e) => e.kind === records.changeKind && tagValue(e, records.targetTag) === address)
1460
- : [], records);
1606
+ : [], records, root);
1461
1607
  }
1462
1608
  /**
1463
1609
  * How an `archived` change is read when the file's kind declares no rule of its
@@ -1505,19 +1651,28 @@ export function isArchived(file) {
1505
1651
  * the admin list passes none, and then no change counts: a Folder shown live
1506
1652
  * when it is archived is recoverable, one any member could hide is not.
1507
1653
  */
1508
- function archiveOf(changes, address, rule, folderAdmins) {
1509
- const [kind, , folderId] = address.split(':');
1654
+ function archiveOf(changes, address, rule,
1655
+ /**
1656
+ * The target's newest root version (SPEC §6.2): an archive signed in another
1657
+ * Folder is ignored, as every fold ignores it. Required so every caller
1658
+ * decides; `undefined` only where only the address is held.
1659
+ */
1660
+ root, folderAdmins) {
1661
+ const [kind, , identifier] = address.split(':');
1510
1662
  if (Number(kind) === KIND_CHANNEL) {
1511
1663
  if (!folderAdmins?.size)
1512
1664
  return undefined;
1513
- changes = changes.filter((change) => tagValue(change, 'h') === folderId && folderAdmins.has(change.pubkey));
1665
+ changes = changes.filter((change) => tagValue(change, 'h') === identifier && folderAdmins.has(change.pubkey));
1514
1666
  }
1667
+ const folders = targetFolders(root);
1515
1668
  let last;
1516
1669
  for (const change of changes) {
1517
1670
  // The target is the first target tag, as every other fold reads it: a
1518
1671
  // change archiving one file must not archive another it also mentions.
1519
1672
  if (change.kind !== rule.changeKind || tagValue(change, rule.targetTag) !== address)
1520
1673
  continue;
1674
+ if (!inTargetFolder(change, folders))
1675
+ continue;
1521
1676
  if (tagValue(change, rule.fieldTag) !== ARCHIVED_FIELD || tagValue(change, rule.valueTag) === undefined)
1522
1677
  continue;
1523
1678
  if (!last || byOrder(last, change) < 0)
@@ -2317,10 +2472,10 @@ manifestFor) {
2317
2472
  pointer,
2318
2473
  manifest,
2319
2474
  projection: manifest.projections[String(root.kind)],
2320
- folded: foldOf(byTarget(foldRuleOf(manifest).targetTag).get(address) ?? [], address, manifest),
2475
+ folded: foldOf(byTarget(foldRuleOf(manifest).targetTag).get(address) ?? [], address, manifest, root),
2321
2476
  viaRecommendation: resolved.viaRecommendation,
2322
2477
  webTemplate: resolved.webTemplate,
2323
- archived: archiveOf(byTarget(archiveRule.targetTag).get(address) ?? [], address, archiveRule),
2478
+ archived: archiveOf(byTarget(archiveRule.targetTag).get(address) ?? [], address, archiveRule, root),
2324
2479
  });
2325
2480
  });
2326
2481
  }
@@ -2463,7 +2618,7 @@ cache) {
2463
2618
  .at(-1);
2464
2619
  if (!root)
2465
2620
  continue;
2466
- const folded = foldOf(events, pointerToAddress(pointer), manifest);
2621
+ const folded = foldOf(events, pointerToAddress(pointer), manifest, root);
2467
2622
  results[reference] = parentRefOf(manifest, pointer.kind, root, folded) ?? null;
2468
2623
  }
2469
2624
  return results;
@@ -2493,7 +2648,8 @@ async function markArchivedParents(entries, query) {
2493
2648
  const parents = [...new Set(waiting.map(({ object }) => object.parentRef))];
2494
2649
  const changes = await readArchiveChanges(parents, query, rules);
2495
2650
  for (const [index, { object }] of waiting.entries()) {
2496
- const archive = archiveOf(changes, object.parentRef, rules[index]);
2651
+ // Only the parent's address is held here, not its root (SPEC §6.2's exception).
2652
+ const archive = archiveOf(changes, object.parentRef, rules[index], undefined);
2497
2653
  if (archive)
2498
2654
  object.archived = { ...archive, with: object.parentRef };
2499
2655
  }
@@ -2650,7 +2806,7 @@ function assembleObject(plan, answered, childChanges = []) {
2650
2806
  },
2651
2807
  };
2652
2808
  }
2653
- const folded = foldOf(events, address, manifest);
2809
+ const folded = foldOf(events, address, manifest, root);
2654
2810
  /*
2655
2811
  Matched on the comment filter's own criteria — the kind **and** the `a` tag.
2656
2812
 
@@ -2679,7 +2835,7 @@ function assembleObject(plan, answered, childChanges = []) {
2679
2835
  viaRecommendation,
2680
2836
  webTemplate: resolved.webTemplate,
2681
2837
  comments,
2682
- archived: archiveOf(events, address, archiveRuleOf(manifest)),
2838
+ archived: archiveOf(events, address, archiveRuleOf(manifest), root),
2683
2839
  });
2684
2840
  /*
2685
2841
  The `list` slot — PRO-7.
@@ -2957,14 +3113,14 @@ function childrenFrom(args) {
2957
3113
  parent, so only a folded value that names it keeps it: the tag is how
2958
3114
  it was found, not proof that it is still here.
2959
3115
  */
2960
- const folded = address ? foldOf(childChanges, address, manifest) : {};
3116
+ const folded = address ? foldOf(childChanges, address, manifest, event) : {};
2961
3117
  if (movedBy && folded[movedBy] && folded[movedBy].value !== childFilter.parent)
2962
3118
  return [];
2963
3119
  if (address && movedIn.has(address) && folded[movedBy]?.value !== childFilter.parent)
2964
3120
  return [];
2965
3121
  if (records.hiddenWhen && folded[records.hiddenWhen.field]?.value === records.hiddenWhen.equals)
2966
3122
  return [];
2967
- if (address && manifest.records && archiveOf(childChanges, address, records))
3123
+ if (address && manifest.records && archiveOf(childChanges, address, records, event))
2968
3124
  return [];
2969
3125
  return [
2970
3126
  buildChildObject({ root: event, manifest, projection: childProjection, records, webTemplate, viaRecommendation, folded }),
@@ -3471,7 +3627,7 @@ cache) {
3471
3627
  if (!rule?.hiddenWhen)
3472
3628
  return false;
3473
3629
  const applied = source.filter((e) => e.kind === rule.changeKind && tagValue(e, rule.targetTag) === addressOfEvent(event));
3474
- return foldChanges(applied, rule)[rule.hiddenWhen.field]?.value === rule.hiddenWhen.equals;
3630
+ return foldChanges(applied, rule, event)[rule.hiddenWhen.field]?.value === rule.hiddenWhen.equals;
3475
3631
  };
3476
3632
  const roots = known
3477
3633
  .filter((e) => containers.has(e.kind))
@@ -3537,7 +3693,7 @@ cache) {
3537
3693
  pointer,
3538
3694
  manifest,
3539
3695
  projection,
3540
- folded: foldChanges(changesFor(address), records),
3696
+ folded: foldChanges(changesFor(address), records, root),
3541
3697
  viaRecommendation,
3542
3698
  webTemplate: resolved.webTemplate,
3543
3699
  });
@@ -3597,7 +3753,7 @@ cache) {
3597
3753
  relays: [],
3598
3754
  };
3599
3755
  const ticketChanges = changesFor(pointerToAddress(ticketPointer));
3600
- const folded = foldChanges(ticketChanges, records);
3756
+ const folded = foldChanges(ticketChanges, records, event);
3601
3757
  const status = statusSpec ? resolveSlot(statusSpec, event, folded, manifest) : null;
3602
3758
  const raw = statusSpec ? rawSlotValue(statusSpec, event, folded, false)?.value : undefined;
3603
3759
  // The owning app's declaration first, the label guess only if it has none.
@@ -4140,7 +4296,8 @@ function folderAdminsOf(admins, channel) {
4140
4296
  * change, and it means nothing.
4141
4297
  */
4142
4298
  function folderArchiveOf(changes, folder) {
4143
- return folder.channel ? archiveOf(changes, folder.channel, ARCHIVE_RULE, new Set(folder.admins ?? [])) : undefined;
4299
+ // A Folder's own rule (h = the Folder, by its admins) stands in for the root check.
4300
+ return folder.channel ? archiveOf(changes, folder.channel, ARCHIVE_RULE, undefined, new Set(folder.admins ?? [])) : undefined;
4144
4301
  }
4145
4302
  /**
4146
4303
  * The folders a person navigates by: the ones with state that no other folder
@@ -4243,13 +4400,13 @@ export async function listFolders(query) {
4243
4400
  listedIn.set(identifier, containers);
4244
4401
  }
4245
4402
  }
4246
- const { recordsOf, archives } = await placeRecordChannels(events, byId, listedIn, query);
4403
+ const { recordsOf, archives, rootOf } = await placeRecordChannels(events, byId, listedIn, query);
4247
4404
  for (const [id, containers] of listedIn) {
4248
4405
  const folder = byId.get(id);
4249
4406
  if (folder)
4250
4407
  folder.listedIn = containers;
4251
4408
  }
4252
- markArchivedFolders(byId, recordsOf, archives);
4409
+ markArchivedFolders(byId, recordsOf, archives, rootOf);
4253
4410
  return [...byId.values()].sort((a, b) => (a.name ?? a.id).localeCompare(b.name ?? b.id));
4254
4411
  }
4255
4412
  /**
@@ -4267,7 +4424,9 @@ export async function listFolders(query) {
4267
4424
  * silently reported every Folder live would put archived Folders back in
4268
4425
  * everyone's sidebar.
4269
4426
  */
4270
- function markArchivedFolders(byId, recordsOf, changes) {
4427
+ function markArchivedFolders(byId, recordsOf, changes,
4428
+ /** Each record's newest root, so an archive signed in another Folder is ignored (SPEC §6.2). */
4429
+ rootOf) {
4271
4430
  for (const folder of byId.values()) {
4272
4431
  const own = folderArchiveOf(changes, folder);
4273
4432
  if (own) {
@@ -4275,7 +4434,7 @@ function markArchivedFolders(byId, recordsOf, changes) {
4275
4434
  continue;
4276
4435
  }
4277
4436
  const records = recordsOf.get(folder.id) ?? [];
4278
- const archives = records.map((record) => archiveOf(changes, record, ARCHIVE_RULE));
4437
+ const archives = records.map((record) => archiveOf(changes, record, ARCHIVE_RULE, rootOf.get(record)));
4279
4438
  if (records.length && archives.every(Boolean))
4280
4439
  folder.archived = { ...archives[0], with: records[0] };
4281
4440
  }
@@ -4399,14 +4558,18 @@ async function countLiveChildren(parents, counted, query, cache) {
4399
4558
  if (!match)
4400
4559
  continue;
4401
4560
  const child = pointerToAddress({ kind: event.kind, pubkey: event.pubkey, identifier, relays: [] });
4402
- if (!counted.has(child))
4403
- children.set(child, { kind: event.kind, rule: match.rule });
4561
+ if (counted.has(child))
4562
+ continue;
4563
+ // The newest version is the root an archive is checked against (SPEC §6.2).
4564
+ const held = children.get(child);
4565
+ if (!held || event.created_at > held.root.created_at)
4566
+ children.set(child, { kind: event.kind, rule: match.rule, root: event });
4404
4567
  }
4405
4568
  if (children.size === 0)
4406
4569
  return byKind;
4407
4570
  const changes = await readArchiveChanges([...children.keys()], query, [...children.values()].map((c) => c.rule));
4408
- for (const [child, { kind, rule }] of children) {
4409
- if (archiveOf(changes, child, rule))
4571
+ for (const [child, { kind, rule, root }] of children) {
4572
+ if (archiveOf(changes, child, rule, root))
4410
4573
  continue;
4411
4574
  byKind.set(kind, (byKind.get(kind) ?? 0) + 1);
4412
4575
  }
@@ -4492,7 +4655,7 @@ async function placeRecordChannels(events, byId, listedIn, query) {
4492
4655
  for (let start = 0; start < archiveAsk.length; start += MAX_FILTERS_PER_QUERY) {
4493
4656
  archives.push(...(await query(archiveAsk.slice(start, start + MAX_FILTERS_PER_QUERY))));
4494
4657
  }
4495
- return { recordsOf: new Map(), archives };
4658
+ return { recordsOf: new Map(), archives, rootOf: new Map() };
4496
4659
  }
4497
4660
  const groups = new Map();
4498
4661
  for (const { pointer } of containersOf.values()) {
@@ -4543,7 +4706,7 @@ async function placeRecordChannels(events, byId, listedIn, query) {
4543
4706
  listedIn.set(channel, placed);
4544
4707
  }
4545
4708
  }
4546
- return { recordsOf, archives };
4709
+ return { recordsOf, archives, rootOf: newest };
4547
4710
  }
4548
4711
  /**
4549
4712
  * Everything in one folder, resolved through the manifests of the apps that own it.
@@ -4718,11 +4881,11 @@ cache, options = {}) {
4718
4881
  if (!root)
4719
4882
  continue;
4720
4883
  const records = foldRuleOf(resolved.manifest);
4721
- const folded = foldChanges(events.filter((e) => e.kind === records.changeKind && hasTagValue(e, records.targetTag, address)), records);
4884
+ const folded = foldChanges(events.filter((e) => e.kind === records.changeKind && hasTagValue(e, records.targetTag, address)), records, root);
4722
4885
  const nestedAdmins = pointer.kind === KIND_CHANNEL
4723
4886
  ? new Set(folderAdminsOf(events.find((e) => e.kind === KIND_CHANNEL_ADMINS && tagValue(e, 'd') === pointer.identifier), root))
4724
4887
  : undefined;
4725
- const archive = archiveOf(events, address, archiveRuleOf(resolved.manifest), nestedAdmins);
4888
+ const archive = archiveOf(events, address, archiveRuleOf(resolved.manifest), root, nestedAdmins);
4726
4889
  archiveByAddress.set(address, archive);
4727
4890
  const parent = parentRefOf(resolved.manifest, pointer.kind, root, folded);
4728
4891
  parentByAddress.set(address, parent);
@@ -4797,8 +4960,9 @@ cache, options = {}) {
4797
4960
  if (outside.length) {
4798
4961
  const ruleFor = (parent) => parentRuleOf.get(parent) ?? ARCHIVE_RULE;
4799
4962
  const changes = await readArchiveChanges(outside, query, outside.map(ruleFor));
4963
+ // A parent in another Folder is held by address only (SPEC §6.2's exception).
4800
4964
  for (const parent of outside)
4801
- archiveByAddress.set(parent, archiveOf(changes, parent, ruleFor(parent)));
4965
+ archiveByAddress.set(parent, archiveOf(changes, parent, ruleFor(parent), undefined));
4802
4966
  }
4803
4967
  const files = [];
4804
4968
  for (const { file, archive } of listed) {