-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathWorkspaceHygieneReportTask.php
More file actions
138 lines (124 loc) · 4.26 KB
/
Copy pathWorkspaceHygieneReportTask.php
File metadata and controls
138 lines (124 loc) · 4.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
<?php
/**
* Workspace Hygiene Report System Task.
*
* Runs the non-destructive workspace hygiene report through Data Machine's
* recurring system-task surface. Disabled by default so installs opt into
* notification/reporting cadence explicitly.
*
* @package DataMachineCode\Tasks
*/
namespace DataMachineCode\Tasks;
use DataMachine\Core\PluginSettings;
use DataMachine\Engine\AI\System\Tasks\SystemTask;
use DataMachineCode\Workspace\Workspace;
defined('ABSPATH') || exit;
class WorkspaceHygieneReportTask extends SystemTask {
/**
* PluginSettings key that gates recurring/manual task execution.
*/
public const SETTING_KEY = 'workspace_hygiene_report_enabled';
/**
* Task type identifier.
*
* @return string
*/
public function getTaskType(): string {
return 'workspace_hygiene_report';
}
/**
* Pure workspace/disk maintenance — runs without agent ownership context.
*
* This task produces a non-destructive disk/worktree hygiene report via the
* Workspace service. It never acts as an agent or invokes an agent-scoped
* ability. It is registered as an agent-less weekly recurring schedule, so it
* must opt out of the SystemTask agent-context gate or
* TaskScheduler::schedule() rejects it before it runs.
*
* @return bool
*/
public function requiresAgentContext(): bool {
return false;
}
/**
* Task metadata for Data Machine system-task surfaces.
*
* @return array<string,mixed>
*/
public static function getTaskMeta(): array {
return array(
'label' => 'Workspace Hygiene Report',
'description' => 'Non-destructive report for workspace disk usage, free space, worktree counts, and local cleanup candidates. Runs weekly when enabled.',
'setting_key' => self::SETTING_KEY,
'default_enabled' => false,
'supports_run' => true,
);
}
/**
* Execute the hygiene report.
*
* Params are optional and mirror `Workspace::workspace_hygiene_report()`.
*
* @param int $jobId Job ID.
* @param array $params Task params.
* @return void
*/
public function executeTask( int $jobId, array $params ): void {
$enabled = (bool) PluginSettings::get(self::SETTING_KEY, false);
if ( ! $enabled ) {
$this->completeJob(
$jobId,
array(
'skipped' => true,
'reason' => sprintf('Workspace hygiene report disabled (PluginSettings: %s=false).', self::SETTING_KEY),
)
);
return;
}
$workspace = new Workspace();
$result = $workspace->workspace_hygiene_report(
array(
'include_cleanup' => array_key_exists('include_cleanup', $params) ? (bool) $params['include_cleanup'] : true,
'include_sizes' => array_key_exists('include_sizes', $params) ? (bool) $params['include_sizes'] : false,
'include_worktree_status' => array_key_exists('include_worktree_status', $params) ? (bool) $params['include_worktree_status'] : false,
'size_limit' => isset($params['size_limit']) ? (int) $params['size_limit'] : Workspace::HYGIENE_DEFAULT_SIZE_LIMIT,
)
);
if ( $result instanceof \WP_Error ) {
do_action(
'datamachine_log',
'error',
'Workspace hygiene report failed',
array(
'task' => $this->getTaskType(),
'jobId' => $jobId,
'error' => $result->get_error_message(),
'code' => $result->get_error_code(),
)
);
$this->failJob($jobId, $result->get_error_message());
return;
}
$worktrees = (array) ( $result['worktrees'] ?? array() );
$cleanup = (array) ( $result['cleanup']['summary'] ?? array() );
$fast_counts = (array) ( $result['fast_stats']['counts'] ?? array() );
$inventory_cleanup_candidates = (int) ( $cleanup['inventory_cleanup_candidate_count'] ?? $fast_counts['cleanup_eligible_unprobed_count'] ?? 0 );
do_action(
'datamachine_log',
'info',
sprintf(
'Workspace hygiene report: %s used, %s free, %d worktree(s), %d inventory cleanup candidate(s) pending fresh safety probes.',
$result['size']['total_human'] ?? 'unknown size',
$result['disk']['free_human'] ?? 'unknown disk',
(int) ( $worktrees['worktrees'] ?? 0 ),
$inventory_cleanup_candidates
),
array(
'task' => $this->getTaskType(),
'jobId' => $jobId,
'report' => $result,
)
);
$this->completeJob($jobId, $result);
}
}