Skip to content
Source
Sign inSign up
TanStack/schemas
Open History to see the last commit

probe.ts

89 lines3.4 KB

1// Weekly live probes (issue #717). Runs `pnpm run probe -- <provider>
2// --write` for each chat probe provider, and opens a pull request on
3// `probe/weekly` when a record file changed. A person reviews and merges
4// it; nothing here merges. No change, no pull request.
5//
6// Each provider's key is an optional secret: with none, the probe script
7// prints `skip <provider>: <KEY> is not set` and the run goes on. The
8// script caps its own requests (MAX_PROBE_REQUESTS) and never prints a key.
9//
10// Admin setup (people only), one per provider below:
11// src secret set GROQ_API_KEY --env development --host api.groq.com --header Authorization < groq-key.txt
12//
13// Opt a provider in: add it to `chatProbes` in
14// src/server/providers/probes/index.ts, then add its id to PROVIDERS, its
15// key to `secrets` and its API host to `egress` here.
16import {
17 defineWorkflow,
18 forge,
19 manual,
20 schedule,
21 setup,
22} from '@tanstack-source/ci'
23​
24const PROVIDERS = ['groq']
25const BRANCH = 'probe/weekly'
26const RECORDS = 'src/server/providers/probes/records'
27​
28export default defineWorkflow('probe', {
29 // Mondays 03:17 UTC.
30 on: [schedule('17 3 * * 1'), manual()],
31}).job('probe', {
32 secrets: { GROQ_API_KEY: { optional: true } },
33 egress: ['registry.npmjs.org', 'api.groq.com'],
34 permissions: { push: [BRANCH], pulls: 'create' },
35 timeoutMinutes: 15,
36 steps: [
37 setup.node(),
38 setup.pnpm(),
39 { name: 'Install', run: 'pnpm install --frozen-lockfile' },
40 {
41 name: 'Probe',
42 do: async (ctx) => {
43 for (const provider of PROVIDERS) {
44 await ctx.sh`pnpm run probe -- ${provider} --write --skip-missing-key`
45 }
46 },
47 },
48 {
49 name: 'Open the pull request',
50 do: async (ctx) => {
51 // Only main feeds the weekly pull request: a manual run on another
52 // ref would push that ref's commits into it.
53 const ref = ctx.env.TANSTACK_SOURCE_REF ?? ''
54 if (ref !== 'main' && ref !== 'refs/heads/main') {
55 ctx.summary(
56 `Run on ${ref || 'an unknown ref'}, not main: records left unpushed, no pull request.`,
57 )
58 return
59 }
60 // A git failure fails the job; it never reads as "no change".
61 const status = await ctx.sh
62 .nothrow`git status --porcelain -- ${RECORDS}`
63 if (status.code !== 0) {
64 throw new Error(`git status failed: ${status.stderr}`)
65 }
66 if (status.stdout.trim() === '') {
67 ctx.summary('No probe record changed: no pull request.')
68 return
69 }
70 await ctx.sh`git switch -C ${BRANCH}`
71 await ctx.sh`git add -- ${RECORDS}`
72 await ctx.sh`git -c user.name=modelschemas-probe -c user.email=probe@modelschemas.invalid commit -m ${'Refresh probe records'}`
73 await ctx.sh`git push --force origin ${`HEAD:refs/heads/${BRANCH}`}`
74 const client = forge(ctx)
75 const [open] = await client.pulls.list({ head: BRANCH, state: 'open' })
76 if (open) {
77 ctx.summary(`Updated the open probe pull request #${open.number}.`)
78 return
79 }
80 await client.pulls.create({
81 title: 'Refresh probe records',
82 head: BRANCH,
83 base: 'main',
84 body: 'The weekly probe run (.source/workflows/probe.ts) changed a record under src/server/providers/probes/records/. Each record holds the status and error message that stated its value. Review the evidence, then merge.',
85 })
86 },
87 },
88 ],
89})