@wirecat/cli-core 0.645.0 → 0.646.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -5,6 +5,18 @@ Notable changes to `@wirecat/cli-core` (`@wirecat/cli-messaging` up to 0.261.0,
5
5
  minor version may break callers, and says how under "Changed — may break callers". `pnpm docs:check` checks the
6
6
  shape of this file.
7
7
 
8
+ ## 0.646.0 — 12.11.2026
9
+
10
+ ### Added
11
+
12
+ - **`decideLink` answers a match whose two people are a company's mailboxes.** `organization: { organization?, name?,
13
+ all? }` with `confirm: false` gives every address of the match's identities to one organization (the one named, else
14
+ one holding an address or the company domain, else one of `name`, else a new company named after the people's
15
+ shared name or the domain; free mail needs a name). A person who is only those addresses becomes the company's and
16
+ is listed as an organization; one who holds more keeps the rest and stays a person. The match closes, `all` closes
17
+ every match on those addresses, and the result names `organization`, `mailboxes`, `people` and `closed`. No
18
+ migration.
19
+
8
20
  ## 0.645.0 — 12.11.2026
9
21
 
10
22
  ### Added
@@ -60,6 +60,19 @@ export interface IdentityLinkDecision {
60
60
  all?: boolean;
61
61
  mailbox?: string;
62
62
  };
63
+ /**
64
+ * With `confirm: false`: the two "people" are a company's mailboxes. Every address of the match's identities goes to
65
+ * one organization: `organization`, else the one holding an address or its company domain, else one of `name`, else
66
+ * a new company named after the people's shared name (a match by name) or the domain. A person who is only those
67
+ * addresses becomes the company's; one who holds more keeps the rest and stays a person. `all` closes every open
68
+ * proposal on those addresses. The result names `organization`, `mailboxes`, `people` (who became the company's)
69
+ * and `closed`.
70
+ */
71
+ organization?: {
72
+ organization?: number;
73
+ name?: string;
74
+ all?: boolean;
75
+ };
63
76
  }
64
77
  /** One `identity_link`, naming the people as they are now, not as they were when it was proposed. */
65
78
  export interface IdentityLinkRow {
@@ -88,7 +88,8 @@ export interface PeopleStore extends AccountPeople {
88
88
  proposeLink(input: IdentityLinkInput): Promise<ProposedAction>;
89
89
  /**
90
90
  * The owner's decision on an `identity_link`; confirm is refused with `identity.stale` when either person changed.
91
- * `company` turns down a shared-mailbox proposal and gives its address to an organization (NEED-2528).
91
+ * `company` turns down a shared-mailbox proposal and gives its address to an organization (NEED-2528);
92
+ * `organization` turns down a match whose two sides are a company's mailboxes and gives them all to it (NEED-2573).
92
93
  */
93
94
  decideLink(id: string, decision: IdentityLinkDecision): Promise<ProposedAction>;
94
95
  /**
@@ -7,7 +7,7 @@ import { holdAddress, organizationOfAddress } from "./address-organization.js";
7
7
  import { ensurePerson } from "./identities.js";
8
8
  import { evidenceKeyOf, linkIndex, linkRows, settleLinks, sidesOf, } from "./identity-link-rows.js";
9
9
  import { currentPerson } from "./merged-persons.js";
10
- import { linkIdentityToPerson, mergePerson, move, recordOf, required, survivorOf } from "./person-links.js";
10
+ import { isOwner, linkIdentityToPerson, mergePerson, move, recordOf, required, survivorOf, unlinkIdentity, } from "./person-links.js";
11
11
  import { resolvePersonLinks } from "./person-resolution.js";
12
12
  import { identitySeparated, separated } from "./person-separation.js";
13
13
  import { proposalOperationsOver, proposerOfAction } from "./proposed-actions.js";
@@ -244,6 +244,132 @@ const toCompany = (context, proposal, company, author, noted) => {
244
244
  settleMentions(context, payload, "found");
245
245
  return rejected;
246
246
  };
247
+ /** The match's own identities as they are now, each with the address it is (`email`) or carries. */
248
+ const identitiesOfMatch = ({ database }, payload) => [payload.identity, payload.with].flatMap((ref) => {
249
+ if (ref === null)
250
+ return [];
251
+ const { provider, id } = parseIdentityRef(ref);
252
+ const row = database
253
+ .prepare("SELECT i.id, l.person_id, lower(trim(coalesce(CASE WHEN i.provider = 'email' THEN i.external_id END, i.email))) " +
254
+ "AS address FROM identities i JOIN identity_links l ON l.identity_id = i.id " +
255
+ "WHERE i.provider = ? AND i.external_id = ?")
256
+ .get(provider, id);
257
+ if (row === undefined)
258
+ return [];
259
+ const address = typeof row.address === "string" && isEmailAddress(row.address) ? row.address : null;
260
+ return [{ pk: Number(row.id), provider, id, person: Number(row.person_id), address }];
261
+ });
262
+ /** The one organization that already holds any of the addresses, else none; two are refused. */
263
+ const holderOf = ({ database }, addresses) => {
264
+ const found = [
265
+ ...new Set(database
266
+ .prepare("SELECT DISTINCT o.id FROM organizations o JOIN organization_identities oi ON oi.organization_id = o.id " +
267
+ "JOIN identities i ON i.id = oi.identity_id WHERE o.deleted_at IS NULL AND i.provider = 'email' " +
268
+ "AND lower(i.external_id) IN (SELECT value FROM json_each(?)) ORDER BY o.id")
269
+ .all(JSON.stringify(addresses))
270
+ .map(({ id }) => Number(id))),
271
+ ];
272
+ if (found.length > 1)
273
+ throw new CliError("validation_error", `${found.length} organizations hold these addresses (${found.map((id) => `organization:${id}`).join(", ")}); ` +
274
+ "name the one meant as a reference", { reason: "organization.ambiguous" });
275
+ return found[0];
276
+ };
277
+ /**
278
+ * NEED-2573 A: the two "people" are the company's mailboxes. Every address of the match's identities goes to one
279
+ * organization. A person who is nothing but those addresses becomes the company's (an organization holds all their
280
+ * identities, so they are listed as an organization, not a person); one who also holds something else keeps it and
281
+ * stays a person, their address identity moved off them. Nobody is joined and the match is turned down.
282
+ */
283
+ const toOrganization = (context, proposal, asked, author, noted) => {
284
+ const payload = proposal.payload;
285
+ if (proposal.status !== "proposed")
286
+ throw new CliError("validation_error", `${proposal.id} is already ${proposal.status}`, {
287
+ reason: "proposal.wrong-state",
288
+ });
289
+ const matched = identitiesOfMatch(context, payload);
290
+ const addresses = [
291
+ ...new Set([...matched.flatMap(({ address }) => address ?? []), ...(payload.mailbox ? [payload.mailbox] : [])]),
292
+ ].sort();
293
+ if (addresses.length === 0)
294
+ throw new CliError("validation_error", `${proposal.id} names no mail address to give to an organization`, {
295
+ reason: "identity.not-shared",
296
+ });
297
+ const persons = [...new Set(matched.map(({ person }) => person))];
298
+ if (persons.some((person) => isOwner(context, person)))
299
+ throw new CliError("validation_error", `${proposal.id} names the owner; the owner's address is no company's`, {
300
+ reason: "identity.owner",
301
+ });
302
+ const name = asked.name?.trim() || undefined;
303
+ const company = addresses.find((address) => !FREE_MAIL.has(address.split("@")[1] ?? ""));
304
+ const how = { method: CONFIRMED, author: "owner" };
305
+ let organization;
306
+ if (asked.organization !== undefined) {
307
+ if (!organizationExists(context, asked.organization))
308
+ throw new CliError("not_found", `no organization:${asked.organization}`, { reason: "organization.not-found" });
309
+ organization = asked.organization;
310
+ }
311
+ else {
312
+ if (name === undefined && company === undefined)
313
+ throw new CliError("validation_error", `${addresses.join(", ")} ${addresses.length > 1 ? "are free mailboxes" : "is a free mailbox"}; ` +
314
+ "name the company or its organization", { reason: "organization.unnamed" });
315
+ const shared = payload.signal === "name"
316
+ ? context.database
317
+ .prepare("SELECT coalesce(p.name, i.name) AS name FROM identities i JOIN identity_links l ON l.identity_id = i.id " +
318
+ "JOIN persons p ON p.id = l.person_id WHERE i.id = ?")
319
+ .get(matched[0]?.pk ?? 0)?.name
320
+ : undefined;
321
+ organization =
322
+ holderOf(context, addresses) ??
323
+ organizationOfAddress(context, {
324
+ name: name ?? (typeof shared === "string" && shared.trim() ? shared.trim() : ((company ?? "").split("@")[1] ?? "")),
325
+ ...(company === undefined ? {} : { email: company }),
326
+ ...how,
327
+ reason: "organization.ambiguous",
328
+ });
329
+ }
330
+ for (const address of addresses)
331
+ holdAddress(context, organization, address, how);
332
+ const held = context.database.prepare("INSERT INTO organization_identities (identity_id, organization_id, method, author, created_at, updated_at) " +
333
+ "VALUES (?, ?, ?, ?, ?, ?) ON CONFLICT (identity_id, organization_id) DO NOTHING");
334
+ const people = [];
335
+ for (const person of persons) {
336
+ const theirs = matched.filter((one) => one.person === person && one.address !== null);
337
+ const all = context.database
338
+ .prepare("SELECT lower(trim(coalesce(CASE WHEN i.provider = 'email' THEN i.external_id END, i.email, ''))) AS address " +
339
+ "FROM identity_links l JOIN identities i ON i.id = l.identity_id WHERE l.person_id = ?")
340
+ .all(person)
341
+ .map(({ address }) => String(address));
342
+ if (all.every((address) => addresses.includes(address))) {
343
+ const at = context.now();
344
+ for (const one of theirs)
345
+ held.run(one.pk, organization, how.method, how.author, at, at);
346
+ people.push(`person:${person}`);
347
+ }
348
+ else
349
+ for (const one of theirs)
350
+ if (one.provider === "email")
351
+ unlinkIdentity(context, { provider: one.provider, id: one.id }, { method: "unlink", by: "owner" });
352
+ }
353
+ const operations = proposalOperationsOver(context);
354
+ const refs = addresses.map((address) => `email:${address}`);
355
+ const others = asked.all
356
+ ? context.database
357
+ .prepare("SELECT id FROM proposed_actions WHERE kind = ? AND status = 'proposed' AND id <> ? AND (" +
358
+ "json_extract(payload, '$.mailbox') IN (SELECT value FROM json_each(?)) " +
359
+ "OR json_extract(payload, '$.identity') IN (SELECT value FROM json_each(?)) " +
360
+ "OR json_extract(payload, '$.with') IN (SELECT value FROM json_each(?))) ORDER BY id")
361
+ .all(IDENTITY_LINK_KIND, Number(proposal.id), JSON.stringify(addresses), JSON.stringify(refs), JSON.stringify(refs))
362
+ .map((row) => String(row.id))
363
+ : [];
364
+ const result = { dismissed: true, organization: `organization:${organization}`, mailboxes: addresses, ...noted };
365
+ for (const other of others) {
366
+ const closed = operations.reject(other, author, { ...result, with: proposal.id });
367
+ settleMentions(context, closed.payload, "found");
368
+ }
369
+ const rejected = operations.reject(proposal.id, author, { ...result, people, closed: [proposal.id, ...others] });
370
+ settleMentions(context, payload, "found");
371
+ return rejected;
372
+ };
247
373
  /**
248
374
  * Decides an `identity_link`, the owner's alone. Confirm links the two people as the owner's link, refused with
249
375
  * `identity.stale` when either person changed since it was proposed; reject keeps them apart, and only new evidence
@@ -258,6 +384,11 @@ export const decideIdentityLink = (context, id, decision) => write(context.datab
258
384
  const author = deciderAuthor(decision.by);
259
385
  const reason = decision.reason?.trim() || undefined;
260
386
  const noted = reason === undefined ? {} : { reason };
387
+ if (decision.organization !== undefined) {
388
+ if (decision.confirm || decision.company !== undefined)
389
+ throw new CliError("validation_error", "the company's mailboxes keep the two apart; it is not a confirm");
390
+ return toOrganization(context, proposal, decision.organization, author, noted);
391
+ }
261
392
  if (decision.company !== undefined) {
262
393
  if (decision.confirm)
263
394
  throw new CliError("validation_error", "a company's mailbox keeps the two people apart; it is not a confirm");
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wirecat/cli-core",
3
- "version": "0.645.0",
3
+ "version": "0.646.0",
4
4
  "description": "The messenger-neutral half of a messaging CLI: domain model, message locators, rendering, name resolution, the SQLite seam, the command skeleton and shared read commands, the send guard, run records and the message store",
5
5
  "license": "Apache-2.0",
6
6
  "author": "Viacheslav Ptsarev",