@chopin-ai/core 0.0.0-stage → 0.3.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.
Files changed (90) hide show
  1. package/LICENSE +202 -0
  2. package/dist/cli.d.ts +16 -0
  3. package/dist/cli.js +43 -0
  4. package/dist/cli.js.map +1 -0
  5. package/dist/evidence/classify.d.ts +31 -0
  6. package/dist/evidence/classify.js +87 -0
  7. package/dist/evidence/classify.js.map +1 -0
  8. package/dist/evidence/corrections.d.ts +14 -0
  9. package/dist/evidence/corrections.js +61 -0
  10. package/dist/evidence/corrections.js.map +1 -0
  11. package/dist/evidence/failures.d.ts +44 -0
  12. package/dist/evidence/failures.js +91 -0
  13. package/dist/evidence/failures.js.map +1 -0
  14. package/dist/evidence/messages-jsonl.d.ts +7 -0
  15. package/dist/evidence/messages-jsonl.js +97 -0
  16. package/dist/evidence/messages-jsonl.js.map +1 -0
  17. package/dist/evidence/points.d.ts +26 -0
  18. package/dist/evidence/points.js +72 -0
  19. package/dist/evidence/points.js.map +1 -0
  20. package/dist/evidence/sanitize.d.ts +16 -0
  21. package/dist/evidence/sanitize.js +70 -0
  22. package/dist/evidence/sanitize.js.map +1 -0
  23. package/dist/evidence/signals.d.ts +25 -0
  24. package/dist/evidence/signals.js +28 -0
  25. package/dist/evidence/signals.js.map +1 -0
  26. package/dist/evidence/usage.d.ts +12 -0
  27. package/dist/evidence/usage.js +24 -0
  28. package/dist/evidence/usage.js.map +1 -0
  29. package/dist/fault.d.ts +3 -0
  30. package/dist/fault.js +4 -0
  31. package/dist/fault.js.map +1 -0
  32. package/dist/gate/admission.d.ts +34 -0
  33. package/dist/gate/admission.js +2 -0
  34. package/dist/gate/admission.js.map +1 -0
  35. package/dist/gate/daily.d.ts +92 -0
  36. package/dist/gate/daily.js +136 -0
  37. package/dist/gate/daily.js.map +1 -0
  38. package/dist/gate/ledger.d.ts +29 -0
  39. package/dist/gate/ledger.js +88 -0
  40. package/dist/gate/ledger.js.map +1 -0
  41. package/dist/gate/sequential.d.ts +39 -0
  42. package/dist/gate/sequential.js +84 -0
  43. package/dist/gate/sequential.js.map +1 -0
  44. package/dist/index.d.ts +51 -0
  45. package/dist/index.js +28 -0
  46. package/dist/index.js.map +1 -0
  47. package/dist/judge/audit.d.ts +45 -0
  48. package/dist/judge/audit.js +55 -0
  49. package/dist/judge/audit.js.map +1 -0
  50. package/dist/judge/calibration.d.ts +70 -0
  51. package/dist/judge/calibration.js +128 -0
  52. package/dist/judge/calibration.js.map +1 -0
  53. package/dist/judge/continuation.d.ts +16 -0
  54. package/dist/judge/continuation.js +48 -0
  55. package/dist/judge/continuation.js.map +1 -0
  56. package/dist/layer/gates.d.ts +20 -0
  57. package/dist/layer/gates.js +37 -0
  58. package/dist/layer/gates.js.map +1 -0
  59. package/dist/layer/layer.d.ts +28 -0
  60. package/dist/layer/layer.js +37 -0
  61. package/dist/layer/layer.js.map +1 -0
  62. package/dist/layer/mcp.d.ts +19 -0
  63. package/dist/layer/mcp.js +93 -0
  64. package/dist/layer/mcp.js.map +1 -0
  65. package/dist/layer/skills.d.ts +10 -0
  66. package/dist/layer/skills.js +60 -0
  67. package/dist/layer/skills.js.map +1 -0
  68. package/dist/layer/versions.d.ts +71 -0
  69. package/dist/layer/versions.js +154 -0
  70. package/dist/layer/versions.js.map +1 -0
  71. package/dist/meta.d.ts +22 -0
  72. package/dist/meta.js +10 -0
  73. package/dist/meta.js.map +1 -0
  74. package/dist/propose/brief.d.ts +31 -0
  75. package/dist/propose/brief.js +52 -0
  76. package/dist/propose/brief.js.map +1 -0
  77. package/dist/propose/candidate.d.ts +15 -0
  78. package/dist/propose/candidate.js +30 -0
  79. package/dist/propose/candidate.js.map +1 -0
  80. package/dist/propose/rejections.d.ts +35 -0
  81. package/dist/propose/rejections.js +35 -0
  82. package/dist/propose/rejections.js.map +1 -0
  83. package/dist/propose/skill.d.ts +4 -0
  84. package/dist/propose/skill.js +34 -0
  85. package/dist/propose/skill.js.map +1 -0
  86. package/dist/transcript.d.ts +21 -0
  87. package/dist/transcript.js +13 -0
  88. package/dist/transcript.js.map +1 -0
  89. package/package.json +26 -3
  90. package/README.md +0 -3
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 [yyyy] [name of copyright owner]
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/dist/cli.d.ts ADDED
@@ -0,0 +1,16 @@
1
+ export interface CliIo {
2
+ out(text: string): void;
3
+ err(text: string): void;
4
+ }
5
+ /** A `chopin` subcommand; `name` may span several words, such as `layer check`. */
6
+ export interface Command {
7
+ name: string;
8
+ /** Its arguments, as shown in the usage. */
9
+ args: string;
10
+ summary: string;
11
+ /** @returns The process exit code. */
12
+ run(args: readonly string[], io: CliIo): Promise<number>;
13
+ }
14
+ export declare const coreCommands: readonly Command[];
15
+ /** @returns The process exit code. */
16
+ export declare function runCli(argv: readonly string[], io: CliIo, commands?: readonly Command[]): Promise<number>;
package/dist/cli.js ADDED
@@ -0,0 +1,43 @@
1
+ import { resolve } from "node:path";
2
+ import { loadLayer } from "./layer/layer.js";
3
+ export const coreCommands = [{
4
+ name: "layer check",
5
+ args: "<dir>",
6
+ summary: "validate a mutable layer directory",
7
+ run: async (args, io) => {
8
+ if (args.length !== 1)
9
+ return usageError(io, "layer check <dir>");
10
+ return layerCheck(resolve(args[0] ?? ""), io);
11
+ },
12
+ }];
13
+ /** @returns The process exit code. */
14
+ export async function runCli(argv, io, commands = coreCommands) {
15
+ const command = commands.find((c) => c.name.split(" ").every((word, i) => argv[i] === word));
16
+ if (command !== undefined)
17
+ return command.run(argv.slice(command.name.split(" ").length), io);
18
+ const asked = argv.length === 0 || ["help", "--help", "-h"].includes(argv[0] ?? "");
19
+ const usage = ["usage: chopin <command>", "", "commands:", ...commands.flatMap((c) => [` ${c.name} ${c.args}`.trimEnd(), ` ${c.summary}`])];
20
+ (asked ? io.out : io.err)(usage.join("\n"));
21
+ return asked ? 0 : 2;
22
+ }
23
+ function usageError(io, usage) {
24
+ io.err(`usage: chopin ${usage}`);
25
+ return 2;
26
+ }
27
+ async function layerCheck(directory, io) {
28
+ const result = await loadLayer(directory);
29
+ if (!result.ok) {
30
+ for (const problem of result.problems)
31
+ io.err(problem);
32
+ return 1;
33
+ }
34
+ const { instructions, skills, mcpServers, gates } = result.layer;
35
+ io.out([
36
+ `instructions: ${instructions === undefined ? "none" : "AGENTS.md"}`,
37
+ `skills: ${skills.map((s) => s.name).join(", ") || "none"}`,
38
+ `mcp servers: ${Object.keys(mcpServers).join(", ") || "none"}`,
39
+ `gate rules: ${gates.length}`,
40
+ ].join("\n"));
41
+ return 0;
42
+ }
43
+ //# sourceMappingURL=cli.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"cli.js","sourceRoot":"","sources":["../src/cli.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AACpC,OAAO,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAC;AAiB7C,MAAM,CAAC,MAAM,YAAY,GAAuB,CAAC;QAC/C,IAAI,EAAE,aAAa;QACnB,IAAI,EAAE,OAAO;QACb,OAAO,EAAE,oCAAoC;QAC7C,GAAG,EAAE,KAAK,EAAE,IAAI,EAAE,EAAE,EAAE,EAAE;YACtB,IAAI,IAAI,CAAC,MAAM,KAAK,CAAC;gBAAE,OAAO,UAAU,CAAC,EAAE,EAAE,mBAAmB,CAAC,CAAC;YAClE,OAAO,UAAU,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,IAAI,EAAE,CAAC,EAAE,EAAE,CAAC,CAAC;QAChD,CAAC;KACF,CAAC,CAAC;AAEH,sCAAsC;AACtC,MAAM,CAAC,KAAK,UAAU,MAAM,CAAC,IAAuB,EAAE,EAAS,EAAE,QAAQ,GAAuB,YAAY;IAC1G,MAAM,OAAO,GAAG,QAAQ,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC,KAAK,C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CAAC;IAC1C,IAAI,CAAC,MAAM,CAAC,EAAE,EAAE,CAAC;QACf,KAAK,MAAM,OAAO,IAAI,MAAM,CAAC,QAAQ;YAAE,EAAE,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC;QACvD,OAAO,CAAC,CAAC;IACX,CAAC;IACD,MAAM,EAAE,YAAY,EAAE,MAAM,EAAE,UAAU,EAAE,KAAK,EAAE,GAAG,MAAM,CAAC,KAAK,CAAC;IACjE,EAAE,CAAC,GAAG,CAAC;QACL,iBAAiB,YAAY,KAAK,SAAS,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,WAAW,EAAE;QACpE,WAAW,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,MAAM,EAAE;QAC3D,gBAAgB,MAAM,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,MAAM,EAAE;QAC9D,eAAe,KAAK,CAAC,MAAM,EAAE;KAC9B,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC;IACd,OAAO,CAAC,CAAC;AACX,CAAC"}
@@ -0,0 +1,31 @@
1
+ import type { TranscriptEntry } from "../transcript.js";
2
+ import type { Signal } from "./signals.js";
3
+ /** Asks a model for a reply to one prompt; the caller picks the model and pays for it. */
4
+ export type Complete = (prompt: string) => Promise<string>;
5
+ /** Why the failure happened. */
6
+ export declare const CAUSES: readonly ["tool_error", "wrong_command", "skipped_verification", "misread_request", "missing_context", "environment", "user_preference", "gave_up_early", "other"];
7
+ /** What the agent did that led to it. */
8
+ export declare const BEHAVIORS: readonly ["read", "edit", "run_command", "answer", "plan", "ask", "other"];
9
+ /** A failure written as three fields; two failures are the same kind only when all three are equal. */
10
+ export interface FailureSignature {
11
+ cause: (typeof CAUSES)[number];
12
+ behavior: (typeof BEHAVIORS)[number];
13
+ /** A short kebab-case name for the mechanism, such as `stops-at-truncated-output`. */
14
+ mechanism: string;
15
+ }
16
+ /**
17
+ * The rules failures are named by; a failure is counted only with failures named under the same rules, so changing
18
+ * them starts the count afresh instead of mixing two ways of naming.
19
+ */
20
+ export declare const CLASSIFIER_VERSION = 3;
21
+ /** What a model makes of a signal: a named failure, or not a failure at all, such as a search that found nothing. */
22
+ export type Classified = FailureSignature | "not_a_failure";
23
+ /**
24
+ * The signature of the failure `signal` marks in `entries`. Signatures already seen, the most common first, are
25
+ * offered whole, so one kind of failure keeps one name; a reply that does not fit gives `undefined` rather than a
26
+ * guess. A session of another agent, `foreign`, may hold refusals by its host's own rules, which are not the agent's.
27
+ */
28
+ export declare function classify(entries: readonly TranscriptEntry[], signal: Signal, known: readonly FailureSignature[], complete: Complete, foreign?: boolean): Promise<Classified | undefined>;
29
+ export declare function parseSignature(reply: string): Classified | undefined;
30
+ /** The key failures of one kind share. */
31
+ export declare function signatureKey(signature: FailureSignature): string;
@@ -0,0 +1,87 @@
1
+ import { isUserWritten } from "./points.js";
2
+ /** Why the failure happened. */
3
+ export const CAUSES = [
4
+ "tool_error", "wrong_command", "skipped_verification", "misread_request", "missing_context", "environment",
5
+ "user_preference", "gave_up_early", "other",
6
+ ];
7
+ /** What the agent did that led to it. */
8
+ export const BEHAVIORS = ["read", "edit", "run_command", "answer", "plan", "ask", "other"];
9
+ const MECHANISM = /^[a-z0-9]+(-[a-z0-9]+){0,7}$/;
10
+ /**
11
+ * What the model is shown of a session: lines before the failure, and after it up to the user's next message, which
12
+ * tell a failure the agent had to recover from apart from a result it used; each line cut to a length.
13
+ */
14
+ const BEFORE = 20;
15
+ const AFTER = 8;
16
+ const ENTRY_CHARS = 1500;
17
+ /** How many named failures are offered, the most common first, so the prompt does not grow with the catalog. */
18
+ const KNOWN_SHOWN = 30;
19
+ /**
20
+ * The rules failures are named by; a failure is counted only with failures named under the same rules, so changing
21
+ * them starts the count afresh instead of mixing two ways of naming.
22
+ */
23
+ export const CLASSIFIER_VERSION = 3;
24
+ /**
25
+ * The signature of the failure `signal` marks in `entries`. Signatures already seen, the most common first, are
26
+ * offered whole, so one kind of failure keeps one name; a reply that does not fit gives `undefined` rather than a
27
+ * guess. A session of another agent, `foreign`, may hold refusals by its host's own rules, which are not the agent's.
28
+ */
29
+ export async function classify(entries, signal, known, complete, foreign = false) {
30
+ const next = entries.findIndex((entry, line) => line > signal.line && isUserWritten(entry));
31
+ const end = Math.min(next === -1 ? entries.length : next + 1, signal.line + 1 + AFTER);
32
+ const window = entries.slice(Math.max(0, signal.line - BEFORE), end);
33
+ const reply = await complete(prompt(window, signal.line - Math.max(0, signal.line - BEFORE), signal, known.slice(0, KNOWN_SHOWN), foreign));
34
+ return parseSignature(reply);
35
+ }
36
+ export function parseSignature(reply) {
37
+ const json = reply.slice(reply.indexOf("{"), reply.lastIndexOf("}") + 1);
38
+ let value;
39
+ try {
40
+ value = JSON.parse(json);
41
+ }
42
+ catch {
43
+ return undefined;
44
+ }
45
+ if (value.failure === false)
46
+ return "not_a_failure";
47
+ const { cause, behavior, mechanism } = value;
48
+ if (!CAUSES.includes(cause) || !BEHAVIORS.includes(behavior))
49
+ return undefined;
50
+ if (typeof mechanism !== "string" || !MECHANISM.test(mechanism))
51
+ return undefined;
52
+ return { cause, behavior, mechanism };
53
+ }
54
+ /** The key failures of one kind share. */
55
+ export function signatureKey(signature) {
56
+ return `${signature.cause}/${signature.behavior}/${signature.mechanism}`;
57
+ }
58
+ function prompt(window, at, signal, known, foreign) {
59
+ return [
60
+ "An agent's session may have gone wrong at the line marked >>> below. If it did, name the failure with three",
61
+ "fields, as one JSON object.",
62
+ `- cause: one of ${CAUSES.join(", ")}`,
63
+ `- behavior: what the agent did that led to it, one of ${BEHAVIORS.join(", ")}`,
64
+ "- mechanism: a kebab-case name for how it failed, of at most 8 words, general enough to recur in other sessions",
65
+ ...(known.length === 0 ? [] : [
66
+ "When it is the same failure as one already named, reply with that one exactly, all three fields as they are;",
67
+ "name a new one when they only sound alike:",
68
+ ...known.map((signature) => JSON.stringify(signature)),
69
+ ]),
70
+ 'Reply {"failure":false} when it is no failure of the agent\'s own making:',
71
+ "- the agent got what it needed and carried on, as the lines after it show, such as when a search rightly found",
72
+ " nothing or one part of a longer command exited non-zero",
73
+ "- the outside world failed, such as the network or a service, and no other step would have fared better",
74
+ ...(foreign ? [
75
+ "- the agent's host refused it by a rule of its own, such as a blocked command, a permission check or a sandbox",
76
+ " limit; this session comes from another agent, and its host's rules are not this agent's",
77
+ ] : []),
78
+ `The signal: ${JSON.stringify(signal)}`,
79
+ "The session around it, one ACP entry per line:",
80
+ ...window.map((entry, line) => {
81
+ const text = JSON.stringify(entry);
82
+ return `${line === at ? ">>> " : ""}${text.length <= ENTRY_CHARS ? text : `${text.slice(0, ENTRY_CHARS)}…`}`;
83
+ }),
84
+ 'Reply with only the JSON object, such as {"cause":"…","behavior":"…","mechanism":"…"} or {"failure":false}.',
85
+ ].join("\n");
86
+ }
87
+ //# sourceMappingURL=classify.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"classify.js","sourceRoot":"","sources":["../../src/evidence/classify.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAM5C,gCAAgC;AAChC,MAAM,CAAC,MAAM,MAAM,GAAG;IACpB,YAAY,EAAE,eAAe,EAAE,sBAAsB,EAAE,iBAAiB,EAAE,iBAAiB,EAAE,aAAa;IAC1G,iBAAiB,EAAE,eAAe,EAAE,OAAO;CACnC,CAAC;AAEX,yCAAyC;AACzC,MAAM,CAAC,MAAM,SAAS,GAAG,CAAC,MAAM,EAAE,MAAM,EAAE,aAAa,EAAE,QAAQ,EAAE,MAAM,EAAE,KAAK,EAAE,OAAO,CAAU,CAAC;AAUpG,MAAM,SAAS,GAAG,8BAA8B,CAAC;AAEjD;;;GAGG;AACH,MAAM,MAAM,GAAG,EAAE,CAAC;AAClB,MAAM,KAAK,GAAG,CAAC,CAAC;AAChB,MAAM,WAAW,GAAG,IAA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AC,CAAC,SAAS,EAAE,EAAE,CAAC,IAAI,CAAC,SAAS,CAAC,SAAS,CAAC,CAAC;SACvD,CAAC;QACF,2EAA2E;QAC3E,gHAAgH;QAChH,2DAA2D;QAC3D,yGAAyG;QACzG,GAAG,CAAC,OAAO,CAAC,CAAC,CAAC;YACZ,gHAAgH;YAChH,2FAA2F;SAC5F,CAAC,CAAC,CAAC,EAAE,CAAC;QACP,eAAe,IAAI,CAAC,SAAS,CAAC,MAAM,CAAC,EAAE;QACvC,gDAAgD;QAChD,GAAG,MAAM,CAAC,GAAG,CAAC,CAAC,KAAK,EAAE,IAAI,EAAE,EAAE;YAC5B,MAAM,IAAI,GAAG,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC;YACnC,OAAO,GAAG,IAAI,KAAK,EAAE,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,GAAG,IAAI,CAAC,MAAM,IAAI,WAAW,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,GAAG,IAAI,CAAC,KAAK,CAAC,CAAC,EAAE,WAAW,CAAC,GAAG,EAAE,CAAC;QAC/G,CAAC,CAAC;QACF,6GAA6G;KAC9G,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;AACf,CAAC"}
@@ -0,0 +1,14 @@
1
+ import type { TranscriptEntry } from "../transcript.js";
2
+ import type { Complete } from "./classify.js";
3
+ /** A user message that may correct the agent; it counts as a failure only once the user confirms it. */
4
+ export interface CorrectionCandidate {
5
+ /** The transcript line of the user's message. */
6
+ line: number;
7
+ /** What the user corrected, in a few words. */
8
+ summary: string;
9
+ }
10
+ /**
11
+ * Lines from `from` on where the user pushed back on what the agent did. A model proposes them, since no event marks
12
+ * a correction; a long session is asked about in batches, and lines the user did not write are dropped.
13
+ */
14
+ export declare function proposeCorrections(entries: readonly TranscriptEntry[], complete: Complete, from?: number): Promise<CorrectionCandidate[]>;
@@ -0,0 +1,61 @@
1
+ import { correctedStep, isUserWritten } from "./points.js";
2
+ /** How much of each message the model is shown, and how much one request carries, so a long session stays cheap. */
3
+ const USER_CHARS = 1200;
4
+ const AGENT_CHARS = 600;
5
+ const BATCH_CHARS = 60_000;
6
+ /**
7
+ * Lines from `from` on where the user pushed back on what the agent did. A model proposes them, since no event marks
8
+ * a correction; a long session is asked about in batches, and lines the user did not write are dropped.
9
+ */
10
+ export async function proposeCorrections(entries, complete, from = 0) {
11
+ const users = entries.flatMap((entry, line) => (line >= from && isUserWritten(entry) ? [line] : []));
12
+ const found = [];
13
+ for (const batch of batches(users.map((line) => excerpt(entries, line)))) {
14
+ const reply = await complete([
15
+ "Below are the user's messages from an agent session, each with the agent step just before it. Find the user",
16
+ "messages that correct the agent: rejecting what it did, pointing out a mistake, or restating a rule it broke.",
17
+ "Questions and new requests are not corrections, nor are praise, agreement, or frustration that names nothing to",
18
+ "do differently. Summarize each as what the agent should do instead, in a few words. Reply with only a JSON",
19
+ 'array such as [{"line":12,"summary":"use pnpm, not npm"}], or [] for none.',
20
+ ...batch,
21
+ ].join("\n"));
22
+ found.push(...parseCandidates(reply, users));
23
+ }
24
+ return found;
25
+ }
26
+ function excerpt(entries, line) {
27
+ const before = correctedStep(entries, line);
28
+ const agent = before === undefined ? "" : `agent before ${line}: ${clip(JSON.stringify(entries[before]), AGENT_CHARS)}\n`;
29
+ return `${agent}user ${line}: ${clip(JSON.stringify(entries[line]), USER_CHARS)}`;
30
+ }
31
+ function batches(excerpts) {
32
+ const all = [];
33
+ let size = Infinity;
34
+ for (const text of excerpts) {
35
+ if (size + text.length > BATCH_CHARS) {
36
+ all.push([]);
37
+ size = 0;
38
+ }
39
+ all.at(-1).push(text);
40
+ size += text.length;
41
+ }
42
+ return all;
43
+ }
44
+ function parseCandidates(reply, users) {
45
+ let found;
46
+ try {
47
+ found = JSON.parse(reply.slice(reply.indexOf("["), reply.lastIndexOf("]") + 1));
48
+ }
49
+ catch {
50
+ return [];
51
+ }
52
+ if (!Array.isArray(found))
53
+ return [];
54
+ return found.flatMap((item) => typeof item.line === "number" && users.includes(item.line) && typeof item.summary === "string" && item.summary !== ""
55
+ ? [{ line: item.line, summary: item.summary }]
56
+ : []);
57
+ }
58
+ function clip(text, limit) {
59
+ return text.length <= limit ? text : `${text.slice(0, limit)}…`;
60
+ }
61
+ //# sourceMappingURL=corrections.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"corrections.js","sourceRoot":"","sources":["../../src/evidence/corrections.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,aAAa,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAE3D,oHAAoH;AACpH,MAAM,UAAU,GAAG,IAAI,CAAC;AACxB,MAAM,WAAW,GAAG,GAAG,CAAC;AACxB,MAAM,WAAW,GAAG,MAAM,CAAC;AAU3B;;;GAGG;AACH,MAAM,CAAC,KAAK,UAAU,kBAAkB,CAAC,OAAmC,EAAE,QAAkB,EAAE,IAAI,GAAG,CAAC;IACxG,MAAM,KAAK,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC,KAAK,EAAE,IAAI,EAAE,EAAE,CAAC,CAAC,IAAI,IAAI,IAAI,IAAI,aAAa,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC;IACrG,MAAM,KAAK,GAA0B,EAAE,CAAC;IACxC,KAAK,MAAM,KAAK,IAAI,OAAO,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,OAAO,CAAC,OAAO,EAAE,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC;QACzE,MAAM,KAAK,GAAG,MAAM,QAAQ,CAAC;YAC3B,6GAA6G;YAC7G,+GAA+G;YAC/G,iHAAiH;YACjH,4GAA4G;YAC5G,4EAA4E;YAC5E,GAAG,KAAK;SACT,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC;QACd,KAAK,CAAC,IAAI,CAAC,GAAG,eAAe,CAAC,KAAK,EAA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C,GAAG,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC;IAClF,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,EAAE,CAAC;IACZ,CAAC;IACD,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC;QAAE,OAAO,EAAE,CAAC;IACrC,OAAO,KAAK,CAAC,OAAO,CAAC,CAAC,IAA2C,EAAE,EAAE,CACnE,OAAO,IAAI,CAAC,IAAI,KAAK,QAAQ,IAAI,KAAK,CAAC,QAAQ,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,OAAO,IAAI,CAAC,OAAO,KAAK,QAAQ,IAAI,IAAI,CAAC,OAAO,KAAK,EAAE;QACnH,CAAC,CAAC,CAAC,EAAE,IAAI,EAAE,IAAI,CAAC,IAAI,EAAE,OAAO,EAAE,IAAI,CAAC,OAAO,EAAE,CAAC;QAC9C,CAAC,CAAC,EAAE,CAAC,CAAC;AACZ,CAAC;AAED,SAAS,IAAI,CAAC,IAAY,EAAE,KAAa;IACvC,OAAO,IAAI,CAAC,MAAM,IAAI,KAAK,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,GAAG,IAAI,CAAC,KAAK,CAAC,CAAC,EAAE,KAAK,CAAC,GAAG,CAAC;AAClE,CAAC"}
@@ -0,0 +1,44 @@
1
+ import { type Complete, type FailureSignature } from "./classify.js";
2
+ /** One failure in one session, with its signature. */
3
+ export interface Failure {
4
+ session: string;
5
+ /** The transcript line where it happened. */
6
+ line: number;
7
+ signature: FailureSignature;
8
+ /** The version of the rules it was named by. */
9
+ classifier?: number;
10
+ }
11
+ /** Failures of one kind, and the distinct sessions they came from. */
12
+ export interface FailureClass {
13
+ key: string;
14
+ /** The signature most of its failures were named with. */
15
+ signature: FailureSignature;
16
+ failures: Failure[];
17
+ sessions: string[];
18
+ }
19
+ /** How many distinct sessions a kind of failure must appear in before it starts a round. */
20
+ export declare const TRIGGER_SESSIONS = 2;
21
+ /**
22
+ * The key failures of one kind share: what the agent did and how it failed. The cause is a judgement that differs
23
+ * between sessions for the same mechanism, so it describes a kind without splitting it.
24
+ */
25
+ export declare function kindKey(signature: FailureSignature): string;
26
+ /**
27
+ * Failures grouped by kind, the kinds seen in the most sessions first. `aliases` maps a kind's key to the key of the
28
+ * kind it is another name for, whose name the merged kind carries.
29
+ */
30
+ export declare function groupFailures(failures: readonly Failure[], aliases?: ReadonlyMap<string, string>): FailureClass[];
31
+ /** The kinds looked at for other names before, and the aliases found among them. */
32
+ export interface KindNaming {
33
+ reviewed: readonly string[];
34
+ aliases: ReadonlyMap<string, string>;
35
+ }
36
+ /**
37
+ * Which kinds are other names for one mechanism. Each behavior with a kind not looked at before is asked about once,
38
+ * with every name given within it; a new kind joins the kind of an earlier name it is grouped with, or else the first
39
+ * new name of its group, while kinds looked at before keep the names they were given, so grouping only ever settles.
40
+ * A reply naming a kind that is not there, or one kind twice, is not taken.
41
+ */
42
+ export declare function kindAliases(kinds: readonly string[], complete: Complete, before?: KindNaming): Promise<Map<string, string>>;
43
+ /** The kinds of failure that have recurred often enough to start a round. */
44
+ export declare function triggered(classes: readonly FailureClass[], sessions?: number): FailureClass[];
@@ -0,0 +1,91 @@
1
+ import { signatureKey } from "./classify.js";
2
+ /** How many distinct sessions a kind of failure must appear in before it starts a round. */
3
+ export const TRIGGER_SESSIONS = 2;
4
+ /**
5
+ * The key failures of one kind share: what the agent did and how it failed. The cause is a judgement that differs
6
+ * between sessions for the same mechanism, so it describes a kind without splitting it.
7
+ */
8
+ export function kindKey(signature) {
9
+ return `${signature.behavior}/${signature.mechanism}`;
10
+ }
11
+ /**
12
+ * Failures grouped by kind, the kinds seen in the most sessions first. `aliases` maps a kind's key to the key of the
13
+ * kind it is another name for, whose name the merged kind carries.
14
+ */
15
+ export function groupFailures(failures, aliases = new Map()) {
16
+ const classes = new Map();
17
+ for (const failure of failures) {
18
+ const own = kindKey(failure.signature);
19
+ const key = aliases.get(own) ?? own;
20
+ const found = classes.get(key) ?? { key, signature: failure.signature, failures: [], sessions: [] };
21
+ found.failures.push(failure);
22
+ if (!found.sessions.includes(failure.session))
23
+ found.sessions.push(failure.session);
24
+ classes.set(key, found);
25
+ }
26
+ for (const found of classes.values()) {
27
+ const signature = mostCommon(found.failures.map((failure) => failure.signature));
28
+ found.signature = { ...signature, mechanism: found.key.slice(found.key.indexOf("/") + 1) };
29
+ }
30
+ return [...classes.values()].sort((a, b) => b.sessions.length - a.sessions.length);
31
+ }
32
+ /**
33
+ * Which kinds are other names for one mechanism. Each behavior with a kind not looked at before is asked about once,
34
+ * with every name given within it; a new kind joins the kind of an earlier name it is grouped with, or else the first
35
+ * new name of its group, while kinds looked at before keep the names they were given, so grouping only ever settles.
36
+ * A reply naming a kind that is not there, or one kind twice, is not taken.
37
+ */
38
+ export async function kindAliases(kinds, complete, before = { reviewed: [], aliases: new Map() }) {
39
+ const aliases = new Map(before.aliases);
40
+ const reviewed = new Set(before.reviewed);
41
+ for (const [behavior, keys] of Map.groupBy(kinds, (key) => key.slice(0, key.indexOf("/")))) {
42
+ if (keys.length < 2 || keys.every((key) => reviewed.has(key)))
43
+ continue;
44
+ const names = keys.map((key) => key.slice(behavior.length + 1));
45
+ const reply = await complete([
46
+ `Below are names an agent's failures were given, all from the behavior "${behavior}". Group the names that name`,
47
+ "the same mechanism in other words, so a fix for one is a fix for all; names that are only related stay apart.",
48
+ 'Reply with only a JSON array of groups, each with the name to keep first, such as [["a-b","a-b-c"]], or [] for',
49
+ "none.",
50
+ ...names,
51
+ ].join("\n"));
52
+ const seen = new Set();
53
+ for (const group of parseGroups(reply)) {
54
+ if (group.length < 2 || group.some((name) => !names.includes(name) || seen.has(name)))
55
+ continue;
56
+ for (const name of group)
57
+ seen.add(name);
58
+ const members = group.map((name) => `${behavior}/${name}`);
59
+ const anchor = members.find((key) => reviewed.has(key)) ?? members[0];
60
+ const target = aliases.get(anchor) ?? anchor;
61
+ for (const key of members)
62
+ if (!reviewed.has(key) && key !== target)
63
+ aliases.set(key, target);
64
+ }
65
+ }
66
+ return aliases;
67
+ }
68
+ function parseGroups(reply) {
69
+ try {
70
+ const value = JSON.parse(reply.slice(reply.indexOf("["), reply.lastIndexOf("]") + 1));
71
+ if (!Array.isArray(value))
72
+ return [];
73
+ return value.filter((group) => Array.isArray(group) && group.every((name) => typeof name === "string"));
74
+ }
75
+ catch {
76
+ return [];
77
+ }
78
+ }
79
+ function mostCommon(signatures) {
80
+ const counts = new Map();
81
+ for (const signature of signatures) {
82
+ const key = signatureKey(signature);
83
+ counts.set(key, { signature, count: (counts.get(key)?.count ?? 0) + 1 });
84
+ }
85
+ return [...counts.values()].reduce((best, next) => (next.count > best.count ? next : best)).signature;
86
+ }
87
+ /** The kinds of failure that have recurred often enough to start a round. */
88
+ export function triggered(classes, sessions = TRIGGER_SESSIONS) {
89
+ return classes.filter((found) => found.sessions.length >= sessions);
90
+ }
91
+ //# sourceMappingURL=failures.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"failures.js","sourceRoot":"","sources":["../../src/evidence/failures.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAwC,MAAM,eAAe,CAAC;AAqBnF,4FAA4F;AAC5F,MAAM,CAAC,MAAM,gBAAgB,GAAG,CAAC,CAAC;AAElC;;;GAGG;AACH,MAAM,UAAU,OAAO,CAAC,SAA2B;IACjD,OAAO,GAAG,SAAS,CAAC,QAAQ,IAAI,SAAS,CAAC,SAAS,EAAE,CAAC;AACxD,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,aAAa,CAAC,QAA4B,EAAE,OAAO,GAAgC,IAAI,GAAG,EAAE;IAC1G,MAAM,OAAO,GAAG,IAAI,GAAG,EAAwB,CAAC;IAChD,KAAK,MAAM,OAAO,IAAI,QAAQ,EAAE,CAAC;QAC/B,MAAM,GAAG,GAAG,OAAO,CAAC,OAAO,CAAC,SAAS,CAAC,CAAC;QACvC,MAAM,GAAG,GAAG,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC,IAAI,GAAG,CAAC;QACp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U,CAAC,UAAuC;IACzD,MAAM,MAAM,GAAG,IAAI,GAAG,EAA0D,CAAC;IACjF,KAAK,MAAM,SAAS,IAAI,UAAU,EAAE,CAAC;QACnC,MAAM,GAAG,GAAG,YAAY,CAAC,SAAS,CAAC,CAAC;QACpC,MAAM,CAAC,GAAG,CAAC,GAAG,EAAE,EAAE,SAAS,EAAE,KAAK,EAAE,CAAC,MAAM,CAAC,GAAG,CAAC,GAAG,CAAC,EAAE,KAAK,IAAI,CAAC,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC;IAC3E,CAAC;IACD,OAAO,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,CAAC,CAAC,MAAM,CAAC,CAAC,IAAI,EAAE,IAAI,EAAE,EAAE,CAAC,CAAC,IAAI,CAAC,KAAK,GAAG,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,SAAS,CAAC;AACxG,CAAC;AAED,6EAA6E;AAC7E,MAAM,UAAU,SAAS,CAAC,OAAgC,EAAE,QAAQ,GAAG,gBAAgB;IACrF,OAAO,OAAO,CAAC,MAAM,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,KAAK,CAAC,QAAQ,CAAC,MAAM,IAAI,QAAQ,CAAC,CAAC;AACtE,CAAC"}
@@ -0,0 +1,7 @@
1
+ import type { TranscriptEntry } from "../transcript.js";
2
+ /**
3
+ * A transcript from session logs written one JSON object per line, each a user or assistant message whose content is
4
+ * text, thinking, tool-use and tool-result blocks. Side threads and bookkeeping lines are left out; user-role text the
5
+ * user did not write stays in, since the model saw it, marked as injected.
6
+ */
7
+ export declare function fromMessagesJsonl(text: string): TranscriptEntry[];