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
5 changes: 3 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Every pull request and every push to main: the app builds, its tests pass, and the bridge's
# scripts parse.
# Every pull request and every push to main: the app builds, its tests pass, the bridge's
# scripts parse, and the bridges' tests pass.
name: CI
on:
pull_request:
Expand All @@ -16,3 +16,4 @@ jobs:
- run: swift build
- run: swift test
- run: for file in bridge/*.mjs bridge/*.ts cli.mjs; do node --check "$file"; done
- run: node --test 'bridge/*.test.mjs'
3 changes: 2 additions & 1 deletion AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ how it is installed, the files in a mind it writes, and how to develop it.

- Swift in `Sources/`, the Node bridge in `bridge/` (it imports only Node and the published `iterate`
package), the installer in `cli.mjs`.
- Check before a pull request: `swift build`, `swift test`, and `node --check` on each changed script.
- Check before a pull request: `swift build`, `swift test`, `node --test 'bridge/*.test.mjs'`, and
`node --check` on each changed script.
- Look at a view without installing: the `--render-*` flags (README, "Developing").
- Install a build on this Mac with `node cli.mjs update`. Only one session reinstalls at a time.
- Every push to `main` is published to npm and reaches installed Macs within the hour.
15 changes: 11 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,11 @@ An act that was refused (the thing is off, or you said no) is no act and shows i

A lent Mac has a context of its own in the mind, `/devices/<name>`, with the name of its lend
(`itx.<name>`). The app adds events there, through `bridge/device-events.mjs`, as you (the iterate
CLI's own sign-in; the Mac's key lends and does nothing else):
CLI's own sign-in; the Mac's key lends and does nothing else). When that connection ends (a
session's sign-in lasts 4 hours, a network goes), the bridge connects again
(`bridge/mind-connection.mjs`: 1 s, then twice as long, at most 60 s); the events wait meanwhile
(at most 2,000: the oldest moves and keys go first), go up in order, and the app says its presence
again:

| Event | When | Payload |
| ------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------- |
Expand Down Expand Up @@ -354,7 +358,9 @@ a binary target of `Package.swift`, carried in the bundle's `Contents/Frameworks
the sink and one peer connection per watcher; the signalling's rules are `LiveWatchers` in
`VoiceMeetCore`, tested on their own. `bridge/device-live.mjs` connects to the mind as you,
subscribes to `device/live-signal`, appends the Mac's signals in order, and asks the facet
`record` for the ICE servers (STUN alone when it has none).
`record` for the ICE servers (STUN alone when it has none). It connects again when its connection
ends, and once a minute it checks that its subscription still hears (an ephemeral
`device/live-ping` that only it hears); watches that run go on through a reconnect, peer to peer.

## Capture: show an agent what you mean

Expand Down Expand Up @@ -457,15 +463,16 @@ Notes from the first runs (macOS 15.7.4, a MacBook Pro):

This repository is the app's home: `git clone https://github.com/iterate/mac-app`, then build and
install from the checkout with `node cli.mjs update` (or `./run-on-mac.sh`). A pull request is
checked by `.github/workflows/ci.yml` on a macOS runner: `swift build`, `swift test`, and every
script of the bridge parses. Every push to `main` publishes a version to npm
checked by `.github/workflows/ci.yml` on a macOS runner: `swift build`, `swift test`, every
script of the bridge parses, and the bridges' tests pass (`node --test 'bridge/*.test.mjs'`). Every push to `main` publishes a version to npm
(`.github/workflows/release.yml`), and a Mac that installed from npm updates itself within the hour.

```sh
./run-on-mac.sh # build, install in /Applications, open, follow the logs
./run-on-mac.sh --build-only # build and sign .build/iterate.app only
./run-on-mac.sh --logs # follow the app's log
swift test # the state machines' tests (needs Xcode): microphone, camera, lend, banner, the record, the mark, record.set's rule, live
node --test 'bridge/*.test.mjs' # the bridges' tests: a fake mind that ends sessions (1008) and subscriptions
npx . # the installer from this folder
npx . init /tmp/mac --no-install # a copy, without building or installing it
```
Expand Down
5 changes: 3 additions & 2 deletions Sources/Live/Bridge.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,16 @@ import VoiceMeetCore
/// back, as JSON lines.
///
/// - from the bridge: `{ signal: { from, want | description | candidate | bye }, who }`,
/// `{ iceServers: [...] }`, and `{ failed }` for the log;
/// `{ iceServers: [...] }`, `{ connected: true | false, failed? }` (the signalling is up or
/// down), and `{ failed }` for the log;
/// - to the bridge: `{ signal: { to, description, tracks } | { to, candidate } | { to, bye, reason } }`,
/// `{ started: { watcher, who, tracks } }` and `{ ended: { watcher, who, seconds, why } }`.
public final class LiveBridge: @unchecked Sendable {
private let process = Process()
private let input = Pipe()
private let lock = NSLock()
private var pending = ""
/// A line of the bridge for the log: `{ failed }`, `{ connected }`.
/// A line of the bridge for the log: `{ failed }`, and `{ connected: true | false }`.
public var said: ([String: Any]) -> Void = { _ in }
public var exited: (Int32) -> Void = { _ in }

Expand Down
12 changes: 12 additions & 0 deletions Sources/VoiceMeetMac/DeviceEvents.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ final class DeviceEvents: ObservableObject {
private var input: FileHandle?
private var publishesTo: Destination?
private var pending = ""
private var wasDown = false
private var timer: Timer?
private var monitors: [Any] = []
private var lastMoveAt = Date.distantPast
Expand Down Expand Up @@ -225,6 +226,16 @@ final class DeviceEvents: ObservableObject {
if failure != why { log.error("not published: \(why)") }
failure = why
}
// the bridge connected again: the mind hears the presence of now, not of before
if let up = said["connected"] as? Bool {
if up, wasDown {
log.info("connected again")
failure = nil
presence = nil
tick()
}
wasDown = !up
}
}
}

Expand All @@ -234,6 +245,7 @@ final class DeviceEvents: ObservableObject {
publisher = nil
publishesTo = nil
presence = nil
wasDown = false
}
}

Expand Down
14 changes: 10 additions & 4 deletions Sources/VoiceMeetMac/DeviceLive.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ final class DeviceLive: ObservableObject {
private var bridge: LiveBridge?
private var bridgeTo: Destination?
private var bridgeEndedAt = Date.distantPast
/// The bridge's word: its connection to the mind is up (`{ connected }`).
private var isSignalling = false
private var acts: [String: Int] = [:]
private var timer: Timer?

Expand Down Expand Up @@ -100,7 +102,7 @@ final class DeviceLive: ObservableObject {
func state() -> [String: Any] {
[
"watchers": watchers.map { ["watcher": $0.id, "who": $0.who, "tracks": $0.tracks, "since": Int($0.since.timeIntervalSince1970 * 1000)] },
"signalling": bridge != nil,
"signalling": bridge != nil && isSignalling,
]
}

Expand All @@ -110,20 +112,24 @@ final class DeviceLive: ObservableObject {
if destination != bridgeTo, let old = bridge {
peer.endAll(why: "the Mac is no longer lent to this mind")
old.stop()
(bridge, bridgeTo, bridgeEndedAt) = (nil, nil, Date())
(bridge, bridgeTo, bridgeEndedAt, isSignalling) = (nil, nil, Date(), false)
}
guard let destination, bridge == nil, Date().timeIntervalSince(bridgeEndedAt) > 10 else { return }
let made = LiveBridge(
node: destination.node, script: destination.script,
arguments: [destination.name, "--iterate", destination.iterateBin, "--mind", destination.mind], peer: peer)
made.said = { line in AppLog("live").info("bridge: \(line)") }
made.said = { [weak self] line in
AppLog("live").info("bridge: \(line)")
guard let up = line["connected"] as? Bool else { return }
DispatchQueue.main.async { MainActor.assumeIsolated { if self?.bridge === made { self?.isSignalling = up } } }
}
made.exited = { [weak self] status in
DispatchQueue.main.async {
MainActor.assumeIsolated {
guard let self, self.bridge === made else { return }
self.log.info("the bridge ended (status \(status))")
self.peer.endAll(why: "the signalling stopped")
(self.bridge, self.bridgeTo, self.bridgeEndedAt) = (nil, nil, Date())
(self.bridge, self.bridgeTo, self.bridgeEndedAt, self.isSignalling) = (nil, nil, Date(), false)
}
}
}
Expand Down
141 changes: 96 additions & 45 deletions bridge/device-events.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,59 +6,110 @@
//
// The app (Sources/VoiceMeetMac/DeviceEvents.swift) starts it and writes one JSON line for each
// event on stdin: { type, payload, ephemeral? }. An ephemeral event reaches whoever listens now and
// is in no log. It connects to the mind once, as the person (the iterate CLI's own sign-in: the
// Mac's key lends and does nothing else), and appends what came in every fifth of a second, in one
// call. stdout is JSON lines for the app's log: { sent } and { failed }.
// is in no log. It connects to the mind as the person (the iterate CLI's own sign-in: the Mac's
// key lends and does nothing else), connects again when that connection ends
// (mind-connection.mjs), and appends what came in every fifth of a second, in one call, in order.
// While the connection is down the events wait; a batch that failed waits again at the front.
// stdout is JSON lines for the app: { sent }, { failed, dropped? }, and { connected } from
// mind-connection.mjs: after `{ connected: true }` the app says its presence again.
import { realpathSync } from "node:fs";
import { dirname, join } from "node:path";
import { createInterface } from "node:readline";
import { pathToFileURL } from "node:url";
import { fileURLToPath, pathToFileURL } from "node:url";
import { parseArgs } from "node:util";
import { isConnectionError, lastingConnection } from "./mind-connection.mjs";

const { positionals, values } = parseArgs({
allowPositionals: true,
options: { iterate: { type: "string" }, mind: { type: "string" } },
});
const name = positionals[0];
if (!name || !values.iterate || !values.mind)
throw new Error("node device-events.mjs <name> --iterate <iterate.js> --mind <address>");
const say = (line) => process.stdout.write(`${JSON.stringify(line)}\n`);
/** The input events that go first when too many wait: presence and the rest are kept. */
const isInput = (event) => /^device\/(key-pressed|mouse-|scrolled)/.test(event?.type);

