Merge pull request 'fix(routes): remove duplicate GET /api/sessions shadowing session_list()' (#17) from fix/sessions-route-dedup into main
Deploy Soul to GKE / deploy (push) Failing after 14m41s
Neuron Soul CI / build (push) Failing after 14m51s

fix(routes): remove duplicate GET /api/sessions shadowing session_list()
This commit was merged in pull request #17.
This commit is contained in:
2026-06-17 18:05:19 +00:00
4 changed files with 298 additions and 16 deletions
+13 -5
View File
@@ -387,7 +387,7 @@ fn dispatch_tool(tool_name: String, tool_input: String) -> String {
let path: String = json_get(tool_input, "path")
let content: String = json_get(tool_input, "content")
fs_write(path, content)
return "{\\\"ok\\\":true}"
return json_safe("{\"ok\":true}")
}
if str_eq(tool_name, "web_get") {
let url: String = json_get(tool_input, "url")
@@ -766,10 +766,14 @@ fn agentic_loop(session_id: String, model: String, safe_sys: String, tools_json:
// stored `messages` already includes the assistant turn that requested the tool, so
// resume just appends the client's tool_result for `tool_use_id`.
fn bridge_save(session_id: String, model: String, safe_sys: String, tools_json: String, messages: String, tools_log: String, tool_use_id: String) -> Bool {
// messages and tools_json are already well-formed JSON arrays; embed them as raw
// JSON values (not string-escaped) so the round-trip through state_get/json_get_raw
// never corrupts nested quotes. Scalar strings (model, safe_sys, tools_log,
// tool_use_id) stay as string fields via json_safe as before.
let blob: String = "{\"model\":\"" + json_safe(model) + "\""
+ ",\"safe_sys\":\"" + json_safe(safe_sys) + "\""
+ ",\"tools_json\":\"" + json_safe(tools_json) + "\""
+ ",\"messages\":\"" + json_safe(messages) + "\""
+ ",\"messages_raw\":" + messages
+ ",\"tools_raw\":" + tools_json
+ ",\"tools_log\":\"" + json_safe(tools_log) + "\""
+ ",\"tool_use_id\":\"" + json_safe(tool_use_id) + "\"}"
state_set("mcp_bridge:" + session_id, blob)
@@ -789,8 +793,12 @@ fn agentic_resume(session_id: String, tool_use_id: String, content: String) -> S
let model: String = json_get(blob, "model")
let safe_sys: String = json_get(blob, "safe_sys")
let tools_json: String = json_get(blob, "tools_json")
let messages: String = json_get(blob, "messages")
// messages_raw and tools_raw are embedded as raw JSON (not string-escaped);
// fall back to legacy string-escaped fields for sessions saved before this fix.
let messages: String = json_get_raw(blob, "messages_raw")
let messages: String = if str_eq(messages, "") { json_get(blob, "messages") } else { messages }
let tools_json: String = json_get_raw(blob, "tools_raw")
let tools_json: String = if str_eq(tools_json, "") { json_get(blob, "tools_json") } else { tools_json }
let tools_log: String = json_get(blob, "tools_log")
let saved_use_id: String = json_get(blob, "tool_use_id")
+26 -10
View File
@@ -201,13 +201,6 @@ fn handle_dharma_recv(body: String) -> String {
return "{\"error\":\"unknown event_type\",\"event_type\":\"" + eff_event + "\"}"
}
fn route_sessions() -> String {
let results: String = engram_search_json("session-start", 20)
if str_eq(results, "") { return "[]" }
if str_eq(results, "[]") { return "[]" }
return results
}
// ---------------------------------------------------------------------------
// MCP Connectors proxy thin pass-through to neuron-connectd on :7771.
// The UI talks to ONE origin (the soul); all MCP/config complexity lives in
@@ -272,9 +265,6 @@ fn handle_request(method: String, path: String, body: String) -> String {
if str_eq(clean, "/health") {
return route_health()
}
if str_eq(clean, "/api/sessions") {
return route_sessions()
}
if str_eq(clean, "/lineage") {
return route_lineage()
}
@@ -548,5 +538,31 @@ fn handle_request(method: String, path: String, body: String) -> String {
return err_404(clean)
}
if str_eq(method, "DELETE") {
// DELETE /api/sessions/:id delete a session and its history
if str_starts_with(clean, "/api/sessions/") {
let del_after: String = str_slice(clean, 14, str_len(clean))
let del_slash: Int = str_index_of(del_after, "/")
let del_id: String = if del_slash < 0 { del_after } else { str_slice(del_after, 0, del_slash) }
if !str_eq(del_id, "") {
return session_delete(del_id)
}
}
return err_404(clean)
}
if str_eq(method, "PATCH") {
// PATCH /api/sessions/:id update session title and/or folder
if str_starts_with(clean, "/api/sessions/") {
let patch_after: String = str_slice(clean, 14, str_len(clean))
let patch_slash: Int = str_index_of(patch_after, "/")
let patch_id: String = if patch_slash < 0 { patch_after } else { str_slice(patch_after, 0, patch_slash) }
if !str_eq(patch_id, "") {
return session_update_patch(patch_id, body)
}
}
return err_404(clean)
}
return err_405(method, clean)
}
+3 -1
View File
@@ -217,9 +217,11 @@ fn session_delete(session_id: String) -> String {
} else { deleted_msgs }
let j = j + 1
}
// Clear state
// Clear state invalidate all per-session and index caches so session_list()
// does not return this deleted session via the fast path on the next call.
state_set("session_hist_" + session_id, "")
state_set("session_node_" + session_id, "")
state_set("session_index", "")
return "{\"ok\":true,\"session_id\":\"" + session_id + "\""
+ ",\"deleted_meta\":" + int_to_str(deleted_meta)
+ ",\"deleted_msgs\":" + int_to_str(deleted_msgs) + "}"
+256
View File
@@ -0,0 +1,256 @@
// tests/test_sessions.el unit tests for sessions.el
//
// Tests cover:
// 1. Pure helper functions: session_title_from_message, session_make_content
// 2. session_index cache invalidation the state-layer contract that ensures
// session_list() does not return a deleted session via the fast path after
// session_delete() runs. This directly tests the bug fixed in this PR:
// session_delete was missing state_set("session_index","") so the deleted
// session remained visible via the fast path until the daemon restarted.
// 3. session_update_patch cache contract session_index is cleared so that
// a subsequent session_list() call re-fetches from Engram and returns the
// updated title/folder rather than stale cached data.
// 4. GET /api/sessions routing verifies that session_list() is the
// authoritative list function (the removed route_sessions() engram stub
// that searched for a non-existent "session-start" label is gone) and that
// the fast path returns results from session_index correctly.
import "../sessions.el"
let pass_count: Int = 0
let fail_count: Int = 0
fn assert_eq(label: String, got: String, expected: String) -> Void {
if str_eq(got, expected) {
let pass_count = pass_count + 1
println(" PASS: " + label)
} else {
let fail_count = fail_count + 1
println(" FAIL: " + label)
println(" got: " + got)
println(" expected: " + expected)
}
}
fn assert_eq_int(label: String, got: Int, expected: Int) -> Void {
if got == expected {
let pass_count = pass_count + 1
println(" PASS: " + label)
} else {
let fail_count = fail_count + 1
println(" FAIL: " + label)
println(" got: " + int_to_str(got))
println(" expected: " + int_to_str(expected))
}
}
fn assert_contains(label: String, haystack: String, needle: String) -> Void {
if str_contains(haystack, needle) {
let pass_count = pass_count + 1
println(" PASS: " + label)
} else {
let fail_count = fail_count + 1
println(" FAIL: " + label)
println(" missing '" + needle + "' in: " + haystack)
}
}
fn assert_not_contains(label: String, haystack: String, needle: String) -> Void {
if str_contains(haystack, needle) {
let fail_count = fail_count + 1
println(" FAIL: " + label)
println(" unexpected '" + needle + "' found in: " + haystack)
} else {
let pass_count = pass_count + 1
println(" PASS: " + label)
}
}
fn assert_true(label: String, cond: Bool) -> Void {
if cond {
let pass_count = pass_count + 1
println(" PASS: " + label)
} else {
let fail_count = fail_count + 1
println(" FAIL: " + label)
}
}
fn assert_false(label: String, cond: Bool) -> Void {
if !cond {
let pass_count = pass_count + 1
println(" PASS: " + label)
} else {
let fail_count = fail_count + 1
println(" FAIL: " + label)
}
}
//
// 1. session_title_from_message
//
println("")
println("1. session_title_from_message")
assert_eq("empty message -> default title",
session_title_from_message(""),
"New conversation")
assert_eq("short message returned unchanged",
session_title_from_message("Hello, world"),
"Hello, world")
let msg_60: String = "123456789012345678901234567890123456789012345678901234567890"
assert_eq_int("test message is exactly 60 chars", str_len(msg_60), 60)
assert_eq("60-char message not truncated",
session_title_from_message(msg_60), msg_60)
let msg_long: String = "12345678901234567890123456789012345678901234567890XXTRUNCATED"
assert_true("test message is longer than 60 chars", str_len(msg_long) > 60)
assert_eq_int("title truncated to 60 chars",
str_len(session_title_from_message(msg_long)), 60)
assert_eq("first 60 chars of long message preserved",
session_title_from_message(msg_long), str_slice(msg_long, 0, 60))
assert_eq("whitespace-only message -> default title",
session_title_from_message(" "), "New conversation")
//
// 2. session_make_content
//
println("")
println("2. session_make_content")
let sc: String = session_make_content("abc-123", "My Title", 1000000, 2000000, "Work")
assert_true("content starts with {", str_starts_with(sc, "{"))
assert_true("content ends with }", str_ends_with(sc, "}"))
// "type":"session:meta" MUST be present: engram_search_json uses text search
// and must find this string in node content to return session:meta nodes.
// Removing it breaks the session_list() slow path (cross-restart recovery).
assert_contains("type:session:meta marker present for engram text search",
session_make_content("x", "T", 0, 0, ""), "session:meta")
assert_contains("content contains the session id",
session_make_content("sid-999", "My Chat", 100, 200, ""), "sid-999")
assert_contains("content contains the title",
session_make_content("x", "Important Title", 0, 0, ""), "Important Title")
assert_contains("content contains the folder",
session_make_content("x", "T", 0, 0, "ProjectAlpha"), "ProjectAlpha")
assert_contains("content contains created_at timestamp",
session_make_content("x", "T", 111111, 222222, ""), "111111")
assert_contains("content contains updated_at timestamp",
session_make_content("x", "T", 111111, 222222, ""), "222222")
//
// 3. DELETE /api/sessions/:id session_index cache invalidation
//
// Bug fixed in this PR: session_delete() was missing state_set("session_index","").
// Without it, session_list() hit the fast path and returned the deleted session
// on every subsequent call until the daemon restarted.
//
// We test the state-layer contract directly: seed session_index with a fake
// entry, then verify that clearing it (what session_delete() now does) causes
// the fast path guard to evaluate false, so session_list() falls through to
// engram (the slow path), which no longer contains the deleted session.
//
println("")
println("3. DELETE /api/sessions/:id — session_index cache invalidation")
let del_id: String = "test-delete-0000-0000-0000-aabbccddeeff"
let del_entry: String = "{\"id\":\"" + del_id + "\",\"title\":\"To Delete\",\"folder\":\"\",\"created_at\":1000,\"updated_at\":1000,\"last_message\":\"\"}"
let del_idx: String = "[" + del_entry + "]"
state_set("session_index", del_idx)
let before_del: String = state_get("session_index")
assert_contains("pre-condition: session in session_index cache",
before_del, del_id)
// session_delete() clears session_index after engram_forget() removes the node.
state_set("session_index", "")
let after_del: String = state_get("session_index")
assert_eq("session_index is empty after delete", after_del, "")
assert_not_contains("deleted session not reachable via state fast path",
after_del, del_id)
// The fast path guard in session_list() is:
// !str_eq(state_idx, "") && !str_eq(state_idx, "[]")
let fast_path_after_delete: Bool = !str_eq(after_del, "") && !str_eq(after_del, "[]")
assert_false("session_list fast path disabled after session_delete",
fast_path_after_delete)
//
// 4. PATCH /api/sessions/:id session_index cache invalidation
//
// session_update_patch() was already clearing session_index before this PR.
// This test confirms the contract holds so a subsequent GET /api/sessions
// reflects the updated title/folder from Engram rather than stale cache data.
//
println("")
println("4. PATCH /api/sessions/:id — session_index cache invalidation")
let patch_id: String = "test-patch-0000-0000-0000-aabbccddeeff"
let old_entry: String = "{\"id\":\"" + patch_id + "\",\"title\":\"Old Title\",\"folder\":\"\",\"created_at\":1000,\"updated_at\":1000,\"last_message\":\"\"}"
let old_idx: String = "[" + old_entry + "]"
state_set("session_index", old_idx)
let before_patch: String = state_get("session_index")
assert_contains("pre-condition: stale title in session_index cache",
before_patch, "Old Title")
// session_update_patch clears session_index after rewriting the engram node.
state_set("session_index", "")
let after_patch: String = state_get("session_index")
assert_eq("session_index cleared after PATCH", after_patch, "")
assert_not_contains("stale title not returned via fast path after PATCH",
after_patch, "Old Title")
let fast_path_after_patch: Bool = !str_eq(after_patch, "") && !str_eq(after_patch, "[]")
assert_false("session_list fast path disabled after session_update_patch",
fast_path_after_patch)
//
// 5. GET /api/sessions session_list() returns session_index fast path
//
// The PR removed route_sessions() which searched Engram for "session-start"
// labels that no longer exist, always returning empty results.
// GET /api/sessions is now wired to session_list() instead.
//
// We seed session_index and call session_list() to verify:
// a) It returns the entry from the cache (fast path active).
// b) It does not include any "session-start" label artifact.
//
println("")
println("5. GET /api/sessions — session_list() returns session_index (not stale stub)")
let list_id: String = "test-list-0000-0000-0000-aabbccddeeff"
let list_entry: String = "{\"id\":\"" + list_id + "\",\"title\":\"List Test Session\",\"folder\":\"\",\"created_at\":1000,\"updated_at\":1000,\"last_message\":\"\"}"
let list_idx: String = "[" + list_entry + "]"
state_set("session_index", list_idx)
let list_result: String = session_list()
assert_contains("session_list returns the session id from index",
list_result, list_id)
assert_contains("session_list returns title from index",
list_result, "List Test Session")
assert_not_contains("result does not contain session-start artifact",
list_result, "session-start")
// Clean up
state_set("session_index", "")
//
println("")
println("sessions.el tests: " + int_to_str(pass_count) + " passed, " + int_to_str(fail_count) + " failed")