Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
"build:plugin": "bun build src/source/v1.js --outfile=src/index.js --target=bun --format=esm --external=@opencode-ai/plugin/tool",
"build:plugin:npm": "npm run build:plugin",
"prepack": "node --check src/index.js",
"check": "node --check src/source/v1.js && node --check src/source/core/args.js && node --check src/source/core/state.js && node --check src/source/core/jobs.js && node --check src/source/core/process.js && node --check src/source/opencode/sdk.js && node --check src/source/opencode/session-context.js && node --check src/source/opencode/command-router.js && node --check src/source/opencode/goal-commands.js && node --check src/source/opencode/loop-commands.js && node --check src/source/opencode/loop-registration.js && node --check src/source/runtime/session-activity.js && node --check src/source/runtime/session-status.js && node --check src/source/runtime/compaction.js && node --check src/source/runtime/action-dispatch.js && node --check src/source/runtime/run-finalization.js && node --check src/source/runtime/executor.js && node --check src/source/runtime/scheduler.js && node --check src/source/runtime/goal-runtime.js && node --check src/source/runtime/goal-policy.js && node --check src/source/runtime/goal-steering.js && node --check src/source/runtime/job-workspace.js && node --check src/source/opencode2/prompt-runtime.js && node --check src/source/opencode2/diagnostics.js && node --check src/source/opencode2/logging.js && node --check src/source/legacy-v1.js && node --check src/index.js && node --check scripts/install-node.mjs && node --check scripts/install-with-goals.mjs && node --check scripts/loopd.mjs && node --check scripts/install-test.mjs && node --check scripts/goal-companion-test.mjs && node --check scripts/loopd-test.mjs && node --check scripts/smoke-test.mjs && node --check scripts/host-adapter-contract-test.mjs && node --check scripts/command-router-test.mjs && node --check scripts/goal-command-handlers-test.mjs && node --check scripts/loop-command-handlers-test.mjs && node --check scripts/loop-registration-test.mjs && node --check scripts/session-activity-test.mjs && node --check scripts/session-status-test.mjs && node --check scripts/compaction-runtime-test.mjs && node --check scripts/executor-runtime-test.mjs && node --check scripts/scheduler-runtime-test.mjs && node --check scripts/goal-runtime-test.mjs && node --check scripts/goal-policy-test.mjs && node --check scripts/goal-steering-test.mjs && node --check scripts/job-workspace-test.mjs && node --check scripts/v2-prompt-runtime-test.mjs && node --check scripts/v2-prompt-interval-test.mjs && node --check scripts/v2-command-runtime-test.mjs && node --check scripts/v2-command-adapter-test.mjs && node --check scripts/v2-diagnostics-test.mjs && node --check scripts/v2-logging-test.mjs && node --check scripts/comprehensive-watchdog.mjs && node --check scripts/comprehensive-test.mjs && node --check scripts/host-loop-canary.mjs && node --check scripts/host-goal-steering-canary.mjs && node --check scripts/publish-workflow-test.mjs",
"test": "node scripts/publish-workflow-test.mjs && node scripts/command-router-test.mjs && node scripts/goal-command-handlers-test.mjs && node scripts/loop-command-handlers-test.mjs && node scripts/loop-registration-test.mjs && node scripts/session-activity-test.mjs && node scripts/session-status-test.mjs && node scripts/compaction-runtime-test.mjs && node scripts/action-dispatch-test.mjs && node scripts/run-finalization-test.mjs && node scripts/executor-runtime-test.mjs && node scripts/scheduler-runtime-test.mjs && node scripts/goal-runtime-test.mjs && node scripts/goal-policy-test.mjs && node scripts/goal-steering-test.mjs && node scripts/job-workspace-test.mjs && node scripts/v2-prompt-runtime-test.mjs && node scripts/v2-prompt-interval-test.mjs && node scripts/v2-command-runtime-test.mjs && node scripts/v2-command-adapter-test.mjs && node scripts/v2-diagnostics-test.mjs && node scripts/v2-logging-test.mjs && node scripts/install-test.mjs && node scripts/goal-companion-test.mjs && node scripts/loopd-test.mjs && node scripts/smoke-test.mjs && node scripts/host-adapter-contract-test.mjs && node scripts/comprehensive-watchdog.mjs",
"check": "node --check src/source/v1.js && node --check src/source/core/args.js && node --check src/source/core/state.js && node --check src/source/core/jobs.js && node --check src/source/core/process.js && node --check src/source/opencode/sdk.js && node --check src/source/opencode/session-context.js && node --check src/source/opencode/command-router.js && node --check src/source/opencode/goal-commands.js && node --check src/source/opencode/loop-commands.js && node --check src/source/opencode/loop-registration.js && node --check src/source/runtime/session-activity.js && node --check src/source/runtime/session-status.js && node --check src/source/runtime/compaction.js && node --check src/source/runtime/action-dispatch.js && node --check src/source/runtime/run-finalization.js && node --check src/source/runtime/run-admission.js && node --check src/source/runtime/executor.js && node --check src/source/runtime/scheduler.js && node --check src/source/runtime/goal-runtime.js && node --check src/source/runtime/goal-policy.js && node --check src/source/runtime/goal-steering.js && node --check src/source/runtime/job-workspace.js && node --check src/source/opencode2/prompt-runtime.js && node --check src/source/opencode2/diagnostics.js && node --check src/source/opencode2/logging.js && node --check src/source/legacy-v1.js && node --check src/index.js && node --check scripts/install-node.mjs && node --check scripts/install-with-goals.mjs && node --check scripts/loopd.mjs && node --check scripts/install-test.mjs && node --check scripts/goal-companion-test.mjs && node --check scripts/loopd-test.mjs && node --check scripts/smoke-test.mjs && node --check scripts/host-adapter-contract-test.mjs && node --check scripts/command-router-test.mjs && node --check scripts/goal-command-handlers-test.mjs && node --check scripts/loop-command-handlers-test.mjs && node --check scripts/loop-registration-test.mjs && node --check scripts/session-activity-test.mjs && node --check scripts/session-status-test.mjs && node --check scripts/compaction-runtime-test.mjs && node --check scripts/executor-runtime-test.mjs && node --check scripts/scheduler-runtime-test.mjs && node --check scripts/goal-runtime-test.mjs && node --check scripts/goal-policy-test.mjs && node --check scripts/goal-steering-test.mjs && node --check scripts/job-workspace-test.mjs && node --check scripts/v2-prompt-runtime-test.mjs && node --check scripts/v2-prompt-interval-test.mjs && node --check scripts/v2-command-runtime-test.mjs && node --check scripts/v2-command-adapter-test.mjs && node --check scripts/v2-diagnostics-test.mjs && node --check scripts/v2-logging-test.mjs && node --check scripts/comprehensive-watchdog.mjs && node --check scripts/comprehensive-test.mjs && node --check scripts/host-loop-canary.mjs && node --check scripts/host-goal-steering-canary.mjs && node --check scripts/publish-workflow-test.mjs",
"test": "node scripts/publish-workflow-test.mjs && node scripts/command-router-test.mjs && node scripts/goal-command-handlers-test.mjs && node scripts/loop-command-handlers-test.mjs && node scripts/loop-registration-test.mjs && node scripts/session-activity-test.mjs && node scripts/session-status-test.mjs && node scripts/compaction-runtime-test.mjs && node scripts/action-dispatch-test.mjs && node scripts/run-finalization-test.mjs && node scripts/run-admission-test.mjs && node scripts/executor-runtime-test.mjs && node scripts/scheduler-runtime-test.mjs && node scripts/goal-runtime-test.mjs && node scripts/goal-policy-test.mjs && node scripts/goal-steering-test.mjs && node scripts/job-workspace-test.mjs && node scripts/v2-prompt-runtime-test.mjs && node scripts/v2-prompt-interval-test.mjs && node scripts/v2-command-runtime-test.mjs && node scripts/v2-command-adapter-test.mjs && node scripts/v2-diagnostics-test.mjs && node scripts/v2-logging-test.mjs && node scripts/install-test.mjs && node scripts/goal-companion-test.mjs && node scripts/loopd-test.mjs && node scripts/smoke-test.mjs && node scripts/host-adapter-contract-test.mjs && node scripts/comprehensive-watchdog.mjs",
"canary:host": "node scripts/host-loop-canary.mjs && node scripts/host-goal-steering-canary.mjs",
"install:global": "node scripts/install-with-goals.mjs",
"pack:zip": "node scripts/make-zip.mjs"
Expand Down
133 changes: 133 additions & 0 deletions scripts/run-admission-test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
import assert from "node:assert/strict"
import path from "node:path"
import { createRunAdmissionRuntime } from "../src/source/runtime/run-admission.js"

