@wirecat/cli-core 0.616.0 → 0.618.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.
Files changed (57) hide show
  1. package/CHANGELOG.md +84 -0
  2. package/dist/background/backfill.d.ts +21 -0
  3. package/dist/background/backfill.js +41 -0
  4. package/dist/background/report/messages.js +4 -1
  5. package/dist/background/report/view.js +4 -1
  6. package/dist/background/schedules.d.ts +17 -0
  7. package/dist/background/schedules.js +21 -8
  8. package/dist/cli/context.d.ts +5 -0
  9. package/dist/cli/index.d.ts +3 -2
  10. package/dist/cli/index.js +3 -2
  11. package/dist/cli/messenger/serve-command.js +24 -2
  12. package/dist/cli/messenger/serve-outdated.d.ts +22 -0
  13. package/dist/cli/messenger/serve-outdated.js +100 -0
  14. package/dist/cli/schedule/schedule-command.d.ts +9 -0
  15. package/dist/cli/schedule/schedule-command.js +13 -0
  16. package/dist/cli/store-recover.js +26 -2
  17. package/dist/domain/links.d.ts +17 -4
  18. package/dist/domain/links.js +20 -7
  19. package/dist/domain/models.d.ts +6 -0
  20. package/dist/domain/relations.d.ts +15 -0
  21. package/dist/domain/relations.js +23 -0
  22. package/dist/errors/reasons.js +7 -0
  23. package/dist/index.d.ts +2 -2
  24. package/dist/index.js +2 -2
  25. package/dist/meetings/mcp/index.d.ts +15 -15
  26. package/dist/services/link-scorers/chat-senders.js +39 -29
  27. package/dist/services/link-scorers/member-chats.js +4 -3
  28. package/dist/services/link-scorers/member-dialog.js +5 -7
  29. package/dist/services/link-scorers/scan.js +17 -8
  30. package/dist/services/link-scorers/shared.d.ts +6 -2
  31. package/dist/services/link-scorers/shared.js +6 -2
  32. package/dist/services/link-scorers/types.d.ts +2 -0
  33. package/dist/store/migrations.d.ts +1 -1
  34. package/dist/store/migrations.js +35 -12
  35. package/dist/store/recover.d.ts +34 -0
  36. package/dist/store/recover.js +188 -6
  37. package/dist/store/refill.d.ts +28 -0
  38. package/dist/store/refill.js +120 -0
  39. package/dist/store/sqlite/bot-tokens.d.ts +11 -0
  40. package/dist/store/sqlite/bot-tokens.js +57 -0
  41. package/dist/store/sqlite/chats.js +2 -0
  42. package/dist/store/sqlite/clients.d.ts +7 -3
  43. package/dist/store/sqlite/clients.js +11 -7
  44. package/dist/store/sqlite/email-index.js +9 -11
  45. package/dist/store/sqlite/knowledge.js +3 -1
  46. package/dist/store/sqlite/messages.d.ts +1 -1
  47. package/dist/store/sqlite/messages.js +3 -1
  48. package/dist/store/sqlite/note-index.js +9 -7
  49. package/dist/store/sqlite/notes.d.ts +1 -1
  50. package/dist/store/sqlite/notes.js +8 -6
  51. package/dist/store/sqlite/open-store.js +2 -0
  52. package/dist/store/sqlite/relation-list.js +2 -0
  53. package/dist/store/sqlite/relation-proposals.d.ts +12 -1
  54. package/dist/store/sqlite/relation-proposals.js +50 -11
  55. package/dist/store/sqlite/transaction.d.ts +13 -0
  56. package/dist/store/sqlite/transaction.js +25 -17
  57. package/package.json +1 -1
@@ -12,17 +12,18 @@ export const isBusy = (error) => sqliteCode(error) === SQLITE_BUSY;
12
12
  * Takes the write lock, waiting as long as the connection's `busy_timeout`. SQLite's own wait sleeps 1 ms, then up to
13
13
  * 100 ms between tries, so a process that commits and begins again at once is never seen free and the waiter fails
14
14
  * with "database is locked" after the full timeout. Trying every millisecond sees a gap of a fraction of a
15
- * millisecond between two writes; a writer that leaves none still starves it, hence `writeBatch`'s pause.
15
+ * millisecond between two writes; a writer that leaves none still starves it, hence the pause after a write that
16
+ * waited. Answers whether it had to wait.
16
17
  */
