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
|
@@ -382,3 +382,426 @@ test("step clears resolved function actions when an RPC throws", async () => {
|
|
|
382
382
|
assert.equal(api.running_paths[request.id], request.path)
|
|
383
383
|
})
|
|
384
384
|
})
|
|
385
|
+
|
|
386
|
+
// ---- jobs ---------------------------------------------------------------------
|
|
387
|
+
|
|
388
|
+
test("a job follows the same installed check as run, and prints the notice it has no page for", async () => {
|
|
389
|
+
const api = new Api(createKernel())
|
|
390
|
+
let installed = false
|
|
391
|
+
let jobCalls = 0
|
|
392
|
+
const script = {
|
|
393
|
+
title: "Demo Plugin",
|
|
394
|
+
installed: async () => installed,
|
|
395
|
+
install: [{ method: "shell.run", params: { message: "install" } }],
|
|
396
|
+
run: [{ method: "shell.run", params: { message: "run" } }],
|
|
397
|
+
job: async () => {
|
|
398
|
+
jobCalls += 1
|
|
399
|
+
return [{ method: "shell.run", params: { message: "work once" } }]
|
|
400
|
+
},
|
|
401
|
+
}
|
|
402
|
+
api.actionContext = async ({ input, args, actionKey }) => ({ input, args, action: actionKey })
|
|
403
|
+
const resolve = (id, actionKey = "job") => api.resolveActionSteps({
|
|
404
|
+
request: { id, path: "/pinokio/plugin/demo/pinokio.js" },
|
|
405
|
+
script,
|
|
406
|
+
scriptDir: "/pinokio/plugin/demo",
|
|
407
|
+
actionKey,
|
|
408
|
+
input: {},
|
|
409
|
+
args: {},
|
|
410
|
+
})
|
|
411
|
+
const steps = [{ method: "shell.run", params: { message: "work once" } }]
|
|
412
|
+
|
|
413
|
+
// not installed: neither action starts. `run` shows its notice on the page,
|
|
414
|
+
// a job prints it
|
|
415
|
+
assert.deepEqual(await resolve("job-1"), [{
|
|
416
|
+
method: "log",
|
|
417
|
+
params: { raw: "Demo Plugin is not installed. Open its plugin page in Pinokio and click Install.\n" },
|
|
418
|
+
}])
|
|
419
|
+
assert.equal(jobCalls, 0, "the job's own steps are not built")
|
|
420
|
+
assert.equal((await resolve("run-1", "run"))[0].method, "notify")
|
|
421
|
+
|
|
422
|
+
// installed: both start
|
|
423
|
+
installed = true
|
|
424
|
+
assert.deepEqual(await resolve("job-2"), steps)
|
|
425
|
+
assert.deepEqual(await resolve("run-2", "run"), script.run)
|
|
426
|
+
|
|
427
|
+
// a plugin with nothing to install is started whatever the check says, by either action
|
|
428
|
+
installed = false
|
|
429
|
+
delete script.install
|
|
430
|
+
assert.deepEqual(await resolve("job-3"), steps)
|
|
431
|
+
assert.deepEqual(await resolve("run-3", "run"), script.run)
|
|
432
|
+
})
|
|
433
|
+
|
|
434
|
+
// a real Api that gets as far as queueing the first step, which is recorded instead of run
|
|
435
|
+
async function withJobApi(fn) {
|
|
436
|
+
await withStepApi(async ({ api, appDir }) => {
|
|
437
|
+
const packets = []
|
|
438
|
+
const queued = []
|
|
439
|
+
const killed = []
|
|
440
|
+
api.kernel.shell = { kill: ({ group }) => killed.push(group), init: async () => {} }
|
|
441
|
+
api.kernel.dns = async () => {}
|
|
442
|
+
api.logSessions = null
|
|
443
|
+
api.listen("test", (packet) => packets.push(packet))
|
|
444
|
+
api.queue = (request, rawrpc) => queued.push({ id: request.id, rawrpc })
|
|
445
|
+
// a job's plugin is never inside an app's folder; the agent works in one here
|
|
446
|
+
const script = path.join(api.kernel.homedir, "plugin", "demo", "pinokio.js")
|
|
447
|
+
await fs.mkdir(path.dirname(script), { recursive: true })
|
|
448
|
+
await fs.writeFile(script, "module.exports = {}\n")
|
|
449
|
+
const steps = [{ method: "test.ok" }]
|
|
450
|
+
api.resolveScript = async () => ({ cwd: path.dirname(script), script: { run: steps, job: steps } })
|
|
451
|
+
const request = (id, action = "job") => ({ id, uri: script, path: script, cwd: appDir, action, input: {} })
|
|
452
|
+
await fn({ api, script, packets, queued, killed, request })
|
|
453
|
+
})
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
test("a plugin's job runs only for a request the job runner registered", async () => {
|
|
457
|
+
await withJobApi(async ({ api, packets, queued, request }) => {
|
|
458
|
+
const refusal = "A plugin's job is started by Pinokio's job runner only."
|
|
459
|
+
|
|
460
|
+
// from a page or the socket: nothing registered it
|
|
461
|
+
const forged = request("forged")
|
|
462
|
+
await api.process(forged)
|
|
463
|
+
assert.deepEqual(queued, [])
|
|
464
|
+
assert.equal(api.running.forged, undefined)
|
|
465
|
+
assert.deepEqual(packets.filter((packet) => packet.type === "error").map((packet) => [packet.id, packet.data]), [["forged", refusal]])
|
|
466
|
+
|
|
467
|
+
// the same id as a registered job, but not the runner's own request
|
|
468
|
+
const real = request("real")
|
|
469
|
+
api.jobs.set("real", real)
|
|
470
|
+
await api.process(request("real"))
|
|
471
|
+
assert.deepEqual(queued, [])
|
|
472
|
+
assert.equal(packets.filter((packet) => packet.type === "error").length, 2)
|
|
473
|
+
|
|
474
|
+
// the runner's request
|
|
475
|
+
await api.process(real)
|
|
476
|
+
assert.deepEqual(queued.map((item) => item.id), ["real"])
|
|
477
|
+
assert.equal(api.running.real, true)
|
|
478
|
+
|
|
479
|
+
// every other action is as before
|
|
480
|
+
await api.process(request("a-run", "run"))
|
|
481
|
+
assert.deepEqual(queued.map((item) => item.id), ["real", "a-run"])
|
|
482
|
+
})
|
|
483
|
+
})
|
|
484
|
+
|
|
485
|
+
test("a job stopped before its steps have started runs none of them", async () => {
|
|
486
|
+
await withJobApi(async ({ api, packets, queued, request }) => {
|
|
487
|
+
// stopped while the plugin's file is being read: it is no longer a job when its turn comes
|
|
488
|
+
const early = request("early")
|
|
489
|
+
api.jobs.set("early", early)
|
|
490
|
+
const { resolveScript } = api
|
|
491
|
+
api.resolveScript = async (...args) => {
|
|
492
|
+
api.endJob("early")
|
|
493
|
+
return resolveScript(...args)
|
|
494
|
+
}
|
|
495
|
+
await api.process(early)
|
|
496
|
+
api.resolveScript = resolveScript
|
|
497
|
+
assert.deepEqual(queued, [])
|
|
498
|
+
assert.equal(api.running.early, undefined)
|
|
499
|
+
assert.deepEqual(packets.map((packet) => packet.type), ["disconnect", "error"])
|
|
500
|
+
})
|
|
501
|
+
})
|
|
502
|
+
|
|
503
|
+
test("a job's steps are not what the kernel remembers as its last run", async () => {
|
|
504
|
+
await withJobApi(async ({ api, script, request }) => {
|
|
505
|
+
api.resolveMethod = async () => ({ method: async () => {}, dirname: path.dirname(script) })
|
|
506
|
+
api.run = async () => ({ response: "ok" })
|
|
507
|
+
const step = { method: "test.ok" }
|
|
508
|
+
|
|
509
|
+
// an ordinary run is remembered, as before
|
|
510
|
+
const run = request("a-run", "run")
|
|
511
|
+
api.running[run.id] = true
|
|
512
|
+
api.resolved_actions[run.id] = { actionKey: "run", steps: [step] }
|
|
513
|
+
await api.step(run, step, {}, 0, 1, {})
|
|
514
|
+
assert.equal(api.executing, run)
|
|
515
|
+
const remembered = api.state
|
|
516
|
+
assert.ok(remembered)
|
|
517
|
+
|
|
518
|
+
// a job is kept by its id and by nothing else: once it has ended, nothing here points at it
|
|
519
|
+
const job = request("job-1")
|
|
520
|
+
api.jobs.set("job-1", job)
|
|
521
|
+
api.running[job.id] = true
|
|
522
|
+
api.resolved_actions[job.id] = { actionKey: "job", steps: [step] }
|
|
523
|
+
await api.step(job, step, {}, 0, 1, {})
|
|
524
|
+
assert.equal(api.executing, run, "not the job's request")
|
|
525
|
+
assert.equal(api.state, remembered, "nor its step's values")
|
|
526
|
+
})
|
|
527
|
+
})
|
|
528
|
+
|
|
529
|
+
test("ending a job lets go of everything kept under its id, and of nothing else", async () => {
|
|
530
|
+
await withJobApi(async ({ api, script, packets, killed, request }) => {
|
|
531
|
+
const calls = []
|
|
532
|
+
api.stopProxy = (...args) => calls.push(["stopProxy", ...args])
|
|
533
|
+
api.kernel.resumeprocess = (...args) => calls.push(["resumeprocess", ...args])
|
|
534
|
+
api.kernel.stopCloudflare = async (...args) => calls.push(["stopCloudflare", ...args])
|
|
535
|
+
api.kernel.markAppLaunchStopped = () => calls.push(["markAppLaunchStopped"])
|
|
536
|
+
|
|
537
|
+
// another run of the same plugin, kept by script path
|
|
538
|
+
let others = 0
|
|
539
|
+
api.running[script] = true
|
|
540
|
+
api.running_paths[script] = script
|
|
541
|
+
api.done[script] = () => { others += 1 }
|
|
542
|
+
api.kernel.memory.local[script] = { kept: true }
|
|
543
|
+
api.waiter[script] = { resolve() {}, reject() { calls.push(["waiter rejected"]) } }
|
|
544
|
+
|
|
545
|
+
const job = request("job-1")
|
|
546
|
+
api.jobs.set("job-1", job)
|
|
547
|
+
await api.process(job)
|
|
548
|
+
for (const kept of ["local", "global", "rpc", "args", "input"]) {
|
|
549
|
+
api.kernel.memory[kept]["job-1"] = { own: true }
|
|
550
|
+
}
|
|
551
|
+
assert.equal(await api.stop({ params: { id: "job-1" } }, null, { internal_completion: true }), true)
|
|
552
|
+
|
|
553
|
+
assert.deepEqual(killed, ["job-1"], "only its own shells")
|
|
554
|
+
assert.equal(api.running["job-1"], undefined)
|
|
555
|
+
assert.equal(api.running_paths["job-1"], undefined)
|
|
556
|
+
assert.equal("job-1" in api.done, false)
|
|
557
|
+
assert.equal(api.resolved_actions["job-1"], undefined)
|
|
558
|
+
for (const kept of ["local", "global", "rpc", "args", "input"]) {
|
|
559
|
+
assert.equal("job-1" in api.kernel.memory[kept], false, `nothing of it is left in memory.${kept}`)
|
|
560
|
+
}
|
|
561
|
+
assert.equal(api.jobs.has("job-1"), false)
|
|
562
|
+
assert.deepEqual(packets.map((packet) => [packet.id, packet.type]), [["job-1", "disconnect"]])
|
|
563
|
+
// ended once: a second time does and says nothing
|
|
564
|
+
assert.equal(api.endJob("job-1"), false)
|
|
565
|
+
|
|
566
|
+
// a shell that cannot be killed does not keep the job: it is let go of all the same
|
|
567
|
+
const stuck = request("job-2")
|
|
568
|
+
api.jobs.set("job-2", stuck)
|
|
569
|
+
await api.process(stuck)
|
|
570
|
+
const { kill } = api.kernel.shell
|
|
571
|
+
api.kernel.shell.kill = () => { throw new Error("the shell would not die") }
|
|
572
|
+
assert.throws(() => api.endJob("job-2"), /the shell would not die/)
|
|
573
|
+
api.kernel.shell.kill = kill
|
|
574
|
+
assert.equal(api.jobs.has("job-2"), false)
|
|
575
|
+
assert.equal(api.running["job-2"], undefined)
|
|
576
|
+
assert.equal(api.hasActiveRequest(stuck), false, "no further step of it runs")
|
|
577
|
+
assert.deepEqual(packets.map((packet) => [packet.id, packet.type]), [["job-1", "disconnect"], ["job-2", "disconnect"]])
|
|
578
|
+
packets.pop()
|
|
579
|
+
assert.deepEqual(killed, ["job-1"])
|
|
580
|
+
assert.equal(packets.length, 1)
|
|
581
|
+
// the other run is untouched
|
|
582
|
+
assert.equal(api.running[script], true)
|
|
583
|
+
assert.equal(api.running_paths[script], script)
|
|
584
|
+
assert.deepEqual(api.kernel.memory.local[script], { kept: true })
|
|
585
|
+
assert.equal(typeof api.done[script], "function")
|
|
586
|
+
assert.equal(others, 0)
|
|
587
|
+
assert.ok(api.waiter[script])
|
|
588
|
+
assert.deepEqual(calls, [], "nothing kept by script path is stopped")
|
|
589
|
+
})
|
|
590
|
+
})
|
|
591
|
+
|
|
592
|
+
// a real Api whose steps really run, with a method that does what the test says
|
|
593
|
+
async function withRunningJobApi(fn) {
|
|
594
|
+
await withJobApi(async ({ api, script, packets, request }) => {
|
|
595
|
+
api.queue = Api.prototype.queue
|
|
596
|
+
// each step is rendered into a copy, as the real template does
|
|
597
|
+
api.kernel.template.render = (value) => (value && typeof value === "object" && !Array.isArray(value) ? { ...value } : value)
|
|
598
|
+
const progress = []
|
|
599
|
+
api.kernel.markAppLaunchProgress = (launchPath) => { progress.push(path.basename(launchPath)) }
|
|
600
|
+
api.kernel.resumeprocess = () => {}
|
|
601
|
+
api.kernel.stopCloudflare = async () => {}
|
|
602
|
+
api.stopProxy = () => {}
|
|
603
|
+
// a script that started this plugin's file is waiting for it by path
|
|
604
|
+
let waiting = 0
|
|
605
|
+
api.done[script] = () => { waiting += 1 }
|
|
606
|
+
let answer = async () => ({})
|
|
607
|
+
api.resolveMethod = async (rpc) => (rpc.method === "test.nothing" ? {} : { method: async () => {}, dirname: path.dirname(script) })
|
|
608
|
+
api.run = async () => answer()
|
|
609
|
+
const until = async (check, what) => {
|
|
610
|
+
for (let tries = 0; tries < 400; tries += 1) {
|
|
611
|
+
if (check()) {
|
|
612
|
+
return
|
|
613
|
+
}
|
|
614
|
+
await new Promise((resolve) => setTimeout(resolve, 5))
|
|
615
|
+
}
|
|
616
|
+
assert.fail(`never happened: ${what}`)
|
|
617
|
+
}
|
|
618
|
+
// `rest` is what else the plugin's file says, such as `daemon`
|
|
619
|
+
const start = async (id, action, steps, rest = {}) => {
|
|
620
|
+
const run = request(id, action)
|
|
621
|
+
if (action === "job") {
|
|
622
|
+
api.jobs.set(id, run)
|
|
623
|
+
}
|
|
624
|
+
api.resolveScript = async () => ({ cwd: path.dirname(script), script: { ...rest, [action]: steps } })
|
|
625
|
+
await api.process(run)
|
|
626
|
+
return run
|
|
627
|
+
}
|
|
628
|
+
await fn({ api, script, packets, progress, start, until, waiting: () => waiting, answers: (next) => { answer = next } })
|
|
629
|
+
})
|
|
630
|
+
}
|
|
631
|
+
|
|
632
|
+
test("a job answers nobody and records nothing under its plugin's path", async () => {
|
|
633
|
+
await withRunningJobApi(async ({ api, script, packets, progress, start, until, waiting, answers }) => {
|
|
634
|
+
const about = (id) => packets.filter((packet) => packet.id === id).map((packet) => packet.type)
|
|
635
|
+
const untouched = (what) => {
|
|
636
|
+
assert.deepEqual(progress, [], `${what}: no progress is recorded for the plugin's file`)
|
|
637
|
+
assert.equal(waiting(), 0, `${what}: a script waiting for this file by its path is not answered`)
|
|
638
|
+
assert.deepEqual(about(script), [], `${what}: nothing is said under the plugin's path`)
|
|
639
|
+
}
|
|
640
|
+
|
|
641
|
+
// a job whose steps run to their end
|
|
642
|
+
await start("done", "job", [{ method: "test.ok" }, { method: "test.ok" }])
|
|
643
|
+
await until(() => about("done").includes("disconnect"), "the job ends by itself")
|
|
644
|
+
await new Promise((resolve) => setTimeout(resolve, 20))
|
|
645
|
+
untouched("a job that ended")
|
|
646
|
+
assert.equal(api.running.done, undefined)
|
|
647
|
+
assert.equal(api.jobs.has("done"), false)
|
|
648
|
+
assert.equal("done" in api.kernel.memory.local, false)
|
|
649
|
+
|
|
650
|
+
// a job whose step fails: the error is the job's, and the job is still the kernel's until it is ended
|
|
651
|
+
answers(async () => ({ error: ["boom"], response: "it broke" }))
|
|
652
|
+
await start("failed", "job", [{ method: "test.ok" }])
|
|
653
|
+
await until(() => about("failed").includes("error"), "the failure is said under the job's id")
|
|
654
|
+
await new Promise((resolve) => setTimeout(resolve, 20))
|
|
655
|
+
untouched("a job that failed")
|
|
656
|
+
assert.equal(api.endJob("failed"), true)
|
|
657
|
+
|
|
658
|
+
// a step that names a method Pinokio does not have
|
|
659
|
+
answers(async () => ({}))
|
|
660
|
+
await start("unknown", "job", [{ method: "test.nothing" }])
|
|
661
|
+
await until(() => about("unknown").includes("error"), "the error is said under the job's id")
|
|
662
|
+
assert.match(packets.find((packet) => packet.id === "unknown" && packet.type === "error").data, /RPC endpoint doesn't exist/)
|
|
663
|
+
await new Promise((resolve) => setTimeout(resolve, 20))
|
|
664
|
+
untouched("a job with an unknown method")
|
|
665
|
+
assert.equal(api.endJob("unknown"), true)
|
|
666
|
+
|
|
667
|
+
// a step that throws inside the queue
|
|
668
|
+
const { step } = api
|
|
669
|
+
api.step = async () => { throw new Error("cannot run this step") }
|
|
670
|
+
await start("thrown", "job", [{ method: "test.ok" }])
|
|
671
|
+
await until(() => about("thrown").includes("error"), "the error is said under the job's id")
|
|
672
|
+
api.step = step
|
|
673
|
+
untouched("a job whose step threw")
|
|
674
|
+
assert.equal(api.endJob("thrown"), true)
|
|
675
|
+
|
|
676
|
+
// a job stopped while a step is still working: the late end of that step changes nothing either
|
|
677
|
+
let finish
|
|
678
|
+
answers(() => new Promise((resolve) => { finish = resolve }))
|
|
679
|
+
await start("late", "job", [{ method: "test.ok" }])
|
|
680
|
+
await until(() => typeof finish === "function", "the step is working")
|
|
681
|
+
assert.equal(api.endJob("late"), true)
|
|
682
|
+
finish({})
|
|
683
|
+
await new Promise((resolve) => setTimeout(resolve, 20))
|
|
684
|
+
untouched("a job stopped in the middle of a step")
|
|
685
|
+
|
|
686
|
+
// the same steps as `run`: its progress is recorded and said under the path, as before
|
|
687
|
+
answers(async () => ({}))
|
|
688
|
+
const run = `${script}?cwd=/somewhere/else`
|
|
689
|
+
await start(run, "run", [{ method: "test.ok" }])
|
|
690
|
+
await until(() => about(run).includes("disconnect") || about(run).includes("error"), "the run ends")
|
|
691
|
+
assert.deepEqual(about(run).filter((type) => type === "error"), [])
|
|
692
|
+
assert.deepEqual(progress, ["pinokio.js"])
|
|
693
|
+
assert.ok(about(script).includes("memory"))
|
|
694
|
+
})
|
|
695
|
+
})
|
|
696
|
+
|
|
697
|
+
test("a job ends when its steps are over, also in a plugin whose run stays up", async () => {
|
|
698
|
+
await withRunningJobApi(async ({ api, script, packets, start, until }) => {
|
|
699
|
+
const about = (id) => packets.filter((packet) => packet.id === id).map((packet) => packet.type)
|
|
700
|
+
const stays = (id) => packets.some((packet) => packet.id === id && packet.type === "start" && packet.data && /daemon mode/.test(packet.data.description || ""))
|
|
701
|
+
|
|
702
|
+
// its last step ran
|
|
703
|
+
await start("ran", "job", [{ method: "test.ok" }], { daemon: true })
|
|
704
|
+
await until(() => about("ran").includes("disconnect"), "the job ends after its last step")
|
|
705
|
+
assert.equal(api.jobs.has("ran"), false)
|
|
706
|
+
assert.equal(api.running.ran, undefined)
|
|
707
|
+
assert.equal(stays("ran"), false)
|
|
708
|
+
|
|
709
|
+
// its last step was skipped
|
|
710
|
+
await start("skipped", "job", [{ method: "test.ok" }, { when: "", method: "test.ok" }], { daemon: true })
|
|
711
|
+
await until(() => about("skipped").includes("disconnect"), "the job ends after a skipped last step")
|
|
712
|
+
assert.equal(api.running.skipped, undefined)
|
|
713
|
+
assert.equal(stays("skipped"), false)
|
|
714
|
+
|
|
715
|
+
// the same file's `run` stays up after its steps, as before
|
|
716
|
+
const run = `${script}?cwd=/somewhere/else`
|
|
717
|
+
await start(run, "run", [{ method: "test.ok" }], { daemon: true })
|
|
718
|
+
await until(() => stays(run), "the run says it stays up")
|
|
719
|
+
await new Promise((resolve) => setTimeout(resolve, 20))
|
|
720
|
+
assert.equal(api.running[run], true)
|
|
721
|
+
assert.equal(about(run).includes("disconnect"), false)
|
|
722
|
+
})
|
|
723
|
+
})
|
|
724
|
+
|
|
725
|
+
test("a job that ends while one of its steps is starting leaves nothing under its id", async () => {
|
|
726
|
+
await withRunningJobApi(async ({ api, start, until, answers }) => {
|
|
727
|
+
const left = (id) => ["running", "running_paths", "done", "resolved_actions"].filter((kept) => id in api[kept])
|
|
728
|
+
.concat(["local", "global", "rpc", "args", "input"].filter((kept) => id in api.kernel.memory[kept]).map((kept) => `memory.${kept}`))
|
|
729
|
+
|
|
730
|
+
// ended while the step is still reading what it needs
|
|
731
|
+
let release
|
|
732
|
+
api.kernel.update_sysinfo = () => new Promise((resolve) => { release = resolve })
|
|
733
|
+
await start("early", "job", [{ method: "test.ok" }])
|
|
734
|
+
await until(() => typeof release === "function", "the step is starting")
|
|
735
|
+
assert.equal(api.endJob("early"), true)
|
|
736
|
+
release()
|
|
737
|
+
await new Promise((resolve) => setTimeout(resolve, 30))
|
|
738
|
+
assert.deepEqual(left("early"), [])
|
|
739
|
+
api.kernel.update_sysinfo = async () => {}
|
|
740
|
+
|
|
741
|
+
// ended just after the step has said what it starts with: the request it carries is not kept either
|
|
742
|
+
api.listen("stopper", (packet) => {
|
|
743
|
+
if (packet.id === "later" && packet.type === "memory") {
|
|
744
|
+
api.endJob("later")
|
|
745
|
+
}
|
|
746
|
+
})
|
|
747
|
+
await start("later", "job", [{ method: "test.ok" }])
|
|
748
|
+
await new Promise((resolve) => setTimeout(resolve, 30))
|
|
749
|
+
assert.deepEqual(left("later"), [])
|
|
750
|
+
api.listeners.stopper = () => {}
|
|
751
|
+
|
|
752
|
+
// ended while its steps are being made: none of them runs
|
|
753
|
+
let ran = 0
|
|
754
|
+
answers(async () => { ran += 1; return {} })
|
|
755
|
+
await start("building", "job", async () => {
|
|
756
|
+
api.endJob("building")
|
|
757
|
+
return [{ method: "test.ok" }]
|
|
758
|
+
})
|
|
759
|
+
await new Promise((resolve) => setTimeout(resolve, 30))
|
|
760
|
+
assert.equal(ran, 0)
|
|
761
|
+
assert.deepEqual(left("building"), [])
|
|
762
|
+
})
|
|
763
|
+
})
|
|
764
|
+
|
|
765
|
+
// ---- errors of a run reach whoever watches that run ----------------------------
|
|
766
|
+
|
|
767
|
+
test("a step that throws outside its own handling is reported as an error of that run", async () => {
|
|
768
|
+
await withStepApi(async ({ api, appDir }) => {
|
|
769
|
+
const packets = []
|
|
770
|
+
api.listen("test", (packet) => packets.push(packet))
|
|
771
|
+
api.logSessions = null
|
|
772
|
+
const script = path.join(appDir, "start.js")
|
|
773
|
+
api.running.thrower = true
|
|
774
|
+
api.step = async () => { throw new Error("cannot resolve this step") }
|
|
775
|
+
api.queue({ id: "thrower", uri: script, path: script, action: "run" }, { method: "test.unknown" }, {}, 0, 1, appDir, {})
|
|
776
|
+
for (let tries = 0; tries < 100 && packets.length === 0; tries += 1) {
|
|
777
|
+
await new Promise((resolve) => setTimeout(resolve, 5))
|
|
778
|
+
}
|
|
779
|
+
assert.equal(packets.length, 1)
|
|
780
|
+
assert.equal(packets[0].id, "thrower")
|
|
781
|
+
assert.equal(packets[0].type, "error")
|
|
782
|
+
assert.match(packets[0].data, /cannot resolve this step/)
|
|
783
|
+
})
|
|
784
|
+
})
|
|
785
|
+
|
|
786
|
+
test("a step that names a method Pinokio does not have is reported under the run's own id", async () => {
|
|
787
|
+
await withStepApi(async ({ api, appDir }) => {
|
|
788
|
+
const packets = []
|
|
789
|
+
api.listen("test", (packet) => packets.push(packet))
|
|
790
|
+
// each step is rendered into a copy, as the real template does
|
|
791
|
+
api.kernel.template.render = (value) => (value && typeof value === "object" && !Array.isArray(value) ? { ...value } : value)
|
|
792
|
+
const script = path.join(appDir, "plugin", "pinokio.js")
|
|
793
|
+
const request = { id: `${script}?cwd=/somewhere`, path: script }
|
|
794
|
+
const steps = [{ method: "test.nothing" }]
|
|
795
|
+
api.running[request.id] = true
|
|
796
|
+
api.resolved_actions[request.id] = { actionKey: "run", steps }
|
|
797
|
+
api.resolveScript = async () => ({ cwd: appDir, script: { run: steps } })
|
|
798
|
+
api.resolveMethod = async () => ({})
|
|
799
|
+
|
|
800
|
+
await api.step(request, steps[0], {}, 0, 1, {})
|
|
801
|
+
|
|
802
|
+
const errors = packets.filter((packet) => packet.type === "error")
|
|
803
|
+
assert.equal(errors.length, 1)
|
|
804
|
+
assert.equal(errors[0].id, request.id, "not under the bare script path, which nobody watching this run listens to")
|
|
805
|
+
assert.match(errors[0].data, /RPC endpoint doesn't exist/)
|
|
806
|
+
})
|
|
807
|
+
})
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
"use strict"
|
|
2
2
|
|
|
3
|
-
// spec/requirements/request-files.md
|
|
4
3
|
const test = require("node:test")
|
|
5
4
|
const assert = require("node:assert/strict")
|
|
6
5
|
const fs = require("fs")
|
|
@@ -20,17 +19,7 @@ const LINE = (file) => `My message is in ${file}. Read it and respond as if I ha
|
|
|
20
19
|
|
|
21
20
|
test("the request line is one line that names the file", () => {
|
|
22
21
|
assert.equal(RequestFile.line("SPEC.md"), LINE("SPEC.md"))
|
|
23
|
-
assert.equal(/[\r\n]/.test(RequestFile.line("
|
|
24
|
-
})
|
|
25
|
-
|
|
26
|
-
test("an Ask's request is saved in the folder the agent starts in, lines intact", async () => {
|
|
27
|
-
await withFolder(async (dir) => {
|
|
28
|
-
const line = await RequestFile.save(dir, "20261117-aaaaaaaa", "one\n\ntwo\n- three")
|
|
29
|
-
assert.equal(line, LINE(".pinokio-temp/requests/20261117-aaaaaaaa.md"), "the path uses / on every system")
|
|
30
|
-
assert.equal(fs.readFileSync(path.join(dir, ".pinokio-temp", "requests", "20261117-aaaaaaaa.md"), "utf8"), "one\n\ntwo\n- three\n")
|
|
31
|
-
await RequestFile.save(dir, "20261117-bbbbbbbb", "another")
|
|
32
|
-
assert.equal(fs.readdirSync(path.join(dir, ".pinokio-temp", "requests")).length, 2, "an earlier request is kept")
|
|
33
|
-
})
|
|
22
|
+
assert.equal(/[\r\n]/.test(RequestFile.line("/home/brain/sessions/20261117-140502-aaaaaaaa/request.md")), false)
|
|
34
23
|
})
|
|
35
24
|
|
|
36
25
|
test("a new folder's request line points to the SPEC.md that holds the words", async () => {
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
"use strict"
|
|
2
|
+
|
|
3
|
+
// Where a shell's cleaned log is kept: logs/shell/cleaned/<path of its group>.
|
|
4
|
+
const test = require("node:test")
|
|
5
|
+
const assert = require("node:assert/strict")
|
|
6
|
+
const fs = require("fs")
|
|
7
|
+
const os = require("os")
|
|
8
|
+
const path = require("path")
|
|
9
|
+
const Kernel = require("../kernel")
|
|
10
|
+
|
|
11
|
+
const withKernel = async (run) => {
|
|
12
|
+
const root = fs.mkdtempSync(path.join(os.tmpdir(), "pinokio-shell-log-"))
|
|
13
|
+
// the home and what comes with Pinokio are two folders, neither inside the other
|
|
14
|
+
const kernel = {
|
|
15
|
+
homedir: path.join(root, "home"),
|
|
16
|
+
systemPath: (...parts) => path.resolve(root, "program", "pinokiod", "system", ...parts),
|
|
17
|
+
shellLogPath: Kernel.prototype.shellLogPath,
|
|
18
|
+
_log: Kernel.prototype._log,
|
|
19
|
+
clearLog: Kernel.prototype.clearLog
|
|
20
|
+
}
|
|
21
|
+
try {
|
|
22
|
+
await run(kernel, path.join(kernel.homedir, "logs", "shell"))
|
|
23
|
+
} finally {
|
|
24
|
+
fs.rmSync(root, { recursive: true, force: true })
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
const files = (dir) => fs.readdirSync(dir, { recursive: true, withFileTypes: true })
|
|
28
|
+
.filter((entry) => entry.isFile())
|
|
29
|
+
.map((entry) => path.relative(dir, path.join(entry.parentPath || entry.path, entry.name)).split(path.sep).join("/"))
|
|
30
|
+
.sort()
|
|
31
|
+
|
|
32
|
+
test("a built-in plugin's shell log is kept under cleaned, like every other script's", async () => {
|
|
33
|
+
await withKernel(async (kernel, logs) => {
|
|
34
|
+
const app = path.join(kernel.homedir, "api", "Speech", "start.js")
|
|
35
|
+
const plugin = path.join(kernel.homedir, "plugin", "mine", "pinokio.js")
|
|
36
|
+
const builtin = kernel.systemPath("plugin", "opencode", "pinokio.js")
|
|
37
|
+
|
|
38
|
+
assert.equal(kernel.shellLogPath(app), path.join("api", "Speech", "start.js"))
|
|
39
|
+
assert.equal(kernel.shellLogPath(plugin), path.join("plugin", "mine", "pinokio.js"))
|
|
40
|
+
assert.equal(kernel.shellLogPath(builtin), path.join("system", "plugin", "opencode", "pinokio.js"))
|
|
41
|
+
|
|
42
|
+
await kernel._log({ cleaned: "app\n" }, app, { index: 1 })
|
|
43
|
+
await kernel._log({ cleaned: "mine\n" }, plugin, { index: 1 })
|
|
44
|
+
await kernel._log({ cleaned: "one\n" }, builtin, { index: 1 })
|
|
45
|
+
await kernel._log({ cleaned: "two\n" }, builtin, { index: 2 })
|
|
46
|
+
await kernel._log({ cleaned: "no group\n" }, undefined, { index: 1 })
|
|
47
|
+
assert.deepEqual(files(logs), [
|
|
48
|
+
"cleaned/api/Speech/start.js.1.txt",
|
|
49
|
+
"cleaned/index.txt",
|
|
50
|
+
"cleaned/plugin/mine/pinokio.js.1.txt",
|
|
51
|
+
"cleaned/system/plugin/opencode/pinokio.js.1.txt",
|
|
52
|
+
"cleaned/system/plugin/opencode/pinokio.js.2.txt"
|
|
53
|
+
], "nothing lands outside cleaned")
|
|
54
|
+
assert.equal(fs.readFileSync(path.join(logs, "cleaned", "system", "plugin", "opencode", "pinokio.js.2.txt"), "utf8"), "two\n")
|
|
55
|
+
|
|
56
|
+
// its earlier logs are cleared where they are kept, and only its own
|
|
57
|
+
await kernel.clearLog(builtin)
|
|
58
|
+
assert.deepEqual(files(logs), [
|
|
59
|
+
"cleaned/api/Speech/start.js.1.txt",
|
|
60
|
+
"cleaned/index.txt",
|
|
61
|
+
"cleaned/plugin/mine/pinokio.js.1.txt"
|
|
62
|
+
])
|
|
63
|
+
})
|
|
64
|
+
})
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
"use strict"
|
|
2
|
+
|
|
3
|
+
// A step's shell takes a moment to start. When its run is stopped in that
|
|
4
|
+
// moment, the shell must not launch its command afterwards: nothing could stop
|
|
5
|
+
// it any more. The real Shells and Shell classes, with a real shell.
|
|
6
|
+
const test = require("node:test")
|
|
7
|
+
const assert = require("node:assert/strict")
|
|
8
|
+
const fs = require("fs")
|
|
9
|
+
const os = require("os")
|
|
10
|
+
const path = require("path")
|
|
11
|
+
const Shells = require("../kernel/shells")
|
|
12
|
+
|
|
13
|
+
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms))
|
|
14
|
+
|
|
15
|
+
// Starts one step of a run; `stopAfter` stops the run that many milliseconds
|
|
16
|
+
// in, the way stopping a run does: its shells are killed and it is no longer
|
|
17
|
+
// running. Gives back whether the step's command did its work.
|
|
18
|
+
const runStep = async ({ stopAfter, owned = true }) => {
|
|
19
|
+
const home = fs.mkdtempSync(path.join(os.tmpdir(), "pinokio-shell-start-"))
|
|
20
|
+
const marker = path.join(home, "the-command-ran")
|
|
21
|
+
fs.writeFileSync(path.join(home, "ENVIRONMENT"), "")
|
|
22
|
+
const kernel = {
|
|
23
|
+
platform: process.platform,
|
|
24
|
+
homedir: home,
|
|
25
|
+
envs: {},
|
|
26
|
+
bracketedPasteSupport: { bash: true },
|
|
27
|
+
connect: { keys: async () => null },
|
|
28
|
+
exists: async (target) => fs.promises.access(target).then(() => true, () => false),
|
|
29
|
+
which: () => null,
|
|
30
|
+
path: (...parts) => path.join(home, ...parts),
|
|
31
|
+
bin: { path: (...parts) => path.join(home, "bin", ...parts), envs: (env) => env || {}, activationCommands: () => [] },
|
|
32
|
+
memory: { local: {} },
|
|
33
|
+
log: () => {},
|
|
34
|
+
template: { render: (value) => value },
|
|
35
|
+
api: { running: { "run-1": true }, resolvePath: (cwd, target) => path.resolve(cwd, target), clearRunning(id) { delete this.running[id] } }
|
|
36
|
+
}
|
|
37
|
+
const shells = new Shells(kernel)
|
|
38
|
+
kernel.shell = shells
|
|
39
|
+
const params = { message: `sleep 0.4 && echo done > "${marker}"`, path: home }
|
|
40
|
+
if (owned) {
|
|
41
|
+
params.$parent = { id: "run-1", path: path.join(home, "pinokio.js"), uri: path.join(home, "pinokio.js"), action: "run" }
|
|
42
|
+
}
|
|
43
|
+
const step = shells.run(params, { group: "run-1", cwd: home }, () => {}).catch((error) => error)
|
|
44
|
+
try {
|
|
45
|
+
if (stopAfter === undefined) {
|
|
46
|
+
await step
|
|
47
|
+
} else {
|
|
48
|
+
await sleep(stopAfter)
|
|
49
|
+
shells.kill({ group: "run-1" })
|
|
50
|
+
delete kernel.api.running["run-1"]
|
|
51
|
+
await Promise.race([step, sleep(4000)])
|
|
52
|
+
await sleep(900) // longer than the command takes
|
|
53
|
+
}
|
|
54
|
+
return { ran: fs.existsSync(marker), registered: shells.shells.length }
|
|
55
|
+
} finally {
|
|
56
|
+
shells.kill({ group: "run-1" })
|
|
57
|
+
fs.rmSync(home, { recursive: true, force: true })
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
test("a run stopped while its shell is still starting does not launch the command afterwards", { skip: process.platform === "win32" }, async () => {
|
|
62
|
+
// left alone, the step runs its command
|
|
63
|
+
assert.equal((await runStep({})).ran, true)
|
|
64
|
+
// stopped before the shell has registered itself, and before it has launched
|
|
65
|
+
for (const stopAfter of [5, 60, 150]) {
|
|
66
|
+
assert.deepEqual(await runStep({ stopAfter }), { ran: false, registered: 0 }, `stopped ${stopAfter} ms in`)
|
|
67
|
+
}
|
|
68
|
+
// a shell that belongs to no run is not asked anything
|
|
69
|
+
assert.equal((await runStep({ owned: false })).ran, true)
|
|
70
|
+
})
|
|
@@ -2,7 +2,7 @@ const test = require('node:test')
|
|
|
2
2
|
const assert = require('node:assert/strict')
|
|
3
3
|
const Module = require('node:module')
|
|
4
4
|
|
|
5
|
-
async function runWithFakeShell ({ platform = 'win32', chunks, response, waitForBreak = false, on, extraParams = {} }) {
|
|
5
|
+
async function runWithFakeShell ({ platform = 'win32', chunks, response, waitForBreak = false, on, extraParams = {}, api = {} }) {
|
|
6
6
|
class FakeShell {
|
|
7
7
|
constructor () {
|
|
8
8
|
this.id = 'fake-shell'
|
|
@@ -69,7 +69,8 @@ async function runWithFakeShell ({ platform = 'win32', chunks, response, waitFor
|
|
|
69
69
|
bin: { envs: (env) => env || {} },
|
|
70
70
|
api: {
|
|
71
71
|
resolvePath: (_cwd, execPath) => execPath,
|
|
72
|
-
running: {}
|
|
72
|
+
running: {},
|
|
73
|
+
...api
|
|
73
74
|
},
|
|
74
75
|
which: () => null
|
|
75
76
|
}
|
|
@@ -163,3 +164,66 @@ test('shell.run does not return an error for a live match covered by break:false
|
|
|
163
164
|
|
|
164
165
|
assert.equal(result.error, undefined)
|
|
165
166
|
})
|
|
167
|
+
|
|
168
|
+
test("shell.run in a job does not fail on error words the agent prints", async () => {
|
|
169
|
+
const text = "I ran it and got Error: no such file, so I fixed the path.\nagent continues"
|
|
170
|
+
const job = await runWithFakeShell({
|
|
171
|
+
platform: 'darwin',
|
|
172
|
+
chunks: [`${text}\n`],
|
|
173
|
+
response: text,
|
|
174
|
+
extraParams: { $parent: { action: "job" } }
|
|
175
|
+
})
|
|
176
|
+
assert.equal(job.output.some((chunk) => chunk.includes('# input.event')), false)
|
|
177
|
+
assert.match(job.result.response, /agent continues/)
|
|
178
|
+
assert.equal(job.result.error, undefined)
|
|
179
|
+
|
|
180
|
+
// a handler the plugin wrote itself still applies
|
|
181
|
+
const own = await runWithFakeShell({
|
|
182
|
+
platform: 'darwin',
|
|
183
|
+
chunks: ["FATAL: out of credits\n"],
|
|
184
|
+
response: "FATAL: out of credits",
|
|
185
|
+
on: [{ event: "/FATAL:/", break: true }],
|
|
186
|
+
extraParams: { $parent: { action: "job" } }
|
|
187
|
+
})
|
|
188
|
+
assert.deepEqual(own.result.error, ["FATAL:"])
|
|
189
|
+
|
|
190
|
+
// every other action is as before
|
|
191
|
+
const run = await runWithFakeShell({
|
|
192
|
+
platform: 'darwin',
|
|
193
|
+
chunks: [`${text}\n`],
|
|
194
|
+
response: text,
|
|
195
|
+
extraParams: { $parent: { action: "run" } }
|
|
196
|
+
})
|
|
197
|
+
assert.deepEqual(run.result.error, ["Error:"])
|
|
198
|
+
})
|
|
199
|
+
|
|
200
|
+
test("an `on` handler cannot trigger a plugin's job, and a job's steps trigger nothing", async () => {
|
|
201
|
+
const queued = []
|
|
202
|
+
const steps = [{ method: "shell.run", params: { message: "work" } }]
|
|
203
|
+
const parent = { id: "r1", uri: "/tmp/plugin/pinokio.js", path: "/tmp/plugin/pinokio.js", action: "run", body: { job: steps, update: steps } }
|
|
204
|
+
const log = console.log
|
|
205
|
+
console.log = () => {}
|
|
206
|
+
try {
|
|
207
|
+
const run = (trigger, from = parent) => runWithFakeShell({
|
|
208
|
+
platform: 'darwin',
|
|
209
|
+
chunks: ["READY\n"],
|
|
210
|
+
response: "READY",
|
|
211
|
+
on: [{ event: "/READY/", trigger }],
|
|
212
|
+
extraParams: { $parent: from },
|
|
213
|
+
api: { running: { r1: true }, queue: (request) => queued.push(request.action) }
|
|
214
|
+
})
|
|
215
|
+
const job = await run("job")
|
|
216
|
+
assert.deepEqual(queued, [], "the job's steps are not queued")
|
|
217
|
+
assert.ok(job.output.some((chunk) => chunk.includes("A plugin's job is started by Pinokio's job runner only.")))
|
|
218
|
+
// a job's own steps trigger nothing: the job goes on and says why
|
|
219
|
+
const inJob = await run("update", { ...parent, action: "job" })
|
|
220
|
+
assert.deepEqual(queued, [])
|
|
221
|
+
assert.ok(inJob.output.some((chunk) => chunk.includes("A job's steps cannot trigger another action.")))
|
|
222
|
+
assert.match(inJob.result.response, /READY/)
|
|
223
|
+
// every other action is triggered as before
|
|
224
|
+
await run("update")
|
|
225
|
+
assert.deepEqual(queued, ["update"])
|
|
226
|
+
} finally {
|
|
227
|
+
console.log = log
|
|
228
|
+
}
|
|
229
|
+
})
|