@bridgebrew/sessions 0.0.0-stage → 0.0.7

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 (44) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +43 -2
  3. package/answers.ts +167 -0
  4. package/browser/branch-actions.tsx +166 -0
  5. package/browser/changes.tsx +415 -0
  6. package/browser/children.tsx +650 -0
  7. package/browser/conversation.tsx +2381 -0
  8. package/browser/decision-questions.tsx +191 -0
  9. package/browser/elapsed.tsx +44 -0
  10. package/browser/gallery.tsx +218 -0
  11. package/browser/index.tsx +3434 -0
  12. package/browser/media-origins.tsx +38 -0
  13. package/browser/mobile.tsx +663 -0
  14. package/browser/open-link.tsx +30 -0
  15. package/browser/pull-request.tsx +116 -0
  16. package/browser/questions.tsx +156 -0
  17. package/browser/session-scheduled.tsx +203 -0
  18. package/browser/subagent-view.tsx +136 -0
  19. package/browser/subagents.tsx +339 -0
  20. package/browser/tabs.tsx +228 -0
  21. package/browser/tsconfig.json +15 -0
  22. package/browser/views/view.tsx +1064 -0
  23. package/browser/views.tsx +166 -0
  24. package/contract.ts +287 -0
  25. package/delegations.ts +1975 -0
  26. package/diff.ts +88 -0
  27. package/docs/en/images.mdx +51 -0
  28. package/docs/en/index.mdx +22 -0
  29. package/docs/en/meta.json +4 -0
  30. package/docs/en/organize-sessions.mdx +53 -0
  31. package/docs/en/start-a-session.mdx +63 -0
  32. package/docs/meta.json +4 -0
  33. package/git.ts +223 -0
  34. package/listing.ts +382 -0
  35. package/package.json +1023 -4
  36. package/peers.ts +677 -0
  37. package/pull-request-watch.ts +282 -0
  38. package/pull-requests.ts +606 -0
  39. package/server.ts +5915 -0
  40. package/text.ts +19 -0
  41. package/tsconfig.json +3 -0
  42. package/viewer-activity.ts +57 -0
  43. package/views/examples.ts +153 -0
  44. package/views/library.ts +1116 -0
