@getfamiliar/shared 0.1.13 → 0.1.16
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/build/ChatInput.d.ts +42 -0
- package/build/ChatInput.js +45 -0
- package/build/ChatMessage.d.ts +11 -0
- package/build/ChatMessage.js +2 -1
- package/build/ChatMessageBus.d.ts +11 -1
- package/build/ChatMessageBus.js +26 -0
- package/build/DockerTools.d.ts +119 -0
- package/build/DockerTools.js +175 -0
- package/build/Plugin.d.ts +103 -1
- package/build/PromptTemplate.d.ts +105 -0
- package/build/PromptTemplate.js +194 -0
- package/build/Schema.d.ts +1 -1
- package/build/Schema.js +23 -0
- package/build/Scratch.d.ts +36 -0
- package/build/Scratch.js +29 -0
- package/build/WebAuth.d.ts +166 -0
- package/build/WebAuth.js +18 -0
- package/build/WebMenu.d.ts +54 -0
- package/build/WebMenu.js +23 -0
- package/build/WebPlugin.d.ts +137 -0
- package/build/WebPlugin.js +13 -0
- package/build/WebServerDefaults.d.ts +9 -0
- package/build/WebServerDefaults.js +9 -0
- package/build/WebStaticAssets.d.ts +72 -0
- package/build/WebStaticAssets.js +204 -0
- package/build/index.d.ts +10 -1
- package/build/index.js +10 -0
- package/package.json +2 -1
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Maximum nesting of template sources (includes and template-valued
|
|
3
|
+
* placeholders like `HANDLER_CONTENT`) below the top-level template.
|
|
4
|
+
*/
|
|
5
|
+
export declare const MAX_TEMPLATE_DEPTH = 3;
|
|
6
|
+
/** One node of a parsed template. */
|
|
7
|
+
export type TemplateNode = {
|
|
8
|
+
readonly type: "text";
|
|
9
|
+
readonly text: string;
|
|
10
|
+
} | {
|
|
11
|
+
readonly type: "placeholder";
|
|
12
|
+
readonly name: string;
|
|
13
|
+
} | {
|
|
14
|
+
readonly type: "include";
|
|
15
|
+
readonly path: string;
|
|
16
|
+
readonly children: readonly TemplateNode[];
|
|
17
|
+
} | {
|
|
18
|
+
readonly type: "source";
|
|
19
|
+
readonly name: string;
|
|
20
|
+
readonly children: readonly TemplateNode[];
|
|
21
|
+
};
|
|
22
|
+
/** A placeholder reference found while parsing, with its position relative to the marker. */
|
|
23
|
+
export interface PlaceholderUse {
|
|
24
|
+
readonly name: string;
|
|
25
|
+
/** `true` when the reference ends up in the system prompt (before `{CACHE_MARKER}`). */
|
|
26
|
+
readonly isBeforeMarker: boolean;
|
|
27
|
+
}
|
|
28
|
+
/** Result of {@link parseTemplate}. */
|
|
29
|
+
export interface ParsedTemplate {
|
|
30
|
+
/** Nodes rendered into the system prompt. */
|
|
31
|
+
readonly systemNodes: readonly TemplateNode[];
|
|
32
|
+
/** Nodes rendered into the head of the current user message. */
|
|
33
|
+
readonly userHeadNodes: readonly TemplateNode[];
|
|
34
|
+
/** Every placeholder reference, in template order (duplicates kept). */
|
|
35
|
+
readonly uses: readonly PlaceholderUse[];
|
|
36
|
+
/** Workspace files pulled in via `{path.md}`, in template order. */
|
|
37
|
+
readonly includes: readonly string[];
|
|
38
|
+
/** Problems found while parsing, phrased for a log line. */
|
|
39
|
+
readonly warnings: readonly string[];
|
|
40
|
+
}
|
|
41
|
+
/** Inputs to {@link parseTemplate}. */
|
|
42
|
+
export interface ParseTemplateOptions {
|
|
43
|
+
/** Human-readable name of the template, used in warnings (e.g. `PROMPT.md`). */
|
|
44
|
+
readonly templateName: string;
|
|
45
|
+
/**
|
|
46
|
+
* Read a workspace-relative include. Returns the (already truncated)
|
|
47
|
+
* text, or `null` when the file does not exist.
|
|
48
|
+
*/
|
|
49
|
+
readonly readInclude: (relativePath: string) => string | null;
|
|
50
|
+
/**
|
|
51
|
+
* Placeholders whose value is itself template text (workspace
|
|
52
|
+
* markdown, e.g. `HANDLER_CONTENT`). They are expanded like includes
|
|
53
|
+
* but not redacted.
|
|
54
|
+
*/
|
|
55
|
+
readonly templateSources: Readonly<Record<string, string>>;
|
|
56
|
+
}
|
|
57
|
+
/** Rendered output of {@link renderTemplate}. */
|
|
58
|
+
export interface RenderedTemplate {
|
|
59
|
+
readonly system: string;
|
|
60
|
+
readonly userHead: string;
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Parse a prompt template into nodes, expanding workspace includes and
|
|
64
|
+
* template-valued placeholders recursively.
|
|
65
|
+
*
|
|
66
|
+
* Only workspace markdown is template: the top-level template, files it
|
|
67
|
+
* includes, and {@link ParseTemplateOptions.templateSources}. Values
|
|
68
|
+
* produced by code or plugins are inserted later by
|
|
69
|
+
* {@link renderTemplate} and never scanned for tokens, so text from an
|
|
70
|
+
* event (a mail containing `{SOUL.md}`) cannot pull files into the
|
|
71
|
+
* prompt.
|
|
72
|
+
*
|
|
73
|
+
* The first `{CACHE_MARKER}` in the top-level template splits system
|
|
74
|
+
* prompt from user-message head. A marker anywhere else (an include, the
|
|
75
|
+
* handler body, a second one) is dropped with a warning.
|
|
76
|
+
*
|
|
77
|
+
* Problems never throw: an invalid include path, a missing include, a
|
|
78
|
+
* cycle or excessive nesting leave a warning and keep the token
|
|
79
|
+
* verbatim (or empty, for a missing file) so the run still proceeds.
|
|
80
|
+
*
|
|
81
|
+
* @param source The top-level template text.
|
|
82
|
+
* @param options Include reader, template sources and naming.
|
|
83
|
+
* @returns The parsed template.
|
|
84
|
+
*/
|
|
85
|
+
export declare function parseTemplate(source: string, options: ParseTemplateOptions): ParsedTemplate;
|
|
86
|
+
/**
|
|
87
|
+
* Render a parsed template with resolved placeholder values. A
|
|
88
|
+
* placeholder without a value stays as its literal `{NAME}` token.
|
|
89
|
+
*
|
|
90
|
+
* @param parsed The parsed template.
|
|
91
|
+
* @param values Resolved value per placeholder name.
|
|
92
|
+
* @param shouldRedactIncludes When `true`, each `{path.md}` include is
|
|
93
|
+
* rendered as `<content of file path>` instead of its body — used for
|
|
94
|
+
* the audit-log copy under `core.logSystemPrompt: non-static`.
|
|
95
|
+
* @returns System prompt and user-message head, each trimmed.
|
|
96
|
+
*/
|
|
97
|
+
export declare function renderTemplate(parsed: ParsedTemplate, values: ReadonlyMap<string, string>, shouldRedactIncludes?: boolean): RenderedTemplate;
|
|
98
|
+
/**
|
|
99
|
+
* Whether an include path stays inside the workspace: relative, no
|
|
100
|
+
* `..` segment, no empty segment.
|
|
101
|
+
*
|
|
102
|
+
* @param includePath The path inside the braces.
|
|
103
|
+
* @returns `true` when the path is acceptable.
|
|
104
|
+
*/
|
|
105
|
+
export declare function isSafeIncludePath(includePath: string): boolean;
|
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
import { CACHE_MARKER_PLACEHOLDER } from "./PromptPlaceholders.js";
|
|
2
|
+
/**
|
|
3
|
+
* Matches one template token: `{NAME}` (upper-case placeholder) or
|
|
4
|
+
* `{path.md}` (workspace include). Anything else in braces — JSON
|
|
5
|
+
* examples, `{lowercase}` prose — is left untouched.
|
|
6
|
+
*/
|
|
7
|
+
const TOKEN_PATTERN = /\{([A-Z][A-Z0-9_]*|[\w./-]+\.md)\}/g;
|
|
8
|
+
/**
|
|
9
|
+
* Maximum nesting of template sources (includes and template-valued
|
|
10
|
+
* placeholders like `HANDLER_CONTENT`) below the top-level template.
|
|
11
|
+
*/
|
|
12
|
+
export const MAX_TEMPLATE_DEPTH = 3;
|
|
13
|
+
/**
|
|
14
|
+
* Parse a prompt template into nodes, expanding workspace includes and
|
|
15
|
+
* template-valued placeholders recursively.
|
|
16
|
+
*
|
|
17
|
+
* Only workspace markdown is template: the top-level template, files it
|
|
18
|
+
* includes, and {@link ParseTemplateOptions.templateSources}. Values
|
|
19
|
+
* produced by code or plugins are inserted later by
|
|
20
|
+
* {@link renderTemplate} and never scanned for tokens, so text from an
|
|
21
|
+
* event (a mail containing `{SOUL.md}`) cannot pull files into the
|
|
22
|
+
* prompt.
|
|
23
|
+
*
|
|
24
|
+
* The first `{CACHE_MARKER}` in the top-level template splits system
|
|
25
|
+
* prompt from user-message head. A marker anywhere else (an include, the
|
|
26
|
+
* handler body, a second one) is dropped with a warning.
|
|
27
|
+
*
|
|
28
|
+
* Problems never throw: an invalid include path, a missing include, a
|
|
29
|
+
* cycle or excessive nesting leave a warning and keep the token
|
|
30
|
+
* verbatim (or empty, for a missing file) so the run still proceeds.
|
|
31
|
+
*
|
|
32
|
+
* @param source The top-level template text.
|
|
33
|
+
* @param options Include reader, template sources and naming.
|
|
34
|
+
* @returns The parsed template.
|
|
35
|
+
*/
|
|
36
|
+
export function parseTemplate(source, options) {
|
|
37
|
+
const state = {
|
|
38
|
+
options,
|
|
39
|
+
uses: [],
|
|
40
|
+
includes: [],
|
|
41
|
+
warnings: [],
|
|
42
|
+
isMarkerSeen: false,
|
|
43
|
+
systemNodes: [],
|
|
44
|
+
userHeadNodes: [],
|
|
45
|
+
};
|
|
46
|
+
parseInto(source, 0, [options.templateName], state, true);
|
|
47
|
+
return {
|
|
48
|
+
systemNodes: state.systemNodes,
|
|
49
|
+
userHeadNodes: state.userHeadNodes,
|
|
50
|
+
uses: state.uses,
|
|
51
|
+
includes: state.includes,
|
|
52
|
+
warnings: state.warnings,
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Render a parsed template with resolved placeholder values. A
|
|
57
|
+
* placeholder without a value stays as its literal `{NAME}` token.
|
|
58
|
+
*
|
|
59
|
+
* @param parsed The parsed template.
|
|
60
|
+
* @param values Resolved value per placeholder name.
|
|
61
|
+
* @param shouldRedactIncludes When `true`, each `{path.md}` include is
|
|
62
|
+
* rendered as `<content of file path>` instead of its body — used for
|
|
63
|
+
* the audit-log copy under `core.logSystemPrompt: non-static`.
|
|
64
|
+
* @returns System prompt and user-message head, each trimmed.
|
|
65
|
+
*/
|
|
66
|
+
export function renderTemplate(parsed, values, shouldRedactIncludes = false) {
|
|
67
|
+
return {
|
|
68
|
+
system: renderNodes(parsed.systemNodes, values, shouldRedactIncludes).trim(),
|
|
69
|
+
userHead: renderNodes(parsed.userHeadNodes, values, shouldRedactIncludes).trim(),
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Parse `source` and append its nodes to the active top-level list (or
|
|
74
|
+
* to `into`, for nested sources).
|
|
75
|
+
*
|
|
76
|
+
* @param source Text to parse.
|
|
77
|
+
* @param depth Nesting depth; 0 is the top-level template.
|
|
78
|
+
* @param chain Names of the sources currently being expanded, for cycle
|
|
79
|
+
* detection and warning context.
|
|
80
|
+
* @param state Shared parse state.
|
|
81
|
+
* @param isTopLevel Whether nodes go to the top-level lists (`true`) or
|
|
82
|
+
* are returned for a nested node.
|
|
83
|
+
* @returns The nodes parsed from a nested source (empty at top level).
|
|
84
|
+
*/
|
|
85
|
+
function parseInto(source, depth, chain, state, isTopLevel) {
|
|
86
|
+
const nested = [];
|
|
87
|
+
const push = (node) => {
|
|
88
|
+
if (!isTopLevel) {
|
|
89
|
+
nested.push(node);
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
92
|
+
(state.isMarkerSeen ? state.userHeadNodes : state.systemNodes).push(node);
|
|
93
|
+
};
|
|
94
|
+
const where = chain[chain.length - 1];
|
|
95
|
+
let cursor = 0;
|
|
96
|
+
for (const match of source.matchAll(TOKEN_PATTERN)) {
|
|
97
|
+
const index = match.index ?? 0;
|
|
98
|
+
if (index > cursor) {
|
|
99
|
+
push({ type: "text", text: source.slice(cursor, index) });
|
|
100
|
+
}
|
|
101
|
+
cursor = index + match[0].length;
|
|
102
|
+
const token = match[1] ?? "";
|
|
103
|
+
if (token === CACHE_MARKER_PLACEHOLDER) {
|
|
104
|
+
if (depth === 0 && !state.isMarkerSeen) {
|
|
105
|
+
state.isMarkerSeen = true;
|
|
106
|
+
continue;
|
|
107
|
+
}
|
|
108
|
+
state.warnings.push(depth === 0
|
|
109
|
+
? `${where}: second {${CACHE_MARKER_PLACEHOLDER}} ignored — only the first one splits the prompt`
|
|
110
|
+
: `${where}: {${CACHE_MARKER_PLACEHOLDER}} ignored — it only works in the top-level template`);
|
|
111
|
+
continue;
|
|
112
|
+
}
|
|
113
|
+
if (token.endsWith(".md")) {
|
|
114
|
+
push(parseInclude(token, match[0], depth, chain, state));
|
|
115
|
+
continue;
|
|
116
|
+
}
|
|
117
|
+
const templateSource = state.options.templateSources[token];
|
|
118
|
+
if (templateSource !== undefined) {
|
|
119
|
+
if (depth >= MAX_TEMPLATE_DEPTH || chain.includes(`{${token}}`)) {
|
|
120
|
+
state.warnings.push(`${where}: {${token}} not expanded — nested too deep or recursive (${chain.join(" → ")})`);
|
|
121
|
+
push({ type: "text", text: match[0] });
|
|
122
|
+
continue;
|
|
123
|
+
}
|
|
124
|
+
const children = parseInto(templateSource, depth + 1, [...chain, `{${token}}`], state, false);
|
|
125
|
+
push({ type: "source", name: token, children });
|
|
126
|
+
continue;
|
|
127
|
+
}
|
|
128
|
+
state.uses.push({ name: token, isBeforeMarker: !state.isMarkerSeen });
|
|
129
|
+
push({ type: "placeholder", name: token });
|
|
130
|
+
}
|
|
131
|
+
if (cursor < source.length) {
|
|
132
|
+
push({ type: "text", text: source.slice(cursor) });
|
|
133
|
+
}
|
|
134
|
+
return nested;
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* Build the node for one `{path.md}` token, reading and recursively
|
|
138
|
+
* parsing the file.
|
|
139
|
+
*/
|
|
140
|
+
function parseInclude(includePath, literal, depth, chain, state) {
|
|
141
|
+
const where = chain[chain.length - 1];
|
|
142
|
+
if (!isSafeIncludePath(includePath)) {
|
|
143
|
+
state.warnings.push(`${where}: include {${includePath}} rejected — use a workspace-relative path without ".."`);
|
|
144
|
+
return { type: "text", text: literal };
|
|
145
|
+
}
|
|
146
|
+
if (depth >= MAX_TEMPLATE_DEPTH || chain.includes(includePath)) {
|
|
147
|
+
state.warnings.push(`${where}: include {${includePath}} not expanded — nested too deep or recursive (${[...chain, includePath].join(" → ")})`);
|
|
148
|
+
return { type: "text", text: literal };
|
|
149
|
+
}
|
|
150
|
+
const body = state.options.readInclude(includePath);
|
|
151
|
+
if (body === null) {
|
|
152
|
+
state.warnings.push(`${where}: include {${includePath}} not found in the workspace`);
|
|
153
|
+
return { type: "include", path: includePath, children: [] };
|
|
154
|
+
}
|
|
155
|
+
state.includes.push(includePath);
|
|
156
|
+
const children = parseInto(body, depth + 1, [...chain, includePath], state, false);
|
|
157
|
+
return { type: "include", path: includePath, children };
|
|
158
|
+
}
|
|
159
|
+
/**
|
|
160
|
+
* Whether an include path stays inside the workspace: relative, no
|
|
161
|
+
* `..` segment, no empty segment.
|
|
162
|
+
*
|
|
163
|
+
* @param includePath The path inside the braces.
|
|
164
|
+
* @returns `true` when the path is acceptable.
|
|
165
|
+
*/
|
|
166
|
+
export function isSafeIncludePath(includePath) {
|
|
167
|
+
if (includePath.startsWith("/")) {
|
|
168
|
+
return false;
|
|
169
|
+
}
|
|
170
|
+
return includePath.split("/").every((segment) => segment !== ".." && segment.length > 0);
|
|
171
|
+
}
|
|
172
|
+
/** Concatenate the rendered text of `nodes`. */
|
|
173
|
+
function renderNodes(nodes, values, shouldRedactIncludes) {
|
|
174
|
+
let out = "";
|
|
175
|
+
for (const node of nodes) {
|
|
176
|
+
switch (node.type) {
|
|
177
|
+
case "text":
|
|
178
|
+
out += node.text;
|
|
179
|
+
break;
|
|
180
|
+
case "placeholder":
|
|
181
|
+
out += values.get(node.name) ?? `{${node.name}}`;
|
|
182
|
+
break;
|
|
183
|
+
case "include":
|
|
184
|
+
out += shouldRedactIncludes
|
|
185
|
+
? `<content of file ${node.path}>`
|
|
186
|
+
: renderNodes(node.children, values, shouldRedactIncludes);
|
|
187
|
+
break;
|
|
188
|
+
case "source":
|
|
189
|
+
out += renderNodes(node.children, values, shouldRedactIncludes);
|
|
190
|
+
break;
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
return out;
|
|
194
|
+
}
|
package/build/Schema.d.ts
CHANGED
|
@@ -62,7 +62,7 @@ export declare const SCHEDULED_SUBAGENTS_CHANNEL = "scheduled_subagents_changed"
|
|
|
62
62
|
* match exactly (see `HandlerResolution.ts`).
|
|
63
63
|
*/
|
|
64
64
|
export declare const TOPIC_PATTERN = "^[\\w-]+(:[\\w-]+)*$";
|
|
65
|
-
export declare const SCHEMA_SQL = "\n-- Drop legacy single-table artifacts from before the events/agentruns\n-- split. Safe no-ops on fresh databases. The current schema reuses the\n-- name events_notify_new() for its own NOTIFY function, so we do NOT\n-- drop it here (CREATE OR REPLACE FUNCTION below handles the update);\n-- dropping would fail anyway once the current events_new_trg exists.\nDROP TRIGGER IF EXISTS events_notify_changed_trg ON events;\nDROP TRIGGER IF EXISTS events_notify_new_trg ON events;\nDROP FUNCTION IF EXISTS events_notify_changed();\nDROP INDEX IF EXISTS events_pending_priority_idx;\nDROP INDEX IF EXISTS events_state_priority_idx;\n\nCREATE TABLE IF NOT EXISTS events (\n id bigserial PRIMARY KEY,\n topic text NOT NULL,\n priority smallint NOT NULL DEFAULT 50,\n state text NOT NULL DEFAULT 'pending',\n payload jsonb NOT NULL DEFAULT '{}'::jsonb,\n idempotency_key text UNIQUE,\n privileged boolean NOT NULL DEFAULT false,\n created_at timestamptz NOT NULL DEFAULT now(),\n updated_at timestamptz NOT NULL DEFAULT now(),\n CONSTRAINT events_topic_format CHECK (topic ~ '^[\\w-]+(:[\\w-]+)*$')\n);\n\n-- Drop legacy columns from earlier schema generations. Single-instance\n-- project; column drops are fine.\nALTER TABLE events DROP COLUMN IF EXISTS supervisor_prompt;\nALTER TABLE events DROP COLUMN IF EXISTS causation_chain;\n\n-- Chat-aware event metadata. `is_chat` opts into automatic user-message\n-- persistence in EventBus.add; `preferred_chat_channel_id` names the\n-- channel an assistant reply should reach the user on. The container is\n-- channel-blind \u2014 both fields are read host-side (for routing) or via\n-- JOIN inside the chatmessages NOTIFY trigger.\nALTER TABLE events ADD COLUMN IF NOT EXISTS is_chat boolean NOT NULL DEFAULT false;\nALTER TABLE events ADD COLUMN IF NOT EXISTS preferred_chat_channel_id text;\n-- Required for non-chat events (validated at `EventBus.add` time and at\n-- the type level via `NewEvent`'s discriminated union); always null for\n-- chat events because the user's text in `chatmessages` already serves\n-- as the agent-visible \"what happened\" message. The EventWatcher copies\n-- this verbatim into the root `agentruns.prompt` so the agent sees the\n-- emitter's framing without the EventWatcher having to reach into the\n-- payload schema.\nALTER TABLE events ADD COLUMN IF NOT EXISTS prompt text;\n-- The topic is the handler's whole address now; the separate start\n-- handler basename is gone.\nALTER TABLE events DROP COLUMN IF EXISTS start_handler;\n-- When true the topic must name an existing handler file exactly; when\n-- false (world events) handler resolution falls back up the topic chain.\n-- Copied onto the root agentrun by the input-event watcher.\nALTER TABLE events ADD COLUMN IF NOT EXISTS is_exact_topic boolean NOT NULL DEFAULT false;\n-- Privileged events originate from a trusted user-input source (operator\n-- at the local terminal via cli-chat, operator on Telegram). The flag\n-- propagates verbatim to the root agentrun and to every child agentrun\n-- spawned via schedule_subagent / start_subagent, so future system tools can\n-- gate risky reads / writes (editing SOUL.md etc.) on whether the run\n-- descends from a trusted input. Default false: any plugin that doesn't\n-- explicitly opt in produces non-privileged events.\nALTER TABLE events ADD COLUMN IF NOT EXISTS privileged boolean NOT NULL DEFAULT false;\n-- When true and the event ends in 'failed', `HostContextImpl.emitAndAwait`\n-- writes a role='assistant' chatmessage with the failure text before\n-- rejecting handle.settled. Default false: only chat plugins (cli-chat,\n-- telegram) opt in. See [[outputChatOnFailure]] on NewEvent for the\n-- rationale.\nALTER TABLE events ADD COLUMN IF NOT EXISTS output_chat_on_failure boolean NOT NULL DEFAULT false;\n\nCREATE INDEX IF NOT EXISTS events_state_priority_idx\n ON events (state, priority DESC, id ASC);\nCREATE INDEX IF NOT EXISTS events_topic_idx ON events (topic);\n\nCREATE TABLE IF NOT EXISTS agentruns (\n id bigserial PRIMARY KEY,\n event_id bigint NOT NULL REFERENCES events(id) ON DELETE CASCADE,\n parent_agentrun_id bigint REFERENCES agentruns(id) ON DELETE CASCADE,\n topic text NOT NULL,\n priority smallint NOT NULL DEFAULT 50,\n state text NOT NULL DEFAULT 'pending',\n prompt text,\n payload jsonb NOT NULL DEFAULT '{}'::jsonb,\n result jsonb,\n result_text text,\n error text,\n privileged boolean NOT NULL DEFAULT false,\n created_at timestamptz NOT NULL DEFAULT now(),\n updated_at timestamptz NOT NULL DEFAULT now(),\n CONSTRAINT agentruns_topic_format CHECK (topic ~ '^[\\w-]+(:[\\w-]+)*$')\n);\n\n-- Forward-compat ALTER for databases created before result_text landed.\n-- Safe no-op on fresh databases where the column already exists.\nALTER TABLE agentruns ADD COLUMN IF NOT EXISTS result_text text;\n-- The topic is the handler's whole address; the basename column is gone.\nALTER TABLE agentruns DROP COLUMN IF EXISTS handler;\n-- Whether handler resolution must match the topic exactly. Root\n-- agentruns copy the event's flag; spawned children are always exact.\nALTER TABLE agentruns ADD COLUMN IF NOT EXISTS is_exact_topic boolean NOT NULL DEFAULT true;\n-- Inherited from the originating event (root agentrun) or parent\n-- agentrun (children spawned via schedule_subagent / start_subagent). See\n-- the matching ALTER on the events table for the trust-model rationale.\nALTER TABLE agentruns ADD COLUMN IF NOT EXISTS privileged boolean NOT NULL DEFAULT false;\n-- Number of retry attempts already made on this agentrun. Bumped each\n-- time AgentRunner postpones the row due to a retryable inference\n-- error (APICallError.isRetryable). Capped per-handler via maxRetries\n-- (frontmatter override) or globally via inference.maxRetries.\nALTER TABLE agentruns ADD COLUMN IF NOT EXISTS retry_count int NOT NULL DEFAULT 0;\n-- Earliest moment the agentrun watcher is allowed to re-claim this\n-- row. NULL = claim immediately (default for fresh rows). A future\n-- timestamp = leave the row alone until then; the watcher's claim\n-- query gates on this so a parked row doesn't waste the slot while\n-- other agentruns (potentially using different models) can still run.\nALTER TABLE agentruns ADD COLUMN IF NOT EXISTS not_before timestamptz;\n-- Resolved model id for this agentrun, of the form\n-- `<provider>/<modelId>` (e.g. `featherless/zai-org/GLM-5.1`).\n-- Populated by AgentRunner once it builds the model client; stays\n-- NULL for any agentrun that never reached generate() (failed at\n-- handler-load time, etc.).\nALTER TABLE agentruns ADD COLUMN IF NOT EXISTS model text;\n-- Resolved prompt (rendered prompt template: system prompt, then\n-- user-message head) used for this agentrun. Populated by AgentRunner\n-- only when core.logSystemPrompt is true; otherwise NULL. Surfaced\n-- by the report layer's Agentrun Start section when the consumer\n-- opts into withDetails.\nALTER TABLE agentruns ADD COLUMN IF NOT EXISTS system_prompt text;\n-- Verbatim model-message array assembled for the first agent.generate()\n-- call (prior history + the run's leading user turn). Populated by\n-- AgentRunner only when inference.captureInitialMessageHistory is true;\n-- otherwise NULL. Surfaced by the report layer's -vv view so an operator\n-- can see exactly what was sent into the model loop.\nALTER TABLE agentruns ADD COLUMN IF NOT EXISTS initial_messages jsonb;\n\n-- Distinguishes how a child agentrun was spawned.\n-- NULL \u2014 root agentrun (created by the input-event watcher, no parent).\n-- 'scheduled' \u2014 fire-and-forget child spawned by `schedule_subagent` without\n-- a future `when` (immediate dispatch under the same event).\n-- 'started' \u2014 synchronous child spawned via the `start_subagent` tool; the\n-- parent is parked in state='waiting' until this row settles.\n-- The Scheduler reads this column to decide whether settling a row should\n-- wake a waiting parent.\nALTER TABLE agentruns ADD COLUMN IF NOT EXISTS calltype text;\nALTER TABLE agentruns DROP CONSTRAINT IF EXISTS agentruns_calltype_format;\nALTER TABLE agentruns ADD CONSTRAINT agentruns_calltype_format\n CHECK (calltype IS NULL OR calltype IN ('scheduled','started'));\n\n-- Partial index for the \"are all started children of $parent settled?\"\n-- check the Scheduler runs after every 'started' child settles. Indexes\n-- only the rows that actually participate in the question.\nCREATE INDEX IF NOT EXISTS agentruns_parent_calltype_state_idx\n ON agentruns (parent_agentrun_id, state)\n WHERE calltype = 'started';\n\n-- Forward-compat: relax the topic CHECK constraint to allow arbitrarily\n-- deep `a:b:c:\u2026` topics. CREATE TABLE IF NOT EXISTS above doesn't\n-- update existing constraints, so we drop+re-add unconditionally. The\n-- DROP IF EXISTS / ADD pair is idempotent.\nALTER TABLE events DROP CONSTRAINT IF EXISTS events_topic_format;\nALTER TABLE events ADD CONSTRAINT events_topic_format CHECK (topic ~ '^[\\w-]+(:[\\w-]+)*$');\nALTER TABLE agentruns DROP CONSTRAINT IF EXISTS agentruns_topic_format;\nALTER TABLE agentruns ADD CONSTRAINT agentruns_topic_format CHECK (topic ~ '^[\\w-]+(:[\\w-]+)*$');\n\nCREATE INDEX IF NOT EXISTS agentruns_state_priority_idx\n ON agentruns (state, priority DESC, id ASC);\nCREATE INDEX IF NOT EXISTS agentruns_event_id_idx ON agentruns (event_id);\nCREATE INDEX IF NOT EXISTS agentruns_parent_idx ON agentruns (parent_agentrun_id);\n\nCREATE TABLE IF NOT EXISTS stepresults (\n id bigserial PRIMARY KEY,\n agent_run_id bigint NOT NULL REFERENCES agentruns(id) ON DELETE CASCADE,\n event_id bigint NOT NULL REFERENCES events(id) ON DELETE CASCADE,\n step_number int NOT NULL,\n finish_reason text NOT NULL,\n result_text text,\n reasoning_text text,\n input_tokens int,\n output_tokens int,\n total_tokens int,\n tool_call_count int NOT NULL DEFAULT 0,\n tool_calls jsonb NOT NULL DEFAULT '[]'::jsonb,\n tool_results jsonb NOT NULL DEFAULT '[]'::jsonb,\n created_at timestamptz NOT NULL DEFAULT now(),\n CONSTRAINT stepresults_step_number_unique UNIQUE (agent_run_id, step_number)\n);\n\nCREATE INDEX IF NOT EXISTS stepresults_agent_run_id_idx ON stepresults (agent_run_id);\nCREATE INDEX IF NOT EXISTS stepresults_event_id_idx ON stepresults (event_id);\n\n-- AI SDK per-step usage breakdown\n-- (LanguageModelUsage.inputTokenDetails / outputTokenDetails). Each\n-- column is NULL when the provider doesn't report that detail.\nALTER TABLE stepresults ADD COLUMN IF NOT EXISTS input_tokens_no_cache int;\nALTER TABLE stepresults ADD COLUMN IF NOT EXISTS input_tokens_cache_read int;\nALTER TABLE stepresults ADD COLUMN IF NOT EXISTS input_tokens_cache_write int;\nALTER TABLE stepresults ADD COLUMN IF NOT EXISTS output_tokens_text int;\nALTER TABLE stepresults ADD COLUMN IF NOT EXISTS output_tokens_reasoning int;\n-- Full step object snapshot, populated only when the operator opts\n-- in via inference.captureRawStepResultToDatabase: true. Used to\n-- inspect provider-specific fields (Anthropic cache control,\n-- OpenAI logprobs, ...) that don't fit dedicated columns.\nALTER TABLE stepresults ADD COLUMN IF NOT EXISTS raw_result jsonb;\n\n-- \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500 chatmessages \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\n--\n-- Persistent chat history across all channels. Each row links to the\n-- event whose lifecycle the message belongs to:\n-- * For role='user': the event the host plugin emitted with isChat=true.\n-- * For role='assistant': the event whose agentrun called send_chat.\n-- The channel is reachable via JOIN to events.preferred_chat_channel_id;\n-- there is intentionally no channel_id column here \u2014 channel routing is\n-- a host-side concern, the container never names a channel.\n--\n-- delivered_at is set when ANY listener returns true from its async\n-- handler. New subscribers replay all undelivered matching rows on\n-- registration so a message produced when nobody was listening is still\n-- delivered later.\nCREATE TABLE IF NOT EXISTS chatmessages (\n id bigserial PRIMARY KEY,\n event_id bigint NOT NULL REFERENCES events(id) ON DELETE CASCADE,\n role text NOT NULL CHECK (role IN ('user','assistant')),\n text_content text NOT NULL,\n created_at timestamptz NOT NULL DEFAULT now(),\n delivered_at timestamptz\n);\n\nCREATE INDEX IF NOT EXISTS chatmessages_event_idx\n ON chatmessages (event_id);\nCREATE INDEX IF NOT EXISTS chatmessages_undelivered_idx\n ON chatmessages (created_at) WHERE delivered_at IS NULL;\n\n-- \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500 NOTIFY triggers \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\n\nCREATE OR REPLACE FUNCTION events_notify_new() RETURNS trigger AS $$\nBEGIN\n PERFORM pg_notify('events_new', NEW.id::text);\n RETURN NEW;\nEND;\n$$ LANGUAGE plpgsql;\n\nDROP TRIGGER IF EXISTS events_new_trg ON events;\nCREATE TRIGGER events_new_trg\n AFTER INSERT ON events\n FOR EACH ROW EXECUTE FUNCTION events_notify_new();\n\nCREATE OR REPLACE FUNCTION events_notify_state() RETURNS trigger AS $$\nBEGIN\n PERFORM pg_notify('events_state', NEW.id::text || ':' || NEW.state);\n RETURN NEW;\nEND;\n$$ LANGUAGE plpgsql;\n\nDROP TRIGGER IF EXISTS events_state_trg ON events;\nCREATE TRIGGER events_state_trg\n AFTER UPDATE OF state ON events\n FOR EACH ROW EXECUTE FUNCTION events_notify_state();\n\nCREATE OR REPLACE FUNCTION agentruns_notify_changed() RETURNS trigger AS $$\nBEGIN\n PERFORM pg_notify(\n 'agentruns_changed',\n NEW.event_id::text || ':' || NEW.id::text\n );\n RETURN NEW;\nEND;\n$$ LANGUAGE plpgsql;\n\nDROP TRIGGER IF EXISTS agentruns_notify_changed_trg ON agentruns;\nCREATE TRIGGER agentruns_notify_changed_trg\n AFTER INSERT OR UPDATE OF state ON agentruns\n FOR EACH ROW EXECUTE FUNCTION agentruns_notify_changed();\n\nCREATE OR REPLACE FUNCTION stepresults_notify_new() RETURNS trigger AS $$\nBEGIN\n PERFORM pg_notify(\n 'stepresults_new',\n NEW.event_id::text || ':' || NEW.agent_run_id::text || ':' || NEW.id::text\n );\n RETURN NEW;\nEND;\n$$ LANGUAGE plpgsql;\n\nDROP TRIGGER IF EXISTS stepresults_new_trg ON stepresults;\nCREATE TRIGGER stepresults_new_trg\n AFTER INSERT ON stepresults\n FOR EACH ROW EXECUTE FUNCTION stepresults_notify_new();\n\n-- chatmessages NOTIFY: payload is \"<channel_id>:<role>:<id>\". Channel\n-- comes from the parent event's preferred_chat_channel_id, JOINed at\n-- trigger time. Empty channel becomes empty prefix \u2014 listeners will\n-- filter it out unless they explicitly subscribe to channelId=\"\".\nCREATE OR REPLACE FUNCTION chatmessages_notify_new() RETURNS trigger AS $$\nDECLARE\n channel text;\nBEGIN\n SELECT preferred_chat_channel_id INTO channel FROM events WHERE id = NEW.event_id;\n PERFORM pg_notify(\n 'chatmessages_new',\n COALESCE(channel, '') || ':' || NEW.role || ':' || NEW.id::text\n );\n RETURN NEW;\nEND;\n$$ LANGUAGE plpgsql;\n\nDROP TRIGGER IF EXISTS chatmessages_new_trg ON chatmessages;\nCREATE TRIGGER chatmessages_new_trg\n AFTER INSERT ON chatmessages\n FOR EACH ROW EXECUTE FUNCTION chatmessages_notify_new();\n\n-- \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500 calendars + calendar_events \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\n--\n-- Plugin-agnostic calendar data layer. `calendars` lists every\n-- subscription (own or shared) any provider plugin (ms365, future\n-- google/caldav) has surfaced; `calendar_events` stores every\n-- instance. Providers persist every occurrence of a recurring\n-- series independently (no master-expansion in core).\n--\n-- Identity:\n-- * `calendars.unique_key` is the provider's stable id. Uniqueness\n-- within `(plugin_id, unique_key)` lets multiple providers\n-- coexist.\n-- * `calendar_events.id` is provider-prefixed (e.g. `ms365:<graph-id>`)\n-- so collisions across providers are impossible by construction.\n--\n-- Refresh reconciliation uses a scan-generation tag: a refresh walk\n-- bumps `calendars.scan_generation`, upserts every event in window\n-- with the new value, then DELETEs events whose generation is older\n-- than the calendar's current value.\n\nCREATE TABLE IF NOT EXISTS calendars (\n id bigserial PRIMARY KEY,\n plugin_id text NOT NULL,\n unique_key text NOT NULL,\n name text NOT NULL,\n type text NOT NULL CHECK (type IN ('own','shared')),\n owner_name text,\n scan_generation int NOT NULL DEFAULT 0,\n created_at timestamptz NOT NULL DEFAULT now(),\n UNIQUE (plugin_id, unique_key)\n);\n\n-- The provider's own \"default calendar\" flag (Graph's\n-- `isDefaultCalendar`). Used by `CalendarService.resolveDefaultCalendar`\n-- as the fallback when `core.defaultCalendar` is unset \u2014 we pick the\n-- first registered calendar (lowest id) flagged here. Added via\n-- ALTER so existing databases don't need a schema reset.\nALTER TABLE calendars ADD COLUMN IF NOT EXISTS is_default smallint NOT NULL DEFAULT 0;\n\nCREATE INDEX IF NOT EXISTS calendars_plugin_id_idx ON calendars (plugin_id);\n\nCREATE TABLE IF NOT EXISTS calendar_events (\n id text PRIMARY KEY,\n calendar_id bigint NOT NULL REFERENCES calendars(id) ON DELETE CASCADE,\n series_master_id text,\n type text NOT NULL CHECK (type IN ('singleInstance','occurrence','exception','seriesMaster')),\n subject text,\n start_dt text NOT NULL,\n end_dt text NOT NULL,\n event_tz text,\n is_all_day smallint NOT NULL DEFAULT 0,\n is_cancelled smallint NOT NULL DEFAULT 0,\n show_as text,\n sensitivity text,\n importance text,\n location text,\n is_online_meeting smallint NOT NULL DEFAULT 0,\n online_meeting_url text,\n organizer_name text,\n organizer_email text,\n response_status text,\n attendees_json jsonb,\n body text,\n attachments jsonb,\n scan_generation int NOT NULL,\n created_at timestamptz NOT NULL DEFAULT now(),\n updated_at timestamptz NOT NULL DEFAULT now()\n);\n\nCREATE INDEX IF NOT EXISTS calendar_events_window_idx\n ON calendar_events (calendar_id, start_dt);\nCREATE INDEX IF NOT EXISTS calendar_events_series_idx\n ON calendar_events (series_master_id);\nCREATE INDEX IF NOT EXISTS calendar_events_generation_idx\n ON calendar_events (calendar_id, scan_generation);\n\n-- \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500 scheduled_subagents \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\n--\n-- One-off agent-scheduled wake-ups. Each row is upserted by the\n-- container's `schedule_subagent` tool and read by the host-side\n-- `ScheduledSubagentScheduler`, which installs a Croner job per row\n-- and atomically deletes the row when it fires. `key` is the\n-- agent-supplied unique id used for replace-on-conflict semantics\n-- (\"rescheduling a key overwrites the previous timing\"). `fire_at`\n-- is the UTC firing time; the agent passes wall-clock-in-`core.timezone`\n-- and the container projects to UTC before insert. On daemon startup,\n-- past-due rows are deleted with a warn log; future rows are\n-- reinstalled. See [[feedback_agent_facing_format]] \u2014 UTC storage,\n-- local-tz projection at every agent-facing edge.\nCREATE TABLE IF NOT EXISTS scheduled_subagents (\n key text PRIMARY KEY,\n fire_at timestamptz NOT NULL,\n topic text NOT NULL,\n prompt text,\n payload jsonb NOT NULL DEFAULT '{}'::jsonb,\n priority smallint NOT NULL DEFAULT 50,\n privileged boolean NOT NULL DEFAULT false,\n created_at timestamptz NOT NULL DEFAULT now(),\n CONSTRAINT scheduled_subagents_topic_format CHECK (topic ~ '^[\\w-]+(:[\\w-]+)*$')\n);\n\n-- The topic is the handler's whole address; the basename column is gone.\nALTER TABLE scheduled_subagents DROP COLUMN IF EXISTS handler;\n-- Re-add the topic CHECK so a widened TOPIC_PATTERN reaches existing tables.\nALTER TABLE scheduled_subagents DROP CONSTRAINT IF EXISTS scheduled_subagents_topic_format;\nALTER TABLE scheduled_subagents ADD CONSTRAINT scheduled_subagents_topic_format CHECK (topic ~ '^[\\w-]+(:[\\w-]+)*$');\n\nCREATE INDEX IF NOT EXISTS scheduled_subagents_fire_at_idx\n ON scheduled_subagents (fire_at);\n\nCREATE OR REPLACE FUNCTION scheduled_subagents_notify_changed() RETURNS trigger AS $$\nDECLARE\n payload_key text;\n op text;\nBEGIN\n IF TG_OP = 'DELETE' THEN\n payload_key := OLD.key;\n op := 'd';\n ELSE\n payload_key := NEW.key;\n op := 'u';\n END IF;\n PERFORM pg_notify('scheduled_subagents_changed', payload_key || ':' || op);\n RETURN COALESCE(NEW, OLD);\nEND;\n$$ LANGUAGE plpgsql;\n\nDROP TRIGGER IF EXISTS scheduled_subagents_notify_changed_trg ON scheduled_subagents;\nCREATE TRIGGER scheduled_subagents_notify_changed_trg\n AFTER INSERT OR UPDATE OR DELETE ON scheduled_subagents\n FOR EACH ROW EXECUTE FUNCTION scheduled_subagents_notify_changed();\n\n-- \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500 inference_events \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\n--\n-- Append-only audit trail of every upstream model HTTP call attempt.\n-- Written by AgentRunner on each generate() outcome (success / retryable /\n-- fatal). Read by the host-side `inference_status` reflection tool to\n-- answer \"is this model misbehaving right now?\". One row per attempt;\n-- retries write multiple rows. NULL agent_run_id is kept for runs whose\n-- row was later deleted (parent event cascade) so the historical signal\n-- survives.\nCREATE TABLE IF NOT EXISTS inference_events (\n id bigserial PRIMARY KEY,\n provider text NOT NULL,\n model text NOT NULL,\n agent_run_id bigint REFERENCES agentruns(id) ON DELETE SET NULL,\n outcome text NOT NULL CHECK (outcome IN ('success','retryable','fatal')),\n status_code int,\n error_excerpt text,\n occurred_at timestamptz NOT NULL DEFAULT now()\n);\n\nCREATE INDEX IF NOT EXISTS inference_events_model_time_idx\n ON inference_events (model, occurred_at DESC);\nCREATE INDEX IF NOT EXISTS inference_events_occurred_at_idx\n ON inference_events (occurred_at DESC);\n\n-- \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500 tool_calls \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\n--\n-- Append-only record of every tool invocation an agentrun attempted,\n-- tagged with the resolved handler markdown path and whether the call\n-- threw. Written by the tool wrapper in the container's ToolsFactory on\n-- each execute() outcome. Read by the heuristic tool-preloader\n-- (ToolCallBus.topToolsForHandler): the tools a handler successfully\n-- used across its recent non-error runs get loaded up front on the next\n-- run, so warm handlers rarely need a tool_list / tool_call round-trip.\n-- NULL agent_run_id survives a parent-event cascade so the historical\n-- usage signal outlives the run row.\nCREATE TABLE IF NOT EXISTS tool_calls (\n id bigserial PRIMARY KEY,\n agent_run_id bigint REFERENCES agentruns(id) ON DELETE SET NULL,\n handler_path text NOT NULL,\n tool_name text NOT NULL,\n successful boolean NOT NULL,\n created_at timestamptz NOT NULL DEFAULT now()\n);\n\nCREATE INDEX IF NOT EXISTS tool_calls_handler_run_idx\n ON tool_calls (handler_path, agent_run_id DESC);\nCREATE INDEX IF NOT EXISTS tool_calls_created_at_idx\n ON tool_calls (created_at DESC);\n";
|
|
65
|
+
export declare const SCHEMA_SQL = "\n-- Drop legacy single-table artifacts from before the events/agentruns\n-- split. Safe no-ops on fresh databases. The current schema reuses the\n-- name events_notify_new() for its own NOTIFY function, so we do NOT\n-- drop it here (CREATE OR REPLACE FUNCTION below handles the update);\n-- dropping would fail anyway once the current events_new_trg exists.\nDROP TRIGGER IF EXISTS events_notify_changed_trg ON events;\nDROP TRIGGER IF EXISTS events_notify_new_trg ON events;\nDROP FUNCTION IF EXISTS events_notify_changed();\nDROP INDEX IF EXISTS events_pending_priority_idx;\nDROP INDEX IF EXISTS events_state_priority_idx;\n\nCREATE TABLE IF NOT EXISTS events (\n id bigserial PRIMARY KEY,\n topic text NOT NULL,\n priority smallint NOT NULL DEFAULT 50,\n state text NOT NULL DEFAULT 'pending',\n payload jsonb NOT NULL DEFAULT '{}'::jsonb,\n idempotency_key text UNIQUE,\n privileged boolean NOT NULL DEFAULT false,\n created_at timestamptz NOT NULL DEFAULT now(),\n updated_at timestamptz NOT NULL DEFAULT now(),\n CONSTRAINT events_topic_format CHECK (topic ~ '^[\\w-]+(:[\\w-]+)*$')\n);\n\n-- Drop legacy columns from earlier schema generations. Single-instance\n-- project; column drops are fine.\nALTER TABLE events DROP COLUMN IF EXISTS supervisor_prompt;\nALTER TABLE events DROP COLUMN IF EXISTS causation_chain;\n\n-- Chat-aware event metadata. `is_chat` opts into automatic user-message\n-- persistence in EventBus.add; `preferred_chat_channel_id` names the\n-- channel an assistant reply should reach the user on. The container is\n-- channel-blind \u2014 both fields are read host-side (for routing) or via\n-- JOIN inside the chatmessages NOTIFY trigger.\nALTER TABLE events ADD COLUMN IF NOT EXISTS is_chat boolean NOT NULL DEFAULT false;\nALTER TABLE events ADD COLUMN IF NOT EXISTS preferred_chat_channel_id text;\n-- Required for non-chat events (validated at `EventBus.add` time and at\n-- the type level via `NewEvent`'s discriminated union); always null for\n-- chat events because the user's text in `chatmessages` already serves\n-- as the agent-visible \"what happened\" message. The EventWatcher copies\n-- this verbatim into the root `agentruns.prompt` so the agent sees the\n-- emitter's framing without the EventWatcher having to reach into the\n-- payload schema.\nALTER TABLE events ADD COLUMN IF NOT EXISTS prompt text;\n-- The topic is the handler's whole address now; the separate start\n-- handler basename is gone.\nALTER TABLE events DROP COLUMN IF EXISTS start_handler;\n-- When true the topic must name an existing handler file exactly; when\n-- false (world events) handler resolution falls back up the topic chain.\n-- Copied onto the root agentrun by the input-event watcher.\nALTER TABLE events ADD COLUMN IF NOT EXISTS is_exact_topic boolean NOT NULL DEFAULT false;\n-- Privileged events originate from a trusted user-input source (operator\n-- at the local terminal via cli-chat, operator on Telegram). The flag\n-- propagates verbatim to the root agentrun and to every child agentrun\n-- spawned via schedule_subagent / start_subagent, so future system tools can\n-- gate risky reads / writes (editing SOUL.md etc.) on whether the run\n-- descends from a trusted input. Default false: any plugin that doesn't\n-- explicitly opt in produces non-privileged events.\nALTER TABLE events ADD COLUMN IF NOT EXISTS privileged boolean NOT NULL DEFAULT false;\n-- When true and the event ends in 'failed', `HostContextImpl.emitAndAwait`\n-- writes a role='assistant' chatmessage with the failure text before\n-- rejecting handle.settled. Default false: only chat plugins (cli-chat,\n-- telegram) opt in. See [[outputChatOnFailure]] on NewEvent for the\n-- rationale.\nALTER TABLE events ADD COLUMN IF NOT EXISTS output_chat_on_failure boolean NOT NULL DEFAULT false;\n\nCREATE INDEX IF NOT EXISTS events_state_priority_idx\n ON events (state, priority DESC, id ASC);\nCREATE INDEX IF NOT EXISTS events_topic_idx ON events (topic);\n\nCREATE TABLE IF NOT EXISTS agentruns (\n id bigserial PRIMARY KEY,\n event_id bigint NOT NULL REFERENCES events(id) ON DELETE CASCADE,\n parent_agentrun_id bigint REFERENCES agentruns(id) ON DELETE CASCADE,\n topic text NOT NULL,\n priority smallint NOT NULL DEFAULT 50,\n state text NOT NULL DEFAULT 'pending',\n prompt text,\n payload jsonb NOT NULL DEFAULT '{}'::jsonb,\n result jsonb,\n result_text text,\n error text,\n privileged boolean NOT NULL DEFAULT false,\n created_at timestamptz NOT NULL DEFAULT now(),\n updated_at timestamptz NOT NULL DEFAULT now(),\n CONSTRAINT agentruns_topic_format CHECK (topic ~ '^[\\w-]+(:[\\w-]+)*$')\n);\n\n-- Forward-compat ALTER for databases created before result_text landed.\n-- Safe no-op on fresh databases where the column already exists.\nALTER TABLE agentruns ADD COLUMN IF NOT EXISTS result_text text;\n-- The topic is the handler's whole address; the basename column is gone.\nALTER TABLE agentruns DROP COLUMN IF EXISTS handler;\n-- Whether handler resolution must match the topic exactly. Root\n-- agentruns copy the event's flag; spawned children are always exact.\nALTER TABLE agentruns ADD COLUMN IF NOT EXISTS is_exact_topic boolean NOT NULL DEFAULT true;\n-- Inherited from the originating event (root agentrun) or parent\n-- agentrun (children spawned via schedule_subagent / start_subagent). See\n-- the matching ALTER on the events table for the trust-model rationale.\nALTER TABLE agentruns ADD COLUMN IF NOT EXISTS privileged boolean NOT NULL DEFAULT false;\n-- Number of retry attempts already made on this agentrun. Bumped each\n-- time AgentRunner postpones the row due to a retryable inference\n-- error (APICallError.isRetryable). Capped per-handler via maxRetries\n-- (frontmatter override) or globally via inference.maxRetries.\nALTER TABLE agentruns ADD COLUMN IF NOT EXISTS retry_count int NOT NULL DEFAULT 0;\n-- Earliest moment the agentrun watcher is allowed to re-claim this\n-- row. NULL = claim immediately (default for fresh rows). A future\n-- timestamp = leave the row alone until then; the watcher's claim\n-- query gates on this so a parked row doesn't waste the slot while\n-- other agentruns (potentially using different models) can still run.\nALTER TABLE agentruns ADD COLUMN IF NOT EXISTS not_before timestamptz;\n-- Resolved model id for this agentrun, of the form\n-- `<provider>/<modelId>` (e.g. `featherless/zai-org/GLM-5.1`).\n-- Populated by AgentRunner once it builds the model client; stays\n-- NULL for any agentrun that never reached generate() (failed at\n-- handler-load time, etc.).\nALTER TABLE agentruns ADD COLUMN IF NOT EXISTS model text;\n-- Resolved prompt (rendered prompt template: system prompt, then\n-- user-message head) used for this agentrun. Populated by AgentRunner\n-- only when core.logSystemPrompt is true; otherwise NULL. Surfaced\n-- by the report layer's Agentrun Start section when the consumer\n-- opts into withDetails.\nALTER TABLE agentruns ADD COLUMN IF NOT EXISTS system_prompt text;\n-- Verbatim model-message array assembled for the first agent.generate()\n-- call (prior history + the run's leading user turn). Populated by\n-- AgentRunner only when inference.captureInitialMessageHistory is true;\n-- otherwise NULL. Surfaced by the report layer's -vv view so an operator\n-- can see exactly what was sent into the model loop.\nALTER TABLE agentruns ADD COLUMN IF NOT EXISTS initial_messages jsonb;\n\n-- Distinguishes how a child agentrun was spawned.\n-- NULL \u2014 root agentrun (created by the input-event watcher, no parent).\n-- 'scheduled' \u2014 fire-and-forget child spawned by `schedule_subagent` without\n-- a future `when` (immediate dispatch under the same event).\n-- 'started' \u2014 synchronous child spawned via the `start_subagent` tool; the\n-- parent is parked in state='waiting' until this row settles.\n-- The Scheduler reads this column to decide whether settling a row should\n-- wake a waiting parent.\nALTER TABLE agentruns ADD COLUMN IF NOT EXISTS calltype text;\nALTER TABLE agentruns DROP CONSTRAINT IF EXISTS agentruns_calltype_format;\nALTER TABLE agentruns ADD CONSTRAINT agentruns_calltype_format\n CHECK (calltype IS NULL OR calltype IN ('scheduled','started'));\n\n-- Partial index for the \"are all started children of $parent settled?\"\n-- check the Scheduler runs after every 'started' child settles. Indexes\n-- only the rows that actually participate in the question.\nCREATE INDEX IF NOT EXISTS agentruns_parent_calltype_state_idx\n ON agentruns (parent_agentrun_id, state)\n WHERE calltype = 'started';\n\n-- Forward-compat: relax the topic CHECK constraint to allow arbitrarily\n-- deep `a:b:c:\u2026` topics. CREATE TABLE IF NOT EXISTS above doesn't\n-- update existing constraints, so we drop+re-add unconditionally. The\n-- DROP IF EXISTS / ADD pair is idempotent.\nALTER TABLE events DROP CONSTRAINT IF EXISTS events_topic_format;\nALTER TABLE events ADD CONSTRAINT events_topic_format CHECK (topic ~ '^[\\w-]+(:[\\w-]+)*$');\nALTER TABLE agentruns DROP CONSTRAINT IF EXISTS agentruns_topic_format;\nALTER TABLE agentruns ADD CONSTRAINT agentruns_topic_format CHECK (topic ~ '^[\\w-]+(:[\\w-]+)*$');\n\nCREATE INDEX IF NOT EXISTS agentruns_state_priority_idx\n ON agentruns (state, priority DESC, id ASC);\nCREATE INDEX IF NOT EXISTS agentruns_event_id_idx ON agentruns (event_id);\nCREATE INDEX IF NOT EXISTS agentruns_parent_idx ON agentruns (parent_agentrun_id);\n\nCREATE TABLE IF NOT EXISTS stepresults (\n id bigserial PRIMARY KEY,\n agent_run_id bigint NOT NULL REFERENCES agentruns(id) ON DELETE CASCADE,\n event_id bigint NOT NULL REFERENCES events(id) ON DELETE CASCADE,\n step_number int NOT NULL,\n finish_reason text NOT NULL,\n result_text text,\n reasoning_text text,\n input_tokens int,\n output_tokens int,\n total_tokens int,\n tool_call_count int NOT NULL DEFAULT 0,\n tool_calls jsonb NOT NULL DEFAULT '[]'::jsonb,\n tool_results jsonb NOT NULL DEFAULT '[]'::jsonb,\n created_at timestamptz NOT NULL DEFAULT now(),\n CONSTRAINT stepresults_step_number_unique UNIQUE (agent_run_id, step_number)\n);\n\nCREATE INDEX IF NOT EXISTS stepresults_agent_run_id_idx ON stepresults (agent_run_id);\nCREATE INDEX IF NOT EXISTS stepresults_event_id_idx ON stepresults (event_id);\n\n-- AI SDK per-step usage breakdown\n-- (LanguageModelUsage.inputTokenDetails / outputTokenDetails). Each\n-- column is NULL when the provider doesn't report that detail.\nALTER TABLE stepresults ADD COLUMN IF NOT EXISTS input_tokens_no_cache int;\nALTER TABLE stepresults ADD COLUMN IF NOT EXISTS input_tokens_cache_read int;\nALTER TABLE stepresults ADD COLUMN IF NOT EXISTS input_tokens_cache_write int;\nALTER TABLE stepresults ADD COLUMN IF NOT EXISTS output_tokens_text int;\nALTER TABLE stepresults ADD COLUMN IF NOT EXISTS output_tokens_reasoning int;\n-- Full step object snapshot, populated only when the operator opts\n-- in via inference.captureRawStepResultToDatabase: true. Used to\n-- inspect provider-specific fields (Anthropic cache control,\n-- OpenAI logprobs, ...) that don't fit dedicated columns.\nALTER TABLE stepresults ADD COLUMN IF NOT EXISTS raw_result jsonb;\n\n-- \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500 chatmessages \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\n--\n-- Persistent chat history across all channels. Each row links to the\n-- event whose lifecycle the message belongs to:\n-- * For role='user': the event the host plugin emitted with isChat=true.\n-- * For role='assistant': the event whose agentrun called send_chat.\n-- The channel is reachable via JOIN to events.preferred_chat_channel_id;\n-- there is intentionally no channel_id column here \u2014 channel routing is\n-- a host-side concern, the container never names a channel.\n--\n-- delivered_at is set when ANY listener returns true from its async\n-- handler. New subscribers replay all undelivered matching rows on\n-- registration so a message produced when nobody was listening is still\n-- delivered later.\nCREATE TABLE IF NOT EXISTS chatmessages (\n id bigserial PRIMARY KEY,\n event_id bigint NOT NULL REFERENCES events(id) ON DELETE CASCADE,\n role text NOT NULL CHECK (role IN ('user','assistant')),\n text_content text NOT NULL,\n created_at timestamptz NOT NULL DEFAULT now(),\n delivered_at timestamptz\n);\n\nCREATE INDEX IF NOT EXISTS chatmessages_event_idx\n ON chatmessages (event_id);\nCREATE INDEX IF NOT EXISTS chatmessages_undelivered_idx\n ON chatmessages (created_at) WHERE delivered_at IS NULL;\n\n-- \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500 NOTIFY triggers \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\n\nCREATE OR REPLACE FUNCTION events_notify_new() RETURNS trigger AS $$\nBEGIN\n PERFORM pg_notify('events_new', NEW.id::text);\n RETURN NEW;\nEND;\n$$ LANGUAGE plpgsql;\n\nDROP TRIGGER IF EXISTS events_new_trg ON events;\nCREATE TRIGGER events_new_trg\n AFTER INSERT ON events\n FOR EACH ROW EXECUTE FUNCTION events_notify_new();\n\nCREATE OR REPLACE FUNCTION events_notify_state() RETURNS trigger AS $$\nBEGIN\n PERFORM pg_notify('events_state', NEW.id::text || ':' || NEW.state);\n RETURN NEW;\nEND;\n$$ LANGUAGE plpgsql;\n\nDROP TRIGGER IF EXISTS events_state_trg ON events;\nCREATE TRIGGER events_state_trg\n AFTER UPDATE OF state ON events\n FOR EACH ROW EXECUTE FUNCTION events_notify_state();\n\nCREATE OR REPLACE FUNCTION agentruns_notify_changed() RETURNS trigger AS $$\nBEGIN\n PERFORM pg_notify(\n 'agentruns_changed',\n NEW.event_id::text || ':' || NEW.id::text\n );\n RETURN NEW;\nEND;\n$$ LANGUAGE plpgsql;\n\nDROP TRIGGER IF EXISTS agentruns_notify_changed_trg ON agentruns;\nCREATE TRIGGER agentruns_notify_changed_trg\n AFTER INSERT OR UPDATE OF state ON agentruns\n FOR EACH ROW EXECUTE FUNCTION agentruns_notify_changed();\n\nCREATE OR REPLACE FUNCTION stepresults_notify_new() RETURNS trigger AS $$\nBEGIN\n PERFORM pg_notify(\n 'stepresults_new',\n NEW.event_id::text || ':' || NEW.agent_run_id::text || ':' || NEW.id::text\n );\n RETURN NEW;\nEND;\n$$ LANGUAGE plpgsql;\n\nDROP TRIGGER IF EXISTS stepresults_new_trg ON stepresults;\nCREATE TRIGGER stepresults_new_trg\n AFTER INSERT ON stepresults\n FOR EACH ROW EXECUTE FUNCTION stepresults_notify_new();\n\n-- chatmessages NOTIFY: payload is \"<channel_id>:<role>:<id>\". Channel\n-- comes from the parent event's preferred_chat_channel_id, JOINed at\n-- trigger time. Empty channel becomes empty prefix \u2014 listeners will\n-- filter it out unless they explicitly subscribe to channelId=\"\".\nCREATE OR REPLACE FUNCTION chatmessages_notify_new() RETURNS trigger AS $$\nDECLARE\n channel text;\nBEGIN\n SELECT preferred_chat_channel_id INTO channel FROM events WHERE id = NEW.event_id;\n PERFORM pg_notify(\n 'chatmessages_new',\n COALESCE(channel, '') || ':' || NEW.role || ':' || NEW.id::text\n );\n RETURN NEW;\nEND;\n$$ LANGUAGE plpgsql;\n\nDROP TRIGGER IF EXISTS chatmessages_new_trg ON chatmessages;\nCREATE TRIGGER chatmessages_new_trg\n AFTER INSERT ON chatmessages\n FOR EACH ROW EXECUTE FUNCTION chatmessages_notify_new();\n\n-- \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500 calendars + calendar_events \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\n--\n-- Plugin-agnostic calendar data layer. `calendars` lists every\n-- subscription (own or shared) any provider plugin (ms365, future\n-- google/caldav) has surfaced; `calendar_events` stores every\n-- instance. Providers persist every occurrence of a recurring\n-- series independently (no master-expansion in core).\n--\n-- Identity:\n-- * `calendars.unique_key` is the provider's stable id. Uniqueness\n-- within `(plugin_id, unique_key)` lets multiple providers\n-- coexist.\n-- * `calendar_events.id` is provider-prefixed (e.g. `ms365:<graph-id>`)\n-- so collisions across providers are impossible by construction.\n--\n-- Refresh reconciliation uses a scan-generation tag: a refresh walk\n-- bumps `calendars.scan_generation`, upserts every event in window\n-- with the new value, then DELETEs events whose generation is older\n-- than the calendar's current value.\n\nCREATE TABLE IF NOT EXISTS calendars (\n id bigserial PRIMARY KEY,\n plugin_id text NOT NULL,\n unique_key text NOT NULL,\n name text NOT NULL,\n type text NOT NULL CHECK (type IN ('own','shared')),\n owner_name text,\n scan_generation int NOT NULL DEFAULT 0,\n created_at timestamptz NOT NULL DEFAULT now(),\n UNIQUE (plugin_id, unique_key)\n);\n\n-- The provider's own \"default calendar\" flag (Graph's\n-- `isDefaultCalendar`). Used by `CalendarService.resolveDefaultCalendar`\n-- as the fallback when `core.defaultCalendar` is unset \u2014 we pick the\n-- first registered calendar (lowest id) flagged here. Added via\n-- ALTER so existing databases don't need a schema reset.\nALTER TABLE calendars ADD COLUMN IF NOT EXISTS is_default smallint NOT NULL DEFAULT 0;\n\nCREATE INDEX IF NOT EXISTS calendars_plugin_id_idx ON calendars (plugin_id);\n\nCREATE TABLE IF NOT EXISTS calendar_events (\n id text PRIMARY KEY,\n calendar_id bigint NOT NULL REFERENCES calendars(id) ON DELETE CASCADE,\n series_master_id text,\n type text NOT NULL CHECK (type IN ('singleInstance','occurrence','exception','seriesMaster')),\n subject text,\n start_dt text NOT NULL,\n end_dt text NOT NULL,\n event_tz text,\n is_all_day smallint NOT NULL DEFAULT 0,\n is_cancelled smallint NOT NULL DEFAULT 0,\n show_as text,\n sensitivity text,\n importance text,\n location text,\n is_online_meeting smallint NOT NULL DEFAULT 0,\n online_meeting_url text,\n organizer_name text,\n organizer_email text,\n response_status text,\n attendees_json jsonb,\n body text,\n attachments jsonb,\n scan_generation int NOT NULL,\n created_at timestamptz NOT NULL DEFAULT now(),\n updated_at timestamptz NOT NULL DEFAULT now()\n);\n\nCREATE INDEX IF NOT EXISTS calendar_events_window_idx\n ON calendar_events (calendar_id, start_dt);\nCREATE INDEX IF NOT EXISTS calendar_events_series_idx\n ON calendar_events (series_master_id);\nCREATE INDEX IF NOT EXISTS calendar_events_generation_idx\n ON calendar_events (calendar_id, scan_generation);\n\n-- \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500 scheduled_subagents \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\n--\n-- One-off agent-scheduled wake-ups. Each row is upserted by the\n-- container's `schedule_subagent` tool and read by the host-side\n-- `ScheduledSubagentScheduler`, which installs a Croner job per row\n-- and atomically deletes the row when it fires. `key` is the\n-- agent-supplied unique id used for replace-on-conflict semantics\n-- (\"rescheduling a key overwrites the previous timing\"). `fire_at`\n-- is the UTC firing time; the agent passes wall-clock-in-`core.timezone`\n-- and the container projects to UTC before insert. On daemon startup,\n-- past-due rows are deleted with a warn log; future rows are\n-- reinstalled. See [[feedback_agent_facing_format]] \u2014 UTC storage,\n-- local-tz projection at every agent-facing edge.\nCREATE TABLE IF NOT EXISTS scheduled_subagents (\n key text PRIMARY KEY,\n fire_at timestamptz NOT NULL,\n topic text NOT NULL,\n prompt text,\n payload jsonb NOT NULL DEFAULT '{}'::jsonb,\n priority smallint NOT NULL DEFAULT 50,\n privileged boolean NOT NULL DEFAULT false,\n created_at timestamptz NOT NULL DEFAULT now(),\n CONSTRAINT scheduled_subagents_topic_format CHECK (topic ~ '^[\\w-]+(:[\\w-]+)*$')\n);\n\n-- The topic is the handler's whole address; the basename column is gone.\nALTER TABLE scheduled_subagents DROP COLUMN IF EXISTS handler;\n-- Re-add the topic CHECK so a widened TOPIC_PATTERN reaches existing tables.\nALTER TABLE scheduled_subagents DROP CONSTRAINT IF EXISTS scheduled_subagents_topic_format;\nALTER TABLE scheduled_subagents ADD CONSTRAINT scheduled_subagents_topic_format CHECK (topic ~ '^[\\w-]+(:[\\w-]+)*$');\n\nCREATE INDEX IF NOT EXISTS scheduled_subagents_fire_at_idx\n ON scheduled_subagents (fire_at);\n\nCREATE OR REPLACE FUNCTION scheduled_subagents_notify_changed() RETURNS trigger AS $$\nDECLARE\n payload_key text;\n op text;\nBEGIN\n IF TG_OP = 'DELETE' THEN\n payload_key := OLD.key;\n op := 'd';\n ELSE\n payload_key := NEW.key;\n op := 'u';\n END IF;\n PERFORM pg_notify('scheduled_subagents_changed', payload_key || ':' || op);\n RETURN COALESCE(NEW, OLD);\nEND;\n$$ LANGUAGE plpgsql;\n\nDROP TRIGGER IF EXISTS scheduled_subagents_notify_changed_trg ON scheduled_subagents;\nCREATE TRIGGER scheduled_subagents_notify_changed_trg\n AFTER INSERT OR UPDATE OR DELETE ON scheduled_subagents\n FOR EACH ROW EXECUTE FUNCTION scheduled_subagents_notify_changed();\n\n-- \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500 inference_events \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\n--\n-- Append-only audit trail of every upstream model HTTP call attempt.\n-- Written by AgentRunner on each generate() outcome (success / retryable /\n-- fatal). Read by the host-side `inference_status` reflection tool to\n-- answer \"is this model misbehaving right now?\". One row per attempt;\n-- retries write multiple rows. NULL agent_run_id is kept for runs whose\n-- row was later deleted (parent event cascade) so the historical signal\n-- survives.\nCREATE TABLE IF NOT EXISTS inference_events (\n id bigserial PRIMARY KEY,\n provider text NOT NULL,\n model text NOT NULL,\n agent_run_id bigint REFERENCES agentruns(id) ON DELETE SET NULL,\n outcome text NOT NULL CHECK (outcome IN ('success','retryable','fatal')),\n status_code int,\n error_excerpt text,\n occurred_at timestamptz NOT NULL DEFAULT now()\n);\n\nCREATE INDEX IF NOT EXISTS inference_events_model_time_idx\n ON inference_events (model, occurred_at DESC);\nCREATE INDEX IF NOT EXISTS inference_events_occurred_at_idx\n ON inference_events (occurred_at DESC);\n\n-- \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500 tool_calls \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\n--\n-- Append-only record of every tool invocation an agentrun attempted,\n-- tagged with the resolved handler markdown path and whether the call\n-- threw. Written by the tool wrapper in the container's ToolsFactory on\n-- each execute() outcome. Read by the heuristic tool-preloader\n-- (ToolCallBus.topToolsForHandler): the tools a handler successfully\n-- used across its recent non-error runs get loaded up front on the next\n-- run, so warm handlers rarely need a tool_list / tool_call round-trip.\n-- NULL agent_run_id survives a parent-event cascade so the historical\n-- usage signal outlives the run row.\nCREATE TABLE IF NOT EXISTS tool_calls (\n id bigserial PRIMARY KEY,\n agent_run_id bigint REFERENCES agentruns(id) ON DELETE SET NULL,\n handler_path text NOT NULL,\n tool_name text NOT NULL,\n successful boolean NOT NULL,\n created_at timestamptz NOT NULL DEFAULT now()\n);\n\nCREATE INDEX IF NOT EXISTS tool_calls_handler_run_idx\n ON tool_calls (handler_path, agent_run_id DESC);\nCREATE INDEX IF NOT EXISTS tool_calls_created_at_idx\n ON tool_calls (created_at DESC);\n\n-- \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500 web_login_tokens / web_sessions \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\n--\n-- Authentication state of the host webserver (host-only; the container\n-- never reads these). A login token is a one-time link minted by\n-- `familiar web login`; redeeming it deletes the row and creates a\n-- session. Only sha256 hashes of tokens are stored, never the tokens.\nCREATE TABLE IF NOT EXISTS web_login_tokens (\n token_hash text PRIMARY KEY,\n login text NOT NULL,\n expires_at timestamptz NOT NULL,\n created_at timestamptz NOT NULL DEFAULT now()\n);\n\nCREATE TABLE IF NOT EXISTS web_sessions (\n id uuid PRIMARY KEY DEFAULT gen_random_uuid(),\n token_hash text NOT NULL UNIQUE,\n login text NOT NULL,\n user_agent text,\n created_at timestamptz NOT NULL DEFAULT now(),\n last_seen_at timestamptz NOT NULL DEFAULT now(),\n expires_at timestamptz NOT NULL\n);\n";
|
|
66
66
|
/**
|
|
67
67
|
* SQL fragment that flips the parent event's terminal state when the
|
|
68
68
|
* **root** agentrun settles. Run inside the same transaction as the
|
package/build/Schema.js
CHANGED
|
@@ -549,6 +549,29 @@ CREATE INDEX IF NOT EXISTS tool_calls_handler_run_idx
|
|
|
549
549
|
ON tool_calls (handler_path, agent_run_id DESC);
|
|
550
550
|
CREATE INDEX IF NOT EXISTS tool_calls_created_at_idx
|
|
551
551
|
ON tool_calls (created_at DESC);
|
|
552
|
+
|
|
553
|
+
-- ───────── web_login_tokens / web_sessions ─────────
|
|
554
|
+
--
|
|
555
|
+
-- Authentication state of the host webserver (host-only; the container
|
|
556
|
+
-- never reads these). A login token is a one-time link minted by
|
|
557
|
+
-- \`familiar web login\`; redeeming it deletes the row and creates a
|
|
558
|
+
-- session. Only sha256 hashes of tokens are stored, never the tokens.
|
|
559
|
+
CREATE TABLE IF NOT EXISTS web_login_tokens (
|
|
560
|
+
token_hash text PRIMARY KEY,
|
|
561
|
+
login text NOT NULL,
|
|
562
|
+
expires_at timestamptz NOT NULL,
|
|
563
|
+
created_at timestamptz NOT NULL DEFAULT now()
|
|
564
|
+
);
|
|
565
|
+
|
|
566
|
+
CREATE TABLE IF NOT EXISTS web_sessions (
|
|
567
|
+
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
|
|
568
|
+
token_hash text NOT NULL UNIQUE,
|
|
569
|
+
login text NOT NULL,
|
|
570
|
+
user_agent text,
|
|
571
|
+
created_at timestamptz NOT NULL DEFAULT now(),
|
|
572
|
+
last_seen_at timestamptz NOT NULL DEFAULT now(),
|
|
573
|
+
expires_at timestamptz NOT NULL
|
|
574
|
+
);
|
|
552
575
|
`;
|
|
553
576
|
/**
|
|
554
577
|
* SQL fragment that flips the parent event's terminal state when the
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Per-event scratch folders (`/scratch/<event-id>/` in the containers,
|
|
3
|
+
* `<scratchDir>/<event-id>/` on the host).
|
|
4
|
+
*
|
|
5
|
+
* Convention: files whose name starts with `.` are internal (spilled tool
|
|
6
|
+
* results, plugin bookkeeping). They are left out wherever scratch files
|
|
7
|
+
* are listed for the model ("staged files" in the prompt) or the user
|
|
8
|
+
* (downloads in the web chat).
|
|
9
|
+
*/
|
|
10
|
+
/** A file in an event's scratch folder, as `ctx.scratch.listFiles` reports it. */
|
|
11
|
+
export interface ScratchFile {
|
|
12
|
+
/** Basename inside `/scratch/<event-id>/`. */
|
|
13
|
+
readonly name: string;
|
|
14
|
+
/** Size in bytes. */
|
|
15
|
+
readonly size: number;
|
|
16
|
+
/** Last modification. */
|
|
17
|
+
readonly modifiedAt: Date;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Whether a scratch file is internal (dotfile) and must not be offered
|
|
21
|
+
* to the model or the user as a staged or produced file.
|
|
22
|
+
*
|
|
23
|
+
* @param name Basename.
|
|
24
|
+
* @returns True for hidden files.
|
|
25
|
+
*/
|
|
26
|
+
export declare function isHiddenScratchFile(name: string): boolean;
|
|
27
|
+
/**
|
|
28
|
+
* File name for spilling an oversized tool result to scratch: hidden
|
|
29
|
+
* (leading `.`, see {@link isHiddenScratchFile}) and with a 4-byte random
|
|
30
|
+
* hex token before the extension, so concurrent spills with the same
|
|
31
|
+
* suggestion never collide — `result.json` → `.result-a1b2c3d4.json`.
|
|
32
|
+
*
|
|
33
|
+
* @param suggestedName Name proposed by the tool runner.
|
|
34
|
+
* @returns The basename to write.
|
|
35
|
+
*/
|
|
36
|
+
export declare function scratchSpillFileName(suggestedName: string): string;
|
package/build/Scratch.js
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { randomBytes } from "node:crypto";
|
|
2
|
+
/**
|
|
3
|
+
* Whether a scratch file is internal (dotfile) and must not be offered
|
|
4
|
+
* to the model or the user as a staged or produced file.
|
|
5
|
+
*
|
|
6
|
+
* @param name Basename.
|
|
7
|
+
* @returns True for hidden files.
|
|
8
|
+
*/
|
|
9
|
+
export function isHiddenScratchFile(name) {
|
|
10
|
+
return name.startsWith(".");
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* File name for spilling an oversized tool result to scratch: hidden
|
|
14
|
+
* (leading `.`, see {@link isHiddenScratchFile}) and with a 4-byte random
|
|
15
|
+
* hex token before the extension, so concurrent spills with the same
|
|
16
|
+
* suggestion never collide — `result.json` → `.result-a1b2c3d4.json`.
|
|
17
|
+
*
|
|
18
|
+
* @param suggestedName Name proposed by the tool runner.
|
|
19
|
+
* @returns The basename to write.
|
|
20
|
+
*/
|
|
21
|
+
export function scratchSpillFileName(suggestedName) {
|
|
22
|
+
const suffix = randomBytes(4).toString("hex");
|
|
23
|
+
const base = suggestedName.replace(/^\.+/, "");
|
|
24
|
+
const dotIdx = base.lastIndexOf(".");
|
|
25
|
+
const named = dotIdx <= 0
|
|
26
|
+
? `${base}-${suffix}`
|
|
27
|
+
: `${base.slice(0, dotIdx)}-${suffix}${base.slice(dotIdx)}`;
|
|
28
|
+
return `.${named}`;
|
|
29
|
+
}
|