Skip to content

ACP: set_config_option can answer with the previous selection when the switch echo lands late #60316

Done

Description

@cameronalizadeh-distyl

Description

Over ACP, a session/set_config_option response can describe the previous selection instead of the one just made.

select in packages/cli/src/acp/sessions.ts writes the new selection into the session's selection Ref and then calls the server's switch; the server's session.model.selected echo is folded back into the same Ref ("Update selection before switching so the echoed event is a no-op"). That holds only while the echo arrives before the next selection. When a client sends its next set_config_option before the previous switch's echo has landed, the echo overwrites the newer selection, and setSessionConfigOption answers from the Ref: the model option's currentValue and the effort options are the previous model's. The newer echo lands a moment later and the Ref is right again, but the response has gone out.

With the script below, eight back-to-back model selections per session: on a 4-vCPU Linux container (2.0.30) 12 of 160 responses named the previous model; pinned to one core with taskset -c 0, 33 of 160. On an M-series Mac a single copy never reproduces (the echo lands within a millisecond), four copies at once reproduce it on v2.2.1: 6 of 480. Every wrong answer was the model selected just before. Same code path at dev head f7b4d509.

A client that makes one selection per session never sees it; one that sets model then effort, or walks the model list, does. #59789 is the failure-side twin of the same Ref-before-switch design.

Plugins

None

OpenCode version

v2.2.1 (npm @opencode/cli, macOS arm64) and 2.0.30 (Linux x64); same code at dev f7b4d50

Steps to reproduce

# acp_echo_repro.py — select models back to back over ACP and compare each
# response's model currentValue with the model just selected.
import json, os, subprocess, sys, tempfile, threading, queue
binary = sys.argv[1] if len(sys.argv) > 1 else "opencode"
models = ["openai/gpt-4o", "openai/gpt-5", "openai/gpt-5-pro", "openai/gpt-5.1",
          "openai/gpt-5.2", "openai/gpt-5.5", "openai/gpt-5.6", "openai/gpt-6-astra"]
wrong = total = 0
for run in range(20):
    home, cwd = tempfile.mkdtemp(), tempfile.mkdtemp()
    env = {"HOME": home, "PATH": os.environ["PATH"], "OPENAI_API_KEY": "x",
           "OPENCODE_DISABLE_MODELS_FETCH": "1", "OPENCODE_DISABLE_AUTOUPDATE": "true",
           "OPENCODE_CONFIG_CONTENT": json.dumps({"agents": {"title": {"disabled": True}}})}
    p = subprocess.Popen([binary, "acp"], cwd=cwd, env=env, stdin=subprocess.PIPE,
                         stdout=subprocess.PIPE, stderr=subprocess.DEVNULL, text=True, bufsize=1)
    out = queue.Queue(); threading.Thread(target=lambda: [out.put(l) for l in p.stdout], daemon=True).start()
    def call(i, method, params):
        p.stdin.write(json.dumps({"jsonrpc": "2.0", "id": i, "method": method, "params": params}) + "\n"); p.stdin.flush()
        while True:
            try: m = json.loads(out.get(timeout=60))
            except json.JSONDecodeError: continue
            if m.get("id") == i: return m["result"]
    call(1, "initialize", {"protocolVersion": 1, "clientCapabilities": {}, "clientInfo": {"name": "repro", "version": "0"}})
    sid = call(2, "session/new", {"cwd": cwd, "mcpServers": []})["sessionId"]
    for n, model in enumerate(models, start=3):
        r = call(n, "session/set_config_option", {"sessionId": sid, "configId": "model", "value": model})
        current = next(o["currentValue"] for o in r["configOptions"] if o["id"] == "model")
        total += 1
        if current != model:
            wrong += 1; print(f"run {run}: selected {model}, response says {current}")
    p.terminate()
print(f"{wrong} of {total} responses described a different model than the one just selected")
  1. taskset -c 0 python3 acp_echo_repro.py /path/to/opencode on Linux; on a fast Mac, run four copies at once instead.
  2. Expected: 0 of 160. Observed: 33 of 160 on one Linux core, 6 of 480 across four concurrent copies on a Mac, each line naming the model selected just before.

In the ACP wire fixture under packages/cli/test/acp, the same thing should show by delivering the first switch's session.model.selected event after the second set_config_option request.

Screenshot

N/A, protocol only.

Operating System

Linux x64 (Debian-based container, 4 vCPU) and macOS arm64 under contention

Terminal

N/A (ACP over stdio)

