f98395f000
* fix(agentctl): start per-repo workspace trackers in passthrough mode CLI passthrough mode starts the agent CLI via the PTY-based InteractiveRunner instead of agentctl's startAgent / startOneShot, so the per-repo workspace trackers created for multi-repo task roots were never started. The bare root tracker exits immediately because it has no git index, leaving polling completely idle — file edits made by the agent never produced workspace-stream notifications and the Files panel stayed stale. Move the tracker startup to instance.Manager.CreateInstance via a new StartAllWorkspaceTrackers helper so polling fires for every repo regardless of whether the agent runs via ACP or passthrough. Fixes #982 Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com> * fix(lifecycle): serialize workspace poll-mode pushes to prevent slow-stuck mode When a session subscribes and then immediately focuses (the normal page-load sequence), the workspace poll aggregator used to spawn two independent goroutines — one pushing slow, one pushing fast — whose HTTP arrival order at agentctl was undefined. With the slow request landing after the fast request the tracker ended up in slow mode (~30s polling) while the aggregator's lastPushed claimed fast, and the suppress-duplicate guard then blocked any future focus event from reconciling the divergence. Multi-repo passthrough sessions hit this every page load: the Files panel saw ~30s-stale state even with the per-repo trackers running. Replace the fire-and-forget goroutine per push with a single in-flight pusher per workspace plus a last-write-wins pending slot — concurrent callers all converge on the same pusher, which drains the queue in order so the final queued mode is what reaches the wire last. Regression test in manager_subscription_test.go simulates the race with an httptest server that adds artificial latency on slow pushes; the test fails on the old parallel-goroutine implementation and passes on the serialized one. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com> * fix(agentctl): address PR review feedback for passthrough tracker fix - Goroutine leak: extract stopWorkspaceTrackers() and call it unconditionally at the top of Manager.Stop(). Previously the early return when status==Stopped skipped tracker teardown in passthrough mode (where Start() never runs), leaking 2 goroutines per per-repo tracker per stopped instance. stopShellAndProcesses delegates to the same helper. - Trim multi-line doc comments to one-liners per CLAUDE.md (the StartAllWorkspaceTrackers docstring and the CreateInstance call-site comment). - Replace the fixed time.Sleep in the race regression test with a poll on len(calls), exiting as soon as the pusher has drained, plus a brief settle window after to keep the test sensitive to the late racing slow push the fix guards against. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com> * fix(web): refresh expanded subtrees on polling-detected file changes The agentctl workspace tracker emits a generic `refresh` event with empty path when its poll cycle detects file changes (it knows something changed but not which file). The Files panel previously only refreshed the root folder on these events, so files added under an expanded subdir never became visible — the merge preserved the stale subtree, and the user had to manually collapse + re-expand to see new files. This is the user-visible symptom of #982 (Files panel does not refresh after agent makes changes in CLI passthrough mode): the backend fixes in this PR get the events flowing, but the frontend needs to act on them correctly. On a `refresh` event we now refresh every currently-expanded folder in addition to the root; for multi-repo task roots we scope to the affected repo's branch using `repository_name`. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com> * fix: address iteration-2 review feedback - Trim doc + inline comment blocks per CLAUDE.md (manager.go, file-browser-hooks.ts, workspace-files.ts). - Replace the hand-rolled fmtSscanf/numErr port parser with strconv.Atoi in the race regression test. - Assert on slowCalls in the race test to lock in the queue-collapse guarantee (at most one slow push lands). Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com> * fix: address iteration-3 review feedback - Remove redundant stopWorkspaceTrackers call from stopShellAndProcesses (it's already invoked at the top of Manager.Stop), and drop the now- misleading "stopping workspace tracker" debug log from the regular shutdown path. - Trim three multi-line comment blocks in manager_subscription.go to one-liners per CLAUDE.md (workspacePushTarget, pushAsync, pushLoop). Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com> * fix: trim multi-line comment block on pendingPush field * fix: trim test docstring on race regression test * fix(web): preserve sibling-repo subtrees on scoped refresh The setTree reducer ran patchNode(updated) at the root, which re-matched folderUpdates.has("") and overwrote the already-merged root children with the raw depth-1 server response — wiping the loaded children of every top-level repo that wasn't part of the refresh scope. Recurse over updated.children instead of updated itself: the root has already been merged in the block above, and each top-level repo either gets its targeted update (if folderUpdates.has(path)) or is left alone (preserving its loaded subtree). Includes a regression test that fails on the old code: a refresh scoped to "thm" used to discard "kandev"'s loaded README.md child. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com> * fix(web): trim three more comment blocks to one-liners * fix: trim five more comment blocks to one-liners --------- Co-authored-by: Kandev Agent <agent@kandev.dev> Co-authored-by: Claude Opus 4.7 <noreply@anthropic.com>
217 lines
7.2 KiB
TypeScript
217 lines
7.2 KiB
TypeScript
import { describe, it, expect, vi, beforeEach } from "vitest";
|
|
import type { FileTreeNode } from "@/lib/types/backend";
|
|
|
|
const requestFileTreeMock = vi.fn();
|
|
const getWebSocketClientMock = vi.fn(() => ({}) as unknown);
|
|
|
|
vi.mock("@/lib/ws/connection", () => ({
|
|
getWebSocketClient: () => getWebSocketClientMock(),
|
|
}));
|
|
vi.mock("@/lib/ws/workspace-files", () => ({
|
|
requestFileTree: (...args: unknown[]) => requestFileTreeMock(...args),
|
|
requestFileContent: vi.fn(),
|
|
searchWorkspaceFiles: vi.fn(),
|
|
}));
|
|
|
|
type ApplyFileChanges = (typeof import("./file-browser-hooks"))["applyFileChanges"];
|
|
let applyFileChanges: ApplyFileChanges;
|
|
|
|
const SESSION_ID = "sess";
|
|
const REFRESH_OP = "refresh";
|
|
const THM_OLD = "thm/old.txt";
|
|
const THM_NEW = "thm/new.txt";
|
|
|
|
beforeEach(async () => {
|
|
vi.resetModules();
|
|
requestFileTreeMock.mockReset();
|
|
getWebSocketClientMock.mockReset().mockReturnValue({});
|
|
({ applyFileChanges } = await import("./file-browser-hooks"));
|
|
});
|
|
|
|
function makeTree(): FileTreeNode {
|
|
return {
|
|
name: "",
|
|
path: "",
|
|
is_dir: true,
|
|
size: 0,
|
|
children: [
|
|
{
|
|
name: "thm",
|
|
path: "thm",
|
|
is_dir: true,
|
|
size: 0,
|
|
children: [{ name: "old.txt", path: THM_OLD, is_dir: false, size: 0 }],
|
|
},
|
|
{ name: "kandev", path: "kandev", is_dir: true, size: 0, children: [] },
|
|
],
|
|
};
|
|
}
|
|
|
|
function mockEmptyTree() {
|
|
// depth=1 responses omit `children` for placeholder nodes (omitempty).
|
|
requestFileTreeMock.mockImplementation((_client: unknown, _sid: string, folder: string) =>
|
|
Promise.resolve({ root: { name: folder, path: folder, is_dir: true } }),
|
|
);
|
|
}
|
|
|
|
function client() {
|
|
return {} as ReturnType<typeof import("@/lib/ws/connection").getWebSocketClient>;
|
|
}
|
|
|
|
// Regression for #982: refresh events must expand all affected-repo folders, not just root.
|
|
describe("applyFileChanges — refresh operation expands to all expanded folders", () => {
|
|
it("refreshes every expanded folder under the affected repo on a refresh event", async () => {
|
|
mockEmptyTree();
|
|
applyFileChanges({
|
|
client: client(),
|
|
sessionId: SESSION_ID,
|
|
expandedPaths: new Set(["thm", "thm/rooms", "kandev"]),
|
|
changes: [{ path: "", operation: REFRESH_OP, repository_name: "thm" }],
|
|
setTree: vi.fn(),
|
|
setLoadState: vi.fn(),
|
|
});
|
|
await new Promise<void>((r) => setTimeout(r, 0));
|
|
// Root + every expanded path under "thm" (not "kandev" — different repo).
|
|
expect(requestFileTreeMock.mock.calls.map((c) => c[2]).sort()).toEqual([
|
|
"",
|
|
"thm",
|
|
"thm/rooms",
|
|
]);
|
|
});
|
|
|
|
it("refreshes every expanded folder when repository_name is missing", async () => {
|
|
mockEmptyTree();
|
|
applyFileChanges({
|
|
client: client(),
|
|
sessionId: SESSION_ID,
|
|
expandedPaths: new Set(["src", "src/components"]),
|
|
changes: [{ path: "", operation: REFRESH_OP }],
|
|
setTree: vi.fn(),
|
|
setLoadState: vi.fn(),
|
|
});
|
|
await new Promise<void>((r) => setTimeout(r, 0));
|
|
expect(requestFileTreeMock.mock.calls.map((c) => c[2]).sort()).toEqual([
|
|
"",
|
|
"src",
|
|
"src/components",
|
|
]);
|
|
});
|
|
|
|
it("preserves the targeted-path behavior for specific operations", async () => {
|
|
mockEmptyTree();
|
|
applyFileChanges({
|
|
client: client(),
|
|
sessionId: SESSION_ID,
|
|
expandedPaths: new Set(["thm", "kandev"]),
|
|
changes: [{ path: THM_NEW, operation: "create" }],
|
|
setTree: vi.fn(),
|
|
setLoadState: vi.fn(),
|
|
});
|
|
await new Promise<void>((r) => setTimeout(r, 0));
|
|
// create event: parent "thm" is expanded, path itself is not — only "thm" refreshed.
|
|
expect(requestFileTreeMock.mock.calls.map((c) => c[2]).sort()).toEqual(["thm"]);
|
|
});
|
|
|
|
it("merges fresh children into the expanded subtree", async () => {
|
|
requestFileTreeMock.mockImplementation((_c: unknown, _s: string, folder: string) =>
|
|
folder === "thm"
|
|
? Promise.resolve({ root: thmChildrenAfter() })
|
|
: Promise.resolve({ root: rootChildrenAfter() }),
|
|
);
|
|
const setTree = vi.fn();
|
|
applyFileChanges({
|
|
client: client(),
|
|
sessionId: SESSION_ID,
|
|
expandedPaths: new Set(["thm"]),
|
|
changes: [{ path: "", operation: REFRESH_OP, repository_name: "thm" }],
|
|
setTree,
|
|
setLoadState: vi.fn(),
|
|
});
|
|
await new Promise<void>((r) => setTimeout(r, 0));
|
|
expect(setTree).toHaveBeenCalled();
|
|
// Apply setTree's reducer to the prev tree to inspect the merged shape.
|
|
const reducer = setTree.mock.calls[0][0] as (prev: FileTreeNode) => FileTreeNode;
|
|
const next = reducer(makeTree());
|
|
const thmNode = next.children?.find((c) => c.path === "thm");
|
|
expect(thmNode?.children?.map((c) => c.path).sort()).toEqual([THM_NEW, THM_OLD]);
|
|
});
|
|
});
|
|
|
|
// Regression (#982): refresh scoped to one repo must not wipe sibling repos' loaded subtrees.
|
|
describe("applyFileChanges — cross-repo subtree preservation", () => {
|
|
it("preserves kandev's loaded children when refresh is scoped to thm", async () => {
|
|
requestFileTreeMock.mockImplementation((_c: unknown, _s: string, folder: string) => {
|
|
if (folder === "thm") {
|
|
return Promise.resolve({ root: thmChildrenAfter() });
|
|
}
|
|
return Promise.resolve({ root: rootChildrenAfter() });
|
|
});
|
|
const prevTree: FileTreeNode = {
|
|
name: "",
|
|
path: "",
|
|
is_dir: true,
|
|
size: 0,
|
|
children: [
|
|
{
|
|
name: "kandev",
|
|
path: "kandev",
|
|
is_dir: true,
|
|
size: 0,
|
|
children: [{ name: "README.md", path: "kandev/README.md", is_dir: false, size: 0 }],
|
|
},
|
|
{
|
|
name: "thm",
|
|
path: "thm",
|
|
is_dir: true,
|
|
size: 0,
|
|
children: [{ name: "old.txt", path: THM_OLD, is_dir: false, size: 0 }],
|
|
},
|
|
],
|
|
};
|
|
const setTree = vi.fn();
|
|
applyFileChanges({
|
|
client: client(),
|
|
sessionId: SESSION_ID,
|
|
expandedPaths: new Set(["thm"]),
|
|
changes: [{ path: "", operation: REFRESH_OP, repository_name: "thm" }],
|
|
setTree,
|
|
setLoadState: vi.fn(),
|
|
});
|
|
await new Promise<void>((r) => setTimeout(r, 0));
|
|
const reducer = setTree.mock.calls[0][0] as (prev: FileTreeNode) => FileTreeNode;
|
|
const next = reducer(prevTree);
|
|
const kandevNode = next.children?.find((c) => c.path === "kandev");
|
|
// kandev's existing child must survive a refresh scoped to a different repo.
|
|
expect(kandevNode?.children?.map((c) => c.path)).toEqual(["kandev/README.md"]);
|
|
const thmNode = next.children?.find((c) => c.path === "thm");
|
|
expect(thmNode?.children?.map((c) => c.path).sort()).toEqual([THM_NEW, THM_OLD]);
|
|
});
|
|
});
|
|
|
|
function thmChildrenAfter(): FileTreeNode {
|
|
return {
|
|
name: "thm",
|
|
path: "thm",
|
|
is_dir: true,
|
|
size: 0,
|
|
children: [
|
|
{ name: "old.txt", path: THM_OLD, is_dir: false, size: 0 },
|
|
{ name: "new.txt", path: THM_NEW, is_dir: false, size: 0 },
|
|
],
|
|
};
|
|
}
|
|
|
|
function rootChildrenAfter(): FileTreeNode {
|
|
// depth=1 backend responses omit `children` (omitempty) — mergeTreeNodes uses absence to preserve loaded subtrees.
|
|
return {
|
|
name: "",
|
|
path: "",
|
|
is_dir: true,
|
|
size: 0,
|
|
children: [
|
|
{ name: "thm", path: "thm", is_dir: true, size: 0 },
|
|
{ name: "kandev", path: "kandev", is_dir: true, size: 0 },
|
|
],
|
|
};
|
|
}
|