code wiki / _hdl_build / nx_workstream_git.nx
nx_workstream_git.nx source
↩ module page · 56 lines · 2834 B
1// nx_workstream_git.nx -- conflict-free PARALLEL version control for autonomous workstreams (operator: "as
2// we work... parallel workstreams committing in git from claude agents just all crash each other instead
3// of committing to their clear repo"). The fix, from the evidence (git worktrees + branch-per-task +
4// merge-queues / GitLab merge-trains / trunk-based):
5// 1. ISOLATE: every workstream gets a UNIQUE branch AND a UNIQUE worktree -- two agents never share a
6// working dir or branch, so they cannot clobber each other (the crash is structurally impossible).
7// 2. INTEGRATE via a MERGE QUEUE: disjoint file-sets merge in PARALLEL; overlapping ones SERIALIZE behind
8// a one-at-a-time queue with a pre-merge rebase+test gate. license_tier: ORIGINAL Pairs with the
9// Agent-tool worktree isolation; refined by /deep-research wfb7jkc6h (running).
10
11import "nx_syscalls.nx"
12
13const WSG_CRASH: i64 = 0 // same branch/worktree -> would clobber (forbidden)
14const WSG_PARALLEL: i64 = 1 // disjoint file-sets -> safe to merge concurrently
15const WSG_SERIALIZE: i64 = 2 // overlapping file-sets -> queue behind, one at a time
16
17// the crash-prevention INVARIANT: distinct workstreams must have distinct branch AND distinct worktree.
18func wsg_isolation_ok(branch_a: i64, branch_b: i64, worktree_a: i64, worktree_b: i64) -> i64 {
19 if branch_a == branch_b { return 0 } // sharing a branch = the crash
20 if worktree_a == worktree_b { return 0 } // sharing a working dir = the crash
21 return 1
22}
23
24// file-sets modeled as bitmasks (bit = a touched file/module); overlap = bitwise AND.
25func wsg_overlap(mask_a: i64, mask_b: i64) -> i64 { return mask_a & mask_b }
26
27// merge mode for two workstreams.
28func wsg_merge_mode(same_branch: i64, mask_a: i64, mask_b: i64) -> i64 {
29 if same_branch == 1 { return WSG_CRASH }
30 if wsg_overlap(mask_a, mask_b) == 0 { return WSG_PARALLEL }
31 return WSG_SERIALIZE
32}
33
34// merge-queue admission: a candidate may merge NOW iff its file-set is disjoint from ALL in-flight merges
35// (else it waits its turn -> no concurrent breakage). returns 1 = go now, 0 = queue.
36func wsg_can_merge_now(inflight_masks: *i64, n_inflight: i64, candidate_mask: i64) -> i64 {
37 var i: i64 = 0
38 while i < n_inflight {
39 if (inflight_masks[i] & candidate_mask) != 0 { return 0 }
40 i = i + 1
41 }
42 return 1
43}
44
45// pre-merge gate (trunk-based discipline): rebase onto trunk + tests must pass before the merge lands.
46func wsg_merge_gate(rebased_clean: i64, tests_pass: i64) -> i64 {
47 if rebased_clean != 1 { return 0 }
48 if tests_pass != 1 { return 0 }
49 return 1
50}
51
52func wsg_mode_label(m: i64) -> *u8 {
53 if m == WSG_PARALLEL { return "PARALLEL" as *u8 }
54 if m == WSG_SERIALIZE { return "SERIALIZE" as *u8 }
55 return "CRASH-FORBIDDEN" as *u8
56}