@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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@estiva-app/interop",
3
- "version": "0.53.0",
3
+ "version": "0.55.0",
4
4
  "description": "Render and act on another app's objects from its published NIP-89 manifest. The owner defines the projection; the consumer decides how it looks.",
5
5
  "license": "MIT",
6
6
  "type": "module",
package/src/index.ts CHANGED
@@ -49,6 +49,7 @@ export {
49
49
  KIND_BARE_FILE,
50
50
  MOVED_TO_TAG,
51
51
  BARE_FILE_MANIFEST_ADDRESS,
52
+ FOLDER_ACTIONS_ADDRESS,
52
53
  ASPECTS,
53
54
  type Aspect,
54
55
  commentKindsOf,
package/src/projection.ts CHANGED
@@ -1390,7 +1390,14 @@ const BARE_FILE_MANIFEST: Manifest = {
1390
1390
  widget: 'card',
1391
1391
  slots: {
1392
1392
  title: { tag: 'title', fold: 'title' },
1393
- body: { field: 'content' },
1393
+ /*
1394
+ The text is the `description` fold, seeded from `content` (SPEC §6.7,
1395
+ Leaf 1): anyone in the Folder saves it with the `describe` action
1396
+ below, the author included — a republished root would lose silently
1397
+ to an older change. Ship's body slot is the same fold, minus the
1398
+ `tag`: a `30840` carries its text in `content`, never in a tag.
1399
+ */
1400
+ body: { fold: 'description', field: 'content' },
1394
1401
  // A bare file may hold bare files — a sub-topic under a topic. Children
1395
1402
  // of *other* kinds are found the other way round, by their own `a`
1396
1403
  // (`parentRefOf` below), which is FOL-4's generic direction; this slot
@@ -1425,8 +1432,8 @@ const BARE_FILE_MANIFEST: Manifest = {
1425
1432
  },
1426
1433
  },
1427
1434
  /*
1428
- Four actions, and the manifest is the only place a consumer learns of any
1429
- of them (FOL-33; `move` from FOL-4). A Peek page offering Rename and Delete on a bare file and
1435
+ The manifest is the only place a consumer learns of any of these actions
1436
+ (FOL-33; `move` from FOL-4, `describe` from Leaf 1). A Peek page offering Rename and Delete on a bare file and
1430
1437
  nothing on a Ship issue does so because *this* list says `rename` and
1431
1438
  `delete` and Ship's does not — never because the kind is 30840. The rule is
1432
1439
  the one FOL-31 set for `comment`: a control is drawn from a declaration.
@@ -1463,6 +1470,25 @@ const BARE_FILE_MANIFEST: Manifest = {
1463
1470
  emits: { kind: 1851, field: 'title' },
1464
1471
  input: { type: 'string' },
1465
1472
  },
1473
+ /*
1474
+ `describe` replaces the file's text with a whole §13.3 block document —
1475
+ a snapshot, last save wins (SPEC §6.7, decided 2026-10-13; live merging
1476
+ between open windows is a later layer). The shape of Ship's
1477
+ `describe-issue`, so a consumer that edits one edits both, and finds it
1478
+ by `emits.field` rather than by this id. Every save is a full copy, so a
1479
+ writer saves on leave, never per keystroke.
1480
+ */
1481
+ {
1482
+ id: 'describe',
1483
+ label: 'Edit',
1484
+ description:
1485
+ "Replace this file's text with new text. A change event, so anyone in the Folder may, and every app " +
1486
+ 'reading the file folds it over the text it was made with; the later save wins.',
1487
+ effect: 'writes',
1488
+ appliesTo: [String(KIND_BARE_FILE)],
1489
+ emits: { kind: 1851, field: 'description' },
1490
+ input: { type: 'string', format: BLOCK_DOCUMENT_FORMAT },
1491
+ },
1466
1492
  /*
1467
1493
  `move` is the `parent` field SPEC §6.7 already defines, declared so a
1468
1494
  consumer draws the control from here as it draws Rename (FOL-4). The value
@@ -1506,7 +1532,13 @@ const BARE_FILE_MANIFEST: Manifest = {
1506
1532
  'an agent that wrote it, may — and its comments stay as separate events.',
1507
1533
  effect: 'destructive',
1508
1534
  appliesTo: [String(KIND_BARE_FILE)],
1509
- emits: { kind: KIND_DELETION },
1535
+ /*
1536
+ `listed`, as `create-file` on a Folder is (INT-12): a file put in a
1537
+ Folder's state by a declaration is taken out of it by one. Without it a
1538
+ consumer following declarations deleted the file and left its row in
1539
+ every Folder that listed it — FOL-48's failure.
1540
+ */
1541
+ emits: { kind: KIND_DELETION, listed: true },
1510
1542
  },
1511
1543
  ],
