Repository navigation
441 lines (385 loc) · 17.2 KB
/
Copy pathpython.yml
File metadata and controls
441 lines (385 loc) · 17.2 KB
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
# Copyright 2026 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# SPDX-License-Identifier: Apache-2.0
name: Python Checks
on:
pull_request:
push:
branches:
- main
workflow_dispatch: {}
permissions:
contents: read
concurrency:
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
cancel-in-progress: ${{ github.ref != 'refs/heads/main' }}
jobs:
lint-and-format:
name: Lint and Format
runs-on: ubuntu-latest
timeout-minutes: 10
permissions:
contents: read
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
persist-credentials: false
- name: Install uv and setup Python
uses: astral-sh/setup-uv@1db35dd8eee11c5894872e0b383ae6c406cf738d # v10.4.0
with:
enable-cache: true
python-version: "3.12"
- name: Install dependencies
run: uv sync --frozen --group lint
- name: Check lockfile is up to date
run: uv lock --check
- name: Format check
run: uv run --no-sync ruff format --check --preview .
- name: Lint with ruff
run: uv run --no-sync ruff check --preview .
- name: Run consistency checks
run: python3 ./scripts/check_consistency.py
- name: Ensure minor/patch Dependabot PRs only update uv.lock
if: >-
github.event_name == 'pull_request' &&
github.event.pull_request.user.login == 'dependabot[bot]' &&
(startsWith(github.head_ref, 'dependabot/uv/py-minor-and-patch') ||
startsWith(github.head_ref, 'dependabot/uv/py-dependencies'))
env:
BASE_SHA: ${{ github.event.pull_request.base.sha }}
run: |
git fetch --no-tags --depth=1 origin "$BASE_SHA"
touched=$(git diff --name-only "$BASE_SHA" HEAD -- 'packages/*/pyproject.toml')
if [[ -n "$touched" ]]; then
echo "::error title=Unexpected pyproject.toml edit::Routine minor/patch Dependabot PRs must only update uv.lock, not published bounds in packages/*/pyproject.toml ($touched). Revert the pyproject.toml edit and run 'uv lock'."
exit 1
fi
type-check:
name: Type Check (${{ matrix.checker }})
runs-on: ubuntu-latest
timeout-minutes: 15
permissions:
contents: read
strategy:
fail-fast: false
matrix:
checker: [ty, pyrefly, pyright]
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
persist-credentials: false
- name: Install system dependencies
run: |
sudo apt-get update
sudo apt-get install -y --no-install-recommends build-essential libffi-dev cmake libjpeg-dev zlib1g-dev
- name: Install uv and setup Python
uses: astral-sh/setup-uv@1db35dd8eee11c5894872e0b383ae6c406cf738d # v10.4.0
with:
enable-cache: true
python-version: "3.12"
- name: Install dependencies
run: uv sync --frozen --group lint
- name: Generate schema typing
run: ./bin/generate_schema_typing --ci
- name: Type check with Ty
if: matrix.checker == 'ty'
run: uv run --no-sync ty check .
- name: Type check with Pyrefly
if: matrix.checker == 'pyrefly'
run: uv run --no-sync pyrefly check
- name: Type check with Pyright
if: matrix.checker == 'pyright'
run: uv run --no-sync pyright
security-and-compliance:
name: Security and Compliance
runs-on: ubuntu-latest
timeout-minutes: 10
permissions:
contents: read
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
persist-credentials: false
- name: Install uv and setup Python
uses: astral-sh/setup-uv@1db35dd8eee11c5894872e0b383ae6c406cf738d # v10.4.0
with:
enable-cache: true
python-version: "3.12"
- name: Install dependencies
run: uv sync --frozen --group lint
- name: Check source file license headers
run: ./bin/check_license
- name: Check Python dependency licenses
run: uv run --no-sync liccheck -s pyproject.toml
- name: Check for hardcoded secrets
run: |
if grep -rE "(sk-[a-zA-Z0-9]{20,}|AIza[a-zA-Z0-9_-]{35}|AKIA[0-9A-Z]{16})" \
packages/ samples/ --include="*.py" | grep -vE "test|mock|fake|example|#"; then
echo "::error::Potential hardcoded secrets found"
exit 1
fi
echo "No hardcoded secrets detected"
tests:
name: Tests (Python ${{ matrix.python-version }})
runs-on: ubuntu-latest
timeout-minutes: 20
permissions:
contents: read
strategy:
matrix:
python-version: ["3.10", "3.11", "3.12", "3.13", "3.14"]
fail-fast: false
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
persist-credentials: false
- name: Install system dependencies
run: |
sudo apt-get update
sudo apt-get install -y build-essential libffi-dev cmake libjpeg-dev zlib1g-dev
- name: Install uv and setup Python
uses: astral-sh/setup-uv@1db35dd8eee11c5894872e0b383ae6c406cf738d # v10.4.0
with:
enable-cache: true
python-version: ${{ matrix.python-version }}
- name: Install dependencies
env:
PY_VER: ${{ matrix.python-version }}
run: uv sync --frozen --python "$PY_VER"
- name: Generate schema typing
run: ./bin/generate_schema_typing --ci
- name: Fetch cross-SDK conformance specs
run: |
SPEC_DIR="${RUNNER_TEMP:-/tmp}/genkit-specs"
mkdir -p "$SPEC_DIR"
curl -sSfL https://raw.githubusercontent.com/genkit-ai/genkit/main/tests/specs/agent.yaml -o "$SPEC_DIR/agent.yaml"
curl -sSfL https://raw.githubusercontent.com/genkit-ai/genkit/main/tests/specs/generate.yaml -o "$SPEC_DIR/generate.yaml"
curl -sSfL https://raw.githubusercontent.com/genkit-ai/genkit/main/tests/specs/reflection_api.yaml -o "$SPEC_DIR/reflection_api.yaml"
curl -sSfL https://raw.githubusercontent.com/genkit-ai/genkit/main/tests/flow_server_tests.yaml -o "$SPEC_DIR/flow_server_tests.yaml"
curl -sSfL https://raw.githubusercontent.com/genkit-ai/genkit/main/genkit-tools/genkit-schema.json -o "$SPEC_DIR/genkit-schema.json"
echo "GENKIT_SPECS_DIR=$SPEC_DIR" >> "$GITHUB_ENV"
- name: Run tests
env:
PY_VER: ${{ matrix.python-version }}
COVERAGE_CORE: sysmon
run: |
if [[ "$PY_VER" == "3.12" ]]; then
uv run --no-sync --python "$PY_VER" \
pytest --cov --cov-report=term-missing:skip-covered
else
uv run --no-sync --python "$PY_VER" pytest
fi
# Floors in packages/*/pyproject.toml are what users resolve against, but the
# tests job only runs uv.lock (newest). This job resolves every direct dep at
# its declared floor so a PR can't silently require an API newer than its
# declared '>=' bound.
tests-minimum-deps:
name: Tests (oldest supported deps, Python 3.10)
runs-on: ubuntu-latest
timeout-minutes: 20
permissions:
contents: read
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
persist-credentials: false
- name: Install system dependencies
run: |
sudo apt-get update
sudo apt-get install -y build-essential libffi-dev cmake libjpeg-dev zlib1g-dev
- name: Install uv and setup Python
uses: astral-sh/setup-uv@1db35dd8eee11c5894872e0b383ae6c406cf738d # v10.4.0
with:
enable-cache: true
python-version: "3.10"
- name: Resolve direct deps at their floors
run: |
cp uv.lock /tmp/uv.lock.before
uv lock --resolution lowest-direct
echo "=== Direct deps downgraded from uv.lock -> declared floors ==="
git diff -U1 uv.lock | grep -E '^[ +-](name|version) = ' || echo 'uv.lock already at floors'
- name: Install dependencies
run: uv sync --frozen --python 3.10
- name: Generate schema typing
run: ./bin/generate_schema_typing --ci
- name: Run tests
run: |
set +e
set -o pipefail
uv run --no-sync --python 3.10 pytest | tee /tmp/min-deps.log
status=$?
if [[ "$status" -ne 0 ]]; then
python3 - <<'PY'
import os, pathlib, re
def parse_lock(path: pathlib.Path) -> dict[str, str]:
out: dict[str, str] = {}
for m in re.finditer(r'name = "([^"]+)"\nversion = "([^"]+)"', path.read_text()):
out[m.group(1).lower().replace('_', '-')] = m.group(2)
return out
before = parse_lock(pathlib.Path('/tmp/uv.lock.before'))
after = parse_lock(pathlib.Path('uv.lock'))
downgraded = {k: (after[k], before[k]) for k in after if k in before and after[k] != before[k]}
log_lines = pathlib.Path('/tmp/min-deps.log').read_text(errors='replace').splitlines()
failures = [ln.strip() for ln in log_lines if re.match(r'^(FAILED|ERROR)\s+\S+', ln.strip())]
failed_pkgs = sorted({m.group(1) for ln in failures if (m := re.search(r'packages/([^/]+)/', ln))})
def declared_deps(pkg: str) -> list[str]:
text = (pathlib.Path('packages') / pkg / 'pyproject.toml').read_text()
deps: list[str] = []
for m in re.finditer(r'"([A-Za-z0-9][A-Za-z0-9._-]*)(?:\[[^\]]*\])?\s*(?:>=|==|~=)', text):
name = m.group(1).lower().replace('_', '-')
if not name.startswith('genkit') and name not in deps:
deps.append(name)
return deps
dep_lines: list[str] = []
target_pkgs = list(failed_pkgs)
if target_pkgs and 'genkit' not in target_pkgs:
target_pkgs.append('genkit')
for pkg in target_pkgs:
for dep in declared_deps(pkg):
if dep in downgraded:
lo, hi = downgraded[dep]
dep_lines.append(f'- packages/{pkg}/pyproject.toml: {dep} {lo} (tested) vs {hi} (uv.lock)')
if not dep_lines:
for pkg_toml in sorted(pathlib.Path('packages').glob('*/pyproject.toml')):
pkg = pkg_toml.parent.name
for dep in declared_deps(pkg):
if dep in downgraded:
lo, hi = downgraded[dep]
dep_lines.append(f'- packages/{pkg}/pyproject.toml: {dep} {lo} (tested) vs {hi} (uv.lock)')
verify_targets = ' '.join(f'packages/{p}/tests' for p in failed_pkgs) or 'packages/'
shown_failures = [f'- {ln}' for ln in failures[:10]]
if len(failures) > 10:
shown_failures.append(f'- ... and {len(failures) - 10} more')
if not shown_failures:
shown_failures = ['- (see pytest output above)']
prompt = '\n'.join([
'Fix the `tests-minimum-deps` CI failure on this branch.',
'',
'Regular tests pass on uv.lock, but tests failed when direct dependencies',
'were resolved to their oldest declared `>=` versions in pyproject.toml.',
'',
'Failing tests:',
*shown_failures,
'',
'Downgraded dependencies in the failing package(s):',
*(dep_lines or ['- (no direct package deps changed; see uv.lock diff in the Resolve step)']),
'',
'What to do:',
'1. Inspect the failing test(s) and traceback. First check whether a small',
' backwards-compatible code change can keep the current minimum version',
' working; prefer that over raising the minimum when it is clean.',
'2. If the newer API/behavior is genuinely required, find the lowest',
' release that supports it:',
' - Check the dependency\'s release notes/changelog for the missing API',
' or fix (and probe candidate versions if the cutoff isn\'t clear).',
' - Do NOT blindly jump to the uv.lock version for shared deps',
' (pydantic, anyio, httpx, fastapi, opentelemetry-*). For leaf',
' provider SDKs (openai, anthropic, google-genai, ollama), finding the',
' exact cutoff is preferred, or bumping to uv.lock if unclear.',
'3. Update the `>=` minimum in the pyproject.toml above (keep any `<`',
' upper bound intact; see docs/dependencies.md for bounds policy).',
'4. Verify locally and refresh the lockfile:',
f' just test-min-deps {verify_targets}',
' uv lock && just check',
'5. Confirm uv.lock is not downgraded, then summarize what changed and why.',
])
print('\n' + '=' * 72)
print('TESTS FAILED ON OLDEST SUPPORTED DEPENDENCIES')
print('Copy-paste everything between the lines below into your coding agent:')
print('-' * 72)
print(prompt)
print('-' * 72)
print('=' * 72)
summary_path = os.environ.get('GITHUB_STEP_SUMMARY')
if summary_path:
with open(summary_path, 'a', encoding='utf-8') as f:
f.write('### `tests-minimum-deps` failed\n\nCopy-paste into your coding agent:\n\n```text\n')
f.write(prompt)
f.write('\n```\n')
PY
echo "::error title=tests-minimum-deps failed::Tests failed on oldest declared '>=' dependency versions. Copy-paste the prompt at the bottom of the Run tests log into your coding agent."
fi
exit "$status"
build:
name: Build Distributions
runs-on: ubuntu-latest
timeout-minutes: 15
permissions:
contents: read
if: always() && !failure() && !cancelled()
needs: [lint-and-format, type-check, security-and-compliance, tests, tests-minimum-deps]
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
persist-credentials: false
- name: Install uv
uses: astral-sh/setup-uv@1db35dd8eee11c5894872e0b383ae6c406cf738d # v10.4.0
with:
enable-cache: true
python-version: "3.12"
- name: Build and verify distributions
run: ./bin/build_dists
- name: Install each wheel in an isolated venv
run: ./bin/check_isolated_wheels
- name: Verify wheel contents
run: |
ERRORS=0
for wheel in dist/*.whl; do
if [[ -f "$wheel" ]]; then
wheel_name=$(basename "$wheel" | sed 's/-[0-9].*//')
if [[ "$wheel_name" == "genkit" ]] || [[ "$wheel_name" == genkit_* ]]; then
echo "Checking $wheel_name..."
if ! unzip -l "$wheel" 2>/dev/null | grep -qE "py\.typed$"; then
echo "::error::Wheel $wheel_name missing py.typed"
ERRORS=$((ERRORS + 1))
fi
if ! unzip -l "$wheel" 2>/dev/null | grep -qE "(LICENSE|licenses/LICENSE)"; then
echo "::error::Wheel $wheel_name missing LICENSE"
ERRORS=$((ERRORS + 1))
fi
fi
fi
done
if [[ "$ERRORS" -gt 0 ]]; then
echo "::error::$ERRORS wheel verification errors found"
exit 1
fi
- name: Upload distributions
uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2
with:
name: python-distributions
path: dist/
retention-days: 7
# =============================================================================
# Single fan-in gate job for branch protection required_status_checks
# =============================================================================
python-checks:
name: Run Python checks
runs-on: ubuntu-latest
permissions:
contents: read
needs: [lint-and-format, type-check, security-and-compliance, tests, tests-minimum-deps, build]
if: always()
steps:
- name: Verify all Python jobs succeeded
env:
RESULTS: >-
${{ needs.lint-and-format.result }},${{ needs.type-check.result }},${{ needs.security-and-compliance.result }},${{ needs.tests.result }},${{ needs.tests-minimum-deps.result }},${{ needs.build.result }}
run: |
echo "Python job results: $RESULTS"
if [[ "$RESULTS" != "success,success,success,success,success,success" ]]; then
echo "::error::One or more Python checks did not succeed: $RESULTS"
exit 1
fi