Skip to content

fix(core): say why a refused machine is offline, as data - #66867

Open
rekram1-node wants to merge 2 commits into
devfrom
machine-offline-reason
Open

rekram1-node wants to merge 2 commits into
devfrom
machine-offline-reason

Conversation

@rekram1-node

@rekram1-node rekram1-node commented Nov 27, 2026 •

Copy link
Copy Markdown
Member

Issue for this PR

No issue. Found while testing protocol 3 (#66457) against a worker that still speaks protocol 2. First step of the machine API data model rework.

Type of change

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

What does this PR do?

When a paired worker speaks a different link protocol than the server, the server refuses it right after the handshake and only logs why. The worker redials every second, and everything the user or an agent sees says just "offline", which reads like a network problem. This PR keeps the refusal per machine as data and shows it in machine.list and in the offline error.

 GET /api/experimental/machine
 { "id": "gpu", "online": false, "paired": true,
+  "refused": { "at": 1795800000000, "reason": { "type": "protocol-mismatch", "worker": 2, "home": 3 } } }

 shell / read / write on gpu:...
-gpu is offline, command not sent
+gpu is offline, command not sent: worker speaks protocol 2, this server needs 3; update opencode-worker on that machine

A worker on a newer protocol than the server gets …; update OpenCode on this server. A hello that passes the handshake but doesn't decode is recorded as { "type": "invalid-hello", "issue": "…" }.

How it works:

Link.parseHello(bytes)        pure: Accepted { hello } | Rejected { refusal }
  refusal = ProtocolMismatch { worker, home } | InvalidHello { issue }
Link.serve: handshake ok, hello Rejected
  -> Machines.refuse(machine, refusal)   stamps it: Machine.Refused { at, reason }
  -> log describeRefusal(refusal), close with 1008 as before
Machine driver keeps the Refused until the next connection is served, which clears it
NotSent (spawn, stdin, files, peer, home) carries it as `refused`; its message uses describeRefusal
Machines.list adds `refused` to an offline machine; the HTTP handler maps _tag to the API's `type`

describeRefusal in packages/core/src/machine/driver.ts is the only place a refusal becomes text, so the advice is computed when shown rather than frozen when the refusal is recorded. decodeHello is gone: the stdio worker client calls parseHello too, and both callers log through Link.reject.

Only refusals after the handshake are recorded: before it the server doesn't know which machine is calling, and nothing about the refusal is sent to the worker. Link.serveDirect (pinned-key links without Machines) records nothing. A worker that proves its key but never sends a hello is still only a timeout, not recorded.

The refusal stays until the machine next connects, so a machine that was refused and then switched off still shows it. That is still the last thing known about it.

API: refused is a new optional field on the experimental Machine.Info; nothing else in the API changes. OpenAPI, @opencode/client and the Rust api-types are regenerated.

Overlap with #66483 (worker self-update on mismatch): both touch the hello line in Link.serveWith. With this PR that becomes a match on parseHello's ProtocolMismatch, which is where the update request fits.

How did you verify your code works?

  • packages/core/test/machine-frames.test.ts: the existing "refuses protocol 2/4" tests (a fake worker sends an old or new hello over a real handshake) check the log line, machine.list's refused.reason and the NotSent message.
  • Core machine tests (machine*, tool-machine*, worker*): 220 pass, 2 fail. The 2 failures (keeps expanding a home directory for a local command with the flag off/on in machine-path.test.ts) fail the same way on origin/dev.
  • Server machine tests: 32 pass.
  • Rust CLI machine tests (bun run cargo test -p opencode-cli --bin opencode-native machine): 7 pass.
  • bunx tsc --noEmit in packages/core and packages/client; bunx tsc -p tsconfig.build.json --noEmit in packages/server, packages/sdk, packages/simulation: clean.

Screenshots / recordings

Not a UI change.

Checklist

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

Added by the opencode bot (AI-generated).

Public API: adds optional refused (timestamp + protocol-mismatch / invalid-hello reason) to experimental Machine.Info and Machine.NotSent (backwards compatible)

@opencodejr opencodejr Bot left a comment

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.

The fix works. I copied the PR's updated protocol 2/4 refusal tests onto the base, where they fail because offlineReason is missing; with this PR they pass. The other core machine tests pass, apart from the 2 machine-path home-directory tests, which fail the same way on the base. tsc is clean in core and server.

Public API (additive, backwards compatible):

  • Machine.Info (from GET machine list, MachineInfo in the SDK/client types) gains an optional offlineReason: string.
  • The Machine.NotSent error gains an optional reason, and its message becomes <id> is offline, command not sent: <reason> when a refusal is recorded.

Existing callers are unaffected.

Needs fixing before merge: packages/protocol/openapi.json wasn't regenerated. bun run check:generated in packages/protocol reports it as stale (it passes on the base), so the "Check generated OpenAPI document" CI step will fail. Running bun run generate there adds the offlineReason property.

Comment thread packages/protocol/src/groups/machine.ts Outdated
}),
),
/** Why the machine's last connection was refused, while it is offline since, such as a worker too old for this server. */
offlineReason: Schema.optionalKey(Schema.String),

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.

This new field isn't in packages/protocol/openapi.json yet. bun run check:generated here reports it as stale, which will fail the "Check generated OpenAPI document" CI step. Running bun run generate in packages/protocol fixes it.

@opencodejr opencodejr Bot left a comment

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.

The OpenAPI document is now regenerated with offlineReason. check:generated in packages/protocol passes. The code is the same as my last review, so the earlier results still hold. No other concerns.

Public API (unchanged since last review, additive and backwards compatible):

  • Machine.Info gains an optional offlineReason: string.
  • The Machine.NotSent error gains an optional reason.

@rekram1-node
rekram1-node force-pushed the machine-offline-reason branch from 84e8073 to 89b6a4f Compare November 27, 2026 22:53
@rekram1-node rekram1-node changed the title fix(core): say why a refused machine is offline fix(core): say why a refused machine is offline, as data Nov 27, 2026

@opencodejr opencodejr Bot left a comment

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.

The openapi thread is fixed: packages/protocol/openapi.json is regenerated and its check passes. I reviewed the reworked version (refusal stored as data, parseHello, the HTTP mapping) and found nothing blocking. Core machine tests pass, apart from the 2 machine-path home-directory tests that also fail on the base. tsc is clean in core and server, and the generated client check shows no diff. The 11 server test failures are the same on the base.

Public API (additive, backwards compatible):

  • Experimental Machine.Info (GET /api/experimental/machine, @opencode/client types, Rust api-types) gains an optional refused: { at: number (epoch ms), reason: { type: "protocol-mismatch", worker, home } | { type: "invalid-hello", issue } }. It replaces the offlineReason string from the earlier revision, which never shipped.
  • Machine.NotSent gains an optional refused, and its message adds the reason when there is one.
  • The agent's machine.list tool output includes refused.

Existing callers are unaffected.

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

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant