Skip to content

Commit 91c3262

Browse files
committed
Avoid creating duplicate task runs when latest_run already exists
1 parent 3490518 commit 91c3262

File tree

1 file changed

+14
-8
lines changed
  • apps/twig/src/renderer/features/sessions/service

1 file changed

+14
-8
lines changed

apps/twig/src/renderer/features/sessions/service/service.ts

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,7 @@ export class SessionService {
214214
executionMode,
215215
adapter,
216216
model,
217+
latestRun?.id,
217218
);
218219
}
219220
} catch (error) {
@@ -372,19 +373,24 @@ export class SessionService {
372373
executionMode?: ExecutionMode,
373374
adapter?: "claude" | "codex",
374375
model?: string,
376+
existingTaskRunId?: string,
375377
): Promise<void> {
376378
if (!auth.client) {
377379
throw new Error("Unable to reach server. Please check your connection.");
378380
}
379381

380-
const taskRun = await auth.client.createTaskRun(taskId);
381-
if (!taskRun?.id) {
382-
throw new Error("Failed to create task run. Please try again.");
382+
let taskRunId = existingTaskRunId;
383+
if (!taskRunId) {
384+
const taskRun = await auth.client.createTaskRun(taskId);
385+
if (!taskRun?.id) {
386+
throw new Error("Failed to create task run. Please try again.");
387+
}
388+
taskRunId = taskRun.id;
383389
}
384390

385391
const result = await trpcVanilla.agent.start.mutate({
386392
taskId,
387-
taskRunId: taskRun.id,
393+
taskRunId,
388394
repoPath,
389395
apiKey: auth.apiKey,
390396
apiHost: auth.apiHost,
@@ -393,7 +399,7 @@ export class SessionService {
393399
adapter,
394400
});
395401

396-
const session = this.createBaseSession(taskRun.id, taskId, taskTitle);
402+
const session = this.createBaseSession(taskRunId, taskId, taskTitle);
397403
session.channel = result.channel;
398404
session.status = "connected";
399405
session.adapter = adapter;
@@ -404,16 +410,16 @@ export class SessionService {
404410

405411
// Persist the config options
406412
if (configOptions) {
407-
setPersistedConfigOptions(taskRun.id, configOptions);
413+
setPersistedConfigOptions(taskRunId, configOptions);
408414
}
409415

410416
// Persist the adapter
411417
if (adapter) {
412-
useSessionAdapterStore.getState().setAdapter(taskRun.id, adapter);
418+
useSessionAdapterStore.getState().setAdapter(taskRunId, adapter);
413419
}
414420

415421
sessionStoreSetters.setSession(session);
416-
this.subscribeToChannel(taskRun.id);
422+
this.subscribeToChannel(taskRunId);
417423

418424
track(ANALYTICS_EVENTS.TASK_RUN_STARTED, {
419425
task_id: taskId,

0 commit comments

Comments
 (0)