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
//! TUI adapters for the contract slash commands.
//!
//! The TUI runs under the alternate screen, so the shared headless
//! `run_contracts` dispatcher — which `emit`s to a thread-local
//! seam — is wrapped here: the session's active profile is stamped onto the
//! command (the same field the headless `--profile` flag sets, not a second
//! resolution or a second privacy decision), the dispatcher runs, and its
//! captured output is pushed into the transcript as a system or error block.
//!
//! Extracted from `dispatch_actions.rs` to keep that file under the size cap;
//! the seam that routes a command at the active profile lives here, with its
//! tests.
use super::transcript::{BlockKind, Transcript};
use crate::cli::ContractsCommand;
use crate::commands::{
capture_output_start, capture_output_take, run_contracts as run_contracts_command,
};
use crate::config::runtime::RuntimeConfig;
use crate::interactive::session_resume::block_on;
use crate::interactive::session_state::SessionState;
use crate::render::RenderFormat;
/// Runs a contract slash command through the shared `run_contracts` dispatcher
/// and pushes its rendered output into the transcript. The TUI runs under the
/// alternate screen, so `run_contracts`'s `emit` output is captured through the
/// thread-local seam instead of going to the process stdout; a non-zero exit
/// surfaces as an error block.
pub(super) fn run_contracts(
transcript: &mut Transcript,
state: &SessionState,
runtime: &RuntimeConfig,
state_db: &saya_store::SqliteStateStore,
format: RenderFormat,
command: &ContractsCommand,
) {
// The session's selected profile maps to the headless `--profile` field:
// setting it (not a second resolution) routes the command at the database
// the user /connect-ed to, matching every other TUI slash command.
let command = with_profile(command, state.profile.as_deref());
capture_output_start();
// The shared dispatcher returns Ok(code) for every typed outcome (a store
// error is emitted as a diagnostic and returned non-zero); the outer Err is
// a render/IO failure, surfaced here as an error block.
let code = match block_on(run_contracts_command(command, runtime, format, state_db)) {
Ok(code) => code,
Err(error) => {
capture_output_take();
transcript.push(BlockKind::Error, error.to_string());
return;
}
};
let (out, err) = capture_output_take();
let body = if out.trim().is_empty() { err } else { out };
if code == 0 {
transcript.push(BlockKind::System, body.trim_end().to_string());
} else {
transcript.push(BlockKind::Error, body.trim_end().to_string());
}
}
/// Sets the `profile` field on a `ContractsCommand` to the session's active
/// profile. This is the same field the headless `--profile` flag sets; it is
/// not a second resolution or a second privacy decision.
///
/// The match is exhaustive on purpose: every variant is named, so a future
/// profile-bearing variant the slash path routes through the TUI is a compile
/// error here, not a silent fall-through that reads the configured default's
/// data. `Forget` has no `profile` field — it addresses a claim by id — so it
/// passes through unchanged.
fn with_profile(command: &ContractsCommand, profile: Option<&str>) -> ContractsCommand {
let profile = profile.map(str::to_string);
match command {
ContractsCommand::List { .. } => ContractsCommand::List { profile },
ContractsCommand::Show { table, .. } => ContractsCommand::Show {
table: table.clone(),
profile,
},
ContractsCommand::Queue { limit, .. } => ContractsCommand::Queue {
profile,
limit: *limit,
},
ContractsCommand::Remember {
table,
kind,
value,
column,
reason,
..
} => ContractsCommand::Remember {
table: table.clone(),
kind: *kind,
value: value.clone(),
column: column.clone(),
reason: reason.clone(),
profile,
},
// `Decide` carries a `profile` field like the other profiled
// reads/writes: the TUI stamps the session's active profile so a
// `/confirm ki-xxxx` resolves against the database the user /connect-ed
// to, not the configured default (cross-profile isolation, the same
// invariant `/queue`'s stamp upholds).
ContractsCommand::Decide {
prefix, decision, ..
} => ContractsCommand::Decide {
prefix: prefix.clone(),
decision: *decision,
profile,
},
// the batch approve is a profiled command like `/queue` — the
// session's active profile is stamped so `/approve-all` approves the
// queue of the database the user is looking at, never the configured
// default's.
ContractsCommand::ApproveAll { limit, yes, .. } => ContractsCommand::ApproveAll {
profile,
limit: *limit,
yes: *yes,
},
ContractsCommand::Forget { claim_id, reason } => ContractsCommand::Forget {
claim_id: claim_id.clone(),
reason: *reason,
},
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::cli::ForgetReasonArg;
// The active profile a /connect set, differing from any configured default.
const ACTIVE: &str = "staging";
/// Every `ContractsCommand` variant that carries a `profile` field must take
/// the session's active profile when routed through the TUI — the same field
/// the headless `--profile` flag sets. A variant that falls through loses the
/// profile and silently reads the configured default, defeating cross-profile
/// isolation. `/queue` is where a human confirms a claim, so the queue showing
/// another profile's candidates is the worst case.
#[test]
fn with_profile_stamps_active_profile_on_every_profiled_contract_variant() {
// `/queue` parses to Queue { profile: None, limit }; the TUI must route it
// at the active profile, not the configured default.
let queue = ContractsCommand::Queue {
profile: None,
limit: Some(20),
};
assert_eq!(
with_profile(&queue, Some(ACTIVE)),
ContractsCommand::Queue {
profile: Some(ACTIVE.into()),
limit: Some(20),
},
"/queue lost the active profile — it would read the configured default's candidates"
);
// The other profile-bearing read/write variants, for completeness.
assert_eq!(
with_profile(&ContractsCommand::List { profile: None }, Some(ACTIVE)),
ContractsCommand::List {
profile: Some(ACTIVE.into()),
}
);
assert_eq!(
with_profile(
&ContractsCommand::Show {
table: "a.b.c".into(),
profile: None,
},
Some(ACTIVE),
),
ContractsCommand::Show {
table: "a.b.c".into(),
profile: Some(ACTIVE.into()),
}
);
assert_eq!(
with_profile(
&ContractsCommand::Remember {
table: "a.b.c".into(),
kind: crate::cli::ClaimKindArg::Alias,
value: "v".into(),
column: None,
reason: None,
profile: None,
},
Some(ACTIVE),
),
ContractsCommand::Remember {
table: "a.b.c".into(),
kind: crate::cli::ClaimKindArg::Alias,
value: "v".into(),
column: None,
reason: None,
profile: Some(ACTIVE.into()),
}
);
}
/// The claim-keyed variant (`Forget`) has no profile field — it addresses a
/// claim by id, so the active profile must NOT be injected. This guards
/// against an over-broad fix that stamps a profile where none exists. (`Decide`
/// is claim-keyed too but carries a `profile` field it stamps, above.)
#[test]
fn with_profile_leaves_claim_keyed_variants_untouched() {
let forget = ContractsCommand::Forget {
claim_id: "c-1".into(),
reason: ForgetReasonArg::UserRequest,
};
assert_eq!(with_profile(&forget, Some(ACTIVE)), forget);
}
/// `None` active profile (no /connect yet) must not invent one: the command
/// keeps `profile: None` and the dispatcher resolves the configured default.
#[test]
fn with_profile_none_keeps_none() {
let queue = ContractsCommand::Queue {
profile: None,
limit: None,
};
assert_eq!(
with_profile(&queue, None),
ContractsCommand::Queue {
profile: None,
limit: None,
}
);
}
}