{"record":{"id":"05f4940eebf7401a","repo":"Hmbown/CodeWhale","slug":"invalid-evidence-bundle-or-record-limit-exceeded","errorCode":null,"errorMessage":"Invalid evidence bundle or record limit exceeded.","messagePattern":"Invalid evidence bundle or record limit exceeded\\.","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"pet/src/core/evidence.ts","lineNumber":116,"sourceCode":"  }\n  let target:Observation['target'];if(o.target!==undefined){const a=object(o.target,'target');onlyKeys(a,['id','kind','version','boundary'],'target');target={id:text(a.id,'target.id'),kind:text(a.kind,'target.kind',64),version:optionalText(a,'version'),boundary:a.boundary===undefined?undefined:enumValue(a.boundary,['local','external','unknown'] as const,'boundary')};}\n  let authority:Observation['authority'];if(o.authority!==undefined){const a=object(o.authority,'authority');onlyKeys(a,['grantId','claimed'],'authority');authority={grantId:optionalText(a,'grantId'),claimed:a.claimed===undefined?undefined:boolean(a.claimed,'claimed')};}\n  let correlation:Observation['correlation'];if(o.correlation!==undefined){const a=object(o.correlation,'correlation');onlyKeys(a,['sessionId','requestId','parentOperationId','messageId'],'correlation');correlation={sessionId:optionalText(a,'sessionId'),requestId:optionalText(a,'requestId'),parentOperationId:optionalText(a,'parentOperationId'),messageId:optionalText(a,'messageId')};}\n  return {version:1,id:text(o.id,'id'),runId:text(o.runId,'runId'),operationId:optionalText(o,'operationId'),sourceId:text(o.sourceId,'sourceId'),epoch:text(o.epoch,'epoch'),sequence:seq,\n    time:{wallMs:num(t.wallMs,'wallMs'),clockId:optionalText(t,'clockId'),monotonicMs:optionalNumber(t,'monotonicMs'),taskMs:optionalNumber(t,'taskMs'),uncertaintyMs:optionalNumber(t,'uncertaintyMs')},receivedAt:optionalNumber(o,'receivedAt'),\n    subject:{agentId:text(s.agentId,'agentId'),sandboxId:optionalText(s,'sandboxId'),isolationGroup:optionalText(s,'isolationGroup')},stage:enumValue(o.stage,STAGES,'stage'),surface:enumValue(o.surface,SURFACES,'surface'),action:text(o.action,'action',128),effect:enumValue(o.effect,EFFECTS,'effect'),status:enumValue(o.status,['started','completed','error','unknown'],'status'),target,actionDigest:optionalText(o,'actionDigest'),authority,correlation,facts:safeFacts};\n}\nexport function validatePolicy(input:unknown):BoundaryPolicy {\n  const o=object(input,'policy');onlyKeys(o,['version','id','sources','grants','expectedSurfaces','forbiddenCrossGroup'],'policy');\n  if(o.version!==1||!Array.isArray(o.sources)||o.sources.length>256||!Array.isArray(o.grants)||o.grants.length>2048)throw new Error('Invalid policy version or limits.');\n  const sources=o.sources.map(x=>{const a=object(x,'source');onlyKeys(a,['id','stages','surfaces','runIds','sandboxIds','isolationGroups','heartbeatMs','description'],'source');return {id:text(a.id,'source.id'),stages:strings(a.stages,'stages').map(v=>enumValue(v,STAGES,'stage')),surfaces:strings(a.surfaces,'surfaces').map(v=>enumValue(v,SURFACES,'surface')),runIds:strings(a.runIds,'runIds'),sandboxIds:a.sandboxIds===undefined?undefined:strings(a.sandboxIds,'sandboxIds'),isolationGroups:a.isolationGroups===undefined?undefined:strings(a.isolationGroups,'isolationGroups'),heartbeatMs:num(a.heartbeatMs,'heartbeatMs',1),description:optionalText(a,'description')};});\n  const grants=o.grants.map(x=>{const a=object(x,'grant');onlyKeys(a,['id','runIds','sandboxIds','targetIds','actions','effects','notBefore','expiresAt','actionDigest'],'grant');const g={id:text(a.id,'grant.id'),runIds:strings(a.runIds,'runIds'),sandboxIds:strings(a.sandboxIds,'sandboxIds'),targetIds:strings(a.targetIds,'targetIds'),actions:strings(a.actions,'actions'),effects:strings(a.effects,'effects').map(v=>enumValue(v,EFFECTS,'effect')),notBefore:num(a.notBefore,'notBefore'),expiresAt:num(a.expiresAt,'expiresAt'),actionDigest:optionalText(a,'actionDigest')};if(g.expiresAt<=g.notBefore)throw new Error('Grant expiry must follow its start.');return g;});\n  if(new Set(sources.map(s=>s.id)).size!==sources.length||new Set(grants.map(g=>g.id)).size!==grants.length)throw new Error('Duplicate policy identity.');\n  return {version:1,id:text(o.id,'policy.id'),sources,grants,expectedSurfaces:strings(o.expectedSurfaces,'expectedSurfaces',true).map(v=>enumValue(v,SURFACES,'surface')),forbiddenCrossGroup:boolean(o.forbiddenCrossGroup,'forbiddenCrossGroup')};\n}\nexport function validateBundle(input:unknown,maxRecords=100_000):EvidenceBundle {\n  const o=object(input,'bundle');onlyKeys(o,['format','name','records','policy','asOf'],'bundle');\n  if(o.format!=='whalesong.evidence/v1'||!Array.isArray(o.records)||o.records.length>maxRecords)throw new Error('Invalid evidence bundle or record limit exceeded.');\n  const records=o.records.map(validateObservation),seen=new Set<string>();\n  for(const record of records){const key=observationKey(record);if(seen.has(key))throw new Error('Duplicate producer incarnation/sequence in evidence bundle. Import cancelled; resolve identity before import.');seen.add(key);}\n  return {format:o.format,name:text(o.name,'name'),records,policy:validatePolicy(o.policy),asOf:num(o.asOf,'asOf')};\n}\nexport function sourceAccepts(source:EvidenceSource,o:Observation):boolean {\n  return source.id===o.sourceId&&source.stages.includes(o.stage)&&source.surfaces.includes(o.surface)&&source.runIds.includes(o.runId)&&(!source.sandboxIds||source.sandboxIds.includes(o.subject.sandboxId??''))&&(!source.isolationGroups||source.isolationGroups.includes(o.subject.isolationGroup??''));\n}\nexport type Authorization = { decision:'permitted'|'denied'|'unknown'; reason:string };\n/** Exact allowlists; no prefix matching, text approval, or ambient default allow. Not enforcement. */\nexport function authorize(o:Observation,policy:BoundaryPolicy):Authorization {\n  const source=policy.sources.find(s=>s.id===o.sourceId);\n  if(!source||!sourceAccepts(source,o))return {decision:'unknown',reason:'Source is not bound to this scope.'};\n  if(o.effect==='unknown')return {decision:'unknown',reason:'Actual effect not established.'};\n  const grant=policy.grants.find(g=>g.id===o.authority?.grantId);\n  if(!grant)return {decision:'denied',reason:'No matching operator-configured grant.'};\n  const u=o.time.uncertaintyMs;\n  if(u===undefined)return {decision:'unknown',reason:'Clock uncertainty absent; grant validity cannot be established.'};\n  if(o.time.wallMs-u<grant.notBefore||o.time.wallMs+u>=grant.expiresAt)return {decision:'denied',reason:'Outside grant validity interval (including clock uncertainty).'};","sourceCodeStart":98,"sourceCodeEnd":134,"githubUrl":"https://github.com/Hmbown/CodeWhale/blob/433685b2024e7bc4c99e1e2e326bcad39b4d9d65/pet/src/core/evidence.ts#L98-L134","documentation":"validateBundle rejects an input that is not a valid whalesong.evidence/v1 bundle: wrong `format` string, `records` missing/not an array, or record count above the limit (default 100,000, overridable via maxRecords). This is the first gate before any per-record validation in pet/src/core/evidence.ts:116.","triggerScenarios":"Calling validateBundle/importTrace with a payload whose format field is not exactly 'whalesong.evidence/v1', records is absent or not an array, or records.length > maxRecords (default 100_000).","commonSituations":"Hand-rolled JSON exported from another tool with a different format string; forgetting to bump maxRecords when importing large historical traces; passing a single record object instead of a bundle wrapper.","solutions":["Set format to exactly 'whalesong.evidence/v1' and wrap records in an array","Split the bundle or pass a higher maxRecords argument if records legitimately exceed 100k","Check for undefined/JSON parsing failures that drop the records field"],"exampleFix":"// before\nimportTrace({records: myRecords})\n// after\nimportTrace({format:'whalesong.evidence/v1', name:'trace', records: myRecords, policy, asOf: Date.now()})","handlingStrategy":"validation","validationCode":"const ok = b && b.format==='whalesong.evidence/v1' && Array.isArray(b.records) && b.records.length<=100000;","typeGuard":"const isBundle = (b:any): b is {format:'whalesong.evidence/v1',records:unknown[],name:string,policy:unknown,asOf:number} => typeof b==='object' && b!==null && b.format==='whalesong.evidence/v1' && Array.isArray(b.records);","tryCatchPattern":"try { const bundle = validateBundle(raw); } catch (e) { if (/Invalid evidence bundle or record limit/.test(e.message)) { /* check format string and record count */ } else throw e; }","preventionTips":["Always set format:'whalesong.evidence/v1' exactly","Check records.length against maxRecords before import","Validate JSON parse output shape before calling the API"],"tags":["validation","schema","limits","bundle"],"backgroundTag":"schema-validation-failed","analyzedSha":"433685b2024e7bc4c99e1e2e326bcad39b4d9d65","analyzedAt":"2026-09-15T12:24:24.634Z","contentChangedAt":"2026-09-15T12:24:24.634Z","schemaVersion":2},"datasetVersion":"2026-09-22T11:17:16.035Z"}