@@ -0,0 +1,282 @@
1
+ // A watched pull request's rules (research-086 S3), pure: what a new read of it is news for the agent, and the message
2
+ // that wakes it. The rules are the reference workbench's (research-086 §2, checked on 2026-11-08): new failed checks;
3
+ // the gate passed (the required checks, else all of them), again when a required one appears after it was told; the
4
+ // optional checks still pending when the gate passed, once, when they all settle; a new comment or review from another
5
+ // account; a new conflict; no check on a head after 15 minutes. Each check or conflict news is told once per head (a
6
+ // push starts over). A run of 10 comment-only wakes ends the watch (a bot answering a bot). Bridge's own additions: a
7
+ // comment wakes only from an account with write access or a bot (its text enters the agent's turn); no "no checks"
8
+ // while the pull request is a draft (a draft runs no CI in Bridge's repositories). The memory lives on the session's
9
+ // record, so a worker restart tells nothing twice.
10
+
11
+ /** A check as `readPullRequest` gives it (the `github` service, never imported). */
12
+ export interface Check {
13
+ name: string;
14
+ outcome: 'pending' | 'passed' | 'failed';
15
+ status: string;
16
+ required: boolean;
17
+ url?: string | undefined;
18
+ }
19
+ /** A comment or a review as `readPullRequest` gives it. */
20
+ export interface Remark {
21
+ id: string;
22
+ kind: 'comment' | 'review';
23
+ author?: string | undefined;
24
+ bot: boolean;
25
+ association: string;
26
+ text: string;
27
+ createdAt: string;
28
+ editedAt?: string | undefined;
29
+ url?: string | undefined;
30
+ reviewState?: string | undefined;
31
+ path?: string | undefined;
32
+ }
33
+ /** What the rules read of a pull request. */
34
+ export interface Read {
35
+ number: number;
36
+ url: string;
37
+ state: 'draft' | 'open' | 'merged' | 'closed';
38
+ baseBranch: string;
39
+ headSha: string;
40
+ mergeable: 'clean' | 'conflicting' | 'unknown';
41
+ viewer?: string | undefined;
42
+ author?: string | undefined;
43
+ checks: Check[];
44
+ remarks: Remark[];
45
+ }
46
+
47
+ /** What a watch remembers between reads (on the session's record). */
48
+ export interface WatchMemory {
49
+ startedAt: string;
50
+ headSha?: string | undefined;
51
+ failedChecks: string[];
52
+ passed: boolean;
53
+ passedChecks: string[];
54
+ owedChecks: string[];
55
+ remarksThrough: string;
56
+ remarkIds: string[];
57
+ conflicting: boolean;
58
+ /** Comment-only wakes in a row. */
59
+ wakes: number;
60
+ /** Since when this head waits for its first check (`undefined`: not waiting). */
61
+ checksAwaitedSince?: string | undefined;
62
+ /** The last read saw a draft. */
63
+ draft?: true | undefined;
64
+ }
65
+
66
+ export type Change =
67
+ | { kind: 'checks-failed'; failed: Check[] }
68
+ | { kind: 'checks-passed'; count: number; required: boolean }
69
+ | { kind: 'remarks'; remarks: Remark[] }
70
+ | { kind: 'no-checks'; minutes: number }
71
+ | { kind: 'conflicting' };
72
+
73
+ export interface Report {
74
+ changes: Change[];
75
+ next: WatchMemory;
76
+ /** The comment-only wakes reached their limit: this wake is the last, the watch ends. */
77
+ exhausted: boolean;
78
+ }
79
+
80
+ export const WAKE_LIMIT = 10;
81
+ export const NO_CHECKS_MS = 15 * 60_000;
82
+ /** Failed reads in a row (rate limits, `gh` missing or signed out, the network: not counted) that end a watch. */
83
+ export const READ_FAILURE_LIMIT = 8;
84
+ const LISTED = 10;
85
+ const SNIPPET = 200;
86
+ /** The accounts whose comments wake: write access, as GitHub tells it. */
87
+ const WRITERS = ['OWNER', 'MEMBER', 'COLLABORATOR'];
88
+
89
+ /** A new watch's memory: comments from now on; checks and conflicts told as the first read finds them. */
90
+ export const startWatch = (now: number, kept?: WatchMemory): WatchMemory =>
91
+ kept
92
+ ? { ...kept, startedAt: new Date(now).toISOString(), wakes: 0 }
93
+ : {
94
+ startedAt: new Date(now).toISOString(),
95
+ failedChecks: [],
96
+ passed: false,
97
+ passedChecks: [],
98
+ owedChecks: [],
99
+ remarksThrough: new Date(now).toISOString(),
100
+ remarkIds: [],
101
+ conflicting: false,
102
+ wakes: 0,
103
+ };
104
+
105
+ const failed = (check: Check) => check.outcome === 'failed';
106
+
107
+ export function evaluate(watch: WatchMemory, read: Read, now: number): Report {
108
+ const changes: Change[] = [];
109
+ const headSha = read.headSha || watch.headSha;
110
+ const headMoved = headSha !== watch.headSha;
111
+ const draft = read.state === 'draft';
112
+
113
+ let failedChecks = headMoved ? [] : watch.failedChecks;
114
+ let passed = headMoved ? false : watch.passed;
115
+ let passedChecks = headMoved ? [] : watch.passedChecks;
116
+ let owedChecks = headMoved ? [] : watch.owedChecks;
117
+ if (read.checks.length > 0) {
118
+ const failing = read.checks.filter(failed);
119
+ const newly = failing.filter((check) => !failedChecks.includes(check.name));
120
+ if (newly.length) changes.push({ kind: 'checks-failed', failed: newly });
121
+ failedChecks = failing.map((check) => check.name);
122
+
123
+ const required = read.checks.filter((check) => check.required);
124
+ const gate = required.length ? required : read.checks;
125
+ const passedNow = gate.every((check) => check.outcome === 'passed');
126
+ const gateNames = gate.map((check) => check.name);
127
+ const told = passed && !passedChecks.length ? gateNames : passedChecks;
128
+ // A required check that appears after the gate was told passed is news; an optional one is not.
129
+ const gateGrew = required.length > 0 && gateNames.some((each) => !told.includes(each));
130
+ const owed =
131
+ passedNow && passed
132
+ ? read.checks
133
+ .filter((check) => !check.required && owedChecks.includes(check.name))
134
+ .map((check) => check.name)
135
+ : [];
136
+ const settled = !failing.length && read.checks.every((check) => check.outcome !== 'pending');
137
+ owedChecks = owed;
138
+ if (passedNow && (!passed || gateGrew)) {
139
+ changes.push({ kind: 'checks-passed', count: gate.length, required: required.length > 0 });
140
+ owedChecks = required.length
141
+ ? read.checks
142
+ .filter((check) => !check.required && check.outcome === 'pending')
143
+ .map((check) => check.name)
144
+ : [];
145
+ } else if (settled && owed.length) {
146
+ changes.push({ kind: 'checks-passed', count: read.checks.length, required: false });
147
+ owedChecks = [];
148
+ }
149
+ passed = passedNow;
150
+ passedChecks = passedNow ? gateNames : [];
151
+ }
152
+
153
+ // A head waits for its first check from its push, or from the moment its pull request left draft.
154
+ let checksAwaitedSince: string | undefined;
155
+ if (draft || read.checks.length) checksAwaitedSince = undefined;
156
+ else if (headMoved || watch.draft) checksAwaitedSince = new Date(now).toISOString();
157
+ else checksAwaitedSince = watch.checksAwaitedSince;
158
+ if (checksAwaitedSince !== undefined && now - Date.parse(checksAwaitedSince) >= NO_CHECKS_MS) {
159
+ changes.push({ kind: 'no-checks', minutes: Math.round((now - Date.parse(checksAwaitedSince)) / 60_000) });
160
+ checksAwaitedSince = undefined;
161
+ }
162
+
163
+ const own = (read.viewer ?? read.author)?.toLowerCase();
164
+ const through = Date.parse(watch.remarksThrough);
165
+ const at = (remark: Remark) => remark.editedAt || remark.createdAt;
166
+ // Comments stamped to the second: those at the cursor's very time are told apart by id.
167
+ const unseen = read.remarks.filter((remark) => {
168
+ const time = Date.parse(at(remark));
169
+ return (
170
+ (time > through || (time === through && !watch.remarkIds.includes(remark.id))) &&
171
+ remark.author?.toLowerCase() !== own
172
+ );
173
+ });
174
+ // Every unseen comment moves the cursor; only those of an account with write access, or a bot, wake.
175
+ const waking = unseen.filter((remark) => remark.bot || WRITERS.includes(remark.association));
176
+ if (waking.length) changes.push({ kind: 'remarks', remarks: waking });
177
+ const latest = Math.max(through, ...unseen.map((remark) => Date.parse(at(remark))));
178
+ const atLatest = unseen.filter((remark) => Date.parse(at(remark)) === latest);
179
+ const remarksThrough = latest === through ? watch.remarksThrough : at(atLatest[0]!);
180
+ const remarkIds = [
181
+ ...(latest === through ? watch.remarkIds : []),
182
+ ...atLatest.map((remark) => remark.id),
183
+ ];
184
+
185
+ // Like a failed check, a conflict a push did not resolve is news for the new head.
186
+ const toldConflict = !headMoved && watch.conflicting;
187
+ if (read.mergeable === 'conflicting' && !toldConflict) changes.push({ kind: 'conflicting' });
188
+ // `unknown`: GitHub still computing after a push; only a clean answer clears a conflict.
189
+ const conflicting = read.mergeable === 'unknown' ? toldConflict : read.mergeable === 'conflicting';
190
+
191
+ const commentsOnly = changes.length > 0 && changes.every((change) => change.kind === 'remarks');
192
+ const progress = headMoved || (changes.length > 0 && !commentsOnly);
193
+ const wakes = (progress ? 0 : watch.wakes) + (commentsOnly ? 1 : 0);
194
+ return {
195
+ changes,
196
+ next: {
197
+ startedAt: watch.startedAt,
198
+ ...(headSha && { headSha }),
199
+ failedChecks,
200
+ passed,
201
+ passedChecks,
202
+ owedChecks,
203
+ remarksThrough,
204
+ remarkIds,
205
+ conflicting,
206
+ wakes,
207
+ ...(checksAwaitedSince !== undefined && { checksAwaitedSince }),
208
+ ...(draft && { draft: true as const }),
209
+ },
210
+ exhausted: commentsOnly && wakes >= WAKE_LIMIT,
211
+ };
212
+ }
213
+
214
+ const snippet = (body: string) => {
215
+ const text = body
216
+ .replaceAll(/<!--[\s\S]*?-->/g, ' ')
217
+ .replaceAll(/\s+/g, ' ')
218
+ .trim();
219
+ if (text.length <= SNIPPET) return text;
220
+ // Half an emoji is a lone surrogate, which an agent's JSON parser refuses.
221
+ const last = text.charCodeAt(SNIPPET - 4);
222
+ return `${text.slice(0, last >= 0xd800 && last <= 0xdbff ? SNIPPET - 4 : SNIPPET - 3)}...`;
223
+ };
224
+ const listed = <T>(items: T[], line: (item: T) => string) => [
225
+ ...items.slice(0, LISTED).map(line),
226
+ ...(items.length > LISTED ? [` - and ${items.length - LISTED} more`] : []),
227
+ ];
228
+
229
+ const changeLines = (change: Change, base: string, commit: string): string[] => {
230
+ switch (change.kind) {
231
+ case 'checks-failed':
232
+ return [
233
+ `- Checks failed${commit}:`,
234
+ ...listed(
235
+ change.failed,
236
+ (check) =>
237
+ ` - ${check.name}${check.status === 'FAILURE' ? '' : ` (${check.status.toLowerCase()})`}${check.url ? ` ${check.url}` : ''}`,
238
+ ),
239
+ ];
240
+ case 'checks-passed':
241
+ return [
242
+ `- All ${change.count} ${change.required ? 'required ' : ''}${change.count === 1 ? 'check' : 'checks'} passed${commit}.`,
243
+ ];
244
+ case 'remarks':
245
+ return [
246
+ `- ${change.remarks.length} new ${change.remarks.length === 1 ? 'comment' : 'comments'}:`,
247
+ ...listed(change.remarks, (remark) => {
248
+ const where = remark.path ? ` on ${remark.path}` : '';
249
+ const body = snippet(remark.text);
250
+ const said = body ? `"${body}"` : (remark.reviewState?.toLowerCase().replaceAll('_', ' ') ?? 'reviewed');
251
+ return ` - ${remark.author ?? 'someone'}${where}: ${said}${remark.url ? ` ${remark.url}` : ''}`;
252
+ }),
253
+ ];
254
+ case 'no-checks':
255
+ return [
256
+ `- No checks have reported${commit} after ${change.minutes} minutes. None may be configured to run on this change.`,
257
+ ];
258
+ case 'conflicting':
259
+ return [`- The branch now conflicts with ${base}.`];
260
+ }
261
+ };
262
+
263
+ /** The message that wakes the agent with `report`'s changes. */
264
+ export const wakeText = (read: Read, report: Report) =>
265
+ [
266
+ `Update on pull request #${read.number} (${read.url}), which Bridge is watching for you:`,
267
+ ...report.changes.flatMap((change) =>
268
+ changeLines(change, read.baseBranch, report.next.headSha ? ` on ${report.next.headSha.slice(0, 7)}` : ''),
269
+ ),
270
+ '',
271
+ report.exhausted
272
+ ? `Bridge stopped watching after ${WAKE_LIMIT} comment-only updates in a row. Call watch_pull_request to watch it again.`
273
+ : 'Look into each item and act on it as your task requires. This pull request stays linked and watched, so do not link or watch it again. End your turn when you are done, and Bridge wakes you on the next change. When you hand the work back to the user, call unwatch_pull_request first so the session returns to their inbox.',
274
+ ].join('\n');
275
+
276
+ /** The message when a closed pull request ends the watch. */
277
+ export const closedText = (number: number, url: string) =>
278
+ `Pull request #${number} (${url}) was closed, so Bridge stopped watching it. Call watch_pull_request if it reopens.`;
279
+
280
+ /** The message when the reads kept failing. */
281
+ export const unreadableText = (number: number, url: string) =>
282
+ `Bridge stopped watching pull request #${number} (${url}) because it failed to read it from GitHub ${READ_FAILURE_LIMIT} times in a row. Check it yourself, and call watch_pull_request to watch it again.`;