permission-config.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /**
  2. * Permission classifier runtime knobs. Extracted from `config.ts` to keep
  3. * the main schema file under the 2_000-line guard.
  4. *
  5. * `classifierTimeoutMs` is the single base for three coupled timeouts
  6. * preserving the invariant `plugin fetch > daemon outer race ≥ daemon LLM
  7. * SDK`:
  8. * - daemon Stage 2 LLM SDK + outer race = base
  9. * - legacy local-runtime plugin permission-check fetch = base + 5_000ms
  10. *
  11. * Plugin fetches via `GET /api/config` (mirrors `skill-evolve-trigger`).
  12. */
  13. export type PermissionPolicyOwner = 'engine' | 'core';
  14. export type PermissionStorageWriteVersion = 1 | 2;
  15. export interface PermissionConfig {
  16. /**
  17. * Final deterministic policy owner. Core is the default; Engine remains an
  18. * explicit rollback path.
  19. */
  20. policyOwner: PermissionPolicyOwner;
  21. /**
  22. * Disk format used for new or explicitly modified permission.json files.
  23. * Readers always accept both versions; v2 is the product default and an
  24. * explicit v1 value is the storage rollback switch.
  25. */
  26. storageWriteVersion: PermissionStorageWriteVersion;
  27. /**
  28. * Base timeout (ms) for the auto-mode permission classifier. Default
  29. * 60_000 calibrated for thinking-class LLMs. Hard floor 5_000ms — lower
  30. * values silently fall back to the default. No ceiling.
  31. */
  32. classifierTimeoutMs: number;
  33. /**
  34. * Whether permission checks may create user-confirmation requests.
  35. * Standard builds default to true. Headless builds may default this to
  36. * false so an ASK decision fails closed instead of waiting forever.
  37. */
  38. userConfirmationEnabled: boolean;
  39. }
  40. export const PERMISSION_CONFIG_DEFAULTS: PermissionConfig = {
  41. policyOwner: 'core',
  42. storageWriteVersion: 2,
  43. classifierTimeoutMs: 60_000,
  44. userConfirmationEnabled: true,
  45. };
  46. /**
  47. * Parse the `permission:` block from raw config. Values below the
  48. * 5_000ms hard floor silently fall back to the default rather than
  49. * crippling the gate.
  50. */
  51. export function parsePermissionConfig(
  52. raw: Record<string, unknown>,
  53. defaults: PermissionConfig = PERMISSION_CONFIG_DEFAULTS,
  54. ): PermissionConfig {
  55. const perm = raw.permission;
  56. if (perm == null || typeof perm !== 'object' || Array.isArray(perm)) {
  57. return { ...defaults };
  58. }
  59. const obj = perm as Record<string, unknown>;
  60. const policyOwner =
  61. obj.policyOwner === undefined
  62. ? defaults.policyOwner
  63. : obj.policyOwner === 'engine' || obj.policyOwner === 'core'
  64. ? obj.policyOwner
  65. : 'engine';
  66. const storageWriteVersion =
  67. obj.storageWriteVersion === 1 || obj.storageWriteVersion === 2
  68. ? obj.storageWriteVersion
  69. : defaults.storageWriteVersion;
  70. const t = obj.classifierTimeoutMs;
  71. const classifierTimeoutMs =
  72. typeof t === 'number' && Number.isFinite(t) && t >= 5_000
  73. ? Math.floor(t)
  74. : defaults.classifierTimeoutMs;
  75. const userConfirmationEnabled =
  76. typeof obj.userConfirmationEnabled === 'boolean'
  77. ? obj.userConfirmationEnabled
  78. : defaults.userConfirmationEnabled;
  79. return { policyOwner, storageWriteVersion, classifierTimeoutMs, userConfirmationEnabled };
  80. }