@ouro.bot/cli 0.1.0-alpha.1114 → 0.1.0-alpha.1116
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/changelog.json +12 -0
- package/deploy/unraid/sanctuary.ouro/bundle-meta.json +1 -1
- package/deploy/unraid/sanctuary.xml +1 -1
- package/dist/heart/daemon/sense-manager.js +11 -7
- package/dist/heart/identity.js +29 -32
- package/dist/nerves/coverage/file-completeness.js +3 -3
- package/dist/senses/app-sessions/sense.js +32 -18
- package/dist/senses/app-sessions/{issue-drain-state.js → wake-runner-state.js} +14 -19
- package/dist/senses/app-sessions/wake-runner.js +155 -0
- package/npm-shrinkwrap.json +2 -2
- package/package.json +1 -1
- package/dist/senses/app-sessions/issue-drains.js +0 -174
package/changelog.json
CHANGED
|
@@ -1,6 +1,18 @@
|
|
|
1
1
|
{
|
|
2
2
|
"_note": "This changelog is maintained as part of the PR/version-bump workflow. Agent-curated, not auto-generated. Agents read this file directly via read_file to understand what changed between versions.",
|
|
3
3
|
"versions": [
|
|
4
|
+
{
|
|
5
|
+
"version": "0.1.0-alpha.1116",
|
|
6
|
+
"changes": [
|
|
7
|
+
"The issue-drain poller moved to Desk, and the app-sessions sense now only runs commands and delivers their wakes. Add a `wakeRunners` entry to `senses.app-sessions` in agent.json (`name`, `command` as an argv array, optional `everySeconds` of 10 or more, default 300, and optional `session`): the sense runs the command on that cadence, sends each wake it prints to its session, and runs the wake's `done` command to report delivery. `ouro status` lists each runner and the command's own status lines, and the daemon still starts without loading the app client. `issueDrains` and `issuePollMinutes` are retired: a config that still has them shows a notice and is otherwise ignored, so move the drains to Desk's drain config (same fields, same state shape, so seen markers carry over) before updating. See docs/app-sessions-sense.md."
|
|
8
|
+
]
|
|
9
|
+
},
|
|
10
|
+
{
|
|
11
|
+
"version": "0.1.0-alpha.1115",
|
|
12
|
+
"changes": [
|
|
13
|
+
"An issue drain can list `ignoredLabels` (for example `agent:drain`): adding or removing one of those labels no longer wakes the drain. Labels starting with `drain:` stay ignored as before, and any other label still wakes it."
|
|
14
|
+
]
|
|
15
|
+
},
|
|
4
16
|
{
|
|
5
17
|
"version": "0.1.0-alpha.1114",
|
|
6
18
|
"changes": [
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
<?xml version="1.0"?>
|
|
2
2
|
<Container version="2">
|
|
3
3
|
<Name>ouro-butler</Name>
|
|
4
|
-
<Repository>ghcr.io/ourostack/ouroboros-butler:0.1.0-alpha.
|
|
4
|
+
<Repository>ghcr.io/ourostack/ouroboros-butler:0.1.0-alpha.1116</Repository>
|
|
5
5
|
<Registry>https://github.com/ourostack/ouroboros/pkgs/container/ouroboros-butler</Registry>
|
|
6
6
|
<Network>host</Network>
|
|
7
7
|
<Shell>sh</Shell>
|
|
@@ -45,7 +45,7 @@ const provider_credentials_1 = require("../provider-credentials");
|
|
|
45
45
|
const sense_truth_1 = require("../sense-truth");
|
|
46
46
|
const machine_identity_1 = require("../machine-identity");
|
|
47
47
|
const shepherd_config_1 = require("../shepherd-config");
|
|
48
|
-
const
|
|
48
|
+
const wake_runner_state_1 = require("../../senses/app-sessions/wake-runner-state");
|
|
49
49
|
const store_1 = require("../../senses/app-sessions/store");
|
|
50
50
|
const waiting_status_1 = require("../../senses/app-sessions/waiting-status");
|
|
51
51
|
const process_manager_1 = require("./process-manager");
|
|
@@ -111,10 +111,10 @@ function readAgentSenses(agentJsonPath) {
|
|
|
111
111
|
if (sense === "bluebubbles" && typeof hostUsername === "string" && hostUsername.trim().length > 0) {
|
|
112
112
|
defaults[sense] = { enabled, hostUsername: hostUsername.trim() };
|
|
113
113
|
}
|
|
114
|
-
//
|
|
114
|
+
// Wake runners the config rejects stay unset here; the sense itself reports the error when it runs them.
|
|
115
115
|
if (sense === "app-sessions") {
|
|
116
116
|
try {
|
|
117
|
-
defaults[sense] = { enabled, ...(0, identity_1.
|
|
117
|
+
defaults[sense] = { enabled, ...(0, identity_1.normalizeWakeRunners)(rawSense, agentJsonPath) };
|
|
118
118
|
}
|
|
119
119
|
catch {
|
|
120
120
|
defaults[sense] = { enabled };
|
|
@@ -127,9 +127,13 @@ function readAgentSenses(agentJsonPath) {
|
|
|
127
127
|
}
|
|
128
128
|
return { senses: defaults, workbenchHasStaleBundleEntry };
|
|
129
129
|
}
|
|
130
|
-
/** The app-sessions row's `ouro status` lines: sessions waiting on the owner, then each
|
|
131
|
-
function appSessionsLines(agentRoot,
|
|
132
|
-
const lines = [
|
|
130
|
+
/** The app-sessions row's `ouro status` lines: sessions waiting on the owner, then each wake runner. */
|
|
131
|
+
function appSessionsLines(agentRoot, config, now) {
|
|
132
|
+
const lines = [
|
|
133
|
+
...(0, waiting_status_1.waitingStatusLines)((0, store_1.readSnapshot)(agentRoot), now),
|
|
134
|
+
...(config.retiredIssueDrains ? ["issueDrains in agent.json is retired and no longer polled: Desk polls drains now, so move them to Desk's drain config and add a wakeRunners entry (docs/app-sessions-sense.md)"] : []),
|
|
135
|
+
...(config.wakeRunners?.length ? (0, wake_runner_state_1.wakeRunnerStatusLines)(config.wakeRunners, (0, wake_runner_state_1.readWakeRunnerStates)(agentRoot), now) : []),
|
|
136
|
+
];
|
|
133
137
|
return lines.length ? { lines } : {};
|
|
134
138
|
}
|
|
135
139
|
function textField(record, key) {
|
|
@@ -1105,7 +1109,7 @@ class DaemonSenseManager {
|
|
|
1105
1109
|
: entry.sense === "workbench"
|
|
1106
1110
|
? WORKBENCH_RUNTIME_INJECTION_DETAIL
|
|
1107
1111
|
: "not enabled in agent.json",
|
|
1108
|
-
...(entry.enabled && entry.sense === "app-sessions" ? appSessionsLines(path.join(this.bundlesRoot, `${agent}.ouro`), context.senses["app-sessions"]
|
|
1112
|
+
...(entry.enabled && entry.sense === "app-sessions" ? appSessionsLines(path.join(this.bundlesRoot, `${agent}.ouro`), context.senses["app-sessions"], Date.now()) : {}),
|
|
1109
1113
|
...(entry.sense === "bluebubbles" && blueBubblesConfigured ? {
|
|
1110
1114
|
proofMethod: blueBubblesRuntimeFacts.proofMethod,
|
|
1111
1115
|
lastProofAt: blueBubblesRuntimeFacts.lastProofAt,
|
package/dist/heart/identity.js
CHANGED
|
@@ -40,7 +40,7 @@ exports.defaultStableVaultEmail = defaultStableVaultEmail;
|
|
|
40
40
|
exports.resolveVaultConfig = resolveVaultConfig;
|
|
41
41
|
exports.persistedSenses = persistedSenses;
|
|
42
42
|
exports.hasStaleWorkbenchBundleEntry = hasStaleWorkbenchBundleEntry;
|
|
43
|
-
exports.
|
|
43
|
+
exports.normalizeWakeRunners = normalizeWakeRunners;
|
|
44
44
|
exports.normalizeSenses = normalizeSenses;
|
|
45
45
|
exports.buildDefaultAgentTemplate = buildDefaultAgentTemplate;
|
|
46
46
|
exports.getAgentName = getAgentName;
|
|
@@ -175,45 +175,42 @@ function hasStaleWorkbenchBundleEntry(rawSenses, rawMcpServers) {
|
|
|
175
175
|
const rawWorkbench = rawSenses.workbench;
|
|
176
176
|
return !(isPlainRecord(rawWorkbench) && rawWorkbench.enabled === false);
|
|
177
177
|
}
|
|
178
|
-
function
|
|
178
|
+
function normalizeWakeRunners(rawSense, configFile) {
|
|
179
179
|
function fail(what) {
|
|
180
|
-
(0, runtime_1.emitNervesEvent)({ level: "error", event: "config_identity.error", component: "config/identity", message: "agent config has invalid
|
|
180
|
+
(0, runtime_1.emitNervesEvent)({ level: "error", event: "config_identity.error", component: "config/identity", message: "agent config has invalid wake runners", meta: { path: configFile } });
|
|
181
181
|
throw new Error(`agent.json at ${configFile}: senses.app-sessions.${what}.`);
|
|
182
182
|
}
|
|
183
|
-
const {
|
|
183
|
+
const { wakeRunners, issueDrains } = rawSense;
|
|
184
184
|
const result = {};
|
|
185
|
-
if (issueDrains !== undefined)
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
185
|
+
if (issueDrains !== undefined)
|
|
186
|
+
result.retiredIssueDrains = true;
|
|
187
|
+
if (wakeRunners !== undefined) {
|
|
188
|
+
if (!Array.isArray(wakeRunners))
|
|
189
|
+
fail("wakeRunners must be an array");
|
|
190
|
+
const names = new Set();
|
|
191
|
+
result.wakeRunners = wakeRunners.map((entry, index) => {
|
|
190
192
|
const item = (entry && typeof entry === "object" ? entry : {});
|
|
191
|
-
const {
|
|
192
|
-
if (typeof
|
|
193
|
-
fail(`
|
|
194
|
-
if (typeof
|
|
195
|
-
fail(`
|
|
196
|
-
if (
|
|
197
|
-
fail(`
|
|
198
|
-
if (
|
|
199
|
-
fail(`
|
|
200
|
-
|
|
201
|
-
if (
|
|
202
|
-
fail(`
|
|
203
|
-
|
|
193
|
+
const { name, command, everySeconds, session } = item;
|
|
194
|
+
if (typeof name !== "string" || !name.trim())
|
|
195
|
+
fail(`wakeRunners[${index}].name must be a non-empty name`);
|
|
196
|
+
if (!Array.isArray(command) || command.length === 0 || command.some(part => typeof part !== "string" || !part))
|
|
197
|
+
fail(`wakeRunners[${index}].command must be a non-empty array of strings`);
|
|
198
|
+
if (everySeconds !== undefined && (typeof everySeconds !== "number" || !Number.isInteger(everySeconds) || everySeconds < 10))
|
|
199
|
+
fail(`wakeRunners[${index}].everySeconds must be a whole number of seconds, 10 or more`);
|
|
200
|
+
if (session !== undefined && (typeof session !== "string" || !session.trim()))
|
|
201
|
+
fail(`wakeRunners[${index}].session must be a Copilot app session id`);
|
|
202
|
+
const key = name.trim();
|
|
203
|
+
if (names.has(key))
|
|
204
|
+
fail(`wakeRunners[${index}].name ${key} is listed twice`);
|
|
205
|
+
names.add(key);
|
|
204
206
|
return {
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
...(
|
|
208
|
-
...(
|
|
207
|
+
name: key,
|
|
208
|
+
command: command,
|
|
209
|
+
...(everySeconds !== undefined ? { everySeconds: everySeconds } : {}),
|
|
210
|
+
...(session !== undefined ? { session: session.trim() } : {}),
|
|
209
211
|
};
|
|
210
212
|
});
|
|
211
213
|
}
|
|
212
|
-
if (issuePollMinutes !== undefined) {
|
|
213
|
-
if (typeof issuePollMinutes !== "number" || !Number.isFinite(issuePollMinutes) || issuePollMinutes < 1)
|
|
214
|
-
fail("issuePollMinutes must be a number of at least 1");
|
|
215
|
-
result.issuePollMinutes = issuePollMinutes;
|
|
216
|
-
}
|
|
217
214
|
return result;
|
|
218
215
|
}
|
|
219
216
|
function normalizeSenses(value, configFile) {
|
|
@@ -307,7 +304,7 @@ function normalizeSenses(value, configFile) {
|
|
|
307
304
|
continue;
|
|
308
305
|
}
|
|
309
306
|
if (senseName === "app-sessions") {
|
|
310
|
-
defaults[senseName] = { enabled, ...
|
|
307
|
+
defaults[senseName] = { enabled, ...normalizeWakeRunners(rawSense, configFile) };
|
|
311
308
|
continue;
|
|
312
309
|
}
|
|
313
310
|
defaults[senseName] = { enabled };
|
|
@@ -103,9 +103,9 @@ const DISPATCH_EXEMPT_PATTERNS = [
|
|
|
103
103
|
// consumed by server readers and the UI. Mailbox read/render modules own
|
|
104
104
|
// the observability for these projections.
|
|
105
105
|
"heart/mailbox/mailbox-types",
|
|
106
|
-
//
|
|
107
|
-
// (
|
|
108
|
-
"senses/app-sessions/
|
|
106
|
+
// Wake-runner status reader: pure state-file read and line rendering for `ouro status`. The runner
|
|
107
|
+
// (wake-runner.ts) owns observability via senses.wake_runner_run and friends.
|
|
108
|
+
"senses/app-sessions/wake-runner-state",
|
|
109
109
|
// Waiting-prompt status reader: pure line rendering for `ouro status`. The notifier
|
|
110
110
|
// (waiting-prompts.ts) owns observability via senses.app_sessions_waiting_notice and friends.
|
|
111
111
|
"senses/app-sessions/waiting-status",
|
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.APP_SESSIONS_WAKE_TRIGGER = exports.WATCH_POLL_MS = exports.SNAPSHOT_MS = void 0;
|
|
3
|
+
exports.WAKE_RUNNER_IDLE_MS = exports.APP_SESSIONS_WAKE_TRIGGER = exports.WATCH_POLL_MS = exports.SNAPSHOT_MS = void 0;
|
|
4
4
|
exports.newReplies = newReplies;
|
|
5
5
|
exports.replyContent = replyContent;
|
|
6
6
|
exports.privateRuntimeDeliver = privateRuntimeDeliver;
|
|
7
7
|
exports.checkWatches = checkWatches;
|
|
8
8
|
exports.recordSnapshot = recordSnapshot;
|
|
9
|
-
exports.
|
|
9
|
+
exports.checkWakeRunners = checkWakeRunners;
|
|
10
10
|
exports.startAppSessionsSense = startAppSessionsSense;
|
|
11
11
|
const node_crypto_1 = require("node:crypto");
|
|
12
12
|
const socket_client_1 = require("../../heart/daemon/socket-client");
|
|
@@ -14,10 +14,10 @@ const identity_1 = require("../../heart/identity");
|
|
|
14
14
|
const pending_1 = require("../../mind/pending");
|
|
15
15
|
const runtime_1 = require("../../nerves/runtime");
|
|
16
16
|
const cmux_1 = require("../shepherd/cmux");
|
|
17
|
-
const issue_drains_1 = require("./issue-drains");
|
|
18
17
|
const client_1 = require("./client");
|
|
19
18
|
const store_1 = require("./store");
|
|
20
19
|
const waiting_prompts_1 = require("./waiting-prompts");
|
|
20
|
+
const wake_runner_1 = require("./wake-runner");
|
|
21
21
|
/**
|
|
22
22
|
* The app-sessions perception loop. Every minute it snapshots the app's sessions (counts and stuck ids) into the
|
|
23
23
|
* agent's state for its prompt and health. Every ten seconds it reads the new part of each watched session's log
|
|
@@ -137,17 +137,30 @@ function recordSnapshot(agentRoot, client, now) {
|
|
|
137
137
|
(0, runtime_1.emitNervesEvent)({ component: "senses", event: "senses.app_sessions_snapshot", message: "snapshotted the Copilot app's sessions", meta: { ...result.counts, appRunning: result.appRunning } });
|
|
138
138
|
return result;
|
|
139
139
|
}
|
|
140
|
-
/**
|
|
141
|
-
|
|
140
|
+
/** How long to wait before looking again when no wake runner is configured, so one added to `agent.json` starts within five minutes. */
|
|
141
|
+
exports.WAKE_RUNNER_IDLE_MS = 5 * 60_000;
|
|
142
|
+
/**
|
|
143
|
+
* Run the wake runners in `agent.json` that are due and return the milliseconds until the next one is. `due` remembers each
|
|
144
|
+
* runner's next run between calls. A bad config is logged and looked at again later.
|
|
145
|
+
*/
|
|
146
|
+
async function checkWakeRunners(agentName, agentRoot, deps, due = new Map()) {
|
|
142
147
|
try {
|
|
143
148
|
const config = (0, identity_1.loadAgentConfig)({ agentName, agentRoot }).senses?.["app-sessions"];
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
149
|
+
const runners = config?.enabled ? config.wakeRunners ?? [] : [];
|
|
150
|
+
const every = (name) => (runners.find(runner => runner.name === name)?.everySeconds ?? wake_runner_1.WAKE_RUNNER_DEFAULT_SECONDS) * 1000;
|
|
151
|
+
const ready = new Set(runners.filter(runner => (due.get(runner.name) ?? 0) <= deps.now).map(runner => runner.name));
|
|
152
|
+
if (runners.length && ready.size)
|
|
153
|
+
await (0, wake_runner_1.runWakeRunners)(agentRoot, runners, deps, ready);
|
|
154
|
+
for (const name of ready)
|
|
155
|
+
due.set(name, deps.now + every(name));
|
|
156
|
+
for (const name of [...due.keys()])
|
|
157
|
+
if (!runners.some(runner => runner.name === name))
|
|
158
|
+
due.delete(name);
|
|
159
|
+
return runners.length ? Math.max(1000, Math.min(...runners.map(runner => due.get(runner.name) - deps.now))) : exports.WAKE_RUNNER_IDLE_MS;
|
|
147
160
|
}
|
|
148
161
|
catch (error) {
|
|
149
|
-
(0, runtime_1.emitNervesEvent)({ level: "warn", component: "senses", event: "senses.
|
|
150
|
-
return
|
|
162
|
+
(0, runtime_1.emitNervesEvent)({ level: "warn", component: "senses", event: "senses.wake_runner_config_error", message: "could not check the wake runners", meta: { error: error.message } });
|
|
163
|
+
return exports.WAKE_RUNNER_IDLE_MS;
|
|
151
164
|
}
|
|
152
165
|
}
|
|
153
166
|
function startAppSessionsSense(options) {
|
|
@@ -160,8 +173,9 @@ function startAppSessionsSense(options) {
|
|
|
160
173
|
let cancelSnapshot;
|
|
161
174
|
let cancelWatch = null;
|
|
162
175
|
let inFlight = Promise.resolve();
|
|
163
|
-
let
|
|
164
|
-
let
|
|
176
|
+
let cancelRunners = null;
|
|
177
|
+
let runnerPoll = Promise.resolve();
|
|
178
|
+
const due = new Map();
|
|
165
179
|
let waitingPoll = Promise.resolve();
|
|
166
180
|
const notifyOwner = options.notifyOwner ?? (0, waiting_prompts_1.ownerNotifier)(options.agentName, agentRoot);
|
|
167
181
|
const snapshotTick = () => {
|
|
@@ -183,26 +197,26 @@ function startAppSessionsSense(options) {
|
|
|
183
197
|
cancelWatch = schedule(watchTick, exports.WATCH_POLL_MS);
|
|
184
198
|
});
|
|
185
199
|
};
|
|
186
|
-
const
|
|
200
|
+
const runnerTick = () => {
|
|
187
201
|
if (stopped)
|
|
188
202
|
return;
|
|
189
|
-
|
|
203
|
+
runnerPoll = checkWakeRunners(options.agentName, agentRoot, { run: options.runCommand ?? wake_runner_1.runCommand, send: options.sendWake ?? (0, wake_runner_1.sendWake)(client), now: now() }, due).then(ms => {
|
|
190
204
|
if (!stopped)
|
|
191
|
-
|
|
205
|
+
cancelRunners = schedule(runnerTick, ms);
|
|
192
206
|
});
|
|
193
207
|
};
|
|
194
208
|
(0, runtime_1.emitNervesEvent)({ component: "senses", event: "senses.app_sessions_start", message: "app-sessions sense started", meta: { agent: options.agentName } });
|
|
195
209
|
snapshotTick();
|
|
196
210
|
watchTick();
|
|
197
|
-
|
|
211
|
+
runnerTick();
|
|
198
212
|
return {
|
|
199
213
|
stop: () => {
|
|
200
214
|
stopped = true;
|
|
201
215
|
cancelSnapshot();
|
|
202
216
|
cancelWatch?.();
|
|
203
|
-
|
|
217
|
+
cancelRunners?.();
|
|
204
218
|
(0, runtime_1.emitNervesEvent)({ component: "senses", event: "senses.app_sessions_end", message: "app-sessions sense stopped", meta: { agent: options.agentName } });
|
|
205
219
|
},
|
|
206
|
-
done: () => Promise.all([inFlight,
|
|
220
|
+
done: () => Promise.all([inFlight, runnerPoll, waitingPoll]).then(() => undefined),
|
|
207
221
|
};
|
|
208
222
|
}
|
|
@@ -33,30 +33,25 @@ var __importStar = (this && this.__importStar) || (function () {
|
|
|
33
33
|
};
|
|
34
34
|
})();
|
|
35
35
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
-
exports.
|
|
37
|
-
exports.
|
|
38
|
-
exports.drainStatusLines = drainStatusLines;
|
|
36
|
+
exports.readWakeRunnerStates = exports.wakeRunnerStateFile = void 0;
|
|
37
|
+
exports.wakeRunnerStatusLines = wakeRunnerStatusLines;
|
|
39
38
|
const path = __importStar(require("node:path"));
|
|
40
39
|
const store_1 = require("./store");
|
|
41
|
-
const
|
|
42
|
-
exports.
|
|
43
|
-
const
|
|
44
|
-
exports.
|
|
45
|
-
function readDrainStates(agentRoot) {
|
|
46
|
-
return (0, store_1.readJson)((0, exports.drainStateFile)(agentRoot), {});
|
|
47
|
-
}
|
|
40
|
+
const wakeRunnerStateFile = (agentRoot) => path.join((0, store_1.appSessionsStateDir)(agentRoot), "wake-runners.json");
|
|
41
|
+
exports.wakeRunnerStateFile = wakeRunnerStateFile;
|
|
42
|
+
const readWakeRunnerStates = (agentRoot) => (0, store_1.readJson)((0, exports.wakeRunnerStateFile)(agentRoot), {});
|
|
43
|
+
exports.readWakeRunnerStates = readWakeRunnerStates;
|
|
48
44
|
function ago(iso, now) {
|
|
49
45
|
const minutes = Math.max(0, Math.round((now - Date.parse(iso)) / 60_000));
|
|
50
46
|
return minutes < 1 ? "just now" : minutes < 90 ? `${minutes}m ago` : `${Math.round(minutes / 60)}h ago`;
|
|
51
47
|
}
|
|
52
|
-
/** One `ouro status` line per
|
|
53
|
-
function
|
|
54
|
-
return
|
|
55
|
-
const state = states[
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
return `${head}: polled ${ago(state.lastPollAt, now)}, ${state.openCount ?? "?"} open, ${state.pending.length} waiting, last wake ${wake}${state.lastError ? `, last error: ${state.lastError}` : ""}`;
|
|
48
|
+
/** One `ouro status` line per runner (last run, wakes delivered, last error), then the lines its command reported. */
|
|
49
|
+
function wakeRunnerStatusLines(runners, states, now) {
|
|
50
|
+
return runners.flatMap(runner => {
|
|
51
|
+
const state = states[runner.name];
|
|
52
|
+
if (!state?.lastRunAt)
|
|
53
|
+
return [`wake runner ${runner.name}: not run yet`];
|
|
54
|
+
const delivered = state.lastWakes === undefined ? "" : `, ${state.lastDelivered}/${state.lastWakes} wake${state.lastWakes === 1 ? "" : "s"} delivered`;
|
|
55
|
+
return [`wake runner ${runner.name}: ran ${ago(state.lastRunAt, now)}${delivered}${state.lastError ? `, last error: ${state.lastError}` : ""}`, ...(state.status ?? [])];
|
|
61
56
|
});
|
|
62
57
|
}
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
+
exports.sendWake = exports.runCommand = exports.WAKE_RUNNER_TIMEOUT_MS = exports.WAKE_RUNNER_DEFAULT_SECONDS = void 0;
|
|
37
|
+
exports.parseWakeOutput = parseWakeOutput;
|
|
38
|
+
exports.runWakeRunner = runWakeRunner;
|
|
39
|
+
exports.runWakeRunners = runWakeRunners;
|
|
40
|
+
const node_child_process_1 = require("node:child_process");
|
|
41
|
+
const path = __importStar(require("node:path"));
|
|
42
|
+
const runtime_1 = require("../../nerves/runtime");
|
|
43
|
+
const client_1 = require("./client");
|
|
44
|
+
const store_1 = require("./store");
|
|
45
|
+
const wake_runner_state_1 = require("./wake-runner-state");
|
|
46
|
+
/**
|
|
47
|
+
* Wake runners: a command the agent runs on a cadence and delivers the wakes of to Copilot app sessions. The agent knows
|
|
48
|
+
* nothing about what the command watches. Desk's `drains.js poll` is the one that watches GitHub issues; anything that
|
|
49
|
+
* prints the same JSON works.
|
|
50
|
+
*
|
|
51
|
+
* The command prints one JSON value: `{ "wakes": [{ "session", "message", "done" }], "status": ["…"] }`. Each wake's message
|
|
52
|
+
* is sent to its session (`session` is optional when the runner names one). `done` is the argv that reports the wake's
|
|
53
|
+
* delivery: it runs after a successful send, and with `--error <reason>` added after a failed one, so the command can keep
|
|
54
|
+
* the wake pending until it lands. `status` lines show under the app-sessions row of `ouro status`.
|
|
55
|
+
*/
|
|
56
|
+
exports.WAKE_RUNNER_DEFAULT_SECONDS = 300;
|
|
57
|
+
exports.WAKE_RUNNER_TIMEOUT_MS = 2 * 60_000;
|
|
58
|
+
const MAX_STATUS_LINES = 20;
|
|
59
|
+
const MAX_LINE = 300;
|
|
60
|
+
const MAX_OUTPUT = 10 * 1024 * 1024;
|
|
61
|
+
/** The command's output as wakes and status lines, or the reason it is not one. */
|
|
62
|
+
function parseWakeOutput(stdout) {
|
|
63
|
+
let parsed;
|
|
64
|
+
try {
|
|
65
|
+
parsed = JSON.parse(stdout);
|
|
66
|
+
}
|
|
67
|
+
catch {
|
|
68
|
+
throw new Error("the command did not print JSON");
|
|
69
|
+
}
|
|
70
|
+
const record = (parsed && typeof parsed === "object" && !Array.isArray(parsed) ? parsed : {});
|
|
71
|
+
if (!Array.isArray(record.wakes))
|
|
72
|
+
throw new Error('the command printed no "wakes" array');
|
|
73
|
+
const wakes = record.wakes.map((entry, index) => {
|
|
74
|
+
const wake = (entry && typeof entry === "object" ? entry : {});
|
|
75
|
+
if (typeof wake.message !== "string" || !wake.message.trim())
|
|
76
|
+
throw new Error(`wakes[${index}] has no message`);
|
|
77
|
+
if (wake.session !== undefined && (typeof wake.session !== "string" || !wake.session.trim()))
|
|
78
|
+
throw new Error(`wakes[${index}].session is not a session id`);
|
|
79
|
+
if (wake.done !== undefined && (!Array.isArray(wake.done) || wake.done.length === 0 || wake.done.some(part => typeof part !== "string")))
|
|
80
|
+
throw new Error(`wakes[${index}].done is not an argv`);
|
|
81
|
+
return { message: wake.message, ...(wake.session !== undefined ? { session: wake.session } : {}), ...(wake.done !== undefined ? { done: wake.done } : {}) };
|
|
82
|
+
});
|
|
83
|
+
// eslint-disable-next-line no-control-regex
|
|
84
|
+
const clean = (line) => line.replace(/[\u0000-\u001f\u007f-\u009f]/g, " ").trim().slice(0, MAX_LINE);
|
|
85
|
+
const status = Array.isArray(record.status) ? record.status.filter((line) => typeof line === "string").map(clean).filter(Boolean).slice(0, MAX_STATUS_LINES) : [];
|
|
86
|
+
return { wakes, status };
|
|
87
|
+
}
|
|
88
|
+
/** Runs an argv with no shell, returning what it printed. A non-zero exit, a timeout or a missing program is a failure with its reason. */
|
|
89
|
+
const runCommand = (argv, timeoutMs) => new Promise(resolve => {
|
|
90
|
+
const [program, ...args] = argv;
|
|
91
|
+
(0, node_child_process_1.execFile)(program, args, { encoding: "utf8", timeout: timeoutMs, maxBuffer: MAX_OUTPUT, env: process.env }, (error, stdout, stderr) => {
|
|
92
|
+
if (!error)
|
|
93
|
+
return resolve({ ok: true, stdout, error: null });
|
|
94
|
+
const reason = String(stderr).trim().split("\n")[0] || error.message;
|
|
95
|
+
resolve({ ok: false, stdout, error: `${path.basename(program)} failed: ${reason}`.slice(0, MAX_LINE) });
|
|
96
|
+
});
|
|
97
|
+
});
|
|
98
|
+
exports.runCommand = runCommand;
|
|
99
|
+
const sendWake = (client) => async (session, message) => {
|
|
100
|
+
const result = await (0, client_1.sendToAppSession)(session, message, { ...client, delivery: "enqueue" });
|
|
101
|
+
return { ok: result.ok, error: result.error };
|
|
102
|
+
};
|
|
103
|
+
exports.sendWake = sendWake;
|
|
104
|
+
/** Run one runner's command, deliver its wakes and report each one back. Returns the runner's new state. */
|
|
105
|
+
async function runWakeRunner(runner, deps) {
|
|
106
|
+
const state = { lastRunAt: new Date(deps.now).toISOString() };
|
|
107
|
+
const ran = await deps.run(runner.command, exports.WAKE_RUNNER_TIMEOUT_MS);
|
|
108
|
+
if (!ran.ok)
|
|
109
|
+
return fail(runner, state, ran.error ?? "the command failed");
|
|
110
|
+
let output;
|
|
111
|
+
try {
|
|
112
|
+
output = parseWakeOutput(ran.stdout);
|
|
113
|
+
}
|
|
114
|
+
catch (error) {
|
|
115
|
+
return fail(runner, state, error.message);
|
|
116
|
+
}
|
|
117
|
+
state.status = output.status;
|
|
118
|
+
state.lastWakes = output.wakes.length;
|
|
119
|
+
state.lastDelivered = 0;
|
|
120
|
+
const problems = [];
|
|
121
|
+
for (const wake of output.wakes) {
|
|
122
|
+
const session = wake.session ?? runner.session;
|
|
123
|
+
const sent = session
|
|
124
|
+
? await deps.send(session, wake.message).catch((error) => ({ ok: false, error: error.message }))
|
|
125
|
+
: { ok: false, error: "the wake names no session and the runner has none" };
|
|
126
|
+
const reason = sent.ok ? null : sent.error ?? "the app refused the message";
|
|
127
|
+
if (!reason)
|
|
128
|
+
state.lastDelivered += 1;
|
|
129
|
+
else
|
|
130
|
+
problems.push(`could not deliver a wake: ${reason}`);
|
|
131
|
+
if (wake.done) {
|
|
132
|
+
const reported = await deps.run(reason === null ? wake.done : [...wake.done, "--error", reason], exports.WAKE_RUNNER_TIMEOUT_MS);
|
|
133
|
+
if (!reported.ok)
|
|
134
|
+
problems.push(`could not report a wake's delivery: ${reported.error ?? "the command failed"}`);
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
if (problems.length)
|
|
138
|
+
state.lastError = problems[0];
|
|
139
|
+
(0, runtime_1.emitNervesEvent)({ level: problems.length ? "warn" : "info", component: "senses", event: "senses.wake_runner_run", message: "ran a wake runner and delivered its wakes", meta: { runner: runner.name, wakes: state.lastWakes, delivered: state.lastDelivered, problems: problems.length } });
|
|
140
|
+
return state;
|
|
141
|
+
}
|
|
142
|
+
function fail(runner, state, reason) {
|
|
143
|
+
state.lastError = reason;
|
|
144
|
+
(0, runtime_1.emitNervesEvent)({ level: "warn", component: "senses", event: "senses.wake_runner_error", message: "a wake runner's command did not give wakes; nothing was delivered", meta: { runner: runner.name, error: reason } });
|
|
145
|
+
return state;
|
|
146
|
+
}
|
|
147
|
+
/** Run the `only` runners of the configured list (all by default) and write the states. A runner dropped from the config loses its state. */
|
|
148
|
+
async function runWakeRunners(agentRoot, runners, deps, only) {
|
|
149
|
+
const stored = (0, wake_runner_state_1.readWakeRunnerStates)(agentRoot);
|
|
150
|
+
const states = Object.fromEntries(runners.map(runner => [runner.name, stored[runner.name] ?? {}]));
|
|
151
|
+
for (const runner of runners)
|
|
152
|
+
if (!only || only.has(runner.name))
|
|
153
|
+
states[runner.name] = await runWakeRunner(runner, deps);
|
|
154
|
+
(0, store_1.writeJson)((0, wake_runner_state_1.wakeRunnerStateFile)(agentRoot), states);
|
|
155
|
+
}
|
package/npm-shrinkwrap.json
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ouro.bot/cli",
|
|
3
|
-
"version": "0.1.0-alpha.
|
|
3
|
+
"version": "0.1.0-alpha.1116",
|
|
4
4
|
"lockfileVersion": 3,
|
|
5
5
|
"requires": true,
|
|
6
6
|
"packages": {
|
|
7
7
|
"": {
|
|
8
8
|
"name": "@ouro.bot/cli",
|
|
9
|
-
"version": "0.1.0-alpha.
|
|
9
|
+
"version": "0.1.0-alpha.1116",
|
|
10
10
|
"dependencies": {
|
|
11
11
|
"@anthropic-ai/sdk": "^0.78.0",
|
|
12
12
|
"@ouro.bot/friends": "0.1.0-alpha.18",
|
package/package.json
CHANGED
|
@@ -1,174 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.sendWake = exports.connectGithub = exports.DRAIN_LABEL_PREFIX = exports.DRAIN_STATUS_MARKER = exports.ISSUE_POLL_MS = void 0;
|
|
4
|
-
exports.wakeMessage = wakeMessage;
|
|
5
|
-
exports.pollDrain = pollDrain;
|
|
6
|
-
exports.pollIssueDrains = pollIssueDrains;
|
|
7
|
-
const node_crypto_1 = require("node:crypto");
|
|
8
|
-
const node_child_process_1 = require("node:child_process");
|
|
9
|
-
const account_pool_1 = require("../../heart/providers/account-pool");
|
|
10
|
-
const runtime_1 = require("../../nerves/runtime");
|
|
11
|
-
const client_1 = require("./client");
|
|
12
|
-
const issue_drain_state_1 = require("./issue-drain-state");
|
|
13
|
-
const store_1 = require("./store");
|
|
14
|
-
/**
|
|
15
|
-
* Issue drains: Copilot app sessions that work a GitHub repository's issues. A deterministic poll (no model turn)
|
|
16
|
-
* lists each repository's open issues and messages the drain's session when one is new or changed since the last
|
|
17
|
-
* poll. Events are written to disk before anyone is woken, so a crash or a refused message loses nothing; an
|
|
18
|
-
* authentication or network failure is recorded for `ouro status` and leaves every marker where it was.
|
|
19
|
-
*
|
|
20
|
-
* `updated_at` alone is not a revision: the drain's own label edits and status comment move it too. An issue wakes the
|
|
21
|
-
* drain only when something other than the drain changed it since the revision last recorded for that issue.
|
|
22
|
-
*/
|
|
23
|
-
exports.ISSUE_POLL_MS = 5 * 60_000;
|
|
24
|
-
/** A drain's own status comment carries this marker, so its creation and edits do not wake the drain again. */
|
|
25
|
-
exports.DRAIN_STATUS_MARKER = "<!-- ouro-drain-status -->";
|
|
26
|
-
/** The drain moves an issue through labels in this namespace (`drain: triaged`, `drain: in progress`); changing them is not news. */
|
|
27
|
-
exports.DRAIN_LABEL_PREFIX = "drain:";
|
|
28
|
-
/** A closed issue's record is kept this long, so one that comes back unchanged is not mistaken for a new issue. */
|
|
29
|
-
const GONE_MS = 30 * 24 * 3_600_000;
|
|
30
|
-
const MAX_TITLES = 20;
|
|
31
|
-
const PAGE = 100;
|
|
32
|
-
const blank = () => ({ seeded: false, seen: {}, pending: [] });
|
|
33
|
-
function oneLine(text) {
|
|
34
|
-
const flat = text.replace(/\s+/g, " ").trim();
|
|
35
|
-
return flat.length > 120 ? `${flat.slice(0, 119)}…` : flat;
|
|
36
|
-
}
|
|
37
|
-
function wakeMessage(repo, events) {
|
|
38
|
-
const shown = events.slice(0, MAX_TITLES).map(event => `#${event.number} ${oneLine(event.title)}`);
|
|
39
|
-
const more = events.length > MAX_TITLES ? `, and ${events.length - MAX_TITLES} more` : "";
|
|
40
|
-
return `New or changed issues in ${repo}: ${shown.join(", ")}${more}. Drain them.`;
|
|
41
|
-
}
|
|
42
|
-
const escapeRegExp = (text) => text.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
43
|
-
/** The body without its ignored trailer lines (`Active-at: …`), so an edit that touches only those lines leaves the digest alone. */
|
|
44
|
-
function bodyWithoutTrailers(body, keys) {
|
|
45
|
-
if (keys.length === 0)
|
|
46
|
-
return body;
|
|
47
|
-
const trailer = new RegExp(`^[ \\t]*(?:${keys.map(escapeRegExp).join("|")})[ \\t]*:.*(?:\\r?\\n|$)`, "gim");
|
|
48
|
-
return body.replace(trailer, "").trimEnd();
|
|
49
|
-
}
|
|
50
|
-
const digestOf = (issue, keys) => (0, node_crypto_1.createHash)("sha1").update(`${issue.title}\n${bodyWithoutTrailers(issue.body ?? "", keys)}`).digest("hex").slice(0, 16);
|
|
51
|
-
/** The drain's own status comment: the marker, written by the polling account. Its creation and every edit are skipped. */
|
|
52
|
-
const ownStatus = (comment, login) => comment.user?.login?.toLowerCase() === login.toLowerCase() && !!comment.body?.includes(exports.DRAIN_STATUS_MARKER);
|
|
53
|
-
/** A reopen, or a label change outside the drain's own namespace. Closes, assignments, mentions and the drain's labels are not news. */
|
|
54
|
-
const newsEvent = (event) => event.event === "reopened" || ((event.event === "labeled" || event.event === "unlabeled") && !event.label?.name?.startsWith(exports.DRAIN_LABEL_PREFIX));
|
|
55
|
-
/** Whether anyone but the drain changed the issue since `before`: a new issue, an edit, another comment, a reopen or a label outside the drain's own. */
|
|
56
|
-
async function changedByOthers(api, repo, issue, before, digest, ignored) {
|
|
57
|
-
if (!before)
|
|
58
|
-
return true;
|
|
59
|
-
if (before.digest && (before.ignored ?? "") === ignored && before.digest !== digest)
|
|
60
|
-
return true;
|
|
61
|
-
const since = before.updatedAt;
|
|
62
|
-
const comments = (await api.comments(repo, issue.number, since)).filter(comment => comment.updated_at > since);
|
|
63
|
-
if (comments.some(comment => !ownStatus(comment, api.login)))
|
|
64
|
-
return true;
|
|
65
|
-
const events = (await api.issueEvents(repo, issue.number)).filter(event => event.created_at > since);
|
|
66
|
-
return events.some(newsEvent);
|
|
67
|
-
}
|
|
68
|
-
/** Poll one drain's repository, record what changed, then wake its session once with the whole batch. */
|
|
69
|
-
async function pollDrain(drain, states, save, deps) {
|
|
70
|
-
const state = states[drain.repo] ?? (states[drain.repo] = blank());
|
|
71
|
-
const at = new Date(deps.now).toISOString();
|
|
72
|
-
try {
|
|
73
|
-
const api = await deps.connect(drain.account);
|
|
74
|
-
const issues = await api.openIssues(drain.repo);
|
|
75
|
-
const trailers = drain.ignoredBodyTrailers ?? [];
|
|
76
|
-
const ignored = trailers.join(",");
|
|
77
|
-
const seen = {};
|
|
78
|
-
const recorded = [];
|
|
79
|
-
for (const issue of issues) {
|
|
80
|
-
const key = String(issue.number);
|
|
81
|
-
const before = (0, issue_drain_state_1.seenIssue)(state.seen[key]);
|
|
82
|
-
const digest = digestOf(issue, trailers);
|
|
83
|
-
seen[key] = { updatedAt: issue.updated_at, digest, ...(ignored ? { ignored } : {}) };
|
|
84
|
-
if (!state.seeded || before?.updatedAt === issue.updated_at)
|
|
85
|
-
continue;
|
|
86
|
-
if (await changedByOthers(api, drain.repo, issue, before, digest, ignored))
|
|
87
|
-
recorded.push({ number: issue.number, title: issue.title, updatedAt: issue.updated_at });
|
|
88
|
-
}
|
|
89
|
-
for (const [key, entry] of Object.entries(state.seen)) {
|
|
90
|
-
const gone = (0, issue_drain_state_1.seenIssue)(entry);
|
|
91
|
-
const goneAt = gone.goneAt ?? deps.now;
|
|
92
|
-
if (!(key in seen) && deps.now - goneAt < GONE_MS)
|
|
93
|
-
seen[key] = { ...gone, goneAt };
|
|
94
|
-
}
|
|
95
|
-
const open = new Set(issues.map(issue => issue.number));
|
|
96
|
-
const numbers = new Set(recorded.map(event => event.number));
|
|
97
|
-
state.pending = [...state.pending.filter(event => open.has(event.number) && !numbers.has(event.number)), ...recorded];
|
|
98
|
-
state.seen = seen;
|
|
99
|
-
state.seeded = true;
|
|
100
|
-
state.openCount = issues.length;
|
|
101
|
-
delete state.lastError;
|
|
102
|
-
}
|
|
103
|
-
catch (error) {
|
|
104
|
-
state.lastError = error.message;
|
|
105
|
-
(0, runtime_1.emitNervesEvent)({ level: "warn", component: "senses", event: "senses.issue_drain_poll_error", message: "could not poll a repository's issues; markers stay where they were", meta: { repo: drain.repo, error: state.lastError } });
|
|
106
|
-
}
|
|
107
|
-
state.lastPollAt = at;
|
|
108
|
-
save();
|
|
109
|
-
(0, runtime_1.emitNervesEvent)({ component: "senses", event: "senses.issue_drain_poll", message: "polled a repository's issues for its drain", meta: { repo: drain.repo, open: state.openCount ?? null, waiting: state.pending.length, ok: !state.lastError } });
|
|
110
|
-
if (state.pending.length === 0)
|
|
111
|
-
return;
|
|
112
|
-
const events = state.pending;
|
|
113
|
-
const sent = await deps.send(drain.session, wakeMessage(drain.repo, events)).catch((error) => ({ ok: false, error: error.message }));
|
|
114
|
-
if (sent.ok) {
|
|
115
|
-
state.pending = [];
|
|
116
|
-
state.lastWakeAt = new Date(deps.now).toISOString();
|
|
117
|
-
state.lastWakeCount = events.length;
|
|
118
|
-
}
|
|
119
|
-
else {
|
|
120
|
-
state.lastError = `could not wake the drain: ${sent.error ?? "the app refused the message"}`;
|
|
121
|
-
(0, runtime_1.emitNervesEvent)({ level: "warn", component: "senses", event: "senses.issue_drain_wake_error", message: "could not message an issue drain; its events wait on disk", meta: { repo: drain.repo, session: drain.session, error: state.lastError } });
|
|
122
|
-
}
|
|
123
|
-
save();
|
|
124
|
-
(0, runtime_1.emitNervesEvent)({ component: "senses", event: "senses.issue_drain_wake", message: "messaged an issue drain about new or changed issues", meta: { repo: drain.repo, session: drain.session, issues: events.length, ok: sent.ok } });
|
|
125
|
-
}
|
|
126
|
-
/** Poll every configured drain once. Repositories dropped from the config lose their state. */
|
|
127
|
-
async function pollIssueDrains(agentRoot, drains, deps) {
|
|
128
|
-
const stored = (0, issue_drain_state_1.readDrainStates)(agentRoot);
|
|
129
|
-
const states = Object.fromEntries(drains.map(drain => [drain.repo, stored[drain.repo] ?? blank()]));
|
|
130
|
-
const save = () => (0, store_1.writeJson)((0, issue_drain_state_1.drainStateFile)(agentRoot), states);
|
|
131
|
-
for (const drain of drains)
|
|
132
|
-
await pollDrain(drain, states, save, deps);
|
|
133
|
-
save();
|
|
134
|
-
}
|
|
135
|
-
/** The drain's GitHub access: its account's token from the `gh` CLI, then the REST API. */
|
|
136
|
-
const connectGithub = async (account) => {
|
|
137
|
-
const tokenArgs = ["auth", "token", "--hostname", "github.com", ...(account ? ["--user", account] : [])];
|
|
138
|
-
const result = (0, node_child_process_1.spawnSync)("gh", tokenArgs, { encoding: "utf8", timeout: 10_000, env: (0, account_pool_1.cleanGhEnv)() });
|
|
139
|
-
const token = result.status === 0 ? String(result.stdout ?? "").trim() : "";
|
|
140
|
-
if (!token)
|
|
141
|
-
throw new Error(`the GitHub CLI has no token for ${account ?? "its active account"} (run \`gh auth login --hostname github.com\`)`);
|
|
142
|
-
const get = async (route) => {
|
|
143
|
-
const response = await fetch(`https://api.github.com${route}`, {
|
|
144
|
-
headers: { Authorization: `Bearer ${token}`, Accept: "application/vnd.github+json", "User-Agent": "ouro-issue-drains" },
|
|
145
|
-
signal: AbortSignal.timeout(30_000),
|
|
146
|
-
});
|
|
147
|
-
if (!response.ok)
|
|
148
|
-
throw new Error(`GitHub answered HTTP ${response.status} for ${route.split("?")[0]}`);
|
|
149
|
-
return { body: await response.json(), next: /rel="next"/.test(response.headers.get("link") ?? "") };
|
|
150
|
-
};
|
|
151
|
-
const all = async (route) => {
|
|
152
|
-
const items = [];
|
|
153
|
-
const join = route.includes("?") ? "&" : "?";
|
|
154
|
-
for (let page = 1;; page++) {
|
|
155
|
-
const { body, next } = await get(`${route}${join}per_page=${PAGE}&page=${page}`);
|
|
156
|
-
items.push(...body);
|
|
157
|
-
if (!next)
|
|
158
|
-
return items;
|
|
159
|
-
}
|
|
160
|
-
};
|
|
161
|
-
const login = account ?? String((await get("/user")).body.login ?? "");
|
|
162
|
-
return {
|
|
163
|
-
login,
|
|
164
|
-
openIssues: async (repo) => (await all(`/repos/${repo}/issues?state=open&sort=updated&direction=desc`)).filter(issue => !issue.pull_request),
|
|
165
|
-
comments: (repo, issue, since) => all(`/repos/${repo}/issues/${issue}/comments?since=${encodeURIComponent(since)}`),
|
|
166
|
-
issueEvents: (repo, issue) => all(`/repos/${repo}/issues/${issue}/events`),
|
|
167
|
-
};
|
|
168
|
-
};
|
|
169
|
-
exports.connectGithub = connectGithub;
|
|
170
|
-
const sendWake = (client) => async (session, message) => {
|
|
171
|
-
const result = await (0, client_1.sendToAppSession)(session, message, { ...client, delivery: "enqueue" });
|
|
172
|
-
return { ok: result.ok, error: result.error };
|
|
173
|
-
};
|
|
174
|
-
exports.sendWake = sendWake;
|