kcode-k1-daemon-lib 0.9.3

Library-only private K1 loopback daemon composition root
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
#![doc = include_str!("../Documentation.md")]

use kcode_gemini_3_1_pro::Gemini31Pro;
use kcode_k1_access::K1Access;
use kcode_k1_access_full_audio::K1AccessFullAudio;
use kcode_k1_access_persons::K1AccessPersons;
use kcode_k1_access_profiles::K1AccessProfiles;
use kcode_k1_accounting::Accounting;
use kcode_k1_accounts::K1Accounts;
use kcode_k1_audio_classification::AudioClassification;
use kcode_k1_chat_service::K1ChatService;
use kcode_k1_codex_adapter::{Adapter as CodexAdapter, Error as CodexAdapterError};
use kcode_k1_daemon_files::DaemonFiles;
use kcode_k1_daemon_http_boundary::{
    Boundary, PUBLIC_ORIGIN, api_not_found, warn_if_slow, write_readiness,
};
use kcode_k1_daemon_provider_config::{
    CODEX_EXECUTABLE_ENV, audio_models, chat_model, codex_configs, codex_executable, people_models,
    resolve_ffmpeg,
};
use kcode_k1_daemon_vault_unlock::VaultUnlock;
use kcode_k1_full_audio::K1FullAudio;
use kcode_k1_groups::K1Groups;
use kcode_k1_http::{Config as HttpConfig, K1Http};
use kcode_k1_http_accounts::K1HttpAccounts;
use kcode_k1_http_people::K1HttpPeople;
use kcode_k1_http_replay::{ReplayConfig, ReplayWindow};
use kcode_k1_invites::K1Invites;
use kcode_k1_objects::K1Objects;
use kcode_k1_peering::K1Peering;
use kcode_k1_persons::K1Persons;
use kcode_k1_txn_ordering::K1TxnOrdering;
use kcode_k1_users::K1Users;
use kcode_k1_vault::{ExposeSecret, K1Vault};
use kcode_speaker_v3_analysis::Analyzer;
use std::fmt;
use std::path::{Path, PathBuf};
use std::process::ExitCode;
use std::sync::Arc;
use std::time::Instant;

const INVITE_LINK_URL: &str = "http://localhost:4321/lib/kcode-k1-ui/*/account.html";
const GEMINI_API_KEY: &str = "gemini-api-key";

struct Prepared {
    boundary: Boundary,
    unused_invites: usize,
    vault: Arc<K1Vault>,
}

enum StartupError {
    Stage(&'static str),
    CodexAdapter(CodexAdapterError),
    Chat(String),
}

impl fmt::Display for StartupError {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Stage(stage) => {
                write!(formatter, "kcode-k1-daemon: startup failed at {stage}")
            }
            Self::CodexAdapter(error) => {
                write!(
                    formatter,
                    "kcode-k1-daemon: startup failed at Codex adapter: {error}"
                )
            }
            Self::Chat(child) => {
                write!(
                    formatter,
                    "kcode-k1-daemon: startup failed at chat service: {child}"
                )
            }
        }
    }
}

fn stage<E>(name: &'static str) -> impl FnOnce(E) -> StartupError {
    move |_| StartupError::Stage(name)
}

pub fn run(k1_root: PathBuf) -> ExitCode {
    let runtime = match tokio::runtime::Builder::new_multi_thread()
        .enable_all()
        .build()
    {
        Ok(runtime) => runtime,
        Err(_) => {
            eprintln!("kcode-k1-daemon: startup failed");
            return ExitCode::from(1);
        }
    };
    let unlock = match VaultUnlock::prompt() {
        Ok(unlock) => unlock,
        Err(_) => {
            eprintln!("kcode-k1-daemon: startup failed");
            return ExitCode::from(1);
        }
    };
    runtime.block_on(run_async(k1_root, unlock))
}

