@pal-code/memory 0.0.18 → 0.0.19

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/README.md CHANGED
@@ -17,9 +17,11 @@ Ships two tools and one prompt fragment:
17
17
  pal add @pal-code/memory
18
18
  ```
19
19
 
20
- Then create a `.memory/` directory in the repo you want it for. **No directory means
21
- no tools and no prompt** — the extension reports itself unavailable, so repos that
22
- don't use it pay zero tokens. This mirrors `@pal-code/jev`.
20
+ The `.memory/` directory is created on load when writing is enabled, which it is by
21
+ default — so the tools work immediately after install. **With `write: false` and no
22
+ existing directory, there are no tools and no prompt**: the extension reports itself
23
+ unavailable, so repos that don't use it pay zero tokens. That gating mirrors
24
+ `@pal-code/jev`.
23
25
 
24
26
  ## Why the staleness verdict is the whole design
25
27
 
@@ -74,7 +76,7 @@ Extension-owned (per `EXTENSIONS.md`) — nothing goes in `.pal.json`:
74
76
  ```json
75
77
  {
76
78
  "dir": ".memory",
77
- "write": false,
79
+ "write": true,
78
80
  "staleAfterDays": 90
79
81
  }
80
82
  ```
@@ -82,13 +84,20 @@ Extension-owned (per `EXTENSIONS.md`) — nothing goes in `.pal.json`:
82
84
  | Key | Default | Meaning |
83
85
  |---|---|---|
84
86
  | `dir` | `.memory` | Directory name, relative to the repo root |
85
- | `write` | `false` | Allow `memory_save`. Reads always work |
87
+ | `write` | `true` | Allow `memory_save`, and create the directory on load. Reads always work |
86
88
  | `staleAfterDays` | `90` | Reserved for time-based decay |
87
89
 
88
90
  Resolution: defaults → project → user. A malformed config is ignored, never fatal.
89
91
 
90
- **Writing is off by default on purpose.** Auto-writing knowledge produces junk that
91
- later agents trust; enabling it is a deliberate act.
92
+ **Writing is on by default**, mirroring `@pal-code/workflow`. On load, `setup()`
93
+ creates the memory directory when `write` is on, so a fresh repo gets working tools
94
+ without hand-editing anything — the same chicken-and-egg `work_start` solves for
95
+ `.work`. The directory is not seeded with an `index.md`; an empty directory already
96
+ reads as "no knowledge recorded yet".
97
+
98
+ Opt out with `{"write": false}`. Auto-writing knowledge can produce junk that later
99
+ agents trust, so a repo that does not want memory on disk should turn it off
100
+ explicitly rather than relying on the extension being inert.
92
101
 
93
102
  ## Keeping it out of git
94
103
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pal-code/memory",
3
- "version": "0.0.18",
3
+ "version": "0.0.19",
4
4
  "description": "Repo knowledge memory for pal — schemas, playbooks and past runs the agent reads before re-exploring",
5
5
  "license": "MIT",
6
6
  "publishConfig": {
@@ -14,7 +14,7 @@
14
14
  "src/**/*.ts"
15
15
  ],
16
16
  "dependencies": {
17
- "@pal-code/core": "0.0.18"
17
+ "@pal-code/core": "0.0.19"
18
18
  },
19
19
  "engines": {
20
20
  "bun": ">=1.1.0"
@@ -38,7 +38,8 @@ describe('resolveMemorySettings', () => {
38
38
  withHome(() => {
39
39
  const settings = resolveMemorySettings(dir);
40
40
  expect(settings.dir).toBe('.memory');
41
- expect(settings.write).toBe(false);
41
+ // Writing defaults on so a fresh repo gets working tools; see config.ts.
42
+ expect(settings.write).toBe(true);
42
43
  expect(settings.staleAfterDays).toBe(90);
43
44
  });
44
45
  rmSync(dir, {recursive: true, force: true});
@@ -89,6 +90,15 @@ describe('resolveMemorySettings', () => {
89
90
 
90
91
  test('a non-boolean write flag is ignored', () => {
91
92
  const dir = project({'memory.json': {write: 'yes'}});
93
+ withHome(() => {
94
+ // Falls back to the default (true) rather than coercing the string.
95
+ expect(resolveMemorySettings(dir).write).toBe(true);
96
+ });
97
+ rmSync(dir, {recursive: true, force: true});
98
+ });
99
+
100
+ test('an explicit write:false is honoured against the on-by-default', () => {
101
+ const dir = project({'memory.json': {write: false}});
92
102
  withHome(() => {
93
103
  expect(resolveMemorySettings(dir).write).toBe(false);
94
104
  });
package/src/config.ts CHANGED
@@ -23,7 +23,11 @@ export interface MemorySettings {
23
23
 
24
24
  const DEFAULTS: MemorySettings = {
25
25
  dir: '.memory',
26
- write: false,
26
+ // Writing is on by default, mirroring `@pal-code/workflow`. Setup creates the
27
+ // directory on load, so a fresh repo gets working memory tools instead of an
28
+ // extension that is inert until the user hand-edits two files. A repo that
29
+ // does not want memory on disk sets `{"write": false}`.
30
+ write: true,
27
31
  staleAfterDays: 90,
28
32
  };
29
33
 
package/src/index.test.ts CHANGED
@@ -36,11 +36,52 @@ function seedMemory(body = '# index\n\nnotes') {
36
36
  }
37
37
 
38
38
  describe('availability', () => {
39
- test('an extension with no .memory directory offers nothing', () => {
39
+ test('an extension with no .memory directory and no write offers nothing', () => {
40
+ writeFileSync(join(repo, 'memory.json'), JSON.stringify({write: false}));
40
41
  expect(extension.available).toBe(false);
41
42
  expect(extension.tools).toEqual([]);
42
43
  });
43
44
 
45
+ test('write enabled creates the directory on setup, making the tools available', async () => {
46
+ // The chicken-and-egg this fixes: `available` used to require the
47
+ // directory, nothing created it, so memory_save could never be reached to
48
+ // create it. setup() now does, before `available` is read.
49
+ enableWrite();
50
+ expect(existsSync(join(repo, '.memory'))).toBe(false);
51
+
52
+ await extension.setup?.();
53
+
54
+ expect(existsSync(join(repo, '.memory'))).toBe(true);
55
+ expect(extension.available).toBe(true);
56
+ expect(extension.tools.map((t) => t.function.name)).toEqual(['memory_lookup', 'memory_save']);
57
+ });
58
+
59
+ test('setup is a no-op when writing is disabled', async () => {
60
+ writeFileSync(join(repo, 'memory.json'), JSON.stringify({write: false}));
61
+
62
+ await extension.setup?.();
63
+
64
+ expect(existsSync(join(repo, '.memory'))).toBe(false);
65
+ expect(extension.available).toBe(false);
66
+ });
67
+
68
+ test('setup does not seed an index.md', async () => {
69
+ // A stub doc would list as a doc that can never be fresh.
70
+ enableWrite();
71
+ await extension.setup?.();
72
+
73
+ expect(existsSync(join(repo, '.memory', 'index.md'))).toBe(false);
74
+ });
75
+
76
+ test('setup respects a custom directory name', async () => {
77
+ writeFileSync(join(repo, 'memory.json'), JSON.stringify({write: true, dir: '.notes'}));
78
+
79
+ await extension.setup?.();
80
+
81
+ expect(existsSync(join(repo, '.notes'))).toBe(true);
82
+ expect(existsSync(join(repo, '.memory'))).toBe(false);
83
+ });
84
+
44
85
  test('a .memory directory makes both tools available', () => {
45
86
  seedMemory();
46
87
  expect(extension.available).toBe(true);
@@ -77,8 +118,9 @@ describe('availability', () => {
77
118
  expect(lookup.function.description).toContain('any task');
78
119
  });
79
120
 
80
- test('the prompt does not claim tools exist in a repo without .memory', () => {
121
+ test('the prompt is withheld when memory is disabled and no directory exists', () => {
81
122
  // Guard against a prompt that mentions tools the model cannot call.
123
+ writeFileSync(join(repo, 'memory.json'), JSON.stringify({write: false}));
82
124
  expect(extension.available).toBe(false);
83
125
  });
84
126
  });
@@ -189,6 +231,7 @@ describe('memory_save', () => {
189
231
 
190
232
  test('refuses to write when writing is disabled, and says how to enable it', async () => {
191
233
  seedMemory();
234
+ writeFileSync(join(repo, 'memory.json'), JSON.stringify({write: false}));
192
235
  const out = await save({
193
236
  id: 'schemas/core.md',
194
237
  kind: 'schema',
package/src/index.ts CHANGED
@@ -14,11 +14,11 @@
14
14
  * no prompt fragment, so a repo that doesn't use this pays zero tokens.
15
15
  */
16
16
 
17
- import {existsSync} from 'node:fs';
17
+ import {existsSync, mkdirSync} from 'node:fs';
18
18
 
19
19
  import type {ToolDef, ToolExtension, ToolHandler} from '@pal-code/core';
20
20
 
21
- import {resolveMemorySettings} from './config.js';
21
+ import {resolveMemorySettings, memoryRoot} from './config.js';
22
22
  import type {MemorySettings} from './config.js';
23
23
  import {checkFreshness, listDocs, saveDoc, searchDocs} from './store.js';
24
24
  import type {MemoryDoc} from './store.js';
@@ -27,10 +27,44 @@ function settingsFor(cwd?: string): MemorySettings {
27
27
  return resolveMemorySettings(cwd);
28
28
  }
29
29
 
30
- /** Is there a `.memory` directory here at all? */
30
+ /**
31
+ * Is this repo using repo memory?
32
+ *
33
+ * Mirrors `@pal-code/workflow`: true when the memory directory exists, or when
34
+ * writing is enabled — because then `setup` can create it. Gating on the
35
+ * directory alone would make the extension permanently unavailable in exactly
36
+ * the repo where someone wants to start recording for the first time.
37
+ */
31
38
  function isAvailable(): boolean {
32
39
  const settings = settingsFor();
33
- return existsSync(settings.dir) || existsSync(`${settings.dir}/index.md`);
40
+ return existsSync(memoryRoot(settings)) || settings.write;
41
+ }
42
+
43
+ /**
44
+ * Create the memory directory on first load when writing is enabled.
45
+ *
46
+ * Without this, `write: true` alone still left the extension inert on a fresh
47
+ * repo: `available` was false because the directory did not exist, and nothing
48
+ * ever created it, so `memory_save` could not be reached to create it either.
49
+ * The same chicken-and-egg that `work_start` solves for `.work`.
50
+ *
51
+ * Deliberately does not seed an `index.md`: an empty directory already reads as
52
+ * "no knowledge recorded yet", and a stub would show up in the index as a doc
53
+ * that can never be fresh.
54
+ *
55
+ * Runs before `available` is read (see core's loader), so creating the
56
+ * directory here is what makes the getter above return true. Never throws — a
57
+ * read-only checkout must degrade to "unavailable", not to a failed extension.
58
+ */
59
+ function ensureMemoryRoot(): void {
60
+ const settings = settingsFor();
61
+ if (!settings.write) return;
62
+ try {
63
+ mkdirSync(memoryRoot(settings), {recursive: true});
64
+ } catch {
65
+ // Read-only filesystem, or a path that is not a directory. Leave it to
66
+ // isAvailable to report the extension as unavailable.
67
+ }
34
68
  }
35
69
 
36
70
  /**
@@ -274,5 +308,6 @@ export default {
274
308
  return isAvailable() ? [lookupDef, saveDef] : [];
275
309
  },
276
310
  prompt: PROMPT,
311
+ setup: ensureMemoryRoot,
277
312
  handlers,
278
313
  } satisfies ToolExtension;