@getengram/cli 0.6.1 → 0.7.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 (206) hide show
  1. package/README.md +1 -1
  2. package/dist/commands/auth.d.ts.map +1 -1
  3. package/dist/commands/auth.js +38 -11
  4. package/dist/commands/auth.js.map +1 -1
  5. package/dist/commands/conversations.d.ts.map +1 -1
  6. package/dist/commands/conversations.js +8 -2
  7. package/dist/commands/conversations.js.map +1 -1
  8. package/dist/commands/exclude.d.ts.map +1 -1
  9. package/dist/commands/exclude.js +73 -21
  10. package/dist/commands/exclude.js.map +1 -1
  11. package/dist/commands/import.d.ts +30 -3
  12. package/dist/commands/import.d.ts.map +1 -1
  13. package/dist/commands/import.js +374 -45
  14. package/dist/commands/import.js.map +1 -1
  15. package/dist/commands/login.d.ts.map +1 -1
  16. package/dist/commands/login.js +144 -29
  17. package/dist/commands/login.js.map +1 -1
  18. package/dist/commands/search.js +2 -2
  19. package/dist/commands/search.js.map +1 -1
  20. package/dist/commands/status.d.ts +16 -0
  21. package/dist/commands/status.d.ts.map +1 -0
  22. package/dist/commands/status.js +51 -0
  23. package/dist/commands/status.js.map +1 -0
  24. package/dist/commands/store.d.ts.map +1 -1
  25. package/dist/commands/store.js +19 -3
  26. package/dist/commands/store.js.map +1 -1
  27. package/dist/commands/upgrade.d.ts +24 -0
  28. package/dist/commands/upgrade.d.ts.map +1 -1
  29. package/dist/commands/upgrade.js +64 -15
  30. package/dist/commands/upgrade.js.map +1 -1
  31. package/dist/commands/usage.d.ts +44 -0
  32. package/dist/commands/usage.d.ts.map +1 -1
  33. package/dist/commands/usage.js +79 -19
  34. package/dist/commands/usage.js.map +1 -1
  35. package/dist/commands/vault.d.ts +11 -2
  36. package/dist/commands/vault.d.ts.map +1 -1
  37. package/dist/commands/vault.js +372 -53
  38. package/dist/commands/vault.js.map +1 -1
  39. package/dist/config-file.d.ts +74 -0
  40. package/dist/config-file.d.ts.map +1 -0
  41. package/dist/config-file.js +152 -0
  42. package/dist/config-file.js.map +1 -0
  43. package/dist/config-guard.d.ts +96 -0
  44. package/dist/config-guard.d.ts.map +1 -0
  45. package/dist/config-guard.js +184 -0
  46. package/dist/config-guard.js.map +1 -0
  47. package/dist/config.d.ts +48 -4
  48. package/dist/config.d.ts.map +1 -1
  49. package/dist/config.js +101 -12
  50. package/dist/config.js.map +1 -1
  51. package/dist/daemon/as-root.d.ts +46 -0
  52. package/dist/daemon/as-root.d.ts.map +1 -0
  53. package/dist/daemon/as-root.js +64 -0
  54. package/dist/daemon/as-root.js.map +1 -0
  55. package/dist/daemon/backoff.d.ts +26 -0
  56. package/dist/daemon/backoff.d.ts.map +1 -0
  57. package/dist/daemon/backoff.js +37 -0
  58. package/dist/daemon/backoff.js.map +1 -0
  59. package/dist/daemon/codex-parser.d.ts +38 -4
  60. package/dist/daemon/codex-parser.d.ts.map +1 -1
  61. package/dist/daemon/codex-parser.js +111 -23
  62. package/dist/daemon/codex-parser.js.map +1 -1
  63. package/dist/daemon/command-path.d.ts +13 -0
  64. package/dist/daemon/command-path.d.ts.map +1 -0
  65. package/dist/daemon/command-path.js +35 -0
  66. package/dist/daemon/command-path.js.map +1 -0
  67. package/dist/daemon/commands.d.ts +146 -5
  68. package/dist/daemon/commands.d.ts.map +1 -1
  69. package/dist/daemon/commands.js +2766 -316
  70. package/dist/daemon/commands.js.map +1 -1
  71. package/dist/daemon/db.d.ts +255 -27
  72. package/dist/daemon/db.d.ts.map +1 -1
  73. package/dist/daemon/db.js +749 -53
  74. package/dist/daemon/db.js.map +1 -1
  75. package/dist/daemon/exclusions.d.ts +23 -3
  76. package/dist/daemon/exclusions.d.ts.map +1 -1
  77. package/dist/daemon/exclusions.js +91 -21
  78. package/dist/daemon/exclusions.js.map +1 -1
  79. package/dist/daemon/homebrew.d.ts +26 -0
  80. package/dist/daemon/homebrew.d.ts.map +1 -0
  81. package/dist/daemon/homebrew.js +43 -0
  82. package/dist/daemon/homebrew.js.map +1 -0
  83. package/dist/daemon/instance.d.ts +427 -0
  84. package/dist/daemon/instance.d.ts.map +1 -0
  85. package/dist/daemon/instance.js +1201 -0
  86. package/dist/daemon/instance.js.map +1 -0
  87. package/dist/daemon/key-source.d.ts +41 -0
  88. package/dist/daemon/key-source.d.ts.map +1 -0
  89. package/dist/daemon/key-source.js +89 -0
  90. package/dist/daemon/key-source.js.map +1 -0
  91. package/dist/daemon/launch.d.ts +56 -0
  92. package/dist/daemon/launch.d.ts.map +1 -0
  93. package/dist/daemon/launch.js +153 -0
  94. package/dist/daemon/launch.js.map +1 -0
  95. package/dist/daemon/launchd.d.ts +215 -0
  96. package/dist/daemon/launchd.d.ts.map +1 -0
  97. package/dist/daemon/launchd.js +527 -0
  98. package/dist/daemon/launchd.js.map +1 -0
  99. package/dist/daemon/login-start.d.ts +2 -0
  100. package/dist/daemon/login-start.d.ts.map +1 -0
  101. package/dist/daemon/login-start.js +25 -0
  102. package/dist/daemon/login-start.js.map +1 -0
  103. package/dist/daemon/ownership.d.ts +123 -0
  104. package/dist/daemon/ownership.d.ts.map +1 -0
  105. package/dist/daemon/ownership.js +245 -0
  106. package/dist/daemon/ownership.js.map +1 -0
  107. package/dist/daemon/package-runner.d.ts +28 -0
  108. package/dist/daemon/package-runner.d.ts.map +1 -0
  109. package/dist/daemon/package-runner.js +113 -0
  110. package/dist/daemon/package-runner.js.map +1 -0
  111. package/dist/daemon/parser.d.ts +45 -7
  112. package/dist/daemon/parser.d.ts.map +1 -1
  113. package/dist/daemon/parser.js +153 -53
  114. package/dist/daemon/parser.js.map +1 -1
  115. package/dist/daemon/powershell.d.ts +25 -0
  116. package/dist/daemon/powershell.d.ts.map +1 -0
  117. package/dist/daemon/powershell.js +30 -0
  118. package/dist/daemon/powershell.js.map +1 -0
  119. package/dist/daemon/preflight.d.ts.map +1 -1
  120. package/dist/daemon/preflight.js +5 -1
  121. package/dist/daemon/preflight.js.map +1 -1
  122. package/dist/daemon/prune.d.ts +101 -0
  123. package/dist/daemon/prune.d.ts.map +1 -0
  124. package/dist/daemon/prune.js +287 -0
  125. package/dist/daemon/prune.js.map +1 -0
  126. package/dist/daemon/queue-error.d.ts +47 -0
  127. package/dist/daemon/queue-error.d.ts.map +1 -0
  128. package/dist/daemon/queue-error.js +59 -0
  129. package/dist/daemon/queue-error.js.map +1 -0
  130. package/dist/daemon/root-daemon.d.ts +60 -0
  131. package/dist/daemon/root-daemon.d.ts.map +1 -0
  132. package/dist/daemon/root-daemon.js +86 -0
  133. package/dist/daemon/root-daemon.js.map +1 -0
  134. package/dist/daemon/scoop.d.ts +52 -0
  135. package/dist/daemon/scoop.d.ts.map +1 -0
  136. package/dist/daemon/scoop.js +84 -0
  137. package/dist/daemon/scoop.js.map +1 -0
  138. package/dist/daemon/scrubber.d.ts +396 -14
  139. package/dist/daemon/scrubber.d.ts.map +1 -1
  140. package/dist/daemon/scrubber.js +915 -52
  141. package/dist/daemon/scrubber.js.map +1 -1
  142. package/dist/daemon/status.d.ts +306 -11
  143. package/dist/daemon/status.d.ts.map +1 -1
  144. package/dist/daemon/status.js +568 -43
  145. package/dist/daemon/status.js.map +1 -1
  146. package/dist/daemon/supervise.d.ts +78 -0
  147. package/dist/daemon/supervise.d.ts.map +1 -0
  148. package/dist/daemon/supervise.js +121 -0
  149. package/dist/daemon/supervise.js.map +1 -0
  150. package/dist/daemon/supervisor.d.ts +18 -0
  151. package/dist/daemon/supervisor.d.ts.map +1 -0
  152. package/dist/daemon/supervisor.js +56 -0
  153. package/dist/daemon/supervisor.js.map +1 -0
  154. package/dist/daemon/syncer.d.ts +380 -21
  155. package/dist/daemon/syncer.d.ts.map +1 -1
  156. package/dist/daemon/syncer.js +1432 -182
  157. package/dist/daemon/syncer.js.map +1 -1
  158. package/dist/daemon/systemd.d.ts +109 -0
  159. package/dist/daemon/systemd.d.ts.map +1 -0
  160. package/dist/daemon/systemd.js +363 -0
  161. package/dist/daemon/systemd.js.map +1 -0
  162. package/dist/daemon/types.d.ts +58 -6
  163. package/dist/daemon/types.d.ts.map +1 -1
  164. package/dist/daemon/vault-changed.d.ts +29 -0
  165. package/dist/daemon/vault-changed.d.ts.map +1 -0
  166. package/dist/daemon/vault-changed.js +47 -0
  167. package/dist/daemon/vault-changed.js.map +1 -0
  168. package/dist/daemon/vault-key-missing.d.ts +70 -0
  169. package/dist/daemon/vault-key-missing.d.ts.map +1 -0
  170. package/dist/daemon/vault-key-missing.js +125 -0
  171. package/dist/daemon/vault-key-missing.js.map +1 -0
  172. package/dist/daemon/watcher.d.ts +87 -4
  173. package/dist/daemon/watcher.d.ts.map +1 -1
  174. package/dist/daemon/watcher.js +286 -36
  175. package/dist/daemon/watcher.js.map +1 -1
  176. package/dist/daemon/worker-main.js +352 -64
  177. package/dist/daemon/worker-main.js.map +1 -1
  178. package/dist/daemon/worker.js +19 -1
  179. package/dist/daemon/worker.js.map +1 -1
  180. package/dist/import-resume.d.ts +163 -0
  181. package/dist/import-resume.d.ts.map +1 -0
  182. package/dist/import-resume.js +267 -0
  183. package/dist/import-resume.js.map +1 -0
  184. package/dist/index.js +242 -33
  185. package/dist/index.js.map +1 -1
  186. package/dist/message-bounds.d.ts +52 -0
  187. package/dist/message-bounds.d.ts.map +1 -0
  188. package/dist/message-bounds.js +157 -0
  189. package/dist/message-bounds.js.map +1 -0
  190. package/dist/output.d.ts +14 -0
  191. package/dist/output.d.ts.map +1 -1
  192. package/dist/output.js +33 -5
  193. package/dist/output.js.map +1 -1
  194. package/dist/private-files.d.ts +126 -0
  195. package/dist/private-files.d.ts.map +1 -0
  196. package/dist/private-files.js +372 -0
  197. package/dist/private-files.js.map +1 -0
  198. package/dist/server-url.d.ts +31 -0
  199. package/dist/server-url.d.ts.map +1 -0
  200. package/dist/server-url.js +47 -0
  201. package/dist/server-url.js.map +1 -0
  202. package/dist/vault-key.d.ts +122 -0
  203. package/dist/vault-key.d.ts.map +1 -0
  204. package/dist/vault-key.js +273 -0
  205. package/dist/vault-key.js.map +1 -0
  206. package/package.json +2 -2
