Skip to content

fix(opencode_cli): support OpenCode 2.2.6 and 2.2.7 with a project policy preflight - #1086

Open
Varshith-Kali wants to merge 9 commits into
NVIDIA:mainfrom
Varshith-Kali:fix-1084-opencode-2x-preflight
Open

Varshith-Kali wants to merge 9 commits into
NVIDIA:mainfrom
Varshith-Kali:fix-1084-opencode-2x-preflight

Conversation

@Varshith-Kali

@Varshith-Kali Varshith-Kali commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #1084.

On OpenCode 2.x the 1.x deny-all policy is silently ignored: opencode debug config prints only a list of configuration sources instead of resolved config. Widening the 1.x version set would have admitted a runtime whose policy cannot be verified, so 2.x gets its own path instead. OPENCODE_CONFIG_CONTENT is honoured on 2.x (verified live on 2.2.7: a content-defined agent resolves), but the project file is the policy the preflight verifies.

What this does

  • Delivers the deny-all policy as a project opencode.jsonc in the per-run temporary directory, with the probe agent and a top-level deny-all permissions list (2.x shape: agents map, permissions list).
  • Preflights with opencode debug agents: the selected agent must exist and its resolved permission list must end in a wildcard (*, *) deny, with every later rule also a deny. A missing agent, a trailing allow or ask, a trailing wildcard allow, or unparsable output all fail closed before inference.
  • Refuses to start any 2.x server when the platform system directory (/etc/opencode on Linux, /Library/Application Support/opencode on macOS, %ProgramData%\opencode on Windows) configures plugins or allows plugin.use: a system-level allow outranks the project deny (verified live on real 2.2.7), so the gate runs before debug agents --standalone in the preflight and before auth list --standalone in the auth probe. It refuses on a plugin.use rule with an effect other than deny, any plugin listed in plugin/plugins (add operations only), or any entry under plugin/ or plugins/. A missing system directory passes; an unreadable one fails closed.
  • Omits --pure from the 2.x argv (it is an unknown flag on 2.x: it prints the run help text and exits 1). The child environment still sets OPENCODE_PURE=1, which 1.x honours; on 2.x it is inert (OPENCODE_PURE is not in the 2.2.7 binary), so 2.x plugin isolation rests on the project plugin.use deny plus the pre-start system-source gate above.
  • Checks 2.x auth with auth list --standalone --format json, which prints [] when unauthenticated; a non-empty integrations array means authenticated.
  • Keeps the 1.x version set, policy delivery, and debug config preflight unchanged. Every other version, including prereleases, still fails closed.

Evidence (live, OpenCode 2.2.6 and 2.2.7 from npm @opencode/cli)

  • A project opencode.jsonc is honoured; the probe agent resolves to the deny-ending list from the issue. OPENCODE_CONFIG_CONTENT is honoured on 2.2.7 (a content-defined agent resolves); OPENCODE_PERMISSION does not change resolved agent permissions; OPENCODE_DISABLE_PROJECT_CONFIG=1 disables the project file on 2.x (verified live: with the flag set, a /etc/opencode/plugins marker plugin loads despite the project policy denying it), so the flag stays unset on 2.x and set only on 1.x.
  • The new preflight was run live against both real binaries: it passes with the measured permission list, and the project file is written with the selected agent.
  • A user-level config can append rules after the project file (measured: an allow appended by a user config shows up in the resolved list), so the preflight reads the fully resolved list rather than trusting the file.

Tests

  • tests/provider/test_opencode_cli.py: 148 passed. New coverage: the debug agents parser (accepts the measured list; rejects a missing agent, trailing allow/ask, trailing wildcard allow, malformed output, non-list output, missing permissions, no wildcard rule), the 2.x version set and auth probe, the project policy shape, and end-to-end fake-binary runs for 2.2.6 and 2.2.7 (policy holds, --pure omitted, project file recorded) plus the fail-closed cases. This round adds the system-source gate: direct unit tests (allow/ask/deny policies, listed and installed plugins, removal-only entries, JSONC parsing, unparseable config, unlistable directory) and fake-binary runs for system plugin plus system allow (auth, preflight and inference all fail closed with no probe markers), system plugin without allow (passes, project deny blocks), and no system sources (passes). The new regression test fails with the gate calls removed and passes with them. 1.x version probes are pinned to (1, 18, 33) and the 2.x mock probes to (2, 2, 6), so the suite no longer depends on the installed OpenCode binary.
  • tests/opencode and tests/unit: previously 4378 passed, 22 skipped, 7 failed. The 7 failures are pre-existing Anthropic provider tests (verified identical on unmodified main: httpx2.InvalidURL in test_native_claude_55_* and test_bearer_auth_scheme_sends_authorization_header), unrelated to this change. The full run was not repeated in this session; the provider-adjacent unit suites were re-run instead: tests/unit/test_agent_cli.py and tests/unit/test_providers.py: 264 passed, 10 skipped, 1 failed (the same pre-existing Anthropic failure, verified identical on unmodified HEAD).
  • ruff check and ruff format --check clean.

Caveats, not yet measured

  • No live inference ran under the 2.x policy (no authenticated session in the sandbox), so whether the resolved denies hold for every tool at run time is still unverified.
  • The 2.x auth-positive path (auth list --standalone --format json returning a non-empty array) is verified against the 2.2.6 binary list implementation and the empty-array case live, not against a real logged-in session.
  • User/org config layering beyond the single user-config allow measured above is not verified; the preflight catches any layering outcome in the resolved list, but only for the selected agent.
  • Two layers outrank even the system directory and are not observable pre-start from SkillSpector: macOS MDM preference policies, and org integration / well-known document policies that outrank project policies. A system plugin allowed through those layers would load before the project policy is read.

