@skillre/dsh-plugin-sticky-wall 0.7.0 → 0.8.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/lib/store.d.ts CHANGED
@@ -1,21 +1,28 @@
1
1
  /**
2
- * Durable note store over the DSH storage domain.
2
+ * Durable note store over the real `.md` files under the notes directory.
3
3
  *
4
4
  * The Host owns all data: the Client renders snapshots and submits intents, and
5
5
  * every write goes through {@link StickyWallStore} — validate, persist, then
6
- * hand back the changed records. The storage domain provides a single
7
- * per-domain write chain, so concurrent writes cannot interleave, and its
8
- * `per-record` layout keeps one file per note: one damaged record cannot take
9
- * the whole wall down.
6
+ * hand back the changed records.
10
7
  *
11
- * The domain spec is mirrored structurally instead of imported: importing
12
- * `@deepseek-ai/dsh-storage-domain` would install a second copy of the domain
13
- * package (and its zod) inside the plugin instead of resolving the host's. The
14
- * mirrored shape is the contract the live Host validates at `open()`:
15
- * `{ name, version, layout, invalidRecords, tables: { <name>: { valueSchema } } }`
16
- * with zod record schemas.
8
+ * 0.8.0 moved the notes off the storage domain and onto plain files the user can
9
+ * open in any editor: one `.md` per note, frontmatter for the attributes, the
10
+ * body being the note text itself. That shape is why this store looks the way it
11
+ * does:
12
+ *
13
+ * - **Reads are synchronous.** Every note is loaded once at `open()` into the
14
+ * in-memory map, which stays the AUTHORITATIVE cache. The wall polls with the
15
+ * cheap `revision` call, so a read must never touch the disk.
16
+ * - **Writes go to disk FIRST, then to the cache.** A resolved mutation means
17
+ * the bytes are on disk; the cache can never run ahead of the file, and a
18
+ * failed write leaves the previous state intact.
19
+ * - **Every mutation is serialised** on one chain, so a check-then-write pair —
20
+ * the `maxNotes` count followed by the write — cannot interleave with another
21
+ * request. The filesystem gives us no such guarantee on its own.
22
+ * - **One unreadable file never takes the wall down.** A file that is too large,
23
+ * misnamed or carries a broken frontmatter block is skipped, or degraded to a
24
+ * note with `metaError`, but never thrown away.
17
25
  */
18
- import { z } from 'zod';
19
26
  import { type CentreReserve, type Note, type NoteInput, type NoteLayout, type NotePatch } from './types.js';
20
27
  /** Failure carrying a stable code the transport maps onto the RPC envelope. */
