perucompras-cli 0.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (150) hide show
  1. perucompras_cli-0.1.0/.github/copilot-instructions.md +22 -0
  2. perucompras_cli-0.1.0/.github/dependabot.yml +10 -0
  3. perucompras_cli-0.1.0/.github/workflows/auto-tag.yml +50 -0
  4. perucompras_cli-0.1.0/.github/workflows/ci.yml +128 -0
  5. perucompras_cli-0.1.0/.github/workflows/health.yml +395 -0
  6. perucompras_cli-0.1.0/.github/workflows/migrations.yml +75 -0
  7. perucompras_cli-0.1.0/.github/workflows/publish.yml +117 -0
  8. perucompras_cli-0.1.0/.gitignore +11 -0
  9. perucompras_cli-0.1.0/AGENTS.md +60 -0
  10. perucompras_cli-0.1.0/CHANGELOG.md +65 -0
  11. perucompras_cli-0.1.0/CLAUDE.md +5 -0
  12. perucompras_cli-0.1.0/LICENSE +16 -0
  13. perucompras_cli-0.1.0/PKG-INFO +429 -0
  14. perucompras_cli-0.1.0/README.md +410 -0
  15. perucompras_cli-0.1.0/alembic.ini +5 -0
  16. perucompras_cli-0.1.0/docs/data-model.md +414 -0
  17. perucompras_cli-0.1.0/docs/endpoints.md +261 -0
  18. perucompras_cli-0.1.0/docs/erd/1-request-log-and-queries.png +0 -0
  19. perucompras_cli-0.1.0/docs/erd/2-catalog-attributes-extraction.png +0 -0
  20. perucompras_cli-0.1.0/docs/proposal-v2.md +545 -0
  21. perucompras_cli-0.1.0/docs/schema/models.py +5 -0
  22. perucompras_cli-0.1.0/migrations/env.py +99 -0
  23. perucompras_cli-0.1.0/migrations/script.py.mako +30 -0
  24. perucompras_cli-0.1.0/migrations/versions/701161616b58_initial_schema.py +493 -0
  25. perucompras_cli-0.1.0/migrations/versions/8a10b1c2d3e4_pending_proposals_view.py +90 -0
  26. perucompras_cli-0.1.0/migrations/versions/a6_membership_views.py +54 -0
  27. perucompras_cli-0.1.0/migrations/versions/blank_kind.py +110 -0
  28. perucompras_cli-0.1.0/migrations/versions/c872d93a6401_current_extraction_view.py +91 -0
  29. perucompras_cli-0.1.0/migrations/versions/categoric_kind.py +39 -0
  30. perucompras_cli-0.1.0/migrations/versions/extraction_provider.py +50 -0
  31. perucompras_cli-0.1.0/migrations/versions/ficha_card_lookup_stamp.py +134 -0
  32. perucompras_cli-0.1.0/migrations/versions/ficha_card_paged_stamp.py +99 -0
  33. perucompras_cli-0.1.0/migrations/versions/ficha_enrichment_attempts.py +134 -0
  34. perucompras_cli-0.1.0/migrations/versions/ficha_input_provenance.py +117 -0
  35. perucompras_cli-0.1.0/migrations/versions/ficha_rules_pass_attempts.py +118 -0
  36. perucompras_cli-0.1.0/migrations/versions/ficha_value_view.py +202 -0
  37. perucompras_cli-0.1.0/migrations/versions/unknown_view.py +76 -0
  38. perucompras_cli-0.1.0/pyproject.toml +58 -0
  39. perucompras_cli-0.1.0/scripts/capture_fixtures.py +345 -0
  40. perucompras_cli-0.1.0/src/piloto_ai_cli/__init__.py +51 -0
  41. perucompras_cli-0.1.0/src/piloto_ai_cli/__main__.py +5 -0
  42. perucompras_cli-0.1.0/src/piloto_ai_cli/api.py +394 -0
  43. perucompras_cli-0.1.0/src/piloto_ai_cli/attribute_text.py +106 -0
  44. perucompras_cli-0.1.0/src/piloto_ai_cli/attributes_service.py +250 -0
  45. perucompras_cli-0.1.0/src/piloto_ai_cli/auto_tag.py +65 -0
  46. perucompras_cli-0.1.0/src/piloto_ai_cli/backoff.py +25 -0
  47. perucompras_cli-0.1.0/src/piloto_ai_cli/cards.py +871 -0
  48. perucompras_cli-0.1.0/src/piloto_ai_cli/cli.py +602 -0
  49. perucompras_cli-0.1.0/src/piloto_ai_cli/connectors.py +106 -0
  50. perucompras_cli-0.1.0/src/piloto_ai_cli/database.py +491 -0
  51. perucompras_cli-0.1.0/src/piloto_ai_cli/enrichment.py +557 -0
  52. perucompras_cli-0.1.0/src/piloto_ai_cli/errors.py +92 -0
  53. perucompras_cli-0.1.0/src/piloto_ai_cli/excel.py +186 -0
  54. perucompras_cli-0.1.0/src/piloto_ai_cli/extraction.py +1316 -0
  55. perucompras_cli-0.1.0/src/piloto_ai_cli/health.py +398 -0
  56. perucompras_cli-0.1.0/src/piloto_ai_cli/health_workflow.py +1060 -0
  57. perucompras_cli-0.1.0/src/piloto_ai_cli/http_client.py +610 -0
  58. perucompras_cli-0.1.0/src/piloto_ai_cli/llm_client.py +259 -0
  59. perucompras_cli-0.1.0/src/piloto_ai_cli/llm_extraction.py +444 -0
  60. perucompras_cli-0.1.0/src/piloto_ai_cli/llm_output.py +90 -0
  61. perucompras_cli-0.1.0/src/piloto_ai_cli/pane.py +303 -0
  62. perucompras_cli-0.1.0/src/piloto_ai_cli/parsers.py +477 -0
  63. perucompras_cli-0.1.0/src/piloto_ai_cli/payload_store.py +227 -0
  64. perucompras_cli-0.1.0/src/piloto_ai_cli/pdf_parser.py +12 -0
  65. perucompras_cli-0.1.0/src/piloto_ai_cli/py.typed +0 -0
  66. perucompras_cli-0.1.0/src/piloto_ai_cli/query_compiler.py +1112 -0
  67. perucompras_cli-0.1.0/src/piloto_ai_cli/query_result.py +175 -0
  68. perucompras_cli-0.1.0/src/piloto_ai_cli/query_service.py +199 -0
  69. perucompras_cli-0.1.0/src/piloto_ai_cli/reextract.py +129 -0
  70. perucompras_cli-0.1.0/src/piloto_ai_cli/review.py +217 -0
  71. perucompras_cli-0.1.0/src/piloto_ai_cli/rules.py +67 -0
  72. perucompras_cli-0.1.0/src/piloto_ai_cli/run_log.py +230 -0
  73. perucompras_cli-0.1.0/src/piloto_ai_cli/schema/__init__.py +25 -0
  74. perucompras_cli-0.1.0/src/piloto_ai_cli/schema/contract-v1.schema.json +261 -0
  75. perucompras_cli-0.1.0/src/piloto_ai_cli/schema/contract.py +257 -0
  76. perucompras_cli-0.1.0/src/piloto_ai_cli/schema/migration_filters.py +59 -0
  77. perucompras_cli-0.1.0/src/piloto_ai_cli/schema/models.py +747 -0
  78. perucompras_cli-0.1.0/src/piloto_ai_cli/schema/snowflake_autogen.py +134 -0
  79. perucompras_cli-0.1.0/src/piloto_ai_cli/search_form.py +82 -0
  80. perucompras_cli-0.1.0/src/piloto_ai_cli/settings.py +174 -0
  81. perucompras_cli-0.1.0/src/piloto_ai_cli/snowflake_url.py +90 -0
  82. perucompras_cli-0.1.0/src/piloto_ai_cli/stamp_service.py +53 -0
  83. perucompras_cli-0.1.0/src/piloto_ai_cli/sync.py +1157 -0
  84. perucompras_cli-0.1.0/src/piloto_ai_cli/unit_converter.py +81 -0
  85. perucompras_cli-0.1.0/tests/conftest.py +340 -0
  86. perucompras_cli-0.1.0/tests/fixtures/README.md +27 -0
  87. perucompras_cli-0.1.0/tests/fixtures/agreements_by_status.json +1 -0
  88. perucompras_cli-0.1.0/tests/fixtures/download_agreement.xlsx +0 -0
  89. perucompras_cli-0.1.0/tests/fixtures/download_agreement_department.xlsx +0 -0
  90. perucompras_cli-0.1.0/tests/fixtures/error_page.html +19 -0
  91. perucompras_cli-0.1.0/tests/fixtures/ficha/1899511-20250220-083315.pdf +0 -0
  92. perucompras_cli-0.1.0/tests/fixtures/ficha/2381152-20260716-183745.pdf +0 -0
  93. perucompras_cli-0.1.0/tests/fixtures/home.html +991 -0
  94. perucompras_cli-0.1.0/tests/fixtures/llm_response_v2.json +46 -0
  95. perucompras_cli-0.1.0/tests/fixtures/manifest.json +213 -0
  96. perucompras_cli-0.1.0/tests/fixtures/search_category.html +2064 -0
  97. perucompras_cli-0.1.0/tests/fixtures/search_category_frijol.html +2090 -0
  98. perucompras_cli-0.1.0/tests/fixtures/search_form.json +26 -0
  99. perucompras_cli-0.1.0/tests/test_api.py +463 -0
  100. perucompras_cli-0.1.0/tests/test_attribute_text.py +150 -0
  101. perucompras_cli-0.1.0/tests/test_attributes.py +392 -0
  102. perucompras_cli-0.1.0/tests/test_auto_tag.py +149 -0
  103. perucompras_cli-0.1.0/tests/test_cards.py +2421 -0
  104. perucompras_cli-0.1.0/tests/test_ci_workflow.py +248 -0
  105. perucompras_cli-0.1.0/tests/test_cli.py +409 -0
  106. perucompras_cli-0.1.0/tests/test_connectors.py +318 -0
  107. perucompras_cli-0.1.0/tests/test_contract.py +124 -0
  108. perucompras_cli-0.1.0/tests/test_contract_validator.py +324 -0
  109. perucompras_cli-0.1.0/tests/test_current_extraction.py +354 -0
  110. perucompras_cli-0.1.0/tests/test_database.py +1198 -0
  111. perucompras_cli-0.1.0/tests/test_database_live.py +134 -0
  112. perucompras_cli-0.1.0/tests/test_department_sync.py +608 -0
  113. perucompras_cli-0.1.0/tests/test_enrichment.py +1070 -0
  114. perucompras_cli-0.1.0/tests/test_errors.py +54 -0
  115. perucompras_cli-0.1.0/tests/test_excel.py +172 -0
  116. perucompras_cli-0.1.0/tests/test_extraction.py +2628 -0
  117. perucompras_cli-0.1.0/tests/test_ficha_value.py +318 -0
  118. perucompras_cli-0.1.0/tests/test_fixtures.py +140 -0
  119. perucompras_cli-0.1.0/tests/test_harness.py +201 -0
  120. perucompras_cli-0.1.0/tests/test_health.py +723 -0
  121. perucompras_cli-0.1.0/tests/test_health_workflow.py +1949 -0
  122. perucompras_cli-0.1.0/tests/test_http_client.py +822 -0
  123. perucompras_cli-0.1.0/tests/test_llm_client.py +600 -0
  124. perucompras_cli-0.1.0/tests/test_llm_extraction.py +324 -0
  125. perucompras_cli-0.1.0/tests/test_llm_output.py +56 -0
  126. perucompras_cli-0.1.0/tests/test_migrations.py +949 -0
  127. perucompras_cli-0.1.0/tests/test_packaging.py +165 -0
  128. perucompras_cli-0.1.0/tests/test_pane.py +599 -0
  129. perucompras_cli-0.1.0/tests/test_parsers.py +274 -0
  130. perucompras_cli-0.1.0/tests/test_payload_store.py +372 -0
  131. perucompras_cli-0.1.0/tests/test_pdf_parser.py +19 -0
  132. perucompras_cli-0.1.0/tests/test_publish_workflow.py +155 -0
  133. perucompras_cli-0.1.0/tests/test_query.py +362 -0
  134. perucompras_cli-0.1.0/tests/test_query_compiler.py +1432 -0
  135. perucompras_cli-0.1.0/tests/test_query_result.py +410 -0
  136. perucompras_cli-0.1.0/tests/test_reextract.py +618 -0
  137. perucompras_cli-0.1.0/tests/test_request_logging.py +408 -0
  138. perucompras_cli-0.1.0/tests/test_review.py +316 -0
  139. perucompras_cli-0.1.0/tests/test_rules.py +211 -0
  140. perucompras_cli-0.1.0/tests/test_run_log.py +320 -0
  141. perucompras_cli-0.1.0/tests/test_save.py +179 -0
  142. perucompras_cli-0.1.0/tests/test_schema.py +139 -0
  143. perucompras_cli-0.1.0/tests/test_search_form.py +62 -0
  144. perucompras_cli-0.1.0/tests/test_settings.py +350 -0
  145. perucompras_cli-0.1.0/tests/test_stamp_service.py +79 -0
  146. perucompras_cli-0.1.0/tests/test_sync.py +1211 -0
  147. perucompras_cli-0.1.0/tests/test_sync_orchestration.py +1072 -0
  148. perucompras_cli-0.1.0/tests/test_unit_converter.py +129 -0
  149. perucompras_cli-0.1.0/tests/test_unknown.py +277 -0
  150. perucompras_cli-0.1.0/uv.lock +1008 -0
