Skip to content

Batch the drafts of a large copy into a Work - #1663

Merged
haowjy merged 2 commits into
mainfrom
perf/batch-draft-admission
Nov 16, 2026
Merged

haowjy merged 2 commits into
mainfrom
perf/batch-draft-admission

Conversation

@haowjy

@haowjy haowjy commented Nov 16, 2026 •

Copy link
Copy Markdown
Owner

Changelog

  • Changed: Reduce draft-creation overhead when copying many files into a Work.

Why

Copying a folder into a Work creates a draft for each tracked file while holding the project tree's list key. On main, one 197-document copy held that key for 6.70 seconds, including 3.88 seconds creating drafts. Other tree writes wait for the key.

Examples

  1. Start pnpm dev, open a project and select a Work in Draft view.
  2. Copy a document into that Work and open it in Changes. The copied text is in the draft; Live is unchanged.
  3. Apply the draft. Its text is now live. This change does not change the screen or the result; it reduces work during admission.

Draft

After Apply

Benchmarks

One instrumented run per version on the same shared 24-CPU host, with 197 numbered text documents and a fresh Work for each copy. The before commit is d2ebf589f9; after is this branch before the final input-size and classifier refinements. Both copies returned 198 entries. These single runs had different host load and cannot establish a speedup. Before load was 35.04/46.30/48.14; after was 79.74/50.61/47.80. CLI elapsed time includes startup and work outside the lock.

Path Before After Change
List-key hold 6,696 ms 3,993 ms Inconclusive
Draft admission 3,879 ms 154 ms Inconclusive
CLI elapsed 10,881 ms 12,355 ms Inconclusive

Design decisions

  1. Batch only genuinely fresh documents. Under sorted document mutation keys, a set query checks for a head, checkpoint, update, named draft and any active draft. All other cases use the existing single-document authority and draft paths. In particular, a pre-seeded head is not replaced and an occupied draft ID retains the existing resolution rule.
  2. Reuse the empty seed. Fresh authorities use installSeeds; fresh drafts share one empty content-only seed. Ownership and liveness are checked before insertion. Draft inserts are bounded and a conflict returns that member to the single path. The list transaction still holds the branch lifecycle fence through commit.
  3. Keep append unchanged. Content appends remain serialized through the existing draft runtime. The single-run timing is not enough to justify a new plural append boundary.

Changes

  • drizzle-created-drafts.ts: set classification, sorted keys, ownership checks, empty authority and draft inserts, and fallback.
  • Branch ownership: share plural placement and owner lookup and one draftability decision between batch and single-document paths.
  • Draft store: share the row shape between single and batch inserts; wire tree admission directly in collab composition.
  • Document-revisions test setup: supply the existing single-document admission behavior to its reader.
  • document-authority-and-schema.md: document the fresh/exceptional boundary.
  • Screenshots: one-document draft and Apply.

Compatibility and risk

  • Breaking or changed for writers: none intended; drafts and Apply retain their existing results.
  • Migrations and state: none. Existing authorities, journals and draft rows are not rewritten.
  • Clients during deploy: no protocol change.
  • Rollback: the rows use the existing schema and remain readable by the single-document path.
  • Where to look: the classifier and liveness/ownership checks in drizzle-created-drafts.ts; seed and insert conflict handling; the unchanged fallback.

Verification

  • Browser: a copied document showed Timing content 1 in Draft; Apply left the same text live. A new file created from the Work's tree appeared in Draft and not Live. A forced copy whose destination ID already had a seeded head showed both its seed and copied content; Undo of Discard restored both. In Trash, Undo restore discarded the same item again; restoring it once more retained its ID and both paragraphs, with a competing name resolved separately.
  • Large copy: before and after each returned 197 files and one folder. A sampled copied document contained the expected text; full content and Apply of the 197-file copy remain unverified because the review screen did not finish loading on either branch or main.
  • A mock-model main run created two files beside one copied file; Apply all sent three items and all three landed.
  • Tests: no new tests retained; the existing document-revisions harness now supplies its admission dependency. pnpm test:db passed 93 files and 605 tests. Typecheck passed. The first two pnpm check runs failed on timeouts; the shutdown test passed in a separate server suite. A third pnpm check passed (301 unit files, then 70 database files); it was started just before a classifier predicate was narrowed to reject any active draft, which the typecheck and tests then read.
  • Review-fix probe: both cases passed, including byte-identical fresh state, seeded live content, cross-account rollback and three overlapping transactions (707 ms). The scratch runner exited nonzero only because its temporary suite was not in the test budget; the suite and temporary manifest entry were removed.
  • Review-fix full DB suite: pnpm test:db passed 93 files and 605 tests. pnpm check passed (301 unit files and 82 related DB files); it started while the wiring fix was being made, with typecheck and tests reading the final files.

Deferred

The requested five-run alternating comparison, with append timed separately, has not been run; the single-run measurements above are inconclusive. Passing snapshots from tree admission to Copy would cross the tree-list store boundary, which currently returns no collab data, and was not a small local change. Copy still reads each draft and its base.

The plural append is not part of this change. Revisit only if a controlled comparison shows the remaining serial append cost matters.

Large Changes loading also occurs on main: 197 copied file rows appeared, but after 30 seconds only 56 had individual Apply buttons and Apply all was absent. The main HAR captured 394 draft-metadata requests and 96 provenance requests, with 186 and 8 unfinished respectively; the database had no lock waits. The lead will open a client investigation from the evidence in work/evidence/p18066-main-review.md. A main run with one copied file and two mock-AI creates did not reproduce the reported missing copy: Apply all sent three items and all three landed.

References

Spawn Agent Model Role
p18066 coder see spawn record Implementation and verification

Take document keys in order and classify journal and draft state before seeding. Batch only fresh authorities and drafts; retained or occupied IDs keep the existing single-document path.
@haowjy

haowjy commented Nov 16, 2026

Copy link
Copy Markdown
Owner Author

Tree lead sign-off: plan review p18027 (two-way classifier) → build → code review p18128 approved with one fix (the batch path re-derived branch ownership in SQL), fixed in 063cded: plural loadDocumentOwners in branch-ownership.ts, one draftable rule both paths call, one draft row builder. Reviewer's probes: fresh/pre-seeded/occupied-id/other-branch drafts identical to the single path byte for byte; 3 concurrent admissions over 30 docs in reverse order, no deadlock. Browser run p18148 on 063cded: 20-document copy into a draft, Live untouched, Apply all lands all 20 with text; mock-AI drafts, Discard and restore; New file on Draft. Not run in the browser: a mixed folder with existing history and two tabs (covered by the reviewer's DB probes). Timing: admission 3.88 s → 0.15 s on one run; the controlled comparison is left inconclusive in the body; plural append not built. CI 10/10.

@haowjy
haowjy merged commit 77a717c into main Nov 16, 2026
11 checks passed
@haowjy
haowjy deleted the perf/batch-draft-admission branch November 16, 2026 06:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant