pinokiod 8.3.13 → 8.3.15
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/kernel/agent_instructions.js +0 -1
- package/kernel/api/index.js +76 -12
- package/kernel/environment.js +1 -1
- package/kernel/index.js +15 -2
- package/kernel/plugin_sources.js +1 -1
- package/kernel/prototype.js +1 -1
- package/kernel/request_file.js +5 -14
- package/kernel/shell.js +12 -0
- package/kernel/shells.js +13 -1
- package/package.json +1 -1
- package/server/index.js +10 -5
- package/server/lib/agent_jobs.js +198 -0
- package/server/lib/agent_sessions.js +131 -0
- package/server/lib/brain.js +161 -91
- package/server/lib/content_validation.js +1 -1
- package/server/public/agent-bar-app.js +28 -6
- package/server/public/agent-bar-compose.css +675 -0
- package/server/public/agent-bar-compose.js +658 -0
- package/server/public/agent-bar-panel.css +2 -0
- package/server/public/agent-bar-panel.js +22 -7
- package/server/public/agent-bar-shell.css +38 -0
- package/server/public/agent-bar-shell.js +128 -34
- package/server/public/agent-bar.css +7 -1
- package/server/public/agent-bar.js +10 -1
- package/server/public/terminal_input_utils.js +4 -0
- package/server/views/app.ejs +5 -1
- package/server/views/partials/main_sidebar.ejs +2 -0
- package/system/plugin/antigravity-cli-auto/pinokio.js +44 -6
- package/system/plugin/claude/pinokio.js +43 -1
- package/system/plugin/claude-auto/pinokio.js +43 -1
- package/system/plugin/codex/pinokio.js +42 -1
- package/system/plugin/codex-auto/pinokio.js +42 -1
- package/system/plugin/grok/pinokio.js +1 -1
- package/system/plugin/grok-auto/pinokio.js +37 -10
- package/system/plugin/opencode/pinokio.js +24 -0
- package/system/plugin/opencode-auto/pinokio.js +82 -10
- package/test/agent-bar-compose.test.js +544 -0
- package/test/agent-bar-shell.test.js +149 -0
- package/test/agent-instructions.test.js +0 -2
- package/test/agent-jobs.test.js +534 -0
- package/test/agent-sessions.test.js +130 -0
- package/test/antigravity-cli-plugin.test.js +63 -18
- package/test/anywhere-api.test.js +1 -1
- package/test/anywhere-onboarding.test.js +1 -2
- package/test/brain.test.js +116 -69
- package/test/grok-plugin.test.js +2 -1
- package/test/opencode-plugin.test.js +7 -4
- package/test/plugin-action-functions.test.js +423 -0
- package/test/request-file.test.js +1 -12
- package/test/shell-log-path.test.js +64 -0
- package/test/shell-stop-while-starting.test.js +70 -0
- package/test/shells-live-error.test.js +66 -2
- package/test/terminal-mouse-input.test.js +12 -0
|
@@ -0,0 +1,534 @@
|
|
|
1
|
+
"use strict"
|
|
2
|
+
|
|
3
|
+
const test = require("node:test")
|
|
4
|
+
const assert = require("node:assert/strict")
|
|
5
|
+
const fs = require("fs")
|
|
6
|
+
const os = require("os")
|
|
7
|
+
const path = require("path")
|
|
8
|
+
const http = require("http")
|
|
9
|
+
const express = require("express")
|
|
10
|
+
const Sessions = require("../server/lib/agent_sessions")
|
|
11
|
+
const Brain = require("../server/lib/brain")
|
|
12
|
+
const { createJobs, plain, registerJobRoutes } = require("../server/lib/agent_jobs")
|
|
13
|
+
|
|
14
|
+
const withFolder = async (run) => {
|
|
15
|
+
const dir = fs.mkdtempSync(path.join(os.tmpdir(), "pinokio-jobs-"))
|
|
16
|
+
try {
|
|
17
|
+
await run(dir)
|
|
18
|
+
} finally {
|
|
19
|
+
fs.rmSync(dir, { recursive: true, force: true })
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
const tree = (dir) => fs.readdirSync(dir).sort().join(" ")
|
|
23
|
+
const read = (...parts) => fs.readFileSync(path.join(...parts), "utf8")
|
|
24
|
+
const json = (...parts) => JSON.parse(read(...parts))
|
|
25
|
+
const until = async (check, what) => {
|
|
26
|
+
for (let tries = 0; tries < 400; tries += 1) {
|
|
27
|
+
if (await check()) {
|
|
28
|
+
return
|
|
29
|
+
}
|
|
30
|
+
await new Promise((resolve) => setTimeout(resolve, 5))
|
|
31
|
+
}
|
|
32
|
+
assert.fail(`never happened: ${what}`)
|
|
33
|
+
}
|
|
34
|
+
const LINE = (file) => `My message is in ${file}. Read it and respond as if I had typed it here.`
|
|
35
|
+
|
|
36
|
+
// ---- the folder and the log ---------------------------------------------------
|
|
37
|
+
|
|
38
|
+
test("a job's folder is kept apart from sessions, in the brain and in an app", async () => {
|
|
39
|
+
await withFolder(async (home) => {
|
|
40
|
+
const brain = path.join(home, "brain")
|
|
41
|
+
const app = path.join(home, "api", "Speech")
|
|
42
|
+
fs.mkdirSync(brain)
|
|
43
|
+
fs.mkdirSync(app, { recursive: true })
|
|
44
|
+
|
|
45
|
+
const inBrain = await Sessions.create(brain, { id: "j1", type: "job", plugin: "/plugin/claude/pinokio.js", from: "Registry Client", label: "answer a post", words: "look at this" })
|
|
46
|
+
assert.equal(inBrain.dir, path.join(brain, "jobs", "j1"))
|
|
47
|
+
assert.equal(inBrain.prompt, LINE("jobs/j1/request.md"))
|
|
48
|
+
assert.deepEqual(json(inBrain.dir, "info.json"), { id: "j1", type: "job", plugin: "/plugin/claude/pinokio.js", cwd: brain, from: "Registry Client", label: "answer a post" })
|
|
49
|
+
assert.equal(read(inBrain.dir, "request.md"), `look at this\n\nOutput folder: ${path.join(inBrain.dir, "output")}\n`)
|
|
50
|
+
|
|
51
|
+
const inApp = await Sessions.create(app, { id: "j2", type: "job", plugin: "/plugin/codex/pinokio.js", app: "Speech", words: "say hi" })
|
|
52
|
+
assert.equal(inApp.dir, path.join(app, ".pinokio-jobs", "j2"))
|
|
53
|
+
assert.equal(inApp.prompt, LINE(".pinokio-jobs/j2/request.md"))
|
|
54
|
+
assert.equal(read(app, ".pinokio-jobs", ".gitignore"), "*\n", "it hides itself from the app's git")
|
|
55
|
+
assert.deepEqual(json(inApp.dir, "info.json"), { id: "j2", type: "job", plugin: "/plugin/codex/pinokio.js", cwd: app, app: "Speech" }, "no from or label when the starter gave none")
|
|
56
|
+
|
|
57
|
+
// a session next to them goes to its own folder and has neither value
|
|
58
|
+
const session = await Sessions.create(app, { id: "s1", plugin: "/plugin/codex/pinokio.js", app: "Speech", from: "x", label: "y", words: "hi" })
|
|
59
|
+
assert.equal(session.dir, path.join(app, ".pinokio-sessions", "s1"))
|
|
60
|
+
assert.deepEqual(Object.keys(json(session.dir, "info.json")), ["id", "type", "plugin", "cwd", "app"])
|
|
61
|
+
assert.equal(tree(app), ".pinokio-jobs .pinokio-sessions")
|
|
62
|
+
assert.equal(tree(brain), "jobs")
|
|
63
|
+
})
|
|
64
|
+
})
|
|
65
|
+
|
|
66
|
+
test("a job's log lines carry its label and who started it, never its request", async () => {
|
|
67
|
+
await withFolder(async (brain) => {
|
|
68
|
+
await Sessions.log(brain, { id: "j1", type: "job", plugin: "/plugin/claude/pinokio.js", from: "Registry\nClient", label: `answer a post ${"x".repeat(300)}`, words: "SECRET WORDS" })
|
|
69
|
+
await Sessions.log(brain, { id: "j2", type: "job", plugin: "/plugin/codex/pinokio.js", app: "Speech", from: "nightly", label: "check" })
|
|
70
|
+
await Sessions.log(brain, { id: "j3", type: "job", plugin: "/plugin/codex/pinokio.js", app: "Speech", words: "SECRET WORDS" })
|
|
71
|
+
await Sessions.logEnded(brain, "j1")
|
|
72
|
+
await Sessions.log(brain, { id: "s1", plugin: "/plugin/codex/pinokio.js", from: "ignored", label: "ignored", words: "the user's words" })
|
|
73
|
+
const lines = read(brain, "log.md").split("\n")
|
|
74
|
+
assert.ok(lines[0].startsWith("- j1 | job | started | /plugin/claude/pinokio.js, from Registry Client | answer a post xxx"))
|
|
75
|
+
assert.ok(lines[0].endsWith("…"))
|
|
76
|
+
assert.equal(lines[1], "- j2 | job | started | /plugin/codex/pinokio.js in Speech, from nightly | check")
|
|
77
|
+
assert.equal(lines[2], "- j3 | job | started | /plugin/codex/pinokio.js in Speech |", "no label: the column is empty")
|
|
78
|
+
assert.equal(lines[3], "- j1 | job | ended")
|
|
79
|
+
assert.equal(lines[4], "- s1 | run | started | /plugin/codex/pinokio.js | the user's words")
|
|
80
|
+
assert.equal(read(brain, "log.md").includes("SECRET"), false)
|
|
81
|
+
})
|
|
82
|
+
})
|
|
83
|
+
|
|
84
|
+
// ---- what a job printed -------------------------------------------------------
|
|
85
|
+
|
|
86
|
+
test("what a terminal printed is kept as plain text", () => {
|
|
87
|
+
const printed = "\u001B[1mbold\u001B[0m plain\r\nnext \u001B]0;a title\u0007line\r\n\u001B[38;5;196mred\u001B[0m\rdone \u001B]8;;http://x\u001B\\link\u001B]8;;\u001B\\\n"
|
|
88
|
+
assert.equal(plain(printed), "bold plain\nnext line\nred\ndone link\n")
|
|
89
|
+
assert.equal(plain(""), "")
|
|
90
|
+
})
|
|
91
|
+
|
|
92
|
+
// ---- running jobs -------------------------------------------------------------
|
|
93
|
+
|
|
94
|
+
// the kernel as the runner sees it: it starts a run, tells what the run prints,
|
|
95
|
+
// lets go of a job once (endJob), and then says `disconnect`
|
|
96
|
+
const fakeKernel = () => {
|
|
97
|
+
const listeners = {}
|
|
98
|
+
const started = [] // the requests
|
|
99
|
+
const released = [] // the jobs the kernel let go of
|
|
100
|
+
const emit = (packet) => Object.values(listeners).forEach((listener) => listener(packet))
|
|
101
|
+
const kernel = {
|
|
102
|
+
api: {
|
|
103
|
+
jobs: new Map(),
|
|
104
|
+
listen: (name, listener) => { listeners[name] = listener },
|
|
105
|
+
process: async (request) => { started.push(request) },
|
|
106
|
+
endJob: (id) => {
|
|
107
|
+
if (!kernel.api.jobs.delete(id)) {
|
|
108
|
+
return false
|
|
109
|
+
}
|
|
110
|
+
released.push(id)
|
|
111
|
+
emit({ id, type: "disconnect" })
|
|
112
|
+
return true
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
// the job's steps are over: the kernel ends it by itself
|
|
117
|
+
const over = (id) => kernel.api.endJob(id)
|
|
118
|
+
return { kernel, listeners, started, released, emit, over }
|
|
119
|
+
}
|
|
120
|
+
const newJob = (home, id) => {
|
|
121
|
+
const dir = path.join(home, "jobs", id)
|
|
122
|
+
fs.mkdirSync(dir, { recursive: true })
|
|
123
|
+
return { id, script: "/plugins/claude/pinokio.js", cwd: home, dir, prompt: LINE(`jobs/${id}/request.md`) }
|
|
124
|
+
}
|
|
125
|
+
const quietly = async (run) => {
|
|
126
|
+
const warn = console.warn
|
|
127
|
+
const said = []
|
|
128
|
+
console.warn = (...args) => said.push(args.join(" "))
|
|
129
|
+
try {
|
|
130
|
+
await run()
|
|
131
|
+
} finally {
|
|
132
|
+
console.warn = warn
|
|
133
|
+
}
|
|
134
|
+
return said
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
test("a job runs the plugin's job with the folder and the request line, and keeps what it printed", async () => {
|
|
138
|
+
await withFolder(async (home) => {
|
|
139
|
+
const t = fakeKernel()
|
|
140
|
+
const ended = []
|
|
141
|
+
const jobs = createJobs({ kernel: t.kernel, ended: async (id) => ended.push(id) })
|
|
142
|
+
assert.equal(jobs.state("a"), null)
|
|
143
|
+
jobs.add(newJob(home, "a"))
|
|
144
|
+
assert.equal(jobs.state("a"), "running")
|
|
145
|
+
assert.deepEqual(t.started[0], {
|
|
146
|
+
id: "a",
|
|
147
|
+
uri: "/plugins/claude/pinokio.js",
|
|
148
|
+
path: "/plugins/claude/pinokio.js",
|
|
149
|
+
cwd: home,
|
|
150
|
+
action: "job",
|
|
151
|
+
input: { cwd: home, prompt: LINE("jobs/a/request.md") }
|
|
152
|
+
}, "the plugin receives the folder and the prompt, nothing else")
|
|
153
|
+
assert.equal(t.kernel.api.jobs.get("a"), t.started[0], "the kernel knows this request as a job")
|
|
154
|
+
|
|
155
|
+
t.emit({ id: "a", type: "stream", data: { raw: "\u001B[32mworking\u001B[0m\r\n" } })
|
|
156
|
+
t.emit({ id: "other", type: "stream", data: { raw: "someone else's output\r\n" } })
|
|
157
|
+
t.emit({ id: "a", type: "stream", data: { raw: "the answer is 4\r\n" } })
|
|
158
|
+
t.emit({ id: "a", type: "result", data: { raw: "not printed" } })
|
|
159
|
+
assert.equal(ended.length, 0)
|
|
160
|
+
t.over("a")
|
|
161
|
+
assert.equal(jobs.state("a"), "running", "not ended before what it printed is written")
|
|
162
|
+
await until(() => jobs.state("a") === "ended", "the job ends")
|
|
163
|
+
assert.deepEqual(ended, ["a"])
|
|
164
|
+
assert.equal(read(home, "jobs", "a", "terminal.txt"), "working\nthe answer is 4\n")
|
|
165
|
+
assert.deepEqual(t.released, ["a"], "the kernel let go of it once")
|
|
166
|
+
assert.equal(t.kernel.api.jobs.has("a"), false)
|
|
167
|
+
// nothing more is kept or said for a job that has ended
|
|
168
|
+
t.emit({ id: "a", type: "stream", data: { raw: "late\r\n" } })
|
|
169
|
+
t.emit({ id: "a", type: "disconnect" })
|
|
170
|
+
t.emit({ id: "a", type: "error", data: "late" })
|
|
171
|
+
await new Promise((resolve) => setTimeout(resolve, 20))
|
|
172
|
+
assert.deepEqual(ended, ["a"])
|
|
173
|
+
assert.deepEqual(t.released, ["a"])
|
|
174
|
+
assert.equal(read(home, "jobs", "a", "terminal.txt"), "working\nthe answer is 4\n")
|
|
175
|
+
assert.deepEqual(Object.keys(t.listeners), ["jobs"], "one listener for every job")
|
|
176
|
+
})
|
|
177
|
+
})
|
|
178
|
+
|
|
179
|
+
test("any number of jobs run at once, each until it ends or is stopped", async () => {
|
|
180
|
+
await withFolder(async (home) => {
|
|
181
|
+
const t = fakeKernel()
|
|
182
|
+
const ended = []
|
|
183
|
+
const jobs = createJobs({ kernel: t.kernel, ended: async (id) => ended.push(id) })
|
|
184
|
+
const ids = ["a", "b", "c", "d", "e", "f"]
|
|
185
|
+
for (const id of ids) {
|
|
186
|
+
jobs.add(newJob(home, id))
|
|
187
|
+
}
|
|
188
|
+
assert.deepEqual(ids.map(jobs.state), ids.map(() => "running"), "none waits for another")
|
|
189
|
+
assert.deepEqual(t.started.map((request) => request.id), ids)
|
|
190
|
+
assert.deepEqual(Object.keys(t.listeners), ["jobs"])
|
|
191
|
+
|
|
192
|
+
// one ends by itself: the others go on
|
|
193
|
+
t.over("b")
|
|
194
|
+
await until(() => jobs.state("b") === "ended", "b ends")
|
|
195
|
+
assert.deepEqual(ids.map(jobs.state), ["running", "ended", "running", "running", "running", "running"])
|
|
196
|
+
|
|
197
|
+
// one is stopped: the answer comes once it has ended, and only it is stopped
|
|
198
|
+
t.emit({ id: "a", type: "stream", data: { raw: "half done" } })
|
|
199
|
+
assert.equal(await jobs.stop("a"), "ended")
|
|
200
|
+
assert.deepEqual(t.released, ["b", "a"])
|
|
201
|
+
assert.deepEqual(ended, ["b", "a"])
|
|
202
|
+
assert.equal(read(home, "jobs", "a", "terminal.txt"), "half done")
|
|
203
|
+
assert.deepEqual(ids.map(jobs.state), ["ended", "ended", "running", "running", "running", "running"])
|
|
204
|
+
|
|
205
|
+
// ended or unknown: nothing happens
|
|
206
|
+
assert.equal(await jobs.stop("a"), "ended")
|
|
207
|
+
assert.equal(await jobs.stop("nobody"), null)
|
|
208
|
+
assert.deepEqual(t.released, ["b", "a"])
|
|
209
|
+
assert.deepEqual(ended, ["b", "a"])
|
|
210
|
+
|
|
211
|
+
// nothing ends a job that is still working
|
|
212
|
+
await new Promise((resolve) => setTimeout(resolve, 60))
|
|
213
|
+
assert.deepEqual(["c", "d", "e", "f"].map(jobs.state), ["running", "running", "running", "running"])
|
|
214
|
+
})
|
|
215
|
+
})
|
|
216
|
+
|
|
217
|
+
test("a job ends once, and everything that ends it gets the same answer", async () => {
|
|
218
|
+
await withFolder(async (home) => {
|
|
219
|
+
const t = fakeKernel()
|
|
220
|
+
const ended = []
|
|
221
|
+
let logged
|
|
222
|
+
const log = new Promise((resolve) => { logged = resolve })
|
|
223
|
+
// the `ended` line takes a while to write
|
|
224
|
+
const jobs = createJobs({ kernel: t.kernel, ended: async (id) => { ended.push(id); await log } })
|
|
225
|
+
|
|
226
|
+
// three stops at once, and the state asked meanwhile
|
|
227
|
+
jobs.add(newJob(home, "a"))
|
|
228
|
+
const stops = [jobs.stop("a"), jobs.stop("a"), jobs.stop("a")]
|
|
229
|
+
await new Promise((resolve) => setTimeout(resolve, 30))
|
|
230
|
+
logged()
|
|
231
|
+
assert.deepEqual(await Promise.all(stops), ["ended", "ended", "ended"])
|
|
232
|
+
assert.deepEqual(ended, ["a"])
|
|
233
|
+
assert.deepEqual(t.released, ["a"])
|
|
234
|
+
|
|
235
|
+
// a stop that arrives while the job is ending by itself, and an error on top
|
|
236
|
+
jobs.add(newJob(home, "b"))
|
|
237
|
+
t.over("b")
|
|
238
|
+
t.emit({ id: "b", type: "error", data: "said after the end" })
|
|
239
|
+
assert.equal(await jobs.stop("b"), "ended")
|
|
240
|
+
assert.deepEqual(ended, ["a", "b"])
|
|
241
|
+
assert.deepEqual(t.released, ["a", "b"])
|
|
242
|
+
assert.equal(read(home, "jobs", "b", "terminal.txt"), "", "nothing is added once a job is ending")
|
|
243
|
+
|
|
244
|
+
// a start that fails after the job was stopped adds nothing to what it printed
|
|
245
|
+
let fail
|
|
246
|
+
const { process: start } = t.kernel.api
|
|
247
|
+
t.kernel.api.process = () => new Promise((resolve, reject) => { fail = reject })
|
|
248
|
+
jobs.add(newJob(home, "late"))
|
|
249
|
+
assert.equal(await jobs.stop("late"), "ended")
|
|
250
|
+
fail(new Error("the plugin's file could not be read"))
|
|
251
|
+
await new Promise((resolve) => setTimeout(resolve, 30))
|
|
252
|
+
assert.equal(read(home, "jobs", "late", "terminal.txt"), "")
|
|
253
|
+
t.kernel.api.process = start
|
|
254
|
+
ended.pop()
|
|
255
|
+
t.released.pop()
|
|
256
|
+
|
|
257
|
+
// the kernel cannot let go of it cleanly: the job has still ended
|
|
258
|
+
jobs.add(newJob(home, "c"))
|
|
259
|
+
t.kernel.api.endJob = () => { throw new Error("a shell would not die") }
|
|
260
|
+
const said = await quietly(async () => {
|
|
261
|
+
assert.equal(await jobs.stop("c"), "ended")
|
|
262
|
+
})
|
|
263
|
+
assert.equal(said.filter((line) => line.includes("a shell would not die")).length, 1)
|
|
264
|
+
assert.deepEqual(ended, ["a", "b", "c"])
|
|
265
|
+
})
|
|
266
|
+
})
|
|
267
|
+
|
|
268
|
+
test("a job that fails or cannot start has ended, with the reason in terminal.txt, and the kernel holds nothing of it", async () => {
|
|
269
|
+
await withFolder(async (home) => {
|
|
270
|
+
const t = fakeKernel()
|
|
271
|
+
const ended = []
|
|
272
|
+
const said = await quietly(async () => {
|
|
273
|
+
// the log line cannot be written: the job has still ended
|
|
274
|
+
const jobs = createJobs({ kernel: t.kernel, ended: async (id) => { ended.push(id); throw new Error("log is not writable") } })
|
|
275
|
+
jobs.add(newJob(home, "a"))
|
|
276
|
+
t.emit({ id: "a", type: "stream", data: { raw: "starting\r\n" } })
|
|
277
|
+
t.emit({ id: "a", type: "error", data: "missing or invalid attribute: job" })
|
|
278
|
+
await until(() => jobs.state("a") === "ended", "an error ends it")
|
|
279
|
+
assert.equal(read(home, "jobs", "a", "terminal.txt"), "starting\n\nmissing or invalid attribute: job\n")
|
|
280
|
+
assert.deepEqual(t.released, ["a"], "what it had started is let go of")
|
|
281
|
+
|
|
282
|
+
// the plugin cannot be started at all
|
|
283
|
+
t.kernel.api.process = async () => { throw new Error("no such script") }
|
|
284
|
+
jobs.add(newJob(home, "b"))
|
|
285
|
+
await until(() => jobs.state("b") === "ended", "a failed start ends it")
|
|
286
|
+
assert.equal(read(home, "jobs", "b", "terminal.txt"), "\nno such script\n")
|
|
287
|
+
assert.deepEqual(t.released, ["a", "b"], "the kernel does not go on holding a job that never started")
|
|
288
|
+
assert.equal(t.kernel.api.jobs.size, 0)
|
|
289
|
+
assert.deepEqual(ended, ["a", "b"])
|
|
290
|
+
})
|
|
291
|
+
assert.equal(said.filter((line) => line.includes("log is not writable")).length, 2)
|
|
292
|
+
})
|
|
293
|
+
})
|
|
294
|
+
|
|
295
|
+
test("when what a job prints cannot be kept, that is said once and the job still ends", async () => {
|
|
296
|
+
await withFolder(async (home) => {
|
|
297
|
+
const t = fakeKernel()
|
|
298
|
+
const jobs = createJobs({ kernel: t.kernel, ended: async () => {} })
|
|
299
|
+
const job = newJob(home, "a")
|
|
300
|
+
fs.rmSync(job.dir, { recursive: true }) // its folder is gone: terminal.txt cannot be written
|
|
301
|
+
const said = await quietly(async () => {
|
|
302
|
+
jobs.add(job)
|
|
303
|
+
t.emit({ id: "a", type: "stream", data: { raw: "one\r\n" } })
|
|
304
|
+
t.emit({ id: "a", type: "stream", data: { raw: "two\r\n" } })
|
|
305
|
+
t.over("a")
|
|
306
|
+
await until(() => jobs.state("a") === "ended", "the job ends")
|
|
307
|
+
})
|
|
308
|
+
assert.equal(said.length, 1)
|
|
309
|
+
assert.match(said[0], /job a: what it prints cannot be kept/)
|
|
310
|
+
})
|
|
311
|
+
})
|
|
312
|
+
|
|
313
|
+
// ---- preparing a job ------------------------------------------------------------
|
|
314
|
+
|
|
315
|
+
const PluginSources = require("../kernel/plugin_sources")
|
|
316
|
+
const WITH_JOB = `{ run: [{ method: "shell.run" }], job: [{ method: "shell.run" }] }`
|
|
317
|
+
const newHome = () => {
|
|
318
|
+
const root = fs.mkdtempSync(path.join(os.tmpdir(), "pinokio-jobs-home-"))
|
|
319
|
+
const home = path.join(root, "home")
|
|
320
|
+
const own = path.join(root, "pinokio", "system") // what comes with Pinokio, outside the home
|
|
321
|
+
const plugin = (folder, body = WITH_JOB) => {
|
|
322
|
+
fs.mkdirSync(folder, { recursive: true })
|
|
323
|
+
fs.writeFileSync(path.join(folder, "pinokio.js"), `module.exports = ${body}\n`)
|
|
324
|
+
}
|
|
325
|
+
plugin(path.join(home, "plugin", "agent"))
|
|
326
|
+
plugin(path.join(home, "plugin", "jobless"), `{ run: [{ method: "shell.run" }] }`)
|
|
327
|
+
plugin(path.join(home, "plugin", "empty"), `{ run: [{ method: "shell.run" }], job: [] }`)
|
|
328
|
+
plugin(path.join(home, "plugin", "both"))
|
|
329
|
+
plugin(path.join(home, "api", "Speech", "tools", "helper"))
|
|
330
|
+
plugin(path.join(own, "plugin", "builtin"))
|
|
331
|
+
plugin(path.join(own, "plugin", "both"))
|
|
332
|
+
const kernel = {
|
|
333
|
+
homedir: home,
|
|
334
|
+
path: (...parts) => path.resolve(home, ...parts),
|
|
335
|
+
systemPath: (...parts) => path.resolve(own, ...parts),
|
|
336
|
+
api: {
|
|
337
|
+
resolveScript: async (file) => {
|
|
338
|
+
await fs.promises.stat(file)
|
|
339
|
+
return { cwd: path.dirname(file), script: require(file) }
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
const brain = Brain.createBrain({ kernel, copyUploads: async () => [], pluginHref: PluginSources.resolveLauncherPluginHref })
|
|
344
|
+
return { root, home, own, brain, dir: path.join(home, "brain"), api: path.join(home, "api") }
|
|
345
|
+
}
|
|
346
|
+
const withHome = async (run) => {
|
|
347
|
+
const t = newHome()
|
|
348
|
+
try {
|
|
349
|
+
await run(t)
|
|
350
|
+
} finally {
|
|
351
|
+
fs.rmSync(t.root, { recursive: true, force: true })
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
const BODY = { plugin: "plugin/agent", cwd: "", prompt: "look at this", from: "", label: "" }
|
|
355
|
+
|
|
356
|
+
test("a job that starts in the brain has its folder, the instruction files and a log line", async () => {
|
|
357
|
+
await withHome(async (t) => {
|
|
358
|
+
const job = await t.brain.prepareJob({ ...BODY, from: "Registry Client", label: "answer a post" })
|
|
359
|
+
assert.match(job.id, /^\d{8}-\d{6}-[a-f0-9]{8}$/)
|
|
360
|
+
const dir = path.join(t.dir, "jobs", job.id)
|
|
361
|
+
assert.deepEqual(job, { id: job.id, script: path.join(t.home, "plugin", "agent", "pinokio.js"), cwd: t.dir, dir, prompt: LINE(`jobs/${job.id}/request.md`) })
|
|
362
|
+
assert.equal(tree(dir), "info.json request.md", "a job has no attached files")
|
|
363
|
+
assert.deepEqual(json(dir, "info.json"), { id: job.id, type: "job", plugin: "/plugin/agent/pinokio.js", cwd: t.dir, from: "Registry Client", label: "answer a post" })
|
|
364
|
+
assert.equal(read(dir, "request.md"), `look at this\n\nOutput folder: ${path.join(dir, "output")}\n`)
|
|
365
|
+
assert.equal(tree(t.dir), "AGENTS.md CLAUDE.md GEMINI.md jobs log.md")
|
|
366
|
+
assert.equal(read(t.dir, "log.md"), `- ${job.id} | job | started | /plugin/agent/pinokio.js, from Registry Client | answer a post\n`)
|
|
367
|
+
await t.brain.jobEnded(job.id)
|
|
368
|
+
assert.equal(read(t.dir, "log.md").split("\n")[1], `- ${job.id} | job | ended`)
|
|
369
|
+
|
|
370
|
+
// the brain can also be named
|
|
371
|
+
for (const cwd of ["brain", "brain/"]) {
|
|
372
|
+
const named = await t.brain.prepareJob({ ...BODY, cwd })
|
|
373
|
+
assert.equal(named.cwd, t.dir)
|
|
374
|
+
assert.equal(named.dir, path.join(t.dir, "jobs", named.id))
|
|
375
|
+
}
|
|
376
|
+
})
|
|
377
|
+
})
|
|
378
|
+
|
|
379
|
+
test("a plugin is named plugin/<name>, which also finds one that comes with Pinokio", async () => {
|
|
380
|
+
await withHome(async (t) => {
|
|
381
|
+
const start = async (plugin) => {
|
|
382
|
+
const job = await t.brain.prepareJob({ ...BODY, plugin })
|
|
383
|
+
return { script: job.script, plugin: json(job.dir, "info.json").plugin }
|
|
384
|
+
}
|
|
385
|
+
const inHome = { script: path.join(t.home, "plugin", "agent", "pinokio.js"), plugin: "/plugin/agent/pinokio.js" }
|
|
386
|
+
assert.deepEqual(await start("plugin/agent"), inHome)
|
|
387
|
+
assert.deepEqual(await start("plugin/agent/"), inHome)
|
|
388
|
+
// not in the home: the one that comes with Pinokio, recorded the way the plugin menu writes it
|
|
389
|
+
assert.deepEqual(await start("plugin/builtin"), { script: path.join(t.own, "plugin", "builtin", "pinokio.js"), plugin: "/pinokio/run/plugin/builtin/pinokio.js" })
|
|
390
|
+
// in both: the user's own wins
|
|
391
|
+
assert.deepEqual(await start("plugin/both"), { script: path.join(t.home, "plugin", "both", "pinokio.js"), plugin: "/plugin/both/pinokio.js" })
|
|
392
|
+
})
|
|
393
|
+
})
|
|
394
|
+
|
|
395
|
+
test("a job that starts in an app has its folder inside that app and the skill line in its request", async () => {
|
|
396
|
+
await withHome(async (t) => {
|
|
397
|
+
const app = path.join(t.api, "Speech")
|
|
398
|
+
fs.rmSync(app, { recursive: true, force: true })
|
|
399
|
+
fs.mkdirSync(app, { recursive: true })
|
|
400
|
+
const job = await t.brain.prepareJob({ ...BODY, cwd: "api/Speech" })
|
|
401
|
+
const dir = path.join(app, ".pinokio-jobs", job.id)
|
|
402
|
+
assert.equal(job.cwd, app, "the answer and the plugin get the full path")
|
|
403
|
+
assert.equal(job.dir, dir)
|
|
404
|
+
assert.equal(job.prompt, LINE(`.pinokio-jobs/${job.id}/request.md`))
|
|
405
|
+
assert.equal(tree(app), ".pinokio-jobs")
|
|
406
|
+
assert.equal(read(app, ".pinokio-jobs", ".gitignore"), "*\n")
|
|
407
|
+
assert.deepEqual(json(dir, "info.json"), { id: job.id, type: "job", plugin: "/plugin/agent/pinokio.js", cwd: app, app: "Speech" })
|
|
408
|
+
assert.equal(read(dir, "request.md"), `look at this\n\nOutput folder: ${path.join(dir, "output")}\n\nThis app's skill (skill_dir): ${path.join(app, ".pinokio-skill")} (skill_state: none)\n`)
|
|
409
|
+
assert.equal(tree(t.dir), "log.md", "only the log is in the brain")
|
|
410
|
+
assert.equal(read(t.dir, "log.md"), `- ${job.id} | job | started | /plugin/agent/pinokio.js in Speech |\n`)
|
|
411
|
+
assert.equal((await t.brain.prepareJob({ ...BODY, cwd: "api/Speech/" })).cwd, app)
|
|
412
|
+
})
|
|
413
|
+
})
|
|
414
|
+
|
|
415
|
+
test("a job that cannot be prepared leaves nothing behind", async () => {
|
|
416
|
+
await withHome(async (t) => {
|
|
417
|
+
fs.mkdirSync(path.join(t.api, "Speech", "sub"), { recursive: true })
|
|
418
|
+
const refused = async (body, status, message) => {
|
|
419
|
+
await assert.rejects(t.brain.prepareJob({ ...BODY, ...body }), (error) => {
|
|
420
|
+
assert.equal(error.status, status)
|
|
421
|
+
assert.match(error.message, message)
|
|
422
|
+
return true
|
|
423
|
+
})
|
|
424
|
+
}
|
|
425
|
+
await refused({ prompt: "" }, 400, /needs words/)
|
|
426
|
+
await refused({ plugin: "plugin/nobody" }, 404, /Plugin not found/)
|
|
427
|
+
await refused({ plugin: "plugin/jobless" }, 400, /no job/)
|
|
428
|
+
await refused({ plugin: "plugin/empty" }, 400, /no job/)
|
|
429
|
+
// one way to name a plugin: its folder from Pinokio's home
|
|
430
|
+
for (const plugin of ["", "agent", "builtin", "claude", "pinokio/run/plugin/builtin", "/pinokio/run/plugin/builtin/pinokio.js", "run/plugin/agent", "/plugin/agent", path.join(t.home, "plugin", "agent"), "plugin", "plugin/", "brain/agent", "plugin/agent/pinokio.js", "api/Speech/tools/helper/pinokio.js",
|
|
431
|
+
// a folder, never a file: also not with a slash after it
|
|
432
|
+
"plugin/agent/pinokio.js/", "api/Speech/start.js/", "api/Speech/pinokio.json", "api/Speech/tools/helper/PINOKIO.JS",
|
|
433
|
+
// never an app's folder, or a plugin file inside one
|
|
434
|
+
"api/Speech", "api/Speech/", "api", "api/", "api/Speech/tools/helper", "api/Speech/tools/helper/", "api/Speech/nothing",
|
|
435
|
+
// nothing but folder names
|
|
436
|
+
"plugin/../api/Speech/tools/helper", "api/Speech/../Speech/tools/helper", "plugin/./agent", "plugin//agent", "plugin/agent//", "plugin/code/claude", "plugin\\agent"]) {
|
|
437
|
+
await refused({ plugin }, 400, /plugin must be a plugin's folder as a path from the Pinokio home/)
|
|
438
|
+
}
|
|
439
|
+
await refused({ cwd: "api/Nothing" }, 404, /App not found/)
|
|
440
|
+
await refused({ cwd: "api/.." }, 404, /App not found/)
|
|
441
|
+
// the start folder is the brain or an app's folder, as a path from Pinokio's home
|
|
442
|
+
for (const cwd of [path.join(t.api, "Speech"), t.dir, "/", "../brain", "api", "api/", "api/Speech/sub", "api\\Speech", "brains", "plugin/agent", "Speech", 7, null, ["brain"], { brain: true }]) {
|
|
443
|
+
await refused({ cwd }, 400, /cwd must be/)
|
|
444
|
+
}
|
|
445
|
+
assert.equal(fs.existsSync(t.dir), false, "no brain, no folder, no log line")
|
|
446
|
+
assert.equal(tree(path.join(t.api, "Speech")), "sub tools")
|
|
447
|
+
})
|
|
448
|
+
})
|
|
449
|
+
|
|
450
|
+
// ---- the calls ------------------------------------------------------------------
|
|
451
|
+
|
|
452
|
+
const call = (port, method, route, { body, headers = {} } = {}) => new Promise((resolve, reject) => {
|
|
453
|
+
const text = body ? JSON.stringify(body) : ""
|
|
454
|
+
const req = http.request({ host: "127.0.0.1", port, method, path: route, headers: { ...(text ? { "content-type": "application/json" } : {}), ...headers } }, (res) => {
|
|
455
|
+
let data = ""
|
|
456
|
+
res.on("data", (chunk) => { data += chunk })
|
|
457
|
+
res.on("end", () => resolve({ code: res.statusCode, ...JSON.parse(data) }))
|
|
458
|
+
})
|
|
459
|
+
req.on("error", reject)
|
|
460
|
+
req.end(text)
|
|
461
|
+
})
|
|
462
|
+
const withServer = async (routes, run) => {
|
|
463
|
+
const app = express()
|
|
464
|
+
app.use(express.json())
|
|
465
|
+
registerJobRoutes(app, routes)
|
|
466
|
+
const server = app.listen(0, "127.0.0.1")
|
|
467
|
+
await new Promise((resolve) => server.once("listening", resolve))
|
|
468
|
+
try {
|
|
469
|
+
await run(server.address().port)
|
|
470
|
+
} finally {
|
|
471
|
+
await new Promise((resolve) => server.close(resolve))
|
|
472
|
+
}
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
test("the calls start a job, tell its state and stop it", async () => {
|
|
476
|
+
await withFolder(async (home) => {
|
|
477
|
+
const t = fakeKernel()
|
|
478
|
+
const jobs = createJobs({ kernel: t.kernel, ended: async () => {} })
|
|
479
|
+
const prepared = []
|
|
480
|
+
const prepare = async (job) => {
|
|
481
|
+
prepared.push(job)
|
|
482
|
+
if (job.plugin === "nobody") {
|
|
483
|
+
throw Object.assign(new Error("Plugin not found."), { status: 404 })
|
|
484
|
+
}
|
|
485
|
+
return newJob(home, `job${prepared.length}`)
|
|
486
|
+
}
|
|
487
|
+
await withServer({ jobs, prepare }, async (port) => {
|
|
488
|
+
const started = await call(port, "POST", "/brain/job", { body: { plugin: " plugin/agent ", cwd: " brain ", prompt: " look ", from: " me ", label: " a label ", files: ["ignored"], extra: "ignored" } })
|
|
489
|
+
assert.deepEqual(started, { code: 200, ok: true, id: "job1", cwd: home })
|
|
490
|
+
assert.deepEqual(prepared[0], { plugin: "plugin/agent", cwd: "brain", prompt: "look", from: "me", label: "a label" })
|
|
491
|
+
// a cwd that is not text is passed on as it is, to be refused
|
|
492
|
+
await call(port, "POST", "/brain/job", { body: { plugin: "plugin/agent", cwd: 7, prompt: "x" } })
|
|
493
|
+
assert.equal(prepared[1].cwd, 7)
|
|
494
|
+
await call(port, "POST", "/brain/job", { body: { plugin: "plugin/agent", prompt: "x" } })
|
|
495
|
+
assert.equal(prepared[2].cwd, "")
|
|
496
|
+
assert.deepEqual(await call(port, "GET", "/brain/job/job1"), { code: 200, ok: true, id: "job1", state: "running" })
|
|
497
|
+
assert.deepEqual(await call(port, "POST", "/brain/job/job1/stop"), { code: 200, ok: true, id: "job1", state: "ended" })
|
|
498
|
+
assert.deepEqual(await call(port, "GET", "/brain/job/job1"), { code: 200, ok: true, id: "job1", state: "ended" })
|
|
499
|
+
assert.deepEqual(t.released, ["job1"])
|
|
500
|
+
|
|
501
|
+
assert.deepEqual(await call(port, "GET", "/brain/job/nobody"), { code: 404, ok: false, error: "Job not found." })
|
|
502
|
+
assert.deepEqual(await call(port, "POST", "/brain/job/nobody/stop"), { code: 404, ok: false, error: "Job not found." })
|
|
503
|
+
assert.deepEqual(await call(port, "POST", "/brain/job", { body: { prompt: "x", plugin: "nobody" } }), { code: 404, ok: false, error: "Plugin not found." })
|
|
504
|
+
assert.equal(jobs.state("job4"), null, "a job that could not be prepared is not started")
|
|
505
|
+
})
|
|
506
|
+
})
|
|
507
|
+
})
|
|
508
|
+
|
|
509
|
+
test("only this computer may use the calls", async () => {
|
|
510
|
+
await withFolder(async (home) => {
|
|
511
|
+
const t = fakeKernel()
|
|
512
|
+
const jobs = createJobs({ kernel: t.kernel, ended: async () => {} })
|
|
513
|
+
let count = 0
|
|
514
|
+
const prepare = async () => newJob(home, `job${count += 1}`)
|
|
515
|
+
await withServer({ jobs, prepare }, async (port) => {
|
|
516
|
+
const start = (headers) => call(port, "POST", "/brain/job", { body: { prompt: "x", plugin: "agent" }, headers })
|
|
517
|
+
const refused = { code: 403, ok: false, error: "Only this computer may use jobs." }
|
|
518
|
+
// Pinokio's own page, opened on this computer
|
|
519
|
+
assert.equal((await start({ origin: `http://127.0.0.1:${port}` })).code, 200)
|
|
520
|
+
// another site's page in the user's browser
|
|
521
|
+
assert.deepEqual(await start({ origin: "https://example.com" }), refused)
|
|
522
|
+
assert.deepEqual(await start({ origin: `http://localhost:${port + 1}` }), refused)
|
|
523
|
+
// passed on from somewhere else
|
|
524
|
+
assert.deepEqual(await start({ "x-forwarded-for": "203.0.113.9" }), refused)
|
|
525
|
+
assert.deepEqual(await start({ "x-pinokio-peer": "1" }), refused)
|
|
526
|
+
// reached under a name that is not this computer's
|
|
527
|
+
assert.deepEqual(await start({ host: "pinokio.example.com" }), refused)
|
|
528
|
+
assert.deepEqual(await call(port, "GET", "/brain/job/job1", { headers: { origin: "https://example.com" } }), refused)
|
|
529
|
+
assert.deepEqual(await call(port, "POST", "/brain/job/job1/stop", { headers: { origin: "https://example.com" } }), refused)
|
|
530
|
+
assert.equal(count, 1, "a refused call prepares nothing")
|
|
531
|
+
assert.equal(jobs.state("job1"), "running", "and stops nothing")
|
|
532
|
+
})
|
|
533
|
+
})
|
|
534
|
+
})
|