21
28
  export declare class WallError extends Error {
@@ -23,46 +30,16 @@ export declare class WallError extends Error {
23
30
  readonly details: Record<string, unknown>;
24
31
  constructor(code: string, message: string, details?: Record<string, unknown>);
25
32
  }
26
- /** The domain declaration handed to `ctx.storageDomain.open()`. */
27
- export declare const DOMAIN_SPEC: {
28
- name: string;
29
- version: number;
30
- layout: "per-record";
31
- invalidRecords: "backup-and-skip";
32
- tables: {
33
- notes: {
34
- valueSchema: z.ZodObject<{
35
- id: z.ZodString;
36
- title: z.ZodDefault<z.ZodString>;
37
- body: z.ZodString;
38
- tags: z.ZodDefault<z.ZodArray<z.ZodString>>;
39
- color: z.ZodDefault<z.ZodEnum<{
40
- yellow: "yellow";
41
- pink: "pink";
42
- green: "green";
43
- blue: "blue";
44
- purple: "purple";
45
- orange: "orange";
46
- }>>;
47
- pinned: z.ZodDefault<z.ZodBoolean>;
48
- archived: z.ZodDefault<z.ZodBoolean>;
49
- createdAt: z.ZodString;
50
- updatedAt: z.ZodString;
51
- layout: z.ZodDefault<z.ZodNullable<z.ZodObject<{
52
- x: z.ZodNumber;
53
- y: z.ZodNumber;
54
- rotate: z.ZodNumber;
55
- width: z.ZodEnum<{
56
- sm: "sm";
57
- md: "md";
58
- lg: "lg";
59
- }>;
60
- }, z.core.$strip>>>;
61
- }, z.core.$loose>;
62
- };
63
- };
64
- };
65
- /** The minimal domain table face this store uses; the rest is unused here. */
33
+ /**
34
+ * The legacy storage-domain table face, kept only so the one-time migration can
35
+ * read what a 0.7.x install left behind.
36
+ *
37
+ * The store itself no longer touches a domain: the caller opens the legacy
38
+ * domain, drains its `notes` table into `Note[]`, and hands that array to
39
+ * `open()` as `readLegacy`. Mirroring the two-method shape here means the Host
40
+ * entry does not have to re-declare it, and keeps the old file's public surface
41
+ * importable while the migration exists.
42
+ */
66
43
  export interface DomainTable {
67
44
  get(key: string): unknown;
68
45
  entries(): Iterable<[string, unknown]>;
@@ -72,40 +49,106 @@ export interface DomainTable {
72
49
  update(key: string, transform: (value: Note) => Note): Promise<Note>;
73
50
  delete(key: string): Promise<boolean>;
74
51
  }
75
- /** The opened-domain handle the facility returns. */
52
+ /** The opened-domain handle a legacy migration reads through. */
76
53
  export interface DomainHandle {
77
54
  table(name: string): DomainTable;
78
55
  close(): Promise<void>;
79
56
  }
57
+ /** Everything `open()` needs to build a store. */
58
+ export interface StoreOptions {
59
+ /** Directory holding one `.md` per note; created recursively when absent. */
60
+ notesDir: string;
61
+ /** Reject a write that would take the live (non-archived) note count past this. */
62
+ maxNotes: number;
63
+ /** Write the built-in welcome note when the wall is empty. */
64
+ seedWelcomeNote: boolean;
65
+ /**
66
+ * Reader for the legacy storage-domain notes.
67
+ *
68
+ * When present, `open()` runs the one-time migration BEFORE scanning the
69
+ * directory. Leave it out and the store opens whatever `.md` files are there
70
+ * and nothing else — which is what the tests and any non-DSH caller want.
71
+ */
72
+ readLegacy?: (() => Promise<Note[]>) | undefined;
73
+ /** Injectable clock; tests use a stepping clock so timestamps are distinct. */
74
+ now?: (() => Date) | undefined;
75
+ /** Sink for non-fatal problems (an oversized or unreadable note file). */
76
+ log?: ((message: string) => void) | undefined;
77
+ }
80
78
  /** The store facade the Host endpoint and the agent tool share. */
81
79
  export declare class StickyWallStore {
82
- private readonly domain;
80
+ /** Absolute directory the notes live in. */
81
+ readonly notesDir: string;
82
+ private readonly notes;
83
83
  private readonly maxNotes;
84
84
  private readonly now;
85
- private readonly notes;
85
+ private readonly log;
86
+ /** Backing counter for {@link revision}. */
87
+ private counter;
86
88
  private closed;
87
89
  /**
88
90
  * Store-level mutation chain.
89
91
  *
90
- * The storage domain serializes its own writes, but a read-then-write pair
91
- * (the `maxNotes` check followed by the `put`) is two steps: two concurrent
92
- * creates at the boundary would both read the pre-write count and both
93
- * succeed. Every mutation therefore runs on this chain, so each check and its
94
- * write are one indivisible step.
92
+ * A read-then-write pair (the `maxNotes` check followed by the file write) is
93
+ * two steps, and two concurrent creates at the boundary would both read the
94
+ * pre-write count and both succeed. Every mutation therefore runs on this
95
+ * chain, so each check and its write are one indivisible step.
95
96
  */
96
97
  private chain;
97
- constructor(domain: DomainHandle, maxNotes: number, now?: () => Date);
98
- /** Open one domain and wrap it in a store. */
99
- static open(open: (spec: typeof DOMAIN_SPEC) => Promise<DomainHandle>, options: {
100
- maxNotes: number;
101
- seedWelcomeNote: boolean;
102
- now?: () => Date;
103
- }): Promise<StickyWallStore>;
104
- /** Release the domain handle; later writes fail instead of silently dropping. */
98
+ private constructor();
99
+ /**
100
+ * Open the notes directory and load every note.
101
+ *
102
+ * Order matters: the one-time migration runs BEFORE the directory is created,
103
+ * because the migration publishes its result with a single atomic `rename` of
104
+ * a staging directory and would otherwise have to rename over a directory this
105
+ * method had just made. Once the directory and the cache agree, the store is
106
+ * usable; every later read is served from memory.
107
+ */
108
+ static open(options: StoreOptions): Promise<StickyWallStore>;
109
+ /**
110
+ * Monotonic change counter; bumps once per mutation that actually changed
111
+ * something.
112
+ *
113
+ * The Client cannot be pushed to, so it polls. An unchanged number means the
114
+ * caller must not touch component state at all — that is what keeps a
115
+ * background refresh from interrupting someone mid-composition in the editor.
116
+ */
117
+ get revision(): number;
118
+ /**
119
+ * Close the store: drain queued writes, then refuse new ones.
120
+ *
121
+ * Draining is the point. A mutation that started before `close()` may still be
122
+ * writing its file; dropping the chain would acknowledge a note to the Client
123
+ * and then lose it on shutdown.
124
+ */
105
125
  close(): Promise<void>;
106
126
  private assertOpen;
107
127
  /** Queue one mutation so its checks and writes cannot interleave with another. */
108
128
  private mutate;
129
+ /** Absolute `.md` path for a note id. */
130
+ pathOf(id: string): string;
131
+ /**
132
+ * Read every note file into the cache.
133
+ *
134
+ * The directory is the source of truth, so this is deliberately forgiving: a
135
+ * subdirectory, a file that is not named after a valid note id, a file too
136
+ * large to be a note, or one that cannot be read is skipped with a log line
137
+ * and the rest of the wall still opens.
138
+ */
139
+ private load;
140
+ /**
141
+ * Persist one note.
142
+ *
143
+ * The bytes go to a sibling temp file and are then `rename`d over the target:
144
+ * within one directory that is atomic, so a reader — or a crash — sees either
145
+ * the old note or the new one and never a half-written file. Writing the
146
+ * target directly would truncate it first, and a crash in that window loses
147
+ * the note outright.
148
+ */
149
+ private writeNote;
150
+ /** Remove one note file; already gone is not an error. */
151
+ private dropFile;
109
152
  private liveCount;
110
153
  /**
111
154
  * Reject a write that would take the live (non-archived) note count past
@@ -118,8 +161,20 @@ export declare class StickyWallStore {
118
161
  private assertRoom;
119
162
  /** Write the one-time welcome note on a brand-new wall. */
120
163
  private seedWelcomeNote;
164
+ /** One note by id, or `null` when the wall has no such note. */
165
+ read(id: string): Note | null;
121
166
  /** Every stored note, newest-relevant first; archived ones only on request. */
122
167
  list(includeArchived?: boolean, sort?: 'pinned' | 'created' | 'updated'): Note[];
168
+ /**
169
+ * The snapshot the store's own mutations work from.
170
+ *
171
+ * Deliberately does NOT check `closed`: {@link close} drains queued writes
172
+ * AFTER flipping the flag, so a mutation that was already acknowledged still
173
+ * has to run to completion — refusing it here would lose a note the caller
174
+ * was told was saved. It is private for exactly that reason: an outside caller
175
+ * always goes through {@link list}, which does check.
176
+ */
177
+ private gathered;
123
178
  /**
124
179
  * Create one note from a Client or agent submission.
125
180
  *