stackmem 1.0.25 → 1.0.27

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.
@@ -0,0 +1,72 @@
1
+ import fs from "node:fs";
2
+ import path from "node:path";
3
+ export const STACKMEM_CONFIG_FILENAME = ".stackmem.json";
4
+ export const DEFAULT_STACKMEM_CONFIG = {
5
+ ignore: [],
6
+ tokenBudget: 2000,
7
+ decayLambda: 0.05,
8
+ captureTypes: ["decision", "constraint", "discovery", "rejection"],
9
+ minQualityScore: 0.3,
10
+ };
11
+ function isStringArray(value) {
12
+ return Array.isArray(value) && value.every((item) => typeof item === "string");
13
+ }
14
+ // Every field is independently optional — a partial or malformed
15
+ // .stackmem.json falls back to the default for whichever fields it got
16
+ // wrong, rather than failing the whole read.
17
+ function mergeWithDefaults(parsed) {
18
+ const raw = (parsed && typeof parsed === "object" ? parsed : {});
19
+ return {
20
+ ignore: isStringArray(raw.ignore) ? raw.ignore : DEFAULT_STACKMEM_CONFIG.ignore,
21
+ tokenBudget: typeof raw.tokenBudget === "number" && raw.tokenBudget > 0
22
+ ? raw.tokenBudget
23
+ : DEFAULT_STACKMEM_CONFIG.tokenBudget,
24
+ decayLambda: typeof raw.decayLambda === "number" && raw.decayLambda > 0
25
+ ? raw.decayLambda
26
+ : DEFAULT_STACKMEM_CONFIG.decayLambda,
27
+ captureTypes: isStringArray(raw.captureTypes) ? raw.captureTypes : DEFAULT_STACKMEM_CONFIG.captureTypes,
28
+ minQualityScore: typeof raw.minQualityScore === "number"
29
+ ? raw.minQualityScore
30
+ : DEFAULT_STACKMEM_CONFIG.minQualityScore,
31
+ };
32
+ }
33
+ // Reads .stackmem.json from `projectRoot` (defaulting to cwd). Missing
34
+ // file, unreadable file, or invalid JSON all fall back to defaults
35
+ // silently — a config file is an optimization, never a hard requirement.
36
+ export function readStackmemConfig(projectRoot = process.cwd()) {
37
+ const configPath = path.join(projectRoot, STACKMEM_CONFIG_FILENAME);
38
+ if (!fs.existsSync(configPath)) {
39
+ return { ...DEFAULT_STACKMEM_CONFIG };
40
+ }
41
+ try {
42
+ const parsed = JSON.parse(fs.readFileSync(configPath, "utf-8"));
43
+ return mergeWithDefaults(parsed);
44
+ }
45
+ catch {
46
+ return { ...DEFAULT_STACKMEM_CONFIG };
47
+ }
48
+ }
49
+ export function defaultConfigFileContents() {
50
+ return JSON.stringify(DEFAULT_STACKMEM_CONFIG, null, 2) + "\n";
51
+ }
52
+ // Minimal glob support for the `ignore` list: a trailing "/" matches a
53
+ // directory name anywhere in the path (like "dist/" already does
54
+ // elsewhere in the hook's own skip list); "*" matches any run of
55
+ // characters. Everything else is an exact match against the basename or
56
+ // full path. Deliberately not a real glob engine — just enough for the
57
+ // documented ignore patterns ("dist/", "migrations/", "*.generated.*").
58
+ export function matchesIgnorePattern(filePath, pattern) {
59
+ const baseName = filePath.split("/").pop() ?? filePath;
60
+ if (pattern.endsWith("/")) {
61
+ const dirName = pattern.slice(0, -1);
62
+ return filePath.split("/").includes(dirName);
63
+ }
64
+ if (pattern.includes("*")) {
65
+ const regex = new RegExp("^" + pattern.split("*").map((part) => part.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")).join(".*") + "$");
66
+ return regex.test(baseName) || regex.test(filePath);
67
+ }
68
+ return filePath === pattern || baseName === pattern;
69
+ }
70
+ export function isIgnoredByConfig(filePath, config) {
71
+ return config.ignore.some((pattern) => matchesIgnorePattern(filePath, pattern));
72
+ }
package/dist/index.js CHANGED
@@ -4,7 +4,7 @@ import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js"
4
4
  import { z } from "zod";
5
5
  import { analyzeCodebase, getFileSummary, findDependencies } from "./ast-index.js";
6
6
  import { getDeviceId } from "./device.js";
7
- import { applyContextBudget, buildSummaryContent, clusterMemories, computeDecayScore, computeMemoryLinks, decideSaveAction, estimateTokens, indicatesFailedApproach, isAuthRelatedFile, isCodeFile, isQualityMessage, } from "./scoring.js";
7
+ import { applyContextBudget, buildSummaryContent, clusterMemories, computeDecayScore, computeMemoryLinks, decideSaveAction, estimateTokens, indicatesFailedApproach, isAuthRelatedFile, isCodeFile, isQualityMessage, parseUtcDate, redactSecrets, } from "./scoring.js";
8
8
  import { supabase } from "./storage.js";
9
9
  const CONTEXT_BUDGET_TOKENS = 2000;
10
10
  const COMPRESSION_THRESHOLD = 20;
@@ -24,7 +24,8 @@ server.registerTool("session_start", {
24
24
  .from("memories")
25
25
  .select("*")
26
26
  .eq("project", project)
27
- .eq("resolved", false);
27
+ .eq("resolved", false)
28
+ .eq("suggested", false);
28
29
  if (error) {
29
30
  throw new Error(`Failed to load memories: ${error.message}`);
30
31
  }
@@ -32,7 +33,7 @@ server.registerTool("session_start", {
32
33
  const scoredMemories = memories
33
34
  .map((memory) => ({
34
35
  ...memory,
35
- decay_score: computeDecayScore(new Date(memory.created_at), memory.access_count ?? 0),
36
+ decay_score: computeDecayScore(parseUtcDate(memory.created_at), memory.access_count ?? 0, memory.pinned ?? false),
36
37
  }))
37
38
  .sort((a, b) => b.decay_score - a.decay_score);
38
39
  const budgetedMemories = applyContextBudget(scoredMemories, CONTEXT_BUDGET_TOKENS);
@@ -112,7 +113,8 @@ async function compressMemories(project) {
112
113
  .from("memories")
113
114
  .select("*")
114
115
  .eq("project", project)
115
- .eq("resolved", false);
116
+ .eq("resolved", false)
117
+ .eq("suggested", false);
116
118
  if (error) {
117
119
  throw new Error(`Failed to load memories for compression: ${error.message}`);
118
120
  }
@@ -162,6 +164,7 @@ server.registerTool("save_memory", {
162
164
  content: z.string().describe("The memory content to save"),
163
165
  },
164
166
  }, async ({ project, type, content }) => {
167
+ content = redactSecrets(content);
165
168
  const { data: existingRows, error: existingError } = await supabase
166
169
  .from("memories")
167
170
  .select("id, content")
@@ -193,7 +196,7 @@ server.registerTool("save_memory", {
193
196
  }
194
197
  const { data: inserted, error } = await supabase
195
198
  .from("memories")
196
- .insert({ project, type, content, device_id: getDeviceId() })
199
+ .insert({ project, type, content, device_id: getDeviceId(), source: "mcp" })
197
200
  .select("id")
198
201
  .single();
199
202
  if (error || !inserted) {
package/dist/scoring.js CHANGED
@@ -65,7 +65,9 @@ function trigramSet(text) {
65
65
  // similarity (typos, partial substrings, shared phrasing) that word-based
66
66
  // containment misses entirely, since containment only ever compares whole
67
67
  // extracted entities.
68
- function ngramOverlap(a, b) {
68
+ // Exported for direct unit testing (src/tests/scoring.test.ts) — hybridScore
69
+ // is the only production caller.
70
+ export function ngramOverlap(a, b) {
69
71
  const trigramsA = trigramSet(a);
70
72
  const trigramsB = trigramSet(b);
71
73
  const union = new Set([...trigramsA, ...trigramsB]);
@@ -114,7 +116,9 @@ const NEGATION_WORDS = [
114
116
  "switched", "removed", "replaced", "no longer", "instead",
115
117
  "deprecated", "reverted", "changed",
116
118
  ];
117
- function containsNegationWord(text) {
119
+ // Exported for direct unit testing (src/tests/scoring.test.ts) — decideSaveAction
120
+ // is the only production caller.
121
+ export function containsNegationWord(text) {
118
122
  const lower = text.toLowerCase();
119
123
  return NEGATION_WORDS.some((word) => lower.includes(word));
120
124
  }
@@ -159,13 +163,32 @@ export function decideSaveAction(newContent, existingMemories) {
159
163
  return { action: "created" };
160
164
  }
161
165
  // --- memory decay --------------------------------------------------------
166
+ // Supabase/PostgREST sometimes returns a timestamptz column without an
167
+ // explicit UTC suffix (e.g. "2026-10-26T00:36:16.36535" — no "Z" or
168
+ // offset — even though the underlying value is UTC). The bare `new
169
+ // Date(...)` constructor then parses that as LOCAL time, silently
170
+ // shifting every timestamp by the machine's UTC offset — several hours
171
+ // of error on every decay score, and a negative "N seconds ago" for
172
+ // anything created in roughly the last half-day. Normalize first.
173
+ export function parseUtcDate(dateString) {
174
+ const hasTimezone = /[Zz]$|[+-]\d{2}:?\d{2}$/.test(dateString);
175
+ return new Date(hasTimezone ? dateString : `${dateString}Z`);
176
+ }
162
177
  // Decay constant tuned for a ~14-day half-life: exp(-0.05 * 14) ≈ 0.5.
163
- const DECAY_LAMBDA = 0.05;
178
+ // Overridable per-project via .stackmem.json's decayLambda (src/config-file.ts).
179
+ export const DEFAULT_DECAY_LAMBDA = 0.05;
180
+ // A pinned memory always sorts first and never decays — 999 is well above
181
+ // any value the exponential formula below can produce, so ranking by this
182
+ // score alone puts every pinned memory ahead of every unpinned one.
183
+ const PINNED_SCORE = 999;
164
184
  // Recency-weighted relevance: frequently-accessed memories decay slower,
165
- // but every memory still fades over time regardless of access count.
166
- export function computeDecayScore(createdAt, accessCount) {
185
+ // but every memory still fades over time regardless of access count —
186
+ // unless pinned.
187
+ export function computeDecayScore(createdAt, accessCount, pinned = false, lambda = DEFAULT_DECAY_LAMBDA) {
188
+ if (pinned)
189
+ return PINNED_SCORE;
167
190
  const daysSinceCreated = (Date.now() - createdAt.getTime()) / (1000 * 60 * 60 * 24);
168
- return (1 + Math.log(1 + accessCount)) * Math.exp(-DECAY_LAMBDA * daysSinceCreated);
191
+ return (1 + Math.log(1 + accessCount)) * Math.exp(-lambda * daysSinceCreated);
169
192
  }
170
193
  // --- context budget ------------------------------------------------------
171
194
  // Rough token estimate: ~4 characters per token.
@@ -401,4 +424,26 @@ export function scoreMemoryRelevance(memoryContent, task) {
401
424
  const wordOverlapRatio = taskWords.length === 0 ? 0 : shared / taskWords.length;
402
425
  return structuralScore * 0.7 + wordOverlapRatio * 0.3;
403
426
  }
427
+ // --- task-aware ranking signals (branch name / PR title overlap) -----------
428
+ const TOKEN_SPLIT_RE = /[^a-zA-Z0-9]+/;
429
+ const MIN_TOKEN_LENGTH = 3;
430
+ // Branch names and PR titles use "/", "-", "_" as word separators (e.g.
431
+ // "feature/stripe-webhook-retry"), unlike prose content — splitWords above
432
+ // only splits on whitespace, so it can't tokenize these the same way.
433
+ export function tokenize(text) {
434
+ const tokens = new Set();
435
+ for (const word of text.toLowerCase().split(TOKEN_SPLIT_RE)) {
436
+ if (word.length >= MIN_TOKEN_LENGTH && !STOP_WORDS.has(word)) {
437
+ tokens.add(word);
438
+ }
439
+ }
440
+ return tokens;
441
+ }
442
+ export function hasTokenOverlap(a, b) {
443
+ for (const token of a) {
444
+ if (b.has(token))
445
+ return true;
446
+ }
447
+ return false;
448
+ }
404
449
  // TODO: optimize the clustering algorithm
package/dist/storage.js CHANGED
@@ -25,13 +25,91 @@
25
25
  import { createClient } from "@supabase/supabase-js";
26
26
  import { SUPABASE_ANON_KEY, SUPABASE_URL } from "./config.js";
27
27
  import { getDeviceId } from "./device.js";
28
- export const supabase = createClient(SUPABASE_URL, SUPABASE_ANON_KEY, {
28
+ const rawSupabase = createClient(SUPABASE_URL, SUPABASE_ANON_KEY, {
29
29
  global: {
30
30
  headers: {
31
31
  "x-device-id": getDeviceId(),
32
32
  },
33
33
  },
34
34
  });
35
+ function sleep(ms) {
36
+ return new Promise((resolve) => setTimeout(resolve, ms));
37
+ }
38
+ // maxAttempts counts RETRIES, not total tries — the default (3 retries,
39
+ // 500ms base) produces exactly the 500ms / 1000ms / 2000ms backoff
40
+ // sequence across 4 total tries (1 initial + 3 retries).
41
+ export async function withRetry(fn, maxAttempts = 3, baseDelayMs = 500) {
42
+ let lastError;
43
+ for (let attempt = 0; attempt <= maxAttempts; attempt++) {
44
+ try {
45
+ return await fn();
46
+ }
47
+ catch (error) {
48
+ lastError = error;
49
+ if (attempt === maxAttempts)
50
+ break;
51
+ const delayMs = baseDelayMs * 2 ** attempt;
52
+ const message = error instanceof Error ? error.message : String(error);
53
+ console.error(`Supabase request failed (retry ${attempt + 1}/${maxAttempts} in ${delayMs}ms): ${message}`);
54
+ await sleep(delayMs);
55
+ }
56
+ }
57
+ throw lastError;
58
+ }
59
+ // A 4xx response (bad request, not found, RLS denial) is a logic error —
60
+ // retrying it wastes time and can't succeed. status 0 (the fetch itself
61
+ // failed: DNS, connection refused, timeout) and 5xx (transient server
62
+ // errors) are the cases retrying can actually help with.
63
+ function isRetryableStatus(status) {
64
+ return status === 0 || status >= 500;
65
+ }
66
+ // Supabase/postgrest query results RESOLVE with { data, error, status }
67
+ // even on failure — they don't reject — so a plain try/catch around the
68
+ // call never sees a 5xx or network failure to retry. This wraps every
69
+ // query builder returned from `.from(...)` so that a retryable result
70
+ // is converted into a throw (for withRetry's catch to see), while a
71
+ // success or a non-retryable 4xx result still resolves normally, exactly
72
+ // as every existing `const { data, error } = await supabase.from(...)...`
73
+ // call site already expects — no call site elsewhere needs to change.
74
+ function wrapWithRetry(target) {
75
+ if (target === null || typeof target !== "object")
76
+ return target;
77
+ return new Proxy(target, {
78
+ get(obj, prop, receiver) {
79
+ if (prop === "then") {
80
+ return (onFulfilled, onRejected) => {
81
+ const attempt = () => new Promise((resolve, reject) => {
82
+ obj.then((result) => {
83
+ if (result && result.error && isRetryableStatus(result.status)) {
84
+ const err = new Error(`Supabase request failed (status ${result.status}): ${result.error.message}`);
85
+ err.status = result.status;
86
+ err.supabaseError = result.error;
87
+ reject(err);
88
+ }
89
+ else {
90
+ resolve(result);
91
+ }
92
+ }, (err) => reject(err));
93
+ });
94
+ return withRetry(attempt).then(onFulfilled, onRejected);
95
+ };
96
+ }
97
+ const value = Reflect.get(obj, prop, receiver);
98
+ if (typeof value === "function") {
99
+ return (...args) => wrapWithRetry(value.apply(obj, args));
100
+ }
101
+ return value;
102
+ },
103
+ });
104
+ }
105
+ export const supabase = new Proxy(rawSupabase, {
106
+ get(obj, prop, receiver) {
107
+ if (prop === "from") {
108
+ return (...args) => wrapWithRetry(obj.from(...args));
109
+ }
110
+ return Reflect.get(obj, prop, receiver);
111
+ },
112
+ });
35
113
  // Run this migration in the Supabase SQL editor to add the access_count
36
114
  // column used by the memory decay scoring in src/scoring.ts (computeDecayScore)
37
115
  // and consumed by cm start / session_start in src/cli.ts and src/index.ts.
@@ -0,0 +1,93 @@
1
+ // CRUD-behavior tests. Tests 1-3 are pure (decideSaveAction, no network).
2
+ // Tests 4-6 exercise the real Supabase backend via the compiled CLI,
3
+ // matching src/tests/regression.test.ts's convention — using a dedicated
4
+ // throwaway project cleaned up in `after()` regardless of pass/fail.
5
+ import { test, after } from "node:test";
6
+ import assert from "node:assert/strict";
7
+ import { execFileSync } from "node:child_process";
8
+ import path from "node:path";
9
+ import { decideSaveAction } from "../scoring.js";
10
+ import { supabase } from "../storage.js";
11
+ const CLI_PATH = path.join(import.meta.dirname, "..", "..", "dist", "cli.js");
12
+ const CRUD_TEST_PROJECT = "crud-test-temp";
13
+ function runCli(args) {
14
+ return execFileSync(process.execPath, [CLI_PATH, ...args], { encoding: "utf-8" });
15
+ }
16
+ // 1. decideSaveAction returns "duplicate" ("skip", in the task's wording)
17
+ // for near-identical content.
18
+ test('decideSaveAction returns "duplicate" for near-identical content', () => {
19
+ const existing = [{ id: "mem-1", content: "add stripe webhook signature verification" }];
20
+ const decision = decideSaveAction("add stripe webhook signature verification", existing);
21
+ assert.equal(decision.action, "duplicate");
22
+ });
23
+ // 2. decideSaveAction returns "superseded" ("replace", in the task's
24
+ // wording) for contradicting content.
25
+ test('decideSaveAction returns "superseded" for contradicting content', () => {
26
+ const existing = [{ id: "mem-1", content: "use redis for session storage" }];
27
+ const decision = decideSaveAction("we switched from redis to postgres for session storage", existing);
28
+ assert.equal(decision.action, "superseded");
29
+ });
30
+ // 3. decideSaveAction returns "created" ("create", in the task's wording)
31
+ // for genuinely new content.
32
+ test('decideSaveAction returns "created" for genuinely new content', () => {
33
+ const existing = [{ id: "mem-1", content: "use redis for session storage" }];
34
+ const decision = decideSaveAction("add rate limiting to the public api gateway", existing);
35
+ assert.equal(decision.action, "created");
36
+ });
37
+ after(async () => {
38
+ const { data: mem } = await supabase.from("memories").select("id").eq("project", CRUD_TEST_PROJECT);
39
+ const ids = (mem ?? []).map((m) => m.id);
40
+ if (ids.length > 0) {
41
+ const idList = ids.join(",");
42
+ await supabase.from("memory_links").delete().or(`source_id.in.(${idList}),target_id.in.(${idList})`);
43
+ }
44
+ await supabase.from("memories").delete().eq("project", CRUD_TEST_PROJECT);
45
+ });
46
+ // 4. A duplicate commit_sha is recognized by the check-commit guard
47
+ // (src/cli.ts cmdCheckCommit) rather than producing a second memory for
48
+ // the same commit — this is the actual mechanism behind "saving a memory
49
+ // with a duplicate commit_sha doesn't create a new one": the hook calls
50
+ // `cm check-commit` once per invocation and skips its entire capture
51
+ // pass if it reports the commit_sha already has a memory, rather than
52
+ // cmdSave de-duplicating per individual save. Exit code is inverted:
53
+ // 0 = not seen yet (proceed), 1 = already has a memory (duplicate, skip).
54
+ test("check-commit: fresh sha allowed, then blocked once a memory exists for it", () => {
55
+ const sha = `test-sha-${Date.now()}-fresh`;
56
+ // No memory for this sha yet — check-commit should exit 0 (allowed).
57
+ runCli(["check-commit", CRUD_TEST_PROJECT, sha]);
58
+ // Save a memory tagged with that sha.
59
+ runCli(["save", CRUD_TEST_PROJECT, "decision", "duplicate commit_sha guard test", "--force", "--commit-sha", sha]);
60
+ // Now the same sha must be blocked (non-zero exit = duplicate).
61
+ assert.throws(() => runCli(["check-commit", CRUD_TEST_PROJECT, sha]));
62
+ });
63
+ // 5. cm pin sets pinned=true.
64
+ test("cm pin sets pinned=true on the target memory", async () => {
65
+ runCli(["save", CRUD_TEST_PROJECT, "decision", "memory to be pinned in crud test", "--force"]);
66
+ const { data } = await supabase
67
+ .from("memories")
68
+ .select("id")
69
+ .eq("project", CRUD_TEST_PROJECT)
70
+ .eq("content", "memory to be pinned in crud test")
71
+ .single();
72
+ assert.ok(data);
73
+ runCli(["pin", CRUD_TEST_PROJECT, data.id]);
74
+ const { data: after_ } = await supabase.from("memories").select("pinned").eq("id", data.id).single();
75
+ assert.ok(after_);
76
+ assert.equal(after_.pinned, true);
77
+ });
78
+ // 6. cm unpin sets pinned=false.
79
+ test("cm unpin sets pinned=false on the target memory", async () => {
80
+ runCli(["save", CRUD_TEST_PROJECT, "decision", "memory to be unpinned in crud test", "--force"]);
81
+ const { data } = await supabase
82
+ .from("memories")
83
+ .select("id")
84
+ .eq("project", CRUD_TEST_PROJECT)
85
+ .eq("content", "memory to be unpinned in crud test")
86
+ .single();
87
+ assert.ok(data);
88
+ runCli(["pin", CRUD_TEST_PROJECT, data.id]);
89
+ runCli(["unpin", CRUD_TEST_PROJECT, data.id]);
90
+ const { data: after_ } = await supabase.from("memories").select("pinned").eq("id", data.id).single();
91
+ assert.ok(after_);
92
+ assert.equal(after_.pinned, false);
93
+ });