@wirecat/cli-core 0.631.0 → 0.633.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 +48 -0
- package/dist/calendar/mcp/index.d.ts +12 -12
- package/dist/cli/messenger/archive-commands.js +4 -1
- package/dist/cli/messenger/context.d.ts +2 -0
- package/dist/cli/messenger/reply-stats-command.js +1 -1
- package/dist/cli/messenger/serve-relay.d.ts +1 -0
- package/dist/cli/messenger/stats-answers.d.ts +5 -2
- package/dist/cli/messenger/stats-answers.js +8 -2
- package/dist/cli/messenger/store-import-command.d.ts +4 -0
- package/dist/cli/messenger/store-import-command.js +166 -0
- package/dist/cli/messenger/stored.d.ts +7 -1
- package/dist/cli/messenger/stored.js +1 -1
- package/dist/cli/messenger/tasks-command.js +5 -3
- package/dist/domain/test-data.d.ts +15 -0
- package/dist/domain/test-data.js +18 -0
- package/dist/entities/chat-stats.d.ts +2 -0
- package/dist/entities/chat-stats.js +2 -0
- package/dist/entities/index.d.ts +18 -2
- package/dist/entities/index.js +3 -2
- package/dist/entities/store-maintenance.d.ts +16 -0
- package/dist/entities/store-maintenance.js +10 -0
- package/dist/errors/reasons.js +9 -0
- package/dist/kit/seed.js +6 -7
- package/dist/search/lucene/registry.js +11 -0
- package/dist/sends/permissions.js +1 -0
- package/dist/services/proposals/kinds/identity-split.d.ts +4 -0
- package/dist/services/proposals/kinds/identity-split.js +65 -0
- package/dist/services/proposals/registry.js +2 -0
- package/dist/services/reply-stats.js +11 -3
- package/dist/services/task-stats.d.ts +7 -2
- package/dist/services/task-stats.js +53 -20
- package/dist/services/tasks.d.ts +1 -0
- package/dist/services/tasks.js +16 -4
- package/dist/store/decisions.js +3 -1
- package/dist/store/drivers/bun-sqlite.js +2 -0
- package/dist/store/drivers/node-sqlite.js +2 -0
- package/dist/store/identity-splits.d.ts +60 -0
- package/dist/store/identity-splits.js +5 -0
- package/dist/store/index.d.ts +2 -0
- package/dist/store/index.js +2 -0
- package/dist/store/path.d.ts +3 -0
- package/dist/store/path.js +25 -6
- package/dist/store/people.d.ts +23 -0
- package/dist/store/people.js +6 -0
- package/dist/store/sqlite/accounts.js +4 -0
- package/dist/store/sqlite/crm-contacts.js +5 -1
- package/dist/store/sqlite/identity-link-rows.d.ts +5 -0
- package/dist/store/sqlite/identity-link-rows.js +5 -3
- package/dist/store/sqlite/identity-split-proposals.d.ts +29 -0
- package/dist/store/sqlite/identity-split-proposals.js +195 -0
- package/dist/store/sqlite/lucene-fields.js +3 -0
- package/dist/store/sqlite/person-doubts.d.ts +36 -0
- package/dist/store/sqlite/person-doubts.js +229 -0
- package/dist/store/sqlite/proposed-actions.js +6 -0
- package/dist/store/sqlite/reply-times.d.ts +3 -1
- package/dist/store/sqlite/reply-times.js +11 -6
- package/package.json +1 -1
- package/parity.json +3 -0
package/dist/store/path.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
+
import { realpathSync } from "node:fs";
|
|
1
2
|
import { userInfo } from "node:os";
|
|
2
|
-
import { isAbsolute, join, relative, resolve } from "node:path";
|
|
3
|
+
import { basename, dirname, isAbsolute, join, relative, resolve } from "node:path";
|
|
3
4
|
import { resolvePaths } from "@wirecat/cli-base";
|
|
4
5
|
/** The folder the CLIs share: the store, its backups and the models. `WIRECAT_STATE_DIR` moves all of it. */
|
|
5
6
|
export const wirecatDirectory = (env = process.env, platform) => resolvePaths({ appName: "WireCat", prefix: "WIRECAT", env, ...(platform ? { platform } : {}) }).state;
|
|
@@ -11,7 +12,7 @@ export const wirecatDirectory = (env = process.env, platform) => resolvePaths({
|
|
|
11
12
|
export const storePath = (env = process.env) => env.MESSAGING_STORE ?? join(wirecatDirectory(env), "wirecat.db");
|
|
12
13
|
// The account's home from the user database, not HOME: the test sandbox moves HOME, and a worker thread's
|
|
13
14
|
// `os.homedir()` still answers the real one (the 2026-10-18 leak).
|
|
14
|
-
const accountHome = () => {
|
|
15
|
+
export const accountHome = () => {
|
|
15
16
|
try {
|
|
16
17
|
return userInfo().homedir;
|
|
17
18
|
}
|
|
@@ -19,12 +20,30 @@ const accountHome = () => {
|
|
|
19
20
|
return undefined;
|
|
20
21
|
}
|
|
21
22
|
};
|
|
23
|
+
/** Whether `file` is in the owner's real WireCat folder, whatever HOME or MESSAGING_STORE this process was given. */
|
|
24
|
+
export const inOwnerFolder = (file, home = accountHome()) => {
|
|
25
|
+
if (!home || file === ":memory:")
|
|
26
|
+
return false;
|
|
27
|
+
const owner = wirecatDirectory({ HOME: home, USERPROFILE: home });
|
|
28
|
+
const within = (path, folder) => {
|
|
29
|
+
const inside = relative(folder, path);
|
|
30
|
+
return !(inside.startsWith("..") || isAbsolute(inside));
|
|
31
|
+
};
|
|
32
|
+
return within(resolve(file), owner) || within(settled(resolve(file)), settled(owner));
|
|
33
|
+
};
|
|
34
|
+
/** The path with every link in its existing part followed, so a link into the owner's folder is seen as one. */
|
|
35
|
+
const settled = (path) => {
|
|
36
|
+
try {
|
|
37
|
+
return realpathSync(path);
|
|
38
|
+
}
|
|
39
|
+
catch {
|
|
40
|
+
const parent = dirname(path);
|
|
41
|
+
return parent === path ? path : join(settled(parent), basename(path));
|
|
42
|
+
}
|
|
43
|
+
};
|
|
22
44
|
/** Throws when a test is about to open a file in the owner's real WireCat folder. */
|
|
23
45
|
export const refuseOwnerStoreInTests = (file, env = process.env, home = accountHome()) => {
|
|
24
|
-
if (!env.VITEST || !
|
|
25
|
-
return;
|
|
26
|
-
const inside = relative(wirecatDirectory({ HOME: home, USERPROFILE: home }), resolve(file));
|
|
27
|
-
if (inside.startsWith("..") || isAbsolute(inside))
|
|
46
|
+
if (!env.VITEST || !inOwnerFolder(file, home))
|
|
28
47
|
return;
|
|
29
48
|
throw new Error(`a test tried to open ${file}, in the owner's real WireCat folder; give it a MESSAGING_STORE or HOME of its own`);
|
|
30
49
|
};
|
package/dist/store/people.d.ts
CHANGED
|
@@ -2,9 +2,11 @@ import type { Contact, Id, Page, PersonAlias, Provider } from "../domain/models.
|
|
|
2
2
|
import type { PersonKind } from "../domain/person-kinds.js";
|
|
3
3
|
import type { PeopleLookup } from "../resolve.js";
|
|
4
4
|
import type { IdentityLinkDecision, IdentityLinkFilter, IdentityLinkInput, IdentityLinkRow, MatchScan } from "./identity-links.js";
|
|
5
|
+
import type { IdentitySplitDecision, IdentitySplitFilter, IdentitySplitInput, IdentitySplitRow } from "./identity-splits.js";
|
|
5
6
|
import type { ContactFilter } from "./sqlite/identities.js";
|
|
6
7
|
import type { StoreContext } from "./sqlite/open.js";
|
|
7
8
|
import { type PeopleList, type PeopleListOptions, type SimilarPerson } from "./sqlite/people-list.js";
|
|
9
|
+
import { type PersonDoubts } from "./sqlite/person-doubts.js";
|
|
8
10
|
import type { PersonMerge } from "./sqlite/person-merges.js";
|
|
9
11
|
import { type PersonPreparationRead } from "./sqlite/person-preparation.js";
|
|
10
12
|
import { type ShownBecause } from "./sqlite/person-ties.js";
|
|
@@ -98,6 +100,27 @@ export interface PeopleStore extends AccountPeople {
|
|
|
98
100
|
items: IdentityLinkRow[];
|
|
99
101
|
hasMore: boolean;
|
|
100
102
|
}>;
|
|
103
|
+
/**
|
|
104
|
+
* Proposes to take identities off their person, or to undo a merge, as an `identity_split` only the owner decides:
|
|
105
|
+
* an agent's unlink or unmerge waits for the owner beside the matches. An open one for the same identities is given
|
|
106
|
+
* back; one the owner turned down is refused.
|
|
107
|
+
*/
|
|
108
|
+
proposeSplit(input: IdentitySplitInput): Promise<ProposedAction>;
|
|
109
|
+
/** The owner's decision on an `identity_split`; confirm is refused with `identity.stale` when the person changed. */
|
|
110
|
+
decideSplit(id: string, decision: IdentitySplitDecision): Promise<ProposedAction>;
|
|
111
|
+
/** The `identity_split` proposals, newest first; `proposed` unless `status` says. */
|
|
112
|
+
splitProposals(filter: IdentitySplitFilter): Promise<{
|
|
113
|
+
items: IdentitySplitRow[];
|
|
114
|
+
hasMore: boolean;
|
|
115
|
+
}>;
|
|
116
|
+
/**
|
|
117
|
+
* People that may be wrong, the owner first: the owner's person holding another name, test data, an address people
|
|
118
|
+
* with other names share, one person with different names and nothing in common. Each says why and how to fix it.
|
|
119
|
+
*/
|
|
120
|
+
doubts(page: {
|
|
121
|
+
limit: number;
|
|
122
|
+
offset: number;
|
|
123
|
+
}): Promise<PersonDoubts>;
|
|
101
124
|
/** The identity, and everyone linked with it, is the owner. */
|
|
102
125
|
linkToOwner(identity: IdentityRef): Promise<PersonRecord>;
|
|
103
126
|
/** A person known by name only, with no identity yet: someone to assign a task to before any message names them. */
|
package/dist/store/people.js
CHANGED
|
@@ -3,8 +3,10 @@ import { keysOver } from "./keys.js";
|
|
|
3
3
|
import { ownerPerson } from "./sqlite/actors.js";
|
|
4
4
|
import * as identities from "./sqlite/identities.js";
|
|
5
5
|
import * as identityProposals from "./sqlite/identity-proposals.js";
|
|
6
|
+
import * as identitySplits from "./sqlite/identity-split-proposals.js";
|
|
6
7
|
import { linkToOwner } from "./sqlite/own-identities.js";
|
|
7
8
|
import { countPeople, listPeople, similarPeople, } from "./sqlite/people-list.js";
|
|
9
|
+
import { personDoubts } from "./sqlite/person-doubts.js";
|
|
8
10
|
import * as personLinks from "./sqlite/person-links.js";
|
|
9
11
|
import { RULE, scanMatches } from "./sqlite/person-match-scan.js";
|
|
10
12
|
import * as personMerges from "./sqlite/person-merges.js";
|
|
@@ -123,6 +125,10 @@ export const peopleStoreOver = (context, keyed) => {
|
|
|
123
125
|
decideLink: async (id, decision) => identityProposals.decideIdentityLink(context, id, decision),
|
|
124
126
|
scanMatches: async (options) => scanMatches(context, options),
|
|
125
127
|
linkProposals: async (filter) => identityProposals.identityLinkRows(context, filter),
|
|
128
|
+
proposeSplit: async (input) => identitySplits.proposeIdentitySplit(context, input),
|
|
129
|
+
decideSplit: async (id, decision) => identitySplits.decideIdentitySplit(context, id, decision),
|
|
130
|
+
splitProposals: async (filter) => identitySplits.identitySplitRows(context, filter),
|
|
131
|
+
doubts: async (page) => personDoubts(context, page),
|
|
126
132
|
linkToOwner: async (identity) => linkToOwner(context, identity),
|
|
127
133
|
list: async (options) => listPeople(database, options),
|
|
128
134
|
counts: async (query) => countPeople(database, query),
|
|
@@ -3,6 +3,7 @@ import { and, asc, eq, inArray, sql } from "./drizzle/core.js";
|
|
|
3
3
|
import { pointerPairs } from "./entity-types.js";
|
|
4
4
|
import { forgetDanglingEvidence, forgetEvidenceTo } from "./evidence-signature.js";
|
|
5
5
|
import { forgetErasedLinks } from "./identity-link-rows.js";
|
|
6
|
+
import { forgetErasedSplits } from "./identity-split-proposals.js";
|
|
6
7
|
import { mergeSql } from "./merged-persons.js";
|
|
7
8
|
import { accountIdentities, accounts, aliases, botUpdates, chatEventPeople, chatEvents, chatMembers, chats, chunks, fetchLeases, involvements, memberCounts, memberObservationMembers, memberObservations, memberStays, messageReactions, messageRevisions, messages, messageTranscripts, pollVotes, projects, syncCursors, syncRanges, syncs, } from "./schema.js";
|
|
8
9
|
import { dropUnusedVectors } from "./vector-index.js";
|
|
@@ -208,9 +209,12 @@ export const dropOrphanPeople = (database, unseenBefore) => {
|
|
|
208
209
|
database.prepare(`DELETE FROM ${table} WHERE identity_id IN (SELECT value FROM json_each(?))`).run(gone);
|
|
209
210
|
database.prepare("DELETE FROM identities WHERE id IN (SELECT value FROM json_each(?))").run(gone);
|
|
210
211
|
forgetErasedLinks(database);
|
|
212
|
+
forgetErasedSplits(database);
|
|
211
213
|
database
|
|
212
214
|
.prepare(`DELETE FROM persons WHERE id IN (SELECT value FROM json_each(?)) AND owner = 0
|
|
213
215
|
AND NOT EXISTS (SELECT 1 FROM identity_links l WHERE l.person_id = persons.id)
|
|
216
|
+
AND CAST(id AS TEXT) NOT IN (SELECT json_extract(payload, '$.person') FROM proposed_actions
|
|
217
|
+
WHERE kind = 'identity_split')
|
|
214
218
|
AND id NOT IN (SELECT id FROM (${referencesTo(database, "persons", new Set(["identity_links"]))}) WHERE id IS NOT NULL)`)
|
|
215
219
|
.run(JSON.stringify(persons));
|
|
216
220
|
};
|
|
@@ -91,6 +91,9 @@ export const removeCrmContacts = (context, key, { users, before, dryRun = false
|
|
|
91
91
|
"AND from_type IN ('deal', 'lead') AND role IS NOT 'owner') OR (from_type = 'person' " +
|
|
92
92
|
`AND from_id IN ${persons} AND kind = 'member-of' AND role = 'contact'))`)
|
|
93
93
|
.run(accountId);
|
|
94
|
+
// A split names the identities that would leave, an address among them: it goes with them, whatever its status.
|
|
95
|
+
database.exec("DELETE FROM proposed_actions WHERE kind = 'identity_split' AND EXISTS (SELECT 1 FROM json_each(payload, " +
|
|
96
|
+
`'$.identities') WHERE value IN (SELECT provider || ':' || external_id FROM identities WHERE id IN ${gone}))`);
|
|
94
97
|
database.exec("DELETE FROM proposed_actions WHERE kind = 'identity_link' AND status = 'proposed' AND (" +
|
|
95
98
|
`json_extract(payload, '$.identity') IN (SELECT provider || ':' || external_id FROM identities WHERE id IN ${gone}) ` +
|
|
96
99
|
`OR json_extract(payload, '$.with') IN (SELECT provider || ':' || external_id FROM identities WHERE id IN ${gone}))`);
|
|
@@ -103,7 +106,8 @@ export const removeCrmContacts = (context, key, { users, before, dryRun = false
|
|
|
103
106
|
const proposed = "SELECT CAST(substr(value, 8) AS INTEGER) AS id FROM proposed_actions, json_each(json_array(" +
|
|
104
107
|
"json_extract(payload, '$.from'), json_extract(payload, '$.to'))) WHERE value LIKE 'person:%' " +
|
|
105
108
|
"UNION SELECT CAST(value AS INTEGER) FROM proposed_actions, json_each(payload, '$.persons') " +
|
|
106
|
-
"WHERE json_type(payload, '$.persons') = 'array'"
|
|
109
|
+
"WHERE json_type(payload, '$.persons') = 'array' " +
|
|
110
|
+
"UNION SELECT CAST(json_extract(payload, '$.person') AS INTEGER) FROM proposed_actions WHERE kind = 'identity_split'";
|
|
107
111
|
database.exec(`DELETE FROM temp.crm_persons WHERE id IN (SELECT id FROM persons WHERE owner <> 0) ` +
|
|
108
112
|
"OR id IN (SELECT person_id FROM identity_links) " +
|
|
109
113
|
`OR id IN (${personRefs}) OR id IN (${proposed})`);
|
|
@@ -9,6 +9,9 @@ export interface LinkRow {
|
|
|
9
9
|
pair: [number, number] | undefined;
|
|
10
10
|
}
|
|
11
11
|
export declare const pairOf: (a: number | string, b: number | string) => string;
|
|
12
|
+
/** The identity a `provider:id` names, if the store still has it. */
|
|
13
|
+
export declare const pkOf: (database: CacheDatabase, ref: string) => number | undefined;
|
|
14
|
+
export declare const personOf: (database: CacheDatabase, identity: number | undefined) => number | undefined;
|
|
12
15
|
/** The people a proposal's identities are on now; merges since may have moved either side. */
|
|
13
16
|
export declare const currentPairOf: (database: CacheDatabase, payload: IdentityLinkPayload) => [number, number] | undefined;
|
|
14
17
|
/** The proposals that still say something: waiting for the owner, or turned down by them; a `page` is newest first. */
|
|
@@ -38,6 +41,8 @@ export declare const linkIndex: (database: CacheDatabase) => {
|
|
|
38
41
|
merged: (from: number, into: number) => void;
|
|
39
42
|
};
|
|
40
43
|
export type LinkIndex = ReturnType<typeof linkIndex>;
|
|
44
|
+
/** The identities on a person now, as one comparable string. */
|
|
45
|
+
export declare const sideOf: (database: CacheDatabase, person: number) => string;
|
|
41
46
|
export declare const sidesOf: (database: CacheDatabase, a: number, b: number) => [string, string];
|
|
42
47
|
/**
|
|
43
48
|
* Brings open proposals up to date with the people they name: one whose two people are one now, whose identity a
|
|
@@ -1,14 +1,15 @@
|
|
|
1
1
|
import { IDENTITY_LINK_KIND } from "../identity-links.js";
|
|
2
2
|
import { currentPerson } from "./merged-persons.js";
|
|
3
3
|
export const pairOf = (a, b) => [Number(a), Number(b)].sort((x, y) => x - y).join("-");
|
|
4
|
-
|
|
4
|
+
/** The identity a `provider:id` names, if the store still has it. */
|
|
5
|
+
export const pkOf = (database, ref) => {
|
|
5
6
|
const at = ref.indexOf(":");
|
|
6
7
|
const row = database
|
|
7
8
|
.prepare("SELECT id FROM identities WHERE provider = ? AND external_id = ?")
|
|
8
9
|
.get(ref.slice(0, at), ref.slice(at + 1));
|
|
9
10
|
return row ? Number(row.id) : undefined;
|
|
10
11
|
};
|
|
11
|
-
const personOf = (database, identity) => {
|
|
12
|
+
export const personOf = (database, identity) => {
|
|
12
13
|
if (identity === undefined)
|
|
13
14
|
return undefined;
|
|
14
15
|
const row = database.prepare("SELECT person_id FROM identity_links WHERE identity_id = ?").get(identity);
|
|
@@ -96,7 +97,8 @@ export const linkIndex = (database) => {
|
|
|
96
97
|
},
|
|
97
98
|
};
|
|
98
99
|
};
|
|
99
|
-
|
|
100
|
+
/** The identities on a person now, as one comparable string. */
|
|
101
|
+
export const sideOf = (database, person) => database
|
|
100
102
|
.prepare("SELECT identity_id FROM identity_links WHERE person_id = ? ORDER BY identity_id")
|
|
101
103
|
.all(person)
|
|
102
104
|
.map(({ identity_id }) => String(identity_id))
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import type { CacheDatabase } from "../driver.js";
|
|
2
|
+
import { type IdentitySplitDecision, type IdentitySplitFilter, type IdentitySplitInput, type IdentitySplitRow } from "../identity-splits.js";
|
|
3
|
+
import type { StoreContext } from "./open.js";
|
|
4
|
+
import { type ProposedAction } from "./proposed-actions.js";
|
|
5
|
+
/**
|
|
6
|
+
* The identities the owner turned a split down for, by the person they are still on: the owner said they belong there.
|
|
7
|
+
* `people doubts` leaves them out. One that moved since counts no longer.
|
|
8
|
+
*/
|
|
9
|
+
export declare const keptTogether: (database: CacheDatabase) => Map<number, Set<string>>;
|
|
10
|
+
/**
|
|
11
|
+
* Proposes to take identities off their person, or to undo a merge, as an `identity_split` only the owner decides. An
|
|
12
|
+
* open proposal for the same identities is given back instead of a second, brought up to date with the person as it
|
|
13
|
+
* is now and with the new evidence added, so proposing again is how a stale one is renewed; its first reason and
|
|
14
|
+
* proposer stay. An identity the owner kept on this person by turning a split down is refused.
|
|
15
|
+
*/
|
|
16
|
+
export declare const proposeIdentitySplit: (context: StoreContext, input: IdentitySplitInput) => ProposedAction;
|
|
17
|
+
/**
|
|
18
|
+
* Decides an `identity_split`, the owner's alone. Confirm carries it out as the owner's own unlink or unmerge, so no
|
|
19
|
+
* automatic match joins them again; it is refused with `identity.stale` when the person changed since it was proposed.
|
|
20
|
+
* Only a waiting proposal is carried out: one an older build approved as a plain action stays as it is.
|
|
21
|
+
*/
|
|
22
|
+
export declare const decideIdentitySplit: (context: StoreContext, id: string, decision: IdentitySplitDecision) => ProposedAction;
|
|
23
|
+
/** The `identity_split` proposals, newest first, each naming the person as they are now. */
|
|
24
|
+
export declare const identitySplitRows: (context: StoreContext, { status, limit, offset }: IdentitySplitFilter) => {
|
|
25
|
+
items: IdentitySplitRow[];
|
|
26
|
+
hasMore: boolean;
|
|
27
|
+
};
|
|
28
|
+
/** Deletes every `identity_split` that names an identity no longer in the store: its payload names the address. */
|
|
29
|
+
export declare const forgetErasedSplits: (database: CacheDatabase) => void;
|
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
import { CliError } from "@wirecat/cli-base";
|
|
2
|
+
import { assertMayDecide, deciderAuthor } from "../decisions.js";
|
|
3
|
+
import { IDENTITY_SPLIT_KIND, } from "../identity-splits.js";
|
|
4
|
+
import { personOf, pkOf, sideOf } from "./identity-link-rows.js";
|
|
5
|
+
import { parseIdentityRef } from "./identity-proposals.js";
|
|
6
|
+
import { currentPerson } from "./merged-persons.js";
|
|
7
|
+
import { recordOf, required, unlinkIdentity } from "./person-links.js";
|
|
8
|
+
import { mergeId, merges, parseMergeId, split, undoMerge } from "./person-merges.js";
|
|
9
|
+
import { proposalOperationsOver, proposerOfAction } from "./proposed-actions.js";
|
|
10
|
+
import { write } from "./transaction.js";
|
|
11
|
+
const refOf = ({ provider, id }) => `${provider}:${id}`;
|
|
12
|
+
const keyOf = (split, person, identities) => `${split} ${person} ${[...identities].sort().join(" ")}`;
|
|
13
|
+
/** What the split would move, checked as the owner's own move would check it, so a proposal can be carried out. */
|
|
14
|
+
const planOf = (context, input) => {
|
|
15
|
+
if ("merge" in input) {
|
|
16
|
+
const { to, from } = parseMergeId(input.merge);
|
|
17
|
+
const id = mergeId(to, from);
|
|
18
|
+
const found = merges(context, to).find((merge) => merge.id === id);
|
|
19
|
+
if (found === undefined)
|
|
20
|
+
throw new CliError("not_found", `nothing of person:${from} is merged into person:${to}`, {
|
|
21
|
+
reason: "identity.no-merge",
|
|
22
|
+
});
|
|
23
|
+
return { split: "unmerge", person: Number(to), identities: found.identities.map(refOf), merge: id };
|
|
24
|
+
}
|
|
25
|
+
const chosen = [...new Map(input.identities.map((ref) => [refOf(ref), ref])).values()];
|
|
26
|
+
if (chosen.length === 0)
|
|
27
|
+
throw new CliError("validation_error", "name at least one identity to split off");
|
|
28
|
+
const persons = new Set(chosen.map((ref) => required(context, ref).personPk));
|
|
29
|
+
if (persons.size > 1)
|
|
30
|
+
throw new CliError("validation_error", "the identities that leave are all of one person", {
|
|
31
|
+
reason: "identity.several-persons",
|
|
32
|
+
});
|
|
33
|
+
const person = [...persons][0];
|
|
34
|
+
if (chosen.length >= recordOf(context, person).identities.length)
|
|
35
|
+
throw new CliError("validation_error", "a split leaves at least one identity with the person", {
|
|
36
|
+
reason: "identity.nothing-left",
|
|
37
|
+
});
|
|
38
|
+
return { split: "unlink", person, identities: chosen.map(refOf), merge: null };
|
|
39
|
+
};
|
|
40
|
+
const splitRows = (database, statuses, page, made) => database
|
|
41
|
+
.prepare("SELECT id, status, payload FROM proposed_actions WHERE kind = ? AND status IN (SELECT value FROM json_each(?)) " +
|
|
42
|
+
(made === undefined ? "" : `AND json_extract(result, '$.person') IS ${made ? "NOT " : ""}NULL `) +
|
|
43
|
+
`ORDER BY id DESC${page === undefined ? "" : " LIMIT ? OFFSET ?"}`)
|
|
44
|
+
.all(IDENTITY_SPLIT_KIND, JSON.stringify(statuses), ...(page === undefined ? [] : [page.limit, page.offset]))
|
|
45
|
+
.map((row) => ({
|
|
46
|
+
id: String(row.id),
|
|
47
|
+
status: String(row.status),
|
|
48
|
+
payload: JSON.parse(String(row.payload)),
|
|
49
|
+
}));
|
|
50
|
+
/**
|
|
51
|
+
* The person the proposal is about now: the one it was proposed for, or whom that person was merged into, while every
|
|
52
|
+
* identity it names is still there. `undefined` once one left, was erased or went elsewhere.
|
|
53
|
+
*/
|
|
54
|
+
const holder = (database, payload) => {
|
|
55
|
+
const person = currentPerson(database, Number(payload.person));
|
|
56
|
+
return payload.identities.every((ref) => personOf(database, pkOf(database, ref)) === person) ? person : undefined;
|
|
57
|
+
};
|
|
58
|
+
/**
|
|
59
|
+
* The identities the owner turned a split down for, by the person they are still on: the owner said they belong there.
|
|
60
|
+
* `people doubts` leaves them out. One that moved since counts no longer.
|
|
61
|
+
*/
|
|
62
|
+
export const keptTogether = (database) => {
|
|
63
|
+
const kept = new Map();
|
|
64
|
+
for (const { payload } of splitRows(database, ["rejected"])) {
|
|
65
|
+
const person = currentPerson(database, Number(payload.person));
|
|
66
|
+
for (const ref of payload.identities) {
|
|
67
|
+
if (personOf(database, pkOf(database, ref)) !== person)
|
|
68
|
+
continue;
|
|
69
|
+
const refs = kept.get(person) ?? new Set();
|
|
70
|
+
refs.add(ref);
|
|
71
|
+
kept.set(person, refs);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
return kept;
|
|
75
|
+
};
|
|
76
|
+
/**
|
|
77
|
+
* Proposes to take identities off their person, or to undo a merge, as an `identity_split` only the owner decides. An
|
|
78
|
+
* open proposal for the same identities is given back instead of a second, brought up to date with the person as it
|
|
79
|
+
* is now and with the new evidence added, so proposing again is how a stale one is renewed; its first reason and
|
|
80
|
+
* proposer stay. An identity the owner kept on this person by turning a split down is refused.
|
|
81
|
+
*/
|
|
82
|
+
export const proposeIdentitySplit = (context, input) => write(context.database, () => {
|
|
83
|
+
const reason = input.reason.trim();
|
|
84
|
+
if (!reason)
|
|
85
|
+
throw new CliError("validation_error", "a proposal to split a person says why", {
|
|
86
|
+
reason: "proposal.reason-required",
|
|
87
|
+
});
|
|
88
|
+
const plan = planOf(context, input);
|
|
89
|
+
const kept = keptTogether(context.database).get(plan.person);
|
|
90
|
+
if (plan.identities.some((ref) => kept?.has(ref)))
|
|
91
|
+
throw new CliError("conflict", "the owner kept these identities together; only the owner splits them", {
|
|
92
|
+
reason: "identity.kept-together",
|
|
93
|
+
});
|
|
94
|
+
const key = keyOf(plan.split, plan.person, plan.identities);
|
|
95
|
+
const operations = proposalOperationsOver(context);
|
|
96
|
+
const payload = {
|
|
97
|
+
split: plan.split,
|
|
98
|
+
person: String(plan.person),
|
|
99
|
+
identities: plan.identities,
|
|
100
|
+
merge: plan.merge,
|
|
101
|
+
side: sideOf(context.database, plan.person),
|
|
102
|
+
evidence: input.evidence ?? [],
|
|
103
|
+
};
|
|
104
|
+
for (const row of splitRows(context.database, ["proposed"])) {
|
|
105
|
+
const person = holder(context.database, row.payload);
|
|
106
|
+
if (person === undefined || keyOf(row.payload.split, person, row.payload.identities) !== key)
|
|
107
|
+
continue;
|
|
108
|
+
const evidence = [...new Set([...row.payload.evidence, ...payload.evidence])];
|
|
109
|
+
context.database
|
|
110
|
+
.prepare("UPDATE proposed_actions SET payload = ? WHERE id = ?")
|
|
111
|
+
.run(JSON.stringify({ ...payload, evidence }), Number(row.id));
|
|
112
|
+
return operations.get(row.id);
|
|
113
|
+
}
|
|
114
|
+
return operations.propose({ kind: IDENTITY_SPLIT_KIND, by: input.by, payload, reason });
|
|
115
|
+
});
|
|
116
|
+
/**
|
|
117
|
+
* Decides an `identity_split`, the owner's alone. Confirm carries it out as the owner's own unlink or unmerge, so no
|
|
118
|
+
* automatic match joins them again; it is refused with `identity.stale` when the person changed since it was proposed.
|
|
119
|
+
* Only a waiting proposal is carried out: one an older build approved as a plain action stays as it is.
|
|
120
|
+
*/
|
|
121
|
+
export const decideIdentitySplit = (context, id, decision) => write(context.database, () => {
|
|
122
|
+
const operations = proposalOperationsOver(context);
|
|
123
|
+
const proposal = operations.get(id);
|
|
124
|
+
if (proposal.kind !== IDENTITY_SPLIT_KIND)
|
|
125
|
+
throw new CliError("validation_error", `${id} is a ${proposal.kind} proposal, not a split of a person`);
|
|
126
|
+
assertMayDecide({ id, kind: IDENTITY_SPLIT_KIND, ownerOnly: true, proposedBy: proposerOfAction(proposal) }, decision.by, context.decisions);
|
|
127
|
+
const author = deciderAuthor(decision.by);
|
|
128
|
+
const reason = decision.reason?.trim() || undefined;
|
|
129
|
+
const noted = reason === undefined ? {} : { reason };
|
|
130
|
+
if (!decision.confirm)
|
|
131
|
+
return operations.reject(id, author, noted);
|
|
132
|
+
if (proposal.status !== "proposed")
|
|
133
|
+
throw new CliError("validation_error", `${id} is already ${proposal.status}`, { reason: "proposal.wrong-state" });
|
|
134
|
+
const payload = proposal.payload;
|
|
135
|
+
const person = Number(payload.person);
|
|
136
|
+
const refs = payload.identities.map(parseIdentityRef);
|
|
137
|
+
if (holder(context.database, payload) !== person || sideOf(context.database, person) !== payload.side)
|
|
138
|
+
throw new CliError("conflict", `the person ${id} names changed since it was proposed; propose it again`, {
|
|
139
|
+
reason: "identity.stale",
|
|
140
|
+
});
|
|
141
|
+
const owner = { method: "unlink", by: "owner" };
|
|
142
|
+
let made;
|
|
143
|
+
if (payload.split === "unmerge") {
|
|
144
|
+
const { to, from } = parseMergeId(payload.merge ?? "");
|
|
145
|
+
made = undoMerge(context, to, from, "owner");
|
|
146
|
+
}
|
|
147
|
+
else
|
|
148
|
+
made =
|
|
149
|
+
refs.length === 1
|
|
150
|
+
? unlinkIdentity(context, refs[0], owner)
|
|
151
|
+
: split(context, String(person), refs, "owner");
|
|
152
|
+
return operations.approve(id, { person: made.uid, ...noted }, author);
|
|
153
|
+
});
|
|
154
|
+
/** A split carried out records the person it made; an older build's approval as a plain action made none. */
|
|
155
|
+
const shownStatus = (status, result) => {
|
|
156
|
+
if (status === "proposed" || status === "rejected")
|
|
157
|
+
return status;
|
|
158
|
+
return typeof result?.person === "string" ? "confirmed" : "dropped";
|
|
159
|
+
};
|
|
160
|
+
const STORED = {
|
|
161
|
+
proposed: ["proposed"],
|
|
162
|
+
rejected: ["rejected"],
|
|
163
|
+
confirmed: ["approved", "executed", "failed"],
|
|
164
|
+
};
|
|
165
|
+
/** The `identity_split` proposals, newest first, each naming the person as they are now. */
|
|
166
|
+
export const identitySplitRows = (context, { status = "proposed", limit, offset }) => {
|
|
167
|
+
if (!Number.isSafeInteger(limit) || limit < 1 || limit > 500)
|
|
168
|
+
throw new CliError("validation_error", "a list of split proposals takes 1–500 items");
|
|
169
|
+
const statuses = status === "all" ? Object.values(STORED).flat() : STORED[status === "dropped" ? "confirmed" : status];
|
|
170
|
+
const made = status === "dropped" ? false : status === "confirmed" ? true : undefined;
|
|
171
|
+
const operations = proposalOperationsOver(context);
|
|
172
|
+
const rows = splitRows(context.database, statuses, { limit: limit + 1, offset }, made);
|
|
173
|
+
const items = rows.slice(0, limit).map((row) => {
|
|
174
|
+
const action = operations.get(row.id);
|
|
175
|
+
return {
|
|
176
|
+
id: row.id,
|
|
177
|
+
status: shownStatus(row.status, action.result),
|
|
178
|
+
split: row.payload.split,
|
|
179
|
+
person: `person:${holder(context.database, row.payload) ?? currentPerson(context.database, Number(row.payload.person))}`,
|
|
180
|
+
identities: row.payload.identities,
|
|
181
|
+
merge: row.payload.merge,
|
|
182
|
+
reason: action.reason,
|
|
183
|
+
evidence: row.payload.evidence,
|
|
184
|
+
proposedBy: proposerOfAction(action),
|
|
185
|
+
createdAt: action.createdAt,
|
|
186
|
+
};
|
|
187
|
+
});
|
|
188
|
+
return { items, hasMore: rows.length > limit };
|
|
189
|
+
};
|
|
190
|
+
/** Deletes every `identity_split` that names an identity no longer in the store: its payload names the address. */
|
|
191
|
+
export const forgetErasedSplits = (database) => {
|
|
192
|
+
for (const row of splitRows(database, ["proposed", "approved", "rejected", "executed", "failed"]))
|
|
193
|
+
if (row.payload.identities.some((ref) => pkOf(database, ref) === undefined))
|
|
194
|
+
database.prepare("DELETE FROM proposed_actions WHERE id = ?").run(Number(row.id));
|
|
195
|
+
};
|
|
@@ -347,6 +347,8 @@ const topic = messengerOnly(({ value }) => always(bound("m.thread_external_id =
|
|
|
347
347
|
/** What `kind:` matches for a chat aliased `c`: private, saved, bot, service, group, channel or unknown. */
|
|
348
348
|
export const PEER_KIND = "CASE WHEN json_extract(c.metadata,'$.peerKind') IN ('private','saved','bot','service','group','channel','unknown') THEN json_extract(c.metadata,'$.peerKind') WHEN c.kind='dialog' AND json_extract(c.metadata,'$.isBot')=1 THEN 'bot' WHEN c.kind='dialog' THEN 'private' ELSE c.kind END";
|
|
349
349
|
const kind = messengerOnly(({ value }) => always(bound(`${PEER_KIND} = ?`, value.toLowerCase())));
|
|
350
|
+
/** A chat the owner muted, until its mute runs out; a chat whose messenger keeps no mute is not muted. */
|
|
351
|
+
const muted = messengerOnly(({ value }) => always(bound(`${value.toLowerCase() === "true" ? "" : "NOT "}(coalesce(c.muted, 0) = 1 AND (c.muted_until IS NULL OR c.muted_until > ?))`, Date.now())));
|
|
350
352
|
const has = ({ value }, { corpus, mail }) => {
|
|
351
353
|
const wanted = value.toLowerCase();
|
|
352
354
|
if (wanted === "transcript")
|
|
@@ -440,6 +442,7 @@ export const FIELD_COMPILERS = {
|
|
|
440
442
|
tag,
|
|
441
443
|
topic,
|
|
442
444
|
kind,
|
|
445
|
+
muted,
|
|
443
446
|
has,
|
|
444
447
|
in: inSourceField,
|
|
445
448
|
to: recipient,
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import type { StoreContext } from "./open.js";
|
|
2
|
+
/** Why a person may be wrong, most urgent first: the owner's own person holding someone else matters most. */
|
|
3
|
+
export declare const DOUBT_REASONS: readonly ["owner-other-name", "test-data", "shared-address", "mixed-names"];
|
|
4
|
+
export type DoubtReason = (typeof DOUBT_REASONS)[number];
|
|
5
|
+
export interface DoubtIdentity {
|
|
6
|
+
identity: string;
|
|
7
|
+
name: string | null;
|
|
8
|
+
/** How the identity came to this person, and who linked it. */
|
|
9
|
+
method: string;
|
|
10
|
+
by: string;
|
|
11
|
+
at: string;
|
|
12
|
+
}
|
|
13
|
+
export interface PersonDoubt {
|
|
14
|
+
person: string;
|
|
15
|
+
name: string | null;
|
|
16
|
+
owner: boolean;
|
|
17
|
+
reason: DoubtReason;
|
|
18
|
+
/** The reason in plain words, naming what was found. */
|
|
19
|
+
why: string;
|
|
20
|
+
/** The identities in doubt, not every identity of the person. */
|
|
21
|
+
identities: DoubtIdentity[];
|
|
22
|
+
/** What the doubt rests on: the identities, a shared address, the merges that brought them. */
|
|
23
|
+
evidence: string[];
|
|
24
|
+
/** memo commands, after `memo`, that would take the doubted identities off; empty when none fits. */
|
|
25
|
+
fix: string[];
|
|
26
|
+
}
|
|
27
|
+
export interface PersonDoubts {
|
|
28
|
+
items: PersonDoubt[];
|
|
29
|
+
total: number;
|
|
30
|
+
hasMore: boolean;
|
|
31
|
+
}
|
|
32
|
+
/** People that may be wrong, a page at a time, each with why and the owner's moves that would set it right. */
|
|
33
|
+
export declare const personDoubts: (context: StoreContext, { limit, offset }: {
|
|
34
|
+
limit: number;
|
|
35
|
+
offset: number;
|
|
36
|
+
}) => PersonDoubts;
|