Skip to content

armgit / bento

Public Imported from GitHub
0

Bento, the office suite that fits in a file

type: mail merge builds each letter at the kernel's 'merge' tier (#699)

nyblnetc580a28aef5d
diff --git a/scripts/test-type-share.ts b/scripts/test-type-share.ts
index fe2141cb..65f46c9b 100644
--- a/scripts/test-type-share.ts
+++ b/scripts/test-type-share.ts
@@ -295,7 +295,7 @@ H('Copy document JSON carries no history and no credentials (tier copyJSON)')
ok(!carriesHistory(docForExport(B.doc)), 'BARE: invents no history')
}
-H('mail merge: every letter is a new document with no history (copyJSON + a fresh identity)')
+H('mail merge: every letter is a new document with no history (the \'merge\' tier + a fresh identity)')
{
const a = mergeDoc(F.doc, { Name: 'Ada' }, ['Name'], 'list.bento.html')
const b = mergeDoc(F.doc, { Name: 'Grace' }, ['Name'], 'list.bento.html')
@@ -308,6 +308,43 @@ H('mail merge: every letter is a new document with no history (copyJSON + a fres
}
const B = await bare()
ok(!carriesHistory(mergeDoc(B.doc, { Name: 'X' }, ['Name'], 's')), 'BARE: invents no history')
+
+ // N letters, N identities. Each is minted in the builder after the tier's
+ // identity reset, so each is distinct from the moment it is written and none
+ // is the template's — two letters sharing one docId would be two files
+ // autosave believes are the same document.
+ const N = 7
+ const ids = Array.from({ length: N }, (_, i) => mergeDoc(F.doc, { Name: `R${i}` }, ['Name'], 's').docId)
+ ok(new Set(ids).size === N, `${N} letters have ${N} distinct docIds`)
+ ok(!ids.includes(F.doc.docId), 'and none of them is the source document\'s')
+ ok(ids.every(id => typeof id === 'string' && id.length > 0), 'every letter HAS an identity — the reset was followed by a mint')
+ const m0 = mergeDoc(F.doc, { Name: 'Z' }, ['Name'], 's')
+ ok(m0.readonly === undefined && m0.template === undefined, 'a letter is neither locked nor a template (both modes dropped)')
+ ok(m0.merge?.row?.Name === 'Z', 'and still records the row it was merged from')
+}
+
+H('mail merge: an EMBEDDED document\'s keys never reach a letter')
+{
+ // Built RAW, not through parseDoc: parseDoc already strips an embed's keys on
+ // intake, so a parsed fixture would carry none and every check below would
+ // pass for free. A document held in memory is not guaranteed to have come
+ // through parseDoc, and projectForCopy works on top-level fields only — it
+ // does not walk into an embed. mergeDoc's pre-strip is what covers this.
+ const src = { ...emptyDoc(), title: 'Letter' } as TypeDoc
+ src.body = [
+ { id: 'p1', kind: 'para', text: 'See the attached figures.' },
+ { id: 'e1', kind: 'embed', text: '',
+ embed: { app: 'bento/dash', view: '<svg/>',
+ doc: { format: 'bento/dash', title: 'Q3',
+ collab: { room: 'w-embed', key: 'EMBED-READ-CAP', ownerPriv: 'EMBED-OWNER-PRIV' } } } } as never,
+ ]
+ ok(has(src, 'EMBED-OWNER-PRIV') && has(src, 'EMBED-READ-CAP'),
+ 'the SOURCE really carries an embedded document\'s owner key and read cap')
+ const letter = mergeDoc(src, { Name: 'Ada' }, ['Name'], 's')
+ ok(!has(letter, 'EMBED-OWNER-PRIV'), 'the letter carries no embedded owner key')
+ ok(!has(letter, 'EMBED-READ-CAP'), 'and no embedded read capability')
+ ok(has(letter, 'Q3') && letter.body.some(b => b.kind === 'embed'),
+ 'while the embed itself — its content — is still in the letter')
}
H('an INVITE keeps history by default, and leaves it out when asked (tier invite)')
diff --git a/type/src/merge.ts b/type/src/merge.ts
index 63dfe7bd..10c2bcb8 100644
--- a/type/src/merge.ts
+++ b/type/src/merge.ts
@@ -23,9 +23,12 @@
// one field the format says never to regenerate is MINTED here,
// because these are new documents rather than copies of one.
//
-// collab GONE. `docForExport` strips it by REMOVING the field, so a
-// private key added to the credentials later is covered without
-// anyone remembering this line. The template's room key and
+// collab GONE. The kernel's 'merge' copy tier drops the whole
+// capability class, so a private key added to the credentials
+// later is covered without anyone remembering this line. Keys
+// inside EMBEDDED documents are stripped first (withoutEmbeddedCaps):
+// projectForCopy works on top-level fields and does not walk
+// into an embed. The template's room key and
// owner key are a live write capability on the author's own
// document; shipping them to every recipient hands each of them
// edit access to the master, unrevocably.
@@ -74,7 +77,9 @@
import { t } from './i18n.ts';
import { registerMenuItem, type FeatureContext } from './features.ts';
-import { docForExport, newDocId, spliceText, type TypeDoc } from './model.ts';
+import { newDocId, spliceText, withoutEmbeddedCaps, type TypeDoc } from './model.ts';
+import { projectForCopy, withoutCaps } from '../../kernel/src/docfields.ts';
+import { TYPE_FIELDS } from './docclass.ts';
// Importing fields.ts here is also what MOUNTS it: registry.ts switches this
// feature on with one line, and the field engine comes with the merge that
// needs it.
@@ -282,25 +287,28 @@ export function sourceOfDoc(parsed: unknown): MergeSource | null {
* The template, merged against one row.
*
* See this file's header for what is minted, what is stripped and why. The
- * clone is deep and taken from `docForExport`, so the credentials are gone
- * before anything else is decided about the output.
+ * clone is deep and built by the kernel's 'merge' copy tier, so the credentials
+ * and the history are gone * before anything else is decided about the output.
*/
export function mergeDoc(template: TypeDoc, row: Record<string, string>,
columns: string[], source: string): TypeDoc {
- const out = JSON.parse(JSON.stringify(docForExport(template))) as TypeDoc;
- // TIER GAP. A merged letter is a file for someone else with a FRESH identity,
- // no capability and no history, and the kernel's copy table has no tier that
- // says exactly that: 'duplicate' resets identity but KEEPS history, 'copyJSON'
- // drops history but keeps docId. So this projects at 'copyJSON' (via
- // docForExport) and then mints the identity here — the one post-projection
- // identity edit projectForCopy's contract otherwise forbids. A 'merge' tier
- // (content only; fresh identity; no capability; no history; modes dropped)
- // would remove it; raised with kernel.
+ // The kernel's 'merge' tier: content kept; identity RESET; capability,
+ // history (revisions, comments, signatures), both modes and any undeclared
+ // key dropped. Embedded documents' keys go first, because the projection
+ // works on top-level fields and never walks into an embed. Deep-cloned
+ // because 'keep' carries values by reference: without it every letter's body
+ // would share objects with the LIVE template.
+ const out = JSON.parse(JSON.stringify(
+ projectForCopy(withoutEmbeddedCaps(withoutCaps(template)), TYPE_FIELDS, 'merge'))) as TypeDoc;
+ // A reset field is ABSENT, and minting it is the caller's step — the same
+ // sanctioned pattern 'duplicate' uses, not an edit of a kept field. Minted
+ // here, per letter, rather than left to parseDoc at first open, so each letter
+ // has its own stable identity from the moment it is written.
out.docId = newDocId();
+ // TypeDoc declares these as required arrays; the tier dropped what they held.
out.signatures = [];
out.revisions = [];
- delete out.comments;
- delete out.template;
+ // The template's last-saved stamp says nothing true about a new letter.
delete out.modified;
out.merge = { source, columns: [...columns], row: { ...row } };
// The TITLE is a plain string, not a block, so its `{{tokens}}` are resolved