@wirecat/cli-core 0.546.0 → 0.548.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 (112) hide show
  1. package/CHANGELOG.md +173 -0
  2. package/dist/calendar/cli/answers.d.ts +7 -2
  3. package/dist/calendar/cli/answers.js +12 -4
  4. package/dist/calendar/cli/index.js +9 -8
  5. package/dist/calendar/feeds.d.ts +19 -0
  6. package/dist/calendar/feeds.js +52 -9
  7. package/dist/calendar/source.d.ts +10 -2
  8. package/dist/calendar/sync.d.ts +2 -0
  9. package/dist/calendar/sync.js +10 -6
  10. package/dist/calendar/testing/memory.js +39 -0
  11. package/dist/cli/messenger/history-query.d.ts +10 -4
  12. package/dist/cli/messenger/history-query.js +87 -31
  13. package/dist/cli/messenger/messages-command.js +10 -4
  14. package/dist/cli/messenger/messages-comments-command.js +4 -2
  15. package/dist/cli/messenger/store-maintenance-command.d.ts +1 -1
  16. package/dist/cli/option-hints.d.ts +6 -0
  17. package/dist/cli/option-hints.js +15 -0
  18. package/dist/errors/reasons.js +13 -1
  19. package/dist/mail/cli/index.js +4 -4
  20. package/dist/mail/index.d.ts +2 -0
  21. package/dist/mail/index.js +2 -0
  22. package/dist/mail/partial.d.ts +34 -0
  23. package/dist/mail/partial.js +94 -0
  24. package/dist/mail/server-search.js +17 -9
  25. package/dist/mail/source.d.ts +13 -2
  26. package/dist/mail/source.js +7 -1
  27. package/dist/mail/sync-types.d.ts +112 -0
  28. package/dist/mail/sync-types.js +1 -0
  29. package/dist/mail/sync.d.ts +4 -106
  30. package/dist/mail/sync.js +49 -4
  31. package/dist/mail/testing/fake.d.ts +3 -1
  32. package/dist/mail/testing/fake.js +10 -2
  33. package/dist/meetings/event-store.d.ts +10 -0
  34. package/dist/meetings/testing/event-store-contract.js +36 -0
  35. package/dist/services/deps.d.ts +0 -2
  36. package/dist/services/deps.js +0 -2
  37. package/dist/services/documents/ingestion.js +1 -1
  38. package/dist/services/documents/knowledge.d.ts +10 -6
  39. package/dist/services/documents/knowledge.js +25 -11
  40. package/dist/services/index.d.ts +1 -0
  41. package/dist/services/index.js +1 -0
  42. package/dist/services/messages.d.ts +2 -0
  43. package/dist/services/messages.js +25 -23
  44. package/dist/services/proposal-tasks.d.ts +3 -2
  45. package/dist/services/proposal-tasks.js +5 -2
  46. package/dist/services/proposals/ids.d.ts +5 -0
  47. package/dist/services/proposals/ids.js +43 -0
  48. package/dist/services/proposals/index.d.ts +3 -0
  49. package/dist/services/proposals/index.js +2 -0
  50. package/dist/services/proposals/kinds/action.d.ts +7 -0
  51. package/dist/services/proposals/kinds/action.js +45 -0
  52. package/dist/services/proposals/kinds/fact.d.ts +4 -0
  53. package/dist/services/proposals/kinds/fact.js +43 -0
  54. package/dist/services/proposals/kinds/findings.d.ts +7 -0
  55. package/dist/services/proposals/kinds/findings.js +75 -0
  56. package/dist/services/proposals/kinds/key-document.d.ts +9 -0
  57. package/dist/services/proposals/kinds/key-document.js +75 -0
  58. package/dist/services/proposals/kinds/memory.d.ts +4 -0
  59. package/dist/services/proposals/kinds/memory.js +42 -0
  60. package/dist/services/proposals/kinds/relationship.d.ts +10 -0
  61. package/dist/services/proposals/kinds/relationship.js +82 -0
  62. package/dist/services/proposals/kinds/shared.d.ts +25 -0
  63. package/dist/services/proposals/kinds/shared.js +49 -0
  64. package/dist/services/proposals/registry.d.ts +9 -0
  65. package/dist/services/proposals/registry.js +129 -0
  66. package/dist/services/proposals/types.d.ts +85 -0
  67. package/dist/services/proposals/types.js +1 -0
  68. package/dist/services/server-search.d.ts +7 -4
  69. package/dist/services/server-search.js +5 -2
  70. package/dist/services/tasks.js +8 -4
  71. package/dist/store/decisions.d.ts +65 -0
  72. package/dist/store/decisions.js +55 -0
  73. package/dist/store/evidence-findings.d.ts +6 -6
  74. package/dist/store/index.d.ts +4 -3
  75. package/dist/store/index.js +2 -1
  76. package/dist/store/meeting-findings.d.ts +2 -0
  77. package/dist/store/messages.d.ts +4 -9
  78. package/dist/store/sqlite/accounts.d.ts +8 -0
  79. package/dist/store/sqlite/accounts.js +2 -2
  80. package/dist/store/sqlite/actors.d.ts +13 -2
  81. package/dist/store/sqlite/actors.js +24 -1
  82. package/dist/store/sqlite/calendar-forget.d.ts +17 -0
  83. package/dist/store/sqlite/calendar-forget.js +68 -0
  84. package/dist/store/sqlite/calendar.js +8 -0
  85. package/dist/store/sqlite/email-locations.d.ts +16 -0
  86. package/dist/store/sqlite/email-locations.js +26 -0
  87. package/dist/store/sqlite/emails.d.ts +6 -2
  88. package/dist/store/sqlite/emails.js +2 -1
  89. package/dist/store/sqlite/evidence-findings.js +13 -3
  90. package/dist/store/sqlite/facts.d.ts +16 -8
  91. package/dist/store/sqlite/facts.js +48 -15
  92. package/dist/store/sqlite/findings-core.d.ts +2 -1
  93. package/dist/store/sqlite/findings-core.js +4 -4
  94. package/dist/store/sqlite/knowledge.d.ts +43 -7
  95. package/dist/store/sqlite/knowledge.js +55 -15
  96. package/dist/store/sqlite/meeting-findings.js +6 -6
  97. package/dist/store/sqlite/memories.d.ts +12 -8
  98. package/dist/store/sqlite/memories.js +33 -10
  99. package/dist/store/sqlite/notes.d.ts +5 -1
  100. package/dist/store/sqlite/notes.js +18 -2
  101. package/dist/store/sqlite/open-store.d.ts +4 -1
  102. package/dist/store/sqlite/open-store.js +8 -2
  103. package/dist/store/sqlite/open.d.ts +3 -0
  104. package/dist/store/sqlite/proposed-actions.d.ts +21 -8
  105. package/dist/store/sqlite/proposed-actions.js +29 -9
  106. package/dist/store/sqlite/reads.d.ts +13 -2
  107. package/dist/store/sqlite/reads.js +8 -4
  108. package/dist/store/sqlite/relation-proposals.d.ts +34 -0
  109. package/dist/store/sqlite/relation-proposals.js +114 -0
  110. package/dist/store/store.d.ts +3 -0
  111. package/dist/store/store.js +2 -0
  112. package/package.json +1 -1
