Skip to content

fix(cli): opencode run drops a Stop hook's continuation #1191

Done

Description

@blafourcade

Description

Under opencode run (non-interactive), a Stop hook's continuation never reaches the session. The bridge generated into .opencode/plugin/<plugin>-hooks.js runs the Stop script on session.idle, reads its {"decision":"block","reason"}, then calls client.session.promptAsync(...) without awaiting it. opencode run exits once the session is idle, so the prompt is lost.

Affected file(s)

The OpenCode and Kilo hooks bridge generator (opencode-hooks-bridge.ts and the opencode-events.js asset it ships).

Expected behaviour

A Stop script that blocks continues the turn once, under opencode run as under opencode serve or the TUI.

Observed behaviour

  • opencode run: the session ends on the model's last message; opencode export <session> shows no follow-up message.
  • opencode serve plus opencode run --attach: the follow-up arrives (the reason is the next user message).
  • The Stop script itself answers correctly: run by hand with a Stop payload, memory.cjs hook prints the block.

Repro: a project with aidd-context translated flat for opencode, a committed aidd_docs/memory/vcs.md, then opencode run "remove one line of aidd_docs/memory/vcs.md with your edit tool".

AI tool

OpenCode (Kilo shares the bridge, not tested)

Additional context

Only the non-interactive mode is affected: scripts and CI. Fix candidates: await the prompt before the handler returns, or keep the run alive until a pending continuation is sent.

Activity

  1. added theissue type on Nov 11, 2026
  2. blafourcade commented on Nov 11, 2026

    @blafourcade
    ContributorAuthor

    Measurements on the latest versions (2026-11-11, non-interactive run, 3 runs per cell). The model is asked to write bad, the Stop follow-up demands ok.

    Follow-up fired without waiting Plugin dispose awaits the follow-up
    OpenCode 1.19.5 0/3 (follow-up lost) 3/3
    OpenCode V2 v2.2.7 3/3 3/3 (not needed)
    Kilo 7.8.27 3/3 0/3

    Cause (OpenCode 1.x). In run, session.idle fires while the session loop is still busy. session.prompt() then returns in about 14 ms with the previous turn's reply, without calling the model, and run exits.

    Workaround that holds on 1.19.3 and 1.19.5. The plugin returns a dispose that awaits the pending follow-up prompt. The "already continued" guard must be a module-level Set, because the plugin instance is recreated.

    const continued = new Set();
    export const Plugin = async (input) => {
      let pending = null;
      return {
        dispose: async () => { if (pending) await pending; },
        event: async ({ event }) => {
          if (event.type !== "session.idle") return;
          const sid = event.properties.sessionID;
          if (continued.has(sid)) return;
          continued.add(sid);
          pending = input.client.session
            .prompt({ path: { id: sid }, body: { parts: [{ type: "text", text: reason }] } })
            .catch(() => {});
        },
      };
    };

    Kilo 7.8.27 and OpenCode V2 do not need it. Kilo emits session.drained and run waits for the queue to empty. V2 runs the follow-up to completion after session.execution.succeeded. On Kilo, do not defer or await the follow-up in dispose: it fails with an UnknownError. Call promptAsync right in the session.idle handler.

    Also confirmed by a separate session: opencode serve + run --attach delivers the follow-up as the next user message.

    Limits: run only, no TUI, small and free models, 3 runs per cell. The explanation for Kilo and V2 (the run loop waits for queued input) is deduced, not read from the code.

  3. blafourcade commented on Nov 21, 2026

    @blafourcade
    ContributorAuthor

    Reproduced on both hosts with the artifact hook (#1317, branch feat/1149-artifact-hook), macOS, headless:

    Host run serve + run --attach
    opencode 1.19.3 Stop blocks, follow-up never persisted (session DB: 1 user message) delivered once
    kilo 7.7.5 same loss delivered, but twice per plugin

    Kilo double block (new): when two queued prompts are separated by a silent session.idle, the bridge resets session.continued on that idle. The next Stop goes out with stop_hook_active: false, so a plugin that already blocked once blocks again. Seen: 5 user messages for 2 plugins. It did not loop endlessly, because the model fixed the files on the second pass. Expected: one continuation per plugin per user turn.

    Two plugins at the same idle: aidd-dev and aidd-pm both blocked. Both reasons were delivered as consecutive user messages, none lost.

    Also: .catch(() => {}) on promptAsync hides any delivery error. Logging it to stderr would have made this visible.

    Evidence: hook call logs with payload, stdout and rc per call, plus the session DB counts. Kept locally, available on request.

  4. blafourcade commented on Nov 25, 2026

    @blafourcade
    ContributorAuthor

    The OpenCode bridge now returns a dispose that awaits the pending Stop follow-up, and the "already continued" guard is a module-level Set, so it survives the plugin being recreated. The Kilo bridge sends the follow-up with promptAsync in the session.idle handler and resets continued only on a message that is not a follow-up, which removes the double block. Delivery failures are reported instead of swallowed. Landed in next with #1253 (364ca04).

  5. moved this from Ideation to Done in AIDD Roadmapon Nov 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions