Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Implementation:1ay1 Agentty Dual Lock Shared File

From Leeroopedia
Knowledge Sources
Domains IO, Concurrency
Last Updated 2026-10-28 12:00 GMT

Overview

The one way agentty writes a file another agentty may also be writing, using dual in-process and cross-process locking with bounded wait and atomic writes.

Description

The agentty::persistence module is the sole policy for shared file writes. Both locks are always taken: a `maya::guarded` (invisible to another process) and a POSIX file lock (invisible to another thread of the same process). Lock order is a type: every Lane has a rank, and a section is entered with a capability proving the thread holds only lower ranks, so nesting is always in rank order and the cross-process order matches.

Bounded and never fatal: the file lock waits at most `kSharedFileWait` (5s). Past that, or on a filesystem that can't lock, the body runs holding the lane alone and `Held::cross_process` is false (logged once per file). A lost update is a bad day; an agentty frozen behind a peer stopped with ^Z is worse.

Atomic writes: `write_atomic` writes to a temp in the same directory then renames. Readers see the old file or the new one, never a mix. `append_atomic` uses a single `write(2)` with `O_APPEND` so concurrent appenders never interleave. `create_exclusive` hard-links a temp, failing if the name is taken.

Usage

Import this when you need to: write a file another agentty process may also write, do a read-modify-write of a shared config file, or atomically write/append/create files.

Code Reference

Source Location

Signature

namespace agentty::persistence {

inline constexpr std::chrono::milliseconds kSharedFileWait{5000};

struct Held { bool cross_process = false; };

// Enter a section: lane + file lock, then run body.
template <class R, class F, class... Args>
auto with_shared_file(const maya::below<R>& cap, LaneOf<R>& lane,
                      const std::filesystem::path& target, F body, Args... args);

enum class Durability { Atomic, Durable };
struct WriteOptions { Durability durability; unsigned mode; bool keep_mode; };

// Atomic replace: temp + rename. Readers never see a mix.
[[nodiscard]] bool write_atomic(Io, const std::filesystem::path& target,
                                std::string_view bytes, WriteOptions = {});

// Single write(2) with O_APPEND — concurrent appenders never interleave.
[[nodiscard]] bool append_atomic(Io, const std::filesystem::path& target,
                                 std::string_view bytes,
                                 Durability = Durability::Atomic, unsigned mode = 0644);

// Append + return the byte offset the record landed at.
[[nodiscard]] std::optional<std::uint64_t> append_record(Io, const std::filesystem::path&,
                                                         std::string_view, Durability = {});

// Create only if it doesn't exist (hard-link, fails if name taken).
[[nodiscard]] bool create_exclusive(Io, const std::filesystem::path&,
                                    std::string_view, unsigned mode = 0644);

// Read-modify-write of one small shared file:
template <class F, class... Args>
Update update_shared(Io, const lock_levels::below_file_lane& cap,
                     const std::filesystem::path& target, WriteOptions, F edit, Args...);

[[nodiscard]] std::optional<std::string> read_file(Io, const std::filesystem::path&);
[[nodiscard]] std::optional<std::string> peek_file(const std::filesystem::path&);
}

Import

#include "agentty/io/shared_file.hpp"

I/O Contract

Inputs

Name Type Required Description
target std::filesystem::path Yes File path to write/lock
bytes std::string_view Yes Content to write
body callable Yes (with_shared_file) Read-modify-write body
WriteOptions struct No Durability (Atomic/Durable), mode, keep_mode

Outputs

Name Type Description
Held.cross_process bool Whether the cross-process lock was acquired
write_atomic() bool True on success
append_record() std::optional<uint64_t> Byte offset of the appended record

Usage Examples

Atomic File Write

#include "agentty/io/shared_file.hpp"

// Atomic write — readers see old or new, never a mix
agentty::persistence::write_atomic(io, "settings.json", json_bytes,
    {.durability = agentty::persistence::Durability::Durable, .mode = 0600});

Read-Modify-Write a Shared File

agentty::persistence::update_shared(io, file_lane_cap, "config.json", opts,
    [](std::optional<std::string> old) -> std::optional<std::string> {
        auto json = old ? parse(*old) : nlohmann::json::object();
        json["key"] = "value";
        return json.dump();
    });
// Returns Update::Written, Unchanged, or Failed

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment