Skip to content

Commit a17b951

Browse files
committed
fix(integrations): apply Opencode extra-args before prompt-derived --command
When the Opencode prompt starts with `/`, `build_exec_args` injects `--command <X>` derived from the prompt. The previous placement of `self._apply_extra_args_env_var(args)` appended operator-injected args AFTER that `--command`, so a user setting `SPECIFY_OPENCODE_EXTRA_ARGS="--command override"` could redirect the command under typical last-wins repeated-flag CLI semantics. Move the hook to immediately after `args = [self.key, "run"]`, before the prompt-parsing block. The operator's `--command override` (if any) now precedes the Spec Kit-derived `--command speckit`, so the canonical choice wins. Adds `test_opencode_extra_args_cannot_clobber_prompt_derived_command` locking the ordering. Also corrects the module docstring to describe the hook as living in `IntegrationBase` (not `SkillsIntegration`) and to acknowledge that this file covers Codex/Devin/Opencode/Copilot in addition to SkillsIntegration stubs. Addresses Copilot review on #2596.
1 parent 8341c12 commit a17b951

2 files changed

Lines changed: 47 additions & 3 deletions

File tree

‎src/specify_cli/integrations/opencode/__init__.py‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ def build_exec_args(
2929
output_json: bool = True,
3030
) -> list[str] | None:
3131
args = [self.key, "run"]
32+
# Apply operator-injected extra args before the prompt-derived
33+
# --command and the canonical --format/-m flags so Spec Kit's
34+
# later appends remain authoritative under repeated-flag CLI
35+
# semantics.
36+
self._apply_extra_args_env_var(args)
3237

3338
message = prompt
3439
if prompt.startswith("/"):
@@ -37,7 +42,6 @@ def build_exec_args(
3742
args.extend(["--command", command])
3843
message = remainder
3944

40-
self._apply_extra_args_env_var(args)
4145
if model:
4246
args.extend(["-m", model])
4347
if output_json:

‎tests/integrations/test_extra_args.py‎

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
1-
"""Tests for the per-integration `SPECIFY_<KEY>_EXTRA_ARGS` env-var hook
2-
in `SkillsIntegration.build_exec_args`. See issue #2595."""
1+
"""Tests for the per-integration `SPECIFY_<KEY>_EXTRA_ARGS` env-var hook.
2+
3+
The hook is implemented in `IntegrationBase._apply_extra_args_env_var`
4+
and wired into every concrete `build_exec_args` —
5+
`MarkdownIntegration`, `TomlIntegration`, `SkillsIntegration`, plus the
6+
overrides in Codex, Devin, Opencode and Copilot. These tests cover both
7+
the shared mechanism (via `SkillsIntegration` stubs near the top of the
8+
file) and each override integration end-to-end (further down). See
9+
issue #2595."""
310

411
import os
512

@@ -216,6 +223,39 @@ def test_opencode_integration_honours_extra_args(monkeypatch):
216223
]
217224

218225

226+
def test_opencode_extra_args_cannot_clobber_prompt_derived_command(
227+
monkeypatch,
228+
):
229+
"""Operator-injected extra args must appear BEFORE the prompt-derived
230+
``--command <X>`` so that Spec Kit's command selection wins under
231+
repeated-flag CLI semantics (last value typically takes precedence).
232+
233+
Locks against the regression where an operator setting
234+
``SPECIFY_OPENCODE_EXTRA_ARGS="--command malicious"`` could redirect
235+
a slash-prefixed prompt to a different command.
236+
"""
237+
from specify_cli.integrations.opencode import OpencodeIntegration
238+
239+
monkeypatch.setenv(
240+
"SPECIFY_OPENCODE_EXTRA_ARGS", "--command operator-override"
241+
)
242+
args = OpencodeIntegration().build_exec_args("/speckit body text")
243+
# Prompt-derived "--command speckit" appears AFTER the
244+
# operator-injected one, so a CLI that resolves repeated flags
245+
# last-wins will honour Spec Kit's choice.
246+
assert args == [
247+
"opencode",
248+
"run",
249+
"--command",
250+
"operator-override",
251+
"--command",
252+
"speckit",
253+
"--format",
254+
"json",
255+
"body text",
256+
]
257+
258+
219259
def test_copilot_integration_honours_extra_args(monkeypatch):
220260
from specify_cli.integrations.copilot import CopilotIntegration
221261

0 commit comments

Comments
 (0)