Skip to content
Open
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
5 changes: 3 additions & 2 deletions apps/server/src/orchestration/projector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -575,15 +575,16 @@ export function projectEvent(
return nextBase;
}

const effectiveTurnCount = Math.max(1, payload.turnCount);
const checkpoints = thread.checkpoints
.filter((entry) => entry.checkpointTurnCount <= payload.turnCount)
.filter((entry) => entry.checkpointTurnCount <= effectiveTurnCount)
Comment thread
macroscopeapp[bot] marked this conversation as resolved.
.toSorted((left, right) => left.checkpointTurnCount - right.checkpointTurnCount)
.slice(-MAX_THREAD_CHECKPOINTS);
const retainedTurnIds = new Set(checkpoints.map((checkpoint) => checkpoint.turnId));
const messages = retainThreadMessagesAfterRevert(
thread.messages,
retainedTurnIds,
payload.turnCount,
effectiveTurnCount,
).slice(-MAX_THREAD_MESSAGES);
const proposedPlans = retainThreadProposedPlansAfterRevert(
thread.proposedPlans,
Expand Down
14 changes: 14 additions & 0 deletions apps/web/src/components/ChatView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ import {
} from "../pendingUserInput";
import {
selectProjectsAcrossEnvironments,
selectThreadByRef,
selectThreadsAcrossEnvironments,
useStore,
} from "../store";
Expand Down Expand Up @@ -2379,6 +2380,17 @@ export default function ChatView(props: ChatViewProps) {
turnCount,
createdAt: new Date().toISOString(),
});

Comment thread
shoaib050326 marked this conversation as resolved.
const threadRef = scopeThreadRef(activeThread.environmentId, activeThread.id);
const updatedThread = selectThreadByRef(useStore.getState(), threadRef);
if (updatedThread && promptRef.current.trim().length === 0) {
const lastUserMessage = [...updatedThread.messages]
.reverse()
.find((m) => m.role === "user");
Comment thread
shoaib050326 marked this conversation as resolved.
if (lastUserMessage) {
setComposerDraftPrompt(composerDraftTarget, lastUserMessage.text);
Comment thread
macroscopeapp[bot] marked this conversation as resolved.
}
}
Comment thread
cursor[bot] marked this conversation as resolved.
} catch (err) {
setThreadError(
activeThread.id,
Expand All @@ -2389,11 +2401,13 @@ export default function ChatView(props: ChatViewProps) {
},
[
activeThread,
composerDraftTarget,
environmentId,
isConnecting,
isRevertingCheckpoint,
isSendBusy,
phase,
setComposerDraftPrompt,
setThreadError,
],
);
Expand Down
80 changes: 80 additions & 0 deletions apps/web/src/store.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1015,4 +1015,84 @@ describe("incremental orchestration updates", () => {
});
expect(threadsOf(next)[0]?.latestTurn?.sourceProposedPlan).toBeUndefined();
});

it("retains first turn messages when reverting single-turn conversation (turnCount=0)", () => {
const state = makeState(
makeThread({
messages: [
{
id: MessageId.make("user-1"),
role: "user",
text: "hello",
turnId: TurnId.make("turn-1"),
createdAt: "2026-02-27T00:00:00.000Z",
completedAt: "2026-02-27T00:00:00.000Z",
streaming: false,
},
{
id: MessageId.make("assistant-1"),
role: "assistant",
text: "hi there",
turnId: TurnId.make("turn-1"),
createdAt: "2026-02-27T00:00:01.000Z",
completedAt: "2026-02-27T00:00:01.000Z",
streaming: false,
},
],
proposedPlans: [
{
id: "plan-1",
turnId: TurnId.make("turn-1"),
planMarkdown: "plan 1",
implementedAt: null,
implementationThreadId: null,
createdAt: "2026-02-27T00:00:00.000Z",
updatedAt: "2026-02-27T00:00:00.000Z",
},
],
activities: [
{
id: EventId.make("activity-1"),
tone: "info",
kind: "step",
summary: "step one",
payload: {},
turnId: TurnId.make("turn-1"),
createdAt: "2026-02-27T00:00:00.000Z",
},
],
turnDiffSummaries: [
{
turnId: TurnId.make("turn-1"),
completedAt: "2026-02-27T00:00:01.000Z",
status: "ready",
checkpointTurnCount: 1,
checkpointRef: CheckpointRef.make("ref-1"),
files: [],
},
],
}),
);

const next = applyOrchestrationEvent(
state,
makeEvent("thread.reverted", {
threadId: ThreadId.make("thread-1"),
turnCount: 0,
}),
localEnvironmentId,
);

expect(threadsOf(next)[0]?.messages.map((message) => message.id)).toEqual([
"user-1",
"assistant-1",
]);
expect(threadsOf(next)[0]?.proposedPlans.map((plan) => plan.id)).toEqual(["plan-1"]);
expect(threadsOf(next)[0]?.activities.map((activity) => activity.id)).toEqual([
EventId.make("activity-1"),
]);
expect(threadsOf(next)[0]?.turnDiffSummaries.map((summary) => summary.turnId)).toEqual([
TurnId.make("turn-1"),
]);
});
});
5 changes: 3 additions & 2 deletions apps/web/src/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1562,11 +1562,12 @@ function applyEnvironmentOrchestrationEvent(

case "thread.reverted":
return updateThreadState(state, event.payload.threadId, (thread) => {
const effectiveTurnCount = Math.max(1, event.payload.turnCount);
const turnDiffSummaries = thread.turnDiffSummaries
.filter(
(entry) =>
entry.checkpointTurnCount !== undefined &&
Comment thread
shoaib050326 marked this conversation as resolved.
entry.checkpointTurnCount <= event.payload.turnCount,
entry.checkpointTurnCount <= effectiveTurnCount,
)
.toSorted(
(left, right) =>
Expand All @@ -1578,7 +1579,7 @@ function applyEnvironmentOrchestrationEvent(
const messages = retainThreadMessagesAfterRevert(
thread.messages,
retainedTurnIds,
event.payload.turnCount,
effectiveTurnCount,
).slice(-MAX_THREAD_MESSAGES);
const proposedPlans = retainThreadProposedPlansAfterRevert(
thread.proposedPlans,
Expand Down
Loading