Skip to content

Commit 27fcdfa

Browse files
committed
feat(api): add prompt cache breakpoint TTL options
Castiron-Internal-PR: openai/openai-python-internal#309 Castiron-Source-SHA: a50ddcdd19dea4a94a7d044f4f0f987242bc99df Castiron-Public-Base-SHA: a3f4302
1 parent a3f4302 commit 27fcdfa

43 files changed

Lines changed: 444 additions & 195 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎.castiron.stats.yml‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
schema_version: 1
2-
generation_id: a993ea45-7f4f-4d48-9eea-27e2f709ca7c
3-
openapi_spec_hash: 1f2d498f08bdd6dacffeb41cee8f44e5
4-
openapi_transformed_spec_hash: 77075630a8655ccbb8d1f790f19f5731
5-
config_hash: 7a8181d81e1b9225f8ee6f09171d712e
6-
codegen_sha: 6f2038ca576a63929de017d9145a842c3a09beb1
7-
codegen_hash: a761b2892108918f3ad49d4929a6b756fa023b3192025bbc2bbcf618dea6c13a
8-
public_codegen_sha: 9fdcc5c60f9247fe11ec25aaa0f486033f738421
2+
generation_id: c495cfcb-e0b2-4857-ba45-da32a7eb08e1
3+
openapi_spec_hash: 2c78c708cab402e8904f555026c78020
4+
openapi_transformed_spec_hash: e513b495f24d8119b952521ffc783d86
5+
config_hash: 26002a7ad4f9fdd7011b62a2da3511b1
6+
codegen_sha: e2d124e8ab7eeed315a84374ed07bafe2bb725b8
7+
codegen_hash: 7895f6379f205b62d586adabb5d30717ed20d34cdf3a0ac59fe47425c879f208
8+
public_codegen_sha: 5210b08536d757d28a0876cc593fc22e53d14ee7

