Skip to content

Commit 44cbfdd

Browse files
Merge origin/main into fix/issue-27579
2 parents e04414c + 6575ce3 commit 44cbfdd

10 files changed

Lines changed: 323 additions & 27 deletions

File tree

‎apps/web/src/keybindings.test.ts‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import {
2121
resolveShortcutCommand,
2222
shouldShowThreadJumpHintsForModifiers,
2323
shortcutLabelForCommand,
24+
terminalNavigationShortcutData,
2425
type ShortcutEventLike,
2526
} from "./keybindings";
2627

@@ -35,6 +36,31 @@ function event(overrides: Partial<ShortcutEventLike> = {}): ShortcutEventLike {
3536
};
3637
}
3738

39+
describe("terminal word navigation", () => {
40+
it.each(["Win32", "Linux x86_64"])(
41+
"leaves Ctrl+Arrow to the terminal encoder on %s",
42+
(platform) => {
43+
for (const key of ["ArrowLeft", "ArrowRight"]) {
44+
assert.equal(terminalNavigationShortcutData(event({ key, ctrlKey: true }), platform), null);
45+
}
46+
},
47+
);
48+
49+
it.each(["Win32", "Linux x86_64", "MacIntel"])(
50+
"preserves Alt+Arrow word navigation on %s",
51+
(platform) => {
52+
assert.equal(
53+
terminalNavigationShortcutData(event({ key: "ArrowLeft", altKey: true }), platform),
54+
"\u001bb",
55+
);
56+
assert.equal(
57+
terminalNavigationShortcutData(event({ key: "ArrowRight", altKey: true }), platform),
58+
"\u001bf",
59+
);
60+
},
61+
);
62+
});
63+
3864
function modShortcut(
3965
key: string,
4066
overrides: Partial<Omit<KeybindingShortcut, "key">> = {},

‎apps/web/src/keybindings.ts‎

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -464,10 +464,6 @@ export function terminalNavigationShortcutData(
464464
return null;
465465
}
466466

467-
if (event.ctrlKey && !event.metaKey && !event.altKey) {
468-
return moveWord;
469-
}
470-
471467
if (event.altKey && !event.metaKey && !event.ctrlKey) {
472468
return moveWord;
473469
}

‎apps/web/src/terminal-links.test.ts‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,36 @@ function createBufferLine(text: string, isWrapped = false): TerminalBufferLineLi
1616
}
1717

