Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 13 additions & 20 deletions plugins/data-machine/docs/core-system/routines-scheduling.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,18 @@ single-flight scheduling, and drift repair.

## Identity contract

| | Before (bespoke scheduler) | After (routines) |
|---|---|---|
| Hook | `datamachine_run_flow_now` | `wp_agent_routine_run_scheduled` |
| Args | `[flow_id, null, {generation}]` | `['routine_id' => 'flow-N']` |
| Group | `data-machine` | `agents-api` |
| Fencing | DM generation options | substrate generations (`agents_routine_generation_<id>`) |
| One-time | single action, same hook | single action under `datamachine_run_flow_once` (group `data-machine`) |
| Reconcile | `FlowScheduleReconciler` + lock | `Registry::reconcile()` via `FlowRoutines::reconcile()` |
| | Routines |
|---|---|
| Hook | `wp_agent_routine_run_scheduled` |
| Args | `['routine_id' => 'flow-N']` |
| Group | `agents-api` |
| Fencing | substrate generations (`agents_routine_generation_<id>`) |
| One-time | single action under `datamachine_run_flow_once` (group `data-machine`) |
| Reconcile | `Registry::reconcile()` via `FlowRoutines::reconcile()` |

`datamachine_run_flow_now` is not a scheduling hook: it carries one-off wakes
(`[flow_id]` / `[flow_id, job_id]`) from queue backpressure deferrals and
stuck-job recovery.

## How it works

Expand Down Expand Up @@ -45,20 +49,9 @@ single-flight scheduling, and drift repair.
actually registered this request, targeting `datamachine/run-flow` or
`datamachine/dispatch-system-task`.

## Migration

Boot does not cancel legacy generated actions (hook `datamachine_run_flow_now`,
args `[flow_id, null, {generation}]`). Use the explicit migration command to
inspect and cancel them:
## Reconcile

```bash
wp datamachine flows migrate-routines # dry run: prints the plan
wp datamachine flows migrate-routines --apply # cancel legacy chains now
wp datamachine flows reconcile-schedules # audit routine coverage
wp datamachine flows reconcile-schedules --apply # repair missing coverage
```

First runs after the migration shift by up to `min(interval, 3600)`s because
the substrate staggers the new chains deterministically per routine id.
`datamachine_run_flow_now` remains a live hook for queue backpressure deferral
ticks and is scheduled to retire one release after this migration.
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ private function scheduleOneTime( int $flow_id, int $timestamp ): array|\WP_Erro
array(
'hook' => FlowRoutines::ONE_TIME_HOOK,
'args' => array( 'flow_id' => $flow_id ),
'group' => FlowRoutines::LEGACY_GROUP,
'group' => FlowRoutines::ONE_TIME_GROUP,
'status' => 'pending',
'per_page' => 1,
),
Expand Down
75 changes: 0 additions & 75 deletions plugins/data-machine/inc/Cli/Commands/Flows/FlowsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -374,81 +374,6 @@ public function reconcile_schedules( array $args, array $assoc_args ): void {
}
}

/**
* Show (or run) the one-shot legacy schedule migration.
*
* ## OPTIONS
*
* [--apply]
* : Cancel the legacy pending actions. Without it this is a dry-run.
*
* [--dry-run]
* : Legacy flag. Like --apply, it runs the migration (kept for compatibility).
*
* [--format=<format>]
* : Output format: table or json.
* ---
* default: table
* ---
*
* ## EXAMPLES
*
* wp datamachine flows migrate-routines
* wp datamachine flows migrate-routines --apply
*
* @subcommand migrate-routines
*/
public function migrate_routines( array $args, array $assoc_args ): void {
$dry_run = ! isset( $assoc_args['dry-run'] ) && ! isset( $assoc_args['apply'] );

$plan = \DataMachine\Engine\Scheduling\FlowRoutines::migrate_legacy_schedules( $dry_run );

$format = (string) ( $assoc_args['format'] ?? 'table' );
if ( 'json' === $format ) {
WP_CLI::line( (string) wp_json_encode( $plan, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES ) );
if ( ! empty( $plan['errors'] ) ) {
WP_CLI::halt( 1 );
}
return;
}

WP_CLI::log( $dry_run ? 'Mode: dry-run' : 'Mode: apply' );
WP_CLI::log(
sprintf(
'Legacy pending actions: %d | Cancelled: %d | Errors: %d',
(int) ( $plan['legacy_actions'] ?? 0 ),
(int) ( $plan['cancelled'] ?? 0 ),
count( (array) ( $plan['errors'] ?? array() ) )
)
);

$flows = array_slice( (array) ( $plan['flows'] ?? array() ), 0, 50 );
if ( ! empty( $flows ) ) {
WP_CLI\Utils\format_items(
'table',
array_map(
static fn( $entry ) => array(
'flow_id' => (int) ( $entry['flow_id'] ?? 0 ),
'routine_id' => (string) ( $entry['routine_id'] ?? '' ),
'old_actions' => count( (array) ( $entry['legacy_action_ids'] ?? array() ) ),
'cancelled' => ! empty( $entry['cancelled'] ) ? 'yes' : ( $dry_run ? 'planned' : 'no' ),
),
$flows
),
array( 'flow_id', 'routine_id', 'old_actions', 'cancelled' )
);
if ( ! empty( $plan['details_truncated'] ) ) {
WP_CLI::warning( 'Detail output was truncated; use --format=json for the full plan.' );
}
}

if ( ! empty( $plan['errors'] ) ) {
WP_CLI::error( 'Migration completed with errors; rerun to retry.' );
}

WP_CLI::success( $dry_run ? 'Dry run complete. Rerun with --apply to cancel the legacy actions.' : 'Migration applied.' );
}

