@andorra-labs/code 0.1.16 → 0.1.17
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.
- package/dist/src/acp-agent.d.ts +6 -0
- package/dist/src/acp-agent.js +15 -4
- package/dist/src/agent-execution.js +11 -8
- package/dist/src/agent-mcp-catalog.js +1 -0
- package/dist/src/agent-mcp.d.ts +3 -0
- package/dist/src/agent-mcp.js +33 -1
- package/dist/src/andorra/entry.d.ts +10 -0
- package/dist/src/andorra/entry.js +18 -4
- package/dist/src/andorra/hosted.d.ts +2 -1
- package/dist/src/andorra/hosted.js +2 -1
- package/dist/src/andorra/notices.d.ts +5 -0
- package/dist/src/andorra/notices.js +7 -0
- package/dist/src/andorra/state.d.ts +3 -2
- package/dist/src/andorra/state.js +4 -4
- package/dist/src/andorra/terminal.js +7 -2
- package/dist/src/anthropic-credentials.js +2 -0
- package/dist/src/app-server/error.d.ts +5 -0
- package/dist/src/app-server/error.js +7 -0
- package/dist/src/app-server/protocol.d.ts +19 -44
- package/dist/src/app-server/protocol.js +5 -1
- package/dist/src/app-server/workspace.d.ts +89 -0
- package/dist/src/app-server/workspace.js +457 -11
- package/dist/src/app-server.d.ts +358 -32
- package/dist/src/app-server.js +2429 -509
- package/dist/src/bridge/cloud.d.ts +27 -0
- package/dist/src/bridge/cloud.js +187 -0
- package/dist/src/bridge/command.d.ts +30 -0
- package/dist/src/bridge/command.js +347 -0
- package/dist/src/bridge/control.d.ts +68 -0
- package/dist/src/bridge/control.js +233 -0
- package/dist/src/bridge/engines.d.ts +90 -0
- package/dist/src/bridge/engines.js +601 -0
- package/dist/src/bridge/host-ops.d.ts +9 -0
- package/dist/src/bridge/host-ops.js +129 -0
- package/dist/src/bridge/host.d.ts +47 -0
- package/dist/src/bridge/host.js +540 -0
- package/dist/src/bridge/link.d.ts +38 -0
- package/dist/src/bridge/link.js +508 -0
- package/dist/src/bridge/login-link.d.ts +29 -0
- package/dist/src/bridge/login-link.js +54 -0
- package/dist/src/bridge/protocol.d.ts +136 -0
- package/dist/src/bridge/protocol.js +11 -0
- package/dist/src/bridge/store.d.ts +12 -0
- package/dist/src/bridge/store.js +61 -0
- package/dist/src/bridge/wording.d.ts +6 -0
- package/dist/src/bridge/wording.js +10 -0
- package/dist/src/browser-server.js +7 -1
- package/dist/src/browser-ui/tokens.css +1 -1
- package/dist/src/change-authors.d.ts +41 -0
- package/dist/src/change-authors.js +146 -0
- package/dist/src/cli.js +182 -32
- package/dist/src/coding-tools.js +15 -15
- package/dist/src/command-screens.d.ts +6 -0
- package/dist/src/command-screens.js +14 -11
- package/dist/src/commands.d.ts +14 -2
- package/dist/src/compaction-phrases.js +1 -1
- package/dist/src/completion.js +82 -5
- package/dist/src/config.d.ts +71 -3
- package/dist/src/config.js +202 -77
- package/dist/src/deadline.js +13 -1
- package/dist/src/decision/checks.js +1 -1
- package/dist/src/decision/requestPhrases.js +0 -1
- package/dist/src/default-graph.js +1 -1
- package/dist/src/file-diff.d.ts +4 -0
- package/dist/src/file-diff.js +2 -1
- package/dist/src/github-app/agent-writes.d.ts +102 -0
- package/dist/src/github-app/agent-writes.js +320 -0
- package/dist/src/github-app/client.d.ts +349 -0
- package/dist/src/github-app/client.js +810 -0
- package/dist/src/github-app/event-source.d.ts +208 -0
- package/dist/src/github-app/event-source.js +684 -0
- package/dist/src/github-app/metrics.d.ts +32 -0
- package/dist/src/github-app/metrics.js +49 -0
- package/dist/src/github-app/review-loop.d.ts +274 -0
- package/dist/src/github-app/review-loop.js +1177 -0
- package/dist/src/github-app/review-node.d.ts +81 -0
- package/dist/src/github-app/review-node.js +205 -0
- package/dist/src/github-app/review-post.d.ts +73 -0
- package/dist/src/github-app/review-post.js +139 -0
- package/dist/src/github-app/review-text.d.ts +107 -0
- package/dist/src/github-app/review-text.js +1133 -0
- package/dist/src/github-app/review-threads.d.ts +47 -0
- package/dist/src/github-app/review-threads.js +107 -0
- package/dist/src/github-app/reviewer.d.ts +121 -0
- package/dist/src/github-app/reviewer.js +598 -0
- package/dist/src/github-app/store.d.ts +59 -0
- package/dist/src/github-app/store.js +148 -0
- package/dist/src/github-app/triggers.d.ts +141 -0
- package/dist/src/github-app/triggers.js +441 -0
- package/dist/src/github-names.d.ts +8 -0
- package/dist/src/github-names.js +8 -0
- package/dist/src/glob-search.d.ts +61 -0
- package/dist/src/glob-search.js +180 -0
- package/dist/src/graph-health.js +6 -1
- package/dist/src/graph-manager-chat.js +1 -1
- package/dist/src/graph-preview.d.ts +46 -0
- package/dist/src/graph-preview.js +55 -0
- package/dist/src/graph-provider-portability.js +2 -1
- package/dist/src/graph-runner.js +64 -3
- package/dist/src/graph-server.js +3 -3
- package/dist/src/graph-sync.js +3 -3
- package/dist/src/graph-ui/app.js +91 -7
- package/dist/src/graph-ui/icons.js +2 -1
- package/dist/src/graph-ui/studio.js +15 -1
- package/dist/src/graph-ui/tokens.css +1 -1
- package/dist/src/graphs.d.ts +34 -1
- package/dist/src/graphs.js +158 -14
- package/dist/src/image-input.d.ts +16 -0
- package/dist/src/image-input.js +41 -0
- package/dist/src/membership.d.ts +10 -0
- package/dist/src/membership.js +19 -4
- package/dist/src/model-health-cache.js +1 -1
- package/dist/src/model-metadata.d.ts +6 -0
- package/dist/src/model-metadata.js +35 -15
- package/dist/src/model-relay/local.d.ts +116 -0
- package/dist/src/model-relay/local.js +468 -0
- package/dist/src/model-relay/remote.d.ts +129 -0
- package/dist/src/model-relay/remote.js +252 -0
- package/dist/src/model-relay/runs.d.ts +34 -0
- package/dist/src/model-relay/runs.js +146 -0
- package/dist/src/model-relay/wire.d.ts +5 -0
- package/dist/src/model-relay/wire.js +5 -0
- package/dist/src/project-registry.d.ts +21 -0
- package/dist/src/project-registry.js +74 -0
- package/dist/src/provider-import.d.ts +0 -12
- package/dist/src/provider-import.js +7 -195
- package/dist/src/providers.d.ts +23 -2
- package/dist/src/providers.js +25 -4
- package/dist/src/pull-request-logs.d.ts +12 -1
- package/dist/src/pull-request-logs.js +14 -3
- package/dist/src/pull-requests.d.ts +327 -7
- package/dist/src/pull-requests.js +429 -51
- package/dist/src/react-runtime.js +1 -1
- package/dist/src/remote/engine-pool.d.ts +88 -4
- package/dist/src/remote/engine-pool.js +366 -89
- package/dist/src/remote/roots.d.ts +19 -0
- package/dist/src/remote/roots.js +45 -7
- package/dist/src/remote/types.d.ts +0 -44
- package/dist/src/run-control.d.ts +7 -0
- package/dist/src/run-control.js +23 -0
- package/dist/src/run-records.d.ts +7 -2
- package/dist/src/run-records.js +71 -6
- package/dist/src/runtime.d.ts +5 -1
- package/dist/src/runtime.js +10 -2
- package/dist/src/session-changes.d.ts +35 -0
- package/dist/src/session-changes.js +194 -6
- package/dist/src/session-monitor.js +2 -1
- package/dist/src/session-reaper.d.ts +1 -1
- package/dist/src/session-reaper.js +6 -1
- package/dist/src/session-summary.d.ts +2 -1
- package/dist/src/session-summary.js +2 -1
- package/dist/src/sessions.d.ts +11 -1
- package/dist/src/sessions.js +17 -4
- package/dist/src/skills/bundled/diagram/SKILL.md +5 -3
- package/dist/src/skills/bundled/graph-manager/SKILL.md +7 -4
- package/dist/src/skills/bundled/provenance.json +2 -2
- package/dist/src/skills/runtime-skills.js +1 -1
- package/dist/src/telemetry/emitter.d.ts +15 -0
- package/dist/src/telemetry/emitter.js +35 -1
- package/dist/src/telemetry/schema.d.ts +25 -2
- package/dist/src/telemetry/schema.js +52 -4
- package/dist/src/telemetry/surface.d.ts +2 -1
- package/dist/src/telemetry/surface.js +4 -1
- package/dist/src/terminal-ui.d.ts +1 -0
- package/dist/src/terminal-ui.js +10 -3
- package/dist/src/test-edit-guard.d.ts +7 -0
- package/dist/src/test-edit-guard.js +49 -1
- package/dist/src/timers.d.ts +2 -0
- package/dist/src/tools.js +59 -9
- package/package.json +6 -1
- package/dist/src/remote/cert.d.ts +0 -11
- package/dist/src/remote/cert.js +0 -87
- package/dist/src/remote/client.d.ts +0 -119
- package/dist/src/remote/client.js +0 -544
- package/dist/src/remote/command.d.ts +0 -4
- package/dist/src/remote/command.js +0 -194
- package/dist/src/remote/errors.d.ts +0 -11
- package/dist/src/remote/errors.js +0 -14
- package/dist/src/remote/network.d.ts +0 -22
- package/dist/src/remote/network.js +0 -102
- package/dist/src/remote/pairing.d.ts +0 -63
- package/dist/src/remote/pairing.js +0 -299
- package/dist/src/remote/pem.d.ts +0 -2
- package/dist/src/remote/pem.js +0 -7
- package/dist/src/remote/serve.d.ts +0 -68
- package/dist/src/remote/serve.js +0 -244
- package/dist/src/remote/server.d.ts +0 -21
- package/dist/src/remote/server.js +0 -406
|
@@ -0,0 +1,684 @@
|
|
|
1
|
+
import { isCodeReviewer, listPullRequests, pullRequestNeedsWatch } from '../pull-requests.js';
|
|
2
|
+
import { GitHubAppError, isMentionNotice } from './client.js';
|
|
3
|
+
import { GitHubWatchMetrics } from './metrics.js';
|
|
4
|
+
import { engineId, readCursor, readSharedCoverage, writeCursor, writeSharedCoverage } from './store.js';
|
|
5
|
+
/**
|
|
6
|
+
* The engine's GitHub App event source (`design/specs/github-app.md`, "The event source"). It tells the
|
|
7
|
+
* account API which PRs this engine watches, pulls the notices the webhook receiver wrote for them, and turns
|
|
8
|
+
* each one into a re-read through `gh` by the pull-request watcher. A notice is a doorbell: it never carries
|
|
9
|
+
* PR text, and nothing is believed from it except that a PR changed.
|
|
10
|
+
*
|
|
11
|
+
* It acts only in the process that holds the watcher's lease, only while the engine is signed in to Andorra,
|
|
12
|
+
* and only while `github.app.enabled` is not false and `ANDORRA_GITHUB_APP` is not `off`. Otherwise every PR
|
|
13
|
+
* is polled through `gh` every 60 seconds, as before the App existed.
|
|
14
|
+
*/
|
|
15
|
+
export const PULL_INTERVAL_MS = 30_000;
|
|
16
|
+
export const PROBE_INTERVAL_MS = 10 * 60_000;
|
|
17
|
+
export const RENEW_INTERVAL_MS = 12 * 60 * 60_000;
|
|
18
|
+
export const UNCOVERED_RESEND_MS = 10 * 60_000;
|
|
19
|
+
export const MIN_SUBSCRIBE_GAP_MS = 10_000;
|
|
20
|
+
export const SUBSCRIBE_RETRY_MS = 60_000;
|
|
21
|
+
export const HEARTBEAT_MS = 60_000;
|
|
22
|
+
export const BREAKER_THRESHOLD = 3;
|
|
23
|
+
export const BREAKER_MIN_BACKOFF_MS = 60_000;
|
|
24
|
+
export const BREAKER_MAX_BACKOFF_MS = 5 * 60_000;
|
|
25
|
+
export const MAX_SUBSCRIBED = 50;
|
|
26
|
+
export const SEEN_IDS = 1_000;
|
|
27
|
+
const MAX_PAGES = 10;
|
|
28
|
+
const WAKE_NOTICE_TTL_MS = 15 * 60_000;
|
|
29
|
+
/** The shortest wait between two runs the source schedules itself, whatever is due: no due time in the past can spin it. */
|
|
30
|
+
export const MIN_TICK_MS = 1_000;
|
|
31
|
+
/** Renewals start this long before the service's `renewBy`, so one failed attempt does not lose coverage. */
|
|
32
|
+
export const RENEW_LEAD_MS = 15 * 60_000;
|
|
33
|
+
/**
|
|
34
|
+
* Coverage is never trusted longer than this after the last successful subscription, whatever `renewBy` says:
|
|
35
|
+
* the service stops writing notices for a subscription whose access check is older than 36 hours.
|
|
36
|
+
*/
|
|
37
|
+
export const COVERAGE_TTL_MS = 36 * 60 * 60_000;
|
|
38
|
+
/** A signed-out answer is believed this long (or until `nudge()`), so a signed-out engine does not take the auth lock every tick. */
|
|
39
|
+
export const SIGNED_OUT_RECHECK_MS = 10 * 60_000;
|
|
40
|
+
/** A non-holder believes the holder's `state.json` only this long; the holder rewrites it at least every minute. */
|
|
41
|
+
export const SHARED_STATE_TTL_MS = 3 * 60_000;
|
|
42
|
+
const SHARED_REWRITE_MS = 60_000;
|
|
43
|
+
const keyOf = (owner, repo, number) => `${owner.toLowerCase()}/${repo.toLowerCase()}#${number}`;
|
|
44
|
+
/** The watcher polls a PR in these states only as a 10-minute safety net: notices reach it. */
|
|
45
|
+
export const watcherCovered = (coverage) => coverage === 'covered' || coverage === 'permissions-pending';
|
|
46
|
+
export class GitHubEventSource {
|
|
47
|
+
options;
|
|
48
|
+
metrics;
|
|
49
|
+
now;
|
|
50
|
+
isReviewer;
|
|
51
|
+
/** off: signed out, switched off here, or nothing learned yet. disabled: the service said `enabled: false` or not allowed. */
|
|
52
|
+
mode = 'off';
|
|
53
|
+
offReason = 'pending';
|
|
54
|
+
breakerOpen = false;
|
|
55
|
+
failures = 0;
|
|
56
|
+
engine;
|
|
57
|
+
cursor;
|
|
58
|
+
cursorLoaded = false;
|
|
59
|
+
coverage = new Map();
|
|
60
|
+
limited = new Set();
|
|
61
|
+
subscribedKeys;
|
|
62
|
+
subscribedSent = new Set();
|
|
63
|
+
subscribedHeads = new Set();
|
|
64
|
+
lastSubscribeAt;
|
|
65
|
+
renewAt;
|
|
66
|
+
/** Until when the last subscription's coverage holds: past it, with no renewal, every PR is polled again. */
|
|
67
|
+
coverageValidUntil;
|
|
68
|
+
subscribeRequested = false;
|
|
69
|
+
subscribeRetryAt = 0;
|
|
70
|
+
subscribeFailed = false;
|
|
71
|
+
connected;
|
|
72
|
+
nextPullAt = 0;
|
|
73
|
+
nextProbeAt = 0;
|
|
74
|
+
seenIds = new Set();
|
|
75
|
+
seenOrder = [];
|
|
76
|
+
watched = new Map();
|
|
77
|
+
holder = false;
|
|
78
|
+
wakeNotices = new Map();
|
|
79
|
+
ticking;
|
|
80
|
+
again = false;
|
|
81
|
+
timer;
|
|
82
|
+
timerAt;
|
|
83
|
+
stopped = false;
|
|
84
|
+
started = false;
|
|
85
|
+
sharedWritten;
|
|
86
|
+
sharedWrittenAt = 0;
|
|
87
|
+
/** Until when a signed-out answer stands without asking again. */
|
|
88
|
+
signedOutUntil = 0;
|
|
89
|
+
/** Aborted by `stop()`: every account-API call in flight is cancelled with it. */
|
|
90
|
+
abort = new AbortController();
|
|
91
|
+
timers;
|
|
92
|
+
constructor(options) {
|
|
93
|
+
this.options = options;
|
|
94
|
+
this.metrics = options.metrics ?? new GitHubWatchMetrics();
|
|
95
|
+
this.now = options.now ?? Date.now;
|
|
96
|
+
this.isReviewer = options.isReviewer ?? (login => isCodeReviewer(login));
|
|
97
|
+
this.timers = options.timers ?? {
|
|
98
|
+
setTimeout: (callback, ms) => { const handle = setTimeout(callback, ms); handle.unref?.(); return handle; },
|
|
99
|
+
clearTimeout: handle => clearTimeout(handle),
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
start() {
|
|
103
|
+
if (this.started || this.stopped)
|
|
104
|
+
return;
|
|
105
|
+
this.started = true;
|
|
106
|
+
this.schedule(0);
|
|
107
|
+
}
|
|
108
|
+
async stop() {
|
|
109
|
+
this.stopped = true;
|
|
110
|
+
if (this.timer !== undefined)
|
|
111
|
+
this.timers.clearTimeout(this.timer);
|
|
112
|
+
this.timer = undefined;
|
|
113
|
+
this.abort.abort();
|
|
114
|
+
await this.ticking?.catch(() => { });
|
|
115
|
+
const summary = this.metrics.take(this.now());
|
|
116
|
+
if (summary)
|
|
117
|
+
await Promise.resolve(this.options.onSummary?.(summary)).catch(() => { });
|
|
118
|
+
}
|
|
119
|
+
/** Something changed here (a PR attached, finished or dismissed; a sign-in or sign-out): look again soon. */
|
|
120
|
+
nudge() {
|
|
121
|
+
// A sign-in, or anything else that nudges, is worth asking about the account again.
|
|
122
|
+
this.signedOutUntil = 0;
|
|
123
|
+
if (this.started && !this.stopped)
|
|
124
|
+
this.schedule(0);
|
|
125
|
+
}
|
|
126
|
+
/** Subscriptions are sent again on the next tick (after a disconnect, or a connection seen by `github.status`). */
|
|
127
|
+
refresh() {
|
|
128
|
+
this.subscribeRequested = true;
|
|
129
|
+
this.nudge();
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* What `github.status` just read from the service. A connection this source did not know of (the person
|
|
133
|
+
* connected GitHub in Settings), or the service on again while this source believed it off, is acted on now
|
|
134
|
+
* rather than at the next 10-minute probe.
|
|
135
|
+
*/
|
|
136
|
+
noteStatus(status) {
|
|
137
|
+
// `enabled: false` in any answer turns event mode off at once, this one included.
|
|
138
|
+
if (!status.enabled || !status.allowed) {
|
|
139
|
+
if (this.mode === 'events')
|
|
140
|
+
this.setDisabled(this.now());
|
|
141
|
+
return;
|
|
142
|
+
}
|
|
143
|
+
const connected = status.connection !== null;
|
|
144
|
+
const changed = this.connected !== undefined && this.connected !== connected;
|
|
145
|
+
this.connected = connected;
|
|
146
|
+
if (this.mode === 'disabled') {
|
|
147
|
+
this.mode = 'events';
|
|
148
|
+
this.nextPullAt = this.now();
|
|
149
|
+
this.refresh();
|
|
150
|
+
}
|
|
151
|
+
else if (changed)
|
|
152
|
+
this.refresh();
|
|
153
|
+
}
|
|
154
|
+
coverageOf(sessionId) {
|
|
155
|
+
if (this.mode === 'off')
|
|
156
|
+
return this.offReason;
|
|
157
|
+
if (this.mode === 'disabled')
|
|
158
|
+
return 'disabled';
|
|
159
|
+
if (this.breakerOpen)
|
|
160
|
+
return 'unavailable';
|
|
161
|
+
const entry = this.watched.get(sessionId);
|
|
162
|
+
if (!entry)
|
|
163
|
+
return 'pending';
|
|
164
|
+
const answer = this.coverage.get(entry.key);
|
|
165
|
+
if (!answer)
|
|
166
|
+
return this.limited.has(entry.key) ? 'limit' : this.subscribeFailed ? 'unavailable' : 'pending';
|
|
167
|
+
// Renewals have failed past the time the service vouched for: the PR is polled through gh again until one succeeds.
|
|
168
|
+
if (this.coverageExpired())
|
|
169
|
+
return 'unavailable';
|
|
170
|
+
return answer.covered ? 'covered' : answer.reason ?? 'not-covered';
|
|
171
|
+
}
|
|
172
|
+
coverageExpired() {
|
|
173
|
+
return this.coverageValidUntil !== undefined && this.now() >= this.coverageValidUntil;
|
|
174
|
+
}
|
|
175
|
+
/** For the watcher: whether notices reach this session's PR, so it is polled only every 10 minutes. */
|
|
176
|
+
covers(sessionId) { return watcherCovered(this.coverageOf(sessionId)); }
|
|
177
|
+
/** For the measures: one gh call the watcher made for a session. Nothing is counted while the source is off. */
|
|
178
|
+
countGhCall(sessionId) {
|
|
179
|
+
if (this.mode === 'off')
|
|
180
|
+
return;
|
|
181
|
+
this.metrics.call(sessionId, this.coverageOf(sessionId), this.now());
|
|
182
|
+
}
|
|
183
|
+
/** The watcher armed an Auto-fix wake: if a code-review notice caused it, its latency is recorded. */
|
|
184
|
+
woke(sessionId) {
|
|
185
|
+
const at = this.wakeNotices.get(sessionId);
|
|
186
|
+
if (at === undefined)
|
|
187
|
+
return;
|
|
188
|
+
this.wakeNotices.delete(sessionId);
|
|
189
|
+
const now = this.now();
|
|
190
|
+
this.metrics.wake(now - at, now);
|
|
191
|
+
}
|
|
192
|
+
get eventMode() {
|
|
193
|
+
if (this.mode === 'events' && !this.breakerOpen && [...this.watched.keys()].some(id => this.covers(id)))
|
|
194
|
+
return 'events';
|
|
195
|
+
return this.mode === 'off' ? 'off' : 'polling';
|
|
196
|
+
}
|
|
197
|
+
get modeReason() {
|
|
198
|
+
if (this.mode === 'off')
|
|
199
|
+
return this.offReason;
|
|
200
|
+
if (this.mode === 'disabled')
|
|
201
|
+
return 'disabled';
|
|
202
|
+
if (this.breakerOpen || this.coverageExpired())
|
|
203
|
+
return 'unavailable';
|
|
204
|
+
return undefined;
|
|
205
|
+
}
|
|
206
|
+
/** What `github.status` reports: this process's view when it holds the lease, else the holder's saved view. */
|
|
207
|
+
async snapshot() {
|
|
208
|
+
if (this.holder) {
|
|
209
|
+
return {
|
|
210
|
+
holder: true, mode: this.eventMode, ...(this.modeReason === undefined ? {} : { reason: this.modeReason }),
|
|
211
|
+
...(this.connected === undefined ? {} : { connected: this.connected }),
|
|
212
|
+
pullRequests: [...this.watched.values()].map(entry => ({ sessionId: entry.sessionId, url: entry.pullRequest.url, coverage: this.coverageOf(entry.sessionId) })),
|
|
213
|
+
};
|
|
214
|
+
}
|
|
215
|
+
const read = await readSharedCoverage(this.options.home).catch(() => undefined);
|
|
216
|
+
// A holder rewrites the file at least every minute; an older one is from a holder that is gone.
|
|
217
|
+
const shared = read !== undefined && this.now() - Date.parse(read.updatedAt) <= SHARED_STATE_TTL_MS ? read : undefined;
|
|
218
|
+
return {
|
|
219
|
+
holder: false, mode: shared?.mode ?? 'off', ...(shared?.reason === undefined ? {} : { reason: shared.reason }),
|
|
220
|
+
...(shared?.connected === undefined ? {} : { connected: shared.connected }),
|
|
221
|
+
pullRequests: Object.entries(shared?.pullRequests ?? {}).map(([sessionId, entry]) => ({ sessionId, url: entry.url, coverage: entry.coverage })),
|
|
222
|
+
};
|
|
223
|
+
}
|
|
224
|
+
schedule(delayMs) {
|
|
225
|
+
if (this.stopped || !this.started)
|
|
226
|
+
return;
|
|
227
|
+
const at = this.now() + Math.max(0, delayMs);
|
|
228
|
+
if (this.timer !== undefined && this.timerAt !== undefined && this.timerAt <= at)
|
|
229
|
+
return;
|
|
230
|
+
if (this.timer !== undefined)
|
|
231
|
+
this.timers.clearTimeout(this.timer);
|
|
232
|
+
this.timerAt = at;
|
|
233
|
+
this.timer = this.timers.setTimeout(() => {
|
|
234
|
+
this.timer = undefined;
|
|
235
|
+
this.timerAt = undefined;
|
|
236
|
+
void this.tick();
|
|
237
|
+
}, Math.max(0, delayMs));
|
|
238
|
+
}
|
|
239
|
+
/** One look at everything: tests drive it directly with a fake clock. */
|
|
240
|
+
tick() {
|
|
241
|
+
if (this.ticking) {
|
|
242
|
+
this.again = true;
|
|
243
|
+
return this.ticking;
|
|
244
|
+
}
|
|
245
|
+
this.ticking = (async () => {
|
|
246
|
+
try {
|
|
247
|
+
do {
|
|
248
|
+
this.again = false;
|
|
249
|
+
await this.run().catch(error => this.options.log?.('warn', `GitHub App events: ${error instanceof Error ? error.message : String(error)}`));
|
|
250
|
+
} while (this.again && !this.stopped);
|
|
251
|
+
}
|
|
252
|
+
finally {
|
|
253
|
+
this.ticking = undefined;
|
|
254
|
+
}
|
|
255
|
+
})();
|
|
256
|
+
return this.ticking;
|
|
257
|
+
}
|
|
258
|
+
setOff(reason) {
|
|
259
|
+
this.mode = 'off';
|
|
260
|
+
this.offReason = reason;
|
|
261
|
+
this.breakerOpen = false;
|
|
262
|
+
this.failures = 0;
|
|
263
|
+
this.coverage.clear();
|
|
264
|
+
this.limited.clear();
|
|
265
|
+
this.subscribedKeys = undefined;
|
|
266
|
+
this.subscribedSent.clear();
|
|
267
|
+
this.subscribedHeads.clear();
|
|
268
|
+
this.subscribeFailed = false;
|
|
269
|
+
this.connected = undefined;
|
|
270
|
+
this.renewAt = undefined;
|
|
271
|
+
this.coverageValidUntil = undefined;
|
|
272
|
+
}
|
|
273
|
+
setDisabled(now) {
|
|
274
|
+
if (this.mode !== 'disabled')
|
|
275
|
+
this.options.log?.('info', 'The GitHub App is turned off on the Andorra service; pull requests are polled through gh.');
|
|
276
|
+
this.mode = 'disabled';
|
|
277
|
+
this.breakerOpen = false;
|
|
278
|
+
this.failures = 0;
|
|
279
|
+
this.nextProbeAt = now + PROBE_INTERVAL_MS;
|
|
280
|
+
}
|
|
281
|
+
async list() {
|
|
282
|
+
const entries = await listPullRequests(this.options.home).catch(() => []);
|
|
283
|
+
const watched = new Map();
|
|
284
|
+
for (const { sessionId, pullRequest } of entries) {
|
|
285
|
+
if (!pullRequestNeedsWatch(pullRequest))
|
|
286
|
+
continue;
|
|
287
|
+
watched.set(sessionId, { sessionId, pullRequest, key: keyOf(pullRequest.owner, pullRequest.repo, pullRequest.number) });
|
|
288
|
+
}
|
|
289
|
+
return watched;
|
|
290
|
+
}
|
|
291
|
+
async run() {
|
|
292
|
+
if (this.stopped)
|
|
293
|
+
return;
|
|
294
|
+
const now = this.now();
|
|
295
|
+
this.watched = await this.list();
|
|
296
|
+
try {
|
|
297
|
+
// Nothing watched: nothing to do (not even reading the config), unless the service still holds a set to clear.
|
|
298
|
+
if (this.watched.size === 0 && !(this.mode === 'events' && this.subscribedSent.size > 0))
|
|
299
|
+
return;
|
|
300
|
+
if (!await this.options.locallyEnabled().catch(() => true)) {
|
|
301
|
+
this.setOff('disabled');
|
|
302
|
+
return;
|
|
303
|
+
}
|
|
304
|
+
if (this.mode === 'off' && this.offReason === 'signed-out' && now < this.signedOutUntil)
|
|
305
|
+
return;
|
|
306
|
+
const signedIn = await this.options.signedIn(this.abort.signal).catch(() => false);
|
|
307
|
+
if (this.stopped)
|
|
308
|
+
return;
|
|
309
|
+
if (!signedIn) {
|
|
310
|
+
this.setOff('signed-out');
|
|
311
|
+
this.signedOutUntil = now + SIGNED_OUT_RECHECK_MS;
|
|
312
|
+
return;
|
|
313
|
+
}
|
|
314
|
+
const held = this.holder;
|
|
315
|
+
if (this.stopped || !await this.options.watcher.claim().catch(() => false))
|
|
316
|
+
return;
|
|
317
|
+
if (!held) {
|
|
318
|
+
// Taken (back) from another process that may have pulled since: its cursor, not the one read before.
|
|
319
|
+
this.cursorLoaded = false;
|
|
320
|
+
this.options.onLeaseAcquired?.();
|
|
321
|
+
}
|
|
322
|
+
if (this.mode === 'off') {
|
|
323
|
+
this.mode = 'events';
|
|
324
|
+
this.subscribeRequested = true;
|
|
325
|
+
this.nextPullAt = now;
|
|
326
|
+
}
|
|
327
|
+
this.engine ??= await engineId(this.options.home);
|
|
328
|
+
if (!this.cursorLoaded) {
|
|
329
|
+
this.cursor = await readCursor(this.options.home).catch(() => undefined);
|
|
330
|
+
this.cursorLoaded = true;
|
|
331
|
+
}
|
|
332
|
+
if (this.mode === 'disabled' && !this.stopped)
|
|
333
|
+
await this.probeEnabled(now);
|
|
334
|
+
if (this.mode === 'events' && !this.stopped)
|
|
335
|
+
await this.maybeSubscribe(now);
|
|
336
|
+
if (this.mode === 'events' && !this.stopped)
|
|
337
|
+
await this.maybeProbeConnection(now);
|
|
338
|
+
if (this.mode === 'events' && !this.stopped)
|
|
339
|
+
await this.maybePull(now);
|
|
340
|
+
}
|
|
341
|
+
finally {
|
|
342
|
+
this.holder = this.options.watcher.holdsLease();
|
|
343
|
+
const at = this.now();
|
|
344
|
+
// Signed out or switched off, the source counts nothing and writes nothing: the engine behaves as before the App.
|
|
345
|
+
const active = this.mode !== 'off';
|
|
346
|
+
if (this.holder && active)
|
|
347
|
+
for (const entry of this.watched.values())
|
|
348
|
+
this.metrics.watched(entry.sessionId, this.coverageOf(entry.sessionId), at);
|
|
349
|
+
for (const [sessionId, noticeAt] of this.wakeNotices)
|
|
350
|
+
if (at - noticeAt > WAKE_NOTICE_TTL_MS)
|
|
351
|
+
this.wakeNotices.delete(sessionId);
|
|
352
|
+
if (this.metrics.due(at) && !this.stopped) {
|
|
353
|
+
const summary = this.metrics.take(at);
|
|
354
|
+
if (summary)
|
|
355
|
+
await Promise.resolve(this.options.onSummary?.(summary)).catch(() => { });
|
|
356
|
+
}
|
|
357
|
+
if (this.holder && active && !this.stopped)
|
|
358
|
+
await this.share(at);
|
|
359
|
+
this.schedule(this.nextDelay(at));
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
/**
|
|
363
|
+
* When to run next: the earliest of what is due, but never sooner than `MIN_TICK_MS`, so a due time that
|
|
364
|
+
* cannot be acted on yet (a failed renewal waiting out its retry, a long `retry-after`) never spins the source.
|
|
365
|
+
*/
|
|
366
|
+
nextDelay(now) {
|
|
367
|
+
let next = now + HEARTBEAT_MS;
|
|
368
|
+
if (this.mode === 'events' && this.holder && this.watched.size > 0) {
|
|
369
|
+
// A subscription can go out no sooner than its retry time and the 10-second gap allow.
|
|
370
|
+
const sendable = Math.max(this.subscribeRetryAt, (this.lastSubscribeAt ?? 0) + MIN_SUBSCRIBE_GAP_MS);
|
|
371
|
+
if (this.anyCoveredAnswer())
|
|
372
|
+
next = Math.min(next, this.nextPullAt);
|
|
373
|
+
if (this.subscribeRequested || this.watchedKeys() !== this.subscribedKeys)
|
|
374
|
+
next = Math.min(next, sendable);
|
|
375
|
+
if (this.renewAt !== undefined)
|
|
376
|
+
next = Math.min(next, Math.max(this.renewAt, sendable));
|
|
377
|
+
if (this.coverageValidUntil !== undefined && this.coverageValidUntil > now)
|
|
378
|
+
next = Math.min(next, this.coverageValidUntil);
|
|
379
|
+
}
|
|
380
|
+
if (this.mode === 'disabled')
|
|
381
|
+
next = Math.min(next, this.nextProbeAt);
|
|
382
|
+
return Math.max(MIN_TICK_MS, next - now);
|
|
383
|
+
}
|
|
384
|
+
watchedKeys() {
|
|
385
|
+
return [...new Set([...this.watched.values()].map(entry => entry.key))].sort().join('\n');
|
|
386
|
+
}
|
|
387
|
+
anyCoveredAnswer() {
|
|
388
|
+
return [...this.watched.values()].some(entry => { const answer = this.coverage.get(entry.key); return answer !== undefined && (answer.covered || answer.reason === 'permissions-pending'); });
|
|
389
|
+
}
|
|
390
|
+
/** A PR sent in the last subscription that it did not cover (a PR over the 50 is not counted: sending again changes nothing). */
|
|
391
|
+
anyUncovered() {
|
|
392
|
+
return [...this.watched.values()].some(entry => {
|
|
393
|
+
if (!this.subscribedSent.has(entry.key))
|
|
394
|
+
return false;
|
|
395
|
+
const answer = this.coverage.get(entry.key);
|
|
396
|
+
return answer === undefined || !(answer.covered || answer.reason === 'permissions-pending');
|
|
397
|
+
});
|
|
398
|
+
}
|
|
399
|
+
/** A head SHA the watcher has learned for a PR the last subscription sent without one. */
|
|
400
|
+
headLearned() {
|
|
401
|
+
for (const entry of this.watched.values()) {
|
|
402
|
+
if (this.subscribedSent.has(entry.key) && !this.subscribedHeads.has(entry.key) && this.options.watcher.headSha?.(entry.sessionId) !== undefined)
|
|
403
|
+
return true;
|
|
404
|
+
}
|
|
405
|
+
return false;
|
|
406
|
+
}
|
|
407
|
+
subscribeDue(now) {
|
|
408
|
+
if (now < this.subscribeRetryAt)
|
|
409
|
+
return false;
|
|
410
|
+
if (this.lastSubscribeAt !== undefined && now - this.lastSubscribeAt < MIN_SUBSCRIBE_GAP_MS)
|
|
411
|
+
return false;
|
|
412
|
+
if (this.subscribeRequested || this.subscribedKeys === undefined || this.watchedKeys() !== this.subscribedKeys)
|
|
413
|
+
return true;
|
|
414
|
+
if (this.renewAt !== undefined && now >= this.renewAt)
|
|
415
|
+
return true;
|
|
416
|
+
if (this.headLearned())
|
|
417
|
+
return true;
|
|
418
|
+
return this.connected === true && this.anyUncovered() && this.lastSubscribeAt !== undefined && now - this.lastSubscribeAt >= UNCOVERED_RESEND_MS;
|
|
419
|
+
}
|
|
420
|
+
async maybeSubscribe(now) {
|
|
421
|
+
if (!this.subscribeDue(now))
|
|
422
|
+
return;
|
|
423
|
+
// The 50 most recently active PRs are sent; the rest answer `limit` and stay on 60-second polling.
|
|
424
|
+
const byKey = new Map();
|
|
425
|
+
for (const entry of [...this.watched.values()].sort((a, b) => Date.parse(b.pullRequest.updatedAt) - Date.parse(a.pullRequest.updatedAt))) {
|
|
426
|
+
if (!byKey.has(entry.key))
|
|
427
|
+
byKey.set(entry.key, entry);
|
|
428
|
+
}
|
|
429
|
+
const sent = [...byKey.values()].slice(0, MAX_SUBSCRIBED);
|
|
430
|
+
const heads = new Set();
|
|
431
|
+
const request = sent.map(entry => {
|
|
432
|
+
const headSha = this.options.watcher.headSha?.(entry.sessionId);
|
|
433
|
+
if (headSha !== undefined)
|
|
434
|
+
heads.add(entry.key);
|
|
435
|
+
return { owner: entry.pullRequest.owner, repo: entry.pullRequest.repo, number: entry.pullRequest.number, ...(headSha === undefined ? {} : { headSha }) };
|
|
436
|
+
});
|
|
437
|
+
const keys = this.watchedKeys();
|
|
438
|
+
this.lastSubscribeAt = now;
|
|
439
|
+
this.subscribeRequested = false;
|
|
440
|
+
let answer;
|
|
441
|
+
try {
|
|
442
|
+
answer = await this.options.client.subscribe(this.engine, request, this.abort.signal);
|
|
443
|
+
}
|
|
444
|
+
catch (error) {
|
|
445
|
+
if (this.stopped || this.failedCall(error, now))
|
|
446
|
+
return;
|
|
447
|
+
this.subscribeFailed = true;
|
|
448
|
+
this.subscribeRetryAt = now + (error instanceof GitHubAppError && error.code === 'rate-limited' ? error.retryAfterMs ?? SUBSCRIBE_RETRY_MS : SUBSCRIBE_RETRY_MS);
|
|
449
|
+
this.subscribeRequested = true;
|
|
450
|
+
this.options.log?.('warn', `GitHub App subscriptions were not sent: ${error instanceof Error ? error.message : String(error)}`);
|
|
451
|
+
return;
|
|
452
|
+
}
|
|
453
|
+
if (!answer.enabled) {
|
|
454
|
+
this.setDisabled(now);
|
|
455
|
+
return;
|
|
456
|
+
}
|
|
457
|
+
this.subscribeFailed = false;
|
|
458
|
+
this.coverage.clear();
|
|
459
|
+
this.limited.clear();
|
|
460
|
+
for (const entry of answer.coverage)
|
|
461
|
+
this.coverage.set(keyOf(entry.owner, entry.repo, entry.number), entry);
|
|
462
|
+
for (const key of byKey.keys())
|
|
463
|
+
if (!sent.some(entry => entry.key === key))
|
|
464
|
+
this.limited.add(key);
|
|
465
|
+
this.subscribedKeys = keys;
|
|
466
|
+
this.subscribedSent = new Set(sent.map(entry => entry.key));
|
|
467
|
+
this.subscribedHeads = heads;
|
|
468
|
+
const renewBy = answer.renewBy === undefined ? undefined : Date.parse(answer.renewBy);
|
|
469
|
+
const vouched = renewBy !== undefined && renewBy > now;
|
|
470
|
+
if (sent.length === 0) {
|
|
471
|
+
// Nothing subscribed: nothing to renew, and no coverage to expire (an idle engine never reads `unavailable`).
|
|
472
|
+
this.renewAt = undefined;
|
|
473
|
+
this.coverageValidUntil = undefined;
|
|
474
|
+
}
|
|
475
|
+
else {
|
|
476
|
+
// Renew a little before the service's `renewBy` (at most 15 minutes, at most half the time left), so one
|
|
477
|
+
// failed attempt can be retried without renewing in a loop when `renewBy` is near; past `renewBy` (and
|
|
478
|
+
// never later than 36 hours on), with no successful renewal, the coverage is no longer trusted.
|
|
479
|
+
const lead = vouched ? Math.min(RENEW_LEAD_MS, (renewBy - now) / 2) : 0;
|
|
480
|
+
this.renewAt = Math.min(now + RENEW_INTERVAL_MS, vouched ? renewBy - lead : Infinity);
|
|
481
|
+
this.coverageValidUntil = Math.min(now + COVERAGE_TTL_MS, vouched ? renewBy : Infinity);
|
|
482
|
+
}
|
|
483
|
+
// An unlinked caller gets `not-connected` for every PR; any other answer means GitHub is connected.
|
|
484
|
+
if (answer.coverage.length > 0)
|
|
485
|
+
this.connected = !answer.coverage.every(entry => entry.reason === 'not-connected');
|
|
486
|
+
if (this.connected === false)
|
|
487
|
+
this.nextProbeAt = Math.max(this.nextProbeAt, now + PROBE_INTERVAL_MS);
|
|
488
|
+
if (this.anyCoveredAnswer() && this.nextPullAt > now + PULL_INTERVAL_MS)
|
|
489
|
+
this.nextPullAt = now;
|
|
490
|
+
}
|
|
491
|
+
/**
|
|
492
|
+
* A failure that changes the mode rather than being retried: signed out, not allowed, or an account API
|
|
493
|
+
* without the GitHub App routes (`404`, as before they are deployed), which is treated like the service
|
|
494
|
+
* saying it is off: one `GET /v1/github` every 10 minutes, not a call a minute. Returns true when it was one
|
|
495
|
+
* of those.
|
|
496
|
+
*/
|
|
497
|
+
failedCall(error, now) {
|
|
498
|
+
if (!(error instanceof GitHubAppError))
|
|
499
|
+
return false;
|
|
500
|
+
if (error.code === 'signed-out') {
|
|
501
|
+
this.setOff('signed-out');
|
|
502
|
+
return true;
|
|
503
|
+
}
|
|
504
|
+
if (error.code === 'not-allowed' || error.code === 'not-found') {
|
|
505
|
+
this.setDisabled(now);
|
|
506
|
+
return true;
|
|
507
|
+
}
|
|
508
|
+
return false;
|
|
509
|
+
}
|
|
510
|
+
/** While the service says it is off, `GET /v1/github` every 10 minutes is how the engine learns it is back. */
|
|
511
|
+
async probeEnabled(now) {
|
|
512
|
+
if (now < this.nextProbeAt)
|
|
513
|
+
return;
|
|
514
|
+
this.nextProbeAt = now + PROBE_INTERVAL_MS;
|
|
515
|
+
try {
|
|
516
|
+
const status = await this.options.client.status(this.abort.signal);
|
|
517
|
+
this.connected = status.connection !== null;
|
|
518
|
+
if (status.enabled && status.allowed) {
|
|
519
|
+
this.options.log?.('info', 'The GitHub App is on again on the Andorra service.');
|
|
520
|
+
this.mode = 'events';
|
|
521
|
+
this.subscribeRequested = true;
|
|
522
|
+
this.nextPullAt = now;
|
|
523
|
+
}
|
|
524
|
+
}
|
|
525
|
+
catch (error) {
|
|
526
|
+
if (!this.stopped)
|
|
527
|
+
this.failedCall(error, now);
|
|
528
|
+
}
|
|
529
|
+
}
|
|
530
|
+
/** Nothing covered and GitHub not connected: `GET /v1/github` every 10 minutes notices a connection made elsewhere. */
|
|
531
|
+
async maybeProbeConnection(now) {
|
|
532
|
+
if (this.connected !== false || this.anyCoveredAnswer() || now < this.nextProbeAt)
|
|
533
|
+
return;
|
|
534
|
+
this.nextProbeAt = now + PROBE_INTERVAL_MS;
|
|
535
|
+
try {
|
|
536
|
+
const status = await this.options.client.status(this.abort.signal);
|
|
537
|
+
if (!status.enabled) {
|
|
538
|
+
this.setDisabled(now);
|
|
539
|
+
return;
|
|
540
|
+
}
|
|
541
|
+
if (status.connection !== null && !this.stopped) {
|
|
542
|
+
this.connected = true;
|
|
543
|
+
this.subscribeRequested = true;
|
|
544
|
+
await this.maybeSubscribe(now);
|
|
545
|
+
}
|
|
546
|
+
}
|
|
547
|
+
catch (error) {
|
|
548
|
+
if (!this.stopped)
|
|
549
|
+
this.failedCall(error, now);
|
|
550
|
+
}
|
|
551
|
+
}
|
|
552
|
+
backoff() {
|
|
553
|
+
return Math.min(BREAKER_MAX_BACKOFF_MS, BREAKER_MIN_BACKOFF_MS * 2 ** Math.max(0, this.failures - BREAKER_THRESHOLD));
|
|
554
|
+
}
|
|
555
|
+
async maybePull(now) {
|
|
556
|
+
if (!this.anyCoveredAnswer() || now < this.nextPullAt)
|
|
557
|
+
return;
|
|
558
|
+
for (let page = 0; page < MAX_PAGES; page++) {
|
|
559
|
+
if (this.stopped)
|
|
560
|
+
return;
|
|
561
|
+
let answer;
|
|
562
|
+
try {
|
|
563
|
+
answer = await this.options.client.events(this.engine, this.cursor, this.abort.signal);
|
|
564
|
+
}
|
|
565
|
+
catch (error) {
|
|
566
|
+
if (this.stopped || this.failedCall(error, now))
|
|
567
|
+
return;
|
|
568
|
+
if (error instanceof GitHubAppError && error.code === 'rate-limited') {
|
|
569
|
+
this.nextPullAt = now + (error.retryAfterMs ?? PULL_INTERVAL_MS);
|
|
570
|
+
return;
|
|
571
|
+
}
|
|
572
|
+
this.failures += 1;
|
|
573
|
+
if (this.failures >= BREAKER_THRESHOLD && !this.breakerOpen) {
|
|
574
|
+
this.breakerOpen = true;
|
|
575
|
+
this.options.log?.('warn', `GitHub App events are unavailable (${error instanceof Error ? error.message : String(error)}); pull requests are polled through gh until the service answers again.`);
|
|
576
|
+
}
|
|
577
|
+
this.nextPullAt = now + (this.breakerOpen ? this.backoff() : PULL_INTERVAL_MS);
|
|
578
|
+
return;
|
|
579
|
+
}
|
|
580
|
+
if (this.stopped)
|
|
581
|
+
return;
|
|
582
|
+
if (!answer.enabled) {
|
|
583
|
+
this.setDisabled(now);
|
|
584
|
+
return;
|
|
585
|
+
}
|
|
586
|
+
const recovered = this.breakerOpen;
|
|
587
|
+
this.breakerOpen = false;
|
|
588
|
+
this.failures = 0;
|
|
589
|
+
if (recovered)
|
|
590
|
+
this.options.log?.('info', 'GitHub App events are back.');
|
|
591
|
+
this.react(answer.events, answer.reset);
|
|
592
|
+
if (answer.reset || recovered)
|
|
593
|
+
this.options.watcher.fullPass();
|
|
594
|
+
if (answer.cursor !== undefined && answer.cursor !== this.cursor) {
|
|
595
|
+
this.cursor = answer.cursor;
|
|
596
|
+
await writeCursor(this.options.home, this.cursor, now).catch(error => this.options.log?.('warn', `Could not save the GitHub App cursor: ${error instanceof Error ? error.message : String(error)}`));
|
|
597
|
+
}
|
|
598
|
+
if (!answer.more)
|
|
599
|
+
break;
|
|
600
|
+
}
|
|
601
|
+
this.nextPullAt = now + PULL_INTERVAL_MS;
|
|
602
|
+
}
|
|
603
|
+
remember(id) {
|
|
604
|
+
if (this.seenIds.has(id))
|
|
605
|
+
return false;
|
|
606
|
+
this.seenIds.add(id);
|
|
607
|
+
this.seenOrder.push(id);
|
|
608
|
+
while (this.seenOrder.length > SEEN_IDS)
|
|
609
|
+
this.seenIds.delete(this.seenOrder.shift());
|
|
610
|
+
return true;
|
|
611
|
+
}
|
|
612
|
+
sessionsFor(notice) {
|
|
613
|
+
return [...this.watched.values()].filter(entry => {
|
|
614
|
+
if (entry.pullRequest.number !== notice.number)
|
|
615
|
+
return false;
|
|
616
|
+
const answer = this.coverage.get(entry.key);
|
|
617
|
+
if (notice.repoId !== undefined && answer?.repoId !== undefined)
|
|
618
|
+
return answer.repoId === notice.repoId;
|
|
619
|
+
return notice.owner !== undefined && notice.repo !== undefined && entry.key === keyOf(notice.owner, notice.repo, notice.number);
|
|
620
|
+
});
|
|
621
|
+
}
|
|
622
|
+
/** Each new notice becomes a re-read through gh (or only a stale mark); a duplicate becomes nothing. */
|
|
623
|
+
react(notices, reset) {
|
|
624
|
+
for (const notice of notices) {
|
|
625
|
+
if (!this.remember(notice.id))
|
|
626
|
+
continue;
|
|
627
|
+
if (notice.kind === 'coverage') {
|
|
628
|
+
this.subscribeRequested = true;
|
|
629
|
+
continue;
|
|
630
|
+
}
|
|
631
|
+
// A mention is acted on even after a gap: the full pass reads PRs, and wakes nobody for a comment.
|
|
632
|
+
if (notice.kind === 'request') {
|
|
633
|
+
if (isMentionNotice(notice))
|
|
634
|
+
this.request(notice);
|
|
635
|
+
continue;
|
|
636
|
+
}
|
|
637
|
+
// After a gap the forced full pass reads every PR, so the notices themselves are not needed.
|
|
638
|
+
if (reset)
|
|
639
|
+
continue;
|
|
640
|
+
for (const entry of this.sessionsFor(notice)) {
|
|
641
|
+
this.options.onStale?.(entry.sessionId);
|
|
642
|
+
if (notice.kind === 'state' || notice.kind === 'head' || notice.kind === 'checks')
|
|
643
|
+
void this.options.watcher.checkNow(entry.sessionId);
|
|
644
|
+
else if ((notice.kind === 'review' || notice.kind === 'comment') && entry.pullRequest.autoFix && this.isReviewer(notice.actor)) {
|
|
645
|
+
const sessionId = entry.sessionId;
|
|
646
|
+
const at = notice.at === undefined ? this.now() : Date.parse(notice.at);
|
|
647
|
+
const known = this.wakeNotices.get(sessionId);
|
|
648
|
+
if (known === undefined || at < known)
|
|
649
|
+
this.wakeNotices.set(sessionId, at);
|
|
650
|
+
const noted = this.wakeNotices.get(sessionId);
|
|
651
|
+
// The wake this notice is timed for is the one its own read arms: a read that arms none forgets the
|
|
652
|
+
// notice, so a later wake with another cause is never counted as its latency.
|
|
653
|
+
void Promise.resolve(this.options.watcher.checkNow(sessionId)).then(() => {
|
|
654
|
+
if (this.wakeNotices.get(sessionId) === noted)
|
|
655
|
+
this.wakeNotices.delete(sessionId);
|
|
656
|
+
}, () => { });
|
|
657
|
+
}
|
|
658
|
+
}
|
|
659
|
+
}
|
|
660
|
+
}
|
|
661
|
+
/** A mention: every session tracking the PR shows the new comment; one task, the one that attached the PR last, is asked. */
|
|
662
|
+
request(notice) {
|
|
663
|
+
const entries = this.sessionsFor(notice);
|
|
664
|
+
for (const entry of entries)
|
|
665
|
+
this.options.onStale?.(entry.sessionId);
|
|
666
|
+
const target = [...entries].sort((a, b) => Date.parse(b.pullRequest.attachedAt) - Date.parse(a.pullRequest.attachedAt) || (a.sessionId < b.sessionId ? -1 : 1))[0];
|
|
667
|
+
if (target !== undefined)
|
|
668
|
+
this.options.onRequest?.(target.sessionId, notice);
|
|
669
|
+
}
|
|
670
|
+
/** The holder's view for `github.status` in other processes: written when it changes, and at least every minute. */
|
|
671
|
+
async share(now) {
|
|
672
|
+
const state = {
|
|
673
|
+
mode: this.eventMode, ...(this.modeReason === undefined ? {} : { reason: this.modeReason }),
|
|
674
|
+
...(this.connected === undefined ? {} : { connected: this.connected }),
|
|
675
|
+
pullRequests: Object.fromEntries([...this.watched.values()].map(entry => [entry.sessionId, { url: entry.pullRequest.url, coverage: this.coverageOf(entry.sessionId) }])),
|
|
676
|
+
};
|
|
677
|
+
const text = JSON.stringify(state);
|
|
678
|
+
if (text === this.sharedWritten && now - this.sharedWrittenAt < SHARED_REWRITE_MS)
|
|
679
|
+
return;
|
|
680
|
+
this.sharedWritten = text;
|
|
681
|
+
this.sharedWrittenAt = now;
|
|
682
|
+
await writeSharedCoverage(this.options.home, { updatedAt: new Date(now).toISOString(), ...state }).catch(() => { this.sharedWritten = undefined; });
|
|
683
|
+
}
|
|
684
|
+
}
|