diff --git a/.claude-plugin/marketplace.json b/.claude-plugin/marketplace.json index b5b0265..7742924 100644 --- a/.claude-plugin/marketplace.json +++ b/.claude-plugin/marketplace.json @@ -10,7 +10,7 @@ "name": "past", "source": "./plugins/past", "description": "Your Claude Code sessions become memory, and every session starts briefed.", - "version": "0.2.0", + "version": "0.2.1", "author": { "name": "past.dev", "url": "https://past.dev" diff --git a/CLAUDE.md b/CLAUDE.md index dd363d4..d9f9290 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -34,6 +34,7 @@ Claude Code .claude-plugin/marketplace.json the catalog — this repository is the marketplace plugins/past/ .claude-plugin/plugin.json the manifest, including the settings form (userConfig) + .claude-plugin/icon.png the directory listing's icon, taken once at the first save hooks/hooks.json SessionStart, UserPromptSubmit, Stop, PreCompact, SessionEnd .mcp.json past_recall: the script's mcp mode, started by Claude Code bin/past-hook.mjs the whole connector, one file, no dependencies @@ -43,6 +44,11 @@ plugins/past/ .github/workflows/check.yml every pull request: the script parses and runs, the manifests validate ``` +Credit refusals persist per operation in this host's state book, under `creditPauses`. +Each pause stores a connection fingerprint, required credits, and the next balance check time. +Use `updateState` for every change. Keep transcript hashes and pending sessions intact on a refusal. +`status --refresh` reads `/api/v1/usage` before it clears a pause. A write pause leaves recall available. + ## The rules this code keeps 1. **A hook never breaks the session.** Every mode exits 0, prints valid JSON or nothing, and diff --git a/plugins/past/.claude-plugin/icon.png b/plugins/past/.claude-plugin/icon.png new file mode 100644 index 0000000..7db7f9a Binary files /dev/null and b/plugins/past/.claude-plugin/icon.png differ diff --git a/plugins/past/.claude-plugin/plugin.json b/plugins/past/.claude-plugin/plugin.json index 73838b9..84d64e1 100644 --- a/plugins/past/.claude-plugin/plugin.json +++ b/plugins/past/.claude-plugin/plugin.json @@ -1,14 +1,15 @@ { "name": "past", "displayName": "past.dev", - "version": "0.2.0", + "version": "0.2.1", "description": "Your Claude Code sessions become memory, and every session starts briefed.", "author": { "name": "past.dev", "url": "https://past.dev" }, "homepage": "https://github.com/pastdotdev/claude-code-plugin", "repository": "https://github.com/pastdotdev/claude-code-plugin", "license": "MIT", + "privacyPolicyUrl": "https://past.dev/privacy", + "icon": "./.claude-plugin/icon.png", "keywords": ["memory", "recall", "context"], - "hooks": "./hooks/hooks.json", "mcpServers": "./.mcp.json", "commands": ["./commands/connect.md", "./commands/status.md", "./commands/cut.md", "./commands/ingest.md", "./commands/backfill.md"], "skills": "./skills/", diff --git a/plugins/past/README.md b/plugins/past/README.md index 331f573..d95c80e 100644 --- a/plugins/past/README.md +++ b/plugins/past/README.md @@ -149,3 +149,12 @@ console. ## License MIT — see [LICENSE](LICENSE). + +## Credits + +After `402 out-of-credits`, the plugin pauses the refused operation and keeps unsent data locally. +A write refusal does not pause recall. A recall refusal pauses recall separately. +On later calls, the plugin checks the balance at most once per five minutes for each paused operation. +The plugin resumes when the balance covers the refused call. The server checks the balance again for each request. +Run `/past:status --refresh` after you add credits to check the balance now. +A new API key or API URL starts with no pause from the previous connection. diff --git a/plugins/past/bin/past-hook.mjs b/plugins/past/bin/past-hook.mjs index ed8e7d1..a8068c2 100755 --- a/plugins/past/bin/past-hook.mjs +++ b/plugins/past/bin/past-hook.mjs @@ -458,8 +458,51 @@ async function request(url, options) { // Every call is time-boxed. A slow network must never hold a prompt, so the caller's budget is // the hook's budget, and an expired call simply returns nothing. +// Each operation pauses independently. Free reads remain available after a write refusal. +const CREDIT_CHECK_MS = 5 * 60 * 1000; +const creditAccount = (config) => hashOf(`${config.apiUrl}\n${config.apiKey}`); +function creditPause(config, operation) { + const pauses = book(loadState()).creditPauses; + const pause = pauses && pauses[operation]; + return pause && pause.account === creditAccount(config) ? pause : null; +} + +async function checkCredits(config, operation, timeoutMs, force = false) { + const pause = creditPause(config, operation); + if (!pause) return true; + const now = Date.now(); + let claimed = false; + updateState((fresh) => { + const own = book(fresh); + const current = own.creditPauses && own.creditPauses[operation]; + if (current && current.account === pause.account && (force || current.nextCheck <= now)) { + current.nextCheck = now + CREDIT_CHECK_MS; + claimed = true; + } + }); + if (!claimed) return false; + const usage = await api(config, '/api/v1/usage', null, timeoutMs, 'GET'); + if (!usage || usage.error) return false; + const available = usage.metered === false || (usage.balance && + typeof usage.balance.left === 'number' && usage.balance.left >= pause.needed); + if (!available) return false; + updateState((fresh) => { + const own = book(fresh); + const current = own.creditPauses && own.creditPauses[operation]; + if (current && current.account === pause.account && current.at === pause.at) + delete own.creditPauses[operation]; + }); + return !creditPause(config, operation); +} + async function api(config, path, body, timeoutMs, method = 'POST') { if (!config.apiKey) return null; + const operation = method === 'POST' && ['/api/v1/ingest', '/api/v1/ingest/batch'].includes(path) ? 'ingest' + : method === 'POST' && path === '/api/v1/recall' ? 'recall' : null; + const started = Date.now(); + if (operation && !await checkCredits(config, operation, Math.min(timeoutMs, 2000))) + return { error: 402, body: JSON.stringify({ code: 'out-of-credits' }), paused: true }; + timeoutMs = Math.max(1, timeoutMs - (Date.now() - started)); const control = new AbortController(); const timer = setTimeout(() => control.abort(), timeoutMs); try { @@ -473,7 +516,23 @@ async function api(config, path, body, timeoutMs, method = 'POST') { body: body === null ? undefined : JSON.stringify(body), signal: control.signal, }); - if (!response.ok) return { error: response.status, body: await response.text().catch(() => '') }; + if (!response.ok) { + const text = await response.text().catch(() => ''); + let refusal; + try { refusal = JSON.parse(text); } catch { /* Keep the HTTP error. */ } + if (operation && response.status === 402 && refusal && refusal.code === 'out-of-credits') { + const needed = refusal.data && refusal.data.creditsNeeded; + updateState((fresh) => { + const own = book(fresh); + if (!own.creditPauses) own.creditPauses = {}; + own.creditPauses[operation] = { + account: creditAccount(config), at: Date.now(), nextCheck: Date.now() + CREDIT_CHECK_MS, + needed: typeof needed === 'number' && needed > 0 ? needed : 1, + }; + }); + } + return { error: response.status, body: text }; + } return await response.json().catch(() => ({})); } catch { return null; @@ -1255,13 +1314,18 @@ async function modeSearch(config, argv) { if (!config.apiKey) { console.log(`Not connected. Run ${commandOf('connect')} first.`); return; } if (!config.identity) { console.log(`No identity set. Run ${commandOf('connect')} to set one.`); return; } const memories = await recall(config, query, 8, 15000); + if (creditPause(config, 'recall')) { console.log(`Recall paused: insufficient credits. Run ${commandOf('status')} --refresh after you add credits.`); return; } if (!memories.length) { console.log('Nothing in past.dev matches that yet.'); return; } for (const [index, memory] of memories.entries()) { console.log(`[${index + 1}] ${(memory.at || '').slice(0, 10)} — ${memory.content.replace(/\s+/g, ' ').trim()}`); } } -function modeStatus(config) { +async function modeStatus(config, argv) { + if (argv.includes('--refresh')) { + for (const operation of ['ingest', 'recall']) + await checkCredits(config, operation, 2000, true); + } // Heals before it reports: a session left overdue by a dead or outdated waiter is sent now. try { ensureWaiters(); } catch { /* status still reports */ } const state = updateState((fresh) => tidyPending(fresh, config, '')); @@ -1299,6 +1363,10 @@ function modeStatus(config) { } lines.push(`Idle timer ${parts.join('; ')}`); } + for (const operation of ['ingest', 'recall']) { + if (creditPause(config, operation)) + lines.push(`Credits ${operation} paused. Unsent data stays local. Run ${commandOf('status')} --refresh after you add credits.`); + } const { lastError } = book(state); if (lastError) { const at = new Date(lastError.at); @@ -1410,7 +1478,7 @@ try { else if (mode === 'idle') await modeIdle(config, argv); else if (mode === 'flush') await modeFlush(config, argv); else if (mode === 'precompact') await modePreCompact(config); - else if (mode === 'status') modeStatus(config); + else if (mode === 'status') await modeStatus(config, argv); else if (mode === 'cut') modeCut(config, argv); else if (mode === 'search') await modeSearch(config, argv); else if (mode === 'backfill') await modeBackfill(config, argv); diff --git a/plugins/past/commands/status.md b/plugins/past/commands/status.md index 6fdaa1d..b6f67b6 100644 --- a/plugins/past/commands/status.md +++ b/plugins/past/commands/status.md @@ -6,3 +6,6 @@ Run `node "${CLAUDE_PLUGIN_ROOT}/bin/past-hook.mjs" status` and show the output If the key or the identity is missing, say which one and point at `/past:connect`. Do not print the key itself beyond the prefix the command already shows. + +If the user asks to check credits now, add `--refresh` to the status command. +This option reads the balance from the API and clears a credit pause only when the balance is sufficient.