@unieai/rabi-tool-call-timeout-policy 0.0.0-stage → 0.2.2-rc.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 DeepSeek
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,8 @@
1
- # Temporary Holding Version
1
+ # @unieai/rabi-tool-call-timeout-policy
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ Tool-call timeout policy: a tools/execute wrapper that arms a per-tool deadline on exec.signal and returns TOOL_TIMEOUT when it wins
4
+
5
+ Part of Rabi, the agent harness UnieAI Code runs on. Installed as a dependency of `@unieai/rabi`;
6
+ not meant to be used on its own.
7
+
8
+ See LICENSE for the license and copyright of this package.
package/lib/index.js ADDED
@@ -0,0 +1,144 @@
1
+ import { deadline, timeoutOf } from "@unieai/rabi-timeout";
2
+ //#region lib/types/index.js
3
+ /**
4
+ * Cooperative tool-call timeout enforcer. A tool declares `timeoutMs` and
5
+ * promises to honor `exec.signal`; this wrapper arms that deadline and maps its
6
+ * own expiry to `TOOL_TIMEOUT` without racing or abandoning the tool promise.
7
+ *
8
+ * FIXME: settle the intended `@unieai/rabi-timeout-guard` rename before the
9
+ * first tagged release — suggestion only, aligning the name with its `guard/`
10
+ * home; decide at resolution time
11
+ * ([regrouping Agent Note](../../../../.agents/notes/archived/architecture/2026-07-29-package-regrouping.md)).
12
+ *
13
+ * @module @unieai/rabi-tool-call-timeout-policy
14
+ */
15
+ var __addDisposableResource = function(env, value, async) {
16
+ if (value !== null && value !== void 0) {
17
+ if (typeof value !== "object" && typeof value !== "function") throw new TypeError("Object expected.");
18
+ var dispose, inner;
19
+ if (async) {
20
+ if (!Symbol.asyncDispose) throw new TypeError("Symbol.asyncDispose is not defined.");
21
+ dispose = value[Symbol.asyncDispose];
22
+ }
23
+ if (dispose === void 0) {
24
+ if (!Symbol.dispose) throw new TypeError("Symbol.dispose is not defined.");
25
+ dispose = value[Symbol.dispose];
26
+ if (async) inner = dispose;
27
+ }
28
+ if (typeof dispose !== "function") throw new TypeError("Object not disposable.");
29
+ if (inner) dispose = function() {
30
+ try {
31
+ inner.call(this);
32
+ } catch (e) {
33
+ return Promise.reject(e);
34
+ }
35
+ };
36
+ env.stack.push({
37
+ value,
38
+ dispose,
39
+ async
40
+ });
41
+ } else if (async) env.stack.push({ async: true });
42
+ return value;
43
+ };
44
+ var __disposeResources = (function(SuppressedError) {
45
+ return function(env) {
46
+ function fail(e) {
47
+ env.error = env.hasError ? new SuppressedError(e, env.error, "An error was suppressed during disposal.") : e;
48
+ env.hasError = true;
49
+ }
50
+ var r, s = 0;
51
+ function next() {
52
+ while (r = env.stack.pop()) try {
53
+ if (!r.async && s === 1) return s = 0, env.stack.push(r), Promise.resolve().then(next);
54
+ if (r.dispose) {
55
+ var result = r.dispose.call(r.value);
56
+ if (r.async) return s |= 2, Promise.resolve(result).then(next, function(e) {
57
+ fail(e);
58
+ return next();
59
+ });
60
+ } else s |= 1;
61
+ } catch (e) {
62
+ fail(e);
63
+ }
64
+ if (s === 1) return env.hasError ? Promise.reject(env.error) : Promise.resolve();
65
+ if (env.hasError) throw env.error;
66
+ }
67
+ return next();
68
+ };
69
+ })(typeof SuppressedError === "function" ? SuppressedError : function(error, suppressed, message) {
70
+ var e = new Error(message);
71
+ return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
72
+ });
73
+ /**
74
+ * The code owned by this plugin, used BOTH as the internal {@link deadline}
75
+ * classification code AND as the structured error `code` on the replacement
76
+ * tool result. Scoping {@link timeoutOf} to it keeps a nested outer deadline
77
+ * (another `tools/execute` wrapper's timer that fired first) from being misread
78
+ * as this plugin's own timeout — it reads as an ordinary upstream cancel.
79
+ */
80
+ const TOOL_TIMEOUT = "TOOL_TIMEOUT";
81
+ /** Cordis plugin name used by loader diagnostics. */
82
+ const name = "timeout-policy";
83
+ /** The tool registry service this plugin wraps (`tools/execute`) and reads (`get`). */
84
+ const inject = ["tools"];
85
+ /**
86
+ * The structured result substituted when this plugin's deadline wins. `content`
87
+ * is the model-facing message; `error.code` is the same {@link TOOL_TIMEOUT}
88
+ * this plugin owns, so a retry/sandbox plugin (and replay) can route on it.
89
+ *
90
+ * @param timeoutMs - the elapsed budget, rendered into the model-facing message.
91
+ * @returns the `isError` {@link ToolExecutionResult} with a `TOOL_TIMEOUT` error.
92
+ */
93
+ function toolTimeoutResult(timeoutMs) {
94
+ const message = `tool call timed out after ${timeoutMs}ms`;
95
+ return {
96
+ content: [{
97
+ type: "text",
98
+ text: `Error: ${message}`
99
+ }],
100
+ isError: true,
101
+ error: {
102
+ message,
103
+ info: {
104
+ name: "ToolTimeoutError",
105
+ code: TOOL_TIMEOUT
106
+ }
107
+ }
108
+ };
109
+ }
110
+ /**
111
+ * Register the timeout wrapper. It resolves the caller-visible tool definition,
112
+ * temporarily replaces `exec.signal`, delegates, restores the upstream signal,
113
+ * and replaces the result only when this wrapper's own timer fired.
114
+ */
115
+ function apply(ctx) {
116
+ ctx.on("tools/execute", async (exec, next) => {
117
+ const env_1 = {
118
+ stack: [],
119
+ error: void 0,
120
+ hasError: false
121
+ };
122
+ try {
123
+ const timeoutMs = ctx.tools.get(exec.name, exec.agent)?.timeoutMs;
124
+ if (timeoutMs === void 0) return next();
125
+ const d = __addDisposableResource(env_1, deadline(exec.signal, timeoutMs, TOOL_TIMEOUT), false);
126
+ const upstream = exec.signal;
127
+ exec.signal = d.signal;
128
+ try {
129
+ const result = await next();
130
+ if (timeoutOf(d.signal, "TOOL_TIMEOUT") !== void 0) return toolTimeoutResult(timeoutMs);
131
+ return result;
132
+ } finally {
133
+ exec.signal = upstream;
134
+ }
135
+ } catch (e_1) {
136
+ env_1.error = e_1;
137
+ env_1.hasError = true;
138
+ } finally {
139
+ __disposeResources(env_1);
140
+ }
141
+ });
142
+ }
143
+ //#endregion
144
+ export { TOOL_TIMEOUT, apply, inject, name };
@@ -0,0 +1,32 @@
1
+ /**
2
+ * Cooperative tool-call timeout enforcer. A tool declares `timeoutMs` and
3
+ * promises to honor `exec.signal`; this wrapper arms that deadline and maps its
4
+ * own expiry to `TOOL_TIMEOUT` without racing or abandoning the tool promise.
5
+ *
6
+ * FIXME: settle the intended `@unieai/rabi-timeout-guard` rename before the
7
+ * first tagged release — suggestion only, aligning the name with its `guard/`
8
+ * home; decide at resolution time
9
+ * ([regrouping Agent Note](../../../../.agents/notes/archived/architecture/2026-07-29-package-regrouping.md)).
10
+ *
11
+ * @module @unieai/rabi-tool-call-timeout-policy
12
+ */
13
+ import type { Context } from '@unieai/cordis';
14
+ /**
15
+ * The code owned by this plugin, used BOTH as the internal {@link deadline}
16
+ * classification code AND as the structured error `code` on the replacement
17
+ * tool result. Scoping {@link timeoutOf} to it keeps a nested outer deadline
18
+ * (another `tools/execute` wrapper's timer that fired first) from being misread
19
+ * as this plugin's own timeout — it reads as an ordinary upstream cancel.
20
+ */
21
+ export declare const TOOL_TIMEOUT = "TOOL_TIMEOUT";
22
+ /** Cordis plugin name used by loader diagnostics. */
23
+ export declare const name = "timeout-policy";
24
+ /** The tool registry service this plugin wraps (`tools/execute`) and reads (`get`). */
25
+ export declare const inject: string[];
26
+ /**
27
+ * Register the timeout wrapper. It resolves the caller-visible tool definition,
28
+ * temporarily replaces `exec.signal`, delegates, restores the upstream signal,
29
+ * and replaces the result only when this wrapper's own timer fired.
30
+ */
31
+ export declare function apply(ctx: Context): void;
32
+ //# sourceMappingURL=index.d.ts.map
package/locale/de.json ADDED
@@ -0,0 +1,6 @@
1
+ {
2
+ "meta": {
3
+ "title": "Timeout-Kontrolle für Tool-Aufrufe",
4
+ "description": "Senden Sie ein Abbruchsignal entsprechend dem Zeitlimit jedes Tools und geben Sie das Timeout-Ergebnis zurück."
5
+ }
6
+ }
package/locale/en.json ADDED
@@ -0,0 +1,5 @@
1
+ {
2
+ "meta": {
3
+ "title": "tool-call-timeout-policy"
4
+ }
5
+ }
package/locale/es.json ADDED
@@ -0,0 +1,6 @@
1
+ {
2
+ "meta": {
3
+ "title": "Control de tiempo de espera de llamada de herramienta",
4
+ "description": "Envía una señal de cancelación de acuerdo con el límite de tiempo de cada herramienta y devuelve el resultado del tiempo de espera."
5
+ }
6
+ }
package/locale/fr.json ADDED
@@ -0,0 +1,6 @@
1
+ {
2
+ "meta": {
3
+ "title": "Contrôle du délai d'expiration des appels d'outils",
4
+ "description": "Envoyez un signal d'annulation en fonction de la limite de temps de chaque outil et renvoyez le résultat du délai d'attente."
5
+ }
6
+ }
package/locale/ja.json ADDED
@@ -0,0 +1,6 @@
1
+ {
2
+ "meta": {
3
+ "title": "ツール呼び出しタイムアウト制御",
4
+ "description": "ツールごとの制限時間に従ってキャンセルを通知し、タイムアウト結果を返します。"
5
+ }
6
+ }
package/locale/pt.json ADDED
@@ -0,0 +1,6 @@
1
+ {
2
+ "meta": {
3
+ "title": "Controle de tempo limite de chamada de ferramenta",
4
+ "description": "Envia um sinal de cancelamento de acordo com o limite de tempo de cada ferramenta e retorna o resultado do timeout."
5
+ }
6
+ }
package/locale/ru.json ADDED
@@ -0,0 +1,6 @@
1
+ {
2
+ "meta": {
3
+ "title": "Управление тайм-аутом вызова инструмента",
4
+ "description": "Отправьте сигнал отмены в соответствии с лимитом времени каждого инструмента и верните результат тайм-аута."
5
+ }
6
+ }
package/locale/zh.json ADDED
@@ -0,0 +1,6 @@
1
+ {
2
+ "meta": {
3
+ "title": "工具调用超时控制",
4
+ "description": "按各工具的时限发出取消信号,并返回超时结果。"
5
+ }
6
+ }
package/package.json CHANGED
@@ -1,6 +1,36 @@
1
1
  {
2
2
  "name": "@unieai/rabi-tool-call-timeout-policy",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "description": "Tool-call timeout policy: a tools/execute wrapper that arms a per-tool deadline on exec.signal and returns TOOL_TIMEOUT when it wins",
4
+ "version": "0.2.2-rc.5",
5
+ "publishConfig": {
6
+ "access": "public"
7
+ },
8
+ "type": "module",
9
+ "main": "lib/index.js",
10
+ "types": "lib/types/index.d.ts",
11
+ "exports": {
12
+ ".": {
13
+ "types": "./lib/types/index.d.ts",
14
+ "default": "./lib/index.js"
15
+ },
16
+ "./src/*": "./src/*",
17
+ "./package.json": "./package.json",
18
+ "./locale/*.json": "./locale/*.json"
19
+ },
20
+ "files": [
21
+ "locale/*.json",
22
+ "lib/index.js",
23
+ "lib/types/**/*.d.ts",
24
+ "lib/types/**/*.d.ts.map",
25
+ "lib/**/*.js.map",
26
+ "src",
27
+ "README.md"
28
+ ],
29
+ "license": "MIT",
30
+ "peerDependencies": {
31
+ "@unieai/rabi-llm": "0.2.2-rc.5",
32
+ "@unieai/rabi-timeout": "0.2.2-rc.5",
33
+ "@unieai/rabi-tools": "0.2.2-rc.5",
34
+ "@unieai/cordis": "4.1.0"
35
+ }
36
+ }
package/src/index.ts ADDED
@@ -0,0 +1,81 @@
1
+ /**
2
+ * Cooperative tool-call timeout enforcer. A tool declares `timeoutMs` and
3
+ * promises to honor `exec.signal`; this wrapper arms that deadline and maps its
4
+ * own expiry to `TOOL_TIMEOUT` without racing or abandoning the tool promise.
5
+ *
6
+ * FIXME: settle the intended `@unieai/rabi-timeout-guard` rename before the
7
+ * first tagged release — suggestion only, aligning the name with its `guard/`
8
+ * home; decide at resolution time
9
+ * ([regrouping Agent Note](../../../../.agents/notes/archived/architecture/2026-07-29-package-regrouping.md)).
10
+ *
11
+ * @module @unieai/rabi-tool-call-timeout-policy
12
+ */
13
+
14
+ import type { Context } from '@unieai/cordis'
15
+ import { deadline, timeoutOf } from '@unieai/rabi-timeout'
16
+ import type { ToolExecutionResult } from '@unieai/rabi-tools'
17
+
18
+ /**
19
+ * The code owned by this plugin, used BOTH as the internal {@link deadline}
20
+ * classification code AND as the structured error `code` on the replacement
21
+ * tool result. Scoping {@link timeoutOf} to it keeps a nested outer deadline
22
+ * (another `tools/execute` wrapper's timer that fired first) from being misread
23
+ * as this plugin's own timeout — it reads as an ordinary upstream cancel.
24
+ */
25
+ export const TOOL_TIMEOUT = 'TOOL_TIMEOUT'
26
+
27
+ /** Cordis plugin name used by loader diagnostics. */
28
+ export const name = 'timeout-policy'
29
+
30
+ /** The tool registry service this plugin wraps (`tools/execute`) and reads (`get`). */
31
+ export const inject = ['tools']
32
+
33
+ /**
34
+ * The structured result substituted when this plugin's deadline wins. `content`
35
+ * is the model-facing message; `error.code` is the same {@link TOOL_TIMEOUT}
36
+ * this plugin owns, so a retry/sandbox plugin (and replay) can route on it.
37
+ *
38
+ * @param timeoutMs - the elapsed budget, rendered into the model-facing message.
39
+ * @returns the `isError` {@link ToolExecutionResult} with a `TOOL_TIMEOUT` error.
40
+ */
41
+ function toolTimeoutResult(timeoutMs: number): ToolExecutionResult {
42
+ const message = `tool call timed out after ${timeoutMs}ms`
43
+ return {
44
+ content: [{ type: 'text', text: `Error: ${message}` }],
45
+ isError: true,
46
+ error: { message, info: { name: 'ToolTimeoutError', code: TOOL_TIMEOUT } },
47
+ }
48
+ }
49
+
50
+ /**
51
+ * Register the timeout wrapper. It resolves the caller-visible tool definition,
52
+ * temporarily replaces `exec.signal`, delegates, restores the upstream signal,
53
+ * and replaces the result only when this wrapper's own timer fired.
54
+ */
55
+ export function apply(ctx: Context): void {
56
+ ctx.on('tools/execute', async (exec, next): Promise<ToolExecutionResult> => {
57
+ const timeoutMs = ctx.tools.get(exec.name, exec.agent)?.timeoutMs
58
+ // A tool that declares no budget: no deadline, delegate unchanged.
59
+ if (timeoutMs === undefined) return next()
60
+
61
+ using d = deadline(exec.signal, timeoutMs, TOOL_TIMEOUT)
62
+ // Swap the derived deadline onto exec for dispatch, then restore the
63
+ // caller's own signal so post-execute listeners never see this plugin's
64
+ // (possibly already-aborted) timeout signal.
65
+ const upstream = exec.signal
66
+ exec.signal = d.signal
67
+ try {
68
+ const result = await next()
69
+ // If OUR timer fired (scoped by code — a nested outer deadline reads as
70
+ // undefined here), the tool/capability saw the abort and reached
71
+ // quiescence; replace whatever it returned (its own abort result) with the
72
+ // structured TOOL_TIMEOUT the model sees.
73
+ if (timeoutOf(d.signal, TOOL_TIMEOUT) !== undefined) {
74
+ return toolTimeoutResult(timeoutMs)
75
+ }
76
+ return result
77
+ } finally {
78
+ exec.signal = upstream
79
+ }
80
+ })
81
+ }