Skip to content

Commit 02d0322

Browse files
committed
feat(agent): add signalComplete() for task run status reporting
1 parent e1ec517 commit 02d0322

File tree

3 files changed

+48
-1
lines changed

3 files changed

+48
-1
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ const mockAuthStore = vi.hoisted(() => ({
6161
client: {
6262
createTaskRun: vi.fn(),
6363
appendTaskRunLog: vi.fn(),
64+
updateTaskRun: vi.fn().mockResolvedValue(undefined),
6465
},
6566
})),
6667
},
@@ -237,6 +238,7 @@ describe("SessionService", () => {
237238
client: {
238239
createTaskRun: createTaskRunMock,
239240
appendTaskRunLog: vi.fn(),
241+
updateTaskRun: vi.fn().mockResolvedValue(undefined),
240242
},
241243
});
242244

@@ -373,6 +375,7 @@ describe("SessionService", () => {
373375
client: {
374376
createTaskRun: createTaskRunMock,
375377
appendTaskRunLog: vi.fn(),
378+
updateTaskRun: vi.fn().mockResolvedValue(undefined),
376379
},
377380
});
378381
mockTrpcAgent.start.mutate.mockResolvedValue({

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,11 @@ export class SessionService {
390390
throw new Error("Failed to create task run. Please try again.");
391391
}
392392

393+
// Signal that the local run is now in progress
394+
auth.client
395+
.updateTaskRun(taskId, taskRun.id, { status: "in_progress" })
396+
.catch((err) => log.warn("Failed to update task run status", { err }));
397+
393398
const result = await trpcVanilla.agent.start.mutate({
394399
taskId,
395400
taskRunId: taskRun.id,
@@ -896,6 +901,16 @@ export class SessionService {
896901
promptStartedAt: Date.now(),
897902
});
898903

904+
// Ensure task run status reflects active work
905+
const auth = useAuthStore.getState();
906+
if (auth.client) {
907+
auth.client
908+
.updateTaskRun(session.taskId, session.taskRunId, {
909+
status: "in_progress",
910+
})
911+
.catch((err) => log.warn("Failed to update task run status", { err }));
912+
}
913+
899914
try {
900915
const result = await trpcVanilla.agent.prompt.mutate({
901916
sessionId: session.taskRunId,

packages/agent/src/agent.ts

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,18 @@ import {
99
} from "./gateway-models.js";
1010
import { PostHogAPIClient } from "./posthog-api.js";
1111
import { SessionLogWriter } from "./session-log-writer.js";
12-
import type { AgentConfig, TaskExecutionOptions } from "./types.js";
12+
import type {
13+
AgentConfig,
14+
TaskExecutionOptions,
15+
TaskRunStatus,
16+
} from "./types.js";
1317
import { Logger } from "./utils/logger.js";
1418

1519
export class Agent {
1620
private posthogAPI?: PostHogAPIClient;
1721
private logger: Logger;
1822
private acpConnection?: InProcessAcpConnection;
23+
private taskId?: string;
1924
private taskRunId?: string;
2025
private sessionLogWriter?: SessionLogWriter;
2126
public debug: boolean;
@@ -84,6 +89,7 @@ export class Agent {
8489
): Promise<InProcessAcpConnection> {
8590
const gatewayConfig = this._configureLlmGateway(options.adapter);
8691

92+
this.taskId = taskId;
8793
this.taskRunId = taskRunId;
8894

8995
let allowedModelIds: Set<string> | undefined;
@@ -175,6 +181,29 @@ export class Agent {
175181
await this.sessionLogWriter?.flushAll();
176182
}
177183

184+
/**
185+
* Signal the final status of the task run to PostHog.
186+
* Mirrors the agent-server's signalTaskComplete() for local runs.
187+
*/
188+
async signalComplete(status: TaskRunStatus = "completed"): Promise<void> {
189+
if (!this.posthogAPI || !this.taskId || !this.taskRunId) {
190+
return;
191+
}
192+
193+
try {
194+
await this.posthogAPI.updateTaskRun(this.taskId, this.taskRunId, {
195+
status,
196+
});
197+
this.logger.info("Task completion signaled", {
198+
taskId: this.taskId,
199+
taskRunId: this.taskRunId,
200+
status,
201+
});
202+
} catch (error) {
203+
this.logger.error("Failed to signal task completion", error);
204+
}
205+
}
206+
178207
async cleanup(): Promise<void> {
179208
if (this.sessionLogWriter && this.taskRunId) {
180209
await this.sessionLogWriter.flush(this.taskRunId);

0 commit comments

Comments
 (0)