mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-29 11:42:17 +00:00
fix(core): publish terminal updates when programs change their titles
This commit is contained in:
parent
922994ac1e
commit
9a238aa080
4 changed files with 153 additions and 10 deletions
|
|
@ -149,14 +149,28 @@ const makeLayer = (options: Options = {}) =>
|
|||
const removing = new Set<Pty.ID>()
|
||||
// Controller activity selects a terminal; observer reads and pane visibility do not.
|
||||
const current = new Map<Session.ID, Pty.ID>()
|
||||
// Titles as of the last listing. Programs retitle terminals inside the daemon, so every listing compares
|
||||
// against these and publishes each change exactly once, however many paths observe it.
|
||||
const titles = new Map<Pty.ID, Pick<Info, "title" | "userTitle">>()
|
||||
|
||||
const list = Effect.fn("PersistentPty.list")(function* (sessionID?: Session.ID) {
|
||||
const response = yield* optionalRequest(daemon, { op: "list" })
|
||||
if (!response) return []
|
||||
if (response.type !== "terminals") return yield* unexpected(response)
|
||||
return response.terminals
|
||||
.map(toInfo)
|
||||
.filter((terminal) => sessionID === undefined || terminal.sessionID === sessionID)
|
||||
if (response && response.type !== "terminals") return yield* unexpected(response)
|
||||
const terminals = response?.terminals.map(toInfo) ?? []
|
||||
const previous = new Map(titles)
|
||||
titles.clear()
|
||||
terminals.forEach((terminal) =>
|
||||
titles.set(terminal.id, { title: terminal.title, userTitle: terminal.userTitle }),
|
||||
)
|
||||
yield* Effect.forEach(
|
||||
terminals.filter((terminal) => {
|
||||
const before = previous.get(terminal.id)
|
||||
return before && (before.title !== terminal.title || before.userTitle !== terminal.userTitle)
|
||||
}),
|
||||
(terminal) => bus.publish(Updated, { sessionID: terminal.sessionID, terminal }),
|
||||
{ discard: true },
|
||||
)
|
||||
return terminals.filter((terminal) => sessionID === undefined || terminal.sessionID === sessionID)
|
||||
})
|
||||
|
||||
const get = Effect.fn("PersistentPty.get")(function* (id: Pty.ID) {
|
||||
|
|
@ -195,6 +209,7 @@ const makeLayer = (options: Options = {}) =>
|
|||
)
|
||||
if (response.type !== "created") return yield* unexpected(response)
|
||||
const terminal = toInfo(response.terminal)
|
||||
titles.set(terminal.id, { title: terminal.title, userTitle: terminal.userTitle })
|
||||
yield* bus.publish(Added, { sessionID, terminal })
|
||||
return terminal
|
||||
})
|
||||
|
|
@ -236,16 +251,15 @@ const makeLayer = (options: Options = {}) =>
|
|||
),
|
||||
)
|
||||
|
||||
// Listing the renamed terminal publishes its update.
|
||||
const rename = Effect.fn("PersistentPty.rename")(function* (id: Pty.ID, title: string | null) {
|
||||
const terminal = yield* get(id)
|
||||
yield* get(id)
|
||||
// Daemons without the feature drop the connection on unknown requests.
|
||||
if (!(yield* daemon.supports("rename").pipe(Effect.mapError(unavailable))))
|
||||
return yield* new UnsupportedError({ feature: "rename" })
|
||||
const response = yield* request(daemon, { op: "rename", id: fromID(id), title })
|
||||
if (response.type !== "ok") return yield* unexpected(response)
|
||||
const renamed = yield* get(id)
|
||||
yield* bus.publish(Updated, { sessionID: terminal.sessionID, terminal: renamed })
|
||||
return renamed
|
||||
return yield* get(id)
|
||||
})
|
||||
|
||||
const snapshot = Effect.fn("PersistentPty.snapshot")(function* (id: Pty.ID) {
|
||||
|
|
@ -321,6 +335,16 @@ const makeLayer = (options: Options = {}) =>
|
|||
)
|
||||
}
|
||||
|
||||
const refreshTitles = () => {
|
||||
runFork(
|
||||
list().pipe(
|
||||
Effect.catchTag("PersistentPty.UnavailableError", (error) =>
|
||||
Effect.logWarning("failed to refresh terminal titles", { error: error.message }),
|
||||
),
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
const removeSession = (sessionID: Session.ID) =>
|
||||
list(sessionID).pipe(
|
||||
Effect.flatMap((terminals) =>
|
||||
|
|
@ -363,6 +387,8 @@ const makeLayer = (options: Options = {}) =>
|
|||
...input,
|
||||
onEvent: (event) => {
|
||||
if (event.type === "exited") removeVisibleExit(id)
|
||||
// Each attachment sees the change; once one listing has recorded it the rest skip it.
|
||||
if (event.type === "title_changed" && titles.get(id)?.title !== event.title) refreshTitles()
|
||||
input.onEvent(event)
|
||||
},
|
||||
})
|
||||
|
|
|
|||
|
|
@ -111,6 +111,9 @@ export const fakeDaemon = Effect.fn(function* (features?: string[]) {
|
|||
const socket = path.join(base, "daemon.sock")
|
||||
const terminals: Array<ReturnType<typeof terminal> & { user_title?: string | null }> = []
|
||||
const failTerminate = new Set<string>()
|
||||
// Titles set by the terminals' programs (OSC 0/2), which a user title hides.
|
||||
const programTitles = new Map<number, string>()
|
||||
const effectiveTitle = (item: (typeof terminals)[number]) => item.user_title ?? programTitles.get(item.id) ?? "test"
|
||||
const ops: string[] = []
|
||||
let nextID = 1
|
||||
yield* listen(socket, (connection, request) => {
|
||||
|
|
@ -136,7 +139,7 @@ export const fakeDaemon = Effect.fn(function* (features?: string[]) {
|
|||
const found = terminals.find((item) => item.id === request.id)
|
||||
if (!found) return { type: "error", message: "terminal not found" }
|
||||
found.user_title = typeof request.title === "string" && request.title !== "" ? request.title : null
|
||||
found.title = found.user_title ?? "test"
|
||||
found.title = effectiveTitle(found)
|
||||
return { type: "ok" }
|
||||
}
|
||||
// Old daemons cannot parse unknown requests and drop the connection.
|
||||
|
|
@ -153,6 +156,13 @@ export const fakeDaemon = Effect.fn(function* (features?: string[]) {
|
|||
return {
|
||||
failTerminate,
|
||||
ops,
|
||||
retitle: (ptyID: string, title: string) => {
|
||||
const id = Number(ptyID.slice("pty_persistent_".length))
|
||||
const found = terminals.find((item) => item.id === id)
|
||||
if (!found) throw new Error(`terminal not found: ${ptyID}`)
|
||||
programTitles.set(id, title)
|
||||
found.title = effectiveTitle(found)
|
||||
},
|
||||
options: {
|
||||
root,
|
||||
handoff: { directory, instanceID: "test", ticket: "ticket", expiresAt: Date.now() + 60_000 },
|
||||
|
|
|
|||
|
|
@ -132,6 +132,50 @@ describe("PersistentPty rename", () => {
|
|||
)
|
||||
})
|
||||
|
||||
describe("PersistentPty program titles", () => {
|
||||
it.live("publishes one update when a program retitles a terminal, and none when a user title hides it", () =>
|
||||
Effect.gen(function* () {
|
||||
const daemon = yield* fakeDaemon(["rename"])
|
||||
yield* Effect.gen(function* () {
|
||||
const pty = yield* PersistentPty.Service
|
||||
const bus = yield* Bus.Service
|
||||
const directory = yield* tmpdirScoped()
|
||||
const sessionID = Session.ID.make("ses_titles")
|
||||
const terminal = yield* pty.create(sessionID, { args: [], cwd: directory.path, title: "test", env: {} })
|
||||
const updates = yield* collectUpdated(bus, 4)
|
||||
|
||||
daemon.retitle(terminal.id, "vim")
|
||||
yield* pty.list()
|
||||
yield* pty.list(sessionID)
|
||||
yield* pty.rename(terminal.id, "build")
|
||||
daemon.retitle(terminal.id, "htop")
|
||||
yield* pty.list()
|
||||
yield* pty.get(terminal.id)
|
||||
yield* pty.rename(terminal.id, null)
|
||||
daemon.retitle(terminal.id, "less")
|
||||
yield* pty.list()
|
||||
|
||||
expect(
|
||||
(yield* Fiber.join(updates)).map((event) => [
|
||||
event.data.sessionID,
|
||||
event.data.terminal.id,
|
||||
event.data.terminal.title,
|
||||
event.data.terminal.userTitle,
|
||||
]),
|
||||
).toEqual([
|
||||
[sessionID, terminal.id, "vim", null],
|
||||
[sessionID, terminal.id, "build", "build"],
|
||||
[sessionID, terminal.id, "htop", null],
|
||||
[sessionID, terminal.id, "less", null],
|
||||
])
|
||||
}).pipe(Effect.provide(layer(daemon)))
|
||||
}),
|
||||
)
|
||||
})
|
||||
|
||||
const collectUpdated = (bus: Bus.Interface, count: number) =>
|
||||
bus.subscribe(Updated).pipe(Stream.take(count), Stream.runCollect, Effect.forkScoped({ startImmediately: true }))
|
||||
|
||||
const nextUpdated = (bus: Bus.Interface) =>
|
||||
bus
|
||||
.subscribe(Updated)
|
||||
|
|
|
|||
|
|
@ -208,6 +208,69 @@ smoke(
|
|||
20_000,
|
||||
)
|
||||
|
||||
smoke(
|
||||
"publishes one update per program title change that the user title does not hide",
|
||||
() =>
|
||||
Effect.gen(function* () {
|
||||
const fixture = yield* testDirectory()
|
||||
const server = yield* ServerProcess.start<never, never>({
|
||||
hostname: "127.0.0.1",
|
||||
port: 0,
|
||||
password: "secret",
|
||||
app: { version: "test-version" },
|
||||
database: { path: fixture.database },
|
||||
pty: { root: fixture.directory },
|
||||
fs: { filewatcher: false },
|
||||
})
|
||||
const base = HttpServer.formatAddress(server.address)
|
||||
const client = OpenCode.make({ baseUrl: base, headers: { authorization: `Basic ${btoa("opencode:secret")}` } })
|
||||
const sessionID = Session.ID.make("ses_persistent_pty_titles")
|
||||
// cat prints each input line back, so input carrying an OSC 2 sequence retitles the terminal.
|
||||
const terminal = yield* Effect.promise(() =>
|
||||
client.experimental.persistentPty.create({
|
||||
sessionID,
|
||||
command: "/bin/sh",
|
||||
args: ["-c", "stty -echo; printf ready; exec cat"],
|
||||
cwd: fixture.root,
|
||||
title: "created",
|
||||
env: {},
|
||||
size: { cols: 40, rows: 10 },
|
||||
}),
|
||||
)
|
||||
yield* waitForText(base, terminal.id, "ready")
|
||||
const events = yield* Effect.acquireRelease(
|
||||
Effect.promise(() => openEventStream(base)),
|
||||
(stream) => Effect.promise(() => stream.close()),
|
||||
)
|
||||
// Two attachments both see each title change; the server still publishes it once.
|
||||
const controller = yield* Effect.acquireRelease(
|
||||
Effect.promise(() => openTerminalSocket(base, terminal.id, "titles-controller")),
|
||||
(connection) => Effect.sync(() => connection.socket.close()),
|
||||
)
|
||||
yield* Effect.acquireRelease(
|
||||
Effect.promise(() => openTerminalSocket(base, terminal.id, "titles-observer", "observer")),
|
||||
(connection) => Effect.sync(() => connection.socket.close()),
|
||||
)
|
||||
const updated = () =>
|
||||
Effect.promise(() => events.next("persistent-pty.updated")).pipe(
|
||||
Effect.map((event) => (isRecord(event.data) ? event.data.terminal : undefined)),
|
||||
)
|
||||
|
||||
controller.socket.send(inputFrame(40, 10, "\u001b]2;vim\u0007first\n"))
|
||||
expect(yield* updated()).toMatchObject({ id: terminal.id, title: "vim", userTitle: null })
|
||||
|
||||
yield* Effect.promise(() => client.experimental.persistentPty.update({ ptyID: terminal.id, title: "build" }))
|
||||
expect(yield* updated()).toMatchObject({ id: terminal.id, title: "build", userTitle: "build" })
|
||||
|
||||
controller.socket.send(inputFrame(40, 10, "\u001b]2;htop\u0007second\n"))
|
||||
yield* waitForText(base, terminal.id, "second")
|
||||
yield* Effect.promise(() => client.experimental.persistentPty.update({ ptyID: terminal.id, title: null }))
|
||||
expect(yield* updated()).toMatchObject({ id: terminal.id, title: "htop", userTitle: null })
|
||||
yield* Effect.promise(() => client.experimental.persistentPty.shutdown())
|
||||
}),
|
||||
20_000,
|
||||
)
|
||||
|
||||
it.live("reports renaming as unsupported on a daemon without the rename feature", () =>
|
||||
Effect.gen(function* () {
|
||||
const fixture = yield* testDirectory()
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue