{"record":{"id":"6448bbbd681be021","repo":"Hmbown/CodeWhale","slug":"sequence-must-be-an-integer","errorCode":null,"errorMessage":"sequence must be an integer.","messagePattern":"sequence must be an integer\\.","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"pet/src/core/evidence.ts","lineNumber":89,"sourceCode":"};\nconst optionalText = (o:Record<string,unknown>,key:string):string|undefined => o[key]===undefined?undefined:text(o[key],key);\nconst strings = (v:unknown,field:string,allowEmpty=false):string[]=>{\n  if(!Array.isArray(v)||(!allowEmpty&&!v.length)||v.length>256)throw new Error(`Invalid ${field}.`);\n  const out=v.map(x=>text(x,field));if(new Set(out).size!==out.length)throw new Error(`Duplicate ${field}.`);return out;\n};\nconst boolean = (v:unknown,field:string):boolean=>{if(typeof v!=='boolean')throw new Error(`Invalid ${field}.`);return v;};\nconst optionalNumber=(o:Record<string,unknown>,key:string)=>o[key]===undefined?undefined:num(o[key],key);\nfunction onlyKeys(o:Record<string,unknown>,keys:string[],label:string):void{\n  for(const k of Object.keys(o))if(!keys.includes(k))throw new Error(`Unknown ${label} field: ${k}.`);\n}\n/** All unrecognized fields are rejected, never secretly retained in metadata-only evidence. */\nexport function validateObservation(input:unknown):Observation {\n  const o=object(input,'observation');\n  onlyKeys(o,['version','id','runId','operationId','sourceId','epoch','sequence','time','receivedAt','subject','stage','surface','action','effect','status','target','actionDigest','authority','correlation','facts'],'observation');\n  if(o.version!==1)throw new Error('Unsupported observation version.');\n  const t=object(o.time,'time'),s=object(o.subject,'subject');\n  onlyKeys(t,['wallMs','clockId','monotonicMs','taskMs','uncertaintyMs'],'time');onlyKeys(s,['agentId','sandboxId','isolationGroup'],'subject');\n  const seq=num(o.sequence,'sequence',1);if(!Number.isSafeInteger(seq))throw new Error('sequence must be an integer.');\n  const facts=object(o.facts??{},'facts'),safeFacts:Observation['facts']={};\n  if(Object.keys(facts).length>48)throw new Error('Too many observation facts.');\n  for(const [k,v] of Object.entries(facts)){\n    text(k,'fact key',80);if(['__proto__','prototype','constructor'].includes(k))throw new Error('Unsafe fact key.');\n    if(typeof v==='string')safeFacts[k]=text(v,'fact value',512);\n    else if(typeof v==='number')safeFacts[k]=num(v,'fact value',-Number.MAX_SAFE_INTEGER);\n    else if(v===null||typeof v==='boolean')safeFacts[k]=v;\n    else throw new Error('Facts must be scalar metadata, not content objects.');\n  }\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');","sourceCodeStart":71,"sourceCodeEnd":107,"githubUrl":"https://github.com/Hmbown/CodeWhale/blob/433685b2024e7bc4c99e1e2e326bcad39b4d9d65/pet/src/core/evidence.ts#L71-L107","documentation":"Observation `sequence` must be a finite number ≥ 1 that is a safe integer; the `num` validator catches non-numbers/out-of-range values and a follow-up `Number.isSafeInteger` check throws `sequence must be an integer.` This guarantees per-source ordering fields are exact integers usable as monotonic sequence numbers.","triggerScenarios":"validateObservation given `sequence` of 1.5, 0, -3, 1e21, NaN, or a numeric string \"12\".","commonSituations":"Sequence computed with floating-point math (averaging, division); JSON parsed from a source that emitted decimals; sequence generated from a timestamp in ms with fractional part; very large counters beyond Number.MAX_SAFE_INTEGER.","solutions":["Ensure sequence is `Math.trunc`-ed and ≥ 1 before validation","Use an integer counter (BigInt on the producer side, converted safely) instead of derived floats","Check the emitter: it should serialize sequence as a JSON integer"],"exampleFix":"// before\nsequence: totalEvents / batchSize\n// after\nsequence: Math.max(1, Math.trunc(totalEvents / batchSize))","handlingStrategy":"validation","validationCode":"if (!Number.isSafeInteger(seq) || seq < 1) throw new Error('sequence must be a safe integer >= 1');","typeGuard":"const isValidSequence = (v: unknown): v is number => Number.isSafeInteger(v) && v >= 1;","tryCatchPattern":"try { validateObservation(rec); } catch (e) { if (e.message === 'sequence must be an integer.') rec.sequence = Math.trunc(Number(rec.sequence)) || 1; throw e; }","preventionTips":["Use an integer counter, not timestamp-derived floats","Serialize sequence with an integer JSON type","Avoid arithmetic that can produce fractions on counters"],"tags":["validation","integer","sequence","typescript"],"backgroundTag":"value-out-of-range","analyzedSha":"433685b2024e7bc4c99e1e2e326bcad39b4d9d65","analyzedAt":"2026-09-15T12:24:24.634Z","contentChangedAt":"2026-09-15T12:24:24.634Z","schemaVersion":2},"datasetVersion":"2026-09-22T16:17:23.217Z"}