Files
plugins/packages/mail-watch/paths.js
T
mola ea9201df1f mail-watch 0.1.0: mail session ends, failed turns and refusals through your own SMTP (#457)
mail-watch 0.1.0: a hooks plugin that mails a rendered event (per event or as a digest) when a session ends, a turn fails, or a request is refused, through the user's own SMTP server.

The digest's due time is kept in the state file (digestAt/digestHours), so a restart neither resets nor fires it early; the timer step is capped at 2^31-1 ms and an early wake re-arms for the rest instead of sending.

Run at the reviewed head 8415dd9eb (sandbox HOME):
- mote: packages/mail-watch `bun test` 44 pass, 0 fail, three times; whole repo `bun test` 2982 pass, 8 skip, 0 fail; `bun scripts/check.mjs` exit 0; e2e.smoke.mjs against a local SMTP sink: onRequest returns undefined, onResponse returns without awaiting the mail, the mail arrives with the rendered event and subject.
- Linux (WSL2 Ubuntu 24.04): packages/mail-watch `bun test` 44 pass, 0 fail.
- Broken by hand, each red: no step cap (2 red: monthly digest, early wake), no due-time-ahead check (early wake), no persisted due time (restart test), no prune, raw guard, no shrink (1 red each). Making the persist write unconditional stayed green, as expected (a cost fix only).
- CI at this head: test and on-npm green.

Not tried: a real SMTP provider (Gmail, Fastmail); the smoke test uses a local sink.
2026-10-29 13:40:31 +08:00

60 lines
2.7 KiB
JavaScript

// Where magpie keeps its own files, decided in exactly one place —
// watch.js and digest.js used to each keep their own rule, and they had
// drifted (one read XDG_CONFIG_DIR, which magpie never reads; neither
// honoured a portable install).
//
// magpie's own rule (internal/appdir): a portable install — a "data"
// folder, or a ".portable" marker, beside the magpie executable — keeps
// everything in that data folder; an installed one keeps them in
// $XDG_CONFIG_HOME/magpie, else ~/.config/magpie. The hook's context
// carries no config dir, so this module finds it another way: a plugin
// installed from the registry lives under the config dir's plugins/
// folder, so walking up from this file to the nearest folder holding one
// of magpie's own files (usage.jsonl, quotas.json, providers.json,
// plugins.json) lands on the config dir — portable or installed, with
// no need to know where the executable is. A locally developed plugin
// (its files outside any config dir) finds no marker and falls back to
// the installed rule, which is what a dev install reads.
//
// MAGPIE_CONFIG_DIR is the test override (it names the parent, the way
// XDG_CONFIG_HOME does: the dir below it is "magpie"). Production never
// sets it.
import { existsSync } from "node:fs"
import { join, dirname, isAbsolute } from "node:path"
import { homedir } from "node:os"
// MARKERS are files magpie itself writes at the root of its config dir:
// usage.jsonl (every call's row), quotas.json (the last readings),
// providers.json (the sign-ins' file), plugins.json (the plugin list —
// present from the moment a registry plugin is installed, before any
// usage exists).
const MARKERS = ["usage.jsonl", "quotas.json", "providers.json", "plugins.json"]
// anchorDir walks up from this package's own folder to the config dir
// that holds it, or null when the package lives outside one.
function anchorDir() {
const here = import.meta.dir ?? import.meta.dirname
if (!here) return null
let dir = here
for (let i = 0; i < 32; i++) {
if (MARKERS.some((m) => existsSync(join(dir, m)))) return dir
const up = dirname(dir)
if (up === dir) return null
dir = up
}
return null
}
export function paths() {
const testRoot = process.env.MAGPIE_CONFIG_DIR
if (testRoot) {
const dir = join(testRoot, "magpie")
return { dir, state: join(dir, "mail-watch-state.json"), usage: join(dir, "usage.jsonl"), quotas: join(dir, "quotas.json") }
}
const anchored = anchorDir()
const xdg = process.env.XDG_CONFIG_HOME
const dir = anchored ?? (xdg && isAbsolute(xdg) ? join(xdg, "magpie") : join(homedir(), ".config", "magpie"))
return { dir, state: join(dir, "mail-watch-state.json"), usage: join(dir, "usage.jsonl"), quotas: join(dir, "quotas.json") }
}