Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

address review (claude): read the PR body back by branch, cap the rewrite loop, name a scratchpad fallback

[._.]/ Adam Eivy93b2380be146
diff --git a/server/services/cosTaskGenerator.js b/server/services/cosTaskGenerator.js
index 3692cd711f..bbe247b352 100644
--- a/server/services/cosTaskGenerator.js
+++ b/server/services/cosTaskGenerator.js
@@ -218,9 +218,19 @@ export function resolveIssueAuthorFilterBlock(promptTaskType, mode = 'self') {
// Per-forge nouns/commands for the swarm directive. The orchestration shape is
// forge-agnostic (partition → fan-out → serialized merge); only the PR/MR noun
// and the merge command differ between GitHub (`gh`) and GitLab (`glab`).
+//
+// `bodyCmd` deliberately passes NO identifier: both CLIs infer the PR/MR from the
+// checked-out branch, and every fan-out agent runs inside its own
+// `claim/issue-<num>` worktree, so the branch already names the right one. Taking
+// a number here would be actively dangerous — `<num>` means the ISSUE number
+// everywhere else in this block, and an issue number is not a PR/MR number. On
+// GitLab the two are separate iid sequences, so `glab mr view <issue-iid>` tends
+// to resolve to a real but UNRELATED MR, whose body of course lacks this agent's
+// trailer — which would send the agent off to "correct" a stranger's MR. That is
+// the #3489 clobbering failure re-created by the check meant to prevent it.
const SWARM_FORGE = {
- gh: { pr: 'PR', mergeCmd: 'gh pr merge', bodyCmd: 'gh pr view <num> --json body -q .body' },
- glab: { pr: 'MR', mergeCmd: 'glab mr merge', bodyCmd: 'glab mr view <iid> --output json | jq -r .description' }
+ gh: { pr: 'PR', mergeCmd: 'gh pr merge', bodyCmd: 'gh pr view --json body -q .body' },
+ glab: { pr: 'MR', mergeCmd: 'glab mr merge', bodyCmd: 'glab mr view --output json | jq -r .description' }
};
/**
@@ -243,7 +253,9 @@ const SWARM_FORGE = {
* body onto another worker's PR. Namespacing the directory is deterministic where
* "invent a unique filename" is not, and it covers every scratch artifact at once.
* The trailer read-back after create/edit is the second layer, catching a wrong
- * body from any other cause.
+ * body from any other cause — bounded at 2 rewrites plus one re-derive, because
+ * Phase C blocks on every agent finishing, so an agent looping on a body it can
+ * never satisfy would stall the whole batch's merge queue.
*/
export function resolveSwarmBlock(promptTaskType, count) {
const n = Number.isInteger(count) ? count : 0;
@@ -266,9 +278,9 @@ export function resolveSwarmBlock(promptTaskType, count) {
## Phase B — Fan out (one subagent per picked issue)
For EACH picked issue, spawn a subagent that runs the single-issue **Phases 2–6 below** for that one issue — claim (own \`claim/issue-<num>\` worktree + assignee + \`in-progress\` label) → verify → implement → changelog → open the ${pr} → run the review gate ({reviewers}) — **but with NO merge and NO Phase 7 cleanup** (the orchestrator owns those; each agent opens its ${pr} the equivalent of \`--no-merge\`). Because each agent claims through the normal Phase 2 assignee marker + race read-back, two agents can never ship the same issue.
-**Each fan-out agent gets its OWN scratch subdirectory — the scratchpad root is off-limits.** Every agent in this run shares one session scratchpad path, and every agent runs these byte-identical instructions, so left to themselves two agents pick the same obvious filename (\`pr-body.md\`) and silently clobber each other — last writer wins, the command still exits 0, and the wrong text lands on the wrong ${pr}. So: **each fan-out agent writes ALL temp files under \`<scratchpad>/issue-<num>/\` (its own issue number), and NEVER writes to the scratchpad root** (the root stays the orchestrator's). That covers ${pr} body drafts, review notes, diff dumps, test output — every scratch artifact, not just the body file. Create the directory before first use (\`mkdir -p\`). Filenames inside it may be as obvious as you like; the directory is what makes them unique.
+**Each fan-out agent gets its OWN scratch subdirectory — the scratchpad root is off-limits.** Every agent in this run shares one session scratchpad path, and every agent runs these byte-identical instructions, so left to themselves two agents pick the same obvious filename (\`pr-body.md\`) and silently clobber each other — last writer wins, the command still exits 0, and the wrong text lands on the wrong ${pr}. So: **each fan-out agent writes ALL temp files under \`<scratchpad>/issue-<num>/\` (its own issue number), and NEVER writes to the scratchpad root** (the root stays the orchestrator's). That covers ${pr} body drafts, review notes, diff dumps, test output — every scratch artifact, not just the body file. Create the directory before first use (\`mkdir -p\`). Filenames inside it may be as obvious as you like; the directory is what makes them unique. **If your environment gives you no scratchpad path at all**, use \`$(mktemp -d)/issue-<num>\` instead — never a path inside the source repo or inside your worktree, where it would show up as untracked cruft or get swept into a commit.
-**Verify the ${pr} body's issue trailer after create AND after every edit.** The ${pr}-body flow is create-then-edit — the file is written once, then re-read minutes later during the review loop — which is a wide window for a stale or foreign body to land. Belt to the namespacing's braces: immediately after \`create\` and after each body \`edit\`, re-read the published body (\`${bodyCmd}\`) and confirm it contains this agent's own trailer (\`Closes #<num>\` or \`Refs #<num>\` for ITS issue number). If it does not, the body is wrong — rewrite it from this agent's own scratch file and re-verify before continuing. Never assume a zero exit code means the right body was published.
+**Verify the ${pr} body's issue trailer after create AND after every edit.** The ${pr}-body flow is create-then-edit — the file is written once, then re-read minutes later during the review loop — which is a wide window for a stale or foreign body to land. Belt to the namespacing's braces: immediately after \`create\` and after each body \`edit\`, re-read the published body with \`${bodyCmd}\` — **note it takes no number: both CLIs resolve the ${pr} from your checked-out \`claim/issue-<num>\` branch, and passing an ISSUE number where a ${pr} number belongs is how you end up reading (and then "correcting") someone else's ${pr}** — and confirm the body carries this agent's own trailer (\`Closes #<num>\` or \`Refs #<num>\` for ITS issue number). If it does not, rewrite the body from this agent's own scratch file and re-verify. **Cap this at 2 rewrites:** if the trailer still doesn't match, the scratch file itself is suspect — re-derive the body from your own branch's commits/diff for one final attempt, and if that also fails, STOP, leave the ${pr} open, and say so in the result you hand back. Never loop on it: Phase C waits for every agent to finish, so one agent stuck re-publishing blocks the whole batch's merges. And never assume a zero exit code means the right body was published.
## Phase C — Serialize the merges (orchestrator, after all agents finish)
Merge the ready ${pr}s ONE AT A TIME. For each: re-sync onto the latest default branch, gate on **required** CI (one re-run on a flaky required check, then proceed; a real failure or an irreconcilable conflict leaves that ${pr} OPEN and recorded — move to the next), then \`${mergeCmd}\`. After all merges, run Phase 7 cleanup once per merged worktree.
diff --git a/server/services/cosTaskGenerator.test.js b/server/services/cosTaskGenerator.test.js
index f68c14861e..1ed8d566e2 100644
--- a/server/services/cosTaskGenerator.test.js
+++ b/server/services/cosTaskGenerator.test.js
@@ -450,6 +450,10 @@ describe('resolveSwarmBlock', () => {
expect(block).toMatch(/scratchpad root/i);
// The scope is ALL temp files, not just the PR body that surfaced the bug.
expect(block).toMatch(/ALL temp files/i);
+ // CoS agents also run under codex/agy/grok/opencode, which inject no
+ // scratchpad path. Without a named fallback such an agent picks its cwd —
+ // the source repo the prompt otherwise forbids writing to.
+ expect(block).toContain('$(mktemp -d)/issue-<num>');
});
it('instructs each agent to verify its own issue trailer after create and after each edit', () => {
@@ -457,12 +461,32 @@ describe('resolveSwarmBlock', () => {
// a stale/foreign body can land minutes later during the review loop. `gh`
// exits 0 either way, so only a read-back catches it.
const block = resolveSwarmBlock('claim-issue', 3);
- expect(block).toContain('gh pr view <num> --json body -q .body');
expect(block).toContain('Closes #<num>');
expect(block).toContain('Refs #<num>');
expect(block).toMatch(/after each edit|after every edit/i);
});
+ it('reads the PR body back by branch, never by a number that could be the issue number', () => {
+ // `<num>` is the ISSUE number everywhere else in this block, and an issue
+ // number is not a PR number. Passing one to `gh pr view` reads the wrong
+ // object (on GitLab, a real but unrelated MR) — so the agent "corrects" a
+ // stranger's PR body, which is the very bug #3489 is about. Both CLIs infer
+ // the PR/MR from the agent's own claim/issue-<num> branch, so no id is needed.
+ const block = resolveSwarmBlock('claim-issue', 3);
+ expect(block).toContain('gh pr view --json body -q .body');
+ expect(block).not.toContain('gh pr view <num>');
+ expect(block).not.toContain('gh pr view <pr-num>');
+ });
+
+ it('caps the rewrite-and-re-verify loop so one stuck agent cannot stall Phase C', () => {
+ // Phase C waits on every agent, so an unbounded "rewrite from scratch file
+ // and re-verify" blocks the whole batch's merges when the scratch file is
+ // itself the wrong one and republishing can never satisfy the check.
+ const block = resolveSwarmBlock('claim-issue', 3);
+ expect(block).toMatch(/Cap this at 2 rewrites/i);
+ expect(block).toMatch(/Never loop on it/i);
+ });
+
it('returns a glab/MR swarm directive for the gitlab claim body', () => {
const block = resolveSwarmBlock('claim-issue-gitlab', 4);
expect(block).toContain('--swarm=4');
@@ -471,7 +495,11 @@ describe('resolveSwarmBlock', () => {
// The scratch/read-back guidance is forge-agnostic — the MR body read-back
// uses the glab command, not the gh one.
expect(block).toContain('<scratchpad>/issue-<num>/');
- expect(block).toContain('glab mr view <iid> --output json');
+ // Same no-identifier rule as the gh path — and it matters MORE here: issue
+ // iids and MR iids are separate sequences on GitLab, so an issue number
+ // passed to `glab mr view` usually resolves to a real, unrelated MR.
+ expect(block).toContain('glab mr view --output json | jq -r .description');
+ expect(block).not.toContain('glab mr view <iid>');
expect(block).not.toContain('gh pr view');
});