My agentic slop goes here. Not intended for anyone else!

more

Changed files
+121 -58
stack
+3 -2
stack/cacheio/bin/cache_cli.ml
···
(* Default app name for the cache CLI tool *)
let app_name = "cacheio-cli" in
-
(* Run with Eio to provide the filesystem *)
+
(* Run with Eio to provide the filesystem and stdin *)
Eio_main.run @@ fun env ->
let fs = env#fs in
+
let stdin = (env#stdin :> Eio.Flow.source_ty Eio.Resource.t) in
let doc = "Cache management tool" in
let man = [
···
`P "This tool provides subcommands for listing, clearing, and managing cached files.";
] in
let info = Cmd.info "cache" ~doc ~man in
-
let cmd = Cmd.group info (Cacheio_cmd.Cmd.make_commands ~app_name fs) in
+
let cmd = Cmd.group info (Cacheio_cmd.Cmd.make_commands ~app_name fs stdin) in
exit (Cmd.eval cmd)
+1 -5
stack/cacheio/bin/example.ml
···
let setup_logs () =
Logs.set_level (Some Logs.Info);
-
let reporter = Logs.format_reporter
-
~pp_header:Logs.pp_header
-
~app:Format.std_formatter
-
~dst:Format.err_formatter () in
-
Logs.set_reporter reporter
+
Logs.set_reporter (Logs_fmt.reporter ())
(** Example: Using CacheIO for caching data *)
let cache_example env sw =
+18 -21
stack/cacheio/lib/cacheio.ml
···
(** {1 Creation} *)
let create ~base_dir =
+
(* Note: RNG initialization should be done by the application *)
+
(* For testing, we'll use a simple deterministic approach *)
+
(* Ensure base directory exists *)
(try Path.mkdir ~perm:0o755 base_dir
with Eio.Io (Eio.Fs.E (Already_exists _), _) -> ()
···
(** {1 Streaming Operations} *)
let get t ~key ~sw =
-
Mutex.use_ro t.mutex @@ fun () ->
+
(* No mutex needed for read operation *)
match find_file t.base_dir key with
| None -> None
| Some (path, _ttl, _flags) ->
···
let dir = ensure_dirs t key in
(* Generate temporary filename using hashed key *)
-
(* Use Mirage crypto RNG for cryptographically secure randomness *)
-
let rand_bytes = Mirage_crypto_rng.generate 8 in
-
let rand_cstruct = Cstruct.of_string rand_bytes in
-
let rand_hex = Cstruct.to_hex_string rand_cstruct in
+
(* Use timestamp and random for temporary file uniqueness *)
+
let rand_hex = Printf.sprintf "%016Lx" (Random.bits64 ()) in
let temp_name = Printf.sprintf "tmp.%s.%s"
(hash_key key)
rand_hex in
···
(** {1 Basic Operations} *)
let exists t ~key =
-
Mutex.use_ro t.mutex @@ fun () ->
+
(* No mutex needed for checking existence *)
match find_file t.base_dir key with
| Some _ -> true
| None -> false
···
| None -> ()
let size t ~key =
-
Mutex.use_ro t.mutex @@ fun () ->
+
(* No mutex needed for getting size *)
match find_file t.base_dir key with
| Some (path, _, _) ->
(try Some (Optint.Int63.to_int64 (Path.stat ~follow:false path).size)
···
| None -> None
let get_path t ~key =
-
Mutex.use_ro t.mutex @@ fun () ->
+
(* No mutex needed for getting path *)
match find_file t.base_dir key with
| Some (path, _, _) -> Some path
| None -> None
···
| None -> ()
let get_flags t ~key =
-
Mutex.use_ro t.mutex @@ fun () ->
+
(* No mutex needed for getting flags *)
match find_file t.base_dir key with
| Some (_, _, flags) -> Some flags
| None -> None
···
with _ -> acc
let scan t =
-
Mutex.use_ro t.mutex @@ fun () ->
+
(* No mutex needed for read-only directory scan *)
try
Path.read_dir t.base_dir
|> List.filter (fun name -> name <> "tmp")
···
Stats.of_entries entries
let expire t =
-
Mutex.use_rw ~protect:false t.mutex @@ fun () ->
+
(* Scan without mutex, then delete with individual locks *)
let entries = scan t in
List.fold_left (fun count entry ->
if Entry.is_expired entry then begin
···
) 0 entries
let clear t =
-
Mutex.use_rw ~protect:false t.mutex @@ fun () ->
+
(* Scan without mutex, then delete with individual locks *)
let entries = scan t in
List.iter (fun entry ->
if not (Entry.is_pinned entry) then
···
) entries
let clear_temporary t =
-
Mutex.use_rw ~protect:false t.mutex @@ fun () ->
+
(* Scan without mutex, then delete with individual locks *)
let entries = scan t in
List.iter (fun entry ->
if Entry.is_temporary entry then
···
let dir = ensure_dirs t key in
(* Generate temporary filename using hashed key *)
-
(* Use Mirage crypto RNG for cryptographically secure randomness *)
-
let rand_bytes = Mirage_crypto_rng.generate 8 in
-
let rand_cstruct = Cstruct.of_string rand_bytes in
-
let rand_hex = Cstruct.to_hex_string rand_cstruct in
+
(* Use timestamp and random for temporary file uniqueness *)
+
let rand_hex = Printf.sprintf "%016Lx" (Random.bits64 ()) in
let temp_name = Printf.sprintf "tmp.%s.%s.chunk"
(hash_key key) rand_hex in
let temp_path = Path.(t.tmp_dir / temp_name) in
···
(* Create final file *)
let final_filename = key_to_filename ?ttl ~flags key in
-
(* Use Mirage crypto RNG for cryptographically secure randomness *)
-
let rand_bytes = Mirage_crypto_rng.generate 8 in
-
let rand_cstruct = Cstruct.of_string rand_bytes in
-
let rand_hex = Cstruct.to_hex_string rand_cstruct in
+
(* Use timestamp and random for temporary file uniqueness *)
+
let rand_hex = Printf.sprintf "%016Lx" (Random.bits64 ()) in
let temp_name = Printf.sprintf "tmp.%s.%s.coalesce"
(hash_key key) rand_hex in
let temp_path = Path.(t.tmp_dir / temp_name) in
+82 -19
stack/cacheio/lib/cmd.ml
···
open Cmdliner
open Eio
-
let list_cmd ~app_name fs =
+
let list_cmd ~app_name fs _stdin =
let list cache_dir stale expired pinned temporary older_than =
let base_dir = Path.(fs / cache_dir) in
let cache = Cacheio.create ~base_dir in
···
let cache_dir_term = Xdge.Cmd.cache_term app_name in
Cmd.v info Term.(const list $ cache_dir_term $ stale $ expired $ pinned $ temporary $ older_than)
-
let expire_cmd ~app_name fs =
+
let expire_cmd ~app_name fs _stdin =
let expire cache_dir =
let base_dir = Path.(fs / cache_dir) in
let cache = Cacheio.create ~base_dir in
···
let cache_dir_term = Xdge.Cmd.cache_term app_name in
Cmd.v info Term.(const expire $ cache_dir_term)
-
let clear_cmd ~app_name fs =
+
let clear_cmd ~app_name fs _stdin =
let clear cache_dir all temporary =
let base_dir = Path.(fs / cache_dir) in
let cache = Cacheio.create ~base_dir in
···
let cache_dir_term = Xdge.Cmd.cache_term app_name in
Cmd.v info Term.(const clear $ cache_dir_term $ all $ temporary)
-
let flag_cmd ~app_name fs =
+
let flag_cmd ~app_name fs _stdin =
let flag cache_dir key add remove set_flags =
let base_dir = Path.(fs / cache_dir) in
let cache = Cacheio.create ~base_dir in
···
let cache_dir_term = Xdge.Cmd.cache_term app_name in
Cmd.v info Term.(const flag $ cache_dir_term $ key $ add $ remove $ set_flags)
-
let stats_cmd ~app_name fs =
+
let stats_cmd ~app_name fs _stdin =
let stats cache_dir =
let base_dir = Path.(fs / cache_dir) in
let cache = Cacheio.create ~base_dir in
···
let cache_dir_term = Xdge.Cmd.cache_term app_name in
Cmd.v info Term.(const stats $ cache_dir_term)
-
let put_cmd ~app_name fs =
+
let put_cmd ~app_name fs stdin =
let put cache_dir key file ttl pinned stale temporary =
let base_dir = Path.(fs / cache_dir) in
let cache = Cacheio.create ~base_dir in
···
let file_path = Path.(fs / path) in
(Path.open_in ~sw file_path :> Eio.Flow.source_ty Eio.Resource.t)
| None ->
-
(* Read from stdin - we need to get it from the environment *)
-
failwith "Reading from stdin requires access to Eio environment - use -f flag for now"
+
(* Use stdin passed from the CLI *)
+
stdin
in
(* Put into cache *)
···
let cache_dir_term = Xdge.Cmd.cache_term app_name in
Cmd.v info Term.(const put $ cache_dir_term $ key $ file $ ttl $ pinned $ stale $ temporary)
-
let get_cmd ~app_name fs =
+
let get_cmd ~app_name fs _stdin =
let get cache_dir key output =
let base_dir = Path.(fs / cache_dir) in
let cache = Cacheio.create ~base_dir in
···
let cache_dir_term = Xdge.Cmd.cache_term app_name in
Cmd.v info Term.(const get $ cache_dir_term $ key $ output)
+
(** Delete command - remove a specific cache entry *)
+
let delete_cmd ~app_name fs _stdin =
+
let delete cache_dir key =
+
let base_dir = Path.(fs / cache_dir) in
+
let cache = Cacheio.create ~base_dir in
+
+
if Cacheio.exists cache ~key then begin
+
Cacheio.delete cache ~key;
+
Printf.printf "Deleted cache entry: %s\n" key
+
end else begin
+
Printf.eprintf "Key not found: %s\n" key;
+
exit 1
+
end
+
in
+
+
let key = Arg.(required & pos 0 (some string) None & info [] ~docv:"KEY"
+
~doc:"Cache key to delete") in
+
+
let doc = "Delete a cache entry" in
+
let man = [
+
`S "DESCRIPTION";
+
`P "Remove a specific cache entry by key.";
+
`S "EXAMPLES";
+
`P "Delete entry: $(b,cache delete mykey)";
+
] in
+
let info = Cmd.info "delete" ~doc ~man in
+
let cache_dir_term = Xdge.Cmd.cache_term app_name in
+
Cmd.v info Term.(const delete $ cache_dir_term $ key)
+
+
(** Exists command - check if a key exists *)
+
let exists_cmd ~app_name fs _stdin =
+
let exists cache_dir key quiet =
+
let base_dir = Path.(fs / cache_dir) in
+
let cache = Cacheio.create ~base_dir in
+
+
if Cacheio.exists cache ~key then begin
+
if not quiet then Printf.printf "Key exists: %s\n" key;
+
exit 0
+
end else begin
+
if not quiet then Printf.printf "Key does not exist: %s\n" key;
+
exit 1
+
end
+
in
+
+
let key = Arg.(required & pos 0 (some string) None & info [] ~docv:"KEY"
+
~doc:"Cache key to check") in
+
let quiet = Arg.(value & flag & info ["quiet"; "q"]
+
~doc:"Suppress output, use exit code only") in
+
+
let doc = "Check if a cache entry exists" in
+
let man = [
+
`S "DESCRIPTION";
+
`P "Check if a cache entry exists. Returns exit code 0 if exists, 1 if not.";
+
`S "EXAMPLES";
+
`P "Check existence: $(b,cache exists mykey)";
+
`P "Use in scripts: $(b,if cache exists mykey -q; then echo 'Found'; fi)";
+
] in
+
let info = Cmd.info "exists" ~doc ~man in
+
let cache_dir_term = Xdge.Cmd.cache_term app_name in
+
Cmd.v info Term.(const exists $ cache_dir_term $ key $ quiet)
+
(** {1 Command Groups} *)
-
let make_commands ~app_name fs = [
-
get_cmd ~app_name fs;
-
put_cmd ~app_name fs;
-
list_cmd ~app_name fs;
-
expire_cmd ~app_name fs;
-
clear_cmd ~app_name fs;
-
flag_cmd ~app_name fs;
-
stats_cmd ~app_name fs
+
let make_commands ~app_name fs stdin = [
+
get_cmd ~app_name fs stdin;
+
put_cmd ~app_name fs stdin;
+
delete_cmd ~app_name fs stdin;
+
exists_cmd ~app_name fs stdin;
+
list_cmd ~app_name fs stdin;
+
expire_cmd ~app_name fs stdin;
+
clear_cmd ~app_name fs stdin;
+
flag_cmd ~app_name fs stdin;
+
stats_cmd ~app_name fs stdin
]
-
let make_cmd ~app_name fs =
+
let make_cmd ~app_name fs stdin =
let doc = "Cache management commands" in
let info = Cmd.info "cache" ~doc in
-
Cmd.group info (make_commands ~app_name fs)
+
Cmd.group info (make_commands ~app_name fs stdin)
+16 -10
stack/cacheio/lib/cmd.mli
···
(** {1 Command Builders} *)
(** Create get command with given filesystem and app name *)
-
val get_cmd : app_name:string -> Eio.Fs.dir_ty Eio.Path.t -> unit Cmdliner.Cmd.t
+
val get_cmd : app_name:string -> Eio.Fs.dir_ty Eio.Path.t -> Eio.Flow.source_ty Eio.Resource.t -> unit Cmdliner.Cmd.t
(** Create put command with given filesystem and app name *)
-
val put_cmd : app_name:string -> Eio.Fs.dir_ty Eio.Path.t -> unit Cmdliner.Cmd.t
+
val put_cmd : app_name:string -> Eio.Fs.dir_ty Eio.Path.t -> Eio.Flow.source_ty Eio.Resource.t -> unit Cmdliner.Cmd.t
(** Create list command with given filesystem and app name *)
-
val list_cmd : app_name:string -> Eio.Fs.dir_ty Eio.Path.t -> unit Cmdliner.Cmd.t
+
val list_cmd : app_name:string -> Eio.Fs.dir_ty Eio.Path.t -> Eio.Flow.source_ty Eio.Resource.t -> unit Cmdliner.Cmd.t
(** Create expire command with given filesystem and app name *)
-
val expire_cmd : app_name:string -> Eio.Fs.dir_ty Eio.Path.t -> unit Cmdliner.Cmd.t
+
val expire_cmd : app_name:string -> Eio.Fs.dir_ty Eio.Path.t -> Eio.Flow.source_ty Eio.Resource.t -> unit Cmdliner.Cmd.t
(** Create clear command with given filesystem and app name *)
-
val clear_cmd : app_name:string -> Eio.Fs.dir_ty Eio.Path.t -> unit Cmdliner.Cmd.t
+
val clear_cmd : app_name:string -> Eio.Fs.dir_ty Eio.Path.t -> Eio.Flow.source_ty Eio.Resource.t -> unit Cmdliner.Cmd.t
(** Create flag management command with given filesystem and app name *)
-
val flag_cmd : app_name:string -> Eio.Fs.dir_ty Eio.Path.t -> unit Cmdliner.Cmd.t
+
val flag_cmd : app_name:string -> Eio.Fs.dir_ty Eio.Path.t -> Eio.Flow.source_ty Eio.Resource.t -> unit Cmdliner.Cmd.t
(** Create statistics command with given filesystem and app name *)
-
val stats_cmd : app_name:string -> Eio.Fs.dir_ty Eio.Path.t -> unit Cmdliner.Cmd.t
+
val stats_cmd : app_name:string -> Eio.Fs.dir_ty Eio.Path.t -> Eio.Flow.source_ty Eio.Resource.t -> unit Cmdliner.Cmd.t
+
+
(** Create delete command with given filesystem and app name *)
+
val delete_cmd : app_name:string -> Eio.Fs.dir_ty Eio.Path.t -> Eio.Flow.source_ty Eio.Resource.t -> unit Cmdliner.Cmd.t
+
+
(** Create exists command with given filesystem and app name *)
+
val exists_cmd : app_name:string -> Eio.Fs.dir_ty Eio.Path.t -> Eio.Flow.source_ty Eio.Resource.t -> unit Cmdliner.Cmd.t
(** {1 Command Groups} *)
-
(** Create all cache commands for the given filesystem and app name *)
-
val make_commands : app_name:string -> Eio.Fs.dir_ty Eio.Path.t -> unit Cmdliner.Cmd.t list
+
(** Create all cache commands for the given filesystem, stdin and app name *)
+
val make_commands : app_name:string -> Eio.Fs.dir_ty Eio.Path.t -> Eio.Flow.source_ty Eio.Resource.t -> unit Cmdliner.Cmd.t list
(** Create main command group containing all cache subcommands *)
-
val make_cmd : app_name:string -> Eio.Fs.dir_ty Eio.Path.t -> unit Cmdliner.Cmd.t
+
val make_cmd : app_name:string -> Eio.Fs.dir_ty Eio.Path.t -> Eio.Flow.source_ty Eio.Resource.t -> unit Cmdliner.Cmd.t
+1 -1
stack/cacheio/lib/dune
···
(public_name cacheio)
(name cacheio)
(modules cacheio flags entry stats range chunk)
-
(libraries eio eio_main digestif yojson ptime ptime.clock.os logs fmt xdge mirage-crypto-rng cstruct))
+
(libraries eio eio_main digestif yojson ptime ptime.clock.os logs fmt xdge cstruct))
(library
(public_name cacheio.cmd)