@wirecat/cli-core 0.604.0 → 0.605.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 +61 -0
- package/dist/calendar/prepare.d.ts +4 -2
- package/dist/calendar/prepare.js +8 -8
- package/dist/cli/index.d.ts +1 -1
- package/dist/cli/index.js +1 -1
- package/dist/cli/messenger/contacts-command.js +3 -2
- package/dist/cli/messenger/doctor-command.js +14 -7
- package/dist/cli/messenger/store-maintenance-command.d.ts +34 -2
- package/dist/cli/messenger/store-maintenance-command.js +31 -4
- package/dist/cli/program.js +11 -2
- package/dist/cli/steps.js +9 -1
- package/dist/cli/store-recover.d.ts +10 -2
- package/dist/cli/store-recover.js +18 -4
- package/dist/domain/record-types.d.ts +61 -0
- package/dist/domain/record-types.js +26 -0
- package/dist/domain/records.d.ts +3 -5
- package/dist/domain/records.js +10 -13
- package/dist/entities/contact-activity.d.ts +4 -0
- package/dist/entities/contact-activity.js +1 -0
- package/dist/entities/contact-context.d.ts +20 -0
- package/dist/entities/contact-context.js +2 -0
- package/dist/entities/index.d.ts +22 -0
- package/dist/errors/failures.d.ts +17 -1
- package/dist/errors/failures.js +57 -1
- package/dist/errors/reasons.js +16 -0
- package/dist/mcp/tool.js +2 -1
- package/dist/runs/recording.js +13 -4
- package/dist/services/deps.js +4 -6
- package/dist/services/index.d.ts +1 -1
- package/dist/services/link-scorers/chat-senders.d.ts +3 -2
- package/dist/services/link-scorers/chat-senders.js +7 -4
- package/dist/services/link-scorers/mail-thread.js +1 -0
- package/dist/services/link-scorers/member-chats.js +1 -0
- package/dist/services/link-scorers/member-dialog.d.ts +1 -1
- package/dist/services/link-scorers/member-dialog.js +4 -3
- package/dist/services/link-scorers/scan.d.ts +5 -2
- package/dist/services/link-scorers/scan.js +98 -53
- package/dist/services/link-scorers/shared.d.ts +2 -2
- package/dist/services/link-scorers/shared.js +7 -4
- package/dist/services/link-scorers/types.d.ts +3 -0
- package/dist/services/metadata.d.ts +1 -1
- package/dist/services/person-context.d.ts +24 -7
- package/dist/services/person-context.js +45 -11
- package/dist/services/person-preparation.js +4 -5
- package/dist/store/damage.d.ts +3 -1
- package/dist/store/damage.js +19 -12
- package/dist/store/document-transfer/index.js +7 -0
- package/dist/store/index.d.ts +1 -0
- package/dist/store/index.js +1 -0
- package/dist/store/marks.d.ts +57 -0
- package/dist/store/marks.js +121 -0
- package/dist/store/project-transfer/preview.js +12 -7
- package/dist/store/recover.js +16 -1
- package/dist/store/sqlite/finance.d.ts +8 -1
- package/dist/store/sqlite/finance.js +10 -3
- package/dist/store/sqlite/open-store.js +4 -1
- package/dist/store/sqlite/own-identities.d.ts +1 -1
- package/dist/store/sqlite/own-identities.js +7 -5
- package/dist/store/sqlite/person-links.d.ts +5 -2
- package/dist/store/sqlite/person-links.js +24 -18
- package/dist/store/sqlite/person-separation.d.ts +5 -0
- package/dist/store/sqlite/person-separation.js +26 -0
- package/dist/store/sqlite/record-projectors.d.ts +44 -0
- package/dist/store/sqlite/record-projectors.js +72 -0
- package/dist/store/sqlite/records.js +12 -70
- package/dist/store/sqlite/transaction.js +2 -5
- package/dist/store/types.d.ts +5 -0
- package/package.json +3 -3
|
@@ -20,6 +20,8 @@ export const DEFAULT_THRESHOLD = 0.5;
|
|
|
20
20
|
/** A person ↔ person scan keeps the old rule's reach: one weak signal is enough to ask the owner. */
|
|
21
21
|
export const PERSON_THRESHOLD = 0.3;
|
|
22
22
|
export const DEFAULT_LIMIT = 20;
|
|
23
|
+
/** How many times one scan spreads from what it found; a project's links are rarely more than a few steps apart. */
|
|
24
|
+
export const MAX_HOPS = 4;
|
|
23
25
|
const projectOf = async (store, reference) => {
|
|
24
26
|
const ref = await canonicalProjectRef(store, reference);
|
|
25
27
|
const entry = (await store.knowledge.vocabulary({ kind: "project" })).entries.find((one) => one.id === ref.slice(8));
|
|
@@ -57,8 +59,9 @@ const pairKey = ({ from, to, kind }) => `${from}\u0000${to}\u0000${kind}`;
|
|
|
57
59
|
export const combinedScore = (weights) => Math.round((1 - weights.reduce((left, weight) => left * (1 - weight), 1)) * 1000) / 1000;
|
|
58
60
|
/**
|
|
59
61
|
* Runs the scorers for one project over the sources named, combines their signals per pair, and proposes what scores
|
|
60
|
-
* `threshold` or more, highest first, at most `limit` new ones.
|
|
61
|
-
*
|
|
62
|
+
* `threshold` or more, highest first, at most `limit` new ones. What scores `threshold` or more is spread from as if
|
|
63
|
+
* linked, up to `MAX_HOPS` times, so one run reaches a member's chats in every messenger. A pair a link joins, or that
|
|
64
|
+
* was rejected once, is never proposed; a pending one is updated in place. People are matched by identity only. With `write: false`
|
|
62
65
|
* nothing is written.
|
|
63
66
|
*/
|
|
64
67
|
export const scanProjectLinks = async (store, input) => {
|
|
@@ -93,60 +96,28 @@ export const scanProjectLinks = async (store, input) => {
|
|
|
93
96
|
nameOnly: 0,
|
|
94
97
|
unresolved: 0,
|
|
95
98
|
};
|
|
96
|
-
const
|
|
97
|
-
const
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
skipped.unresolved++;
|
|
113
|
-
continue;
|
|
114
|
-
}
|
|
115
|
-
const weight = Math.min(1, Math.max(0, candidate.weight));
|
|
116
|
-
const held = pairs.get(key) ?? { candidate, signals: new Map() };
|
|
117
|
-
const scorerId = `${scorer.id}@${scorer.version}`;
|
|
118
|
-
const before = held.signals.get(scorerId);
|
|
119
|
-
if (!before || before.weight < weight)
|
|
120
|
-
held.signals.set(scorerId, {
|
|
121
|
-
scorer: scorerId,
|
|
122
|
-
weight,
|
|
123
|
-
why: candidate.why.slice(0, 500),
|
|
124
|
-
...(candidate.purpose && scorerInput.purposes.includes(candidate.purpose)
|
|
125
|
-
? { purpose: candidate.purpose }
|
|
126
|
-
: {}),
|
|
127
|
-
});
|
|
128
|
-
held.candidate = {
|
|
129
|
-
...held.candidate,
|
|
130
|
-
evidence: [...new Set([...held.candidate.evidence, ...candidate.evidence])],
|
|
131
|
-
};
|
|
132
|
-
pairs.set(key, held);
|
|
133
|
-
}
|
|
99
|
+
const fixed = new Map();
|
|
100
|
+
const candidatesOf = async (scorer, seeds) => {
|
|
101
|
+
if (scorer.spreads)
|
|
102
|
+
return scorer.candidates({ ...scorerInput, confirmed: seeds });
|
|
103
|
+
const held = fixed.get(scorer) ?? (await scorer.candidates(scorerInput));
|
|
104
|
+
fixed.set(scorer, held);
|
|
105
|
+
return held;
|
|
106
|
+
};
|
|
107
|
+
let seeds = scorerInput.confirmed;
|
|
108
|
+
let collected = await collect(store, scorerInput, scorers, (scorer) => candidatesOf(scorer, seeds));
|
|
109
|
+
for (let hop = 0; hop < MAX_HOPS && scorers.some((one) => one.spreads); hop++) {
|
|
110
|
+
const grown = await seedsOf(store, seeds, collected.scored, threshold);
|
|
111
|
+
if (!grown)
|
|
112
|
+
break;
|
|
113
|
+
seeds = grown;
|
|
114
|
+
collected = await collect(store, scorerInput, scorers, (scorer) => candidatesOf(scorer, seeds));
|
|
134
115
|
}
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
const list = [...signals.values()].sort((a, b) => b.weight - a.weight || a.scorer.localeCompare(b.scorer));
|
|
138
|
-
const purpose = list.find((one) => one.purpose)?.purpose;
|
|
139
|
-
return {
|
|
140
|
-
candidate,
|
|
141
|
-
purpose,
|
|
142
|
-
signals: list.map(({ scorer, weight, why }) => ({ scorer, weight, why })),
|
|
143
|
-
score: combinedScore(list.map((one) => one.weight)),
|
|
144
|
-
};
|
|
145
|
-
})
|
|
146
|
-
.sort((a, b) => b.score - a.score || a.candidate.from.localeCompare(b.candidate.from));
|
|
116
|
+
skipped.nameOnly = collected.nameOnly;
|
|
117
|
+
skipped.unresolved = collected.unresolved;
|
|
147
118
|
const proposals = [];
|
|
148
119
|
let fresh = 0;
|
|
149
|
-
for (const { candidate, purpose, signals, score } of scored) {
|
|
120
|
+
for (const { candidate, purpose, signals, score } of collected.scored) {
|
|
150
121
|
const state = await store.knowledge.relationState(null, candidate);
|
|
151
122
|
if (!state) {
|
|
152
123
|
skipped.unresolved++;
|
|
@@ -207,6 +178,80 @@ export const scanProjectLinks = async (store, input) => {
|
|
|
207
178
|
}
|
|
208
179
|
return { project: project?.ref ?? null, proposals, skipped };
|
|
209
180
|
};
|
|
181
|
+
/** Every scorer's candidates, combined per pair and scored, highest first. */
|
|
182
|
+
const collect = async (store, input, scorers, candidatesOf) => {
|
|
183
|
+
const pairs = new Map();
|
|
184
|
+
const nameOnly = new Set();
|
|
185
|
+
let unresolved = 0;
|
|
186
|
+
for (const scorer of scorers) {
|
|
187
|
+
input.signal?.throwIfAborted();
|
|
188
|
+
for (const candidate of await candidatesOf(scorer)) {
|
|
189
|
+
const key = pairKey(candidate);
|
|
190
|
+
const verdict = await admissible(store, input, candidate);
|
|
191
|
+
if (verdict === "owner")
|
|
192
|
+
continue;
|
|
193
|
+
if (verdict === "name-only") {
|
|
194
|
+
nameOnly.add(key);
|
|
195
|
+
continue;
|
|
196
|
+
}
|
|
197
|
+
if (verdict !== "ok") {
|
|
198
|
+
unresolved++;
|
|
199
|
+
continue;
|
|
200
|
+
}
|
|
201
|
+
const weight = Math.min(1, Math.max(0, candidate.weight));
|
|
202
|
+
const held = pairs.get(key) ?? { candidate, signals: new Map() };
|
|
203
|
+
const scorerId = `${scorer.id}@${scorer.version}`;
|
|
204
|
+
const before = held.signals.get(scorerId);
|
|
205
|
+
if (!before || before.weight < weight)
|
|
206
|
+
held.signals.set(scorerId, {
|
|
207
|
+
scorer: scorerId,
|
|
208
|
+
weight,
|
|
209
|
+
why: candidate.why.slice(0, 500),
|
|
210
|
+
...(candidate.purpose && input.purposes.includes(candidate.purpose) ? { purpose: candidate.purpose } : {}),
|
|
211
|
+
});
|
|
212
|
+
held.candidate = {
|
|
213
|
+
...held.candidate,
|
|
214
|
+
evidence: [...new Set([...held.candidate.evidence, ...candidate.evidence])],
|
|
215
|
+
};
|
|
216
|
+
pairs.set(key, held);
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
const scored = [...pairs.values()]
|
|
220
|
+
.map(({ candidate, signals }) => {
|
|
221
|
+
const list = [...signals.values()].sort((a, b) => b.weight - a.weight || a.scorer.localeCompare(b.scorer));
|
|
222
|
+
const purpose = list.find((one) => one.purpose)?.purpose;
|
|
223
|
+
return {
|
|
224
|
+
candidate,
|
|
225
|
+
purpose,
|
|
226
|
+
signals: list.map(({ scorer, weight, why }) => ({ scorer, weight, why })),
|
|
227
|
+
score: combinedScore(list.map((one) => one.weight)),
|
|
228
|
+
};
|
|
229
|
+
})
|
|
230
|
+
.sort((a, b) => b.score - a.score || a.candidate.from.localeCompare(b.candidate.from));
|
|
231
|
+
return { scored, nameOnly: nameOnly.size, unresolved };
|
|
232
|
+
};
|
|
233
|
+
/**
|
|
234
|
+
* The links to spread from, grown by what scored `threshold` or more and was never rejected; `undefined` when
|
|
235
|
+
* nothing new was found. Spreading in one run reaches what the owner would otherwise confirm round after round.
|
|
236
|
+
*/
|
|
237
|
+
const seedsOf = async (store, seeds, scored, threshold) => {
|
|
238
|
+
const chats = new Set(seeds.chats);
|
|
239
|
+
const people = new Set(seeds.people);
|
|
240
|
+
const size = chats.size + people.size;
|
|
241
|
+
for (const { candidate, score } of scored) {
|
|
242
|
+
if (score < threshold || candidate.kind !== "member-of")
|
|
243
|
+
continue;
|
|
244
|
+
if (chats.has(candidate.from) || people.has(candidate.from))
|
|
245
|
+
continue;
|
|
246
|
+
if ((await store.knowledge.relationState(null, candidate))?.rejected !== false)
|
|
247
|
+
continue;
|
|
248
|
+
if (candidate.from.startsWith("chat:"))
|
|
249
|
+
chats.add(candidate.from);
|
|
250
|
+
else if (candidate.from.startsWith("person:"))
|
|
251
|
+
people.add(candidate.from);
|
|
252
|
+
}
|
|
253
|
+
return chats.size + people.size === size ? undefined : { chats: [...chats].sort(), people: [...people].sort() };
|
|
254
|
+
};
|
|
210
255
|
/** A person candidate stands only on an identity that is that person's; a chat only in a source the scan reads. */
|
|
211
256
|
const admissible = async (store, input, candidate) => {
|
|
212
257
|
if (candidate.from === candidate.to || !Number.isFinite(candidate.weight))
|
|
@@ -16,8 +16,8 @@ export declare const sameAccount: (a: AccountKey, b: {
|
|
|
16
16
|
}) => boolean;
|
|
17
17
|
/** The person an identity is, as `person:<uid>`; `undefined` when the store has no person for it. */
|
|
18
18
|
export declare const personRefOf: (store: MessageStore, provider: string, id: Id) => Promise<string | undefined>;
|
|
19
|
-
/** How many projects a person is a confirmed member of. */
|
|
20
|
-
export declare const projectsOf: (store: MessageStore, person: string) => Promise<number>;
|
|
19
|
+
/** How many projects a person is a confirmed member of, counting `project` whether confirmed or only found. */
|
|
20
|
+
export declare const projectsOf: (store: MessageStore, person: string, project: string) => Promise<number>;
|
|
21
21
|
/** Each confirmed member's identities in one messenger, by member. */
|
|
22
22
|
export declare const identitiesIn: (store: MessageStore, people: readonly string[], provider: string) => Promise<{
|
|
23
23
|
person: string;
|
|
@@ -30,10 +30,13 @@ export const personRefOf = async (store, provider, id) => {
|
|
|
30
30
|
const person = await store.people.personOf({ provider, id });
|
|
31
31
|
return person ? `person:${person.uid}` : undefined;
|
|
32
32
|
};
|
|
33
|
-
/** How many projects a person is a confirmed member of. */
|
|
34
|
-
export const projectsOf = async (store, person) => new Set(
|
|
35
|
-
|
|
36
|
-
.
|
|
33
|
+
/** How many projects a person is a confirmed member of, counting `project` whether confirmed or only found. */
|
|
34
|
+
export const projectsOf = async (store, person, project) => new Set([
|
|
35
|
+
project,
|
|
36
|
+
...(await store.knowledge.relations(null, person))
|
|
37
|
+
.filter((one) => one.confirmed && one.kind === "member-of" && one.from === person && one.to.startsWith("project:"))
|
|
38
|
+
.map((one) => one.to),
|
|
39
|
+
]).size;
|
|
37
40
|
/** Each confirmed member's identities in one messenger, by member. */
|
|
38
41
|
export const identitiesIn = async (store, people, provider) => {
|
|
39
42
|
const found = [];
|
|
@@ -31,6 +31,7 @@ export interface ScorerInput {
|
|
|
31
31
|
store: MessageStore;
|
|
32
32
|
/** `null` for a scan with no project, which only person ↔ person rules answer. */
|
|
33
33
|
project: ScannedProject | null;
|
|
34
|
+
/** The project's confirmed chats and people, and, within one scan, what the scan already found above threshold. */
|
|
34
35
|
confirmed: {
|
|
35
36
|
chats: string[];
|
|
36
37
|
people: string[];
|
|
@@ -48,6 +49,8 @@ export interface ScorerInput {
|
|
|
48
49
|
export interface LinkScorer {
|
|
49
50
|
readonly id: string;
|
|
50
51
|
readonly version: number;
|
|
52
|
+
/** Reads `confirmed`, so a scan runs it again each time the links it spreads from grow. */
|
|
53
|
+
readonly spreads?: boolean;
|
|
51
54
|
candidates(input: ScorerInput): Promise<LinkCandidate[]>;
|
|
52
55
|
}
|
|
53
56
|
export type Signal = NonNullable<ProposalRow["signals"]>[number];
|
|
@@ -14,12 +14,12 @@ export declare const metadataService: (deps: ServiceDeps) => {
|
|
|
14
14
|
dryRun?: boolean;
|
|
15
15
|
}) => Promise<{
|
|
16
16
|
items: ({
|
|
17
|
+
error?: undefined;
|
|
17
18
|
chatId: string;
|
|
18
19
|
tags: import("../domain/channel-tags.js").ChannelTagMatch[];
|
|
19
20
|
algorithm: string;
|
|
20
21
|
fetchedAt: string | null;
|
|
21
22
|
source: string;
|
|
22
|
-
error?: undefined;
|
|
23
23
|
} | {
|
|
24
24
|
tags?: undefined;
|
|
25
25
|
algorithm?: undefined;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { ChatKind, Id, Message, Provider } from "../domain/models.js";
|
|
2
|
-
import type { AccountKey, IdentityRef, LinkedIdentity, MessageStore, PersonRecord } from "../store/store.js";
|
|
2
|
+
import type { AccountKey, IdentityRef, LinkedIdentity, MessageStore, PersonRecord, StoredAccount } from "../store/store.js";
|
|
3
3
|
export declare const CONTEXT_MESSAGES = 10;
|
|
4
4
|
export declare const CONTEXT_BYTES: number;
|
|
5
5
|
export interface ContextMessage {
|
|
@@ -37,11 +37,16 @@ export interface ContextEvent {
|
|
|
37
37
|
}
|
|
38
38
|
/** `not_fetched`: nothing of its history was ever read into the store; `partial`: only some of it was. */
|
|
39
39
|
export type NotReadReason = "not_fetched" | "partial";
|
|
40
|
+
export type AccountScope = StoredAccount["scope"];
|
|
40
41
|
export interface PersonContext {
|
|
41
42
|
person: PersonRecord & {
|
|
42
43
|
provider: Provider;
|
|
43
44
|
id: Id;
|
|
44
45
|
};
|
|
46
|
+
/** Every account read, with its scope: only accounts of the scopes the caller may read. */
|
|
47
|
+
accounts: (AccountKey & {
|
|
48
|
+
scope: AccountScope;
|
|
49
|
+
})[];
|
|
45
50
|
shared: SharedChat[];
|
|
46
51
|
/** Each way, the newest in a messenger dialog whatever `limits` says; mail only within them. */
|
|
47
52
|
last: {
|
|
@@ -78,15 +83,23 @@ export interface ContextOptions {
|
|
|
78
83
|
since?: number;
|
|
79
84
|
/** ms: what splits upcoming events from past ones; the clock when absent. */
|
|
80
85
|
now?: number;
|
|
86
|
+
/**
|
|
87
|
+
* The account scopes read; the asked account's own scope when absent. One exact email address joins a personal
|
|
88
|
+
* and a work contact into one person, so a personal account must not read that person's work mail by default.
|
|
89
|
+
*/
|
|
90
|
+
scopes?: readonly AccountScope[];
|
|
81
91
|
}
|
|
82
92
|
export declare const toContext: (key: AccountKey, message: Message, chatTitle: string | null) => ContextMessage;
|
|
93
|
+
/** The stored accounts of `scopes`, or of the asked account's own scope; the asked account must be stored. */
|
|
94
|
+
export declare const readableAccounts: (store: MessageStore, asked: AccountKey, scopes?: readonly AccountScope[]) => Promise<StoredAccount[]>;
|
|
83
95
|
/**
|
|
84
|
-
* The accounts each identity is read in: for the messenger asked, only the account asked
|
|
85
|
-
* second account of the same messenger is somebody else's view; for a linked messenger, every
|
|
86
|
-
* of this store that saw that identity.
|
|
96
|
+
* The accounts each identity is read in, among `readable` only: for the messenger asked, only the account asked
|
|
97
|
+
* about — a second account of the same messenger is somebody else's view; for a linked messenger, every readable
|
|
98
|
+
* account of this store that saw that identity.
|
|
87
99
|
*/
|
|
88
|
-
export declare const readsOf: (record: PersonRecord, asked: AccountKey) => {
|
|
100
|
+
export declare const readsOf: (record: PersonRecord, asked: AccountKey, readable: readonly StoredAccount[]) => {
|
|
89
101
|
key: AccountKey;
|
|
102
|
+
scope: AccountScope;
|
|
90
103
|
identity: LinkedIdentity;
|
|
91
104
|
}[];
|
|
92
105
|
/**
|
|
@@ -94,11 +107,15 @@ export declare const readsOf: (record: PersonRecord, asked: AccountKey) => {
|
|
|
94
107
|
* only: nothing is fetched, nothing marked read. Identity is by id, never by name.
|
|
95
108
|
*/
|
|
96
109
|
export declare const personContext: (store: MessageStore, asked: AccountKey, reference: string, options?: ContextOptions) => Promise<PersonContext>;
|
|
97
|
-
/**
|
|
110
|
+
/**
|
|
111
|
+
* `personContext` of a person already found, read in the identities and accounts `reads` names; their calendar
|
|
112
|
+
* events in every calendar account of `readable`.
|
|
113
|
+
*/
|
|
98
114
|
export declare const recordContext: (store: MessageStore, record: PersonRecord, reads: {
|
|
99
115
|
key: AccountKey;
|
|
116
|
+
scope: AccountScope;
|
|
100
117
|
identity: LinkedIdentity;
|
|
101
|
-
}[], { messages, bytes, since, now }?: ContextOptions) => Promise<Omit<PersonContext, "person">>;
|
|
118
|
+
}[], readable: readonly StoredAccount[], { messages, bytes, since, now }?: ContextOptions) => Promise<Omit<PersonContext, "person">>;
|
|
102
119
|
/** `max:Ana` names a person in another messenger of this store; a reference with no prefix, this one. */
|
|
103
120
|
export declare const identityIn: (store: MessageStore, asked: AccountKey, reference: string) => Promise<IdentityRef>;
|
|
104
121
|
export declare const CHAT_MESSAGES = 20;
|
|
@@ -80,14 +80,30 @@ const eventsOnce = async (store, filter, limit) => {
|
|
|
80
80
|
}
|
|
81
81
|
return { items, cut: true };
|
|
82
82
|
};
|
|
83
|
+
const nameOf = ({ provider, account }) => `${provider}:${account}`;
|
|
84
|
+
/** The stored accounts of `scopes`, or of the asked account's own scope; the asked account must be stored. */
|
|
85
|
+
export const readableAccounts = async (store, asked, scopes) => {
|
|
86
|
+
const stored = await store.accounts.storedAccounts();
|
|
87
|
+
const own = stored.find((one) => nameOf(one) === nameOf(asked));
|
|
88
|
+
if (!own || (asked.scope !== undefined && asked.scope !== own.scope))
|
|
89
|
+
throw new CliError("not_found", `no ${asked.provider} account ${asked.account} in the store`);
|
|
90
|
+
const allowed = scopes ?? [own.scope];
|
|
91
|
+
return stored.filter((one) => allowed.includes(one.scope));
|
|
92
|
+
};
|
|
83
93
|
/**
|
|
84
|
-
* The accounts each identity is read in: for the messenger asked, only the account asked
|
|
85
|
-
* second account of the same messenger is somebody else's view; for a linked messenger, every
|
|
86
|
-
* of this store that saw that identity.
|
|
94
|
+
* The accounts each identity is read in, among `readable` only: for the messenger asked, only the account asked
|
|
95
|
+
* about — a second account of the same messenger is somebody else's view; for a linked messenger, every readable
|
|
96
|
+
* account of this store that saw that identity.
|
|
87
97
|
*/
|
|
88
|
-
export const readsOf = (record, asked
|
|
89
|
-
.
|
|
90
|
-
.
|
|
98
|
+
export const readsOf = (record, asked, readable) => {
|
|
99
|
+
const scopeOf = new Map(readable.map((one) => [nameOf(one), one.scope]));
|
|
100
|
+
return record.identities.flatMap((identity) => (identity.provider === asked.provider ? [asked.account] : identity.accounts).flatMap((account) => {
|
|
101
|
+
const scope = scopeOf.get(nameOf({ provider: identity.provider, account }));
|
|
102
|
+
return identity.accounts.includes(account) && scope !== undefined
|
|
103
|
+
? [{ key: { provider: identity.provider, account }, scope, identity }]
|
|
104
|
+
: [];
|
|
105
|
+
}));
|
|
106
|
+
};
|
|
91
107
|
/**
|
|
92
108
|
* What the store knows about one person, across every identity linked to them. Reads the store
|
|
93
109
|
* only: nothing is fetched, nothing marked read. Identity is by id, never by name.
|
|
@@ -97,13 +113,17 @@ export const personContext = async (store, asked, reference, options = {}) => {
|
|
|
97
113
|
const record = await store.people.personOf({ provider: asked.provider, id: found.id });
|
|
98
114
|
if (!record)
|
|
99
115
|
throw new CliError("not_found", `no person ${found.id} in the store`);
|
|
116
|
+
const readable = await readableAccounts(store, asked, options.scopes);
|
|
100
117
|
return {
|
|
101
118
|
person: { ...record, provider: asked.provider, id: found.id },
|
|
102
|
-
...(await recordContext(store, record, readsOf(record, asked), options)),
|
|
119
|
+
...(await recordContext(store, record, readsOf(record, asked, readable), readable, options)),
|
|
103
120
|
};
|
|
104
121
|
};
|
|
105
|
-
/**
|
|
106
|
-
|
|
122
|
+
/**
|
|
123
|
+
* `personContext` of a person already found, read in the identities and accounts `reads` names; their calendar
|
|
124
|
+
* events in every calendar account of `readable`.
|
|
125
|
+
*/
|
|
126
|
+
export const recordContext = async (store, record, reads, readable, { messages = CONTEXT_MESSAGES, bytes = CONTEXT_BYTES, since, now = Date.now() } = {}) => {
|
|
107
127
|
const recent = (message) => since === undefined || Date.parse(message.timestamp) >= since;
|
|
108
128
|
const shared = [];
|
|
109
129
|
const notRead = [];
|
|
@@ -165,9 +185,17 @@ export const recordContext = async (store, record, reads, { messages = CONTEXT_M
|
|
|
165
185
|
cut ||= named.hasMore;
|
|
166
186
|
}
|
|
167
187
|
const personId = Number(record.uid);
|
|
168
|
-
const
|
|
188
|
+
const calendars = [];
|
|
189
|
+
for (const one of readable)
|
|
190
|
+
if ((await store.calendar.calendars(one.id)).length)
|
|
191
|
+
calendars.push(one);
|
|
192
|
+
const accountIds = calendars.map(({ id }) => id);
|
|
193
|
+
const nothing = { items: [], cut: false };
|
|
194
|
+
const upcoming = accountIds.length ? await eventsOnce(store, { personId, accountIds, from: now }, messages) : nothing;
|
|
169
195
|
const upcomingIds = new Set(upcoming.items.map(({ id }) => id));
|
|
170
|
-
const past =
|
|
196
|
+
const past = accountIds.length
|
|
197
|
+
? await eventsOnce(store, { personId, accountIds, to: now, newest: true, ...(since === undefined ? {} : { from: since }) }, messages)
|
|
198
|
+
: nothing;
|
|
171
199
|
cut ||= upcoming.cut || past.cut;
|
|
172
200
|
direct.sort(newestFirst);
|
|
173
201
|
const each = [...direct, ...beyond].sort(newestFirst);
|
|
@@ -183,7 +211,13 @@ export const recordContext = async (store, record, reads, { messages = CONTEXT_M
|
|
|
183
211
|
};
|
|
184
212
|
cut ||= direct.length > messages || groups.length > messages || mentions.length > messages;
|
|
185
213
|
const kept = withinBytes(lists, bytes);
|
|
214
|
+
const read = [
|
|
215
|
+
...reads.map(({ key, scope }) => ({ ...key, scope })),
|
|
216
|
+
...calendars.map(({ provider, account, scope }) => ({ provider, account, scope })),
|
|
217
|
+
];
|
|
218
|
+
const accounts = [...new Map(read.map((one) => [nameOf(one), one])).values()].sort((a, b) => nameOf(a).localeCompare(nameOf(b)));
|
|
186
219
|
return {
|
|
220
|
+
accounts,
|
|
187
221
|
shared: shared.sort((a, b) => Date.parse(b.lastMessageAt ?? "") - Date.parse(a.lastMessageAt ?? "")),
|
|
188
222
|
last: {
|
|
189
223
|
fromThem: each.find((message) => !message.outgoing) ?? null,
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { CliError } from "@wirecat/cli-base";
|
|
2
2
|
import { parseMeetingReference } from "../meetings/index.js";
|
|
3
|
-
import { CONTEXT_BYTES, CONTEXT_MESSAGES, personContext,
|
|
3
|
+
import { CONTEXT_BYTES, CONTEXT_MESSAGES, personContext, readableAccounts, toContext, } from "./person-context.js";
|
|
4
4
|
import { personMeetingContext } from "./person-meeting-context.js";
|
|
5
5
|
export const PREPARE_BYTES = 96 * 1024;
|
|
6
6
|
const MEETINGS = 5;
|
|
@@ -34,7 +34,6 @@ export const personPreparation = async (store, asked, reference, options = {}) =
|
|
|
34
34
|
coverage: { included: items.length, hasMore: over || cut, cutBy: cut ? "bytes" : over ? "limit" : null },
|
|
35
35
|
};
|
|
36
36
|
};
|
|
37
|
-
const reads = readsOf(context.person, asked);
|
|
38
37
|
const partial = new Set(context.notRead.map((chat) => `${chat.provider}/${chat.account}/${chat.chatId}`));
|
|
39
38
|
const waiting = [];
|
|
40
39
|
for (const chat of context.shared.filter((one) => one.kind === "dialog")) {
|
|
@@ -51,10 +50,10 @@ export const personPreparation = async (store, asked, reference, options = {}) =
|
|
|
51
50
|
});
|
|
52
51
|
}
|
|
53
52
|
waiting.sort((a, b) => Date.parse(b.message.timestamp) - Date.parse(a.message.timestamp));
|
|
54
|
-
const accounts =
|
|
53
|
+
const accounts = context.accounts.map(({ provider, account }) => ({ provider, account }));
|
|
55
54
|
const held = await store.people.preparation(context.person.uid, { accounts, limit });
|
|
56
|
-
const
|
|
57
|
-
const meetings = await meetingsOf(store, context.person.uid,
|
|
55
|
+
const readable = await readableAccounts(store, asked, options.scopes);
|
|
56
|
+
const meetings = await meetingsOf(store, context.person.uid, readable, options.since, () => left);
|
|
58
57
|
const sections = {
|
|
59
58
|
waiting: budgeted({ items: waiting, hasMore: false }),
|
|
60
59
|
tasks: budgeted(held.tasks),
|
package/dist/store/damage.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { StoreHealth } from "./health.js";
|
|
2
|
+
import type { DamageMark } from "./marks.js";
|
|
2
3
|
/**
|
|
3
4
|
* What a damaged store means, in the owner's words: one structure the CLIs render and the desktop app can show as it
|
|
4
5
|
* is. Built from a health check only, so it never reads the damaged file again.
|
|
@@ -16,4 +17,5 @@ export interface DamageExplanation {
|
|
|
16
17
|
* vectors, which a search by meaning computes again for any text that has none.
|
|
17
18
|
*/
|
|
18
19
|
export declare const isDerivedTable: (table: string, searchIndexes: readonly string[]) => boolean;
|
|
19
|
-
|
|
20
|
+
/** `mark`: a run saw the damage; the quick check reads too little to see it, so it alone can make a store damaged. */
|
|
21
|
+
export declare const explainDamage: (health: StoreHealth, command: string, mark?: DamageMark | null) => DamageExplanation | null;
|
package/dist/store/damage.js
CHANGED
|
@@ -8,8 +8,10 @@ export const isDerivedTable = (table, searchIndexes) => DERIVED.has(table) ||
|
|
|
8
8
|
table.startsWith("vector_index_") ||
|
|
9
9
|
searchIndexes.some((index) => table === index || table.startsWith(`${index}_`));
|
|
10
10
|
const WIRECAT_UNITS = ["memo-*", "mail-*", "tg-*", "max-*", "wirecat-*"];
|
|
11
|
-
|
|
12
|
-
|
|
11
|
+
const seen = (mark) => `\`${mark.command}\` found the store damaged at ${mark.at} (${mark.error})`;
|
|
12
|
+
/** `mark`: a run saw the damage; the quick check reads too little to see it, so it alone can make a store damaged. */
|
|
13
|
+
export const explainDamage = (health, command, mark = null) => {
|
|
14
|
+
if (health.ok && !mark)
|
|
13
15
|
return null;
|
|
14
16
|
const indexNames = (health.searchIndexes ?? []).map((index) => index.name);
|
|
15
17
|
const badIndexes = (health.searchIndexes ?? []).filter((index) => !index.ok).map((index) => index.name);
|
|
@@ -18,14 +20,17 @@ export const explainDamage = (health, command) => {
|
|
|
18
20
|
const derived = [...new Set([...tables.filter((table) => isDerivedTable(table, indexNames)), ...badIndexes])];
|
|
19
21
|
const unopened = health.error !== undefined;
|
|
20
22
|
const json = (health.invalidJson ?? []).map(({ table, column }) => `${table}.${column}`);
|
|
21
|
-
const
|
|
22
|
-
?
|
|
23
|
-
:
|
|
24
|
-
?
|
|
25
|
-
:
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
23
|
+
const checked = health.ok
|
|
24
|
+
? `${seen(mark)}; the quick check reads too little to see it, and a deep check that passes clears it`
|
|
25
|
+
: unopened
|
|
26
|
+
? `the store file cannot be opened: ${health.error}`
|
|
27
|
+
: health.checked === "quick"
|
|
28
|
+
? "the store file's header or schema is damaged"
|
|
29
|
+
: `the store file is damaged: ${health.problems ?? 0} problems` +
|
|
30
|
+
(sources.length ? ` in the data of ${named(sources)}` : "") +
|
|
31
|
+
(derived.length ? `${sources.length ? ", and" : " in"} the search data of ${named(derived)}` : "") +
|
|
32
|
+
(json.length ? `; values that are not valid JSON in ${named(json)}` : "");
|
|
33
|
+
const what = mark && !health.ok ? `${checked}; ${seen(mark)}` : checked;
|
|
29
34
|
const impact = [
|
|
30
35
|
...(sources.length
|
|
31
36
|
? [`some rows of ${named(sources)} cannot be read; a recovery keeps every row it can read and names the rest`]
|
|
@@ -36,7 +41,9 @@ export const explainDamage = (health, command) => {
|
|
|
36
41
|
...(json.length
|
|
37
42
|
? [`a read of a row with a broken JSON value fails ("malformed JSON"); a recovery clears those values`]
|
|
38
43
|
: []),
|
|
39
|
-
|
|
44
|
+
mark
|
|
45
|
+
? "reads that do not touch a damaged page work; every command that writes the store refuses until it is recovered"
|
|
46
|
+
: "reads that do not touch a damaged page work; a write that touches one fails until the store is recovered",
|
|
40
47
|
];
|
|
41
48
|
return {
|
|
42
49
|
what,
|
|
@@ -49,7 +56,7 @@ export const explainDamage = (health, command) => {
|
|
|
49
56
|
`journalctl --user ${WIRECAT_UNITS.map((unit) => `-u '${unit}'`).join(" ")} --since -14d | grep -i -e malformed -e corrupt -e 'disk full'`,
|
|
50
57
|
"journalctl -k --since -14d | grep -i -e 'I/O error' -e nvme -e ata",
|
|
51
58
|
`df -h ${health.path.replace(/\/[^/]*$/, "")}`,
|
|
52
|
-
...(health.checked === "quick" ? [`${command} store recover`] : []),
|
|
59
|
+
...(health.checked === "quick" || health.ok ? [`${command} store recover`] : []),
|
|
53
60
|
],
|
|
54
61
|
impact,
|
|
55
62
|
externalUnaffected: "only this computer's copy is damaged: Telegram, MAX, the mail servers and Google Drive keep their own data, " +
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { realpathSync, statSync } from "node:fs";
|
|
2
2
|
import { resolve } from "node:path";
|
|
3
3
|
import { CliError } from "@wirecat/cli-base";
|
|
4
|
+
import { damagedFile } from "../../errors/failures.js";
|
|
4
5
|
import { MIGRATIONS, migrate } from "../migrations.js";
|
|
5
6
|
import { applyTransfer } from "../sqlite/document-transfer/apply.js";
|
|
6
7
|
import { planTransfer } from "../sqlite/document-transfer/plan.js";
|
|
@@ -75,6 +76,9 @@ const openPreviewTarget = async (path) => {
|
|
|
75
76
|
};
|
|
76
77
|
export const previewDocumentTransfer = async (options) => {
|
|
77
78
|
const location = locations(options);
|
|
79
|
+
return damagedFile([location.source, location.destination], () => previewWith(location, options));
|
|
80
|
+
};
|
|
81
|
+
const previewWith = async (location, options) => {
|
|
78
82
|
const source = await openSource(location.source);
|
|
79
83
|
try {
|
|
80
84
|
const target = await openPreviewTarget(location.destination);
|
|
@@ -93,6 +97,9 @@ export const applyDocumentTransfer = async (options) => {
|
|
|
93
97
|
if (!/^[a-f0-9]{64}$/.test(options.expected_fingerprint))
|
|
94
98
|
throw new CliError("validation_error", "apply requires the fingerprint returned by a transfer preview");
|
|
95
99
|
const location = locations(options);
|
|
100
|
+
return damagedFile([location.source, location.destination], () => applyWith(location, options));
|
|
101
|
+
};
|
|
102
|
+
const applyWith = async (location, options) => {
|
|
96
103
|
const source = await openSource(location.source);
|
|
97
104
|
try {
|
|
98
105
|
const snapshot = transferSnapshot(source.database, options.collection);
|
package/dist/store/index.d.ts
CHANGED
|
@@ -20,6 +20,7 @@ export { EVIDENCE_FINDINGS_KIND, type EvidenceFindingsBatch, type EvidenceFindin
|
|
|
20
20
|
export { type DamagedTable, type InvalidJson, QUICK_HEALTH_CHECKS, type SearchIndexHealth, type StoreHealth, storeHealth, } from "./health.js";
|
|
21
21
|
export { ORGANIZATIONS_QUERY, organization, PROJECTS_QUERY, project, REMINDERS_QUERY, reminder, } from "./knowledge-query.js";
|
|
22
22
|
export { MAIL_FINDINGS_KIND, type MailFindingsBatch, type MailFindingsCheck, type MailFindingsStore, } from "./mail-findings.js";
|
|
23
|
+
export { clearDamageMark, type DamageMark, damageMarkPath, type HealthMark, healthMarkPath, markStoreDamaged, readDamageMark, readHealthMark, recordDeepCheck, } from "./marks.js";
|
|
23
24
|
export type { MeetingFindingsBatch, MeetingFindingsStore } from "./meeting-findings.js";
|
|
24
25
|
export { MIGRATIONS, type Migration, migrate } from "./migrations.js";
|
|
25
26
|
export { openCache } from "./open.js";
|
package/dist/store/index.js
CHANGED
|
@@ -12,6 +12,7 @@ export { EVIDENCE_FINDINGS_KIND, validateEvidenceFindings, } from "./evidence-fi
|
|
|
12
12
|
export { QUICK_HEALTH_CHECKS, storeHealth, } from "./health.js";
|
|
13
13
|
export { ORGANIZATIONS_QUERY, organization, PROJECTS_QUERY, project, REMINDERS_QUERY, reminder, } from "./knowledge-query.js";
|
|
14
14
|
export { MAIL_FINDINGS_KIND, } from "./mail-findings.js";
|
|
15
|
+
export { clearDamageMark, damageMarkPath, healthMarkPath, markStoreDamaged, readDamageMark, readHealthMark, recordDeepCheck, } from "./marks.js";
|
|
15
16
|
export { MIGRATIONS, migrate } from "./migrations.js";
|
|
16
17
|
export { openCache } from "./open.js";
|
|
17
18
|
export { storePath } from "./path.js";
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import type { StoreHealth } from "./health.js";
|
|
2
|
+
/**
|
|
3
|
+
* Both marks live beside the store, not in it: the store may be the file that cannot be read. Each holds no content,
|
|
4
|
+
* only times, command names and SQLite's own message.
|
|
5
|
+
*/
|
|
6
|
+
export declare const damageMarkPath: (store: string) => string;
|
|
7
|
+
export declare const healthMarkPath: (store: string) => string;
|
|
8
|
+
/** A run saw SQLite say the store is damaged. Every writable open refuses while it names the file in place. */
|
|
9
|
+
export interface DamageMark {
|
|
10
|
+
at: string;
|
|
11
|
+
command: string;
|
|
12
|
+
cli_version?: string;
|
|
13
|
+
run_id?: string;
|
|
14
|
+
error: string;
|
|
15
|
+
/** The file it was seen in: a store restored, reset or recovered since is another file, and the mark is stale. */
|
|
16
|
+
inode: number;
|
|
17
|
+
}
|
|
18
|
+
/** The newest deep check of the store, and the time of the newest one that passed: the last known good. */
|
|
19
|
+
export interface HealthMark {
|
|
20
|
+
checked_at: string;
|
|
21
|
+
ok: boolean;
|
|
22
|
+
problems: number;
|
|
23
|
+
schema: number | null;
|
|
24
|
+
cli: string;
|
|
25
|
+
cli_version?: string;
|
|
26
|
+
last_ok_at: string | null;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Leaves the damage mark when `error` is SQLite saying the store is damaged; the first sighting is kept. Never
|
|
30
|
+
* throws: the failure being reported matters more than the mark.
|
|
31
|
+
*/
|
|
32
|
+
export declare const markStoreDamaged: (error: unknown, { store, command, version, runId, now, }: {
|
|
33
|
+
store: string;
|
|
34
|
+
command: string;
|
|
35
|
+
version?: string;
|
|
36
|
+
runId?: string;
|
|
37
|
+
now?: () => number;
|
|
38
|
+
}) => boolean;
|
|
39
|
+
/** The mark, or null; a mark left by another file at this path is removed. An unreadable mark still counts. */
|
|
40
|
+
export declare const readDamageMark: (store: string) => DamageMark | null;
|
|
41
|
+
export declare const clearDamageMark: (store: string) => void;
|
|
42
|
+
/** Thrown by a writable open of a store a run found damaged, so timers fail fast instead of writing into it. */
|
|
43
|
+
export declare const refuseDamagedStore: (store: string, cli?: string) => void;
|
|
44
|
+
export declare const readHealthMark: (store: string) => HealthMark | null;
|
|
45
|
+
/**
|
|
46
|
+
* Records a deep check of the store. One that passed also removes the damage mark: the file reads whole. Returns the
|
|
47
|
+
* mark it replaced, whose `last_ok_at` is the last known good before this check, and the damage mark it removed.
|
|
48
|
+
*/
|
|
49
|
+
export declare const recordDeepCheck: (store: string, health: Pick<StoreHealth, "ok" | "problems">, { cli, version, schema, now, }: {
|
|
50
|
+
cli: string;
|
|
51
|
+
version?: string;
|
|
52
|
+
schema: number | null;
|
|
53
|
+
now?: () => number;
|
|
54
|
+
}) => {
|
|
55
|
+
previous: HealthMark | null;
|
|
56
|
+
cleared: DamageMark | null;
|
|
57
|
+
};
|