Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 15 additions & 5 deletions docs/todo11.md
Original file line number Diff line number Diff line change
Expand Up @@ -464,12 +464,22 @@ scope 確認の結果、`drain_pipe` / `wait_with_timeout` / `run_cmd` は **cra
- [x] `cargo clippy --workspace -- -D warnings` clean
- [x] 173b 完了マーク (本 sub section)、173c/d/e は本 entry 内に未着手として残置

##### 173c: `drain_pipe` 3 variant 抽出
##### 173c: `drain_pipe` 3 variant 抽出 ✅ 実装完了 (2026-06-14)

- **variant A `drain_pipe_unlimited`**: `read_to_string` 全読み — cli-pr-monitor (1 callsite)
- **variant B `drain_pipe_capped(pipe, max_lines)`**: `read_until` + silent truncate — cli-push-runner / cli-push-pipeline / hooks-stop-quality (3 callsites、MAX_LINES 20/40 を callsite parameter として渡す)
- **variant C `drain_pipe_capped_reporting(pipe, max_lines)`**: 上記 + `"... (N lines truncated)"` 末尾報告 — cli-merge-pipeline (1 callsite、MAX_LINES=200)
- 作業計画: 173a 完了後に詳細展開
- **variant A `drain_pipe_unlimited`**: `read_to_string` 全読み — cli-pr-monitor (`runner.rs` 内部 2 callsites + `stages/push_jj_bookmark.rs` 2 callsites = 計 4 callsites)
- **variant B `drain_pipe_capped(pipe, max_lines)`**: `read_until` + silent truncate — cli-push-runner (`runner.rs` 2 callsites + `stages/{scratch_file_warning, push_jj_bookmark, pr_size_check, lint_screen, bookmark_check}.rs` 10 callsites = 計 12 callsites)、cli-push-pipeline (2 callsites)、hooks-stop-quality (2 callsites)
- **variant C `drain_pipe_capped_reporting(pipe, max_lines)`**: variant B + `"... (N lines truncated)"` 末尾報告 — cli-merge-pipeline (2 callsites、MAX_LINES=200)
- 計 22 callsites を 5 crate 横断で migration、`MAX_LINES` 定数は各 crate に保持して callsite で parameter として渡す形に変更 (挙動保存、crate 別の設計意図 = ログ用なら 40、メモリ保護なら 200、stop hook なら 20 を維持)
- **作業計画**:
- [x] `lib-subprocess/src/lib.rs` に 3 variant + 6 test (各 variant の代表 case: 短/長/over-cap/under-cap/reporting on/off) 追加
- [x] cli-pr-monitor `runner.rs` の `drain_pipe` impl + NOTE コメント削除、4 callsite を `lib_subprocess::drain_pipe_unlimited` に置換
- [x] cli-push-runner `runner.rs` の `drain_pipe` impl 削除、`MAX_LINES` を `pub(crate) const` 化して stage から参照可能に、12 callsite を `drain_pipe_capped(pipe, MAX_LINES)` に置換
- [x] cli-push-pipeline `main.rs` の `drain_pipe` impl 削除、2 callsite を `drain_pipe_capped(pipe, MAX_LINES)` に置換 (lib-subprocess は 173a で dep 追加済)
- [x] hooks-stop-quality `Cargo.toml` に `lib-subprocess` dep 追加、`drain_pipe` impl 削除、2 callsite を `drain_pipe_capped(pipe, MAX_LINES)` に置換
- [x] cli-merge-pipeline `main.rs` の `drain_pipe` impl 削除、2 callsite を `drain_pipe_capped_reporting(pipe, MAX_LINES)` に置換 (lib-subprocess は 173a で dep 追加済)
- [x] `cargo test --workspace` で全 pass 確認 (lib-subprocess 15 test + 6 影響 crate 含む workspace 全体 pass)
- [x] `cargo clippy --workspace -- -D warnings` clean
- [x] 173c 完了マーク (本 sub section)、173d/e は本 entry 内に未着手として残置

##### 173d: `run_cmd` variant 抽出

Expand Down
43 changes: 3 additions & 40 deletions src/cli-merge-pipeline/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
mod feedback;

use lib_jj_helpers::{get_jj_bookmarks as lib_get_jj_bookmarks, StderrMode};
use lib_subprocess::combine_output;
use lib_subprocess::{combine_output, drain_pipe_capped_reporting};
use serde::Deserialize;
use std::path::{Path, PathBuf};
use std::process::Command;
Expand Down Expand Up @@ -127,46 +127,9 @@ fn log_info(message: &str) {
eprintln!("[merge-pipeline] {}", message);
}

// ─── パイプ排出 ───