‎.github/workflows/castiron-custom-code-comment.yml‎

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,97 @@ jobs:
344344
return;
345345
}
346346
347+
main-change:
348+
name: Invalidate next queue after main advances
349+
if: github.event.workflow_run.event == 'push' && github.event.workflow_run.head_branch == 'main'
350+
runs-on: ubuntu-latest
351+
timeout-minutes: 5
352+
concurrency:
353+
group: castiron-budget-writes-${{ github.repository_id }}
354+
cancel-in-progress: false
355+
queue: max
356+
permissions:
357+
actions: write
358+
contents: read
359+
pull-requests: read
360+
statuses: write
361+
steps:
362+
- name: Re-evaluate live next queue after main advances
363+
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0
364+
with:
365+
script: |
366+
// Main pushes only invalidate. The normal authenticated evaluator
367+
// and serialized publisher are the only paths that restore success.
368+
const repo = `${context.repo.owner}/${context.repo.repo}`;
369+
const signal = context.payload.workflow_run;
370+
const {data: run} = await github.rest.actions.getWorkflowRun({...context.repo, run_id: signal.id});
371+
const {data: workflow} = await github.rest.actions.getWorkflow({
372+
...context.repo, workflow_id: 'castiron-custom-code.yml',
373+
});
374+
if (!Number.isSafeInteger(run.workflow_id) || run.workflow_id !== workflow.id ||
375+
workflow.path !== '.github/workflows/castiron-custom-code.yml') return;
376+
if (run.repository.full_name !== repo || run.head_repository?.full_name !== repo ||
377+
run.event !== 'push' || run.head_branch !== 'main' || run.status !== 'completed' ||
378+
run.id !== signal.id || run.run_attempt !== signal.run_attempt || run.head_sha !== signal.head_sha ||
379+
!/^[0-9a-f]{40}$/.test(run.head_sha) ||
380+
run.path.split('@', 1)[0] !== '.github/workflows/castiron-custom-code.yml') {
381+
throw new Error('Unexpected or superseded main signal');
382+
}
383+
const {data: main} = await github.rest.git.getRef({...context.repo, ref: 'heads/main'});
384+
// A newer main push has its own signal. An old retry must not churn
385+
// candidates already qualified against that newer snapshot.
386+
if (run.head_sha !== main.object.sha) return;
387+
const targets = new Set();
388+
let cursor = null;
389+
do {
390+
const result = await github.graphql(`query($owner:String!,$name:String!,$branch:String!,$cursor:String) {
391+
repository(owner:$owner,name:$name) { mergeQueue(branch:$branch) {
392+
entries(first:100,after:$cursor) {
393+
nodes { headCommit { oid } }
394+
pageInfo { hasNextPage endCursor }
395+
}
396+
} }
397+
}`, {owner: context.repo.owner, name: context.repo.repo, branch: 'next', cursor});
398+
const page = result.repository.mergeQueue?.entries;
399+
if (!page) break;
400+
for (const entry of page.nodes) {
401+
if (entry.headCommit === null) continue; // Unprepared entries have no statuses to invalidate.
402+
const sha = entry.headCommit?.oid;
403+
if (!/^[0-9a-f]{40}$/.test(sha || '')) throw new Error('Invalid live queue SHA');
404+
targets.add(sha);
405+
}
406+
if (!page.pageInfo.hasNextPage) break;
407+
const next = page.pageInfo.endCursor;
408+
if (!next || next === cursor) throw new Error('Invalid queue pagination');
409+
cursor = next;
410+
} while (true);
411+
const url = `${context.serverUrl}/${repo}/actions/runs/${context.runId}`;
412+
// Invalidate the entire live next queue before slower rerun discovery.
413+
// This also covers bootstraps whose policy pin is now stale. Ordinary
414+
// pinned-policy candidates can regain success through normal checks.
415+
for (const sha of targets) {
416+
for (const name of ['Castiron next / budget-only change', 'Castiron next / custom-code budget']) {
417+
await github.rest.repos.createCommitStatus({...context.repo, sha, context: name, state: 'pending',
418+
description: `Main advanced; reevaluation pending. [main ${main.object.sha}]`, target_url: url});
419+
}
420+
}
421+
for (const sha of targets) {
422+
const runs = await github.paginate(github.rest.actions.listWorkflowRuns, {
423+
...context.repo, workflow_id: 'castiron-custom-code.yml', head_sha: sha, event: 'merge_group', per_page: 100,
424+
});
425+
const matches = runs.filter(item => item.repository.full_name === repo &&
426+
item.workflow_id === workflow.id && item.event === 'merge_group' && item.head_sha === sha &&
427+
typeof item.head_branch === 'string' && item.head_branch.startsWith('gh-readonly-queue/next/') &&
428+
item.path.split('@', 1)[0] === '.github/workflows/castiron-custom-code.yml' &&
429+
Number.isSafeInteger(item.id) && item.id > 0);
430+
matches.sort((a, b) => b.id - a.id);
431+
// A running evaluation will check main at publication. Do not
432+
// restart an older completed run when a newer one is in flight.
433+
if (matches[0]?.status === 'completed') {
434+
await github.rest.actions.reRunWorkflow({...context.repo, run_id: matches[0].id});
435+
}
436+
}
437+
347438
compute:
348439
name: Compute trusted custom-code report
349440
if: contains(fromJSON('["pull_request", "merge_group"]'), github.event.workflow_run.event) && (github.event.workflow_run.path == '.github/workflows/castiron-custom-code.yml' || startsWith(github.event.workflow_run.path, '.github/workflows/castiron-custom-code.yml@'))

‎.github/workflows/castiron-custom-code.yml‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
name: Castiron custom code
33

44
on:
5+
push:
6+
branches: [main]
57
pull_request:
68
types: [opened, synchronize, reopened, ready_for_review, edited]
79
pull_request_review:
@@ -22,7 +24,7 @@ env:
2224

2325
jobs:
2426
queue-signal:
25-
if: github.event_name == 'merge_group' || github.event_name == 'pull_request_review'
27+
if: github.event_name == 'merge_group' || github.event_name == 'pull_request_review' || github.event_name == 'push'
2628
runs-on: ubuntu-latest
2729
timeout-minutes: 1
2830
permissions: {}

