Skip to content

fix(opencode): pass missing auth headers in run --attach#16097

Open
ericclemmons wants to merge 5 commits intoanomalyco:devfrom
ericclemmons:fix/run-attach-auth
Open

fix(opencode): pass missing auth headers in run --attach#16097
ericclemmons wants to merge 5 commits intoanomalyco:devfrom
ericclemmons:fix/run-attach-auth

Conversation

@ericclemmons
Copy link

@ericclemmons ericclemmons commented Mar 5, 2026

Issue for this PR

Closes #16096

Type of change

  • Bug fix
  • New feature
  • Refactor / code improvement
  • Documentation

What does this PR do?

Adds missing Basic auth headers to run command, just like attach.

How did you verify your code works?

Relied on tests (that I've since removed to be consistent with the other patterns) and comparing run against opencode serve vs. one with Basic auth (like what OpenCode.app starts):

  1. opencode --attach http://127.0.0.1:45103 --dir ~/workspace run howdy

  2. OPENCODE_SERVER_USERNAME=opencode OPENCODE_SERVER_PASSWORD=hunter2 opencode --attach=localhost:4096 run howdy

    Error: Session not found

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

Notes

I originally started refactoring out a getAuthorizationHeader utility based on

function getAuthorizationHeader(): string | undefined {
, but opted for inlining & duplicating what was in
const headers = (() => {
const password = args.password ?? process.env.OPENCODE_SERVER_PASSWORD
if (!password) return undefined
const auth = `Basic ${Buffer.from(`opencode:${password}`).toString("base64")}`
return { Authorization: auth }
})()

I did use AI to author this because I was in the middle of another project when I finally asked Claude "Wait, is this an opencode bug??" and wound up here.

I'm sorry. 😔

@github-actions
Copy link
Contributor

github-actions bot commented Mar 5, 2026

The following comment was made by an LLM, it may be inaccurate:

Based on my search, I found one potentially related PR:

feat(github-action): Support remote server connection via --attach (#15866)

However, this appears to be a feature addition for GitHub Actions rather than a direct duplicate of the auth headers bug fix. The current PR (#16097) is specifically addressing a bug where auth headers are missing when using run --attach, while #15866 focuses on enabling remote server connection support in GitHub Actions.

No duplicate PRs found

Fixes anomalyco#16096

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@ericclemmons ericclemmons force-pushed the fix/run-attach-auth branch from c8a6687 to 35405a3 Compare March 5, 2026 04:27
ericclemmons and others added 4 commits March 4, 2026 22:28
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Consolidates auth header logic from worker.ts and attach.ts into a
shared util used by run.ts, attach.ts, and worker.ts.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Match existing inline pattern from attach.ts instead of extracting.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Match attach.ts pattern: accept --password/-p flag and fall back to
OPENCODE_SERVER_PASSWORD env var.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Comment on lines +283 to +287
.option("password", {
alias: ["p"],
type: "string",
describe: "basic auth password (defaults to OPENCODE_SERVER_PASSWORD)",
})
Copy link
Author

@ericclemmons ericclemmons Mar 5, 2026

Choose a reason for hiding this comment

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

So that we're consistent with attach:

.option("password", {
alias: ["p"],
type: "string",
describe: "basic auth password (defaults to OPENCODE_SERVER_PASSWORD)",
}),

Comment on lines +656 to +661
const headers = (() => {
const password = args.password ?? process.env.OPENCODE_SERVER_PASSWORD
if (!password) return undefined
const auth = `Basic ${Buffer.from(`opencode:${password}`).toString("base64")}`
return { Authorization: auth }
})()
Copy link
Author

Choose a reason for hiding this comment

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

This is modeled after

const headers = (() => {
const password = args.password ?? process.env.OPENCODE_SERVER_PASSWORD
if (!password) return undefined
const auth = `Basic ${Buffer.from(`opencode:${password}`).toString("base64")}`
return { Authorization: auth }
})()

if (args.attach) {
const sdk = createOpencodeClient({ baseUrl: args.attach, directory })
const headers = (() => {
const password = args.password ?? process.env.OPENCODE_SERVER_PASSWORD
Copy link
Member

Choose a reason for hiding this comment

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

This still hardcodes "opencode", so run --attach will keep failing against servers using a non-default OPENCODE_SERVER_USERNAME. Can we read the username from env here (like worker.ts) instead of only fixing the password path?

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 run --attach ignores OPENCODE_SERVER_PASSWORD

2 participants