@@ -0,0 +1,22 @@
1
+ # Copilot instructions
2
+
3
+ [`AGENTS.md`](../AGENTS.md) is the canonical source of agent instructions for
4
+ this repository: read it and follow it for code changes and reviews. The rules
5
+ below repeat its decision rules because code review reads only this file.
6
+
7
+ ## Decisions: best practice by default
8
+
9
+ - Apply the established best practice for every fix and every review finding,
10
+ and state it as the one required fix, not as a menu of options.
11
+ - Never propose a known bad practice, not even as one option among others (for
12
+ example: new failures piling up as comments on one already-assigned issue that
13
+ never reaches an owner again; reusing a column with a second meaning to avoid a
14
+ migration; shipping a known defect with a note; silently continuing without
15
+ required configuration; swallowing errors; over-broad privileges; weakening
16
+ tests to get green).
17
+ - Raise a question for the maintainers only when two or more options that are
18
+ each a genuine best practice compete, or the choice is theirs whatever the
19
+ practice (scope, cost, external stakeholders, production or irreversible
20
+ changes). Then list only those options with trade-offs and recommend one.
21
+ - Decisions already recorded in the issue, PR replies or docs take precedence.
22
+ Never re-ask an answered question.
@@ -0,0 +1,10 @@
1
+ version: 2
2
+ updates:
3
+ - package-ecosystem: uv
4
+ directory: /
5
+ schedule:
6
+ interval: weekly
7
+ - package-ecosystem: github-actions
8
+ directory: /
9
+ schedule:
10
+ interval: weekly
@@ -0,0 +1,50 @@
1
+ name: Auto tag
2
+
3
+ on:
4
+ push:
5
+ branches: [main]
6
+ paths:
7
+ - pyproject.toml
8
+
9
+ permissions:
10
+ contents: read
11
+
12
+ jobs:
13
+ tag:
14
+ runs-on: ubuntu-latest
15
+ steps:
16
+ - uses: actions/checkout@v7
17
+ with:
18
+ fetch-depth: 0
19
+ token: ${{ secrets.RELEASE_TOKEN || github.token }}
20
+ - uses: actions/setup-python@v7
21
+ with:
22
+ python-version: "3.15"
23
+ - uses: astral-sh/setup-uv@v7
24
+ with:
25
+ enable-cache: true
26
+ - id: version
27
+ env:
28
+ BEFORE: ${{ github.event.before }}
29
+ run: |
30
+ tag=$(uv run --locked python -m piloto_ai_cli.auto_tag "$BEFORE")
31
+ if [ -n "$tag" ]; then
32
+ if git rev-parse --verify --quiet "refs/tags/$tag" >/dev/null; then
33
+ echo "Tag $tag already exists; skipping."
34
+ exit 0
35
+ fi
36
+ echo "tag=$tag" >> "$GITHUB_OUTPUT"
37
+ fi
38
+ - if: steps.version.outputs.tag != ''
39
+ env:
40
+ RELEASE_TOKEN: ${{ secrets.RELEASE_TOKEN }}
41
+ TAG: ${{ steps.version.outputs.tag }}
42
+ run: |
43
+ if [ -z "$RELEASE_TOKEN" ]; then
44
+ echo "::error::RELEASE_TOKEN secret is not set; it is required to push release tags."
45
+ exit 1
46
+ fi
47
+ git config user.name "github-actions[bot]"
48
+ git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
49
+ git tag "$TAG"
50
+ git push origin "$TAG"
@@ -0,0 +1,128 @@
1
+ name: CI
2
+
3
+ on:
4
+ push:
5
+ branches: [main]
6
+ pull_request:
7
+ workflow_dispatch:
8
+
9
+ permissions:
10
+ contents: read
11
+
12
+ jobs:
13
+ lint:
14
+ name: Lint
15
+ runs-on: ubuntu-latest
16
+ steps:
17
+ - uses: actions/checkout@v7
18
+ - uses: actions/setup-python@v7
19
+ with:
20
+ python-version: "3.15"
21
+ - uses: astral-sh/setup-uv@v7
22
+ with:
23
+ enable-cache: true
24
+ - run: uv sync --locked --group dev
25
+ - run: uv run ruff check
26
+ - run: uv run ruff format --check
27
+
28
+ typecheck:
29
+ name: Typecheck
30
+ runs-on: ubuntu-latest
31
+ steps:
32
+ - uses: actions/checkout@v7
33
+ - uses: actions/setup-python@v7
34
+ with:
35
+ python-version: "3.15"
36
+ - uses: astral-sh/setup-uv@v7
37
+ with:
38
+ enable-cache: true
39
+ - run: uv sync --locked --group dev
40
+ - run: uv run mypy --strict src
41
+
42
+ test:
43
+ name: Test
44
+ runs-on: ubuntu-latest
45
+ steps:
46
+ - uses: actions/checkout@v7
47
+ - uses: actions/setup-python@v7
48
+ with:
49
+ python-version: "3.15"
50
+ - uses: astral-sh/setup-uv@v7
51
+ with:
52
+ enable-cache: true
53
+ - run: uv sync --locked --group dev
54
+ - run: uv run pytest
55
+
56
+ actionlint:
57
+ name: Actionlint
58
+ runs-on: ubuntu-latest
59
+ steps:
60
+ - uses: actions/checkout@v7
61
+ - uses: raven-actions/actionlint@v2
62
+
63
+ snowflake-migrations-config:
64
+ name: Snowflake migration credentials
65
+ if: >-
66
+ vars.SNOWFLAKE_MIGRATIONS_ENABLED == 'true' &&
67
+ (github.event_name != 'pull_request' ||
68
+ github.event.pull_request.head.repo.full_name == github.repository)
69
+ runs-on: ubuntu-latest
70
+ outputs:
71
+ enabled: ${{ steps.credentials.outputs.enabled }}
72
+ steps:
73
+ - name: Check credentials
74
+ id: credentials
75
+ env:
76
+ SNOWFLAKE_CI_MIGRATOR_PAT: ${{ secrets.SNOWFLAKE_CI_MIGRATOR_PAT }}
77
+ run: |
78
+ if [[ -n "$SNOWFLAKE_CI_MIGRATOR_PAT" ]]; then
79
+ echo "enabled=true" >> "$GITHUB_OUTPUT"
80
+ else
81
+ echo "enabled=false" >> "$GITHUB_OUTPUT"
82
+ fi
83
+
84
+ # Checks the migrations against real Snowflake on the scratch database
85
+ # PILOTO_AI_CLI_CI, never on PILOTO_AI: it downgrades back to base, which
86
+ # drops every CLI table. Its identity (snowflake-infra cli_ci.tf) has no
87
+ # access to PILOTO_AI. Deploying to PILOTO_AI.CLI is migrations.yml's job.
88
+ snowflake-migrations:
89
+ name: Snowflake migrations
90
+ needs: snowflake-migrations-config
91
+ if: needs.snowflake-migrations-config.outputs.enabled == 'true'
92
+ runs-on: ubuntu-latest
93
+ timeout-minutes: 10
94
+ concurrency:
95
+ group: snowflake-cli-ci-migrations
96
+ cancel-in-progress: false
97
+ steps:
98
+ - uses: actions/checkout@v7
99
+ - uses: actions/setup-python@v7
100
+ with:
101
+ python-version: "3.15"
102
+ - uses: astral-sh/setup-uv@v7
103
+ with:
104
+ enable-cache: true
105
+ - run: uv sync --locked --group dev
106
+ - name: Build Snowflake database URL
107
+ id: url
108
+ env:
109
+ SNOWFLAKE_PAT: ${{ secrets.SNOWFLAKE_CI_MIGRATOR_PAT }}
110
+ SNOWFLAKE_ACCOUNT: ${{ vars.SNOWFLAKE_ACCOUNT }}
111
+ SNOWFLAKE_WAREHOUSE: ${{ vars.SNOWFLAKE_WAREHOUSE }}
112
+ # Fixed on purpose: no repository variable can point this check at
113
+ # the deployed database.
114
+ SNOWFLAKE_DATABASE: PILOTO_AI_CLI_CI
115
+ SNOWFLAKE_USER: PILOTO_AI_CLI_CI_MIGRATOR_SVC
116
+ SNOWFLAKE_ROLE: PILOTO_AI_CLI_CI_MIGRATOR
117
+ run: python -I src/piloto_ai_cli/snowflake_url.py
118
+ - name: Reset the scratch schema
119
+ # A cancelled earlier run can leave the scratch schema at any revision.
120
+ run: uv run alembic downgrade base
121
+ - name: Upgrade and check the cli schema
122
+ run: |
123
+ uv run alembic upgrade head
124
+ uv run alembic check
125
+ - name: Downgrade the scratch schema
126
+ # Also exercises every downgrade, and leaves the scratch schema empty.
127
+ if: always() && steps.url.outcome == 'success'
128
+ run: uv run alembic downgrade base
@@ -0,0 +1,395 @@
1
+ name: Health
2
+
3
+ # Runs `pc health` against the live site and the pilot database. Each failed
4
+ # check gets its own issue assigned to Copilot; a check whose issue is still open
5
+ # is recorded on that issue instead. Closed issues are never reopened. It never
6
+ # runs on pull requests.
7
+ on:
8
+ schedule:
9
+ # Daily (decided 2026-11-05). Live checks must not run more often than
10
+ # every 3 hours, so do not add schedules. The "Enforce the 3-hour spacing"
11
+ # step stops any run (a dispatch, a queued run, a re-run) that comes sooner.
12
+ - cron: "23 10 * * *"
13
+ workflow_dispatch:
14
+ inputs:
15
+ force_failure:
16
+ description: Add a failed workflow.forced_failure check, to test that the run opens an issue for it assigned to Copilot
17
+ required: false
18
+ type: boolean
19
+ default: false
20
+
21
+ permissions:
22
+ contents: read
23
+
24
+ concurrency:
25
+ group: pc-health
26
+ cancel-in-progress: false
27
+
28
+ jobs:
29
+ preflight:
30
+ # Fails the run before any live check when the Copilot user token is not
31
+ # set, so a failure could not reach an owner. A separate job on its own
32
+ # runner that runs no project or third-party code (no checkout, no
33
+ # install): the health job, which runs dependencies, never references the
34
+ # token, not even to test whether it is set.
35
+ name: Check the issue token
36
+ if: vars.HEALTH_CHECK_ENABLED == 'true'
37
+ # The token lives in this environment, which only this job and the issues
38
+ # job use; its deployment branch policy allows only main.
39
+ environment: pc-health-issues
40
+ runs-on: ubuntu-latest
41
+ timeout-minutes: 5
42
+ permissions: {}
43
+ steps:
44
+ - name: Check the Copilot token
45
+ env:
46
+ COPILOT_ASSIGN_TOKEN_SET: ${{ secrets.COPILOT_ASSIGN_TOKEN != '' }}
47
+ run: |
48
+ if [[ "$COPILOT_ASSIGN_TOKEN_SET" != "true" ]]; then
49
+ echo "::error::HEALTH_CHECK_ENABLED is true but this required repository configuration is not set: secret COPILOT_ASSIGN_TOKEN in environment pc-health-issues"
50
+ exit 1
51
+ fi
52
+
53
+ health:
54
+ name: pc health
55
+ needs: preflight
56
+ if: vars.HEALTH_CHECK_ENABLED == 'true'
57
+ # The database secret lives in this environment, whose deployment branch
58
+ # policy allows only main: a workflow edited on another branch can't read
59
+ # it. The Copilot token is in another environment this job never uses.
60
+ environment: pc-health
61
+ runs-on: ubuntu-latest
62
+ # Spacing and setup (a few minutes), the install (at most 2 x 5), pc health
63
+ # (at most 20) and the report all fit inside it, so a step's own timeout,
64
+ # never the job's, stops a hang and the failure is still reported.
65
+ timeout-minutes: 40
66
+ permissions:
67
+ # Read this workflow's earlier runs to enforce the 3-hour spacing.
68
+ actions: read
69
+ contents: read
70
+ outputs:
71
+ failed: ${{ steps.report.outputs.failed }}
72
+ # Identifies the report (the attempt of this job that produced it), so a
73
+ # re-run of the issues job never records the same report twice.
74
+ report-id: ${{ steps.report.outputs.report-id }}
75
+ # When the report was made: the time stamped on its issues and comments,
76
+ # and the lower bound of the issues the issues job inspects.
77
+ report-time: ${{ steps.report.outputs.report-time }}
78
+ env:
79
+ REPORT_DIR: health-report
80
+ # Install and run only the runtime dependencies: the dev group (linters,
81
+ # test tools) is unneeded third-party code in a job that holds secrets.
82
+ UV_NO_DEV: "1"
83
+ steps:
84
+ - name: Check configuration
85
+ env:
86
+ PILOTO_DATABASE_URL: ${{ secrets.PILOTO_HEALTH_DATABASE_URL }}
87
+ PILOTO_PAYLOAD_STAGE: ${{ vars.PILOTO_PAYLOAD_STAGE }}
88
+ run: |
89
+ # GitHub masks only a secret's whole value: also mask the password or
90
+ # token embedded in the database URL, raw and percent-decoded, so it
91
+ # can't show in a log on its own.
92
+ mask() {
93
+ [[ -n "$1" ]] || return 0
94
+ echo "::add-mask::$1"
95
+ local decoded
96
+ decoded=$(printf '%b' "${1//%/\\x}")
97
+ if [[ "$decoded" != "$1" ]]; then echo "::add-mask::$decoded"; fi
98
+ }
99
+ if [[ "$PILOTO_DATABASE_URL" =~ ^[^:/]+://[^:/@]*:([^@]*)@ ]]; then
100
+ mask "${BASH_REMATCH[1]}"
101
+ fi
102
+ query="${PILOTO_DATABASE_URL#*\?}"
103
+ if [[ "$query" != "$PILOTO_DATABASE_URL" ]]; then
104
+ IFS='&' read -ra pairs <<< "$query"
105
+ for pair in "${pairs[@]}"; do
106
+ key="${pair%%=*}"
107
+ case "${key,,}" in
108
+ password|passcode|token|private_key|private_key_file_pwd) mask "${pair#*=}" ;;
109
+ esac
110
+ done
111
+ fi
112
+ missing=()
113
+ if [[ -z "$PILOTO_DATABASE_URL" ]]; then missing+=("secret PILOTO_HEALTH_DATABASE_URL"); fi
114
+ if [[ -z "$PILOTO_PAYLOAD_STAGE" ]]; then missing+=("variable PILOTO_PAYLOAD_STAGE"); fi
115
+ if (( ${#missing[@]} )); then
116
+ list=$(printf '%s, ' "${missing[@]}")
117
+ echo "::error::HEALTH_CHECK_ENABLED is true but this required repository configuration is not set: ${list%, }"
118
+ exit 1
119
+ fi
120
+ # Same rule as PAYLOAD_STAGE_RE in src/piloto_ai_cli/settings.py.
121
+ stage_re='^@[A-Za-z_][A-Za-z0-9_$]*(\.[A-Za-z_][A-Za-z0-9_$]*){0,2}$'
122
+ if [[ ! "$PILOTO_PAYLOAD_STAGE" =~ $stage_re ]]; then
123
+ echo "::error::Repository variable PILOTO_PAYLOAD_STAGE must be an unquoted Snowflake stage name, such as @PILOTO_AI.CLI.PAYLOADS."
124
+ exit 1
125
+ fi
126
+ - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
127
+ with:
128
+ # Later steps run third-party code (uv sync, pc health) and never need
129
+ # git credentials, so keep the job token out of .git/config.
130
+ persist-credentials: false
131
+ - uses: actions/setup-python@6d4fd22b20465ccc0f7354476588e8df7c9ca0e0 # v7.1.0
132
+ with:
133
+ python-version: "3.15"
134
+ # The 3-hour spacing check runs before any live request: it fails if
135
+ # another run of this workflow, or an earlier attempt of this one, started
136
+ # its live checks less than 3 hours ago. Inside the concurrency group, so a
137
+ # queued run sees the run before it. The gh steps are the only run steps
138
+ # given the job token, and they run before the project's dependencies are
139
+ # installed (uv sync): the helper is stdlib-only and runs with python -I.
140
+ # The SHA-pinned actions (checkout, setup-python, setup-uv) also receive
141
+ # this read-only token (actions: read, contents: read) through their
142
+ # default token inputs; keep this job's permissions read-only.
143
+ - name: List recent runs
144
+ env:
145
+ GH_TOKEN: ${{ github.token }}
146
+ run: |
147
+ set -euo pipefail
148
+ mkdir -p "$RUNNER_TEMP/health-spacing/jobs"
149
+ # A run can be re-run up to 30 days after it was created; older runs
150
+ # cannot have started live checks recently.
151
+ since=$(date -u -d "31 days ago" +%Y-%m-%dT%H:%M:%SZ)
152
+ gh api --paginate \
153
+ "repos/$GITHUB_REPOSITORY/actions/workflows/health.yml/runs?per_page=100&created=%3E%3D$since" \
154
+ > "$RUNNER_TEMP/health-spacing/runs.json"
155
+ - name: Select runs to inspect
156
+ run: |
157
+ set -euo pipefail
158
+ spacing_dir="$RUNNER_TEMP/health-spacing"
159
+ python -I src/piloto_ai_cli/health_workflow.py recent-runs \
160
+ --runs "$spacing_dir/runs.json" \
161
+ --run-id "$GITHUB_RUN_ID" > "$spacing_dir/runs.txt"
162
+ - name: List the jobs of recent runs
163
+ env:
164
+ GH_TOKEN: ${{ github.token }}
165
+ run: |
166
+ set -euo pipefail
167
+ spacing_dir="$RUNNER_TEMP/health-spacing"
168
+ while read -r -u 3 run_id; do
169
+ gh api --paginate "repos/$GITHUB_REPOSITORY/actions/runs/$run_id/jobs?filter=all&per_page=100" \
170
+ > "$spacing_dir/jobs/$run_id.json"
171
+ done 3< "$spacing_dir/runs.txt"
172
+ - name: Enforce the 3-hour spacing
173
+ run: |
174
+ set -euo pipefail
175
+ spacing_dir="$RUNNER_TEMP/health-spacing"
176
+ python -I src/piloto_ai_cli/health_workflow.py check-spacing \
177
+ --runs "$spacing_dir/runs.json" \
178
+ --run-id "$GITHUB_RUN_ID" \
179
+ --run-attempt "$GITHUB_RUN_ATTEMPT" \
180
+ --jobs-dir "$spacing_dir/jobs"
181
+ # A hung install (a stalled download) is stopped by its step timeout, and
182
+ # a stopped or failed install is reported as a failed workflow.install.
183
+ - uses: astral-sh/setup-uv@37802adc94f370d6bfd71619e3f0bf239e1f3b78 # v7.6.0
184
+ id: setup-uv
185
+ timeout-minutes: 5
186
+ with:
187
+ enable-cache: true
188
+ - run: uv sync --locked
189
+ id: install
190
+ timeout-minutes: 5
191
+ - name: Run pc health
192
+ id: health
193
+ # A hung pc health (e.g. a stalled database query) must still end in a
194
+ # report and an issue: stop it well before the job's own timeout and
195
+ # carry on, so the next step reports it as a failed workflow.pc_health.
196
+ timeout-minutes: 20
197
+ continue-on-error: true
198
+ env:
199
+ PILOTO_PROFILE: pilot
200
+ PILOTO_DATABASE_URL: ${{ secrets.PILOTO_HEALTH_DATABASE_URL }}
201
+ # Every response the checks receive is kept in this Snowflake stage
202
+ # (keep raw sources), never only on the runner.
203
+ PILOTO_PAYLOAD_STAGE: ${{ vars.PILOTO_PAYLOAD_STAGE }}
204
+ # pc health makes no LLM call, but the pilot profile requires a key:
205
+ # a placeholder keeps the real key out of this job.
206
+ PILOTO_LLM_API_KEY: unused-by-pc-health
207
+ run: |
208
+ status=0
209
+ uv run --locked pc --json health > "$RUNNER_TEMP/pc-health.json" || status=$?
210
+ echo "exit-code=$status" >> "$GITHUB_OUTPUT"
211
+ - name: Prepare report
212
+ id: report
213
+ # Also when the CLI could not be installed, so that failure still ends
214
+ # in an issue. Not after a configuration or spacing failure: those stop
215
+ # the run on purpose, before any live check.
216
+ if: >-
217
+ always() && (steps.health.outcome != 'skipped'
218
+ || steps.setup-uv.outcome == 'failure' || steps.setup-uv.outcome == 'cancelled'
219
+ || steps.install.outcome == 'failure' || steps.install.outcome == 'cancelled')
220
+ env:
221
+ # Only read to redact it from the report (and so from every issue).
222
+ PILOTO_DATABASE_URL: ${{ secrets.PILOTO_HEALTH_DATABASE_URL }}
223
+ # No exit code when the step was stopped by its timeout: 124, like timeout(1).
224
+ EXIT_CODE: ${{ steps.health.outputs.exit-code || '124' }}
225
+ FORCED: ${{ github.event_name == 'workflow_dispatch' && inputs.force_failure }}
226
+ INSTALL_FAILED: >-
227
+ ${{ steps.setup-uv.outcome == 'failure' || steps.setup-uv.outcome == 'cancelled'
228
+ || steps.install.outcome == 'failure' || steps.install.outcome == 'cancelled' }}
229
+ # The stdlib-only helper, run in isolated mode: it works without the
230
+ # project's environment, so a failed install is still reported.
231
+ run: |
232
+ {
233
+ echo "report-id=$GITHUB_RUN_ID.$GITHUB_RUN_ATTEMPT"
234
+ echo "report-time=$(date -u +%Y-%m-%dT%H:%M:%SZ)"
235
+ python -I src/piloto_ai_cli/health_workflow.py report \
236
+ --report "$RUNNER_TEMP/pc-health.json" \
237
+ --exit-code "$EXIT_CODE" \
238
+ --forced "$FORCED" \
239
+ --install-failed "$INSTALL_FAILED" \
240
+ --output-dir "$REPORT_DIR"
241
+ } >> "$GITHUB_OUTPUT"
242
+ - name: Upload report
243
+ if: >-
244
+ always() && (steps.health.outcome != 'skipped'
245
+ || steps.setup-uv.outcome == 'failure' || steps.setup-uv.outcome == 'cancelled'
246
+ || steps.install.outcome == 'failure' || steps.install.outcome == 'cancelled')
247
+ uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2
248
+ with:
249
+ name: health-report
250
+ path: health-report/
251
+ if-no-files-found: warn
252
+
253
+ issues:
254
+ # A separate job on a fresh runner: the health job runs the project's
255
+ # dependencies (uv sync, pc health), and anything they leave on a runner
256
+ # could read a token a later step there holds. This job runs no third-party
257
+ # code: checkout, the redacted report artifact, gh and the stdlib-only
258
+ # helper. It alone can write issues and uses the Copilot user token.
259
+ name: Health issues
260
+ needs: health
261
+ # !cancelled(): also after a failed health job (e.g. a failed install that
262
+ # was reported), which the implicit success() would skip.
263
+ if: ${{ !cancelled() && needs.health.outputs.failed == 'true' }}
264
+ environment: pc-health-issues
265
+ runs-on: ubuntu-latest
266
+ timeout-minutes: 10
267
+ permissions:
268
+ contents: read
269
+ issues: write
270
+ env:
271
+ REPORT_DIR: health-report
272
+ steps:
273
+ - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
274
+ with:
275
+ persist-credentials: false
276
+ - uses: actions/setup-python@6d4fd22b20465ccc0f7354476588e8df7c9ca0e0 # v7.1.0
277
+ with:
278
+ python-version: "3.15"
279
+ - uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8.0.2
280
+ with:
281
+ name: health-report
282
+ path: health-report/
283
+ - name: List the open issues
284
+ # Only gh runs with a token here. The open issues take new recurrences;
285
+ # the issues updated since the report, open or closed, show where this
286
+ # report is already recorded (recording it always updates the issue).
287
+ env:
288
+ GH_TOKEN: ${{ github.token }}
289
+ REPORT_TIME: ${{ needs.health.outputs.report-time }}
290
+ run: |
291
+ set -euo pipefail
292
+ gh api --paginate "repos/$GITHUB_REPOSITORY/issues?state=open&per_page=100" \
293
+ > "$RUNNER_TEMP/open-issues.json"
294
+ gh api --paginate \
295
+ "repos/$GITHUB_REPOSITORY/issues?state=all&since=$REPORT_TIME&per_page=100" \
296
+ > "$RUNNER_TEMP/recent-issues.json"
297
+ - name: Select the issues to inspect
298
+ # No token in this step: the stdlib-only helper prints the issues this
299
+ # workflow opened (by author and hidden check= marker), open or closed,
300
+ # updated since the report.
301
+ run: |
302
+ set -euo pipefail
303
+ python -I src/piloto_ai_cli/health_workflow.py tracked-issues \
304
+ --issues "$RUNNER_TEMP/recent-issues.json" > "$RUNNER_TEMP/tracked-issues.txt"
305
+ - name: List the comments of those issues
306
+ # Only gh runs with a token here. The comments show which reports an
307
+ # issue already records, so a re-run of this job records nothing twice.
308
+ env:
309
+ GH_TOKEN: ${{ github.token }}
310
+ run: |
311
+ set -euo pipefail
312
+ mkdir -p "$RUNNER_TEMP/issue-comments"
313
+ while read -r -u 3 number; do
314
+ gh api --paginate "repos/$GITHUB_REPOSITORY/issues/$number/comments?per_page=100" \
315
+ > "$RUNNER_TEMP/issue-comments/$number.json"
316
+ done 3< "$RUNNER_TEMP/tracked-issues.txt"
317
+ - name: Plan the health issues
318
+ # No token in this step. The helper uses the standard library only, so
319
+ # it runs with plain isolated Python and no installed dependencies.
320
+ env:
321
+ RUN_URL: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}
322
+ REPORT_ID: ${{ needs.health.outputs.report-id }}
323
+ REPORT_TIME: ${{ needs.health.outputs.report-time }}
324
+ run: |
325
+ set -euo pipefail
326
+ # The helper keeps only the open issues this workflow opened, by their
327
+ # hidden check= marker, and plans one action per failed check. An
328
+ # issue that already records this report (hidden report= marker in its
329
+ # body or a comment the workflow wrote) gets no second record.
330
+ python -I src/piloto_ai_cli/health_workflow.py plan \
331
+ --report "$REPORT_DIR/report.json" \
332
+ --open-issues "$RUNNER_TEMP/open-issues.json" \
333
+ --run-url "$RUN_URL" \
334
+ --report-id "$REPORT_ID" \
335
+ --report-time "$REPORT_TIME" \
336
+ --recent-issues "$RUNNER_TEMP/recent-issues.json" \
337
+ --comments-dir "$RUNNER_TEMP/issue-comments" \
338
+ --output-dir "$RUNNER_TEMP/health-issues"
339
+ - name: Open or update the health issues
340
+ # The Copilot user token is only in this step, which runs nothing but
341
+ # gh over the plan.
342
+ env:
343
+ GH_TOKEN: ${{ github.token }}
344
+ COPILOT_ASSIGN_TOKEN: ${{ secrets.COPILOT_ASSIGN_TOKEN }}
345
+ COPILOT_LOGIN: copilot-swe-agent[bot]
346
+ run: |
347
+ set -euo pipefail
348
+ repo="$GITHUB_REPOSITORY"
349
+ plan_dir="$RUNNER_TEMP/health-issues"
350
+ # Carry out every action even if one fails, then fail the step: a
351
+ # later run retries what is left (an unassigned open issue is assigned).
352
+ # The plan is read on its own descriptor so gh never consumes it as input.
353
+ errors=0
354
+ while IFS=$'\t' read -r -u 3 action number assign title_file body_file key; do
355
+ if [[ "$action" == open ]]; then
356
+ if ! number=$(gh api -X POST "repos/$repo/issues" \
357
+ -f "title=$(< "$title_file")" -F "body=@$body_file" --jq .number); then
358
+ echo "::error::Could not open the issue for check $key."
359
+ errors=$((errors + 1))
360
+ continue
361
+ fi
362
+ echo "Opened issue #$number for check $key."
363
+ elif [[ "$action" == recur ]]; then
364
+ if ! gh api -X POST "repos/$repo/issues/$number/comments" \
365
+ -F "body=@$body_file" --jq .html_url; then
366
+ echo "::error::Could not record the failure of check $key on issue #$number."
367
+ errors=$((errors + 1))
368
+ continue
369
+ fi
370
+ echo "Recorded the failure of check $key on open issue #$number."
371
+ else
372
+ echo "Open issue #$number already records this report's failure of check $key."
373
+ fi
374
+ if [[ "$assign" == true ]]; then
375
+ # Copilot can only be assigned with a user token; GitHub ignores an
376
+ # assignee it cannot assign, so check the issue now has one.
377
+ if ! assigned=$(GH_TOKEN="$COPILOT_ASSIGN_TOKEN" gh api -X POST \
378
+ "repos/$repo/issues/$number/assignees" \
379
+ -f "assignees[]=$COPILOT_LOGIN" --jq '.assignees | length') \
380
+ || (( assigned == 0 )); then
381
+ echo "::error::Issue #$number was not assigned to Copilot. COPILOT_ASSIGN_TOKEN must be a user token with Copilot coding agent access and write access to this repository's issues."
382
+ errors=$((errors + 1))
383
+ continue
384
+ fi
385
+ echo "Assigned issue #$number to Copilot."
386
+ fi
387
+ done 3< "$plan_dir/plan.tsv"
388
+ if (( errors )); then
389
+ exit 1
390
+ fi
391
+ - name: Fail on failed checks
392
+ if: always()
393
+ run: |
394
+ echo "::error::pc health reported failed checks; see the health issues and the health-report artifact."
395
+ exit 1
@@ -0,0 +1,75 @@
1
+ name: Deploy migrations
2
+
3
+ # Keeps the deployed schema PILOTO_AI.CLI at the latest Alembic revision: after
4
+ # CI passes on a push to main, it upgrades the schema to head at that commit. It
5
+ # never downgrades, because a downgrade drops tables that hold real data. CI's
6
+ # migration check runs on the scratch database PILOTO_AI_CLI_CI instead, so a
7
+ # migration reaches PILOTO_AI only after it upgraded, matched the models and
8
+ # downgraded cleanly there.
9
+ on:
10
+ workflow_run:
11
+ workflows: [CI]
12
+ types: [completed]
13
+ branches: [main]
14
+ workflow_dispatch:
15
+
16
+ permissions:
17
+ contents: read
18
+
19
+ concurrency:
20
+ group: snowflake-cli-deploy-migrations
21
+ cancel-in-progress: false
22
+
23
+ jobs:
24
+ deploy:
25
+ name: Upgrade PILOTO_AI.CLI to head
26
+ if: >-
27
+ vars.SNOWFLAKE_MIGRATIONS_ENABLED == 'true' &&
28
+ (github.event_name == 'workflow_dispatch' ||
29
+ (github.event.workflow_run.conclusion == 'success' &&
30
+ github.event.workflow_run.event == 'push'))
31
+ # SNOWFLAKE_MIGRATOR_PAT lives in this environment, whose deployment branch
32
+ # policy allows only main: this role owns the deployed tables, so a pull
33
+ # request or another branch must not read it.
34
+ environment: snowflake-migrations
35
+ runs-on: ubuntu-latest
36
+ timeout-minutes: 15
37
+ env:
38
+ # Alembic and the models are runtime dependencies; skip the dev group.
39
+ UV_NO_DEV: "1"
40
+ steps:
41
+ - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
42
+ with:
43
+ # The commit CI checked; a manual run deploys the current main.
44
+ ref: ${{ github.event.workflow_run.head_sha || github.sha }}
45
+ persist-credentials: false
46
+ - uses: actions/setup-python@6d4fd22b20465ccc0f7354476588e8df7c9ca0e0 # v7.1.0
47
+ with:
48
+ python-version: "3.15"
49
+ - name: Build Snowflake database URL
50
+ # Before any third-party code is installed. Fails at once, naming what
51
+ # is missing, when the deploy is enabled but not configured.
52
+ env:
53
+ SNOWFLAKE_PAT: ${{ secrets.SNOWFLAKE_MIGRATOR_PAT }}
54
+ SNOWFLAKE_ACCOUNT: ${{ vars.SNOWFLAKE_ACCOUNT }}
55
+ SNOWFLAKE_WAREHOUSE: ${{ vars.SNOWFLAKE_WAREHOUSE }}
56
+ SNOWFLAKE_DATABASE: PILOTO_AI
57
+ SNOWFLAKE_USER: PILOTO_AI_CLI_MIGRATOR_SVC
58
+ SNOWFLAKE_ROLE: PILOTO_AI_CLI_MIGRATOR
59
+ run: python -I src/piloto_ai_cli/snowflake_url.py
60
+ - uses: astral-sh/setup-uv@37802adc94f370d6bfd71619e3f0bf239e1f3b78 # v7.6.0
61
+ with:
62
+ enable-cache: true
63
+ - run: uv sync --locked
64
+ - name: Upgrade the cli schema to head
65
+ run: uv run --locked alembic upgrade head
66
+ - name: Verify the cli schema is at head and matches the models
67
+ run: |
68
+ set -euo pipefail
69
+ current=$(uv run --locked alembic current)
70
+ echo "$current"
71
+ if [[ "$current" != *"(head)"* ]]; then
72
+ echo "::error::PILOTO_AI.CLI is not at head after the upgrade: ${current:-no revision}" >&2
73
+ exit 1
74
+ fi
75
+ uv run --locked alembic check