brain: Identify state commits by target

573d38faa7e7226c89cde2afcf749098d12ff9b5
Include the abbreviated review target in synthetic state commit
summaries so all-ref history remains useful. Keep the human summary
independent from the machine-readable state body.

Assisted-by: Codex:gpt-5.6-sol
Alexis Sellier committed ago 1 parent 988950b2
crates/brain/src/backend.rs +16 -13
28 28
29 29
use crate::error::Error;
30 30
31 31
/// Directory marking a Jujutsu workspace.
32 32
const JJ_DIR: &str = ".jj";
33 -
/// Commit message used for synthetic brain state commits.
34 -
const SYNTHETIC_BRAIN_MESSAGE: &str = "brain state";
33 +
/// Number of hexadecimal digits shown for a review target in a state commit summary.
34 +
const REVIEW_TARGET_ABBREVIATION_LEN: usize = 7;
35 35
/// Commit-message section for accepted-hunk state and its resolved target.
36 36
const STATE_SECTION: &str = "brain-v1";
37 37
/// Hidden Git ref category holding all review state.
38 38
const BRAIN_REF_CATEGORY: &str = "brain";
39 39
/// Namespace under the brain category for acceptance state commits.
571 571
    format!("{} .. {}", review.base.as_str(), review.target.as_str())
572 572
}
573 573
574 574
/// Encodes resolved-target metadata, accepted hunks, and rejected commits.
575 575
fn encode_state(state: &PersistedState) -> String {
576 +
    let target = state.target.to_string();
577 +
    let target = &target[..REVIEW_TARGET_ABBREVIATION_LEN];
576 578
    let mut message = format!(
577 -
        "{SYNTHETIC_BRAIN_MESSAGE}\n\n{STATE_SECTION}\ntarget {}\n",
579 +
        "Brain review {target}\n\n{STATE_SECTION}\ntarget {}\n",
578 580
        state.target
579 581
    );
580 582
    for (commit, decision) in &state.rejected {
581 583
        message.push_str(&format!(
582 584
            "reject {commit} {} {}\n",
598 600
    message
599 601
}
600 602
601 603
/// Decodes resolved-target metadata, accepted hunks, and rejected commits.
602 604
fn decode_state(message: &str) -> Option<PersistedState> {
603 -
    let header = format!("{SYNTHETIC_BRAIN_MESSAGE}\n\n{STATE_SECTION}\n");
604 -
    let mut body = message.strip_prefix(&header)?.lines();
605 +
    let (_, body) = message.split_once("\n\n")?;
606 +
    let mut body = body.strip_prefix(&format!("{STATE_SECTION}\n"))?.lines();
605 607
    let mut metadata = body.next()?.split_whitespace();
606 608
    if metadata.next()? != "target" {
607 609
        return None;
608 610
    }
609 611
    let target = metadata.next()?.parse().ok()?;
2391 2393
                },
2392 2394
            )]),
2393 2395
        };
2394 2396
        let message = encode_state(&state);
2395 2397
2396 -
        assert!(message.starts_with("brain state\n\nbrain-v1\ntarget "));
2397 -
        assert_eq!(decode_state(&message), Some(state));
2398 -
        assert_eq!(decode_state(SYNTHETIC_BRAIN_MESSAGE), None);
2398 +
        assert!(message.starts_with("Brain review 2222222\n\nbrain-v1\ntarget "));
2399 +
        assert_eq!(decode_state(&message), Some(state.clone()));
2400 +
        let (_, body) = message.split_once("\n\n").unwrap();
2401 +
        assert_eq!(decode_state(&format!("brain state\n\n{body}")), Some(state));
2399 2402
    }
2400 2403
2401 2404
    #[test]
2402 2405
    fn empty_accepted_state_roundtrips_through_commit_message() {
2403 2406
        let state = PersistedState {
2416 2419
    #[test]
2417 2420
    fn accepted_state_rejects_inconsistent_line_totals() {
2418 2421
        let commit = "1111111111111111111111111111111111111111";
2419 2422
        let target = format!("target {commit}\n");
2420 2423
        let inconsistent = format!(
2421 -
            "{SYNTHETIC_BRAIN_MESSAGE}\n\n{STATE_SECTION}\n{target}{commit} alpha 0 1 2\n{commit} beta 0 1 3\n"
2424 +
            "Brain review 1111111\n\n{STATE_SECTION}\n{target}{commit} alpha 0 1 2\n{commit} beta 0 1 3\n"
2422 2425
        );
2423 2426
        let zero =
2424 -
            format!("{SYNTHETIC_BRAIN_MESSAGE}\n\n{STATE_SECTION}\n{target}{commit} alpha 0 0 1\n");
2427 +
            format!("Brain review 1111111\n\n{STATE_SECTION}\n{target}{commit} alpha 0 0 1\n");
2425 2428
        let duplicate = format!(
2426 -
            "{SYNTHETIC_BRAIN_MESSAGE}\n\n{STATE_SECTION}\n{target}{commit} alpha 0 1 1\n{commit} alpha 0 1 1\n"
2429 +
            "Brain review 1111111\n\n{STATE_SECTION}\n{target}{commit} alpha 0 1 1\n{commit} alpha 0 1 1\n"
2427 2430
        );
2428 2431
2429 2432
        assert_eq!(decode_state(&inconsistent), None);
2430 2433
        assert_eq!(decode_state(&zero), None);
2431 2434
        assert_eq!(decode_state(&duplicate), None);
2433 2436
2434 2437
    #[test]
2435 2438
    fn accepted_state_rejects_malformed_payload() {
2436 2439
        let commit = "1111111111111111111111111111111111111111";
2437 2440
        let extra_field = format!(
2438 -
            "{SYNTHETIC_BRAIN_MESSAGE}\n\n{STATE_SECTION}\ntarget {commit}\n{commit} alpha 0 1 1 extra\n"
2441 +
            "Brain review 1111111\n\n{STATE_SECTION}\ntarget {commit}\n{commit} alpha 0 1 1 extra\n"
2439 2442
        );
2440 2443
        let missing_target =
2441 -
            format!("{SYNTHETIC_BRAIN_MESSAGE}\n\n{STATE_SECTION}\n{commit} alpha 0 1 1\n");
2444 +
            format!("Brain review 1111111\n\n{STATE_SECTION}\n{commit} alpha 0 1 1\n");
2442 2445
2443 2446
        assert_eq!(decode_state(&extra_field), None);
2444 2447
        assert_eq!(decode_state(&missing_target), None);
2445 2448
    }
2446 2449
f +0 -0
new.rs +0 -0
image.png +0 -0
new.txt +0 -0