Skip to main content

codewhale_command_contract/
handler.rs

1//! Generic handler transport for staged command migration.
2//!
3//! The output type is generic so FEAT-014 does not move or duplicate the
4//! TUI-owned `CommandResult`. During in-place adoption, the TUI instantiates
5//! `CommandHandler<crate::commands::CommandResult>`.
6
7use crate::facets::{
8    CommandConfigStatusContext, CommandCostContext, CommandDebugChangeContext,
9    CommandDebugDiagnosticsContext, CommandDebugDiffContext, CommandDebugHistoryContext,
10    CommandDebugReceiptsContext, CommandDebugUndoContext, CommandMediaContext,
11    CommandMemoryContext, CommandModePolicyContext, CommandModelContext, CommandPermissionsContext,
12    CommandPluginContext, CommandPresentationContext, CommandProjectContext, CommandSessionContext,
13    CommandSessionControlContext, CommandSessionExportContext, CommandSessionLifecycleContext,
14    CommandSessionStructcopyContext, CommandSkillGroupContext, CommandSkillsContext,
15    CommandSystemPromptContext, CommandWorkspaceContext,
16};
17
18/// Exact host capabilities exposed to one contextual command handler.
19///
20/// The set lives in the external contract crate so command registrations can
21/// declare least authority without naming the TUI host. The dispatcher uses
22/// the declaration to populate only those slots in [`CommandContexts`].
23#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
24pub struct CommandCapabilities(u32);
25
26impl CommandCapabilities {
27    pub const NONE: Self = Self(0);
28    pub const SESSION: Self = Self(1 << 0);
29    pub const MODEL: Self = Self(1 << 1);
30    pub const COST: Self = Self(1 << 2);
31    pub const MODE_POLICY: Self = Self(1 << 3);
32    pub const SYSTEM_PROMPT: Self = Self(1 << 4);
33    pub const SKILLS: Self = Self(1 << 5);
34    pub const WORKSPACE: Self = Self(1 << 6);
35    pub const PRESENTATION: Self = Self(1 << 7);
36    pub const MEDIA: Self = Self(1 << 8);
37    /// Memory-group host data (FEAT-019 D1).
38    pub const MEMORY: Self = Self(1 << 9);
39    /// Project-group host data (FEAT-021 D1).
40    pub const PROJECT: Self = Self(1 << 10);
41    /// Skills-group host data (FEAT-022 D1).
42    pub const SKILL_GROUP: Self = Self(1 << 11);
43    /// Plugin-group host data (FEAT-020 D1), appended after current main capabilities.
44    pub const PLUGIN: Self = Self(1 << 12);
45    /// Session-lifecycle host data (FEAT-023 D3), the next non-conflicting bit
46    /// after `PLUGIN`. Required only by the seven host-dependent lifecycle
47    /// commands; `/compact` and `/purge` remain pure. Never widened by the
48    /// basic session capability.
49    pub const SESSION_LIFECYCLE: Self = Self(1 << 13);
50    /// Session-control host data (FEAT-024 D3), the next non-conflicting bit
51    /// after `SESSION_LIFECYCLE`. Required only by the six host-dependent
52    /// control commands (`/relay`, `/rename`, `/resume`, `/rc`, `/remote-env`,
53    /// `/title`); `/remote-env` also declares `PRESENTATION`. Bit 14 fit the
54    /// original `u16` backing without speculative widening in FEAT-023.
55    pub const SESSION_CONTROL: Self = Self(1 << 14);
56    /// Session-export host data (FEAT-025 D1), the next non-conflicting bit
57    /// after `SESSION_CONTROL`. Required only by the host-dependent `/export`
58    /// command (and its `/daochu` alias) and by `/share`, which publishes the
59    /// same redacted projection; every concrete App, snapshot, clipboard,
60    /// filesystem, history, and turn-handoff access stays behind the TUI export
61    /// adapter.
62    ///
63    /// This filled the original 16-bit space. FEAT-029 widened the backing
64    /// storage before allocating the next independent diagnostics authority;
65    /// the published identity of this bit remains unchanged.
66    pub const SESSION_EXPORT: Self = Self(1 << 15);
67    /// Debug diagnostics host data (FEAT-029 D3/D4). This is the first bit in
68    /// the widened backing storage; other debug commands retain their own independent
69    /// authority and do not borrow this facet.
70    pub const DEBUG_DIAGNOSTICS: Self = Self(1 << 16);
71
72    /// Debug receipts authority; independent from diagnostics and other debug operations.
73    pub const DEBUG_RECEIPTS: Self = Self(1 << 17);
74    /// Debug change authority; independent from diagnostics and other debug operations.
75    pub const DEBUG_CHANGE: Self = Self(1 << 18);
76    /// Debug history authority; independent from diagnostics and other debug operations.
77    pub const DEBUG_HISTORY: Self = Self(1 << 19);
78    /// Debug diff authority; independent from diagnostics and other debug operations.
79    pub const DEBUG_DIFF: Self = Self(1 << 20);
80    /// Debug undo authority; independent from diagnostics and other debug operations.
81    pub const DEBUG_UNDO: Self = Self(1 << 21);
82
83    /// One human-selected structural copy; independent from export/recovery.
84    pub const SESSION_STRUCTCOPY: Self = Self(1 << 22);
85    /// Permission observations and token-checked removal, independent of status.
86    pub const PERMISSIONS: Self = Self(1 << 23);
87    /// Read-only status observations, with no permission mutation authority.
88    pub const CONFIG_STATUS: Self = Self(1 << 24);
89    /// Config-group host data (FEAT-028 D3), the next non-conflicting bit after
90    /// `CONFIG_STATUS`. Read/write settings, keys, presets, the editability
91    /// audit and login/logout observations for the config command group. It does
92    /// not grant pet, trust, mode, or workspace authority.
93    pub const CONFIG: Self = Self(1 << 25);
94    /// Pet presence/mode/sound/habitat host data (FEAT-028 D3), independent of
95    /// general configuration and system settings.
96    pub const PET: Self = Self(1 << 26);
97    /// Workspace trust and trusted-path allow-list host data (FEAT-028 D3),
98    /// independent of the general config facet and of `WORKSPACE` operations.
99    pub const TRUST: Self = Self(1 << 27);
100    /// Account/provider session state for `/login` and `/logout` (FEAT-028 D3).
101    pub const LOGIN: Self = Self(1 << 28);
102    /// Workbar placement/panel state for `/workbar` (FEAT-028 D3).
103    pub const WORK_SURFACE: Self = Self(1 << 29);
104    pub const NAVIGATION: Self = Self(1 << 30);
105    pub const VOICE_INPUT: Self = Self(1 << 31);
106
107    /// Raw bit pattern, for tests that pin the capability-space capacity.
108    ///
109    /// Kept `#[cfg(test)]` so the `u32` backing stays an implementation detail
110    /// and nothing can widen it accidentally through a public accessor.
111    #[cfg(test)]
112    pub(crate) const fn bits_for_test(self) -> u32 {
113        self.0
114    }
115
116    pub const fn union(self, other: Self) -> Self {
117        Self(self.0 | other.0)
118    }
119
120    pub const fn contains(self, capability: Self) -> bool {
121        !capability.is_empty() && self.0 & capability.0 == capability.0
122    }
123
124    pub const fn is_empty(self) -> bool {
125        self.0 == 0
126    }
127}
128
129impl std::ops::BitOr for CommandCapabilities {
130    type Output = Self;
131
132    fn bitor(self, rhs: Self) -> Self::Output {
133        self.union(rhs)
134    }
135}
136
137/// A command handler that is either argument-only or capability-scoped.
138#[derive(Clone, Copy)]
139pub enum CommandHandler<R> {
140    Pure(fn(Option<&str>) -> R),
141    Contextual {
142        capabilities: CommandCapabilities,
143        handler: fn(CommandContexts<'_>, Option<&str>) -> R,
144    },
145}
146
147/// Transport envelope with one independently optional facet slot.
148pub struct CommandContexts<'a> {
149    session: Option<&'a mut dyn CommandSessionContext>,
150    model: Option<&'a mut dyn CommandModelContext>,
151    cost: Option<&'a mut dyn CommandCostContext>,
152    mode_policy: Option<&'a mut dyn CommandModePolicyContext>,
153    system_prompt: Option<&'a mut dyn CommandSystemPromptContext>,
154    skills: Option<&'a mut dyn CommandSkillsContext>,
155    workspace: Option<&'a mut dyn CommandWorkspaceContext>,
156    presentation: Option<&'a mut dyn CommandPresentationContext>,
157    media: Option<&'a mut dyn CommandMediaContext>,
158    memory: Option<&'a mut dyn CommandMemoryContext>,
159    project: Option<&'a mut dyn CommandProjectContext>,
160    skill_group: Option<&'a mut dyn CommandSkillGroupContext>,
161    plugin: Option<&'a mut dyn CommandPluginContext>,
162    lifecycle: Option<&'a mut dyn CommandSessionLifecycleContext>,
163    control: Option<&'a mut dyn CommandSessionControlContext>,
164    export: Option<&'a mut dyn CommandSessionExportContext>,
165    structcopy: Option<&'a mut dyn CommandSessionStructcopyContext>,
166    debug_receipts: Option<&'a mut dyn CommandDebugReceiptsContext>,
167    debug_change: Option<&'a mut dyn CommandDebugChangeContext>,
168    debug_history: Option<&'a mut dyn CommandDebugHistoryContext>,
169    debug_diff: Option<&'a mut dyn CommandDebugDiffContext>,
170    debug_undo: Option<&'a mut dyn CommandDebugUndoContext>,
171    debug_diagnostics: Option<&'a mut dyn CommandDebugDiagnosticsContext>,
172    permissions: Option<&'a mut dyn CommandPermissionsContext>,
173    config_status: Option<&'a mut dyn CommandConfigStatusContext>,
174    config: Option<&'a mut dyn crate::config::CommandConfigContext>,
175    pet: Option<&'a mut dyn crate::config::CommandPetContext>,
176    trust: Option<&'a mut dyn crate::config::CommandTrustContext>,
177    login: Option<&'a mut dyn crate::config::CommandLoginContext>,
178    work_surface: Option<&'a mut dyn crate::config::CommandWorkSurfaceContext>,
179    navigation: Option<&'a mut dyn crate::navigation::CommandNavigationContext>,
180    voice_input: Option<&'a mut dyn crate::navigation::CommandVoiceInputContext>,
181}
182
183/// Consumed envelope used when one handler needs several independent facets.
184pub struct ContextParts<'a> {
185    pub session: Option<&'a mut dyn CommandSessionContext>,
186    pub model: Option<&'a mut dyn CommandModelContext>,
187    pub cost: Option<&'a mut dyn CommandCostContext>,
188    pub mode_policy: Option<&'a mut dyn CommandModePolicyContext>,
189    pub system_prompt: Option<&'a mut dyn CommandSystemPromptContext>,
190    pub skills: Option<&'a mut dyn CommandSkillsContext>,
191    pub workspace: Option<&'a mut dyn CommandWorkspaceContext>,
192    pub presentation: Option<&'a mut dyn CommandPresentationContext>,
193    pub media: Option<&'a mut dyn CommandMediaContext>,
194    pub memory: Option<&'a mut dyn CommandMemoryContext>,
195    pub project: Option<&'a mut dyn CommandProjectContext>,
196    pub skill_group: Option<&'a mut dyn CommandSkillGroupContext>,
197    pub plugin: Option<&'a mut dyn CommandPluginContext>,
198    pub lifecycle: Option<&'a mut dyn CommandSessionLifecycleContext>,
199    pub control: Option<&'a mut dyn CommandSessionControlContext>,
200    pub export: Option<&'a mut dyn CommandSessionExportContext>,
201    pub structcopy: Option<&'a mut dyn CommandSessionStructcopyContext>,
202    pub debug_receipts: Option<&'a mut dyn CommandDebugReceiptsContext>,
203    pub debug_change: Option<&'a mut dyn CommandDebugChangeContext>,
204    pub debug_history: Option<&'a mut dyn CommandDebugHistoryContext>,
205    pub debug_diff: Option<&'a mut dyn CommandDebugDiffContext>,
206    pub debug_undo: Option<&'a mut dyn CommandDebugUndoContext>,
207    pub debug_diagnostics: Option<&'a mut dyn CommandDebugDiagnosticsContext>,
208    pub permissions: Option<&'a mut dyn CommandPermissionsContext>,
209    pub config_status: Option<&'a mut dyn CommandConfigStatusContext>,
210    pub config: Option<&'a mut dyn crate::config::CommandConfigContext>,
211    pub pet: Option<&'a mut dyn crate::config::CommandPetContext>,
212    pub trust: Option<&'a mut dyn crate::config::CommandTrustContext>,
213    pub login: Option<&'a mut dyn crate::config::CommandLoginContext>,
214    pub work_surface: Option<&'a mut dyn crate::config::CommandWorkSurfaceContext>,
215    pub navigation: Option<&'a mut dyn crate::navigation::CommandNavigationContext>,
216    pub voice_input: Option<&'a mut dyn crate::navigation::CommandVoiceInputContext>,
217}
218
219impl<'a> CommandContexts<'a> {
220    pub fn empty() -> Self {
221        Self {
222            session: None,
223            model: None,
224            cost: None,
225            mode_policy: None,
226            system_prompt: None,
227            skills: None,
228            workspace: None,
229            presentation: None,
230            media: None,
231            memory: None,
232            project: None,
233            skill_group: None,
234            plugin: None,
235            lifecycle: None,
236            control: None,
237            export: None,
238            structcopy: None,
239            debug_receipts: None,
240            debug_change: None,
241            debug_history: None,
242            debug_diff: None,
243            debug_undo: None,
244            debug_diagnostics: None,
245            permissions: None,
246            config_status: None,
247            config: None,
248            pet: None,
249            trust: None,
250            login: None,
251            work_surface: None,
252            navigation: None,
253            voice_input: None,
254        }
255    }
256
257    pub fn into_parts(self) -> ContextParts<'a> {
258        ContextParts {
259            session: self.session,
260            model: self.model,
261            cost: self.cost,
262            mode_policy: self.mode_policy,
263            system_prompt: self.system_prompt,
264            skills: self.skills,
265            workspace: self.workspace,
266            presentation: self.presentation,
267            media: self.media,
268            memory: self.memory,
269            project: self.project,
270            skill_group: self.skill_group,
271            plugin: self.plugin,
272            lifecycle: self.lifecycle,
273            control: self.control,
274            export: self.export,
275            structcopy: self.structcopy,
276            debug_receipts: self.debug_receipts,
277            debug_change: self.debug_change,
278            debug_history: self.debug_history,
279            debug_diff: self.debug_diff,
280            debug_undo: self.debug_undo,
281            debug_diagnostics: self.debug_diagnostics,
282            permissions: self.permissions,
283            config_status: self.config_status,
284            config: self.config,
285            pet: self.pet,
286            trust: self.trust,
287            login: self.login,
288            work_surface: self.work_surface,
289            navigation: self.navigation,
290            voice_input: self.voice_input,
291        }
292    }
293
294    pub fn with_config(mut self, value: &'a mut dyn crate::config::CommandConfigContext) -> Self {
295        assert!(
296            self.config.replace(value).is_none(),
297            "config facet already set"
298        );
299        self
300    }
301
302    pub fn with_pet(mut self, value: &'a mut dyn crate::config::CommandPetContext) -> Self {
303        assert!(self.pet.replace(value).is_none(), "pet facet already set");
304        self
305    }
306
307    pub fn with_trust(mut self, value: &'a mut dyn crate::config::CommandTrustContext) -> Self {
308        assert!(
309            self.trust.replace(value).is_none(),
310            "trust facet already set"
311        );
312        self
313    }
314
315    pub fn with_login(mut self, value: &'a mut dyn crate::config::CommandLoginContext) -> Self {
316        assert!(
317            self.login.replace(value).is_none(),
318            "login facet already set"
319        );
320        self
321    }
322
323    pub fn with_work_surface(
324        mut self,
325        value: &'a mut dyn crate::config::CommandWorkSurfaceContext,
326    ) -> Self {
327        assert!(
328            self.work_surface.replace(value).is_none(),
329            "work-surface facet already set"
330        );
331        self
332    }
333
334    pub fn with_navigation(
335        mut self,
336        value: &'a mut dyn crate::navigation::CommandNavigationContext,
337    ) -> Self {
338        assert!(
339            self.navigation.replace(value).is_none(),
340            "navigation facet already set"
341        );
342        self
343    }
344
345    pub fn with_voice_input(
346        mut self,
347        value: &'a mut dyn crate::navigation::CommandVoiceInputContext,
348    ) -> Self {
349        assert!(
350            self.voice_input.replace(value).is_none(),
351            "voice-input facet already set"
352        );
353        self
354    }
355
356    pub fn with_permissions(mut self, value: &'a mut dyn CommandPermissionsContext) -> Self {
357        assert!(
358            self.permissions.replace(value).is_none(),
359            "permissions facet already set"
360        );
361        self
362    }
363    pub fn with_config_status(mut self, value: &'a mut dyn CommandConfigStatusContext) -> Self {
364        assert!(
365            self.config_status.replace(value).is_none(),
366            "config status facet already set"
367        );
368        self
369    }
370
371    pub fn with_session(mut self, value: &'a mut dyn CommandSessionContext) -> Self {
372        assert!(
373            self.session.replace(value).is_none(),
374            "session facet already set"
375        );
376        self
377    }
378
379    pub fn with_model(mut self, value: &'a mut dyn CommandModelContext) -> Self {
380        assert!(
381            self.model.replace(value).is_none(),
382            "model facet already set"
383        );
384        self
385    }
386
387    pub fn with_cost(mut self, value: &'a mut dyn CommandCostContext) -> Self {
388        assert!(self.cost.replace(value).is_none(), "cost facet already set");
389        self
390    }
391
392    pub fn with_mode_policy(mut self, value: &'a mut dyn CommandModePolicyContext) -> Self {
393        assert!(
394            self.mode_policy.replace(value).is_none(),
395            "mode-policy facet already set"
396        );
397        self
398    }
399
400    pub fn with_system_prompt(mut self, value: &'a mut dyn CommandSystemPromptContext) -> Self {
401        assert!(
402            self.system_prompt.replace(value).is_none(),
403            "system-prompt facet already set"
404        );
405        self
406    }
407
408    pub fn with_skills(mut self, value: &'a mut dyn CommandSkillsContext) -> Self {
409        assert!(
410            self.skills.replace(value).is_none(),
411            "skills facet already set"
412        );
413        self
414    }
415
416    pub fn with_workspace(mut self, value: &'a mut dyn CommandWorkspaceContext) -> Self {
417        assert!(
418            self.workspace.replace(value).is_none(),
419            "workspace facet already set"
420        );
421        self
422    }
423
424    pub fn with_presentation(mut self, value: &'a mut dyn CommandPresentationContext) -> Self {
425        assert!(
426            self.presentation.replace(value).is_none(),
427            "presentation facet already set"
428        );
429        self
430    }
431
432    pub fn with_media(mut self, value: &'a mut dyn CommandMediaContext) -> Self {
433        assert!(
434            self.media.replace(value).is_none(),
435            "media facet already set"
436        );
437        self
438    }
439
440    pub fn with_memory(mut self, value: &'a mut dyn CommandMemoryContext) -> Self {
441        assert!(
442            self.memory.replace(value).is_none(),
443            "memory facet already set"
444        );
445        self
446    }
447
448    pub fn with_project(mut self, value: &'a mut dyn CommandProjectContext) -> Self {
449        assert!(
450            self.project.replace(value).is_none(),
451            "project facet already set"
452        );
453        self
454    }
455
456    pub fn with_skill_group(mut self, value: &'a mut dyn CommandSkillGroupContext) -> Self {
457        assert!(
458            self.skill_group.replace(value).is_none(),
459            "skill-group facet already set"
460        );
461        self
462    }
463
464    pub fn with_plugin(mut self, value: &'a mut dyn CommandPluginContext) -> Self {
465        assert!(
466            self.plugin.replace(value).is_none(),
467            "plugin facet already set"
468        );
469        self
470    }
471
472    pub fn with_lifecycle(mut self, value: &'a mut dyn CommandSessionLifecycleContext) -> Self {
473        assert!(
474            self.lifecycle.replace(value).is_none(),
475            "lifecycle facet already set"
476        );
477        self
478    }
479
480    pub fn with_control(mut self, value: &'a mut dyn CommandSessionControlContext) -> Self {
481        assert!(
482            self.control.replace(value).is_none(),
483            "control facet already set"
484        );
485        self
486    }
487
488    pub fn with_structcopy(mut self, value: &'a mut dyn CommandSessionStructcopyContext) -> Self {
489        assert!(
490            self.structcopy.replace(value).is_none(),
491            "structcopy facet already set"
492        );
493        self
494    }
495
496    pub fn with_export(mut self, value: &'a mut dyn CommandSessionExportContext) -> Self {
497        assert!(
498            self.export.replace(value).is_none(),
499            "export facet already set"
500        );
501        self
502    }
503
504    pub fn with_debug_receipts(mut self, value: &'a mut dyn CommandDebugReceiptsContext) -> Self {
505        assert!(
506            self.debug_receipts.replace(value).is_none(),
507            "debug_receipts facet already set"
508        );
509        self
510    }
511
512    pub fn with_debug_change(mut self, value: &'a mut dyn CommandDebugChangeContext) -> Self {
513        assert!(
514            self.debug_change.replace(value).is_none(),
515            "debug_change facet already set"
516        );
517        self
518    }
519
520    pub fn with_debug_history(mut self, value: &'a mut dyn CommandDebugHistoryContext) -> Self {
521        assert!(
522            self.debug_history.replace(value).is_none(),
523            "debug_history facet already set"
524        );
525        self
526    }
527
528    pub fn with_debug_diff(mut self, value: &'a mut dyn CommandDebugDiffContext) -> Self {
529        assert!(
530            self.debug_diff.replace(value).is_none(),
531            "debug_diff facet already set"
532        );
533        self
534    }
535
536    pub fn with_debug_undo(mut self, value: &'a mut dyn CommandDebugUndoContext) -> Self {
537        assert!(
538            self.debug_undo.replace(value).is_none(),
539            "debug_undo facet already set"
540        );
541        self
542    }
543
544    pub fn with_debug_diagnostics(
545        mut self,
546        value: &'a mut dyn CommandDebugDiagnosticsContext,
547    ) -> Self {
548        assert!(
549            self.debug_diagnostics.replace(value).is_none(),
550            "debug diagnostics facet already set"
551        );
552        self
553    }
554}
555
556impl Default for CommandContexts<'_> {
557    fn default() -> Self {
558        Self::empty()
559    }
560}