@@ -49,9 +49,15 @@ export type DirectPublicationOperation = {
4949} ;
5050
5151export type DirectPublicationPlan = {
52- itemKey : string ;
52+ canonicalTargetKey : string ;
53+ fenceKey : string ;
5354 revision : number ;
54- identity : { itemKey : string ; revision : number ; claimGeneration : number } ;
55+ identity : {
56+ canonicalTargetKey : string ;
57+ fenceKey : string ;
58+ revision : number ;
59+ claimGeneration : number ;
60+ } ;
5561 operations : DirectPublicationOperation [ ] ;
5662 totalBytes : number ;
5763} ;
@@ -178,6 +184,8 @@ export class ExactReviewDirectPublicationStore {
178184 this . storage . sql . exec (
179185 `CREATE TABLE IF NOT EXISTS ${ EXACT_REVIEW_DIRECT_PUBLICATION_TABLE } (
180186 item_key TEXT NOT NULL,
187+ canonical_target_key TEXT NOT NULL DEFAULT '',
188+ fence_key TEXT NOT NULL DEFAULT '',
181189 revision INTEGER NOT NULL CHECK (revision >= 1),
182190 identity_item_key TEXT NOT NULL,
183191 identity_revision INTEGER NOT NULL CHECK (identity_revision >= 1),
@@ -197,6 +205,42 @@ export class ExactReviewDirectPublicationStore {
197205 PRIMARY KEY (item_key, revision)
198206 ) STRICT` ,
199207 ) ;
208+ const directPublicationColumns = new Set (
209+ Array . from (
210+ this . storage . sql . exec (
211+ `SELECT name FROM pragma_table_info('${ EXACT_REVIEW_DIRECT_PUBLICATION_TABLE } ')` ,
212+ ) ,
213+ ) . map ( ( row ) => String ( row . name || "" ) ) ,
214+ ) ;
215+ if ( ! directPublicationColumns . has ( "canonical_target_key" ) ) {
216+ this . storage . sql . exec (
217+ `ALTER TABLE ${ EXACT_REVIEW_DIRECT_PUBLICATION_TABLE }
218+ ADD COLUMN canonical_target_key TEXT NOT NULL DEFAULT ''` ,
219+ ) ;
220+ }
221+ if ( ! directPublicationColumns . has ( "fence_key" ) ) {
222+ this . storage . sql . exec (
223+ `ALTER TABLE ${ EXACT_REVIEW_DIRECT_PUBLICATION_TABLE }
224+ ADD COLUMN fence_key TEXT NOT NULL DEFAULT ''` ,
225+ ) ;
226+ }
227+ // Every pre-envelope receipt used its sole item key for both roles. Keep
228+ // those completed receipts dedupe-compatible while all new writes preserve
229+ // the explicit dual identity below.
230+ this . storage . sql . exec (
231+ `UPDATE ${ EXACT_REVIEW_DIRECT_PUBLICATION_TABLE }
232+ SET canonical_target_key = item_key
233+ WHERE canonical_target_key = ''` ,
234+ ) ;
235+ this . storage . sql . exec (
236+ `UPDATE ${ EXACT_REVIEW_DIRECT_PUBLICATION_TABLE }
237+ SET fence_key = item_key
238+ WHERE fence_key = ''` ,
239+ ) ;
240+ this . storage . sql . exec (
241+ `CREATE INDEX IF NOT EXISTS exact_review_direct_publication_target_revision
242+ ON ${ EXACT_REVIEW_DIRECT_PUBLICATION_TABLE } (canonical_target_key, revision)` ,
243+ ) ;
200244 this . storage . sql . exec (
201245 `CREATE TABLE IF NOT EXISTS ${ CANONICAL_RECORD_TUPLE_RECEIPT_TABLE } (
202246 delivery_id TEXT PRIMARY KEY,
@@ -317,29 +361,34 @@ export class ExactReviewDirectPublicationStore {
317361 const canonicalOperations = canonicalTupleOperations ( plan ) ;
318362 return this . storage . transactionSync ( ( ) => {
319363 this . pruneTerminalSync ( now ) ;
320- const existing = this . readSync ( plan . itemKey , plan . revision ) ;
364+ const existing = this . readSync ( plan . fenceKey , plan . revision ) ;
321365 if ( existing ) {
322366 if ( [ "pending" , "committing" , "retryable" ] . includes ( existing . state ) ) {
323367 this . storage . sql . exec (
324368 `DELETE FROM ${ EXACT_REVIEW_DIRECT_PUBLICATION_TABLE }
325369 WHERE item_key = ? AND revision = ?` ,
326- plan . itemKey ,
370+ plan . fenceKey ,
327371 plan . revision ,
328372 ) ;
329373 } else {
330374 if ( canonicalStoredPlan ( existing ) !== canonicalIncomingPlan ( plan , storedOperations ) ) {
331375 throw new Error ( "conflicting direct publication retry" ) ;
332376 }
333- return { outcome : "deduped" as const , row : existing , supersededRevisions : [ ] } ;
377+ return {
378+ outcome :
379+ existing . state === "superseded" ? ( "superseded" as const ) : ( "deduped" as const ) ,
380+ row : existing ,
381+ supersededRevisions : [ ] ,
382+ } ;
334383 }
335384 }
336385
337386 const newer = Array . from (
338387 this . storage . sql . exec (
339388 `SELECT revision FROM ${ EXACT_REVIEW_DIRECT_PUBLICATION_TABLE }
340- WHERE item_key = ? AND revision > ?
389+ WHERE fence_key = ? AND revision > ?
341390 ORDER BY revision DESC LIMIT 1` ,
342- plan . itemKey ,
391+ plan . fenceKey ,
343392 plan . revision ,
344393 ) ,
345394 ) [ 0 ] ;
@@ -429,9 +478,15 @@ export class ExactReviewDirectPublicationStore {
429478
430479 const revision = this . nextTupleRevisionSync ( mutation . repoSlug , mutation . itemId ) ;
431480 const plan : CanonicalDirectPublicationPlan = {
432- itemKey : mutation . key ,
481+ canonicalTargetKey : mutation . key ,
482+ fenceKey : mutation . key ,
433483 revision,
434- identity : { itemKey : mutation . key , revision, claimGeneration : 1 } ,
484+ identity : {
485+ canonicalTargetKey : mutation . key ,
486+ fenceKey : mutation . key ,
487+ revision,
488+ claimGeneration : 1 ,
489+ } ,
435490 operations : mutation . operations ,
436491 totalBytes : mutation . operations . reduce ( ( sum , operation ) => sum + operation . bytes , 0 ) ,
437492 } ;
@@ -961,7 +1016,7 @@ export class ExactReviewDirectPublicationStore {
9611016 chunks . length ,
9621017 operation . content === null ? 1 : 0 ,
9631018 revision ,
964- plan . itemKey ,
1019+ plan . canonicalTargetKey ,
9651020 plan . identity . claimGeneration ,
9661021 now ,
9671022 ) ;
@@ -1192,13 +1247,16 @@ export class ExactReviewDirectPublicationStore {
11921247 private insertSync ( row : DirectPublicationRow ) {
11931248 this . storage . sql . exec (
11941249 `INSERT INTO ${ EXACT_REVIEW_DIRECT_PUBLICATION_TABLE }
1195- (item_key, revision, identity_item_key, identity_revision, claim_generation,
1250+ (item_key, canonical_target_key, fence_key, revision,
1251+ identity_item_key, identity_revision, claim_generation,
11961252 operations_json, total_bytes, file_count, state, attempts, created_at, updated_at,
11971253 next_attempt_at, commit_sha, failure_reason)
1198- VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)` ,
1199- row . itemKey ,
1254+ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)` ,
1255+ row . fenceKey ,
1256+ row . canonicalTargetKey ,
1257+ row . fenceKey ,
12001258 row . revision ,
1201- row . identity . itemKey ,
1259+ row . identity . fenceKey ,
12021260 row . identity . revision ,
12031261 row . identity . claimGeneration ,
12041262 JSON . stringify ( row . operations ) ,
@@ -1225,7 +1283,9 @@ function canonicalTupleOperations(
12251283 operation . content !== null ,
12261284 ) ;
12271285 if ( primaryWrites . length > 1 ) {
1228- throw new Error ( `direct publication tuple writes both primary sections: ${ plan . itemKey } ` ) ;
1286+ throw new Error (
1287+ `direct publication tuple writes both primary sections: ${ plan . canonicalTargetKey } ` ,
1288+ ) ;
12291289 }
12301290 const primaryWrite = primaryWrites [ 0 ] ;
12311291 const first = operations [ 0 ] ! ;
@@ -1260,7 +1320,7 @@ function canonicalTupleOperations(
12601320 addDelete ( "decision-packets" ) ;
12611321 }
12621322 if ( operations . length > EXACT_REVIEW_DIRECT_PUBLICATION_MAX_FILES ) {
1263- throw new Error ( `canonical record tuple exceeds its file limit: ${ plan . itemKey } ` ) ;
1323+ throw new Error ( `canonical record tuple exceeds its file limit: ${ plan . canonicalTargetKey } ` ) ;
12641324 }
12651325 return operations ;
12661326}
@@ -1465,16 +1525,19 @@ export async function validateDirectPublicationPlan(
14651525 value : DirectPublicationPlan ,
14661526) : Promise < CanonicalDirectPublicationPlan > {
14671527 const plan = value && typeof value === "object" ? value : ( { } as DirectPublicationPlan ) ;
1468- const itemKey = boundedItemKey ( plan . itemKey ) ;
1469- const itemIdentity = / ^ ( [ A - Z a - z 0 - 9 _ . - ] + ) \/ ( [ A - Z a - z 0 - 9 _ . - ] + ) # ( [ 1 - 9 ] \d * ) $ / . exec ( itemKey ) ;
1470- if ( ! itemIdentity ) throw new Error ( "invalid direct publication item key" ) ;
1528+ const canonicalTargetKey = boundedItemKey ( plan . canonicalTargetKey ) ;
1529+ const itemIdentity = / ^ ( [ A - Z a - z 0 - 9 _ . - ] + ) \/ ( [ A - Z a - z 0 - 9 _ . - ] + ) # ( [ 1 - 9 ] \d * ) $ / . exec ( canonicalTargetKey ) ;
1530+ if ( ! itemIdentity ) throw new Error ( "invalid direct publication canonical target key" ) ;
1531+ const fenceKey = boundedItemKey ( plan . fenceKey ) ;
1532+ if ( ! fenceKey ) throw new Error ( "invalid direct publication fence key" ) ;
14711533 if ( ! Number . isSafeInteger ( plan . revision ) || plan . revision < 1 ) {
14721534 throw new Error ( "invalid direct publication revision" ) ;
14731535 }
14741536 const identity = plan . identity ;
14751537 if (
14761538 ! identity ||
1477- boundedItemKey ( identity . itemKey ) !== itemKey ||
1539+ boundedItemKey ( identity . canonicalTargetKey ) !== canonicalTargetKey ||
1540+ boundedItemKey ( identity . fenceKey ) !== fenceKey ||
14781541 identity . revision !== plan . revision ||
14791542 ! Number . isSafeInteger ( identity . claimGeneration ) ||
14801543 identity . claimGeneration < 1
@@ -1546,9 +1609,15 @@ export async function validateDirectPublicationPlan(
15461609 throw new Error ( "direct publication plan exceeds the per-POST byte limit" ) ;
15471610 }
15481611 return {
1549- itemKey,
1612+ canonicalTargetKey,
1613+ fenceKey,
15501614 revision : plan . revision ,
1551- identity : { itemKey, revision : plan . revision , claimGeneration : identity . claimGeneration } ,
1615+ identity : {
1616+ canonicalTargetKey,
1617+ fenceKey,
1618+ revision : plan . revision ,
1619+ claimGeneration : identity . claimGeneration ,
1620+ } ,
15521621 operations,
15531622 totalBytes,
15541623 } ;
@@ -1574,7 +1643,8 @@ function directPublicationRowFromPlan(options: {
15741643 failureReason : string | null ;
15751644} ) : DirectPublicationRow {
15761645 return {
1577- itemKey : options . plan . itemKey ,
1646+ canonicalTargetKey : options . plan . canonicalTargetKey ,
1647+ fenceKey : options . plan . fenceKey ,
15781648 revision : options . plan . revision ,
15791649 identity : options . plan . identity ,
15801650 operations : options . operations ,
@@ -1591,10 +1661,12 @@ function directPublicationRowFromPlan(options: {
15911661
15921662function directPublicationRow ( row : Record < string , unknown > ) : DirectPublicationRow {
15931663 return {
1594- itemKey : String ( row . item_key ) ,
1664+ canonicalTargetKey : String ( row . canonical_target_key ) ,
1665+ fenceKey : String ( row . fence_key ) ,
15951666 revision : Number ( row . revision ) ,
15961667 identity : {
1597- itemKey : String ( row . identity_item_key ) ,
1668+ canonicalTargetKey : String ( row . canonical_target_key ) ,
1669+ fenceKey : String ( row . fence_key ) ,
15981670 revision : Number ( row . identity_revision ) ,
15991671 claimGeneration : Number ( row . claim_generation ) ,
16001672 } ,
@@ -1615,24 +1687,37 @@ function canonicalIncomingPlan(
16151687 operations : DirectPublicationStoredOperation [ ] ,
16161688) {
16171689 return JSON . stringify ( {
1618- itemKey : plan . itemKey ,
1690+ canonicalTargetKey : plan . canonicalTargetKey ,
1691+ fenceKey : plan . fenceKey ,
16191692 revision : plan . revision ,
1620- identity : plan . identity ,
1693+ // The claim generation fences completion ownership, not the immutable
1694+ // state tuple. A reclaimed batch must be able to dedupe an accepted tuple
1695+ // after its prior worker died before completion.
1696+ identity : stablePublicationIdentity ( plan . identity ) ,
16211697 operations,
16221698 totalBytes : plan . totalBytes ,
16231699 } ) ;
16241700}
16251701
16261702function canonicalStoredPlan ( plan : DirectPublicationRow ) {
16271703 return JSON . stringify ( {
1628- itemKey : plan . itemKey ,
1704+ canonicalTargetKey : plan . canonicalTargetKey ,
1705+ fenceKey : plan . fenceKey ,
16291706 revision : plan . revision ,
1630- identity : plan . identity ,
1707+ identity : stablePublicationIdentity ( plan . identity ) ,
16311708 operations : plan . operations ,
16321709 totalBytes : plan . totalBytes ,
16331710 } ) ;
16341711}
16351712
1713+ function stablePublicationIdentity ( identity : DirectPublicationPlan [ "identity" ] ) {
1714+ return {
1715+ canonicalTargetKey : identity . canonicalTargetKey ,
1716+ fenceKey : identity . fenceKey ,
1717+ revision : identity . revision ,
1718+ } ;
1719+ }
1720+
16361721function canonicalTuplePath ( path : string ) {
16371722 const match =
16381723 / ^ r e c o r d s \/ ( [ A - Z a - z 0 - 9 ] [ A - Z a - z 0 - 9 _ . - ] { 0 , 199 } ) \/ ( i t e m s | c l o s e d | p l a n s | d e c i s i o n - p a c k e t s ) \/ ( [ 1 - 9 ] \d * ) \. ( m d | j s o n ) $ / . exec (
@@ -1645,8 +1730,10 @@ function canonicalTuplePath(path: string) {
16451730}
16461731
16471732function boundedItemKey ( value : unknown ) {
1648- const text = String ( value || "" ) . trim ( ) ;
1649- return text && text . length <= 500 && ! text . includes ( "\0" ) && ! / [ \r \n ] / . test ( text ) ? text : "" ;
1733+ if ( typeof value !== "string" || value !== value . trim ( ) ) return "" ;
1734+ return value && value . length <= 500 && ! value . includes ( "\0" ) && ! / [ \r \n ] / . test ( value )
1735+ ? value
1736+ : "" ;
16501737}
16511738
16521739function canonicalPath ( value : unknown ) {
0 commit comments