@@ -1,8 +1,13 @@
1
1
  import { type QueryAst } from "@wirecat/cli-base/query";
2
+ import { type Decider } from "../decisions.js";
2
3
  import type { AccountKey } from "../types.js";
3
4
  import { RELATION_KINDS } from "./link-kinds.js";
4
5
  import { type MeetingProjectsStore } from "./meeting-projects.js";
6
+ import type { Author } from "./memories.js";
7
+ import { type Link } from "./notes.js";
5
8
  import type { StoreContext } from "./open.js";
9
+ import type { ProposedAction } from "./proposed-actions.js";
10
+ import { type LinkDecision } from "./relation-proposals.js";
6
11
  import type { TagCreationOptions } from "./tag-creation.js";
7
12
  import { type Label, type TagKind, type TagRow } from "./tags.js";
8
13
  import { type ThingState } from "./things.js";
@@ -177,21 +182,43 @@ export interface KnowledgeStore extends VocabularyStore, MeetingProjectsStore {
177
182
  description?: string;
178
183
  }): Promise<Project>;
179
184
  projects(query?: QueryAst): Promise<Project[]>;
180
- relate(key: KnowledgeScope, input: {
185
+ /** The owner's relationship is a link; `confirmed: false` keeps it a weak suggestion only the owner decides. */
186
+ relate(key: KnowledgeScope, input: RelateInput & {
187
+ by: "owner";
188
+ }): Promise<KnowledgeRelation>;
189
+ /** An agent's relationship is a `relationship` proposal for the owner, never a link. */
190
+ relate(key: KnowledgeScope, input: RelateInput & {
191
+ by: Exclude<Author, "owner">;
192
+ }): Promise<ProposedAction>;
193
+ /**
194
+ * An agent's or a rule's relationship, written as a `relationship` proposal for the owner instead of a link. The
195
+ * same pending pair returns the proposal already held.
196
+ */
197
+ proposeRelation(key: KnowledgeScope, input: {
181
198
  from: string;
182
199
  to: string;
183
200
  kind: KnowledgeRelation["kind"];
184
201
  role?: string;
185
- evidence?: string;
186
- confirmed?: boolean;
187
- provenance?: string;
188
- }): Promise<KnowledgeRelation>;
202
+ evidence?: string[];
203
+ reason?: string;
204
+ }, by: Exclude<Author, "owner">): Promise<ProposedAction>;
205
+ /** Decides a stored `relationship` or `key_document` proposal; the link and the decision land together. */
206
+ decideLinkProposal(id: string, decision: LinkDecision): Promise<{
207
+ proposal: ProposedAction;
208
+ link: Link | null;
209
+ }>;
210
+ /** Decides an unconfirmed link written before proposals were stored. Its author is unknown, so only the owner may. */
211
+ decideSuggestedLink(id: string, decision: LinkDecision): Promise<Link | null>;
189
212
  relations(key: KnowledgeScope, reference?: string): Promise<KnowledgeRelation[]>;
190
- removeRelation(key: KnowledgeScope, id: string): Promise<{
213
+ /**
214
+ * Removing an unconfirmed relationship rejects it, so `by` must be allowed to decide it. Removing a link to a
215
+ * project, or rejecting any suggestion, leaves a rejected `relationship` proposal so a scan never proposes it again.
216
+ */
217
+ removeRelation(key: KnowledgeScope, id: string, by: Decider): Promise<{
191
218
  id: string;
192
219
  removed: true;
193
220
  }>;
194
- confirmRelation(key: KnowledgeScope, id: string): Promise<KnowledgeRelation>;
221
+ confirmRelation(key: KnowledgeScope, id: string, by: Decider): Promise<KnowledgeRelation>;
195
222
  schedule(key: AccountKey, task: string, dueAt: string, timezone: string): Promise<Reminder>;
196
223
  reminders(key: AccountKey, query?: QueryAst): Promise<Reminder[]>;
197
224
  cancelReminder(key: AccountKey, id: string): Promise<Reminder>;
@@ -202,4 +229,13 @@ export interface KnowledgeStore extends VocabularyStore, MeetingProjectsStore {
202
229
  }): Promise<Reminder[]>;
