@crewhaus/tool-state 0.7.0 → 0.8.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/LICENSE +201 -0
- package/NOTICE +9 -0
- package/README.md +33 -5
- package/dist/index.d.ts +1 -1
- package/dist/index.js +101 -5
- package/dist/paths.d.ts +21 -2
- package/dist/paths.js +73 -14
- package/dist/store.js +5 -1
- package/dist/vector.d.ts +25 -0
- package/dist/vector.js +63 -0
- package/package.json +12 -6
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/NOTICE
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
CrewHaus Factory
|
|
2
|
+
Copyright 2026 Max Meier
|
|
3
|
+
|
|
4
|
+
This product includes software developed by Max Meier
|
|
5
|
+
(operating as StudioMax — https://studiomax.io) and contributors.
|
|
6
|
+
|
|
7
|
+
The CrewHaus name and logo are trademarks of Max Meier. See
|
|
8
|
+
https://github.com/crewhaus/factory/blob/main/TRADEMARK.md for the
|
|
9
|
+
use-of-marks policy.
|
package/README.md
CHANGED
|
@@ -10,8 +10,9 @@ it was killed, and what the other agent found out an hour ago. These twenty
|
|
|
10
10
|
tools are that memory, and a model spends no tokens deciding any of it.
|
|
11
11
|
|
|
12
12
|
The package exports each tool and the frozen `STATE_TOOLS` list a catalog
|
|
13
|
-
registers.
|
|
14
|
-
|
|
13
|
+
registers. `all-state` turns on the twenty tools below, and the `all-memory`
|
|
14
|
+
and `all-data-stores` roll-ups include it. `VectorDelete` is not in it (see
|
|
15
|
+
below). A spec can also name just the tools it wants:
|
|
15
16
|
|
|
16
17
|
```yaml
|
|
17
18
|
tools:
|
|
@@ -70,6 +71,14 @@ refuses any directory that holds anything this package did not put there —
|
|
|
70
71
|
and the workspace root always. `mode: "merge"` is the way to write into a
|
|
71
72
|
directory that is not ours.
|
|
72
73
|
|
|
74
|
+
`StateImport` never writes the run's own record either (0.8.0): the session
|
|
75
|
+
logs, the tool results the runtime saved to a file, and the proof bundles,
|
|
76
|
+
which the runtime hands each call on its bridge. An import with an entry
|
|
77
|
+
that would land in one is refused, and so is a `replace` of a directory that
|
|
78
|
+
holds one, in every permission mode and before anything is written. A
|
|
79
|
+
`stateDir` of `.crewhaus` would otherwise put an entry such as
|
|
80
|
+
`sessions/<id>.jsonl` in the live session log.
|
|
81
|
+
|
|
73
82
|
**Determinism.** Same inputs against the same state, same bytes out. Listings
|
|
74
83
|
are sorted by plain code-unit comparison — never `localeCompare`, whose order
|
|
75
84
|
depends on the machine's locale. Nothing samples a random source. And **no
|
|
@@ -167,9 +176,28 @@ pinecone or weaviate collection over HTTP, so this one is labelled
|
|
|
167
176
|
`scope: "external"` with `ioCapability: "network"`, takes a justification, and
|
|
168
177
|
is kept out of a list whose meaning it would quietly dilute.
|
|
169
178
|
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
179
|
+
The categories follow the same line: `VectorDelete` is only in `all-vector`.
|
|
180
|
+
No roll-up includes it — not `all-state`, `all-memory` or `all-data-stores`,
|
|
181
|
+
and not `all-network` either, on purpose: a spec that wrote `all-network` on
|
|
182
|
+
0.7.0 did not get a destructive delete, and upgrading should not hand it one.
|
|
183
|
+
Grant it with `tools: [all-vector]`, or by name: `tools: [vectorDelete]`.
|
|
184
|
+
|
|
185
|
+
**It needs a store.** Name it in the spec, and a compiled bundle, `crewhaus run`
|
|
186
|
+
and `crewhaus eval` build it at boot:
|
|
187
|
+
|
|
188
|
+
```yaml
|
|
189
|
+
tool_config:
|
|
190
|
+
vectorDelete:
|
|
191
|
+
backend: qdrant # qdrant, pinecone, weaviate or lance
|
|
192
|
+
url: https://qdrant.example:6333
|
|
193
|
+
collection: chunks
|
|
194
|
+
api_key: $QDRANT_API_KEY # read from the environment, never compiled in
|
|
195
|
+
protected_collections: [audit]
|
|
196
|
+
```
|
|
197
|
+
|
|
198
|
+
`in-memory` is refused: a store that starts empty in every process has nothing
|
|
199
|
+
in it to erase. A host that already built its store registers it instead, the
|
|
200
|
+
way `@crewhaus/tool-retrieve` is wired:
|
|
173
201
|
|
|
174
202
|
```ts
|
|
175
203
|
import { registerVectorTarget } from "@crewhaus/tool-state";
|
package/dist/index.d.ts
CHANGED
|
@@ -40,4 +40,4 @@ export declare const STATE_TOOLS: ReadonlyArray<RegisteredTool>;
|
|
|
40
40
|
export declare const VECTOR_TOOLS: ReadonlyArray<RegisteredTool>;
|
|
41
41
|
export { ToolPermissionError } from "./paths";
|
|
42
42
|
export { DEFAULT_STATE_DIR } from "./store";
|
|
43
|
-
export { MAX_VECTOR_IDS, VectorTargetError, _resetVectorTarget, countIsIndicative, getVectorTarget, parseChunkId, registerVectorTarget, selectVectorIds, type ChunkId, type VectorCountConsistency, type VectorDeleteTarget, type VectorSelection, type VectorTargetRegistration, } from "./vector";
|
|
43
|
+
export { MAX_VECTOR_IDS, VectorTargetError, _resetVectorTarget, countIsIndicative, getVectorTarget, parseChunkId, registerVectorDeleteConfig, registerVectorTarget, selectVectorIds, type ChunkId, type VectorCountConsistency, type VectorDeleteConfigInput, type VectorDeleteTarget, type VectorSelection, type VectorTargetRegistration, } from "./vector";
|
package/dist/index.js
CHANGED
|
@@ -46,6 +46,7 @@
|
|
|
46
46
|
import { readdirSync, statSync } from "node:fs";
|
|
47
47
|
import * as path from "node:path";
|
|
48
48
|
import { buildTool } from "@crewhaus/tool-builder";
|
|
49
|
+
import { protectedRootBeneath, protectedRootHolding, protectedRootsOf, protectedWriteRefusal, } from "@crewhaus/tool-safety/fs";
|
|
49
50
|
import { z } from "zod";
|
|
50
51
|
import { DEFAULT_B, DEFAULT_K1, buildIndex, isInvertedIndex, searchIndex, snippet, } from "./lib/bm25";
|
|
51
52
|
import { filterRecords, highestSeq, parseJsonl, toJsonlLine, } from "./lib/jsonl";
|
|
@@ -53,7 +54,7 @@ import { MAX_KEY_LENGTH, compareStrings, decodeSegment, encodeSegment, validateK
|
|
|
53
54
|
import { byteLength, casConflict, expiryFrom, formatInstant, isExpired, isPlainObject, isStoredRecord, parseInstant, } from "./lib/records";
|
|
54
55
|
import { ToolPermissionError, entryNameEscapes, resolveSafe, workspaceRoot, } from "./paths";
|
|
55
56
|
import { DEFAULT_STATE_DIR, MAX_FILE_BYTES, appendLine, createExclusive, describeIoError, errorCode, fileSize, listNames, readBytes, readJsonFile, readTextFile, relativeToRoot, removeFile, removeTree, sha256, statePath, stateRoot, walkFiles, withLock, writeBuffer, writeJsonAtomic, } from "./store";
|
|
56
|
-
import { MAX_VECTOR_IDS, applyVectorDeletes, capList, checkCollection, countIsIndicative, describeBackend, getVectorTarget, observeCount, readVectorToolConfig, selectVectorIds, unionProtected, } from "./vector";
|
|
57
|
+
import { MAX_VECTOR_IDS, VECTOR_DELETE_EXAMPLE, applyVectorDeletes, capList, checkCollection, countIsIndicative, describeBackend, getVectorTarget, observeCount, readVectorToolConfig, selectVectorIds, unionProtected, } from "./vector";
|
|
57
58
|
/** Compact JSON — the reader is a model, and every byte is context. */
|
|
58
59
|
const json = (value) => JSON.stringify(value);
|
|
59
60
|
// --- limits -----------------------------------------------------------------
|
|
@@ -249,6 +250,10 @@ function readLog(toolName, root, dir, name) {
|
|
|
249
250
|
// ---------------------------------------------------------------------------
|
|
250
251
|
export const kvSet = stateTool({
|
|
251
252
|
name: "KvSet",
|
|
253
|
+
operativeArgs: [
|
|
254
|
+
{ field: "stateDir", kind: "path", default: DEFAULT_STATE_DIR, relocates: true },
|
|
255
|
+
{ field: "key", kind: "id", within: "namespace" },
|
|
256
|
+
],
|
|
252
257
|
description: "Store a JSON value under a key in a namespace, with an optional TTL and an optional compare-and-set on the current version. Use it to remember something across turns, runs or agents; pass `expectedVersion` when another agent might be writing the same key and the write is refused rather than clobbering theirs.",
|
|
253
258
|
inputSchema: z.object({
|
|
254
259
|
namespace: z.string().min(1).describe("a group of keys, e.g. 'orders' or 'session'"),
|
|
@@ -329,8 +334,16 @@ export const kvSet = stateTool({
|
|
|
329
334
|
return locked.ok ? locked.value : locked.reason;
|
|
330
335
|
},
|
|
331
336
|
});
|
|
337
|
+
// The readers declare the store and the record as the writers do (C004): a
|
|
338
|
+
// deny on the store — `alwaysDeny *(.crewhaus/state/**)` — reads the default
|
|
339
|
+
// when the call leaves stateDir out, and a listing that names no record
|
|
340
|
+
// stands for every record (`*`), so a deny naming one fires on it.
|
|
332
341
|
export const kvGet = stateTool({
|
|
333
342
|
name: "KvGet",
|
|
343
|
+
operativeArgs: [
|
|
344
|
+
{ field: "stateDir", kind: "path", default: DEFAULT_STATE_DIR, relocates: true },
|
|
345
|
+
{ field: "key", kind: "id", within: "namespace" },
|
|
346
|
+
],
|
|
334
347
|
description: "Read one key back, with its version and expiry. Use it before a compare-and-set write, or to recover a value a previous turn stored; expiry is evaluated only when you pass `now`, and the result says whether it was checked.",
|
|
335
348
|
inputSchema: z.object({
|
|
336
349
|
namespace: z.string().min(1),
|
|
@@ -389,6 +402,10 @@ export const kvGet = stateTool({
|
|
|
389
402
|
});
|
|
390
403
|
export const kvDelete = stateTool({
|
|
391
404
|
name: "KvDelete",
|
|
405
|
+
operativeArgs: [
|
|
406
|
+
{ field: "stateDir", kind: "path", default: DEFAULT_STATE_DIR, relocates: true },
|
|
407
|
+
{ field: "key", kind: "id", within: "namespace" },
|
|
408
|
+
],
|
|
392
409
|
description: "Delete a key, optionally only while it is still at the version you read. Use it to release a claim or drop state you are finished with; deleting a key that is not there is reported, not an error.",
|
|
393
410
|
inputSchema: z.object({
|
|
394
411
|
namespace: z.string().min(1),
|
|
@@ -439,6 +456,11 @@ export const kvDelete = stateTool({
|
|
|
439
456
|
});
|
|
440
457
|
export const kvList = stateTool({
|
|
441
458
|
name: "KvList",
|
|
459
|
+
operativeArgs: [
|
|
460
|
+
{ field: "stateDir", kind: "path", default: DEFAULT_STATE_DIR, relocates: true },
|
|
461
|
+
// A prefix: the listing holds every key that starts with it, `/` and all.
|
|
462
|
+
{ field: "prefix", kind: "id", within: "namespace", default: "*", prefix: true },
|
|
463
|
+
],
|
|
442
464
|
description: "List the keys in a namespace, sorted, with their versions and optionally their values. Use it to see what a previous run left behind; records whose files are corrupt are listed separately instead of failing the whole call.",
|
|
443
465
|
inputSchema: z.object({
|
|
444
466
|
namespace: z.string().min(1),
|
|
@@ -518,6 +540,10 @@ function isCounter(value) {
|
|
|
518
540
|
}
|
|
519
541
|
export const counterIncrement = stateTool({
|
|
520
542
|
name: "CounterIncrement",
|
|
543
|
+
operativeArgs: [
|
|
544
|
+
{ field: "stateDir", kind: "path", default: DEFAULT_STATE_DIR, relocates: true },
|
|
545
|
+
{ field: "name", kind: "id" },
|
|
546
|
+
],
|
|
521
547
|
description: "Add to a durable named counter and return the new value, optionally refusing to cross a limit. Use it for attempt counts, quotas and budgets that must survive a restart; the read-modify-write runs under a lock, so two agents incrementing at once both count.",
|
|
522
548
|
inputSchema: z.object({
|
|
523
549
|
name: z.string().min(1).describe("counter name, e.g. 'retries.orders'"),
|
|
@@ -592,6 +618,10 @@ export const counterIncrement = stateTool({
|
|
|
592
618
|
});
|
|
593
619
|
export const counterGet = stateTool({
|
|
594
620
|
name: "CounterGet",
|
|
621
|
+
operativeArgs: [
|
|
622
|
+
{ field: "stateDir", kind: "path", default: DEFAULT_STATE_DIR, relocates: true },
|
|
623
|
+
{ field: "name", kind: "id", default: "*" },
|
|
624
|
+
],
|
|
595
625
|
description: "Read one counter, or every counter when you name none. Use it to check a budget before spending it; a counter that has never been incremented reads as 0 rather than as an error.",
|
|
596
626
|
inputSchema: z.object({
|
|
597
627
|
name: z.string().min(1).optional().describe("omit to list every counter, sorted by name"),
|
|
@@ -669,6 +699,10 @@ function versionFileName(version) {
|
|
|
669
699
|
}
|
|
670
700
|
export const checkpointSave = stateTool({
|
|
671
701
|
name: "CheckpointSave",
|
|
702
|
+
operativeArgs: [
|
|
703
|
+
{ field: "stateDir", kind: "path", default: DEFAULT_STATE_DIR, relocates: true },
|
|
704
|
+
{ field: "name", kind: "id" },
|
|
705
|
+
],
|
|
672
706
|
description: "Save a named JSON checkpoint as a new numbered version, so a long flow can be resumed after a crash or a restart. Use it at each stage boundary; earlier versions are kept unless you set `keep`, and `expectedVersion` refuses the save when somebody else checkpointed in the meantime.",
|
|
673
707
|
inputSchema: z.object({
|
|
674
708
|
name: z.string().min(1).describe("checkpoint name, e.g. 'migration' or 'crawl.batch-3'"),
|
|
@@ -748,6 +782,10 @@ export const checkpointSave = stateTool({
|
|
|
748
782
|
});
|
|
749
783
|
export const checkpointLoad = stateTool({
|
|
750
784
|
name: "CheckpointLoad",
|
|
785
|
+
operativeArgs: [
|
|
786
|
+
{ field: "stateDir", kind: "path", default: DEFAULT_STATE_DIR, relocates: true },
|
|
787
|
+
{ field: "name", kind: "id" },
|
|
788
|
+
],
|
|
751
789
|
description: "Load a checkpoint back — the newest version, or an earlier one by number. Use it when resuming: load, see how far the last run got, and carry on from there instead of starting again.",
|
|
752
790
|
inputSchema: z.object({
|
|
753
791
|
name: z.string().min(1),
|
|
@@ -800,6 +838,10 @@ export const checkpointLoad = stateTool({
|
|
|
800
838
|
});
|
|
801
839
|
export const checkpointList = stateTool({
|
|
802
840
|
name: "CheckpointList",
|
|
841
|
+
operativeArgs: [
|
|
842
|
+
{ field: "stateDir", kind: "path", default: DEFAULT_STATE_DIR, relocates: true },
|
|
843
|
+
{ field: "name", kind: "id", default: "*" },
|
|
844
|
+
],
|
|
803
845
|
description: "List the saved checkpoints with their version counts and newest label. Use it to find out what a previous run left to resume from before loading anything.",
|
|
804
846
|
inputSchema: z.object({
|
|
805
847
|
name: z.string().min(1).optional().describe("only this checkpoint"),
|
|
@@ -854,6 +896,10 @@ export const checkpointList = stateTool({
|
|
|
854
896
|
// ---------------------------------------------------------------------------
|
|
855
897
|
export const journalAppend = stateTool({
|
|
856
898
|
name: "JournalAppend",
|
|
899
|
+
operativeArgs: [
|
|
900
|
+
{ field: "stateDir", kind: "path", default: DEFAULT_STATE_DIR, relocates: true },
|
|
901
|
+
{ field: "stream", kind: "id" },
|
|
902
|
+
],
|
|
857
903
|
description: "Append one entry to an append-only JSONL stream and return its monotonic sequence number. Use it to record what happened, in order, so a later run or a reviewer can replay it; the sequence is allocated under a lock and the line written with O_APPEND, so concurrent writers never interleave.",
|
|
858
904
|
inputSchema: z.object({
|
|
859
905
|
stream: z.string().min(1).describe("stream name, e.g. 'runs' or 'orders.processed'"),
|
|
@@ -897,6 +943,10 @@ export const journalAppend = stateTool({
|
|
|
897
943
|
});
|
|
898
944
|
export const journalRead = stateTool({
|
|
899
945
|
name: "JournalRead",
|
|
946
|
+
operativeArgs: [
|
|
947
|
+
{ field: "stateDir", kind: "path", default: DEFAULT_STATE_DIR, relocates: true },
|
|
948
|
+
{ field: "stream", kind: "id", default: "*" },
|
|
949
|
+
],
|
|
900
950
|
description: "Read a journal stream back, filtered by sequence range, kind or substring — or list the streams when you name none. Use `sinceSeq` with the last sequence you handled to read only what is new; corrupt lines are reported with their line numbers and the rest of the stream is still returned.",
|
|
901
951
|
inputSchema: z.object({
|
|
902
952
|
stream: z
|
|
@@ -958,6 +1008,10 @@ export const journalRead = stateTool({
|
|
|
958
1008
|
// ---------------------------------------------------------------------------
|
|
959
1009
|
export const blackboardPost = stateTool({
|
|
960
1010
|
name: "BlackboardPost",
|
|
1011
|
+
operativeArgs: [
|
|
1012
|
+
{ field: "stateDir", kind: "path", default: DEFAULT_STATE_DIR, relocates: true },
|
|
1013
|
+
{ field: "topic", kind: "id" },
|
|
1014
|
+
],
|
|
961
1015
|
description: "Post a note to a shared topic that other crew members can read. Use it to leave findings, claims or warnings for agents working the same problem — it is a durable pinboard, not a delivery mechanism, so nobody is notified and nobody is guaranteed to read it.",
|
|
962
1016
|
inputSchema: z.object({
|
|
963
1017
|
topic: z.string().min(1).describe("topic name, e.g. 'incident-421'"),
|
|
@@ -1001,6 +1055,10 @@ export const blackboardPost = stateTool({
|
|
|
1001
1055
|
});
|
|
1002
1056
|
export const blackboardRead = stateTool({
|
|
1003
1057
|
name: "BlackboardRead",
|
|
1058
|
+
operativeArgs: [
|
|
1059
|
+
{ field: "stateDir", kind: "path", default: DEFAULT_STATE_DIR, relocates: true },
|
|
1060
|
+
{ field: "topic", kind: "id", default: "*" },
|
|
1061
|
+
],
|
|
1004
1062
|
description: "Read a topic's posts, filtered by author, tag or sequence — or list the topics when you name none. Use `latestPerAuthor` to get each crew member's most recent word without wading through the whole thread.",
|
|
1005
1063
|
inputSchema: z.object({
|
|
1006
1064
|
topic: z.string().min(1).optional().describe("omit to list every topic"),
|
|
@@ -1076,6 +1134,10 @@ function isNote(value) {
|
|
|
1076
1134
|
}
|
|
1077
1135
|
export const noteWrite = stateTool({
|
|
1078
1136
|
name: "NoteWrite",
|
|
1137
|
+
operativeArgs: [
|
|
1138
|
+
{ field: "stateDir", kind: "path", default: DEFAULT_STATE_DIR, relocates: true },
|
|
1139
|
+
{ field: "id", kind: "id" },
|
|
1140
|
+
],
|
|
1079
1141
|
description: "Write or overwrite a durable note under an id, with an optional title and tags. Use it to keep what the crew learned — a convention, a workaround, an answer worth not deriving twice — somewhere NoteSearch can find it again.",
|
|
1080
1142
|
inputSchema: z.object({
|
|
1081
1143
|
id: z.string().min(1).describe("note id; writing the same id again replaces the note"),
|
|
@@ -1135,6 +1197,7 @@ export const noteWrite = stateTool({
|
|
|
1135
1197
|
});
|
|
1136
1198
|
export const noteSearch = stateTool({
|
|
1137
1199
|
name: "NoteSearch",
|
|
1200
|
+
operativeArgs: [{ field: "stateDir", kind: "path", default: DEFAULT_STATE_DIR, beneath: "all" }],
|
|
1138
1201
|
description: "Search the stored notes with BM25 ranking over their titles, tags and text. Use it to recall what the crew already worked out; the matching is LEXICAL ONLY — no embeddings, no synonyms, no stemming — so a note about 'automobiles' will not answer a query about 'cars'.",
|
|
1139
1202
|
inputSchema: z.object({
|
|
1140
1203
|
query: z.string().min(1).describe("the words to look for"),
|
|
@@ -1240,6 +1303,13 @@ export const noteSearch = stateTool({
|
|
|
1240
1303
|
// ---------------------------------------------------------------------------
|
|
1241
1304
|
export const indexBuild = stateTool({
|
|
1242
1305
|
name: "IndexBuild",
|
|
1306
|
+
// `paths` are the files whose text goes into the index: a deny or ask reads
|
|
1307
|
+
// them, an allow on the index name need not.
|
|
1308
|
+
operativeArgs: [
|
|
1309
|
+
{ field: "stateDir", kind: "path", default: DEFAULT_STATE_DIR, relocates: true },
|
|
1310
|
+
{ field: "name", kind: "id" },
|
|
1311
|
+
{ field: "paths", kind: "path", source: true },
|
|
1312
|
+
],
|
|
1243
1313
|
description: "Build a named inverted index over a list of workspace text files, so they can be searched without a model reading them. Use it once per corpus and re-run it when the files change — the index is a snapshot and watches nothing; binary and over-large files are skipped and reported rather than mangled.",
|
|
1244
1314
|
inputSchema: z.object({
|
|
1245
1315
|
name: z.string().min(1).describe("index name, used later by IndexSearch"),
|
|
@@ -1270,7 +1340,7 @@ export const indexBuild = stateTool({
|
|
|
1270
1340
|
}
|
|
1271
1341
|
catch (err) {
|
|
1272
1342
|
if (err instanceof ToolPermissionError) {
|
|
1273
|
-
skipped.push({ path: given, reason: "resolves outside the workspace" });
|
|
1343
|
+
skipped.push({ path: given, reason: err.reason ?? "resolves outside the workspace" });
|
|
1274
1344
|
continue;
|
|
1275
1345
|
}
|
|
1276
1346
|
throw err;
|
|
@@ -1328,6 +1398,10 @@ export const indexBuild = stateTool({
|
|
|
1328
1398
|
});
|
|
1329
1399
|
export const indexSearch = stateTool({
|
|
1330
1400
|
name: "IndexSearch",
|
|
1401
|
+
operativeArgs: [
|
|
1402
|
+
{ field: "stateDir", kind: "path", default: DEFAULT_STATE_DIR, relocates: true },
|
|
1403
|
+
{ field: "name", kind: "id" },
|
|
1404
|
+
],
|
|
1331
1405
|
description: "Query an index built by IndexBuild and get the ranked files back, with optional snippets. Use it to narrow a large corpus to the two or three files worth reading; ranking is BM25 over literal terms, and a file whose size or mtime no longer matches the index is flagged stale rather than silently trusted.",
|
|
1332
1406
|
inputSchema: z.object({
|
|
1333
1407
|
name: z.string().min(1),
|
|
@@ -1461,6 +1535,7 @@ function replaceRefusal(root) {
|
|
|
1461
1535
|
}
|
|
1462
1536
|
export const stateExport = stateTool({
|
|
1463
1537
|
name: "StateExport",
|
|
1538
|
+
operativeArgs: [{ field: "stateDir", kind: "path", default: DEFAULT_STATE_DIR, beneath: "all" }],
|
|
1464
1539
|
description: "Dump the whole state directory as one JSON document, with a digest per file. Use it to back state up, move it to another machine, or attach it to a bug report; text files are carried as text and anything else as base64, and symlinks are skipped rather than followed.",
|
|
1465
1540
|
inputSchema: z.object({
|
|
1466
1541
|
prefix: z
|
|
@@ -1526,6 +1601,7 @@ export const stateExport = stateTool({
|
|
|
1526
1601
|
});
|
|
1527
1602
|
export const stateImport = stateTool({
|
|
1528
1603
|
name: "StateImport",
|
|
1604
|
+
operativeArgs: [{ field: "stateDir", kind: "path", default: DEFAULT_STATE_DIR, beneath: "all" }],
|
|
1529
1605
|
description: "Restore a state directory from a StateExport document, merging into what is there or replacing it. Use it to seed a fresh workspace or roll state back; `dryRun` reports exactly what would be written first, every entry path is checked for escapes before anything is created, and 'replace' refuses any directory holding anything this package did not put there, so it can never be pointed at a source tree.",
|
|
1530
1606
|
inputSchema: z.object({
|
|
1531
1607
|
document: z
|
|
@@ -1548,10 +1624,22 @@ export const stateImport = stateTool({
|
|
|
1548
1624
|
stateDir: stateDirField,
|
|
1549
1625
|
}),
|
|
1550
1626
|
destructive: true,
|
|
1551
|
-
execute: async (input) => {
|
|
1627
|
+
execute: async (input, ctx) => {
|
|
1552
1628
|
const root = stateRoot("StateImport", input.stateDir);
|
|
1553
1629
|
const mode = input.mode ?? "merge";
|
|
1554
1630
|
const dryRun = input.dryRun === true;
|
|
1631
|
+
// 0.8.0 — the run's own record (`RuntimeBridge.protectedRoots`: the
|
|
1632
|
+
// session logs, spilled tool results, proof bundles) is never written:
|
|
1633
|
+
// a `stateDir` at the workspace root or above `.crewhaus` would put an
|
|
1634
|
+
// entry such as `.crewhaus/sessions/<id>.jsonl` in the live log, where a
|
|
1635
|
+
// forged fetch grounds any text. Refused in every mode, before anything
|
|
1636
|
+
// is written, as CopyPath and MovePath refuse.
|
|
1637
|
+
const protectedRoots = protectedRootsOf(ctx);
|
|
1638
|
+
if (mode === "replace" &&
|
|
1639
|
+
protectedRoots.length > 0 &&
|
|
1640
|
+
protectedRootBeneath(root.abs, protectedRoots) !== undefined) {
|
|
1641
|
+
return protectedWriteRefusal("StateImport", `replacing ${root.rel}`);
|
|
1642
|
+
}
|
|
1555
1643
|
// Validate every path BEFORE writing anything, so a document with one bad
|
|
1556
1644
|
// entry cannot leave the state directory half-replaced.
|
|
1557
1645
|
const planned = [];
|
|
@@ -1574,6 +1662,9 @@ export const stateImport = stateTool({
|
|
|
1574
1662
|
}
|
|
1575
1663
|
throw err;
|
|
1576
1664
|
}
|
|
1665
|
+
if (protectedRoots.length > 0 && protectedRootHolding(target, protectedRoots) !== undefined) {
|
|
1666
|
+
return protectedWriteRefusal("StateImport", `importing ${file.path}`);
|
|
1667
|
+
}
|
|
1577
1668
|
const body = file.encoding === "utf8"
|
|
1578
1669
|
? Buffer.from(file.content, "utf8")
|
|
1579
1670
|
: Buffer.from(file.content, "base64");
|
|
@@ -1623,6 +1714,10 @@ export const stateImport = stateTool({
|
|
|
1623
1714
|
// ---------------------------------------------------------------------------
|
|
1624
1715
|
export const dedupeMark = stateTool({
|
|
1625
1716
|
name: "DedupeMark",
|
|
1717
|
+
operativeArgs: [
|
|
1718
|
+
{ field: "stateDir", kind: "path", default: DEFAULT_STATE_DIR, relocates: true },
|
|
1719
|
+
{ field: "scope", kind: "id" },
|
|
1720
|
+
],
|
|
1626
1721
|
description: "Record that an external id has been handled, and say whether it had been seen before. Use it as the guard in front of anything that must not happen twice — sending a mail, charging a card, filing a ticket — because a retried run marks the same id and gets `alreadySeen: true` instead of doing it again.",
|
|
1627
1722
|
inputSchema: z.object({
|
|
1628
1723
|
scope: z.string().min(1).describe("what is being de-duplicated, e.g. 'emails' or 'webhook'"),
|
|
@@ -1690,6 +1785,7 @@ export const dedupeMark = stateTool({
|
|
|
1690
1785
|
// ---------------------------------------------------------------------------
|
|
1691
1786
|
export const vectorDelete = stateTool({
|
|
1692
1787
|
name: "VectorDelete",
|
|
1788
|
+
operativeArgs: [{ field: "ids", kind: "id" }],
|
|
1693
1789
|
description: "Delete entries from the registered vector store by id, reporting how many deletes were ATTEMPTED and what the store's count was before and after. Use it to erase indexed content on request, and read the result exactly as it is worded: the store offers no way to ask whether an id exists, so nothing here can tell you an id was present or is now gone, and on an eventually-consistent backend `countAfter` is one indicative observation rather than proof.",
|
|
1694
1790
|
inputSchema: z.object({
|
|
1695
1791
|
ids: z
|
|
@@ -1729,7 +1825,7 @@ export const vectorDelete = stateTool({
|
|
|
1729
1825
|
execute: async (input, ctx) => {
|
|
1730
1826
|
const target = getVectorTarget();
|
|
1731
1827
|
if (target === undefined) {
|
|
1732
|
-
return
|
|
1828
|
+
return `VectorDelete has no vector store registered, and will not invent one. Name the store in the spec — ${VECTOR_DELETE_EXAMPLE} — or have the host call registerVectorTarget({ store, collection }). Nothing was deleted.`;
|
|
1733
1829
|
}
|
|
1734
1830
|
const policy = readVectorToolConfig(ctx?.toolConfig);
|
|
1735
1831
|
if (!policy.ok) {
|
|
@@ -1855,4 +1951,4 @@ export const STATE_TOOLS = Object.freeze([
|
|
|
1855
1951
|
export const VECTOR_TOOLS = Object.freeze([vectorDelete]);
|
|
1856
1952
|
export { ToolPermissionError } from "./paths";
|
|
1857
1953
|
export { DEFAULT_STATE_DIR } from "./store";
|
|
1858
|
-
export { MAX_VECTOR_IDS, VectorTargetError, _resetVectorTarget, countIsIndicative, getVectorTarget, parseChunkId, registerVectorTarget, selectVectorIds, } from "./vector";
|
|
1954
|
+
export { MAX_VECTOR_IDS, VectorTargetError, _resetVectorTarget, countIsIndicative, getVectorTarget, parseChunkId, registerVectorDeleteConfig, registerVectorTarget, selectVectorIds, } from "./vector";
|
package/dist/paths.d.ts
CHANGED
|
@@ -1,10 +1,29 @@
|
|
|
1
1
|
import { CrewhausError } from "@crewhaus/errors";
|
|
2
|
-
/**
|
|
2
|
+
/**
|
|
3
|
+
* The longest path this resolves, in UTF-16 code units; a longer one is
|
|
4
|
+
* refused before it is walked. Nothing that long can be opened or created:
|
|
5
|
+
* PATH_MAX is 1,024 bytes on macOS and 4,096 on Linux, Windows stops at
|
|
6
|
+
* 32,767 UTF-16 units, and a path's UTF-8 form is never shorter than its
|
|
7
|
+
* UTF-16 one. The limit is twice that because the part of a path that exists
|
|
8
|
+
* is itself under PATH_MAX, so the part that does not is over PATH_MAX on its
|
|
9
|
+
* own: no spelling of the place reaches it, not even one through a symlink
|
|
10
|
+
* that shortens the part that exists.
|
|
11
|
+
*/
|
|
12
|
+
export declare const MAX_PATH_CHARS: number;
|
|
13
|
+
/**
|
|
14
|
+
* Raised when a caller-supplied path resolves outside the workspace root, or
|
|
15
|
+
* is longer than any path can be.
|
|
16
|
+
*/
|
|
3
17
|
export declare class ToolPermissionError extends CrewhausError {
|
|
4
18
|
readonly name = "ToolPermissionError";
|
|
5
19
|
readonly toolName: string;
|
|
6
20
|
readonly path: string;
|
|
7
|
-
|
|
21
|
+
/**
|
|
22
|
+
* Why, when it is not that the location escapes the workspace root: a path
|
|
23
|
+
* longer than any path can be. A caller that rewords the refusal says this.
|
|
24
|
+
*/
|
|
25
|
+
readonly reason: string | undefined;
|
|
26
|
+
constructor(toolName: string, attemptedPath: string, reason?: string);
|
|
8
27
|
}
|
|
9
28
|
/**
|
|
10
29
|
* A validated path, in both forms the tools need.
|
package/dist/paths.js
CHANGED
|
@@ -22,15 +22,45 @@
|
|
|
22
22
|
import { lstatSync, readlinkSync, realpathSync } from "node:fs";
|
|
23
23
|
import * as path from "node:path";
|
|
24
24
|
import { CrewhausError } from "@crewhaus/errors";
|
|
25
|
-
/**
|
|
25
|
+
/**
|
|
26
|
+
* The longest path this resolves, in UTF-16 code units; a longer one is
|
|
27
|
+
* refused before it is walked. Nothing that long can be opened or created:
|
|
28
|
+
* PATH_MAX is 1,024 bytes on macOS and 4,096 on Linux, Windows stops at
|
|
29
|
+
* 32,767 UTF-16 units, and a path's UTF-8 form is never shorter than its
|
|
30
|
+
* UTF-16 one. The limit is twice that because the part of a path that exists
|
|
31
|
+
* is itself under PATH_MAX, so the part that does not is over PATH_MAX on its
|
|
32
|
+
* own: no spelling of the place reaches it, not even one through a symlink
|
|
33
|
+
* that shortens the part that exists.
|
|
34
|
+
*/
|
|
35
|
+
export const MAX_PATH_CHARS = 2 * (process.platform === "darwin" ? 1_024 : process.platform === "win32" ? 32_767 : 4_096);
|
|
36
|
+
/** A path as a refusal quotes it: whole, unless it is past {@link MAX_PATH_CHARS}. */
|
|
37
|
+
function shownPath(given) {
|
|
38
|
+
return given.length > MAX_PATH_CHARS ? `${given.slice(0, 64)}…` : given;
|
|
39
|
+
}
|
|
40
|
+
/** Why a path past {@link MAX_PATH_CHARS} is refused: its length and the limit. */
|
|
41
|
+
function tooLong(abs) {
|
|
42
|
+
return `it is ${abs.length} characters once resolved, over the ${MAX_PATH_CHARS}-character limit: no path that long can be opened or created`;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Raised when a caller-supplied path resolves outside the workspace root, or
|
|
46
|
+
* is longer than any path can be.
|
|
47
|
+
*/
|
|
26
48
|
export class ToolPermissionError extends CrewhausError {
|
|
27
49
|
name = "ToolPermissionError";
|
|
28
50
|
toolName;
|
|
29
51
|
path;
|
|
30
|
-
|
|
31
|
-
|
|
52
|
+
/**
|
|
53
|
+
* Why, when it is not that the location escapes the workspace root: a path
|
|
54
|
+
* longer than any path can be. A caller that rewords the refusal says this.
|
|
55
|
+
*/
|
|
56
|
+
reason;
|
|
57
|
+
constructor(toolName, attemptedPath, reason) {
|
|
58
|
+
super("tool", reason === undefined
|
|
59
|
+
? `tool "${toolName}" rejected path "${attemptedPath}": resolved location escapes the workspace root`
|
|
60
|
+
: `tool "${toolName}" rejected path "${shownPath(attemptedPath)}": ${reason}`);
|
|
32
61
|
this.toolName = toolName;
|
|
33
62
|
this.path = attemptedPath;
|
|
63
|
+
this.reason = reason;
|
|
34
64
|
}
|
|
35
65
|
}
|
|
36
66
|
/**
|
|
@@ -41,7 +71,7 @@ export class ToolPermissionError extends CrewhausError {
|
|
|
41
71
|
* writes through it to wherever it points. `lstat` answers about the link
|
|
42
72
|
* itself, which is what a containment check has to reason about.
|
|
43
73
|
*/
|
|
44
|
-
function
|
|
74
|
+
function nameExists(abs) {
|
|
45
75
|
try {
|
|
46
76
|
lstatSync(abs);
|
|
47
77
|
return true;
|
|
@@ -109,15 +139,7 @@ export function workspaceRoot() {
|
|
|
109
139
|
function resolveLocation(target, depth = 0) {
|
|
110
140
|
if (depth > 40)
|
|
111
141
|
throw new Error(`symlink chain at "${target}" is too long to resolve`);
|
|
112
|
-
|
|
113
|
-
const tail = [];
|
|
114
|
-
while (!existsNoFollow(probe)) {
|
|
115
|
-
tail.unshift(path.basename(probe));
|
|
116
|
-
const parent = path.dirname(probe);
|
|
117
|
-
if (parent === probe)
|
|
118
|
-
break; // reached the filesystem root
|
|
119
|
-
probe = parent;
|
|
120
|
-
}
|
|
142
|
+
const { probe, tail } = deepestName(target);
|
|
121
143
|
let probeReal;
|
|
122
144
|
try {
|
|
123
145
|
probeReal = realpathSync(probe);
|
|
@@ -139,7 +161,41 @@ function resolveLocation(target, depth = 0) {
|
|
|
139
161
|
const base = realpathSync(path.dirname(probe));
|
|
140
162
|
probeReal = resolveLocation(path.resolve(base, link), depth + 1);
|
|
141
163
|
}
|
|
142
|
-
|
|
164
|
+
// One string, not one argument per name: Bun's `path.join` keeps only
|
|
165
|
+
// (argument count mod 65,536) of its arguments.
|
|
166
|
+
return tail.length > 0 ? path.join(probeReal, tail) : probeReal;
|
|
167
|
+
}
|
|
168
|
+
/**
|
|
169
|
+
* `target`, an absolute path, split where its deepest prefix that exists as
|
|
170
|
+
* a NAME ends: that prefix, and the names below it, which do not exist, as
|
|
171
|
+
* one string ("" when `target` itself exists). This is `path.dirname` and
|
|
172
|
+
* `path.basename` worked by index on `target` itself: handing them each
|
|
173
|
+
* shorter prefix in turn copied the rest of the path once per missing name,
|
|
174
|
+
* the square of a count the caller picks.
|
|
175
|
+
*/
|
|
176
|
+
function deepestName(target) {
|
|
177
|
+
const top = path.parse(target).root.length;
|
|
178
|
+
const isSep = (i) => target[i] === "/" || target[i] === path.sep;
|
|
179
|
+
let probe = target;
|
|
180
|
+
let start = target.length;
|
|
181
|
+
let stop = -1;
|
|
182
|
+
while (!nameExists(probe)) {
|
|
183
|
+
// `path.basename(probe)` is `target.slice(start, end)`, and
|
|
184
|
+
// `path.dirname(probe)` is `target.slice(0, up)`.
|
|
185
|
+
let end = probe.length;
|
|
186
|
+
while (end > top && isSep(end - 1))
|
|
187
|
+
end--;
|
|
188
|
+
if (stop < 0)
|
|
189
|
+
stop = end;
|
|
190
|
+
start = end;
|
|
191
|
+
while (start > top && !isSep(start - 1))
|
|
192
|
+
start--;
|
|
193
|
+
const up = start > top ? start - 1 : top;
|
|
194
|
+
if (up === probe.length)
|
|
195
|
+
break; // reached the filesystem root
|
|
196
|
+
probe = target.slice(0, up);
|
|
197
|
+
}
|
|
198
|
+
return { probe, tail: stop < 0 ? "" : target.slice(start, stop) };
|
|
143
199
|
}
|
|
144
200
|
export function resolveSafe(toolName, rel, root = workspaceRoot()) {
|
|
145
201
|
const rootResolved = path.resolve(root);
|
|
@@ -149,6 +205,9 @@ export function resolveSafe(toolName, rel, root = workspaceRoot()) {
|
|
|
149
205
|
if (abs !== rootResolved && !abs.startsWith(`${rootResolved}${path.sep}`)) {
|
|
150
206
|
throw new ToolPermissionError(toolName, rel);
|
|
151
207
|
}
|
|
208
|
+
// Longer than any path can be: refused before the walk below reads it.
|
|
209
|
+
if (abs.length > MAX_PATH_CHARS)
|
|
210
|
+
throw new ToolPermissionError(toolName, rel, tooLong(abs));
|
|
152
211
|
// 2) Symlink-aware containment. The real path must land inside the real
|
|
153
212
|
// root, so an in-workspace symlink pointing at /etc is refused here.
|
|
154
213
|
let rootReal;
|
package/dist/store.js
CHANGED
|
@@ -69,7 +69,11 @@ export function stateRoot(toolName, stateDir) {
|
|
|
69
69
|
*/
|
|
70
70
|
export function statePath(toolName, root, ...segments) {
|
|
71
71
|
const label = segments.join("/");
|
|
72
|
-
|
|
72
|
+
// One string, not one argument per segment: Bun's `path.join` keeps only
|
|
73
|
+
// (argument count mod 65,536) of its arguments, and StateImport's count is
|
|
74
|
+
// the document's. Empty segments are dropped first, as `join` drops them,
|
|
75
|
+
// so `a/` still names `a` and not `a/`.
|
|
76
|
+
const joined = path.join(root.abs, segments.filter((s) => s !== "").join(path.sep));
|
|
73
77
|
if (!isInside(root.abs, joined))
|
|
74
78
|
throw new ToolPermissionError(toolName, label);
|
|
75
79
|
resolveWithin(toolName, root.real, joined, label);
|
package/dist/vector.d.ts
CHANGED
|
@@ -88,6 +88,31 @@ export declare class VectorTargetError extends CrewhausError {
|
|
|
88
88
|
* OPEN when it was not what it said it was — see the three refusals below.
|
|
89
89
|
*/
|
|
90
90
|
export declare function registerVectorTarget(registration: VectorTargetRegistration): void;
|
|
91
|
+
/** The spec's `tool_config.vectorDelete` block: which store to delete from. */
|
|
92
|
+
export type VectorDeleteConfigInput = {
|
|
93
|
+
readonly backend?: unknown;
|
|
94
|
+
readonly url?: unknown;
|
|
95
|
+
readonly collection?: unknown;
|
|
96
|
+
readonly api_key?: unknown;
|
|
97
|
+
readonly apiKey?: unknown;
|
|
98
|
+
readonly protected_collections?: unknown;
|
|
99
|
+
readonly protectedCollections?: unknown;
|
|
100
|
+
readonly count_consistency?: unknown;
|
|
101
|
+
readonly countConsistency?: unknown;
|
|
102
|
+
};
|
|
103
|
+
/** What a spec writes to give VectorDelete a store — quoted by every refusal about it. */
|
|
104
|
+
export declare const VECTOR_DELETE_EXAMPLE = "tool_config.vectorDelete: { backend: qdrant, url: https://qdrant.example:6333, collection: chunks, api_key: $QDRANT_API_KEY }";
|
|
105
|
+
/**
|
|
106
|
+
* Deliver the spec's block at boot: build the named store and register it,
|
|
107
|
+
* with the block's protection list. The api key is read from the environment
|
|
108
|
+
* when written `$VAR`, like every tool_config credential.
|
|
109
|
+
*
|
|
110
|
+
* A block without `backend` registers nothing, so the tool keeps refusing and
|
|
111
|
+
* says what to write — the same as a spec with no block. `in-memory` is
|
|
112
|
+
* refused: a store that starts empty in this process holds nothing to erase,
|
|
113
|
+
* and a delete from it would report a clean success having deleted nothing.
|
|
114
|
+
*/
|
|
115
|
+
export declare function registerVectorDeleteConfig(input: VectorDeleteConfigInput): void;
|
|
91
116
|
export declare function getVectorTarget(): VectorTargetRegistration | undefined;
|
|
92
117
|
/** Test-only — back to "nothing registered", which is the shipped default. */
|
|
93
118
|
export declare function _resetVectorTarget(): void;
|
package/dist/vector.js
CHANGED
|
@@ -36,6 +36,7 @@
|
|
|
36
36
|
* equal the one before it for deletes that did land.
|
|
37
37
|
*/
|
|
38
38
|
import { CrewhausError } from "@crewhaus/errors";
|
|
39
|
+
import { createVectorStore } from "@crewhaus/vector-store";
|
|
39
40
|
import { compareStrings, validateKey } from "./lib/names";
|
|
40
41
|
/** Most ids one call takes. Bounds the work, and the result, per call. */
|
|
41
42
|
export const MAX_VECTOR_IDS = 1_000;
|
|
@@ -105,6 +106,68 @@ export function registerVectorTarget(registration) {
|
|
|
105
106
|
}
|
|
106
107
|
activeTarget = registration;
|
|
107
108
|
}
|
|
109
|
+
/** Backends a delete can reach. An in-process store starts empty in every process. */
|
|
110
|
+
const DELETE_BACKENDS = ["qdrant", "pinecone", "weaviate", "lance"];
|
|
111
|
+
/** What a spec writes to give VectorDelete a store — quoted by every refusal about it. */
|
|
112
|
+
export const VECTOR_DELETE_EXAMPLE = "tool_config.vectorDelete: { backend: qdrant, url: https://qdrant.example:6333, collection: chunks, api_key: $QDRANT_API_KEY }";
|
|
113
|
+
function oneOf(block, snake, camel) {
|
|
114
|
+
const a = Object.hasOwn(block, snake);
|
|
115
|
+
const b = Object.hasOwn(block, camel);
|
|
116
|
+
if (a && b) {
|
|
117
|
+
throw new VectorTargetError(`tool_config.vectorDelete sets both ${snake} and ${camel}. Write it once, as ${snake}.`);
|
|
118
|
+
}
|
|
119
|
+
return a ? block[snake] : block[camel];
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* Deliver the spec's block at boot: build the named store and register it,
|
|
123
|
+
* with the block's protection list. The api key is read from the environment
|
|
124
|
+
* when written `$VAR`, like every tool_config credential.
|
|
125
|
+
*
|
|
126
|
+
* A block without `backend` registers nothing, so the tool keeps refusing and
|
|
127
|
+
* says what to write — the same as a spec with no block. `in-memory` is
|
|
128
|
+
* refused: a store that starts empty in this process holds nothing to erase,
|
|
129
|
+
* and a delete from it would report a clean success having deleted nothing.
|
|
130
|
+
*/
|
|
131
|
+
export function registerVectorDeleteConfig(input) {
|
|
132
|
+
const block = (input ?? {});
|
|
133
|
+
const backend = block["backend"];
|
|
134
|
+
if (backend === undefined)
|
|
135
|
+
return;
|
|
136
|
+
if (typeof backend !== "string" ||
|
|
137
|
+
!DELETE_BACKENDS.includes(backend)) {
|
|
138
|
+
throw new VectorTargetError(`tool_config.vectorDelete.backend must be one of ${DELETE_BACKENDS.join(", ")}, not ${describeStoreValue(backend)}${backend === "in-memory" ? " — an in-memory store starts empty in every process, so there is nothing in it to delete" : ""}. For example: ${VECTOR_DELETE_EXAMPLE}`);
|
|
139
|
+
}
|
|
140
|
+
const url = block["url"];
|
|
141
|
+
const collection = block["collection"];
|
|
142
|
+
const apiKey = oneOf(block, "api_key", "apiKey");
|
|
143
|
+
for (const [name, value] of [
|
|
144
|
+
["url", url],
|
|
145
|
+
["collection", collection],
|
|
146
|
+
["api_key", apiKey],
|
|
147
|
+
]) {
|
|
148
|
+
if (value !== undefined && typeof value !== "string") {
|
|
149
|
+
throw new VectorTargetError(`tool_config.vectorDelete.${name} must be a string.`);
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
const store = createVectorStore({
|
|
153
|
+
backend: backend,
|
|
154
|
+
...(url !== undefined ? { url: url } : {}),
|
|
155
|
+
...(collection !== undefined ? { collection: collection } : {}),
|
|
156
|
+
...(apiKey !== undefined ? { apiKey: apiKey } : {}),
|
|
157
|
+
});
|
|
158
|
+
const protectedCollections = oneOf(block, "protected_collections", "protectedCollections");
|
|
159
|
+
const countConsistency = oneOf(block, "count_consistency", "countConsistency");
|
|
160
|
+
registerVectorTarget({
|
|
161
|
+
store,
|
|
162
|
+
...(collection !== undefined ? { collection: collection } : {}),
|
|
163
|
+
...(protectedCollections !== undefined
|
|
164
|
+
? { protectedCollections: protectedCollections }
|
|
165
|
+
: {}),
|
|
166
|
+
...(countConsistency !== undefined
|
|
167
|
+
? { countConsistency: countConsistency }
|
|
168
|
+
: {}),
|
|
169
|
+
});
|
|
170
|
+
}
|
|
108
171
|
export function getVectorTarget() {
|
|
109
172
|
return activeTarget;
|
|
110
173
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@crewhaus/tool-state",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.8.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Deterministic harness state tools: key-value storage, counters, checkpoints, journals, blackboards and lexical search over local notes",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -8,16 +8,19 @@
|
|
|
8
8
|
"exports": {
|
|
9
9
|
".": {
|
|
10
10
|
"types": "./dist/index.d.ts",
|
|
11
|
-
"import": "./dist/index.js"
|
|
11
|
+
"import": "./dist/index.js",
|
|
12
|
+
"default": "./dist/index.js"
|
|
12
13
|
}
|
|
13
14
|
},
|
|
14
15
|
"scripts": {
|
|
15
16
|
"test": "bun test src"
|
|
16
17
|
},
|
|
17
18
|
"dependencies": {
|
|
18
|
-
"@crewhaus/errors": "0.
|
|
19
|
-
"@crewhaus/tool-builder": "0.
|
|
20
|
-
"@crewhaus/tool-catalog": "0.
|
|
19
|
+
"@crewhaus/errors": "0.8.0",
|
|
20
|
+
"@crewhaus/tool-builder": "0.8.0",
|
|
21
|
+
"@crewhaus/tool-catalog": "0.8.0",
|
|
22
|
+
"@crewhaus/tool-safety": "0.8.0",
|
|
23
|
+
"@crewhaus/vector-store": "0.8.0",
|
|
21
24
|
"zod": "^3.23.8"
|
|
22
25
|
},
|
|
23
26
|
"devDependencies": {
|
|
@@ -41,5 +44,8 @@
|
|
|
41
44
|
"publishConfig": {
|
|
42
45
|
"access": "public"
|
|
43
46
|
},
|
|
44
|
-
"files": ["dist", "README.md", "LICENSE", "NOTICE"]
|
|
47
|
+
"files": ["dist", "README.md", "LICENSE", "NOTICE"],
|
|
48
|
+
"engines": {
|
|
49
|
+
"bun": ">=1.3.14"
|
|
50
|
+
}
|
|
45
51
|
}
|