Skip to content

Commit 09f86ff

Browse files
fix(web): only mark projects reconciled after definitive outcome
selectUnreconciledProjects no longer eagerly adds keys to the reconciled set. The hook marks a project reconciled only after: - A successful import (importedCount + skippedCount returned). - A definitive domain error (project not found, workspace mismatch, scan error, auth error) — these won't resolve without user action. Transient failures (unsupported-server, unexpected defect, interrupted) leave the project eligible for retry on the next render cycle. This fixes the scenario where imports fail against an old server (pre-#5362) and the project is permanently marked done, preventing retry after a server upgrade without a full client remount. Also: console.info now logs for every successful import (even when importedCount is 0) with projectId, workspaceRoot, and skippedCount for E2E observability. Co-authored-by: Lewis Marshall <[email protected]>
1 parent 9e16acd commit 09f86ff

2 files changed

Lines changed: 87 additions & 44 deletions

File tree

‎apps/web/src/hooks/useAgentSessionAutoReconcile.test.ts‎

Lines changed: 46 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,12 @@ import * as Cause from "effect/Cause";
55
import { AsyncResult } from "effect/unstable/reactivity";
66
import { RpcClientError } from "effect/unstable/rpc";
77

8-
import { classifyImportFailure, selectUnreconciledProjects } from "./useAgentSessionAutoReconcile";
8+
import {
9+
classifyImportFailure,
10+
isDefinitiveOutcome,
11+
projectReconcileKey,
12+
selectUnreconciledProjects,
13+
} from "./useAgentSessionAutoReconcile";
914

