mirror of
https://github.com/anomalyco/opencode.git
synced 2026-04-27 00:05:26 +00:00
fix task_status stale terminal reads
This commit is contained in:
@@ -18,6 +18,22 @@ const ctx = {
|
||||
ask: async () => {},
|
||||
}
|
||||
|
||||
async function user(sessionID: string) {
|
||||
await Session.updateMessage({
|
||||
id: Identifier.ascending("message"),
|
||||
sessionID,
|
||||
role: "user",
|
||||
time: {
|
||||
created: Date.now(),
|
||||
},
|
||||
agent: "build",
|
||||
model: {
|
||||
providerID: "test-provider",
|
||||
modelID: "test-model",
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
async function assistant(input: { sessionID: string; text: string; error?: string }) {
|
||||
const msg = await Session.updateMessage({
|
||||
id: Identifier.ascending("message"),
|
||||
@@ -116,13 +132,13 @@ describe("tool.task_status", () => {
|
||||
const tool = await TaskStatusTool.init()
|
||||
|
||||
SessionStatus.set(session.id, { type: "busy" })
|
||||
setTimeout(async () => {
|
||||
const transition = Bun.sleep(150).then(async () => {
|
||||
SessionStatus.set(session.id, { type: "idle" })
|
||||
await assistant({
|
||||
sessionID: session.id,
|
||||
text: "finished later",
|
||||
})
|
||||
}, 150)
|
||||
})
|
||||
|
||||
const result = await tool.execute(
|
||||
{
|
||||
@@ -133,9 +149,83 @@ describe("tool.task_status", () => {
|
||||
ctx,
|
||||
)
|
||||
|
||||
await transition
|
||||
expect(result.output).toContain("state: completed")
|
||||
expect(result.output).toContain("finished later")
|
||||
},
|
||||
})
|
||||
})
|
||||
|
||||
test("returns error when child run fails", async () => {
|
||||
await using tmp = await tmpdir({ git: true })
|
||||
|
||||
await Instance.provide({
|
||||
directory: tmp.path,
|
||||
fn: async () => {
|
||||
const session = await Session.create({})
|
||||
const tool = await TaskStatusTool.init()
|
||||
|
||||
await assistant({
|
||||
sessionID: session.id,
|
||||
text: "",
|
||||
error: "child failed",
|
||||
})
|
||||
|
||||
const result = await tool.execute({ task_id: session.id }, ctx)
|
||||
expect(result.output).toContain("state: error")
|
||||
expect(result.output).toContain("child failed")
|
||||
expect(result.metadata.state).toBe("error")
|
||||
},
|
||||
})
|
||||
})
|
||||
|
||||
test("wait=true times out with timed_out metadata", async () => {
|
||||
await using tmp = await tmpdir({ git: true })
|
||||
|
||||
await Instance.provide({
|
||||
directory: tmp.path,
|
||||
fn: async () => {
|
||||
const session = await Session.create({})
|
||||
const tool = await TaskStatusTool.init()
|
||||
|
||||
SessionStatus.set(session.id, { type: "busy" })
|
||||
const result = await tool.execute(
|
||||
{
|
||||
task_id: session.id,
|
||||
wait: true,
|
||||
timeout_ms: 80,
|
||||
},
|
||||
ctx,
|
||||
)
|
||||
|
||||
expect(result.output).toContain("Timed out after 80ms")
|
||||
expect(result.metadata.timed_out).toBe(true)
|
||||
expect(result.metadata.state).toBe("running")
|
||||
SessionStatus.set(session.id, { type: "idle" })
|
||||
},
|
||||
})
|
||||
})
|
||||
|
||||
test("returns running for resumed task with a newer user turn", async () => {
|
||||
await using tmp = await tmpdir({ git: true })
|
||||
|
||||
await Instance.provide({
|
||||
directory: tmp.path,
|
||||
fn: async () => {
|
||||
const session = await Session.create({})
|
||||
const tool = await TaskStatusTool.init()
|
||||
|
||||
await user(session.id)
|
||||
await assistant({
|
||||
sessionID: session.id,
|
||||
text: "old done",
|
||||
})
|
||||
await user(session.id)
|
||||
|
||||
const result = await tool.execute({ task_id: session.id }, ctx)
|
||||
expect(result.output).toContain("state: running")
|
||||
expect(result.output).toContain("Task is starting.")
|
||||
},
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user