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 README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ bun run src/index.ts run --all-projects
bun run src/index.ts run --project default --issue ENG-123
bun run src/index.ts run --project default --poll
bun run src/index.ts run --project default --poll --poll-interval-ms 15000 --max-poll-cycles 20
bun run src/index.ts run --all-projects --poll --no-exit-when-idle
bun run src/index.ts status --project default --issue ENG-123
bun run src/index.ts projects
```
Expand Down
4 changes: 4 additions & 0 deletions src/args.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ export function parseArgs(argv: string[]): CliCommand {
const projectId = readFlagValue(args, "--project");
const allProjects = args.includes("--all-projects");
const poll = args.includes("--poll");
const exitWhenIdle = args.includes("--no-exit-when-idle")
? false
: undefined;
const pollIntervalMs = readOptionalPositiveInt(args, "--poll-interval-ms");
const maxPollCycles = readOptionalPositiveInt(args, "--max-poll-cycles");
if (projectId && allProjects) {
Expand All @@ -37,6 +40,7 @@ export function parseArgs(argv: string[]): CliCommand {
projectId,
allProjects,
poll,
exitWhenIdle,
pollIntervalMs,
maxPollCycles,
},
Expand Down
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ function printHelp(): void {
"piv-loop - Codex CLI orchestration workflow",
"",
"Commands:",
" piv-loop run [--project <PROJECT_ID>] [--issue <LINEAR_KEY_OR_URL>] [--poll] [--poll-interval-ms <MS>] [--max-poll-cycles <N>]",
" piv-loop run --all-projects [--issue <LINEAR_KEY_OR_URL>]",
" piv-loop run [--project <PROJECT_ID>] [--issue <LINEAR_KEY_OR_URL>] [--poll] [--no-exit-when-idle] [--poll-interval-ms <MS>] [--max-poll-cycles <N>]",
" piv-loop run --all-projects [--issue <LINEAR_KEY_OR_URL>] [--poll] [--no-exit-when-idle]",
" piv-loop status --project <PROJECT_ID> --issue <LINEAR_KEY>",
" piv-loop projects",
" piv-loop help",
Expand Down
114 changes: 76 additions & 38 deletions src/workflow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,33 @@ export async function runWorkflow(
return;
}

for (const project of projects) {
await runProjectWorkflow(project, options);
const projectContexts = projects.map((project) => ({
config: project,
linear: new LinearClient(project),
polling: resolvePollingSettings(project, options),
}));
const globalPolling = resolveGlobalPollingSettings(projectContexts);
let cycle = 0;

while (true) {
cycle += 1;
let totalIssues = 0;

for (const context of projectContexts) {
totalIssues += await runProjectCycle(
context.config,
options,
context.linear,
cycle,
context.polling,
);
}

if (shouldStopPolling(globalPolling, options, cycle, totalIssues)) {
return;
}

await sleep(globalPolling.intervalMs);
}
}

Expand All @@ -57,48 +82,61 @@ function pickProjects(
return config.projects.slice(0, 1);
}

async function runProjectWorkflow(
function resolveGlobalPollingSettings(
projects: Array<{ config: ResolvedProjectConfig; polling: PollingSettings }>,
): PollingSettings {
const first = projects[0];
if (!first) {
return {
enabled: false,
intervalMs: 30000,
exitWhenIdle: true,
};
}
return first.polling;
}

export function shouldStopPolling(
polling: PollingSettings,
options: RunOptions,
cycle: number,
totalIssues: number,
): boolean {
if (!polling.enabled || options.issueArg) {
return true;
}
if (polling.maxCycles !== undefined && cycle >= polling.maxCycles) {
return true;
}
if (totalIssues === 0 && polling.exitWhenIdle) {
return true;
}
return false;
}

async function runProjectCycle(
config: ResolvedProjectConfig,
options: RunOptions,
): Promise<void> {
const linear = new LinearClient(config);
linear: LinearClient,
cycle: number,
polling: PollingSettings,
): Promise<number> {
const projectLogger = logger.child({ projectId: config.id });
const polling = resolvePollingSettings(config, options);
let cycle = 0;

while (true) {
cycle += 1;
const issues = await linear.fetchWork(options.issueArg);
projectLogger.info(
{ cycle, issueCount: issues.length, pollingEnabled: polling.enabled },
"Fetched eligible Linear issues",
);

if (issues.length === 0) {
projectLogger.info({ cycle }, "No eligible Linear issues found.");
}

for (const issue of issues) {
await processIssue(config, linear, issue);
}
const issues = await linear.fetchWork(options.issueArg);
projectLogger.info(
{ cycle, issueCount: issues.length, pollingEnabled: polling.enabled },
"Fetched eligible Linear issues",
);

if (!polling.enabled || options.issueArg) {
return;
}
if (polling.maxCycles !== undefined && cycle >= polling.maxCycles) {
projectLogger.info(
{ cycle, maxCycles: polling.maxCycles },
"Polling stopped after reaching configured max cycles",
);
return;
}
if (issues.length === 0 && polling.exitWhenIdle) {
projectLogger.info({ cycle }, "Polling exited after idle cycle.");
return;
}
if (issues.length === 0) {
projectLogger.info({ cycle }, "No eligible Linear issues found.");
}

await sleep(polling.intervalMs);
for (const issue of issues) {
await processIssue(config, linear, issue);
}

return issues.length;
}

async function processIssue(
Expand Down
25 changes: 25 additions & 0 deletions tests/args.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ describe("parseArgs", () => {
projectId: undefined,
allProjects: false,
poll: false,
exitWhenIdle: undefined,
pollIntervalMs: undefined,
maxPollCycles: undefined,
},
Expand All @@ -25,6 +26,7 @@ describe("parseArgs", () => {
projectId: "api",
allProjects: false,
poll: false,
exitWhenIdle: undefined,
pollIntervalMs: undefined,
maxPollCycles: undefined,
},
Expand All @@ -49,12 +51,35 @@ describe("parseArgs", () => {
projectId: undefined,
allProjects: false,
poll: true,
exitWhenIdle: undefined,
pollIntervalMs: 15000,
maxPollCycles: 20,
},
});
});

it("parses no-exit-when-idle flag", () => {
const parsed = parseArgs([
"bun",
"piv-loop",
"run",
"--poll",
"--no-exit-when-idle",
]);
expect(parsed).toEqual({
kind: "run",
options: {
issueArg: undefined,
projectId: undefined,
allProjects: false,
poll: true,
exitWhenIdle: false,
pollIntervalMs: undefined,
maxPollCycles: undefined,
},
});
});

it("rejects invalid poll-interval-ms", () => {
expect(() =>
parseArgs(["bun", "piv-loop", "run", "--poll-interval-ms", "0"]),
Expand Down
53 changes: 53 additions & 0 deletions tests/workflow.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
buildPlanComment,
parseReviewOutcome,
resolvePollingSettings,
shouldStopPolling,
} from "../src/workflow";

describe("parseReviewOutcome", () => {
Expand Down Expand Up @@ -122,6 +123,58 @@ describe("resolvePollingSettings", () => {
});
});

describe("shouldStopPolling", () => {
it("stops immediately when polling is disabled", () => {
const stop = shouldStopPolling(
{ enabled: false, intervalMs: 30000, exitWhenIdle: true },
{},
1,
2,
);
expect(stop).toBe(true);
});

it("stops immediately when issue is explicitly targeted", () => {
const stop = shouldStopPolling(
{ enabled: true, intervalMs: 30000, exitWhenIdle: false },
{ poll: true, issueArg: "ENG-1" },
1,
1,
);
expect(stop).toBe(true);
});

it("stops after max polling cycles", () => {
const stop = shouldStopPolling(
{ enabled: true, intervalMs: 30000, maxCycles: 2, exitWhenIdle: false },
{ poll: true },
2,
3,
);
expect(stop).toBe(true);
});

it("stops on global idle cycle only when enabled", () => {
const stop = shouldStopPolling(
{ enabled: true, intervalMs: 30000, exitWhenIdle: true },
{ poll: true },
1,
0,
);
expect(stop).toBe(true);
});

it("continues when any project has work in the cycle", () => {
const stop = shouldStopPolling(
{ enabled: true, intervalMs: 30000, exitWhenIdle: true },
{ poll: true },
1,
1,
);
expect(stop).toBe(false);
});
});

describe("buildIssueJobLogFields", () => {
it("returns consistent issue job fields", () => {
const now = new Date().toISOString();
Expand Down
Loading