Skip to content

fix(opencode): read usage from each instance data directory - #17759

Open
fixfon wants to merge 2 commits into
pingdotgg:mainfrom
fixfon:fix/opencode-instance-usage-roots
Open

fixfon wants to merge 2 commits into
pingdotgg:mainfrom
fixfon:fix/opencode-instance-usage-roots

Conversation

@fixfon

@fixfon fixfon commented Oct 10, 2026 •

Copy link
Copy Markdown
Contributor

Problem

OpenCode usage scans received configured provider instances but resolved data directories from the host environment once. History from instances with their own OPENCODE_DATA_DIR or XDG_DATA_HOME was omitted.

Change

Resolve roots from each instance's already merged environment, then deduplicate canonical paths across all instances before scanning. Preserve the existing comma-separated override precedence, home expansion, XDG fallback, and history reader.

Fixes #17631.

Scope and approval

#17631 was filed by maintainer Julius and specifies per-instance root resolution, canonical deduplication, and a two-instance regression test. This PR implements that scope in the OpenCode scanner and focused provider/service usage tests. The portable alias regression extends the existing service-test junction fixture; it adds no diagnostic suppression.

Verification

macOS, Node 24.21.0; synthetic temporary SQLite stores, no live provider history.

  • Before the fix, the distinct-instance history regression failed because the scanner returned the unrelated host root. The default compatibility cases passed. The alias test also fails when only cross-instance root deduplication is disabled: it receives two scanner sources instead of one, before aggregation.
  • After the fix, the two instance stores both contribute distinct messages (200 combined input tokens); aliases yield one canonical source and one record. Absolute, relative, and absent XDG_DATA_HOME default cases pass. The existing SQLite/WAL and legacy migration test passes.
  • node node_modules/vite-plus/bin/vp test run packages/provider-opencode/src/server/usage.test.ts apps/server/src/usage/UsageService.test.ts — 33 tests passed across two files, independently rerun after commit.
  • node node_modules/typescript/bin/tsc --noEmit -p packages/provider-opencode/tsconfig.json and node node_modules/typescript/bin/tsc --noEmit -p apps/server/tsconfig.json — exit 0; existing Effect suggestions elsewhere, no errors.
  • node node_modules/vite-plus/bin/vp lint packages/provider-opencode/src/server/usage.ts packages/provider-opencode/src/server/usage.test.ts apps/server/src/usage/UsageService.test.ts and the equivalent vp fmt --check — passed. git diff --check passed.

The local Vite+ entry point was invoked with Node 24 directly because the global installation wrapper selected a different runtime. Windows was not executed; the alias fixture uses a directory junction for Windows compatibility.

Implemented with GPT-6.1-Sol through Codex desktop; reviewed independently through Codex.

@github-actions github-actions Bot added vouch:unvouched PR author is not yet trusted in the VOUCHED list. size:S 10-29 changed lines (additions + deletions). labels Oct 10, 2026
@macroscopeapp

macroscopeapp Bot commented Oct 10, 2026

Copy link
Copy Markdown
Contributor

Approvability

Verdict: Not approved

Macroscope's review found this PR not approvable — The production change is a focused, well-tested correction that makes usage scans honor each OpenCode instance’s existing environment and deduplicate shared roots. Human review is still required because the PR adds a file-level directive suppressing a static-analysis diagnostic.

You can add or adjust custom eligibility rules. Learn more.

@coderabbitai

coderabbitai Bot commented Oct 10, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: Path: .coderabbit.config.ts
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: ce66a2ef-3f12-49ab-8813-d6bc5eb6b923


📥 Commits

Reviewing files that changed from the base of the PR and between 72458d8 and d2990e8.



📒 Files selected for processing (2)
  • apps/server/src/usage/UsageService.test.ts
  • packages/provider-opencode/src/server/usage.test.ts


💤 Files with no reviewable changes (1)
  • packages/provider-opencode/src/server/usage.test.ts


Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 7 remain after this review.




📝 Walkthrough
📝 Walkthrough

Walkthrough

The OpenCode usage reader now resolves history directories from each provider instance’s environment, combines and deduplicates the resulting roots, and scans them. Tests cover per-instance overrides, directory aliases, host-default fallback, and usage aggregation.

Changes

OpenCode usage scanning

