@@ -36,6 +36,8 @@ export const BACKFILL_CAP = 200
3636const LOCK_OPTIONS = { timeoutMs : 5_000 }
3737/** Errors the server reports when a proposal's base moved: refresh, compare, and hold instead of resending. */
3838const CONFLICTS = new Set ( [ "version_conflict" , "open_proposal_exists" , "bad_target" , "stale_target" ] )
39+ /** Statuses answering a request the server refused as invalid: request validation, wire limits, body size. */
40+ const REFUSED_REQUEST = new Set ( [ 400 , 413 , 422 ] )
3941/** Item errors that end a proposal: the server will never accept it as is. */
4042const FINAL = new Set ( [ "invalid" , "no_change" ] )
4143
@@ -153,7 +155,8 @@ export function pull(root: string, store: string, ctx: Extract<Context, { status
153155async function pullOnce ( root : string , store : string , ctx : Extract < Context , { status : "ready" } > ) : Promise < PullOutcome > {
154156 const { scope, actAs } = ctx
155157 const before = await readSyncState ( root , store )
156- if ( before . unsupported_until && Date . parse ( before . unsupported_until ) > Date . now ( ) )
158+ const backoff = forScope ( before , scope ) . unsupported_until
159+ if ( backoff && Date . parse ( backoff ) > Date . now ( ) )
157160 return { store, outcome : "unsupported" , error : "the workspace server does not support lesson sync yet" }
158161 const cached = await remoteFor ( root , store , scope )
159162 const started = Date . now ( )
@@ -168,7 +171,7 @@ async function pullOnce(root: string, store: string, ctx: Extract<Context, { sta
168171 local_keys : local . keys ,
169172 } )
170173 } catch ( error ) {
171- return recordPullFailure ( root , store , error , Date . now ( ) - started )
174+ return recordPullFailure ( root , store , scope , error , Date . now ( ) - started )
172175 }
173176 // The caller's own proposals refresh the ledger's pending and receipt slots. Optional: a failure keeps the pull.
174177 const submissions = await ownSubmissions ( actAs , scope . datamateId , forScope ( before , scope ) ) . catch ( ( error ) => {
@@ -195,7 +198,7 @@ async function pullOnce(root: string, store: string, ctx: Extract<Context, { sta
195198 await writeRemote ( root , store , remote )
196199 const state = forScope ( await readSyncState ( root , store ) , scope )
197200 // No earlier cache: sync was off, or access was lost. Lessons changed meanwhile were never proposed, so the
198- // backfill runs again (it skips every identity the ledger already knows ).
201+ // backfill runs again (it skips a lesson only when the ledger already holds its exact content ).
199202 if ( ! previous ) state . backfill = { complete : false , done : [ ] }
200203 state . local_keys_offset = local . next
201204 applyPull ( state , remote , ! response . unchanged || ! previous )
@@ -230,13 +233,14 @@ async function ownSubmissions(actAs: ActAs, datamateId: number, state: Pick<Sync
230233 return rows
231234}
232235
233- async function recordPullFailure ( root : string , store : string , error : unknown , latency : number ) : Promise < PullOutcome > {
236+ async function recordPullFailure ( root : string , store : string , scope : Scope , error : unknown , latency : number ) : Promise < PullOutcome > {
234237 const failure = error instanceof LessonApiError ? error : new LessonApiError ( "transient" , errText ( error ) )
235238 const message = redactSecrets ( failure . message )
236239 log . warn ( "learn sync pull failed" , { store, kind : failure . kind , latency_ms : latency , error : message } )
237240 track ( { operation : "pull" , outcome : "error" , error_kind : failure . kind , latency_ms : latency } )
238241 await Store . transaction ( root , async ( ) => {
239- const state = await readSyncState ( root , store )
242+ // Recorded under the scope, so the backoff applies to this account and workspace only.
243+ const state = forScope ( await readSyncState ( root , store ) , scope )
240244 state . last_pull = { at : now ( ) , outcome : failure . kind === "unsupported" ? "unsupported" : "error" , error : message , latency_ms : latency }
241245 if ( failure . kind === "unsupported" ) state . unsupported_until = new Date ( Date . now ( ) + UNSUPPORTED_BACKOFF_MS ) . toISOString ( )
242246 // Access answers invalidate the cache; transient failures keep it.
@@ -297,7 +301,8 @@ function satisfied(proposal: Proposal, store: string, remote: Remote | undefined
297301 if ( entry ?. pending ?. submission_hash === proposal . submission_hash || entry ?. pending ?. content_hash === proposal . submission_hash ) return true
298302 if ( entry ?. receipts . some ( ( r ) => r . submission_hash === proposal . submission_hash && r . status !== "rejected" ) ) return true
299303 const live = remote ?. lessons . find ( ( l ) => identityKey ( remoteIdentity ( l ) ) === identityKey ( identity ) )
300- if ( proposal . item . change_type === "remove" ) return ! live
304+ // Without a pulled cache nothing proves the lesson is gone.
305+ if ( proposal . item . change_type === "remove" ) return ! ! remote && ! live
301306 return ! ! live && live . text === proposal . item . text && live . pinned === proposal . item . pinned &&
302307 JSON . stringify ( live . tags ) === JSON . stringify ( proposal . item . tags ) && JSON . stringify ( live . trigger_paths ) === JSON . stringify ( proposal . item . trigger_paths )
303308}
@@ -327,8 +332,8 @@ export async function push(root: string, store: string, ctx: Extract<Context, {
327332 const report = emptyReport ( store )
328333 const { scope, actAs } = ctx
329334 const live = ( ) => opts . deadline === undefined || Date . now ( ) < opts . deadline
330- const sync = await readSyncState ( root , store )
331- if ( sync . unsupported_until && Date . parse ( sync . unsupported_until ) > Date . now ( ) ) {
335+ const backoff = forScope ( await readSyncState ( root , store ) , scope ) . unsupported_until
336+ if ( backoff && Date . parse ( backoff ) > Date . now ( ) ) {
332337 report . error = "the workspace server does not support lesson sync yet"
333338 return report
334339 }
@@ -356,7 +361,10 @@ export async function push(root: string, store: string, ctx: Extract<Context, {
356361 outbox . proposals = outbox . proposals . flatMap ( ( p ) => {
357362 if ( p . state !== "held" || p . item . lesson_key !== opts . resubmit || ! sameScope ( p . scope , scope ) ) return [ p ]
358363 const next = rebase ( p , store , remote , state )
359- return next ? [ next ] : [ ]
364+ if ( next ) return [ next ]
365+ report . dropped ++
366+ log . info ( "learn sync held proposal dropped on resubmit: nothing left to propose" , { store, key : p . item . lesson_key } )
367+ return [ ]
360368 } )
361369 await writeOutbox ( root , store , outbox )
362370 }
@@ -405,9 +413,10 @@ export async function push(root: string, store: string, ctx: Extract<Context, {
405413 }
406414 } catch ( error ) {
407415 const classified = error instanceof LessonApiError ? error : new LessonApiError ( "transient" , errText ( error ) )
408- // A request the server refused (a 4xx answer, such as a wire limit) is never resent as it was: its items are
409- // sent one by one to find the item it refused, and that item is held.
410- if ( classified . kind !== "rejected" || classified . status === undefined ) failure = classified
416+ // A request the server refused as invalid (a wire limit answers 400 `VALIDATION_ERROR`) is never resent as it
417+ // was: its items are sent one by one to find the item it refused, and that item is held. Any other answer, such
418+ // as a 401 for an expired login, stops the push and leaves the queue as it is.
419+ if ( classified . kind !== "rejected" || ! REFUSED_REQUEST . has ( classified . status ?? 0 ) ) failure = classified
411420 else if ( chunk . length > 1 ) pending . unshift ( ...chunk . map ( ( proposal ) => [ proposal ] ) )
412421 else refusedAlone . push ( { proposal : chunk [ 0 ] , message : redactSecrets ( classified . message ) } )
413422 }
0 commit comments