Skip to content

refactor(shared): consolidate session storage into generic factory#2715

Open
iyoda wants to merge 1 commit intocode-yeongyu:devfrom
iyoda:refactor/session-storage-abstraction
Open

refactor(shared): consolidate session storage into generic factory#2715
iyoda wants to merge 1 commit intocode-yeongyu:devfrom
iyoda:refactor/session-storage-abstraction

Conversation

@iyoda
Copy link
Copy Markdown
Contributor

@iyoda iyoda commented Mar 20, 2026

Summary

  • Extract duplicated CRUD logic from 5 session storage implementations into a single createSessionStorage<TState, TSerialized> factory
  • Function overloads provide conditional return types (TState | null vs TState) based on defaultValue presence
  • serialize/deserialize hooks handle custom transformations (Set↔Array, property name remapping)
  • structuredClone(defaultValue) prevents shared mutable reference bugs across sessions
  • All external API signatures preserved — zero consumer-side changes required

Changes

File Before After Delta
shared/session-storage.ts (new) 78 lines +78
shared/session-storage.test.ts (new) 230 lines +230
hooks/agent-usage-reminder/storage.ts 43 lines 12 lines -31
hooks/interactive-bash-session/storage.ts 60 lines 29 lines -31
hooks/rules-injector/storage.ts 60 lines 24 lines -36
shared/session-injected-paths.ts 59 lines 26 lines -33
shared/index.ts +1 line +1

Net: -52 lines of production code (5 implementations → 1 factory + 4 thin wrappers)

Design Decisions

  • No auto-envelope: AgentUsageState already contains sessionID/updatedAt as domain fields — auto-wrapping would cause field collision
  • serialize(state, sessionID): 3 implementations embed sessionID in serialized output, so serialize needs access to it
  • Backward compatibility: On-disk JSON format is identical to previous implementations. injectedRealPaths ?? [] fallback preserved for old data

Test plan

  • 14 new tests: round-trip, defaultValue cloning, corrupted JSON, Set↔Array serialization, backward-compat fixtures for all 3 hook formats
  • 52 existing hook tests pass (zero regressions)
  • bun run typecheck — no new errors
  • Consensus review (Architect + Critic) completed before implementation

🤖 Generated with Claude Code


Summary by cubic

Consolidates session storage into a generic factory to remove duplication without changing behavior or public APIs. On-disk JSON formats stay the same, so no consumer changes are required.

  • Refactors
    • Added shared createSessionStorage<TState, TSerialized> with serialize/deserialize hooks and typed overloads (non-nullable loads when defaultValue is provided).
    • Replaced bespoke storage in agent-usage-reminder, interactive-bash-session, rules-injector, and session-injected-paths; external function signatures unchanged.
    • Preserved existing JSON formats and fallbacks (including injectedRealPaths ?? []); directory auto-create and no-op clear behavior retained.
    • Used structuredClone for defaultValue to avoid shared mutable state; expanded tests for round-trip, corrupted JSON, Set↔Array, backward-compat fixtures, clear-on-missing, and auto-create directory.
    • Re-exported from shared/index.ts for reuse.

Written for commit d638e28. Summary will update on new commits.

Copy link
Copy Markdown

@cubic-dev-ai cubic-dev-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1 issue found across 7 files

Confidence score: 2/5

  • There is a high-confidence, user-impacting security risk in src/shared/session-storage.ts: unsanitized sessionID is used to build filesystem paths, which can allow directory traversal outside storageDir.
  • Because this affects load/save/clear operations, an attacker could potentially read, overwrite, or delete unintended files, making this risky to merge before validation/path normalization is added.
  • Pay close attention to src/shared/session-storage.ts - path construction from sessionID needs traversal prevention to keep file access confined to storageDir.
Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="src/shared/session-storage.ts">

<violation number="1" location="src/shared/session-storage.ts:43">
P1: Unsanitized `sessionID` is used in path construction, enabling directory traversal outside `storageDir` for load/save/clear operations.</violation>
</file>

Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.

@code-yeongyu code-yeongyu added the triage:refactor PR: Code refactor label Mar 24, 2026
@code-yeongyu
Copy link
Copy Markdown
Owner

Good refactor! Clean consolidation of session storage patterns. However, this has merge conflicts with the current dev branch. Could you rebase on latest dev? We just merged several PRs that likely caused the conflicts. Happy to merge once rebased.

Extract duplicated CRUD logic from 5 session storage implementations
into a single createSessionStorage<TState, TSerialized> factory with
function overloads for conditional return types and serialize/deserialize
hooks for custom transformations.

Constraint: All external API signatures must remain unchanged
Constraint: On-disk JSON format must be backward-compatible
Rejected: Auto-envelope wrapping { sessionID, ...data, updatedAt } | AgentUsageState already contains sessionID/updatedAt as domain fields, causing field collision
Rejected: Keep duplicates + ESLint custom rule | detects but does not reduce duplication; each new hook requires full copy-paste
Confidence: high
Scope-risk: narrow
Directive: defaultValue must be structuredClone-compatible (no functions, symbols, or DOM nodes)
Not-tested: Concurrent read/write from multiple processes (pre-existing TOCTOU in original code)

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
@iyoda iyoda force-pushed the refactor/session-storage-abstraction branch from fd9d88a to d638e28 Compare March 30, 2026 10:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

triage:refactor PR: Code refactor

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants