diff --git a/.github/workflows/desktop-release-artifacts.yml b/.github/workflows/desktop-release-artifacts.yml index 565a09265e..756664c3fc 100644 --- a/.github/workflows/desktop-release-artifacts.yml +++ b/.github/workflows/desktop-release-artifacts.yml @@ -333,12 +333,12 @@ jobs: run: | set -euo pipefail mkdir -p dist/previous-desktop - mapfile -t assets < <( - gh release view "${RELEASE_TAG}" \ - --repo "${GITHUB_REPOSITORY}" \ - --json assets \ - --jq '.assets[].name | select(. == "DESKTOP-SHA256SUMS" or (startswith("LoopX") and (endswith(".dmg") or endswith(".zip") or endswith(".exe") or endswith(".msi"))))' - ) + release_id="$(gh api "repos/${GITHUB_REPOSITORY}/releases/tags/${RELEASE_TAG}" --jq '.id')" + [[ "${release_id}" =~ ^[0-9]+$ ]] + asset_list="$(gh api --paginate "repos/${GITHUB_REPOSITORY}/releases/${release_id}/assets?per_page=100" \ + --jq '.[].name | select(. == "DESKTOP-SHA256SUMS" or (startswith("LoopX") and (endswith(".dmg") or endswith(".zip") or endswith(".exe") or endswith(".msi"))))')" + assets=() + if [ -n "${asset_list}" ]; then mapfile -t assets <<<"${asset_list}"; fi for asset in "${assets[@]}"; do gh release download "${RELEASE_TAG}" \ --repo "${GITHUB_REPOSITORY}" \ @@ -385,18 +385,90 @@ jobs: cd dist/desktop sha256sum --check DESKTOP-SHA256SUMS + - name: Resume desktop release without replacing assets + if: github.event_name == 'release' + env: + GH_TOKEN: ${{ github.token }} + RELEASE_TAG: ${{ steps.identity.outputs.release-tag }} + RELEASE_VERSION: ${{ steps.identity.outputs.release-version }} + SOURCE_REF: ${{ github.sha }} + run: | + set -euo pipefail + shopt -s nullglob + if [ "$RELEASE_TAG" != "v$RELEASE_VERSION" ] && [ "$RELEASE_TAG" != "$RELEASE_VERSION" ]; then + echo "::error::Desktop release version does not match its tag" >&2 + exit 1 + fi + python scripts/desktop_release_artifacts.py verify-checksums \ + --dist-dir dist/desktop --version "$RELEASE_VERSION" \ + --checksum-file dist/desktop/DESKTOP-SHA256SUMS + tmpdir="$(mktemp -d)" + trap 'rm -rf "$tmpdir"' EXIT + verify_ref() { + local actual + actual="$(gh api "repos/$GITHUB_REPOSITORY/commits/$RELEASE_TAG" --jq '.sha')" + if [ "$actual" != "$SOURCE_REF" ]; then + echo "::error::Desktop release tag does not match the build commit" >&2 + return 1 + fi + } + read_assets() { + local release_id + release_id="$(gh api "repos/$GITHUB_REPOSITORY/releases/tags/$RELEASE_TAG" --jq '.id')" + [[ "$release_id" =~ ^[0-9]+$ ]] + remote_assets="$(gh api --paginate "repos/$GITHUB_REPOSITORY/releases/$release_id/assets?per_page=100" --jq '.[].name')" + } + check_asset() { + local artifact="$1" name matches + name="${artifact##*/}" + [[ "$name" =~ ^[A-Za-z0-9._+-]+$ ]] + matches="$(LC_ALL=C grep -Fixc -- "$name" <<<"$remote_assets" || true)" + asset_exists=0 + if [ "$matches" -eq 0 ]; then return; fi + if [ "$matches" -ne 1 ] || ! grep -Fxq -- "$name" <<<"$remote_assets"; then + echo "::error::Desktop release asset name collision: $name" >&2 + return 1 + fi + gh release download "$RELEASE_TAG" --repo "$GITHUB_REPOSITORY" --pattern "$name" --dir "$tmpdir" --clobber + if ! cmp -s "$artifact" "$tmpdir/$name"; then + echo "::error::Desktop release asset differs: $name" >&2 + return 1 + fi + asset_exists=1 + } + binaries=(dist/desktop/*.dmg dist/desktop/*.zip dist/desktop/*.exe dist/desktop/*.msi) + manifest=dist/desktop/DESKTOP-SHA256SUMS + verify_ref + read_assets + for artifact in "${binaries[@]}" "$manifest"; do check_asset "$artifact"; done + for artifact in "${binaries[@]}"; do + if ! grep -Fxq -- "${artifact##*/}" <<<"$remote_assets"; then + gh release upload "$RELEASE_TAG" "$artifact" --repo "$GITHUB_REPOSITORY" + fi + done + read_assets + for artifact in "${binaries[@]}"; do + check_asset "$artifact" + if [ "$asset_exists" -ne 1 ]; then echo "::error::Missing uploaded desktop artifact" >&2; exit 1; fi + done + check_asset "$manifest" + if [ "$asset_exists" -eq 0 ]; then gh release upload "$RELEASE_TAG" "$manifest" --repo "$GITHUB_REPOSITORY"; fi + read_assets + check_asset "$manifest" + if [ "$asset_exists" -ne 1 ]; then echo "::error::Missing uploaded desktop manifest" >&2; exit 1; fi + verify_ref + - name: Replace complete desktop binary set + if: github.event_name == 'workflow_dispatch' env: GH_TOKEN: ${{ github.token }} RELEASE_TAG: ${{ steps.identity.outputs.release-tag }} run: | set -euo pipefail - if gh release view "${RELEASE_TAG}" \ - --repo "${GITHUB_REPOSITORY}" \ - --json assets \ - --jq 'any(.assets[]; .name == "DESKTOP-SHA256SUMS")' \ - | grep -qx true - then + release_id="$(gh api "repos/${GITHUB_REPOSITORY}/releases/tags/${RELEASE_TAG}" --jq '.id')" + [[ "${release_id}" =~ ^[0-9]+$ ]] + asset_list="$(gh api --paginate "repos/${GITHUB_REPOSITORY}/releases/${release_id}/assets?per_page=100" --jq '.[].name')" + if grep -Fxq DESKTOP-SHA256SUMS <<<"${asset_list}"; then gh release delete-asset "${RELEASE_TAG}" DESKTOP-SHA256SUMS \ --repo "${GITHUB_REPOSITORY}" \ --yes @@ -410,6 +482,7 @@ jobs: --clobber - name: Replace desktop checksum manifest last + if: github.event_name == 'workflow_dispatch' env: GH_TOKEN: ${{ github.token }} RELEASE_TAG: ${{ steps.identity.outputs.release-tag }} @@ -438,6 +511,7 @@ jobs: --repo "${GITHUB_REPOSITORY}" \ --pattern "${asset}" \ --dir dist/published-desktop + cmp "dist/desktop/${asset}" "dist/published-desktop/${asset}" done python scripts/desktop_release_artifacts.py verify-checksums \ --dist-dir dist/published-desktop \ diff --git a/.github/workflows/desktop-updater.yml b/.github/workflows/desktop-updater.yml index a6eaa65f18..9fdbd8d771 100644 --- a/.github/workflows/desktop-updater.yml +++ b/.github/workflows/desktop-updater.yml @@ -74,7 +74,7 @@ jobs: steps: - uses: actions/checkout@v7 with: - ref: ${{ github.event.release.tag_name || 'main' }} + ref: ${{ github.sha }} - id: identity env: RELEASE_TAG: ${{ github.event.release.tag_name }} @@ -216,7 +216,7 @@ jobs: path: dist/updater/* if-no-files-found: error publish: - needs: [identity, build-macos, build-windows, validate] + needs: [identity, build-macos, build-windows, validate, windows-desktop-tests] # A skipped Windows build is valid only on stable, where the feed is # Darwin-only. A main release must have its Windows artifact. if: >- @@ -224,6 +224,7 @@ jobs: && needs.identity.result == 'success' && needs.build-macos.result == 'success' && needs.validate.result == 'success' + && needs.windows-desktop-tests.result == 'success' && (needs.identity.outputs.channel != 'main' || needs.build-windows.result == 'success') runs-on: ubuntu-latest permissions: @@ -250,33 +251,130 @@ jobs: SOURCE_REF: ${{ needs.identity.outputs.ref }} EVENT_NAME: ${{ github.event_name }} CHANNEL: ${{ needs.identity.outputs.channel }} + VERSION: ${{ needs.identity.outputs.version }} run: | set -euo pipefail - # Stable builds intentionally have no Windows artifact. Without - # nullglob, Bash passes the unmatched pattern to `gh`, which exits - # before the feed and channel pointer are published. shopt -s nullglob - if [ "$EVENT_NAME" != release ]; then - gh release create "$RELEASE_TAG" --prerelease --target "$SOURCE_REF" --title "Desktop main $SOURCE_REF" --notes "Signed App and matching runtime preview." + python - "$VERSION" "$RELEASE_TAG" "$CHANNEL" <<'PY' + import json + import sys + from pathlib import Path + from scripts.desktop_update_feed import build + + version, tag, channel = sys.argv[1:] + if channel == "stable": + expected_tag = "v" + version + elif channel == "main" and version.startswith("0.0.0-main."): + expected_tag = "desktop-main-" + version.removeprefix("0.0.0-main.").replace(".", "-") + else: + raise SystemExit("invalid desktop release identity") + if tag != expected_tag or json.loads(Path("dist/updater/desktop-updater.json").read_text()) != build(Path("dist/updater"), version, tag, channel): + raise SystemExit("desktop release feed does not match its identity and artifacts") + PY + tmpdir="$(mktemp -d)" + trap 'rm -rf "$tmpdir"' EXIT + verify_ref() { + local actual + actual="$(gh api "repos/$GITHUB_REPOSITORY/commits/$RELEASE_TAG" --jq '.sha')" + if [ "$actual" != "$SOURCE_REF" ]; then + echo "::error::Desktop release tag does not match the build commit" >&2 + return 1 + fi + } + if ! gh api --include "repos/$GITHUB_REPOSITORY/releases/tags/$RELEASE_TAG" > "$tmpdir/release-probe"; then + if [ "$EVENT_NAME" != release ] && grep -Eq '^HTTP/[0-9.]+ 404([[:space:]]|$)' "$tmpdir/release-probe"; then + if gh api --include "repos/$GITHUB_REPOSITORY/commits/$RELEASE_TAG" > "$tmpdir/tag-probe"; then + verify_ref + elif ! grep -Eq '^HTTP/[0-9.]+ 404([[:space:]]|$)' "$tmpdir/tag-probe"; then + echo "::error::Cannot inspect desktop release tag $RELEASE_TAG" >&2 + exit 1 + fi + gh release create "$RELEASE_TAG" --prerelease --target "$SOURCE_REF" --title "Desktop main $SOURCE_REF" --notes "Signed App and matching runtime preview." + else + echo "::error::Cannot inspect desktop release $RELEASE_TAG" >&2 + exit 1 + fi fi - for artifact in dist/updater/*.gz dist/updater/*.sig dist/updater/*.exe; do - gh release upload "$RELEASE_TAG" "$artifact" + read_assets() { + local release_id tag="$1" + release_id="$(gh api "repos/$GITHUB_REPOSITORY/releases/tags/$tag" --jq '.id')" + [[ "$release_id" =~ ^[0-9]+$ ]] + remote_assets="$(gh api --paginate "repos/$GITHUB_REPOSITORY/releases/$release_id/assets?per_page=100" --jq '.[].name')" + } + check_asset() { + local artifact="$1" name matches + name="${artifact##*/}" + [[ "$name" =~ ^[A-Za-z0-9._-]+$ ]] + matches="$(LC_ALL=C grep -Fixc -- "$name" <<<"$remote_assets" || true)" + asset_exists=0 + if [ "$matches" -eq 0 ]; then return; fi + if [ "$matches" -ne 1 ] || ! grep -Fxq -- "$name" <<<"$remote_assets"; then + echo "::error::Desktop release asset name collision: $name" >&2 + return 1 + fi + gh release download "$RELEASE_TAG" --pattern "$name" --dir "$tmpdir" --clobber + if ! cmp -s "$artifact" "$tmpdir/$name"; then + echo "::error::Desktop release asset differs: $name" >&2 + return 1 + fi + asset_exists=1 + } + artifacts=(dist/updater/*.gz dist/updater/*.sig dist/updater/*.exe) + feed=dist/updater/desktop-updater.json + verify_ref + read_assets "$RELEASE_TAG" + # Reject every conflict before uploading any missing file. + for artifact in "${artifacts[@]}" "$feed"; do check_asset "$artifact"; done + for artifact in "${artifacts[@]}"; do + name="${artifact##*/}" + if ! grep -Fxq -- "$name" <<<"$remote_assets"; then + gh release upload "$RELEASE_TAG" "$artifact" + fi done - gh release upload "$RELEASE_TAG" dist/updater/desktop-updater.json --clobber + read_assets "$RELEASE_TAG" + for artifact in "${artifacts[@]}"; do + check_asset "$artifact" + if [ "$asset_exists" -ne 1 ]; then echo "::error::Missing uploaded desktop artifact" >&2; exit 1; fi + done + check_asset "$feed" + if [ "$asset_exists" -eq 0 ]; then gh release upload "$RELEASE_TAG" "$feed"; fi + read_assets "$RELEASE_TAG" + for artifact in "${artifacts[@]}" "$feed"; do + check_asset "$artifact" + if [ "$asset_exists" -ne 1 ]; then echo "::error::Incomplete desktop release" >&2; exit 1; fi + done + verify_ref # Channel pointers are independent of repository Latest, which also # serves separately versioned plugin releases. Advance only after the # immutable signed assets and feed are completely uploaded. pointer="desktop-$CHANNEL" - gh release view "$pointer" >/dev/null 2>&1 || gh release create "$pointer" --prerelease --latest=false --target "$SOURCE_REF" --title "Desktop $CHANNEL channel" --notes "Pointer to the latest complete signed desktop build." - pointer_assets="$(gh release view "$pointer" --json assets --jq '.assets[].name')" + if ! gh api --include "repos/$GITHUB_REPOSITORY/releases/tags/$pointer" > "$tmpdir/pointer-probe"; then + if grep -Eq '^HTTP/[0-9.]+ 404([[:space:]]|$)' "$tmpdir/pointer-probe"; then + gh release create "$pointer" --prerelease --latest=false --target "$SOURCE_REF" --title "Desktop $CHANNEL channel" --notes "Pointer to the latest complete signed desktop build." + else + echo "::error::Cannot inspect desktop channel $pointer" >&2 + exit 1 + fi + fi + read_assets "$pointer" + pointer_assets="$remote_assets" + pointer_matches="$(LC_ALL=C grep -Fixc desktop-updater.json <<<"$pointer_assets" || true)" + if [ "$pointer_matches" -gt 1 ] || { [ "$pointer_matches" -eq 1 ] && ! grep -Fxq desktop-updater.json <<<"$pointer_assets"; }; then + echo "::error::Desktop channel feed name collision" >&2 + exit 1 + fi decision=advance if grep -Fxq desktop-updater.json <<<"$pointer_assets"; then mkdir -p dist/current-pointer - gh release download "$pointer" --pattern desktop-updater.json --dir dist/current-pointer - decision="$(python scripts/desktop_update_feed.py \ - --channel-pointer-decision "$CHANNEL" \ - --current-feed dist/current-pointer/desktop-updater.json \ - --candidate-feed dist/updater/desktop-updater.json)" + gh release download "$pointer" --pattern desktop-updater.json --dir dist/current-pointer --clobber + if cmp -s dist/current-pointer/desktop-updater.json dist/updater/desktop-updater.json; then + decision=retain + else + decision="$(python scripts/desktop_update_feed.py \ + --channel-pointer-decision "$CHANNEL" \ + --current-feed dist/current-pointer/desktop-updater.json \ + --candidate-feed dist/updater/desktop-updater.json)" + fi fi if [ "$decision" = advance ]; then gh release upload "$pointer" dist/updater/desktop-updater.json --clobber diff --git a/.github/workflows/python-tests.yml b/.github/workflows/python-tests.yml index f6329b30dc..4bd7ba0a4a 100644 --- a/.github/workflows/python-tests.yml +++ b/.github/workflows/python-tests.yml @@ -192,7 +192,11 @@ jobs: npm run smoke:chat-upgrade npm run smoke:delegation-search npm run smoke:task-inspector-route + npm run smoke:manager-goal-routing + LOOPX_PERSONAL_WORKSPACE_PACKAGED=1 npm run smoke:manager-goal-routing-browser + npm run smoke:managed-goal-result-schema npm run smoke:conversation-find + npm run smoke:conversation-history - name: Qualify packaged team evidence and feedback recovery if: matrix.shard == 2 working-directory: apps/presentation/dashboard @@ -204,6 +208,7 @@ jobs: LOOPX_USAGE_PING: "0" run: | python -m pip install --disable-pip-version-check -e ".[test]" + npm --prefix apps/presentation/dashboard run smoke:bound-conversation node apps/presentation/dashboard/smoke/team-task-revalidation-smoke.mjs - name: Preserve synthetic original-task recovery views if: always() && matrix.shard == 2 diff --git a/.github/workflows/release-artifacts.yml b/.github/workflows/release-artifacts.yml index b7ce17a6f8..5e53501a0d 100644 --- a/.github/workflows/release-artifacts.yml +++ b/.github/workflows/release-artifacts.yml @@ -16,6 +16,7 @@ on: - "apps/presentation/dashboard/**" - "scripts/chat_bundle.py" - "scripts/verify_installed_chat.py" + - "scripts/verify_installed_runtime.py" - "README.md" - "scripts/release_artifacts.py" - "scripts/runtime_artifact.py" @@ -137,7 +138,7 @@ jobs: dist/packages/*.whl test "$("${RUNNER_TEMP}/loopx-wheel/bin/loopx" --version)" = \ "loopx ${RELEASE_TAG#v}" - "${RUNNER_TEMP}/loopx-wheel/bin/python" - <<'PY' + "${RUNNER_TEMP}/loopx-wheel/bin/python" -I - <<'PY' from loopx.control_plane.effect_program import SettlementIdentity from loopx.control_plane.effect_runtime import ( collect_effect_runtime_readiness, @@ -226,9 +227,11 @@ jobs: - name: Verify installed frontend and sdist rebuild run: | python scripts/verify_installed_chat.py --python "${RUNNER_TEMP}/loopx-wheel/bin/python" + python scripts/verify_installed_runtime.py --python "${RUNNER_TEMP}/loopx-wheel/bin/python" python -m pip wheel --no-deps dist/packages/*.tar.gz --wheel-dir "${RUNNER_TEMP}/sdist-wheel" "${RUNNER_TEMP}/loopx-wheel/bin/python" -m pip install --force-reinstall --no-deps "${RUNNER_TEMP}"/sdist-wheel/*.whl python scripts/verify_installed_chat.py --python "${RUNNER_TEMP}/loopx-wheel/bin/python" + python scripts/verify_installed_runtime.py --python "${RUNNER_TEMP}/loopx-wheel/bin/python" - name: Resolve browser fixture correction without rebuilding product bytes env: @@ -570,6 +573,7 @@ jobs: path: | output/playwright/personal-workspace/acceptance-results.json output/playwright/personal-workspace/qualification.json + output/playwright/personal-workspace/attached-host-pending-queue*.png if-no-files-found: error retention-days: 7 diff --git a/.github/workflows/update-notes.yml b/.github/workflows/update-notes.yml index 6053e0a196..7b0784e43e 100644 --- a/.github/workflows/update-notes.yml +++ b/.github/workflows/update-notes.yml @@ -70,7 +70,11 @@ jobs: - name: Package reviewable draft if: steps.generate.outputs.changed == 'true' - run: git diff --binary > update-note.patch + env: + NOTE_FILE: ${{ steps.generate.outputs.note_file }} + run: | + git add --intent-to-add -- "$NOTE_FILE" + git diff --binary > update-note.patch - name: Upload update-note draft if: steps.generate.outputs.changed == 'true' diff --git a/apps/desktop/loopx-control-plane/src-tauri/src/service_lifecycle_windows_update_tests.rs b/apps/desktop/loopx-control-plane/src-tauri/src/service_lifecycle_windows_update_tests.rs index e32222644e..2ccc05e778 100644 --- a/apps/desktop/loopx-control-plane/src-tauri/src/service_lifecycle_windows_update_tests.rs +++ b/apps/desktop/loopx-control-plane/src-tauri/src/service_lifecycle_windows_update_tests.rs @@ -35,7 +35,7 @@ use ring::signature::{Ed25519KeyPair, KeyPair}; use serde_json::json; use std::collections::HashMap; use std::io::{Cursor, Read, Write}; -use std::net::TcpListener; +use std::net::{TcpListener, TcpStream}; use std::sync::atomic::{AtomicBool, Ordering}; use std::sync::Arc; use std::time::Duration; @@ -149,6 +149,7 @@ fn feed_body(artifact_url: &str, signature: &str) -> Vec { struct FeedServer { origin: String, stop: Arc, + accepted: Arc, thread: Option>, } @@ -167,14 +168,21 @@ impl FeedServer { .map(|(path, content_type, body)| (path.to_string(), (content_type, body))) .collect(); let stop = Arc::new(AtomicBool::new(false)); + let accepted = Arc::new(AtomicBool::new(false)); let thread = std::thread::spawn({ let stop = Arc::clone(&stop); + let accepted = Arc::clone(&accepted); move || { while !stop.load(Ordering::Acquire) { let Ok((mut stream, _)) = listener.accept() else { std::thread::sleep(Duration::from_millis(10)); continue; }; + // Accepted Windows sockets inherit the listener's non-blocking mode. + stream + .set_nonblocking(false) + .expect("blocking feed connection"); + accepted.store(true, Ordering::Release); let _ = stream.set_read_timeout(Some(Duration::from_secs(5))); let mut request = Vec::new(); let mut buffer = [0u8; 512]; @@ -213,6 +221,7 @@ impl FeedServer { Self { origin, stop, + accepted, thread: Some(thread), } } @@ -227,6 +236,73 @@ impl Drop for FeedServer { } } +#[test] +fn loopback_feed_waits_for_complete_request_headers() { + let server = FeedServer::start(|_| vec![("/feed", "text/plain", b"ok".to_vec())]); + let address = server.origin.strip_prefix("http://").expect("feed origin"); + let mut client = TcpStream::connect(address).expect("connect to feed"); + client + .set_nonblocking(true) + .expect("non-blocking observation"); + wait_until( + || server.accepted.load(Ordering::Acquire), + "feed connection acceptance", + ); + // A non-blocking peek keeps the Windows connection usable while checking for an early reply. + let assert_pending = |stream: &TcpStream| { + let until = std::time::Instant::now() + Duration::from_millis(500); + loop { + match stream.peek(&mut [0u8; 1]) { + Err(error) if error.kind() == std::io::ErrorKind::WouldBlock => {} + other => panic!("feed replied or disconnected before complete headers: {other:?}"), + } + if std::time::Instant::now() >= until { + break; + } + std::thread::sleep(Duration::from_millis(10)); + } + }; + assert_pending(&client); + client + .set_nonblocking(false) + .expect("blocking client writes"); + client + .write_all(b"GET /feed HTTP/1.1\r\nHost: localhost\r\n") + .expect("send partial headers"); + client + .set_nonblocking(true) + .expect("non-blocking observation"); + assert_pending(&client); + client + .set_nonblocking(false) + .expect("blocking client writes"); + client.write_all(b"\r\n").expect("finish request headers"); + client + .set_read_timeout(Some(Duration::from_secs(5))) + .expect("client response timeout"); + let mut response = String::new(); + client + .read_to_string(&mut response) + .expect("read feed response"); + assert_eq!(response, "HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\nContent-Length: 2\r\nConnection: close\r\n\r\nok"); + + let mut missing = TcpStream::connect(address).expect("connect for missing route"); + missing + .set_read_timeout(Some(Duration::from_secs(5))) + .expect("missing route timeout"); + missing + .write_all(b"GET /missing HTTP/1.1\r\nHost: localhost\r\n\r\n") + .expect("request missing route"); + response.clear(); + missing + .read_to_string(&mut response) + .expect("read missing route response"); + assert_eq!( + response, + "HTTP/1.1 404 Not Found\r\nContent-Length: 0\r\nConnection: close\r\n\r\n" + ); +} + /// The updater the production window builds, with the pre-exit hook injected /// as an argument so the plugin's default hook can be contrasted with ours. fn updater_with( diff --git a/apps/desktop/loopx-control-plane/src-tauri/src/services_tests.rs b/apps/desktop/loopx-control-plane/src-tauri/src/services_tests.rs index ecb7547f7b..880671b4d6 100644 --- a/apps/desktop/loopx-control-plane/src-tauri/src/services_tests.rs +++ b/apps/desktop/loopx-control-plane/src-tauri/src/services_tests.rs @@ -584,6 +584,46 @@ fn read_probe_request(stream: &mut TcpStream, path: &str) -> bool { .starts_with(&format!("GET {path} HTTP/1.1")) } +fn respond_to_probe_request(stream: &mut TcpStream, path: &str, payload: &str) { + stream.set_nonblocking(false).unwrap(); + assert!(read_probe_request(stream, path)); + write!( + stream, + "HTTP/1.1 200 OK\r\nConnection: close\r\n\r\n{payload}" + ) + .unwrap(); +} + +#[test] +fn readiness_fixture_waits_for_complete_request_on_nonblocking_stream() { + let listener = std::net::TcpListener::bind(("127.0.0.1", 0)).unwrap(); + let address = listener.local_addr().unwrap(); + let (accepted, ready) = std::sync::mpsc::channel(); + let server = thread::spawn(move || { + let (mut stream, _) = listener.accept().unwrap(); + stream.set_nonblocking(true).unwrap(); // Model an inherited nonblocking listener on macOS. + accepted.send(()).unwrap(); + respond_to_probe_request(&mut stream, "/ready", "{}"); + }); + let mut client = TcpStream::connect(address).unwrap(); + client + .set_read_timeout(Some(Duration::from_millis(100))) + .unwrap(); + ready.recv_timeout(Duration::from_secs(2)).unwrap(); + client.write_all(b"GET /ready HTTP/1.1\r\nHost: ").unwrap(); + let mut early = [0; 1]; + assert!(matches!(client.read(&mut early), Err(error) + if matches!(error.kind(), ErrorKind::WouldBlock | ErrorKind::TimedOut))); + client + .set_read_timeout(Some(Duration::from_secs(2))) + .unwrap(); + client.write_all(b"127.0.0.1\r\n\r\n").unwrap(); + let mut response = String::new(); + client.read_to_string(&mut response).unwrap(); + assert!(response.starts_with("HTTP/1.1 200 OK\r\n")); + server.join().unwrap(); +} + #[test] fn a_trickling_readiness_response_cannot_extend_the_deadline() { let listener = std::net::TcpListener::bind(("127.0.0.1", 0)).unwrap(); @@ -1142,10 +1182,11 @@ fn connected_runtime_requires_both_healthy_exact_identity_listeners() { let deadline = Instant::now() + Duration::from_secs(2); loop { if let Ok((mut stream, _)) = listener.accept() { - stream.set_read_timeout(Some(Duration::from_secs(1))).unwrap(); - let mut request = [0; 1024]; - let _ = stream.read(&mut request); - let _ = write!(stream, "HTTP/1.1 200 OK\r\nConnection: close\r\n\r\n{payload}"); + respond_to_probe_request( + &mut stream, + kind.probe_path(), + &payload.to_string(), + ); return; } if Instant::now() >= deadline { return; } diff --git a/apps/presentation/dashboard/package.json b/apps/presentation/dashboard/package.json index 6360cf9020..398f038ea0 100644 --- a/apps/presentation/dashboard/package.json +++ b/apps/presentation/dashboard/package.json @@ -27,6 +27,9 @@ "smoke:capability-configuration": "rm -rf node_modules/.cache/loopx-capability-configuration-smoke && tsc --ignoreConfig --target ES2022 --module NodeNext --moduleResolution NodeNext --types node --skipLibCheck --strict --outDir node_modules/.cache/loopx-capability-configuration-smoke smoke/capability-configuration-smoke.ts src/data/capability-configuration.ts && node node_modules/.cache/loopx-capability-configuration-smoke/smoke/capability-configuration-smoke.js", "smoke:chat-model-menu": "vite build --ssr smoke/chat-model-menu-smoke.mjs --outDir node_modules/.cache/chat-model-menu --emptyOutDir && node node_modules/.cache/chat-model-menu/chat-model-menu-smoke.js", "smoke:chat-route": "vite build --ssr smoke/chat-route-smoke.ts --outDir node_modules/.cache/loopx-chat-route-smoke --emptyOutDir && node node_modules/.cache/loopx-chat-route-smoke/chat-route-smoke.js", + "smoke:manager-goal-routing": "vite build --ssr smoke/manager-goal-routing-smoke.ts --outDir node_modules/.cache/loopx-manager-goal-routing --emptyOutDir && node node_modules/.cache/loopx-manager-goal-routing/manager-goal-routing-smoke.js", + "smoke:manager-goal-routing-browser": "LOOPX_PLAYWRIGHT_PACKAGE=\"$PWD/node_modules/playwright\" node smoke/manager-goal-routing-browser.mjs", + "smoke:managed-goal-result-schema": "vite build --ssr smoke/managed-goal-result-schema-smoke.ts --outDir node_modules/.cache/managed-goal-result-schema --emptyOutDir && node node_modules/.cache/managed-goal-result-schema/managed-goal-result-schema-smoke.js", "smoke:chat-stream-stall": "vite build --ssr smoke/chat-stream-stall-smoke.ts --outDir node_modules/.cache/loopx-chat-stream-stall --emptyOutDir && node node_modules/.cache/loopx-chat-stream-stall/chat-stream-stall-smoke.js", "smoke:chat-turn-acceptance-retry": "vite build --ssr smoke/chat-turn-acceptance-retry-smoke.ts --outDir node_modules/.cache/loopx-chat-turn-acceptance-retry --emptyOutDir && node node_modules/.cache/loopx-chat-turn-acceptance-retry/chat-turn-acceptance-retry-smoke.js", "smoke:demo-readiness": "bash ../../../scripts/loopx-python.sh --exec ../../../examples/dashboard-demo-readiness-smoke.py", diff --git a/apps/presentation/dashboard/smoke/bound-conversation-smoke.ts b/apps/presentation/dashboard/smoke/bound-conversation-smoke.ts index e78e28c46b..861d002c3a 100644 --- a/apps/presentation/dashboard/smoke/bound-conversation-smoke.ts +++ b/apps/presentation/dashboard/smoke/bound-conversation-smoke.ts @@ -276,6 +276,18 @@ try { .map(row => new URL(row.path, origin)); child.stdin.write("history-extra\n"); const added = await next(); + globalThis.fetch = async (input, init) => { + const response = await routedRead(input, init); + if (!new URL(String(input), origin).searchParams.has("known_history")) return response; + const value = await response.json(); + assert.ok(value.messages.length > 0); + value.messages[0].text = null; + return new Response(JSON.stringify(value), {status: response.status}); + }; + const invalidSuffix = await fetchChatHistory(options, recovered); + assert.deepEqual(invalidSuffix.messages, [], "An invalid suffix must not publish a partial history"); + assert.deepEqual(invalidSuffix.unavailableSessionIds, [sessionId]); + globalThis.fetch = routedRead; calls.length = 0; const resumed = await fetchChatHistory(options, recovered); assert.equal(readRequests().length, 1, "A proven append must not download the old complete transcript"); diff --git a/apps/presentation/dashboard/smoke/conversation-find-browser-smoke.mjs b/apps/presentation/dashboard/smoke/conversation-find-browser-smoke.mjs index 46f2575ab1..43c164260b 100644 --- a/apps/presentation/dashboard/smoke/conversation-find-browser-smoke.mjs +++ b/apps/presentation/dashboard/smoke/conversation-find-browser-smoke.mjs @@ -28,8 +28,11 @@ try { browser = await launchBrowser(loadPlaywright().chromium); for (const locale of ["en", "zh-CN"]) { const zh = locale === "zh-CN"; + let corruptHistory = null, invalidReplies = 0; + const pageErrors = []; await command("fault"); workspace = await openWorkspacePage(browser, url, {beforeGoto(_api, page) { + page.on("pageerror", error => pageErrors.push(error.message)); return Promise.all([page.addInitScript(value => localStorage.setItem("loopx-pw-locale", value), locale), page.route("**/api/chat/sessions**", async route => { const request = route.request(), target = new URL(request.url()); @@ -42,6 +45,18 @@ try { } assert.equal(request.method(), "GET", "Finding original messages cannot mutate the store"); const response = await route.fetch({url: `${origin}${target.pathname}${target.search}`}); + if (corruptHistory && target.pathname === "/api/chat/sessions/old" && response.ok()) { + const snapshot = await response.json(); + assert.ok(snapshot.messages.length > 0); + if (corruptHistory === "text") snapshot.messages[0].text = null; + else if (corruptHistory === "attachments") snapshot.messages[0].attachments = "invalid attachments"; + else if (corruptHistory === "reply_context") snapshot.messages[0].reply_context = {source_text: "", parent: null}; + else if (corruptHistory === "collaborations") snapshot.messages[0].collaborations = "invalid collaborations"; + else snapshot.messages[0].return_delivery = {schema_version: "manager_return_delivery_status_v0", + phase: "conclusion", status: "delivered", request_revision: {invalid: true}}; + invalidReplies += 1; + return route.fulfill({status: response.status(), json: snapshot}); + } return route.fulfill({response}); })]); }}); @@ -56,7 +71,28 @@ try { await bar.getByText(zh ? "对话记录尚未完整读取,结果仅包含已加载消息。" : "History is incomplete; results include loaded messages only.").waitFor(); const retained = await active.getAttribute("data-timeline-id"); await command("recover"); - await page.getByRole("button", {name: zh ? "重试读取" : "Retry history", exact: true}).click(); + const retry = page.getByRole("button", {name: zh ? "重试读取" : "Retry history", exact: true}); + for (const field of ["text", "attachments", "reply_context", "collaborations", "return_delivery"]) { + corruptHistory = field; + const before = invalidReplies; + const invalidRead = page.waitForResponse(response => new URL(response.url()).pathname === "/api/chat/sessions/old"); + await retry.click(); + await (await invalidRead).finished(); + try { + await page.waitForFunction(label => [...document.querySelectorAll("button")].some(button => + button.textContent.trim() === label && !button.disabled), zh ? "重试读取" : "Retry history"); + } catch (error) { + throw new Error(`${field} history did not recover; page errors: ${pageErrors.join(" | ")}; console errors: ${workspace.errors.join(" | ")}`, {cause: error}); + } + assert.ok(invalidReplies > before, `The malformed ${field} response was exercised`); + await bar.getByText(zh ? "对话记录尚未完整读取,结果仅包含已加载消息。" : "History is incomplete; results include loaded messages only.").waitFor(); + assert.equal(await active.getAttribute("data-timeline-id"), retained, "Invalid history must not hide the current message"); + assert.deepEqual(pageErrors, [], "Invalid history must not crash the page"); + assert.deepEqual(workspace.errors.filter(error => !error.startsWith("Failed to load resource: the server responded with a status of 503")), [], + "Invalid history must not cause a render error"); + } + corruptHistory = null; + await retry.click(); await bar.getByRole("status").filter({hasText: "2 / 3"}).waitFor(); assert.equal(await active.getAttribute("data-timeline-id"), retained, "Backfill preserves the selected original message"); // Reopen the timeline after the complete read to exercise folded history. @@ -137,7 +173,7 @@ try { assert.deepEqual(await command("inspect"), {store_unchanged: true, turn_count: 0}); await workspace.close(); workspace = null; } - console.log("conversation-find: packaged HTTP/store, en/zh, full-text expansion, navigation/wrap, literal no-match, scope reset, narrow layout and zero writes passed"); + console.log("conversation-find: packaged HTTP/store, en/zh, malformed history recovery, full-text expansion, navigation/wrap, literal no-match, scope reset, narrow layout and zero writes passed"); } finally { await workspace?.close(); await browser?.close(); server?.kill("SIGTERM"); child.stdin.end(); if (child.exitCode === null) await exited; lines.close(); diff --git a/apps/presentation/dashboard/smoke/conversation-history-smoke.ts b/apps/presentation/dashboard/smoke/conversation-history-smoke.ts index faaf03c328..cfe82bbe9c 100644 --- a/apps/presentation/dashboard/smoke/conversation-history-smoke.ts +++ b/apps/presentation/dashboard/smoke/conversation-history-smoke.ts @@ -4,7 +4,7 @@ import { once } from "node:events"; import { resolve } from "node:path"; import { createInterface } from "node:readline"; import { resolveTestPython } from "../../../../scripts/test-python.mjs"; -import { fetchChatHistory, fetchChatSessionHistory } from "../src/data/chat"; +import { fetchChatHistory, fetchChatSession, fetchChatSessionHistory } from "../src/data/chat"; import { readConversationReturns } from "../src/data/conversation-return-observation"; import { conversationReturnSessions, reconcileConversationReturns } from "../src/data/conversation-returns"; @@ -116,6 +116,10 @@ try { (value: any) => {value.message_page.snapshot_sha256 = "0".repeat(64);}, (value: any) => {value.message_page.offset = 0;}, (value: any) => {value.messages[0].message_id = complete.messages[0].message_id;}, + (value: any) => {value.messages[0].text = null;}, + (value: any) => {value.messages[0].role = null;}, + (value: any) => {value.messages[0].created_at = 7;}, + (value: any) => {value.messages[0].turn_id = 7;}, (value: any) => {delete value.message_page;}, ]) { globalThis.fetch = async (input, init) => { @@ -126,6 +130,15 @@ try { }; await assert.rejects(() => fetchChatSessionHistory("old"), "Never accept mixed or partial pages"); } + globalThis.fetch = async (input, init) => { + const response = await routedFetch(input, init); + if (new URL(String(input), origin).searchParams.has("message_cursor")) return response; + const value = await response.json(); + assert.ok(value.messages.length > 0); + value.messages[0].text = null; + return new Response(JSON.stringify(value), {status: response.status}); + }; + await assert.rejects(() => fetchChatSessionHistory("old"), "A malformed first page must not be published"); globalThis.fetch = routedFetch; const abort = new AbortController(); abort.abort(); requests.length = 0; await assert.rejects(() => fetchChatSessionHistory("old", abort.signal)); @@ -145,6 +158,61 @@ try { // The existing full-snapshot service remains compatible on the first read. globalThis.fetch = async () => new Response(JSON.stringify(complete)); assert.deepEqual((await fetchChatSessionHistory(complete.session)).messages, complete.messages); + const collaboration = {schema_version: "collaboration_request_readback_v0", request_id: "request", agent_id: "agent", + read_status: "pending", decision: "pending", returns: [], + brief: {purpose: "purpose", context: "context", constraints: [], inputs: [], acceptance: [], return_requirement: "reply"}}; + const legacy = {...complete, messages: [{schema_version: "loopx_chat_message_v1", message_id: "legacy-id", + turn_id: null, role: "agent", text: "", created_at: "2025-01-01T00:00:00Z", extension: {preserved: true}, + attachments: [{id: "file", name: "note.txt", size: 0, mime_type: "text/plain", data_url: "data:text/plain;base64,"}], + reply_context: {source_text: "question", parent: {message_id: "parent", content: "quoted", complete: true}, + thread: {root_message_id: "root", thread_id: "thread", before_message_id: "legacy-id", complete: false, + messages: [{message_id: "earlier", content: "before", content_truncated: false, position: 0}], omitted_message_count: 0}}, + collaboration, collaborations: [collaboration], + return_delivery: {schema_version: "manager_return_delivery_status_v0", phase: "conclusion", status: "delivered", request_revision: 1}}]}; + globalThis.fetch = async () => new Response(JSON.stringify(legacy)); + assert.deepEqual((await fetchChatSessionHistory("old")).messages, legacy.messages); + assert.deepEqual((await fetchChatSession("old")).messages, legacy.messages); + for (const invalid of [ + {...legacy, session: {...legacy.session, session_id: "different-session"}}, + {...legacy, schema_version: "unsupported"}, + ]) { + globalThis.fetch = async () => new Response(JSON.stringify(invalid)); + await assert.rejects(() => fetchChatSession("old"), "Direct reads must match the requested Session and schema"); + } + const unversioned = structuredClone(legacy); + delete (unversioned.messages[0] as Partial).schema_version; + globalThis.fetch = async () => new Response(JSON.stringify(unversioned)); + assert.deepEqual((await fetchChatSessionHistory("old")).messages, unversioned.messages); + const nullAttachments = {...legacy, messages: [{...legacy.messages[0], attachments: null}]}; + globalThis.fetch = async () => new Response(JSON.stringify(nullAttachments)); + assert.deepEqual((await fetchChatSessionHistory("old")).messages, nullAttachments.messages); + const duplicates = {...legacy, messages: [legacy.messages[0], {...legacy.messages[0]}]}; + globalThis.fetch = async () => new Response(JSON.stringify(duplicates)); + await assert.rejects(() => fetchChatSessionHistory("old"), "Legacy history must reject duplicate message IDs"); + await assert.rejects(() => fetchChatSession("old"), "Direct reads must reject duplicate message IDs"); + const selectedTurn = {...legacy.messages[0], turn_id: "selected-turn"}; + const unrelated = {...legacy.messages[0], message_id: "other", turn_id: "other-turn", text: null}; + globalThis.fetch = async () => new Response(JSON.stringify({...legacy, messages: [selectedTurn, unrelated]})); + assert.deepEqual((await fetchChatSessionHistory("old", undefined, "selected-turn")).messages, [selectedTurn], + "An unrelated damaged legacy message must not hide the selected Turn"); + for (const invalid of [ + {...legacy.messages[0], schema_version: "unsupported"}, + {...legacy.messages[0], message_id: ""}, {...legacy.messages[0], role: null}, + {...legacy.messages[0], text: null}, {...legacy.messages[0], created_at: null}, + {...legacy.messages[0], turn_id: 7}, {...legacy.messages[0], attachments: "invalid"}, + {...legacy.messages[0], attachments: [{id: "file"}]}, + {...legacy.messages[0], reply_context: {source_text: "", parent: null}}, + {...legacy.messages[0], reply_context: {...legacy.messages[0].reply_context, thread: {messages: "invalid"}}}, + {...legacy.messages[0], collaborations: "invalid"}, + {...legacy.messages[0], collaboration: {...collaboration, brief: null}}, + {...legacy.messages[0], collaboration: {...collaboration, schema_version: "unsupported"}}, + {...legacy.messages[0], collaborations: [{...collaboration, returns: "invalid"}]}, + {...legacy.messages[0], return_delivery: {...legacy.messages[0].return_delivery, request_revision: {invalid: true}}}, null, + ]) { + globalThis.fetch = async () => new Response(JSON.stringify({...legacy, messages: [invalid]})); + await assert.rejects(() => fetchChatSessionHistory("old"), "Malformed legacy messages must not be published"); + await assert.rejects(() => fetchChatSession("old"), "Malformed direct messages must not be published"); + } globalThis.fetch = routedFetch; child.stdin.end("inspect\n"); assert.deepEqual(await next(), { store_unchanged: true, turn_count: 0 }); diff --git a/apps/presentation/dashboard/smoke/managed-goal-result-schema-smoke.ts b/apps/presentation/dashboard/smoke/managed-goal-result-schema-smoke.ts new file mode 100644 index 0000000000..71e6b2d3ba --- /dev/null +++ b/apps/presentation/dashboard/smoke/managed-goal-result-schema-smoke.ts @@ -0,0 +1,49 @@ +import assert from "node:assert/strict"; +import {fetchManagedGoalResults, readManagedGoalResult} from "../src/data/chat"; + +const goalId = "product-release", todoId = "todo_report", digest = "a".repeat(64); +const row = {todo_id: todoId, title: "Report", producer_agent_id: "lead", sha256: digest, + content_type: "text/markdown", size_bytes: 12, completed_at: null}; +const page = {ok: true, items: [row], total: 1, next_cursor: null, unavailable_count: 0, unavailable_todo_ids: []}; +const detail = {ok: true, goal_id: goalId, todo_id: todoId, text: "Report body", + result: {sha256: digest, producer_agent_id: "lead", content_type: "text/markdown"}}; + +const originalFetch = globalThis.fetch; +let payload: unknown; +globalThis.fetch = async () => Response.json(payload); +try { + payload = page; + assert.deepEqual(await fetchManagedGoalResults(goalId), page); + const emptyPage = {...page, items: [], total: 2, next_cursor: "opaque:40", unavailable_count: 1, unavailable_todo_ids: [todoId]}; + payload = emptyPage; + assert.deepEqual(await fetchManagedGoalResults(goalId, "opaque:0"), emptyPage); + const expanded = {...detail, result: {...detail.result, acceptance_contract_revision: 3}}; + payload = expanded; + assert.deepEqual(await readManagedGoalResult(goalId, todoId), expanded); + + for (const invalid of [ + {...detail, goal_id: "other-goal"}, {...detail, todo_id: "todo_other"}, + {...detail, text: 42}, {...detail, result: {...detail.result, sha256: "not-a-digest"}}, + {...detail, ok: false}, + ]) { + payload = invalid; + await assert.rejects(readManagedGoalResult(goalId, todoId), "invalid expanded result must fail closed"); + } + for (const invalid of [ + {...page, items: [{...row, todo_id: null}]}, + {...page, items: [{...row, sha256: "not-a-digest"}]}, + {...page, items: [{...row, size_bytes: -1}]}, + {...page, total: -1}, + {...page, total: 0}, + {...page, next_cursor: ""}, + {...page, unavailable_count: 1}, + {...page, unavailable_todo_ids: [42], unavailable_count: 1}, + {...page, ok: false}, + ]) { + payload = invalid; + await assert.rejects(fetchManagedGoalResults(goalId), "invalid inventory must fail closed"); + } +} finally { + globalThis.fetch = originalFetch; +} +console.log("managed goal results: page and exact readback validation passed"); diff --git a/apps/presentation/dashboard/smoke/manager-goal-routing-browser.mjs b/apps/presentation/dashboard/smoke/manager-goal-routing-browser.mjs new file mode 100644 index 0000000000..519a2f169e --- /dev/null +++ b/apps/presentation/dashboard/smoke/manager-goal-routing-browser.mjs @@ -0,0 +1,135 @@ +// Exercise the rendered conversation against the repository's disposable API fixture. +import assert from "node:assert/strict"; +import {createServer} from "node:net"; +import {once} from "node:events"; + +const probe = createServer(); +await new Promise(resolve => probe.listen(0, "127.0.0.1", resolve)); +const port = probe.address().port; +await new Promise(resolve => probe.close(resolve)); +process.env.LOOPX_PERSONAL_WORKSPACE_PORT = String(port); +const {packaged, startServer} = await import("../../../../examples/personal-workspace-browser/fixture.mjs"); +const {openWorkspacePage} = await import("../../../../examples/personal-workspace-browser/scenario-context.mjs"); +const {launchBrowser, loadPlaywright, waitForHttp} = await import("../../../../examples/dashboard-browser-smoke-support.mjs"); + +const server = await startServer(); +let browser; +try { + const url = `http://127.0.0.1:${port}/${packaged ? "chat/" : ""}?statusUrl=/status.json`; + await waitForHttp(url); + browser = await launchBrowser(loadPlaywright().chromium); + const localized = {newPage: options => browser.newPage({locale: "zh-CN", ...options})}; + const opened = await openWorkspacePage(localized, url, { + beforeGoto(api) { + api.transformStatus = fixture => { + const source = fixture.run_history.goals.find(goal => goal.id === "product-release"); + const goal = JSON.parse(JSON.stringify(source).replaceAll("product-release", "manager")); + goal.display_name = "Manager Goal"; + fixture.run_history.goals.push(goal); + }; + }, + }); + const {page, api} = opened; + try { + const requests = []; + page.on("request", request => { + if (request.method() === "POST" && new URL(request.url()).pathname === "/api/chat/sessions") { + requests.push(request.postDataJSON()); + } + }); + await page.getByRole("navigation", {name: "管家视图"}).getByRole("button", {name: /^(Chat|对话)$/}).click(); + const composer = page.getByLabel("向 LoopX 发送消息"); + await composer.fill("保留原管家草稿"); + await page.getByText("Manager Goal", {exact: true}).first().click(); + await page.getByRole("navigation", {name: "Goal 视图"}).getByRole("button", {name: /^(Chat|对话)$/}).click(); + assert.equal(await composer.inputValue(), "", "Goal must not inherit the steward draft"); + await composer.fill("仅发给同名 Goal"); + const beforeGoal = api.turnRequests.length; + await page.getByRole("button", {name: "发送", exact: true}).click(); + const goalDeadline = Date.now() + 10_000; + while (api.turnRequests.length === beforeGoal && Date.now() < goalDeadline) await page.waitForTimeout(25); + assert.equal(api.turnRequests.length, beforeGoal + 1, "Goal send must reach one Session"); + await page.locator(".personal-message-pending").waitFor({state: "hidden"}); + assert.ok(requests.some(body => body.context_kind === "goal" && body.goal_id === "manager")); + const goalSession = api.turnRequests.at(-1)?.sessionId; + assert.ok(goalSession); + await page.locator(".personal-manager-link").first().click(); + assert.equal(await composer.inputValue(), "保留原管家草稿", "the old steward draft stays intact"); + assert.equal(await page.getByText("仅发给同名 Goal", {exact: true}).count(), 0); + await composer.fill("仅发给管家"); + const beforeManager = api.turnRequests.length; + await page.getByRole("button", {name: "发送", exact: true}).click(); + const managerDeadline = Date.now() + 10_000; + while (api.turnRequests.length === beforeManager && Date.now() < managerDeadline) await page.waitForTimeout(25); + assert.equal(api.turnRequests.length, beforeManager + 1, "steward send must reach one Session"); + await page.locator(".personal-message-pending").waitFor({state: "hidden"}); + assert.ok(requests.some(body => body.context_kind === "manager" && !body.goal_id)); + const managerSession = api.turnRequests.at(-1)?.sessionId; + assert.ok(managerSession); + assert.notEqual(managerSession, goalSession, JSON.stringify({requests, turns: api.turnRequests})); + + // A reload must recover each channel from its own stored Session. + await page.reload({waitUntil: "domcontentloaded"}); + await page.getByRole("navigation", {name: "管家视图"}).getByRole("button", {name: /^(Chat|对话)$/}).click(); + await page.getByText("仅发给管家", {exact: true}).waitFor({state: "visible"}); + assert.equal(await page.getByText("仅发给同名 Goal", {exact: true}).count(), 0); + await page.getByText("Manager Goal", {exact: true}).first().click(); + await page.getByRole("navigation", {name: "Goal 视图"}).getByRole("button", {name: /^(Chat|对话)$/}).click(); + await page.getByText("仅发给同名 Goal", {exact: true}).waitFor({state: "visible"}); + assert.equal(await page.getByText("仅发给管家", {exact: true}).count(), 0); + assert.equal(api.turnRequests.length, beforeManager + 1, "recovery must not create another turn"); + + for (const [sessionId, kind] of [[goalSession, "goal"], [managerSession, "manager"]]) { + const turnId = `running-${kind}`; + const pattern = `**/api/chat/sessions/${sessionId}/turns/${turnId}/events`; + const held = []; + page.__loopxRuntime.turnMessages.set(turnId, `正在执行 ${kind}`); + page.__loopxRuntime.sessions.set(sessionId, {...page.__loopxRuntime.sessions.get(sessionId), active_turn_id: turnId, status: "busy"}); + await page.route(pattern, route => { held.push(route); }); + await page.reload({waitUntil: "domcontentloaded"}); + if (kind === "goal") { + await page.getByText("Manager Goal", {exact: true}).first().click(); + await page.getByRole("navigation", {name: "Goal 视图"}).getByRole("button", {name: /^(Chat|对话)$/}).click(); + } else { + await page.locator(".personal-manager-link").first().click(); + await page.getByRole("navigation", {name: "管家视图"}).getByRole("button", {name: /^(Chat|对话)$/}).click(); + } + const stop = page.getByRole("button", {name: "中断本轮", exact: true}); + await stop.waitFor({state: "visible", timeout: 10_000}); + const streamDeadline = Date.now() + 10_000; + while (!held.length && Date.now() < streamDeadline) await page.waitForTimeout(25); + assert.ok(held.length, `${kind} must adopt the running Session before interruption`); + await stop.click(); + const deadline = Date.now() + 10_000; + while (!api.interrupts.some(row => row.sessionId === sessionId && row.turnId === turnId) && Date.now() < deadline) await page.waitForTimeout(25); + assert.deepEqual(api.interrupts.at(-1), {sessionId, turnId}, `${kind} interrupt must reach its own Session; alerts=${JSON.stringify(await page.getByRole("alert").allTextContents())}`); + await page.unroute(pattern); + await Promise.all(held.map(route => route.abort().catch(() => {}))); + } + + await page.getByText("Manager Goal", {exact: true}).first().click(); + await page.getByRole("navigation", {name: "Goal 视图"}).getByRole("button", {name: /^(Chat|对话)$/}).click(); + await page.getByRole("button", {name: "开启 LoopX 模式", exact: true}).click(); + await page.getByLabel("已注册的协调身份").selectOption("lead"); + await page.getByLabel("协调员总 token 额度").fill("100000"); + await page.getByRole("button", {name: "保存设置", exact: true}).click(); + await page.locator(".goal-loopx-mode-settings").waitFor({state: "detached"}); + const beforeStart = api.loopxModeRequests.filter(row => row.operation === "start").length; + await page.getByRole("button", {name: "开启 LoopX 模式", exact: true}).click(); + const startDeadline = Date.now() + 10_000; + while (api.loopxModeRequests.filter(row => row.operation === "start").length === beforeStart && Date.now() < startDeadline) await page.waitForTimeout(25); + const starts = api.loopxModeRequests.filter(row => row.operation === "start"); + assert.equal(starts.length, beforeStart + 1, "starting LoopX must submit one request"); + assert.equal(starts.at(-1)?.sessionId, goalSession, "LoopX must start in the Goal Session"); + console.log(`manager-goal browser (${packaged ? "packaged" : "development"}): draft, session, send, reload, interruption and LoopX isolation passed`); + } finally { + await opened.close(); + } +} finally { + await browser?.close(); + if (server.exitCode === null && server.signalCode === null) { + const exited = once(server, "exit"); + server.kill("SIGTERM"); + await exited; + } +} diff --git a/apps/presentation/dashboard/smoke/manager-goal-routing-smoke.ts b/apps/presentation/dashboard/smoke/manager-goal-routing-smoke.ts new file mode 100644 index 0000000000..722c729649 --- /dev/null +++ b/apps/presentation/dashboard/smoke/manager-goal-routing-smoke.ts @@ -0,0 +1,35 @@ +import assert from "node:assert/strict"; +import {conversationChannelId, conversationContextKey, conversationContextOfKey, createChatSession} from "../src/data/chat"; +import {conversationInputKey} from "../src/features/personal-workspace/use-conversation-input-state"; + +const manager = {kind: "manager"} as const; +const goal = {kind: "goal", goalId: "manager"} as const; +const ordinaryGoal = {kind: "goal", goalId: "another-goal"} as const; +const managerKey = conversationContextKey(manager); +const goalKey = conversationContextKey(goal); +assert.notEqual(goalKey, managerKey, "a Goal named manager must not share the steward's context key"); +assert.equal(managerKey, "manager", "existing steward drafts keep their key"); +assert.equal(conversationContextKey(ordinaryGoal), ordinaryGoal.goalId, "other Goal keys stay compatible"); +assert.deepEqual(conversationContextOfKey(ordinaryGoal.goalId), ordinaryGoal); +assert.deepEqual(conversationContextOfKey(goalKey), goal); +assert.deepEqual(conversationContextOfKey(managerKey), manager); +assert.notEqual(conversationInputKey(goalKey, "codex", false), conversationInputKey(managerKey, "codex", false)); +assert.equal(conversationChannelId(goal), "goal.manager"); +assert.equal(conversationChannelId(manager), "manager"); + +const originalFetch = globalThis.fetch; +const requests: Array> = []; +globalThis.fetch = async (_input, init) => { + requests.push(JSON.parse(String(init?.body))); + return Response.json({ok: true}); +}; +try { + await createChatSession(conversationContextOfKey(goalKey), "codex"); + await createChatSession(conversationContextOfKey(managerKey), "codex"); + assert.equal(requests[0].context_kind, "goal"); + assert.equal(requests[0].goal_id, "manager"); + assert.equal(requests[1].context_kind, "manager"); +} finally { + globalThis.fetch = originalFetch; +} +console.log("manager-goal-routing: separate contexts, drafts and session requests passed"); diff --git a/apps/presentation/dashboard/smoke/team-evidence-return-smoke.mjs b/apps/presentation/dashboard/smoke/team-evidence-return-smoke.mjs index 836db72719..b2047edad7 100644 --- a/apps/presentation/dashboard/smoke/team-evidence-return-smoke.mjs +++ b/apps/presentation/dashboard/smoke/team-evidence-return-smoke.mjs @@ -42,6 +42,29 @@ try { const evidence = dialog.getByRole("region", {name: "执行证据"}); const original = () => evidence.getByLabel("证据内容: report.json"); await original().waitFor(); + const mismatchedReadback = route => route.request().postDataJSON()?.operation === "read" + ? route.fulfill({json: {...legacyReadback, operation_id: "another-operation"}}) : route.fallback(); + await page.route("**/api/chat/sessions/*/loopx", mismatchedReadback); + await evidence.getByRole("button", {name: "重新检查交付", exact: true}).click(); + await evidence.getByRole("alert").waitFor({timeout: 3000}); + assert.equal(await original().count(), 0); + await page.unroute("**/api/chat/sessions/*/loopx", mismatchedReadback); + await evidence.getByRole("button", {name: "重新检查交付", exact: true}).click(); + await original().waitFor(); + await dialog.getByRole("button", {name: "返回执行列表", exact: true}).click(); + await dialog.locator('.goal-team-record[data-state="accepted"]').first().waitFor(); + const malformedInventory = route => route.request().postDataJSON()?.operation === "operations" + ? route.fulfill({json: {items: [], has_more: false, next_cursor: null, page_readback_complete: "false"}}) + : route.fallback(); + await page.route("**/api/chat/sessions/*/loopx", malformedInventory); + await dialog.getByRole("button", {name: "重新核验", exact: true}).click(); + await dialog.getByRole("alert").waitFor({timeout: 3000}); + assert.equal(await dialog.locator('.goal-team-record[data-state="accepted"]').count(), 0); + await page.unroute("**/api/chat/sessions/*/loopx", malformedInventory); + await dialog.getByRole("button", {name: "重新核验", exact: true}).click(); + await dialog.locator('.goal-team-record[data-state="accepted"]').first().waitFor(); + await openEvidence.first().click(); + await original().waitFor(); // Rechecking the same version must preserve the reader's exact comparison. await evidence.getByRole("button", {name: /^修订依据/}).click(); await evidence.getByLabel("对照产物", {exact: false}).selectOption({label: "report.md"}); diff --git a/apps/presentation/dashboard/smoke/team-report-smoke.mjs b/apps/presentation/dashboard/smoke/team-report-smoke.mjs index 412daf425c..d0e8efd84b 100644 --- a/apps/presentation/dashboard/smoke/team-report-smoke.mjs +++ b/apps/presentation/dashboard/smoke/team-report-smoke.mjs @@ -3,6 +3,7 @@ import {createElement} from "react"; import {renderToStaticMarkup} from "react-dom/server"; import {MarkdownText} from "../src/features/personal-workspace/markdown.tsx"; import {TeamArtifactContent} from "../src/features/personal-workspace/team-artifact-content.tsx"; +import {adoptLoopXTeamWork, fetchLoopXTeamWork, readLoopXTeamWork, revalidateLoopXTeamWork} from "../src/data/chat.ts"; const text = '# Research\n\n| Measure | Value |\n|---|---:|\n| **Cash** | 75 |\n| Escaped \\| label | 25 |\n\n[Source](https://example.org/report)\n\n[Unsafe](javascript:alert(1))\n![Tracking](https://example.org/pixel)'; const artifact = {ref: "report.md", sha256: "a".repeat(64), text}; @@ -74,3 +75,50 @@ assert.doesNotMatch(boundary, /href="(?:javascript:|ftp:|mailto:)/); const json = renderToStaticMarkup(createElement(TeamArtifactContent, {artifact: {...artifact, ref: "report.json"}, label: "JSON"})); assert.doesNotMatch(json, //, "File suffix does not turn JSON evidence into a report"); console.log("report rendering: readable table, raw fidelity, safe links and inert HTML, chat parity passed"); + +const readback = {operation_id: "source", request_id: "request", agent_id: "agent", todo_id: null, + status: "accepted", worker_active: false, recovery_required: false, artifacts: [artifact], extension: {preserved: true}}; +const originalFetch = globalThis.fetch; +try { + let response = readback; + let operation; + globalThis.fetch = async (_url, init) => { + assert.equal(JSON.parse(init.body).operation, operation); + return new Response(JSON.stringify(response)); + }; + for (const [name, read] of [ + ["read", () => readLoopXTeamWork("session", "source")], + ["revalidate", () => revalidateLoopXTeamWork("session", "source")], + ["adopt", () => adoptLoopXTeamWork("session", "source", "consumer")], + ]) { + operation = name; + for (const invalid of [ + {...readback, ok: false}, {...readback, operation_id: "other"}, {...readback, request_id: null}, + {...readback, artifacts: [{...artifact, text: null}]}, {...readback, dependencies: "invalid"}, + {...readback, current_use: {state: "unknown"}}, + {...readback, adoptions: [{consumer_operation_id: "consumer", source_artifacts: null}]}, + ]) { + response = invalid; + await assert.rejects(read, /cannot be verified/); + } + response = readback; + assert.deepEqual(await read(), readback, `${name} retries keep the complete original readback`); + } + operation = "operations"; + const row = {record_id: "record", operation_id: "source", status: "accepted", recovery_required: false, + artifacts: [{ref: artifact.ref, sha256: artifact.sha256}]}; + const page = {items: [row], has_more: true, next_cursor: "next", page_readback_complete: true, extension: "preserved"}; + for (const invalid of [ + {...page, page_readback_complete: "false"}, {...page, has_more: "false"}, {...page, items: "invalid"}, + {...page, items: [{...row, recovery_required: "false"}]}, {...page, next_cursor: null}, + {...page, items: [{...row, status: "unavailable", recovery_required: null}]}, + {...page, schema_version: "unsupported"}, {...page, ok: false}, + ]) { + response = invalid; + await assert.rejects(() => fetchLoopXTeamWork("session"), /cannot be verified/); + } + response = page; + assert.deepEqual(await fetchLoopXTeamWork("session"), page); + response = {...page, has_more: false, next_cursor: null}; + assert.deepEqual(await fetchLoopXTeamWork("session", "next"), response, "A later page remains readable"); +} finally {globalThis.fetch = originalFetch;} diff --git a/apps/presentation/dashboard/smoke/team-task-revalidation-smoke.mjs b/apps/presentation/dashboard/smoke/team-task-revalidation-smoke.mjs index a4fa4476c4..a9d881d9e8 100644 --- a/apps/presentation/dashboard/smoke/team-task-revalidation-smoke.mjs +++ b/apps/presentation/dashboard/smoke/team-task-revalidation-smoke.mjs @@ -36,14 +36,12 @@ try { const configured = api.loopxModeRequests.findLast(row => row.operation === "configure"); Object.assign(page.__loopxRuntime.loopxModes.get(configured.sessionId), {enabled: true, paused: false, active_turn_id: "fixture-loopx-turn", native: {status: "active", tokenBudget: 100000}}); - let selected = "failure-1"; await page.route("**/api/chat/sessions/*/loopx", async route => { const body = route.request().postDataJSON(); - if (!["read", "revalidate"].includes(body?.operation)) return route.fallback(); + if (!["operations", "read", "revalidate"].includes(body?.operation)) return route.fallback(); console.log(`HTTP ${body.operation}: ${body.operation_id}`); const started = Date.now(); - const response = await page.request.post(`${origin}/api/chat/sessions/recovery/loopx`, {data: { - operation: body.operation, operation_id: ["accepted-analysis", "synthesis-1"].includes(body.operation_id) ? selected : body.operation_id}}); + const response = await page.request.post(`${origin}/api/chat/sessions/recovery/loopx`, {data: body}); const {status, worker_active, recovery_required} = await response.json(); console.log(`HTTP ${body.operation} returned ${response.status()} in ${Date.now() - started}ms`, {status, worker_active, recovery_required}); diff --git a/apps/presentation/dashboard/src/data/chat.ts b/apps/presentation/dashboard/src/data/chat.ts index 8488f528ef..a83038e9be 100644 --- a/apps/presentation/dashboard/src/data/chat.ts +++ b/apps/presentation/dashboard/src/data/chat.ts @@ -1,6 +1,6 @@ import {normalizeWorkYardstickCapture} from "../../../../../loopx/control_plane/work_items/yardstick_decision.js"; import {sourceReadApiUrl} from "./local-status-query"; -import type {OracleEvidence} from "./goal-acceptance-observation"; +import {oracleEvidenceSchema, type OracleEvidence} from "./goal-acceptance-observation"; import { HANDOFF_MODES, EXECUTION_HANDOFF_MODES, type ExecutionHandoffMode } from "../../../../../loopx/control_plane/coordination/handoff_mode_vocabulary.js"; import { normalizeGoalDraft } from "../../../../../loopx/control_plane/collaboration/goal_draft.js"; import { parseTurnStep, type TurnStep } from "./turn-steps.js"; @@ -716,16 +716,19 @@ export function conversationSupportsStatusProjection(context: ConversationContex // Goal ids are single path segments, so a key containing "/" never names a Goal. const PROJECT_CONVERSATION_KEY_PREFIX = "project/"; +const MANAGER_GOAL_CONVERSATION_KEY = "goal/manager"; export function conversationContextKey(context: ConversationContext): string { if (context.kind === "manager") return "manager"; if (context.kind === "managed") return `managed/${encodeURIComponent(context.goalId)}/${encodeURIComponent(context.agentId)}/${encodeURIComponent(context.sessionId)}`; if (context.kind === "bound") return `bound/${encodeURIComponent(context.bindingId)}/${encodeURIComponent(context.sessionId)}`; - return context.kind === "goal" ? context.goalId : `${PROJECT_CONVERSATION_KEY_PREFIX}${context.projectRef}`; + if (context.kind === "goal") return context.goalId === "manager" ? MANAGER_GOAL_CONVERSATION_KEY : context.goalId; + return `${PROJECT_CONVERSATION_KEY_PREFIX}${context.projectRef}`; } export function conversationContextOfKey(key: string): ConversationContext { if (key === "manager") return { kind: "manager" }; + if (key === MANAGER_GOAL_CONVERSATION_KEY) return { kind: "goal", goalId: "manager" }; if (key.startsWith("managed/")) { const [, goalId = "", agentId = "", sessionId = ""] = key.split("/"); return {kind: "managed", goalId: decodeURIComponent(goalId), agentId: decodeURIComponent(agentId), sessionId: decodeURIComponent(sessionId)}; @@ -968,8 +971,57 @@ export type ChatSessionSnapshot = { }; }; +const chatReplyContextSchema = z.object({ + source_text: z.string(), + parent: z.object({message_id: z.string(), content: z.string(), complete: z.boolean()}).passthrough(), + thread: z.object({ + omitted_message_count: z.number().nullish(), + messages: z.array(z.object({message_id: z.string(), content: z.string(), content_truncated: z.boolean(), + sender: z.object({kind: z.string()}).passthrough().nullish(), created_at: z.string().nullish()}).passthrough()), + }).passthrough().nullish(), +}).passthrough(); + +const chatCollaborationSchema = z.object({ + schema_version: z.literal("collaboration_request_readback_v0").optional(), + request_id: z.string(), agent_id: z.string(), goal_id: z.string().nullish(), + request_revision: z.number().nullish(), decision_reason: z.string().nullish(), + brief: z.object({ + purpose: z.string(), context: z.string(), constraints: z.array(z.string()), + inputs: z.array(z.object({ref: z.string(), description: z.string(), sha256: z.string().nullish()}).passthrough()), + acceptance: z.array(z.string()), return_requirement: z.string(), + }).passthrough(), + returns: z.array(z.object({phase: z.string(), status: z.string()}).passthrough()), +}).passthrough(); + +const chatVisibleMessagesSchema = z.array(z.object({ + schema_version: z.literal("loopx_chat_message_v1").optional(), + message_id: z.string().min(1), turn_id: z.string().nullable(), role: z.string(), + text: z.string(), created_at: z.string(), + attachments: z.array(z.object({id: z.string(), name: z.string(), size: z.number(), + mime_type: z.string(), data_url: z.string()}).passthrough()).nullish(), + reply_context: chatReplyContextSchema.nullish(), + collaboration: chatCollaborationSchema.nullish(), + collaborations: z.array(chatCollaborationSchema).nullish(), + return_delivery: z.object({schema_version: z.literal("manager_return_delivery_status_v0"), + phase: z.enum(["decision", "conclusion"]), status: z.string(), request_revision: z.number().int().nonnegative().nullish(), + error: z.string().nullish(), verification: z.string().nullish(), + created_at: z.string().nullish(), delivered_at: z.string().nullish()}).passthrough().nullish(), +}).passthrough()); + +function requireValidChatMessages(messages: unknown): void { + const parsed = chatVisibleMessagesSchema.safeParse(messages); + if (!parsed.success || new Set(parsed.data.map(message => message.message_id)).size !== parsed.data.length) { + throw new Error("The original Session messages could not be verified."); + } +} + export async function fetchChatSession(sessionId: string, signal?: AbortSignal) { - return requestJson(`/api/chat/sessions/${encodeURIComponent(sessionId)}`, { signal }); + const snapshot = await requestJson(`/api/chat/sessions/${encodeURIComponent(sessionId)}`, { signal }); + if (snapshot?.ok !== true || snapshot.schema_version !== "loopx_chat_store_v1" || snapshot.session?.session_id !== sessionId) { + throw new Error("The original Session scope changed during history read."); + } + requireValidChatMessages(snapshot.messages); + return snapshot; } export async function fetchSavedChatAnswer(sessionId: string, messageId: string, signal: AbortSignal, turnId?: string) { @@ -1174,9 +1226,12 @@ async function readChatSessionHistory(session: ChatSessionSummary | string, sign // page cannot silently lose the cursor contract and masquerade as complete. if (!Object.hasOwn(snapshot, "message_page")) { if (cursor !== null) throw new Error("The history page contract changed during read."); + const legacyMessages = turnId === undefined ? snapshot.messages : snapshot.messages.filter(row => row.turn_id === turnId); + requireValidChatMessages(legacyMessages); return {...snapshot, verifiedHistoryBasis: undefined, - messages: turnId === undefined ? snapshot.messages : snapshot.messages.filter(row => row.turn_id === turnId)}; + messages: legacyMessages}; } + requireValidChatMessages(snapshot.messages); const parsed = chatMessagePageSchema.safeParse(snapshot.message_page); if (!parsed.success) throw new Error("The original history page could not be verified."); const page = parsed.data, end = messages.length + snapshot.messages.length; @@ -1245,6 +1300,7 @@ async function tryResumeChatSessionHistory(session: ChatSessionSummary, saved: C || !historyBasisMatches(proof.data.next, saved.messages.length + snapshot.messages.length)) { throw new Error("The original history suffix could not be verified."); } + requireValidChatMessages(snapshot.messages); const ids = new Set(saved.messages.map(message => message.message_id)); for (const message of snapshot.messages) { if (!message || typeof message.message_id !== "string" || !message.message_id || ids.has(message.message_id)) { @@ -1840,43 +1896,87 @@ export type DelegationReadback = { blocking_operation_id?: string; blocking_input_ref?: string; path?: string[]}; dependencies?: DelegationDependency[]; adoptions?: DelegationAdoption[]; }; +const delegationArtifactVersionSchema = z.object({ref: z.string().min(1), sha256: z.string().regex(/^[a-f0-9]{64}$/)}); +const delegationReadbackSchema = z.object({ + ok: z.literal(true).optional(), + operation_id: z.string().min(1), request_id: z.string().min(1), agent_id: z.string().min(1), + todo_id: z.string().nullable(), status: z.string().min(1), worker_active: z.boolean(), recovery_required: z.boolean(), + artifacts: z.array(delegationArtifactVersionSchema.extend({text: z.string()})).optional(), error: z.string().optional(), + validation: z.object({source: z.enum(["goal_acceptance", "todo_validation", "request_validation"]), + basis_sha256: z.string(), check_count: z.number().int().nonnegative(), pinned_file_count: z.number().int().nonnegative(), + checked_at: z.string().optional(), output_versions: z.array(delegationArtifactVersionSchema).optional(), + oracle_checks: z.array(oracleEvidenceSchema.extend({criterion_id: z.string()})).optional()}).optional(), + task_failure: z.object({recovery_kind: z.enum(["repair_required", "replan_required"]), + summary: z.string(), turn_key: z.string()}).optional(), + current_use: z.object({state: z.enum(["current", "unavailable"]), + reason: z.string().optional(), blocking_operation_id: z.string().optional(), + blocking_input_ref: z.string().optional(), path: z.array(z.string()).optional()}).optional(), + dependencies: z.array(delegationArtifactVersionSchema.extend({operation_id: z.string(), input_ref: z.string(), + relation: z.enum(["responds_to", "revises", "uses"]), state: z.enum(["current", "unavailable"])})).optional(), + adoptions: z.array(z.object({requester_agent_id: z.string(), consumer_operation_id: z.string(), + consumer_request_id: z.string(), consumer_agent_id: z.string(), consumer_todo_id: z.string().nullable(), + state: z.enum(["current", "unavailable"]), source_artifacts: z.array(delegationArtifactVersionSchema), + consumer_artifacts: z.array(delegationArtifactVersionSchema)})).optional(), +}); +function requireDelegationReadback(readback: unknown, operationId: string): DelegationReadback { + const parsed = delegationReadbackSchema.safeParse(readback); + if (!parsed.success || parsed.data.operation_id !== operationId) { + throw new Error("执行详情无法核验,请重新读取 / Execution details cannot be verified; retry"); + } + return readback as DelegationReadback; +} export function readLoopXTeamWork(sessionId: string, operationId: string) { - return requestJson(`/api/chat/sessions/${sessionId}/loopx`, { + return requestJson(`/api/chat/sessions/${sessionId}/loopx`, { method: "POST", body: JSON.stringify({operation: "read", operation_id: operationId}), - }); + }).then(readback => requireDelegationReadback(readback, operationId)); } export function adoptLoopXTeamWork(sessionId: string, operationId: string, consumerOperationId: string) { - return requestJson(`/api/chat/sessions/${sessionId}/loopx`, { + return requestJson(`/api/chat/sessions/${sessionId}/loopx`, { method: "POST", body: JSON.stringify({operation: "adopt", operation_id: operationId, consumer_operation_id: consumerOperationId}), - }); + }).then(readback => requireDelegationReadback(readback, operationId)); } export function revalidateLoopXTeamWork(sessionId: string, operationId: string) { - return requestJson(`/api/chat/sessions/${sessionId}/loopx`, { + return requestJson(`/api/chat/sessions/${sessionId}/loopx`, { method: "POST", body: JSON.stringify({operation: "revalidate", operation_id: operationId}), - }); + }).then(readback => requireDelegationReadback(readback, operationId)); } -export type ManagedGoalResultRow = { - todo_id: string; title: string; producer_agent_id: string; sha256: string; - content_type: string; size_bytes: number; completed_at?: string | null; -}; -export type ManagedGoalResultPage = { - ok: true; items: ManagedGoalResultRow[]; total: number; next_cursor: string | null; - unavailable_count: number; unavailable_todo_ids: string[]; -}; -export type ManagedGoalResultRead = { - ok: true; goal_id: string; todo_id: string; text: string; - result: {sha256: string; content_type: string; producer_agent_id: string}; -}; -export function fetchManagedGoalResults(goalId: string, cursor?: string) { +const managedResultIdentitySchema = z.object({ + sha256: z.string().regex(/^[a-f0-9]{64}$/), + content_type: z.string().min(1), producer_agent_id: z.string().min(1), +}).passthrough(); +const managedGoalResultRowSchema = managedResultIdentitySchema.extend({ + todo_id: z.string().min(1), title: z.string().min(1), size_bytes: z.number().int().nonnegative(), + completed_at: z.string().nullable().optional(), +}); +const managedGoalResultPageSchema = z.object({ + ok: z.literal(true), items: z.array(managedGoalResultRowSchema), total: z.number().int().nonnegative(), + next_cursor: z.string().min(1).nullable(), unavailable_count: z.number().int().nonnegative(), + unavailable_todo_ids: z.array(z.string().min(1)), +}).passthrough().refine(page => page.unavailable_count === page.unavailable_todo_ids.length + && page.items.length + page.unavailable_count <= page.total); +const managedGoalResultReadSchema = z.object({ + ok: z.literal(true), goal_id: z.string().min(1), todo_id: z.string().min(1), text: z.string(), + result: managedResultIdentitySchema, +}).passthrough(); +export type ManagedGoalResultRow = z.infer; +export type ManagedGoalResultPage = z.infer; +export type ManagedGoalResultRead = z.infer; +export async function fetchManagedGoalResults(goalId: string, cursor?: string) { const params = new URLSearchParams({goal_id: goalId}); if (cursor) params.set("cursor", cursor); - return requestJson(`/api/chat/goal-results?${params}`); + const page = managedGoalResultPageSchema.safeParse(await requestJson(`/api/chat/goal-results?${params}`)); + if (!page.success) throw new Error("报告列表响应不完整 / Report inventory response is incomplete"); + return page.data; } -export function readManagedGoalResult(goalId: string, todoId: string) { - return requestJson( +export async function readManagedGoalResult(goalId: string, todoId: string) { + const response = managedGoalResultReadSchema.safeParse(await requestJson( `/api/chat/goal-results/${encodeURIComponent(todoId)}?goal_id=${encodeURIComponent(goalId)}`, - ); + )); + if (!response.success || response.data.goal_id !== goalId || response.data.todo_id !== todoId) { + throw new Error("报告详情响应不完整或不匹配 / Report detail response is incomplete or mismatched"); + } + return response.data; } export type DelegationState = "unavailable" | "accepted" | "rejected" | "recovery_required" | "stopped" | "executing" | "validating" | "dispatched" | "unknown"; @@ -1912,10 +2012,24 @@ export function delegationStateLabel(row: DelegationStateFacts, zh: boolean) { const label = DELEGATION_STATE_LABELS[delegationState(row)]; return zh ? label.zh : label.en; } -export function fetchLoopXTeamWork(sessionId: string, cursor?: string) { - return requestJson(`/api/chat/sessions/${sessionId}/loopx`, { +const delegationInventorySchema = z.object({ + ok: z.literal(true).optional(), schema_version: z.literal("loopx_delegation_inventory_v0").optional(), + items: z.array(z.object({record_id: z.string().min(1), operation_id: z.string().min(1).nullable(), + agent_id: z.string().optional(), todo_id: z.string().nullable().optional(), status: z.string().min(1), + worker_active: z.boolean().optional(), recovery_required: z.boolean().nullable(), + artifacts: z.array(delegationArtifactVersionSchema).optional(), + current_use: z.object({state: z.enum(["current", "unavailable"])}).optional()})), + has_more: z.boolean(), next_cursor: z.string().min(1).nullable(), page_readback_complete: z.boolean(), +}).refine(page => page.has_more === (page.next_cursor !== null) + && (!page.page_readback_complete || page.items.every(row => row.status !== "unavailable"))); +export async function fetchLoopXTeamWork(sessionId: string, cursor?: string) { + const inventory = await requestJson(`/api/chat/sessions/${sessionId}/loopx`, { method: "POST", body: JSON.stringify({operation: "operations", limit: 10, ...(cursor ? {cursor} : {})}), }); + if (!delegationInventorySchema.safeParse(inventory).success) { + throw new Error("执行列表无法核验,请重新读取 / Execution inventory cannot be verified; retry"); + } + return inventory as DelegationInventory; } export function inspectLoopXMember(sessionId: string, bindingId: string) { return requestJson(`/api/chat/sessions/${sessionId}/loopx`, { diff --git a/apps/presentation/dashboard/src/features/personal-workspace/goal-managed-results.tsx b/apps/presentation/dashboard/src/features/personal-workspace/goal-managed-results.tsx index f51e4442c7..a87cd6747d 100644 --- a/apps/presentation/dashboard/src/features/personal-workspace/goal-managed-results.tsx +++ b/apps/presentation/dashboard/src/features/personal-workspace/goal-managed-results.tsx @@ -35,8 +35,7 @@ export function GoalManagedResults({goalId, zh, hideEmpty = false, catalogOnly = async function read(row: ManagedGoalResultRow, current: number, focusReader = false) { const result = await readManagedGoalResult(goalId, row.todo_id); if (current !== generation.current) return; - if (result.todo_id !== row.todo_id || result.goal_id !== goalId || - result.result.sha256 !== row.sha256 || result.result.producer_agent_id !== row.producer_agent_id || + if (result.result.sha256 !== row.sha256 || result.result.producer_agent_id !== row.producer_agent_id || result.result.content_type !== row.content_type) { throw new Error(zh ? "报告版本或验收已变化" : "Report version or acceptance changed"); } @@ -55,13 +54,6 @@ export function GoalManagedResults({goalId, zh, hideEmpty = false, catalogOnly = try { const next = await fetchManagedGoalResults(goalId, cursor); if (current !== generation.current) return; - if (!Array.isArray(next.items) || !Number.isInteger(next.total) || - !Number.isInteger(next.unavailable_count) || - !Array.isArray(next.unavailable_todo_ids) || - next.unavailable_count !== next.unavailable_todo_ids.length || - (next.next_cursor !== null && typeof next.next_cursor !== "string")) { - throw new Error(zh ? "报告列表响应不完整" : "Report inventory response is incomplete"); - } setPage(next); inventoryRead = true; const previous = chosen.current; diff --git a/apps/presentation/dashboard/src/features/personal-workspace/manager-team-result.tsx b/apps/presentation/dashboard/src/features/personal-workspace/manager-team-result.tsx index 4d3910f688..6f6df67f7f 100644 --- a/apps/presentation/dashboard/src/features/personal-workspace/manager-team-result.tsx +++ b/apps/presentation/dashboard/src/features/personal-workspace/manager-team-result.tsx @@ -171,20 +171,10 @@ async function collectManagedGoalIndex(goalId: string): Promise, force const row = matched.values().next().value; if (!row) return {kind: "waiting"}; const read = await readManagedGoalResult(goalId, row.todo_id); - if (read.goal_id !== goalId || read.todo_id !== row.todo_id || - read.result.sha256 !== row.sha256 || read.result.producer_agent_id !== row.producer_agent_id || - read.result.content_type !== row.content_type || typeof read.text !== "string") return {kind: "unavailable"}; + if (read.result.sha256 !== row.sha256 || read.result.producer_agent_id !== row.producer_agent_id || + read.result.content_type !== row.content_type) return {kind: "unavailable"}; return {kind: "accepted", artifact: managedReportArtifact(row.content_type, row.sha256, read.text), agentId: row.producer_agent_id, title: row.title}; } catch { /* A failed inventory or exact read cannot keep an earlier report visible. */ } diff --git a/apps/presentation/dashboard/src/features/personal-workspace/personal-workspace-page.tsx b/apps/presentation/dashboard/src/features/personal-workspace/personal-workspace-page.tsx index a87734025c..7accdcbc43 100644 --- a/apps/presentation/dashboard/src/features/personal-workspace/personal-workspace-page.tsx +++ b/apps/presentation/dashboard/src/features/personal-workspace/personal-workspace-page.tsx @@ -1061,7 +1061,7 @@ export function PersonalWorkspacePage({ } const conversationKey = workspaceConversations?.managed ? conversationContextKey({kind: "managed", ...workspaceConversations.managed.target}) : originalConversation ? conversationContextKey({ kind: "bound", ...originalConversation.target }) : selectedWorkspaceRef ? conversationContextKey({ kind: "project", projectRef: selectedWorkspaceRef }) - : selectedGoalId ?? "manager"; + : conversationContextKey(selectedGoalId != null ? {kind: "goal", goalId: selectedGoalId} : {kind: "manager"}); const composerDraftKey = conversationInputKey(conversationKey, selectedAgentId, managedWorkActive || Boolean(originalConversation)); const [modelSettingsSaving, setModelSettingsSaving] = useState>({}); async function saveModelSettings(change: () => void | Promise) { @@ -1436,7 +1436,6 @@ export function PersonalWorkspacePage({ // Draft edits do not change history. A retained history action still calls // the latest committed handler in its own conversation. const currentTimelineActions = useRef(null); - const timelineActionTarget = agentWorkActive ? conversationKey : selectedGoal?.goalId ?? conversationKey; const canCancelTimelineTurn = !readOnly && Boolean(callbacks.onCancelConversationPreparation); const canInterruptTimelineTurn = !readOnly && Boolean(callbacks.onInterruptConversationTurn); const goalDraftTargetChanged = t("actionReview.targetChanged"); @@ -1447,8 +1446,8 @@ export function PersonalWorkspacePage({ apply: readOnly ? undefined : confirmGoalDraft, suggest: readOnly ? undefined : suggestReply, adjust: focusTurnComposer, - cancel: canCancelTimelineTurn ? () => callbacks.onCancelConversationPreparation?.(timelineActionTarget) : undefined, - interrupt: canInterruptTimelineTurn ? (turnId: string) => callbacks.onInterruptConversationTurn?.(timelineActionTarget, turnId) : undefined, + cancel: canCancelTimelineTurn ? () => callbacks.onCancelConversationPreparation?.(conversationKey) : undefined, + interrupt: canInterruptTimelineTurn ? (turnId: string) => callbacks.onInterruptConversationTurn?.(conversationKey, turnId) : undefined, openEvidence: (goalId: string) => { selectGoal(goalId, "chat"); }, }; useLayoutEffect(() => { currentTimelineActions.current = nextTimelineActions; }); diff --git a/apps/presentation/dashboard/src/views/dashboard-page.tsx b/apps/presentation/dashboard/src/views/dashboard-page.tsx index 0a85804d04..60e3376ee2 100644 --- a/apps/presentation/dashboard/src/views/dashboard-page.tsx +++ b/apps/presentation/dashboard/src/views/dashboard-page.tsx @@ -631,6 +631,12 @@ function agentModelSelectionKey(contextId: string, agentId: string) { return `${contextId}:${agentId}`; } +function runConversationKey(run: {goalId: string; runId?: string}) { + return run.runId === "manager:resume-failed" + ? conversationContextKey({kind: "manager"}) + : conversationContextKey({kind: "goal", goalId: run.goalId}); +} + type PersonalManagerAnswer = { lines: string[]; text: string; @@ -1461,6 +1467,7 @@ function PersonalGoalHome({ const sessionDiscoveryKey = model.goals.some((goal) => goal.activationState === "active" && goal.loadState === "loading") ? "loading" : model.goals.map((goal) => `${goal.goalId}:${goal.agentId}`).join("|"); const contextId = conversationContextKey(conversationContext); + const selectedGoalContextId = selectedGoal ? conversationContextKey({kind: "goal", goalId: selectedGoal.goalId}) : ""; const activeConversationContext = useRef(contextId); activeConversationContext.current = contextId; const discoveredAgents: PersonalAgentOption[] = runtimeAgents.length > 0 @@ -2176,7 +2183,7 @@ function PersonalGoalHome({ try { const completed = await readCompletedChatTurn(session.session_id, turnId, controller.signal); if (controller.signal.aborted) return; - const persisted = !completed || await projectRecoveredTurnProposals(manager ? "manager" : goalId!, session.goal_id ?? undefined, turnId, completed.proposals, null); + const persisted = !completed || await projectRecoveredTurnProposals(manager ? "manager" : conversationContextKey({kind: "goal", goalId: goalId!}), session.goal_id ?? undefined, turnId, completed.proposals, null); if (persisted) projected.add(key); else retry = true; } catch { @@ -2214,7 +2221,7 @@ function PersonalGoalHome({ const next = { ...current }; for (const row of rows) { if (!row.session) continue; - next[row.goalId] = { + next[conversationContextKey({kind: "goal", goalId: row.goalId})] = { agentId: row.session.agent_id, resumable: row.session.resumable, sessionId: row.session.session_id, @@ -2356,14 +2363,15 @@ function PersonalGoalHome({ } async function prepareGoalConversation(goalId: string, agentId: string, signal?: AbortSignal) { - const key = `${goalId}:${agentId}`; + const targetContextId = conversationContextKey({kind: "goal", goalId}); + const key = `${targetContextId}:${agentId}`; const existing = sessionIds.current.get(key); if (existing) return existing; const session = await createChatSession({ kind: "goal", goalId }, agentId, newSessionRequired.current.has(key) ? "new" : "resume_latest", signal, requestedAgentModels.current[key], requestedAgentEfforts.current[key]); - recordSessionAdmission(goalId, session.session); + recordSessionAdmission(targetContextId, session.session); sessionIds.current.set(key, session.session_id); newSessionRequired.current.delete(key); - recordRuntimeBinding(goalId, {agentId, resumable: true, sessionId: session.session_id, status: session.session.status}); + recordRuntimeBinding(targetContextId, {agentId, resumable: true, sessionId: session.session_id, status: session.session.status}); return session.session_id; } @@ -2420,8 +2428,8 @@ function PersonalGoalHome({ }); void recordProjectionExchange({ answer: [answer.text, ...answer.lines.slice(0, 3)].filter(Boolean).join("\n"), - contextKind: targetContextId === "manager" ? "manager" : "goal", - goalId: targetContextId === "manager" ? undefined : targetContextId, + contextKind: targetContext.kind === "manager" ? "manager" : "goal", + goalId: targetContext.kind === "goal" ? targetContext.goalId : undefined, question, }).then((receipt) => { updateConversationMessage(targetContextId, userMessageId, { @@ -2775,8 +2783,8 @@ function PersonalGoalHome({ } } - async function interruptManagerTurn(run?: { agentId: string; goalId: string; sessionId?: string; turnId?: string }) { - const targetContextId = run?.goalId ?? contextId; + async function interruptManagerTurn(run?: { agentId: string; goalId: string; runId?: string; sessionId?: string; turnId?: string }, exactContextId?: string) { + const targetContextId = exactContextId ?? (run ? runConversationKey(run) : contextId); const binding = runtimeBindings[targetContextId]; const agentId = run?.agentId ?? binding?.agentId ?? selectedAgent.agentId; const sessionKey = `${targetContextId}:${agentId}`; @@ -2813,8 +2821,8 @@ function PersonalGoalHome({ } } - async function retryManagerSession(run: { agentId: string; goalId: string; sessionId?: string }) { - const targetContextId = run.goalId; + async function retryManagerSession(run: { agentId: string; goalId: string; runId?: string; sessionId?: string }) { + const targetContextId = runConversationKey(run); const sessionKey = `${targetContextId}:${run.agentId}`; const sessionId = run.sessionId ?? runtimeBindings[targetContextId]?.sessionId ?? sessionIds.current.get(sessionKey); if (!sessionId) return; @@ -2839,11 +2847,12 @@ function PersonalGoalHome({ } } - function startNewManagerSession(run: { agentId: string; goalId: string }) { - const sessionKey = `${run.goalId}:${run.agentId}`; + function startNewManagerSession(run: { agentId: string; goalId: string; runId?: string }) { + const targetContextId = runConversationKey(run); + const sessionKey = `${targetContextId}:${run.agentId}`; sessionIds.current.delete(sessionKey); newSessionRequired.current.add(sessionKey); - recordRuntimeBinding(run.goalId, { + recordRuntimeBinding(targetContextId, { agentId: run.agentId, resumable: true, sessionId: "new-session-pending", @@ -2851,13 +2860,14 @@ function PersonalGoalHome({ }); } - async function closeManagerSession(run: { agentId: string; goalId: string; sessionId?: string }) { - const sessionKey = `${run.goalId}:${run.agentId}`; - const sessionId = run.sessionId ?? runtimeBindings[run.goalId]?.sessionId ?? sessionIds.current.get(sessionKey); + async function closeManagerSession(run: { agentId: string; goalId: string; runId?: string; sessionId?: string }) { + const targetContextId = runConversationKey(run); + const sessionKey = `${targetContextId}:${run.agentId}`; + const sessionId = run.sessionId ?? runtimeBindings[targetContextId]?.sessionId ?? sessionIds.current.get(sessionKey); if (sessionId && sessionId !== "new-session-pending") await closeChatSession(sessionId); sessionIds.current.delete(sessionKey); newSessionRequired.current.add(sessionKey); - recordRuntimeBinding(run.goalId, null); + recordRuntimeBinding(targetContextId, null); } function chooseAgent(agentId: string) { @@ -2980,30 +2990,30 @@ function PersonalGoalHome({ // A persistent chat session is not itself waiting work. Only surface a // Goal-level execution row when there is execution, a status observation, // or a wait/fault. Observations are not deliverable Files. - ...(selectedGoal && (runtimeBindings[selectedGoal.goalId]?.turnId + ...(selectedGoal && (runtimeBindings[selectedGoalContextId]?.turnId || selectedGoal.hasRunObservation || goalHasExecutionSummary(selectedGoal)) ? [{ id: `run:${selectedGoal.goalId}`, kind: "run" as const, run: { - agentId: runtimeBindings[selectedGoal.goalId]?.agentId ?? selectedGoal.agentId, - agentLabel: personalAgentLabel(runtimeBindings[selectedGoal.goalId]?.agentId ?? selectedGoal.agentId), - canInterrupt: Boolean(runtimeBindings[selectedGoal.goalId]?.turnId), + agentId: runtimeBindings[selectedGoalContextId]?.agentId ?? selectedGoal.agentId, + agentLabel: personalAgentLabel(runtimeBindings[selectedGoalContextId]?.agentId ?? selectedGoal.agentId), + canInterrupt: Boolean(runtimeBindings[selectedGoalContextId]?.turnId), completedSteps: selectedGoal.agentTodos.filter((todo) => todo.done).length, goalId: selectedGoal.goalId, goalTitle: selectedGoal.title, latestActivity: selectedGoal.agentSentence, - resumable: runtimeBindings[selectedGoal.goalId]?.resumable ?? true, + resumable: runtimeBindings[selectedGoalContextId]?.resumable ?? true, runId: `goal:${selectedGoal.goalId}`, - sessionId: runtimeBindings[selectedGoal.goalId]?.sessionId, - sessionStatus: runtimeBindings[selectedGoal.goalId]?.status, - status: runtimeBindings[selectedGoal.goalId]?.turnId + sessionId: runtimeBindings[selectedGoalContextId]?.sessionId, + sessionStatus: runtimeBindings[selectedGoalContextId]?.status, + status: runtimeBindings[selectedGoalContextId]?.turnId ? "running" as const : selectedGoal.state === "需修复" ? "failed" as const : "waiting" as const, title: selectedGoal.nextSentence, totalSteps: selectedGoal.agentTodos.length || 1, - turnId: runtimeBindings[selectedGoal.goalId]?.turnId, + turnId: runtimeBindings[selectedGoalContextId]?.turnId, }, }] : []), ...contextMessages.map((message): WorkspaceTimelineItem => ({ @@ -3134,6 +3144,7 @@ function PersonalGoalHome({ onApplyAttention: (attention) => openGoalChat(attention.goalId), onCorrectRun: async (run, message) => { if (!run.sessionId) throw new Error("这个 Run 还没有可纠偏的执行 Session。"); + const targetContextId = runConversationKey(run); const proposal = await previewTypedAction({ actionKind: "run.correct", context: { kind: "run", goal_id: run.goalId, todo_id: run.todoId }, @@ -3143,7 +3154,7 @@ function PersonalGoalHome({ }); const applied = await applyTypedAction(proposal.proposal_id); const turnId = typeof applied.turn?.turn_id === "string" ? applied.turn.turn_id : undefined; - recordRuntimeBinding(run.goalId, { + recordRuntimeBinding(targetContextId, { agentId: run.agentId, resumable: true, sessionId: run.sessionId, @@ -3151,15 +3162,15 @@ function PersonalGoalHome({ turnId, }); if (turnId) { - activeTurnIds.current.set(run.goalId, turnId); + activeTurnIds.current.set(targetContextId, turnId); const controller = new AbortController(); const observationKey = `${run.sessionId}:${turnId}`; // History recovery must adopt this correction's observer when // the owner returns to Chat, just as it adopts composer Turns. turnObservers.current.set(observationKey, controller); - streamControllers.current.set(run.goalId, controller); + streamControllers.current.set(targetContextId, controller); let streamedText = ""; - const messageId = appendManagerAssistantMessage(run.goalId, { + const messageId = appendManagerAssistantMessage(targetContextId, { activity: ["正在把纠偏送入原执行 Session"], sourceTurnId: turnId, sourceSessionId: run.sessionId, @@ -3174,10 +3185,10 @@ function PersonalGoalHome({ signal: controller.signal, onDelta: (delta) => { streamedText += delta; - updateConversationMessage(run.goalId, messageId, { text: streamedText }); + updateConversationMessage(targetContextId, messageId, { text: streamedText }); }, }); - updateConversationMessage(run.goalId, messageId, { + updateConversationMessage(targetContextId, messageId, { activity: [], pending: false, text: visibleAgentMessage(streamed.response.message || streamedText.trim()) || `${run.agentLabel} 已完成纠偏。`, @@ -3185,18 +3196,18 @@ function PersonalGoalHome({ } catch (error) { const interrupted = interruptedTurnIds.current.delete(turnId) || (error instanceof ChatApiError && error.payload.error_code === "turn_interrupted"); - updateConversationMessage(run.goalId, messageId, { + updateConversationMessage(targetContextId, messageId, { activity: [], pending: false, text: interrupted ? [streamedText.trim(), "已中断。你可以在当前会话继续发送消息。"].filter(Boolean).join("\n\n") : error instanceof Error ? error.message : "纠偏回合失败。", }); } finally { if (turnObservers.current.get(observationKey) === controller) turnObservers.current.delete(observationKey); - const ownsStream = streamControllers.current.get(run.goalId) === controller; - if (ownsStream) streamControllers.current.delete(run.goalId); - if (ownsStream && activeTurnIds.current.get(run.goalId) === turnId) { - activeTurnIds.current.delete(run.goalId); - recordRuntimeBinding(run.goalId, { + const ownsStream = streamControllers.current.get(targetContextId) === controller; + if (ownsStream) streamControllers.current.delete(targetContextId); + if (ownsStream && activeTurnIds.current.get(targetContextId) === turnId) { + activeTurnIds.current.delete(targetContextId); + recordRuntimeBinding(targetContextId, { agentId: run.agentId, resumable: true, sessionId: run.sessionId, @@ -3225,7 +3236,7 @@ function PersonalGoalHome({ goalId: targetContextId, sessionId: binding.sessionId, turnId, - }); + }, targetContextId); }, onSteerConversationTurn: async (targetContextId, turnId, message, ingressId) => { const binding = runtimeBindings[targetContextId]; @@ -3240,6 +3251,7 @@ function PersonalGoalHome({ }, onOpenRunSession: async (run) => { if (!run.sessionId) return; + const targetContextId = runConversationKey(run); const sessionId = run.sessionId; const snapshot = await fetchChatSession(sessionId); setExecutionSessionSnapshots((current) => ({ @@ -3248,7 +3260,7 @@ function PersonalGoalHome({ })); setMessagesByContext((current) => ({ ...current, - [run.goalId]: snapshot.messages.map((message) => ({ + [targetContextId]: snapshot.messages.map((message) => ({ sourceMessageId: message.message_id, goalDraft: normalizeGoalDraft(message.goal_draft), sourceSessionId: sessionId, @@ -3263,7 +3275,7 @@ function PersonalGoalHome({ text: message.role === "user" ? message.reply_context?.source_text ?? message.text : visibleAgentMessage(message.text), })), })); - recordRuntimeBinding(run.goalId, { + recordRuntimeBinding(targetContextId, { agentId: run.agentId, resumable: snapshot.session.resumable, sessionId, @@ -3386,7 +3398,7 @@ function PersonalGoalHome({ } }, onPrepareLoopX: (agentId, goalId) => prepareGoalConversation(goalId, agentId), - onStartLoopX: (operation, agentId, goalId, settings) => { void sendManagerQuestion(operation === "start" ? "开启 LoopX 模式,持续推进当前 Goal。" : "恢复 LoopX 模式。", {agentId, contextId: goalId, loopxMode: {operation, settings}}); }, + onStartLoopX: (operation, agentId, goalId, settings) => { void sendManagerQuestion(operation === "start" ? "开启 LoopX 模式,持续推进当前 Goal。" : "恢复 LoopX 模式。", {agentId, contextId: conversationContextKey({kind: "goal", goalId}), loopxMode: {operation, settings}}); }, onStartNewRunSession: startNewManagerSession, }} goalArchiveLoadState={goalArchiveLoadState} diff --git a/apps/usage-collector/README.md b/apps/usage-collector/README.md index accfa63158..941eddc9ca 100644 --- a/apps/usage-collector/README.md +++ b/apps/usage-collector/README.md @@ -50,10 +50,21 @@ be inflated; use edge rate limiting if needed, not a new stored IP identifier. cd apps/usage-collector npx wrangler d1 create loopx-usage cp wrangler.example.toml wrangler.toml # set database_id; ignored local configuration -npx wrangler d1 execute loopx-usage --remote --file schema.sql +npx wrangler d1 execute loopx-usage --remote --file schema-v0.sql +npx wrangler d1 migrations apply loopx-usage --remote npx wrangler deploy ``` +`schema-v0.sql` bootstraps only a new database. D1 migrations then build the +current schema and record the applied migration names for future upgrades. +`schema.sql` is the current snapshot for local tests; it already contains +`pings.arch` and must not be followed by migration `0001-basic-usage.sql`. + +If an existing database was initialized from `schema.sql`, back it up and +inspect its schema and D1 migration ledger before upgrading. A maintainer must +approve its migration baseline. Do not rerun the bootstrap or change the ledger +automatically; neither operation is an existing-database repair procedure. + ## Upgrade an existing v0 deployment (including #5111) Back up D1 before changing it. Apply the additive migration exactly once using diff --git a/apps/usage-collector/schema-v0.sql b/apps/usage-collector/schema-v0.sql new file mode 100644 index 0000000000..6205e6b1a7 --- /dev/null +++ b/apps/usage-collector/schema-v0.sql @@ -0,0 +1,18 @@ +-- LoopX usage collector (Cloudflare D1). One row per installation per UTC day. +CREATE TABLE IF NOT EXISTS installs ( + install_id TEXT PRIMARY KEY, + first_day TEXT NOT NULL +); + +CREATE TABLE IF NOT EXISTS pings ( + day TEXT NOT NULL, + install_id TEXT NOT NULL, + version TEXT NOT NULL, + os TEXT NOT NULL, + python TEXT NOT NULL, + channel TEXT NOT NULL, + PRIMARY KEY (day, install_id) +); + +CREATE INDEX IF NOT EXISTS pings_install ON pings (install_id); +CREATE INDEX IF NOT EXISTS installs_first_day ON installs (first_day); diff --git a/apps/usage-collector/src/collector.js b/apps/usage-collector/src/collector.js index 6c6e2d87e7..bbace0756d 100644 --- a/apps/usage-collector/src/collector.js +++ b/apps/usage-collector/src/collector.js @@ -108,10 +108,10 @@ export async function buildStats(db, day) { const rows = await db .prepare( `SELECT ${column} AS key, COUNT(*) AS installs FROM (` + - `SELECT install_id, ${column}, MAX(day) FROM pings WHERE substr(day, 1, 7) = ?1 GROUP BY install_id` + + `SELECT install_id, ${column}, MAX(day) FROM pings WHERE day >= ?1 AND day < date(?1, '+1 month') GROUP BY install_id` + `) GROUP BY key ORDER BY installs DESC`, ) - .bind(day.slice(0, 7)) + .bind(`${day.slice(0, 7)}-01`) .all(); breakdown[column] = suppressSmall(rows.results); } @@ -209,10 +209,9 @@ export async function handle(request, db, now = new Date()) { const bytes = new Uint8Array(size); let offset = 0; for (const chunk of chunks) { bytes.set(chunk, offset); offset += chunk.byteLength; } - const raw = new TextDecoder().decode(bytes); let parsed; try { - parsed = JSON.parse(raw); + parsed = JSON.parse(new TextDecoder("utf-8", { fatal: true }).decode(bytes)); } catch { return json({ error: "invalid JSON" }, 400); } diff --git a/apps/usage-collector/test/collector.test.mjs b/apps/usage-collector/test/collector.test.mjs index 943b239110..8286a533aa 100644 --- a/apps/usage-collector/test/collector.test.mjs +++ b/apps/usage-collector/test/collector.test.mjs @@ -1,6 +1,6 @@ // Runs the collector against real SQLite through a minimal D1-shaped shim. import assert from "node:assert/strict"; -import { readFileSync } from "node:fs"; +import { readFileSync, readdirSync } from "node:fs"; import { DatabaseSync } from "node:sqlite"; import test from "node:test"; import { createServer } from "node:http"; @@ -83,6 +83,20 @@ test("ping endpoint rejects wrong method, type, size and JSON", async () => { assert.equal(db.raw.get("SELECT COUNT(*) AS n FROM pings").n, 0); }); +test("ping rejects invalid UTF-8 even in a duplicate JSON field", async () => { + const db = d1(); + const body = Buffer.from(JSON.stringify(ping(1)).replace( + '"schema":"loopx_usage_ping_v0"', + '"schema":"X","schema":"loopx_usage_ping_v0"', + )); + body[body.indexOf(Buffer.from('"X"')) + 1] = 0xff; + const request = new Request("https://collector.example/v0/ping", { + method: "POST", headers: { "content-type": "application/json" }, body, + }); + assert.equal((await handle(request, db, at("2026-10-01"))).status, 400); + assert.equal(db.raw.get("SELECT COUNT(*) AS n FROM pings").n, 0); +}); + test("one row per installation per day; first day kept; only payload fields stored", async () => { const db = d1(); assert.equal((await handle(post(ping(1)), db, at("2026-10-01"))).status, 204); @@ -115,6 +129,22 @@ test("stats report monthly active, new installs and suppressed breakdowns", asyn assert.deepEqual(stats.current_month_breakdown.version, { "1.2.0": 5 }); }); +test("monthly breakdown includes only the current calendar month at year and leap-day boundaries", async () => { + for (const [day, previous, current, next] of [ + ["2026-12-31", "2026-11-30", "2026-12-31", "2027-01-01"], + ["2024-02-29", "2024-01-31", "2024-02-29", "2024-03-01"], + ]) { + const db = d1(); + for (let n = 1; n <= 5; n++) { + await handle(post(ping(n, { os: "windows" })), db, at(previous)); + await handle(post(ping(n, { os: "linux" })), db, at(current)); + await handle(post(ping(n + 5, { os: "darwin" })), db, at(next)); + } + const response = await handle(new Request("https://collector.example/v0/stats"), db, at(day)); + assert.deepEqual((await response.json()).current_month_breakdown.os, { linux: 5 }); + } +}); + test("suppressSmall folds small buckets into other", () => { assert.deepEqual( suppressSmall([ @@ -246,6 +276,41 @@ test("v1 heartbeat has architecture; aggregates reject identifiers and store onl assert.equal("install_id" in stats, false); }); +test("documented fresh deployment migrates to the current schema and keeps legacy pings", () => { + const readme = readFileSync(new URL("../README.md", import.meta.url), "utf8"); + const fresh = readme.split("## Fresh deployment\n")[1].split("\n## ")[0]; + const commands = [...fresh.matchAll(/^npx wrangler d1 (execute|migrations apply) (\S+) (.+)$/gm)]; + assert.deepEqual(commands.map(command => command[1]), ["execute", "migrations apply"]); + for (const command of commands) { + assert.equal(command[2], "loopx-usage"); + assert.ok(command[3].split(/\s+/).includes("--remote")); + } + const schemaFile = /--file (\S+)/.exec(commands[0][3])?.[1]; + assert.ok(schemaFile, "fresh bootstrap must name its schema file"); + const db = new DatabaseSync(":memory:"); + const snapshot = new DatabaseSync(":memory:"); + try { + db.exec(readFileSync(new URL(`../${schemaFile}`, import.meta.url), "utf8")); + db.exec("INSERT INTO pings (day, install_id, version, os, python, channel) VALUES ('2026-09-26', 'old', '1.2.0', 'linux', '3.13', 'pip')"); + const migrations = new URL("../migrations/", import.meta.url); + for (const file of readdirSync(migrations).filter(file => file.endsWith(".sql")).sort()) { + db.exec(readFileSync(new URL(file, migrations), "utf8")); + } + snapshot.exec(SCHEMA); + const objects = "SELECT type, name FROM sqlite_master WHERE name NOT LIKE 'sqlite_%' ORDER BY type, name"; + assert.deepEqual(db.prepare(objects).all(), snapshot.prepare(objects).all()); + for (const {type, name} of snapshot.prepare(objects).all()) { + const pragma = type === "table" ? "table_info" : "index_info"; + assert.deepEqual(db.prepare(`PRAGMA ${pragma}("${name}")`).all(), + snapshot.prepare(`PRAGMA ${pragma}("${name}")`).all(), name); + } + assert.deepEqual({...db.prepare("SELECT install_id, arch FROM pings").get()}, {install_id: "old", arch: "other"}); + } finally { + db.close(); + snapshot.close(); + } +}); + test("existing v0 database upgrades without deleting heartbeat history", () => { const db = new DatabaseSync(":memory:"); db.exec("CREATE TABLE pings (day TEXT, install_id TEXT, version TEXT, os TEXT, python TEXT, channel TEXT, PRIMARY KEY(day, install_id)); INSERT INTO pings VALUES ('2026-09-26', 'old', '1.2.0', 'linux', '3.13', 'pip');"); diff --git a/docs/guides/kunluncode-adapter.zh-CN.html b/docs/guides/kunluncode-adapter.zh-CN.html index 3fd433aa21..7f80115bb9 100644 --- a/docs/guides/kunluncode-adapter.zh-CN.html +++ b/docs/guides/kunluncode-adapter.zh-CN.html @@ -492,7 +492,7 @@

拆开项目绑定和 MCP 安装

--skip-mcp "$LOOPX_KUNLUN" install -
同名 entry 保护若 loopx-kunluncode 不是 LoopX 管理的 entry,安装器拒绝覆盖。先 readback,只有确认应接管时才使用 --replace。新 entry 添加失败时会恢复可重建的旧 command entry;无法安全恢复的 entry 会在删除前被拒绝。
+
同名 entry 保护若 loopx-kunluncode 不是 LoopX 管理的 entry,安装器拒绝覆盖。先回读配置,只有确认应接管时才使用 --replace。添加命令返回失败且没有同名 entry 时,安装器会尝试恢复旧 command entry,并核对结果。无法安全恢复的 entry 会在删除前被拒绝。超时后,安装器先回读,不自动再次写入;有旧 entry 时,错误信息会给出私有备份路径。备份可能含秘密,确认配置后再删除。替换或恢复时应协调其他配置写入方。

Native goal-pro/goal 由 app-server 外层回写,本身不依赖 MCP;--skip-mcp 后仍可运行。MCP 用于 readback、交互使用和 --mode headless。

diff --git a/docs/guides/kunluncode-adapter.zh-CN.md b/docs/guides/kunluncode-adapter.zh-CN.md index 77a67e212c..a100bc3b0a 100644 --- a/docs/guides/kunluncode-adapter.zh-CN.md +++ b/docs/guides/kunluncode-adapter.zh-CN.md @@ -244,8 +244,13 @@ connect install uninstall add run status ``` 可以先加 `--dry-run` 查看将使用的路径。若同名 MCP entry 不是 LoopX 管理的 entry, -安装器会拒绝覆盖;只有确认旧 entry 可以替换时才使用 `--replace`。替换新 entry 失败时, -安装器会恢复可重建的旧 command entry;无法安全恢复的 entry 会在删除前被拒绝。 +安装器会拒绝覆盖;只有确认旧 entry 可以替换时才使用 `--replace`。添加命令返回失败且 +回读确认没有同名 entry 时,安装器会尝试恢复旧 command entry,并核对结果。无法安全恢复 +的 entry 会在删除前被拒绝。 + +替换前,安装器把旧 entry 保存为私有 JSON。命令超时后,安装器先回读状态,不自动再次写入。 +如果有旧 entry,错误信息会给出备份路径。先检查备份和 `kunluncode config get mcp_servers`, +再决定如何恢复。备份可能含秘密;确认配置后再删除。替换或恢复时应协调其他配置写入方。 默认 native `goal-pro`/`goal` 路径本身不依赖 MCP 完成回写,因此 `--skip-mcp` 后仍可运行; MCP entry 用于 readback、交互使用和 `--mode headless` 兼容路径。 diff --git a/examples/personal-workspace-browser/team-evidence.mjs b/examples/personal-workspace-browser/team-evidence.mjs index bd617a37ae..93ea218d4c 100644 --- a/examples/personal-workspace-browser/team-evidence.mjs +++ b/examples/personal-workspace-browser/team-evidence.mjs @@ -163,7 +163,9 @@ export const teamEvidenceScenario = { await page.setViewportSize({width: 1512, height: 982}); const changedReport = async route => { const body = route.request().method() === "POST" ? route.request().postDataJSON() : {}; - if (body.operation === "read") return route.fulfill({json: {operation_id: body.operation_id, status: "accepted", artifacts: [ + if (body.operation === "read") return route.fulfill({json: {operation_id: body.operation_id, + request_id: "request-analysis", agent_id: "local-analyst", todo_id: "todo_analysis", + status: "accepted", worker_active: false, recovery_required: false, artifacts: [ {ref: "report.md", sha256: "f".repeat(64), text: "newer unverified report"}, ]}}); return route.fallback(); @@ -231,7 +233,8 @@ export const teamEvidenceScenario = { const changedSource = async route => { const body = route.request().method() === "POST" ? route.request().postDataJSON() : {}; if (body.operation === "read" && body.operation_id === "accepted-analysis") { - return route.fulfill({json: {operation_id: body.operation_id, status: "accepted", recovery_required: false, + return route.fulfill({json: {operation_id: body.operation_id, request_id: "request-analysis", + agent_id: "local-analyst", todo_id: "todo_analysis", status: "accepted", worker_active: false, recovery_required: false, artifacts: [{ref: "report.json", sha256: "f".repeat(64), text: "newer content must not appear"}]}}); } return route.fallback(); @@ -274,8 +277,8 @@ export const teamEvidenceScenario = { const rejectedSource = async route => { const body = route.request().method() === "POST" ? route.request().postDataJSON() : {}; if (body.operation === "read" && body.operation_id === "review-objection") { - return route.fulfill({json: {operation_id: body.operation_id, agent_id: "independent-reviewer", - status: "rejected", recovery_required: false}}); + return route.fulfill({json: {operation_id: body.operation_id, request_id: "request-review", + agent_id: "independent-reviewer", todo_id: "todo_review", status: "rejected", worker_active: false, recovery_required: false}}); } return route.fallback(); }; @@ -295,8 +298,8 @@ export const teamEvidenceScenario = { const body = route.request().method() === "POST" ? route.request().postDataJSON() : {}; if (body.operation === "read" && body.operation_id === "accepted-analysis") { revisionRechecks++; - return route.fulfill({json: {operation_id: body.operation_id, agent_id: "local-analyst", - status: "accepted", recovery_required: false, + return route.fulfill({json: {operation_id: body.operation_id, request_id: "request-analysis", + agent_id: "local-analyst", todo_id: "todo_analysis", status: "accepted", worker_active: false, recovery_required: false, artifacts: [{ref: "report.json", sha256: "f".repeat(64), text: "newer revision"}]}}); } return route.fallback(); diff --git a/loopx/capabilities/native_chat/session_queue.py b/loopx/capabilities/native_chat/session_queue.py index 217e882154..fadafb4386 100644 --- a/loopx/capabilities/native_chat/session_queue.py +++ b/loopx/capabilities/native_chat/session_queue.py @@ -86,6 +86,12 @@ def _drain_session_queue_events(controller: Any, *, session_id: str, work_dir: P objective=objective, ) except Exception as exc: + with controller.lock: + worker_active = controller._has_live_turn_worker(session, active) + if worker_active: + # An observer cannot settle its live worker's Turn. + controller.closed.wait(0.05) + continue controller._fail_queue_preparation(session_id, active_turn_id, exc) else: continue diff --git a/loopx/kunluncode_goal_mode/README.md b/loopx/kunluncode_goal_mode/README.md index dad4c77994..05882e9d33 100644 --- a/loopx/kunluncode_goal_mode/README.md +++ b/loopx/kunluncode_goal_mode/README.md @@ -75,10 +75,26 @@ identity selection still come from the current working directory and the ignored `.loopx/kunluncode.json` binding. The installer never overwrites a foreign same-name entry unless `--replace` is -explicit. If adding the replacement fails, it restores a command-based previous -entry; registrations that cannot be safely reconstructed are rejected before +explicit. If adding a replacement exits unsuccessfully and readback shows no +same-name entry, it attempts to restore the previous command-based entry and +checks the result. It rejects entries that cannot be reconstructed before removal. +Before replacement, the installer saves the previous entry in a private JSON +recovery file with its name, command and arguments. If a command times out, it +reads the current configuration and stops +without automatically writing another entry. If there was a previous entry, +the error gives the recovery-file path. Inspect that file and +`kunluncode config get mcp_servers` locally before deciding how to restore it; +the file can contain secrets. Remove it after verifying the chosen configuration. +Coordinate other writers while replacing or restoring an entry: the installer +does not use an atomic compare-and-swap with them. + +If `uv pip install` times out, the installer accepts the environment only after +an isolated probe checks the pinned MCP version, LoopX version and source +checkout when applicable. A matching registration does not prove the server +starts; run the MCP test below. + Read the connection back: ```bash diff --git a/loopx/kunluncode_goal_mode/cli.py b/loopx/kunluncode_goal_mode/cli.py index cff0cae4a7..90372419b4 100644 --- a/loopx/kunluncode_goal_mode/cli.py +++ b/loopx/kunluncode_goal_mode/cli.py @@ -9,9 +9,12 @@ import shutil import subprocess import sys +import tempfile +from dataclasses import dataclass from pathlib import Path -from typing import Any +from typing import Any, NoReturn +from loopx import __version__ as LOOPX_VERSION from loopx.control_plane.projects.registry_codec import ( load_project_registry, mutate_project_registry, @@ -45,6 +48,12 @@ ) +@dataclass(frozen=True) +class _McpBackup: + entry: dict[str, Any] + path: Path + + def _run( command: list[str], *, @@ -70,19 +79,23 @@ def _venv_python(venv: Path) -> Path: def _compatible_python(value: str | Path) -> bool: - probe = _run( - [ - str(value), - "-c", - ( - "from importlib.metadata import version; " - "from mcp.server.fastmcp import FastMCP; " - "import loopx.kunluncode_goal_mode.server; " - f"assert version('mcp') == '{MCP_SDK_VERSION}'" - ), - ], - timeout=30, - ) + try: + probe = _run( + [ + str(value), + "-c", + ( + "from importlib.metadata import version; " + "import sys; " + "from mcp.server.fastmcp import FastMCP; " + "import loopx.kunluncode_goal_mode.server; " + f"sys.exit(version('mcp') != '{MCP_SDK_VERSION}')" + ), + ], + timeout=30, + ) + except subprocess.TimeoutExpired as exc: + raise RuntimeError("KunlunCode MCP Python verification timed out; status is unknown") from exc return probe.returncode == 0 @@ -91,6 +104,34 @@ def _source_root() -> Path | None: return candidate if (candidate / "pyproject.toml").is_file() else None +def _provisioned_python_matches(python: Path, source_root: Path | None) -> bool: + source = str((source_root / "loopx").resolve()) if source_root is not None else None + probe = _run( + [str(python), "-I", "-c", ( + "from importlib.metadata import version; from pathlib import Path; " + "import sys, loopx; from mcp.server.fastmcp import FastMCP; " + "import loopx.kunluncode_goal_mode.server; " + f"source = {source!r}; " + f"ok = version('mcp') == {MCP_SDK_VERSION!r} and " + f"version('loopx') == {LOOPX_VERSION!r} and loopx.__version__ == {LOOPX_VERSION!r}; " + "ok = ok and (source is None or Path(loopx.__file__).resolve().is_relative_to(Path(source))); " + "sys.exit(not ok)" + )], + timeout=30, + ) + return probe.returncode == 0 + + +def _create_mcp_venv(uv: str, python: Path) -> None: + try: + created = _run([uv, "venv", "--python", sys.executable, str(DEFAULT_MCP_VENV)]) + except subprocess.TimeoutExpired as exc: + state = "interpreter exists" if python.is_file() else "no interpreter found" + raise RuntimeError(f"uv venv timed out; {state}; inspect the environment before retrying") from exc + if created.returncode != 0: + raise RuntimeError("uv venv failed: " + (created.stdout + created.stderr)[-800:]) + + def provision_mcp_python(*, dry_run: bool = False) -> str: """Provision the adapter only through uv; never mutate system Python.""" if _compatible_python(sys.executable): @@ -109,23 +150,28 @@ def provision_mcp_python(*, dry_run: bool = False) -> str: return str(venv_python) DEFAULT_MCP_VENV.parent.mkdir(parents=True, exist_ok=True) if not venv_python.is_file(): - created = _run([uv, "venv", "--python", sys.executable, str(DEFAULT_MCP_VENV)]) - if created.returncode != 0: - raise RuntimeError( - "uv venv failed: " + (created.stdout + created.stderr)[-800:] - ) - installed = _run( - [ - uv, - "pip", - "install", - "--python", - str(venv_python), - *loopx_requirement, - MCP_REQUIREMENT, - ], - timeout=300, - ) + _create_mcp_venv(uv, venv_python) + try: + installed = _run( + [ + uv, + "pip", + "install", + "--python", + str(venv_python), + *loopx_requirement, + MCP_REQUIREMENT, + ], + timeout=300, + ) + except subprocess.TimeoutExpired as exc: + try: + matches = _provisioned_python_matches(venv_python, source_root) + except (OSError, subprocess.TimeoutExpired) as probe_error: + raise RuntimeError("uv pip install timed out; environment status could not be verified") from probe_error + if matches: + return str(venv_python) + raise RuntimeError("uv pip install timed out; environment does not match the requested adapter") from exc if installed.returncode != 0 or not _compatible_python(venv_python): raise RuntimeError( "uv pip install failed: " + (installed.stdout + installed.stderr)[-1200:] @@ -134,14 +180,26 @@ def provision_mcp_python(*, dry_run: bool = False) -> str: def _configured_mcp_servers(kunluncode: str) -> list[dict[str, Any]]: - result = _run([kunluncode, "config", "get", "mcp_servers"]) + try: + result = _run([kunluncode, "config", "get", "mcp_servers"]) + except subprocess.TimeoutExpired as exc: + raise RuntimeError("KunlunCode MCP configuration check timed out; status is unknown") from exc if result.returncode != 0: raise RuntimeError("cannot read KunlunCode MCP configuration") try: - payload = json.loads(result.stdout or "[]") + payload = json.loads(result.stdout) except json.JSONDecodeError as exc: raise RuntimeError("KunlunCode returned invalid mcp_servers JSON") from exc - return [item for item in payload if isinstance(item, dict)] + if not isinstance(payload, list) or not all(isinstance(item, dict) for item in payload): + raise RuntimeError("KunlunCode returned invalid mcp_servers JSON") + return payload + + +def _named_mcp_server(kunluncode: str) -> dict[str, Any] | None: + entries = [item for item in _configured_mcp_servers(kunluncode) if item.get("name") == MCP_SERVER_NAME] + if len(entries) > 1: + raise RuntimeError(f"multiple KunlunCode MCP entries named {MCP_SERVER_NAME!r}; inspect configuration") + return entries[0] if entries else None def _is_managed_server(entry: dict[str, Any]) -> bool: @@ -178,6 +236,8 @@ def _mcp_add_command( def _restorable_mcp_command( kunluncode: str, entry: dict[str, Any] ) -> list[str] | None: + if set(entry) - {"name", "command", "args"}: + return None command = entry.get("command") args = entry.get("args", []) if not isinstance(command, str) or not command.strip() or not isinstance(args, list): @@ -192,6 +252,144 @@ def _restorable_mcp_command( ) +def _save_mcp_backup(entry: dict[str, Any]) -> _McpBackup: + directory = DEFAULT_MCP_VENV.parent / "recovery" + directory.mkdir(mode=0o700, parents=True, exist_ok=True) + if directory.is_symlink() or ( + os.name != "nt" and (directory.stat().st_mode & 0o077 or directory.stat().st_uid != os.getuid()) + ): + raise RuntimeError("KunlunCode MCP recovery directory is not private") + descriptor, name = tempfile.mkstemp(prefix="mcp-entry-", suffix=".json", dir=directory) + path = Path(name) + try: + with os.fdopen(descriptor, "w", encoding="utf-8") as stream: + json.dump(entry, stream, ensure_ascii=False) + stream.flush() + os.fsync(stream.fileno()) + if json.loads(path.read_text(encoding="utf-8")) != entry: + raise RuntimeError("KunlunCode MCP recovery backup did not verify") + except BaseException: + path.unlink(missing_ok=True) + raise + return _McpBackup(entry, path) + + +def _discard_mcp_backup(backup: _McpBackup | None) -> None: + if backup is not None: + backup.path.unlink() + + +def _backup_hint(backup: _McpBackup | None) -> str: + return f"; previous entry saved at {backup.path}" if backup is not None else "" + + +def _requested_mcp_server(python: str) -> dict[str, Any]: + return {"name": MCP_SERVER_NAME, "command": python, "args": [str(MCP_SCRIPT)]} + + +def _checked_mcp_server(kunluncode: str, backup: _McpBackup | None, reason: str) -> dict[str, Any] | None: + try: + return _named_mcp_server(kunluncode) + except (OSError, RuntimeError) as exc: + raise RuntimeError(f"{reason}; MCP configuration could not be verified{_backup_hint(backup)}; inspect before retrying") from exc + + +def _report_mcp_uncertain( + kunluncode: str, backup: _McpBackup | None, reason: str, + requested_python: str | None, +) -> NoReturn: + current = _checked_mcp_server(kunluncode, backup, reason) + if backup is not None and current == backup.entry: + raise RuntimeError(f"{reason}; the previous entry is still present{_backup_hint(backup)}; verify before retrying") + if requested_python is not None and current == _requested_mcp_server(requested_python): + state = "the requested entry is present; verify it before retrying" + elif current is None: + state = "no entry is present; inspect configuration before " + ("restoring" if backup is not None else "retrying") + else: + state = "another entry is present; inspect it before retrying" + raise RuntimeError(f"{reason}; {state}{_backup_hint(backup)}") + + +def _recover_mcp_failure( + kunluncode: str, backup: _McpBackup | None, reason: str, + requested_python: str | None, +) -> NoReturn: + previous = backup.entry if backup is not None else None + current = _checked_mcp_server(kunluncode, backup, reason) + if previous is not None and current == previous: + _discard_mcp_backup(backup) + raise RuntimeError(f"{reason}; the previous entry is still present") + if current is not None: + if requested_python is not None and current == _requested_mcp_server(requested_python): + raise RuntimeError(f"{reason}; the requested entry is present{_backup_hint(backup)}; verify it before retrying") + raise RuntimeError(f"{reason}; another entry is present{_backup_hint(backup)}; inspect it before retrying") + if backup is None: + raise RuntimeError(f"{reason}; no entry is present; inspect configuration before retrying") + _restore_mcp_backup(kunluncode, backup, reason) + + +def _restore_mcp_backup(kunluncode: str, backup: _McpBackup, reason: str) -> NoReturn: + previous = backup.entry + restore_command = _restorable_mcp_command(kunluncode, previous) + if restore_command is None: + raise RuntimeError(f"{reason}; the previous entry cannot be safely restored{_backup_hint(backup)}") + try: + restored = _run(restore_command) + except subprocess.TimeoutExpired: + restore_status = "restoration timed out" + except OSError: + restore_status = "restoration could not start" + else: + restore_status = "restoration command completed" if restored.returncode == 0 else "restoration command failed" + current = _checked_mcp_server(kunluncode, backup, f"{reason}; {restore_status}") + if current == previous and restore_status == "restoration command completed": + _discard_mcp_backup(backup) + raise RuntimeError(f"{reason}; the previous entry was restored") + if current == previous: + raise RuntimeError(f"{reason}; previous entry is present, but {restore_status}{_backup_hint(backup)}; verify before retrying") + raise RuntimeError(f"{reason}; {restore_status}, but previous entry was not verified{_backup_hint(backup)}; inspect before retrying") + + +def _prepare_mcp_backup(kunluncode: str, previous: dict[str, Any]) -> _McpBackup: + try: + backup = _save_mcp_backup(previous) + except (OSError, RuntimeError) as exc: + raise RuntimeError("KunlunCode MCP entry could not be backed up; no replacement was attempted") from exc + current = _checked_mcp_server(kunluncode, backup, "KunlunCode MCP entry recheck failed") + if current != previous: + raise RuntimeError(f"KunlunCode MCP entry changed before removal{_backup_hint(backup)}") + return backup + + +def _remove_mcp_for_replacement(kunluncode: str, backup: _McpBackup) -> None: + try: + removed = _run([kunluncode, "mcp", "remove", MCP_SERVER_NAME]) + except subprocess.TimeoutExpired: + _report_mcp_uncertain(kunluncode, backup, "KunlunCode MCP removal timed out", None) + except OSError: + _report_mcp_uncertain(kunluncode, backup, "KunlunCode MCP removal could not start", None) + if removed.returncode != 0: + _report_mcp_uncertain(kunluncode, backup, "KunlunCode MCP removal failed", None) + + +def _register_mcp_server(kunluncode: str, python: str, backup: _McpBackup | None) -> None: + current = _checked_mcp_server(kunluncode, backup, "KunlunCode MCP registration precheck failed") + if current is not None: + raise RuntimeError(f"KunlunCode MCP entry appeared before registration{_backup_hint(backup)}; inspect before retrying") + try: + added = _run(_mcp_add_command(kunluncode, name=MCP_SERVER_NAME, command=python, args=[str(MCP_SCRIPT)])) + except subprocess.TimeoutExpired: + _report_mcp_uncertain(kunluncode, backup, "KunlunCode MCP registration timed out", python) + except OSError: + _report_mcp_uncertain(kunluncode, backup, "KunlunCode MCP registration could not start", python) + if added.returncode != 0: + _recover_mcp_failure(kunluncode, backup, "KunlunCode MCP registration failed: " + (added.stdout + added.stderr)[-800:], python) + current = _checked_mcp_server(kunluncode, backup, "KunlunCode MCP registration completed") + if current != _requested_mcp_server(python): + raise RuntimeError(f"KunlunCode MCP registration did not match readback{_backup_hint(backup)}") + _discard_mcp_backup(backup) + + def install_mcp(*, python: str | None, dry_run: bool, replace: bool) -> str: kunluncode = shutil.which("kunluncode") if not kunluncode: @@ -208,14 +406,7 @@ def install_mcp(*, python: str | None, dry_run: bool, replace: bool) -> str: f"{selected_python} must import LoopX and {MCP_REQUIREMENT}; " "use uv to sync the adapter environment" ) - existing = next( - ( - item - for item in _configured_mcp_servers(kunluncode) - if item.get("name") == MCP_SERVER_NAME - ), - None, - ) + existing = _named_mcp_server(kunluncode) if existing and not _is_managed_server(existing) and not replace: raise RuntimeError( f"KunlunCode MCP server {MCP_SERVER_NAME!r} is user-owned; pass --replace to replace it" @@ -230,34 +421,11 @@ def install_mcp(*, python: str | None, dry_run: bool, replace: bool) -> str: ) if dry_run: return selected_python + backup = None if existing: - removed = _run([kunluncode, "mcp", "remove", MCP_SERVER_NAME]) - if removed.returncode != 0: - raise RuntimeError("failed to remove prior managed KunlunCode MCP entry") - added = _run( - _mcp_add_command( - kunluncode, - name=MCP_SERVER_NAME, - command=selected_python, - args=[str(MCP_SCRIPT)], - ) - ) - if added.returncode != 0: - if restore_command is not None: - restored = _run(restore_command) - if restored.returncode == 0: - raise RuntimeError( - "KunlunCode MCP registration failed; the previous entry was restored: " - + (added.stdout + added.stderr)[-800:] - ) - raise RuntimeError( - "KunlunCode MCP registration failed and automatic restoration of the previous " - "entry also failed; inspect `kunluncode config get mcp_servers` before retrying" - ) - raise RuntimeError( - "KunlunCode MCP registration failed: " - + (added.stdout + added.stderr)[-800:] - ) + backup = _prepare_mcp_backup(kunluncode, existing) + _remove_mcp_for_replacement(kunluncode, backup) + _register_mcp_server(kunluncode, selected_python, backup) return selected_python @@ -265,14 +433,7 @@ def uninstall_mcp(*, dry_run: bool) -> bool: kunluncode = shutil.which("kunluncode") if not kunluncode: raise RuntimeError("kunluncode is not on PATH") - existing = next( - ( - item - for item in _configured_mcp_servers(kunluncode) - if item.get("name") == MCP_SERVER_NAME - ), - None, - ) + existing = _named_mcp_server(kunluncode) if existing is None: return False if not _is_managed_server(existing): diff --git a/pyproject.toml b/pyproject.toml index 623d084858..562e65b666 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -95,6 +95,7 @@ include = ["loopx*"] "loopx.control_plane.todos" = ["*.ts", "*.json"] "loopx.control_plane.turn_driver" = ["*.ts"] "loopx.control_plane.work_items" = ["*.ts"] +"loopx.canary" = ["module_metric_baseline.json"] "*" = ["README.md", "README.zh-CN.md", "docs/*.md", "docs/protocols/*.md"] "loopx.capabilities.content_ops" = ["templates/*.json", "experiences/*.json"] "loopx.capabilities.pr_review_queue" = ["experiences/*/*/*.json", "experiences/*.md"] diff --git a/scripts/verify_installed_runtime.py b/scripts/verify_installed_runtime.py new file mode 100644 index 0000000000..dfe6acd623 --- /dev/null +++ b/scripts/verify_installed_runtime.py @@ -0,0 +1,37 @@ +#!/usr/bin/env python3 +"""Verify distribution-only runtime resources outside the source checkout.""" + +import argparse +import os +import subprocess +import tempfile + +CHECK = r""" +from pathlib import Path +import sys +import loopx +from loopx.canary.maintainability_ratchet import MODULE_METRIC_BASELINE_PATH, module_metric_baseline +from loopx.control_plane.effect_runtime import effect_runtime_result + +package = Path(loopx.__file__).resolve() +assert package.is_relative_to(Path(sys.prefix).resolve()), package +assert module_metric_baseline(MODULE_METRIC_BASELINE_PATH) +projection = effect_runtime_result("manager.discovery.project", { + "arguments": {}, + "sources": [{"source_id": "local", "source_host": "fixture"}], + "pages": [{"ok": True, "matched": 1, "rows": [{"agent_id": "fixture-agent"}]}], +}) +assert projection["ok"] and projection["view"] == "agents" +assert projection["matched"] == 1 +assert projection["rows"][0]["agent_id"] == "fixture-agent" +assert projection["rows"][0]["source_id"] == "local" +print("Installed Manager discovery and canary baseline passed") +""" + +if __name__ == "__main__": + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("--python", required=True) + args = parser.parse_args() + with tempfile.TemporaryDirectory(prefix="loopx-installed-runtime-") as directory: + env = {**os.environ, "TMPDIR": directory, "LOOPX_USAGE_PING": "0"} + subprocess.run([args.python, "-I", "-c", CHECK], cwd=directory, env=env, check=True) diff --git a/tests/desktop/test_release_backup.py b/tests/desktop/test_release_backup.py new file mode 100644 index 0000000000..add4316947 --- /dev/null +++ b/tests/desktop/test_release_backup.py @@ -0,0 +1,177 @@ +"""The desktop release backup must fail when its remote inventory is unavailable.""" + +import hashlib +import os +import shutil +import subprocess +import sys +import tempfile +import unittest +from pathlib import Path + +WORKFLOW = ( + Path(__file__).resolve().parents[2] + / ".github/workflows/desktop-release-artifacts.yml" +) + + +def workflow_script(name): + lines = WORKFLOW.read_text(encoding="utf-8").splitlines() + step = next(i for i, line in enumerate(lines) if line.strip() == f"- name: {name}") + run = next(i for i in range(step + 1, len(lines)) if lines[i].strip() == "run: |") + indent = len(lines[run + 1]) - len(lines[run + 1].lstrip()) + body = [] + for line in lines[run + 1:]: + if line.strip() and len(line) - len(line.lstrip()) < indent: + break + body.append(line[indent:]) + return "\n".join(body) + + +class DesktopReleaseBackupTests(unittest.TestCase): + def test_inventory_failure_cannot_report_an_empty_backup(self): + bash = shutil.which("bash") + if not bash: + self.skipTest("bash is required to run the workflow step") + for fail_at in ("release", "assets"): + with self.subTest(fail_at=fail_at), tempfile.TemporaryDirectory() as temporary: + root = Path(temporary) + bin_dir = root / "bin" + bin_dir.mkdir() + gh = bin_dir / "gh" + gh.write_text( + '#!/bin/sh\nprintf called > "$GH_CALLED"\n' + 'if [ "$GH_FAIL_AT" = release ] || [ "$2" = --paginate ]; then exit 23; fi\n' + 'printf "42\\n"\n', + encoding="utf-8", + ) + gh.chmod(0o755) + env = { + **os.environ, + "PATH": str(bin_dir) + os.pathsep + os.environ.get("PATH", ""), + "GH_CALLED": str(root / "gh-called"), + "GH_FAIL_AT": fail_at, + "GITHUB_REPOSITORY": "loopx-project/loopx", + "RELEASE_TAG": "v1.2.4", + } + result = subprocess.run( + [bash, "-c", workflow_script("Back up existing desktop release assets")], cwd=root, env=env, + capture_output=True, text=True, timeout=30, check=False, + ) + self.assertTrue((root / "gh-called").exists()) + self.assertNotEqual(result.returncode, 0) + self.assertFalse((root / "dist/previous-desktop/ASSET-INVENTORY.txt").exists()) + + @unittest.skipUnless(sys.platform.startswith("linux"), "release upload runs on Ubuntu") + def test_successful_inventory_preserves_assets_and_their_hashes(self): + bash = shutil.which("bash") + if not bash: + self.skipTest("bash is required to run the workflow step") + for name in ("", "LoopX_1.2.4_aarch64.dmg"): + with self.subTest(asset=name), tempfile.TemporaryDirectory() as temporary: + root = Path(temporary) + bin_dir = root / "bin" + bin_dir.mkdir() + gh = bin_dir / "gh" + gh.write_text( + '#!/bin/sh\n' + 'if [ "$1" = api ]; then\n' + ' if [ "$2" = --paginate ]; then [ -z "$GH_ASSET" ] || printf "%s\\n" "$GH_ASSET"; else printf "42\\n"; fi\n' + 'else cp "$GH_SOURCE/$GH_ASSET" "dist/previous-desktop/$GH_ASSET"; fi\n', + encoding="utf-8", + ) + gh.chmod(0o755) + source = root / "remote" + source.mkdir() + if name: + (source / name).write_bytes(b"old desktop asset") + env = { + **os.environ, + "PATH": str(bin_dir) + os.pathsep + os.environ.get("PATH", ""), + "GH_ASSET": name, + "GH_SOURCE": str(source), + "GITHUB_REPOSITORY": "loopx-project/loopx", + "RELEASE_TAG": "v1.2.4", + } + result = subprocess.run( + [bash, "-c", workflow_script("Back up existing desktop release assets")], cwd=root, env=env, + capture_output=True, text=True, timeout=30, check=False, + ) + self.assertEqual(result.returncode, 0, result.stderr) + backup = root / "dist/previous-desktop" + self.assertIn(f"asset_count={int(bool(name))}", (backup / "ASSET-INVENTORY.txt").read_text()) + if name: + self.assertEqual((backup / name).read_bytes(), b"old desktop asset") + digest = hashlib.sha256(b"old desktop asset").hexdigest() + self.assertIn(f"{digest} {name}", (backup / "PREVIOUS-DESKTOP-SHA256SUMS").read_text()) + + def test_failed_manifest_lookup_never_starts_replacement(self): + bash = shutil.which("bash") + if not bash: + self.skipTest("bash is required to run the workflow step") + for fail_at in ("release", "assets"): + with self.subTest(fail_at=fail_at), tempfile.TemporaryDirectory() as temporary: + root = Path(temporary) + bin_dir = root / "bin" + bin_dir.mkdir() + gh = bin_dir / "gh" + gh.write_text( + '#!/bin/sh\nif [ "$1" = api ]; then\n' + ' if [ "$GH_FAIL_AT" = release ] || [ "$2" = --paginate ]; then exit 23; fi\n' + ' printf "42\\n"; exit 0\nfi\n' + 'if [ "$2" = view ]; then exit 23; fi\n' + 'printf called > "$GH_UPLOAD"\nexit 0\n', + encoding="utf-8", + ) + gh.chmod(0o755) + env = { + **os.environ, + "PATH": str(bin_dir) + os.pathsep + os.environ.get("PATH", ""), + "GH_FAIL_AT": fail_at, + "GH_UPLOAD": str(root / "upload-called"), + "GITHUB_REPOSITORY": "loopx-project/loopx", + "RELEASE_TAG": "v1.2.4", + } + result = subprocess.run( + [bash, "-c", workflow_script("Replace complete desktop binary set")], + cwd=root, env=env, capture_output=True, text=True, timeout=30, check=False, + ) + self.assertNotEqual(result.returncode, 0) + self.assertFalse((root / "upload-called").exists()) + + def test_existing_manifest_is_removed_before_any_binary_replacement(self): + bash = shutil.which("bash") + if not bash: + self.skipTest("bash is required to run the workflow step") + with tempfile.TemporaryDirectory() as temporary: + root = Path(temporary) + bin_dir = root / "bin" + bin_dir.mkdir() + gh = bin_dir / "gh" + gh.write_text( + '#!/bin/sh\nif [ "$1" = api ]; then\n' + ' if [ "$2" = --paginate ]; then printf "DESKTOP-SHA256SUMS\\n"; else printf "42\\n"; fi\n' + 'elif [ "$2" = delete-asset ]; then printf called > "$GH_DELETED"\n' + 'elif [ "$2" = upload ]; then [ -f "$GH_DELETED" ] || exit 20; printf called > "$GH_UPLOAD"\nfi\n', + encoding="utf-8", + ) + gh.chmod(0o755) + env = { + **os.environ, + "PATH": str(bin_dir) + os.pathsep + os.environ.get("PATH", ""), + "GH_DELETED": str(root / "manifest-deleted"), + "GH_UPLOAD": str(root / "upload-called"), + "GITHUB_REPOSITORY": "loopx-project/loopx", + "RELEASE_TAG": "v1.2.4", + } + result = subprocess.run( + [bash, "-c", workflow_script("Replace complete desktop binary set")], + cwd=root, env=env, capture_output=True, text=True, timeout=30, check=False, + ) + self.assertEqual(result.returncode, 0, result.stderr) + self.assertTrue((root / "manifest-deleted").exists()) + self.assertTrue((root / "upload-called").exists()) + + +if __name__ == "__main__": + unittest.main() diff --git a/tests/desktop/test_release_resume.py b/tests/desktop/test_release_resume.py new file mode 100644 index 0000000000..8f4e2e1124 --- /dev/null +++ b/tests/desktop/test_release_resume.py @@ -0,0 +1,145 @@ +"""Run the release workflow against an isolated fake GitHub release.""" + +import hashlib +import os +import shutil +import subprocess +import tempfile +import unittest +from pathlib import Path + +from test_release_backup import WORKFLOW, workflow_script +from test_update_feed import FAKE_GH + + +class DesktopReleaseResumeTests(unittest.TestCase): + def fixture(self): + bash = shutil.which("bash") + if not bash: + self.skipTest("bash is required to run the workflow step") + temporary = tempfile.TemporaryDirectory() + self.addCleanup(temporary.cleanup) + root = Path(temporary.name) + dist = root / "dist/desktop" + dist.mkdir(parents=True) + names = ( + "LoopX.app.zip", + "LoopX_1.2.4_aarch64.dmg", + "LoopX_1.2.4_x64-setup.exe", + "LoopX_1.2.4_x64_en-US.msi", + ) + for name in names: + (dist / name).write_bytes(("fixture:" + name).encode()) + (dist / "DESKTOP-SHA256SUMS").write_text( + "".join(f"{hashlib.sha256((dist / name).read_bytes()).hexdigest()} {name}\n" for name in sorted(names)), + encoding="ascii", + ) + scripts = root / "scripts" + scripts.mkdir() + shutil.copy2(WORKFLOW.parents[2] / "scripts/desktop_release_artifacts.py", scripts) + bin_dir = root / "bin" + bin_dir.mkdir() + gh = bin_dir / "gh" + gh.write_text('#!/bin/sh\nexec python "$GH_FAKE_SCRIPT" "$@"\n', encoding="utf-8") + gh.chmod(0o755) + fake_script = root / "fake-gh.py" + fake_script.write_text(FAKE_GH, encoding="utf-8") + remote = root / "remote" + (remote / "v1.2.4").mkdir(parents=True) + env = { + **os.environ, + "PATH": str(bin_dir) + os.pathsep + os.environ.get("PATH", ""), + "GH_FAKE_SCRIPT": str(fake_script), + "GH_LOG": str(root / "gh.log"), + "GH_REMOTE": str(remote), + "GITHUB_REPOSITORY": "loopx-project/loopx", + "RELEASE_TAG": "v1.2.4", + "RELEASE_VERSION": "1.2.4", + "SOURCE_REF": "a" * 40, + "CHANNEL": "stable", + } + return bash, root, env, remote / "v1.2.4" + + def execute(self, fixture, step="Resume desktop release without replacing assets"): + bash, root, env, _ = fixture + return subprocess.run( + [bash, "-c", workflow_script(step)], + cwd=root, env=env, capture_output=True, text=True, timeout=30, check=False, + ) + + def test_interrupted_release_resumes_and_repeated_execution_is_idempotent(self): + for interrupted in ("LoopX.app.zip", "DESKTOP-SHA256SUMS"): + with self.subTest(interrupted=interrupted): + fixture = self.fixture() + _, root, env, remote = fixture + env["GH_FAIL"] = "upload:" + interrupted + self.assertNotEqual(self.execute(fixture).returncode, 0) + self.assertFalse((remote / "DESKTOP-SHA256SUMS").exists()) + self.assertGreater(len(list(remote.iterdir())), 0) + del env["GH_FAIL"] + resumed = self.execute(fixture) + self.assertEqual(resumed.returncode, 0, resumed.stderr) + uploads = [line for line in (root / "gh.log").read_text().splitlines() if line.startswith("release upload ")] + self.assertTrue(uploads[-1].startswith("release upload v1.2.4 dist/desktop/DESKTOP-SHA256SUMS")) + self.assertFalse(any("--clobber" in line for line in uploads)) + again = self.execute(fixture) + self.assertEqual(again.returncode, 0, again.stderr) + self.assertEqual(uploads, [line for line in (root / "gh.log").read_text().splitlines() if line.startswith("release upload ")]) + for artifact in (root / "dist/desktop").iterdir(): + self.assertEqual((remote / artifact.name).read_bytes(), artifact.read_bytes()) + verified = self.execute(fixture, "Verify published desktop release") + self.assertEqual(verified.returncode, 0, verified.stderr) + + def test_existing_conflict_or_wrong_tag_commit_stops_all_uploads(self): + for failure in ("content", "case", "manifest", "commit", "inventory"): + with self.subTest(failure=failure): + fixture = self.fixture() + _, root, env, remote = fixture + if failure == "content": + (remote / "LoopX_1.2.4_x64-setup.exe").write_bytes(b"different") + elif failure == "case": + (remote / "loopx.app.zip").write_bytes(b"different") + elif failure == "manifest": + (remote / "DESKTOP-SHA256SUMS").write_bytes(b"different") + elif failure == "commit": + env["GH_REMOTE_REF"] = "b" * 40 + else: + env["GH_FAIL"] = "api:unavailable" + original = {p.name: p.read_bytes() for p in remote.iterdir()} + self.assertNotEqual(self.execute(fixture).returncode, 0) + self.assertEqual({p.name: p.read_bytes() for p in remote.iterdir()}, original) + self.assertFalse(any(line.startswith("release upload ") for line in (root / "gh.log").read_text().splitlines())) + + def test_corrupt_remote_readback_cannot_publish_the_manifest(self): + fixture = self.fixture() + _, _, env, remote = fixture + env["GH_CORRUPT"] = "LoopX_1.2.4_aarch64.dmg" + self.assertNotEqual(self.execute(fixture).returncode, 0) + self.assertFalse((remote / "DESKTOP-SHA256SUMS").exists()) + + def test_consistent_but_different_published_set_fails_final_verification(self): + fixture = self.fixture() + _, root, _, remote = fixture + names = [p.name for p in (root / "dist/desktop").iterdir() if p.name != "DESKTOP-SHA256SUMS"] + for name in names: + (remote / name).write_bytes(("other:" + name).encode()) + (remote / "DESKTOP-SHA256SUMS").write_text( + "".join(f"{hashlib.sha256((remote / name).read_bytes()).hexdigest()} {name}\n" for name in sorted(names)), + encoding="ascii", + ) + self.assertNotEqual(self.execute(fixture, "Verify published desktop release").returncode, 0) + + def test_release_and_manual_republication_use_separate_steps(self): + workflow = WORKFLOW.read_text(encoding="utf-8") + for name, event in ( + ("Resume desktop release without replacing assets", "release"), + ("Replace complete desktop binary set", "workflow_dispatch"), + ("Replace desktop checksum manifest last", "workflow_dispatch"), + ): + with self.subTest(step=name): + step = workflow.split("- name: " + name, 1)[1].split("- name:", 1)[0] + self.assertIn(f"if: github.event_name == '{event}'", step) + + +if __name__ == "__main__": + unittest.main() diff --git a/tests/desktop/test_update_feed.py b/tests/desktop/test_update_feed.py index dcaa7bd67b..2af9088190 100644 --- a/tests/desktop/test_update_feed.py +++ b/tests/desktop/test_update_feed.py @@ -30,6 +30,75 @@ + base64.b64encode(b"\x00" * 64) ).decode("ascii") +# Stand in for GitHub only; the actual workflow shell and filesystem remain real. +FAKE_GH = r'''import os +from pathlib import Path +import shutil +import sys + +args = sys.argv[1:] +remote = Path(os.environ["GH_REMOTE"]) +with Path(os.environ["GH_LOG"]).open("a", encoding="utf-8") as log: + log.write(" ".join(args) + "\n") +if args[0] == "api": + endpoint = next((arg for arg in args[1:] if arg.startswith("repos/")), "") + if os.environ.get("GH_FAIL") == "api:unavailable": + print("HTTP/2.0 503 Service Unavailable\n\n{}") + sys.exit(1) + if "commits/" in endpoint: + if "--include" in args: + missing = os.environ.get("GH_TAG_MISSING") == "1" + print("HTTP/2.0 404 Not Found\n\n{}" if missing else "HTTP/2.0 200 OK\n\n{}") + sys.exit(1 if missing else 0) + print(os.environ.get("GH_REMOTE_REF", os.environ["SOURCE_REF"])) + elif "releases/tags/" in endpoint: + tag = endpoint.rsplit("/", 1)[1] + exists = (remote / tag).is_dir() + if "--include" in args: + print("HTTP/2.0 200 OK\n\n{}" if exists else "HTTP/2.0 404 Not Found\n\n{}") + sys.exit(0 if exists else 1) + print("43" if tag.startswith("desktop-") and tag == "desktop-" + os.environ["CHANNEL"] else "42") + elif "/assets?" in endpoint: + tag = "desktop-" + os.environ["CHANNEL"] if "/43/" in endpoint else os.environ["RELEASE_TAG"] + for asset in sorted((remote / tag).iterdir()): + print(asset.name) + sys.exit(0) +if args[:2] == ["release", "view"]: + directory = remote / args[2] + if not directory.is_dir(): + sys.exit(1) + if "--json" in args: + for asset in sorted(directory.iterdir()): + print(asset.name) + sys.exit(0) +if args[:2] == ["release", "create"]: + (remote / args[2]).mkdir() + sys.exit(0) +if args[:2] == ["release", "upload"]: + source = Path(args[3]) + target = remote / args[2] / source.name + if os.environ.get("GH_FAIL") == "upload:" + source.name: + sys.exit(17) + if "--clobber" in args and os.environ.get("GH_FAIL") == "clobber:" + source.name: + if target.exists(): + target.unlink() + sys.exit(19) + if target.exists() and "--clobber" not in args: + sys.exit(1) + shutil.copyfile(source, target) + if os.environ.get("GH_CORRUPT") == source.name: + target.write_bytes(b"corrupted remote asset") + sys.exit(0) +if args[:2] == ["release", "download"]: + name = args[args.index("--pattern") + 1] + if os.environ.get("GH_FAIL") == "download:" + name: + sys.exit(18) + directory = Path(args[args.index("--dir") + 1]) + shutil.copyfile(remote / args[2] / name, directory / name) + sys.exit(0) +raise SystemExit("unexpected fake gh command: " + " ".join(args)) +''' + class FeedTests(unittest.TestCase): def test_only_core_stable_releases_advance_pointer(self): @@ -141,6 +210,8 @@ def test_workflow_gates_the_signed_windows_build_and_checks_its_version(self): def test_stable_publish_does_not_require_the_windows_build(self): workflow = WORKFLOW.read_text(encoding="utf-8") + identity = workflow.split("\n identity:\n", 1)[1].split("\n build-macos:\n", 1)[0] + self.assertIn("ref: ${{ github.sha }}", identity) build_macos = workflow.split("\n build-macos:\n", 1)[1].split( "\n build-windows:\n", 1 )[0] @@ -156,7 +227,7 @@ def test_stable_publish_does_not_require_the_windows_build(self): publish = workflow.split("\n publish:\n", 1)[1] self.assertIn( - "needs: [identity, build-macos, build-windows, validate]", publish + "needs: [identity, build-macos, build-windows, validate, windows-desktop-tests]", publish ) condition = publish.split("if: >-", 1)[1].split("runs-on:", 1)[0] expression = " ".join(line.strip() for line in condition.splitlines()) @@ -168,6 +239,7 @@ def test_stable_publish_does_not_require_the_windows_build(self): .replace("needs.build-macos.result", "darwin_result") .replace("needs.build-windows.result", "windows_result") .replace("needs.validate.result", "validate_result") + .replace("needs.windows-desktop-tests.result", "native_test_result") .replace("needs.identity.outputs.channel", "channel") ) @@ -177,6 +249,7 @@ def publish_after( darwin_result="success", windows_result="skipped", validate_result="success", + native_test_result="success", ): return eval( # noqa: S307 - repository-owned expression, no builtins. python_expression, @@ -187,6 +260,7 @@ def publish_after( "darwin_result": darwin_result, "windows_result": windows_result, "validate_result": validate_result, + "native_test_result": native_test_result, }, ) @@ -203,6 +277,9 @@ def publish_after( "a main release must reject a missing Windows build", ) self.assertTrue(publish_after("main", windows_result="success")) + for channel in ("main", "stable"): + for status in ("failure", "cancelled", "skipped"): + self.assertFalse(publish_after(channel, windows_result="success", native_test_result=status)) def _upload_step_script(self): lines = WORKFLOW.read_text(encoding="utf-8").splitlines() @@ -252,10 +329,10 @@ def _working_bash(self): return candidate self.skipTest("a working bash is required for the upload-step regression") - def _run_upload_step(self, channel, version, tag, include_windows): + def _upload_fixture(self, channel, version, tag, include_windows): bash = self._working_bash() script = self._upload_step_script() - case_dir = self.root / f"upload-{channel}-{int(include_windows)}" + case_dir = self.root / f"upload-{channel}-{int(include_windows)}-{tag}" updater = case_dir / "dist" / "updater" updater.mkdir(parents=True) for name in ("LoopX.app.tar.gz", "LoopX.app.tar.gz.sig"): @@ -275,33 +352,172 @@ def _run_upload_step(self, channel, version, tag, include_windows): bin_dir.mkdir() gh_log = case_dir / "gh.log" gh = bin_dir / "gh" - gh.write_text( - "#!/bin/sh\n" - 'printf \'%s\\n\' "$*" >> "$GH_LOG"\n' - "exit 0\n", - encoding="utf-8", - ) + gh.write_text('#!/bin/sh\nexec python "$GH_FAKE_SCRIPT" "$@"\n', encoding="utf-8") gh.chmod(0o755) + fake_script = case_dir / "fake-gh.py" + fake_script.write_text(FAKE_GH, encoding="utf-8") + remote = case_dir / "remote" + (remote / tag).mkdir(parents=True) + (remote / f"desktop-{channel}").mkdir() + scripts = case_dir / "scripts" + scripts.mkdir() + shutil.copy2(Path(feed.__file__), scripts / "desktop_update_feed.py") env = { **os.environ, "PATH": f"{bin_dir}{os.pathsep}{os.environ.get('PATH', '')}", "GH_LOG": str(gh_log), + "GH_FAKE_SCRIPT": str(fake_script), + "GH_REMOTE": str(remote), + "GITHUB_REPOSITORY": "loopx-project/loopx", "RELEASE_TAG": tag, - "SOURCE_REF": "source-ref", + "SOURCE_REF": "a" * 40, "EVENT_NAME": "release", "CHANNEL": channel, + "VERSION": version, } - result = subprocess.run( - [bash, "-c", script], - cwd=case_dir, - env=env, - capture_output=True, - text=True, - timeout=30, + return bash, script, case_dir, env, gh_log, remote + + def _execute_upload(self, fixture): + bash, script, case_dir, env, _, _ = fixture + return subprocess.run( + [bash, "-c", script], cwd=case_dir, env=env, check=False, + capture_output=True, text=True, timeout=30, ) + + def _run_upload_step(self, channel, version, tag, include_windows): + fixture = self._upload_fixture(channel, version, tag, include_windows) + _, _, _, _, gh_log, _ = fixture + result = self._execute_upload(fixture) self.assertEqual(result.returncode, 0, result.stderr) return gh_log.read_text(encoding="utf-8").splitlines() + def test_interrupted_upload_resumes_without_replacing_assets(self): + fixture = self._upload_fixture("stable", "1.2.4", "v1.2.4", False) + _, _, case_dir, env, log, remote = fixture + env["GH_FAIL"] = "upload:LoopX.app.tar.gz.sig" + self.assertNotEqual(self._execute_upload(fixture).returncode, 0) + release = remote / "v1.2.4" + self.assertEqual({path.name for path in release.iterdir()}, {"LoopX.app.tar.gz"}) + self.assertEqual(list((remote / "desktop-stable").iterdir()), []) + del env["GH_FAIL"] + resumed = self._execute_upload(fixture) + self.assertEqual(resumed.returncode, 0, resumed.stderr) + again = self._execute_upload(fixture) + self.assertEqual(again.returncode, 0, again.stderr) + for local in (case_dir / "dist/updater").iterdir(): + self.assertEqual((release / local.name).read_bytes(), local.read_bytes()) + self.assertEqual( + (remote / "desktop-stable/desktop-updater.json").read_bytes(), + (release / "desktop-updater.json").read_bytes(), + ) + calls = log.read_text(encoding="utf-8").splitlines() + tag_uploads = [line for line in calls if line.startswith("release upload v1.2.4 ")] + self.assertFalse(any("--clobber" in line for line in tag_uploads)) + pointer_upload = "release upload desktop-stable dist/updater/desktop-updater.json --clobber" + self.assertEqual(calls.count(pointer_upload), 1) + self.assertLess( + calls.index("release upload v1.2.4 dist/updater/desktop-updater.json"), + calls.index(pointer_upload), + ) + + def test_mismatched_existing_asset_aborts_before_any_upload(self): + fixture = self._upload_fixture("stable", "1.2.4", "v1.2.4", False) + _, _, _, _, log, remote = fixture + existing = remote / "v1.2.4/LoopX.app.tar.gz.sig" + existing.write_bytes(b"different signature") + self.assertNotEqual(self._execute_upload(fixture).returncode, 0) + self.assertEqual(existing.read_bytes(), b"different signature") + self.assertEqual({p.name for p in existing.parent.iterdir()}, {existing.name}) + self.assertFalse(any(line.startswith("release upload ") for line in log.read_text().splitlines())) + + def test_wrong_release_commit_or_failed_readback_never_advances_pointer(self): + for failure in ("wrong-commit", "download"): + with self.subTest(failure=failure): + # Each scenario has an independent remote release. + if failure == "wrong-commit": + version, tag = "1.2.4", "v1.2.4" + else: + version, tag = "1.2.5", "v1.2.5" + fixture = self._upload_fixture("stable", version, tag, False) + _, _, _, env, _, remote = fixture + if failure == "wrong-commit": + env["GH_REMOTE_REF"] = "b" * 40 + else: + env["GH_FAIL"] = "download:LoopX.app.tar.gz" + self.assertNotEqual(self._execute_upload(fixture).returncode, 0) + self.assertEqual(list((remote / "desktop-stable").iterdir()), []) + self.assertFalse((remote / tag / "desktop-updater.json").exists()) + + def test_preview_release_creation_resumes_and_api_failure_never_creates(self): + fixture = self._upload_fixture("main", "0.0.0-main.123.1", "desktop-main-123-1", True) + _, _, _, env, log, remote = fixture + (remote / env["RELEASE_TAG"]).rmdir() + env["EVENT_NAME"] = "workflow_dispatch" + env["GH_FAIL"] = "api:unavailable" + self.assertNotEqual(self._execute_upload(fixture).returncode, 0) + self.assertFalse((remote / env["RELEASE_TAG"]).exists()) + del env["GH_FAIL"] + env["GH_REMOTE_REF"] = "b" * 40 + self.assertNotEqual(self._execute_upload(fixture).returncode, 0) + self.assertFalse((remote / env["RELEASE_TAG"]).exists()) + del env["GH_REMOTE_REF"] + env["GH_TAG_MISSING"] = "1" + env["GH_FAIL"] = "upload:LoopX.app.tar.gz.sig" + self.assertNotEqual(self._execute_upload(fixture).returncode, 0) + del env["GH_FAIL"] + result = self._execute_upload(fixture) + self.assertEqual(result.returncode, 0, result.stderr) + creates = [line for line in log.read_text().splitlines() if line.startswith("release create ")] + self.assertEqual(len(creates), 1) + + def test_conflicting_feed_and_case_collision_are_rejected_before_upload(self): + for version, name in (("1.2.4", "desktop-updater.json"), ("1.2.5", "loopx.app.tar.gz")): + with self.subTest(name=name): + tag = "v" + version + fixture = self._upload_fixture("stable", version, tag, False) + _, _, _, _, log, remote = fixture + (remote / tag / name).write_bytes(b"conflict") + self.assertNotEqual(self._execute_upload(fixture).returncode, 0) + self.assertEqual({p.name for p in (remote / tag).iterdir()}, {name}) + self.assertFalse(any(line.startswith("release upload ") for line in log.read_text().splitlines())) + + def test_wrong_feed_or_corrupt_remote_upload_never_publishes_a_pointer(self): + for version, failure in (("1.2.4", "feed"), ("1.2.5", "corrupt")): + with self.subTest(failure=failure): + fixture = self._upload_fixture("stable", version, "v" + version, False) + _, _, case_dir, env, _, remote = fixture + if failure == "feed": + (case_dir / "dist/updater/desktop-updater.json").write_text('{"version":"0.0.0"}') + else: + env["GH_CORRUPT"] = "LoopX.app.tar.gz" + self.assertNotEqual(self._execute_upload(fixture).returncode, 0) + self.assertEqual(list((remote / "desktop-stable").iterdir()), []) + self.assertFalse((remote / ("v" + version) / "desktop-updater.json").exists()) + + def test_newer_channel_pointer_survives_an_older_release_retry(self): + fixture = self._upload_fixture("stable", "1.2.4", "v1.2.4", False) + _, _, _, _, _, remote = fixture + pointer = remote / "desktop-stable/desktop-updater.json" + pointer.write_text('{"version":"1.2.5"}', encoding="utf-8") + result = self._execute_upload(fixture) + self.assertEqual(result.returncode, 0, result.stderr) + self.assertEqual(pointer.read_text(encoding="utf-8"), '{"version":"1.2.5"}') + + def test_failed_pointer_replacement_recovers_from_complete_release(self): + fixture = self._upload_fixture("stable", "1.2.4", "v1.2.4", False) + _, _, _, env, _, remote = fixture + pointer = remote / "desktop-stable/desktop-updater.json" + pointer.write_text('{"version":"1.2.3"}', encoding="utf-8") + env["GH_FAIL"] = "clobber:desktop-updater.json" + self.assertNotEqual(self._execute_upload(fixture).returncode, 0) + self.assertFalse(pointer.exists()) + feed_path = remote / "v1.2.4/desktop-updater.json" + self.assertTrue(feed_path.is_file()) + del env["GH_FAIL"] + result = self._execute_upload(fixture) + self.assertEqual(result.returncode, 0, result.stderr) + self.assertEqual(pointer.read_bytes(), feed_path.read_bytes()) + def test_upload_step_skips_absent_windows_artifact_on_stable(self): stable = self._run_upload_step("stable", "1.2.4", "v1.2.4", False) self.assertIn("release upload v1.2.4 dist/updater/LoopX.app.tar.gz", stable) @@ -309,7 +525,7 @@ def test_upload_step_skips_absent_windows_artifact_on_stable(self): "release upload v1.2.4 dist/updater/LoopX.app.tar.gz.sig", stable ) self.assertIn( - "release upload v1.2.4 dist/updater/desktop-updater.json --clobber", + "release upload v1.2.4 dist/updater/desktop-updater.json", stable, ) self.assertIn( @@ -334,8 +550,7 @@ def test_upload_step_skips_absent_windows_artifact_on_stable(self): feed_upload = next( index for index, line in enumerate(main) - if "dist/updater/desktop-updater.json --clobber" in line - and "desktop-main " not in line + if line.startswith("release upload desktop-main-123-1 dist/updater/desktop-updater.json") ) pointer_upload = next( index diff --git a/tests/presentation/test_chat_turn_queue.py b/tests/presentation/test_chat_turn_queue.py index a2f37d4909..b06b13d3ed 100644 --- a/tests/presentation/test_chat_turn_queue.py +++ b/tests/presentation/test_chat_turn_queue.py @@ -1,6 +1,7 @@ """Ordinary browser queue intent through real HTTP, store and native stdio.""" import json +import threading import time import pytest @@ -507,17 +508,39 @@ def test_concurrent_direct_recovery_serializes_the_original_send_and_readback(or assert len([row for row in capture.read_text().splitlines() if json.loads(row)["method"] == "turn/steer"]) == 1 -def test_proven_non_delivery_preserves_fifo_and_fresh_attempt_can_steer(ordinary): # noqa: F811 +def test_proven_non_delivery_preserves_fifo_and_fresh_attempt_can_steer(ordinary, monkeypatch): # noqa: F811 store, runtime, contexts, request, capture, *_ = ordinary sid, active, queued, _ = held_queue(ordinary) path = f"/api/chat/sessions/{sid}/queue/{queued}/steer" body = {"target_turn_id": active, "client_ingress_id": "not-attached"} + # Early reattachment can hide an observer that fails the live Turn. + observer_parked = threading.Event() + allow_probe = threading.Event() + observer_waited = threading.Event() + original_wait = runtime.closed.wait + + def observe_queue_wait(timeout=None): + if threading.current_thread() is runtime.session_queue_threads.get(sid): + if not observer_parked.is_set(): + observer_parked.set() + allow_probe.wait(5) + elif sid not in runtime.adapters: + observer_waited.set() + return original_wait(timeout) + + monkeypatch.setattr(runtime.closed, "wait", observe_queue_wait) + assert observer_parked.wait(5), "queue observer must reach its active-Turn wait" adapter = runtime.adapters.pop(sid) try: assert request(path, body)[1]["error_code"] == "live_steering_session_not_attached" + allow_probe.set() + assert observer_waited.wait(5), "queue observer must yield while the original worker owns the Turn" + assert store.load_turn(sid, active)["status"] == "running" + assert store.load_session(sid)["active_turn_id"] == active assert [t["turn_id"] for t in store.queued_turns(sid)] == [queued] finally: runtime.adapters[sid] = adapter + allow_probe.set() assert request(path, {**body, "client_ingress_id": "recovered"})[0] == 200 assert request(path, body)[0] == 409 assert store.load_turn(sid, queued)["_queue_steering"]["status"] == "delivered" diff --git a/tests/test_kunluncode_goal_mode.py b/tests/test_kunluncode_goal_mode.py index bd9fe3f927..91d22fb8d2 100644 --- a/tests/test_kunluncode_goal_mode.py +++ b/tests/test_kunluncode_goal_mode.py @@ -4,6 +4,7 @@ import inspect import json import subprocess +import sys from pathlib import Path from types import SimpleNamespace @@ -552,12 +553,14 @@ def test_installer_registers_only_the_owned_global_mcp_entry( monkeypatch: pytest.MonkeyPatch, ) -> None: calls: list[list[str]] = [] + entries: list[dict[str, object]] = [] monkeypatch.setattr(cli.shutil, "which", lambda name: f"/usr/bin/{name}") monkeypatch.setattr(cli, "_compatible_python", lambda _python: True) - monkeypatch.setattr(cli, "_configured_mcp_servers", lambda _binary: []) + monkeypatch.setattr(cli, "_configured_mcp_servers", lambda _binary: list(entries)) def capture(command: list[str], **_kwargs) -> subprocess.CompletedProcess[str]: calls.append(command) + entries.append({"name": cli.MCP_SERVER_NAME, "command": "/managed/.venv/bin/python", "args": [str(cli.MCP_SCRIPT)]}) return subprocess.CompletedProcess(command, 0, "", "") monkeypatch.setattr(cli, "_run", capture) @@ -642,6 +645,7 @@ def test_installer_refuses_to_replace_a_foreign_named_entry( def test_installer_restores_previous_entry_when_replacement_fails( monkeypatch: pytest.MonkeyPatch, + tmp_path: Path, ) -> None: calls: list[list[str]] = [] previous = { @@ -649,16 +653,20 @@ def test_installer_restores_previous_entry_when_replacement_fails( "command": "/user/custom-server", "args": ["--serve"], } + entries = [previous] + monkeypatch.setattr(cli, "DEFAULT_MCP_VENV", tmp_path / "managed" / ".venv") monkeypatch.setattr(cli.shutil, "which", lambda name: f"/usr/bin/{name}") monkeypatch.setattr(cli, "_compatible_python", lambda _python: True) - monkeypatch.setattr( - cli, "_configured_mcp_servers", lambda _binary: [previous] - ) + monkeypatch.setattr(cli, "_configured_mcp_servers", lambda _binary: list(entries)) def capture(command: list[str], **_kwargs) -> subprocess.CompletedProcess[str]: calls.append(command) + if command[1:3] == ["mcp", "remove"]: + entries.clear() if command[1:3] == ["mcp", "add"] and "/managed/.venv/bin/python" in command: return subprocess.CompletedProcess(command, 1, "", "new entry rejected") + if command[1:3] == ["mcp", "add"]: + entries.append(previous) return subprocess.CompletedProcess(command, 0, "", "") monkeypatch.setattr(cli, "_run", capture) @@ -720,13 +728,15 @@ def test_installer_normalizes_relative_python_to_an_absolute_path( ) -> None: interpreter = tmp_path / ".venv" / "bin" / "python" calls: list[list[str]] = [] + entries: list[dict[str, object]] = [] monkeypatch.chdir(tmp_path) monkeypatch.setattr(cli.shutil, "which", lambda name: f"/usr/bin/{name}") monkeypatch.setattr(cli, "_compatible_python", lambda _python: True) - monkeypatch.setattr(cli, "_configured_mcp_servers", lambda _binary: []) + monkeypatch.setattr(cli, "_configured_mcp_servers", lambda _binary: list(entries)) def capture(command: list[str], **_kwargs) -> subprocess.CompletedProcess[str]: calls.append(command) + entries.append({"name": cli.MCP_SERVER_NAME, "command": str(interpreter), "args": [str(cli.MCP_SCRIPT)]}) return subprocess.CompletedProcess(command, 0, "", "") monkeypatch.setattr(cli, "_run", capture) @@ -1793,3 +1803,22 @@ def test_mcp_pin_has_one_source_of_truth() -> None: assert cli.MCP_REQUIREMENT.startswith("mcp==") assert cli.MCP_SDK_VERSION not in probe_source assert "MCP_SDK_VERSION" in probe_source + + +@pytest.mark.parametrize("matches", [True, False]) +def test_optimized_python_rejects_wrong_mcp_version( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch, matches: bool +) -> None: + version = cli.MCP_SDK_VERSION if matches else "0.0.0" + (tmp_path / "sitecustomize.py").write_text( + "import importlib.metadata, sys, types\n" + f"importlib.metadata.version = lambda name: {version!r}\n" + "for name in ('mcp', 'mcp.server', 'mcp.server.fastmcp', " + "'loopx.kunluncode_goal_mode.server'):\n" + " sys.modules[name] = types.ModuleType(name)\n" + "sys.modules['mcp.server.fastmcp'].FastMCP = object\n", + encoding="utf-8", + ) + monkeypatch.setenv("PYTHONPATH", str(tmp_path)) + monkeypatch.setenv("PYTHONOPTIMIZE", "1") + assert cli._compatible_python(sys.executable) is matches diff --git a/tests/test_kunluncode_install_recovery.py b/tests/test_kunluncode_install_recovery.py new file mode 100644 index 0000000000..592ee90835 --- /dev/null +++ b/tests/test_kunluncode_install_recovery.py @@ -0,0 +1,422 @@ +"""Exercise installer recovery against an isolated KunlunCode command fixture.""" + +from __future__ import annotations + +import json +import os +import stat +import subprocess +import venv +from pathlib import Path + +import pytest + +from loopx.kunluncode_goal_mode import cli + + +FAKE_KUNLUN = r'''#!/usr/bin/env python3 +import json, os, sys, time +from pathlib import Path +path = Path(os.environ["KUNLUN_FIXTURE_STATE"]) +state = json.loads(path.read_text()) +mode = os.environ["KUNLUN_FIXTURE_MODE"] +args = sys.argv[1:] +def save(): + path.write_text(json.dumps(state)) +if args[:2] == ["config", "get"]: + if mode == "change_before_remove": + counter = path.with_name("config-reads") + reads = int(counter.read_text()) + 1 if counter.exists() else 1 + counter.write_text(str(reads)) + if reads == 2: + state["entries"] = [entry for entry in state["entries"] if entry.get("name") != "loopx-kunluncode"] + state["entries"].append({"name": "loopx-kunluncode", "command": "/fixture/concurrent", "args": []}) + save() + if mode == "race_before_restore" and "add_new" in state["events"] and not path.with_name("race-injected").exists(): + snapshot = list(state["entries"]) + state["entries"].append({"name": "loopx-kunluncode", "command": "/fixture/concurrent", "args": []}) + path.with_name("race-injected").touch() + save() + print(json.dumps(snapshot)) + elif mode == "invalid_config": + print("{}") + elif mode == "status_unavailable" and "add_new" in state["events"]: + sys.exit(7) + else: + print(json.dumps(state["entries"])) + sys.exit(0) +if args[:2] == ["mcp", "remove"]: + state["events"].append("remove") + if mode != "remove_intact_timeout": + state["entries"] = [entry for entry in state["entries"] if entry.get("name") != args[2]] + save() + if mode in ("remove_intact_timeout", "remove_absent_timeout"): + time.sleep(10) + sys.exit(0) +if args[:2] == ["mcp", "add"]: + command = args[args.index("--command") + 1] + arguments = args[args.index("--args") + 1:] if "--args" in args else [] + is_new = command == os.environ["KUNLUN_FIXTURE_NEW"] + state["events"].append("add_new" if is_new else "add_old") + others = [entry for entry in state["entries"] if entry.get("name") != args[2]] + if is_new and mode == "add_foreign_timeout": + state["entries"] = others + [{"name": args[2], "command": "/fixture/other", "args": []}] + elif (not is_new and mode != "failure_restore_no_write") or (is_new and mode in ("normal", "add_written_timeout", "add_written_failure")): + state["entries"] = others + [{"name": args[2], "command": command, "args": arguments}] + save() + if (is_new and mode in ("add_written_timeout", "add_absent_timeout", "add_foreign_timeout", "status_unavailable", "race_before_restore")) or (not is_new and mode == "failure_restore_written_timeout"): + time.sleep(10) + if is_new and mode in ("add_written_failure", "add_absent_failure", "failure_restore_no_write", "failure_restore_written_timeout"): + sys.exit(7) + sys.exit(0) +raise SystemExit("unexpected fixture command") +''' + + +@pytest.fixture +def kunlun_fixture(tmp_path: Path, monkeypatch: pytest.MonkeyPatch): + if os.name == "nt": + pytest.skip("the isolated executable fixture requires POSIX scripts") + tool = tmp_path / "kunluncode" + tool.write_text(FAKE_KUNLUN, encoding="utf-8") + tool.chmod(0o755) + interpreter = tmp_path / "adapter-python" + interpreter.write_text("#!/bin/sh\nexit 0\n", encoding="utf-8") + interpreter.chmod(0o755) + state = tmp_path / "state.json" + monkeypatch.setattr(cli, "DEFAULT_MCP_VENV", tmp_path / "managed" / ".venv") + previous = {"name": cli.MCP_SERVER_NAME, "command": "/fixture/old", "args": ["--serve"]} + unrelated = {"name": "unrelated-service", "command": "/fixture/unrelated", "args": ["--keep"]} + state.write_text(json.dumps({"entries": [previous, unrelated], "events": []}), encoding="utf-8") + monkeypatch.setenv("PATH", str(tmp_path) + os.pathsep + os.environ.get("PATH", "")) + monkeypatch.setenv("KUNLUN_FIXTURE_STATE", str(state)) + monkeypatch.setenv("KUNLUN_FIXTURE_NEW", str(interpreter)) + real_run = cli._run + + def bounded_run(command, **kwargs): + if command[1:3] in (["mcp", "add"], ["mcp", "remove"]): + kwargs["timeout"] = 0.8 + return real_run(command, **kwargs) + + monkeypatch.setattr(cli, "_run", bounded_run) + return interpreter, state, previous, unrelated + + +@pytest.mark.parametrize( + ("mode", "expected", "backup_retained"), + [ + ("add_written_timeout", "new", True), + ("add_absent_timeout", "missing", True), + ("add_foreign_timeout", "other", True), + ("status_unavailable", "missing", True), + ("remove_intact_timeout", "old", True), + ("remove_absent_timeout", "missing", True), + ], +) +def test_timeout_checks_state_before_recovery( + mode, expected, backup_retained, kunlun_fixture, monkeypatch: pytest.MonkeyPatch +): + interpreter, state_path, previous, unrelated = kunlun_fixture + monkeypatch.setenv("KUNLUN_FIXTURE_MODE", mode) + with pytest.raises(RuntimeError, match="timed out") as error: + cli.install_mcp(python=str(interpreter), dry_run=False, replace=True) + state = json.loads(state_path.read_text(encoding="utf-8")) + assert [item for item in state["entries"] if item.get("name") == unrelated["name"]] == [unrelated] + entries = [item for item in state["entries"] if item.get("name") == cli.MCP_SERVER_NAME] + if expected == "old": + assert entries == [previous] + elif expected == "new": + assert entries == [{"name": cli.MCP_SERVER_NAME, "command": str(interpreter), "args": [str(cli.MCP_SCRIPT)]}] + assert "requested entry is present" in str(error.value) + elif expected == "other": + assert entries == [{"name": cli.MCP_SERVER_NAME, "command": "/fixture/other", "args": []}] + assert "another entry is present" in str(error.value) + else: + assert entries == [] + assert ("could not be verified" if mode == "status_unavailable" else "no entry is present") in str(error.value) + assert "add_old" not in state["events"] + backups = list((cli.DEFAULT_MCP_VENV.parent / "recovery").glob("mcp-entry-*.json")) + assert bool(backups) is backup_retained + if backup_retained: + assert len(backups) == 1 and json.loads(backups[0].read_text(encoding="utf-8")) == previous + assert str(backups[0]) in str(error.value) + + +@pytest.mark.parametrize("mode", ["add_written_timeout", "add_absent_timeout"]) +def test_timeout_without_previous_entry_does_not_blindly_retry(mode, kunlun_fixture, monkeypatch): + interpreter, state_path, _, unrelated = kunlun_fixture + state_path.write_text(json.dumps({"entries": [unrelated], "events": []}), encoding="utf-8") + monkeypatch.setenv("KUNLUN_FIXTURE_MODE", mode) + with pytest.raises(RuntimeError, match="timed out"): + cli.install_mcp(python=str(interpreter), dry_run=False, replace=False) + state = json.loads(state_path.read_text(encoding="utf-8")) + assert unrelated in state["entries"] + assert state["events"] == ["add_new"] + targets = [item for item in state["entries"] if item.get("name") == cli.MCP_SERVER_NAME] + assert len(targets) == (1 if mode == "add_written_timeout" else 0) + + +@pytest.mark.parametrize("invalid", ["extra_metadata", "duplicate", "invalid_config"]) +def test_unrestorable_or_ambiguous_registration_is_never_removed( + invalid, kunlun_fixture, monkeypatch: pytest.MonkeyPatch +): + interpreter, state_path, previous, _ = kunlun_fixture + state = json.loads(state_path.read_text(encoding="utf-8")) + if invalid == "extra_metadata": + state["entries"][0]["env"] = {"TOKEN": "fixture"} + elif invalid == "duplicate": + state["entries"].append(dict(previous)) + state_path.write_text(json.dumps(state), encoding="utf-8") + monkeypatch.setenv("KUNLUN_FIXTURE_MODE", invalid) + with pytest.raises(RuntimeError): + cli.install_mcp(python=str(interpreter), dry_run=False, replace=True) + after = json.loads(state_path.read_text(encoding="utf-8")) + assert after == state + + +@pytest.mark.parametrize(("mode", "expected"), [("add_written_failure", "new"), ("add_absent_failure", "old")]) +def test_failed_add_rechecks_state_before_restoring(mode, expected, kunlun_fixture, monkeypatch): + interpreter, state_path, previous, unrelated = kunlun_fixture + monkeypatch.setenv("KUNLUN_FIXTURE_MODE", mode) + with pytest.raises(RuntimeError, match="registration failed"): + cli.install_mcp(python=str(interpreter), dry_run=False, replace=True) + state = json.loads(state_path.read_text(encoding="utf-8")) + assert unrelated in state["entries"] + entries = [item for item in state["entries"] if item.get("name") == cli.MCP_SERVER_NAME] + if expected == "old": + assert entries == [previous] + assert "add_old" in state["events"] + else: + assert entries == [{"name": cli.MCP_SERVER_NAME, "command": str(interpreter), "args": [str(cli.MCP_SCRIPT)]}] + assert "add_old" not in state["events"] + + +def test_restore_success_without_readback_does_not_claim_recovery(kunlun_fixture, monkeypatch): + interpreter, state_path, _, unrelated = kunlun_fixture + monkeypatch.setenv("KUNLUN_FIXTURE_MODE", "failure_restore_no_write") + with pytest.raises(RuntimeError, match="not verified"): + cli.install_mcp(python=str(interpreter), dry_run=False, replace=True) + assert json.loads(state_path.read_text(encoding="utf-8"))["entries"] == [unrelated] + + +def test_normal_failure_restoration_timeout_is_checked_before_reporting(kunlun_fixture, monkeypatch): + interpreter, state_path, previous, unrelated = kunlun_fixture + monkeypatch.setenv("KUNLUN_FIXTURE_MODE", "failure_restore_written_timeout") + with pytest.raises(RuntimeError, match="previous entry is present, but restoration timed out"): + cli.install_mcp(python=str(interpreter), dry_run=False, replace=True) + state = json.loads(state_path.read_text(encoding="utf-8")) + assert previous in state["entries"] and unrelated in state["entries"] + backup, = (cli.DEFAULT_MCP_VENV.parent / "recovery").glob("mcp-entry-*.json") + assert json.loads(backup.read_text(encoding="utf-8")) == previous + + +def test_timeout_backup_is_private_and_does_not_leak_arguments(kunlun_fixture, monkeypatch): + interpreter, state_path, previous, unrelated = kunlun_fixture + previous["args"] = ["--token", "fixture-secret"] + state_path.write_text(json.dumps({"entries": [previous, unrelated], "events": []}), encoding="utf-8") + monkeypatch.setenv("KUNLUN_FIXTURE_MODE", "add_absent_timeout") + with pytest.raises(RuntimeError, match="previous entry saved at") as error: + cli.install_mcp(python=str(interpreter), dry_run=False, replace=True) + backup, = (cli.DEFAULT_MCP_VENV.parent / "recovery").glob("mcp-entry-*.json") + assert json.loads(backup.read_text(encoding="utf-8")) == previous + assert stat.S_IMODE(backup.stat().st_mode) == 0o600 + assert "fixture-secret" not in str(error.value) + + +def test_successful_replacement_removes_backup_only_after_readback(kunlun_fixture, monkeypatch): + interpreter, state_path, _, unrelated = kunlun_fixture + monkeypatch.setenv("KUNLUN_FIXTURE_MODE", "normal") + assert cli.install_mcp(python=str(interpreter), dry_run=False, replace=True) == str(interpreter) + state = json.loads(state_path.read_text(encoding="utf-8")) + assert unrelated in state["entries"] + assert {"name": cli.MCP_SERVER_NAME, "command": str(interpreter), "args": [str(cli.MCP_SCRIPT)]} in state["entries"] + assert list((cli.DEFAULT_MCP_VENV.parent / "recovery").glob("mcp-entry-*.json")) == [] + + +def test_success_without_matching_readback_preserves_backup(kunlun_fixture, monkeypatch): + interpreter, state_path, previous, unrelated = kunlun_fixture + monkeypatch.setenv("KUNLUN_FIXTURE_MODE", "add_success_no_write") + with pytest.raises(RuntimeError, match="did not match readback"): + cli.install_mcp(python=str(interpreter), dry_run=False, replace=True) + assert json.loads(state_path.read_text(encoding="utf-8"))["entries"] == [unrelated] + backup, = (cli.DEFAULT_MCP_VENV.parent / "recovery").glob("mcp-entry-*.json") + assert json.loads(backup.read_text(encoding="utf-8")) == previous + + +def test_insecure_backup_directory_prevents_removal(kunlun_fixture, monkeypatch): + interpreter, state_path, previous, unrelated = kunlun_fixture + directory = cli.DEFAULT_MCP_VENV.parent / "recovery" + directory.mkdir(parents=True, mode=0o755) + directory.chmod(0o755) + monkeypatch.setenv("KUNLUN_FIXTURE_MODE", "normal") + with pytest.raises(RuntimeError, match="could not be backed up"): + cli.install_mcp(python=str(interpreter), dry_run=False, replace=True) + assert json.loads(state_path.read_text(encoding="utf-8")) == {"entries": [previous, unrelated], "events": []} + + +def test_symlinked_backup_directory_prevents_removal(kunlun_fixture, monkeypatch, tmp_path): + interpreter, state_path, previous, unrelated = kunlun_fixture + parent = cli.DEFAULT_MCP_VENV.parent + parent.mkdir(parents=True) + target = tmp_path / "elsewhere" + target.mkdir() + (parent / "recovery").symlink_to(target, target_is_directory=True) + monkeypatch.setenv("KUNLUN_FIXTURE_MODE", "normal") + with pytest.raises(RuntimeError, match="could not be backed up"): + cli.install_mcp(python=str(interpreter), dry_run=False, replace=True) + assert json.loads(state_path.read_text(encoding="utf-8")) == {"entries": [previous, unrelated], "events": []} + assert list(target.iterdir()) == [] + + +def test_incomplete_backup_is_removed_and_registration_is_preserved(kunlun_fixture, monkeypatch): + interpreter, state_path, previous, unrelated = kunlun_fixture + monkeypatch.setenv("KUNLUN_FIXTURE_MODE", "normal") + + def fail_fsync(_descriptor): + raise OSError("synthetic sync failure") + + monkeypatch.setattr(cli.os, "fsync", fail_fsync) + with pytest.raises(RuntimeError, match="could not be backed up"): + cli.install_mcp(python=str(interpreter), dry_run=False, replace=True) + assert json.loads(state_path.read_text(encoding="utf-8")) == {"entries": [previous, unrelated], "events": []} + assert list((cli.DEFAULT_MCP_VENV.parent / "recovery").iterdir()) == [] + + +def test_duplicate_registration_is_not_removed_on_uninstall(kunlun_fixture, monkeypatch): + interpreter, state_path, _, _ = kunlun_fixture + entry = {"name": cli.MCP_SERVER_NAME, "command": str(interpreter), "args": [str(cli.MCP_SCRIPT)]} + state_path.write_text(json.dumps({"entries": [entry, entry], "events": []}), encoding="utf-8") + monkeypatch.setenv("KUNLUN_FIXTURE_MODE", "duplicate") + with pytest.raises(RuntimeError, match="multiple"): + cli.uninstall_mcp(dry_run=False) + assert json.loads(state_path.read_text(encoding="utf-8")) == {"entries": [entry, entry], "events": []} + + +def test_changed_registration_is_not_removed(kunlun_fixture, monkeypatch): + interpreter, state_path, previous, unrelated = kunlun_fixture + monkeypatch.setenv("KUNLUN_FIXTURE_MODE", "change_before_remove") + with pytest.raises(RuntimeError, match="changed before removal"): + cli.install_mcp(python=str(interpreter), dry_run=False, replace=True) + state = json.loads(state_path.read_text(encoding="utf-8")) + assert unrelated in state["entries"] + assert {entry["command"] for entry in state["entries"]} == {"/fixture/unrelated", "/fixture/concurrent"} + assert state["events"] == [] + backup, = (cli.DEFAULT_MCP_VENV.parent / "recovery").glob("mcp-entry-*.json") + assert json.loads(backup.read_text(encoding="utf-8")) == previous + + +def test_timeout_does_not_overwrite_entry_added_after_readback(kunlun_fixture, monkeypatch): + interpreter, state_path, _, unrelated = kunlun_fixture + monkeypatch.setenv("KUNLUN_FIXTURE_MODE", "race_before_restore") + with pytest.raises(RuntimeError, match="timed out"): + cli.install_mcp(python=str(interpreter), dry_run=False, replace=True) + state = json.loads(state_path.read_text(encoding="utf-8")) + assert unrelated in state["entries"] + assert any(entry.get("command") == "/fixture/concurrent" for entry in state["entries"]) + assert "add_old" not in state["events"] + + +def test_install_cli_reports_timeout_and_saved_backup_as_json(kunlun_fixture, monkeypatch, capsys): + interpreter, state_path, previous, unrelated = kunlun_fixture + monkeypatch.setenv("KUNLUN_FIXTURE_MODE", "add_absent_timeout") + assert cli.main(["install", "--python", str(interpreter), "--replace"]) == 1 + error = json.loads(capsys.readouterr().err) + assert error["ok"] is False + assert "timed out" in error["error"] and "previous entry saved at" in error["error"] + state = json.loads(state_path.read_text(encoding="utf-8")) + assert previous not in state["entries"] and unrelated in state["entries"] + backups = list((cli.DEFAULT_MCP_VENV.parent / "recovery").glob("mcp-entry-*.json")) + assert len(backups) == 1 and json.loads(backups[0].read_text()) == previous + + +@pytest.mark.parametrize("probe_state", ["match", "mismatch", "timeout"]) +def test_timed_out_uv_install_verifies_the_environment_before_reporting( + probe_state, tmp_path: Path, monkeypatch: pytest.MonkeyPatch +): + venv = tmp_path / ".venv" + python = cli._venv_python(venv) + python.parent.mkdir(parents=True) + python.touch() + monkeypatch.setattr(cli, "DEFAULT_MCP_VENV", venv) + monkeypatch.setattr(cli.shutil, "which", lambda _name: "/fixture/uv") + + def timeout(command, **_kwargs): + if command[0] == "/fixture/uv": + raise subprocess.TimeoutExpired(command, 300) + if command[0] == str(python) and probe_state == "timeout": + raise subprocess.TimeoutExpired(command, 30) + code = 0 if command[0] == str(python) and probe_state == "match" else 1 + return subprocess.CompletedProcess(command, code, "", "") + + monkeypatch.setattr(cli, "_run", timeout) + if probe_state == "match": + assert cli.provision_mcp_python() == str(python) + else: + with pytest.raises(RuntimeError, match="timed out"): + cli.provision_mcp_python() + + +@pytest.mark.parametrize("interpreter_exists", [True, False]) +def test_timed_out_venv_creation_reports_state_without_starting_install( + interpreter_exists, tmp_path: Path, monkeypatch: pytest.MonkeyPatch +): + venv_path = tmp_path / ".venv" + python = cli._venv_python(venv_path) + monkeypatch.setattr(cli, "DEFAULT_MCP_VENV", venv_path) + monkeypatch.setattr(cli.shutil, "which", lambda _name: "/fixture/uv") + calls = [] + + def timeout(command, **_kwargs): + if command[0] != "/fixture/uv": + return subprocess.CompletedProcess(command, 1, "", "") + calls.append(command) + if interpreter_exists: + python.parent.mkdir(parents=True) + python.touch() + raise subprocess.TimeoutExpired(command, 120) + + monkeypatch.setattr(cli, "_run", timeout) + with pytest.raises(RuntimeError, match="interpreter exists" if interpreter_exists else "no interpreter found"): + cli.provision_mcp_python() + assert len(calls) == 1 and calls[0][1] == "venv" + + +def test_provisioned_python_probe_verifies_versions_and_checkout(tmp_path: Path, monkeypatch): + env_dir = tmp_path / "adapter" + venv.create(env_dir, with_pip=False) + python = cli._venv_python(env_dir) + site = Path(subprocess.check_output( + [str(python), "-I", "-c", "import sysconfig; print(sysconfig.get_paths()['purelib'])"], text=True, + ).strip()) + loopx_package = site / "loopx" + (loopx_package / "kunluncode_goal_mode").mkdir(parents=True) + (loopx_package / "__init__.py").write_text(f"__version__ = {cli.LOOPX_VERSION!r}\n") + (loopx_package / "kunluncode_goal_mode/__init__.py").write_text("") + (loopx_package / "kunluncode_goal_mode/server.py").write_text("") + mcp_package = site / "mcp/server" + mcp_package.mkdir(parents=True) + (mcp_package.parent / "__init__.py").write_text("") + (mcp_package / "__init__.py").write_text("") + (mcp_package / "fastmcp.py").write_text("class FastMCP: pass\n") + loopx_meta = site / f"loopx-{cli.LOOPX_VERSION}.dist-info" + mcp_meta = site / f"mcp-{cli.MCP_SDK_VERSION}.dist-info" + loopx_meta.mkdir() + mcp_meta.mkdir() + (loopx_meta / "METADATA").write_text(f"Metadata-Version: 2.1\nName: loopx\nVersion: {cli.LOOPX_VERSION}\n") + metadata = mcp_meta / "METADATA" + metadata.write_text(f"Metadata-Version: 2.1\nName: mcp\nVersion: {cli.MCP_SDK_VERSION}\n") + monkeypatch.setenv("PYTHONPATH", str(tmp_path / "untrusted")) + assert cli._provisioned_python_matches(python, None) + assert cli._provisioned_python_matches(python, site) + assert not cli._provisioned_python_matches(python, tmp_path / "different-checkout") + checkout = tmp_path / "editable-checkout" + checkout.mkdir() + loopx_package.rename(checkout / "loopx") + (site / "loopx-editable.pth").write_text(str(checkout) + "\n") + assert cli._provisioned_python_matches(python, checkout) + assert not cli._provisioned_python_matches(python, site) + (loopx_meta / "METADATA").write_text("Metadata-Version: 2.1\nName: loopx\nVersion: 0.0.1\n") + assert not cli._provisioned_python_matches(python, checkout) + (loopx_meta / "METADATA").write_text(f"Metadata-Version: 2.1\nName: loopx\nVersion: {cli.LOOPX_VERSION}\n") + metadata.write_text("Metadata-Version: 2.1\nName: mcp\nVersion: 0.0.1\n") + assert not cli._provisioned_python_matches(python, None) diff --git a/tests/test_update_notes_patch.py b/tests/test_update_notes_patch.py new file mode 100644 index 0000000000..6b7f82d73e --- /dev/null +++ b/tests/test_update_notes_patch.py @@ -0,0 +1,38 @@ +"""The review artifact must contain both the new note and archive change.""" + +import os +from pathlib import Path +import subprocess +import textwrap + + +def test_update_note_patch_applies_new_note_and_archive(tmp_path: Path) -> None: + workflow = (Path(__file__).resolve().parents[1] / ".github/workflows/update-notes.yml").read_text() + step = workflow.split("- name: Package reviewable draft", 1)[1].split( + "- name: Upload update-note draft", 1 + )[0] + script = textwrap.dedent(step.split("run: |\n", 1)[1]) + repository = tmp_path / "source" + repository.mkdir() + + def git(*args: str, cwd: Path = repository) -> None: + subprocess.run(["git", *args], cwd=cwd, check=True, capture_output=True) + + git("init", "-q") + git("config", "user.name", "Fixture") + git("config", "user.email", "fixture@example.invalid") + notes = repository / "docs/update-notes" + notes.mkdir(parents=True) + (notes / "README.md").write_text("old archive\n") + git("add", "docs/update-notes/README.md") + git("commit", "-qm", "initial") + clean = tmp_path / "clean" + git("clone", "-q", str(repository), str(clean)) + note = "docs/update-notes/2026-01-01-to-2026-01-14.md" + (repository / note).write_text("new article\n") + (notes / "README.md").write_text("new archive\n") + subprocess.run(["bash", "-e", "-c", script], cwd=repository, check=True, + env={**os.environ, "NOTE_FILE": note}) + git("apply", str(repository / "update-note.patch"), cwd=clean) + assert (clean / note).read_text() == "new article\n" + assert (clean / "docs/update-notes/README.md").read_text() == "new archive\n"