{
  "files": {
    "main.ts": "import {run} from './core.ts';\nimport {POLICY} from './policy.ts';\nexport function evaluate(payload: any): any { return run(payload, POLICY); }\n",
    "core.ts": "import {collectRoles, actionMatches} from './helpers.ts';\n\nexport function run(payload: any, policy: any): any {\n  const {subject, resource, now, issued_at, action, grants, role_parents} = payload;\n  const result = (allowed: boolean, reason: string, matched: number[] = []): any => ({\n    allowed,\n    reason,\n    matched,\n    audit: !allowed && policy.audit_denials ? [reason] : []\n  });\n\n  if (subject.tenant !== resource.tenant) return result(false, 'tenant');\n  if (now - issued_at > policy.token_ttl) return result(false, 'token');\n  if (policy.suspended_denies && subject.suspended) return result(false, 'suspended');\n  if (policy.owner_bypass && subject.owner) return result(true, 'owner');\n\n  const roles = collectRoles(subject, role_parents, policy);\n  const matched: number[] = [];\n  let hasAllow = false;\n  let hasDeny = false;\n\n  for (let i = 0; i < grants.length; i++) {\n    const grant = grants[i];\n    if (!roles.has(grant.role)) continue;\n    if (!actionMatches(grant.action, action, policy)) continue;\n    const scopeMatches = policy.resource_scope === 'exact'\n      ? grant.scope === resource.id\n      : resource.id.startsWith(grant.scope);\n    if (!scopeMatches) continue;\n    if (grant.expires !== null) {\n      const unexpired = policy.expiry_inclusive\n        ? now <= grant.expires\n        : now < grant.expires;\n      if (!unexpired) continue;\n    }\n    matched.push(i);\n    if (grant.allow) hasAllow = true;\n    else hasDeny = true;\n  }\n\n  if (matched.length === 0) return result(policy.default_allow, 'default');\n  return result(hasAllow && (!policy.deny_wins || !hasDeny), 'grants', matched);\n}\n",
    "policy.ts": "export const POLICY: any = {\n  action_casefold: true,\n  audit_denials: true,\n  default_allow: true,\n  deny_wins: true,\n  expiry_inclusive: true,\n  group_union: true,\n  inherit_roles: true,\n  owner_bypass: true,\n  resource_scope: 'exact',\n  suspended_denies: true,\n  token_ttl: 100,\n  wildcard_action: true\n};\n",
    "helpers.ts": "export function collectRoles(subject: any, roleParents: any, policy: any): Set<string> {\n  const roles = new Set<string>(subject.roles);\n  if (policy.group_union) {\n    for (const group of subject.groups) roles.add(group);\n  }\n  if (policy.inherit_roles) {\n    const pending = Array.from(roles);\n    while (pending.length > 0) {\n      const role = pending.pop()!;\n      if (!Object.prototype.hasOwnProperty.call(roleParents, role)) continue;\n      for (const parent of roleParents[role]) {\n        if (!roles.has(parent)) {\n          roles.add(parent);\n          pending.push(parent);\n        }\n      }\n    }\n  }\n  return roles;\n}\n\nexport function actionMatches(grantAction: string, requestedAction: string, policy: any): boolean {\n  if (policy.wildcard_action && grantAction === '*') return true;\n  return policy.action_casefold\n    ? grantAction.toLowerCase() === requestedAction.toLowerCase()\n    : grantAction === requestedAction;\n}\n"
  }
}