‎api_reference/openapi.transformed.yml‎

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74734,7 +74734,8 @@ components:
7473474734
required:
7473574735
- mode
7473674736
title: Prompt cache breakpoint
74737-
description: Marks the exact end of a reusable prompt prefix. The breakpoint inherits its TTL from the request's `prompt_cache_options.ttl`; the boundary is not rounded to a token block.
74737+
description: Marks the exact end of a reusable prompt prefix. When `ttl` is omitted, the breakpoint inherits the request's `prompt_cache_options.ttl`; the boundary is not rounded to a token block.
74738+
x-stainless-go-constant-constructor: true
7473874739
ModerationMode:
7473974740
type: string
7474074741
enum:
@@ -74964,7 +74965,8 @@ components:
7496474965
required:
7496574966
- mode
7496674967
title: Prompt cache breakpoint
74967-
description: Marks the exact end of a reusable prompt prefix. The breakpoint inherits its TTL from the request's `prompt_cache_options.ttl`; the boundary is not rounded to a token block.
74968+
description: Marks the exact end of a reusable prompt prefix. When `ttl` is omitted, the breakpoint inherits the request's `prompt_cache_options.ttl`; the boundary is not rounded to a token block.
74969+
x-stainless-go-constant-constructor: true
7496874970
InputTextContent:
7496974971
properties:
7497074972
type:
@@ -74979,6 +74981,7 @@ components:
7497974981
description: The text input to the model.
7498074982
prompt_cache_breakpoint:
7498174983
$ref: '#/components/schemas/PromptCacheBreakpointConfig'
74984+
x-stainless-go-constant-constructor: true
7498274985
type: object
7498374986
required:
7498474987
- type
@@ -75254,6 +75257,7 @@ components:
7525475257
description: The detail level of the image to be sent to the model. One of `high`, `low`, `auto`, or `original`. Defaults to `auto`.
7525575258
prompt_cache_breakpoint:
7525675259
$ref: '#/components/schemas/PromptCacheBreakpointConfig'
75260+
x-stainless-go-constant-constructor: true
7525775261
type: object
7525875262
required:
7525975263
- type
@@ -75285,6 +75289,7 @@ components:
7528575289
description: The detail level of the screenshot image to be sent to the model. One of `high`, `low`, `auto`, or `original`. Defaults to `auto`.
7528675290
prompt_cache_breakpoint:
7528775291
$ref: '#/components/schemas/PromptCacheBreakpointConfig'
75292+
x-stainless-go-constant-constructor: true
7528875293
type: object
7528975294
required:
7529075295
- type
@@ -75322,6 +75327,7 @@ components:
7532275327
The content of the file to be sent to the model.
7532375328
prompt_cache_breakpoint:
7532475329
$ref: '#/components/schemas/PromptCacheBreakpointConfig'
75330+
x-stainless-go-constant-constructor: true
7532575331
file_url:
7532675332
type: string
7532775333
format: uri
@@ -77314,6 +77320,7 @@ components:
7731477320
anyOf:
7731577321
- $ref: '#/components/schemas/PromptCacheBreakpointParam'
7731677322
- type: 'null'
77323+
x-stainless-go-constant-constructor: true
7731777324
type: object
7731877325
required:
7731977326
- type
@@ -77351,6 +77358,7 @@ components:
7735177358
anyOf:
7735277359
- $ref: '#/components/schemas/PromptCacheBreakpointParam'
7735377360
- type: 'null'
77361+
x-stainless-go-constant-constructor: true
7735477362
type: object
7735577363
required:
7735677364
- type
@@ -77401,6 +77409,7 @@ components:
7740177409
anyOf:
7740277410
- $ref: '#/components/schemas/PromptCacheBreakpointParam'
7740377411
- type: 'null'
77412+
x-stainless-go-constant-constructor: true
7740477413
type: object
7740577414
required:
7740677415
- type
@@ -99861,6 +99870,7 @@ components:
9986199870
The content of the file to be sent to the model.
9986299871
prompt_cache_breakpoint:
9986399872
$ref: '#/components/schemas/BetaPromptCacheBreakpointConfig'
99873+
x-stainless-go-constant-constructor: true
9986499874
file_url:
9986599875
type: string
9986699876
format: uri
@@ -99898,7 +99908,8 @@ components:
9989899908
required:
9989999909
- mode
9990099910
title: Prompt cache breakpoint
99901-
description: Marks the exact end of a reusable prompt prefix. The breakpoint inherits its TTL from the request's `prompt_cache_options.ttl`; the boundary is not rounded to a token block.
99911+
description: Marks the exact end of a reusable prompt prefix. When `ttl` is omitted, the breakpoint inherits the request's `prompt_cache_options.ttl`; the boundary is not rounded to a token block.
99912+
x-stainless-go-constant-constructor: true
9990299913
BetaInputImageContent:
9990399914
properties:
9990499915
type:
@@ -99924,6 +99935,7 @@ components:
9992499935
description: The detail level of the image to be sent to the model. One of `high`, `low`, `auto`, or `original`. Defaults to `auto`.
9992599936
prompt_cache_breakpoint:
9992699937
$ref: '#/components/schemas/BetaPromptCacheBreakpointConfig'
99938+
x-stainless-go-constant-constructor: true
9992799939
type: object
9992899940
required:
9992999941
- type
@@ -99951,6 +99963,7 @@ components:
9995199963
description: The text input to the model.
9995299964
prompt_cache_breakpoint:
9995399965
$ref: '#/components/schemas/BetaPromptCacheBreakpointConfig'
99966+
x-stainless-go-constant-constructor: true
9995499967
type: object
9995599968
required:
9995699969
- type
@@ -101731,6 +101744,7 @@ components:
101731101744
anyOf:
101732101745
- $ref: '#/components/schemas/BetaPromptCacheBreakpointParam'
101733101746
- type: 'null'
101747+
x-stainless-go-constant-constructor: true
101734101748
type: object
101735101749
required:
101736101750
- type
@@ -101755,7 +101769,8 @@ components:
101755101769
required:
101756101770
- mode
101757101771
title: Prompt cache breakpoint
101758-
description: Marks the exact end of a reusable prompt prefix. The breakpoint inherits its TTL from the request's `prompt_cache_options.ttl`; the boundary is not rounded to a token block.
101772+
description: Marks the exact end of a reusable prompt prefix. When `ttl` is omitted, the breakpoint inherits the request's `prompt_cache_options.ttl`; the boundary is not rounded to a token block.
101773+
x-stainless-go-constant-constructor: true
101759101774
BetaDetailEnum:
101760101775
type: string
101761101776
enum:
@@ -101780,6 +101795,7 @@ components:
101780101795
anyOf:
101781101796
- $ref: '#/components/schemas/BetaPromptCacheBreakpointParam'
101782101797
- type: 'null'
101798+
x-stainless-go-constant-constructor: true
101783101799
type: object
101784101800
required:
101785101801
- type
@@ -101899,6 +101915,7 @@ components:
101899101915
anyOf:
101900101916
- $ref: '#/components/schemas/BetaPromptCacheBreakpointParam'
101901101917
- type: 'null'
101918+
x-stainless-go-constant-constructor: true
101902101919
type: object
101903101920
required:
101904101921
- type
@@ -104224,6 +104241,7 @@ components:
104224104241
description: The detail level of the screenshot image to be sent to the model. One of `high`, `low`, `auto`, or `original`. Defaults to `auto`.
104225104242
prompt_cache_breakpoint:
104226104243
$ref: '#/components/schemas/BetaPromptCacheBreakpointConfig'
104244+
x-stainless-go-constant-constructor: true
104227104245
type: object
104228104246
required:
104229104247
- type

