-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathexecute-native-agent-task-lifecycle.test.mjs
More file actions
193 lines (179 loc) · 8.22 KB
/
Copy pathexecute-native-agent-task-lifecycle.test.mjs
File metadata and controls
193 lines (179 loc) · 8.22 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import assert from "node:assert/strict"
import { chmod, mkdtemp, mkdir, readFile, writeFile } from "node:fs/promises"
import { tmpdir } from "node:os"
import { join, resolve } from "node:path"
import { spawn } from "node:child_process"
const root = resolve(".")
const executor = join(root, ".github/scripts/run-agent-task/execute-native-agent-task.mjs")
async function run(mode = "success") {
const temp = await mkdtemp(join(tmpdir(), "wp-codebox-native-lifecycle-"))
const workspace = join(temp, "workspace")
await mkdir(join(workspace, ".codebox"), { recursive: true })
await writeFile(join(workspace, "README.md"), "before\n")
const request = {
schema: "wp-codebox/agent-task-workflow-request/v1",
model: { provider: "openai", name: "gpt-5" },
external_package_source: {
repository: "owner/agents",
revision: "a".repeat(40),
path: "agent.agent.json",
digest: `sha256-bytes-v1:${"b".repeat(64)}`,
},
runtime_sources: [],
workload: { id: "run-1", label: "Update" },
target_repo: "owner/repo",
prompt: "Edit README using workspace_read and workspace_edit.",
writable_paths: mode === "deny" ? "src/**" : "README.md",
runner_workspace: {
enabled: true,
repo: "owner/repo",
base: "main",
branch_prefix: "wp-codebox/agent-task/",
},
validation_dependencies: "echo validation >> .codebox/order",
verification_commands: [
{ command: mode === "verify-fail" ? "false" : "echo verification >> .codebox/order" },
],
drift_checks: [{ command: "echo drift >> .codebox/order" }],
success: { requires_pr: mode !== "no-op-maintenance" },
access: {
caller_repo: "owner/repo",
allowed_repos: ["owner/repo"],
access_token_repos: ["owner/repo"],
},
limits: { max_turns: 1, time_budget_ms: 1000 },
artifacts: { expected: [], declarations: [] },
outputs: {
projections: {
pr_url: mode === "no-op-maintenance"
? { path: "metadata.runner_workspace_publication.pull_request.url", required: false }
: "metadata.runner_workspace_publication.pull_request.url",
},
},
callback_data: {},
run_agent: true,
dry_run: false,
}
await writeFile(join(workspace, ".codebox", "agent-task-request.json"), JSON.stringify(request))
const fakeCli = join(temp, "fake-cli.mjs")
const noOp = mode.startsWith("no-op")
await writeFile(fakeCli, `
import { mkdir, readFile, writeFile } from "node:fs/promises"
import { join } from "node:path"
const output = process.argv[process.argv.indexOf("--result-file") + 1]
const input = JSON.parse(await readFile(process.argv[process.argv.indexOf("--input-file") + 1], "utf8"))
const root = join(process.cwd(), ".codebox", "agent-task-artifacts", "files")
await mkdir(root, { recursive: true })
const patch = ${noOp} ? "" : "--- a/README.md\\n+++ b/README.md\\n@@ -1 +1 @@\\n-before\\n+after\\n"
await writeFile(join(root, "patch.diff"), patch)
await writeFile(join(root, "changed-files.json"), JSON.stringify({
schema: "wp-codebox/changed-files/v1",
files: ${noOp} ? [] : [{ path: "/workspace/README.md", relativePath: "README.md", status: "modified", beforeMode: "100644", afterMode: "100644" }],
}))
await writeFile(join(process.cwd(), ".codebox", "order"), "runtime\\n")
const summary = {
schema: "wp-codebox/agent-task-run-result/v1",
success: true,
status: "succeeded",
refs: {
patches: [{ kind: "codebox-patch", path: join(root, "patch.diff") }],
changed_files: [{ kind: "codebox-changed-files", path: join(root, "changed-files.json") }],
},
}
const result = {
schema: "wp-codebox/agent-task-run/v1",
success: true,
status: "succeeded",
agent_task_run_result: summary,
agent_result: {
artifacts: { directory: root },
changedFiles: { artifact: "changed-files.json", bytes: ${noOp} ? 0 : 1 },
patch: { artifact: "patch.diff", bytes: ${noOp} ? 0 : 1 },
},
metadata: { imported_agent: { slug: "fixture-agent" }, tool_contract: { tools: ["workspace_read", "workspace_edit"] } },
agent_runtime_diagnostics: { prepared_paths: { workspaces: input.task_input.workspaces.map((workspace) => ({ target: workspace.target, mode: workspace.mode, metadata: { baselineSource: "fixture-baseline", workspaceRoot: "/workspace", sourceMode: workspace.sourceMode } })) }, sandbox_workspace: { mounts: input.task_input.workspaces.map((workspace) => ({ target: workspace.target })) }, local_executor_root: "/workspace" },
}
await writeFile(output, JSON.stringify(result))
`)
const publisher = join(temp, "publisher.mjs")
const publisherOrderPath = join(workspace, ".codebox", "order")
await writeFile(publisher, `
import { appendFile } from "node:fs/promises"
export async function publishRunnerWorkspace({ testHook }) {
await appendFile(testHook.orderPath, "publish\\n")
return {
schema: "wp-codebox/runner-workspace-publication-result/v1",
success: true,
status: "published",
backend: "fixture",
pull_request: { url: "https://github.com/owner/repo/pull/1", reused: false, opened: true },
}
}
`)
const gh = join(temp, "gh")
await writeFile(gh, `#!/usr/bin/env node
process.stdout.write(JSON.stringify({
html_url: "https://github.com/owner/repo/pull/1",
base: { repo: { full_name: "owner/repo" } },
}))
`)
await chmod(gh, 0o755)
const result = await new Promise((done) => {
const child = spawn(process.execPath, [executor], {
cwd: workspace,
env: {
...process.env,
NODE_ENV: "test",
AGENT_TASK_REQUEST_PATH: join(workspace, ".codebox", "agent-task-request.json"),
AGENT_TASK_WORKSPACE: workspace,
WP_CODEBOX_WORKFLOW_ROOT: root,
WP_CODEBOX_CLI_PATH: fakeCli,
WP_CODEBOX_TEST_SKIP_MATERIALIZATION: "true",
WP_CODEBOX_TEST_PUBLISHER_MODULE: publisher,
WP_CODEBOX_TEST_PUBLISHER_HOOK: JSON.stringify({ orderPath: publisherOrderPath }),
GITHUB_TOKEN: "token",
EXPLICIT_ACCESS_TOKEN_CONFIGURED: "true",
EXTERNAL_PACKAGE_SOURCE_POLICY: JSON.stringify({
version: 1,
repositories: { "owner/agents": ["agent.agent.json"] },
}),
PATH: `${temp}:${process.env.PATH || ""}`,
},
})
let stderr = ""
child.stderr.on("data", (chunk) => { stderr += chunk })
child.on("close", (code) => done({ code, stderr }))
})
return {
...result,
workspace,
result: JSON.parse(await readFile(join(workspace, ".codebox", "agent-task-workflow-result.json"), "utf8")),
order: await readFile(join(workspace, ".codebox", "order"), "utf8").catch(() => ""),
}
}
const success = await run()
assert.equal(success.code, 0, success.stderr)
assert.equal(await readFile(join(success.workspace, "README.md"), "utf8"), "after\n")
assert.match(success.order, /runtime\nvalidation\nverification\ndrift\npublish\n/)
assert.equal(success.result.runtime_result.metadata.runner_workspace_publication.pull_request.url, "https://github.com/owner/repo/pull/1")
assert.equal(success.result.runtime_result.agent_runtime_diagnostics.prepared_paths.workspaces[0].target, "/workspace")
assert.equal(success.result.runtime_result.agent_runtime_diagnostics.prepared_paths.workspaces[0].mode, "readwrite")
assert.equal(success.result.runtime_result.agent_runtime_diagnostics.sandbox_workspace.mounts[0].target, "/workspace")
assert.equal(success.result.runtime_result.agent_runtime_diagnostics.local_executor_root, "/workspace")
const failedVerification = await run("verify-fail")
assert.equal(failedVerification.code, 1)
assert(!failedVerification.order.includes("publish"))
assert.equal(failedVerification.result.failure.stage, "verification")
assert.equal(failedVerification.result.runtime_result.success, true, "verification failures retain the normalized runtime result")
const denied = await run("deny")
assert.equal(denied.code, 1)
assert.equal(denied.order, "runtime\n")
const noOpRequired = await run("no-op")
assert.equal(noOpRequired.code, 1)
assert(!noOpRequired.order.includes("publish"))
assert.equal(noOpRequired.result.failure.stage, "no-op")
assert.equal(noOpRequired.result.runtime_result.success, true, "downstream failures retain the normalized runtime result")
const noOpMaintenance = await run("no-op-maintenance")
assert.equal(noOpMaintenance.code, 0)
assert(!noOpMaintenance.order.includes("publish"))
console.log("native agent task lifecycle ok")