diff --git a/CHANGELOG.md b/CHANGELOG.md index 4b9f5c45c..10ef5ad88 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -25,6 +25,10 @@ Nightly builds of `master` are on - The packages' .NET dependency floors move from `10.0.13` to the `10.0.15` servicing release: on `net472` and `netstandard2.0`, `System.Text.Json`, `System.Threading.Channels`, `System.Collections.Immutable`, `Microsoft.Bcl.AsyncInterfaces`, `Microsoft.Bcl.Memory` and `Microsoft.Bcl.TimeProvider`; in `OpenCodeDotNet.Sdk.Extensions`, on every target, `Microsoft.Extensions.DependencyInjection.Abstractions` and `Microsoft.Extensions.Options.ConfigurationExtensions`. No security advisory of the .NET 10.0.14 or 10.0.15 release lists these packages. - Background-service discovery (`OpenCodeServer.DiscoverAsync` and the probe `EnsureAsync` runs) now takes the client's proxy decision for a loopback service. Behind an environment proxy (`HTTP_PROXY`, `HTTPS_PROXY` or `ALL_PROXY`), set `NO_PROXY` to reach a loopback service directly, as with opencode's CLI; behind a system proxy, that proxy's own bypass list applies, as it does for the client. .NET Framework is unchanged. +### 🐛 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. + ### 📚 Documentation - The streaming guide says that a turn's end and `client.Experimental.WaitForSessionAsync` cover one session, not the background jobs its prompt started, and that a job `ListJobsAsync` lists is not necessarily still running. It recommends a `Message` with every permission Reject: at opencode v2.3.0, a Reject without one ends the turn as a shutdown, and a background subagent rejected that way stays listed as `cancelled` with its parent never told. diff --git a/docs/architecture/client-runtime.md b/docs/architecture/client-runtime.md index 30c159fa1..ec4a54610 100644 --- a/docs/architecture/client-runtime.md +++ b/docs/architecture/client-runtime.md @@ -373,7 +373,7 @@ resolves the message's network window from those carried traits. the .NET convention. The operations that answer only when work ends — `experimental.session.wait`, `session.shell`, `session.command`, `session.generate`, `experimental.generate.text`, `experimental.generate.image`, - `experimental.generate.decisions` — run with no window + `experimental.generate.decisions`, `rpc.call` — run with no window unless the call sets one, so the caller's token is their deadline, as upstream's client bounds them; a client-wide value does not reach them. A hand-written table keyed by operation id names them; a Tools test holds its ids to the pinned document, and the source watch pins the upstream diff --git a/docs/guide/requests.md b/docs/guide/requests.md index 31f956545..64000bc4b 100644 --- a/docs/guide/requests.md +++ b/docs/guide/requests.md @@ -1,6 +1,6 @@ # 📝 Requests -Date: 2026-11-10 +Date: 2026-11-15 Every operation that sends anything takes one request record. They are plain immutable records you fill with an object initializer — with one twist worth knowing before you write your first `PATCH`: @@ -228,7 +228,7 @@ var sessions = await client.Sessions.ListSessionsAsync(requestOptions: new OpenC }); ``` -Seven calls answer only when work ends, so they wait with no window at all and your +Eight calls answer only when work ends, so they wait with no window at all and your cancellation token is their deadline: - `client.Experimental.WaitForSessionAsync`, which waits for a session to go idle; @@ -237,7 +237,8 @@ cancellation token is their deadline: - `session.GenerateTextAsync` and `client.Experimental.GenerateTextAsync`, which wait for the whole text generation; - `client.Experimental.GenerateImagesAsync` and `client.Experimental.GenerateDecisionsAsync`, which - wait for image and decision generation. + wait for image and decision generation; +- `client.Rpc.CallAsync`, which waits for the plugin's method to return. The client-wide value does not reach them; a per-call value does: diff --git a/spec/receipt.json b/spec/receipt.json index 69b6982f2..a6112407b 100644 --- a/spec/receipt.json +++ b/spec/receipt.json @@ -970,6 +970,16 @@ "matched": true } ] + }, + { + "path": "packages/core/src/rpc.ts", + "sha256": "bccdda8febbbf254519bc3480c08eb8b5712e60159e595b4ceea5b321f7ac5fc", + "anchors": [ + { + "text": "// A long-lived RPC must release its location when the routing cache invalidates it.", + "matched": true + } + ] } ] } diff --git a/spec/source-watch.json b/spec/source-watch.json index b5aeb14c2..c6587e4b2 100644 --- a/spec/source-watch.json +++ b/spec/source-watch.json @@ -1334,6 +1334,20 @@ } } ] + }, + { + "path": "packages/core/src/rpc.ts", + "sha256": "bccdda8febbbf254519bc3480c08eb8b5712e60159e595b4ceea5b321f7ac5fc", + "behaviors": [ + { + "owner": "opencode-sdk-dotnet", + "behavior": "rpc.call answers only when the plugin\u0027s handler returns or the routing cache closes the RPC (rpc.unavailable), so a long-lived handler holds the response headers until it ends; the SDK\u0027s network timeout leaves it unbounded unless a call sets a window.", + "anchor": { + "type": "contains", + "text": "// A long-lived RPC must release its location when the routing cache invalidates it." + } + } + ] } ] } diff --git a/src/OpenCode.Sdk/Common/Options/OpenCodeClientOptions.cs b/src/OpenCode.Sdk/Common/Options/OpenCodeClientOptions.cs index 4eedb014c..2de650f9e 100644 --- a/src/OpenCode.Sdk/Common/Options/OpenCodeClientOptions.cs +++ b/src/OpenCode.Sdk/Common/Options/OpenCodeClientOptions.cs @@ -44,9 +44,9 @@ public sealed class OpenCodeClientOptions /// Gets or sets how long an ordinary call may go without progress: waiting for the response /// headers, and then between reads of the body. The default, , is /// 100 seconds. The operations that answer only when work ends — waiting for a session to go - /// idle, running a shell command or a command, generating text — ignore this value and run with - /// no window, - /// so the call's cancellation token is their deadline; a value set on + /// idle, running a shell command or a command, generating text, images or decisions, calling a + /// plugin's method — ignore this value and run with no window, so the call's cancellation token + /// is their deadline; a value set on /// bounds them for that call. Accepts a /// value above zero and at most milliseconds, or /// for no window. diff --git a/src/OpenCode.Sdk/Internal/Operations/OperationTraits.cs b/src/OpenCode.Sdk/Internal/Operations/OperationTraits.cs index c8604acca..83e123d3a 100644 --- a/src/OpenCode.Sdk/Internal/Operations/OperationTraits.cs +++ b/src/OpenCode.Sdk/Internal/Operations/OperationTraits.cs @@ -15,6 +15,7 @@ internal readonly record struct OperationTraits(bool Reconnects, bool HasNoNetwo ["experimental.generate.image"] = new(Reconnects: false, HasNoNetworkWindow: true), ["experimental.generate.text"] = new(Reconnects: false, HasNoNetworkWindow: true), ["experimental.session.wait"] = new(Reconnects: false, HasNoNetworkWindow: true), + ["rpc.call"] = new(Reconnects: false, HasNoNetworkWindow: true), ["session.command"] = new(Reconnects: false, HasNoNetworkWindow: true), ["session.generate"] = new(Reconnects: false, HasNoNetworkWindow: true), ["session.shell"] = new(Reconnects: false, HasNoNetworkWindow: true), diff --git a/src/OpenCode.Sdk/Internal/Transport/NetworkTimeouts.cs b/src/OpenCode.Sdk/Internal/Transport/NetworkTimeouts.cs index 2fd1b08e4..7918f3e0b 100644 --- a/src/OpenCode.Sdk/Internal/Transport/NetworkTimeouts.cs +++ b/src/OpenCode.Sdk/Internal/Transport/NetworkTimeouts.cs @@ -7,8 +7,8 @@ namespace OpenCode.Sdk.Internal; /// /// /// The default is the .NET convention. A long-poll operation answers only when work ends: the server -/// sends no headers until a session is idle, a shell command exits, a command finishes its work, or a -/// generation completes. A network window would cut a healthy wait there, so a long poll runs with no +/// sends no headers until a session is idle, a shell command exits, a command finishes its work, a +/// generation completes, or a plugin's method returns. A network window would cut a healthy wait there, so a long poll runs with no /// window and the caller's token is its deadline, as upstream's own client bounds it; a client-wide /// value does not reach it, only a per-call one does. /// diff --git a/tests/OpenCode.Sdk.Tests/Transport/NetworkTimeoutTests.cs b/tests/OpenCode.Sdk.Tests/Transport/NetworkTimeoutTests.cs index 49e8fb46c..7dea71900 100644 --- a/tests/OpenCode.Sdk.Tests/Transport/NetworkTimeoutTests.cs +++ b/tests/OpenCode.Sdk.Tests/Transport/NetworkTimeoutTests.cs @@ -1,3 +1,4 @@ +using System.Net; using System.Threading.Channels; using OpenCode.Sdk.Internal; using OpenCode.Sdk.Tests.Support; @@ -9,9 +10,9 @@ namespace OpenCode.Sdk.Tests.Transport; /// /// The network timeout as a caller sets it: the client's value bounds an ordinary call, a /// per-call value wins either way, and a long-poll operation runs with no window unless the call -/// sets one, because its server answers only when the work ends. The server here never answers, -/// so the window alone decides when the exchange is cut; the clock is the test's, and each check -/// reads the token the exchange was sent under, which the window cancels the moment it passes. +/// sets one, because its server answers only when the work ends. The server here holds its +/// answer, so the window alone decides when the exchange is cut; the clock is the test's, and each +/// check reads the token the exchange was sent under, which the window cancels the moment it passes. /// public sealed class NetworkTimeoutTests { @@ -19,6 +20,10 @@ public sealed class NetworkTimeoutTests private const string LongPollOperation = "session.shell"; + private const string RpcOperation = "rpc.call"; + + private const string PluginAnswer = "plugin answer"; + private static readonly TimeSpan DefaultWindow = TimeSpan.FromSeconds(100); private static readonly TimeSpan ClientWindow = TimeSpan.FromMinutes(2); @@ -94,6 +99,29 @@ public async Task A_Long_Poll_Should_Be_Cut_When_Its_Call_Window_Passes() await scenario.AssertCutExactlyAtAsync(ShortCallWindow); } + [Test] + public async Task An_Rpc_Call_Should_Complete_With_Its_Answer_After_The_Client_Window_Passes() + { + var time = new ManualTimeProvider(); + using var handler = new HoldingHandler(); + using var httpClient = new HttpClient(handler, disposeHandler: false); + using var pipeline = PipelineFactory.Create(httpClient, networkTimeout: ClientWindow, time: time); + var adapter = new RecordingResponseAdapter(); + var call = pipeline.ExecuteAsync(RpcOperation, HttpMethod.Post, "/api/rpc/test/method", adapter, null, CancellationToken.None); + var sentUnder = await handler.WaitForSendAsync(HangGuard.Bound); + + time.Advance(ClientWindow + OneTick); + await Assert.That(sentUnder.IsCancellationRequested).IsFalse(); + + using var answer = new HttpResponseMessage(HttpStatusCode.OK) + { + Content = new StringContent(PluginAnswer), + }; + handler.Answer(answer); + _ = await call.WaitAsync(HangGuard.Bound, CancellationToken.None); + await Assert.That(adapter.AdaptedRawBody).IsEqualTo(PluginAnswer); + } + [Test] public async Task The_Long_Polls_Should_Be_The_Operations_That_Hold_Their_Headers_Until_The_Work_Ends() { @@ -105,6 +133,7 @@ public async Task The_Long_Polls_Should_Be_The_Operations_That_Hold_Their_Header ["experimental.generate.image"] = new(Reconnects: false, HasNoNetworkWindow: true), ["experimental.generate.text"] = new(Reconnects: false, HasNoNetworkWindow: true), ["experimental.session.wait"] = new(Reconnects: false, HasNoNetworkWindow: true), + ["rpc.call"] = new(Reconnects: false, HasNoNetworkWindow: true), ["session.command"] = new(Reconnects: false, HasNoNetworkWindow: true), ["session.generate"] = new(Reconnects: false, HasNoNetworkWindow: true), ["session.shell"] = new(Reconnects: false, HasNoNetworkWindow: true), @@ -148,13 +177,13 @@ public async Task Options_Should_Accept_A_Window_A_Timer_Can_Run(long ticks) } /// - /// A server that never answers, a pipeline on a manual clock, and one call already waiting + /// A server that holds its answer, a pipeline on a manual clock, and one call already waiting /// under its window. The handler hands back the token the exchange was sent under; the window /// cancels it synchronously when it passes, so it is the exact witness of a cut. /// private sealed class HangingScenario : IDisposable { - private readonly NeverAnsweringHandler _handler; + private readonly HoldingHandler _handler; private readonly HttpClient _httpClient; private readonly Pipeline _pipeline; private readonly ManualTimeProvider _time; @@ -162,7 +191,7 @@ private sealed class HangingScenario : IDisposable private readonly Task _call; private readonly CancellationToken _sentUnder; - private HangingScenario(NeverAnsweringHandler handler, HttpClient httpClient, Pipeline pipeline, ManualTimeProvider time, + private HangingScenario(HoldingHandler handler, HttpClient httpClient, Pipeline pipeline, ManualTimeProvider time, CancellationTokenSource caller, Task call, CancellationToken sentUnder) { _handler = handler; @@ -177,7 +206,7 @@ private HangingScenario(NeverAnsweringHandler handler, HttpClient httpClient, Pi public static async Task StartAsync(string operationId, TimeSpan? clientWindow, TimeSpan? callWindow) { var time = new ManualTimeProvider(); - var handler = new NeverAnsweringHandler(); + var handler = new HoldingHandler(); var httpClient = new HttpClient(handler, disposeHandler: false); var pipeline = PipelineFactory.Create(httpClient, networkTimeout: clientWindow, time: time); var caller = new CancellationTokenSource(); @@ -218,21 +247,25 @@ public void Dispose() } } - /// Answers nothing until the exchange is cancelled, as a server busy with long work does, and reports the token it was sent under. - private sealed class NeverAnsweringHandler : HttpMessageHandler + /// + /// Holds its answer until the test releases one or the exchange is cancelled, as a server busy + /// with long work does, and reports the token the exchange was sent under. + /// + private sealed class HoldingHandler : HttpMessageHandler { private readonly Channel _received = Channel.CreateUnbounded(); + private readonly Channel _answer = Channel.CreateUnbounded(); /// Completes with the token of the first exchange the handler receives. public Task WaitForSendAsync(TimeSpan hangGuard) => _received.Reader.ReadAsync().AsTask().WaitAsync(hangGuard); + /// Releases the held answer to the waiting exchange. + public void Answer(HttpResponseMessage answer) => _ = _answer.Writer.TryWrite(answer); + protected override async Task SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) { _ = _received.Writer.TryWrite(cancellationToken); - - // The answer is never given, so only the exchange's token ends the wait. - var answer = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); - return await answer.Task.WaitAsync(cancellationToken).ConfigureAwait(false); + return await _answer.Reader.ReadAsync(cancellationToken).ConfigureAwait(false); } } } diff --git a/tests/OpenCode.Sdk.Tools.Tests/Generator/Emission/HandWrittenTransportTableTests.cs b/tests/OpenCode.Sdk.Tools.Tests/Generator/Emission/HandWrittenTransportTableTests.cs index 2c2693d5a..77d849a42 100644 --- a/tests/OpenCode.Sdk.Tools.Tests/Generator/Emission/HandWrittenTransportTableTests.cs +++ b/tests/OpenCode.Sdk.Tools.Tests/Generator/Emission/HandWrittenTransportTableTests.cs @@ -35,6 +35,7 @@ await Assert.That(ids).IsEquivalentTo( "experimental.generate.image", "experimental.generate.text", "experimental.session.wait", + "rpc.call", "session.command", "session.generate", "session.shell",