File size: 7,455 Bytes
914f53d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import { describe, expect, test } from 'bun:test';
import { readFileSync } from 'node:fs';
import { join } from 'node:path';
import { requiredWorkflowsForPaths } from '../src/ci-gates.js';
import { shouldBlankPrForWorkflowRunHandoff } from '../src/handoff-gate.js';
import { repoRoot } from './helpers.js';

const workflowPath = join(
  repoRoot,
  '.github/workflows/pr-agent-orchestrator.yml',
);
const ciGateWorkflowPath = join(repoRoot, '.github/workflows/ci-gate.yml');

/** Slice from `  jobId:` through the line before the next 2-space job key. */
function jobBlock(workflow: string, jobId: string): string {
  const lines = workflow.split('\n');
  const start = lines.findIndex((l) => l === `  ${jobId}:`);
  if (start < 0) throw new Error(`job ${jobId} not found in workflow`);
  let end = lines.length;
  for (let i = start + 1; i < lines.length; i++) {
    if (/^  [a-z][a-z0-9_-]*:$/.test(lines[i]!)) {
      end = i;
      break;
    }
  }
  return lines.slice(start, end).join('\n');
}

describe('workflow cancelled-Plan guards', () => {
  const workflow = readFileSync(workflowPath, 'utf8');

  for (const jobId of [
    'aggregate',
    'wait-ci',
    'recheck-handoff',
    'finalize',
  ] as const) {
    test(`${jobId} requires needs.plan.result == 'success'`, () => {
      const block = jobBlock(workflow, jobId);
      // Empty should_skip after a cancelled Plan used to evaluate as != 'true'
      // and revive Aggregate/Finalize — plan.result success is the real gate.
      expect(block).toContain("needs.plan.result == 'success'");
    });
  }

  test('resolve-context blanks pr_number for handoff on workflow_run', () => {
    expect(workflow).toContain("context.eventName === 'workflow_run'");
    expect(workflow).toContain("ready-for-human-review");
    expect(workflow).toContain('agent-resume');
    // After resolving the PR, handoff-without-resume blanks pr_number.
    expect(workflow).toMatch(
      /hasHandoff && !hasResume[\s\S]*?core\.setOutput\('pr_number', ''\)/,
    );
  });
});

describe('shouldBlankPrForWorkflowRunHandoff', () => {
  test('blanks workflow_run when ready-for-human-review and no agent-resume', () => {
    expect(
      shouldBlankPrForWorkflowRunHandoff('workflow_run', [
        'ready-for-human-review',
        'agent-needs-human',
      ]),
    ).toBe(true);
  });

  test('does not blank when agent-resume is present', () => {
    expect(
      shouldBlankPrForWorkflowRunHandoff('workflow_run', [
        'ready-for-human-review',
        'agent-resume',
      ]),
    ).toBe(false);
  });

  test('never blanks pull_request events for this reason', () => {
    expect(
      shouldBlankPrForWorkflowRunHandoff('pull_request', [
        'ready-for-human-review',
      ]),
    ).toBe(false);
  });

  test('does not blank workflow_run without handoff label', () => {
    expect(
      shouldBlankPrForWorkflowRunHandoff('workflow_run', ['agent-in-progress']),
    ).toBe(false);
  });
});

describe('CI Gate empty-area handling', () => {
  const workflow = readFileSync(ciGateWorkflowPath, 'utf8');

  test('settles successfully after grace when no gated workflow registers', () => {
    expect(workflow).toContain(
      'else if (elapsedMs >= GRACE_MS || alreadySettled)',
    );
    expect(workflow).toContain(
      'const EMPTY_AREA_DESCRIPTION = "No required area builds for this change"',
    );
    expect(workflow).toContain(
      'latest?.description !== EMPTY_AREA_DESCRIPTION',
    );
    expect(workflow).toContain('description = EMPTY_AREA_DESCRIPTION');
  });
});

