@linxin666/dsh-remote-bridge 0.4.11 → 0.4.13

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (61) hide show
  1. package/README.i18n.yaml +2 -2
  2. package/README.md +47 -10
  3. package/README.zh.md +47 -10
  4. package/lib/client.js +1 -1
  5. package/lib/client.js.map +1 -1
  6. package/lib/index.js +18238 -416
  7. package/lib/types/core/connection.d.ts +9 -0
  8. package/lib/types/core/devices.d.ts +27 -6
  9. package/lib/types/core/liveactivity.d.ts +7 -0
  10. package/lib/types/core/model-efforts.d.ts +83 -0
  11. package/lib/types/core/protocol.d.ts +3 -1
  12. package/lib/types/core/push.d.ts +51 -2
  13. package/lib/types/core/turn-end-quiet.d.ts +104 -40
  14. package/lib/types/host/firewall.d.ts +74 -0
  15. package/lib/types/host/host-bind-capability.d.ts +90 -0
  16. package/lib/types/host/ingress-gate.d.ts +59 -0
  17. package/lib/types/host/ingress-sync.d.ts +429 -0
  18. package/lib/types/host/lan-bind-plan.d.ts +96 -0
  19. package/lib/types/host/lan-bind.d.ts +106 -0
  20. package/lib/types/host/lan-forward.d.ts +112 -0
  21. package/lib/types/host/lan.d.ts +61 -0
  22. package/lib/types/host/pair-origin.d.ts +34 -19
  23. package/lib/types/host/relay-bridge-connector.d.ts +215 -0
  24. package/lib/types/host/relay-connector.d.ts +152 -0
  25. package/lib/types/host/relay-control.d.ts +124 -0
  26. package/lib/types/host/relay-registry.d.ts +114 -0
  27. package/lib/types/host/relay.d.ts +170 -0
  28. package/lib/types/host/routes.d.ts +24 -1
  29. package/lib/types/host/tunnel-orphan.d.ts +145 -0
  30. package/lib/types/host/tunnel-plan.d.ts +41 -0
  31. package/lib/types/host/tunnel.d.ts +289 -0
  32. package/lib/types/index.d.ts +440 -11
  33. package/package.json +4 -2
  34. package/src/client/panel.module.css +16 -1
  35. package/src/core/connection.ts +244 -13
  36. package/src/core/devices.ts +31 -6
  37. package/src/core/hub.ts +17 -5
  38. package/src/core/liveactivity.ts +16 -2
  39. package/src/core/model-efforts.ts +223 -0
  40. package/src/core/protocol.ts +4 -1
  41. package/src/core/push.ts +70 -4
  42. package/src/core/turn-end-quiet.ts +281 -98
  43. package/src/host/firewall.ts +230 -0
  44. package/src/host/host-bind-capability.ts +249 -0
  45. package/src/host/ingress-gate.ts +203 -0
  46. package/src/host/ingress-sync.ts +1074 -0
  47. package/src/host/lan-bind-plan.ts +134 -0
  48. package/src/host/lan-bind.ts +250 -0
  49. package/src/host/lan-forward.ts +321 -0
  50. package/src/host/lan.ts +142 -0
  51. package/src/host/pair-origin.ts +44 -38
  52. package/src/host/relay-bridge-connector.ts +783 -0
  53. package/src/host/relay-connector.ts +317 -0
  54. package/src/host/relay-control.ts +224 -0
  55. package/src/host/relay-registry.ts +263 -0
  56. package/src/host/relay.ts +294 -0
  57. package/src/host/routes.ts +47 -3
  58. package/src/host/tunnel-orphan.ts +325 -0
  59. package/src/host/tunnel-plan.ts +58 -0
  60. package/src/host/tunnel.ts +688 -0
  61. package/src/index.ts +1303 -39
@@ -20,6 +20,7 @@ import type { DeviceRoster } from './devices.ts';
20
20
  import type { RemoteBridgeRegistry } from './extension.ts';
21
21
  import { type JobRoster } from './jobs.ts';
22
22
  import type { LiveActivityNotifier } from './liveactivity.ts';
23
+ import type { ModelEffortPreferences } from './model-efforts.ts';
23
24
  import type { PairingService } from './pairing.ts';
24
25
  import type { PushNotifier } from './push.ts';
25
26
  import type { QuestionClaims } from './questions.ts';
