11name : Klaud Cold wake on run end
2- run-name : Klaud Cold wake on run end - ${{ github.event.workflow_run.display_title }}
2+ run-name : >-
3+ ${{ github.event_name == 'workflow_dispatch'
4+ && format('Klaud Cold wake at {0} - {1}', inputs.not-before, inputs.candidate)
5+ || format('Klaud Cold wake on run end - {0}', github.event.workflow_run.display_title) }}
36
47# A parked Klaud session resumes only after recovery sees its owned runs end. Rather than wait
5- # for the hourly :30 wake, start a recovery-only planner pass as soon as a Klaud-owned
6- # targeted run (e2e Test - klaud-*) or PR sweep (klaud/auto-* branch) completes, unless another
7- # run of that candidate is still active and will wake recovery when it ends. No checkout and no
8- # event data reaches a shell: the event gates the job and the pinned script reads it as data.
8+ # for the hourly :47 wake, start a recovery-only planner pass for the candidate as soon as a
9+ # Klaud-owned targeted run (e2e Test - klaud-*) or PR sweep (klaud/auto-* branch) completes,
10+ # unless its session's agent is still running and sees the end itself, or another of its runs is still
11+ # active and will wake recovery when it ends. Recovery dispatches this workflow for a rerun it
12+ # holds until a set time (a parked infrastructure rerun, or a preempted run's grace period); that
13+ # run sleeps until then and wakes recovery for the candidate. No checkout and no event data
14+ # reaches a shell: the event gates the job and the pinned script reads it as data.
915on : # zizmor: ignore[concurrency-limits]
1016 workflow_run : # zizmor: ignore[dangerous-triggers] runs no code from the triggering run
1117 workflows : [End-to-End Tests, Run Sweep]
1218 types : [completed]
19+ workflow_dispatch :
20+ inputs :
21+ candidate :
22+ description : Candidate whose recovery wakes at not-before
23+ type : string
24+ required : true
25+ not-before :
26+ description : When to wake recovery, an ISO 8601 time at most 4 hours ahead
27+ type : string
28+ required : true
1329
1430permissions : {}
1531
1632jobs :
1733 wake :
1834 name : Wake Klaud recovery
1935 if : >-
20- github.event.workflow_run.head_repository.full_name == github.repository
36+ github.event_name == 'workflow_run'
37+ && github.event.workflow_run.head_repository.full_name == github.repository
2138 && github.event.workflow_run.conclusion != 'skipped'
2239 && (startsWith(github.event.workflow_run.display_title, 'e2e Test - klaud-')
2340 || startsWith(github.event.workflow_run.head_branch, 'klaud/auto-'))
2441 runs-on : ubuntu-latest
2542 permissions :
26- actions : write # Read the candidate's runs and dispatch the planner's recovery-only wake.
43+ actions : write # Read the candidate's runs and jobs and dispatch the planner's recovery-only wake.
2744 steps :
2845 - name : Dispatch a recovery-only planner pass
2946 uses : actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0
@@ -35,20 +52,23 @@ jobs:
3552 new RegExp(`^klaud/auto-(${id})$`).exec(ended.head_branch ?? '')
3653 ?? new RegExp(`^e2e Test - klaud-\\d+-(${id})$`).exec(ended.display_title)
3754 )?.[1];
55+ const inputs = { wake: 'true' };
3856 if (candidate) {
3957 const branch = `klaud/auto-${candidate}`;
4058 const title = new RegExp(`^e2e Test - klaud-\\d+-${candidate}$`);
59+ const statuses = ['requested', 'waiting', 'pending', 'queued', 'in_progress'];
4160 const runs = (workflow, filters) => github.paginate(
4261 github.rest.actions.listWorkflowRuns,
4362 { ...context.repo, workflow_id: workflow, per_page: 100, ...filters },
4463 );
45- const [targeted, sweeps] = await Promise.all([
46- Promise.all(['requested', 'waiting', 'pending', 'queued', 'in_progress'] .map(
64+ const [targeted, sweeps, plans ] = await Promise.all([
65+ Promise.all(statuses .map(
4766 (status) => runs('e2e-tests.yml', {
4867 actor: 'Klaud-Cold', event: 'workflow_dispatch', status,
4968 }),
5069 )),
5170 runs('run-sweep.yml', { branch }),
71+ Promise.all(statuses.map((status) => runs('klaud-plan.yml', { status }))),
5272 ]);
5373 const active = [
5474 ...targeted.flat().filter((run) => title.test(run.display_title)),
5878 core.info(`Run ${active.id} of candidate ${candidate} is still active; its end wakes recovery.`);
5979 return;
6080 }
81+ const jobs = await Promise.all(plans.flat().map((run) => github.paginate(
82+ github.rest.actions.listJobsForWorkflowRun,
83+ { ...context.repo, run_id: run.id, per_page: 100 },
84+ )));
85+ const running = jobs.flat().filter(
86+ (job) => job.name.startsWith(`Run candidate ${candidate}`) && job.status !== 'completed',
87+ );
88+ const agent = running.find((job) => !(job.steps ?? []).some(
89+ (step) => step.name === 'Let Klaud Cold own the candidate PR' && step.status === 'completed',
90+ ));
91+ if (agent) {
92+ core.info(`Session job ${agent.id} of candidate ${candidate} is still running its agent; it sees the run end.`);
93+ return;
94+ }
95+ // Recovery skips a session whose job still runs, so let its cleanup steps finish first (up to 10 minutes).
96+ for (const job of running) {
97+ for (let poll = 0; poll < 40; poll += 1) {
98+ await new Promise((resolve) => setTimeout(resolve, 15000));
99+ const { data } = await github.rest.actions.getJobForWorkflowRun({ ...context.repo, job_id: job.id });
100+ if (data.status === 'completed') break;
101+ }
102+ }
103+ inputs['wake-candidate'] = candidate;
104+ }
105+ await github.rest.actions.createWorkflowDispatch({
106+ ...context.repo, workflow_id: 'klaud-plan.yml', ref: 'main', inputs,
107+ });
108+
109+ due :
110+ name : Wake Klaud recovery when a rerun is due
111+ if : >-
112+ github.event_name == 'workflow_dispatch' && github.ref == 'refs/heads/main'
113+ && github.actor == 'Klaud-Cold'
114+ runs-on : ubuntu-latest
115+ timeout-minutes : 250
116+ # Every pass that holds the same rerun asks for the same wake; keep only the newest.
117+ concurrency :
118+ group : klaud-due-wake-${{ inputs.candidate }}-${{ inputs.not-before }}
119+ cancel-in-progress : true
120+ permissions :
121+ actions : write # Dispatch the planner's recovery-only wake.
122+ steps :
123+ - name : Dispatch a recovery-only planner pass at not-before
124+ uses : actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0
125+ env :
126+ CANDIDATE : ${{ inputs.candidate }}
127+ NOT_BEFORE : ${{ inputs.not-before }}
128+ with :
129+ script : |
130+ const { CANDIDATE: candidate, NOT_BEFORE: notBefore } = process.env;
131+ const wait = Date.parse(notBefore) - Date.now();
132+ if (!/^[0-9a-f]{16}-[0-9a-f]{16}$/.test(candidate) || !(wait <= 4 * 60 * 60 * 1000)) {
133+ core.setFailed(`No wake for candidate ${candidate} at ${notBefore}: invalid or over 4 hours ahead.`);
134+ return;
61135 }
136+ await new Promise((resolve) => setTimeout(resolve, Math.max(wait, 0)));
62137 await github.rest.actions.createWorkflowDispatch({
63- ...context.repo, workflow_id: 'klaud-plan.yml', ref: 'main', inputs: { wake: 'true' },
138+ ...context.repo, workflow_id: 'klaud-plan.yml', ref: 'main',
139+ inputs: { wake: 'true', 'wake-candidate': candidate },
64140 });
0 commit comments