‎scripts/castiron/custom_code_budget.py‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -737,11 +737,17 @@ def prepare_main_snapshot(
737737
"""Create and verify an adoption commit without altering a ref, index or metadata."""
738738
base, authenticated_main = map(report.require_sha, (base, authenticated_main))
739739
strict = {"schema_version": 2, "max_custom_patch_lines": 0, "generated_files": "strict"}
740-
if read_policy(repo, authenticated_main) != strict or "next_policy" in read_policy(repo, base):
740+
main_policy = read_policy(repo, authenticated_main)
741+
if {
742+
key: value for key, value in main_policy.items() if key != "ratchet"
743+
} != strict or "next_policy" in read_policy(repo, base):
741744
raise ValueError("main snapshot preparation requires strict-zero main and unpinned next")
742745
if policy.merge_base(repo, base, authenticated_main) != base:
743746
raise ValueError("protected next must be an ancestor of authenticated main")
744-
value = {**strict, "next_policy": {"repository": repository, "main_sha": authenticated_main}}
747+
value = {
748+
**main_policy,
749+
"next_policy": {"repository": repository, "main_sha": authenticated_main},
750+
}
745751
blob = report.git(
746752
repo,
747753
"hash-object",

‎scripts/castiron/custom_code_policy.py‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -312,10 +312,11 @@ def is_main_snapshot_bootstrap(
312312
if "next_policy" in read_policy(repo, base):
313313
return False
314314
strict = {"schema_version": 2, "max_custom_patch_lines": 0, "generated_files": "strict"}
315-
if read_policy(repo, authenticated_main) != strict:
315+
main_policy = read_policy(repo, authenticated_main)
316+
if {key: value for key, value in main_policy.items() if key != "ratchet"} != strict:
316317
return False
317318
expected_policy = {
318-
**strict,
319+
**main_policy,
319320
"next_policy": {"repository": repository, "main_sha": authenticated_main},
320321
}
321322
if read_policy(repo, head) != expected_policy:

0 commit comments

Comments
 (0)