Skip to content

Commit 5b771ad

Browse files
committed
Merge remote-tracking branch 'origin/main' into perf/native-build-identity
2 parents f08ff72 + 7d35d90 commit 5b771ad

118 files changed

Lines changed: 6527 additions & 401 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎Cargo.toml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ errno = "0.3"
140140
windows-sys = { version = "0.61", default-features = false, features = ["Win32_System_Diagnostics_Debug"] }
141141
# Recall's local store: SQLite compiled in, so every build has the same engine version.
142142
rusqlite = { version = "0.37", features = ["bundled", "column_decltype"] }
143-
rustix = { version = "1.1.4", default-features = false, features = ["fs", "process", "std", "termios"] }
143+
rustix = { version = "1.1.4", default-features = false, features = ["fs", "process", "std", "stdio", "termios"] }
144144
saphyr-parser = "0.1"
145145
regex = "1"
146146
reqwest = { version = "0.12", default-features = false, features = ["json", "rustls-tls"] }

‎clients/typescript/src/schema.d.ts‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1914,7 +1914,7 @@ export interface components {
19141914
name: string;
19151915
target: string;
19161916
};
1917-
/** @description Locations report bounded facts, never connection strings or user exception text. */
1917+
/** @description Locations report bounded facts, never connection strings; a failed warehouse read carries only its redacted error text. */
19181918
LocationHealth: {
19191919
/** @enum {string} */
19201920
kind: "heartbeat";
@@ -1939,6 +1939,8 @@ export interface components {
19391939
} | {
19401940
/** Format: int64 */
19411941
elapsed_millis: number;
1942+
/** @description A failed read's warehouse error, redacted and bounded; absent otherwise. */
1943+
error?: string | null;
19421944
failure?: null | components["schemas"]["WarehouseSyncFailure"];
19431945
/** Format: int64 */
19441946
generation: number;
@@ -3587,7 +3589,7 @@ export interface components {
35873589
session_id: string;
35883590
};
35893591
/**
3590-
* @description Closed failure stages keep warehouse errors and credentials out of health observations.
3592+
* @description The stage a warehouse sync failed at; only a failed read also reports its redacted error.
35913593
* @enum {string}
35923594
*/
35933595
WarehouseSyncFailure: "watermark" | "read" | "delivery" | "busy";
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
//! Apply one message to the `sqb dev` screen.
2+
3+
use std::time::Instant;
4+
5+
use sqlbuild_core::screen_text::models::PhaseState;
6+
7+
use crate::dev_screen::_helpers::lines::{compose, ready_lines};
8+
use crate::dev_screen::_helpers::transitions::{
9+
child_event, client_text, key_pressed, located, logged, run_reported, step_finished,
10+
step_started, warehouse_settled,
11+
};
12+
use crate::dev_screen::models::{DevScreen, DevStep, ScreenAction, ScreenMessage, ScreenOutput};
13+
14+
/// `screen` changed by `message` at `now` (local wall time `clock`), and the text to write.
15+
pub(crate) fn apply_screen_message(
16+
screen: DevScreen,
17+
message: ScreenMessage,
18+
now: Instant,
19+
clock: &str,
20+
) -> (DevScreen, ScreenOutput) {
21+
let mut action: ScreenAction = ScreenAction::None;
22+
let mut raw: String = String::new();
23+
let (mut screen, mut lines): (DevScreen, Vec<String>) = match message {
24+
ScreenMessage::Started { step, detail } => step_started(screen, (step, detail), now),
25+
ScreenMessage::Finished { step, detail } => {
26+
step_finished(screen, (step, PhaseState::Ok), detail, now)
27+
}
28+
ScreenMessage::Child(event) => child_event(screen, event, now, clock),
29+
ScreenMessage::Location(location) => located(screen, location),
30+
ScreenMessage::Triggers(triggers) => (DevScreen { triggers, ..screen }, Vec::new()),
31+
ScreenMessage::Warehouse { adapter, state } => {
32+
let (screen, closing, lines) = warehouse_settled(screen, (adapter, state), now, clock);
33+
raw = closing;
34+
(screen, lines)
35+
}
36+
ScreenMessage::ClientOutput(text) => {
37+
let (screen, text) = client_text(screen, &text, now);
38+
return (screen, ScreenOutput { text, action });
39+
}
40+
ScreenMessage::Run {
41+
name,
42+
detail,
43+
finished,
44+
} => {
45+
let lines: Vec<String> = run_reported(&screen, (name, detail), finished, clock);
46+
(screen, lines)
47+
}
48+
ScreenMessage::Log(line) => logged(screen, line),
49+
ScreenMessage::Key(key) => {
50+
let (screen, shown, asked) = key_pressed(screen, key);
51+
action = asked;
52+
(screen, shown)
53+
}
54+
ScreenMessage::Tick => {
55+
if !screen.live || screen.ready || screen.client_output || connecting(&screen) {
56+
return (
57+
screen,
58+
ScreenOutput {
59+
text: String::new(),
60+
action,
61+
},
62+
);
63+
}
64+
let frame: usize = screen.frame + 1;
65+
(DevScreen { frame, ..screen }, Vec::new())
66+
}
67+
};
68+
if screen.event_gap && !lines.is_empty() {
69+
screen.event_gap = false;
70+
lines.insert(0, String::new());
71+
}
72+
let (screen, ready) = ready_lines(screen, now);
73+
lines.extend(ready);
74+
let (screen, text) = compose(screen, (&raw, &lines), now);
75+
(screen, ScreenOutput { text, action })
76+
}
77+
78+
/// The foreground connection may prompt on the terminal itself, so nothing redraws over it.
79+
fn connecting(screen: &DevScreen) -> bool {
80+
screen.steps.iter().any(|row| {
81+
row.step == DevStep::Warehouse && matches!(row.state, PhaseState::Running { .. })
82+
})
83+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
//! Route this process's stderr into the screen's log while the screen owns the terminal.
2+
3+
use std::fs::File;
4+
#[cfg(unix)]
5+
use std::io::{BufRead, BufReader};
6+
#[cfg(unix)]
7+
use std::os::fd::AsFd;
8+
9+
use tokio::sync::mpsc::UnboundedSender;
10+
11+
use crate::dev_screen::models::ScreenMessage;
12+
13+
/// Point stderr at a pipe whose lines become log messages, and return the terminal it pointed at.
14+
#[cfg(unix)]
15+
pub(crate) fn capture_stderr(
16+
messages: UnboundedSender<ScreenMessage>,
17+
) -> std::io::Result<Option<File>> {
18+
let terminal: File = File::from(std::io::stderr().as_fd().try_clone_to_owned()?);
19+
let (reader, writer) = std::io::pipe()?;
20+
rustix::stdio::dup2_stderr(&writer)?;
21+
drop(writer);
22+
std::thread::spawn(move || {
23+
for line in BufReader::new(reader).split(b'\n') {
24+
let Ok(line) = line else {
25+
return;
26+
};
27+
let text: String = String::from_utf8_lossy(&line)
28+
.trim_end_matches('\r')
29+
.to_owned();
30+
if messages.send(ScreenMessage::Log(text)).is_err() {
31+
return;
32+
}
33+
}
34+
});
35+
Ok(Some(terminal))
36+
}
37+
38+
/// Without file-descriptor redirection, stderr stays on the terminal.
39+
#[cfg(not(unix))]
40+
pub(crate) fn capture_stderr(
41+
_messages: UnboundedSender<ScreenMessage>,
42+
) -> std::io::Result<Option<File>> {
43+
Ok(None)
44+
}
45+
46+
/// Point stderr back at the terminal.
47+
#[cfg(unix)]
48+
pub(crate) fn release_stderr(terminal: &File) {
49+
let _ = rustix::stdio::dup2_stderr(terminal);
50+
}
51+
52+
/// Nothing was redirected.
53+
#[cfg(not(unix))]
54+
pub(crate) fn release_stderr(_terminal: &File) {}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
//! The screen task: apply each message, write its text, and leave the closing text behind.
2+
3+
use std::fs::File;
4+
use std::io::Write;
5+
use std::time::Instant;
6+
7+
use sqlbuild_core::screen_text::main::redraw_rows::redraw_rows;
8+
use tokio::sync::mpsc::{UnboundedReceiver, UnboundedSender};
9+
use tokio::sync::oneshot;
10+
11+
use crate::dev_screen::_helpers::apply_screen_message::apply_screen_message;
12+
use crate::dev_screen::_helpers::capture::release_stderr;
13+
use crate::dev_screen::_helpers::keys::KeyReader;
14+
use crate::dev_screen::constants::{FAILURE_LOG_LINES, TICK};
15+
use crate::dev_screen::models::{DevScreen, ScreenAction, ScreenMessage};
16+
17+
/// Apply each message and write what it draws, until the session finishes; then leave the closing text.
18+
pub(crate) async fn draw_screen(
19+
mut screen: DevScreen,
20+
(mut inbox, actions): (
21+
UnboundedReceiver<ScreenMessage>,
22+
UnboundedSender<ScreenAction>,
23+
),
24+
mut finished: oneshot::Receiver<bool>,
25+
(keys, terminal): (Option<KeyReader>, Option<File>),
26+
) {
27+
let mut tick = tokio::time::interval(TICK);
28+
tick.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Skip);
29+
let failed: bool = loop {
30+
let message: ScreenMessage = tokio::select! {
31+
failed = &mut finished => break failed.unwrap_or(false),
32+
message = inbox.recv() => match message {
33+
Some(message) => message,
34+
None => break false,
35+
},
36+
_ = tick.tick(), if screen.live => ScreenMessage::Tick,
37+
};
38+
let clock: String = chrono::Local::now().format("%H:%M:%S").to_string();
39+
let output;
40+
(screen, output) = apply_screen_message(screen, message, Instant::now(), &clock);
41+
write_screen(terminal.as_ref(), &output.text);
42+
if output.action != ScreenAction::None {
43+
let _ = actions.send(output.action);
44+
}
45+
};
46+
drop(keys);
47+
if let Some(terminal) = terminal.as_ref() {
48+
release_stderr(terminal);
49+
}
50+
write_screen(None, &closing_text(&screen, failed));
51+
}
52+
53+
/// What stays on the terminal after the screen stops: the live rows cleared and, on failure, the newest log lines that explain it.
54+
fn closing_text(screen: &DevScreen, failed: bool) -> String {
55+
let mut text: String = if screen.live && screen.drawn > 0 {
56+
redraw_rows(screen.drawn, &[])
57+
} else {
58+
String::new()
59+
};
60+
if failed && !screen.show_log {
61+
let skip: usize = screen.log.len().saturating_sub(FAILURE_LOG_LINES);
62+
for line in screen.log.iter().skip(skip) {
63+
text.push_str(line);
64+
text.push('\n');
65+
}
66+
}
67+
text
68+
}
69+
70+
/// Write to the terminal stderr was taken from, or to stderr when it was not redirected.
71+
fn write_screen(terminal: Option<&File>, text: &str) {
72+
if text.is_empty() {
73+
return;
74+
}
75+
match terminal {
76+
Some(mut terminal) => drop(terminal.write_all(text.as_bytes())),
77+
None => {
78+
let mut stderr = std::io::stderr().lock();
79+
drop(
80+
stderr
81+
.write_all(text.as_bytes())
82+
.and_then(|()| stderr.flush()),
83+
);
84+
}
85+
}
86+
}
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
//! Hotkeys from the terminal: stdin in raw mode, handed back to the warehouse client while it prompts.
2+
3+
use std::sync::Arc;
4+
use std::sync::atomic::Ordering;
5+
use std::thread::JoinHandle;
6+
#[cfg(unix)]
7+
use std::time::Duration;
8+
9+
#[cfg(unix)]
10+
use rustix::termios::{
11+
LocalModes, OptionalActions, SpecialCodeIndex, Termios, tcgetattr, tcsetattr,
12+
};
13+
use tokio::sync::mpsc::UnboundedSender;
14+
15+
#[cfg(unix)]
16+
use crate::dev_screen::constants::KEY_POLL;
17+
use crate::dev_screen::models::{KeyGate, ScreenMessage};
18+
19+
/// The hotkey thread; dropping it restores the terminal.
20+
pub(crate) struct KeyReader {
21+
gate: Arc<KeyGate>,
22+
thread: Option<JoinHandle<()>>,
23+
}
24+
25+
impl KeyReader {
26+
/// Start reading keys into `keys`, or `None` when stdin is not a terminal.
27+
#[cfg(unix)]
28+
pub(crate) fn start(keys: UnboundedSender<ScreenMessage>) -> Option<Self> {
29+
let cooked: Termios = match tcgetattr(std::io::stdin()) {
30+
Ok(cooked) => cooked,
31+
Err(_) => return None,
32+
};
33+
let gate: Arc<KeyGate> = Arc::new(KeyGate::default());
34+
let shared: Arc<KeyGate> = Arc::clone(&gate);
35+
let thread = std::thread::spawn(move || read_keys(&shared, &cooked, &keys));
36+
Some(Self {
37+
gate,
38+
thread: Some(thread),
39+
})
40+
}
41+
42+
/// Hotkeys need a Unix terminal; the screen is plain elsewhere.
43+
#[cfg(not(unix))]
44+
pub(crate) fn start(_keys: UnboundedSender<ScreenMessage>) -> Option<Self> {
45+
None
46+
}
47+
48+
pub(crate) fn gate(&self) -> Arc<KeyGate> {
49+
Arc::clone(&self.gate)
50+
}
51+
}
52+
53+
impl Drop for KeyReader {
54+
fn drop(&mut self) {
55+
self.gate.stop.store(true, Ordering::SeqCst);
56+
if let Some(thread) = self.thread.take() {
57+
drop(thread.join());
58+
}
59+
}
60+
}
61+
62+
/// Keys without echo or line buffering; Ctrl-C still interrupts, and reads give up after a tenth of a second.
63+
#[cfg(unix)]
64+
fn raw_mode(cooked: &Termios) -> Termios {
65+
let mut raw: Termios = cooked.clone();
66+
raw.local_modes -= LocalModes::ICANON | LocalModes::ECHO;
67+
raw.special_codes[SpecialCodeIndex::VMIN] = 0;
68+
raw.special_codes[SpecialCodeIndex::VTIME] = 1;
69+
raw
70+
}
71+
72+
#[cfg(unix)]
73+
fn read_keys(gate: &KeyGate, cooked: &Termios, keys: &UnboundedSender<ScreenMessage>) {
74+
let raw: Termios = raw_mode(cooked);
75+
let mut byte: [u8; 1] = [0];
76+
while !gate.stop.load(Ordering::SeqCst) && !keys.is_closed() {
77+
let reading: bool = gate.reading.load(Ordering::SeqCst);
78+
if gate.closed.load(Ordering::SeqCst) {
79+
if reading {
80+
let _ = tcsetattr(std::io::stdin(), OptionalActions::Now, cooked);
81+
gate.reading.store(false, Ordering::SeqCst);
82+
}
83+
std::thread::sleep(KEY_POLL);
84+
continue;
85+
}
86+
if !reading {
87+
if tcsetattr(std::io::stdin(), OptionalActions::Now, &raw).is_err() {
88+
return;
89+
}
90+
gate.reading.store(true, Ordering::SeqCst);
91+
}
92+
match rustix::io::read(std::io::stdin(), &mut byte) {
93+
Ok(1) => drop(keys.send(ScreenMessage::Key(char::from(byte[0]).to_ascii_lowercase()))),
94+
Ok(_) => {}
95+
Err(_) => std::thread::sleep(Duration::from_millis(100)),
96+
}
97+
}
98+
let _ = tcsetattr(std::io::stdin(), OptionalActions::Now, cooked);
99+
gate.reading.store(false, Ordering::SeqCst);
100+
}

0 commit comments

Comments
 (0)