kcode-k1-chat-thread-durable-state 0.4.17

Authorized durable state transitions for one K1 chat thread
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
#![forbid(unsafe_code)]

use kcode_k1_access_kmap::K1AccessKmap;
use kcode_k1_chat_persistence::Session;
pub use kcode_k1_chat_state::BoxId;
use kcode_k1_chat_state::{AGENT_MESSAGE_TYPE, AGENT_RESPONSE_TYPE, USER_MESSAGE_TYPE};
pub use kcode_k1_chat_thread_durable_turn::{
    BoxValue, ChatBox, ChatDiagnostic, EventRecord, ModelUsage, PreflightItem, PreflightMode,
    PreparedCall, PreparedMailboxFlush, PreparedPreflightCall, Status, TokenBreakdown, ToolCallId,
};
use kcode_k1_chat_thread_durable_turn::{DurableTurn, RestartError, ShimOutput};
pub use kcode_k1_chat_thread_ktools::{AccessContext, AccessPolicy, ProfileId, SetLaunchNodeKtool};
use kcode_k1_chat_thread_ktools::{ChatThreadKtoolExecutor, ChatThreadKtools};
use serde::Deserialize;
use std::sync::Arc;

#[derive(Clone, Debug, Eq, PartialEq)]
pub enum TransitionError {
    Unauthorized,
    NotStalled,
    NotRestartable,
    Internal(String),
}

pub struct DurableThread {
    turn: DurableTurn,
    ktools: ChatThreadKtoolExecutor,
    authorized: bool,
}

impl DurableThread {
    pub fn recover(session: Session, kmap: Arc<K1AccessKmap>) -> Result<Self, String> {
        Self::recover_with_ktools(session, ChatThreadKtools::new(kmap))
    }

    pub fn recover_with_social(
        session: Session,
        kmap: Arc<K1AccessKmap>,
        social: kcode_k1_ktool_social::SocialKtools,
    ) -> Result<Self, String> {
        Self::recover_with_ktools(session, ChatThreadKtools::new_with_social(kmap, social))
    }

    pub fn recover_with_social_and_set_launch_node(
        session: Session,
        kmap: Arc<K1AccessKmap>,
        social: kcode_k1_ktool_social::SocialKtools,
        set_launch_node: SetLaunchNodeKtool,
    ) -> Result<Self, String> {
        Self::recover_with_ktools(
            session,
            ChatThreadKtools::new_with_social_and_set_launch_node(kmap, social, set_launch_node),
        )
    }

    pub fn boxes(&self) -> &[ChatBox] {
        self.turn.boxes()
    }

    pub fn events(&self) -> Vec<EventRecord> {
        self.turn.events()
    }

    pub fn status(&self) -> Status {
        self.turn.status()
    }

    pub fn preflight_calls(&self) -> &[PreparedPreflightCall] {
        self.turn.preflight_calls()
    }

    pub fn preflight_executor(&self) -> ChatThreadKtoolExecutor {
        self.ktools.clone()
    }

    pub fn prepare_preflight(
        &mut self,
        context: AccessContext,
        profile_id: ProfileId,
        policy: AccessPolicy,
        items: Vec<PreflightItem>,
    ) -> Result<Vec<PreparedPreflightCall>, TransitionError> {
        for item in &items {
            if let PreflightItem::KtoolCall { name, .. } = item {
                let supported = self
                    .ktools
                    .supports(name)
                    .map_err(TransitionError::Internal)?;
                if !supported {
                    return Err(TransitionError::Internal(
                        "unsupported preflight Ktool".to_owned(),
                    ));
                }
            }
        }
        let installed = self.bind_authorization(context, profile_id, policy)?;
        match self.turn.prepare_preflight(items) {
            Ok(calls) => Ok(calls),
            Err(error) => {
                if installed {
                    self.clear_authorization();
                }
                Err(TransitionError::Internal(error))
            }
        }
    }

    pub fn authorize_preflight(
        &mut self,
        context: AccessContext,
        profile_id: ProfileId,
        policy: AccessPolicy,
    ) -> Result<(), TransitionError> {
        self.bind_authorization(context, profile_id, policy)
            .map(|_| ())
    }

    pub fn accept_box(
        &mut self,
        box_type: String,
        contents: String,
        hidden_type: String,
        hidden_contents: String,
    ) -> Result<(), String> {
        self.turn
            .accept(box_type, contents, hidden_type, hidden_contents)
    }

