The PRD's first acceptance gate now holds: grep -RinE '\bx\.ai\b|grok' crates/ --include='*.rs' → 0 matches (exempt: NOTICE and third-party license archives, README provenance, and the required 'Based on Grok Build Open Source' attribution, now sourced from version_attribution.txt). Wire-visible renames (both sides in this repo, changed in lockstep): - Auth method id 'grok.com' → 'kimi-code' (AuthMethodKind::KimiCode). - Every x.ai/* and _x.ai/* ACP ext method and meta key → kigi/* / _kigi/* (~200 names; grokShell → kigiShell). Session-file replay keeps a read-side alias for the legacy '_x.ai/session/update' method so existing updates.jsonl histories load; writes emit only the new name (both directions test-pinned). - Agent types grok-build* → kigi* with a documented legacy-prefix alias at resolution time so persisted sessions keep resolving. - ToolNamespace/BuiltinAgentName GrokBuild* → Kigi* (wire snake_case kigi/kigi_concise/kigi_hashline; schema regenerated); grok_build implementation dirs renamed to kigi*. - x-grok-* headers → x-kigi-*, __GROK_* sentinels → __KIGI_*, themes grokday/groknight → kigiday/kiginight (old persisted values fall back to the default theme), web_fetch allowlist xAI hosts → kimi.com + moonshot platforms, changelog CDN → this repo, grok-build changelog archives deleted. - BYOK default endpoint removed: [endpoints] api_base_url is now truly optional with NO default — consumers fail fast with the flag name when unset (no silent x.ai egress). Mock harnesses inject it explicitly. - System-prompt identity fixed: 'released by xAI' → 'an unofficial community CLI for Kimi' (template + regenerated encrypted form). Also repaired pre-existing grok-era test debt found by the sweep: the stale trace_classify default-model pin, the grok-pager UA label test, pty-harness stale-binary reuse and non-hermetic moonshot routing (a PTY test could previously reach the real api.moonshot.cn), and the outdated oauth fixture scope key. Gates: §9 grep 0; fmt clean; workspace check/clippy 0/0 (-D warnings); FULL cargo test --workspace: 234 suites, 21,961 passed, 0 failed; deny advisories ok.
104 lines
3.4 KiB
Rust
104 lines
3.4 KiB
Rust
use std::fmt;
|
|
|
|
use tokio::sync::{mpsc, oneshot};
|
|
|
|
use crate::{
|
|
common::{AcpChannelFailure, AcpResult, acp_channel_failure_error},
|
|
message::{AcpAgentMessage, AcpArgs, AcpClientMessage, AcpMethod, AcpRequest},
|
|
};
|
|
|
|
/// Receiver/sender pair, either for client/agent or agent/client message types.
|
|
pub struct AcpChannel<I, O> {
|
|
pub rx: mpsc::UnboundedReceiver<I>,
|
|
pub tx: mpsc::UnboundedSender<O>,
|
|
}
|
|
|
|
impl<I: AcpMethod, O: AcpMethod> AcpChannel<I, O> {
|
|
pub fn new(rx: mpsc::UnboundedReceiver<I>, tx: mpsc::UnboundedSender<O>) -> Self {
|
|
Self { rx, tx }
|
|
}
|
|
}
|
|
|
|
/// Client channel: receive client messages from agent, send agent messages to agent.
|
|
pub type AcpClientChannel = AcpChannel<AcpClientMessage, AcpAgentMessage>;
|
|
/// Agent channel: receive agent messages from client, send client messages to client.
|
|
pub type AcpAgentChannel = AcpChannel<AcpAgentMessage, AcpClientMessage>;
|
|
|
|
/// Create a linked pair of client/agent channels.
|
|
pub fn acp_channels() -> (AcpClientChannel, AcpAgentChannel) {
|
|
let (tx1, rx1) = mpsc::unbounded_channel();
|
|
let (tx2, rx2) = mpsc::unbounded_channel();
|
|
(AcpChannel::new(rx1, tx2), AcpChannel::new(rx2, tx1))
|
|
}
|
|
|
|
pub async fn acp_send<R, T>(request: T, tx: &mpsc::UnboundedSender<R>) -> AcpResult<T::Response>
|
|
where
|
|
T: AcpRequest,
|
|
R: From<AcpArgs<T>> + fmt::Debug,
|
|
{
|
|
let (response_tx, response_rx) = oneshot::channel();
|
|
let method = request.method_name();
|
|
let args = AcpArgs {
|
|
request,
|
|
response_tx,
|
|
};
|
|
|
|
tx.send(args.into()).map_err(|_| {
|
|
acp_channel_failure_error(
|
|
format!("unable to send '{method}' request, channel closed"),
|
|
AcpChannelFailure::SendFailed,
|
|
)
|
|
})?;
|
|
|
|
response_rx.await.map_err(|_| {
|
|
acp_channel_failure_error(
|
|
format!("unable to receive '{method}' response, channel closed"),
|
|
AcpChannelFailure::RecvFailed,
|
|
)
|
|
})?
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod acp_send_failure_tests {
|
|
use super::acp_send;
|
|
use crate::common::{AcpChannelFailure, acp_channel_failure};
|
|
use crate::message::AcpAgentMessage;
|
|
use agent_client_protocol as acp;
|
|
use tokio::sync::mpsc;
|
|
|
|
fn ext_request() -> acp::ExtRequest {
|
|
acp::ExtRequest::new(
|
|
"kigi/test",
|
|
serde_json::value::to_raw_value(&serde_json::json!({}))
|
|
.unwrap()
|
|
.into(),
|
|
)
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn send_failed_when_receiver_dropped_before_send() {
|
|
let (tx, rx) = mpsc::unbounded_channel::<AcpAgentMessage>();
|
|
drop(rx); // no peer listening -> enqueue fails
|
|
let err = acp_send(ext_request(), &tx).await.unwrap_err();
|
|
assert_eq!(
|
|
acp_channel_failure(&err),
|
|
Some(AcpChannelFailure::SendFailed)
|
|
);
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn recv_failed_when_response_channel_dropped_after_send() {
|
|
let (tx, mut rx) = mpsc::unbounded_channel::<AcpAgentMessage>();
|
|
let mut send_fut = Box::pin(acp_send(ext_request(), &tx));
|
|
// First poll enqueues the request, then parks on the response channel.
|
|
assert!(futures::poll!(send_fut.as_mut()).is_pending());
|
|
// The peer "receives" the request then drops it (dropping response_tx).
|
|
drop(rx.try_recv().expect("request should be enqueued"));
|
|
let err = send_fut.await.unwrap_err();
|
|
assert_eq!(
|
|
acp_channel_failure(&err),
|
|
Some(AcpChannelFailure::RecvFailed)
|
|
);
|
|
}
|
|
}
|