Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -192,12 +192,13 @@ jobs:
- name: Run cross-platform PTY service tests
if: matrix.settings.run && matrix.settings.pty
working-directory: packages/core
run: bun test test/kilocode/pty-platform.test.ts test/kilocode/pty-smoke.test.ts --timeout 60000
run: bun test test/kilocode/pty-platform.test.ts --timeout 60000

- name: Run cross-platform PTY route tests
- name: Run cross-platform PTY route and TUI smoke tests
if: matrix.settings.run && matrix.settings.pty
working-directory: packages/opencode
run: |
bun test test/kilocode/pty-smoke.test.ts --timeout 60000
bun test test/server/httpapi-pty.test.ts --test-name-pattern "serves Agent Manager regular terminal" --timeout 60000
bun test test/server/httpapi-v2-pty.test.ts --test-name-pattern "serves Agent Manager script terminal" --timeout 60000

Expand Down
91 changes: 0 additions & 91 deletions packages/core/src/kilocode/pty/smoke.ts
Original file line number Diff line number Diff line change
@@ -1,99 +1,8 @@
import { Shell } from "../../shell"
import { KiloPtyTermination } from "./termination"
import { spawn } from "#pty"
import { mkdtemp, rm } from "node:fs/promises"
import os from "node:os"
import path from "node:path"
import { stripVTControlCharacters } from "node:util"

const TIMEOUT = 15_000
const OUTPUT_LIMIT = 20_000
const DIAGNOSTIC =
/(?:TUI worker error\b|(?:^|[\r\n])\s*(?:panic|fatal(?: error)?|unhandled exception|uncaught exception)\b)/i

export async function render(file: string, args: string[] = ["--pure"], timeout = 60_000) {
const dir = await mkdtemp(path.join(os.tmpdir(), "kilo-pty-render-"))
const env: Record<string, string> = {}
for (const key of ["PATH", "SystemRoot", "SYSTEMROOT", "ComSpec", "LANG", "LC_ALL", "LC_CTYPE", "LANGUAGE"]) {
const value = process.env[key]
if (value !== undefined) env[key] = value
}
Object.assign(env, {
TERM: "xterm-256color",
KILO_TERMINAL: "1",
KILO_TEST_HOME: dir,
KILO_NO_DAEMON: "1",
KILO_DISABLE_AUTOUPDATE: "1",
KILO_DISABLE_MODELS_FETCH: "1",
KILO_DISABLE_PROJECT_CONFIG: "1",
KILO_DISABLE_DEFAULT_PLUGINS: "1",
KILO_PURE: "1",
KILO_CONFIG_CONTENT: JSON.stringify({ enabled_providers: [], experimental: { openTelemetry: false } }),
KILO_AUTH_CONTENT: "{}",
HOME: dir,
USERPROFILE: dir,
APPDATA: path.join(dir, "AppData", "Roaming"),
LOCALAPPDATA: path.join(dir, "AppData", "Local"),
XDG_DATA_HOME: path.join(dir, ".local", "share"),
XDG_CACHE_HOME: path.join(dir, ".cache"),
XDG_CONFIG_HOME: path.join(dir, ".config"),
XDG_STATE_HOME: path.join(dir, ".local", "state"),
TMPDIR: dir,
TMP: dir,
TEMP: dir,
})

try {
const proc = spawn(file, args, { name: "xterm-256color", cwd: dir, env, cols: 100, rows: 40 })
const state = { output: "", phase: "prompt" }
const ready = Promise.withResolvers<void>()
const data = proc.onData((chunk) => {
const raw = state.output + chunk
const text = stripVTControlCharacters(raw)
state.output = raw.slice(-OUTPUT_LIMIT)
if (DIAGNOSTIC.test(text)) {
ready.reject(new Error(`TUI diagnostic during ${state.phase}: ${JSON.stringify(state.output)}`))
return
}
const visible = text.replace(/[\r\n]/g, "")
if (state.phase === "prompt" && visible.includes("Ask anything...")) {
state.phase = "palette"
state.output = ""
try {
proc.write("\x10")
} catch (err) {
ready.reject(err)
}
return
}
if (state.phase === "palette" && visible.includes("Commands")) ready.resolve()
})
const exit = proc.onExit((event) => {
ready.reject(
new Error(
`TUI exited during ${state.phase} (code ${event.exitCode}, signal ${event.signal ?? "none"}): ${JSON.stringify(state.output)}`,
),
)
})
const timer = setTimeout(
() =>
ready.reject(
new Error(`TUI timed out during ${state.phase} after ${timeout}ms: ${JSON.stringify(state.output)}`),
),
timeout,
)
try {
await ready.promise
} finally {
clearTimeout(timer)
data.dispose()
exit.dispose()
await KiloPtyTermination.terminate(proc)
}
} finally {
await rm(dir, { recursive: true, force: true })
}
}