@@ -2,54 +2,374 @@ import type { Engram } from "@getengram/sdk";
2
2
  import { DaemonDb } from "./db.js";
3
3
  import type { SecretScrubber } from "./scrubber.js";
4
4
  import type { ParsedMessage, SessionMeta } from "./types.js";
5
+ /**
6
+ * The key fingerprint of a Syncer built with a fixed client and no
7
+ * KeySource (tests, and callers that manage the key themselves). Not a
8
+ * SHA-256, so the server is never asked about it.
9
+ */
10
+ export declare const FIXED_KEY_FP = "fixed";
11
+ /**
12
+ * The key's SHA-256 as lowercase hex (engram#594). It is the hash the server
13
+ * keeps of the key, so the server can say which earlier keys are the same
14
+ * account's. The key itself is never written to daemon.db.
15
+ */
16
+ export declare function keyFingerprint(key: string): string;
17
+ /**
18
+ * An account the daemon captures and sends for: an API key, and the server
19
+ * it is a key of (engram#685). The two are read from config.json together,
20
+ * and a change to either is a change of account.
21
+ */
22
+ export interface Account {
23
+ key: string;
24
+ /** The server's base URL (resolveBaseUrl). */
25
+ server: string;
26
+ }
27
+ /**
28
+ * What daemon.db records about the account a message was captured for
29
+ * (engram#594, engram#685). On the default server, the key's fingerprint
30
+ * (keyFingerprint), as before servers were recorded, so nothing recorded
31
+ * then changes. On any other server, the key's fingerprint followed by
32
+ * "@" and a tag of the server's address, so what is queued for one server
33
+ * is never sent to another. Neither the key nor the address is written.
34
+ */
35
+ export declare function accountFingerprint(account: Account): string;
36
+ /** Whether `a` and `b` are the same account: the same key, for the same
37
+ * server. */
38
+ export declare function sameAccount(a: Account | undefined, b: Account | undefined): boolean;
39
+ /** What a server may be asked about in checkOwnership (ownershipCandidates). */
40
+ export interface OwnershipCandidate {
41
+ /** The account fingerprint the rows are under. */
42
+ fp: string;
43
+ /** The key hash the server is asked about. */
44
+ keyHash: string;
45
+ /** Only the rows without a record of their server may move (adoptKeys). */
46
+ unknownServerOnly: boolean;
47
+ }
48
+ /**
49
+ * Which of `others` (other account fingerprints in daemon.db) the server of
50
+ * the account `runFp` may be asked about, each with the key hash to ask
51
+ * with (checkOwnership). What it confirms is its account's is sent with
52
+ * that account (engram#685):
53
+ *
54
+ * - The default server: what was queued under another key for it, or
55
+ * before servers were recorded, as before. Never what was queued for
56
+ * another server.
57
+ * - Any other server: what was queued for that same server under another
58
+ * key. And, under any key in `unknownServer` (its own included), what
59
+ * was queued before servers were recorded (daemon.db version 4): then a
60
+ * key's fingerprint alone stood for whatever server was configured, so
61
+ * it may be this server's, and the server confirms the key is its
62
+ * account's before it moves. Never what was recorded for the default
63
+ * server: an answer from another server doesn't make it that server's.
64
+ */
65
+ export declare function ownershipCandidates(runFp: string, others: readonly string[], unknownServer: ReadonlySet<string>): OwnershipCandidate[];
66
+ /**
67
+ * Where the daemon's account, its API key and server, comes from
68
+ * (engram#587, engram#594, engram#685). It is read again every
69
+ * KEY_POLL_MS: a new key or server is used without a restart, and no key
70
+ * stops capture and sending.
71
+ */
72
+ export interface KeySource {
73
+ /** The account configured at start, if any. */
74
+ current?: Account;
75
+ /**
76
+ * config.json couldn't be read at start, so which account is configured
77
+ * isn't known: nothing is captured or sent until it has been read.
78
+ */
79
+ unread?: boolean;
80
+ /**
81
+ * The account configured now: its key from ENGRAM_API_KEY, else
82
+ * ~/.engram/config.json, and its server from ENGRAM_BASE_URL, else
83
+ * config.json, else the default, read together. Throws when config.json
84
+ * is needed and can't be read or parsed. That is not a logout: the
85
+ * account in use stays until the file can be read.
86
+ */
87
+ read(): Promise<Account | undefined>;
88
+ /** A client for an account. */
89
+ connect(account: Account): Engram;
90
+ /** No key is configured any more: let go of what was built for the last
91
+ * one (the vault read through it). Called after capture has stopped. */
92
+ disconnect?(): void;
93
+ /**
94
+ * Which of `keyHashes` (key fingerprints) are keys of `account`, asked
95
+ * of its server. Throws when the server doesn't answer; an Error with a
96
+ * numeric `status` when it refused.
97
+ */
98
+ ownedKeys?(account: Account, keyHashes: string[]): Promise<string[]>;
99
+ }
100
+ /**
101
+ * Starts and stops reading transcripts (engram#594). Capture runs only
102
+ * while a key is configured, so whatever is read is queued for the account
103
+ * signed in at the time. `stop(true)` first reads what was written but not
104
+ * read yet: when the account changes, those lines are queued for the
105
+ * account they were written under.
106
+ */
107
+ export interface CaptureControl {
108
+ start(): void;
109
+ stop(drain: boolean): void;
110
+ }
111
+ /**
112
+ * After Engram answers that it doesn't have a conversation (engram#593),
113
+ * when it is asked again: the n-th such answer in a row is followed by a
114
+ * check after the n-th wait here, and the answer after the last of them is
115
+ * final (about four days after the first). A check sends one message. If
116
+ * that goes through, Engram has the conversation after all (a restore, or
117
+ * an answer that was wrong) and the rest follows as usual.
118
+ *
119
+ * Until then the conversation's messages, and any captured for its session
120
+ * later, stay in daemon.db and aren't sent. The conversation is never
121
+ * created again: the user may have deleted it, and a deleted conversation
122
+ * must not come back. Kept in daemon.db, so a restart doesn't start the
123
+ * schedule over.
124
+ */
125
+ export declare const GONE_RECHECK_MS: number[];
5
126
  /**
6
127
  * Batches parsed messages and sends them to the Engram API.
7
128
  * If offline, messages sit in the SQLite queue and are retried
8
129
  * on the next flush cycle.
130
+ *
131
+ * Every queued message records the key it was captured under (engram#594),
132
+ * as a fingerprint, and is sent only with that key or with a key the server
133
+ * confirmed is the same account's (a rotated or re-issued key). The
134
+ * configured key is read every KEY_POLL_MS. A new key is used from the next
135
+ * request on; no key, after `engram auth logout`, stops capture and sending
136
+ * at once. Nothing queued is deleted either way: it waits on this machine
137
+ * until its account signs in again.
138
+ *
139
+ * While signed out, capture stops and each transcript's read offset stays
140
+ * where it was, so what is written meanwhile is read, and queued, for the
141
+ * next account that signs in.
9
142
  */