assert.throws(() => createRunAdmissionRuntime({}), /untilReached/)
assert.throws(() => createRunAdmissionRuntime({ untilReached: async () => false }), /scheduleDueWork/)

let clock = 1_000_000
const writes = []
const logs = []
const shellCalls = []
const notifications = []
const toasts = []
const schedules = []
const stopFiles = new Set()
const untilJobs = new Set()
const normalizePath = (value) => String(value).replace(/\\/g, "/")

const runtime = createRunAdmissionRuntime({
untilReached: async (_directory, job) => untilJobs.has(job.id),
scheduleDueWork: async (...args) => { schedules.push(args) },
now: () => clock,
pathExists: async (target) => stopFiles.has(normalizePath(target)),
writeState: async (...args) => { writes.push(args) },
appendLoopLog: async (...args) => { logs.push(args) },
runShellCommand: async (command) => {
shellCalls.push(command)
if (command.includes("fail")) return { code: 7, stdout: "bad", stderr: "preflight error" }
return { code: 0, stdout: "ok", stderr: "" }
},
notifyJob: async (...args) => { notifications.push(args) },
toast: async (...args) => { toasts.push(args) },
dangerousShell: (command) => command.includes("danger"),
})

const dueState = {
jobs: [
{ id: "disabled", enabled: false, intervalMs: 0 },
{ id: "paused", enabled: true, paused: true, intervalMs: 0 },
{ id: "maxed", enabled: true, maxRuns: 1, runCount: 1, intervalMs: 0 },
{ id: "goal-done", kind: "goal", goalStatus: "completed", enabled: true, intervalMs: 0 },
{ id: "watch-no", enabled: true, watchPaths: ["x"], watchTriggered: false, intervalMs: 0 },
{ id: "watch-yes", enabled: true, watchPaths: ["x"], watchTriggered: true, intervalMs: 99_999 },
{ id: "interval", enabled: true, intervalMs: 10_000, lastRunAt: clock - 20_000 },
{ id: "run-now", enabled: true, intervalMs: 999_999, lastRunAt: clock, runNowRequestedAt: clock },
],
}
assert.deepEqual(runtime.dueJobs(dueState).map((job) => job.id), ["run-now", "watch-yes", "interval"])
assert.deepEqual(runtime.dueJobs(dueState, true).map((job) => job.id), ["run-now", "watch-no", "watch-yes", "interval"])

