Skip to content

The agent that grows with you

fix(desktop): ignore prototype session map keys

luyifanbf35655955ec
diff --git a/apps/desktop/src/store/compaction.test.ts b/apps/desktop/src/store/compaction.test.ts
index 0f7f11900b8..dd292062197 100644
--- a/apps/desktop/src/store/compaction.test.ts
+++ b/apps/desktop/src/store/compaction.test.ts
@@ -7,6 +7,15 @@ describe('compaction store', () => {
afterEach(() => $compactingSessions.set({}))
+ it('tracks session ids that collide with object prototype keys', () => {
+ expect(sessionCompacting('toString').get()).toBe(false)
+
+ setSessionCompacting('toString', true)
+
+ expect(Object.hasOwn($compactingSessions.get(), 'toString')).toBe(true)
+ expect(sessionCompacting('toString').get()).toBe(true)
+ })
+
it('scopes the view to the session asked for, not whichever is active', () => {
setSessionCompacting('session-a', true)
diff --git a/apps/desktop/src/store/compaction.ts b/apps/desktop/src/store/compaction.ts
index d3100958f65..00fb46b394b 100644
--- a/apps/desktop/src/store/compaction.ts
+++ b/apps/desktop/src/store/compaction.ts
@@ -4,13 +4,14 @@ import { atom, computed } from 'nanostores'
// transcript looks like it reset; per-session so a background chat can't
// clobber the foreground view.
const keyFor = (sessionId: string | null | undefined): string => sessionId ?? ''
+const hasSession = (sessions: Record<string, true>, key: string): boolean => Object.hasOwn(sessions, key)
export const $compactingSessions = atom<Record<string, true>>({})
/** Is `sessionId` compacting? Per-session because a transcript may be a tile,
* and a tile must never wear the primary chat's compaction state. */
export function sessionCompacting(sessionId: null | string) {
- return computed($compactingSessions, sessions => keyFor(sessionId) in sessions)
+ return computed($compactingSessions, sessions => hasSession(sessions, keyFor(sessionId)))
}
export function setSessionCompacting(sessionId: string | null | undefined, active: boolean): void {
@@ -18,7 +19,7 @@ export function setSessionCompacting(sessionId: string | null | undefined, activ
const sessions = $compactingSessions.get()
if (active) {
- if (key in sessions) {
+ if (hasSession(sessions, key)) {
return
}
@@ -27,7 +28,7 @@ export function setSessionCompacting(sessionId: string | null | undefined, activ
return
}
- if (!(key in sessions)) {
+ if (!hasSession(sessions, key)) {
return
}
diff --git a/apps/desktop/src/store/subagents.test.ts b/apps/desktop/src/store/subagents.test.ts
index beb777d151c..5ca542f0923 100644
--- a/apps/desktop/src/store/subagents.test.ts
+++ b/apps/desktop/src/store/subagents.test.ts
@@ -167,6 +167,17 @@ describe('subagent store', () => {
expect($subagentsBySession.get().s2).toHaveLength(1)
})
+ it('creates and clears a session id that collides with an object prototype key', () => {
+ expect(() =>
+ upsertSubagent('toString', { goal: 'proto', status: 'running', subagent_id: 'a1', task_index: 0 })
+ ).not.toThrow()
+ expect($subagentsBySession.get().toString).toHaveLength(1)
+
+ clearSessionSubagents('toString')
+
+ expect(Object.hasOwn($subagentsBySession.get(), 'toString')).toBe(false)
+ })
+
// Regression test for #64015: still-RUNNING background subagents must survive
// the per-turn wipe that previously dropped them at message.start. The fix
// replaces clearSessionSubagents() with pruneFinishedSessionSubagents() at
diff --git a/apps/desktop/src/store/subagents.ts b/apps/desktop/src/store/subagents.ts
index e855cf4726d..b08a70bd03f 100644
--- a/apps/desktop/src/store/subagents.ts
+++ b/apps/desktop/src/store/subagents.ts
@@ -74,6 +74,14 @@ function setSessionSubagents(sid: string, previous: SubagentProgress[], next: Su
$subagentsBySession.set({ ...$subagentsBySession.get(), [sid]: next })
}
+const hasSubagentsForSession = (map: Record<string, SubagentProgress[]>, sid: string): boolean =>
+ Object.hasOwn(map, sid)
+
+const getSubagentsForSession = (
+ map: Record<string, SubagentProgress[]>,
+ sid: string
+): SubagentProgress[] | undefined => (hasSubagentsForSession(map, sid) ? map[sid] : undefined)
+
const isStr = (v: unknown): v is string => typeof v === 'string'
const str = (v: unknown) => (isStr(v) ? v : '')
const num = (v: unknown) => (typeof v === 'number' && Number.isFinite(v) ? v : undefined)
@@ -236,7 +244,7 @@ function toProgress(payload: SubagentPayload, prev: SubagentProgress | undefined
/** Reconcile a scoped, race-checked snapshot without replacing stream history. */
export function reconcileSubagentSnapshot(sid: string, children: SubagentPayload[]) {
const map = $subagentsBySession.get()
- const previous = map[sid] ?? []
+ const previous = getSubagentsForSession(map, sid) ?? []
const ids = new Set(children.map(p => str(p.subagent_id)).filter(Boolean))
const next = previous.filter(item => TERMINAL.has(item.status) || ids.has(item.id))
@@ -279,7 +287,7 @@ export function reconcileSubagentSnapshot(sid: string, children: SubagentPayload
export function clearSessionSubagents(sid: string) {
const map = $subagentsBySession.get()
- if (!(sid in map)) {
+ if (!hasSubagentsForSession(map, sid)) {
return
}
@@ -301,7 +309,7 @@ export function clearSessionSubagents(sid: string) {
*/
export function pruneFinishedSessionSubagents(sid: string) {
const map = $subagentsBySession.get()
- const list = map[sid]
+ const list = getSubagentsForSession(map, sid)
if (!list?.length) {
return
@@ -318,7 +326,7 @@ export function pruneFinishedSessionSubagents(sid: string) {
export function pruneDelegateFallbackSubagents(sid: string) {
const map = $subagentsBySession.get()
- const list = map[sid]
+ const list = getSubagentsForSession(map, sid)
if (!list?.length) {
return
@@ -335,7 +343,7 @@ export function pruneDelegateFallbackSubagents(sid: string) {
export function upsertSubagent(sid: string, payload: SubagentPayload, createIfMissing = true, eventType?: string) {
const map = $subagentsBySession.get()
- const list = map[sid] ?? []
+ const list = getSubagentsForSession(map, sid) ?? []
const id = idOf(payload)
const idx = list.findIndex(item => item.id === id)
diff --git a/apps/desktop/src/store/todos.test.ts b/apps/desktop/src/store/todos.test.ts
index 8980e4a82c8..c427562a064 100644
--- a/apps/desktop/src/store/todos.test.ts
+++ b/apps/desktop/src/store/todos.test.ts
@@ -42,6 +42,14 @@ describe('setSessionTodos finished-list auto-clear', () => {
expect($todosBySession.get().s1).toBeUndefined()
})
+ it('clears finished lists for session ids that collide with object prototype keys', () => {
+ setSessionTodos('toString', [todo('a', 'completed')])
+
+ vi.advanceTimersByTime(5_000)
+
+ expect(Object.hasOwn($todosBySession.get(), 'toString')).toBe(false)
+ })
+
it('cancels the pending clear when a new active list arrives', () => {
setSessionTodos('s1', [todo('a', 'completed')])
vi.advanceTimersByTime(2_000)
@@ -87,6 +95,11 @@ describe('clearActiveSessionTodos (turn-end cleanup)', () => {
expect($todosBySession.get().s1).toBeUndefined()
})
+
+ it('ignores an inherited prototype key on an empty map', () => {
+ expect(() => clearActiveSessionTodos('toString')).not.toThrow()
+ expect(Object.hasOwn($todosBySession.get(), 'toString')).toBe(false)
+ })
})
describe('todosForHydration (stale-active guard on restore)', () => {
diff --git a/apps/desktop/src/store/todos.ts b/apps/desktop/src/store/todos.ts
index 1454bd28d18..b32a8465354 100644
--- a/apps/desktop/src/store/todos.ts
+++ b/apps/desktop/src/store/todos.ts
@@ -70,6 +70,11 @@ export function todosForHydration(todos: readonly TodoItem[] | null): TodoItem[]
const FINISHED_LINGER_MS = 4_000
const clearTimers = keyedTimeouts()
+const hasSessionTodos = (map: Record<string, TodoItem[]>, sid: string): boolean => Object.hasOwn(map, sid)
+
+const getSessionTodos = (map: Record<string, TodoItem[]>, sid: string): TodoItem[] | undefined =>
+ hasSessionTodos(map, sid) ? map[sid] : undefined
+
function acceptRevision(sid: string, revision?: null | number): boolean {
const revisions = $todoRevisionsBySession.get()
const current = revisions[sid]
@@ -113,7 +118,7 @@ function dropSessionTodos(sid: string, forgetRevision: boolean) {
const map = $todosBySession.get()
- if (sid in map) {
+ if (hasSessionTodos(map, sid)) {
const { [sid]: _drop, ...rest } = map
$todosBySession.set(rest)
}
@@ -121,7 +126,7 @@ function dropSessionTodos(sid: string, forgetRevision: boolean) {
if (forgetRevision) {
const revisions = $todoRevisionsBySession.get()
- if (sid in revisions) {
+ if (Object.hasOwn(revisions, sid)) {
const { [sid]: _drop, ...rest } = revisions
$todoRevisionsBySession.set(rest)
}
@@ -138,7 +143,7 @@ export function clearSessionTodos(sid: string) {
// composer forever. A finished list is left untouched so its short linger
// still shows the last checkmark landing.
export function clearActiveSessionTodos(sid: string) {
- const todos = $todosBySession.get()[sid]
+ const todos = getSessionTodos($todosBySession.get(), sid)
if (!todos || !todoListActive(todos)) {
return