Skip to content

refactor(ocm): pair linked servers with native OpenCode links and drop legacy CLI stubs - #498

Merged
chriswritescode-dev merged 4 commits into
mainfrom
refactor/ocm-native-opencode
Nov 29, 2026
Merged

chriswritescode-dev merged 4 commits into
mainfrom
refactor/ocm-native-opencode

Conversation

@chriswritescode-dev

@chriswritescode-dev chriswritescode-dev commented Nov 29, 2026 •

Copy link
Copy Markdown
Owner

User description

Summary

OpenCode now ships its own pairing (opencode pair), so the Manager no longer needs ocm-cli's custom server-linking protocol.

  • Linked servers are paired with OpenCode's native links. The Manager accepts either the https://<host>/auth/connect/<code> link or the QR JSON ({code, urls, name?}) that opencode pair prints, redeems the code for a session token, and stores it as the server password.
  • OpenCode session tokens expire after 30 days, so the Manager renews any paired token within 7 days of expiry (on startup and every 12 hours) using POST /api/pair with the current token. Plain passwords are never touched, and a failed renewal is logged and retried while the old token is still valid.
  • Remove ocm link, its one-shot /redeem listener, the ocm://link server-link schema and the HMAC/AES-GCM pairing crypto, along with their tests and the uqr dependency. The Manager app's own ocm://link pairing for the mobile app is unchanged.
  • Remove the 410 stubs that only told ocm < 0.4.0 to upgrade (POST /mirror, GET/POST /mirror/target).
  • ocm-cli tests always use the file token store, so running them on macOS no longer prompts for the Keychain.
  • The server dialog and ocm-cli README point to opencode pair --remote (or opencode pair on the same network).

Tests

Registry tests cover link and QR parsing, redemption, renewal inside and outside the window, plain passwords, disabled servers and renewal failures. The event hub test covers renewal on start, every 12 hours, and stopping on shutdown. Typecheck and lint pass. Frontend (4126) and ocm-cli (437) tests pass. Backend tests pass except the macOS-only sandbox suites and the real-binary OAuth/provider tests that #497 fixes for OpenCode 2.4.


PR Type

Enhancement, Tests


Description

  • Adopt native OpenCode pairing instead of custom protocol

    • Accept https://…/auth/connect/<code> links and QR JSON payloads
    • Redeem the connect code and store the session token as password
  • Renew pairing tokens automatically

    • Renew within 7 days of expiry on start and every 12 hours
    • Skip plain passwords and disabled servers, guard concurrent edits
  • Remove legacy code paths

    • Delete ocm link, the /redeem listener and HKDF/AES-GCM crypto
    • Drop the 410 stubs for POST /mirror and /mirror/target
  • Update dialog copy, README and tests for the new flow


Diagram Walkthrough

flowchart LR
  A["Native connect link or QR JSON"] -- "parse connect code" --> B["OpenCodeConnectPairing schema"]
  B -- "GET /auth/connect/:code" --> C["Session token stored as password"]
  C -- "within 7 days of expiry" --> D["Renew via POST /api/pair"]
  D -- "every 12 hours" --> E["RemoteServerEventHub timer"]
  F["ocm link, pairing crypto, 410 stubs"] -- "deleted" --> G["Removed legacy code and deps"]
Loading

File Walkthrough

Relevant files
Miscellaneous
5 files
repo-mirror.ts
Remove legacy 410 mirror upgrade stubs                                     
+0/-15   
ocm.ts
Remove ocm link command and its usage entries
+0/-171 
link.ts
Delete one-shot pairing server and service discovery         
+0/-394 
index.ts
Remove pairing crypto exports from shared package               
+0/-17   
pairing.ts
Delete HMAC proof and AES-GCM pairing module                         
+0/-168 
Enhancement
4 files
opencode-server-registry.ts
Redeem native connect codes and renew pairing tokens         
+110/-28
remote-server-events.ts
Schedule pairing token renewal on start and interval         
+17/-1   
OpenCodeServerDialog.tsx
Parse native connect links and update pairing guidance     
+17/-13 
opencode-servers.ts
Replace server link schema with connect link parser           
+81/-27 
Tests
8 files
opencode-server-registry.test.ts
Cover native pairing parsing and token renewal paths         
+179/-105
repo-mirror.test.ts
Drop tests for removed legacy mirror stubs                             
+0/-36   
opencode-servers.test.ts
Assert pairing failures use native connect endpoint           
+2/-2     
remote-server-events.test.ts
Test renewal timer start, interval and shutdown                   
+34/-0   
manager-app-link.test.ts
Replace opencode link tests with connect link tests           
+82/-24 
OpenCodeServerDialog.test.tsx
Test native link prefill, errors and renewal note               
+12/-28 
cli-home.ts
Force linux platform to avoid Keychain prompts                     
+3/-0     
link.test.ts
Delete tests for removed ocm link module       
+0/-506 
Documentation
1 files
README.md
Document opencode pair workflow and drop ocm link
+7/-56   
Dependencies
1 files
package.json
Drop unused uqr QR rendering dependency         
+0/-1     