const { addressOf, connectMind } = await import(
pathToFileURL(join(dirname(dirname(values.iterate)), "dist/node.mjs")).href
);
const device = connectMind({ address: addressOf(values.mind), signIn: false }).then(({ itx }) =>
itx.cd(`/devices/${name}`),
);
// a failure is told with the first batch
device.catch(() => {});
/** The queue and its sender: `add(event)`, `send()` (also every `everyMs`), `end()`. */
export function deviceEvents({ name, connect, say, sleep, mostWaiting = 2_000, everyMs = 200 }) {
const link = lastingConnection({
connect,
say,
sleep,
connected: async (link) => (link.device = link.itx.cd(`/devices/${name}`)),
});
let waiting = [];
let sent = 0;
let refusals = 0;
let sending;

// At most this many wait: a Mac with no network drops the oldest moves and keys, it does not grow.
const mostWaiting = 2_000;
let waiting = [];
let sent = 0;
let isSending = false;
// At most `mostWaiting` wait: a Mac with no network drops its oldest moves and keys, it does
// not grow.
function bound() {
while (waiting.length > mostWaiting) {
const input = waiting.findIndex(isInput);
waiting.splice(input === -1 ? 0 : input, 1);
}
}

async function send() {
if (isSending || waiting.length === 0) return;
isSending = true;
const batch = waiting;
waiting = [];
try {
await (await device).append(...batch);
sent += batch.length;
say({ sent });
} catch (error) {
say({ failed: String(error?.message ?? error).slice(0, 300), dropped: batch.length });
async function sendOnce() {
if (waiting.length === 0) return;
const connection = await link.now();
const batch = waiting;
waiting = [];
try {
await connection.device.append(...batch);
sent += batch.length;
refusals = 0;
say({ sent });
} catch (error) {
const failed = String(error?.message ?? error).slice(0, 300);
const isBroken = isConnectionError(error);
// the mind refused the batch three times: it is dropped, so it holds back nothing
if (!isBroken && ++refusals >= 3) {
refusals = 0;
return say({ failed, dropped: batch.length });
}
waiting = [...batch, ...waiting];
bound();
if (isBroken) link.broke(connection, error);
else say({ failed });
}
}
isSending = false;
const send = () => (sending ??= sendOnce().finally(() => (sending = undefined)));
const timer = setInterval(() => send().catch(() => {}), everyMs);

return {
add(event) {
waiting.push(event);
bound();
},
send,
waiting: () => waiting,
/** What waits goes, for at most `ms`; then the connection ends. */
async end(ms = 5000) {
clearInterval(timer);
const flushed = (async () => {
while (waiting.length) await send();
})();
await Promise.race([flushed.catch(() => {}), new Promise((r) => setTimeout(r, ms).unref())]);
link.stop();
},
};
}
setInterval(send, 200);

for await (const line of createInterface({ input: process.stdin })) {
if (!line) continue;
waiting.push(JSON.parse(line));
if (waiting.length > mostWaiting) waiting.splice(0, waiting.length - mostWaiting);
if (process.argv[1] && realpathSync(process.argv[1]) === fileURLToPath(import.meta.url)) {
const { positionals, values } = parseArgs({
allowPositionals: true,
options: { iterate: { type: "string" }, mind: { type: "string" } },
});
const name = positionals[0];
if (!name || !values.iterate || !values.mind)
throw new Error("node device-events.mjs <name> --iterate <iterate.js> --mind <address>");
const { addressOf, connectMind } = await import(
pathToFileURL(join(dirname(dirname(values.iterate)), "dist/node.mjs")).href
);
const events = deviceEvents({
name,
connect: () => connectMind({ address: addressOf(values.mind), signIn: false }),
say: (line) => process.stdout.write(`${JSON.stringify(line)}\n`),
});
for await (const line of createInterface({ input: process.stdin })) {
if (line) events.add(JSON.parse(line));
}
// stdin closed: the app is gone, or it turned the events off
await events.end();
process.exit(0);
}
// stdin closed: the app is gone, or it turned the events off
await send();
process.exit(0);
Loading
Loading