chore(foundry): improve sandbox impl + status pill (#252)

* Improve Daytona sandbox provisioning and frontend UI

Refactor git clone script in Daytona provider to use cleaner shell logic for GitHub token authentication and branch checkout. Add support for private repository clones with token-based auth. Improve Daytona provider error handling and git configuration setup.

Frontend improvements include enhanced dev panel, workspace dashboard, sidebar navigation, and UI components for better task/session management. Update interest manager and backend client to support improved session state handling.

Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>

* Add header status pill showing task/session/sandbox state

Surface aggregate status (error, provisioning, running, ready, no sandbox)
as a colored pill in the transcript panel header. Integrates task runtime
status, session status, and sandbox availability via the sandboxProcesses
interest topic so the pill accurately reflects unreachable sandboxes.

Includes mock tasks demonstrating error, provisioning, and running states,
unit tests for deriveHeaderStatus, and workspace-dashboard integration.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

---------

Co-authored-by: Claude Haiku 4.5 <noreply@anthropic.com>
This commit is contained in:
Nathan Flurry 2026-03-14 12:14:06 -07:00 committed by GitHub
parent 5a1b32a271
commit 70d31f819c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
82 changed files with 2625 additions and 4166 deletions

View file

@ -82,6 +82,7 @@ const DEFAULT_BASE_URL = "http://sandbox-agent";
const DEFAULT_REPLAY_MAX_EVENTS = 50;
const DEFAULT_REPLAY_MAX_CHARS = 12_000;
const EVENT_INDEX_SCAN_EVENTS_LIMIT = 500;
const MAX_EVENT_INDEX_INSERT_RETRIES = 3;
const SESSION_CANCEL_METHOD = "session/cancel";
const MANUAL_CANCEL_ERROR = "Manual session/cancel calls are not allowed. Use destroySession(sessionId) instead.";
const HEALTH_WAIT_MIN_DELAY_MS = 500;
@ -841,6 +842,7 @@ export class SandboxAgent {
private readonly pendingPermissionRequests = new Map<string, PendingPermissionRequestState>();
private readonly nextSessionEventIndexBySession = new Map<string, number>();
private readonly seedSessionEventIndexBySession = new Map<string, Promise<void>>();
private readonly pendingObservedEnvelopePersistenceBySession = new Map<string, Promise<void>>();
constructor(options: SandboxAgentConnectOptions) {
const baseUrl = options.baseUrl?.trim();
@ -906,6 +908,7 @@ export class SandboxAgent {
this.liveConnections.clear();
const pending = [...this.pendingLiveConnections.values()];
this.pendingLiveConnections.clear();
this.pendingObservedEnvelopePersistenceBySession.clear();
const pendingSettled = await Promise.allSettled(pending);
for (const item of pendingSettled) {
@ -969,7 +972,6 @@ export class SandboxAgent {
};
await this.persist.updateSession(record);
this.nextSessionEventIndexBySession.set(record.id, 1);
live.bindSession(record.id, record.agentSessionId);
let session = this.upsertSessionHandle(record);
@ -1639,7 +1641,9 @@ export class SandboxAgent {
agent,
serverId,
onObservedEnvelope: (connection, envelope, direction, localSessionId) => {
void this.persistObservedEnvelope(connection, envelope, direction, localSessionId);
void this.enqueueObservedEnvelopePersistence(connection, envelope, direction, localSessionId).catch((error) => {
console.error("Failed to persist observed sandbox-agent envelope", error);
});
},
onPermissionRequest: async (connection, localSessionId, agentSessionId, request) =>
this.enqueuePermissionRequest(connection, localSessionId, agentSessionId, request),
@ -1675,17 +1679,32 @@ export class SandboxAgent {
return;
}
const event: SessionEvent = {
id: randomId(),
eventIndex: await this.allocateSessionEventIndex(localSessionId),
sessionId: localSessionId,
createdAt: nowMs(),
connectionId: connection.connectionId,
sender: direction === "outbound" ? "client" : "agent",
payload: cloneEnvelope(envelope),
};
let event: SessionEvent | null = null;
for (let attempt = 0; attempt < MAX_EVENT_INDEX_INSERT_RETRIES; attempt += 1) {
event = {
id: randomId(),
eventIndex: await this.allocateSessionEventIndex(localSessionId),
sessionId: localSessionId,
createdAt: nowMs(),
connectionId: connection.connectionId,
sender: direction === "outbound" ? "client" : "agent",
payload: cloneEnvelope(envelope),
};
try {
await this.persist.insertEvent(event);
break;
} catch (error) {
if (!isSessionEventIndexConflict(error) || attempt === MAX_EVENT_INDEX_INSERT_RETRIES - 1) {
throw error;
}
}
}
if (!event) {
return;
}
await this.persist.insertEvent(event);
await this.persistSessionStateFromEvent(localSessionId, envelope, direction);
const listeners = this.eventListeners.get(localSessionId);
@ -1698,6 +1717,34 @@ export class SandboxAgent {
}
}
private async enqueueObservedEnvelopePersistence(
connection: LiveAcpConnection,
envelope: AnyMessage,
direction: AcpEnvelopeDirection,
localSessionId: string | null,
): Promise<void> {
if (!localSessionId) {
return;
}
const previous = this.pendingObservedEnvelopePersistenceBySession.get(localSessionId) ?? Promise.resolve();
const current = previous
.catch(() => {
// Keep later envelope persistence moving even if an earlier write failed.
})
.then(() => this.persistObservedEnvelope(connection, envelope, direction, localSessionId));
this.pendingObservedEnvelopePersistenceBySession.set(localSessionId, current);
try {
await current;
} finally {
if (this.pendingObservedEnvelopePersistenceBySession.get(localSessionId) === current) {
this.pendingObservedEnvelopePersistenceBySession.delete(localSessionId);
}
}
}
private async persistSessionStateFromEvent(sessionId: string, envelope: AnyMessage, direction: AcpEnvelopeDirection): Promise<void> {
if (direction !== "inbound") {
return;
@ -2066,6 +2113,14 @@ export class SandboxAgent {
}
}
function isSessionEventIndexConflict(error: unknown): boolean {
if (!(error instanceof Error)) {
return false;
}
return /UNIQUE constraint failed: .*session_id, .*event_index/.test(error.message);
}
type PendingPermissionRequestState = {
id: string;
sessionId: string;

View file

@ -5,7 +5,15 @@ import { dirname, resolve } from "node:path";
import { join } from "node:path";
import { fileURLToPath } from "node:url";
import { tmpdir } from "node:os";
import { InMemorySessionPersistDriver, SandboxAgent, type SessionEvent } from "../src/index.ts";
import {
InMemorySessionPersistDriver,
SandboxAgent,
type ListEventsRequest,
type ListPage,
type SessionEvent,
type SessionPersistDriver,
type SessionRecord,
} from "../src/index.ts";
import { spawnSandboxAgent, isNodeRuntime, type SandboxAgentSpawnHandle } from "../src/spawn.ts";
import { prepareMockAgentDataHome } from "./helpers/mock-agent.ts";
import WebSocket from "ws";
@ -40,6 +48,44 @@ function sleep(ms: number): Promise<void> {
return new Promise((resolve) => setTimeout(resolve, ms));
}
class StrictUniqueSessionPersistDriver implements SessionPersistDriver {
private readonly events = new InMemorySessionPersistDriver({
maxEventsPerSession: 500,
});
private readonly eventIndexesBySession = new Map<string, Set<number>>();
async getSession(id: string): Promise<SessionRecord | null> {
return this.events.getSession(id);
}
async listSessions(request?: { cursor?: string; limit?: number }): Promise<ListPage<SessionRecord>> {
return this.events.listSessions(request);
}
async updateSession(session: SessionRecord): Promise<void> {
await this.events.updateSession(session);
}
async listEvents(request: ListEventsRequest): Promise<ListPage<SessionEvent>> {
return this.events.listEvents(request);
}
async insertEvent(event: SessionEvent): Promise<void> {
await sleep(5);
const indexes = this.eventIndexesBySession.get(event.sessionId) ?? new Set<number>();
if (indexes.has(event.eventIndex)) {
throw new Error("UNIQUE constraint failed: sandbox_agent_events.session_id, sandbox_agent_events.event_index");
}
indexes.add(event.eventIndex);
this.eventIndexesBySession.set(event.sessionId, indexes);
await sleep(5);
await this.events.insertEvent(event);
}
}
async function waitFor<T>(fn: () => T | undefined | null, timeoutMs = 6000, stepMs = 30): Promise<T> {
const started = Date.now();
while (Date.now() - started < timeoutMs) {
@ -207,6 +253,27 @@ describe("Integration: TypeScript SDK flat session API", () => {
await sdk.dispose();
});
it("preserves observed event indexes across session creation follow-up calls", async () => {
const persist = new StrictUniqueSessionPersistDriver();
const sdk = await SandboxAgent.connect({
baseUrl,
token,
persist,
});
const session = await sdk.createSession({ agent: "mock" });
const prompt = await session.prompt([{ type: "text", text: "preserve event indexes" }]);
expect(prompt.stopReason).toBe("end_turn");
const events = await waitForAsync(async () => {
const page = await sdk.getEvents({ sessionId: session.id, limit: 200 });
return page.items.length >= 4 ? page : null;
});
expect(new Set(events.items.map((event) => event.eventIndex)).size).toBe(events.items.length);
await sdk.dispose();
});
it("covers agent query flags and filesystem HTTP helpers", async () => {
const sdk = await SandboxAgent.connect({
baseUrl,