body: JSON.stringify({ nonce, proof }),
headers: {
Accept: 'application/json',
Authorization: buildOpenCodeBasicAuth(token, OPENCODE_SERVER_USERNAME),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Hardcoded username

Renewal always authenticates as OPENCODE_SERVER_USERNAME, not the server's stored username. A server whose username was edited after pairing, or that has a manually entered password matching the token pattern under a custom username, fails renewal with 401 on every cycle.

@lil-worker-cstech

lil-worker-cstech Bot commented Nov 29, 2026 •

Copy link
Copy Markdown
Contributor

PR Reviewer Guide 🔍

(Review updated until commit 6c6011e)

Since the last review — 3 findings were open on the previous HEAD

  • ✅ 2 addressed by the pushes since then: backend/src/services/opencode-server-registry.ts:326-328, backend/src/services/opencode-server-registry.ts:323-324
  • ⏳ 1 still open

Here are some key observations to aid the review process:

⚠️ Risk checks: 82 finding(s) detected
Sensitive paths (2): ocm-cli/package.json, pnpm-lock.yaml
Risky shapes (77): Removed JS/TS function in backend/src/services/opencode-server-registry.test.ts, Removed JS/TS function in backend/src/services/opencode-server-registry.test.ts, Hardcoded password assignment in backend/src/services/opencode-server-registry.test.ts, Hardcoded password assignment in backend/src/services/opencode-server-registry.test.ts, Hardcoded password assignment in backend/src/services/opencode-server-registry.test.ts, Hardcoded password assignment in backend/src/services/opencode-server-registry.test.ts, Hardcoded password assignment in backend/src/services/opencode-server-registry.test.ts, Hardcoded password assignment in backend/src/services/opencode-server-registry.test.ts, Hardcoded password assignment in backend/src/services/opencode-server-registry.test.ts, Hardcoded password assignment in backend/src/services/opencode-server-registry.test.ts (+67 more)
Deleted files (3): ocm-cli/src/link.ts, ocm-cli/test/link.test.ts, shared/src/opencode/pairing.ts
⏱️ Estimated effort to review: 3 🔵🔵🔵⚪⚪
⚠️ Risk level: Medium
✅ Merge recommendation: Needs review
Merge recommendation checks
✅ CI on reviewed head: passed
✅ Head is current with its base: up to date
✅ Human authors: all authors are human
⚠️ No sensitive paths or risky changes: 82 finding(s)
⚠️ Changed lines within cap: 2139 changed lines exceeds cap 100
🔎 Failure modes

What: Token renewal fails with 401 on every 12-hour cycle. The token then expires after 30 days and the linked server disconnects, even though the stored credentials were valid.
Where: backend/src/services/opencode-server-registry.ts:pairWithToken
Trigger: A server stores a pairing-shaped token under a username other than `opencode`. For example, the username was edited after pairing.
Detected by: A repeated `Failed to renew the pairing token` warning in the logs. No test covers a custom username.
Covered in this PR: No

What: A plain password shaped like `<digits>.<base64url>` is treated as a pairing token. It is either sent to `/api/pair` for renewal or reported as an expired token every 12 hours.
Where: backend/src/services/opencode-server-registry.ts:renewPairingToken
Trigger: A user enters a manual password such as `1234.secret` or `1730000000.abc`.
Detected by: A misleading "has expired; pair the server again" warning in the logs.
Covered in this PR: No

What: A QR payload is rejected as a whole when any entry in `urls` is not a bare http(s) origin, even if the first URL is valid.
Where: shared/src/schemas/opencode-servers.ts:parseConnectPayload
Trigger: `opencode pair` prints a QR payload where a secondary URL carries a path or uses another scheme.
Detected by: The user sees the "not a valid OpenCode pairing link" error. Tests only cover payloads whose URLs are all invalid.
Covered in this PR: No

🧪 PR contains tests
🔒 No security concerns identified
✅ Resolved findings

backend/src/services/opencode-server-registry.ts:323-324

Silent expiry

When a pairing token has already expired (remaining <= 0), the function returns without logging. The token can no longer be renewed, so the server breaks with no signal that it must be paired again. Log a warning so operators can find the cause.

backend/src/services/opencode-server-registry.ts:326-328

Renewal race

renewPairingToken reads the stored token, awaits pairWithToken (two HTTP calls with up to a 10s timeout each), then calls updateOpenCodeServer unconditionally. If update() changed the password, pairing, or base URL during that wait, the renewed token from the old server replaces the user's new credentials. Re-read the stored password and the server's base URL just before writing, and skip the write if either changed.

@lil-worker-cstech

Copy link
Copy Markdown
Contributor

Persistent review updated to latest commit 6c6011e

@chriswritescode-dev
chriswritescode-dev merged commit 55f5864 into main Nov 29, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant