-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsmoke-workspace-policy-tools.php
More file actions
130 lines (110 loc) · 3.73 KB
/
Copy pathsmoke-workspace-policy-tools.php
File metadata and controls
130 lines (110 loc) · 3.73 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
<?php
/**
* Smoke test for workspace tool pipeline policy exposure.
*
* Run: php tests/smoke-workspace-policy-tools.php
*/
declare( strict_types=1 );
namespace {
if (! defined('ABSPATH') ) {
define('ABSPATH', __DIR__ . '/');
}
$GLOBALS['dmc_workspace_policy_filters'] = array();
function add_filter( string $tag, callable $callback, int $priority = 10, int $accepted_args = 1 ): void
{
$GLOBALS['dmc_workspace_policy_filters'][ $tag ][ $priority ][] = $callback;
}
function apply_filters( string $tag, $value )
{
if (empty($GLOBALS['dmc_workspace_policy_filters'][ $tag ]) ) {
return $value;
}
ksort($GLOBALS['dmc_workspace_policy_filters'][ $tag ]);
foreach ( $GLOBALS['dmc_workspace_policy_filters'][ $tag ] as $callbacks ) {
foreach ( $callbacks as $callback ) {
$value = $callback($value);
}
}
return $value;
}
}
namespace DataMachine\Engine\AI\Tools {
class BaseTool
{
/**
* @param array<int,string> $contexts Context names. @param array<string,mixed> $options Tool options.
*/
protected function registerTool( string $tool_id, callable $definition_callback, array $contexts = array(), array $options = array() ): void
{
\add_filter(
'datamachine_tools',
static function ( array $tools ) use ( $tool_id, $definition_callback, $contexts, $options ): array {
$tools[ $tool_id ] = array(
'_callable' => $definition_callback,
'modes' => $contexts,
) + $options;
return $tools;
}
);
}
}
}
namespace {
include __DIR__ . '/../inc/Tools/WorkspaceTools.php';
$failures = array();
$passes = 0;
$assert = function ( string $label, bool $condition ) use ( &$failures, &$passes ): void {
if ($condition ) {
++$passes;
echo " ok {$label}\n";
return;
}
$failures[] = $label;
echo " fail {$label}\n";
};
echo "Workspace policy tools - smoke\n";
new \DataMachineCode\Tools\WorkspaceTools();
$tools = apply_filters('datamachine_tools', array());
$default_pipeline_tools = array(
'workspace_path',
'workspace_capabilities',
'workspace_list',
'workspace_show',
'workspace_ls',
'workspace_read',
'workspace_grep',
);
foreach ( $default_pipeline_tools as $tool ) {
$assert("{$tool} remains default pipeline-visible and sandbox-visible", array( 'chat', 'pipeline', 'sandbox' ) === ( $tools[ $tool ]['modes'] ?? null ));
}
$policy_tools = array(
'workspace_write',
'workspace_edit',
'workspace_apply_patch',
'workspace_delete',
'workspace_git_status',
'workspace_git_log',
'workspace_git_diff',
'workspace_git_pull',
'workspace_worktree_add',
'workspace_git_add',
'workspace_git_commit',
'workspace_git_push',
'workspace_git_rebase',
'workspace_git_reset',
'workspace_pr_status',
'workspace_pr_rebase',
);
foreach ( $policy_tools as $tool ) {
$assert("{$tool} is available in real chat, pipeline, and sandbox modes", array( 'chat', 'pipeline', 'sandbox' ) === ( $tools[ $tool ]['modes'] ?? null ));
$assert("{$tool} requires explicit opt-in for pipeline use", true === ( $tools[ $tool ]['requires_opt_in'] ?? null ));
}
if ($failures ) {
echo "\nFAIL: " . count($failures) . " assertion(s) failed\n";
foreach ( $failures as $failure ) {
echo " - {$failure}\n";
}
exit(1);
}
echo "\nOK ({$passes} assertions)\n";
}