@yaks/context 0.0.0-stage → 0.2.36

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,202 @@
1
+
2
+ Apache License
3
+ Version 2.0, January 2004
4
+ http://www.apache.org/licenses/
5
+
6
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7
+
8
+ 1. Definitions.
9
+
10
+ "License" shall mean the terms and conditions for use, reproduction,
11
+ and distribution as defined by Sections 1 through 9 of this document.
12
+
13
+ "Licensor" shall mean the copyright owner or entity authorized by
14
+ the copyright owner that is granting the License.
15
+
16
+ "Legal Entity" shall mean the union of the acting entity and all
17
+ other entities that control, are controlled by, or are under common
18
+ control with that entity. For the purposes of this definition,
19
+ "control" means (i) the power, direct or indirect, to cause the
20
+ direction or management of such entity, whether by contract or
21
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
22
+ outstanding shares, or (iii) beneficial ownership of such entity.
23
+
24
+ "You" (or "Your") shall mean an individual or Legal Entity
25
+ exercising permissions granted by this License.
26
+
27
+ "Source" form shall mean the preferred form for making modifications,
28
+ including but not limited to software source code, documentation
29
+ source, and configuration files.
30
+
31
+ "Object" form shall mean any form resulting from mechanical
32
+ transformation or translation of a Source form, including but
33
+ not limited to compiled object code, generated documentation,
34
+ and conversions to other media types.
35
+
36
+ "Work" shall mean the work of authorship, whether in Source or
37
+ Object form, made available under the License, as indicated by a
38
+ copyright notice that is included in or attached to the work
39
+ (an example is provided in the Appendix below).
40
+
41
+ "Derivative Works" shall mean any work, whether in Source or Object
42
+ form, that is based on (or derived from) the Work and for which the
43
+ editorial revisions, annotations, elaborations, or other modifications
44
+ represent, as a whole, an original work of authorship. For the purposes
45
+ of this License, Derivative Works shall not include works that remain
46
+ separable from, or merely link (or bind by name) to the interfaces of,
47
+ the Work and Derivative Works thereof.
48
+
49
+ "Contribution" shall mean any work of authorship, including
50
+ the original version of the Work and any modifications or additions
51
+ to that Work or Derivative Works thereof, that is intentionally
52
+ submitted to Licensor for inclusion in the Work by the copyright owner
53
+ or by an individual or Legal Entity authorized to submit on behalf of
54
+ the copyright owner. For the purposes of this definition, "submitted"
55
+ means any form of electronic, verbal, or written communication sent
56
+ to the Licensor or its representatives, including but not limited to
57
+ communication on electronic mailing lists, source code control systems,
58
+ and issue tracking systems that are managed by, or on behalf of, the
59
+ Licensor for the purpose of discussing and improving the Work, but
60
+ excluding communication that is conspicuously marked or otherwise
61
+ designated in writing by the copyright owner as "Not a Contribution."
62
+
63
+ "Contributor" shall mean Licensor and any individual or Legal Entity
64
+ on behalf of whom a Contribution has been received by Licensor and
65
+ subsequently incorporated within the Work.
66
+
67
+ 2. Grant of Copyright License. Subject to the terms and conditions of
68
+ this License, each Contributor hereby grants to You a perpetual,
69
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70
+ copyright license to reproduce, prepare Derivative Works of,
71
+ publicly display, publicly perform, sublicense, and distribute the
72
+ Work and such Derivative Works in Source or Object form.
73
+
74
+ 3. Grant of Patent License. Subject to the terms and conditions of
75
+ this License, each Contributor hereby grants to You a perpetual,
76
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
+ (except as stated in this section) patent license to make, have made,
78
+ use, offer to sell, sell, import, and otherwise transfer the Work,
79
+ where such license applies only to those patent claims licensable
80
+ by such Contributor that are necessarily infringed by their
81
+ Contribution(s) alone or by combination of their Contribution(s)
82
+ with the Work to which such Contribution(s) was submitted. If You
83
+ institute patent litigation against any entity (including a
84
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
85
+ or a Contribution incorporated within the Work constitutes direct
86
+ or contributory patent infringement, then any patent licenses
87
+ granted to You under this License for that Work shall terminate
88
+ as of the date such litigation is filed.
89
+
90
+ 4. Redistribution. You may reproduce and distribute copies of the
91
+ Work or Derivative Works thereof in any medium, with or without
92
+ modifications, and in Source or Object form, provided that You
93
+ meet the following conditions:
94
+
95
+ (a) You must give any other recipients of the Work or
96
+ Derivative Works a copy of this License; and
97
+
98
+ (b) You must cause any modified files to carry prominent notices
99
+ stating that You changed the files; and
100
+
101
+ (c) You must retain, in the Source form of any Derivative Works
102
+ that You distribute, all copyright, patent, trademark, and
103
+ attribution notices from the Source form of the Work,
104
+ excluding those notices that do not pertain to any part of
105
+ the Derivative Works; and
106
+
107
+ (d) If the Work includes a "NOTICE" text file as part of its
108
+ distribution, then any Derivative Works that You distribute must
109
+ include a readable copy of the attribution notices contained
110
+ within such NOTICE file, excluding those notices that do not
111
+ pertain to any part of the Derivative Works, in at least one
112
+ of the following places: within a NOTICE text file distributed
113
+ as part of the Derivative Works; within the Source form or
114
+ documentation, if provided along with the Derivative Works; or,
115
+ within a display generated by the Derivative Works, if and
116
+ wherever such third-party notices normally appear. The contents
117
+ of the NOTICE file are for informational purposes only and
118
+ do not modify the License. You may add Your own attribution
119
+ notices within Derivative Works that You distribute, alongside
120
+ or as an addendum to the NOTICE text from the Work, provided
121
+ that such additional attribution notices cannot be construed
122
+ as modifying the License.
123
+
124
+ You may add Your own copyright statement to Your modifications and
125
+ may provide additional or different license terms and conditions
126
+ for use, reproduction, or distribution of Your modifications, or
127
+ for any such Derivative Works as a whole, provided Your use,
128
+ reproduction, and distribution of the Work otherwise complies with
129
+ the conditions stated in this License.
130
+
131
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
132
+ any Contribution intentionally submitted for inclusion in the Work
133
+ by You to the Licensor shall be under the terms and conditions of
134
+ this License, without any additional terms or conditions.
135
+ Notwithstanding the above, nothing herein shall supersede or modify
136
+ the terms of any separate license agreement you may have executed
137
+ with Licensor regarding such Contributions.
138
+
139
+ 6. Trademarks. This License does not grant permission to use the trade
140
+ names, trademarks, service marks, or product names of the Licensor,
141
+ except as required for reasonable and customary use in describing the
142
+ origin of the Work and reproducing the content of the NOTICE file.
143
+
144
+ 7. Disclaimer of Warranty. Unless required by applicable law or
145
+ agreed to in writing, Licensor provides the Work (and each
146
+ Contributor provides its Contributions) on an "AS IS" BASIS,
147
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
+ implied, including, without limitation, any warranties or conditions
149
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
+ PARTICULAR PURPOSE. You are solely responsible for determining the
151
+ appropriateness of using or redistributing the Work and assume any
152
+ risks associated with Your exercise of permissions under this License.
153
+
154
+ 8. Limitation of Liability. In no event and under no legal theory,
155
+ whether in tort (including negligence), contract, or otherwise,
156
+ unless required by applicable law (such as deliberate and grossly
157
+ negligent acts) or agreed to in writing, shall any Contributor be
158
+ liable to You for damages, including any direct, indirect, special,
159
+ incidental, or consequential damages of any character arising as a
160
+ result of this License or out of the use or inability to use the
161
+ Work (including but not limited to damages for loss of goodwill,
162
+ work stoppage, computer failure or malfunction, or any and all
163
+ other commercial damages or losses), even if such Contributor
164
+ has been advised of the possibility of such damages.
165
+
166
+ 9. Accepting Warranty or Additional Liability. While redistributing
167
+ the Work or Derivative Works thereof, You may choose to offer,
168
+ and charge a fee for, acceptance of support, warranty, indemnity,
169
+ or other liability obligations and/or rights consistent with this
170
+ License. However, in accepting such obligations, You may act only
171
+ on Your own behalf and on Your sole responsibility, not on behalf
172
+ of any other Contributor, and only if You agree to indemnify,
173
+ defend, and hold each Contributor harmless for any liability
174
+ incurred by, or claims asserted against, such Contributor by reason
175
+ of your accepting any such warranty or additional liability.
176
+
177
+ END OF TERMS AND CONDITIONS
178
+
179
+ APPENDIX: How to apply the Apache License to your work.
180
+
181
+ To apply the Apache License to your work, attach the following
182
+ boilerplate notice, with the fields enclosed by brackets "[]"
183
+ replaced with your own identifying information. (Don't include
184
+ the brackets!) The text should be enclosed in the appropriate
185
+ comment syntax for the file format. We also recommend that a
186
+ file or class name and description of purpose be included on the
187
+ same "printed page" as the copyright notice for easier
188
+ identification within third-party archives.
189
+
190
+ Copyright 2026 Jeff Peterson
191
+
192
+ Licensed under the Apache License, Version 2.0 (the "License");
193
+ you may not use this file except in compliance with the License.
194
+ You may obtain a copy of the License at
195
+
196
+ http://www.apache.org/licenses/LICENSE-2.0
197
+
198
+ Unless required by applicable law or agreed to in writing, software
199
+ distributed under the License is distributed on an "AS IS" BASIS,
200
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201
+ See the License for the specific language governing permissions and
202
+ limitations under the License.
package/README.md CHANGED
@@ -1,3 +1,86 @@
1
- # Temporary Holding Version
1
+ # @yaks/context
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
+ Explicit instruction snapshots for graph transcripts. This package constructs
4
+ prompt entries and hashes source text; the caller decides which sources are
5
+ trusted enough to include in a model request and persists those entries. The
6
+ separate `outputView` helper stores snapshots of large tool results.
7
+
8
+ ## Storage and limitations
9
+
10
+ `promptEntry` returns a bundle: one entity's components as a JSON object,
11
+ including its `entity.eid` identifier. It does not persist the entry. Compose
12
+ the session vocabulary for `entry` and `content`, and optionally `@yaks/blob`
13
+ for content-addressed storage of text. The calling program decides when to load
14
+ files and when to admit their snapshots.
15
+
16
+ The file loader records disk provenance. It does not resolve graph references
17
+ from generated file headers. `revision` hashes one text snapshot, not an entire
18
+ provider request, and does not imply provider cache validity.
19
+
20
+ `instructionFiles(cwd, home?)` from `@yaks/context/host` reads
21
+ `~/.agents/AGENTS.md` first, followed by `AGENTS.md` files from the filesystem
22
+ root down to `cwd`. It resolves symlinks, deduplicates real paths, skips missing
23
+ files, and propagates other read errors. It uses Deno filesystem APIs and is
24
+ POSIX-oriented. The core has no filesystem dependency. Snapshot immutability is
25
+ an admission convention, not a graph write guard: callers with graph write
26
+ access can still edit historical entries.
27
+
28
+ The root module exports `contextDoc`, `snapshot`, `promptEntry`, `outputView`,
29
+ `outputValue`, `OUTPUT_LIMIT`, and the `Snapshot` and `SourceResolver` types.
30
+ `./vocab` exports the component document and its `docs` list without filesystem
31
+ code.
32
+
33
+ ## Construct an entry
34
+
35
+ ```ts
36
+ import { promptEntry, snapshot } from '@yaks/context'
37
+
38
+ const source = await snapshot('Answer in English.', 'application:instructions')
39
+ const entry = promptEntry(
40
+ 'session-1',
41
+ 1,
42
+ source.body,
43
+ source.source,
44
+ 'shared',
45
+ source.revision,
46
+ )
47
+ // Persist entry using your graph. The session must exist, and seq must be allocated
48
+ // by the caller to preserve transcript order.
49
+ ```
50
+
51
+ ## Large tool results
52
+
53
+ `outputView(graph, resultEntry, limit = 16384)` produces a model-facing view of
54
+ a stored tool result. Results within the limit are unchanged. Larger results get
55
+ a JSON summary with its id, byte size, shape, preview, revision and inspection
56
+ instructions. The limit counts UTF-8 bytes. User messages and admitted
57
+ instructions are not shortened.
58
+
59
+ The full original entry remains available to the UI. A `context_output` row
60
+ stores a revisioned copy, using the existing blob-backed body property.
61
+ Structured answers retain their full data and accompanying content; JSON text is
62
+ parsed for shape inspection, while ordinary prose stays text. A multi-user host
63
+ supplies `outputView`'s optional `keep` callback to scope snapshots to the
64
+ reader. `@yaks/context/result` supplies the shared summary format. Identical
65
+ text is deduplicated by `@yaks/blob`; editing the original entry does not alter
66
+ the copy. Its source is the original entry EID. These rows are data, not
67
+ instructions or transcript entries, and do not wake sessions. A fork uses the
68
+ same snapshot address for inherited results. The session package accepts an
69
+ optional `resultText` projection callback; it does not depend on the context
70
+ package.
71
+
72
+ The harness applies this projection to every tool, including third-party MCP
73
+ servers, and supplies `result_query` and value-inspection tools even for custom
74
+ tool sets. `agent({ outputLimit })` configures the per-result byte limit
75
+ (integer at least 512). There is no aggregate request budget yet: many
76
+ individually small results can still produce a large request. Previously sent
77
+ provider context is not retroactively removed. Snapshot generation hashes and
78
+ reads the full text; it bounds model input, not storage I/O or memory use.
79
+ Changing policy changes future model-facing projections, not stored transcript
80
+ history.
81
+
82
+ Snapshots are regular graph data with the same access policy as other entities.
83
+ They are not a security boundary against callers with graph write access.
84
+ Passing the advertised revision to an inspection tool detects a modified value
85
+ rather than silently reading its replacement. Automatic snapshot garbage
86
+ collection is not implemented.
@@ -0,0 +1,4 @@
1
+ import { type Snapshot } from "./mod.js";
2
+ /** Global first, then root-to-leaf directory guidance. Missing files are fine;
3
+ * permission/read errors are not. No mtimes affect instruction precedence. */ export declare let instructionFiles: (cwd: string, home?: string | undefined) => Promise<Snapshot[]>;
4
+ //# sourceMappingURL=host.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"host.d.ts","sources":["../host.ts"],"names":[],"mappings":"AAEA,SAAS,KAAK,QAAQ,mBAA4B;AAClD;4EAC4E,GAC5E,OAAO,YAAI,mBACT,KAAK,MAAM,EACX,OAAM,MAAM,GAAG,SAAS,KACvB,QAAQ,YA4BV"}
package/_dist/mod.d.ts ADDED
@@ -0,0 +1,13 @@
1
+ /** Explicit instruction snapshots; storage and session execution are separate. */ import type { Bundle } from "@yaks/graph";
2
+ import { contextDoc } from "./vocab.js";
3
+ export { contextDoc };
4
+ export declare let promptEntry: (session: string, seq: number | undefined, body: string, source: string, scope?: string, revision?: string) => Bundle;
5
+ /** Resolver results are text snapshots, not privileged until explicitly admitted. */ export type Snapshot = {
6
+ body: string;
7
+ source: string;
8
+ revision: string;
9
+ };
10
+ export type SourceResolver = (source: string) => Promise<Snapshot | undefined>;
11
+ /** Freeze a source revision without coupling context to a storage backend. */ export declare let snapshot: (body: string, source: string) => Promise<Snapshot>;
12
+ export { type KeepOutput, OUTPUT_LIMIT, outputValue, outputView } from "./output.js";
13
+ //# sourceMappingURL=mod.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"mod.d.ts","sources":["../mod.ts"],"names":[],"mappings":"AAAA,gFAAgF,GAChF,cAAc,MAAM,2BAAgC;AACpD,SAAS,UAAU,qBAAoB;AAEvC,SAAS,UAAU,GAAE;AAErB,OAAO,YAAI,cACT,SAAS,MAAM,EACf,KAAK,MAAM,GAAG,SAAS,EACvB,MAAM,MAAM,EACZ,QAAQ,MAAM,EACd,gBACA,WAAW,MAAM,KAChB,OAKD;AAEF,mFAAmF,GACnF,YAAY;EAAa,MAAM,MAAM;EAAE,QAAQ,MAAM;EAAE,UAAU,MAAM;;AACvE,YAAY,kBAAkB,QAAQ,MAAM,KAAK,QAAQ,WAAW,SAAS;AAE7E,4EAA4E,GAC5E,OAAO,YAAI,WACT,MAAM,MAAM,EACZ,QAAQ,MAAM,KACb,QAAQ,UAUV;AAED,SACE,KAAK,UAAU,EACf,YAAY,EACZ,WAAW,EACX,UAAU,sBACQ"}
@@ -0,0 +1,6 @@
1
+ /** Model-facing views of complete tool results. Programs keep their full answers. */ import type { Bundle, Graph } from "@yaks/graph";
2
+ export declare const OUTPUT_LIMIT: 16_384;
3
+ export type KeepOutput = (value: unknown, source: Bundle) => Promise<unknown>;
4
+ /** JSON answers retain their structure; ordinary tool prose remains text. */ export declare let outputValue: (text: string) => unknown;
5
+ /** The source entry remains unchanged. Hosts may supply a scoped snapshot store. */ export declare const outputView: (g: Graph, entry: Bundle, limit: typeof OUTPUT_LIMIT, keep?: KeepOutput) => Promise<string>;
6
+ //# sourceMappingURL=output.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"output.d.ts","sources":["../output.ts"],"names":[],"mappings":"AAAA,mFAAmF,GACnF,cAAc,MAAM,EAAQ,KAAK,2BAAgC;AAKjE,OAAO,cAAM,cAAe,OAAM;AAClC,YAAY,cAAc,OAAO,OAAO,EAAE,QAAQ,WAAW,QAAQ,OAAO;AAE5E,2EAA2E,GAC3E,OAAO,YAAI,cAAe,MAAM,MAAM,KAAG,OAAO,CAM/C;AAWD,kFAAkF,GAClF,OAAO,cAAM,aACX,GAAG,OACH,OAAO,QACP,cAAQ,cACR,OAAO,eACN,QAAQ,MAAM,EAkChB"}
@@ -0,0 +1,34 @@
1
+ export declare let RESULT_LIMIT: number;
2
+ export declare let RESULT_MAX: number;
3
+ /** Regex escaping as it appears in a result_query JSON argument. */ export declare let jqRegex: string;
4
+ /**
5
+ * The values a jq filter makes of a kept result, with bounded work even for
6
+ * a recursive filter. Preserve backslashes in escapes jq does not recognize,
7
+ * so regex escapes can be entered directly. Refusals describe the root and
8
+ * can point to an offending expression or give a query the caller can retry.
9
+ *
10
+ * ```ts
11
+ * import { equal } from '@yaks/testing'
12
+ * equal(jqOver('.a', { a: 1 }), [1])
13
+ * ```
14
+ */ export declare let jqOver: (filter: string, value: unknown) => unknown[];
15
+ export declare let resultBytes: (value: unknown) => number;
16
+ type Shape = {
17
+ type: string;
18
+ keys?: string[];
19
+ characters?: number;
20
+ rows?: number;
21
+ path?: string;
22
+ columns?: string[];
23
+ };
24
+ declare let shape: (value: unknown) => Shape;
25
+ export type ResultSummary = {
26
+ id: string;
27
+ bytes: number;
28
+ shape: ReturnType<typeof shape>;
29
+ preview: unknown;
30
+ message: string;
31
+ };
32
+ export declare let resultSummary: (id: string, value: unknown, bytes?: number) => ResultSummary;
33
+ export declare let RESULT_SCHEMA: Record<string, unknown>;
34
+ //# sourceMappingURL=result.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"result.d.ts","sources":["../result.ts"],"names":[],"mappings":"AAUA,OAAO,YAAI,cAAc,MAAM,CAAS;AACxC,OAAO,YAAI,YAAY,MAAM,CAAa;AAK1C,kEAAkE,GAClE,OAAO,YAAI,SAAS,MAAM,CACwD;AAgKlF;;;;;;;;;;CAUC,GACD,OAAO,YAAI,SACT,QAAQ,MAAM,EACd,OAAO,OAAO,KAEb,OAAO,GAaT;AACD,OAAO,YAAI,cAAe,OAAO,OAAO,KAAG,MAAM,CACiB;KA2B7D;EACH,MAAM,MAAM;EACZ,OAAO,MAAM;EACb,aAAa,MAAM;EACnB,OAAO,MAAM;EACb,OAAO,MAAM;EACb,UAAU,MAAM;;AAElB,YAAI,QAAS,OAAO,OAAO,KAAG;AA2C9B,YAAY;EACV,IAAI,MAAM;EACV,OAAO,MAAM;EACb,OAAO,kBAAkB;EACzB,SAAS,OAAO;EAChB,SAAS,MAAM;;AAEjB,OAAO,YAAI,gBACT,IAAI,MAAM,EACV,OAAO,OAAO,EACd,QAAO,MAAM,KACZ,cAQD;AAEF,OAAO,YAAI,eAAe,OAAO,MAAM,EAAE,OAAO,EAa/C"}
@@ -0,0 +1,6 @@
1
+ import type { VocabDoc } from "@yaks/vocab";
2
+ /** The `prompt` entry's vocabulary, in the form `loadVocab` accepts. */ export declare let contextDoc: VocabDoc;
3
+ /** What this package is: its manifest's description, the one place it is
4
+ * written, which a host stamps on each document here. */ export declare let description: string;
5
+ /** Every document this plugin declares. */ export declare let docs: VocabDoc[];
6
+ //# sourceMappingURL=vocab.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"vocab.d.ts","sources":["../vocab.ts"],"names":[],"mappings":"AAIA,cAAc,QAAQ,2BAAgC;AAItD,sEAAsE,GACtE,OAAO,YAAI,YAAY,SAAc;AAErC;uDACuD,GACvD,OAAO,YAAI,aAAa,MAAM,CAAuB;AAErD,yCAAyC,GACzC,OAAO,YAAI,MAAM,WAAyB"}
package/deno.json ADDED
@@ -0,0 +1 @@
1
+ {"publish":{"exclude":["**/*_test.ts"]},"name":"@yaks/context","version":"0.2.36","license":"Apache-2.0","description":"Constructs instruction snapshots and prompt entries for graph transcripts, leaving admission, storage, and execution to the caller.","exports":{".":"./mod.ts","./host":"./host.ts","./result":"./result.ts","./vocab":"./vocab.ts"},"imports":{"@yaks/context":"./mod.ts","@yaks/context/host":"./host.ts","@yaks/context/result":"./result.ts","@yaks/context/vocab":"./vocab.ts"}}
package/host.js ADDED
@@ -0,0 +1,34 @@
1
+ import * as io from "@yaks/file/io";
2
+ import * as system from "@yaks/process/system";
3
+ import { snapshot } from "./mod.js";
4
+ /** Global first, then root-to-leaf directory guidance. Missing files are fine;
5
+ * permission/read errors are not. No mtimes affect instruction precedence. */ export let instructionFiles = async (cwd, home = system.env.get('HOME'))=>{
6
+ let directory = await io.realPath(cwd);
7
+ let paths = home ? [
8
+ home + '/.agents/AGENTS.md'
9
+ ] : [];
10
+ let ancestors = [];
11
+ for(;;){
12
+ ancestors.unshift(directory == '/' ? '/AGENTS.md' : directory + '/AGENTS.md');
13
+ if (directory == '/') break;
14
+ directory = directory.slice(0, directory.lastIndexOf('/')) || '/';
15
+ }
16
+ paths.push(...ancestors);
17
+ let seen = new Set();
18
+ let out = [];
19
+ for (let path of paths){
20
+ let source;
21
+ try {
22
+ source = await io.realPath(path);
23
+ } catch (e) {
24
+ if (io.isNotFound(e)) continue;
25
+ throw e;
26
+ }
27
+ if (seen.has(source)) continue;
28
+ seen.add(source);
29
+ let body = await io.readTextFile(source);
30
+ out.push(await snapshot(body, source));
31
+ }
32
+ return out;
33
+ };
34
+ //# sourceMappingURL=host.js.map
package/host.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"host.js","sources":["./host.ts"],"names":[],"mappings":"AAAA,YAAY,6BAAkC;AAC9C,YAAY,wCAA6C;AACzD,SAAwB,QAAQ,mBAAkB;AAClD;4EAC4E,GAC5E,OAAO,IAAI,mBAAmB,OAC5B,KACA,OAA2B,OAAO,GAAG,CAAC,GAAG,CAAC,OAAO;EAEjD,IAAI,YAAY,MAAM,GAAG,QAAQ,CAAC;EAClC,IAAI,QAAQ,OAAO;IAAC,OAAO;GAAqB,GAAG,EAAE;EACrD,IAAI,YAAsB,EAAE;EAC5B,OAAS;IACP,UAAU,OAAO,CACf,aAAa,MAAM,eAAe,YAAY;IAEhD,IAAI,aAAa,KAAK;IACtB,YAAY,UAAU,KAAK,CAAC,GAAG,UAAU,WAAW,CAAC,SAAS;EAChE;EACA,MAAM,IAAI,IAAI;EACd,IAAI,OAAO,IAAI;EACf,IAAI,MAAkB,EAAE;EACxB,KAAK,IAAI,QAAQ,MAAO;IACtB,IAAI;IACJ,IAAI;MACF,SAAS,MAAM,GAAG,QAAQ,CAAC;IAC7B,EAAE,OAAO,GAAG;MACV,IAAI,GAAG,UAAU,CAAC,IAAI;MACtB,MAAM;IACR;IACA,IAAI,KAAK,GAAG,CAAC,SAAS;IACtB,KAAK,GAAG,CAAC;IACT,IAAI,OAAO,MAAM,GAAG,YAAY,CAAC;IACjC,IAAI,IAAI,CAAC,MAAM,SAAS,MAAM;EAChC;EACA,OAAO;AACT,EAAC"}
package/host.ts ADDED
@@ -0,0 +1,37 @@
1
+ import * as io from '@yaks/file/io'
2
+ import * as system from '@yaks/process/system'
3
+ import { type Snapshot, snapshot } from './mod.js'
4
+ /** Global first, then root-to-leaf directory guidance. Missing files are fine;
5
+ * permission/read errors are not. No mtimes affect instruction precedence. */
6
+ export let instructionFiles = async (
7
+ cwd: string,
8
+ home: string | undefined = system.env.get('HOME'),
9
+ ): Promise<Snapshot[]> => {
10
+ let directory = await io.realPath(cwd)
11
+ let paths = home ? [home + '/.agents/AGENTS.md'] : []
12
+ let ancestors: string[] = []
13
+ for (;;) {
14
+ ancestors.unshift(
15
+ directory == '/' ? '/AGENTS.md' : directory + '/AGENTS.md',
16
+ )
17
+ if (directory == '/') break
18
+ directory = directory.slice(0, directory.lastIndexOf('/')) || '/'
19
+ }
20
+ paths.push(...ancestors)
21
+ let seen = new Set<string>()
22
+ let out: Snapshot[] = []
23
+ for (let path of paths) {
24
+ let source: string
25
+ try {
26
+ source = await io.realPath(path)
27
+ } catch (e) {
28
+ if (io.isNotFound(e)) continue
29
+ throw e
30
+ }
31
+ if (seen.has(source)) continue
32
+ seen.add(source)
33
+ let body = await io.readTextFile(source)
34
+ out.push(await snapshot(body, source))
35
+ }
36
+ return out
37
+ }
package/mod.js ADDED
@@ -0,0 +1,34 @@
1
+ /** Explicit instruction snapshots; storage and session execution are separate. */ import { contextDoc } from "./vocab.js";
2
+ export { contextDoc };
3
+ export let promptEntry = (session, seq, body, source, scope = 'shared', revision)=>({
4
+ entity: {
5
+ eid: crypto.randomUUID()
6
+ },
7
+ entry: {
8
+ session,
9
+ ...seq == null ? {} : {
10
+ seq
11
+ }
12
+ },
13
+ prompt: {
14
+ scope,
15
+ source,
16
+ ...revision ? {
17
+ revision
18
+ } : {}
19
+ },
20
+ content: {
21
+ body
22
+ }
23
+ });
24
+ /** Freeze a source revision without coupling context to a storage backend. */ export let snapshot = async (body, source)=>{
25
+ let digest = await crypto.subtle.digest('SHA-256', new TextEncoder().encode(body));
26
+ let revision = Array.from(new Uint8Array(digest), (n)=>n.toString(16).padStart(2, '0')).join('');
27
+ return {
28
+ body,
29
+ source,
30
+ revision
31
+ };
32
+ };
33
+ export { OUTPUT_LIMIT, outputValue, outputView } from "./output.js";
34
+ //# sourceMappingURL=mod.js.map
package/mod.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"mod.js","sources":["./mod.ts"],"names":[],"mappings":"AAAA,gFAAgF,GAEhF,SAAS,UAAU,qBAAoB;AAEvC,SAAS,UAAU,GAAE;AAErB,OAAO,IAAI,cAAc,CACvB,SACA,KACA,MACA,QACA,QAAQ,QAAQ,EAChB,WACW,CAAC;IACZ,QAAQ;MAAE,KAAK,OAAO,UAAU;IAAG;IACnC,OAAO;MAAE;MAAS,GAAG,OAAO,OAAO,CAAC,IAAI;QAAE;MAAI,CAAC;IAAC;IAChD,QAAQ;MAAE;MAAO;MAAQ,GAAG,WAAW;QAAE;MAAS,IAAI,CAAC,CAAC;IAAC;IACzD,SAAS;MAAE;IAAK;EAClB,CAAC,EAAC;AAMF,4EAA4E,GAC5E,OAAO,IAAI,WAAW,OACpB,MACA;EAEA,IAAI,SAAS,MAAM,OAAO,MAAM,CAAC,MAAM,CACrC,WACA,IAAI,cAAc,MAAM,CAAC;EAE3B,IAAI,WAAW,MAAM,IAAI,CACvB,IAAI,WAAW,SACf,CAAC,IAAM,EAAE,QAAQ,CAAC,IAAI,QAAQ,CAAC,GAAG,MAClC,IAAI,CAAC;EACP,OAAO;IAAE;IAAM;IAAQ;EAAS;AAClC,EAAC;AAED,SAEE,YAAY,EACZ,WAAW,EACX,UAAU,sBACQ"}
package/mod.ts ADDED
@@ -0,0 +1,46 @@
1
+ /** Explicit instruction snapshots; storage and session execution are separate. */
2
+ import type { Bundle } from 'jsr:@yaks/graph@0.2.36'
3
+ import { contextDoc } from './vocab.js'
4
+
5
+ export { contextDoc }
6
+
7
+ export let promptEntry = (
8
+ session: string,
9
+ seq: number | undefined,
10
+ body: string,
11
+ source: string,
12
+ scope = 'shared',
13
+ revision?: string,
14
+ ): Bundle => ({
15
+ entity: { eid: crypto.randomUUID() },
16
+ entry: { session, ...seq == null ? {} : { seq } },
17
+ prompt: { scope, source, ...revision ? { revision } : {} },
18
+ content: { body },
19
+ })
20
+
21
+ /** Resolver results are text snapshots, not privileged until explicitly admitted. */
22
+ export type Snapshot = { body: string; source: string; revision: string }
23
+ export type SourceResolver = (source: string) => Promise<Snapshot | undefined>
24
+
25
+ /** Freeze a source revision without coupling context to a storage backend. */
26
+ export let snapshot = async (
27
+ body: string,
28
+ source: string,
29
+ ): Promise<Snapshot> => {
30
+ let digest = await crypto.subtle.digest(
31
+ 'SHA-256',
32
+ new TextEncoder().encode(body),
33
+ )
34
+ let revision = Array.from(
35
+ new Uint8Array(digest),
36
+ (n) => n.toString(16).padStart(2, '0'),
37
+ ).join('')
38
+ return { body, source, revision }
39
+ }
40
+
41
+ export {
42
+ type KeepOutput,
43
+ OUTPUT_LIMIT,
44
+ outputValue,
45
+ outputView,
46
+ } from './output.js'
package/output.js ADDED
@@ -0,0 +1,74 @@
1
+ /** Model-facing views of complete tool results. Programs keep their full answers. */ import { snapshot } from "./mod.js";
2
+ import { derivedEid } from "@yaks/graph/identity";
3
+ import { resultBytes, resultSummary } from "./result.js";
4
+ export const OUTPUT_LIMIT = 16_384;
5
+ /** JSON answers retain their structure; ordinary tool prose remains text. */ export let outputValue = (text)=>{
6
+ try {
7
+ return JSON.parse(text);
8
+ } catch {
9
+ return text;
10
+ }
11
+ };
12
+ // A result's `result` with its value, read by name where the line was read
13
+ // without it.
14
+ let valued = async (g, entry)=>{
15
+ let result = entry.result;
16
+ if (!result || 'value' in result) return result ?? {};
17
+ let [row] = await g.get([
18
+ entry.entity.eid
19
+ ], [
20
+ 'result'
21
+ ]);
22
+ return row?.result ?? result;
23
+ };
24
+ /** The source entry remains unchanged. Hosts may supply a scoped snapshot store. */ export const outputView = async (g, entry, limit = OUTPUT_LIMIT, keep)=>{
25
+ if (!Number.isSafeInteger(limit) || limit < 512) {
26
+ throw new Error('output limit must be an integer >= 512');
27
+ }
28
+ let text = String(entry.content?.body ?? '');
29
+ // The model receives the tool's prose. A concise view (search hits, for
30
+ // example) stays useful even when its backing rows carry large bodies, so
31
+ // the value is read only where there is no prose or too much of it: a
32
+ // transcript is read without its results' values (@yaks/session `reach`).
33
+ let fits = new TextEncoder().encode(text).length <= limit;
34
+ if (fits && text.length > 0) return text;
35
+ // A result keeps its tool's answer as data in `result{value}`; an answer
36
+ // not yet landed carries it in `output{value}`.
37
+ let kept = (await valued(g, entry)).value ?? entry.output?.value;
38
+ let value = kept ?? outputValue(text);
39
+ if (fits && resultBytes(value) <= limit) return text;
40
+ if (keep) return JSON.stringify(await keep(value, entry));
41
+ let body = kept == null ? text : JSON.stringify(value);
42
+ let revision = (await snapshot(body, entry.entity.eid)).revision;
43
+ let handle = derivedEid('output:' + entry.entity.eid + ':' + revision);
44
+ let [existing] = await g.get([
45
+ handle
46
+ ], [
47
+ 'context_output'
48
+ ]);
49
+ if (!existing?.context_output) {
50
+ await g.apply([
51
+ {
52
+ entity: {
53
+ eid: handle
54
+ },
55
+ context_output: {
56
+ source: entry.entity.eid,
57
+ revision,
58
+ body
59
+ }
60
+ }
61
+ ], {
62
+ trusted: true
63
+ });
64
+ } else if (existing.context_output.body !== body) {
65
+ throw new Error('Stored output snapshot does not match its revision');
66
+ }
67
+ let summary = resultSummary(handle, value);
68
+ summary.message = 'The complete result is kept in the graph. Use result_query({id, query}) with jq syntax, or graph_value_read and graph_value_search with entity=id, component=context_output, property=body. The preview is abbreviated.';
69
+ return JSON.stringify({
70
+ ...summary,
71
+ revision
72
+ });
73
+ };
74
+ //# sourceMappingURL=output.js.map
package/output.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"output.js","sources":["./output.ts"],"names":[],"mappings":"AAAA,mFAAmF,GAEnF,SAAS,QAAQ,mBAAkB;AACnC,SAAS,UAAU,oCAAyC;AAC5D,SAAS,WAAW,EAAE,aAAa,sBAAqB;AAExD,OAAO,MAAM,eAAe,OAAM;AAGlC,2EAA2E,GAC3E,OAAO,IAAI,cAAc,CAAC;EACxB,IAAI;IACF,OAAO,KAAK,KAAK,CAAC;EACpB,EAAE,OAAM;IACN,OAAO;EACT;AACF,EAAC;AAED,2EAA2E;AAC3E,cAAc;AACd,IAAI,SAAS,OAAO,GAAU;EAC5B,IAAI,SAAS,MAAM,MAAM;EACzB,IAAI,CAAC,UAAU,WAAW,QAAQ,OAAO,UAAU,CAAC;EACpD,IAAI,CAAC,IAAI,GAAG,MAAM,EAAE,GAAG,CAAC;IAAC,MAAM,MAAM,CAAC,GAAG;GAAC,EAAE;IAAC;GAAS;EACtD,OAAO,AAAC,KAAK,UAA+B;AAC9C;AAEA,kFAAkF,GAClF,OAAO,MAAM,aAAa,OACxB,GACA,OACA,QAAQ,YAAY,EACpB;EAEA,IAAI,CAAC,OAAO,aAAa,CAAC,UAAU,QAAQ,KAAK;IAC/C,MAAM,IAAI,MAAM;EAClB;EACA,IAAI,OAAO,OAAO,AAAC,MAAM,OAAO,EAAuB,QAAQ;EAC/D,wEAAwE;EACxE,0EAA0E;EAC1E,sEAAsE;EACtE,0EAA0E;EAC1E,IAAI,OAAO,IAAI,cAAc,MAAM,CAAC,MAAM,MAAM,IAAI;EACpD,IAAI,QAAQ,KAAK,MAAM,GAAG,GAAG,OAAO;EACpC,yEAAyE;EACzE,gDAAgD;EAChD,IAAI,OAAO,CAAC,MAAM,OAAO,GAAG,MAAM,EAAE,KAAK,IACtC,MAAM,MAAM,EAAuB;EACtC,IAAI,QAAQ,QAAQ,YAAY;EAChC,IAAI,QAAQ,YAAY,UAAU,OAAO,OAAO;EAChD,IAAI,MAAM,OAAO,KAAK,SAAS,CAAC,MAAM,KAAK,OAAO;EAClD,IAAI,OAAO,QAAQ,OAAO,OAAO,KAAK,SAAS,CAAC;EAChD,IAAI,WAAW,CAAC,MAAM,SAAS,MAAM,MAAM,MAAM,CAAC,GAAG,CAAC,EAAE,QAAQ;EAChE,IAAI,SAAS,WAAW,YAAY,MAAM,MAAM,CAAC,GAAG,GAAG,MAAM;EAC7D,IAAI,CAAC,SAAS,GAAG,MAAM,EAAE,GAAG,CAAC;IAAC;GAAO,EAAE;IAAC;GAAiB;EACzD,IAAI,CAAC,UAAU,gBAAgB;IAC7B,MAAM,EAAE,KAAK,CAAC;MAAC;QACb,QAAQ;UAAE,KAAK;QAAO;QACtB,gBAAgB;UAAE,QAAQ,MAAM,MAAM,CAAC,GAAG;UAAE;UAAU;QAAK;MAC7D;KAAE,EAAE;MAAE,SAAS;IAAK;EACtB,OAAO,IAAI,AAAC,SAAS,cAAc,CAAU,IAAI,KAAK,MAAM;IAC1D,MAAM,IAAI,MAAM;EAClB;EACA,IAAI,UAAU,cAAc,QAAQ;EACpC,QAAQ,OAAO,GACb;EACF,OAAO,KAAK,SAAS,CAAC;IAAE,GAAG,OAAO;IAAE;EAAS;AAC/C,EAAC"}