Skip to content
Merged
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
1 change: 1 addition & 0 deletions apps/desktop/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@
"@xterm/headless": "6.1.0-beta.195",
"@xterm/xterm": "6.1.0-beta.195",
"ai": "^6.0.0",
"ansi_up": "^6.0.6",
"better-auth": "1.4.18",
"better-sqlite3": "12.6.2",
"bindings": "^1.5.0",
Expand Down
19 changes: 19 additions & 0 deletions apps/desktop/src/lib/trpc/routers/ui-state/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ const paneSchema = z.object({
"devtools",
"git-graph",
"database-explorer",
"action-logs",
]),
name: z.string(),
isNew: z.boolean().optional(),
Expand Down Expand Up @@ -99,6 +100,24 @@ const paneSchema = z.object({
connectionId: z.string().nullable(),
})
.optional(),
actionLogs: z
.object({
jobs: z.array(
z.object({
detailsUrl: z.string(),
name: z.string(),
status: z.enum([
"success",
"failure",
"pending",
"skipped",
"cancelled",
]),
}),
),
initialJobIndex: z.number().optional(),
})
.optional(),
workspaceRun: z
.object({
workspaceId: z.string(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {
fetchCheckJobSteps,
fetchGitHubPRComments,
fetchGitHubPRStatus,
fetchStructuredJobLogs,
getRepoContext,
type PullRequestCommentsTarget,
} from "../utils/github";
Expand Down Expand Up @@ -1570,5 +1571,34 @@ export const createGitStatusProcedures = () => {

return fetchCheckJobSteps(repoPath, input.detailsUrl);
}),

getJobLogs: publicProcedure
.input(
z.object({
workspaceId: z.string(),
detailsUrl: z.string(),
}),
)
.query(async ({ input }) => {
const workspace = getWorkspace(input.workspaceId);
if (!workspace) {
return [];
}

const worktree = workspace.worktreeId
? getWorktree(workspace.worktreeId)
: null;

let repoPath: string | null = worktree?.path ?? null;
if (!repoPath && workspace.type === "branch") {
const project = getProject(workspace.projectId);
repoPath = project?.mainRepoPath ?? null;
}
if (!repoPath) {
return [];
}

return fetchStructuredJobLogs(repoPath, input.detailsUrl);
}),
});
};
103 changes: 103 additions & 0 deletions apps/desktop/src/lib/trpc/routers/workspaces/utils/github/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -491,3 +491,106 @@ export async function fetchCheckJobSteps(
return [];
}
}

export interface StructuredJobStep {
name: string;
number: number;
status: "queued" | "in_progress" | "completed";
conclusion: string | null;
durationSeconds: number | null;
logs: string;
}

/**
* Fetches job step metadata and logs, returning structured per-step data.
*/
export async function fetchStructuredJobLogs(
worktreePath: string,
detailsUrl: string,
): Promise<StructuredJobStep[]> {
const jobId = parseJobIdFromUrl(detailsUrl);
const nwo = parseNwoFromActionsUrl(detailsUrl);
if (!jobId || !nwo) {
return [];
}

try {
const [jobResult, logsResult] = await Promise.all([
execWithShellEnv("gh", ["api", `repos/${nwo}/actions/jobs/${jobId}`], {
cwd: worktreePath,
}),
execWithShellEnv(
"gh",
["api", `repos/${nwo}/actions/jobs/${jobId}/logs`],
{ cwd: worktreePath, maxBuffer: 10 * 1024 * 1024 },
),
]);

const raw: unknown = JSON.parse(jobResult.stdout.trim());
const result = GHJobResponseSchema.safeParse(raw);
if (!result.success || !result.data.steps) {
return [];
}

const steps = result.data.steps;
const rawLogs = logsResult.stdout;

// Parse raw logs into per-step sections.
// GitHub log format: each line starts with a timestamp like "2024-01-01T00:00:00.0000000Z "
// Steps are separated by ##[group] / ##[endgroup] markers, but these aren't always reliable.
// Instead, match by step started_at/completed_at time ranges.
const logLines = rawLogs.split("\n");
const stepLogs: Map<number, string[]> = new Map();

// Build time ranges for each step
const stepRanges = steps.map((step) => ({
number: step.number,
start: step.started_at ? new Date(step.started_at).getTime() : 0,
end: step.completed_at
? new Date(step.completed_at).getTime()
: Number.POSITIVE_INFINITY,
}));

for (const line of logLines) {
const tsMatch = line.match(
/^(\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d+Z)\s/,
);
if (!tsMatch) continue;
const lineTime = new Date(tsMatch[1]).getTime();
const lineContent = line.slice(tsMatch[0].length);

// Find which step this line belongs to
for (const range of stepRanges) {
if (lineTime >= range.start && lineTime <= range.end + 1000) {
if (!stepLogs.has(range.number)) {
stepLogs.set(range.number, []);
}
stepLogs.get(range.number)?.push(lineContent);
break;
}
}
}

return steps.map((step) => {
let durationSeconds: number | null = null;
if (step.started_at && step.completed_at) {
durationSeconds = Math.round(
(new Date(step.completed_at).getTime() -
new Date(step.started_at).getTime()) /
1000,
);
}
return {
name: step.name,
number: step.number,
status: step.status,
conclusion: step.conclusion ?? null,
durationSeconds,
logs: stepLogs.get(step.number)?.join("\n") ?? "",
};
});
} catch (err) {
console.error("[fetchStructuredJobLogs] Failed:", err);
return [];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export {
fetchCheckJobSteps,
fetchGitHubPRComments,
fetchGitHubPRStatus,
fetchStructuredJobLogs,
resolveReviewThread,
} from "./github";
export { getPRForBranch } from "./pr-resolution";
Expand Down
Loading
Loading