Activity

  1. opencodejr commented on Oct 29, 2026

    @opencodejr
    Contributor

    Thanks for the detailed report. I can confirm this.

    On @opencode/cli 2.2.1 (Linux, pinned to one core with taskset -c 0), I ran a version of your script that takes 8 openai/ models from the session's own model list. Some of the hard-coded IDs aren't in the bundled catalog. It starts opencode acp, opens a session and sends session/set_config_option for each model back to back. 93 of 160 responses reported the wrong model currentValue, and each one named the model selected just before.

    It still happens on the latest dev (f7b4d50) run from source: 50 of 160 wrong, same pattern. The script below reproduces it.

    This fits your analysis: select writes the new selection before switching, and a late echo of the previous switch then overwrites it before the response is built. Related: #59789, the failure-side version of the same pattern.

    Reproduction script (acp_echo_repro.py)
    # Select models back to back over ACP and compare each set_config_option
    # response's `model` currentValue with the model just selected.
    # Usage: python3 acp_echo_repro.py [opencode command...]
    import json, os, subprocess, sys, tempfile, threading, queue
    cmd = sys.argv[1:] or ["opencode"]
    wrong = total = 0
    for run in range(20):
        home, cwd = tempfile.mkdtemp(), tempfile.mkdtemp()
        env = {"HOME": home, "PATH": os.environ["PATH"], "OPENAI_API_KEY": "x",
               "OPENCODE_DISABLE_MODELS_FETCH": "1", "OPENCODE_DISABLE_AUTOUPDATE": "true",
               "OPENCODE_CONFIG_CONTENT": json.dumps({"agents": {"title": {"disabled": True}}})}
        p = subprocess.Popen(cmd + ["acp"], cwd=cwd, env=env, stdin=subprocess.PIPE,
                             stdout=subprocess.PIPE, stderr=subprocess.DEVNULL, text=True, bufsize=1)
        out = queue.Queue(); threading.Thread(target=lambda: [out.put(l) for l in p.stdout], daemon=True).start()
        def call(i, method, params):
            p.stdin.write(json.dumps({"jsonrpc": "2.0", "id": i, "method": method, "params": params}) + "\n"); p.stdin.flush()
            while True:
                try: m = json.loads(out.get(timeout=120))
                except json.JSONDecodeError: continue
                if m.get("id") == i:
                    if "error" in m: raise SystemExit(f"{method} failed: {m['error']}")
                    return m["result"]
        call(1, "initialize", {"protocolVersion": 1, "clientCapabilities": {}, "clientInfo": {"name": "repro", "version": "0"}})
        s = call(2, "session/new", {"cwd": cwd, "mcpServers": []})
        sid = s["sessionId"]
        opt = next(o for o in s["configOptions"] if o["id"] == "model")
        values = [v["value"] for v in opt["options"]] if "value" in opt["options"][0] else [v["value"] for g in opt["options"] for v in g["options"]]
        models = [v for v in values if v.startswith("openai/")][:8]
        for n, model in enumerate(models, start=3):
            r = call(n, "session/set_config_option", {"sessionId": sid, "configId": "model", "value": model})
            current = next(o["currentValue"] for o in r["configOptions"] if o["id"] == "model")
            total += 1
            if current != model:
                wrong += 1; print(f"run {run}: selected {model}, response says {current}", flush=True)
        p.terminate()
    print(f"{wrong} of {total} responses described a different model than the one just selected")

    Run it: python3 acp_echo_repro.py

  2. Vaili commented on Oct 31, 2026

    @Vaili

    I reproduced the stale currentValue with back-to-back selections and I'd like to take it. I'll start with a failing ACP test that sends a second set_config_option before the first echo lands, then stop a late echo from overwriting a newer selection.

  3. dimiboi commented on Nov 2, 2026

    @dimiboi

    Two more data points from #61302, which I've closed as a duplicate of this:

    • Still reproduces on 2.2.5 (macOS 26.6.1, Node 26.13.0).
    • Reproduces from a single opencode acp process on an M-series Mac when the models are real providers (LM Studio local models plus OpenRouter), not the bundled catalogue: 12 of 70 replies across ten 7-switch sweeps, 1 of 24 in a 3×8 sweep, and 2 of 1092 across 364 models with all plugins and MCP servers removed. Every wrong reply named the model selected just before.

    Downstream effect: bb's ACP model discovery walks the model list and stores each reply's thought_level option under the requested model, so one model's reasoning levels end up under another (get-bb/bb#6871).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions