diff --git a/CHANGELOG.md b/CHANGELOG.md index c45da012..a63d9b47 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -34,6 +34,24 @@ The next SDK version is **2.0.0**, on the same opencode **2.3.0** pin already re - `OpenCodeServer.FindIncumbentAsync` reports whether the registered background service that answers at a given URL is ready, starting or stopping, or failed (`OpenCodeServerIncumbent`, `OpenCodeServerState`), as opencode's `serve --service` checks, before it binds, whether a compatible service already owns its address; it answers null when no compatible registered service answers there. - `RpcDefinition` declares a plugin's RPC definition once, with `Method` (`RpcMethod`) and `Event` (`RpcEventDefinition`) descriptors that carry your source-generated JSON metadata, so it works under native AOT and on every target. `client.Rpc.CallAsync(method, input)` calls a method with typed input and output, as opencode's own client binds a definition, and a failure the RPC registry or the plugin reports throws `OpenCodeRpcException` with its `ErrorType`, its message and its data. It derives from `OpenCodeApiException`, which is no longer sealed, so an existing `catch (OpenCodeApiException)` still catches it. - `client.Events.SubscribeAsync(rpcEvent)` yields one RPC definition's events as `RpcEventPayload`, the shared `EventRpc` and its data read with your metadata, as opencode's own client subscribes to a definition's events. It selects nothing at the feed and keeps only its own events, so queued unrelated events count against its 4,096-event queue, and an event whose data does not match your type throws `OpenCodeTransportException`. +- `OpenCodeServiceConfiguration` adds typed administration of the background service's config, + matching the CLI's `service get`, `set`, and `unset` surface. `ReadAsync` returns an + `OpenCodeServiceConfig` without the password or remote route and migrates or repairs legacy + config as the CLI's read does. `GetOrCreatePasswordAsync` returns a stored password or creates + and stores one when it is absent or empty. Setters change the hostname, port, password, CORS + origins, environment entries and disabled flag; `UnsetAsync(OpenCodeServiceSetting)` and + `UnsetEnvironmentVariableAsync` remove settings, and `DisableRemoteAsync` turns remote access + off. Sets and unsets stop the registered service first except `SetDisabledAsync(false)` and + unsetting Disabled; nothing restarts it. `OpenCodeServiceConfigOptions` selects the channel and + installed-version comparand. `ReadAsync`, `GetOrCreatePasswordAsync`, and the set and unset + operations refuse a config or donor the CLI may read but the SDK's JSON reader rejects, rather + than returning empty settings or erasing them. +- `OpenCodeServiceConfiguration.EnableRemoteAsync` turns remote access on through the installed + `opencode service set remote true`, using the `CliCommand` and `Environment` members of + `OpenCodeServiceConfigOptions`. Only `EnableRemoteAsync` reads those two members. After CLI + success, the SDK reads and returns the selected config's secret route through the shared + migrating and repairing read. The SDK does not check that the command, channel and environment + agree. Cancellation after CLI success can leave remote access enabled. ### πŸ”§ Changes @@ -48,6 +66,10 @@ The next SDK version is **2.0.0**, on the same opencode **2.3.0** pin already re ### πŸ› Fixes - `client.Rpc.CallAsync` runs until the plugin's method returns, as opencode's own client does: it no longer takes the client-wide `NetworkTimeout` (100 s by default). A per-call `NetworkTimeout` still bounds it, and the cancellation token is its deadline. +- Legacy service-config and registration migration now write the decoded UTF-8 text opencode's + CLI writes: one leading byte-order mark is dropped and invalid sequences become U+FFFD. A donor + whose decoded text still begins with U+FEFF is not migrated. Before: donor bytes were copied + unchanged. ### πŸ“š Documentation diff --git a/GLOSSARY.md b/GLOSSARY.md index 734ce436..955628ea 100644 --- a/GLOSSARY.md +++ b/GLOSSARY.md @@ -295,6 +295,13 @@ _Avoid_: guarded server handle, shared handle (draft names for the same thing) The CLI's compiled release identity that selects the registration and service-config filenames and the legacy-migration rules. The SDK has none compiled in: a caller may name one. +**SDK-managed write**: +A service-configuration operation that assigns or removes a setting through the SDK: +`OpenCodeServiceConfiguration.GetOrCreatePasswordAsync`, `SetDisabledAsync`, `SetHostnameAsync`, +`SetPortAsync`, `SetPasswordAsync`, `SetCorsOriginsAsync`, `SetEnvironmentVariableAsync`, +`DisableRemoteAsync`, `UnsetAsync`, or `UnsetEnvironmentVariableAsync`. The term excludes +`ReadAsync` and `EnableRemoteAsync`, even though their config reads can migrate or repair a file. + #### Snapshot and refresh **Protocol surface**: diff --git a/docs/ROADMAP.md b/docs/ROADMAP.md index e4cfaceb..83d3c4b3 100644 --- a/docs/ROADMAP.md +++ b/docs/ROADMAP.md @@ -1,6 +1,6 @@ # Roadmap -Date: 2026-11-15 +Date: 2026-11-17 Operational state: what ships today, what is queued next, what is still open, and what is known to be incomplete. This file is a summary and shrinks as work lands. `../AGENTS.md` routes to the @@ -171,11 +171,14 @@ These do not block `1.0.0` and run beside the road. carriers stay the representation; existing types are never retrofitted (ADR-0036). - A Hosting package, if a concrete consumer β€” the MCP server or a first external host β€” needs host-owned process lifecycle. -- Research: turning the service's remote access on and off from .NET. Upstream exposes it only - through the CLI (`opencode service set remote`, `opencode pair --remote`) over an OpenTunnel - route, not through its client library or the HTTP API; the SDK reads the setting and surfaces - the remote URL in `ServerInfo.Urls`, and would have to author CLI config contents for the first - time, where today it only copies a config verbatim when it migrates one. +- Research: remote access beyond service configuration. `OpenCodeServiceConfiguration` + ([#213](https://github.com/opencode-dotnet/opencode-sdk-dotnet/issues/213)) reads and changes the + background service's config through typed `service get`, `set`, and `unset` operations. + Sets and unsets stop the service except turning the disabled flag off or unsetting it. + `DisableRemoteAsync` turns remote access off; `EnableRemoteAsync` runs the installed + `opencode service set remote true` for OpenTunnel setup, then reads and returns the selected + config's route. The SDK does not yet offer remote-address pairing (`opencode pair --remote`) or + tunnel setup without an installed CLI; the latter would need an OpenTunnel client integration. ## Open Questions diff --git a/docs/architecture/client-runtime.md b/docs/architecture/client-runtime.md index 76ab5625..d293810a 100644 --- a/docs/architecture/client-runtime.md +++ b/docs/architecture/client-runtime.md @@ -1,6 +1,6 @@ # Client Runtime Architecture -Date: 2026-11-16 +Date: 2026-11-17 Canonical current rules for client construction, transport ownership, API errors, streams, and the local server launcher. Protocol and generated-model rules live in @@ -90,15 +90,20 @@ upstream's behaviour. Each one is listed, with its reason, in the section it con `Plugins.WaitForPluginsAsync` and `LanguageModels.WaitForDefaultAsync`, are described under [Error channels](#error-channels). - Client options are snapshotted at construction. Explicit-endpoint construction reads no process - environment variable; consumers resolve their own configuration. `OpenCodeServer.DiscoverAsync`, - `OpenCodeServer.FindIncumbentAsync`, `OpenCodeServer.ShutdownPersistentPtyAsync`, - `OpenCodeServer.StopAsync`, `OpenCodeServer.EnsureAsync` and the non-explicit paths of - `OpenCodeServer.ResolveAsync` are the doors that read the environment, and they read exactly - `XDG_STATE_HOME`, `XDG_CONFIG_HOME`, `OPENCODE_CONFIG_DIR`, and the user profile (`USERPROFILE` on - Windows, `HOME` elsewhere), because those roots locate the registration file. For discovery that - file is the mode's complete endpoint-and-credential contract; `FindIncumbentAsync` takes its - endpoint from the caller's `url` and only the id, credential, pid and version from the file. - None of them reads an `OPENCODE_*` credential variable. + environment variable; consumers resolve their own configuration. The path lookup used by + `OpenCodeServer.DiscoverAsync`, `OpenCodeServer.FindIncumbentAsync`, + `OpenCodeServer.ShutdownPersistentPtyAsync`, `OpenCodeServer.StopAsync`, + `OpenCodeServer.EnsureAsync`, the non-explicit paths of `OpenCodeServer.ResolveAsync`, and the + `OpenCodeServiceConfiguration` operations uses `XDG_STATE_HOME`, `XDG_CONFIG_HOME`, + `OPENCODE_CONFIG_DIR`, and the user profile (`USERPROFILE` on Windows, `HOME` elsewhere, with the + platform profile folder as fallback). Those roots locate the registration and service-config + files. For discovery the registration is the complete endpoint-and-credential contract; + `FindIncumbentAsync` takes its endpoint from the caller's `url` and only the id, credential, pid + and version from the registration. This lookup does not select credentials from an `OPENCODE_*` + variable. Configuration operations also compose the launcher seams, which snapshot the whole host + environment; `EnableRemoteAsync` passes that environment, with its `Environment` option layered + over it, to the CLI. Its command lookup uses the process's `PATH` and `PATHEXT`, as the standalone + launcher does. The CLI environment overlay does not redirect the SDK's own config lookup. `Pipeline` owns construction, the policy roster, the one-shot plane and disposal orchestration. Its Events-neutral `RegisterOwned(IDisposable)` hook lets a hand-written family door register a @@ -432,7 +437,8 @@ resolves the message's network window from those carried traits. `OperationCanceledException` carrying the caller's token, not an SDK-owned linked token. The rule applies to HTTP operations, SSE, cursor traversal, both terminal families, and `StartAsync`, `DiscoverAsync`, `FindIncumbentAsync`, `EnsureAsync`, `ShutdownPersistentPtyAsync`, `StopAsync`, - `ResolveAsync`, `ServiceOperations.ReconnectAsync` and `ServiceOperations.RestartAsync`. + `ResolveAsync`, `ServiceOperations.ReconnectAsync`, `ServiceOperations.RestartAsync`, and the + `OpenCodeServiceConfiguration` operations. This identity guarantee applies when one caller token is supplied, including the same token supplied at both enumeration entry points; two distinct cancellable entry tokens are both observed but have no token-identity guarantee. @@ -1075,8 +1081,12 @@ copying over only the behavior members (`Location`) the delegate set; the object received stays identity-unset for as long as the caller keeps a reference to it. Every call builds a new client over its own owned transport, which the caller disposes. -The failure plane of every local-server door, standalone start and background-service discovery -alike, is `OpenCodeServerException : OpenCodeException`. A bounded stderr tail rides every startup +The local lifecycle failures of every local-server door, standalone start, the background-service +doors and the service configuration alike, use `OpenCodeServerException : OpenCodeException`; +refused arguments are `ArgumentException`, the caller's cancellation is +`OperationCanceledException`, and a request a door sends to a running service +(`ShutdownPersistentPtyAsync`'s shutdown) fails on the API and transport planes. +A bounded stderr tail rides every startup failure that reaches a running child: an exit before readiness (naming the exit code β€” on Windows in its full 32 bits, a negative status code with its hexadecimal form β€” or on Linux and macOS the terminating signal), a stdout that closed before readiness, a readiness timeout (naming the @@ -1149,18 +1159,23 @@ The channel's service config is read the CLI's way: a document whose members do `disabled` must be a boolean, `remote` an object holding a string `route` (or, in the legacy shape, a boolean), and `remoteAccepted` the literal `true` when present β€” is no config at all, so its `env` overlay and the legacy migration it gates do not apply; a legacy-shape document still reads, and only the current shape is migrated. -The CLI's read rewrites a legacy-shape document in place; Discovery, Ensure and Stop read it as it -is and leave the rewrite to the daemon's next read, which reads the same `env`. -`OpenCodeServiceConfiguration` operations read and repair it the CLI's way, copying a current-shape -donor or repairing a legacy `remote` in place, and write it (`GetOrCreatePasswordAsync` stores a -password). Remote access is set only through the CLI (`opencode service set remote`); the SDK's -config repair can generate a route, and the service's remote URL reaches callers in `ServerInfo.Urls` -while its tunnel is attached. The CLI's pairing command also reads the `disabled` key +Legacy config and registration donors are written as the CLI's decoded UTF-8 text: one leading +byte-order mark is dropped and each invalid sequence is replaced by U+FFFD. A donor whose decoded +text still begins with U+FEFF is not migrated. +The CLI's read rewrites a legacy-shape document in place. Discovery, Ensure and Stop leave that +repair to the CLI or a later shared config-store read. The daemon's next read sees the same `env` +overlay Ensure reads. `OpenCodeServiceConfiguration` reads and changes the config through that +store, as described below. Remote access is turned on through the +installed CLI by `OpenCodeServiceConfiguration.EnableRemoteAsync` and turned off by +`DisableRemoteAsync`; legacy config repair can also generate a route. The service's remote URL +reaches callers in `ServerInfo.Urls` while its tunnel is attached. The CLI's pairing command also +reads the `disabled` key (`opencode pair` needs the service). `ResolveAsync` reads the config's `disabled` key after the config migration and before the registration migration, as `resolve` does. The pinned client's `Service.discover`, `ensure` and `stop` do not read it; neither do `DiscoverAsync`, `FindIncumbentAsync`, `EnsureAsync`, `StopAsync` or `ShutdownPersistentPtyAsync`. A direct registration path has no config file and is never disabled. -Resolution reads the service configuration through the config store, as the CLI does; a refused document reads as absent, as for Discovery, Ensure and Stop. +Resolution uses the same migrating and repairing config-store read as configuration administration, +but treats a strict-reader refusal as absent config rather than throwing the administration refusal. The registration and service-config files are read the way libuv opens files, sharing read, write, and delete, so a poll never makes the daemon's remove-on-exit fail on Windows, and a UTF-8 byte-order mark is skipped as the CLI's decoder skips it. A daemon bound to every interface @@ -1322,15 +1337,130 @@ registration (or whose registration is gone) as `OPENCODE_PTY_HANDOFF` and remov otherwise; `complete` clears only a sidecar a different source wrote, with no expiry check; and `clear` removes it idempotently, the same seam Stop's handoff step routes through. +`OpenCodeServiceConfiguration` provides typed access to the CLI's `service get`, `set`, and `unset` +over the selected channel's config. `OpenCodeServiceConfigOptions.Channel` follows the channel rules +above, with null selecting the shared `service.json`; `InstalledVersion` is used only by the +registration migration of an SDK stop the operation runs. `ReadAsync` is `get` with no key: it +returns the saved settings without the password or remote route, represents remote access as on or +off, and stops nothing. `GetOrCreatePasswordAsync` is `get password`: it returns a nonempty stored +password, or generates 32 random bytes in unpadded base64url, stores them, and returns that +password. It stops nothing. + +[SDK-managed writes](../../GLOSSARY.md#connection-modes) are defined in the glossary. + +The store has one shared read. When the current config is absent, not merely unreadable, it +considers the legacy donor: a current-shape donor the strict reader can materialize is migrated +exclusively through the decoded-text migration above. A donor the CLI may copy but the strict +reader cannot materialize produces a refusal result instead. A present config the strict reader +cannot materialize also produces a refusal unless the SDK proves the CLI cannot read it. +`ReadAsync` and SDK-managed writes turn that result into `OpenCodeServerException` naming the +refused file. A legacy boolean `remote` is repaired and written in place: true becomes a route +generated from eight random bytes in lowercase hex, and false drops the member. A document proven +unreadable by the CLI is empty config. `ReadAsync` otherwise writes nothing: a current-shape +document, a proven-unreadable document, an unreadable file, or an absent file with no admissible +donor creates no config or directory. Resolution and remote-on consume the same read result with +the refusal handling described in their contracts. + +SDK-managed writes serialize the whole decoded and merged document as the CLI's `write` does. +Decoded members start in schema order: `disabled`, `remote`, `remoteAccepted`, `hostname`, `port`, +`password`, `cors`, `env`. Unknown members and extra members inside `remote` are dropped. A remote +member repaired by that read follows the other members; assigning a present member keeps its +position, assigning an absent member adds it last, and removing a member drops it. Environment +names follow JavaScript object order: canonical array-index names from 0 through 4294967294 first, +numerically ascending, then other names in insertion order. Output follows `JSON.stringify` with +two-space indentation and a final newline. + +The write creates the config directory, writes to the config path followed by a dot, this process's +pid and `.tmp`, then renames that file over the config. A new temporary file is owner-only where +the platform supports mode 0600; Windows inherits the parent directory's DACL. An existing +temporary file is truncated and overwritten without changing its access mode. The rename has one +initial attempt and up to 20 retries, 10 ms apart, on the injected clock. +Nothing serializes writers. Separate processes use separate temporary names; writes in one process +share a name and can interfere, and the last successful rename determines the published file. A +failed write or exhausted rename leaves any temporary file in place. Filesystem failures at +directory creation, temporary writing or final rename become `OpenCodeServerException` naming the +config and temporary file, with the filesystem exception inside. + +The nine changing operations are `SetDisabledAsync`, `SetHostnameAsync`, `SetPortAsync`, +`SetPasswordAsync`, `SetCorsOriginsAsync`, `SetEnvironmentVariableAsync`, `DisableRemoteAsync`, +`UnsetAsync`, and `UnsetEnvironmentVariableAsync`. Each reads first. Every set and unset except +`SetDisabledAsync(false)` and `UnsetAsync(OpenCodeServiceSetting.Disabled)` then stops the +registered service as `StopAsync` does for the same channel and installed version, with the default +persistent-terminal clear, reads again, merges, and writes. The two exceptions merge and write +after their first read without stopping. Nothing restarts the service. A stop that throws prevents +the requested merge and final write; a donor copy or repair already made by the first read stays. +The second read may also refuse after the service has stopped. Every successful set and unset +writes, even when its value was already stored or no file existed. + +`SetPortAsync` accepts 1 through 65535. Hostnames, passwords, and environment names and values may +be empty strings; null arguments and undefined unset settings are refused. `SetCorsOriginsAsync` +requires a nonempty list, trims each item with ECMAScript's WhiteSpace and LineTerminator set, +refuses commas inside an item, and preserves order and duplicates. Its `CorsOriginPolicy` accepts +an item only when it equals the HTTP or HTTPS origin reconstructed through .NET's `Uri`, +`IdnMapping`, and IP handling. The oracle `tools/oracles/service-config.ts` compares a checked +corpus with the CLI's own WHATWG-based validation; this bounded evidence does not establish full +parser equivalence. Invalid values are refused with an `ArgumentException` subtype before +configuration I/O, stopping, or CLI execution. + +`DisableRemoteAsync` is both `set remote false` and `unset remote`: it stops the service, forgets +the route and records `remoteAccepted` when remote access was enabled, leaving that marker +unchanged otherwise. Setting an environment entry preserves its position when already present; +unsetting an entry removes `env` and appends the remaining map last, or drops it when empty. +`UnsetAsync` selects Disabled, Hostname, Port, Password or CorsOrigins through +`OpenCodeServiceSetting`; remote access and environment entries use their dedicated operations. + +The public config view has no password or route property. Its default string representation and +the internal config document's do not print their members. Store diagnostics name paths rather +than stored credential fields. Within this administration surface, `GetOrCreatePasswordAsync` is +the result that returns the password and `EnableRemoteAsync` is the result that returns the route. + +`OpenCodeServiceConfiguration.EnableRemoteAsync` is `set remote true`. It delegates tunnel setup +to the installed CLI's own OpenTunnel client rather than porting that client (maintainer's decision, +2026-11-15, #213). `OpenCodeServiceConfigOptions.CliCommand`, defaulting to `opencode`, supplies the +executable and leading arguments; the first entry is resolved as the standalone launcher's command, +and `service set remote true` is appended after the leading arguments. Only `EnableRemoteAsync` +reads `CliCommand` and `Environment`. It captures both before resolution and layers the environment +entries over the host environment for the child. No SDK-generated password is injected; inherited +or explicitly supplied credentials remain. Standard output is drained and discarded, and standard +error is retained as a bounded tail even when the host sets `OPENCODE_PRINT_LOGS=1`. + +The CLI creates or resumes the device's tunnel, which can take up to five minutes, then stops its +own channel's registered service and stores or reuses its route. Nothing restarts the service. +There is no SDK config read before the CLI run, no pre-run administrative refusal, and no SDK +service stop or config write during that run: the installed CLI uses its own read and write +behavior, including on documents SDK-managed writes refuse. + +After CLI success, the SDK obtains the selected config's route through the store's shared read. +That read may migrate a donor or repair and write a legacy remote value; it is not a separate +read-only route parser. A refused document is unavailable for this purpose rather than an +administrative write refusal. If the shared read yields no readable route, the operation throws +`OpenCodeServerException` naming the selected config; roots and repair failures can also propagate. +The returned route is the secret part of the remote address, hidden by the CLI's own `get`. +Treat the route as a credential (maintainer's decision, 2026-11-15, #213). + +For the CLI runner, exit code 0 is success; every other end, an unknown status included, throws +`OpenCodeServerException` carrying the exit description and bounded stderr tail. That tail is +external CLI text and can contain whatever the CLI printed, including secrets. An unresolvable +command throws before a child starts. Caller cancellation during the run ends the child through +the launcher's cleanup ladder. The subsequent shared read receives the same caller token; +cancellation after CLI success can throw while remote access remains enabled. + +The CLI writes its own compiled channel's config under its environment's roots, while the SDK +reads under its process's roots and selected Channel. The SDK reads the config that `CliCommand`, +`Channel` and `Environment` select together. It does not check that they agree. A mismatch is not +checked before the run. A missing readable route is reported afterward, but a readable route +already present in the wrong selected config can leave a mismatch undetected. + The background service's recorded divergences from upstream on Windows (ADR-0031): | Divergence | Upstream | SDK | Reason | |---|---|---|---| -| Sidecar publish | The pinned client's `publish` (`packages/client/src/pty-handoff.ts`) renames the temporary file over the sidecar with `node:fs` `rename`, which fails with `EPERM` while any handle holds the destination, whatever that handle shares | A POSIX-semantics rename: `SetFileInformationByHandle(FileRenameInfoEx)` with `FILE_RENAME_FLAG_REPLACE_IF_EXISTS` and `FILE_RENAME_FLAG_POSIX_SEMANTICS`, on a source opened with `DELETE` access. A system or volume that refuses it with `ERROR_INVALID_PARAMETER`, `ERROR_NOT_SUPPORTED`, or `ERROR_INVALID_FUNCTION` (Windows before 10 1607, some Windows Server 2016 installations, FAT, ReFS, some network shares; the codes Git's own POSIX-semantics rename falls back on) takes the platform replace instead: `MoveFileEx` with `MOVEFILE_REPLACE_EXISTING`, or `File.Replace` on the downlevel targets | The sidecar's readers include holders the SDK does not control, such as real-time antivirus and the indexer, and they open a fresh file with delete sharing, as the SDK's own reader does. The POSIX-semantics rename replaces the destination under them, and they keep reading the contents they opened, so the failure class is removed rather than retried. It is the rename Bun itself makes in its install path. A holder that opened the sidecar without delete sharing still fails the publish, and the replacement then starts without a handoff, as after any failed publish. | +| Sidecar and service-config publish | The pinned client's `publish` (`packages/client/src/pty-handoff.ts`) renames the temporary file over the persistent-terminal handoff sidecar with `node:fs` `rename`, which fails with `EPERM` while any handle holds the destination, whatever that handle shares. Upstream's `write` (`packages/cli/src/services/service-config.ts:180`) uses a plain `fs.rename` for the service config, with 20 retries 10 ms apart. | A POSIX-semantics rename: `SetFileInformationByHandle(FileRenameInfoEx)` with `FILE_RENAME_FLAG_REPLACE_IF_EXISTS` and `FILE_RENAME_FLAG_POSIX_SEMANTICS`, on a source opened with `DELETE` access. A system or volume that refuses it with `ERROR_INVALID_PARAMETER`, `ERROR_NOT_SUPPORTED`, or `ERROR_INVALID_FUNCTION` (Windows before 10 1607, some Windows Server 2016 installations, FAT, ReFS, some network shares; the codes Git's own POSIX-semantics rename falls back on) takes the platform replace instead: `MoveFileEx` with `MOVEFILE_REPLACE_EXISTING`, or `File.Replace` on the downlevel targets. `ServiceConfigStore.WriteAsync` calls `ServiceFileSystem.Rename` through `RenameAsync`; the SDK keeps the config's 20 retries 10 ms apart, and each attempt uses this POSIX-semantics rename with its platform fallback (maintainer's decision, 2026-11-17, #939). | The sidecar's publish or the service config's write can meet holders the SDK does not control, such as real-time antivirus and the indexer, and they open a fresh file with delete sharing, as the SDK's own reader does. The POSIX-semantics rename replaces the destination under them, and they keep reading the contents they opened, so the failure class is removed rather than retried. It is the rename Bun itself makes in its install path. A holder that opened the sidecar without delete sharing still fails the publish, and the replacement then starts without a handoff, as after any failed publish. | | Case-only collision in a non-ASCII name | Bun's spawn groups spellings with JavaScript's `toUpperCase`, which applies full Unicode case mapping, so `straße` and `STRASSE` are one variable | Groups with `ToUpperInvariant`, so those two stay separate variables. 158 code points map differently, none of them ASCII: those with a multi-character uppercase (`ß`, `Ε‰`, `ΐ`) or a mapping .NET doesn't have (`Δ±`, `Ζ›`) | No ASCII name is affected. The base class library has no full case mapping, so matching it would mean carrying a case table in the SDK for names that environment variables rarely use. | -The divergence is reversed when upstream changes its publish to one that survives such holders, or -when the POSIX-semantics rename proves unsound on a volume the SDK supports. +The divergence is reversed when upstream changes its publish or the service config's write to one +that survives such holders, or when the POSIX-semantics rename proves unsound on a volume the SDK +supports. The daemon side of the lifecycle is the spawned CLI's, not the SDK's: the incumbent check, the exit-0 loser on a port another daemon holds, the `chdir` into the user home, and the registration @@ -1348,9 +1478,11 @@ resolver snapshot, public Ensure reads the config anew at every spawn. Each background-service door chooses `TimeProvider.System` once at its composition root: Discover supplies its probe, from one factory that `ShutdownPersistentPtyAsync` and `FindIncumbentAsync` share; Stop shares it across the handoff and terminator; Ensure shares it -across the election collaborators and spawned contenders. Stop and the election share the same -handoff step. Internal collaborators require that provider, including the handoff ticket's -request pipeline. +across the election collaborators and spawned contenders. The service configuration chooses no clock +of its own: its door takes the standalone launcher root's clock from `LauncherSeams.Time`, and its +write's rename retry, the stop its operations run, and the CLI run that turns remote access on share +it. Stop and the election share the same handoff step. Internal collaborators require that provider, +including the handoff ticket's request pipeline. Request deadlines, election and termination polls, and contender exit backoff run on it. Election deadline comparisons and spawn stamps read UTC, and handoff expiry reads UTC epoch milliseconds; neither is a monotonic elapsed-time policy. Each request deadline arms after probe headers, after @@ -1385,7 +1517,10 @@ reason (ADR-0031): - **Stricter readers than upstream's `JSON.parse`.** The registration and service-config readers refuse a repeated member. The registration reader also refuses a pid that is not a positive - 32-bit integer and a URL that is not absolute HTTP or HTTPS. A refused document reads as absent. + 32-bit integer and a URL that is not absolute HTTP or HTTPS. For discovery, incumbent lookup, + Ensure, Stop and persistent-terminal shutdown, a refused document reads as absent. Resolution also + treats the shared config read's refusal as absent; configuration administration handles it as + described below. Reason: the repository's fail-closed boundary, which treats malformed state as missing rather than acting on it. For the pid the mechanism is hard: upstream's promise client reads the registration with an unchecked `JSON.parse` (`packages/client/src/promise/service.ts:118` at the @@ -1422,4 +1557,25 @@ reason (ADR-0031): `new URL` throws. Reason: parity would reimplement WHATWG's host, IPv4 and IDNA rules for hosts no loopback service uses (maintainer's decision, 2026-11-15, #847). Reversal: a consumer needs the throw. +- **The service configuration refuses a document it cannot prove the CLI would fail to read.** + (maintainer's decision, 2026-11-15, #213). The CLI's UTF-8 decoding and JSON parse can retain + settings the SDK's strict materializer refuses, including repeated members, escaped lone + surrogates and malformed UTF-8 replaced during decoding. The shared config read returns a refusal + when strict materialization fails unless the SDK proves the CLI's read rejects the document; a + parse stopped at an SDK limit, such as nesting depth, is not proof. + `OpenCodeServiceConfiguration.ReadAsync` and SDK-managed writes, including + `GetOrCreatePasswordAsync`, turn that result into `OpenCodeServerException` naming the file. + Donor classification uses the current shape alone, as the CLI's migration does, and refuses an + unmaterializable donor the CLI may copy before copying it. The first read precedes any requested + service stop; a stopping edit checks again after the stop, and migration or repair already made + by an earlier read remains. A document proven unreadable by the CLI is empty config: + `ReadAsync` leaves it unwritten and an SDK-managed edit replaces it. `ReadAsync` refuses even a + current-shape document needing no repair, because returning empty config would misrepresent + settings the CLI reads. Resolution retains the absent-on-refusal behavior recorded above. + `EnableRemoteAsync` has no protective config read before delegating to the CLI; its later shared + read reports a refusal as no readable route. Reason: SDK-managed edits must not erase settings + the CLI would retain, whether in the config or an admissible legacy donor (maintainer's decision, + 2026-11-15, #213). Reversal: the strict reader admits what the CLI's parse admits, or the + maintainer asks for the CLI's literal behavior (maintainer's decision, 2026-11-15, #213). + Everything else follows the chain at the pin. diff --git a/docs/guide/connection-modes.md b/docs/guide/connection-modes.md index 1206ab88..775efbbd 100644 --- a/docs/guide/connection-modes.md +++ b/docs/guide/connection-modes.md @@ -1,6 +1,6 @@ # πŸ”Œ Connection modes -Date: 2026-11-16 +Date: 2026-11-17 There are three ways to get a client bound to a running opencode server: **let the SDK start one**, **point it at one you already have**, or **discover the background service the opencode CLI runs @@ -15,6 +15,7 @@ register any of them with a container. - [πŸ›‘ Stopping the background service](#-stopping-the-background-service) - [🧹 Shutting down the service's persistent terminals](#-shutting-down-the-services-persistent-terminals) - [🟒 Ensuring the background service](#-ensuring-the-background-service) + - [πŸ”§ Configuring the background service](#-configuring-the-background-service) - [πŸ”Œ Supplying a handler](#-supplying-a-handler) - [🧩 Registering with dependency injection](#-registering-with-dependency-injection) @@ -435,8 +436,8 @@ working. The client never reads either one, or any other environment variable, f in the snippet above is your application's choice; a configuration section or a secret store works exactly as well. The canonical [construction contract](../architecture/client-runtime.md#construction-and-transport-ownership) -names the background-service doors, including Ensure and the resolver's non-explicit paths, that -read path variables, never a credential variable. +names the background-service doors, including Ensure and the resolver's non-explicit paths, and the +service-configuration operations that use path variables without selecting credentials from them. ### An app-owned server on a fixed endpoint @@ -724,10 +725,10 @@ URL that cannot be a base address: a relative one, or one such as `mailto:owner@ ### πŸ›‘ Stopping the background service `OpenCodeServer.StopAsync` is `opencode service stop` for your code: it ends the daemon the -registration names and removes the registration. Nothing else in the SDK ever stops the shared -service β€” disposing a discovered handle is a no-op by contract β€” so this is the one call to make -deliberately, for the reasons the CLI has the command: replacing a version, shutting the service -down before an uninstall, or ending a daemon that no longer answers. +registration names and removes the registration. Disposing a discovered handle is still a no-op. +Configuration changes and managed restart operations can also stop the shared service; call this +door when the stop itself is your intention, such as when replacing a version, before an uninstall, +or to end a daemon that no longer answers. ```csharp await OpenCodeServer.StopAsync(); @@ -828,7 +829,7 @@ await using var server = await OpenCodeServer.EnsureAsync(); using var client = server.CreateClient(); var info = await client.Server.GetInfoAsync(); Console.WriteLine($"opencode {info.ServerInfo.Version} (pid {server.ProcessId})"); -// Disposal is a no-op: the service is shared. StopAsync is the one way to end it. +// Disposal is a no-op: the service is shared. Call StopAsync when you intend to stop it. ``` What it does: @@ -874,6 +875,39 @@ at another version, or the 120-second bound expired (a contender's failure when otherwise the timeout with the last failed replacement as the inner exception, when there was one); and `OperationCanceledException` for your own token. +### πŸ”§ Configuring the background service + +`OpenCodeServiceConfiguration` reads and changes the background-service config managed by the CLI's +`opencode service get`, `set`, and `unset`. + +```csharp +var config = await OpenCodeServiceConfiguration.ReadAsync(); +Console.WriteLine($"port {config.Port}, remote access {(config.RemoteEnabled ? "on" : "off")}"); + +await OpenCodeServiceConfiguration.SetPortAsync(4097); // stops the running service first +await using var server = await OpenCodeServer.EnsureAsync(); // starts it again on the new port +``` + +- Sets and unsets stop the registered service first, except `SetDisabledAsync(false)` and + `UnsetAsync(OpenCodeServiceSetting.Disabled)`. Nothing automatically restarts it; call + `EnsureAsync` or start it through the CLI. +- `ReadAsync` stops nothing and exposes neither password nor remote route. Like the CLI's + `service get`, it can migrate a legacy config or repair a legacy remote-access value. +- `GetOrCreatePasswordAsync` returns the password, creating and storing one when it is absent or + empty. This is the administration call whose result carries the password. +- `OpenCodeServiceConfigOptions.Channel` selects another channel's config using the + [channel rules](#shaping-the-lookup). These options have no direct registration-file selector. +- `ReadAsync` and SDK-managed writes refuse a config or donor the CLI may read but the strict + materializer cannot safely represent, rather than showing empty settings or overwriting them. + The [canonical contract](../architecture/client-runtime.md#connection-modes) owns the read, + refusal, CORS, stop and whole-file write rules, including configuration selection. +- `EnableRemoteAsync` runs the installed `opencode service set remote true`; install the CLI on + `PATH` or name it in `CliCommand`. Tunnel setup can take minutes. The `Environment` option reaches + the CLI, not the SDK's own lookup. After success the SDK reads the route through the shared config + read, which can migrate or repair. Keep the returned route like a password. Cancellation after + CLI success can leave remote access enabled. See + [configuration failures](errors-and-responses.md#-when-a-local-server-door-fails). + ## πŸ”Œ Supplying a handler Set `HttpHandler` for a proxy or client certificate. The SDK reads it once and never disposes it; diff --git a/docs/guide/errors-and-responses.md b/docs/guide/errors-and-responses.md index f09cc58a..8afac843 100644 --- a/docs/guide/errors-and-responses.md +++ b/docs/guide/errors-and-responses.md @@ -1,6 +1,6 @@ # 🧱 Errors and responses -Date: 2026-11-16 +Date: 2026-11-17 Every one-shot call of a client family answers with a **response envelope** that carries the same four members no matter which operation produced it, and every failure is either a typed API error you can branch on @@ -315,9 +315,10 @@ Terminal sessions own their connections separately. See the canonical ## πŸš€ When a local-server door fails The local-server doors β€” `OpenCodeServer.StartAsync`, `OpenCodeServer.DiscoverAsync`, -`OpenCodeServer.FindIncumbentAsync`, `OpenCodeServer.ResolveAsync`, `OpenCodeServer.ShutdownPersistentPtyAsync`, -`OpenCodeServer.StopAsync`, and `OpenCodeServer.EnsureAsync` β€” use `OpenCodeServerException` for -their local lifecycle failures. +`OpenCodeServer.FindIncumbentAsync`, `OpenCodeServer.ResolveAsync`, +`OpenCodeServer.ShutdownPersistentPtyAsync`, `OpenCodeServer.StopAsync`, +`OpenCodeServer.EnsureAsync`, and the `OpenCodeServiceConfiguration` operations β€” use +`OpenCodeServerException` for their local lifecycle failures. Discovery uses it for exactly one cause: an XDG variable was unset and no user home directory resolved either (`USERPROFILE` on Windows, `HOME` elsewhere, and the profile folder all empty), so the registration roots cannot be located. Every ordinary way a background service can be absent @@ -338,9 +339,33 @@ start, the registered service speaks an incompatible health protocol at a versio `VersionPolicy.Error` met a service at another version, or the 120-second bound expired. `ShutdownPersistentPtyAsync` shares discovery's roots cause; where discovery answers null it does nothing and completes, and a failed shutdown request throws `OpenCodeApiException` or -`OpenCodeTransportException`, not `OpenCodeServerException`. The -[connection guide](connection-modes.md#️-discovering-the-background-service) covers discovery, -the registered owner's state, stop, the persistent-terminal shutdown, and ensure. +`OpenCodeTransportException`, not `OpenCodeServerException`. + +Service-configuration operations share the roots cause. `ReadAsync` and SDK-managed writes +(`GetOrCreatePasswordAsync` and every set and unset) can also refuse a config or legacy donor the +CLI may read but the SDK's strict materializer cannot represent. The message names the file; +inspect repeated members, undecodable strings, malformed UTF-8 and excessive nesting using the +[configuration contract](../architecture/client-runtime.md#connection-modes). A failed config +write, including a legacy repair performed during a read, carries the filesystem error as its +inner exception. A changing operation can also fail while stopping the service: its requested +change is then unmade, though migration or repair from the first read can remain. + +`EnableRemoteAsync` applies no administrative refusal before the CLI runs. It can fail because +the command could not be resolved or started, or because the CLI ended unsuccessfully; an exit +failure carries its exit description and bounded stderr tail. That tail is the CLI's own text and +can contain secrets. After CLI success, the shared config read can fail to locate the roots or +write a repair, or yield no route the SDK can read. The missing-route message names the config; +check that `CliCommand`, `Channel`, and `Environment` describe the same config. A readable route +does not prove that those selections agree. Cancellation after CLI success can leave remote access +enabled. + +Invalid arguments, including a port outside 1 through 65535, refused CORS items and invalid +options used by the operation, throw an `ArgumentException` subtype before configuration I/O, +stopping, or CLI execution. + +The [connection guide](connection-modes.md#️-discovering-the-background-service) covers discovery, +the registered owner's state, stop, the persistent-terminal shutdown, ensure, and +[service configuration](connection-modes.md#-configuring-the-background-service). `ResolveAsync` also reports an explicit server it could not reach, authenticate with or read as `OpenCodeServerException`, retaining the original probe exception in `InnerException`. A tagged