@pal-code/memory 0.0.19 → 0.0.21

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
@@ -109,6 +109,18 @@ printf '.memory/\n' >> ~/.config/git/ignore
109
109
  git config --global core.excludesFile ~/.config/git/ignore
110
110
  ```
111
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
+
112
124
  `ripgrep` and `biome` inherit this through git, so nothing lints or greps it either.
113
125
 
114
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.19",
3
+ "version": "0.0.21",
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.19"
17
+ "@pal-code/core": "0.0.21"
18
18
  },
19
19
  "engines": {
20
20
  "bun": ">=1.1.0"
@@ -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.ts CHANGED
@@ -19,6 +19,7 @@ import {existsSync, mkdirSync} from 'node:fs';
19
19
  import type {ToolDef, ToolExtension, ToolHandler} from '@pal-code/core';
20
20
 
21
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';
@@ -309,5 +310,17 @@ export default {
309
310
  },
310
311
  prompt: PROMPT,
311
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
+ },
312
325
  handlers,
313
326
  } satisfies ToolExtension;