yaml-flow 5.3.0 → 5.4.2

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.
@@ -1,13 +1,13 @@
1
- import * as fs from 'fs';
2
- import * as os from 'os';
3
- import * as path from 'path';
1
+ import * as fs7 from 'fs';
2
+ import * as os3 from 'os';
3
+ import * as path7 from 'path';
4
4
  import { randomUUID } from 'crypto';
5
5
  import { execFileSync, spawn, execFile } from 'child_process';
6
6
  import { fileURLToPath } from 'url';
7
7
  import fg from 'fast-glob';
8
8
  import { lockSync } from 'proper-lockfile';
9
- import jsonata2 from 'jsonata';
10
9
  import addFormats from 'ajv-formats';
10
+ import jsonata2 from 'jsonata';
11
11
 
12
12
  var __create = Object.create;
13
13
  var __defProp = Object.defineProperty;
@@ -2964,7 +2964,7 @@ var require_compile = __commonJS({
2964
2964
  const schOrFunc = root.refs[ref];
2965
2965
  if (schOrFunc)
2966
2966
  return schOrFunc;
2967
- let _sch = resolve3.call(this, root, ref);
2967
+ let _sch = resolve6.call(this, root, ref);
2968
2968
  if (_sch === void 0) {
2969
2969
  const schema = (_a = root.localRefs) === null || _a === void 0 ? void 0 : _a[ref];
2970
2970
  const { schemaId } = this.opts;
@@ -2991,7 +2991,7 @@ var require_compile = __commonJS({
2991
2991
  function sameSchemaEnv(s1, s2) {
2992
2992
  return s1.schema === s2.schema && s1.root === s2.root && s1.baseId === s2.baseId;
2993
2993
  }
2994
- function resolve3(root, ref) {
2994
+ function resolve6(root, ref) {
2995
2995
  let sch;
2996
2996
  while (typeof (sch = this.refs[ref]) == "string")
2997
2997
  ref = sch;
@@ -3205,8 +3205,8 @@ var require_utils = __commonJS({
3205
3205
  }
3206
3206
  return ind;
3207
3207
  }
3208
- function removeDotSegments(path2) {
3209
- let input = path2;
3208
+ function removeDotSegments(path8) {
3209
+ let input = path8;
3210
3210
  const output = [];
3211
3211
  let nextSlash = -1;
3212
3212
  let len = 0;
@@ -3404,8 +3404,8 @@ var require_schemes = __commonJS({
3404
3404
  wsComponent.secure = void 0;
3405
3405
  }
3406
3406
  if (wsComponent.resourceName) {
3407
- const [path2, query] = wsComponent.resourceName.split("?");
3408
- wsComponent.path = path2 && path2 !== "/" ? path2 : void 0;
3407
+ const [path8, query] = wsComponent.resourceName.split("?");
3408
+ wsComponent.path = path8 && path8 !== "/" ? path8 : void 0;
3409
3409
  wsComponent.query = query;
3410
3410
  wsComponent.resourceName = void 0;
3411
3411
  }
@@ -3563,7 +3563,7 @@ var require_fast_uri = __commonJS({
3563
3563
  }
3564
3564
  return uri;
3565
3565
  }
3566
- function resolve3(baseURI, relativeURI, options) {
3566
+ function resolve6(baseURI, relativeURI, options) {
3567
3567
  const schemelessOptions = options ? Object.assign({ scheme: "null" }, options) : { scheme: "null" };
3568
3568
  const resolved = resolveComponent(parse(baseURI, schemelessOptions), parse(relativeURI, schemelessOptions), schemelessOptions, true);
3569
3569
  schemelessOptions.skipEscape = true;
@@ -3790,7 +3790,7 @@ var require_fast_uri = __commonJS({
3790
3790
  var fastUri = {
3791
3791
  SCHEMES,
3792
3792
  normalize,
3793
- resolve: resolve3,
3793
+ resolve: resolve6,
3794
3794
  resolveComponent,
3795
3795
  equal,
3796
3796
  serialize,
@@ -7740,25 +7740,25 @@ function parseRootPathNamespace(pathValue) {
7740
7740
  const match = ROOT_PATH_NAMESPACE_RE.exec(pathValue);
7741
7741
  return match ? match[1] : null;
7742
7742
  }
7743
- function validateJsonataExprWithNamespaces(expr, path2, allowedNamespaces, errors) {
7743
+ function validateJsonataExprWithNamespaces(expr, path8, allowedNamespaces, errors) {
7744
7744
  try {
7745
7745
  jsonata2(expr);
7746
7746
  } catch (err) {
7747
7747
  const message = err instanceof Error ? err.message : String(err);
7748
- errors.push(`${path2}: invalid JSONata expression (${message})`);
7748
+ errors.push(`${path8}: invalid JSONata expression (${message})`);
7749
7749
  return;
7750
7750
  }
7751
7751
  const usedNamespaces = referencedNamespaces(expr);
7752
7752
  for (const namespace of usedNamespaces) {
7753
7753
  if (!allowedNamespaces.has(namespace)) {
7754
- errors.push(`${path2}: disallowed namespace "${namespace}" in expression`);
7754
+ errors.push(`${path8}: disallowed namespace "${namespace}" in expression`);
7755
7755
  }
7756
7756
  }
7757
7757
  }
7758
- function walkViewPathReferences(value, path2, errors) {
7758
+ function walkViewPathReferences(value, path8, errors) {
7759
7759
  if (Array.isArray(value)) {
7760
7760
  value.forEach((entry, index) => {
7761
- walkViewPathReferences(entry, `${path2}/${index}`, errors);
7761
+ walkViewPathReferences(entry, `${path8}/${index}`, errors);
7762
7762
  });
7763
7763
  return;
7764
7764
  }
@@ -7766,14 +7766,14 @@ function walkViewPathReferences(value, path2, errors) {
7766
7766
  const rootNamespace = parseRootPathNamespace(value);
7767
7767
  if (!rootNamespace) return;
7768
7768
  if (!(/* @__PURE__ */ new Set(["card_data", "requires", "fetched_sources", "computed_values"])).has(rootNamespace)) {
7769
- errors.push(`${path2}: disallowed namespace "${rootNamespace}" in view reference`);
7769
+ errors.push(`${path8}: disallowed namespace "${rootNamespace}" in view reference`);
7770
7770
  }
7771
7771
  return;
7772
7772
  }
7773
7773
  if (!value || typeof value !== "object") return;
7774
7774
  const record = value;
7775
7775
  for (const [key, next] of Object.entries(record)) {
7776
- walkViewPathReferences(next, `${path2}/${key}`, errors);
7776
+ walkViewPathReferences(next, `${path8}/${key}`, errors);
7777
7777
  }
7778
7778
  }
7779
7779
  function getValidator() {
@@ -7787,8 +7787,8 @@ function validateLiveCardSchema(node) {
7787
7787
  const validate = getValidator();
7788
7788
  const valid = validate(node);
7789
7789
  const errors = (validate.errors ?? []).map((e) => {
7790
- const path2 = e.instancePath || "/";
7791
- return `${path2}: ${e.message ?? "unknown error"}`;
7790
+ const path8 = e.instancePath || "/";
7791
+ return `${path8}: ${e.message ?? "unknown error"}`;
7792
7792
  });
7793
7793
  if (node && typeof node === "object" && !Array.isArray(node)) {
7794
7794
  const source_defs = node.source_defs;
@@ -7887,1098 +7887,1696 @@ function validateLiveCardDefinition(node) {
7887
7887
  if (!runtime.ok) return { ok: false, errors: runtime.errors };
7888
7888
  return { ok: true, errors: [] };
7889
7889
  }
7890
-
7891
- // src/card-compute/index.ts
7892
- function deepGet(obj, path2) {
7893
- if (!path2 || !obj) return void 0;
7894
- const parts = path2.split(".");
7895
- let cur = obj;
7896
- for (let i = 0; i < parts.length; i++) {
7897
- if (cur == null) return void 0;
7898
- cur = cur[parts[i]];
7899
- }
7900
- return cur;
7901
- }
7902
- function deepSet(obj, path2, value) {
7903
- const parts = path2.split(".");
7904
- let cur = obj;
7905
- for (let i = 0; i < parts.length - 1; i++) {
7906
- if (cur[parts[i]] == null || typeof cur[parts[i]] !== "object") cur[parts[i]] = {};
7907
- cur = cur[parts[i]];
7908
- }
7909
- cur[parts[parts.length - 1]] = value;
7910
- }
7911
- async function run(node, options) {
7912
- if (!node?.compute?.length) return node;
7913
- if (!node.card_data) node.card_data = {};
7914
- node.computed_values = {};
7915
- node._sourcesData = options?.sourcesData ?? {};
7916
- const ctx = {
7917
- card_data: node.card_data,
7918
- requires: node.requires ?? {},
7919
- fetched_sources: node._sourcesData,
7920
- computed_values: node.computed_values
7921
- };
7922
- for (const step of node.compute) {
7923
- try {
7924
- const val = await jsonata2(step.expr).evaluate(ctx);
7925
- deepSet(node.computed_values, step.bindTo, val);
7926
- ctx.computed_values = node.computed_values;
7927
- } catch (err) {
7928
- console.error(`CardCompute.run error on "${node.id ?? "?"}.${step.bindTo}":`, err);
7890
+ function createBoardCommandHandlers(deps) {
7891
+ function cmdInit(args) {
7892
+ const dir = args[0];
7893
+ if (!dir) {
7894
+ throw new Error("Usage: board-live-cards init <dir> [--task-executor <script>] [--chat-handler <script>] [--inference-adapter <script>] [--runtime-out <dir>]");
7895
+ }
7896
+ const teIdx = args.indexOf("--task-executor");
7897
+ const taskExecutor = teIdx !== -1 ? args[teIdx + 1] : void 0;
7898
+ const chIdx = args.indexOf("--chat-handler");
7899
+ const chatHandler = chIdx !== -1 ? args[chIdx + 1] : void 0;
7900
+ const iaIdx = args.indexOf("--inference-adapter");
7901
+ const inferenceAdapter = iaIdx !== -1 ? args[iaIdx + 1] : void 0;
7902
+ const roIdx = args.indexOf("--runtime-out");
7903
+ const runtimeOut = roIdx !== -1 ? args[roIdx + 1] : void 0;
7904
+ if (roIdx !== -1 && !runtimeOut) {
7905
+ throw new Error("Usage: board-live-cards init <dir> [--task-executor <script>] [--chat-handler <script>] [--inference-adapter <script>] [--runtime-out <dir>]");
7906
+ }
7907
+ const result = deps.initBoard(dir);
7908
+ if (taskExecutor) {
7909
+ const teExtraIdx = args.indexOf("--task-executor-extra");
7910
+ let teExtra;
7911
+ if (teExtraIdx !== -1 && args[teExtraIdx + 1]) {
7912
+ try {
7913
+ teExtra = JSON.parse(args[teExtraIdx + 1]);
7914
+ } catch {
7915
+ }
7916
+ }
7917
+ const teConfig = { command: taskExecutor, ...teExtra ? { extra: teExtra } : {} };
7918
+ fs7.writeFileSync(path7.join(dir, ".task-executor"), JSON.stringify(teConfig, null, 2), "utf-8");
7929
7919
  }
7930
- }
7931
- return node;
7932
- }
7933
- async function evalExpr(expr, node) {
7934
- const ctx = {
7935
- card_data: node.card_data ?? {},
7936
- requires: node.requires ?? {},
7937
- fetched_sources: node._sourcesData ?? {},
7938
- computed_values: node.computed_values ?? {}
7939
- };
7940
- return jsonata2(expr).evaluate(ctx);
7941
- }
7942
- function resolve(node, path2) {
7943
- if (path2.startsWith("fetched_sources.")) {
7944
- return deepGet(node._sourcesData ?? {}, path2.slice("fetched_sources.".length));
7945
- }
7946
- return deepGet(node, path2);
7947
- }
7948
- var VALID_ELEMENT_KINDS = /* @__PURE__ */ new Set([
7949
- "metric",
7950
- "table",
7951
- "editable-table",
7952
- "chart",
7953
- "form",
7954
- "filter",
7955
- "list",
7956
- "notes",
7957
- "todo",
7958
- "alert",
7959
- "narrative",
7960
- "badge",
7961
- "text",
7962
- "markdown",
7963
- "ref",
7964
- "custom",
7965
- "actions"
7966
- ]);
7967
- var ALLOWED_KEYS = /* @__PURE__ */ new Set(["id", "meta", "requires", "provides", "view", "card_data", "compute", "source_defs"]);
7968
- function validateNode(node) {
7969
- const errors = [];
7970
- if (!node || typeof node !== "object" || Array.isArray(node)) {
7971
- return { ok: false, errors: ["Node must be a non-null object"] };
7972
- }
7973
- const n = node;
7974
- if (typeof n.id !== "string" || !n.id) errors.push("id: required, must be a non-empty string");
7975
- for (const key of Object.keys(n)) {
7976
- if (!ALLOWED_KEYS.has(key)) errors.push(`Unknown top-level key: "${key}"`);
7977
- }
7978
- if (n.card_data == null || typeof n.card_data !== "object" || Array.isArray(n.card_data)) {
7979
- errors.push("card_data: required, must be an object");
7980
- }
7981
- if (n.meta != null) {
7982
- if (typeof n.meta !== "object" || Array.isArray(n.meta)) {
7983
- errors.push("meta: must be an object");
7984
- } else {
7985
- const meta = n.meta;
7986
- if (meta.title != null && typeof meta.title !== "string") errors.push("meta.title: must be a string");
7987
- if (meta.tags != null && !Array.isArray(meta.tags)) errors.push("meta.tags: must be an array");
7920
+ if (chatHandler) {
7921
+ fs7.writeFileSync(path7.join(dir, ".chat-handler"), chatHandler, "utf-8");
7988
7922
  }
7989
- }
7990
- if (n.requires != null && !Array.isArray(n.requires)) errors.push("requires: must be an array of strings");
7991
- if (n.provides != null) {
7992
- if (!Array.isArray(n.provides)) {
7993
- errors.push("provides: must be an array of { bindTo, ref } bindings");
7994
- } else {
7995
- n.provides.forEach((p, i) => {
7996
- if (!p || typeof p !== "object" || Array.isArray(p)) {
7997
- errors.push(`provides[${i}]: must be an object with bindTo and ref`);
7998
- } else {
7999
- const b = p;
8000
- if (typeof b.bindTo !== "string" || !b.bindTo) errors.push(`provides[${i}]: missing required "bindTo" string`);
8001
- if (typeof b.ref !== "string" || !b.ref) errors.push(`provides[${i}]: missing required "ref" string`);
8002
- }
8003
- });
7923
+ if (inferenceAdapter) {
7924
+ fs7.writeFileSync(path7.join(dir, ".inference-adapter"), inferenceAdapter, "utf-8");
8004
7925
  }
8005
- }
8006
- if (n.compute != null) {
8007
- if (!Array.isArray(n.compute)) {
8008
- errors.push("compute: must be an array of compute steps");
7926
+ const runtimeOutDir = deps.configureRuntimeOutDir(dir, runtimeOut);
7927
+ const live = deps.loadBoard(dir);
7928
+ deps.writeJsonAtomic(deps.resolveStatusSnapshotPath(dir), deps.buildBoardStatusObject(dir, live));
7929
+ if (result === "exists") {
7930
+ console.log(`Board already initialized at ${path7.resolve(dir)}${taskExecutor ? ` (task-executor updated: ${taskExecutor})` : ""} (runtime-out: ${runtimeOutDir})`);
8009
7931
  } else {
8010
- n.compute.forEach((step, i) => {
8011
- if (!step || typeof step !== "object" || Array.isArray(step)) {
8012
- errors.push(`compute[${i}]: must be a compute step object`);
8013
- } else {
8014
- const s = step;
8015
- if (typeof s.bindTo !== "string" || !s.bindTo) errors.push(`compute[${i}]: missing required "bindTo" property`);
8016
- if (typeof s.expr !== "string" || !s.expr) errors.push(`compute[${i}]: missing required "expr" string (JSONata expression)`);
8017
- }
8018
- });
7932
+ console.log(`Board initialized at ${path7.resolve(dir)}${taskExecutor ? ` (task-executor: ${taskExecutor})` : ""} (runtime-out: ${runtimeOutDir})`);
8019
7933
  }
8020
7934
  }
8021
- if (n.source_defs != null) {
8022
- if (!Array.isArray(n.source_defs)) {
8023
- errors.push("source_defs: must be an array");
8024
- } else {
8025
- const bindTos = /* @__PURE__ */ new Set();
8026
- const outputFiles = /* @__PURE__ */ new Set();
8027
- n.source_defs.forEach((src, i) => {
8028
- if (!src || typeof src !== "object" || Array.isArray(src)) {
8029
- errors.push(`source_defs[${i}]: must be an object`);
8030
- } else {
8031
- const s = src;
8032
- if (typeof s.bindTo !== "string" || !s.bindTo) {
8033
- errors.push(`source_defs[${i}]: missing required "bindTo" property`);
8034
- } else {
8035
- if (bindTos.has(s.bindTo)) {
8036
- errors.push(`source_defs[${i}]: bindTo "${s.bindTo}" is not unique across source_defs`);
8037
- }
8038
- bindTos.add(s.bindTo);
8039
- }
8040
- if (typeof s.outputFile !== "string" || !s.outputFile) {
8041
- errors.push(`source_defs[${i}]: missing required "outputFile" property`);
8042
- } else {
8043
- if (outputFiles.has(s.outputFile)) {
8044
- errors.push(`source_defs[${i}]: outputFile "${s.outputFile}" is not unique across source_defs`);
8045
- }
8046
- outputFiles.add(s.outputFile);
8047
- }
8048
- if (s.optionalForCompletionGating != null && typeof s.optionalForCompletionGating !== "boolean") {
8049
- errors.push(`source_defs[${i}]: optionalForCompletionGating must be a boolean`);
8050
- }
8051
- }
8052
- });
7935
+ function cmdStatus(args) {
7936
+ const rgIdx = args.indexOf("--rg");
7937
+ const asJson = args.includes("--json");
7938
+ const dir = rgIdx !== -1 ? args[rgIdx + 1] : void 0;
7939
+ if (!dir) {
7940
+ console.error("Usage: board-live-cards status --rg <dir>");
7941
+ process.exit(1);
8053
7942
  }
8054
- }
8055
- if (n.view != null) {
8056
- if (typeof n.view !== "object" || Array.isArray(n.view)) {
8057
- errors.push("view: must be an object");
7943
+ const statusOutPath = deps.resolveStatusSnapshotPath(dir);
7944
+ let statusObject;
7945
+ if (fs7.existsSync(statusOutPath)) {
7946
+ statusObject = JSON.parse(fs7.readFileSync(statusOutPath, "utf-8"));
8058
7947
  } else {
8059
- const view = n.view;
8060
- if (!Array.isArray(view.elements) || view.elements.length === 0) {
8061
- errors.push("view.elements: required, must be a non-empty array");
8062
- } else {
8063
- view.elements.forEach((elem, i) => {
8064
- if (!elem || typeof elem !== "object") {
8065
- errors.push(`view.elements[${i}]: must be an object`);
8066
- return;
8067
- }
8068
- if (!elem.kind || typeof elem.kind !== "string") {
8069
- errors.push(`view.elements[${i}].kind: required, must be a string`);
8070
- } else if (!VALID_ELEMENT_KINDS.has(elem.kind)) {
8071
- errors.push(`view.elements[${i}].kind: unknown kind "${elem.kind}". Valid: ${[...VALID_ELEMENT_KINDS].join(", ")}`);
8072
- }
8073
- if (elem.data != null && (typeof elem.data !== "object" || Array.isArray(elem.data))) {
8074
- errors.push(`view.elements[${i}].data: must be an object`);
8075
- }
8076
- });
8077
- }
8078
- if (view.layout != null && (typeof view.layout !== "object" || Array.isArray(view.layout))) errors.push("view.layout: must be an object");
8079
- if (view.features != null && (typeof view.features !== "object" || Array.isArray(view.features))) errors.push("view.features: must be an object");
7948
+ statusObject = deps.buildBoardStatusObject(dir, deps.loadBoard(dir));
7949
+ deps.writeJsonAtomic(statusOutPath, statusObject);
8080
7950
  }
8081
- }
8082
- return { ok: errors.length === 0, errors };
8083
- }
8084
- async function enrichSources(source_defs, context) {
8085
- if (!source_defs || source_defs.length === 0) return [];
8086
- const evalCtx = {
8087
- card_data: context.card_data ?? {},
8088
- requires: context.requires ?? {}
8089
- };
8090
- return Promise.all(
8091
- source_defs.map(async (src) => {
8092
- const _projections = {};
8093
- if (src.projections && typeof src.projections === "object" && !Array.isArray(src.projections)) {
8094
- for (const [key, expr] of Object.entries(src.projections)) {
8095
- if (typeof expr === "string" && expr.trim().length > 0) {
7951
+ if (asJson) {
7952
+ console.log(JSON.stringify(statusObject, null, 2));
7953
+ return;
7954
+ }
7955
+ console.log(`Board: ${statusObject.meta.board.path}`);
7956
+ console.log(`Tasks: ${statusObject.summary.card_count}`);
7957
+ console.log("");
7958
+ for (const card of statusObject.cards) {
7959
+ const dataKeys = card.provides_runtime.join(", ");
7960
+ console.log(` ${card.status.padEnd(12)} ${card.name}${dataKeys ? ` \u2014 [${dataKeys}]` : ""}`);
7961
+ }
7962
+ console.log("");
7963
+ console.log(`Schedule: ${statusObject.summary.eligible} eligible, ${statusObject.summary.pending} pending, ${statusObject.summary.blocked} blocked, ${statusObject.summary.unresolved} unresolved`);
7964
+ }
7965
+ function cmdValidateCard(args) {
7966
+ const cardIdx = args.indexOf("--card");
7967
+ const globIdx = args.indexOf("--card-glob");
7968
+ const rgIdx = args.indexOf("--rg");
7969
+ const cardFile = cardIdx !== -1 ? args[cardIdx + 1] : void 0;
7970
+ const cardGlob = globIdx !== -1 ? args[globIdx + 1] : void 0;
7971
+ const boardDir = rgIdx !== -1 ? args[rgIdx + 1] : void 0;
7972
+ if (!cardFile && !cardGlob || cardFile && cardGlob) {
7973
+ throw new Error("Usage: board-live-cards validate-card (--card <card.json> | --card-glob <glob>) [--rg <boardDir>]");
7974
+ }
7975
+ let teConfig;
7976
+ if (boardDir) {
7977
+ teConfig = deps.readTaskExecutorConfig(boardDir);
7978
+ if (!teConfig) {
7979
+ throw new Error(`--rg specified but no .task-executor found in ${boardDir}`);
7980
+ }
7981
+ }
7982
+ const files = cardFile ? [path7.resolve(cardFile)] : deps.resolveCardGlobMatches(cardGlob);
7983
+ if (files.length === 0) {
7984
+ throw new Error(`No card files matched glob: ${cardGlob}`);
7985
+ }
7986
+ let failures = 0;
7987
+ for (const f of files) {
7988
+ const label = path7.relative(process.cwd(), f) || f;
7989
+ if (!fs7.existsSync(f)) {
7990
+ console.error(`FAIL ${label}: file not found`);
7991
+ failures++;
7992
+ continue;
7993
+ }
7994
+ let card;
7995
+ try {
7996
+ card = JSON.parse(fs7.readFileSync(f, "utf-8"));
7997
+ } catch (err) {
7998
+ console.error(`FAIL ${label}: invalid JSON \u2014 ${err instanceof Error ? err.message : String(err)}`);
7999
+ failures++;
8000
+ continue;
8001
+ }
8002
+ const result = deps.validateLiveCardDefinition(card);
8003
+ const sourceErrors = [];
8004
+ if (teConfig && Array.isArray(card.source_defs)) {
8005
+ for (const src of card.source_defs) {
8006
+ const bindTo = typeof src.bindTo === "string" ? src.bindTo : "(unknown)";
8007
+ const tmpFile = path7.join(os3.tmpdir(), `validate-src-${bindTo}-${Date.now()}.json`);
8008
+ try {
8009
+ fs7.writeFileSync(tmpFile, JSON.stringify(src), "utf-8");
8010
+ let stdout;
8096
8011
  try {
8097
- _projections[key] = await jsonata2(expr).evaluate(evalCtx);
8012
+ stdout = deps.execCommandSync(teConfig.command, ["validate-source-def", "--in", tmpFile], { shell: true, timeout: 1e4 });
8013
+ } catch (execErr) {
8014
+ stdout = typeof execErr?.stdout === "string" ? execErr.stdout : Buffer.isBuffer(execErr?.stdout) ? execErr.stdout.toString("utf-8") : "";
8015
+ if (!stdout.trim()) {
8016
+ sourceErrors.push(`source "${bindTo}": executor validate-source-def failed \u2014 ${execErr instanceof Error ? execErr.message : String(execErr)}`);
8017
+ continue;
8018
+ }
8019
+ }
8020
+ const parsed = JSON.parse(stdout.trim());
8021
+ if (!parsed.ok && Array.isArray(parsed.errors)) {
8022
+ for (const error of parsed.errors) {
8023
+ sourceErrors.push(`source "${bindTo}": ${error}`);
8024
+ }
8025
+ }
8026
+ } catch (err) {
8027
+ sourceErrors.push(`source "${bindTo}": executor validate-source-def failed \u2014 ${err instanceof Error ? err.message : String(err)}`);
8028
+ } finally {
8029
+ try {
8030
+ fs7.unlinkSync(tmpFile);
8098
8031
  } catch {
8099
- _projections[key] = void 0;
8100
8032
  }
8101
8033
  }
8102
8034
  }
8103
8035
  }
8104
- return { ...src, _projections };
8105
- })
8106
- );
8107
- }
8108
- var CardCompute = {
8109
- run,
8110
- eval: evalExpr,
8111
- resolve,
8112
- validate: validateNode,
8113
- enrichSources
8114
- };
8115
-
8116
- // src/cli/board-live-cards-cli.ts
8117
- var BOARD_FILE = "board-graph.json";
8118
- var JOURNAL_FILE = "board-journal.jsonl";
8119
- var TASK_EXECUTOR_LOG_FILE = "task-executor.jsonl";
8120
- var INFERENCE_ADAPTER_LOG_FILE = "inference-adapter.jsonl";
8121
- var INVENTORY_FILE = "cards-inventory.jsonl";
8122
- var RUNTIME_OUT_FILE = ".runtime-out";
8123
- var DEFAULT_RUNTIME_OUT_DIR = "runtime-out";
8124
- var RUNTIME_STATUS_FILE = "board-livegraph-status.json";
8125
- var RUNTIME_CARDS_DIR = "cards";
8126
- var RUNTIME_DATA_OBJECTS_DIR = "data-objects";
8127
- var INFERENCE_ADAPTER_FILE = ".inference-adapter";
8128
- var TASK_EXECUTOR_FILE = ".task-executor";
8129
- var DEFAULT_TASK_COMPLETION_RULE = "all_required_sources_fetched";
8130
- function readTaskExecutorConfig(boardDir) {
8131
- const executorFile = path.join(boardDir, TASK_EXECUTOR_FILE);
8132
- if (!fs.existsSync(executorFile)) return void 0;
8133
- const raw = fs.readFileSync(executorFile, "utf-8").trim();
8134
- if (!raw) return void 0;
8135
- try {
8136
- const parsed = JSON.parse(raw);
8137
- if (parsed && typeof parsed === "object" && typeof parsed.command === "string") {
8138
- return parsed;
8036
+ const allErrors = [...result.errors, ...sourceErrors];
8037
+ if (allErrors.length === 0) {
8038
+ console.log(`OK ${label}`);
8039
+ } else {
8040
+ console.error(`FAIL ${label}:`);
8041
+ for (const error of allErrors) {
8042
+ console.error(` ${error}`);
8043
+ }
8044
+ failures++;
8045
+ }
8139
8046
  }
8140
- } catch {
8141
- }
8142
- return { command: raw };
8143
- }
8144
- var EMPTY_CONFIG = { settings: { completion: "manual", refreshStrategy: "data-changed" }, tasks: {} };
8145
- var BoardJournal = class {
8146
- journalPath;
8147
- lastDrainedId;
8148
- constructor(journalPath, lastDrainedJournalId) {
8149
- this.journalPath = journalPath;
8150
- this.lastDrainedId = lastDrainedJournalId;
8047
+ if (failures > 0) {
8048
+ throw new Error(`${failures} of ${files.length} card(s) failed validation.`);
8049
+ }
8050
+ console.log(`
8051
+ ${files.length} card(s) passed validation.`);
8151
8052
  }
8152
- append(event) {
8153
- const entry = { id: randomUUID(), event };
8154
- fs.appendFileSync(this.journalPath, JSON.stringify(entry) + "\n", "utf-8");
8155
- }
8156
- drain() {
8157
- if (!fs.existsSync(this.journalPath)) return [];
8158
- const content = fs.readFileSync(this.journalPath, "utf-8").trim();
8159
- if (!content) return [];
8160
- const entries = content.split("\n").map((l) => JSON.parse(l));
8161
- let startIdx = 0;
8162
- if (this.lastDrainedId) {
8163
- const drainedIdx = entries.findIndex((e) => e.id === this.lastDrainedId);
8164
- if (drainedIdx !== -1) startIdx = drainedIdx + 1;
8053
+ function cmdRemoveCard(args) {
8054
+ const rgIdx = args.indexOf("--rg");
8055
+ const idIdx = args.indexOf("--id");
8056
+ const dir = rgIdx !== -1 ? args[rgIdx + 1] : void 0;
8057
+ const cardId = idIdx !== -1 ? args[idIdx + 1] : void 0;
8058
+ if (!dir || !cardId) {
8059
+ console.error("Usage: board-live-cards remove-card --rg <dir> --id <card-id>");
8060
+ process.exit(1);
8165
8061
  }
8166
- const undrained = entries.slice(startIdx);
8167
- if (undrained.length > 0) {
8168
- this.lastDrainedId = undrained[undrained.length - 1].id;
8062
+ deps.appendEventToJournal(dir, {
8063
+ type: "task-removal",
8064
+ taskName: cardId,
8065
+ timestamp: (/* @__PURE__ */ new Date()).toISOString()
8066
+ });
8067
+ void deps.processAccumulatedEventsInfinitePass(dir);
8068
+ console.log(`Card "${cardId}" removed.`);
8069
+ }
8070
+ function cmdRetrigger(args) {
8071
+ const rgIdx = args.indexOf("--rg");
8072
+ const taskIdx = args.indexOf("--task");
8073
+ const dir = rgIdx !== -1 ? args[rgIdx + 1] : void 0;
8074
+ const taskName = taskIdx !== -1 ? args[taskIdx + 1] : void 0;
8075
+ if (!dir || !taskName) {
8076
+ console.error("Usage: board-live-cards retrigger --rg <dir> --task <task-name>");
8077
+ process.exit(1);
8169
8078
  }
8170
- return undrained.map((e) => e.event);
8171
- }
8172
- get size() {
8173
- if (!fs.existsSync(this.journalPath)) return 0;
8174
- const content = fs.readFileSync(this.journalPath, "utf-8").trim();
8175
- if (!content) return 0;
8176
- const entries = content.split("\n").map((l) => JSON.parse(l));
8177
- if (!this.lastDrainedId) return entries.length;
8178
- const drainedIdx = entries.findIndex((e) => e.id === this.lastDrainedId);
8179
- return drainedIdx === -1 ? entries.length : entries.length - drainedIdx - 1;
8180
- }
8181
- get lastDrainedJournalId() {
8182
- return this.lastDrainedId;
8079
+ deps.appendEventToJournal(dir, {
8080
+ type: "task-restart",
8081
+ taskName,
8082
+ timestamp: (/* @__PURE__ */ new Date()).toISOString()
8083
+ });
8084
+ void deps.processAccumulatedEventsInfinitePass(dir);
8085
+ console.log(`Task "${taskName}" retriggered.`);
8183
8086
  }
8184
- };
8185
- function readCardInventory(boardDir) {
8186
- const inventoryPath = path.join(boardDir, INVENTORY_FILE);
8187
- if (!fs.existsSync(inventoryPath)) return [];
8188
- const lines = fs.readFileSync(inventoryPath, "utf-8").split("\n").filter((l) => l.trim());
8189
- return lines.map((l) => JSON.parse(l));
8190
- }
8191
- function lookupCardPath(boardDir, cardId) {
8192
- const entries = readCardInventory(boardDir);
8193
- const entry = entries.find((e) => e.cardId === cardId);
8194
- return entry?.cardFilePath ?? null;
8195
- }
8196
- function appendCardInventory(boardDir, entry) {
8197
- const inventoryPath = path.join(boardDir, INVENTORY_FILE);
8198
- const normalized = { ...entry, cardFilePath: path.resolve(entry.cardFilePath) };
8199
- fs.appendFileSync(inventoryPath, JSON.stringify(normalized) + "\n");
8087
+ return {
8088
+ cmdInit,
8089
+ cmdStatus,
8090
+ cmdValidateCard,
8091
+ cmdRemoveCard,
8092
+ cmdRetrigger
8093
+ };
8200
8094
  }
8201
- function buildCardInventoryIndex(boardDir) {
8202
- const byCardId = /* @__PURE__ */ new Map();
8203
- const byCardPath = /* @__PURE__ */ new Map();
8204
- for (const entry of readCardInventory(boardDir)) {
8205
- const normalizedPath = path.resolve(entry.cardFilePath);
8206
- const normalizedEntry = {
8207
- ...entry,
8208
- cardFilePath: normalizedPath
8209
- };
8210
- const existingById = byCardId.get(entry.cardId);
8211
- if (existingById && existingById.cardFilePath !== normalizedPath) {
8212
- throw new Error(
8213
- `Inventory invariant violation: card id "${entry.cardId}" maps to multiple files: "${existingById.cardFilePath}" and "${normalizedPath}"`
8214
- );
8095
+ function createCallbackCommandHandlers(deps) {
8096
+ function cmdTaskCompleted(args) {
8097
+ const rgIdx = args.indexOf("--rg");
8098
+ const tokenIdx = args.indexOf("--token");
8099
+ const dataIdx = args.indexOf("--data");
8100
+ const dir = rgIdx !== -1 ? args[rgIdx + 1] : void 0;
8101
+ const token = tokenIdx !== -1 ? args[tokenIdx + 1] : void 0;
8102
+ if (!dir || !token) {
8103
+ console.error("Usage: board-live-cards task-completed --rg <dir> --token <token> [--data <json>]");
8104
+ process.exit(1);
8215
8105
  }
8216
- const existingByPath = byCardPath.get(normalizedPath);
8217
- if (existingByPath && existingByPath.cardId !== entry.cardId) {
8218
- throw new Error(
8219
- `Inventory invariant violation: file "${normalizedPath}" maps to multiple ids: "${existingByPath.cardId}" and "${entry.cardId}"`
8220
- );
8106
+ const decoded = deps.decodeCallbackToken(token);
8107
+ if (!decoded) {
8108
+ console.error("Invalid callback token");
8109
+ process.exit(1);
8221
8110
  }
8222
- byCardId.set(entry.cardId, normalizedEntry);
8223
- byCardPath.set(normalizedPath, normalizedEntry);
8224
- }
8225
- return { byCardId, byCardPath };
8226
- }
8227
- function initBoard(dir) {
8228
- const boardPath = path.join(dir, BOARD_FILE);
8229
- if (fs.existsSync(boardPath)) {
8230
- const envelope2 = JSON.parse(fs.readFileSync(boardPath, "utf-8"));
8231
- restore(envelope2.graph);
8232
- return "exists";
8233
- }
8234
- if (fs.existsSync(dir)) {
8235
- const entries = fs.readdirSync(dir);
8236
- if (entries.length > 0) {
8237
- throw new Error(`Directory "${dir}" is not empty and has no valid ${BOARD_FILE}`);
8111
+ const data = dataIdx !== -1 ? JSON.parse(args[dataIdx + 1]) : {};
8112
+ deps.writeRuntimeDataObjects(dir, data);
8113
+ deps.appendEventToJournal(dir, {
8114
+ type: "task-completed",
8115
+ taskName: decoded.taskName,
8116
+ data,
8117
+ timestamp: (/* @__PURE__ */ new Date()).toISOString()
8118
+ });
8119
+ void deps.processAccumulatedEventsForced(dir);
8120
+ console.log("Task completed.");
8121
+ }
8122
+ function cmdTaskFailed(args) {
8123
+ const rgIdx = args.indexOf("--rg");
8124
+ const tokenIdx = args.indexOf("--token");
8125
+ const errorIdx = args.indexOf("--error");
8126
+ const dir = rgIdx !== -1 ? args[rgIdx + 1] : void 0;
8127
+ const token = tokenIdx !== -1 ? args[tokenIdx + 1] : void 0;
8128
+ const errorMsg = errorIdx !== -1 ? args[errorIdx + 1] : "unknown error";
8129
+ if (!dir || !token) {
8130
+ console.error("Usage: board-live-cards task-failed --rg <dir> --token <token> [--error <message>]");
8131
+ process.exit(1);
8132
+ }
8133
+ const decoded = deps.decodeCallbackToken(token);
8134
+ if (!decoded) {
8135
+ console.error("Invalid callback token");
8136
+ process.exit(1);
8137
+ }
8138
+ deps.appendEventToJournal(dir, {
8139
+ type: "task-failed",
8140
+ taskName: decoded.taskName,
8141
+ error: errorMsg,
8142
+ timestamp: (/* @__PURE__ */ new Date()).toISOString()
8143
+ });
8144
+ void deps.processAccumulatedEventsForced(dir);
8145
+ console.log("Task failed.");
8146
+ }
8147
+ function cmdSourceDataFetched(args) {
8148
+ const tmpIdx = args.indexOf("--tmp");
8149
+ const tokenIdx = args.indexOf("--token");
8150
+ const tmpFile = tmpIdx !== -1 ? args[tmpIdx + 1] : void 0;
8151
+ const token = tokenIdx !== -1 ? args[tokenIdx + 1] : void 0;
8152
+ if (!tmpFile || !token) {
8153
+ console.error("Usage: board-live-cards source-data-fetched --tmp <tmp-file> --token <sourceToken>");
8154
+ process.exit(1);
8155
+ }
8156
+ const payload = deps.decodeSourceToken(token);
8157
+ if (!payload) {
8158
+ console.error("Invalid source token");
8159
+ process.exit(1);
8160
+ }
8161
+ const { cbk, rg, cid, b, d, cs } = payload;
8162
+ const destPath = path7.join(rg, cid, d);
8163
+ fs7.mkdirSync(path7.dirname(destPath), { recursive: true });
8164
+ fs7.renameSync(tmpFile, destPath);
8165
+ console.log(`[source-data-fetched] ${cid}.${b} -> ${cid}/${d}`);
8166
+ const fetchedAt = (/* @__PURE__ */ new Date()).toISOString();
8167
+ const cbkDecoded = deps.decodeCallbackToken(cbk);
8168
+ if (!cbkDecoded) {
8169
+ console.error("Invalid callback token embedded in source token");
8170
+ process.exit(1);
8171
+ }
8172
+ deps.appendEventToJournal(rg, {
8173
+ type: "task-progress",
8174
+ taskName: cbkDecoded.taskName,
8175
+ update: { bindTo: b, outputFile: d, fetchedAt, sourceChecksum: cs },
8176
+ timestamp: fetchedAt
8177
+ });
8178
+ void deps.processAccumulatedEventsInfinitePass(rg);
8179
+ }
8180
+ function cmdSourceDataFetchFailure(args) {
8181
+ const tokenIdx = args.indexOf("--token");
8182
+ const reasonIdx = args.indexOf("--reason");
8183
+ const token = tokenIdx !== -1 ? args[tokenIdx + 1] : void 0;
8184
+ const reason = reasonIdx !== -1 ? args[reasonIdx + 1] : "unknown";
8185
+ if (!token) {
8186
+ console.error("Usage: board-live-cards source-data-fetch-failure --token <sourceToken> [--reason <msg>]");
8187
+ process.exit(1);
8238
8188
  }
8189
+ const payload = deps.decodeSourceToken(token);
8190
+ if (!payload) {
8191
+ console.error("Invalid source token");
8192
+ process.exit(1);
8193
+ }
8194
+ const { cbk, rg, cid, b, d, cs } = payload;
8195
+ console.log(`[source-data-fetch-failure] ${cid}.${b}: ${reason}`);
8196
+ const cbkDecoded = deps.decodeCallbackToken(cbk);
8197
+ if (!cbkDecoded) {
8198
+ console.error("Invalid callback token embedded in source token");
8199
+ process.exit(1);
8200
+ }
8201
+ const timestamp = (/* @__PURE__ */ new Date()).toISOString();
8202
+ deps.appendEventToJournal(rg, {
8203
+ type: "task-progress",
8204
+ taskName: cbkDecoded.taskName,
8205
+ update: { bindTo: b, outputFile: d, failure: true, reason, sourceChecksum: cs },
8206
+ timestamp
8207
+ });
8208
+ void deps.processAccumulatedEventsInfinitePass(rg);
8209
+ }
8210
+ function cmdTaskProgress(args) {
8211
+ const rgIdx = args.indexOf("--rg");
8212
+ const tokenIdx = args.indexOf("--token");
8213
+ const updateIdx = args.indexOf("--update");
8214
+ const dir = rgIdx !== -1 ? args[rgIdx + 1] : void 0;
8215
+ const token = tokenIdx !== -1 ? args[tokenIdx + 1] : void 0;
8216
+ const updateJson = updateIdx !== -1 ? args[updateIdx + 1] : "{}";
8217
+ if (!dir || !token) {
8218
+ console.error("Usage: board-live-cards task-progress --rg <dir> --token <token> [--update <json>]");
8219
+ process.exit(1);
8220
+ }
8221
+ const decoded = deps.decodeCallbackToken(token);
8222
+ if (!decoded) {
8223
+ console.error("Invalid callback token");
8224
+ process.exit(1);
8225
+ }
8226
+ const update = updateJson ? JSON.parse(updateJson) : {};
8227
+ deps.appendEventToJournal(dir, {
8228
+ type: "task-progress",
8229
+ taskName: decoded.taskName,
8230
+ update,
8231
+ timestamp: (/* @__PURE__ */ new Date()).toISOString()
8232
+ });
8233
+ void deps.processAccumulatedEventsInfinitePass(dir);
8239
8234
  }
8240
- fs.mkdirSync(dir, { recursive: true });
8241
- const live = createLiveGraph(EMPTY_CONFIG);
8242
- const snap = snapshot(live);
8243
- const envelope = { lastDrainedJournalId: "", graph: snap };
8244
- fs.writeFileSync(boardPath, JSON.stringify(envelope, null, 2));
8245
- return "created";
8246
- }
8247
- function loadBoardEnvelope(dir) {
8248
- const raw = fs.readFileSync(path.join(dir, BOARD_FILE), "utf-8");
8249
- return JSON.parse(raw);
8250
- }
8251
- function loadBoard(dir) {
8252
- const envelope = loadBoardEnvelope(dir);
8253
- return restore(envelope.graph);
8254
- }
8255
- function saveBoard(dir, rg, journal) {
8256
- const snap = rg.snapshot();
8257
- const envelope = {
8258
- lastDrainedJournalId: journal.lastDrainedJournalId,
8259
- graph: snap
8235
+ return {
8236
+ cmdTaskCompleted,
8237
+ cmdTaskFailed,
8238
+ cmdTaskProgress,
8239
+ cmdSourceDataFetched,
8240
+ cmdSourceDataFetchFailure
8260
8241
  };
8261
- writeJsonAtomic(path.join(dir, BOARD_FILE), envelope);
8262
- const live = restore(snap);
8263
- const statusObject = buildBoardStatusObject(dir, live);
8264
- writeJsonAtomic(resolveStatusSnapshotPath(dir), statusObject);
8265
8242
  }
8266
- function runtimeOutConfigPath(boardDir) {
8267
- return path.join(boardDir, RUNTIME_OUT_FILE);
8268
- }
8269
- function resolveConfiguredRuntimeOutDir(boardDir) {
8270
- const cfgPath = runtimeOutConfigPath(boardDir);
8271
- if (fs.existsSync(cfgPath)) {
8272
- const configured = fs.readFileSync(cfgPath, "utf-8").trim();
8273
- if (configured) {
8274
- return path.isAbsolute(configured) ? configured : path.resolve(boardDir, configured);
8243
+ function createNonCoreCommandHandlers(deps) {
8244
+ function cmdRunSourceFetch(args) {
8245
+ const inIdx = args.indexOf("--in");
8246
+ const outIdx = args.indexOf("--out");
8247
+ const errIdx = args.indexOf("--err");
8248
+ const inFile = inIdx !== -1 ? args[inIdx + 1] : void 0;
8249
+ const outFile = outIdx !== -1 ? args[outIdx + 1] : void 0;
8250
+ const errFile = errIdx !== -1 ? args[errIdx + 1] : void 0;
8251
+ if (!inFile || !outFile) {
8252
+ console.error("Usage: board-live-cards run-source-fetch --in <source.json> --out <result.json> [--err <error.txt>]");
8253
+ process.exit(1);
8254
+ }
8255
+ if (!fs7.existsSync(inFile)) {
8256
+ const msg = `Input file not found: ${inFile}`;
8257
+ if (errFile) fs7.writeFileSync(errFile, msg);
8258
+ console.error(`[run-source-fetch] ${msg}`);
8259
+ process.exit(1);
8260
+ }
8261
+ let source;
8262
+ try {
8263
+ const raw = fs7.readFileSync(inFile, "utf-8");
8264
+ source = JSON.parse(raw);
8265
+ } catch (err) {
8266
+ const msg = `Failed to parse input file: ${err.message}`;
8267
+ if (errFile) fs7.writeFileSync(errFile, msg);
8268
+ console.error(`[run-source-fetch] ${msg}`);
8269
+ process.exit(1);
8270
+ }
8271
+ if (!source.cli) {
8272
+ const msg = "Source definition missing cli field (board-live-cards built-in executor only understands source.cli)";
8273
+ if (errFile) fs7.writeFileSync(errFile, msg);
8274
+ console.error(`[run-source-fetch] ${msg}`);
8275
+ process.exit(1);
8276
+ }
8277
+ console.log(`[run-source-fetch] executing: ${source.cli}`);
8278
+ const timeout = source.timeout ?? 12e4;
8279
+ const sourceCwd = typeof source.cwd === "string" ? source.cwd : process.cwd();
8280
+ const sourceBoardDir = typeof source.boardDir === "string" ? source.boardDir : void 0;
8281
+ const cmdParts = deps.splitCommandLine(source.cli);
8282
+ if (cmdParts.length === 0) {
8283
+ const msg = "Source cli command is empty";
8284
+ if (errFile) fs7.writeFileSync(errFile, msg);
8285
+ console.error(`[run-source-fetch] ${msg}`);
8286
+ process.exit(1);
8287
+ }
8288
+ const rawCmd = cmdParts[0];
8289
+ const { cmd, args: cliArgs } = deps.resolveCommandInvocation(rawCmd, cmdParts.slice(1));
8290
+ let stdout;
8291
+ try {
8292
+ stdout = deps.execCommandSync(cmd, cliArgs, {
8293
+ shell: false,
8294
+ encoding: "utf-8",
8295
+ timeout,
8296
+ cwd: sourceCwd,
8297
+ env: {
8298
+ ...process.env,
8299
+ ...sourceBoardDir ? { BOARD_DIR: sourceBoardDir } : {}
8300
+ }
8301
+ });
8302
+ } catch (err) {
8303
+ const msg = err.message ?? String(err);
8304
+ console.error(`[run-source-fetch] cli failed: ${msg}`);
8305
+ if (errFile) fs7.writeFileSync(errFile, msg);
8306
+ process.exit(1);
8307
+ }
8308
+ const result = stdout.trim();
8309
+ try {
8310
+ fs7.writeFileSync(outFile, result);
8311
+ console.log(`[run-source-fetch] result written to ${outFile}`);
8312
+ } catch (err) {
8313
+ const msg = `Failed to write output file: ${err.message}`;
8314
+ console.error(`[run-source-fetch] ${msg}`);
8315
+ if (errFile) fs7.writeFileSync(errFile, msg);
8316
+ process.exit(1);
8275
8317
  }
8276
8318
  }
8277
- const defaultDir = path.join(boardDir, DEFAULT_RUNTIME_OUT_DIR);
8278
- fs.writeFileSync(cfgPath, defaultDir, "utf-8");
8279
- return defaultDir;
8280
- }
8281
- function configureRuntimeOutDir(boardDir, runtimeOut) {
8282
- let resolved;
8283
- if (runtimeOut) {
8284
- resolved = path.isAbsolute(runtimeOut) ? runtimeOut : path.resolve(boardDir, runtimeOut);
8285
- } else {
8286
- resolved = path.join(boardDir, DEFAULT_RUNTIME_OUT_DIR);
8287
- }
8288
- fs.mkdirSync(resolved, { recursive: true });
8289
- fs.writeFileSync(runtimeOutConfigPath(boardDir), resolved, "utf-8");
8290
- return resolved;
8291
- }
8292
- function resolveStatusSnapshotPath(boardDir) {
8293
- return path.join(resolveConfiguredRuntimeOutDir(boardDir), RUNTIME_STATUS_FILE);
8294
- }
8295
- function resolveComputedValuesPath(boardDir, cardId) {
8296
- return path.join(resolveConfiguredRuntimeOutDir(boardDir), RUNTIME_CARDS_DIR, `${cardId}.computed.json`);
8297
- }
8298
- function resolveDataObjectsDirPath(boardDir) {
8299
- return path.join(resolveConfiguredRuntimeOutDir(boardDir), RUNTIME_DATA_OBJECTS_DIR);
8300
- }
8301
- function toDataObjectFileName(token) {
8302
- return token.replace(/[\\/]/g, "__");
8303
- }
8304
- function writeRuntimeDataObjects(boardDir, data) {
8305
- for (const [token, payload] of Object.entries(data)) {
8306
- if (!token) continue;
8307
- const fileName = toDataObjectFileName(token);
8308
- if (!fileName) continue;
8309
- const filePath = path.join(resolveDataObjectsDirPath(boardDir), fileName);
8310
- writeJsonAtomic(filePath, payload);
8311
- }
8312
- }
8313
- function writeJsonAtomic(filePath, payload) {
8314
- fs.mkdirSync(path.dirname(filePath), { recursive: true });
8315
- const tmpPath = `${filePath}.${process.pid}.${randomUUID()}.tmp`;
8316
- fs.writeFileSync(tmpPath, JSON.stringify(payload, null, 2), "utf-8");
8317
- fs.renameSync(tmpPath, filePath);
8318
- }
8319
- function withBoardLock(boardDir, fn) {
8320
- const boardPath = path.join(boardDir, BOARD_FILE);
8321
- const release = lockSync(boardPath, { retries: { retries: 5, minTimeout: 100 } });
8322
- try {
8323
- return fn();
8324
- } finally {
8325
- release();
8319
+ async function cmdProbeSource(args) {
8320
+ const cardIdx = args.indexOf("--card");
8321
+ const sourceIdxArg = args.indexOf("--source-idx");
8322
+ const sourceBindArg = args.indexOf("--source-bind");
8323
+ const mockProjectionsIdx = args.indexOf("--mock-projections");
8324
+ const rgIdx = args.indexOf("--rg");
8325
+ const outIdx = args.indexOf("--out");
8326
+ const cardFilePath = cardIdx !== -1 ? args[cardIdx + 1] : void 0;
8327
+ const sourceIdxVal = sourceIdxArg !== -1 ? parseInt(args[sourceIdxArg + 1], 10) : 0;
8328
+ const sourceBindVal = sourceBindArg !== -1 ? args[sourceBindArg + 1] : void 0;
8329
+ const mockProjectionsRaw = mockProjectionsIdx !== -1 ? args[mockProjectionsIdx + 1] : void 0;
8330
+ const boardDirArg = rgIdx !== -1 ? args[rgIdx + 1] : void 0;
8331
+ const outFile = outIdx !== -1 ? args[outIdx + 1] : void 0;
8332
+ if (!cardFilePath) {
8333
+ console.error("Usage: board-live-cards probe-source --card <card.json> [--source-idx <n>] [--source-bind <name>] [--mock-projections <json>] [--rg <boardDir>] [--out <result.json>]");
8334
+ process.exit(1);
8335
+ }
8336
+ let card;
8337
+ try {
8338
+ card = JSON.parse(fs7.readFileSync(path7.resolve(cardFilePath), "utf-8"));
8339
+ } catch (e) {
8340
+ console.error(`[probe-source] Cannot read card: ${e.message}`);
8341
+ process.exit(1);
8342
+ }
8343
+ const source_defs = card.source_defs ?? [];
8344
+ if (source_defs.length === 0) {
8345
+ console.error(`[probe-source] Card "${card.id}" has no source_defs`);
8346
+ process.exit(1);
8347
+ }
8348
+ let sourceIdx;
8349
+ if (sourceBindVal) {
8350
+ sourceIdx = source_defs.findIndex((s) => s.bindTo === sourceBindVal);
8351
+ if (sourceIdx === -1) {
8352
+ console.error(`[probe-source] No source with bindTo="${sourceBindVal}" in card "${card.id}"`);
8353
+ process.exit(1);
8354
+ }
8355
+ } else {
8356
+ sourceIdx = sourceIdxVal;
8357
+ if (isNaN(sourceIdx) || sourceIdx < 0 || sourceIdx >= source_defs.length) {
8358
+ console.error(`[probe-source] --source-idx ${sourceIdxVal} out of range (card has ${source_defs.length} source(s))`);
8359
+ process.exit(1);
8360
+ }
8361
+ }
8362
+ const sourceDef = source_defs[sourceIdx];
8363
+ const cardDir = path7.resolve(path7.dirname(cardFilePath));
8364
+ const boardDir = boardDirArg ? path7.resolve(boardDirArg) : cardDir;
8365
+ let mockProjections = {};
8366
+ if (mockProjectionsRaw) {
8367
+ const raw = mockProjectionsRaw.startsWith("@") ? fs7.readFileSync(path7.resolve(mockProjectionsRaw.slice(1)), "utf-8") : mockProjectionsRaw;
8368
+ try {
8369
+ mockProjections = JSON.parse(raw);
8370
+ } catch (e) {
8371
+ console.error(`[probe-source] --mock-projections is not valid JSON: ${e.message}`);
8372
+ process.exit(1);
8373
+ }
8374
+ }
8375
+ const teConfig = deps.readTaskExecutorConfig(boardDir);
8376
+ const taskExecutor = teConfig?.command;
8377
+ const taskExecutorExtraB64 = teConfig?.extra ? Buffer.from(JSON.stringify(teConfig.extra)).toString("base64") : void 0;
8378
+ const inPayload = {
8379
+ ...sourceDef,
8380
+ cwd: typeof sourceDef.cwd === "string" && sourceDef.cwd ? sourceDef.cwd : cardDir,
8381
+ boardDir: typeof sourceDef.boardDir === "string" && sourceDef.boardDir ? sourceDef.boardDir : boardDir,
8382
+ _projections: mockProjections
8383
+ };
8384
+ let sourceKind = "unknown";
8385
+ if (taskExecutor) {
8386
+ try {
8387
+ const capRaw = deps.execCommandSync(taskExecutor, ["describe-capabilities"], {
8388
+ shell: true,
8389
+ timeout: 8e3,
8390
+ encoding: "utf-8"
8391
+ });
8392
+ const caps = JSON.parse(String(capRaw));
8393
+ const knownKinds = caps?.sourceKinds ? Object.keys(caps.sourceKinds) : [];
8394
+ const defKeys = new Set(Object.keys(sourceDef));
8395
+ sourceKind = knownKinds.find((k) => defKeys.has(k)) ?? "unknown";
8396
+ } catch {
8397
+ }
8398
+ }
8399
+ console.log(`[probe-source] card: ${card.id}`);
8400
+ console.log(`[probe-source] source[${sourceIdx}]: bindTo="${sourceDef.bindTo}" kind=${sourceKind}`);
8401
+ console.log(`[probe-source] _projections: ${JSON.stringify(mockProjections)}`);
8402
+ console.log(`[probe-source] executor: ${taskExecutor ?? "built-in (source.cli only)"}`);
8403
+ console.log("[probe-source] running fetch...");
8404
+ const ts = Date.now();
8405
+ const inFile = path7.join(os3.tmpdir(), `probe-in-${sourceDef.bindTo}-${ts}.json`);
8406
+ const tmpOut = path7.join(os3.tmpdir(), `probe-out-${sourceDef.bindTo}-${ts}.json`);
8407
+ const errFile = path7.join(os3.tmpdir(), `probe-err-${sourceDef.bindTo}-${ts}.txt`);
8408
+ fs7.writeFileSync(inFile, JSON.stringify(inPayload, null, 2), "utf-8");
8409
+ let passed = false;
8410
+ let errorMsg;
8411
+ let resultRaw;
8412
+ try {
8413
+ if (taskExecutor) {
8414
+ const executorArgs = ["run-source-fetch", "--in", inFile, "--out", tmpOut, "--err", errFile];
8415
+ if (taskExecutorExtraB64) executorArgs.push("--extra", taskExecutorExtraB64);
8416
+ deps.execCommandSync(taskExecutor, executorArgs, {
8417
+ shell: true,
8418
+ timeout: sourceDef.timeout ?? 3e4
8419
+ });
8420
+ } else {
8421
+ if (!inPayload.cli) {
8422
+ throw new Error("No task-executor registered and source has no cli field \u2014 cannot probe with built-in executor");
8423
+ }
8424
+ const cmdParts = deps.splitCommandLine(inPayload.cli);
8425
+ const rawCmd = cmdParts[0];
8426
+ const { cmd, args: cliArgs } = deps.resolveCommandInvocation(rawCmd, cmdParts.slice(1));
8427
+ const stdout = deps.execCommandSync(cmd, cliArgs, {
8428
+ shell: false,
8429
+ encoding: "utf-8",
8430
+ timeout: sourceDef.timeout ?? 3e4,
8431
+ cwd: inPayload.cwd
8432
+ });
8433
+ fs7.writeFileSync(tmpOut, String(stdout).trim(), "utf-8");
8434
+ }
8435
+ passed = fs7.existsSync(tmpOut);
8436
+ if (passed) {
8437
+ resultRaw = fs7.readFileSync(tmpOut, "utf-8");
8438
+ } else {
8439
+ errorMsg = fs7.existsSync(errFile) ? fs7.readFileSync(errFile, "utf-8").trim() : "executor produced no output file";
8440
+ }
8441
+ } catch (e) {
8442
+ errorMsg = e.message ?? String(e);
8443
+ if (!errorMsg && fs7.existsSync(errFile)) {
8444
+ errorMsg = fs7.readFileSync(errFile, "utf-8").trim();
8445
+ }
8446
+ }
8447
+ for (const f of [inFile, errFile]) {
8448
+ try {
8449
+ fs7.unlinkSync(f);
8450
+ } catch {
8451
+ }
8452
+ }
8453
+ if (passed && resultRaw !== void 0) {
8454
+ const resultSize = resultRaw.length;
8455
+ const sample = resultRaw.slice(0, 300);
8456
+ console.log("[probe-source] STATUS: PROBE_PASS");
8457
+ console.log(`[probe-source] result size: ${resultSize} bytes`);
8458
+ console.log(`[probe-source] sample: ${sample}${resultSize > 300 ? "..." : ""}`);
8459
+ if (outFile) {
8460
+ fs7.writeFileSync(path7.resolve(outFile), resultRaw);
8461
+ console.log(`[probe-source] result written to: ${outFile}`);
8462
+ } else {
8463
+ try {
8464
+ fs7.unlinkSync(tmpOut);
8465
+ } catch {
8466
+ }
8467
+ }
8468
+ } else {
8469
+ console.log("[probe-source] STATUS: PROBE_FAIL");
8470
+ if (errorMsg) console.log(`[probe-source] error: ${errorMsg}`);
8471
+ try {
8472
+ if (fs7.existsSync(tmpOut)) fs7.unlinkSync(tmpOut);
8473
+ } catch {
8474
+ }
8475
+ }
8476
+ const summary = {
8477
+ status: passed ? "PROBE_PASS" : "PROBE_FAIL",
8478
+ cardId: card.id,
8479
+ sourceIdx,
8480
+ bindTo: sourceDef.bindTo,
8481
+ sourceKind,
8482
+ mockProjectionsKeys: Object.keys(mockProjections),
8483
+ resultSizeBytes: resultRaw !== void 0 ? resultRaw.length : 0,
8484
+ error: errorMsg ?? void 0
8485
+ };
8486
+ console.log(`[probe-source:result] ${JSON.stringify(summary)}`);
8487
+ process.exit(passed ? 0 : 1);
8488
+ }
8489
+ function cmdDescribeTaskExecutorCapabilities(args) {
8490
+ const rgIdx = args.indexOf("--rg");
8491
+ const boardDir = rgIdx !== -1 ? path7.resolve(args[rgIdx + 1]) : void 0;
8492
+ if (!boardDir) {
8493
+ console.error("Usage: board-live-cards describe-task-executor-capabilities --rg <dir>");
8494
+ process.exit(1);
8495
+ }
8496
+ const teConfig = deps.readTaskExecutorConfig(boardDir);
8497
+ if (!teConfig) {
8498
+ console.error(`[describe-task-executor-capabilities] No .task-executor registered in ${boardDir}`);
8499
+ process.exit(1);
8500
+ }
8501
+ try {
8502
+ const stdout = deps.execCommandSync(teConfig.command, ["describe-capabilities"], {
8503
+ shell: true,
8504
+ timeout: 1e4,
8505
+ encoding: "utf-8"
8506
+ });
8507
+ process.stdout.write(String(stdout));
8508
+ if (!String(stdout).endsWith("\n")) process.stdout.write("\n");
8509
+ } catch (e) {
8510
+ console.error(`[describe-task-executor-capabilities] Executor failed: ${e.message ?? e}`);
8511
+ process.exit(1);
8512
+ }
8326
8513
  }
8327
- }
8328
- function decodeCallbackToken2(token) {
8329
- try {
8330
- const payload = JSON.parse(Buffer.from(token, "base64url").toString());
8331
- if (typeof payload?.t === "string") return { taskName: payload.t };
8332
- return null;
8333
- } catch {
8334
- return null;
8514
+ function cmdHelp() {
8515
+ console.log(`
8516
+ board-live-cards-cli \u2014 LiveCards board CLI
8517
+
8518
+ USAGE
8519
+ board-live-cards-cli <command> [options]
8520
+
8521
+ BOARD MANAGEMENT
8522
+ init <dir> [--task-executor <script>] [--chat-handler <script>] [--inference-adapter <script>] [--runtime-out <dir>]
8523
+ Create a new board in <dir>.
8524
+ If --task-executor is given, writes <dir>/.task-executor with the script path.
8525
+ If --chat-handler is given, writes <dir>/.chat-handler with the script path.
8526
+ If --inference-adapter is given, writes <dir>/.inference-adapter with the script path.
8527
+ Writes <dir>/.runtime-out (default: <dir>/runtime-out).
8528
+ Published runtime files:
8529
+ <runtime-out>/board-livegraph-status.json
8530
+ <runtime-out>/cards/<card-id>.computed.json
8531
+ Re-running init on an existing board is safe; handler registrations are updated.
8532
+
8533
+ status --rg <dir> [--json]
8534
+ Read and print the published status snapshot from <runtime-out>/board-livegraph-status.json.
8535
+ --json emits the stable machine-readable status object.
8536
+
8537
+ CARD MANAGEMENT
8538
+ upsert-card --rg <dir> (--card <card.json> | --card-glob <glob>) [--card-id <card-id>] [--restart]
8539
+ Insert or update one or many cards.
8540
+ Enforces strict one-to-one mapping between card id and file path:
8541
+ - same id + same file path: update
8542
+ - new id + new file path: insert
8543
+ - id remap or file remap: rejected
8544
+ If --card-id is provided, it must match the id inside the file.
8545
+ --card-id is valid only with --card (single file), not with --card-glob.
8546
+ --restart clears the task so it re-triggers from scratch.
8547
+
8548
+ validate-card (--card <card.json> | --card-glob <glob>) [--rg <boardDir>]
8549
+ Validate one or many card JSON files without adding them to a board.
8550
+ Checks JSON Schema structure, runtime expression syntax, and provides.ref namespaces.
8551
+ When --rg is provided, also invokes the board's task executor validate-source-def
8552
+ subcommand to structurally validate each source definition against supported kinds.
8553
+ Exits with code 1 if any card fails validation.
8554
+
8555
+ remove-card --rg <dir> --id <card-id>
8556
+ Remove a card and its task from the board.
8557
+
8558
+ retrigger --rg <dir> --task <task-name>
8559
+ Mark a task not-started and drain to re-trigger it.
8560
+
8561
+ TASK CALLBACKS (called by task executor scripts)
8562
+ task-completed --token <callbackToken> [--data <json>]
8563
+ Signal successful task completion with optional JSON result data.
8564
+
8565
+ task-failed --token <callbackToken> [--error <message>]
8566
+ Signal task failure with an optional error message.
8567
+
8568
+ task-progress --rg <dir> --token <callbackToken> [--update <json>]
8569
+ Signal task progress with optional update payload (for waiting on more evidence, etc.).
8570
+
8571
+ SOURCE CALLBACKS (called internally by run-sourcedefs-internal)
8572
+ source-data-fetched --tmp <file> --token <sourceToken>
8573
+ Atomically rename <file> into the outputFile destination and record delivery
8574
+ via journal events. Appends a task-progress event to re-invoke the card handler.
8575
+
8576
+ source-data-fetch-failure --token <sourceToken> [--reason <message>]
8577
+ Record a source fetch failure via journal events and append a task-progress event.
8578
+
8579
+ INTERNAL COMMANDS
8580
+ process-accumulated-events --rg <dir>
8581
+ Executes forced drain for this board.
8582
+ This command is also used as the background relay worker.
8583
+ By default it schedules a detached worker and returns quickly.
8584
+ Internal workers run with --inline-loop to perform the settle loop.
8585
+
8586
+ Eventual-progress guarantee is relay-based (not per-call blocking guarantee):
8587
+ 1) at least one runner continues processing,
8588
+ 2) no crash/forced exit in relay window,
8589
+ 3) lock stays healthy,
8590
+ 4) event production eventually quiesces.
8591
+
8592
+ run-sourcedefs-internal --card <card.json> --token <callbackToken> --rg <dir>
8593
+ Execute all source[] entries for a card, then report delivery or failure.
8594
+ (Internal command \u2014 invoked by the card-handler. Not intended for direct use.)
8595
+
8596
+ If <dir>/.task-executor exists, invokes it with run-source-fetch subcommand:
8597
+ <executor> run-source-fetch --in <source_json> --out <outfile> --err <errfile>
8598
+
8599
+ If no .task-executor is registered, uses board-live-cards built-in run-source-fetch.
8600
+
8601
+ run-source-fetch --in <source.json> --out <result.json> [--err <error.txt>]
8602
+ Execute a source definition. Board-live-cards reads source.cli and executes it.
8603
+ Writes result to --out. Presence of --out after exit indicates success.
8604
+
8605
+ describe-task-executor-capabilities --rg <dir>
8606
+ Invoke the registered task-executor's describe-capabilities subcommand and
8607
+ print its capabilities JSON to stdout. Requires a .task-executor file in <dir>.
8608
+
8609
+ probe-source --card <card.json> [--source-idx <n>] [--source-bind <name>]
8610
+ [--mock-projections <json>] [--rg <boardDir>] [--out <result.json>]
8611
+ Validate that a card source can be fetched successfully.
8612
+ Reads the card file, extracts the chosen source (default: index 0), builds the
8613
+ run-source-fetch --in payload with the supplied _projections data, invokes the
8614
+ registered task-executor (or built-in executor for source.cli), and reports pass/fail.
8615
+ --mock-projections: JSON string (or @file.json) providing pre-resolved _projections values
8616
+ the source needs. Craft the minimal payload that exercises the
8617
+ source \u2014 e.g. '{"holdings":[{"ticker":"AAPL","quantity":10}]}'.
8618
+ If omitted, _projections is passed as empty ({}).
8619
+ --source-idx: 0-based index into card.source_defs[]. Default: 0.
8620
+ --source-bind: Select source by its bindTo name instead of index.
8621
+ --rg: Board directory used to find .task-executor. Defaults to the
8622
+ directory containing the card file.
8623
+ --out: Optional path to write the raw fetch result JSON.
8624
+ Prints a structured report ending with a [probe-source:result] JSON line.
8625
+ Exits 0 on PROBE_PASS, 1 on PROBE_FAIL.
8626
+
8627
+ run-inference-internal --in <input.json> --token <inferenceToken>
8628
+ Execute inference via registered .inference-adapter and forward result to inference-done.
8629
+ inferenceToken encodes boardDir (rg), cardId (cid), callbackToken (cbk), checksum (cs).
8630
+ (Internal command \u2014 invoked by the card-handler when custom completion rule is used.)
8631
+
8632
+ inference-done --tmp <result.json> --token <inferenceToken>
8633
+ Persist llm_task_completion_inference on the card and append a task-progress event.
8634
+ Reads boardDir/callbackToken/checksum from decoded inferenceToken; deletes --tmp file after reading.
8635
+ (Internal command \u2014 invoked by run-inference-internal.)
8636
+
8637
+ RUN-SOURCE-FETCH PROTOCOL
8638
+ External task-executors implement:
8639
+ <executor> run-source-fetch --in <source.json> --out <result.json> [--err <error.txt>]
8640
+
8641
+ INPUT: --in file contains the full source_defs[x] definition object
8642
+ OUTPUT: --out file is written with the result to signal success.
8643
+ --err file may be written to explain failure.
8644
+
8645
+ Exit code and --out presence determine success:
8646
+ Exit 0 + --out file present \u2192 source delivery recorded, card re-evaluated.
8647
+ Exit non-zero OR --out absent \u2192 source-data-fetch-failure recorded.
8648
+
8649
+ BOARD-LIVE-CARDS BUILT-IN EXECUTOR
8650
+ Understands source.cli field only:
8651
+ "source_defs": [{ "cli": "node ../fetch-prices.js", "bindTo": "prices", "outputFile": "prices.json" }]
8652
+
8653
+ The source.cli command is executed with:
8654
+ - Direct command invocation (no shell; quote-aware argument parsing)
8655
+ - Stdout is captured and delivered to the card as-is
8656
+ - Timeout from source.timeout (default 120s)
8657
+
8658
+ The source.cli command must:
8659
+ - Execute successfully (exit 0)
8660
+ - Write output to stdout
8661
+ - Complete within the timeout
8662
+
8663
+ The output format is the concern of the card's compute function to interpret.
8664
+
8665
+ External task-executors can interpret source definitions however they want.
8666
+
8667
+ EXAMPLES
8668
+ board-live-cards-cli init ./my-board
8669
+ board-live-cards-cli init ./my-board --task-executor ./executors/my-runner.py
8670
+ board-live-cards-cli upsert-card --rg ./my-board --card cards/prices.json
8671
+ board-live-cards-cli status --rg ./my-board
8672
+ board-live-cards-cli retrigger --rg ./my-board --task price-fetch
8673
+ board-live-cards-cli probe-source --card cards/card-market-prices.json --source-idx 0 --rg ./my-board --mock-projections '{"holdings":[{"ticker":"AAPL","quantity":10}]}'
8674
+ `.trimStart());
8335
8675
  }
8676
+ return {
8677
+ cmdHelp,
8678
+ cmdRunSourceFetch,
8679
+ cmdProbeSource,
8680
+ cmdDescribeTaskExecutorCapabilities
8681
+ };
8336
8682
  }
8337
- function encodeSourceToken(payload) {
8338
- return Buffer.from(JSON.stringify(payload)).toString("base64url");
8339
- }
8340
- function decodeSourceToken(token) {
8341
- try {
8342
- const p = JSON.parse(Buffer.from(token, "base64url").toString());
8343
- if (typeof p?.cbk === "string" && typeof p?.cid === "string" && typeof p?.b === "string" && typeof p?.d === "string") {
8344
- return p;
8683
+ function createCardCommandHandlers(deps) {
8684
+ function cmdUpsertCard(args) {
8685
+ const rgIdx = args.indexOf("--rg");
8686
+ const cardIdx = args.indexOf("--card");
8687
+ const globIdx = args.indexOf("--card-glob");
8688
+ const cardIdIdx = args.indexOf("--card-id");
8689
+ const restart = args.includes("--restart");
8690
+ const dir = rgIdx !== -1 ? args[rgIdx + 1] : void 0;
8691
+ const cardFile = cardIdx !== -1 ? args[cardIdx + 1] : void 0;
8692
+ const cardGlob = globIdx !== -1 ? args[globIdx + 1] : void 0;
8693
+ const requestedCardId = cardIdIdx !== -1 ? args[cardIdIdx + 1] : void 0;
8694
+ if (!dir || !cardFile && !cardGlob || cardFile && cardGlob) {
8695
+ console.error("Usage: board-live-cards upsert-card --rg <dir> (--card <card.json> | --card-glob <glob>) [--card-id <card-id>] [--restart]");
8696
+ process.exit(1);
8697
+ }
8698
+ if (cardGlob && requestedCardId) {
8699
+ console.error("Usage: --card-id may be used only with --card (single file), not with --card-glob");
8700
+ process.exit(1);
8701
+ }
8702
+ const cardFiles = cardFile ? [path7.resolve(cardFile)] : deps.resolveCardGlobMatches(cardGlob);
8703
+ if (!cardFile && cardFiles.length === 0) {
8704
+ console.error(`No card files matched glob: ${cardGlob}`);
8705
+ process.exit(1);
8706
+ }
8707
+ const idx = deps.buildCardInventoryIndex(dir);
8708
+ const batchByCardId = /* @__PURE__ */ new Map();
8709
+ const batchByCardPath = /* @__PURE__ */ new Map();
8710
+ const plans = [];
8711
+ const logs = [];
8712
+ for (const absCardPath of cardFiles) {
8713
+ if (!fs7.existsSync(absCardPath)) {
8714
+ console.error(`Card file not found: ${absCardPath}`);
8715
+ process.exit(1);
8716
+ }
8717
+ const card = JSON.parse(fs7.readFileSync(absCardPath, "utf-8"));
8718
+ if (!card.id) {
8719
+ console.error(`Card JSON must have an "id" field (${absCardPath})`);
8720
+ process.exit(1);
8721
+ }
8722
+ if (requestedCardId && requestedCardId !== card.id) {
8723
+ console.error(
8724
+ `Card id mismatch: --card-id "${requestedCardId}" does not match file id "${card.id}" (${absCardPath})`
8725
+ );
8726
+ process.exit(1);
8727
+ }
8728
+ const seenPathCardId = batchByCardPath.get(absCardPath);
8729
+ if (seenPathCardId && seenPathCardId !== card.id) {
8730
+ console.error(
8731
+ `Upsert rejected: file "${absCardPath}" appears multiple times in batch with conflicting ids ("${seenPathCardId}" vs "${card.id}")`
8732
+ );
8733
+ process.exit(1);
8734
+ }
8735
+ const seenCardPath = batchByCardId.get(card.id);
8736
+ if (seenCardPath && seenCardPath !== absCardPath) {
8737
+ console.error(
8738
+ `Upsert rejected: card id "${card.id}" appears multiple times in batch with conflicting files ("${seenCardPath}" vs "${absCardPath}")`
8739
+ );
8740
+ process.exit(1);
8741
+ }
8742
+ const existingById = idx.byCardId.get(card.id);
8743
+ const existingByPath = idx.byCardPath.get(absCardPath);
8744
+ if (existingByPath && existingByPath.cardId !== card.id) {
8745
+ console.error(
8746
+ `Upsert rejected: file "${absCardPath}" is already mapped to card id "${existingByPath.cardId}", cannot remap to "${card.id}"`
8747
+ );
8748
+ process.exit(1);
8749
+ }
8750
+ if (existingById && existingById.cardFilePath !== absCardPath) {
8751
+ console.error(
8752
+ `Upsert rejected: card id "${card.id}" is already mapped to file "${existingById.cardFilePath}", cannot remap to "${absCardPath}"`
8753
+ );
8754
+ process.exit(1);
8755
+ }
8756
+ batchByCardPath.set(absCardPath, card.id);
8757
+ batchByCardId.set(card.id, absCardPath);
8758
+ plans.push({
8759
+ card,
8760
+ absCardPath,
8761
+ isInsert: !existingById
8762
+ });
8763
+ }
8764
+ for (const plan of plans) {
8765
+ const { card, absCardPath, isInsert } = plan;
8766
+ if (isInsert) {
8767
+ const newEntry = {
8768
+ cardId: card.id,
8769
+ cardFilePath: absCardPath,
8770
+ addedAt: (/* @__PURE__ */ new Date()).toISOString()
8771
+ };
8772
+ deps.appendCardInventory(dir, newEntry);
8773
+ idx.byCardId.set(card.id, newEntry);
8774
+ idx.byCardPath.set(absCardPath, newEntry);
8775
+ }
8776
+ const taskConfig = deps.liveCardToTaskConfig(card);
8777
+ deps.appendEventToJournal(dir, {
8778
+ type: "task-upsert",
8779
+ taskName: card.id,
8780
+ taskConfig,
8781
+ timestamp: (/* @__PURE__ */ new Date()).toISOString()
8782
+ });
8783
+ if (restart) {
8784
+ deps.appendEventToJournal(dir, {
8785
+ type: "task-restart",
8786
+ taskName: card.id,
8787
+ timestamp: (/* @__PURE__ */ new Date()).toISOString()
8788
+ });
8789
+ }
8790
+ logs.push(`Card "${card.id}" ${isInsert ? "upserted (inserted)" : "upserted (updated)"}${restart ? " (restarted)" : ""}.`);
8791
+ }
8792
+ void deps.processAccumulatedEventsInfinitePass(dir);
8793
+ if (cardGlob) {
8794
+ console.log(`Upserted ${cardFiles.length} cards from glob: ${cardGlob}${restart ? " (restarted)" : ""}`);
8795
+ } else {
8796
+ console.log(logs[0]);
8345
8797
  }
8346
- return null;
8347
- } catch {
8348
- return null;
8349
- }
8350
- }
8351
- function markRequested(entry, requestedAt) {
8352
- entry.lastRequestedAt = requestedAt;
8353
- }
8354
- function markFetchFailed(entry, reason) {
8355
- entry.lastError = reason;
8356
- delete entry.lastFetchedAt;
8357
- }
8358
- function markFetchCompleted(entry, fetchedAt) {
8359
- entry.lastFetchedAt = fetchedAt;
8360
- delete entry.lastError;
8361
- }
8362
- function isSourceInFlight(entry) {
8363
- if (!entry?.lastRequestedAt) return false;
8364
- return !entry.lastFetchedAt || entry.lastFetchedAt < entry.lastRequestedAt;
8365
- }
8366
- function decideSourceAction(entry, queueRequestedAt) {
8367
- if (!entry?.lastRequestedAt) return "dispatch";
8368
- const inFlight = isSourceInFlight(entry);
8369
- if (inFlight) return "in-flight";
8370
- if (!entry.lastFetchedAt) return "dispatch";
8371
- if (entry.lastFetchedAt < queueRequestedAt) return "dispatch";
8372
- return "idle";
8373
- }
8374
- function nextEntryAfterFetchDelivery(entry, fetchedAt) {
8375
- const next = { ...entry };
8376
- markFetchCompleted(next, fetchedAt);
8377
- return next;
8378
- }
8379
- function nextEntryAfterFetchFailure(entry, reason) {
8380
- const next = { ...entry };
8381
- markFetchFailed(next, reason);
8382
- return next;
8383
- }
8384
- function runtimePath(boardDir, cardId) {
8385
- return path.join(boardDir, cardId, "runtime.json");
8386
- }
8387
- function readRuntimeState(boardDir, cardId) {
8388
- const p = runtimePath(boardDir, cardId);
8389
- if (!fs.existsSync(p)) return { _sources: {} };
8390
- try {
8391
- return JSON.parse(fs.readFileSync(p, "utf-8"));
8392
- } catch {
8393
- return { _sources: {} };
8394
8798
  }
8799
+ return { cmdUpsertCard };
8395
8800
  }
8396
- function writeRuntimeState(boardDir, cardId, state) {
8397
- const p = runtimePath(boardDir, cardId);
8398
- fs.mkdirSync(path.dirname(p), { recursive: true });
8399
- fs.writeFileSync(p, JSON.stringify(state, null, 2));
8400
- }
8401
- function appendEventToJournal(boardDir, event) {
8402
- const journalPath = path.join(boardDir, JOURNAL_FILE);
8403
- const entry = { id: randomUUID(), event };
8404
- fs.appendFileSync(journalPath, JSON.stringify(entry) + "\n", "utf-8");
8405
- }
8406
- function getUndrainedEntries(boardDir, lastDrainedId) {
8407
- const journalPath = path.join(boardDir, JOURNAL_FILE);
8408
- if (!fs.existsSync(journalPath)) return [];
8409
- const content = fs.readFileSync(journalPath, "utf-8").trim();
8410
- if (!content) return [];
8411
- const entries = content.split("\n").map((l) => JSON.parse(l));
8412
- if (!lastDrainedId) return entries;
8413
- const idx = entries.findIndex((e) => e.id === lastDrainedId);
8414
- return idx === -1 ? entries : entries.slice(idx + 1);
8415
- }
8416
- function determineLatestPendingAccumulated(boardDir) {
8417
- const boardPath = path.join(boardDir, BOARD_FILE);
8418
- if (!fs.existsSync(boardPath)) return 0;
8419
- try {
8420
- const envelope = loadBoardEnvelope(boardDir);
8421
- return getUndrainedEntries(boardDir, envelope.lastDrainedJournalId).length;
8422
- } catch {
8423
- return 0;
8801
+ function createExecutionCommandHandlers(deps) {
8802
+ function invokeSourceDataFetched(sourceToken, tmpFile, callback) {
8803
+ const { cmd, args } = deps.getCliInvocation("source-data-fetched", ["--tmp", tmpFile, "--token", sourceToken]);
8804
+ deps.execCommandAsync(cmd, args, (err, stdout, stderr) => {
8805
+ if (err) console.error(`[source-data-fetched] call failed:`, err.message);
8806
+ if (stdout) console.log(stdout.trim());
8807
+ if (stderr) console.error(stderr.trim());
8808
+ });
8424
8809
  }
8425
- }
8426
- function shouldUseShellForCommand(cmd, forceShell) {
8427
- if (typeof forceShell === "boolean") return forceShell;
8428
- return process.platform === "win32" && /\.(cmd|bat)$/i.test(cmd);
8429
- }
8430
- var _gitBashPath;
8431
- var GIT_BASH_CACHE_FILE = path.join(os.tmpdir(), ".board-live-cards-git-bash-cache.json");
8432
- function findGitBash() {
8433
- if (_gitBashPath !== void 0) return _gitBashPath;
8434
- if (process.platform !== "win32") return _gitBashPath = false;
8435
- try {
8436
- const cached = JSON.parse(fs.readFileSync(GIT_BASH_CACHE_FILE, "utf8"));
8437
- if (cached.path === false || typeof cached.path === "string" && fs.existsSync(cached.path)) {
8438
- return _gitBashPath = cached.path;
8810
+ function invokeSourceDataFetchFailure(sourceToken, reason, callback) {
8811
+ const { cmd, args } = deps.getCliInvocation("source-data-fetch-failure", ["--token", sourceToken, "--reason", reason]);
8812
+ deps.execCommandAsync(cmd, args, (err) => callback(err));
8813
+ }
8814
+ function cmdRunSources(args) {
8815
+ const cardIdx = args.indexOf("--card");
8816
+ const tokenIdx = args.indexOf("--token");
8817
+ const rgIdx = args.indexOf("--rg");
8818
+ const sourceChecksumsIdx = args.indexOf("--source-checksums");
8819
+ const cardFilePath = cardIdx !== -1 ? args[cardIdx + 1] : void 0;
8820
+ const callbackToken = tokenIdx !== -1 ? args[tokenIdx + 1] : void 0;
8821
+ const boardDir = rgIdx !== -1 ? args[rgIdx + 1] : void 0;
8822
+ const sourceChecksumsJson = sourceChecksumsIdx !== -1 ? args[sourceChecksumsIdx + 1] : void 0;
8823
+ const sourceChecksums = sourceChecksumsJson ? JSON.parse(sourceChecksumsJson) : void 0;
8824
+ if (!cardFilePath || !callbackToken || !boardDir) {
8825
+ console.error("Usage: board-live-cards run-sourcedefs-internal --card <path> --token <token> --rg <dir> [--source-checksums <json>]");
8826
+ process.exit(1);
8439
8827
  }
8440
- } catch {
8441
- }
8442
- const candidates = [
8443
- process.env.SHELL,
8444
- process.env.PROGRAMFILES && path.join(process.env.PROGRAMFILES, "Git", "usr", "bin", "bash.exe"),
8445
- process.env.PROGRAMFILES && path.join(process.env.PROGRAMFILES, "Git", "bin", "bash.exe"),
8446
- process.env["PROGRAMFILES(X86)"] && path.join(process.env["PROGRAMFILES(X86)"], "Git", "bin", "bash.exe"),
8447
- process.env.LOCALAPPDATA && path.join(process.env.LOCALAPPDATA, "Programs", "Git", "bin", "bash.exe")
8448
- ];
8449
- for (const c of candidates) {
8450
- if (c && /bash(\.exe)?$/i.test(c) && fs.existsSync(c)) {
8451
- _gitBashPath = c;
8828
+ const card = JSON.parse(fs7.readFileSync(cardFilePath, "utf-8"));
8829
+ if (path7.basename(cardFilePath).startsWith("card-enriched-")) {
8452
8830
  try {
8453
- fs.writeFileSync(GIT_BASH_CACHE_FILE, JSON.stringify({ path: c }));
8831
+ fs7.unlinkSync(cardFilePath);
8454
8832
  } catch {
8455
8833
  }
8456
- return _gitBashPath;
8834
+ }
8835
+ console.log(`[run-sourcedefs-internal] Processing card "${card.id}"`);
8836
+ const teConfig = deps.readTaskExecutorConfig(boardDir);
8837
+ const taskExecutor = teConfig?.command;
8838
+ const taskExecutorExtraB64 = teConfig?.extra ? Buffer.from(JSON.stringify(teConfig.extra)).toString("base64") : void 0;
8839
+ function runSource(src) {
8840
+ const sourceChecksumForInvoke = src.outputFile ? sourceChecksums?.[src.outputFile] : void 0;
8841
+ const sourceToken = deps.encodeSourceToken({
8842
+ cbk: callbackToken,
8843
+ rg: boardDir,
8844
+ cid: card.id,
8845
+ b: src.bindTo,
8846
+ d: src.outputFile ?? "",
8847
+ cs: sourceChecksumForInvoke
8848
+ });
8849
+ function reportFailure(reason) {
8850
+ invokeSourceDataFetchFailure(sourceToken, reason, (err) => {
8851
+ if (err) console.error(`[run-sourcedefs-internal] source-data-fetch-failure call failed:`, err.message);
8852
+ });
8853
+ }
8854
+ function reportFetched(outFile2) {
8855
+ invokeSourceDataFetched(sourceToken, outFile2);
8856
+ }
8857
+ if (taskExecutor) {
8858
+ if (!src.outputFile) {
8859
+ console.warn(`[run-sourcedefs-internal] source "${src.bindTo}" has no outputFile configured \u2014 cannot deliver`);
8860
+ reportFailure("no outputFile configured");
8861
+ return;
8862
+ }
8863
+ const inFile = path7.join(os3.tmpdir(), `card-source-in-${src.bindTo}-${Date.now()}.json`);
8864
+ const outFile2 = path7.join(os3.tmpdir(), `card-source-out-${src.bindTo}-${Date.now()}.json`);
8865
+ const errFile = path7.join(os3.tmpdir(), `card-source-err-${src.bindTo}-${Date.now()}.txt`);
8866
+ const sourceForExecutor = {
8867
+ ...src,
8868
+ cwd: typeof src.cwd === "string" && src.cwd ? src.cwd : path7.dirname(cardFilePath || ""),
8869
+ boardDir: typeof src.boardDir === "string" && src.boardDir ? src.boardDir : boardDir
8870
+ };
8871
+ deps.appendTaskExecutorLog(boardDir, sourceForExecutor, "external-task-executor");
8872
+ fs7.writeFileSync(inFile, JSON.stringify(sourceForExecutor, null, 2), "utf-8");
8873
+ const executorArgs = ["run-source-fetch", "--in", inFile, "--out", outFile2, "--err", errFile];
8874
+ if (taskExecutorExtraB64) executorArgs.push("--extra", taskExecutorExtraB64);
8875
+ console.log(`[run-sourcedefs-internal] task-executor: ${taskExecutor} ${executorArgs.join(" ")}`);
8876
+ try {
8877
+ deps.execCommandSync(taskExecutor, executorArgs, {
8878
+ shell: true,
8879
+ timeout: src.timeout ?? 12e4
8880
+ });
8881
+ } catch (err) {
8882
+ const reason = err.message ?? String(err);
8883
+ console.error(`[run-sourcedefs-internal] task-executor failed for source "${src.bindTo}":`, reason);
8884
+ reportFailure(reason);
8885
+ return;
8886
+ }
8887
+ if (fs7.existsSync(outFile2)) {
8888
+ reportFetched(outFile2);
8889
+ } else {
8890
+ const errMsg = fs7.existsSync(errFile) ? fs7.readFileSync(errFile, "utf-8").trim() : "executor produced no output file";
8891
+ console.warn(`[run-sourcedefs-internal] source "${src.bindTo}": ${errMsg}`);
8892
+ reportFailure(errMsg);
8893
+ }
8894
+ return;
8895
+ }
8896
+ if (!src.outputFile) {
8897
+ console.warn(`[run-sourcedefs-internal] source "${src.bindTo}" has no outputFile configured \u2014 cannot deliver`);
8898
+ reportFailure("no outputFile configured");
8899
+ return;
8900
+ }
8901
+ const outFile = path7.join(os3.tmpdir(), `card-source-out-${src.bindTo}-${Date.now()}.json`);
8902
+ if (!src.cli) {
8903
+ const errMsg = "source.cli is required for built-in source execution";
8904
+ console.warn(`[run-sourcedefs-internal] source "${src.bindTo}": ${errMsg}`);
8905
+ reportFailure(errMsg);
8906
+ return;
8907
+ }
8908
+ const timeout = src.timeout ?? 12e4;
8909
+ const sourceCwd = typeof src.cwd === "string" ? src.cwd : path7.dirname(cardFilePath || "");
8910
+ const sourceBoardDir = typeof src.boardDir === "string" ? src.boardDir : boardDir;
8911
+ const sourceForBuiltInExecutor = {
8912
+ ...src,
8913
+ cwd: sourceCwd,
8914
+ boardDir: sourceBoardDir
8915
+ };
8916
+ deps.appendTaskExecutorLog(boardDir, sourceForBuiltInExecutor, "built-in-run-source-fetch");
8917
+ const cmdParts = deps.splitCommandLine(src.cli);
8918
+ if (cmdParts.length === 0) {
8919
+ const errMsg = "source.cli command is empty";
8920
+ console.warn(`[run-sourcedefs-internal] source "${src.bindTo}": ${errMsg}`);
8921
+ reportFailure(errMsg);
8922
+ return;
8923
+ }
8924
+ const rawCmd = cmdParts[0];
8925
+ const { cmd, args: cliArgs } = deps.resolveCommandInvocation(rawCmd, cmdParts.slice(1));
8926
+ let stdout;
8927
+ try {
8928
+ stdout = deps.execCommandSync(cmd, cliArgs, {
8929
+ shell: false,
8930
+ encoding: "utf-8",
8931
+ timeout,
8932
+ cwd: sourceCwd,
8933
+ env: {
8934
+ ...process.env,
8935
+ ...sourceBoardDir ? { BOARD_DIR: sourceBoardDir } : {}
8936
+ }
8937
+ });
8938
+ } catch (err) {
8939
+ const reason = err.message ?? String(err);
8940
+ console.error(`[run-sourcedefs-internal] source fetch failed for source "${src.bindTo}":`, reason);
8941
+ reportFailure(reason);
8942
+ return;
8943
+ }
8944
+ fs7.writeFileSync(outFile, stdout.trim(), "utf-8");
8945
+ reportFetched(outFile);
8946
+ }
8947
+ const source_defs = card.source_defs ?? [];
8948
+ for (const src of source_defs) {
8949
+ runSource(src);
8457
8950
  }
8458
8951
  }
8459
- _gitBashPath = false;
8460
- try {
8461
- fs.writeFileSync(GIT_BASH_CACHE_FILE, JSON.stringify({ path: false }));
8462
- } catch {
8463
- }
8464
- return _gitBashPath;
8465
- }
8466
- function shellQuote(s) {
8467
- return "'" + s.replace(/'/g, "'\\''") + "'";
8468
- }
8469
- function spawnDetachedCommand(cmd, args) {
8470
- if (process.platform === "win32") {
8471
- const bash = findGitBash();
8472
- if (bash) {
8473
- const shellCmd = [cmd, ...args].map((a) => shellQuote(a.replace(/\\/g, "/"))).join(" ");
8474
- const child3 = spawn(bash, ["-c", shellCmd], { detached: true, stdio: "ignore", windowsHide: true });
8475
- child3.unref();
8952
+ function cmdRunInference(args) {
8953
+ const inIdx = args.indexOf("--in");
8954
+ const tokenIdx = args.indexOf("--token");
8955
+ const inFile = inIdx !== -1 ? args[inIdx + 1] : void 0;
8956
+ const inferenceToken = tokenIdx !== -1 ? args[tokenIdx + 1] : void 0;
8957
+ if (!inFile || !inferenceToken) {
8958
+ console.error("Usage: board-live-cards run-inference-internal --in <input.json> --token <inference-token>");
8959
+ process.exit(1);
8960
+ }
8961
+ const decodedToken = deps.decodeSourceToken(inferenceToken);
8962
+ if (!decodedToken) {
8963
+ console.error("Invalid inference token");
8964
+ process.exit(1);
8965
+ }
8966
+ const callbackToken = decodedToken.cbk;
8967
+ const boardDir = decodedToken.rg;
8968
+ const cbkDecoded = deps.decodeCallbackToken(callbackToken);
8969
+ if (!cbkDecoded) {
8970
+ console.error("Invalid callback token embedded in inference token");
8971
+ process.exit(1);
8972
+ }
8973
+ function spawnInferenceDone(tmpFile) {
8974
+ const { cmd, args: cliArgs } = deps.getCliInvocation("inference-done", ["--tmp", tmpFile, "--token", inferenceToken]);
8975
+ deps.spawnDetachedCommand(cmd, cliArgs);
8976
+ }
8977
+ function spawnInferenceDoneError(reason) {
8978
+ const tmpFile = path7.join(os3.tmpdir(), `card-inference-err-${Date.now()}.json`);
8979
+ fs7.writeFileSync(tmpFile, JSON.stringify({ isTaskCompleted: false, reason }), "utf-8");
8980
+ spawnInferenceDone(tmpFile);
8981
+ }
8982
+ if (!fs7.existsSync(inFile)) {
8983
+ spawnInferenceDoneError(`inference input not found: ${inFile}`);
8476
8984
  return;
8477
8985
  }
8478
- const child2 = spawn("cmd", ["/c", "start", "/b", "", cmd, ...args], {
8479
- detached: true,
8480
- stdio: "ignore",
8481
- windowsHide: true
8482
- });
8483
- child2.unref();
8484
- return;
8485
- }
8486
- const child = spawn(cmd, args, { detached: true, stdio: "ignore" });
8487
- child.unref();
8488
- }
8489
- function execCommandSync(cmd, args, options) {
8490
- const output = execFileSync(cmd, args, {
8491
- shell: shouldUseShellForCommand(cmd, options?.shell),
8492
- timeout: options?.timeout,
8493
- encoding: options?.encoding,
8494
- cwd: options?.cwd,
8495
- windowsHide: true,
8496
- env: options?.env
8497
- });
8498
- return typeof output === "string" ? output : output.toString("utf-8");
8499
- }
8500
- function execCommandAsync(cmd, args, callback) {
8501
- execFile(
8502
- cmd,
8503
- args,
8504
- { shell: shouldUseShellForCommand(cmd), encoding: "utf8", windowsHide: true },
8505
- (err, stdout, stderr) => callback(err ?? null, stdout, stderr)
8506
- );
8507
- }
8508
- function splitCommandLine(command) {
8509
- const tokens = [];
8510
- let current = "";
8511
- let quote = null;
8512
- for (const ch of command.trim()) {
8513
- if (quote) {
8514
- if (ch === quote) {
8515
- quote = null;
8516
- } else {
8517
- current += ch;
8518
- }
8519
- continue;
8986
+ const adapterFile = path7.join(boardDir, deps.INFERENCE_ADAPTER_FILE);
8987
+ const inferenceAdapter = fs7.existsSync(adapterFile) ? fs7.readFileSync(adapterFile, "utf-8").trim() : void 0;
8988
+ if (!inferenceAdapter) {
8989
+ spawnInferenceDoneError(`inference adapter is not configured (${deps.INFERENCE_ADAPTER_FILE})`);
8990
+ return;
8991
+ }
8992
+ const outFile = path7.join(os3.tmpdir(), `card-inference-out-${Date.now()}.json`);
8993
+ const errFile = path7.join(os3.tmpdir(), `card-inference-err-${Date.now()}.txt`);
8994
+ const adapterParts = deps.splitCommandLine(inferenceAdapter);
8995
+ if (adapterParts.length === 0) {
8996
+ spawnInferenceDoneError("inference adapter command is empty");
8997
+ return;
8998
+ }
8999
+ const adapterRawCmd = adapterParts[0];
9000
+ const adapterRawArgs = adapterParts.slice(1);
9001
+ const { cmd: adapterCmd, args: adapterArgsPrefix } = deps.resolveCommandInvocation(adapterRawCmd, adapterRawArgs);
9002
+ const adapterArgs = [...adapterArgsPrefix, "run-inference", "--in", inFile, "--out", outFile, "--err", errFile];
9003
+ try {
9004
+ deps.execCommandSync(adapterCmd, adapterArgs, {
9005
+ shell: false,
9006
+ timeout: 12e4,
9007
+ cwd: boardDir,
9008
+ env: {
9009
+ ...process.env,
9010
+ BOARD_DIR: boardDir
9011
+ }
9012
+ });
9013
+ } catch (err) {
9014
+ const reason = err.message ?? String(err);
9015
+ spawnInferenceDoneError(reason);
9016
+ return;
8520
9017
  }
8521
- if (ch === '"' || ch === "'") {
8522
- quote = ch;
8523
- continue;
9018
+ if (!fs7.existsSync(outFile)) {
9019
+ const errMsg = fs7.existsSync(errFile) ? fs7.readFileSync(errFile, "utf-8").trim() : "inference adapter produced no output file";
9020
+ spawnInferenceDoneError(errMsg);
9021
+ return;
8524
9022
  }
8525
- if (/\s/.test(ch)) {
8526
- if (current) {
8527
- tokens.push(current);
8528
- current = "";
9023
+ spawnInferenceDone(outFile);
9024
+ }
9025
+ function cmdInferenceDone(args) {
9026
+ const tmpIdx = args.indexOf("--tmp");
9027
+ const tokenIdx = args.indexOf("--token");
9028
+ const tmpFile = tmpIdx !== -1 ? args[tmpIdx + 1] : void 0;
9029
+ const inferenceToken = tokenIdx !== -1 ? args[tokenIdx + 1] : void 0;
9030
+ if (!tmpFile || !inferenceToken) {
9031
+ console.error("Usage: board-live-cards inference-done --tmp <result.json> --token <inference-token>");
9032
+ process.exit(1);
9033
+ }
9034
+ const decodedToken = deps.decodeSourceToken(inferenceToken);
9035
+ if (!decodedToken) {
9036
+ console.error("Invalid inference token");
9037
+ process.exit(1);
9038
+ }
9039
+ const { cbk: callbackToken, rg: dir, cs: inputChecksum } = decodedToken;
9040
+ const decoded = deps.decodeCallbackToken(callbackToken);
9041
+ if (!decoded) {
9042
+ console.error("Invalid callback token embedded in inference token");
9043
+ process.exit(1);
9044
+ }
9045
+ const taskName = decoded.taskName;
9046
+ const cardPath = deps.lookupCardPath(dir, taskName);
9047
+ if (!cardPath) {
9048
+ console.error(`Card file for task "${taskName}" not found in inventory`);
9049
+ process.exit(1);
9050
+ }
9051
+ let result = {};
9052
+ if (fs7.existsSync(tmpFile)) {
9053
+ try {
9054
+ result = JSON.parse(fs7.readFileSync(tmpFile, "utf-8").trim());
9055
+ } catch (err) {
9056
+ result = { isTaskCompleted: false, reason: `failed to parse inference result: ${err instanceof Error ? err.message : String(err)}` };
9057
+ }
9058
+ try {
9059
+ fs7.unlinkSync(tmpFile);
9060
+ } catch {
9061
+ }
9062
+ } else {
9063
+ result = { isTaskCompleted: false, reason: `inference result file not found: ${tmpFile}` };
9064
+ }
9065
+ const isTaskCompletedFlag = result.isTaskCompleted === true;
9066
+ const inferenceCompletedAt = (/* @__PURE__ */ new Date()).toISOString();
9067
+ const card = JSON.parse(fs7.readFileSync(cardPath, "utf-8"));
9068
+ if (!card.card_data) card.card_data = {};
9069
+ const cardData = card.card_data;
9070
+ const existingInference = cardData.llm_task_completion_inference && typeof cardData.llm_task_completion_inference === "object" ? cardData.llm_task_completion_inference : {};
9071
+ cardData.llm_task_completion_inference = {
9072
+ ...existingInference,
9073
+ isTaskCompleted: isTaskCompletedFlag,
9074
+ reason: typeof result.reason === "string" ? result.reason : "",
9075
+ evidence: typeof result.evidence === "string" ? result.evidence : "",
9076
+ inferenceCompletedAt
9077
+ };
9078
+ fs7.writeFileSync(cardPath, JSON.stringify(card, null, 2), "utf-8");
9079
+ const runtimePath = path7.join(dir, taskName, "runtime.json");
9080
+ let runtime = { _sources: {} };
9081
+ if (fs7.existsSync(runtimePath)) {
9082
+ try {
9083
+ runtime = JSON.parse(fs7.readFileSync(runtimePath, "utf-8"));
9084
+ } catch {
8529
9085
  }
8530
- continue;
8531
9086
  }
8532
- current += ch;
8533
- }
8534
- if (quote) {
8535
- throw new Error(`Unterminated quote in command: ${command}`);
9087
+ const inferenceEntry = runtime._inferenceEntry ?? {};
9088
+ runtime._inferenceEntry = deps.nextEntryAfterFetchDelivery(inferenceEntry, inferenceCompletedAt);
9089
+ fs7.mkdirSync(path7.dirname(runtimePath), { recursive: true });
9090
+ fs7.writeFileSync(runtimePath, JSON.stringify(runtime, null, 2), "utf-8");
9091
+ deps.appendEventToJournal(dir, {
9092
+ type: "task-progress",
9093
+ taskName,
9094
+ update: {
9095
+ kind: "inference-done",
9096
+ isTaskCompleted: isTaskCompletedFlag,
9097
+ inputChecksum
9098
+ },
9099
+ timestamp: inferenceCompletedAt
9100
+ });
9101
+ void deps.processAccumulatedEventsInfinitePass(dir);
9102
+ }
9103
+ async function cmdTryDrain(args) {
9104
+ const rgIdx = args.indexOf("--rg");
9105
+ const inlineLoop = args.includes("--inline-loop");
9106
+ const boardDir = rgIdx !== -1 ? args[rgIdx + 1] : void 0;
9107
+ if (!boardDir) {
9108
+ console.error("Usage: board-live-cards process-accumulated-events --rg <dir>");
9109
+ process.exit(1);
9110
+ }
9111
+ await deps.processAccumulatedEventsForced(boardDir, { inlineLoop });
8536
9112
  }
8537
- if (current) tokens.push(current);
8538
- return tokens;
9113
+ return { cmdRunSources, cmdRunInference, cmdInferenceDone, cmdTryDrain };
8539
9114
  }
8540
- function resolveCommandInvocation(rawCmd, rawArgs) {
8541
- if (/^(node|node\.exe)$/i.test(rawCmd)) {
8542
- return { cmd: process.execPath, args: rawArgs };
8543
- }
8544
- if (/\.m?js$/i.test(rawCmd)) {
8545
- return { cmd: process.execPath, args: [rawCmd, ...rawArgs] };
9115
+ function deepGet(obj, path8) {
9116
+ if (!path8 || !obj) return void 0;
9117
+ const parts = path8.split(".");
9118
+ let cur = obj;
9119
+ for (let i = 0; i < parts.length; i++) {
9120
+ if (cur == null) return void 0;
9121
+ cur = cur[parts[i]];
8546
9122
  }
8547
- return { cmd: rawCmd, args: rawArgs };
9123
+ return cur;
8548
9124
  }
8549
- function spawnDetachedProcessAccumulatedWorker(boardDir) {
8550
- const { cmd, args: cliArgs } = getCliInvocation("process-accumulated-events", ["--rg", boardDir, "--inline-loop"]);
8551
- try {
8552
- spawnDetachedCommand(cmd, cliArgs);
8553
- return true;
8554
- } catch {
8555
- return false;
9125
+ function deepSet(obj, path8, value) {
9126
+ const parts = path8.split(".");
9127
+ let cur = obj;
9128
+ for (let i = 0; i < parts.length - 1; i++) {
9129
+ if (cur[parts[i]] == null || typeof cur[parts[i]] !== "object") cur[parts[i]] = {};
9130
+ cur = cur[parts[i]];
8556
9131
  }
9132
+ cur[parts[parts.length - 1]] = value;
8557
9133
  }
8558
- async function processAccumulatedEventsInlineLoop(boardDir, settleDelayMs = 50) {
8559
- while (determineLatestPendingAccumulated(boardDir) > 0) {
8560
- const ran = await processAccumulatedEvents(boardDir);
8561
- if (!ran) return false;
8562
- await new Promise((resolve3) => setTimeout(resolve3, settleDelayMs));
9134
+ async function run(node, options) {
9135
+ if (!node?.compute?.length) return node;
9136
+ if (!node.card_data) node.card_data = {};
9137
+ node.computed_values = {};
9138
+ node._sourcesData = options?.sourcesData ?? {};
9139
+ const ctx = {
9140
+ card_data: node.card_data,
9141
+ requires: node.requires ?? {},
9142
+ fetched_sources: node._sourcesData,
9143
+ computed_values: node.computed_values
9144
+ };
9145
+ for (const step of node.compute) {
9146
+ try {
9147
+ const val = await jsonata2(step.expr).evaluate(ctx);
9148
+ deepSet(node.computed_values, step.bindTo, val);
9149
+ ctx.computed_values = node.computed_values;
9150
+ } catch (err) {
9151
+ console.error(`CardCompute.run error on "${node.id ?? "?"}.${step.bindTo}":`, err);
9152
+ }
8563
9153
  }
8564
- return true;
8565
- }
8566
- function shouldAvoidDetachedProcessSpawn() {
8567
- return process.env.BOARD_LIVE_CARDS_NO_SPAWN === "1";
9154
+ return node;
8568
9155
  }
8569
- async function processAccumulatedEvents(boardDir) {
8570
- const boardPath = path.join(boardDir, BOARD_FILE);
8571
- let release;
8572
- try {
8573
- release = lockSync(boardPath, { retries: 0 });
8574
- } catch {
8575
- return false;
8576
- }
8577
- try {
8578
- const { rg, journal } = createBoardReactiveGraph(boardDir);
8579
- const undrained = journal.drain();
8580
- rg.pushAll(undrained);
8581
- await rg.dispose({ wait: true });
8582
- saveBoard(boardDir, rg, journal);
8583
- return true;
8584
- } finally {
8585
- release();
8586
- }
9156
+ async function evalExpr(expr, node) {
9157
+ const ctx = {
9158
+ card_data: node.card_data ?? {},
9159
+ requires: node.requires ?? {},
9160
+ fetched_sources: node._sourcesData ?? {},
9161
+ computed_values: node.computed_values ?? {}
9162
+ };
9163
+ return jsonata2(expr).evaluate(ctx);
8587
9164
  }
8588
- async function processAccumulatedEventsInfinitePass(boardDir, settleDelayMs = 50, options) {
8589
- if (options?.inlineLoop || shouldAvoidDetachedProcessSpawn()) {
8590
- return processAccumulatedEventsInlineLoop(boardDir, settleDelayMs);
9165
+ function resolve4(node, path8) {
9166
+ if (path8.startsWith("fetched_sources.")) {
9167
+ return deepGet(node._sourcesData ?? {}, path8.slice("fetched_sources.".length));
8591
9168
  }
8592
- return spawnDetachedProcessAccumulatedWorker(boardDir);
8593
- }
8594
- async function processAccumulatedEventsForced(boardDir, options) {
8595
- await processAccumulatedEvents(boardDir);
8596
- await processAccumulatedEventsInfinitePass(boardDir, 50, options);
8597
- }
8598
- function liveCardToTaskConfig(card) {
8599
- const requires = card.requires;
8600
- const provides = card.provides?.map((p) => p.bindTo) ?? [];
8601
- return {
8602
- requires: requires && requires.length > 0 ? requires : void 0,
8603
- provides,
8604
- taskHandlers: ["card-handler"],
8605
- description: card.meta?.title ?? card.id
8606
- };
9169
+ return deepGet(node, path8);
8607
9170
  }
8608
- var __dirname$1 = path.dirname(fileURLToPath(import.meta.url));
8609
- var REPO_ROOT = path.resolve(__dirname$1, "..", "..");
8610
- var LOCAL_TSX_CLI = path.join(REPO_ROOT, "node_modules", "tsx", "dist", "cli.mjs");
8611
- function getCliInvocation(command, args) {
8612
- const jsPath = path.join(__dirname$1, "board-live-cards-cli.js");
8613
- if (fs.existsSync(jsPath)) {
8614
- return { cmd: process.execPath, args: [jsPath, command, ...args] };
9171
+ var VALID_ELEMENT_KINDS = /* @__PURE__ */ new Set([
9172
+ "metric",
9173
+ "table",
9174
+ "editable-table",
9175
+ "chart",
9176
+ "form",
9177
+ "filter",
9178
+ "list",
9179
+ "notes",
9180
+ "todo",
9181
+ "alert",
9182
+ "narrative",
9183
+ "badge",
9184
+ "text",
9185
+ "markdown",
9186
+ "ref",
9187
+ "custom",
9188
+ "actions"
9189
+ ]);
9190
+ var ALLOWED_KEYS = /* @__PURE__ */ new Set(["id", "meta", "requires", "provides", "view", "card_data", "compute", "source_defs"]);
9191
+ function validateNode(node) {
9192
+ const errors = [];
9193
+ if (!node || typeof node !== "object" || Array.isArray(node)) {
9194
+ return { ok: false, errors: ["Node must be a non-null object"] };
8615
9195
  }
8616
- const tsPath = path.join(__dirname$1, "board-live-cards-cli.ts");
8617
- if (fs.existsSync(tsPath) && fs.existsSync(LOCAL_TSX_CLI)) {
8618
- return { cmd: process.execPath, args: [LOCAL_TSX_CLI, tsPath, command, ...args] };
9196
+ const n = node;
9197
+ if (typeof n.id !== "string" || !n.id) errors.push("id: required, must be a non-empty string");
9198
+ for (const key of Object.keys(n)) {
9199
+ if (!ALLOWED_KEYS.has(key)) errors.push(`Unknown top-level key: "${key}"`);
8619
9200
  }
8620
- const npxCmd = process.platform === "win32" ? "npx.cmd" : "npx";
8621
- return { cmd: npxCmd, args: ["tsx", tsPath, command, ...args] };
8622
- }
8623
- function invokeRunSources(boardDir, cardPath, callbackToken, callback) {
8624
- const args = ["--card", cardPath, "--token", callbackToken, "--rg", boardDir];
8625
- const { cmd, args: cmdArgs } = getCliInvocation("run-sourcedefs-internal", args);
8626
- try {
8627
- spawnDetachedCommand(cmd, cmdArgs);
8628
- callback(null);
8629
- } catch (err) {
8630
- callback(err instanceof Error ? err : new Error(String(err)));
9201
+ if (n.card_data == null || typeof n.card_data !== "object" || Array.isArray(n.card_data)) {
9202
+ errors.push("card_data: required, must be an object");
8631
9203
  }
8632
- }
8633
- function invokeRunInference(boardDir, cardId, inputFile, callbackToken, checksum, callback) {
8634
- const inferenceToken = encodeSourceToken({ cbk: callbackToken, rg: boardDir, cid: cardId, b: "", d: "", cs: checksum });
8635
- const { cmd, args } = getCliInvocation("run-inference-internal", ["--in", inputFile, "--token", inferenceToken]);
8636
- try {
8637
- spawnDetachedCommand(cmd, args);
8638
- callback(null);
8639
- } catch (err) {
8640
- callback(err instanceof Error ? err : new Error(String(err)));
9204
+ if (n.meta != null) {
9205
+ if (typeof n.meta !== "object" || Array.isArray(n.meta)) {
9206
+ errors.push("meta: must be an object");
9207
+ } else {
9208
+ const meta = n.meta;
9209
+ if (meta.title != null && typeof meta.title !== "string") errors.push("meta.title: must be a string");
9210
+ if (meta.tags != null && !Array.isArray(meta.tags)) errors.push("meta.tags: must be an array");
9211
+ }
8641
9212
  }
8642
- }
8643
- function appendTaskExecutorLog(boardDir, hydratedSource, mode) {
8644
- try {
8645
- const entry = {
8646
- timestamp: (/* @__PURE__ */ new Date()).toISOString(),
8647
- mode,
8648
- hydratedSource
8649
- };
8650
- fs.appendFileSync(path.join(boardDir, TASK_EXECUTOR_LOG_FILE), JSON.stringify(entry) + "\n", "utf-8");
8651
- } catch (logErr) {
8652
- console.error(`[task-executor-log] append failed: ${logErr instanceof Error ? logErr.message : String(logErr)}`);
9213
+ if (n.requires != null && !Array.isArray(n.requires)) errors.push("requires: must be an array of strings");
9214
+ if (n.provides != null) {
9215
+ if (!Array.isArray(n.provides)) {
9216
+ errors.push("provides: must be an array of { bindTo, ref } bindings");
9217
+ } else {
9218
+ n.provides.forEach((p, i) => {
9219
+ if (!p || typeof p !== "object" || Array.isArray(p)) {
9220
+ errors.push(`provides[${i}]: must be an object with bindTo and ref`);
9221
+ } else {
9222
+ const b = p;
9223
+ if (typeof b.bindTo !== "string" || !b.bindTo) errors.push(`provides[${i}]: missing required "bindTo" string`);
9224
+ if (typeof b.ref !== "string" || !b.ref) errors.push(`provides[${i}]: missing required "ref" string`);
9225
+ }
9226
+ });
9227
+ }
8653
9228
  }
8654
- }
8655
- function appendInferenceAdapterLog(boardDir, cardId, payload) {
8656
- try {
8657
- const entry = {
8658
- timestamp: (/* @__PURE__ */ new Date()).toISOString(),
8659
- cardId,
8660
- payload
8661
- };
8662
- fs.appendFileSync(path.join(boardDir, INFERENCE_ADAPTER_LOG_FILE), JSON.stringify(entry) + "\n", "utf-8");
8663
- } catch (logErr) {
8664
- console.error(`[inference-adapter-log] append failed: ${logErr instanceof Error ? logErr.message : String(logErr)}`);
9229
+ if (n.compute != null) {
9230
+ if (!Array.isArray(n.compute)) {
9231
+ errors.push("compute: must be an array of compute steps");
9232
+ } else {
9233
+ n.compute.forEach((step, i) => {
9234
+ if (!step || typeof step !== "object" || Array.isArray(step)) {
9235
+ errors.push(`compute[${i}]: must be a compute step object`);
9236
+ } else {
9237
+ const s = step;
9238
+ if (typeof s.bindTo !== "string" || !s.bindTo) errors.push(`compute[${i}]: missing required "bindTo" property`);
9239
+ if (typeof s.expr !== "string" || !s.expr) errors.push(`compute[${i}]: missing required "expr" string (JSONata expression)`);
9240
+ }
9241
+ });
9242
+ }
8665
9243
  }
8666
- }
8667
- function invokeSourceDataFetched(sourceToken, tmpFile, callback) {
8668
- const { cmd, args } = getCliInvocation("source-data-fetched", ["--tmp", tmpFile, "--token", sourceToken]);
8669
- execCommandAsync(cmd, args, (err, stdout, stderr) => {
8670
- if (err) console.error(`[source-data-fetched] call failed:`, err.message);
8671
- if (stdout) console.log(stdout.trim());
8672
- if (stderr) console.error(stderr.trim());
8673
- });
8674
- }
8675
- function invokeSourceDataFetchFailure(sourceToken, reason, callback) {
8676
- const { cmd, args } = getCliInvocation("source-data-fetch-failure", ["--token", sourceToken, "--reason", reason]);
8677
- execCommandAsync(cmd, args, (err) => callback(err));
8678
- }
8679
- function createBoardReactiveGraph(boardDir) {
8680
- const envelope = loadBoardEnvelope(boardDir);
8681
- const live = restore(envelope.graph);
8682
- const journalPath = path.join(boardDir, JOURNAL_FILE);
8683
- const journal = new BoardJournal(journalPath, envelope.lastDrainedJournalId);
8684
- const handlers = {
8685
- "card-handler": async (input) => {
8686
- const cardPath = lookupCardPath(boardDir, input.nodeId);
8687
- if (!cardPath) return "task-initiate-failure";
8688
- const card = JSON.parse(fs.readFileSync(cardPath, "utf-8"));
8689
- const cardId = card.id;
8690
- const cardState = card.card_data ?? {};
8691
- const allSources = card.source_defs ?? [];
8692
- const requiredSources = allSources.filter((s) => s.optionalForCompletionGating !== true);
8693
- const runtime = readRuntimeState(boardDir, cardId);
8694
- let runtimeDirty = false;
8695
- const currentExecutionCount = input.taskState?.executionCount ?? 0;
8696
- if (typeof runtime._lastExecutionCount === "number" && runtime._lastExecutionCount !== currentExecutionCount) {
8697
- runtime._sources = {};
8698
- runtime._inferenceEntry = void 0;
8699
- }
8700
- if (runtime._lastExecutionCount !== currentExecutionCount) {
8701
- runtime._lastExecutionCount = currentExecutionCount;
8702
- runtimeDirty = true;
8703
- }
8704
- if (input.update) {
8705
- const u = input.update;
8706
- const outputFile = u.outputFile;
8707
- if (outputFile) {
8708
- if (!runtime._sources[outputFile]) runtime._sources[outputFile] = {};
8709
- const entry = runtime._sources[outputFile];
8710
- if (u.failure) {
8711
- runtime._sources[outputFile] = nextEntryAfterFetchFailure(entry, u.reason ?? "unknown");
8712
- runtimeDirty = true;
9244
+ if (n.source_defs != null) {
9245
+ if (!Array.isArray(n.source_defs)) {
9246
+ errors.push("source_defs: must be an array");
9247
+ } else {
9248
+ const bindTos = /* @__PURE__ */ new Set();
9249
+ const outputFiles = /* @__PURE__ */ new Set();
9250
+ n.source_defs.forEach((src, i) => {
9251
+ if (!src || typeof src !== "object" || Array.isArray(src)) {
9252
+ errors.push(`source_defs[${i}]: must be an object`);
9253
+ } else {
9254
+ const s = src;
9255
+ if (typeof s.bindTo !== "string" || !s.bindTo) {
9256
+ errors.push(`source_defs[${i}]: missing required "bindTo" property`);
9257
+ } else {
9258
+ if (bindTos.has(s.bindTo)) {
9259
+ errors.push(`source_defs[${i}]: bindTo "${s.bindTo}" is not unique across source_defs`);
9260
+ }
9261
+ bindTos.add(s.bindTo);
9262
+ }
9263
+ if (typeof s.outputFile !== "string" || !s.outputFile) {
9264
+ errors.push(`source_defs[${i}]: missing required "outputFile" property`);
8713
9265
  } else {
8714
- runtime._sources[outputFile] = nextEntryAfterFetchDelivery(
8715
- entry,
8716
- u.fetchedAt ?? (/* @__PURE__ */ new Date()).toISOString()
8717
- );
8718
- runtimeDirty = true;
9266
+ if (outputFiles.has(s.outputFile)) {
9267
+ errors.push(`source_defs[${i}]: outputFile "${s.outputFile}" is not unique across source_defs`);
9268
+ }
9269
+ outputFiles.add(s.outputFile);
9270
+ }
9271
+ if (s.optionalForCompletionGating != null && typeof s.optionalForCompletionGating !== "boolean") {
9272
+ errors.push(`source_defs[${i}]: optionalForCompletionGating must be a boolean`);
8719
9273
  }
8720
- if (runtimeDirty) writeRuntimeState(boardDir, cardId, runtime);
8721
9274
  }
9275
+ });
9276
+ }
9277
+ }
9278
+ if (n.view != null) {
9279
+ if (typeof n.view !== "object" || Array.isArray(n.view)) {
9280
+ errors.push("view: must be an object");
9281
+ } else {
9282
+ const view = n.view;
9283
+ if (!Array.isArray(view.elements) || view.elements.length === 0) {
9284
+ errors.push("view.elements: required, must be a non-empty array");
9285
+ } else {
9286
+ view.elements.forEach((elem, i) => {
9287
+ if (!elem || typeof elem !== "object") {
9288
+ errors.push(`view.elements[${i}]: must be an object`);
9289
+ return;
9290
+ }
9291
+ if (!elem.kind || typeof elem.kind !== "string") {
9292
+ errors.push(`view.elements[${i}].kind: required, must be a string`);
9293
+ } else if (!VALID_ELEMENT_KINDS.has(elem.kind)) {
9294
+ errors.push(`view.elements[${i}].kind: unknown kind "${elem.kind}". Valid: ${[...VALID_ELEMENT_KINDS].join(", ")}`);
9295
+ }
9296
+ if (elem.data != null && (typeof elem.data !== "object" || Array.isArray(elem.data))) {
9297
+ errors.push(`view.elements[${i}].data: must be an object`);
9298
+ }
9299
+ });
8722
9300
  }
8723
- const sourcesData = {};
8724
- for (const src of allSources) {
8725
- if (src.outputFile) {
8726
- const filePath = path.join(boardDir, cardId, src.outputFile);
8727
- if (fs.existsSync(filePath)) {
8728
- const raw = fs.readFileSync(filePath, "utf-8").trim();
9301
+ if (view.layout != null && (typeof view.layout !== "object" || Array.isArray(view.layout))) errors.push("view.layout: must be an object");
9302
+ if (view.features != null && (typeof view.features !== "object" || Array.isArray(view.features))) errors.push("view.features: must be an object");
9303
+ }
9304
+ }
9305
+ return { ok: errors.length === 0, errors };
9306
+ }
9307
+ async function enrichSources(source_defs, context) {
9308
+ if (!source_defs || source_defs.length === 0) return [];
9309
+ const evalCtx = {
9310
+ card_data: context.card_data ?? {},
9311
+ requires: context.requires ?? {}
9312
+ };
9313
+ return Promise.all(
9314
+ source_defs.map(async (src) => {
9315
+ const _projections = {};
9316
+ if (src.projections && typeof src.projections === "object" && !Array.isArray(src.projections)) {
9317
+ for (const [key, expr] of Object.entries(src.projections)) {
9318
+ if (typeof expr === "string" && expr.trim().length > 0) {
8729
9319
  try {
8730
- sourcesData[src.bindTo] = JSON.parse(raw);
9320
+ _projections[key] = await jsonata2(expr).evaluate(evalCtx);
8731
9321
  } catch {
8732
- sourcesData[src.bindTo] = raw;
9322
+ _projections[key] = void 0;
8733
9323
  }
8734
9324
  }
8735
9325
  }
8736
9326
  }
8737
- const requires = {};
8738
- for (const [token, taskData] of Object.entries(input.state ?? {})) {
8739
- if (taskData !== null && typeof taskData === "object" && !Array.isArray(taskData)) {
8740
- const unwrapped = taskData[token];
8741
- requires[token] = unwrapped !== void 0 ? unwrapped : taskData;
9327
+ return { ...src, _projections };
9328
+ })
9329
+ );
9330
+ }
9331
+ var CardCompute = {
9332
+ run,
9333
+ eval: evalExpr,
9334
+ resolve: resolve4,
9335
+ validate: validateNode,
9336
+ enrichSources
9337
+ };
9338
+
9339
+ // src/cli/board-live-cards-lib-types.ts
9340
+ function isSourceInFlight(entry) {
9341
+ if (!entry?.lastRequestedAt) return false;
9342
+ return !entry.lastFetchedAt || entry.lastFetchedAt < entry.lastRequestedAt;
9343
+ }
9344
+ function decideSourceAction(entry, queueRequestedAt) {
9345
+ if (!entry?.lastRequestedAt) return "dispatch";
9346
+ const inFlight = isSourceInFlight(entry);
9347
+ if (inFlight) return "in-flight";
9348
+ if (!entry.lastFetchedAt) return "dispatch";
9349
+ if (entry.lastFetchedAt < queueRequestedAt) return "dispatch";
9350
+ return "idle";
9351
+ }
9352
+ function nextEntryAfterFetchDelivery(entry, fetchedAt) {
9353
+ const next = { ...entry, lastFetchedAt: fetchedAt };
9354
+ delete next.lastError;
9355
+ return next;
9356
+ }
9357
+ function nextEntryAfterFetchFailure(entry, reason) {
9358
+ const next = { ...entry, lastError: reason };
9359
+ delete next.lastFetchedAt;
9360
+ return next;
9361
+ }
9362
+
9363
+ // src/cli/board-live-cards-lib-card-handler.ts
9364
+ var DEFAULT_TASK_COMPLETION_RULE = "all_required_sources_fetched";
9365
+ function createCardHandlerFn(boardDir, adapters) {
9366
+ return async (input) => {
9367
+ const cardPath = adapters.cardStore.lookupCardPath(boardDir, input.nodeId);
9368
+ if (!cardPath) return "task-initiate-failure";
9369
+ const card = adapters.cardStore.readCard(cardPath);
9370
+ if (!card) return "task-initiate-failure";
9371
+ const cardId = card.id;
9372
+ const cardState = card.card_data ?? {};
9373
+ const allSources = card.source_defs ?? [];
9374
+ const requiredSources = allSources.filter((s) => s.optionalForCompletionGating !== true);
9375
+ const session = adapters.runtimeStore.openSession(boardDir, cardId);
9376
+ const currentExecutionCount = input.taskState?.executionCount ?? 0;
9377
+ const lastExecCount = session.getLastExecutionCount();
9378
+ if (typeof lastExecCount === "number" && lastExecCount !== currentExecutionCount) {
9379
+ session.resetSources();
9380
+ session.resetInferenceEntry();
9381
+ }
9382
+ if (lastExecCount !== currentExecutionCount) {
9383
+ session.setLastExecutionCount(currentExecutionCount);
9384
+ }
9385
+ if (input.update) {
9386
+ const u = input.update;
9387
+ const outputFile = u.outputFile;
9388
+ if (outputFile) {
9389
+ const entry = session.getSourceEntry(outputFile);
9390
+ if (u.failure) {
9391
+ session.setSourceEntry(outputFile, nextEntryAfterFetchFailure(entry, u.reason ?? "unknown"));
8742
9392
  } else {
8743
- requires[token] = taskData;
9393
+ session.setSourceEntry(outputFile, nextEntryAfterFetchDelivery(
9394
+ entry,
9395
+ u.fetchedAt ?? (/* @__PURE__ */ new Date()).toISOString()
9396
+ ));
8744
9397
  }
9398
+ session.flush();
8745
9399
  }
8746
- const computeNode = {
8747
- id: cardId,
8748
- card_data: { ...cardState },
8749
- requires,
8750
- source_defs: allSources,
8751
- compute: card.compute
8752
- };
8753
- computeNode._sourcesData = sourcesData;
8754
- if (card.compute) {
8755
- await CardCompute.run(computeNode, { sourcesData });
8756
- }
8757
- const cvPath = resolveComputedValuesPath(boardDir, cardId);
8758
- writeJsonAtomic(cvPath, {
8759
- schema_version: "v1",
8760
- card_id: cardId,
8761
- computed_values: computeNode.computed_values ?? {}
8762
- });
8763
- const enrichedCard = { ...card };
8764
- const enrichedSources = await CardCompute.enrichSources(
8765
- Array.isArray(card.source_defs) ? card.source_defs : void 0,
8766
- {
8767
- card_data: card.card_data,
8768
- requires,
8769
- sourcesData,
8770
- computed_values: computeNode.computed_values
9400
+ }
9401
+ const sourcesData = {};
9402
+ for (const src of allSources) {
9403
+ if (src.outputFile) {
9404
+ const content = adapters.cardStore.readSourceFileContent(boardDir, cardId, src.outputFile);
9405
+ if (content !== null) {
9406
+ sourcesData[src.bindTo] = content;
8771
9407
  }
8772
- );
8773
- const sourceCwd = path.dirname(cardPath);
8774
- enrichedCard.source_defs = Array.isArray(enrichedSources) ? enrichedSources.map((src) => ({
8775
- ...src,
8776
- cwd: typeof src.cwd === "string" && src.cwd ? src.cwd : sourceCwd,
8777
- boardDir: typeof src.boardDir === "string" && src.boardDir ? src.boardDir : boardDir
8778
- })) : enrichedSources;
8779
- const enrichedByOutput = /* @__PURE__ */ new Map();
8780
- for (const src of Array.isArray(enrichedCard.source_defs) ? enrichedCard.source_defs : []) {
9408
+ }
9409
+ }
9410
+ const requires = {};
9411
+ for (const [token, taskData] of Object.entries(input.state ?? {})) {
9412
+ if (taskData !== null && typeof taskData === "object" && !Array.isArray(taskData)) {
9413
+ const unwrapped = taskData[token];
9414
+ requires[token] = unwrapped !== void 0 ? unwrapped : taskData;
9415
+ } else {
9416
+ requires[token] = taskData;
9417
+ }
9418
+ }
9419
+ const computeNode = {
9420
+ id: cardId,
9421
+ card_data: { ...cardState },
9422
+ requires,
9423
+ source_defs: allSources,
9424
+ compute: card.compute
9425
+ };
9426
+ computeNode._sourcesData = sourcesData;
9427
+ if (card.compute) {
9428
+ await CardCompute.run(computeNode, { sourcesData });
9429
+ }
9430
+ adapters.outputStore.writeComputedValues(boardDir, cardId, computeNode.computed_values ?? {});
9431
+ const enrichedCard = { ...card };
9432
+ const enrichedSources = await CardCompute.enrichSources(
9433
+ Array.isArray(card.source_defs) ? card.source_defs : void 0,
9434
+ {
9435
+ card_data: card.card_data,
9436
+ requires,
9437
+ sourcesData,
9438
+ computed_values: computeNode.computed_values
9439
+ }
9440
+ );
9441
+ const sourceCwd = cardPath.replace(/[\\/][^\\/]*$/, "");
9442
+ enrichedCard.source_defs = Array.isArray(enrichedSources) ? enrichedSources.map((src) => ({
9443
+ ...src,
9444
+ cwd: typeof src.cwd === "string" && src.cwd ? src.cwd : sourceCwd,
9445
+ boardDir: typeof src.boardDir === "string" && src.boardDir ? src.boardDir : boardDir
9446
+ })) : enrichedSources;
9447
+ const now = (/* @__PURE__ */ new Date()).toISOString();
9448
+ const runQueuedAt = input.update ? void 0 : now;
9449
+ const undeliveredRequired = requiredSources.filter((s) => {
9450
+ const outputFile = s.outputFile;
9451
+ if (typeof outputFile !== "string" || !outputFile) return true;
9452
+ let entry = session.getSourceEntry(outputFile);
9453
+ if (runQueuedAt) {
9454
+ entry = { ...entry, queueRequestedAt: runQueuedAt };
9455
+ session.setSourceEntry(outputFile, entry);
9456
+ }
9457
+ const qrt = entry.queueRequestedAt ?? entry.lastRequestedAt ?? now;
9458
+ const action = decideSourceAction(entry, qrt);
9459
+ if (action === "in-flight") return false;
9460
+ return action === "dispatch";
9461
+ });
9462
+ session.flush();
9463
+ if (undeliveredRequired.length > 0) {
9464
+ let stampedAny = false;
9465
+ for (const src of undeliveredRequired) {
8781
9466
  const outputFile = src.outputFile;
8782
- if (typeof outputFile === "string" && outputFile) {
8783
- enrichedByOutput.set(outputFile, src);
8784
- }
9467
+ if (typeof outputFile !== "string" || !outputFile) continue;
9468
+ const entry = session.getSourceEntry(outputFile);
9469
+ session.setSourceEntry(outputFile, { ...entry, lastRequestedAt: now });
9470
+ stampedAny = true;
9471
+ }
9472
+ if (stampedAny) session.flush();
9473
+ if (!stampedAny) return "task-initiated";
9474
+ const result = await adapters.invocationAdapter.requestSourceFetch(
9475
+ boardDir,
9476
+ enrichedCard,
9477
+ input.callbackToken
9478
+ );
9479
+ if (!result.dispatched && result.error) {
9480
+ console.error(`[card-handler] ${input.nodeId}: source fetch dispatch failed: ${result.error}`);
8785
9481
  }
8786
- const now = (/* @__PURE__ */ new Date()).toISOString();
8787
- const runQueuedAt = input.update ? void 0 : now;
8788
- const undeliveredRequired = requiredSources.filter((s) => {
8789
- const outputFile = s.outputFile;
8790
- if (typeof outputFile !== "string" || !outputFile) return true;
8791
- if (!runtime._sources[outputFile]) runtime._sources[outputFile] = {};
8792
- const entry = runtime._sources[outputFile];
9482
+ return "task-initiated";
9483
+ }
9484
+ const providesBindings = card.provides ?? [];
9485
+ const data = {};
9486
+ for (const { bindTo, ref } of providesBindings) {
9487
+ data[bindTo] = CardCompute.resolve(computeNode, ref);
9488
+ }
9489
+ const completionRule = typeof card.when_is_task_completed === "string" && card.when_is_task_completed.trim() ? card.when_is_task_completed.trim() : DEFAULT_TASK_COMPLETION_RULE;
9490
+ const cardData = card.card_data;
9491
+ const llmCompletion = cardData?.llm_task_completion_inference ?? {};
9492
+ const isLlmTaskCompleted = llmCompletion.isTaskCompleted === true;
9493
+ const inferenceEntry = session.getInferenceEntry();
9494
+ const inferenceRequestedAt = typeof inferenceEntry.lastRequestedAt === "string" ? inferenceEntry.lastRequestedAt : void 0;
9495
+ const inferenceCompletedAt = typeof llmCompletion.inferenceCompletedAt === "string" ? llmCompletion.inferenceCompletedAt : void 0;
9496
+ const inferencePending = !!inferenceRequestedAt && (!inferenceCompletedAt || inferenceCompletedAt < inferenceRequestedAt);
9497
+ const latestRequiredSourceFetchedAt = requiredSources.reduce((latest, src) => {
9498
+ const fetchedAt = session.getSourceEntry(src.outputFile).lastFetchedAt;
9499
+ if (typeof fetchedAt !== "string") return latest;
9500
+ if (!latest || fetchedAt > latest) return fetchedAt;
9501
+ return latest;
9502
+ }, void 0);
9503
+ const shouldRequestInference = !inferenceRequestedAt || !inferenceCompletedAt || !!latestRequiredSourceFetchedAt && latestRequiredSourceFetchedAt > inferenceCompletedAt;
9504
+ if (completionRule !== DEFAULT_TASK_COMPLETION_RULE) {
9505
+ if (isLlmTaskCompleted) ; else if (inferencePending) {
9506
+ return "task-initiated";
9507
+ } else if (!shouldRequestInference) {
9508
+ return "task-initiated";
9509
+ } else {
9510
+ const inferencePayload = {
9511
+ cardId,
9512
+ taskName: input.nodeId,
9513
+ completionRule,
9514
+ context: {
9515
+ requires,
9516
+ sourcesData,
9517
+ computed_values: computeNode.computed_values ?? {},
9518
+ provides: data,
9519
+ card_data: computeNode.card_data ?? {}
9520
+ }
9521
+ };
9522
+ let updatedInferenceEntry = { ...inferenceEntry };
8793
9523
  if (runQueuedAt) {
8794
- entry.queueRequestedAt = runQueuedAt;
8795
- runtimeDirty = true;
9524
+ updatedInferenceEntry = { ...updatedInferenceEntry, queueRequestedAt: runQueuedAt };
9525
+ session.setInferenceEntry(updatedInferenceEntry);
8796
9526
  }
8797
- const qrt = entry.queueRequestedAt ?? entry.lastRequestedAt ?? now;
8798
- const action = decideSourceAction(entry, qrt);
8799
- if (action === "in-flight") return false;
8800
- return action === "dispatch";
8801
- });
8802
- if (runtimeDirty) writeRuntimeState(boardDir, cardId, runtime);
8803
- if (undeliveredRequired.length > 0) {
8804
- let stampedAny = false;
8805
- for (const src of undeliveredRequired) {
8806
- const outputFile = src.outputFile;
8807
- if (typeof outputFile !== "string" || !outputFile) continue;
8808
- const entry = runtime._sources[outputFile] ?? {};
8809
- markRequested(entry, now);
8810
- runtime._sources[outputFile] = entry;
8811
- stampedAny = true;
8812
- }
8813
- if (stampedAny) writeRuntimeState(boardDir, cardId, runtime);
8814
- if (!stampedAny) return "task-initiated";
8815
- const enrichedCardPath = path.join(os.tmpdir(), `card-enriched-${cardId}-${Date.now()}.json`);
8816
- fs.writeFileSync(enrichedCardPath, JSON.stringify(enrichedCard, null, 2), "utf-8");
8817
- invokeRunSources(boardDir, enrichedCardPath, input.callbackToken, (err) => {
8818
- if (err) {
8819
- console.error(`[card-handler] ${input.nodeId}:`, err.message);
8820
- try {
8821
- fs.unlinkSync(enrichedCardPath);
8822
- } catch {
8823
- }
8824
- }
8825
- });
8826
- return "task-initiated";
8827
- }
8828
- const providesBindings = card.provides ?? [];
8829
- const data = {};
8830
- for (const { bindTo, ref } of providesBindings) {
8831
- data[bindTo] = CardCompute.resolve(computeNode, ref);
8832
- }
8833
- const completionRule = typeof card.when_is_task_completed === "string" && card.when_is_task_completed.trim() ? card.when_is_task_completed.trim() : DEFAULT_TASK_COMPLETION_RULE;
8834
- const cardData = card.card_data;
8835
- const llmCompletion = cardData?.llm_task_completion_inference ?? {};
8836
- const isLlmTaskCompleted = llmCompletion.isTaskCompleted === true;
8837
- const inferenceEntry = runtime._inferenceEntry ?? {};
8838
- const inferenceRequestedAt = typeof inferenceEntry.lastRequestedAt === "string" ? inferenceEntry.lastRequestedAt : void 0;
8839
- const inferenceCompletedAt = typeof llmCompletion.inferenceCompletedAt === "string" ? llmCompletion.inferenceCompletedAt : void 0;
8840
- const inferencePending = !!inferenceRequestedAt && (!inferenceCompletedAt || inferenceCompletedAt < inferenceRequestedAt);
8841
- const latestRequiredSourceFetchedAt = requiredSources.reduce((latest, src) => {
8842
- const fetchedAt = runtime._sources[src.outputFile]?.lastFetchedAt;
8843
- if (typeof fetchedAt !== "string") return latest;
8844
- if (!latest || fetchedAt > latest) return fetchedAt;
8845
- return latest;
8846
- }, void 0);
8847
- const shouldRequestInference = !inferenceRequestedAt || !inferenceCompletedAt || !!latestRequiredSourceFetchedAt && latestRequiredSourceFetchedAt > inferenceCompletedAt;
8848
- if (completionRule !== DEFAULT_TASK_COMPLETION_RULE) {
8849
- if (isLlmTaskCompleted) ; else if (inferencePending) {
9527
+ const inferenceQrt = updatedInferenceEntry.queueRequestedAt ?? updatedInferenceEntry.lastRequestedAt ?? now;
9528
+ const inferenceAction = decideSourceAction(updatedInferenceEntry, inferenceQrt);
9529
+ if (inferenceAction === "in-flight") {
9530
+ session.flush();
8850
9531
  return "task-initiated";
8851
- } else if (!shouldRequestInference) {
9532
+ }
9533
+ if (inferenceAction === "idle") {
8852
9534
  return "task-initiated";
8853
- } else {
8854
- const now2 = (/* @__PURE__ */ new Date()).toISOString();
8855
- const inferencePayload = {
8856
- cardId,
9535
+ }
9536
+ adapters.outputStore.appendInferenceLog(boardDir, cardId, inferencePayload);
9537
+ session.setInferenceEntry({ ...updatedInferenceEntry, lastRequestedAt: now });
9538
+ session.flush();
9539
+ const inferenceResult = await adapters.invocationAdapter.requestInference(
9540
+ boardDir,
9541
+ cardId,
9542
+ inferencePayload,
9543
+ input.callbackToken
9544
+ );
9545
+ if (!inferenceResult.dispatched) {
9546
+ const failedAt = (/* @__PURE__ */ new Date()).toISOString();
9547
+ adapters.inputStore.appendEvent(boardDir, {
9548
+ type: "task-failed",
8857
9549
  taskName: input.nodeId,
8858
- completionRule,
8859
- context: {
8860
- requires,
8861
- sourcesData,
8862
- computed_values: computeNode.computed_values ?? {},
8863
- provides: data,
8864
- card_data: computeNode.card_data ?? {}
8865
- }
8866
- };
8867
- if (runQueuedAt) {
8868
- inferenceEntry.queueRequestedAt = runQueuedAt;
8869
- runtimeDirty = true;
8870
- }
8871
- const inferenceQrt = inferenceEntry.queueRequestedAt ?? inferenceEntry.lastRequestedAt ?? now2;
8872
- const inferenceAction = decideSourceAction(inferenceEntry, inferenceQrt);
8873
- if (inferenceAction === "in-flight") {
8874
- runtime._inferenceEntry = inferenceEntry;
8875
- if (runtimeDirty) writeRuntimeState(boardDir, cardId, runtime);
8876
- return "task-initiated";
8877
- }
8878
- if (inferenceAction === "idle") {
8879
- return "task-initiated";
8880
- }
8881
- const inferenceInFile = path.join(os.tmpdir(), `card-inference-${cardId}-${Date.now()}.json`);
8882
- fs.writeFileSync(inferenceInFile, JSON.stringify(inferencePayload, null, 2), "utf-8");
8883
- appendInferenceAdapterLog(boardDir, cardId, inferencePayload);
8884
- markRequested(inferenceEntry, now2);
8885
- runtime._inferenceEntry = inferenceEntry;
8886
- runtimeDirty = true;
8887
- invokeRunInference(boardDir, cardId, inferenceInFile, input.callbackToken, void 0, (err) => {
8888
- if (err) {
8889
- console.error(`[card-handler] ${input.nodeId}:`, err.message);
8890
- const failedAt = (/* @__PURE__ */ new Date()).toISOString();
8891
- appendEventToJournal(boardDir, {
8892
- type: "task-failed",
8893
- taskName: input.nodeId,
8894
- error: err.message,
8895
- timestamp: failedAt
8896
- });
8897
- }
9550
+ error: inferenceResult.error ?? "inference dispatch failed",
9551
+ timestamp: failedAt
8898
9552
  });
8899
- return "task-initiated";
8900
9553
  }
9554
+ return "task-initiated";
8901
9555
  }
8902
- writeRuntimeDataObjects(boardDir, data);
8903
- const undeliveredOptional = allSources.filter((s) => {
8904
- if (s.optionalForCompletionGating !== true) return false;
8905
- const entry = runtime._sources[s.outputFile];
8906
- if (!entry?.lastRequestedAt) return true;
8907
- if (!entry.lastFetchedAt) return true;
8908
- return entry.lastFetchedAt <= entry.lastRequestedAt;
8909
- });
8910
- if (undeliveredOptional.length > 0) {
8911
- invokeRunSources(boardDir, cardPath, input.callbackToken, (err) => {
8912
- if (err) console.error(`[card-handler] ${input.nodeId}:`, err.message);
8913
- });
8914
- }
8915
- appendEventToJournal(boardDir, {
8916
- type: "task-completed",
8917
- taskName: input.nodeId,
8918
- data,
8919
- timestamp: (/* @__PURE__ */ new Date()).toISOString()
8920
- });
8921
- return "task-initiated";
8922
9556
  }
8923
- };
8924
- const rg = createReactiveGraph(live, { handlers });
8925
- return { rg, journal };
8926
- }
8927
- function resolveCardGlobMatches(cardGlob) {
8928
- const patterns = cardGlob.split(",").map((s) => s.trim()).filter(Boolean).map((p) => p.replace(/\\/g, "/"));
8929
- const matches = fg.sync(patterns, {
8930
- absolute: true,
8931
- onlyFiles: true,
8932
- unique: true,
8933
- dot: false
8934
- });
8935
- return [...matches].map((m) => path.resolve(m)).sort((a, b) => a.localeCompare(b));
8936
- }
8937
- function cmdInit(args) {
8938
- const dir = args[0];
8939
- if (!dir) {
8940
- throw new Error("Usage: board-live-cards init <dir> [--task-executor <script>] [--chat-handler <script>] [--inference-adapter <script>] [--runtime-out <dir>]");
8941
- }
8942
- const teIdx = args.indexOf("--task-executor");
8943
- const taskExecutor = teIdx !== -1 ? args[teIdx + 1] : void 0;
8944
- const chIdx = args.indexOf("--chat-handler");
8945
- const chatHandler = chIdx !== -1 ? args[chIdx + 1] : void 0;
8946
- const iaIdx = args.indexOf("--inference-adapter");
8947
- const inferenceAdapter = iaIdx !== -1 ? args[iaIdx + 1] : void 0;
8948
- const roIdx = args.indexOf("--runtime-out");
8949
- const runtimeOut = roIdx !== -1 ? args[roIdx + 1] : void 0;
8950
- if (roIdx !== -1 && !runtimeOut) {
8951
- throw new Error("Usage: board-live-cards init <dir> [--task-executor <script>] [--chat-handler <script>] [--inference-adapter <script>] [--runtime-out <dir>]");
8952
- }
8953
- const result = initBoard(dir);
8954
- if (taskExecutor) {
8955
- const teExtraIdx = args.indexOf("--task-executor-extra");
8956
- let teExtra;
8957
- if (teExtraIdx !== -1 && args[teExtraIdx + 1]) {
8958
- try {
8959
- teExtra = JSON.parse(args[teExtraIdx + 1]);
8960
- } catch {
8961
- }
9557
+ adapters.outputStore.writeDataObjects(boardDir, data);
9558
+ const undeliveredOptional = allSources.filter((s) => {
9559
+ if (s.optionalForCompletionGating !== true) return false;
9560
+ const entry = session.getSourceEntry(s.outputFile);
9561
+ if (!entry.lastRequestedAt) return true;
9562
+ if (!entry.lastFetchedAt) return true;
9563
+ return entry.lastFetchedAt <= entry.lastRequestedAt;
9564
+ });
9565
+ if (undeliveredOptional.length > 0) {
9566
+ adapters.invocationAdapter.requestSourceFetch(boardDir, enrichedCard, input.callbackToken).catch((err) => console.error(`[card-handler] ${input.nodeId}: optional source fetch:`, err));
8962
9567
  }
8963
- const teConfig = { command: taskExecutor, ...teExtra ? { extra: teExtra } : {} };
8964
- fs.writeFileSync(path.join(dir, TASK_EXECUTOR_FILE), JSON.stringify(teConfig, null, 2), "utf-8");
8965
- }
8966
- if (chatHandler) {
8967
- fs.writeFileSync(path.join(dir, ".chat-handler"), chatHandler, "utf-8");
8968
- }
8969
- if (inferenceAdapter) {
8970
- fs.writeFileSync(path.join(dir, INFERENCE_ADAPTER_FILE), inferenceAdapter, "utf-8");
8971
- }
8972
- const runtimeOutDir = configureRuntimeOutDir(dir, runtimeOut);
8973
- const live = loadBoard(dir);
8974
- writeJsonAtomic(resolveStatusSnapshotPath(dir), buildBoardStatusObject(dir, live));
8975
- if (result === "exists") {
8976
- console.log(`Board already initialized at ${path.resolve(dir)}${taskExecutor ? ` (task-executor updated: ${taskExecutor})` : ""} (runtime-out: ${runtimeOutDir})`);
8977
- } else {
8978
- console.log(`Board initialized at ${path.resolve(dir)}${taskExecutor ? ` (task-executor: ${taskExecutor})` : ""} (runtime-out: ${runtimeOutDir})`);
8979
- }
9568
+ adapters.inputStore.appendEvent(boardDir, {
9569
+ type: "task-completed",
9570
+ taskName: input.nodeId,
9571
+ data,
9572
+ timestamp: (/* @__PURE__ */ new Date()).toISOString()
9573
+ });
9574
+ return "task-initiated";
9575
+ };
8980
9576
  }
8981
- function buildBoardStatusObject(dir, live) {
9577
+
9578
+ // src/cli/board-live-cards-lib-board-status.ts
9579
+ function buildBoardStatusObject(boardPath, live) {
8982
9580
  const taskState = live.state.tasks;
8983
9581
  const taskConfig = live.config.tasks;
8984
9582
  const cardNames = Object.keys(taskState);
@@ -9062,15 +9660,15 @@ function buildBoardStatusObject(dir, live) {
9062
9660
  let orphanCards = 0;
9063
9661
  for (const [name, cfg] of Object.entries(taskConfig)) {
9064
9662
  const requiresNone = (cfg.requires ?? []).length === 0;
9065
- const provides = cfg.provides ?? [];
9066
- const feedsAny = provides.some((p) => (dependentsByToken.get(p) ?? []).some((d) => d !== name));
9663
+ const providesList = cfg.provides ?? [];
9664
+ const feedsAny = providesList.some((p) => (dependentsByToken.get(p) ?? []).some((d) => d !== name));
9067
9665
  if (requiresNone && !feedsAny) orphanCards += 1;
9068
9666
  }
9069
9667
  return {
9070
9668
  schema_version: "v1",
9071
9669
  meta: {
9072
9670
  board: {
9073
- path: path.resolve(dir)
9671
+ path: boardPath
9074
9672
  }
9075
9673
  },
9076
9674
  summary: {
@@ -9092,1193 +9690,901 @@ function buildBoardStatusObject(dir, live) {
9092
9690
  cards
9093
9691
  };
9094
9692
  }
9095
- function cmdStatus(args) {
9096
- const rgIdx = args.indexOf("--rg");
9097
- const asJson = args.includes("--json");
9098
- const dir = rgIdx !== -1 ? args[rgIdx + 1] : void 0;
9099
- if (!dir) {
9100
- console.error("Usage: board-live-cards status --rg <dir>");
9101
- process.exit(1);
9693
+ var NodeRuntimeStoreSession = class {
9694
+ constructor(boardDir, cardId) {
9695
+ this.boardDir = boardDir;
9696
+ this.cardId = cardId;
9697
+ this.state = NodeRuntimeInternalStore.readState(boardDir, cardId);
9102
9698
  }
9103
- const statusOutPath = resolveStatusSnapshotPath(dir);
9104
- let statusObject;
9105
- if (fs.existsSync(statusOutPath)) {
9106
- statusObject = JSON.parse(fs.readFileSync(statusOutPath, "utf-8"));
9107
- } else {
9108
- statusObject = buildBoardStatusObject(dir, loadBoard(dir));
9109
- writeJsonAtomic(statusOutPath, statusObject);
9699
+ boardDir;
9700
+ cardId;
9701
+ state;
9702
+ dirty = false;
9703
+ getSourceEntry(outputFile) {
9704
+ return { ...this.state._sources[outputFile] ?? {} };
9110
9705
  }
9111
- if (asJson) {
9112
- console.log(JSON.stringify(statusObject, null, 2));
9113
- return;
9706
+ setSourceEntry(outputFile, entry) {
9707
+ this.state._sources[outputFile] = entry;
9708
+ this.dirty = true;
9709
+ }
9710
+ resetSources() {
9711
+ this.state._sources = {};
9712
+ this.dirty = true;
9713
+ }
9714
+ getInferenceEntry() {
9715
+ return { ...this.state._inferenceEntry ?? {} };
9716
+ }
9717
+ setInferenceEntry(entry) {
9718
+ this.state._inferenceEntry = entry;
9719
+ this.dirty = true;
9720
+ }
9721
+ resetInferenceEntry() {
9722
+ this.state._inferenceEntry = void 0;
9723
+ this.dirty = true;
9724
+ }
9725
+ getLastExecutionCount() {
9726
+ return this.state._lastExecutionCount;
9114
9727
  }
9115
- console.log(`Board: ${statusObject.meta.board.path}`);
9116
- console.log(`Tasks: ${statusObject.summary.card_count}`);
9117
- console.log("");
9118
- for (const card of statusObject.cards) {
9119
- const dataKeys = card.provides_runtime.join(", ");
9120
- console.log(` ${card.status.padEnd(12)} ${card.name}${dataKeys ? ` \u2014 [${dataKeys}]` : ""}`);
9728
+ setLastExecutionCount(count) {
9729
+ this.state._lastExecutionCount = count;
9730
+ this.dirty = true;
9121
9731
  }
9122
- console.log("");
9123
- console.log(`Schedule: ${statusObject.summary.eligible} eligible, ${statusObject.summary.pending} pending, ${statusObject.summary.blocked} blocked, ${statusObject.summary.unresolved} unresolved`);
9732
+ flush() {
9733
+ if (!this.dirty) return;
9734
+ NodeRuntimeInternalStore.writeState(this.boardDir, this.cardId, this.state);
9735
+ this.dirty = false;
9736
+ }
9737
+ };
9738
+ var NodeRuntimeInternalStore = class {
9739
+ openSession(boardDir, cardId) {
9740
+ return new NodeRuntimeStoreSession(boardDir, cardId);
9741
+ }
9742
+ static readState(boardDir, cardId) {
9743
+ const p = path7.join(boardDir, cardId, "runtime.json");
9744
+ if (!fs7.existsSync(p)) return { _sources: {} };
9745
+ try {
9746
+ return JSON.parse(fs7.readFileSync(p, "utf-8"));
9747
+ } catch {
9748
+ return { _sources: {} };
9749
+ }
9750
+ }
9751
+ static writeState(boardDir, cardId, state) {
9752
+ const p = path7.join(boardDir, cardId, "runtime.json");
9753
+ fs7.mkdirSync(path7.dirname(p), { recursive: true });
9754
+ fs7.writeFileSync(p, JSON.stringify(state, null, 2));
9755
+ }
9756
+ };
9757
+ function createNodeRuntimeStore() {
9758
+ return new NodeRuntimeInternalStore();
9124
9759
  }
9125
- function cmdTaskCompleted(args) {
9126
- const rgIdx = args.indexOf("--rg");
9127
- const tokenIdx = args.indexOf("--token");
9128
- const dataIdx = args.indexOf("--data");
9129
- const dir = rgIdx !== -1 ? args[rgIdx + 1] : void 0;
9130
- const token = tokenIdx !== -1 ? args[tokenIdx + 1] : void 0;
9131
- if (!dir || !token) {
9132
- console.error("Usage: board-live-cards task-completed --rg <dir> --token <token> [--data <json>]");
9133
- process.exit(1);
9760
+ function writeJsonAtomic(filePath, payload) {
9761
+ fs7.mkdirSync(path7.dirname(filePath), { recursive: true });
9762
+ const tmpPath = `${filePath}.${process.pid}.${randomUUID()}.tmp`;
9763
+ fs7.writeFileSync(tmpPath, JSON.stringify(payload, null, 2), "utf-8");
9764
+ fs7.renameSync(tmpPath, filePath);
9765
+ }
9766
+ var NodeOutputStore = class {
9767
+ constructor(resolveComputedValuesPath2, resolveDataObjectsDirPath2, inferenceAdapterLogFile) {
9768
+ this.resolveComputedValuesPath = resolveComputedValuesPath2;
9769
+ this.resolveDataObjectsDirPath = resolveDataObjectsDirPath2;
9770
+ this.inferenceAdapterLogFile = inferenceAdapterLogFile;
9771
+ }
9772
+ resolveComputedValuesPath;
9773
+ resolveDataObjectsDirPath;
9774
+ inferenceAdapterLogFile;
9775
+ writeComputedValues(boardDir, cardId, values) {
9776
+ writeJsonAtomic(this.resolveComputedValuesPath(boardDir, cardId), {
9777
+ schema_version: "v1",
9778
+ card_id: cardId,
9779
+ computed_values: values
9780
+ });
9134
9781
  }
9135
- const decoded = decodeCallbackToken2(token);
9136
- if (!decoded) {
9137
- console.error("Invalid callback token");
9138
- process.exit(1);
9782
+ writeDataObjects(boardDir, data) {
9783
+ for (const [token, payload] of Object.entries(data)) {
9784
+ if (!token) continue;
9785
+ const fileName = token.replace(/[\\/]/g, "__");
9786
+ if (!fileName) continue;
9787
+ writeJsonAtomic(path7.join(this.resolveDataObjectsDirPath(boardDir), fileName), payload);
9788
+ }
9139
9789
  }
9140
- const data = dataIdx !== -1 ? JSON.parse(args[dataIdx + 1]) : {};
9141
- writeRuntimeDataObjects(dir, data);
9142
- appendEventToJournal(dir, {
9143
- type: "task-completed",
9144
- taskName: decoded.taskName,
9145
- data,
9146
- timestamp: (/* @__PURE__ */ new Date()).toISOString()
9147
- });
9148
- void processAccumulatedEventsForced(dir);
9149
- console.log("Task completed.");
9790
+ appendInferenceLog(boardDir, cardId, payload) {
9791
+ try {
9792
+ const entry = { timestamp: (/* @__PURE__ */ new Date()).toISOString(), cardId, payload };
9793
+ fs7.appendFileSync(
9794
+ path7.join(boardDir, this.inferenceAdapterLogFile),
9795
+ JSON.stringify(entry) + "\n",
9796
+ "utf-8"
9797
+ );
9798
+ } catch (logErr) {
9799
+ console.error(`[inference-adapter-log] append failed: ${logErr instanceof Error ? logErr.message : String(logErr)}`);
9800
+ }
9801
+ }
9802
+ };
9803
+ function createNodeOutputStore(resolveComputedValuesPath2, resolveDataObjectsDirPath2, inferenceAdapterLogFile) {
9804
+ return new NodeOutputStore(resolveComputedValuesPath2, resolveDataObjectsDirPath2, inferenceAdapterLogFile);
9150
9805
  }
9151
- function cmdTaskFailed(args) {
9152
- const rgIdx = args.indexOf("--rg");
9153
- const tokenIdx = args.indexOf("--token");
9154
- const errorIdx = args.indexOf("--error");
9155
- const dir = rgIdx !== -1 ? args[rgIdx + 1] : void 0;
9156
- const token = tokenIdx !== -1 ? args[tokenIdx + 1] : void 0;
9157
- const errorMsg = errorIdx !== -1 ? args[errorIdx + 1] : "unknown error";
9158
- if (!dir || !token) {
9159
- console.error("Usage: board-live-cards task-failed --rg <dir> --token <token> [--error <message>]");
9160
- process.exit(1);
9806
+ var NodeInputStore = class {
9807
+ constructor(journalFile) {
9808
+ this.journalFile = journalFile;
9161
9809
  }
9162
- const decoded = decodeCallbackToken2(token);
9163
- if (!decoded) {
9164
- console.error("Invalid callback token");
9165
- process.exit(1);
9810
+ journalFile;
9811
+ appendEvent(boardDir, event) {
9812
+ const journalPath = path7.join(boardDir, this.journalFile);
9813
+ const entry = { id: randomUUID(), event };
9814
+ fs7.appendFileSync(journalPath, JSON.stringify(entry) + "\n", "utf-8");
9166
9815
  }
9167
- appendEventToJournal(dir, {
9168
- type: "task-failed",
9169
- taskName: decoded.taskName,
9170
- error: errorMsg,
9171
- timestamp: (/* @__PURE__ */ new Date()).toISOString()
9172
- });
9173
- void processAccumulatedEventsForced(dir);
9174
- console.log("Task failed.");
9816
+ };
9817
+ function createNodeInputStore(journalFile) {
9818
+ return new NodeInputStore(journalFile);
9175
9819
  }
9176
- function cmdValidateCard(args) {
9177
- const cardIdx = args.indexOf("--card");
9178
- const globIdx = args.indexOf("--card-glob");
9179
- const rgIdx = args.indexOf("--rg");
9180
- const cardFile = cardIdx !== -1 ? args[cardIdx + 1] : void 0;
9181
- const cardGlob = globIdx !== -1 ? args[globIdx + 1] : void 0;
9182
- const boardDir = rgIdx !== -1 ? args[rgIdx + 1] : void 0;
9183
- if (!cardFile && !cardGlob || cardFile && cardGlob) {
9184
- throw new Error("Usage: board-live-cards validate-card (--card <card.json> | --card-glob <glob>) [--rg <boardDir>]");
9185
- }
9186
- let teConfig;
9187
- if (boardDir) {
9188
- teConfig = readTaskExecutorConfig(boardDir);
9189
- if (!teConfig) {
9190
- throw new Error(`--rg specified but no .task-executor found in ${boardDir}`);
9820
+ function shouldSuppressSpawn() {
9821
+ return process.env.BOARD_LIVE_CARDS_NO_SPAWN === "1";
9822
+ }
9823
+ function getCliInvocationPath(cliDir) {
9824
+ const tsPath = path7.join(cliDir, "board-live-cards-cli.ts");
9825
+ const localTsxBin = path7.join(cliDir, "..", "..", "node_modules", ".bin", "tsx");
9826
+ const localTsxMjs = path7.join(cliDir, "..", "..", "node_modules", "tsx", "dist", "cli.mjs");
9827
+ const localTsx = fs7.existsSync(localTsxMjs) ? localTsxMjs : localTsxBin;
9828
+ if (fs7.existsSync(tsPath) && fs7.existsSync(localTsx)) {
9829
+ return { cmd: process.execPath, args: [localTsx, tsPath] };
9830
+ }
9831
+ const distJsPath = path7.join(cliDir, "dist", "cli", "board-live-cards-cli.js");
9832
+ if (fs7.existsSync(distJsPath)) {
9833
+ return { cmd: process.execPath, args: [distJsPath] };
9834
+ }
9835
+ const jsPath = path7.join(cliDir, "board-live-cards-cli.js");
9836
+ if (fs7.existsSync(jsPath)) {
9837
+ return { cmd: process.execPath, args: [jsPath] };
9838
+ }
9839
+ return null;
9840
+ }
9841
+ function buildCliInvocation(cliDir, command, args) {
9842
+ const found = getCliInvocationPath(cliDir);
9843
+ if (found) {
9844
+ return { cmd: found.cmd, args: [...found.args, command, ...args] };
9845
+ }
9846
+ const tsPath = path7.join(cliDir, "board-live-cards-cli.ts");
9847
+ const npxCmd = process.platform === "win32" ? "npx.cmd" : "npx";
9848
+ return { cmd: npxCmd, args: ["tsx", tsPath, command, ...args] };
9849
+ }
9850
+ var _gitBashCache;
9851
+ var GIT_BASH_CACHE_FILE = path7.join(os3.tmpdir(), ".board-live-cards-git-bash-path.json");
9852
+ function findGitBash() {
9853
+ if (_gitBashCache !== void 0) return _gitBashCache;
9854
+ try {
9855
+ const cached = JSON.parse(fs7.readFileSync(GIT_BASH_CACHE_FILE, "utf-8"));
9856
+ if (typeof cached?.path === "string" || cached?.path === false) {
9857
+ _gitBashCache = cached.path;
9858
+ return _gitBashCache;
9859
+ }
9860
+ } catch {
9861
+ }
9862
+ const candidates = [
9863
+ "C:\\Program Files\\Git\\bin\\bash.exe",
9864
+ "C:\\Program Files (x86)\\Git\\bin\\bash.exe"
9865
+ ];
9866
+ for (const c of candidates) {
9867
+ if (fs7.existsSync(c)) {
9868
+ _gitBashCache = c;
9869
+ try {
9870
+ fs7.writeFileSync(GIT_BASH_CACHE_FILE, JSON.stringify({ path: c }));
9871
+ } catch {
9872
+ }
9873
+ return c;
9874
+ }
9875
+ }
9876
+ _gitBashCache = false;
9877
+ try {
9878
+ fs7.writeFileSync(GIT_BASH_CACHE_FILE, JSON.stringify({ path: false }));
9879
+ } catch {
9880
+ }
9881
+ return false;
9882
+ }
9883
+ function shellQuote(s) {
9884
+ return "'" + s.replace(/'/g, "'\\''") + "'";
9885
+ }
9886
+ function spawnDetached(cmd, args) {
9887
+ if (process.platform === "win32") {
9888
+ const bash = findGitBash();
9889
+ if (bash) {
9890
+ const shellCmd = [cmd, ...args].map((a) => shellQuote(a.replace(/\\/g, "/"))).join(" ");
9891
+ const child3 = spawn(bash, ["-c", shellCmd], { detached: true, stdio: "ignore", windowsHide: true });
9892
+ child3.unref();
9893
+ return;
9894
+ }
9895
+ const child2 = spawn("cmd", ["/c", "start", "/b", "", cmd, ...args], {
9896
+ detached: true,
9897
+ stdio: "ignore",
9898
+ windowsHide: true
9899
+ });
9900
+ child2.unref();
9901
+ return;
9902
+ }
9903
+ const child = spawn(cmd, args, { detached: true, stdio: "ignore" });
9904
+ child.unref();
9905
+ }
9906
+ var NodeInvocationAdapter = class {
9907
+ constructor(cliDir, encodeSourceToken2) {
9908
+ this.cliDir = cliDir;
9909
+ this.encodeSourceToken = encodeSourceToken2;
9910
+ }
9911
+ cliDir;
9912
+ encodeSourceToken;
9913
+ async requestSourceFetch(boardDir, enrichedCard, callbackToken) {
9914
+ if (shouldSuppressSpawn()) {
9915
+ return { dispatched: false, invocationId: void 0 };
9916
+ }
9917
+ try {
9918
+ const cardId = enrichedCard.id ?? "unknown";
9919
+ const enrichedCardPath = path7.join(os3.tmpdir(), `card-enriched-${cardId}-${Date.now()}.json`);
9920
+ fs7.writeFileSync(enrichedCardPath, JSON.stringify(enrichedCard, null, 2), "utf-8");
9921
+ const args = ["--card", enrichedCardPath, "--token", callbackToken, "--rg", boardDir];
9922
+ const { cmd, args: cmdArgs } = buildCliInvocation(this.cliDir, "run-sourcedefs-internal", args);
9923
+ const invocationId = randomUUID();
9924
+ spawnDetached(cmd, cmdArgs);
9925
+ return { dispatched: true, invocationId };
9926
+ } catch (err) {
9927
+ return { dispatched: false, error: err instanceof Error ? err.message : String(err) };
9191
9928
  }
9192
9929
  }
9193
- const files = cardFile ? [path.resolve(cardFile)] : resolveCardGlobMatches(cardGlob);
9194
- if (files.length === 0) {
9195
- throw new Error(`No card files matched glob: ${cardGlob}`);
9196
- }
9197
- let failures = 0;
9198
- for (const f of files) {
9199
- const label = path.relative(process.cwd(), f) || f;
9200
- if (!fs.existsSync(f)) {
9201
- console.error(`FAIL ${label}: file not found`);
9202
- failures++;
9203
- continue;
9930
+ async requestInference(boardDir, cardId, inferencePayload, callbackToken) {
9931
+ if (shouldSuppressSpawn()) {
9932
+ return { dispatched: false, invocationId: void 0 };
9204
9933
  }
9205
- let card;
9206
9934
  try {
9207
- card = JSON.parse(fs.readFileSync(f, "utf-8"));
9935
+ const inferenceInFile = path7.join(os3.tmpdir(), `card-inference-${cardId}-${Date.now()}.json`);
9936
+ fs7.writeFileSync(inferenceInFile, JSON.stringify(inferencePayload, null, 2), "utf-8");
9937
+ const inferenceToken = this.encodeSourceToken({
9938
+ cbk: callbackToken,
9939
+ rg: boardDir,
9940
+ cid: cardId,
9941
+ b: "",
9942
+ d: "",
9943
+ cs: void 0
9944
+ });
9945
+ const { cmd, args } = buildCliInvocation(
9946
+ this.cliDir,
9947
+ "run-inference-internal",
9948
+ ["--in", inferenceInFile, "--token", inferenceToken]
9949
+ );
9950
+ const invocationId = randomUUID();
9951
+ spawnDetached(cmd, args);
9952
+ return { dispatched: true, invocationId };
9208
9953
  } catch (err) {
9209
- console.error(`FAIL ${label}: invalid JSON \u2014 ${err instanceof Error ? err.message : String(err)}`);
9210
- failures++;
9211
- continue;
9954
+ return { dispatched: false, error: err instanceof Error ? err.message : String(err) };
9212
9955
  }
9213
- const result = validateLiveCardDefinition(card);
9214
- const sourceErrors = [];
9215
- if (teConfig && Array.isArray(card.source_defs)) {
9216
- for (const src of card.source_defs) {
9217
- const bindTo = typeof src.bindTo === "string" ? src.bindTo : "(unknown)";
9218
- const tmpFile = path.join(os.tmpdir(), `validate-src-${bindTo}-${Date.now()}.json`);
9219
- try {
9220
- fs.writeFileSync(tmpFile, JSON.stringify(src), "utf-8");
9221
- let stdout;
9222
- try {
9223
- stdout = execCommandSync(teConfig.command, ["validate-source-def", "--in", tmpFile], { shell: true, timeout: 1e4 });
9224
- } catch (execErr) {
9225
- stdout = typeof execErr?.stdout === "string" ? execErr.stdout : Buffer.isBuffer(execErr?.stdout) ? execErr.stdout.toString("utf-8") : "";
9226
- if (!stdout.trim()) {
9227
- sourceErrors.push(`source "${bindTo}": executor validate-source-def failed \u2014 ${execErr instanceof Error ? execErr.message : String(execErr)}`);
9228
- continue;
9229
- }
9230
- }
9231
- const parsed = JSON.parse(stdout.trim());
9232
- if (!parsed.ok && Array.isArray(parsed.errors)) {
9233
- for (const e of parsed.errors) {
9234
- sourceErrors.push(`source "${bindTo}": ${e}`);
9235
- }
9236
- }
9237
- } catch (err) {
9238
- sourceErrors.push(`source "${bindTo}": executor validate-source-def failed \u2014 ${err instanceof Error ? err.message : String(err)}`);
9239
- } finally {
9240
- try {
9241
- fs.unlinkSync(tmpFile);
9242
- } catch {
9243
- }
9244
- }
9245
- }
9956
+ }
9957
+ };
9958
+ function createNodeInvocationAdapter(cliDir, encodeSourceToken2) {
9959
+ return new NodeInvocationAdapter(cliDir, encodeSourceToken2);
9960
+ }
9961
+ var NodeCardStore = class {
9962
+ constructor(lookupCardPathFn) {
9963
+ this.lookupCardPathFn = lookupCardPathFn;
9964
+ }
9965
+ lookupCardPathFn;
9966
+ readCard(cardPath) {
9967
+ try {
9968
+ return JSON.parse(fs7.readFileSync(cardPath, "utf-8"));
9969
+ } catch {
9970
+ return null;
9246
9971
  }
9247
- const allErrors = [...result.errors, ...sourceErrors];
9248
- if (allErrors.length === 0) {
9249
- console.log(`OK ${label}`);
9250
- } else {
9251
- console.error(`FAIL ${label}:`);
9252
- for (const e of allErrors) {
9253
- console.error(` ${e}`);
9972
+ }
9973
+ readSourceFileContent(boardDir, cardId, outputFile) {
9974
+ const filePath = path7.join(boardDir, cardId, outputFile);
9975
+ if (!fs7.existsSync(filePath)) return null;
9976
+ try {
9977
+ const raw = fs7.readFileSync(filePath, "utf-8").trim();
9978
+ try {
9979
+ return JSON.parse(raw);
9980
+ } catch {
9981
+ return raw;
9254
9982
  }
9255
- failures++;
9983
+ } catch {
9984
+ return null;
9256
9985
  }
9257
9986
  }
9258
- if (failures > 0) {
9259
- throw new Error(`${failures} of ${files.length} card(s) failed validation.`);
9260
- } else {
9261
- console.log(`
9262
- ${files.length} card(s) passed validation.`);
9987
+ lookupCardPath(boardDir, nodeId) {
9988
+ return this.lookupCardPathFn(boardDir, nodeId);
9263
9989
  }
9990
+ };
9991
+ function createNodeCardStore(lookupCardPath2) {
9992
+ return new NodeCardStore(lookupCardPath2);
9264
9993
  }
9265
- function cmdRemoveCard(args) {
9266
- const rgIdx = args.indexOf("--rg");
9267
- const idIdx = args.indexOf("--id");
9268
- const dir = rgIdx !== -1 ? args[rgIdx + 1] : void 0;
9269
- const cardId = idIdx !== -1 ? args[idIdx + 1] : void 0;
9270
- if (!dir || !cardId) {
9271
- console.error("Usage: board-live-cards remove-card --rg <dir> --id <card-id>");
9272
- process.exit(1);
9994
+
9995
+ // src/cli/board-live-cards-cli.ts
9996
+ var BOARD_FILE = "board-graph.json";
9997
+ var JOURNAL_FILE = "board-journal.jsonl";
9998
+ var TASK_EXECUTOR_LOG_FILE = "task-executor.jsonl";
9999
+ var INFERENCE_ADAPTER_LOG_FILE = "inference-adapter.jsonl";
10000
+ var INVENTORY_FILE = "cards-inventory.jsonl";
10001
+ var RUNTIME_OUT_FILE = ".runtime-out";
10002
+ var DEFAULT_RUNTIME_OUT_DIR = "runtime-out";
10003
+ var RUNTIME_STATUS_FILE = "board-livegraph-status.json";
10004
+ var RUNTIME_CARDS_DIR = "cards";
10005
+ var RUNTIME_DATA_OBJECTS_DIR = "data-objects";
10006
+ var INFERENCE_ADAPTER_FILE = ".inference-adapter";
10007
+ var TASK_EXECUTOR_FILE = ".task-executor";
10008
+ function readTaskExecutorConfig(boardDir) {
10009
+ const executorFile = path7.join(boardDir, TASK_EXECUTOR_FILE);
10010
+ if (!fs7.existsSync(executorFile)) return void 0;
10011
+ const raw = fs7.readFileSync(executorFile, "utf-8").trim();
10012
+ if (!raw) return void 0;
10013
+ try {
10014
+ const parsed = JSON.parse(raw);
10015
+ if (parsed && typeof parsed === "object" && typeof parsed.command === "string") {
10016
+ return parsed;
10017
+ }
10018
+ } catch {
9273
10019
  }
9274
- appendEventToJournal(dir, {
9275
- type: "task-removal",
9276
- taskName: cardId,
9277
- timestamp: (/* @__PURE__ */ new Date()).toISOString()
9278
- });
9279
- void processAccumulatedEventsInfinitePass(dir);
9280
- console.log(`Card "${cardId}" removed.`);
10020
+ return { command: raw };
9281
10021
  }
9282
- function cmdSourceDataFetched(args) {
9283
- const tmpIdx = args.indexOf("--tmp");
9284
- const tokenIdx = args.indexOf("--token");
9285
- const tmpFile = tmpIdx !== -1 ? args[tmpIdx + 1] : void 0;
9286
- const token = tokenIdx !== -1 ? args[tokenIdx + 1] : void 0;
9287
- if (!tmpFile || !token) {
9288
- console.error("Usage: board-live-cards source-data-fetched --tmp <tmp-file> --token <sourceToken>");
9289
- process.exit(1);
10022
+ var EMPTY_CONFIG = { settings: { completion: "manual", refreshStrategy: "data-changed" }, tasks: {} };
10023
+ var BoardJournal = class {
10024
+ journalPath;
10025
+ lastDrainedId;
10026
+ constructor(journalPath, lastDrainedJournalId) {
10027
+ this.journalPath = journalPath;
10028
+ this.lastDrainedId = lastDrainedJournalId;
9290
10029
  }
9291
- const payload = decodeSourceToken(token);
9292
- if (!payload) {
9293
- console.error("Invalid source token");
9294
- process.exit(1);
10030
+ append(event) {
10031
+ const entry = { id: randomUUID(), event };
10032
+ fs7.appendFileSync(this.journalPath, JSON.stringify(entry) + "\n", "utf-8");
9295
10033
  }
9296
- const { cbk, rg, cid, b, d, cs } = payload;
9297
- const destPath = path.join(rg, cid, d);
9298
- fs.mkdirSync(path.dirname(destPath), { recursive: true });
9299
- fs.renameSync(tmpFile, destPath);
9300
- console.log(`[source-data-fetched] ${cid}.${b} \u2192 ${cid}/${d}`);
9301
- const fetchedAt = (/* @__PURE__ */ new Date()).toISOString();
9302
- const cbkDecoded = decodeCallbackToken2(cbk);
9303
- if (!cbkDecoded) {
9304
- console.error("Invalid callback token embedded in source token");
9305
- process.exit(1);
10034
+ drain() {
10035
+ if (!fs7.existsSync(this.journalPath)) return [];
10036
+ const content = fs7.readFileSync(this.journalPath, "utf-8").trim();
10037
+ if (!content) return [];
10038
+ const entries = content.split("\n").map((l) => JSON.parse(l));
10039
+ let startIdx = 0;
10040
+ if (this.lastDrainedId) {
10041
+ const drainedIdx = entries.findIndex((e) => e.id === this.lastDrainedId);
10042
+ if (drainedIdx !== -1) startIdx = drainedIdx + 1;
10043
+ }
10044
+ const undrained = entries.slice(startIdx);
10045
+ if (undrained.length > 0) {
10046
+ this.lastDrainedId = undrained[undrained.length - 1].id;
10047
+ }
10048
+ return undrained.map((e) => e.event);
9306
10049
  }
9307
- appendEventToJournal(rg, {
9308
- type: "task-progress",
9309
- taskName: cbkDecoded.taskName,
9310
- update: { bindTo: b, outputFile: d, fetchedAt, sourceChecksum: cs },
9311
- timestamp: fetchedAt
9312
- });
9313
- void processAccumulatedEventsInfinitePass(rg);
9314
- }
9315
- function cmdSourceDataFetchFailure(args) {
9316
- const tokenIdx = args.indexOf("--token");
9317
- const reasonIdx = args.indexOf("--reason");
9318
- const token = tokenIdx !== -1 ? args[tokenIdx + 1] : void 0;
9319
- const reason = reasonIdx !== -1 ? args[reasonIdx + 1] : "unknown";
9320
- if (!token) {
9321
- console.error("Usage: board-live-cards source-data-fetch-failure --token <sourceToken> [--reason <msg>]");
9322
- process.exit(1);
10050
+ get size() {
10051
+ if (!fs7.existsSync(this.journalPath)) return 0;
10052
+ const content = fs7.readFileSync(this.journalPath, "utf-8").trim();
10053
+ if (!content) return 0;
10054
+ const entries = content.split("\n").map((l) => JSON.parse(l));
10055
+ if (!this.lastDrainedId) return entries.length;
10056
+ const drainedIdx = entries.findIndex((e) => e.id === this.lastDrainedId);
10057
+ return drainedIdx === -1 ? entries.length : entries.length - drainedIdx - 1;
9323
10058
  }
9324
- const payload = decodeSourceToken(token);
9325
- if (!payload) {
9326
- console.error("Invalid source token");
9327
- process.exit(1);
10059
+ get lastDrainedJournalId() {
10060
+ return this.lastDrainedId;
9328
10061
  }
9329
- const { cbk, rg, cid, b, d, cs } = payload;
9330
- console.log(`[source-data-fetch-failure] ${cid}.${b}: ${reason}`);
9331
- const cbkDecoded = decodeCallbackToken2(cbk);
9332
- if (!cbkDecoded) {
9333
- console.error("Invalid callback token embedded in source token");
9334
- process.exit(1);
10062
+ };
10063
+ function readCardInventory(boardDir) {
10064
+ const inventoryPath = path7.join(boardDir, INVENTORY_FILE);
10065
+ if (!fs7.existsSync(inventoryPath)) return [];
10066
+ const lines = fs7.readFileSync(inventoryPath, "utf-8").split("\n").filter((l) => l.trim());
10067
+ return lines.map((l) => JSON.parse(l));
10068
+ }
10069
+ function lookupCardPath(boardDir, cardId) {
10070
+ const entries = readCardInventory(boardDir);
10071
+ const entry = entries.find((e) => e.cardId === cardId);
10072
+ return entry?.cardFilePath ?? null;
10073
+ }
10074
+ function appendCardInventory(boardDir, entry) {
10075
+ const inventoryPath = path7.join(boardDir, INVENTORY_FILE);
10076
+ const normalized = { ...entry, cardFilePath: path7.resolve(entry.cardFilePath) };
10077
+ fs7.appendFileSync(inventoryPath, JSON.stringify(normalized) + "\n");
10078
+ }
10079
+ function buildCardInventoryIndex(boardDir) {
10080
+ const byCardId = /* @__PURE__ */ new Map();
10081
+ const byCardPath = /* @__PURE__ */ new Map();
10082
+ for (const entry of readCardInventory(boardDir)) {
10083
+ const normalizedPath = path7.resolve(entry.cardFilePath);
10084
+ const normalizedEntry = {
10085
+ ...entry,
10086
+ cardFilePath: normalizedPath
10087
+ };
10088
+ const existingById = byCardId.get(entry.cardId);
10089
+ if (existingById && existingById.cardFilePath !== normalizedPath) {
10090
+ throw new Error(
10091
+ `Inventory invariant violation: card id "${entry.cardId}" maps to multiple files: "${existingById.cardFilePath}" and "${normalizedPath}"`
10092
+ );
10093
+ }
10094
+ const existingByPath = byCardPath.get(normalizedPath);
10095
+ if (existingByPath && existingByPath.cardId !== entry.cardId) {
10096
+ throw new Error(
10097
+ `Inventory invariant violation: file "${normalizedPath}" maps to multiple ids: "${existingByPath.cardId}" and "${entry.cardId}"`
10098
+ );
10099
+ }
10100
+ byCardId.set(entry.cardId, normalizedEntry);
10101
+ byCardPath.set(normalizedPath, normalizedEntry);
9335
10102
  }
9336
- const timestamp = (/* @__PURE__ */ new Date()).toISOString();
9337
- appendEventToJournal(rg, {
9338
- type: "task-progress",
9339
- taskName: cbkDecoded.taskName,
9340
- update: { bindTo: b, outputFile: d, failure: true, reason, sourceChecksum: cs },
9341
- timestamp
9342
- });
9343
- void processAccumulatedEventsInfinitePass(rg);
10103
+ return { byCardId, byCardPath };
9344
10104
  }
9345
- function cmdRunSources(args) {
9346
- const cardIdx = args.indexOf("--card");
9347
- const tokenIdx = args.indexOf("--token");
9348
- const rgIdx = args.indexOf("--rg");
9349
- const sourceChecksumsIdx = args.indexOf("--source-checksums");
9350
- const cardFilePath = cardIdx !== -1 ? args[cardIdx + 1] : void 0;
9351
- const callbackToken = tokenIdx !== -1 ? args[tokenIdx + 1] : void 0;
9352
- const boardDir = rgIdx !== -1 ? args[rgIdx + 1] : void 0;
9353
- const sourceChecksumsJson = sourceChecksumsIdx !== -1 ? args[sourceChecksumsIdx + 1] : void 0;
9354
- const sourceChecksums = sourceChecksumsJson ? JSON.parse(sourceChecksumsJson) : void 0;
9355
- if (!cardFilePath || !callbackToken || !boardDir) {
9356
- console.error("Usage: board-live-cards run-sourcedefs-internal --card <path> --token <token> --rg <dir> [--source-checksums <json>]");
9357
- process.exit(1);
10105
+ function initBoard(dir) {
10106
+ const boardPath = path7.join(dir, BOARD_FILE);
10107
+ if (fs7.existsSync(boardPath)) {
10108
+ const envelope2 = JSON.parse(fs7.readFileSync(boardPath, "utf-8"));
10109
+ restore(envelope2.graph);
10110
+ return "exists";
9358
10111
  }
9359
- const card = JSON.parse(fs.readFileSync(cardFilePath, "utf-8"));
9360
- if (path.basename(cardFilePath).startsWith("card-enriched-")) {
9361
- try {
9362
- fs.unlinkSync(cardFilePath);
9363
- } catch {
10112
+ if (fs7.existsSync(dir)) {
10113
+ const entries = fs7.readdirSync(dir);
10114
+ if (entries.length > 0) {
10115
+ throw new Error(`Directory "${dir}" is not empty and has no valid ${BOARD_FILE}`);
9364
10116
  }
9365
10117
  }
9366
- console.log(`[run-sourcedefs-internal] Processing card "${card.id}"`);
9367
- const teConfig = readTaskExecutorConfig(boardDir);
9368
- const taskExecutor = teConfig?.command;
9369
- const taskExecutorExtraB64 = teConfig?.extra ? Buffer.from(JSON.stringify(teConfig.extra)).toString("base64") : void 0;
9370
- function runSource(src) {
9371
- const sourceChecksumForInvoke = src.outputFile ? sourceChecksums?.[src.outputFile] : void 0;
9372
- const sourceToken = encodeSourceToken({
9373
- cbk: callbackToken,
9374
- rg: boardDir,
9375
- cid: card.id,
9376
- b: src.bindTo,
9377
- d: src.outputFile ?? "",
9378
- cs: sourceChecksumForInvoke
9379
- });
9380
- function reportFailure(reason) {
9381
- invokeSourceDataFetchFailure(sourceToken, reason, (err) => {
9382
- if (err) console.error(`[run-sourcedefs-internal] source-data-fetch-failure call failed:`, err.message);
9383
- });
9384
- }
9385
- function reportFetched(outFile2) {
9386
- invokeSourceDataFetched(sourceToken, outFile2);
9387
- }
9388
- if (taskExecutor) {
9389
- if (!src.outputFile) {
9390
- console.warn(`[run-sourcedefs-internal] source "${src.bindTo}" has no outputFile configured \u2014 cannot deliver`);
9391
- reportFailure("no outputFile configured");
9392
- return;
9393
- }
9394
- const inFile = path.join(os.tmpdir(), `card-source-in-${src.bindTo}-${Date.now()}.json`);
9395
- const outFile2 = path.join(os.tmpdir(), `card-source-out-${src.bindTo}-${Date.now()}.json`);
9396
- const errFile = path.join(os.tmpdir(), `card-source-err-${src.bindTo}-${Date.now()}.txt`);
9397
- const sourceForExecutor = {
9398
- ...src,
9399
- cwd: typeof src.cwd === "string" && src.cwd ? src.cwd : path.dirname(cardFilePath || ""),
9400
- boardDir: typeof src.boardDir === "string" && src.boardDir ? src.boardDir : boardDir
9401
- };
9402
- appendTaskExecutorLog(boardDir, sourceForExecutor, "external-task-executor");
9403
- fs.writeFileSync(inFile, JSON.stringify(sourceForExecutor, null, 2), "utf-8");
9404
- const executorArgs = ["run-source-fetch", "--in", inFile, "--out", outFile2, "--err", errFile];
9405
- if (taskExecutorExtraB64) executorArgs.push("--extra", taskExecutorExtraB64);
9406
- console.log(`[run-sourcedefs-internal] task-executor: ${taskExecutor} ${executorArgs.join(" ")}`);
9407
- try {
9408
- execCommandSync(taskExecutor, executorArgs, {
9409
- shell: true,
9410
- timeout: src.timeout ?? 12e4
9411
- });
9412
- } catch (err) {
9413
- const reason = err.message ?? String(err);
9414
- console.error(`[run-sourcedefs-internal] task-executor failed for source "${src.bindTo}":`, reason);
9415
- reportFailure(reason);
9416
- return;
9417
- }
9418
- if (fs.existsSync(outFile2)) {
9419
- reportFetched(outFile2);
9420
- } else {
9421
- const errMsg = fs.existsSync(errFile) ? fs.readFileSync(errFile, "utf-8").trim() : "executor produced no output file";
9422
- console.warn(`[run-sourcedefs-internal] source "${src.bindTo}": ${errMsg}`);
9423
- reportFailure(errMsg);
9424
- }
9425
- return;
9426
- }
9427
- if (!src.outputFile) {
9428
- console.warn(`[run-sourcedefs-internal] source "${src.bindTo}" has no outputFile configured \u2014 cannot deliver`);
9429
- reportFailure("no outputFile configured");
9430
- return;
9431
- }
9432
- const outFile = path.join(os.tmpdir(), `card-source-out-${src.bindTo}-${Date.now()}.json`);
9433
- if (!src.cli) {
9434
- const errMsg = "source.cli is required for built-in source execution";
9435
- console.warn(`[run-sourcedefs-internal] source "${src.bindTo}": ${errMsg}`);
9436
- reportFailure(errMsg);
9437
- return;
9438
- }
9439
- const timeout = src.timeout ?? 12e4;
9440
- const sourceCwd = typeof src.cwd === "string" ? src.cwd : path.dirname(cardFilePath || "");
9441
- const sourceBoardDir = typeof src.boardDir === "string" ? src.boardDir : boardDir;
9442
- const sourceForBuiltInExecutor = {
9443
- ...src,
9444
- cwd: sourceCwd,
9445
- boardDir: sourceBoardDir
9446
- };
9447
- appendTaskExecutorLog(boardDir, sourceForBuiltInExecutor, "built-in-run-source-fetch");
9448
- const cmdParts = splitCommandLine(src.cli);
9449
- if (cmdParts.length === 0) {
9450
- const errMsg = "source.cli command is empty";
9451
- console.warn(`[run-sourcedefs-internal] source "${src.bindTo}": ${errMsg}`);
9452
- reportFailure(errMsg);
9453
- return;
9454
- }
9455
- const rawCmd = cmdParts[0];
9456
- const { cmd, args: cliArgs } = resolveCommandInvocation(rawCmd, cmdParts.slice(1));
9457
- let stdout;
9458
- try {
9459
- stdout = execCommandSync(cmd, cliArgs, {
9460
- shell: false,
9461
- encoding: "utf-8",
9462
- timeout,
9463
- cwd: sourceCwd,
9464
- env: {
9465
- ...process.env,
9466
- ...sourceBoardDir ? { BOARD_DIR: sourceBoardDir } : {}
9467
- }
9468
- });
9469
- } catch (err) {
9470
- const reason = err.message ?? String(err);
9471
- console.error(`[run-sourcedefs-internal] source fetch failed for source "${src.bindTo}":`, reason);
9472
- reportFailure(reason);
9473
- return;
10118
+ fs7.mkdirSync(dir, { recursive: true });
10119
+ const live = createLiveGraph(EMPTY_CONFIG);
10120
+ const snap = snapshot(live);
10121
+ const envelope = { lastDrainedJournalId: "", graph: snap };
10122
+ fs7.writeFileSync(boardPath, JSON.stringify(envelope, null, 2));
10123
+ return "created";
10124
+ }
10125
+ function loadBoardEnvelope(dir) {
10126
+ const raw = fs7.readFileSync(path7.join(dir, BOARD_FILE), "utf-8");
10127
+ return JSON.parse(raw);
10128
+ }
10129
+ function loadBoard(dir) {
10130
+ const envelope = loadBoardEnvelope(dir);
10131
+ return restore(envelope.graph);
10132
+ }
10133
+ function saveBoard(dir, rg, journal) {
10134
+ const snap = rg.snapshot();
10135
+ const envelope = {
10136
+ lastDrainedJournalId: journal.lastDrainedJournalId,
10137
+ graph: snap
10138
+ };
10139
+ writeJsonAtomic2(path7.join(dir, BOARD_FILE), envelope);
10140
+ const live = restore(snap);
10141
+ const statusObject = buildBoardStatusObject(path7.resolve(dir), live);
10142
+ writeJsonAtomic2(resolveStatusSnapshotPath(dir), statusObject);
10143
+ }
10144
+ function runtimeOutConfigPath(boardDir) {
10145
+ return path7.join(boardDir, RUNTIME_OUT_FILE);
10146
+ }
10147
+ function resolveConfiguredRuntimeOutDir(boardDir) {
10148
+ const cfgPath = runtimeOutConfigPath(boardDir);
10149
+ if (fs7.existsSync(cfgPath)) {
10150
+ const configured = fs7.readFileSync(cfgPath, "utf-8").trim();
10151
+ if (configured) {
10152
+ return path7.isAbsolute(configured) ? configured : path7.resolve(boardDir, configured);
9474
10153
  }
9475
- fs.writeFileSync(outFile, stdout.trim(), "utf-8");
9476
- reportFetched(outFile);
9477
10154
  }
9478
- const source_defs = card.source_defs ?? [];
9479
- for (const src of source_defs) {
9480
- runSource(src);
10155
+ const defaultDir = path7.join(boardDir, DEFAULT_RUNTIME_OUT_DIR);
10156
+ fs7.writeFileSync(cfgPath, defaultDir, "utf-8");
10157
+ return defaultDir;
10158
+ }
10159
+ function configureRuntimeOutDir(boardDir, runtimeOut) {
10160
+ let resolved;
10161
+ if (runtimeOut) {
10162
+ resolved = path7.isAbsolute(runtimeOut) ? runtimeOut : path7.resolve(boardDir, runtimeOut);
10163
+ } else {
10164
+ resolved = path7.join(boardDir, DEFAULT_RUNTIME_OUT_DIR);
10165
+ }
10166
+ fs7.mkdirSync(resolved, { recursive: true });
10167
+ fs7.writeFileSync(runtimeOutConfigPath(boardDir), resolved, "utf-8");
10168
+ return resolved;
10169
+ }
10170
+ function resolveStatusSnapshotPath(boardDir) {
10171
+ return path7.join(resolveConfiguredRuntimeOutDir(boardDir), RUNTIME_STATUS_FILE);
10172
+ }
10173
+ function resolveComputedValuesPath(boardDir, cardId) {
10174
+ return path7.join(resolveConfiguredRuntimeOutDir(boardDir), RUNTIME_CARDS_DIR, `${cardId}.computed.json`);
10175
+ }
10176
+ function resolveDataObjectsDirPath(boardDir) {
10177
+ return path7.join(resolveConfiguredRuntimeOutDir(boardDir), RUNTIME_DATA_OBJECTS_DIR);
10178
+ }
10179
+ function toDataObjectFileName(token) {
10180
+ return token.replace(/[\\/]/g, "__");
10181
+ }
10182
+ function writeRuntimeDataObjects(boardDir, data) {
10183
+ for (const [token, payload] of Object.entries(data)) {
10184
+ if (!token) continue;
10185
+ const fileName = toDataObjectFileName(token);
10186
+ if (!fileName) continue;
10187
+ const filePath = path7.join(resolveDataObjectsDirPath(boardDir), fileName);
10188
+ writeJsonAtomic2(filePath, payload);
10189
+ }
10190
+ }
10191
+ function writeJsonAtomic2(filePath, payload) {
10192
+ fs7.mkdirSync(path7.dirname(filePath), { recursive: true });
10193
+ const tmpPath = `${filePath}.${process.pid}.${randomUUID()}.tmp`;
10194
+ fs7.writeFileSync(tmpPath, JSON.stringify(payload, null, 2), "utf-8");
10195
+ fs7.renameSync(tmpPath, filePath);
10196
+ }
10197
+ function withBoardLock(boardDir, fn) {
10198
+ const boardPath = path7.join(boardDir, BOARD_FILE);
10199
+ const release = lockSync(boardPath, { retries: { retries: 5, minTimeout: 100 } });
10200
+ try {
10201
+ return fn();
10202
+ } finally {
10203
+ release();
10204
+ }
10205
+ }
10206
+ function decodeCallbackToken2(token) {
10207
+ try {
10208
+ const payload = JSON.parse(Buffer.from(token, "base64url").toString());
10209
+ if (typeof payload?.t === "string") return { taskName: payload.t };
10210
+ return null;
10211
+ } catch {
10212
+ return null;
9481
10213
  }
9482
10214
  }
9483
- function cmdTaskProgress(args) {
9484
- const rgIdx = args.indexOf("--rg");
9485
- const tokenIdx = args.indexOf("--token");
9486
- const updateIdx = args.indexOf("--update");
9487
- const dir = rgIdx !== -1 ? args[rgIdx + 1] : void 0;
9488
- const token = tokenIdx !== -1 ? args[tokenIdx + 1] : void 0;
9489
- const updateJson = updateIdx !== -1 ? args[updateIdx + 1] : "{}";
9490
- if (!dir || !token) {
9491
- console.error("Usage: board-live-cards task-progress --rg <dir> --token <token> [--update <json>]");
9492
- process.exit(1);
9493
- }
9494
- const decoded = decodeCallbackToken2(token);
9495
- if (!decoded) {
9496
- console.error("Invalid callback token");
9497
- process.exit(1);
9498
- }
9499
- const update = updateJson ? JSON.parse(updateJson) : {};
9500
- appendEventToJournal(dir, {
9501
- type: "task-progress",
9502
- taskName: decoded.taskName,
9503
- update,
9504
- timestamp: (/* @__PURE__ */ new Date()).toISOString()
9505
- });
9506
- void processAccumulatedEventsInfinitePass(dir);
10215
+ function encodeSourceToken(payload) {
10216
+ return Buffer.from(JSON.stringify(payload)).toString("base64url");
9507
10217
  }
9508
- function cmdRunInference(args) {
9509
- const inIdx = args.indexOf("--in");
9510
- const tokenIdx = args.indexOf("--token");
9511
- const inFile = inIdx !== -1 ? args[inIdx + 1] : void 0;
9512
- const inferenceToken = tokenIdx !== -1 ? args[tokenIdx + 1] : void 0;
9513
- if (!inFile || !inferenceToken) {
9514
- console.error("Usage: board-live-cards run-inference-internal --in <input.json> --token <inference-token>");
9515
- process.exit(1);
9516
- }
9517
- const decodedToken = decodeSourceToken(inferenceToken);
9518
- if (!decodedToken) {
9519
- console.error("Invalid inference token");
9520
- process.exit(1);
9521
- }
9522
- const callbackToken = decodedToken.cbk;
9523
- const boardDir = decodedToken.rg;
9524
- const cbkDecoded = decodeCallbackToken2(callbackToken);
9525
- if (!cbkDecoded) {
9526
- console.error("Invalid callback token embedded in inference token");
9527
- process.exit(1);
9528
- }
9529
- function spawnInferenceDone(tmpFile) {
9530
- const { cmd, args: cliArgs } = getCliInvocation("inference-done", ["--tmp", tmpFile, "--token", inferenceToken]);
9531
- spawnDetachedCommand(cmd, cliArgs);
9532
- }
9533
- function spawnInferenceDoneError(reason) {
9534
- const tmpFile = path.join(os.tmpdir(), `card-inference-err-${Date.now()}.json`);
9535
- fs.writeFileSync(tmpFile, JSON.stringify({ isTaskCompleted: false, reason }), "utf-8");
9536
- spawnInferenceDone(tmpFile);
10218
+ function decodeSourceToken(token) {
10219
+ try {
10220
+ const p = JSON.parse(Buffer.from(token, "base64url").toString());
10221
+ if (typeof p?.cbk === "string" && typeof p?.cid === "string" && typeof p?.b === "string" && typeof p?.d === "string") {
10222
+ return p;
10223
+ }
10224
+ return null;
10225
+ } catch {
10226
+ return null;
9537
10227
  }
9538
- if (!fs.existsSync(inFile)) {
9539
- spawnInferenceDoneError(`inference input not found: ${inFile}`);
9540
- return;
10228
+ }
10229
+ function appendEventToJournal(boardDir, event) {
10230
+ const journalPath = path7.join(boardDir, JOURNAL_FILE);
10231
+ const entry = { id: randomUUID(), event };
10232
+ fs7.appendFileSync(journalPath, JSON.stringify(entry) + "\n", "utf-8");
10233
+ }
10234
+ function getUndrainedEntries(boardDir, lastDrainedId) {
10235
+ const journalPath = path7.join(boardDir, JOURNAL_FILE);
10236
+ if (!fs7.existsSync(journalPath)) return [];
10237
+ const content = fs7.readFileSync(journalPath, "utf-8").trim();
10238
+ if (!content) return [];
10239
+ const entries = content.split("\n").map((l) => JSON.parse(l));
10240
+ if (!lastDrainedId) return entries;
10241
+ const idx = entries.findIndex((e) => e.id === lastDrainedId);
10242
+ return idx === -1 ? entries : entries.slice(idx + 1);
10243
+ }
10244
+ function determineLatestPendingAccumulated(boardDir) {
10245
+ const boardPath = path7.join(boardDir, BOARD_FILE);
10246
+ if (!fs7.existsSync(boardPath)) return 0;
10247
+ try {
10248
+ const envelope = loadBoardEnvelope(boardDir);
10249
+ return getUndrainedEntries(boardDir, envelope.lastDrainedJournalId).length;
10250
+ } catch {
10251
+ return 0;
9541
10252
  }
9542
- const adapterFile = path.join(boardDir, INFERENCE_ADAPTER_FILE);
9543
- const inferenceAdapter = fs.existsSync(adapterFile) ? fs.readFileSync(adapterFile, "utf-8").trim() : void 0;
9544
- if (!inferenceAdapter) {
9545
- spawnInferenceDoneError(`inference adapter is not configured (${INFERENCE_ADAPTER_FILE})`);
9546
- return;
10253
+ }
10254
+ function shouldUseShellForCommand(cmd, forceShell) {
10255
+ if (typeof forceShell === "boolean") return forceShell;
10256
+ return process.platform === "win32" && /\.(cmd|bat)$/i.test(cmd);
10257
+ }
10258
+ var _gitBashPath;
10259
+ var GIT_BASH_CACHE_FILE2 = path7.join(os3.tmpdir(), ".board-live-cards-git-bash-cache.json");
10260
+ function findGitBash2() {
10261
+ if (_gitBashPath !== void 0) return _gitBashPath;
10262
+ if (process.platform !== "win32") return _gitBashPath = false;
10263
+ try {
10264
+ const cached = JSON.parse(fs7.readFileSync(GIT_BASH_CACHE_FILE2, "utf8"));
10265
+ if (cached.path === false || typeof cached.path === "string" && fs7.existsSync(cached.path)) {
10266
+ return _gitBashPath = cached.path;
10267
+ }
10268
+ } catch {
9547
10269
  }
9548
- const outFile = path.join(os.tmpdir(), `card-inference-out-${Date.now()}.json`);
9549
- const errFile = path.join(os.tmpdir(), `card-inference-err-${Date.now()}.txt`);
9550
- const adapterParts = splitCommandLine(inferenceAdapter);
9551
- if (adapterParts.length === 0) {
9552
- spawnInferenceDoneError("inference adapter command is empty");
9553
- return;
10270
+ const candidates = [
10271
+ process.env.SHELL,
10272
+ process.env.PROGRAMFILES && path7.join(process.env.PROGRAMFILES, "Git", "usr", "bin", "bash.exe"),
10273
+ process.env.PROGRAMFILES && path7.join(process.env.PROGRAMFILES, "Git", "bin", "bash.exe"),
10274
+ process.env["PROGRAMFILES(X86)"] && path7.join(process.env["PROGRAMFILES(X86)"], "Git", "bin", "bash.exe"),
10275
+ process.env.LOCALAPPDATA && path7.join(process.env.LOCALAPPDATA, "Programs", "Git", "bin", "bash.exe")
10276
+ ];
10277
+ for (const c of candidates) {
10278
+ if (c && /bash(\.exe)?$/i.test(c) && fs7.existsSync(c)) {
10279
+ _gitBashPath = c;
10280
+ try {
10281
+ fs7.writeFileSync(GIT_BASH_CACHE_FILE2, JSON.stringify({ path: c }));
10282
+ } catch {
10283
+ }
10284
+ return _gitBashPath;
10285
+ }
9554
10286
  }
9555
- const adapterRawCmd = adapterParts[0];
9556
- const adapterRawArgs = adapterParts.slice(1);
9557
- const { cmd: adapterCmd, args: adapterArgsPrefix } = resolveCommandInvocation(adapterRawCmd, adapterRawArgs);
9558
- const adapterArgs = [...adapterArgsPrefix, "run-inference", "--in", inFile, "--out", outFile, "--err", errFile];
10287
+ _gitBashPath = false;
9559
10288
  try {
9560
- execCommandSync(adapterCmd, adapterArgs, {
9561
- shell: false,
9562
- timeout: 12e4,
9563
- cwd: boardDir,
9564
- env: {
9565
- ...process.env,
9566
- BOARD_DIR: boardDir
9567
- }
9568
- });
9569
- } catch (err) {
9570
- const reason = err.message ?? String(err);
9571
- spawnInferenceDoneError(reason);
9572
- return;
10289
+ fs7.writeFileSync(GIT_BASH_CACHE_FILE2, JSON.stringify({ path: false }));
10290
+ } catch {
9573
10291
  }
9574
- if (!fs.existsSync(outFile)) {
9575
- const errMsg = fs.existsSync(errFile) ? fs.readFileSync(errFile, "utf-8").trim() : "inference adapter produced no output file";
9576
- spawnInferenceDoneError(errMsg);
10292
+ return _gitBashPath;
10293
+ }
10294
+ function shellQuote2(s) {
10295
+ return "'" + s.replace(/'/g, "'\\''") + "'";
10296
+ }
10297
+ function spawnDetachedCommand(cmd, args) {
10298
+ if (process.platform === "win32") {
10299
+ const bash = findGitBash2();
10300
+ if (bash) {
10301
+ const shellCmd = [cmd, ...args].map((a) => shellQuote2(a.replace(/\\/g, "/"))).join(" ");
10302
+ const child3 = spawn(bash, ["-c", shellCmd], { detached: true, stdio: "ignore", windowsHide: true });
10303
+ child3.unref();
10304
+ return;
10305
+ }
10306
+ const child2 = spawn("cmd", ["/c", "start", "/b", "", cmd, ...args], {
10307
+ detached: true,
10308
+ stdio: "ignore",
10309
+ windowsHide: true
10310
+ });
10311
+ child2.unref();
9577
10312
  return;
9578
10313
  }
9579
- spawnInferenceDone(outFile);
10314
+ const child = spawn(cmd, args, { detached: true, stdio: "ignore" });
10315
+ child.unref();
9580
10316
  }
9581
- function cmdInferenceDone(args) {
9582
- const tmpIdx = args.indexOf("--tmp");
9583
- const tokenIdx = args.indexOf("--token");
9584
- const tmpFile = tmpIdx !== -1 ? args[tmpIdx + 1] : void 0;
9585
- const inferenceToken = tokenIdx !== -1 ? args[tokenIdx + 1] : void 0;
9586
- if (!tmpFile || !inferenceToken) {
9587
- console.error("Usage: board-live-cards inference-done --tmp <result.json> --token <inference-token>");
9588
- process.exit(1);
9589
- }
9590
- const decodedToken = decodeSourceToken(inferenceToken);
9591
- if (!decodedToken) {
9592
- console.error("Invalid inference token");
9593
- process.exit(1);
9594
- }
9595
- const { cbk: callbackToken, rg: dir, cs: inputChecksum } = decodedToken;
9596
- const decoded = decodeCallbackToken2(callbackToken);
9597
- if (!decoded) {
9598
- console.error("Invalid callback token embedded in inference token");
9599
- process.exit(1);
9600
- }
9601
- const taskName = decoded.taskName;
9602
- const cardPath = lookupCardPath(dir, taskName);
9603
- if (!cardPath) {
9604
- console.error(`Card file for task "${taskName}" not found in inventory`);
9605
- process.exit(1);
9606
- }
9607
- let result = {};
9608
- if (fs.existsSync(tmpFile)) {
9609
- try {
9610
- result = JSON.parse(fs.readFileSync(tmpFile, "utf-8").trim());
9611
- } catch (err) {
9612
- result = { isTaskCompleted: false, reason: `failed to parse inference result: ${err instanceof Error ? err.message : String(err)}` };
10317
+ function execCommandSync(cmd, args, options) {
10318
+ const output = execFileSync(cmd, args, {
10319
+ shell: shouldUseShellForCommand(cmd, options?.shell),
10320
+ timeout: options?.timeout,
10321
+ encoding: options?.encoding,
10322
+ cwd: options?.cwd,
10323
+ windowsHide: true,
10324
+ env: options?.env
10325
+ });
10326
+ return typeof output === "string" ? output : output.toString("utf-8");
10327
+ }
10328
+ function execCommandAsync(cmd, args, callback) {
10329
+ execFile(
10330
+ cmd,
10331
+ args,
10332
+ { shell: shouldUseShellForCommand(cmd), encoding: "utf8", windowsHide: true },
10333
+ (err, stdout, stderr) => callback(err ?? null, stdout, stderr)
10334
+ );
10335
+ }
10336
+ function splitCommandLine(command) {
10337
+ const tokens = [];
10338
+ let current = "";
10339
+ let quote = null;
10340
+ for (const ch of command.trim()) {
10341
+ if (quote) {
10342
+ if (ch === quote) {
10343
+ quote = null;
10344
+ } else {
10345
+ current += ch;
10346
+ }
10347
+ continue;
9613
10348
  }
9614
- try {
9615
- fs.unlinkSync(tmpFile);
9616
- } catch {
10349
+ if (ch === '"' || ch === "'") {
10350
+ quote = ch;
10351
+ continue;
9617
10352
  }
9618
- } else {
9619
- result = { isTaskCompleted: false, reason: `inference result file not found: ${tmpFile}` };
9620
- }
9621
- const isTaskCompletedFlag = result.isTaskCompleted === true;
9622
- const inferenceCompletedAt = (/* @__PURE__ */ new Date()).toISOString();
9623
- const card = JSON.parse(fs.readFileSync(cardPath, "utf-8"));
9624
- if (!card.card_data) card.card_data = {};
9625
- const cardData = card.card_data;
9626
- const existingInference = cardData.llm_task_completion_inference && typeof cardData.llm_task_completion_inference === "object" ? cardData.llm_task_completion_inference : {};
9627
- cardData.llm_task_completion_inference = {
9628
- ...existingInference,
9629
- isTaskCompleted: isTaskCompletedFlag,
9630
- reason: typeof result.reason === "string" ? result.reason : "",
9631
- evidence: typeof result.evidence === "string" ? result.evidence : "",
9632
- inferenceCompletedAt
9633
- };
9634
- fs.writeFileSync(cardPath, JSON.stringify(card, null, 2), "utf-8");
9635
- const runtimePath2 = path.join(dir, taskName, "runtime.json");
9636
- let runtime = { _sources: {} };
9637
- if (fs.existsSync(runtimePath2)) {
9638
- try {
9639
- runtime = JSON.parse(fs.readFileSync(runtimePath2, "utf-8"));
9640
- } catch {
10353
+ if (/\s/.test(ch)) {
10354
+ if (current) {
10355
+ tokens.push(current);
10356
+ current = "";
10357
+ }
10358
+ continue;
9641
10359
  }
10360
+ current += ch;
9642
10361
  }
9643
- const inferenceEntry = runtime._inferenceEntry ?? {};
9644
- runtime._inferenceEntry = nextEntryAfterFetchDelivery(inferenceEntry, inferenceCompletedAt);
9645
- fs.mkdirSync(path.dirname(runtimePath2), { recursive: true });
9646
- fs.writeFileSync(runtimePath2, JSON.stringify(runtime, null, 2), "utf-8");
9647
- appendEventToJournal(dir, {
9648
- type: "task-progress",
9649
- taskName,
9650
- update: {
9651
- kind: "inference-done",
9652
- isTaskCompleted: isTaskCompletedFlag,
9653
- inputChecksum
9654
- },
9655
- timestamp: inferenceCompletedAt
9656
- });
9657
- void processAccumulatedEventsInfinitePass(dir);
10362
+ if (quote) {
10363
+ throw new Error(`Unterminated quote in command: ${command}`);
10364
+ }
10365
+ if (current) tokens.push(current);
10366
+ return tokens;
9658
10367
  }
9659
- function cmdRunSourceFetch(args) {
9660
- const inIdx = args.indexOf("--in");
9661
- const outIdx = args.indexOf("--out");
9662
- const errIdx = args.indexOf("--err");
9663
- const inFile = inIdx !== -1 ? args[inIdx + 1] : void 0;
9664
- const outFile = outIdx !== -1 ? args[outIdx + 1] : void 0;
9665
- const errFile = errIdx !== -1 ? args[errIdx + 1] : void 0;
9666
- if (!inFile || !outFile) {
9667
- console.error("Usage: board-live-cards run-source-fetch --in <source.json> --out <result.json> [--err <error.txt>]");
9668
- process.exit(1);
10368
+ function resolveCommandInvocation(rawCmd, rawArgs) {
10369
+ if (/^(node|node\.exe)$/i.test(rawCmd)) {
10370
+ return { cmd: process.execPath, args: rawArgs };
9669
10371
  }
9670
- if (!fs.existsSync(inFile)) {
9671
- const msg = `Input file not found: ${inFile}`;
9672
- if (errFile) fs.writeFileSync(errFile, msg);
9673
- console.error(`[run-source-fetch] ${msg}`);
9674
- process.exit(1);
10372
+ if (/\.m?js$/i.test(rawCmd)) {
10373
+ return { cmd: process.execPath, args: [rawCmd, ...rawArgs] };
9675
10374
  }
9676
- let source;
10375
+ return { cmd: rawCmd, args: rawArgs };
10376
+ }
10377
+ function spawnDetachedProcessAccumulatedWorker(boardDir) {
10378
+ const { cmd, args: cliArgs } = getCliInvocation("process-accumulated-events", ["--rg", boardDir, "--inline-loop"]);
9677
10379
  try {
9678
- const raw = fs.readFileSync(inFile, "utf-8");
9679
- source = JSON.parse(raw);
9680
- } catch (err) {
9681
- const msg = `Failed to parse input file: ${err.message}`;
9682
- if (errFile) fs.writeFileSync(errFile, msg);
9683
- console.error(`[run-source-fetch] ${msg}`);
9684
- process.exit(1);
9685
- }
9686
- if (!source.cli) {
9687
- const msg = "Source definition missing cli field (board-live-cards built-in executor only understands source.cli)";
9688
- if (errFile) fs.writeFileSync(errFile, msg);
9689
- console.error(`[run-source-fetch] ${msg}`);
9690
- process.exit(1);
10380
+ spawnDetachedCommand(cmd, cliArgs);
10381
+ return true;
10382
+ } catch {
10383
+ return false;
9691
10384
  }
9692
- console.log(`[run-source-fetch] executing: ${source.cli}`);
9693
- const timeout = source.timeout ?? 12e4;
9694
- const sourceCwd = typeof source.cwd === "string" ? source.cwd : process.cwd();
9695
- const sourceBoardDir = typeof source.boardDir === "string" ? source.boardDir : void 0;
9696
- const cmdParts = splitCommandLine(source.cli);
9697
- if (cmdParts.length === 0) {
9698
- const msg = "Source cli command is empty";
9699
- if (errFile) fs.writeFileSync(errFile, msg);
9700
- console.error(`[run-source-fetch] ${msg}`);
9701
- process.exit(1);
10385
+ }
10386
+ async function processAccumulatedEventsInlineLoop(boardDir, settleDelayMs = 50) {
10387
+ while (determineLatestPendingAccumulated(boardDir) > 0) {
10388
+ const ran = await processAccumulatedEvents(boardDir);
10389
+ if (!ran) return false;
10390
+ await new Promise((resolve6) => setTimeout(resolve6, settleDelayMs));
9702
10391
  }
9703
- const rawCmd = cmdParts[0];
9704
- const { cmd, args: cliArgs } = resolveCommandInvocation(rawCmd, cmdParts.slice(1));
9705
- let stdout;
10392
+ return true;
10393
+ }
10394
+ function shouldAvoidDetachedProcessSpawn() {
10395
+ return process.env.BOARD_LIVE_CARDS_NO_SPAWN === "1";
10396
+ }
10397
+ async function processAccumulatedEvents(boardDir) {
10398
+ const boardPath = path7.join(boardDir, BOARD_FILE);
10399
+ const cliDir = path7.resolve(__dirname$1, "..", "..");
10400
+ let release;
9706
10401
  try {
9707
- stdout = execCommandSync(cmd, cliArgs, {
9708
- shell: false,
9709
- encoding: "utf-8",
9710
- timeout,
9711
- cwd: sourceCwd,
9712
- env: {
9713
- ...process.env,
9714
- ...sourceBoardDir ? { BOARD_DIR: sourceBoardDir } : {}
9715
- }
9716
- });
9717
- } catch (err) {
9718
- const msg = err.message ?? String(err);
9719
- console.error(`[run-source-fetch] cli failed: ${msg}`);
9720
- if (errFile) fs.writeFileSync(errFile, msg);
9721
- process.exit(1);
10402
+ release = lockSync(boardPath, { retries: 0 });
10403
+ } catch {
10404
+ return false;
9722
10405
  }
9723
- const result = stdout.trim();
9724
10406
  try {
9725
- fs.writeFileSync(outFile, result);
9726
- console.log(`[run-source-fetch] result written to ${outFile}`);
9727
- } catch (err) {
9728
- const msg = `Failed to write output file: ${err.message}`;
9729
- console.error(`[run-source-fetch] ${msg}`);
9730
- if (errFile) fs.writeFileSync(errFile, msg);
9731
- process.exit(1);
10407
+ const cardHandlerAdapters = {
10408
+ cardStore: createNodeCardStore(lookupCardPath),
10409
+ runtimeStore: createNodeRuntimeStore(),
10410
+ outputStore: createNodeOutputStore(resolveComputedValuesPath, resolveDataObjectsDirPath, INFERENCE_ADAPTER_LOG_FILE),
10411
+ inputStore: createNodeInputStore(JOURNAL_FILE),
10412
+ invocationAdapter: createNodeInvocationAdapter(cliDir, encodeSourceToken)
10413
+ };
10414
+ const envelope = loadBoardEnvelope(boardDir);
10415
+ const live = restore(envelope.graph);
10416
+ const journal = new BoardJournal(path7.join(boardDir, JOURNAL_FILE), envelope.lastDrainedJournalId);
10417
+ const rg = createReactiveGraph(live, { handlers: { "card-handler": createCardHandlerFn(boardDir, cardHandlerAdapters) } });
10418
+ const undrained = journal.drain();
10419
+ rg.pushAll(undrained);
10420
+ await rg.dispose({ wait: true });
10421
+ saveBoard(boardDir, rg, journal);
10422
+ return true;
10423
+ } finally {
10424
+ release();
9732
10425
  }
9733
10426
  }
9734
- function cmdUpsertCard(args) {
9735
- const rgIdx = args.indexOf("--rg");
9736
- const cardIdx = args.indexOf("--card");
9737
- const globIdx = args.indexOf("--card-glob");
9738
- const cardIdIdx = args.indexOf("--card-id");
9739
- const restart = args.includes("--restart");
9740
- const dir = rgIdx !== -1 ? args[rgIdx + 1] : void 0;
9741
- const cardFile = cardIdx !== -1 ? args[cardIdx + 1] : void 0;
9742
- const cardGlob = globIdx !== -1 ? args[globIdx + 1] : void 0;
9743
- const requestedCardId = cardIdIdx !== -1 ? args[cardIdIdx + 1] : void 0;
9744
- if (!dir || !cardFile && !cardGlob || cardFile && cardGlob) {
9745
- console.error("Usage: board-live-cards upsert-card --rg <dir> (--card <card.json> | --card-glob <glob>) [--card-id <card-id>] [--restart]");
9746
- process.exit(1);
9747
- }
9748
- if (cardGlob && requestedCardId) {
9749
- console.error("Usage: --card-id may be used only with --card (single file), not with --card-glob");
9750
- process.exit(1);
9751
- }
9752
- const cardFiles = cardFile ? [path.resolve(cardFile)] : resolveCardGlobMatches(cardGlob);
9753
- if (!cardFile && cardFiles.length === 0) {
9754
- console.error(`No card files matched glob: ${cardGlob}`);
9755
- process.exit(1);
9756
- }
9757
- const idx = buildCardInventoryIndex(dir);
9758
- const batchByCardId = /* @__PURE__ */ new Map();
9759
- const batchByCardPath = /* @__PURE__ */ new Map();
9760
- const plans = [];
9761
- const logs = [];
9762
- for (const absCardPath of cardFiles) {
9763
- if (!fs.existsSync(absCardPath)) {
9764
- console.error(`Card file not found: ${absCardPath}`);
9765
- process.exit(1);
9766
- }
9767
- const card = JSON.parse(fs.readFileSync(absCardPath, "utf-8"));
9768
- if (!card.id) {
9769
- console.error(`Card JSON must have an "id" field (${absCardPath})`);
9770
- process.exit(1);
9771
- }
9772
- if (requestedCardId && requestedCardId !== card.id) {
9773
- console.error(
9774
- `Card id mismatch: --card-id "${requestedCardId}" does not match file id "${card.id}" (${absCardPath})`
9775
- );
9776
- process.exit(1);
9777
- }
9778
- const seenPathCardId = batchByCardPath.get(absCardPath);
9779
- if (seenPathCardId && seenPathCardId !== card.id) {
9780
- console.error(
9781
- `Upsert rejected: file "${absCardPath}" appears multiple times in batch with conflicting ids ("${seenPathCardId}" vs "${card.id}")`
9782
- );
9783
- process.exit(1);
9784
- }
9785
- const seenCardPath = batchByCardId.get(card.id);
9786
- if (seenCardPath && seenCardPath !== absCardPath) {
9787
- console.error(
9788
- `Upsert rejected: card id "${card.id}" appears multiple times in batch with conflicting files ("${seenCardPath}" vs "${absCardPath}")`
9789
- );
9790
- process.exit(1);
9791
- }
9792
- const existingById = idx.byCardId.get(card.id);
9793
- const existingByPath = idx.byCardPath.get(absCardPath);
9794
- if (existingByPath && existingByPath.cardId !== card.id) {
9795
- console.error(
9796
- `Upsert rejected: file "${absCardPath}" is already mapped to card id "${existingByPath.cardId}", cannot remap to "${card.id}"`
9797
- );
9798
- process.exit(1);
9799
- }
9800
- if (existingById && existingById.cardFilePath !== absCardPath) {
9801
- console.error(
9802
- `Upsert rejected: card id "${card.id}" is already mapped to file "${existingById.cardFilePath}", cannot remap to "${absCardPath}"`
9803
- );
9804
- process.exit(1);
9805
- }
9806
- batchByCardPath.set(absCardPath, card.id);
9807
- batchByCardId.set(card.id, absCardPath);
9808
- plans.push({
9809
- card,
9810
- absCardPath,
9811
- isInsert: !existingById
9812
- });
10427
+ async function processAccumulatedEventsInfinitePass(boardDir, settleDelayMs = 50, options) {
10428
+ if (options?.inlineLoop || shouldAvoidDetachedProcessSpawn()) {
10429
+ return processAccumulatedEventsInlineLoop(boardDir, settleDelayMs);
9813
10430
  }
9814
- for (const plan of plans) {
9815
- const { card, absCardPath, isInsert } = plan;
9816
- if (isInsert) {
9817
- const newEntry = {
9818
- cardId: card.id,
9819
- cardFilePath: absCardPath,
9820
- addedAt: (/* @__PURE__ */ new Date()).toISOString()
9821
- };
9822
- appendCardInventory(dir, newEntry);
9823
- idx.byCardId.set(card.id, newEntry);
9824
- idx.byCardPath.set(absCardPath, newEntry);
9825
- }
9826
- const taskConfig = liveCardToTaskConfig(card);
9827
- appendEventToJournal(dir, {
9828
- type: "task-upsert",
9829
- taskName: card.id,
9830
- taskConfig,
9831
- timestamp: (/* @__PURE__ */ new Date()).toISOString()
9832
- });
9833
- if (restart) {
9834
- appendEventToJournal(dir, {
9835
- type: "task-restart",
9836
- taskName: card.id,
9837
- timestamp: (/* @__PURE__ */ new Date()).toISOString()
9838
- });
9839
- }
9840
- logs.push(`Card "${card.id}" ${isInsert ? "upserted (inserted)" : "upserted (updated)"}${restart ? " (restarted)" : ""}.`);
10431
+ return spawnDetachedProcessAccumulatedWorker(boardDir);
10432
+ }
10433
+ async function processAccumulatedEventsForced(boardDir, options) {
10434
+ await processAccumulatedEvents(boardDir);
10435
+ await processAccumulatedEventsInfinitePass(boardDir, 50, options);
10436
+ }
10437
+ function liveCardToTaskConfig(card) {
10438
+ const requires = card.requires;
10439
+ const provides = card.provides?.map((p) => p.bindTo) ?? [];
10440
+ return {
10441
+ requires: requires && requires.length > 0 ? requires : void 0,
10442
+ provides,
10443
+ taskHandlers: ["card-handler"],
10444
+ description: card.meta?.title ?? card.id
10445
+ };
10446
+ }
10447
+ var __dirname$1 = path7.dirname(fileURLToPath(import.meta.url));
10448
+ var REPO_ROOT = path7.resolve(__dirname$1, "..", "..");
10449
+ var LOCAL_TSX_CLI = path7.join(REPO_ROOT, "node_modules", "tsx", "dist", "cli.mjs");
10450
+ function getCliInvocation(command, args) {
10451
+ const jsPath = path7.join(__dirname$1, "board-live-cards-cli.js");
10452
+ if (fs7.existsSync(jsPath)) {
10453
+ return { cmd: process.execPath, args: [jsPath, command, ...args] };
9841
10454
  }
9842
- void processAccumulatedEventsInfinitePass(dir);
9843
- if (cardGlob) {
9844
- console.log(`Upserted ${cardFiles.length} cards from glob: ${cardGlob}${restart ? " (restarted)" : ""}`);
9845
- } else {
9846
- console.log(logs[0]);
10455
+ const tsPath = path7.join(__dirname$1, "board-live-cards-cli.ts");
10456
+ if (fs7.existsSync(tsPath) && fs7.existsSync(LOCAL_TSX_CLI)) {
10457
+ return { cmd: process.execPath, args: [LOCAL_TSX_CLI, tsPath, command, ...args] };
9847
10458
  }
10459
+ const npxCmd = process.platform === "win32" ? "npx.cmd" : "npx";
10460
+ return { cmd: npxCmd, args: ["tsx", tsPath, command, ...args] };
9848
10461
  }
9849
- async function cmdTryDrain(args) {
9850
- const rgIdx = args.indexOf("--rg");
9851
- const inlineLoop = args.includes("--inline-loop");
9852
- const boardDir = rgIdx !== -1 ? args[rgIdx + 1] : void 0;
9853
- if (!boardDir) {
9854
- console.error("Usage: board-live-cards process-accumulated-events --rg <dir>");
9855
- process.exit(1);
10462
+ function appendTaskExecutorLog(boardDir, hydratedSource, mode) {
10463
+ try {
10464
+ const entry = {
10465
+ timestamp: (/* @__PURE__ */ new Date()).toISOString(),
10466
+ mode,
10467
+ hydratedSource
10468
+ };
10469
+ fs7.appendFileSync(path7.join(boardDir, TASK_EXECUTOR_LOG_FILE), JSON.stringify(entry) + "\n", "utf-8");
10470
+ } catch (logErr) {
10471
+ console.error(`[task-executor-log] append failed: ${logErr instanceof Error ? logErr.message : String(logErr)}`);
9856
10472
  }
9857
- await processAccumulatedEventsForced(boardDir, { inlineLoop });
9858
10473
  }
9859
- function cmdRetrigger(args) {
9860
- const rgIdx = args.indexOf("--rg");
9861
- const taskIdx = args.indexOf("--task");
9862
- const dir = rgIdx !== -1 ? args[rgIdx + 1] : void 0;
9863
- const taskName = taskIdx !== -1 ? args[taskIdx + 1] : void 0;
9864
- if (!dir || !taskName) {
9865
- console.error("Usage: board-live-cards retrigger --rg <dir> --task <task-name>");
9866
- process.exit(1);
9867
- }
9868
- appendEventToJournal(dir, {
9869
- type: "task-restart",
9870
- taskName,
9871
- timestamp: (/* @__PURE__ */ new Date()).toISOString()
10474
+ function resolveCardGlobMatches(cardGlob) {
10475
+ const patterns = cardGlob.split(",").map((s) => s.trim()).filter(Boolean).map((p) => p.replace(/\\/g, "/"));
10476
+ const matches = fg.sync(patterns, {
10477
+ absolute: true,
10478
+ onlyFiles: true,
10479
+ unique: true,
10480
+ dot: false
9872
10481
  });
9873
- void processAccumulatedEventsInfinitePass(dir);
9874
- console.log(`Task "${taskName}" retriggered.`);
10482
+ return [...matches].map((m) => path7.resolve(m)).sort((a, b) => a.localeCompare(b));
9875
10483
  }
9876
10484
  async function cli(argv) {
10485
+ const boardCommandHandlers = createBoardCommandHandlers({
10486
+ initBoard,
10487
+ configureRuntimeOutDir,
10488
+ loadBoard,
10489
+ writeJsonAtomic: writeJsonAtomic2,
10490
+ resolveStatusSnapshotPath,
10491
+ buildBoardStatusObject: (dir, live) => buildBoardStatusObject(path7.resolve(dir), live),
10492
+ readTaskExecutorConfig,
10493
+ resolveCardGlobMatches,
10494
+ validateLiveCardDefinition,
10495
+ execCommandSync,
10496
+ appendEventToJournal,
10497
+ processAccumulatedEventsInfinitePass
10498
+ });
10499
+ const callbackCommandHandlers = createCallbackCommandHandlers({
10500
+ decodeCallbackToken: decodeCallbackToken2,
10501
+ decodeSourceToken,
10502
+ writeRuntimeDataObjects,
10503
+ appendEventToJournal,
10504
+ processAccumulatedEventsForced,
10505
+ processAccumulatedEventsInfinitePass
10506
+ });
10507
+ const nonCoreCommandHandlers = createNonCoreCommandHandlers({
10508
+ readTaskExecutorConfig,
10509
+ execCommandSync,
10510
+ splitCommandLine,
10511
+ resolveCommandInvocation
10512
+ });
10513
+ const cardCommandHandlers = createCardCommandHandlers({
10514
+ resolveCardGlobMatches,
10515
+ buildCardInventoryIndex,
10516
+ appendCardInventory,
10517
+ liveCardToTaskConfig,
10518
+ appendEventToJournal,
10519
+ processAccumulatedEventsInfinitePass
10520
+ });
10521
+ const executionCommandHandlers = createExecutionCommandHandlers({
10522
+ INFERENCE_ADAPTER_FILE,
10523
+ readTaskExecutorConfig,
10524
+ execCommandSync,
10525
+ execCommandAsync,
10526
+ splitCommandLine,
10527
+ resolveCommandInvocation,
10528
+ encodeSourceToken,
10529
+ decodeSourceToken,
10530
+ decodeCallbackToken: decodeCallbackToken2,
10531
+ spawnDetachedCommand,
10532
+ getCliInvocation,
10533
+ appendTaskExecutorLog,
10534
+ appendEventToJournal,
10535
+ processAccumulatedEventsInfinitePass,
10536
+ processAccumulatedEventsForced,
10537
+ lookupCardPath,
10538
+ nextEntryAfterFetchDelivery
10539
+ });
9877
10540
  const cmd = argv[0];
9878
10541
  const rest = argv.slice(1);
9879
10542
  switch (cmd) {
9880
10543
  case "help":
9881
10544
  case "--help":
9882
10545
  case "-h":
9883
- return cmdHelp();
10546
+ return nonCoreCommandHandlers.cmdHelp();
9884
10547
  case "init":
9885
- return cmdInit(rest);
10548
+ return boardCommandHandlers.cmdInit(rest);
9886
10549
  case "status":
9887
- return cmdStatus(rest);
10550
+ return boardCommandHandlers.cmdStatus(rest);
9888
10551
  case "upsert-card":
9889
- return cmdUpsertCard(rest);
10552
+ return cardCommandHandlers.cmdUpsertCard(rest);
9890
10553
  case "validate-card":
9891
- return cmdValidateCard(rest);
10554
+ return boardCommandHandlers.cmdValidateCard(rest);
9892
10555
  case "remove-card":
9893
- return cmdRemoveCard(rest);
10556
+ return boardCommandHandlers.cmdRemoveCard(rest);
9894
10557
  case "retrigger":
9895
- return cmdRetrigger(rest);
10558
+ return boardCommandHandlers.cmdRetrigger(rest);
9896
10559
  case "task-completed":
9897
- return cmdTaskCompleted(rest);
10560
+ return callbackCommandHandlers.cmdTaskCompleted(rest);
9898
10561
  case "task-failed":
9899
- return cmdTaskFailed(rest);
10562
+ return callbackCommandHandlers.cmdTaskFailed(rest);
9900
10563
  case "task-progress":
9901
- return cmdTaskProgress(rest);
10564
+ return callbackCommandHandlers.cmdTaskProgress(rest);
9902
10565
  case "source-data-fetched":
9903
- return cmdSourceDataFetched(rest);
10566
+ return callbackCommandHandlers.cmdSourceDataFetched(rest);
9904
10567
  case "source-data-fetch-failure":
9905
- return cmdSourceDataFetchFailure(rest);
10568
+ return callbackCommandHandlers.cmdSourceDataFetchFailure(rest);
9906
10569
  case "run-sourcedefs-internal":
9907
- return cmdRunSources(rest);
10570
+ return executionCommandHandlers.cmdRunSources(rest);
9908
10571
  case "run-inference-internal":
9909
- return cmdRunInference(rest);
10572
+ return executionCommandHandlers.cmdRunInference(rest);
9910
10573
  case "inference-done":
9911
- return cmdInferenceDone(rest);
10574
+ return executionCommandHandlers.cmdInferenceDone(rest);
9912
10575
  case "run-source-fetch":
9913
- return cmdRunSourceFetch(rest);
10576
+ return nonCoreCommandHandlers.cmdRunSourceFetch(rest);
9914
10577
  case "probe-source":
9915
- return await cmdProbeSource(rest);
10578
+ return await nonCoreCommandHandlers.cmdProbeSource(rest);
9916
10579
  case "describe-task-executor-capabilities":
9917
- return cmdDescribeTaskExecutorCapabilities(rest);
10580
+ return nonCoreCommandHandlers.cmdDescribeTaskExecutorCapabilities(rest);
9918
10581
  case "process-accumulated-events":
9919
- return await cmdTryDrain(rest);
10582
+ return await executionCommandHandlers.cmdTryDrain(rest);
9920
10583
  default:
9921
10584
  throw new Error(`Unknown command: ${cmd ?? "(none)"}`);
9922
10585
  }
9923
10586
  }
9924
- async function cmdProbeSource(args) {
9925
- const cardIdx = args.indexOf("--card");
9926
- const sourceIdxArg = args.indexOf("--source-idx");
9927
- const sourceBindArg = args.indexOf("--source-bind");
9928
- const mockProjectionsIdx = args.indexOf("--mock-projections");
9929
- const rgIdx = args.indexOf("--rg");
9930
- const outIdx = args.indexOf("--out");
9931
- const cardFilePath = cardIdx !== -1 ? args[cardIdx + 1] : void 0;
9932
- const sourceIdxVal = sourceIdxArg !== -1 ? parseInt(args[sourceIdxArg + 1], 10) : 0;
9933
- const sourceBindVal = sourceBindArg !== -1 ? args[sourceBindArg + 1] : void 0;
9934
- const mockProjectionsRaw = mockProjectionsIdx !== -1 ? args[mockProjectionsIdx + 1] : void 0;
9935
- const boardDirArg = rgIdx !== -1 ? args[rgIdx + 1] : void 0;
9936
- const outFile = outIdx !== -1 ? args[outIdx + 1] : void 0;
9937
- if (!cardFilePath) {
9938
- console.error("Usage: board-live-cards probe-source --card <card.json> [--source-idx <n>] [--source-bind <name>] [--mock-projections <json>] [--rg <boardDir>] [--out <result.json>]");
9939
- process.exit(1);
9940
- }
9941
- let card;
9942
- try {
9943
- card = JSON.parse(fs.readFileSync(path.resolve(cardFilePath), "utf-8"));
9944
- } catch (e) {
9945
- console.error(`[probe-source] Cannot read card: ${e.message}`);
9946
- process.exit(1);
9947
- }
9948
- const source_defs = card.source_defs ?? [];
9949
- if (source_defs.length === 0) {
9950
- console.error(`[probe-source] Card "${card.id}" has no source_defs`);
9951
- process.exit(1);
9952
- }
9953
- let sourceIdx;
9954
- if (sourceBindVal) {
9955
- sourceIdx = source_defs.findIndex((s) => s.bindTo === sourceBindVal);
9956
- if (sourceIdx === -1) {
9957
- console.error(`[probe-source] No source with bindTo="${sourceBindVal}" in card "${card.id}"`);
9958
- process.exit(1);
9959
- }
9960
- } else {
9961
- sourceIdx = sourceIdxVal;
9962
- if (isNaN(sourceIdx) || sourceIdx < 0 || sourceIdx >= source_defs.length) {
9963
- console.error(`[probe-source] --source-idx ${sourceIdxVal} out of range (card has ${source_defs.length} source(s))`);
9964
- process.exit(1);
9965
- }
9966
- }
9967
- const sourceDef = source_defs[sourceIdx];
9968
- const cardDir = path.resolve(path.dirname(cardFilePath));
9969
- const boardDir = boardDirArg ? path.resolve(boardDirArg) : cardDir;
9970
- let mockProjections = {};
9971
- if (mockProjectionsRaw) {
9972
- const raw = mockProjectionsRaw.startsWith("@") ? fs.readFileSync(path.resolve(mockProjectionsRaw.slice(1)), "utf-8") : mockProjectionsRaw;
9973
- try {
9974
- mockProjections = JSON.parse(raw);
9975
- } catch (e) {
9976
- console.error(`[probe-source] --mock-projections is not valid JSON: ${e.message}`);
9977
- process.exit(1);
9978
- }
9979
- }
9980
- const teConfig = readTaskExecutorConfig(boardDir);
9981
- const taskExecutor = teConfig?.command;
9982
- const taskExecutorExtraB64 = teConfig?.extra ? Buffer.from(JSON.stringify(teConfig.extra)).toString("base64") : void 0;
9983
- const inPayload = {
9984
- ...sourceDef,
9985
- cwd: typeof sourceDef.cwd === "string" && sourceDef.cwd ? sourceDef.cwd : cardDir,
9986
- boardDir: typeof sourceDef.boardDir === "string" && sourceDef.boardDir ? sourceDef.boardDir : boardDir,
9987
- _projections: mockProjections
9988
- };
9989
- let sourceKind = "unknown";
9990
- if (taskExecutor) {
9991
- try {
9992
- const capRaw = execCommandSync(taskExecutor, ["describe-capabilities"], {
9993
- shell: true,
9994
- timeout: 8e3,
9995
- encoding: "utf-8"
9996
- });
9997
- const caps = JSON.parse(String(capRaw));
9998
- const knownKinds = caps?.sourceKinds ? Object.keys(caps.sourceKinds) : [];
9999
- const defKeys = new Set(Object.keys(sourceDef));
10000
- sourceKind = knownKinds.find((k) => defKeys.has(k)) ?? "unknown";
10001
- } catch {
10002
- }
10003
- }
10004
- console.log(`[probe-source] card: ${card.id}`);
10005
- console.log(`[probe-source] source[${sourceIdx}]: bindTo="${sourceDef.bindTo}" kind=${sourceKind}`);
10006
- console.log(`[probe-source] _projections: ${JSON.stringify(mockProjections)}`);
10007
- console.log(`[probe-source] executor: ${taskExecutor ?? "built-in (source.cli only)"}`);
10008
- console.log(`[probe-source] running fetch...`);
10009
- const ts = Date.now();
10010
- const inFile = path.join(os.tmpdir(), `probe-in-${sourceDef.bindTo}-${ts}.json`);
10011
- const tmpOut = path.join(os.tmpdir(), `probe-out-${sourceDef.bindTo}-${ts}.json`);
10012
- const errFile = path.join(os.tmpdir(), `probe-err-${sourceDef.bindTo}-${ts}.txt`);
10013
- fs.writeFileSync(inFile, JSON.stringify(inPayload, null, 2), "utf-8");
10014
- let passed = false;
10015
- let errorMsg;
10016
- let resultRaw;
10017
- try {
10018
- if (taskExecutor) {
10019
- const executorArgs = ["run-source-fetch", "--in", inFile, "--out", tmpOut, "--err", errFile];
10020
- if (taskExecutorExtraB64) executorArgs.push("--extra", taskExecutorExtraB64);
10021
- execCommandSync(taskExecutor, executorArgs, {
10022
- shell: true,
10023
- timeout: sourceDef.timeout ?? 3e4
10024
- });
10025
- } else {
10026
- if (!inPayload.cli) {
10027
- throw new Error("No task-executor registered and source has no cli field \u2014 cannot probe with built-in executor");
10028
- }
10029
- const cmdParts = splitCommandLine(inPayload.cli);
10030
- const rawCmd = cmdParts[0];
10031
- const { cmd, args: cliArgs } = resolveCommandInvocation(rawCmd, cmdParts.slice(1));
10032
- const stdout = execCommandSync(cmd, cliArgs, {
10033
- shell: false,
10034
- encoding: "utf-8",
10035
- timeout: sourceDef.timeout ?? 3e4,
10036
- cwd: inPayload.cwd
10037
- });
10038
- fs.writeFileSync(tmpOut, stdout.trim(), "utf-8");
10039
- }
10040
- passed = fs.existsSync(tmpOut);
10041
- if (passed) {
10042
- resultRaw = fs.readFileSync(tmpOut, "utf-8");
10043
- } else {
10044
- errorMsg = fs.existsSync(errFile) ? fs.readFileSync(errFile, "utf-8").trim() : "executor produced no output file";
10045
- }
10046
- } catch (e) {
10047
- errorMsg = e.message ?? String(e);
10048
- if (!errorMsg && fs.existsSync(errFile)) {
10049
- errorMsg = fs.readFileSync(errFile, "utf-8").trim();
10050
- }
10051
- }
10052
- for (const f of [inFile, errFile]) {
10053
- try {
10054
- fs.unlinkSync(f);
10055
- } catch {
10056
- }
10057
- }
10058
- if (passed && resultRaw !== void 0) {
10059
- const resultSize = resultRaw.length;
10060
- const sample = resultRaw.slice(0, 300);
10061
- console.log(`[probe-source] STATUS: PROBE_PASS`);
10062
- console.log(`[probe-source] result size: ${resultSize} bytes`);
10063
- console.log(`[probe-source] sample: ${sample}${resultSize > 300 ? "..." : ""}`);
10064
- if (outFile) {
10065
- fs.writeFileSync(path.resolve(outFile), resultRaw);
10066
- console.log(`[probe-source] result written to: ${outFile}`);
10067
- } else {
10068
- try {
10069
- fs.unlinkSync(tmpOut);
10070
- } catch {
10071
- }
10072
- }
10073
- } else {
10074
- console.log(`[probe-source] STATUS: PROBE_FAIL`);
10075
- if (errorMsg) console.log(`[probe-source] error: ${errorMsg}`);
10076
- try {
10077
- if (fs.existsSync(tmpOut)) fs.unlinkSync(tmpOut);
10078
- } catch {
10079
- }
10080
- }
10081
- const summary = {
10082
- status: passed ? "PROBE_PASS" : "PROBE_FAIL",
10083
- cardId: card.id,
10084
- sourceIdx,
10085
- bindTo: sourceDef.bindTo,
10086
- sourceKind,
10087
- mockProjectionsKeys: Object.keys(mockProjections),
10088
- resultSizeBytes: resultRaw !== void 0 ? resultRaw.length : 0,
10089
- error: errorMsg ?? void 0
10090
- };
10091
- console.log(`[probe-source:result] ${JSON.stringify(summary)}`);
10092
- process.exit(passed ? 0 : 1);
10093
- }
10094
- function cmdDescribeTaskExecutorCapabilities(args) {
10095
- const rgIdx = args.indexOf("--rg");
10096
- const boardDir = rgIdx !== -1 ? path.resolve(args[rgIdx + 1]) : void 0;
10097
- if (!boardDir) {
10098
- console.error("Usage: board-live-cards describe-task-executor-capabilities --rg <dir>");
10099
- process.exit(1);
10100
- }
10101
- const teConfig = readTaskExecutorConfig(boardDir);
10102
- if (!teConfig) {
10103
- console.error(`[describe-task-executor-capabilities] No .task-executor registered in ${boardDir}`);
10104
- process.exit(1);
10105
- }
10106
- try {
10107
- const stdout = execCommandSync(teConfig.command, ["describe-capabilities"], {
10108
- shell: true,
10109
- timeout: 1e4,
10110
- encoding: "utf-8"
10111
- });
10112
- process.stdout.write(String(stdout));
10113
- if (!String(stdout).endsWith("\n")) process.stdout.write("\n");
10114
- } catch (e) {
10115
- console.error(`[describe-task-executor-capabilities] Executor failed: ${e.message ?? e}`);
10116
- process.exit(1);
10117
- }
10118
- }
10119
- function cmdHelp() {
10120
- console.log(`
10121
- board-live-cards-cli \u2014 LiveCards board CLI
10122
-
10123
- USAGE
10124
- board-live-cards-cli <command> [options]
10125
-
10126
- BOARD MANAGEMENT
10127
- init <dir> [--task-executor <script>] [--chat-handler <script>] [--inference-adapter <script>] [--runtime-out <dir>]
10128
- Create a new board in <dir>.
10129
- If --task-executor is given, writes <dir>/.task-executor with the script path.
10130
- If --chat-handler is given, writes <dir>/.chat-handler with the script path.
10131
- If --inference-adapter is given, writes <dir>/.inference-adapter with the script path.
10132
- Writes <dir>/.runtime-out (default: <dir>/runtime-out).
10133
- Published runtime files:
10134
- <runtime-out>/board-livegraph-status.json
10135
- <runtime-out>/cards/<card-id>.computed.json
10136
- Re-running init on an existing board is safe; handler registrations are updated.
10137
-
10138
- status --rg <dir> [--json]
10139
- Read and print the published status snapshot from <runtime-out>/board-livegraph-status.json.
10140
- --json emits the stable machine-readable status object.
10141
-
10142
- CARD MANAGEMENT
10143
- upsert-card --rg <dir> (--card <card.json> | --card-glob <glob>) [--card-id <card-id>] [--restart]
10144
- Insert or update one or many cards.
10145
- Enforces strict one-to-one mapping between card id and file path:
10146
- - same id + same file path: update
10147
- - new id + new file path: insert
10148
- - id remap or file remap: rejected
10149
- If --card-id is provided, it must match the id inside the file.
10150
- --card-id is valid only with --card (single file), not with --card-glob.
10151
- --restart clears the task so it re-triggers from scratch.
10152
-
10153
- validate-card (--card <card.json> | --card-glob <glob>) [--rg <boardDir>]
10154
- Validate one or many card JSON files without adding them to a board.
10155
- Checks JSON Schema structure, runtime expression syntax, and provides.ref namespaces.
10156
- When --rg is provided, also invokes the board's task executor validate-source-def
10157
- subcommand to structurally validate each source definition against supported kinds.
10158
- Exits with code 1 if any card fails validation.
10159
-
10160
- remove-card --rg <dir> --id <card-id>
10161
- Remove a card and its task from the board.
10162
-
10163
- retrigger --rg <dir> --task <task-name>
10164
- Mark a task not-started and drain to re-trigger it.
10165
-
10166
- TASK CALLBACKS (called by task executor scripts)
10167
- task-completed --token <callbackToken> [--data <json>]
10168
- Signal successful task completion with optional JSON result data.
10169
-
10170
- task-failed --token <callbackToken> [--error <message>]
10171
- Signal task failure with an optional error message.
10172
-
10173
- task-progress --rg <dir> --token <callbackToken> [--update <json>]
10174
- Signal task progress with optional update payload (for waiting on more evidence, etc.).
10175
-
10176
- SOURCE CALLBACKS (called internally by run-sourcedefs-internal)
10177
- source-data-fetched --tmp <file> --token <sourceToken>
10178
- Atomically rename <file> into the outputFile destination and record delivery
10179
- via journal events. Appends a task-progress event to re-invoke the card handler.
10180
-
10181
- source-data-fetch-failure --token <sourceToken> [--reason <message>]
10182
- Record a source fetch failure via journal events and append a task-progress event.
10183
-
10184
- INTERNAL COMMANDS
10185
- process-accumulated-events --rg <dir>
10186
- Executes forced drain for this board.
10187
- This command is also used as the background relay worker.
10188
- By default it schedules a detached worker and returns quickly.
10189
- Internal workers run with --inline-loop to perform the settle loop.
10190
-
10191
- Eventual-progress guarantee is relay-based (not per-call blocking guarantee):
10192
- 1) at least one runner continues processing,
10193
- 2) no crash/forced exit in relay window,
10194
- 3) lock stays healthy,
10195
- 4) event production eventually quiesces.
10196
-
10197
- run-sourcedefs-internal --card <card.json> --token <callbackToken> --rg <dir>
10198
- Execute all source[] entries for a card, then report delivery or failure.
10199
- (Internal command \u2014 invoked by the card-handler. Not intended for direct use.)
10200
-
10201
- If <dir>/.task-executor exists, invokes it with run-source-fetch subcommand:
10202
- <executor> run-source-fetch --in <source_json> --out <outfile> --err <errfile>
10203
-
10204
- If no .task-executor is registered, uses board-live-cards built-in run-source-fetch.
10205
-
10206
- run-source-fetch --in <source.json> --out <result.json> [--err <error.txt>]
10207
- Execute a source definition. Board-live-cards reads source.cli and executes it.
10208
- Writes result to --out. Presence of --out after exit indicates success.
10209
-
10210
- describe-task-executor-capabilities --rg <dir>
10211
- Invoke the registered task-executor's describe-capabilities subcommand and
10212
- print its capabilities JSON to stdout. Requires a .task-executor file in <dir>.
10213
-
10214
- probe-source --card <card.json> [--source-idx <n>] [--source-bind <name>]
10215
- [--mock-projections <json>] [--rg <boardDir>] [--out <result.json>]
10216
- Validate that a card source can be fetched successfully.
10217
- Reads the card file, extracts the chosen source (default: index 0), builds the
10218
- run-source-fetch --in payload with the supplied _projections data, invokes the
10219
- registered task-executor (or built-in executor for source.cli), and reports pass/fail.
10220
- --mock-projections: JSON string (or @file.json) providing pre-resolved _projections values
10221
- the source needs. Craft the minimal payload that exercises the
10222
- source \u2014 e.g. '{"holdings":[{"ticker":"AAPL","quantity":10}]}'.
10223
- If omitted, _projections is passed as empty ({}).
10224
- --source-idx: 0-based index into card.source_defs[]. Default: 0.
10225
- --source-bind: Select source by its bindTo name instead of index.
10226
- --rg: Board directory used to find .task-executor. Defaults to the
10227
- directory containing the card file.
10228
- --out: Optional path to write the raw fetch result JSON.
10229
- Prints a structured report ending with a [probe-source:result] JSON line.
10230
- Exits 0 on PROBE_PASS, 1 on PROBE_FAIL.
10231
-
10232
- run-inference-internal --in <input.json> --token <inferenceToken>
10233
- Execute inference via registered .inference-adapter and forward result to inference-done.
10234
- inferenceToken encodes boardDir (rg), cardId (cid), callbackToken (cbk), checksum (cs).
10235
- (Internal command \u2014 invoked by the card-handler when custom completion rule is used.)
10236
-
10237
- inference-done --tmp <result.json> --token <inferenceToken>
10238
- Persist llm_task_completion_inference on the card and append a task-progress event.
10239
- Reads boardDir/callbackToken/checksum from decoded inferenceToken; deletes --tmp file after reading.
10240
- (Internal command \u2014 invoked by run-inference-internal.)
10241
-
10242
- RUN-SOURCE-FETCH PROTOCOL
10243
- External task-executors implement:
10244
- <executor> run-source-fetch --in <source.json> --out <result.json> [--err <error.txt>]
10245
-
10246
- INPUT: --in file contains the full source_defs[x] definition object
10247
- OUTPUT: --out file is written with the result to signal success.
10248
- --err file may be written to explain failure.
10249
-
10250
- Exit code and --out presence determine success:
10251
- Exit 0 + --out file present \u2192 source delivery recorded, card re-evaluated.
10252
- Exit non-zero OR --out absent \u2192 source-data-fetch-failure recorded.
10253
-
10254
- BOARD-LIVE-CARDS BUILT-IN EXECUTOR
10255
- Understands source.cli field only:
10256
- "source_defs": [{ "cli": "node ../fetch-prices.js", "bindTo": "prices", "outputFile": "prices.json" }]
10257
-
10258
- The source.cli command is executed with:
10259
- - Direct command invocation (no shell; quote-aware argument parsing)
10260
- - Stdout is captured and delivered to the card as-is
10261
- - Timeout from source.timeout (default 120s)
10262
-
10263
- The source.cli command must:
10264
- - Execute successfully (exit 0)
10265
- - Write output to stdout
10266
- - Complete within the timeout
10267
-
10268
- The output format is the concern of the card's compute function to interpret.
10269
-
10270
- External task-executors can interpret source definitions however they want.
10271
-
10272
- EXAMPLES
10273
- board-live-cards-cli init ./my-board
10274
- board-live-cards-cli init ./my-board --task-executor ./executors/my-runner.py
10275
- board-live-cards-cli upsert-card --rg ./my-board --card cards/prices.json
10276
- board-live-cards-cli status --rg ./my-board
10277
- board-live-cards-cli retrigger --rg ./my-board --task price-fetch
10278
- board-live-cards-cli probe-source --card cards/card-market-prices.json --source-idx 0 --rg ./my-board --mock-projections '{"holdings":[{"ticker":"AAPL","quantity":10}]}'
10279
- `.trimStart());
10280
- }
10281
- var isMain = process.argv[1] && path.resolve(process.argv[1]) === path.resolve(new URL(import.meta.url).pathname.replace(/^\/([A-Z]:)/, "$1"));
10587
+ var isMain = process.argv[1] && path7.resolve(process.argv[1]) === path7.resolve(new URL(import.meta.url).pathname.replace(/^\/([A-Z]:)/, "$1"));
10282
10588
  if (isMain) {
10283
10589
  cli(process.argv.slice(2)).catch((err) => {
10284
10590
  const msg = err instanceof Error ? err.message : String(err);
@@ -10287,6 +10593,6 @@ if (isMain) {
10287
10593
  });
10288
10594
  }
10289
10595
 
10290
- export { BoardJournal, appendCardInventory, appendEventToJournal, buildCardInventoryIndex, cli, createBoardReactiveGraph, decideSourceAction, decodeSourceToken, encodeSourceToken, getUndrainedEntries, initBoard, isSourceInFlight, liveCardToTaskConfig, loadBoard, loadBoardEnvelope, lookupCardPath, nextEntryAfterFetchDelivery, nextEntryAfterFetchFailure, processAccumulatedEvents, processAccumulatedEventsForced, processAccumulatedEventsInfinitePass, readCardInventory, saveBoard, withBoardLock };
10596
+ export { BoardJournal, appendCardInventory, appendEventToJournal, buildCardInventoryIndex, cli, decideSourceAction, decodeSourceToken, encodeSourceToken, getUndrainedEntries, initBoard, isSourceInFlight, liveCardToTaskConfig, loadBoard, loadBoardEnvelope, lookupCardPath, nextEntryAfterFetchDelivery, nextEntryAfterFetchFailure, processAccumulatedEvents, processAccumulatedEventsForced, processAccumulatedEventsInfinitePass, readCardInventory, saveBoard, withBoardLock };
10291
10597
  //# sourceMappingURL=board-live-cards-cli.js.map
10292
10598
  //# sourceMappingURL=board-live-cards-cli.js.map