Repository navigation
refactor(client): share the persistent pty protocol #60323
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
1868f94
refactor(client): share the persistent pty protocol
jlongster fa6ff45
fix(client): decode only the size from attached terminal info
jlongster 7570ccb
test(client): cover optional persistent pty message fields
jlongster 4d79950
fix(client): decode only the persistent pty message fields clients read
jlongster File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,113 @@ | ||
| import { describe, expect, test } from "bun:test" | ||
| import { Option } from "effect" | ||
| import { decodePersistentPtyMessage, persistentPtyFrame } from "../src/solid" | ||
|
|
||
| const info = { | ||
| id: "pty_1", | ||
| title: "zsh", | ||
| command: "/bin/zsh", | ||
| args: [], | ||
| cwd: "/repo", | ||
| status: "running", | ||
| pid: 42, | ||
| sessionID: "ses_1", | ||
| foregroundProcess: null, | ||
| size: { cols: 80, rows: 24 }, | ||
| output: { head: 0, tail: 128 }, | ||
| } | ||
|
|
||
| const decode = (value: object) => Option.getOrUndefined(decodePersistentPtyMessage(JSON.stringify(value))) | ||
|
|
||
| describe("decodePersistentPtyMessage", () => { | ||
| test("decodes every message the server sends, keeping only the fields clients read", () => { | ||
| const replay = { requestedOffset: 128, availableOffset: 0, endOffset: 256, truncated: false } | ||
|
|
||
| expect( | ||
| [ | ||
| { type: "attached", inputProtocol: 1, info, role: "controller", generation: 3, replay }, | ||
| { type: "replay_complete", endOffset: 256 }, | ||
| { type: "resized", cols: 100, rows: 30, generation: 4, checkpoint: "G2M=" }, | ||
| { type: "controller_changed", attachmentID: "attachment-1", generation: 5 }, | ||
| { type: "exited", exitCode: 0, finalOffset: 512 }, | ||
| { type: "title_changed", title: "vim" }, | ||
| { type: "foreground_process_changed", process: "vim" }, | ||
| { type: "foreground_process_changed", process: null }, | ||
| ].map(decode), | ||
| ).toEqual([ | ||
| { type: "attached", inputProtocol: 1, info: { size: info.size }, role: "controller" }, | ||
| { type: "replay_complete" }, | ||
| { type: "resized", cols: 100, rows: 30, checkpoint: "G2M=" }, | ||
| { type: "controller_changed", attachmentID: "attachment-1" }, | ||
| { type: "exited", exitCode: 0 }, | ||
| { type: "title_changed", title: "vim" }, | ||
| { type: "foreground_process_changed", process: "vim" }, | ||
| { type: "foreground_process_changed", process: null }, | ||
| ]) | ||
| }) | ||
|
|
||
| test("reads only the size from attached terminal info, whatever else the server version sends", () => { | ||
| const attached = (terminal: object) => ({ type: "attached", inputProtocol: 1, info: terminal, role: "controller" }) | ||
|
|
||
| expect(decode(attached(info))).toEqual(attached({ size: info.size })) | ||
| expect(decode(attached({ ...info, userTitle: "build", sessionID: "dummy" }))).toEqual(attached({ size: info.size })) | ||
| expect(decode(attached({ id: "pty_1" }))).toBeUndefined() | ||
| }) | ||
|
|
||
| test("decodes minimal messages and the fields JSON.stringify drops", () => { | ||
| expect(decode({ type: "attached", info: { size: info.size } })).toEqual({ | ||
| type: "attached", | ||
| info: { size: info.size }, | ||
| }) | ||
| expect(decode({ type: "replay_complete" })).toEqual({ type: "replay_complete" }) | ||
| expect(decode({ type: "controller_changed", generation: 5 })).toEqual({ type: "controller_changed" }) | ||
| expect(decode({ type: "controller_changed", attachmentID: null })).toEqual({ | ||
| type: "controller_changed", | ||
| attachmentID: null, | ||
| }) | ||
| expect(decode({ type: "exited", finalOffset: 512 })).toEqual({ type: "exited" }) | ||
| expect(decode({ type: "exited", exitCode: null })).toEqual({ type: "exited", exitCode: null }) | ||
| }) | ||
|
|
||
| test("ignores fields it does not know", () => { | ||
| expect(decode({ type: "title_changed", title: "vim", extra: true })).toEqual({ | ||
| type: "title_changed", | ||
| title: "vim", | ||
| }) | ||
| }) | ||
|
|
||
| test("returns None for unknown message types", () => { | ||
| expect(Option.isNone(decodePersistentPtyMessage(JSON.stringify({ type: "future_message", value: 1 })))).toBe(true) | ||
| }) | ||
|
|
||
| test("returns None for malformed input", () => { | ||
| const inputs = [ | ||
| "", | ||
| "not json", | ||
| "null", | ||
| "42", | ||
| JSON.stringify({ endOffset: 1 }), | ||
| JSON.stringify({ type: "resized", cols: "100", rows: 30, generation: 4, checkpoint: "" }), | ||
| JSON.stringify({ type: "resized", cols: 100, rows: 30, generation: 4 }), | ||
| JSON.stringify({ type: "attached", inputProtocol: 1, role: "controller", generation: 1 }), | ||
| JSON.stringify({ type: "foreground_process_changed" }), | ||
| ] | ||
|
|
||
| expect(inputs.map((input) => Option.isNone(decodePersistentPtyMessage(input)))).toEqual(inputs.map(() => true)) | ||
| }) | ||
| }) | ||
|
|
||
| describe("persistentPtyFrame", () => { | ||
| test("encodes a size-only interaction frame", () => { | ||
| expect(Array.from(persistentPtyFrame({ cols: 300, rows: 24 }))).toEqual([0, 1, 44, 0, 24]) | ||
| }) | ||
|
|
||
| test("encodes an input frame with its data", () => { | ||
| expect(Array.from(persistentPtyFrame({ cols: 80, rows: 513 }, new Uint8Array([104, 105])))).toEqual([ | ||
| 1, 0, 80, 2, 1, 104, 105, | ||
| ]) | ||
| }) | ||
|
|
||
| test("encodes empty input as an input frame", () => { | ||
| expect(Array.from(persistentPtyFrame({ cols: 80, rows: 24 }, new Uint8Array()))).toEqual([1, 0, 80, 0, 24]) | ||
| }) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is stricter than before, and failures are silent.
attachednow has to match all ofPersistentPty.Info(including refinements like thesesprefix onsessionIDandPositiveIntsizes), plusgenerationandreplay. If any of that doesn't match, the message is dropped with no error. The old code only neededinputProtocolandinfo.size. Withoutattached, the pane never setsattached = trueor shows the "out of date" failure, so it just stays empty. That's what breaks the TUI fixtures, and a small server/client drift would do the same to users. Could theattachedschema be limited to the fields the client actually uses? Alternatively, a message whosetypeis a known one but that doesn't decode could callsetFailure(...)instead of being ignored.