describe('Cloud V2 validation gate paths', () => {
  test('requires validation when its workflow definition changes', () => {
    expect(
      requiredWorkflowsForPaths(
        ['.github/workflows/cloud-v2-validation.yml'],
        repoRoot,
      ),
    ).toContain('Cloud V2 Validation');
  });
});

describe('workflow tooling pin', () => {
  const workflow = readFileSync(workflowPath, 'utf8');

  test('tool_ref dispatch input and resolve-context output exist', () => {
    expect(workflow).toContain('tool_ref:');
    expect(workflow).toContain("core.setOutput('tool_ref'");
    expect(workflow).toContain('github.event.inputs.tool_ref');
  });

  for (const jobId of [
    'plan',
    'aggregate',
    'wait-ci',
    'recheck-handoff',
    'finalize',
    'review-bugbot',
  ] as const) {
    test(`${jobId} checks out tool_ref`, () => {
      expect(jobBlock(workflow, jobId)).toContain(
        'needs.resolve-context.outputs.tool_ref',
      );
    });
  }

  for (const jobId of ['review-standards', 'review-depth', 'review-codex'] as const) {
    test(`${jobId} overlays tooling from tool_ref onto PR HEAD`, () => {
      const block = jobBlock(workflow, jobId);
      expect(block).toContain('head_sha');
      expect(block).toContain('Pin orchestrator tooling to base');
      expect(block).toContain('needs.resolve-context.outputs.tool_ref');
    });
  }

  test('fix job uses a sibling tool checkout hidden from git add -A', () => {
    const block = jobBlock(workflow, 'fix');
    expect(block).toContain('.pr-agent-tool');
    expect(block).toContain('PR_AGENT_TOOL_ROOT');
    expect(block).toContain('.git/info/exclude');
    expect(block).toContain('head_ref');
  });
});

describe('continue-loop self-dispatch guards (#3851)', () => {
  const workflow = readFileSync(workflowPath, 'utf8');

  test('workflow can dispatch itself', () => {
    // createWorkflowDispatch needs actions: write; read-only strands the loop.
    expect(workflow).toMatch(/permissions:[\s\S]*?actions: write/);
  });

  test('continue-loop is gated on a real plan and an explicit continuation ask', () => {
    const block = jobBlock(workflow, 'continue-loop');
    expect(block).toContain("needs.plan.result == 'success'");
    expect(block).toContain("needs.aggregate.outputs.needs_continuation == 'true'");
    expect(block).toContain(
      "needs.recheck-handoff.outputs.needs_continuation == 'true'",
    );
    expect(block).toContain('bun run cli continue');
  });

  test('continue-loop can never race a handoff', () => {
    const block = jobBlock(workflow, 'continue-loop');
    expect(block).toContain("needs.plan.outputs.should_handoff != 'true'");
    expect(block).toContain("needs.aggregate.outputs.should_handoff != 'true'");
    expect(block).toContain("needs.recheck-handoff.outputs.should_handoff != 'true'");
    expect(block).toContain("needs.finalize.result == 'skipped'");
  });

  test('continuation signals are exported by both deciding jobs', () => {
    expect(jobBlock(workflow, 'aggregate')).toContain(
      'needs_continuation: ${{ steps.agg.outputs.needs_continuation }}',
    );
    expect(jobBlock(workflow, 'recheck-handoff')).toContain(
      'needs_continuation: ${{ steps.recheck.outputs.needs_continuation }}',
    );
  });

  test('BUGBOT_STARTED is passed through without a false default', () => {
    // A blank value must stay blank: it means "unknown", not "did not start".
    expect(jobBlock(workflow, 'review-bugbot')).toContain(
      'bugbot_started: ${{ steps.poll.outputs.bugbot_started }}',
    );
    expect(jobBlock(workflow, 'aggregate')).toContain(
      'BUGBOT_STARTED: ${{ needs.review-bugbot.outputs.bugbot_started }}',
    );
  });
});