Skip to content

Commit bbe59dd

Browse files
authored
Merge pull request #468 from Extra-Chill/fix/cleanup-run-id
Fix cleanup run scheduling context
2 parents 30d9ff3 + f96bc59 commit bbe59dd

2 files changed

Lines changed: 130 additions & 1 deletion

File tree

inc/Abilities/WorkspaceAbilities.php

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1464,6 +1464,7 @@ private function registerAbilities(): void {
14641464
),
14651465
'user_id' => array( 'type' => 'integer' ),
14661466
'agent_id' => array( 'type' => 'integer' ),
1467+
'agent_slug' => array( 'type' => 'string' ),
14671468
),
14681469
),
14691470
'output_schema' => array(
@@ -2914,6 +2915,14 @@ public static function workspaceCleanupRun( array $input ): array|\WP_Error {
29142915
if ( isset($input['agent_id']) ) {
29152916
$context['agent_id'] = (int) $input['agent_id'];
29162917
}
2918+
if ( isset($input['agent_slug']) && '' !== trim( (string) $input['agent_slug']) ) {
2919+
$context['agent_slug'] = sanitize_key( (string) $input['agent_slug']);
2920+
} elseif ( empty($context['agent_id']) ) {
2921+
$agent_slug = self::resolveCleanupAgentSlug( (int) ( $context['user_id'] ?? 0 ) );
2922+
if ( '' !== $agent_slug ) {
2923+
$context['agent_slug'] = $agent_slug;
2924+
}
2925+
}
29172926

29182927
$batch_result = \DataMachine\Engine\Tasks\TaskScheduler::scheduleBatch(
29192928
$task_type,
@@ -2925,7 +2934,10 @@ public static function workspaceCleanupRun( array $input ): array|\WP_Error {
29252934
}
29262935

29272936
$job_ids = is_array($batch_result['job_ids'] ?? null) ? $batch_result['job_ids'] : array();
2928-
$job_id = (int) ( $job_ids[0] ?? 0 );
2937+
$job_id = (int) ( $job_ids[0] ?? ( $batch_result['batch_job_id'] ?? 0 ) );
2938+
if ( $job_id <= 0 ) {
2939+
return new \WP_Error('workspace_cleanup_schedule_empty', 'Workspace cleanup scheduling returned no job id. Check Data Machine logs for the rejected task reason.', array( 'status' => 500 ));
2940+
}
29292941

29302942
return array(
29312943
'success' => true,
@@ -2937,6 +2949,27 @@ public static function workspaceCleanupRun( array $input ): array|\WP_Error {
29372949
);
29382950
}
29392951

2952+
/**
2953+
* Resolve the active Data Machine agent slug for CLI-scheduled cleanup jobs.
2954+
*
2955+
* @param int $user_id Optional user id from the caller context.
2956+
* @return string
2957+
*/
2958+
private static function resolveCleanupAgentSlug( int $user_id = 0 ): string {
2959+
if ( ! class_exists('\\DataMachine\\Core\\FilesRepository\\DirectoryManager') ) {
2960+
return '';
2961+
}
2962+
2963+
try {
2964+
$manager = new \DataMachine\Core\FilesRepository\DirectoryManager();
2965+
$effective_user = $manager->get_effective_user_id($user_id);
2966+
$agent_slug = $manager->resolve_agent_slug(array( 'user_id' => $effective_user ));
2967+
return '' !== trim( (string) $agent_slug) ? sanitize_key( (string) $agent_slug) : '';
2968+
} catch ( \Throwable $e ) {
2969+
return '';
2970+
}
2971+
}
2972+
29402973
/**
29412974
* Remove a worktree.
29422975
*
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
<?php
2+
/**
3+
* Smoke test for workspace cleanup run scheduling context.
4+
*
5+
* php tests/smoke-workspace-cleanup-run-context.php
6+
*
7+
* @package DataMachineCode\Tests
8+
*/
9+
10+
declare( strict_types=1 );
11+
12+
namespace {
13+
if ( ! defined('ABSPATH') ) {
14+
define('ABSPATH', __DIR__);
15+
}
16+
17+
function sanitize_key( $key ): string {
18+
return strtolower(preg_replace('/[^a-z0-9_\-]/', '', (string) $key));
19+
}
20+
21+
class WP_Error {
22+
public function __construct(
23+
private string $code,
24+
private string $message,
25+
private array $data = array()
26+
) {}
27+
28+
public function get_error_code(): string {
29+
return $this->code;
30+
}
31+
32+
public function get_error_message(): string {
33+
return $this->message;
34+
}
35+
}
36+
}
37+
38+
namespace DataMachine\Core\FilesRepository {
39+
class DirectoryManager {
40+
public function get_effective_user_id( int $user_id ): int {
41+
return $user_id > 0 ? $user_id : 1;
42+
}
43+
44+
public function resolve_agent_slug( array $args ): string {
45+
return 1 === (int) ( $args['user_id'] ?? 0 ) ? 'intelligence-chubes4' : '';
46+
}
47+
}
48+
}
49+
50+
namespace DataMachine\Engine\Tasks {
51+
class TaskScheduler {
52+
public static array $last_context = array();
53+
public static array|false $next_result = array( 'job_ids' => array( 987 ), 'batch_job_id' => 0 );
54+
55+
public static function scheduleBatch( string $task_type, array $items, array $context = array() ): array|false { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.Found
56+
self::$last_context = $context;
57+
return self::$next_result;
58+
}
59+
}
60+
}
61+
62+
namespace {
63+
include_once dirname(__DIR__) . '/inc/Abilities/WorkspaceAbilities.php';
64+
65+
function datamachine_code_cleanup_run_context_assert( bool $condition, string $message ): void {
66+
if ( ! $condition ) {
67+
fwrite(STDERR, "Assertion failed: {$message}\n");
68+
exit(1);
69+
}
70+
}
71+
72+
$result = \DataMachineCode\Abilities\WorkspaceAbilities::workspaceCleanupRun(
73+
array(
74+
'mode' => 'artifacts',
75+
'source' => 'workspace_cleanup_cli',
76+
)
77+
);
78+
79+
datamachine_code_cleanup_run_context_assert(is_array($result), 'cleanup run returns success array');
80+
datamachine_code_cleanup_run_context_assert('cleanup-run-987' === ( $result['run_id'] ?? '' ), 'cleanup run uses direct job id');
81+
datamachine_code_cleanup_run_context_assert('intelligence-chubes4' === ( \DataMachine\Engine\Tasks\TaskScheduler::$last_context['agent_slug'] ?? '' ), 'cleanup run supplies resolved agent slug');
82+
83+
\DataMachine\Engine\Tasks\TaskScheduler::$next_result = array( 'job_ids' => array(), 'batch_job_id' => 0 );
84+
$result = \DataMachineCode\Abilities\WorkspaceAbilities::workspaceCleanupRun(
85+
array(
86+
'mode' => 'artifacts',
87+
'agent_slug' => 'explicit-agent',
88+
)
89+
);
90+
91+
datamachine_code_cleanup_run_context_assert($result instanceof \WP_Error, 'cleanup run without scheduled job returns an error');
92+
datamachine_code_cleanup_run_context_assert('workspace_cleanup_schedule_empty' === $result->get_error_code(), 'cleanup run reports empty schedule explicitly');
93+
datamachine_code_cleanup_run_context_assert('explicit-agent' === ( \DataMachine\Engine\Tasks\TaskScheduler::$last_context['agent_slug'] ?? '' ), 'explicit agent slug is sanitized and forwarded');
94+
95+
echo "Workspace cleanup run context smoke passed.\n";
96+
}

0 commit comments

Comments
 (0)