@@ -107,6 +108,14 @@ export interface BridgeConnectionOptions {
107
108
  readonly usage: UsageService;
108
109
  /** Host-process permission presets, read and switched through their real service objects. */
109
110
  readonly permissions: SessionPermissions;
111
+ /**
112
+ * 每个模型自己的思考档偏好表(`core/model-efforts.ts`)。
113
+ *
114
+ * 一个宿主一份、所有连接与所有会话共享:它记的是这个模型在这台机器上被选过
115
+ * 哪一档,与哪条会话无关(契约 §4)。它与宿主自己的 `reasoning.defaultEffort`
116
+ * 是两件事,`session.models` 用另一个键名 `preferredEffort` 公布它。
117
+ */
118
+ readonly modelEfforts: ModelEffortPreferences;
110
119
  /**
111
120
  * The in-process owner peer every gateway call speaks for, minted by the
112
121
  * bridge itself (`core/owner-peer.ts`). Dispatch refuses unpaired
@@ -18,13 +18,15 @@ export declare const DEVICE_ID_BYTES = 12;
18
18
  * On-disk schema version of the roster file.
19
19
  *
20
20
  * 1 carried the six identity fields only; 2 adds the optional `push` block per
21
- * row (§28). The difference is additive, so a version-1 file reads as-is: no row
22
- * loses a field, every paired phone stays paired, and the file is rewritten as
23
- * version 2 the next time anything is written. The reader therefore accepts any
24
- * version whose row table validates rather than quarantining a file it merely
25
- * does not recognise — a downgrade must never un-pair every phone.
21
+ * row (§28); 3 adds the optional `relayToken` inside that block, the credential the
22
+ * hosted relay signs a delivery for instead of a local Apple key. Every step is
23
+ * additive, so an older file reads as-is: no row loses a field, every paired phone
24
+ * stays paired, and the file is rewritten as the current version the next time
25
+ * anything is written. The reader therefore accepts any version whose row table
26
+ * validates rather than quarantining a file it merely does not recognise — a
27
+ * downgrade must never un-pair every phone.
26
28
  */
27
- export declare const ROSTER_VERSION = 2;
29
+ export declare const ROSTER_VERSION = 3;
28
30
  /** The APNs environments a device may register for. */
29
31
  export type PushEnvironment = 'sandbox' | 'production';
30
32
  /** One Live Activity push token, with the session the card belongs to. */
@@ -58,6 +60,16 @@ export interface PushRegistration {
58
60
  readonly bundle: string;
59
61
  readonly locale?: string;
60
62
  readonly liveActivityTokens?: readonly PushLiveActivityToken[];
63
+ /**
64
+ * The bearer the hosted relay accepts for this device's fingerprint.
65
+ *
66
+ * The App obtains it from the account service, scoped to `(userId, fingerprint)`
67
+ * and valid for 90 days, and hands it to this bridge through `push.register`. It
68
+ * is what lets a machine with no Apple signing key still deliver §28's events: the
69
+ * relay signs them with the key that never leaves the service. Absent is a phone
70
+ * whose only route is this machine's own key, which is the pre-relay behaviour.
71
+ */
72
+ readonly relayToken?: string;
61
73
  readonly registeredAt: number;
62
74
  }
63
75
  /** One device a notification can be addressed to. */
@@ -169,4 +181,13 @@ export declare class DeviceRoster {
169
181
  removePush(deviceToken: string): Promise<boolean>;
170
182
  /** @returns every paired device that registered for notifications. */
171
183
  pushTargets(): readonly PushTargetView[];
184
+ /**
185
+ * @returns every paired device that holds a relay token.
186
+ *
187
+ * The relay path's own reachability question: a row that registered an APNs
188
+ * token before the relay existed is a phone this machine cannot notify without a
189
+ * local key, so counting {@link pushTargets} there would claim a delivery that
190
+ * has no route at all.
191
+ */
192
+ relayTargets(): readonly PushTargetView[];
172
193
  }
@@ -187,6 +187,8 @@ export interface LiveActivityAddress {
187
187
  readonly environment: 'sandbox' | 'production';
188
188
  /** The app bundle id; the topic is this bundle plus the Live Activity suffix. */
189
189
  readonly bundle: string;
190
+ /** The relay credential this card's device row holds, when the bridge stands on the relay. */
191
+ readonly relayToken?: string;
190
192
  }
191
193
  /** Whatever actually reaches a card; the APNs client implements this. */
192
194
  export interface LiveActivitySender {
@@ -206,6 +208,11 @@ export interface LiveActivityNotifierOptions {
206
208
  readonly targets: () => readonly LiveActivityAddress[];
207
209
  /** The sender; absent is a bridge with no signing key, and then nothing happens. */
208
210
  readonly sender?: LiveActivitySender;
211
+ /**
212
+ * Whether the configured route is reachable right now; the same probe the alert
213
+ * channel beside it reads, so a bridge with neither route sends no card either.
214
+ */
215
+ readonly available?: () => boolean;
209
216
  readonly now?: () => number;
210
217
  readonly log?: (message: string) => void;
211
218
  /**
@@ -0,0 +1,83 @@
1
+ /**
2
+ * The host's per-model reasoning-effort preferences:
3
+ * `$DSH_HOME/remote-bridge/model-efforts.json`.
4
+ *
5
+ * A preference belongs to a MODEL, not to a session or a device: the phone picks
6
+ * a level for one model once, and every session on this host that later selects
7
+ * that model is handed the same level. The table therefore has exactly one scope
8
+ * — the whole host — and nothing expires an entry when a session ends.
9
+ *
10
+ * It is deliberately not the catalog's `reasoning.defaultEffort`: that is what
11
+ * the harness publishes for a model, this is what the user chose here, and the
12
+ * two disagree the moment somebody picks a level the catalog does not call the
13
+ * default. The wire keeps them apart too (`session.models` rows carry this one
14
+ * as `preferredEffort`, beside the catalog's own `reasoning`).
15
+ * @module dsh-remote-bridge/core/model-efforts
16
+ */
17
+ /** The schema version this build writes; an older file is read as the table it is. */
18
+ export declare const EFFORT_TABLE_VERSION = 1;
19
+ /** One remembered choice: the model it belongs to and the level it was given. */
20
+ export interface EffortPreference {
21
+ readonly provider: string;
22
+ readonly model: string;
23
+ readonly effort: string;
24
+ }
25
+ export interface ModelEffortPreferencesOptions {
26
+ /** Absolute path of the preference file. */
27
+ readonly file: string;
28
+ readonly log?: (message: string) => void;
29
+ }
30
+ /**
31
+ * The persisted effort table, shared by every connection of one host.
32
+ *
33
+ * Read once per process and written atomically: a corrupt file is an EMPTY table
34
+ * with one log line, never a failed mount — a phone must still be able to read
35
+ * the catalog and pick a model on a host whose preference file went bad, and the
36
+ * next write replaces the file rather than writing into the damage.
37
+ */
38
+ export declare class ModelEffortPreferences {
39
+ #private;
40
+ constructor(options: ModelEffortPreferencesOptions);
41
+ /**
42
+ * Read the table once. A missing file is an empty table, and an unreadable or
43
+ * malformed one is reported and then read as empty.
44
+ * @returns when the in-memory table reflects the file.
45
+ */
46
+ load(): Promise<void>;
47
+ /**
48
+ * @param provider - provider id.
49
+ * @param model - model id.
50
+ * @returns the level remembered for that model, or undefined when the table has none.
51
+ */
52
+ effortOf(provider: string, model: string): string | undefined;
53
+ /**
54
+ * Remember one level and persist it before answering.
55
+ *
56
+ * The write is what the caller is told succeeded, so the memory copy is set
57
+ * first only for the duration of it: a failed write rejects, and the answer
58
+ * the client sees is the failure rather than a level this host never stored.
59
+ * @param provider - provider id.
60
+ * @param model - model id.
61
+ * @param effort - the level chosen for that model.
62
+ */
63
+ save(provider: string, model: string, effort: string): Promise<void>;
64
+ /**
65
+ * Drop the remembered level for one model, but only the one judged stale.
66
+ *
67
+ * A level a model stopped publishing is worse than none at all: the backfill in
68
+ * `session.setModel` would send it, the harness would refuse, and the model
69
+ * would become unswitchable — and because that backfill is the only writer, the
70
+ * row could never heal itself. Dropping it makes the next switch plain.
71
+ *
72
+ * The `staleEffort` argument is what keeps that cure from being worse than the
73
+ * disease: an awaited catalog read sits between judging a level stale and
74
+ * arriving here, and frames of one connection are dispatched concurrently, so
75
+ * the user may have typed a NEW level in that window. A row that no longer holds
76
+ * the level this caller judged is a newer choice, and it survives.
77
+ * @param provider - provider id.
78
+ * @param model - model id.
79
+ * @param staleEffort - the level this caller found the catalog no longer offers.
80
+ * @param reason - why it is being dropped, for the log line.
81
+ */
82
+ forget(provider: string, model: string, staleEffort: string, reason: string): Promise<void>;
83
+ }
@@ -17,6 +17,8 @@ export declare const METHOD_SESSION_PERMISSIONS = "session.permissions";
17
17
  export declare const METHOD_SESSION_SET_PERMISSION = "session.setPermission";
18
18
  export declare const METHOD_SESSION_MODELS = "session.models";
19
19
  export declare const METHOD_SESSION_SET_MODEL = "session.setModel";
20
+ /** Remember one model's reasoning level, without switching the session's model. */
21
+ export declare const METHOD_SESSION_SET_EFFORT = "session.setEffort";
20
22
  export declare const METHOD_SESSION_CREATE = "session.create";
21
23
  export declare const METHOD_SESSION_FORK = "session.fork";
22
24
  export declare const METHOD_SESSION_COMMANDS = "session.commands";
@@ -483,7 +485,7 @@ export declare const CODE_COMMANDS_UNAVAILABLE = "commands-unavailable";
483
485
  * it is a fact about this bridge, not about what a particular host mounts — and
484
486
  * only a call that needs the domain fails. A refusal the `dsh-agents` plugin
485
487
  * itself raised crosses with the plugin's own code instead (`team-not-found`,
486
- * `member-not-found`, `preset-unknown`, `workspace-required` and their
488
+ * `member-not-found`, `preset-unknown`, `workspace-unavailable` and their
487
489
  * neighbours).
488
490
  */
489
491
  export declare const CODE_SQUADS_UNAVAILABLE = "squads-unavailable";
@@ -26,6 +26,16 @@
26
26
  */
27
27
  /** One thing a notification can be about. */
28
28
  export type PushEvent = 'turn-end' | 'turn-error' | 'question' | 'approval';
29
+ /**
30
+ * Which way this bridge delivers a notification.
31
+ *
32
+ * `direct` signs for APNs with this machine's own Apple key, `relay` asks the
33
+ * hosted service to sign with the one key no deployment may hold, and `off` is a
34
+ * bridge with neither route — the channel's original shape, byte for byte. The two
35
+ * routes are exclusive rather than a fallback chain: a machine holding a key keeps
36
+ * using it, so a developer's delivery never travels through the service.
37
+ */
38
+ export type PushRoute = 'direct' | 'relay' | 'off';
29
39
  /** Window inside which the same `(sessionId, event)` is one notification. */
30
40
  export declare const PUSH_DEDUPE_MS = 30000;
31
41
  /** Shortest gap between two notifications about the same session. */
@@ -42,6 +52,14 @@ export interface PushAddress {
42
52
  readonly environment: 'sandbox' | 'production';
43
53
  readonly bundle: string;
44
54
  readonly locale?: string;
55
+ /**
56
+ * The relay credential that same row registered, when it registered one.
57
+ *
58
+ * Present on the address rather than looked up by the sender so the two routes
59
+ * stay one decision: a row carries the token only in relay mode, and the sender
60
+ * that reads it is the only one this bridge built.
61
+ */
62
+ readonly relayToken?: string;
45
63
  }
46
64
  /** The request body one APNs delivery carries (protocol §28.4). */
47
65
  export interface PushNotification {
@@ -61,10 +79,19 @@ export interface PushNotification {
61
79
  /** What one delivery attempt reported. */
62
80
  export interface PushSendResult {
63
81
  readonly ok: boolean;
64
- /** The status APNs answered, when it answered at all. */
82
+ /** The status APNs answered, or the relay did, when it answered at all. */
65
83
  readonly status?: number;
66
84
  /** The reason APNs named, or this bridge's own note. */
67
85
  readonly reason?: string;
86
+ /**
87
+ * Whether the answer said this registration will never work again.
88
+ *
89
+ * APNs says it in a status and a reason this module can read; the relay says it
90
+ * in a count of tokens it just retired, which names no status of its own. The
91
+ * sender that learned it reports the fact here instead of borrowing an APNs
92
+ * status it did not receive.
93
+ */
94
+ readonly dead?: boolean;
68
95
  }
69
96
  /** One delivery a sender refused, in the words the bridge needs to read it. */
70
97
  export interface PushDeliveryFailure {
@@ -74,6 +101,8 @@ export interface PushDeliveryFailure {
74
101
  readonly status?: number;
75
102
  /** The reason APNs named, or this bridge's own note. */
76
103
  readonly reason?: string;
104
+ /** Whether this failure was final about the registration behind it. */
105
+ readonly dead?: boolean;
77
106
  }
78
107
  /** What one {@link PushNotifier.notify} ended up doing, once every attempt settled. */
79
108
  export interface PushDeliveryReport {
@@ -113,6 +142,18 @@ export interface PushNotifierOptions {
113
142
  readonly targets: () => readonly PushAddress[];
114
143
  /** The sender; absent is a bridge with no signing key, and then nothing happens. */
115
144
  readonly sender?: PushSender;
145
+ /**
146
+ * Whether the configured route can reach a phone right now.
147
+ *
148
+ * Absent means "the sender is reason enough", which is what a machine holding its
149
+ * own Apple key is: the key is configured or it is not. The relay has a second
150
+ * fact in front of the credential — some paired row must still hold a relay
151
+ * token — and reading it per event is what keeps a bridge with neither route
152
+ * exactly as inert as one with no key at all.
153
+ */
154
+ readonly available?: () => boolean;
155
+ /** Which route this notifier stands on; `direct` by default, as before the relay existed. */
156
+ readonly mode?: PushRoute;
116
157
  /** The session's own title (§26's source), read per event. */
117
158
  readonly titles?: (sessionId: string) => string | undefined;
118
159
  readonly now?: () => number;
@@ -151,8 +192,16 @@ export declare function pushEventOfTurnEnd(reason: string): PushEvent;
151
192
  export declare class PushNotifier {
152
193
  #private;
153
194
  constructor(options: PushNotifierOptions);
154
- /** @returns whether this bridge will actually deliver anything. */
195
+ /**
196
+ * @returns whether this bridge will actually deliver anything.
197
+ *
198
+ * Read by every entrance — a delta path, a claim, a watcher — so a bridge whose
199
+ * route is not armed is indistinguishable from one that never had a channel: no
200
+ * target is read, no clock is consulted and no timer is armed.
201
+ */
155
202
  get enabled(): boolean;
203
+ /** @returns which road a delivery would take: this machine's key or the hosted relay. */
204
+ get mode(): PushRoute;
156
205
  /**
157
206
  * Remember the workspace each session belongs to, from a roster this bridge
158
207
  * just served.
@@ -1,58 +1,107 @@
1
1
  /**
2
- * When a finished turn may be announced: after the session it belongs to falls
2
+ * When a finished turn may be announced: after the family it belongs to falls
3
3
  * quiet, not when the frame arrives.
4
4
  *
5
5
  * A `turn/end` increment is a fact about one turn and nothing more. The work the
6
- * user started with it may still be going: the child sessions it delegated to and
7
- * the background jobs the host still holds under those sessions outlive the turn
8
- * that started them. A phone told "turn finished" at the frame wakes on a session
9
- * the roster still badges as running, reads a card that says ended while the work
10
- * goes on, and is told again by a second turn end it never asked for. §28's
11
- * notification and §29's card answer the same question — "is this session working
12
- * right now" — and that question has one answer, the one the client already
13
- * computes in `SessionRun`: the host's own `agentRunning` for the session, any
14
- * descendant session still running, and any job the host attributes to either.
6
+ * user started with it may still be going: the subagent sessions the host spawned
7
+ * under it at any depth, and the background jobs the host still holds under any of
8
+ * them, outlive the turn that started them. A phone told "turn finished" at the
9
+ * frame wakes on a roster that still badges a session running, reads a card that
10
+ * says ended while the work goes on, and is told again by a second turn end it
11
+ * never asked for. §28's notification and §29's card answer the same question — "is
12
+ * this work still going" — and that question has one answer, the family's.
15
13
  *
16
- * So a turn end is registered here instead of sent, and sent once the host says
17
- * the session is quiet. Three rules hold that together:
14
+ * The family is the root a session's parent chain climbs to, plus every session
15
+ * under that root at any depth. A leaf session's own turn end therefore waits for
16
+ * the root and for the siblings and cousins it shares that root with, which is the
17
+ * work the user actually started. So a turn end is registered here instead of sent,
18
+ * and sent once the host says the family is quiet. Three rules hold that together:
18
19
  *
19
- * - the answer comes from the host and nowhere else: one `session/list` read for
20
- * the running flag and the parent chain, and the host's job registry for the
21
- * jobs it attributes to a session. This module invents no timer-based guess and
22
- * reads no client state, because a guess that says "quiet" is a lock screen
23
- * saying the work finished when it did not;
20
+ * - the answer comes from the host and nowhere else: one `session/list` read for the
21
+ * running flags, the parent chain and each session's origin, and the host's job
22
+ * registry for the jobs it attributes to a session. This module invents no
23
+ * timer-based guess and reads no client state, because a guess that says "quiet"
24
+ * is a lock screen saying the work finished when it did not;
24
25
  * - nothing on the frame path waits: a registration stores five fields and arms a
25
26
  * timer, so a follow stream still publishes its increment at the speed it did
26
27
  * before;
27
- * - the wait is bounded. At {@link TURN_END_QUIET_MAX_MS} the announcement goes
28
- * out anyway and the line it logs names what was still going, because a
29
- * notification that never arrives is the failure this channel exists to prevent.
28
+ * - the wait has no deadline. A family that stays busy is never announced, and a
29
+ * host that cannot answer is asked again rather than read as quiet: announcing
30
+ * work that is still running is the failure this channel exists to prevent, and a
31
+ * notification that never arrives is the lesser one. One question, though, is
32
+ * bounded ({@link TURN_END_QUIET_PROBE_TIMEOUT_MS}): a read the gateway never
33
+ * returns is abandoned as that question's own failure and the entry asks again, so
34
+ * a stuck call cannot leave the entry holding no question to ask and no way to
35
+ * speak. One line at {@link TURN_END_QUIET_REPORT_MS} says the bridge is still
36
+ * waiting, so an operator is not left guessing why a phone stayed silent.
37
+ *
38
+ * Not every turn end is the phone's to hear. A session the host spawned as a
39
+ * subagent (`origin: "subagent"`) ends turns nobody asked this bridge about, so its
40
+ * turn end is dropped on its first answer unless the phone itself prompted that
41
+ * session over the wire.
30
42
  * @module dsh-remote-bridge/core/turn-end-quiet
31
43
  */
32
44
  import type { JobRoster } from './jobs.ts';
33
45
  import type { LiveActivityNotifier } from './liveactivity.ts';
34
46
  import { type PushNotifier } from './push.ts';
35
47
  import { type BridgeGateway, type GatewayPeer } from './wire.ts';
36
- /** Longest a registered turn end waits for its session to fall quiet, in milliseconds. */
37
- export declare const TURN_END_QUIET_MAX_MS = 600000;
48
+ /**
49
+ * How long a held turn end waits before the bridge logs that it is still waiting,
50
+ * in milliseconds.
51
+ *
52
+ * A progress line and never a send: the wait itself has no cap, because announcing a
53
+ * turn while its family still works is exactly what this module exists to prevent.
54
+ */
55
+ export declare const TURN_END_QUIET_REPORT_MS = 600000;
38
56
  /**
39
57
  * How long a held turn end waits before asking the host again, in milliseconds.
40
58
  *
41
- * A turn end usually finds its session already quiet — the host clears the flag
42
- * as the turn finishes — so the first question is the one that matters, and five
59
+ * A turn end usually finds its family already quiet — the host clears the flag as
60
+ * the turn finishes — so the first question is the one that matters, and five
43
61
  * seconds is short enough that the notification a user waits for is not visibly
44
62
  * late. The interval is also what bounds how many questions one held turn end can
45
63
  * cost: at most one every five seconds, never more than one in flight.
46
64
  */
47
65
  export declare const TURN_END_QUIET_INTERVAL_MS = 5000;
48
- /** The host's answer about the work one session still holds. */
66
+ /**
67
+ * How long one question to the host may stay unanswered before the bridge abandons
68
+ * that question, in milliseconds.
69
+ *
70
+ * Three times {@link TURN_END_QUIET_INTERVAL_MS}. A `session/list` read the host is
71
+ * willing to make comes back well inside one interval, so a question that outlives
72
+ * three of them is a call that will not return — a stuck gateway request, not a slow
73
+ * host — and an entry that keeps waiting for it has no next question to ask, which is
74
+ * exactly how a phone goes permanently silent. The bound belongs to the question and
75
+ * never to the turn end: an abandoned question answers nothing, the entry stays held,
76
+ * and the next question leaves on the usual interval.
77
+ */
78
+ export declare const TURN_END_QUIET_PROBE_TIMEOUT_MS = 15000;
79
+ /**
80
+ * How many session ids the gate remembers the phone prompting, at most.
81
+ *
82
+ * The set exists to tell the phone's own subagent sessions from the ones it merely
83
+ * watches, and a bridge that runs for weeks must not grow it without end: the oldest
84
+ * id is dropped first, and a session dropped that way and prompted again enters as a
85
+ * new one.
86
+ */
87
+ export declare const TURN_END_PHONE_PROMPTED_MAX = 5000;
88
+ /** The host's answer about the work one session's family still holds. */
49
89
  export interface QuietAnswer {
50
- /** Whether nothing under this session is still going. */
90
+ /** Whether nothing in this session's family is still going. */
51
91
  readonly quiet: boolean;
52
92
  /** What was still going when it was not, in one token for the log line. */
53
93
  readonly busy?: string;
94
+ /**
95
+ * Present only when the host says another session spawned this one
96
+ * (`origin: "subagent"`).
97
+ *
98
+ * Absent is a host that said nothing about the session's origin, which is not the
99
+ * same as saying it has none: a turn end is announced for anything the host does
100
+ * not name as a subagent.
101
+ */
102
+ readonly subagent?: boolean;
54
103
  }
55
- /** Whether one session is quiet right now, as the host answers it. */
104
+ /** Whether one session's family is quiet right now, as the host answers it. */
56
105
  export type QuietProbe = (sessionId: string) => Promise<QuietAnswer>;
57
106
  export interface TurnEndQuietOptions {
58
107
  /** The notification path of §28; absent is a bridge that sends no notification. */
@@ -62,17 +111,22 @@ export interface TurnEndQuietOptions {
62
111
  /**
63
112
  * The host's own answer, asked off the frame path.
64
113
  *
65
- * Absent is a bridge assembled without one, which cannot tell a quiet session
66
- * from a busy one and therefore reports at the frame rather than holding
67
- * anything back.
114
+ * Absent is a bridge assembled without one, which cannot tell a quiet family from
115
+ * a busy one — nor a subagent session from the phone's own — and therefore reports
116
+ * at the frame rather than holding anything back.
68
117
  */
69
118
  readonly probe?: QuietProbe;
70
119
  readonly now?: () => number;
71
120
  readonly log?: (message: string) => void;
72
- /** The wait budget; the default is {@link TURN_END_QUIET_MAX_MS}, and a deployment moves it. */
73
- readonly maxWaitMs?: number;
121
+ /** When the still-waiting line is logged; the default is {@link TURN_END_QUIET_REPORT_MS}. */
122
+ readonly reportAfterMs?: number;
74
123
  /** The re-check interval; the default is {@link TURN_END_QUIET_INTERVAL_MS}. */
75
124
  readonly intervalMs?: number;
125
+ /**
126
+ * How many phone-prompted session ids to remember; the default and the ceiling are
127
+ * both {@link TURN_END_PHONE_PROMPTED_MAX}, and a deployment lowers it for a suite.
128
+ */
129
+ readonly phonePromptLimit?: number;
76
130
  }
77
131
  /**
78
132
  * The bridge's outbound turn-end path: registration now, announcement when quiet.
@@ -90,7 +144,7 @@ export declare class TurnEndQuietGate {
90
144
  /** @returns how many turn ends are held right now. */
91
145
  get held(): number;
92
146
  /**
93
- * Hold one finished turn until its session is quiet, then announce it.
147
+ * Hold one finished turn until its family is quiet, then announce it.
94
148
  *
95
149
  * Returns at once and never awaits a host read: the caller is a follow stream
96
150
  * that has already published its increment.
@@ -98,6 +152,16 @@ export declare class TurnEndQuietGate {
98
152
  * @param reason - the host's own reason for ending it.
99
153
  */
100
154
  noteTurnEnd(sessionId: string, reason: string): void;
155
+ /**
156
+ * Remember that the phone itself prompted one session.
157
+ *
158
+ * This is the one thing that makes a subagent session's turn end announceable: the
159
+ * host spawns those on its own, and a phone that never asked that session anything
160
+ * is not waiting to hear how its turn went. Bounded and first in, first out, so the
161
+ * memory is the recent work rather than everything since the bridge started.
162
+ * @param sessionId - the session a `session/prompt` frame carried.
163
+ */
164
+ notePhonePrompt(sessionId: string): void;
101
165
  /**
102
166
  * Drop a held turn end because another turn is on.
103
167
  *
@@ -121,14 +185,14 @@ export interface SessionQuietProbeOptions {
121
185
  readonly jobs: JobRoster;
122
186
  }
123
187
  /**
124
- * The host's answer to "is this session quiet", built from its own two facts.
188
+ * The host's answer to "is this session's family quiet", built from its own facts.
125
189
  *
126
- * The session list is where the running flag and the parent chain come from, and
127
- * the job registry is where a job's owner and status come from; neither is
128
- * captured once, because both move while a held turn end waits. A read that cannot
129
- * be made is thrown rather than answered as quiet: "the host did not answer" and
130
- * "nothing is running" are different facts, and the caller retries the one and
131
- * announces the other.
190
+ * The session list is where the running flags, the parent chain and each session's
191
+ * origin come from, and the job registry is where a job's owner and status come
192
+ * from; neither is captured once, because both move while a held turn end waits. A
193
+ * read that cannot be made is thrown rather than answered as quiet: "the host did
194
+ * not answer" and "nothing is running" are different facts, and the caller retries
195
+ * the one and announces the other.
132
196
  * @param options - the faces the answer is read through.
133
197
  * @returns the probe the gate asks.
134
198
  */
@@ -0,0 +1,74 @@
1
+ /**
2
+ * Host firewall management for the LAN bind toggle: while LAN access is on,
3
+ * keep one inbound allow rule for the bound port; when it turns off, remove
4
+ * the rule. Windows manages the Windows Defender Firewall rule via netsh;
5
+ * Linux uses the first available manager among firewalld / ufw / iptables.
6
+ * On every other platform (macOS included) the plugin reports the firewall
7
+ * as unmanaged: the port usually needs no rule there, and managing pf or the
8
+ * application firewall is out of scope for a distributable plugin.
9
+ *
10
+ * Ported from the dsh-LAN reference implementation (MIT), adapted to this
11
+ * package's structure and test seams.
12
+ */
13
+ /** Rule name shared by every backend so re-runs recreate the same rule. */
14
+ export declare const FIREWALL_RULE_NAME = "remote-web-ui (auto)";
15
+ /** One command execution result (test seam replaces the runner). */
16
+ export interface ToolResult {
17
+ ok: boolean;
18
+ out: string;
19
+ err: string;
20
+ /** true when the binary could not be spawned at all (ENOENT et al). */
21
+ missing?: boolean;
22
+ }
23
+ /** The command runner seam: spawnSync in production, a stub in tests. */
24
+ export type CommandRunner = (cmd: string, args: readonly string[]) => ToolResult;
25
+ /** Production runner: spawn without a shell, capture text, 20s timeout. */
26
+ export declare const spawnRunner: CommandRunner;
27
+ /** A firewall backend manages one allow rule for a TCP port. */
28
+ export interface FirewallBackend {
29
+ /** Short label surfaced in the settings card (netsh, firewalld, ...). */
30
+ readonly label: string;
31
+ ruleExists(port: number): boolean;
32
+ addRule(port: number): boolean;
33
+ removeRule(port: number): boolean;
34
+ }
35
+ /**
36
+ * Detect the platform firewall manager. Returns undefined when the platform
37
+ * has no supported manager (macOS, unknown Linux without tools): the port
38
+ * then usually needs no rule and the UI reports "unmanaged".
39
+ */
40
+ export declare function detectFirewallBackend(platform: NodeJS.Platform, run?: CommandRunner): FirewallBackend | undefined;
41
+ /** Cached detection for the running platform (per process). */
42
+ export declare function firewallBackend(): FirewallBackend | undefined;
43
+ /** Firewall state reported to the settings card. */
44
+ export interface FirewallSummary {
45
+ /** true when the rule state matches the LAN toggle (or nothing to manage). */
46
+ ok: boolean;
47
+ /** false when no supported firewall manager exists on this platform. */
48
+ managed: boolean;
49
+ /** Backend label or an explanation for unmanaged platforms. */
50
+ note?: string;
51
+ }
52
+ /**
53
+ * Delete-and-add: recreating the rule is idempotent and locale-proof (netsh
54
+ * output is localized, so parsing the live rule's port is fragile; the Linux
55
+ * backends follow the same recreate pattern).
56
+ */
57
+ export declare function ensureFirewallRule(port: number): boolean;
58
+ export declare function removeFirewallRule(port: number): boolean;
59
+ /**
60
+ * Human-readable firewall state for the status endpoint. A missing backend
61
+ * means the platform has nothing to manage; detection stays at the call site
62
+ * so an explicit undefined cannot silently re-probe the real OS mid-test.
63
+ */
64
+ export declare function computeFirewallSummary(port: number, lanEnabled: boolean, backend: FirewallBackend | undefined): FirewallSummary;
65
+ /** How long a firewallSummary answer is reused before the backend is probed again. */
66
+ export declare const FIREWALL_SUMMARY_TTL_MS = 30000;
67
+ /** Forget the cached summary (rule mutations call this). */
68
+ export declare function invalidateFirewallSummary(): void;
69
+ /**
70
+ * The settings card polls this every ten seconds; the probes are blocking
71
+ * spawnSync subprocesses on managed platforms, so short-TTL memoization
72
+ * keeps one poll from freezing the host event loop per request.
73
+ */
74
+ export declare function firewallSummary(port: number, lanEnabled: boolean): FirewallSummary;