@wirecat/cli-core 0.598.0 → 0.600.0

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.
package/CHANGELOG.md CHANGED
@@ -7,6 +7,47 @@ shape of this file.
7
7
 
8
8
  ## Unreleased
9
9
 
10
+ ## 0.600.0 — 07.11.2026
11
+
12
+ ### Changed — may break callers
13
+
14
+ - **`schedule agent` and `schedule command` take `--deliver-to <route>`, no longer `--to <route>`.** `--to` names who a
15
+ message is for in every CLI (mail's `<address>`, a messenger's `<chat>`), so mail's command lint refused the route
16
+ flag. `schedule list` and the answer of `schedule agent|command` name the routes `deliver_to`. The schedule file
17
+ still keeps them as `to`, so a schedule saved with `--to` goes on delivering; `deliveries[].to` in `last.json` is
18
+ unchanged.
19
+ - **The shared commands pass `cli-dev lint` with no overrides.** `lint/overrides.json` is gone: an override here never
20
+ reached a CLI that mounts the commands, so each CLI failed on its bump or copied it. Needs cli-base 0.80.7, whose
21
+ vocabulary has `--notify-on`, `--deliver-to`, `--max-cost-per-day`, `--who`, `--open-new`, `waiting` and `recover`. A CLI that
22
+ copied those overrides removes them when it bumps (`cli-dev lint` reports them as stale). `stats tasks show
23
+ --chat` moved to the baseline.
24
+
25
+ ## 0.599.0 — 07.11.2026
26
+
27
+ ### Added
28
+
29
+ - **`store recover`** tells whether the store file is damaged: it reads every page and every search index, names the
30
+ damaged tables, and answers in plain words what happened, likely causes, how to look further, what is affected, that
31
+ the messengers', mail servers' and Drive's own data is not, and the commands to run. It changes nothing.
32
+ `storeRecoveryCheck` gives the same to a CLI with its own `store` command.
33
+ - **`store recover run`** rebuilds a damaged store: it copies every readable row into a new file with the `sqlite3`
34
+ program (3.43 or newer; node:sqlite has no recovery API), rebuilds every search index, the mentions and the vector
35
+ index from the data, checks the result, and puts it in place of the store, keeping the damaged file beside it. It
36
+ refuses while a WireCat timer is scheduled or a process has the store open, and names them. The answer lists the
37
+ tables that lost rows, the rows that point at lost ones, and the mail sync that fetches lost mail again.
38
+ `storeRecoveryRun` and `storeRecoverCommand` give it to other CLIs.
39
+ - **`doctor` checks the store's health**: the file header, its size and the schema's own pages, in milliseconds. A
40
+ damaged store is explained in the answer's `store.damage` (`explainDamage`: what, causes, investigate, impact,
41
+ external_unaffected, steps) and points at `store recover`, the whole-file check. `storeSummary` takes
42
+ `{ deep, command }`, and `DOCTOR_DEEP_OPTION` words a CLI's own `--deep`.
43
+
44
+ ### Fixed
45
+
46
+ - **Opening the store a second time in one process no longer drops the locks of the connection already open.** The
47
+ open touched the existing file through its own descriptor, and on Linux closing any descriptor of a file releases
48
+ every lock the process holds on it, so another process could checkpoint and delete the write-ahead log under a
49
+ writer that went on writing into it. That can damage the store. The file is now created only when it is missing.
50
+
10
51
  ## 0.598.0 — 07.11.2026
11
52
 
12
53
  ### Added
