fix(web): fail-fast when invoke lock is busy; document single-interp limit

Long sse-live chats held INVOKE_LOCK indefinitely and starved short routes
(UI freeze). Timed try-lock returns interpreter busy → HTTP 503; ADR 0008
records that HTTP is multi-goroutine while Marqdo evaluation stays single-flight.
Also lands plan/delta streaming and soft map guards used by qdagent chat.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
cflmyandCursor committed 2026-10-26 13:27:53 +08:00
1 parent b792f44556
commit 1b11fe063a
11 files changed
+838 -163

No files matched your search

+104 -4
View File
@@ -98,22 +98,88 @@ fn cancel_requested() -> bool {
{
return true;
}
// Web sse-live creates an empty token file up front; only an explicit
// body ("1" / "cancelled") means the client asked to stop. Empty ≠ cancel.
if let Ok(tok) = std::env::var("MARQDO_CANCEL_TOKEN") {
let path = std::path::Path::new(&tok);
if path.is_file() {
if let Ok(body) = std::fs::read_to_string(path) {
let t = body.trim();
return t.is_empty() || t == "1" || t.eq_ignore_ascii_case("cancelled");
return t == "1" || t.eq_ignore_ascii_case("cancelled");
}
return true;
}
}
false
}
fn parse_sse_events(reader: impl BufRead, echo: bool) -> Result<Vec<Value>, String> {
/// Mirror host `net::push_openai_delta_events`: append semantic frames to
/// `MARQDO_EVENT_FORWARD` so subtask/web sse-live can flush tokens before the
/// lib call returns (OpenAI-style incremental SSE).
fn forward_event_bus_frame(ev: &Value) {
let Ok(path) = std::env::var("MARQDO_EVENT_FORWARD") else {
return;
};
if path.is_empty() {
return;
}
use std::io::Write;
if let Ok(mut f) = std::fs::OpenOptions::new()
.create(true)
.append(true)
.open(&path)
{
let _ = writeln!(f, "{ev}");
let _ = f.flush();
}
}
/// Publish `{type:delta|plan|reasoning|error, text|message}` from one OpenAI chunk.
/// `content_type` is usually `delta` (answer tokens) or `plan` (parent protocol LLM).
fn forward_openai_chunk(data: &str, content_type: &str) {
let data = data.trim();
if data.is_empty() || data == "[DONE]" {
return;
}
let Ok(v) = serde_json::from_str::<Value>(data) else {
return;
};
if let Some(msg) = v.get("error").and_then(|e| {
e.get("message")
.and_then(|m| m.as_str())
.or_else(|| e.as_str())
}) {
forward_event_bus_frame(&json!({"type": "error", "message": msg}));
return;
}
let reasoning = v
.pointer("/choices/0/delta/reasoning_content")
.and_then(|c| c.as_str())
.unwrap_or("");
if !reasoning.is_empty() {
forward_event_bus_frame(&json!({"type": "reasoning", "text": reasoning}));
}
let content = v
.pointer("/choices/0/delta/content")
.and_then(|c| c.as_str())
.unwrap_or("");
if !content.is_empty() {
let ty = if content_type.is_empty() {
"delta"
} else {
content_type
};
forward_event_bus_frame(&json!({"type": ty, "text": content}));
}
}
fn parse_sse_events(
reader: impl BufRead,
echo: bool,
content_type: &str,
) -> Result<Vec<Value>, String> {
let mut events = Vec::new();
let mut data_lines: Vec<String> = Vec::new();
let mut accumulated = String::new();
for line in reader.lines() {
if cancel_requested() {
events.push(json!({"type": "cancelled", "message": "MARQDO_CANCEL"}));
@@ -128,9 +194,23 @@ fn parse_sse_events(reader: impl BufRead, echo: bool) -> Result<Vec<Value>, Stri
let data = data_lines.join("\n");
data_lines.clear();
if data.trim() == "[DONE]" {
// Parent owns run lifecycle `done`; only forward for answer streams.
if content_type == "delta" || content_type.is_empty() {
let done = json!({"type": "done", "result": accumulated});
forward_event_bus_frame(&done);
}
events.push(json!({"type": "done", "data": "[DONE]"}));
break;
}
forward_openai_chunk(&data, content_type);
if let Ok(v) = serde_json::from_str::<Value>(&data) {
if let Some(c) = v
.pointer("/choices/0/delta/content")
.and_then(|x| x.as_str())
{
accumulated.push_str(c);
}
}
events.push(json!({"type": "data", "data": data}));
}
continue;
@@ -142,8 +222,13 @@ fn parse_sse_events(reader: impl BufRead, echo: bool) -> Result<Vec<Value>, Stri
if !data_lines.is_empty() {
let data = data_lines.join("\n");
if data.trim() == "[DONE]" {
if content_type == "delta" || content_type.is_empty() {
let done = json!({"type": "done", "result": accumulated});
forward_event_bus_frame(&done);
}
events.push(json!({"type": "done", "data": "[DONE]"}));
} else {
forward_openai_chunk(&data, content_type);
events.push(json!({"type": "data", "data": data}));
}
}
@@ -252,6 +337,8 @@ fn chat_completions(args: &Value) -> Result<Value, String> {
let messages = build_messages(args)?;
let stream = opt_bool(args, "stream", false);
let echo = opt_bool(args, "echo", false);
// Answer tokens → "delta"; parent plan protocol → "plan" (UI process trail).
let stream_kind = opt_text(args, "stream_kind").unwrap_or("delta");
let suffix = opt_text(args, "suffix").unwrap_or("/chat/completions");
let bearer = opt_text(args, "bearer").unwrap_or("Bearer ");
@@ -309,7 +396,8 @@ fn chat_completions(args: &Value) -> Result<Value, String> {
"transport": "plugin",
}));
}
let events = parse_sse_events(BufReader::new(r.into_reader()), echo)?;
let events =
parse_sse_events(BufReader::new(r.into_reader()), echo, stream_kind)?;
Ok(json!({
"events": events,
"finish": "stop",
@@ -525,6 +613,18 @@ mod tests {
assert_eq!(out["finish"], "cancelled");
}
#[test]
fn f06_empty_cancel_token_is_not_cancelled() {
// sse-live creates an empty token file; that must NOT abort the turn.
let tok = std::env::temp_dir().join(format!("llm-cancel-empty-{}", std::process::id()));
std::fs::write(&tok, "").unwrap();
std::env::set_var("MARQDO_CANCEL_TOKEN", &tok);
std::env::remove_var("MARQDO_CANCEL");
assert!(!cancel_requested());
std::env::remove_var("MARQDO_CANCEL_TOKEN");
let _ = std::fs::remove_file(&tok);
}
/// F01: two local endpoints receive distinct models/auth — no cross-talk.
#[test]
fn f01_dual_endpoint_isolation() {
+15
View File
@@ -799,6 +799,13 @@ func (g *gzipResponseWriter) Hijack() (net.Conn, *bufio.ReadWriter, error) {
return nil, nil, fmt.Errorf("underlying ResponseWriter does not support hijacking")
}
func (g *gzipResponseWriter) Flush() {
_ = g.w.Flush()
if f, ok := g.ResponseWriter.(http.Flusher); ok {
f.Flush()
}
}
type statusRecorder struct {
http.ResponseWriter
status int
@@ -816,6 +823,14 @@ func (s *statusRecorder) Hijack() (net.Conn, *bufio.ReadWriter, error) {
return nil, nil, fmt.Errorf("underlying ResponseWriter does not support hijacking")
}
// Flush must be forwarded so sse-live / EventBus token streams are not buffered
// behind access-log wrapping (otherwise clients see 0 bytes until the invoke ends).
func (s *statusRecorder) Flush() {
if f, ok := s.ResponseWriter.(http.Flusher); ok {
f.Flush()
}
}
func writeJSON(w http.ResponseWriter, code int, v any) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(code)
+18 -2
View File
@@ -137,6 +137,11 @@ func (st *state) handleInvoke(w http.ResponseWriter, r *http.Request, route invo
if err != nil {
code := http.StatusInternalServerError
msg := err.Error()
if strings.Contains(msg, "interpreter busy") {
w.Header().Set("Retry-After", "2")
jsonErr(w, http.StatusServiceUnavailable, msg)
return
}
if strings.Contains(msg, "unknown library") ||
strings.Contains(msg, "call_lib_path: need") ||
strings.Contains(msg, "not allowed") ||
@@ -260,6 +265,10 @@ func (st *state) handleInvokeLiveSSE(w http.ResponseWriter, r *http.Request, rou
done := make(chan callResult, 1)
go func() {
v, e := plugin.CallLibPath(route.FnPath, args)
if e != nil && strings.Contains(e.Error(), "interpreter busy") {
// Another invoke still holds the lock — surface as failed run, not hang.
e = fmt.Errorf("interpreter busy: try again shortly")
}
done <- callResult{value: v, err: e}
}()
@@ -339,13 +348,20 @@ func normalizeLiveSSELine(seq uint64, line string) map[string]any {
base["seq"] = seq
return base
}
// LLM plugin / net SSE: {type:delta|reasoning, text:...}
if t, _ := obj["type"].(string); t == "delta" || t == "reasoning" {
// LLM plugin / net SSE: {type:delta|plan|reasoning, text:...}
if t, _ := obj["type"].(string); t == "delta" || t == "plan" || t == "reasoning" {
text, _ := obj["text"].(string)
base["type"] = "message.delta"
base["payload"] = map[string]any{"delta": text, "kind": t}
return base
}
// Plan process cards — keep as message.delta for back-compat, but never
// promote `result` into payload.delta (that caused full-answer duplication).
if t, _ := obj["type"].(string); t == "decompose" || t == "decision" || t == "await" || t == "round" || t == "done" || t == "tool_start" || t == "tool_end" || t == "plan_act" {
base["type"] = "message.delta"
base["payload"] = obj
return base
}
if text, ok := obj["text"].(string); ok && text != "" {
base["type"] = "message.delta"
base["payload"] = map[string]any{"delta": text}