@orgsdk/executor 0.1.0 → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +9 -7
- package/dist/cli.js +12 -13
- package/dist/client.js +79 -17
- package/dist/contract.js +39 -6
- package/dist/executor-configuration.js +1 -1
- package/dist/isolated-actions.js +4 -5
- package/dist/isolated-worker.js +5 -0
- package/dist/mcp/aliases.js +29 -0
- package/dist/mcp/client.js +239 -0
- package/dist/mcp/contract.js +57 -0
- package/dist/mcp/functions.js +40 -0
- package/dist/mcp/network.js +61 -0
- package/dist/mcp/schema-types.js +35 -0
- package/dist/mcp/transport.js +18 -0
- package/dist/project-data.js +1 -1
- package/dist/register-executor.js +23 -0
- package/dist/runner.js +30 -24
- package/dist/sdk-declarations.js +8 -1
- package/dist/session-job-contract.js +29 -0
- package/dist/session-job.js +256 -0
- package/dist/session-plugin-call.js +33 -0
- package/dist/sessions/manager.js +5 -3
- package/dist/sessions/process.js +3 -3
- package/dist/sessions/session-runtime.js +7 -3
- package/dist/sessions/worker-guard.js +20 -0
- package/dist/spawn-job.js +69 -0
- package/dist/supervisor.js +102 -146
- package/dist/worker.js +48 -45
- package/package.json +15 -10
- package/dist/generated/schema.d.ts +0 -6443
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
// src/session-plugin-call.ts
|
|
2
|
+
import { z } from "zod";
|
|
3
|
+
import { loadPlugin } from "./plugins.js";
|
|
4
|
+
import { mcpPlugin } from "./mcp/functions.js";
|
|
5
|
+
import { pluginSchema } from "./contract.js";
|
|
6
|
+
import { jsonValue } from "./sessions/values.js";
|
|
7
|
+
var pluginCallSchema = z.object({
|
|
8
|
+
type: z.literal("session_plugin_call"),
|
|
9
|
+
agentId: z.uuid(),
|
|
10
|
+
capability: z.string(),
|
|
11
|
+
plugin: pluginSchema,
|
|
12
|
+
name: z.string().regex(/^[a-z][a-zA-Z0-9]*$/),
|
|
13
|
+
args: z.object({}).catchall(z.json())
|
|
14
|
+
});
|
|
15
|
+
async function executePluginCall(request, client) {
|
|
16
|
+
const { plugin, agentId, name, args } = request;
|
|
17
|
+
const remote = plugin.distribution === "mcp" ? mcpPlugin(plugin, client, client.signal ?? new AbortController().signal) : void 0;
|
|
18
|
+
try {
|
|
19
|
+
const functions = remote?.functions ?? await loadPlugin(plugin, client, agentId);
|
|
20
|
+
const fn = functions.find((entry) => entry.name === name);
|
|
21
|
+
if (!fn || fn.calling !== "named") {
|
|
22
|
+
throw new Error("Plugin function is unavailable");
|
|
23
|
+
}
|
|
24
|
+
const result = jsonValue(await fn.handler(args));
|
|
25
|
+
process.stdout.write(JSON.stringify({ result }) + "\n");
|
|
26
|
+
} finally {
|
|
27
|
+
await remote?.runtime.close();
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
export {
|
|
31
|
+
executePluginCall,
|
|
32
|
+
pluginCallSchema
|
|
33
|
+
};
|
package/dist/sessions/manager.js
CHANGED
|
@@ -30,7 +30,7 @@ var SessionManager = class {
|
|
|
30
30
|
limits;
|
|
31
31
|
sessions = /* @__PURE__ */ new Map();
|
|
32
32
|
projects = /* @__PURE__ */ new Map();
|
|
33
|
-
runtime(history, directory, events) {
|
|
33
|
+
runtime(history, directory, events, functions) {
|
|
34
34
|
events ??= new SessionEvents(
|
|
35
35
|
history,
|
|
36
36
|
this.limits,
|
|
@@ -38,7 +38,7 @@ var SessionManager = class {
|
|
|
38
38
|
);
|
|
39
39
|
return new SessionRuntime(events, directory, {
|
|
40
40
|
journal: this.journal,
|
|
41
|
-
broker: this.broker,
|
|
41
|
+
broker: functions ? new FunctionBroker([...this.options.functions ?? [], ...functions]) : this.broker,
|
|
42
42
|
limits: this.limits,
|
|
43
43
|
options: this.options,
|
|
44
44
|
release: (session) => {
|
|
@@ -72,7 +72,9 @@ var SessionManager = class {
|
|
|
72
72
|
};
|
|
73
73
|
const session = this.runtime(
|
|
74
74
|
newHistory(scope, request.configurationRevision),
|
|
75
|
-
request.workspaceDirectory
|
|
75
|
+
request.workspaceDirectory,
|
|
76
|
+
void 0,
|
|
77
|
+
request.functions
|
|
76
78
|
);
|
|
77
79
|
this.reserve(session);
|
|
78
80
|
await session.start("session.opened");
|
package/dist/sessions/process.js
CHANGED
|
@@ -10,7 +10,7 @@ import { errorText, jsonValue } from "./values.js";
|
|
|
10
10
|
import { SessionError } from "./types.js";
|
|
11
11
|
function sessionCommand(options) {
|
|
12
12
|
if (options.mode === "host")
|
|
13
|
-
return workerCommand(import.meta.url, "./worker");
|
|
13
|
+
return workerCommand(import.meta.url, "./worker-guard");
|
|
14
14
|
for (const path of [
|
|
15
15
|
options.workspaceDirectory,
|
|
16
16
|
options.sourceDirectory
|
|
@@ -38,7 +38,7 @@ function sessionCommand(options) {
|
|
|
38
38
|
"--user",
|
|
39
39
|
`${process.getuid?.() || 1e3}:${process.getgid?.() || 1e3}`,
|
|
40
40
|
"--mount",
|
|
41
|
-
`type=bind,src=${options.workspaceDirectory},dst=/project/workspace`,
|
|
41
|
+
options.workspaceVolume ? `type=volume,src=${options.workspaceVolume.name},dst=/project/workspace,volume-subpath=${options.workspaceVolume.subpath}` : `type=bind,src=${options.workspaceDirectory},dst=/project/workspace`,
|
|
42
42
|
...options.sourceDirectory ? [
|
|
43
43
|
"--mount",
|
|
44
44
|
`type=bind,src=${options.sourceDirectory},dst=/executor/src,readonly`
|
|
@@ -49,7 +49,7 @@ function sessionCommand(options) {
|
|
|
49
49
|
"bun",
|
|
50
50
|
options.image ?? process.env.EXECUTOR_IMAGE ?? "orgsdk-job:dev",
|
|
51
51
|
"--no-env-file",
|
|
52
|
-
"/executor/src/sessions/worker.ts"
|
|
52
|
+
"/executor/src/sessions/worker-guard.ts"
|
|
53
53
|
];
|
|
54
54
|
}
|
|
55
55
|
var messageSchema = z.discriminatedUnion("type", [
|
|
@@ -47,7 +47,8 @@ var SessionRuntime = class {
|
|
|
47
47
|
mode: options.mode ?? "docker",
|
|
48
48
|
workspaceDirectory: this.workspaceDirectory,
|
|
49
49
|
image: options.image,
|
|
50
|
-
sourceDirectory: options.sourceDirectory
|
|
50
|
+
sourceDirectory: options.sourceDirectory,
|
|
51
|
+
workspaceVolume: options.workspaceVolume
|
|
51
52
|
},
|
|
52
53
|
this.workerHooks()
|
|
53
54
|
);
|
|
@@ -140,8 +141,11 @@ var SessionRuntime = class {
|
|
|
140
141
|
if (this.active) interruptCell(this.active.record, reason, this.events);
|
|
141
142
|
this.history.closedAt = (/* @__PURE__ */ new Date()).toISOString();
|
|
142
143
|
this.events.append(null, `session.${status}`, { reason });
|
|
143
|
-
|
|
144
|
-
|
|
144
|
+
try {
|
|
145
|
+
await this.save();
|
|
146
|
+
} finally {
|
|
147
|
+
this.services.release(this);
|
|
148
|
+
}
|
|
145
149
|
})();
|
|
146
150
|
return this.finishing;
|
|
147
151
|
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
// src/sessions/worker-guard.ts
|
|
2
|
+
import { spawnProcess, workerCommand } from "../process-runtime.js";
|
|
3
|
+
var worker = spawnProcess(workerCommand(import.meta.url, "./worker"), {
|
|
4
|
+
stdout: "pipe",
|
|
5
|
+
stderr: "ignore"
|
|
6
|
+
});
|
|
7
|
+
var stop = () => {
|
|
8
|
+
worker.kill("SIGKILL");
|
|
9
|
+
process.stdin.destroy();
|
|
10
|
+
};
|
|
11
|
+
process.stdin.on("data", (chunk) => worker.stdin.write(chunk));
|
|
12
|
+
process.stdin.once("end", stop);
|
|
13
|
+
process.stdin.once("error", stop);
|
|
14
|
+
process.stdout.once("error", stop);
|
|
15
|
+
worker.stdout.pipe(process.stdout, { end: false });
|
|
16
|
+
try {
|
|
17
|
+
process.exitCode = await worker.exited;
|
|
18
|
+
} finally {
|
|
19
|
+
process.stdin.destroy();
|
|
20
|
+
}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
// src/spawn-job.ts
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
import { z } from "zod";
|
|
4
|
+
import {
|
|
5
|
+
agentMountArguments,
|
|
6
|
+
developmentSourceArguments,
|
|
7
|
+
jobEnvironment
|
|
8
|
+
} from "./executor-configuration.js";
|
|
9
|
+
import {
|
|
10
|
+
spawnProcess,
|
|
11
|
+
stopHostProcess,
|
|
12
|
+
workerCommand
|
|
13
|
+
} from "./process-runtime.js";
|
|
14
|
+
function spawnJob(options, stdout = "ignore") {
|
|
15
|
+
const { base, agentId, directory, mode, name } = options;
|
|
16
|
+
const host = mode === "host";
|
|
17
|
+
const command = host ? workerCommand(import.meta.url, "./worker") : [
|
|
18
|
+
"docker",
|
|
19
|
+
"run",
|
|
20
|
+
"--add-host",
|
|
21
|
+
"host.docker.internal:host-gateway",
|
|
22
|
+
"--rm",
|
|
23
|
+
"-i",
|
|
24
|
+
"--name",
|
|
25
|
+
name,
|
|
26
|
+
"--network",
|
|
27
|
+
z.string().min(1).parse(process.env.EXECUTION_NETWORK),
|
|
28
|
+
"--read-only",
|
|
29
|
+
"--tmpfs",
|
|
30
|
+
"/tmp:rw,noexec,nosuid,size=128m",
|
|
31
|
+
"--cap-drop=ALL",
|
|
32
|
+
"--security-opt=no-new-privileges",
|
|
33
|
+
"--pids-limit=128",
|
|
34
|
+
"--memory=512m",
|
|
35
|
+
"--cpus=1",
|
|
36
|
+
"--user",
|
|
37
|
+
`${process.getuid?.() || 1e3}:${process.getgid?.() || 1e3}`,
|
|
38
|
+
...agentMountArguments(agentId, directory),
|
|
39
|
+
...developmentSourceArguments(),
|
|
40
|
+
"--workdir",
|
|
41
|
+
"/agent/workspace",
|
|
42
|
+
"-e",
|
|
43
|
+
`ORGSDK_SERVER_URL=${base}`,
|
|
44
|
+
"-e",
|
|
45
|
+
"EXECUTOR_AGENT_DIR=/agent",
|
|
46
|
+
z.string().min(1).parse(process.env.EXECUTOR_IMAGE)
|
|
47
|
+
];
|
|
48
|
+
const child = spawnProcess(command, {
|
|
49
|
+
cwd: join(directory, "workspace"),
|
|
50
|
+
detached: host && process.platform !== "win32",
|
|
51
|
+
env: jobEnvironment(base, directory, host),
|
|
52
|
+
stdout,
|
|
53
|
+
stderr: "ignore"
|
|
54
|
+
});
|
|
55
|
+
const kill = () => {
|
|
56
|
+
if (host) {
|
|
57
|
+
stopHostProcess(child);
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
void spawnProcess(["docker", "kill", name], {
|
|
61
|
+
stdout: "ignore",
|
|
62
|
+
stderr: "ignore"
|
|
63
|
+
}).exited.catch(() => void 0);
|
|
64
|
+
};
|
|
65
|
+
return { child, kill };
|
|
66
|
+
}
|
|
67
|
+
export {
|
|
68
|
+
spawnJob
|
|
69
|
+
};
|
package/dist/supervisor.js
CHANGED
|
@@ -4,24 +4,18 @@ import {
|
|
|
4
4
|
agentMountArguments,
|
|
5
5
|
jobEnvironment
|
|
6
6
|
} from "./executor-configuration.js";
|
|
7
|
-
import {
|
|
8
|
-
developmentSourceArguments as developmentSourceArguments2,
|
|
9
|
-
agentMountArguments as agentMountArguments2,
|
|
10
|
-
jobEnvironment as jobEnvironment2
|
|
11
|
-
} from "./executor-configuration.js";
|
|
12
|
-
import {
|
|
13
|
-
isEntrypoint,
|
|
14
|
-
spawnProcess,
|
|
15
|
-
stopHostProcess,
|
|
16
|
-
workerCommand
|
|
17
|
-
} from "./process-runtime.js";
|
|
7
|
+
import { isEntrypoint } from "./process-runtime.js";
|
|
18
8
|
import { setTimeout as sleep } from "node:timers/promises";
|
|
9
|
+
import { developmentSourceArguments as developmentSourceArguments2 } from "./executor-configuration.js";
|
|
10
|
+
import { SessionJobs } from "./session-job.js";
|
|
11
|
+
import { spawnJob } from "./spawn-job.js";
|
|
19
12
|
import { runIsolatedJob } from "./isolated-actions.js";
|
|
20
13
|
import { chown } from "node:fs/promises";
|
|
21
14
|
import { join } from "node:path";
|
|
22
15
|
import { z } from "zod";
|
|
23
16
|
import { Client } from "./client.js";
|
|
24
17
|
import { jobSchema, pluginSchema } from "./contract.js";
|
|
18
|
+
import { registerExecutor } from "./register-executor.js";
|
|
25
19
|
import {
|
|
26
20
|
agentDirectory,
|
|
27
21
|
installPlugin,
|
|
@@ -49,149 +43,113 @@ async function runAgent(base, registration, mode = "docker", signal) {
|
|
|
49
43
|
}
|
|
50
44
|
const client = new Client(base, registration.token, signal);
|
|
51
45
|
try {
|
|
52
|
-
await client
|
|
53
|
-
protocol_version: 3,
|
|
54
|
-
capabilities: ["action", "code", "validate", "test"],
|
|
55
|
-
agent_id: agentId
|
|
56
|
-
});
|
|
46
|
+
await registerExecutor(client, agentId);
|
|
57
47
|
} catch (error) {
|
|
58
48
|
if (signal.aborted || error instanceof Error && /API (401|403)/.test(error.message))
|
|
59
49
|
return;
|
|
60
50
|
throw error;
|
|
61
51
|
}
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
52
|
+
let sessions;
|
|
53
|
+
try {
|
|
54
|
+
while (!signal.aborted) {
|
|
55
|
+
try {
|
|
56
|
+
const config = z.object({
|
|
57
|
+
agent_id: z.string(),
|
|
58
|
+
plugins: z.array(pluginSchema)
|
|
59
|
+
}).parse(await client.configuration());
|
|
60
|
+
if (config.agent_id !== agentId)
|
|
61
|
+
throw new Error("Executor agent configuration mismatch");
|
|
62
|
+
for (const plugin of config.plugins) {
|
|
63
|
+
if (plugin.distribution === "mcp") continue;
|
|
64
|
+
try {
|
|
65
|
+
await installPlugin(plugin, agentId, void 0, signal);
|
|
66
|
+
} catch (error) {
|
|
67
|
+
console.error(
|
|
68
|
+
`Plugin ${plugin.slug} installation failed; claimed jobs will report the error: ${error instanceof Error ? error.message : "installation failed"}`
|
|
69
|
+
);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
const claimed = await client.claim();
|
|
73
|
+
if (!claimed) {
|
|
74
|
+
await sleep(2e3, void 0, { signal }).catch(() => void 0);
|
|
75
|
+
continue;
|
|
76
|
+
}
|
|
77
|
+
if (signal.aborted) return;
|
|
78
|
+
const job = jobSchema.parse(claimed);
|
|
79
|
+
if (job.snapshot.root_agent_id !== agentId)
|
|
80
|
+
throw new Error("Claimed job belongs to another agent");
|
|
81
|
+
if (["validate", "test"].includes(job.operation)) {
|
|
82
|
+
await runIsolatedJob(
|
|
83
|
+
job,
|
|
84
|
+
base,
|
|
85
|
+
developmentSourceArguments2(),
|
|
86
|
+
signal,
|
|
87
|
+
mode
|
|
73
88
|
);
|
|
89
|
+
continue;
|
|
74
90
|
}
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
await sleep(2e3, void 0, { signal }).catch(() => void 0);
|
|
82
|
-
continue;
|
|
83
|
-
}
|
|
84
|
-
if (signal.aborted) return;
|
|
85
|
-
const job = jobSchema.parse(claimed);
|
|
86
|
-
if (job.snapshot.root_agent_id !== agentId)
|
|
87
|
-
throw new Error("Claimed job belongs to another agent");
|
|
88
|
-
if (["validate", "test"].includes(job.operation)) {
|
|
89
|
-
await runIsolatedJob(
|
|
90
|
-
job,
|
|
91
|
+
if (job.operation === "session") {
|
|
92
|
+
sessions ??= new SessionJobs({ base, agentId, directory, mode });
|
|
93
|
+
await sessions.run(job, signal);
|
|
94
|
+
continue;
|
|
95
|
+
}
|
|
96
|
+
const { child, kill } = spawnJob({
|
|
91
97
|
base,
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
mode
|
|
98
|
+
agentId,
|
|
99
|
+
directory,
|
|
100
|
+
mode,
|
|
101
|
+
name: `orgsdk-job-${job.run_id}`
|
|
102
|
+
});
|
|
103
|
+
child.stdin.end(JSON.stringify(job));
|
|
104
|
+
signal.addEventListener("abort", kill, { once: true });
|
|
105
|
+
if (signal.aborted) kill();
|
|
106
|
+
const deadline = setTimeout(
|
|
107
|
+
kill,
|
|
108
|
+
Math.max(
|
|
109
|
+
0,
|
|
110
|
+
Math.min(36e5, Date.parse(job.deadline) - Date.now())
|
|
111
|
+
)
|
|
95
112
|
);
|
|
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
|
-
z.string().min(1).parse(process.env.EXECUTOR_IMAGE)
|
|
129
|
-
];
|
|
130
|
-
const child = spawnProcess(command, {
|
|
131
|
-
cwd: join(directory, "workspace"),
|
|
132
|
-
detached: mode === "host" && process.platform !== "win32",
|
|
133
|
-
env: jobEnvironment(base, directory, mode === "host"),
|
|
134
|
-
stdout: "ignore",
|
|
135
|
-
stderr: "ignore"
|
|
136
|
-
});
|
|
137
|
-
child.stdin.write(JSON.stringify(job));
|
|
138
|
-
child.stdin.end();
|
|
139
|
-
const kill = () => {
|
|
140
|
-
if (mode === "host") stopHostProcess(child);
|
|
141
|
-
else
|
|
142
|
-
void spawnProcess(["docker", "kill", name], {
|
|
143
|
-
stdout: "ignore",
|
|
144
|
-
stderr: "ignore"
|
|
145
|
-
}).exited;
|
|
146
|
-
};
|
|
147
|
-
signal.addEventListener("abort", kill, { once: true });
|
|
148
|
-
if (signal.aborted) kill();
|
|
149
|
-
const deadline = setTimeout(
|
|
150
|
-
kill,
|
|
151
|
-
Math.max(0, Math.min(36e5, Date.parse(job.deadline) - Date.now()))
|
|
152
|
-
);
|
|
153
|
-
const jobLifetime = new AbortController();
|
|
154
|
-
const control = new Client(
|
|
155
|
-
base,
|
|
156
|
-
job.capability,
|
|
157
|
-
AbortSignal.any([signal, jobLifetime.signal])
|
|
158
|
-
);
|
|
159
|
-
const monitor = setInterval(() => {
|
|
160
|
-
void (async () => {
|
|
161
|
-
try {
|
|
162
|
-
const state = z.object({ cancel_requested: z.boolean() }).parse(
|
|
163
|
-
await control.request(
|
|
164
|
-
`/internal/execution/v1/runs/${job.run_id}/heartbeat/`,
|
|
165
|
-
"POST"
|
|
166
|
-
)
|
|
167
|
-
);
|
|
168
|
-
if (!state.cancel_requested && !signal.aborted) return;
|
|
169
|
-
} catch {
|
|
170
|
-
}
|
|
171
|
-
if (!jobLifetime.signal.aborted) kill();
|
|
172
|
-
})();
|
|
173
|
-
}, 1e4);
|
|
174
|
-
try {
|
|
175
|
-
await child.exited;
|
|
176
|
-
} finally {
|
|
177
|
-
if (mode === "host") stopHostProcess(child);
|
|
178
|
-
jobLifetime.abort();
|
|
179
|
-
clearInterval(monitor);
|
|
180
|
-
clearTimeout(deadline);
|
|
181
|
-
signal.removeEventListener("abort", kill);
|
|
113
|
+
const jobLifetime = new AbortController();
|
|
114
|
+
const control = new Client(
|
|
115
|
+
base,
|
|
116
|
+
job.capability,
|
|
117
|
+
AbortSignal.any([signal, jobLifetime.signal])
|
|
118
|
+
);
|
|
119
|
+
const monitor = setInterval(() => {
|
|
120
|
+
void (async () => {
|
|
121
|
+
try {
|
|
122
|
+
const state = z.object({ cancel_requested: z.boolean() }).parse(await control.heartbeat(job.run_id));
|
|
123
|
+
if (!state.cancel_requested && !signal.aborted) return;
|
|
124
|
+
} catch {
|
|
125
|
+
}
|
|
126
|
+
if (!jobLifetime.signal.aborted) kill();
|
|
127
|
+
})();
|
|
128
|
+
}, 1e4);
|
|
129
|
+
try {
|
|
130
|
+
await child.exited;
|
|
131
|
+
} finally {
|
|
132
|
+
if (mode === "host") kill();
|
|
133
|
+
jobLifetime.abort();
|
|
134
|
+
clearInterval(monitor);
|
|
135
|
+
clearTimeout(deadline);
|
|
136
|
+
signal.removeEventListener("abort", kill);
|
|
137
|
+
}
|
|
138
|
+
} catch (error) {
|
|
139
|
+
if (signal.aborted || error instanceof Error && /API (401|403)/.test(error.message))
|
|
140
|
+
return;
|
|
141
|
+
console.error(
|
|
142
|
+
error instanceof Error ? error.message : "Executor failure"
|
|
143
|
+
);
|
|
144
|
+
await sleep(2e3, void 0, { signal }).catch(() => void 0);
|
|
182
145
|
}
|
|
183
|
-
} catch (error) {
|
|
184
|
-
if (signal.aborted || error instanceof Error && /API (401|403)/.test(error.message))
|
|
185
|
-
return;
|
|
186
|
-
console.error(
|
|
187
|
-
error instanceof Error ? error.message : "Executor failure"
|
|
188
|
-
);
|
|
189
|
-
await sleep(2e3, void 0, { signal }).catch(() => void 0);
|
|
190
146
|
}
|
|
147
|
+
} finally {
|
|
148
|
+
await sessions?.dispose();
|
|
191
149
|
}
|
|
192
150
|
}
|
|
193
151
|
async function manage(mode = "docker") {
|
|
194
|
-
const base = z.string().url().parse(process.env.
|
|
152
|
+
const base = z.string().url().parse(process.env.ORGSDK_SERVER_URL);
|
|
195
153
|
const bootstrap = new Client(
|
|
196
154
|
base,
|
|
197
155
|
z.string().min(32).parse(process.env.LOCAL_EXECUTOR_BOOTSTRAP_TOKEN),
|
|
@@ -200,9 +158,7 @@ async function manage(mode = "docker") {
|
|
|
200
158
|
const workers = /* @__PURE__ */ new Map();
|
|
201
159
|
while (!shutdown.signal.aborted) {
|
|
202
160
|
try {
|
|
203
|
-
const response = z.object({ registrations: z.array(registrationSchema) }).parse(
|
|
204
|
-
await bootstrap.request("/internal/execution/v1/bootstrap/", "POST")
|
|
205
|
-
);
|
|
161
|
+
const response = z.object({ registrations: z.array(registrationSchema) }).parse(await bootstrap.bootstrap());
|
|
206
162
|
const active = new Set(
|
|
207
163
|
response.registrations.map((item) => item.agent_id)
|
|
208
164
|
);
|
|
@@ -259,9 +215,9 @@ async function manage(mode = "docker") {
|
|
|
259
215
|
}
|
|
260
216
|
if (isEntrypoint(import.meta.url)) await manage();
|
|
261
217
|
export {
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
218
|
+
agentMountArguments,
|
|
219
|
+
developmentSourceArguments,
|
|
220
|
+
jobEnvironment,
|
|
265
221
|
manage,
|
|
266
222
|
registrationSchema,
|
|
267
223
|
runAgent
|
package/dist/worker.js
CHANGED
|
@@ -4,51 +4,54 @@ import { z } from "zod";
|
|
|
4
4
|
import { Client } from "./client.js";
|
|
5
5
|
import { jobSchema } from "./contract.js";
|
|
6
6
|
import { Runner } from "./runner.js";
|
|
7
|
-
|
|
8
|
-
var
|
|
9
|
-
var
|
|
10
|
-
var
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
7
|
+
import { executePluginCall, pluginCallSchema } from "./session-plugin-call.js";
|
|
8
|
+
var request = JSON.parse(await readStdin());
|
|
9
|
+
var base = z.string().url().parse(process.env.ORGSDK_SERVER_URL);
|
|
10
|
+
var call = pluginCallSchema.safeParse(request);
|
|
11
|
+
if (call.success) {
|
|
12
|
+
await executePluginCall(call.data, new Client(base, call.data.capability));
|
|
13
|
+
} else {
|
|
14
|
+
await runJob(jobSchema.parse(request));
|
|
15
|
+
}
|
|
16
|
+
async function runJob(job) {
|
|
17
|
+
const controller = new AbortController();
|
|
18
|
+
const client = new Client(base, job.capability, controller.signal);
|
|
19
|
+
const control = new Client(base, job.capability);
|
|
20
|
+
const heartbeat = z.object({
|
|
21
|
+
cancel_requested: z.boolean(),
|
|
22
|
+
status: z.string()
|
|
23
|
+
});
|
|
24
|
+
let stopped = false;
|
|
25
|
+
const timer = setInterval(() => {
|
|
26
|
+
void (async () => {
|
|
27
|
+
try {
|
|
28
|
+
const state = heartbeat.parse(await control.heartbeat(job.run_id));
|
|
29
|
+
if (state.cancel_requested || ["failed", "interrupted", "cancelled"].includes(state.status))
|
|
30
|
+
controller.abort();
|
|
31
|
+
} catch {
|
|
27
32
|
controller.abort();
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
() => controller.abort(),
|
|
35
|
-
Math.max(0, Date.parse(job.deadline) - Date.now())
|
|
36
|
-
);
|
|
37
|
-
try {
|
|
38
|
-
const output = await new Runner(job, client, controller.signal).run();
|
|
39
|
-
await control.request(
|
|
40
|
-
`/internal/execution/v1/runs/${job.run_id}/complete/`,
|
|
41
|
-
"POST",
|
|
42
|
-
{ status: "succeeded", output: { result: output } }
|
|
33
|
+
}
|
|
34
|
+
})();
|
|
35
|
+
}, 1e4);
|
|
36
|
+
const deadline = setTimeout(
|
|
37
|
+
() => controller.abort(),
|
|
38
|
+
Math.max(0, Date.parse(job.deadline) - Date.now())
|
|
43
39
|
);
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
40
|
+
try {
|
|
41
|
+
const output = await new Runner(job, client, controller.signal).run();
|
|
42
|
+
await control.complete(job.run_id, {
|
|
43
|
+
status: "succeeded",
|
|
44
|
+
output: { result: output }
|
|
45
|
+
});
|
|
46
|
+
stopped = true;
|
|
47
|
+
} catch (error) {
|
|
48
|
+
await control.complete(job.run_id, {
|
|
49
|
+
status: controller.signal.aborted ? "cancelled" : "failed",
|
|
50
|
+
error: error instanceof Error ? error.message : "Execution failed"
|
|
51
|
+
}).catch(() => void 0);
|
|
52
|
+
} finally {
|
|
53
|
+
clearInterval(timer);
|
|
54
|
+
clearTimeout(deadline);
|
|
55
|
+
}
|
|
56
|
+
if (!stopped) process.exitCode = 1;
|
|
53
57
|
}
|
|
54
|
-
if (!stopped) process.exitCode = 1;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
|
+
"version": "0.2.1",
|
|
2
3
|
"name": "@orgsdk/executor",
|
|
3
|
-
"version": "0.1.0",
|
|
4
4
|
"publishConfig": {
|
|
5
5
|
"access": "public",
|
|
6
6
|
"registry": "https://registry.npmjs.org/"
|
|
@@ -14,25 +14,30 @@
|
|
|
14
14
|
},
|
|
15
15
|
"type": "module",
|
|
16
16
|
"scripts": {
|
|
17
|
-
"test": "
|
|
17
|
+
"test": "npm run test:unit && npm run test:integration",
|
|
18
18
|
"typecheck": "tsc --noEmit",
|
|
19
|
-
"check": "
|
|
19
|
+
"check": "npm run typecheck && npm test",
|
|
20
20
|
"worker": "node dist/worker.js",
|
|
21
21
|
"supervisor": "node dist/supervisor.js",
|
|
22
|
-
"build": "
|
|
22
|
+
"build": "tsx scripts/build.ts",
|
|
23
23
|
"prepack": "npm run build",
|
|
24
24
|
"smoke:node": "node scripts/verify-runtime-package.mjs node",
|
|
25
|
-
"smoke:bun": "node scripts/verify-runtime-package.mjs bun"
|
|
25
|
+
"smoke:bun": "node scripts/verify-runtime-package.mjs bun",
|
|
26
|
+
"test:unit": "bun ../scripts/test.ts unit src",
|
|
27
|
+
"test:integration": "bun ../scripts/test.ts integration src"
|
|
26
28
|
},
|
|
27
29
|
"dependencies": {
|
|
28
|
-
"
|
|
30
|
+
"@modelcontextprotocol/client": "2.4.0",
|
|
31
|
+
"ajv": "8.20.0",
|
|
32
|
+
"esbuild": "0.28.2",
|
|
33
|
+
"pino": "^10.4.0",
|
|
29
34
|
"typescript": "~6.0.3",
|
|
30
|
-
"zod": "^4.0.0"
|
|
31
|
-
"esbuild": "0.28.2"
|
|
35
|
+
"zod": "^4.0.0"
|
|
32
36
|
},
|
|
33
37
|
"devDependencies": {
|
|
34
|
-
"@types/bun": "^1.
|
|
35
|
-
"@types/node": "^22.20.5"
|
|
38
|
+
"@types/bun": "^1.4.4",
|
|
39
|
+
"@types/node": "^22.20.5",
|
|
40
|
+
"tsx": "^4.23.15"
|
|
36
41
|
},
|
|
37
42
|
"bin": {
|
|
38
43
|
"orgsdk-executor": "bin/orgsdk-executor.mjs"
|