pacework 0.0.0-stage → 1.0.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.
@@ -0,0 +1,152 @@
1
+ import fs from "node:fs"
2
+ import os from "node:os"
3
+ import path from "node:path"
4
+
5
+ const CURRENT_DIR = path.join(os.homedir(), ".pacework")
6
+ const LEGACY_DIR = path.join(os.homedir(), ".kentral")
7
+ /**
8
+ * True on machines set up before the Kentral → pacework rename. Their config,
9
+ * installed copy and background service live under ~/.kentral, so the runner
10
+ * keeps using it rather than stranding a running service.
11
+ */
12
+ export const USES_LEGACY_DIR = !fs.existsSync(CURRENT_DIR) && fs.existsSync(LEGACY_DIR)
13
+ export const PACEWORK_DIR = USES_LEGACY_DIR ? LEGACY_DIR : CURRENT_DIR
14
+ export const STATE_DIR = path.join(PACEWORK_DIR, "runner")
15
+ export const PID_FILE = path.join(STATE_DIR, "runner.pid")
16
+ export const LOG_FILE = path.join(STATE_DIR, "runner.log")
17
+ export const CMD_FILE = path.join(STATE_DIR, "runner.cmd.json")
18
+ export const JOBS_FILE = path.join(STATE_DIR, "jobs.jsonl")
19
+ export const CONFIG_FILE = path.join(PACEWORK_DIR, "config.json")
20
+
21
+ export function ensureStateDir() {
22
+ fs.mkdirSync(STATE_DIR, { recursive: true })
23
+ }
24
+
25
+ export function readPid() {
26
+ try {
27
+ const pid = Number.parseInt(fs.readFileSync(PID_FILE, "utf8").trim(), 10)
28
+ return Number.isNaN(pid) ? null : pid
29
+ } catch {
30
+ return null
31
+ }
32
+ }
33
+
34
+ export function isRunning(pid) {
35
+ if (!pid) return false
36
+ try {
37
+ process.kill(pid, 0)
38
+ return true
39
+ } catch {
40
+ return false
41
+ }
42
+ }
43
+
44
+ export function clearPid() {
45
+ try { fs.unlinkSync(PID_FILE) } catch {}
46
+ }
47
+
48
+ export function readConfig() {
49
+ try { return JSON.parse(fs.readFileSync(CONFIG_FILE, "utf8")) } catch { return {} }
50
+ }
51
+
52
+ export function writeConfig(data) {
53
+ fs.mkdirSync(PACEWORK_DIR, { recursive: true })
54
+ fs.writeFileSync(CONFIG_FILE, JSON.stringify(data, null, 2), { encoding: "utf8", mode: 0o600 })
55
+ try { fs.chmodSync(CONFIG_FILE, 0o600) } catch {}
56
+ }
57
+
58
+ export function readRunnerSnapshot({ logLines = 120 } = {}) {
59
+ const config = readConfig()
60
+ const pid = readPid()
61
+ const running = isRunning(pid)
62
+ const logs = readLogTail(logLines)
63
+ const jobs = summarizeJobs(logs.entries)
64
+
65
+ return {
66
+ configured: Boolean(config.apiKey && config.workspaceId),
67
+ running,
68
+ pid: running ? pid : null,
69
+ workspace: config.workspaceSlug ?? config.workspaceId ?? null,
70
+ executor: config.executor ?? "codex",
71
+ runnerName: config.runnerName ?? null,
72
+ repoRoot: config.repoRoot ?? null,
73
+ appUrl: config.appUrl ?? "https://pacework.io",
74
+ startedAt: readStartedAt(),
75
+ jobs,
76
+ logs: logs.entries,
77
+ logFile: LOG_FILE,
78
+ }
79
+ }
80
+
81
+ export function readLogTail(limit = 120) {
82
+ try {
83
+ const raw = fs.readFileSync(LOG_FILE, "utf8")
84
+ const lines = raw.split("\n").filter(Boolean).slice(-Math.max(1, limit))
85
+ return { entries: lines.map(parseLogLine), exists: true }
86
+ } catch {
87
+ return { entries: [], exists: false }
88
+ }
89
+ }
90
+
91
+ function parseLogLine(raw) {
92
+ try {
93
+ const entry = JSON.parse(raw)
94
+ return {
95
+ ts: entry.ts ?? null,
96
+ level: entry.level ?? "info",
97
+ event: entry.event ?? null,
98
+ message: entry.msg ?? "",
99
+ runId: entry.run_id ?? entry.meta?.run_id ?? null,
100
+ taskId: entry.task_id ?? entry.meta?.task_id ?? null,
101
+ taskIdentifier: entry.meta?.task_identifier ?? null,
102
+ taskTitle: entry.meta?.task_title ?? null,
103
+ durationMs: entry.meta?.duration_ms ?? null,
104
+ }
105
+ } catch {
106
+ return { ts: null, level: "info", event: null, message: raw }
107
+ }
108
+ }
109
+
110
+ function summarizeJobs(entries) {
111
+ const jobs = new Map()
112
+ for (const entry of entries) {
113
+ if (!entry.runId || !entry.event?.startsWith("runner.job.")) continue
114
+ const existing = jobs.get(entry.runId) ?? {
115
+ id: entry.runId,
116
+ identifier: entry.taskIdentifier,
117
+ title: entry.taskTitle,
118
+ status: "pending",
119
+ startedAt: null,
120
+ finishedAt: null,
121
+ durationMs: null,
122
+ message: null,
123
+ }
124
+ if (entry.event === "runner.job.start") {
125
+ existing.status = "running"
126
+ existing.startedAt = entry.ts
127
+ } else if (entry.event === "runner.job.complete") {
128
+ existing.status = "completed"
129
+ existing.finishedAt = entry.ts
130
+ existing.durationMs = entry.durationMs
131
+ } else if (entry.event === "runner.job.error") {
132
+ existing.status = "error"
133
+ existing.finishedAt = entry.ts
134
+ existing.message = entry.message
135
+ } else if (entry.event === "runner.job.cancel") {
136
+ existing.status = "canceled"
137
+ existing.finishedAt = entry.ts
138
+ }
139
+ existing.identifier ||= entry.taskIdentifier
140
+ existing.title ||= entry.taskTitle
141
+ jobs.set(entry.runId, existing)
142
+ }
143
+ return [...jobs.values()].slice(-20).reverse()
144
+ }
145
+
146
+ function readStartedAt() {
147
+ try {
148
+ return JSON.parse(fs.readFileSync(CMD_FILE, "utf8")).startedAt ?? null
149
+ } catch {
150
+ return null
151
+ }
152
+ }
@@ -0,0 +1,202 @@
1
+ import { spawn, execFileSync } from "node:child_process"
2
+ import fs from "node:fs"
3
+ import net from "node:net"
4
+ import path from "node:path"
5
+
6
+ /**
7
+ * What a run's checkout needs before an agent can build and test in it: the
8
+ * repository's dependencies, the services its tests talk to, and its secrets.
9
+ *
10
+ * Set per repository in pacework (Runners → Cloud runs → Environment). A cloud
11
+ * job starts from a bare machine, so there it always applies; on a person's
12
+ * machine only when the repository asks for it, since that machine usually has
13
+ * its own setup already.
14
+ */
15
+
16
+ /** Services a repository can ask for, started as containers on the job's machine. */
17
+ export const SERVICES = {
18
+ postgres: {
19
+ image: "postgres:16",
20
+ port: 5432,
21
+ env: { POSTGRES_PASSWORD: "postgres", POSTGRES_DB: "app" },
22
+ exports: {
23
+ DATABASE_URL: "postgresql://postgres:postgres@localhost:5432/app",
24
+ PGHOST: "localhost",
25
+ PGPORT: "5432",
26
+ PGUSER: "postgres",
27
+ PGPASSWORD: "postgres",
28
+ PGDATABASE: "app",
29
+ },
30
+ },
31
+ mysql: {
32
+ image: "mysql:8",
33
+ port: 3306,
34
+ env: { MYSQL_ROOT_PASSWORD: "mysql", MYSQL_DATABASE: "app" },
35
+ exports: { MYSQL_URL: "mysql://root:mysql@localhost:3306/app" },
36
+ },
37
+ redis: {
38
+ image: "redis:7",
39
+ port: 6379,
40
+ env: {},
41
+ exports: { REDIS_URL: "redis://localhost:6379" },
42
+ },
43
+ mongodb: {
44
+ image: "mongo:7",
45
+ port: 27017,
46
+ env: {},
47
+ exports: { MONGODB_URI: "mongodb://localhost:27017/app", MONGO_URL: "mongodb://localhost:27017/app" },
48
+ },
49
+ }
50
+
51
+ /** The install command a repository's lockfile implies, or null when there is none to run. */
52
+ export function detectSetupCommand(dir) {
53
+ const has = (file) => fs.existsSync(path.join(dir, file))
54
+ if (has("pnpm-lock.yaml")) return "corepack enable && pnpm install --frozen-lockfile"
55
+ if (has("yarn.lock")) return "corepack enable && yarn install --frozen-lockfile"
56
+ if (has("bun.lockb") || has("bun.lock")) return "npx -y bun install --frozen-lockfile"
57
+ if (has("package-lock.json")) return "npm ci"
58
+ if (has("package.json")) return "npm install"
59
+ if (has("uv.lock")) return "pip install uv && uv sync"
60
+ if (has("poetry.lock")) return "pip install poetry && poetry install"
61
+ if (has("requirements.txt")) return "pip install -r requirements.txt"
62
+ if (has("Gemfile.lock")) return "bundle install"
63
+ if (has("go.mod")) return "go mod download"
64
+ if (has("Cargo.lock")) return "cargo fetch"
65
+ return null
66
+ }
67
+
68
+ /**
69
+ * Secrets a cloud job passes to its agent: the repository's GitHub Actions
70
+ * secrets named PACEWORK_ENV_<NAME> (or KENTRAL_ENV_<NAME>, from before the
71
+ * rename), given to the agent as <NAME>. The workflow
72
+ * hands over all of the repository's secrets; only these are kept.
73
+ */
74
+ export function agentSecretsFrom(allSecretsJson) {
75
+ let all = {}
76
+ try {
77
+ all = JSON.parse(allSecretsJson || "{}") || {}
78
+ } catch {
79
+ return {}
80
+ }
81
+ const kept = {}
82
+ for (const [name, value] of Object.entries(all)) {
83
+ const prefix = ["PACEWORK_ENV_", "KENTRAL_ENV_"].find((candidate) => name.startsWith(candidate))
84
+ if (prefix && name.length > prefix.length && typeof value === "string") {
85
+ // The current prefix wins when a repository has both.
86
+ if (prefix === "PACEWORK_ENV_" || !(name.slice(prefix.length) in kept)) kept[name.slice(prefix.length)] = value
87
+ }
88
+ }
89
+ return kept
90
+ }
91
+
92
+ function portOpen(port) {
93
+ return new Promise((resolve) => {
94
+ const socket = net.connect({ host: "127.0.0.1", port }, () => {
95
+ socket.end()
96
+ resolve(true)
97
+ })
98
+ socket.on("error", () => resolve(false))
99
+ socket.setTimeout(1000, () => {
100
+ socket.destroy()
101
+ resolve(false)
102
+ })
103
+ })
104
+ }
105
+
106
+ const started = new Map()
107
+
108
+ /**
109
+ * Start the named services once per process and return the variables that
110
+ * point at them. A service that does not come up is reported, not fatal: the
111
+ * agent can still work, and says what it could not verify.
112
+ */
113
+ export async function startServices(names, log = () => {}) {
114
+ const env = {}
115
+ const problems = []
116
+ for (const name of names) {
117
+ const service = SERVICES[name]
118
+ if (!service) {
119
+ problems.push(`unknown service "${name}"`)
120
+ continue
121
+ }
122
+ if (!started.has(name)) {
123
+ const ready = (async () => {
124
+ if (await portOpen(service.port)) return true
125
+ log(`> starting ${name} (${service.image})`)
126
+ const envArgs = Object.entries(service.env).flatMap(([key, value]) => ["-e", `${key}=${value}`])
127
+ execFileSync("docker", ["run", "-d", "--rm", "--name", `pacework-${name}`, "-p", `${service.port}:${service.port}`, ...envArgs, service.image], { stdio: "ignore" })
128
+ const deadline = Date.now() + 90_000
129
+ while (Date.now() < deadline) {
130
+ if (await portOpen(service.port)) {
131
+ // Databases accept connections a moment before they accept queries.
132
+ await new Promise((resolve) => setTimeout(resolve, 3000))
133
+ return true
134
+ }
135
+ await new Promise((resolve) => setTimeout(resolve, 1000))
136
+ }
137
+ return false
138
+ })().catch(() => false)
139
+ started.set(name, ready)
140
+ }
141
+ if (await started.get(name)) Object.assign(env, service.exports)
142
+ else problems.push(`${name} did not start`)
143
+ }
144
+ return { env, problems }
145
+ }
146
+
147
+ /** Run the setup command in the checkout. Resolves with whether it worked and the end of its output. */
148
+ export function runSetupCommand(command, cwd, env, { timeoutMs = 20 * 60_000, log = () => {} } = {}) {
149
+ return new Promise((resolve) => {
150
+ log(`> setting up the environment: ${command}`)
151
+ const child = spawn(process.platform === "win32" ? "cmd" : "bash", process.platform === "win32" ? ["/c", command] : ["-lc", command], {
152
+ cwd,
153
+ env,
154
+ stdio: ["ignore", "pipe", "pipe"],
155
+ })
156
+ let tail = ""
157
+ const keep = (chunk) => {
158
+ tail = `${tail}${chunk.toString()}`.slice(-4000)
159
+ }
160
+ child.stdout.on("data", keep)
161
+ child.stderr.on("data", keep)
162
+ const timer = setTimeout(() => {
163
+ child.kill("SIGKILL")
164
+ tail += `\n(setup stopped after ${Math.round(timeoutMs / 60_000)} minutes)`
165
+ }, timeoutMs)
166
+ child.on("close", (code) => {
167
+ clearTimeout(timer)
168
+ resolve({ ok: code === 0, tail: tail.trim() })
169
+ })
170
+ child.on("error", (error) => {
171
+ clearTimeout(timer)
172
+ resolve({ ok: false, tail: String(error) })
173
+ })
174
+ })
175
+ }
176
+
177
+ /**
178
+ * Prepare a run's checkout. Returns the variables the agent gets and, when
179
+ * something failed, a note for its prompt so it does not mistake a missing
180
+ * dependency for a bug in the code.
181
+ */
182
+ export async function prepareRunEnvironment({ environment, checkoutPath, isCloud, secrets = {}, baseEnv = process.env, log = () => {} }) {
183
+ const config = environment || {}
184
+ const applies = isCloud || config.run_locally === true
185
+ if (!applies) return { env: {}, note: null }
186
+
187
+ const notes = []
188
+ const services = isCloud && Array.isArray(config.services) ? config.services : []
189
+ const { env: serviceEnv, problems } = services.length > 0 ? await startServices(services, log) : { env: {}, problems: [] }
190
+ if (problems.length > 0) notes.push(`Services: ${problems.join("; ")}.`)
191
+
192
+ const env = { ...serviceEnv, ...secrets }
193
+ const command = (typeof config.setup_command === "string" && config.setup_command.trim()) || detectSetupCommand(checkoutPath)
194
+ if (command) {
195
+ const result = await runSetupCommand(command, checkoutPath, { ...baseEnv, ...env }, { log })
196
+ if (!result.ok) {
197
+ notes.push(`The setup command \`${command}\` failed, so dependencies may be missing. Its output ended with:\n${result.tail.slice(-1500)}`)
198
+ }
199
+ }
200
+
201
+ return { env, note: notes.length > 0 ? notes.join("\n") : null }
202
+ }
@@ -0,0 +1,13 @@
1
+ /**
2
+ * The product was renamed from Kentral to pacework. Workflows and services set
3
+ * up before the rename still pass KENTRAL_* variables, so each one moves to its
4
+ * PACEWORK_* name when that is not set. Moved, not copied: KENTRAL_SECRETS holds
5
+ * every repository secret and must not ride along into the agent's environment.
6
+ * Imported first by every entry point.
7
+ */
8
+ for (const key of Object.keys(process.env)) {
9
+ if (!key.startsWith("KENTRAL_")) continue
10
+ const current = `PACEWORK_${key.slice("KENTRAL_".length)}`
11
+ if (process.env[current] === undefined) process.env[current] = process.env[key]
12
+ delete process.env[key]
13
+ }
package/lib/logger.mjs ADDED
@@ -0,0 +1,233 @@
1
+ const DEFAULT_SCHEMA_VERSION = 1
2
+ const DEFAULT_SERVICE = "script"
3
+ const LOG_LEVEL_PRIORITY = {
4
+ trace: 10,
5
+ debug: 20,
6
+ info: 30,
7
+ warn: 40,
8
+ error: 50,
9
+ fatal: 60,
10
+ }
11
+
12
+ const REDACTED_KEYS = new Set([
13
+ "authorization",
14
+ "cookie",
15
+ "set-cookie",
16
+ "x-pacework-internal-secret",
17
+ "x-kentral-internal-secret",
18
+ "x-dispatch-secret",
19
+ "access_token",
20
+ "refresh_token",
21
+ "raw_api_key",
22
+ "secret",
23
+ "token",
24
+ ])
25
+
26
+ function mergeMeta(baseMeta, extraMeta) {
27
+ if (!baseMeta && !extraMeta) {
28
+ return undefined
29
+ }
30
+
31
+ return {
32
+ ...(baseMeta || {}),
33
+ ...(extraMeta || {}),
34
+ }
35
+ }
36
+
37
+ function pruneNullish(value) {
38
+ if (Array.isArray(value)) {
39
+ return value
40
+ .map((entry) => pruneNullish(entry))
41
+ .filter((entry) => entry !== undefined)
42
+ }
43
+
44
+ if (!value || typeof value !== "object") {
45
+ return value ?? undefined
46
+ }
47
+
48
+ const entries = Object.entries(value)
49
+ .map(([key, entryValue]) => [key, pruneNullish(entryValue)])
50
+ .filter(([, entryValue]) => entryValue !== undefined)
51
+
52
+ if (entries.length === 0) {
53
+ return undefined
54
+ }
55
+
56
+ return Object.fromEntries(entries)
57
+ }
58
+
59
+ function redactForLogs(value) {
60
+ if (Array.isArray(value)) {
61
+ return value.map(redactForLogs)
62
+ }
63
+
64
+ if (!value || typeof value !== "object") {
65
+ return value
66
+ }
67
+
68
+ return Object.fromEntries(
69
+ Object.entries(value).map(([key, entryValue]) => {
70
+ if (REDACTED_KEYS.has(key.toLowerCase())) {
71
+ return [key, "[redacted]"]
72
+ }
73
+
74
+ return [key, redactForLogs(entryValue)]
75
+ }),
76
+ )
77
+ }
78
+
79
+ function normalizeEnvironment(value) {
80
+ if (value === "production" || value === "test") {
81
+ return value
82
+ }
83
+
84
+ return "development"
85
+ }
86
+
87
+ function normalizeMinLevel(value) {
88
+ if (value && LOG_LEVEL_PRIORITY[value]) {
89
+ return value
90
+ }
91
+
92
+ return "info"
93
+ }
94
+
95
+ function normalizeSchemaVersion(value) {
96
+ const schemaVersion = typeof value === "number" ? value : Number(value)
97
+
98
+ if (Number.isInteger(schemaVersion) && schemaVersion > 0) {
99
+ return schemaVersion
100
+ }
101
+
102
+ return DEFAULT_SCHEMA_VERSION
103
+ }
104
+
105
+ function parseBoolean(value) {
106
+ if (typeof value === "boolean") {
107
+ return value
108
+ }
109
+
110
+ if (typeof value !== "string") {
111
+ return undefined
112
+ }
113
+
114
+ const normalizedValue = value.trim().toLowerCase()
115
+ if (["1", "true", "yes", "on"].includes(normalizedValue)) {
116
+ return true
117
+ }
118
+
119
+ if (["0", "false", "no", "off"].includes(normalizedValue)) {
120
+ return false
121
+ }
122
+
123
+ return undefined
124
+ }
125
+
126
+ function resolveLoggerRuntimeOptions(input = process.env) {
127
+ const env = normalizeEnvironment(input.NODE_ENV)
128
+
129
+ return {
130
+ env,
131
+ schemaVersion: normalizeSchemaVersion(input.LOG_SCHEMA_VERSION),
132
+ includeStacks: parseBoolean(input.LOG_INCLUDE_STACKS) ?? env !== "test",
133
+ minLevel: normalizeMinLevel(input.LOG_LEVEL),
134
+ }
135
+ }
136
+
137
+ function normalizeError(error, options) {
138
+ if (error instanceof Error) {
139
+ const cause = error.cause
140
+ return pruneNullish({
141
+ error_name: error.name,
142
+ error_message: error.message,
143
+ error_code: typeof error.code === "string" || typeof error.code === "number" ? error.code : undefined,
144
+ error_stack: options.includeStacks ? error.stack : undefined,
145
+ cause: typeof cause === "string"
146
+ ? cause
147
+ : cause instanceof Error
148
+ ? cause.message
149
+ : undefined,
150
+ }) || {}
151
+ }
152
+
153
+ if (typeof error === "string") {
154
+ return { error_message: error }
155
+ }
156
+
157
+ if (error && typeof error === "object") {
158
+ return pruneNullish({
159
+ error_name: typeof error.name === "string" ? error.name : undefined,
160
+ error_message: typeof error.message === "string" ? error.message : JSON.stringify(error),
161
+ error_code: typeof error.code === "string" || typeof error.code === "number" ? error.code : undefined,
162
+ }) || {}
163
+ }
164
+
165
+ return {}
166
+ }
167
+
168
+ function shouldLog(level, minLevel) {
169
+ return LOG_LEVEL_PRIORITY[level] >= LOG_LEVEL_PRIORITY[minLevel]
170
+ }
171
+
172
+ function getConsoleMethod(level) {
173
+ if (level === "error" || level === "fatal") {
174
+ return console.error
175
+ }
176
+
177
+ if (level === "warn") {
178
+ return console.warn
179
+ }
180
+
181
+ if (level === "debug" || level === "trace") {
182
+ return console.debug
183
+ }
184
+
185
+ return console.info
186
+ }
187
+
188
+ export function createLogger(baseContext, runtimeOptions = resolveLoggerRuntimeOptions()) {
189
+ const normalizedBase = {
190
+ service: DEFAULT_SERVICE,
191
+ ...baseContext,
192
+ }
193
+
194
+ const write = (level) => (event, msg, fields, error) => {
195
+ if (!shouldLog(level, runtimeOptions.minLevel)) {
196
+ return
197
+ }
198
+
199
+ const { service = DEFAULT_SERVICE, component, ...contextFields } = normalizedBase
200
+ const payload = pruneNullish({
201
+ ts: new Date().toISOString(),
202
+ level,
203
+ event,
204
+ service,
205
+ component,
206
+ env: runtimeOptions.env,
207
+ msg,
208
+ schema_version: runtimeOptions.schemaVersion,
209
+ ...contextFields,
210
+ ...fields,
211
+ meta: mergeMeta(normalizedBase.meta, fields?.meta),
212
+ ...normalizeError(error, { includeStacks: runtimeOptions.includeStacks && (level === "error" || level === "fatal") }),
213
+ })
214
+
215
+ getConsoleMethod(level)(JSON.stringify(redactForLogs(payload)))
216
+ }
217
+
218
+ return {
219
+ child(extraContext) {
220
+ return createLogger({
221
+ ...normalizedBase,
222
+ ...extraContext,
223
+ meta: mergeMeta(normalizedBase.meta, extraContext.meta),
224
+ }, runtimeOptions)
225
+ },
226
+ trace: write("trace"),
227
+ debug: write("debug"),
228
+ info: write("info"),
229
+ warn: write("warn"),
230
+ error: write("error"),
231
+ fatal: write("fatal"),
232
+ }
233
+ }