Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion docs/architecture/client-runtime.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 4 additions & 3 deletions docs/guide/requests.md
Original file line number Diff line number Diff line change
@@ -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`:
Expand Down Expand Up @@ -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;
Expand All @@ -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:

Expand Down
10 changes: 10 additions & 0 deletions spec/receipt.json
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
]
}
]
}
14 changes: 14 additions & 0 deletions spec/source-watch.json
Original file line number Diff line number Diff line change
Expand Up @@ -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."
}
}
]
}
]
}
6 changes: 3 additions & 3 deletions src/OpenCode.Sdk/Common/Options/OpenCodeClientOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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, <see langword="null"/>, 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
/// <see cref="OpenCodeRequestOptions.NetworkTimeout"/> bounds them for that call. Accepts a
/// value above zero and at most <see cref="int.MaxValue"/> milliseconds, or
/// <see cref="Timeout.InfiniteTimeSpan"/> for no window.
Expand Down
1 change: 1 addition & 0 deletions src/OpenCode.Sdk/Internal/Operations/OperationTraits.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
4 changes: 2 additions & 2 deletions src/OpenCode.Sdk/Internal/Transport/NetworkTimeouts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ namespace OpenCode.Sdk.Internal;
/// </summary>
/// <remarks>
/// 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.
/// </remarks>
Expand Down
59 changes: 46 additions & 13 deletions tests/OpenCode.Sdk.Tests/Transport/NetworkTimeoutTests.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Net;
using System.Threading.Channels;
using OpenCode.Sdk.Internal;
using OpenCode.Sdk.Tests.Support;
Expand All @@ -9,16 +10,20 @@ namespace OpenCode.Sdk.Tests.Transport;
/// <summary>
/// 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.
/// </summary>
public sealed class NetworkTimeoutTests
{
private const string OrdinaryOperation = "test.operation";

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);
Expand Down Expand Up @@ -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()
{
Expand All @@ -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),
Expand Down Expand Up @@ -148,21 +177,21 @@ public async Task Options_Should_Accept_A_Window_A_Timer_Can_Run(long ticks)
}

/// <summary>
/// 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.
/// </summary>
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;
private readonly CancellationTokenSource _caller;
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;
Expand All @@ -177,7 +206,7 @@ private HangingScenario(NeverAnsweringHandler handler, HttpClient httpClient, Pi
public static async Task<HangingScenario> 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();
Expand Down Expand Up @@ -218,21 +247,25 @@ public void Dispose()
}
}

/// <summary>Answers nothing until the exchange is cancelled, as a server busy with long work does, and reports the token it was sent under.</summary>
private sealed class NeverAnsweringHandler : HttpMessageHandler
/// <summary>
/// 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.
/// </summary>
private sealed class HoldingHandler : HttpMessageHandler
{
private readonly Channel<CancellationToken> _received = Channel.CreateUnbounded<CancellationToken>();
private readonly Channel<HttpResponseMessage> _answer = Channel.CreateUnbounded<HttpResponseMessage>();

/// <summary>Completes with the token of the first exchange the handler receives.</summary>
public Task<CancellationToken> WaitForSendAsync(TimeSpan hangGuard) => _received.Reader.ReadAsync().AsTask().WaitAsync(hangGuard);

/// <summary>Releases the held answer to the waiting exchange.</summary>
public void Answer(HttpResponseMessage answer) => _ = _answer.Writer.TryWrite(answer);

protected override async Task<HttpResponseMessage> 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<HttpResponseMessage>(TaskCreationOptions.RunContinuationsAsynchronously);
return await answer.Task.WaitAsync(cancellationToken).ConfigureAwait(false);
return await _answer.Reader.ReadAsync(cancellationToken).ConfigureAwait(false);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Loading