forked from anomalyco/opencode
-
Notifications
You must be signed in to change notification settings - Fork 0
refactor(run): collapse dual permission handling paths (F10) #13
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
Closed
+353
−30
Closed
Changes from all commits
Commits
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
222 changes: 222 additions & 0 deletions
222
packages/opencode/test/cli/run-attach-permission.test.ts
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,222 @@ | ||
| import { describe, expect, test } from "bun:test" | ||
| import { dispatchPermissionAsked, replyPermissionAttachMode } from "../../src/cli/cmd/run" | ||
|
|
||
| // In attach mode, `runEventsHandle` is null because the local `opencode run --attach` | ||
| // process is just an SSE viewer of a remote opencode server — and that server does | ||
| // not currently spin up its own RunEvents handler. So the local SSE-loop branch is | ||
| // the only auto-responder for `permission.asked` events. F10 collapses the dispatch | ||
| // into `dispatchPermissionAsked`; these tests pin the contract that: | ||
| // - non-attach (hasRunEventsHandle=true) NEVER calls sdk.permission.reply | ||
| // - attach (hasRunEventsHandle=false) calls sdk.permission.reply EXACTLY ONCE | ||
| // per permission.asked event matching the active sessionID. | ||
|
|
||
| type ReplyCall = { requestID: string; reply: "once" | "always" | "reject" } | ||
|
|
||
| function makeStubSdk() { | ||
| const calls: ReplyCall[] = [] | ||
| return { | ||
| sdk: { | ||
| permission: { | ||
| reply: async (input: { requestID: string; reply: "once" | "always" | "reject" }) => { | ||
| calls.push({ requestID: input.requestID, reply: input.reply }) | ||
| return { data: undefined } | ||
| }, | ||
| }, | ||
| }, | ||
| calls, | ||
| } | ||
| } | ||
|
|
||
| const ROOT_SESSION = "ses_root_0000000000000000000000" | ||
|
|
||
| const askedEvent = { | ||
| id: "perm_abc", | ||
| sessionID: ROOT_SESSION, | ||
| permission: "bash", | ||
| patterns: ["rm -rf /"], | ||
| } | ||
|
|
||
| describe("cli/run replyPermissionAttachMode (helper unit tests)", () => { | ||
| test("dangerously-skip-permissions=true: replies once, no UI", async () => { | ||
| const { sdk, calls } = makeStubSdk() | ||
| const printed: string[] = [] | ||
|
|
||
| await replyPermissionAttachMode({ | ||
| sdk, | ||
| permission: askedEvent, | ||
| skipPermissions: true, | ||
| jsonMode: false, | ||
| println: (msg) => printed.push(msg), | ||
| }) | ||
|
|
||
| expect(calls).toHaveLength(1) | ||
| expect(calls[0]).toEqual({ requestID: "perm_abc", reply: "once" }) | ||
| expect(printed).toEqual([]) | ||
| }) | ||
|
|
||
| test("dangerously-skip-permissions=false, jsonMode=false: rejects and prints UI", async () => { | ||
| const { sdk, calls } = makeStubSdk() | ||
| const printed: string[] = [] | ||
|
|
||
| await replyPermissionAttachMode({ | ||
| sdk, | ||
| permission: askedEvent, | ||
| skipPermissions: false, | ||
| jsonMode: false, | ||
| println: (msg) => printed.push(msg), | ||
| }) | ||
|
|
||
| expect(calls).toHaveLength(1) | ||
| expect(calls[0]).toEqual({ requestID: "perm_abc", reply: "reject" }) | ||
| expect(printed).toHaveLength(1) | ||
| expect(printed[0]).toContain("permission requested: bash") | ||
| expect(printed[0]).toContain("rm -rf /") | ||
| expect(printed[0]).toContain("auto-rejecting") | ||
| }) | ||
|
|
||
| test("dangerously-skip-permissions=false, jsonMode=true: rejects without UI", async () => { | ||
| const { sdk, calls } = makeStubSdk() | ||
| const printed: string[] = [] | ||
|
|
||
| await replyPermissionAttachMode({ | ||
| sdk, | ||
| permission: askedEvent, | ||
| skipPermissions: false, | ||
| jsonMode: true, | ||
| println: (msg) => printed.push(msg), | ||
| }) | ||
|
|
||
| expect(calls).toHaveLength(1) | ||
| expect(calls[0]).toEqual({ requestID: "perm_abc", reply: "reject" }) | ||
| expect(printed).toEqual([]) | ||
| }) | ||
| }) | ||
|
|
||
| describe("cli/run dispatchPermissionAsked (dual-path contract)", () => { | ||
| test("F10 invariant: non-attach (hasRunEventsHandle=true) NEVER calls sdk.permission.reply", async () => { | ||
| const { sdk, calls } = makeStubSdk() | ||
| const printed: string[] = [] | ||
|
|
||
| const handled = await dispatchPermissionAsked({ | ||
| permission: askedEvent, | ||
| sessionID: ROOT_SESSION, | ||
| hasRunEventsHandle: true, | ||
| sdk, | ||
| skipPermissions: false, | ||
| jsonMode: false, | ||
| println: (msg) => printed.push(msg), | ||
| }) | ||
|
|
||
| expect(handled).toBe(true) | ||
| // The defining invariant: when RunEvents is active in-process, the SSE loop | ||
| // must NOT also reply via SDK or both responders fire on the same event. | ||
| expect(calls).toHaveLength(0) | ||
| expect(printed).toHaveLength(1) | ||
| expect(printed[0]).toContain("auto-rejecting") | ||
| }) | ||
|
|
||
| test("non-attach + skipPermissions: still no SDK reply, and no UI line (RunEvents owns both)", async () => { | ||
| const { sdk, calls } = makeStubSdk() | ||
| const printed: string[] = [] | ||
|
|
||
| await dispatchPermissionAsked({ | ||
| permission: askedEvent, | ||
| sessionID: ROOT_SESSION, | ||
| hasRunEventsHandle: true, | ||
| sdk, | ||
| skipPermissions: true, | ||
| jsonMode: false, | ||
| println: (msg) => printed.push(msg), | ||
| }) | ||
|
|
||
| expect(calls).toHaveLength(0) | ||
| expect(printed).toEqual([]) | ||
| }) | ||
|
|
||
| test("non-attach + jsonMode: still no SDK reply, no UI line (RunEvents emits JSON)", async () => { | ||
| const { sdk, calls } = makeStubSdk() | ||
| const printed: string[] = [] | ||
|
|
||
| await dispatchPermissionAsked({ | ||
| permission: askedEvent, | ||
| sessionID: ROOT_SESSION, | ||
| hasRunEventsHandle: true, | ||
| sdk, | ||
| skipPermissions: false, | ||
| jsonMode: true, | ||
| println: (msg) => printed.push(msg), | ||
| }) | ||
|
|
||
| expect(calls).toHaveLength(0) | ||
| expect(printed).toEqual([]) | ||
| }) | ||
|
|
||
| test("F10 invariant: attach (hasRunEventsHandle=false) replies EXACTLY ONCE", async () => { | ||
| const { sdk, calls } = makeStubSdk() | ||
| const printed: string[] = [] | ||
|
|
||
| const handled = await dispatchPermissionAsked({ | ||
| permission: askedEvent, | ||
| sessionID: ROOT_SESSION, | ||
| hasRunEventsHandle: false, | ||
| sdk, | ||
| skipPermissions: false, | ||
| jsonMode: false, | ||
| println: (msg) => printed.push(msg), | ||
| }) | ||
|
|
||
| expect(handled).toBe(true) | ||
| expect(calls).toHaveLength(1) | ||
| expect(calls[0]).toEqual({ requestID: "perm_abc", reply: "reject" }) | ||
| }) | ||
|
|
||
| test("filters out events for non-active sessions and does not reply or log", async () => { | ||
| const { sdk, calls } = makeStubSdk() | ||
| const printed: string[] = [] | ||
|
|
||
| const handled = await dispatchPermissionAsked({ | ||
| permission: { ...askedEvent, sessionID: "ses_other_0000000000000000000000" }, | ||
| sessionID: ROOT_SESSION, | ||
| hasRunEventsHandle: false, | ||
| sdk, | ||
| skipPermissions: false, | ||
| jsonMode: false, | ||
| println: (msg) => printed.push(msg), | ||
| }) | ||
|
|
||
| expect(handled).toBe(false) | ||
| expect(calls).toEqual([]) | ||
| expect(printed).toEqual([]) | ||
| }) | ||
|
|
||
| test("non-attach branch is inert under all flag combinations: even invoked alongside the attach branch on one event, total replies = 1", async () => { | ||
| // The F10 invariant is enforced by the call site (one dispatch per event). | ||
| // This test pins the *dispatcher* contract that makes that enforcement | ||
| // possible: regardless of skipPermissions/jsonMode, the non-attach branch | ||
| // must never reply via SDK. If a future change accidentally fans the | ||
| // non-attach branch out to also call sdk.permission.reply, this test fails. | ||
| const { sdk, calls } = makeStubSdk() | ||
|
|
||
| await dispatchPermissionAsked({ | ||
| permission: askedEvent, | ||
| sessionID: ROOT_SESSION, | ||
| hasRunEventsHandle: true, | ||
| sdk, | ||
| skipPermissions: false, | ||
| jsonMode: true, | ||
| println: () => {}, | ||
| }) | ||
| await dispatchPermissionAsked({ | ||
| permission: askedEvent, | ||
| sessionID: ROOT_SESSION, | ||
| hasRunEventsHandle: false, | ||
| sdk, | ||
| skipPermissions: false, | ||
| jsonMode: true, | ||
| println: () => {}, | ||
| }) | ||
|
|
||
| expect(calls).toHaveLength(1) | ||
| expect(calls[0].requestID).toBe("perm_abc") | ||
| }) | ||
| }) |
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.
Doc says non-attach mode is where “server-side RunEvents owns the reply”, but in this file non-attach mode runs
RunEvents.makein the same CLI process. Consider changing wording to “in-process/local RunEvents” to avoid confusion with attach mode’s remote server context.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.
Good catch — fixed in 49d67d9. Replaced 'server-side RunEvents' with 'in-process RunEvents' / 'in-process alongside prompt.loop' across both helpers' doc blocks and the call-site inline comment. Kept the failure-mode hypothetical at line ~286 ('e.g. attach mode also gets a server-side RunEvents') because there it correctly refers to a literal future remote-side RunEvents on the attached server.