    pub fn accept_external_box(
        &mut self,
        box_type: String,
        contents: String,
        hidden_type: String,
        hidden_contents: String,
    ) -> Result<(), TransitionError> {
        if box_type == USER_MESSAGE_TYPE {
            return Err(TransitionError::Unauthorized);
        }
        self.accept_box(box_type, contents, hidden_type, hidden_contents)
            .map_err(TransitionError::Internal)
    }

    pub fn accept_user(
        &mut self,
        context: AccessContext,
        profile_id: ProfileId,
        policy: AccessPolicy,
        contents: String,
    ) -> Result<(), TransitionError> {
        let installed = self.bind_authorization(context, profile_id, policy)?;
        match self.turn.accept(
            USER_MESSAGE_TYPE.into(),
            contents,
            String::new(),
            String::new(),
        ) {
            Ok(()) => Ok(()),
            Err(error) => {
                if installed {
                    self.clear_authorization();
                }
                Err(TransitionError::Internal(error))
            }
        }
    }

    pub fn accept_return(
        &mut self,
        id: ToolCallId,
        result: Result<String, String>,
    ) -> Result<(), String> {
        self.turn.accept_tool_return(id, result)
    }

    pub fn prepare_stage(
        &mut self,
        job: u64,
        text: String,
        boxes: Vec<BoxValue>,
    ) -> Result<Vec<PreparedCall>, String> {
        self.turn.prepare_stage(job, text, boxes)
    }

    pub fn launch_action(&mut self, name: &str, arguments: &str) -> Result<String, String> {
        if !kcode_k1_ktool_docs::is_known_ktool(name) {
            return Err("unknown Ktool".into());
        }
        match name {
            "KtoolDocs" => kcode_k1_ktool_docs::ktool_docs(arguments),
            "SendMessage" => launch_send_message(&mut self.turn, arguments),
            _ => self.ktools.launch(name, arguments),
        }
    }

    pub fn accept_tool_message(&mut self, id: ToolCallId, contents: String) -> Result<(), String> {
        self.turn.accept_tool_message(id, contents)
    }

    pub fn accept_tool_return(
        &mut self,
        id: ToolCallId,
        result: Result<String, String>,
    ) -> Result<(), String> {
        self.turn.accept_tool_return(id, result)
    }

    pub fn accept_tool_return_v2(
        &mut self,
        id: ToolCallId,
        result: Result<String, String>,
        metadata_type: String,
        metadata_contents: String,
    ) -> Result<(), String> {
        self.turn
            .accept_tool_return_v2(id, result, metadata_type, metadata_contents)
    }

    pub fn prepare_mailbox_flush(
        &mut self,
        job: u64,
    ) -> Result<Option<PreparedMailboxFlush>, String> {
        self.turn.prepare_mailbox_flush(job)
    }

    pub fn prepared_input(&self, prepared: &PreparedMailboxFlush) -> Result<String, String> {
        self.turn.validate_mailbox_flush(prepared)?;
        render_input(prepared.values())
    }

    pub fn commit_mailbox_flush(&mut self, prepared: PreparedMailboxFlush) -> Result<(), String> {
        self.turn.commit_mailbox_flush(prepared)
    }

    pub fn begin_input(&mut self) -> Result<Option<(u64, String)>, String> {
        let Some(start) = self.turn.begin()? else {
            return Ok(None);
        };
        Ok(Some((start.job, render_input(&start.values)?)))
    }

    pub fn complete(&mut self, job: u64, output: ShimOutput<BoxValue>) -> Result<bool, String> {
        self.turn.complete(job, output)
    }

    pub fn complete_with_terminal_response(
        &mut self,
        job: u64,
        output: ShimOutput<BoxValue>,
    ) -> Result<(bool, u64), String> {
        let terminal_index = self.turn.boxes().len();
        let resume = self.complete(job, output)?;
        let terminal =
            self.turn.boxes().get(terminal_index).ok_or_else(|| {
                "completion did not append a terminal Agent Response box".to_owned()
            })?;
        if terminal.box_type() != AGENT_RESPONSE_TYPE {
            return Err("completion terminal box was not an Agent Response".to_owned());
        }
        Ok((resume, terminal.id().get()))
    }

