-
Notifications
You must be signed in to change notification settings - Fork 1
feat(sync): lease generated delivery attempts #2895
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
cdbf0fb
feat(sync): lease generated delivery attempts
codex-automation d8f4503
fix(sync): refresh leased delivery attempts
codex-automation 046d83e
fix(sync): pass version hash through environment
codex-automation fd9d65b
fix(sync): quote tree-ish for actionlint and update Maint 71 contract…
codex-automation File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| 'use strict'; | ||
|
|
||
| const test = require('node:test'); | ||
| const assert = require('node:assert/strict'); | ||
| const { | ||
| DELIVERY_RECORD_SCHEMA, | ||
| formatDeliveryRecord, | ||
| parseDeliveryRecord, | ||
| mergeEligibility, | ||
| } = require('../sync_pr_lease_contract'); | ||
|
|
||
| const current = { | ||
| schema: DELIVERY_RECORD_SCHEMA, | ||
| durable_issue_url: 'https://github.com/stranske/Workflows/issues/1836', | ||
| plan_id: 'plan-abc', | ||
| generation: 'template-abc', | ||
| repository: 'stranske/Ready', | ||
| desired_tree_hash: 'tree-abc', | ||
| source_commit: 'source-abc', | ||
| lease_expires_at: '2026-08-02T00:00:00Z', | ||
| predecessor_prs: ['#10'], | ||
| successor_prs: [], | ||
| }; | ||
|
|
||
| test('an unexpired matching delivery record is merge eligible', () => { | ||
| const marker = formatDeliveryRecord(current); | ||
| const parsed = parseDeliveryRecord(`summary\n${marker}`); | ||
| assert.deepEqual(parsed, { ...current, terminal_disposition: '' }); | ||
| assert.deepEqual(mergeEligibility(parsed, { | ||
| now: '2026-08-01T22:00:00Z', | ||
| planId: 'plan-abc', | ||
| repository: 'stranske/Ready', | ||
| desiredTreeHash: 'tree-abc', | ||
| }), { eligible: true, reason: 'current_unexpired' }); | ||
| assert.equal(mergeEligibility({ ...parsed, lease_expires_at: '2026-08-01T21:00:00Z' }, { | ||
| now: '2026-08-01T22:00:00Z', | ||
| }).reason, 'lease_expired'); | ||
| assert.equal(mergeEligibility(parsed, { now: '2026-08-01T22:00:00Z', desiredTreeHash: 'other' }).reason, 'desired_tree_mismatch'); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| 'use strict'; | ||
|
|
||
| // A generated PR is a short-lived delivery attempt. The durable campaign issue | ||
| // retains coordination history; this marker lets the producer and merger agree | ||
| // on which attempt is current without treating an arbitrary open PR as current. | ||
| const DELIVERY_RECORD_SCHEMA = 'sync-pr-delivery-record/v1'; | ||
| const DELIVERY_RECORD_MARKER = 'sync-pr-delivery-record:v1'; | ||
|
|
||
| function clean(value) { | ||
| return String(value || '').trim(); | ||
| } | ||
|
|
||
| function unique(values) { | ||
| return [...new Set((values || []).map(clean).filter(Boolean))]; | ||
| } | ||
|
|
||
| function normalizeRecord(record = {}) { | ||
| const normalized = { | ||
| schema: clean(record.schema) || DELIVERY_RECORD_SCHEMA, | ||
| durable_issue_url: clean(record.durable_issue_url), | ||
| plan_id: clean(record.plan_id), | ||
| generation: clean(record.generation), | ||
| repository: clean(record.repository), | ||
| desired_tree_hash: clean(record.desired_tree_hash), | ||
| source_commit: clean(record.source_commit), | ||
| lease_expires_at: clean(record.lease_expires_at), | ||
| predecessor_prs: unique(record.predecessor_prs), | ||
| successor_prs: unique(record.successor_prs), | ||
| terminal_disposition: clean(record.terminal_disposition), | ||
| }; | ||
| return normalized; | ||
| } | ||
|
|
||
| function deliveryRecordErrors(record = {}) { | ||
| const normalized = normalizeRecord(record); | ||
| const required = [ | ||
| 'durable_issue_url', 'plan_id', 'generation', 'repository', | ||
| 'desired_tree_hash', 'source_commit', 'lease_expires_at', | ||
| ]; | ||
| const errors = []; | ||
| if (normalized.schema !== DELIVERY_RECORD_SCHEMA) errors.push('schema'); | ||
| for (const field of required) if (!normalized[field]) errors.push(field); | ||
| if (normalized.terminal_disposition && !['merged', 'superseded', 'expired', 'blocked'].includes(normalized.terminal_disposition)) { | ||
| errors.push('terminal_disposition'); | ||
| } | ||
| if (normalized.lease_expires_at && Number.isNaN(Date.parse(normalized.lease_expires_at))) { | ||
| errors.push('lease_expires_at'); | ||
| } | ||
| return errors; | ||
| } | ||
|
|
||
| function formatDeliveryRecord(record = {}) { | ||
| const normalized = normalizeRecord(record); | ||
| const errors = deliveryRecordErrors(normalized); | ||
| if (errors.length) throw new Error(`Invalid delivery record: ${errors.join(', ')}`); | ||
| return `<!-- ${DELIVERY_RECORD_MARKER} ${JSON.stringify(normalized)} -->`; | ||
| } | ||
|
|
||
| function parseDeliveryRecord(body = '') { | ||
| const match = String(body || '').match(new RegExp(`<!--\\s*${DELIVERY_RECORD_MARKER}\\s+([\\s\\S]*?)\\s*-->`)); | ||
| if (!match) return null; | ||
| try { | ||
| const record = normalizeRecord(JSON.parse(match[1])); | ||
| return deliveryRecordErrors(record).length ? null : record; | ||
| } catch (_) { | ||
| return null; | ||
| } | ||
| } | ||
|
|
||
| function mergeEligibility(record, { now = new Date().toISOString(), planId = '', repository = '', desiredTreeHash = '' } = {}) { | ||
| const normalized = normalizeRecord(record); | ||
| const errors = deliveryRecordErrors(normalized); | ||
| if (errors.length) return { eligible: false, reason: `invalid:${errors.join(',')}` }; | ||
| if (normalized.terminal_disposition) return { eligible: false, reason: `terminal:${normalized.terminal_disposition}` }; | ||
| if (Date.parse(normalized.lease_expires_at) <= Date.parse(now)) return { eligible: false, reason: 'lease_expired' }; | ||
| if (clean(planId) && normalized.plan_id !== clean(planId)) return { eligible: false, reason: 'plan_mismatch' }; | ||
| if (clean(repository) && normalized.repository !== clean(repository)) return { eligible: false, reason: 'repository_mismatch' }; | ||
| if (clean(desiredTreeHash) && normalized.desired_tree_hash !== clean(desiredTreeHash)) return { eligible: false, reason: 'desired_tree_mismatch' }; | ||
| return { eligible: true, reason: 'current_unexpired' }; | ||
| } | ||
|
|
||
| module.exports = { | ||
| DELIVERY_RECORD_SCHEMA, | ||
| DELIVERY_RECORD_MARKER, | ||
| normalizeRecord, | ||
| deliveryRecordErrors, | ||
| formatDeliveryRecord, | ||
| parseDeliveryRecord, | ||
| mergeEligibility, | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.