1512
1544
  vocabularies: {
@@ -1522,6 +1554,55 @@ function bareFileManifest(): ResolvedManifest {
1522
1554
  return { manifest: BARE_FILE_MANIFEST, address: BARE_FILE_MANIFEST_ADDRESS, viaRecommendation: false }
1523
1555
  }
1524
1556
 
1557
+ /**
1558
+ * What a Folder's built-in actions are declared by — SPEC §3.2, as
1559
+ * {@link BARE_FILE_MANIFEST_ADDRESS} is for the bare file. The
1560
+ * `declaredBy.address` of an action no app declared.
1561
+ */
1562
+ export const FOLDER_ACTIONS_ADDRESS = 'spec:3.2'
1563
+
1564
+ /**
1565
+ * A Folder's built-in actions, offered beside the ones apps declare on it
1566
+ * (INT-12).
1567
+ *
1568
+ * A Folder is owned by no app (SPEC §3.2), so what every app may do to one
1569
+ * cannot sit on any `kind:31990` — a manifest claiming `39000` is what §3.2
1570
+ * forbids, and a creation declared there reaches a consumer only through that
1571
+ * claim. Putting a new file in a Folder is the first: a bare file with the
1572
+ * Folder in `h` (§6.7 — never `buzz-channel`, so a private Folder's titles stay
1573
+ * its own), then the `kind:1852` `add` that lists it (§3.3), which `listed`
1574
+ * makes the runtime's job as it is for Ship's `add-project`.
1575
+ *
1576
+ * It travels the cross-app path ({@link borrowedFrom}) rather than the owner's,
1577
+ * so it carries `declaredBy` naming this section, and
1578
+ * {@link resolveActingManifest} hands it back by that address. The projection
1579
+ * is the bare file's own, so the created object is drawn the way every other
1580
+ * bare file is. Each consumer offers it only if it may sign `30840` and `1852`
1581
+ * (§3.3).
1582
+ */
1583
+ const FOLDER_CREATOR: ResolvedManifest = {
1584
+ manifest: {
1585
+ name: 'Folder',
1586
+ about: 'What every app may do in a Folder, whichever app made it.',
1587
+ projections: { [String(KIND_BARE_FILE)]: BARE_FILE_MANIFEST.projections![String(KIND_BARE_FILE)] },
1588
+ actions: [
1589
+ {
1590
+ id: 'create-file',
1591
+ label: 'New file',
1592
+ description:
1593
+ 'Put a new file in this Folder: a title, and nothing else yet. Everyone who can read the Folder can read ' +
1594
+ 'it, and the Folder lists it straight away. The app opening files calls it by its own word — Peek says topic.',
1595
+ effect: 'writes',
1596
+ appliesTo: String(KIND_CHANNEL_METADATA),
1597
+ emits: { kind: KIND_BARE_FILE, listed: true },
1598
+ input: { type: 'object', properties: { title: { type: 'string' } }, required: ['title'] },
1599
+ },
1600
+ ],
1601
+ },
1602
+ address: FOLDER_ACTIONS_ADDRESS,
1603
+ viaRecommendation: false,
1604
+ }
1605
+
1525
1606
  /**
1526
1607
  * The built-in manifest, named by the app that opens it — PRO-24.
1527
1608
  *
@@ -1917,18 +1998,30 @@ function creatorsOrNone(kind: number, query: QueryFn, cache?: ProjectionCache):
1917
1998
  * creates here is the owner's to offer, so another manifest's claim to create
1918
1999
  * it — a staging copy of the same app, most likely — is dropped rather than
1919
2000
  * drawn as a second row.
2001
+ *
2002
+ * **A Folder's built-in creations come first and are never dropped**
2003
+ * ({@link FOLDER_CREATOR}, INT-12): no app owns a Folder (SPEC §3.2), so no
2004
+ * app's claim to create the same kind there outranks the specification's —
2005
+ * a manifest declaring a bare file on a Folder is the second row, not this.
1920
2006
  */
1921
2007
  function borrowedFrom(kind: number, owner: ResolvedManifest, creators: readonly ResolvedManifest[]): ResolvedManifest[] {
2008
+ const builtIn = kind === KIND_CHANNEL_METADATA ? [FOLDER_CREATOR] : []
2009
+ const builtInMakes = new Set(builtIn.flatMap((creator) => (creator.manifest.actions ?? []).map((a) => a.emits.kind)))
1922
2010
  const ownerMakes = new Set(
1923
2011
  (owner.manifest.actions ?? [])
1924
2012
  .filter((action) => isCreationAction(action) && asArray(action.appliesTo).includes(String(kind)))
1925
2013
  .map((action) => action.emits.kind),
1926
2014
  )
1927
- return creators.flatMap((creator) => {
1928
- if (creator.address === owner.address) return []
1929
- const actions = (creator.manifest.actions ?? []).filter((action) => !ownerMakes.has(action.emits.kind))
1930
- return actions.length ? [{ ...creator, manifest: { ...creator.manifest, actions } }] : []
1931
- })
2015
+ return [
2016
+ ...builtIn,
2017
+ ...creators.flatMap((creator) => {
2018
+ if (creator.address === owner.address) return []
2019
+ const actions = (creator.manifest.actions ?? []).filter(
2020
+ (action) => !ownerMakes.has(action.emits.kind) && !builtInMakes.has(action.emits.kind),
2021
+ )
2022
+ return actions.length ? [{ ...creator, manifest: { ...creator.manifest, actions } }] : []
2023
+ }),
2024
+ ]
1932
2025
  }
1933
2026
 
1934
2027
  /** Every borrowed creation, resolved as the owner's actions are, with its declaring app named. */
@@ -1960,6 +2053,8 @@ export async function resolveActingManifest(
1960
2053
  ): Promise<ResolvedManifest | null> {
1961
2054
  const owner = await resolveManifest(pointer, query, cache)
1962
2055
  if (!owner || !declaredBy || declaredBy === owner.address) return owner
2056
+ // A Folder's built-in needs no sweep, so a refused one must not cost it (INT-12).
2057
+ if (pointer.kind === KIND_CHANNEL_METADATA && declaredBy === FOLDER_ACTIONS_ADDRESS) return FOLDER_CREATOR
1963
2058
  const creators = await creatorsOn(pointer.kind, query, cache)
1964
2059
  return borrowedFrom(pointer.kind, owner, creators).find((creator) => creator.address === declaredBy) ?? null
1965
2060
  }
@@ -2050,9 +2145,64 @@ import { contentFormatOf, type ContentFormat } from '@estiva-app/protocol'
2050
2145
  export const BODY_SLOT = 'body'
2051
2146
 
2052
2147
 
2053
- function foldChanges(changes: SignedEvent[], rule: RecordsRule) {
2148
+ /**
2149
+ * Does this change sit in the Folder its target is filed in — SPEC §6.2.
2150
+ *
2151
+ * The relay checks a change only against its own `h`: the signer must be a
2152
+ * member of *that* Folder. Nothing on the relay ties it to the target's Folder,
2153
+ * so before this a member of any Folder could rename, move, archive or rewrite
2154
+ * a file in a Folder they cannot read, and every reader who could see the
2155
+ * change folded it in. Probed on production 2026-10-13: QA-2's `description`
2156
+ * change to QA-1's page, signed with another Folder's `h`, was accepted. So
2157
+ * the reader ignores it, for every kind (Miky, 2026-10-13).
2158
+ *
2159
+ * The target's Folder is any placement its root carries — `h`, or Ship's
2160
+ * `buzz-channel` (§7.3), whichever the root has; about half of Ship's projects
2161
+ * carry one and half the other — or, for a `kind:39000`, the Folder itself.
2162
+ * A change's own Folder is read the same way. Measured before this shipped:
2163
+ * of 2,790 changes whose roots were readable, none would be dropped but two
2164
+ * duplicated project changes and four Folder archives `folderArchiveOf`
2165
+ * already ignored.
2166
+ *
2167
+ * A change's Folder is its `h` alone: that is the tag the relay checks the
2168
+ * signer's membership against, so a `buzz-channel` on a change proves nothing.
2169
+ *
2170
+ * Without the root there is nothing to compare, so every change counts: the
2171
+ * caller that has only an address (an archived parent read by address) is no
2172
+ * worse than before. Nor does a root that names no Folder (an early topic,
2173
+ * from when `h` was SHOULD) — it has no Folder to be outside of.
2174
+ *
2175
+ * Built once per root, and asked once per change.
2176
+ */
2177
+ function targetFolders(root: SignedEvent | undefined): ReadonlySet<string> | undefined {
2178
+ if (!root) return undefined
2179
+ const folders = new Set(PLACEMENTS.flatMap((name) => root.tags.filter((t) => t[0] === name && t[1]).map((t) => t[1]!)))
2180
+ if (root.kind === KIND_CHANNEL_METADATA) {
2181
+ const own = tagValue(root, 'd')
2182
+ if (own) folders.add(own)
2183
+ }
2184
+ return folders.size ? folders : undefined
2185
+ }
2186
+
2187
+ function inTargetFolder(change: SignedEvent, folders: ReadonlySet<string> | undefined): boolean {
2188
+ if (!folders) return true
2189
+ // The first `h` with a value, as the relay reads it: an empty `["h"]` ahead
2190
+ // of a real one must not make this read a different Folder from the one the
2191
+ // relay checked the signer against.
2192
+ const own = change.tags.find((t) => t[0] === 'h' && t[1])?.[1]
2193
+ return own !== undefined && folders.has(own)
2194
+ }
2195
+
2196
+ /**
2197
+ * Fold `changes` under `rule`, ignoring any not in `root`'s Folder
2198
+ * ({@link targetFolders}). `root` is required so every caller decides: pass
2199
+ * `undefined` only where the root genuinely is not at hand, and the newest
2200
+ * version where several are.
2201
+ */
2202
+ function foldChanges(changes: SignedEvent[], rule: RecordsRule, root: SignedEvent | undefined) {
2054
2203
  const fields: Record<string, { value: string; by: string; at: number; format: ContentFormat }> = {}
2055
- for (const change of [...changes].sort(byOrder)) {
2204
+ const folders = targetFolders(root)
2205
+ for (const change of [...changes].filter((c) => inTargetFolder(c, folders)).sort(byOrder)) {
2056
2206
  const field = tagValue(change, rule.fieldTag)
2057
2207
  const value = tagValue(change, rule.valueTag)
2058
2208
  if (!field || value === undefined) continue // a partial change sets nothing
@@ -2075,13 +2225,14 @@ function foldChanges(changes: SignedEvent[], rule: RecordsRule) {
2075
2225
  * the **first** target tag, as `archiveOf` reads it. Empty when the app declares
2076
2226
  * no change events — `foldRuleOf`'s substitute is never asked for.
2077
2227
  */
2078
- function foldOf(events: SignedEvent[], address: string, manifest: Manifest) {
2228
+ function foldOf(events: SignedEvent[], address: string, manifest: Manifest, root: SignedEvent | undefined) {
2079
2229
  const records = foldRuleOf(manifest)
2080
2230
  return foldChanges(
2081
2231
  manifest.records
2082
2232
  ? events.filter((e) => e.kind === records.changeKind && tagValue(e, records.targetTag) === address)
2083
2233
  : [],
2084
2234
  records,
2235
+ root,
2085
2236
  )
2086
2237
  }
2087
2238
 
@@ -2163,18 +2314,26 @@ function archiveOf(
2163
2314
  changes: SignedEvent[],
2164
2315
  address: string,
2165
2316
  rule: RecordsRule,
2317
+ /**
2318
+ * The target's newest root version (SPEC §6.2): an archive signed in another
2319
+ * Folder is ignored, as every fold ignores it. Required so every caller
2320
+ * decides; `undefined` only where only the address is held.
2321
+ */
2322
+ root: SignedEvent | undefined,
2166
2323
  folderAdmins?: ReadonlySet<string>,
2167
2324
  ): Archive | undefined {
2168
- const [kind, , folderId] = address.split(':')
2325
+ const [kind, , identifier] = address.split(':')
2169
2326
  if (Number(kind) === KIND_CHANNEL) {
2170
2327
  if (!folderAdmins?.size) return undefined
2171
- changes = changes.filter((change) => tagValue(change, 'h') === folderId && folderAdmins.has(change.pubkey))
2328
+ changes = changes.filter((change) => tagValue(change, 'h') === identifier && folderAdmins.has(change.pubkey))
2172
2329
  }
2330
+ const folders = targetFolders(root)
2173
2331
  let last: SignedEvent | undefined
2174
2332
  for (const change of changes) {
2175
2333
  // The target is the first target tag, as every other fold reads it: a
2176
2334
  // change archiving one file must not archive another it also mentions.
2177
2335
  if (change.kind !== rule.changeKind || tagValue(change, rule.targetTag) !== address) continue
2336
+ if (!inTargetFolder(change, folders)) continue
2178
2337
  if (tagValue(change, rule.fieldTag) !== ARCHIVED_FIELD || tagValue(change, rule.valueTag) === undefined) continue
2179
2338
  if (!last || byOrder(last, change) < 0) last = change
2180
2339
  }
@@ -3310,10 +3469,10 @@ export function projectEvents(
3310
3469
  pointer,
3311
3470
  manifest,
3312
3471
  projection: manifest.projections![String(root.kind)],
3313
- folded: foldOf(byTarget(foldRuleOf(manifest).targetTag).get(address) ?? [], address, manifest),
3472
+ folded: foldOf(byTarget(foldRuleOf(manifest).targetTag).get(address) ?? [], address, manifest, root),
3314
3473
  viaRecommendation: resolved.viaRecommendation,
3315
3474
  webTemplate: resolved.webTemplate,
3316
- archived: archiveOf(byTarget(archiveRule.targetTag).get(address) ?? [], address, archiveRule),
3475
+ archived: archiveOf(byTarget(archiveRule.targetTag).get(address) ?? [], address, archiveRule, root),
3317
3476
  })
3318
3477
  })
3319
3478
  }
@@ -3463,7 +3622,7 @@ export async function resolveParents(
3463
3622
  .sort(byOrder)
3464
3623
  .at(-1)
3465
3624
  if (!root) continue
3466
- const folded = foldOf(events, pointerToAddress(pointer), manifest)
3625
+ const folded = foldOf(events, pointerToAddress(pointer), manifest, root)
3467
3626
  results[reference] = parentRefOf(manifest, pointer.kind, root, folded) ?? null
3468
3627
  }
3469
3628
  return results
@@ -3510,7 +3669,8 @@ async function markArchivedParents(
3510
3669
  const parents = [...new Set(waiting.map(({ object }) => object.parentRef!))]
3511
3670
  const changes = await readArchiveChanges(parents, query, rules)
3512
3671
  for (const [index, { object }] of waiting.entries()) {
3513
- const archive = archiveOf(changes, object.parentRef!, rules[index]!)
3672
+ // Only the parent's address is held here, not its root (SPEC §6.2's exception).
3673
+ const archive = archiveOf(changes, object.parentRef!, rules[index]!, undefined)
3514
3674
  if (archive) object.archived = { ...archive, with: object.parentRef! }
3515
3675
  }
3516
3676
  }
@@ -3719,7 +3879,7 @@ function assembleObject(plan: ObjectPlan, answered: SignedEvent[], childChanges:
3719
3879
  }
3720
3880
  }
3721
3881
 
3722
- const folded = foldOf(events, address, manifest)
3882
+ const folded = foldOf(events, address, manifest, root)
3723
3883
 
3724
3884
  /*
3725
3885
  Matched on the comment filter's own criteria — the kind **and** the `a` tag.
@@ -3750,7 +3910,7 @@ function assembleObject(plan: ObjectPlan, answered: SignedEvent[], childChanges:
3750
3910
  viaRecommendation,
3751
3911
  webTemplate: resolved.webTemplate,
3752
3912
  comments,
3753
- archived: archiveOf(events, address, archiveRuleOf(manifest)),
3913
+ archived: archiveOf(events, address, archiveRuleOf(manifest), root),
3754
3914
  })
3755
3915
  /*
3756
3916
  The `list` slot — PRO-7.
@@ -4061,11 +4221,11 @@ function childrenFrom(args: {
4061
4221
  parent, so only a folded value that names it keeps it: the tag is how
4062
4222
  it was found, not proof that it is still here.
4063
4223
  */
4064
- const folded = address ? foldOf(childChanges, address, manifest) : {}
4224
+ const folded = address ? foldOf(childChanges, address, manifest, event) : {}
4065
4225
  if (movedBy && folded[movedBy] && folded[movedBy].value !== childFilter.parent) return []
4066
4226
  if (address && movedIn.has(address) && folded[movedBy!]?.value !== childFilter.parent) return []
4067
4227
  if (records.hiddenWhen && folded[records.hiddenWhen.field]?.value === records.hiddenWhen.equals) return []
4068
- if (address && manifest.records && archiveOf(childChanges, address, records)) return []
4228
+ if (address && manifest.records && archiveOf(childChanges, address, records, event)) return []
4069
4229
  return [
4070
4230
  buildChildObject({ root: event, manifest, projection: childProjection, records, webTemplate, viaRecommendation, folded }),
4071
4231
  ]
@@ -4653,7 +4813,7 @@ export async function resolveFolderProject(
4653
4813
  const applied = source.filter(
4654
4814
  (e) => e.kind === rule.changeKind && tagValue(e, rule.targetTag) === addressOfEvent(event),
4655
4815
  )
4656
- return foldChanges(applied, rule)[rule.hiddenWhen.field]?.value === rule.hiddenWhen.equals
4816
+ return foldChanges(applied, rule, event)[rule.hiddenWhen.field]?.value === rule.hiddenWhen.equals
4657
4817
  }
4658
4818
 
4659
4819
  const roots = known
@@ -4730,7 +4890,7 @@ export async function resolveFolderProject(
4730
4890
  pointer,
4731
4891
  manifest,
4732
4892
  projection,
4733
- folded: foldChanges(changesFor(address), records),
4893
+ folded: foldChanges(changesFor(address), records, root),
4734
4894
  viaRecommendation,
4735
4895
  webTemplate: resolved.webTemplate,
4736
4896
  })
@@ -4792,7 +4952,7 @@ export async function resolveFolderProject(
4792
4952
  relays: [],
4793
4953
  }
4794
4954
  const ticketChanges = changesFor(pointerToAddress(ticketPointer))
4795
- const folded = foldChanges(ticketChanges, records)
4955
+ const folded = foldChanges(ticketChanges, records, event)
4796
4956
  const status = statusSpec ? resolveSlot(statusSpec, event, folded, manifest) : null
4797
4957
  const raw = statusSpec ? rawSlotValue(statusSpec, event, folded, false)?.value : undefined
4798
4958
 
@@ -5438,7 +5598,8 @@ function folderAdminsOf(admins: SignedEvent | undefined, channel: SignedEvent |
5438
5598
  * change, and it means nothing.
5439
5599
  */
5440
5600
  function folderArchiveOf(changes: SignedEvent[], folder: Pick<FolderSummary, 'id' | 'channel' | 'admins'>): Archive | undefined {
5441
- return folder.channel ? archiveOf(changes, folder.channel, ARCHIVE_RULE, new Set(folder.admins ?? [])) : undefined
5601
+ // A Folder's own rule (h = the Folder, by its admins) stands in for the root check.
5602
+ return folder.channel ? archiveOf(changes, folder.channel, ARCHIVE_RULE, undefined, new Set(folder.admins ?? [])) : undefined
5442
5603
  }
5443
5604
 
5444
5605
  /** One folder, enough to draw a sidebar row. */
@@ -5633,12 +5794,12 @@ export async function listFolders(query: QueryFn): Promise<FolderSummary[]> {
5633
5794
  listedIn.set(identifier, containers)
5634
5795
  }
5635
5796
  }
5636
- const { recordsOf, archives } = await placeRecordChannels(events, byId, listedIn, query)
5797
+ const { recordsOf, archives, rootOf } = await placeRecordChannels(events, byId, listedIn, query)
5637
5798
  for (const [id, containers] of listedIn) {
5638
5799
  const folder = byId.get(id)
5639
5800
  if (folder) folder.listedIn = containers
5640
5801
  }
5641
- markArchivedFolders(byId, recordsOf, archives)
5802
+ markArchivedFolders(byId, recordsOf, archives, rootOf)
5642
5803
  return [...byId.values()].sort((a, b) => (a.name ?? a.id).localeCompare(b.name ?? b.id))
5643
5804
  }
5644
5805
 
@@ -5661,6 +5822,8 @@ function markArchivedFolders(
5661
5822
  byId: Map<string, FolderSummary>,
5662
5823
  recordsOf: Map<string, string[]>,
5663
5824
  changes: SignedEvent[],
5825
+ /** Each record's newest root, so an archive signed in another Folder is ignored (SPEC §6.2). */
5826
+ rootOf: ReadonlyMap<string, SignedEvent>,
5664
5827
  ): void {
5665
5828
  for (const folder of byId.values()) {
5666
5829
  const own = folderArchiveOf(changes, folder)
@@ -5669,7 +5832,7 @@ function markArchivedFolders(
5669
5832
  continue
5670
5833
  }
5671
5834
  const records = recordsOf.get(folder.id) ?? []
5672
- const archives = records.map((record) => archiveOf(changes, record, ARCHIVE_RULE))
5835
+ const archives = records.map((record) => archiveOf(changes, record, ARCHIVE_RULE, rootOf.get(record)))
5673
5836
  if (records.length && archives.every(Boolean)) folder.archived = { ...archives[0]!, with: records[0] }
5674
5837
  }
5675
5838
  }
@@ -5811,19 +5974,22 @@ async function countLiveChildren(
5811
5974
  for (let start = 0; start < filters.length; start += MAX_FILTERS_PER_QUERY) {
5812
5975
  events.push(...(await query(filters.slice(start, start + MAX_FILTERS_PER_QUERY))))
5813
5976
  }
5814
- const children = new Map<string, { kind: number; rule: RecordsRule }>()
5977
+ const children = new Map<string, { kind: number; rule: RecordsRule; root: SignedEvent }>()
5815
5978
  for (const event of events) {
5816
5979
  const identifier = tagValue(event, 'd')
5817
5980
  if (identifier === undefined) continue
5818
5981
  const match = wanted.find((w) => w.kind === event.kind && hasTagValue(event, w.via, w.parent))
5819
5982
  if (!match) continue
5820
5983
  const child = pointerToAddress({ kind: event.kind, pubkey: event.pubkey, identifier, relays: [] })
5821
- if (!counted.has(child)) children.set(child, { kind: event.kind, rule: match.rule })
5984
+ if (counted.has(child)) continue
5985
+ // The newest version is the root an archive is checked against (SPEC §6.2).
5986
+ const held = children.get(child)
5987
+ if (!held || event.created_at > held.root.created_at) children.set(child, { kind: event.kind, rule: match.rule, root: event })
5822
5988
  }
5823
5989
  if (children.size === 0) return byKind
5824
5990
  const changes = await readArchiveChanges([...children.keys()], query, [...children.values()].map((c) => c.rule))
5825
- for (const [child, { kind, rule }] of children) {
5826
- if (archiveOf(changes, child, rule)) continue
5991
+ for (const [child, { kind, rule, root }] of children) {
5992
+ if (archiveOf(changes, child, rule, root)) continue
5827
5993
  byKind.set(kind, (byKind.get(kind) ?? 0) + 1)
5828
5994
  }
5829
5995
  return byKind
@@ -5877,7 +6043,7 @@ async function placeRecordChannels(
5877
6043
  byId: Map<string, FolderSummary>,
5878
6044
  listedIn: Map<string, string[]>,
5879
6045
  query: QueryFn,
5880
- ): Promise<{ recordsOf: Map<string, string[]>; archives: SignedEvent[] }> {
6046
+ ): Promise<{ recordsOf: Map<string, string[]>; archives: SignedEvent[]; rootOf: Map<string, SignedEvent> }> {
5881
6047
  const containersOf = new Map<string, { pointer: AddressPointer; containers: string[] }>()
5882
6048
  for (const event of events) {
5883
6049
  if (event.kind !== KIND_FOLDER_STATE) continue
@@ -5908,7 +6074,7 @@ async function placeRecordChannels(
5908
6074
  for (let start = 0; start < archiveAsk.length; start += MAX_FILTERS_PER_QUERY) {
5909
6075
  archives.push(...(await query(archiveAsk.slice(start, start + MAX_FILTERS_PER_QUERY))))
5910
6076
  }
5911
- return { recordsOf: new Map(), archives }
6077
+ return { recordsOf: new Map(), archives, rootOf: new Map() }
5912
6078
  }
5913
6079
 
5914
6080
  const groups = new Map<string, { kind: number; pubkey: string; identifiers: string[] }>()
@@ -5956,7 +6122,7 @@ async function placeRecordChannels(
5956
6122
  listedIn.set(channel, placed)
5957
6123
  }
5958
6124
  }
5959
- return { recordsOf, archives }
6125
+ return { recordsOf, archives, rootOf: newest }
5960
6126
  }
5961
6127
 
5962
6128
  /**
@@ -6154,6 +6320,7 @@ export async function resolveFolderContents(
6154
6320
  const folded = foldChanges(
6155
6321
  events.filter((e) => e.kind === records.changeKind && hasTagValue(e, records.targetTag, address)),
6156
6322
  records,
6323
+ root,
6157
6324
  )
6158
6325
  const nestedAdmins =
6159
6326
  pointer.kind === KIND_CHANNEL
@@ -6164,7 +6331,7 @@ export async function resolveFolderContents(
6164
6331
  ),
6165
6332
  )
6166
6333
  : undefined
6167
- const archive = archiveOf(events, address, archiveRuleOf(resolved.manifest), nestedAdmins)
6334
+ const archive = archiveOf(events, address, archiveRuleOf(resolved.manifest), root, nestedAdmins)
6168
6335
  archiveByAddress.set(address, archive)
6169
6336
  const parent = parentRefOf(resolved.manifest, pointer.kind, root, folded)
6170
6337
  parentByAddress.set(address, parent)
@@ -6240,7 +6407,8 @@ export async function resolveFolderContents(
6240
6407
  if (outside.length) {
6241
6408
  const ruleFor = (parent: string) => parentRuleOf.get(parent) ?? ARCHIVE_RULE
6242
6409
  const changes = await readArchiveChanges(outside, query, outside.map(ruleFor))
6243
- for (const parent of outside) archiveByAddress.set(parent, archiveOf(changes, parent, ruleFor(parent)))
6410
+ // A parent in another Folder is held by address only (SPEC §6.2's exception).
6411
+ for (const parent of outside) archiveByAddress.set(parent, archiveOf(changes, parent, ruleFor(parent), undefined))
6244
6412
  }
6245
6413
  const files: ForeignObject[] = []
6246
6414
  for (const { file, archive } of listed) {