pendingyou 0.43.1 → 0.43.2
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.md +37 -0
- package/dist/apps/codex-wake.js +33 -6
- package/dist/apps/opencode-plugin.js +11 -5
- package/dist/apps/pi-extension.js +8 -4
- package/dist/args.js +2 -0
- package/dist/drafter/claude-code.js +119 -0
- package/dist/drafter/config.js +35 -0
- package/dist/drafter/eval.js +188 -0
- package/dist/drafter/own-messages.js +18 -0
- package/dist/drafter/prompt.js +124 -0
- package/dist/drafter/tiers.js +106 -0
- package/dist/drafter/types.js +10 -0
- package/dist/drafter/validate.js +240 -0
- package/dist/handed.js +4 -1
- package/dist/io.js +6 -1
- package/dist/main.js +1 -0
- package/dist/permission.js +3 -1
- package/dist/pickup.js +9 -2
- package/dist/presence.js +18 -3
- package/dist/setup.js +8 -2
- package/dist/version.js +1 -1
- package/mod/.claude-plugin/plugin.json +1 -1
- package/mod/README.md +1 -1
- package/mod/hooks/register.js +178 -13
- package/mod/hooks/wake.js +90 -6
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,42 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 0.43.2 (2026-11-17)
|
|
4
|
+
|
|
5
|
+
- Fixed: a Claude Code session Pending You showed as running didn't hear a question you handed its agent. The wake mod
|
|
6
|
+
listened only for 12 hours after the session last used Pending You, and kept its name only in memory when it had no
|
|
7
|
+
cards of its own, so after 12 quiet hours, or after a restart, `claude --resume`, `/resume`, `/clear` or an upgrade
|
|
8
|
+
that reloaded the mod, the session heard nothing until you typed to it. Now an open session that has used Pending You
|
|
9
|
+
under a name hears a question handed to it for as long as it's open: within seconds through the computer's one wait,
|
|
10
|
+
as before, and on its own timer as the fallback (every minute for the first half hour after it last used Pending
|
|
11
|
+
You, every 3 minutes up to 12 hours, then every 10 minutes). Its name, and when it last used Pending You, are kept
|
|
12
|
+
with its cards in the mod's store, so a restart, a resume and a reload keep hearing. A session the mod knows no name
|
|
13
|
+
for goes by the one its presence last gave Pending You, which is the name Pending You shows running, so sessions open
|
|
14
|
+
across this upgrade hear too. Of several sessions by one name, the one that used Pending You most recently takes a
|
|
15
|
+
question handed to it; another takes it only if nobody has after half a minute. A session doesn't tell the same
|
|
16
|
+
question twice after a reload, and a subagent never renames its session. `/clear` starts a new conversation, which
|
|
17
|
+
hears from its first call with its name, as before. A branch (`/branch`) is a new task: it neither says nor hears its
|
|
18
|
+
parent's name until it uses Pending You itself, and Pending You no longer shows it running under that name. Who
|
|
19
|
+
hears it is otherwise unchanged: the session in the card's folder, one session only, never the one that asked.
|
|
20
|
+
- Codex: of two threads by one name, an open one takes a handed question before a closed one used more recently.
|
|
21
|
+
- Codex, OpenCode and Pi: a session its app says is open (presence) keeps listening for a question handed to it however
|
|
22
|
+
long it's been quiet, not only for 12 hours, and its listener is started again when it stops (Codex's keeper,
|
|
23
|
+
OpenCode's plugin and Pi's extension, every 5 minutes). Closed, the 12 hours as before.
|
|
24
|
+
- Fixed: `status` said "Setup: waiting for its first session. Start Claude Code (or restart it)…" beside "Ready" for a
|
|
25
|
+
sign-in its assistants had been using for days, whose own test question never went all the way round. It now says
|
|
26
|
+
"Setup: finished. It’s in use, so its unfinished test question no longer matters.", and
|
|
27
|
+
for a sign-in that reported but has posted nothing, "reported, but its test question isn’t sent yet…". A session of a
|
|
28
|
+
sign-in in use is no longer told, as it starts, that "setup’s test question isn’t sent yet" (every `init` started
|
|
29
|
+
those reminders over). Needs Pending You's server from the same release, which says when a sign-in is in use.
|
|
30
|
+
- Fixed: Codex, OpenCode and Pi: when a card came back from the assistant you handed it to, its session heard why, but
|
|
31
|
+
not your answer after: the listener forgot the card with the notice. It keeps it now, until it's answered.
|
|
32
|
+
- When the session-start or next-message hook can't reach Pending You, its line now says to call `list_pending` with
|
|
33
|
+
your name.
|
|
34
|
+
- Guide 2.61.2 (a patch: no words change) carries the same mod in the Pending You plugin for Claude.
|
|
35
|
+
- Run `npx pendingyou@latest init`. Claude Code: nothing to restart (open sessions reload the mod by themselves).
|
|
36
|
+
Codex: restart it, or each thread picks it up once its keeper starts again (its next session start or message after
|
|
37
|
+
its last one ended). OpenCode and Pi: restart them (Pi: `/reload`), since their plugin points at the version init
|
|
38
|
+
installed.
|
|
39
|
+
|
|
3
40
|
## 0.43.1 (2026-11-16)
|
|
4
41
|
|
|
5
42
|
- Claude Code: when you answer a card while its session sits idle, the turn that wakes it now carries your answer in
|
package/dist/apps/codex-wake.js
CHANGED
|
@@ -6,7 +6,7 @@ import { DEFAULT_ORIGIN } from "../args.js";
|
|
|
6
6
|
import { connectionMachine, readCredential } from "../credentials.js";
|
|
7
7
|
import { configDir, readJson, withLock, writeWhole } from "../files.js";
|
|
8
8
|
import { APPROVED_DRAFT_NOTICE, approvesDraft, clip, delegatedOf, handledOf, isClosed, noticeOf, } from "../format.js";
|
|
9
|
-
import { chooseThread, goesBy, HANDED_FOR_MS, handedCard, handedMessage, namesAnotherComputer, } from "../handed.js";
|
|
9
|
+
import { chooseThread, goesBy, HANDED_FOR_MS, handedCard, handedMessage, namesAnotherComputer, OPEN_FOR_MS, } from "../handed.js";
|
|
10
10
|
import { clearHerdr, herdrTarget, isUrgency, PANE_ID, reportCommand, writeHerdr, } from "../herdr.js";
|
|
11
11
|
import { linkSoon } from "../link.js";
|
|
12
12
|
import { macName, tailscaleName } from "../remote.js";
|
|
@@ -68,6 +68,9 @@ function threadOf(value) {
|
|
|
68
68
|
...(names.length ? { names: names.slice(-NAMES) } : {}),
|
|
69
69
|
...(typeof value.cwd === 'string' && value.cwd ? { cwd: value.cwd } : {}),
|
|
70
70
|
...(typeof value.herdr === 'string' && PANE_ID.test(value.herdr) ? { herdr: value.herdr } : {}),
|
|
71
|
+
...(typeof value.liveAt === 'number' && Number.isFinite(value.liveAt)
|
|
72
|
+
? { liveAt: value.liveAt }
|
|
73
|
+
: {}),
|
|
71
74
|
...(typeof value.answeredAt === 'number' && Number.isFinite(value.answeredAt)
|
|
72
75
|
? { answeredAt: value.answeredAt }
|
|
73
76
|
: {}),
|
|
@@ -88,7 +91,9 @@ async function load(io, app) {
|
|
|
88
91
|
turns: isObject(file?.turns) ? file.turns : {},
|
|
89
92
|
};
|
|
90
93
|
}
|
|
91
|
-
const mayBeHanded = (thread, now) => Boolean(thread.names?.length) &&
|
|
94
|
+
const mayBeHanded = (thread, now) => Boolean(thread.names?.length) &&
|
|
95
|
+
(now - thread.at < HANDED_FOR_MS ||
|
|
96
|
+
(thread.liveAt !== undefined && now - thread.liveAt < OPEN_FOR_MS));
|
|
92
97
|
const listenable = (thread, now) => thread !== undefined && (Object.keys(thread.cards).length > 0 || mayBeHanded(thread, now));
|
|
93
98
|
async function change(io, app, edit) {
|
|
94
99
|
const path = statePath(io, app);
|
|
@@ -100,7 +105,8 @@ async function change(io, app, edit) {
|
|
|
100
105
|
if (!(now - turn.at < KEEP_TURN_MS))
|
|
101
106
|
delete state.turns[id];
|
|
102
107
|
for (const [id, thread] of Object.entries(state.threads))
|
|
103
|
-
if (!(now - thread.at < KEEP_THREAD_MS) ||
|
|
108
|
+
if (!(now - Math.max(thread.at, thread.liveAt ?? 0) < KEEP_THREAD_MS) ||
|
|
109
|
+
!listenable(thread, now))
|
|
104
110
|
delete state.threads[id];
|
|
105
111
|
await writeWhole(path, `${JSON.stringify(state, null, 2)}\n`, { secret: true });
|
|
106
112
|
return state;
|
|
@@ -327,6 +333,22 @@ export async function touchThread(io, app, thread, cwd) {
|
|
|
327
333
|
}
|
|
328
334
|
catch { }
|
|
329
335
|
}
|
|
336
|
+
export async function markOpen(io, app, thread, state, at) {
|
|
337
|
+
try {
|
|
338
|
+
if (!THREAD.test(thread) || !(await load(io, app)).threads[thread])
|
|
339
|
+
return;
|
|
340
|
+
await change(io, app, (record) => {
|
|
341
|
+
const known = record.threads[thread];
|
|
342
|
+
if (!known)
|
|
343
|
+
return;
|
|
344
|
+
if (state === 'live')
|
|
345
|
+
known.liveAt = Math.max(known.liveAt ?? 0, at);
|
|
346
|
+
else if (known.liveAt !== undefined && known.liveAt <= at)
|
|
347
|
+
delete known.liveAt;
|
|
348
|
+
});
|
|
349
|
+
}
|
|
350
|
+
catch { }
|
|
351
|
+
}
|
|
330
352
|
export async function threadNames(io, app, thread) {
|
|
331
353
|
if (!thread)
|
|
332
354
|
return [];
|
|
@@ -367,6 +389,7 @@ async function candidates(io, app) {
|
|
|
367
389
|
...(record.cwd ? { cwd: record.cwd } : {}),
|
|
368
390
|
at: record.at,
|
|
369
391
|
live: app === 'codex' || (await listening(io, app, thread)),
|
|
392
|
+
...(record.liveAt !== undefined && now - record.liveAt < OPEN_FOR_MS ? { open: true } : {}),
|
|
370
393
|
});
|
|
371
394
|
}
|
|
372
395
|
return found;
|
|
@@ -625,6 +648,7 @@ async function handedHere(io, options, requests) {
|
|
|
625
648
|
}
|
|
626
649
|
return mine;
|
|
627
650
|
}
|
|
651
|
+
const stillTheirs = (heard) => noticeOf(heard)?.kind === 'came_back' && !isClosed(heard);
|
|
628
652
|
async function giveBack(io, origin, list) {
|
|
629
653
|
for (const { heard } of list)
|
|
630
654
|
await releaseHanded(io, origin, heard.requestId, momentOf(heard)).catch(() => { });
|
|
@@ -687,8 +711,10 @@ export async function listen(io, options) {
|
|
|
687
711
|
const card = cards[heard.requestId];
|
|
688
712
|
if (!card || !heard.ready || delegatedOf(heard))
|
|
689
713
|
continue;
|
|
690
|
-
if (await wasHanded(io, origin, heard.requestId, momentOf(heard)))
|
|
691
|
-
|
|
714
|
+
if (await wasHanded(io, origin, heard.requestId, momentOf(heard))) {
|
|
715
|
+
if (!stillTheirs(heard))
|
|
716
|
+
done.push(heard.requestId);
|
|
717
|
+
}
|
|
692
718
|
else
|
|
693
719
|
ready.push({ heard, card });
|
|
694
720
|
}
|
|
@@ -715,7 +741,8 @@ export async function listen(io, options) {
|
|
|
715
741
|
return 0;
|
|
716
742
|
}
|
|
717
743
|
for (const { heard } of ready)
|
|
718
|
-
|
|
744
|
+
if (!stillTheirs(heard))
|
|
745
|
+
done.push(heard.requestId);
|
|
719
746
|
for (const heard of requests)
|
|
720
747
|
if (cards[heard.requestId] && isClosed(heard) && !done.includes(heard.requestId))
|
|
721
748
|
done.push(heard.requestId);
|
|
@@ -13,6 +13,7 @@ const TEXT_CHARS = 100_000;
|
|
|
13
13
|
const CARD_TOOL = /^[A-Za-z0-9_-]*pendingyou[A-Za-z0-9_-]*_(post_request|post_answer|update_request|reply_in_thread|cancel_request|answer_delegated|hand_back)$/i;
|
|
14
14
|
const NAMED_TOOL = /^[A-Za-z0-9_-]*pendingyou[A-Za-z0-9_-]*_(whoami|list_pending|get_request)$/i;
|
|
15
15
|
export const HANDED_FOR_MS = 12 * 60 * 60_000;
|
|
16
|
+
export const OPEN_FOR_MS = 15 * 60_000;
|
|
16
17
|
const NUDGED = 'Asked OpenCode to put what it needs from you on a card.';
|
|
17
18
|
const isObject = (value) => typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
18
19
|
const ourMark = (part) => {
|
|
@@ -34,10 +35,11 @@ export function wantsListener(file, session, now = Date.now()) {
|
|
|
34
35
|
return false;
|
|
35
36
|
if (isObject(thread.cards) && Object.keys(thread.cards).length > 0)
|
|
36
37
|
return true;
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
38
|
+
if (!Array.isArray(thread.names) || thread.names.length === 0)
|
|
39
|
+
return false;
|
|
40
|
+
if (typeof thread.liveAt === 'number' && now - thread.liveAt < OPEN_FOR_MS)
|
|
41
|
+
return true;
|
|
42
|
+
return typeof thread.at === 'number' && now - thread.at < HANDED_FOR_MS;
|
|
41
43
|
}
|
|
42
44
|
const BASE62 = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
|
|
43
45
|
export function partId(now = Date.now()) {
|
|
@@ -172,8 +174,12 @@ export async function pendingYou(input, deps) {
|
|
|
172
174
|
deps.presence(id, 'live');
|
|
173
175
|
stopBeat ??=
|
|
174
176
|
deps.every?.(PRESENCE_EVERY_MS, () => {
|
|
175
|
-
for (const each of present)
|
|
177
|
+
for (const each of present) {
|
|
176
178
|
deps.presence?.(each, 'live');
|
|
179
|
+
const session = sessions.get(each);
|
|
180
|
+
if (session && !session.listener && !session.parent && deps.wanted?.(each))
|
|
181
|
+
listenFor(each, session);
|
|
182
|
+
}
|
|
177
183
|
}) ?? null;
|
|
178
184
|
};
|
|
179
185
|
const closed = (id) => {
|
|
@@ -11,6 +11,7 @@ const STOP_MS = 2000;
|
|
|
11
11
|
export const CARD_TOOL = /^mcp__[A-Za-z0-9_]*pendingyou[A-Za-z0-9_]*__(post_request|post_answer|update_request|reply_in_thread|cancel_request|answer_delegated|hand_back)$/i;
|
|
12
12
|
export const NAMED_TOOL = /^mcp__[A-Za-z0-9_]*pendingyou[A-Za-z0-9_]*__(whoami|list_pending|get_request)$/i;
|
|
13
13
|
export const HANDED_FOR_MS = 12 * 60 * 60_000;
|
|
14
|
+
export const OPEN_FOR_MS = 15 * 60_000;
|
|
14
15
|
export const HANDED_PREFIX = 'Pending You: your person handed you';
|
|
15
16
|
const HANDED_REPLY = ' on the question handed to you, ';
|
|
16
17
|
const isHandedWake = (text) => {
|
|
@@ -93,10 +94,11 @@ export function wantsListener(file, session, now = Date.now()) {
|
|
|
93
94
|
return false;
|
|
94
95
|
if (isObject(thread.cards) && Object.keys(thread.cards).length > 0)
|
|
95
96
|
return true;
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
97
|
+
if (!Array.isArray(thread.names) || thread.names.length === 0)
|
|
98
|
+
return false;
|
|
99
|
+
if (typeof thread.liveAt === 'number' && now - thread.liveAt < OPEN_FOR_MS)
|
|
100
|
+
return true;
|
|
101
|
+
return typeof thread.at === 'number' && now - thread.at < HANDED_FOR_MS;
|
|
100
102
|
}
|
|
101
103
|
function reasonOf(printed) {
|
|
102
104
|
try {
|
|
@@ -172,6 +174,8 @@ export function pendingYou(setup, options = {}) {
|
|
|
172
174
|
if (closed || !present)
|
|
173
175
|
return;
|
|
174
176
|
presence(present, 'live');
|
|
177
|
+
if (!listener)
|
|
178
|
+
listen();
|
|
175
179
|
heartbeat();
|
|
176
180
|
}, PRESENCE_EVERY_MS);
|
|
177
181
|
unref(beat);
|
package/dist/args.js
CHANGED
|
@@ -125,6 +125,7 @@ const FLAGS = {
|
|
|
125
125
|
name: true,
|
|
126
126
|
transcript: true,
|
|
127
127
|
keep: false,
|
|
128
|
+
fork: false,
|
|
128
129
|
pid: true,
|
|
129
130
|
at: true,
|
|
130
131
|
},
|
|
@@ -428,6 +429,7 @@ export function parseArgs(argv, env = {}) {
|
|
|
428
429
|
...given('transcript'),
|
|
429
430
|
...(agentName ? { agentName } : {}),
|
|
430
431
|
...(flags.has('keep') ? { keep: true } : {}),
|
|
432
|
+
...(flags.has('fork') ? { fork: true } : {}),
|
|
431
433
|
...(pid ? { pid } : {}),
|
|
432
434
|
...(at ? { at } : {}),
|
|
433
435
|
};
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
import { mkdir } from 'node:fs/promises';
|
|
2
|
+
import { join } from 'node:path';
|
|
3
|
+
import { configDir } from "../files.js";
|
|
4
|
+
import { atLeast, claudeVersion } from "../mod.js";
|
|
5
|
+
import { SCHEMA, SYSTEM, turnText } from "./prompt.js";
|
|
6
|
+
import { isVerdict } from "./validate.js";
|
|
7
|
+
export const CLAUDE_MIN = '2.1.326';
|
|
8
|
+
export const CALL_MS = 30_000;
|
|
9
|
+
export const DRAFTER_MARK = 'PENDINGYOU_DRAFTER';
|
|
10
|
+
export function claudeArgs(model) {
|
|
11
|
+
return [
|
|
12
|
+
'-p',
|
|
13
|
+
'--model',
|
|
14
|
+
model,
|
|
15
|
+
'--setting-sources',
|
|
16
|
+
'',
|
|
17
|
+
'--strict-mcp-config',
|
|
18
|
+
'--tools',
|
|
19
|
+
'',
|
|
20
|
+
'--disable-slash-commands',
|
|
21
|
+
'--no-session-persistence',
|
|
22
|
+
'--system-prompt',
|
|
23
|
+
SYSTEM,
|
|
24
|
+
'--json-schema',
|
|
25
|
+
JSON.stringify(SCHEMA),
|
|
26
|
+
'--output-format',
|
|
27
|
+
'stream-json',
|
|
28
|
+
'--verbose',
|
|
29
|
+
];
|
|
30
|
+
}
|
|
31
|
+
export function claudeEnv(env, home) {
|
|
32
|
+
return {
|
|
33
|
+
HOME: env.HOME || home,
|
|
34
|
+
PATH: env.PATH ?? '',
|
|
35
|
+
...(env.USER ? { USER: env.USER } : {}),
|
|
36
|
+
LANG: 'C.UTF-8',
|
|
37
|
+
TERM: 'dumb',
|
|
38
|
+
[DRAFTER_MARK]: '1',
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
export const runDir = (io) => join(configDir(io), 'drafter', 'run');
|
|
42
|
+
const isObject = (value) => typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
43
|
+
const SIGNIN = /not logged in|log ?in|logged out|authenticat|invalid api key|oauth|credential/i;
|
|
44
|
+
export function readCall(result) {
|
|
45
|
+
if (result.code === 127)
|
|
46
|
+
return { failure: 'missing' };
|
|
47
|
+
if (result.code === 124)
|
|
48
|
+
return { failure: 'timeout' };
|
|
49
|
+
let limit = false;
|
|
50
|
+
let final = null;
|
|
51
|
+
for (const line of result.stdout.split('\n')) {
|
|
52
|
+
if (!line.trim().startsWith('{'))
|
|
53
|
+
continue;
|
|
54
|
+
let event;
|
|
55
|
+
try {
|
|
56
|
+
event = JSON.parse(line);
|
|
57
|
+
}
|
|
58
|
+
catch {
|
|
59
|
+
continue;
|
|
60
|
+
}
|
|
61
|
+
if (!isObject(event))
|
|
62
|
+
continue;
|
|
63
|
+
if (event.type === 'rate_limit_event' && isObject(event.rate_limit_info)) {
|
|
64
|
+
const info = event.rate_limit_info;
|
|
65
|
+
if (info.status === 'rejected' || info.isUsingOverage === true || info.overageInUse === true)
|
|
66
|
+
limit = true;
|
|
67
|
+
}
|
|
68
|
+
if (event.type === 'result')
|
|
69
|
+
final = event;
|
|
70
|
+
}
|
|
71
|
+
const costUsd = final && typeof final.total_cost_usd === 'number' ? { costUsd: final.total_cost_usd } : {};
|
|
72
|
+
if (!final) {
|
|
73
|
+
if (limit)
|
|
74
|
+
return { failure: 'limit' };
|
|
75
|
+
const said = `${result.stderr}\n${result.stdout}`;
|
|
76
|
+
return { failure: result.code !== 0 && SIGNIN.test(said) ? 'signin' : 'failed' };
|
|
77
|
+
}
|
|
78
|
+
if (final.is_error === true) {
|
|
79
|
+
if (limit)
|
|
80
|
+
return { failure: 'limit', ...costUsd };
|
|
81
|
+
const said = `${typeof final.result === 'string' ? final.result : ''}\n${result.stderr}`;
|
|
82
|
+
return { failure: SIGNIN.test(said) ? 'signin' : 'failed', ...costUsd };
|
|
83
|
+
}
|
|
84
|
+
const verdict = final.structured_output;
|
|
85
|
+
if (!isVerdict(verdict))
|
|
86
|
+
return { failure: limit ? 'limit' : 'output', ...costUsd };
|
|
87
|
+
return { verdict: verdict, ...(limit ? { overLimit: true } : {}), ...costUsd };
|
|
88
|
+
}
|
|
89
|
+
export async function claudeReady(io) {
|
|
90
|
+
const version = await io.run('claude', ['--version'], 20_000);
|
|
91
|
+
if (version.code === 127)
|
|
92
|
+
return 'missing';
|
|
93
|
+
const found = claudeVersion(version.stdout);
|
|
94
|
+
if (version.code !== 0 || !found || !atLeast(found, CLAUDE_MIN))
|
|
95
|
+
return 'old';
|
|
96
|
+
const help = await io.run('claude', ['--help'], 20_000);
|
|
97
|
+
return help.code === 0 && help.stdout.includes('--json-schema') ? null : 'old';
|
|
98
|
+
}
|
|
99
|
+
export function claudeCodeDrafter(io) {
|
|
100
|
+
return {
|
|
101
|
+
id: 'claude-code',
|
|
102
|
+
async call(model, input) {
|
|
103
|
+
const started = io.now();
|
|
104
|
+
const cwd = runDir(io);
|
|
105
|
+
try {
|
|
106
|
+
await mkdir(cwd, { recursive: true, mode: 0o700 });
|
|
107
|
+
}
|
|
108
|
+
catch {
|
|
109
|
+
return { model, seconds: 0, failure: 'failed' };
|
|
110
|
+
}
|
|
111
|
+
const result = await io.run('claude', claudeArgs(model), CALL_MS, {
|
|
112
|
+
env: claudeEnv(io.env, io.home),
|
|
113
|
+
stdin: turnText(input),
|
|
114
|
+
cwd,
|
|
115
|
+
});
|
|
116
|
+
return { model, seconds: (io.now() - started) / 1000, ...readCall(result) };
|
|
117
|
+
},
|
|
118
|
+
};
|
|
119
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { join } from 'node:path';
|
|
2
|
+
import { configDir, readJson } from "../files.js";
|
|
3
|
+
import { DEFAULT_CONFIG, } from "./types.js";
|
|
4
|
+
export const MAX_TIERS = 3;
|
|
5
|
+
const MODEL = /^[a-z][a-z0-9.-]{1,60}$/;
|
|
6
|
+
export const configPath = (io) => join(configDir(io), 'drafter', 'config.json');
|
|
7
|
+
export function configOf(value) {
|
|
8
|
+
if (typeof value !== 'object' || value === null)
|
|
9
|
+
return DEFAULT_CONFIG;
|
|
10
|
+
const given = value;
|
|
11
|
+
const tiers = Array.isArray(given.tiers)
|
|
12
|
+
? given.tiers.flatMap((tier) => {
|
|
13
|
+
if (typeof tier !== 'object' || tier === null)
|
|
14
|
+
return [];
|
|
15
|
+
const { model, below } = tier;
|
|
16
|
+
if (typeof model !== 'string' || !MODEL.test(model))
|
|
17
|
+
return [];
|
|
18
|
+
if (typeof below !== 'number' || !(below >= 0 && below <= 1))
|
|
19
|
+
return [];
|
|
20
|
+
return [{ model, below }];
|
|
21
|
+
})
|
|
22
|
+
: [];
|
|
23
|
+
const unsure = given.unsure === 'card' || given.unsure === 'quiet' || given.unsure === 'none'
|
|
24
|
+
? given.unsure
|
|
25
|
+
: DEFAULT_CONFIG.unsure;
|
|
26
|
+
const atLimit = given.atLimit === 'stop' || given.atLimit === 'go' ? given.atLimit : DEFAULT_CONFIG.atLimit;
|
|
27
|
+
return {
|
|
28
|
+
tiers: tiers.length && tiers.length <= MAX_TIERS ? tiers : DEFAULT_CONFIG.tiers,
|
|
29
|
+
unsure,
|
|
30
|
+
atLimit,
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
export async function readConfig(io) {
|
|
34
|
+
return configOf(await readJson(configPath(io)).catch(() => null));
|
|
35
|
+
}
|
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
import { flags } from "./tiers.js";
|
|
2
|
+
import { linksOf } from "./validate.js";
|
|
3
|
+
const needsIn = (kinds) => kinds.some((kind) => kind === 'question' || kind === 'step');
|
|
4
|
+
const answerIn = (kinds) => kinds.includes('answer');
|
|
5
|
+
const empty = () => ({ caught: 0, missed: 0, wrong: 0 });
|
|
6
|
+
const tally = (counts, truth, got) => {
|
|
7
|
+
if (truth && got)
|
|
8
|
+
counts.caught++;
|
|
9
|
+
else if (truth)
|
|
10
|
+
counts.missed++;
|
|
11
|
+
else if (got)
|
|
12
|
+
counts.wrong++;
|
|
13
|
+
};
|
|
14
|
+
const emptyScore = () => ({
|
|
15
|
+
turns: 0,
|
|
16
|
+
needs: empty(),
|
|
17
|
+
answer: empty(),
|
|
18
|
+
right: 0,
|
|
19
|
+
answered: 0,
|
|
20
|
+
failures: {},
|
|
21
|
+
sure: { turns: 0, right: 0 },
|
|
22
|
+
});
|
|
23
|
+
function add(score, row, verdict, sure) {
|
|
24
|
+
score.turns++;
|
|
25
|
+
const truthNeeds = needsIn(row.labels);
|
|
26
|
+
const truthAnswer = answerIn(row.labels);
|
|
27
|
+
const kinds = verdict ? [verdict.kind, verdict.also] : [];
|
|
28
|
+
const gotNeeds = needsIn(kinds);
|
|
29
|
+
const gotAnswer = answerIn(kinds);
|
|
30
|
+
tally(score.needs, truthNeeds, gotNeeds);
|
|
31
|
+
tally(score.answer, truthAnswer, gotAnswer);
|
|
32
|
+
const right = truthNeeds === gotNeeds && truthAnswer === gotAnswer;
|
|
33
|
+
if (right)
|
|
34
|
+
score.right++;
|
|
35
|
+
if (verdict)
|
|
36
|
+
score.answered++;
|
|
37
|
+
if (sure) {
|
|
38
|
+
score.sure.turns++;
|
|
39
|
+
if (right)
|
|
40
|
+
score.sure.right++;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
const percentile = (values, at) => {
|
|
44
|
+
if (!values.length)
|
|
45
|
+
return 0;
|
|
46
|
+
const sorted = [...values].sort((a, b) => a - b);
|
|
47
|
+
return sorted[Math.min(sorted.length - 1, Math.floor(at * (sorted.length - 1) + 0.5))];
|
|
48
|
+
};
|
|
49
|
+
export function report(rows, drafts, tiers) {
|
|
50
|
+
const perTier = tiers.map((tier) => ({ model: tier.model, score: emptyScore() }));
|
|
51
|
+
const together = emptyScore();
|
|
52
|
+
const actions = {};
|
|
53
|
+
const broken = {};
|
|
54
|
+
const result = {
|
|
55
|
+
turns: rows.length,
|
|
56
|
+
tiers: perTier,
|
|
57
|
+
together,
|
|
58
|
+
unsure: 0,
|
|
59
|
+
actions,
|
|
60
|
+
notices: { dropped: 0, expected: 0, wrong: 0 },
|
|
61
|
+
duplicates: { dropped: 0, expected: 0 },
|
|
62
|
+
grouped: { right: 0, expected: 0 },
|
|
63
|
+
main: { left: 0, expected: 0 },
|
|
64
|
+
links: { kept: 0, expected: 0 },
|
|
65
|
+
broken,
|
|
66
|
+
seconds: { median: 0, p90: 0 },
|
|
67
|
+
calls: { made: 0, valid: 0 },
|
|
68
|
+
costUsd: 0,
|
|
69
|
+
};
|
|
70
|
+
const seconds = [];
|
|
71
|
+
rows.forEach((row, index) => {
|
|
72
|
+
const draft = drafts[index];
|
|
73
|
+
for (const [at, call] of draft.calls.entries()) {
|
|
74
|
+
const tier = perTier[at];
|
|
75
|
+
if (!tier)
|
|
76
|
+
continue;
|
|
77
|
+
if (call.failure)
|
|
78
|
+
tier.score.failures[call.failure] = (tier.score.failures[call.failure] ?? 0) + 1;
|
|
79
|
+
if (call.seconds)
|
|
80
|
+
seconds.push(call.seconds);
|
|
81
|
+
result.costUsd += call.costUsd ?? 0;
|
|
82
|
+
result.calls.made++;
|
|
83
|
+
if (call.verdict)
|
|
84
|
+
result.calls.valid++;
|
|
85
|
+
if (call.verdict)
|
|
86
|
+
add(tier.score, row, call.verdict, call.verdict.confidence >= (tiers[at]?.below ?? 1));
|
|
87
|
+
else
|
|
88
|
+
tier.score.turns++;
|
|
89
|
+
}
|
|
90
|
+
const decided = draft.checked?.verdict;
|
|
91
|
+
const flagged = draft.notice || draft.action === 'none' ? undefined : decided;
|
|
92
|
+
add(together, row, flagged && flags(flagged) ? flagged : undefined, !draft.unsure && !!decided);
|
|
93
|
+
if (draft.unsure)
|
|
94
|
+
result.unsure++;
|
|
95
|
+
actions[draft.action] = (actions[draft.action] ?? 0) + 1;
|
|
96
|
+
if (row.expect?.notice)
|
|
97
|
+
result.notices.expected++;
|
|
98
|
+
if (draft.notice) {
|
|
99
|
+
result.notices.dropped++;
|
|
100
|
+
if (!row.expect?.notice)
|
|
101
|
+
result.notices.wrong++;
|
|
102
|
+
}
|
|
103
|
+
if (row.expect?.covered) {
|
|
104
|
+
result.duplicates.expected++;
|
|
105
|
+
if (draft.action === 'covered')
|
|
106
|
+
result.duplicates.dropped++;
|
|
107
|
+
}
|
|
108
|
+
if (row.expect?.questions) {
|
|
109
|
+
result.grouped.expected++;
|
|
110
|
+
const cards = draft.checked?.cards ?? [];
|
|
111
|
+
if (cards.length === 1 && cards[0]?.questions.length === row.expect.questions)
|
|
112
|
+
result.grouped.right++;
|
|
113
|
+
}
|
|
114
|
+
if (row.expect?.main) {
|
|
115
|
+
result.main.expected++;
|
|
116
|
+
if (draft.action === 'main')
|
|
117
|
+
result.main.left++;
|
|
118
|
+
}
|
|
119
|
+
const links = linksOf(row.input.final);
|
|
120
|
+
if (links.length && (draft.action === 'cards' || draft.action === 'answer')) {
|
|
121
|
+
result.links.expected++;
|
|
122
|
+
if (draft.checked && links.every((link) => draft.checked?.links.includes(link)))
|
|
123
|
+
result.links.kept++;
|
|
124
|
+
}
|
|
125
|
+
for (const rule of draft.checked?.broken ?? [])
|
|
126
|
+
broken[rule] = (broken[rule] ?? 0) + 1;
|
|
127
|
+
});
|
|
128
|
+
result.seconds = { median: percentile(seconds, 0.5), p90: percentile(seconds, 0.9) };
|
|
129
|
+
return result;
|
|
130
|
+
}
|
|
131
|
+
export function passMark(score, p90, calls) {
|
|
132
|
+
const failed = [];
|
|
133
|
+
const share = (part, whole) => (whole ? part / whole : 0);
|
|
134
|
+
const needs = score.needs;
|
|
135
|
+
const answer = score.answer;
|
|
136
|
+
if (share(needs.caught, needs.caught + needs.missed) < 0.75)
|
|
137
|
+
failed.push('needs caught under 75%');
|
|
138
|
+
if (share(needs.wrong, needs.caught + needs.wrong) > 0.1)
|
|
139
|
+
failed.push('needs flags over 10% wrong');
|
|
140
|
+
if (share(answer.caught, answer.caught + answer.missed) < 0.85)
|
|
141
|
+
failed.push('answers caught under 85%');
|
|
142
|
+
if (share(answer.wrong, answer.caught + answer.wrong) > 0.25)
|
|
143
|
+
failed.push('answer flags over 25% wrong');
|
|
144
|
+
if (calls.made && share(calls.valid, calls.made) < 0.98)
|
|
145
|
+
failed.push('under 98% of calls valid');
|
|
146
|
+
if (p90 > 10)
|
|
147
|
+
failed.push('p90 over 10 seconds');
|
|
148
|
+
return failed;
|
|
149
|
+
}
|
|
150
|
+
export function fromV1(out) {
|
|
151
|
+
if (typeof out !== 'object' || out === null)
|
|
152
|
+
return undefined;
|
|
153
|
+
const o = out;
|
|
154
|
+
const kinds = ['question', 'step', 'answer', 'none'];
|
|
155
|
+
if (!kinds.includes(o.kind) || typeof o.confidence !== 'number')
|
|
156
|
+
return undefined;
|
|
157
|
+
const kind = o.kind;
|
|
158
|
+
const also = kinds.includes(o.also) && o.also !== 'none' ? o.also : null;
|
|
159
|
+
const title = typeof o.title === 'string' ? o.title : '';
|
|
160
|
+
const options = Array.isArray(o.options)
|
|
161
|
+
? o.options.flatMap((option) => typeof option === 'object' &&
|
|
162
|
+
option &&
|
|
163
|
+
typeof option.label === 'string'
|
|
164
|
+
? [{ label: option.label }]
|
|
165
|
+
: [])
|
|
166
|
+
: [];
|
|
167
|
+
const card = {
|
|
168
|
+
title,
|
|
169
|
+
summary: typeof o.summary === 'string' ? o.summary : '',
|
|
170
|
+
options,
|
|
171
|
+
recommended: typeof o.recommended === 'string' ? o.recommended : null,
|
|
172
|
+
questions: [],
|
|
173
|
+
steps: [],
|
|
174
|
+
};
|
|
175
|
+
return {
|
|
176
|
+
kind,
|
|
177
|
+
also,
|
|
178
|
+
confidence: o.confidence,
|
|
179
|
+
covered: false,
|
|
180
|
+
stakes: 'normal',
|
|
181
|
+
inTheirName: false,
|
|
182
|
+
shows: false,
|
|
183
|
+
cards: (kind === 'question' || kind === 'step') && title ? [card] : [],
|
|
184
|
+
answerTitle: kind === 'answer' || also === 'answer' ? title : '',
|
|
185
|
+
settles: [],
|
|
186
|
+
outcome: '',
|
|
187
|
+
};
|
|
188
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
const NOTICE_CHARS = 400;
|
|
2
|
+
const NOTICES = [
|
|
3
|
+
{ id: 'limit', pattern: /^You(?:'|’)ve (?:hit|reached) your (?:[\w-]+ ){0,3}limit\b/i },
|
|
4
|
+
{
|
|
5
|
+
id: 'limit',
|
|
6
|
+
pattern: /^(?:Claude (?:AI )?)?(?:usage|[\w-]+ ?hour|weekly|session) limit reached\b/i,
|
|
7
|
+
},
|
|
8
|
+
{ id: 'credit', pattern: /^Credit balance is too low\b/i },
|
|
9
|
+
{ id: 'error', pattern: /^API Error\b/ },
|
|
10
|
+
{ id: 'error', pattern: /^Prompt is too long\b/ },
|
|
11
|
+
{ id: 'interrupted', pattern: /^\[Request interrupted by user/ },
|
|
12
|
+
];
|
|
13
|
+
export function appNotice(final) {
|
|
14
|
+
const text = final.trim();
|
|
15
|
+
if (!text || text.length > NOTICE_CHARS || text.includes('\n\n'))
|
|
16
|
+
return null;
|
|
17
|
+
return NOTICES.find((notice) => notice.pattern.test(text))?.id ?? null;
|
|
18
|
+
}
|