Skip to content
pi-c Artifact [a412414839]
Login

Artifact [a412414839]

Artifact a412414839d30a83c6529832e69e068b3c0211c272e8d90a4898739bb01fbdc1:


Shared journal store

Several pic processes may open one workspace journal at once, each working its own conversation. This replaces the rule "one pic process owns the journal" (durability contract D18) with "one pic process owns a conversation at a time".

Status: in force. Built as described; docs/dev/durability.md is the standing description, this file the reasoning.

What the user gets

Not offered: merging, two processes in one conversation, sync between machines.

The problem

The exclusive lock did two jobs. It kept writers apart, which SQLite can do by itself. And it was the proof that the previous owner was dead: at open, every started effect, generation and hook run was known to be abandoned, so recovery could mark it unknown or run it again. With two live processes that proof is gone. Recovery that treats a live process's started effect as abandoned would mark a running command unknown, or run a replay-safe tool twice at once.

So the design question is liveness, not locking: how does a process know that the owner of some unfinished work is gone, and how do two processes never run the same request.

Model

Two locks, both flock, both released by the kernel when their holder and the children it forked have all exited.

1. The journal lock (shared)

<journal>.pic-lock, the file that exists today. Every open store holds it shared for its lifetime. It is taken exclusive only to create or migrate the schema.

2. The conversation lease (exclusive)

<journal>.pic-leases/<conversation id>, one small file per conversation being worked. Holding its exclusive flock is the right to advance that conversation: to run its request, journal started rows under it, cancel it, compact it. The file holds the owner's pid, for messages only; the lock is the truth.

lease held      the conversation has a live owner; leave its work alone
lease free      nobody is working it; whatever is `started` under it
                was abandoned and may be recovered by whoever takes it

One mechanism answers both questions. Liveness is "can I take the lease". Claiming a request is "I took the lease". There is no owner table, no heartbeat, no timeout, no pid check, and no schema change: every started row already maps to a conversation (effect and generation through their request, hook run through session_id).

A lease cannot go stale. A process that dies by any means drops it. A running command's supervisor is a forked child and holds it too, so the lease outlives a killed pic exactly until its command has been killed, which is what recovery needs before it writes unknown.

Lease files are removed when released cleanly. A crash leaves an empty, unlocked file, which is harmless and is removed by the next owner. Taking a lease re-checks that the locked file is still the one at the path, so a remove and a create cannot hand out two leases.

Rules

  1. Lease before work. A process takes a conversation's lease before it journals anything recovery interprets (started effects, generations, hook runs) and before it changes the state of the conversation's request. It holds it until its store closes. pic run gives back the lease of each leftover conversation as it finishes it.
  2. Taking a lease recovers. The first thing the new owner does, in one transaction, is today's recovery restricted to that conversation: replay-safe started effects back to intent, the others to unknown, started generations to failed, started hook runs to interrupted, waiting results flushed.
  3. Read after you hold. A claimer re-reads the request's status after it has the lease. Everything that changes a request happens under its lease, so what it reads then is stable.
  4. Admission stays a transaction. "One unfinished request per conversation" is still decided inside BEGIN IMMEDIATE. The lease adds who runs it.
  5. No write transaction across provider or tool I/O. Unchanged. A lease is held across I/O; a write transaction never is.

Open

lock journal (shared)  ->  open SQLite  ->  schema current?
   no: swap to exclusive, create or migrate, swap back to shared
for each conversation with `started` rows: try its lease without waiting
   taken: recover it (rule 2), release
   held:  a live owner; skip
check invariants

So an open still settles everything the dead left behind, as it does today, and touches nothing the living hold.

Waiting

A lease held by a dying process (its supervisor is still killing a command) frees within moments. Whoever wants a specific conversation waits up to one second for it, as open waits for the journal lock today. pic run, which wants any free request, never waits on one while another is free; when nothing is free but something is held, it waits once, up to a second, then returns 0 and says what it left: pic: request KEY is being run by another pic process (pid N); left to it. pic hooks end OWNER waits up to 60 seconds per conversation, as it does today for the journal, because the holder is normally that owner's own dying child.

SQLite

What changes, by module

The lease lives in the store. Callers outside store.c, loop.c and hooks.c do not learn about it.

