worktrunk icon indicating copy to clipboard operation
worktrunk copied to clipboard

Conflict probe: orphaned merge-tree processes and lost results with expensive merge drivers; no way to skip it in picker/JSON

Open mattconcord opened this issue 1 month ago • 2 comments

Setup

  • worktrunk v0.81.0, git with merge-tree --write-tree, macOS.
  • One repository with about 285 linked worktrees.
  • A global attributes file (core.attributesFile) sets *.json merge=mergiraf, a structural merge driver. The repo contains roughly 205 generated JSON catalogues of about 2.2 MB each, so a branch that touches them sends all of them through the driver.

Observed

The conflict probe is git merge-tree --write-tree <target> <branch> (compute_merge_tree_outcome, src/git/repository/integration.rs), run once per worktree. With the default of 32 concurrent commands (WORKTRUNK_MAX_CONCURRENT_COMMANDS), wt list, the wt switch picker and the integration probe in wt remove/wt merge can start many of these at once.

  1. Probes outlive wt. Since #4274 the probe is finish_once_started(), so cancel_background_commands() (src/shell_exec.rs) no longer signals it. When the picker exits, or wt list is interrupted before it finishes, the running git merge-tree processes are reparented to PID 1 and run to completion. With slow merge drivers that is tens of seconds of CPU each, and with 285 worktrees it is a large number of processes.
  2. Their results are discarded. The persistent cache write (sha_cache::put_merge_conflicts in run_merge_tree) happens in the wt process that spawned the probe. After wt exits, nothing reads the orphan's output, so the work is lost. The next invocation recomputes it, so repeated picker use never converges. #4274 mentions this trade-off; the cost is much larger with an expensive driver.
  3. The probe cannot be switched off for the picker or JSON. [list] columns narrows the task plan for the table (required_tasks_for_render), but --format json always plans all_columns and the picker always runs the full task set (comments in src/commands/list/collect/mod.rs and src/commands/picker/mod.rs). The Status column pulls in MergeTreeConflicts, so there is no way to keep the status column and skip the probe.

Numbers

One probe on a branch that touches the catalogues (git merge-tree --write-tree <target> <branch>):

attributes source wall/CPU peak RSS
global merge=mergiraf 42 s CPU 741 MB
GIT_CONFIG_COUNT=1 GIT_CONFIG_KEY_0=core.attributesFile GIT_CONFIG_VALUE_0=/dev/null 2.6 s CPU 603 MB

The result still reports conflicts either way (50 vs 48 conflicted files), so the probe answers its question without the driver. The repository's own .gitattributes still applies under the override. Mergiraf can resolve some conflicts that text merge cannot, so this is only acceptable for a preview, not for real merges.

Asks

a. A config setting and/or flag to skip the conflict probe (or the whole MergeTreeConflicts task), honoured by the picker and by --format json, not only by the table's [list] columns. b. Either persist results from probes that outlive wt (for example have the probe process write its own cache entry), or bound them with a deadline that kills the process and cleans up its .merge_file_* temp files. c. Optionally run preview probes with a wt-controlled attribute source (for example a GIT_CONFIG_* override of core.attributesFile, or git --attr-source), so user-configured expensive merge drivers are skipped for previews only.

Workaround

A wrapper script ahead of wt on PATH that, for the probe-heavy subcommands only, sets WORKTRUNK_MAX_CONCURRENT_COMMANDS=8 and exports the GIT_CONFIG_* override above. Scripts that only need a branch-to-path map avoid wt list and use git worktree list --porcelain instead. This works but is easy to miss, and it disables custom attribute drivers for everything wt runs under it.

Related: #4273 (closed by #4274), #3883.

🤖 Generated with Claude Code

mattconcord avatar Oct 25 '26 00:10 mattconcord

The lifetime and cache concern is supported by the current code: merge-tree is allowed to finish after cancellation, while the persistent cache write happens after the command returns in the wt process. #4274 deliberately chose that lifetime to avoid Git leaving .merge_file_* files when a driver is signalled. I have not reproduced the reported 42-second cost in this CI environment, so those numbers remain the reporter's measurements.

A skip option needs to cover more than MergeTreeConflicts: WouldMergeAdd also calls the same merge-tree probe, and dirty worktrees can run a separate working-tree conflict probe. The current task plan includes these for picker and JSON; the documented JSON contract emits every field. Omitting the probes therefore needs an explicit choice for the conflict and integration fields rather than simply removing one task.

I'm leaving this open as a performance and behavior issue. I haven't opened a fix PR: a deadline that signals Git could restore the temp-file leak from #4273, while skipping probes changes the status/JSON semantics. The design decision is whether to expose an explicitly incomplete preview, persist work outside the exiting wt process, or bound the driver in a way that owns temp-file cleanup. A narrow regression test and fix can follow that choice.

worktrunk-bot avatar Oct 25 '26 00:10 worktrunk-bot

@max-sixty I took the first two points and left the third alone: there is no new switch to skip the conflict probe.

https://github.com/max-sixty/worktrunk/pull/4922 runs git merge-tree under a shell in its own process group, so leaving the picker or interrupting wt list still does not SIGTERM a merge driver (#4273). The shell writes the existing merge-tree-conflicts cache entry before it exits, so the next list can reuse the result. A probe that has not started still does not start. This does not change what ^…± means (#3842).

Jack-mi avatar Nov 26 '26 08:11 Jack-mi