codeburn/src/sync/push.ts
Andrew Lee 50c8251719 fix(sync): close credential-leak paths; session retraction; span/key/CLI hardening
Review rounds 2-3 + self-review on --attribution:

Credential egress (round 2):
- normalizeRemoteUrl: scp userinfo expressed as an optional regex group
  let backtracking re-parse a credential prefix as host:path
  (x-access-token:ghp_...@host/repo -> token in git.repo). Userinfo is
  now split off at the first @ BEFORE any host matching.
- Positive validation (allow-list) as the final gate on EVERY branch:
  host must be hostname-shaped, every path segment repo-shaped, total
  identity <= 200 chars. Kills transport-helper remotes (ext:: leaks
  local SSH key paths, codecommit:: leaks AWS profile names), residual
  @, spaces/colons, and unbounded strings.
- sanitizePrLinks: links are rebuilt from origin + pathname — userinfo,
  query strings, and fragments are dropped instead of passed through;
  collapsed duplicates dedupe.

Attribution correctness (round 3 + self-review):
- Double-count fix with precise retraction semantics: when a commit
  migrates to a later-parsed tighter-window session, the loser re-emits
  git.commit_count=0. Empty records are emitted ONLY on a true loss in
  THIS computation (lostCandidacy) — a commit that merely aged out of
  the --since range was lost to nobody, and retracting it would
  permanently zero a still-correct server-side count. The sync layer
  additionally requires a prior ledgered state for the session.
- Session dedup key includes project + both window timestamps, so
  ongoing sessions re-emit with corrected span times.
- Span end times clamped like the usage builder (never 0, never
  earlier than start + 1ms).
- CLI mirrors the usage path on attribution push failures instead of
  claiming success.
- Identity normalization: case-insensitive .git strip, doubled path
  slashes collapse.

AI-Origin: human
2026-08-02 12:56:59 +00:00

252 lines
8.9 KiB
TypeScript