203
230
  acknowledgeReminder(key: AccountKey, id: string, receipt: string): Promise<Reminder>;
204
231
  }
232
+ export interface RelateInput {
233
+ from: string;
234
+ to: string;
235
+ kind: KnowledgeRelation["kind"];
236
+ role?: string;
237
+ evidence?: string;
238
+ confirmed?: boolean;
239
+ provenance?: string;
240
+ }
205
241
  export declare const knowledgeStoreOver: (context: StoreContext) => KnowledgeStore;
@@ -6,6 +6,7 @@ import { normalizeTag } from "../../domain/tags.js";
6
6
  import { normalizeVocabularyName } from "../../domain/vocabulary.js";
7
7
  import { formatMeetingReference } from "../../meetings/index.js";
8
8
  import { isClosedState } from "../../tasks/index.js";
9
+ import { assertMayDecide } from "../decisions.js";
9
10
  import { fold } from "../normalize.js";
10
11
  import { ulid } from "../ulid.js";
11
12
  import { ownerPerson } from "./actors.js";
@@ -14,6 +15,7 @@ import { meetingProjectsStoreOver } from "./meeting-projects.js";
14
15
  import { holdingText } from "./note-index.js";
15
16
  import { insertLink, linkOf } from "./notes.js";
16
17
  import { queryFilter } from "./query-filter.js";
18
+ import { relationProposalsOver } from "./relation-proposals.js";
17
19
  import { addTags as addThingTags, canonicalTagNamed, createTag, labelsOf, liveTagging, removeTags as removeThingTags, setMainTopic, } from "./tags.js";
18
20
  import { INBOX_KEY_LENGTH, TASK_KEY_PREFIXES } from "./task-keys.js";
19
21
  import { inboxKey, taskAccountOf, taskIdOf, taskRowOf } from "./tasks.js";
@@ -24,6 +26,11 @@ const TYPED_KEY_PREFIXES = new Set(Object.values(TASK_KEY_PREFIXES));
24
26
  export const ORGANIZATION_KINDS = ["company", "team", "family", "community"];
25
27
  export const PROJECT_TYPES = ["work", "client", "personal", "oss", "other"];
26
28
  export const SCOPES = ["personal", "work"];
29
+ /**
30
+ * A link records no actor, only free-text provenance anyone could write, so whoever proposed one is unverified and
31
+ * only the owner decides it.
32
+ */
33
+ const UNVERIFIED = { type: "unknown" };
27
34
  const iso = (value) => new Date(Number(value)).toISOString();
