Skip to content

Commit 4d03a60

Browse files
author
ikeboy003
committed
pingdotgg#437: trim titles so a whitespace title cannot drop thread.created
1 parent 24c11d5 commit 4d03a60

2 files changed

Lines changed: 91 additions & 0 deletions

File tree

‎backend/src/contract_tests.rs‎

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6974,6 +6974,80 @@ async fn terminal_panes_have_their_own_identity_shell_and_lifecycle() {
69746974
let _ = std::fs::remove_dir_all(&dir);
69756975
}
69766976

6977+
/// #437: a WHITESPACE title must not silently un-promote the draft.
6978+
///
6979+
/// `thread.created` is the only event `orchestrationEventEffects.ts:31`
6980+
/// promotes a draft on, and its contract payload types `title` as
6981+
/// `TrimmedNonEmptyString` — `TrimmedString.check(isNonEmpty())`, so the client
6982+
/// TRIMS FIRST and then requires non-empty. The backend used to accept any
6983+
/// title that was merely `!s.is_empty()`, so a titleSeed of " " encoded fine
6984+
/// here, decoded to "" there, failed the check, and took the WHOLE EVENT with
6985+
/// it. The durable row still existed and the turn still ran; only the promotion
6986+
/// was lost, which at the glass is "2 minutes for a hi" with the composer stuck
6987+
/// on Thinking — indistinguishable from a hung backend.
6988+
///
6989+
/// The assertion is that a whitespace title falls through to the SAME fallback
6990+
/// an absent title takes, so the payload can never carry a value the client
6991+
/// will refuse. Asserted on the announced thread because it and the event are
6992+
/// built from one `title` binding.
6993+
#[tokio::test]
6994+
async fn a_whitespace_title_falls_back_instead_of_shipping_an_undecodable_event() {
6995+
let (state, _dir) = test_state().await;
6996+
6997+
let (tx, mut rx) = mpsc::unbounded_channel();
6998+
request(&state, &tx, "orchestration.subscribeShell", json!({})).await;
6999+
let _ = drain(&mut rx);
7000+
7001+
let command = json!({
7002+
"type": "thread.turn.start",
7003+
"commandId": "c-ws",
7004+
"threadId": "t-ws",
7005+
// Whitespace everywhere a title can come from, so the fallback is the
7006+
// only honest source left. The message text is whitespace too —
7007+
// otherwise it would supply the title and the test would pass without
7008+
// the trim ever being exercised.
7009+
"titleSeed": " ",
7010+
"message": { "messageId": "m-ws", "role": "user", "text": " ", "attachments": [] },
7011+
"runtimeMode": "full-access",
7012+
"bootstrap": { "createThread": { "projectId": "p-ws", "title": " \t " }},
7013+
});
7014+
ensure_thread_on_shell(&state, &command).await.unwrap();
7015+
7016+
let announced = drain_until(&mut rx, std::time::Duration::from_secs(2), |f| {
7017+
f.get("values")
7018+
.and_then(|v| v.as_array())
7019+
.map(|arr| {
7020+
arr.iter()
7021+
.any(|x| x.get("kind").and_then(Value::as_str) == Some("thread-upserted"))
7022+
})
7023+
.unwrap_or(false)
7024+
})
7025+
.await;
7026+
let upsert = announced
7027+
.iter()
7028+
.flat_map(|f| f["values"].as_array().cloned().unwrap_or_default())
7029+
.find(|v| v["kind"] == "thread-upserted")
7030+
.unwrap_or_else(|| panic!("no thread-upserted in {announced:#?}"));
7031+
let title = upsert["thread"]["title"].as_str().unwrap_or_default();
7032+
7033+
assert_eq!(
7034+
title.trim(),
7035+
title,
7036+
"#437: the title still carries surrounding whitespace, so the contract \
7037+
trims it to something shorter than what the backend recorded"
7038+
);
7039+
assert!(
7040+
!title.is_empty(),
7041+
"#437: a whitespace-only title survived as an empty/blank title. \
7042+
`TrimmedNonEmptyString` refuses it, the client drops `thread.created` \
7043+
entirely, and the draft never promotes."
7044+
);
7045+
assert_eq!(
7046+
title, "New thread",
7047+
"#437: a blank title must take the SAME fallback an absent title takes"
7048+
);
7049+
}
7050+
69777051
/// #137/#78/#79: the FIRST turn's context reaches the thread.
69787052
///
69797053
/// The composer chooses a project, a worktree/branch and a runtime mode

‎backend/src/server_main.rs‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -815,8 +815,21 @@ async fn ensure_thread_on_shell(state: &AppState, command: &Value) -> Result<(),
815815
// away before the runtime ever started — the thread showed one project
816816
// while the work happened somewhere else (#137/#78/#79).
817817
let boot = command.pointer("/bootstrap/createThread");
818+
// TRIMMED, not merely non-empty (#437). These values are encoded into
819+
// `ThreadCreatedPayload`, whose `title` is `TrimmedNonEmptyString` and whose
820+
// `branch`/`worktreePath` are `NullOr(TrimmedNonEmptyString)` —
821+
// `TrimmedString.check(isNonEmpty())`, so the client TRIMS FIRST and then
822+
// requires non-empty. A whitespace-only value passes `!s.is_empty()` here
823+
// and decodes to "" there, which fails the check and drops the whole event.
824+
//
825+
// Dropping `thread.created` is not a cosmetic failure: it is the only event
826+
// orchestrationEventEffects.ts:31 promotes a draft on, so the view stays
827+
// parked on /draft/<id> while the turn runs to completion somewhere it is
828+
// not looking. That is the exact "2 minutes for a hi" symptom this finding
829+
// is about, reachable again through a title of " ".
818830
let str_of = |v: Option<&Value>| {
819831
v.and_then(Value::as_str)
832+
.map(str::trim)
820833
.filter(|s| !s.is_empty())
821834
.map(str::to_string)
822835
};
@@ -826,7 +839,11 @@ async fn ensure_thread_on_shell(state: &AppState, command: &Value) -> Result<(),
826839
command
827840
.pointer("/message/text")
828841
.and_then(|t| t.as_str())
842+
// Take the 48 chars FIRST, then trim: truncation can leave a
843+
// trailing space, and " hello wor" must not become a title the
844+
// client refuses.
829845
.map(|t| t.chars().take(48).collect::<String>())
846+
.map(|t| t.trim().to_string())
830847
.filter(|s| !s.is_empty())
831848
})
832849
.unwrap_or_else(|| "New thread".into());

0 commit comments

Comments
 (0)