From 3d08a43a359e544f38a52a86f9f685bedea3dca2 Mon Sep 17 00:00:00 2001 From: Rongsong Shen Date: Wed, 2 Sep 2026 13:12:04 +0000 Subject: [PATCH] feat(scheduler-sim-repl-verbosity): task 4.1 - RED-phase sim_print_decision Gate tests Adds 3 tests in a new test_sim_print_decision_gate.c covering the old unconditional per-decision echo (sim_loop.c's sim_print_decision()) no longer duplicating the Round Rollup's per-round summary under `continue` mode (Requirement 2.3), while `step` mode and headless (non-interactive) batch runs stay completely unaffected (Requirements 5.1, 5.4). This is exactly the duplication bug-1610 reported. 1 of 3 tests fails as expected RED evidence (continue-mode bare "[sim] PEND jobid=..." line still appears alongside the Round Rollup's own summary today); the other 2 (step-mode and headless regression checks) correctly pass since they pin already-correct, unaffected behavior. New file rather than extending either sibling: test_repl_round_rollup.c is already 1022 lines (222 over the 800-line cap), and test_repl_event_echo.c (704 lines) deliberately keeps its fixtures job-free (bug-1595 discipline), which this task's decision-producing fixtures cannot follow. No production code touched (sim_loop.c/repl.c/repl_rollup.c/repl_break.c untouched) - tests only, per this task's RED-phase boundary. The actual gate implementation is task 4.2. Co-Authored-By: Claude Sonnet 5 Claude-Session: https://claude.ai/code/session_01YAWMhfRJFnmmnG8h97yUDd --- .../scheduler-sim-repl-verbosity/tasks.md | 2 +- src/scheduler/testsuite/meson.build | 30 + .../testsuite/test_sim_print_decision_gate.c | 704 ++++++++++++++++++ 3 files changed, 735 insertions(+), 1 deletion(-) create mode 100644 src/scheduler/testsuite/test_sim_print_decision_gate.c diff --git a/.kiro/specs/scheduler-sim-repl-verbosity/tasks.md b/.kiro/specs/scheduler-sim-repl-verbosity/tasks.md index 5109c1f1..dfd49d23 100644 --- a/.kiro/specs/scheduler-sim-repl-verbosity/tasks.md +++ b/.kiro/specs/scheduler-sim-repl-verbosity/tasks.md @@ -45,7 +45,7 @@ - _Boundary: Event Echo Gate_ - [ ] 4. (P) Integration: retire the redundant per-decision echo during continuous execution — Depends: 2.2 -- [ ] 4.1 (P) Write failing tests confirming the old per-decision echo no longer duplicates the new round summary +- [x] 4.1 (P) Write failing tests confirming the old per-decision echo no longer duplicates the new round summary - Cover: during continuous execution, no bare per-decision line appears outside the round summary; while single-stepping, full per-decision detail still prints exactly as before; a non-interactive (headless) replay's decision output is unaffected - Observable: the new test cases fail while the old unconditional per-decision print path still runs alongside the new round summary - _Requirements: 2.3, 5.1, 5.4_ diff --git a/src/scheduler/testsuite/meson.build b/src/scheduler/testsuite/meson.build index c78aa01d..d0e921da 100644 --- a/src/scheduler/testsuite/meson.build +++ b/src/scheduler/testsuite/meson.build @@ -962,6 +962,33 @@ sched_repl_event_echo_test = executable( dependencies: [scheduler_test_deps, libedit_dep], install: false) +# Contract test for the interactive simulation REPL's `sim_print_decision` +# Gate component (scheduler-sim-repl-verbosity spec, task 4.1, Requirements +# 2.3, 5.1, 5.4 "retire the redundant per-decision echo during continuous +# execution"): under `continue` mode, sim_print_decision()'s (sim_loop.c) +# bare per-decision line must no longer appear outside the Round Rollup's +# own per-round summary; `step` mode and non-interactive (headless, no -i) +# batch runs are completely unaffected and keep printing that bare line +# exactly as today. Same white-box #include pattern as +# sched_repl_round_rollup_test/sched_repl_event_echo_test above (core.c, +# core_clock.c, core_events.c, core_session.c, decision.c, plugin.c, +# plugins/builtin_fcfs.c, recording.c, recording_session.c, sim.c, +# sim_loop.c, sim_record.c, sim_verify.c, +# snapshot.c/snapshot_wire.c/snapshot_load.c, repl.c, repl_rollup.c, +# repl_break.c, repl_info.c, repl_params.c). Split into its own file rather +# than extended onto either sibling: test_repl_round_rollup.c is already +# 1022 lines (222 over this project's 800-line cap) on its own, and +# test_repl_event_echo.c (704 lines) deliberately keeps every one of its +# own fixtures job-free (bug-1595 jobid-reuse safety discipline), which +# this task's decision-producing fixtures cannot follow -- see this file's +# own header comment for the full rationale. +sched_sim_print_decision_gate_test = executable( + 'sched_sim_print_decision_gate_test', + ['test_sim_print_decision_gate.c'], + include_directories:[incdir], + dependencies: [scheduler_test_deps, libedit_dep], + install: false) + # Contract test for the interactive simulation REPL's breakpoint and # state-inspection slice (scheduler-core spec, task 12.2, Req 5 AC 22 # "Breakpoints"/"State inspection"): setting a breakpoint on a specific @@ -1418,6 +1445,9 @@ test('scheduler repl round rollup test', test('scheduler repl event echo test', sched_repl_event_echo_test, suite: 'scheduler', env: ['ASAN_OPTIONS=detect_leaks=0']) +test('scheduler sim print decision gate test', + sched_sim_print_decision_gate_test, suite: 'scheduler', + env: ['ASAN_OPTIONS=detect_leaks=0']) test('scheduler repl breakpoints inspection test', sched_repl_breakpoints_inspection_test, suite: 'scheduler', env: ['ASAN_OPTIONS=detect_leaks=0']) diff --git a/src/scheduler/testsuite/test_sim_print_decision_gate.c b/src/scheduler/testsuite/test_sim_print_decision_gate.c new file mode 100644 index 00000000..b0940fd3 --- /dev/null +++ b/src/scheduler/testsuite/test_sim_print_decision_gate.c @@ -0,0 +1,704 @@ +/* + * test_sim_print_decision_gate.c - contract test for the interactive + * simulation REPL's `sim_print_decision` Gate component + * (scheduler-sim-repl-verbosity spec, task 4.1, Requirements 2.3, 5.1, 5.4): + * retiring sim_print_decision()'s (sim_loop.c) unconditional bare + * per-decision echo under `continue` mode now that the Round Rollup (task + * 2.2, already implemented/committed) prints its own per-round summary -- + * while leaving `step` mode and non-interactive (headless) batch output + * completely untouched. + * + * Why a new file rather than extending an existing one: per this task's own + * dispatch instructions, line counts of the two most recent sibling REPL + * test files were checked first -- + * - test_repl_round_rollup.c is 1022 lines, already 222 lines OVER this + * project's 800-line cap (src/scheduler/CLAUDE.md) on its own; landing + * three more tests there would only grow that pre-existing violation. + * - test_repl_event_echo.c is 704 lines (96 lines of headroom before the + * cap), but its own header comment documents a deliberate, load-bearing + * "every fixture in this file is job-free" discipline (bug-1595 jobid- + * reuse safety) -- this task's fixtures are decision-producing rounds, + * which are inherently job-having, so extending that file would break + * its own stated invariant AND still risk pushing it over cap once the + * three tests' fixture/assertion surface (including a THIRD harness -- + * the non-fork, non-interactive batch-capture technique neither sibling + * file has any use for) is accounted for. + * Neither candidate has real headroom, so (following this spec's now- + * established proactive-split convention -- see test_repl_round_rollup.c's/ + * test_repl_event_echo.c's own "Correction (post-review, ...)" header notes + * citing design.md) this task creates a new file instead of reacting to a + * cap violation after the fact. + * + * bug-1610 (buglog.json) is the original user report of exactly this + * duplication: under `continue`, an unchanged PEND decision prints an + * identical bare "[sim] PEND jobid=..." line every round it remains + * pending, with zero new information each time now that the Round Rollup + * (task 2.2) already reports the same information once per collapsed + * range. This task's tests are the RED-phase proof that the duplication + * bug-1610 reported must go away. + * + * bug-1595 jobid-reuse safety: each test below uses its own dedicated + * replay fixture and a jobid unique to that fixture (6101/6102/6103) -- + * no fixture mixes a "real" job under test with an unrelated padding/filler + * job, so the specific hazard bug-1595 logged (a filler event's jobid + * accidentally mutating the real job under test) has no surface to occur + * on here. + * + * THIS TASK (4.1) IS RED-PHASE ONLY: sim_loop.c's decision-drain loop + * still calls sim_print_decision(d) unconditionally today, with no + * reference to sim->repl_state->mode or sim->interactive at that call site + * (task 4.2, a separate later task, adds the gate). Every symbol this file + * references already exists today, so all three tests build and link + * cleanly -- but test_continue_mode... below FAILS ITS OWN RUNTIME + * ASSERTION, because today's sim_print_decision() still unconditionally + * prints the bare per-decision line even in continue mode, duplicating the + * Round Rollup's own summary. The other two tests (step mode, headless + * batch) pin ALREADY-CORRECT, unaffected behavior and PASS today -- a + * legitimate regression-safety net for task 4.2 to keep green, the same + * shape task 3.1's own step-mode regression test took. Verified concretely + * against a real incremental build before this task was reported done -- + * see this task's own Status Report. + * + * Assumed output contract this file pins (matches design.md's + * `sim_print_decision` Gate component and repl_rollup.c's own Round Rollup + * print format, which test_repl_round_rollup.c's header comment documents + * in full): + * - sim_print_decision() (sim_loop.c) prints one bare + * "[sim] DISPATCH jobid=" / "[sim] PEND jobid=" / + * "[sim] PREEMPT jobid=" line per drained decision, in EVERY mode, + * TODAY. After task 4.2, this bare line must no longer appear while + * sim->interactive && rs->mode == XCE_REPL_MODE_CONTINUE -- the Round + * Rollup's own "[sim] round - (...): dispatch,

+ * pend, preempt -- jobids=..." summary line becomes the sole + * source of decision-related output in that mode. + * - Under `step` mode and non-interactive (headless, no -i/--interactive) + * batch runs, sim_print_decision()'s bare per-decision line must + * continue to print exactly as it does today -- this gate applies + * ONLY when sim->interactive && rs->mode == XCE_REPL_MODE_CONTINUE. + * - Tests use strstr() substring containment on captured real stdout, + * the same technique test_repl_round_rollup.c's/test_repl_event_echo.c's + * own tests establish. The Round Rollup's own summary line format + * ("... : dispatch,

pend, preempt -- jobids=...") never + * contains the bare line's literal substring ("PEND jobid=" / + * "DISPATCH jobid=" / "PREEMPT jobid="), so a strstr() check for the + * bare line's absence cannot be confused with the round summary's own, + * differently-shaped text. + */ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +#include "controller.capnp.h" +#include "kvmgr.h" +#include "scheduler.h" +#include "xstring.h" + +/* Pull in the real functions under test into this TU (white-box #include, + * same convention as test_repl_round_rollup.c/test_repl_event_echo.c/ + * test_repl_execution_control.c). */ +#include "../src/core.c" +#include "../src/core_clock.c" +#include "../src/core_events.c" +#include "../src/core_session.c" +#include "../src/decision.c" +#include "../src/plugin.c" +#include "../src/plugins/builtin_fcfs.c" +#include "../src/recording.c" +#include "../src/recording_session.c" +#include "../src/repl.c" +#include "../src/repl_rollup.c" +#include "../src/repl_break.c" +#include "../src/repl_info.c" +#include "../src/repl_params.c" +#include "../src/sim.c" +#include "../src/sim_loop.c" +#include "../src/sim_picker.c" /* bug-1567: sched_sim_driver_run() now calls sim_bootstrap_picker() (sim_loop.c-cap sibling) as an extern -- undefined reference at link time otherwise */ +#include "../src/sim_record.c" +#include "../src/sim_verify.c" +#include "../src/snapshot.c" +#include "../src/snapshot_load.c" +#include "../src/snapshot_wire.c" + +void kill_sched_job(void* arg) { (void)arg; } +void kill_sched_queue(void* arg) { (void)arg; } +void kill_sched_worker(void* arg) { (void)arg; } +void kill_sched_user(void* arg) { (void)arg; } +void kill_sched_project(void* arg) { (void)arg; } +void kill_sched_resval(xce_val_t* val) { (void)val; } + +/* resource-accounting.md's reconciliation (resource.c, not linked here -- + * this file only #includes core_events.c). core_events.c's + * XCE_CORE_EVENT_JOB_RUSAGE handler calls this whenever job->accounts != + * NULL; link-satisfying stub only, same rationale as every other stub in + * this block (see test_job_update.c's identical stub). */ +int sched_reconcile_job_resource_usage(xce_sched_core_t* c, + xce_job_account_t* a, + xce_job_rusages_t* r) +{ + (void)c; (void)a; (void)r; return 0; +} + +void sched_worker_release_host_ctx(xce_sched_worker_t* w) { (void)w; } + +int sched_fill_limit_requests(xce_scheduler_t* sched, xvec_idval_t* vec, + xlist_t* limits) +{ + (void)sched; + (void)vec; + (void)limits; + return 0; +} + +/* sched_sim_driver_run()'s (sim_loop.c, scheduler-sim-decision-parity task + * 2.1) new round-boundary call to the relocated sched_account_jobs() + * (core.h) -- a no-op stand-in here since this file does not link + * scheduler.c/resource.c, same rationale test_sim_equivalence.c's own copy + * documents. */ +int sched_account_jobs(xce_sched_core_t* core) +{ + (void)core; + return 0; +} + +/* Real reimplementation of schedobj.c's sched_resolve_job_picker -- same + * copy test_sim_alt_events.c/test_sim_verify.c/test_sim_event_loop.c/ + * test_repl_execution_control.c/test_repl_round_rollup.c/ + * test_repl_event_echo.c each carry (schedobj.c is not linked here: it + * would need plugin_register(), which requires a genuine + * xce_scheduler_t* neither this file's fixtures nor sim_loop.c's own + * core-scoped registration path have, Req 5 AC 17). */ +int sched_resolve_job_picker(xce_sched_core_t* core, xce_sched_plugin_t** out) +{ + xce_sched_plugin_t* plugin = NULL; + int rc = 0; + + if ((core == NULL) || (out == NULL)) { + return -EINVAL; + } + + *out = NULL; + + if (core->job_pick_plugin == NULL) { + return -EINVAL; + } + + lock(core->plugins, RTTI_LOCK_READ); + rc = key_find(core->plugins, core->job_pick_plugin, (void**)&plugin); + unlock(core->plugins); + + if (rc <= 0) { + return (rc < 0) ? rc : -ENOENT; + } + + if ((plugin == NULL) || (plugin->picker_iface == NULL) + || (plugin->picker_iface->next_job == NULL) + || (plugin->picker_iface->begin_session == NULL) + || (plugin->picker_iface->end_session == NULL) + || (plugin->picker_iface->on_dispatch == NULL) + || (plugin->picker_iface->on_pend == NULL)) { + return -EINVAL; + } + + *out = plugin; + + return 0; +} + +void setUp(void) { return; } +void tearDown(void) { return; } + +/* ---------------------------------------------------------------------- */ +/* Fixture helpers -- duplicated from test_repl_round_rollup.c/ */ +/* test_repl_event_echo.c/test_sim_event_loop.c (same techniques those */ +/* files already establish), per this file family's established split */ +/* convention (see header comment above): copied, not shared via a new */ +/* header. */ +/* ---------------------------------------------------------------------- */ + +static void make_tmp_dir(char* out, size_t out_len, const char* suffix) +{ + char tmpl[PATH_MAX]; + + snprintf(tmpl, sizeof(tmpl), "/tmp/sched_pdgate%s_XXXXXX", suffix); + TEST_ASSERT_NOT_NULL(mkdtemp(tmpl)); + TEST_ASSERT_TRUE(strlen(tmpl) < out_len); + strcpy(out, tmpl); +} + +static void build_fixture_scheduler(xce_scheduler_t* sched, char* record_dir) +{ + int rc; + + memset(sched, 0, sizeof(*sched)); + mylock_init(&(sched->record_lock)); + + rc = xce_sched_core_init(&(sched->core)); + TEST_ASSERT_EQUAL_INT(0, rc); + + sched->core.job_pick_plugin = (char*)"builtin_fcfs"; + sched->record_dir = record_dir; +} + +static FILE* write_header_and_snapshot(char* out_path, size_t out_path_len, + const char* suffix) +{ + char record_dir[PATH_MAX]; + xce_scheduler_t sched; + int rc; + + make_tmp_dir(record_dir, sizeof(record_dir), suffix); + build_fixture_scheduler(&sched, record_dir); + + rc = sched_record_start(&sched, "fixture.replay"); + TEST_ASSERT_EQUAL_INT(0, rc); + TEST_ASSERT_TRUE(strlen(sched.record_file_path) < out_path_len); + strcpy(out_path, sched.record_file_path); + + return sched.record_file; +} + +static void append_queue_update_event(FILE* f, uint64_t ts, + const char* queue_name) +{ + xce_queue_t queue; + replay_event_t src; + struct capn c; + struct capn_segment* cs; + ReplayEvent_ptr p; + + memset(&queue, 0, sizeof(queue)); + queue.queue = (char*)queue_name; + queue.status = QUEUE_STAT_OPEN; + queue.priority = 1; + + memset(&src, 0, sizeof(src)); + src.ts = ts; + src.body_which = ReplayEvent_body_queueUpdate; + src.body.queue_update = &queue; + + capn_init_malloc(&c); + cs = capn_root(&c).seg; + encode_ReplayEvent_ptr(cs, &p, &src); + capn_setp(capn_root(&c), 0, p.p); + + TEST_ASSERT_EQUAL_INT(0, write_capnp_message(f, &c)); + capn_free(&c); +} + +static void append_jobs_push_event(FILE* f, uint64_t ts, uint64_t jobid, + const char* queue_name) +{ + job_submit_data_t submit; + xce_job_t job; + xce_job_t* jobs[1]; + xce_jobs_data_t jobs_data; + replay_event_t src; + struct capn c; + struct capn_segment* cs; + ReplayEvent_ptr p; + + memset(&submit, 0, sizeof(submit)); + submit.queue = (char*)queue_name; + submit.user = (char*)"alice"; + submit.mintasks = 1; + submit.maxtasks = 1; + + memset(&job, 0, sizeof(job)); + job.jobid = jobid; + job.state = JOB_STAT_PEND; + job.submit = &submit; + + jobs[0] = &job; + + memset(&jobs_data, 0, sizeof(jobs_data)); + jobs_data.n_total = 1; + jobs_data.count = 1; + jobs_data.jobs = jobs; + + memset(&src, 0, sizeof(src)); + src.ts = ts; + src.body_which = ReplayEvent_body_jobsPush; + src.body.jobs_push = &jobs_data; + + capn_init_malloc(&c); + cs = capn_root(&c).seg; + encode_ReplayEvent_ptr(cs, &p, &src); + capn_setp(capn_root(&c), 0, p.p); + + TEST_ASSERT_EQUAL_INT(0, write_capnp_message(f, &c)); + capn_free(&c); +} + +static void append_replay_session(FILE* f, uint64_t begin_round) +{ + struct capn c; + struct capn_segment* cs; + ReplaySession_ptr sp; + + capn_init_malloc(&c); + cs = capn_root(&c).seg; + sp = new_ReplaySession(cs); + ReplaySession_set_beginRound(sp, begin_round); + capn_setp(capn_root(&c), 0, sp.p); + + TEST_ASSERT_EQUAL_INT(0, write_capnp_message(f, &c)); + capn_free(&c); +} + +static void append_end_of_stream_marker(FILE* f) +{ + replay_event_t src; + struct capn c; + struct capn_segment* cs; + ReplayEvent_ptr p; + + memset(&src, 0, sizeof(src)); + src.body_which = ReplayEvent_body_endSession; + + capn_init_malloc(&c); + cs = capn_root(&c).seg; + encode_ReplayEvent_ptr(cs, &p, &src); + capn_setp(capn_root(&c), 0, p.p); + + TEST_ASSERT_EQUAL_INT(0, write_capnp_message(f, &c)); + capn_free(&c); +} + +static void write_text_file(const char* path, const char* content) +{ + FILE* f = fopen(path, "w"); + + TEST_ASSERT_NOT_NULL(f); + TEST_ASSERT_TRUE(fputs(content, f) >= 0); + fclose(f); +} + +static char* read_whole_file(const char* path) +{ + FILE* f = fopen(path, "rb"); + long len; + char* buf; + + TEST_ASSERT_NOT_NULL(f); + TEST_ASSERT_EQUAL_INT(0, fseek(f, 0, SEEK_END)); + len = ftell(f); + TEST_ASSERT_TRUE(len >= 0); + rewind(f); + + buf = (char*)malloc((size_t)len + 1); + TEST_ASSERT_NOT_NULL(buf); + if (len > 0) { + TEST_ASSERT_EQUAL_INT((int)len, (int)fread(buf, 1, (size_t)len, f)); + } + buf[len] = '\0'; + fclose(f); + + return buf; +} + +/* child_run_repl_and_exit / run_repl_fixture_and_capture - the same fork/ + * freopen/waitpid/capture shape test_repl_round_rollup.c's/ + * test_repl_event_echo.c's own pair establishes: run a scripted command + * sequence to completion against a real sched_sim_driver_run() with + * args.interactive = true, capturing real stdout. Used by the `continue`- + * mode and `step`-mode tests below, which both need a real REPL command + * loop reading a scripted stdin. */ +static void child_run_repl_and_exit(const char* replay_path, + const char* script_path, + const char* capture_path) +{ + xce_sched_sim_driver_t sim; + sched_sim_args_t args; + + if (freopen(script_path, "r", stdin) == NULL) { + _exit(120); + } + if (freopen(capture_path, "w", stdout) == NULL) { + _exit(121); + } + + memset(&sim, 0, sizeof(sim)); + memset(&args, 0, sizeof(args)); + args.replay_path = replay_path; + args.sim_start_ts = SCHED_SIM_START_TS_LATEST; + args.sim_speed = 0; + args.interactive = true; + + (void)sched_sim_driver_run(&sim, &args); + + fflush(stdout); + _exit(111); /* unreachable -- the script's `quit` must already have + * exited */ +} + +static char* run_repl_fixture_and_capture(const char* replay_path, + const char* script, + const char* aux_suffix) +{ + char aux_dir[PATH_MAX]; + char script_path[PATH_MAX]; + char capture_path[PATH_MAX]; + pid_t pid; + int status; + + make_tmp_dir(aux_dir, sizeof(aux_dir), aux_suffix); + snprintf(script_path, sizeof(script_path), "%s/script.txt", aux_dir); + snprintf(capture_path, sizeof(capture_path), "%s/capture.out", aux_dir); + write_text_file(script_path, script); + + pid = fork(); + TEST_ASSERT_TRUE_MESSAGE(pid >= 0, "fork() must succeed"); + + if (pid == 0) { + child_run_repl_and_exit(replay_path, script_path, capture_path); + _exit(112); /* unreachable -- child_run_repl_and_exit never returns */ + } + + TEST_ASSERT_EQUAL_INT_MESSAGE(pid, waitpid(pid, &status, 0), + "waitpid must reap the REPL child"); + TEST_ASSERT_TRUE_MESSAGE(WIFEXITED(status), + "the REPL child must exit normally via quit's " + "exit(0)"); + TEST_ASSERT_EQUAL_INT(0, WEXITSTATUS(status)); + + return read_whole_file(capture_path); +} + +/* run_sim_and_capture_stdout - the same dup()/dup2()-around-STDOUT_FILENO, + * NO-FORK capture technique test_sim_event_loop.c's own + * run_sim_and_capture_stdout() establishes: args.interactive is left at its + * memset(0) default (false), so sched_sim_driver_run() takes the batch + * (non-interactive, headless) path -- no REPL command loop, no stdin/ + * readline involved at all, matching Requirement 5.4's "non-interactive + * (headless) replay's decision output is unaffected" scope exactly, and the + * `test_sim_*.c` pattern this task's dispatch instructions point at. `sim` + * is `static` for the same never-joined-detached-picker-thread reason + * test_sim_event_loop.c's own copy documents. */ +static char* run_sim_and_capture_stdout(const char* replay_path, int* out_rc) +{ + sched_sim_args_t args; + static xce_sched_sim_driver_t sim; + char tmp[] = "/tmp/xce_pdgate_run_XXXXXX"; + int fd = mkstemp(tmp); + int saved_stdout; + off_t len; + char* buf; + + TEST_ASSERT_TRUE_MESSAGE(fd >= 0, "mkstemp for stdout capture must " + "succeed"); + + fflush(stdout); + saved_stdout = dup(STDOUT_FILENO); + TEST_ASSERT_TRUE_MESSAGE(saved_stdout >= 0, "dup(STDOUT_FILENO) must " + "succeed"); + TEST_ASSERT_TRUE_MESSAGE(dup2(fd, STDOUT_FILENO) >= 0, + "dup2 redirect of STDOUT_FILENO must succeed"); + + memset(&args, 0, sizeof(args)); + args.replay_path = replay_path; + args.sim_start_ts = SCHED_SIM_START_TS_LATEST; + args.sim_speed = 0; /* frozen -- deterministic, no wall-clock wait */ + /* args.interactive left false -- this is the headless/batch path. */ + + *out_rc = sched_sim_driver_run(&sim, &args); + + fflush(stdout); + dup2(saved_stdout, STDOUT_FILENO); + close(saved_stdout); + + len = lseek(fd, 0, SEEK_CUR); + TEST_ASSERT_TRUE(len >= 0); + + buf = (char*)calloc(1, (size_t)len + 1); + TEST_ASSERT_NOT_NULL(buf); + TEST_ASSERT_TRUE(lseek(fd, 0, SEEK_SET) == 0); + if (len > 0) { + ssize_t nread = read(fd, buf, (size_t)len); + TEST_ASSERT_TRUE(nread == len); + } + close(fd); + unlink(tmp); + + return buf; +} + +/* ---------------------------------------------------------------------- */ +/* Test 1: under `continue` mode, a round with >=1 decision must NOT print */ +/* a bare per-decision line -- the Round Rollup's own summary line is the */ +/* sole source of decision output in this mode (Requirement 2.3). THIS IS */ +/* THE TEST EXPECTED TO FAIL TODAY (see header comment): sim_loop.c's */ +/* decision-drain loop still calls sim_print_decision(d) unconditionally, */ +/* so today's real captured output contains BOTH the bare line AND the */ +/* Round Rollup's summary -- exactly the duplication bug-1610 reported. */ +/* ---------------------------------------------------------------------- */ + +static void +test_continue_mode_produces_no_bare_decision_line_outside_round_summary( + void) +{ + char replay_path[PATH_MAX]; + FILE* f; + char* captured; + char jobid_x[64]; + char bare_pend_marker[96]; + + f = write_header_and_snapshot(replay_path, sizeof(replay_path), + "_pdgatecontinue"); + TEST_ASSERT_NOT_NULL(f); + + append_queue_update_event(f, 1000ULL, "normal"); + append_jobs_push_event(f, 1500ULL, 6101ULL, "normal"); + append_replay_session(f, 2000000000ULL); /* round 1: 1 pend (X) */ + append_end_of_stream_marker(f); + fclose(f); + + captured = run_repl_fixture_and_capture(replay_path, "continue\nquit\n", + "_pdgatecontinueaux"); + + fmt_jobid(jobid_x, sizeof(jobid_x), 6101ULL); + snprintf(bare_pend_marker, sizeof(bare_pend_marker), "[sim] PEND jobid=%s", + jobid_x); + + /* Sanity check (already true today, per task 2.2): the Round Rollup's + * own summary line for this round IS present -- proves the fixture + * actually produced a decision and that the round summary path is the + * one under test here, not a fixture-setup mistake. */ + TEST_ASSERT_NOT_NULL_MESSAGE( + strstr(captured, "1 pend"), + "sanity check: the Round Rollup's own per-round summary must " + "report this round's decision (already implemented by task 2.2)"); + + /* THE ASSERTION EXPECTED TO FAIL TODAY: sim_print_decision()'s bare + * per-decision line must no longer appear outside the Round Rollup's + * own summary while executing in continue mode (Requirement 2.3) -- + * today it still does, duplicating the information bug-1610 + * originally reported. */ + TEST_ASSERT_NULL_MESSAGE( + strstr(captured, bare_pend_marker), + "under continue mode, sim_print_decision()'s bare per-decision " + "line must NOT appear outside the Round Rollup's own summary -- " + "the round summary is the sole source of decision output in this " + "mode (Requirement 2.3); today's unconditional echo still prints " + "it, duplicating bug-1610's originally-reported information"); + + free(captured); + + printf( + "test_continue_mode_produces_no_bare_decision_line_outside_round_" + "summary: PASS\n"); +} + +/* ---------------------------------------------------------------------- */ +/* Test 2: while single-stepping, the bare per-decision line still prints */ +/* exactly as before -- full detail, unaffected by this feature's eventual */ +/* continue-mode change (Requirement 5.1). Regression-safety net: this */ +/* test already passes today and must stay green through task 4.2. */ +/* ---------------------------------------------------------------------- */ + +static void test_step_mode_still_prints_full_bare_decision_line(void) +{ + char replay_path[PATH_MAX]; + FILE* f; + char* captured; + char jobid_x[64]; + char bare_pend_marker[96]; + + f = write_header_and_snapshot(replay_path, sizeof(replay_path), + "_pdgatestep"); + TEST_ASSERT_NOT_NULL(f); + + append_queue_update_event(f, 1000ULL, "normal"); + append_jobs_push_event(f, 1500ULL, 6102ULL, "normal"); + append_replay_session(f, 2000000000ULL); /* round 1: 1 pend (X) */ + append_end_of_stream_marker(f); + fclose(f); + + captured = run_repl_fixture_and_capture(replay_path, "step\nquit\n", + "_pdgatestepaux"); + + fmt_jobid(jobid_x, sizeof(jobid_x), 6102ULL); + snprintf(bare_pend_marker, sizeof(bare_pend_marker), "[sim] PEND jobid=%s", + jobid_x); + + TEST_ASSERT_NOT_NULL_MESSAGE( + strstr(captured, bare_pend_marker), + "while single-stepping, sim_print_decision()'s bare per-decision " + "line must still print exactly as before -- full detail, " + "completely unaffected by this feature's continue-mode change " + "(Requirement 5.1)"); + + free(captured); + + printf("test_step_mode_still_prints_full_bare_decision_line: PASS\n"); +} + +/* ---------------------------------------------------------------------- */ +/* Test 3: a non-interactive (headless, no -i/--interactive) batch replay */ +/* run's decision output is completely unaffected -- this whole feature */ +/* (including this specific gate) only ever applies when sim->interactive */ +/* is true (Requirement 5.4). No REPL/readline/prompt involved at all -- */ +/* same direct-call, no-fork pattern test_sim_event_loop.c's own */ +/* run_sim_and_capture_stdout() establishes for headless `--sim` runs. */ +/* ---------------------------------------------------------------------- */ + +static void +test_headless_noninteractive_batch_run_decision_output_unaffected(void) +{ + char replay_path[PATH_MAX]; + FILE* f; + char* captured; + char jobid_x[64]; + char bare_pend_marker[96]; + int rc; + + f = write_header_and_snapshot(replay_path, sizeof(replay_path), + "_pdgateheadless"); + TEST_ASSERT_NOT_NULL(f); + + append_queue_update_event(f, 1000ULL, "normal"); + append_jobs_push_event(f, 1500ULL, 6103ULL, "normal"); + append_replay_session(f, 2000000000ULL); /* round 1: 1 pend (X) */ + append_end_of_stream_marker(f); + fclose(f); + + captured = run_sim_and_capture_stdout(replay_path, &rc); + + fmt_jobid(jobid_x, sizeof(jobid_x), 6103ULL); + snprintf(bare_pend_marker, sizeof(bare_pend_marker), "[sim] PEND jobid=%s", + jobid_x); + + TEST_ASSERT_EQUAL_INT(0, rc); + TEST_ASSERT_NOT_NULL_MESSAGE( + strstr(captured, bare_pend_marker), + "a non-interactive (headless) batch replay run's bare per-decision " + "line must print exactly as today -- this feature's gate only " + "ever applies when sim->interactive is true (Requirement 5.4)"); + + free(captured); + + printf( + "test_headless_noninteractive_batch_run_decision_output_" + "unaffected: PASS\n"); +} + +int main(void) +{ + UNITY_BEGIN(); + + RUN_TEST( + test_continue_mode_produces_no_bare_decision_line_outside_round_summary); + RUN_TEST(test_step_mode_still_prints_full_bare_decision_line); + RUN_TEST( + test_headless_noninteractive_batch_run_decision_output_unaffected); + + return UNITY_END(); +}