store.c

platform.h / platform.c

loop.c

hooks.c

main.c

toolpar.c, tools.c, the TUI, pic ps

Schema. No table changes and no migration. (If review wants pic sessions to show owners without probing leases, that would be the first schema change; not proposed.)

The stale-file tool result

Two conversations in one workspace can change the same file. pic does not merge. It tells the agent what happened, with what it needs to redo the work.

The journal knows which conversation wrote what: every edit and write is an effect with its path. A file is stale for a conversation when another conversation has a finished, successful edit or write of that file later than this conversation's last finished read, edit or write of it, or, when it has none, later than this conversation's first entry (paths compared by pic_tool_path_key; "this conversation" is the path to its head, so a fork inherits what its parent saw). The second clause is what keeps one conversation after another from ever seeing this: a file an earlier conversation wrote is not "changed since" for a later one.

and the file as read would return it (same line and byte caps, same truncation notice).

It is computed in the loop, in the parent process, when the call's result is journaled (edit) or before the call is started (write); tools.c does not change beyond exporting the path key it already has.

Failure cases

kill -9 of A while B works. A's lease drops (once its command supervisor has killed the command). B is not disturbed: it never looks at A's conversation. The next process to open the journal takes A's lease in its recovery pass and settles A's started rows; the next pic run (or ask --session) finishes the request. B's own started rows are under B's lease and are skipped by everyone.

A crashes between "started" and the tool's effect, or after it. Same as today, per conversation: replay-safe runs again from its journaled plan, unsafe becomes unknown and is never run again.

Two processes race to claim one admitted request. Both flock; the kernel picks one. The loser skips it. If the winner finishes and releases before the loser tries, the loser takes the lease, re-reads the request (rule 3), finds it finished, and gives the lease back.

Two processes race to admit into one conversation. The lease orders them; behind it, the admission transaction still refuses a second unfinished request.

Two processes create or migrate the journal at once. One gets the journal lock exclusive; the other waits up to a second, retakes it shared and finds the schema current, or is refused.

A lease file left by a crash. Unlocked, so free. The next taker recovers the conversation.

A lease holder that hangs (alive, not progressing) keeps its conversation, as a hung pic keeps the whole journal today. Kill it.

Writer starvation or a stuck fsync. After 30 s the waiting statement fails and its process stops; see Busy handling.

Process dies holding the journal lock exclusive (mid-migration). The migration is one transaction; SQLite rolls it back; the lock drops.

Tests

New test/pic/shared.sh (Makefile PIC_SCRIPTS, tools/remote/payload.sh), using the suite's await/HOLD so nothing depends on timing:

  1. Side question: A holds in a tool; B asks in a new conversation and is answered; dump, sessions, log, check succeed meanwhile; A then finishes; both answered.
  2. Recovery leaves the living alone: while A holds a started unsafe effect, B opens, runs and exits; A's effect is still started, then done, and its command ran once.
  3. kill -9 A mid-command while B is mid-run: B finishes untouched; pic run then settles A (unknown, command not run again, not left running).
  4. Crash matrix with a live neighbour (fault builds): for every PIC_CRASH_AT point of crash.sh, crash A there while B holds a started effect; pic run finishes A with the same ledger counts as the one-process matrix; B is unaffected and pic check passes at every step.
  5. Claim race: one admitted request, four pic run at once; its command ran exactly once; all exit 0.
  6. pic run beside a live owner: returns 0, names the request it left, changes nothing.
  7. Admission race: two submits to one conversation; exactly one admitted.
  8. Same conversation: ask --session X while X is live is refused naming the pid; no hooks ran for it.
  9. cancel: refused for a live request; works once its owner is killed.
  10. A leftover lease file does not block; a symlinked journal path meets the same leases.
  11. Stale file: edit miss with another conversation's edit in between carries the note and current text; write is refused once, then goes through; with one conversation the Pi messages are unchanged.
  12. Load: four processes, several tool calls each; no database is locked.

Changed: crash.sh fence and alias (a second pic run now returns 0 and leaves the request, instead of being refused); an old-style exclusive holder of .pic-lock still gets owned by another pic process. migrate.sh: a migration is refused while another process holds the journal lock, and runs once it is released.

Out of scope