@wirecat/cli-core 0.603.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.
Files changed (84) hide show
  1. package/CHANGELOG.md +92 -0
  2. package/dist/calendar/prepare.d.ts +4 -2
  3. package/dist/calendar/prepare.js +8 -8
  4. package/dist/cli/index.d.ts +1 -1
  5. package/dist/cli/index.js +1 -1
  6. package/dist/cli/messenger/contacts-command.js +3 -2
  7. package/dist/cli/messenger/doctor-command.js +14 -7
  8. package/dist/cli/messenger/search-answers.js +8 -1
  9. package/dist/cli/messenger/store-maintenance-command.d.ts +34 -2
  10. package/dist/cli/messenger/store-maintenance-command.js +31 -4
  11. package/dist/cli/model-catalogue-command.js +28 -2
  12. package/dist/cli/program.js +11 -2
  13. package/dist/cli/steps.js +9 -1
  14. package/dist/cli/store-recover.d.ts +10 -2
  15. package/dist/cli/store-recover.js +18 -4
  16. package/dist/domain/record-types.d.ts +61 -0
  17. package/dist/domain/record-types.js +26 -0
  18. package/dist/domain/records.d.ts +3 -5
  19. package/dist/domain/records.js +10 -13
  20. package/dist/entities/contact-activity.d.ts +4 -0
  21. package/dist/entities/contact-activity.js +1 -0
  22. package/dist/entities/contact-context.d.ts +20 -0
  23. package/dist/entities/contact-context.js +2 -0
  24. package/dist/entities/index.d.ts +22 -0
  25. package/dist/errors/failures.d.ts +17 -1
  26. package/dist/errors/failures.js +57 -1
  27. package/dist/errors/reasons.js +16 -0
  28. package/dist/mcp/tool.js +2 -1
  29. package/dist/runs/recording.js +13 -4
  30. package/dist/search/lucene/plain.d.ts +2 -1
  31. package/dist/search/lucene/plain.js +15 -1
  32. package/dist/services/deps.js +4 -6
  33. package/dist/services/index.d.ts +1 -1
  34. package/dist/services/link-scorers/chat-senders.d.ts +3 -2
  35. package/dist/services/link-scorers/chat-senders.js +7 -4
  36. package/dist/services/link-scorers/mail-thread.js +1 -0
  37. package/dist/services/link-scorers/member-chats.js +1 -0
  38. package/dist/services/link-scorers/member-dialog.d.ts +1 -1
  39. package/dist/services/link-scorers/member-dialog.js +4 -3
  40. package/dist/services/link-scorers/scan.d.ts +5 -2
  41. package/dist/services/link-scorers/scan.js +98 -53
  42. package/dist/services/link-scorers/shared.d.ts +2 -2
  43. package/dist/services/link-scorers/shared.js +7 -4
  44. package/dist/services/link-scorers/types.d.ts +3 -0
  45. package/dist/services/metadata.d.ts +1 -1
  46. package/dist/services/model-downloads.d.ts +5 -3
  47. package/dist/services/model-downloads.js +6 -6
  48. package/dist/services/notes-search.js +4 -2
  49. package/dist/services/person-context.d.ts +24 -7
  50. package/dist/services/person-context.js +45 -11
  51. package/dist/services/person-preparation.js +4 -5
  52. package/dist/services/search-all.d.ts +6 -0
  53. package/dist/services/search-all.js +16 -8
  54. package/dist/speech/install.d.ts +6 -7
  55. package/dist/speech/install.js +17 -10
  56. package/dist/store/damage.d.ts +3 -1
  57. package/dist/store/damage.js +19 -12
  58. package/dist/store/document-transfer/index.js +7 -0
  59. package/dist/store/index.d.ts +1 -0
  60. package/dist/store/index.js +1 -0
  61. package/dist/store/marks.d.ts +57 -0
  62. package/dist/store/marks.js +121 -0
  63. package/dist/store/project-transfer/preview.js +12 -7
  64. package/dist/store/recover.js +16 -1
  65. package/dist/store/sqlite/finance.d.ts +8 -1
  66. package/dist/store/sqlite/finance.js +10 -3
  67. package/dist/store/sqlite/note-dates.d.ts +6 -0
  68. package/dist/store/sqlite/note-dates.js +23 -0
  69. package/dist/store/sqlite/notes.d.ts +13 -0
  70. package/dist/store/sqlite/notes.js +6 -1
  71. package/dist/store/sqlite/open-store.js +4 -1
  72. package/dist/store/sqlite/own-identities.d.ts +1 -1
  73. package/dist/store/sqlite/own-identities.js +7 -5
  74. package/dist/store/sqlite/person-links.d.ts +5 -2
  75. package/dist/store/sqlite/person-links.js +24 -18
  76. package/dist/store/sqlite/person-separation.d.ts +5 -0
  77. package/dist/store/sqlite/person-separation.js +26 -0
  78. package/dist/store/sqlite/record-projectors.d.ts +44 -0
  79. package/dist/store/sqlite/record-projectors.js +72 -0
  80. package/dist/store/sqlite/records.js +12 -70
  81. package/dist/store/sqlite/transaction.js +2 -5
  82. package/dist/store/types.d.ts +5 -0
  83. package/package.json +3 -3
  84. package/parity.json +3 -1
