|
| 1 | +import { Effect, Stream } from "effect" |
| 2 | +import { Llm, OpenCodeDriver } from "opencode-drive" |
| 3 | +import path from "node:path" |
| 4 | + |
| 5 | +// Run from this checkout; OPENCODE_DEV selects the unmodified base or the fix checkout. |
| 6 | +// OPENCODE_REPRO_BEFORE=1 OPENCODE_DEV=/path/to/base opencode-drive run script/repro/hidden-approval.ts |
| 7 | +// OPENCODE_DEV=$PWD opencode-drive run script/repro/hidden-approval.ts |
| 8 | +const before = process.env.OPENCODE_REPRO_BEFORE === "1" |
| 9 | +const label = before ? "BEFORE" : "AFTER" |
| 10 | +const viewport = { cols: 100, rows: 30 } |
| 11 | + |
| 12 | +export default OpenCodeDriver.use( |
| 13 | + { |
| 14 | + opencode: { |
| 15 | + dev: process.env.OPENCODE_DEV ?? process.cwd(), |
| 16 | + env: { BUN_OPTIONS: `--preload=${path.resolve("script/repro/hidden-approval-preload.ts")}` }, |
| 17 | + }, |
| 18 | + project: { git: true, files: { "src/example.ts": "export const example = true\n" } }, |
| 19 | + config: { |
| 20 | + autoupdate: false, |
| 21 | + permissions: [ |
| 22 | + { action: "*", resource: "*", effect: "allow" }, |
| 23 | + { action: "glob", resource: "*", effect: "ask" }, |
| 24 | + ], |
| 25 | + }, |
| 26 | + tui: { viewport }, |
| 27 | + keepArtifacts: true, |
| 28 | + }, |
| 29 | + ({ ui, tui, tuis, llm, opencode }) => |
| 30 | + Effect.gen(function* () { |
| 31 | + yield* llm.title(() => Effect.succeed("Waiting for search approval")) |
| 32 | + yield* llm.serve((_request, index) => |
| 33 | + index === 0 |
| 34 | + ? Stream.make( |
| 35 | + Llm.text("I will find the TypeScript files once you approve the search."), |
| 36 | + Llm.toolCall({ |
| 37 | + index: 0, |
| 38 | + id: "call_search", |
| 39 | + name: "glob", |
| 40 | + input: { pattern: "**/*.ts", path: ".", limit: 10 }, |
| 41 | + }), |
| 42 | + Llm.finish("tool-calls"), |
| 43 | + ) |
| 44 | + : Stream.make(Llm.text("Search complete. Found src/example.ts.")), |
| 45 | + ) |
| 46 | + yield* ui.submit("Find the TypeScript files.") |
| 47 | + yield* ui.waitFor("Allow once", { timeout: 20_000 }) |
| 48 | + const session = (yield* opencode.session.list({ limit: 1 })).data[0] |
| 49 | + if (!session) return yield* Effect.die("No session created") |
| 50 | + const initial = yield* opencode.permission.list({ sessionID: session.id }) |
| 51 | + if (initial.length !== 1) return yield* Effect.die("Expected one pending approval") |
| 52 | + yield* ui.screenshot(`${label.toLowerCase()}-initial-approval`) |
| 53 | + |
| 54 | + // This is the real automatic sweep, not a manual cache invalidation. |
| 55 | + yield* Effect.sleep("10 seconds") |
| 56 | + const pending = yield* opencode.permission.list({ sessionID: session.id }) |
| 57 | + if (pending.length !== (before ? 0 : 1)) |
| 58 | + return yield* Effect.die(`Unexpected pending approval count: ${pending.length}`) |
| 59 | + if (pending[0] && pending[0].id !== initial[0]?.id) return yield* Effect.die("Approval identity changed") |
| 60 | + |
| 61 | + yield* tui.close() |
| 62 | + const reopened = yield* tuis.launch({ viewport, recording: true }) |
| 63 | + if (!reopened.recording) return yield* Effect.die("Recording unavailable") |
| 64 | + yield* reopened.ui.submit("/sessions") |
| 65 | + yield* reopened.ui.waitFor("Waiting for search approval") |
| 66 | + yield* reopened.ui.enter() |
| 67 | + yield* reopened.ui.waitFor("Find the TypeScript files.", { timeout: 20_000 }) |
| 68 | + if (before && (yield* reopened.ui.matches("Allow once"))) |
| 69 | + return yield* Effect.die("Baseline unexpectedly retained its approval") |
| 70 | + if (!before) yield* reopened.ui.waitFor("Allow once") |
| 71 | + yield* reopened.recording.mark( |
| 72 | + `${label}: reopened after cleanup (${before ? "approval lost; still active" : "same approval retained"})`, |
| 73 | + ) |
| 74 | + yield* reopened.ui.screenshot(`${label.toLowerCase()}-reopened`) |
| 75 | + yield* Effect.sleep("3 seconds") |
| 76 | + yield* reopened.recording.mark(`${label}: press Enter to approve`) |
| 77 | + yield* reopened.ui.enter() |
| 78 | + if (!before) yield* reopened.ui.waitFor("Search complete.", { timeout: 15_000 }) |
| 79 | + yield* Effect.sleep("3 seconds") |
| 80 | + yield* reopened.ui.screenshot(`${label.toLowerCase()}-result`) |
| 81 | + const active = yield* opencode.session.active() |
| 82 | + if (Boolean(active[session.id]) !== before) return yield* Effect.die("Unexpected execution status") |
| 83 | + console.log(JSON.stringify({ label, pending: pending.length, active: Boolean(active[session.id]) })) |
| 84 | + console.log("video:", yield* reopened.recording.finish()) |
| 85 | + yield* opencode.session.interrupt({ sessionID: session.id }) |
| 86 | + yield* opencode.session.wait({ sessionID: session.id }) |
| 87 | + }), |
| 88 | +) |
0 commit comments