…licy preflight (NVIDIA#1084)

On OpenCode 2.x, OPENCODE_CONFIG_CONTENT is silently ignored and debug
config prints only configuration sources, so the 1.x deny-all policy
cannot be delivered or verified the 1.x way. This adds a separate 2.x
path: the deny-all policy is delivered as a project opencode.jsonc in
the per-run temporary directory, the 2.x argv omits --pure (an unknown
flag on 2.x), and the preflight runs opencode debug agents and fails
closed unless the selected agent exists and its resolved permission
list ends in a wildcard deny with no later non-deny rule. The 1.x
version set, policy, and preflight are unchanged, and any other
version still fails closed.

Signed-off-by: Varshith Puli <pulivarshit@gmail.com>

@yashrajp22 yashrajp22 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Source review of 9dbf4fc -> 4acd675, with a completed Greptile review of the same comparison. The comments identify two runtime-contract issues from the pinned official OpenCode 2.2.6/2.2.7 source and one test-isolation regression. I have not executed the candidate, built or tested fresh wheels, run the sample corpus, or made live-provider calls; those behavioral checks remain pending.

Run without external plugins. 1.x only: ``--pure`` is an unknown
flag on 2.x (verified on 2.2.6/2.2.7, #1084: it prints the ``run``
help text and exits 1), so the 2.x argv omits it and relies on
``OPENCODE_PURE=1`` from the child environment instead. The version

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we enforce external-plugin isolation before admitting 2.x? The 2.2.6/2.2.7 sources do not consume OPENCODE_PURE or OPENCODE_TEST_MANAGED_CONFIG_DIR. System configuration is still discovered, and plugin discovery retains directory plugins even when inline plugin: [] is supplied. For example, a machine-wide /etc/opencode/plugins plugin remains eligible to load while the selected agent has deny-all tool permissions, so this preflight can pass without preserving the existing no-external-plugins boundary. Please suppress those sources before startup, or keep these versions rejected until that boundary is enforceable, and cover a configured system plugin.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed. Verified live against real OpenCode 2.2.7 with a marker plugin in /etc/opencode/plugins (top-level code writes a marker on import, setup writes one on activation).

The project policy now denies plugin.use for every target. That deny is checked in the plugin loader before the module is imported, so a denied plugin never runs, however it was configured. With the policy in place, neither marker appears and the probe agent still resolves.

Two things from the investigation changed the approach. First, a plugins ["-*"] remove op does not close the boundary: it stops activation but the module is still imported first, so the marker still appears. It also empties debug agents entirely, which would fail the preflight closed. So it is not in the config. Second, OPENCODE_DISABLE_PROJECT_CONFIG was disabling the project file entirely in the real child environment, which made the 2.x policy undeliverable. It is removed; the run directory is a fresh temp dir whose only project config is the one this provider writes.

Two earlier live claims in the PR are corrected in the code comments: OPENCODE_CONFIG_CONTENT is honoured on 2.x (a content-defined agent resolves), and OPENCODE_DISABLE_PROJECT_CONFIG does block the project file. The unit tests now enforce the shape: the 2.x fake refuses the probe when the plugin-use deny is missing, so removing the policy fails the test the way the real binary would load the plugin.

One caveat, stated in the code: a system-level policy allow outranks this project deny by design, and that is the machine owner explicit choice, not something the project file can override.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Update on this thread: the gate now also closes the legacy-form bypass (1eb8839). A managed allow written as integration.use with a plugin: resource decodes to plugin.use in the pinned 2.2.6/2.2.7 policy schema, and I verified live on real 2.2.7 that it lets a system plugin import during startup despite the project deny. The gate now applies that decoder before checking the action, so the legacy form refuses before any server starts, same as the direct form. The boundary from my last note still stands: there is no supported way to suppress managed sources, so refusal stays the enforcement, and a managed document that only changes non-plugin settings still loads but cannot inject a plugin.

raise AgentCLIError(
f"OpenCode policy preflight could not write the project policy: {exc}"
) from exc
stdout_raw = run_probe([binary, "debug", "agents"], "agents")

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could the 2.x probes and inference use a scoped standalone server? debug agents calls the default service resolver, which starts a background service. Each invocation gets a different XDG_STATE_HOME registration, but the default listen port stays fixed. An ordinary OpenCode service already using that port, or overlapping scan calls (the CLI default concurrency is two), therefore makes this isolated service fail to bind and the 15-second preflight fail. A fresh registration directory does not isolate the port. Please verify the path with an existing service and concurrent calls, including shutdown on errors.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed. Both the preflight probe and the 2.x run argv now use --standalone, so each call runs a private server on an OS-assigned port instead of the background service on the fixed channel port.

Verified live on real 2.2.7 with an ordinary service already listening on 49374: the managed debug agents call fails with "Managed service port 49374 on 127.0.0.1 is already in use", while debug agents --standalone succeeds. Two concurrent standalone calls both succeed, and no server is left behind afterwards (its stdin lease closes with the command).

The 2.x fake binary records an agents-standalone marker, and the preflight and run tests assert it, so a regression that drops the flag fails the suite.

Comment on lines +988 to +989
version = _probe_opencode_version(binary)
pure_arg = [] if version is not None and version[0] == 2 else ["--pure"]

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please mock the new version probe in the existing argv unit tests. TestBuildOpencodeArgv calls /usr/bin/opencode without mocking it and unconditionally expects --pure; when a supported 2.x binary is installed there, test_argv_exact_shape_without_model and test_argv_disables_external_plugins now fail even though the builder follows the intended 2.x behavior. Pin the mocked version for the 1.x expectations and cover the 2.x branch explicitly so the unit results do not depend on a developer’s installation.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed. TestBuildOpencodeArgv now pins the mocked version to 1.18.33 with an autouse fixture, so the 1.x expectations no longer depend on which binary (if any) a developer has installed. The 2.x branch is covered explicitly for both 2.2.6 and 2.2.7: --pure absent, --standalone present, agent and format shape intact. An unknown version keeps the 1.x shape and the preflight fails closed on version.

The 2.x fake binary also accepts debug agents --standalone now, and records a marker the preflight and run tests assert, so the standalone flag is covered end to end rather than only in the argv unit tests.

… OpenCode 2.x (NVIDIA#1084)

Address the three review findings on the 2.x admission, all verified
live against real OpenCode 2.2.7 with a marker plugin in
/etc/opencode/plugins and a service already on the default port:

- Plugin isolation: the project policy now denies plugin.use for every
  target, which the loader checks before importing a plugin module, so
  a system plugin's code never runs. A plugins remove op was tried and
  rejected: it stops activation but not the import, and it empties
  debug agents. Also remove OPENCODE_DISABLE_PROJECT_CONFIG from the
  child environment: it disables the project file entirely, which made
  the 2.x policy undeliverable (the run directory is a fresh temp dir
  whose only project config is the one this provider writes).
- Standalone servers: the preflight probe and the run argv now use
  --standalone on 2.x, so a busy default port or overlapping scans no
  longer fail the preflight, and no service is left behind.
- Argv tests: pin the mocked version for the 1.x expectations and cover
  the 2.x branch explicitly, and make the 2.x fake refuse the probe
  when the plugin-use deny is missing.

The two earlier live claims this corrects: OPENCODE_CONFIG_CONTENT is
honoured on 2.x (a content-defined agent resolves), and
OPENCODE_DISABLE_PROJECT_CONFIG does block the project file.

Signed-off-by: Varshith Puli <pulivarshit@gmail.com>

@yashrajp22 yashrajp22 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Source review of 9dbf4fc -> 9e24905, with a completed Greptile review of the same comparison. The standalone preflight/inference and version-pinned argv test changes address those earlier source findings; behavioral verification remains pending. The existing plugin-isolation request also remains unresolved when a system plugin-use allow overrides the project deny; I have not duplicated that inline comment. The new comments cover the separate authentication startup path and a 1.x configuration-isolation regression. I have not executed the candidate, built or tested fresh wheels, run the sample corpus, or made live-provider calls.

Comment on lines +1197 to +1199
auth_argv = (
[binary, "auth", "list", "--format", "json"]
if _is_supported_opencode_version_v2(version)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could the 2.x authentication probe use the same isolated directory, plugin policy and standalone service as inference? This call still omits --standalone and cwd, and never writes the project policy. In both supported releases, auth uses the default service resolver and the caller’s working directory. An ordinary OpenCode service occupying the fixed port therefore makes availability fail even with valid credentials; when the port is free, the probe can load caller/system plugins without the new plugin-use deny. The standalone fix in debug agents does not cover this separate command. Please cover availability with an existing service and a configured project/system plugin, including cleanup.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed. The 2.x auth probe now runs with the same isolation as inference: it writes the project policy (plugin.use deny) into its temp dir, runs auth list --standalone --format json with cwd set to that dir, and the temp dir is removed in the existing finally block. I verified live on real 2.2.7 that auth list --standalone --format json prints [] when unauthenticated, and the new fake-binary tests cover a busy fixed port, a configured plugin, and probe cleanup. Pushed as 99d62dc.

Comment on lines +755 to +758
# No OPENCODE_DISABLE_PROJECT_CONFIG here: on 2.x the deny-all
# policy is delivered as a project opencode.jsonc in the run's
# temporary directory, and that flag disables the project file
# entirely (verified live on real 2.2.7, #1084: with the flag

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please retain OPENCODE_DISABLE_PROJECT_CONFIG=1 for the supported 1.x runtimes. A fresh temporary directory can still have project ancestors—for example, when TMPDIR points beneath a repository containing .opencode. In 1.18.31 and 1.19.4, removing this flag enables the ancestor configuration walk; configuration initialization writes a missing .gitignore and starts dependency installation in discovered directories before the inline policy and our resolved-config check. --pure suppresses external plugin imports, not this setup. This can modify a project outside the temporary directory before preflight finishes. Please make the 2.x policy-delivery change version-specific and cover a temporary directory beneath an existing .opencode project.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed. The flag removal is now version-specific: _prepare_opencode_env probes the binary and keeps OPENCODE_DISABLE_PROJECT_CONFIG=1 on 1.x (and on an unknowable version), omitting it only on 2.x where the project policy must be delivered. I verified live on real 1.19.4 that without the flag, a run from a temp dir beneath a project writes a .gitignore into the ancestor .opencode directory, and with the flag set it writes nothing. New tests cover a temp dir beneath an existing .opencode project for both versions. Pushed as 99d62dc.

… off on 1.x (NVIDIA#1084)

Address the two review findings on the 2.x admission, both verified
live against real OpenCode 1.19.4 and 2.2.7:

- 1.x keeps OPENCODE_DISABLE_PROJECT_CONFIG=1. The flag removal in the
  previous round was too broad: a fresh temp dir can sit beneath a
  repository containing .opencode, and without the flag 1.x walks up
  and runs config initialisation in that ancestor project (live on
  1.19.4: it writes a missing .gitignore into the ancestor .opencode
  directory before the preflight finishes). The removal is now
  version-specific: _prepare_opencode_env probes the binary and omits
  the flag only on 2.x, where the project opencode.jsonc carries the
  policy and the flag would disable it.
- 2.x auth probe uses the same isolation as inference: it now runs
  `auth list --standalone --format json` with cwd set to its isolated
  temp dir, after writing the project policy (plugin.use deny) into
  that dir. The probe previously resolved the default service from
  the caller's cwd with no policy, so a busy fixed port failed
  availability with valid credentials and a configured project/system
  plugin could load. Verified live on 2.2.7: `auth list --standalone
  --format json` prints [] when unauthenticated. The probe temp dir
  is removed in the existing finally block.

Tests: fake-binary coverage for the temp dir beneath an existing
.opencode project (1.x keeps the flag, 2.x omits it and delivers the
policy), the auth probe against a busy fixed port and a configured
plugin, and probe temp-dir cleanup; 128 passed in
tests/provider/test_opencode_cli.py.

Signed-off-by: Varshith Puli <pulivarshit@gmail.com>
@Varshith-Kali

Copy link
Copy Markdown
Contributor Author

Hi @yashrajp22, quick heads-up while the re-review is pending: the test-unit run on head 99d62dc was cancelled after about six hours (started 07:49Z, cancelled 13:49Z), so there is no successful test-unit result on this head yet. The other checks (lint, docker-smoke, OpenCode TypeScript Tests, DCO) are all green. Could you re-run test-unit when you get a chance, or let me know if the cancellation was deliberate? Happy to fix anything it turns up.

@rng1995 rng1995 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[SkillSpector Review]

Decision: Changes Requested.

Value and readiness

A separate, exactly pinned 2.2.6/2.2.7 path is useful for restoring OpenCode compatibility. The selected-agent deny-ending check, standalone inference/preflight/auth commands, isolated auth cwd and restored 1.x project-config flag address substantial parts of the earlier feedback. The no-external-plugins boundary still has an explicit bypass, and the version-mocking correction remains incomplete, so this head is not ready to merge.

Material findings

P0 — keep 2.x rejected until external plugins are suppressed before import. _opencode_config_2x explicitly acknowledges that a system-level plugin.use allow outranks its project deny (source). The 2.x branch starts debug agents --standalone, then verifies only the selected agent's permission list; it never establishes that the separate plugin policy suppressed all external module imports. A configured system plugin can therefore run during startup while that agent's wildcard tool deny still passes. The auth command starts under the same overridable project policy. This leaves the existing human plugin-isolation request unresolved. Suppress those sources or enforce a non-overridable policy before server startup, otherwise retain the current fail-closed rejection of 2.x. Cover a configured system plugin with an overriding system allow, including import/activation markers for auth, preflight and inference; checking that the project JSON contains a deny is insufficient.

P2 — pin the remaining 1.x unit-test version probes. The new autouse fixture is scoped to TestBuildOpencodeArgv. TestOpencodeDenyAllPolicy.test_policy_overrides_hostile_ambient_configuration still calls the real /usr/bin/opencode through both the argv builder and environment preparation, then unconditionally indexes OPENCODE_DISABLE_PROJECT_CONFIG expecting "1" (test). With a supported 2.x installation, the intended implementation omits that key and this test fails. TestOpencodeAuthCheck.test_probe_uses_scrubbed_environment has the same 1.x assertion while mocking subprocess.run but leaving the new Popen-based version probes real. Pin _probe_opencode_version in all tests that assume 1.x, while preserving the explicit fake-binary 2.x integration coverage. The unit suite must not depend on whether a developer installed OpenCode 2.x at that path.

PIC tradeoffs

Treating a system plugin allow as an accepted exception changes this provider's existing no-external-plugins contract. That needs an explicit maintainer/PIC decision; compatibility alone does not establish the isolation guarantee. The PR description and README also retain the older claims that 2.x ignores OPENCODE_CONFIG_CONTENT and that the project-disable flag does not block the policy, while the updated source and author replies correct those measurements; align the public explanation with the final supported behavior and evidence.

Verification and gaps

Read the entire four-file diff, exact-head production configuration/auth/preflight/runner context and regression source, linked issue #1084, all seven reviews, all ten inline comments across all five threads and the issue discussion. Static AST parsing of both changed Python files and their base versions succeeded. The fresh post-review head still matches the frozen inventory. Five hosted checks passed; test-unit was cancelled after approximately six hours on this exact head, so no successful unit result is established. All five human review threads remain unresolved and the human Changes Requested review remains active. Author-reported fake/live measurements were inspected as evidence, not reproduced. No contributor code, tests, imports, builds, hooks, scanner, regex probes or model calls were executed.

Reviewed head: 99d62dc0ce342a4a015bf590a49daf5564f81461.

…gures plugins (NVIDIA#1086)

A system-level plugin.use allow outranks the project deny, so a
configured system plugin runs during startup while the agent permission
check passes (verified live on real 2.2.7). Add a pre-start gate for
2.x only: before debug agents --standalone in the preflight and before
auth list --standalone in the auth probe, inspect the platform system
directory (/etc/opencode on Linux, /Library/Application Support/opencode
on macOS, %ProgramData%\opencode on Windows) and refuse when it
configures any plugin source: a policies rule with action plugin.use
and an effect other than deny, any plugin listed in plugin/plugins
(add operations only), or any entry under plugin/ or plugins/. A
missing directory passes; an unreadable directory or config file fails
closed. System opencode.json/opencode.jsonc is parsed JSONC-tolerant.

Pin _probe_opencode_version to (1, 18, 33) in the 1.x unit tests
(TestOpencodeAuthCheck, TestOpencodeVersionRange,
TestOpencodeDenyAllPolicy) and to (2, 2, 6) in
TestOpencodeVersionRangeV2, so the suite no longer depends on which
OpenCode binary a developer has installed.

Align the README and PR body with the verified behavior:
OPENCODE_CONFIG_CONTENT is honoured on 2.2.7,
OPENCODE_DISABLE_PROJECT_CONFIG disables the project file on 2.x, and
OPENCODE_PURE is not in the 2.2.7 binary, so 2.x plugin isolation rests
on the project policy deny plus the new system-source gate.

Signed-off-by: Varshith Puli <pulivarshit@gmail.com>
@Varshith-Kali

Copy link
Copy Markdown
Contributor Author

Thanks for the review, this head addresses both findings.

P0: added a pre-start system-source gate for 2.x only (commit ce65029). _check_opencode_system_sources runs before debug agents --standalone in the preflight and before auth list --standalone in the auth probe (inference is covered through the preflight). It inspects the platform system directory, /etc/opencode on Linux, /Library/Application Support/opencode on macOS, %ProgramData%\opencode on Windows, and refuses when that directory configures any plugin source: a policies rule with action exactly plugin.use and an effect other than deny, any plugin listed in plugin/plugins (add operations only), or any entry under plugin/ or plugins/. System opencode.json/opencode.jsonc is parsed JSONC-tolerant, and an unparseable config or unreadable directory fails closed. A missing directory passes. The project plugin.use deny stays as the second layer, and the docstring that treated a system allow as an accepted exception is corrected.

Tests: new fake-binary runs cover system plugin plus system allow (auth, preflight and inference all fail closed, with no probe markers at all), system plugin without allow (passes, project deny blocks), and no system sources (passes), plus direct gate unit tests for allow/ask/deny policies, listed and installed plugins, removal-only entries, JSONC parsing, unparseable config and unlistable directories. The regression test fails with the gate calls removed and passes with them. tests/provider/test_opencode_cli.py: 148 passed. ruff check and ruff format --check clean.

P2: pinned _probe_opencode_version to (1, 18, 33) in TestOpencodeAuthCheck, TestOpencodeVersionRange and TestOpencodeDenyAllPolicy, and to (2, 2, 6) in TestOpencodeVersionRangeV2, so the suite no longer depends on which OpenCode binary a developer has installed. I verified the fragility directly: with the probe returning (2, 2, 7), the 1.x flag assertion fails, which is what the pins prevent.

PR body and README aligned with the verified behavior: OPENCODE_CONFIG_CONTENT is honoured on 2.2.7, OPENCODE_DISABLE_PROJECT_CONFIG=1 disables the project file on 2.x, and OPENCODE_PURE is not in the 2.2.7 binary, so 2.x plugin isolation rests on the project policy deny plus the new gate.

Two residual risks I want to flag openly: macOS MDM preference policies outrank even the system directory, and org integration / well-known document policies outrank project policies. Neither is observable pre-start from SkillSpector, so the gate cannot see them. The preflight still catches any layering outcome in the resolved agent permission list, but only for the selected agent.

@yashrajp22 yashrajp22 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for adding the isolated auth command and restoring the 1.x project-config flag. The earlier plugin-isolation request remains open: MDM and organization policies can still override the project deny. The new system-source gate also needs the corrections below.

Comment on lines +974 to +976
while lookahead < length and text[lookahead] in " \t\r\n":
lookahead += 1
if lookahead < length and text[lookahead] in "}]":

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please account for comments when deciding whether a comma is trailing. For example, the valid JSONC {"policies": [], /* note */} keeps its comma here because the next non-whitespace character is /. The comment is removed later, leaving {"policies": [], }, which json.loads rejects. Both supported OpenCode releases accept this syntax, so a harmless system config now makes authentication and inference fail. Skip comment trivia during the lookahead, or strip comments before removing trailing commas.

Comment on lines +547 to +548
_OPENCODE_SYSTEM_CONFIG_DIR = os.path.join(
os.environ.get("ProgramData", r"C:\ProgramData"), "opencode"

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please derive this directory from the same environment passed to OpenCode. Here it uses the parent ProgramData, but _scrub_env() drops that variable because PROGRAMDATA is absent from _RUNTIME_ENV_NAMES. OpenCode then falls back to C:\ProgramData. With a parent value such as D:\ProgramData, the gate can pass after inspecting D: while the child loads an allowed system plugin from C:. Keep the inspected path and the child's effective path consistent so the startup guard cannot miss that configuration.

Comment on lines +995 to +997
with open(path, encoding="utf-8") as handle:
raw = handle.read()
except OSError as exc:

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please convert decoding failures to AgentCLIError here too. A system config containing invalid UTF-8 raises UnicodeDecodeError from handle.read(), which is not an OSError. It then escapes _opencode_auth_check() and is_llm_available() instead of returning (False, reason). MCP preflight and report generation expect that availability result, so a damaged config can abort the scan/report instead of producing the normal unavailable-provider explanation.

Comment on lines +434 to +441
@pytest.fixture(autouse=True)
def _pin_probe_version_2x(self, monkeypatch: pytest.MonkeyPatch) -> None:
# These tests mock subprocess.run but the Popen-based version
# probes in _build_opencode_argv and _prepare_opencode_env are
# real: pin the probe to 2.2.6 so the whole auth check runs in a
# consistent 2.x world instead of depending on which binary a
# developer has installed. The mocked run version below agrees.
monkeypatch.setattr(_agent_cli, "_probe_opencode_version", lambda _binary: (2, 2, 6))

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please point this fixture and the ordinary 2.x fake-binary fixtures at an empty temporary system directory. Pinning the version and mocking the commands still leaves _check_opencode_system_sources() reading the real host directory. On a developer machine with a system plugin, it returns unavailable before the mocked auth command runs, breaking the success and argv expectations. The dedicated configured-source cases can override that isolated default.

- JSONC trailing commas: skip comment trivia in the lookahead, so
  {"policies": [], /* note */} parses instead of leaving a trailing
  comma for json.loads to reject.
- ProgramData: derive the system directory from the child environment
  and keep PROGRAMDATA in the scrubbed env, so the gate inspects the
  directory the child resolves.
- Decode failures: convert UnicodeDecodeError in the system config
  read to AgentCLIError, so the auth check returns (False, reason).
- Tests: point the 2.x fixtures at an empty temporary system
  directory, so results never depend on the host system directory.

Signed-off-by: Varshith Puli <pulivarshit@gmail.com>
@Varshith-Kali

Copy link
Copy Markdown
Contributor Author

Thanks for the review, all four are fixed in 89e91f3:

  1. Trailing comma before a comment: the JSONC stripper now skips comment trivia (line and block comments) in the trailing-comma lookahead, so {"policies": [], /* note */} parses instead of leaving a trailing comma for json.loads to reject. Regression tests cover block comments, line comments, and a comma before a comment that precedes another key (kept).

  2. ProgramData mismatch: the gate now derives the system directory from the child environment passed to OpenCode (_opencode_system_config_dir), and PROGRAMDATA is kept in the scrubbed env, so the inspected path is the path the child resolves. Tests cover the D:\ProgramData case and the C:\ProgramData fallback.

  3. Decoding failures: _load_opencode_system_config now converts UnicodeDecodeError to AgentCLIError, so the auth check returns (False, reason) instead of raising. Covered at the gate and at the auth probe.

  4. Test isolation: the 2.x fixtures (TestOpencodeVersionRangeV2, TestOpencodeDenyAllPolicyV2, TestOpencodeAuthProbeIsolation, TestOpencodeAncestorProject) now point the gate at an empty temporary system directory; the dedicated configured-source cases override that default.

Tests: tests/provider/test_opencode_cli.py, 156 passed (8 new). One pre-existing failure in tests/unit/test_providers.py (Anthropic bearer test, httpx URL parse) fails on the base commit too and is unrelated.

@yashrajp22 yashrajp22 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the JSONC and UTF-8 fixes. The Windows directory request still needs a case-insensitive lookup: Python exposes PROGRAMDATA, but _scrub_env() creates a plain dictionary and the new helper asks for ProgramData. With PROGRAMDATA=D:\ProgramData, the guard checks C: while OpenCode reads D:, so a system plugin allowed in D: can bypass the startup check. Please normalize the key lookup and match OpenCode's fallback for an empty value.

The fixture-isolation request also remains on Windows: passing child_env makes the helper bypass _OPENCODE_SYSTEM_CONFIG_DIR, so patching that constant still leaves the auth fixtures reading the host directory. Please isolate the effective lookup.

The earlier plugin-isolation request remains open because higher-priority MDM and organization policies can override the project deny.

- Windows directory: look ProgramData up case-insensitively in the child
  environment, and fall back to C:\ProgramData on an empty value, matching
  OpenCode's `env.ProgramData || "C:\\ProgramData"`, so the gate inspects
  the directory the child resolves.
- Fixture isolation: the 2.x fixtures now patch the effective lookup
  _opencode_system_config_dir, which the gate resolves through, instead of
  the module constant that the child-env derivation bypasses on Windows.
- MDM policies: the gate now also inspects the macOS MDM managed-preference
  plist (ai.opencode.managed.plist), refusing when it allows plugin.use or
  lists a plugin. There is no supported way to suppress managed sources in
  2.2.6/2.2.7 (the server never passes options.system), so the boundary is
  enforced by refusal.
- Tests: 12 new tests in tests/provider/test_opencode_cli.py (168 passed).

Signed-off-by: Varshith Puli <pulivarshit@gmail.com>
@Varshith-Kali

Copy link
Copy Markdown
Contributor Author

Thanks for the review, all three are addressed in c097354:

  1. Windows directory: _opencode_system_config_dir now looks ProgramData up case-insensitively in the child environment (the scrubbed env is a plain dict, so PROGRAMDATA was missed and the gate fell back to C: while OpenCode read D:), and an empty value falls back to C:\ProgramData, matching OpenCode env.ProgramData || "C:\\ProgramData" in the pinned discovery.ts. Covered by test_system_dir_lookup_is_case_insensitive and test_system_dir_empty_programdata_falls_back; the old code was verified to miss the D: allow and to resolve an empty value to a relative opencode path.

  2. Fixture isolation: the gate resolves through _opencode_system_config_dir only (production code never reads the constant directly), so the 2.x fixtures in TestOpencodeVersionRangeV2, TestOpencodeDenyAllPolicyV2, TestOpencodeAuthProbeIsolation, and TestOpencodeAncestorProject now patch that helper instead of the constant, and the configured-source cases override it the same way. Covered by test_effective_lookup_is_the_helper_not_the_constant, which shows the gate refusing on a configured host directory until the effective lookup is patched to the empty temp dir.

  3. MDM and organization policies: verified against the pinned 2.2.6/2.2.7 source (discovery.ts, config.ts, server-process.ts) that there is no supported way to suppress managed sources: no CLI flag and no environment variable sets options.system, and the server bootstrap never passes it, so MDM preferences always load on macOS when deployed. Suppression is not possible in this PR, so the boundary is enforced by refusal instead: the gate now also inspects the MDM managed-preference plist (/Library/Managed Preferences/<user>/ai.opencode.managed.plist and the machine-wide one, first present wins) with the same rules as the system directory, refusing when it allows plugin.use or lists a plugin, and failing closed when a present plist cannot be read or parsed. A missing plist passes, and the check runs even when the system directory is missing. The organization-policy vector through well-known integration documents cannot reach the child either: those documents need a stored credential, and the child runs with an isolated home that has none. Covered by 9 new MDM tests. One residual, stated plainly: a managed document that only changes non-plugin settings still loads, but it cannot inject a plugin, and the preflight still verifies the resolved agent permissions through debug agents.

Tests: tests/provider/test_opencode_cli.py, 168 passed (12 new); tests/provider, 168 passed, 4 deselected; ruff check and ruff format clean.

@yashrajp22 yashrajp22 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Windows production lookup now handles uppercase and empty ProgramData values. Please address the remaining managed-preference lookup and platform-specific assertions below.

"""
if username is None:
try:
username = getpass.getuser()

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we make this lookup match the username used by the OpenCode child? Python's getpass.getuser() prefers LOGNAME over USER, while the bundled Bun 1.4.4 runtime uses USER on macOS. With LOGNAME=alice and USER=bob, this guard checks Alice's plist while OpenCode loads Bob's. If Alice's/global plist is absent and Bob's plist lists an external plugin directory with a matching plugin.use allow, the guard passes and that plugin can load before the agent-permission check. Please use the supported child's effective identity and environment, or refuse startup when that path cannot be established.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, fixed. The guard now reads USER from the environment passed to the OpenCode child, which is what Bun's os.userInfo resolves to, instead of getpass.getuser() which prefers LOGNAME. If USER is missing or blank in the child environment, startup is refused rather than guessing a user. Added regression tests covering LOGNAME alice with USER bob (Bob's allow plist now refuses, Alice's alone passes) and the missing/blank USER refusal.

Comment thread tests/provider/test_opencode_cli.py Outdated
Comment on lines +1641 to +1642
monkeypatch.setattr(_agent_cli, "_opencode_managed_preference_paths", lambda: [str(plist)])
_check_opencode_system_sources()

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please explicitly set a non-macOS platform for this case. On macOS, sys.platform remains darwin, so the gate reads the allow-policy plist created above and correctly raises AgentCLIError. The test therefore fails instead of exercising the off-Darwin branch.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, the test now sets the platform to linux explicitly so it exercises the off-Darwin branch instead of relying on the host platform.

Comment thread tests/provider/test_opencode_cli.py Outdated
Comment on lines +1663 to +1665
assert _opencode_managed_preference_paths("someuser") == [
"/Library/Managed Preferences/someuser/ai.opencode.managed.plist",
"/Library/Managed Preferences/ai.opencode.managed.plist",

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we make these expectations portable? On Windows, the helper uses os.path.join and inserts backslashes, so these fixed POSIX strings do not match. The earlier test_scrub_env_keeps_programdata also asks a plain dictionary for ProgramData even though Windows stores that key as PROGRAMDATA. Use platform-appropriate path expectations (or scope the Darwin-specific case) and normalize the environment-key assertion so both cases work on Windows.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. The plist path expectations are built with os.path.join now, and the ProgramData check matches the key case-insensitively, so both hold on Windows.

…ser (NVIDIA#1086)

The MDM plist guard derived its username from getpass.getuser(),
which prefers LOGNAME, while the OpenCode child (Bun 1.4.4
os.userInfo) resolves its username from USER. With LOGNAME alice
and USER bob the guard checked Alice's plist while OpenCode loaded
Bob's, so a plugin.use allow in Bob's plist passed the gate.

Derive the username from the child environment instead: the gate
now reads USER from the environment passed to the child (falling
back to the parent environment when no child env is given) and
refuses startup when USER is missing or blank, since the child's
effective identity cannot then be established. The explicit
username override is unchanged.

Tests: force a non-macOS platform in the off-Darwin plist test so
it exercises that branch on macOS too, and make the plist path and
ProgramData expectations portable (os.path.join expectations and a
case-insensitive environment key match) so they hold on Windows.

Signed-off-by: Varshith Puli <pulivarshit@gmail.com>

@rng1995 rng1995 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[SkillSpector Review]

Decision — Request changes

Value and readiness — The pinned 2.x path addresses the earlier version-test, standalone process, ProgramData, and macOS username findings. Its pre-start gate still differs from the supported runtimes' managed configuration lookup and policy decoding.

Material findings

  • [P0] src/skillspector/providers/_agent_cli.py:1225–1246 uses case-sensitive string membership on the directory listing before it probes the managed config and plugin paths. OpenCode 2.2.6 and 2.2.7 open those fixed paths through the filesystem. On Windows and case-insensitive macOS volumes, a differently cased config filename can carry both an allow and an external plugin while the gate skips it. Probe the canonical pathnames under the child's filesystem semantics and fail closed on access errors. Verify a case-insensitive fixture refuses before auth, preflight, or inference starts. OpenCode directory loader
  • [P0] src/skillspector/providers/_agent_cli.py:1103–1115 checks the raw action string only. Both pinned versions decode legacy integration.use statements with a plugin: resource as plugin.use. A managed allow in that form can authorize an external plugin discovered from another layer even when the managed document itself lists zero plugins. Apply the pinned decoder's semantics before checking the action, or conservatively refuse ambiguous managed policy before startup. Add a pre-import marker regression. OpenCode policy schema
  • [P1] src/skillspector/providers/_agent_cli.py:807,1614–1669,2015–2028 gives both auth and inference a fresh temporary OPENCODE_DB. In both pinned versions, the server uses that path and the credential service queries that database. A stored login in the user's normal 2.x database is therefore outside the probe's credential store. Establish a supported isolated credential path and exercise it with an actual authenticated 2.x runtime. A fake non-empty JSON response verifies parsing only. OpenCode server database path · OpenCode credential service

PIC tradeoffs — Plugin isolation needs to hold before server startup. The selected agent's resolved deny list can confirm tool policy only after startup, and sharing a credential store can reopen org-supplied configuration. Keep 2.x fail-closed until both the pre-import boundary and authenticated provider flow are established.

Verification and gaps — Reviewed the current PR delta against the prior request and traced startup against the published v2.2.6 and v2.2.7 source. The author addressed the latest ProgramData, USER versus LOGNAME, and portable-test feedback. Exact-head hosted CI succeeded, with 26,270 unit tests passed. The hosted run and author-provided fake-binary fixtures provide no authenticated live inference result. Local review was static. Hosted CI

Reviewed head: a3c989af04b096df727d48445946178c5a76cdda

…DIA#1086

The system-source gate matched opencode.json, opencode.jsonc, plugin and plugins against os.listdir output with case-sensitive string membership, while OpenCode opens the fixed pathnames through the filesystem. On Windows and case-insensitive macOS volumes a differently cased file (OpenCode.JSON, Plugins) is the config OpenCode loads, so the gate skipped it and a managed allow plus external plugin could bypass the pre-start check.

Probe each canonical pathname with os.stat instead, so the filesystem's own case semantics decide what the child would open, and fail closed on stat errors other than not present. The directory listing is kept only as the access probe for a missing or unlistable directory.

Tests: case-insensitive volume fixture refuses a differently cased allow config (opencode.json and opencode.jsonc spellings) and a differently cased Plugins directory, with a control showing the same names pass on a case-sensitive filesystem where OpenCode never opens them.
Signed-off-by: Varshith Puli <pulivarshit@gmail.com>
@Varshith-Kali

Copy link
Copy Markdown
Contributor Author

Fixed the case-sensitive gate (review 5977061517, first P0). _check_opencode_system_directory no longer matches opencode.json, opencode.jsonc, plugin, and plugins against the os.listdir output: that is a case-sensitive string comparison on on-disk casing, while OpenCode opens the fixed pathnames through the filesystem (pinned config.ts loadDirectory over discovery.ts names). On Windows and case-insensitive macOS volumes a differently cased file is the config OpenCode loads, so the gate skipped it.

Each name is now probed as its canonical pathname with os.stat, so the filesystem's own case semantics decide what the child would open, and a stat failure other than not present fails closed. The listing is kept only as the access probe for a missing or unlistable directory. A differently cased plugin directory is handled the same way.

Tests: a new case-insensitive volume fixture refuses a differently cased allow config (OpenCode.JSON and OPENCODE.JSONC spellings) and a differently cased Plugins directory, with a control showing the same names pass on a case-sensitive filesystem where OpenCode never opens them. Without the fix the three refusal tests fail; with it the full test_opencode_cli.py file passes (175 passed), ruff check and format clean. Pushed as 8ab154b.

The other two findings in this review are not covered by this commit: the legacy integration.use decoding (second P0) and the isolated credential database (P1) are still open.

…e 2.x gate (NVIDIA#1086)

Pinned OpenCode 2.2.6/2.2.7 policy.ts decodes a legacy integration.use
statement with a plugin: resource as plugin.use, so a managed allow in
that form authorized an external plugin while the gate saw no plugin.use
rule. The gate now applies the same decoder before checking the action,
and refuses ambiguous managed policy (a non-string action, or an
integration.use rule with a non-string resource) instead of passing it.

Verified live on real 2.2.7: with a legacy allow in /etc/opencode and a
marker plugin in /etc/opencode/plugins, debug agents --standalone imports
the plugin (marker appears) while the probe agent still resolves under
the project deny; without the allow the same plugin does not import.
The fixed gate refuses that config before any server starts.

Tests: 9 new (8 direct gate cases, 1 pre-import marker regression across
preflight, inference, and auth); tests/provider/test_opencode_cli.py, 184
passed; ruff check and format clean.

Signed-off-by: Varshith Puli <pulivarshit@gmail.com>
@Varshith-Kali

Copy link
Copy Markdown
Contributor Author

Thanks for the review. The legacy policy finding is fixed in 1eb8839, and the P1 is queued with the reason below.

  1. Legacy integration.use (second P0): the gate now applies the pinned decoder before checking the action (_opencode_effective_policy_action, mirroring policy.ts: an integration.use rule with a plugin: resource decodes to plugin.use, mcp: and skill: decode to their own actions, a bare resource keeps integration.use, and the effect is preserved). Ambiguous managed policy refuses instead of passing (a non-string action, or integration.use with a non-string resource). Verified live on real 2.2.7: a legacy allow in /etc/opencode plus a marker plugin in /etc/opencode/plugins gets the plugin imported during debug agents --standalone while the probe agent still resolves under the project deny; with the same plugin and no managed allow, nothing imports. The fixed gate refuses that config before any server starts. 9 new tests, including a pre-import marker regression across preflight, inference, and auth: tests/provider/test_opencode_cli.py, 184 passed; ruff check and format clean.

  2. Isolated credential database (P1): queued, not faked. I verified from the pinned source that the finding is real (databasePath honors OPENCODE_DB and the credential service reads that database), but the fix needs a live authenticated 2.x runtime to verify, and there is none available here: this machine has no stored 2.x login (its local opencode.db holds zero credentials) and no authenticated account to test against. The candidate paths are a snapshot copy of the real database into the temp dir (isolated writes, real credentials readable) or pointing OPENCODE_DB at the live database (a supported variable, but the probe server would write into the live user database, and sharing the store is the tradeoff your PIC note flags). Either path needs a real logged-in 2.2.7 run to prove a stored login is actually seen and usable; a synthetic database row would only prove parsing, so I did not ship one. Happy to implement the copy path once a live authenticated runtime is available to verify against, or take direction on which tradeoff to take.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

opencode_cli on OpenCode 2.x: the 1.x deny-all policy is ignored; a project opencode.jsonc resolves to deny-all

3 participants