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
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,10 @@ export const sessionHandlers = HttpApiBuilder.group(InstanceHttpApi, "session",
sessionID: ctx.params.sessionID,
})
.pipe(Effect.mapError(() => new HttpApiError.BadRequest({})))
return HttpServerResponse.stream(Stream.make(JSON.stringify(message)).pipe(Stream.encodeText), {
const body = Stream.make(JSON.stringify(message)).pipe(Stream.encodeText)
const settledBody =
ctx.payload.noReply === true ? body : body.pipe(Stream.ensuring(runState.settle(ctx.params.sessionID)))
return HttpServerResponse.stream(settledBody, {
contentType: "application/json",
})
})
Expand Down
13 changes: 12 additions & 1 deletion packages/opencode/src/session/run-state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { SessionStatus } from "./status"
export interface Interface {
readonly assertNotBusy: (sessionID: SessionID) => Effect.Effect<void, Session.BusyError>
readonly cancel: (sessionID: SessionID) => Effect.Effect<void>
readonly settle: (sessionID: SessionID) => Effect.Effect<void>
readonly ensureRunning: (
sessionID: SessionID,
onInterrupt: Effect.Effect<SessionV1.WithParts>,
Expand Down Expand Up @@ -85,6 +86,16 @@ const layer = Layer.effect(
yield* existing.cancel
})

const settle = Effect.fn("SessionRunState.settle")(function* (sessionID: SessionID) {
const data = yield* InstanceState.get(state)
const existing = data.runners.get(sessionID)
if (existing?.busy) return
data.runners.delete(sessionID)
const current = yield* status.get(sessionID)
if (current.type === "idle") return
yield* status.set(sessionID, { type: "idle" })
})

const ensureRunning = Effect.fn("SessionRunState.ensureRunning")(function* (
sessionID: SessionID,
onInterrupt: Effect.Effect<SessionV1.WithParts>,
Expand All @@ -104,7 +115,7 @@ const layer = Layer.effect(
.pipe(Effect.catchTag("RunnerBusy", () => Effect.fail(busyError(sessionID))))
})

return Service.of({ assertNotBusy, cancel, ensureRunning, startShell })
return Service.of({ assertNotBusy, cancel, settle, ensureRunning, startShell })
}),
)

Expand Down
28 changes: 28 additions & 0 deletions packages/opencode/test/server/httpapi-sdk.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -805,6 +805,34 @@ describe("HttpApi SDK", () => {
),
)

serverPathParity("settles session status immediately after prompt stream completes", (serverPath) =>
withFakeLlm(serverPath, ({ sdk, llm }) =>
Effect.gen(function* () {
yield* llm.text("status settled", { usage: { input: 5, output: 3 } })
const session = yield* capture(() =>
sdk.session.create({
title: "status settles",
permission: [{ permission: "*", pattern: "*", action: "allow" }],
}),
)
const sessionID = String(record(session.data).id)
const prompt = yield* capture(() =>
sdk.session.prompt({
sessionID,
agent: "build",
model: { providerID: "test", modelID: "test-model" },
parts: [{ type: "text", text: "hello status" }],
}),
)
const status = yield* capture(() => sdk.session.status())

expect(prompt.status).toBe(200)
expect(status.status).toBe(200)
expect(record(status.data)[sessionID]).toBeUndefined()
}),
),
)

httpapi(
"includes project skills in REST API prompt context",
withFakeLlmProject("default", { setup: writeProjectSkill }, ({ sdk, llm }) =>
Expand Down
3 changes: 3 additions & 0 deletions packages/opencode/test/session/prompt.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1062,6 +1062,7 @@ it.instance(
const prompt = yield* SessionPrompt.Service
const sessions = yield* Session.Service
const status = yield* SessionStatus.Service
const run = yield* SessionRunState.Service

yield* llm.hang

Expand All @@ -1071,6 +1072,8 @@ it.instance(
const fiber = yield* prompt.loop({ sessionID: chat.id }).pipe(Effect.forkChild)
yield* llm.wait(1)
expect((yield* status.get(chat.id)).type).toBe("busy")
yield* run.settle(chat.id)
expect((yield* status.get(chat.id)).type).toBe("busy")
yield* prompt.cancel(chat.id)
yield* Fiber.await(fiber)
expect((yield* status.get(chat.id)).type).toBe("idle")
Expand Down
Loading