wkappbot-code 0.3.10 → 0.3.11

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.
@@ -2,71 +2,86 @@
2
2
  // wkappbot-code postinstall -- npm runs this after `npm install wkappbot-code` (local or -g).
3
3
  // Owner order 2026-11-06: the Default cloud environment's setup script is `npm install wkappbot-code`, and new cloud
4
4
  // ROOTs still started with no hooks, because the package had no install script and npm only copied files.
5
+ // Owner order 2026-11-08: "npm install wkappbot-code should do the initial setup and the tests automatically" -- on
6
+ // Windows, macOS and Linux alike, not only on a cloud VM.
5
7
  // CONTRACT: (a) ALWAYS exits 0 and returns within a moment -- the real work runs DETACHED and logs to
6
- // ~/.wkappbot-code/postinstall.log, then writes ~/.wkappbot-code/postinstall.done; (b) on a Claude Code cloud VM
7
- // (CLAUDE_CODE_REMOTE set, or a Linux HOME of /root that holds .claude) it runs `wkappbot-code install --no-npm`,
8
- // which lays the web harness, wires ~/.claude/settings.json (PreToolUse, Stop, UserPromptSubmit, SessionStart through
9
- // the lite harness), the global git hooks, the wkskill/wk-verify shims and the public skills, keeping every user
10
- // setting; idempotent: a done marker of the same version skips it; (c) on any other machine it only prints the one
11
- // command to run. WKAPPBOT_CODE_NO_POSTINSTALL=1 skips it (the selftest sets it so npm-installs in its cases never
12
- // touch the machine running it). WKAPPBOT_CODE_DEV_SEAT=1 adds --dev-seat (the full pwsh harness instead of lite);
13
- // WKAPPBOT_CODE_INSTALL_ARGS adds install flags. A user install takes the lite harness: on a stranger's cloud VM the
14
- // owner's full fleet harness (busy-fleet, letter gates, emergency-edit) would be the wrong default.
8
+ // ~/.wkappbot-code/postinstall.log; (b) the worker runs `wkappbot-code install --no-npm` (lays the web harness, wires
9
+ // ~/.claude/settings.json through the lite harness -- PreToolUse, Stop, UserPromptSubmit, SessionStart -- the global git
10
+ // hooks, the wkskill/wk-verify shims and the public skills, keeping every user setting), then `wkappbot-code doctor`,
11
+ // then a short self-test (lib/setup-run.js: core --version, skill read on-load, one hook allow, one hook refuse), and
12
+ // writes ~/.wkappbot-code/postinstall.done {version, installRc, doctor:{pass,warn,fail,skip}, selftest:{n,m}, ...};
13
+ // postinstall.running exists while the worker lives; (c) the first `wkappbot-code` run afterwards prints ONE summary
14
+ // line (lib/setup-run.js printBanner) and opens OpenCode; (d) idempotent: a done file of the same version with
15
+ // installRc 0 skips the rerun. WKAPPBOT_CODE_NO_POSTINSTALL=1 skips everything (the selftest sets it so npm-installs
16
+ // in its cases never touch the machine running it). WKAPPBOT_CODE_DEV_SEAT=1 adds --dev-seat (the full pwsh harness
17
+ // instead of lite); WKAPPBOT_CODE_INSTALL_ARGS adds install flags. A user install takes the lite harness: on a
18
+ // stranger's machine the owner's full fleet harness (busy-fleet, letter gates, emergency-edit) would be the wrong default.
15
19
  // Spec: wkappbot skill read agent-harness-landscape-2026-howto
16
- import { spawn } from "node:child_process";
17
- import { existsSync, mkdirSync, openSync, readFileSync, writeFileSync } from "node:fs";
20
+ import { spawn, spawnSync } from "node:child_process";
21
+ import { existsSync, mkdirSync, openSync, readFileSync } from "node:fs";
18
22
  import { homedir } from "node:os";
23
+ import { appendFileSync } from "node:fs";
19
24
  import { dirname, join } from "node:path";
20
25
  import { fileURLToPath } from "node:url";