    pub fn complete_recoverable_failure_with_terminal_response(
        &mut self,
        job: u64,
        message: String,
    ) -> Result<(bool, u64), String> {
        let terminal_index = self.turn.boxes().len();
        let resume = self.turn.complete_recoverable_failure(job, message)?;
        let terminal = self.turn.boxes().get(terminal_index).ok_or_else(|| {
            "recoverable failure did not append a terminal Agent Response box".to_owned()
        })?;
        if terminal.box_type() != AGENT_RESPONSE_TYPE {
            return Err("recoverable failure terminal box was not an Agent Response".to_owned());
        }
        Ok((resume, terminal.id().get()))
    }

    pub fn reset_provider_context(&mut self) -> Result<(), String> {
        self.turn.reset_provider_context()
    }

    pub fn record_diagnostic(&mut self, diagnostic: ChatDiagnostic) -> Result<(), String> {
        self.turn.record_diagnostic(diagnostic)
    }

    pub fn halt_critical(&mut self, message: String) -> Result<(), String> {
        let result = self.turn.halt_critical(message);
        let _ = self.ktools.clear_authorization();
        self.authorized = false;
        result
    }

    pub fn record_model_usage(
        &mut self,
        connected_box_id: u64,
        usage: ModelUsage,
    ) -> Result<(), String> {
        self.turn.record_model_usage(connected_box_id, usage)
    }

    pub fn fail(&mut self, job: u64, error: String, restartable: bool) {
        self.turn.fail(job, error, restartable);
        self.clear_authorization();
    }

    pub fn restart(
        &mut self,
        context: AccessContext,
        profile_id: ProfileId,
        policy: AccessPolicy,
    ) -> Result<(), TransitionError> {
        let installed = self.bind_authorization(context, profile_id, policy)?;
        if let Err(error) = self.turn.restart().map_err(|error| match error {
            RestartError::NotStalled => TransitionError::NotStalled,
            RestartError::ProviderActionAccepted => TransitionError::NotRestartable,
        }) {
            if installed {
                self.clear_authorization();
            }
            return Err(error);
        }
        Ok(())
    }

    pub fn clear_authorization(&mut self) {
        if self.has_unresolved_preflight() {
            return;
        }
        let _ = self.ktools.clear_authorization();
        self.authorized = false;
    }

    fn has_unresolved_preflight(&self) -> bool {
        self.turn.preflight_calls().iter().any(|call| {
            !self.turn.boxes().iter().any(|value| {
                value
                    .tool_result_metadata()
                    .ok()
                    .flatten()
                    .is_some_and(|result| result.tool_call_id == call.tool_call_id)
            })
        })
    }

    fn recover_with_ktools(session: Session, ktools: ChatThreadKtools) -> Result<Self, String> {
        Ok(Self {
            turn: DurableTurn::recover(session)?,
            ktools: ChatThreadKtoolExecutor::new(ktools),
            authorized: false,
        })
    }

    fn bind_authorization(
        &mut self,
        context: AccessContext,
        profile_id: ProfileId,
        policy: AccessPolicy,
    ) -> Result<bool, TransitionError> {
        let installed = !self.authorized;
        if self
            .ktools
            .bind_authorization(context, profile_id, policy)
            .is_err()
        {
            if installed {
                let _ = self.ktools.clear_authorization();
            }
            return Err(TransitionError::Unauthorized);
        }
        self.authorized = true;
        Ok(installed)
    }
}

#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
struct SendMessageArguments {
    message: String,
}

fn launch_send_message(turn: &mut DurableTurn, arguments: &str) -> Result<String, String> {
    let parsed: SendMessageArguments =
        serde_json::from_str(arguments).map_err(|_| invalid_send_message())?;
    if parsed.message.is_empty() {
        return Err(invalid_send_message());
    }
    turn.accept(
        AGENT_MESSAGE_TYPE.into(),
        parsed.message,
        String::new(),
        String::new(),
    )?;
    Ok("success".into())
}

fn invalid_send_message() -> String {
    "invalid SendMessage arguments".into()
}

fn render_input(values: &[BoxValue]) -> Result<String, String> {
    let mut output = String::new();
    for value in values {
        let BoxValue::History(section) = value else {
            return Err("Codex provider input contains a non-history value".into());
        };
        if section.is_empty() {
            continue;
        }
        if !output.is_empty() && !output.ends_with('\n') {
            output.push('\n');
        }
        output.push_str(section);
    }
    Ok(output)
}

#[cfg(test)]
mod tests;