1015
function makeProject(
1116
environmentId: string,
@@ -34,35 +39,31 @@ describe("selectUnreconciledProjects", () => {
3439
expect(result[1]!.id).toBe("proj-b");
3540
});
3641

37-
it("marks returned projects as reconciled", () => {
42+
it("does not mutate the reconciled set", () => {
3843
const reconciled = new Set<string>();
3944
const projects = [makeProject("env-1", "proj-a")];
4045
selectUnreconciledProjects(projects, reconciled);
41-
expect(reconciled.has("env-1\0proj-a")).toBe(true);
46+
expect(reconciled.size).toBe(0);
4247
});
4348

44-
it("skips already-reconciled projects on subsequent calls", () => {
45-
const reconciled = new Set<string>();
46-
const projects = [makeProject("env-1", "proj-a"), makeProject("env-1", "proj-b")];
47-
selectUnreconciledProjects(projects, reconciled);
48-
const result = selectUnreconciledProjects(projects, reconciled);
49+
it("skips projects already in the reconciled set", () => {
50+
const project = makeProject("env-1", "proj-a");
51+
const reconciled = new Set([projectReconcileKey(project)]);
52+
const result = selectUnreconciledProjects([project], reconciled);
4953
expect(result).toHaveLength(0);
5054
});
5155

52-
it("returns only newly added projects", () => {
53-
const reconciled = new Set<string>();
54-
const initial = [makeProject("env-1", "proj-a")];
55-
selectUnreconciledProjects(initial, reconciled);
56-
57-
const withNew = [...initial, makeProject("env-1", "proj-b")];
58-
const result = selectUnreconciledProjects(withNew, reconciled);
56+
it("returns only projects not yet reconciled", () => {
57+
const projA = makeProject("env-1", "proj-a");
58+
const projB = makeProject("env-1", "proj-b");
59+
const reconciled = new Set([projectReconcileKey(projA)]);
60+
const result = selectUnreconciledProjects([projA, projB], reconciled);
5961
expect(result).toHaveLength(1);
6062
expect(result[0]!.id).toBe("proj-b");
6163
});
6264

6365
it("returns empty array when projects list is empty", () => {
64-
const reconciled = new Set<string>();
65-
const result = selectUnreconciledProjects([], reconciled);
66+
const result = selectUnreconciledProjects([], new Set());
6667
expect(result).toHaveLength(0);
6768
});
6869

@@ -71,23 +72,21 @@ describe("selectUnreconciledProjects", () => {
7172
const projects = [makeProject("env-1", "proj-a"), makeProject("env-2", "proj-a")];
7273
const result = selectUnreconciledProjects(projects, reconciled);
7374
expect(result).toHaveLength(2);
74-
75-
const second = selectUnreconciledProjects(projects, reconciled);
76-
expect(second).toHaveLength(0);
7775
});
7876

79-
it("handles a project removed then re-added (stays reconciled)", () => {
77+
it("allows retry when a project is not marked reconciled after failure", () => {
8078
const reconciled = new Set<string>();
81-
const projects = [makeProject("env-1", "proj-a"), makeProject("env-1", "proj-b")];
82-
selectUnreconciledProjects(projects, reconciled);
79+
const projects = [makeProject("env-1", "proj-a")];
8380

84-
const removed = [makeProject("env-1", "proj-b")];
85-
const afterRemove = selectUnreconciledProjects(removed, reconciled);
86-
expect(afterRemove).toHaveLength(0);
81+
const first = selectUnreconciledProjects(projects, reconciled);
82+
expect(first).toHaveLength(1);
8783

88-
const reAdded = [makeProject("env-1", "proj-a"), makeProject("env-1", "proj-b")];
89-
const afterReAdd = selectUnreconciledProjects(reAdded, reconciled);
90-
expect(afterReAdd).toHaveLength(0);
84+
const second = selectUnreconciledProjects(projects, reconciled);
85+
expect(second).toHaveLength(1);
86+
87+
reconciled.add(projectReconcileKey(projects[0]!));
88+
const third = selectUnreconciledProjects(projects, reconciled);
89+
expect(third).toHaveLength(0);
9190
});
9291
});
9392

@@ -151,3 +150,21 @@ describe("classifyImportFailure", () => {
151150
);
152151
});
153152
});
153+
154+
describe("isDefinitiveOutcome", () => {
155+
it("treats expected failures as definitive (no retry)", () => {
156+
expect(isDefinitiveOutcome("expected")).toBe(true);
157+
});
158+
159+
it("treats unsupported-server as non-definitive (retry after upgrade)", () => {
160+
expect(isDefinitiveOutcome("unsupported-server")).toBe(false);
161+
});
162+
163+
it("treats unexpected failures as non-definitive (retry)", () => {
164+
expect(isDefinitiveOutcome("unexpected")).toBe(false);
165+
});
166+
167+
it("treats interrupted as non-definitive (retry)", () => {
168+
expect(isDefinitiveOutcome("interrupted")).toBe(false);
169+
});
170+
});

‎apps/web/src/hooks/useAgentSessionAutoReconcile.ts‎

Lines changed: 41 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,26 @@ import { agentSessionImport } from "../state/agentSessions";
1212
import { useAllEnvironmentShellsBootstrapped, useProjects } from "../state/entities";
1313
import { useAtomCommand } from "../state/use-atom-command";
1414

15+
/** Stable key for a project in a specific environment. */
16+
export function projectReconcileKey(project: {
17+
readonly environmentId: string;
18+
readonly id: string;
19+
}): string {
20+
return `${project.environmentId}\0${project.id}`;
21+
}
22+
1523
/**
16-
* Return the subset of `projects` that have not yet been reconciled according
17-
* to `reconciled`. Each returned project is added to `reconciled` as a side
18-
* effect so the caller can track which projects still need work.
24+
* Return the subset of `projects` not yet in `reconciled`.
25+
* Does **not** mark them reconciled — the caller must do so after a
26+
* definitive outcome so transient failures allow retry.
1927
*/
2028
export function selectUnreconciledProjects(
2129
projects: ReadonlyArray<EnvironmentProject>,
22-
reconciled: Set<string>,
30+
reconciled: ReadonlySet<string>,
2331
): ReadonlyArray<EnvironmentProject> {
2432
const pending: EnvironmentProject[] = [];
2533
for (const project of projects) {
26-
const key = `${project.environmentId}\0${project.id}`;
27-
if (reconciled.has(key)) continue;
28-
reconciled.add(key);
34+
if (reconciled.has(projectReconcileKey(project))) continue;
2935
pending.push(project);
3036
}
3137
return pending;
@@ -72,10 +78,19 @@ export function classifyImportFailure(
7278
return "unexpected";
7379
}
7480

81+
/**
82+
* Whether the failure kind is definitive enough to mark the project reconciled
83+
* and not retry. Transient failures (unsupported-server, unexpected, interrupted)
84+
* leave the project eligible for retry after a server upgrade or remount.
85+
*/
86+
export function isDefinitiveOutcome(kind: ReturnType<typeof classifyImportFailure>): boolean {
87+
return kind === "expected";
88+
}
89+
7590
/**
7691
* Automatically imports external agent sessions (Claude Code, Codex) for every
7792
* known project once the environment shells are bootstrapped. Runs once per
78-
* project per mount cycle.
93+
* project per mount cycle for successful imports; retries on transient failures.
7994
*
8095
* Reuses the existing `agentSessions.import` RPC, which is idempotent: threads
8196
* whose `import:` id already exists are skipped by the server, and the
@@ -103,6 +118,8 @@ export function useAgentSessionAutoReconcile(): void {
103118
for (const project of pending) {
104119
if (cancelled) return;
105120

121+
const key = projectReconcileKey(project);
122+
106123
if (unsupportedServersRef.current.has(project.environmentId)) continue;
107124

108125
const result = await importSessions({
@@ -116,11 +133,11 @@ export function useAgentSessionAutoReconcile(): void {
116133
if (cancelled) return;
117134

118135
if (result._tag === "Success") {
119-
if (result.value.importedCount > 0) {
120-
console.info(
121-
`[auto-reconcile] Imported ${result.value.importedCount} agent session(s) for project "${project.title}" (${project.id})`,
122-
);
123-
}
136+
reconciledRef.current.add(key);
137+
console.info(
138+
`[auto-reconcile] project "${project.title}" (${project.id}, root "${project.workspaceRoot}"): ` +
139+
`imported ${result.value.importedCount}, skipped ${result.value.skippedCount}`,
140+
);
124141
continue;
125142
}
126143

@@ -130,14 +147,23 @@ export function useAgentSessionAutoReconcile(): void {
130147

131148
if (kind === "unsupported-server") {
132149
unsupportedServersRef.current.add(project.environmentId);
150+
for (const key of reconciledRef.current) {
151+
if (key.startsWith(`${project.environmentId}\0`)) {
152+
reconciledRef.current.delete(key);
153+
}
154+
}
133155
console.warn(
134156
`[auto-reconcile] Server for environment "${project.environmentId}" does not support agentSessions.import. ` +
135-
`Auto-reconcile of external agent sessions requires a server build that includes PR #5362 ` +
136-
`(shipped after [email protected]). Upgrade the server to enable automatic session import.`,
157+
`Auto-reconcile requires a server build that includes PR #5362 (shipped after [email protected]). ` +
158+
`Upgrade the server to enable automatic session import.`,
137159
);
138160
continue;
139161
}
140162

163+
if (isDefinitiveOutcome(kind)) {
164+
reconciledRef.current.add(key);
165+
}
166+
141167
const squashed = squashAtomCommandFailure(result);
142168
const errorMessage =
143169
squashed instanceof Error ? squashed.message : JSON.stringify(squashed);

0 commit comments

Comments
 (0)