zelari-code 2.0.0-alpha.3 → 2.0.0-alpha.5

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.
@@ -1939,40 +1939,47 @@ import os3 from "node:os";
1939
1939
  function getProviderConfigPath() {
1940
1940
  return process.env.ANATHEMA_PROVIDER_CONFIG_FILE ?? path4.join(os3.homedir(), ".tmp", "zelari-code", "provider.json");
1941
1941
  }
1942
- function getProviderConfig() {
1943
- const envActive = process.env.ANATHEMA_ACTIVE_PROVIDER;
1944
- const envModel = process.env.OPENAI_MODEL;
1945
- const file2 = getProviderConfigPath();
1946
- let stored = null;
1947
- if (existsSync3(file2)) {
1948
- try {
1949
- const raw = readFileSync3(file2, "utf-8");
1950
- const parsed = JSON.parse(raw);
1951
- if (parsed && typeof parsed === "object" && typeof parsed.activeProviderId === "string" && parsed.modelByProvider && typeof parsed.modelByProvider === "object") {
1952
- stored = {
1953
- activeProviderId: parsed.activeProviderId,
1954
- modelByProvider: { ...DEFAULTS.modelByProvider, ...parsed.modelByProvider },
1955
- thinkingByProvider: { ...DEFAULTS.thinkingByProvider, ...parsed.thinkingByProvider },
1956
- customEndpoints: mergeCustomEndpoints(parsed.customEndpoints),
1957
- krakenVerifier: mergeKrakenVerifier(parsed.krakenVerifier)
1958
- };
1959
- }
1960
- } catch {
1961
- }
1942
+ function mergeStoredProviderConfig(parsed) {
1943
+ if (parsed && typeof parsed === "object" && typeof parsed.activeProviderId === "string" && parsed.modelByProvider && typeof parsed.modelByProvider === "object") {
1944
+ return {
1945
+ activeProviderId: parsed.activeProviderId,
1946
+ modelByProvider: { ...DEFAULTS.modelByProvider, ...parsed.modelByProvider },
1947
+ thinkingByProvider: { ...DEFAULTS.thinkingByProvider, ...parsed.thinkingByProvider },
1948
+ customEndpoints: mergeCustomEndpoints(parsed.customEndpoints),
1949
+ krakenVerifier: mergeKrakenVerifier(parsed.krakenVerifier)
1950
+ };
1962
1951
  }
1963
- const base = stored ?? {
1952
+ return cloneDefaults();
1953
+ }
1954
+ function cloneDefaults() {
1955
+ return {
1964
1956
  ...DEFAULTS,
1965
1957
  modelByProvider: { ...DEFAULTS.modelByProvider },
1966
1958
  thinkingByProvider: { ...DEFAULTS.thinkingByProvider },
1967
1959
  customEndpoints: { ...DEFAULTS.customEndpoints }
1968
1960
  };
1961
+ }
1962
+ function applyEnvOverrides(config2) {
1963
+ const envActive = process.env.ANATHEMA_ACTIVE_PROVIDER;
1964
+ const envModel = process.env.OPENAI_MODEL;
1969
1965
  if (envActive && PROVIDERS.some((p3) => p3.id === envActive)) {
1970
- base.activeProviderId = envActive;
1966
+ config2.activeProviderId = envActive;
1971
1967
  }
1972
1968
  if (envModel && envModel.trim().length > 0) {
1973
- base.modelByProvider[base.activeProviderId] = envModel;
1969
+ config2.modelByProvider[config2.activeProviderId] = envModel;
1970
+ }
1971
+ return config2;
1972
+ }
1973
+ function getProviderConfig() {
1974
+ const file2 = getProviderConfigPath();
1975
+ let parsed = null;
1976
+ if (existsSync3(file2)) {
1977
+ try {
1978
+ parsed = JSON.parse(readFileSync3(file2, "utf-8"));
1979
+ } catch {
1980
+ }
1974
1981
  }
1975
- return base;
1982
+ return applyEnvOverrides(mergeStoredProviderConfig(parsed));
1976
1983
  }
1977
1984
  function writeProviderConfig(config2) {
1978
1985
  const file2 = getProviderConfigPath();
@@ -2105,25 +2112,12 @@ function getActiveModel() {
2105
2112
  }
2106
2113
  async function loadProviderConfig() {
2107
2114
  const file2 = getProviderConfigPath();
2115
+ let parsed = null;
2108
2116
  try {
2109
- const raw = await fs2.readFile(file2, "utf-8");
2110
- const parsed = JSON.parse(raw);
2111
- if (parsed && typeof parsed === "object" && typeof parsed.activeProviderId === "string" && parsed.modelByProvider && typeof parsed.modelByProvider === "object") {
2112
- return {
2113
- activeProviderId: parsed.activeProviderId,
2114
- modelByProvider: { ...DEFAULTS.modelByProvider, ...parsed.modelByProvider },
2115
- thinkingByProvider: { ...DEFAULTS.thinkingByProvider, ...parsed.thinkingByProvider },
2116
- customEndpoints: mergeCustomEndpoints(parsed.customEndpoints)
2117
- };
2118
- }
2117
+ parsed = JSON.parse(await fs2.readFile(file2, "utf-8"));
2119
2118
  } catch {
2120
2119
  }
2121
- return {
2122
- ...DEFAULTS,
2123
- modelByProvider: { ...DEFAULTS.modelByProvider },
2124
- thinkingByProvider: { ...DEFAULTS.thinkingByProvider },
2125
- customEndpoints: { ...DEFAULTS.customEndpoints }
2126
- };
2120
+ return applyEnvOverrides(mergeStoredProviderConfig(parsed));
2127
2121
  }
2128
2122
  var DEFAULTS;
2129
2123
  var init_providerConfig = __esm({
@@ -28387,6 +28381,23 @@ var init_modelSurface = __esm({
28387
28381
  }
28388
28382
  });
28389
28383
 
28384
+ // packages/core/dist/session/agentAdapter.js
28385
+ function derivedToAgentMessages(messages) {
28386
+ const out = [];
28387
+ for (const m of messages) {
28388
+ const agent = { role: m.role, content: m.content };
28389
+ if (m.toolCallId !== void 0)
28390
+ agent.toolCallId = m.toolCallId;
28391
+ out.push(agent);
28392
+ }
28393
+ return out;
28394
+ }
28395
+ var init_agentAdapter = __esm({
28396
+ "packages/core/dist/session/agentAdapter.js"() {
28397
+ "use strict";
28398
+ }
28399
+ });
28400
+
28390
28401
  // packages/core/dist/session/writer.js
28391
28402
  import { promises as fs9 } from "node:fs";
28392
28403
  import path14 from "node:path";
@@ -28841,6 +28852,7 @@ var init_session = __esm({
28841
28852
  "use strict";
28842
28853
  init_types8();
28843
28854
  init_modelSurface();
28855
+ init_agentAdapter();
28844
28856
  init_writer();
28845
28857
  init_replay();
28846
28858
  init_store();
@@ -30170,6 +30182,7 @@ __export(dist_exports, {
30170
30182
  defaultPersonaParse: () => defaultPersonaParse,
30171
30183
  deriveMessages: () => deriveMessages,
30172
30184
  deriveMissionState: () => deriveMissionState,
30185
+ derivedToAgentMessages: () => derivedToAgentMessages,
30173
30186
  detectAssistantTextLoop: () => detectAssistantTextLoop,
30174
30187
  detectAssistantTextLoopWindow: () => detectAssistantTextLoopWindow,
30175
30188
  detectDegradedRun: () => detectDegradedRun,
@@ -30740,6 +30753,34 @@ var init_sessionSpine = __esm({
30740
30753
  userMessage(text) {
30741
30754
  void this.append({ kind: "user.message", actor: ACTOR_USER, data: { text } });
30742
30755
  }
30756
+ /**
30757
+ * Log an assistant message outside the streaming path — legacy
30758
+ * `--history` import (Exit-1/E1.2). Same event shape the message_end
30759
+ * coalescer emits, so deriveMessages() treats both identically.
30760
+ */
30761
+ assistantMessage(text, extra) {
30762
+ void this.append({
30763
+ kind: "assistant.message",
30764
+ actor: ACTOR_AGENT,
30765
+ data: { text, ...extra }
30766
+ });
30767
+ }
30768
+ /** Await all pending appends (import → derive read-back needs this). */
30769
+ async flush() {
30770
+ await this.chain;
30771
+ }
30772
+ /**
30773
+ * Derive prior-turn model context from the on-disk log. Null when the
30774
+ * log is missing/empty — callers decide whether that means "fresh".
30775
+ */
30776
+ async derivedPriorTurns() {
30777
+ if (this.status !== "active" && this.status !== "closed") return null;
30778
+ const report = await readSessionLog(
30779
+ path21.join(this.sessionsDir, this.sessionId, "events.jsonl")
30780
+ ).catch(() => null);
30781
+ if (!report || report.events.length === 0) return null;
30782
+ return deriveMessages(report.events);
30783
+ }
30743
30784
  /** Mirror one BrainEvent (coalescing message deltas until message_end). */
30744
30785
  mirrorBrainEvent(ev) {
30745
30786
  if (this.status !== "active" || !this.writer) return;
@@ -39550,6 +39591,526 @@ var init_toolRegistry = __esm({
39550
39591
  }
39551
39592
  });
39552
39593
 
39594
+ // src/cli/phase.ts
39595
+ var phase_exports = {};
39596
+ __export(phase_exports, {
39597
+ PHASES: () => PHASES,
39598
+ PLAN_ALLOWED_WRITE_TOOLS: () => PLAN_ALLOWED_WRITE_TOOLS,
39599
+ PLAN_BLOCKED_TOOLS: () => PLAN_BLOCKED_TOOLS,
39600
+ describePhase: () => describePhase,
39601
+ nextPhase: () => nextPhase,
39602
+ parsePhase: () => parsePhase
39603
+ });
39604
+ function parsePhase(input) {
39605
+ const v = input.trim().toLowerCase();
39606
+ return PHASES.includes(v) ? v : null;
39607
+ }
39608
+ function nextPhase(current) {
39609
+ return current === "plan" ? "build" : "plan";
39610
+ }
39611
+ function describePhase(phase2) {
39612
+ switch (phase2) {
39613
+ case "plan":
39614
+ return "plan \u2014 explore & design only (no project writes; plan files allowed)";
39615
+ default:
39616
+ return "build \u2014 implement with full tools";
39617
+ }
39618
+ }
39619
+ var PHASES, PLAN_ALLOWED_WRITE_TOOLS, PLAN_BLOCKED_TOOLS;
39620
+ var init_phase = __esm({
39621
+ "src/cli/phase.ts"() {
39622
+ "use strict";
39623
+ PHASES = ["plan", "build"];
39624
+ PLAN_ALLOWED_WRITE_TOOLS = /* @__PURE__ */ new Set([
39625
+ // Workspace plan/docs — intentional plan-mode outputs
39626
+ "createPlan",
39627
+ "createTask",
39628
+ "updateTask",
39629
+ "createMilestone",
39630
+ "createDocument",
39631
+ "createDecision",
39632
+ "linkDocuments"
39633
+ // Soft writes that only touch .zelari / plan paths are still gated in
39634
+ // toolRegistry by path when needed; write_file/edit_file stay DENIED.
39635
+ ]);
39636
+ PLAN_BLOCKED_TOOLS = /* @__PURE__ */ new Set([
39637
+ "write_file",
39638
+ "edit_file",
39639
+ "apply_diff",
39640
+ "bash"
39641
+ ]);
39642
+ }
39643
+ });
39644
+
39645
+ // src/cli/mode.ts
39646
+ function nextMode(current) {
39647
+ const i = MODES.indexOf(current);
39648
+ return MODES[(i + 1) % MODES.length] ?? "kraken";
39649
+ }
39650
+ function parseMode(input) {
39651
+ const v = input.trim().toLowerCase();
39652
+ if (MODES.includes(v)) return v;
39653
+ return MODE_ALIASES[v] ?? null;
39654
+ }
39655
+ function describeMode(mode) {
39656
+ switch (mode) {
39657
+ case "council":
39658
+ return "council \u2014 multi-member plan/design (Caronte\u2026Lucifero; build needs ZELARI_COUNCIL_CAN_BUILD=1)";
39659
+ case "zelari":
39660
+ return "zelari \u2014 mission: plan@council \u2192 build@kraken (legacy: ZELARI_BUILD_VIA_AGENT=0)";
39661
+ default:
39662
+ return "kraken \u2014 super-agent lead (spawns explore/general/verify tentacles; default implementer)";
39663
+ }
39664
+ }
39665
+ var MODES, MODE_ALIASES;
39666
+ var init_mode = __esm({
39667
+ "src/cli/mode.ts"() {
39668
+ "use strict";
39669
+ MODES = ["kraken", "council", "zelari"];
39670
+ MODE_ALIASES = {
39671
+ agent: "kraken",
39672
+ single: "kraken"
39673
+ };
39674
+ }
39675
+ });
39676
+
39677
+ // src/cli/headless.ts
39678
+ import { readFileSync as readFileSync22 } from "node:fs";
39679
+ function defaultProfileForMode(mode) {
39680
+ switch (mode) {
39681
+ case "council":
39682
+ return "council/v1";
39683
+ case "zelari":
39684
+ return "mission/v1";
39685
+ default:
39686
+ return "kraken/v1";
39687
+ }
39688
+ }
39689
+ function parseHeadlessFlags(argv) {
39690
+ if (!argv.includes("--headless")) {
39691
+ return { options: null };
39692
+ }
39693
+ let task;
39694
+ let output = "json";
39695
+ let mode = "kraken";
39696
+ let phase2 = "build";
39697
+ let modeExplicit = false;
39698
+ let councilFlag = false;
39699
+ let provider;
39700
+ let model;
39701
+ let history2;
39702
+ let todos2;
39703
+ let once = false;
39704
+ let profile;
39705
+ let resumeSessionId;
39706
+ let exportSessionPath;
39707
+ let strictDone = false;
39708
+ let krakenGraph;
39709
+ let planOnly = process.env.ZELARI_KRAKEN_PLAN_ONLY === "1" || process.env.ZELARI_KRAKEN_PLAN_ONLY === "true";
39710
+ let runPlan = process.env.ZELARI_KRAKEN_RUN_PLAN;
39711
+ for (let i = 0; i < argv.length; i++) {
39712
+ const arg = argv[i];
39713
+ if (arg === "--headless") continue;
39714
+ if (arg === "--output") {
39715
+ const next = argv[i + 1];
39716
+ if (next === "json" || next === "plain") {
39717
+ output = next;
39718
+ i++;
39719
+ } else {
39720
+ return {
39721
+ options: null,
39722
+ error: `--output requires 'json' or 'plain', got '${next ?? "(missing)"}'`
39723
+ };
39724
+ }
39725
+ } else if (arg === "--task") {
39726
+ task = argv[i + 1];
39727
+ i++;
39728
+ } else if (arg === "--task-file") {
39729
+ const next = argv[i + 1];
39730
+ if (next) {
39731
+ try {
39732
+ const fromFile = readFileSync22(next, "utf-8");
39733
+ if (fromFile.trim()) task = fromFile;
39734
+ } catch {
39735
+ }
39736
+ }
39737
+ i++;
39738
+ } else if (arg === "--council") {
39739
+ councilFlag = true;
39740
+ } else if (arg === "--mode") {
39741
+ const next = argv[i + 1];
39742
+ const parsed = next ? parseMode(next) : null;
39743
+ if (!parsed) {
39744
+ return {
39745
+ options: null,
39746
+ error: `--mode requires 'kraken', 'council', or 'zelari' (agent=alias), got '${next ?? "(missing)"}'`
39747
+ };
39748
+ }
39749
+ mode = parsed;
39750
+ modeExplicit = true;
39751
+ i++;
39752
+ } else if (arg === "--phase") {
39753
+ const next = argv[i + 1];
39754
+ const parsed = next ? parsePhase(next) : null;
39755
+ if (!parsed) {
39756
+ return {
39757
+ options: null,
39758
+ error: `--phase requires 'plan' or 'build', got '${next ?? "(missing)"}'`
39759
+ };
39760
+ }
39761
+ phase2 = parsed;
39762
+ i++;
39763
+ } else if (arg === "--provider") {
39764
+ provider = argv[i + 1];
39765
+ i++;
39766
+ } else if (arg === "--model") {
39767
+ model = argv[i + 1];
39768
+ i++;
39769
+ } else if (arg === "--history" || arg === "--history-file") {
39770
+ const next = argv[i + 1];
39771
+ if (next) {
39772
+ let raw = null;
39773
+ if (arg === "--history-file") {
39774
+ try {
39775
+ raw = readFileSync22(next, "utf-8");
39776
+ } catch {
39777
+ raw = null;
39778
+ }
39779
+ } else {
39780
+ raw = next;
39781
+ }
39782
+ if (raw) {
39783
+ try {
39784
+ const parsedHist = JSON.parse(raw);
39785
+ if (Array.isArray(parsedHist)) {
39786
+ history2 = parsedHist.filter(
39787
+ (m) => !!m && typeof m === "object" && typeof m.role === "string"
39788
+ ).map((m) => {
39789
+ const role = String(m.role);
39790
+ const raw2 = m.content;
39791
+ const content = typeof raw2 === "string" ? raw2 : raw2 == null ? "" : typeof raw2 === "object" ? JSON.stringify(raw2) : String(raw2);
39792
+ const msg = {
39793
+ role,
39794
+ content
39795
+ };
39796
+ if (typeof m.toolCallId === "string") {
39797
+ msg.toolCallId = m.toolCallId;
39798
+ }
39799
+ return msg;
39800
+ }).filter(
39801
+ (m) => m.role === "user" || m.role === "assistant" || m.role === "tool" || m.role === "system"
39802
+ );
39803
+ }
39804
+ } catch {
39805
+ }
39806
+ }
39807
+ i++;
39808
+ }
39809
+ } else if (arg === "--todos") {
39810
+ const next = argv[i + 1];
39811
+ if (next) {
39812
+ try {
39813
+ const parsed = JSON.parse(next);
39814
+ if (Array.isArray(parsed)) {
39815
+ todos2 = parsed.filter(
39816
+ (t) => !!t && typeof t === "object" && typeof t.content === "string"
39817
+ ).map((t) => ({
39818
+ id: typeof t.id === "string" ? t.id : void 0,
39819
+ content: String(t.content).slice(0, 500),
39820
+ status: t.status
39821
+ }));
39822
+ }
39823
+ } catch {
39824
+ }
39825
+ i++;
39826
+ }
39827
+ } else if (arg === "--once") {
39828
+ once = true;
39829
+ } else if (arg === "--profile") {
39830
+ const next = argv[i + 1];
39831
+ if (!next || next.startsWith("--")) {
39832
+ return { options: null, error: `--profile requires a profile id (e.g. kraken/v1), got '${next ?? "(missing)"}'` };
39833
+ }
39834
+ try {
39835
+ resolveProfile(next);
39836
+ } catch (err) {
39837
+ return {
39838
+ options: null,
39839
+ error: err instanceof Error ? err.message : String(err)
39840
+ };
39841
+ }
39842
+ profile = next;
39843
+ i++;
39844
+ } else if (arg === "--resume") {
39845
+ const next = argv[i + 1];
39846
+ if (!next || next.startsWith("--")) {
39847
+ return { options: null, error: `--resume requires a session id, got '${next ?? "(missing)"}'` };
39848
+ }
39849
+ resumeSessionId = next;
39850
+ i++;
39851
+ } else if (arg === "--export-session") {
39852
+ const next = argv[i + 1];
39853
+ if (!next || next.startsWith("--")) {
39854
+ return { options: null, error: `--export-session requires a path (or - for stdout), got '${next ?? "(missing)"}'` };
39855
+ }
39856
+ exportSessionPath = next;
39857
+ i++;
39858
+ } else if (arg === "--strict-done") {
39859
+ strictDone = true;
39860
+ } else if (arg === "--kraken-graph") {
39861
+ krakenGraph = argv[i + 1];
39862
+ i++;
39863
+ } else if (arg === "--kraken-graph-file") {
39864
+ const next = argv[i + 1];
39865
+ if (next) {
39866
+ try {
39867
+ const fromFile = readFileSync22(next, "utf-8");
39868
+ if (fromFile.trim()) krakenGraph = fromFile;
39869
+ } catch {
39870
+ }
39871
+ }
39872
+ i++;
39873
+ } else if (arg === "--plan-only") {
39874
+ planOnly = true;
39875
+ } else if (arg === "--run-plan") {
39876
+ runPlan = argv[i + 1];
39877
+ i++;
39878
+ }
39879
+ }
39880
+ if (councilFlag && !modeExplicit) {
39881
+ mode = "council";
39882
+ } else if (councilFlag && modeExplicit && mode !== "council") {
39883
+ return {
39884
+ options: null,
39885
+ error: `--council conflicts with --mode ${mode}`
39886
+ };
39887
+ }
39888
+ if (task && krakenGraph) {
39889
+ return { options: null, error: "--task and --kraken-graph are mutually exclusive" };
39890
+ }
39891
+ if ((!task || task.trim().length === 0) && (!krakenGraph || krakenGraph.trim().length === 0)) {
39892
+ return { options: null, error: "--headless requires --task <prompt> or --kraken-graph <goal>" };
39893
+ }
39894
+ return {
39895
+ options: {
39896
+ task: task ?? "",
39897
+ output,
39898
+ mode,
39899
+ phase: phase2,
39900
+ useCouncil: mode === "council",
39901
+ provider,
39902
+ model,
39903
+ ...history2 && history2.length > 0 ? { history: history2 } : {},
39904
+ ...todos2 && todos2.length > 0 ? { todos: todos2 } : {},
39905
+ ...once ? { once: true } : {},
39906
+ ...profile ? { profile } : {},
39907
+ ...resumeSessionId ? { resumeSessionId } : {},
39908
+ ...exportSessionPath ? { exportSessionPath } : {},
39909
+ ...strictDone ? { strictDone: true } : {},
39910
+ ...krakenGraph ? { krakenGraph } : {},
39911
+ ...planOnly ? { planOnly: true } : {},
39912
+ ...runPlan ? { runPlan } : {}
39913
+ }
39914
+ };
39915
+ }
39916
+ async function resolveHeadlessKey(providerId) {
39917
+ const spec = PROVIDERS.find((p3) => p3.id === providerId);
39918
+ if (!spec) {
39919
+ return { error: `unknown provider: '${providerId}'` };
39920
+ }
39921
+ const resolved = await resolveApiKeyWithMeta(providerId);
39922
+ if (!resolved || !resolved.apiKey) {
39923
+ return {
39924
+ error: `no API key for provider '${providerId}'.
39925
+ Set the env var ${spec.envVar} or save a key via /login.`
39926
+ };
39927
+ }
39928
+ const { resolveBaseUrl: resolveBaseUrl2 } = await Promise.resolve().then(() => (init_openai_compatible(), openai_compatible_exports));
39929
+ return {
39930
+ apiKey: resolved.apiKey,
39931
+ baseUrl: resolveBaseUrl2(providerId)
39932
+ };
39933
+ }
39934
+ function resolveHeadlessProvider(opts) {
39935
+ const provider = opts.provider ?? getActiveProvider().id;
39936
+ const model = opts.model ?? getModelForProvider(provider);
39937
+ return { provider, model };
39938
+ }
39939
+ function emitEvent(event) {
39940
+ process.stdout.write(JSON.stringify(event) + "\n");
39941
+ }
39942
+ var init_headless = __esm({
39943
+ "src/cli/headless.ts"() {
39944
+ "use strict";
39945
+ init_keyStore();
39946
+ init_providerConfig();
39947
+ init_openai_compatible();
39948
+ init_phase();
39949
+ init_mode();
39950
+ init_runtime2();
39951
+ }
39952
+ });
39953
+
39954
+ // src/cli/headlessSpine.ts
39955
+ var headlessSpine_exports = {};
39956
+ __export(headlessSpine_exports, {
39957
+ derivedModelSeed: () => derivedModelSeed,
39958
+ exportSessionById: () => exportSessionById,
39959
+ missionStateFromSpine: () => missionStateFromSpine,
39960
+ openHeadlessSpine: () => openHeadlessSpine,
39961
+ resolveHeadlessProfileId: () => resolveHeadlessProfileId,
39962
+ seedHeadlessModelHistory: () => seedHeadlessModelHistory
39963
+ });
39964
+ function resolveHeadlessProfileId(mode, explicit) {
39965
+ if (explicit) return resolveProfile(explicit).id;
39966
+ return defaultProfileForMode(mode ?? "kraken");
39967
+ }
39968
+ async function openHeadlessSpine(opts) {
39969
+ const profileId = resolveHeadlessProfileId(opts.mode, opts.profile);
39970
+ let profileTools = [];
39971
+ try {
39972
+ profileTools = resolveProfile(profileId).tools;
39973
+ } catch {
39974
+ profileTools = [];
39975
+ }
39976
+ const extra = {
39977
+ profile: profileId,
39978
+ workspace: opts.workspace ?? process.cwd(),
39979
+ toolManifestHash: profileTools.length > 0 ? toolManifestHash(profileTools) : void 0
39980
+ };
39981
+ const mirrorOpts = {
39982
+ baseDir: opts.baseDir,
39983
+ quiet: opts.quiet,
39984
+ extraStarted: extra
39985
+ };
39986
+ const spine = await SessionSpineMirror.adopt(opts.sessionId, mirrorOpts);
39987
+ if (spine.status === "active") {
39988
+ spine.note("headless.profile", { profile: profileId, mode: opts.mode ?? "kraken" });
39989
+ }
39990
+ return {
39991
+ sessionId: opts.sessionId,
39992
+ profileId,
39993
+ spine,
39994
+ observe(ev) {
39995
+ if (ev && typeof ev === "object" && "type" in ev) {
39996
+ spine.mirrorBrainEvent(ev);
39997
+ }
39998
+ },
39999
+ userMessage(text) {
40000
+ spine.userMessage(text);
40001
+ },
40002
+ verificationRun(payload) {
40003
+ spine.verificationRun(payload);
40004
+ },
40005
+ missionPhase(phase2, note) {
40006
+ spine.missionPhase(phase2, note);
40007
+ },
40008
+ note(text, data) {
40009
+ spine.note(text, data);
40010
+ },
40011
+ async close(reason = "host-exit") {
40012
+ await spine.close(reason);
40013
+ },
40014
+ async interrupt(note) {
40015
+ if (note) spine.note("headless.interrupt", { note });
40016
+ await spine.release();
40017
+ },
40018
+ async exportJson() {
40019
+ try {
40020
+ const store6 = new SessionStore(resolveSessionsDir({ baseDir: opts.baseDir }));
40021
+ if (!await store6.exists(opts.sessionId)) return null;
40022
+ return await exportSessionJson(store6, opts.sessionId);
40023
+ } catch {
40024
+ return null;
40025
+ }
40026
+ }
40027
+ };
40028
+ }
40029
+ async function exportSessionById(sessionId2, baseDir) {
40030
+ try {
40031
+ const store6 = SessionStore.withDefaults(baseDir ? { baseDir } : {});
40032
+ if (!await store6.exists(sessionId2)) {
40033
+ return { ok: false, error: `session not found: ${sessionId2}` };
40034
+ }
40035
+ return { ok: true, json: await exportSessionJson(store6, sessionId2) };
40036
+ } catch (err) {
40037
+ return { ok: false, error: err instanceof Error ? err.message : String(err) };
40038
+ }
40039
+ }
40040
+ async function missionStateFromSpine(sessionId2, baseDir) {
40041
+ try {
40042
+ const store6 = SessionStore.withDefaults(baseDir ? { baseDir } : {});
40043
+ if (!await store6.exists(sessionId2)) return null;
40044
+ const projection = await store6.projection(sessionId2);
40045
+ return deriveMissionState(projection);
40046
+ } catch {
40047
+ return null;
40048
+ }
40049
+ }
40050
+ async function seedHeadlessModelHistory(handle, legacy) {
40051
+ const mirror = handle.spine;
40052
+ const legacySeed = filterLegacySeed(legacy);
40053
+ if (mirror.status !== "active") {
40054
+ return { history: legacySeed, importedCount: 0, source: "legacy-fallback" };
40055
+ }
40056
+ const existing = await mirror.derivedPriorTurns();
40057
+ if (existing && existing.length > 0) {
40058
+ return { history: derivedModelSeed(existing), importedCount: 0, source: "spine" };
40059
+ }
40060
+ if (legacySeed.length === 0) {
40061
+ return { history: [], importedCount: 0, source: "spine" };
40062
+ }
40063
+ for (const m of legacySeed) {
40064
+ if (m.role === "user") {
40065
+ mirror.userMessage(m.content);
40066
+ } else {
40067
+ mirror.assistantMessage(m.content, { imported: "legacy-history" });
40068
+ }
40069
+ }
40070
+ await mirror.flush();
40071
+ const derived = await mirror.derivedPriorTurns() ?? [];
40072
+ return {
40073
+ history: derivedModelSeed(derived),
40074
+ importedCount: legacySeed.length,
40075
+ source: "spine-import"
40076
+ };
40077
+ }
40078
+ function filterLegacySeed(legacy) {
40079
+ return (legacy ?? []).filter((m) => m.role === "user" || m.role === "assistant").map(
40080
+ (m) => m.role === "assistant" && m.content ? {
40081
+ role: "assistant",
40082
+ content: cleanAgentContent(m.content, {
40083
+ stripQuestion: false,
40084
+ stripThink: false
40085
+ })
40086
+ } : { role: m.role, content: m.content ?? "" }
40087
+ ).filter((m) => (m.content ?? "").trim().length > 0);
40088
+ }
40089
+ function derivedModelSeed(derived) {
40090
+ return derivedToAgentMessages(derived).map(
40091
+ (m) => m.role === "system" ? { role: "user", content: m.content } : m
40092
+ ).map(
40093
+ (m) => m.role === "assistant" && m.content ? {
40094
+ role: "assistant",
40095
+ content: cleanAgentContent(m.content, {
40096
+ stripQuestion: false,
40097
+ stripThink: false
40098
+ })
40099
+ } : m
40100
+ ).filter((m) => m.role === "user" || m.role === "assistant").filter((m) => (m.content ?? "").trim().length > 0);
40101
+ }
40102
+ var init_headlessSpine = __esm({
40103
+ "src/cli/headlessSpine.ts"() {
40104
+ "use strict";
40105
+ init_dist();
40106
+ init_session();
40107
+ init_mission2();
40108
+ init_runtime2();
40109
+ init_sessionSpine();
40110
+ init_headless();
40111
+ }
40112
+ });
40113
+
39553
40114
  // src/cli/state/fileStateStore.ts
39554
40115
  import { createHash as createHash10, randomUUID as randomUUID2 } from "node:crypto";
39555
40116
  import { promises as fs21 } from "node:fs";
@@ -40502,57 +41063,6 @@ var init_phaseState = __esm({
40502
41063
  }
40503
41064
  });
40504
41065
 
40505
- // src/cli/phase.ts
40506
- var phase_exports = {};
40507
- __export(phase_exports, {
40508
- PHASES: () => PHASES,
40509
- PLAN_ALLOWED_WRITE_TOOLS: () => PLAN_ALLOWED_WRITE_TOOLS,
40510
- PLAN_BLOCKED_TOOLS: () => PLAN_BLOCKED_TOOLS,
40511
- describePhase: () => describePhase,
40512
- nextPhase: () => nextPhase,
40513
- parsePhase: () => parsePhase
40514
- });
40515
- function parsePhase(input) {
40516
- const v = input.trim().toLowerCase();
40517
- return PHASES.includes(v) ? v : null;
40518
- }
40519
- function nextPhase(current) {
40520
- return current === "plan" ? "build" : "plan";
40521
- }
40522
- function describePhase(phase2) {
40523
- switch (phase2) {
40524
- case "plan":
40525
- return "plan \u2014 explore & design only (no project writes; plan files allowed)";
40526
- default:
40527
- return "build \u2014 implement with full tools";
40528
- }
40529
- }
40530
- var PHASES, PLAN_ALLOWED_WRITE_TOOLS, PLAN_BLOCKED_TOOLS;
40531
- var init_phase = __esm({
40532
- "src/cli/phase.ts"() {
40533
- "use strict";
40534
- PHASES = ["plan", "build"];
40535
- PLAN_ALLOWED_WRITE_TOOLS = /* @__PURE__ */ new Set([
40536
- // Workspace plan/docs — intentional plan-mode outputs
40537
- "createPlan",
40538
- "createTask",
40539
- "updateTask",
40540
- "createMilestone",
40541
- "createDocument",
40542
- "createDecision",
40543
- "linkDocuments"
40544
- // Soft writes that only touch .zelari / plan paths are still gated in
40545
- // toolRegistry by path when needed; write_file/edit_file stay DENIED.
40546
- ]);
40547
- PLAN_BLOCKED_TOOLS = /* @__PURE__ */ new Set([
40548
- "write_file",
40549
- "edit_file",
40550
- "apply_diff",
40551
- "bash"
40552
- ]);
40553
- }
40554
- });
40555
-
40556
41066
  // src/cli/provider/localCli/claudeStreamJson.ts
40557
41067
  function textBlock(text) {
40558
41068
  return { type: "text", text };
@@ -40816,14 +41326,14 @@ var init_claudeProvider = __esm({
40816
41326
  });
40817
41327
 
40818
41328
  // src/cli/workspace/projectInstructions.ts
40819
- import { existsSync as existsSync26, readFileSync as readFileSync22 } from "node:fs";
41329
+ import { existsSync as existsSync26, readFileSync as readFileSync23 } from "node:fs";
40820
41330
  import { join as join20 } from "node:path";
40821
41331
  function loadProjectInstructions(projectRoot = process.cwd(), maxChars = MAX_CHARS) {
40822
41332
  for (const name of CANDIDATES) {
40823
41333
  const full = join20(projectRoot, name);
40824
41334
  if (!existsSync26(full)) continue;
40825
41335
  try {
40826
- let raw = readFileSync22(full, "utf8");
41336
+ let raw = readFileSync23(full, "utf8");
40827
41337
  raw = raw.replace(/\r\n/g, "\n").trim();
40828
41338
  if (!raw) continue;
40829
41339
  if (raw.length <= maxChars) {
@@ -40867,7 +41377,7 @@ __export(workspaceSummary_exports, {
40867
41377
  buildWorkspaceSummary: () => buildWorkspaceSummary,
40868
41378
  buildZelariReadHint: () => buildZelariReadHint
40869
41379
  });
40870
- import { existsSync as existsSync27, readFileSync as readFileSync23, readdirSync as readdirSync6, statSync as statSync5 } from "node:fs";
41380
+ import { existsSync as existsSync27, readFileSync as readFileSync24, readdirSync as readdirSync6, statSync as statSync5 } from "node:fs";
40871
41381
  import { join as join21, relative } from "node:path";
40872
41382
  function buildWorkspaceSummary(projectRoot = process.cwd(), options = {}) {
40873
41383
  const { maxEntries = 30, maxChars = 3500, maxDeps = 24, maxScripts = 16 } = options;
@@ -40905,7 +41415,7 @@ function buildPlanSummary(projectRoot = process.cwd(), options) {
40905
41415
  if (!existsSync27(planPath)) return null;
40906
41416
  let plan;
40907
41417
  try {
40908
- plan = JSON.parse(readFileSync23(planPath, "utf8"));
41418
+ plan = JSON.parse(readFileSync24(planPath, "utf8"));
40909
41419
  } catch {
40910
41420
  return null;
40911
41421
  }
@@ -41063,7 +41573,7 @@ function readPackageJson(projectRoot) {
41063
41573
  const p3 = join21(projectRoot, "package.json");
41064
41574
  if (!existsSync27(p3)) return null;
41065
41575
  try {
41066
- return JSON.parse(readFileSync23(p3, "utf8"));
41576
+ return JSON.parse(readFileSync24(p3, "utf8"));
41067
41577
  } catch {
41068
41578
  return null;
41069
41579
  }
@@ -41191,7 +41701,7 @@ var composeContext_exports = {};
41191
41701
  __export(composeContext_exports, {
41192
41702
  composeProjectContext: () => composeProjectContext
41193
41703
  });
41194
- import { existsSync as existsSync29, readdirSync as readdirSync7, readFileSync as readFileSync24 } from "node:fs";
41704
+ import { existsSync as existsSync29, readdirSync as readdirSync7, readFileSync as readFileSync25 } from "node:fs";
41195
41705
  import { join as join23 } from "node:path";
41196
41706
  function cap2(text, max, label) {
41197
41707
  if (!text || text.length <= max) return { text: text || "", truncated: false };
@@ -41332,15 +41842,15 @@ function readDurableHeadSync(projectRoot) {
41332
41842
  try {
41333
41843
  const headPath = join23(projectRoot, ".zelari", "state", "HEAD.json");
41334
41844
  if (!existsSync29(headPath)) return "";
41335
- const head = JSON.parse(readFileSync24(headPath, "utf8"));
41845
+ const head = JSON.parse(readFileSync25(headPath, "utf8"));
41336
41846
  if (!head?.id) return "";
41337
41847
  const metaPath = join23(projectRoot, ".zelari", "state", "commits", `${head.id}.json`);
41338
41848
  if (!existsSync29(metaPath)) return "";
41339
- const meta3 = JSON.parse(readFileSync24(metaPath, "utf8"));
41849
+ const meta3 = JSON.parse(readFileSync25(metaPath, "utf8"));
41340
41850
  const discPath = meta3.artifactDir ? join23(projectRoot, ".zelari", "state", meta3.artifactDir, "discoveries.json") : join23(projectRoot, ".zelari", "state", "artifacts", head.id, "discoveries.json");
41341
41851
  let discoveries = [];
41342
41852
  if (existsSync29(discPath)) {
41343
- discoveries = JSON.parse(readFileSync24(discPath, "utf8"));
41853
+ discoveries = JSON.parse(readFileSync25(discPath, "utf8"));
41344
41854
  }
41345
41855
  const reusable = discoveries.filter((d) => d.reusable !== false);
41346
41856
  const lines = [
@@ -41373,13 +41883,13 @@ var planDetect_exports = {};
41373
41883
  __export(planDetect_exports, {
41374
41884
  hasWorkspacePlan: () => hasWorkspacePlan
41375
41885
  });
41376
- import { existsSync as existsSync30, readFileSync as readFileSync25 } from "node:fs";
41886
+ import { existsSync as existsSync30, readFileSync as readFileSync26 } from "node:fs";
41377
41887
  import { join as join24 } from "node:path";
41378
41888
  function hasWorkspacePlan(projectRoot = process.cwd()) {
41379
41889
  const planPath = join24(resolveWorkspaceRoot(projectRoot), "plan.json");
41380
41890
  if (!existsSync30(planPath)) return false;
41381
41891
  try {
41382
- const parsed = JSON.parse(readFileSync25(planPath, "utf8"));
41892
+ const parsed = JSON.parse(readFileSync26(planPath, "utf8"));
41383
41893
  return Array.isArray(parsed.phases) && parsed.phases.length > 0;
41384
41894
  } catch {
41385
41895
  return false;
@@ -41441,7 +41951,7 @@ import {
41441
41951
  existsSync as existsSync31,
41442
41952
  readdirSync as readdirSync8,
41443
41953
  writeFileSync as writeFileSync17,
41444
- readFileSync as readFileSync26,
41954
+ readFileSync as readFileSync27,
41445
41955
  mkdirSync as mkdirSync15,
41446
41956
  renameSync as renameSync4
41447
41957
  } from "node:fs";
@@ -41462,7 +41972,7 @@ function readPlan(ctx) {
41462
41972
  if (existsSync31(jsonPath)) {
41463
41973
  try {
41464
41974
  const parsed = JSON.parse(
41465
- readFileSync26(jsonPath, "utf8")
41975
+ readFileSync27(jsonPath, "utf8")
41466
41976
  );
41467
41977
  const { phases, tasks, milestones, ...root } = parsed;
41468
41978
  return {
@@ -42026,7 +42536,7 @@ function searchDocumentsStub(ctx) {
42026
42536
  const results = [];
42027
42537
  for (const file2 of files) {
42028
42538
  if (!existsSync31(file2)) continue;
42029
- const raw = readFileSync26(file2, "utf8");
42539
+ const raw = readFileSync27(file2, "utf8");
42030
42540
  const content = raw.toLowerCase();
42031
42541
  let idx = -1;
42032
42542
  let matchLen = 0;
@@ -42199,10 +42709,12 @@ __export(updater_exports, {
42199
42709
  REGISTRY_URL: () => REGISTRY_URL,
42200
42710
  checkForUpdate: () => checkForUpdate,
42201
42711
  compareSemver: () => compareSemver,
42712
+ distTagForVersion: () => distTagForVersion,
42202
42713
  fetchLatestVersion: () => fetchLatestVersion,
42203
42714
  getCurrentVersion: () => getCurrentVersion,
42204
42715
  looksLikeBrokenShim: () => looksLikeBrokenShim,
42205
42716
  performUpdate: () => performUpdate,
42717
+ registryUrlForTag: () => registryUrlForTag,
42206
42718
  resolveBundledNpmCli: () => resolveBundledNpmCli
42207
42719
  });
42208
42720
  import { createRequire as createRequire2 } from "node:module";
@@ -42256,6 +42768,15 @@ function compareSemver(a, b) {
42256
42768
  if (bPre === null) return -1;
42257
42769
  return aPre < bPre ? -1 : 1;
42258
42770
  }
42771
+ function distTagForVersion(version2) {
42772
+ if (version2.includes("-alpha.")) return "alpha";
42773
+ if (version2.includes("-beta.")) return "beta";
42774
+ if (version2.includes("-next.")) return "next";
42775
+ return "latest";
42776
+ }
42777
+ function registryUrlForTag(tag = distTagForVersion(getCurrentVersion())) {
42778
+ return `https://registry.npmjs.org/zelari-code/${tag}`;
42779
+ }
42259
42780
  async function fetchLatestVersion(fetcher = fetch, registryUrl = REGISTRY_URL, timeoutMs = 5e3) {
42260
42781
  try {
42261
42782
  const controller = new AbortController();
@@ -42277,7 +42798,8 @@ async function fetchLatestVersion(fetcher = fetch, registryUrl = REGISTRY_URL, t
42277
42798
  }
42278
42799
  async function checkForUpdate(fetcher = fetch, registryUrl) {
42279
42800
  const currentVersion = getCurrentVersion();
42280
- const latest = await fetchLatestVersion(fetcher, registryUrl);
42801
+ const url2 = registryUrl ?? registryUrlForTag();
42802
+ const latest = await fetchLatestVersion(fetcher, url2);
42281
42803
  if ("error" in latest) {
42282
42804
  return {
42283
42805
  currentVersion,
@@ -42293,8 +42815,9 @@ async function checkForUpdate(fetcher = fetch, registryUrl) {
42293
42815
  updateAvailable: cmp < 0
42294
42816
  };
42295
42817
  }
42296
- async function performUpdate(packageName = "zelari-code", executor = spawn12, resolveNpmCli = resolveBundledNpmCli) {
42297
- const args = ["install", "-g", `${packageName}@latest`];
42818
+ async function performUpdate(packageName = "zelari-code", executor = spawn12, resolveNpmCli = resolveBundledNpmCli, channel) {
42819
+ const tag = channel ?? distTagForVersion(getCurrentVersion());
42820
+ const args = ["install", "-g", `${packageName}@${tag}`];
42298
42821
  const primary = await runNpm(executor, args, "shim");
42299
42822
  if (primary.ok) return primary;
42300
42823
  const npmCli = resolveNpmCli();
@@ -42522,7 +43045,7 @@ var init_mcpClient = __esm({
42522
43045
  import {
42523
43046
  existsSync as existsSync33,
42524
43047
  mkdirSync as mkdirSync16,
42525
- readFileSync as readFileSync27,
43048
+ readFileSync as readFileSync28,
42526
43049
  writeFileSync as writeFileSync18
42527
43050
  } from "node:fs";
42528
43051
  import { dirname as dirname7, join as join26 } from "node:path";
@@ -42536,7 +43059,7 @@ function getProjectMcpPath(projectRoot) {
42536
43059
  function readFile2(path63) {
42537
43060
  if (!existsSync33(path63)) return {};
42538
43061
  try {
42539
- const parsed = JSON.parse(readFileSync27(path63, "utf8"));
43062
+ const parsed = JSON.parse(readFileSync28(path63, "utf8"));
42540
43063
  const out = {};
42541
43064
  for (const [name, cfg] of Object.entries(parsed.mcpServers ?? {})) {
42542
43065
  if (!cfg || typeof cfg.command !== "string" || !cfg.command.trim()) continue;
@@ -42764,7 +43287,7 @@ __export(mcpManager_exports, {
42764
43287
  readMcpConfig: () => readMcpConfig,
42765
43288
  registerMcpTools: () => registerMcpTools
42766
43289
  });
42767
- import { existsSync as existsSync34, readFileSync as readFileSync28 } from "node:fs";
43290
+ import { existsSync as existsSync34, readFileSync as readFileSync29 } from "node:fs";
42768
43291
  import { join as join27 } from "node:path";
42769
43292
  import { homedir as homedir11 } from "node:os";
42770
43293
  function readMcpConfig(projectRoot = process.cwd(), opts) {
@@ -42779,7 +43302,7 @@ function readMcpConfig(projectRoot = process.cwd(), opts) {
42779
43302
  for (const p3 of paths) {
42780
43303
  if (!existsSync34(p3)) continue;
42781
43304
  try {
42782
- const parsed = JSON.parse(readFileSync28(p3, "utf8"));
43305
+ const parsed = JSON.parse(readFileSync29(p3, "utf8"));
42783
43306
  for (const [name, cfg] of Object.entries(parsed.mcpServers ?? {})) {
42784
43307
  if (!cfg || typeof cfg.command !== "string" || cfg.command.length === 0) continue;
42785
43308
  merged[name] = cfg;
@@ -43018,7 +43541,7 @@ __export(agentsMd_exports, {
43018
43541
  serializeAgentsMd: () => serializeAgentsMd,
43019
43542
  updateAgentsMd: () => updateAgentsMd
43020
43543
  });
43021
- import { existsSync as existsSync35, readFileSync as readFileSync29, writeFileSync as writeFileSync19 } from "node:fs";
43544
+ import { existsSync as existsSync35, readFileSync as readFileSync30, writeFileSync as writeFileSync19 } from "node:fs";
43022
43545
  import { createHash as createHash11 } from "node:crypto";
43023
43546
  import { join as join28 } from "node:path";
43024
43547
  import { readFile as readFile3 } from "node:fs/promises";
@@ -43076,7 +43599,7 @@ async function genConventions(ctx) {
43076
43599
  const lines = [];
43077
43600
  const claudeMd = join28(ctx.projectRoot, "CLAUDE.MD");
43078
43601
  if (existsSync35(claudeMd)) {
43079
- const content = readFileSync29(claudeMd, "utf8");
43602
+ const content = readFileSync30(claudeMd, "utf8");
43080
43603
  const match = content.match(/## Architecture rules[\s\S]+?(?=\n## |\n*$)/);
43081
43604
  if (match) {
43082
43605
  lines.push('<!-- Extracted from CLAUDE.MD "Architecture rules" -->');
@@ -43110,7 +43633,7 @@ async function genBuild(ctx) {
43110
43633
  async function genOpenQuestions(ctx) {
43111
43634
  const path63 = join28(ctx.rootDir, "risks.md");
43112
43635
  if (!existsSync35(path63)) return "_No open questions._";
43113
- const content = readFileSync29(path63, "utf8");
43636
+ const content = readFileSync30(path63, "utf8");
43114
43637
  const lines = content.split("\n");
43115
43638
  const questions = [];
43116
43639
  let currentTitle = "";
@@ -43186,7 +43709,7 @@ function titleCase(id) {
43186
43709
  async function updateAgentsMd(ctx, projectRoot) {
43187
43710
  const agentsPath = join28(projectRoot, "AGENTS.MD");
43188
43711
  if (existsSync35(agentsPath)) {
43189
- const content = readFileSync29(agentsPath, "utf8");
43712
+ const content = readFileSync30(agentsPath, "utf8");
43190
43713
  const hasAnyMarker = AUTO_SECTIONS.some((id) => content.includes(MARKER_OPEN(id)));
43191
43714
  if (!hasAnyMarker) {
43192
43715
  return {
@@ -43202,7 +43725,7 @@ async function updateAgentsMd(ctx, projectRoot) {
43202
43725
  }
43203
43726
  let manualContent = "";
43204
43727
  if (existsSync35(agentsPath)) {
43205
- const { manualBlocks } = parseAgentsMd(readFileSync29(agentsPath, "utf8"));
43728
+ const { manualBlocks } = parseAgentsMd(readFileSync30(agentsPath, "utf8"));
43206
43729
  manualContent = manualBlocks.after;
43207
43730
  } else {
43208
43731
  const projectName2 = projectName(projectRoot);
@@ -43218,7 +43741,7 @@ async function updateAgentsMd(ctx, projectRoot) {
43218
43741
  ""
43219
43742
  ].join("\n");
43220
43743
  }
43221
- const oldContent = existsSync35(agentsPath) ? readFileSync29(agentsPath, "utf8") : "";
43744
+ const oldContent = existsSync35(agentsPath) ? readFileSync30(agentsPath, "utf8") : "";
43222
43745
  const { sections: oldSections } = parseAgentsMd(oldContent);
43223
43746
  const changedSections = [];
43224
43747
  for (const id of AUTO_SECTIONS) {
@@ -43355,7 +43878,7 @@ var init_completeDesign = __esm({
43355
43878
  });
43356
43879
 
43357
43880
  // src/cli/workspace/planDriftCheck.ts
43358
- import { existsSync as existsSync36, readFileSync as readFileSync30, readdirSync as readdirSync9, statSync as statSync6, writeFileSync as writeFileSync20 } from "node:fs";
43881
+ import { existsSync as existsSync36, readFileSync as readFileSync31, readdirSync as readdirSync9, statSync as statSync6, writeFileSync as writeFileSync20 } from "node:fs";
43359
43882
  import { join as join29 } from "node:path";
43360
43883
  function findCanonicalDoc(rootDir) {
43361
43884
  const docsDir = join29(rootDir, "docs");
@@ -43386,7 +43909,7 @@ function firstString2(v) {
43386
43909
  }
43387
43910
  function readFileSyncSafe(path63) {
43388
43911
  try {
43389
- return readFileSync30(path63, "utf8");
43912
+ return readFileSync31(path63, "utf8");
43390
43913
  } catch {
43391
43914
  return null;
43392
43915
  }
@@ -43401,7 +43924,7 @@ async function runPlanDriftCheck(rootDir) {
43401
43924
  }
43402
43925
  let plan;
43403
43926
  try {
43404
- plan = JSON.parse(readFileSync30(planPath, "utf8"));
43927
+ plan = JSON.parse(readFileSync31(planPath, "utf8"));
43405
43928
  } catch {
43406
43929
  return { ran: false, reason: ".zelari/plan.json corrupt" };
43407
43930
  }
@@ -43527,7 +44050,7 @@ var init_planDriftCheck = __esm({
43527
44050
 
43528
44051
  // src/cli/workspace/projectSmoke.ts
43529
44052
  import { spawn as spawn14 } from "node:child_process";
43530
- import { existsSync as existsSync37, readFileSync as readFileSync31 } from "node:fs";
44053
+ import { existsSync as existsSync37, readFileSync as readFileSync32 } from "node:fs";
43531
44054
  import { join as join30 } from "node:path";
43532
44055
  function pickSmokeScript(scripts) {
43533
44056
  if (!scripts) return null;
@@ -43546,7 +44069,7 @@ async function runProjectSmoke(projectRoot, timeoutMs = DEFAULT_TIMEOUT_MS3) {
43546
44069
  }
43547
44070
  let scripts = {};
43548
44071
  try {
43549
- const pkg = JSON.parse(readFileSync31(pkgPath, "utf8"));
44072
+ const pkg = JSON.parse(readFileSync32(pkgPath, "utf8"));
43550
44073
  scripts = pkg.scripts ?? {};
43551
44074
  } catch {
43552
44075
  return { ran: false, reason: "package.json unreadable (skipped)" };
@@ -43634,7 +44157,7 @@ __export(postCouncilHook_exports, {
43634
44157
  runPostCouncilHook: () => runPostCouncilHook
43635
44158
  });
43636
44159
  import { spawn as spawn15 } from "node:child_process";
43637
- import { existsSync as existsSync38, readFileSync as readFileSync32 } from "node:fs";
44160
+ import { existsSync as existsSync38, readFileSync as readFileSync33 } from "node:fs";
43638
44161
  import { join as join31 } from "node:path";
43639
44162
  async function runCompleteDesignPostProcessor(ctx, options) {
43640
44163
  if (options?.runMode === "implementation") {
@@ -43656,7 +44179,7 @@ async function runCompleteDesignPostProcessor(ctx, options) {
43656
44179
  }
43657
44180
  let phaseCount = 0;
43658
44181
  try {
43659
- const parsed = JSON.parse(readFileSync32(planJsonPath2, "utf8"));
44182
+ const parsed = JSON.parse(readFileSync33(planJsonPath2, "utf8"));
43660
44183
  phaseCount = Array.isArray(parsed.phases) ? parsed.phases.length : 0;
43661
44184
  } catch {
43662
44185
  return { ran: false, reason: ".zelari/plan.json corrupt" };
@@ -43891,7 +44414,7 @@ __export(councilFeedback_exports, {
43891
44414
  import {
43892
44415
  promises as fs22,
43893
44416
  existsSync as existsSync39,
43894
- readFileSync as readFileSync33,
44417
+ readFileSync as readFileSync34,
43895
44418
  writeFileSync as writeFileSync21,
43896
44419
  mkdirSync as mkdirSync17
43897
44420
  } from "node:fs";
@@ -44001,7 +44524,7 @@ var init_councilFeedback = __esm({
44001
44524
  load() {
44002
44525
  if (!existsSync39(this.file)) return;
44003
44526
  try {
44004
- const raw = readFileSync33(this.file, "utf-8");
44527
+ const raw = readFileSync34(this.file, "utf-8");
44005
44528
  const parsed = JSON.parse(raw);
44006
44529
  if (parsed && Array.isArray(parsed.entries)) {
44007
44530
  this.entries = parsed.entries.filter(
@@ -45375,38 +45898,6 @@ var init_brokerHandlers = __esm({
45375
45898
  }
45376
45899
  });
45377
45900
 
45378
- // src/cli/mode.ts
45379
- function nextMode(current) {
45380
- const i = MODES.indexOf(current);
45381
- return MODES[(i + 1) % MODES.length] ?? "kraken";
45382
- }
45383
- function parseMode(input) {
45384
- const v = input.trim().toLowerCase();
45385
- if (MODES.includes(v)) return v;
45386
- return MODE_ALIASES[v] ?? null;
45387
- }
45388
- function describeMode(mode) {
45389
- switch (mode) {
45390
- case "council":
45391
- return "council \u2014 multi-member plan/design (Caronte\u2026Lucifero; build needs ZELARI_COUNCIL_CAN_BUILD=1)";
45392
- case "zelari":
45393
- return "zelari \u2014 mission: plan@council \u2192 build@kraken (legacy: ZELARI_BUILD_VIA_AGENT=0)";
45394
- default:
45395
- return "kraken \u2014 super-agent lead (spawns explore/general/verify tentacles; default implementer)";
45396
- }
45397
- }
45398
- var MODES, MODE_ALIASES;
45399
- var init_mode = __esm({
45400
- "src/cli/mode.ts"() {
45401
- "use strict";
45402
- MODES = ["kraken", "council", "zelari"];
45403
- MODE_ALIASES = {
45404
- agent: "kraken",
45405
- single: "kraken"
45406
- };
45407
- }
45408
- });
45409
-
45410
45901
  // src/cli/kraken/planner.ts
45411
45902
  var planner_exports = {};
45412
45903
  __export(planner_exports, {
@@ -47878,7 +48369,7 @@ var init_prereqChecks = __esm({
47878
48369
  });
47879
48370
 
47880
48371
  // src/cli/plugins/prefs.ts
47881
- import { existsSync as existsSync42, readFileSync as readFileSync34, writeFileSync as writeFileSync22, mkdirSync as mkdirSync18 } from "node:fs";
48372
+ import { existsSync as existsSync42, readFileSync as readFileSync35, writeFileSync as writeFileSync22, mkdirSync as mkdirSync18 } from "node:fs";
47882
48373
  import path53 from "node:path";
47883
48374
  import os11 from "node:os";
47884
48375
  function getPluginPrefsPath() {
@@ -47888,7 +48379,7 @@ function getPluginPrefs() {
47888
48379
  const file2 = getPluginPrefsPath();
47889
48380
  try {
47890
48381
  if (!existsSync42(file2)) return { ...DEFAULTS2, dontAskAgain: {} };
47891
- const raw = readFileSync34(file2, "utf-8");
48382
+ const raw = readFileSync35(file2, "utf-8");
47892
48383
  const parsed = JSON.parse(raw);
47893
48384
  if (parsed && typeof parsed === "object" && parsed.dontAskAgain && typeof parsed.dontAskAgain === "object") {
47894
48385
  const clean = {};
@@ -48690,7 +49181,7 @@ __export(atMentions_exports, {
48690
49181
  extractAtMentions: () => extractAtMentions,
48691
49182
  hasAtMentions: () => hasAtMentions
48692
49183
  });
48693
- import { existsSync as existsSync46, readFileSync as readFileSync36, statSync as statSync9 } from "node:fs";
49184
+ import { existsSync as existsSync46, readFileSync as readFileSync37, statSync as statSync9 } from "node:fs";
48694
49185
  import { basename as basename3, isAbsolute as isAbsolute2, relative as relative3, resolve, sep } from "node:path";
48695
49186
  function isImagePath(abs) {
48696
49187
  const ext = abs.split(".").pop()?.toLowerCase() ?? "";
@@ -48796,7 +49287,7 @@ function resolveMention(token, cwd) {
48796
49287
  note: `image too large (${Math.round(st.size / 1024)} KB) \u2014 path only`
48797
49288
  };
48798
49289
  }
48799
- const dataBase64 = readFileSync36(abs).toString("base64");
49290
+ const dataBase64 = readFileSync37(abs).toString("base64");
48800
49291
  return {
48801
49292
  raw: token,
48802
49293
  path: rel2,
@@ -48807,7 +49298,7 @@ function resolveMention(token, cwd) {
48807
49298
  };
48808
49299
  }
48809
49300
  try {
48810
- const buf = readFileSync36(abs);
49301
+ const buf = readFileSync37(abs);
48811
49302
  const head = buf.subarray(0, 800).toString("utf8");
48812
49303
  if (!isProbablyText(abs, head)) {
48813
49304
  return {
@@ -48897,388 +49388,6 @@ var init_atMentions = __esm({
48897
49388
  }
48898
49389
  });
48899
49390
 
48900
- // src/cli/headless.ts
48901
- import { readFileSync as readFileSync37 } from "node:fs";
48902
- function defaultProfileForMode(mode) {
48903
- switch (mode) {
48904
- case "council":
48905
- return "council/v1";
48906
- case "zelari":
48907
- return "mission/v1";
48908
- default:
48909
- return "kraken/v1";
48910
- }
48911
- }
48912
- function parseHeadlessFlags(argv) {
48913
- if (!argv.includes("--headless")) {
48914
- return { options: null };
48915
- }
48916
- let task;
48917
- let output = "json";
48918
- let mode = "kraken";
48919
- let phase2 = "build";
48920
- let modeExplicit = false;
48921
- let councilFlag = false;
48922
- let provider;
48923
- let model;
48924
- let history2;
48925
- let todos2;
48926
- let once = false;
48927
- let profile;
48928
- let resumeSessionId;
48929
- let exportSessionPath;
48930
- let strictDone = false;
48931
- let krakenGraph;
48932
- let planOnly = process.env.ZELARI_KRAKEN_PLAN_ONLY === "1" || process.env.ZELARI_KRAKEN_PLAN_ONLY === "true";
48933
- let runPlan = process.env.ZELARI_KRAKEN_RUN_PLAN;
48934
- for (let i = 0; i < argv.length; i++) {
48935
- const arg = argv[i];
48936
- if (arg === "--headless") continue;
48937
- if (arg === "--output") {
48938
- const next = argv[i + 1];
48939
- if (next === "json" || next === "plain") {
48940
- output = next;
48941
- i++;
48942
- } else {
48943
- return {
48944
- options: null,
48945
- error: `--output requires 'json' or 'plain', got '${next ?? "(missing)"}'`
48946
- };
48947
- }
48948
- } else if (arg === "--task") {
48949
- task = argv[i + 1];
48950
- i++;
48951
- } else if (arg === "--task-file") {
48952
- const next = argv[i + 1];
48953
- if (next) {
48954
- try {
48955
- const fromFile = readFileSync37(next, "utf-8");
48956
- if (fromFile.trim()) task = fromFile;
48957
- } catch {
48958
- }
48959
- }
48960
- i++;
48961
- } else if (arg === "--council") {
48962
- councilFlag = true;
48963
- } else if (arg === "--mode") {
48964
- const next = argv[i + 1];
48965
- const parsed = next ? parseMode(next) : null;
48966
- if (!parsed) {
48967
- return {
48968
- options: null,
48969
- error: `--mode requires 'kraken', 'council', or 'zelari' (agent=alias), got '${next ?? "(missing)"}'`
48970
- };
48971
- }
48972
- mode = parsed;
48973
- modeExplicit = true;
48974
- i++;
48975
- } else if (arg === "--phase") {
48976
- const next = argv[i + 1];
48977
- const parsed = next ? parsePhase(next) : null;
48978
- if (!parsed) {
48979
- return {
48980
- options: null,
48981
- error: `--phase requires 'plan' or 'build', got '${next ?? "(missing)"}'`
48982
- };
48983
- }
48984
- phase2 = parsed;
48985
- i++;
48986
- } else if (arg === "--provider") {
48987
- provider = argv[i + 1];
48988
- i++;
48989
- } else if (arg === "--model") {
48990
- model = argv[i + 1];
48991
- i++;
48992
- } else if (arg === "--history" || arg === "--history-file") {
48993
- const next = argv[i + 1];
48994
- if (next) {
48995
- let raw = null;
48996
- if (arg === "--history-file") {
48997
- try {
48998
- raw = readFileSync37(next, "utf-8");
48999
- } catch {
49000
- raw = null;
49001
- }
49002
- } else {
49003
- raw = next;
49004
- }
49005
- if (raw) {
49006
- try {
49007
- const parsedHist = JSON.parse(raw);
49008
- if (Array.isArray(parsedHist)) {
49009
- history2 = parsedHist.filter(
49010
- (m) => !!m && typeof m === "object" && typeof m.role === "string"
49011
- ).map((m) => {
49012
- const role = String(m.role);
49013
- const raw2 = m.content;
49014
- const content = typeof raw2 === "string" ? raw2 : raw2 == null ? "" : typeof raw2 === "object" ? JSON.stringify(raw2) : String(raw2);
49015
- const msg = {
49016
- role,
49017
- content
49018
- };
49019
- if (typeof m.toolCallId === "string") {
49020
- msg.toolCallId = m.toolCallId;
49021
- }
49022
- return msg;
49023
- }).filter(
49024
- (m) => m.role === "user" || m.role === "assistant" || m.role === "tool" || m.role === "system"
49025
- );
49026
- }
49027
- } catch {
49028
- }
49029
- }
49030
- i++;
49031
- }
49032
- } else if (arg === "--todos") {
49033
- const next = argv[i + 1];
49034
- if (next) {
49035
- try {
49036
- const parsed = JSON.parse(next);
49037
- if (Array.isArray(parsed)) {
49038
- todos2 = parsed.filter(
49039
- (t) => !!t && typeof t === "object" && typeof t.content === "string"
49040
- ).map((t) => ({
49041
- id: typeof t.id === "string" ? t.id : void 0,
49042
- content: String(t.content).slice(0, 500),
49043
- status: t.status
49044
- }));
49045
- }
49046
- } catch {
49047
- }
49048
- i++;
49049
- }
49050
- } else if (arg === "--once") {
49051
- once = true;
49052
- } else if (arg === "--profile") {
49053
- const next = argv[i + 1];
49054
- if (!next || next.startsWith("--")) {
49055
- return { options: null, error: `--profile requires a profile id (e.g. kraken/v1), got '${next ?? "(missing)"}'` };
49056
- }
49057
- try {
49058
- resolveProfile(next);
49059
- } catch (err) {
49060
- return {
49061
- options: null,
49062
- error: err instanceof Error ? err.message : String(err)
49063
- };
49064
- }
49065
- profile = next;
49066
- i++;
49067
- } else if (arg === "--resume") {
49068
- const next = argv[i + 1];
49069
- if (!next || next.startsWith("--")) {
49070
- return { options: null, error: `--resume requires a session id, got '${next ?? "(missing)"}'` };
49071
- }
49072
- resumeSessionId = next;
49073
- i++;
49074
- } else if (arg === "--export-session") {
49075
- const next = argv[i + 1];
49076
- if (!next || next.startsWith("--")) {
49077
- return { options: null, error: `--export-session requires a path (or - for stdout), got '${next ?? "(missing)"}'` };
49078
- }
49079
- exportSessionPath = next;
49080
- i++;
49081
- } else if (arg === "--strict-done") {
49082
- strictDone = true;
49083
- } else if (arg === "--kraken-graph") {
49084
- krakenGraph = argv[i + 1];
49085
- i++;
49086
- } else if (arg === "--kraken-graph-file") {
49087
- const next = argv[i + 1];
49088
- if (next) {
49089
- try {
49090
- const fromFile = readFileSync37(next, "utf-8");
49091
- if (fromFile.trim()) krakenGraph = fromFile;
49092
- } catch {
49093
- }
49094
- }
49095
- i++;
49096
- } else if (arg === "--plan-only") {
49097
- planOnly = true;
49098
- } else if (arg === "--run-plan") {
49099
- runPlan = argv[i + 1];
49100
- i++;
49101
- }
49102
- }
49103
- if (councilFlag && !modeExplicit) {
49104
- mode = "council";
49105
- } else if (councilFlag && modeExplicit && mode !== "council") {
49106
- return {
49107
- options: null,
49108
- error: `--council conflicts with --mode ${mode}`
49109
- };
49110
- }
49111
- if (task && krakenGraph) {
49112
- return { options: null, error: "--task and --kraken-graph are mutually exclusive" };
49113
- }
49114
- if ((!task || task.trim().length === 0) && (!krakenGraph || krakenGraph.trim().length === 0)) {
49115
- return { options: null, error: "--headless requires --task <prompt> or --kraken-graph <goal>" };
49116
- }
49117
- return {
49118
- options: {
49119
- task: task ?? "",
49120
- output,
49121
- mode,
49122
- phase: phase2,
49123
- useCouncil: mode === "council",
49124
- provider,
49125
- model,
49126
- ...history2 && history2.length > 0 ? { history: history2 } : {},
49127
- ...todos2 && todos2.length > 0 ? { todos: todos2 } : {},
49128
- ...once ? { once: true } : {},
49129
- ...profile ? { profile } : {},
49130
- ...resumeSessionId ? { resumeSessionId } : {},
49131
- ...exportSessionPath ? { exportSessionPath } : {},
49132
- ...strictDone ? { strictDone: true } : {},
49133
- ...krakenGraph ? { krakenGraph } : {},
49134
- ...planOnly ? { planOnly: true } : {},
49135
- ...runPlan ? { runPlan } : {}
49136
- }
49137
- };
49138
- }
49139
- async function resolveHeadlessKey(providerId) {
49140
- const spec = PROVIDERS.find((p3) => p3.id === providerId);
49141
- if (!spec) {
49142
- return { error: `unknown provider: '${providerId}'` };
49143
- }
49144
- const resolved = await resolveApiKeyWithMeta(providerId);
49145
- if (!resolved || !resolved.apiKey) {
49146
- return {
49147
- error: `no API key for provider '${providerId}'.
49148
- Set the env var ${spec.envVar} or save a key via /login.`
49149
- };
49150
- }
49151
- const { resolveBaseUrl: resolveBaseUrl2 } = await Promise.resolve().then(() => (init_openai_compatible(), openai_compatible_exports));
49152
- return {
49153
- apiKey: resolved.apiKey,
49154
- baseUrl: resolveBaseUrl2(providerId)
49155
- };
49156
- }
49157
- function resolveHeadlessProvider(opts) {
49158
- const provider = opts.provider ?? getActiveProvider().id;
49159
- const model = opts.model ?? getModelForProvider(provider);
49160
- return { provider, model };
49161
- }
49162
- function emitEvent(event) {
49163
- process.stdout.write(JSON.stringify(event) + "\n");
49164
- }
49165
- var init_headless = __esm({
49166
- "src/cli/headless.ts"() {
49167
- "use strict";
49168
- init_keyStore();
49169
- init_providerConfig();
49170
- init_openai_compatible();
49171
- init_phase();
49172
- init_mode();
49173
- init_runtime2();
49174
- }
49175
- });
49176
-
49177
- // src/cli/headlessSpine.ts
49178
- var headlessSpine_exports = {};
49179
- __export(headlessSpine_exports, {
49180
- exportSessionById: () => exportSessionById,
49181
- missionStateFromSpine: () => missionStateFromSpine,
49182
- openHeadlessSpine: () => openHeadlessSpine,
49183
- resolveHeadlessProfileId: () => resolveHeadlessProfileId
49184
- });
49185
- function resolveHeadlessProfileId(mode, explicit) {
49186
- if (explicit) return resolveProfile(explicit).id;
49187
- return defaultProfileForMode(mode ?? "kraken");
49188
- }
49189
- async function openHeadlessSpine(opts) {
49190
- const profileId = resolveHeadlessProfileId(opts.mode, opts.profile);
49191
- let profileTools = [];
49192
- try {
49193
- profileTools = resolveProfile(profileId).tools;
49194
- } catch {
49195
- profileTools = [];
49196
- }
49197
- const extra = {
49198
- profile: profileId,
49199
- workspace: opts.workspace ?? process.cwd(),
49200
- toolManifestHash: profileTools.length > 0 ? toolManifestHash(profileTools) : void 0
49201
- };
49202
- const mirrorOpts = {
49203
- baseDir: opts.baseDir,
49204
- quiet: opts.quiet,
49205
- extraStarted: extra
49206
- };
49207
- const spine = await SessionSpineMirror.adopt(opts.sessionId, mirrorOpts);
49208
- if (spine.status === "active") {
49209
- spine.note("headless.profile", { profile: profileId, mode: opts.mode ?? "kraken" });
49210
- }
49211
- return {
49212
- sessionId: opts.sessionId,
49213
- profileId,
49214
- spine,
49215
- observe(ev) {
49216
- if (ev && typeof ev === "object" && "type" in ev) {
49217
- spine.mirrorBrainEvent(ev);
49218
- }
49219
- },
49220
- userMessage(text) {
49221
- spine.userMessage(text);
49222
- },
49223
- verificationRun(payload) {
49224
- spine.verificationRun(payload);
49225
- },
49226
- missionPhase(phase2, note) {
49227
- spine.missionPhase(phase2, note);
49228
- },
49229
- note(text, data) {
49230
- spine.note(text, data);
49231
- },
49232
- async close(reason = "host-exit") {
49233
- await spine.close(reason);
49234
- },
49235
- async interrupt(note) {
49236
- if (note) spine.note("headless.interrupt", { note });
49237
- await spine.release();
49238
- },
49239
- async exportJson() {
49240
- try {
49241
- const store6 = new SessionStore(resolveSessionsDir({ baseDir: opts.baseDir }));
49242
- if (!await store6.exists(opts.sessionId)) return null;
49243
- return await exportSessionJson(store6, opts.sessionId);
49244
- } catch {
49245
- return null;
49246
- }
49247
- }
49248
- };
49249
- }
49250
- async function exportSessionById(sessionId2, baseDir) {
49251
- try {
49252
- const store6 = SessionStore.withDefaults(baseDir ? { baseDir } : {});
49253
- if (!await store6.exists(sessionId2)) {
49254
- return { ok: false, error: `session not found: ${sessionId2}` };
49255
- }
49256
- return { ok: true, json: await exportSessionJson(store6, sessionId2) };
49257
- } catch (err) {
49258
- return { ok: false, error: err instanceof Error ? err.message : String(err) };
49259
- }
49260
- }
49261
- async function missionStateFromSpine(sessionId2, baseDir) {
49262
- try {
49263
- const store6 = SessionStore.withDefaults(baseDir ? { baseDir } : {});
49264
- if (!await store6.exists(sessionId2)) return null;
49265
- const projection = await store6.projection(sessionId2);
49266
- return deriveMissionState(projection);
49267
- } catch {
49268
- return null;
49269
- }
49270
- }
49271
- var init_headlessSpine = __esm({
49272
- "src/cli/headlessSpine.ts"() {
49273
- "use strict";
49274
- init_session();
49275
- init_mission2();
49276
- init_runtime2();
49277
- init_sessionSpine();
49278
- init_headless();
49279
- }
49280
- });
49281
-
49282
49391
  // src/cli/triggerLock.ts
49283
49392
  var triggerLock_exports = {};
49284
49393
  __export(triggerLock_exports, {
@@ -55015,6 +55124,9 @@ function strictGateEventPayload(evaluation) {
55015
55124
  };
55016
55125
  }
55017
55126
 
55127
+ // src/cli/hooks/useChatTurn.ts
55128
+ init_headlessSpine();
55129
+
55018
55130
  // src/cli/hooks/permissionPicker.ts
55019
55131
  init_toolPermissions();
55020
55132
 
@@ -55378,6 +55490,18 @@ function useChatTurn(params) {
55378
55490
  try {
55379
55491
  const anchored = maybeAnchorShortAnswer(userText);
55380
55492
  const effectiveUserText = anchored ?? userText;
55493
+ let historyForModel;
55494
+ {
55495
+ const mirror = writerRef.current?.spine ?? null;
55496
+ let spineSeed = null;
55497
+ if (mirror && mirror.status === "active") {
55498
+ const derived = await mirror.derivedPriorTurns();
55499
+ if (derived && derived.length > 0) {
55500
+ spineSeed = derivedModelSeed(derived);
55501
+ }
55502
+ }
55503
+ historyForModel = spineSeed ?? getHistory();
55504
+ }
55381
55505
  writerRef.current?.spine?.userMessage(effectiveUserText);
55382
55506
  const localCli = (process.env.ZELARI_LOCAL_CLI ?? "").trim();
55383
55507
  let localCliProvider = null;
@@ -55533,7 +55657,7 @@ _(${req.context})_` : "") + "\n\u2192 scegli dalla lista (il turno continua dopo
55533
55657
  });
55534
55658
  void writerRef.current?.append(compactionEvent);
55535
55659
  }
55536
- historySeedLen = getHistory().length;
55660
+ historySeedLen = historyForModel.length;
55537
55661
  let composedWorkspace = "";
55538
55662
  let composedInstructions = "";
55539
55663
  let hasPlan = false;
@@ -55716,7 +55840,7 @@ _(${req.context})_` : "") + "\n\u2192 scegli dalla lista (il turno continua dopo
55716
55840
  // v1.8.0: shared rolling history (agent/council/zelari) so short
55717
55841
  // answers bind to prior ---QUESTION--- blocks. Possibly empty
55718
55842
  // when ZELARI_HISTORY_TURNS=0.
55719
- ...getHistory(),
55843
+ ...historyForModel,
55720
55844
  { role: "user", content: effectiveUserText }
55721
55845
  ],
55722
55846
  tools: toolRegistry.toOpenAITools().map((t) => ({
@@ -58838,12 +58962,13 @@ async function handleUpdateCheck(ctx) {
58838
58962
  }
58839
58963
  }
58840
58964
  async function handleUpdatePerform(ctx) {
58841
- appendSystem(
58842
- ctx.setMessages,
58843
- "[update] running `npm install -g zelari-code@latest`..."
58844
- );
58845
58965
  try {
58846
- const { performUpdate: performUpdate2 } = await Promise.resolve().then(() => (init_updater(), updater_exports));
58966
+ const { performUpdate: performUpdate2, distTagForVersion: distTagForVersion2, getCurrentVersion: getCurrentVersion2 } = await Promise.resolve().then(() => (init_updater(), updater_exports));
58967
+ const tag = distTagForVersion2(getCurrentVersion2());
58968
+ appendSystem(
58969
+ ctx.setMessages,
58970
+ `[update] running \`npm install -g zelari-code@${tag}\`...`
58971
+ );
58847
58972
  const res = await performUpdate2();
58848
58973
  if (res.ok) {
58849
58974
  let prereqBlock = "";
@@ -59089,7 +59214,7 @@ async function handlePromoteMember(ctx, memberId) {
59089
59214
  }
59090
59215
 
59091
59216
  // src/cli/branchManager.ts
59092
- import { promises as fs32, existsSync as existsSync44, readFileSync as readFileSync35, writeFileSync as writeFileSync23, mkdirSync as mkdirSync19, statSync as statSync7, rmSync as rmSync3 } from "node:fs";
59217
+ import { promises as fs32, existsSync as existsSync44, readFileSync as readFileSync36, writeFileSync as writeFileSync23, mkdirSync as mkdirSync19, statSync as statSync7, rmSync as rmSync3 } from "node:fs";
59093
59218
  import path56 from "node:path";
59094
59219
  import os13 from "node:os";
59095
59220
  var META_FILENAME = "meta.json";
@@ -59115,7 +59240,7 @@ function readBranchMeta(name, baseDir) {
59115
59240
  throw new BranchNotFoundError(`Branch "${name}" not found`);
59116
59241
  }
59117
59242
  try {
59118
- const raw = readFileSync35(metaPath, "utf-8");
59243
+ const raw = readFileSync36(metaPath, "utf-8");
59119
59244
  const parsed = JSON.parse(raw);
59120
59245
  if (!parsed || typeof parsed !== "object" || typeof parsed.name !== "string" || typeof parsed.createdAt !== "number" || typeof parsed.fromSessionId !== "string") {
59121
59246
  throw new BranchCorruptError(`Branch "${name}" meta.json is malformed`);
@@ -61424,6 +61549,7 @@ async function runHeadlessSingle(opts, provider, model, providerStream) {
61424
61549
  profile: opts.profile,
61425
61550
  workspace: process.cwd()
61426
61551
  });
61552
+ const seededHistory = await seedHeadlessModelHistory(spine, opts.history);
61427
61553
  if (opts.task) spine.userMessage(opts.task);
61428
61554
  resetKrakenCandidates();
61429
61555
  resetKrakenTurnMetrics();
@@ -61573,15 +61699,7 @@ async function runHeadlessSingle(opts, provider, model, providerStream) {
61573
61699
  }
61574
61700
  ];
61575
61701
  }
61576
- const historySeed = (opts.history ?? []).filter((m) => m.role === "user" || m.role === "assistant").map(
61577
- (m) => m.role === "assistant" && m.content ? {
61578
- role: "assistant",
61579
- content: cleanAgentContent(m.content, {
61580
- stripQuestion: false,
61581
- stripThink: false
61582
- })
61583
- } : { role: m.role, content: m.content ?? "" }
61584
- ).filter((m) => (m.content ?? "").trim().length > 0);
61702
+ const historySeed = seededHistory.history;
61585
61703
  const effectiveTask = buildAgentUserWithHistory(opts.task, historySeed);
61586
61704
  const maxToolLoop = (() => {
61587
61705
  const n = envNumber(process.env.ZELARI_MAX_TOOL_LOOP_ITERATIONS, {
@@ -61888,6 +62006,7 @@ async function runHeadlessCouncil(opts, provider, model, providerStream) {
61888
62006
  profile: opts.profile,
61889
62007
  workspace: process.cwd()
61890
62008
  });
62009
+ const seededHistory = await seedHeadlessModelHistory(spine, opts.history);
61891
62010
  if (opts.task) spine.userMessage(opts.task);
61892
62011
  const { shouldAllowCouncilBuild: shouldAllowCouncilBuild2 } = await Promise.resolve().then(() => (init_buildPolicy(), buildPolicy_exports));
61893
62012
  let councilRunMode = planModeFromOpts(opts) ? "design-phase" : "implementation";
@@ -61905,15 +62024,7 @@ async function runHeadlessCouncil(opts, provider, model, providerStream) {
61905
62024
  );
61906
62025
  const { FeedbackStore: FeedbackStore2 } = await Promise.resolve().then(() => (init_councilFeedback(), councilFeedback_exports));
61907
62026
  const feedbackStore = new FeedbackStore2();
61908
- const historySeed = (opts.history ?? []).map(
61909
- (m) => m.role === "assistant" && m.content ? {
61910
- ...m,
61911
- content: cleanAgentContent(m.content, {
61912
- stripQuestion: false,
61913
- stripThink: false
61914
- })
61915
- } : m
61916
- );
62027
+ const historySeed = seededHistory.history;
61917
62028
  const effectiveTask = buildCouncilTaskWithHistory(opts.task, historySeed);
61918
62029
  let exitCode = 0;
61919
62030
  const scrub = createStreamScrubber2();
@@ -62032,6 +62143,7 @@ async function runHeadlessZelari(opts, provider, model, providerStream) {
62032
62143
  profile: opts.profile ?? "mission/v1",
62033
62144
  workspace: projectRoot
62034
62145
  });
62146
+ const seededHistory = await seedHeadlessModelHistory(spine, opts.history);
62035
62147
  if (opts.task) spine.userMessage(opts.task);
62036
62148
  spine.missionPhase("design", "mission-start");
62037
62149
  const { buildMissionBrief: buildMissionBrief2 } = await Promise.resolve().then(() => (init_council(), council_exports));
@@ -62064,15 +62176,7 @@ async function runHeadlessZelari(opts, provider, model, providerStream) {
62064
62176
  process.stderr.write(message + "\n");
62065
62177
  }
62066
62178
  };
62067
- const historySeed = (opts.history ?? []).map(
62068
- (m) => m.role === "assistant" && m.content ? {
62069
- ...m,
62070
- content: cleanAgentContent(m.content, {
62071
- stripQuestion: false,
62072
- stripThink: false
62073
- })
62074
- } : m
62075
- );
62179
+ const historySeed = seededHistory.history;
62076
62180
  const missionTask = buildCouncilTaskWithHistory(opts.task, historySeed);
62077
62181
  emit(`[zelari] mission brief
62078
62182
  ${JSON.stringify({ deliverable: brief.deliverableThisMission, mvp: brief.sliceMvp?.title }, null, 0)}`);