@wirecat/cli-core 0.606.0 → 0.608.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 +196 -0
- package/dist/calendar/index.d.ts +1 -0
- package/dist/calendar/index.js +1 -0
- package/dist/calendar/reads.js +5 -0
- package/dist/cli/app-mcp-command.d.ts +6 -2
- package/dist/cli/app-mcp-command.js +11 -2
- package/dist/cli/csv.d.ts +10 -0
- package/dist/cli/csv.js +22 -0
- package/dist/cli/early-refusal.d.ts +10 -0
- package/dist/cli/early-refusal.js +19 -0
- package/dist/cli/index.d.ts +3 -0
- package/dist/cli/index.js +3 -0
- package/dist/cli/profile-token.d.ts +29 -0
- package/dist/cli/profile-token.js +65 -0
- package/dist/cli/program.js +4 -2
- package/dist/crm/cli/csv.d.ts +23 -0
- package/dist/crm/cli/csv.js +135 -0
- package/dist/crm/cli/index.d.ts +1 -1
- package/dist/crm/cli/index.js +33 -21
- package/dist/crm/cli/pipeline-view.d.ts +3 -0
- package/dist/crm/cli/pipeline-view.js +36 -0
- package/dist/crm/writes.d.ts +6 -4
- package/dist/crm/writes.js +17 -13
- package/dist/documents.d.ts +2 -0
- package/dist/documents.js +2 -0
- package/dist/domain/business.d.ts +79 -0
- package/dist/domain/business.js +42 -0
- package/dist/domain/finance.d.ts +2 -0
- package/dist/domain/finance.js +2 -2
- package/dist/domain/google-documents.d.ts +2 -0
- package/dist/domain/google-documents.js +3 -1
- package/dist/domain/money.d.ts +2 -0
- package/dist/domain/money.js +11 -3
- package/dist/domain/providers.d.ts +36 -1
- package/dist/domain/providers.js +69 -7
- package/dist/domain/record-fields.d.ts +42 -0
- package/dist/domain/record-fields.js +122 -0
- package/dist/domain/record-types.d.ts +489 -16
- package/dist/domain/record-types.js +374 -7
- package/dist/domain/records.d.ts +96 -5
- package/dist/domain/records.js +178 -32
- package/dist/domain/ref-summaries.js +5 -0
- package/dist/domain/references.d.ts +1 -1
- package/dist/domain/references.js +5 -0
- package/dist/domain/vcard.d.ts +25 -0
- package/dist/domain/vcard.js +267 -0
- package/dist/entities/deal.d.ts +14 -0
- package/dist/entities/deal.js +3 -0
- package/dist/entities/index.d.ts +12 -0
- package/dist/errors/reasons.js +63 -4
- package/dist/fetch/addresses.d.ts +5 -0
- package/dist/fetch/addresses.js +76 -0
- package/dist/fetch/page.d.ts +64 -0
- package/dist/fetch/page.js +256 -0
- package/dist/index.d.ts +5 -3
- package/dist/index.js +5 -3
- package/dist/meetings/calendar-text.d.ts +1 -1
- package/dist/meetings/calendar-text.js +4 -0
- package/dist/meetings/event-store.d.ts +2 -2
- package/dist/search/account-list.d.ts +4 -0
- package/dist/search/account-list.js +19 -8
- package/dist/search/query.js +2 -2
- package/dist/services/events-search.js +2 -2
- package/dist/services/index.d.ts +1 -1
- package/dist/services/index.js +1 -1
- package/dist/services/ingest.d.ts +56 -8
- package/dist/services/ingest.js +95 -61
- package/dist/services/messages-search.js +3 -2
- package/dist/services/metadata.d.ts +1 -1
- package/dist/services/notes-search.js +1 -0
- package/dist/services/proposals/kinds/contracts.d.ts +4 -0
- package/dist/services/proposals/kinds/contracts.js +9 -0
- package/dist/services/proposals/kinds/finance.d.ts +4 -0
- package/dist/services/proposals/kinds/finance.js +14 -35
- package/dist/services/proposals/kinds/orders.js +7 -30
- package/dist/services/proposals/kinds/shared.d.ts +27 -1
- package/dist/services/proposals/kinds/shared.js +34 -0
- package/dist/services/proposals/kinds/source-review.d.ts +4 -0
- package/dist/services/proposals/kinds/source-review.js +45 -0
- package/dist/services/proposals/kinds/subscriptions.d.ts +9 -0
- package/dist/services/proposals/kinds/subscriptions.js +24 -0
- package/dist/services/proposals/kinds/transactions.d.ts +4 -0
- package/dist/services/proposals/kinds/transactions.js +10 -0
- package/dist/services/proposals/registry.js +9 -0
- package/dist/services/search-all-meetings.js +2 -2
- package/dist/services/search-all.js +4 -3
- package/dist/services/search-kind.js +3 -3
- package/dist/store/decisions.js +5 -1
- package/dist/store/index.d.ts +14 -3
- package/dist/store/index.js +3 -0
- package/dist/store/people.js +1 -1
- package/dist/store/source-review.d.ts +64 -0
- package/dist/store/source-review.js +7 -0
- package/dist/store/sqlite/accounts.d.ts +1 -1
- package/dist/store/sqlite/accounts.js +24 -1
- package/dist/store/sqlite/added-sources.d.ts +82 -0
- package/dist/store/sqlite/added-sources.js +237 -0
- package/dist/store/sqlite/business-contracts.d.ts +90 -0
- package/dist/store/sqlite/business-contracts.js +182 -0
- package/dist/store/sqlite/calendar-events.js +7 -3
- package/dist/store/sqlite/calendar-forget.js +3 -0
- package/dist/store/sqlite/calendar.d.ts +21 -1
- package/dist/store/sqlite/calendar.js +27 -7
- package/dist/store/sqlite/chat-rekey.js +7 -0
- package/dist/store/sqlite/chats.d.ts +3 -1
- package/dist/store/sqlite/chats.js +15 -1
- package/dist/store/sqlite/crm-rows.d.ts +44 -0
- package/dist/store/sqlite/crm-rows.js +195 -0
- package/dist/store/sqlite/crm.d.ts +2 -11
- package/dist/store/sqlite/crm.js +11 -166
- package/dist/store/sqlite/deals.d.ts +4 -8
- package/dist/store/sqlite/deals.js +5 -3
- package/dist/store/sqlite/document-transfer/snapshot.js +2 -2
- package/dist/store/sqlite/emails.js +2 -0
- package/dist/store/sqlite/entity-types.d.ts +14 -9
- package/dist/store/sqlite/entity-types.js +4 -10
- package/dist/store/sqlite/evidence-signature.d.ts +114 -0
- package/dist/store/sqlite/evidence-signature.js +349 -0
- package/dist/store/sqlite/evidence-sources.d.ts +2 -1
- package/dist/store/sqlite/evidence-sources.js +7 -3
- package/dist/store/sqlite/facts.d.ts +7 -2
- package/dist/store/sqlite/facts.js +21 -5
- package/dist/store/sqlite/finance.d.ts +18 -105
- package/dist/store/sqlite/finance.js +100 -219
- package/dist/store/sqlite/knowledge.d.ts +8 -2
- package/dist/store/sqlite/knowledge.js +65 -71
- package/dist/store/sqlite/labelled.d.ts +3 -0
- package/dist/store/sqlite/labelled.js +11 -0
- package/dist/store/sqlite/link-kinds.d.ts +2 -0
- package/dist/store/sqlite/link-kinds.js +2 -0
- package/dist/store/sqlite/manifest.js +2 -0
- package/dist/store/sqlite/memories.d.ts +23 -9
- package/dist/store/sqlite/memories.js +41 -13
- package/dist/store/sqlite/messages.js +2 -0
- package/dist/store/sqlite/migrations.generated.js +103 -0
- package/dist/store/sqlite/money-lines.d.ts +48 -0
- package/dist/store/sqlite/money-lines.js +76 -0
- package/dist/store/sqlite/money-transactions.d.ts +132 -0
- package/dist/store/sqlite/money-transactions.js +295 -0
- package/dist/store/sqlite/notes.d.ts +11 -2
- package/dist/store/sqlite/notes.js +20 -25
- package/dist/store/sqlite/orders.d.ts +36 -6
- package/dist/store/sqlite/orders.js +126 -46
- package/dist/store/sqlite/payment-facts.js +1 -1
- package/dist/store/sqlite/private-people.js +2 -0
- package/dist/store/sqlite/products.d.ts +78 -0
- package/dist/store/sqlite/products.js +131 -0
- package/dist/store/sqlite/project-transfer/records.js +26 -1
- package/dist/store/sqlite/project-transfer/snapshot.js +5 -3
- package/dist/store/sqlite/proposed-actions.d.ts +1 -1
- package/dist/store/sqlite/proposed-actions.js +24 -7
- package/dist/store/sqlite/providers.d.ts +0 -4
- package/dist/store/sqlite/providers.js +20 -2
- package/dist/store/sqlite/record-matching.d.ts +80 -0
- package/dist/store/sqlite/record-matching.js +264 -0
- package/dist/store/sqlite/record-projection.d.ts +89 -0
- package/dist/store/sqlite/record-projection.js +50 -0
- package/dist/store/sqlite/record-projectors-direct.d.ts +17 -0
- package/dist/store/sqlite/record-projectors-direct.js +125 -0
- package/dist/store/sqlite/record-projectors-money.d.ts +26 -0
- package/dist/store/sqlite/record-projectors-money.js +214 -0
- package/dist/store/sqlite/record-projectors-phone.d.ts +28 -0
- package/dist/store/sqlite/record-projectors-phone.js +212 -0
- package/dist/store/sqlite/record-projectors.d.ts +3 -44
- package/dist/store/sqlite/record-projectors.js +7 -67
- package/dist/store/sqlite/record-relations.d.ts +24 -0
- package/dist/store/sqlite/record-relations.js +92 -0
- package/dist/store/sqlite/record-remap.d.ts +12 -0
- package/dist/store/sqlite/record-remap.js +92 -0
- package/dist/store/sqlite/record-run.d.ts +71 -0
- package/dist/store/sqlite/record-run.js +179 -0
- package/dist/store/sqlite/record-settling.d.ts +9 -0
- package/dist/store/sqlite/record-settling.js +74 -0
- package/dist/store/sqlite/record-stamps.d.ts +13 -0
- package/dist/store/sqlite/record-stamps.js +30 -0
- package/dist/store/sqlite/record-writes.d.ts +43 -0
- package/dist/store/sqlite/record-writes.js +307 -0
- package/dist/store/sqlite/records.d.ts +52 -42
- package/dist/store/sqlite/records.js +123 -353
- package/dist/store/sqlite/ref-summaries.d.ts +6 -5
- package/dist/store/sqlite/ref-summaries.js +60 -8
- package/dist/store/sqlite/refunds.d.ts +31 -0
- package/dist/store/sqlite/refunds.js +112 -0
- package/dist/store/sqlite/reviewed.d.ts +109 -0
- package/dist/store/sqlite/reviewed.js +173 -0
- package/dist/store/sqlite/schema.d.ts +2840 -83
- package/dist/store/sqlite/schema.js +256 -1
- package/dist/store/sqlite/source-review-marks.d.ts +54 -0
- package/dist/store/sqlite/source-review-marks.js +143 -0
- package/dist/store/sqlite/source-review.d.ts +54 -0
- package/dist/store/sqlite/source-review.js +221 -0
- package/dist/store/sqlite/stage-amounts.d.ts +14 -0
- package/dist/store/sqlite/stage-amounts.js +23 -0
- package/dist/store/sqlite/subscriptions.d.ts +135 -0
- package/dist/store/sqlite/subscriptions.js +294 -0
- package/dist/store/sqlite/text-input.d.ts +3 -0
- package/dist/store/sqlite/text-input.js +13 -0
- package/dist/store/sqlite/things.js +2 -9
- package/dist/store/sqlite/transaction.d.ts +15 -0
- package/dist/store/sqlite/transaction.js +57 -0
- package/dist/store/sqlite/wallets.d.ts +88 -0
- package/dist/store/sqlite/wallets.js +143 -0
- package/dist/store/store-errors.d.ts +1 -0
- package/dist/store/store-errors.js +45 -0
- package/dist/store/store.d.ts +16 -1
- package/dist/store/store.js +30 -6
- package/package.json +5 -2
- package/schemas/examples/records-business.jsonl +11 -0
- package/schemas/records-envelope.v1.schema.json +1160 -5
|
@@ -0,0 +1,349 @@
|
|
|
1
|
+
import { CliError } from "@wirecat/cli-base";
|
|
2
|
+
import { ENTITY_TABLES, isEntityType } from "./entity-types.js";
|
|
3
|
+
import { asReviewTarget, holds, raiseSourceReview, scrubSourceReviews, sourceChecker, } from "./source-review-marks.js";
|
|
4
|
+
import { referenceOfThing, storedThing, thingOf } from "./things.js";
|
|
5
|
+
export const QUOTE_LIMIT = 500;
|
|
6
|
+
const GONE = { sig: "gone", rev: null, deleted: true, changedAt: null };
|
|
7
|
+
const part = (value) => (value == null ? "" : String(value));
|
|
8
|
+
const sigOf = (...values) => values.map(part).join("|");
|
|
9
|
+
const latest = (...times) => {
|
|
10
|
+
let last = null;
|
|
11
|
+
for (const time of times)
|
|
12
|
+
if (time != null && (last === null || Number(time) > last))
|
|
13
|
+
last = Number(time);
|
|
14
|
+
return last;
|
|
15
|
+
};
|
|
16
|
+
const folded = (text) => text
|
|
17
|
+
.normalize("NFC")
|
|
18
|
+
.replace(/\p{Cc}/gu, " ")
|
|
19
|
+
.replace(/\s+/gu, " ")
|
|
20
|
+
.trim();
|
|
21
|
+
/**
|
|
22
|
+
* The quote still stands in the text word for word, whatever the spacing and line breaks. Case and accents count:
|
|
23
|
+
* the search normalizer folds them, and a changed letter here may be the change that matters.
|
|
24
|
+
*/
|
|
25
|
+
export const quoteKept = (quote, text) => {
|
|
26
|
+
const wanted = folded(quote);
|
|
27
|
+
return wanted.length > 0 && folded(text).includes(wanted);
|
|
28
|
+
};
|
|
29
|
+
const textOf = (database, sql, ...values) => {
|
|
30
|
+
const row = database.prepare(sql).get(...values);
|
|
31
|
+
return row?.text == null ? undefined : String(row.text);
|
|
32
|
+
};
|
|
33
|
+
const holdsIn = (sql) => (database, id, quote) => quoteKept(quote, textOf(database, sql, id) ?? "");
|
|
34
|
+
const revised = (table, owner) => `(SELECT max(created_at) FROM ${table} WHERE ${owner} = t.id)`;
|
|
35
|
+
/** A note or a document: `created_at` is in the signature so a new row that takes a deleted one's id never matches. */
|
|
36
|
+
const versioned = (table, revisions, owner) => ({
|
|
37
|
+
signature: (database, id) => {
|
|
38
|
+
const hash = table === "documents" ? "t.content_hash" : "NULL";
|
|
39
|
+
const row = database
|
|
40
|
+
.prepare(`SELECT ${hash} AS content_hash, t.revision, t.deleted_at, t.created_at, ${revised(revisions, owner)} AS revised ` +
|
|
41
|
+
`FROM ${table} t WHERE t.id = ?`)
|
|
42
|
+
.get(id);
|
|
43
|
+
return (row && {
|
|
44
|
+
sig: sigOf(row.created_at, row.content_hash, row.revision, row.deleted_at),
|
|
45
|
+
rev: Number(row.revision),
|
|
46
|
+
deleted: row.deleted_at != null,
|
|
47
|
+
changedAt: latest(row.revised, row.deleted_at),
|
|
48
|
+
});
|
|
49
|
+
},
|
|
50
|
+
holds: holdsIn(`SELECT body AS text FROM ${table} WHERE id = ?`),
|
|
51
|
+
textAt: (database, id, rev) => textOf(database, `SELECT body AS text FROM ${table} WHERE id = ? AND revision = ?`, id, rev) ??
|
|
52
|
+
textOf(database, `SELECT body AS text FROM ${revisions} WHERE ${owner} = ? AND revision = ? ORDER BY id`, id, rev),
|
|
53
|
+
});
|
|
54
|
+
/** The revision counts too: a provider may resend changed text without an edit time, and the store still keeps one. */
|
|
55
|
+
const message = {
|
|
56
|
+
signature: (database, id) => {
|
|
57
|
+
const row = database
|
|
58
|
+
.prepare("SELECT t.edited_at, t.deleted_at, t.created_at, (SELECT max(id) FROM message_revisions WHERE message_id = t.id) " +
|
|
59
|
+
`AS rev, ${revised("message_revisions", "message_id")} AS revised FROM messages t WHERE t.id = ?`)
|
|
60
|
+
.get(id);
|
|
61
|
+
return (row && {
|
|
62
|
+
sig: sigOf(row.created_at, row.edited_at, row.deleted_at, row.rev),
|
|
63
|
+
rev: Number(row.rev ?? 0),
|
|
64
|
+
deleted: row.deleted_at != null,
|
|
65
|
+
changedAt: latest(row.revised, row.deleted_at),
|
|
66
|
+
});
|
|
67
|
+
},
|
|
68
|
+
holds: holdsIn("SELECT text FROM messages WHERE id = ?"),
|
|
69
|
+
textAt: (database, id, rev) => textOf(database, "SELECT text FROM message_revisions WHERE message_id = ? AND id > ? ORDER BY id LIMIT 1", id, rev) ?? textOf(database, "SELECT text FROM messages WHERE id = ?", id),
|
|
70
|
+
};
|
|
71
|
+
const transcriptSignature = (database, id) => {
|
|
72
|
+
const row = database
|
|
73
|
+
.prepare("SELECT content_hash, deleted_at, superseded_at, created_at FROM meeting_transcripts WHERE id = ?")
|
|
74
|
+
.get(id);
|
|
75
|
+
return (row && {
|
|
76
|
+
sig: sigOf(row.created_at, row.content_hash, row.superseded_at, row.deleted_at),
|
|
77
|
+
rev: null,
|
|
78
|
+
deleted: row.deleted_at != null,
|
|
79
|
+
changedAt: latest(row.deleted_at),
|
|
80
|
+
});
|
|
81
|
+
};
|
|
82
|
+
const TRANSCRIPT_PAGE = 200;
|
|
83
|
+
/** Reads a transcript a page at a time, keeping only as much of the text before as a quote can span. */
|
|
84
|
+
const transcriptHolds = (database, id, quote) => {
|
|
85
|
+
const wanted = folded(quote);
|
|
86
|
+
if (!wanted)
|
|
87
|
+
return false;
|
|
88
|
+
const page = database.prepare("SELECT position, text FROM meeting_transcript_rows WHERE meeting_transcript_id = ? AND position > ? " +
|
|
89
|
+
"ORDER BY position LIMIT ?");
|
|
90
|
+
let tail = "";
|
|
91
|
+
let after = -1;
|
|
92
|
+
for (;;) {
|
|
93
|
+
const rows = page.all(id, after, TRANSCRIPT_PAGE);
|
|
94
|
+
for (const row of rows) {
|
|
95
|
+
const text = `${tail} ${folded(String(row.text))}`.trim();
|
|
96
|
+
if (text.includes(wanted))
|
|
97
|
+
return true;
|
|
98
|
+
tail = text.slice(-wanted.length);
|
|
99
|
+
after = Number(row.position);
|
|
100
|
+
}
|
|
101
|
+
if (rows.length < TRANSCRIPT_PAGE)
|
|
102
|
+
return false;
|
|
103
|
+
}
|
|
104
|
+
};
|
|
105
|
+
const transcript = { signature: transcriptSignature, holds: transcriptHolds };
|
|
106
|
+
const transcriptRow = {
|
|
107
|
+
signature: (database, id) => {
|
|
108
|
+
const row = database
|
|
109
|
+
.prepare("SELECT meeting_transcript_id AS transcript, created_at FROM meeting_transcript_rows WHERE id = ?")
|
|
110
|
+
.get(id);
|
|
111
|
+
const whole = row && transcriptSignature(database, Number(row.transcript));
|
|
112
|
+
return whole && { ...whole, sig: sigOf(row.created_at, whole.sig) };
|
|
113
|
+
},
|
|
114
|
+
holds: holdsIn("SELECT text FROM meeting_transcript_rows WHERE id = ?"),
|
|
115
|
+
};
|
|
116
|
+
/** A calendar citation names the event; what changes is each entry of it, whose older versions are its revisions. */
|
|
117
|
+
const event = {
|
|
118
|
+
signature: (database, id) => {
|
|
119
|
+
const row = database
|
|
120
|
+
.prepare("SELECT e.created_at, e.source_deleted_at, e.deleted_at, max(r.id) AS rev, max(r.created_at) AS revised " +
|
|
121
|
+
"FROM events e LEFT JOIN calendar_entries c ON c.event_id = e.id " +
|
|
122
|
+
"LEFT JOIN calendar_entry_revisions r ON r.entry_id = c.id WHERE e.id = ? GROUP BY e.id")
|
|
123
|
+
.get(id);
|
|
124
|
+
return (row && {
|
|
125
|
+
sig: sigOf(row.created_at, row.rev, row.source_deleted_at, row.deleted_at),
|
|
126
|
+
rev: Number(row.rev ?? 0),
|
|
127
|
+
deleted: row.source_deleted_at != null || row.deleted_at != null,
|
|
128
|
+
changedAt: latest(row.revised, row.source_deleted_at, row.deleted_at),
|
|
129
|
+
});
|
|
130
|
+
},
|
|
131
|
+
holds: holdsIn("SELECT coalesce(title, '') || ' ' || coalesce(description, '') || ' ' || coalesce(location, '') AS text " +
|
|
132
|
+
"FROM events WHERE id = ?"),
|
|
133
|
+
};
|
|
134
|
+
const status = (table) => ({
|
|
135
|
+
signature: (database, id) => {
|
|
136
|
+
const row = database.prepare(`SELECT status, deleted_at, updated_at FROM ${table} WHERE id = ?`).get(id);
|
|
137
|
+
return (row && {
|
|
138
|
+
sig: sigOf(row.status, row.deleted_at),
|
|
139
|
+
rev: null,
|
|
140
|
+
deleted: row.deleted_at != null,
|
|
141
|
+
changedAt: latest(row.updated_at),
|
|
142
|
+
});
|
|
143
|
+
},
|
|
144
|
+
holds: holdsIn(`SELECT ${table === "facts" ? "statement" : "body"} AS text FROM ${table} WHERE id = ?`),
|
|
145
|
+
});
|
|
146
|
+
const columnsOf = new WeakMap();
|
|
147
|
+
const hasColumn = (database, table, column) => {
|
|
148
|
+
const known = columnsOf.get(database) ?? new Map();
|
|
149
|
+
columnsOf.set(database, known);
|
|
150
|
+
const key = `${table}.${column}`;
|
|
151
|
+
if (!known.has(key))
|
|
152
|
+
known.set(key, database.prepare("SELECT 1 FROM pragma_table_info(?) WHERE name = ?").get(table, column) !== undefined);
|
|
153
|
+
return known.get(key) === true;
|
|
154
|
+
};
|
|
155
|
+
const existence = (database, table, id) => {
|
|
156
|
+
const deleted = hasColumn(database, table, "deleted_at") ? "deleted_at" : "NULL AS deleted_at";
|
|
157
|
+
const created = hasColumn(database, table, "created_at") ? "created_at" : "NULL AS created_at";
|
|
158
|
+
const row = database.prepare(`SELECT ${deleted}, ${created} FROM ${table} WHERE id = ?`).get(id);
|
|
159
|
+
return (row && {
|
|
160
|
+
sig: sigOf(row.created_at, row.deleted_at),
|
|
161
|
+
rev: null,
|
|
162
|
+
deleted: row.deleted_at != null,
|
|
163
|
+
changedAt: latest(row.deleted_at),
|
|
164
|
+
});
|
|
165
|
+
};
|
|
166
|
+
const existing = (table) => ({ signature: (database, id) => existence(database, table, id) });
|
|
167
|
+
/** A thing an import made from a source's record changes when the record does. */
|
|
168
|
+
const recorded = (type, table) => ({
|
|
169
|
+
signature: (database, id) => {
|
|
170
|
+
const base = existence(database, table, id);
|
|
171
|
+
if (!base)
|
|
172
|
+
return undefined;
|
|
173
|
+
const records = database
|
|
174
|
+
.prepare("SELECT fingerprint, revision, deleted_at, updated_at FROM records WHERE entity_type = ? AND entity_id = ? ORDER BY id")
|
|
175
|
+
.all(type, id);
|
|
176
|
+
if (records.length === 0)
|
|
177
|
+
return base;
|
|
178
|
+
return {
|
|
179
|
+
sig: [base.sig, ...records.map((record) => sigOf(record.fingerprint, record.revision, record.deleted_at))].join(";"),
|
|
180
|
+
rev: records[0]?.revision == null ? null : Number(records[0].revision),
|
|
181
|
+
deleted: base.deleted || records.every((record) => record.deleted_at != null),
|
|
182
|
+
changedAt: latest(base.changedAt, ...records.map((record) => record.updated_at)),
|
|
183
|
+
};
|
|
184
|
+
},
|
|
185
|
+
});
|
|
186
|
+
/**
|
|
187
|
+
* Every kind of source a memory or fact may cite, by its `links.to_type`. An email, a meeting, a person and a project
|
|
188
|
+
* are checked for existence only; any other row an import made from a record follows that record.
|
|
189
|
+
*/
|
|
190
|
+
export const SOURCE_KINDS = {
|
|
191
|
+
document: versioned("documents", "document_revisions", "document_id"),
|
|
192
|
+
document_picture: versioned("documents", "document_revisions", "document_id"),
|
|
193
|
+
note: versioned("notes", "note_revisions", "note_id"),
|
|
194
|
+
message,
|
|
195
|
+
meeting_transcript: transcript,
|
|
196
|
+
meeting_transcript_row: transcriptRow,
|
|
197
|
+
event,
|
|
198
|
+
fact: status("facts"),
|
|
199
|
+
memory: status("memories"),
|
|
200
|
+
email: { ...existing("emails"), holds: holdsIn("SELECT body_text AS text FROM emails WHERE id = ?") },
|
|
201
|
+
meeting: existing("meetings"),
|
|
202
|
+
person: existing("persons"),
|
|
203
|
+
project: existing("projects"),
|
|
204
|
+
};
|
|
205
|
+
export const sourceKindOf = (type) => SOURCE_KINDS[type] ?? (isEntityType(type) ? recorded(type, ENTITY_TABLES[type]) : undefined);
|
|
206
|
+
/** The signature of a cited row as it is now; a row the store no longer holds is `deleted`. */
|
|
207
|
+
export const evidenceSignature = (database, thing) => sourceKindOf(thing.type)?.signature(database, thing.id) ?? GONE;
|
|
208
|
+
/** Whether a cited row's text holds the quote now; `undefined` where the store keeps no text for its kind. */
|
|
209
|
+
export const sourceHolds = (database, thing, quote) => {
|
|
210
|
+
const holds = sourceKindOf(thing.type)?.holds;
|
|
211
|
+
return holds === undefined ? undefined : holds(database, thing.id, quote);
|
|
212
|
+
};
|
|
213
|
+
/** The text of a cited row at version `rev`, from its history; `undefined` where there is none. */
|
|
214
|
+
export const sourceTextAt = (database, thing, rev) => sourceKindOf(thing.type)?.textAt?.(database, thing.id, rev);
|
|
215
|
+
/** The reference and quote of one cited source, refusing a quote too long or empty. */
|
|
216
|
+
export const evidenceInputOf = (input) => {
|
|
217
|
+
if (typeof input === "string")
|
|
218
|
+
return { ref: input };
|
|
219
|
+
if (input.quote === undefined)
|
|
220
|
+
return { ref: input.ref };
|
|
221
|
+
const quote = input.quote.trim();
|
|
222
|
+
if (!quote || quote.length > QUOTE_LIMIT)
|
|
223
|
+
throw new CliError("validation_error", `a quote from the evidence takes 1–${QUOTE_LIMIT} characters`, {
|
|
224
|
+
reason: "evidence.quote-length",
|
|
225
|
+
});
|
|
226
|
+
return { ref: input.ref, quote };
|
|
227
|
+
};
|
|
228
|
+
/** Refuses a quote the cited source's text does not hold: a claim may rest only on words the source said. */
|
|
229
|
+
export const assertQuoted = (database, thing, ref, quote) => {
|
|
230
|
+
if (quote === undefined || sourceHolds(database, thing, quote) !== false)
|
|
231
|
+
return;
|
|
232
|
+
throw new CliError("validation_error", `${ref} does not say "${quote.slice(0, 80)}"`, {
|
|
233
|
+
reason: "evidence.quote-not-found",
|
|
234
|
+
});
|
|
235
|
+
};
|
|
236
|
+
/** A new link's metadata: the version cited is the version signed. */
|
|
237
|
+
export const evidenceMetadata = (signature, at, quote) => ({
|
|
238
|
+
v: 1,
|
|
239
|
+
sig: signature.sig,
|
|
240
|
+
rev: signature.rev,
|
|
241
|
+
cited_rev: signature.rev,
|
|
242
|
+
...(quote === undefined ? {} : { quote }),
|
|
243
|
+
checked_at: at,
|
|
244
|
+
});
|
|
245
|
+
/** The stored signature of an `evidence` link, or `undefined` for one written before signatures. */
|
|
246
|
+
export const storedEvidenceMetadata = (metadata) => {
|
|
247
|
+
if (typeof metadata !== "string")
|
|
248
|
+
return undefined;
|
|
249
|
+
try {
|
|
250
|
+
const parsed = JSON.parse(metadata);
|
|
251
|
+
if (parsed?.v !== 1 || typeof parsed.sig !== "string")
|
|
252
|
+
return undefined;
|
|
253
|
+
return { ...parsed, cited_rev: parsed.cited_rev ?? parsed.rev ?? null };
|
|
254
|
+
}
|
|
255
|
+
catch {
|
|
256
|
+
return undefined;
|
|
257
|
+
}
|
|
258
|
+
};
|
|
259
|
+
/**
|
|
260
|
+
* Signs an `evidence` link again. It keeps the quote and the version cited, and the link's own `updated_at` stays, as
|
|
261
|
+
* nothing about the link changed. A link written before signatures takes the version it is signed at as cited.
|
|
262
|
+
*/
|
|
263
|
+
export const resignEvidence = (database, link, signature, at, previous) => {
|
|
264
|
+
const metadata = {
|
|
265
|
+
...evidenceMetadata(signature, at, previous?.quote),
|
|
266
|
+
cited_rev: previous === undefined ? signature.rev : previous.cited_rev,
|
|
267
|
+
};
|
|
268
|
+
database.prepare("UPDATE links SET metadata = ? WHERE id = ?").run(JSON.stringify(metadata), link);
|
|
269
|
+
};
|
|
270
|
+
/**
|
|
271
|
+
* Before cited rows go, or a message's text is erased: each evidence link to them drops its quote, which holds the
|
|
272
|
+
* source's words. `ids` is SQL naming the row ids. A row deleted for good also loses its pointer, so the link still
|
|
273
|
+
* says that a source was cited and is gone, and never names another row that takes the same id later; its old
|
|
274
|
+
* reference stays only as `target_text`, which nothing resolves since `target_folded` is empty. A tombstone
|
|
275
|
+
* (`keepPointer`) keeps it, signed as deleted. A memory or fact that still holds gets a `deleted` review in the same
|
|
276
|
+
* write, since the check will not meet a link with no pointer, nor one already signed as deleted; a link already
|
|
277
|
+
* signed as deleted was reported when the check met it.
|
|
278
|
+
*/
|
|
279
|
+
export const forgetEvidenceTo = (database, type, ids, at, { keepPointer = false } = {}) => {
|
|
280
|
+
const links = database
|
|
281
|
+
.prepare("SELECT id, from_type, from_id, to_type, to_id, metadata FROM links WHERE kind = 'evidence' AND to_type = ? " +
|
|
282
|
+
`AND to_id IN (${ids.sql})`)
|
|
283
|
+
.all(type, ...ids.params);
|
|
284
|
+
const detach = database.prepare("UPDATE links SET to_type = NULL, to_id = NULL, target_text = ?, target_folded = NULL, metadata = ? WHERE id = ?");
|
|
285
|
+
const forgotten = links.map((link) => {
|
|
286
|
+
const thing = storedThing(link.to_type, link.to_id);
|
|
287
|
+
const previous = storedEvidenceMetadata(link.metadata);
|
|
288
|
+
const ref = referenceOfThing(database, thing) ?? null;
|
|
289
|
+
const citedRev = previous?.cited_rev ?? null;
|
|
290
|
+
const signature = evidenceSignature(database, thing);
|
|
291
|
+
const reported = previous?.sig === signature.sig && signature.deleted;
|
|
292
|
+
if (keepPointer) {
|
|
293
|
+
const metadata = { ...evidenceMetadata(signature, at, undefined), cited_rev: previous ? citedRev : signature.rev };
|
|
294
|
+
database.prepare("UPDATE links SET metadata = ? WHERE id = ?").run(JSON.stringify(metadata), Number(link.id));
|
|
295
|
+
}
|
|
296
|
+
else {
|
|
297
|
+
const metadata = {
|
|
298
|
+
v: 1,
|
|
299
|
+
sig: GONE.sig,
|
|
300
|
+
rev: null,
|
|
301
|
+
cited_rev: citedRev,
|
|
302
|
+
checked_at: at,
|
|
303
|
+
gone: type,
|
|
304
|
+
};
|
|
305
|
+
detach.run(ref ?? `${type}:${thing.id}`, JSON.stringify(metadata), Number(link.id));
|
|
306
|
+
}
|
|
307
|
+
return { link: Number(link.id), from: storedThing(link.from_type, link.from_id), ref, citedRev, reported };
|
|
308
|
+
});
|
|
309
|
+
const member = database.prepare(`SELECT 1 WHERE ? IN (${ids.sql})`);
|
|
310
|
+
scrubSourceReviews(database, (ref) => {
|
|
311
|
+
const thing = thingOf(database, ref);
|
|
312
|
+
return thing?.type === type && member.get(thing.id, ...ids.params) !== undefined;
|
|
313
|
+
}, { refs: !keepPointer });
|
|
314
|
+
const byTarget = new Map();
|
|
315
|
+
for (const one of forgotten) {
|
|
316
|
+
const target = asReviewTarget(one.from.type, one.from.id);
|
|
317
|
+
if (one.reported || !target || !holds(database, target))
|
|
318
|
+
continue;
|
|
319
|
+
const entry = byTarget.get(`${target.type}:${target.id}`) ?? { target, sources: [] };
|
|
320
|
+
const named = keepPointer ? { ref: one.ref } : { ref: null, kind: type };
|
|
321
|
+
entry.sources.push({ link: one.link, ...named, cause: "deleted", citedRev: one.citedRev });
|
|
322
|
+
byTarget.set(`${target.type}:${target.id}`, entry);
|
|
323
|
+
}
|
|
324
|
+
if (byTarget.size) {
|
|
325
|
+
const proposer = sourceChecker(database, at);
|
|
326
|
+
for (const { target, sources } of byTarget.values())
|
|
327
|
+
raiseSourceReview(database, target, { sources, proposer, at });
|
|
328
|
+
}
|
|
329
|
+
return forgotten;
|
|
330
|
+
};
|
|
331
|
+
/** `forgetEvidenceTo` for one row. */
|
|
332
|
+
export const forgetEvidenceToRow = (database, thing, at, options) => forgetEvidenceTo(database, thing.type, { sql: "?", params: [thing.id] }, at, options);
|
|
333
|
+
/**
|
|
334
|
+
* After a purge, in its write: forgets evidence still pointing at a row of any citable type that is no longer there,
|
|
335
|
+
* so a kind the purge did not name is not missed. The purge names what it can beforehand, while references resolve.
|
|
336
|
+
*/
|
|
337
|
+
export const forgetDanglingEvidence = (database, at) => {
|
|
338
|
+
scrubSourceReviews(database, (ref) => thingOf(database, ref) === undefined, { refs: true });
|
|
339
|
+
return Object.entries(ENTITY_TABLES).flatMap(([type, table]) => forgetEvidenceTo(database, type, {
|
|
340
|
+
sql: "SELECT l.to_id FROM links l WHERE l.kind = 'evidence' AND l.to_type = ? AND l.to_id IS NOT NULL " +
|
|
341
|
+
`AND NOT EXISTS (SELECT 1 FROM ${table} t WHERE t.id = l.to_id)`,
|
|
342
|
+
params: [type],
|
|
343
|
+
}, at));
|
|
344
|
+
};
|
|
345
|
+
/** Deletes a document row after forgetting the evidence that cites it, so no link is left pointing at a reused id. */
|
|
346
|
+
export const deleteDocumentRow = (database, id, at) => {
|
|
347
|
+
forgetEvidenceToRow(database, { type: "document", id }, at);
|
|
348
|
+
database.prepare("DELETE FROM documents WHERE id = ?").run(id);
|
|
349
|
+
};
|
|
@@ -17,7 +17,8 @@ export interface EvidenceSource {
|
|
|
17
17
|
export declare const sourcesOfReferences: (database: CacheDatabase, references: readonly string[]) => EvidenceSource[];
|
|
18
18
|
/**
|
|
19
19
|
* Every `evidence` link from a memory or fact, in the order they were written, including one whose target was
|
|
20
|
-
* deleted since: `evidenceOf` drops those, so a reader would never
|
|
20
|
+
* deleted since, or deleted for good and so kept with no pointer: `evidenceOf` drops those, so a reader would never
|
|
21
|
+
* learn the record lost a source.
|
|
21
22
|
*/
|
|
22
23
|
export declare const evidenceSourcesOf: (database: CacheDatabase, from: {
|
|
23
24
|
type: string;
|
|
@@ -17,14 +17,18 @@ export const sourcesOfReferences = (database, references) => [...refReadsOf(data
|
|
|
17
17
|
const GONE_KIND = { meeting_transcript: "meeting", meeting_transcript_row: "meeting" };
|
|
18
18
|
/**
|
|
19
19
|
* Every `evidence` link from a memory or fact, in the order they were written, including one whose target was
|
|
20
|
-
* deleted since: `evidenceOf` drops those, so a reader would never
|
|
20
|
+
* deleted since, or deleted for good and so kept with no pointer: `evidenceOf` drops those, so a reader would never
|
|
21
|
+
* learn the record lost a source.
|
|
21
22
|
*/
|
|
22
23
|
export const evidenceSourcesOf = (database, from) => {
|
|
23
24
|
const cited = database
|
|
24
|
-
.prepare(`SELECT to_type, to_id
|
|
25
|
-
|
|
25
|
+
.prepare(`SELECT to_type, to_id, CASE WHEN json_valid(metadata) THEN json_extract(metadata, '$.gone') END AS gone ` +
|
|
26
|
+
`FROM links WHERE from_type = ? AND from_id = ? AND kind = ${linkKind("evidence")} ` +
|
|
27
|
+
"AND (to_id IS NOT NULL OR gone IS NOT NULL) ORDER BY id")
|
|
26
28
|
.all(from.type, from.id)
|
|
27
29
|
.map((link) => {
|
|
30
|
+
if (link.to_id == null)
|
|
31
|
+
return { type: String(link.gone), ref: undefined };
|
|
28
32
|
const thing = storedThing(link.to_type, link.to_id);
|
|
29
33
|
return { type: thing.type, ref: referenceOfThing(database, thing) };
|
|
30
34
|
});
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import { type FACT_STATUSES, FACT_TYPES, MEMORY_SCOPES } from "../../domain/knowledge.js";
|
|
2
2
|
import { type Decider } from "../decisions.js";
|
|
3
|
+
import type { SourceReviewMarker } from "../source-review.js";
|
|
3
4
|
import { type Actor, type ProposedByFilter } from "./actors.js";
|
|
5
|
+
import type { EvidenceInput } from "./evidence-signature.js";
|
|
4
6
|
import { type EvidenceSource } from "./evidence-sources.js";
|
|
5
7
|
import { type Author } from "./memories.js";
|
|
6
8
|
import type { StoreContext } from "./open.js";
|
|
@@ -34,6 +36,8 @@ export interface Fact {
|
|
|
34
36
|
scope: (typeof MEMORY_SCOPES)[number] | null;
|
|
35
37
|
source: string;
|
|
36
38
|
evidence: string[];
|
|
39
|
+
/** The open review of its sources (`memory_source_review`), when one waits for the owner. */
|
|
40
|
+
sourceReview: SourceReviewMarker | null;
|
|
37
41
|
createdAt: string;
|
|
38
42
|
updatedAt: string;
|
|
39
43
|
}
|
|
@@ -46,7 +50,7 @@ export interface FactInput {
|
|
|
46
50
|
subject?: string;
|
|
47
51
|
/** When it happened, as the evidence shows (ISO). */
|
|
48
52
|
occurredAt?: string;
|
|
49
|
-
evidence?:
|
|
53
|
+
evidence?: EvidenceInput[];
|
|
50
54
|
scope?: Fact["scope"] & string;
|
|
51
55
|
/** The proposed or accepted fact this one corrects, `superseded` once this one is accepted. */
|
|
52
56
|
supersedes?: string;
|
|
@@ -79,7 +83,8 @@ export interface FactsStore {
|
|
|
79
83
|
reject(reference: string, by: Decider, reason?: string): Promise<Fact>;
|
|
80
84
|
/**
|
|
81
85
|
* Erases the fact: its row, its links, its tags, and the memory it was migrated from, which still held its text.
|
|
82
|
-
*
|
|
86
|
+
* Evidence of memories and facts that cited it stays with no pointer and shows as a missing source; `links` counts
|
|
87
|
+
* the links erased. A decision that replaced it no longer names it. A backup made before keeps it.
|
|
83
88
|
*/
|
|
84
89
|
remove(reference: string, by: Decider): Promise<{
|
|
85
90
|
ref: string;
|
|
@@ -7,6 +7,7 @@ import { evidenceSourcesOf } from "./evidence-sources.js";
|
|
|
7
7
|
import { linkKind } from "./link-kinds.js";
|
|
8
8
|
import { authorActor, authorOf, dropLinksOf, erase, evidenceOf, keepHighestId, linkEvidence, nextId, projectOf, subjectOf, } from "./memories.js";
|
|
9
9
|
import { requiredThing } from "./notes.js";
|
|
10
|
+
import { closeSourceReviews, eraseSourceReviews, sourceReviewOf } from "./source-review-marks.js";
|
|
10
11
|
import { write } from "./transaction.js";
|
|
11
12
|
const NAMED = "SELECT f.*, b.name AS author_name, b.kind AS author_kind FROM facts f LEFT JOIN bots b ON f.author_type = 'bot' AND b.id = f.author_id";
|
|
12
13
|
const iso = (value) => new Date(Number(value)).toISOString();
|
|
@@ -35,6 +36,7 @@ export const factOperationsOver = (context) => {
|
|
|
35
36
|
scope: row.scope == null ? null : row.scope,
|
|
36
37
|
source: String(row.source),
|
|
37
38
|
evidence: evidenceOf(context, { type: "fact", id: Number(row.id) }),
|
|
39
|
+
sourceReview: sourceReviewOf(database, { type: "fact", id: Number(row.id) }),
|
|
38
40
|
createdAt: iso(row.created_at),
|
|
39
41
|
updatedAt: iso(row.updated_at),
|
|
40
42
|
});
|
|
@@ -49,10 +51,16 @@ export const factOperationsOver = (context) => {
|
|
|
49
51
|
proposedBy: proposerOf(database, row.author_type, row.author_id),
|
|
50
52
|
proposed: row.status === "proposed",
|
|
51
53
|
}, by);
|
|
52
|
-
const decided = (row, by) => assertMayDecide({
|
|
54
|
+
const decided = (row, by, ownerOnly = false) => assertMayDecide({
|
|
53
55
|
id: `fact:${row.id}`,
|
|
54
56
|
proposedBy: proposerOf(database, row.author_type, row.author_id),
|
|
57
|
+
ownerOnly,
|
|
55
58
|
}, by, context.decisions);
|
|
59
|
+
/** Accepting it would retire a fact the owner stands behind, which only the owner may, as for memories. */
|
|
60
|
+
const retiresAccepted = (row) => row.status === "proposed" &&
|
|
61
|
+
row.supersedes_id != null &&
|
|
62
|
+
database.prepare("SELECT 1 FROM facts WHERE id = ? AND status = 'accepted'").get(Number(row.supersedes_id)) !==
|
|
63
|
+
undefined;
|
|
56
64
|
/** Keeps who decided for the owner and why in `metadata.decision`; the decider's actor is in `confirmed_by`. */
|
|
57
65
|
const keepDecision = (id, by, reason) => {
|
|
58
66
|
const why = [by === "owner" ? undefined : by.reason.trim(), reason?.trim() || undefined].filter(Boolean).join(": ");
|
|
@@ -73,11 +81,14 @@ export const factOperationsOver = (context) => {
|
|
|
73
81
|
.prepare("UPDATE facts SET status = 'accepted', confirmed_by_type = ?, confirmed_by_id = ?, updated_at = ? WHERE id = ?")
|
|
74
82
|
.run(actor.type, actor.id, at, id);
|
|
75
83
|
keepDecision(id, by, reason);
|
|
76
|
-
if (row.supersedes_id != null)
|
|
84
|
+
if (row.supersedes_id != null) {
|
|
77
85
|
database
|
|
78
86
|
.prepare("UPDATE facts SET status = 'superseded', updated_at = ? WHERE id = ? AND status IN ('proposed', 'accepted')")
|
|
79
87
|
.run(at, Number(row.supersedes_id));
|
|
88
|
+
closeSourceReviews(database, { type: "fact", id: Number(row.supersedes_id) }, { actor, at, why: "superseded" });
|
|
89
|
+
}
|
|
80
90
|
};
|
|
91
|
+
const closing = (id, by, why) => closeSourceReviews(database, { type: "fact", id }, { actor: authorActor(context, deciderAuthor(by)).actor, at: now(), why });
|
|
81
92
|
return {
|
|
82
93
|
add: (input) => {
|
|
83
94
|
if (input.type === "payment")
|
|
@@ -135,7 +146,7 @@ export const factOperationsOver = (context) => {
|
|
|
135
146
|
},
|
|
136
147
|
accept: (reference, by, reason) => {
|
|
137
148
|
const row = rowOf(reference);
|
|
138
|
-
decided(row, by);
|
|
149
|
+
decided(row, by, retiresAccepted(row));
|
|
139
150
|
write(database, () => accept(Number(row.id), by, reason));
|
|
140
151
|
return factOf(rowOf(reference));
|
|
141
152
|
},
|
|
@@ -151,7 +162,10 @@ export const factOperationsOver = (context) => {
|
|
|
151
162
|
throw new CliError("validation_error", "only a decision can be reversed");
|
|
152
163
|
if (row.status !== "accepted")
|
|
153
164
|
throw new CliError("validation_error", "only an accepted decision can be reversed");
|
|
154
|
-
database
|
|
165
|
+
write(database, () => {
|
|
166
|
+
database.prepare("UPDATE facts SET status = 'reversed', updated_at = ? WHERE id = ?").run(now(), Number(row.id));
|
|
167
|
+
closing(Number(row.id), by, "reversed");
|
|
168
|
+
});
|
|
155
169
|
return factOf(rowOf(reference));
|
|
156
170
|
},
|
|
157
171
|
revoke: (reference, by, reason) => {
|
|
@@ -193,6 +207,7 @@ export const factOperationsOver = (context) => {
|
|
|
193
207
|
write(database, () => {
|
|
194
208
|
database.prepare("UPDATE facts SET status = 'rejected', updated_at = ? WHERE id = ?").run(now(), Number(row.id));
|
|
195
209
|
keepDecision(Number(row.id), by, reason);
|
|
210
|
+
closing(Number(row.id), by, "rejected");
|
|
196
211
|
});
|
|
197
212
|
return factOf(rowOf(reference));
|
|
198
213
|
},
|
|
@@ -206,7 +221,8 @@ export const factOperationsOver = (context) => {
|
|
|
206
221
|
if (twin !== undefined)
|
|
207
222
|
keepHighestId(database, "memories", now());
|
|
208
223
|
database.prepare("UPDATE facts SET supersedes_id = NULL WHERE supersedes_id = ?").run(id);
|
|
209
|
-
|
|
224
|
+
eraseSourceReviews(database, { type: "fact", id });
|
|
225
|
+
const count = dropLinksOf(database, { type: "fact", id }, now());
|
|
210
226
|
database.prepare("DELETE FROM links WHERE from_type = 'fact' AND from_id = ?").run(id);
|
|
211
227
|
database.prepare("DELETE FROM taggings WHERE taggable_type = 'fact' AND taggable_id = ?").run(id);
|
|
212
228
|
database.prepare("DELETE FROM facts WHERE id = ?").run(id);
|