simple-memory 0.0.0-stage → 0.6.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/LICENSE +21 -0
- package/README.md +315 -2
- package/bin/sm.js +90 -0
- package/lib/platform.js +120 -0
- package/lib/update.js +200 -0
- package/package.json +41 -4
- package/scripts/check-update.js +104 -0
- package/scripts/postinstall.js +442 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 simple-memory contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,316 @@
|
|
|
1
|
-
#
|
|
1
|
+
# simple-memory (`sm`)
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
[](LICENSE)
|
|
4
|
+
[](rust/rust-toolchain.toml)
|
|
5
|
+
[](DESIGN.md)
|
|
6
|
+
[](#mcp-server-sm-mcp)
|
|
7
|
+
|
|
8
|
+
A single-binary SQLite + FTS5 memory store, prompt hook, and MCP server for coding agents.
|
|
9
|
+
|
|
10
|
+
Store verified facts and architectural decisions from the shell with no embeddings or background daemons. Inject matching context automatically via prompt hooks in Claude Code and `agy` (Antigravity CLI), or query and write on demand from any MCP client.
|
|
11
|
+
|
|
12
|
+

|
|
13
|
+
|
|
14
|
+
## Why simple-memory
|
|
15
|
+
|
|
16
|
+
- **One local SQLite file**: No external database, daemon, or embedding service; everything stays in `~/.simple-memory/memory.db` (WAL mode, bundled SQLite).
|
|
17
|
+
- **Evidence-tagged records**: Items store an evidence tag, kind, local timestamp, and extracted references (tickets, commits, paths).
|
|
18
|
+
- **Signal-first default ranking**: Exact references rank first, then exact phrases, accented matches, and weighted BM25 biased toward durable verified/decided records.
|
|
19
|
+
- **Dual folded and raw FTS5 search**: Folds accents (`dorde` finds `Đorđe`, `cafe` finds `café`) while scoring exact accents higher and prefix-matching 4+ letter ASCII words.
|
|
20
|
+
- **Automatic catch-up without cron**: The prompt hook triggers one background pass at most every 10 minutes to rotate checked backups (every 24 h, keep 7), import task logs/handoffs/memory files, and summarize idle sessions.
|
|
21
|
+
|
|
22
|
+
## Install
|
|
23
|
+
|
|
24
|
+
Install globally via npm (Node.js 18+) and initialize `~/.simple-memory/`:
|
|
25
|
+
|
|
26
|
+
```sh
|
|
27
|
+
npm install -g simple-memory
|
|
28
|
+
sm init
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
You can also run one-off commands without a global install:
|
|
32
|
+
|
|
33
|
+
```sh
|
|
34
|
+
npx simple-memory --help
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
Prebuilt native Rust binaries ship through npm optional dependencies for macOS (`arm64` via `simple-memory-darwin-arm64`, `x64` via `simple-memory-darwin-x64`). On Linux (`x64` and `arm64`), `postinstall` falls back to GitHub Release tarballs when published; otherwise install from source with `./scripts/install.sh` below.
|
|
38
|
+
|
|
39
|
+
Install globally with `npm install -g simple-memory` (not `npx`) before running `sm hooks print`, so hook and MCP configs point to a stable path. Make sure `node` is on the `PATH` seen by your hook and MCP hosts, or install the standalone native binary via `./scripts/install.sh` for minimal-`PATH` GUI hosts.
|
|
40
|
+
|
|
41
|
+
### Upgrading
|
|
42
|
+
|
|
43
|
+
Check for a newer release or upgrade with `sm update`:
|
|
44
|
+
|
|
45
|
+
```sh
|
|
46
|
+
sm update --check # check npm for a newer version and refresh the local cache
|
|
47
|
+
sm update # run npm install -g simple-memory@latest for npm installs, or print the source-upgrade command
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
Interactive npm CLI runs also read `$SIMPLE_MEMORY_HOME/update-check.json` (refreshed in the background at most once every 24 hours) and print a one-line notice to stderr when a newer release exists. The reminder stays silent on non-TTY streams, CI, `mcp`, `hook`/`hooks`, `update`, `--format json`, `--version`/`-v`, `--help`/`-h`, or when `SM_NO_UPDATE_CHECK=1` is set.
|
|
51
|
+
|
|
52
|
+
### Build from source (Linux or custom Rust build)
|
|
53
|
+
|
|
54
|
+
Requires [Rust (`rustup`)](https://rustup.rs/). `rust/rust-toolchain.toml` pins Rust `1.99.1`. Clone your repository copy and run:
|
|
55
|
+
|
|
56
|
+
```sh
|
|
57
|
+
git clone <repo-url>
|
|
58
|
+
cd simple-memory
|
|
59
|
+
./scripts/install.sh
|
|
60
|
+
export PATH="$HOME/.local/bin:$PATH" # add to your shell profile if needed
|
|
61
|
+
sm init
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
`./scripts/install.sh` builds the release binary from `rust/`, atomically installs `sm` (plus a `simple-memory` symlink) to `SM_INSTALL_DIR` (default `~/.local/bin`), honors `CARGO_TARGET_DIR`, and verifies `sm --version`.
|
|
65
|
+
|
|
66
|
+
<details>
|
|
67
|
+
<summary>Manual install and atomic replacement</summary>
|
|
68
|
+
|
|
69
|
+
Build and replace via a temporary sibling path + `mv -f` rather than overwriting a running binary in place or running `ln -sf`:
|
|
70
|
+
|
|
71
|
+
```sh
|
|
72
|
+
cd rust && cargo build --release --locked
|
|
73
|
+
mkdir -p ~/.local/bin
|
|
74
|
+
cp target/release/sm ~/.local/bin/sm.new && mv -f ~/.local/bin/sm.new ~/.local/bin/sm
|
|
75
|
+
ln -s sm ~/.local/bin/simple-memory.new && mv -f ~/.local/bin/simple-memory.new ~/.local/bin/simple-memory
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
`ln -sf` unlinks the old symlink before relinking (leaving a brief window where a firing hook finds no `sm`), and copying over a mapped binary in place on macOS can break its code signature.
|
|
79
|
+
|
|
80
|
+
</details>
|
|
81
|
+
|
|
82
|
+
## 60-second quickstart
|
|
83
|
+
|
|
84
|
+
Inside any git repository, `sm` automatically scopes writes and queries to that repository (plus `global` and `unassigned` items):
|
|
85
|
+
|
|
86
|
+
```sh
|
|
87
|
+
sm put --kind decision --tag decided --evidence "ADR-004" --refs ADR-004 \
|
|
88
|
+
"Limit HTTP connection pool to 16 sockets per worker"
|
|
89
|
+
# m_01M7R350TK1Z1DKWVWT4NSF8MQ
|
|
90
|
+
|
|
91
|
+
sm put --kind fact --tag verified --evidence "loadtest 10m run, 0 pool waits" --refs ADR-004 \
|
|
92
|
+
"Pool size 16 keeps p99 request latency under 12 ms at peak load"
|
|
93
|
+
# m_01M7R350W6QV508RDWBHA9S45W
|
|
94
|
+
|
|
95
|
+
sm query "connection pool ADR-004"
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
```text
|
|
99
|
+
[m_01M7R350TK1Z1DKWVWT4NSF8MQ] #1 repo=my-repo kind=decision tag=decided as_of=2026-11-18T19:46:44+00:00 ~26 tok why=terms:4/4,bias:decision,bias:decided,recent refs=ADR-004
|
|
100
|
+
Limit HTTP connection pool to 16 sockets per worker
|
|
101
|
+
[m_01M7R350W6QV508RDWBHA9S45W] #2 repo=my-repo kind=fact tag=verified as_of=2026-11-18T19:46:44+00:00 ~32 tok why=terms:3/4,bias:fact,bias:verified,recent refs=ADR-004
|
|
102
|
+
Pool size 16 keeps p99 request latency under 12 ms at peak load
|
|
103
|
+
next: sm show m_01M7R350TK1Z1DKWVWT4NSF8MQ m_01M7R350W6QV508RDWBHA9S45W; sm related m_01M7R350TK1Z1DKWVWT4NSF8MQ
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
`sm query` (alias `sm search`) also accepts unquoted multi-word terms, `-term` or `-"exact phrase"` exclusions (quote the query or use `--` when starting with `-` or excluding multi-word phrases in a shell), and relative `--since` / `--until` bounds (`30m`, `24h`, `7d`, `2w`):
|
|
107
|
+
|
|
108
|
+
```sh
|
|
109
|
+
sm search connection pool -staging --since 7d
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
Open full body and provenance with `sm show` (or `sm show --format json`), update metadata or text append-only with `sm amend`, retire obsolete items with `sm retire`, or replace outdated facts with `--supersedes` (`sm supersede OLD NEW`):
|
|
113
|
+
|
|
114
|
+
```sh
|
|
115
|
+
sm show m_01M7R350TK1Z1DKWVWT4NSF8MQ
|
|
116
|
+
sm amend m_01M7R350TK1Z1DKWVWT4NSF8MQ --add-refs ADR-009
|
|
117
|
+
# m_01M7R3512K8Q9V4X7B2N6M3P1R
|
|
118
|
+
sm retire m_01M7R350W6QV508RDWBHA9S45W --reason "replaced by new benchmark"
|
|
119
|
+
sm put --kind decision --tag decided --supersedes m_01M7R3512K8Q9V4X7B2N6M3P1R \
|
|
120
|
+
--evidence "ADR-009" --refs ADR-009 \
|
|
121
|
+
"Raise HTTP connection pool to 24 sockets per worker after read-replica split"
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
`sm put` refuses writes without `--tag`, and requires `--evidence` for `executed` and `verified` items. Duplicate active bodies in the same repo return the existing item ID. When a new item overlaps active work items (`task`, `handoff`) from the last 24 hours or active durable items (`fact`, `decision`, `learning`) in the same repo, `sm put` prints a warning to stderr.
|
|
125
|
+
|
|
126
|
+
## Agent integrations
|
|
127
|
+
|
|
128
|
+
Print ready-to-merge JSON for each harness (`sm hooks print` writes no files):
|
|
129
|
+
|
|
130
|
+
| Harness | Command | Merge target |
|
|
131
|
+
|---|---|---|
|
|
132
|
+
| Claude Code | `sm hooks print claude-code` | `"hooks"` in `~/.claude/settings.json` |
|
|
133
|
+
| Antigravity CLI (`agy`) | `sm hooks print agy` | `~/.gemini/config/hooks.json` |
|
|
134
|
+
| Any MCP client | `sm hooks print mcp` | `"mcpServers"` in `.mcp.json` or client MCP config |
|
|
135
|
+
|
|
136
|
+
Example output of `sm hooks print claude-code`:
|
|
137
|
+
|
|
138
|
+
```json
|
|
139
|
+
{
|
|
140
|
+
"hooks": {
|
|
141
|
+
"UserPromptSubmit": [
|
|
142
|
+
{
|
|
143
|
+
"hooks": [
|
|
144
|
+
{
|
|
145
|
+
"type": "command",
|
|
146
|
+
"command": "/path/to/.local/bin/sm hook prompt",
|
|
147
|
+
"timeout": 15
|
|
148
|
+
}
|
|
149
|
+
]
|
|
150
|
+
}
|
|
151
|
+
]
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
### How the prompt hook works
|
|
157
|
+
|
|
158
|
+
1. Skips slash commands, machine-wrapped notifications, and prompts under 3 words.
|
|
159
|
+
2. Uses `claude -p` (Haiku, with hooks, tools, and MCP servers off) to turn the prompt into keywords or `NONE`. Set `[prompt_hook] model = "none"` for offline IDF local-keyword search with zero model calls.
|
|
160
|
+
3. Falls back to local-keyword search on missing `claude`, error, or timeout (with a 10-minute cooldown after two consecutive failures that clears automatically when `claude_bin`, `model`, or `timeout` changes), skipping built-in English stopwords plus `[prompt_hook] extra_stopwords`.
|
|
161
|
+
4. Injects at most 4 short lines carrying repo, kind, evidence tag, date, compact refs, and title (including `--in <scopes>` in the follow-up hint when project scopes match), and tracks shown IDs per session (per compaction epoch in Claude Code, per `conversationId` in `agy`). When the prompt or rewritten query names a configured `[prompt_hook.projects]` code mapped in `[scopes]`, those scope matchers union additively with the current repo and apply automatic `scope:<path>` bias (`+0.3` via `BIAS_BOOST`) on both the Haiku and local paths so cross-repo project notes surface without dropping local matches (opt out of automatic scope bias with `[query] default_bias = ["relevance"]`). On scopes with at least 20 active items, the Haiku path drops low-information hits whose matched-term IDF sum falls below `[prompt_hook] min_idf` (default `3.0`; `0.0` disables via `run_query`; exact ref hits bypass; dropped IDs log under `floor_dropped`). The local fallback applies `[prompt_hook] local_min_idf` (default `8.0`; `0.0` disables) as its local IDF floor. Disable per invocation with `SM_PROMPT_HOOK=0`.
|
|
162
|
+
|
|
163
|
+
### MCP server (`sm mcp`)
|
|
164
|
+
|
|
165
|
+
Speaks JSON-RPC 2.0 over stdio (`MCP 2026-07-28` stateless metadata and `2024-11-05` through `2025-11-25` `initialize` handshakes) and serves eight tools:
|
|
166
|
+
|
|
167
|
+
| Tool | Arguments | Purpose |
|
|
168
|
+
|---|---|---|
|
|
169
|
+
| `memory_query` | `query`; optional `bias`, `kind`, `tag`, `source`, `since`, `until`, `recent`, `include_history`, `in`, `limit`, `all_repos`, `repo` | Ranked search (`sm query --format json`); `bias` accepts `scope:<path>` |
|
|
170
|
+
| `memory_list` | optional `limit`, `offset`, `kind`, `tag`, `source`, `since`, `until`, `status`, `include_history`, `all_repos`, `repo`, `in` | Browse items newest-first without a query |
|
|
171
|
+
| `memory_show` | `ids` (non-empty array) | Full body, refs, and provenance per ID |
|
|
172
|
+
| `memory_related` | `id`; optional `limit` | Supersede chain, shared refs, same session |
|
|
173
|
+
| `memory_put` | `text`, `tag`; optional `kind`, `evidence`, `refs`, `supersedes`, `repo` | Store item, redact secrets, warn on overlaps |
|
|
174
|
+
| `memory_amend` | `id`; optional `text`, `title`, `kind`, `tag`, `evidence`, `repo`, `add_refs`, `remove_refs`, `refs`, `add_tags`, `remove_tags`, `tags`, `as_of` | Append-only update (creates a new item and supersedes `id`) |
|
|
175
|
+
| `memory_retire` | `id`; optional `reason` | Soft-retire an active item with no replacement |
|
|
176
|
+
| `memory_supersede` | `old_id`, `new_id` | Retire `old_id` in favor of `new_id` |
|
|
177
|
+
|
|
178
|
+
## Ontology and retrieval gate
|
|
179
|
+
|
|
180
|
+
`simple-memory` organizes notes in a two-layer ontology:
|
|
181
|
+
|
|
182
|
+
- **Layer 1 (SQLite `items` columns)**: every item stores `kind` (`fact`, `decision`, `learning`, `episode`, `task`, `handoff`), evidence `tag` (`executed`, `verified`, `decided`, `client-stated`, `derived`, `assumed`, `prior`, `planned`), `repo`, extracted `refs`, and `source` provenance.
|
|
183
|
+
- **Layer 2 (config `[scopes]` tree)**: `~/.simple-memory/config.toml` groups repos and case-sensitive whole-word project codes (such as `"GLX"`) into a slash-separated hierarchy (company, client, project levels) with automatic ancestor roll-up.
|
|
184
|
+
|
|
185
|
+
Both prompt-hook recall (`sm hook prompt`) and explicit lookup (`sm query` / `sm mcp`) converge on the same SQL filter, FTS5 + ref candidate pool, and rank/bias gate:
|
|
186
|
+
|
|
187
|
+

|
|
188
|
+
|
|
189
|
+
- **Hard scope filter (`--in`)**: `sm query "auth timeout" --in acme/globex` searches across all repos and keeps only items matching `acme/globex` or its descendants.
|
|
190
|
+
- **Soft scope bias (`--bias scope:<path>`)**: `sm query "auth timeout" --bias scope:acme/globex` (or MCP `memory_query` `bias`, or `[query] default_bias`) keeps the normal repo result set, widens the FTS candidate pool to `100`, and adds `+0.3` (`BIAS_BOOST`) per matched scope without overriding exact `ref_hits`, `phrase`, or `accent` matches (reported as `why=...,bias:scope:acme/globex`). When `--bias` contains only `scope:<path>` tokens, they merge additively on top of `[query] default_bias` so durable kind, tag, and recency boosts stay active; pass `relevance` or another explicit kind/tag token (such as `--bias relevance,scope:acme/globex`) to replace the defaults. Stack parent and child paths (`--bias scope:acme,scope:acme/globex`) for hierarchical specificity.
|
|
191
|
+
- **Automatic hook scope bias**: When `sm hook prompt` matches a `[prompt_hook.projects]` code to `[scopes]`, matched paths union additively into SQL and apply automatic `scope:<path>` bias on both the Haiku and local paths. Set `[query] default_bias = ["relevance"]` to opt out of automatic scope bias (along with recency and kind/tag bias) while keeping additive SQL scope inclusion.
|
|
192
|
+
|
|
193
|
+
## Multi-repo scopes and maintenance
|
|
194
|
+
|
|
195
|
+
Group related repos or project codes locally in `~/.simple-memory/config.toml` under `[scopes]`:
|
|
196
|
+
|
|
197
|
+
```toml
|
|
198
|
+
[scopes]
|
|
199
|
+
"acme" = ["repo:acme-internal"]
|
|
200
|
+
"acme/globex" = ["GLX"]
|
|
201
|
+
"acme/globex/portal" = ["repo:globex-portal"]
|
|
202
|
+
```
|
|
203
|
+
|
|
204
|
+
Filter hard across scopes with `sm query "auth timeout" --in acme/globex`, soft-boost a scope without filtering via `sm query "auth timeout" --bias scope:acme/globex`, and inspect active, superseded, and retired store counts, recall open rates, catch-up runs, and scope totals with `sm stats` (`--days N`, `--format json`).
|
|
205
|
+
|
|
206
|
+
Check store health anytime:
|
|
207
|
+
|
|
208
|
+
```sh
|
|
209
|
+
sm doctor --imports # check DB integrity and list unimported task logs, handoffs, memory files
|
|
210
|
+
sm upkeep --report # read-only report of near-duplicates and stale planned/assumed items
|
|
211
|
+
sm backup # verified SQLite backup copy in ~/.simple-memory/backups/
|
|
212
|
+
```
|
|
213
|
+
|
|
214
|
+
## Extending simple-memory
|
|
215
|
+
|
|
216
|
+
Modules follow a downward architecture (`cli` -> `service` -> `store` / `sources` / `config` -> `core`, plus shared `py`, `clock`, `error`), with three explicit helper edges (`rust/src/config.rs` -> `service::capture`, `rust/src/config.rs` -> `service::query::LOCAL_TAU`, and `rust/src/config.rs` -> `sources`; `stands_as_word` lives in `rust/src/core/terms.rs`):
|
|
217
|
+
|
|
218
|
+
- **Custom stopwords and ranking defaults**: Set `[prompt_hook] extra_stopwords` for domain/language filler skipped by the local fallback, and `[query] default_bias` to override default ranking without recompiling.
|
|
219
|
+
- **New import source**: Add a parser under `rust/src/sources/` returning `core::item::Item` records, register its subcommand in `rust/src/cli/args.rs` and `rust/src/service/import_.rs`, and wire it into `rust/src/service/catchup.rs` + `rust/src/service/doctor.rs` (`--imports`).
|
|
220
|
+
- **New MCP tool or schema field**: Add tool schemas and handlers in `rust/src/service/mcp.rs` over shared `service/*` calls; place numbered `.sql` migrations under `rust/migrations/` and register them in `rust/src/store/connect.rs`.
|
|
221
|
+
- **Additional redaction patterns**: Extend `rust/src/core/redact.rs` with linear-time regex patterns and unit tests.
|
|
222
|
+
|
|
223
|
+
<details>
|
|
224
|
+
<summary>CLI command reference</summary>
|
|
225
|
+
|
|
226
|
+
| Command | Summary |
|
|
227
|
+
|---|---|
|
|
228
|
+
| `sm init` | Create home dir, `memory.db`, and commented `config.toml` |
|
|
229
|
+
| `sm config` | Print effective configuration (defaults plus `config.toml`) as TOML |
|
|
230
|
+
| `sm put TEXT --tag TAG [...]` | Store item (`--kind`, `--evidence`, `--refs`, `--tags`, `--supersedes`, `--repo`, `--as-of`) |
|
|
231
|
+
| `sm query TEXT [...]` (alias `sm search`) | Ranked search with unquoted terms, `-term`/`-"phrase"` exclusions, relative `--since`/`--until` (`30m`, `24h`, `7d`, `2w`), additive `--bias scope:<path>`, `--limit`, `--recent`, `--kind`, `--tag`, `--source`, `--in` (mutually exclusive with `--repo`), `--all`, `--include-history`, `--format` |
|
|
232
|
+
| `sm show ID [ID ...] [--format text\|json]` | Print full text and provenance (`text` or `json` array); exits 1 if any ID is missing |
|
|
233
|
+
| `sm related ID [...]` | Show supersede chain, shared-ref items, and same-session items |
|
|
234
|
+
| `sm supersede OLD NEW` | Mark `OLD` superseded by `NEW` |
|
|
235
|
+
| `sm retire ID [--reason TEXT]` | Soft-retire an active item with no replacement (default reason: `manual`) |
|
|
236
|
+
| `sm amend ID [...]` | Append-only update: create a new item with changed fields and supersede `ID` atomically |
|
|
237
|
+
| `sm list [...]` | Browse items newest-first without a query (`--limit`, `--offset`, `--repo \| --all \| --in`, `--kind`, `--tag`, `--source`, `--since`, `--until`, `--status \| --include-history`, `--format`) |
|
|
238
|
+
| `sm import tasklog\|handoff\|legacy\|proposal PATH` | Import YAML task logs, Markdown handoffs, legacy DBs, or proposal JSONL (`--apply`, `--dry-run`) |
|
|
239
|
+
| `sm import automemory \| sessions \| jsonl \| catch-up` | Sync Claude memory files, summarize idle sessions, import JSONL, or run full catch-up |
|
|
240
|
+
| `sm export [--include-history] [--repo R \| --all]` | Dump items sorted by ID as JSONL to stdout |
|
|
241
|
+
| `sm upkeep [--dry-run] [--report]` | Retire older/claimed session handoffs; `--report` flags near-duplicates and stale items |
|
|
242
|
+
| `sm backup [--keep N]` | Checked SQLite backup in `backups/` |
|
|
243
|
+
| `sm doctor [--rebuild-fts] [--backfill-refs] [--imports]` | Verify SQLite integrity, rebuild FTS5, backfill refs, or audit unimported files |
|
|
244
|
+
| `sm stats [--days N] [--format text\|json]` | Store counts (active, superseded, retired), recall open rate, catch-up runs, backups, and `[scopes]` rollup |
|
|
245
|
+
| `sm hook prompt [--agy]` | Harness hook reading prompt JSON on stdin |
|
|
246
|
+
| `sm hooks print claude-code\|agy\|mcp` | Print harness configuration JSON to stdout |
|
|
247
|
+
| `sm mcp` | Run JSON-RPC 2.0 MCP server on stdio |
|
|
248
|
+
| `sm update [--check]` | Check npm for a newer release and upgrade npm installs (or print the source-upgrade command) |
|
|
249
|
+
| `sm --version`, `sm -v` | Print installed binary version |
|
|
250
|
+
|
|
251
|
+
</details>
|
|
252
|
+
|
|
253
|
+
<details>
|
|
254
|
+
<summary>Configuration and environment variables</summary>
|
|
255
|
+
|
|
256
|
+
`sm init` writes a commented template to `~/.simple-memory/config.toml` and never overwrites an existing file:
|
|
257
|
+
|
|
258
|
+
- `[repos] roots`: Parent directories whose direct children map workspace/transcript folders to repo keys (`[repo_aliases]` maps aliases such as worktrees).
|
|
259
|
+
- `[keyword_aliases]`: Query term expansions (e.g. `"k8s" = "kubernetes"`).
|
|
260
|
+
- `[query] default_bias`: Default is `["recency", "fact", "decision", "learning", "executed", "verified", "decided", "client-stated"]`. Accepts the same tokens as `--bias`, including `scope:<path>` (for example `"scope:acme/globex"`). Explicit `--bias` with only `scope:<path>` tokens merges on top of `default_bias`; set `["relevance"]` to disable recency, kind/tag, and automatic prompt-hook scope bias.
|
|
261
|
+
- `[prompt_hook]`: `enabled`, `model` (`"haiku"` or `"none"`), `timeout`, `limit`, `claude_bin`, `extra_stopwords`, `min_idf` (default `3.0`; `0.0` disables), `local_min_idf` (default `8.0`; `0.0` disables), and `[prompt_hook.projects]` project codes.
|
|
262
|
+
- `[catch_up]`: `enabled`, `interval_minutes` (default `10`).
|
|
263
|
+
- `[upkeep]`: `tasklog_roots` and `handoff_dirs`.
|
|
264
|
+
- `[capture]`: `enabled`, `root` (`~/.claude/projects`), `idle_minutes`, `since_days`, `min_prompts`, `max_per_run`, `model`, `timeout`, `dialogue_chars`.
|
|
265
|
+
- `[automemory]`: `enabled`, `root` (`~/.claude/projects`).
|
|
266
|
+
- `[scopes]` and `[backup]`: Path matchers plus backup `interval_hours` (`24`) and `keep` (`7`).
|
|
267
|
+
|
|
268
|
+
Environment variables:
|
|
269
|
+
|
|
270
|
+
- `SIMPLE_MEMORY_HOME`: Override `~/.simple-memory`.
|
|
271
|
+
- `SM_UTC_OFFSET`: Explicit offset (`+00:00`, `-05:00`, `Z`, `+0530`) overriding OS local timezone (`localtime_r`).
|
|
272
|
+
- `SM_PROMPT_HOOK=0`: Disable prompt-hook recall and background catch-up for one process tree (such as subagent runs).
|
|
273
|
+
- `SM_BINARY_PATH`: Explicit path to a `sm` binary for the npm wrapper; exits with an error if the path does not exist instead of falling back.
|
|
274
|
+
- `SM_SKIP_DOWNLOAD=1`: Skip the npm `postinstall` GitHub Release binary fallback download.
|
|
275
|
+
- `SM_NO_UPDATE_CHECK=1`: Disable the npm wrapper's 24-hour interactive update reminder.
|
|
276
|
+
|
|
277
|
+
</details>
|
|
278
|
+
|
|
279
|
+
<details>
|
|
280
|
+
<summary>Privacy, redaction, and limits</summary>
|
|
281
|
+
|
|
282
|
+
- **Local by default**: Everything stays in your local `memory.db`. Model calls run only when Claude Haiku is invoked through your local `claude -p` for `[prompt_hook]` query distillation or `[capture]` session cards (disable either in `config.toml`).
|
|
283
|
+
- **Automatic write redaction**: `put_item` and `put_items_batch` strip `<private>...</private>` blocks and replace known secret shapes (private keys, cloud access keys, API tokens such as `sk-`/token prefixes, JWTs, bearer tokens, signed URL query parameters, URL credentials, and `password=`-style assignments) with `[REDACTED:<kind>]` before SQLite insert.
|
|
284
|
+
- **Known limits**: Search is lexical/prefix-based with configurable `[keyword_aliases]` rather than semantic embeddings; multi-machine sync is manual (copy `memory.db` or files under `backups/`).
|
|
285
|
+
|
|
286
|
+
</details>
|
|
287
|
+
|
|
288
|
+
<details>
|
|
289
|
+
<summary>Development and testing</summary>
|
|
290
|
+
|
|
291
|
+
Run all checks with `HOME` and `SIMPLE_MEMORY_HOME` pointed at an empty scratch directory (preserving `RUSTUP_HOME` and `CARGO_HOME`):
|
|
292
|
+
|
|
293
|
+
```sh
|
|
294
|
+
cd rust
|
|
295
|
+
cargo fmt --check
|
|
296
|
+
cargo build --release --locked
|
|
297
|
+
cargo clippy --all-targets --locked -- -D warnings
|
|
298
|
+
cargo clippy --all-targets --locked --features parity -- -D warnings
|
|
299
|
+
mkdir -p /tmp/sm-test/.simple-memory
|
|
300
|
+
env RUSTUP_HOME="${RUSTUP_HOME:-$HOME/.rustup}" CARGO_HOME="${CARGO_HOME:-$HOME/.cargo}" \
|
|
301
|
+
HOME=/tmp/sm-test SIMPLE_MEMORY_HOME=/tmp/sm-test/.simple-memory \
|
|
302
|
+
cargo test --locked --features parity --no-fail-fast
|
|
303
|
+
cd ..
|
|
304
|
+
TMPDIR=/tmp bash scripts/dead_code_scan.sh rust
|
|
305
|
+
TMPDIR=/tmp bash scripts/smoke.sh rust/target/release/sm
|
|
306
|
+
TMPDIR=/tmp bash scripts/test_scripts.sh
|
|
307
|
+
node --test npm/test/npm_wrapper.test.mjs
|
|
308
|
+
```
|
|
309
|
+
|
|
310
|
+
Note: Avoid colons (`:`) inside scratch `HOME` paths on macOS (`DYLD_FALLBACK_LIBRARY_PATH` treats `:` as a path separator); test custom timezone offsets via `SM_UTC_OFFSET=+05:30`.
|
|
311
|
+
|
|
312
|
+
</details>
|
|
313
|
+
|
|
314
|
+
## License
|
|
315
|
+
|
|
316
|
+
[MIT](LICENSE)
|
package/bin/sm.js
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
"use strict";
|
|
3
|
+
|
|
4
|
+
const { spawn } = require("node:child_process");
|
|
5
|
+
const os = require("node:os");
|
|
6
|
+
const path = require("node:path");
|
|
7
|
+
const { resolveBinary } = require("../lib/platform");
|
|
8
|
+
const { maybeNotifyAndRefresh } = require("../lib/update");
|
|
9
|
+
const { version: PACKAGE_VERSION } = require("../package.json");
|
|
10
|
+
|
|
11
|
+
const FORWARDED_SIGNALS = ["SIGINT", "SIGTERM", "SIGHUP"];
|
|
12
|
+
|
|
13
|
+
function spawnUpdateCheck(cachePath) {
|
|
14
|
+
const scriptPath = path.resolve(__dirname, "..", "scripts", "check-update.js");
|
|
15
|
+
const checker = spawn(process.execPath, [scriptPath, cachePath], {
|
|
16
|
+
detached: true,
|
|
17
|
+
stdio: "ignore",
|
|
18
|
+
env: process.env,
|
|
19
|
+
});
|
|
20
|
+
checker.on("error", () => {});
|
|
21
|
+
checker.unref();
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const resolved = resolveBinary();
|
|
25
|
+
if (!resolved.ok) {
|
|
26
|
+
process.stderr.write(
|
|
27
|
+
`simple-memory: ${resolved.reason} (${process.platform}-${process.arch}).\n` +
|
|
28
|
+
"Build from source with ./scripts/install.sh or set SM_BINARY_PATH to an existing sm binary.\n"
|
|
29
|
+
);
|
|
30
|
+
process.exit(1);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const argv = process.argv.slice(2);
|
|
34
|
+
const child = spawn(resolved.path, argv, {
|
|
35
|
+
stdio: "inherit",
|
|
36
|
+
env: { ...process.env, SM_INSTALL_METHOD: "npm" },
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
const handlers = new Map();
|
|
40
|
+
for (const sig of FORWARDED_SIGNALS) {
|
|
41
|
+
const handler = () => {
|
|
42
|
+
if (child.exitCode === null && child.signalCode === null) {
|
|
43
|
+
try {
|
|
44
|
+
child.kill(sig);
|
|
45
|
+
} catch {
|
|
46
|
+
// Child exited between check and signal delivery.
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
};
|
|
50
|
+
handlers.set(sig, handler);
|
|
51
|
+
process.on(sig, handler);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function detachSignalHandlers() {
|
|
55
|
+
for (const [sig, handler] of handlers) {
|
|
56
|
+
process.removeListener(sig, handler);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
child.on("error", (err) => {
|
|
61
|
+
detachSignalHandlers();
|
|
62
|
+
process.stderr.write(
|
|
63
|
+
`simple-memory: failed to execute ${resolved.path}: ${err && err.message ? err.message : String(err)}\n`
|
|
64
|
+
);
|
|
65
|
+
process.exit(1);
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
child.on("exit", (code, signal) => {
|
|
69
|
+
detachSignalHandlers();
|
|
70
|
+
if (signal) {
|
|
71
|
+
process.removeAllListeners(signal);
|
|
72
|
+
try {
|
|
73
|
+
process.kill(process.pid, signal);
|
|
74
|
+
} catch {
|
|
75
|
+
// Fall through to 128 + signal exit code below.
|
|
76
|
+
}
|
|
77
|
+
// Node ignores SIGPIPE by default, so self-kill returns without terminating.
|
|
78
|
+
const sigNum = os.constants.signals[signal] || 1;
|
|
79
|
+
process.exit(128 + sigNum);
|
|
80
|
+
}
|
|
81
|
+
maybeNotifyAndRefresh({
|
|
82
|
+
argv,
|
|
83
|
+
env: process.env,
|
|
84
|
+
currentVersion: PACKAGE_VERSION,
|
|
85
|
+
isStderrTTY: Boolean(process.stderr.isTTY),
|
|
86
|
+
stderr: process.stderr,
|
|
87
|
+
spawnRefresh: spawnUpdateCheck,
|
|
88
|
+
});
|
|
89
|
+
process.exit(code === null ? 1 : code);
|
|
90
|
+
});
|
package/lib/platform.js
ADDED
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const fs = require("node:fs");
|
|
4
|
+
const path = require("node:path");
|
|
5
|
+
|
|
6
|
+
// Target = { key: string, pkg: string, binName: "sm", assetName: string, rustTarget: string }
|
|
7
|
+
const TARGETS = Object.freeze({
|
|
8
|
+
"darwin-arm64": Object.freeze({
|
|
9
|
+
key: "darwin-arm64",
|
|
10
|
+
pkg: "simple-memory-darwin-arm64",
|
|
11
|
+
binName: "sm",
|
|
12
|
+
assetName: "sm-darwin-arm64.tar.gz",
|
|
13
|
+
rustTarget: "aarch64-apple-darwin",
|
|
14
|
+
}),
|
|
15
|
+
"darwin-x64": Object.freeze({
|
|
16
|
+
key: "darwin-x64",
|
|
17
|
+
pkg: "simple-memory-darwin-x64",
|
|
18
|
+
binName: "sm",
|
|
19
|
+
assetName: "sm-darwin-x64.tar.gz",
|
|
20
|
+
rustTarget: "x86_64-apple-darwin",
|
|
21
|
+
}),
|
|
22
|
+
"linux-x64": Object.freeze({
|
|
23
|
+
key: "linux-x64",
|
|
24
|
+
pkg: "simple-memory-linux-x64",
|
|
25
|
+
binName: "sm",
|
|
26
|
+
assetName: "sm-linux-x64.tar.gz",
|
|
27
|
+
rustTarget: "x86_64-unknown-linux-gnu",
|
|
28
|
+
}),
|
|
29
|
+
"linux-arm64": Object.freeze({
|
|
30
|
+
key: "linux-arm64",
|
|
31
|
+
pkg: "simple-memory-linux-arm64",
|
|
32
|
+
binName: "sm",
|
|
33
|
+
assetName: "sm-linux-arm64.tar.gz",
|
|
34
|
+
rustTarget: "aarch64-unknown-linux-gnu",
|
|
35
|
+
}),
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
function isRegularFile(filePath) {
|
|
39
|
+
if (typeof filePath !== "string" || filePath.length === 0) {
|
|
40
|
+
return false;
|
|
41
|
+
}
|
|
42
|
+
try {
|
|
43
|
+
return fs.statSync(filePath).isFile();
|
|
44
|
+
} catch {
|
|
45
|
+
return false;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function resolveTarget(platform = process.platform, arch = process.arch) {
|
|
50
|
+
const key = `${platform}-${arch}`;
|
|
51
|
+
return Object.prototype.hasOwnProperty.call(TARGETS, key) ? TARGETS[key] : null;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
// ResolveResult =
|
|
55
|
+
// | { ok: true, path: string, source: "env" | "optionalDependency" | "vendor" }
|
|
56
|
+
// | { ok: false, reason: string, target: Target | null }
|
|
57
|
+
function resolveBinary(options = {}) {
|
|
58
|
+
const {
|
|
59
|
+
env = process.env,
|
|
60
|
+
platform = process.platform,
|
|
61
|
+
arch = process.arch,
|
|
62
|
+
resolvePkg = require.resolve,
|
|
63
|
+
vendorDir = path.resolve(__dirname, "..", "vendor"),
|
|
64
|
+
isFile = isRegularFile,
|
|
65
|
+
} = options;
|
|
66
|
+
|
|
67
|
+
const target = resolveTarget(platform, arch);
|
|
68
|
+
|
|
69
|
+
const envPath = env && typeof env.SM_BINARY_PATH === "string" ? env.SM_BINARY_PATH : "";
|
|
70
|
+
if (envPath.length > 0) {
|
|
71
|
+
if (isFile(envPath)) {
|
|
72
|
+
return { ok: true, path: path.resolve(envPath), source: "env" };
|
|
73
|
+
}
|
|
74
|
+
return {
|
|
75
|
+
ok: false,
|
|
76
|
+
reason: `SM_BINARY_PATH is not an existing file: ${envPath}`,
|
|
77
|
+
target,
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
if (!target) {
|
|
82
|
+
return {
|
|
83
|
+
ok: false,
|
|
84
|
+
reason: `Unsupported platform: ${platform}-${arch}`,
|
|
85
|
+
target: null,
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
try {
|
|
90
|
+
const resolved = resolvePkg(`${target.pkg}/bin/${target.binName}`);
|
|
91
|
+
if (isFile(resolved)) {
|
|
92
|
+
return { ok: true, path: resolved, source: "optionalDependency" };
|
|
93
|
+
}
|
|
94
|
+
} catch (err) {
|
|
95
|
+
if (
|
|
96
|
+
!err ||
|
|
97
|
+
(err.code !== "MODULE_NOT_FOUND" && err.code !== "ERR_PACKAGE_PATH_NOT_EXPORTED")
|
|
98
|
+
) {
|
|
99
|
+
throw err;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
const vendorPath = path.join(vendorDir, target.binName);
|
|
104
|
+
if (isFile(vendorPath)) {
|
|
105
|
+
return { ok: true, path: vendorPath, source: "vendor" };
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
return {
|
|
109
|
+
ok: false,
|
|
110
|
+
reason: `No prebuilt sm binary found for ${target.key} (tried ${target.pkg} and ${vendorPath})`,
|
|
111
|
+
target,
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
module.exports = {
|
|
116
|
+
TARGETS,
|
|
117
|
+
isRegularFile,
|
|
118
|
+
resolveTarget,
|
|
119
|
+
resolveBinary,
|
|
120
|
+
};
|
package/lib/update.js
ADDED
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const fs = require("node:fs");
|
|
4
|
+
const os = require("node:os");
|
|
5
|
+
const path = require("node:path");
|
|
6
|
+
|
|
7
|
+
// Semver = { major: number, minor: number, patch: number }
|
|
8
|
+
// UpdateCache = { checked_at: number, latest: string }
|
|
9
|
+
// NotifyResult = { notified: boolean, refreshed: boolean, latest: string | null }
|
|
10
|
+
const DEFAULT_TTL_MS = 86400_000;
|
|
11
|
+
const SKIP_SUBCOMMANDS = new Set(["mcp", "hook", "hooks", "update"]);
|
|
12
|
+
const SKIP_FLAGS = new Set(["--version", "-v", "-h", "--help"]);
|
|
13
|
+
|
|
14
|
+
function parseSemver(v) {
|
|
15
|
+
if (typeof v !== "string") {
|
|
16
|
+
return null;
|
|
17
|
+
}
|
|
18
|
+
const m = /^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)$/.exec(v);
|
|
19
|
+
if (!m) {
|
|
20
|
+
return null;
|
|
21
|
+
}
|
|
22
|
+
const major = Number(m[1]);
|
|
23
|
+
const minor = Number(m[2]);
|
|
24
|
+
const patch = Number(m[3]);
|
|
25
|
+
if (
|
|
26
|
+
!Number.isSafeInteger(major) ||
|
|
27
|
+
!Number.isSafeInteger(minor) ||
|
|
28
|
+
!Number.isSafeInteger(patch)
|
|
29
|
+
) {
|
|
30
|
+
return null;
|
|
31
|
+
}
|
|
32
|
+
return { major, minor, patch };
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function isNewerVersion(latest, current) {
|
|
36
|
+
const a = parseSemver(latest);
|
|
37
|
+
const b = parseSemver(current);
|
|
38
|
+
if (!a || !b) {
|
|
39
|
+
return false;
|
|
40
|
+
}
|
|
41
|
+
if (a.major !== b.major) {
|
|
42
|
+
return a.major > b.major;
|
|
43
|
+
}
|
|
44
|
+
if (a.minor !== b.minor) {
|
|
45
|
+
return a.minor > b.minor;
|
|
46
|
+
}
|
|
47
|
+
return a.patch > b.patch;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function shouldCheckUpdate(options = {}) {
|
|
51
|
+
const { argv = [], env = {}, isStderrTTY = false } = options;
|
|
52
|
+
if (isStderrTTY !== true) {
|
|
53
|
+
return false;
|
|
54
|
+
}
|
|
55
|
+
if (env && env.SM_NO_UPDATE_CHECK === "1") {
|
|
56
|
+
return false;
|
|
57
|
+
}
|
|
58
|
+
const ciRaw = env && typeof env.CI === "string" ? env.CI.trim().toLowerCase() : "";
|
|
59
|
+
if (ciRaw.length > 0 && ciRaw !== "0" && ciRaw !== "false") {
|
|
60
|
+
return false;
|
|
61
|
+
}
|
|
62
|
+
if (!Array.isArray(argv)) {
|
|
63
|
+
return false;
|
|
64
|
+
}
|
|
65
|
+
if (argv.length > 0 && SKIP_SUBCOMMANDS.has(argv[0])) {
|
|
66
|
+
return false;
|
|
67
|
+
}
|
|
68
|
+
for (let i = 0; i < argv.length; i += 1) {
|
|
69
|
+
const arg = argv[i];
|
|
70
|
+
if (arg === "--") {
|
|
71
|
+
break;
|
|
72
|
+
}
|
|
73
|
+
if (SKIP_FLAGS.has(arg)) {
|
|
74
|
+
return false;
|
|
75
|
+
}
|
|
76
|
+
if (arg === "--format=json") {
|
|
77
|
+
return false;
|
|
78
|
+
}
|
|
79
|
+
if (arg === "--format" && argv[i + 1] === "json") {
|
|
80
|
+
return false;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
return true;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
function expandTilde(p, homeDir) {
|
|
87
|
+
if (p === "~") {
|
|
88
|
+
return homeDir;
|
|
89
|
+
}
|
|
90
|
+
if (p.startsWith("~/") || p.startsWith("~\\")) {
|
|
91
|
+
return path.join(homeDir, p.slice(2));
|
|
92
|
+
}
|
|
93
|
+
return p;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
function cachePathForEnv(env = process.env, homeDir = os.homedir()) {
|
|
97
|
+
const smHome =
|
|
98
|
+
env && typeof env.SIMPLE_MEMORY_HOME === "string" && env.SIMPLE_MEMORY_HOME.trim().length > 0
|
|
99
|
+
? expandTilde(env.SIMPLE_MEMORY_HOME.trim(), homeDir)
|
|
100
|
+
: path.join(homeDir, ".simple-memory");
|
|
101
|
+
return path.join(smHome, "update-check.json");
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
function readUpdateCache(cachePath) {
|
|
105
|
+
if (typeof cachePath !== "string" || cachePath.length === 0) {
|
|
106
|
+
return null;
|
|
107
|
+
}
|
|
108
|
+
let raw;
|
|
109
|
+
try {
|
|
110
|
+
raw = fs.readFileSync(cachePath, "utf8");
|
|
111
|
+
} catch {
|
|
112
|
+
return null;
|
|
113
|
+
}
|
|
114
|
+
let parsed;
|
|
115
|
+
try {
|
|
116
|
+
parsed = JSON.parse(raw);
|
|
117
|
+
} catch {
|
|
118
|
+
return null;
|
|
119
|
+
}
|
|
120
|
+
if (
|
|
121
|
+
!parsed ||
|
|
122
|
+
typeof parsed !== "object" ||
|
|
123
|
+
typeof parsed.checked_at !== "number" ||
|
|
124
|
+
!Number.isFinite(parsed.checked_at) ||
|
|
125
|
+
parsed.checked_at < 0 ||
|
|
126
|
+
parseSemver(parsed.latest) === null
|
|
127
|
+
) {
|
|
128
|
+
return null;
|
|
129
|
+
}
|
|
130
|
+
return { checked_at: parsed.checked_at, latest: parsed.latest };
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
function isCacheStale(cache, nowMs = Date.now(), ttlMs = DEFAULT_TTL_MS) {
|
|
134
|
+
if (!cache || typeof cache.checked_at !== "number" || !Number.isFinite(cache.checked_at)) {
|
|
135
|
+
return true;
|
|
136
|
+
}
|
|
137
|
+
return nowMs - cache.checked_at >= ttlMs;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
function formatUpdateNotice(current, latest) {
|
|
141
|
+
return (
|
|
142
|
+
`Update available: ${current} -> ${latest}. ` +
|
|
143
|
+
"Run: sm update (or npm install -g simple-memory@latest; disable: SM_NO_UPDATE_CHECK=1)\n"
|
|
144
|
+
);
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
function maybeNotifyAndRefresh(options = {}) {
|
|
148
|
+
const {
|
|
149
|
+
argv = [],
|
|
150
|
+
env = process.env,
|
|
151
|
+
currentVersion = "",
|
|
152
|
+
isStderrTTY = false,
|
|
153
|
+
stderr = process.stderr,
|
|
154
|
+
spawnRefresh = null,
|
|
155
|
+
homeDir = os.homedir(),
|
|
156
|
+
nowMs = Date.now(),
|
|
157
|
+
ttlMs = DEFAULT_TTL_MS,
|
|
158
|
+
} = options;
|
|
159
|
+
|
|
160
|
+
if (!shouldCheckUpdate({ argv, env, isStderrTTY })) {
|
|
161
|
+
return { notified: false, refreshed: false, latest: null };
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
const cachePath = cachePathForEnv(env, homeDir);
|
|
165
|
+
const cache = readUpdateCache(cachePath);
|
|
166
|
+
let notified = false;
|
|
167
|
+
let refreshed = false;
|
|
168
|
+
|
|
169
|
+
if (cache && isNewerVersion(cache.latest, currentVersion)) {
|
|
170
|
+
try {
|
|
171
|
+
stderr.write(formatUpdateNotice(currentVersion, cache.latest));
|
|
172
|
+
notified = true;
|
|
173
|
+
} catch {
|
|
174
|
+
// Best-effort stderr notice must never alter command exit status.
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
if (isCacheStale(cache, nowMs, ttlMs) && typeof spawnRefresh === "function") {
|
|
179
|
+
try {
|
|
180
|
+
spawnRefresh(cachePath);
|
|
181
|
+
refreshed = true;
|
|
182
|
+
} catch {
|
|
183
|
+
// Best-effort background refresh must never alter command exit status.
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
return { notified, refreshed, latest: cache ? cache.latest : null };
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
module.exports = {
|
|
191
|
+
DEFAULT_TTL_MS,
|
|
192
|
+
parseSemver,
|
|
193
|
+
isNewerVersion,
|
|
194
|
+
shouldCheckUpdate,
|
|
195
|
+
cachePathForEnv,
|
|
196
|
+
readUpdateCache,
|
|
197
|
+
isCacheStale,
|
|
198
|
+
formatUpdateNotice,
|
|
199
|
+
maybeNotifyAndRefresh,
|
|
200
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,43 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "simple-memory",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.6.0",
|
|
4
|
+
"description": "Local single-file SQLite + FTS5 memory store and MCP server for coding agents",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"keywords": [
|
|
7
|
+
"memory",
|
|
8
|
+
"agents",
|
|
9
|
+
"sqlite",
|
|
10
|
+
"fts5",
|
|
11
|
+
"mcp"
|
|
12
|
+
],
|
|
13
|
+
"repository": {
|
|
14
|
+
"type": "git",
|
|
15
|
+
"url": "https://github.com/tuanhung303/simple-memory.git"
|
|
16
|
+
},
|
|
17
|
+
"homepage": "https://github.com/tuanhung303/simple-memory#readme",
|
|
18
|
+
"bugs": {
|
|
19
|
+
"url": "https://github.com/tuanhung303/simple-memory/issues"
|
|
20
|
+
},
|
|
21
|
+
"engines": {
|
|
22
|
+
"node": ">=18"
|
|
23
|
+
},
|
|
24
|
+
"preferUnplugged": true,
|
|
25
|
+
"bin": {
|
|
26
|
+
"sm": "bin/sm.js",
|
|
27
|
+
"simple-memory": "bin/sm.js"
|
|
28
|
+
},
|
|
29
|
+
"files": [
|
|
30
|
+
"bin/",
|
|
31
|
+
"lib/",
|
|
32
|
+
"scripts/",
|
|
33
|
+
"LICENSE",
|
|
34
|
+
"README.md"
|
|
35
|
+
],
|
|
36
|
+
"scripts": {
|
|
37
|
+
"postinstall": "node scripts/postinstall.js"
|
|
38
|
+
},
|
|
39
|
+
"optionalDependencies": {
|
|
40
|
+
"simple-memory-darwin-arm64": "0.6.0",
|
|
41
|
+
"simple-memory-darwin-x64": "0.6.0"
|
|
42
|
+
}
|
|
43
|
+
}
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const crypto = require("node:crypto");
|
|
4
|
+
const fs = require("node:fs");
|
|
5
|
+
const os = require("node:os");
|
|
6
|
+
const path = require("node:path");
|
|
7
|
+
const { cachePathForEnv, parseSemver } = require("../lib/update");
|
|
8
|
+
|
|
9
|
+
const DEFAULT_UPDATE_URL = "https://registry.npmjs.org/simple-memory/latest";
|
|
10
|
+
const DEFAULT_TIMEOUT_MS = 3000;
|
|
11
|
+
const MAX_RESPONSE_BYTES = 256 * 1024;
|
|
12
|
+
|
|
13
|
+
async function readTextWithLimit(body, maxBytes) {
|
|
14
|
+
if (!body) {
|
|
15
|
+
return "";
|
|
16
|
+
}
|
|
17
|
+
const chunks = [];
|
|
18
|
+
let total = 0;
|
|
19
|
+
for await (const chunk of body) {
|
|
20
|
+
const buf = Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk);
|
|
21
|
+
total += buf.length;
|
|
22
|
+
if (total > maxBytes) {
|
|
23
|
+
if (typeof body.cancel === "function") {
|
|
24
|
+
await body.cancel().catch(() => {});
|
|
25
|
+
}
|
|
26
|
+
throw new Error("Response exceeded size limit");
|
|
27
|
+
}
|
|
28
|
+
chunks.push(buf);
|
|
29
|
+
}
|
|
30
|
+
return Buffer.concat(chunks, total).toString("utf8");
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function writeCacheAtomic(cachePath, checkedAt, latest) {
|
|
34
|
+
const dir = path.dirname(cachePath);
|
|
35
|
+
fs.mkdirSync(dir, { recursive: true });
|
|
36
|
+
const nonce = `${process.pid}.${crypto.randomBytes(6).toString("hex")}`;
|
|
37
|
+
const tmpPath = path.join(dir, `.${path.basename(cachePath)}.tmp.${nonce}`);
|
|
38
|
+
const payload = JSON.stringify({ checked_at: checkedAt, latest }) + "\n";
|
|
39
|
+
try {
|
|
40
|
+
fs.writeFileSync(tmpPath, payload, { mode: 0o644 });
|
|
41
|
+
fs.renameSync(tmpPath, cachePath);
|
|
42
|
+
} finally {
|
|
43
|
+
try {
|
|
44
|
+
fs.unlinkSync(tmpPath);
|
|
45
|
+
} catch {
|
|
46
|
+
// Temp file was already renamed or never created.
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
async function fetchAndWriteUpdateCache(options = {}) {
|
|
52
|
+
const {
|
|
53
|
+
cachePath,
|
|
54
|
+
env = process.env,
|
|
55
|
+
url = (env && env.SM_UPDATE_CHECK_URL) || DEFAULT_UPDATE_URL,
|
|
56
|
+
timeoutMs = DEFAULT_TIMEOUT_MS,
|
|
57
|
+
fetchImpl = globalThis.fetch,
|
|
58
|
+
nowMs = null,
|
|
59
|
+
maxBytes = MAX_RESPONSE_BYTES,
|
|
60
|
+
} = options;
|
|
61
|
+
|
|
62
|
+
if (typeof cachePath !== "string" || cachePath.length === 0) {
|
|
63
|
+
return false;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
try {
|
|
67
|
+
const res = await fetchImpl(url, {
|
|
68
|
+
headers: { accept: "application/json" },
|
|
69
|
+
signal: AbortSignal.timeout(timeoutMs),
|
|
70
|
+
});
|
|
71
|
+
if (!res || !res.ok) {
|
|
72
|
+
if (res && res.body && typeof res.body.cancel === "function") {
|
|
73
|
+
await res.body.cancel().catch(() => {});
|
|
74
|
+
}
|
|
75
|
+
return false;
|
|
76
|
+
}
|
|
77
|
+
const text = await readTextWithLimit(res.body, maxBytes);
|
|
78
|
+
const data = JSON.parse(text);
|
|
79
|
+
const version = data && typeof data.version === "string" ? data.version.trim() : "";
|
|
80
|
+
if (!parseSemver(version)) {
|
|
81
|
+
return false;
|
|
82
|
+
}
|
|
83
|
+
const checkedAt = typeof nowMs === "number" ? nowMs : Date.now();
|
|
84
|
+
writeCacheAtomic(cachePath, checkedAt, version);
|
|
85
|
+
return true;
|
|
86
|
+
} catch {
|
|
87
|
+
return false;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
if (require.main === module) {
|
|
92
|
+
const cachePath = process.argv[2] || cachePathForEnv(process.env, os.homedir());
|
|
93
|
+
fetchAndWriteUpdateCache({ cachePath, env: process.env }).then(
|
|
94
|
+
() => process.exit(0),
|
|
95
|
+
() => process.exit(0)
|
|
96
|
+
);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
module.exports = {
|
|
100
|
+
DEFAULT_UPDATE_URL,
|
|
101
|
+
DEFAULT_TIMEOUT_MS,
|
|
102
|
+
MAX_RESPONSE_BYTES,
|
|
103
|
+
fetchAndWriteUpdateCache,
|
|
104
|
+
};
|
|
@@ -0,0 +1,442 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const crypto = require("node:crypto");
|
|
4
|
+
const fs = require("node:fs");
|
|
5
|
+
const path = require("node:path");
|
|
6
|
+
const zlib = require("node:zlib");
|
|
7
|
+
const { isRegularFile, resolveBinary, resolveTarget } = require("../lib/platform");
|
|
8
|
+
|
|
9
|
+
const DEFAULT_TIMEOUT_MS = 15000;
|
|
10
|
+
const MAX_SUMS_BYTES = 1024 * 1024;
|
|
11
|
+
const MAX_DOWNLOAD_BYTES = 50 * 1024 * 1024;
|
|
12
|
+
const MAX_UNCOMPRESSED_BYTES = 100 * 1024 * 1024;
|
|
13
|
+
const DEFAULT_RELEASE_BASE = "https://github.com/tuanhung303/simple-memory/releases/download";
|
|
14
|
+
|
|
15
|
+
function parseTimeoutMs(raw) {
|
|
16
|
+
if (typeof raw !== "string" || raw.trim().length === 0) {
|
|
17
|
+
return DEFAULT_TIMEOUT_MS;
|
|
18
|
+
}
|
|
19
|
+
const value = Number(raw);
|
|
20
|
+
if (!Number.isFinite(value) || value <= 0) {
|
|
21
|
+
return DEFAULT_TIMEOUT_MS;
|
|
22
|
+
}
|
|
23
|
+
return Math.floor(value);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function parseSha256Sums(text, assetName) {
|
|
27
|
+
let matched = null;
|
|
28
|
+
for (const rawLine of text.split(/\r?\n/)) {
|
|
29
|
+
const line = rawLine.trim();
|
|
30
|
+
if (line.length === 0) {
|
|
31
|
+
continue;
|
|
32
|
+
}
|
|
33
|
+
const m = /^([0-9a-fA-F]{64})\s+\*?(.+)$/.exec(line);
|
|
34
|
+
if (!m) {
|
|
35
|
+
continue;
|
|
36
|
+
}
|
|
37
|
+
const digest = m[1].toLowerCase();
|
|
38
|
+
const file = m[2].trim();
|
|
39
|
+
if (file === assetName || file === `./${assetName}`) {
|
|
40
|
+
if (matched !== null && matched !== digest) {
|
|
41
|
+
throw new Error(`Conflicting SHA256SUMS entries for ${assetName}`);
|
|
42
|
+
}
|
|
43
|
+
matched = digest;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
return matched;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
async function readStreamWithLimit(body, maxBytes) {
|
|
50
|
+
if (!body) {
|
|
51
|
+
return Buffer.alloc(0);
|
|
52
|
+
}
|
|
53
|
+
const chunks = [];
|
|
54
|
+
let total = 0;
|
|
55
|
+
for await (const chunk of body) {
|
|
56
|
+
const buf = Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk);
|
|
57
|
+
total += buf.length;
|
|
58
|
+
if (total > maxBytes) {
|
|
59
|
+
if (typeof body.cancel === "function") {
|
|
60
|
+
await body.cancel().catch(() => {});
|
|
61
|
+
} else if (typeof body.destroy === "function") {
|
|
62
|
+
body.destroy();
|
|
63
|
+
}
|
|
64
|
+
const err = new Error(`Download exceeded limit of ${maxBytes} bytes`);
|
|
65
|
+
err.code = "ERR_SIZE_LIMIT";
|
|
66
|
+
throw err;
|
|
67
|
+
}
|
|
68
|
+
chunks.push(buf);
|
|
69
|
+
}
|
|
70
|
+
return Buffer.concat(chunks, total);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function isZeroBlock(buf) {
|
|
74
|
+
for (let i = 0; i < buf.length; i += 1) {
|
|
75
|
+
if (buf[i] !== 0) {
|
|
76
|
+
return false;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
return true;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
function readCString(buf, start, end) {
|
|
83
|
+
let stop = start;
|
|
84
|
+
while (stop < end && buf[stop] !== 0) {
|
|
85
|
+
stop += 1;
|
|
86
|
+
}
|
|
87
|
+
return buf.toString("utf8", start, stop);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
function parseOctal(buf, start, end) {
|
|
91
|
+
const raw = readCString(buf, start, end).trim();
|
|
92
|
+
if (raw.length === 0) {
|
|
93
|
+
return 0;
|
|
94
|
+
}
|
|
95
|
+
if (!/^[0-7]+$/.test(raw)) {
|
|
96
|
+
throw new Error(`Invalid octal field in tar header: ${raw}`);
|
|
97
|
+
}
|
|
98
|
+
return Number.parseInt(raw, 8);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
function verifyTarChecksum(header) {
|
|
102
|
+
const stored = parseOctal(header, 148, 156);
|
|
103
|
+
let sum = 0;
|
|
104
|
+
for (let i = 0; i < 512; i += 1) {
|
|
105
|
+
sum += i >= 148 && i < 156 ? 0x20 : header[i];
|
|
106
|
+
}
|
|
107
|
+
if (sum !== stored) {
|
|
108
|
+
throw new Error("Invalid tar header checksum");
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
function parsePaxPath(data) {
|
|
113
|
+
let offset = 0;
|
|
114
|
+
let paxPath = null;
|
|
115
|
+
while (offset < data.length) {
|
|
116
|
+
const spaceIdx = data.indexOf(0x20, offset);
|
|
117
|
+
if (spaceIdx === -1) {
|
|
118
|
+
break;
|
|
119
|
+
}
|
|
120
|
+
const len = Number.parseInt(data.toString("ascii", offset, spaceIdx), 10);
|
|
121
|
+
if (!Number.isFinite(len) || len <= 0 || offset + len > data.length) {
|
|
122
|
+
throw new Error("Invalid PAX extended header");
|
|
123
|
+
}
|
|
124
|
+
const record = data.toString("utf8", spaceIdx + 1, offset + len);
|
|
125
|
+
const body = record.endsWith("\n") ? record.slice(0, -1) : record;
|
|
126
|
+
const eqIdx = body.indexOf("=");
|
|
127
|
+
if (eqIdx > 0 && body.slice(0, eqIdx) === "path") {
|
|
128
|
+
paxPath = body.slice(eqIdx + 1);
|
|
129
|
+
}
|
|
130
|
+
offset += len;
|
|
131
|
+
}
|
|
132
|
+
return paxPath;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
function assertSafeEntryPath(entryPath) {
|
|
136
|
+
if (
|
|
137
|
+
entryPath.length === 0 ||
|
|
138
|
+
entryPath.includes("\0") ||
|
|
139
|
+
entryPath.startsWith("/") ||
|
|
140
|
+
entryPath.startsWith("\\")
|
|
141
|
+
) {
|
|
142
|
+
throw new Error(`Unsafe archive entry path: ${entryPath}`);
|
|
143
|
+
}
|
|
144
|
+
const segments = entryPath.split(/[\\/]/);
|
|
145
|
+
if (segments.includes("..")) {
|
|
146
|
+
throw new Error(`Unsafe archive entry path: ${entryPath}`);
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
function extractBinaryFromTarGz(
|
|
151
|
+
tarGzBytes,
|
|
152
|
+
targetName = "sm",
|
|
153
|
+
maxUncompressedBytes = MAX_UNCOMPRESSED_BYTES
|
|
154
|
+
) {
|
|
155
|
+
const tarBytes = zlib.gunzipSync(tarGzBytes, {
|
|
156
|
+
maxOutputLength: maxUncompressedBytes,
|
|
157
|
+
});
|
|
158
|
+
if (tarBytes.length > maxUncompressedBytes) {
|
|
159
|
+
const err = new Error(`Uncompressed archive exceeded limit of ${maxUncompressedBytes} bytes`);
|
|
160
|
+
err.code = "ERR_SIZE_LIMIT";
|
|
161
|
+
throw err;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
let offset = 0;
|
|
165
|
+
let pendingPaxPath = null;
|
|
166
|
+
let extracted = null;
|
|
167
|
+
|
|
168
|
+
while (offset + 512 <= tarBytes.length) {
|
|
169
|
+
const header = tarBytes.subarray(offset, offset + 512);
|
|
170
|
+
if (isZeroBlock(header)) {
|
|
171
|
+
break;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
verifyTarChecksum(header);
|
|
175
|
+
|
|
176
|
+
const name = readCString(header, 0, 100);
|
|
177
|
+
const size = parseOctal(header, 124, 136);
|
|
178
|
+
const typeflag = String.fromCharCode(header[156]);
|
|
179
|
+
const magic = readCString(header, 257, 263);
|
|
180
|
+
const prefix = magic.startsWith("ustar") ? readCString(header, 345, 500) : "";
|
|
181
|
+
const rawPath = prefix.length > 0 ? `${prefix}/${name}` : name;
|
|
182
|
+
|
|
183
|
+
const dataStart = offset + 512;
|
|
184
|
+
const dataEnd = dataStart + size;
|
|
185
|
+
if (dataEnd > tarBytes.length) {
|
|
186
|
+
throw new Error("Truncated tar archive entry");
|
|
187
|
+
}
|
|
188
|
+
const nextOffset = dataStart + Math.ceil(size / 512) * 512;
|
|
189
|
+
|
|
190
|
+
// PAX extended headers may accompany files produced by bsdtar or GNU tar.
|
|
191
|
+
if (typeflag === "x" || typeflag === "g") {
|
|
192
|
+
if (typeflag === "x") {
|
|
193
|
+
const paxPath = parsePaxPath(tarBytes.subarray(dataStart, dataEnd));
|
|
194
|
+
if (paxPath !== null) {
|
|
195
|
+
pendingPaxPath = paxPath;
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
offset = nextOffset;
|
|
199
|
+
continue;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
const entryPath = pendingPaxPath !== null ? pendingPaxPath : rawPath;
|
|
203
|
+
pendingPaxPath = null;
|
|
204
|
+
|
|
205
|
+
assertSafeEntryPath(entryPath);
|
|
206
|
+
|
|
207
|
+
if (typeflag === "5") {
|
|
208
|
+
if (entryPath === targetName || entryPath === `./${targetName}`) {
|
|
209
|
+
throw new Error(`Archive entry ${entryPath} is a directory, expected regular file`);
|
|
210
|
+
}
|
|
211
|
+
offset = nextOffset;
|
|
212
|
+
continue;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
if (typeflag !== "0" && typeflag !== "\0") {
|
|
216
|
+
throw new Error(`Unsupported tar entry type '${typeflag}' for ${entryPath}`);
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
if (entryPath === targetName || entryPath === `./${targetName}`) {
|
|
220
|
+
if (extracted !== null) {
|
|
221
|
+
throw new Error(`Archive contains duplicate entries for ${targetName}`);
|
|
222
|
+
}
|
|
223
|
+
extracted = Buffer.from(tarBytes.subarray(dataStart, dataEnd));
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
offset = nextOffset;
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
if (extracted === null) {
|
|
230
|
+
throw new Error(`Archive does not contain regular file ${targetName}`);
|
|
231
|
+
}
|
|
232
|
+
if (extracted.length === 0) {
|
|
233
|
+
throw new Error(`Archive entry ${targetName} is empty`);
|
|
234
|
+
}
|
|
235
|
+
return extracted;
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
function readVendorVersion(versionPath) {
|
|
239
|
+
try {
|
|
240
|
+
return fs.readFileSync(versionPath, "utf8").trim();
|
|
241
|
+
} catch (err) {
|
|
242
|
+
if (err && (err.code === "ENOENT" || err.code === "ENOTDIR")) {
|
|
243
|
+
return null;
|
|
244
|
+
}
|
|
245
|
+
throw err;
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
function unlinkQuietly(filePath) {
|
|
250
|
+
try {
|
|
251
|
+
fs.unlinkSync(filePath);
|
|
252
|
+
} catch (err) {
|
|
253
|
+
if (!err || err.code !== "ENOENT") {
|
|
254
|
+
throw err;
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
async function runPostinstall(options = {}) {
|
|
260
|
+
const pkg = require("../package.json");
|
|
261
|
+
const {
|
|
262
|
+
env = process.env,
|
|
263
|
+
platform = process.platform,
|
|
264
|
+
arch = process.arch,
|
|
265
|
+
version = pkg.version,
|
|
266
|
+
vendorDir = path.resolve(__dirname, "..", "vendor"),
|
|
267
|
+
resolvePkg = require.resolve,
|
|
268
|
+
fetchImpl = globalThis.fetch,
|
|
269
|
+
stderr = process.stderr,
|
|
270
|
+
maxDownloadBytes = MAX_DOWNLOAD_BYTES,
|
|
271
|
+
maxUncompressedBytes = MAX_UNCOMPRESSED_BYTES,
|
|
272
|
+
} = options;
|
|
273
|
+
|
|
274
|
+
if (env.SM_SKIP_DOWNLOAD === "1") {
|
|
275
|
+
return 0;
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
const target = resolveTarget(platform, arch);
|
|
279
|
+
if (!target) {
|
|
280
|
+
stderr.write(
|
|
281
|
+
`simple-memory: no prebuilt binary for ${platform}-${arch}; build from source with ./scripts/install.sh or set SM_BINARY_PATH.\n`
|
|
282
|
+
);
|
|
283
|
+
return 0;
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
// Pass empty env so SM_BINARY_PATH never masks whether the optionalDependency is installed.
|
|
287
|
+
const optResolved = resolveBinary({
|
|
288
|
+
env: {},
|
|
289
|
+
platform,
|
|
290
|
+
arch,
|
|
291
|
+
resolvePkg,
|
|
292
|
+
vendorDir,
|
|
293
|
+
});
|
|
294
|
+
if (optResolved.ok && optResolved.source === "optionalDependency") {
|
|
295
|
+
return 0;
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
const vendorBin = path.join(vendorDir, target.binName);
|
|
299
|
+
const vendorVersionPath = path.join(vendorDir, ".version");
|
|
300
|
+
if (isRegularFile(vendorBin) && readVendorVersion(vendorVersionPath) === version) {
|
|
301
|
+
return 0;
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
const rawBaseUrl = env.SM_DOWNLOAD_BASE_URL || `${DEFAULT_RELEASE_BASE}/v${version}`;
|
|
305
|
+
const baseUrl = rawBaseUrl.replace(/\/+$/, "");
|
|
306
|
+
const timeoutMs = parseTimeoutMs(env.SM_DOWNLOAD_TIMEOUT_MS);
|
|
307
|
+
|
|
308
|
+
const sumsUrl = `${baseUrl}/SHA256SUMS`;
|
|
309
|
+
let sumsText;
|
|
310
|
+
try {
|
|
311
|
+
const sumsRes = await fetchImpl(sumsUrl, {
|
|
312
|
+
signal: AbortSignal.timeout(timeoutMs),
|
|
313
|
+
});
|
|
314
|
+
if (!sumsRes.ok) {
|
|
315
|
+
stderr.write(
|
|
316
|
+
`simple-memory: skipped binary download (${sumsUrl} returned HTTP ${sumsRes.status}); build from source with ./scripts/install.sh or set SM_BINARY_PATH.\n`
|
|
317
|
+
);
|
|
318
|
+
return 0;
|
|
319
|
+
}
|
|
320
|
+
const sumsBuf = await readStreamWithLimit(sumsRes.body, MAX_SUMS_BYTES);
|
|
321
|
+
sumsText = sumsBuf.toString("utf8");
|
|
322
|
+
} catch (err) {
|
|
323
|
+
if (err && err.code === "ERR_SIZE_LIMIT") {
|
|
324
|
+
stderr.write(`simple-memory: SHA256SUMS exceeded size limit: ${err.message}\n`);
|
|
325
|
+
return 1;
|
|
326
|
+
}
|
|
327
|
+
stderr.write(
|
|
328
|
+
`simple-memory: skipped binary download (${err && err.message ? err.message : String(err)}); build from source with ./scripts/install.sh or set SM_BINARY_PATH.\n`
|
|
329
|
+
);
|
|
330
|
+
return 0;
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
let expectedHash;
|
|
334
|
+
try {
|
|
335
|
+
expectedHash = parseSha256Sums(sumsText, target.assetName);
|
|
336
|
+
} catch (err) {
|
|
337
|
+
stderr.write(`simple-memory: ${err.message}\n`);
|
|
338
|
+
return 1;
|
|
339
|
+
}
|
|
340
|
+
if (!expectedHash) {
|
|
341
|
+
stderr.write(
|
|
342
|
+
`simple-memory: SHA256SUMS fetched from ${sumsUrl} does not contain an entry for ${target.assetName}\n`
|
|
343
|
+
);
|
|
344
|
+
return 1;
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
const assetUrl = `${baseUrl}/${target.assetName}`;
|
|
348
|
+
let tarGzBytes;
|
|
349
|
+
try {
|
|
350
|
+
const assetRes = await fetchImpl(assetUrl, {
|
|
351
|
+
signal: AbortSignal.timeout(timeoutMs),
|
|
352
|
+
});
|
|
353
|
+
if (!assetRes.ok) {
|
|
354
|
+
stderr.write(
|
|
355
|
+
`simple-memory: skipped binary download (${assetUrl} returned HTTP ${assetRes.status}); build from source with ./scripts/install.sh or set SM_BINARY_PATH.\n`
|
|
356
|
+
);
|
|
357
|
+
return 0;
|
|
358
|
+
}
|
|
359
|
+
const contentLengthHeader = assetRes.headers && assetRes.headers.get("content-length");
|
|
360
|
+
if (contentLengthHeader !== null && contentLengthHeader !== undefined) {
|
|
361
|
+
const declared = Number(contentLengthHeader);
|
|
362
|
+
if (Number.isFinite(declared) && declared > maxDownloadBytes) {
|
|
363
|
+
if (assetRes.body && typeof assetRes.body.cancel === "function") {
|
|
364
|
+
await assetRes.body.cancel().catch(() => {});
|
|
365
|
+
}
|
|
366
|
+
stderr.write(
|
|
367
|
+
`simple-memory: ${target.assetName} Content-Length ${declared} exceeds limit of ${maxDownloadBytes} bytes\n`
|
|
368
|
+
);
|
|
369
|
+
return 1;
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
tarGzBytes = await readStreamWithLimit(assetRes.body, maxDownloadBytes);
|
|
373
|
+
} catch (err) {
|
|
374
|
+
if (err && err.code === "ERR_SIZE_LIMIT") {
|
|
375
|
+
stderr.write(`simple-memory: ${target.assetName} ${err.message}\n`);
|
|
376
|
+
return 1;
|
|
377
|
+
}
|
|
378
|
+
stderr.write(
|
|
379
|
+
`simple-memory: skipped binary download (${err && err.message ? err.message : String(err)}); build from source with ./scripts/install.sh or set SM_BINARY_PATH.\n`
|
|
380
|
+
);
|
|
381
|
+
return 0;
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
const actualHash = crypto.createHash("sha256").update(tarGzBytes).digest("hex");
|
|
385
|
+
if (actualHash !== expectedHash) {
|
|
386
|
+
stderr.write(
|
|
387
|
+
`simple-memory: SHA-256 mismatch for ${target.assetName}: expected ${expectedHash}, got ${actualHash}\n`
|
|
388
|
+
);
|
|
389
|
+
return 1;
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
let binaryBytes;
|
|
393
|
+
try {
|
|
394
|
+
binaryBytes = extractBinaryFromTarGz(tarGzBytes, target.binName, maxUncompressedBytes);
|
|
395
|
+
} catch (err) {
|
|
396
|
+
stderr.write(
|
|
397
|
+
`simple-memory: failed to extract ${target.binName} from ${target.assetName}: ${err && err.message ? err.message : String(err)}\n`
|
|
398
|
+
);
|
|
399
|
+
return 1;
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
fs.mkdirSync(vendorDir, { recursive: true });
|
|
403
|
+
const nonce = `${process.pid}.${crypto.randomBytes(6).toString("hex")}`;
|
|
404
|
+
const tmpBin = path.join(vendorDir, `.sm.tmp.${nonce}`);
|
|
405
|
+
const tmpVer = path.join(vendorDir, `.version.tmp.${nonce}`);
|
|
406
|
+
try {
|
|
407
|
+
fs.writeFileSync(tmpBin, binaryBytes, { mode: 0o755 });
|
|
408
|
+
fs.chmodSync(tmpBin, 0o755);
|
|
409
|
+
fs.renameSync(tmpBin, vendorBin);
|
|
410
|
+
fs.writeFileSync(tmpVer, `${version}\n`, { mode: 0o644 });
|
|
411
|
+
fs.renameSync(tmpVer, vendorVersionPath);
|
|
412
|
+
} finally {
|
|
413
|
+
unlinkQuietly(tmpBin);
|
|
414
|
+
unlinkQuietly(tmpVer);
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
return 0;
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
if (require.main === module) {
|
|
421
|
+
runPostinstall().then(
|
|
422
|
+
(code) => {
|
|
423
|
+
process.exit(code);
|
|
424
|
+
},
|
|
425
|
+
(err) => {
|
|
426
|
+
process.stderr.write(
|
|
427
|
+
`simple-memory: postinstall error: ${err && err.message ? err.message : String(err)}\n`
|
|
428
|
+
);
|
|
429
|
+
process.exit(1);
|
|
430
|
+
}
|
|
431
|
+
);
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
module.exports = {
|
|
435
|
+
DEFAULT_TIMEOUT_MS,
|
|
436
|
+
MAX_DOWNLOAD_BYTES,
|
|
437
|
+
MAX_UNCOMPRESSED_BYTES,
|
|
438
|
+
parseSha256Sums,
|
|
439
|
+
readStreamWithLimit,
|
|
440
|
+
extractBinaryFromTarGz,
|
|
441
|
+
runPostinstall,
|
|
442
|
+
};
|