Files
OwnCord/Server/plugin/host_storage.go
T
Claude 5f1d6fc287 refactor(server): remove the store abstraction layer (D3)
Deletes Server/store (SQLiteStore, MemStore, the composed Store
interface) and collapses to a single sqlc-backed db package, executing
the prior audit's P4 "single data layer" direction (finding #6).

SQLiteStore was a pure pass-through to *db.DB, so consumers now depend
on narrow interfaces that *db.DB satisfies directly:

  - service.Store   (service/datastore.go, renamed from store/store.go)
  - ws.EventStore   (ws/eventstore.go)
  - plugin.PluginStore (plugin/pluginstore.go)

The event- and plugin-KV methods that lived in the store's SQLite
implementation move into the db package (db/event_queries.go,
db/plugin_queries.go), keeping their raw-SQL form.

Tests: the MemStore-based unit tests now run against a real in-memory
SQLite db opened per-test with migrations applied, via package-local
seed helpers. Fault-injection tests embed a real *db.DB and override the
single method under test, preserving error-path coverage. Full server
suite and sqlc-verify are green.

Docs: audit finding #6 and A-2026-07-06 marked resolved; decisions D3
updated; architecture server.md / data-model.md diagrams and prose
updated to the api -> service -> db layering.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01UA17KPvqGBX3XbXYnMf1rA
2026-07-19 16:33:58 +00:00

56 lines
1.8 KiB
Go

// Phase C Step 9 — `storage` host capability.
//
// Plugins get a per-plugin namespaced KV store backed by the PluginStore
// rows in the events/plugin schema. Capacity caps and value-size caps are
// enforced here so a misbehaving plugin can't fill the database.
package plugin
import (
"context"
"fmt"
)
const (
maxPluginValueBytes = 64 * 1024 // 64 KB per value
maxPluginScanLimit = 1000 // hard cap on PluginKVScan results
)
// StoragePut writes a single key/value pair on behalf of inst.
func (r *Registry) StoragePut(ctx context.Context, inst *Instance, key string, value []byte) error {
if !inst.Manifest.HasCapability(CapStorage) {
return ErrCapabilityNotGranted
}
if len(value) > maxPluginValueBytes {
return fmt.Errorf("plugin storage: value exceeds %d bytes", maxPluginValueBytes)
}
return r.cfg.Store.PluginKVSet(ctx, inst.ID, key, value)
}
// StorageGet returns the value for key, or (nil, error) when missing.
func (r *Registry) StorageGet(ctx context.Context, inst *Instance, key string) ([]byte, error) {
if !inst.Manifest.HasCapability(CapStorage) {
return nil, ErrCapabilityNotGranted
}
return r.cfg.Store.PluginKVGet(ctx, inst.ID, key)
}
// StorageDelete removes a key.
func (r *Registry) StorageDelete(ctx context.Context, inst *Instance, key string) error {
if !inst.Manifest.HasCapability(CapStorage) {
return ErrCapabilityNotGranted
}
return r.cfg.Store.PluginKVDelete(ctx, inst.ID, key)
}
// StorageScan returns all keys with the given prefix, capped at maxPluginScanLimit.
func (r *Registry) StorageScan(ctx context.Context, inst *Instance, prefix string, limit int) (map[string][]byte, error) {
if !inst.Manifest.HasCapability(CapStorage) {
return nil, ErrCapabilityNotGranted
}
if limit <= 0 || limit > maxPluginScanLimit {
limit = maxPluginScanLimit
}
return r.cfg.Store.PluginKVScan(ctx, inst.ID, prefix, limit)
}