Skip to content

refactor(cli, workspace): Extract lock acquisition#502

Merged
JeanMertz merged 2 commits intomainfrom
prr71
Apr 3, 2026
Merged

refactor(cli, workspace): Extract lock acquisition#502
JeanMertz merged 2 commits intomainfrom
prr71

Conversation

@JeanMertz
Copy link
Copy Markdown
Collaborator

The lock acquisition logic — polling at 500ms intervals, timing out after 30 seconds ($JP_LOCK_DURATION), and showing an interactive prompt on contention — was inline in query.rs. It is now extracted into cmd::lock and shared across all commands. As a result, config set, conversation edit, and conversation rm now poll and prompt on lock contention rather than failing immediately.

In jp_workspace, Workspace::lock_conversation now returns a LockResult enum instead of Option<ConversationLock>. The enum variant AlreadyLocked carries the ConversationHandle back so callers avoid an extra acquire_conversation round-trip. Two new methods — create_and_lock_conversation and its _with_id variant — atomically create and lock a conversation, removing the repetitive create-then-acquire-then-lock pattern across call sites.

ConversationLock now holds an Option<Arc<ConversationFileLock>> instead of a required Arc, so it works for in-memory workspaces without on-disk storage. The lock file placement falls back to workspace storage when user storage is absent, ensuring a flock can always be acquired.

The lock acquisition logic — polling at 500ms intervals, timing out
after 30 seconds (`$JP_LOCK_DURATION`), and showing an interactive
prompt on contention — was inline in `query.rs`. It is now extracted
into `cmd::lock` and shared across all commands. As a result, `config
set`, `conversation edit`, and `conversation rm` now poll and prompt on
lock contention rather than failing immediately.

In `jp_workspace`, `Workspace::lock_conversation` now returns a
`LockResult` enum instead of `Option<ConversationLock>`. The enum
variant `AlreadyLocked` carries the `ConversationHandle` back so callers
avoid an extra `acquire_conversation` round-trip. Two new methods —
`create_and_lock_conversation` and its `_with_id` variant — atomically
create and lock a conversation, removing the repetitive
create-then-acquire-then-lock pattern across call sites.

`ConversationLock` now holds an `Option<Arc<ConversationFileLock>>`
instead of a required `Arc`, so it works for in-memory workspaces
without on-disk storage. The lock file placement falls back to workspace
storage when user storage is absent, ensuring a flock can always be
acquired.

Signed-off-by: Jean Mertz <git@jeanmertz.com>
Signed-off-by: Jean Mertz <git@jeanmertz.com>
@JeanMertz JeanMertz merged commit 32dd1a6 into main Apr 3, 2026
12 checks passed
@JeanMertz JeanMertz deleted the prr71 branch April 3, 2026 04:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant