cron-purpose.ts 1.4 KB

1234567891011121314151617181920212223242526272829303132
  1. /** Cron task name used for the daemon-managed memory cleanup automation. */
  2. export const MEMORY_CLEANUP_CRON_NAME = 'memory-cleanup';
  3. /** Stable prefix for sessions created by cron-like background automations. */
  4. export const CRON_SESSION_PURPOSE_PREFIX = 'cron:';
  5. /** Build a machine-readable purpose for sessions spawned by a cron-like task. */
  6. export function createCronSessionPurpose(agentName: string, cronName: string): string {
  7. return `${CRON_SESSION_PURPOSE_PREFIX}${agentName}:${cronName}`;
  8. }
  9. /** Format the user-visible title for a Cron-created session. */
  10. export function formatCronRunTitle(cronName: string, _input?: number | Date): string {
  11. return cronName;
  12. }
  13. /** Parse `cron:<agentName>:<cronName>` purpose values written into session metadata. */
  14. export function parseCronSessionPurpose(
  15. purpose: string | undefined,
  16. ): { agentName: string; cronName: string } | null {
  17. if (!purpose?.startsWith(CRON_SESSION_PURPOSE_PREFIX)) return null;
  18. const parts = purpose.slice(CRON_SESSION_PURPOSE_PREFIX.length).split(':');
  19. if (parts.length < 2) return null;
  20. const [agentName, ...rest] = parts;
  21. const cronName = rest.join(':');
  22. return agentName && cronName ? { agentName, cronName } : null;
  23. }
  24. /** Legacy memory-cleanup sessions predate machine-readable cron purpose metadata. */
  25. export function isLegacyMemoryCleanupTitle(title: string | undefined): boolean {
  26. return /^Memory cleanup \d{4}-\d{2}-\d{2}$/.test(title ?? '');
  27. }