28
35
  const bounded = (limit = 100, offset = 0) => {
29
36
  if (!Number.isInteger(limit) ||
@@ -226,6 +233,7 @@ export const knowledgeStoreOver = (context) => {
226
233
  confidenceCategory: link.confirmed ? "owner-confirmed" : "weak",
227
234
  createdAt: link.createdAt,
228
235
  });
236
+ const proposals = relationProposalsOver(context);
229
237
  const RELATIONS = `SELECT * FROM links WHERE kind IN (${RELATION_KINDS.map(linkKind).join(",")}) AND to_id IS NOT NULL`;
230
238
  const relation = (id) => {
231
239
  const row = /^\d+$/.test(id) ? database.prepare(`${RELATIONS} AND id = ?`).get(Number(id)) : undefined;
@@ -233,6 +241,19 @@ export const knowledgeStoreOver = (context) => {
233
241
  throw new CliError("not_found", "no relationship with that id");
234
242
  return row;
235
243
  };
244
+ /** The two ends of a relationship, checked, and the link that already joins them. */
245
+ const pair = (key, input) => {
246
+ const from = related(key, input.from, true);
247
+ const to = related(key, input.to);
248
+ if ((from.type === to.type && from.id === to.id) || !RELATION_KINDS.includes(input.kind))
249
+ throw new CliError("validation_error", "a relationship needs distinct references and a supported kind");
250
+ if ((from.type === "chat" || from.type === "email_thread") && (input.kind !== "member-of" || to.type !== "project"))
251
+ throw new CliError("validation_error", "a chat is related only as member-of a project");
252
+ const existing = database
253
+ .prepare(`${RELATIONS} AND from_type=? AND from_id=? AND to_type=? AND to_id=? AND kind=?`)
254
+ .get(from.type, from.id, to.type, to.id, input.kind);
255
+ return { from, to, existing };
256
+ };
236
257
  /** A chat may only be the member: its reference names its own account, so it is never another account's. */
237
258
  const related = (key, reference, member = false) => {
238
259
  checked(key);
@@ -517,17 +538,19 @@ export const knowledgeStoreOver = (context) => {
517
538
  .all(...(where?.params ?? []))
518
539
  .map(projectOf);
519
540
  },
520
- relate: async (key, input) => {
521
- const from = related(key, input.from, true);
522
- const to = related(key, input.to);
523
- if ((from.type === to.type && from.id === to.id) || !RELATION_KINDS.includes(input.kind))
524
- throw new CliError("validation_error", "a relationship needs distinct references and a supported kind");
525
- if ((from.type === "chat" || from.type === "email_thread") &&
526
- (input.kind !== "member-of" || to.type !== "project"))
527
- throw new CliError("validation_error", "a chat is related only as member-of a project");
528
- const existing = database
529
- .prepare(`${RELATIONS} AND from_type=? AND from_id=? AND to_type=? AND to_id=? AND kind=?`)
530
- .get(from.type, from.id, to.type, to.id, input.kind);
541
+ relate: (async (key, input) => {
542
+ if (input.by === undefined)
543
+ throw new CliError("validation_error", "a relationship names who writes it: the owner or an agent", {
544
+ reason: "proposal.decider-required",
545
+ });
546
+ const { from, existing } = pair(key, input);
547
+ if (input.by !== "owner") {
548
+ if (existing)
549
+ throw new CliError("validation_error", `relationship ${String(existing.id)} already links them`, {
550
+ reason: "relationship.exists",
551
+ });
552
+ return proposals.propose(from, { ...input, ...(input.evidence === undefined ? {} : { reason: input.evidence }), evidence: [] }, input.by);
553
+ }
531
554
  if (existing)
532
555
  return relationOf(linkOf(database, existing));
533
556
  const confirmed = input.confirmed !== false;
@@ -541,7 +564,17 @@ export const knowledgeStoreOver = (context) => {
541
564
  ...(input.evidence === undefined ? {} : { evidence: input.evidence }),
542
565
  ...(input.provenance === undefined ? {} : { provenance: input.provenance }),
543
566
  })));
567
+ }),
568
+ proposeRelation: async (key, input, by) => {
569
+ const { from, existing } = pair(key, input);
570
+ if (existing)
571
+ throw new CliError("validation_error", `relationship ${String(existing.id)} already links them`, {
572
+ reason: "relationship.exists",
573
+ });
574
+ return proposals.propose(from, input, by);
544
575
  },
576
+ decideLinkProposal: async (id, decision) => proposals.decideProposal(id, decision),
577
+ decideSuggestedLink: async (id, decision) => proposals.decideLink(id, decision),
545
578
  relations: async (key, reference) => {
546
579
  checked(key);
547
580
  const thing = reference === undefined ? undefined : thingOf(database, reference);
@@ -552,16 +585,23 @@ export const knowledgeStoreOver = (context) => {
552
585
  .all(thing?.type ?? null, thing?.type ?? null, thing?.id ?? null, thing?.type ?? null, thing?.id ?? null)
553
586
  .map((row) => relationOf(linkOf(database, row)));
554
587
  },
555
- removeRelation: async (key, id) => {
588
+ removeRelation: async (key, id, by) => {
556
589
  checked(key);
557
- relation(id);
558
- database.prepare("DELETE FROM links WHERE id = ?").run(Number(id));
590
+ const row = relation(id);
591
+ const link = linkOf(database, row);
592
+ assertMayDecide({ id: `link:${id}`, proposedBy: UNVERIFIED }, by, context.decisions);
593
+ write(database, () => {
594
+ if (!link.confirmed || String(row.to_type) === "project")
595
+ proposals.rejected(link, by);
596
+ database.prepare("DELETE FROM links WHERE id = ?").run(Number(id));
597
+ });
559
598
  return { id, removed: true };
560
599
  },
561
- confirmRelation: async (key, id) => {
600
+ confirmRelation: async (key, id, by) => {
562
601
  checked(key);
563
602
  const row = relation(id);
564
603
  const link = linkOf(database, row);
604
+ assertMayDecide({ id: `link:${id}`, proposedBy: UNVERIFIED }, by, context.decisions);
565
605
  related(key, link.from, true);
566
606
  related(key, String(link.to));
567
607
  database
@@ -1,5 +1,6 @@
1
1
  import { CliError } from "@wirecat/cli-base";
2
2
  import { findingHash, MEETING_FINDINGS_KIND, meetingCueFingerprint, meetingFindingFingerprint, parseMeetingReference, validateMeetingFindings, } from "../../meetings/index.js";
3
+ import { assertMayDecide, deciderAuthor } from "../decisions.js";
3
4
  import { findingsCoreOver } from "./findings-core.js";
4
5
  import { authorActor } from "./memories.js";
5
6
  import { proposalOperationsOver } from "./proposed-actions.js";
@@ -176,6 +177,9 @@ export const meetingFindingsStoreOver = (context) => {
176
177
  decision.accept.some((candidate) => !batch.input.findings.some((finding) => finding.id === candidate)))
177
178
  return invalid("accept only unique IDs shown in this batch");
178
179
  const accepted = [...decision.accept].sort();
180
+ if (decision.by === undefined)
181
+ return invalid("a findings review names who decides: the owner or an agent");
182
+ assertMayDecide({ id, proposedBy: proposals.proposer(id) }, decision.by, context.decisions);
179
183
  if (batch.receipt) {
180
184
  if (JSON.stringify(batch.receipt.accepted) !== JSON.stringify(accepted))
181
185
  return invalid("this findings batch was already reviewed with a different decision");
@@ -191,12 +195,8 @@ export const meetingFindingsStoreOver = (context) => {
191
195
  bot: proposal.proposedBy.name,
192
196
  ...(input.agentModel === null ? {} : { model: input.agentModel }),
193
197
  };
194
- const items = core.outcomes(account, batch.input, accepted, agent);
195
- return view(proposals.approve(id, {
196
- previewFingerprint: batch.fingerprint,
197
- accepted,
198
- items,
199
- }));
198
+ const items = core.outcomes(account, batch.input, accepted, agent, decision.by);
199
+ return view(proposals.approve(id, { previewFingerprint: batch.fingerprint, accepted, items }, deciderAuthor(decision.by)));
200
200
  }),
201
201
  };
202
202
  };
@@ -1,12 +1,14 @@
1
1
  import { MEMORY_KINDS, MEMORY_SCOPES, type MEMORY_STATUSES } from "../../domain/knowledge.js";
2
- import { type Actor } from "./actors.js";
2
+ import { type Decider } from "../decisions.js";
3
+ import { type Actor, type ProposedByFilter } from "./actors.js";
3
4
  import { type EvidenceSource } from "./evidence-sources.js";
4
5
  import { type NoteIndexState } from "./note-index.js";
5
6
  import type { StoreContext } from "./open.js";
6
- /** Who wrote it: the owner, or an agent by its bot handle and the model it ran on. */
7
+ /** Who wrote it: the owner, or an agent (or a rule scorer) by its bot handle and the model it ran on. */
7
8
  export type Author = "owner" | {
8
9
  bot: string;
9
10
  model?: string;
11
+ kind?: "agent" | "rule";
10
12
  };
11
13
  export interface Memory {
12
14
  id: string;
@@ -22,11 +24,12 @@ export interface Memory {
22
24
  /** The project it belongs to, as `project:<id>`. */
23
25
  project: string | null;
24
26
  projectName: string | null;
25
- /** `name` is a bot's handle; `null` for a person. */
27
+ /** `name` is a bot's handle and `kind` whether it is an agent or a rule; both `null` for a person. */
26
28
  author: {
27
29
  type: Actor["type"];
28
30
  id: string;
29
31
  name: string | null;
32
+ kind: "agent" | "rule" | null;
30
33
  };
31
34
  model: string | null;
32
35
  confidence: number | null;
@@ -63,21 +66,22 @@ export interface MemoriesStore {
63
66
  kind?: Memory["kind"];
64
67
  status?: Memory["status"];
65
68
  scope?: Memory["scope"];
69
+ proposedBy?: ProposedByFilter;
66
70
  limit?: number;
67
71
  }): Promise<Memory[]>;
68
72
  /** Moves it to another project, or to none with `null`. */
69
73
  update(reference: string, change: {
70
74
  project: string | null;
71
75
  }): Promise<Memory>;
72
- /** The owner stands behind it; its evidence was checked now. */
73
- confirm(reference: string): Promise<Memory>;
74
- /** Its evidence no longer holds. */
75
- markStale(reference: string): Promise<Memory>;
76
+ /** The owner, or an agent for the owner, stands behind it; its evidence was checked now. */
77
+ confirm(reference: string, by: Decider): Promise<Memory>;
78
+ /** Its evidence no longer holds; for an agent's memory this is how it is turned down, so `by` must be allowed. */
79
+ markStale(reference: string, by: Decider): Promise<Memory>;
76
80
  /**
77
81
  * Erases the memory: its row, its text in the search and meaning indexes, and every link from or to it. A memory
78
82
  * that replaced it no longer names it. A backup made before keeps it.
79
83
  */
80
- remove(reference: string): Promise<{
84
+ remove(reference: string, by: Decider): Promise<{
81
85
  ref: string;
82
86
  removed: true;
83
87
  links: number;
@@ -1,7 +1,8 @@
1
1
  import { CliError } from "@wirecat/cli-base";
2
2
  import { MEMORY_KINDS, MEMORY_SCOPES } from "../../domain/knowledge.js";
3
+ import { assertMayDecide, assertMayWithdraw } from "../decisions.js";
3
4
  import { normalize } from "../normalize.js";
4
- import { botNamed, ownerPerson } from "./actors.js";
5
+ import { botNamed, ownerPerson, proposedBySql, proposerOf } from "./actors.js";
5
6
  import { evidenceSourcesOf } from "./evidence-sources.js";
6
7
  import { linkKind } from "./link-kinds.js";
7
8
  import { CORPORA, corpusIndexState, drainCorpus } from "./note-index.js";
@@ -9,7 +10,7 @@ import { requiredThing } from "./notes.js";
9
10
  import { stemmerCache } from "./stems.js";
10
11
  import { referenceOfThing, storedThing } from "./things.js";
11
12
  import { write } from "./transaction.js";
12
- const NAMED = "SELECT m.*, b.name AS author_name FROM memories m LEFT JOIN bots b ON m.author_type = 'bot' AND b.id = m.author_id";
13
+ const NAMED = "SELECT m.*, b.name AS author_name, b.kind AS author_kind FROM memories m LEFT JOIN bots b ON m.author_type = 'bot' AND b.id = m.author_id";
13
14
  const iso = (value) => new Date(Number(value)).toISOString();
14
15
  const oneOf = (value, allowed, what) => {
15
16
  if (typeof value !== "string" || !allowed.includes(value))
@@ -20,7 +21,7 @@ export const authorActor = (context, author) => {
20
21
  if (author === "owner")
21
22
  return { actor: ownerPerson(context.database), model: null };
22
23
  return {
23
- actor: botNamed(context.database, author.bot, context.now(), "agent"),
24
+ actor: botNamed(context.database, author.bot, context.now(), author.kind ?? "agent"),
24
25
  model: author.model === undefined ? null : author.model.trim().slice(0, 200) || null,
25
26
  };
26
27
  };
@@ -28,6 +29,7 @@ export const authorOf = (row) => ({
28
29
  type: row.author_type,
29
30
  id: String(row.author_id),
30
31
  name: row.author_name == null ? null : String(row.author_name),
32
+ kind: row.author_type !== "bot" ? null : row.author_kind === "rule" || row.author_name === "rule" ? "rule" : "agent",
31
33
  });
32
34
  /** Writes `evidence` links from a row to each reference, refusing one the store does not hold. */
33
35
  export const linkEvidence = (context, from, references, author) => {
@@ -153,6 +155,10 @@ export const memoriesStoreOver = (context) => {
153
155
  throw new CliError("not_found", `${reference} is a fact now: store.facts holds it`);
154
156
  return row;
155
157
  };
158
+ const decided = (reference, by) => {
159
+ const row = rowOf(reference);
160
+ assertMayDecide({ id: `memory:${row.id}`, proposedBy: proposerOf(database, row.author_type, row.author_id) }, by, context.decisions);
161
+ };
156
162
  const setStatus = (reference, status, verified) => {
157
163
  const row = rowOf(reference);
158
164
  const at = now();
@@ -179,6 +185,10 @@ export const memoriesStoreOver = (context) => {
179
185
  if (input.confidence !== undefined && !(input.confidence >= 0 && input.confidence <= 1))
180
186
  throw new CliError("validation_error", "confidence takes 0–1");
181
187
  const status = oneOf(input.status ?? "proposed", ["proposed", "confirmed"], "a new memory's status");
188
+ if (status === "confirmed" && input.author !== "owner")
189
+ throw new CliError("permission_error", "an agent's memory waits as proposed; only the owner confirms it", {
190
+ reason: "proposal.owner-only",
191
+ });
182
192
  const id = write(database, () => {
183
193
  const { actor, model } = authorActor(context, input.author);
184
194
  const subject = input.subject === undefined ? undefined : requiredThing(database, input.subject, "the subject");
@@ -201,11 +211,12 @@ export const memoriesStoreOver = (context) => {
201
211
  list: async (options = {}) => {
202
212
  const subject = options.subject === undefined ? undefined : requiredThing(database, options.subject);
203
213
  const project = options.project === undefined ? null : projectId(database, options.project);
214
+ const by = proposedBySql("m.author_type", "m.author_id", options.proposedBy);
204
215
  return database
205
216
  .prepare(`${NAMED} WHERE m.deleted_at IS NULL AND (? IS NULL OR (m.subject_type = ? AND m.subject_id = ?)) ` +
206
217
  "AND (? IS NULL OR m.project_id = ?) AND (? IS NULL OR m.kind = ?) AND (? IS NULL OR m.status = ?) AND (? IS NULL OR m.scope = ?) " +
207
- "ORDER BY m.created_at DESC, m.id DESC LIMIT ?")
208
- .all(subject?.type ?? null, subject?.type ?? null, subject?.id ?? null, project, project, options.kind ?? null, options.kind ?? null, options.status ?? null, options.status ?? null, options.scope ?? null, options.scope ?? null, Math.min(Math.max(options.limit ?? 100, 1), 500))
218
+ `AND ${by.sql} ORDER BY m.created_at DESC, m.id DESC LIMIT ?`)
219
+ .all(subject?.type ?? null, subject?.type ?? null, subject?.id ?? null, project, project, options.kind ?? null, options.kind ?? null, options.status ?? null, options.status ?? null, options.scope ?? null, options.scope ?? null, ...by.params, Math.min(Math.max(options.limit ?? 100, 1), 500))
209
220
  .map(memoryOf);
210
221
  },
211
222
  update: async (reference, change) => write(database, () => {
@@ -214,10 +225,22 @@ export const memoriesStoreOver = (context) => {
214
225
  database.prepare("UPDATE memories SET project_id = ?, updated_at = ? WHERE id = ?").run(project, now(), id);
215
226
  return memoryOf(rowOf(`memory:${id}`));
216
227
  }),
217
- confirm: async (reference) => setStatus(reference, "confirmed", true),
218
- markStale: async (reference) => setStatus(reference, "stale", false),
219
- remove: async (reference) => {
220
- const id = Number(rowOf(reference).id);
228
+ confirm: async (reference, by) => {
229
+ decided(reference, by);
230
+ return setStatus(reference, "confirmed", true);
231
+ },
232
+ markStale: async (reference, by) => {
233
+ decided(reference, by);
234
+ return setStatus(reference, "stale", false);
235
+ },
236
+ remove: async (reference, by) => {
237
+ const row = rowOf(reference);
238
+ assertMayWithdraw({
239
+ id: `memory:${row.id}`,
240
+ proposedBy: proposerOf(database, row.author_type, row.author_id),
241
+ proposed: row.status === "proposed",
242
+ }, by);
243
+ const id = Number(row.id);
221
244
  const links = erase(database, () => {
222
245
  keepHighestId(database, "memories", now());
223
246
  database.prepare("UPDATE memories SET supersedes_id = NULL WHERE supersedes_id = ?").run(id);
@@ -238,7 +261,7 @@ export const memoriesStoreOver = (context) => {
238
261
  throw new CliError("validation_error", "say what to find in the memories");
239
262
  const match = `normalized_text : (${words.map((word) => `"${word.replaceAll('"', '""')}"*`).join(" AND ")})`;
240
263
  return database
241
- .prepare("SELECT m.*, b.name AS author_name FROM memory_words w JOIN memories m ON m.id = w.rowid " +
264
+ .prepare("SELECT m.*, b.name AS author_name, b.kind AS author_kind FROM memory_words w JOIN memories m ON m.id = w.rowid " +
242
265
  "LEFT JOIN bots b ON m.author_type = 'bot' AND b.id = m.author_id WHERE memory_words MATCH ? " +
243
266
  "AND m.deleted_at IS NULL AND (? IS NULL OR (m.subject_type = ? AND m.subject_id = ?)) " +
244
267
  "AND (? IS NULL OR m.scope = ?) ORDER BY bm25(memory_words), m.id LIMIT ?")
@@ -1,4 +1,5 @@
1
1
  import type { Section } from "../../readers/types.js";
2
+ import { type Decider } from "../decisions.js";
2
3
  import type { CacheDatabase } from "../driver.js";
3
4
  import { type DocumentMetadata } from "./document-metadata.js";
4
5
  import { type ImportedDocuments } from "./imported-documents.js";
@@ -81,6 +82,8 @@ export interface LinkInput {
81
82
  evidence?: string;
82
83
  provenance?: string;
83
84
  confirmed?: boolean;
85
+ /** Who writes it: a confirmed link needs the owner. */
86
+ by?: Decider;
84
87
  }
85
88
  export interface FileNoteInput {
86
89
  folderId: string;
@@ -151,7 +154,8 @@ export interface NotesStore extends NoteSearch, ImportedDocuments {
151
154
  }>;
152
155
  addLink(input: LinkInput): Promise<Link>;
153
156
  links(options?: LinkListFilter): Promise<Link[]>;
154
- removeLink(id: string): Promise<{
157
+ /** Only the owner removes a link: it records no actor, so who proposed it cannot be checked. */
158
+ removeLink(id: string, by: Decider): Promise<{
155
159
  id: string;
156
160
  removed: true;
157
161
  }>;
@@ -4,6 +4,7 @@ import { googleDocumentLinks } from "../../domain/google-documents.js";
4
4
  import { DOCUMENT_ACCOUNT_PROVIDERS, filesAccountSql, folderProviderOf, folderProviderSql, isDocumentAccountProvider, } from "../../domain/providers.js";
5
5
  import { parseReference } from "../../domain/references.js";
6
6
  import { normalizeTag } from "../../domain/tags.js";
7
+ import { assertMayWithdraw } from "../decisions.js";
7
8
  import { fold } from "../normalize.js";
8
9
  import { ulid } from "../ulid.js";
9
10
  import { ownerPerson } from "./actors.js";
@@ -149,6 +150,20 @@ export const resolvePersonLinks = (database, names) => {
149
150
  if (check.get(key))
150
151
  resolveKey(database, key);
151
152
  };
153
+ /** A confirmed link is the owner's statement, of any kind; an agent's link is written unconfirmed. */
154
+ const ownerLink = (input) => {
155
+ if (input.confirmed === false)
156
+ return input;
157
+ if (input.by === undefined)
158
+ throw new CliError("validation_error", "a confirmed link names who writes it: the owner", {
159
+ reason: "proposal.decider-required",
160
+ });
161
+ if (input.by !== "owner")
162
+ throw new CliError("permission_error", "an agent's link is written unconfirmed; only the owner confirms one", {
163
+ reason: "proposal.owner-only",
164
+ });
165
+ return input;
166
+ };
152
167
  /** Writes one link; an identical resolved link is answered instead of doubled. */
153
168
  export const insertLink = (context, input) => {
154
169
  const { database, now } = context;
@@ -592,7 +607,7 @@ export const notesStoreOver = (context) => {
592
607
  database.prepare("DELETE FROM notes WHERE id = ?").run(Number(bare));
593
608
  return { id, removed: true };
594
609
  }),
595
- addLink: async (input) => write(database, () => insertLink(context, input)),
610
+ addLink: async (input) => write(database, () => insertLink(context, ownerLink(input))),
596
611
  links: async (options = {}) => {
597
612
  const pair = (reference) => {
598
613
  if (reference === undefined)
@@ -617,7 +632,8 @@ export const notesStoreOver = (context) => {
617
632
  .all(from.type, from.type, from.id, to.type, to.type, to.id, options.unresolved ? 1 : 0, ...filters.map(({ value }) => value))
618
633
  .map((row) => linkOf(database, row));
619
634
  },
620
- removeLink: async (id) => write(database, () => {
635
+ removeLink: async (id, by) => write(database, () => {
636
+ assertMayWithdraw({ id: `link:${id}`, proposedBy: { type: "unknown" }, proposed: false }, by);
621
637
  if (!/^\d+$/.test(id) || !database.prepare("DELETE FROM links WHERE id = ?").run(Number(id)).changes)
622
638
  throw new CliError("not_found", `no link ${id} in the local store`);
623
639
  return { id, removed: true };
@@ -1,3 +1,4 @@
1
+ import { type DecisionPolicy } from "../decisions.js";
1
2
  import { type StoreContext } from "./open.js";
2
3
  export interface StoreOptions {
3
4
  /** Open an existing compatible store without maintenance; SQLite refuses writes. */
@@ -11,6 +12,8 @@ export interface StoreOptions {
11
12
  version?: string;
12
13
  /** Where the notice of an upgrade that locks other CLIs out goes; stderr, since stdout may carry JSON or MCP. */
13
14
  warn?: (line: string) => void;
15
+ /** The host's decision policy (its `agent_decisions`); the default lets an agent decide with a reason. */
16
+ decisions?: DecisionPolicy;
14
17
  }
15
18
  export declare const BACKFILL_ON_OPEN = 5000;
16
- export declare const openStoreContext: ({ path, env, now, command, version, warn, readOnly, }?: StoreOptions) => Promise<StoreContext>;
19
+ export declare const openStoreContext: ({ path, env, now, command, version, warn, readOnly, decisions, }?: StoreOptions) => Promise<StoreContext>;
@@ -1,6 +1,7 @@
1
1
  import { mkdirSync, statSync, writeFileSync } from "node:fs";
2
2
  import { dirname } from "node:path";
3
3
  import { CliError } from "@wirecat/cli-base";
4
+ import { DEFAULT_DECISION_POLICY } from "../decisions.js";
4
5
  import { migrate } from "../migrations.js";
5
6
  import { storeCapable } from "../open.js";
6
7
  import { storePath } from "../path.js";
@@ -31,7 +32,7 @@ const createWritable = (file) => {
31
32
  throw new CliError("permission_error", "the message store cannot be written from here; run where it is writable, or set MESSAGING_STORE", { reason: "store.not-writable" });
32
33
  }
33
34
  };
34
- export const openStoreContext = async ({ path, env, now = Date.now, command, version, warn = (line) => process.stderr.write(`${line}\n`), readOnly = false, } = {}) => {
35
+ export const openStoreContext = async ({ path, env, now = Date.now, command, version, warn = (line) => process.stderr.write(`${line}\n`), readOnly = false, decisions = DEFAULT_DECISION_POLICY, } = {}) => {
35
36
  if (typeof readOnly !== "boolean")
36
37
  throw new CliError("validation_error", "readOnly must be a boolean");
37
38
  await storeCapable();
@@ -68,7 +69,12 @@ export const openStoreContext = async ({ path, env, now = Date.now, command, ver
68
69
  });
69
70
  }
70
71
  loadProviders(database);
71
- return { ...opened, now, sharedConfig: sharedConfigPath(file, env ?? process.env) };
72
+ return {
73
+ ...opened,
74
+ now,
75
+ sharedConfig: sharedConfigPath(file, env ?? process.env),
76
+ decisions,
77
+ };
72
78
  }
73
79
  catch (error) {
74
80
  database.close();
@@ -1,3 +1,4 @@
1
+ import type { DecisionPolicy } from "../decisions.js";
1
2
  import { type CacheDatabase } from "../driver.js";
2
3
  import type { SQLiteAsyncDatabase } from "./drizzle/core.js";
3
4
  /** Drizzle over the store's connection. Synchronous inside: a transaction never awaits (plan D3). */
@@ -11,6 +12,8 @@ export interface StoreContext extends OpenedSqlite {
11
12
  now: () => number;
12
13
  /** The owner's shared config file, whose `task_categories` edits the store's list; none in a bare context. */
13
14
  sharedConfig?: string;
15
+ /** Who may decide proposals, set once by the host at open; every decide method reads it. */
16
+ decisions?: DecisionPolicy;
14
17
  }
15
18
  /**
16
19
  * One SQLite connection, seen two ways: the `CacheDatabase` seam the hand-written SQL and the
@@ -1,5 +1,6 @@
1
+ import { type Decider, type Proposer } from "../decisions.js";
1
2
  import type { AccountKey } from "../types.js";
2
- import type { Actor } from "./actors.js";
3
+ import { type Actor, type ProposedByFilter } from "./actors.js";
3
4
  import { type Author } from "./memories.js";
4
5
  import type { StoreContext } from "./open.js";
5
6
  export declare const PROPOSAL_STATUSES: readonly ["proposed", "approved", "rejected", "executed", "failed"];
@@ -18,11 +19,12 @@ export interface ProposedAction {
18
19
  payload: unknown;
19
20
  reason: string | null;
20
21
  status: (typeof PROPOSAL_STATUSES)[number];
21
- /** `name` is a bot's handle; `null` for a person. */
22
+ /** `name` is a bot's handle and `kind` whether it is an agent or a rule; both `null` for a person. */
22
23
  proposedBy: {
23
24
  type: Actor["type"];
24
25
  id: string;
25
26
  name: string | null;
27
+ kind: "agent" | "rule" | null;
26
28
  };
27
29
  decidedBy: {
28
30
  type: Actor["type"];
@@ -50,14 +52,20 @@ export interface ProposedActionsStore {
50
52
  pending(options?: {
51
53
  limit?: number;
52
54
  }): Promise<ProposedAction[]>;
53
- /** Newest first; every status unless one is named. */
55
+ /** Newest first; every status unless one is named, every kind unless `kinds` names some or `except` leaves some out. */
54
56
  list(options?: {
55
57
  status?: ProposedAction["status"];
56
58
  limit?: number;
59
+ kinds?: readonly string[];
60
+ except?: readonly string[];
61
+ proposedBy?: ProposedByFilter;
62
+ /** A `project:<id>` the action targets or its payload names as `to`, `from` or `project`. */
63
+ project?: string;
57
64
  }): Promise<ProposedAction[]>;
58
- /** `result` records what the approval made, such as the task it became. */
59
- approve(id: string, result?: unknown): Promise<ProposedAction>;
60
- reject(id: string): Promise<ProposedAction>;
65
+ /** `result` records what the approval made, such as the task it became. `by` must be allowed to decide it. */
66
+ approve(id: string, by: Decider, result?: unknown): Promise<ProposedAction>;
67
+ /** `reason` is kept as the result: why it was turned down. */
68
+ reject(id: string, by: Decider, reason?: string): Promise<ProposedAction>;
61
69
  /** An approved action the adapter carried out, with what the provider answered. */
62
70
  executed(id: string, result?: unknown): Promise<ProposedAction>;
63
71
  failed(id: string, error: string): Promise<ProposedAction>;
@@ -65,10 +73,15 @@ export interface ProposedActionsStore {
65
73
  }
66
74
  type ProposalOperations = Omit<{
67
75
  [K in keyof ProposedActionsStore]: ProposedActionsStore[K] extends (...args: infer A) => Promise<infer R> ? (...args: A) => R : never;
68
- }, "approve"> & {
69
- /** `by` records an agent deciding for the owner; the public store always records the owner. */
76
+ }, "approve" | "reject"> & {
77
+ /** Records a decision already checked; `by` records an agent deciding for the owner. */
70
78
  approve(id: string, result?: unknown, by?: Author): ProposedAction;
79
+ reject(id: string, by?: Author, result?: unknown): ProposedAction;
80
+ /** Who proposed it, for `mayDecide`. */
81
+ proposer(id: string): Proposer;
71
82
  };
83
+ /** Who proposed an action, for `mayDecide`. */
84
+ export declare const proposerOfAction: ({ proposedBy }: ProposedAction) => Proposer;
72
85
  export declare const proposalOperationsOver: (context: StoreContext) => ProposalOperations;
73
86
  export declare const proposedActionsStoreOver: (context: StoreContext) => ProposedActionsStore;
74
87
  export {};