@estiva-app/interop 0.54.0 → 0.56.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.54.0",
3
+ "version": "0.56.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/folders.ts CHANGED
@@ -119,7 +119,7 @@ export function planRenameFolder(
119
119
  * Delete a Folder: `[kind:9008]`.
120
120
  *
121
121
  * **For an empty Folder.** The relay refuses while the Folder holds a file —
122
- * a `30840`, `30850` or `30851` under its `h`, or any address its listing
122
+ * a `30840`, `30842`, `30850` or `30851` under its `h`, or any address its listing
123
123
  * names — with a reason naming the count ("folder holds N file(s) … remove
124
124
  * them first"), and a consumer shows that reason. A file moved out keeps its
125
125
  * `h`, so a Folder whose listing looks empty can still refuse.
package/src/projection.ts CHANGED
@@ -685,6 +685,15 @@ interface SlotSpec {
685
685
  * value is an address.
686
686
  */
687
687
  movedBy?: string
688
+ /**
689
+ * The child names its own parent with its `via` tag, **of any kind** — the
690
+ * bare file's rule (SPEC §6.7 Nesting), declared rather than hard-coded so
691
+ * an owned kind can have it: a document sits under a topic, a project or
692
+ * another document (Miky, 2026-10-14). With it, `movedBy` names the field
693
+ * without limiting the parent to the declaring kind, and a consumer reads
694
+ * the parent off the child instead of matching the declaring kind's prefix.
695
+ */
696
+ anyParent?: boolean
688
697
  }
689
698
  }
690
699
 
@@ -1390,7 +1399,14 @@ const BARE_FILE_MANIFEST: Manifest = {
1390
1399
  widget: 'card',
1391
1400
  slots: {
1392
1401
  title: { tag: 'title', fold: 'title' },
1393
- body: { field: 'content' },
1402
+ /*
1403
+ The text is the `description` fold, seeded from `content` (SPEC §6.7,
1404
+ Leaf 1): anyone in the Folder saves it with the `describe` action
1405
+ below, the author included — a republished root would lose silently
1406
+ to an older change. Ship's body slot is the same fold, minus the
1407
+ `tag`: a `30840` carries its text in `content`, never in a tag.
1408
+ */
1409
+ body: { fold: 'description', field: 'content' },
1394
1410
  // A bare file may hold bare files — a sub-topic under a topic. Children
1395
1411
  // of *other* kinds are found the other way round, by their own `a`
1396
1412
  // (`parentRefOf` below), which is FOL-4's generic direction; this slot
@@ -1399,7 +1415,7 @@ const BARE_FILE_MANIFEST: Manifest = {
1399
1415
  // `movedBy` is SPEC §6.7's `parent` field, declared rather than
1400
1416
  // hard-coded since MAN-1, so a bare file moves by the rule every other
1401
1417
  // kind does — the move action below sets the same field.
1402
- list: { children: { kind: KIND_BARE_FILE, via: 'a', limit: 200, movedBy: 'parent' } },
1418
+ list: { children: { kind: KIND_BARE_FILE, via: 'a', limit: 200, movedBy: 'parent', anyParent: true } },
1403
1419
  },
1404
1420
  },
1405
1421
  /*
@@ -1425,8 +1441,8 @@ const BARE_FILE_MANIFEST: Manifest = {
1425
1441
  },
1426
1442
  },
1427
1443
  /*
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
1444
+ The manifest is the only place a consumer learns of any of these actions
1445
+ (FOL-33; `move` from FOL-4, `describe` from Leaf 1). A Peek page offering Rename and Delete on a bare file and
1430
1446
  nothing on a Ship issue does so because *this* list says `rename` and
1431
1447
  `delete` and Ship's does not — never because the kind is 30840. The rule is
1432
1448
  the one FOL-31 set for `comment`: a control is drawn from a declaration.
@@ -1463,6 +1479,25 @@ const BARE_FILE_MANIFEST: Manifest = {
1463
1479
  emits: { kind: 1851, field: 'title' },
1464
1480
  input: { type: 'string' },
1465
1481
  },
1482
+ /*
1483
+ `describe` replaces the file's text with a whole §13.3 block document —
1484
+ a snapshot, last save wins (SPEC §6.7, decided 2026-10-13; live merging
1485
+ between open windows is a later layer). The shape of Ship's
1486
+ `describe-issue`, so a consumer that edits one edits both, and finds it
1487
+ by `emits.field` rather than by this id. Every save is a full copy, so a
1488
+ writer saves on leave, never per keystroke.
1489
+ */
1490
+ {
1491
+ id: 'describe',
1492
+ label: 'Edit',
1493
+ description:
1494
+ "Replace this file's text with new text. A change event, so anyone in the Folder may, and every app " +
1495
+ 'reading the file folds it over the text it was made with; the later save wins.',
1496
+ effect: 'writes',
1497
+ appliesTo: [String(KIND_BARE_FILE)],
1498
+ emits: { kind: 1851, field: 'description' },
1499
+ input: { type: 'string', format: BLOCK_DOCUMENT_FORMAT },
1500
+ },
1466
1501
  /*
1467
1502
  `move` is the `parent` field SPEC §6.7 already defines, declared so a
1468
1503
  consumer draws the control from here as it draws Rename (FOL-4). The value
@@ -2119,9 +2154,64 @@ import { contentFormatOf, type ContentFormat } from '@estiva-app/protocol'
2119
2154
  export const BODY_SLOT = 'body'
2120
2155
 
2121
2156
 
2122
- function foldChanges(changes: SignedEvent[], rule: RecordsRule) {
2157
+ /**
2158
+ * Does this change sit in the Folder its target is filed in — SPEC §6.2.
2159
+ *
2160
+ * The relay checks a change only against its own `h`: the signer must be a
2161
+ * member of *that* Folder. Nothing on the relay ties it to the target's Folder,
2162
+ * so before this a member of any Folder could rename, move, archive or rewrite
2163
+ * a file in a Folder they cannot read, and every reader who could see the
2164
+ * change folded it in. Probed on production 2026-10-13: QA-2's `description`
2165
+ * change to QA-1's page, signed with another Folder's `h`, was accepted. So
2166
+ * the reader ignores it, for every kind (Miky, 2026-10-13).
2167
+ *
2168
+ * The target's Folder is any placement its root carries — `h`, or Ship's
2169
+ * `buzz-channel` (§7.3), whichever the root has; about half of Ship's projects
2170
+ * carry one and half the other — or, for a `kind:39000`, the Folder itself.
2171
+ * A change's own Folder is read the same way. Measured before this shipped:
2172
+ * of 2,790 changes whose roots were readable, none would be dropped but two
2173
+ * duplicated project changes and four Folder archives `folderArchiveOf`
2174
+ * already ignored.
2175
+ *
2176
+ * A change's Folder is its `h` alone: that is the tag the relay checks the
2177
+ * signer's membership against, so a `buzz-channel` on a change proves nothing.
2178
+ *
2179
+ * Without the root there is nothing to compare, so every change counts: the
2180
+ * caller that has only an address (an archived parent read by address) is no
2181
+ * worse than before. Nor does a root that names no Folder (an early topic,
2182
+ * from when `h` was SHOULD) — it has no Folder to be outside of.
2183
+ *
2184
+ * Built once per root, and asked once per change.
2185
+ */
2186
+ function targetFolders(root: SignedEvent | undefined): ReadonlySet<string> | undefined {
2187
+ if (!root) return undefined
2188
+ const folders = new Set(PLACEMENTS.flatMap((name) => root.tags.filter((t) => t[0] === name && t[1]).map((t) => t[1]!)))
2189
+ if (root.kind === KIND_CHANNEL_METADATA) {
2190
+ const own = tagValue(root, 'd')
2191
+ if (own) folders.add(own)
2192
+ }
2193
+ return folders.size ? folders : undefined
2194
+ }
2195
+
2196
+ function inTargetFolder(change: SignedEvent, folders: ReadonlySet<string> | undefined): boolean {
2197
+ if (!folders) return true
2198
+ // The first `h` with a value, as the relay reads it: an empty `["h"]` ahead
2199
+ // of a real one must not make this read a different Folder from the one the
2200
+ // relay checked the signer against.
2201
+ const own = change.tags.find((t) => t[0] === 'h' && t[1])?.[1]
2202
+ return own !== undefined && folders.has(own)
2203
+ }
2204
+
2205
+ /**
2206
+ * Fold `changes` under `rule`, ignoring any not in `root`'s Folder
2207
+ * ({@link targetFolders}). `root` is required so every caller decides: pass
2208
+ * `undefined` only where the root genuinely is not at hand, and the newest
2209
+ * version where several are.
2210
+ */
2211
+ function foldChanges(changes: SignedEvent[], rule: RecordsRule, root: SignedEvent | undefined) {
2123
2212
  const fields: Record<string, { value: string; by: string; at: number; format: ContentFormat }> = {}
2124
- for (const change of [...changes].sort(byOrder)) {
2213
+ const folders = targetFolders(root)
2214
+ for (const change of [...changes].filter((c) => inTargetFolder(c, folders)).sort(byOrder)) {
2125
2215
  const field = tagValue(change, rule.fieldTag)
2126
2216
  const value = tagValue(change, rule.valueTag)
2127
2217
  if (!field || value === undefined) continue // a partial change sets nothing
@@ -2144,13 +2234,14 @@ function foldChanges(changes: SignedEvent[], rule: RecordsRule) {
2144
2234
  * the **first** target tag, as `archiveOf` reads it. Empty when the app declares
2145
2235
  * no change events — `foldRuleOf`'s substitute is never asked for.
2146
2236
  */
2147
- function foldOf(events: SignedEvent[], address: string, manifest: Manifest) {
2237
+ function foldOf(events: SignedEvent[], address: string, manifest: Manifest, root: SignedEvent | undefined) {
2148
2238
  const records = foldRuleOf(manifest)
2149
2239
  return foldChanges(
2150
2240
  manifest.records
2151
2241
  ? events.filter((e) => e.kind === records.changeKind && tagValue(e, records.targetTag) === address)
2152
2242
  : [],
2153
2243
  records,
2244
+ root,
2154
2245
  )
2155
2246
  }
2156
2247
 
@@ -2232,18 +2323,26 @@ function archiveOf(
2232
2323
  changes: SignedEvent[],
2233
2324
  address: string,
2234
2325
  rule: RecordsRule,
2326
+ /**
2327
+ * The target's newest root version (SPEC §6.2): an archive signed in another
2328
+ * Folder is ignored, as every fold ignores it. Required so every caller
2329
+ * decides; `undefined` only where only the address is held.
2330
+ */
2331
+ root: SignedEvent | undefined,
2235
2332
  folderAdmins?: ReadonlySet<string>,
2236
2333
  ): Archive | undefined {
2237
- const [kind, , folderId] = address.split(':')
2334
+ const [kind, , identifier] = address.split(':')
2238
2335
  if (Number(kind) === KIND_CHANNEL) {
2239
2336
  if (!folderAdmins?.size) return undefined
2240
- changes = changes.filter((change) => tagValue(change, 'h') === folderId && folderAdmins.has(change.pubkey))
2337
+ changes = changes.filter((change) => tagValue(change, 'h') === identifier && folderAdmins.has(change.pubkey))
2241
2338
  }
2339
+ const folders = targetFolders(root)
2242
2340
  let last: SignedEvent | undefined
2243
2341
  for (const change of changes) {
2244
2342
  // The target is the first target tag, as every other fold reads it: a
2245
2343
  // change archiving one file must not archive another it also mentions.
2246
2344
  if (change.kind !== rule.changeKind || tagValue(change, rule.targetTag) !== address) continue
2345
+ if (!inTargetFolder(change, folders)) continue
2247
2346
  if (tagValue(change, rule.fieldTag) !== ARCHIVED_FIELD || tagValue(change, rule.valueTag) === undefined) continue
2248
2347
  if (!last || byOrder(last, change) < 0) last = change
2249
2348
  }
@@ -2850,7 +2949,8 @@ export interface ForeignObject {
2850
2949
  * The kind a move may name — the kind whose projection declares the
2851
2950
  * `movedBy` (MAN-1): `30850` for a Ship issue, so a move control offers
2852
2951
  * projects and not other issues. Absent with `parentField` on a bare file,
2853
- * which may sit under any kind (§6.7). {@link nestingOf}'s `moveTargetsOf`
2952
+ * or a kind declared with `anyParent` (a document), which may sit under any
2953
+ * kind (§6.7). {@link nestingOf}'s `moveTargetsOf`
2854
2954
  * reads it.
2855
2955
  */
2856
2956
  parentKind?: number
@@ -3379,10 +3479,10 @@ export function projectEvents(
3379
3479
  pointer,
3380
3480
  manifest,
3381
3481
  projection: manifest.projections![String(root.kind)],
3382
- folded: foldOf(byTarget(foldRuleOf(manifest).targetTag).get(address) ?? [], address, manifest),
3482
+ folded: foldOf(byTarget(foldRuleOf(manifest).targetTag).get(address) ?? [], address, manifest, root),
3383
3483
  viaRecommendation: resolved.viaRecommendation,
3384
3484
  webTemplate: resolved.webTemplate,
3385
- archived: archiveOf(byTarget(archiveRule.targetTag).get(address) ?? [], address, archiveRule),
3485
+ archived: archiveOf(byTarget(archiveRule.targetTag).get(address) ?? [], address, archiveRule, root),
3386
3486
  })
3387
3487
  })
3388
3488
  }
@@ -3532,7 +3632,7 @@ export async function resolveParents(
3532
3632
  .sort(byOrder)
3533
3633
  .at(-1)
3534
3634
  if (!root) continue
3535
- const folded = foldOf(events, pointerToAddress(pointer), manifest)
3635
+ const folded = foldOf(events, pointerToAddress(pointer), manifest, root)
3536
3636
  results[reference] = parentRefOf(manifest, pointer.kind, root, folded) ?? null
3537
3637
  }
3538
3638
  return results
@@ -3579,7 +3679,8 @@ async function markArchivedParents(
3579
3679
  const parents = [...new Set(waiting.map(({ object }) => object.parentRef!))]
3580
3680
  const changes = await readArchiveChanges(parents, query, rules)
3581
3681
  for (const [index, { object }] of waiting.entries()) {
3582
- const archive = archiveOf(changes, object.parentRef!, rules[index]!)
3682
+ // Only the parent's address is held here, not its root (SPEC §6.2's exception).
3683
+ const archive = archiveOf(changes, object.parentRef!, rules[index]!, undefined)
3583
3684
  if (archive) object.archived = { ...archive, with: object.parentRef! }
3584
3685
  }
3585
3686
  }
@@ -3788,7 +3889,7 @@ function assembleObject(plan: ObjectPlan, answered: SignedEvent[], childChanges:
3788
3889
  }
3789
3890
  }
3790
3891
 
3791
- const folded = foldOf(events, address, manifest)
3892
+ const folded = foldOf(events, address, manifest, root)
3792
3893
 
3793
3894
  /*
3794
3895
  Matched on the comment filter's own criteria — the kind **and** the `a` tag.
@@ -3819,7 +3920,7 @@ function assembleObject(plan: ObjectPlan, answered: SignedEvent[], childChanges:
3819
3920
  viaRecommendation,
3820
3921
  webTemplate: resolved.webTemplate,
3821
3922
  comments,
3822
- archived: archiveOf(events, address, archiveRuleOf(manifest)),
3923
+ archived: archiveOf(events, address, archiveRuleOf(manifest), root),
3823
3924
  })
3824
3925
  /*
3825
3926
  The `list` slot — PRO-7.
@@ -3892,7 +3993,7 @@ function withPeople({ object, children }: AssembledObject, people: People): Fore
3892
3993
  */
3893
3994
  function declaredChildSpec(projection: {
3894
3995
  slots: Record<string, SlotSpec | SlotSpec[]>
3895
- }): { kind: number; via: string; limit?: number; match?: string; movedBy?: string } | undefined {
3996
+ }): { kind: number; via: string; limit?: number; match?: string; movedBy?: string; anyParent?: boolean } | undefined {
3896
3997
  const spec = projection.slots.list
3897
3998
  return !Array.isArray(spec) ? spec?.children : undefined
3898
3999
  }
@@ -3944,8 +4045,9 @@ function parentRefOf(
3944
4045
  return prefix.test(moved.value) ? moved.value : undefined
3945
4046
  }
3946
4047
  }
3947
- if (kind === KIND_BARE_FILE) {
3948
- return root.tags.find((t) => t[0] === 'a' && t[1] && /^\d+:[0-9a-f]{64}:/.test(t[1]))?.[1]
4048
+ const own = ownParentTagOf(manifest, kind)
4049
+ if (own) {
4050
+ return root.tags.find((t) => t[0] === own && t[1] && /^\d+:[0-9a-f]{64}:/.test(t[1]))?.[1]
3949
4051
  }
3950
4052
  for (const [parentKind, projection] of Object.entries(manifest.projections ?? {})) {
3951
4053
  const children = declaredChildSpec(projection as { slots: Record<string, SlotSpec | SlotSpec[]> })
@@ -3970,8 +4072,9 @@ function parentRefOf(
3970
4072
  * parent it names — SPEC §7.2's `movedBy`, read off whichever projection lists
3971
4073
  * `kind` as its children (MAN-1).
3972
4074
  *
3973
- * `parentKind` is absent for a bare file: §6.7 lets one sit under a file of any
3974
- * kind, so its own list naming bare files is not a limit on where it may go.
4075
+ * `parentKind` is absent for a bare file and for any kind a list declares with
4076
+ * `anyParent` (a document): each may sit under a file of any kind (§6.7), so
4077
+ * the list naming it is not a limit on where it may go.
3975
4078
  * Absent too for `match: 'identifier'`, where `movedBy` is ignored because a
3976
4079
  * move's value is an address and that list compares identifiers.
3977
4080
  */
@@ -3979,7 +4082,27 @@ function movedByOf(manifest: Manifest, kind: number): { field: string; parentKin
3979
4082
  for (const [parentKind, projection] of Object.entries(manifest.projections ?? {})) {
3980
4083
  const children = declaredChildSpec(projection as { slots: Record<string, SlotSpec | SlotSpec[]> })
3981
4084
  if (!children || children.kind !== kind || !children.movedBy || children.match === 'identifier') continue
3982
- return kind === KIND_BARE_FILE ? { field: children.movedBy } : { field: children.movedBy, parentKind: Number(parentKind) }
4085
+ // Any list declaring `anyParent` for the kind lifts the limit, so this and
4086
+ // `ownParentTagOf` never disagree about where the kind may sit.
4087
+ return ownParentTagOf(manifest, kind) !== undefined
4088
+ ? { field: children.movedBy }
4089
+ : { field: children.movedBy, parentKind: Number(parentKind) }
4090
+ }
4091
+ return undefined
4092
+ }
4093
+
4094
+ /**
4095
+ * The tag a `kind` object names its own parent with, of any kind — `a` on a
4096
+ * bare file (SPEC §6.7), and the `via` of any list declaring the kind with
4097
+ * `anyParent` (a document's, 2026-10-14). Undefined for every other kind,
4098
+ * whose parent is found from the parent's declared list instead.
4099
+ */
4100
+ function ownParentTagOf(manifest: Manifest, kind: number): string | undefined {
4101
+ if (kind === KIND_BARE_FILE) return 'a'
4102
+ for (const projection of Object.values(manifest.projections ?? {})) {
4103
+ const children = declaredChildSpec(projection as { slots: Record<string, SlotSpec | SlotSpec[]> })
4104
+ // Strictly `true`: nothing validates a published manifest, and "false" is truthy.
4105
+ if (children?.kind === kind && children.anyParent === true && children.match !== 'identifier') return children.via
3983
4106
  }
3984
4107
  return undefined
3985
4108
  }
@@ -4130,11 +4253,11 @@ function childrenFrom(args: {
4130
4253
  parent, so only a folded value that names it keeps it: the tag is how
4131
4254
  it was found, not proof that it is still here.
4132
4255
  */
4133
- const folded = address ? foldOf(childChanges, address, manifest) : {}
4256
+ const folded = address ? foldOf(childChanges, address, manifest, event) : {}
4134
4257
  if (movedBy && folded[movedBy] && folded[movedBy].value !== childFilter.parent) return []
4135
4258
  if (address && movedIn.has(address) && folded[movedBy!]?.value !== childFilter.parent) return []
4136
4259
  if (records.hiddenWhen && folded[records.hiddenWhen.field]?.value === records.hiddenWhen.equals) return []
4137
- if (address && manifest.records && archiveOf(childChanges, address, records)) return []
4260
+ if (address && manifest.records && archiveOf(childChanges, address, records, event)) return []
4138
4261
  return [
4139
4262
  buildChildObject({ root: event, manifest, projection: childProjection, records, webTemplate, viaRecommendation, folded }),
4140
4263
  ]
@@ -4722,7 +4845,7 @@ export async function resolveFolderProject(
4722
4845
  const applied = source.filter(
4723
4846
  (e) => e.kind === rule.changeKind && tagValue(e, rule.targetTag) === addressOfEvent(event),
4724
4847
  )
4725
- return foldChanges(applied, rule)[rule.hiddenWhen.field]?.value === rule.hiddenWhen.equals
4848
+ return foldChanges(applied, rule, event)[rule.hiddenWhen.field]?.value === rule.hiddenWhen.equals
4726
4849
  }
4727
4850
 
4728
4851
  const roots = known
@@ -4799,7 +4922,7 @@ export async function resolveFolderProject(
4799
4922
  pointer,
4800
4923
  manifest,
4801
4924
  projection,
4802
- folded: foldChanges(changesFor(address), records),
4925
+ folded: foldChanges(changesFor(address), records, root),
4803
4926
  viaRecommendation,
4804
4927
  webTemplate: resolved.webTemplate,
4805
4928
  })
@@ -4861,7 +4984,7 @@ export async function resolveFolderProject(
4861
4984
  relays: [],
4862
4985
  }
4863
4986
  const ticketChanges = changesFor(pointerToAddress(ticketPointer))
4864
- const folded = foldChanges(ticketChanges, records)
4987
+ const folded = foldChanges(ticketChanges, records, event)
4865
4988
  const status = statusSpec ? resolveSlot(statusSpec, event, folded, manifest) : null
4866
4989
  const raw = statusSpec ? rawSlotValue(statusSpec, event, folded, false)?.value : undefined
4867
4990
 
@@ -5507,7 +5630,8 @@ function folderAdminsOf(admins: SignedEvent | undefined, channel: SignedEvent |
5507
5630
  * change, and it means nothing.
5508
5631
  */
5509
5632
  function folderArchiveOf(changes: SignedEvent[], folder: Pick<FolderSummary, 'id' | 'channel' | 'admins'>): Archive | undefined {
5510
- return folder.channel ? archiveOf(changes, folder.channel, ARCHIVE_RULE, new Set(folder.admins ?? [])) : undefined
5633
+ // A Folder's own rule (h = the Folder, by its admins) stands in for the root check.
5634
+ return folder.channel ? archiveOf(changes, folder.channel, ARCHIVE_RULE, undefined, new Set(folder.admins ?? [])) : undefined
5511
5635
  }
5512
5636
 
5513
5637
  /** One folder, enough to draw a sidebar row. */
@@ -5702,12 +5826,12 @@ export async function listFolders(query: QueryFn): Promise<FolderSummary[]> {
5702
5826
  listedIn.set(identifier, containers)
5703
5827
  }
5704
5828
  }
5705
- const { recordsOf, archives } = await placeRecordChannels(events, byId, listedIn, query)
5829
+ const { recordsOf, archives, rootOf } = await placeRecordChannels(events, byId, listedIn, query)
5706
5830
  for (const [id, containers] of listedIn) {
5707
5831
  const folder = byId.get(id)
5708
5832
  if (folder) folder.listedIn = containers
5709
5833
  }
5710
- markArchivedFolders(byId, recordsOf, archives)
5834
+ markArchivedFolders(byId, recordsOf, archives, rootOf)
5711
5835
  return [...byId.values()].sort((a, b) => (a.name ?? a.id).localeCompare(b.name ?? b.id))
5712
5836
  }
5713
5837
 
@@ -5730,6 +5854,8 @@ function markArchivedFolders(
5730
5854
  byId: Map<string, FolderSummary>,
5731
5855
  recordsOf: Map<string, string[]>,
5732
5856
  changes: SignedEvent[],
5857
+ /** Each record's newest root, so an archive signed in another Folder is ignored (SPEC §6.2). */
5858
+ rootOf: ReadonlyMap<string, SignedEvent>,
5733
5859
  ): void {
5734
5860
  for (const folder of byId.values()) {
5735
5861
  const own = folderArchiveOf(changes, folder)
@@ -5738,7 +5864,7 @@ function markArchivedFolders(
5738
5864
  continue
5739
5865
  }
5740
5866
  const records = recordsOf.get(folder.id) ?? []
5741
- const archives = records.map((record) => archiveOf(changes, record, ARCHIVE_RULE))
5867
+ const archives = records.map((record) => archiveOf(changes, record, ARCHIVE_RULE, rootOf.get(record)))
5742
5868
  if (records.length && archives.every(Boolean)) folder.archived = { ...archives[0]!, with: records[0] }
5743
5869
  }
5744
5870
  }
@@ -5880,19 +6006,22 @@ async function countLiveChildren(
5880
6006
  for (let start = 0; start < filters.length; start += MAX_FILTERS_PER_QUERY) {
5881
6007
  events.push(...(await query(filters.slice(start, start + MAX_FILTERS_PER_QUERY))))
5882
6008
  }
5883
- const children = new Map<string, { kind: number; rule: RecordsRule }>()
6009
+ const children = new Map<string, { kind: number; rule: RecordsRule; root: SignedEvent }>()
5884
6010
  for (const event of events) {
5885
6011
  const identifier = tagValue(event, 'd')
5886
6012
  if (identifier === undefined) continue
5887
6013
  const match = wanted.find((w) => w.kind === event.kind && hasTagValue(event, w.via, w.parent))
5888
6014
  if (!match) continue
5889
6015
  const child = pointerToAddress({ kind: event.kind, pubkey: event.pubkey, identifier, relays: [] })
5890
- if (!counted.has(child)) children.set(child, { kind: event.kind, rule: match.rule })
6016
+ if (counted.has(child)) continue
6017
+ // The newest version is the root an archive is checked against (SPEC §6.2).
6018
+ const held = children.get(child)
6019
+ if (!held || event.created_at > held.root.created_at) children.set(child, { kind: event.kind, rule: match.rule, root: event })
5891
6020
  }
5892
6021
  if (children.size === 0) return byKind
5893
6022
  const changes = await readArchiveChanges([...children.keys()], query, [...children.values()].map((c) => c.rule))
5894
- for (const [child, { kind, rule }] of children) {
5895
- if (archiveOf(changes, child, rule)) continue
6023
+ for (const [child, { kind, rule, root }] of children) {
6024
+ if (archiveOf(changes, child, rule, root)) continue
5896
6025
  byKind.set(kind, (byKind.get(kind) ?? 0) + 1)
5897
6026
  }
5898
6027
  return byKind
@@ -5946,7 +6075,7 @@ async function placeRecordChannels(
5946
6075
  byId: Map<string, FolderSummary>,
5947
6076
  listedIn: Map<string, string[]>,
5948
6077
  query: QueryFn,
5949
- ): Promise<{ recordsOf: Map<string, string[]>; archives: SignedEvent[] }> {
6078
+ ): Promise<{ recordsOf: Map<string, string[]>; archives: SignedEvent[]; rootOf: Map<string, SignedEvent> }> {
5950
6079
  const containersOf = new Map<string, { pointer: AddressPointer; containers: string[] }>()
5951
6080
  for (const event of events) {
5952
6081
  if (event.kind !== KIND_FOLDER_STATE) continue
@@ -5977,7 +6106,7 @@ async function placeRecordChannels(
5977
6106
  for (let start = 0; start < archiveAsk.length; start += MAX_FILTERS_PER_QUERY) {
5978
6107
  archives.push(...(await query(archiveAsk.slice(start, start + MAX_FILTERS_PER_QUERY))))
5979
6108
  }
5980
- return { recordsOf: new Map(), archives }
6109
+ return { recordsOf: new Map(), archives, rootOf: new Map() }
5981
6110
  }
5982
6111
 
5983
6112
  const groups = new Map<string, { kind: number; pubkey: string; identifiers: string[] }>()
@@ -6025,7 +6154,7 @@ async function placeRecordChannels(
6025
6154
  listedIn.set(channel, placed)
6026
6155
  }
6027
6156
  }
6028
- return { recordsOf, archives }
6157
+ return { recordsOf, archives, rootOf: newest }
6029
6158
  }
6030
6159
 
6031
6160
  /**
@@ -6223,6 +6352,7 @@ export async function resolveFolderContents(
6223
6352
  const folded = foldChanges(
6224
6353
  events.filter((e) => e.kind === records.changeKind && hasTagValue(e, records.targetTag, address)),
6225
6354
  records,
6355
+ root,
6226
6356
  )
6227
6357
  const nestedAdmins =
6228
6358
  pointer.kind === KIND_CHANNEL
@@ -6233,7 +6363,7 @@ export async function resolveFolderContents(
6233
6363
  ),
6234
6364
  )
6235
6365
  : undefined
6236
- const archive = archiveOf(events, address, archiveRuleOf(resolved.manifest), nestedAdmins)
6366
+ const archive = archiveOf(events, address, archiveRuleOf(resolved.manifest), root, nestedAdmins)
6237
6367
  archiveByAddress.set(address, archive)
6238
6368
  const parent = parentRefOf(resolved.manifest, pointer.kind, root, folded)
6239
6369
  parentByAddress.set(address, parent)
@@ -6309,7 +6439,8 @@ export async function resolveFolderContents(
6309
6439
  if (outside.length) {
6310
6440
  const ruleFor = (parent: string) => parentRuleOf.get(parent) ?? ARCHIVE_RULE
6311
6441
  const changes = await readArchiveChanges(outside, query, outside.map(ruleFor))
6312
- for (const parent of outside) archiveByAddress.set(parent, archiveOf(changes, parent, ruleFor(parent)))
6442
+ // A parent in another Folder is held by address only (SPEC §6.2's exception).
6443
+ for (const parent of outside) archiveByAddress.set(parent, archiveOf(changes, parent, ruleFor(parent), undefined))
6313
6444
  }
6314
6445
  const files: ForeignObject[] = []
6315
6446
  for (const { file, archive } of listed) {