Metadata-Version: 2.4
Name: hindsight-stash
Version: 0.1.0
Summary: A Hindsight memory bank as a Strands Stash source: its knowledge pages and its observations.
Author: Hindsight
License-Expression: MIT
Project-URL: Homepage, https://github.com/vectorize-io/hindsight-stash
Project-URL: Issues, https://github.com/vectorize-io/hindsight-stash/issues
Project-URL: Hindsight, https://hindsight.vectorize.io
Keywords: hindsight,strands,stash,memory,agents,claude-code,codex
Classifier: Programming Language :: Python :: 3
Classifier: Operating System :: POSIX
Classifier: Topic :: Software Development
Requires-Python: >=3.10
Description-Content-Type: text/markdown
License-File: LICENSE
License-File: LICENSE-APACHE
Dynamic: license-file

# Hindsight for Strands Stash

Give your coding agent what [Hindsight](https://hindsight.vectorize.io) knows about your
project, as plain files. This plugin mounts one Hindsight memory bank in
[Strands Stash](https://github.com/strands-agents/stash). The agent reads it with `ls`, `cat`
and `grep`, and each of its sessions goes back to the bank, so the next session knows
more.

The mount has two folders:

| Folder | What is in it |
|---|---|
| `pages/` | The bank's knowledge pages, in their folders. Each page answers one question about the project, and Hindsight keeps it current. |
| `memories/` | The bank's observations, grouped by topic, with an `index.md` overview in each folder. The links between the files open. |

`stash search` finds pages by meaning and by keyword, and files in `memories/` that
hold the words of the query. A search also brings in a page or a topic that the bank
learned after the mount started.

## Requirements

- [Strands Stash](https://github.com/strands-agents/stash#install) 0.1.0 or later, with a harness
  that it supports: Claude Code, Codex, or Kiro.
- Python 3.10 or later, and [pipx](https://pipx.pypa.io).
- A Hindsight server: a [Hindsight Cloud](https://ui.hindsight.vectorize.io/signup)
  account and an API key, or a self-hosted server.

## Install

```sh
pipx install hindsight-stash
command -v stash-source-hindsight
```

Stash runs the program `stash-source-hindsight` from the `PATH` of its daemon. pipx
puts it in `~/.local/bin`. If `command -v` finds nothing, run `pipx ensurepath` and open
a new terminal.

## Upgrade

Upgrade the plugin, then restart the Stash daemon, so that it starts the new version:

```sh
pipx upgrade hindsight-stash
stash daemon stop && stash daemon start
```

[PyPI](https://pypi.org/project/hindsight-stash/#history) lists each version.
The daemon keeps the files of each mount across the restart.

To remove the plugin: `pipx uninstall hindsight-stash`.

## Configure

Add a source of kind `plugin` to a Stash config file:

```toml
mount_point = "/tmp/hindsight-mount"
harnesses = ["claude"]

[sources.project]
kind = "plugin"
plugin = "hindsight"
description = "What Hindsight knows about this project"
settings = { bank = "my-project" }
env = { HINDSIGHT_API_KEY = { from_env = "HINDSIGHT_API_KEY" } }
write.trajectory = {}
```

The files show under `<mount_point>/plugin/<source name>/`, here
`/tmp/hindsight-mount/plugin/project/`.

| `settings` key | Required | Default | Description |
|---|---|---|---|
| `bank` | yes | | The id of the memory bank. |
| `url` | no | `https://api.hindsight.vectorize.io` | The Hindsight API, for a self-hosted server. It must be `https`, except on `localhost`. |
| `actor` | no | `stash` | Who writes the sessions. It goes in the `actor:` tag of each document. |

- `env` gives the plugin its API key. `from_env` reads the variable from the
  environment of the command that starts the Stash daemon. You can also read it from a
  file, `{ file = "/path/to/key" }`, or a command, `{ command = ["op", "read", "..."] }`.
  For a server with no authentication, leave `env` out.
- `write.trajectory = {}` sends each session of the agent to the bank. Leave it out
  for a mount that only reads.
- `harnesses` is `claude`, `codex`, or `kiro`.

The [Stash configuration reference](https://github.com/strands-agents/stash/blob/main/docs/user/configuration.md)
has the other keys of a mount.

### Which bank to use

- **A new bank, to try it.** Any name works. The trial below makes one.
- **The bank of the Hindsight coding-agents plugin.** It names each bank after the git
  repository, `coding-agent::<repo>`, for example `coding-agent::my-service`. Use the
  same name to read what it learned. If the coding-agents plugin also runs, it
  writes each session too: set `"retainSessions": false` in its config, so each session
  goes to the bank one time.

## Try it

This trial takes about 10 minutes. It makes a bank with some facts about an example
project, mounts it, and asks Claude Code a question that only the bank can answer.

### 1. Put some knowledge in a new bank

```sh
export HINDSIGHT_API_KEY=<your API key>
export HS=https://api.hindsight.vectorize.io/v1/default/banks/stash-trial

for fact in \
  "Kestrel must be deployed with make ship-kestrel, never with kubectl, so the migration checks run first." \
  "Kestrel deploys are frozen every Thursday after 14:00 UTC for the weekly release review." \
  "A failed Kestrel deploy is rolled back by the on-call engineer with make rollback-kestrel REV=<previous>." \
  "When vLLM runs out of GPU memory on Kestrel, lower max_num_seqs from 64 to 32. Keep the 16k context."
do
  curl -s -X POST "$HS/memories" \
    -H "Authorization: Bearer $HINDSIGHT_API_KEY" -H "Content-Type: application/json" \
    -d "{\"items\": [{\"content\": \"$fact\"}]}"
  echo
done

curl -s -X POST "$HS/pages" \
  -H "Authorization: Bearer $HINDSIGHT_API_KEY" -H "Content-Type: application/json" \
  -d '{"name": "Deploying Kestrel", "source_query": "How do we deploy Kestrel, when are deploys frozen, and how do we roll back?"}'
```

Hindsight now extracts the facts, groups them into observations, and writes the page.
Wait 2 or 3 minutes, until the page has text:

```sh
curl -s "$HS/pages/export" -H "Authorization: Bearer $HINDSIGHT_API_KEY" | grep -c ship-kestrel
```

A number above 0 means that it is ready.

### 2. Mount the bank

Make an empty project folder, and save this as `stash.toml` in it:

```toml
mount_point = "/tmp/hindsight-mount"
harnesses = ["claude"]

[sources.kestrel]
kind = "plugin"
plugin = "hindsight"
description = "What Hindsight knows about the Kestrel project"
settings = { bank = "stash-trial" }
env = { HINDSIGHT_API_KEY = { from_env = "HINDSIGHT_API_KEY" } }
write.trajectory = {}
```

In the same terminal, so that the daemon gets `HINDSIGHT_API_KEY`:

```sh
mkdir -p ~/kestrel-trial && cd ~/kestrel-trial   # with stash.toml in it
git init -q
stash daemon stop 2>/dev/null   # a daemon that runs already has no API key
stash create kestrel --config stash.toml --attach
stash ls                        # wait until kestrel is ready
find /tmp/hindsight-mount/plugin/kestrel -type f
```

You see the page and the observations:

```text
/tmp/hindsight-mount/plugin/kestrel/pages/deploying-kestrel.md
/tmp/hindsight-mount/plugin/kestrel/memories/index.md
/tmp/hindsight-mount/plugin/kestrel/memories/<topic>.md
```

Search the bank:

```sh
stash search kestrel "roll back a failed deploy" --path /plugin/kestrel
```

The page comes first, then the memory files that hold the words.

### 3. Ask the agent

Start Claude Code in `~/kestrel-trial`, and ask:

> For the Kestrel project: when are deploys frozen, how do I roll back a failed deploy,
> and what should I do when vLLM runs out of GPU memory? Name each file you read.

The agent reads the mount, and answers: Thursday after 14:00 UTC,
`make rollback-kestrel REV=<previous>`, and lower `max_num_seqs` from 64 to 32. It
names files under `/tmp/hindsight-mount/plugin/kestrel/`.

### 4. See the session in the bank

When the agent finishes its turn, Stash sends the session to the bank. After a minute:

```sh
curl -s "$HS/documents?limit=100" -H "Authorization: Bearer $HINDSIGHT_API_KEY" | grep -o '"id":"stash::[^"]*"'
```

You see one document, `stash::<session id>`. Hindsight makes new observations from it,
so a later session can find what this one learned. The
[Hindsight Cloud console](https://ui.hindsight.vectorize.io) shows the bank too.

### 5. Clean up

```sh
stash delete kestrel --remove-data --yes
curl -s -X DELETE "$HS" -H "Authorization: Bearer $HINDSIGHT_API_KEY"
```

## How it works

- **Reads.** One download of the page tree, the pages, and the observation tree answers
  every read for 60 seconds. When Stash refreshes the files of a mount, one round costs
  three calls to Hindsight. A file that the download does not have makes one more
  download, at most once in 5 seconds.
- **Write-back.** Each session goes to the bank as an async retain of its transcript,
  in the shape that the Hindsight coding-agents plugin uses: the `conversation`
  strategy, with the tags `source:chat`, `harness:stash`, and `actor:<actor>`. On
  Hindsight 0.8.6 and later, a session is one document, `stash::<session>`, and each
  batch adds to it. On an older server, or a bank with `store_document_text = false`,
  each batch is its own document, so that a batch that Stash sends again is not stored
  two times.

## Limits

- Stash reads the list of files only when the mount starts. A page or a topic that the
  bank gets later shows when a search finds it, or after `stash delete NAME --remove-data --yes`
  and `stash create`.
- A search of `memories/` matches words, not meaning.
- Two pages in one folder whose titles give the same file name both show, and Stash adds
  an id to the second name.

## Troubleshooting

- **The mount shows no files.** Run `stash status <mount>`. The page
  `<mount_point>/_sources/<source>.md` says why the source failed. The Stash daemon logs are in
  `~/.stash/logs/daemon/`, one file a day.
- **`access_denied`.** The daemon has no API key, or a wrong one. Stop the daemon, export
  `HINDSIGHT_API_KEY`, and run `stash create` again.
- **The plugin is not found.** The daemon's `PATH` has no `stash-source-hindsight`. Run
  `pipx ensurepath`, or give the full path in the source:
  `command = ["/home/me/.local/bin/stash-source-hindsight"]`.

## Develop

```sh
git clone https://github.com/vectorize-io/hindsight-stash && cd hindsight-stash
python3 -m venv .venv
.venv/bin/pip install -e .
.venv/bin/python -m unittest discover -s tests
```

`src/stash_source_hindsight/_sdk.py` is a copy of the Stash plugin SDK. Copy it again
from the Stash repository to upgrade it.

## License

MIT. `_sdk.py` is from Stash, under the Apache License 2.0 (`LICENSE-APACHE`).