@@ -0,0 +1,61 @@
1
+ /** Types with a table of their own; anything else a source sends is `custom:<name>` and stays a record only. */
2
+ export declare const RECORD_ENTITY_TYPES: readonly ["invoice", "payment", "person", "organization", "project"];
3
+ export type RecordEntityType = (typeof RECORD_ENTITY_TYPES)[number];
4
+ export interface RecordRoleSpec {
5
+ /** Several relations may carry it; a single role may also be named in `fields.<role>`. */
6
+ many?: boolean;
7
+ amount?: boolean;
8
+ }
9
+ export interface RecordTypeSpec {
10
+ table: string;
11
+ /** `proposal`: the import proposes the entity and the owner decides; `identity`: only the owner's `map` gives one. */
12
+ review: "proposal" | "identity";
13
+ /** The roles the entity is made from; any other role word is kept as the source's own. */
14
+ roles: Record<string, RecordRoleSpec>;
15
+ }
16
+ export declare const RECORD_TYPES: {
17
+ readonly invoice: {
18
+ readonly table: "invoices";
19
+ readonly review: "proposal";
20
+ readonly roles: {
21
+ readonly issuer: {};
22
+ readonly counterparty: {};
23
+ readonly project: {};
24
+ };
25
+ };
26
+ readonly payment: {
27
+ readonly table: "payments";
28
+ readonly review: "proposal";
29
+ readonly roles: {
30
+ readonly payer: {};
31
+ readonly payee: {};
32
+ readonly project: {};
33
+ readonly settles: {
34
+ readonly many: true;
35
+ readonly amount: true;
36
+ };
37
+ };
38
+ };
39
+ readonly person: {
40
+ readonly table: "persons";
41
+ readonly review: "identity";
42
+ readonly roles: {};
43
+ };
44
+ readonly organization: {
45
+ readonly table: "organizations";
46
+ readonly review: "identity";
47
+ readonly roles: {};
48
+ };
49
+ readonly project: {
50
+ readonly table: "projects";
51
+ readonly review: "identity";
52
+ readonly roles: {};
53
+ };
54
+ };
55
+ /** A role word, a source provider, and the name after `custom:`. */
56
+ export declare const WORD_BODY = "[a-z0-9][a-z0-9_-]{0,62}";
57
+ export declare const TYPE_PATTERN: string;
58
+ export declare const TYPE_RULE: string;
59
+ export declare const rolesOf: (type: RecordEntityType) => [string, RecordRoleSpec][];
60
+ export declare const roleNames: (types: readonly RecordEntityType[], keep: (role: RecordRoleSpec) => boolean | undefined) => string[];
61
+ export declare const AMOUNT_ROLES: string[];
@@ -0,0 +1,26 @@
1
+ /** Types with a table of their own; anything else a source sends is `custom:<name>` and stays a record only. */
2
+ export const RECORD_ENTITY_TYPES = ["invoice", "payment", "person", "organization", "project"];
3
+ export const RECORD_TYPES = {
4
+ invoice: {
5
+ table: "invoices",
6
+ review: "proposal",
7
+ roles: { issuer: {}, counterparty: {}, project: {} },
8
+ },
9
+ payment: {
10
+ table: "payments",
11
+ review: "proposal",
12
+ roles: { payer: {}, payee: {}, project: {}, settles: { many: true, amount: true } },
13
+ },
14
+ person: { table: "persons", review: "identity", roles: {} },
15
+ organization: { table: "organizations", review: "identity", roles: {} },
16
+ project: { table: "projects", review: "identity", roles: {} },
17
+ };
18
+ /** A role word, a source provider, and the name after `custom:`. */
19
+ export const WORD_BODY = "[a-z0-9][a-z0-9_-]{0,62}";
20
+ export const TYPE_PATTERN = `^(${RECORD_ENTITY_TYPES.join("|")}|custom:${WORD_BODY})$`;
21
+ export const TYPE_RULE = `a type is one of ${RECORD_ENTITY_TYPES.join(", ")} or custom:<name>`;
22
+ export const rolesOf = (type) => Object.entries(RECORD_TYPES[type].roles);
23
+ export const roleNames = (types, keep) => [
24
+ ...new Set(types.flatMap((type) => rolesOf(type).flatMap(([name, role]) => (keep(role) ? [name] : [])))),
25
+ ];
26
+ export const AMOUNT_ROLES = roleNames(RECORD_ENTITY_TYPES, (role) => role.amount);
@@ -1,9 +1,7 @@
1
+ export { RECORD_ENTITY_TYPES, type RecordEntityType } from "./record-types.js";
1
2
  export declare const RECORDS_SCHEMA_VERSION = 1;
2
3
  export declare const RECORD_OPERATIONS: readonly ["upsert", "delete"];
3
- /** Types with a table of their own; anything else a source sends is `custom:<name>` and stays a record only. */
4
- export declare const RECORD_ENTITY_TYPES: readonly ["invoice", "payment", "person", "organization", "project"];
5
4
  export type RecordOperation = (typeof RECORD_OPERATIONS)[number];