/**
* codeburn sync — push orchestration.
*
* Extracted from the CLI action so the flatten/filter/batch/send/ledger
* pipeline is unit-testable without a full CLI invocation.
*/
import type { ProjectSummary } from '../types.js'
import { assertHttps } from './discovery.js'
import { ledgerKeySet, appendToLedger, type LedgerEntry } from './ledger.js'
import {
buildOtlpPayload,
batchCalls,
buildAttributionOtlpPayload,
flattenAttributionRecords,
type CallWithSession,
type AttributionItem,
type OtlpPayload,
} from './otlp.js'
import type { SessionAttributionRecord } from '../yield.js'
/**
* Safety valve, not a routine cap — pushes now loop until all batches are
* sent (429s are waited out). This only bounds a single push in pathological
* cases (e.g. corrupted ledger causing a full re-send of years of data).
*/
export const MAX_PER_PUSH = 50_000
/** Flatten parsed projects into individual calls and filter out already-sent ones. */
export function collectUnsentCalls(projects: ProjectSummary[]): {
allCalls: CallWithSession[]
unsent: CallWithSession[]
} {
const allCalls: CallWithSession[] = []
for (const project of projects) {
for (const session of project.sessions) {
for (const turn of session.turns) {
for (const call of turn.assistantCalls) {
allCalls.push({
call,
sessionId: session.sessionId,
project: project.project,
})
}
}
}
}
const sent = ledgerKeySet()
const unsent = allCalls.filter(c => !sent.has(c.call.deduplicationKey))
return { allCalls, unsent }
}
export type PushOutcome = 'complete' | 'auth-rejected' | 'rate-limited' | 'server-error'
export interface PushResult {
outcome: PushOutcome
totalSent: number
totalRejected: number
totalCostSent: number
retryAfter?: string
httpStatus?: number
/** Total milliseconds spent waiting on 429 Retry-After */
totalWaitMs?: number
}
export interface SendBatchesOptions {
endpoint: string
accessToken: string
batches: CallWithSession[][]
log?: (msg: string) => void
/** Injectable sleep for tests. Defaults to real setTimeout. */
sleep?: (ms: number) => Promise<void>
/** Max wait per 429 (caps Retry-After). Default 120s. */
maxWaitMs?: number
/** Consecutive 429 retries per batch before giving up. Default 3. */
max429Retries?: number
}
/** Parse Retry-After header: delta-seconds or HTTP-date. Returns ms, or null. */
export function parseRetryAfterMs(value: string | null): number | null {
if (!value) return null
if (/^-?\d+$/.test(value.trim())) {
const seconds = Number(value)
return seconds >= 0 ? seconds * 1000 : null
}
const date = Date.parse(value)
if (!isNaN(date)) return Math.max(0, date - Date.now())
return null
}
/**
* Send batches sequentially until all are sent. Ledgers each fully-accepted
* batch. Partially-rejected batches are NOT ledgered (OTLP doesn't identify
* which spans were rejected; deterministic span IDs make full-batch retry safe).
*
* 429 responses are honored: waits Retry-After (capped at maxWaitMs, default
* backoff 5s when absent) and retries the same batch, up to max429Retries
* consecutive times before giving up. Stops on 401/5xx — unsent batches
* retry on the next push.
*/
export async function sendBatches(opts: SendBatchesOptions): Promise<PushResult> {
return sendBatchesCore({
...opts,
buildPayload: buildOtlpPayload,
toOutbound: c => ({ key: c.call.deduplicationKey, ts: c.call.timestamp, costUSD: c.call.costUSD }),
})
}
/** How sendBatchesCore ledgers and prices a batch item. */
type OutboundItem = { key: string; ts: string; costUSD: number }
type SendBatchesCoreOptions<T> = Omit<SendBatchesOptions, 'batches'> & {
batches: T[][]
buildPayload: (batch: T[]) => OtlpPayload
toOutbound: (item: T) => OutboundItem
}
async function sendBatchesCore<T>(opts: SendBatchesCoreOptions<T>): Promise<PushResult> {
assertHttps(opts.endpoint, 'Traces endpoint')
const log = opts.log ?? (() => {})
const sleep = opts.sleep ?? ((ms: number) => new Promise<void>(r => setTimeout(r, ms)))
const maxWaitMs = opts.maxWaitMs ?? 120_000
const max429Retries = opts.max429Retries ?? 3
let totalSent = 0
let totalRejected = 0
let totalCostSent = 0
let totalWaitMs = 0
for (const batch of opts.batches) {
let attempts429 = 0
// Retry loop for the current batch (429 only)
for (;;) {
const payload = opts.buildPayload(batch)
const response = await fetch(opts.endpoint, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${opts.accessToken}`,
},
body: JSON.stringify(payload),
})
if (response.status === 401) {
return { outcome: 'auth-rejected', totalSent, totalRejected, totalCostSent, totalWaitMs, httpStatus: 401 }
}
if (response.status === 429) {
const retryAfterHeader = response.headers.get('Retry-After')
attempts429++
if (attempts429 > max429Retries) {
return {
outcome: 'rate-limited', totalSent, totalRejected, totalCostSent, totalWaitMs,
retryAfter: retryAfterHeader ?? undefined, httpStatus: 429,
}
}
const waitMs = Math.min(parseRetryAfterMs(retryAfterHeader) ?? 5000, maxWaitMs)
log(` Rate limited — waiting ${Math.round(waitMs / 1000)}s before retrying (attempt ${attempts429}/${max429Retries})`)
totalWaitMs += waitMs
await sleep(waitMs)
continue
}
if (!response.ok) {
return { outcome: 'server-error', totalSent, totalRejected, totalCostSent, totalWaitMs, httpStatus: response.status }
}
// Check for partial success
let rejected = 0
try {
const body = await response.json() as { partialSuccess?: { rejectedSpans?: number | string } }
// proto3 int64 JSON mapping: strict protojson servers send int64 as a
// string — Number() both so `totalRejected +=` never concatenates.
rejected = Number(body?.partialSuccess?.rejectedSpans ?? 0)
if (!Number.isFinite(rejected) || rejected < 0) rejected = 0
} catch { /* empty response = full success */ }
if (rejected > 0) {
// OTLP partial_success doesn't identify WHICH spans were rejected.
// Ledger nothing — the whole batch retries on the next push.
totalRejected += rejected
log(` Batch: ${rejected}/${batch.length} spans rejected — whole batch will retry on next push`)
} else {
const outbound = batch.map(opts.toOutbound)
const entries: LedgerEntry[] = outbound.map(o => ({ key: o.key, ts: o.ts }))
appendToLedger(entries)
totalSent += batch.length
totalCostSent += outbound.reduce((s, o) => s + o.costUSD, 0)
}
break // batch done (success or partial) — move to next batch
}
}
return { outcome: 'complete', totalSent, totalRejected, totalCostSent, totalWaitMs }
}
/**
* Safety valve for attribution items, mirroring MAX_PER_PUSH: bounds a first
* `--since all --attribution` push over a long history. Remaining facts are
* sent on the next push (the ledger tracks progress).
*/
export const MAX_ATTRIBUTION_PER_PUSH = 10_000
/** Flatten attribution records into items and filter out already-sent ones. */
export function collectUnsentAttribution(records: SessionAttributionRecord[]): {
allItems: AttributionItem[]
unsent: AttributionItem[]
} {
const sent = ledgerKeySet()
// Empty records (no commits, no PR links) exist only to RETRACT a session
// span whose commits migrated to another session. Send one only when a
// PRIOR state for that session was already ledgered — a session that was
// never sent has nothing to retract.
const sessionsWithPriorState = new Set<string>()
for (const key of sent) {
if (key.startsWith('attr:s:')) {
const sessionId = key.slice('attr:s:'.length, key.lastIndexOf(':'))
sessionsWithPriorState.add(sessionId)
}
}
const sendable = records.filter(r =>
r.commits.length > 0 || r.prLinks.length > 0 || sessionsWithPriorState.has(r.sessionId),
)
const allItems = flattenAttributionRecords(sendable)
const unsent = allItems.filter(i => !sent.has(i.dedupKey))
return { allItems, unsent }
}
export interface SendAttributionBatchesOptions extends Omit<SendBatchesOptions, 'batches'> {
batches: AttributionItem[][]
}
/**
* Send attribution batches through the same retry/ledger pipeline as usage
* batches. Items are ledgered by their state-encoding dedup keys, so an
* identical attribution fact is sent once and a state transition (commit
* merged to main, commit reverted) re-sends the updated fact.
*/
export async function sendAttributionBatches(opts: SendAttributionBatchesOptions): Promise<PushResult> {
return sendBatchesCore({
...opts,
buildPayload: buildAttributionOtlpPayload,
toOutbound: item => ({ key: item.dedupKey, ts: item.timestamp, costUSD: 0 }),
})
}
export { batchCalls }