@niwa-labs/memory 0.0.0-stage → 0.0.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/LICENSE +21 -0
- package/README.md +107 -2
- package/package.json +33 -4
- package/src/cockpit/actions.ts +95 -0
- package/src/cockpit/index.tsx +23 -0
- package/src/cockpit/notes.ts +78 -0
- package/src/cockpit/page.tsx +191 -0
- package/src/consolidate.ts +113 -0
- package/src/context.ts +41 -0
- package/src/fold.ts +308 -0
- package/src/holds.ts +13 -0
- package/src/index.ts +775 -0
- package/src/records.ts +538 -0
- package/src/same.ts +15 -0
- package/src/search.ts +48 -0
- package/src/share-question.ts +27 -0
- package/src/top.ts +44 -0
- package/test/cockpit-actions.test.ts +53 -0
- package/test/cockpit-notes.test.ts +89 -0
- package/test/consolidate.test.ts +171 -0
- package/test/context.test.ts +241 -0
- package/test/corroboration.test.ts +61 -0
- package/test/delegate-context.test.ts +16 -0
- package/test/fixtures/host.ts +229 -0
- package/test/intake.test.ts +69 -0
- package/test/notes.test.ts +124 -0
- package/test/projects.test.ts +37 -0
- package/test/recovery.test.ts +174 -0
- package/test/search.test.ts +88 -0
- package/test/top.test.ts +84 -0
- package/tsconfig.json +15 -0
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
// What a consolidation does to the notes of one memory. Notes that came to say the same are merged; the
|
|
2
|
+
// notes the owner confirmed or that have enough sources are lifted into the top layer while its part has
|
|
3
|
+
// room, the strongest first, and a weaker one lifted before gives way; unconfirmed notes of one source
|
|
4
|
+
// that went stale are retired, and so are the oldest unplaced notes past the limit. A note put in or out
|
|
5
|
+
// of the top layer by hand is never moved, merged away or retired; a confirmed note is never retired.
|
|
6
|
+
|
|
7
|
+
import { holds } from "./holds.ts";
|
|
8
|
+
import type { NoteView } from "./records.ts";
|
|
9
|
+
import { same } from "./same.ts";
|
|
10
|
+
|
|
11
|
+
/** The limits a consolidation keeps. */
|
|
12
|
+
export interface ConsolidationLimits {
|
|
13
|
+
/** How many sources lift an unconfirmed note into the top layer. */
|
|
14
|
+
readonly liftAfter: number;
|
|
15
|
+
/** How many notes each part of the top layer holds: about the owner, each project, in general. */
|
|
16
|
+
readonly topNotes: number;
|
|
17
|
+
/** How many notes hold in one memory. */
|
|
18
|
+
readonly notes: number;
|
|
19
|
+
/** How long an unconfirmed note of one source holds without a change, in milliseconds. */
|
|
20
|
+
readonly staleAfter: number;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** What a consolidation does. */
|
|
24
|
+
export interface Consolidation {
|
|
25
|
+
readonly merged: Array<{ note: string; into: string }>;
|
|
26
|
+
readonly lifted: string[];
|
|
27
|
+
readonly dropped: string[];
|
|
28
|
+
readonly retired: string[];
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** The part of the top layer a note belongs to. */
|
|
32
|
+
function partOf(note: NoteView): string {
|
|
33
|
+
return note.about === "owner" ? "owner" : `work ${note.project ?? ""}`;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** The stronger first: confirmed, then with more sources, then the one changed or written later. */
|
|
37
|
+
function stronger(a: NoteView, b: NoteView): number {
|
|
38
|
+
return Number(b.confirmed) - Number(a.confirmed) || b.sources - a.sources || b.changed - a.changed || b.written - a.written;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* What to do with the notes of one memory, in the order they were written, at the moment `now`.
|
|
43
|
+
* `sourcesOf` is how many sources a note has once the notes merged into it are counted.
|
|
44
|
+
*/
|
|
45
|
+
export function consolidate(
|
|
46
|
+
notes: readonly NoteView[],
|
|
47
|
+
limits: ConsolidationLimits,
|
|
48
|
+
now: number,
|
|
49
|
+
sourcesOf: (note: NoteView, merged: readonly NoteView[]) => number,
|
|
50
|
+
): Consolidation {
|
|
51
|
+
const live = notes.filter(holds);
|
|
52
|
+
const merged: Array<{ note: string; into: string }> = [];
|
|
53
|
+
const gone = new Set<string>();
|
|
54
|
+
const into = new Map<string, NoteView[]>();
|
|
55
|
+
for (const [at, one] of live.entries()) {
|
|
56
|
+
if (gone.has(one.note)) continue;
|
|
57
|
+
for (const other of live.slice(at + 1)) {
|
|
58
|
+
if (gone.has(other.note) || gone.has(one.note) || one.about !== other.about || one.project !== other.project || !same(one.text, other.text)) continue;
|
|
59
|
+
// The older is kept, unless only the newer has the owner behind it; a note placed by hand is never merged away.
|
|
60
|
+
const keepNewer = (other.confirmed && !one.confirmed) || (other.pinned && !one.pinned);
|
|
61
|
+
const [kept, away] = keepNewer ? [other, one] : [one, other];
|
|
62
|
+
if (away.pinned) continue;
|
|
63
|
+
merged.push({ note: away.note, into: kept.note });
|
|
64
|
+
gone.add(away.note);
|
|
65
|
+
into.set(kept.note, [...(into.get(kept.note) ?? []), away]);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
const held = live
|
|
69
|
+
.filter((note) => !gone.has(note.note))
|
|
70
|
+
.map((note) => ({ ...note, sources: sourcesOf(note, into.get(note.note) ?? []), confirmed: note.confirmed || (into.get(note.note) ?? []).some((one) => one.confirmed) }));
|
|
71
|
+
|
|
72
|
+
// Each part of the top layer: what is placed by hand stays, and the rest of the room goes to the strongest.
|
|
73
|
+
const lifted: string[] = [];
|
|
74
|
+
const dropped: string[] = [];
|
|
75
|
+
const top = new Set(held.filter((note) => note.top).map((note) => note.note));
|
|
76
|
+
const parts = new Map<string, typeof held>();
|
|
77
|
+
for (const note of held) parts.set(partOf(note), [...(parts.get(partOf(note)) ?? []), note]);
|
|
78
|
+
for (const part of parts.values()) {
|
|
79
|
+
const placed = part.filter((note) => note.pinned && note.top);
|
|
80
|
+
const earned = part.filter((note) => !note.pinned && (note.top || note.confirmed || note.sources >= limits.liftAfter)).sort(stronger);
|
|
81
|
+
const room = Math.max(0, limits.topNotes - placed.length);
|
|
82
|
+
earned.forEach((note, rank) => {
|
|
83
|
+
if (rank < room && !note.top) {
|
|
84
|
+
lifted.push(note.note);
|
|
85
|
+
top.add(note.note);
|
|
86
|
+
}
|
|
87
|
+
if (rank >= room && note.top) {
|
|
88
|
+
dropped.push(note.note);
|
|
89
|
+
top.delete(note.note);
|
|
90
|
+
}
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
// Retire what went stale, then the oldest of what is left unplaced past the limit.
|
|
95
|
+
const retired: string[] = [];
|
|
96
|
+
const movable = (note: (typeof held)[number]) => !note.pinned && !note.confirmed && !top.has(note.note);
|
|
97
|
+
for (const note of held) {
|
|
98
|
+
if (movable(note) && note.sources <= 1 && now - note.changed > limits.staleAfter) retired.push(note.note);
|
|
99
|
+
}
|
|
100
|
+
let holding = held.length - retired.length;
|
|
101
|
+
for (const note of held) {
|
|
102
|
+
if (holding <= limits.notes) break;
|
|
103
|
+
if (!movable(note) || retired.includes(note.note)) continue;
|
|
104
|
+
retired.push(note.note);
|
|
105
|
+
holding -= 1;
|
|
106
|
+
}
|
|
107
|
+
return { merged, lifted: lifted.sort((a, b) => order(held, a) - order(held, b)), dropped, retired };
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/** Where a note stands among the notes, by when it was written. */
|
|
111
|
+
function order(notes: readonly NoteView[], note: string): number {
|
|
112
|
+
return notes.findIndex((one) => one.note === note);
|
|
113
|
+
}
|
package/src/context.ts
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
// What a memory tells an addressee of a context request. Its own agent and the owner are told a note
|
|
2
|
+
// whole. Another address is told only the conclusion and where it holds: a project's note goes to the
|
|
3
|
+
// agents of that project, a note about the owner only once the owner said yes for that address.
|
|
4
|
+
|
|
5
|
+
import type { ShareState } from "./fold.ts";
|
|
6
|
+
import type { ContextItem, NoteView } from "./records.ts";
|
|
7
|
+
|
|
8
|
+
/** Who reads: the memory's own agent or the owner, or another address and the project it works in. */
|
|
9
|
+
export type Reader = { readonly kind: "home" } | { readonly kind: "other"; readonly project?: string };
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Where a note stands for a reader: `told` (it may be said), `private` (it is about the owner and he has not said
|
|
13
|
+
* yes), or `elsewhere` (it holds for a project the reader is not in, and is not so much as named to it).
|
|
14
|
+
*/
|
|
15
|
+
export type Standing = "told" | "private" | "elsewhere";
|
|
16
|
+
|
|
17
|
+
/** Whether a project lies in the project an agent works in: the same directory, or one under it. */
|
|
18
|
+
export function within(project: string, home: string): boolean {
|
|
19
|
+
const root = home.endsWith("/") ? home : `${home}/`;
|
|
20
|
+
return project === home || project.startsWith(root);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** Where a note stands for a reader; `share` is the owner's word on it so far. */
|
|
24
|
+
export function standing(note: NoteView, reader: Reader, share: ShareState | undefined): Standing {
|
|
25
|
+
if (reader.kind === "home") return "told";
|
|
26
|
+
if (note.project !== undefined && (reader.project === undefined || !within(note.project, reader.project))) return "elsewhere";
|
|
27
|
+
return note.about === "owner" && share !== "allowed" ? "private" : "told";
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** What a context answer tells of a note: the conclusion and where it holds; what backs it only to its own readers. */
|
|
31
|
+
export function conclusion(note: NoteView, reader: Reader): ContextItem {
|
|
32
|
+
return {
|
|
33
|
+
note: note.note,
|
|
34
|
+
text: note.text,
|
|
35
|
+
about: note.about,
|
|
36
|
+
...(note.project === undefined ? {} : { project: note.project }),
|
|
37
|
+
...(reader.kind === "home" && note.detail !== undefined ? { detail: note.detail } : {}),
|
|
38
|
+
confirmed: note.confirmed,
|
|
39
|
+
source: { owner: { kind: "agent", id: note.memory }, note: note.note, seq: note.seq },
|
|
40
|
+
};
|
|
41
|
+
}
|
package/src/fold.ts
ADDED
|
@@ -0,0 +1,308 @@
|
|
|
1
|
+
// The memories as the journal holds them: the records of the module folded, in order, into the notes of
|
|
2
|
+
// every memory. A fold can stop at any position, which is how a memory is read as it stood then.
|
|
3
|
+
|
|
4
|
+
import { stableJson } from "@niwa-labs/core";
|
|
5
|
+
import type { Address, StoredRecord } from "@niwa-labs/core";
|
|
6
|
+
import type { TurnAuthor } from "@niwa-labs/agent/records";
|
|
7
|
+
|
|
8
|
+
import { ConsolidationStarted, Consolidated, DigestTaken, NoteConfirmed, NoteCorroborated, NoteCorrected, NoteDropped, NoteErased, NoteLifted, NoteRetracted, NoteWritten, PageWritten, ShareAnswered, ShareWanted } from "./records.ts";
|
|
9
|
+
import type { MemoryName, NoteId, NoteSource, NoteView, PageName, PageView } from "./records.ts";
|
|
10
|
+
|
|
11
|
+
/** A record type with its schema: what the fold parses a stored record with. */
|
|
12
|
+
interface Typed<T> {
|
|
13
|
+
readonly name: string;
|
|
14
|
+
readonly schema: { safeParse(value: unknown): { success: true; data: T } | { success: false } };
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/** The data of a stored record of a type, or undefined when it is of another type or does not parse. */
|
|
18
|
+
export function dataAs<T>(stored: StoredRecord, type: Typed<T>): T | undefined {
|
|
19
|
+
if (stored.type !== type.name) return undefined;
|
|
20
|
+
const parsed = type.schema.safeParse(stored.data);
|
|
21
|
+
return parsed.success ? parsed.data : undefined;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** A digest taken into a memory: where it is in the journal, and what it tells of. */
|
|
25
|
+
export interface TakenDigest {
|
|
26
|
+
readonly turn: string;
|
|
27
|
+
readonly record: string;
|
|
28
|
+
readonly session: string;
|
|
29
|
+
readonly project?: string;
|
|
30
|
+
/** The position where it was taken. */
|
|
31
|
+
readonly seq: number;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* What a source counts as: a session or a record is one source whoever read it; a conclusion is one
|
|
36
|
+
* source for each writer who drew it; the owner's word is one.
|
|
37
|
+
*/
|
|
38
|
+
export function sourceKey(source: NoteSource, by: TurnAuthor): string {
|
|
39
|
+
switch (source.kind) {
|
|
40
|
+
case "owner":
|
|
41
|
+
return "owner";
|
|
42
|
+
case "inferred":
|
|
43
|
+
return stableJson(["inferred", by]);
|
|
44
|
+
case "session":
|
|
45
|
+
return stableJson(["session", source.session]);
|
|
46
|
+
case "record":
|
|
47
|
+
return stableJson(["record", source.record]);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** Where the owner's word on a note leaving for an address stands: asked, or answered. */
|
|
52
|
+
export type ShareState = "asked" | "allowed" | "refused";
|
|
53
|
+
|
|
54
|
+
/** A purpose as it is compared: its words, one space apart. */
|
|
55
|
+
function purposeText(purpose: string): string {
|
|
56
|
+
return purpose.trim().replace(/\s+/g, " ");
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** The key of the owner's word on one revision of one note for one address and one purpose, or for any. */
|
|
60
|
+
function shareKey(memory: MemoryName, note: NoteId, revision: number, to: Address, purpose: string | undefined): string {
|
|
61
|
+
return stableJson([memory, note, revision, to, purpose === undefined ? null : purposeText(purpose)]);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/** The notes of every memory, as far as the records folded so far say. */
|
|
65
|
+
export class Memories {
|
|
66
|
+
readonly #notes = new Map<MemoryName, Map<NoteId, NoteView>>();
|
|
67
|
+
readonly #pages = new Map<MemoryName, Map<PageName, PageView>>();
|
|
68
|
+
readonly #digests = new Map<MemoryName, TakenDigest[]>();
|
|
69
|
+
readonly #shares = new Map<string, { state: ShareState; at: number }>();
|
|
70
|
+
/** The sources each note counts, by memory and note. */
|
|
71
|
+
readonly #sources = new Map<string, Set<string>>();
|
|
72
|
+
/** How many records changed each memory since it was last consolidated. */
|
|
73
|
+
readonly #changes = new Map<MemoryName, number>();
|
|
74
|
+
/** The position of the last record folded. */
|
|
75
|
+
seq = 0;
|
|
76
|
+
|
|
77
|
+
/** The record types a fold reads. */
|
|
78
|
+
static readonly TYPES: readonly string[] = [NoteWritten, NoteCorroborated, NoteConfirmed, NoteCorrected, NoteRetracted, NoteErased, NoteLifted, NoteDropped, PageWritten, DigestTaken, ShareWanted, ShareAnswered, Consolidated].map((type) => type.name);
|
|
79
|
+
|
|
80
|
+
/** The notes of one memory, in the order they were written. */
|
|
81
|
+
notesOf(memory: MemoryName): NoteView[] {
|
|
82
|
+
return [...(this.#notes.get(memory)?.values() ?? [])];
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/** How many notes hold in all memories. */
|
|
86
|
+
count(): number {
|
|
87
|
+
let held = 0;
|
|
88
|
+
for (const notes of this.#notes.values()) for (const note of notes.values()) if (note.status === "live") held += 1;
|
|
89
|
+
return held;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
note(memory: MemoryName, note: NoteId): NoteView | undefined {
|
|
93
|
+
return this.#notes.get(memory)?.get(note);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/** The pages of one memory, by name. */
|
|
97
|
+
pagesOf(memory: MemoryName): PageView[] {
|
|
98
|
+
return [...(this.#pages.get(memory)?.values() ?? [])];
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
page(memory: MemoryName, page: PageName): PageView | undefined {
|
|
102
|
+
return this.#pages.get(memory)?.get(page);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/** The digests taken into one memory, oldest first. */
|
|
106
|
+
digestsOf(memory: MemoryName): readonly TakenDigest[] {
|
|
107
|
+
return this.#digests.get(memory) ?? [];
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Where the owner's word on a note leaving for an address stands, for one revision of the note and one purpose.
|
|
112
|
+
* His latest answer, for the purpose or for every purpose, stands before a question that still waits.
|
|
113
|
+
*/
|
|
114
|
+
share(memory: MemoryName, note: NoteId, revision: number, to: Address, purpose: string): ShareState | undefined {
|
|
115
|
+
const named = this.#shares.get(shareKey(memory, note, revision, to, purpose));
|
|
116
|
+
const every = this.#shares.get(shareKey(memory, note, revision, to, undefined));
|
|
117
|
+
const answered = [named, every].filter((one) => one !== undefined && one.state !== "asked").sort((a, b) => (b?.at ?? 0) - (a?.at ?? 0));
|
|
118
|
+
return answered[0]?.state ?? named?.state ?? every?.state;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/** Erase here what `later` erased: an erased note is not read back at any position. */
|
|
122
|
+
eraseAsIn(later: Memories): void {
|
|
123
|
+
for (const [memory, notes] of this.#notes) {
|
|
124
|
+
for (const [id, note] of notes) {
|
|
125
|
+
if (later.note(memory, id)?.status !== "erased") continue;
|
|
126
|
+
const { detail: _dropped, ...rest } = note;
|
|
127
|
+
notes.set(id, { ...rest, text: "", status: "erased", top: false });
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/** How many records changed a memory since it was last consolidated. */
|
|
133
|
+
changesOf(memory: MemoryName): number {
|
|
134
|
+
return this.#changes.get(memory) ?? 0;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/** How many sources a note has with the sources of the notes merged into it. */
|
|
138
|
+
sourcesWith(memory: MemoryName, note: NoteId, merged: readonly NoteId[]): number {
|
|
139
|
+
const all = new Set(this.#sources.get(`${memory} ${note}`));
|
|
140
|
+
for (const one of merged) for (const key of this.#sources.get(`${memory} ${one}`) ?? []) all.add(key);
|
|
141
|
+
return Math.max(all.size, 1);
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/** Whether a note already counts this source. */
|
|
145
|
+
counts(memory: MemoryName, note: NoteId, key: string): boolean {
|
|
146
|
+
return this.#sources.get(`${memory} ${note}`)?.has(key) ?? false;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/** Fold one more record. Records come in the order of the journal. */
|
|
150
|
+
apply(stored: StoredRecord): void {
|
|
151
|
+
this.seq = Math.max(this.seq, Number(stored.seq));
|
|
152
|
+
const at = Number(stored.seq);
|
|
153
|
+
this.#when = Number(stored.at);
|
|
154
|
+
const consolidated = dataAs(stored, Consolidated);
|
|
155
|
+
if (consolidated !== undefined) {
|
|
156
|
+
this.#consolidated(consolidated, at);
|
|
157
|
+
return;
|
|
158
|
+
}
|
|
159
|
+
if (stored.type === ConsolidationStarted.name) return;
|
|
160
|
+
const memory = (stored.data as { memory?: unknown } | null)?.memory;
|
|
161
|
+
if (typeof memory === "string" && !stored.type.startsWith("memory.share.")) this.#changes.set(memory as MemoryName, this.changesOf(memory as MemoryName) + 1);
|
|
162
|
+
const written = dataAs(stored, NoteWritten);
|
|
163
|
+
if (written !== undefined) {
|
|
164
|
+
const notes = this.#notes.get(written.memory) ?? new Map<NoteId, NoteView>();
|
|
165
|
+
this.#notes.set(written.memory, notes);
|
|
166
|
+
if (notes.has(written.note)) return;
|
|
167
|
+
const ownersWord = written.source.kind === "owner" && written.by.kind === "owner";
|
|
168
|
+
this.#sources.set(`${written.memory} ${written.note}`, new Set([sourceKey(written.source, written.by)]));
|
|
169
|
+
notes.set(written.note, {
|
|
170
|
+
memory: written.memory,
|
|
171
|
+
note: written.note,
|
|
172
|
+
text: written.text,
|
|
173
|
+
...(written.detail === undefined ? {} : { detail: written.detail }),
|
|
174
|
+
about: written.about,
|
|
175
|
+
...(written.project === undefined ? {} : { project: written.project }),
|
|
176
|
+
source: written.source,
|
|
177
|
+
by: written.by,
|
|
178
|
+
status: "live",
|
|
179
|
+
confirmed: ownersWord,
|
|
180
|
+
sources: 1,
|
|
181
|
+
changed: Number(stored.at),
|
|
182
|
+
top: false,
|
|
183
|
+
pinned: false,
|
|
184
|
+
revision: 1,
|
|
185
|
+
written: at,
|
|
186
|
+
seq: at,
|
|
187
|
+
});
|
|
188
|
+
return;
|
|
189
|
+
}
|
|
190
|
+
const taken = dataAs(stored, DigestTaken);
|
|
191
|
+
if (taken !== undefined) {
|
|
192
|
+
const digests = this.#digests.get(taken.memory) ?? [];
|
|
193
|
+
this.#digests.set(taken.memory, digests);
|
|
194
|
+
digests.push({ turn: taken.turn, record: taken.record, session: taken.session, ...(taken.project === undefined ? {} : { project: taken.project }), seq: at });
|
|
195
|
+
return;
|
|
196
|
+
}
|
|
197
|
+
const wanted = dataAs(stored, ShareWanted);
|
|
198
|
+
if (wanted !== undefined) {
|
|
199
|
+
const key = shareKey(wanted.memory, wanted.note, wanted.revision, wanted.for, wanted.purpose);
|
|
200
|
+
if (!this.#shares.has(key)) this.#shares.set(key, { state: "asked", at });
|
|
201
|
+
return;
|
|
202
|
+
}
|
|
203
|
+
const answered = dataAs(stored, ShareAnswered);
|
|
204
|
+
if (answered !== undefined) {
|
|
205
|
+
this.#shares.set(shareKey(answered.memory, answered.note, answered.revision, answered.for, answered.purpose), { state: answered.allowed ? "allowed" : "refused", at });
|
|
206
|
+
return;
|
|
207
|
+
}
|
|
208
|
+
const page = dataAs(stored, PageWritten);
|
|
209
|
+
if (page !== undefined) {
|
|
210
|
+
const pages = this.#pages.get(page.memory) ?? new Map<PageName, PageView>();
|
|
211
|
+
this.#pages.set(page.memory, pages);
|
|
212
|
+
pages.set(page.page, {
|
|
213
|
+
memory: page.memory,
|
|
214
|
+
page: page.page,
|
|
215
|
+
title: page.title,
|
|
216
|
+
text: page.text,
|
|
217
|
+
...(page.project === undefined ? {} : { project: page.project }),
|
|
218
|
+
revision: page.revision,
|
|
219
|
+
by: page.by,
|
|
220
|
+
seq: at,
|
|
221
|
+
});
|
|
222
|
+
return;
|
|
223
|
+
}
|
|
224
|
+
const corroborated = dataAs(stored, NoteCorroborated);
|
|
225
|
+
if (corroborated !== undefined) {
|
|
226
|
+
const counted = this.#sources.get(`${corroborated.memory} ${corroborated.note}`);
|
|
227
|
+
if (counted === undefined) return;
|
|
228
|
+
counted.add(sourceKey(corroborated.source, corroborated.by));
|
|
229
|
+
const ownersWord = corroborated.source.kind === "owner" && corroborated.by.kind === "owner";
|
|
230
|
+
this.#change(corroborated.memory, corroborated.note, at, (note) => ({ ...note, sources: counted.size, confirmed: note.confirmed || ownersWord }));
|
|
231
|
+
return;
|
|
232
|
+
}
|
|
233
|
+
const confirmed = dataAs(stored, NoteConfirmed);
|
|
234
|
+
if (confirmed !== undefined) {
|
|
235
|
+
this.#change(confirmed.memory, confirmed.note, at, (note) => ({ ...note, confirmed: true }));
|
|
236
|
+
return;
|
|
237
|
+
}
|
|
238
|
+
const corrected = dataAs(stored, NoteCorrected);
|
|
239
|
+
if (corrected !== undefined) {
|
|
240
|
+
// A new text is a new claim: what backed the old one does not back it, and the one who wrote it is its
|
|
241
|
+
// only source. It earns its place in the top layer again, unless it was placed by hand.
|
|
242
|
+
const source: NoteSource = corrected.by.kind === "owner" ? { kind: "owner" } : { kind: "inferred" };
|
|
243
|
+
this.#sources.set(`${corrected.memory} ${corrected.note}`, new Set([sourceKey(source, corrected.by)]));
|
|
244
|
+
this.#change(corrected.memory, corrected.note, at, (note) => {
|
|
245
|
+
const { detail: _dropped, ...rest } = note;
|
|
246
|
+
return {
|
|
247
|
+
...rest,
|
|
248
|
+
text: corrected.text,
|
|
249
|
+
...(corrected.detail === undefined ? {} : { detail: corrected.detail }),
|
|
250
|
+
source,
|
|
251
|
+
by: corrected.by,
|
|
252
|
+
sources: 1,
|
|
253
|
+
top: note.pinned && note.top,
|
|
254
|
+
revision: note.revision + 1,
|
|
255
|
+
confirmed: corrected.by.kind === "owner",
|
|
256
|
+
};
|
|
257
|
+
});
|
|
258
|
+
return;
|
|
259
|
+
}
|
|
260
|
+
const retracted = dataAs(stored, NoteRetracted);
|
|
261
|
+
if (retracted !== undefined) {
|
|
262
|
+
this.#change(retracted.memory, retracted.note, at, (note) => (note.status === "live" ? { ...note, status: "retracted" } : note));
|
|
263
|
+
return;
|
|
264
|
+
}
|
|
265
|
+
const lifted = dataAs(stored, NoteLifted);
|
|
266
|
+
if (lifted !== undefined) {
|
|
267
|
+
this.#change(lifted.memory, lifted.note, at, (note) => ({ ...note, top: true, pinned: true }));
|
|
268
|
+
return;
|
|
269
|
+
}
|
|
270
|
+
const dropped = dataAs(stored, NoteDropped);
|
|
271
|
+
if (dropped !== undefined) {
|
|
272
|
+
this.#change(dropped.memory, dropped.note, at, (note) => ({ ...note, top: false, pinned: true }));
|
|
273
|
+
return;
|
|
274
|
+
}
|
|
275
|
+
const erased = dataAs(stored, NoteErased);
|
|
276
|
+
if (erased !== undefined) {
|
|
277
|
+
this.#change(erased.memory, erased.note, at, (note) => {
|
|
278
|
+
const { detail: _dropped, ...rest } = note;
|
|
279
|
+
return { ...rest, text: "", status: "erased", top: false };
|
|
280
|
+
});
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
/** When the record folded last was written. */
|
|
285
|
+
#when = 0;
|
|
286
|
+
|
|
287
|
+
#change(memory: MemoryName, id: NoteId, at: number, fn: (note: NoteView) => NoteView): void {
|
|
288
|
+
const notes = this.#notes.get(memory);
|
|
289
|
+
const note = notes?.get(id);
|
|
290
|
+
if (notes === undefined || note === undefined || note.status === "erased") return;
|
|
291
|
+
notes.set(id, { ...fn(note), seq: at, changed: this.#when });
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
#consolidated(done: { memory: MemoryName; merged: Array<{ note: NoteId; into: NoteId }>; lifted: NoteId[]; dropped: NoteId[]; retired: NoteId[] }, at: number): void {
|
|
295
|
+
const { memory } = done;
|
|
296
|
+
this.#changes.set(memory, 0);
|
|
297
|
+
for (const { note, into } of done.merged) {
|
|
298
|
+
const away = this.note(memory, note);
|
|
299
|
+
const kept = this.#sources.get(`${memory} ${into}`);
|
|
300
|
+
for (const key of this.#sources.get(`${memory} ${note}`) ?? []) kept?.add(key);
|
|
301
|
+
this.#change(memory, note, at, (one) => ({ ...one, status: "merged", top: false }));
|
|
302
|
+
this.#change(memory, into, at, (one) => ({ ...one, sources: kept?.size ?? one.sources, confirmed: one.confirmed || away?.confirmed === true }));
|
|
303
|
+
}
|
|
304
|
+
for (const note of done.lifted) this.#change(memory, note, at, (one) => ({ ...one, top: true }));
|
|
305
|
+
for (const note of done.dropped) this.#change(memory, note, at, (one) => ({ ...one, top: false }));
|
|
306
|
+
for (const note of done.retired) this.#change(memory, note, at, (one) => ({ ...one, status: "retired", top: false }));
|
|
307
|
+
}
|
|
308
|
+
}
|
package/src/holds.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
// What holds where: the two questions every reader of a memory asks of a note.
|
|
2
|
+
|
|
3
|
+
import type { NoteView } from "./records.ts";
|
|
4
|
+
|
|
5
|
+
/** Whether a note holds now: neither taken back nor erased. */
|
|
6
|
+
export function holds(note: NoteView): boolean {
|
|
7
|
+
return note.status === "live";
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
/** Whether what holds for `scope` (a project, or none for every project) holds where `project` is asked for. */
|
|
11
|
+
export function holdsFor(scope: string | undefined, project: string | undefined): boolean {
|
|
12
|
+
return scope === undefined || project === undefined || scope === project;
|
|
13
|
+
}
|