6
- export type RecordEntityType = (typeof RECORD_ENTITY_TYPES)[number];
7
5
  export interface RecordRelation {
8
6
  /** What the target is to this record: `counterparty`, `payer`, `settles`, or a source's own word. */
9
7
  role: string;
@@ -99,7 +97,7 @@ export declare const RECORDS_ENVELOPE_SCHEMA: {
99
97
  };
100
98
  readonly type: {
101
99
  readonly type: "string";
102
- readonly pattern: "^(invoice|payment|person|organization|project|custom:[a-z0-9][a-z0-9_-]{0,62})$";
100
+ readonly pattern: string;
103
101
  };
104
102
  readonly external_id: {
105
103
  readonly type: "string";
@@ -138,7 +136,7 @@ export declare const RECORDS_ENVELOPE_SCHEMA: {
138
136
  };
139
137
  readonly type: {
140
138
  readonly type: "string";
141
- readonly pattern: "^(invoice|payment|person|organization|project|custom:[a-z0-9][a-z0-9_-]{0,62})$";
139
+ readonly pattern: string;
142
140
  };
143
141
  readonly external_id: {
144
142
  readonly type: "string";
@@ -1,11 +1,10 @@
1
1
  import { createHash } from "node:crypto";
2
2
  import { holdsCredential, secretNamed } from "../runs/secrets.js";
3
+ import { AMOUNT_ROLES, TYPE_PATTERN, TYPE_RULE, WORD_BODY } from "./record-types.js";
4
+ export { RECORD_ENTITY_TYPES } from "./record-types.js";
3
5
  export const RECORDS_SCHEMA_VERSION = 1;
4
6
  export const RECORD_OPERATIONS = ["upsert", "delete"];
5
- /** Types with a table of their own; anything else a source sends is `custom:<name>` and stays a record only. */
6
- export const RECORD_ENTITY_TYPES = ["invoice", "payment", "person", "organization", "project"];
7
- const WORD = "^[a-z0-9][a-z0-9_-]{0,62}$";
8
- const TYPE = "^(invoice|payment|person|organization|project|custom:[a-z0-9][a-z0-9_-]{0,62})$";
7
+ const WORD = `^${WORD_BODY}$`;
9
8
  const MAX_ID = 512;
10
9
  const MAX_FIELDS_BYTES = 65_536;
11
10
  const MAX_RELATIONS = 100;
@@ -41,7 +40,7 @@ export const RECORDS_ENVELOPE_SCHEMA = {
41
40
  additionalProperties: false,
42
41
  properties: {
43
42
  key: { type: "string", minLength: 1, maxLength: MAX_ID },
44
- type: { type: "string", pattern: TYPE },
43
+ type: { type: "string", pattern: TYPE_PATTERN },
45
44
  external_id: { type: "string", minLength: 1, maxLength: MAX_ID },
46
45
  operation: { enum: [...RECORD_OPERATIONS] },
47
46
  fields: {
@@ -58,7 +57,7 @@ export const RECORDS_ENVELOPE_SCHEMA = {
58
57
  additionalProperties: false,
59
58
  properties: {
60
59
  role: { type: "string", pattern: WORD },
61
- type: { type: "string", pattern: TYPE },
60
+ type: { type: "string", pattern: TYPE_PATTERN },
62
61
  external_id: { type: "string", minLength: 1, maxLength: MAX_ID },
63
62
  amount: {
64
63
  type: "string",
@@ -70,7 +69,7 @@ export const RECORDS_ENVELOPE_SCHEMA = {
70
69
  },
71
70
  };
72
71
  const wordPattern = new RegExp(WORD);
73
- const typePattern = new RegExp(TYPE);
72
+ const typePattern = new RegExp(TYPE_PATTERN);
74
73
  const decimalPattern = new RegExp(DECIMAL);
75
74
  const isObject = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
76
75
  const escapePointer = (segment) => String(segment).replaceAll("~", "~0").replaceAll("/", "~1");
@@ -115,13 +114,13 @@ const relationErrors = (relations, at) => {
115
114
  if (typeof relation.role !== "string" || !wordPattern.test(relation.role))
116
115
  return [invalid([...here, "role"], "a role is lowercase letters, digits, - and _")];
117
116
  if (typeof relation.type !== "string" || !typePattern.test(relation.type))
118
- return [invalid([...here, "type"], `a type is one of ${RECORD_ENTITY_TYPES.join(", ")} or custom:<name>`)];
117
+ return [invalid([...here, "type"], TYPE_RULE)];
119
118
  if (!idText(relation.external_id))
120
119
  return [invalid([...here, "external_id"], `an external id takes 1–${MAX_ID} characters`)];
121
120
  if (relation.amount !== undefined && (typeof relation.amount !== "string" || !decimalPattern.test(relation.amount)))
122
121
  return [invalid([...here, "amount"], 'an amount is a decimal string, such as "13000.00"')];
123
- if (relation.amount !== undefined && relation.role !== "settles")
124
- return [invalid([...here, "amount"], "only a settles relation carries an amount")];
122
+ if (relation.amount !== undefined && !AMOUNT_ROLES.includes(relation.role))
123
+ return [invalid([...here, "amount"], `only a ${AMOUNT_ROLES.join(" or ")} relation carries an amount`)];
125
124
  return [];
126
125
  });
127
126
  };
@@ -143,9 +142,7 @@ export const recordErrors = (record, index) => {
143
142
  if (!idText(record.key))
144
143
  return [invalid([...at, "key"], `a key takes 1–${MAX_ID} characters`)];
145
144
  if (typeof record.type !== "string" || !typePattern.test(record.type))
146
- return withKey([
147
- invalid([...at, "type"], `a type is one of ${RECORD_ENTITY_TYPES.join(", ")} or custom:<name>`, "record.type"),
148
- ]);
145
+ return withKey([invalid([...at, "type"], TYPE_RULE, "record.type")]);
149
146
  if (!idText(record.external_id))
150
147
  return withKey([invalid([...at, "external_id"], `an external id takes 1–${MAX_ID} characters`)]);
151
148
  if (!RECORD_OPERATIONS.includes(record.operation))
@@ -9,6 +9,10 @@ export declare const linkedPerson: v.StrictObjectSchema<{
9
9
  readonly username: v.NullableSchema<v.StringSchema<undefined>, undefined>;
10
10
  readonly is_bot: v.NullableSchema<v.BooleanSchema<undefined>, undefined>;
11
11
  readonly accounts: v.ArraySchema<v.SchemaWithPipe<readonly [v.StringSchema<undefined>, v.MinLengthAction<string, 1, undefined>]>, undefined>;
12
+ readonly seen_in: v.ArraySchema<v.StrictObjectSchema<{
13
+ readonly account: v.SchemaWithPipe<readonly [v.StringSchema<undefined>, v.MinLengthAction<string, 1, undefined>]>;
14
+ readonly scope: v.PicklistSchema<["personal", "work"], undefined>;
15
+ }, undefined>, undefined>;
12
16
  readonly method: v.StringSchema<undefined>;
13
17
  readonly linked_by: v.StringSchema<undefined>;
14
18
  readonly linked_at: v.SchemaWithPipe<readonly [v.StringSchema<undefined>, v.IsoTimestampAction<string, "an ISO 8601 time in UTC">, v.EndsWithAction<string, "Z", "an ISO 8601 time in UTC, ending in `Z`">]>;
@@ -8,6 +8,7 @@ const linkedIdentity = v.strictObject({
8
8
  username: v.nullable(text),
9
9
  is_bot: v.nullable(v.boolean()),
10
10
  accounts: v.array(id),
11
+ seen_in: v.array(v.strictObject({ account: id, scope: v.picklist(["personal", "work"]) })),
11
12
  method: text,
12
13
  linked_by: text,
13
14
  linked_at: time,
@@ -669,6 +669,10 @@ export declare const contactContext: v.UnionSchema<[v.StrictObjectSchema<{
669
669
  readonly username: v.NullableSchema<v.StringSchema<undefined>, undefined>;
670
670
  readonly is_bot: v.NullableSchema<v.BooleanSchema<undefined>, undefined>;
671
671
  readonly accounts: v.ArraySchema<v.SchemaWithPipe<readonly [v.StringSchema<undefined>, v.MinLengthAction<string, 1, undefined>]>, undefined>;
672
+ readonly seen_in: v.ArraySchema<v.StrictObjectSchema<{
673
+ readonly account: v.SchemaWithPipe<readonly [v.StringSchema<undefined>, v.MinLengthAction<string, 1, undefined>]>;
674
+ readonly scope: v.PicklistSchema<["personal", "work"], undefined>;
675
+ }, undefined>, undefined>;
672
676
  readonly method: v.StringSchema<undefined>;
673
677
  readonly linked_by: v.StringSchema<undefined>;
674
678
  readonly linked_at: v.SchemaWithPipe<readonly [v.StringSchema<undefined>, v.IsoTimestampAction<string, "an ISO 8601 time in UTC">, v.EndsWithAction<string, "Z", "an ISO 8601 time in UTC, ending in `Z`">]>;
@@ -676,6 +680,12 @@ export declare const contactContext: v.UnionSchema<[v.StrictObjectSchema<{
676
680
  readonly provider: v.StringSchema<undefined>;
677
681
  readonly id: v.SchemaWithPipe<readonly [v.StringSchema<undefined>, v.MinLengthAction<string, 1, undefined>]>;
678
682
  }, undefined>;
683
+ /** The accounts read: only those of the scopes the caller may read. */
684
+ readonly accounts: v.ArraySchema<v.StrictObjectSchema<{
685
+ readonly provider: v.StringSchema<undefined>;
686
+ readonly account: v.SchemaWithPipe<readonly [v.StringSchema<undefined>, v.MinLengthAction<string, 1, undefined>]>;
687
+ readonly scope: v.PicklistSchema<["personal", "work"], undefined>;
688
+ }, undefined>, undefined>;
679
689
  readonly shared: v.ArraySchema<v.StrictObjectSchema<{
680
690
  readonly provider: v.StringSchema<undefined>;
681
691
  readonly account: v.SchemaWithPipe<readonly [v.StringSchema<undefined>, v.MinLengthAction<string, 1, undefined>]>;
@@ -1459,6 +1469,10 @@ export declare const contactPreparation: v.StrictObjectSchema<{
1459
1469
  readonly username: v.NullableSchema<v.StringSchema<undefined>, undefined>;
1460
1470
  readonly is_bot: v.NullableSchema<v.BooleanSchema<undefined>, undefined>;
1461
1471
  readonly accounts: v.ArraySchema<v.SchemaWithPipe<readonly [v.StringSchema<undefined>, v.MinLengthAction<string, 1, undefined>]>, undefined>;
1472
+ readonly seen_in: v.ArraySchema<v.StrictObjectSchema<{
1473
+ readonly account: v.SchemaWithPipe<readonly [v.StringSchema<undefined>, v.MinLengthAction<string, 1, undefined>]>;
1474
+ readonly scope: v.PicklistSchema<["personal", "work"], undefined>;
1475
+ }, undefined>, undefined>;
1462
1476
  readonly method: v.StringSchema<undefined>;
1463
1477
  readonly linked_by: v.StringSchema<undefined>;
1464
1478
  readonly linked_at: v.SchemaWithPipe<readonly [v.StringSchema<undefined>, v.IsoTimestampAction<string, "an ISO 8601 time in UTC">, v.EndsWithAction<string, "Z", "an ISO 8601 time in UTC, ending in `Z`">]>;
@@ -1466,6 +1480,12 @@ export declare const contactPreparation: v.StrictObjectSchema<{
1466
1480
  readonly provider: v.StringSchema<undefined>;
1467
1481
  readonly id: v.SchemaWithPipe<readonly [v.StringSchema<undefined>, v.MinLengthAction<string, 1, undefined>]>;
1468
1482
  }, undefined>;
1483
+ /** The accounts read: only those of the scopes the caller may read. */
1484
+ readonly accounts: v.ArraySchema<v.StrictObjectSchema<{
1485
+ readonly provider: v.StringSchema<undefined>;
1486
+ readonly account: v.SchemaWithPipe<readonly [v.StringSchema<undefined>, v.MinLengthAction<string, 1, undefined>]>;
1487
+ readonly scope: v.PicklistSchema<["personal", "work"], undefined>;
1488
+ }, undefined>, undefined>;
1469
1489
  readonly shared: v.ArraySchema<v.StrictObjectSchema<{
1470
1490
  readonly provider: v.StringSchema<undefined>;
1471
1491
  readonly account: v.SchemaWithPipe<readonly [v.StringSchema<undefined>, v.MinLengthAction<string, 1, undefined>]>;
@@ -36,6 +36,8 @@ export const contextEvent = v.strictObject({
36
36
  });
37
37
  const context = v.strictObject({
38
38
  person: contextPerson,
39
+ /** The accounts read: only those of the scopes the caller may read. */
40
+ accounts: v.array(v.strictObject({ provider: text, account: id, scope: v.picklist(["personal", "work"]) })),
39
41
  shared: v.array(sharedChat),
40
42
  last: v.strictObject({
41
43
  from_them: v.nullable(contextMessage),
@@ -2689,6 +2689,10 @@ export declare const ENTITIES: {
2689
2689
  readonly username: v.NullableSchema<v.StringSchema<undefined>, undefined>;
2690
2690
  readonly is_bot: v.NullableSchema<v.BooleanSchema<undefined>, undefined>;
2691
2691
  readonly accounts: v.ArraySchema<v.SchemaWithPipe<readonly [v.StringSchema<undefined>, v.MinLengthAction<string, 1, undefined>]>, undefined>;
2692
+ readonly seen_in: v.ArraySchema<v.StrictObjectSchema<{
2693
+ readonly account: v.SchemaWithPipe<readonly [v.StringSchema<undefined>, v.MinLengthAction<string, 1, undefined>]>;
2694
+ readonly scope: v.PicklistSchema<["personal", "work"], undefined>;
2695
+ }, undefined>, undefined>;
2692
2696
  readonly method: v.StringSchema<undefined>;
2693
2697
  readonly linked_by: v.StringSchema<undefined>;
2694
2698
  readonly linked_at: v.SchemaWithPipe<readonly [v.StringSchema<undefined>, v.IsoTimestampAction<string, "an ISO 8601 time in UTC">, v.EndsWithAction<string, "Z", "an ISO 8601 time in UTC, ending in `Z`">]>;
@@ -2696,6 +2700,11 @@ export declare const ENTITIES: {
2696
2700
  readonly provider: v.StringSchema<undefined>;
2697
2701
  readonly id: v.SchemaWithPipe<readonly [v.StringSchema<undefined>, v.MinLengthAction<string, 1, undefined>]>;
2698
2702
  }, undefined>;
2703
+ readonly accounts: v.ArraySchema<v.StrictObjectSchema<{
2704
+ readonly provider: v.StringSchema<undefined>;
2705
+ readonly account: v.SchemaWithPipe<readonly [v.StringSchema<undefined>, v.MinLengthAction<string, 1, undefined>]>;
2706
+ readonly scope: v.PicklistSchema<["personal", "work"], undefined>;
2707
+ }, undefined>, undefined>;
2699
2708
  readonly shared: v.ArraySchema<v.StrictObjectSchema<{
2700
2709
  readonly provider: v.StringSchema<undefined>;
2701
2710
  readonly account: v.SchemaWithPipe<readonly [v.StringSchema<undefined>, v.MinLengthAction<string, 1, undefined>]>;
@@ -3465,6 +3474,10 @@ export declare const ENTITIES: {
3465
3474
  readonly username: v.NullableSchema<v.StringSchema<undefined>, undefined>;
3466
3475
  readonly is_bot: v.NullableSchema<v.BooleanSchema<undefined>, undefined>;
3467
3476
  readonly accounts: v.ArraySchema<v.SchemaWithPipe<readonly [v.StringSchema<undefined>, v.MinLengthAction<string, 1, undefined>]>, undefined>;
3477
+ readonly seen_in: v.ArraySchema<v.StrictObjectSchema<{
3478
+ readonly account: v.SchemaWithPipe<readonly [v.StringSchema<undefined>, v.MinLengthAction<string, 1, undefined>]>;
3479
+ readonly scope: v.PicklistSchema<["personal", "work"], undefined>;
3480
+ }, undefined>, undefined>;
3468
3481
  readonly method: v.StringSchema<undefined>;
3469
3482
  readonly linked_by: v.StringSchema<undefined>;
3470
3483
  readonly linked_at: v.SchemaWithPipe<readonly [v.StringSchema<undefined>, v.IsoTimestampAction<string, "an ISO 8601 time in UTC">, v.EndsWithAction<string, "Z", "an ISO 8601 time in UTC, ending in `Z`">]>;
@@ -3472,6 +3485,11 @@ export declare const ENTITIES: {
3472
3485
  readonly provider: v.StringSchema<undefined>;
3473
3486
  readonly id: v.SchemaWithPipe<readonly [v.StringSchema<undefined>, v.MinLengthAction<string, 1, undefined>]>;
3474
3487
  }, undefined>;
3488
+ readonly accounts: v.ArraySchema<v.StrictObjectSchema<{
3489
+ readonly provider: v.StringSchema<undefined>;
3490
+ readonly account: v.SchemaWithPipe<readonly [v.StringSchema<undefined>, v.MinLengthAction<string, 1, undefined>]>;
3491
+ readonly scope: v.PicklistSchema<["personal", "work"], undefined>;
3492
+ }, undefined>, undefined>;
3475
3493
  readonly shared: v.ArraySchema<v.StrictObjectSchema<{
3476
3494
  readonly provider: v.StringSchema<undefined>;
3477
3495
  readonly account: v.SchemaWithPipe<readonly [v.StringSchema<undefined>, v.MinLengthAction<string, 1, undefined>]>;
@@ -3748,6 +3766,10 @@ export declare const ENTITIES: {
3748
3766
  readonly username: v.NullableSchema<v.StringSchema<undefined>, undefined>;
3749
3767
  readonly is_bot: v.NullableSchema<v.BooleanSchema<undefined>, undefined>;
3750
3768
  readonly accounts: v.ArraySchema<v.SchemaWithPipe<readonly [v.StringSchema<undefined>, v.MinLengthAction<string, 1, undefined>]>, undefined>;
3769
+ readonly seen_in: v.ArraySchema<v.StrictObjectSchema<{
3770
+ readonly account: v.SchemaWithPipe<readonly [v.StringSchema<undefined>, v.MinLengthAction<string, 1, undefined>]>;
3771
+ readonly scope: v.PicklistSchema<["personal", "work"], undefined>;
3772
+ }, undefined>, undefined>;
3751
3773
  readonly method: v.StringSchema<undefined>;
3752
3774
  readonly linked_by: v.StringSchema<undefined>;
3753
3775
  readonly linked_at: v.SchemaWithPipe<readonly [v.StringSchema<undefined>, v.IsoTimestampAction<string, "an ISO 8601 time in UTC">, v.EndsWithAction<string, "Z", "an ISO 8601 time in UTC, ending in `Z`">]>;
@@ -1,4 +1,4 @@
1
- import { type CliError } from "@wirecat/cli-base";
1
+ import { CliError } from "@wirecat/cli-base";
2
2
  /**
3
3
  * **By shape, not by class.** A package linked during development brings its own copy of
4
4
  * cli-base, and an error built with one copy is not an `instanceof` the other's `CliError` —
@@ -14,3 +14,19 @@ export declare const isCommanderFailure: (value: unknown) => value is Error & {
14
14
  };
15
15
  /** What failed, never what it said: a messenger's refusal can quote the message back. */
16
16
  export declare const failureKey: (error: unknown) => string;
17
+ /**
18
+ * SQLite's primary result code: node:sqlite's `errcode`, or `errno` only on bun:sqlite's `SQLiteError`. A filesystem
19
+ * error's errno shares the low byte with SQLite's codes: Windows' EIO, -4070, reads as SQLITE_NOTADB.
20
+ */
21
+ export declare const sqliteCode: (error: unknown) => number | undefined;
22
+ /**
23
+ * A damaged store file as the owner's problem, not a bug: SQLITE_CORRUPT or SQLITE_NOTADB becomes `store.damaged`.
24
+ * `next` is the command line that says what to do, such as `tg store recover`, only where that command exists.
25
+ * Only for the CLI's own store: a file the owner named goes through `damagedFile`. Any other failure is returned as
26
+ * it came.
27
+ */
28
+ export declare const storeDamaged: (error: unknown, next?: string) => unknown;
29
+ /** Runs `work` on store files the owner named, so a damaged one is named rather than taken for the CLI's own. */
30
+ export declare const damagedFile: <T>(files: readonly string[], work: () => Promise<T>) => Promise<T>;
31
+ /** Whether `error` is SQLite saying the database file is damaged. */
32
+ export declare const isStoreDamage: (error: unknown) => boolean;
@@ -1,4 +1,4 @@
1
- import { errorCodes } from "@wirecat/cli-base";
1
+ import { CliError, errorCodes } from "@wirecat/cli-base";
2
2
  /**
3
3
  * **By shape, not by class.** A package linked during development brings its own copy of
4
4
  * cli-base, and an error built with one copy is not an `instanceof` the other's `CliError` —
@@ -16,3 +16,59 @@ export const isCommanderFailure = (value) => {
16
16
  };
17
17
  /** What failed, never what it said: a messenger's refusal can quote the message back. */
18
18
  export const failureKey = (error) => isCliFailure(error) ? error.code : error instanceof Error ? error.name : "error";
19
+ const SQLITE_CORRUPT = 11;
20
+ const SQLITE_NOTADB = 26;
21
+ const DAMAGED_TEXT = /database disk image is malformed|file is not a database|fts5: corruption/i;
22
+ /**
23
+ * SQLite's primary result code: node:sqlite's `errcode`, or `errno` only on bun:sqlite's `SQLiteError`. A filesystem
24
+ * error's errno shares the low byte with SQLite's codes: Windows' EIO, -4070, reads as SQLITE_NOTADB.
25
+ */
26
+ export const sqliteCode = (error) => {
27
+ const { errcode, errno, name } = (error ?? {});
28
+ const code = typeof errcode === "number" ? errcode : name === "SQLiteError" && typeof errno === "number" ? errno : null;
29
+ return code === null ? undefined : code & 0xff;
30
+ };
31
+ /** The SQLite failure that says the database file is damaged, on the error or up to five `cause`s behind it. */
32
+ const damageIn = (error) => {
33
+ if (isCliFailure(error) || isCommanderFailure(error))
34
+ return undefined;
35
+ let at = error;
36
+ for (let depth = 0; depth < 5 && at instanceof Error; depth += 1) {
37
+ const code = sqliteCode(at);
38
+ if ((code !== undefined && [SQLITE_CORRUPT, SQLITE_NOTADB].includes(code)) || DAMAGED_TEXT.test(at.message))
39
+ return at;
40
+ at = at.cause;
41
+ }
42
+ return undefined;
43
+ };
44
+ // The record's frames are where SQLite failed, not where the failure was renamed.
45
+ const wrapping = (error, original) => {
46
+ Object.defineProperty(error, "cause", { value: original, configurable: true, writable: true });
47
+ if (original instanceof Error && original.stack)
48
+ error.stack = original.stack;
49
+ return error;
50
+ };
51
+ /**
52
+ * A damaged store file as the owner's problem, not a bug: SQLITE_CORRUPT or SQLITE_NOTADB becomes `store.damaged`.
53
+ * `next` is the command line that says what to do, such as `tg store recover`, only where that command exists.
54
+ * Only for the CLI's own store: a file the owner named goes through `damagedFile`. Any other failure is returned as
55
+ * it came.
56
+ */
57
+ export const storeDamaged = (error, next) => damageIn(error)
58
+ ? wrapping(new CliError("configuration_error", `the store file is damaged${next ? `; \`${next}\` says what is damaged and what to do` : ""}`, { reason: "store.damaged" }), error)
59
+ : error;
60
+ /** Runs `work` on store files the owner named, so a damaged one is named rather than taken for the CLI's own. */
61
+ export const damagedFile = async (files, work) => {
62
+ try {
63
+ return await work();
64
+ }
65
+ catch (error) {
66
+ if (!damageIn(error))
67
+ throw error;
68
+ throw wrapping(new CliError("validation_error", `${files.join(" or ")} is damaged or not a database`, {
69
+ reason: "store.file-damaged",
70
+ }), error);
71
+ }
72
+ };
73
+ /** Whether `error` is SQLite saying the database file is damaged. */
74
+ export const isStoreDamage = (error) => damageIn(error) !== undefined;
@@ -157,6 +157,17 @@ const OTHERS = [
157
157
  ],
158
158
  validation("store.refused", "the local store refused the document text"),
159
159
  ["store.not-writable", "permission_error", "the message store cannot be written from this process"],
160
+ [
161
+ "store.damaged",
162
+ "configuration_error",
163
+ "the store file is damaged; store recover says what is lost and recovers it",
164
+ ],
165
+ validation("store.file-damaged", "the named store file is damaged or not a database"),
166
+ [
167
+ "store.unreadable",
168
+ "configuration_error",
169
+ "the store file's header or schema is damaged, so it cannot be recovered",
170
+ ],
160
171
  validation("store.in-use", "a process or a scheduled timer has the store open; stop it, then recover the store"),
161
172
  [
162
173
  "store.sqlite3-missing",
@@ -240,6 +251,11 @@ const OTHERS = [
240
251
  validation("order.status", "a rejected or deleted order's status does not change"),
241
252
  ["model.not-downloaded", "not_found", "the model is not downloaded on this machine"],
242
253
  ["person.no-owner", "not_found", "no person is marked as the owner in the store"],
254
+ [
255
+ "identity.separated",
256
+ "conflict",
257
+ "the owner separated this identity from that person; only the owner links it again",
258
+ ],
243
259
  ["transcription.no-key", "authentication_error", "no OpenAI key is set for transcription"],
244
260
  validation("transcription.too-large", "the audio is over the transcription provider's size limit"),
245
261
  [
package/dist/mcp/tool.js CHANGED
@@ -8,6 +8,7 @@ import { DEFAULT_OUTPUT_BYTES } from "../cli/execution.js";
8
8
  import { MAX_BUFFERED_INPUT } from "../cli/input-policy.js";
9
9
  import { refuseLacking } from "../cli/messenger/lacks.js";
10
10
  import { ENTITIES } from "../entities/index.js";
11
+ import { storeDamaged } from "../errors/failures.js";
11
12
  import { failureContext } from "../errors/reasons.js";
12
13
  import { startRecording } from "../runs/recording.js";
13
14
  import { newRunId } from "../runs/run.js";
@@ -317,7 +318,7 @@ export const answered = (answer, maxBytes = DEFAULT_OUTPUT_BYTES, targets) => {
317
318
  export const failed = (error, errors, effects) => {
318
319
  const judged = judgedEffects(effects);
319
320
  const context = { ...failureContext(errors), ...(judged ? { effects: judged } : {}) };
320
- return mcpErrorResult(JSON.parse(agentJson(errorObjectOf(error, context))));
321
+ return mcpErrorResult(JSON.parse(agentJson(errorObjectOf(storeDamaged(error), context))));
321
322
  };
322
323
  /** An option's name as an MCP argument: `beforeId` is `before_id`. */
323
324
  export const snakeOf = (key) => key.replace(/[A-Z]/g, (letter) => `_${letter.toLowerCase()}`);
@@ -1,5 +1,7 @@
1
1
  import { processStreams } from "@wirecat/cli-base";
2
- import { isCliFailure } from "../errors/failures.js";
2
+ import { isCliFailure, storeDamaged } from "../errors/failures.js";
3
+ import { markStoreDamaged } from "../store/marks.js";
4
+ import { storePath } from "../store/path.js";
3
5
  import { endpointTemplate, errorDetailOf, framesOf } from "./error-detail.js";
4
6
  import { providerErrorKey, renderEvent } from "./events.js";
5
7
  import { DIAGNOSTIC_LOG_EVENTS } from "./log-events.js";
@@ -106,7 +108,14 @@ export const startRecording = (options) => {
106
108
  ...(partial ? { partial } : {}),
107
109
  });
108
110
  },
109
- fail: async (error) => {
111
+ fail: async (thrown) => {
112
+ const error = storeDamaged(thrown);
113
+ markStoreDamaged(thrown, {
114
+ store: storePath(options.env),
115
+ command: `${options.app.command} ${options.command}`,
116
+ version: options.app.version,
117
+ runId,
118
+ });
110
119
  const failed = run ?? (held ? keep(open({ startedAt })) : undefined);
111
120
  if (failed) {
112
121
  if (!isCliFailure(error))
@@ -121,8 +130,8 @@ export const startRecording = (options) => {
121
130
  ...(run ? {} : { keptBecauseFailed: true }),
122
131
  });
123
132
  }
124
- if (typeof error === "object" && error !== null)
125
- settled.add(error);
133
+ if (typeof thrown === "object" && thrown !== null)
134
+ settled.add(thrown);
126
135
  },
127
136
  };
128
137
  };
@@ -2,7 +2,8 @@
2
2
  * Free text as the search reads it: a character the query language takes as syntax where it cannot be syntax — the
3
3
  * colon of `10:30` or `HH:MM`, a URL's slashes, an unbalanced quote or bracket — is made literal by quoting the word
4
4
  * that holds it, a `word:` that names no search field is that word, and a bare emoji or punctuation mark beside other
5
- * words is dropped. A query that already reads well comes back unchanged, so this can run again on its own output;
5
+ * words is dropped. A `?` that ends a bare word is a question mark, not a one-character wildcard. A query that
6
+ * already reads well, and has no such word, comes back unchanged, so this can run again on its own output;
6
7
  * field queries and Boolean operators keep their meaning. A field's own mistake (`kind:nonsense`) is left for the
7
8
  * search to report. `fields` are the names a list of its own (memories: `text`, `scope`) reads as fields.
8
9
  */
@@ -56,6 +56,13 @@ const read = (text, fields) => {
56
56
  const unknown = leaves.find(({ field }) => !fields.has(field));
57
57
  if (unknown)
58
58
  return { at: text.lastIndexOf(unknown.field, unknown.span.start) };
59
+ // "invoice?" and "что с бюджетом?" ask a question; a `?` inside a word (`inv?ice`) stays one character.
60
+ const questions = leaves.flatMap(({ implicit, operator, value, span }) => {
61
+ const mark = /^[^*?]*[^*?\\]\?+$/u.test(value) && implicit && operator === "wildcard" ? /\?+$/u.exec(value) : null;
62
+ return mark ? [span.end - mark[0].length] : [];
63
+ });
64
+ if (questions.length)
65
+ return { questions };
59
66
  // An emoji or a lone `^` is no word of the index, so ANDed with the rest it would find nothing at all.
60
67
  const wordless = leaves.filter(({ implicit, operator, value }) => implicit && (operator === "term" || operator === "phrase") && !/[\p{L}\p{N}]/u.test(value));
61
68
  const words = wordless.length < leaves.length;
@@ -79,7 +86,8 @@ const without = (text, leaves) => [...leaves]
79
86
  * Free text as the search reads it: a character the query language takes as syntax where it cannot be syntax — the
80
87
  * colon of `10:30` or `HH:MM`, a URL's slashes, an unbalanced quote or bracket — is made literal by quoting the word
81
88
  * that holds it, a `word:` that names no search field is that word, and a bare emoji or punctuation mark beside other
82
- * words is dropped. A query that already reads well comes back unchanged, so this can run again on its own output;
89
+ * words is dropped. A `?` that ends a bare word is a question mark, not a one-character wildcard. A query that
90
+ * already reads well, and has no such word, comes back unchanged, so this can run again on its own output;
83
91
  * field queries and Boolean operators keep their meaning. A field's own mistake (`kind:nonsense`) is left for the
84
92
  * search to report. `fields` are the names a list of its own (memories: `text`, `scope`) reads as fields.
85
93
  */
@@ -94,6 +102,12 @@ export const plainQuery = (text, fields = SEARCH_FIELDS) => {
94
102
  catch {
95
103
  return text;
96
104
  }
105
+ if ("questions" in reading) {
106
+ current = reading.questions
107
+ .sort((a, b) => b - a)
108
+ .reduce((left, at) => left.slice(0, at) + left.slice(at).replace(/^\?+/u, (marks) => "\\?".repeat(marks.length)), current);
109
+ continue;
110
+ }
97
111
  if ("wordless" in reading) {
98
112
  // Punctuation alone (`!!!`) is no search; the parser's error says so better than an empty answer.
99
113
  if (!reading.words && current !== text)
@@ -1,5 +1,5 @@
1
1
  import { CliError } from "@wirecat/cli-base";
2
- import { isCliFailure } from "../errors/failures.js";
2
+ import { isCliFailure, sqliteCode } from "../errors/failures.js";
3
3
  import { typedCommand } from "../runs/scope.js";
4
4
  /** The store with the running account bound: the default for a service's reads and writes. */
5
5
  export const accountStore = async (deps) => (await deps.store()).forAccount(await deps.account());
@@ -10,16 +10,14 @@ export const accountStore = async (deps) => (await deps.store()).forAccount(awai
10
10
  export const crossAccount = (deps, _reason) => deps.store();
11
11
  const NO_LOCAL_STORE = "this answer comes from the messenger, not from the local store";
12
12
  /**
13
- * Busy: another process (`serve`) held the write lock past `busy_timeout`. Node's `node:sqlite` sets `errcode`,
14
- * Bun's `bun:sqlite` sets `errno`; the low byte is the primary code, so the extended BUSY codes match too.
13
+ * Busy: another process (`serve`) held the write lock past `busy_timeout`; `sqliteCode` is the primary code, so the
14
+ * extended BUSY codes match too.
15
15
  */
16
16
  const SQLITE_BUSY = 5;
17
17
  const storeAbsent = (error) => {
18
18
  if (isCliFailure(error))
19
19
  return error.code === "validation_error" && error.message === NO_LOCAL_STORE;
20
- const { errcode, errno } = (error ?? {});
21
- const code = errcode ?? errno;
22
- return error instanceof Error && typeof code === "number" && (code & 0xff) === SQLITE_BUSY;
20
+ return error instanceof Error && sqliteCode(error) === SQLITE_BUSY;
23
21
  };
24
22
  /** `not_found`: nothing has recorded whose account this profile is; `authentication_error`: the connection has no self. */
25
23
  const accountUnknown = (error) => isCliFailure(error) && (error.code === "not_found" || error.code === "authentication_error");
@@ -96,7 +96,7 @@ export type { ModerateOptions, ModerationService, ShownRules } from "./moderatio
96
96
  export { moderationService } from "./moderation.js";
97
97
  export type { ContactSync, PeopleService } from "./people.js";
98
98
  export { linkPeople, peopleService, phoneOf } from "./people.js";
99
- export type { ContextEvent, ContextMessage, ContextOptions, NotReadReason, PersonContext, SharedChat, } from "./person-context.js";
99
+ export type { AccountScope, ContextEvent, ContextMessage, ContextOptions, NotReadReason, PersonContext, SharedChat, } from "./person-context.js";
100
100
  export { CONTEXT_BYTES, CONTEXT_MESSAGES, identityIn, personContext } from "./person-context.js";
101
101
  export type { PersonPreparation, WaitingDialog } from "./person-preparation.js";
102
102
  export { PREPARE_BYTES, personPreparation } from "./person-preparation.js";
@@ -1,6 +1,7 @@
1
1
  import type { LinkScorer } from "./types.js";
2
2
  /**
3
- * A person who wrote 5 or more messages in the window in one of the project's confirmed chats, found by the sender's
4
- * identity, never by the name a message shows. In a group of more than 30 people writing there says less.
3
+ * A person who wrote 5 or more messages in the window in one of the project's confirmed chats, or any in a dialog or a
4
+ * group of 10 people or fewer, found by the sender's identity, never by the name a message shows. In a group of more
5
+ * than 30 people writing there says less.
5
6
  */
6
7
  export declare const chatSenders: LinkScorer;
@@ -3,15 +3,18 @@ import { contactRef, personRefOf, sameAccount } from "./shared.js";
3
3
  const WEIGHT = 0.5;
4
4
  const LARGE_GROUP_WEIGHT = 0.2;
5
5
  const LARGE_GROUP = 30;
6
+ const SMALL_CHAT = 10;
6
7
  const MIN_MESSAGES = 5;
7
8
  const READ_LIMIT = 2000;
8
9
  /**
9
- * A person who wrote 5 or more messages in the window in one of the project's confirmed chats, found by the sender's
10
- * identity, never by the name a message shows. In a group of more than 30 people writing there says less.
10
+ * A person who wrote 5 or more messages in the window in one of the project's confirmed chats, or any in a dialog or a
11
+ * group of 10 people or fewer, found by the sender's identity, never by the name a message shows. In a group of more
12
+ * than 30 people writing there says less.
11
13
  */
12
14
  export const chatSenders = {
13
15
  id: "chat-senders",
14
- version: 1,
16
+ version: 2,
17
+ spreads: true,
15
18
  candidates: async ({ store, project, confirmed, sources, since, owner, signal }) => {
16
19
  if (!project)
17
20
  return [];
@@ -35,7 +38,7 @@ export const chatSenders = {
35
38
  const size = Math.max((await store.roster.members(key, reference.chat)).length, senders.size);
36
39
  const weight = size > LARGE_GROUP ? LARGE_GROUP_WEIGHT : WEIGHT;
37
40
  for (const [id, { count, last }] of senders) {
38
- if (count < MIN_MESSAGES)
41
+ if (size > SMALL_CHAT && count < MIN_MESSAGES)
39
42
  continue;
40
43
  const person = await personRefOf(store, key.provider, id);
41
44
  if (!person || person === owner)