/**
* Diagnose or explicitly repair literal JSON solidus escapes in one flow.
*
Expand Down
3 changes: 1 addition & 2 deletions plugins/data-machine/inc/Core/Jobs/SchedulerEvidence.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
use DataMachine\Core\DirectJobEnqueuer;
use DataMachine\Engine\AI\AIConcurrencyBackpressure;
use DataMachine\Engine\AI\System\Tasks\SystemTask;
use DataMachine\Engine\Scheduling\FlowRoutines;
use DataMachine\Engine\Tasks\TaskScheduler;

defined( 'ABSPATH' ) || exit;
Expand Down Expand Up @@ -75,7 +74,7 @@ public static function hookJobArgs(): array {
AIConcurrencyBackpressure::RESUME_HOOK => array( 'arg' => 'job_id' ),
PipelineBatchScheduler::BATCH_HOOK => array( 'arg' => 'parent_job_id' ),
TaskScheduler::BATCH_HOOK => array( 'arg' => 'parent_job_id' ),
FlowRoutines::LEGACY_HOOK => array(
'datamachine_run_flow_now' => array(
'arg' => 'job_id',
'position' => 1,
),
Expand Down
40 changes: 5 additions & 35 deletions plugins/data-machine/inc/Engine/Actions/Engine.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,32 +145,6 @@ function datamachine_dispatch_run_flow( $flow_id, $job_id = null ): void {
}
}

/**
* Bridge for the legacy datamachine_run_flow_now hook.
*
* Positional wakes (`[ flow_id ]`, `[ flow_id, job_id ]`) from backpressure
* deferrals and stuck-job recovery still dispatch. A legacy recurring action
* carries the schedule generation marker at args[2]; after the routines
* migration those must not run, or they would double-fire beside the
* routine. TODO(3458): drop the legacy branch after one release.
*
* @param int $flow_id Flow ID.
* @param mixed $job_id Optional pre-created job ID.
* @param mixed $schedule_generation Legacy generation marker, if any.
*/
function datamachine_dispatch_scheduled_flow_wake( $flow_id, $job_id = null, $schedule_generation = null ): void {
if ( null !== $schedule_generation ) {
do_action(
'datamachine_log',
'warning',
'Legacy recurring schedule action ignored after routines migration',
array( 'flow_id' => (int) $flow_id )
);
return;
}
datamachine_dispatch_run_flow( $flow_id, $job_id );
}

/** Validate durable resume ownership before entering canonical step execution. */
function datamachine_resume_ai_step_action( $job_id, string $flow_step_id, $operation_generation = 0, $operation_claim_token = '', $ai_resume_generation = 0 ): void {
$job_id = (int) $job_id;
Expand Down Expand Up @@ -262,19 +236,15 @@ static function ( $code, $context = array() ): void {
/**
* Bridge: datamachine_run_flow_now → datamachine/run-flow ability.
*
* Still a live execution path for one-off wakes: queue backpressure
* deferrals and stuck-job recovery re-runs schedule this hook with
* positional args `[ flow_id ]` / `[ flow_id, job_id ]`. Recurring
* schedules moved to Agents API Routines; a legacy recurring action
* (identified by the generation marker at args[2]) that is still pending
* after migration is ignored and logged so it cannot double-fire beside
* its routine. TODO(3458): drop the legacy branch after one release.
* One-off wakes: queue backpressure deferrals and stuck-job recovery
* re-runs schedule this hook with positional args `[ flow_id ]` /
* `[ flow_id, job_id ]`.
*/
add_action(
'datamachine_run_flow_now',
'datamachine_dispatch_scheduled_flow_wake',
'datamachine_dispatch_run_flow',
10,
3
2
);

/**
Expand Down
Loading
Loading