Three intertwined fixes for the torn-write that corrupted the prod
data/chat-cache.json mid-session:
- ChatCache.save / persistCooldown now write via temp file + rename.
rename(2) is atomic on the same filesystem, so a crashed or
concurrent writer can never leave NULs in the middle of the file.
A small serialising write queue stops two concurrent saves from
racing to the same temp name.
- ChatCache throws if constructed without a filePath under NODE_ENV=test.
The old default (./data/chat-cache.json) silently routed test
writes into the production cache — exactly how the corruption
happened. Future test that forgets to pass a tmp path fails loudly
instead of stomping data.
- Cooldown / delta-media / username-persist tests now pass an
explicit tmp ChatCache so the new guard is satisfied.
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
Adds an inline subreddit editor on the dashboard that lets you add,
remove, and edit (tone, type, weight) the subreddit list, with changes
persisted to config.yaml on disk so they survive restarts.
Backend
- src/config/persistence.ts: round-trips config.yaml via
YAML.parseDocument, mutating existing row nodes in place so their
trailing inline `# ...` comments survive an edit; new rows are
appended as flow-style maps to match the on-disk idiom; removed rows
are dropped. Default values ('default' tone, 'image' type) are
omitted on write so the file stays clean. Atomic via tmp+rename.
- src/web/routes/config.ts: GET /api/config now also returns the
current subreddit list and the available tone keys. PUT /api/config
accepts a `subreddits` field, validates with the existing
SubredditConfigSchema, rejects duplicates, persists, then mutates
ctx.config.reddit.subreddits in place so the processor picks up the
new tone immediately without a restart.
Frontend
- public/index.html: replaces the read-only subreddit chip list in the
Configuration snapshot section with a grid of editable rows (name,
type, tone dropdown, weight, remove). + Add / Save buttons with
inline status. Tones are sourced from /api/config so the catalog
drives the dropdown.
Tests
- tests/config-persistence.test.ts: covers in-place mutation comment
preservation, removal, appends, reorder, default omission, and
atomic write cleanup.
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
Introduces a tone-profile system that lets the processor adapt outgoing
message style per-subreddit (and per-flair) instead of using one tone
for everything. Profiles act as a "tone mic" layered after the base
prompt and strategy override, so they shape voice without redefining
the whole message.
- src/config/tone-profiles.ts defines the catalog (default, dominant,
submissive, playful, direct, ...) and a resolveTone() helper that
falls back to the default profile for unknown keys.
- config.yaml maps subreddits to tone keys; the schema in
src/config/index.ts validates it.
- src/services/processor.ts threads the resolved tone into the prompt
build, picking based on subreddit and optional link flair.
- src/reddit/api.ts adds link_flair_text to RedditPost so the
processor can use it for tone selection.
- src/ai/openrouter.ts accepts and forwards the tone-shaped prompt.
- public/index.html surfaces the active tone in the dashboard.
- Tests cover the catalog, config validation, and processor integration.
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
Adds a durable mapping so the UI never has to show raw t2_xxx IDs or
MXIDs just because the in-memory cache is cold after a restart.
ChatService seeds from the table on construction and writes through
every resolution path; cached chats whose username is still t2_xxx
get upgraded from the DB on loadCache.
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
Threads MessagesRepository through ChatService so first outgoing chat
messages are persisted for stats, and extends the repo + DB client to
support chat-room-keyed lookups. Adds tests covering the new behavior.
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>