tests — persistence

Module: tests-persistence Cohesion: 0.80 Members: 0

tests — persistence

This document describes the persistence module, which is responsible for managing conversation state and ensuring data integrity within the application. It comprises two primary components: ConversationBranchManager for handling multiple conversation threads, and SessionLock for preventing concurrent access to session data.

1. Conversation Branch Management

The ConversationBranchManager module provides a robust system for managing conversation history, inspired by version control systems like Git. It allows users to create, switch between, merge, and fork different conversation paths, enabling exploration of alternative responses without losing previous context.

1.1 Purpose

The ConversationBranchManager is designed to:

1.2 Core Concepts

1.3 Key API

The ConversationBranchManager class exposes the following public methods:

1.4 Branching Model

The ConversationBranchManager supports a tree-like structure for conversations, where branches can fork from any point in their parent's history.

graph TD
    A[main: Main conversation]
    B[child1: Child Branch 1]
    C[child2: Child Branch 2]
    D[grandchild: Grandchild Branch]

    A --> B
    A --> C
    B --> D

In this diagram:

1.5 Persistence Details

Each ConversationBranch is serialized to a JSON file. The storage path is ~/.codebuddy/branches//.json. This allows for easy inspection and management of individual branch data. The fs-extra library is used for file system operations, providing robust handling of directory creation and file I/O.

2. Session Locking

The SessionLock module provides a critical mechanism to prevent data corruption when multiple processes might attempt to access or modify the same session-related files concurrently. It implements a PID-based lock file system.

2.1 Purpose

The SessionLock is designed to:

2.2 Core Concepts

2.3 Key API

The SessionLock class and its helper function provide the following:

2.4 How it Works

When acquire() is called:

  1. It first checks if a lock file already exists.
  2. If it exists, it reads the LockInfo from it.
  3. It then determines if the process identified by LockInfo.pid is still alive and if LockInfo.timestamp is recent.

  1. If no lock file exists or a stale one was cleaned up, it attempts to create a new lock file with the current process's pid, timestamp, and hostname. This write uses an exclusive flag (wx) to prevent race conditions where two processes try to create the file simultaneously.

3. Integration and Usage

The persistence module forms the backbone of state management for conversational interactions.

Together, these modules provide a robust and reliable foundation for managing complex conversational state in a persistent and concurrent-safe manner.