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
44 changes: 12 additions & 32 deletions apps/code/src/renderer/features/sessions/service/service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1527,7 +1527,7 @@ describe("SessionService", () => {
expect(mockSessionStoreSetters.appendEvents).not.toHaveBeenCalled();
});

it("processes a pending cloud log gap after active reconciliation finishes", async () => {
it("queues a pending cloud log gap when stale fetches can't fill it, without appending", async () => {
const service = getSessionService();
let sessionState = createMockSession({
taskRunId: "run-123",
Expand Down Expand Up @@ -1628,38 +1628,18 @@ describe("SessionService", () => {
});
resolveFirstLocalLogs("");

// The pending request must drain after the in-flight one resolves —
// verify the second readLocalLogs call eventually happens.
await vi.waitFor(() => {
expect(mockSessionStoreSetters.appendEvents).toHaveBeenCalledTimes(2);
});
expect(mockSessionStoreSetters.appendEvents).toHaveBeenNthCalledWith(
1,
"run-123",
[
expect.objectContaining({
message: expect.objectContaining({
params: { entry: firstEntry },
}),
}),
],
6,
);
expect(mockSessionStoreSetters.appendEvents).toHaveBeenNthCalledWith(
2,
"run-123",
[
expect.objectContaining({
message: expect.objectContaining({
params: { entry: secondEntry },
}),
}),
expect.objectContaining({
message: expect.objectContaining({
params: { entry: thirdEntry },
}),
}),
],
8,
);
expect(mockTrpcLogs.readLocalLogs.query).toHaveBeenCalledTimes(2);
});
// Stale fetches can't fill the gap; we must NOT append the snapshot's
// tail slice (positions [expectedCount-N, expectedCount]) on top of an
// events array that's still at processedLineCount=5 — that path used
// to corrupt the array with duplicates/gaps and ratchet
// processedLineCount past entries we don't actually have, leading to
// unbounded growth on long-running cloud runs.
expect(mockSessionStoreSetters.appendEvents).not.toHaveBeenCalled();
});
it("flips status to connected on _posthog/run_started", async () => {
const service = getSessionService();
Expand Down
17 changes: 6 additions & 11 deletions apps/code/src/renderer/features/sessions/service/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3560,23 +3560,18 @@ export class SessionService {
return;
}

// The fetched logs lag behind expectedCount and `newEntries` is the latest
// tail slice of the snapshot — appending it here would create duplicates
// and gaps in `session.events` (and bump processedLineCount past entries
// we don't actually have). Skip; the next snapshot/log update will retry
// once the source has caught up.
log.warn("Cloud task log count inconsistency", {
taskRunId,
currentCount,
expectedCount,
fetchedCount: rawEntries.length,
entriesReceived: newEntries.length,
});
Comment on lines 3568 to 3574
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Now that the warning fires specifically when the fetch lagged behind expectedCount, adding fetchedCount: rawEntries.length would make it immediately clear how far behind the S3/local fetch was — useful for diagnosing future incidents without needing to correlate separate log lines.

Suggested change
log.warn("Cloud task log count inconsistency", {
taskRunId,
currentCount,
expectedCount,
entriesReceived: newEntries.length,
});
log.warn("Cloud task log count inconsistency", {
taskRunId,
currentCount,
expectedCount,
fetchedCount: rawEntries.length,
entriesReceived: newEntries.length,
});
Prompt To Fix With AI
This is a comment left during a code review.
Path: apps/code/src/renderer/features/sessions/service/service.ts
Line: 3568-3573

Comment:
Now that the warning fires specifically when the fetch lagged behind `expectedCount`, adding `fetchedCount: rawEntries.length` would make it immediately clear how far behind the S3/local fetch was — useful for diagnosing future incidents without needing to correlate separate log lines.

```suggestion
    log.warn("Cloud task log count inconsistency", {
      taskRunId,
      currentCount,
      expectedCount,
      fetchedCount: rawEntries.length,
      entriesReceived: newEntries.length,
    });
```

How can I resolve this? If you propose a fix, please make it concise.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call — added in 7e676a9.

let newEvents = convertStoredEntriesToEvents(newEntries);
newEvents = this.filterSkippedPromptEvents(taskRunId, session, newEvents);
if (hasSessionPromptEvent(newEvents)) {
sessionStoreSetters.clearTailOptimisticItems(taskRunId);
}
sessionStoreSetters.appendEvents(
taskRunId,
newEvents,
latestCount + newEntries.length,
);
this.updatePromptStateFromEvents(taskRunId, newEvents);
}

private createBaseSession(
Expand Down
Loading