@tidegap/core 0.19.0 → 0.20.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/README.md +1 -1
- package/dist/agent-snippet.d.ts +30 -0
- package/dist/agent-snippet.js +120 -0
- package/dist/channels/commands.d.ts +137 -0
- package/dist/channels/commands.js +377 -0
- package/dist/channels/delivery-codes.d.ts +9 -0
- package/dist/channels/delivery-codes.js +53 -0
- package/dist/channels/digest.d.ts +102 -0
- package/dist/channels/digest.js +146 -0
- package/dist/channels/format.d.ts +127 -0
- package/dist/channels/format.js +407 -0
- package/dist/channels/index.d.ts +15 -0
- package/dist/channels/index.js +15 -0
- package/dist/channels/kinds/command.d.ts +13 -0
- package/dist/channels/kinds/command.js +65 -0
- package/dist/channels/kinds/discord.d.ts +16 -0
- package/dist/channels/kinds/discord.js +67 -0
- package/dist/channels/kinds/email.d.ts +20 -0
- package/dist/channels/kinds/email.js +162 -0
- package/dist/channels/kinds/index.d.ts +8 -0
- package/dist/channels/kinds/index.js +17 -0
- package/dist/channels/kinds/ntfy.d.ts +25 -0
- package/dist/channels/kinds/ntfy.js +163 -0
- package/dist/channels/kinds/pushover.d.ts +11 -0
- package/dist/channels/kinds/pushover.js +77 -0
- package/dist/channels/kinds/shared.d.ts +25 -0
- package/dist/channels/kinds/shared.js +39 -0
- package/dist/channels/kinds/shortcuts.d.ts +9 -0
- package/dist/channels/kinds/shortcuts.js +45 -0
- package/dist/channels/kinds/slack.d.ts +8 -0
- package/dist/channels/kinds/slack.js +57 -0
- package/dist/channels/kinds/telegram.d.ts +4 -0
- package/dist/channels/kinds/telegram.js +26 -0
- package/dist/channels/kinds/webhook.d.ts +30 -0
- package/dist/channels/kinds/webhook.js +177 -0
- package/dist/channels/payload.d.ts +36 -0
- package/dist/channels/payload.js +61 -0
- package/dist/channels/plan.d.ts +24 -0
- package/dist/channels/plan.js +66 -0
- package/dist/channels/registry.d.ts +191 -0
- package/dist/channels/registry.js +125 -0
- package/dist/channels/telegram.d.ts +110 -0
- package/dist/channels/telegram.js +168 -0
- package/dist/channels/types.d.ts +107 -0
- package/dist/channels/types.js +26 -0
- package/dist/claude-tools.d.ts +29 -0
- package/dist/claude-tools.js +49 -0
- package/dist/data/tables.generated.d.ts +6 -0
- package/dist/data/tables.generated.js +37 -0
- package/dist/energy.d.ts +77 -0
- package/dist/energy.js +131 -0
- package/dist/grid.d.ts +17 -2
- package/dist/grid.js +43 -4
- package/dist/handoff.d.ts +152 -0
- package/dist/handoff.js +430 -0
- package/dist/history.d.ts +2 -0
- package/dist/history.js +3 -1
- package/dist/index.d.ts +43 -13
- package/dist/index.js +32 -6
- package/dist/job-cadence.d.ts +76 -0
- package/dist/job-cadence.js +240 -0
- package/dist/jobs.d.ts +173 -0
- package/dist/jobs.js +794 -0
- package/dist/notify.d.ts +2 -0
- package/dist/notify.js +2 -1
- package/dist/postpone.d.ts +113 -0
- package/dist/postpone.js +185 -0
- package/dist/providers/anthropic.js +7 -3
- package/dist/providers/base.d.ts +113 -1
- package/dist/providers/base.js +15 -0
- package/dist/providers/budget.d.ts +25 -0
- package/dist/providers/budget.js +57 -0
- package/dist/providers/defaults.js +4 -0
- package/dist/providers/defer.d.ts +16 -0
- package/dist/providers/defer.js +31 -0
- package/dist/providers/gemini.d.ts +1 -0
- package/dist/providers/gemini.js +27 -20
- package/dist/providers/index.d.ts +2 -2
- package/dist/providers/index.js +1 -1
- package/dist/providers/ollama.d.ts +1 -0
- package/dist/providers/ollama.js +26 -22
- package/dist/providers/openai.js +5 -5
- package/dist/providers/timeout.d.ts +18 -0
- package/dist/providers/timeout.js +33 -0
- package/dist/quota.d.ts +7 -2
- package/dist/quota.js +12 -27
- package/dist/readiness.d.ts +169 -0
- package/dist/readiness.js +254 -0
- package/dist/redact.d.ts +42 -0
- package/dist/redact.js +110 -0
- package/dist/region-host.d.ts +30 -0
- package/dist/region-host.js +69 -0
- package/dist/region.d.ts +102 -20
- package/dist/region.js +175 -23
- package/dist/run-session.d.ts +36 -0
- package/dist/run-session.js +72 -0
- package/dist/schedule-job.d.ts +95 -0
- package/dist/schedule-job.js +195 -0
- package/dist/scheduler.d.ts +245 -14
- package/dist/scheduler.js +919 -109
- package/dist/sign.d.ts +8 -1
- package/dist/sign.js +49 -3
- package/dist/storage/sqlite.d.ts +157 -5
- package/dist/storage/sqlite.js +661 -20
- package/dist/task-events.d.ts +102 -0
- package/dist/task-events.js +308 -0
- package/dist/tool-surface.d.ts +31 -2
- package/dist/tool-surface.js +359 -65
- package/dist/types.d.ts +246 -3
- package/dist/types.js +8 -1
- package/dist/zone-time.d.ts +22 -0
- package/dist/zone-time.js +76 -0
- package/package.json +10 -5
- package/dist/aggregator.d.ts.map +0 -1
- package/dist/aggregator.js.map +0 -1
- package/dist/budget.d.ts.map +0 -1
- package/dist/budget.js.map +0 -1
- package/dist/data/tables.generated.d.ts.map +0 -1
- package/dist/data/tables.generated.js.map +0 -1
- package/dist/energy.d.ts.map +0 -1
- package/dist/energy.js.map +0 -1
- package/dist/grid.d.ts.map +0 -1
- package/dist/grid.js.map +0 -1
- package/dist/history.d.ts.map +0 -1
- package/dist/history.js.map +0 -1
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js.map +0 -1
- package/dist/notify.d.ts.map +0 -1
- package/dist/notify.js.map +0 -1
- package/dist/paths.d.ts.map +0 -1
- package/dist/paths.js.map +0 -1
- package/dist/providers/anthropic.d.ts.map +0 -1
- package/dist/providers/anthropic.js.map +0 -1
- package/dist/providers/base.d.ts.map +0 -1
- package/dist/providers/base.js.map +0 -1
- package/dist/providers/defaults.d.ts.map +0 -1
- package/dist/providers/defaults.js.map +0 -1
- package/dist/providers/gemini.d.ts.map +0 -1
- package/dist/providers/gemini.js.map +0 -1
- package/dist/providers/index.d.ts.map +0 -1
- package/dist/providers/index.js.map +0 -1
- package/dist/providers/ollama.d.ts.map +0 -1
- package/dist/providers/ollama.js.map +0 -1
- package/dist/providers/openai.d.ts.map +0 -1
- package/dist/providers/openai.js.map +0 -1
- package/dist/quota.d.ts.map +0 -1
- package/dist/quota.js.map +0 -1
- package/dist/recommend.d.ts.map +0 -1
- package/dist/recommend.js.map +0 -1
- package/dist/redact.d.ts.map +0 -1
- package/dist/redact.js.map +0 -1
- package/dist/region.d.ts.map +0 -1
- package/dist/region.js.map +0 -1
- package/dist/routing.d.ts.map +0 -1
- package/dist/routing.js.map +0 -1
- package/dist/scheduler.d.ts.map +0 -1
- package/dist/scheduler.js.map +0 -1
- package/dist/select-window.d.ts.map +0 -1
- package/dist/select-window.js.map +0 -1
- package/dist/sign.d.ts.map +0 -1
- package/dist/sign.js.map +0 -1
- package/dist/storage/sqlite.d.ts.map +0 -1
- package/dist/storage/sqlite.js.map +0 -1
- package/dist/tool-surface.d.ts.map +0 -1
- package/dist/tool-surface.js.map +0 -1
- package/dist/types.d.ts.map +0 -1
- package/dist/types.js.map +0 -1
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Handoff fields of a task queued from an agent session: the folder a run
|
|
3
|
+
* works in (`cwd`), its tool profile and the result file Tidegap writes when
|
|
4
|
+
* it completes. Pure rules with injected file-system reads, so the MCP
|
|
5
|
+
* server, the CLI and the tick apply the same checks, at enqueue and again
|
|
6
|
+
* before a run or a write.
|
|
7
|
+
*
|
|
8
|
+
* Runs stay read-only: a profile only names one of the existing tool sets,
|
|
9
|
+
* and only Tidegap writes a result file, as a new file, never through a
|
|
10
|
+
* link.
|
|
11
|
+
*/
|
|
12
|
+
/** A tool profile: a name for one of the existing tool sets. */
|
|
13
|
+
export type HandoffProfile = "read" | "web" | "none";
|
|
14
|
+
export declare const HANDOFF_PROFILES: readonly HandoffProfile[];
|
|
15
|
+
/** The tools of each profile. `read` is scoped to the run's folder by the adapter. */
|
|
16
|
+
export declare const PROFILE_TOOLS: Readonly<Record<HandoffProfile, readonly string[]>>;
|
|
17
|
+
/** The handoff fields of a task body (`ProviderCallSpec.handoff`). */
|
|
18
|
+
export interface HandoffSpec {
|
|
19
|
+
/** The folder a `claude-code` run works in. */
|
|
20
|
+
cwd?: string;
|
|
21
|
+
/** The tool profile; `read` when only `cwd` is given. */
|
|
22
|
+
profile?: HandoffProfile;
|
|
23
|
+
/** The new file Tidegap writes the result text to when the task completes. */
|
|
24
|
+
resultPath?: string;
|
|
25
|
+
/**
|
|
26
|
+
* Wake the idle Claude Code session that queued the task when it
|
|
27
|
+
* finishes (the plugin's Stop hook). Needs a checked session origin.
|
|
28
|
+
*/
|
|
29
|
+
wake?: boolean;
|
|
30
|
+
}
|
|
31
|
+
/** Longest result or output path. */
|
|
32
|
+
export declare const HANDOFF_PATH_MAX_CHARS = 1024;
|
|
33
|
+
/** What the rules need to know about one path; no link is followed. */
|
|
34
|
+
export interface HandoffFsEntry {
|
|
35
|
+
kind: "file" | "dir" | "symlink" | "other";
|
|
36
|
+
uid: number;
|
|
37
|
+
}
|
|
38
|
+
/** The file-system reads of the rules. Tests inject their own. */
|
|
39
|
+
export interface HandoffFs {
|
|
40
|
+
/** The real path (links resolved), or undefined when the path does not resolve. */
|
|
41
|
+
realpath(path: string): string | undefined;
|
|
42
|
+
/** What is at `path` itself (a link is not followed), or undefined when nothing is. */
|
|
43
|
+
lstat(path: string): HandoffFsEntry | undefined;
|
|
44
|
+
/**
|
|
45
|
+
* The identity of the folder or file at `path` (links followed), such as
|
|
46
|
+
* `<dev>:<ino>`, or undefined when nothing is there. Two spellings of one
|
|
47
|
+
* folder (another letter case, a firmlink such as `/System/Volumes/Data`)
|
|
48
|
+
* have the same identity. Optional: without it, only the spelling counts.
|
|
49
|
+
*/
|
|
50
|
+
identity?(path: string): string | undefined;
|
|
51
|
+
}
|
|
52
|
+
export declare const nodeHandoffFs: HandoffFs;
|
|
53
|
+
/** Where a result or output file may go, and how the rules read the disk. */
|
|
54
|
+
export interface HandoffPathContext {
|
|
55
|
+
/** The task's own folder (`handoff.cwd`, or the command's `cwd`). */
|
|
56
|
+
cwd?: string;
|
|
57
|
+
/** The origin session's project folder (the `origin_cwd` column). */
|
|
58
|
+
originCwd?: string;
|
|
59
|
+
/** Default: this user's home folder. */
|
|
60
|
+
home?: string;
|
|
61
|
+
/** Default: the system temp folder and `/tmp`. */
|
|
62
|
+
tmpDirs?: readonly string[];
|
|
63
|
+
fs?: HandoffFs;
|
|
64
|
+
/** Default: this process's user id. */
|
|
65
|
+
uid?: number;
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Folders under the home folder that no handoff file or `claude-code` folder
|
|
69
|
+
* may be in. The handoff and tools pages list them; docs-drift compares.
|
|
70
|
+
*/
|
|
71
|
+
export declare const PROTECTED_HOME_DIRS: readonly string[];
|
|
72
|
+
/**
|
|
73
|
+
* Check a result path: the location rules, and create-only (nothing may be
|
|
74
|
+
* at the path yet, not even a link). Returns the error message, or
|
|
75
|
+
* undefined when the path is allowed.
|
|
76
|
+
*/
|
|
77
|
+
export declare function resultPathError(path: unknown, ctx?: HandoffPathContext): string | undefined;
|
|
78
|
+
/**
|
|
79
|
+
* Check an `output_path`: the location rules; a file already there must be
|
|
80
|
+
* a regular file (not a link) that this user owns, which the JSON then
|
|
81
|
+
* replaces. Returns the error message, or undefined when the path is allowed.
|
|
82
|
+
*/
|
|
83
|
+
export declare function outputPathError(path: unknown, ctx?: HandoffPathContext): string | undefined;
|
|
84
|
+
/**
|
|
85
|
+
* Check the folder of a handed-off run. Every runner: an absolute path in
|
|
86
|
+
* its `path.resolve` form to an existing folder. `claude-code` also: not
|
|
87
|
+
* `/` or the home folder, owned by this user, and not inside a dot folder
|
|
88
|
+
* of the home folder, `~/Library` or a `.git` folder (links resolved too).
|
|
89
|
+
* Returns the error message, or undefined.
|
|
90
|
+
*/
|
|
91
|
+
export declare function handoffCwdError(cwd: unknown, runner: "claude-code" | "command", ctx?: Pick<HandoffPathContext, "home" | "fs" | "uid">): string | undefined;
|
|
92
|
+
/** The profile and tools a handoff asks for, or the reason it is refused. */
|
|
93
|
+
export interface ProfileChoice {
|
|
94
|
+
profile?: HandoffProfile;
|
|
95
|
+
tools?: string[];
|
|
96
|
+
error?: string;
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* The tool set of a profile. A `cwd` with no profile means `read`; `web`
|
|
100
|
+
* runs in a fresh temporary folder, so it refuses a `cwd`. Neither given:
|
|
101
|
+
* no profile (the runner's default tools apply).
|
|
102
|
+
*/
|
|
103
|
+
export declare function profileTools(profile: unknown, cwd: string | undefined): ProfileChoice;
|
|
104
|
+
/** True when two tool lists name the same tools. */
|
|
105
|
+
export declare function sameTools(a: readonly string[], b: readonly string[]): boolean;
|
|
106
|
+
/** What a task runs and where (the handed-off work shape), from its body. */
|
|
107
|
+
export interface TaskWorkShape {
|
|
108
|
+
runner?: string;
|
|
109
|
+
cwd?: string;
|
|
110
|
+
profile?: string;
|
|
111
|
+
tools?: string[];
|
|
112
|
+
resultPath?: string;
|
|
113
|
+
/** True when the task wakes its idle origin session when it finishes. */
|
|
114
|
+
wake?: boolean;
|
|
115
|
+
/** The shell command of a one-off command task. */
|
|
116
|
+
command?: string;
|
|
117
|
+
}
|
|
118
|
+
/** {@link TaskWorkShape} of a task; empty for a closure task or a corrupt body. */
|
|
119
|
+
export declare function taskWorkShape(record: {
|
|
120
|
+
bodyJson?: string;
|
|
121
|
+
}): TaskWorkShape;
|
|
122
|
+
/** The details of a task's newest `result_file` event, or undefined. */
|
|
123
|
+
export declare function latestResultFile(events: ReadonlyArray<{
|
|
124
|
+
kind: string;
|
|
125
|
+
details: Record<string, unknown>;
|
|
126
|
+
}> | undefined): Record<string, unknown> | undefined;
|
|
127
|
+
/** A result file's state in words: `written, 12 bytes`, `not written: <error>`, or `not written yet`. */
|
|
128
|
+
export declare function resultFileText(details: Record<string, unknown> | undefined): string;
|
|
129
|
+
/**
|
|
130
|
+
* Why a task may not ask for a wake, or undefined when it may: a wake is
|
|
131
|
+
* `true` or absent, and `true` needs a checked Claude Code session origin
|
|
132
|
+
* (the body's `origin` and a valid session id), so the right session wakes.
|
|
133
|
+
*/
|
|
134
|
+
export declare function wakeError(wake: unknown, origin: {
|
|
135
|
+
agent?: unknown;
|
|
136
|
+
} | undefined, sessionId: unknown): string | undefined;
|
|
137
|
+
/** A wake marker with no wake task left to wait for goes this long after its last refresh (the waiter refreshes it at every Stop). */
|
|
138
|
+
export declare const WAKE_MARKER_MAX_AGE_MS: number;
|
|
139
|
+
/** `~/.tidegap/sessions/wake`: one marker per session with a task to wake it for. */
|
|
140
|
+
export declare function wakeMarkersDir(home?: string): string;
|
|
141
|
+
/** The wake marker of one session, or undefined for an id that fails the id rule. */
|
|
142
|
+
export declare function wakeMarkerPath(sessionId: string, dir?: string): string | undefined;
|
|
143
|
+
/**
|
|
144
|
+
* Create or refresh a session's wake marker (file `0600`, folder `0700`),
|
|
145
|
+
* so the plugin's Stop hook starts its waiter. Returns the error message,
|
|
146
|
+
* or undefined. Something other than a regular file at the path is left
|
|
147
|
+
* alone and reported.
|
|
148
|
+
*/
|
|
149
|
+
export declare function writeWakeMarker(sessionId: string, dir?: string, now?: Date): string | undefined;
|
|
150
|
+
/** Remove a session's wake marker; a missing one is fine. */
|
|
151
|
+
export declare function removeWakeMarker(sessionId: string, dir?: string): void;
|
|
152
|
+
//# sourceMappingURL=handoff.d.ts.map
|
package/dist/handoff.js
ADDED
|
@@ -0,0 +1,430 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Handoff fields of a task queued from an agent session: the folder a run
|
|
3
|
+
* works in (`cwd`), its tool profile and the result file Tidegap writes when
|
|
4
|
+
* it completes. Pure rules with injected file-system reads, so the MCP
|
|
5
|
+
* server, the CLI and the tick apply the same checks, at enqueue and again
|
|
6
|
+
* before a run or a write.
|
|
7
|
+
*
|
|
8
|
+
* Runs stay read-only: a profile only names one of the existing tool sets,
|
|
9
|
+
* and only Tidegap writes a result file, as a new file, never through a
|
|
10
|
+
* link.
|
|
11
|
+
*/
|
|
12
|
+
import { chmodSync, lstatSync, mkdirSync, realpathSync, statSync, unlinkSync, utimesSync, writeFileSync } from "node:fs";
|
|
13
|
+
import { homedir, tmpdir } from "node:os";
|
|
14
|
+
import { basename, dirname, isAbsolute, join, relative, resolve } from "node:path";
|
|
15
|
+
import { isValidSessionId } from "./history.js";
|
|
16
|
+
import { resolveTidegapHome } from "./paths.js";
|
|
17
|
+
export const HANDOFF_PROFILES = ["read", "web", "none"];
|
|
18
|
+
/** The tools of each profile. `read` is scoped to the run's folder by the adapter. */
|
|
19
|
+
export const PROFILE_TOOLS = {
|
|
20
|
+
read: ["Read", "Grep", "Glob"],
|
|
21
|
+
web: ["WebSearch", "WebFetch"],
|
|
22
|
+
none: [],
|
|
23
|
+
};
|
|
24
|
+
/** Longest result or output path. */
|
|
25
|
+
export const HANDOFF_PATH_MAX_CHARS = 1024;
|
|
26
|
+
export const nodeHandoffFs = {
|
|
27
|
+
realpath(path) {
|
|
28
|
+
try {
|
|
29
|
+
// The native call also returns the on-disk letter case (macOS).
|
|
30
|
+
return realpathSync.native(path);
|
|
31
|
+
}
|
|
32
|
+
catch {
|
|
33
|
+
return undefined;
|
|
34
|
+
}
|
|
35
|
+
},
|
|
36
|
+
identity(path) {
|
|
37
|
+
try {
|
|
38
|
+
const s = statSync(path);
|
|
39
|
+
return `${s.dev}:${s.ino}`;
|
|
40
|
+
}
|
|
41
|
+
catch {
|
|
42
|
+
return undefined;
|
|
43
|
+
}
|
|
44
|
+
},
|
|
45
|
+
lstat(path) {
|
|
46
|
+
try {
|
|
47
|
+
const s = lstatSync(path);
|
|
48
|
+
const kind = s.isSymbolicLink() ? "symlink" : s.isDirectory() ? "dir" : s.isFile() ? "file" : "other";
|
|
49
|
+
return { kind, uid: s.uid };
|
|
50
|
+
}
|
|
51
|
+
catch {
|
|
52
|
+
return undefined;
|
|
53
|
+
}
|
|
54
|
+
},
|
|
55
|
+
};
|
|
56
|
+
/**
|
|
57
|
+
* Folders under the home folder that no handoff file or `claude-code` folder
|
|
58
|
+
* may be in. The handoff and tools pages list them; docs-drift compares.
|
|
59
|
+
*/
|
|
60
|
+
export const PROTECTED_HOME_DIRS = Object.freeze([".tidegap", ".config", ".claude", ".ssh", ".gnupg", ".aws", "Library"]);
|
|
61
|
+
// eslint-disable-next-line no-control-regex
|
|
62
|
+
const CONTROL_CHARS = /[\u0000-\u001f\u007f-\u009f]/;
|
|
63
|
+
/** True when `path` is `dir` or below it. */
|
|
64
|
+
function inside(path, dir) {
|
|
65
|
+
if (path === dir)
|
|
66
|
+
return true;
|
|
67
|
+
return path.startsWith(dir.endsWith("/") ? dir : `${dir}/`);
|
|
68
|
+
}
|
|
69
|
+
function segments(rel) {
|
|
70
|
+
return rel.split("/").filter((s) => s !== "");
|
|
71
|
+
}
|
|
72
|
+
function unique(values) {
|
|
73
|
+
return [...new Set(values.filter((v) => typeof v === "string" && v !== ""))];
|
|
74
|
+
}
|
|
75
|
+
/** The home folder as given and as its real path. */
|
|
76
|
+
function homes(home, fs) {
|
|
77
|
+
return unique([home, fs.realpath(home)]);
|
|
78
|
+
}
|
|
79
|
+
/** True when `path` is `dir` or below it, in any letter case (a case-insensitive disk). */
|
|
80
|
+
function insideAnyCase(path, dir) {
|
|
81
|
+
return inside(path.toLowerCase(), dir.toLowerCase());
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Where `path` sits relative to the home folder: `{ first: undefined }` when
|
|
85
|
+
* it is the home folder, `{ first }` with the first name below it, or
|
|
86
|
+
* undefined when it is not inside. Matches the spelling in any letter case,
|
|
87
|
+
* then the identity of each folder above `path`, so another spelling of the
|
|
88
|
+
* home folder (a firmlink, another case) is found too.
|
|
89
|
+
*/
|
|
90
|
+
function homeSegment(path, homeDirs, fs) {
|
|
91
|
+
for (const h of homeDirs) {
|
|
92
|
+
if (!insideAnyCase(path, h))
|
|
93
|
+
continue;
|
|
94
|
+
const first = segments(path)[segments(h).length];
|
|
95
|
+
return first === undefined ? { home: h } : { home: h, first };
|
|
96
|
+
}
|
|
97
|
+
const identity = fs.identity?.bind(fs);
|
|
98
|
+
if (!identity)
|
|
99
|
+
return undefined;
|
|
100
|
+
const homeIds = new Set(homeDirs.map((h) => identity(h)).filter((id) => id !== undefined));
|
|
101
|
+
if (homeIds.size === 0)
|
|
102
|
+
return undefined;
|
|
103
|
+
let cur = path;
|
|
104
|
+
let child;
|
|
105
|
+
for (;;) {
|
|
106
|
+
const id = identity(cur);
|
|
107
|
+
if (id !== undefined && homeIds.has(id))
|
|
108
|
+
return child === undefined ? { home: cur } : { home: cur, first: child };
|
|
109
|
+
const up = dirname(cur);
|
|
110
|
+
if (up === cur)
|
|
111
|
+
return undefined;
|
|
112
|
+
child = basename(cur);
|
|
113
|
+
cur = up;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
/** True when `dir` is `/` or the home folder, by spelling or identity. */
|
|
117
|
+
function isRootOrHome(dir, homeDirs, fs) {
|
|
118
|
+
const real = fs.realpath(dir) ?? dir;
|
|
119
|
+
if (dir === "/" || real === "/")
|
|
120
|
+
return true;
|
|
121
|
+
const id = fs.identity?.(real);
|
|
122
|
+
if (id !== undefined && id === fs.identity?.("/"))
|
|
123
|
+
return true;
|
|
124
|
+
const at = homeSegment(real, homeDirs, fs);
|
|
125
|
+
return at !== undefined && at.first === undefined;
|
|
126
|
+
}
|
|
127
|
+
/** True when a name is `.git` in any letter case. */
|
|
128
|
+
function isGitName(name) {
|
|
129
|
+
return name.toLowerCase() === ".git";
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* The shape rule: an absolute path of at most {@link HANDOFF_PATH_MAX_CHARS}
|
|
133
|
+
* characters, without control characters, equal to its `path.resolve` form
|
|
134
|
+
* (no `.` or `..` segments, no repeated or trailing `/`). Links are not
|
|
135
|
+
* resolved here.
|
|
136
|
+
*/
|
|
137
|
+
function shapeError(name, path) {
|
|
138
|
+
if (typeof path !== "string" || path === "")
|
|
139
|
+
return `${name} must be an absolute path`;
|
|
140
|
+
if (CONTROL_CHARS.test(path))
|
|
141
|
+
return `${name} must not contain control characters`;
|
|
142
|
+
if (path.length > HANDOFF_PATH_MAX_CHARS)
|
|
143
|
+
return `${name} is longer than ${HANDOFF_PATH_MAX_CHARS} characters`;
|
|
144
|
+
if (!isAbsolute(path))
|
|
145
|
+
return `${name} must be an absolute path, got ${JSON.stringify(path)}`;
|
|
146
|
+
if (resolve(path) !== path) {
|
|
147
|
+
return `${name} ${JSON.stringify(path)} must have no "." or ".." segments and no repeated or trailing "/"`;
|
|
148
|
+
}
|
|
149
|
+
return undefined;
|
|
150
|
+
}
|
|
151
|
+
/**
|
|
152
|
+
* The protected folder `path` is in, or undefined: a home dot folder of the
|
|
153
|
+
* list, `~/Library`, or any `.git` folder. Names match in any letter case,
|
|
154
|
+
* and the home folder also by identity (see {@link homeSegment}).
|
|
155
|
+
*/
|
|
156
|
+
function protectedDir(path, homeDirs, fs) {
|
|
157
|
+
const at = homeSegment(path, homeDirs, fs);
|
|
158
|
+
const first = at?.first?.toLowerCase();
|
|
159
|
+
const match = first === undefined ? undefined : PROTECTED_HOME_DIRS.find((d) => d.toLowerCase() === first);
|
|
160
|
+
if (at && match)
|
|
161
|
+
return join(homeDirs.includes(at.home) ? at.home : (homeDirs[0] ?? at.home), match);
|
|
162
|
+
if (segments(path).some(isGitName))
|
|
163
|
+
return "a .git folder";
|
|
164
|
+
return undefined;
|
|
165
|
+
}
|
|
166
|
+
/**
|
|
167
|
+
* The location rules of a result or output file. 1: the shape rule. 2: the
|
|
168
|
+
* real path of the parent folder is inside the real path of an allowed root
|
|
169
|
+
* (the task's folder, the session's project folder, the temp folders; never
|
|
170
|
+
* `/` or the home folder itself), no folder or file name below that root
|
|
171
|
+
* starts with `.`, and every folder below that root is this user's. 3:
|
|
172
|
+
* neither the path nor the parent's real path is in a protected folder.
|
|
173
|
+
*/
|
|
174
|
+
function locationError(name, path, ctx) {
|
|
175
|
+
const shape = shapeError(name, path);
|
|
176
|
+
if (shape)
|
|
177
|
+
return shape;
|
|
178
|
+
const p = path;
|
|
179
|
+
const fs = ctx.fs ?? nodeHandoffFs;
|
|
180
|
+
const homeDirs = homes(ctx.home ?? homedir(), fs);
|
|
181
|
+
// A session started in `/` or the home folder names no project: those are not roots.
|
|
182
|
+
const roots = unique([ctx.cwd, ctx.originCwd, ...(ctx.tmpDirs ?? [tmpdir(), "/tmp"])]).filter((root) => !isRootOrHome(root, homeDirs, fs));
|
|
183
|
+
const allowed = `; allowed folders: ${roots.join(", ") || "none"}`;
|
|
184
|
+
const uid = ctx.uid ?? process.getuid?.();
|
|
185
|
+
const isProtected = (candidate) => {
|
|
186
|
+
const dir = protectedDir(candidate, homeDirs, fs);
|
|
187
|
+
return dir ? `${name} ${p} is inside ${dir}, a protected folder${allowed}` : undefined;
|
|
188
|
+
};
|
|
189
|
+
const given = isProtected(p);
|
|
190
|
+
if (given)
|
|
191
|
+
return given;
|
|
192
|
+
const parent = dirname(p);
|
|
193
|
+
const realParent = fs.realpath(parent);
|
|
194
|
+
if (!realParent)
|
|
195
|
+
return `${name} ${p}: the folder ${parent} does not exist${allowed}`;
|
|
196
|
+
const real = isProtected(join(realParent, basename(p)));
|
|
197
|
+
if (real)
|
|
198
|
+
return real;
|
|
199
|
+
let dotted = false;
|
|
200
|
+
for (const root of roots) {
|
|
201
|
+
const realRoot = fs.realpath(root);
|
|
202
|
+
if (!realRoot || !inside(realParent, realRoot))
|
|
203
|
+
continue;
|
|
204
|
+
const below = [...segments(relative(realRoot, realParent)), basename(p)];
|
|
205
|
+
const lexical = inside(p, root) ? segments(relative(root, p)) : [];
|
|
206
|
+
if ([...below, ...lexical].some((s) => s.startsWith("."))) {
|
|
207
|
+
dotted = true;
|
|
208
|
+
continue;
|
|
209
|
+
}
|
|
210
|
+
// Another user's folder below the root could be swapped for a link after this check.
|
|
211
|
+
for (let dir = realParent; dir !== realRoot && inside(dir, realRoot); dir = dirname(dir)) {
|
|
212
|
+
const entry = fs.lstat(dir);
|
|
213
|
+
if (entry?.kind !== "dir" || (uid !== undefined && entry.uid !== uid)) {
|
|
214
|
+
return `${name} ${p}: the folder ${dir} is not a folder you own${allowed}`;
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
return undefined;
|
|
218
|
+
}
|
|
219
|
+
if (dotted)
|
|
220
|
+
return `${name} ${p}: no folder or file name below the allowed folder may start with "."${allowed}`;
|
|
221
|
+
return `${name} ${p} must be inside one of these folders: ${roots.join(", ") || "none"}`;
|
|
222
|
+
}
|
|
223
|
+
/**
|
|
224
|
+
* Check a result path: the location rules, and create-only (nothing may be
|
|
225
|
+
* at the path yet, not even a link). Returns the error message, or
|
|
226
|
+
* undefined when the path is allowed.
|
|
227
|
+
*/
|
|
228
|
+
export function resultPathError(path, ctx = {}) {
|
|
229
|
+
const location = locationError("result path", path, ctx);
|
|
230
|
+
if (location)
|
|
231
|
+
return location;
|
|
232
|
+
const fs = ctx.fs ?? nodeHandoffFs;
|
|
233
|
+
if (fs.lstat(path))
|
|
234
|
+
return `result path ${path} already exists; a result file is never replaced`;
|
|
235
|
+
return undefined;
|
|
236
|
+
}
|
|
237
|
+
/**
|
|
238
|
+
* Check an `output_path`: the location rules; a file already there must be
|
|
239
|
+
* a regular file (not a link) that this user owns, which the JSON then
|
|
240
|
+
* replaces. Returns the error message, or undefined when the path is allowed.
|
|
241
|
+
*/
|
|
242
|
+
export function outputPathError(path, ctx = {}) {
|
|
243
|
+
const location = locationError("output path", path, ctx);
|
|
244
|
+
if (location)
|
|
245
|
+
return location;
|
|
246
|
+
const fs = ctx.fs ?? nodeHandoffFs;
|
|
247
|
+
const entry = fs.lstat(path);
|
|
248
|
+
if (!entry)
|
|
249
|
+
return undefined;
|
|
250
|
+
const uid = ctx.uid ?? process.getuid?.();
|
|
251
|
+
if (entry.kind !== "file" || (uid !== undefined && entry.uid !== uid)) {
|
|
252
|
+
return `output path ${path} exists and is not a regular file you own`;
|
|
253
|
+
}
|
|
254
|
+
return undefined;
|
|
255
|
+
}
|
|
256
|
+
/**
|
|
257
|
+
* Check the folder of a handed-off run. Every runner: an absolute path in
|
|
258
|
+
* its `path.resolve` form to an existing folder. `claude-code` also: not
|
|
259
|
+
* `/` or the home folder, owned by this user, and not inside a dot folder
|
|
260
|
+
* of the home folder, `~/Library` or a `.git` folder (links resolved too).
|
|
261
|
+
* Returns the error message, or undefined.
|
|
262
|
+
*/
|
|
263
|
+
export function handoffCwdError(cwd, runner, ctx = {}) {
|
|
264
|
+
const shape = shapeError("cwd", cwd);
|
|
265
|
+
if (shape)
|
|
266
|
+
return shape;
|
|
267
|
+
const dir = cwd;
|
|
268
|
+
const fs = ctx.fs ?? nodeHandoffFs;
|
|
269
|
+
const real = fs.realpath(dir);
|
|
270
|
+
const entry = real ? fs.lstat(real) : undefined;
|
|
271
|
+
if (!real || entry?.kind !== "dir")
|
|
272
|
+
return `cwd ${dir} is not an existing folder`;
|
|
273
|
+
if (runner === "command")
|
|
274
|
+
return undefined;
|
|
275
|
+
const homeDirs = homes(ctx.home ?? homedir(), fs);
|
|
276
|
+
if (isRootOrHome(dir, homeDirs, fs) || isRootOrHome(real, homeDirs, fs))
|
|
277
|
+
return `cwd ${dir} is the root or home folder`;
|
|
278
|
+
const uid = ctx.uid ?? process.getuid?.();
|
|
279
|
+
if (uid !== undefined && entry.uid !== uid)
|
|
280
|
+
return `cwd ${dir} belongs to another user`;
|
|
281
|
+
for (const candidate of [dir, real]) {
|
|
282
|
+
const at = homeSegment(candidate, homeDirs, fs);
|
|
283
|
+
const first = at?.first;
|
|
284
|
+
if (at && first !== undefined && (first.startsWith(".") || first.toLowerCase() === "library")) {
|
|
285
|
+
return `cwd ${dir} is inside ${join(at.home, first)}, a protected folder`;
|
|
286
|
+
}
|
|
287
|
+
if (segments(candidate).some(isGitName))
|
|
288
|
+
return `cwd ${dir} is inside a .git folder`;
|
|
289
|
+
}
|
|
290
|
+
return undefined;
|
|
291
|
+
}
|
|
292
|
+
/**
|
|
293
|
+
* The tool set of a profile. A `cwd` with no profile means `read`; `web`
|
|
294
|
+
* runs in a fresh temporary folder, so it refuses a `cwd`. Neither given:
|
|
295
|
+
* no profile (the runner's default tools apply).
|
|
296
|
+
*/
|
|
297
|
+
export function profileTools(profile, cwd) {
|
|
298
|
+
if (profile === undefined) {
|
|
299
|
+
return cwd === undefined ? {} : { profile: "read", tools: [...PROFILE_TOOLS.read] };
|
|
300
|
+
}
|
|
301
|
+
if (typeof profile !== "string" || !HANDOFF_PROFILES.includes(profile)) {
|
|
302
|
+
return { error: `profile must be one of ${HANDOFF_PROFILES.join(", ")}, got ${JSON.stringify(profile)}` };
|
|
303
|
+
}
|
|
304
|
+
const p = profile;
|
|
305
|
+
if (p === "web" && cwd !== undefined) {
|
|
306
|
+
return { error: "profile web runs in a fresh temporary folder and cannot take a cwd; use profile read with a cwd" };
|
|
307
|
+
}
|
|
308
|
+
return { profile: p, tools: [...PROFILE_TOOLS[p]] };
|
|
309
|
+
}
|
|
310
|
+
/** True when two tool lists name the same tools. */
|
|
311
|
+
export function sameTools(a, b) {
|
|
312
|
+
return a.length === b.length && [...a].sort().join(",") === [...b].sort().join(",");
|
|
313
|
+
}
|
|
314
|
+
/** {@link TaskWorkShape} of a task; empty for a closure task or a corrupt body. */
|
|
315
|
+
export function taskWorkShape(record) {
|
|
316
|
+
if (!record.bodyJson)
|
|
317
|
+
return {};
|
|
318
|
+
let spec;
|
|
319
|
+
try {
|
|
320
|
+
spec = JSON.parse(record.bodyJson);
|
|
321
|
+
}
|
|
322
|
+
catch {
|
|
323
|
+
return {};
|
|
324
|
+
}
|
|
325
|
+
const str = (v) => (typeof v === "string" && v !== "" ? v : undefined);
|
|
326
|
+
const oneOff = spec?.provider === "command" && spec.command?.origin === "cli";
|
|
327
|
+
const out = {};
|
|
328
|
+
const runner = str(spec?.provider);
|
|
329
|
+
if (runner)
|
|
330
|
+
out.runner = runner;
|
|
331
|
+
const cwd = str(spec?.handoff?.cwd) ?? (oneOff ? str(spec.command?.cwd) : undefined);
|
|
332
|
+
if (cwd)
|
|
333
|
+
out.cwd = cwd;
|
|
334
|
+
const profile = str(spec?.handoff?.profile);
|
|
335
|
+
if (profile)
|
|
336
|
+
out.profile = profile;
|
|
337
|
+
if (Array.isArray(spec?.tools))
|
|
338
|
+
out.tools = spec.tools.filter((t) => typeof t === "string");
|
|
339
|
+
const resultPath = str(spec?.handoff?.resultPath);
|
|
340
|
+
if (resultPath)
|
|
341
|
+
out.resultPath = resultPath;
|
|
342
|
+
if (spec?.handoff?.wake === true)
|
|
343
|
+
out.wake = true;
|
|
344
|
+
const command = oneOff ? str(spec.command?.run) : undefined;
|
|
345
|
+
if (command)
|
|
346
|
+
out.command = command;
|
|
347
|
+
return out;
|
|
348
|
+
}
|
|
349
|
+
/** The details of a task's newest `result_file` event, or undefined. */
|
|
350
|
+
export function latestResultFile(events) {
|
|
351
|
+
return [...(events ?? [])].reverse().find((e) => e.kind === "result_file")?.details;
|
|
352
|
+
}
|
|
353
|
+
/** A result file's state in words: `written, 12 bytes`, `not written: <error>`, or `not written yet`. */
|
|
354
|
+
export function resultFileText(details) {
|
|
355
|
+
if (!details)
|
|
356
|
+
return "not written yet";
|
|
357
|
+
if (details.written === true)
|
|
358
|
+
return typeof details.bytes === "number" ? `written, ${details.bytes} bytes` : "written";
|
|
359
|
+
return `not written: ${typeof details.error === "string" ? details.error : "unknown error"}`;
|
|
360
|
+
}
|
|
361
|
+
/**
|
|
362
|
+
* Why a task may not ask for a wake, or undefined when it may: a wake is
|
|
363
|
+
* `true` or absent, and `true` needs a checked Claude Code session origin
|
|
364
|
+
* (the body's `origin` and a valid session id), so the right session wakes.
|
|
365
|
+
*/
|
|
366
|
+
export function wakeError(wake, origin, sessionId) {
|
|
367
|
+
if (wake === undefined || wake === false)
|
|
368
|
+
return undefined;
|
|
369
|
+
if (wake !== true)
|
|
370
|
+
return "wake must be true or false";
|
|
371
|
+
if (origin?.agent !== "claude-code" || !isValidSessionId(sessionId)) {
|
|
372
|
+
return "a wake needs a task queued from a Claude Code session (the session that wakes); queue it from the session, or drop the wake";
|
|
373
|
+
}
|
|
374
|
+
return undefined;
|
|
375
|
+
}
|
|
376
|
+
/** A wake marker with no wake task left to wait for goes this long after its last refresh (the waiter refreshes it at every Stop). */
|
|
377
|
+
export const WAKE_MARKER_MAX_AGE_MS = 12 * 3_600_000;
|
|
378
|
+
/** `~/.tidegap/sessions/wake`: one marker per session with a task to wake it for. */
|
|
379
|
+
export function wakeMarkersDir(home = resolveTidegapHome()) {
|
|
380
|
+
return join(home, "sessions", "wake");
|
|
381
|
+
}
|
|
382
|
+
/** The wake marker of one session, or undefined for an id that fails the id rule. */
|
|
383
|
+
export function wakeMarkerPath(sessionId, dir = wakeMarkersDir()) {
|
|
384
|
+
return isValidSessionId(sessionId) ? join(dir, sessionId) : undefined;
|
|
385
|
+
}
|
|
386
|
+
/**
|
|
387
|
+
* Create or refresh a session's wake marker (file `0600`, folder `0700`),
|
|
388
|
+
* so the plugin's Stop hook starts its waiter. Returns the error message,
|
|
389
|
+
* or undefined. Something other than a regular file at the path is left
|
|
390
|
+
* alone and reported.
|
|
391
|
+
*/
|
|
392
|
+
export function writeWakeMarker(sessionId, dir = wakeMarkersDir(), now = new Date()) {
|
|
393
|
+
const path = wakeMarkerPath(sessionId, dir);
|
|
394
|
+
if (!path)
|
|
395
|
+
return "not a session id";
|
|
396
|
+
try {
|
|
397
|
+
mkdirSync(dir, { recursive: true, mode: 0o700 });
|
|
398
|
+
chmodSync(dir, 0o700);
|
|
399
|
+
let there;
|
|
400
|
+
try {
|
|
401
|
+
there = lstatSync(path);
|
|
402
|
+
}
|
|
403
|
+
catch {
|
|
404
|
+
there = undefined;
|
|
405
|
+
}
|
|
406
|
+
if (there && !there.isFile())
|
|
407
|
+
return `${path} is not a regular file`;
|
|
408
|
+
if (there)
|
|
409
|
+
utimesSync(path, now, now);
|
|
410
|
+
else
|
|
411
|
+
writeFileSync(path, `${now.toISOString()}\n`, { mode: 0o600, flag: "wx" });
|
|
412
|
+
return undefined;
|
|
413
|
+
}
|
|
414
|
+
catch (err) {
|
|
415
|
+
return err.message;
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
/** Remove a session's wake marker; a missing one is fine. */
|
|
419
|
+
export function removeWakeMarker(sessionId, dir = wakeMarkersDir()) {
|
|
420
|
+
const path = wakeMarkerPath(sessionId, dir);
|
|
421
|
+
if (!path)
|
|
422
|
+
return;
|
|
423
|
+
try {
|
|
424
|
+
unlinkSync(path);
|
|
425
|
+
}
|
|
426
|
+
catch {
|
|
427
|
+
// already gone
|
|
428
|
+
}
|
|
429
|
+
}
|
|
430
|
+
//# sourceMappingURL=handoff.js.map
|
package/dist/history.d.ts
CHANGED
|
@@ -64,6 +64,8 @@ export interface TaskActions {
|
|
|
64
64
|
reschedule: boolean;
|
|
65
65
|
/** Queued or scheduled provider-call task: mark it due for the next tick. */
|
|
66
66
|
runNow: boolean;
|
|
67
|
+
/** Queued or scheduled provider-call task: move it later ("Later", same rule as `reschedule`). */
|
|
68
|
+
later: boolean;
|
|
67
69
|
/** Completed provider-call task: enqueue a reply child. */
|
|
68
70
|
reply: boolean;
|
|
69
71
|
/** Finished (completed or failed): mark read or unread. */
|
package/dist/history.js
CHANGED
|
@@ -146,7 +146,9 @@ export function taskActions(record) {
|
|
|
146
146
|
stopSeries: pending && isRepeating(record),
|
|
147
147
|
reschedule: movable && providerCall,
|
|
148
148
|
runNow: movable && providerCall,
|
|
149
|
-
|
|
149
|
+
later: movable && providerCall,
|
|
150
|
+
// A command job's occurrence has no conversation to continue.
|
|
151
|
+
reply: record.status === "completed" && providerCall && spec?.provider !== "command",
|
|
150
152
|
markRead: record.status === "completed" || record.status === "failed",
|
|
151
153
|
};
|
|
152
154
|
}
|