# `MingaAgent.BufferForkStore`
[🔗](https://github.com/jsmestad/minga/blob/main/lib/minga_agent/buffer_fork_store.ex#L1)

Holds the mapping of file paths to Buffer.Fork pids for an agent session.

Each agent session gets its own store. Forks are created lazily on the
first write to a file that has an open buffer. The store monitors each
fork and removes it if the fork process dies.

The store is an Agent wrapping a map and a list of monitor refs. Tool
callbacks (which run in a spawned Task) query and update it via the
public API.

## Lifecycle

    {:ok, store} = BufferForkStore.start_link()
    {:ok, fork_pid} = BufferForkStore.get_or_create(store, "/abs/path/lib/foo.ex", buffer_pid)
    forks = BufferForkStore.all(store)
    BufferForkStore.stop(store)

# `state`

```elixir
@type state() :: %{
  forks: %{required(String.t()) =&gt; pid()},
  monitors: %{required(reference()) =&gt; String.t()}
}
```

The store's internal state.

# `all`

```elixir
@spec all(GenServer.server()) :: %{required(String.t()) =&gt; pid()}
```

Returns all forks as a `%{path => fork_pid}` map.

# `child_spec`

Returns a specification to start this module under a supervisor.

See `Supervisor`.

# `discard_all`

```elixir
@spec discard_all(GenServer.server()) :: :ok
```

Discards all forks without merging.

# `get`

```elixir
@spec get(GenServer.server(), String.t()) :: pid() | nil
```

Returns the fork pid for `path`, or nil if no fork exists.

# `get_or_create`

```elixir
@spec get_or_create(GenServer.server(), String.t(), pid()) ::
  {:ok, pid()} | {:error, term()}
```

Returns the fork pid for `path`, creating one from `buffer_pid` if needed.

If a fork already exists for this path, returns it. Otherwise creates a
new fork from the parent buffer, stores it, and returns the pid.

# `merge_all`

```elixir
@spec merge_all(GenServer.server()) :: [
  {String.t(), :ok | {:conflict, term()} | {:error, term()}}
]
```

Merges all dirty forks back to their parent buffers. Returns results per path.

# `start_link`

```elixir
@spec start_link(keyword()) :: GenServer.on_start()
```

Starts a new empty fork store.

# `stop`

```elixir
@spec stop(GenServer.server()) :: :ok
```

Stops the store, cleaning up all forks.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
