prompt.ts 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. import { feature } from 'bun:bundle'
  2. import { getFeatureValue_CACHED_WITH_REFRESH } from '../../services/analytics/growthbook.js'
  3. import { DEFAULT_CRON_JITTER_CONFIG } from '../../utils/cronTasks.js'
  4. import { isEnvTruthy } from '../../utils/envUtils.js'
  5. const KAIROS_CRON_REFRESH_MS = 5 * 60 * 1000
  6. export const DEFAULT_MAX_AGE_DAYS =
  7. DEFAULT_CRON_JITTER_CONFIG.recurringMaxAgeMs / (24 * 60 * 60 * 1000)
  8. /**
  9. * Unified gate for the cron scheduling system. Combines the build-time
  10. * `feature('AGENT_TRIGGERS')` flag (dead code elimination) with the runtime
  11. * `tengu_kairos_cron` GrowthBook gate on a 5-minute refresh window.
  12. *
  13. * AGENT_TRIGGERS is independently shippable from KAIROS — the cron module
  14. * graph (cronScheduler/cronTasks/cronTasksLock/cron.ts + the three tools +
  15. * /loop skill) has zero imports into src/assistant/ and no feature('KAIROS')
  16. * calls. The REPL.tsx kairosEnabled read is safe:
  17. * kairosEnabled is unconditionally in AppStateStore with default false, so
  18. * when KAIROS is off the scheduler just gets assistantMode: false.
  19. *
  20. * Called from Tool.isEnabled() (lazy, post-init) and inside useEffect /
  21. * imperative setup, never at module scope — so the disk cache has had a
  22. * chance to populate.
  23. *
  24. * The default is `true` — /loop is GA (announced in changelog). GrowthBook
  25. * is disabled for Bedrock/Vertex/Foundry and when DISABLE_TELEMETRY /
  26. * CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC are set; a `false` default would
  27. * break /loop for those users (GH #31759). The GB gate now serves purely as
  28. * a fleet-wide kill switch — flipping it to `false` stops already-running
  29. * schedulers on their next isKilled poll tick, not just new ones.
  30. *
  31. * `CLAUDE_CODE_DISABLE_CRON` is a local override that wins over GB.
  32. */
  33. export function isKairosCronEnabled(): boolean {
  34. return !isEnvTruthy(process.env.CLAUDE_CODE_DISABLE_CRON)
  35. }
  36. /**
  37. * Kill switch for disk-persistent (durable) cron tasks. Narrower than
  38. * {@link isKairosCronEnabled} — flipping this off forces `durable: false` at
  39. * the call() site, leaving session-only cron (in-memory, GA) untouched.
  40. *
  41. * Defaults to `true` so Bedrock/Vertex/Foundry and DISABLE_TELEMETRY users get
  42. * durable cron. Does NOT consult CLAUDE_CODE_DISABLE_CRON (that kills the whole
  43. * scheduler via isKairosCronEnabled).
  44. */
  45. export function isDurableCronEnabled(): boolean {
  46. return getFeatureValue_CACHED_WITH_REFRESH(
  47. 'tengu_kairos_cron_durable',
  48. true,
  49. KAIROS_CRON_REFRESH_MS,
  50. )
  51. }
  52. export const CRON_CREATE_TOOL_NAME = 'CronCreate'
  53. export const CRON_DELETE_TOOL_NAME = 'CronDelete'
  54. export const CRON_LIST_TOOL_NAME = 'CronList'
  55. export function buildCronCreateDescription(durableEnabled: boolean): string {
  56. return durableEnabled
  57. ? 'Schedule a prompt to run at a future time — either recurring on a cron schedule, or once at a specific time. Pass durable: true to persist to .claude/scheduled_tasks.json; otherwise session-only.'
  58. : 'Schedule a prompt to run at a future time within this Claude session — either recurring on a cron schedule, or once at a specific time.'
  59. }
  60. export function buildCronCreatePrompt(durableEnabled: boolean): string {
  61. const durabilitySection = durableEnabled
  62. ? `## Durability
  63. By default (durable: false) the job lives only in this Claude session — nothing is written to disk, and the job is gone when Claude exits. Pass durable: true to write to .claude/scheduled_tasks.json so the job survives restarts. Only use durable: true when the user explicitly asks for the task to persist ("keep doing this every day", "set this up permanently"). Most "remind me in 5 minutes" / "check back in an hour" requests should stay session-only.`
  64. : `## Session-only
  65. Jobs live only in this Claude session — nothing is written to disk, and the job is gone when Claude exits.`
  66. const durableRuntimeNote = durableEnabled
  67. ? 'Durable jobs persist to .claude/scheduled_tasks.json and survive session restarts — on next launch they resume automatically. One-shot durable tasks that were missed while the REPL was closed are surfaced for catch-up. Session-only jobs die with the process. '
  68. : ''
  69. return `Schedule a prompt to be enqueued at a future time. Use for both recurring schedules and one-shot reminders.
  70. Uses standard 5-field cron in the user's local timezone: minute hour day-of-month month day-of-week. "0 9 * * *" means 9am local — no timezone conversion needed.
  71. ## One-shot tasks (recurring: false)
  72. For "remind me at X" or "at <time>, do Y" requests — fire once then auto-delete.
  73. Pin minute/hour/day-of-month/month to specific values:
  74. "remind me at 2:30pm today to check the deploy" → cron: "30 14 <today_dom> <today_month> *", recurring: false
  75. "tomorrow morning, run the smoke test" → cron: "57 8 <tomorrow_dom> <tomorrow_month> *", recurring: false
  76. ## Recurring jobs (recurring: true, the default)
  77. For "every N minutes" / "every hour" / "weekdays at 9am" requests:
  78. "*/5 * * * *" (every 5 min), "0 * * * *" (hourly), "0 9 * * 1-5" (weekdays at 9am local)
  79. ## Avoid the :00 and :30 minute marks when the task allows it
  80. Every user who asks for "9am" gets \`0 9\`, and every user who asks for "hourly" gets \`0 *\` — which means requests from across the planet land on the API at the same instant. When the user's request is approximate, pick a minute that is NOT 0 or 30:
  81. "every morning around 9" → "57 8 * * *" or "3 9 * * *" (not "0 9 * * *")
  82. "hourly" → "7 * * * *" (not "0 * * * *")
  83. "in an hour or so, remind me to..." → pick whatever minute you land on, don't round
  84. Only use minute 0 or 30 when the user names that exact time and clearly means it ("at 9:00 sharp", "at half past", coordinating with a meeting). When in doubt, nudge a few minutes early or late — the user will not notice, and the fleet will.
  85. ${durabilitySection}
  86. ## Runtime behavior
  87. Jobs only fire while the REPL is idle (not mid-query). ${durableRuntimeNote}The scheduler adds a small deterministic jitter on top of whatever you pick: recurring tasks fire up to 10% of their period late (max 15 min); one-shot tasks landing on :00 or :30 fire up to 90 s early. Picking an off-minute is still the bigger lever.
  88. Recurring tasks auto-expire after ${DEFAULT_MAX_AGE_DAYS} days — they fire one final time, then are deleted. This bounds session lifetime. Tell the user about the ${DEFAULT_MAX_AGE_DAYS}-day limit when scheduling recurring jobs.
  89. Returns a job ID you can pass to ${CRON_DELETE_TOOL_NAME}.`
  90. }
  91. export const CRON_DELETE_DESCRIPTION = 'Cancel a scheduled cron job by ID'
  92. export function buildCronDeletePrompt(durableEnabled: boolean): string {
  93. return durableEnabled
  94. ? `Cancel a cron job previously scheduled with ${CRON_CREATE_TOOL_NAME}. Removes it from .claude/scheduled_tasks.json (durable jobs) or the in-memory session store (session-only jobs).`
  95. : `Cancel a cron job previously scheduled with ${CRON_CREATE_TOOL_NAME}. Removes it from the in-memory session store.`
  96. }
  97. export const CRON_LIST_DESCRIPTION = 'List scheduled cron jobs'
  98. export function buildCronListPrompt(durableEnabled: boolean): string {
  99. return durableEnabled
  100. ? `List all cron jobs scheduled via ${CRON_CREATE_TOOL_NAME}, both durable (.claude/scheduled_tasks.json) and session-only.`
  101. : `List all cron jobs scheduled via ${CRON_CREATE_TOOL_NAME} in this session.`
  102. }