async fn run_async(k1_root: PathBuf, unlock: VaultUnlock) -> ExitCode {
    let started = Instant::now();
    let prepared = match startup(k1_root, unlock).await {
        Ok(prepared) => prepared,
        Err(error) => {
            warn_if_slow(started.elapsed(), "error");
            eprintln!("{error}");
            return ExitCode::from(1);
        }
    };
    let elapsed = started.elapsed();
    if write_readiness(prepared.unused_invites).is_err() {
        warn_if_slow(elapsed, "error");
        eprintln!("kcode-k1-daemon: startup failed");
        return ExitCode::from(1);
    }
    warn_if_slow(elapsed, "ready");
    let Prepared {
        boundary, vault, ..
    } = prepared;
    let result = boundary.serve().await;
    drop(vault);
    match result {
        Ok(()) => ExitCode::SUCCESS,
        Err(()) => {
            eprintln!("kcode-k1-daemon: listener failed");
            ExitCode::from(1)
        }
    }
}

async fn startup(k1_root: PathBuf, unlock: VaultUnlock) -> Result<Prepared, StartupError> {
    let state_root = state_root(&k1_root);
    let files = DaemonFiles::open(&state_root).map_err(stage("daemon files"))?;
    let ordering = Arc::new(
        K1TxnOrdering::open(&state_root.join("ordering")).map_err(stage("transaction ordering"))?,
    );
    let peering = Arc::new(
        K1Peering::open(&state_root.join("peering"), Arc::clone(&ordering))
            .map_err(stage("peering"))?,
    );
    let vault = unlock
        .open(&state_root, Arc::clone(&ordering), Arc::clone(&peering))
        .map_err(stage("Vault"))?;
    let persons = Arc::new(
        K1Persons::open(
            &state_root.join("persons"),
            Arc::clone(&ordering),
            Arc::clone(&peering),
        )
        .map_err(stage("Persons"))?,
    );
    let invites = Arc::new(
        K1Invites::open(
            &state_root.join("invites"),
            Arc::clone(&ordering),
            Arc::clone(&peering),
        )
        .map_err(stage("Invites"))?,
    );
    let accounts = Arc::new(K1Accounts::open(Arc::clone(&invites)).map_err(stage("Accounts"))?);
    let users = Arc::new(K1Users::new(Arc::clone(&accounts), Arc::clone(&persons)));
    let groups = Arc::new(
        K1Groups::open(
            &state_root.join("groups"),
            Arc::clone(&ordering),
            Arc::clone(&peering),
        )
        .map_err(stage("Groups"))?,
    );
    let profiles = Arc::new(
        K1AccessProfiles::open(
            &state_root.join("access-profiles"),
            Arc::clone(&ordering),
            Arc::clone(&peering),
        )
        .map_err(stage("Access Profiles"))?,
    );
    let gemini_key = vault
        .secret(GEMINI_API_KEY)
        .map_err(stage("Gemini API key"))?
        .ok_or(StartupError::Stage("Gemini API key"))?;
    let gemini = Gemini31Pro::new(
        gemini_key.expose_secret().to_owned(),
        Accounting::new(),
        std::time::Duration::from_secs(30 * 60),
    )
    .map_err(stage("Gemini client"))?;
    let executable = codex_executable(std::env::var_os(CODEX_EXECUTABLE_ENV));
    let working_directory = std::env::current_dir()
        .map_err(stage("working directory"))?
        .to_string_lossy()
        .into_owned();
    let (audio_config, chat_config) = codex_configs(executable, working_directory);
    let audio_codex_adapter = CodexAdapter::open(audio_config)
        .await
        .map_err(StartupError::CodexAdapter)?;
    let chat_codex_adapter = audio_codex_adapter
        .with_config(chat_config)
        .map_err(StartupError::CodexAdapter)?;
    let analyzer = Analyzer::from_codex_adapter(gemini, audio_codex_adapter);
    let objects = Arc::new(
        K1Objects::open(Arc::clone(&ordering), Arc::clone(&peering)).map_err(stage("Objects"))?,
    );
    let classification = Arc::new(
        AudioClassification::open(
            &state_root.join("audio-classification"),
            Arc::clone(&ordering),
            Arc::clone(&peering),
            Arc::clone(&objects),
            analyzer,
        )
        .map_err(stage("Audio Classification"))?,
    );
    let ffmpeg = resolve_ffmpeg().map_err(stage("FFmpeg"))?;
    let full_audio = Arc::new(
        K1FullAudio::open(ffmpeg, Arc::clone(&objects), Arc::clone(&classification))
            .map_err(stage("Full Audio"))?,
    );
    let access = Arc::new(
        K1Access::open(
            &state_root.join("access"),
            Arc::clone(&ordering),
            Arc::clone(&peering),
            Arc::clone(&groups),
        )
        .map_err(stage("Access"))?,
    );
    let chat = K1ChatService::open(
        &state_root.join("chat"),
        Arc::clone(&ordering),
        Arc::clone(&peering),
        Arc::clone(&access),
        Arc::clone(&profiles),
        chat_codex_adapter,
    )
    .map_err(StartupError::Chat)?;
    let access_persons = Arc::new(
        K1AccessPersons::open(
            Arc::clone(&access),
            Arc::clone(&profiles),
            Arc::clone(&persons),
        )
        .map_err(stage("Access Persons"))?,
    );
    let models = audio_models();
    let audio = Arc::new(
        K1AccessFullAudio::open_for_models(
            Arc::clone(&access),
            Arc::clone(&profiles),
            full_audio,
            classification,
            Arc::clone(&groups),
            models.to_vec(),
        )
        .map_err(stage("Access Full Audio"))?,
    );
    let replay = ReplayWindow::open(ReplayConfig {
        epoch_file: files.replay_epoch_path().to_owned(),
        max_nonces_per_epoch: usize::MAX,
    })
    .await
    .map_err(stage("HTTP replay"))?;
    let unused_invites = kcode_k1_daemon_invite_stock::reconcile(
        &invites,
        files.invite_links_path(),
        INVITE_LINK_URL,
    )
    .map_err(stage("invite stock"))?;
    if unused_invites < 100 {
        return Err(StartupError::Stage("minimum invite stock"));
    }
    let adapter = K1HttpAccounts::new(
        Arc::clone(&accounts),
        Arc::clone(&invites),
        Arc::clone(&users),
    );
    let people_models: Arc<[kcode_k1_http_people::LocalModel]> = Arc::from(people_models());
    let people = K1HttpPeople::new_with_models(accounts, users, groups, profiles, people_models)
        .map_err(stage("People HTTP"))?;
    let http = K1Http::new(
        HttpConfig {
            server_id: files.server_id().to_owned(),
            public_origin: PUBLIC_ORIGIN.to_owned(),
            max_body_bytes: usize::MAX,
        },
        replay,
        adapter.identity_provider(),
    )
    .map_err(stage("K1 HTTP"))?;
    let person_routes =
        kcode_k1_http_persons::authenticated_routes(access_persons, access, models[0])
            .map_err(stage("Persons HTTP"))?;
    let authenticated = adapter
        .authenticated_routes()
        .merge(people.authenticated_routes())
        .merge(kcode_k1_http_audio::authenticated_routes(Arc::clone(
            &audio,
        )))
        .merge(kcode_k1_http_audio_artifacts::authenticated_routes(audio))
        .merge(person_routes)
        .merge(kcode_k1_http_chat::router(chat, chat_model()))
        .fallback(api_not_found);
    let api = http.router(
        adapter.registration_endpoint(),
        kcode_k1_terms::endpoint(),
        authenticated,
    );
    let boundary = Boundary::bind(api, files.server_id().to_owned())
        .await
        .map_err(stage("listener bind"))?;
    Ok(Prepared {
        boundary,
        unused_invites,
        vault,
    })
}

