mirror of
https://github.com/binaricat/Netcatty.git
synced 2026-11-16 20:11:08 +00:00
* Preload compact history on first turn after app restart (#753 hedge) Symptom (confirmed on Copilot CLI, originally reported on Codex in #753): after closing and reopening Netcatty, the AI chat UI still shows the prior conversation but the agent responds "this is the beginning of our conversation, no previous records". Earlier context is lost entirely. Root cause: the bridge relied on session/load throwing "not found" to trigger the catch-block fallback that replays compact history. Some ACP agents (Copilot CLI, some Codex builds) silently spawn a new session when handed a stale id instead of erroring. The catch-block never fires → historyReplayFallback stays false → the first turn sends only the latest prompt → agent sees zero context. Fix: when we're creating a new provider process AND telling it to resume an existing session id AND the renderer gave us compact history, preload historyReplayFallback=true as a hedge. If the agent really did reload the session, the replay is ~3KB of redundant context (small waste). If the agent silently started fresh, the replay restores durable constraints + last few raw turns so the first response is coherent. After the first successful streamed turn clears the flag (the round-2 post-stream hook), steady state is back to sending only the latest prompt. Cost is bounded to one replay per app-restart-and-prompt. Test: "replays compact history on the first turn after app restart even when session/load 'succeeds'" — mocks createACPProvider to behave like Copilot CLI (no error thrown, no real resume), asserts the first streamText call carries history+latest (length 2) and the second only latest (length 1). Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> * Fix AI session resume and agent switching * Preserve hidden draft when switching agents --------- Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
161 lines
5.0 KiB
TypeScript
161 lines
5.0 KiB
TypeScript
import test from "node:test";
|
|
import assert from "node:assert/strict";
|
|
|
|
import type {
|
|
AIPanelView,
|
|
AISession,
|
|
} from "../../infrastructure/ai/types.ts";
|
|
import { createEmptyDraft } from "./aiDraftState.ts";
|
|
import {
|
|
pruneInactiveScopedSessions,
|
|
pruneInactiveScopedTransientState,
|
|
} from "./aiScopeCleanup.ts";
|
|
|
|
function createSession(id: string, scope: AISession["scope"], externalSessionId?: string): AISession {
|
|
return {
|
|
id,
|
|
title: id,
|
|
agentId: "catty",
|
|
scope,
|
|
messages: [],
|
|
externalSessionId,
|
|
createdAt: 1,
|
|
updatedAt: 1,
|
|
};
|
|
}
|
|
|
|
test("pruneInactiveScopedTransientState removes closed workspace and terminal scope state", () => {
|
|
const activeSessionIdMap = {
|
|
"terminal:open-terminal": "session-open",
|
|
"terminal:closed-terminal": "session-closed-terminal",
|
|
"workspace:open-workspace": "session-open-workspace",
|
|
"workspace:closed-workspace": "session-closed-workspace",
|
|
};
|
|
const draftsByScope = {
|
|
"terminal:open-terminal": createEmptyDraft("catty"),
|
|
"terminal:closed-terminal": createEmptyDraft("catty"),
|
|
"workspace:open-workspace": createEmptyDraft("catty"),
|
|
"workspace:closed-workspace": createEmptyDraft("catty"),
|
|
};
|
|
const panelViewByScope = {
|
|
"terminal:open-terminal": { mode: "draft" },
|
|
"terminal:closed-terminal": { mode: "session", sessionId: "session-closed-terminal" },
|
|
"workspace:open-workspace": { mode: "draft" },
|
|
"workspace:closed-workspace": { mode: "session", sessionId: "session-closed-workspace" },
|
|
} satisfies Record<string, AIPanelView>;
|
|
|
|
const next = pruneInactiveScopedTransientState(
|
|
activeSessionIdMap,
|
|
draftsByScope,
|
|
panelViewByScope,
|
|
new Set(["open-terminal", "open-workspace"]),
|
|
);
|
|
|
|
assert.deepEqual(next.activeSessionIdMap, {
|
|
"terminal:open-terminal": "session-open",
|
|
"workspace:open-workspace": "session-open-workspace",
|
|
});
|
|
assert.deepEqual(next.draftsByScope, {
|
|
"terminal:open-terminal": draftsByScope["terminal:open-terminal"],
|
|
"workspace:open-workspace": draftsByScope["workspace:open-workspace"],
|
|
});
|
|
assert.deepEqual(next.panelViewByScope, {
|
|
"terminal:open-terminal": panelViewByScope["terminal:open-terminal"],
|
|
"workspace:open-workspace": panelViewByScope["workspace:open-workspace"],
|
|
});
|
|
});
|
|
|
|
test("pruneInactiveScopedSessions preserves restorable terminal ACP ids across reconnects", () => {
|
|
const sessions = [
|
|
createSession("terminal-restorable", {
|
|
type: "terminal",
|
|
targetId: "closed-restorable",
|
|
hostIds: ["host-1"],
|
|
}, "ext-1"),
|
|
createSession("terminal-local", {
|
|
type: "terminal",
|
|
targetId: "closed-local",
|
|
hostIds: ["local-shell"],
|
|
}, "ext-2"),
|
|
createSession("workspace-closed", {
|
|
type: "workspace",
|
|
targetId: "closed-workspace",
|
|
}, "ext-3"),
|
|
createSession("terminal-open", {
|
|
type: "terminal",
|
|
targetId: "open-terminal",
|
|
hostIds: ["host-2"],
|
|
}, "ext-4"),
|
|
];
|
|
|
|
const next = pruneInactiveScopedSessions(
|
|
sessions,
|
|
new Set(["open-terminal"]),
|
|
);
|
|
|
|
assert.deepEqual(next.orphanedSessionIds, [
|
|
"terminal-restorable",
|
|
"terminal-local",
|
|
"workspace-closed",
|
|
]);
|
|
assert.deepEqual(next.sessions, [
|
|
sessions[0],
|
|
sessions[3],
|
|
]);
|
|
});
|
|
|
|
test("pruneInactiveScopedSessions preserves original sessions when orphaned restorable chats are already detached", () => {
|
|
const sessions = [
|
|
createSession("terminal-restorable", {
|
|
type: "terminal",
|
|
targetId: "closed-restorable",
|
|
hostIds: ["host-1"],
|
|
}),
|
|
createSession("terminal-open", {
|
|
type: "terminal",
|
|
targetId: "open-terminal",
|
|
hostIds: ["host-2"],
|
|
}, "ext-4"),
|
|
];
|
|
|
|
const next = pruneInactiveScopedSessions(
|
|
sessions,
|
|
new Set(["open-terminal"]),
|
|
);
|
|
|
|
assert.deepEqual(next.orphanedSessionIds, ["terminal-restorable"]);
|
|
assert.equal(next.sessions, sessions);
|
|
});
|
|
|
|
test("pruneInactiveScopedSessions treats sessions displayed elsewhere as in-use, not orphaned", () => {
|
|
// terminal-restorable's original scope (terminal-closed-A) is gone, but
|
|
// the user resumed it into terminal-open-B from history. The session's
|
|
// externalSessionId must be preserved and it must not appear in the
|
|
// orphaned list, otherwise the active chat loses ACP continuity.
|
|
const resumedElsewhere = createSession("terminal-restorable", {
|
|
type: "terminal",
|
|
targetId: "terminal-closed-A",
|
|
hostIds: ["host-1"],
|
|
}, "ext-resumed");
|
|
|
|
const trulyOrphaned = createSession("terminal-stale", {
|
|
type: "terminal",
|
|
targetId: "terminal-closed-C",
|
|
hostIds: ["host-2"],
|
|
}, "ext-stale");
|
|
|
|
const sessions = [resumedElsewhere, trulyOrphaned];
|
|
|
|
const next = pruneInactiveScopedSessions(
|
|
sessions,
|
|
new Set(["terminal-open-B"]),
|
|
new Set(["terminal-restorable"]),
|
|
);
|
|
|
|
// Only the one not being displayed anywhere should show up as orphaned.
|
|
assert.deepEqual(next.orphanedSessionIds, ["terminal-stale"]);
|
|
// The resumed session must retain its externalSessionId.
|
|
const resumedNext = next.sessions.find((s) => s.id === "terminal-restorable");
|
|
assert.equal(resumedNext?.externalSessionId, "ext-resumed");
|
|
});
|