Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
153 changes: 153 additions & 0 deletions packages/cli/src/serve/live/live-task-service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -757,6 +757,60 @@ describe('LiveTaskService', () => {
).toEqual(['task-1', 'task-2']);
});

it('polls a mixed-case task through its canonical bridge entry', async () => {
const harness = makeHarness();
const sessionId = '550e8400-e29b-41d4-a716-446655440000';
const persistedSessionId = sessionId.toUpperCase();
const liveSummary: BridgeSessionSummary = {
sessionId,
workspaceCwd: '/conversations',
createdAt: '2026-07-30T00:00:00.000Z',
displayName: 'Active task',
clientCount: 1,
hasActivePrompt: true,
};
harness.summaries.set(sessionId, liveSummary);
harness.resident.add(sessionId);
persistedSessions.set(persistedSessionId, persisted(persistedSessionId));
persistedSessionOwners.set(persistedSessionId, '/conversations');
listWorkspaceSessionsForResponse.mockResolvedValue({
sessions: [{ ...liveSummary, sessionId: persistedSessionId }],
});
const subscribeEvents = vi.spyOn(harness.bridge, 'subscribeEvents');

const result = await harness.service.handle({
callerSessionId: 'live-root',
name: 'wait_threads',
arguments: {
targets: [{ threadId: persistedSessionId }],
timeoutMs: 10,
},
});

expect(result).toMatchObject({
timedOut: true,
polls: [{ thread: { id: persistedSessionId } }],
});
expect(harness.bridge.getSessionEventEpoch).toHaveBeenCalledWith(sessionId);
expect(harness.bridge.getSessionLastEventId).toHaveBeenCalledWith(
sessionId,
);
expect(subscribeEvents).toHaveBeenCalledWith(
sessionId,
expect.objectContaining({ lastEventId: 7 }),
);
expect(harness.bridge.getSessionEventEpoch).not.toHaveBeenCalledWith(
persistedSessionId,
);
expect(harness.bridge.getSessionLastEventId).not.toHaveBeenCalledWith(
persistedSessionId,
);
expect(subscribeEvents).not.toHaveBeenCalledWith(
persistedSessionId,
expect.any(Object),
);
});