fn state_root(k1_root: &Path) -> PathBuf {
    k1_root.join("state")
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn public_operation_and_state_root_are_fixed() {
        let _: fn(PathBuf) -> ExitCode = run;
        assert_eq!(
            state_root(Path::new("/trusted/k1")),
            PathBuf::from("/trusted/k1/state")
        );
    }

    #[test]
    fn fixed_provider_key_and_origins_remain_exact_and_distinct() {
        assert_eq!(GEMINI_API_KEY, "gemini-api-key");
        assert_eq!(
            INVITE_LINK_URL,
            "http://localhost:4321/lib/kcode-k1-ui/*/account.html"
        );
        assert_eq!(PUBLIC_ORIGIN, "http://localhost:4450");
        assert_ne!(INVITE_LINK_URL, PUBLIC_ORIGIN);
    }

    #[test]
    fn startup_error_rendering_is_stage_specific_and_secret_safe() {
        assert_eq!(
            StartupError::Stage("Vault").to_string(),
            "kcode-k1-daemon: startup failed at Vault"
        );
        let error = CodexAdapterError {
            kind: kcode_k1_codex_adapter::ErrorKind::Unavailable,
            message: "safe adapter display".to_owned(),
            diagnostics: b"RAW_SECRET_DIAGNOSTIC".to_vec(),
        };
        let rendered = StartupError::CodexAdapter(error).to_string();
        assert_eq!(
            rendered,
            "kcode-k1-daemon: startup failed at Codex adapter: safe adapter display"
        );
        assert!(!rendered.contains("RAW_SECRET_DIAGNOSTIC"));
        let rendered =
            StartupError::Chat("open chat service: safe child failure".to_owned()).to_string();
        assert_eq!(
            rendered,
            "kcode-k1-daemon: startup failed at chat service: open chat service: safe child failure"
        );
    }

    #[test]
    fn selected_composition_dependencies_and_constructor_are_compatible() {
        const MANIFEST: &str = include_str!("../Cargo.toml");
        for selected in [
            "kcode-k1-access-full-audio = \"0.7.4\"",
            "kcode-k1-audio-classification = \"0.5.6\"",
            "kcode-k1-chat-service = \"0.2.0\"",
            "kcode-k1-codex-adapter = \"0.5.0\"",
            "kcode-k1-daemon-http-boundary = \"0.1.0\"",
            "kcode-k1-daemon-provider-config = \"0.1.2\"",
            "kcode-k1-daemon-vault-unlock = \"0.1.0\"",
            "kcode-k1-full-audio = \"0.3.6\"",
            "kcode-k1-groups = \"0.3.1\"",
            "kcode-k1-http-audio = \"0.1.4\"",
            "kcode-k1-http-audio-artifacts = \"0.1.0\"",
            "kcode-k1-http-chat = \"0.1.0\"",
            "kcode-speaker-v3-analysis = { version = \"0.3.4\", default-features = false, features = [\"adapter-providers\"] }",
        ] {
            assert!(MANIFEST.contains(selected));
        }
        assert!(!MANIFEST.lines().any(|line| {
            line.trim_start()
                .starts_with("kcode-speaker-v3-terra-analysis ")
        }));
        fn require_constructor(_: fn(Gemini31Pro, CodexAdapter) -> Analyzer) {}
        require_constructor(Analyzer::from_codex_adapter);
        let models = people_models();
        assert_eq!(
            models
                .iter()
                .map(kcode_k1_http_people::LocalModel::name)
                .collect::<Vec<_>>(),
            [
                "All models — special; includes current and future models",
                "GPT-5.6 Terra",
                "GPT-5.6 Sol",
                "GPT-5.6 Luna",
                "Gemini 3.1 Pro",
            ]
        );
    }
}