{"record":{"id":"f3ccf05094e9c88b","repo":"Hmbown/CodeWhale","slug":"invalid-grant-object-required","errorCode":null,"errorMessage":"Invalid grant: object required.","messagePattern":"Invalid grant: object required\\.","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"pet/src/core/evidence.ts","lineNumber":110,"sourceCode":"  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');\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.'};","sourceCodeStart":92,"sourceCodeEnd":128,"githubUrl":"https://github.com/Hmbown/CodeWhale/blob/433685b2024e7bc4c99e1e2e326bcad39b4d9d65/pet/src/core/evidence.ts#L92-L128","documentation":"Each entry in a policy's `grants` array must be an object; the validator calls `object(x,'grant')` which throws `Invalid grant: object required.` when the entry is a scalar, array, or null. The same pattern applies to sources (`Invalid source: object required.`).","triggerScenarios":"validatePolicy given `grants` containing e.g. a bare string grant id \"g1\", a null, or an array instead of the grant object {id, runIds, sandboxIds, targetIds, actions, effects, notBefore, expiresAt, ...}.","commonSituations":"A config author listing grant IDs directly instead of full grant objects; JSON where the grants array was double-nested or flattened by a faulty generator; YAML-to-JSON conversion collapsing objects.","solutions":["Replace each grant entry with its full object form containing at least id, runIds, sandboxIds, targetIds, actions, effects, notBefore, expiresAt","Inspect the failing array element's type (typeof / Array.isArray) before validating","Fix the policy generator/serializer that emitted non-object entries"],"exampleFix":"// before\n\"grants\": [\"g1\", \"g2\"]\n// after\n\"grants\": [{\"id\":\"g1\",\"runIds\":[\"r1\"],\"sandboxIds\":[],\"targetIds\":[],\"actions\":[],\"effects\":[\"allow\"],\"notBefore\":0,\"expiresAt\":9999999999999}]","handlingStrategy":"type-guard","validationCode":"for (const g of policy.grants ?? []) if (g === null || typeof g !== 'object' || Array.isArray(g)) throw new Error('each grant must be an object');","typeGuard":"const isGrantObject = (g: unknown): g is Record<string, unknown> => typeof g === 'object' && g !== null && !Array.isArray(g);","tryCatchPattern":"try { validatePolicy(raw); } catch (e) { if (e.message === 'Invalid grant: object required.') console.error('grants entries must be objects, not ids — check your generator'); throw e; }","preventionTips":["Type grants in TypeScript so non-object entries fail at compile time","Validate generated policies against a JSON Schema before validatePolicy","Keep a canonical example policy to compare generated output against"],"tags":["validation","policy","type-mismatch","schema"],"backgroundTag":"type-mismatch","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"}