@loksly/agent-sessions 0.0.0-stage → 0.2.1
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/LICENSE +21 -0
- package/README.md +119 -2
- package/dist/cli.d.ts +2 -0
- package/dist/cli.js +18 -0
- package/dist/craft.d.ts +14 -0
- package/dist/craft.js +41 -0
- package/dist/git.d.ts +52 -0
- package/dist/git.js +140 -0
- package/dist/hook.d.ts +24 -0
- package/dist/hook.js +194 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.js +10 -0
- package/dist/paths.d.ts +8 -0
- package/dist/paths.js +14 -0
- package/dist/protocol.d.ts +68 -0
- package/dist/protocol.js +36 -0
- package/dist/providers.d.ts +36 -0
- package/dist/providers.js +32 -0
- package/dist/reader.d.ts +19 -0
- package/dist/reader.js +54 -0
- package/dist/redact.d.ts +1 -0
- package/dist/redact.js +29 -0
- package/dist/select.d.ts +20 -0
- package/dist/select.js +36 -0
- package/dist/testsignal.d.ts +18 -0
- package/dist/testsignal.js +70 -0
- package/dist/touched.d.ts +8 -0
- package/dist/touched.js +31 -0
- package/package.json +56 -4
- package/schema/session-record.schema.json +104 -0
- package/src/cli.ts +18 -0
- package/src/craft.ts +39 -0
- package/src/git.ts +165 -0
- package/src/hook.ts +189 -0
- package/src/index.ts +10 -0
- package/src/paths.ts +16 -0
- package/src/protocol.ts +97 -0
- package/src/providers.ts +71 -0
- package/src/reader.ts +56 -0
- package/src/redact.ts +30 -0
- package/src/select.ts +38 -0
- package/src/testsignal.ts +62 -0
- package/src/touched.ts +29 -0
package/src/redact.ts
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Secrets that must not end up on a screen.
|
|
3
|
+
*
|
|
4
|
+
* `detail` carries the command as typed, and the screen may be a panel anyone
|
|
5
|
+
* walking by can read: a `curl` with a published calendar URL, a variable with
|
|
6
|
+
* a token or an `Authorization:` header would be shown in full. The record is
|
|
7
|
+
* also stored on disk, so the secret would outlive the command.
|
|
8
|
+
*
|
|
9
|
+
* The list is deliberately short and specific: hiding too much turns messages
|
|
10
|
+
* into noise and defeats the point, which is reading at a glance what each
|
|
11
|
+
* session is doing.
|
|
12
|
+
*/
|
|
13
|
+
const SECRETS: [RegExp, string][] = [
|
|
14
|
+
// Secret Google Calendar addresses and the like.
|
|
15
|
+
[/private-[0-9a-f]{16,}/gi, 'private-<hidden>'],
|
|
16
|
+
// Credentials inside a URL.
|
|
17
|
+
[/(https?:\/\/)[^\s/@]+:[^\s/@]+@/gi, '$1<hidden>@'],
|
|
18
|
+
// Headers and parameters with a telling name. `Authorization` consumes two
|
|
19
|
+
// words: with a single `\S+` it hid "Bearer" and left the token in sight,
|
|
20
|
+
// which is exactly the mistake this must not make.
|
|
21
|
+
[/\b(authorization)\s*:\s*(?:\S+[ \t]+)?\S+/gi, '$1: <hidden>'],
|
|
22
|
+
[/\bbearer[ \t]+\S+/gi, 'Bearer <hidden>'],
|
|
23
|
+
[/\b(api[-_]?key|access[-_]?token|token|secret|password|passwd|pwd)\b(\s*[=:]\s*)\S+/gi, '$1$2<hidden>'],
|
|
24
|
+
// Token formats recognisable at a glance.
|
|
25
|
+
[/\b(gh[pousr]_[A-Za-z0-9]{16,}|sk-[A-Za-z0-9-]{16,}|AKIA[0-9A-Z]{16})\b/g, '<hidden>'],
|
|
26
|
+
];
|
|
27
|
+
|
|
28
|
+
export function redact(text: string): string {
|
|
29
|
+
return SECRETS.reduce((acc, [re, to]) => acc.replace(re, to), text);
|
|
30
|
+
}
|
package/src/select.ts
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Generic choices any client makes over a set of sessions.
|
|
3
|
+
*/
|
|
4
|
+
import { CLAIMS, STATE_PRIORITY, type SessionRecord } from './protocol.ts';
|
|
5
|
+
|
|
6
|
+
type Timed = Pick<SessionRecord, 'state' | 'since' | 'updated'>;
|
|
7
|
+
|
|
8
|
+
/** Is this session asking for the operator? */
|
|
9
|
+
export function claims(r: Pick<SessionRecord, 'state'>): boolean {
|
|
10
|
+
return CLAIMS.has(r.state);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Drops sessions that no longer exist. There are two deadlines because the
|
|
15
|
+
* signals differ: an `active` session says it is working, so a silent one is
|
|
16
|
+
* dead soon; the others legitimately wait for the operator and deserve much
|
|
17
|
+
* more slack, but not eternity.
|
|
18
|
+
*/
|
|
19
|
+
export function pruneZombies<T extends Timed>(records: T[], now: number, zombieMs: number, staleMs: number): T[] {
|
|
20
|
+
return records.filter((r) => now - r.updated <= (r.state === 'active' ? zombieMs : staleMs));
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Display order: first the ones asking for you, by state priority and, within
|
|
25
|
+
* a state, the one waiting longest. Then the rest, most recent activity first.
|
|
26
|
+
*/
|
|
27
|
+
export function order<T extends Timed>(records: T[]): T[] {
|
|
28
|
+
return [...records].sort((a, b) => {
|
|
29
|
+
const ca = claims(a) ? 1 : 0;
|
|
30
|
+
const cb = claims(b) ? 1 : 0;
|
|
31
|
+
if (ca !== cb) return cb - ca;
|
|
32
|
+
const pa = STATE_PRIORITY[a.state];
|
|
33
|
+
const pb = STATE_PRIORITY[b.state];
|
|
34
|
+
if (pa !== pb) return pb - pa;
|
|
35
|
+
if (ca === 1) return a.since - b.since;
|
|
36
|
+
return b.updated - a.updated;
|
|
37
|
+
});
|
|
38
|
+
}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Did the tests fail? The hook checks after every shell command that looks
|
|
3
|
+
* like a test run, reading its output. Pure and cheap: it is on the path of
|
|
4
|
+
* every tool call.
|
|
5
|
+
*
|
|
6
|
+
* It is a heuristic over text, not a verdict: when unclear it says nothing
|
|
7
|
+
* (null) and the previous value is kept.
|
|
8
|
+
*/
|
|
9
|
+
export const TEST_COMMAND = /\b(npm (run )?test|pnpm test|yarn test|pytest|vitest|jest|cargo test|go test|node --test|mvn test|gradle test|phpunit|rspec)\b/;
|
|
10
|
+
|
|
11
|
+
const FAIL = [
|
|
12
|
+
/\b[1-9]\d* (failed|failing|failures?|errors?)\b/i,
|
|
13
|
+
/^ℹ fail [1-9]/m,
|
|
14
|
+
/^not ok\b/m,
|
|
15
|
+
/\bFAIL(ED)?\b/,
|
|
16
|
+
/\bTests?:\s+[1-9]\d* failed/i,
|
|
17
|
+
/^✖ /m,
|
|
18
|
+
];
|
|
19
|
+
const OK = [/\b0 failed\b/i, /^ℹ fail 0\b/m, /\b\d+ passed\b/i, /\ball tests passed\b/i, /\bOK \(\d+ tests?\)/, /\btest result: ok\b/];
|
|
20
|
+
|
|
21
|
+
function text(response: unknown): string {
|
|
22
|
+
if (typeof response === 'string') return response;
|
|
23
|
+
if (response && typeof response === 'object') {
|
|
24
|
+
const o = response as Record<string, unknown>;
|
|
25
|
+
return [o.stdout, o.stderr, o.output, o.result].filter((v) => typeof v === 'string').join('\n') || JSON.stringify(response).slice(0, 50_000);
|
|
26
|
+
}
|
|
27
|
+
return '';
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** 'fail', 'ok', or null if this was not a test run or the output is unclear. */
|
|
31
|
+
export function testOutcome(event: string, tool: string, input: unknown, response: unknown): 'fail' | 'ok' | null {
|
|
32
|
+
if (event !== 'PostToolUse' || !/^(Bash|run_shell_command)$/.test(tool)) return null;
|
|
33
|
+
const cmd = input && typeof input === 'object' ? String((input as Record<string, unknown>).command ?? '') : '';
|
|
34
|
+
if (!TEST_COMMAND.test(cmd)) return null;
|
|
35
|
+
const r = response && typeof response === 'object' ? (response as Record<string, unknown>) : {};
|
|
36
|
+
const code = [r.exit_code, r.exitCode, r.returnCode, r.code].find((v) => typeof v === 'number') as number | undefined;
|
|
37
|
+
if (code !== undefined) return code === 0 ? 'ok' : 'fail';
|
|
38
|
+
const out = text(response).slice(-50_000);
|
|
39
|
+
if (!out) return null;
|
|
40
|
+
// "0 failed" wins over a stray FAIL inside a test name.
|
|
41
|
+
if (OK.some((re) => re.test(out)) && !/\b[1-9]\d* failed\b/i.test(out) && !/^ℹ fail [1-9]/m.test(out)) return 'ok';
|
|
42
|
+
if (FAIL.some((re) => re.test(out))) return 'fail';
|
|
43
|
+
return null;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Did the last shell command fail? Claude Code reports non-zero exits with the
|
|
48
|
+
* `PostToolUseFailure` event (it has to be in the hooks); Gemini and others
|
|
49
|
+
* carry the exit code in the response. A rebase with conflicts is also
|
|
50
|
+
* recognised by its text. null = it was not a shell command.
|
|
51
|
+
*/
|
|
52
|
+
export function commandFailed(event: string, tool: string, input: unknown, response: unknown): boolean | null {
|
|
53
|
+
if (!/^(Bash|run_shell_command)$/.test(tool)) return null;
|
|
54
|
+
if (event === 'PostToolUseFailure') return true;
|
|
55
|
+
if (event !== 'PostToolUse') return null;
|
|
56
|
+
const r = response && typeof response === 'object' ? (response as Record<string, unknown>) : {};
|
|
57
|
+
const code = [r.exit_code, r.exitCode, r.returnCode, r.code].find((v) => typeof v === 'number') as number | undefined;
|
|
58
|
+
if (code !== undefined) return code !== 0;
|
|
59
|
+
const cmd = input && typeof input === 'object' ? String((input as Record<string, unknown>).command ?? '') : '';
|
|
60
|
+
if (/\bgit\s+rebase\b/.test(cmd) && /\b(CONFLICT|could not apply|error: )/.test(text(response).slice(-20_000))) return true;
|
|
61
|
+
return false;
|
|
62
|
+
}
|
package/src/touched.ts
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Files a session has touched, so that a client can tell when two sessions
|
|
3
|
+
* step on each other. Called by the hook on every event, so it is pure and
|
|
4
|
+
* cheap: no I/O, it only merges the event into the previous list.
|
|
5
|
+
*/
|
|
6
|
+
import { isAbsolute, join } from 'node:path';
|
|
7
|
+
|
|
8
|
+
/** Cap of the list: enough to spot recent clashes without bloating the JSON. */
|
|
9
|
+
export const MAX_FILES = 20;
|
|
10
|
+
|
|
11
|
+
/** Tools that write, from Claude Code and Gemini CLI. */
|
|
12
|
+
const WRITERS = /^(edit|multiedit|write|notebookedit|write_file|replace|edit_file)$/i;
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* The list after this event, or the previous one unchanged. Emptied when a
|
|
16
|
+
* session starts: a new session has touched nothing yet, and inheriting the
|
|
17
|
+
* previous list would report phantom clashes.
|
|
18
|
+
*/
|
|
19
|
+
export function touchedFiles(prev: string[] | undefined, event: string, tool: string, input: unknown, cwd: string): string[] | undefined {
|
|
20
|
+
if (event === 'SessionStart') return undefined;
|
|
21
|
+
if (event !== 'PreToolUse' && event !== 'PostToolUse') return prev;
|
|
22
|
+
if (!WRITERS.test(tool)) return prev;
|
|
23
|
+
if (typeof input !== 'object' || input === null) return prev;
|
|
24
|
+
const o = input as Record<string, unknown>;
|
|
25
|
+
const raw = [o.file_path, o.notebook_path, o.path].find((v): v is string => typeof v === 'string' && v.length > 0);
|
|
26
|
+
if (!raw) return prev;
|
|
27
|
+
const file = isAbsolute(raw) ? raw : join(cwd, raw);
|
|
28
|
+
return [file, ...(prev ?? []).filter((f) => f !== file)].slice(0, MAX_FILES);
|
|
29
|
+
}
|