Skip to content

feat(manager): service-token headers in opencode.jsonc - #309

Merged
delpher merged 2 commits into
masterfrom
manager/service-token-headers
Dec 1, 2026
Merged

delpher merged 2 commits into
masterfrom
manager/service-token-headers

Conversation

@delpher

@delpher delpher commented Dec 1, 2026

Copy link
Copy Markdown
Owner

ТЗ-5 (#244 v2), the manager part's first piece: writing the service-token header into opencode.jsonc without a JSONC parser that throws comments away.

withJsoncServerHeaders(text, name, headers)

  • sets "X-Service-Token-<id>": "{file:…}" on the OpenCode server record (mcp.<name>, and the mcp.servers.<name> layout some profiles use);
  • keeps comments, indentation and the profile's own headers and their order;
  • a header given null or "" is removed, and the empty headers object goes with the last one — adding and then removing gives the file back as it was;
  • a headers that is not an object, or a server that is not there, is reported as a SettingError (the plan's fallback is to show the line to paste by hand), never overwritten;
  • shared helpers: withJsoncMember (one member set/added/removed in a JSONC object) and one serverMember lookup, now also used by withJsoncServerDisabled.

Tests (one new case in manager.test.ts): one-line and multi-line servers, a comment inside headers, another header kept, removal leaving the file parseable, empty value as removal, the add→remove round trip, and both refusals.

tsc -p tsconfig.json clean; dist/test/manager.test.js — 44 pass, 1 skip, 0 fail.

Not in this PR (next, after the broker's /api/services lands): the token store in ~/.secrets/aichat-<id>-<agent>.token, serviceTokens in the agent config, the manager UI and the check call.

withJsoncServerHeaders sets X-Service-Token-<id> on an OpenCode server without touching comments, layout or the profile's other headers, and removes a header given null/empty (adding then removing gives the text back). A headers that is not an object is reported, not overwritten. Shared member scanner (withJsoncMember) and server lookup reused by the disabled switch.
@delpher
delpher force-pushed the manager/service-token-headers branch from 23764f3 to e8d6d92 Compare December 1, 2026 19:20
@delpher

delpher commented Dec 1, 2026

Copy link
Copy Markdown
Owner Author

Review (claude-alexey): one blocker, one nit.

Blocker — members() loops forever on an unterminated /* comment. j = text.indexOf("*/", j + 2) + 2 gives 1 when there is no */, so the scan jumps back to the start of the object, reaches the same comment again and never ends: the manager hangs on a broken opencode.jsonc instead of answering "додайте рядок вручну". valueEnd already guards this (if (j <= 0) return text.length). Same guard here (e.g. const e = text.indexOf("*/", j + 2); if (e < 0) break; j = e + 2;) plus a test with {"mcp":{"aichat":{ /* unterminated "type": "remote" }}}, which should throw a SettingError or come back unchanged, but never hang.

Nit — removing the last member after a line comment. "url": "x", // note + newline + "headers": {…}: the backward scan from headers stops at the comment text, not at the comma. The comma stays, and the object ends with a trailing comma. Whether that matters depends on how OpenCode parses JSONC (trailing commas). Either skip a trailing // comment when looking for the comma, or note it as accepted.

Everything else looks good: round trip, comments inside headers, other headers kept, refusal when headers is not an object.

…d last member takes the comma before it

claude 6332: members() and findMember() did indexOf("*/") + 2, so an unterminated comment sent the scan back to the start of the object and hung the manager. The scan now stops there, and withJsoncServerHeaders reports the broken file by name ("не закритий коментар /*…*/"), the same guard valueEnd already had.

Removing the last member now finds its separator across a comment (commaBetween) and drops the member's own newline, so no dangling ', // comment' and no blank line is left. A /* inside a string stays text, not a comment.
@delpher
delpher merged commit 94e13e8 into master Dec 1, 2026
3 checks passed
@delpher
delpher deleted the manager/service-token-headers branch December 1, 2026 19:31
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