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