Skip to content

A self-improving RLM agent for coding workflows and long-running autonomous tasks.

Rust

fix(kernel): disclose cwd and environment reset after restore (#3525)

Sebastian Müllere69265e7aa4e
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
index a5bd2f6be6..3e6943f64b 100644
--- a/.github/workflows/ci.yml
+++ b/.github/workflows/ci.yml
@@ -348,7 +348,7 @@ jobs:
# and only synthetic loopback providers/state. One leg avoids 8 downloads.
- name: Compare the lifecycle binaries (pinned TS reference)
id: lifecycle_parity
- if: matrix.shard == 1 && steps.selection.outputs.units != '0' && (hashFiles('scripts/review_graceful_shutdown_parity.py') != '' || hashFiles('scripts/review_goal_backoff_parity.py') != '' || hashFiles('scripts/review_draft_disconnect_parity.py') != '')
+ if: matrix.shard == 1 && steps.selection.outputs.units != '0' && (hashFiles('scripts/review_graceful_shutdown_parity.py') != '' || hashFiles('scripts/review_goal_backoff_parity.py') != '' || hashFiles('scripts/review_draft_disconnect_parity.py') != '' || hashFiles('scripts/review_restore_notice_parity.py') != '')
env:
GH_TOKEN: ${{ github.token }}
run: |
@@ -371,7 +371,7 @@ jobs:
test -x "${PRIME_AGENT_KERNEL_PYTHON}"
mkdir -p "${RUNNER_TEMP}/lifecycle-parity-receipts"
comparison_status=0
- for scenario in graceful_shutdown goal_backoff draft_disconnect; do
+ for scenario in graceful_shutdown goal_backoff draft_disconnect restore_notice; do
verifier="scripts/review_${scenario}_parity.py"
if [ -f "${verifier}" ]; then
if [ "${scenario}" = "draft_disconnect" ]; then
@@ -382,7 +382,7 @@ jobs:
done
exit "${comparison_status}"
- name: Upload the binary comparison receipts
- if: always() && matrix.shard == 1 && steps.selection.outputs.units != '0' && (hashFiles('scripts/review_graceful_shutdown_parity.py') != '' || hashFiles('scripts/review_goal_backoff_parity.py') != '' || hashFiles('scripts/review_draft_disconnect_parity.py') != '')
+ if: always() && matrix.shard == 1 && steps.selection.outputs.units != '0' && (hashFiles('scripts/review_graceful_shutdown_parity.py') != '' || hashFiles('scripts/review_goal_backoff_parity.py') != '' || hashFiles('scripts/review_draft_disconnect_parity.py') != '' || hashFiles('scripts/review_restore_notice_parity.py') != '')
uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4.6.2
with:
name: lifecycle-parity-receipts-attempt-${{ github.run_attempt }}
diff --git a/crates/pa-core/src/session_engine/state_restore_notice.rs b/crates/pa-core/src/session_engine/state_restore_notice.rs
index 8dbcba3cd6..cac4544142 100644
--- a/crates/pa-core/src/session_engine/state_restore_notice.rs
+++ b/crates/pa-core/src/session_engine/state_restore_notice.rs
@@ -17,7 +17,8 @@ fn now_millis() -> u64 {
}
/// The notice text: the `[python-state-restored]` header, the
-/// revived-or-fresh line, and the failed-names disclosure.
+/// revived-or-fresh line, the failed-names disclosure, and the env/cwd
+/// reset line (a fresh kernel revives neither).
#[must_use]
pub fn notice_content(result: &RestoreResult) -> String {
let mut lines = vec!["[python-state-restored]".to_string(), String::new()];
@@ -42,6 +43,9 @@ pub fn notice_content(result: &RestoreResult) -> String {
.join(", ")
));
}
+ lines.push(
+ "The working directory and environment variables were reset to the session defaults; re-apply any os.chdir(...) or os.environ[...] changes you still need.".to_string(),
+ );
lines.join("\n")
}
@@ -94,7 +98,7 @@ mod tests {
};
assert_eq!(
content,
- "[python-state-restored]\n\nYour Python kernel state was revived from your previous session. These names are available again: data, helper."
+ "[python-state-restored]\n\nYour Python kernel state was revived from your previous session. These names are available again: data, helper.\nThe working directory and environment variables were reset to the session defaults; re-apply any os.chdir(...) or os.environ[...] changes you still need."
);
}
@@ -110,7 +114,7 @@ mod tests {
};
assert_eq!(
content,
- "[python-state-restored]\n\nYour previous Python kernel state could not be revived; the kernel is starting fresh, so re-create any variables, imports, or loaded data you need."
+ "[python-state-restored]\n\nYour previous Python kernel state could not be revived; the kernel is starting fresh, so re-create any variables, imports, or loaded data you need.\nThe working directory and environment variables were reset to the session defaults; re-apply any os.chdir(...) or os.environ[...] changes you still need."
);
}
@@ -121,7 +125,7 @@ mod tests {
panic!("text content");
};
assert!(
- content.ends_with("These could not be restored and must be recreated if needed: sock.")
+ content.contains("These could not be restored and must be recreated if needed: sock.")
);
}
}
diff --git a/crates/pa-core/tests/kernel_snapshot_resume.rs b/crates/pa-core/tests/kernel_snapshot_resume.rs
index cf60e51687..8bb00f6053 100644
--- a/crates/pa-core/tests/kernel_snapshot_resume.rs
+++ b/crates/pa-core/tests/kernel_snapshot_resume.rs
@@ -12,7 +12,7 @@
//! `kernel-state.dill` snapshot, and a resumed session prewarms from the snapshot alone so its
//! first `ipython` call sees the old variables. The kernel Python is ambient; skipped if absent.
-use std::path::PathBuf;
+use std::path::{Path, PathBuf};
use std::time::{Duration, Instant};
use pa_ai::faux::{
@@ -243,7 +243,7 @@ async fn session_end_then_resume_prewarms_and_revives_the_namespace() {
let faux_one = faux_session(vec![
ipython_tool_call_step(
"call-setup",
- "data = {'n': 42}\nmarker = 'lane-snapshot'\nprint('setup done')",
+ "data = {'n': 42}\nmarker = 'lane-snapshot'\nimport os\nos.makedirs('moved', exist_ok=True)\nos.chdir('moved')\nos.environ['PA_RESTORE_PROBE'] = 'set'\nprint('setup done')",
),
text_step("state defined"),
]);
@@ -280,7 +280,10 @@ async fn session_end_then_resume_prewarms_and_revives_the_namespace() {
// Session two: RESUME with the prewarm config flag off; only `hasSnapshot` may fire the boot.
let faux_two = faux_session(vec![
- ipython_tool_call_step("call-read", "print(marker)"),
+ ipython_tool_call_step(
+ "call-read",
+ "print(marker)\nimport os\nprint(os.getcwd())\nprint(os.environ.get('PA_RESTORE_PROBE'))",
+ ),
text_step("read back"),
]);
let resumed_manager = SessionManager::open(&fixture.cwd, &fixture.sessions_dir, &session_file);
@@ -317,6 +320,27 @@ async fn session_end_then_resume_prewarms_and_revives_the_namespace() {
"the resumed namespace must revive: {results:?}"
);
+ // The reset the notice discloses: the resumed kernel runs at the
+ // session cwd (not the moved dir) and the probe env var is gone.
+ let read_back = results
+ .iter()
+ .find(|text| text.contains("lane-snapshot"))
+ .expect("the resumed read cell ran");
+ let session_cwd = fixture
+ .cwd
+ .canonicalize()
+ .expect("canonicalize the fixture cwd");
+ assert!(
+ read_back
+ .lines()
+ .any(|line| Path::new(line.trim()) == session_cwd.as_path()),
+ "the resumed kernel must run at the session cwd: {read_back}"
+ );
+ assert!(
+ read_back.lines().any(|line| line.trim() == "None"),
+ "the probe env var must be reset: {read_back}"
+ );
+
// The restore notice rode the turn and landed durably, naming what came back (TS
// `_onIpythonStateRestored`, display row with the `restored` details flag).
let entries = resumed.session.entries().await;
@@ -343,6 +367,10 @@ async fn session_end_then_resume_prewarms_and_revives_the_namespace() {
content.contains("marker"),
"the notice names what returned: {content}"
);
+ assert!(
+ content.contains("The working directory and environment variables were reset to the session defaults; re-apply any os.chdir(...) or os.environ[...] changes you still need."),
+ "the notice must disclose the cwd/env reset: {content}"
+ );
assert_eq!(
notice.details,
Some(serde_json::json!({ "restored": true })),
diff --git a/scripts/review_restore_notice_parity.py b/scripts/review_restore_notice_parity.py
new file mode 100644
index 0000000000..56b747b72a
--- /dev/null
+++ b/scripts/review_restore_notice_parity.py
@@ -0,0 +1,280 @@
+#!/usr/bin/env python3
+"""Compare real TS/Rust resume behavior, recording #3525's intended notice addition.
+
+Runs the same setup, resume, and acknowledgement fixture against the pinned TS 0.9.8 release and Rust.
+Both must revive a namespace while resetting cwd/env; Rust alone must disclose
+that reset. Only the exact added sentence is excluded from notice parity.
+All provider traffic, homes, sessions, and subprocesses belong to this fixture.
+"""
+import argparse
+import http.server
+import json
+import os
+from pathlib import Path
+import signal
+import subprocess
+import sys
+import tempfile
+import threading
+import time
+
+from review_goal_backoff_parity import TS_ARCHIVE_SHA256, sha256
+
+RESET = ("The working directory and environment variables were reset to the session defaults; "
+ "re-apply any os.chdir(...) or os.environ[...] changes you still need.")
+PROBE = "RESTORE_PARITY_RESULT="
+SETUP = """import os
+restore_parity_marker = 'namespace-survived'
+os.mkdir('moved')
+os.chdir('moved')
+os.environ['PA_RESTORE_PARITY_PROBE'] = 'changed'
+print('RESTORE_PARITY_SETUP=' + __import__('json').dumps({'cwd': os.getcwd(), 'env': os.environ.get('PA_RESTORE_PARITY_PROBE')}))"""
+READ = """import os
+print('RESTORE_PARITY_RESULT=' + __import__('json').dumps({'marker': restore_parity_marker, 'cwd': os.getcwd(), 'env': os.environ.get('PA_RESTORE_PARITY_PROBE')}))"""
+
+
+def texts(value):
+ if isinstance(value, str):
+ yield value
+ elif isinstance(value, list):
+ for item in value:
+ yield from texts(item)
+ elif isinstance(value, dict):
+ # Content blocks have one text member. Avoid matching tool-call arguments.
+ if isinstance(value.get('text'), str):
+ yield value['text']
+
+
+class Server(http.server.ThreadingHTTPServer):
+ daemon_threads = True
+
+ def __init__(self, root):
+ super().__init__(('127.0.0.1', 0), Handler)
+ self.phase = 'setup'
+ self.requests = {'setup': [], 'resume': []}
+ self.tool_names = {}
+ self.root = root
+ self.snapshot_ready = False
+
+
+class Handler(http.server.BaseHTTPRequestHandler):
+ def log_message(self, *_args):
+ pass
+
+ def do_POST(self):
+ if self.path != '/v1/chat/completions':
+ self.send_error(404)
+ return
+ body = json.loads(self.rfile.read(int(self.headers['Content-Length'])))
+ requests = self.server.requests[self.server.phase]
+ requests.append(body)
+ if len(requests) == 1:
+ offered = {tool.get('function', {}).get('name') for tool in body.get('tools', [])}
+ name = next((name for name in ('python_repl', 'ipython') if name in offered), None)
+ if name is None:
+ self.send_error(422, 'No supported Python REPL tool offered')
+ return
+ self.server.tool_names[self.server.phase] = name
+ delta = {'tool_calls': [{'index': 0, 'id': 'call_' + self.server.phase,
+ 'type': 'function', 'function': {'name': name,
+ 'arguments': json.dumps({'code': SETUP if self.server.phase == 'setup' else READ})}}]}
+ finish = 'tool_calls'
+ else:
+ if self.server.phase == 'setup':
+ # Observe the real debounced snapshot before ending either process.
+ # Rust's existing print shutdown only disposes descendant kernels;
+ # the fixture does not claim root shutdown/flush parity.
+ deadline = time.monotonic() + 15
+ while time.monotonic() < deadline:
+ paths = list(self.server.root.rglob('kernel-state.dill'))
+ if any(path.stat().st_size > 0 for path in paths):
+ self.server.snapshot_ready = True
+ break
+ threading.Event().wait(0.025)
+ delta, finish = {'content': 'Fixture complete.'}, 'stop'
+ def chunk(delta, reason=None):
+ return {'id': 'restore-parity', 'object': 'chat.completion.chunk', 'created': 1,
+ 'model': 'restore-fixture', 'choices': [{'index': 0, 'delta': delta,
+ 'finish_reason': reason}]}
+ wire = ''.join('data: ' + json.dumps(item) + '\n\n' for item in (
+ chunk({'role': 'assistant', **delta}), chunk({}, finish))) + 'data: [DONE]\n\n'
+ self.send_response(200)
+ self.send_header('Content-Type', 'text/event-stream')
+ self.send_header('Content-Length', str(len(wire.encode())))
+ self.end_headers()
+ self.wfile.write(wire.encode())
+
+
+def invoke(command, cwd, env):
+ process = subprocess.Popen(command, cwd=cwd, env=env, stdout=subprocess.PIPE,
+ stderr=subprocess.PIPE, text=True, start_new_session=True)
+ try:
+ stdout, stderr = process.communicate(timeout=75)
+ except subprocess.TimeoutExpired:
+ os.killpg(process.pid, signal.SIGKILL)
+ stdout, stderr = process.communicate(timeout=5)
+ raise RuntimeError('binary timed out: ' + stderr[-2000:])
+ if process.returncode:
+ raise RuntimeError('binary exited %s: %s' % (process.returncode, stderr[-3000:]))
+ return {'exit_code': process.returncode, 'stdout': stdout, 'stderr': stderr}
+
+
+def probe_from(requests, marker):
+ for body in requests:
+ for message in body.get('messages', []):
+ if message.get('role') != 'tool':
+ continue
+ for text in texts(message.get('content')):
+ for line in text.splitlines():
+ if line.startswith(marker):
+ return json.loads(line[len(marker):])
+ raise AssertionError('missing actual tool result ' + marker)
+
+
+def notices_in(request):
+ return [text for message in request.get('messages', [])
+ if message.get('role') != 'assistant'
+ for text in texts(message.get('content')) if '[python-state-restored]' in text]
+
+
+def run_case(binary, label, root, observation):
+ for name in ('home', 'agent', 'sessions', 'work', 'tmp'):
+ (root / name).mkdir(parents=True)
+ cwd, agent, sessions = root / 'work', root / 'agent', root / 'sessions'
+ env = {key: os.environ[key] for key in ('PATH', 'LANG', 'LC_ALL', 'TZ',
+ 'PRIME_AGENT_KERNEL_PYTHON', 'PA_E2E_KERNEL_PYTHON', 'PYTHONPATH') if key in os.environ}
+ if label == 'ts' and 'PI_PACKAGE_DIR' in os.environ:
+ env['PI_PACKAGE_DIR'] = os.environ['PI_PACKAGE_DIR']
+ env.update(HOME=str(root / 'home'), TMPDIR=str(root / 'tmp'), DO_NOT_TRACK='1',
+ PI_OFFLINE='1', PI_CODING_AGENT_DIR=str(agent),
+ PRIME_AGENT_CODING_AGENT_DIR=str(agent), PI_SESSION_DIR=str(sessions),
+ PRIME_AGENT_SESSION_DIR=str(sessions), XDG_CONFIG_HOME=str(root / 'config'),
+ XDG_CACHE_HOME=str(root / 'cache'), XDG_DATA_HOME=str(root / 'data'))
+ # Both runtimes receive the same fixture skill library. This isolates the
+ # restore notice from different installed/bundled skill discovery paths.
+ library = Path(os.environ['PI_PACKAGE_DIR']) / 'skills'
+ skill_paths = sorted(str(path) for path in library.glob('*/src') if path.is_dir())
+ env['PYTHONPATH'] = os.pathsep.join(skill_paths + [env.get('PYTHONPATH', '')])
+ observation['fixture_pythonpath'] = env['PYTHONPATH']
+ server = Server(root)
+ thread = threading.Thread(target=server.serve_forever, daemon=True)
+ thread.start()
+ (agent / 'models.json').write_text(json.dumps({'providers': {'fixture': {
+ 'api': 'openai-completions', 'apiKey': 'fixture-only',
+ 'baseUrl': 'http://127.0.0.1:%s/v1' % server.server_address[1],
+ 'models': [{'id': 'restore-fixture', 'name': 'Restore fixture', 'reasoning': False,
+ 'contextWindow': 128000, 'maxTokens': 4096}]}}}))
+ (agent / 'settings.json').write_text(json.dumps({'defaultProvider': 'fixture',
+ 'defaultModel': 'restore-fixture', 'prewarmPythonKernel': False}))
+ command = [str(binary), '--mode', 'json', '--provider', 'fixture', '--model',
+ 'restore-fixture', '--session-dir', str(sessions)]
+ try:
+ version = invoke([str(binary), '--version'], cwd, env)
+ setup = invoke(command + ['Set up the restore fixture.'], cwd, env)
+ observation.update(setup_stdout=setup['stdout'], setup_stderr=setup['stderr'],
+ setup_requests=server.requests['setup'],
+ snapshot_ready_before_setup_completion=server.snapshot_ready)
+ setup_probe = probe_from(server.requests['setup'], 'RESTORE_PARITY_SETUP=')
+ observation['setup'] = setup_probe
+ assert setup_probe == {'cwd': str(cwd / 'moved'), 'env': 'changed'}, setup_probe
+ session_files = list(sessions.rglob('*.jsonl'))
+ session_files = [p for p in session_files if json.loads(p.read_text().splitlines()[0]).get('type') == 'session']
+ assert len(session_files) == 1, 'expected one persisted session: %s' % session_files
+ snapshots = list(root.rglob('kernel-state.dill'))
+ assert len(snapshots) == 1 and snapshots[0].stat().st_size, 'setup did not persist a kernel snapshot'
+ assert server.snapshot_ready, 'debounced snapshot never became ready before setup completion'
+ assert not notices_in(server.requests['setup'][0]), 'fresh session emitted a restore notice'
+ server.phase = 'resume'
+ resume = invoke(command + ['--resume', str(session_files[0]), 'Read the restored state.',
+ 'Acknowledge the restore notice.'], cwd, env)
+ observation.update(setup_stdout=setup['stdout'], resume_stdout=resume['stdout'],
+ stderr={'setup': setup['stderr'], 'resume': resume['stderr']},
+ request_sequence={phase: [{'number': i + 1,
+ 'roles': [m.get('role') for m in body.get('messages', [])],
+ 'restore_notices': notices_in(body)}
+ for i, body in enumerate(requests)]
+ for phase, requests in server.requests.items()})
+ actual = probe_from(server.requests['resume'], PROBE)
+ assert actual == {'marker': 'namespace-survived', 'cwd': str(cwd), 'env': None}, actual
+ # TS queues a restore triggered by the first cell for the next user turn
+ # (sendCustomMessage(nextTurn), consumed during prompt preparation).
+ # Submit the same acknowledgement prompt to both binaries and record
+ # every request, including any pre-existing delivery-timing difference.
+ notice_request_numbers = [i + 1 for i, body in enumerate(server.requests['resume'])
+ if notices_in(body)]
+ notices = notices_in(server.requests['resume'][-1])
+ assert len(notices) == 1, 'restore notice missing from acknowledgement request: %s' % notices
+ notice = notices[0]
+ assert 'revived from your previous session' in notice and 'restore_parity_marker' in notice, notice
+ # Persisted/renderable notice is inspected separately from the provider request.
+ entries = [json.loads(line) for line in session_files[0].read_text().splitlines()]
+ stored = [row for row in entries if row.get('customType') == 'ipython_state_restored']
+ assert len(stored) == 1 and stored[0].get('display') is True, stored
+ stored_notice = '\n'.join(texts(stored[0].get('content')))
+ assert stored_notice in notice, 'provider must receive the persisted notice'
+ assert (RESET in stored_notice) == (label == 'rust'), stored_notice
+ return {'version': version['stdout'].strip(), 'setup': setup_probe,
+ 'restored': {**actual, 'cwd': '<session-cwd>'},
+ 'notice': stored_notice, 'notice_request_numbers': notice_request_numbers,
+ 'request_sequence': observation['request_sequence'],
+ 'offered_repl_tool_names': server.tool_names,
+ 'request_counts': {k: len(v) for k, v in server.requests.items()},
+ 'display': stored[0]['display'], 'details': stored[0].get('details'),
+ 'setup_stdout': setup['stdout'], 'resume_stdout': resume['stdout'],
+ 'stderr': {'setup': setup['stderr'], 'resume': resume['stderr']}}
+ finally:
+ server.shutdown()
+ server.server_close()
+ thread.join(timeout=2)
+
+
+def main():
+ parser = argparse.ArgumentParser(description=__doc__)
+ parser.add_argument('--receipt', type=Path, required=True)
+ args = parser.parse_args()
+ binaries = {label: Path(os.environ['PA_' + label.upper() + '_BINARY']).resolve()
+ for label in ('ts', 'rust')}
+ assert sys.platform == 'linux', 'requires Linux hosted CI'
+ archive_hash = sha256(os.environ['PA_TS_ARCHIVE'])
+ assert archive_hash == TS_ARCHIVE_SHA256, 'TS release archive hash mismatch'
+ result = {'ts_release_archive_sha256': archive_hash,
+ 'binaries': {label: {'path': str(path), 'sha256': sha256(path)}
+ for label, path in binaries.items()},
+ 'intended_difference': RESET,
+ 'tool_name_normalization': 'Select offered python_repl or legacy ipython; identical cell code.',
+ 'fixture_prerequisites': [
+ 'Wait for observable nonempty debounced snapshot before setup final answer in both binaries.',
+ 'Existing Rust print shutdown disposes descendants but does not await root snapshot; no disposal parity claimed.',
+ 'Both kernels receive the same pinned TS skills/src library via PYTHONPATH; this is fixture setup, not a production namespace claim.'],
+ 'runs': {}, 'verified': False}
+ try:
+ with tempfile.TemporaryDirectory(prefix='restore-notice-parity-') as temp:
+ for label, binary in binaries.items():
+ observation = {}
+ result['runs'][label] = observation
+ try:
+ observation.update(run_case(binary, label, Path(temp) / label, observation))
+ except Exception as error:
+ observation['error'] = str(error)
+ ts, rust = result['runs']['ts'], result['runs']['rust']
+ errors = {label: run['error'] for label, run in result['runs'].items() if 'error' in run}
+ assert not errors, errors
+ assert rust['notice'].removesuffix('\n' + RESET) == ts['notice'], 'additional notice difference'
+ assert rust['restored'] == ts['restored'], 'restored state differs'
+ assert rust['request_counts'] == ts['request_counts'] == {'setup': 2, 'resume': 3}
+ result['notice_timing_equal'] = ts['notice_request_numbers'] == rust['notice_request_numbers']
+ result['timing_scope'] = ('Observed, not normalized: #3525 changes only notice_content and its tests; '
+ 'kernel boot and next-turn queue timing are unchanged from base Rust. '
+ 'Both binaries must deliver the notice by the acknowledgement prompt.')
+ result['verified'] = True
+ except Exception as error:
+ result['error'] = str(error)
+ args.receipt.parent.mkdir(parents=True, exist_ok=True)
+ args.receipt.write_text(json.dumps(result, indent=2) + '\n')
+ print(json.dumps({'receipt': str(args.receipt), 'verified': result['verified'],
+ 'error': result.get('error')}))
+ return 0 if result['verified'] else 1
+
+
+if __name__ == '__main__':
+ sys.exit(main())