26
+ import { buildDone, files, markRunning, parseDoctor, platformOf, selfTest, writeDone } from "../lib/setup-run.js";
21
27
 
22
28
  const HERE = dirname(fileURLToPath(import.meta.url));
23
29
  const CLI = join(HERE, "wkappbot-code.js");
24
- const STATE = join(homedir(), ".wkappbot-code");
25
- const LOG = join(STATE, "postinstall.log");
26
- const DONE = join(STATE, "postinstall.done");
30
+ const F = files(homedir());
31
+ const STATE = F.dir, LOG = F.log, DONE = F.done;
27
32
 
28
33
  function version() {
29
34
  try { return JSON.parse(readFileSync(join(HERE, "..", "package.json"), "utf8")).version || "?"; } catch { return "?"; }
30
35
  }
31
- export function isCloudVm(env = process.env, home = homedir(), platform = process.platform) {
36
+ export function isCloudVm(env = process.env, home = homedir(), platform = platformOf(env)) {
32
37
  if (env.CLAUDE_CODE_REMOTE) return true;
33
38
  return platform === "linux" && home === "/root" && existsSync(join(home, ".claude"));
34
39
  }
35
- function stamp(msg) { try { mkdirSync(STATE, { recursive: true }); writeFileSync(LOG, new Date().toISOString() + " " + msg + "\n", { flag: "a" }); } catch {} }
40
+ function stamp(msg) { try { mkdirSync(STATE, { recursive: true }); appendFileSync(LOG, new Date().toISOString() + " " + msg + "\n"); } catch {} }
36
41
 
37
- // --run: the detached worker. Runs the installer to the end, then records the done marker with its exit code.
42
+ // --run: the detached worker. install, then doctor, then the self-test, then the done file.
38
43
  async function runWorker() {
39
44
  const args = [CLI, "install", "--no-npm"];
40
45
  if (process.env.WKAPPBOT_CODE_DEV_SEAT === "1") args.push("--dev-seat");
41
46
  // extra install flags, e.g. "--no-bin --no-skills" (the selftest; a seat without the SDK release assets)
42
47
  args.push(...String(process.env.WKAPPBOT_CODE_INSTALL_ARGS || "").split(/\s+/).filter((a) => /^--[a-z-]+$/.test(a)));
48
+ // the children are the worker's own: they must not print the first-run banner (it is for the user's next command)
49
+ const env = { ...process.env, WKAPPBOT_CODE_NO_UPDATE_CHECK: "1", WKAPPBOT_CODE_SETUP_WORKER: "1" };
43
50
  stamp("worker start: node " + args.join(" "));
44
51
  const out = openSync(LOG, "a");
45
- const rc = await new Promise((res) => {
46
- const c = spawn(process.execPath, args, { stdio: ["ignore", out, out], env: { ...process.env, WKAPPBOT_CODE_NO_UPDATE_CHECK: "1" } });
52
+ const installRc = await new Promise((res) => {
53
+ const c = spawn(process.execPath, args, { stdio: ["ignore", out, out], env });
47
54
  c.on("error", () => res(-1));
48
55
  c.on("close", (code) => res(code ?? -1));
49
56
  });
50
- try { writeFileSync(DONE, JSON.stringify({ version: version(), rc, at: new Date().toISOString() }) + "\n"); } catch {}
51
- stamp("worker done rc " + rc);
57
+ stamp("install rc " + installRc + "; doctor");
58
+ const dr = spawnSync(process.execPath, [CLI, "doctor"], { encoding: "utf8", env, timeout: 300000 });
59
+ const dtext = (dr.stdout || "") + (dr.stderr || "");
60
+ try { appendFileSync(LOG, dtext); } catch {}
61
+ const doctor = parseDoctor(dtext);
62
+ stamp("doctor pass " + doctor.pass + " warn " + doctor.warn + " fail " + doctor.fail + " skip " + doctor.skip + "; self-test");
63
+ let st = { n: 0, m: 4, items: [{ ok: false, name: "self-test", detail: "did not run" }] };
64
+ try { st = selfTest({ env, home: homedir() }); } catch (e) { st.items[0].detail = String(e); }
65
+ for (const i of st.items) stamp("selftest " + (i.ok ? "PASS " : "FAIL ") + i.name + (i.ok ? "" : " -- " + i.detail));
66
+ try { writeDone(homedir(), buildDone({ version: version(), installRc, doctor, selftest: st, env: process.env })); } catch (e) { stamp("done file error: " + e); }
67
+ stamp("worker done: install rc " + installRc + ", doctor " + doctor.pass + "/" + (doctor.pass + doctor.warn + doctor.fail) + ", selftest " + st.n + "/" + st.m);
52
68
  }
53
69
 
54
70
  function main() {
55
71
  if (process.argv[2] === "--run") return runWorker().catch(() => {}).finally(() => process.exit(0));
56
72
  if (process.env.WKAPPBOT_CODE_NO_POSTINSTALL === "1") return;
57
- if (!isCloudVm()) {
58
- console.log("[wkappbot-code] installed. To wire the harness and OpenCode on this machine run: npx wkappbot-code install (then: wkappbot-code doctor)");
59
- return;
60
- }
73
+ const cloud = isCloudVm();
61
74
  try {
62
75
  const d = JSON.parse(readFileSync(DONE, "utf8"));
63
- if (d.version === version() && d.rc === 0) { console.log("[wkappbot-code] cloud VM: harness already wired by " + d.version + " (" + DONE + ")"); return; }
76
+ if (d.version === version() && (d.installRc ?? d.rc) === 0) { console.log("[wkappbot-code] already set up by " + d.version + " (" + DONE + ")"); return; }
64
77
  } catch {}
65
78
  mkdirSync(STATE, { recursive: true });
79
+ markRunning(homedir(), version());
66
80
  const out = openSync(LOG, "a");
67
- const c = spawn(process.execPath, [fileURLToPath(import.meta.url), "--run"], { detached: true, stdio: ["ignore", out, out], env: process.env });
81
+ const c = spawn(process.execPath, [fileURLToPath(import.meta.url), "--run"], { detached: true, stdio: ["ignore", out, out], env: process.env, windowsHide: true });
68
82
  c.unref();
69
- console.log("[wkappbot-code] cloud VM detected: wiring the Claude Code harness in the background (log: " + LOG + ", done marker: " + DONE + ")");
83
+ console.log("[wkappbot-code] " + (cloud ? "cloud VM detected: wiring the Claude Code harness in the background" : "setting up in the background (install, doctor, self-test)") +
84
+ " (log: " + LOG + ", result: " + DONE + "); the first `wkappbot-code` run prints the summary");
70
85
  }
71
86
 
72
87
  try { main(); } catch (e) { try { stamp("postinstall error: " + e); } catch {} }
@@ -40,6 +40,7 @@ import { dirname, isAbsolute, join, relative, resolve } from "node:path";
40
40
  import { fileURLToPath, pathToFileURL } from "node:url";
41
41
  import { quietCheck, runUpdate, updateRow } from "../lib/update.js"; import { rmTreeHard } from "../lib/rmtree.js"; import { plainFileUrl } from "../lib/fileurl.js";
42
42
  import { applySets, askEach, buildConfig, configRow, detect, lineDiff, printDetected } from "../lib/config.js";
43
+ import { launchOpenCode, printBanner } from "../lib/setup-run.js";
43
44
 
44
45
  const PKG_DIR = resolve(dirname(fileURLToPath(import.meta.url)), "..");
45
46
  const PKG = JSON.parse(readFileSync(join(PKG_DIR, "package.json"), "utf8"));
@@ -616,6 +617,10 @@ if (["install", "doctor", "config"].includes(o.cmd) && !o.update) {
616
617
  const nv = quietCheck(PKG.version, console.log);
617
618
  if (nv && (o.autoUpdate || process.env.WKAPPBOT_CODE_AUTO_UPDATE === "1")) upd();
618
619
  }
620
+ // The one-line result of the automatic post-install setup (lib/setup-run.js), once; not for the worker's own children.
621
+ if (!["--version", "-v", "version", "uninstall"].includes(o.cmd) && process.env.WKAPPBOT_CODE_SETUP_WORKER !== "1") { try { printBanner(); } catch {} }
622
+ // Bare `wkappbot-code`: open OpenCode (its start screen); no opencode on PATH falls through to the usage below.
623
+ if (process.argv.length <= 2 && o.cmd === "help") { const rc = launchOpenCode(); if (rc !== null) process.exit(rc); }
619
624
  switch (o.cmd) {
620
625
  case "--version": case "-v": case "version":
621
626
  console.log("wkappbot-code " + PKG.version + " (opencode-ai " + OPENCODE_VERSION + ")"); process.exit(0);
@@ -0,0 +1,135 @@
1
+ // wkappbot-code lib/setup-run.js -- what `npm install wkappbot-code` does after the files land, and what the first
2
+ // `wkappbot-code` run says about it (owner order 2026-11-08: "npm install wkappbot-code should do the initial setup and
3
+ // the tests automatically", "typing wkappbot-code should show the OpenCode start screen").
4
+ // worker bin/postinstall.js --run: install --no-npm, then doctor, then selfTest() below, then ONE postinstall.done
5
+ // JSON {version, installRc, rc, doctor:{pass,warn,fail,skip}, selftest:{n,m}, platform, at, items}.
6
+ // banner bannerFor() / printBanner(): the one line the first run prints ("setup ok, doctor 9/9, selftest 4/4", the
7
+ // failing item with the log path, or "setup still running"), shown ONCE per done file (postinstall.seen).
8
+ // Files in ~/.wkappbot-code: postinstall.log, postinstall.running (worker alive), postinstall.done, postinstall.seen.
9
+ // A user's own settings.json entries are the installer's job to keep (wkappbot-code.js install); this module only reads.
10
+ // Spec: wkappbot skill read agent-harness-landscape-2026-howto
11
+ import { spawnSync } from "node:child_process";
12
+ import { existsSync, mkdirSync, mkdtempSync, readFileSync, renameSync, rmSync, writeFileSync } from "node:fs";
13
+ import { homedir, tmpdir } from "node:os";
14
+ import { join } from "node:path";
15
+
16
+ const RUNNING_STALE_MS = 30 * 60 * 1000; // a worker that left no done file for 30 minutes is dead, not "still running"
17
+ const IS_WIN = process.platform === "win32";
18
+
19
+ // WKAPPBOT_CODE_FAKE_PLATFORM is the selftest's way to run the non-cloud road "as win32 / darwin" on one machine.
20
+ export const platformOf = (env = process.env) => env.WKAPPBOT_CODE_FAKE_PLATFORM || process.platform;
21
+ export const stateDir = (home = homedir()) => join(home, ".wkappbot-code");
22
+ export const files = (home = homedir()) => {
23
+ const d = stateDir(home);
24
+ return { dir: d, log: join(d, "postinstall.log"), running: join(d, "postinstall.running"), done: join(d, "postinstall.done"), seen: join(d, "postinstall.seen") };
25
+ };
26
+ const readJson = (f) => { try { return JSON.parse(readFileSync(f, "utf8")); } catch { return null; } };
27
+ // Same MSYS rule as wkappbot-code.js shPath: the sh Git for Windows starts reads "D:\a\x" as host D.
28
+ const shPath = (p) => (IS_WIN && /^[A-Za-z]:[\\/]/.test(p) ? "/" + p[0].toLowerCase() + p.slice(2).replace(/\\/g, "/") : p);
29
+
30
+ // ---- doctor ---------------------------------------------------------------------------------------
31
+ // doctor prints one `PASS|WARN|FAIL|SKIP name detail` row per part; SKIP rows are not counted in the n/m.
32
+ export function parseDoctor(text) {
33
+ const c = { pass: 0, warn: 0, fail: 0, skip: 0, failed: [] };
34
+ for (const line of String(text || "").split(/\r?\n/)) {
35
+ const m = /^(PASS|WARN|FAIL|SKIP) (\S+)/.exec(line);
36
+ if (!m) continue;
37
+ c[m[1].toLowerCase()]++;
38
+ if (m[1] === "FAIL") c.failed.push(m[2]);
39
+ }
40
+ return c;
41
+ }
42
+
43
+ // ---- self-test ------------------------------------------------------------------------------------
44
+ function installState(env, home) {
45
+ const cfg = env.OPENCODE_CONFIG_DIR || join(env.XDG_CONFIG_HOME || join(home, ".config"), "opencode");
46
+ return readJson(join(cfg, "wkappbot-code", "install-state.json")) || {};
47
+ }
48
+ const hookPayload = (cmd) => JSON.stringify({ hook_event_name: "PreToolUse", session_id: "wkappbot-code-selftest", cwd: tmpdir(), tool_name: "Bash", tool_input: { command: cmd } });
49
+
50
+ // Four items: the core answers --version, the core prints skill on-load, the lite hook ALLOWS a plain command (rc 0) and
51
+ // REFUSES git --no-verify (rc 2). Returns {n, m, items:[{name, ok, detail}]}.
52
+ export function selfTest({ env = process.env, home = homedir() } = {}) {
53
+ const st = installState(env, home);
54
+ const dest = (st.bin && st.bin.dest) || env.WK_BIN_DEST || join(home, ".local", "share", "wkappbot", IS_WIN ? "wkappbot.exe" : "wkappbot");
55
+ const kih = st.kihDir || env.WK_HARNESS_DIR || join(home, ".wk-harness");
56
+ const lite = join(kih, "tools", "wkharness-lite.sh");
57
+ const items = [];
58
+ const add = (name, ok, detail) => items.push({ name, ok: !!ok, detail: String(detail || "").replace(/\s+/g, " ").slice(0, 200) });
59
+ const scratch = mkdtempSync(join(tmpdir(), "wk-appbot-selftest-"));
60
+ const shell = IS_WIN && /\.(cmd|bat)$/i.test(dest); // node refuses to start a .cmd without a shell
61
+ try {
62
+ const core = (args) => (existsSync(dest) ? spawnSync(dest, args, { encoding: "utf8", timeout: 120000, cwd: scratch, shell, env: { ...env, WKAPPBOT_NO_REMOTE_CHECK: "1" } }) : null);
63
+ let r = core(["--version"]);
64
+ add("core --version", r && r.status === 0 && (r.stdout || "").trim(), r ? "rc " + r.status + " " + (r.stdout || r.stderr) : dest + " absent");
65
+ r = core(["skill", "read", "on-load"]);
66
+ add("skill read on-load", r && /\bID\s*:\s*on-load\b/.test(r.stdout || ""), r ? "rc " + r.status : dest + " absent");
67
+ const hook = (cmd) => (existsSync(lite)
68
+ ? spawnSync("sh", [shPath(lite), "pre"], { encoding: "utf8", timeout: 60000, input: hookPayload(cmd), env: { ...env, WKHARNESS_LITE_DIR: shPath(join(scratch, "lite")) } }) : null);
69
+ r = hook("ls /");
70
+ add("hook allow", r && r.status === 0, r ? "rc " + r.status + " " + (r.stderr || "") : lite + " absent");
71
+ r = hook("git commit --no-verify -m selftest");
72
+ add("hook refuse", r && r.status === 2, r ? "rc " + r.status + " " + (r.stderr || "") : lite + " absent");
73
+ } finally { rmSync(scratch, { recursive: true, force: true }); }
74
+ return { n: items.filter((i) => i.ok).length, m: items.length, items };
75
+ }
76
+
77
+ // ---- done / running -------------------------------------------------------------------------------
78
+ export function markRunning(home, version) {
79
+ const f = files(home);
80
+ mkdirSync(f.dir, { recursive: true });
81
+ rmSync(f.done, { force: true }); // the old verdict belongs to the old install
82
+ writeFileSync(f.running, JSON.stringify({ version, at: new Date().toISOString(), pid: process.pid }) + "\n");
83
+ }
84
+ export function writeDone(home, rec) {
85
+ const f = files(home);
86
+ const tmp = f.done + ".tmp";
87
+ writeFileSync(tmp, JSON.stringify(rec) + "\n");
88
+ renameSync(tmp, f.done); // the done file appears whole or not at all (the selftest polls for it)
89
+ rmSync(f.running, { force: true });
90
+ }
91
+ export function buildDone({ version, installRc, doctor, selftest, env = process.env }) {
92
+ return { version, installRc, rc: installRc, doctor: { pass: doctor.pass, warn: doctor.warn, fail: doctor.fail, skip: doctor.skip },
93
+ selftest: { n: selftest.n, m: selftest.m }, platform: platformOf(env), at: new Date().toISOString(),
94
+ items: selftest.items.filter((i) => !i.ok).map((i) => i.name + ": " + i.detail), doctorFailed: doctor.failed };
95
+ }
96
+
97
+ // ---- banner ---------------------------------------------------------------------------------------
98
+ // Pure: the line to print for a state ("" = nothing to say) and whether it counts as seen. `now` is a ms clock.
99
+ export function bannerFor({ done, running, seen, logPath, now = Date.now() }) {
100
+ if (running && !done) {
101
+ const age = now - Date.parse(running.at || 0);
102
+ if (age >= 0 && age < RUNNING_STALE_MS) return { line: "[wkappbot-code] setup still running (log: " + logPath + ") -- opening OpenCode anyway", mark: false };
103
+ return { line: "[wkappbot-code] setup did not finish (worker started " + (running.at || "?") + ", no result); log: " + logPath + " -- rerun: wkappbot-code install", mark: false };
104
+ }
105
+ if (!done || (seen && seen === done.at)) return { line: "", mark: false };
106
+ const d = done.doctor || {}, s = done.selftest || {};
107
+ const total = (d.pass || 0) + (d.warn || 0) + (d.fail || 0);
108
+ const tail = "doctor " + (d.pass || 0) + "/" + total + (d.warn ? " (" + d.warn + " warn)" : "") + ", selftest " + (s.n || 0) + "/" + (s.m || 0);
109
+ const installRc = done.installRc ?? done.rc;
110
+ if (installRc === 0 && !d.fail && s.n === s.m && s.m > 0) return { line: "[wkappbot-code] setup ok, " + tail, mark: true };
111
+ const bad = [];
112
+ if (installRc !== 0) bad.push("install rc " + installRc);
113
+ for (const f of done.doctorFailed || []) bad.push("doctor " + f + " FAIL");
114
+ for (const i of done.items || []) bad.push("selftest " + i);
115
+ if (!bad.length) bad.push(d.fail ? "doctor " + d.fail + " FAIL" : "selftest " + (s.n || 0) + "/" + (s.m || 0));
116
+ return { line: "[wkappbot-code] setup needs attention: " + bad.slice(0, 3).join("; ") + " (" + tail + "); log: " + logPath, mark: true };
117
+ }
118
+ export function printBanner({ home = homedir(), out = console.log, now = Date.now() } = {}) {
119
+ const f = files(home);
120
+ const done = readJson(f.done), running = readJson(f.running);
121
+ let seen = ""; try { seen = readFileSync(f.seen, "utf8").trim(); } catch {}
122
+ const b = bannerFor({ done, running, seen, logPath: f.log, now });
123
+ if (b.line) out(b.line);
124
+ if (b.mark && done) { try { writeFileSync(f.seen, done.at + "\n"); } catch {} }
125
+ return b.line;
126
+ }
127
+
128
+ // ---- bare launch ----------------------------------------------------------------------------------
129
+ // `wkappbot-code` with no arguments opens OpenCode (its start screen). Returns the exit code, or null when opencode
130
+ // cannot be started (not on PATH): the caller then prints the usage as before.
131
+ export function launchOpenCode(env = process.env) {
132
+ const r = spawnSync("opencode", [], { stdio: "inherit", shell: IS_WIN, env });
133
+ if (r.error) return null;
134
+ return r.status ?? 1;
135
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "wkappbot-code",
3
- "version": "0.3.10",
3
+ "version": "0.3.11",
4
4
  "description": "wkappbot-code -- Wisdom Kernel: the pinned OpenCode bridge plus the reflex harness plugin, the wkappbot core, the web harness and the latest public skills in one install",
5
5
  "type": "module",
6
6
  "scripts": {