Skip to content

armgit / bento

Public Imported from GitHub
0

Bento, the office suite that fits in a file

docfields: add the 'merge' copy tier (one document per recipient) (#698)

nyblnet1cc5dba2e6eb
diff --git a/kernel/src/docfields.ts b/kernel/src/docfields.ts
index 14d06113..5801f3a5 100644
--- a/kernel/src/docfields.ts
+++ b/kernel/src/docfields.ts
@@ -143,10 +143,19 @@ export type FieldClass = 'content' | 'identity' | 'mode' | 'capability' | 'histo
* copy). The rest are copies handed to someone else. */
export type Tier =
| 'file' | 'duplicate' | 'invite' | 'reader' | 'audience'
- | 'package' | 'link' | 'template' | 'copyJSON'
+ | 'package' | 'link' | 'template' | 'copyJSON' | 'merge'
export const TIERS: readonly Tier[] =
- ['file', 'duplicate', 'invite', 'reader', 'audience', 'package', 'link', 'template', 'copyJSON']
+ ['file', 'duplicate', 'invite', 'reader', 'audience', 'package', 'link', 'template', 'copyJSON', 'merge']
+
+/** The 'merge' tier: one complete document per recipient (type's mail merge) —
+ * a file for SOMEONE ELSE, so content is kept, identity is reset (a fresh
+ * docId, minted by the builder after projection), and capability, history,
+ * modes (readonly/template) and unknown keys are all dropped. Distinct from
+ * 'duplicate' (keeps history, re-mints owner creds) and 'copyJSON' (keeps the
+ * docId). NOTE: projectForCopy does NOT recurse into embedded documents, so a
+ * caller that merges a doc carrying embeds must pre-strip them
+ * (withoutEmbeddedCaps/withoutCaps) exactly as the other export tiers do. */
/** What a copy does with one field in one tier.
* - keep: copy the live value as-is.
@@ -182,25 +191,25 @@ export const CLASS_RULES: {
} = {
content: {
file: 'keep', duplicate: 'keep', invite: 'keep', reader: 'keep', audience: 'keep',
- package: 'keep', link: 'keep', template: 'keep', copyJSON: 'keep',
+ package: 'keep', link: 'keep', template: 'keep', copyJSON: 'keep', merge: 'keep',
},
identity: {
file: 'keep', duplicate: 'reset', invite: 'keep', reader: 'keep', audience: 'keep',
- package: 'keep', link: 'keep', template: 'keep', copyJSON: 'keep',
+ package: 'keep', link: 'keep', template: 'keep', copyJSON: 'keep', merge: 'reset',
},
mode: {
file: 'per-field', duplicate: 'per-field', invite: 'per-field', reader: 'per-field',
audience: 'per-field', package: 'per-field', link: 'per-field', template: 'per-field',
- copyJSON: 'per-field',
+ copyJSON: 'per-field', merge: 'per-field',
},
capability: {
file: 'keep', duplicate: 'reset', invite: { allowlist: 'invite' }, reader: { allowlist: 'reader' },
audience: { allowlist: 'audience' }, package: 'drop', link: { allowlist: 'reader' },
- template: 'drop', copyJSON: 'drop',
+ template: 'drop', copyJSON: 'drop', merge: 'drop',
},
history: {
file: 'keep', duplicate: 'keep', invite: 'keep', reader: 'drop', audience: 'drop',
- package: 'drop', link: 'drop', template: 'drop', copyJSON: 'drop',
+ package: 'drop', link: 'drop', template: 'drop', copyJSON: 'drop', merge: 'drop',
},
}
@@ -215,7 +224,7 @@ export const MODE_FIELD_RULES = {
// read-only.
readonly: {
file: 'keep', duplicate: 'keep', invite: 'drop', reader: { set: true }, audience: { set: true },
- package: { set: true }, link: { set: true }, template: 'drop', copyJSON: 'drop',
+ package: { set: true }, link: { set: true }, template: 'drop', copyJSON: 'drop', merge: 'drop',
},
// template is kept ONLY by the owner's own round-trips and set by the template
// copy. parseDoc treats template:true as "this open IS a new document" (fresh
@@ -224,7 +233,7 @@ export const MODE_FIELD_RULES = {
// and silently loses the room.
template: {
file: 'keep', duplicate: 'keep', invite: 'drop', reader: 'drop', audience: 'drop',
- package: 'drop', link: 'drop', template: { set: true }, copyJSON: 'drop',
+ package: 'drop', link: 'drop', template: { set: true }, copyJSON: 'drop', merge: 'drop',
},
} satisfies Record<string, Record<Tier, Rule>>
@@ -238,7 +247,7 @@ export type ModeField = keyof typeof MODE_FIELD_RULES
* not ride it). This is the fail-closed default for anything not in the map. */
const UNKNOWN_RULE: Record<Tier, Rule> = {
file: 'keep', duplicate: 'keep', invite: 'drop', reader: 'drop', audience: 'drop',
- package: 'drop', link: 'drop', template: 'drop', copyJSON: 'drop',
+ package: 'drop', link: 'drop', template: 'drop', copyJSON: 'drop', merge: 'drop',
}
/** Extract the field names an app's class map assigns to class C. */
diff --git a/scripts/test-field-class.ts b/scripts/test-field-class.ts
index 92e90cfe..49917eff 100644
--- a/scripts/test-field-class.ts
+++ b/scripts/test-field-class.ts
@@ -86,6 +86,10 @@ const EXPECT: Record<Tier, string[]> = {
template: ['docId', 'title', 'slides', 'template', 'modified', 'preview', 'autosave'].sort(),
// Copy document JSON: content only — collab, modes, history, unknown all gone
copyJSON: ['docId', 'title', 'slides', 'modified', 'preview', 'autosave'].sort(),
+ // mail merge — one complete document per recipient: content kept, identity
+ // RESET (docId absent, the builder mints a fresh one), capability + history +
+ // modes + unknown all dropped. copyJSON minus docId.
+ merge: ['title', 'slides', 'modified', 'preview', 'autosave'].sort(),
}
console.log('projectForCopy emits exactly the ruled field set per tier')
@@ -166,12 +170,40 @@ console.log('\na {set} mode cell fires even when the SOURCE has no such field')
const tpl = projectForCopy(noModes(), MAP, 'template', OPTS) as Record<string, unknown>
ok(tpl.template === true, 'template copy of a doc with NO template key is still a template')
ok(!('readonly' in tpl), 'and invents no readonly')
- for (const tier of ['file', 'duplicate', 'invite', 'copyJSON'] as Tier[]) {
+ for (const tier of ['file', 'duplicate', 'invite', 'copyJSON', 'merge'] as Tier[]) {
const out = projectForCopy(noModes(), MAP, tier, OPTS) as Record<string, unknown>
ok(!('readonly' in out) && !('template' in out), `${tier} invents no mode field (no set cell there)`)
}
}
+console.log('\nthe merge tier — a complete document for someone else (type\'s mail merge)')
+{
+ const out = projectForCopy(liveDoc(), MAP, 'merge', OPTS) as Record<string, unknown>
+ // identity reset — the one that makes merge distinct from copyJSON
+ ok(!('docId' in out), 'identity is RESET: docId is omitted, so the builder mints a fresh one per letter')
+ ok(!('collab' in out), 'capability dropped: no room, no keys (a letter is not a live document)')
+ ok(!('revisions' in out) && !('trail' in out), 'history dropped: revisions and trail do not ride a letter')
+ ok(!('readonly' in out) && !('template' in out), 'modes dropped: a letter is neither locked nor a template (template would fork on open)')
+ ok(!('mystery' in out), 'an unknown key is dropped (fail-closed)')
+ ok(out.title === 'Secret plan' && same((out.slides as string[]), ['a', 'b']), 'content is kept verbatim')
+ // distinct from the two neighbours it could be confused with
+ const cj = projectForCopy(liveDoc(), MAP, 'copyJSON', OPTS) as Record<string, unknown>
+ ok('docId' in cj && !('docId' in out), 'merge resets docId where copyJSON keeps it')
+ const dup = projectForCopy(liveDoc(), MAP, 'duplicate', OPTS) as Record<string, unknown>
+ ok('revisions' in dup && !('revisions' in out), 'merge drops history where duplicate keeps it')
+ // absent-field source: nothing is fabricated (no {set} cell at this tier)
+ const bare = projectForCopy({ docId: 'D', title: 'hi' }, MAP, 'merge', OPTS) as Record<string, unknown>
+ ok(same(keysOf(bare), ['title']), 'a bare source yields content only — docId reset, nothing invented')
+ // BOUNDARY (security condition): projectForCopy does NOT recurse into embedded
+ // documents. A caller merging a doc with embeds must pre-strip them; the kernel
+ // leaves a nested doc's own fields untouched. This pins why merge.ts must keep
+ // the withoutEmbeddedCaps/withoutCaps wrapper.
+ const withEmbed = { ...liveDoc(), slides: [{ embed: { collab: { ownerPriv: 'NESTED-SECRET' } } }] }
+ const mergedEmbed = projectForCopy(withEmbed, MAP, 'merge', OPTS) as { slides: Array<{ embed: { collab: unknown } }> }
+ ok(JSON.stringify(mergedEmbed.slides).includes('NESTED-SECRET'),
+ 'a nested embed is NOT walked — the caller must strip embeds before merge (kept as a guard, not a feature)')
+}
+
console.log('\ndocContentKey: counts content + history, excludes capability + bookkeeping, skips absent')
{
const base = liveDoc()