@estiva-app/interop 0.64.0 → 0.65.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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@estiva-app/interop",
3
- "version": "0.64.0",
3
+ "version": "0.65.1",
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
@@ -40,6 +40,7 @@ export {
40
40
  ARCHIVED_FIELD,
41
41
  SUMMARY_FIELD,
42
42
  isArchived,
43
+ archivedAncestorOf,
43
44
  isClosedStatus,
44
45
  archiveImpact,
45
46
  type Archive,
package/src/projection.ts CHANGED
@@ -1438,7 +1438,7 @@ const BARE_FILE_MANIFEST: Manifest = {
1438
1438
  /*
1439
1439
  No `body`: a topic is a conversation and has no text (SPEC §6.7,
1440
1440
  2026-10-19), so nothing draws one and no document app lists it.
1441
- why: https://github.com/estiva-app/estiva-docs/blob/main/decisions/a-document-app-writes-what-a-files-app-shares.md
1441
+ why: https://github.com/estiva-app/estiva-docs/blob/main/decisions/a-card-changes-what-a-files-app-shares.md
1442
1442
  */
1443
1443
  // A bare file may hold bare files — a sub-topic under a topic. Children
1444
1444
  // of *other* kinds are found the other way round, by their own `a`
@@ -2519,10 +2519,16 @@ function archiveOf(
2519
2519
  /**
2520
2520
  * The first archived ancestor of `address`, walking `parentOf`, with its archive.
2521
2521
  *
2522
+ * SPEC §6.5's rule for a file archived with something above it, for an app
2523
+ * that folds its own files: the closest archived ancestor, with `with` its
2524
+ * address, so every app names the same thing to unarchive (PEEKM-2). A file
2525
+ * read through {@link resolveForeignObject}'s `archivedWith` is marked by the
2526
+ * same rule. `archiveAt` gives a file's own archive only.
2527
+ *
2522
2528
  * Stops at a cycle rather than looping: `parentRef` is written by people, and
2523
2529
  * two files each moved under the other is a state the relay accepts.
2524
2530
  */
2525
- function archivedAncestorOf(
2531
+ export function archivedAncestorOf(
2526
2532
  address: string,
2527
2533
  parentOf: (address: string) => string | undefined,
2528
2534
  archiveAt: (address: string) => Archive | undefined,
@@ -3521,7 +3527,7 @@ export async function resolveForeignObject(
3521
3527
  const childOwners = await childOwnersOf(plan, answered, childChanges, query, cache)
3522
3528
  const assembled = assembleObject(plan, answered, childChanges, childOwners)
3523
3529
  if (assembled.object.unreachable) return assembled.object
3524
- if (options.archivedWith) await markArchivedParents([{ object: assembled.object, manifest: resolved.manifest }], query)
3530
+ if (options.archivedWith) await markArchivedParents([{ object: assembled.object, manifest: resolved.manifest }], query, cache)
3525
3531
  return withFiles(
3526
3532
  withBorrowed(
3527
3533
  withPeople(assembled, await (lookupPeople ?? peopleViaRelay(query))(peopleOf(assembled))),
@@ -3663,7 +3669,7 @@ export async function resolveForeignObjects(
3663
3669
  built: assembleObject(plan, events, childChanges, ownerLists[i]),
3664
3670
  }))
3665
3671
  if (options.archivedWith) {
3666
- await markArchivedParents(assembled.map(({ plan, built }) => ({ object: built.object, manifest: plan.resolved.manifest })), query)
3672
+ await markArchivedParents(assembled.map(({ plan, built }) => ({ object: built.object, manifest: plan.resolved.manifest })), query, cache)
3667
3673
  }
3668
3674
  const wanted = [...new Set(assembled.flatMap(({ built }) => peopleOf(built)))]
3669
3675
  const reachable = assembled.some(({ built }) => !built.object.unreachable)
@@ -4004,42 +4010,143 @@ function withFiles(object: ForeignObject, files: ForeignObject[] | undefined): F
4004
4010
  * rather than drawing live work. The cascade is computed here and never written
4005
4011
  * to the issue, so unarchiving the project restores it exactly.
4006
4012
  *
4007
- * **One read for the whole set, and only when some object has a parent**, so a
4008
- * topic page or a project page pays nothing. One level: the parent's own
4009
- * parent is not followed, because the page is about this file and its
4010
- * container, and a deeper walk is a round trip per level. A Folder listing
4011
- * walks the whole chain it holds ({@link resolveFolderContents}).
4013
+ * **The whole parent chain, the closest archived file named** (SPEC §6.5,
4014
+ * PEEKM-2): a page under a page under an archived page is archived with the
4015
+ * one nearest it, as a Folder listing reads it ({@link archivedAncestorOf}), so
4016
+ * every app closes the same box and names the same thing to unarchive. One
4017
+ * read per level for the whole set, and only while some file still has a
4018
+ * parent, so a topic page or a project page pays nothing.
4019
+ *
4020
+ * Each ancestor is read with its root, so its archive is folded by SPEC §6.2's
4021
+ * `h` rule like any other change, and its own parent is read by its own kind:
4022
+ * an issue's moved `project`, a page's `parent`. An ancestor whose root this
4023
+ * reader cannot see ends the walk there, as not archived. Stops at a cycle.
4012
4024
  *
4013
- * The parent is folded by the child's app's rule when that app also draws the
4014
- * parent's kind — a project is Ship's as its issue is — and by SPEC's rule
4015
- * otherwise, since the parent's own manifest is not in hand ({@link relatedRuleOf}).
4025
+ * Each ancestor's rule is its own app's: the child's when that app also draws
4026
+ * the ancestor's kind — a project is Ship's as its issue is — otherwise the
4027
+ * ancestor's manifest, read through `cache`.
4016
4028
  */
4017
4029
  async function markArchivedParents(
4018
- entries: { object: ForeignObject; manifest?: Pick<Manifest, 'records' | 'projections'> }[],
4030
+ entries: { object: ForeignObject; manifest?: Manifest }[],
4019
4031
  query: QueryFn,
4032
+ cache?: ProjectionCache,
4020
4033
  ): Promise<void> {
4021
- const waiting = entries.filter(
4022
- ({ object: o }) => !o.unreachable && !o.archived && o.parentRef && addressPointerOf(o.parentRef),
4034
+ await walkArchivedAbove(
4035
+ entries
4036
+ .filter(({ object: o }) => !o.unreachable && !o.archived && o.parentRef && plainAddress(o.parentRef))
4037
+ .map(({ object, manifest }) => ({
4038
+ manifest,
4039
+ at: plainAddress(object.parentRef!)!,
4040
+ seen: new Set([plainAddress(object.address ?? '') ?? '']),
4041
+ mark: (archive: Archive) => {
4042
+ object.archived = archive
4043
+ },
4044
+ })),
4045
+ query,
4046
+ cache,
4023
4047
  )
4024
- if (waiting.length === 0) return
4025
- const rules = waiting.map(({ object, manifest }) => relatedRuleOf(manifest, object.parentRef!))
4026
- const parents = [...new Set(waiting.map(({ object }) => object.parentRef!))]
4027
- const changes = await readArchiveChanges(parents, query, rules)
4028
- for (const [index, { object }] of waiting.entries()) {
4029
- // Only the parent's address is held here, not its root (SPEC §6.2's exception).
4030
- const archive = archiveOf(changes, object.parentRef!, rules[index]!, undefined)
4031
- if (archive) object.archived = { ...archive, with: object.parentRef! }
4032
- }
4048
+ }
4049
+
4050
+ // An `naddr` parent names the same file as its plain address (FEE-2), and
4051
+ // only the plain one is what a change's target tag and `with` carry.
4052
+ function plainAddress(reference: string) {
4053
+ const pointer = addressPointerOf(reference)
4054
+ return pointer ? `${pointer.kind}:${pointer.pubkey}:${pointer.identifier}` : undefined
4033
4055
  }
4034
4056
 
4035
4057
  /**
4036
- * The rule another file's archive is read by, from an app known to hold it:
4037
- * that app's own, when it draws the file's kind — an issue's project is Ship's,
4038
- * a hive's inspection the hive log's — and SPEC's otherwise.
4058
+ * {@link markArchivedParents}'s walk, from `at` upwards: `mark` is called with
4059
+ * the closest archived file's archive, `with` its address, and not at all when
4060
+ * none is. `manifest` is the app of the file below `at`, `seen` what is below.
4039
4061
  */
4040
- function relatedRuleOf(manifest: Pick<Manifest, 'records' | 'projections'> | undefined, address: string): RecordsRule {
4041
- const kind = addressPointerOf(address)?.kind
4042
- return manifest && kind !== undefined && manifest.projections?.[String(kind)] ? archiveRuleOf(manifest) : ARCHIVE_RULE
4062
+ async function walkArchivedAbove(
4063
+ starts: { at: string; manifest?: Manifest; seen: Set<string>; mark: (archive: Archive) => void }[],
4064
+ query: QueryFn,
4065
+ cache?: ProjectionCache,
4066
+ ): Promise<void> {
4067
+ type Walk = (typeof starts)[number]
4068
+ let walking: Walk[] = starts
4069
+ while (walking.length) {
4070
+ // Each child's ancestor is read by that child's app when it draws the
4071
+ // ancestor's kind — a project is Ship's as its issue is — and by the
4072
+ // ancestor's own manifest otherwise, so one file in a batch never sets the
4073
+ // rule another file's ancestor is read by.
4074
+ const drawnBy: Manifest[] = []
4075
+ const keyed = walking.map((walk) => {
4076
+ const own = walk.manifest?.projections?.[String(addressPointerOf(walk.at)!.kind)] ? walk.manifest : undefined
4077
+ if (own && !drawnBy.includes(own)) drawnBy.push(own)
4078
+ return { walk, key: `${walk.at}|${own ? drawnBy.indexOf(own) : ''}`, own }
4079
+ })
4080
+ // A level whose manifest cannot be read ends the walk, its own archive
4081
+ // uncounted (SPEC §6.5): which change archives it is the manifest's to say.
4082
+ // A bare file's is a constant, so a refused opener sweep never ends it.
4083
+ const theirOwn = new Map<string, Promise<Manifest | undefined>>()
4084
+ const resolved = await Promise.all(
4085
+ [...new Map(keyed.map((entry) => [entry.key, entry])).values()].map(async ({ key, walk, own }) => {
4086
+ const pointer = addressPointerOf(walk.at)!
4087
+ if (!own && !theirOwn.has(walk.at)) {
4088
+ theirOwn.set(
4089
+ walk.at,
4090
+ resolveManifest(pointer, query, cache).then(
4091
+ (r) => r?.manifest,
4092
+ () => (pointer.kind === KIND_BARE_FILE ? bareFileManifest().manifest : undefined),
4093
+ ),
4094
+ )
4095
+ }
4096
+ return { key, address: walk.at, pointer, manifest: own ?? (await theirOwn.get(walk.at)) }
4097
+ }),
4098
+ )
4099
+ const ancestors = resolved.filter((a): a is typeof a & { manifest: Manifest } => !!a.manifest)
4100
+ const rules = ancestors.map(({ manifest }) => archiveRuleOf(manifest))
4101
+ const addresses = [...new Set(ancestors.map(({ address }) => address))]
4102
+ const roots = new Map<string, { kinds: number[]; authors: string[]; '#d': string[] }>()
4103
+ for (const address of addresses) {
4104
+ const pointer = addressPointerOf(address)!
4105
+ const group = `${pointer.kind}:${pointer.pubkey}`
4106
+ const filter = roots.get(group) ?? { kinds: [pointer.kind], authors: [pointer.pubkey], '#d': [] }
4107
+ filter['#d'].push(pointer.identifier)
4108
+ roots.set(group, filter)
4109
+ }
4110
+ const filters = [
4111
+ ...[...roots.values()].flatMap((filter) => {
4112
+ const groups: Record<string, unknown>[] = []
4113
+ for (let start = 0; start < filter['#d'].length; start += PARENT_ROOT_GROUP) {
4114
+ groups.push({ ...filter, '#d': filter['#d'].slice(start, start + PARENT_ROOT_GROUP) })
4115
+ }
4116
+ return groups
4117
+ }),
4118
+ ...archiveFilters(addresses, rules),
4119
+ ]
4120
+ const events = await queryChunked(filters, query)
4121
+ const read = new Map<string, { archive?: Archive; parent?: string; manifest?: Manifest }>()
4122
+ for (const [index, { key, address, pointer, manifest }] of ancestors.entries()) {
4123
+ const root = events
4124
+ .filter((e) => e.kind === pointer.kind && e.pubkey === pointer.pubkey && tagValue(e, 'd') === pointer.identifier)
4125
+ .sort(byOrder)
4126
+ .at(-1)
4127
+ // Not readable here: the walk ends, and nothing above it is claimed.
4128
+ if (!root) continue
4129
+ const parent = manifest?.projections?.[String(pointer.kind)]
4130
+ ? parentRefOf(manifest, pointer.kind, root, foldOf(events, address, manifest, root))
4131
+ : undefined
4132
+ read.set(key, { archive: archiveOf(events, address, rules[index]!, root), parent, manifest })
4133
+ }
4134
+ const next: Walk[] = []
4135
+ for (const { walk, key } of keyed) {
4136
+ const found = read.get(key)
4137
+ if (!found) continue
4138
+ if (found.archive) {
4139
+ walk.mark({ ...found.archive, with: walk.at })
4140
+ continue
4141
+ }
4142
+ walk.seen.add(walk.at)
4143
+ const parent = found.parent && plainAddress(found.parent)
4144
+ if (parent && !walk.seen.has(parent)) {
4145
+ next.push({ ...walk, manifest: found.manifest, at: parent })
4146
+ }
4147
+ }
4148
+ walking = next
4149
+ }
4043
4150
  }
4044
4151
 
4045
4152
  /**
@@ -6784,8 +6891,8 @@ export async function resolveFolderContents(
6784
6891
  const listed: { file: ForeignObject; archive?: Archive }[] = []
6785
6892
  const archiveByAddress = new Map<string, Archive | undefined>()
6786
6893
  const parentByAddress = new Map<string, string | undefined>()
6787
- // How a parent outside this listing is read: its child's app's rule when that app draws it.
6788
- const parentRuleOf = new Map<string, RecordsRule>()
6894
+ // Whose app reads a parent outside this listing: its child's, when that app draws it.
6895
+ const manifestByAddress = new Map<string, Manifest>()
6789
6896
  for (const { address, pointer } of pointers) {
6790
6897
  const resolved = manifests.get(manifestKeyOf(pointer))
6791
6898
  // No app claims this kind, so there is no projection to draw it with.
@@ -6816,8 +6923,9 @@ export async function resolveFolderContents(
6816
6923
  const archive = archiveOf(events, address, archiveRuleOf(resolved.manifest), root, nestedAdmins)
6817
6924
  archiveByAddress.set(address, archive)
6818
6925
  const parent = parentRefOf(resolved.manifest, pointer.kind, root, folded)
6819
- parentByAddress.set(address, parent)
6820
- if (parent) parentRuleOf.set(parent, relatedRuleOf(resolved.manifest, parent))
6926
+ // Plain, as the listing's own addresses are, so an `naddr` parent listed here is found here.
6927
+ parentByAddress.set(address, parent && (plainAddress(parent) ?? parent))
6928
+ manifestByAddress.set(address, resolved.manifest)
6821
6929
  // An app hiding a record from its own lists is saying it is not part of the
6822
6930
  // folder any more. `hiddenWhen` is the app's own declaration of that — the
6823
6931
  // archive above is the same statement for a kind whose app declared none.
@@ -6869,36 +6977,63 @@ export async function resolveFolderContents(
6869
6977
  never written to the children, so unarchiving the parent restores
6870
6978
  exactly what was there.
6871
6979
 
6872
- The chain is walked through everything this Folder lists. A parent that
6873
- lives in another Folder is read once for all such parents, one level: a
6874
- project whose record channel lists its issues is the case that matters,
6875
- and there the project is listed too.
6980
+ The chain is walked through everything this Folder lists. Where it
6981
+ leaves the Folder, the rest is walked as a link reads it (SPEC §6.5,
6982
+ PEEKM-2): each ancestor with its root, so its archive is folded by
6983
+ §6.2's `h` rule, at any depth. One read per level for every such exit,
6984
+ and none when the chain stays here or meets an archived file first.
6876
6985
 
6877
6986
  Only by parent. A file this Folder lists is never hidden because some
6878
6987
  *other* Folder that lists it is archived — that is what lets a file
6879
6988
  placed in two Folders stay visible in the one that is not archived.
6880
6989
  */
6881
- const outside = [
6882
- ...new Set(
6883
- listed.flatMap(({ file }) => {
6884
- const parent = file.address && parentByAddress.get(file.address)
6885
- return parent && !archiveByAddress.has(parent) && addressPointerOf(parent) ? [parent] : []
6886
- }),
6887
- ),
6888
- ]
6889
- if (outside.length) {
6890
- const ruleFor = (parent: string) => parentRuleOf.get(parent) ?? ARCHIVE_RULE
6891
- const changes = await readArchiveChanges(outside, query, outside.map(ruleFor))
6892
- // A parent in another Folder is held by address only (SPEC §6.2's exception).
6893
- for (const parent of outside) archiveByAddress.set(parent, archiveOf(changes, parent, ruleFor(parent), undefined))
6990
+ // One walk per exit and the app reading it, as walkArchivedAbove keys its
6991
+ // reads: two children of one outside parent, drawn by different apps, each
6992
+ // read it by their own app's rule.
6993
+ const exits = new Map<string, { at: string; manifest: Manifest | undefined; seen: Set<string>; archive?: Archive }>()
6994
+ const exitOf = new Map<string, string>()
6995
+ const drawnBy: Manifest[] = []
6996
+ for (const { file, archive } of listed) {
6997
+ if (archive || !file.address) continue
6998
+ const seen = new Set([file.address])
6999
+ let child = file.address
7000
+ for (let at = parentByAddress.get(child); at && !seen.has(at); child = at, at = parentByAddress.get(at)) {
7001
+ if (!archiveByAddress.has(at)) {
7002
+ const plain = plainAddress(at)
7003
+ if (!plain) break
7004
+ const manifest = manifestByAddress.get(child)
7005
+ const own = manifest?.projections?.[String(addressPointerOf(plain)!.kind)] ? manifest : undefined
7006
+ if (own && !drawnBy.includes(own)) drawnBy.push(own)
7007
+ const key = `${plain}|${own ? drawnBy.indexOf(own) : ''}`
7008
+ if (!exits.has(key)) exits.set(key, { at: plain, manifest, seen })
7009
+ exitOf.set(file.address, key)
7010
+ break
7011
+ }
7012
+ if (archiveByAddress.get(at)) break
7013
+ seen.add(at)
7014
+ }
6894
7015
  }
7016
+ await walkArchivedAbove(
7017
+ [...exits.values()].map((exit) => ({
7018
+ at: exit.at,
7019
+ manifest: exit.manifest,
7020
+ seen: exit.seen,
7021
+ mark: (found: Archive) => {
7022
+ exit.archive = found
7023
+ },
7024
+ })),
7025
+ query,
7026
+ cache,
7027
+ )
6895
7028
  const files: ForeignObject[] = []
6896
7029
  for (const { file, archive } of listed) {
7030
+ const exit = file.address ? exitOf.get(file.address) : undefined
6897
7031
  const effective =
6898
7032
  archive ??
6899
7033
  (file.address
6900
7034
  ? archivedAncestorOf(file.address, (a) => parentByAddress.get(a), (a) => archiveByAddress.get(a))
6901
- : undefined)
7035
+ : undefined) ??
7036
+ (exit ? exits.get(exit)?.archive : undefined)
6902
7037
  if (!effective) files.push(file)
6903
7038
  else if (options.includeArchived) files.push({ ...file, archived: effective })
6904
7039
  }