1818
describe("extractTerminalLinks", () => {
19+
it.each([
20+
String.raw`C:\dev\ai-test\dir 1`,
21+
String.raw`C:\dev\ai-test\dir 1\test`,
22+
"C:\\",
23+
String.raw`\\server\my share\dir 1`,
24+
"/home/Jane Doe/project",
25+
String.raw`C:\dev\folder (draft)\name!`,
26+
])("keeps the full PowerShell prompt path %s in one link", (path) => {
27+
expect(extractTerminalLinks(`PS ${path}> `)).toEqual([
28+
{ kind: "path", text: path, start: 3, end: 3 + path.length },
29+
]);
30+
});
31+
32+
it("keeps links in a command separate from the PowerShell prompt path", () => {
33+
const path = String.raw`C:\dev\dir 1\test`;
34+
const prompt = `PS ${path}> `;
35+
const url = "https://example.com";
36+
expect(extractTerminalLinks(`${prompt}echo ${url}`)).toEqual([
37+
{ kind: "path", text: path, start: 3, end: 3 + path.length },
38+
{ kind: "url", text: url, start: prompt.length + 5, end: prompt.length + 5 + url.length },
39+
]);
40+
});
41+
42+
it("does not extend ordinary output paths through trailing prose", () => {
43+
const path = String.raw`C:\dev\project`;
44+
expect(extractTerminalLinks(`see ${path} for details`)).toEqual([
45+
{ kind: "path", text: path, start: 4, end: 4 + path.length },
46+
]);
47+
});
48+
1949
it.each([
2050
String.raw`src\index.ts:12`,
2151
String.raw`.\src\index.ts:12:7`,

‎apps/web/src/terminal-links.ts‎

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ export interface WrappedTerminalLinkLine {
2727
}
2828

2929
const URL_PATTERN = /https?:\/\/[^\s"'`<>]+/giu;
30+
// The default PowerShell prompt delimits its unquoted path with `PS ` and `>`.
31+
const POWERSHELL_PROMPT_PATH_PATTERN = /^PS ((?:[A-Za-z]:[\\/]|\\\\|\/)[^>\r\n]*)>/u;
3032
// Bare paths start only at a segment boundary: hover detection runs this on
3133
// every frame, so separator-free output must not be rescanned at every suffix.
3234
const FILE_PATH_PATTERN =
@@ -104,17 +106,25 @@ function collectMatches(
104106
}
105107

106108
export function extractTerminalLinks(line: string): TerminalLinkMatch[] {
109+
const promptPath = POWERSHELL_PROMPT_PATH_PATTERN.exec(line)?.[1];
110+
const promptMatches: TerminalLinkMatch[] = promptPath
111+
? [{ kind: "path", text: promptPath, start: 3, end: 3 + promptPath.length }]
112+
: [];
107113
const requestRoutes = Array.from(line.matchAll(REQUEST_ROUTE_PATTERN), (match) => ({
108114
start: match.index,
109115
end: match.index + match[0].length,
110116
}));
111-
const urlMatches = collectMatches(line, "url", URL_PATTERN, []);
112-
const quotedMatches = collectMatches(line, "path", QUOTED_FILE_PATH_PATTERN, urlMatches);
117+
const urlMatches = collectMatches(line, "url", URL_PATTERN, promptMatches);
118+
const quotedMatches = collectMatches(line, "path", QUOTED_FILE_PATH_PATTERN, [
119+
...promptMatches,
120+
...urlMatches,
121+
]);
113122
const pathMatches = collectMatches(line, "path", FILE_PATH_PATTERN, [
123+
...promptMatches,
114124
...urlMatches,
115125
...quotedMatches,
116126
]);
117-
return [...urlMatches, ...quotedMatches, ...pathMatches]
127+
return [...promptMatches, ...urlMatches, ...quotedMatches, ...pathMatches]
118128
.filter(
119129
(match) => match.kind === "url" || !requestRoutes.some((route) => overlaps(match, route)),
120130
)

‎apps/web/src/terminal/ghostty/core.test.ts‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
import { writeTerminalOutputUpdate } from "../../components/ThreadTerminalDrawer";
1313
import { GHOSTTY_CELL_WIDE, GhosttyTerminalCore, ghosttyCellText } from "./core";
1414
import { loadGhosttyRuntime } from "./runtime";
15+
import { terminalNavigationShortcutData } from "../../keybindings";
1516

1617
vi.mock("./vendor/ghostty-vt.wasm?url", async () => ({
1718
default: (await import("./vendor/ghostty-vt.wasm?inline")).default,
@@ -101,6 +102,26 @@ describe("GhosttyTerminalCore snapshots", () => {
101102
vi.restoreAllMocks();
102103
});
103104

105+
it.each([
106+
{ key: "ArrowLeft", expected: "\x1b[1;5D" },
107+
{ key: "ArrowRight", expected: "\x1b[1;5C" },
108+
])("sends Ctrl+$key as a modified arrow instead of Alt+B/F", async ({ key, expected }) => {
109+
const core = await createCore();
110+
const event = {
111+
code: key,
112+
key,
113+
repeat: false,
114+
shiftKey: false,
115+
ctrlKey: true,
116+
altKey: false,
117+
metaKey: false,
118+
isComposing: false,
119+
getModifierState: () => false,
120+
} as unknown as KeyboardEvent;
121+
122+
expect(terminalNavigationShortcutData(event, "Win32") ?? core.encodeKey(event)).toBe(expected);
123+
});
124+
104125
it.each([
105126
{ code: "", key: "Backspace", expected: "\x7f" },
106127
{ code: "Unidentified", key: "Backspace", expected: "\x7f" },

‎apps/web/src/terminal/ghostty/surface.test.ts‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -606,12 +606,20 @@ describe("terminalLinkAtPositionWithRange", () => {
606606
row("com/reference", true),
607607
row("~/project/file", false),
608608
row("C:\\repo\\file.ts", false),
609+
row("PS C:\\repo\\dir 1", false, true),
610+
row("\\test> ", true),
609611
];
610612

611613
expect(terminalLinkAtPositionWithRange(rows, 0, 8)?.text).toBe("https://example.com/reference");
612614
expect(terminalLinkAtPositionWithRange(rows, 1, 4)?.text).toBe("https://example.com/reference");
613615
expect(terminalLinkAtPositionWithRange(rows, 2, 2)?.text).toBe("~/project/file");
614616
expect(terminalLinkAtPositionWithRange(rows, 3, 4)?.text).toBe("C:\\repo\\file.ts");
617+
expect(terminalLinkAtPositionWithRange(rows, 4, 13)).toEqual({
618+
text: "C:\\repo\\dir 1\\test",
619+
range: { start: { x: 3, y: 4 }, end: { x: 4, y: 5 } },
620+
});
621+
expect(terminalLinkAtPositionWithRange(rows, 5, 2)?.text).toBe("C:\\repo\\dir 1\\test");
622+
expect(terminalLinkAtPositionWithRange(rows, 5, 5)).toBeNull();
615623
expect(terminalLinkAtPositionWithRange(rows, 1, 4)).toEqual({
616624
text: "https://example.com/reference",
617625
range: {

‎packages/client-runtime/src/state/terminalOutput.ts‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -286,9 +286,14 @@ export function readTerminalOutputUpdate(
286286
return { type: "reset", data: terminalOutputText(output), cursor: nextCursor };
287287
}
288288

289-
const appended = output.chunks.filter(
290-
(chunk) => chunk.startOffset + chunk.data.length > cursor.offset,
291-
);
289+
// Appends and compaction preserve offset order, so unread chunks form a suffix.
290+
let firstUnread = output.chunks.length;
291+
while (firstUnread > 0) {
292+
const chunk = output.chunks[firstUnread - 1]!;
293+
if (!(chunk.startOffset + chunk.data.length > cursor.offset)) break;
294+
firstUnread -= 1;
295+
}
296+
const appended = output.chunks.slice(firstUnread);
292297
if (appended.length === 0) {
293298
return { type: "none", cursor: nextCursor };
294299
}

‎packages/client-runtime/src/state/terminalSession.test.ts‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,46 @@ describe("terminal session reducers", () => {
166166
expect(readTerminalOutputUpdate(state.output, update.cursor).type).toBe("none");
167167
});
168168

169+
it("bounds historical chunk inspection when reading new tail output", () => {
170+
let state = applyTerminalAttachStreamEvent(EMPTY_TERMINAL_BUFFER_STATE, {
171+
type: "snapshot",
172+
snapshot: { ...BASE_SNAPSHOT, history: "" },
173+
});
174+
for (let index = 0; index < 512; index += 1) {
175+
state = applyTerminalAttachStreamEvent(state, {
176+
type: "output",
177+
threadId: TARGET.threadId,
178+
terminalId: TARGET.terminalId,
179+
data: "history\r\n",
180+
});
181+
}
182+
const cursor = readTerminalOutputUpdate(state.output, INITIAL_TERMINAL_OUTPUT_CURSOR).cursor;
183+
const next = applyTerminalAttachStreamEvent(state, {
184+
type: "output",
185+
threadId: TARGET.threadId,
186+
terminalId: TARGET.terminalId,
187+
data: "latest\r\n",
188+
});
189+
let historicalChunkReads = 0;
190+
const chunks = next.output.chunks.map((chunk, index) =>
191+
index < state.output.chunks.length
192+
? {
193+
...chunk,
194+
get startOffset() {
195+
historicalChunkReads += 1;
196+
return chunk.startOffset;
197+
},
198+
}
199+
: chunk,
200+
);
201+
202+
expect(readTerminalOutputUpdate({ ...next.output, chunks }, cursor)).toMatchObject({
203+
type: "append",
204+
data: "latest\r\n",
205+
});
206+
expect(historicalChunkReads).toBeLessThanOrEqual(2);
207+
});
208+
169209
it("preserves the byte-limited tail and resets a cursor before a partially trimmed chunk", () => {
170210
const initial = applyTerminalAttachStreamEvent(EMPTY_TERMINAL_BUFFER_STATE, {
171211
type: "snapshot",

‎packages/provider-pi/src/server/status.test.ts‎

Lines changed: 94 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,15 @@ function piProbeSpawner(version: string) {
5252
});
5353
}
5454

55-
const piDiscoverySpawner = (inventory: unknown) =>
55+
/** Fails the test if status runs `pi auth check` without `authCheck` or with other arguments. */
56+
const piDiscoverySpawner = (
57+
inventory: unknown,
58+
options: {
59+
readonly version?: string;
60+
readonly state?: unknown;
61+
readonly authCheck?: string | undefined;
62+
} = {},
63+
) =>
5664
Effect.gen(function* () {
5765
const stdout = yield* Queue.unbounded<Uint8Array, Cause.Done>();
5866
let stdinBuffer = "";
@@ -71,7 +79,9 @@ const piDiscoverySpawner = (inventory: unknown) =>
7179
? inventory
7280
: request.type === "get_commands"
7381
? { commands: [] }
74-
: {};
82+
: request.type === "get_state"
83+
? (options.state ?? {})
84+
: {};
7585
yield* Queue.offer(
7686
stdout,
7787
encoder.encode(
@@ -83,16 +93,23 @@ const piDiscoverySpawner = (inventory: unknown) =>
8393
);
8494
return ChildProcessSpawner.make((command) => {
8595
const args = ChildProcess.isStandardCommand(command) ? command.args : [];
96+
if (args.includes("--version")) {
97+
return Effect.succeed(processHandle({ stdout: `pi ${options.version ?? "0.84.3"}\n` }));
98+
}
99+
if (args[0] === "auth") {
100+
const expected = ["auth", "check", "--provider", "openai", "--json", "--no-refresh"];
101+
return options.authCheck === undefined || args.join(" ") !== expected.join(" ")
102+
? Effect.die(`Unexpected pi ${args.join(" ")}`)
103+
: Effect.succeed(processHandle({ stdout: options.authCheck, exitCode: 1 }));
104+
}
86105
return Effect.succeed(
87-
args.includes("--version")
88-
? processHandle({ stdout: "pi 0.84.3\n" })
89-
: ChildProcessSpawner.makeHandle({
90-
...processHandle({}),
91-
exitCode: Effect.never,
92-
isRunning: Effect.succeed(true),
93-
stdin,
94-
stdout: Stream.fromQueue(stdout),
95-
}),
106+
ChildProcessSpawner.makeHandle({
107+
...processHandle({}),
108+
exitCode: Effect.never,
109+
isRunning: Effect.succeed(true),
110+
stdin,
111+
stdout: Stream.fromQueue(stdout),
112+
}),
96113
);
97114
});
98115
});
@@ -146,6 +163,72 @@ describe("PiProvider", () => {
146163
}).pipe(Effect.provide(NodeServices.layer)),
147164
);
148165

166+
it.effect.each([
167+
{
168+
label: "a pinned model without credentials",
169+
version: "1.4.0",
170+
activeModel: { provider: "openai", id: "gpt-5" },
171+
authCheck: '{"status":"not_ready","provider":"openai","reason":"credentials_not_configured"}',
172+
message: "Pi default (openai/gpt-5) has no credentials configured for openai.",
173+
},
174+
{
175+
label: "a pinned model with a runtime --api-key",
176+
version: "1.4.0",
177+
launchArgs: "--provider openai --model gpt-5 --api-key fake-key",
178+
activeModel: { provider: "openai", id: "gpt-5" },
179+
authCheck: undefined,
180+
message: "Pi default (openai/gpt-5) uses openai, which has no available models in Pi.",
181+
},
182+
{
183+
label: "a pinned model whose provider auth check cannot find",
184+
version: "1.4.0",
185+
activeModel: { provider: "openai", id: "gpt-5" },
186+
authCheck: '{"status":"not_ready","provider":"openai","reason":"provider_not_found"}',
187+
message: "Pi default (openai/gpt-5) uses openai, which has no available models in Pi.",
188+
},
189+
{
190+
label: "a pinned model on Pi without auth check",
191+
version: MINIMUM_PI_VERSION,
192+
activeModel: { provider: "openai", id: "gpt-5" },
193+
authCheck: undefined,
194+
message: "Pi default (openai/gpt-5) uses openai, which has no available models in Pi.",
195+
},
196+
{
197+
label: "an uncatalogued model id of a provider with credentials",
198+
version: "1.4.0",
199+
activeModel: { provider: "anthropic", id: "claude-next" },
200+
authCheck: undefined,
201+
message: undefined,
202+
},
203+
{
204+
label: "an extension model Pi lists as available",
205+
version: "1.4.0",
206+
activeModel: { provider: "router", id: "auto" },
207+
authCheck: undefined,
208+
message: undefined,
209+
},
210+
])("diagnoses Pi's active model readiness for $label", (row) =>
211+
Effect.gen(function* () {
212+
const spawner = yield* piDiscoverySpawner(
213+
{
214+
models: [
215+
{ provider: "anthropic", id: "claude-sonnet-4" },
216+
{ provider: "router", id: "auto" },
217+
],
218+
},
219+
{ version: row.version, state: { model: row.activeModel }, authCheck: row.authCheck },
220+
);
221+
const snapshot = yield* checkPiProviderStatus({
222+
...settings,
223+
launchArgs: row.launchArgs ?? "",
224+
}).pipe(Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner));
225+
assert.equal(snapshot.status, "ready");
226+
assert.equal(snapshot.auth.status, "authenticated");
227+
if (row.message === undefined) assert.isUndefined(snapshot.message);
228+
else assert.include(snapshot.message ?? "", row.message);
229+
}).pipe(Effect.provide(NodeServices.layer)),
230+
);
231+
149232
// regression: #18498 a malformed model inventory must not restore stale Pi models
150233
it.effect.each([
151234
["missing models", {}],

0 commit comments

Comments
 (0)