Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
8 changes: 6 additions & 2 deletions capsules/assistant/browser/agent-stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -2073,13 +2073,17 @@ async function startLiveTurnForPrompt(userText, { resumeTurn = null } = {}) {
lastScrollTop = scroller.scrollTop;
lastMaxTop = scroller.scrollHeight - scroller.clientHeight;
};
/* Layout can clamp a saved pin when the scroll range shrinks. A reader
above that reachable position has moved up; a clamp alone keeps follow. */
const stayedAtPin = (top) =>
scroller.scrollTop >= Math.min(top, scroller.scrollHeight - scroller.clientHeight) - 1;
/* Land on the end now and once more after layout settles, unless the reader
moved up in between or another turn took the transcript. */
const pinToEnd = () => {
scroller.scrollTop = scroller.scrollHeight;
const top = scroller.scrollTop;
window.requestAnimationFrame(() => {
if (generation === ctx.streamGeneration && scroller.scrollTop >= top - 1) {
if (generation === ctx.streamGeneration && stayedAtPin(top)) {
scroller.scrollTop = scroller.scrollHeight;
if (endScrollTop === top) {
endScrollTop = scroller.scrollTop;
Expand Down Expand Up @@ -2928,7 +2932,7 @@ async function startLiveTurnForPrompt(userText, { resumeTurn = null } = {}) {
/* The last flush and the rich render below make the reply taller; keep
a reader who was following at the end. One who scrolled up since the
stream ended (its listeners are gone) stays where they are. */
const pinned = endScrollTop !== null && !!scroller && scroller.scrollTop >= endScrollTop - 1;
const pinned = endScrollTop !== null && !!scroller && stayedAtPin(endScrollTop);
/* Streaming painted plain text (live tokens never run Markdown, see
adaptQuality). The finished reply, stopped early or not, now gets the
render a reopened chat gives it: the whole text through the history
Expand Down
31 changes: 30 additions & 1 deletion scripts/assistant-reply-layout-browser-smoke.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,8 @@ try {
server.listen(0, "127.0.0.1"); await once(server, "listening");
browser = await chromium.launch({ headless: true, ...(process.env.BRAVE_BIN ? { executablePath: process.env.BRAVE_BIN } : {}) });
page = await browser.newPage({ viewport: { width: 1280, height: 800 } });
const cdp = await page.context().newCDPSession(page);
await cdp.send("Emulation.setCPUThrottlingRate", { rate: 4 });
page.on("pageerror", error => pageErrors.push(`${stage}: ${error}`));
await page.goto(`http://127.0.0.1:${server.address().port}/`);
const room = page.frameLocator("iframe").frameLocator("iframe");
Expand Down Expand Up @@ -224,7 +226,17 @@ try {
seen.status = await expectNewestAboveComposer(".agent-progress", "the status line");
await typeLongFollowUp();
seen.statusTyping = await expectNewestAboveComposer(".agent-progress", "the status line, while a follow-up is typed");
await input.fill("");

// Let layout clamp the scroll position between the stream's final pin
// and its idle Markdown render. A layout clamp is not a reader scrolling up.
await frame.evaluate(() => {
const scheduleIdle = window.requestIdleCallback;
window.requestIdleCallback = (callback, options) => {
window.requestIdleCallback = scheduleIdle;
window.__finishReply = () => scheduleIdle(callback, options);
return 0;
};
});

stage = "streamed reply";
fixture.events = [
Expand All @@ -233,7 +245,24 @@ try {
];
await room.locator(".agent-msg-agent.is-streaming").waitFor({ state: "detached", timeout: 15000 });
await room.locator(".agent-msg-agent").last().filter({ hasText: "Streamed step 25" }).waitFor({ timeout: 5000 });
await frame.waitForFunction(() => typeof window.__finishReply === "function", null, { timeout: 5000 });
const beforeShrink = await frame.evaluate(() => document.querySelector("#agent-harness-stream").scrollTop);
await page.setViewportSize({ width: 1280, height: 1600 });
await input.fill("");
await frame.waitForFunction((top) => {
const scroller = document.querySelector("#agent-harness-stream");
return scroller.scrollTop < top - 1 && scroller.scrollHeight - scroller.scrollTop - scroller.clientHeight <= 1;
}, beforeShrink, { timeout: 5000 });
await frame.evaluate(() => new Promise(done => requestAnimationFrame(() => requestAnimationFrame(done))));
await frame.evaluate(() => { window.__finishReply(); delete window.__finishReply; });
await room.locator('.agent-msg-agent[data-stream-phase="presentation_done"]').last().waitFor({ timeout: 5000 });
seen.reply = await expectNewestAboveComposer(".agent-msg-agent", "the newest reply");
await cdp.send("Emulation.setCPUThrottlingRate", { rate: 1 });
await page.setViewportSize({ width: 1280, height: 800 });
await frame.evaluate(() => {
const scroller = document.querySelector("#agent-harness-stream");
scroller.scrollTop = scroller.scrollHeight;
});
await typeLongFollowUp();
seen.replyTyping = await expectNewestAboveComposer(".agent-msg-agent", "the newest reply, while a follow-up is typed");

Expand Down
95 changes: 93 additions & 2 deletions scripts/ci-installed-journeys.py
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,7 @@ def __init__(self, home, data, address, environment, gib, platform=sys.platform)
self.stop = threading.Event()
self.error = None
self.failure = None
self.census_gap = None
self.thread = None
self.outside = [os.getpid()]
self.engine = None
Expand Down Expand Up @@ -490,6 +491,87 @@ def events(self):
def poll(self):
return 1 if self.error or self.pid in self.fds and pidfd_exited(self.fds[self.pid]) else None

def record_census_gap(self, pid, fd):
"""Capture the failed sample before cleanup, without changing its verdict."""
if self.census_gap is not None:
return
observed = {"pid": pid, "sampled_at": time.monotonic()}
directory = Path("/proc") / str(pid)
for name, limit in (("stat", 4096), ("cmdline", 8192), ("cgroup", 4096)):
try:
if name == "stat":
text = bounded_text(directory / name, limit)
fields = text[text.rfind(")") + 2:].split()
observed.update(ppid=int(fields[1]), start_ticks=int(fields[19]), state=fields[0])
elif name == "cmdline":
text = bounded_text(directory / name, limit).rstrip("\0")
observed[name] = text.split("\0") if text else []
else:
observed[name] = cgroup_membership(pid)
except (OSError, RuntimeError, ValueError, IndexError) as error:
observed[name + "_error"] = PUBLIC_ERROR_TYPES.get(type(error), "redacted")
retained = self.known.get(pid, {})
detail = {"retained": retained.get("census_sample", {}), "observed": observed,
"role": retained.get("role"), "exe_sha256": retained.get("exe_sha256"),
"expected_cgroup": self.binding["ControlGroup"]}
try:
pids = [int(value) for value in bounded_text(self.group / "cgroup.procs", 8192).split()]
if len(pids) > 512:
raise RuntimeError("finite journey process census exceeds its bound")
detail["expected_cgroup_procs"] = pids
except (OSError, RuntimeError, ValueError) as error:
detail["expected_cgroup_procs_error"] = PUBLIC_ERROR_TYPES.get(type(error), "redacted")
parent_pid = observed.get("ppid", detail["retained"].get("ppid", retained.get("parent")))
parent = {"pid": parent_pid}
if parent_pid is not None and parent_pid > 0:
try:
if parent_pid in self.fds:
parent["pidfd_exited"] = pidfd_exited(self.fds[parent_pid])
else:
parent_fd = os.pidfd_open(parent_pid, 0)
try:
parent["pidfd_exited"] = pidfd_exited(parent_fd)
finally:
os.close(parent_fd)
except (OSError, RuntimeError) as error:
parent["pidfd_error"] = PUBLIC_ERROR_TYPES.get(type(error), "redacted")
try:
parent["cgroup"] = cgroup_membership(parent_pid)
except (OSError, RuntimeError, ValueError) as error:
parent["cgroup_error"] = PUBLIC_ERROR_TYPES.get(type(error), "redacted")
detail["parent"] = parent
try:
detail["pidfd_exited"] = pidfd_exited(fd)
except (OSError, RuntimeError) as error:
detail["pidfd_error"] = PUBLIC_ERROR_TYPES.get(type(error), "redacted")
detail["same_start_ticks"] = (observed["start_ticks"] == retained["start_ticks"]
if "start_ticks" in observed and "start_ticks" in retained else None)
# Raw argv and non-unit cgroup names stay with the private Runtime log.
# CI uploads JSON and PNG only; this file deliberately uses .private.log.
try:
private_json(self.control / "census-gap.private.log", detail)
detail["private_capture"] = "saved"
except (OSError, RuntimeError) as error:
detail["private_capture"] = PUBLIC_ERROR_TYPES.get(type(error), "redacted")
for key in ("retained", "observed", "parent"):
sample = dict(detail[key])
if "cmdline" in sample:
# Only the bound executable's name and fixed harness verbs leave
# the runner. Arbitrary positional values can contain credentials.
executable = Path(retained.get("executable", "")).name
sample["cmdline"] = [executable if index == 0 and Path(arg).name == executable else
arg if arg in ("gateway", "init", "daemon", "--internal-local-llama-guard")
else "<redacted>" for index, arg in enumerate(sample["cmdline"])]
if "cgroup" in sample and sample["cgroup"] != self.binding["ControlGroup"]:
sample["cgroup_sha256"] = hashlib.sha256(sample["cgroup"].encode()).hexdigest()
sample["cgroup"] = "/" + "/".join(
part if re.fullmatch(r"[A-Za-z0-9_.@-]{1,64}\.(slice|scope|service|mount|socket)", part)
else "<name>" for part in sample["cgroup"].split("/")[1:])
if key == "retained":
sample.update(role=retained.get("role"), exe_sha256=retained.get("exe_sha256"))
detail[key] = sample
self.census_gap = detail

def observe_once(self, require_runtime=True):
if (self.metric("memory.max") != str(self.budget) or self.metric("memory.swap.max") != "0"
or self.metric("pids.max") != "512"):
Expand Down Expand Up @@ -585,9 +667,16 @@ def observe_once(self, require_runtime=True):
"model_provider" if executable == (self.data / "bin/model-provider").resolve() else
"llama_server" if executable.name == "llama-server" else "other")
self.known[pid] = identity
self.known[pid]["census_sample"] = {
"pid": pid, "ppid": identity["parent"], "start_ticks": identity["start_ticks"],
"state": identity["state"], "cmdline": command, "cgroup": membership,
"sampled_at": time.monotonic(),
}
current[pid] = self.known[pid]
if any(pid not in current and not pidfd_exited(fd) for pid, fd in self.fds.items()):
raise RuntimeError("finite journey live child left its unit census")
for pid, fd in self.fds.items():
if pid not in current and not pidfd_exited(fd):
self.record_census_gap(pid, fd)
raise RuntimeError("finite journey live child left its unit census")
if require_runtime and self.pid not in current:
raise RuntimeError("finite journey Runtime left its unit")
memory = [self.metric(name) for name in ("memory.current", "memory.peak")]
Expand Down Expand Up @@ -708,6 +797,8 @@ def finish(self):
"actions": self.actions, "outside_runtime_unit": True}
if self.failure:
record["observer_failure"] = self.failure
if self.census_gap:
record["census_gap"] = self.census_gap
if runtime_exited:
record["runtime_exited_before_cleanup"] = True
phase = "unit_binding"
Expand Down
Loading
Loading