17
- const beginImmediate = (database) => {
18
+ export const beginImmediate = (database) => {
18
19
  const timeout = Number(database.prepare("PRAGMA busy_timeout").get()?.timeout ?? 0);
19
20
  const deadline = Date.now() + timeout;
20
21
  database.exec("PRAGMA busy_timeout = 0");
21
22
  try {
22
- for (;;) {
23
+ for (let waited = false;; waited = true) {
23
24
  try {
24
25
  database.exec("BEGIN IMMEDIATE");
25
- return;
26
+ return waited;
26
27
  }
27
28
  catch (error) {
28
29
  if (!isBusy(error) || Date.now() >= deadline)
@@ -35,6 +36,16 @@ const beginImmediate = (database) => {
35
36
  database.exec(`PRAGMA busy_timeout = ${timeout}`);
36
37
  }
37
38
  };
39
+ /**
40
+ * On some errors (a full disk, an I/O error) SQLite has already rolled the transaction back, and the ROLLBACK then
41
+ * fails with "no transaction is active". That failure must not take the place of the error that ended it.
42
+ */
43
+ export const rollBack = (database, sql = "ROLLBACK") => {
44
+ try {
45
+ database.exec(sql);
46
+ }
47
+ catch { }
48
+ };
38
49
  const connections = new WeakMap();
39
50
  const connectionOf = (database) => {
40
51
  let connection = connections.get(database);
@@ -57,13 +68,7 @@ export const write = (database, body, { beforeCommit } = {}) => transact(databas
57
68
  * the next `write`. The outermost pauses 2 ms after it commits: a loop that begins again at once leaves another
58
69
  * process's `write` a gap of microseconds, and it misses that gap until its 5 s wait runs out.
59
70
  */
60
- export const writeBatch = (database, body) => {
61
- const outer = connectionOf(database).depth === 0;
62
- const result = transact(database, body, false);
63
- if (outer)
64
- Atomics.wait(pause, 0, 0, BATCH_PAUSE_MS);
65
- return result;
66
- };
71
+ export const writeBatch = (database, body) => transact(database, body, false);
67
72
  const BATCH_PAUSE_MS = 2;
68
73
  const transact = (database, body, drain, beforeCommit) => {
69
74
  const connection = connectionOf(database);
@@ -71,9 +76,8 @@ const transact = (database, body, drain, beforeCommit) => {
71
76
  throw new Error("a store write cannot run inside a read");
72
77
  const outer = connection.depth === 0;
73
78
  const name = `w${connection.depth + 1}`;
74
- if (outer)
75
- beginImmediate(database);
76
- else
79
+ const waited = outer && beginImmediate(database);
80
+ if (!outer)
77
81
  database.exec(`SAVEPOINT ${name}`);
78
82
  connection.depth += 1;
79
83
  try {
@@ -85,10 +89,14 @@ const transact = (database, body, drain, beforeCommit) => {
85
89
  }
86
90
  beforeCommit?.();
87
91
  database.exec(outer ? "COMMIT" : `RELEASE ${name}`);
92
+ // A write that had to wait has others waiting behind it, and its caller's next write would begin before they see
93
+ // the lock free. Pausing only then costs an uncontended write nothing.
94
+ if (outer && (waited || !drain))
95
+ Atomics.wait(pause, 0, 0, BATCH_PAUSE_MS);
88
96
  return result;
89
97
  }
90
98
  catch (error) {
91
- database.exec(outer ? "ROLLBACK" : `ROLLBACK TO ${name}; RELEASE ${name}`);
99
+ rollBack(database, outer ? "ROLLBACK" : `ROLLBACK TO ${name}; RELEASE ${name}`);
92
100
  throw error;
93
101
  }
94
102
  finally {
@@ -146,7 +154,7 @@ export const rehearse = async ({ database, client }, body) => {
146
154
  else
147
155
  delete target[name];
148
156
  connection.depth -= 1;
149
- database.exec("ROLLBACK");
157
+ rollBack(database);
150
158
  }
151
159
  };
152
160
  /** Several queries that must see one state of the file. Never drains: that is the outer write's work, or nobody's. */
@@ -164,7 +172,7 @@ export const read = (database, body) => {
164
172
  return result;
165
173
  }
166
174
  catch (error) {
167
- database.exec(`ROLLBACK TO ${name}; RELEASE ${name}`);
175
+ rollBack(database, `ROLLBACK TO ${name}; RELEASE ${name}`);
168
176
  throw error;
169
177
  }
170
178
  finally {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wirecat/cli-core",
3
- "version": "0.616.0",
3
+ "version": "0.618.0",
4
4
  "description": "The messenger-neutral half of a messaging CLI: domain model, message locators, rendering, name resolution, the SQLite seam, the command skeleton and shared read commands, the send guard, run records and the message store",
5
5
  "license": "Apache-2.0",
6
6
  "author": "Viacheslav Ptsarev",