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
2 changes: 2 additions & 0 deletions electron/src/main/agents/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1640,5 +1640,7 @@ function makeEmptyChain(
subagentRecord: null,
startTime: new Date().toISOString(),
endTime: null,
errorDetail: null,
errorTitle: null,
};
}
3 changes: 2 additions & 1 deletion electron/src/main/ipc/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { getForegroundLiveRegistry } from '../tools/process/foreground-live';
import { SEND_INPUT_MAX_TEXT_LENGTH } from '../tools/process/send-input';
import { getSessionManager } from '../session/singleton';
import { IPC_CHANNELS, type ChatSessionSnapshot } from '../../shared/types/ipc';
import { ChainStatus } from '../../shared/types/chain';
import { ChainStatus, lastChainError } from '../../shared/types/chain';
import { flattenSessionMessages } from '../../shared/types/session';
import { clearAllChatHistory } from './chat-history';
import { chatCancelSchema, chatQueueNextSchema, chatSendSchema, chatSnapshotSchema, chatStopSchema } from './payload-schemas';
Expand Down Expand Up @@ -137,6 +137,7 @@ export function registerChatIPC(): void {
sessionId,
messages: liveAgent && live ? [...liveAgent.messages] : flattenSessionMessages(session),
live,
lastChainError: live ? null : lastChainError(session.chains),
};
},
);
Expand Down
4 changes: 4 additions & 0 deletions electron/src/main/ipc/chat/persist.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,8 @@ export function persistTurnConversation(
agent: Agent,
selection?: ModelSelection | null,
webContents?: WebContents,
errorDetail?: string | null,
errorTitle?: string | null,
): void {
setChatHistory(sessionId, fullHistory);
try {
Expand All @@ -203,6 +205,8 @@ export function persistTurnConversation(
agentName: agent.name,
agentType: agent.type,
agentTier: agent.tier,
errorDetail,
errorTitle,
}, sessionId);
const update = updated ? buildSessionUpdatedEvent(updated, null) : null;
if (update && webContents) {
Expand Down
1 change: 1 addition & 0 deletions electron/src/main/ipc/chat/send.ts
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,7 @@ export async function startChatTurn(
persistTurnConversation(
sessionId, fullHistory, turnMessagesFromAgent(activeAgent), ChainStatus.FAILED,
agent, activeAgent.selection, webContents,
detail, title,
);
activeAgent.messages = fullHistory;
sendTurnEvent(webContents, activeAgent, IPC_CHANNELS.CHAT_ERROR, {
Expand Down
2 changes: 2 additions & 0 deletions electron/src/main/ipc/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import { BrowserWindow, dialog, ipcMain } from 'electron';
import { IPC_CHANNELS } from '../../shared/types/ipc';
import { flattenSessionMessages, sessionForRenderer } from '../../shared/types/session';
import { lastChainError } from '../../shared/types/chain';
import type { ModelSelection } from '../../shared/types/provider';
import {
getSessionManager,
Expand Down Expand Up @@ -358,6 +359,7 @@ export function registerSessionIPC(): void {
messages,
live,
workspace,
lastChainError: session && !live ? lastChainError(session.chains) : null,
};
});

Expand Down
15 changes: 14 additions & 1 deletion electron/src/main/session/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -639,6 +639,8 @@ export class SessionManager {
subagentRecord: null,
startTime: now,
endTime: null,
errorDetail: null,
errorTitle: null,
};
chains = [...chains, chain];

Expand Down Expand Up @@ -711,6 +713,8 @@ export class SessionManager {
finishActiveChain(
status: ChainStatus = ChainStatus.COMPLETED,
sessionId?: string,
errorDetail?: string | null,
errorTitle?: string | null,
): Session | null {
const targetId = sessionId ?? this.selectedSessionId();
const session = targetId ? this.ensureSession(targetId) : null;
Expand All @@ -729,6 +733,8 @@ export class SessionManager {
...existing,
status: terminal,
endTime: now,
errorDetail: errorDetail ?? null,
errorTitle: errorTitle ?? null,
};
const chains = session.chains.map((c) =>
c.id === chain.id ? chain : c,
Expand Down Expand Up @@ -769,6 +775,8 @@ export class SessionManager {
agentName?: string;
agentType?: string;
agentTier?: string;
errorDetail?: string | null;
errorTitle?: string | null;
}, sessionId?: string): Session | null {
const targetId = sessionId ?? this.selectedSessionId();
let session = targetId ? this.ensureSession(targetId) : null;
Expand Down Expand Up @@ -827,7 +835,12 @@ export class SessionManager {
if (status === ChainStatus.ACTIVE) {
return session;
}
return this.finishActiveChain(status, targetId ?? undefined);
return this.finishActiveChain(
status,
targetId ?? undefined,
params.errorDetail,
params.errorTitle,
);
}

/**
Expand Down
14 changes: 13 additions & 1 deletion electron/src/main/session/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ CREATE TABLE IF NOT EXISTS chains (
subagent_record_json TEXT,
messages_json TEXT NOT NULL DEFAULT '[]',
start_time TEXT,
end_time TEXT
end_time TEXT,
error_detail TEXT,
error_title TEXT
);

CREATE TABLE IF NOT EXISTS subagent_chains (
Expand Down Expand Up @@ -72,4 +74,14 @@ export function applySessionSchemaMigrations(db: SqliteDatabase): void {
}
}
}

if (tables.has('chains')) {
const chainColumns = db.prepare('PRAGMA table_info(chains)').all() as Array<{ name: string }>;
const existing = new Set(chainColumns.map((c) => c.name));
for (const col of ['error_detail', 'error_title']) {
if (!existing.has(col)) {
db.prepare(`ALTER TABLE chains ADD COLUMN ${col} TEXT`).run();
}
}
}
}
15 changes: 12 additions & 3 deletions electron/src/main/session/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,8 @@ interface ChainRow {
messages_json: string;
start_time: string | null;
end_time: string | null;
error_detail: string | null;
error_title: string | null;
}

interface SubagentChainRow {
Expand Down Expand Up @@ -293,6 +295,8 @@ function chainFromRow(row: ChainRow): Chain {
subagentRecord,
startTime: row.start_time,
endTime: row.end_time,
errorDetail: row.error_detail ?? null,
errorTitle: row.error_title ?? null,
};
}

Expand Down Expand Up @@ -351,8 +355,8 @@ function sessionFromRow(row: SessionRow, chains: Chain[], subagentChains: Subage
}

const INSERT_CHAIN_SQL = `
INSERT INTO chains (id, session_id, ordinal, status, selection_json, model_label, agent_name, agent_type, agent_tier, subagent_record_json, messages_json, start_time, end_time)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
INSERT INTO chains (id, session_id, ordinal, status, selection_json, model_label, agent_name, agent_type, agent_tier, subagent_record_json, messages_json, start_time, end_time, error_detail, error_title)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
`;

const INSERT_SUBAGENT_CHAIN_SQL = `
Expand Down Expand Up @@ -385,6 +389,8 @@ function insertChainRow(
serializeMessages(chain.messages),
chain.startTime,
chain.endTime,
chain.errorDetail,
chain.errorTitle,
);
}

Expand All @@ -395,7 +401,8 @@ function updateChainRow(db: SqliteDatabase, chain: Chain): number {
SET status = ?, selection_json = ?, model_label = ?,
agent_name = ?, agent_type = ?, agent_tier = ?,
subagent_record_json = ?, messages_json = ?,
start_time = ?, end_time = ?
start_time = ?, end_time = ?,
error_detail = ?, error_title = ?
WHERE id = ? AND session_id = ?`,
)
.run(
Expand All @@ -411,6 +418,8 @@ function updateChainRow(db: SqliteDatabase, chain: Chain): number {
serializeMessages(chain.messages),
chain.startTime,
chain.endTime,
chain.errorDetail,
chain.errorTitle,
chain.id,
chain.sessionId,
).changes;
Expand Down
5 changes: 4 additions & 1 deletion electron/src/renderer/components/ChainFooter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ interface ChainFooterProps {
elapsedSeconds?: number;
interrupted?: boolean;
failed?: boolean;
/** Error detail from a FAILED chain, shown as a tooltip on the badge. */
errorDetail?: string | null;
}

export function ChainFooter({
Expand All @@ -22,6 +24,7 @@ export function ChainFooter({
elapsedSeconds,
interrupted,
failed,
errorDetail,
}: ChainFooterProps) {
const showSub = hasUsage(subUsage);
const showUsage = hasUsage(usage);
Expand All @@ -35,7 +38,7 @@ export function ChainFooter({
</StatusBadge>
)}
{failed && !interrupted && (
<StatusBadge tone="error" size="xs" className="gap-1">
<StatusBadge tone="error" size="xs" className="gap-1" title={errorDetail ?? undefined}>
<Icon name="alert" size={12} />
Failed
</StatusBadge>
Expand Down
14 changes: 7 additions & 7 deletions electron/src/renderer/components/ChatStream.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,12 @@ export function ChatStream({
return (
<div className="relative flex min-h-0 min-w-0 flex-1 flex-col overflow-hidden">
<div className="orchid-chat-scroll px-6 py-5" ref={containerRef}>
{/* History + live tail + active footer render as ONE keyed sequence.
History nodes remain referentially stable across live-only frames,
while the small tail/footer path updates independently. Keeping the
final nodes flat lets React retain shared segment/footer keys across
the live→committed swap instead of replaying entrance animation. */}
{streamNodes}
{error && (
<div className="orchid-error-slot">
<ErrorBanner
Expand All @@ -347,13 +353,6 @@ export function ChatStream({
/>
</div>
)}

{/* History + live tail + active footer render as ONE keyed sequence.
History nodes remain referentially stable across live-only frames,
while the small tail/footer path updates independently. Keeping the
final nodes flat lets React retain shared segment/footer keys across
the live→committed swap instead of replaying entrance animation. */}
{streamNodes}
</div>
{isUserScrolledUp ? (
<Button
Expand Down Expand Up @@ -411,6 +410,7 @@ function renderStreamItem(
elapsedSeconds={item.elapsedSeconds}
interrupted={item.interrupted}
failed={item.failed}
errorDetail={item.errorDetail}
/>
);
}
Expand Down
1 change: 1 addition & 0 deletions electron/src/renderer/components/ChatView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,7 @@ export function ChatView({ isVisible = true, bootstrapConfig = null, onNotify, a
sessionId: result.session.id,
messages: result.messages,
live: result.live,
lastChainError: result.lastChainError,
});
},
[session, chat.beginSessionSwitch, chat.hydrateSnapshot, todos.applyFromSession, draftTabVisible, messageQueue.clearQueue],
Expand Down
8 changes: 8 additions & 0 deletions electron/src/renderer/hooks/useChat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -800,6 +800,14 @@ export function useChat(
// sequence affinity for the selected session so stale turn/sequence
// leftovers from a prior generation are discarded (not blindly applied).
replayHydrationBuffer(bufferedEvents);
// Restore error from the last FAILED chain so the banner persists
// across session switches and restarts.
if (snapshot.lastChainError) {
const errorText = snapshot.lastChainError.title && !snapshot.lastChainError.detail.startsWith(snapshot.lastChainError.title)
? `${snapshot.lastChainError.title}: ${snapshot.lastChainError.detail}`
: snapshot.lastChainError.detail;
dispatchProjection({ type: 'local_error', error: errorText, status: 'error' });
}
return;
}

Expand Down
2 changes: 2 additions & 0 deletions electron/src/renderer/hooks/useSession.ts
Original file line number Diff line number Diff line change
Expand Up @@ -661,6 +661,8 @@ function makeLocalSession(): Session {
subagentRecord: null,
startTime: now,
endTime: null,
errorDetail: null,
errorTitle: null,
}],
activeChainId: chainId,
createdAt: now,
Expand Down
2 changes: 2 additions & 0 deletions electron/src/renderer/utils/stream-building.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export type StreamItem =
elapsedSeconds?: number;
interrupted?: boolean;
failed?: boolean;
errorDetail?: string | null;
}
| {
kind: 'collapsed-stub';
Expand Down Expand Up @@ -288,6 +289,7 @@ export function buildHistoryStreamItems(opts: {
chain.status === ChainStatus.INTERRUPTED ||
(isLastChain && interrupted),
failed: chain.status === ChainStatus.FAILED,
errorDetail: chain.errorDetail,
};
if (isActive) {
activeFooter = footer;
Expand Down
4 changes: 4 additions & 0 deletions electron/src/shared/serialization/chain-subagent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ export function chainToStorageDict(chain: Chain): ChainStorageDict {
}
if (chain.startTime) dict.startTime = chain.startTime;
if (chain.endTime != null) dict.endTime = chain.endTime;
if (chain.errorDetail) dict.errorDetail = chain.errorDetail;
if (chain.errorTitle) dict.errorTitle = chain.errorTitle;
return dict;
}

Expand Down Expand Up @@ -92,6 +94,8 @@ export function chainFromStorageDict(data: unknown): Chain {
subagentRecord,
startTime,
endTime,
errorDetail: typeof raw.errorDetail === 'string' ? raw.errorDetail : null,
errorTitle: typeof raw.errorTitle === 'string' ? raw.errorTitle : null,
};
}

Expand Down
19 changes: 19 additions & 0 deletions electron/src/shared/types/chain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ export interface Chain {
* ISO timestamp when the chain finished, or null while ACTIVE / unknown.
*/
readonly endTime: string | null;
/** Error detail persisted when status is FAILED; null otherwise. */
readonly errorDetail: string | null;
/** Short error title (auth/rate-limit/timeout/etc) persisted when FAILED. */
readonly errorTitle: string | null;
}

// ── Storage dict ────────────────────────────────────────────────────────────
Expand All @@ -86,6 +90,8 @@ export interface ChainStorageDict {
subagentRecord?: unknown;
startTime?: string;
endTime?: string | null;
errorDetail?: string | null;
errorTitle?: string | null;
[key: string]: unknown;
}

Expand Down Expand Up @@ -159,3 +165,16 @@ export function parseChainStatus(raw: unknown): ChainStatus {
if (raw === 'failed') return ChainStatus.FAILED;
return ChainStatus.COMPLETED;
}

/** Extract the error detail from the last FAILED chain in a session, if any. */
export function lastChainError(
chains: readonly Chain[],
): { detail: string; title?: string | null } | null {
for (let i = chains.length - 1; i >= 0; i--) {
const chain = chains[i]!;
if (chain.status === ChainStatus.FAILED && chain.errorDetail) {
return { detail: chain.errorDetail, title: chain.errorTitle };
}
}
return null;
}
4 changes: 4 additions & 0 deletions electron/src/shared/types/ipc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,8 @@ export interface ChatSessionSnapshot {
sessionId: string;
messages: Message[];
live: ChatSnapshot | null;
/** Error detail from the last FAILED chain, if any (for hydration restore). */
lastChainError?: { detail: string; title?: string | null } | null;
}

/**
Expand All @@ -196,6 +198,8 @@ export interface SessionOpenResult {
live: ChatSnapshot | null;
/** Resolved workspace after activation (session → sticky → unbound). */
workspace: WorkspaceInfo;
/** Error detail from the last FAILED chain, if any (for hydration restore). */
lastChainError?: { detail: string; title?: string | null } | null;
}

export interface SubagentSnapshotRequest { sessionId: string; }
Expand Down
4 changes: 4 additions & 0 deletions electron/tests/parity/sessions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,8 @@ describe('Session Parity', () => {
subagentRecord: null,
startTime: null,
endTime: null,
errorDetail: null,
errorTitle: null,
},
{
id: 'c2',
Expand All @@ -275,6 +277,8 @@ describe('Session Parity', () => {
subagentRecord: null,
startTime: null,
endTime: null,
errorDetail: null,
errorTitle: null,
},
],
});
Expand Down
Loading
Loading