@@ -16,7 +16,7 @@ const ADDRESS = /^[^\s@/,<>"]+@[^\s@/,<>"]+\.[^\s@/,<>"]+$/;
16
16
  export const ROUTE_HELP = "telegram-bot:<profile>/<chat id or user:id>, or email:<profile>/<address>";
17
17
  export const parseRoute = (text) => {
18
18
  const [, kind = "", profile = "", target = ""] = /^([a-z-]+):([^/]+)\/(.+)$/.exec(text.trim()) ?? [];
19
- const refuse = (why) => new CliError("validation_error", `--to ${text}: ${why} — ${ROUTE_HELP}`);
19
+ const refuse = (why) => new CliError("validation_error", `--deliver-to ${text}: ${why} — ${ROUTE_HELP}`);
20
20
  if (!(kind in PROGRAMS))
21
21
  throw refuse(kind ? `${kind} is not a route` : "a route is <kind>:<profile>/<where>");
22
22
  if (!PROFILE.test(profile))
@@ -27,7 +27,7 @@ export interface Schedule {
27
27
  maxCostPerDay?: number;
28
28
  /** When a finished run shows a desktop notice and goes along `to`; absent means `notifyOf`'s default. */
29
29
  notify?: Notify;
30
- /** Delivery routes (`telegram-bot:<profile>/<chat>`, `email:<profile>/<address>`) the result goes along after the run. */
30
+ /** `--deliver-to`'s routes (`telegram-bot:<profile>/<chat>`, `email:<profile>/<address>`), answered as `deliver_to`. */
31
31
  to?: string[];
32
32
  }
33
33
  export declare const NOTIFY_WHEN: readonly ["always", "failures", "never"];
@@ -99,6 +99,7 @@ export { accountFileFor, profilesHere, rememberAccount } from "./messenger/accou
99
99
  export { terminalAsker } from "./messenger/ask.js";
100
100
  export type { CompletionOptions } from "./messenger/complete-command.js";
101
101
  export { type ConnectOptions, connectFor, type Messenger, type MessengerContext, messengerContext, type ProfileState, type ReadConnection, type StoredMessages, } from "./messenger/context.js";
102
+ export { DOCTOR_DEEP_OPTION, damageNotes } from "./messenger/doctor-command.js";
102
103
  export { COMMAND_NEEDS } from "./messenger/lacks.js";
103
104
  export { httpTokenFile, type McpEnvironment, type McpHost, serverEntry } from "./messenger/mcp-command.js";
104
105
  export { type MessageItem, messageItem, type ReadMessage } from "./messenger/message-items.js";
@@ -131,5 +132,6 @@ export { accountCommand, addMeetingBatchCommand, addMeetingPreparationCommand, a
131
132
  export { assertSharedContracts, customizeSharedCommand, hasSharedContract, overrideSharedContract, type SharedContractOverride, sharedContractOverrides, sharedContracts, } from "./shared-contracts.js";
132
133
  export { runnable, withSafety } from "./steps.js";
133
134
  export { type StoreInfo, sharedStoreCommand, storeInfo, storeInfoCommand } from "./store-info.js";
135
+ export { type RecoveryCheck, type RecoveryRun, recoveryNotes, storeRecoverCommand, storeRecoveryCheck, storeRecoveryRun, } from "./store-recover.js";
134
136
  export { changeStoreSetting, isStoreSetting, STORE_SETTINGS, storeSettings } from "./store-settings.js";
135
137
  export { restartServers, type UpgradeContext } from "./upgrade-command.js";
package/dist/cli/index.js CHANGED
@@ -89,6 +89,7 @@ export { readMeetingSeriesInventory } from "./meeting-series-inventory-read.js";
89
89
  export { accountFileFor, profilesHere, rememberAccount } from "./messenger/accounts.js";
90
90
  export { terminalAsker } from "./messenger/ask.js";
91
91
  export { connectFor, messengerContext, } from "./messenger/context.js";
92
+ export { DOCTOR_DEEP_OPTION, damageNotes } from "./messenger/doctor-command.js";
92
93
  export { COMMAND_NEEDS } from "./messenger/lacks.js";
93
94
  export { httpTokenFile, serverEntry } from "./messenger/mcp-command.js";
94
95
  export { messageItem } from "./messenger/message-items.js";
@@ -120,5 +121,6 @@ export { accountCommand, addMeetingBatchCommand, addMeetingPreparationCommand, a
120
121
  export { assertSharedContracts, customizeSharedCommand, hasSharedContract, overrideSharedContract, sharedContractOverrides, sharedContracts, } from "./shared-contracts.js";
121
122
  export { runnable, withSafety } from "./steps.js";
122
123
  export { sharedStoreCommand, storeInfo, storeInfoCommand } from "./store-info.js";
124
+ export { recoveryNotes, storeRecoverCommand, storeRecoveryCheck, storeRecoveryRun, } from "./store-recover.js";
123
125
  export { changeStoreSetting, isStoreSetting, STORE_SETTINGS, storeSettings } from "./store-settings.js";
124
126
  export { restartServers } from "./upgrade-command.js";
@@ -8,6 +8,9 @@ import { type Messenger } from "./context.js";
8
8
  * open — each is a field in the answer, never an exception.
9
9
  */
10
10
  export declare const doctorCommand: (messenger: Messenger) => Command;
11
+ export declare const DOCTOR_DEEP_OPTION = "also read every page of the store and check every search index (about 30 s for 6 GB); without it, only the file header and schema";
12
+ /** The plain-words account of a damaged store, under the answer. */
13
+ export declare const damageNotes: (store: unknown) => string[];
11
14
  /** What the messenger asked this profile to wait for, still in force — what the next command would be refused. */
12
15
  export declare const floodState: (memory: FloodMemory) => {
13
16
  path: string;
@@ -49,10 +49,24 @@ export const doctorCommand = (messenger) => {
49
49
  renderAnswer(this, { ...output, ...(renderer ? { renderer } : {}) }, itemOf(report, { next: unchecked ? [nextStep("doctor.online", { args: { online: true } })] : [] }));
50
50
  for (const fix of fixes)
51
51
  (renderer ?? output.renderer).note(`to make it private: ${fix}`);
52
+ for (const line of damageNotes(report.store))
53
+ (renderer ?? output.renderer).note(line);
52
54
  });
53
55
  command.addCommand(reportCommand(messenger));
54
56
  return command;
55
57
  };
58
+ export const DOCTOR_DEEP_OPTION = "also read every page of the store and check every search index (about 30 s for 6 GB); without it, only the file header and schema";
59
+ /** The shared doctor stays quick; `store recover` is its deep check, and a CLI may add `--deep` to its own doctor. */
60
+ const summaryOptions = (command, app) => ({
61
+ deep: command.opts().deep === true,
62
+ command: app,
63
+ });
64
+ /** The plain-words account of a damaged store, under the answer. */
65
+ export const damageNotes = (store) => {
66
+ const damage = store
67
+ ?.damage;
68
+ return damage ? [damage.what, damage.external_unaffected, ...damage.steps.map((step) => `next: ${step}`)] : [];
69
+ };
56
70
  const INCLUDES = [
57
71
  "the version, the runtime and the operating system",
58
72
  "what `doctor` answers: paths with the home folder shown as ~, the store's schema and counts, the messenger's own checks",
@@ -149,7 +163,7 @@ const diagnose = async (command, messenger) => {
149
163
  cli,
150
164
  config: { path, found: existsSync(path), issues: [] },
151
165
  settings: { error: messageOf(error) },
152
- store: snakeKeys(await storeSummary(env)),
166
+ store: snakeKeys(await storeSummary(env, summaryOptions(command, app.command))),
153
167
  runs: { directory: runsDirFor(app, env), kept: listRuns(runsDirFor(app, env)).length },
154
168
  files: filesReport(files),
155
169
  },
@@ -174,7 +188,7 @@ const diagnose = async (command, messenger) => {
174
188
  cli,
175
189
  profile: null,
176
190
  config,
177
- store: snakeKeys(await storeSummary(env)),
191
+ store: snakeKeys(await storeSummary(env, summaryOptions(command, app.command))),
178
192
  readers: engineStates(),
179
193
  runs: { directory: runsDirFor(app, env), kept: listRuns(runsDirFor(app, env)).length },
180
194
  files: filesReport(files),
@@ -210,7 +224,7 @@ const diagnose = async (command, messenger) => {
210
224
  account: { remembered: account?.account ?? null },
211
225
  // A session file on disk says nothing about whether the messenger still accepts it.
212
226
  login: { state: "not checked" },
213
- store: snakeKeys(await storeSummary(env)),
227
+ store: snakeKeys(await storeSummary(env, summaryOptions(command, app.command))),
214
228
  readers: engineStates(),
215
229
  sends: sendsState(new SendJournal(sendsPathFor(app, profile, env))),
216
230
  flood: floodState(new FloodMemory(flood)),
@@ -1,12 +1,27 @@
1
1
  import { Command } from "commander";
2
2
  import type { CacheDatabase } from "../../store/driver.js";
3
3
  import type { Messenger } from "./context.js";
4
- /** `doctor`'s short summary of the store. Must answer for a broken file, so an error is a field. */
5
- export declare const storeSummary: (env: NodeJS.ProcessEnv) => Promise<{
4
+ /**
5
+ * `doctor`'s short summary of the store. Must answer for a broken file, so an error is a field. `health` is the quick
6
+ * check unless `deep`; `damage` says in plain words what a failed one means and what to run.
7
+ */
8
+ export declare const storeSummary: (env: NodeJS.ProcessEnv, { deep, command }?: {
9
+ deep?: boolean;
10
+ command?: string;
11
+ }) => Promise<{
6
12
  path: string;
7
13
  exists: boolean;
8
- error?: undefined;
9
14
  } | {
15
+ health: {
16
+ checked: "deep" | "quick";
17
+ covers: string;
18
+ ok: boolean;
19
+ ms: number;
20
+ problems?: number | undefined;
21
+ damagedTables?: import("../../store/health.js").DamagedTable[] | undefined;
22
+ damagedSearchIndexes?: string[] | undefined;
23
+ };
24
+ damage?: import("../../store/damage.js").DamageExplanation | undefined;
10
25
  path: string;
11
26
  exists: boolean;
12
27
  schema: number;
@@ -15,6 +30,16 @@ export declare const storeSummary: (env: NodeJS.ProcessEnv) => Promise<{
15
30
  chats?: number | undefined;
16
31
  messages?: number | undefined;
17
32
  } | {
33
+ health: {
34
+ checked: "deep" | "quick";
35
+ covers: string;
36
+ ok: boolean;
37
+ ms: number;
38
+ problems?: number | undefined;
39
+ damagedTables?: import("../../store/health.js").DamagedTable[] | undefined;
40
+ damagedSearchIndexes?: string[] | undefined;
41
+ };
42
+ damage?: import("../../store/damage.js").DamageExplanation | undefined;
18
43
  path: string;
19
44
  exists: boolean;
20
45
  error: string;
@@ -8,6 +8,8 @@ import { RULES_VERSION } from "../../conversations/link.js";
8
8
  import { syncsOneChat } from "../../ports/traits.js";
9
9
  import { isSealed, sealFile, unsealFile } from "../../sealed.js";
10
10
  import { fileSchemaOf, SPEAKS, writableBy } from "../../store/compatibility.js";
11
+ import { explainDamage } from "../../store/damage.js";
12
+ import { QUICK_HEALTH_CHECKS, storeHealth } from "../../store/health.js";
11
13
  import { migrate } from "../../store/migrations.js";
12
14
  import { openCache } from "../../store/open.js";
13
15
  import { storePath } from "../../store/path.js";
@@ -18,6 +20,7 @@ import { destroysNothing, LOCAL_READ, LOCAL_WRITE } from "../effects.js";
18
20
  import { itemOf, renderAnswer, snakeKeys } from "../envelope.js";
19
21
  import { lineOf, nextStep } from "../steps.js";
20
22
  import { storeInfoCommand } from "../store-info.js";
23
+ import { storeRecoverCommand } from "../store-recover.js";
21
24
  import { answerOf } from "./ask.js";
22
25
  import { ENCRYPT_OPTION, passwordOf } from "./password.js";
23
26
  import { servingProfiles } from "./serve-command.js";
@@ -55,11 +58,28 @@ const readingOnly = async (path, read) => {
55
58
  database.close();
56
59
  }
57
60
  };
58
- /** `doctor`'s short summary of the store. Must answer for a broken file, so an error is a field. */
59
- export const storeSummary = async (env) => {
61
+ /**
62
+ * `doctor`'s short summary of the store. Must answer for a broken file, so an error is a field. `health` is the quick
63
+ * check unless `deep`; `damage` says in plain words what a failed one means and what to run.
64
+ */
65
+ export const storeSummary = async (env, { deep = false, command } = {}) => {
60
66
  const path = storePath(env);
61
67
  if (!existsSync(path))
62
68
  return { path, exists: false };
69
+ const checked = await storeHealth(path, { deep });
70
+ const health = {
71
+ checked: checked.checked,
72
+ covers: deep ? "every page and every search index" : QUICK_HEALTH_CHECKS,
73
+ ok: checked.ok,
74
+ ms: checked.ms,
75
+ ...(checked.problems ? { problems: checked.problems } : {}),
76
+ ...(checked.damagedTables?.length ? { damagedTables: checked.damagedTables } : {}),
77
+ ...(checked.searchIndexes?.some((index) => !index.ok)
78
+ ? { damagedSearchIndexes: checked.searchIndexes.filter((index) => !index.ok).map((index) => index.name) }
79
+ : {}),
80
+ };
81
+ const damage = command ? explainDamage(checked, command) : null;
82
+ const healthOf = { health, ...(damage ? { damage } : {}) };
63
83
  try {
64
84
  return await reading(path, (database) => {
65
85
  const { version, writable } = schemaOf(database);
@@ -70,11 +90,12 @@ export const storeSummary = async (env) => {
70
90
  speaks: SPEAKS,
71
91
  writable,
72
92
  ...(version > 0 ? { chats: count(database, "chats"), messages: count(database, "messages") } : {}),
93
+ ...healthOf,
73
94
  };
74
95
  });
75
96
  }
76
97
  catch (error) {
77
- return { path, exists: true, error: messageOf(error) };
98
+ return { path, exists: true, error: messageOf(error), ...healthOf };
78
99
  }
79
100
  };
80
101
  /** A maintenance answer: one item, snake_case, its hints as steps where a registered one fits. */
@@ -91,6 +112,7 @@ export const storeMaintenanceCommands = (messenger) => [
91
112
  resetCommand(messenger),
92
113
  copiesCommand(),
93
114
  storeTablesCommand(messenger),
115
+ storeRecoverCommand({ command: messenger.app.command, version: messenger.app.version }),
94
116
  ];
95
117
  /** Renaming tables under a running `serve` would pull them from under its statements. */
96
118
  const refuseWhileServing = (command, messenger) => {
@@ -71,8 +71,10 @@ const untilChildEnds = async (command, wait) => {
71
71
  environment.endUntilStopped?.();
72
72
  }
73
73
  };
74
- const scheduleAnswer = ({ environment, createdAt, ...schedule }, extra) => ({
74
+ // The file keeps `to`, as schedules saved before the flag was renamed have it; the answer names it as the flag does.
75
+ const scheduleAnswer = ({ environment, createdAt, to, ...schedule }, extra) => ({
75
76
  ...snakeKeys(schedule),
77
+ ...(to?.length ? { deliver_to: to } : {}),
76
78
  environment,
77
79
  created_at: createdAt,
78
80
  ...snakeKeys(extra),
@@ -82,11 +84,11 @@ const withWhen = (command) => command
82
84
  .option("--at <HH:MM>", "every day at this local time")
83
85
  .option("--at-time <time>", "one run: 30m, 2d from now, or an ISO time; the schedule removes itself after it")
84
86
  .addOption(new Option("--notify-on <when>", "desktop notice after a run (default failures for an agent that repeats with --every, else always)").choices(NOTIFY_WHEN))
85
- .option("--to <route>", `also send the result, when --notify-on says so, by the owner's own tg or mail: ${ROUTE_HELP}; repeat it for more`, (route, routes = []) => [...routes, routeText(parseRoute(route))]);
86
- /** What a schedule keeps of `--notify-on` and `--to`. */
87
- const deliveryOf = ({ notifyOn, to }) => ({
87
+ .option("--deliver-to <route>", `also send the result, when --notify-on says so, by the owner's own tg or mail: ${ROUTE_HELP}; repeat it for more`, (route, routes = []) => [...routes, routeText(parseRoute(route))]);
88
+ /** What a schedule keeps of `--notify-on` and `--deliver-to`. */
89
+ const deliveryOf = ({ notifyOn, deliverTo }) => ({
88
90
  ...(notifyOn ? { notify: notifyOn } : {}),
89
- ...(to?.length ? { to } : {}),
91
+ ...(deliverTo?.length ? { to: deliverTo } : {}),
90
92
  });
91
93
  /** What the schedule command needs of a CLI: who it is and, per command, where it prints and which profile runs. */
92
94
  /** `skill show follow-ups` exists only for messenger CLIs; any other CLI gets a read-only summary it can run. */
@@ -365,7 +367,7 @@ export const scheduleCommandFor = ({ app, context: contextOf, newMail, agentInpu
365
367
  kind: schedule.kind,
366
368
  when: describeWhen(schedule.when),
367
369
  notify: notifyOf(schedule),
368
- ...(schedule.to?.length ? { to: schedule.to } : {}),
370
+ ...(schedule.to?.length ? { deliverTo: schedule.to } : {}),
369
371
  last: lastRun(scheduleFile(state, schedule.name)) ?? null,
370
372
  ...(schedule.kind === "agent"
371
373
  ? {
@@ -386,12 +388,12 @@ export const scheduleCommandFor = ({ app, context: contextOf, newMail, agentInpu
386
388
  return "";
387
389
  }
388
390
  return rows
389
- .map(({ name, kind, when, notify, to, last, spentTodayUsd, maxCostPerDayUsd }) => [
391
+ .map(({ name, kind, when, notify, deliverTo, last, spentTodayUsd, maxCostPerDayUsd }) => [
390
392
  name,
391
393
  kind,
392
394
  when,
393
395
  `notify ${notify}`,
394
- ...(to ? [`to ${to.join(", ")}`] : []),
396
+ ...(deliverTo ? [`to ${deliverTo.join(", ")}`] : []),
395
397
  last
396
398
  ? `last ${last.at.slice(0, 16).replace("T", " ")} exit ${last.exitCode}` +
397
399
  (last.failures
@@ -9,4 +9,7 @@ export type StoreInfo = v.InferOutput<typeof storeInfoEntity>;
9
9
  export declare const storeInfo: (env: NodeJS.ProcessEnv) => Promise<StoreInfo>;
10
10
  export declare const storeInfoCommand: () => Command;
11
11
  /** `store info` for a tool with no `store` command of its own. */
12
- export declare const sharedStoreCommand: () => Command;
12
+ export declare const sharedStoreCommand: (app?: {
13
+ command: string;
14
+ version?: string;
15
+ }) => Command;
@@ -7,6 +7,7 @@ import { fileFacts, openSqlite, pendingNormalization, rowCounts, searchIndexStat
7
7
  import { environmentOf, outputFor } from "./context.js";
8
8
  import { LOCAL_READ } from "./effects.js";
9
9
  import { itemOf, renderAnswer, snakeKeys } from "./envelope.js";
10
+ import { storeRecoverCommand } from "./store-recover.js";
10
11
  /** Before version 6 the column does not exist, and a file behind this build is not migrated here. */
11
12
  const pendingIfKnown = (database) => database.prepare("SELECT 1 FROM pragma_table_info('messages') WHERE name = 'normalized_text'").get()
12
13
  ? pendingNormalization(database)
@@ -67,6 +68,9 @@ export const storeInfoCommand = () => annotate(new Command("info"), { output: {
67
68
  renderAnswer(this, outputFor(this), itemOf(answer));
68
69
  });
69
70
  /** `store info` for a tool with no `store` command of its own. */
70
- export const sharedStoreCommand = () => annotate(new Command("store"), { effects: LOCAL_READ })
71
- .description("the shared local store every WireCat tool reads")
72
- .addCommand(storeInfoCommand());
71
+ export const sharedStoreCommand = (app) => {
72
+ const store = annotate(new Command("store"), { effects: LOCAL_READ })
73
+ .description("the shared local store every WireCat tool reads")
74
+ .addCommand(storeInfoCommand());
75
+ return app ? store.addCommand(storeRecoverCommand(app)) : store;
76
+ };
@@ -0,0 +1,56 @@
1
+ import { Command } from "commander";
2
+ import { type DamageExplanation } from "../store/damage.js";
3
+ import { type RecoverOptions, type Recovery } from "../store/recover.js";
4
+ export interface RecoveryCheck {
5
+ path: string;
6
+ exists: boolean;
7
+ ok: boolean;
8
+ seconds?: number;
9
+ problems?: number;
10
+ damagedTables?: {
11
+ table: string | null;
12
+ problems: number;
13
+ sample: string;
14
+ }[];
15
+ damagedSearchIndexes?: {
16
+ name: string;
17
+ error?: string;
18
+ }[];
19
+ rows?: Record<string, number | null>;
20
+ explanation?: DamageExplanation | null;
21
+ inUse?: {
22
+ pid: number;
23
+ name: string;
24
+ }[] | null;
25
+ timers?: string[] | null;
26
+ sqlite3?: {
27
+ version: string | null;
28
+ usable: boolean;
29
+ };
30
+ /** The commands to run, in order. */
31
+ next: string[];
32
+ }
33
+ /** `store recover`: what is wrong with the store and what to run, reading every page; changes nothing. */
34
+ export declare const storeRecoveryCheck: (env: NodeJS.ProcessEnv, command: string) => Promise<RecoveryCheck>;
35
+ export type RecoveryRun = Omit<Recovery, "health" | "copyErrors"> & {
36
+ /** `sqlite3` reported these while copying; most are rows that broke a constraint and were skipped. */
37
+ copyErrors: number;
38
+ checkedOk: boolean;
39
+ /** The commands that fetch again what was lost, then the timers to start. */
40
+ next: string[];
41
+ };
42
+ /** `store recover run`: refuses while a WireCat timer is scheduled, so none writes to the file being replaced. */
43
+ export declare const storeRecoveryRun: (env: NodeJS.ProcessEnv, { command, version, note, timers: scheduled, copy, }: {
44
+ command: string;
45
+ version?: string;
46
+ note?: (text: string) => void;
47
+ timers?: () => string[] | undefined;
48
+ copy?: RecoverOptions["copy"];
49
+ }) => Promise<RecoveryRun>;
50
+ /** The lines a person reads under the answer; the JSON carries the same in its fields. */
51
+ export declare const recoveryNotes: (answer: RecoveryCheck | RecoveryRun) => string[];
52
+ /** `store recover` and `store recover run`, for a CLI whose commands run on cli-core's context. */
53
+ export declare const storeRecoverCommand: ({ command, version }: {
54
+ command: string;
55
+ version?: string;
56
+ }) => Command;
@@ -0,0 +1,153 @@
1
+ import { existsSync } from "node:fs";
2
+ import { CliError } from "@wirecat/cli-base";
3
+ import { annotate } from "@wirecat/cli-base/commands";
4
+ import { Command } from "commander";
5
+ import { explainDamage } from "../store/damage.js";
6
+ import { storeHealth } from "../store/health.js";
7
+ import { storePath } from "../store/path.js";
8
+ import { activeTimers, recoverStore, sqlite3Program, storeHolders, tableRows, } from "../store/recover.js";
9
+ import { openSqlite } from "../store/store.js";
10
+ import { environmentOf, outputFor } from "./context.js";
11
+ import { LOCAL_READ, LOCAL_WRITE } from "./effects.js";
12
+ import { itemOf, renderAnswer, snakeKeys } from "./envelope.js";
13
+ /** The tables whose counts say what the store holds; every other table is in `store info`. */
14
+ const COUNTED = [
15
+ "accounts",
16
+ "chats",
17
+ "messages",
18
+ "attachments",
19
+ "emails",
20
+ "documents",
21
+ "notes",
22
+ "meetings",
23
+ "chunks",
24
+ "embeddings",
25
+ "mentions",
26
+ ];
27
+ const stopTimers = (timers) => `systemctl --user stop ${timers.join(" ")}`;
28
+ /** `store recover`: what is wrong with the store and what to run, reading every page; changes nothing. */
29
+ export const storeRecoveryCheck = async (env, command) => {
30
+ const path = storePath(env);
31
+ if (!existsSync(path))
32
+ return { path, exists: false, ok: true, next: [] };
33
+ const health = await storeHealth(path, { deep: true });
34
+ let rows = {};
35
+ try {
36
+ const { database } = await openSqlite(path, { readOnly: true });
37
+ try {
38
+ const all = tableRows(database);
39
+ rows = Object.fromEntries(COUNTED.filter((table) => table in all).map((table) => [table, all[table] ?? null]));
40
+ }
41
+ finally {
42
+ database.close();
43
+ }
44
+ }
45
+ catch { }
46
+ const explanation = explainDamage(health, command);
47
+ const inUse = storeHolders(path) ?? null;
48
+ const timers = activeTimers() ?? null;
49
+ const sqlite3 = sqlite3Program();
50
+ const next = health.ok
51
+ ? []
52
+ : [
53
+ ...(timers?.length ? [stopTimers(timers)] : []),
54
+ ...(inUse ?? []).map(({ pid, name }) => `stop ${name} (process ${pid}): it has the store open`),
55
+ ...(sqlite3.usable ? [] : ["install sqlite3 3.43 or newer (apt install sqlite3, brew install sqlite)"]),
56
+ `${command} store recover run`,
57
+ ];
58
+ return {
59
+ path,
60
+ exists: true,
61
+ ok: health.ok,
62
+ seconds: Math.round(health.ms / 100) / 10,
63
+ problems: health.problems ?? 0,
64
+ damagedTables: health.damagedTables ?? [],
65
+ damagedSearchIndexes: (health.searchIndexes ?? [])
66
+ .filter((index) => !index.ok)
67
+ .map(({ name, error }) => ({ name, ...(error ? { error } : {}) })),
68
+ rows,
69
+ explanation,
70
+ inUse,
71
+ timers,
72
+ sqlite3: { version: sqlite3.version, usable: sqlite3.usable },
73
+ next,
74
+ };
75
+ };
76
+ /** `store recover run`: refuses while a WireCat timer is scheduled, so none writes to the file being replaced. */
77
+ export const storeRecoveryRun = async (env, { command, version, note, timers: scheduled = activeTimers, copy, }) => {
78
+ const timers = scheduled() ?? [];
79
+ if (timers.length > 0)
80
+ throw new CliError("validation_error", `stop the timers first, they open the store: ${stopTimers(timers)}`, {
81
+ reason: "store.in-use",
82
+ });
83
+ const recovery = await recoverStore({
84
+ path: storePath(env),
85
+ command,
86
+ ...(version ? { version } : {}),
87
+ ...(note ? { note } : {}),
88
+ ...(copy ? { copy } : {}),
89
+ });
90
+ const mail = recovery.mailToFetch.map(({ account, emails }) => `mail <profile of ${account}> sync --rescan # ${emails} emails`);
91
+ const lostMail = recovery.lost.some(({ table }) => table === "emails") && mail.length === 0
92
+ ? ["mail <profile> sync --rescan # each mail profile"]
93
+ : [];
94
+ const { health, copyErrors, ...kept } = recovery;
95
+ return {
96
+ ...kept,
97
+ copyErrors: copyErrors.length,
98
+ checkedOk: health.ok,
99
+ next: [...mail, ...lostMail, "systemctl --user start <the timers stopped before>"],
100
+ };
101
+ };
102
+ /** The lines a person reads under the answer; the JSON carries the same in its fields. */
103
+ export const recoveryNotes = (answer) => {
104
+ if ("keptAt" in answer) {
105
+ const lost = answer.lost.map(({ table, before, read, kept }) => `${table}: ${kept} kept of ${before ?? "an uncountable number"}${read > kept ? `, ${read - kept} read but unusable` : ""}`);
106
+ return [
107
+ `recovered in ${answer.seconds.total} s; the damaged file is kept at ${answer.keptAt}`,
108
+ ...(lost.length ? lost : ["no table lost a row"]),
109
+ ...answer.dangling.map(({ table, parent, rows }) => `${rows} rows of ${table} point at ${parent} rows the store does not hold`),
110
+ ...answer.next.map((line) => `next: ${line}`),
111
+ ];
112
+ }
113
+ if (!answer.exists)
114
+ return ["no store yet"];
115
+ if (answer.ok)
116
+ return [`the store is healthy (checked every page and search index in ${answer.seconds} s)`];
117
+ const why = answer.explanation;
118
+ return [
119
+ ...(why ? [why.what, ...why.impact, why.externalUnaffected] : []),
120
+ ...answer.next.map((line) => `next: ${line}`),
121
+ ];
122
+ };
123
+ /** `store recover` and `store recover run`, for a CLI whose commands run on cli-core's context. */
124
+ export const storeRecoverCommand = ({ command, version }) => {
125
+ const recover = annotate(new Command("recover"), { effects: LOCAL_READ, examples: [`${command} store recover`] })
126
+ .summary("what is damaged in the store and how to recover it")
127
+ .description("check every page and search index of the store, say what is damaged, what it means and the commands that recover it; changes nothing")
128
+ .action(async function () {
129
+ const output = outputFor(this);
130
+ const answer = await storeRecoveryCheck(environmentOf(this).env ?? process.env, command);
131
+ renderAnswer(this, output, itemOf(snakeKeys(answer)));
132
+ if (output.format === "pretty")
133
+ for (const line of recoveryNotes(answer))
134
+ output.renderer.note(line);
135
+ });
136
+ recover.addCommand(annotate(new Command("run"), { effects: LOCAL_WRITE, examples: [`${command} store recover run`] })
137
+ .summary("rebuild a damaged store from every row it can read")
138
+ .description("copy every readable row into a new file with the sqlite3 program, rebuild the search indexes, mentions and vector index, " +
139
+ "check it, and put it in place of the store; the damaged file is kept beside it. Refuses while a WireCat timer or process has the store")
140
+ .action(async function () {
141
+ const output = outputFor(this);
142
+ const answer = await storeRecoveryRun(environmentOf(this).env ?? process.env, {
143
+ command,
144
+ ...(version ? { version } : {}),
145
+ note: (text) => output.renderer.note(text),
146
+ });
147
+ renderAnswer(this, output, itemOf(snakeKeys(answer)));
148
+ if (output.format === "pretty")
149
+ for (const line of recoveryNotes(answer))
150
+ output.renderer.note(line);
151
+ }));
152
+ return recover;
153
+ };
@@ -4124,7 +4124,7 @@ export declare const ENTITIES: {
4124
4124
  readonly kind: v.PicklistSchema<["command", "agent"], undefined>;
4125
4125
  readonly when: v.StringSchema<undefined>;
4126
4126
  readonly notify: v.OptionalSchema<v.PicklistSchema<["always", "failures", "never"], undefined>, undefined>;
4127
- readonly to: v.OptionalSchema<v.ArraySchema<v.StringSchema<undefined>, undefined>, undefined>;
4127
+ readonly deliver_to: v.OptionalSchema<v.ArraySchema<v.StringSchema<undefined>, undefined>, undefined>;
4128
4128
  readonly last: v.NullableSchema<v.StrictObjectSchema<{
4129
4129
  readonly at: v.SchemaWithPipe<readonly [v.StringSchema<undefined>, v.IsoTimestampAction<string, "an ISO 8601 time in UTC">, v.EndsWithAction<string, "Z", "an ISO 8601 time in UTC, ending in `Z`">]>;
4130
4130
  readonly exit_code: v.NumberSchema<undefined>;
@@ -51,7 +51,7 @@ export declare const scheduledJob: v.StrictObjectSchema<{
51
51
  /** When a finished run shows a desktop notice: `always`, `failures` or `never`; the default is shown, not left out. */
52
52
  readonly notify: v.OptionalSchema<v.PicklistSchema<["always", "failures", "never"], undefined>, undefined>;
53
53
  /** Where a run's result is sent after it: `telegram-bot:<profile>/<chat>` or `email:<profile>/<address>`. */
54
- readonly to: v.OptionalSchema<v.ArraySchema<v.StringSchema<undefined>, undefined>, undefined>;
54
+ readonly deliver_to: v.OptionalSchema<v.ArraySchema<v.StringSchema<undefined>, undefined>, undefined>;
55
55
  readonly last: v.NullableSchema<v.StrictObjectSchema<{
56
56
  readonly at: v.SchemaWithPipe<readonly [v.StringSchema<undefined>, v.IsoTimestampAction<string, "an ISO 8601 time in UTC">, v.EndsWithAction<string, "Z", "an ISO 8601 time in UTC, ending in `Z`">]>;
57
57
  readonly exit_code: v.NumberSchema<undefined>;
@@ -35,7 +35,7 @@ export const scheduledJob = v.strictObject({
35
35
  /** When a finished run shows a desktop notice: `always`, `failures` or `never`; the default is shown, not left out. */
36
36
  notify: v.optional(v.picklist(["always", "failures", "never"])),
37
37
  /** Where a run's result is sent after it: `telegram-bot:<profile>/<chat>` or `email:<profile>/<address>`. */
38
- to: v.optional(v.array(text)),
38
+ deliver_to: v.optional(v.array(text)),
39
39
  last: v.nullable(v.strictObject({
40
40
  at: time,
41
41
  exit_code: v.number(),
@@ -156,6 +156,14 @@ const OTHERS = [
156
156
  ],
157
157
  validation("store.refused", "the local store refused the document text"),
158
158
  ["store.not-writable", "permission_error", "the message store cannot be written from this process"],
159
+ validation("store.in-use", "a process or a scheduled timer has the store open; stop it, then recover the store"),
160
+ [
161
+ "store.sqlite3-missing",
162
+ "configuration_error",
163
+ "recovering the store needs the sqlite3 program, 3.43 or newer, with its .recover command",
164
+ ],
165
+ validation("store.recovery-failed", "the recovered store did not pass its check, so the store was left as it was"),
166
+ validation("store.changed-during-recovery", "the store was written to during the recovery, so it was left as it was"),
159
167
  [
160
168
  "store.vector-extension-missing",
161
169
  "configuration_error",
@@ -0,0 +1,19 @@
1
+ import type { StoreHealth } from "./health.js";
2
+ /**
3
+ * What a damaged store means, in the owner's words: one structure the CLIs render and the desktop app can show as it
4
+ * is. Built from a health check only, so it never reads the damaged file again.
5
+ */
6
+ export interface DamageExplanation {
7
+ what: string;
8
+ causes: string[];
9
+ investigate: string[];
10
+ impact: string[];
11
+ externalUnaffected: string;
12
+ steps: string[];
13
+ }
14
+ /**
15
+ * Tables a recovery rebuilds from the others: search indexes, the vector index, what the indexing finds, and the
16
+ * vectors, which a search by meaning computes again for any text that has none.
17
+ */
18
+ export declare const isDerivedTable: (table: string, searchIndexes: readonly string[]) => boolean;
19
+ export declare const explainDamage: (health: StoreHealth, command: string) => DamageExplanation | null;