1+ // Human-facing background task commands.
2+ // Handles task listing/show/cancel/notify/audit plus registry maintenance for tasks, flows, and sessions.
3+
14import fs from "node:fs" ;
25import { timestampMsToIsoString } from "@openclaw/normalization-core/number-coercion" ;
36import { normalizeOptionalString } from "@openclaw/normalization-core/string-coerce" ;
@@ -129,6 +132,7 @@ function buildSessionRegistryPreserveKeys(params: {
129132 for ( const key of Object . keys ( params . store ) ) {
130133 const jobId = parseCronRunSessionJobId ( key ) ;
131134 if ( ! jobId ) {
135+ // Non-cron session rows are outside this maintenance pass; preserve them.
132136 preserveKeys . add ( key ) ;
133137 continue ;
134138 }
@@ -161,6 +165,7 @@ async function runSessionRegistryMaintenance(params: {
161165 const beforeStore = loadSessionStore ( target . storePath , { skipCache : true } ) ;
162166 const beforeCount = Object . keys ( beforeStore ) . length ;
163167 if ( params . apply ) {
168+ // Apply mode mutates each store atomically through updateSessionStore.
164169 const applied = await updateSessionStore (
165170 target . storePath ,
166171 ( store ) => {
@@ -191,6 +196,7 @@ async function runSessionRegistryMaintenance(params: {
191196 continue ;
192197 }
193198 const previewStore = structuredClone ( beforeStore ) ;
199+ // Preview mode runs pruning against a clone so dry-run output cannot change stores.
194200 const { preserveKeys, preservedRunning } = buildSessionRegistryPreserveKeys ( {
195201 store : previewStore ,
196202 runningCronJobIds,
@@ -350,6 +356,7 @@ function toSystemAuditFindings(params: {
350356 severityFilter ?: TaskSystemAuditSeverity ;
351357 codeFilter ?: TaskSystemAuditCode ;
352358} ) {
359+ // Human audit reconciles inspectable tasks first so stale detached runs are reflected.
353360 const taskFindings = listTaskAuditFindings ( { tasks : reconcileInspectableTasks ( ) } ) ;
354361 const flowFindings = listTaskFlowAuditFindings ( ) ;
355362 return buildTaskSystemAuditFindings ( {
@@ -360,6 +367,7 @@ function toSystemAuditFindings(params: {
360367 } ) ;
361368}
362369
370+ /** Lists background tasks with optional runtime/status filters. */
363371export async function tasksListCommand (
364372 opts : { json ?: boolean ; runtime ?: string ; status ?: string } ,
365373 runtime : RuntimeEnv ,
@@ -412,6 +420,7 @@ export async function tasksListCommand(
412420 }
413421}
414422
423+ /** Shows one task record by id or lookup token. */
415424export async function tasksShowCommand (
416425 opts : { json ?: boolean ; lookup : string } ,
417426 runtime : RuntimeEnv ,
@@ -458,6 +467,7 @@ export async function tasksShowCommand(
458467 }
459468}
460469
470+ /** Updates a task's notification policy. */
461471export async function tasksNotifyCommand (
462472 opts : { lookup : string ; notify : TaskNotifyPolicy } ,
463473 runtime : RuntimeEnv ,
@@ -480,6 +490,7 @@ export async function tasksNotifyCommand(
480490 runtime . log ( `Updated ${ updated . taskId } notify policy to ${ updated . notifyPolicy } .` ) ;
481491}
482492
493+ /** Cancels a detached task run by lookup token. */
483494export async function tasksCancelCommand ( opts : { lookup : string } , runtime : RuntimeEnv ) {
484495 const task = reconcileTaskLookupToken ( opts . lookup ) ;
485496 if ( ! task ) {
@@ -507,6 +518,7 @@ export async function tasksCancelCommand(opts: { lookup: string }, runtime: Runt
507518 ) ;
508519}
509520
521+ /** Prints or serializes combined task/task-flow audit findings. */
510522export async function tasksAuditCommand (
511523 opts : {
512524 json ?: boolean ;
@@ -587,6 +599,7 @@ export async function tasksAuditCommand(
587599 }
588600}
589601
602+ /** Previews or applies task, task-flow, and backing session-registry maintenance. */
590603export async function tasksMaintenanceCommand (
591604 opts : { json ?: boolean ; apply ?: boolean } ,
592605 runtime : RuntimeEnv ,
0 commit comments