Files
openclaw/test/e2e/qa-lab/runtime/heartbeat-active-hours-runtime.ts
Peter Steinberger 82d1a03f25 refactor(agents): move implicit-main fallback into load-time roster injection (#112678)
* refactor(agents): require explicit roster defaults

* feat(onboard): create named first roster agent

* refactor(agents): remove runtime main fallbacks

* style(agents): apply roster refactor formatting

* refactor(agents): finish roster-only runtime sweep

* fix(doctor): migrate legacy main session sqlite

* fix(doctor): harden roster session migrations

* fix(onboard): commit first agent atomically

* fix(config): support empty-roster analysis

* fix(agents): preserve legacy main state during creation

* fix(setup): materialize baseline agent roster

* fix(agents): harden legacy default transfer recovery

* fix(agents): simplify roster-only legacy compatibility

* fix(agents): preserve staged first-agent entries

* fix(config): migrate persisted implicit-main rosters

* fix(config): preserve staged empty rosters

* fix(agents): finalize roster-only upgrade paths

* fix(sessions): close legacy main migration outcomes

* fix(config): migrate legacy roster markers at load

* fix(sessions): preserve roster upgrade history

* refactor(sessions): restore lean legacy main compatibility

* fix(setup): prepare first-agent credentials before publish

* fix(config): stabilize roster snapshot migration

* refactor(sessions): shrink legacy main compatibility

* fix(agents): restore roster compatibility fidelity

* fix(sessions): preserve divergent legacy history

* refactor(agents): narrow roster-only scope

* fix(config): isolate roster migration

* test(agents): align roster-only fixtures

* fix(agents): keep main agent undeletable

* fix(agents): harden roster migration invariants

* fix(agents): close setup and audit scope gaps

* fix(cron): scope session reaper throttles by agent

* fix(agents): preserve scoped owner precedence

* fix(config): preserve authored config ownership

* fix(setup): keep default workspace and roster in sync

* fix(setup): preserve default entry workspace on bare runs

* fix(agents): adapt roster rebase to keyed entries

* fix(agents): honor both roster representations

* fix(agents): route roster reads through shared helpers

* fix(config): preserve canonical roster writes

* fix(cron): resolve dynamic default for session reaper

* fix(agents): close dynamic default migration gaps

* fix(agents): align scoped session ownership

* fix(sessions): preserve legacy main directory casing

* fix(agents): align cron and legacy auth ownership

* fix(setup): provision the committed default workspace

* fix(cron): align scoped ownership and reaping

* fix(cron): treat blank agent ids as absent

* fix(cron): retain configured session-store owners

* fix(agents): repair roster-aware CI boundaries

* fix(cron): preserve scoped ownership resolution

* fix(agents): preserve rosterless maintenance paths

* fix(agents): propagate roster ownership through runtime boundaries

* fix(agents): preserve roster ownership across runtime paths

* fix(agents): harden roster diagnostics and legacy routing

* fix(agents): remove redundant diagnostic import

* test(agents): type CLI policy fixture explicitly

* fix(config): preserve canonical roster mutation identity

* fix(doctor): read canonical agent rosters consistently

* fix(config): resolve compound roster unsets safely

* fix(config): finalize main-session reconciliation

* fix(doctor): read canonical session state safely

* fix(sessions): preserve current visibility alias

* fix(config): track roster include provenance

* test(config): type roster provenance cases

* fix(config): refine roster include ownership

* fix(agents): preserve staged roster invariants

* test(config): align fixtures with explicit roster ownership

* test(node-host): preserve optional plan typing

* fix(config): preserve authored roster projections

* test(config): keep raw roster fixtures explicit

* test(config): normalize rosters at runtime fixtures

* fix(config): protect authored roster ownership

* fix(agents): require explicit session ownership

* fix(agents): enforce scoped roster ownership

* fix(sessions): merge fixed-store agent partitions

* fix(agents): harden roster ownership boundaries

* fix(config): reject ambiguous roster projections

* fix(sessions): preserve persisted store ownership

* fix(sessions): keep collision diagnostics additive

* fix(security): scan malformed roster workspaces

* test(config): align snapshot fixtures after rebase

* test(agents): use explicit roster fixtures

* fix(config): harden roster diagnostic boundaries

* fix(sessions): isolate fixed-store agent databases

* test(agents): type malformed default markers

* refactor(sessions): extract store collision resolution

* test(system-agent): split oversized setup coverage

* style(system-agent): format split setup suite

* fix(sessions): preserve promoted store ownership

* fix(sessions): derive scoped owner before target

* fix(sessions): preserve explicit sqlite ownership

* fix(agents): restore roster compatibility across CI

* fix(agents): enforce roster-owned runtime boundaries

* fix(agents): satisfy default lookup lint

* test(sessions): split known-owner coverage

* fix(state): satisfy path identity lint

* fix(agents): preserve malformed roster safety boundaries

* fix(agents): restore roster compatibility at runtime boundaries

* fix(config): satisfy roster boundary type checks

* fix(agents): preserve roster ownership across runtime probes

Setup inference probes now execute as the configured roster owner. Malformed agent-prefixed session rows are intentionally omitted by the fail-closed visibility contract rather than normalized by tests.

* fix(agents): satisfy session list owner lint

* fix(agents): preserve roster-owned runtime boundaries

Restore shared logical rows for exact SQLite session locators while keeping their physical database owner separate. The ownership regression test now constructs an explicit sole-owner database directly instead of relying on first-touch capture, matching the intentional shared-store contract.

* fix(sessions): preserve multiply owned exact stores

* fix(sessions): restore runtime owner boundaries

Keep incognito sentinels agent-owned, fold default-agent approvals into the global snapshot, and preserve the configless legacy-main CLI policy fallback. Also repair the existing CLI watchdog test lifecycle so the compact shard observes its timeout without an unawaited assertion or async timer stall; product behavior is unchanged by that test-only fix.

* test(ci): align owner-scoped fixtures

These assertions are unchanged. The fixtures now declare the intended non-default runner, expose the session-key constant imported by production status code, and select the main approvals bucket explicitly on Windows.

* fix(agents): close final roster ownership gaps
2026-07-24 22:38:09 -07:00

212 lines
7.0 KiB
TypeScript

// Heartbeat active-hours evidence runs the real wake-lane guards and reload path.
// Interval cadence itself is covered by the system cron monitor integration tests.
import fs from "node:fs/promises";
import path from "node:path";
import { pathToFileURL } from "node:url";
import type { OpenClawConfig } from "../../../../src/config/types.openclaw.js";
import { formatErrorMessage } from "../../../../src/infra/errors.js";
import { isWithinActiveHours } from "../../../../src/infra/heartbeat-active-hours.js";
import { startHeartbeatRunner } from "../../../../src/infra/heartbeat-runner.js";
import { requestHeartbeat } from "../../../../src/infra/heartbeat-wake.js";
import { createQaScriptEvidenceWriter } from "./script-evidence.js";
const DEFAULT_TIMEOUT_MS = 5_000;
const HEARTBEAT_INTERVAL_MS = 100;
const HEARTBEAT_INTERVAL = `${HEARTBEAT_INTERVAL_MS}ms`;
type HeartbeatRuntimeOptions = {
artifactBase: string;
repoRoot: string;
timeoutMs: number;
};
type SchedulerObservation = {
at: string;
outcome: "active-fire" | "quiet-hours-skip";
};
function parseOptions(argv: string[], repoRoot = process.cwd()): HeartbeatRuntimeOptions {
let artifactBase = path.join(repoRoot, ".artifacts", "qa-e2e", "heartbeat-active-hours");
let timeoutMs = DEFAULT_TIMEOUT_MS;
for (let index = 0; index < argv.length; index += 1) {
const arg = argv[index];
if (arg === "--output-dir") {
artifactBase = path.resolve(repoRoot, argv[++index] ?? "");
continue;
}
if (arg === "--timeout-ms") {
timeoutMs = Number(argv[++index]);
continue;
}
if (arg === "--") {
continue;
}
throw new Error(`Unknown argument: ${arg}`);
}
if (!Number.isFinite(timeoutMs) || timeoutMs <= 0) {
throw new Error("--timeout-ms must be a positive number");
}
return { artifactBase, repoRoot, timeoutMs };
}
function heartbeatConfig(quietHours: boolean): OpenClawConfig {
return {
agents: {
entries: { main: { default: true } },
defaults: {
heartbeat: {
activeHours: quietHours
? { start: "00:00", end: "00:00", timezone: "UTC" }
: { start: "00:00", end: "24:00", timezone: "UTC" },
every: HEARTBEAT_INTERVAL,
target: "none",
},
},
},
};
}
async function waitForObservation(
observations: SchedulerObservation[],
outcome: SchedulerObservation["outcome"],
afterCount: number,
timeoutMs: number,
) {
const deadline = Date.now() + timeoutMs;
while (Date.now() < deadline) {
if (observations.slice(afterCount).some((entry) => entry.outcome === outcome)) {
return;
}
await new Promise((resolve) => {
setTimeout(resolve, 20);
});
}
throw new Error(`heartbeat wake lane did not observe ${outcome} within ${timeoutMs}ms`);
}
async function pokeScheduledHeartbeat(params: {
observations: SchedulerObservation[];
outcome: SchedulerObservation["outcome"];
afterCount: number;
timeoutMs: number;
}) {
// The cron monitor fires at or after the configured due slot. Wait past one
// interval so the runner's cooldown gate admits the equivalent scheduled poke.
await new Promise((resolve) => {
setTimeout(resolve, HEARTBEAT_INTERVAL_MS + 50);
});
requestHeartbeat({
source: "interval",
intent: "scheduled",
reason: "interval",
coalesceMs: 0,
});
await waitForObservation(
params.observations,
params.outcome,
params.afterCount,
params.timeoutMs,
);
}
function createWriter(options: HeartbeatRuntimeOptions) {
return createQaScriptEvidenceWriter({
artifactBase: options.artifactBase,
logFileName: "heartbeat-active-hours.log",
primaryModel: "heartbeat/scheduler",
providerMode: "mock-openai",
repoRoot: options.repoRoot,
target: {
id: "heartbeat-active-hours",
title: "Heartbeat active-hours scheduler",
sourcePath: "test/e2e/qa-lab/runtime/heartbeat-active-hours-runtime.ts",
docsRefs: ["docs/gateway/heartbeat.md"],
codeRefs: [
"test/e2e/qa-lab/runtime/heartbeat-active-hours-runtime.ts",
"src/infra/heartbeat-runner.ts",
"src/infra/heartbeat-active-hours.ts",
],
},
});
}
export async function runHeartbeatActiveHoursRuntime(options: HeartbeatRuntimeOptions) {
await fs.mkdir(options.artifactBase, { recursive: true });
const writer = createWriter(options);
const startedAt = Date.now();
const observations: SchedulerObservation[] = [];
let currentConfig = heartbeatConfig(false);
const runner = startHeartbeatRunner({
cfg: currentConfig,
readCurrentConfig: () => currentConfig,
runOnce: async ({ cfg, heartbeat }) => {
const active = isWithinActiveHours(cfg!, heartbeat);
const outcome = active ? "active-fire" : "quiet-hours-skip";
observations.push({ at: new Date().toISOString(), outcome });
writer.appendLog(`heartbeat-active-hours: ${outcome}\n`);
return active
? { status: "ran", durationMs: 1 }
: { status: "skipped", reason: "quiet-hours" };
},
stableSchedulerSeed: "qa-heartbeat-active-hours",
});
try {
await pokeScheduledHeartbeat({
observations,
outcome: "active-fire",
afterCount: 0,
timeoutMs: options.timeoutMs,
});
const beforeQuiet = observations.length;
currentConfig = heartbeatConfig(true);
runner.updateConfig(currentConfig);
await pokeScheduledHeartbeat({
observations,
outcome: "quiet-hours-skip",
afterCount: beforeQuiet,
timeoutMs: options.timeoutMs,
});
const beforeReload = observations.length;
currentConfig = heartbeatConfig(false);
runner.updateConfig(currentConfig);
await pokeScheduledHeartbeat({
observations,
outcome: "active-fire",
afterCount: beforeReload,
timeoutMs: options.timeoutMs,
});
const summaryPath = path.join(options.artifactBase, "heartbeat-active-hours-summary.json");
await fs.writeFile(summaryPath, `${JSON.stringify({ observations }, null, 2)}\n`, "utf8");
return await writer.write({
artifacts: [{ kind: "summary", filePath: summaryPath }],
details: "Observed active fire, quiet-hours skip, and active-hours reload fire",
durationMs: Math.max(1, Date.now() - startedAt),
status: "pass",
});
} catch (error) {
const details = formatErrorMessage(error);
writer.appendLog(`heartbeat-active-hours: ${details}\n`);
return await writer.write({
details,
durationMs: Math.max(1, Date.now() - startedAt),
status: "fail",
});
} finally {
runner.stop();
}
}
if (import.meta.url === pathToFileURL(process.argv[1] ?? "").href) {
runHeartbeatActiveHoursRuntime(parseOptions(process.argv.slice(2)))
.then((evidence) => {
const status = evidence.entries[0]?.result.status;
process.stdout.write(`heartbeat-active-hours: ${status}\n`);
process.exitCode = status === "pass" ? 0 : 1;
})
.catch((error: unknown) => {
process.stderr.write(`heartbeat-active-hours: ${formatErrorMessage(error)}\n`);
process.exitCode = 1;
});
}