Skip to content
Closed
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ const mockAuthStore = vi.hoisted(() => ({
client: {
createTaskRun: vi.fn(),
appendTaskRunLog: vi.fn(),
updateTaskRun: vi.fn().mockResolvedValue(undefined),
},
})),
},
Expand Down Expand Up @@ -238,6 +239,7 @@ describe("SessionService", () => {
client: {
createTaskRun: createTaskRunMock,
appendTaskRunLog: vi.fn(),
updateTaskRun: vi.fn().mockResolvedValue(undefined),
},
});

Expand Down Expand Up @@ -374,6 +376,7 @@ describe("SessionService", () => {
client: {
createTaskRun: createTaskRunMock,
appendTaskRunLog: vi.fn(),
updateTaskRun: vi.fn().mockResolvedValue(undefined),
},
});
mockTrpcAgent.start.mutate.mockResolvedValue({
Expand Down
15 changes: 15 additions & 0 deletions apps/twig/src/renderer/features/sessions/service/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,11 @@ export class SessionService {
throw new Error("Failed to create task run. Please try again.");
}

// Signal that the local run is now in progress
auth.client
.updateTaskRun(taskId, taskRun.id, { status: "in_progress" })
.catch((err) => log.warn("Failed to update task run status", { err }));

const result = await trpcVanilla.agent.start.mutate({
taskId,
taskRunId: taskRun.id,
Expand Down Expand Up @@ -900,6 +905,16 @@ export class SessionService {
promptStartedAt: Date.now(),
});

// Ensure task run status reflects active work
const auth = useAuthStore.getState();
if (auth.client) {
auth.client
.updateTaskRun(session.taskId, session.taskRunId, {
status: "in_progress",
})
.catch((err) => log.warn("Failed to update task run status", { err }));
}

try {
const result = await trpcVanilla.agent.prompt.mutate({
sessionId: session.taskRunId,
Expand Down
27 changes: 26 additions & 1 deletion packages/agent/src/agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,18 @@ import {
} from "./gateway-models.js";
import { PostHogAPIClient } from "./posthog-api.js";
import { SessionLogWriter } from "./session-log-writer.js";
import type { AgentConfig, TaskExecutionOptions } from "./types.js";
import type {
AgentConfig,
TaskExecutionOptions,
TaskRunStatus,
} from "./types.js";
import { Logger } from "./utils/logger.js";

export class Agent {
private posthogAPI?: PostHogAPIClient;
private logger: Logger;
private acpConnection?: InProcessAcpConnection;
private taskId?: string;
private taskRunId?: string;
private sessionLogWriter?: SessionLogWriter;
public debug: boolean;
Expand Down Expand Up @@ -71,6 +76,7 @@ export class Agent {
): Promise<InProcessAcpConnection> {
const gatewayConfig = this._configureLlmGateway(options.adapter);

this.taskId = taskId;
this.taskRunId = taskRunId;

let allowedModelIds: Set<string> | undefined;
Expand Down Expand Up @@ -162,6 +168,25 @@ export class Agent {
await this.sessionLogWriter?.flushAll();
}

async signalComplete(status: TaskRunStatus = "completed"): Promise<void> {
if (!this.posthogAPI || !this.taskId || !this.taskRunId) {
return;
}

try {
await this.posthogAPI.updateTaskRun(this.taskId, this.taskRunId, {
status,
});
this.logger.info("Task completion signaled", {
taskId: this.taskId,
taskRunId: this.taskRunId,
status,
});
} catch (error) {
this.logger.error("Failed to signal task completion", error);
}
}

async cleanup(): Promise<void> {
if (this.sessionLogWriter && this.taskRunId) {
await this.sessionLogWriter.flush(this.taskRunId);
Expand Down
Loading