Worktree: agent editing and human review
Let an agent edit and inspect a Unit in an isolated Worktree draft, then let a human review and merge it through Web.
Worktree1 provides an agent with an isolated editing space. Inside a draft, the agent can pull, inspect, execute, and commit for as many rounds as it needs, then mark the Worktree Ready once the structured and visual checks pass. A person then reviews the rendered page through Web and chooses Merge or Reopen.
create Worktree→ agent edits draft→ agent structured and visual checks→ Ready→ human reviews through Web→ Merge / Reopen→ TrunkWhy use Worktree
- Intermediate edits by the agent never reach trunk.
- Each draft has its own Snapshots, changesets, revisions, Presence, and realtime rooms.
- The agent can synchronize, edit, and verify over several rounds before presenting a result.
- Ready freezes the draft at its current revision and establishes an explicit review boundary.
- A person can review, continue editing, Merge, or Reopen through Web.
- Merge still passes through the authorization and commit boundaries of the trunk Collaboration Service itself.
Complete workflow
create / select Worktree→ select Worktree Unit→ Collaboration Runtime load→ pull→ inspect→ api find / show→ execute(write)→ runtime.commit()→ inspect + screenshot + layout lint→ markReady→ Web review→ merge / reopenOnce Document loading and content operations, Office file import and export, and Visual inspection are working, point the same capabilities at a Worktree target. This produces the recommended agent editing flow.
Worktree target
Collaboration Runtime operates on the Snapshot, changesets, and revision of a single Unit, and it has no concept of a Worktree. The application adapter maps worktreeID + unitID to the Snapshot, Submit, WebSocket, and Session Ticket URLs of the Worktree Endpoint, then passes those URLs to the runtime backend.
worktreeID + unitID→ application target adapter→ Worktree protocol URLs→ Collaboration RuntimeThe same runtime execution model therefore connects either to trunk or to an isolated Worktree draft.
Agent operations in a draft
The agent uses the same core loop for a Worktree Unit as for an ordinary Unit:
load → pull → inspect → execute(write) → commitFor a ready-made Inspection entry point, @univer-cli/content-inspection-command provides two separate factories:
createContentInspectionCommand()applies to an ordinary Unit target.createWorktreeContentInspectionCommand()applies when the caller must explicitly choose--trunkor--worktree <id>.
The application applies the same kind of target selection to the execute, screenshot, and other commands, and it owns acquisition and release of the selected Runtime.
Select a Worktree in Commander
The Inspection preset has a Worktree variant that requires the caller to choose either --trunk or --worktree <id>:
import type { WorktreeContentInspectionCommandDependencies } from "@univer-cli/content-inspection-command";import { createWorktreeContentInspectionCommand } from "@univer-cli/content-inspection-command";import { Command } from "commander";function addWorktreeCommands( program: Command, dependencies: { acquireRuntime: WorktreeContentInspectionCommandDependencies["acquireRuntime"]; markReady(worktreeID: string): Promise<void>; },): void { program.addCommand( createWorktreeContentInspectionCommand({ acquireRuntime: dependencies.acquireRuntime, }), ); const worktreeCommand = new Command("worktree"); worktreeCommand .command("ready") .argument("<worktree-id>") .action(async (worktreeID) => { await dependencies.markReady(worktreeID); }); program.addCommand(worktreeCommand);}acquireRuntime and markReady are application dependencies declared explicitly here, not helpers hidden inside AI SDK. The former implements the mapping from Worktree target to Runtime described in the previous section; the latter calls the Collaboration SDK Worktree Client.
The execute, screenshot, and layout lint commands must use the same target model; otherwise one Unit ID can resolve to different trunk or draft targets. Web can expose Reopen, Discard, Merge, and real page review on the same Worktree Client. A product can wrap some of those operations in additional Commander subcommands when it needs to.
Agent checks
Before Ready, the agent can combine two kinds of checks:
structured check: pull → inspectvisual check: Unit → Render Runtime → screenshot / layout lint- Inspection verifies the structured content of a Workbook, Worksheet, Range, Document, Paragraph, Presentation, or Slide.
- Screenshot provides the rendered page for the agent to review.
- Layout Lint diagnoses layout issues on a Slide.
- When a check finds a problem, the agent can execute, commit, and check again.
Distinguish commit, ready, and merge
| Operation | Meaning |
|---|---|
runtime.commit() | Submit the changeset produced by the current mutations to the Worktree draft |
markReady() | Freeze each Unit's current draft revision and enter ready |
reopenWorktree() | Return from ready to draft to keep editing |
mergeWorktree() | Merge Worktree Units into trunk one by one |
runtime.commit() advances only the draft. It does not merge into trunk. Ready moves the agent's work into review, but the result still requires a Worktree Merge before it reaches trunk.
Human review through Web
Web loads the same draft with the Worktree Client and the Worktree collaboration configuration. A review surface can present:
- The current status of the Worktree.
- The rendered page of a Unit in the draft.
- Switching between trunk and draft.
- A merge preview for each Unit.
- Ready, Reopen, Discard, and Merge operations.
A draft in Ready is read-only. Before a person or an agent edits again, Reopen it to Draft. After approval, Merge, then switch back to trunk to inspect the integrated result.
Collaboration SDK boundary
Worktree Service, Endpoint, Client, and Database Adapter belong to Collaboration SDK. AI SDK supplies the composable Unit Runtime, Inspection, API Reference, Execution, and Screenshot capabilities. The application owns the Worktree lifecycle commands, target mapping, identity, and authorization.
See Collaboration extensions for Server composition, lifecycle, and merge constraints.
Footnotes
-
This Worktree is an isolated collaboration draft of document content. It is not a Git worktree or a checkout of source files; edits reach trunk only through the review and merge workflow. ↩
How is this guide?