fix: address review batch on scanner accuracy, wizard targeting, and a11y

Scanner: cwd is read from every complete line in the transcript prefix
(Claude often opens with cwd-less records); alreadyImported compares with
normalizeProjectPathForComparison against one shell snapshot instead of
exact-match lookups.

Wizard: remote paths target the machine the user just connected instead of
the local primary — agent probes, terminal, keybindings, and import all
follow it; per-driver cards pick the most usable instance; probes still
pending show Checking instead of an Install button; import retries skip
already-landed paths and the chooser resets on target switch; an unmount
during an in-flight terminal open now reaps the created PTY; copy falls
back when the Clipboard API is missing (plain-HTTP LAN).

FirstRunGate: freshness is judged only against primary-environment state,
so a saved remote's project can't masquerade as the cwd bootstrap.

UI consistency: connection cards expose aria-pressed and a focus ring,
ready status uses success tokens, the back control is the Button primitive.

Co-Authored-By: Claude Fable 5 <[email protected]>
This commit is contained in:
Theo BrowneandClaude Fable 5 committed 2026-08-14 17:27:15 -07:00
1 parent 59338a38aa
commit 5988af8179
5 files changed
+219 -84

No files matched your search

@@ -1,10 +1,9 @@
import * as NodeServices from "@effect/platform-node/NodeServices";
import { describe, expect, it } from "@effect/vitest";
import { type OrchestrationProject, ProjectId } from "@t3tools/contracts";
import { type OrchestrationProjectShell, ProjectId } from "@t3tools/contracts";
import * as Effect from "effect/Effect";
import * as FileSystem from "effect/FileSystem";
import * as Layer from "effect/Layer";
import * as Option from "effect/Option";
import * as Path from "effect/Path";
import * as ServerConfig from "../config.ts";
@@ -12,7 +11,7 @@ import * as ProjectionSnapshotQuery from "../orchestration/Services/ProjectionSn
import * as ServerSettings from "../serverSettings.ts";
import * as AgentSessionScanner from "./AgentSessionScanner.ts";
const makeProject = (workspaceRoot: string): OrchestrationProject => ({
const makeProjectShell = (workspaceRoot: string): OrchestrationProjectShell => ({
id: ProjectId.make("project-1"),
title: "Imported",
workspaceRoot,
@@ -20,24 +19,24 @@ const makeProject = (workspaceRoot: string): OrchestrationProject => ({
scripts: [],
createdAt: "2026-01-01T00:00:00.000Z",
updatedAt: "2026-01-01T00:00:00.000Z",
deletedAt: null,
});
/** Only `getActiveProjectByWorkspaceRoot` is exercised; the rest must not be called. */
/** Only `getShellSnapshot` is exercised; the rest must not be called. */
const makeProjectionSnapshotQueryLayer = (importedWorkspaceRoots: ReadonlyArray<string>) =>
Layer.succeed(ProjectionSnapshotQuery.ProjectionSnapshotQuery, {
getCommandReadModel: () => Effect.die("unused"),
getSnapshot: () => Effect.die("unused"),
getShellSnapshot: () => Effect.die("unused"),
getShellSnapshot: () =>
Effect.succeed({
snapshotSequence: 0,
projects: importedWorkspaceRoots.map((workspaceRoot) => makeProjectShell(workspaceRoot)),
threads: [],
updatedAt: "2026-01-01T00:00:00.000Z",
}),
getArchivedShellSnapshot: () => Effect.die("unused"),
getSnapshotSequence: () => Effect.die("unused"),
getCounts: () => Effect.die("unused"),
getActiveProjectByWorkspaceRoot: (workspaceRoot) =>
Effect.succeed(
importedWorkspaceRoots.includes(workspaceRoot)
? Option.some(makeProject(workspaceRoot))
: Option.none(),
),
getActiveProjectByWorkspaceRoot: () => Effect.die("unused"),
getProjectShellById: () => Effect.die("unused"),
getFirstActiveThreadIdByProjectId: () => Effect.die("unused"),
getThreadCheckpointContext: () => Effect.die("unused"),
@@ -347,6 +346,32 @@ it.layer(NodeServices.layer)("AgentSessionScanner", (it) => {
}),
);
it.effect("finds the cwd on a later line when the first records carry none", () =>
Effect.gen(function* () {
const path = yield* Path.Path;
const claudeHomePath = yield* makeTempDir("t3code-claude-home-");
const codexHomePath = yield* makeTempDir("t3code-codex-home-");
const workspace = yield* makeTempDir("t3code-workspace-");
// Claude transcripts often open with records that have no cwd.
const contents = [
JSON.stringify({ type: "file-history-snapshot", messageId: "m1" }),
JSON.stringify({ type: "queue-operation", operation: "enqueue" }),
JSON.stringify({ type: "user", cwd: workspace, sessionId: "s1" }),
"",
].join("\n");
yield* writeTranscript({
filePath: path.join(claudeHomePath, "projects", "-slug", "a.jsonl"),
contents,
mtimeMs: Date.parse("2026-01-01T00:00:00.000Z"),
});
const result = yield* runScan({ claudeHomePath, codexHomePath });
expect(result.candidates.map((candidate) => candidate.path)).toEqual([workspace]);
}),
);
it.effect("skips malformed transcripts without failing the scan", () =>
Effect.gen(function* () {
const path = yield* Path.Path;
+41 -28
View File
@@ -29,6 +29,7 @@ import * as Option from "effect/Option";
import * as Path from "effect/Path";
import { HostProcessEnvironment, HostProcessPlatform } from "@t3tools/shared/hostProcess";
import { normalizeProjectPathForComparison } from "@t3tools/shared/path";
import * as ServerConfig from "../config.ts";
import * as ProjectionSnapshotQuery from "../orchestration/Services/ProjectionSnapshotQuery.ts";
@@ -155,32 +156,40 @@ export const make = Effect.gen(function* () {
fileSystem.stat(target).pipe(Effect.map(Option.some), Effect.orElseSucceed(Option.none));
/**
* Read the head of a transcript and return its first complete line. Returns
* `null` when the file is unreadable or its first line exceeds the prefix.
* Read the head of a transcript and return its complete lines. Returns an
* empty list when the file is unreadable; a trailing partial line (cut by
* the prefix cap) is dropped rather than parsed as truncated JSON.
*/
const readFirstLine = Effect.fn("AgentSessionScanner.readFirstLine")(function* (
const readHeadLines = Effect.fn("AgentSessionScanner.readHeadLines")(function* (
filePath: string,
): Effect.fn.Return<string | null> {
): Effect.fn.Return<ReadonlyArray<string>> {
const prefix = yield* Effect.scoped(
fileSystem
.open(filePath, { flag: "r" })
.pipe(Effect.flatMap((file) => file.readAlloc(TRANSCRIPT_PREFIX_BYTES))),
).pipe(Effect.orElseSucceed(Option.none<Uint8Array>));
if (Option.isNone(prefix)) return null;
if (Option.isNone(prefix)) return [];
const text = decoder.decode(prefix.value);
const newlineIndex = text.indexOf("\n");
if (newlineIndex === -1) {
// Either a single-line file smaller than the prefix, or a first record
// too large to trust as complete JSON.
return prefix.value.length < TRANSCRIPT_PREFIX_BYTES ? text : null;
const lines = text.split("\n");
// A prefix-capped read may end mid-line; only a file smaller than the cap
// is guaranteed to have a complete final line.
if (prefix.value.length >= TRANSCRIPT_PREFIX_BYTES) {
lines.pop();
}
return text.slice(0, newlineIndex);
return lines;
});
// Transcripts often open with records that carry no cwd (Claude writes
// file-history snapshots and queue operations first), so scan every
// complete line in the prefix for the first one that names a directory.
const readCwd = Effect.fn("AgentSessionScanner.readCwd")(function* (filePath: string) {
const line = yield* readFirstLine(filePath);
return line === null ? null : extractCwd(line.trim());
const lines = yield* readHeadLines(filePath);
for (const line of lines) {
const cwd = extractCwd(line.trim());
if (cwd !== null) return cwd;
}
return null;
});
const latestMtimeMs = Effect.fn("AgentSessionScanner.latestMtimeMs")(function* (
@@ -404,25 +413,29 @@ export const make = Effect.gen(function* () {
: Math.max(existing.lastActiveAtMs, candidate.lastActiveAtMs);
}
// One snapshot read, compared with the same normalization the
// project.create invariant uses, so a root that differs only by case or
// separators is still recognized as imported.
const shellSnapshot = yield* projectionSnapshotQuery
.getShellSnapshot()
.pipe(
Effect.mapError(
(cause) => new AgentSessionScanError({ operation: "read-projects", cause }),
),
);
const importedRoots = new Set(
shellSnapshot.projects.map((project) =>
normalizeProjectPathForComparison(project.workspaceRoot),
),
);
const candidates: Array<AgentSessionProjectCandidate> = [];
for (const [key, entry] of merged.entries()) {
// Projects may have been created under either the recorded spelling or
// the resolved realpath (e.g. a symlinked home) — check both.
const lookupPaths = key === entry.path ? [entry.path] : [entry.path, key];
let alreadyImported = false;
for (const lookupPath of lookupPaths) {
const existingProject = yield* projectionSnapshotQuery
.getActiveProjectByWorkspaceRoot(lookupPath)
.pipe(
Effect.mapError(
(cause) => new AgentSessionScanError({ operation: "read-projects", cause }),
),
);
if (Option.isSome(existingProject)) {
alreadyImported = true;
break;
}
}
const alreadyImported =
importedRoots.has(normalizeProjectPathForComparison(entry.path)) ||
importedRoots.has(normalizeProjectPathForComparison(key));
candidates.push({
path: entry.path,
title: path.basename(entry.path) || entry.path,
@@ -57,10 +57,19 @@ export function FirstRunGate({
// own cwd auto-bootstrap: web mode creates a project + thread from cwd at
// startup (`autoBootstrapProjectFromCwd` defaults on there), so "no
// projects at all" would mean `npx t3` users never see the wizard. Any
// other project, or more than one thread, is real user state.
// other project, more than one thread, or state in a non-primary
// environment is real user state — the aggregate hooks span every
// environment, and a saved remote's project must never read as "the
// bootstrap project" just because its root string matches the primary cwd.
const serverCwd = serverConfig?.cwd ?? null;
const primaryEnvironmentId = serverConfig?.environment.environmentId ?? null;
const workspaceFresh =
projects.every((project) => project.workspaceRoot === serverCwd) && threads.length <= 1;
projects.every(
(project) =>
project.environmentId === primaryEnvironmentId && project.workspaceRoot === serverCwd,
) &&
threads.length <= 1 &&
threads.every((thread) => thread.environmentId === primaryEnvironmentId);
useEffect(() => {
if (decision !== "pending" || !hydrated) return;
@@ -27,7 +27,9 @@ import { agentSessionScan } from "../../state/agentSessions";
import { useEnvironments, usePrimaryEnvironment } from "../../state/environments";
import { useEnvironmentQuery } from "../../state/query";
import { projectEnvironment } from "../../state/projects";
import { primaryServerKeybindingsAtom, serverEnvironment } from "../../state/server";
import { DEFAULT_RESOLVED_KEYBINDINGS } from "@t3tools/shared/keybindings";
import { serverEnvironment } from "../../state/server";
import { terminalEnvironment } from "../../state/terminal";
import { useAtomCommand } from "../../state/use-atom-command";
import { connectPairing } from "../../connection/onboarding";
@@ -51,20 +53,33 @@ import { cn } from "../../lib/utils";
type WizardStep = "connection" | "connect-machines" | "pair-direct" | "agents" | "import";
type ConnectionMode = "local" | "connect" | "direct";
/**
* The machine the agent and import steps run against: the primary environment
* when it's connected, otherwise the first connected environment (the remote
* paths on web/hosted have no primary). Deliberately not a persisted "primary
* machine" concept — just whichever machine is reachable right now, labeled
* inline on each step.
* The machine the agent and import steps run against. Local mode targets the
* primary environment; the remote modes prefer the machine the user just
* connected (the most recently added connected non-primary environment), so
* probing and import happen where their code lives rather than on the local
* server that happens to serve the app. Deliberately not a persisted
* "primary machine" concept — just whichever machine fits the chosen path
* right now, labeled inline on each step.
*/
function useOnboardingTargetEnvironment() {
function useOnboardingTargetEnvironment(mode: ConnectionMode) {
const { environments } = useEnvironments();
const primaryEnvironment = usePrimaryEnvironment();
const connectedRemotes = environments.filter(
(environment) =>
environment.connection.phase === "connected" &&
environment.entry.target._tag !== "PrimaryConnectionTarget",
);
if (mode !== "local" && connectedRemotes.length > 0) {
// Registry order is append-order, so the last entry is the newest link.
return connectedRemotes[connectedRemotes.length - 1] ?? null;
}
if (primaryEnvironment !== null && primaryEnvironment.connection.phase === "connected") {
return primaryEnvironment;
}
return environments.find((environment) => environment.connection.phase === "connected") ?? null;
return connectedRemotes[0] ?? null;
}
const AGENT_ONBOARDING_THREAD_ID = ThreadId.make("onboarding-agent-setup");
@@ -85,6 +100,7 @@ export function WelcomeWizard({
}) {
const completeOnboarding = useCompleteOnboarding();
const [step, setStep] = useState<WizardStep>("connection");
const [mode, setMode] = useState<ConnectionMode>("local");
const finish = useCallback(() => {
completeOnboarding();
onDone();
@@ -105,9 +121,18 @@ export function WelcomeWizard({
{step === "connection" ? (
<ConnectionStep
localAvailable={localAvailable}
onLocal={() => setStep("agents")}
onConnect={() => setStep("connect-machines")}
onDirect={() => setStep("pair-direct")}
onLocal={() => {
setMode("local");
setStep("agents");
}}
onConnect={() => {
setMode("connect");
setStep("connect-machines");
}}
onDirect={() => {
setMode("direct");
setStep("pair-direct");
}}
/>
) : step === "connect-machines" ? (
<ConnectMachinesStep
@@ -117,9 +142,13 @@ export function WelcomeWizard({
) : step === "pair-direct" ? (
<PairDirectStep onBack={() => setStep("connection")} onPaired={() => setStep("agents")} />
) : step === "agents" ? (
<AgentsStep onContinue={() => setStep("import")} onSkip={() => setStep("import")} />
<AgentsStep
mode={mode}
onContinue={() => setStep("import")}
onSkip={() => setStep("import")}
/>
) : (
<ImportStep onDone={finish} />
<ImportStep mode={mode} onDone={finish} />
)}
</section>
</div>
@@ -208,9 +237,11 @@ function ConnectionCard({
return (
<button
type="button"
aria-pressed={selected}
onClick={onSelect}
className={cn(
"flex flex-col rounded-xl border p-4 text-left transition-colors",
"flex cursor-pointer flex-col rounded-xl border p-4 text-left transition-colors",
"outline-none focus-visible:ring-2 focus-visible:ring-ring",
selected
? "border-primary bg-primary/5 ring-1 ring-primary/25"
: "border-border/70 bg-background/40 hover:border-border",
@@ -450,13 +481,15 @@ const AGENT_LOGIN_COMMANDS: Record<string, string> = {
* path), and the terminal also handles the interactive login that follows.
*/
function AgentsStep({
mode,
onContinue,
onSkip,
}: {
readonly mode: ConnectionMode;
readonly onContinue: () => void;
readonly onSkip: () => void;
}) {
const targetEnvironment = useOnboardingTargetEnvironment();
const targetEnvironment = useOnboardingTargetEnvironment(mode);
if (targetEnvironment === null) {
return (
<StepShell title="Set up your agents" description="Waiting for an environment connection…">
@@ -501,10 +534,22 @@ function ConnectedAgentsStep({
void refreshProviders({ environmentId, input: {} });
}, [environmentId, refreshProviders]);
// A driver can have several instances; the card should reflect the most
// usable one (ready beats needs-login beats installed beats present), not
// whichever happened to be listed first.
const byDriver = useMemo(() => {
const usability = (provider: ServerProvider): number => {
if (!provider.enabled) return 0;
if (!provider.installed) return 1;
if (provider.auth.status !== "authenticated") return 2;
return 3;
};
const map = new Map<string, ServerProvider>();
for (const provider of providers ?? []) {
if (!map.has(provider.driver)) map.set(provider.driver, provider);
const existing = map.get(provider.driver);
if (existing === undefined || usability(provider) > usability(existing)) {
map.set(provider.driver, provider);
}
}
return map;
}, [providers]);
@@ -588,6 +633,9 @@ function AgentCard({
const Icon = meta?.icon;
const displayName = driver === "claudeAgent" ? "Claude Code" : (meta?.label ?? driver);
const summary = getProviderSummary(provider);
// `undefined` means the probe hasn't reported yet — offering Install
// against unknown state would be wrong either way it resolves.
const pending = provider === undefined;
// A provider disabled in settings is neither ready nor installable from
// here; the summary already reads "Disabled" and the card offers no action.
const disabled = provider !== undefined && !provider.enabled;
@@ -599,7 +647,7 @@ function AgentCard({
<div
className={cn(
"rounded-xl border p-4",
ready ? "border-emerald-500/40" : terminalOpen ? "border-primary" : "border-border/70",
ready ? "border-success/30" : terminalOpen ? "border-primary" : "border-border/70",
)}
>
<div className="flex items-center gap-2.5">
@@ -612,10 +660,12 @@ function AgentCard({
</p>
<div className="mt-3">
{ready ? (
<span className="inline-flex items-center gap-1 text-xs font-semibold text-emerald-500">
<span className="inline-flex items-center gap-1 text-xs font-semibold text-success-foreground">
<CheckIcon className="size-3.5" />
Ready
</span>
) : pending ? (
<span className="text-xs text-muted-foreground">Checking…</span>
) : disabled ? (
<span className="text-xs text-muted-foreground">Enable in Settings</span>
) : (
@@ -651,8 +701,9 @@ function AgentInstallTerminal({
readonly installed: boolean;
readonly onClose: () => void;
}) {
const keybindings = useAtomValue(primaryServerKeybindingsAtom);
const serverConfig = useAtomValue(serverEnvironment.configValueAtom(environmentId));
// Keybindings follow the machine the terminal runs on, not the primary.
const keybindings = serverConfig?.keybindings ?? DEFAULT_RESOLVED_KEYBINDINGS;
// Same terminal typography preference the thread drawer honors.
const [advancedTypography] = useLocalStorage(
TYPOGRAPHY_ADVANCED_STORAGE_KEY,
@@ -701,7 +752,16 @@ function AgentInstallTerminal({
preparedRef.current = false;
return;
}
if (cancelled || command.length === 0) return;
// Cleanup may have run while the open was in flight — its close was a
// no-op against a not-yet-created session, so reap the PTY here.
if (cancelled) {
void closeTerminal({
environmentId,
input: { threadId: AGENT_ONBOARDING_THREAD_ID, terminalId },
});
return;
}
if (command.length === 0) return;
// Pre-type without the trailing carriage return; the user submits.
// The terminal id is unique to this mount, so this session has never
// been written to before.
@@ -778,8 +838,14 @@ function AgentInstallTerminal({
* Choose expands a checklist including older ones. Projects only — thread
* history import is a follow-up.
*/
function ImportStep({ onDone }: { readonly onDone: () => void }) {
const targetEnvironment = useOnboardingTargetEnvironment();
function ImportStep({
mode,
onDone,
}: {
readonly mode: ConnectionMode;
readonly onDone: () => void;
}) {
const targetEnvironment = useOnboardingTargetEnvironment(mode);
const environmentId = targetEnvironment?.environmentId ?? null;
const machineLabel = targetEnvironment?.label ?? "this machine";
const providers = useAtomValue(
@@ -793,6 +859,16 @@ function ImportStep({ onDone }: { readonly onDone: () => void }) {
const [deselected, setDeselected] = useState<ReadonlySet<string>>(new Set());
const [isImporting, setIsImporting] = useState(false);
const [importError, setImportError] = useState("");
// Paths that already imported this session, so a retry after a partial
// failure skips them instead of tripping the duplicate-root invariant.
const importedPathsRef = useRef(new Set<string>());
// Candidate paths are per-environment; a target switch would otherwise
// leave stale entries in the deselection set (and stale success records).
useEffect(() => {
setDeselected(new Set());
importedPathsRef.current = new Set();
}, [environmentId]);
const candidates = useMemo(
() => (scan.data?.candidates ?? []).filter((candidate) => !candidate.alreadyImported),
@@ -819,10 +895,16 @@ function ImportStep({ onDone }: { readonly onDone: () => void }) {
const defaultModelSelection = resolveDefaultProviderModelSelection(providers ?? [], null);
// Interrupted imports are neither failures nor successes — the command was
// superseded or the environment dropped — but they still didn't land, so
// they must not read as "imported everything".
let imported = 0;
// they must not read as "imported everything". Retries skip paths that
// already landed this session (re-creating them would only trip the
// duplicate-root invariant and read as a failure).
let imported =
importedPathsRef.current.size > 0
? selection.filter((candidate) => importedPathsRef.current.has(candidate.path)).length
: 0;
let failed = 0;
for (const candidate of selection) {
if (importedPathsRef.current.has(candidate.path)) continue;
const result = await createProject({
environmentId,
input: {
@@ -835,6 +917,7 @@ function ImportStep({ onDone }: { readonly onDone: () => void }) {
});
if (result._tag === "Success") {
imported += 1;
importedPathsRef.current.add(candidate.path);
} else if (!isAtomCommandInterrupted(result)) {
failed += 1;
}
@@ -981,14 +1064,10 @@ function StepShell({
return (
<>
{onBack ? (
<button
type="button"
onClick={onBack}
className="mt-3 inline-flex items-center gap-1 text-xs text-muted-foreground hover:text-foreground"
>
<Button className="-ml-2 mt-3" onClick={onBack} size="xs" variant="ghost-muted">
<ChevronLeftIcon className="size-3.5" />
Back
</button>
</Button>
) : null}
<h1
className={cn(
@@ -1016,6 +1095,23 @@ function CommandBlock({
readonly prominent?: boolean;
}) {
const [copied, setCopied] = useState(false);
// The Clipboard API only exists in secure contexts; the wizard's Direct
// path explicitly supports plain-HTTP LAN servers, so fall back to the
// selection-based copy there.
const copyCommand = () => {
if (navigator.clipboard !== undefined) {
void navigator.clipboard.writeText(command);
} else {
const scratch = document.createElement("textarea");
scratch.value = command;
document.body.append(scratch);
scratch.select();
document.execCommand("copy");
scratch.remove();
}
setCopied(true);
window.setTimeout(() => setCopied(false), 1500);
};
return (
<div
className={cn(
@@ -1028,16 +1124,7 @@ function CommandBlock({
<span className="mr-2 text-muted-foreground">$</span>
{command}
</span>
<Button
size="icon-xs"
variant="ghost"
aria-label="Copy command"
onClick={() => {
void navigator.clipboard.writeText(command);
setCopied(true);
window.setTimeout(() => setCopied(false), 1500);
}}
>
<Button size="icon-xs" variant="ghost" aria-label="Copy command" onClick={copyCommand}>
{copied ? <CheckIcon className="size-3.5" /> : <CopyIcon className="size-3.5" />}
</Button>
</div>
+3 -2
View File
@@ -31,5 +31,6 @@ have already run and offers them as projects. The default imports projects
active in the last 30 days; **Choose** lists everything found. Imports create
projects only — thread history import is coming later.
Every step after the connection choice can be skipped. Projects can always be
added later from the command palette.
The agent and import steps can be skipped, and the remote paths offer Back to
choose a different connection. Projects can always be added later from the
command palette.