Layer / File(s) Summary
Resolve and scan per-instance data roots
packages/provider-opencode/src/server/usage.ts, packages/provider-opencode/src/server/usage.test.ts, apps/server/src/usage/UsageService.test.ts
The scan resolves roots from each instance’s environment and deduplicates directories before scanning. Tests cover environment overrides, aliases, default-directory behavior, and aggregated OpenCode usage.

Priority: ⬇️ Low

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix

Suggested reviewers: juliusmarminge



Merge Risk: ⚪ Minimal · up to d2990

No actionable merge-blocking risk is established for the OpenCode usage-scanning change. It is mergeable after normal checks.

Architecture Summary

Architecture risk: 🔵 Low · up to d2990

The change affects 2 systems.

Changed systems: packages/provider-opencode, apps/server

Architecture concerns
No architecture-level concerns identified.

Review details

Systems and components

  • observed — packages/provider-opencode (library) was modified; 2 changed files map to changed impact.
  • observed — apps/server (service) was modified; 1 changed file maps to changed impact.

Before / after behavior

  • observed — Modified behavior in packages/provider-opencode/src/server/usage.ts: resolveOpenCodeDataDirs now accepts a NodeJS.ProcessEnv argument and no longer obtains the environment from HostProcess.Environment.
  • observed — Modified behavior in packages/provider-opencode/src/server/usage.ts: The reader’s scan callback now resolves data directories separately for each instance from instance.environment and deduplicates the combined roots with a set, replacing the single global directory resolution.
  • observed — Modified behavior in apps/server/src/usage/UsageService.test.ts: Imports OpenCodeDriver for the aliased-roots test; the existing mergeUsage import is unchanged.
  • observed — Modified behavior in apps/server/src/usage/UsageService.test.ts: Adds an OpenCode message file under the real root used by the alias test, with assistant role, timestamp, model, and token data.


Pre-merge checks | Passed 4
✅ Passed checks (4 passed)
Check name Status Explanation
Linked Issues check Passed Issue #17631 requires per-instance OpenCode root resolution, canonical-path deduplication, and a two-instance regression test. The reported implementation resolves roots from each instance environment…
Out of Scope Changes check Passed The changes are limited to the OpenCode usage reader and focused provider and service tests. The service test verifies canonical scan deduplication and remains directly connected to issue #17631. No u…
Title check Passed The title clearly and concisely describes the main fix: reading OpenCode usage from each instance data directory.
Description check Passed The description includes the required Problem, Change, Scope and approval, and Verification sections. It explains the issue, implementation, linked issue, focused tests, toolchain checks, observed res…

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create a new PR



  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Comment @coderabbitai help to get the list of available commands.

@fixfon

fixfon commented Oct 10, 2026

Copy link
Copy Markdown
Contributor Author

Addressed the new diagnostic-suppression concern in d2990e8 by reusing the existing junction fixture in UsageService.test.ts. The cumulative PR now adds no diagnostic suppression or native filesystem import.

That fixture directly calls the public OpenCode usage reader with two instance environments and asserts one canonical scanner source before aggregation. Disabling only cross-instance root deduplication makes this assertion fail (two sources instead of one), so service-level record deduplication cannot hide duplicate scans. The distinct-root and default regressions remain in the provider tests. Production behavior is unchanged from the previous head.

Independently reran the final focused suites: 33/33 tests pass. Both affected-package typechecks and three-file lint/format pass. Updated the PR body and linked issue evidence to this final coverage. A scoped independent code re-review found the suppression concern addressed and no new findings.

@fixfon

fixfon commented Oct 10, 2026

Copy link
Copy Markdown
Contributor Author

@macroscope-app please re-run the approvability check on the current head (d2990e8). The "Not approved" verdict above was posted at 07:54 UTC for 72458d8, before d2990e8 moved the alias regression onto the existing junction fixture in UsageService.test.ts. The current cumulative diff adds no diagnostic suppression.

@macroscopeapp

macroscopeapp Bot commented Oct 10, 2026

Copy link
Copy Markdown
Contributor

Sorry, I'm unable to act on this request because you do not have permissions within this repository.

This branch has not been deployed

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

Labels

size:S 10-29 changed lines (additions + deletions). vouch:unvouched PR author is not yet trusted in the VOUCHED list.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Usage: OpenCode history ignores each instance's XDG_DATA_HOME / OPENCODE_DATA_DIR

1 participant