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 (agy): trust unmerged paths over the rebase exit code

Adam Eivy1941b18a1ccc
diff --git a/server/services/appUpdater.js b/server/services/appUpdater.js
index 9b4da30c77..2d6ff7f05a 100644
--- a/server/services/appUpdater.js
+++ b/server/services/appUpdater.js
@@ -50,16 +50,24 @@ async function failWithGitConflict({ app, repoPath, pullResult, stepId, emit, st
steps.push({ step: stepId, success: false, message });
const label = app?.name || repoPath;
const branch = pullResult.branch || 'the default branch';
+ // `app` is what routes the agent's workspace: agentWorkspacePrep resolves the
+ // cwd as `getAppWorkspace(metadata.app)`, which returns the app's PRIMARY
+ // repoPath. A companion repository has no app record of its own, so claiming
+ // the app there would spawn the agent in the wrong checkout — one that isn't
+ // even conflicted. Only the primary repo may carry it; a companion conflict
+ // runs from the default workspace and is steered by the absolute path below.
+ const isPrimaryRepo = app?.repoPath === repoPath;
const task = {
description: `Resolve git conflict in ${label} at ${repoPath}`,
priority: 'HIGH',
- app: app?.id || null,
+ app: (isPrimaryRepo && app?.id) || null,
context: `Updating ${label} failed: the checkout could not be brought onto origin/${branch}. `
+ `A fast-forward and then a rebase with --autostash were both attempted. `
+ `Error: ${error}\n\n`
- + `Resolve the conflict in ${repoPath} (finish or abort any in-progress rebase, reconcile the `
- + `working tree, commit or restore the stashed work), then leave the checkout clean on `
- + `origin/${branch} so the update can be re-run.`,
+ + `The conflicted repository is at ${repoPath}`
+ + (isPrimaryRepo ? '' : ` — a companion repository of ${label}, NOT your default workspace, so work there explicitly`)
+ + `. Finish or abort any in-progress rebase, reconcile the working tree, commit or restore the `
+ + `stashed work, then leave the checkout clean on origin/${branch} so the update can be re-run.`,
position: 'top'
};
const created = await import('./cosTaskStore.js')
diff --git a/server/services/appUpdater.test.js b/server/services/appUpdater.test.js
index eede9471ec..6a1b2220f3 100644
--- a/server/services/appUpdater.test.js
+++ b/server/services/appUpdater.test.js
@@ -122,6 +122,7 @@ describe('managed app updates', () => {
}),
'internal',
);
+ expect(mock.addTask.mock.calls[0][0].context).toContain('Merge conflict in server/index.js');
// The conflict is upstream of every mutating step — nothing may run on a
// checkout that is mid-rebase or carrying conflict markers.
expect(mock.spawn).not.toHaveBeenCalled();
@@ -146,8 +147,15 @@ describe('managed app updates', () => {
expect(result.success).toBe(false);
expect(emit).toHaveBeenCalledWith('git-pull:companion-1', 'error', expect.stringContaining('CoS agent'));
+ // `app` routes the agent's workspace to the app's PRIMARY repoPath, so a
+ // companion conflict must NOT claim it — the agent would land in a checkout
+ // that isn't even conflicted. The absolute path steers it instead.
expect(mock.addTask).toHaveBeenCalledWith(
- expect.objectContaining({ description: `Resolve git conflict in Example App at ${companionRepo}` }),
+ expect.objectContaining({
+ description: `Resolve git conflict in Example App at ${companionRepo}`,
+ app: null,
+ context: expect.stringContaining(companionRepo),
+ }),
'internal',
);
expect(mock.restart).not.toHaveBeenCalled();
diff --git a/server/services/git.js b/server/services/git.js
index 236bf04724..71cc57aa5c 100644
--- a/server/services/git.js
+++ b/server/services/git.js
@@ -833,15 +833,28 @@ export async function pull(dir) {
* 3. On conflict, abort any in-progress rebase and report `conflict: true`
* rather than throwing, so the caller can hand the mess to a CoS agent.
*
- * `rebase --abort` is best-effort: a conflict raised while `--autostash`
- * re-applies the stash happens AFTER the rebase completed, so there is no
- * rebase to abort and the working tree keeps its conflict markers. That is
- * still `conflict: true` — a human or an agent has to finish it.
+ * A zero exit status is NOT enough to call the rebase clean. `--autostash`
+ * re-applies the stash after the rebase finishes, and git only WARNS when that
+ * apply conflicts ("Applying autostash resulted in conflicts") — the pull still
+ * exits 0 with conflict markers in the working tree. So a successful rebase is
+ * confirmed by asking git for unmerged paths, not by its exit code; otherwise
+ * the caller would build and restart on a broken tree. There is no rebase to
+ * abort in that state (it already completed), which is why the abort below sits
+ * on the non-zero path only.
*
* @param {string} dir
* @returns {Promise<{success: boolean, branch: string, output: string, conflict: boolean, rebased?: boolean, error?: string}>}
*/
export async function updateDefaultBranch(dir) {
+ // Git splits a failed pull across both streams — fetch progress and rebase
+ // instructions on stderr, the `CONFLICT (content): …` lines naming the files
+ // on stdout — so an `stderr || stdout` pick would drop exactly the part that
+ // says WHICH files collided from the UI message and the CoS task's context.
+ const bothStreams = (result, fallback) => [result.stdout, result.stderr]
+ .map(stream => (stream || '').trim())
+ .filter(Boolean)
+ .join('\n') || fallback;
+
await execGit(['fetch', 'origin'], dir);
const branch = await getDefaultBranch(dir, { strict: true });
if (!branch) throw new ServerError('could not determine origin default branch', { status: 400, code: 'NO_DEFAULT_BRANCH' });
@@ -860,7 +873,7 @@ export async function updateDefaultBranch(dir) {
branch,
output,
conflict: true,
- error: (checkout.stderr || `could not check out ${branch}`).trim()
+ error: bothStreams(checkout, `could not check out ${branch}`)
};
}
}
@@ -871,7 +884,18 @@ export async function updateDefaultBranch(dir) {
const rebase = await execGit(['pull', '--rebase', '--autostash', 'origin', branch], dir, { ignoreExitCode: true });
output += rebase.stdout + rebase.stderr;
- if (rebase.exitCode === 0) return { success: true, branch, output, conflict: false, rebased: true };
+ if (rebase.exitCode === 0) {
+ const unmerged = await execGit(['diff', '--name-only', '--diff-filter=U'], dir, { ignoreExitCode: true });
+ const conflicted = unmerged.stdout.trim().split('\n').filter(Boolean);
+ if (!conflicted.length) return { success: true, branch, output, conflict: false, rebased: true };
+ return {
+ success: false,
+ branch,
+ output,
+ conflict: true,
+ error: `re-applying the autostashed local changes conflicted in: ${conflicted.join(', ')}`
+ };
+ }
await execGit(['rebase', '--abort'], dir, { ignoreExitCode: true });
return {
@@ -879,7 +903,7 @@ export async function updateDefaultBranch(dir) {
branch,
output,
conflict: true,
- error: (rebase.stderr || rebase.stdout || `rebase onto origin/${branch} failed`).trim()
+ error: bothStreams(rebase, `rebase onto origin/${branch} failed`)
};
}
diff --git a/server/services/git.updateDefaultBranch.test.js b/server/services/git.updateDefaultBranch.test.js
index b50323d8de..8f98d85c36 100644
--- a/server/services/git.updateDefaultBranch.test.js
+++ b/server/services/git.updateDefaultBranch.test.js
@@ -54,22 +54,50 @@ describe('updateDefaultBranch', () => {
expect(result).toMatchObject({ success: true, branch: 'main', conflict: false, rebased: true });
expect(commands()).toContain('pull --rebase --autostash origin main');
+ expect(commands()).toContain('diff --name-only --diff-filter=U');
expect(commands()).not.toContain('rebase --abort');
});
it('reports a conflict and aborts the rebase when --autostash cannot reconcile', async () => {
withGit({
'pull --ff-only origin main': fail('fatal: Not possible to fast-forward, aborting.'),
- 'pull --rebase --autostash origin main': fail('CONFLICT (content): Merge conflict in server/index.js')
+ // Git splits a failed pull across both streams: fetch progress on stderr,
+ // the lines naming the colliding files on stdout.
+ 'pull --rebase --autostash origin main': {
+ stdout: 'CONFLICT (content): Merge conflict in server/index.js',
+ stderr: 'From github.com:example/example\nerror: could not apply 1a2b3c4',
+ exitCode: 1
+ }
});
const result = await updateDefaultBranch('/repo');
expect(result).toMatchObject({ success: false, branch: 'main', conflict: true });
- expect(result.error).toMatch(/CONFLICT/);
+ // The file that collided has to survive into the message — it is the whole
+ // actionable content of the CoS task the caller queues from this.
+ expect(result.error).toContain('CONFLICT (content): Merge conflict in server/index.js');
+ expect(result.error).toContain('could not apply 1a2b3c4');
expect(commands()).toContain('rebase --abort');
});
+ it('reports a conflict when the autostash re-apply collides, which git exits 0 on', async () => {
+ // `--autostash` re-applies the stash AFTER the rebase finishes and git only
+ // WARNS when that apply conflicts, so exit status alone would call this a
+ // clean update and let the caller build and restart on a conflicted tree.
+ withGit({
+ 'pull --ff-only origin main': fail('fatal: Not possible to fast-forward, aborting.'),
+ 'pull --rebase --autostash origin main': ok('Applying autostash resulted in conflicts.'),
+ 'diff --name-only --diff-filter=U': ok('server/index.js\nclient/src/App.jsx\n')
+ });
+
+ const result = await updateDefaultBranch('/repo');
+
+ expect(result).toMatchObject({ success: false, branch: 'main', conflict: true });
+ expect(result.error).toContain('server/index.js, client/src/App.jsx');
+ // The rebase already completed, so there is nothing to abort.
+ expect(commands()).not.toContain('rebase --abort');
+ });
+
it('reports a conflict when local changes block the branch switch', async () => {
withGit({
'checkout main': fail('error: Your local changes to the following files would be overwritten by checkout')
@@ -80,5 +108,6 @@ describe('updateDefaultBranch', () => {
expect(result).toMatchObject({ success: false, branch: 'main', conflict: true });
expect(result.error).toMatch(/local changes/);
expect(commands()).not.toContain('pull --ff-only origin main');
+ expect(commands()).not.toContain('rebase --abort');
});
});