sideby 0.38.0 → 0.39.0
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/build.json +1 -1
- package/dist/src/cli/commands.d.ts +6 -0
- package/dist/src/cli/commands.js +88 -0
- package/dist/src/cli/json-schemas.js +35 -0
- package/dist/src/cli/main.js +32 -1
- package/dist/src/core/cli-commands.js +14 -2
- package/dist/src/core/config.d.ts +13 -0
- package/dist/src/core/config.js +18 -0
- package/dist/src/core/launch.d.ts +5 -0
- package/dist/src/core/launch.js +11 -1
- package/dist/src/core/network.d.ts +11 -1
- package/dist/src/core/network.js +21 -1
- package/dist/src/core/progress-state.d.ts +80 -0
- package/dist/src/core/progress-state.js +216 -0
- package/dist/src/core/progress.d.ts +36 -0
- package/dist/src/core/progress.js +77 -0
- package/dist/src/core/prompt-origin.d.ts +40 -0
- package/dist/src/core/prompt-origin.js +98 -0
- package/dist/src/core/search-catalog.js +25 -0
- package/dist/src/core/unattended-attended.d.ts +109 -0
- package/dist/src/core/unattended-attended.js +337 -0
- package/dist/src/core/unattended.d.ts +10 -2
- package/dist/src/core/unattended.js +11 -2
- package/dist/src/handoff/hook.js +19 -4
- package/dist/src/menubar/script.js +9 -3
- package/dist/src/network/hook.d.ts +9 -0
- package/dist/src/network/hook.js +186 -4
- package/dist/src/panel/handler.d.ts +3 -0
- package/dist/src/panel/handler.js +15 -0
- package/dist/src/panel/i18n.d.ts +18 -0
- package/dist/src/panel/i18n.js +36 -0
- package/dist/src/panel/page-script.js +67 -2
- package/dist/src/panel/page-styles.js +14 -0
- package/dist/src/progress/hook.d.ts +50 -0
- package/dist/src/progress/hook.js +251 -0
- package/dist/src/runtime.d.ts +9 -0
- package/dist/src/runtime.js +45 -2
- package/dist/src/unattended/hook.d.ts +5 -0
- package/dist/src/unattended/hook.js +103 -9
- package/package.json +1 -1
- package/schemas/config.json +13 -0
- package/schemas/progress.json +137 -0
- package/schemas/unattended.json +38 -0
- package/skills/sideby/SKILL.md +3 -1
- package/skills/sideby/references/network.md +1 -1
- package/skills/sideby/references/progress.md +25 -0
- package/skills/sideby/references/unattended.md +9 -1
|
@@ -0,0 +1,216 @@
|
|
|
1
|
+
// Progress Reminders' state (spec §3.42, ADR-0076): the config value the hook reads by hand, the two records each
|
|
2
|
+
// Claude session keeps under `<state>/progress/sessions/`, and whether a `Stop` ends a reminder's turn. A leaf module
|
|
3
|
+
// (Node built-ins and leaf modules only): `progress-hook` and `handoff-hook` import it.
|
|
4
|
+
//
|
|
5
|
+
// Every level of the folder must be a real folder of the user that nobody else can write to (`ownedFolder`). The
|
|
6
|
+
// records hold hashes, times and a random id only: never a command, a task's description, a reply or a prompt.
|
|
7
|
+
import { readdir, readFile, rm, stat } from 'node:fs/promises';
|
|
8
|
+
import { join } from 'node:path';
|
|
9
|
+
import { ownedFolder, readSmallFile, sha256, writeFileAtomic } from './fs-safe.js';
|
|
10
|
+
import { takeFileLock } from './lock.js';
|
|
11
|
+
/** Set in a session's environment: the interval at launch, used only when the config cannot be read. */
|
|
12
|
+
export const PROGRESS_ENV = 'SIDEBY_PROGRESS_EVERY';
|
|
13
|
+
/** The interval in whole minutes: at least this, at most `PROGRESS_MAX` (under the hook's five-hour timeout). */
|
|
14
|
+
export const PROGRESS_MIN = 1;
|
|
15
|
+
export const PROGRESS_MAX = 240;
|
|
16
|
+
/** What turning it on picks when no interval is given. */
|
|
17
|
+
export const PROGRESS_DEFAULT = 10;
|
|
18
|
+
/** The intervals the Panel offers. */
|
|
19
|
+
export const PROGRESS_CHOICES = [5, 10, 15, 30, 60];
|
|
20
|
+
/** The `timeout` of the `Stop` entry, in seconds (the same as Auto Handoff's IdleWatch). */
|
|
21
|
+
export const PROGRESS_STOP_TIMEOUT_S = 18_000;
|
|
22
|
+
/** The `timeout` of the `UserPromptSubmit` entry, in seconds. */
|
|
23
|
+
export const PROGRESS_PROMPT_TIMEOUT_S = 10;
|
|
24
|
+
const PARTS = ['progress', 'sessions'];
|
|
25
|
+
/** A record left unchanged this long is removed at the next write. */
|
|
26
|
+
const KEEP_MS = 24 * 3600_000;
|
|
27
|
+
const RECORD_MAX = 4096;
|
|
28
|
+
/** Whether `v` is an interval sideby takes. */
|
|
29
|
+
export const validEvery = (v) => typeof v === 'number' && Number.isInteger(v) && v >= PROGRESS_MIN && v <= PROGRESS_MAX;
|
|
30
|
+
const fromEnv = (env) => {
|
|
31
|
+
const raw = env[PROGRESS_ENV];
|
|
32
|
+
const n = raw === undefined || raw === '' ? Number.NaN : Number(raw);
|
|
33
|
+
return validEvery(n) ? n : undefined;
|
|
34
|
+
};
|
|
35
|
+
/**
|
|
36
|
+
* Config `progress.every`, read by hand (no schema) on every check: undefined means off. No config file means the
|
|
37
|
+
* defaults (off), as for every other config reader; only a file that exists but cannot be read or is not JSON falls
|
|
38
|
+
* back to the interval the session started with.
|
|
39
|
+
*/
|
|
40
|
+
export async function readProgressEvery(configFile, env) {
|
|
41
|
+
let text;
|
|
42
|
+
try {
|
|
43
|
+
// The config may be a link a dotfiles manager keeps; reading through it is fine.
|
|
44
|
+
text = await readFile(configFile, 'utf8');
|
|
45
|
+
}
|
|
46
|
+
catch (err) {
|
|
47
|
+
return err.code === 'ENOENT' ? undefined : fromEnv(env);
|
|
48
|
+
}
|
|
49
|
+
if (text.length > 4 * 1024 * 1024)
|
|
50
|
+
return fromEnv(env);
|
|
51
|
+
let config;
|
|
52
|
+
try {
|
|
53
|
+
config = JSON.parse(text);
|
|
54
|
+
}
|
|
55
|
+
catch {
|
|
56
|
+
return fromEnv(env);
|
|
57
|
+
}
|
|
58
|
+
const section = config && typeof config === 'object' ? config.progress : undefined;
|
|
59
|
+
const every = section && typeof section === 'object' ? section.every : undefined;
|
|
60
|
+
return validEvery(every) ? every : undefined;
|
|
61
|
+
}
|
|
62
|
+
/** The first 32 hex digits of a SHA-256: how a session id and a prompt id are kept. */
|
|
63
|
+
export const hashOf = (s) => sha256(s).slice(0, 32);
|
|
64
|
+
const str = (v) => (typeof v === 'string' && v ? v : undefined);
|
|
65
|
+
const isTime = (v) => typeof v === 'string' && Number.isFinite(Date.parse(v));
|
|
66
|
+
function parseJson(text) {
|
|
67
|
+
if (!text)
|
|
68
|
+
return undefined;
|
|
69
|
+
try {
|
|
70
|
+
const v = JSON.parse(text);
|
|
71
|
+
return v && typeof v === 'object' && !Array.isArray(v) ? v : undefined;
|
|
72
|
+
}
|
|
73
|
+
catch {
|
|
74
|
+
return undefined;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
function parseWatch(text) {
|
|
78
|
+
const v = parseJson(text);
|
|
79
|
+
const id = str(v?.id);
|
|
80
|
+
if (!v || !id || !isTime(v.since) || !isTime(v.at))
|
|
81
|
+
return undefined;
|
|
82
|
+
return { id, since: v.since, at: v.at, ...(isTime(v.wokeAt) ? { wokeAt: v.wokeAt } : {}) };
|
|
83
|
+
}
|
|
84
|
+
function parsePrompt(text) {
|
|
85
|
+
const v = parseJson(text);
|
|
86
|
+
const prompt = str(v?.prompt);
|
|
87
|
+
if (!v || !prompt || !isTime(v.at))
|
|
88
|
+
return undefined;
|
|
89
|
+
return { at: v.at, prompt, reminder: v.reminder === true };
|
|
90
|
+
}
|
|
91
|
+
const watchName = (sessionId) => `${hashOf(sessionId)}.json`;
|
|
92
|
+
const promptName = (sessionId) => `${hashOf(sessionId)}.prompt.json`;
|
|
93
|
+
/**
|
|
94
|
+
* Every write of a session's two records, and their removal, holds the session's lock (`<hash>.lock` beside them),
|
|
95
|
+
* so no check-then-write of one caller can be undone by another. A lock still held after `LOCK_WAIT` is given up on:
|
|
96
|
+
* nothing is written (a dead holder frees it at once; one that stopped touching it, after `staleMs`). The wait stays
|
|
97
|
+
* under the 10 seconds Claude Code gives the `UserPromptSubmit` entry.
|
|
98
|
+
*/
|
|
99
|
+
const LOCK = { staleMs: 15_000, beatMs: 5_000 };
|
|
100
|
+
const LOCK_WAIT = { timeoutMs: 8_000, pollMs: 25 };
|
|
101
|
+
/** Runs `fn` holding the lock of the session whose records start with `hash`; undefined when the lock is not had. */
|
|
102
|
+
async function underLock(dir, hash, wait, fn) {
|
|
103
|
+
const release = await takeFileLock(join(dir, `${hash}.lock`), wait ? { ...LOCK, wait } : LOCK).catch(() => null);
|
|
104
|
+
if (!release)
|
|
105
|
+
return undefined;
|
|
106
|
+
try {
|
|
107
|
+
return { value: await fn() };
|
|
108
|
+
}
|
|
109
|
+
finally {
|
|
110
|
+
await release();
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
/** `<state>/progress/sessions`, checked one level at a time; null when it is missing (without `create`) or not safe. */
|
|
114
|
+
async function folder(stateDir, create) {
|
|
115
|
+
if (!create)
|
|
116
|
+
return ownedFolder(stateDir, PARTS, { create: false }).catch(() => null);
|
|
117
|
+
return ownedFolder(stateDir, PARTS, { create: true, private: true });
|
|
118
|
+
}
|
|
119
|
+
export async function readWatch(stateDir, sessionId) {
|
|
120
|
+
const dir = await folder(stateDir, false);
|
|
121
|
+
return dir ? parseWatch(await readSmallFile(join(dir, watchName(sessionId)), RECORD_MAX)) : undefined;
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* Replaces the session's watch (file 600, atomically) under the session's lock, so it never lands between another
|
|
125
|
+
* watch's last check and its `wokeAt`. False when the lock stays held: then nothing is written and the caller must not
|
|
126
|
+
* act as the session's watch. Throws when the folder is not safe to use.
|
|
127
|
+
*/
|
|
128
|
+
export async function writeWatch(stateDir, sessionId, record, opts = {}) {
|
|
129
|
+
const dir = (await folder(stateDir, true));
|
|
130
|
+
const done = await underLock(dir, hashOf(sessionId), opts.lockWait ?? LOCK_WAIT, () => writeFileAtomic(join(dir, watchName(sessionId)), `${JSON.stringify(record)}\n`, 0o600));
|
|
131
|
+
return done !== undefined;
|
|
132
|
+
}
|
|
133
|
+
/**
|
|
134
|
+
* Marks the watch `id` woken at `at`, only while the session's record is still that watch: the check and the write
|
|
135
|
+
* hold the session's lock, so a later `Stop` replaces the record before or after, never in between. False when the
|
|
136
|
+
* record is another watch's, is missing, or the lock stays held (`beforeWrite` runs inside the lock, for tests).
|
|
137
|
+
*/
|
|
138
|
+
export async function markWatchWoken(stateDir, sessionId, id, at, deps = {}) {
|
|
139
|
+
const dir = await folder(stateDir, false);
|
|
140
|
+
if (!dir)
|
|
141
|
+
return false;
|
|
142
|
+
const done = await underLock(dir, hashOf(sessionId), LOCK_WAIT, async () => {
|
|
143
|
+
const file = join(dir, watchName(sessionId));
|
|
144
|
+
const fresh = parseWatch(await readSmallFile(file, RECORD_MAX));
|
|
145
|
+
if (fresh?.id !== id)
|
|
146
|
+
return false;
|
|
147
|
+
await deps.beforeWrite?.();
|
|
148
|
+
await writeFileAtomic(file, `${JSON.stringify({ ...fresh, wokeAt: at.toISOString() })}\n`, 0o600);
|
|
149
|
+
return true;
|
|
150
|
+
});
|
|
151
|
+
return done?.value === true;
|
|
152
|
+
}
|
|
153
|
+
export async function readPromptRecord(stateDir, sessionId) {
|
|
154
|
+
const dir = await folder(stateDir, false);
|
|
155
|
+
return dir ? parsePrompt(await readSmallFile(join(dir, promptName(sessionId)), RECORD_MAX)) : undefined;
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* Notes the session's latest prompt under the session's lock, unless the record already holds a later one: two
|
|
159
|
+
* background hooks may finish out of order, and an older prompt never replaces a newer one (`at` is when the hook
|
|
160
|
+
* started, so it follows the order the prompts came in). The same prompt noted again at the same time or later
|
|
161
|
+
* replaces it. Then removes records nobody changed for a day. False when the lock stays held or a later prompt is kept.
|
|
162
|
+
* `beforeRemove` runs with the file's name before each removal takes its lock (for tests).
|
|
163
|
+
*/
|
|
164
|
+
export async function writePromptRecord(stateDir, sessionId, record, now, opts = {}) {
|
|
165
|
+
const dir = (await folder(stateDir, true));
|
|
166
|
+
const done = await underLock(dir, hashOf(sessionId), opts.lockWait ?? LOCK_WAIT, async () => {
|
|
167
|
+
const file = join(dir, promptName(sessionId));
|
|
168
|
+
const old = parsePrompt(await readSmallFile(file, RECORD_MAX));
|
|
169
|
+
const t = Date.parse(record.at);
|
|
170
|
+
if (old && (old.prompt === record.prompt ? Date.parse(old.at) > t : Date.parse(old.at) >= t))
|
|
171
|
+
return false;
|
|
172
|
+
await writeFileAtomic(file, `${JSON.stringify(record)}\n`, 0o600);
|
|
173
|
+
return true;
|
|
174
|
+
});
|
|
175
|
+
await pruneRecords(dir, now, opts.beforeRemove);
|
|
176
|
+
return done?.value === true;
|
|
177
|
+
}
|
|
178
|
+
/**
|
|
179
|
+
* Removes records nobody changed for a day. Each is checked again and removed under its session's lock, taken without
|
|
180
|
+
* waiting (a held one is left for next time), so a record its session rewrote meanwhile is never removed.
|
|
181
|
+
*/
|
|
182
|
+
async function pruneRecords(dir, now, beforeRemove) {
|
|
183
|
+
const old = async (file) => {
|
|
184
|
+
const st = await stat(file).catch(() => null);
|
|
185
|
+
return Boolean(st && now.getTime() - st.mtimeMs > KEEP_MS);
|
|
186
|
+
};
|
|
187
|
+
for (const name of await readdir(dir).catch(() => [])) {
|
|
188
|
+
const hash = /^([0-9a-f]{32})\.(?:prompt\.)?json$/.exec(name)?.[1];
|
|
189
|
+
if (!hash)
|
|
190
|
+
continue;
|
|
191
|
+
const file = join(dir, name);
|
|
192
|
+
if (!(await old(file)))
|
|
193
|
+
continue;
|
|
194
|
+
await beforeRemove?.(name);
|
|
195
|
+
await underLock(dir, hash, null, async () => {
|
|
196
|
+
if (await old(file))
|
|
197
|
+
await rm(file, { force: true }).catch(() => { });
|
|
198
|
+
});
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
/**
|
|
202
|
+
* Whether the `Stop` that carries `promptId` ends the turn a Progress Reminder started: the session's latest prompt
|
|
203
|
+
* has that id and was a reminder. A record that is missing, late or for another prompt says no, so a caller that
|
|
204
|
+
* cannot tell treats the turn as an ordinary one.
|
|
205
|
+
*/
|
|
206
|
+
export async function progressReminderTurn(stateDir, sessionId, promptId) {
|
|
207
|
+
if (!sessionId || !promptId)
|
|
208
|
+
return false;
|
|
209
|
+
try {
|
|
210
|
+
const r = await readPromptRecord(stateDir, sessionId);
|
|
211
|
+
return Boolean(r?.reminder && r.prompt === hashOf(promptId));
|
|
212
|
+
}
|
|
213
|
+
catch {
|
|
214
|
+
return false;
|
|
215
|
+
}
|
|
216
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import type { Env } from '../types.ts';
|
|
2
|
+
import { type Config } from './config.ts';
|
|
3
|
+
import { type SessionHookPart } from './session-hooks.ts';
|
|
4
|
+
/** Families whose sessions get reminders: only Claude Code lets a hook wake an idle session. */
|
|
5
|
+
export declare const PROGRESS_FAMILIES: readonly string[];
|
|
6
|
+
/**
|
|
7
|
+
* The session hooks of Progress Reminders: in the background after every turn (`Stop`, `asyncRewake`, so its exit 2
|
|
8
|
+
* wakes the idle session) and on every prompt (`UserPromptSubmit`, `async`, so it never holds a prompt back). Codex
|
|
9
|
+
* has no hook that can wake a session, so it gets none.
|
|
10
|
+
*/
|
|
11
|
+
export declare function progressHookPart(): SessionHookPart;
|
|
12
|
+
/** The line a Claude Launch prints when the user's own `--settings` leaves no room for the hooks. */
|
|
13
|
+
export declare function progressSettingsLine(env: Env): string;
|
|
14
|
+
/** What `sideby progress` and the Panel show. */
|
|
15
|
+
export interface ProgressView {
|
|
16
|
+
on: boolean;
|
|
17
|
+
/** Minutes between reminders; absent while off. */
|
|
18
|
+
every?: number;
|
|
19
|
+
/** What turning it on picks. */
|
|
20
|
+
defaultEvery: number;
|
|
21
|
+
/** The intervals the Panel offers. */
|
|
22
|
+
choices: number[];
|
|
23
|
+
/** The Families whose sessions get reminders. */
|
|
24
|
+
families: string[];
|
|
25
|
+
}
|
|
26
|
+
export declare function progressView(config: Config): ProgressView;
|
|
27
|
+
/**
|
|
28
|
+
* Turns Progress Reminders on with `every` minutes, or off (undefined). Writes only sideby's config, so it applies at
|
|
29
|
+
* once with no preview; the caller prints the undo.
|
|
30
|
+
*/
|
|
31
|
+
export declare function setProgress(configFile: string, every: number | undefined): Promise<{
|
|
32
|
+
changed: boolean;
|
|
33
|
+
before?: number;
|
|
34
|
+
after?: number;
|
|
35
|
+
config: Config['progress'];
|
|
36
|
+
}>;
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { editConfigProgress } from './config.js';
|
|
2
|
+
import { PROGRESS_CHOICES, PROGRESS_DEFAULT, PROGRESS_PROMPT_TIMEOUT_S, PROGRESS_STOP_TIMEOUT_S, validEvery, } from './progress-state.js';
|
|
3
|
+
import { sidebyHookCommand } from './session-hooks.js';
|
|
4
|
+
/** Families whose sessions get reminders: only Claude Code lets a hook wake an idle session. */
|
|
5
|
+
export const PROGRESS_FAMILIES = ['claude'];
|
|
6
|
+
/**
|
|
7
|
+
* The session hooks of Progress Reminders: in the background after every turn (`Stop`, `asyncRewake`, so its exit 2
|
|
8
|
+
* wakes the idle session) and on every prompt (`UserPromptSubmit`, `async`, so it never holds a prompt back). Codex
|
|
9
|
+
* has no hook that can wake a session, so it gets none.
|
|
10
|
+
*/
|
|
11
|
+
export function progressHookPart() {
|
|
12
|
+
const command = (event) => sidebyHookCommand('progress-hook', 'claude', event);
|
|
13
|
+
return {
|
|
14
|
+
id: 'progress',
|
|
15
|
+
claude: {
|
|
16
|
+
Stop: [
|
|
17
|
+
{
|
|
18
|
+
hooks: [
|
|
19
|
+
{
|
|
20
|
+
type: 'command',
|
|
21
|
+
command: command('Stop'),
|
|
22
|
+
asyncRewake: true,
|
|
23
|
+
timeout: PROGRESS_STOP_TIMEOUT_S,
|
|
24
|
+
},
|
|
25
|
+
],
|
|
26
|
+
},
|
|
27
|
+
],
|
|
28
|
+
UserPromptSubmit: [
|
|
29
|
+
{
|
|
30
|
+
hooks: [
|
|
31
|
+
{
|
|
32
|
+
type: 'command',
|
|
33
|
+
command: command('UserPromptSubmit'),
|
|
34
|
+
async: true,
|
|
35
|
+
timeout: PROGRESS_PROMPT_TIMEOUT_S,
|
|
36
|
+
},
|
|
37
|
+
],
|
|
38
|
+
},
|
|
39
|
+
],
|
|
40
|
+
},
|
|
41
|
+
codex: {},
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
function prefersChinese(env) {
|
|
45
|
+
return /^zh/i.test(env.LC_ALL || env.LC_MESSAGES || env.LANG || '');
|
|
46
|
+
}
|
|
47
|
+
/** The line a Claude Launch prints when the user's own `--settings` leaves no room for the hooks. */
|
|
48
|
+
export function progressSettingsLine(env) {
|
|
49
|
+
return prefersChinese(env)
|
|
50
|
+
? 'sideby:已经给了 `--settings`,这个会话没有进度提醒(它的 hook 要通过 `--settings` 加入)。'
|
|
51
|
+
: 'sideby: `--settings` is already given, so this session gets no progress reminders (their hooks go in through `--settings`).';
|
|
52
|
+
}
|
|
53
|
+
export function progressView(config) {
|
|
54
|
+
const every = config.progress?.every;
|
|
55
|
+
return {
|
|
56
|
+
on: validEvery(every),
|
|
57
|
+
...(validEvery(every) ? { every } : {}),
|
|
58
|
+
defaultEvery: PROGRESS_DEFAULT,
|
|
59
|
+
choices: [...PROGRESS_CHOICES],
|
|
60
|
+
families: [...PROGRESS_FAMILIES],
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Turns Progress Reminders on with `every` minutes, or off (undefined). Writes only sideby's config, so it applies at
|
|
65
|
+
* once with no preview; the caller prints the undo.
|
|
66
|
+
*/
|
|
67
|
+
export async function setProgress(configFile, every) {
|
|
68
|
+
const r = await editConfigProgress(configFile, every);
|
|
69
|
+
const before = r.before?.every;
|
|
70
|
+
const after = r.after?.every;
|
|
71
|
+
return {
|
|
72
|
+
changed: r.changed,
|
|
73
|
+
...(validEvery(before) ? { before } : {}),
|
|
74
|
+
...(validEvery(after) ? { after } : {}),
|
|
75
|
+
config: r.after,
|
|
76
|
+
};
|
|
77
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/** A prompt Claude Code made itself starts with this (a background task's notification, a hook's wake-up). */
|
|
2
|
+
export declare const HOST_MADE = "<task-notification>";
|
|
3
|
+
/** Whether Claude Code made the prompt itself (checked with Claude Code 2.1.325). */
|
|
4
|
+
export declare const hostMade: (prompt: string) => boolean;
|
|
5
|
+
/** Whether the prompt is a background hook's wake-up, which Claude Code made itself. */
|
|
6
|
+
export declare const hookWakeUp: (prompt: string) => boolean;
|
|
7
|
+
/** How every Progress Reminder starts (spec §3.42, ADR-0076); the model reads it, so it is plain words. */
|
|
8
|
+
export declare const PROGRESS_MARK = "sideby progress reminder:";
|
|
9
|
+
/** The tags around a message Network Guard's `Held` held back and hands over later (spec §3.35, network/hook.ts). */
|
|
10
|
+
export declare const HELD_OPEN = "<sideby-held-message>";
|
|
11
|
+
export declare const HELD_CLOSE = "</sideby-held-message>";
|
|
12
|
+
/**
|
|
13
|
+
* Whether the prompt is the wake-up of a Progress Reminder and nothing else: Claude Code's wrapper, the reminder's
|
|
14
|
+
* opening words, and only one hook's message (two wake-ups at once might be joined into one prompt). A reminder that
|
|
15
|
+
* Network Guard held and handed over later counts too: `Held`'s own wake-up with one held message between the tags,
|
|
16
|
+
* which is the reminder's wake-up alone.
|
|
17
|
+
*/
|
|
18
|
+
export declare function progressWakeUp(prompt: string): boolean;
|
|
19
|
+
/** Whether the prompt is, or points to, a Message `sideby send` kept. */
|
|
20
|
+
export declare const sidebyMessage: (prompt: string) => boolean;
|
|
21
|
+
/**
|
|
22
|
+
* Set in the environment of a Claude session that has `unattended-hook`: the marks of the prompts sideby put on its
|
|
23
|
+
* command line (`launchPromptMarks`). `?` until the Host starts, and for a command line with more than `MARKS_MAX`
|
|
24
|
+
* of them; a session whose value is not a list counts no prompt as typed.
|
|
25
|
+
*/
|
|
26
|
+
export declare const LAUNCH_PROMPTS_ENV = "SIDEBY_LAUNCH_PROMPTS";
|
|
27
|
+
/** The value before the final arguments are known, or when there are too many to list. */
|
|
28
|
+
export declare const LAUNCH_PROMPTS_UNSET = "?";
|
|
29
|
+
/**
|
|
30
|
+
* The marks of every argument that may be a prompt: each one that does not start with `-`, the value of each
|
|
31
|
+
* `--x=value`, and every argument after a `--`, whole (there even `- review` or `--x=y` is prompt text). Over-counting
|
|
32
|
+
* an option's value is harmless: a typed prompt equal to it is only not counted. More than `MARKS_MAX` of them give
|
|
33
|
+
* `LAUNCH_PROMPTS_UNSET`: a list cut short would let a launch prompt past it count as typed.
|
|
34
|
+
*/
|
|
35
|
+
export declare function launchPromptMarks(args: readonly string[]): string;
|
|
36
|
+
/**
|
|
37
|
+
* Whether `prompt` may be one sideby put on the command line, read from the session's environment. Undefined, `?` or
|
|
38
|
+
* anything that is not a list of marks means sideby cannot tell, and the answer is yes.
|
|
39
|
+
*/
|
|
40
|
+
export declare function launchPrompt(prompt: string, env: Record<string, string | undefined>): boolean;
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
// What sideby can tell about who wrote a prompt from the prompt alone (spec §3.35, §3.37 "有人在的会话", ADR-0075): a
|
|
2
|
+
// prompt Claude Code made itself, a Message `sideby send` delivered, and a prompt sideby passed on the Host's command
|
|
3
|
+
// line. A leaf module (Node built-ins only): `network-hook` and `unattended-hook` run it before every prompt.
|
|
4
|
+
import { createHash } from 'node:crypto';
|
|
5
|
+
/** A prompt Claude Code made itself starts with this (a background task's notification, a hook's wake-up). */
|
|
6
|
+
export const HOST_MADE = '<task-notification>';
|
|
7
|
+
/** Whether Claude Code made the prompt itself (checked with Claude Code 2.1.325). */
|
|
8
|
+
export const hostMade = (prompt) => prompt.trimStart().startsWith(HOST_MADE);
|
|
9
|
+
/**
|
|
10
|
+
* How Claude Code wraps a background hook's message when its exit 2 wakes the session (`asyncRewake`): `<task-notification>
|
|
11
|
+
* <summary>Stop hook feedback</summary> </task-notification> <system-reminder> Stop hook blocking error from command
|
|
12
|
+
* "Stop": <stderr> …` (Claude Code 2.1.325).
|
|
13
|
+
*/
|
|
14
|
+
const WAKE_UP = /^<task-notification>\s*<summary>Stop hook feedback<\/summary>/;
|
|
15
|
+
/** Whether the prompt is a background hook's wake-up, which Claude Code made itself. */
|
|
16
|
+
export const hookWakeUp = (prompt) => WAKE_UP.test(prompt.trimStart());
|
|
17
|
+
/** How every Progress Reminder starts (spec §3.42, ADR-0076); the model reads it, so it is plain words. */
|
|
18
|
+
export const PROGRESS_MARK = 'sideby progress reminder:';
|
|
19
|
+
/** The tags around a message Network Guard's `Held` held back and hands over later (spec §3.35, network/hook.ts). */
|
|
20
|
+
export const HELD_OPEN = '<sideby-held-message>';
|
|
21
|
+
export const HELD_CLOSE = '</sideby-held-message>';
|
|
22
|
+
/** One background hook's wake-up: Claude Code's wrapper and one hook's message (two at once may be joined). */
|
|
23
|
+
const oneWakeUp = (prompt) => hookWakeUp(prompt) &&
|
|
24
|
+
prompt.split('Stop hook blocking error').length <= 2 &&
|
|
25
|
+
prompt.split(HOST_MADE).length === 2;
|
|
26
|
+
/**
|
|
27
|
+
* Whether the prompt is the wake-up of a Progress Reminder and nothing else: Claude Code's wrapper, the reminder's
|
|
28
|
+
* opening words, and only one hook's message (two wake-ups at once might be joined into one prompt). A reminder that
|
|
29
|
+
* Network Guard held and handed over later counts too: `Held`'s own wake-up with one held message between the tags,
|
|
30
|
+
* which is the reminder's wake-up alone.
|
|
31
|
+
*/
|
|
32
|
+
export function progressWakeUp(prompt) {
|
|
33
|
+
const from = prompt.indexOf(HELD_OPEN);
|
|
34
|
+
const to = prompt.indexOf(HELD_CLOSE);
|
|
35
|
+
if (from === -1 && to === -1)
|
|
36
|
+
return oneWakeUp(prompt) && prompt.includes(PROGRESS_MARK);
|
|
37
|
+
if (from === -1 || to < from)
|
|
38
|
+
return false;
|
|
39
|
+
if (prompt.indexOf(HELD_OPEN, from + 1) !== -1 || prompt.indexOf(HELD_CLOSE, to + 1) !== -1)
|
|
40
|
+
return false;
|
|
41
|
+
const held = prompt.slice(from + HELD_OPEN.length, to).trim();
|
|
42
|
+
const outer = prompt.slice(0, from) + prompt.slice(to + HELD_CLOSE.length);
|
|
43
|
+
return oneWakeUp(outer) && !outer.includes(PROGRESS_MARK) && oneWakeUp(held) && held.includes(PROGRESS_MARK);
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* The mark every Message `sideby send` delivers, and the line it asks the user to paste, carries (`sideby message
|
|
47
|
+
* <id>`, an id of 8 hex digits; core/messages.ts).
|
|
48
|
+
*/
|
|
49
|
+
const SIDEBY_MESSAGE = /\bsideby message [0-9a-f]{8}\b/;
|
|
50
|
+
/** Whether the prompt is, or points to, a Message `sideby send` kept. */
|
|
51
|
+
export const sidebyMessage = (prompt) => SIDEBY_MESSAGE.test(prompt);
|
|
52
|
+
/**
|
|
53
|
+
* Set in the environment of a Claude session that has `unattended-hook`: the marks of the prompts sideby put on its
|
|
54
|
+
* command line (`launchPromptMarks`). `?` until the Host starts, and for a command line with more than `MARKS_MAX`
|
|
55
|
+
* of them; a session whose value is not a list counts no prompt as typed.
|
|
56
|
+
*/
|
|
57
|
+
export const LAUNCH_PROMPTS_ENV = 'SIDEBY_LAUNCH_PROMPTS';
|
|
58
|
+
/** The value before the final arguments are known, or when there are too many to list. */
|
|
59
|
+
export const LAUNCH_PROMPTS_UNSET = '?';
|
|
60
|
+
const MARKS_MAX = 64;
|
|
61
|
+
const MARK = /^[0-9a-f]{16}$/;
|
|
62
|
+
/** A prompt as compared: trimmed, with every run of white space as one space, so a Host that tidies it still matches. */
|
|
63
|
+
const mark = (text) => createHash('sha256').update(text.trim().replace(/\s+/g, ' ')).digest('hex').slice(0, 16);
|
|
64
|
+
/**
|
|
65
|
+
* The marks of every argument that may be a prompt: each one that does not start with `-`, the value of each
|
|
66
|
+
* `--x=value`, and every argument after a `--`, whole (there even `- review` or `--x=y` is prompt text). Over-counting
|
|
67
|
+
* an option's value is harmless: a typed prompt equal to it is only not counted. More than `MARKS_MAX` of them give
|
|
68
|
+
* `LAUNCH_PROMPTS_UNSET`: a list cut short would let a launch prompt past it count as typed.
|
|
69
|
+
*/
|
|
70
|
+
export function launchPromptMarks(args) {
|
|
71
|
+
const out = new Set();
|
|
72
|
+
let dashes = false;
|
|
73
|
+
for (const a of args) {
|
|
74
|
+
if (!dashes && a === '--') {
|
|
75
|
+
dashes = true;
|
|
76
|
+
continue;
|
|
77
|
+
}
|
|
78
|
+
const value = dashes || !a.startsWith('-') ? a : a.includes('=') ? a.slice(a.indexOf('=') + 1) : '';
|
|
79
|
+
if (value.trim())
|
|
80
|
+
out.add(mark(value));
|
|
81
|
+
if (out.size > MARKS_MAX)
|
|
82
|
+
return LAUNCH_PROMPTS_UNSET;
|
|
83
|
+
}
|
|
84
|
+
return [...out].join(',');
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Whether `prompt` may be one sideby put on the command line, read from the session's environment. Undefined, `?` or
|
|
88
|
+
* anything that is not a list of marks means sideby cannot tell, and the answer is yes.
|
|
89
|
+
*/
|
|
90
|
+
export function launchPrompt(prompt, env) {
|
|
91
|
+
const value = env[LAUNCH_PROMPTS_ENV];
|
|
92
|
+
if (value === undefined)
|
|
93
|
+
return true;
|
|
94
|
+
const marks = value === '' ? [] : value.split(',');
|
|
95
|
+
if (marks.length > MARKS_MAX || !marks.every((m) => MARK.test(m)))
|
|
96
|
+
return true;
|
|
97
|
+
return marks.includes(mark(prompt));
|
|
98
|
+
}
|
|
@@ -223,6 +223,31 @@ const ENTRIES = [
|
|
|
223
223
|
target: { settings: 'network', find: 'net' },
|
|
224
224
|
cliCommand: 'sideby network',
|
|
225
225
|
},
|
|
226
|
+
{
|
|
227
|
+
id: 'setting.progress',
|
|
228
|
+
kind: 'setting',
|
|
229
|
+
title: { en: 'Progress updates', zh: '进度提醒' },
|
|
230
|
+
synonyms: [
|
|
231
|
+
'progress',
|
|
232
|
+
'progress reminder',
|
|
233
|
+
'background',
|
|
234
|
+
'background task',
|
|
235
|
+
'waiting',
|
|
236
|
+
'subagent',
|
|
237
|
+
'heartbeat',
|
|
238
|
+
'silent',
|
|
239
|
+
'进度',
|
|
240
|
+
'后台',
|
|
241
|
+
'后台任务',
|
|
242
|
+
'等待',
|
|
243
|
+
'子任务',
|
|
244
|
+
'没反应',
|
|
245
|
+
'报进度',
|
|
246
|
+
],
|
|
247
|
+
panel: at(SET, { en: 'Progress updates', zh: '进度提醒' }),
|
|
248
|
+
target: { settings: 'progress', find: 'progress' },
|
|
249
|
+
cliCommand: 'sideby progress',
|
|
250
|
+
},
|
|
226
251
|
{
|
|
227
252
|
id: 'setting.skill',
|
|
228
253
|
kind: 'setting',
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
import { type UnattendedState } from './unattended-state.ts';
|
|
2
|
+
/** How long a session counts as attended after each prompt someone typed into it. */
|
|
3
|
+
export declare const ATTENDED_MS: number;
|
|
4
|
+
/** The `timeout` of the hook entry: the longest wait for the transcript (`ORIGIN_WAIT_MS`) with room to spare. */
|
|
5
|
+
export declare const ATTENDED_HOOK_TIMEOUT_S = 30;
|
|
6
|
+
/** How long the hook waits for Claude Code to write the prompt into the transcript (70–180 ms with Claude Code 2.1.325). */
|
|
7
|
+
export declare const ORIGIN_WAIT_MS = 10000;
|
|
8
|
+
/** One Attended Session as kept on disk. */
|
|
9
|
+
export interface AttendedRecord {
|
|
10
|
+
sessionId: string;
|
|
11
|
+
family: string;
|
|
12
|
+
account?: string;
|
|
13
|
+
cwd?: string;
|
|
14
|
+
/** The last prompt someone typed. */
|
|
15
|
+
at: string;
|
|
16
|
+
/** When this stretch of being attended began: `at` of the first prompt after the last one ran out. */
|
|
17
|
+
since: string;
|
|
18
|
+
/** When the user was told in the session, during this stretch. */
|
|
19
|
+
toldAt?: string;
|
|
20
|
+
}
|
|
21
|
+
/** An Attended Session as `sideby unattended --json` and the Panel show it. */
|
|
22
|
+
export interface AttendedSession {
|
|
23
|
+
sessionId: string;
|
|
24
|
+
family: string;
|
|
25
|
+
account?: string;
|
|
26
|
+
cwd?: string;
|
|
27
|
+
since: string;
|
|
28
|
+
/** When it stops counting as attended unless someone types again. */
|
|
29
|
+
until: string;
|
|
30
|
+
}
|
|
31
|
+
/** When the record stops counting, in ms; it counts while this is later than now. */
|
|
32
|
+
export declare const attendedEnd: (r: AttendedRecord) => number;
|
|
33
|
+
/** The session's record while it still counts at `now`; undefined otherwise or when the folder is not safe. */
|
|
34
|
+
export declare function readAttended(stateDir: string, sessionId: string, now: Date): Promise<AttendedRecord | undefined>;
|
|
35
|
+
/**
|
|
36
|
+
* Notes a prompt someone typed into the session at `at`: it counts as attended for `ATTENDED_MS` from then. A stretch
|
|
37
|
+
* that was still running goes on (its `since` and `toldAt` are kept); otherwise a new one starts. A prompt no later than
|
|
38
|
+
* the one already noted changes nothing. Removes records that ran out over an hour ago. Throws when the folder is not
|
|
39
|
+
* safe to use, or when the session's record stays locked for 5 seconds.
|
|
40
|
+
*/
|
|
41
|
+
export declare function markAttended(stateDir: string, who: {
|
|
42
|
+
sessionId: string;
|
|
43
|
+
family: string;
|
|
44
|
+
account?: string;
|
|
45
|
+
cwd?: string;
|
|
46
|
+
}, at: Date): Promise<AttendedRecord>;
|
|
47
|
+
/**
|
|
48
|
+
* Notes that the user was told in the session during the stretch that began at `since`. Best effort: a record that
|
|
49
|
+
* stays locked is left as it is. `beforeWrite` runs between the read and the write (for tests).
|
|
50
|
+
*/
|
|
51
|
+
export declare function markTold(stateDir: string, sessionId: string, since: string, now: Date, deps?: {
|
|
52
|
+
beforeWrite?: () => Promise<void>;
|
|
53
|
+
}): Promise<void>;
|
|
54
|
+
/**
|
|
55
|
+
* Every session that counts as attended at `now`, latest end first. `prune` also removes long-expired records;
|
|
56
|
+
* `beforeRemove` runs before each removal takes its lock (for tests).
|
|
57
|
+
*/
|
|
58
|
+
export declare function listAttended(stateDir: string, now: Date, opts?: {
|
|
59
|
+
prune?: boolean;
|
|
60
|
+
beforeRemove?: () => Promise<void>;
|
|
61
|
+
}): Promise<AttendedSession[]>;
|
|
62
|
+
/**
|
|
63
|
+
* Whether `transcript` is the file where Claude Code keeps session `sessionId`'s transcript:
|
|
64
|
+
* `<config dir>/projects/<folder>/<session id>.jsonl`, the config dir being `CLAUDE_CONFIG_DIR` or `~/.claude`, compared
|
|
65
|
+
* after resolving links. A hook input that names any other file is not believed, so a made-up transcript elsewhere marks
|
|
66
|
+
* nothing.
|
|
67
|
+
*/
|
|
68
|
+
export declare function ownTranscript(transcript: string, sessionId: string, env: {
|
|
69
|
+
HOME?: string | undefined;
|
|
70
|
+
CLAUDE_CONFIG_DIR?: string | undefined;
|
|
71
|
+
}): Promise<boolean>;
|
|
72
|
+
/** What a session's transcript says of one prompt. Its text is only for checking, and is never kept. */
|
|
73
|
+
export interface PromptLine {
|
|
74
|
+
/** `human` for a prompt a person typed; `other` for anything else that has an origin. */
|
|
75
|
+
origin: 'human' | 'other';
|
|
76
|
+
/** When Claude Code wrote the line (its `timestamp`, in ms); missing when it is not a time. */
|
|
77
|
+
at?: number;
|
|
78
|
+
/** The prompt as the transcript holds it (a string, or its text blocks joined); missing when it is neither. */
|
|
79
|
+
text?: string;
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* The transcript line of prompt `promptId`: Claude Code writes a `user` line with the prompt's `promptId`, its text, a
|
|
83
|
+
* `timestamp` and an `origin` (`human` for a typed prompt; `task-notification`, `peer` for a Message from another
|
|
84
|
+
* process or session; none under `-p`; checked with Claude Code 2.1.325). The line is written shortly after the hook
|
|
85
|
+
* starts, so the transcript is read from the start of the line `ORIGIN_BACK_BYTES` before its end at the start, then every `ORIGIN_POLL_MS` what
|
|
86
|
+
* was added, for at most `ORIGIN_WAIT_MS`. Only lines holding the prompt id are parsed, and of them only `type`,
|
|
87
|
+
* `promptId`, `origin.kind`, `timestamp` and the message's text are looked at. Undefined when it cannot tell.
|
|
88
|
+
*/
|
|
89
|
+
export declare function promptOrigin(transcript: string, promptId: string, deps?: {
|
|
90
|
+
sleep?: (ms: number) => Promise<void>;
|
|
91
|
+
clock?: () => number;
|
|
92
|
+
}): Promise<PromptLine | undefined>;
|
|
93
|
+
/**
|
|
94
|
+
* Whether one session is unattended right now: Unattended Mode is on (the Schedule, the manual state and the session
|
|
95
|
+
* flag, read fresh) and the session is not an Attended Session. `attended` is its record while it counts. Hooks and
|
|
96
|
+
* later features that must stay quiet in an unattended session ask this.
|
|
97
|
+
*/
|
|
98
|
+
export declare function sessionUnattended(opts: {
|
|
99
|
+
stateDir: string;
|
|
100
|
+
configFile: string;
|
|
101
|
+
/** The session was started with `--unattended`. */
|
|
102
|
+
session: boolean;
|
|
103
|
+
sessionId?: string;
|
|
104
|
+
now: Date;
|
|
105
|
+
}): Promise<{
|
|
106
|
+
on: boolean;
|
|
107
|
+
state: UnattendedState;
|
|
108
|
+
attended?: AttendedRecord;
|
|
109
|
+
}>;
|