feat(worker): persistent streaming Claude session + live session registry
StreamingClaudeSession drives claude --input-format stream-json over a kept- open stdin: sends user messages, interrupts the in-flight turn via the verified control_request protocol, and tracks turn state from result events (treating an interrupt-aborted error_during_execution result as turn-ended). IClaudeStreamTransport abstracts the process I/O so it is unit-tested with a fake (no real claude). LiveSessionRegistry maps taskId -> live session for the hub to route into. Backs the upcoming in-app interactive sessions; autonomous task execution untouched.
This commit is contained in:
@@ -0,0 +1,92 @@
|
||||
using ClaudeDo.Worker.Runner;
|
||||
using ClaudeDo.Worker.Runner.Interfaces;
|
||||
|
||||
namespace ClaudeDo.Worker.Tests.Runner;
|
||||
|
||||
public sealed class LiveSessionRegistryTests
|
||||
{
|
||||
private sealed class FakeLiveSession : ILiveSession
|
||||
{
|
||||
public bool StopCalled { get; private set; }
|
||||
public bool IsTurnInFlight => false;
|
||||
|
||||
public Task SendUserMessageAsync(string text, CancellationToken ct) => Task.CompletedTask;
|
||||
public Task InterruptAsync(CancellationToken ct) => Task.CompletedTask;
|
||||
|
||||
public Task StopAsync()
|
||||
{
|
||||
StopCalled = true;
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public ValueTask DisposeAsync() => ValueTask.CompletedTask;
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Register_ThenTryGet_ReturnsSession()
|
||||
{
|
||||
var registry = new LiveSessionRegistry();
|
||||
var session = new FakeLiveSession();
|
||||
|
||||
registry.Register("task-1", session);
|
||||
|
||||
Assert.True(registry.TryGet("task-1", out var retrieved));
|
||||
Assert.Same(session, retrieved);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void TryGet_Missing_ReturnsFalse()
|
||||
{
|
||||
var registry = new LiveSessionRegistry();
|
||||
|
||||
Assert.False(registry.TryGet("no-such-task", out _));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Unregister_RemovesSession()
|
||||
{
|
||||
var registry = new LiveSessionRegistry();
|
||||
registry.Register("task-1", new FakeLiveSession());
|
||||
registry.Unregister("task-1");
|
||||
|
||||
Assert.False(registry.TryGet("task-1", out _));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Register_WhenSessionAlreadyExists_StopsPreviousSession()
|
||||
{
|
||||
var registry = new LiveSessionRegistry();
|
||||
var first = new FakeLiveSession();
|
||||
var second = new FakeLiveSession();
|
||||
|
||||
registry.Register("task-1", first);
|
||||
registry.Register("task-1", second);
|
||||
|
||||
// Give the fire-and-forget stop a tick to run.
|
||||
await Task.Delay(50);
|
||||
|
||||
Assert.True(first.StopCalled);
|
||||
Assert.True(registry.TryGet("task-1", out var retrieved));
|
||||
Assert.Same(second, retrieved);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task StopAsync_StopsAndRemovesSession()
|
||||
{
|
||||
var registry = new LiveSessionRegistry();
|
||||
var session = new FakeLiveSession();
|
||||
registry.Register("task-1", session);
|
||||
|
||||
await registry.StopAsync("task-1");
|
||||
|
||||
Assert.True(session.StopCalled);
|
||||
Assert.False(registry.TryGet("task-1", out _));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task StopAsync_MissingTask_DoesNotThrow()
|
||||
{
|
||||
var registry = new LiveSessionRegistry();
|
||||
await registry.StopAsync("no-such-task"); // should not throw
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,173 @@
|
||||
using System.Text.Json;
|
||||
using ClaudeDo.Worker.Runner;
|
||||
using ClaudeDo.Worker.Tests.Infrastructure;
|
||||
using Microsoft.Extensions.Logging.Abstractions;
|
||||
|
||||
namespace ClaudeDo.Worker.Tests.Runner;
|
||||
|
||||
public sealed class StreamingClaudeSessionTests
|
||||
{
|
||||
private static StreamingClaudeSession Build(
|
||||
FakeClaudeStreamTransport transport,
|
||||
List<string> received)
|
||||
{
|
||||
return new StreamingClaudeSession(
|
||||
transport,
|
||||
line => { received.Add(line); return Task.CompletedTask; },
|
||||
NullLogger<StreamingClaudeSession>.Instance);
|
||||
}
|
||||
|
||||
private static string ResultLine(bool isError = false, string subtype = "success") =>
|
||||
JsonSerializer.Serialize(new { type = "result", is_error = isError, subtype });
|
||||
|
||||
private static string UserMessageLine(string text) =>
|
||||
JsonSerializer.Serialize(new
|
||||
{
|
||||
type = "user",
|
||||
message = new { role = "user", content = new[] { new { type = "text", text } } },
|
||||
parent_tool_use_id = (string?)null
|
||||
});
|
||||
|
||||
// ---- Start sends first prompt as user-message, IsTurnInFlight = true ----
|
||||
|
||||
[Fact]
|
||||
public async Task Start_SendsFirstPromptAsUserMessage_AndTurnIsInFlight()
|
||||
{
|
||||
var transport = new FakeClaudeStreamTransport();
|
||||
var received = new List<string>();
|
||||
var session = Build(transport, received);
|
||||
|
||||
await session.StartAsync([], "/tmp", "hello world", CancellationToken.None);
|
||||
|
||||
Assert.True(session.IsTurnInFlight);
|
||||
Assert.Single(transport.Written);
|
||||
|
||||
using var doc = JsonDocument.Parse(transport.Written[0]);
|
||||
var root = doc.RootElement;
|
||||
Assert.Equal("user", root.GetProperty("type").GetString());
|
||||
var text = root.GetProperty("message").GetProperty("content")[0].GetProperty("text").GetString();
|
||||
Assert.Equal("hello world", text);
|
||||
|
||||
await session.DisposeAsync();
|
||||
}
|
||||
|
||||
// ---- Pushing a result line flips IsTurnInFlight to false ----
|
||||
|
||||
[Fact]
|
||||
public async Task PushingResultLine_FlipsIsTurnInFlightToFalse()
|
||||
{
|
||||
var transport = new FakeClaudeStreamTransport();
|
||||
var session = Build(transport, []);
|
||||
|
||||
await session.StartAsync([], "/tmp", "prompt", CancellationToken.None);
|
||||
Assert.True(session.IsTurnInFlight);
|
||||
|
||||
await transport.PushLineAsync(ResultLine());
|
||||
|
||||
Assert.False(session.IsTurnInFlight);
|
||||
|
||||
await session.DisposeAsync();
|
||||
}
|
||||
|
||||
// ---- Sending while in-flight: interrupt first, then user message after result ----
|
||||
|
||||
[Fact]
|
||||
public async Task SendWhileInFlight_WritesInterruptFirst_ThenUserMessage()
|
||||
{
|
||||
var transport = new FakeClaudeStreamTransport();
|
||||
var session = Build(transport, []);
|
||||
|
||||
await session.StartAsync([], "/tmp", "first", CancellationToken.None);
|
||||
// Written[0] = first user message. Turn is in flight.
|
||||
Assert.True(session.IsTurnInFlight);
|
||||
|
||||
// Fire the second send on a background task (it will block waiting for the turn to end).
|
||||
var sendTask = Task.Run(() => session.SendUserMessageAsync("second", CancellationToken.None));
|
||||
|
||||
// Give the background task time to reach the await-turn-ended point.
|
||||
await Task.Delay(50);
|
||||
|
||||
// Push a result line to unblock it.
|
||||
await transport.PushLineAsync(ResultLine());
|
||||
|
||||
await sendTask;
|
||||
|
||||
// Written[0] = first prompt, Written[1] = interrupt, Written[2] = second user message.
|
||||
Assert.True(transport.Written.Count >= 3, $"Expected ≥3 writes, got {transport.Written.Count}");
|
||||
|
||||
// Written[1] must be an interrupt control_request.
|
||||
using var interruptDoc = JsonDocument.Parse(transport.Written[1]);
|
||||
Assert.Equal("control_request", interruptDoc.RootElement.GetProperty("type").GetString());
|
||||
Assert.Equal("interrupt", interruptDoc.RootElement.GetProperty("request").GetProperty("subtype").GetString());
|
||||
|
||||
// Last write must be the user message with "second".
|
||||
using var userDoc = JsonDocument.Parse(transport.Written[^1]);
|
||||
Assert.Equal("user", userDoc.RootElement.GetProperty("type").GetString());
|
||||
var text = userDoc.RootElement.GetProperty("message").GetProperty("content")[0].GetProperty("text").GetString();
|
||||
Assert.Equal("second", text);
|
||||
|
||||
await session.DisposeAsync();
|
||||
}
|
||||
|
||||
// ---- Sending while idle writes user message with no interrupt ----
|
||||
|
||||
[Fact]
|
||||
public async Task SendWhileIdle_WritesUserMessageWithNoInterrupt()
|
||||
{
|
||||
var transport = new FakeClaudeStreamTransport();
|
||||
var session = Build(transport, []);
|
||||
|
||||
await session.StartAsync([], "/tmp", "first", CancellationToken.None);
|
||||
await transport.PushLineAsync(ResultLine()); // end the turn → idle
|
||||
|
||||
Assert.False(session.IsTurnInFlight);
|
||||
var countBefore = transport.Written.Count;
|
||||
|
||||
await session.SendUserMessageAsync("second", CancellationToken.None);
|
||||
|
||||
// Exactly one new write, no interrupt.
|
||||
Assert.Equal(countBefore + 1, transport.Written.Count);
|
||||
using var doc = JsonDocument.Parse(transport.Written[^1]);
|
||||
Assert.Equal("user", doc.RootElement.GetProperty("type").GetString());
|
||||
|
||||
await session.DisposeAsync();
|
||||
}
|
||||
|
||||
// ---- Result with is_error / error_during_execution still ends the turn ----
|
||||
|
||||
[Fact]
|
||||
public async Task ResultWithIsError_StillEndsTurn_NoThrow()
|
||||
{
|
||||
var transport = new FakeClaudeStreamTransport();
|
||||
var session = Build(transport, []);
|
||||
|
||||
await session.StartAsync([], "/tmp", "prompt", CancellationToken.None);
|
||||
Assert.True(session.IsTurnInFlight);
|
||||
|
||||
await transport.PushLineAsync(ResultLine(isError: true, subtype: "error_during_execution"));
|
||||
|
||||
Assert.False(session.IsTurnInFlight);
|
||||
|
||||
await session.DisposeAsync();
|
||||
}
|
||||
|
||||
// ---- onLine receives every pushed stdout line ----
|
||||
|
||||
[Fact]
|
||||
public async Task OnLine_ReceivesEveryPushedLine()
|
||||
{
|
||||
var transport = new FakeClaudeStreamTransport();
|
||||
var received = new List<string>();
|
||||
var session = Build(transport, received);
|
||||
|
||||
await session.StartAsync([], "/tmp", "prompt", CancellationToken.None);
|
||||
|
||||
var lines = new[] { "{\"type\":\"assistant\"}", "{\"type\":\"stream_event\"}", ResultLine() };
|
||||
foreach (var l in lines)
|
||||
await transport.PushLineAsync(l);
|
||||
|
||||
Assert.Equal(lines, received);
|
||||
|
||||
await session.DisposeAsync();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user