/// サブプロセス出力の最大収集行数(メモリ保護)
const MAX_LINES: usize = 200;

fn drain_pipe(pipe: impl std::io::Read + Send + 'static) -> std::thread::JoinHandle<String> {
std::thread::spawn(move || {
use std::io::BufRead;
let mut reader = std::io::BufReader::new(pipe);
let mut collected = Vec::with_capacity(MAX_LINES);
let mut buf = Vec::new();
let mut truncated = 0usize;

loop {
buf.clear();
match reader.read_until(b'\n', &mut buf) {
Ok(0) => break,
Ok(_) => {
if collected.len() < MAX_LINES {
collected.push(
String::from_utf8_lossy(&buf)
.trim_end_matches(&['\r', '\n'][..])
.to_string(),
);
} else {
truncated += 1;
}
}
Err(_) => break,
}
}
if truncated > 0 {
collected.push(format!("... ({} lines truncated)", truncated));
}
collected.join("\n")
})
}

// ─── コマンド実行 ───

fn run_cmd(name: &str, cmd: &str, timeout_secs: u64) -> (bool, String) {
let mut child = match Command::new("cmd")
.args(["/c", cmd])
Expand All @@ -178,8 +141,8 @@ fn run_cmd(name: &str, cmd: &str, timeout_secs: u64) -> (bool, String) {
Err(e) => return (false, format!("Failed to execute {}: {}", cmd, e)),
};

let stdout_handle = drain_pipe(child.stdout.take().unwrap());
let stderr_handle = drain_pipe(child.stderr.take().unwrap());
let stdout_handle = drain_pipe_capped_reporting(child.stdout.take().unwrap(), MAX_LINES);
let stderr_handle = drain_pipe_capped_reporting(child.stderr.take().unwrap(), MAX_LINES);

let deadline = std::time::Instant::now() + Duration::from_secs(timeout_secs);
let timed_out = loop {
Expand Down
19 changes: 3 additions & 16 deletions src/cli-pr-monitor/src/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,11 @@ use std::path::{Path, PathBuf};
use std::process::Command;
use std::time::Duration;

// NOTE: push-pipeline 版は MAX_LINES=40 でログ表示用に切り詰めるが、
// こちらは check-ci-coderabbit の JSON 出力全体をパースするため制限なし。
use lib_subprocess::drain_pipe_unlimited;

const POLL_INTERVAL_MS: u64 = 100;
pub(crate) const JJ_CMD_TIMEOUT_SECS: u64 = 30;

pub(crate) fn drain_pipe(
pipe: impl std::io::Read + Send + 'static,
) -> std::thread::JoinHandle<String> {
std::thread::spawn(move || {
use std::io::Read;
let mut output = String::new();
let mut reader = std::io::BufReader::new(pipe);
let _ = reader.read_to_string(&mut output);
output.trim_end().to_string()
})
}

/// 引数を配列で直接渡す版(スペースを含む引数を正しくハンドリング)
pub(crate) fn run_cmd_direct(
program: &str,
Expand All @@ -43,8 +30,8 @@ pub(crate) fn run_cmd_direct(
}
};

let stdout_handle = drain_pipe(child.stdout.take().unwrap());
let stderr_handle = drain_pipe(child.stderr.take().unwrap());
let stdout_handle = drain_pipe_unlimited(child.stdout.take().unwrap());
let stderr_handle = drain_pipe_unlimited(child.stderr.take().unwrap());

let deadline = std::time::Instant::now() + Duration::from_secs(timeout_secs);
let timed_out = loop {
Expand Down
4 changes: 2 additions & 2 deletions src/cli-pr-monitor/src/stages/push_jj_bookmark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,9 +165,9 @@ fn run_jj(args: &[&str], error_prefix: &str) -> Result<String, String> {
.map_err(|e| format!("{}: {}", error_prefix, e))?;

let stdout_handle =
crate::runner::drain_pipe(child.stdout.take().expect("stdout must be piped"));
lib_subprocess::drain_pipe_unlimited(child.stdout.take().expect("stdout must be piped"));
let stderr_handle =
crate::runner::drain_pipe(child.stderr.take().expect("stderr must be piped"));
lib_subprocess::drain_pipe_unlimited(child.stderr.take().expect("stderr must be piped"));

let status =
lib_subprocess::wait_with_timeout_safe(error_prefix, &mut child, JJ_TIMEOUT_SECS)
Expand Down
38 changes: 3 additions & 35 deletions src/cli-push-pipeline/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
//! 1 - パイプライン失敗(テスト失敗等)
//! 2 - 設定エラー

use lib_subprocess::combine_output;
use lib_subprocess::{combine_output, drain_pipe_capped};
use serde::Deserialize;
use std::path::{Path, PathBuf};
use std::process::Command;
Expand Down Expand Up @@ -67,41 +67,9 @@ fn log_info(message: &str) {
eprintln!("[push-pipeline] {}", message);
}

// ─── パイプ排出 (hooks-stop-quality から移植) ───

/// サブプロセス出力の最大収集行数
const MAX_LINES: usize = 40;

/// サブプロセスの stdout/stderr を別スレッドで収集する(最大 MAX_LINES 行)
fn drain_pipe(pipe: impl std::io::Read + Send + 'static) -> std::thread::JoinHandle<String> {
std::thread::spawn(move || {
use std::io::BufRead;
let mut reader = std::io::BufReader::new(pipe);
let mut collected = Vec::with_capacity(MAX_LINES);
let mut buf = Vec::new();

loop {
buf.clear();
match reader.read_until(b'\n', &mut buf) {
Ok(0) => break,
Ok(_) => {
if collected.len() < MAX_LINES {
collected.push(
String::from_utf8_lossy(&buf)
.trim_end_matches(&['\r', '\n'][..])
.to_string(),
);
}
}
Err(_) => break,
}
}
collected.join("\n")
})
}

// ─── コマンド実行 (hooks-stop-quality から移植) ───

/// シェルコマンドを実行し、タイムアウト付きで結果を返す
fn run_cmd(name: &str, cmd: &str, timeout_secs: u64) -> (bool, String) {
let mut child = match Command::new("cmd")
Expand All @@ -114,8 +82,8 @@ fn run_cmd(name: &str, cmd: &str, timeout_secs: u64) -> (bool, String) {
Err(e) => return (false, format!("Failed to execute {}: {}", cmd, e)),
};

let stdout_handle = drain_pipe(child.stdout.take().unwrap());
let stderr_handle = drain_pipe(child.stderr.take().unwrap());
let stdout_handle = drain_pipe_capped(child.stdout.take().unwrap(), MAX_LINES);
let stderr_handle = drain_pipe_capped(child.stderr.take().unwrap(), MAX_LINES);

let deadline = std::time::Instant::now() + Duration::from_secs(timeout_secs);
let timed_out = loop {
Expand Down
36 changes: 4 additions & 32 deletions src/cli-push-runner/src/runner.rs
Original file line number Diff line number Diff line change
@@ -1,38 +1,10 @@
use std::process::Command;

use lib_subprocess::{combine_output, wait_with_timeout_basic};
use lib_subprocess::{combine_output, drain_pipe_capped, wait_with_timeout_basic};

use crate::log::log_info;

const MAX_LINES: usize = 40;

pub(crate) fn drain_pipe(
pipe: impl std::io::Read + Send + 'static,
) -> std::thread::JoinHandle<String> {
std::thread::spawn(move || {
use std::io::BufRead;
let mut reader = std::io::BufReader::new(pipe);
let mut collected = Vec::with_capacity(MAX_LINES);
let mut buf = Vec::new();
loop {
buf.clear();
match reader.read_until(b'\n', &mut buf) {
Ok(0) => break,
Ok(_) => {
if collected.len() < MAX_LINES {
collected.push(
String::from_utf8_lossy(&buf)
.trim_end_matches(&['\r', '\n'][..])
.to_string(),
);
}
}
Err(_) => break,
}
}
collected.join("\n")
})
}
pub(crate) const MAX_LINES: usize = 40;

pub(crate) fn run_cmd(label: &str, cmd: &str, timeout_secs: u64) -> (bool, String) {
let mut child = match Command::new("cmd")
Expand All @@ -45,8 +17,8 @@ pub(crate) fn run_cmd(label: &str, cmd: &str, timeout_secs: u64) -> (bool, Strin
Err(e) => return (false, format!("Failed to execute {}: {}", cmd, e)),
};

let stdout_handle = drain_pipe(child.stdout.take().expect("stdout must be piped"));
let stderr_handle = drain_pipe(child.stderr.take().expect("stderr must be piped"));
let stdout_handle = drain_pipe_capped(child.stdout.take().expect("stdout must be piped"), MAX_LINES);
let stderr_handle = drain_pipe_capped(child.stderr.take().expect("stderr must be piped"), MAX_LINES);

let exit_status = match wait_with_timeout_basic(label, &mut child, timeout_secs) {
Ok(status) => status,
Expand Down
10 changes: 6 additions & 4 deletions src/cli-push-runner/src/stages/bookmark_check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,12 @@ fn run_jj_bookmark_list() -> Result<String, String> {
.spawn()
.map_err(|e| format!("jj bookmark list 起動失敗: {}", e))?;

let stdout_handle =
crate::runner::drain_pipe(child.stdout.take().expect("stdout must be piped"));
let stderr_handle =
crate::runner::drain_pipe(child.stderr.take().expect("stderr must be piped"));
let stdout_handle = lib_subprocess::drain_pipe_unlimited(
child.stdout.take().expect("stdout must be piped"),
);
let stderr_handle = lib_subprocess::drain_pipe_unlimited(
child.stderr.take().expect("stderr must be piped"),
);

let status =
lib_subprocess::wait_with_timeout_basic("jj bookmark list", &mut child, JJ_TIMEOUT_SECS)
Expand Down
10 changes: 8 additions & 2 deletions src/cli-push-runner/src/stages/lint_screen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,14 @@ fn invoke_classifier(params: &InvokeParams<'_>, diff: &str) -> Result<Classifier
.map_err(|e| format!("stdin 書き込み失敗: {}", e))?;
}

let stdout_handle = crate::runner::drain_pipe(child.stdout.take().expect("stdout piped"));
let stderr_handle = crate::runner::drain_pipe(child.stderr.take().expect("stderr piped"));
let stdout_handle = lib_subprocess::drain_pipe_capped(
child.stdout.take().expect("stdout piped"),
crate::runner::MAX_LINES,
);
let stderr_handle = lib_subprocess::drain_pipe_capped(
child.stderr.take().expect("stderr piped"),
crate::runner::MAX_LINES,
);

let exit = wait_with_timeout_basic(STAGE, &mut child, params.timeout_secs + 5)
.map_err(|e| format!("wait 失敗: {}", e))?;
Expand Down
8 changes: 6 additions & 2 deletions src/cli-push-runner/src/stages/pr_size_check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,12 @@ fn run_jj_diff_stat(revset: &str) -> Result<String, String> {
.spawn()
.map_err(|e| format!("jj diff --stat 起動失敗: {}", e))?;

let stdout_handle = crate::runner::drain_pipe(child.stdout.take().expect("stdout must be piped"));
let stderr_handle = crate::runner::drain_pipe(child.stderr.take().expect("stderr must be piped"));
let stdout_handle = lib_subprocess::drain_pipe_unlimited(
child.stdout.take().expect("stdout must be piped"),
);
let stderr_handle = lib_subprocess::drain_pipe_unlimited(
child.stderr.take().expect("stderr must be piped"),
);

let status =
lib_subprocess::wait_with_timeout_basic("jj diff --stat", &mut child, JJ_TIMEOUT_SECS)
Expand Down
10 changes: 6 additions & 4 deletions src/cli-push-runner/src/stages/push_jj_bookmark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,10 +139,12 @@ fn run_jj(args: &[&str], error_prefix: &str) -> Result<String, String> {
.spawn()
.map_err(|e| format!("{}: {}", error_prefix, e))?;

let stdout_handle =
crate::runner::drain_pipe(child.stdout.take().expect("stdout must be piped"));
let stderr_handle =
crate::runner::drain_pipe(child.stderr.take().expect("stderr must be piped"));
let stdout_handle = lib_subprocess::drain_pipe_unlimited(
child.stdout.take().expect("stdout must be piped"),
);
let stderr_handle = lib_subprocess::drain_pipe_unlimited(
child.stderr.take().expect("stderr must be piped"),
);

let status =
lib_subprocess::wait_with_timeout_basic(error_prefix, &mut child, JJ_TIMEOUT_SECS)
Expand Down
12 changes: 8 additions & 4 deletions src/cli-push-runner/src/stages/scratch_file_warning.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,10 +226,14 @@ fn run_jj_file_list_at() -> Result<String, String> {
.spawn()
.map_err(|e| format!("jj file list 起動失敗: {}", e))?;

let stdout_handle =
crate::runner::drain_pipe(child.stdout.take().expect("stdout must be piped"));
let stderr_handle =
crate::runner::drain_pipe(child.stderr.take().expect("stderr must be piped"));
let stdout_handle = lib_subprocess::drain_pipe_capped(
child.stdout.take().expect("stdout must be piped"),
crate::runner::MAX_LINES,
);
let stderr_handle = lib_subprocess::drain_pipe_capped(
child.stderr.take().expect("stderr must be piped"),
crate::runner::MAX_LINES,
);

let status =
lib_subprocess::wait_with_timeout_basic("jj file list", &mut child, JJ_TIMEOUT_SECS)
Expand Down
1 change: 1 addition & 0 deletions src/hooks-stop-quality/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ edition = "2021"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
toml = "0.8"
lib-subprocess = { path = "../lib-subprocess" }

# [profile.release] は workspace root (Cargo.toml) に集約 (ADR-026)
Loading