Skip to main content

runtime/turn/
prompt.rs

1// This Source Code Form is subject to the terms of the Mozilla Public
2// License, v. 2.0. If a copy of the MPL was not distributed with this
3// file, You can obtain one at https://mozilla.org/MPL/2.0/.
4// Copyright (c) 2026 2youg1 and the sprawling contributors
5
6//! What a run has recorded of its prompt.
7//!
8//! `prompt_assembled` is written once per run: on the first turn, and
9//! again only when the payload a turn assembles differs from the last
10//! one written. The prefix is frozen for the run and the breakpoint plan
11//! moves only when the conversation goes from empty to not, so every
12//! later line was a copy of the first; what does move between turns is
13//! recorded by `prompt_shape_compared`. The comparison is on the payload
14//! rather than on the turn index, so a turn whose request really differs
15//! still gets its own line (`crates/runtime/spec/Prefix.lean` ยง8-39, item 5).
16
17use kernel::Payload;
18
19use crate::prefix::FrozenPrefix;
20
21/// The prompt one turn is assembled from: the run's frozen prefix, and
22/// what the run has already recorded of it.
23#[derive(Debug)]
24pub struct RunPrompt<'r> {
25    pub(super) prefix: &'r FrozenPrefix,
26    pub(super) recorded: &'r mut PromptRecord,
27}
28
29impl<'r> RunPrompt<'r> {
30    pub fn new(prefix: &'r FrozenPrefix, recorded: &'r mut PromptRecord) -> RunPrompt<'r> {
31        RunPrompt { prefix, recorded }
32    }
33}
34
35/// The `prompt_assembled` payload a run wrote last; empty before its
36/// first turn. It lives across turns, in the run's own state.
37#[derive(Debug, Default)]
38pub struct PromptRecord {
39    written: Option<Payload>,
40}
41
42impl PromptRecord {
43    /// Whether `payload` is what this run last wrote, so writing it again
44    /// would add a copy rather than a fact.
45    pub(super) fn holds(&self, payload: &Payload) -> bool {
46        self.written.as_ref() == Some(payload)
47    }
48
49    /// Remembers `payload` as the line this run wrote last.
50    pub(super) fn remember(&mut self, payload: Payload) {
51        self.written = Some(payload);
52    }
53}