const expired = {
id: "expired",
name: "expired-job",
enabled: true,
createdAt: new Date(clock - 20_000).toISOString(),
maxRuntimeMs: 10_000,
}
const expiredState = { jobs: [expired, { id: "keep", enabled: true }] }
const expiredResult = await runtime.admitJob("/repo", {}, "session-expired", expiredState, expired)
assert.deepEqual(expiredResult, { admitted: false, reason: "max_runtime_reached" })
assert.deepEqual(expiredState.jobs.map((job) => job.id), ["keep"])
assert.equal(notifications.at(-1)[2], "max_runtime_reached")
assert.equal(logs.at(-1)[1], "max-runtime")
assert.match(toasts.at(-1)[1], /--max-runtime/)
assert.equal(schedules.at(-1)[2], "session-expired")

stopFiles.add(normalizePath(path.resolve("/repo", "STOP")))
const stopped = { id: "stopped", enabled: true, stopFile: "STOP" }
const stoppedState = { jobs: [stopped] }
const stoppedResult = await runtime.admitJob("/repo", {}, "session-stop", stoppedState, stopped)
assert.deepEqual(stoppedResult, { admitted: false, reason: "stop_file" })
assert.equal(stoppedState.jobs.length, 0)
assert.equal(notifications.at(-1)[2], "stop_file")
assert.match(toasts.at(-1)[1], /--stop-file/)

untilJobs.add("until-job")
const untilJob = { id: "until-job", enabled: true, until: "DONE" }
const untilState = { jobs: [untilJob] }
const untilResult = await runtime.admitJob("/repo", {}, "session-until", untilState, untilJob)
assert.deepEqual(untilResult, { admitted: false, reason: "until_reached" })
assert.equal(untilState.jobs.length, 0)
assert.equal(notifications.at(-1)[2], "until_reached")
assert.match(toasts.at(-1)[1], /--until/)

const blocked = {
id: "blocked-preflight",
enabled: true,
safe: true,
preflightCommand: "danger --all",
runNowRequestedAt: clock,
}
const blockedState = { jobs: [blocked] }
const shellCountBeforeBlock = shellCalls.length
const blockedResult = await runtime.admitJob("/repo", {}, "session-blocked", blockedState, blocked)
assert.deepEqual(blockedResult, { admitted: false, reason: "preflight_blocked" })
assert.equal(shellCalls.length, shellCountBeforeBlock)
assert.equal(blocked.paused, true)
assert.equal(blocked.runNowRequestedAt, undefined)
assert.equal(notifications.at(-1)[2], "preflight_blocked")
assert.equal(writes.at(-1)[2], blockedState)

const failed = {
id: "failed-preflight",
enabled: true,
preflightCommand: "preflight-fail",
runNowRequestedAt: clock,
failureCount: 2,
}
const failedState = { jobs: [failed] }
const failedResult = await runtime.admitJob("/repo", {}, "session-failed", failedState, failed)
assert.deepEqual(failedResult, { admitted: false, reason: "preflight_failed" })
assert.equal(failed.paused, true)
assert.equal(failed.failureCount, 3)
assert.equal(failed.runNowRequestedAt, undefined)
assert.match(failed.lastPreflightFailure, /exit=7/)
assert.match(failed.lastPreflightFailure, /preflight error/)
assert.equal(logs.findLast((entry) => entry[1] === "preflight")?.[2].code, 7)
assert.equal(notifications.at(-1)[2], "preflight_failed")

const admitted = {
id: "admitted",
enabled: true,
preflightCommand: "preflight-ok",
runNowRequestedAt: clock,
}
const admittedState = { jobs: [admitted] }
const admittedResult = await runtime.admitJob("/repo", {}, "session-admitted", admittedState, admitted)
assert.deepEqual(admittedResult, { admitted: true, job: admitted, runNowRequested: true })
assert.equal(admitted.paused, undefined)
assert.equal(admitted.runNowRequestedAt, clock)
assert.equal(logs.findLast((entry) => entry[1] === "preflight")?.[2].code, 0)

console.log("run admission runtime tests passed")
Loading