export async function smoke() {
const proc = spawn(Shell.preferred(), [], {
Expand Down
53 changes: 0 additions & 53 deletions packages/core/test/kilocode/pty-smoke.test.ts

This file was deleted.

121 changes: 120 additions & 1 deletion packages/opencode/src/kilocode/cli/cmd/pty-smoke.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,123 @@
import { cmd } from "@/cli/cmd/cmd"
import { mkdir, mkdtemp, rm } from "node:fs/promises"
import os from "node:os"
import path from "node:path"
import { stripVTControlCharacters } from "node:util"
import { VtScreen } from "./tui/vt/vt-screen"

const OUTPUT_LIMIT = 20_000
const DIAGNOSTIC =
/(?:TUI worker error\b|(?:^|[\r\n])\s*(?:panic|fatal(?: error)?|unhandled exception|uncaught exception)\b)/i

export async function render(file: string, args: string[] = ["--pure"], timeout = 60_000) {
const { spawn } = await import("@opencode-ai/core/pty/driver")
const { KiloPtyTermination } = await import("@opencode-ai/core/kilocode/pty/termination")
const dir = await mkdtemp(path.join(os.tmpdir(), "kilo-pty-render-"))
const env: Record<string, string> = {}
for (const key of ["PATH", "SystemRoot", "SYSTEMROOT", "ComSpec", "LANG", "LC_ALL", "LC_CTYPE", "LANGUAGE"]) {
const value = process.env[key]
if (value !== undefined) env[key] = value
}
Object.assign(env, {
TERM: "xterm-256color",
KILO_TERMINAL: "1",
KILO_TEST_HOME: dir,
KILO_NO_DAEMON: "1",
KILO_DISABLE_AUTOUPDATE: "1",
KILO_DISABLE_MODELS_FETCH: "1",
KILO_DISABLE_PROJECT_CONFIG: "1",
KILO_DISABLE_DEFAULT_PLUGINS: "1",
KILO_PURE: "1",
KILO_CONFIG_CONTENT: JSON.stringify({ enabled_providers: ["anthropic"], experimental: { openTelemetry: false } }),
KILO_AUTH_CONTENT: "{}",
ANTHROPIC_API_KEY: "dummy",
HOME: dir,
USERPROFILE: dir,
APPDATA: path.join(dir, "AppData", "Roaming"),
LOCALAPPDATA: path.join(dir, "AppData", "Local"),
XDG_DATA_HOME: path.join(dir, ".local", "share"),
XDG_CACHE_HOME: path.join(dir, ".cache"),
XDG_CONFIG_HOME: path.join(dir, ".config"),
XDG_STATE_HOME: path.join(dir, ".local", "state"),
TMPDIR: dir,
TMP: dir,
TEMP: dir,
})

try {
const cwd = path.join(dir, "project")
await mkdir(cwd)
const proc = spawn(file, args, { name: "xterm-256color", cwd, env, cols: 100, rows: 40 })
const screen = new VtScreen(100, 40)
const state = {
output: "",
phase: "screen",
suffix: crypto.randomUUID().slice(0, 8),
prefix: crypto.randomUUID().slice(0, 8),
}
const ready = Promise.withResolvers<void>()
const write = (value: string) => {
try {
proc.write(value)
} catch (err) {
ready.reject(err)
}
}
const probe = () => {
if (state.phase === "edit" || state.phase === "done" || !screen.text().trim()) return
state.phase = "input"
write(`\x05\x15${state.suffix}`)
}
const data = proc.onData((chunk) => {
const raw = state.output + chunk
state.output = raw.slice(-OUTPUT_LIMIT)
if (DIAGNOSTIC.test(stripVTControlCharacters(raw))) {
ready.reject(new Error(`TUI diagnostic during ${state.phase}: ${JSON.stringify(state.output)}`))
return
}
screen.write(chunk)
const text = screen.text()
if (state.phase === "screen") return probe()
if (state.phase === "input" && text.includes(state.suffix)) {
state.phase = "edit"
write(`\x01${state.prefix}`)
return
}
if (state.phase === "edit" && text.includes(state.prefix + state.suffix)) {
state.phase = "done"
ready.resolve()
}
})
const exit = proc.onExit((event) => {
ready.reject(
new Error(
`TUI exited during ${state.phase} (code ${event.exitCode}, signal ${event.signal ?? "none"}): ${JSON.stringify(state.output)}`,
),
)
})
const retry = setInterval(probe, 1_000)
const timer = setTimeout(
() =>
ready.reject(
new Error(
`TUI timed out during ${state.phase} after ${timeout}ms: screen=${JSON.stringify(screen.text())}, output=${JSON.stringify(state.output)}`,
),
),
timeout,
)
try {
await ready.promise
} finally {
clearTimeout(timer)
clearInterval(retry)
data.dispose()
exit.dispose()
await KiloPtyTermination.terminate(proc)
}
} finally {
await rm(dir, { recursive: true, force: true })
}
}

export const PtySmokeCommand = cmd({
command: "__pty-smoke",
Expand All @@ -7,7 +126,7 @@ export const PtySmokeCommand = cmd({
if (process.env.KILO_PTY_SMOKE !== "1") throw new Error("PTY smoke command is release-only")
const { PtySmoke } = await import("@opencode-ai/core/kilocode/pty/smoke")
await PtySmoke.smoke()
await PtySmoke.render(process.execPath)
await render(process.execPath)
console.log("Compiled TUI startup smoke test passed")
},
})
6 changes: 4 additions & 2 deletions packages/opencode/src/kilocode/cli/cmd/tui/vt/vt-screen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ export class VtScreen {
private state: "ground" | "esc" | "csi" | "osc" | "osc-esc" = "ground"
private params = ""
private intermediate = ""
private bell = false

constructor(cols = 80, rows = 24) {
this.cols = Math.max(1, cols)
Expand Down Expand Up @@ -229,8 +230,9 @@ export class VtScreen {
this.intermediate = ""
return
}
if (ch === "]") {
if ("]PX^_".includes(ch)) {
this.state = "osc"
this.bell = ch === "]"
return
}
if (ch === "7") {
Expand Down Expand Up @@ -271,7 +273,7 @@ export class VtScreen {
}

private osc(ch: string, code: number) {
if (code === 0x07) {
if ((this.bell && code === 0x07) || code === 0x18 || code === 0x1a || code === 0x9c) {
this.state = "ground"
return
}
Expand Down
Loading
Loading