Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
b281ef3
fix(cli): honor explicit provider identity when attaching to a runnin…
KooshaPari Oct 22, 2026
17d3aa1
fix(cli): import socket identity via the public server re-export
KooshaPari Oct 22, 2026
0f64745
fix(cli): derive the provider key the daemon actually publishes
KooshaPari Oct 22, 2026
bbaf215
fix(cli): use the fork's environment variable names in the port
KooshaPari Oct 22, 2026
10e1c6c
style: apply rustfmt to the provider-identity port
KooshaPari Oct 22, 2026
2a44f0e
fix(cli): spawn a temporary server for the isolated client
KooshaPari Oct 22, 2026
6d79f51
fix(cli): clear the clippy lints the CI gate caught
KooshaPari Oct 23, 2026
f5117ca
test(server): import the socket paths into the nested test module
KooshaPari Oct 25, 2026
9822c84
test(server): scope the test-module imports and drop the last unsafe …
KooshaPari Oct 25, 2026
16fde59
test(server): use a KCODE_HOME that require_kcode_path accepts
KooshaPari Oct 25, 2026
7c453a3
refactor(cli): keep the provider-identity change inside the size budget
KooshaPari Oct 26, 2026
ed7c509
fix(cli): repair the module split so the workspace compiles again
KooshaPari Oct 26, 2026
7277751
fix(cli): make provider isolation actually bind a private daemon
KooshaPari Oct 26, 2026
1bf1adb
fix(server): create the shared socket's parent at the bind site
KooshaPari Oct 26, 2026
022e400
fix(cli): clear the clippy lints the CI gate caught
KooshaPari Oct 26, 2026
efbe61a
docs(acceptance): record the provider-identity acceptance matrix
KooshaPari Oct 26, 2026
23b38b6
docs(acceptance): record that the app-core test deadlock is pre-existing
KooshaPari Oct 27, 2026
7e1b68e
fix(server): take the shared environment lock in the socket identity …
KooshaPari Oct 27, 2026
dd4d381
docs(acceptance): re-record the matrix against the final binary
KooshaPari Oct 27, 2026
ee8a563
fix(cli): restore the Windows build after the module split
KooshaPari Oct 28, 2026
1b576fd
fix(ci): refresh the size baselines that master outgrew
KooshaPari Oct 28, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions crates/jcode-app-core/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -602,7 +602,6 @@ use self::state::{
pub use crate::plan::{SwarmTaskProgress, VersionedPlan};

pub use self::await_members_state::pending_await_members_for_session;
use self::reload_state::clear_reload_marker_if_stale_for_pid;
#[cfg(test)]
pub(crate) use self::reload_state::subscribe_reload_signal_for_tests;
pub use self::reload_state::{
Expand All @@ -614,14 +613,16 @@ pub use self::reload_state::{
};

pub use self::lifecycle::configure_temporary_server;
pub use self::socket::scrub_inherited_provider_identity as scrub_provider_identity;
#[cfg(unix)]
pub use self::socket::spawn_server_notify;
#[cfg(unix)]
use self::socket::{acquire_daemon_lock, mark_close_on_exec};
pub use self::socket::{

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CRITICAL: cleanup_socket_pair was removed from the public re-exports here, but crates/jcode-app-core/src/server/socket_tests.rs still imports it via super::. Tests will fail to compile. Re-export cleanup_socket_pair or update the test import to socket::cleanup_socket_pair.

cleanup_socket_pair, connect_socket, debug_socket_path, has_live_listener, is_server_ready,
reap_stale_socket_if_dead, set_socket_path, socket_path, wait_for_server_ready,
SocketIdentity, connect_socket, debug_socket_path, default_socket_path, has_live_listener,
is_private_socket, is_server_ready, read_socket_identity, reap_stale_socket_if_dead,
set_socket_path, socket_path,
};
#[cfg(unix)]
use self::socket::{acquire_daemon_lock, mark_close_on_exec};
use self::socket::{signal_ready_fd, socket_has_live_listener};

pub use self::util::ServerIdentity;
Expand Down Expand Up @@ -2280,7 +2281,7 @@ impl Server {
}

#[cfg(unix)]
let _daemon_lock = acquire_daemon_lock()?;
let _daemon_lock = acquire_daemon_lock(&self.socket_path)?;

if socket_has_live_listener(&self.socket_path).await {
anyhow::bail!(
Expand All @@ -2292,8 +2293,7 @@ impl Server {
// Remove existing sockets (uses transport abstraction for cross-platform cleanup)
crate::transport::remove_socket(&self.socket_path);
crate::transport::remove_socket(&self.debug_socket_path);

let main_listener = Listener::bind(&self.socket_path)?;
let main_listener = socket::bind_and_publish(&self.socket_path, self.provider.as_ref())?;
let debug_listener = Listener::bind(&self.debug_socket_path)?;

#[cfg(unix)]
Expand All @@ -2306,7 +2306,7 @@ impl Server {

// Preserve an in-flight reload marker for exec-based reloads owned by this
// process, but clear stale markers from unrelated/stale processes.
clear_reload_marker_if_stale_for_pid(std::process::id());
self::socket::clear_stale_reload_marker_for(&self.socket_path);

// Reclaim disk from stale agent scratch work (old cargo target dirs,
// repo clones) and superseded binaries in builds/versions. This runs in
Expand Down
19 changes: 18 additions & 1 deletion crates/jcode-app-core/src/server/reload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const RELOAD_GRACEFUL_SHUTDOWN_TIMEOUT: Duration = Duration::from_secs(2);
fn prepare_server_exec(cmd: &mut std::process::Command, socket_path: &std::path::Path) {
// The replacement daemon must own the published socket paths. Unlink them
// before exec so we never inherit a stale on-disk endpoint through reload.
crate::server::cleanup_socket_pair(socket_path);
crate::server::socket::cleanup_socket_pair(socket_path);
cmd.env_remove("KCODE_READY_FD");

// The shared daemon may have inherited stderr from the client process that
Expand Down Expand Up @@ -197,6 +197,23 @@ pub(super) async fn await_reload_signal(
);
let mut cmd = ProcessCommand::new(&binary);
cmd.arg("serve").arg("--socket").arg(socket.as_os_str());
// The previous generation resolved a provider and stamped its
// identity into this process's environment. Without an explicit
// model the replacement would inherit that ambient identity and
// silently keep serving (and stamping) the previous generation's
// provider. Carry the configured default model forward
// explicitly -- the same channel `spawn_server_with_lifetime`
// uses -- and drop the stale ambient identity so the new
// generation re-derives its provider from scratch.
let effective_model = crate::config::config().provider.default_model.clone();
if let Some(effective_model) = effective_model
.as_deref()
.map(str::trim)
.filter(|model| !model.is_empty())
{
cmd.arg("--model").arg(effective_model);
}
crate::server::socket::scrub_inherited_provider_identity(&mut cmd);
// Auto provider detection is dominated by credential-file probes.
// The replacement process is the same trusted daemon with the same
// environment, so carry the already-resolved, non-secret status
Expand Down
96 changes: 91 additions & 5 deletions crates/jcode-app-core/src/server/reload_state.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::{has_live_listener, is_server_ready};
use super::socket::{has_live_listener, is_server_ready};
use serde::{Deserialize, Serialize};
use std::path::PathBuf;
use std::time::Duration;
Expand Down Expand Up @@ -26,11 +26,37 @@ pub fn clear_reload_marker() {
let _ = std::fs::remove_file(reload_marker_path());
}

pub(super) fn clear_reload_marker_if_stale_for_pid(current_pid: u32) {
/// Who owns the runtime-dir reload marker.
///
/// The marker lives at a runtime-dir-scoped path, so a custom-socket server
/// and the shared daemon share the same file. They do not share the
/// *ownership*: the shared daemon owns the marker and cleans up after stale
/// predecessors, while a custom-socket server must leave a marker it does not
/// own alone. Clearing it would strand the shared daemon in `Starting` and make
/// it reject messages until the marker expired (issue #1748).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(super) enum ForeignMarkerPolicy {
/// The shared daemon owns the runtime-dir marker and cleans up after
/// stale predecessors.
MayClear,
/// A custom-socket server shares the file but not the ownership.
Preserve,
}

impl ForeignMarkerPolicy {
fn preserves(self, state_pid: u32, current_pid: u32) -> bool {
self == Self::Preserve && state_pid != current_pid
}
}

pub(super) fn clear_reload_marker_if_stale_for_pid(current_pid: u32, policy: ForeignMarkerPolicy) {
if let Some(state) = ReloadState::load() {
if state.phase == ReloadPhase::Starting && state.pid == current_pid {
return;
}
if policy.preserves(state.pid, current_pid) {
return;
}
clear_reload_marker();
}
}
Expand Down Expand Up @@ -803,7 +829,7 @@ mod tests {
}
.write();

clear_reload_marker_if_stale_for_pid(current);
clear_reload_marker_if_stale_for_pid(current, ForeignMarkerPolicy::MayClear);
assert!(
reload_marker_exists(),
"an in-flight Starting marker owned by this pid must survive cleanup"
Expand All @@ -829,7 +855,7 @@ mod tests {
detail: None,
}
.write();
clear_reload_marker_if_stale_for_pid(current);
clear_reload_marker_if_stale_for_pid(current, ForeignMarkerPolicy::MayClear);
assert!(
!reload_marker_exists(),
"a foreign Starting marker must be cleared"
Expand All @@ -845,13 +871,73 @@ mod tests {
detail: None,
}
.write();
clear_reload_marker_if_stale_for_pid(current);
clear_reload_marker_if_stale_for_pid(current, ForeignMarkerPolicy::MayClear);
assert!(
!reload_marker_exists(),
"a completed marker must be cleared on stale check"
);
}

/// A custom-socket server shares the runtime-dir marker file with the
/// shared daemon, but not its ownership. Clearing a marker it does not own
/// would strand the shared daemon in `Starting` and make it reject messages
/// until the marker expired.
#[tokio::test]
#[allow(clippy::await_holding_lock)]
async fn clear_reload_marker_if_stale_for_pid_preserves_a_foreign_marker() {
let _lock = crate::storage::lock_test_env();
let temp = tempfile::tempdir().expect("tempdir");
let _guard = EnvGuard::set_runtime_dir(temp.path());

let current = std::process::id();

// A foreign pid still in Starting: stale for the shared daemon, but
// owned by someone else for a custom-socket server.
ReloadState {
request_id: "req-foreign".to_string(),
hash: "hash-foreign".to_string(),
phase: ReloadPhase::Starting,
pid: current.wrapping_add(1),
timestamp: chrono::Utc::now().to_rfc3339(),
detail: None,
}
.write();

clear_reload_marker_if_stale_for_pid(current, ForeignMarkerPolicy::Preserve);
assert!(
reload_marker_exists(),
"a custom-socket server must not clear a marker it does not own"
);
}

/// A custom-socket server still reloads itself, and on Unix the exec keeps
/// the same pid, so its *own* marker must still be clearable.
#[tokio::test]
#[allow(clippy::await_holding_lock)]
async fn clear_reload_marker_if_stale_for_pid_preserves_only_foreign_markers() {
let _lock = crate::storage::lock_test_env();
let temp = tempfile::tempdir().expect("tempdir");
let _guard = EnvGuard::set_runtime_dir(temp.path());

let current = std::process::id();

ReloadState {
request_id: "req-own".to_string(),
hash: "hash-own".to_string(),
phase: ReloadPhase::SocketReady,
pid: current,
timestamp: chrono::Utc::now().to_rfc3339(),
detail: None,
}
.write();

clear_reload_marker_if_stale_for_pid(current, ForeignMarkerPolicy::Preserve);
assert!(
!reload_marker_exists(),
"a custom-socket server must still clear its own completed marker"
);
}

#[tokio::test]
#[allow(clippy::await_holding_lock)]
async fn publish_reload_socket_ready_leaves_failed_marker_untouched() {
Expand Down
Loading
Loading