Co-authored-by: David Gwilliam <dhgwilliam@gmail.com> Co-committed-by: David Gwilliam <dhgwilliam@gmail.com>
This commit was merged in pull request #38.
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
# knox.maintenance — OpenCode plugin
|
||||
|
||||
Rolling retention for the knowledge index, driven by use instead of a timer.
|
||||
|
||||
## Why
|
||||
|
||||
Snippet candidates pile up: every successful `write`/`edit`/`shell` call is
|
||||
extracted as a candidate, and only a fraction ever gets curated. Without
|
||||
retention the queue grows until it is unusable — at one point 17,262 candidates
|
||||
against 25 encoded.
|
||||
|
||||
A systemd timer would work, but it is a second thing to install, it fires on an
|
||||
idle machine, and it duplicates the scheduling knox already needs to know about.
|
||||
Curating while the machine is in use is both simpler and more useful.
|
||||
|
||||
## How it works
|
||||
|
||||
`setup` registers a `prompt` session hook. On the first prompt of the session
|
||||
(and at most once every 24 hours in-process) the plugin runs:
|
||||
|
||||
```sh
|
||||
knox snippets expire --days 7 --every 24h
|
||||
```
|
||||
|
||||
Fire-and-forget: output is discarded and the prompt is never delayed.
|
||||
|
||||
## Where the clock lives
|
||||
|
||||
In the index, not in the plugin: `knox snippets expire --every` records its last
|
||||
run in `settings['snippets_expire_last_run']` and no-ops while the interval has
|
||||
not elapsed. So:
|
||||
|
||||
- restarting OpenCode reloads this module but cannot cause a burst of sweeps;
|
||||
- a cron job or a human running the same command shares the same schedule;
|
||||
- the in-process `lastAttempt` guard is only an optimisation, to avoid spawning
|
||||
a CLI that would immediately decide it is not due.
|
||||
|
||||
## Configuration
|
||||
|
||||
| Variable | Default | Meaning |
|
||||
|---|---|---|
|
||||
| `KNOX_SNIPPET_RETENTION_DAYS` | `7` | How long an uncurated candidate stays reviewable |
|
||||
| `KNOX_BIN` | `~/assistant/knox/knox` | Binary to invoke |
|
||||
|
||||
## What it never does
|
||||
|
||||
- It never touches `encoded` snippets; only uncurated `candidate` rows expire.
|
||||
- Nothing is deleted. Expiry is a status change with a reason, reversible with
|
||||
`knox snippets reopen` (or `--scope techniques` for every rejected technique).
|
||||
@@ -0,0 +1,62 @@
|
||||
// NOTE: no import from "@opencode/plugin". A local plugin loaded by path cannot
|
||||
// resolve that bare specifier — identical code without the import loads, with
|
||||
// it the plugin fails with "Plugin failed to load" (see knox.claims).
|
||||
import { execFile } from "node:child_process"
|
||||
import { existsSync } from "node:fs"
|
||||
|
||||
/**
|
||||
* knox.maintenance — rolling housekeeping for the knowledge index.
|
||||
*
|
||||
* Curation has to happen on a schedule, but knox is not a daemon for it: the
|
||||
* watcher owns ingestion, not retention. Rather than a systemd timer, this
|
||||
* rides the same `prompt` hook knox.claims uses, so the sweep happens while the
|
||||
* machine is actually in use and nothing has to be installed alongside it.
|
||||
*
|
||||
* The schedule itself lives in the index (`settings['snippets_expire_last_run']`,
|
||||
* enforced by `knox snippets expire --every`). That matters: this plugin is one
|
||||
* caller among several — a cron job or a human running the same command shares
|
||||
* one clock, and restarting OpenCode (which reloads this module) cannot cause a
|
||||
* burst of sweeps. The in-process guard below only avoids spawning a CLI that
|
||||
* would immediately decide it is not due.
|
||||
*/
|
||||
|
||||
// The OpenCode server does not inherit the service's PATH, so prefer the
|
||||
// checkout binary when it exists and fall back to PATH lookup.
|
||||
const DEFAULT_BIN = `${process.env.HOME ?? ""}/assistant/knox/knox`
|
||||
const BIN = process.env.KNOX_BIN ?? (existsSync(DEFAULT_BIN) ? DEFAULT_BIN : "knox")
|
||||
|
||||
/** How long an uncurated snippet candidate stays reviewable. */
|
||||
const RETENTION_DAYS = process.env.KNOX_SNIPPET_RETENTION_DAYS ?? "7"
|
||||
/** Minimum gap between sweeps; knox enforces the same value authoritatively. */
|
||||
const EVERY = "24h"
|
||||
const EVERY_MS = 24 * 60 * 60 * 1000
|
||||
const EXPIRE_TIMEOUT_MS = 120_000
|
||||
|
||||
let lastAttempt = 0
|
||||
|
||||
/**
|
||||
* Expire candidates that fell out of the review window, and quiet the log while
|
||||
* doing it: this runs in the background of a user prompt, not as a report.
|
||||
*/
|
||||
function maintain(): void {
|
||||
const now = Date.now()
|
||||
if (now - lastAttempt < EVERY_MS) return
|
||||
lastAttempt = now
|
||||
execFile(
|
||||
BIN,
|
||||
["snippets", "expire", "--days", RETENTION_DAYS, "--every", EVERY],
|
||||
{ timeout: EXPIRE_TIMEOUT_MS },
|
||||
() => {},
|
||||
)
|
||||
}
|
||||
|
||||
export default {
|
||||
id: "knox.maintenance",
|
||||
async setup(ctx: any) {
|
||||
// Prompt admission is the natural place: the index is already being
|
||||
// refreshed here by knox.claims, and the sweep is fire-and-forget.
|
||||
await ctx.session.hook("prompt", () => {
|
||||
maintain()
|
||||
})
|
||||
},
|
||||
}
|
||||
Reference in New Issue
Block a user