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
13 changes: 12 additions & 1 deletion packages/codework/src/cli/cmd/handlers/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ interface RenderState {
readonly usage: UsageSummary;
readonly textSeen: boolean;
readonly textEndsWithNewline: boolean;
readonly reason?: EventList.LLMEnded["data"]["reason"];
}

const initialRenderState: RenderState = {
Expand All @@ -47,7 +48,11 @@ const render = (state: Ref.Ref<RenderState>, ended?: Queue.Queue<string>) =>
return;
}
if (isLLMEnded(event)) {
yield* Ref.update(state, (current) => ({ ...current, usage: addUsage(current.usage, event.data.message) }));
yield* Ref.update(state, (current) => ({
...current,
usage: addUsage(current.usage, event.data.message),
reason: event.data.reason,
}));
return;
}
if (ended !== undefined && isTurnEnded(event)) {
Expand All @@ -59,6 +64,12 @@ const finish = Effect.fn("CLI.finish")(function* (state: Ref.Ref<RenderState>, c
const rendered = yield* Ref.get(state);
if (rendered.textSeen && !rendered.textEndsWithNewline) yield* writeOut("\n");
yield* writeError(usage(rendered.usage, columns));
if (rendered.reason === "length") {
yield* writeError("Response was truncated: the model reached its output token limit.\n");
yield* Effect.sync(() => {
process.exitCode = 1;
});
}
});

const awaitMessage = Effect.fn("CLI.awaitMessage")(function* (ended: Queue.Queue<string>, messageId: string) {
Expand Down
2 changes: 0 additions & 2 deletions packages/codework/src/server/contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,6 @@ export const ThreadInfo = Schema.Struct({
foundation: optional(Schema.String),
/** Where it runs instead of the session's space and directory. */
placement: optional(Schema.Struct({ spaceId: Schema.String, directory: Session.AbsolutePath })),
/** Its unfinished run was cut off by restarts too often to resume on its own; the next prompt runs it. */
parked: optional(Schema.Boolean),
usage: ThreadUsage,
session: SessionInfo,
});
Expand Down
1 change: 0 additions & 1 deletion packages/codework/src/server/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ const toThreadInfo = (info: Session.ThreadInfo): ThreadInfo => ({
...(info.thinkingLevel === undefined ? {} : { thinkingLevel: info.thinkingLevel }),
...(info.foundation === undefined ? {} : { foundation: info.foundation }),
...(info.placement === undefined ? {} : { placement: info.placement }),
...(info.parked ? { parked: true } : {}),
usage: info.usage,
session: toSessionInfo(info.session),
});
Expand Down
5 changes: 5 additions & 0 deletions packages/codework/test/__artifacts__/cli.truncated.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"status": 1,
"stdout": "The answer starts here and\n",
"warning": "Response was truncated: the model reached its output token limit.\n"
}
191 changes: 191 additions & 0 deletions packages/codework/test/__artifacts__/recovery.crashloop.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,191 @@
{
"log": [
{
"type": "session.created"
},
{
"type": "session.prompt.admitted",
"delivery": "steer",
"text": "Keep crashing."
},
{
"type": "session.prompt.promoted",
"delivery": "steer",
"text": "Keep crashing."
},
{
"type": "session.thread.system.changed"
},
{
"type": "session.llm.started"
},
{
"type": "session.llm.failed",
"reason": "error",
"errorMessage": "The host stopped while the model was answering, on attempt 1",
"parts": []
},
{
"type": "session.llm.started"
},
{
"type": "session.llm.failed",
"reason": "error",
"errorMessage": "The host stopped while the model was answering, on attempt 2",
"parts": []
},
{
"type": "session.llm.started"
},
{
"type": "session.llm.failed",
"reason": "error",
"errorMessage": "The host stopped while the model was answering, on attempt 3",
"parts": []
},
{
"type": "session.llm.started"
},
{
"type": "session.llm.failed",
"reason": "error",
"errorMessage": "The host stopped while the model was answering, on attempt 4",
"parts": []
},
{
"type": "session.llm.started"
},
{
"type": "session.llm.failed",
"reason": "error",
"errorMessage": "The run was interrupted 5 times in a row; it is not resumed again",
"parts": []
},
{
"type": "session.run.failed",
"reason": "crashloop",
"message": "The run was interrupted 5 times in a row; it is not resumed again"
},
{
"type": "session.prompt.admitted",
"delivery": "steer",
"text": "Try again."
},
{
"type": "session.prompt.promoted",
"delivery": "steer",
"text": "Try again."
},
{
"type": "session.llm.started"
},
{
"type": "session.llm.ended",
"reason": "stop",
"parts": [
{
"text": "Back to normal."
}
]
},
{
"type": "session.turn.ended"
}
],
"history": [
{
"role": "user",
"state": "committed",
"parts": [
{
"text": "Keep crashing."
}
]
},
{
"role": "assistant",
"state": "aborted",
"parts": []
},
{
"role": "assistant",
"state": "aborted",
"parts": []
},
{
"role": "assistant",
"state": "aborted",
"parts": []
},
{
"role": "assistant",
"state": "aborted",
"parts": []
},
{
"role": "assistant",
"state": "aborted",
"parts": []
},
{
"role": "user",
"state": "committed",
"parts": [
{
"text": "Try again."
}
]
},
{
"role": "assistant",
"state": "committed",
"parts": [
{
"text": "Back to normal."
}
]
}
],
"requests": [
[
{
"role": "user",
"text": "Keep crashing."
}
],
[
{
"role": "user",
"text": "Keep crashing."
}
],
[
{
"role": "user",
"text": "Keep crashing."
}
],
[
{
"role": "user",
"text": "Keep crashing."
}
],
[
{
"role": "user",
"text": "Keep crashing."
}
],
[
{
"role": "user",
"text": "Keep crashing."
},
{
"role": "user",
"text": "Try again."
}
]
]
}
89 changes: 89 additions & 0 deletions packages/codework/test/__artifacts__/recovery.interrupted.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
{
"log": [
{
"type": "session.created"
},
{
"type": "session.prompt.admitted",
"delivery": "steer",
"text": "First."
},
{
"type": "session.prompt.promoted",
"delivery": "steer",
"text": "First."
},
{
"type": "session.thread.system.changed"
},
{
"type": "session.llm.started"
},
{
"type": "session.prompt.admitted",
"delivery": "followUp",
"text": "Then this."
},
{
"type": "session.llm.failed",
"reason": "error",
"errorMessage": "The host stopped while the model was answering, on attempt 1",
"parts": []
},
{
"type": "session.llm.started"
},
{
"type": "session.llm.failed",
"reason": "error",
"errorMessage": "The host stopped while the model was answering, on attempt 2",
"parts": []
},
{
"type": "session.run.failed",
"reason": "interrupted",
"message": "The host stopped while the model was answering, on attempt 2"
}
],
"history": [
{
"role": "user",
"state": "committed",
"parts": [
{
"text": "First."
}
]
},
{
"role": "assistant",
"state": "aborted",
"parts": []
},
{
"role": "assistant",
"state": "aborted",
"parts": []
}
],
"queue": [
{
"delivery": "followUp",
"text": "Then this."
}
],
"requests": [
[
{
"role": "user",
"text": "First."
}
],
[
{
"role": "user",
"text": "First."
}
]
]
}
Loading
Loading