10
143
  export declare class Syncer {
11
144
  private readonly scrubber;
145
+ private readonly keys;
12
146
  private db;
13
- private client;
147
+ /** The key in use; null while signed out. */
148
+ private active;
149
+ /** The key captures are queued under (null: signed out). */
150
+ private captureFp;
14
151
  private timer;
15
- private flushing;
16
- private authFailed;
152
+ private keyTimer;
153
+ private running;
154
+ /** A read of the configured key in progress, shared by its callers. */
155
+ private keyCheck;
156
+ /** stop() was called: capture doesn't start again, and a key change only
157
+ * stops sending. */
158
+ private stopped;
159
+ private capture;
160
+ private capturing;
161
+ /** Paused because the API key was rejected: until when, and how many
162
+ * pauses in a row without a request going through. */
163
+ private authHeldUntil;
164
+ private authHoldStep;
165
+ /** The account configured when it was last read (undefined: none). */
166
+ private currentAccount;
167
+ /** config.json hasn't been read since start (KeySource.unread): which
168
+ * account is configured isn't known yet. */
169
+ private unread;
170
+ /** The fingerprint rows that belonged to no key were last handed to. */
171
+ private claimedFor;
172
+ private ownership;
173
+ /** The sessions the ownership gate holds were logged, under this key. */
174
+ private gateLogged;
175
+ /** The last message logged for each lasting problem, so a problem that
176
+ * lasts is logged once, not on every poll. */
177
+ private problems;
17
178
  /** Billing backoff state */
18
179
  private billingBackoffUntil;
19
180
  private billingBackoffStep;
20
- /** Sessions awaiting conversation creation (deferred from parse time). */
21
- private pendingSessions;
22
- constructor(db: DaemonDb, client: Engram, scrubber?: SecretScrubber | null);
181
+ /** Sessions whose conversation create failed: when to try again. The
182
+ * sessions themselves live in daemon.db (engram#370) — losing this map
183
+ * on a restart only means an earlier retry. */
184
+ private createRetry;
185
+ /**
186
+ * Nothing is sent from this account until this time, after the server
187
+ * refused a request for when it came rather than for what it carried: a
188
+ * rate limit, or a pause for maintenance (engram#596). `throttleStep`
189
+ * counts such pauses in a row without a request going through.
190
+ */
191
+ private throttledUntil;
192
+ private throttleStep;
193
+ /** A maintenance pause is among those pauses. It holds only sends, so a
194
+ * create or a delete going through doesn't show it's over. */
195
+ private throttlePaused;
196
+ /**
197
+ * Conversations whose last send failed in a way that passes on its own
198
+ * (the server out of reach, a timeout, an error on the server's side):
199
+ * when each is tried again (engram#596). Like createRetry, a restart only
200
+ * means an earlier retry; the rows themselves stay queued in daemon.db.
201
+ */
202
+ private sendRetry;
203
+ /** Opt-out deletes that failed the same way: when each is tried again. */
204
+ private deleteRetry;
205
+ /**
206
+ * The key in use may not delete conversations: the server refused an
207
+ * opt-out's delete for want of the 'delete' permission (engram#597). A
208
+ * key's permissions don't change, so its deletes are kept and not tried
209
+ * again with it: they used to be retried, and logged, for as long as the
210
+ * key was in use. A different key tries again, and so does a restart.
211
+ */
212
+ private deletesRefused;
213
+ /** Rows orphaned by a pre-#370 daemon are adopted once per process. */
214
+ private orphansAdopted;
215
+ /**
216
+ * Sends are waiting for the vault (engram#588), and why, as last said: the
217
+ * key can't be read (engram#677), it decrypts none of the vault's secrets
218
+ * (engram#694), or the vault isn't read yet. A new why is said too.
219
+ */
220
+ private vaultHeld;
221
+ /** Per-conversation reduced batch size after a failed send (halves down to
222
+ * 1 so even a single pathological message eventually goes alone; restored
223
+ * to the full batch on the next success). */
224
+ private batchOverride;
225
+ /**
226
+ * With `keys`, `client` is the client for `keys.current` (built from it
227
+ * when null), and no `keys.current` starts the Syncer signed out, or
228
+ * waiting for config.json (`keys.unread`). Without `keys`, `client`
229
+ * sends everything, under FIXED_KEY_FP.
230
+ */
231
+ constructor(db: DaemonDb, client: Engram | null, scrubber?: SecretScrubber | null, keys?: KeySource | null);
232
+ /** Hand capture to the Syncer (engram#594), which runs it while a key is
233
+ * configured. Set before start(). */
234
+ setCaptureControl(control: CaptureControl): void;
235
+ /** Start syncing, and capturing while a key is configured. */
236
+ start(): void;
237
+ /**
238
+ * Stop capturing and syncing. Queued messages stay in daemon.db. A final
239
+ * flush() may follow; it sends only with the key still configured.
240
+ */
241
+ stop(): void;
242
+ private startCapture;
243
+ private stopCapture;
244
+ /**
245
+ * Read the configured account and act on a change (engram#594,
246
+ * engram#685): no key stops capture and sending at once, and a new key or
247
+ * server is used from the next request on. Calls made while a read is in
248
+ * progress share it.
249
+ */
250
+ checkKey(): Promise<void>;
251
+ private readKey;
252
+ /** An account is configured that isn't the one in use: a sign-in, or a
253
+ * change of key, server or account. */
254
+ private signIn;
255
+ /** Stop capturing and sending under the key in use. Nothing queued is
256
+ * touched. */
257
+ private deactivate;
258
+ /** No key is configured any more (`engram auth logout`). */
259
+ private signOut;
260
+ private logSignedOut;
261
+ /** Forget what was learned under the previous key: its pauses, retry
262
+ * timers and batch sizes say nothing about the next one. */
263
+ private resetForNewKey;
264
+ /**
265
+ * Hand what belongs to no key to the key in use: rows queued before
266
+ * daemon.db recorded keys, or while no key was in use. Once per key; a
267
+ * failure is retried on the next flush.
268
+ */
269
+ private claim;
270
+ private logProblem;
23
271
  /**
24
272
  * Called by the parser when new messages are ready for a session.
25
- * Queues messages locally; conversation creation is deferred to flush time
26
- * to avoid blocking the parser on network calls.
273
+ * Queues them in daemon.db before it returns; creating the conversation
274
+ * is deferred to flush time, so the parser never waits on the network.
275
+ *
276
+ * A failure to queue throws a QueueWriteError, synchronously (engram#584):
277
+ * the watcher stops at the line and reads it again later, instead of
278
+ * saving its resume offset past a message that was never queued. The
279
+ * returned promise covers only the server-side delete an in-chat opt-out
280
+ * starts, which retries on its own.
27
281
  */
28
282
  onMessages(sessionId: string, meta: SessionMeta, messages: ParsedMessage[]): Promise<void>;
29
283
  /**
30
284
  * The user said not to save this session. Semantics are "this session was
31
285
  * never captured": stop future capture, wipe the local queue, and delete
32
286
  * whatever already reached the server. The remote delete is persisted in
33
- * excluded_sessions.delete_pending and retried from the flush loop until
34
- * it succeeds — offline opt-out must still stick.
287
+ * remote_deletes and retried from the flush loop until it succeeds —
288
+ * offline opt-out must still stick. A copy synced under another key is
289
+ * deleted with that key's account's key (engram#594), once it signs in
290
+ * again.
291
+ *
292
+ * The local part happens before this returns, in one transaction; if it
293
+ * fails, the QueueWriteError stops the watcher at the opt-out line, so
294
+ * nothing after it is read until the opt-out has taken (engram#584).
35
295
  */
36
296
  private excludeSession;
37
- /** Delete server-side conversations for opted-out sessions; keeps
38
- * delete_pending until the server confirms. */
297
+ /** Delete server-side conversations for opted-out sessions, each with the
298
+ * key of the account it is in; a delete is kept until the server
299
+ * confirms. */
39
300
  private retryRemoteDeletes;
40
- startFlushLoop(): void;
41
- stopFlushLoop(): void;
42
- /** Flush all pending messages to the API. */
301
+ /**
302
+ * Flush all pending messages to the API, with the key configured now. A
303
+ * call while a flush is in progress returns at once.
304
+ */
43
305
  flush(): Promise<void>;
44
- /** Create conversations for sessions that were deferred during parsing. */
306
+ /** Settles once no flush is in progress: shutdown waits for it before
307
+ * closing daemon.db, so a send that went through is recorded as sent. */
308
+ idle(): Promise<void>;
309
+ private runFlush;
310
+ private flushWith;
311
+ /**
312
+ * Ask the server, once per account, which of the other keys something
313
+ * here is queued or mapped under are keys of this account (engram#594): a
314
+ * rotated or re-issued key. Theirs is handed to this account, so it is
315
+ * sent with this key, and a session continues in the conversation it
316
+ * already has. The rest keep waiting for their own account. Only what was
317
+ * queued for this account's server is asked about (ownershipCandidates,
318
+ * engram#685), and only key fingerprints go to the server, never a key.
319
+ * When the server can't answer, it is asked again on OWNERSHIP_RETRY_MS.
320
+ */
321
+ private checkOwnership;
322
+ private retryOwnership;
323
+ /** While a session with a conversation under an earlier key waits for the
324
+ * server to say whether that key is this account's (OWNERSHIP_GATE_MS). */
325
+ private awaitingOwnership;
326
+ /** Say how many queued messages wait for another account: another API
327
+ * key's, or the same key's on another server (engram#685). */
328
+ private logHeld;
329
+ /**
330
+ * Create conversations for sessions that were deferred during parsing.
331
+ * Every session stays in daemon.db until its conversation exists and its
332
+ * messages have moved onto it — a failure here only means "try again".
333
+ * Returns false while any session is still waiting on a failed create.
334
+ */
45
335
  private createPendingConversations;
46
- /** Per-conversation reduced batch size after a failed send (halves down to
47
- * 1 so even a single pathological message eventually goes alone; restored
48
- * to the full batch on the next success). */
49
- private batchOverride;
336
+ private createPendingConversation;
337
+ /** Pace retries for a session whose create failed: 30s → 2m → 10m …
338
+ * Returns the wait. */
339
+ private deferCreate;
340
+ /**
341
+ * Send what is queued for one conversation, batch by batch. `recheck`:
342
+ * Engram said it doesn't have the conversation (engram#593), and this
343
+ * checks again with one message before anything more is sent.
344
+ */
50
345
  private flushConversation;
51
346
  /**
52
- * Enter billing backoff — escalates each time: 5min → 15min → 1hr.
347
+ * Stop sending a conversation Engram said it doesn't have (engram#593).
348
+ * Its messages stay in daemon.db; the flush loop leaves it out until it
349
+ * is due to be checked again (GONE_RECHECK_MS).
350
+ */
351
+ private setAside;
352
+ /** The conversations Engram said it doesn't have that are due to be
353
+ * checked again, with messages queued under `run`'s key (engram#593). */
354
+ private dueRechecks;
355
+ /**
356
+ * Whether queued messages may be sent: the vault has been read through the
357
+ * current key, so the scrub before each send covers it (engram#588). Until
358
+ * then nothing is sent — captures keep queueing in daemon.db — and the
359
+ * status says why.
360
+ */
361
+ private vaultReady;
362
+ private holdForVault;
363
+ /**
364
+ * Scrub queued rows against the set held now, and write the scrubbed text
365
+ * back so the queue stops holding the value too. A failed write-back
366
+ * doesn't stop the send: what's sent is scrubbed either way.
367
+ */
368
+ private rescrub;
369
+ /**
370
+ * Enter billing backoff — escalates each time it is entered:
371
+ * 5min → 15min → 1hr for a plan limit, 1min → 2min → 5min for a full
372
+ * memory (see STORAGE_FULL_BACKOFF_MS).
53
373
  *
54
374
  * A full memory is separated out here. It arrives as a 402 like any other
55
375
  * billing error, but retrying can never clear it on its own: it needs an
@@ -59,6 +379,45 @@ export declare class Syncer {
59
379
  * even when the machine is offline.
60
380
  */
61
381
  private enterBillingBackoff;
382
+ /**
383
+ * The API key was rejected: pause sending on AUTH_RETRY_MS. Escalates once
384
+ * per pause, like the billing backoff: sends rejected together are one
385
+ * rejection. Captures keep queueing in daemon.db meanwhile.
386
+ */
387
+ private enterAuthHold;
388
+ private authHeld;
389
+ /**
390
+ * The server refused a request for when it came, not for what it carried:
391
+ * a rate limit, or a pause for maintenance (engram#596). Nothing is sent
392
+ * from this account until the wait is over, and each pause in a row
393
+ * without a request going through waits longer (retryDelay), up to a few
394
+ * minutes. Escalates once per pause, like the other holds: requests
395
+ * refused together are one refusal. The wait is drawn at random within
396
+ * its range, so daemons refused together don't all come back together.
397
+ */
398
+ private enterThrottle;
399
+ private throttled;
400
+ /**
401
+ * A request with `run`'s key went through: the key works, and the server
402
+ * takes requests again. A maintenance pause holds only sends, so only a
403
+ * send (`sent`) shows one is over. Lifts no pause early; the next one
404
+ * only starts over at the shortest wait.
405
+ */
406
+ private requestWentThrough;
407
+ /** True while sending with `run` must not or can only fail: its key is
408
+ * no longer the one configured, the key was rejected, the account is
409
+ * inside a billing backoff, or it waits out a rate limit or a pause. */
410
+ private halted;
411
+ /**
412
+ * When the daemon next tries what failed, for the status (engram#596):
413
+ * the end of a pause that holds every send, else the earliest turn of a
414
+ * conversation or a session waiting after a failure of its own. Null when
415
+ * nothing waits for a set time.
416
+ */
417
+ private nextRetryAt;
418
+ /** Unsent messages the key in use will send, for the status: 0 while
419
+ * signed out, and when daemon.db can't be read. */
420
+ private pendingCount;
62
421
  /** Reset billing backoff after a successful sync. */
63
422
  private resetBillingBackoff;
64
423
  private createConversation;
@@ -1 +1 @@
1
- {"version":3,"file":"syncer.d.ts","sourceRoot":"","sources":["../../src/daemon/syncer.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,gBAAgB,CAAC;AAC7C,OAAO,EAAE,QAAQ,EAAE,MAAM,SAAS,CAAC;AAInC,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,eAAe,CAAC;AACpD,OAAO,KAAK,EAAE,aAAa,EAAE,WAAW,EAAc,MAAM,YAAY,CAAC;AAqBzE;;;;GAIG;AACH,qBAAa,MAAM;IAoBf,OAAO,CAAC,QAAQ,CAAC,QAAQ;IAnB3B,OAAO,CAAC,EAAE,CAAW;IACrB,OAAO,CAAC,MAAM,CAAS;IACvB,OAAO,CAAC,KAAK,CAA+C;IAC5D,OAAO,CAAC,QAAQ,CAAS;IACzB,OAAO,CAAC,UAAU,CAAS;IAE3B,4BAA4B;IAC5B,OAAO,CAAC,mBAAmB,CAAa;IACxC,OAAO,CAAC,kBAAkB,CAAa;IAEvC,0EAA0E;IAC1E,OAAO,CAAC,eAAe,CAGnB;gBAGF,EAAE,EAAE,QAAQ,EACZ,MAAM,EAAE,MAAM,EACG,QAAQ,GAAE,cAAc,GAAG,IAAW;IAMzD;;;;OAIG;IACG,UAAU,CACd,SAAS,E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