Skip to content

Commit e197c79

Browse files
committed
feat: add session depth warning and compaction event tracking (P4k-b/c)
- Track assistant message count, warn at ~250 messages about potential compaction issues with Claude models - Handle session.compacted SSE events, notify user when compaction occurs - Reset counters on session switch and new session creation - Mitigates upstream opencode compaction bug (anomalyco/opencode#14367)
1 parent ab18f75 commit e197c79

1 file changed

Lines changed: 38 additions & 0 deletions

File tree

‎src/connector/bridges/OpenCodeBridge.ts‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ export class OpenCodeBridge implements AgentBridge {
5656
private pendingCorrelation: string | undefined;
5757
private stopped = false;
5858
private sessionBusy = false;
59+
private messageCount = 0;
60+
private depthWarned = false;
5961
private onQuestion: QuestionCallback | undefined;
6062
private onPermission: PermissionCallback | undefined;
6163
private readonly subAgentTracker = new SubAgentTracker();
@@ -326,6 +328,8 @@ export class OpenCodeBridge implements AgentBridge {
326328
this.sessionId = sessionId;
327329
this.lastCompletedAssistantId = undefined;
328330
this.sessionBusy = false;
331+
this.messageCount = 0;
332+
this.depthWarned = false;
329333
}
330334
console.log(`[OpenCodeBridge] new session created and bound, session=${this.sessionId}`);
331335
} catch (err) {
@@ -379,6 +383,8 @@ export class OpenCodeBridge implements AgentBridge {
379383
this.sessionId = resolvedId;
380384
this.lastCompletedAssistantId = undefined;
381385
this.sessionBusy = false;
386+
this.messageCount = 0;
387+
this.depthWarned = false;
382388
console.log(`[OpenCodeBridge] switched to session=${resolvedId}`);
383389
}
384390

@@ -480,6 +486,8 @@ export class OpenCodeBridge implements AgentBridge {
480486
this.handleQuestionAsked(event.properties);
481487
} else if (event.type === 'permission.asked') {
482488
this.handlePermissionAsked(event.properties);
489+
} else if (event.type === 'session.compacted') {
490+
this.handleSessionCompacted(event.properties);
483491
}
484492
}
485493

@@ -501,6 +509,12 @@ export class OpenCodeBridge implements AgentBridge {
501509

502510
if (info.role === 'assistant' && info.id && info.time?.completed) {
503511
this.lastCompletedAssistantId = info.id;
512+
this.messageCount++;
513+
if (this.messageCount >= 250 && !this.depthWarned) {
514+
this.depthWarned = true;
515+
console.log(`[OpenCodeBridge] session depth warning: ${this.messageCount} messages`);
516+
this.emitDepthWarning();
517+
}
504518
}
505519

506520
if (info.role === 'assistant' && info.error) {
@@ -663,6 +677,30 @@ export class OpenCodeBridge implements AgentBridge {
663677
}
664678
}
665679

680+
private handleSessionCompacted(props: Record<string, unknown> | undefined): void {
681+
const sessionID = (props?.['sessionID'] ?? props?.['id']) as string | undefined;
682+
if (sessionID && sessionID !== this.sessionId) return;
683+
console.log(`[OpenCodeBridge] session.compacted received for session=${sessionID ?? this.sessionId}`);
684+
for (const entry of this.pending.values()) {
685+
if (!entry.settled) {
686+
entry.onOutput(entry.taskId, 'stdout',
687+
'\n⚠️ Session history was compacted by opencode. ' +
688+
'If errors occur, consider switching to a different model or starting a new session (`/pf new`).\n');
689+
}
690+
}
691+
}
692+
693+
private emitDepthWarning(): void {
694+
for (const entry of this.pending.values()) {
695+
if (!entry.settled) {
696+
entry.onOutput(entry.taskId, 'stdout',
697+
'\n⚠️ This session has reached ~250 messages. ' +
698+
'Long sessions may trigger compaction issues with Claude models. ' +
699+
'Consider starting a new session (`/pf new`) soon.\n');
700+
}
701+
}
702+
}
703+
666704
private extractErrorMessage(error: unknown): string {
667705
if (!error) return 'Unknown error';
668706
if (typeof error === 'string') return error;

0 commit comments

Comments
 (0)