it('suppresses previously delivered text and markers for an unchanged cursor', async () => {
const harness = makeHarness();
const summary: BridgeSessionSummary = {
Expand Down Expand Up @@ -903,6 +957,105 @@ describe('LiveTaskService', () => {
expect(harness.sendPrompt).toHaveBeenCalledOnce();
});

it('reuses the canonical bridge entry for a mixed-case persisted task', async () => {
const harness = makeHarness();
const sessionId = '550e8400-e29b-41d4-a716-446655440000';
const persistedSessionId = sessionId.toUpperCase();
const liveSummary: BridgeSessionSummary = {
sessionId,
workspaceCwd: '/conversations',
createdAt: '2026-07-30T00:00:00.000Z',
displayName: 'Existing task',
clientCount: 0,
hasActivePrompt: false,
};
const persistedSummary = {
...liveSummary,
sessionId: persistedSessionId,
};
harness.summaries.set(sessionId, liveSummary);
harness.resident.add(sessionId);
persistedSessions.set(persistedSessionId, persisted(persistedSessionId));
persistedSessionOwners.set(persistedSessionId, '/conversations');
listWorkspaceSessionsForResponse.mockResolvedValue({
sessions: [persistedSummary],
});

const result = await harness.service.handle({
callerSessionId: 'live-root',
name: 'send_message_to_thread',
arguments: {
threadId: persistedSessionId,
prompt: 'continue this task',
},
});

expect(result).toEqual({ threadId: persistedSessionId });
expect(harness.bridge.resumeSession).not.toHaveBeenCalled();
expect(harness.bridge.changeSessionCwd).not.toHaveBeenCalled();
expect(harness.sendPrompt).toHaveBeenCalledWith(
sessionId,
expect.objectContaining({ sessionId }),
undefined,
expect.any(Object),
);
expect(harness.resident).not.toContain(persistedSessionId);
});

it('uses one canonical bridge id while resuming a mixed-case persisted task', async () => {
const harness = makeHarness();
const sessionId = '550e8400-e29b-41d4-a716-446655440000';
const persistedSessionId = sessionId.toUpperCase();
const sourceId = `${LIVE_SESSION_SOURCE_PREFIX}mixed-case`;
const summary: BridgeSessionSummary = {
sessionId: persistedSessionId,
workspaceCwd: '/conversations',
createdAt: '2026-07-30T00:00:00.000Z',
displayName: 'Persisted task',
clientCount: 0,
hasActivePrompt: false,
};
harness.summaries.set(persistedSessionId, summary);
persistedSessions.set(persistedSessionId, persisted(persistedSessionId));
persistedSessionOwners.set(persistedSessionId, '/conversations');
sessionSources.set(persistedSessionId, {
sourceType: 'default',
sourceId,
});
listWorkspaceSessionsForResponse.mockResolvedValue({ sessions: [summary] });

await harness.service.handle({
callerSessionId: 'live-root',
name: 'send_message_to_thread',
arguments: {
threadId: persistedSessionId,
prompt: 'continue this task',
},
});

expect(harness.bridge.resumeSession).toHaveBeenCalledWith({
sessionId,
workspaceCwd: '/conversations',
sourceType: 'default',
sourceId,
});
expect(harness.materializeConversationDirectory).toHaveBeenCalledWith(
sessionId,
);
expect(harness.bridge.changeSessionCwd).toHaveBeenCalledWith(sessionId, {
path: `/conversations/${sessionId}`,
allowedRoots: ['/conversations'],
managedRelocation: 'live-conversation',
});
expect(harness.sendPrompt).toHaveBeenCalledWith(
sessionId,
expect.objectContaining({ sessionId }),
undefined,
expect.any(Object),
);
expect(harness.resident).not.toContain(persistedSessionId);
});

it('restores Live source identity before following a cold Live task', async () => {
const harness = makeHarness();
const sourceId = `${LIVE_SESSION_SOURCE_PREFIX}call-2`;
Expand Down
39 changes: 24 additions & 15 deletions packages/cli/src/serve/live/live-task-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import {
readLoadableLiveConversationMetadata,
} from '../../runtime/live-session-source.js';
import { conversationRuntimeUnavailableError } from '../conversations/conversation-runtime-errors.js';
import { normalizeSessionIdForLookup } from '../../config/session-id.js';

const DEFAULT_LIST_LIMIT = 20;
const DEFAULT_READ_TURN_LIMIT = 3;
Expand All @@ -50,6 +51,7 @@ const DEFAULT_ITEM_TEXT_CHARS = 4_000;

interface LocatedTask {
runtime: WorkspaceRuntime;
bridgeSessionId: string;
Comment thread
doudouOUC marked this conversation as resolved.
Resolved
persisted: Awaited<ReturnType<SessionService['loadSession']>>;
summary: BridgeSessionSummary;
}
Expand Down Expand Up @@ -801,11 +803,11 @@ export class LiveTaskService {
const { cursor } = decodeCursor(target.afterCursor, target.threadId);
const lastEventId =
cursor.eventEpoch ===
task.runtime.bridge.getSessionEventEpoch(target.threadId)
task.runtime.bridge.getSessionEventEpoch(task.bridgeSessionId)
? cursor.eventId
: task.runtime.bridge.getSessionLastEventId(target.threadId);
: task.runtime.bridge.getSessionLastEventId(task.bridgeSessionId);
Comment thread
doudouOUC marked this conversation as resolved.
Resolved
for await (const event of task.runtime.bridge.subscribeEvents(
target.threadId,
task.bridgeSessionId,
{ lastEventId, signal },
)) {
const reason = eventWakeReason(event);
Expand All @@ -831,9 +833,11 @@ export class LiveTaskService {
...(task.summary.clientCount > 0
? {
eventEpoch: task.runtime.bridge.getSessionEventEpoch(
target.threadId,
task.bridgeSessionId,
),
eventId: task.runtime.bridge.getSessionLastEventId(
task.bridgeSessionId,
),
eventId: task.runtime.bridge.getSessionLastEventId(target.threadId),
}
: {}),
updatedAt: laterActivityTimestamp(
Expand Down Expand Up @@ -861,7 +865,7 @@ export class LiveTaskService {
const failed = task.summary.hasTurnError === true;
const revision =
task.summary.clientCount > 0
? task.runtime.bridge.getSessionLastEventId(target.threadId)
? task.runtime.bridge.getSessionLastEventId(task.bridgeSessionId)
: epochSeconds(
laterActivityTimestamp(
task.summary.updatedAt,
Expand Down Expand Up @@ -944,7 +948,11 @@ export class LiveTaskService {
localHost(args['hostId']);
const located = await this.locateTask(threadId);
await this.ensureResident(located);
await this.dispatchPrompt(located.runtime.bridge, threadId, prompt);
await this.dispatchPrompt(
located.runtime.bridge,
located.bridgeSessionId,
prompt,
);
return { threadId };
}

Expand Down Expand Up @@ -1053,7 +1061,7 @@ export class LiveTaskService {

private async ensureResident(task: LocatedTask): Promise<void> {
try {
task.runtime.bridge.getSessionSummary(task.summary.sessionId);
task.runtime.bridge.getSessionSummary(task.bridgeSessionId);
return;
} catch (error) {
if (!(error instanceof SessionNotFoundError)) throw error;
Expand All @@ -1067,19 +1075,19 @@ export class LiveTaskService {
)
: await service.readCreationMetadata(task.summary.sessionId);
if (metadata === undefined) {
throw new SessionNotFoundError(task.summary.sessionId);
throw new SessionNotFoundError(task.bridgeSessionId);
}
Comment thread
doudouOUC marked this conversation as resolved.
Resolved
await task.runtime.bridge.resumeSession({
sessionId: task.summary.sessionId,
sessionId: task.bridgeSessionId,
workspaceCwd: task.runtime.workspaceCwd,
...metadata,
});
if (task.runtime.provenance === 'live-conversation') {
const directory = await this.options.materializeConversationDirectory(
task.summary.sessionId,
task.bridgeSessionId,
);
const changed = await task.runtime.bridge.changeSessionCwd(
task.summary.sessionId,
task.bridgeSessionId,
{
path: directory,
allowedRoots: [task.runtime.workspaceCwd],
Expand Down Expand Up @@ -1132,8 +1140,9 @@ export class LiveTaskService {
}

private async locateTask(threadId: string): Promise<LocatedTask> {
const bridgeSessionId = normalizeSessionIdForLookup(threadId);
Comment thread
doudouOUC marked this conversation as resolved.
Resolved
Comment thread
doudouOUC marked this conversation as resolved.
Resolved
const live =
this.options.workspaceRegistry.resolveLiveSessionOwner(threadId);
this.options.workspaceRegistry.resolveLiveSessionOwner(bridgeSessionId);
Comment thread
doudouOUC marked this conversation as resolved.
Resolved
Comment thread
doudouOUC marked this conversation as resolved.
Resolved
if (live.kind === 'ambiguous') {
throw new Error(`Task id is ambiguous: ${threadId}`);
}
Expand Down Expand Up @@ -1167,7 +1176,7 @@ export class LiveTaskService {
const persisted = await service.loadSession(threadId);
let summary: BridgeSessionSummary;
try {
summary = runtime.bridge.getSessionSummary(threadId);
summary = runtime.bridge.getSessionSummary(bridgeSessionId);
Comment thread
doudouOUC marked this conversation as resolved.
Resolved
} catch (error) {
if (
!(error instanceof SessionNotFoundError) &&
Expand All @@ -1193,7 +1202,7 @@ export class LiveTaskService {
if (!found) throw new SessionNotFoundError(threadId);
summary = found;
}
return { runtime, persisted, summary };
return { runtime, bridgeSessionId, persisted, summary };
}
}

Expand Down
Loading