Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions .github/workflows/macos.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
name: macOS checks

on:
pull_request:
paths:
- 'Runway/**'
- 'RunwayWidget/**'
- 'Shared/**'
- 'RunwayTests/**'
- 'Runway.xcodeproj/**'
- 'scripts/**'
- 'script/**'
- 'tests/install-selfcheck.py'
- '.github/workflows/macos.yml'
push:
branches: [main]
paths:
- 'Runway/**'
- 'RunwayWidget/**'
- 'Shared/**'
- 'RunwayTests/**'
- 'Runway.xcodeproj/**'
- 'scripts/**'
- 'script/**'
- 'tests/install-selfcheck.py'
- '.github/workflows/macos.yml'

permissions:
contents: read

jobs:
test:
runs-on: macos-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: '3.12'
- name: Verify installer replacement and rollback
run: python3 tests/install-selfcheck.py
- name: Test macOS app
run: >-
xcodebuild -project Runway.xcodeproj -scheme Runway
-configuration Debug -destination 'platform=macOS'
CODE_SIGN_IDENTITY=- CODE_SIGN_STYLE=Manual DEVELOPMENT_TEAM=
-quiet test
35 changes: 28 additions & 7 deletions Runway/RunwayApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ final class AppDelegate: NSObject, NSApplicationDelegate, NSWindowDelegate {
}
_ = AppUpdater.shared
if BackgroundRefreshAgent.isStableInstall {
WidgetRegistration.refresh()
AppRegistration.refresh()
}
let hasCurrentAccess = OnboardingStateStore.hasCurrentCodexDataAccess()
if OnboardingStateStore.completedCurrentVersion() {
Expand Down Expand Up @@ -177,16 +177,33 @@ final class AppDelegate: NSObject, NSApplicationDelegate, NSWindowDelegate {

}

private enum WidgetRegistration {
enum AppRegistration {
static func obsoletePaths(currentApp: URL, registeredApps: [URL]) -> [String] {
let current = currentApp.resolvingSymlinksInPath().standardizedFileURL
return Set(registeredApps.filter {
$0.resolvingSymlinksInPath().standardizedFileURL != current
}.map { $0.standardizedFileURL.path }).sorted()
}

static func refresh() {
guard Bundle.main.bundleIdentifier == "com.codexusage.CodexUsageMonitor" else { return }
let app = Bundle.main.bundleURL.path
let plugin = Bundle.main.bundleURL
.appendingPathComponent("Contents/PlugIns/RunwayWidget.appex").path
guard FileManager.default.fileExists(atPath: plugin) else { return }
let registered = ["com.codexusage.CodexUsageMonitor", "com.codexusage.CodexUsageMonitor.debug"]
.flatMap { NSWorkspace.shared.urlsForApplications(withBundleIdentifier: $0) }
let obsolete = obsoletePaths(currentApp: Bundle.main.bundleURL, registeredApps: registered)

DispatchQueue.global(qos: .utility).async {
run("/System/Library/Frameworks/CoreServices.framework/Versions/Current/Frameworks/LaunchServices.framework/Versions/Current/Support/lsregister", ["-f", "-R", "-trusted", app])
run("/usr/bin/pluginkit", ["-a", plugin])
let lsregister = "/System/Library/Frameworks/CoreServices.framework/Versions/Current/Frameworks/LaunchServices.framework/Versions/Current/Support/lsregister"
for path in obsolete {
run("/usr/bin/pluginkit", ["-r", path + "/Contents/PlugIns/RunwayWidget.appex"])
run(lsregister, ["-u", path])
}
run(lsregister, ["-f", "-R", "-trusted", app])
if FileManager.default.fileExists(atPath: plugin) {
run("/usr/bin/pluginkit", ["-a", plugin])
}
}
}

Expand All @@ -196,8 +213,12 @@ private enum WidgetRegistration {
process.arguments = arguments
process.standardOutput = FileHandle.nullDevice
process.standardError = FileHandle.nullDevice
try? process.run()
process.waitUntilExit()
do {
try process.run()
process.waitUntilExit()
} catch {
// Registration is retried on the next normal launch.
}
}
}

Expand Down
16 changes: 16 additions & 0 deletions RunwayTests/RunwayTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,22 @@ import SwiftUI
import Testing
@testable import Runway

@Test("Install registration keeps the current app and retires duplicate build and backup paths")
func appRegistrationObsoletePaths() throws {
let directory = FileManager.default.temporaryDirectory.appendingPathComponent(UUID().uuidString)
defer { try? FileManager.default.removeItem(at: directory) }
let current = directory.appendingPathComponent("Runway.app", isDirectory: true)
try FileManager.default.createDirectory(at: current, withIntermediateDirectories: true)
let alias = directory.appendingPathComponent("CodexUsageMonitor.app")
try FileManager.default.createSymbolicLink(at: alias, withDestinationURL: current)
let old = directory.appendingPathComponent("Build Products/Previous-Runway.app")
let debug = directory.appendingPathComponent("Debug/Runway.app")
let duplicate = directory.appendingPathComponent("Build Products/../Build Products/Previous-Runway.app")
#expect(AppRegistration.obsoletePaths(currentApp: current,
registeredApps: [current, alias, old, debug, duplicate]) == [old.path, debug.path].sorted())
#expect(AppRegistration.obsoletePaths(currentApp: current, registeredApps: [current, alias]).isEmpty)
}

