@pal-code/memory 0.0.18 → 0.0.20

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
 
@@ -100,6 +109,18 @@ printf '.memory/\n' >> ~/.config/git/ignore
100
109
  git config --global core.excludesFile ~/.config/git/ignore
101
110
  ```
102
111
 
112
+ On startup the extension runs `git check-ignore` on the directory and prints a
113
+ one-line advisory naming this command when it is **not** ignored — nothing is
114
+ ever rewritten for you, since a global exclude affects every repo on the
115
+ machine. Check it yourself at any time:
116
+
117
+ ```sh
118
+ git check-ignore -v .memory # empty output = not ignored
119
+ ```
120
+
121
+ Note that a global exclude never affects files git already tracks; if `.memory`
122
+ was committed before, untrack it with `git rm -r --cached .memory`.
123
+
103
124
  `ripgrep` and `biome` inherit this through git, so nothing lints or greps it either.
104
125
 
105
126
  One consequence worth knowing: `pal`'s `grep_search` passes `--hidden` but not
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.20",
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.20"
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
 
@@ -0,0 +1,104 @@
1
+ /**
2
+ * The advisory fires only when git genuinely does not ignore the directory.
3
+ *
4
+ * Every case here pins HOME and disables the machine's global excludes, because
5
+ * a developer machine that already ignores `.memory` would otherwise make the
6
+ * "unignored" case silently pass for the wrong reason.
7
+ */
8
+
9
+ import {afterEach, beforeEach, describe, expect, test} from 'bun:test';
10
+ import {execFileSync} from 'node:child_process';
11
+ import {mkdirSync, mkdtempSync, rmSync, writeFileSync} from 'node:fs';
12
+ import {tmpdir} from 'node:os';
13
+ import {join} from 'node:path';
14
+
15
+ import {isDirGitIgnored, unignoredDirNotice} from './gitignore.js';
16
+
17
+ const originalHome = process.env.HOME;
18
+ const originalProfile = process.env.USERPROFILE;
19
+ const originalExcludes = process.env.GIT_CONFIG_GLOBAL;
20
+
21
+ let root: string;
22
+ let home: string;
23
+
24
+ beforeEach(() => {
25
+ root = mkdtempSync(join(tmpdir(), 'pal-gi-'));
26
+ home = join(root, 'home');
27
+ mkdirSync(home, {recursive: true});
28
+ // An empty global config, so the machine's real ignore file cannot leak in.
29
+ writeFileSync(join(home, '.gitconfig'), '');
30
+ process.env.HOME = home;
31
+ process.env.USERPROFILE = home;
32
+ process.env.GIT_CONFIG_GLOBAL = join(home, '.gitconfig');
33
+ });
34
+
35
+ afterEach(() => {
36
+ process.env.HOME = originalHome;
37
+ process.env.USERPROFILE = originalProfile;
38
+ if (originalExcludes === undefined) delete process.env.GIT_CONFIG_GLOBAL;
39
+ else process.env.GIT_CONFIG_GLOBAL = originalExcludes;
40
+ rmSync(root, {recursive: true, force: true});
41
+ });
42
+
43
+ /** A real git repo with the given directory created inside it. */
44
+ function repoWith(dirName: string): string {
45
+ const dir = mkdtempSync(join(root, 'repo-'));
46
+ execFileSync('git', ['init', '-q'], {cwd: dir});
47
+ mkdirSync(join(dir, dirName), {recursive: true});
48
+ return dir;
49
+ }
50
+
51
+ describe('isDirGitIgnored', () => {
52
+ test('false when git tracks nothing about the directory', () => {
53
+ const dir = repoWith('.memory');
54
+ expect(isDirGitIgnored('.memory', dir)).toBe(false);
55
+ });
56
+
57
+ test('true when a repo-local exclude covers it', () => {
58
+ const dir = repoWith('.memory');
59
+ writeFileSync(join(dir, '.git', 'info', 'exclude'), '.memory/\n');
60
+ expect(isDirGitIgnored('.memory', dir)).toBe(true);
61
+ });
62
+
63
+ test('true when .gitignore covers it', () => {
64
+ const dir = repoWith('.work');
65
+ writeFileSync(join(dir, '.gitignore'), '.work/\n');
66
+ expect(isDirGitIgnored('.work', dir)).toBe(true);
67
+ });
68
+
69
+ test('undefined outside a git repo, so no spurious advisory', () => {
70
+ const dir = mkdtempSync(join(root, 'plain-'));
71
+ mkdirSync(join(dir, '.memory'), {recursive: true});
72
+ expect(isDirGitIgnored('.memory', dir)).toBeUndefined();
73
+ });
74
+ });
75
+
76
+ describe('unignoredDirNotice', () => {
77
+ test('names the directory and the exact command to fix it', () => {
78
+ const dir = repoWith('.memory');
79
+ const notice = unignoredDirNotice('.memory', dir);
80
+
81
+ expect(notice).toBeDefined();
82
+ expect(notice).toContain('.memory is not ignored by git');
83
+ expect(notice).toContain("printf '.memory/\\n' >> ~/.config/git/ignore");
84
+ });
85
+
86
+ test('is silent once the directory is ignored', () => {
87
+ const dir = repoWith('.memory');
88
+ writeFileSync(join(dir, '.git', 'info', 'exclude'), '.memory/\n');
89
+ expect(unignoredDirNotice('.memory', dir)).toBeUndefined();
90
+ });
91
+
92
+ test('is silent outside a git repo', () => {
93
+ const dir = mkdtempSync(join(root, 'plain-'));
94
+ mkdirSync(join(dir, '.memory'), {recursive: true});
95
+ expect(unignoredDirNotice('.memory', dir)).toBeUndefined();
96
+ });
97
+
98
+ test('reports on a path even when it does not exist — callers gate on existence', () => {
99
+ // git check-ignore is path-shaped, not existence-shaped. Both extensions
100
+ // check existsSync before calling this, so that gate lives there.
101
+ const dir = repoWith('.memory');
102
+ expect(unignoredDirNotice('.work', dir)).toContain('.work is not ignored');
103
+ });
104
+ });
@@ -0,0 +1,56 @@
1
+ /**
2
+ * Is a scratch directory ignored by git, machine-wide?
3
+ *
4
+ * Both `@pal-code/memory` and `@pal-code/workflow` write into a directory that
5
+ * must never be committed, and both READMEs tell the user to add it to
6
+ * `~/.config/git/ignore` — but nothing ever checked that they did. The failure
7
+ * is silent and ugly: the files show up in `git status` and can be committed by
8
+ * accident.
9
+ *
10
+ * Detection only. Rewriting someone's global git config from inside an editor
11
+ * would affect every repo on the machine, so the extension reports the exact
12
+ * command instead and lets the user decide.
13
+ */
14
+
15
+ import {execFileSync} from 'node:child_process';
16
+
17
+ /**
18
+ * Run `git check-ignore -q <dir>` and report whether it is ignored.
19
+ *
20
+ * Returns `undefined` when the answer is unknowable — not a git repo, git not
21
+ * installed, or the directory does not exist yet. Callers must treat
22
+ * `undefined` as "say nothing" rather than as "not ignored", so a non-git
23
+ * project never produces a spurious advisory.
24
+ */
25
+ export function isDirGitIgnored(dir: string, cwd: string = process.cwd()): boolean | undefined {
26
+ try {
27
+ execFileSync('git', ['check-ignore', '-q', dir], {
28
+ cwd,
29
+ stdio: 'ignore',
30
+ timeout: 2000,
31
+ });
32
+ // exit 0 → the path IS ignored
33
+ return true;
34
+ } catch (err) {
35
+ // exit 1 → not ignored; anything else (no git, no repo) → unknown.
36
+ const code = (err as {status?: number}).status;
37
+ if (code === 1) return false;
38
+ return undefined;
39
+ }
40
+ }
41
+
42
+ /**
43
+ * The one-line advisory for an unignored scratch directory, or undefined when
44
+ * there is nothing to say.
45
+ *
46
+ * @param dir Directory as the user sees it, e.g. `.memory`.
47
+ * @param cwd Project root the check runs in.
48
+ */
49
+ export function unignoredDirNotice(dir: string, cwd: string = process.cwd()): string | undefined {
50
+ const ignored = isDirGitIgnored(dir, cwd);
51
+ if (ignored !== false) return undefined;
52
+ return (
53
+ `${dir} is not ignored by git — it will show up in git status and can be committed by accident. ` +
54
+ `Hide it machine-wide: printf '${dir}/\\n' >> ~/.config/git/ignore`
55
+ );
56
+ }
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,12 @@
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
+ import {unignoredDirNotice} from './gitignore.js';
22
23
  import type {MemorySettings} from './config.js';
23
24
  import {checkFreshness, listDocs, saveDoc, searchDocs} from './store.js';
24
25
  import type {MemoryDoc} from './store.js';
@@ -27,10 +28,44 @@ function settingsFor(cwd?: string): MemorySettings {
27
28
  return resolveMemorySettings(cwd);
28
29
  }
29
30
 
30
- /** Is there a `.memory` directory here at all? */
31
+ /**
32
+ * Is this repo using repo memory?
33
+ *
34
+ * Mirrors `@pal-code/workflow`: true when the memory directory exists, or when
35
+ * writing is enabled — because then `setup` can create it. Gating on the
36
+ * directory alone would make the extension permanently unavailable in exactly
37
+ * the repo where someone wants to start recording for the first time.
38
+ */
31
39
  function isAvailable(): boolean {
32
40
  const settings = settingsFor();
33
- return existsSync(settings.dir) || existsSync(`${settings.dir}/index.md`);
41
+ return existsSync(memoryRoot(settings)) || settings.write;
42
+ }
43
+
44
+ /**
45
+ * Create the memory directory on first load when writing is enabled.
46
+ *
47
+ * Without this, `write: true` alone still left the extension inert on a fresh
48
+ * repo: `available` was false because the directory did not exist, and nothing
49
+ * ever created it, so `memory_save` could not be reached to create it either.
50
+ * The same chicken-and-egg that `work_start` solves for `.work`.
51
+ *
52
+ * Deliberately does not seed an `index.md`: an empty directory already reads as
53
+ * "no knowledge recorded yet", and a stub would show up in the index as a doc
54
+ * that can never be fresh.
55
+ *
56
+ * Runs before `available` is read (see core's loader), so creating the
57
+ * directory here is what makes the getter above return true. Never throws — a
58
+ * read-only checkout must degrade to "unavailable", not to a failed extension.
59
+ */
60
+ function ensureMemoryRoot(): void {
61
+ const settings = settingsFor();
62
+ if (!settings.write) return;
63
+ try {
64
+ mkdirSync(memoryRoot(settings), {recursive: true});
65
+ } catch {
66
+ // Read-only filesystem, or a path that is not a directory. Leave it to
67
+ // isAvailable to report the extension as unavailable.
68
+ }
34
69
  }
35
70
 
36
71
  /**
@@ -274,5 +309,18 @@ export default {
274
309
  return isAvailable() ? [lookupDef, saveDef] : [];
275
310
  },
276
311
  prompt: PROMPT,
312
+ setup: ensureMemoryRoot,
313
+ /**
314
+ * Warn once at startup when the memory directory would be tracked by git.
315
+ *
316
+ * Deliberately not part of `setup()`: core reads `setupNotice` only for an
317
+ * available extension, and the answer is "nothing to say" in the common case
318
+ * (no `.memory` yet, or already ignored), so the check costs one `git` call.
319
+ */
320
+ setupNotice: () => {
321
+ const settings = settingsFor();
322
+ if (!existsSync(memoryRoot(settings))) return undefined;
323
+ return unignoredDirNotice(settings.dir);
324
+ },
277
325
  handlers,
278
326
  } satisfies ToolExtension;