theokit 0.76.3 → 0.77.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (113) hide show
  1. package/README.md +59 -0
  2. package/dist/{actions-virtual-module-52O6HJZT.js → actions-virtual-module-UIX5YWZR.js} +4 -4
  3. package/dist/{actions-virtual-module-HS4W7HQU.js → actions-virtual-module-V5Q4XVL5.js} +2 -2
  4. package/dist/{add-DOR7HORT.js → add-NFDL4OJ7.js} +2 -2
  5. package/dist/{agent-HCK5RHH7.js → agent-MJWQRSJ2.js} +2 -2
  6. package/dist/{app-typed-client-CJCOALMM.js → app-typed-client-6FQB5FQO.js} +4 -4
  7. package/dist/{app-typed-client-BWITJLJA.js → app-typed-client-JCBQGPMP.js} +2 -2
  8. package/dist/{build-Y3CWTWP6.js → build-Y457R5HE.js} +10 -63
  9. package/dist/build-Y457R5HE.js.map +1 -0
  10. package/dist/{chunk-W2I6L4IJ.js → chunk-35OI2TKT.js} +2 -2
  11. package/dist/{chunk-OIYSUQM7.js → chunk-3TCWOHLY.js} +4 -2
  12. package/dist/{chunk-OIYSUQM7.js.map → chunk-3TCWOHLY.js.map} +1 -1
  13. package/dist/{chunk-UQQQ3V43.js → chunk-3XR6CBUA.js} +65 -5
  14. package/dist/chunk-3XR6CBUA.js.map +1 -0
  15. package/dist/{chunk-BTDTHXUI.js → chunk-BFDEEJOK.js} +1 -1
  16. package/dist/{chunk-BTDTHXUI.js.map → chunk-BFDEEJOK.js.map} +1 -1
  17. package/dist/{chunk-XI4FALFZ.js → chunk-E22GPCCO.js} +24 -1
  18. package/dist/chunk-E22GPCCO.js.map +1 -0
  19. package/dist/{chunk-OED37LE5.js → chunk-GNV34JMI.js} +37 -3
  20. package/dist/chunk-GNV34JMI.js.map +1 -0
  21. package/dist/{chunk-2SGNODVJ.js → chunk-HNALEGOL.js} +40 -10
  22. package/dist/chunk-HNALEGOL.js.map +1 -0
  23. package/dist/{chunk-ZQJORKBM.js → chunk-HZAXFD55.js} +49 -2
  24. package/dist/chunk-HZAXFD55.js.map +1 -0
  25. package/dist/{chunk-JVXDEIT4.js → chunk-I2IDWM26.js} +33 -9
  26. package/dist/chunk-I2IDWM26.js.map +1 -0
  27. package/dist/{chunk-PVCXREGE.js → chunk-KQBY62AE.js} +2 -2
  28. package/dist/{chunk-BXQCJJPT.js → chunk-N5YHENWJ.js} +2 -2
  29. package/dist/{chunk-FEK25JUL.js → chunk-OKFRGAYZ.js} +67 -9
  30. package/dist/chunk-OKFRGAYZ.js.map +1 -0
  31. package/dist/{chunk-XDLFOATW.js → chunk-OSSNCUS6.js} +4 -4
  32. package/dist/{chunk-C2BRSL4W.js → chunk-PQWVT6JC.js} +11 -1
  33. package/dist/{chunk-C2BRSL4W.js.map → chunk-PQWVT6JC.js.map} +1 -1
  34. package/dist/chunk-V3LMUQSB.js +98 -0
  35. package/dist/chunk-V3LMUQSB.js.map +1 -0
  36. package/dist/{chunk-47FFT43U.js → chunk-YH5CDWLU.js} +25 -1
  37. package/dist/chunk-YH5CDWLU.js.map +1 -0
  38. package/dist/cli/index.js +13 -13
  39. package/dist/{db-ZWDKD57T.js → db-5WOK6HA2.js} +3 -3
  40. package/dist/{define-middleware-BOHpPOBs.d.ts → define-middleware-D7LFaTv-.d.ts} +27 -2
  41. package/dist/{dev-R2ZYHZV4.js → dev-N5LQFLBR.js} +9 -7
  42. package/dist/dev-N5LQFLBR.js.map +1 -0
  43. package/dist/docker-OTV3M54A.js +130 -0
  44. package/dist/docker-OTV3M54A.js.map +1 -0
  45. package/dist/{generate-6F24XT72.js → generate-4DY3MEYB.js} +73 -41
  46. package/dist/generate-4DY3MEYB.js.map +1 -0
  47. package/dist/{index-DfsJ3Qhk.d.ts → index-C2lcEI-4.d.ts} +2 -2
  48. package/dist/index.d.ts +1 -0
  49. package/dist/index.js +5 -4
  50. package/dist/index.js.map +1 -1
  51. package/dist/{info-IPMW56PI.js → info-ELSPSCXK.js} +3 -3
  52. package/dist/{internal-api-6RAOYIET.js → internal-api-5FTPYSEQ.js} +2 -2
  53. package/dist/{internal-api-YGFPLQ4E.js → internal-api-H3C2ID6N.js} +4 -4
  54. package/dist/{job-backend-WvUs5rO1.d.ts → job-backend-BX-PwnZW.d.ts} +25 -0
  55. package/dist/{load-config-USRR4GXP.js → load-config-SKLFH7YL.js} +3 -3
  56. package/dist/{mcp-FBXEX45H.js → mcp-3ENI2IPS.js} +3 -3
  57. package/dist/{openapi-CA3E3LZH.js → openapi-OVXYHZ2S.js} +3 -3
  58. package/dist/{preview-DLIVPM7A.js → preview-Y3JNYOFK.js} +3 -3
  59. package/dist/{routes-BJ7GGRCV.js → routes-UNJ77IQU.js} +3 -3
  60. package/dist/{schema-FBM2UH72.js → schema-2EAIJFWA.js} +2 -2
  61. package/dist/server/define/index.d.ts +5 -4
  62. package/dist/server/define/index.js +1 -1
  63. package/dist/server/http/index.d.ts +4 -3
  64. package/dist/server/http/index.js +3 -3
  65. package/dist/server/index.d.ts +5 -5
  66. package/dist/server/index.js +7 -5
  67. package/dist/server/index.js.map +1 -1
  68. package/dist/server/jobs/index.d.ts +75 -6
  69. package/dist/server/jobs/index.js +4 -2
  70. package/dist/{server-boundary-EKIGYL7B.js → server-boundary-5ZTBC2KF.js} +2 -2
  71. package/dist/{server-boundary-IDWIB24L.js → server-boundary-DTF6ZEUO.js} +4 -4
  72. package/dist/{start-LNT5UVSY.js → start-EJFYT7ZU.js} +13 -37
  73. package/dist/start-EJFYT7ZU.js.map +1 -0
  74. package/dist/vite-plugin/index.js +5 -4
  75. package/dist/{vite-plugin-LE42ZIGV.js → vite-plugin-MNZHNOM4.js} +6 -6
  76. package/package.json +3 -3
  77. package/dist/build-Y3CWTWP6.js.map +0 -1
  78. package/dist/chunk-2SGNODVJ.js.map +0 -1
  79. package/dist/chunk-47FFT43U.js.map +0 -1
  80. package/dist/chunk-FEK25JUL.js.map +0 -1
  81. package/dist/chunk-JVXDEIT4.js.map +0 -1
  82. package/dist/chunk-OED37LE5.js.map +0 -1
  83. package/dist/chunk-UQQQ3V43.js.map +0 -1
  84. package/dist/chunk-XI4FALFZ.js.map +0 -1
  85. package/dist/chunk-ZQJORKBM.js.map +0 -1
  86. package/dist/dev-R2ZYHZV4.js.map +0 -1
  87. package/dist/docker-LZZB4D5E.js +0 -102
  88. package/dist/docker-LZZB4D5E.js.map +0 -1
  89. package/dist/generate-6F24XT72.js.map +0 -1
  90. package/dist/start-LNT5UVSY.js.map +0 -1
  91. /package/dist/{actions-virtual-module-52O6HJZT.js.map → actions-virtual-module-UIX5YWZR.js.map} +0 -0
  92. /package/dist/{actions-virtual-module-HS4W7HQU.js.map → actions-virtual-module-V5Q4XVL5.js.map} +0 -0
  93. /package/dist/{add-DOR7HORT.js.map → add-NFDL4OJ7.js.map} +0 -0
  94. /package/dist/{agent-HCK5RHH7.js.map → agent-MJWQRSJ2.js.map} +0 -0
  95. /package/dist/{app-typed-client-CJCOALMM.js.map → app-typed-client-6FQB5FQO.js.map} +0 -0
  96. /package/dist/{app-typed-client-BWITJLJA.js.map → app-typed-client-JCBQGPMP.js.map} +0 -0
  97. /package/dist/{chunk-W2I6L4IJ.js.map → chunk-35OI2TKT.js.map} +0 -0
  98. /package/dist/{chunk-PVCXREGE.js.map → chunk-KQBY62AE.js.map} +0 -0
  99. /package/dist/{chunk-BXQCJJPT.js.map → chunk-N5YHENWJ.js.map} +0 -0
  100. /package/dist/{chunk-XDLFOATW.js.map → chunk-OSSNCUS6.js.map} +0 -0
  101. /package/dist/{db-ZWDKD57T.js.map → db-5WOK6HA2.js.map} +0 -0
  102. /package/dist/{info-IPMW56PI.js.map → info-ELSPSCXK.js.map} +0 -0
  103. /package/dist/{internal-api-6RAOYIET.js.map → internal-api-5FTPYSEQ.js.map} +0 -0
  104. /package/dist/{internal-api-YGFPLQ4E.js.map → internal-api-H3C2ID6N.js.map} +0 -0
  105. /package/dist/{load-config-USRR4GXP.js.map → load-config-SKLFH7YL.js.map} +0 -0
  106. /package/dist/{mcp-FBXEX45H.js.map → mcp-3ENI2IPS.js.map} +0 -0
  107. /package/dist/{openapi-CA3E3LZH.js.map → openapi-OVXYHZ2S.js.map} +0 -0
  108. /package/dist/{preview-DLIVPM7A.js.map → preview-Y3JNYOFK.js.map} +0 -0
  109. /package/dist/{routes-BJ7GGRCV.js.map → routes-UNJ77IQU.js.map} +0 -0
  110. /package/dist/{schema-FBM2UH72.js.map → schema-2EAIJFWA.js.map} +0 -0
  111. /package/dist/{server-boundary-EKIGYL7B.js.map → server-boundary-5ZTBC2KF.js.map} +0 -0
  112. /package/dist/{server-boundary-IDWIB24L.js.map → server-boundary-DTF6ZEUO.js.map} +0 -0
  113. /package/dist/{vite-plugin-LE42ZIGV.js.map → vite-plugin-MNZHNOM4.js.map} +0 -0
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../src/cli/commands/generate.ts","../src/cli/commands/generate-containment.ts","../src/cli/commands/generate-policy-placeholder.ts","../src/cli/commands/generate-resource.ts","../src/cli/commands/generate-schedule.ts","../src/server/cron/define-cron.ts","../src/cli/commands/generate-types.ts"],"sourcesContent":["import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs'\nimport { dirname, posix, resolve } from 'node:path'\n\nimport { loadConfig } from '../../config/load-config.js'\n\nimport { detectProjectPackageManager, type PackageManager } from './add.js'\nimport { pathTraversalRefusal } from './generate-containment.js'\nimport {\n UNDECIDED_POLICY_IDENT,\n undecidedPolicyDeclaration,\n} from './generate-policy-placeholder.js'\nimport { generateResource } from './generate-resource.js'\nimport { resolveScheduleTarget } from './generate-schedule.js'\nimport {\n VALID_TYPES,\n type GeneratorType,\n type GenerateOptions,\n type GenerateResult,\n type GenerateStatus,\n} from './generate-types.js'\n\nexport {}\n\nfunction toKebabCase(name: string): boolean {\n return /^[a-z][a-z0-9/-]*$/.test(name)\n}\n\n/**\n * Reserved JS identifier names that conflict with object prototype machinery\n * or would shadow built-in module shapes if used as action/route names.\n * Mirrors action-scan's RESERVED_NAMES (T1.4).\n */\nconst RESERVED_BASENAMES = new Set([\n 'index',\n 'constructor',\n '__proto__',\n 'prototype',\n 'hasOwnProperty',\n])\n\n/**\n * Validate that a name segment doesn't hit the reserved-identifier list.\n * Checks the BASENAME (last `/` segment) — nested `admin/constructor` still\n * rejected because the file `constructor.ts` would be the conflict.\n */\nfunction hasReservedSegment(name: string): boolean {\n const basename = name.includes('/') ? (name.split('/').pop() ?? name) : name\n return RESERVED_BASENAMES.has(basename)\n}\n\nfunction toPascalCase(name: string): string {\n return name\n .split(/[-/]/)\n .map((s) => s.charAt(0).toUpperCase() + s.slice(1))\n .join('')\n}\n\nfunction toCamelCase(name: string): string {\n const pascal = toPascalCase(name)\n return pascal.charAt(0).toLowerCase() + pascal.slice(1)\n}\n\nfunction generateRouteTemplate(name: string, displayPath: string): string {\n return [\n `import { route, type AccessDecision } from 'theokit/server/define'`,\n `import { z } from 'zod'`,\n ``,\n ...undecidedPolicyDeclaration(displayPath, 'a stub today is a real handler tomorrow'),\n ``,\n `export const GET = route()`,\n ` .policy(${UNDECIDED_POLICY_IDENT})`,\n ` .handler(({ ctx }) => {`,\n ` return { message: 'TODO: implement ${name} GET' }`,\n ` })`,\n ` .build()`,\n ``,\n ].join('\\n')\n}\n\nfunction generateControllerTemplate(name: string, displayPath: string): string {\n const className = toPascalCase(name) + 'Controller'\n return [\n `// AUTO-GENERATED by \\`theokit generate controller ${name}\\``,\n `import { Controller, Get, UseGuards, type CanActivate } from '@theokit/http'`,\n ``,\n `// TODO(policy): every controller route declares who may call it (ADR 0001), and \\`theokit build\\``,\n `// refuses one that does not. Generated as a REFUSAL (403) rather than @Public(), because the`,\n `// generator cannot know your access model. Replace @UseGuards(UndecidedAccess) in`,\n `// ${displayPath} with your own guard, or with @Public() if the routes are genuinely open,`,\n `// then delete this class.`,\n `class UndecidedAccess implements CanActivate {`,\n ` canActivate(): boolean {`,\n ` return false`,\n ` }`,\n `}`,\n ``,\n `// Served at /api/${name.replace(/\\//g, '-')}: @Controller() infers the prefix from the class name.`,\n `@Controller()`,\n `@UseGuards(UndecidedAccess)`,\n `export class ${className} {`,\n ` @Get()`,\n ` findAll(): string {`,\n ` return 'This action returns all ${name}'`,\n ` }`,\n `}`,\n ``,\n ].join('\\n')\n}\n\nfunction generateActionTemplate(name: string): string {\n const camel = toCamelCase(name)\n return [\n `import { action } from 'theokit/server/define'`,\n `import { z } from 'zod'`,\n ``,\n `export const ${camel} = action()`,\n ` .input(z.object({}))`,\n ` .handler(({ input, ctx }) => {`,\n ` return { message: 'TODO: implement ${name}' }`,\n ` })`,\n ` .build()`,\n ``,\n ].join('\\n')\n}\n\n/**\n * Co-located test template for `theokit generate action <name>` (T5.2 +\n * plan Q4 scenario 1: roundtrip serialize). Skeleton uses vitest BDD shape\n * per testing.md.\n */\nfunction generateAgentTemplate(name: string, displayPath: string): string {\n return [\n `import { AgentBuilder } from '@theokit/agents'`,\n `import type { AccessDecision } from 'theokit/server/define'`,\n `import { z } from 'zod'`,\n ``,\n `// Zero-config: this file is auto-served at POST /api/agents/${name}.`,\n `// Add tools with tool('name')...build() and chain them via .tool(...).`,\n ``,\n ...undecidedPolicyDeclaration(displayPath, 'an agent endpoint runs a model on your key'),\n `export const policy = ${UNDECIDED_POLICY_IDENT}`,\n ``,\n `export default AgentBuilder.create()`,\n ` .input(z.object({ message: z.string() }))`,\n ` .model('openai/gpt-4o-mini')`,\n ` .system('You are a helpful ${name} assistant.')`,\n ` .build()`,\n ``,\n ].join('\\n')\n}\n\nfunction generateToolboxTemplate(name: string): string {\n const camel = toCamelCase(name)\n return [\n `import { tool } from 'theokit/server/define'`,\n `import { z } from 'zod'`,\n ``,\n `export const ${camel}Hello = tool('${name}_hello')`,\n ` .describe('Say hello')`,\n ` .input(z.object({ name: z.string() }))`,\n ` .execute(({ name }) => \\`Hello, \\${name}!\\`)`,\n ` .build()`,\n ``,\n ].join('\\n')\n}\n\nfunction generateWorkflowTemplate(name: string): string {\n const camel = toCamelCase(name)\n return [\n `import { Workflow, fn } from '@theokit/sdk/workflow'`,\n ``,\n `/**`,\n ` * A minimal workflow — a typed, multi-step pipeline (@theokit/sdk). Each \\`fn(...)\\` step is a pure`,\n ` * function; a step can also run an agent via \\`agentStep(name, agent, mapInput)\\`. Import + run it from`,\n ` * a route, action, cron job, or tool:`,\n ` *`,\n ` * const run = await ${camel}Workflow.run({ name: 'Ada' })`,\n ` * console.log(run.status, run.output) // 'completed' 'Hello, Ada!'`,\n ` */`,\n `export const ${camel}Workflow = Workflow.create({ name: '${name}' })`,\n ` .then(fn('normalize', (input: { name: string }) => ({ name: input.name.trim() })))`,\n ` .then(fn('greet', (step: { name: string }) => \\`Hello, \\${step.name}!\\`))`,\n ` .commit()`,\n ``,\n ].join('\\n')\n}\n\nfunction generateEvalTemplate(name: string): string {\n const camel = toCamelCase(name)\n return [\n `import { Eval, Scorers } from '@theokit/sdk/eval'`,\n ``,\n `/**`,\n ` * An eval for your agent (@theokit/sdk) — runs the model over a dataset and scores each output.`,\n ` * To score the REAL agent, mirror its config: point \\`agent.model\\` (and, ideally, a \\`systemPrompt\\`)`,\n ` * at the same values your \\`agents/chat.ts\\` uses. Run it (needs a provider key in the environment):`,\n ` *`,\n ` * const run = await ${camel}Eval.run()`,\n ` * console.log(run.aggregate.meanScore)`,\n ` */`,\n `export const ${camel}Eval = Eval.create({`,\n ` name: '${name}',`,\n ` dataset: [{ input: 'Say ok', expected: 'ok' }],`,\n ` scorers: [Scorers.containsExpected({ caseSensitive: false })],`,\n ` // Mirror your agent: same model (+ systemPrompt) so this evaluates the agent your users talk to.`,\n ` agent: { apiKey: process.env.OPENROUTER_API_KEY ?? '', model: { id: 'openai/gpt-4o-mini' } },`,\n `})`,\n ``,\n ].join('\\n')\n}\n\nfunction generateSandboxTemplate(name: string): string {\n const camel = toCamelCase(name)\n const base = name.split('/').pop() ?? name\n const snake = base.replace(/-/g, '_')\n return [\n `import { tool } from 'theokit/server/define'`,\n `import { LocalSandbox } from '@theokit/sdk/sandbox'`,\n `import { z } from 'zod'`,\n ``,\n `/**`,\n ` * A sandbox-backed agent tool. \\`LocalSandbox\\` (@theokit/sdk) runs a shell command with a timeout +`,\n ` * output cap, isolated from your app process — so the agent can run commands SAFELY. This IS an agent`,\n ` * capability: connect it to your agent with \\`.tool(...)\\`:`,\n ` *`,\n ` * // agents/chat.ts`,\n ` * import { ${camel}Tool } from './sandbox/${base}.js'`,\n ` * AgentBuilder.create().model(...).tool(${camel}Tool).build()`,\n ` */`,\n `export const ${camel}Tool = tool('${snake}')`,\n ` .describe('Run a shell command in an isolated sandbox and return its output.')`,\n ` .input(z.object({ command: z.string().describe('The shell command to run, e.g. \"ls -la\"') }))`,\n ` .execute(async ({ command }) => {`,\n ` const sandbox = new LocalSandbox({ timeoutMs: 5_000 })`,\n ` const { stdout, stderr, exitCode } = await sandbox.execute(command)`,\n ` return exitCode === 0 ? stdout : \\`exit \\${exitCode}: \\${stderr}\\``,\n ` })`,\n ` .build()`,\n ``,\n ].join('\\n')\n}\n\nfunction generateMemoryTemplate(_name: string): string {\n return [\n `/**`,\n ` * Conversation memory — nothing to wire.`,\n ` *`,\n ` * Since @theokit/sdk 4.0, every agent session persists AUTOMATICALLY to a native Claude-shaped`,\n ` * transcript. TheoKit roots it at your app under \\`.data/agent-sessions/projects/<cwd>/<agentId>.jsonl\\`,`,\n ` * keyed by the conversation's session id — a same-session follow-up request resumes prior turns with`,\n ` * zero setup. There is no storage adapter to construct or pass to the agent anymore.`,\n ` *`,\n ` * Keep these transcripts out of git — add \\`.data/\\` to your \\`.gitignore\\`.`,\n ` *`,\n ` * Advanced: to change WHERE sessions are written (e.g. \\`~/.claude\\` so the Claude Code CLI can`,\n ` * \\`--continue\\` them), set the SDK's \\`local.baseDir\\` where you create the agent. Most apps never need to.`,\n ` */`,\n `export {}`,\n ``,\n ].join('\\n')\n}\n\n/** The project directories from `theo.config.ts`, each relative to the project root. */\ninterface ProjectLayout {\n appDir: string\n serverDir: string\n agentsDir: string\n}\n\n/** Where a single-file kind is written, relative to the project root, and what it says. */\ninterface SingleFileKind {\n path: (name: string, layout: ProjectLayout) => string\n /** `displayPath` is the project-relative path, for a template that names its own file. */\n template: (name: string, displayPath: string) => string\n}\n\n/**\n * The single-file kinds. `resource` writes several files and `schedule` checks more than its\n * directory, so neither is here. Every path starts from a directory `theo.config.ts` declares: the\n * scaffold puts them under `src/`, and a hardcoded `server/` wrote files no scanner reads (#1031).\n */\nconst SINGLE_FILE_KINDS: Partial<Record<GeneratorType, SingleFileKind>> = {\n // Agent-capability generators. These live UNDER `agentsDir` — they are facets of the agent\n // domain, not standalone top-level concerns — and the folder-semantic scanner treats each as\n // composition (never a phantom route).\n workflow: {\n path: (name, { agentsDir }) => posix.join(agentsDir, 'workflows', `${name}.ts`),\n template: generateWorkflowTemplate,\n },\n eval: {\n path: (name, { agentsDir }) => posix.join(agentsDir, 'evals', `${name}.ts`),\n template: generateEvalTemplate,\n },\n sandbox: {\n path: (name, { agentsDir }) => posix.join(agentsDir, 'sandbox', `${name}.ts`),\n template: generateSandboxTemplate,\n },\n memory: {\n path: (name, { agentsDir }) => posix.join(agentsDir, 'memory', `${name}.ts`),\n template: generateMemoryTemplate,\n },\n route: {\n path: (name, { serverDir }) => posix.join(serverDir, 'routes', `${name}.ts`),\n template: generateRouteTemplate,\n },\n action: {\n path: (name, { serverDir }) => posix.join(serverDir, 'actions', `${name}.ts`),\n template: generateActionTemplate,\n },\n page: {\n path: (name, { appDir }) => posix.join(appDir, name, 'page.tsx'),\n template: generatePageTemplate,\n },\n ws: {\n path: (name, { serverDir }) => posix.join(serverDir, 'ws', `${name}.ts`),\n template: generateWsTemplate,\n },\n controller: {\n path: (name, { serverDir }) => posix.join(serverDir, 'controllers', `${name}.controller.ts`),\n template: generateControllerTemplate,\n },\n // `<agentsDir>/<name>.ts` is what the agent scanner serves at `/api/agents/<name>`, the URL the\n // template promises. The old `server/agents/<name>.agent.ts` was outside `agentsDir` in both the\n // flat and the scaffold layout, so the agent was never served.\n agent: {\n path: (name, { agentsDir }) => posix.join(agentsDir, `${name}.ts`),\n template: generateAgentTemplate,\n },\n toolbox: {\n path: (name, { serverDir }) => posix.join(serverDir, 'toolboxes', `${name}.tools.ts`),\n template: generateToolboxTemplate,\n },\n}\n\n/** The project layout from `theo.config.ts`, or the refusal naming why it could not be read. */\nasync function readLayout(cwd: string): Promise<ProjectLayout | GenerateResult> {\n try {\n const { appDir, serverDir, agentsDir } = await loadConfig(cwd)\n return { appDir, serverDir, agentsDir }\n } catch (err) {\n return {\n status: 'invalid_config',\n message: `Cannot read ${resolve(cwd, 'theo.config.ts')}: ${(err as Error).message}`,\n }\n }\n}\n\nasync function resolveTemplate(\n cwd: string,\n kind: GeneratorType,\n name: string,\n): Promise<Target | GenerateResult | null> {\n const spec = SINGLE_FILE_KINDS[kind]\n if (spec === undefined) return null\n const layout = await readLayout(cwd)\n if ('status' in layout) return layout\n const displayPath = spec.path(name, layout)\n return { filePath: resolve(cwd, displayPath), content: spec.template(name, displayPath) }\n}\n\nfunction generateActionTestTemplate(name: string): string {\n const camel = toCamelCase(name)\n return [\n `import { describe, it, expect } from 'vitest'`,\n ``,\n `import { ${camel} } from './${name.split('/').pop()}.js'`,\n ``,\n `describe('${name} action', () => {`,\n ` it('should accept a valid input shape', () => {`,\n ` expect(${camel}.input).toBeDefined()`,\n ` expect(typeof ${camel}.handler).toBe('function')`,\n ` })`,\n ``,\n ` it('should reject invalid input via zod schema', () => {`,\n ` const parsed = ${camel}.input.safeParse({ __invalid: true })`,\n ` // Empty object schema accepts {}; tighten this assertion when adding fields.`,\n ` expect(parsed.success).toBe(true)`,\n ` })`,\n `})`,\n ``,\n ].join('\\n')\n}\n\nfunction generatePageTemplate(name: string): string {\n const pascal = toPascalCase(name)\n return [`export default function ${pascal}Page() {`, ` return <h1>${pascal}</h1>`, `}`, ``].join(\n '\\n',\n )\n}\n\nfunction generateWsTemplate(_name: string): string {\n // `websocket()` is the public builder. The `defineWebSocket` this template used to import is not\n // exported by any public entry point, so the generated file failed to load.\n return [\n `import { websocket } from 'theokit/server/define'`,\n ``,\n `export default websocket()`,\n ` .onMessage((ws, data) => {`,\n ` ws.send(\\`echo: \\${data}\\`)`,\n ` })`,\n ` .build()`,\n ``,\n ].join('\\n')\n}\n\n/** A file to write: where it goes and what it says. */\ninterface Target {\n filePath: string\n content: string\n}\n\n/** Whether `value` names a generator kind. */\nfunction isGeneratorType(value: string): value is GeneratorType {\n return (VALID_TYPES as readonly string[]).includes(value)\n}\n\n/**\n * The refusal for options `generate` cannot act on: not a project, an unknown kind, a name that is\n * not kebab-case, or a reserved basename. `undefined` when the options are acceptable.\n */\nfunction refuseOptions({ cwd, type, name }: GenerateOptions): GenerateResult | undefined {\n if (!existsSync(resolve(cwd, 'theo.config.ts')) && !existsSync(resolve(cwd, 'theo.config.js'))) {\n return {\n status: 'not_a_project',\n message: 'Not a Theo project. cwd has no theo.config.ts or theo.config.js',\n }\n }\n\n if (!isGeneratorType(type)) {\n return {\n status: 'invalid_kind',\n message: `Invalid generator type \"${type}\". Available: ${VALID_TYPES.join(', ')}`,\n }\n }\n\n if (!name || !toKebabCase(name)) {\n return {\n status: 'invalid_name',\n message: `Invalid name \"${name}\". Use kebab-case: lowercase letters, numbers, hyphens. Example: my-route`,\n }\n }\n\n // EC-2-related: reject reserved JS identifier basenames (would shadow\n // built-in prototype machinery in virtual module emit or scan).\n if (hasReservedSegment(name)) {\n return {\n status: 'invalid_name',\n message: `Reserved name \"${name}\" — basename collides with built-in identifier (index/constructor/__proto__/prototype/hasOwnProperty).`,\n }\n }\n return undefined\n}\n\n/** Where a single-file kind goes and what it says, or the refusal that explains why it cannot. */\nasync function resolveTarget(\n cwd: string,\n type: GeneratorType,\n name: string,\n): Promise<Target | GenerateResult> {\n // A schedule's directory comes from the project config, so resolving it is async and may refuse.\n if (type === 'schedule') return resolveScheduleTarget(cwd, name)\n const target = await resolveTemplate(cwd, type, name)\n if (target === null) return { status: 'invalid_kind', message: `Unknown type: ${type}` }\n return target\n}\n\n/** Write `target` unless it escapes the project or already exists. */\nfunction writeTarget(\n cwd: string,\n type: GeneratorType,\n name: string,\n { filePath, content }: Target,\n): GenerateResult {\n // EC-4: confirm the resolved filePath stays inside cwd. `toKebabCase` rejects most traversal\n // vectors in the name, but a directory read from config (`agentsDir`) can still point outside.\n const escaped = pathTraversalRefusal(cwd, filePath)\n if (escaped !== undefined) return escaped\n\n if (existsSync(filePath)) {\n return { status: 'already_exists', filePath, kind: type, name }\n }\n\n mkdirSync(dirname(filePath), { recursive: true })\n writeFileSync(filePath, content)\n\n // T5.2 + plan Q4: emit co-located test file alongside actions (only\n // for action type; routes/pages/ws keep prior single-file behavior).\n // Skip if existing test file present (preserve user-customized tests).\n if (type === 'action') {\n const testPath = filePath.replace(/\\.ts$/, '.test.ts')\n if (!existsSync(testPath)) {\n writeFileSync(testPath, generateActionTestTemplate(name))\n }\n }\n\n if (type === 'controller') {\n const message = controllerDependencyNotice(cwd)\n if (message !== undefined) return { status: 'created', filePath, kind: type, name, message }\n }\n return { status: 'created', filePath, kind: type, name }\n}\n\n/**\n * What a controller build needs from the project. The generated file imports `@theokit/http`,\n * which declares `reflect-metadata` as a required peer. `@theokit/http` sends every controller\n * through SWC (`transformControllerSource`) and asks for `@swc/core`, an optional peer, so the\n * build fails without it even for a controller with no parameter decorators.\n */\nconst CONTROLLER_DEPENDENCIES = ['@theokit/http', 'reflect-metadata'] as const\nconst CONTROLLER_DEV_DEPENDENCIES = ['@swc/core'] as const\n\n/**\n * The install command for the packages a controller build needs that `package.json` does not\n * declare, or `undefined` when it declares them all.\n *\n * It reads what the project declares, not what resolves. `theokit` and `@theokit/agents` depend\n * on `@theokit/http`, so npm hoists it into the app's `node_modules` and a resolution check\n * always passed there, while `@swc/core` was missing and `theokit build` failed (#986).\n */\nfunction controllerDependencyNotice(cwd: string): string | undefined {\n const declared = declaredPackages(cwd)\n const missing = CONTROLLER_DEPENDENCIES.filter((pkg) => !declared.names.has(pkg))\n const missingDev = CONTROLLER_DEV_DEPENDENCIES.filter((pkg) => !declared.names.has(pkg))\n if (missing.length === 0 && missingDev.length === 0) return undefined\n\n const pm = detectProjectPackageManager(cwd)\n const commands = [\n ...(missing.length > 0 ? [installCommand(pm, missing, false)] : []),\n ...(missingDev.length > 0 ? [installCommand(pm, missingDev, true)] : []),\n ]\n return [\n `A controller build needs ${[...missing, ...missingDev].join(', ')}, which ${declared.source} does not declare.`,\n `Install before running the app: ${commands.join(' && ')}`,\n ].join(' ')\n}\n\nfunction installCommand(pm: PackageManager, packages: readonly string[], dev: boolean): string {\n const verb = pm === 'npm' ? 'install' : 'add'\n return [pm, verb, ...(dev ? ['-D'] : []), ...packages].join(' ')\n}\n\n/**\n * The package names `package.json` lists under `dependencies` or `devDependencies`, and how to\n * name the file in the notice. A missing or unreadable file declares nothing, and the notice says\n * why, so the user sees the problem rather than an install list that looks wrong.\n */\nfunction declaredPackages(cwd: string): { names: Set<string>; source: string } {\n const path = resolve(cwd, 'package.json')\n if (!existsSync(path)) return { names: new Set(), source: 'the project (it has no package.json)' }\n let manifest: unknown\n try {\n manifest = JSON.parse(readFileSync(path, 'utf-8'))\n } catch (error) {\n const reason = error instanceof Error ? error.message : String(error)\n return { names: new Set(), source: `package.json (it does not parse: ${reason})` }\n }\n const names = new Set<string>()\n if (typeof manifest === 'object' && manifest !== null) {\n for (const field of ['dependencies', 'devDependencies'] as const) {\n const listed = (manifest as Record<string, unknown>)[field]\n if (typeof listed === 'object' && listed !== null) {\n for (const pkg of Object.keys(listed)) names.add(pkg)\n }\n }\n }\n return { names, source: 'package.json' }\n}\n\n/**\n * Programmatic generate. Returns a structured result instead of throwing —\n * Studio (`theokit_generate` tool) consumes this directly. The CLI wrapper\n * below maps the structured result to console output + exit code semantics.\n */\nexport async function generate(opts: GenerateOptions): Promise<GenerateResult> {\n const refused = refuseOptions(opts)\n if (refused !== undefined) return refused\n const { cwd, name } = opts\n // refuseOptions refused every value that is not a generator kind.\n const kind = opts.type as GeneratorType\n\n // Resource generates multiple files — handle separately\n if (kind === 'resource') {\n const layout = await readLayout(cwd)\n if ('status' in layout) return layout\n return generateResource(cwd, name, opts.fields ?? [], layout.serverDir)\n }\n\n const resolved = await resolveTarget(cwd, kind, name)\n if ('status' in resolved) return resolved\n return writeTarget(cwd, kind, name, resolved)\n}\n\n/** The statuses the CLI reports by throwing, and the message each throws. */\nconst CLI_REFUSALS: Record<\n Exclude<GenerateStatus, 'created' | 'already_exists'>,\n (result: GenerateResult, name: string) => string\n> = {\n // The generator's own reason when it gave one: `generate resource` refuses a project that HAS a\n // theo.config.ts when the schema file is missing, and the generic text blamed the config (#1022).\n not_a_project: (result) =>\n result.message ?? 'Not a Theo project. Run this from a project root with theo.config.ts',\n invalid_kind: (result) => result.message ?? 'Invalid kind',\n // `generate` returns invalid_name for a malformed name, a reserved name and a path that\n // resolves outside the project; each message says which, so the CLI must not replace it.\n invalid_name: (result, name) => result.message ?? `Invalid name \"${name}\"`,\n invalid_config: (result) => result.message ?? 'Invalid theo.config.ts',\n agent_not_found: (result) => result.message ?? 'No chat agent found',\n}\n\n/**\n * CLI entry point — preserves the original surface (throws + console.log).\n * Wraps the programmatic `generate` function.\n */\nexport async function generateCommand(\n type: string,\n name: string,\n fields?: string[],\n): Promise<void> {\n const result = await generate({ cwd: process.cwd(), type, name, fields })\n if (result.status === 'already_exists') {\n console.log(`\\n ⚠ ${result.filePath} already exists. Skipping.\\n`)\n } else if (result.status === 'created') {\n console.log(`\\n ✓ Created ${type}: ${result.filePath}\\n`)\n if (result.message !== undefined) console.log(` ${result.message}\\n`)\n } else {\n throw new Error(CLI_REFUSALS[result.status](result, name))\n }\n}\n","/**\n * The one rule every generator applies before writing: the file stays inside the project. Shared\n * by `generate` and the schedule generator, which checks it before looking for the chat agent so an\n * `agentsDir` outside the project is refused as traversal, never reported as a missing agent there.\n */\nimport { isAbsolute, relative, resolve, sep } from 'node:path'\n\nimport type { GenerateResult } from './generate-types.js'\n\n/**\n * Whether `filePath` lies strictly inside `cwd`. Decided by `path.relative`, never by a string\n * prefix: `/tmp/x/app-outside/...` starts with `/tmp/x/app` and is still outside it. A null byte,\n * an absolute path and a `..` escape are all outside.\n */\nfunction isPathInside(cwd: string, filePath: string): boolean {\n if (filePath.includes('\\x00')) return false\n const fromRoot = relative(resolve(cwd), filePath)\n return fromRoot !== '' && !isAbsolute(fromRoot) && fromRoot.split(sep)[0] !== '..'\n}\n\n/** The refusal for a `filePath` outside the project at `cwd`, or `undefined` when it is inside. */\nexport function pathTraversalRefusal(cwd: string, filePath: string): GenerateResult | undefined {\n if (isPathInside(cwd, filePath)) return undefined\n return {\n status: 'invalid_name',\n message: `Path traversal denied: \"${filePath}\" is outside the project root ${resolve(cwd)}.`,\n }\n}\n","/**\n * The access policy a generated route carries until its author decides one.\n *\n * ADR 0001 made an undeclared `policy` a build error because \"a route that nobody had thought\n * about was indistinguishable from a route deliberately left open\". The generators then wrote\n * `'public'` — including on five methods that read and write a database table — so a generated\n * file was the not-thought-about case wearing the deliberate value, which is the precise state\n * the gate exists to make impossible (usetheokit/theokit#416). `theo build` accepted it, because\n * `assertEveryMethodDeclaresPolicy` asks whether a policy was declared, not which one.\n *\n * A generated route now REFUSES. The author still has to write the answer down — the gate's whole\n * purpose — but the value standing in until they do is the safe one rather than the open one.\n *\n * ## Why an AccessDecision and not `() => false`\n *\n * `evaluateRoutePolicy` maps a bare `false` to \"access denied by route policy\", which tells the\n * reader nothing about where to go. The contract already says a denial \"carries its reason so the\n * caller can say it\", so the refusal names its own file: the first `curl` against a forgotten\n * route prints the path to edit.\n *\n * ## Why a named const rather than a lambda per method\n *\n * Five methods would mean five copies, and deleting four of them is the kind of chore that leaves\n * the fifth behind. One const is one thing to replace, and `grep -rn undecidedPolicy` counts the\n * routes still awaiting a decision — which is the number ADR 0001 wanted to make greppable, and\n * which `grep -c \"policy('public')\"` could not give once generated placeholders were mixed in with\n * real decisions.\n */\n\n/** The identifier the generated methods reference. Exported so callers cannot misspell it. */\nexport const UNDECIDED_POLICY_IDENT = 'undecidedPolicy'\n\n/**\n * Lines declaring the placeholder, to splice into a generated module.\n *\n * @param routePath - the generated file's own path, repo-relative, so the refusal can name it\n * @param what - what the route does, in one clause, for the TODO comment\n */\nexport function undecidedPolicyDeclaration(routePath: string, what: string): string[] {\n return [\n `// TODO(policy): every route declares who may call it (ADR 0001). Generated as a REFUSAL`,\n `// rather than 'public', because the generator cannot know your access model —`,\n `// ${what}.`,\n `// Replace this with 'public' if the route is genuinely open, or with a check such as`,\n `// \\`({ subject, params }) => requireOwner(subject, ownerOf(params.id))\\`, then delete the const.`,\n `const ${UNDECIDED_POLICY_IDENT} = (): AccessDecision => ({`,\n ` allowed: false,`,\n ` reason:`,\n ` '${routePath} still carries the policy the generator wrote, which refuses everyone. ' +`,\n ` 'Declare who may call this route (ADR 0001).',`,\n `})`,\n ]\n}\n","import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs'\nimport { dirname, join, resolve } from 'node:path'\n\nimport {\n UNDECIDED_POLICY_IDENT,\n undecidedPolicyDeclaration,\n} from './generate-policy-placeholder.js'\nimport type { GenerateResult } from './generate-types.js'\n\n// --- Field parsing ---\n\nconst ALLOWED_FIELD_TYPES = new Set(['string', 'text', 'number', 'boolean'])\nconst RESERVED_FIELDS = new Set(['id', 'createdAt', 'created_at'])\n\ninterface ResourceField {\n name: string\n type: 'string' | 'text' | 'number' | 'boolean'\n zodType: string\n}\n\nfunction parseResourceFields(args: string[]): ResourceField[] {\n if (args.length === 0) {\n throw new Error(\n 'Resource requires at least one field. Example: theokit generate resource posts title:string',\n )\n }\n return args.map((arg) => {\n const parts = arg.split(':')\n if (parts.length !== 2 || !parts[0] || !parts[1]) {\n throw new Error(`Invalid field \"${arg}\". Use name:type format (e.g. title:string)`)\n }\n const [name, type] = parts\n if (!ALLOWED_FIELD_TYPES.has(type)) {\n throw new Error(\n `Unknown field type \"${type}\" in \"${arg}\". Valid types: ${[...ALLOWED_FIELD_TYPES].join(', ')}`,\n )\n }\n if (RESERVED_FIELDS.has(name)) {\n throw new Error(`Field \"${name}\" is reserved (auto-generated by the framework)`)\n }\n const fieldType = type as ResourceField['type']\n return {\n name,\n type: fieldType,\n zodType: mapZodType(fieldType),\n }\n })\n}\n\n// --- Dialects ---\n\n/**\n * The SQL dialects the generator writes code for. Drizzle's synchronous SQLite drivers expose\n * `.all()`/`.get()`/`.run()` on a query; the Postgres drivers do not, so the two need different\n * table builders AND different handler bodies (usetheokit/theokit#1022).\n */\ntype ResourceDialect = 'sqlite' | 'postgresql'\n\ninterface DialectSpec {\n /** The Drizzle module the table builders come from. */\n readonly module: string\n /** A minimal `<serverDir>/db/index.ts` exporting `db`, shown when the project has none. */\n readonly dbIndexExample: readonly string[]\n /** Matches an existing named import from `module`; group 1 is the list of names. */\n readonly importPattern: RegExp\n /** The table builder function. */\n readonly table: string\n /** Every column builder the generated table may use, imported up front. */\n readonly columns: readonly string[]\n readonly idColumn: string\n readonly createdAtColumn: readonly string[]\n readonly column: (name: string, type: ResourceField['type']) => string\n}\n\nconst DIALECTS: Record<ResourceDialect, DialectSpec> = {\n sqlite: {\n module: 'drizzle-orm/sqlite-core',\n dbIndexExample: [\n `import Database from 'better-sqlite3'`,\n `import { drizzle } from 'drizzle-orm/better-sqlite3'`,\n `import * as schema from './schema.js'`,\n `export const db = drizzle(new Database('data.db'), { schema })`,\n ],\n importPattern: /import\\s*\\{([^}]*)\\}\\s*from\\s*['\"]drizzle-orm\\/sqlite-core['\"]/,\n table: 'sqliteTable',\n columns: ['integer', 'text'],\n idColumn: `integer('id').primaryKey({ autoIncrement: true })`,\n createdAtColumn: [\n `text('created_at')`,\n ` .notNull()`,\n ` .$defaultFn(() => new Date().toISOString().split('T')[0])`,\n ],\n column: (name, type) => {\n switch (type) {\n case 'string':\n case 'text':\n return `text('${name}').notNull()`\n case 'number':\n return `integer('${name}').notNull()`\n case 'boolean':\n return `integer('${name}', { mode: 'boolean' }).notNull().default(false)`\n }\n },\n },\n postgresql: {\n module: 'drizzle-orm/pg-core',\n dbIndexExample: [\n `import pg from 'pg'`,\n `import { drizzle } from 'drizzle-orm/node-postgres'`,\n `import * as schema from './schema.js'`,\n `const pool = new pg.Pool({ connectionString: process.env.DATABASE_URL })`,\n `pool.on('error', (err) => console.warn('[pg] idle client error:', err.message))`,\n `export const db = drizzle(pool, { schema })`,\n ],\n importPattern: /import\\s*\\{([^}]*)\\}\\s*from\\s*['\"]drizzle-orm\\/pg-core['\"]/,\n table: 'pgTable',\n columns: ['boolean', 'integer', 'serial', 'text', 'timestamp'],\n idColumn: `serial('id').primaryKey()`,\n createdAtColumn: [`timestamp('created_at').notNull().defaultNow()`],\n column: (name, type) => {\n switch (type) {\n case 'string':\n case 'text':\n return `text('${name}').notNull()`\n case 'number':\n return `integer('${name}').notNull()`\n case 'boolean':\n return `boolean('${name}').notNull().default(false)`\n }\n },\n },\n}\n\n/** A `dialect:` value in `drizzle.config.ts`, mapped to what the generator does with it. */\nconst DIALECT_ALIASES: Record<string, ResourceDialect | undefined> = {\n sqlite: 'sqlite',\n turso: 'sqlite',\n postgresql: 'postgresql',\n}\n\nconst SCHEMA_IMPORT_DIALECTS: [RegExp, string][] = [\n [/from ['\"]drizzle-orm\\/sqlite-core['\"]/, 'sqlite'],\n [/from ['\"]drizzle-orm\\/pg-core['\"]/, 'postgresql'],\n [/from ['\"]drizzle-orm\\/mysql-core['\"]/, 'mysql'],\n [/from ['\"]drizzle-orm\\/singlestore-core['\"]/, 'singlestore'],\n]\n\n/**\n * The project's dialect: `drizzle.config.ts` says it outright; failing that, the core module the\n * schema already imports from does. A schema that imports from nothing (a fresh file) and no config\n * leaves the historical default, SQLite.\n *\n * @returns the dialect, or the raw name of one the generator cannot write code for.\n */\nfunction detectDialect(cwd: string, schemaSource: string): string {\n const config = readIfPresent(resolve(cwd, 'drizzle.config.ts'))\n if (config !== undefined) {\n const declared = /dialect\\s*:\\s*['\"]([a-z0-9-]+)['\"]/.exec(config)\n if (declared) return declared[1]\n }\n for (const [pattern, dialect] of SCHEMA_IMPORT_DIALECTS) {\n if (pattern.test(schemaSource)) return dialect\n }\n return 'sqlite'\n}\n\nfunction mapZodType(type: ResourceField['type']): string {\n switch (type) {\n case 'string':\n case 'text':\n return 'z.string()'\n case 'number':\n return 'z.number()'\n case 'boolean':\n return 'z.boolean()'\n }\n}\n\n// --- Template generators ---\n\nfunction generateSchemaEntry(\n resourceName: string,\n fields: ResourceField[],\n dialect: DialectSpec,\n): string {\n const [createdAtFirst, ...createdAtRest] = dialect.createdAtColumn\n const cols = [\n ` id: ${dialect.idColumn},`,\n ...fields.map((f) => ` ${f.name}: ${dialect.column(f.name, f.type)},`),\n ` createdAt: ${createdAtFirst}`,\n ...createdAtRest,\n ]\n cols[cols.length - 1] += ','\n return [\n ``,\n `export const ${resourceName} = ${dialect.table}('${resourceName}', {`,\n ...cols,\n `})`,\n ``,\n ].join('\\n')\n}\n\n/**\n * The schema source with the table builders the appended table uses imported. The generator used to\n * append `sqliteTable(...)` with no import at all, so the schema file stopped compiling.\n *\n * An existing named import from the dialect module gains the missing names; without one, an import\n * line goes on top. Names already imported are not repeated.\n */\nfunction withDialectImports(source: string, dialect: DialectSpec): string {\n const needed = [dialect.table, ...dialect.columns]\n const existing = dialect.importPattern.exec(source)\n if (existing) {\n const have = existing[1]\n .split(',')\n .map((n) => n.trim())\n .filter(Boolean)\n const merged = [...new Set([...have, ...needed])].sort((a, b) => a.localeCompare(b))\n return source.replace(existing[0], `import { ${merged.join(', ')} } from '${dialect.module}'`)\n }\n return `import { ${[...needed].sort((a, b) => a.localeCompare(b)).join(', ')} } from '${dialect.module}'\\n${source}`\n}\n\n/**\n * Query expressions per dialect. SQLite's synchronous drivers finish a query with `.all()`, `.get()`\n * or `.run()`; the Postgres drivers return a promise and have none of the three, so the Postgres\n * handlers await the builder and take the first row of `returning()`.\n */\ninterface QueryShapes {\n readonly async: boolean\n list(table: string): string\n insert(table: string): string\n findById(table: string): string\n updateById(table: string): string\n deleteById(table: string): string\n}\n\nconst QUERIES: Record<ResourceDialect, QueryShapes> = {\n sqlite: {\n async: false,\n list: (t) => `db.select().from(${t}).all()`,\n insert: (t) => `db.insert(${t}).values(body).returning().get()`,\n findById: (t) => `db.select().from(${t}).where(eq(${t}.id, params.id)).get()`,\n updateById: (t) => `db.update(${t}).set(body).where(eq(${t}.id, params.id)).returning().get()`,\n deleteById: (t) => `db.delete(${t}).where(eq(${t}.id, params.id)).run()`,\n },\n postgresql: {\n async: true,\n list: (t) => `await db.select().from(${t})`,\n insert: (t) => `(await db.insert(${t}).values(body).returning())[0]`,\n findById: (t) => `(await db.select().from(${t}).where(eq(${t}.id, params.id)))[0]`,\n updateById: (t) =>\n `(await db.update(${t}).set(body).where(eq(${t}.id, params.id)).returning())[0]`,\n deleteById: (t) => `await db.delete(${t}).where(eq(${t}.id, params.id))`,\n },\n}\n\ninterface RouteTemplateInput {\n resourceName: string\n fields: ResourceField[]\n queries: QueryShapes\n /** Project-relative path of the file being written, named by the placeholder policy. */\n displayPath: string\n}\n\nfunction generateRouteIndex({\n resourceName,\n fields,\n queries,\n displayPath,\n}: RouteTemplateInput): string {\n const bodyFields = fields.map((f) => ` ${f.name}: ${f.zodType},`).join('\\n')\n const asyncKw = queries.async ? 'async ' : ''\n return [\n `import { route, type AccessDecision } from 'theokit/server/define'`,\n `import { z } from 'zod'`,\n `import { db } from '../../db/index.js'`,\n `import { ${resourceName} } from '../../db/schema.js'`,\n ``,\n ...undecidedPolicyDeclaration(displayPath, 'these methods read and write a table'),\n ``,\n `export const GET = route()`,\n ` .policy(${UNDECIDED_POLICY_IDENT})`,\n ` .handler(${asyncKw}() => ${queries.list(resourceName)})`,\n ` .build()`,\n ``,\n `export const POST = route()`,\n ` .policy(${UNDECIDED_POLICY_IDENT})`,\n ` .body(`,\n ` z.object({`,\n bodyFields,\n ` }),`,\n ` )`,\n ` .status(201)`,\n ` .handler(${asyncKw}({ body }) => {`,\n ` const result = ${queries.insert(resourceName)}`,\n ` return result`,\n ` })`,\n ` .build()`,\n ``,\n ].join('\\n')\n}\n\nfunction generateRouteId({\n resourceName,\n fields,\n queries,\n displayPath,\n}: RouteTemplateInput): string {\n const updateFields = fields.map((f) => ` ${f.name}: ${f.zodType}.optional(),`).join('\\n')\n const asyncKw = queries.async ? 'async ' : ''\n return [\n `import { route, type AccessDecision } from 'theokit/server/define'`,\n `import { z } from 'zod'`,\n `import { db } from '../../db/index.js'`,\n `import { ${resourceName} } from '../../db/schema.js'`,\n `import { eq } from 'drizzle-orm'`,\n ``,\n ...undecidedPolicyDeclaration(\n displayPath,\n 'these methods read, update and delete rows of a table',\n ),\n ``,\n `export const GET = route()`,\n ` .policy(${UNDECIDED_POLICY_IDENT})`,\n ` .params(z.object({ id: z.coerce.number() }))`,\n ` .handler(${asyncKw}({ params }) => {`,\n ` const item = ${queries.findById(resourceName)}`,\n ` if (!item) return new Response(JSON.stringify({ error: 'Not found' }), { status: 404 })`,\n ` return item`,\n ` })`,\n ` .build()`,\n ``,\n `export const PUT = route()`,\n ` .policy(${UNDECIDED_POLICY_IDENT})`,\n ` .params(z.object({ id: z.coerce.number() }))`,\n ` .body(`,\n ` z.object({`,\n updateFields,\n ` }),`,\n ` )`,\n ` .handler(${asyncKw}({ params, body }) => {`,\n ` const result = ${queries.updateById(resourceName)}`,\n ` if (!result) return new Response(JSON.stringify({ error: 'Not found' }), { status: 404 })`,\n ` return result`,\n ` })`,\n ` .build()`,\n ``,\n `export const DELETE = route()`,\n ` .policy(${UNDECIDED_POLICY_IDENT})`,\n ` .params(z.object({ id: z.coerce.number() }))`,\n ` .status(204)`,\n ` .handler(${asyncKw}({ params }) => {`,\n ` ${queries.deleteById(resourceName)}`,\n ` })`,\n ` .build()`,\n ``,\n ].join('\\n')\n}\n\nfunction generateTestTemplate(resourceName: string, serverDir: string): string {\n return [\n `import { describe, it, expect } from 'vitest'`,\n ``,\n `describe('${resourceName} API', () => {`,\n ` it('should have a schema defined', async () => {`,\n ` const schema = await import('../${serverDir}/db/schema.js')`,\n ` expect(schema.${resourceName}).toBeDefined()`,\n ` })`,\n ``,\n ` it('should have route handlers', async () => {`,\n ` const index = await import('../${serverDir}/routes/${resourceName}/index.js')`,\n ` expect(index.GET).toBeDefined()`,\n ` expect(index.POST).toBeDefined()`,\n ` })`,\n `})`,\n ``,\n ].join('\\n')\n}\n\n// --- Orchestrator ---\n\n/**\n * Generate a CRUD resource: a table appended to `<serverDir>/db/schema.ts`, two route files under\n * `<serverDir>/routes/<name>/` and a test.\n *\n * @param serverDir - the project-relative `serverDir` from `theo.config.ts`. The generator used to\n * assume `server/`, so in the scaffold (`src/server`) it refused every time.\n */\nfunction readIfPresent(path: string): string | undefined {\n try {\n return readFileSync(path, 'utf-8')\n } catch (err) {\n if ((err as NodeJS.ErrnoException).code === 'ENOENT') return undefined\n throw err\n }\n}\n\n/** The files TypeScript resolves `../../db/index.js` to. */\nconst DB_INDEX_FILES = ['index.ts', 'index.tsx', 'index.js']\n\n/** Create `path` only if it does not exist; `false` when it already did. */\nfunction createExclusive(path: string, content: string): boolean {\n try {\n writeFileSync(path, content, { flag: 'wx' })\n return true\n } catch (err) {\n if ((err as NodeJS.ErrnoException).code === 'EEXIST') return false\n throw err\n }\n}\n\nexport function generateResource(\n cwd: string,\n name: string,\n fieldArgs: string[],\n serverDir = 'server',\n): GenerateResult {\n let fields: ResourceField[]\n try {\n fields = parseResourceFields(fieldArgs)\n } catch (err) {\n return { status: 'invalid_name', message: err instanceof Error ? err.message : String(err) }\n }\n\n const schemaRel = join(serverDir, 'db', 'schema.ts')\n const routeDirRel = join(serverDir, 'routes', name)\n const schemaPath = resolve(cwd, schemaRel)\n const routeIndexPath = resolve(cwd, routeDirRel, 'index.ts')\n const routeIdPath = resolve(cwd, routeDirRel, '[id].ts')\n const testPath = resolve(cwd, `tests/${name}.test.ts`)\n\n // Read rather than check-then-read, and create the new files with `wx` rather than\n // check-then-write: the answer can change between a check and the write that trusts it.\n const existingSchema = readIfPresent(schemaPath)\n if (existingSchema === undefined) {\n return {\n status: 'not_a_project',\n message:\n `${schemaRel} not found. \\`theokit generate resource\\` appends the table to the Drizzle ` +\n `schema under the configured serverDir (\"${serverDir}\"); create it first.`,\n }\n }\n if (existingSchema.includes(`export const ${name} =`)) {\n return {\n status: 'already_exists',\n filePath: schemaPath,\n kind: 'resource',\n name,\n message: `Table \"${name}\" already exists in ${schemaRel}`,\n }\n }\n\n const detected = detectDialect(cwd, existingSchema)\n const dialectName = DIALECT_ALIASES[detected]\n if (dialectName === undefined) {\n return {\n status: 'invalid_config',\n message:\n `\\`theokit generate resource\\` writes code for SQLite and PostgreSQL; this project uses ` +\n `\"${detected}\" (from drizzle.config.ts or the imports in ${schemaRel}). Nothing was written.`,\n }\n }\n const dialect = DIALECTS[dialectName]\n const queries = QUERIES[dialectName]\n\n // The routes import `db` from `<serverDir>/db/index.js`. Without the file the command reported\n // success and the next build failed with `Could not resolve` (usetheokit/theokit#1038). Which\n // driver to install is the project's choice, so the generator refuses rather than writing one.\n const dbIndexRel = join(serverDir, 'db', 'index.ts')\n if (!DB_INDEX_FILES.some((file) => existsSync(resolve(cwd, serverDir, 'db', file)))) {\n const example = dialect.dbIndexExample.map((line) => ' ' + line).join('\\n')\n return {\n status: 'not_a_project',\n message:\n `${dbIndexRel} not found. The routes \\`theokit generate resource\\` writes import \\`db\\` ` +\n `from it, so the build would fail to resolve it. Create it with \\`export const db\\`, for ` +\n `example (${dialectName}):\\n\\n${example}\\n\\n` +\n `Nothing was written.`,\n }\n }\n\n // 1. Create the route files first: an existing route refuses before the schema is touched.\n mkdirSync(dirname(routeIndexPath), { recursive: true })\n const template = { resourceName: name, fields, queries }\n if (\n !createExclusive(\n routeIndexPath,\n generateRouteIndex({ ...template, displayPath: join(routeDirRel, 'index.ts') }),\n )\n ) {\n return { status: 'already_exists', filePath: routeIndexPath, kind: 'resource', name }\n }\n writeFileSync(\n routeIdPath,\n generateRouteId({ ...template, displayPath: join(routeDirRel, '[id].ts') }),\n )\n\n // 2. Append the table, with the builders it uses imported.\n writeFileSync(\n schemaPath,\n withDialectImports(existingSchema, dialect) + generateSchemaEntry(name, fields, dialect),\n )\n\n // 3. Create the test file, keeping one the user already has.\n mkdirSync(dirname(testPath), { recursive: true })\n createExclusive(testPath, generateTestTemplate(name, serverDir))\n\n return { status: 'created', filePath: routeIndexPath, kind: 'resource', name }\n}\n","/**\n * The `theokit generate schedule` generator.\n *\n * A schedule must land where `theokit build` scans (`<agentsDir>/schedules`), so its directory is\n * read from the project config through `loadConfig`, the same reader the build uses. When the\n * config cannot be read the generator refuses by name instead of guessing a directory.\n *\n * A schedule runs the app's `chat` agent, so a project without one is refused too: a schedule\n * importing a module that does not exist would make `theokit build` fail on it.\n *\n * The schedule declares `defineCron('<last segment of the name>')`, so that segment is checked with\n * `defineCron`'s own rule and the scanner's own discovery: a name the build would reject, skip, or\n * find already used by another cron file is refused as `invalid_name` before anything is written.\n */\nimport { basename, dirname, extname, relative, resolve, sep } from 'node:path'\n\nimport { loadConfig } from '../../config/load-config.js'\nimport { cronScanDirs, isDiscoverableCronFile, listCronFiles } from '../../server/cron/cron-scan.js'\nimport { CRON_NAME_RULE, isValidCronName } from '../../server/cron/define-cron.js'\nimport { findAgentFile } from '../../server/scan/agent-scan.js'\n\nimport { pathTraversalRefusal } from './generate-containment.js'\nimport type { GenerateResult } from './generate-types.js'\n\n/** Where the schedule goes and what it says, or the refusal that explains why it cannot be written. */\nexport async function resolveScheduleTarget(\n cwd: string,\n name: string,\n): Promise<{ filePath: string; content: string } | GenerateResult> {\n const malformed = cronNameRefusal(name)\n if (malformed !== undefined) return malformed\n let config: Awaited<ReturnType<typeof loadConfig>>\n try {\n config = await loadConfig(cwd)\n } catch (err) {\n return {\n status: 'invalid_config',\n message: `Cannot read ${resolve(cwd, 'theo.config.ts')}: ${(err as Error).message}`,\n }\n }\n const { agentsDir, serverDir } = config\n const agentsRoot = resolve(cwd, agentsDir)\n const cronDirs = cronScanDirs(cwd, serverDir, agentsDir)\n const filePath = resolve(cronDirs.schedules, `${name}.ts`)\n // Before the agent lookup: a chat agent missing from a directory outside the project is not\n // something to create, and the refusal must name the escape, not the missing file.\n const escaped = pathTraversalRefusal(cwd, filePath)\n if (escaped !== undefined) return escaped\n // The agent scanner's own rule, so the file found is the one the build serves as `chat`.\n const chatPath = findAgentFile(agentsRoot, 'chat')\n if (chatPath === undefined) {\n return {\n status: 'agent_not_found',\n message:\n `No \"chat\" agent in ${agentsRoot} (the build serves it from chat.{ts,tsx,js,jsx} or ` +\n 'chat/index.{ts,tsx,js,jsx}). A schedule runs the chat agent; create it first.',\n }\n }\n // The build scans both homes with one duplicate-name guard (build.ts, emitCronArtifacts).\n const taken = cronNameTaken(name, filePath, [cronDirs.crons, cronDirs.schedules])\n if (taken !== undefined) return taken\n return { filePath, content: generateScheduleTemplate(name, importSpecifier(filePath, chatPath)) }\n}\n\n/** The cron name a schedule called `name` declares: its last `/` segment. */\nexport function cronNameOf(name: string): string {\n return name.split('/').pop() ?? name\n}\n\n/** A refusal when the build would skip the file or `defineCron` would reject its name. */\nfunction cronNameRefusal(name: string): GenerateResult | undefined {\n const cronName = cronNameOf(name)\n if (!isDiscoverableCronFile(`${cronName}.ts`)) {\n return {\n status: 'invalid_name',\n message: `Invalid schedule name \"${name}\": the build does not discover a cron file named \"${cronName}.ts\".`,\n }\n }\n if (!isValidCronName(cronName)) {\n return {\n status: 'invalid_name',\n message: `Invalid schedule name \"${name}\": its cron name \"${cronName}\" is one defineCron rejects. ${CRON_NAME_RULE}`,\n }\n }\n return undefined\n}\n\n/**\n * A refusal when another cron file already carries this schedule's cron name. The generator names\n * a cron after its file, so a file of the same name in either cron home is the clash the build's\n * duplicate-name guard would fail on. The target itself is left to the `already_exists` answer.\n *\n * Compared by FILE name, while the build compares the name each file passes to `defineCron`, which\n * it learns only by importing the file. A hand-written `foo.ts` declaring `defineCron('bar')` is\n * not seen here, so schedule `bar` is written and the build then fails with\n * `DuplicateCronNameError`; and a `bar.ts` declaring another name refuses schedule `bar` although\n * the build would accept it. Importing every cron file to read its name would run user code at\n * generate time, which this generator does not do.\n */\nfunction cronNameTaken(\n name: string,\n filePath: string,\n cronDirs: readonly string[],\n): GenerateResult | undefined {\n const cronName = cronNameOf(name)\n const clash = listCronFiles(cronDirs).find(\n (path) => path !== filePath && basename(path, extname(path)) === cronName,\n )\n if (clash === undefined) return undefined\n return {\n status: 'invalid_name',\n message: `Invalid schedule name \"${name}\": the cron name \"${cronName}\" is already used by ${clash}. Cron names must be unique across server/crons/ and agents/schedules/.`,\n }\n}\n\n/**\n * The ESM specifier the schedule at `fromFile` uses to import `toFile` (`../chat.js`). A TypeScript\n * source is imported by its `.js` name, as TypeScript and tsx resolve it; a `.js` or `.jsx` file\n * keeps its own extension.\n */\nexport function importSpecifier(fromFile: string, toFile: string): string {\n const path = relative(dirname(fromFile), toFile)\n .split(sep)\n .join('/')\n .replace(/\\.tsx?$/, '.js')\n return path.startsWith('.') ? path : `./${path}`\n}\n\nfunction generateScheduleTemplate(name: string, chatSpecifier: string): string {\n const base = cronNameOf(name)\n return [\n `import { defineCron } from 'theokit/server/cron'`,\n `import { resolveProvider } from 'theokit/server/agent'`,\n `import { streamAgentTurnInProcess } from '@theokit/agents'`,\n ``,\n `import chat from '${chatSpecifier}'`,\n ``,\n `/**`,\n ` * A scheduled agent run, a first-class TheoKit cron. \\`theokit build\\` discovers it and translates the`,\n ` * schedule to your deploy target's native cron; \\`theokit start\\` runs it in-process.`,\n ` *`,\n ` * Each fire is one model run of your \\`chat\\` agent, so it spends tokens. Nobody is present to approve`,\n ` * a gated tool, so the run uses the \\`auto-reject\\` posture: every approval is answered \"no\" and the`,\n ` * gated tool does not run.`,\n ` *`,\n ` * Schedules are UTC (https://crontab.guru). \\`signal\\` aborts when the scheduler stops.`,\n ` */`,\n `export default defineCron('${base}', {`,\n ` schedule: '0 9 * * *', // every day at 09:00 UTC`,\n ` async handler({ traceId, scheduledAt, signal }) {`,\n ` let outcome = 'ok'`,\n ` try {`,\n ` const agent = chat as { model?: string; plugins?: readonly unknown[] }`,\n ` const { apiKey } = resolveProvider(agent.model, { plugins: agent.plugins })`,\n ` const run = streamAgentTurnInProcess(chat, apiKey, {`,\n ` message: \\`Scheduled run \"${base}\" at \\${scheduledAt.toISOString()}\\`,`,\n ` signal,`,\n ` approvals: {`,\n ` kind: 'auto-reject',`,\n ` reason: 'unattended schedule \"${base}\": nobody is present to approve',`,\n ` },`,\n ` })`,\n ` for await (const chunk of run) {`,\n ` // The SDK can report a failed run as a chunk rather than a rejection.`,\n ` if (chunk.type === 'error') {`,\n ` throw new Error(\\`[${base}] agent run failed: \\${chunk.errorText ?? 'no error text'}\\`)`,\n ` }`,\n ` }`,\n ` } catch (err) {`,\n ` outcome = \\`failed: \\${err instanceof Error ? err.message : String(err)}\\``,\n ` throw err`,\n ` } finally {`,\n ` // One traced line per fire, failed fires included: the scheduler's own error line has no trace id.`,\n ` console.log(\\`[${base}] fire \\${outcome} at \\${scheduledAt.toISOString()} (trace \\${traceId})\\`)`,\n ` }`,\n ` },`,\n `})`,\n ``,\n ].join('\\n')\n}\n","import type { CronDefinition, CronOptions } from './cron-types.js'\nimport { validateCronSchedule } from './cron-validate.js'\n\nconst NAME_RE = /^[a-z0-9][a-z0-9-]{0,63}$/\n\n/** What {@link isValidCronName} requires, in words, for an error message. */\nexport const CRON_NAME_RULE =\n 'Must be 1-64 chars, lowercase alphanumeric + hyphen, starting with [a-z0-9].'\n\n/** Whether `defineCron` accepts `name`. The generator asks this before writing a cron. */\nexport function isValidCronName(name: string): boolean {\n return typeof name === 'string' && NAME_RE.test(name)\n}\n\nfunction validateName(name: string): void {\n if (!isValidCronName(name)) {\n throw new Error(`defineCron: invalid name \"${name}\". ${CRON_NAME_RULE}`)\n }\n}\n\n/**\n * Declare a time-triggered handler. Pure identity helper — no\n * registration side effect; the build-time scanner (T1.3) discovers\n * definitions by walking `server/crons/` and emits a manifest the\n * adapters translate at deploy.\n *\n * @example\n * ```ts\n * // server/crons/morning-summary.ts\n * export default defineCron('morning-summary', {\n * schedule: '0 9 * * *', // 09:00 UTC\n * async handler({ traceId, scheduledAt, signal }) {\n * // ...\n * },\n * })\n * ```\n */\nexport function defineCron(name: string, options: CronOptions): CronDefinition {\n validateName(name)\n validateCronSchedule(options.schedule)\n return {\n name,\n schedule: options.schedule,\n handler: options.handler,\n concurrency: options.concurrency ?? 'forbid',\n }\n}\n","/**\n * Shared types for the `theokit generate` CLI command family.\n *\n * Extracted to break the circular dependency between generate.ts ↔ generate-resource.ts\n * (architecture-remediation plan T1.1, 2026-06-12).\n */\n\nexport const VALID_TYPES = [\n 'route',\n 'action',\n 'page',\n 'ws',\n 'controller',\n 'agent',\n 'toolbox',\n 'workflow',\n 'eval',\n 'sandbox',\n 'schedule',\n 'memory',\n 'resource',\n] as const\nexport type GeneratorType = (typeof VALID_TYPES)[number]\n\nexport interface GenerateOptions {\n cwd: string\n type: string\n name: string\n fields?: string[]\n}\n\nexport type GenerateStatus =\n | 'created'\n | 'already_exists'\n | 'invalid_kind'\n | 'invalid_name'\n | 'not_a_project'\n | 'invalid_config'\n | 'agent_not_found'\n\nexport interface GenerateResult {\n status: GenerateStatus\n filePath?: string\n kind?: GeneratorType\n name?: string\n message?: string\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;AAAA,SAAS,cAAAA,aAAY,aAAAC,YAAW,gBAAAC,eAAc,iBAAAC,sBAAqB;AACnE,SAAS,WAAAC,UAAS,OAAO,WAAAC,gBAAe;;;ACIxC,SAAS,YAAY,UAAU,SAAS,WAAW;AASnD,SAAS,aAAa,KAAa,UAA2B;AAC5D,MAAI,SAAS,SAAS,IAAM,EAAG,QAAO;AACtC,QAAM,WAAW,SAAS,QAAQ,GAAG,GAAG,QAAQ;AAChD,SAAO,aAAa,MAAM,CAAC,WAAW,QAAQ,KAAK,SAAS,MAAM,GAAG,EAAE,CAAC,MAAM;AAChF;AAGO,SAAS,qBAAqB,KAAa,UAA8C;AAC9F,MAAI,aAAa,KAAK,QAAQ,EAAG,QAAO;AACxC,SAAO;AAAA,IAC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name","resolve","resolve","resolve","dirname","relative","resolve","sep","resolve","relative","dirname","sep","basename","resolve","existsSync","mkdirSync","dirname","writeFileSync","readFileSync"]}
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../src/cli/commands/start/index.ts","../src/server/define/health-route.ts","../src/server/rate-limit/client-ip.ts","../src/server/rate-limit/rate-limit-per-route.ts","../src/cli/commands/start/assert-sdk-compatible.ts","../src/server/agent/sdk-compat.ts","../src/cli/commands/start/background-work.ts","../src/server/cron/cron-lock-from-config.ts","../src/cli/commands/start/graceful-shutdown.ts","../src/cli/commands/start/manifest-loader.ts","../src/cli/commands/start/public-exposure-gate.ts","../src/cli/commands/start/handlers.ts","../src/server/http/static.ts","../src/cli/commands/start/ssr-setup.ts","../src/cli/commands/start/request-handler.ts","../src/cli/commands/start/resolve-listen-host.ts","../src/cli/commands/start/websocket-handler.ts"],"sourcesContent":["/**\n * theokit start — production server orchestration spine.\n *\n * T4.2 (architecture-cleanup, ADR-0017): stages extracted to sibling modules.\n * - start-bootstrap-stages.ts — config/registry/storage bootstrap + resolveSsrEntry\n * - start-manifest-loader.ts — manifest.json or scan fallback\n * - start-ssr-setup.ts — SSR entry-server + HTML template split\n * - start-handlers.ts — branch handlers (action/route/static/404)\n * - start-request-handler.ts — request lifecycle wiring\n * - start-websocket-handler.ts — WS upgrade (opt-in)\n * - start-graceful-shutdown.ts — SIGTERM/SIGINT drain\n */\n\nimport { existsSync, readFileSync } from 'node:fs'\nimport { createServer } from 'node:http'\nimport { join, resolve } from 'node:path'\n\nimport { loadConfig } from '../../../config/load-config.js'\nimport { loadEnv } from '../../../config/load-env.js'\nimport { resolvePluginSpecifiers } from '../../../config/resolve-plugin-specifiers.js'\nimport { initCacheEngineFromConfig } from '../../../server/cache-bootstrap.js'\nimport { defineHealthRoute } from '../../../server/define/health-route.js'\nimport { createCorsHandler } from '../../../server/http/cors.js'\nimport { jobBackendFromConfig } from '../../../server/jobs/job-backend-from-config.js'\nimport { createObservabilityPluginFromConfig } from '../../../server/observability-bootstrap.js'\nimport { createPluginRunnerFromConfig } from '../../../server/plugins/load-plugins.js'\nimport { createRouteRateLimiter } from '../../../server/rate-limit/rate-limit-per-route.js'\nimport { createProductionLoader } from '../../../server/scan/module-loader.js'\nimport { resolveTransformer } from '../../../server/transformer.js'\nimport { preflightNodeAndBindings } from '../../preflight-node-version.js'\nimport { CONTROLLER_MANIFEST_FILE } from '../build/emit-controllers.js'\n\nimport { assertSdkCompatible } from './assert-sdk-compatible.js'\nimport { startBackgroundWork } from './background-work.js'\nimport {\n configureAgentRegistryFromConfig,\n configureStorageManagerFromConfig,\n} from './bootstrap-stages.js'\nimport { installGracefulShutdown } from './graceful-shutdown.js'\nimport type { RequestHandlerCtx } from './handlers.js'\nimport { loadRoutesAndActions } from './manifest-loader.js'\nimport { assessPublicExposure } from './public-exposure-gate.js'\nimport { createRequestHandler } from './request-handler.js'\nimport { describeListenTarget, resolveListenTarget } from './resolve-listen-host.js'\nimport { loadAssetsMap, setupSsr } from './ssr-setup.js'\nimport { attachWebSocketHandler } from './websocket-handler.js'\n\n// Backwards-compat: external test fixtures may import resolveSsrEntry from here.\nexport { resolveSsrEntry } from './bootstrap-stages.js'\n\ninterface StartOptions {\n port?: number\n}\n\nexport async function startCommand(options: StartOptions): Promise<void> {\n const cwd = process.cwd()\n // Preflight (FIRST — BEFORE anything that touches native bindings).\n preflightNodeAndBindings(cwd)\n loadEnv({ cwd, mode: 'production' })\n const config = await loadConfig(cwd)\n\n // M48 — fail fast if the installed @theokit/sdk is present but incompatible (before serving any\n // request). Absent SDK stays silent here (an api-only app is valid); the request path guards it lazily.\n assertSdkCompatible()\n\n await configureAgentRegistryFromConfig(config.agents?.registry)\n await configureStorageManagerFromConfig(config.storage)\n // #352 — without this, `revalidateTag` / `revalidatePath` / `updateTag` throw\n // in every application: they resolve the engine from a singleton nothing\n // initialized.\n await initCacheEngineFromConfig(config.cache)\n\n const distDir = resolve(cwd, '.theokit')\n const clientDir = resolve(distDir, 'client')\n // B-035 — the route-to-chunks map `theokit build` emits. Read ONCE: it is a build artifact,\n // so a per-request read would be a syscall per request for a value that cannot change.\n // A build that predates the map, or a malformed one, serves without preloads rather than\n // failing to boot — the page is correct either way and only loses one round trip.\n const assetsMap = loadAssetsMap(resolve(clientDir, 'assets-map.json'))\n // theokit#123 — compiled controllers, when `theokit build` emitted any.\n //\n // Keyed on the MANIFEST rather than on the directory existing: a stale `dist/controllers` left by\n // an earlier build whose sources were since deleted would otherwise keep serving routes the app\n // no longer declares. `theokit build` writes the manifest only when it compiles something, and\n // `cleanOutDir` removes both, so the manifest is the authoritative \"this build has controllers\".\n const controllersDistDir = existsSync(resolve(distDir, CONTROLLER_MANIFEST_FILE))\n ? resolve(distDir, 'controllers')\n : undefined\n // #95 — honor config `serverDir` (default \"server\") in production start, matching dev.\n const serverDir = resolve(cwd, config.serverDir)\n\n if (!existsSync(clientDir)) {\n throw new Error('No build found. Run `theo build` first.')\n }\n\n const indexHtml = readFileSync(join(clientDir, 'index.html'), 'utf-8')\n const loadModule = createProductionLoader()\n // `PORT` is what every container platform injects, and `theo start` read only\n // the config — so an image told to listen on the platform's port listened on\n // 3000 instead, and the platform's health check found nothing\n // (usetheokit/theokit#402). Explicit flag beats environment beats config: the\n // flag is a person typing now, the environment is where the process was put.\n const envPort = Number.parseInt(process.env.PORT ?? '', 10)\n const port = options.port ?? (Number.isInteger(envPort) ? envPort : undefined) ?? config.port\n // #353 — observability is registered FIRST when configured, so its span brackets\n // the user's own hooks. The honest cost of one ordered list: its `onResponse`\n // also runs first, so the span closes just before the tail of the chain. Head\n // coverage matters more — auth and rate-limit hooks live there.\n const observabilityPlugin = createObservabilityPluginFromConfig(config.observability, process.env)\n // #425 — a `plugins` entry MAY be a module specifier, so the same declaration the build bakes\n // into a deployed entry is the one this server registers. Constructed plugins pass through.\n const declaredPlugins = await resolvePluginSpecifiers(config.plugins ?? [], cwd)\n const pluginRunner = await createPluginRunnerFromConfig(\n observabilityPlugin === undefined ? declaredPlugins : [observabilityPlugin, ...declaredPlugins],\n )\n const transformer = resolveTransformer(config.serialization)\n\n const custom404Path = join(clientDir, '404.html')\n const custom500Path = join(clientDir, '500.html')\n const custom404Html = existsSync(custom404Path) ? readFileSync(custom404Path, 'utf-8') : null\n const custom500Html = existsSync(custom500Path) ? readFileSync(custom500Path, 'utf-8') : null\n\n const {\n routes: cachedRoutes,\n actions: cachedActions,\n wsRoutes: cachedWsRoutes,\n agents: cachedAgents,\n } = loadRoutesAndActions(distDir, serverDir, config.agentsDir, cwd)\n\n // `createRouteRateLimiter` accepts BOTH config shapes — it detects the legacy flat form and\n // treats it as the default bucket — so one call covers everything the schema allows.\n //\n // The previous code built a limiter only for the flat shape, on the belief that the per-route\n // variant was handled by an api-middleware path. No such path runs under `theokit start`, so a\n // per-route config produced `null` here and `handlers.ts` skipped limiting on every request. The\n // app booted clean, the config validated, and nothing was ever limited — see\n // usetheokit/theokit#321. A config that validates and then does nothing is worse than one that\n // fails loudly, because the operator has no reason to look.\n const rateLimiter = config.rateLimit ? createRouteRateLimiter(config.rateLimit) : null\n\n const ssr = await setupSsr({\n distDir,\n indexHtml,\n ssrConfigEnabled: config.ssr,\n ssrStreamingConfig: config.ssrStreaming,\n })\n\n const server = createServer(\n createRequestHandler({\n buildCtx: (req, res, requestId, startTime): RequestHandlerCtx => ({\n req,\n res,\n url: req.url ?? '/',\n requestId,\n startTime,\n clientDir,\n custom404Html,\n cachedRoutes,\n cachedActions,\n cachedAgents,\n loadModule,\n serverDir,\n projectRoot: cwd,\n controllersDistDir,\n pluginRunner,\n transformer,\n csrfMode: config.security?.csrf ?? 'strict',\n disallowed: config.security?.disallowed,\n jobBackend: jobBackendFromConfig(config),\n rateLimiter,\n }),\n securityHeadersConfig: config.security?.headers ?? {},\n // #409 — built once at startup, like the security headers beside it. Declaring `cors` and\n // being served by this command used to mean no CORS at all, which reads in a browser as a\n // blocked fetch and in the config as a setting that is present and validated.\n corsHandler: config.security?.cors ? createCorsHandler(config.security.cors) : null,\n ssrRender: ssr.render,\n ssrRenderStreaming: ssr.renderStreaming,\n ssrStreamingEnabled: ssr.streamingEnabled,\n htmlHead: ssr.htmlHead,\n htmlTail: ssr.htmlTail,\n assetsMap,\n indexHtml,\n custom500Html,\n // M7-2: serve a built-in liveness route on the Node listener. Readiness\n // probe wiring from theo.config.ts is a documented follow-up (see the M7\n // implementation summary § Scope note).\n reservedRoutes: { health: defineHealthRoute() },\n }),\n )\n\n await attachWebSocketHandler(server, cachedWsRoutes, loadModule)\n\n // `config.host` was never passed here, and `listen(port)` with no address binds\n // every interface — so the server listened wider than its own configuration,\n // whose default says `localhost`. Passing it broke containers, where `localhost`\n // means nobody, so `HOST` now gets a say (usetheokit/theokit#402).\n const listenTarget = resolveListenTarget(config.host)\n\n // Deciding WHERE to listen settled reachability; this settles consequence. The refusal happens\n // BEFORE `listen` because a server that binds and then complains has already accepted the first\n // request — the log entry would arrive after the exposure it describes.\n const exposure = assessPublicExposure({\n routes: cachedRoutes,\n target: listenTarget,\n allowUnauthenticatedWrites: config.security?.allowUnauthenticatedWrites ?? false,\n // `cachedRoutes` comes from the file-route scan and never describes controllers. Without this\n // the gate reads an empty table as \"nothing is exposed\" and binds a public interface in silence,\n // while a controller marked `theokit:public` on a POST sits on it (theokit#543).\n hasControllers: controllersDistDir !== undefined,\n })\n if (exposure.kind === 'refused') {\n console.error(`\\n ${exposure.message}\\n`)\n // Non-zero: an orchestrator restarting this container must see a failure, not a clean exit that\n // reads as \"the process decided to stop\" (docs/adr/0002 — an abnormal ending is never reported\n // as normal).\n process.exitCode = 1\n return\n }\n if (exposure.kind === 'unverified') {\n console.warn(`\\n ${exposure.message}\\n`)\n }\n\n // Crons (theokit#324) and jobs (#1010), started only once the server is cleared to listen: a\n // refused boot must not leave timers holding the process open.\n const background = await startBackgroundWork(config, distDir, cwd, loadModule)\n\n server.listen(port, listenTarget.host, () => {\n console.log(`\\n Theo production server`)\n // The line states the bound address, because it used to print `localhost`\n // either way — so a container serving everyone and one serving nobody were\n // indistinguishable in the log.\n console.log(`${describeListenTarget(listenTarget, port)}\\n`)\n if (exposure.kind === 'allowed-by-override') {\n // The override permits the exposure; it does not make it quiet. Each start names what is\n // open, so `allowUnauthenticatedWrites: true` cannot be forgotten in a config nobody reopens.\n const n = exposure.exposures.length\n console.warn(\n ` security.allowUnauthenticatedWrites is on — ${String(n)} unauthenticated write ${n === 1 ? 'route is' : 'routes are'} reachable:`,\n )\n for (const e of exposure.exposures) console.warn(` ${e.method} ${e.routePath}`)\n console.warn('')\n }\n for (const line of background.summaries) console.log(` ${line}\\n`)\n })\n\n installGracefulShutdown(server, () => background.stop())\n}\n","/**\n * M7-2 — health/ready reserved routes for the convention/filesystem-route\n * server. Liveness (`/__theo/health`, always 200) and readiness\n * (`/__theo/ready`, 200/503 from a probe) are registered on a reserved\n * namespace BEFORE the user-route catch-all + 404 branch, which is why a user\n * route can never shadow them. Liveness and readiness are kept\n * separate by design: liveness says \"the process is up\", readiness says\n * \"dependencies are up\".\n *\n * @public\n */\n\n/** Reserved path for the liveness endpoint. */\nexport const HEALTH_PATH = '/__theo/health'\n/** Reserved path for the readiness endpoint. */\nexport const READY_PATH = '/__theo/ready'\n\n/** Config produced by {@link defineHealthRoute}. */\nexport interface HealthRouteConfig {\n readonly kind: 'health'\n /** Returns the liveness body (auto-serialized to JSON). Default: `{ status: 'ok' }`. */\n readonly handler: () => unknown\n}\n\n/** Config produced by {@link defineReadyRoute}. */\nexport interface ReadyRouteConfig {\n readonly kind: 'ready'\n /** Resolves `true` when dependencies are ready. A throw/reject is treated as not-ready (503). */\n readonly probe: () => boolean | Promise<boolean>\n}\n\n/** The reserved-route registry consulted by {@link serveReservedRoute}. */\nexport interface ReservedRoutes {\n readonly health?: HealthRouteConfig\n readonly ready?: ReadyRouteConfig\n}\n\n/** A reserved-route response: an HTTP status + a JSON-serializable body. */\nexport interface ReservedResponse {\n readonly status: number\n readonly body: unknown\n}\n\n/**\n * Define the liveness route. The default handler returns `{ status: 'ok' }`.\n * Liveness is always 200 — it asserts the process is up, not that dependencies are.\n */\nexport function defineHealthRoute(\n handler: () => unknown = () => ({ status: 'ok' }),\n): HealthRouteConfig {\n return { kind: 'health', handler }\n}\n\n/**\n * Define the readiness route. The `probe` decides 200 (ready) vs 503 (not-ready).\n * A probe that throws/rejects is treated as not-ready (503) — never a 500 crash.\n */\nexport function defineReadyRoute(probe: () => boolean | Promise<boolean>): ReadyRouteConfig {\n return { kind: 'ready', probe }\n}\n\n/**\n * Pure dispatcher: map a request pathname to a reserved-route response, or\n * `null` when the path is not reserved (so the user catch-all + 404 take over).\n * Liveness defaults to 200 `{status:'ok'}`; readiness without a probe is\n * trivially ready (200). Registered BEFORE the user catch-all by the caller.\n */\nexport async function serveReservedRoute(\n pathname: string,\n routes: ReservedRoutes = {},\n): Promise<ReservedResponse | null> {\n if (pathname === HEALTH_PATH) {\n const handler = routes.health?.handler ?? (() => ({ status: 'ok' }))\n return { status: 200, body: handler() }\n }\n if (pathname === READY_PATH) {\n const probe = routes.ready?.probe\n if (probe === undefined) {\n return { status: 200, body: { status: 'ready' } }\n }\n try {\n const ready = await probe()\n return ready\n ? { status: 200, body: { status: 'ready' } }\n : { status: 503, body: { status: 'not-ready' } }\n } catch (err) {\n // 503 (the dependency is not ready) but NEVER swallow the cause — a\n // readiness probe failing silently is invisible to ops (project rule:\n // \"NEVER swallow exceptions\"). Log the cause, keep the 503 contract.\n console.error('[theo] readiness probe threw — treating as not-ready', err)\n return { status: 503, body: { status: 'not-ready' } }\n }\n }\n return null\n}\n","import type { IncomingMessage } from 'node:http'\n\n/**\n * Resolves the address a rate-limit bucket should be keyed on.\n *\n * ## Why this is not just `req.socket.remoteAddress`\n *\n * Behind any reverse proxy — Caddy, nginx, a load balancer, an ingress controller — the socket\n * address is the PROXY's, identical for every visitor on the internet. Keying buckets on it gives\n * the whole world one shared budget, so the first few requests each minute exhaust it and everyone\n * else is refused. That is worse than no limiting: a limit meant to stop one abusive client becomes\n * a denial of service any single client can trigger for everybody.\n *\n * ## Why it is opt-in\n *\n * `x-forwarded-for` is a request header, and a request header is whatever the client typed. Reading\n * it without being told to would let anyone bypass the limiter by rotating a forged value — a\n * one-line `curl -H`. So the default is to trust nothing and use the socket.\n *\n * ## Why the RIGHTMOST entry\n *\n * Each proxy APPENDS the address that connected to it. A client that sends `x-forwarded-for: 1.2.3.4`\n * through one proxy produces `1.2.3.4, <real client>` — the forgery lands on the left, and the entry\n * the trusted proxy wrote is last. Counting from the right therefore skips exactly the hops the\n * operator vouched for, and everything a client can influence stays to the left of where we look.\n *\n * This mirrors the `'trusted-proxy'` policy in `adapters/web-shim.ts`, which documents the same rule\n * for the Web-Request runtimes.\n */\n\n/**\n * How many proxies sit in front of the app.\n *\n * `false` (the default) trusts nothing and uses the socket address. `true` means one trusted proxy.\n * A number names the hop count for a longer chain — a CDN in front of your own proxy is `2`.\n */\nexport type TrustProxy = boolean | number\n\n/** `true` is shorthand for a single proxy; `false` for none. A number passes through. */\nfunction hopCount(trustProxy: TrustProxy): number {\n if (trustProxy === true) return 1\n if (trustProxy === false) return 0\n return trustProxy\n}\n\n/** Reads a header that Node may hand back as an array. */\nfunction headerValue(req: IncomingMessage, name: string): string | undefined {\n const raw = req.headers[name]\n if (Array.isArray(raw)) return raw[raw.length - 1]\n return raw\n}\n\n/**\n * The forwarded-header half of the decision, with no runtime in it.\n *\n * Extracted (usetheokit/theokit#612) so the Node path and the Web path cannot drift: a `Request`\n * has the same headers and no socket, and the arithmetic on those headers is the part that is easy\n * to get subtly wrong twice. What the two runtimes genuinely disagree about — where the fallback\n * address comes from — stays with each caller.\n *\n * @param header the raw `x-forwarded-for` value, if present\n * @param realIpHeader the raw `x-real-ip` value, if present\n * @param trustProxy how many proxies the operator vouched for\n * @returns the address to key on, or `undefined` when no trusted entry could be read\n *\n * Module-private: the two exported resolvers below are the whole surface, and a caller holding\n * the raw header arithmetic would be re-deciding the part that is easy to get wrong.\n */\nfunction addressFromForwardedHeaders(\n header: string | undefined,\n realIpHeader: string | undefined,\n trustProxy: TrustProxy,\n): string | undefined {\n const hops = hopCount(trustProxy)\n if (hops < 1) return undefined\n\n if (header) {\n const entries = header\n .split(',')\n .map((entry) => entry.trim())\n .filter(Boolean)\n\n // Count in from the right by the number of hops the operator vouched for. A chain shorter than\n // configured means a request did NOT come through the expected proxies — a direct hit on the\n // app's port, say — so we report nothing rather than reading an entry the client could have\n // written.\n const index = entries.length - hops\n if (index >= 0 && index < entries.length) return entries[index]\n }\n\n // `x-real-ip` carries a single address and is written by the proxy, not appended to, so there is\n // no hop arithmetic to do. Only consulted when a proxy is trusted at all.\n const realIp = realIpHeader?.trim()\n return realIp === undefined || realIp === '' ? undefined : realIp\n}\n\nexport function resolveClientIp(req: IncomingMessage, trustProxy: TrustProxy = false): string {\n // `req.socket` is typed as always-present in Node typings; the optional chain keeps the fallback\n // reachable for test doubles built as plain object literals.\n // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition -- defensive for test doubles\n const socketAddress = req.socket?.remoteAddress ?? 'unknown'\n\n return (\n addressFromForwardedHeaders(\n headerValue(req, 'x-forwarded-for'),\n headerValue(req, 'x-real-ip'),\n trustProxy,\n ) ?? socketAddress\n )\n}\n\n/**\n * The Web-`Request` twin of {@link resolveClientIp}.\n *\n * There is no socket to fall back to, so the answer is `undefined` when no trusted proxy wrote an\n * address. A caller MUST treat that as \"cannot identify\" rather than substituting a constant:\n * bucketing every visitor under one placeholder turns a rate limiter into a shared budget the first\n * caller each window exhausts for the whole internet.\n */\nexport function resolveClientIpFromRequest(\n request: Request,\n trustProxy: TrustProxy = false,\n): string | undefined {\n return addressFromForwardedHeaders(\n request.headers.get('x-forwarded-for') ?? undefined,\n request.headers.get('x-real-ip') ?? undefined,\n trustProxy,\n )\n}\n","// T5a.1d — Web Crypto migration. The last node:crypto consumer in server/.\n// `createHash('sha256')` is sync but Web Crypto's `subtle.digest('SHA-256', ...)`\n// is async. This propagates through hashFragment → deriveKey → the factory's\n// returned checker, which is why the checker is async. `theokit start` awaits it\n// (usetheokit/theokit#321 — before that fix this factory had no production consumer at all, and a\n// per-route config silently disabled rate limiting outright). IncomingMessage stays as a type-only\n// import (TS-erased; runtime-clean).\nimport type { IncomingMessage } from 'node:http'\n\nimport { parseCookieHeader } from '../http/cookies.js'\n\nimport { resolveClientIp, type TrustProxy } from './client-ip.js'\nimport {\n type BakedStoreDeclaration,\n InMemoryStore,\n type RateLimitStore,\n} from './rate-limit-store.js'\nimport type { RateLimitConfig, RateLimitResult } from './rate-limit.js'\n\n/**\n * T2.2 — Per-route + per-user rate limiting.\n *\n * Layered on top of `rate-limit-store.ts`. The route map allows\n * declarative policies (\"strict /api/login, loose everything else\")\n * driven by config, not handler-decorated. `keyBy` selects what\n * identifier the limiter buckets on.\n *\n * ADR D2: per-route via path matching, NOT per-handler decorator.\n * Operators can tune policies without touching route definitions.\n */\n\nexport type KeyByMode = 'ip' | 'session' | 'user' | ((req: IncomingMessage) => string)\n\nexport interface RouteRateLimitConfig {\n /** Fallback config used when no per-route entry matches. */\n default?: RateLimitConfig\n /** Map of path pattern → config. Exact-string keys (RegExp via API). */\n routes?: Record<string, RateLimitConfig>\n /** Same as `routes` but each entry is a [pattern, config] tuple, RegExp allowed. */\n routePatterns?: readonly [string | RegExp, RateLimitConfig][]\n /** Bucket identifier strategy. Default 'ip'. */\n keyBy?: KeyByMode\n /** Cookie name used by keyBy='session'. Defaults to 'theo_session'. */\n cookieName?: string\n /**\n * Optional shared store (for multi-route correlation). Default per-limiter InMemoryStore.\n *\n * B-257 widened this to a union, and the second arm is not a store this runner uses. A deployed\n * entry's BUILD-time declaration — `{ module, factory, options }` from\n * `config/schemas/rate-limit.ts` — lands on the same key, because `theokit start` hands the\n * parsed config straight to `createRouteRateLimiter`. `runtimeStore()` below discriminates on\n * `incr` and drops the declaration.\n *\n * The union is declared rather than the parameter widened at the call site: a cast there would\n * make the collision invisible to every other reader of this type, and the collision is the fact\n * worth carrying.\n */\n store?: RateLimitStore | BakedStoreDeclaration\n /**\n * How many reverse proxies sit in front of the app, for `keyBy: 'ip'`. Default `false` — trust\n * none and key on the socket address.\n *\n * Set this whenever the app is behind Caddy, nginx, a load balancer or an ingress controller:\n * without it every visitor keys on the proxy's address and shares one bucket, so a handful of\n * requests exhausts the budget for the entire internet. With it, the client address is read from\n * `x-forwarded-for` counting in from the right, past exactly the hops declared here.\n *\n * Leaving it off by default is deliberate — `x-forwarded-for` is client-writable, and honouring it\n * uninvited turns the limiter into a one-header bypass. See `client-ip.ts`.\n */\n trustProxy?: TrustProxy\n}\n\n/**\n * Normalize a path for matching: strip query string, drop trailing slash\n * unless root. EC-5: `/api/login` and `/api/login/` collapse to the same\n * canonical form so attackers can't bypass strict limits.\n */\nfunction normalizePath(input: string): string {\n const noQuery = input.split('?')[0]\n if (noQuery.length > 1 && noQuery.endsWith('/')) return noQuery.slice(0, -1)\n return noQuery\n}\n\n/**\n * Test whether `path` matches `pattern`. String patterns are compared\n * after trailing-slash normalization (EC-5). RegExp uses `.test` after\n * resetting `lastIndex` (defensive against `/g` flag).\n */\nexport function matchRoutePattern(path: string, pattern: string | RegExp): boolean {\n const canonical = normalizePath(path)\n if (typeof pattern === 'string') {\n return canonical === normalizePath(pattern)\n }\n pattern.lastIndex = 0\n return pattern.test(canonical)\n}\n\n/**\n * Hash a string with SHA-256 and return the first 16 base64url chars.\n * Used by `keyBy='session'` so the raw cookie value never lands in a\n * rate-limit key (which may flow into audit logs).\n *\n * T5a.1d — async via Web Crypto subtle.digest (no node:crypto). The\n * base64url encoding is done manually because btoa+url-safe transform is\n * available everywhere but `digest('base64url')` is Node-only.\n */\nasync function hashFragment(input: string): Promise<string> {\n const buf = await globalThis.crypto.subtle.digest('SHA-256', new TextEncoder().encode(input))\n const bytes = new Uint8Array(buf)\n let bin = ''\n for (const b of bytes) bin += String.fromCharCode(b)\n // eslint-disable-next-line sonarjs/slow-regex -- input is fixed-length 44 chars (SHA-256 base64), trailing '=' padding ≤ 2 chars, no ReDoS surface\n const b64url = btoa(bin).replace(/\\+/g, '-').replace(/\\//g, '_').replace(/=+$/, '')\n return b64url.slice(0, 16)\n}\n\n// T3.2 DRY consolidation: cookie parsing moved to ../http/cookies.ts.\n// The canonical `parseCookieHeader` returns a Map for O(1) lookup; this\n// wrapper preserves the original `readCookie(req, name)` signature so the\n// rest of the file stays untouched.\nfunction readCookie(req: IncomingMessage, name: string): string | undefined {\n return parseCookieHeader(req.headers.cookie ?? undefined).get(name)\n}\n\n/**\n * Build the rate-limit bucket key for the request based on `keyBy`.\n *\n * EC-6: session mode reads the configured `cookieName`. With the wrong\n * cookie name (e.g., default 'theo_session' but app uses 'app_session'),\n * we fall back to IP so anonymous users still get rate-limited rather\n * than sharing an empty bucket.\n */\nexport async function deriveKey(\n req: IncomingMessage,\n keyBy: KeyByMode,\n cookieName: string,\n trustProxy: TrustProxy = false,\n): Promise<string> {\n if (typeof keyBy === 'function') return keyBy(req)\n // Behind a proxy the socket address is the proxy's, the same for every visitor. `resolveClientIp`\n // returns it unchanged unless the operator declared how many proxies to trust — see its comment\n // for why reading `x-forwarded-for` uninvited would hand out a one-header bypass.\n const ip = resolveClientIp(req, trustProxy)\n switch (keyBy) {\n case 'session': {\n const cookie = readCookie(req, cookieName)\n return cookie ? `session:${await hashFragment(cookie)}` : `ip:${ip}`\n }\n case 'user': {\n const userId = (req as unknown as { user?: { id?: string } }).user?.id\n return userId ? `user:${userId}` : `ip:${ip}`\n }\n case 'ip':\n default:\n return `ip:${ip}`\n }\n}\n\n/**\n * Per-route rate limiter factory. Returns a sync checker compatible with\n * the existing api-middleware shape.\n *\n * Backwards-compatibility (ADR D2): a flat `{ windowMs, max }` config is\n * accepted and treated as `default` (no per-route variants).\n */\n/**\n * The runtime store, or nothing — a BUILD-time declaration is neither.\n *\n * B-257 — `store` is two things under one key. `config/schemas/rate-limit.ts` accepts\n * `{ module, factory, options }`, which `adapters/deployed-rate-limit.ts` bakes into a deployed\n * entry at build time; this runner's `store` is a live `RateLimitStore`. `theokit start` hands the\n * parsed config straight in, so both arrive here.\n *\n * Discriminated on `incr` rather than on `module`: `incr` is what this runner would CALL, so the\n * question asked is the one that matters. Keying on the declaration's shape instead would accept\n * any other plain object as a store and fail one line later, inside the caller.\n *\n * Dropping the declaration is correct and not a silent downgrade: a long-lived process keeps its\n * counter between requests, which is why `node` and `bun` declare `enforcesRateLimit: 'always'`\n * while the serverless adapters require a store to enforce at all.\n */\nfunction runtimeStore(store: RouteRateLimitConfig['store']): RateLimitStore | undefined {\n // Optional chaining rather than an explicit `=== null` guard, and the reason is a lint finding\n // worth keeping visible: `no-unnecessary-condition` calls that comparison dead, because the\n // TYPE says `| undefined`. It is right about the type and this is a public entry point a\n // JavaScript caller reaches with no typechecking at all, so `null` arrives here whatever the\n // signature says. `?.` answers both without asserting a comparison the checker can call dead.\n return typeof (store as RateLimitStore | undefined)?.incr === 'function'\n ? (store as RateLimitStore)\n : undefined\n}\n\nexport function createRouteRateLimiter(config: RouteRateLimitConfig | RateLimitConfig) {\n // Detect legacy flat shape\n const isFlat =\n 'windowMs' in config && 'max' in config && !('default' in config) && !('routes' in config)\n const cfg: RouteRateLimitConfig = isFlat ? { default: config } : config\n\n const store = runtimeStore(cfg.store) ?? new InMemoryStore()\n // CR-005: validate store shape ONCE at construction. The previous\n // implementation ran `instanceof InMemoryStore` on every request and\n // threw at request-time if a non-InMemoryStore was passed — which\n // turned a clear config error into a runtime 500 on the first request.\n if (!(store instanceof InMemoryStore)) {\n throw new Error(\n 'createRouteRateLimiter: async RateLimitStore implementations require a dedicated async middleware path. ' +\n 'Use the InMemoryStore default for the sync facade.',\n )\n }\n const inMemoryStore = store\n const keyBy = cfg.keyBy ?? 'ip'\n const cookieName = cfg.cookieName ?? 'theo_session'\n const trustProxy = cfg.trustProxy ?? false\n\n // Build a pre-compiled list of (pattern, config) tuples for matching.\n const patternList: [string | RegExp, RateLimitConfig][] = []\n if (cfg.routes) {\n for (const [pattern, c] of Object.entries(cfg.routes)) patternList.push([pattern, c])\n }\n if (cfg.routePatterns) {\n for (const tuple of cfg.routePatterns) patternList.push(tuple)\n }\n\n return async function checkRouteRateLimit(req: IncomingMessage): Promise<RateLimitResult> {\n const url = req.url ?? ''\n let matched: RateLimitConfig | undefined\n for (const [pattern, c] of patternList) {\n if (matchRoutePattern(url, pattern)) {\n matched = c\n break\n }\n }\n const effective = matched ?? cfg.default\n if (!effective) {\n // No route match + no default → not limited.\n return { limited: false, headers: {} }\n }\n\n // Bucket key includes normalized path so /api/login and /api/login/\n // collapse to the same bucket (EC-5).\n const bucketSuffix = typeof matched === 'undefined' ? '*default*' : normalizePath(url)\n const key = `${await deriveKey(req, keyBy, cookieName, trustProxy)}|${bucketSuffix}`\n const state = inMemoryStore.incrSync(key, effective.windowMs)\n\n if (state.count > effective.max) {\n const retryAfter = Math.ceil((state.resetAt - Date.now()) / 1000)\n return {\n limited: true,\n headers: {\n 'X-RateLimit-Limit': String(effective.max),\n 'X-RateLimit-Remaining': '0',\n 'Retry-After': String(retryAfter),\n },\n }\n }\n return {\n limited: false,\n headers: {\n 'X-RateLimit-Limit': String(effective.max),\n 'X-RateLimit-Remaining': String(Math.max(0, effective.max - state.count)),\n },\n }\n }\n}\n\n/**\n * T5a.2 Phase D slice 1/3 — Web-Standards rate-limiter inputs context.\n *\n * Web `Request` has no equivalent of `req.socket.remoteAddress` (Node\n * runtime concept) or `req.user` (set by upstream middleware). The\n * Web-shaped rate-limiter requires the caller to pass these explicitly:\n *\n * - `clientIp` — resolved per-runtime (Node: `socket.remoteAddress`;\n * CF Workers: `request.headers.get('cf-connecting-ip')`; Vercel:\n * `x-forwarded-for` first hop; Bun/Deno: adapter-specific).\n * - `userId` — resolved by auth middleware (Phase D slice 3/3 ships\n * the Web-shaped session helper).\n *\n * Defaults: `clientIp = 'unknown'`, `userId = undefined` (matches the\n * IncomingMessage path's fallback semantics).\n */\nexport interface DeriveKeyRequestContext {\n clientIp?: string\n userId?: string\n}\n\n/**\n * T5a.2 Phase D slice 1/3 — Web-Standards-shaped key derivation.\n *\n * Mirror of `deriveKey(req: IncomingMessage, keyBy, cookieName)` for the\n * Web `Request` shape. Same `'ip' | 'session' | 'user'` enum cases (the\n * `function` callback case is IncomingMessage-only because the existing\n * `KeyByMode` callback type is Node-shaped; Web callers use the enum\n * cases or call a future `KeyByModeWeb` shape — out of T5a.2 Phase D scope).\n *\n * Uses `getCookieFromRequest` (Phase B slice 6/6) for session-mode cookie\n * lookup. Cookie parsing has the same CR-009 percent-encoding safety.\n */\nexport async function deriveKeyFromRequest(\n request: Request,\n keyBy: Exclude<KeyByMode, (req: IncomingMessage) => string>,\n cookieName: string,\n ctx: DeriveKeyRequestContext = {},\n): Promise<string> {\n const ip = ctx.clientIp ?? 'unknown'\n switch (keyBy) {\n case 'session': {\n // Reuse the Web cookie helper extracted in Phase B slice 6/6 so\n // CR-009 percent-encoding sanity stays consistent across paths.\n const { getCookieFromRequest } = await import('../http/cookies.js')\n const cookie = getCookieFromRequest(request, cookieName)\n return cookie ? `session:${await hashFragment(cookie)}` : `ip:${ip}`\n }\n case 'user': {\n return ctx.userId ? `user:${ctx.userId}` : `ip:${ip}`\n }\n case 'ip':\n default:\n return `ip:${ip}`\n }\n}\n\n/**\n * T5a.2 Phase D slice 1/3 — Web-Standards rate-limiter factory.\n *\n * Mirror of `createRouteRateLimiter(config)` returning a checker that\n * accepts `(request: Request, ctx?: DeriveKeyRequestContext)` instead of\n * `(req: IncomingMessage)`. Same `RouteRateLimitConfig` accepted; same\n * `keyBy` enum cases; same `InMemoryStore` constraint (CR-005 guard).\n *\n * Same returned `RateLimitResult` shape (headers + limited boolean).\n *\n * Web `Request` has no `req.url` path-only property — uses\n * `new URL(request.url).pathname + search` to derive the URL the way the\n * IncomingMessage path's `req.url ?? ''` would.\n */\nexport function createRouteRateLimiterWeb(config: RouteRateLimitConfig | RateLimitConfig) {\n const isFlat =\n 'windowMs' in config && 'max' in config && !('default' in config) && !('routes' in config)\n const cfg: RouteRateLimitConfig = isFlat ? { default: config } : config\n\n const store = runtimeStore(cfg.store) ?? new InMemoryStore()\n if (!(store instanceof InMemoryStore)) {\n throw new Error(\n 'createRouteRateLimiterWeb: async RateLimitStore implementations require a dedicated async middleware path. ' +\n 'Use the InMemoryStore default for the Web facade.',\n )\n }\n const inMemoryStore = store\n const keyBy = (cfg.keyBy ?? 'ip') as Exclude<KeyByMode, (req: IncomingMessage) => string>\n const cookieName = cfg.cookieName ?? 'theo_session'\n\n const patternList: [string | RegExp, RateLimitConfig][] = []\n if (cfg.routes) {\n for (const [pattern, c] of Object.entries(cfg.routes)) patternList.push([pattern, c])\n }\n if (cfg.routePatterns) {\n for (const tuple of cfg.routePatterns) patternList.push(tuple)\n }\n\n return async function checkRouteRateLimitWeb(\n request: Request,\n ctx: DeriveKeyRequestContext = {},\n ): Promise<RateLimitResult> {\n // Web Request guarantees absolute URL — extract path+query for pattern\n // matching (mirror of IncomingMessage's `req.url ?? ''`).\n const parsed = new URL(request.url)\n const url = `${parsed.pathname}${parsed.search}`\n let matched: RateLimitConfig | undefined\n for (const [pattern, c] of patternList) {\n if (matchRoutePattern(url, pattern)) {\n matched = c\n break\n }\n }\n const effective = matched ?? cfg.default\n if (!effective) {\n return { limited: false, headers: {} }\n }\n\n const bucketSuffix = typeof matched === 'undefined' ? '*default*' : normalizePath(url)\n const key = `${await deriveKeyFromRequest(request, keyBy, cookieName, ctx)}|${bucketSuffix}`\n const state = inMemoryStore.incrSync(key, effective.windowMs)\n\n if (state.count > effective.max) {\n const retryAfter = Math.ceil((state.resetAt - Date.now()) / 1000)\n return {\n limited: true,\n headers: {\n 'X-RateLimit-Limit': String(effective.max),\n 'X-RateLimit-Remaining': '0',\n 'Retry-After': String(retryAfter),\n },\n }\n }\n return {\n limited: false,\n headers: {\n 'X-RateLimit-Limit': String(effective.max),\n 'X-RateLimit-Remaining': String(Math.max(0, effective.max - state.count)),\n },\n }\n }\n}\n","/**\n * M48 (ecosystem-integration-guarantee) T3.2 — boot-time SDK compatibility fail-fast.\n *\n * The agent runtime is `@theokit/sdk` (G2). Today a missing / mis-versioned SDK is invisible until\n * the FIRST agent request, where `sdk-adapter.ts` yields a lazy `SDK_NOT_INSTALLED` stream event.\n * This runs at `theokit start` boot instead: if the installed SDK is present but outside the\n * supported range it throws a typed error naming found-vs-required; if it is absent (a legitimately\n * api-only app — the SDK is an OPTIONAL peer) it stays silent unless the caller marks it required.\n *\n * CLI/adapter layer → `node:*` is allowed here (unlike `server/`). The version range check itself is\n * the pure `server/agent/sdk-compat.ts` helper (one source of truth with the contract-test drift guard).\n */\nimport { readFileSync } from 'node:fs'\nimport { createRequire } from 'node:module'\n\nimport { SUPPORTED_SDK_RANGE, satisfiesSdkRange } from '../../../server/agent/sdk-compat.js'\n\n/** Typed error surfaced at boot when the installed `@theokit/sdk` is incompatible or absent-but-required. */\nexport class SdkIncompatibleError extends Error {\n readonly code = 'SDK_INCOMPATIBLE' as const\n readonly found: string | undefined\n readonly required: string\n constructor(found: string | undefined, required: string) {\n super(\n found === undefined\n ? `@theokit/sdk is required but not installed — run: pnpm add @theokit/sdk@${required}`\n : `@theokit/sdk ${found} does not satisfy the required range ${required} — run: pnpm add @theokit/sdk@${required}`,\n )\n this.name = 'SdkIncompatibleError'\n this.found = found\n this.required = required\n }\n}\n\n/** Resolve the version of the ACTUALLY-installed `@theokit/sdk`, or `undefined` when absent. */\nfunction defaultResolveSdkVersion(): string | undefined {\n try {\n const require = createRequire(import.meta.url)\n const pkgPath = require.resolve('@theokit/sdk/package.json')\n const pkg = JSON.parse(readFileSync(pkgPath, 'utf8')) as { version?: string }\n return typeof pkg.version === 'string' ? pkg.version : undefined\n } catch {\n return undefined\n }\n}\n\n/**\n * Fail fast at boot when the installed `@theokit/sdk` is incompatible.\n *\n * - present + in range → returns (silent)\n * - present + out of range → throws {@link SdkIncompatibleError} (found + required)\n * - absent + `required !== true` → returns (api-only app; the request path still guards lazily)\n * - absent + `required === true` → throws {@link SdkIncompatibleError}\n *\n * `resolveVersion` is injectable for tests (DIP); production uses {@link defaultResolveSdkVersion}.\n */\nexport function assertSdkCompatible(opts?: {\n required?: boolean\n resolveVersion?: () => string | undefined\n}): void {\n const version = (opts?.resolveVersion ?? defaultResolveSdkVersion)()\n if (version === undefined) {\n if (opts?.required === true) throw new SdkIncompatibleError(undefined, SUPPORTED_SDK_RANGE)\n return\n }\n if (!satisfiesSdkRange(version, SUPPORTED_SDK_RANGE)) {\n throw new SdkIncompatibleError(version, SUPPORTED_SDK_RANGE)\n }\n}\n","/**\n * M48 (ecosystem-integration-guarantee) — the supported `@theokit/sdk` version range + a pure,\n * dependency-free `||`-aware caret semver check. Shared by the contract test's version-drift guard\n * (`tests/integration/contract-sdk-seam.test.ts`) and the boot-time fail-fast\n * (`cli/commands/start/assert-sdk-compatible.ts`) — one source of truth for \"which SDK theokit runs on\".\n *\n * Web-Standards discipline (G8/R3a): `server/` code uses no `node:*`. This is pure string logic.\n * Rule 9 (Don't Reinvent) trade-off (ADR D1): a small inline caret checker instead of a `semver`\n * dependency, matching the theo-ui seam's existing precedent (`contract-usetheo-ui-vite-plugin.test.ts`).\n */\n\n/** The single source of truth for the supported SDK range. Mirrors the `package.json` peer floor. */\nexport const SUPPORTED_SDK_RANGE = '^4.0.1 || ^5.0.0'\n\n// Bounded semver: three `\\d+` tuples + an optional `-tag.N` prerelease. No nested quantifiers →\n// no catastrophic backtracking; the security/detect-unsafe-regex heuristic false-positives here.\n// eslint-disable-next-line security/detect-unsafe-regex -- bounded \\d+ groups anchored by `.`/`$`, no backtracking\nconst SEMVER_RE = /^(\\d+)\\.(\\d+)\\.(\\d+)(?:-([a-z]+)\\.(\\d+))?$/\n\ninterface Semver {\n major: string\n minor: string\n patch: string\n tag: string | undefined\n pre: string | undefined\n}\n\nfunction parseSemver(value: string): Semver | null {\n const m = SEMVER_RE.exec(value)\n if (!m) return null\n return { major: m[1], minor: m[2], patch: m[3], tag: m[4], pre: m[5] }\n}\n\n/**\n * Return `true` when `version` satisfies `range`, where `range` is a `||`-joined series of caret\n * pins (e.g. `^4.0.1` or `^0.14.0 || ^1.0.0`). A version satisfies the range when it satisfies ANY\n * clause. Covers the semver subset the monorepo uses (caret pins, optional `-tag.N` prerelease);\n * no range-sets or build metadata.\n */\nexport function satisfiesSdkRange(version: string, range: string): boolean {\n return range\n .split('||')\n .map((clause) => clause.trim())\n .some((clause) => satisfiesSingleCaret(version, clause))\n}\n\nfunction satisfiesSingleCaret(version: string, range: string): boolean {\n if (!range.startsWith('^')) return false\n const pin = parseSemver(range.slice(1))\n const ver = parseSemver(version)\n if (!pin || !ver) return false\n if (pin.tag !== undefined) return satisfiesPrereleaseCaret(ver, pin)\n if (ver.tag !== undefined) return false // a prerelease never satisfies a stable caret pin\n return satisfiesStableCaret(ver, pin)\n}\n\nfunction satisfiesPrereleaseCaret(ver: Semver, pin: Semver): boolean {\n // Prerelease pin: same X.Y.Z + same tag + prerelease number >= pin's.\n if (pin.major !== ver.major || pin.minor !== ver.minor || pin.patch !== ver.patch) return false\n if (ver.tag !== pin.tag) return false\n return Number(ver.pre ?? '0') >= Number(pin.pre ?? '0')\n}\n\nfunction satisfiesStableCaret(ver: Semver, pin: Semver): boolean {\n if (pin.major !== ver.major) return false\n if (pin.major === '0') {\n // 0.X.Y caret: minor must match exactly; patch can be >=.\n if (pin.minor !== ver.minor) return false\n return Number(ver.patch) >= Number(pin.patch)\n }\n // >= 1.0.0 caret: minor can be >=, patch anything within the same/greater minor.\n if (Number(ver.minor) > Number(pin.minor)) return true\n if (Number(ver.minor) === Number(pin.minor)) return Number(ver.patch) >= Number(pin.patch)\n return false\n}\n","import { resolve } from 'node:path'\n\nimport type { TheoConfig } from '../../../config/schema.js'\nimport { cronLockFromConfig } from '../../../server/cron/cron-lock-from-config.js'\nimport { createCronScheduler } from '../../../server/cron/cron-runtime-node.js'\n\nimport { loadCronDefinitions } from './cron-bootstrap.js'\nimport { loadJobDefinitions, startJobWorker } from './job-bootstrap.js'\n\n/** What `theokit start` runs beside the HTTP server: the cron scheduler and the job worker. */\nexport interface BackgroundWork {\n /** Boot-log lines, one per kind of work that was declared. */\n readonly summaries: readonly string[]\n /** Stop scheduling and polling; resolves after the job in progress, if any, has finished. */\n stop(): Promise<void>\n}\n\n/**\n * Load `.theokit/crons.json` and `.theokit/jobs.json` and start what they declare.\n *\n * Crons (theokit#324) and jobs (usetheokit/theokit#1010) are both build artifacts the node target\n * announces as \"run by `theokit start`\", and until #1010 only the crons were.\n */\nexport async function startBackgroundWork(\n config: Pick<TheoConfig, 'jobs' | 'cron'>,\n distDir: string,\n projectRoot: string,\n loadModule: (filePath: string) => unknown,\n): Promise<BackgroundWork> {\n const summaries: string[] = []\n const cronDefinitions = await loadCronDefinitions(\n resolve(distDir, 'crons.json'),\n projectRoot,\n loadModule,\n )\n let scheduler: ReturnType<typeof createCronScheduler> | undefined\n if (cronDefinitions.length > 0) {\n // #1016 — each replica runs this scheduler; the lock decides which one runs a tick.\n const { lock, description } = cronLockFromConfig(config)\n scheduler = createCronScheduler(cronDefinitions, { lock })\n scheduler.start()\n summaries.push(`Crons: ${String(cronDefinitions.length)} scheduled in-process, ${description}`)\n }\n\n const jobDefinitions = await loadJobDefinitions(\n resolve(distDir, 'jobs.json'),\n projectRoot,\n loadModule,\n )\n const jobs = startJobWorker(config, jobDefinitions)\n if (jobs.summary !== undefined) summaries.push(jobs.summary)\n\n return {\n summaries,\n async stop() {\n scheduler?.stop()\n if (jobs.worker !== undefined) {\n await jobs.worker.stop()\n console.log(' [theokit] jobs worker stopped')\n }\n },\n }\n}\n","import type { TheoConfig } from '../../config/schema.js'\n\nimport type { CronLock } from './cron-lock.js'\n\nexport interface ResolvedCronLock {\n readonly lock: CronLock | undefined\n /** How the boot log describes it, after \"Crons: N scheduled in-process, \". */\n readonly description: string\n}\n\n/**\n * The cron lock `theokit start` uses, from `theo.config.ts` (usetheokit/theokit#1016):\n *\n * 1. `cron.lock: false`: none, by choice.\n * 2. `cron.lock: <CronLock>`: that one.\n * 3. a `jobs.backend` that offers `cronLock()` (`PostgresJobBackend` does): a lock on its pool, since\n * replicas sharing a job database already share somewhere to agree.\n * 4. otherwise none, and the description says each replica fires every cron.\n *\n * Duck-typed rather than `instanceof PostgresJobBackend`: the backend in the config comes from the\n * library build and this code from the CLI build, which carry separate copies of the class.\n */\nexport function cronLockFromConfig(config: Pick<TheoConfig, 'cron' | 'jobs'>): ResolvedCronLock {\n const declared = config.cron?.lock\n if (declared === false) {\n return { lock: undefined, description: 'no distributed lock (cron.lock: false)' }\n }\n if (declared !== undefined) {\n return {\n lock: declared as CronLock,\n description: 'one firing per tick across processes (cron.lock)',\n }\n }\n const backend = config.jobs?.backend as { cronLock?: unknown; name?: unknown } | undefined\n if (backend !== undefined && typeof backend.cronLock === 'function') {\n const lock = (backend.cronLock as () => CronLock).call(backend)\n return {\n lock,\n description: `one firing per tick across processes (lock on the ${String(backend.name)} jobs backend)`,\n }\n }\n return {\n lock: undefined,\n description:\n 'no distributed lock: every process running theokit start fires each cron. ' +\n 'Run one replica, or set cron.lock (a PostgresJobBackend in jobs.backend provides one)',\n }\n}\n","/**\n * Graceful shutdown stage for `theokit start` (T4.2 architecture-cleanup,\n * ADR-0007 D6 — SIGTERM evicts agents + drains StorageManager).\n *\n * EC-13: SIGTERM evicts agents IMMEDIATELY (no per-request drain). In-flight\n * requests get aborted mid-stream — acceptable because the platform LB\n * removed this pod from rotation BEFORE sending SIGTERM (K8s preStop hook +\n * terminationGracePeriodSeconds; same on Vercel/CF/Render).\n *\n * Re-entry guard: multiple SIGTERMs in quick succession run shutdown ONCE.\n */\n\nimport type { Server as HttpServer } from 'node:http'\n\nimport { warnOnce } from '../../../server/observability/logger.js'\n\n/** How long shutdown waits for the job in progress before moving on. */\nconst BACKGROUND_STOP_BUDGET_MS = 20_000\n\n/**\n * @param stopBackgroundWork - stops the cron scheduler and the job worker. Runs FIRST and is\n * awaited, so a job in progress finishes (and is acked) before the storage pools it uses are\n * closed below.\n */\nexport function installGracefulShutdown(\n server: HttpServer,\n stopBackgroundWork: () => Promise<void> = () => Promise.resolve(),\n): void {\n let shuttingDown = false\n const shutdown = (signal: NodeJS.Signals): void => {\n if (shuttingDown) return\n shuttingDown = true\n console.log(`\\n [theokit] ${signal} received — evicting agents`)\n void (async () => {\n try {\n // Bounded: a job handler that never returns must not hold the process past the platform's\n // grace period. Its lease expires and another worker takes the job.\n await Promise.race([\n stopBackgroundWork(),\n new Promise<void>((resolve) => setTimeout(resolve, BACKGROUND_STOP_BUDGET_MS).unref()),\n ])\n } catch (err) {\n warnOnce('shutdown.background_error', {\n event: 'shutdown.background_error',\n message: err instanceof Error ? err.message : String(err),\n })\n }\n // Lazy-import SDK only at shutdown time to avoid forcing the dep on\n // apps that don't use agents at all.\n try {\n const sdk = (await import('@theokit/sdk').catch(() => null)) as {\n Agent?: { registry?: { evictAll?: () => Promise<void> } }\n } | null\n if (sdk?.Agent?.registry?.evictAll !== undefined) {\n await sdk.Agent.registry.evictAll()\n }\n } catch (err) {\n const msg = err instanceof Error ? err.message : String(err)\n warnOnce('shutdown.evict_error', {\n event: 'shutdown.evict_error',\n message: msg,\n })\n }\n // T3.1 — drain the StorageManager AFTER agent eviction. Order matters:\n // agents may still hold open pool refs while evicting; closing pools\n // first would break in-flight queries.\n try {\n const { getStorageManager } = await import('../../../server/storage/storage-manager.js')\n const manager = getStorageManager()\n await manager.dispose()\n } catch (err) {\n const msg = err instanceof Error ? err.message : String(err)\n warnOnce('shutdown.dispose_error', {\n event: 'shutdown.dispose_error',\n message: msg,\n })\n }\n // #353 — flush telemetry LAST, so the spans covering eviction and the\n // storage drain are in the batch that leaves. Without this the exporter's\n // final buffer dies with the process, and the most interesting spans a\n // deploy produces — the ones from the shutdown itself — are the ones\n // guaranteed never to arrive.\n try {\n const { getObservabilityAdapter } =\n await import('../../../server/observability-bootstrap.js')\n await getObservabilityAdapter()?.shutdown()\n } catch (err) {\n const msg = err instanceof Error ? err.message : String(err)\n warnOnce('shutdown.observability_error', {\n event: 'shutdown.observability_error',\n message: msg,\n })\n }\n console.log(` [theokit] shutdown complete`)\n server.close(() => {\n process.exit(0)\n })\n setTimeout(() => {\n warnOnce('shutdown.forced_exit', {\n event: 'shutdown.forced_exit',\n message: 'forced exit after 25s timeout',\n })\n process.exit(0)\n }, 25_000).unref()\n })()\n }\n process.on('SIGTERM', () => {\n shutdown('SIGTERM')\n })\n process.on('SIGINT', () => {\n shutdown('SIGINT')\n })\n}\n","/**\n * Manifest loading stage for `theokit start` (T4.2 architecture-cleanup).\n *\n * Loads pre-built manifest from `.theokit/manifest.json` if present; otherwise\n * scans server/ for routes/actions/ws at startup with a structured warn.\n */\n\nimport { existsSync } from 'node:fs'\nimport { dirname, join } from 'node:path'\n\nimport { warnOnce } from '../../../server/observability/logger.js'\nimport type { ActionNode } from '../../../server/scan/action-scan.js'\nimport { scanServerActions } from '../../../server/scan/action-scan.js'\nimport type { AgentNode } from '../../../server/scan/agent-scan.js'\nimport { scanAgents } from '../../../server/scan/agent-scan.js'\nimport { loadManifest } from '../../../server/scan/manifest.js'\nimport type { ServerRouteNode } from '../../../server/scan/match.js'\nimport { scanServerRoutes } from '../../../server/scan/scan.js'\nimport type { WebSocketRouteNode } from '../../../server/scan/ws-scan.js'\nimport { scanWebSocketRoutes } from '../../../server/scan/ws-scan.js'\n\ninterface LoadedRoutes {\n routes: ServerRouteNode[]\n actions: ActionNode[]\n wsRoutes: WebSocketRouteNode[]\n agents: AgentNode[]\n}\n\nexport function loadRoutesAndActions(\n distDir: string,\n serverDir: string,\n // #95 follow-up — agents dir name (config `agentsDir`, default \"agents\") for the live-scan fallback.\n agentsDir = 'agents',\n // #871 — the app root, so agent paths decode against what encoded them.\n projectRoot: string = dirname(serverDir),\n): LoadedRoutes {\n const manifestPath = join(distDir, 'manifest.json')\n\n if (existsSync(manifestPath)) {\n const manifest = loadManifest(distDir, serverDir, projectRoot)\n return {\n routes: manifest.routes,\n actions: manifest.actions,\n wsRoutes: manifest.websockets,\n agents: manifest.agents,\n }\n }\n warnOnce('bootstrap.manifest_not_found', {\n event: 'bootstrap.manifest_not_found',\n message:\n 'No manifest found, scanning routes at startup. Run \"theo build\" to generate manifest.',\n serverDir,\n })\n return {\n routes: scanServerRoutes(serverDir),\n actions: scanServerActions(serverDir),\n wsRoutes: scanWebSocketRoutes(serverDir),\n // Agents live at <projectRoot>/<agentsDir>; projectRoot = serverDir's parent for the canonical layout.\n agents: scanAgents(projectRoot, agentsDir),\n }\n}\n","/**\n * Refuse to put unauthenticated write routes on a public network interface.\n *\n * `resolve-listen-host.ts` settled WHICH address gets bound and made the log say so. This settles\n * whether that address should be bound at all, given what is behind it. The two are deliberately\n * separate: one is about reachability, this one is about consequence.\n *\n * The gap it closes has a shape worth naming. ADR 0001 made every route declare who may call it and\n * stopped absence from meaning open — a real improvement, and an incomplete one, because `'public'`\n * is a declaration too. A route table where every entry says `policy('public')` passes the build\n * gate perfectly and is, in substance, a table nobody protected. Nothing downstream could tell the\n * two apart, since the policy value never left the module; `detectRoutePolicyKinds` is what made it\n * legible at scan time, and this is the first thing to act on it.\n *\n * ## What it refuses, and what it does not\n *\n * Refused: a non-loopback bind while at least one POST / PUT / PATCH / DELETE declares the literal\n * `'public'`. Those are the requests that spend money, mutate state, or send mail on the operator's\n * behalf, and an unauthenticated one reachable from a network is the shape of an open relay.\n *\n * NOT refused: public GET / HEAD / OPTIONS. Public read endpoints are ordinary — health checks,\n * catalogues, landing APIs — and a gate that fired on them would be switched off within a day. A\n * gate with a stated edge is worth more than a gate nobody runs. That means this does NOT protect\n * against a public GET that leaks data; that is authorization work the policy function must do, and\n * saying so here is cheaper than letting an operator infer a guarantee that was never offered.\n *\n * ## Why absence is not safety\n *\n * A manifest built before this existed carries no `publicMethods`, and reading that silence as\n * \"nothing is public\" would be the same defect the gate exists to prevent, one field over. The\n * verdict is `unverified`: the server still starts (an upgrade must not break a running deploy),\n * and the operator is told plainly that the check did not run and what to do about it.\n */\nimport type { ServerRouteNode } from '../../../server/scan/match.js'\n\nimport type { ListenTarget } from './resolve-listen-host.js'\n\n/**\n * Methods that change something. The rest are readable requests, out of scope by the reasoning in\n * the module docblock.\n */\nconst MUTATING_METHODS = new Set(['POST', 'PUT', 'PATCH', 'DELETE'])\n\n/** Addresses that reach only this machine. */\nconst LOOPBACK_HOSTS = new Set(['localhost', '127.0.0.1', '::1'])\n\n/** One unauthenticated write, named so the operator can go and look at it. */\nexport interface Exposure {\n readonly routePath: string\n readonly method: string\n}\n\nexport type ExposureVerdict =\n /** Bound to the loopback — nothing outside this machine can reach it, so there is nothing to judge. */\n | { readonly kind: 'not-exposed' }\n /** Public bind, and every mutating route is guarded. */\n | { readonly kind: 'allowed' }\n /** Public bind with unauthenticated writes, permitted by an explicit written decision. */\n | { readonly kind: 'allowed-by-override'; readonly exposures: readonly Exposure[] }\n /** Public bind with unauthenticated writes and no override. The server must not start. */\n | { readonly kind: 'refused'; readonly exposures: readonly Exposure[]; readonly message: string }\n /** Public bind, and the route table cannot answer the question. Starts, says so. */\n | { readonly kind: 'unverified'; readonly message: string }\n\nexport interface ExposureAssessment {\n readonly routes: readonly ServerRouteNode[]\n readonly target: ListenTarget\n /** `security.allowUnauthenticatedWrites` from theo.config.ts — an explicit, written decision. */\n readonly allowUnauthenticatedWrites: boolean\n /**\n * Whether this build emitted compiled controllers, which `routes` does not describe.\n *\n * The caller already knows: `start` resolves `dist/controllers.json` to decide whether to serve\n * them at all. Passing the fact in is what lets an empty `routes` mean two different things —\n * \"this app serves nothing\", which is safe to bind, and \"this app serves through controllers the\n * manifest does not list\", which is not judged here at all.\n *\n * `undefined` is read as unknown, not as false. Distinguishing them is the point of the field, and\n * a caller that has not been updated should not be answered with confidence it did not supply.\n */\n readonly hasControllers?: boolean\n}\n\n/** Does this address reach anything beyond this machine? */\nfunction isPubliclyBound(host: string): boolean {\n return !LOOPBACK_HOSTS.has(host.toLowerCase())\n}\n\nfunction unauthenticatedWrites(routes: readonly ServerRouteNode[]): Exposure[] {\n const found: Exposure[] = []\n for (const route of routes) {\n for (const method of route.publicMethods ?? []) {\n if (MUTATING_METHODS.has(method)) found.push({ routePath: route.routePath, method })\n }\n }\n return found\n}\n\n/**\n * Why the gate cannot judge this route table, or `null` when it can.\n *\n * Two different absences reach here, and telling them apart matters because the operator's next\n * move differs. Returning a REASON rather than a boolean is what keeps the message honest: a\n * warning that prescribes `theo build` to someone whose build is fine teaches them to ignore it.\n *\n * - `stale-manifest` — routes exist and declare mutating methods, but none carries `publicMethods`.\n * The signature of a manifest built before this check existed. Regenerating it answers the\n * question.\n *\n * - `empty-table` — the manifest describes no routes at all. Regenerating it changes nothing: the\n * scan behind that array reads `server/routes/`, and an app serving through controllers has none\n * (usetheokit/theokit#543). Measured on a nine-controller app: sixteen routes served, `routes: []`\n * written.\n *\n * This one was silently `allowed` until 2026-08-28. `.some()` on an empty array is false for both\n * questions above, so the gate concluded there was nothing to expose and bound a public interface\n * without a word — while a controller declaring `@SetMetadata('theokit:public', true)` on a POST\n * sat on it. Absence of a description is not absence of exposure, which is the sentence this whole\n * file is written around.\n */\nfunction whyCannotAnswer(\n routes: readonly ServerRouteNode[],\n hasControllers: boolean | undefined,\n): 'stale-manifest' | 'empty-table' | null {\n if (routes.length === 0) {\n // An app that serves nothing is safe to bind anywhere, and warning about it is the kind of\n // noise that gets a gate switched off. Only an empty table that is NOT the whole story warrants\n // a word — controllers present, or a caller that did not say.\n return hasControllers === false ? null : 'empty-table'\n }\n if (routes.some((r) => r.publicMethods !== undefined)) return null\n return routes.some((r) => (r.methods ?? []).some((m) => MUTATING_METHODS.has(m)))\n ? 'stale-manifest'\n : null\n}\n\nfunction unverifiedMessage(reason: 'stale-manifest' | 'empty-table', host: string): string {\n const head = `Binding ${host} without checking whether its write routes are authenticated.`\n return reason === 'stale-manifest'\n ? [\n head,\n ' This manifest predates the check and records no policy kinds. Run `theo build` to',\n ' regenerate it; until then the server starts, and this warning is the whole of what',\n ' is known about the exposure.',\n ].join('\\n')\n : [\n head,\n ' The manifest describes no routes, so there is nothing here to judge. If this app serves',\n ' through controllers, that is expected and rebuilding will not change it — the route scan',\n ' reads `server/routes/` only (usetheokit/theokit#543). Whatever your controllers mark',\n ' `theokit:public` on a POST/PUT/PATCH/DELETE is reachable from this address, and this',\n ' warning is the whole of what is known about it.',\n ].join('\\n')\n}\n\nfunction refusalMessage(exposures: readonly Exposure[], host: string): string {\n const list = exposures.map((e) => ` ${e.method} ${e.routePath}`).join('\\n')\n return [\n exposures.length === 1\n ? `Refusing to bind ${host}: 1 write route accepts unauthenticated requests.`\n : `Refusing to bind ${host}: ${String(exposures.length)} write routes accept unauthenticated requests.`,\n '',\n list,\n '',\n \" Each declares `policy('public')`, so anyone who can reach this address can call it.\",\n ' On a loopback bind that is a demo; on this one it is an open endpoint.',\n '',\n ' Resolve it one of two ways:',\n ' • give each route a real policy — `policy(({ subject }) => subject !== null)`, or',\n ' `requireOwner(subject, record.ownerId)` from `theokit/server/define`. A plugin hook',\n ' establishes `ctx.subject`; the policy reads it.',\n ' • decide otherwise, in writing: `security: { allowUnauthenticatedWrites: true }` in',\n ' theo.config.ts. The routes stay open and the startup log keeps saying so.',\n ].join('\\n')\n}\n\n/**\n * Judge a route table against the address about to be bound.\n *\n * Pure — it reads, it decides, it returns. The caller prints and exits, which keeps this testable\n * without a process and keeps the exit policy in one place.\n */\nexport function assessPublicExposure(input: ExposureAssessment): ExposureVerdict {\n if (!isPubliclyBound(input.target.host)) return { kind: 'not-exposed' }\n\n const blind = whyCannotAnswer(input.routes, input.hasControllers)\n if (blind !== null) {\n return { kind: 'unverified', message: unverifiedMessage(blind, input.target.host) }\n }\n\n const exposures = unauthenticatedWrites(input.routes)\n if (exposures.length === 0) return { kind: 'allowed' }\n if (input.allowUnauthenticatedWrites) return { kind: 'allowed-by-override', exposures }\n\n return { kind: 'refused', exposures, message: refusalMessage(exposures, input.target.host) }\n}\n","import type { IncomingMessage, ServerResponse } from 'node:http'\nimport { extname } from 'node:path'\nimport { pathToFileURL } from 'node:url'\n\nimport type { RouteSubject } from '../../../core/contracts/route-policy.js'\nimport { readAgentPolicy } from '../../../server/agent/agent-access.js'\nimport { getApprovalRegistry } from '../../../server/agent/approval-registry.js'\nimport {\n handleAgentApproval,\n isApprovalPath,\n parseApprovalAgentName,\n} from '../../../server/agent/approve-agent.js'\nimport { mountAgent } from '../../../server/agent/mount-agent.js'\nimport { resolveProvider } from '../../../server/agent/provider-resolver.js'\nimport { matchAgentAuxRoute, serveMatchedAuxRoute } from '../../../server/agent/serve-aux-routes.js'\nimport { executeAction } from '../../../server/http/action-execute.js'\nimport { dispatchControllerRequest } from '../../../server/http/controller-dispatch.js'\nimport { executeRoute } from '../../../server/http/execute.js'\nimport { createWebRequestSource } from '../../../server/http/node-request.js'\nimport { writeWebResponseToServerResponse } from '../../../server/http/node-web-adapter.js'\nimport { serveThroughPluginLifecycle } from '../../../server/http/plugin-lifecycle.js'\nimport { createAgentSubjectResolver } from '../../../server/http/resolve-agent-subject.js'\nimport { sendError } from '../../../server/http/send-response.js'\nimport { serveStaticFile } from '../../../server/http/static.js'\nimport type { JobBackend } from '../../../server/jobs/job-backend.js'\nimport { logRequest } from '../../../server/observability/logger.js'\nimport { findSuggestion } from '../../../server/observability/suggest.js'\nimport type { PluginRunner } from '../../../server/plugins/plugin-runner.js'\nimport type { ActionNode } from '../../../server/scan/action-scan.js'\nimport type { AgentNode } from '../../../server/scan/agent-scan.js'\nimport { matchRoute } from '../../../server/scan/match.js'\nimport type { ServerRouteNode } from '../../../server/scan/match.js'\nimport type { LoadModule } from '../../../server/scan/module-loader.js'\nimport type { CsrfMode, DisallowedConfig } from '../../../server/security/csrf.js'\nimport type { TheoTransformer } from '../../../server/transformer.js'\n\n/**\n * Load a controller module that `theokit build` already compiled — theokit#123.\n *\n * A plain dynamic `import()`, deliberately: production must not need `@swc/core`. That peer is a\n * native binary the app would otherwise carry solely to re-do work the build already did, and a\n * missing optional peer would degrade into a runtime 404 instead of a build failure.\n */\nasync function loadCompiledController(absPath: string): Promise<Record<string, unknown>> {\n return (await import(pathToFileURL(absPath).href)) as Record<string, unknown>\n}\n\n/** Response header carrying the per-request correlation id. */\nconst X_REQUEST_ID = 'x-request-id'\n\n/**\n * T6.1 (PV-7 SRP): start.ts request orchestrator decomposed into 5 focused\n * per-branch handlers. Each handler returns `true` if it handled the\n * request (response sent) so the orchestrator can stop iterating.\n *\n * The original 455-LOC monolith closed over 14+ locals; the shared shape\n * `RequestHandlerCtx` makes the dependencies explicit + reviewable.\n */\nexport interface RequestHandlerCtx {\n req: IncomingMessage\n res: ServerResponse\n url: string\n requestId: string\n startTime: number\n // Pre-loaded build artifacts\n clientDir: string\n custom404Html: string | null\n // Manifest-resolved tables\n cachedRoutes: ServerRouteNode[]\n cachedActions: ActionNode[]\n cachedAgents: AgentNode[]\n // Runtime infra\n loadModule: LoadModule\n serverDir: string\n /** App root (= `process.cwd()` at `theokit start`); mountAgent points `.theokit/` discovery here. */\n projectRoot: string\n /**\n * theokit#123 — absolute path to the COMPILED controllers emitted by `theokit build`\n * (`<distDir>/controllers`), or `undefined` when the build produced none.\n *\n * `undefined` is the routes-only app, and it must stay free: no scan, no import, no cost.\n */\n controllersDistDir: string | undefined\n pluginRunner: PluginRunner | undefined\n transformer: TheoTransformer | undefined\n csrfMode: CsrfMode\n disallowed: DisallowedConfig | undefined\n /**\n * `theo.config.ts > jobs.backend`. Present, every file route gets `ctx.queue`; it used to be\n * read by nothing, so `ctx.queue` was undefined under `theokit start` (usetheokit/theokit#1009).\n */\n jobBackend: JobBackend | undefined\n /**\n * Async because the per-route limiter hashes the session cookie with Web Crypto when\n * `keyBy: 'session'`, and `subtle.digest` is promise-based.\n */\n rateLimiter:\n | ((req: IncomingMessage) => Promise<{ limited: boolean; headers: Record<string, string> }>)\n | null\n}\n\n/**\n * The caller's identity, from the application's own `server/context.ts` — the seam every\n * `route()` already reads and no agent URL ever reached (usetheokit/theokit#365).\n *\n * Memoized per request by `createAgentSubjectResolver` and invoked only on a path this process is\n * about to answer, and only when that path's agent declares a policy.\n */\nfunction agentSubjectResolver(c: RequestHandlerCtx): () => Promise<RouteSubject | null> {\n return createAgentSubjectResolver({\n req: c.req,\n res: c.res,\n loadModule: c.loadModule,\n serverDir: c.serverDir,\n pluginRunner: c.pluginRunner,\n })\n}\n\n/** Apply rate limit; return true if request was limited (response sent). */\nasync function applyRateLimit(c: RequestHandlerCtx, method: string): Promise<boolean> {\n if (!c.rateLimiter) return false\n const check = await c.rateLimiter(c.req)\n for (const [k, v] of Object.entries(check.headers)) c.res.setHeader(k, v)\n if (check.limited) {\n sendError(c.res, 'RATE_LIMITED', 'Too many requests', 429, undefined, c.requestId)\n logRequest({\n method,\n url: c.url,\n status: 429,\n duration: Date.now() - c.startTime,\n requestId: c.requestId,\n })\n return true\n }\n return false\n}\n\n/** Branch 1: action routes (`/api/__actions/{file}/{exportName}`). */\nexport async function tryServeAction(c: RequestHandlerCtx): Promise<boolean> {\n if (!c.url.startsWith('/api/__actions/')) return false\n c.res.setHeader(X_REQUEST_ID, c.requestId)\n\n if (await applyRateLimit(c, c.req.method ?? 'POST')) return true\n\n const pathAfterPrefix = c.url.slice('/api/__actions/'.length).split('?')[0]\n const segments = pathAfterPrefix.split('/').filter(Boolean)\n if (segments.length < 2) {\n sendError(\n c.res,\n 'BAD_REQUEST',\n 'Action URL must be /api/__actions/{file}/{exportName}',\n 400,\n undefined,\n c.requestId,\n )\n logRequest({\n method: c.req.method ?? 'POST',\n url: c.url,\n status: 400,\n duration: Date.now() - c.startTime,\n requestId: c.requestId,\n })\n return true\n }\n const exportName = segments[segments.length - 1]\n const actionPath = segments.slice(0, -1).join('/')\n const action = c.cachedActions.find((a) => a.actionPath === actionPath)\n if (!action) {\n const actionPaths = c.cachedActions.map((a) => a.actionPath)\n const suggestion = findSuggestion(actionPath, actionPaths)\n const msg = suggestion\n ? `Action \"${actionPath}\" not found. Did you mean: ${suggestion}?`\n : `Action \"${actionPath}\" not found`\n sendError(c.res, 'NOT_FOUND', msg, 404, undefined, c.requestId)\n logRequest({\n method: c.req.method ?? 'POST',\n url: c.url,\n status: 404,\n duration: Date.now() - c.startTime,\n requestId: c.requestId,\n })\n return true\n }\n await executeAction(\n action.filePath,\n exportName,\n c.req,\n c.res,\n c.loadModule,\n c.serverDir,\n c.requestId,\n c.pluginRunner,\n c.csrfMode,\n c.disallowed,\n )\n logRequest({\n method: c.req.method ?? 'POST',\n url: c.url,\n status: c.res.statusCode,\n duration: Date.now() - c.startTime,\n requestId: c.requestId,\n })\n return true\n}\n\n/**\n * Branch 1.4: agent AUXILIARY routes served identically in dev + prod (M15/M16 follow-up) — agent\n * cards (`/.well-known/<name>/agent-card.json`), MCP (`/api/agents/<name>/mcp`), the pending-approvals\n * listing, the durable run stream and the two thread routes. Before this, they were dev-only, so a\n * built/deployed app 404'd them.\n *\n * theokit#400 — this branch runs for EVERY url, so it must decide ownership without converting the\n * request: `incomingMessageToWebRequest` drains the Node body stream, and a POST with a JSON body to\n * an ordinary `/api` file route then reached `parseJsonBody` with a readable that had already ended\n * and waited forever for an `'end'` that had already fired — no status, no timeout, no response.\n * `matchAgentAuxRoute` is handed a method and a url and cannot convert anything, so the ordering is\n * now a property of the signature rather than of this comment.\n *\n * usetheokit/theokit#405 — and having a match separate from the answer is what lets the plugin\n * lifecycle run here at all. It did not, in either surface: six endpoints answered without\n * `onRequest`/`onResponse`/`onError`, so an app embedding TheoKit could not observe them and the\n * observability plugin emitted no `http.request` span for the two that spend tokens. The same\n * bracket the plain agent turn uses now wraps this one, from the same function.\n */\nexport async function tryServeAgentAux(c: RequestHandlerCtx): Promise<boolean> {\n const urlPath = c.url.split('?')[0]\n const deps = {\n agents: c.cachedAgents,\n loadModule: c.loadModule,\n baseUrl: `http://${c.req.headers.host ?? 'localhost'}`,\n // M34 (#97) — the MCP aux route drives the agent (spends tokens); enforce CSRF like the run route.\n csrfMode: c.csrfMode,\n // M39 — the thread follow-up route drives the agent; resolve the key on demand.\n // theokit#328 — the thread route drives an agent, so its key follows the model too.\n resolveApiKey: (model: string | undefined, plugins?: readonly unknown[]) =>\n resolveProvider(model, { plugins }).apiKey,\n // usetheokit/theokit#365 — who is asking. Memoized and LAZY: built here, invoked only inside\n // `serveMatchedAuxRoute` and only when the matched agent declares a policy, so the application's\n // `createContext` never runs for a url this branch declines.\n resolveSubject: agentSubjectResolver(c),\n }\n\n const method = (c.req.method ?? 'GET').toUpperCase()\n const route = await matchAgentAuxRoute(method, urlPath, deps)\n if (route === null) return false\n\n c.res.setHeader(X_REQUEST_ID, c.requestId)\n await serveThroughPluginLifecycle(\n {\n source: createWebRequestSource(c.req),\n res: c.res,\n requestId: c.requestId,\n pluginRunner: c.pluginRunner,\n failureMessage: 'Agent aux handler failed',\n },\n async (request) => {\n await writeWebResponseToServerResponse(\n await serveMatchedAuxRoute(route, request, deps),\n c.res,\n )\n },\n )\n logRequest({\n method,\n url: c.url,\n status: c.res.statusCode,\n duration: Date.now() - c.startTime,\n requestId: c.requestId,\n })\n return true\n}\n\n/**\n * Branch 1.5: agent convention routes (`/api/agents/<name>`, M2). Runs BEFORE the generic `/api/*`\n * branch so a scanned `agents/<name>.ts` owns its path (parity with dev). Loads the module, resolves\n * the provider apiKey (fail-fast), and streams the M0/M1 UIMessageStream via `mountAgent`.\n */\nexport async function tryServeAgent(c: RequestHandlerCtx): Promise<boolean> {\n if (!c.url.startsWith('/api/agents/')) return false\n const urlPath = c.url.split('?')[0]\n\n // HITL approve route (`/api/agents/<name>/approve/<id>`, M4) — resolve the pending approval.\n // Handled BEFORE the agent-path exact match (the approve path never equals an `agentPath`).\n if (isApprovalPath(urlPath)) {\n c.res.setHeader(X_REQUEST_ID, c.requestId)\n if (await applyRateLimit(c, c.req.method ?? 'POST')) return true\n const method = (c.req.method ?? 'POST').toUpperCase()\n if (method !== 'POST') {\n sendError(\n c.res,\n 'METHOD_NOT_ALLOWED',\n 'Approve endpoints accept POST',\n 405,\n undefined,\n c.requestId,\n )\n logRequest({\n method,\n url: c.url,\n status: 405,\n duration: Date.now() - c.startTime,\n requestId: c.requestId,\n })\n return true\n }\n // usetheokit/theokit#405 — the approve route settles a human decision and answered with no\n // plugin lifecycle at all, so a hook that fires for every other route never fired for this one\n // and no `http.request` span was emitted for it. Same bracket as the turn below.\n await serveThroughPluginLifecycle(\n {\n source: createWebRequestSource(c.req),\n res: c.res,\n requestId: c.requestId,\n pluginRunner: c.pluginRunner,\n failureMessage: 'Approve handler failed',\n },\n async (request) => {\n // usetheokit/theokit#365 — the approve route settles a paused tool, so it answers to the\n // named agent's declared policy. The resolver is memoized and lazy: `handleAgentApproval`\n // invokes it only when the agent declares one.\n const approveAgent = c.cachedAgents.find((a) => a.name === parseApprovalAgentName(urlPath))\n const policy =\n approveAgent === undefined\n ? undefined\n : readAgentPolicy(await c.loadModule(approveAgent.filePath), approveAgent.filePath)\n const response = await handleAgentApproval(\n request,\n urlPath,\n getApprovalRegistry(),\n c.csrfMode,\n { policy, resolveSubject: agentSubjectResolver(c) },\n )\n await writeWebResponseToServerResponse(response, c.res)\n },\n )\n logRequest({\n method,\n url: c.url,\n status: c.res.statusCode,\n duration: Date.now() - c.startTime,\n requestId: c.requestId,\n })\n return true\n }\n\n const agent = c.cachedAgents.find((a) => a.agentPath === urlPath)\n if (!agent) return false // fall through to the generic /api/* branch (may 404 there)\n\n c.res.setHeader(X_REQUEST_ID, c.requestId)\n if (await applyRateLimit(c, c.req.method ?? 'POST')) return true\n\n const method = (c.req.method ?? 'POST').toUpperCase()\n if (method !== 'POST') {\n sendError(\n c.res,\n 'METHOD_NOT_ALLOWED',\n 'Agent endpoints accept POST',\n 405,\n undefined,\n c.requestId,\n )\n logRequest({\n method,\n url: c.url,\n status: 405,\n duration: Date.now() - c.startTime,\n requestId: c.requestId,\n })\n return true\n }\n\n await serveAgentTurn(c, agent, method)\n return true\n}\n\n/**\n * Serves one agent turn through the plugin lifecycle.\n *\n * Extracted from `tryServeAgent` so that function stays what its name says — a router over the\n * agent paths — while the turn itself, which is what grew a lifecycle, reads in one piece.\n */\nasync function serveAgentTurn(\n c: RequestHandlerCtx,\n agent: { filePath: string; name: string },\n method: string,\n): Promise<void> {\n // theokit#324 — the plugin lifecycle runs here too.\n //\n // This branch used to mount the agent without ever consulting the runner, so `onRequest`,\n // `onResponse` and `onError` fired for every OTHER route and never for an agent turn — leaving an\n // app embedding TheoKit with no supported place to observe or bound agent state. Reported with a repro\n // by a consumer who found it by instrumenting a hook and watching it stay silent — the failure is\n // invisible by construction, since a hook that never runs looks exactly like one with nothing to\n // say.\n //\n // The bracket itself moved to `serveThroughPluginLifecycle` when the aux and approve branches\n // needed the same one (usetheokit/theokit#405): this shape was copied twice and drifted five\n // ways, which is the argument for having one of it.\n const resolveSubject = agentSubjectResolver(c)\n\n await serveThroughPluginLifecycle(\n {\n source: createWebRequestSource(c.req),\n res: c.res,\n requestId: c.requestId,\n pluginRunner: c.pluginRunner,\n failureMessage: 'Agent handler failed',\n },\n async (request) => {\n const mod = await c.loadModule(agent.filePath)\n // theokit#326 — resolve against the model the agent declares, not by env priority.\n const apiKey = (model: string | undefined, plugins?: readonly unknown[]): string =>\n resolveProvider(model, { plugins }).apiKey\n const response = await mountAgent(mod, request, apiKey, {\n source: agent.filePath,\n csrfMode: c.csrfMode,\n projectRoot: c.projectRoot,\n // usetheokit/theokit#365 — the policy is read off `mod` inside `mountAgent`; what this\n // caller owes is the identity to judge it against. usetheokit/theokit#406 — and it is the\n // same value the run's spans are labelled with, so `source` above never reaches telemetry.\n agentName: agent.name,\n resolveSubject,\n })\n await writeWebResponseToServerResponse(response, c.res)\n },\n )\n logRequest({\n method,\n url: c.url,\n status: c.res.statusCode,\n duration: Date.now() - c.startTime,\n requestId: c.requestId,\n })\n}\n\n/** Branch 2: API routes (`/api/*` excluding actions). */\nexport async function tryServeApiRoute(c: RequestHandlerCtx): Promise<boolean> {\n if (!c.url.startsWith('/api/')) return false\n c.res.setHeader(X_REQUEST_ID, c.requestId)\n\n if (await applyRateLimit(c, c.req.method ?? 'GET')) return true\n\n const match = matchRoute(c.url, c.cachedRoutes)\n if (!match) {\n // theokit#123 — controller fall-through, mirroring the dev `api-middleware` arm.\n //\n // Before this, `theokit dev` served a decorator controller and `theokit start` 404'd it: the\n // production path has no Vite/swc transform, so an uncompiled `.controller.ts` could not load.\n // `theokit build` now emits compiled modules and this branch serves them, so the SAME app\n // answers the same routes in both. Reached only after a file-route miss, so file routes keep\n // precedence exactly as in dev.\n if (c.controllersDistDir !== undefined) {\n const handled = await dispatchControllerRequest({\n controllersDir: c.controllersDistDir,\n loadModule: loadCompiledController,\n req: c.req,\n res: c.res,\n csrfMode: c.csrfMode,\n disallowed: c.disallowed,\n requestId: c.requestId,\n // #607 — the runner this branch never received. Without it a `@Controller` route ran no\n // plugin hook at all in production: not `onRequest`, not `preHandler`, not `onResponse`,\n // not `onError`. The file-route branch below has always passed it.\n pluginRunner: c.pluginRunner,\n })\n if (handled) return true\n }\n const urlPath = c.url.split('?')[0]\n const routePaths = c.cachedRoutes.map((r) => r.routePath)\n const suggestion = findSuggestion(urlPath, routePaths)\n const msg = suggestion\n ? `API route not found: ${urlPath}. Did you mean: ${suggestion}?`\n : 'API route not found'\n sendError(c.res, 'NOT_FOUND', msg, 404, undefined, c.requestId)\n logRequest({\n method: c.req.method ?? 'GET',\n url: c.url,\n status: 404,\n duration: Date.now() - c.startTime,\n requestId: c.requestId,\n })\n return true\n }\n const method = (c.req.method ?? 'GET').toUpperCase()\n // T3.1 (ADR-0016) — context object replaces 12 positional args\n await executeRoute({\n route: match.route,\n method,\n params: match.params,\n req: c.req,\n res: c.res,\n loadModule: c.loadModule,\n serverDir: c.serverDir,\n requestId: c.requestId,\n pluginRunner: c.pluginRunner,\n transformer: c.transformer,\n csrfMode: c.csrfMode,\n disallowed: c.disallowed,\n jobBackend: c.jobBackend,\n })\n logRequest({\n method,\n url: c.url,\n status: c.res.statusCode,\n duration: Date.now() - c.startTime,\n requestId: c.requestId,\n })\n return true\n}\n\n/** Branch 3: static files (returns true if a static asset was served). */\nexport function tryServeStatic(c: RequestHandlerCtx): boolean {\n return serveStaticFile(c.req, c.res, c.clientDir)\n}\n\n/** Branch 4: custom 404 for URLs that look like missing assets. */\nexport function tryServeCustom404(c: RequestHandlerCtx): boolean {\n const urlPath = c.url.split('?')[0]\n if (c.custom404Html && extname(urlPath)) {\n c.res.writeHead(404, { 'Content-Type': 'text/html' })\n c.res.end(c.custom404Html)\n return true\n }\n return false\n}\n","/* eslint-disable security/detect-non-literal-fs-filename --\n * Static-file server. The URL path IS user-controlled, so every fs call here is guarded twice\n * before it runs: a string check that rejects a URL walking out with `..` (403), and then a\n * `realpath` check that asks the filesystem whether the target actually lives under `clientDir`.\n *\n * The second guard is not decoration. This header used to claim the string check alone was\n * authoritative, and #428 disproved it: `path.resolve` never touches the disk, so a symlink inside\n * `clientDir` sailed through it and the server returned a file from anywhere on the host. Both\n * guards are now required for that claim to hold.\n */\nimport { closeSync, fstatSync, openSync, readFileSync, realpathSync } from 'node:fs'\nimport type { IncomingMessage, ServerResponse } from 'node:http'\nimport { resolve, extname, sep } from 'node:path'\n\nconst MIME_TYPES: Record<string, string> = {\n '.html': 'text/html',\n '.js': 'application/javascript',\n '.mjs': 'application/javascript',\n '.css': 'text/css',\n '.json': 'application/json',\n '.png': 'image/png',\n '.jpg': 'image/jpeg',\n '.jpeg': 'image/jpeg',\n '.gif': 'image/gif',\n '.svg': 'image/svg+xml',\n '.ico': 'image/x-icon',\n '.woff': 'font/woff',\n '.woff2': 'font/woff2',\n '.ttf': 'font/ttf',\n '.txt': 'text/plain',\n '.map': 'application/json',\n}\n\nexport function serveStaticFile(\n req: IncomingMessage,\n res: ServerResponse,\n clientDir: string,\n): boolean {\n const urlPath = (req.url ?? '/').split('?')[0]\n\n // Path traversal prevention (EC-1) — rejects a URL that walks out with `..`.\n // `sep` matters: without it `/srv/client-backup` passes as \"inside\" `/srv/client`.\n const filePath = resolve(clientDir, '.' + urlPath)\n if (filePath !== clientDir && !filePath.startsWith(clientDir + sep)) {\n res.writeHead(403)\n res.end('Forbidden')\n return true\n }\n\n // #428 — the check above is string arithmetic; `resolve` never touches the disk, so it cannot\n // see that an entry inside `clientDir` IS a file somewhere else. Ask the filesystem instead.\n // Symlinks are not banned — one that stays inside the served tree is ordinary and still served;\n // only the ones that leave are refused, and they are refused as \"not here\" rather than 403 so\n // the response does not confirm what lies outside.\n let realPath: string\n let realRoot: string\n try {\n realPath = realpathSync(filePath)\n realRoot = realpathSync(clientDir)\n } catch {\n return false // missing, unreadable, or a broken link — all \"not served\"\n }\n if (realPath !== realRoot && !realPath.startsWith(realRoot + sep)) return false\n\n // One descriptor for the type check and the bytes: re-opening by path between them is what\n // lets the file that was checked differ from the file that is served (CodeQL js/file-system-race).\n let fd: number\n try {\n fd = openSync(realPath, 'r')\n } catch {\n return false\n }\n try {\n if (!fstatSync(fd).isFile()) return false\n\n const ext = extname(filePath)\n const contentType = MIME_TYPES[ext] ?? 'application/octet-stream'\n const content = readFileSync(fd)\n\n res.writeHead(200, {\n 'Content-Type': contentType,\n 'Content-Length': content.length,\n })\n res.end(content)\n return true\n } finally {\n closeSync(fd)\n }\n}\n","/**\n * SSR setup stage for `theokit start` (T4.2 architecture-cleanup).\n *\n * Loads the SSR entry-server module if configured + builds template split\n * around the React root div. Returns null renderers when SSR is disabled.\n */\n\nimport { readFileSync } from 'node:fs'\nimport type { ServerResponse } from 'node:http'\n\nimport { findRootDiv } from '../../../core/contracts/find-root-div.js'\nimport { parseAssetsMap } from '../../../core/contracts/module-preloads.js'\n\nimport { resolveSsrEntry } from './bootstrap-stages.js'\n\nexport interface SsrRenderResult {\n html: string\n hydrationData: {\n loaderData?: unknown\n actionData?: unknown\n errors?: unknown\n }\n}\n\nexport type RenderStreamingResult = { redirect: Response } | { streaming: true } | undefined\n\nexport type SsrRender = (\n url: string,\n options?: { nonce?: string },\n) => Promise<SsrRenderResult | { redirect: Response } | string>\n\nexport type SsrRenderStreaming = (\n url: string,\n response: ServerResponse,\n options?: {\n signal?: AbortSignal\n nonce?: string\n /**\n * The template up to and including `<div id=\"root\">`, written before React\n * produces a byte. Without it the streamed response is React's output alone\n * — no `<html>`, no `<head>` (usetheokit/theokit#343).\n */\n htmlHead?: string\n /** The template after `</div>`, written after the hydration data script. */\n htmlTail?: string\n },\n) => Promise<RenderStreamingResult>\n\ninterface SsrSetupResult {\n enabled: boolean\n streamingEnabled: boolean\n render: SsrRender | null\n renderStreaming: SsrRenderStreaming | null\n htmlHead: string\n htmlTail: string\n}\n\nexport function isSsrRenderResult(value: unknown): value is SsrRenderResult {\n if (typeof value !== 'object' || value === null) return false\n if (!('html' in value)) return false\n const html = (value as Record<string, unknown>).html\n if (typeof html !== 'string') return false\n return true\n}\n\ninterface SsrEntryServer {\n render: SsrRender\n renderStreaming?: SsrRenderStreaming\n}\n\nexport async function setupSsr(opts: {\n distDir: string\n indexHtml: string\n ssrConfigEnabled: boolean\n ssrStreamingConfig: boolean | undefined\n}): Promise<SsrSetupResult> {\n const ssrServerPath: string | null = opts.ssrConfigEnabled ? resolveSsrEntry(opts.distDir) : null\n const enabled = ssrServerPath !== null\n const streamingEnabled = enabled && Boolean(opts.ssrStreamingConfig)\n\n if (ssrServerPath === null) {\n return {\n enabled: false,\n streamingEnabled: false,\n render: null,\n renderStreaming: null,\n htmlHead: '',\n htmlTail: '',\n }\n }\n\n const mod = (await import(ssrServerPath)) as SsrEntryServer\n const render = mod.render\n const renderStreaming = typeof mod.renderStreaming === 'function' ? mod.renderStreaming : null\n\n // Split HTML template on root div\n let htmlHead = ''\n let htmlTail = ''\n const rootDiv = findRootDiv(opts.indexHtml)\n if (rootDiv) {\n htmlHead = opts.indexHtml.slice(0, rootDiv.insertAt)\n htmlTail = opts.indexHtml.slice(rootDiv.insertAt)\n }\n\n return { enabled, streamingEnabled, render, renderStreaming, htmlHead, htmlTail }\n}\n\n/**\n * Read the route-to-chunks map `theokit build` emits (B-035).\n *\n * Returns `undefined` — never throws — when the file is absent or unreadable. A server that\n * refused to boot because a build predated this feature would turn a missing optimisation into an\n * outage, and the document is correct without it: it simply learns about its chunks one round trip\n * later, which is exactly the state this feature improves on.\n */\nexport function loadAssetsMap(path: string): Record<string, string[]> | undefined {\n try {\n return parseAssetsMap(readFileSync(path, 'utf8'))\n } catch {\n return undefined\n }\n}\n","/**\n * Inline request handler for `theokit start` (T4.2 architecture-cleanup).\n *\n * Wires the per-request flow: security headers → action/route/static branches\n * → SSR fallback → CSR fallback → 500 page.\n *\n * Refactored: CC reduced from 33 to ≤10 per function\n * (architecture-remediation T2.1, 2026-06-12).\n */\n\nimport type { IncomingMessage, ServerResponse } from 'node:http'\n\nimport { injectModulePreloads } from '../../../core/contracts/module-preloads.js'\nimport { generateNonce } from '../../../server/auth/nonce.js'\nimport { type ReservedRoutes, serveReservedRoute } from '../../../server/define/health-route.js'\nimport type { CorsHandler } from '../../../server/http/cors.js'\nimport { sendError } from '../../../server/http/send-response.js'\nimport { TRACE_HEADER, extractTraceId } from '../../../server/http/trace-context.js'\nimport { buildSecurityHeaders } from '../../../server/security/security-headers.js'\nimport { extractHeadTags, injectIntoHead } from '../../../vite-plugin/hoist-head-tags.js'\nimport { applyNonceToInlineScripts } from '../../../vite-plugin/ssr-dev-middleware.js'\n\nimport {\n tryServeAction,\n tryServeAgent,\n tryServeAgentAux,\n tryServeApiRoute,\n tryServeCustom404,\n tryServeStatic,\n type RequestHandlerCtx,\n} from './handlers.js'\nimport {\n isSsrRenderResult,\n type SsrRender,\n type SsrRenderResult,\n type SsrRenderStreaming,\n} from './ssr-setup.js'\n\ninterface RequestHandlerContext {\n buildCtx: (\n req: IncomingMessage,\n res: ServerResponse,\n requestId: string,\n startTime: number,\n ) => RequestHandlerCtx\n securityHeadersConfig: Parameters<typeof buildSecurityHeaders>[0]\n /**\n * #409 — `security.cors` had exactly one consumer, Vite's `configureServer` hook, so an app that\n * worked cross-origin under `theokit dev` stopped working the moment this command served it:\n * same config, same code, no error and no warning. `null` when the app declared no `cors` block,\n * which is what it meant before and still means — no headers, not permissive ones.\n */\n corsHandler: CorsHandler | null\n ssrRender: SsrRender | null\n ssrRenderStreaming: SsrRenderStreaming | null\n ssrStreamingEnabled: boolean\n htmlHead: string\n htmlTail: string\n /**\n * B-035 — the route-to-chunks relation `theokit build` emits, read ONCE at startup and never\n * per request: it is a build artifact and cannot change while the server runs. Absent when the\n * build predates the map, which serves without preloads rather than failing the request.\n */\n assetsMap?: Record<string, string[]>\n indexHtml: string\n custom500Html: string | null\n /** M7-2: reserved health/ready routes served before the user catch-all. */\n reservedRoutes?: ReservedRoutes\n}\n\n/**\n * M7-2: serve a reserved `/__theo/*` route (health/ready) before any user\n * branch. Returns true when the request was handled.\n */\nasync function tryServeReserved(\n ctx: RequestHandlerContext,\n url: string,\n res: ServerResponse,\n): Promise<boolean> {\n const pathname = new URL(url, 'http://localhost').pathname\n const reserved = await serveReservedRoute(pathname, ctx.reservedRoutes ?? {})\n if (reserved === null) return false\n const payload = JSON.stringify(reserved.body)\n res.writeHead(reserved.status, {\n 'Content-Type': 'application/json',\n 'Content-Length': Buffer.byteLength(payload),\n })\n res.end(payload)\n return true\n}\n\nfunction asSsrRenderResult(value: SsrRenderResult): SsrRenderResult {\n return value\n}\n\nfunction isRedirectResult(result: unknown): result is { redirect: Response } {\n return result !== null && typeof result === 'object' && 'redirect' in result\n}\n\nfunction sendRedirect(res: ServerResponse, result: { redirect: Response }): void {\n res.writeHead(302, { Location: result.redirect.headers.get('location') ?? '/' })\n res.end()\n}\n\nfunction send500(res: ServerResponse, custom500Html: string | null): void {\n if (!res.headersSent) {\n res.writeHead(500, { 'Content-Type': 'text/html' })\n }\n if (!res.writableEnded) {\n res.end(custom500Html ?? '<h1>500 — Server Error</h1>')\n }\n}\n\n/**\n * Moves the route's `<title>`/`<meta>`/`<link>` from the rendered body into the head.\n *\n * `ctx.htmlHead` is the template up to and including `<div id=\"root\">`, so it still contains the\n * `</head>` this injects before. Without it, a route's metadata ships inside the body and only\n * reaches the head after hydration — which never happens for a crawler that does not run\n * JavaScript, and those are exactly the ones that render social cards.\n *\n * The dev middleware does the same thing; both paths have to, or previews work in one and not the\n * other, which is worse than neither.\n */\nexport function withHoistedHead(\n htmlHead: string,\n ssrHtml: string,\n nonce: string,\n): { head: string; body: string } {\n const { html, headTags } = extractHeadTags(ssrHtml)\n\n // The nonce is stamped onto the template's own inline scripts here, per request, because the\n // nonce differs per request while `ctx.htmlHead` is computed once at startup. Without it, a CSP\n // with a nonce blocks anything the template inlines — including the theme-init script that\n // applications put in `<head>` specifically to avoid a flash of the wrong theme on load.\n const head = applyNonceToInlineScripts(injectIntoHead(htmlHead, headTags), nonce)\n return { head, body: html }\n}\n\nexport function buildSsrHtml(\n ctx: RequestHandlerContext,\n result: string | SsrRenderResult,\n nonce: string,\n url: string,\n): string {\n if (typeof result === 'string') {\n const { head, body } = withHoistedHead(ctx.htmlHead, result, nonce)\n return injectModulePreloads(head, ctx.assetsMap, url) + body + ctx.htmlTail\n }\n if (isSsrRenderResult(result)) {\n const rendered = asSsrRenderResult(result)\n const dataJson = JSON.stringify(rendered.hydrationData).replace(/</g, '\\\\u003c')\n const hydrationScript = `<script${\n nonce ? ` nonce=\"${nonce}\"` : ''\n }>window.__staticRouterHydrationData=${dataJson}</script>`\n const { head, body } = withHoistedHead(ctx.htmlHead, rendered.html, nonce)\n return injectModulePreloads(head, ctx.assetsMap, url) + body + hydrationScript + ctx.htmlTail\n }\n return applyNonceToInlineScripts(ctx.htmlHead, nonce) + ctx.htmlTail\n}\n\nasync function handleSsrStreaming(\n ctx: RequestHandlerContext,\n req: IncomingMessage,\n res: ServerResponse,\n url: string,\n nonce: string,\n): Promise<boolean> {\n if (!ctx.ssrStreamingEnabled || !ctx.ssrRenderStreaming) return false\n\n const controller = new AbortController()\n const onClose = (): void => {\n controller.abort()\n }\n req.on('close', onClose)\n try {\n const result = await ctx.ssrRenderStreaming(url, res, {\n signal: controller.signal,\n nonce,\n // #343 — the streamed response carried neither of these, so `ssrStreaming: true`\n // served a bare React tree: no `<head>`, no client entry, no hydration data.\n //\n // `applyNonceToInlineScripts` and not `withHoistedHead`: hoisting reads the\n // RENDERED body for head elements, and nothing is rendered yet when the head\n // has to flush. Metadata hoisting under streaming is the same defect on a\n // different surface, and it is M9's, not this one's.\n // B-035, found by the independent code-review audit: the preloads were wired into\n // `buildSsrHtml`, which serves the SYNCHRONOUS path only, while this branch is dispatched\n // FIRST — so `ssrStreaming: true` turned the feature off on Node while the Cloudflare\n // worker injected on its equivalent branch. The exclusion argued just above is about\n // METADATA hoisting, which needs the rendered body; a `modulepreload` link needs nothing\n // from it, so that argument does not reach here.\n htmlHead: injectModulePreloads(\n applyNonceToInlineScripts(ctx.htmlHead, nonce),\n ctx.assetsMap,\n url,\n ),\n htmlTail: ctx.htmlTail,\n })\n if (isRedirectResult(result)) sendRedirect(res, result)\n return true\n } catch (streamErr) {\n console.error('[SSR Stream Error]', (streamErr as Error).message)\n send500(res, ctx.custom500Html)\n return true\n } finally {\n req.removeListener('close', onClose)\n }\n}\n\nasync function handleSsrSync(\n ctx: RequestHandlerContext,\n res: ServerResponse,\n url: string,\n nonce: string,\n): Promise<boolean> {\n if (!ctx.ssrRender) return false\n\n try {\n const result = await ctx.ssrRender(url, { nonce })\n if (isRedirectResult(result)) {\n sendRedirect(res, result)\n return true\n }\n res.writeHead(200, { 'Content-Type': 'text/html' })\n res.end(buildSsrHtml(ctx, result, nonce, url))\n return true\n } catch (ssrErr) {\n console.error('[SSR Error] Falling back to CSR:', (ssrErr as Error).message)\n return false\n }\n}\n\nfunction handleFatalError(ctx: RequestHandlerContext, res: ServerResponse, err: unknown): void {\n if (ctx.custom500Html && !res.headersSent) {\n res.writeHead(500, { 'Content-Type': 'text/html' })\n res.end(ctx.custom500Html)\n } else if (!res.headersSent) {\n sendError(res, 'INTERNAL_ERROR', (err as Error).message, 500)\n } else {\n res.end()\n }\n}\n\nexport function createRequestHandler(\n ctx: RequestHandlerContext,\n): (req: IncomingMessage, res: ServerResponse) => void {\n return (req: IncomingMessage, res: ServerResponse) => {\n void (async () => {\n const url = req.url ?? '/'\n // #353 — production minted a fresh UUID on every request and discarded the\n // incoming W3C `traceparent`, so a trace crossing into this server started\n // over and no span could continue it. `theo dev` has honoured the header on\n // `/api/*` since Phase 7 (`vite-plugin/api-middleware.ts:368`); this is the\n // same resolution on the path a deploy actually serves.\n //\n // The tier-2 fallback (`x-request-id`) is caller-controlled and validated\n // inside `extractTraceId` before it is trusted — it ends up in the logs.\n const requestId = extractTraceId(req)\n const start = Date.now()\n // Echoed under both names, matching dev: `x-request-id` is what existing\n // consumers read, `x-trace-id` is the canonical one.\n res.setHeader('x-request-id', requestId)\n res.setHeader(TRACE_HEADER, requestId)\n\n // Preflight before anything else, and it answers rather than routing — the same order the\n // dev middleware uses (`vite-plugin/api-middleware.ts`), because an OPTIONS the router\n // handles is an OPTIONS the browser never gets a CORS answer to.\n if (ctx.corsHandler?.handlePreflight(req, res) === true) return\n ctx.corsHandler?.applyHeaders(req, res)\n\n const nonce = generateNonce()\n const securityHeaders = buildSecurityHeaders(\n ctx.securityHeadersConfig,\n { production: true },\n { nonce },\n )\n for (const [k, v] of Object.entries(securityHeaders)) {\n res.setHeader(k, v)\n }\n\n const handlerCtx = ctx.buildCtx(req, res, requestId, start)\n\n try {\n if (await tryServeReserved(ctx, url, res)) return\n if (await tryServeAction(handlerCtx)) return\n if (await tryServeAgentAux(handlerCtx)) return\n if (await tryServeAgent(handlerCtx)) return\n if (await tryServeApiRoute(handlerCtx)) return\n if (tryServeStatic(handlerCtx)) return\n if (tryServeCustom404(handlerCtx)) return\n\n if (await handleSsrStreaming(ctx, req, res, url, nonce)) return\n if (await handleSsrSync(ctx, res, url, nonce)) return\n\n // CSR fallback\n res.writeHead(200, { 'Content-Type': 'text/html' })\n res.end(ctx.indexHtml)\n } catch (err) {\n handleFatalError(ctx, res, err)\n }\n })()\n }\n}\n","/**\n * Resolve the address `server.listen` should bind, and say which it is.\n *\n * `config.host` was declared, defaulted to `'localhost'`, documented as the way to\n * open a server to the LAN, and never passed to `listen`. Node with no address\n * binds EVERY interface, so the production server listened wider than its\n * configuration said — and its default said the narrow thing.\n *\n * Fixing that broke containers, which is the second half of this story\n * (usetheokit/theokit#402). Inside a container `localhost` means *nobody*: the\n * image starts, prints a URL, and refuses every request including its own. So the\n * environment gets a say. `HOST` is the variable every container platform already\n * sets or expects to set, and honouring it costs the operator nothing to discover.\n *\n * Precedence, narrowest authority last: explicit config beats `HOST`, because a\n * value written into the project is a decision and an environment variable is a\n * deployment detail. Absent both, the loopback — binding every interface is a\n * decision someone should have to write down.\n *\n * The second export exists because the log lied. `theo start` printed `localhost`\n * whether it bound the loopback or every interface, so a container that serves\n * everyone and a container that serves nobody produced byte-identical output.\n * That is `docs/adr/0002-an-abnormal-ending-is-never-reported-as-normal.md` in the\n * startup path: the observable state must distinguish the two.\n */\n\nexport interface ListenTarget {\n /** The address handed to `server.listen`. */\n readonly host: string\n /** Where the value came from, so the log can say so rather than guess. */\n readonly source: 'config' | 'env' | 'default'\n}\n\nexport function resolveListenTarget(\n host: string | boolean | undefined,\n env: string | undefined = process.env.HOST,\n): ListenTarget {\n if (host === true) return { host: '0.0.0.0', source: 'config' }\n if (typeof host === 'string' && host !== '') return { host, source: 'config' }\n // `host: false` is an explicit \"do not open me up\" and outranks the environment.\n if (host !== false && env !== undefined && env !== '') return { host: env, source: 'env' }\n return { host: 'localhost', source: 'default' }\n}\n\n/**\n * The line `theo start` prints, given what it actually bound.\n *\n * `0.0.0.0` is not a URL a human can open, so the loopback is offered — but the\n * bound address is stated beside it, because \"every interface\" and \"this machine\n * only\" are the two states an operator most needs to tell apart, and they were\n * indistinguishable.\n */\nexport function describeListenTarget(target: ListenTarget, port: number): string {\n const url = target.host === '0.0.0.0' || target.host === '::' ? 'localhost' : target.host\n const bound =\n target.host === '0.0.0.0' || target.host === '::'\n ? `bound to ${target.host} (every interface)`\n : `bound to ${target.host} only`\n return ` → http://${url}:${String(port)} [${bound}${provenance(target.source)}]`\n}\n\n/** Where the address came from, and — when nobody chose it — what to write to change it. */\nfunction provenance(source: ListenTarget['source']): string {\n if (source === 'env') return ' from HOST'\n if (source === 'config') return ''\n return ' — set `host: true` in theo.config.ts or HOST=0.0.0.0 to reach it from outside this machine'\n}\n","/**\n * WebSocket upgrade handler for `theokit start` (T4.2 architecture-cleanup).\n *\n * Wires `server.on('upgrade')` for declared WS routes. Opt-in: only attached\n * when `wsRoutes.length > 0`. Lazy-imports `ws` package — throws an actionable\n * error when wsRoutes declared but `ws` not installed.\n */\n\nimport type { Server as HttpServer } from 'node:http'\n\nimport type * as WsLib from 'ws'\n\nimport type { WebSocketHandler } from '../../../server/define/define-websocket.js'\nimport type { LoadModule } from '../../../server/scan/module-loader.js'\nimport type { WebSocketRouteNode } from '../../../server/scan/ws-scan.js'\n\nexport async function attachWebSocketHandler(\n server: HttpServer,\n wsRoutes: WebSocketRouteNode[],\n loadModule: LoadModule,\n): Promise<void> {\n if (wsRoutes.length === 0) return\n\n let WebSocketServerCtor: typeof WsLib.WebSocketServer\n try {\n const wsModule = await import('ws')\n WebSocketServerCtor = wsModule.WebSocketServer\n } catch {\n throw new Error('WebSocket routes found but \"ws\" package is not installed. Run: npm install ws')\n }\n\n const wss = new WebSocketServerCtor({ noServer: true })\n\n server.on('upgrade', (request, socket, head) => {\n void (async () => {\n const url = request.url ?? '/'\n if (!url.startsWith('/ws/')) {\n socket.destroy()\n return\n }\n\n const wsPath = url.split('?')[0]\n const match = wsRoutes.find((r) => r.wsPath === wsPath)\n if (!match) {\n socket.destroy()\n return\n }\n\n try {\n const mod = await loadModule(match.filePath)\n const handler = ((mod as { default?: unknown }).default ?? mod) as WebSocketHandler\n\n wss.handleUpgrade(request, socket, head, (ws) => {\n handler.onOpen?.(ws, request)\n ws.on('message', (data: Buffer) => {\n handler.onMessage?.(ws, data.toString())\n })\n ws.on('close', (code: number, reason: Buffer) => {\n handler.onClose?.(ws, code, reason)\n })\n ws.on('error', (err: Error) => {\n handler.onError?.(ws, err)\n })\n })\n } catch {\n socket.destroy()\n }\n })()\n })\n}\n"],"mappings":";;;;;;;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,QAAQ,WAAW,UAAW,SAAQ,IAAI,KAAK,IAAI;AAAA,CAAI;AAAA,EACpE,CAAC;AAED,0BAAwB,QAAQ,MAAM,WAAW,KAAK,CAAC;AACzD;","names":["existsSync","readFileSync","join","resolve","require","resolve","extname","readFileSync","resolve","method","extname","readFileSync","readFileSync","resolve","existsSync","readFileSync","join"]}
File without changes