@Test("Streaming JSONL handles split UTF-8, large rows, CRLF, malformed rows and a final row", arguments: [1, 7, 65_536])
func streamingJSONLBoundaries(chunkSize: Int) throws {
let url = FileManager.default.temporaryDirectory.appendingPathComponent(UUID().uuidString)
Expand Down
2 changes: 1 addition & 1 deletion script/build_and_run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ xcodebuild \
build

open_app() {
/usr/bin/open -n "$APP_BUNDLE"
/usr/bin/open "$APP_BUNDLE"
}

case "$MODE" in
Expand Down
97 changes: 73 additions & 24 deletions scripts/install_widget.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,43 @@
set -euo pipefail

ROOT="$(cd "$(dirname "$0")/.." && pwd)"
DERIVED_DATA="$ROOT/build/DerivedData"
DERIVED_DATA="$(mktemp -d "${TMPDIR:-/tmp}/runway-install.XXXXXX")"
APP="$DERIVED_DATA/Build/Products/Release/Runway.app"
DEST="/Applications/Runway.app"
PLUGIN="$DEST/Contents/PlugIns/RunwayWidget.appex"
AGENT="$HOME/Library/LaunchAgents/com.codexusage.Runway.refresh.plist"
LEGACY_APP="/Applications/CodexUsageMonitor.app"
LEGACY_AGENT="$HOME/Library/LaunchAgents/com.codexusage.CodexUsageMonitor.refresh.plist"
LSREGISTER="/System/Library/Frameworks/CoreServices.framework/Versions/Current/Frameworks/LaunchServices.framework/Versions/Current/Support/lsregister"
INSTALL_COMPLETED=0
LOCK_ACQUIRED=0
LOCKFILE="$HOME/Library/Caches/com.codexusage.Runway/install.lock"

cleanup_build() {
if [[ "$INSTALL_COMPLETED" -eq 0 ]]; then
for installed in "$DEST" "$LEGACY_APP"; do
previous="$DERIVED_DATA/Previous-$(basename "$installed")"
if [[ ( -e "$previous" || -L "$previous" ) && ! -e "$installed" && ! -L "$installed" ]]; then
if mv "$previous" "$installed"; then
"$LSREGISTER" -f "$installed" >/dev/null 2>&1 || true
else
echo "Could not restore $installed; recovery copy: $previous" >&2
fi
fi
done
fi
if [[ -d "$APP" ]]; then
pluginkit -r "$APP/Contents/PlugIns/RunwayWidget.appex" >/dev/null 2>&1 || true
"$LSREGISTER" -u "$APP" >/dev/null 2>&1 || true
fi
if [[ "$INSTALL_COMPLETED" -eq 0 && ( -e "$DERIVED_DATA/Previous-Runway.app" || -L "$DERIVED_DATA/Previous-Runway.app" || -e "$DERIVED_DATA/Previous-CodexUsageMonitor.app" || -L "$DERIVED_DATA/Previous-CodexUsageMonitor.app" ) ]]; then
echo "Previous application retained in $DERIVED_DATA" >&2
else
rm -rf "$DERIVED_DATA"
fi
if [[ "$LOCK_ACQUIRED" -eq 1 ]]; then rm -f "$LOCKFILE"; fi
}
trap cleanup_build EXIT

xcodebuild \
-project "$ROOT/Runway.xcodeproj" \
Expand All @@ -17,51 +47,70 @@ xcodebuild \
-destination 'platform=macOS' \
-derivedDataPath "$DERIVED_DATA" \
ENABLE_HARDENED_RUNTIME=NO \
"$@" \
-quiet \
build

mkdir -p "$(dirname "$LOCKFILE")"
if ! /usr/bin/shlock -p "$$" -f "$LOCKFILE"; then
echo "Another Runway installation is running." >&2
exit 1
fi
LOCK_ACQUIRED=1

for agent in "$AGENT" "$LEGACY_AGENT"; do
if [[ -f "$agent" ]]; then
launchctl bootout "gui/$(id -u)" "$agent" >/dev/null 2>&1 || true
label="$(basename "$agent" .plist)"
service="gui/$(id -u)/$label"
status=0
launchctl print "$service" >/dev/null 2>&1 || status=$?
if [[ "$status" -eq 0 ]]; then
launchctl bootout "$service"
elif [[ "$status" -ne 113 ]]; then
echo "Could not inspect $service (launchctl $status)" >&2
exit 1
fi
status=0
launchctl print "$service" >/dev/null 2>&1 || status=$?
if [[ "$status" -ne 113 ]]; then
echo "Could not retire $service" >&2
exit 1
fi
rm -f "$agent"

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Restore refresh-agent plists when installation rolls back

If staging, backup archival, unregistration, or replacement fails after this loop, both refresh-agent plists have already been deleted, but cleanup_build() only restores application bundles. The installer also terminates the running app before these operations and only reopens it on success, so an enabled background-refresh schedule remains disabled after rollback—even across login—until the user manually launches Runway again. The archival-failure fixture reproduces this: the previous app survives, but no agent plists remain and no app is reopened. Preserve the previous agent configuration and restore it on failure along with the application.

Useful? React with 👍 / 👎.

done
pkill -f '^/Applications/(Runway|CodexUsageMonitor)[.]app/Contents/' >/dev/null 2>&1 || true
BACKUP="$ROOT/build/install-backups/$(date -u +%Y%m%dT%H%M%SZ)"
BACKUP="$ROOT/build/install-backups.noindex/$(date -u +%Y%m%dT%H%M%SZ)"
mkdir -p "$BACKUP"
STAGED="$BACKUP/New-Runway.app"
STAGED="$DERIVED_DATA/New-Runway.app"
ditto "$APP" "$STAGED"
for installed in "$LEGACY_APP" "$DEST"; do
if [[ -d "$installed" && ! -L "$installed" ]]; then
archive="$BACKUP/Previous-$(basename "$installed").zip"
ditto -c -k --sequesterRsrc --keepParent "$installed" "$archive"
unzip -tq "$archive" >/dev/null
fi
done
if [[ -e "$LEGACY_APP" && ! -L "$LEGACY_APP" ]]; then
mv "$LEGACY_APP" "$BACKUP/Previous-CodexUsageMonitor.app"
"$LSREGISTER" -u "$LEGACY_APP"
mv "$LEGACY_APP" "$DERIVED_DATA/Previous-CodexUsageMonitor.app"
fi
if [[ -d "$DEST" ]]; then
mv "$DEST" "$BACKUP/Previous-Runway.app"
"$LSREGISTER" -u "$DEST"
mv "$DEST" "$DERIVED_DATA/Previous-Runway.app"
fi
if ! mv "$STAGED" "$DEST"; then
if [[ -d "$BACKUP/Previous-Runway.app" ]]; then
mv "$BACKUP/Previous-Runway.app" "$DEST"
fi
if [[ -d "$BACKUP/Previous-CodexUsageMonitor.app" ]]; then
mv "$BACKUP/Previous-CodexUsageMonitor.app" "$LEGACY_APP"
fi
exit 1
fi
if [[ -L "$LEGACY_APP" ]]; then rm "$LEGACY_APP"; fi
ln -s Runway.app "$LEGACY_APP"
if [[ -f "$LEGACY_AGENT" ]]; then
mv "$LEGACY_AGENT" "$BACKUP/Previous-CodexUsageMonitor.refresh.plist"
INSTALL_COMPLETED=1
if [[ -L "$LEGACY_APP" ]]; then
target="$(readlink "$LEGACY_APP")"
if [[ "$target" == Runway.app || "$target" == "$DEST" ]]; then rm "$LEGACY_APP"; fi
fi

/System/Library/Frameworks/CoreServices.framework/Versions/Current/Frameworks/LaunchServices.framework/Versions/Current/Support/lsregister \
-f -R -trusted "$DEST"
"$LSREGISTER" -f -R -trusted "$DEST"
pluginkit -a "$PLUGIN"
# WidgetKit keeps executable paths in existing sessions after an app rename.
/usr/bin/killall chronod >/dev/null 2>&1 || true

# Resume the existing refresh schedule immediately after replacement, including
# installs where the user does not reopen the workspace window.
if [[ -f "$AGENT" ]]; then
launchctl bootstrap "gui/$(id -u)" "$AGENT"
fi
/usr/bin/open "$DEST"

echo "Installed $DEST"
Expand Down
Loading
Loading