@code-yeongyu/senpi-codemode 2026.11.26 → 2026.11.27

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 (52) hide show
  1. package/CHANGELOG.md +19 -0
  2. package/README.md +87 -0
  3. package/package.json +5 -5
  4. package/src/bridge/reserved.ts +2 -0
  5. package/src/config/feature-settings.ts +19 -0
  6. package/src/config/settings.ts +8 -0
  7. package/src/extension/eval-status-ticker.ts +5 -2
  8. package/src/extension/runtime-factory.ts +31 -5
  9. package/src/extension/session-manager.ts +4 -1
  10. package/src/index.ts +14 -6
  11. package/src/kernels/js/babel-ast.ts +64 -0
  12. package/src/kernels/js/binding-patterns.js +166 -0
  13. package/src/kernels/js/cell-run-context.js +8 -0
  14. package/src/kernels/js/declaration-extent.js +157 -0
  15. package/src/kernels/js/erase-types.ts +174 -0
  16. package/src/kernels/js/js-source-scan.js +181 -0
  17. package/src/kernels/js/kernel-contract.ts +2 -0
  18. package/src/kernels/js/last-expression.js +207 -0
  19. package/src/kernels/js/local-module-loader.ts +7 -2
  20. package/src/kernels/js/persist-declarations.js +233 -0
  21. package/src/kernels/js/rewrite-imports.ts +3 -60
  22. package/src/kernels/js/typescript-cell.ts +39 -0
  23. package/src/kernels/js/worker-core.js +1 -0
  24. package/src/kernels/js/worker-indirect-eval.js +4 -860
  25. package/src/kernels/js/worker-runtime.js +2 -0
  26. package/src/kernels/js/worker-scope.js +71 -0
  27. package/src/kernels/js/worker-startup.ts +1 -0
  28. package/src/prompt/eval-prompt-template.ts +4 -2
  29. package/src/prompt/eval-prompt.ts +4 -0
  30. package/src/skill/bun/SKILL.md +1 -0
  31. package/src/tool/cell-handler.ts +17 -1
  32. package/src/tool/detached-cell-manager.ts +42 -4
  33. package/src/tool/detached-notification-queue.ts +48 -7
  34. package/src/tool/detached-settling.ts +26 -0
  35. package/src/tool/eval-tool-options.ts +5 -1
  36. package/src/tool/eval-tool.ts +3 -1
  37. package/src/tool/js-cell-parse-message.ts +11 -3
  38. package/src/tool/managed-cell.ts +3 -0
  39. package/src/tool/run-eval-cell.ts +36 -11
  40. package/src/tool/scoped-watches.ts +53 -0
  41. package/src/tool/types.ts +14 -0
  42. package/src/written-files/model-writes.ts +55 -0
  43. package/src/written-files/ownership.ts +73 -0
  44. package/src/written-files/report.ts +63 -0
  45. package/src/written-files/scan-boundaries.ts +164 -0
  46. package/src/written-files/source-file.ts +31 -0
  47. package/src/written-files/tracker.ts +361 -0
  48. package/src/written-files/window.ts +92 -0
  49. package/src/written-files/workspace-fs.ts +104 -0
  50. package/src/written-files/workspace-scan.ts +253 -0
  51. package/src/written-files/write-hooks.ts +155 -0
  52. package/src/written-files/written-files.ts +155 -0
@@ -0,0 +1,166 @@
1
+ import { IDENTIFIER_RE, skipBlockComment, skipLineComment, skipQuotedLiteral, skipTemplateLiteral } from "./js-source-scan.js";
2
+
3
+ // Text helpers for binding patterns (`{ a, b: [c] = d }`) in the declaration persistence rewrite.
4
+
5
+ export function collectPatternNames(source, bindings) {
6
+ const pattern = trimPattern(source);
7
+ if (!pattern) return;
8
+ const defaultStart = findTopLevelEquals(pattern);
9
+ if (defaultStart >= 0) {
10
+ collectPatternNames(pattern.slice(0, defaultStart), bindings);
11
+ return;
12
+ }
13
+ if (pattern.startsWith("{") && pattern.endsWith("}")) {
14
+ for (const property of splitPatternElements(pattern.slice(1, -1))) {
15
+ const trimmed = trimPattern(property);
16
+ if (!trimmed) continue;
17
+ if (trimmed.startsWith("...")) {
18
+ collectPatternNames(trimmed.slice(3), bindings);
19
+ continue;
20
+ }
21
+ const equals = findTopLevelEquals(trimmed);
22
+ const colon = findTopLevelColon(trimmed);
23
+ if (colon >= 0 && (equals < 0 || colon < equals)) collectPatternNames(trimmed.slice(colon + 1), bindings);
24
+ else collectPatternNames(equals < 0 ? trimmed : trimmed.slice(0, equals), bindings);
25
+ }
26
+ return;
27
+ }
28
+ if (pattern.startsWith("[") && pattern.endsWith("]")) {
29
+ for (const element of splitPatternElements(pattern.slice(1, -1))) {
30
+ const trimmed = trimPattern(element);
31
+ if (!trimmed) continue;
32
+ collectPatternNames(trimmed.startsWith("...") ? trimmed.slice(3) : trimmed, bindings);
33
+ }
34
+ return;
35
+ }
36
+ const equals = findTopLevelEquals(pattern);
37
+ const name = trimPattern(equals < 0 ? pattern : pattern.slice(0, equals));
38
+ if (IDENTIFIER_RE.test(name)) bindings.push(name);
39
+ }
40
+
41
+ export function splitPatternElements(source) {
42
+ const elements = [];
43
+ let start = 0;
44
+ let round = 0;
45
+ let square = 0;
46
+ let curly = 0;
47
+ for (let index = 0; index < source.length; index += 1) {
48
+ const char = source[index];
49
+ const next = source[index + 1];
50
+ if (char === "/" && next === "/") {
51
+ index = skipLineComment(source, index) - 1;
52
+ continue;
53
+ }
54
+ if (char === "/" && next === "*") {
55
+ index = skipBlockComment(source, index) - 1;
56
+ continue;
57
+ }
58
+ if (char === "'" || char === '"') {
59
+ index = skipQuotedLiteral(source, index) - 1;
60
+ continue;
61
+ }
62
+ if (char === "`") {
63
+ index = skipTemplateLiteral(source, index) - 1;
64
+ continue;
65
+ }
66
+ if (char === "(") round += 1;
67
+ else if (char === ")") round = Math.max(0, round - 1);
68
+ else if (char === "[") square += 1;
69
+ else if (char === "]") square = Math.max(0, square - 1);
70
+ else if (char === "{") curly += 1;
71
+ else if (char === "}") curly = Math.max(0, curly - 1);
72
+ else if (char === "," && round === 0 && square === 0 && curly === 0) {
73
+ elements.push(source.slice(start, index));
74
+ start = index + 1;
75
+ }
76
+ }
77
+ elements.push(source.slice(start));
78
+ return elements;
79
+ }
80
+
81
+ export function findTopLevelEquals(source) {
82
+ return findTopLevelCharacter(source, "=");
83
+ }
84
+
85
+ export function findTopLevelColon(source) {
86
+ return findTopLevelCharacter(source, ":");
87
+ }
88
+
89
+ function findTopLevelCharacter(source, target) {
90
+ let round = 0;
91
+ let square = 0;
92
+ let curly = 0;
93
+ for (let index = 0; index < source.length; index += 1) {
94
+ const char = source[index];
95
+ const next = source[index + 1];
96
+ if (char === "/" && next === "/") {
97
+ index = skipLineComment(source, index) - 1;
98
+ continue;
99
+ }
100
+ if (char === "/" && next === "*") {
101
+ index = skipBlockComment(source, index) - 1;
102
+ continue;
103
+ }
104
+ if (char === "'" || char === '"') {
105
+ index = skipQuotedLiteral(source, index) - 1;
106
+ continue;
107
+ }
108
+ if (char === "`") {
109
+ index = skipTemplateLiteral(source, index) - 1;
110
+ continue;
111
+ }
112
+ if (char === "(") round += 1;
113
+ else if (char === ")") round = Math.max(0, round - 1);
114
+ else if (char === "[") square += 1;
115
+ else if (char === "]") square = Math.max(0, square - 1);
116
+ else if (char === "{") curly += 1;
117
+ else if (char === "}") curly = Math.max(0, curly - 1);
118
+ else if (char === target && round === 0 && square === 0 && curly === 0) return index;
119
+ }
120
+ return -1;
121
+ }
122
+
123
+ export function trimPattern(source) {
124
+ let start = 0;
125
+ while (start < source.length) {
126
+ if (/\s/u.test(source[start])) {
127
+ start += 1;
128
+ continue;
129
+ }
130
+ if (source.startsWith("//", start)) {
131
+ start = skipLineComment(source, start);
132
+ continue;
133
+ }
134
+ if (source.startsWith("/*", start)) {
135
+ start = skipBlockComment(source, start);
136
+ continue;
137
+ }
138
+ break;
139
+ }
140
+ let end = start;
141
+ for (let index = start; index < source.length; index += 1) {
142
+ const char = source[index];
143
+ const next = source[index + 1];
144
+ if (/\s/u.test(char)) continue;
145
+ if (char === "/" && next === "/") {
146
+ index = skipLineComment(source, index) - 1;
147
+ continue;
148
+ }
149
+ if (char === "/" && next === "*") {
150
+ index = skipBlockComment(source, index) - 1;
151
+ continue;
152
+ }
153
+ if (char === "'" || char === '"') {
154
+ index = skipQuotedLiteral(source, index) - 1;
155
+ end = index + 1;
156
+ continue;
157
+ }
158
+ if (char === "`") {
159
+ index = skipTemplateLiteral(source, index) - 1;
160
+ end = index + 1;
161
+ continue;
162
+ }
163
+ end = index + 1;
164
+ }
165
+ return source.slice(start, end);
166
+ }
@@ -11,6 +11,14 @@ export function runInCell(cell, run) {
11
11
  return cellRuns.run(cell, run);
12
12
  }
13
13
 
14
+ /**
15
+ * Runs `run` with no cell in its async context, so the released-cell guards do not refuse it. Only teardown of what a
16
+ * cell itself acquired belongs here: cleaning up must still work after the cell was stopped.
17
+ */
18
+ export function runOutsideCell(run) {
19
+ return cellRuns.exit(run);
20
+ }
21
+
14
22
  export function currentCell() {
15
23
  return cellRuns.getStore();
16
24
  }
@@ -0,0 +1,157 @@
1
+ import { EXPRESSION_PREFIX_KEYWORDS, REGEX_PREFIX_KEYWORDS, isDecimalDigit, isDeclarationContinuation, isExpressionOperator, isIdentifierStart, isLineTerminator, nextSignificantIndex, readIdentifier, skipBlockComment, skipLineComment, skipNumberLiteral, skipQuotedLiteral, skipRegexLiteral, skipTemplateLiteral } from "./js-source-scan.js";
2
+
3
+ // Where a top-level declaration ends, and where its declarators split.
4
+
5
+ export function findDeclarationEnd(code, start) {
6
+ let round = 0;
7
+ let square = 0;
8
+ let curly = 0;
9
+ let canEnd = false;
10
+ let canStartRegex = true;
11
+
12
+ for (let index = start; index < code.length; index += 1) {
13
+ const char = code[index];
14
+ const next = code[index + 1];
15
+ if (char === "/" && next === "/") {
16
+ index = skipLineComment(code, index) - 1;
17
+ continue;
18
+ }
19
+ if (char === "/" && next === "*") {
20
+ index = skipBlockComment(code, index) - 1;
21
+ continue;
22
+ }
23
+ if (char === "'" || char === '"') {
24
+ index = skipQuotedLiteral(code, index) - 1;
25
+ canEnd = true;
26
+ canStartRegex = false;
27
+ continue;
28
+ }
29
+ if (char === "`") {
30
+ index = skipTemplateLiteral(code, index) - 1;
31
+ canEnd = true;
32
+ canStartRegex = false;
33
+ continue;
34
+ }
35
+ if (char === "/" && canStartRegex) {
36
+ index = skipRegexLiteral(code, index) - 1;
37
+ canEnd = true;
38
+ canStartRegex = false;
39
+ continue;
40
+ }
41
+ if (isIdentifierStart(char)) {
42
+ const end = readIdentifier(code, index);
43
+ const token = code.slice(index, end);
44
+ canEnd = !EXPRESSION_PREFIX_KEYWORDS.has(token);
45
+ canStartRegex = REGEX_PREFIX_KEYWORDS.has(token);
46
+ index = end - 1;
47
+ continue;
48
+ }
49
+ if (isDecimalDigit(char)) {
50
+ index = skipNumberLiteral(code, index) - 1;
51
+ canEnd = true;
52
+ canStartRegex = false;
53
+ continue;
54
+ }
55
+ if (char === "(") {
56
+ round += 1;
57
+ canEnd = false;
58
+ canStartRegex = true;
59
+ continue;
60
+ }
61
+ if (char === ")") {
62
+ round = Math.max(0, round - 1);
63
+ canEnd = true;
64
+ canStartRegex = false;
65
+ continue;
66
+ }
67
+ if (char === "[") {
68
+ square += 1;
69
+ canEnd = false;
70
+ canStartRegex = true;
71
+ continue;
72
+ }
73
+ if (char === "]") {
74
+ square = Math.max(0, square - 1);
75
+ canEnd = true;
76
+ canStartRegex = false;
77
+ continue;
78
+ }
79
+ if (char === "{") {
80
+ curly += 1;
81
+ canEnd = false;
82
+ canStartRegex = true;
83
+ continue;
84
+ }
85
+ if (char === "}") {
86
+ curly = Math.max(0, curly - 1);
87
+ canEnd = true;
88
+ canStartRegex = false;
89
+ continue;
90
+ }
91
+ if (char === ";" && round === 0 && square === 0 && curly === 0) return index + 1;
92
+ if (char === "," && round === 0 && square === 0 && curly === 0) {
93
+ canEnd = false;
94
+ canStartRegex = true;
95
+ continue;
96
+ }
97
+ if (isLineTerminator(char) && round === 0 && square === 0 && curly === 0 && canEnd) {
98
+ const nextIndex = nextSignificantIndex(code, index + 1);
99
+ if (nextIndex >= code.length || !isDeclarationContinuation(code[nextIndex])) return index;
100
+ }
101
+ if (!/\s/u.test(char)) {
102
+ canEnd = false;
103
+ canStartRegex = isExpressionOperator(char);
104
+ }
105
+ }
106
+ return code.length;
107
+ }
108
+
109
+ export function splitDeclarators(code, start, end) {
110
+ const ranges = [];
111
+ let segmentStart = start;
112
+ let round = 0;
113
+ let square = 0;
114
+ let curly = 0;
115
+ let canStartRegex = true;
116
+ for (let index = start; index < end; index += 1) {
117
+ const char = code[index];
118
+ const next = code[index + 1];
119
+ if (char === "/" && next === "/") {
120
+ index = Math.min(end, skipLineComment(code, index)) - 1;
121
+ continue;
122
+ }
123
+ if (char === "/" && next === "*") {
124
+ index = Math.min(end, skipBlockComment(code, index)) - 1;
125
+ continue;
126
+ }
127
+ if (char === "'" || char === '"') {
128
+ index = Math.min(end, skipQuotedLiteral(code, index)) - 1;
129
+ canStartRegex = false;
130
+ continue;
131
+ }
132
+ if (char === "`") {
133
+ index = Math.min(end, skipTemplateLiteral(code, index)) - 1;
134
+ canStartRegex = false;
135
+ continue;
136
+ }
137
+ if (char === "/" && canStartRegex) {
138
+ index = Math.min(end, skipRegexLiteral(code, index)) - 1;
139
+ canStartRegex = false;
140
+ continue;
141
+ }
142
+ if (char === "(") round += 1;
143
+ else if (char === ")") round = Math.max(0, round - 1);
144
+ else if (char === "[") square += 1;
145
+ else if (char === "]") square = Math.max(0, square - 1);
146
+ else if (char === "{") curly += 1;
147
+ else if (char === "}") curly = Math.max(0, curly - 1);
148
+ else if (char === "," && round === 0 && square === 0 && curly === 0) {
149
+ ranges.push([segmentStart, index]);
150
+ segmentStart = index + 1;
151
+ }
152
+ if (!/\s/u.test(char)) canStartRegex = isExpressionOperator(char) || char === "(" || char === "[" || char === "{";
153
+ }
154
+ if (segmentStart < end && code.slice(segmentStart, end).trim() !== ";") ranges.push([segmentStart, end - (code[end - 1] === ";" ? 1 : 0)]);
155
+ else if (ranges.length > 0 && code.slice(segmentStart, end).trim() !== ";") ranges.push([end, end]);
156
+ return ranges;
157
+ }
@@ -0,0 +1,174 @@
1
+ import { type AstNode, nodeFrom, parseCellProgram, walkNodes } from "./babel-ast.ts";
2
+
3
+ /**
4
+ * The outcome of erasing TypeScript syntax from a cell. `erased` code has every type-only token replaced by spaces
5
+ * (line breaks kept), so each remaining token sits at its original line and column and diagnostics point at the
6
+ * submitted text. `unsupported` names a construct that generates runtime code (enum, namespace, parameter property,
7
+ * `import =`, `export =`) and so cannot be erased.
8
+ */
9
+ export type TypeErasure =
10
+ | { readonly kind: "erased"; readonly code: string }
11
+ | { readonly kind: "unsupported"; readonly construct: string; readonly line: number; readonly column: number }
12
+ | { readonly kind: "invalid" };
13
+
14
+ type Range = { readonly start: number; readonly end: number; readonly statement: boolean };
15
+
16
+ const TYPE_ONLY_STATEMENTS = new Set(["TSTypeAliasDeclaration", "TSInterfaceDeclaration", "TSDeclareFunction"]);
17
+ const DECLARABLE = new Set([
18
+ "VariableDeclaration",
19
+ "ClassDeclaration",
20
+ "TSEnumDeclaration",
21
+ "TSModuleDeclaration",
22
+ "FunctionDeclaration",
23
+ ]);
24
+ const TYPE_SYNTAX = new Set(["TSTypeAnnotation", "TSTypeParameterDeclaration", "TSTypeParameterInstantiation"]);
25
+ const TYPE_SUFFIX_EXPRESSIONS = new Set(["TSAsExpression", "TSSatisfiesExpression", "TSNonNullExpression"]);
26
+ const UNSUPPORTED: Readonly<Record<string, string>> = {
27
+ TSEnumDeclaration: "enum",
28
+ TSModuleDeclaration: "namespace",
29
+ TSParameterProperty: "constructor parameter property",
30
+ TSImportEqualsDeclaration: "import = require()",
31
+ TSExportAssignment: "export =",
32
+ };
33
+ const MEMBER_MODIFIER_RE = /\b(?:public|private|protected|readonly|override)\b/gu;
34
+
35
+ export function eraseTypeScript(code: string): TypeErasure {
36
+ const ast = parseCellProgram(code, false);
37
+ if (ast === undefined) return { kind: "invalid" };
38
+ const ranges: Range[] = [];
39
+ let unsupported: { construct: string; at: AstNode } | undefined;
40
+ const blank = (start: number, end: number, statement = false) => {
41
+ if (end > start) ranges.push({ start, end, statement });
42
+ };
43
+ walkNodes(ast.program, (node) => {
44
+ if (unsupported !== undefined) return;
45
+ const value = node.value;
46
+ if (value.declare === true && DECLARABLE.has(node.type)) return blank(node.start, node.end, true);
47
+ const construct = UNSUPPORTED[node.type];
48
+ if (construct !== undefined && !(node.type === "TSImportEqualsDeclaration" && value.importKind === "type")) {
49
+ unsupported = { construct, at: node };
50
+ return;
51
+ }
52
+ if (TYPE_ONLY_STATEMENTS.has(node.type) || node.type === "TSImportEqualsDeclaration") {
53
+ return blank(node.start, node.end, true);
54
+ }
55
+ if (isTypeOnlyModuleStatement(node)) return blank(node.start, node.end, true);
56
+ if (node.type === "ImportSpecifier" && value.importKind === "type")
57
+ return blank(node.start, commaEnd(code, node.end));
58
+ if (TYPE_SYNTAX.has(node.type)) return blank(node.start, node.end);
59
+ if (TYPE_SUFFIX_EXPRESSIONS.has(node.type)) return blank(nodeFrom(value.expression)?.end ?? node.end, node.end);
60
+ if (node.type === "TSTypeAssertion") return blank(node.start, nodeFrom(value.expression)?.start ?? node.start);
61
+ eraseMarkers(code, node, blank);
62
+ });
63
+ if (unsupported !== undefined) {
64
+ const position = lineColumn(code, unsupported.at.start);
65
+ return { kind: "unsupported", construct: unsupported.construct, ...position };
66
+ }
67
+ return { kind: "erased", code: applyBlanks(code, ranges) };
68
+ }
69
+
70
+ function isTypeOnlyModuleStatement(node: AstNode): boolean {
71
+ if (
72
+ node.type === "ImportDeclaration" ||
73
+ node.type === "ExportNamedDeclaration" ||
74
+ node.type === "ExportAllDeclaration"
75
+ ) {
76
+ return node.value.importKind === "type" || node.value.exportKind === "type";
77
+ }
78
+ return false;
79
+ }
80
+
81
+ function eraseMarkers(
82
+ code: string,
83
+ node: AstNode,
84
+ blank: (start: number, end: number, statement?: boolean) => void,
85
+ ): void {
86
+ const value = node.value;
87
+ if (node.type === "Identifier" && typeof value.name === "string") {
88
+ const nameEnd = node.start + value.name.length;
89
+ if (value.optional === true) blank(...charRange(code, "?", nameEnd, node.end));
90
+ return;
91
+ }
92
+ if (node.type === "VariableDeclarator" && value.definite === true) {
93
+ const id = nodeFrom(value.id);
94
+ if (id !== undefined && typeof id.value.name === "string")
95
+ blank(...charRange(code, "!", id.start + id.value.name.length, id.end));
96
+ return;
97
+ }
98
+ if (isFunctionNode(node)) eraseThisParameter(code, value.params, blank);
99
+ if (node.type === "ClassDeclaration" || node.type === "ClassExpression") return eraseClassHeader(code, node, blank);
100
+ if (!isClassMember(node.type)) return;
101
+ if (
102
+ value.declare === true ||
103
+ value.abstract === true ||
104
+ node.type === "TSDeclareMethod" ||
105
+ node.type === "TSIndexSignature"
106
+ ) {
107
+ return blank(node.start, node.end, true);
108
+ }
109
+ const key = nodeFrom(value.key);
110
+ if (key === undefined) return;
111
+ for (const match of code.slice(node.start, key.start).matchAll(MEMBER_MODIFIER_RE)) {
112
+ const at = node.start + match.index;
113
+ blank(at, at + match[0].length);
114
+ }
115
+ const markerEnd = nodeFrom(value.typeAnnotation)?.start ?? nodeFrom(value.returnType)?.start ?? node.end;
116
+ if (value.optional === true) blank(...charRange(code, "?", key.end, markerEnd));
117
+ if (value.definite === true) blank(...charRange(code, "!", key.end, markerEnd));
118
+ }
119
+
120
+ function eraseClassHeader(code: string, node: AstNode, blank: (start: number, end: number) => void): void {
121
+ if (node.value.abstract === true && code.startsWith("abstract", node.start))
122
+ blank(node.start, node.start + "abstract".length);
123
+ const clauses = node.value.implements;
124
+ if (!Array.isArray(clauses) || clauses.length === 0) return;
125
+ const first = nodeFrom(clauses[0]);
126
+ const last = nodeFrom(clauses.at(-1));
127
+ if (first === undefined || last === undefined) return;
128
+ blank(code.lastIndexOf("implements", first.start), last.end);
129
+ }
130
+
131
+ function eraseThisParameter(code: string, params: unknown, blank: (start: number, end: number) => void): void {
132
+ if (!Array.isArray(params)) return;
133
+ const first = nodeFrom(params[0]);
134
+ if (first?.type === "Identifier" && first.value.name === "this") blank(first.start, commaEnd(code, first.end));
135
+ }
136
+
137
+ function isFunctionNode(node: AstNode): boolean {
138
+ return /^(?:FunctionDeclaration|FunctionExpression|ArrowFunctionExpression|ClassMethod|ObjectMethod|TSDeclareMethod)$/u.test(
139
+ node.type,
140
+ );
141
+ }
142
+
143
+ function isClassMember(type: string): boolean {
144
+ return /^(?:ClassProperty|ClassAccessorProperty|ClassMethod|ClassPrivateProperty|ClassPrivateMethod|TSDeclareMethod|TSIndexSignature)$/u.test(
145
+ type,
146
+ );
147
+ }
148
+
149
+ function charRange(code: string, char: string, from: number, to: number): [number, number] {
150
+ const at = code.indexOf(char, from);
151
+ return at >= 0 && at < to ? [at, at + 1] : [from, from];
152
+ }
153
+
154
+ function commaEnd(code: string, end: number): number {
155
+ const match = /^\s*,/u.exec(code.slice(end));
156
+ return match === null ? end : end + match[0].length;
157
+ }
158
+
159
+ /** Blanks every range with spaces, keeping line breaks; a whole statement starts with `;` so ASI cannot merge lines. */
160
+ function applyBlanks(code: string, ranges: readonly Range[]): string {
161
+ const chars = code.split("");
162
+ for (const range of ranges) {
163
+ for (let index = range.start; index < range.end; index += 1) {
164
+ if (!/[\n\r\u2028\u2029]/u.test(chars[index] ?? "")) chars[index] = " ";
165
+ }
166
+ }
167
+ for (const range of ranges) if (range.statement) chars[range.start] = ";";
168
+ return chars.join("");
169
+ }
170
+
171
+ function lineColumn(code: string, offset: number): { line: number; column: number } {
172
+ const before = code.slice(0, offset).split(/\r\n|[\r\n\u2028\u2029]/u);
173
+ return { line: before.length, column: (before.at(-1)?.length ?? 0) + 1 };
174
+ }
@@ -0,0 +1,181 @@
1
+ // Lexical scanning shared by the cell rewrites: skip comments, strings, templates and regexes; read identifiers.
2
+
3
+ const IDENTIFIER_START_RE = /[$_\p{ID_Start}]/u;
4
+
5
+ const IDENTIFIER_CONTINUE_RE = /[$_\p{ID_Continue}\u200c\u200d]/u;
6
+
7
+ export const IDENTIFIER_RE = /^[\p{ID_Start}$_][\p{ID_Continue}\u200c\u200d]*$/u;
8
+
9
+ export const REGEX_PREFIX_KEYWORDS = new Set([
10
+ "await",
11
+ "case",
12
+ "delete",
13
+ "do",
14
+ "else",
15
+ "in",
16
+ "instanceof",
17
+ "new",
18
+ "of",
19
+ "return",
20
+ "throw",
21
+ "typeof",
22
+ "void",
23
+ "yield",
24
+ ]);
25
+
26
+ export const EXPRESSION_PREFIX_KEYWORDS = new Set(["await", "delete", "new", "throw", "typeof", "void", "yield"]);
27
+
28
+ export function nextSignificantIndex(code, start) {
29
+ for (let index = start; index < code.length; index += 1) {
30
+ if (/\s/u.test(code[index])) continue;
31
+ if (code.startsWith("//", index)) {
32
+ index = skipLineComment(code, index) - 1;
33
+ continue;
34
+ }
35
+ if (code.startsWith("/*", index)) {
36
+ index = skipBlockComment(code, index) - 1;
37
+ continue;
38
+ }
39
+ return index;
40
+ }
41
+ return code.length;
42
+ }
43
+
44
+ export function isDeclarationContinuation(char) {
45
+ return char !== undefined && /[.[(,+\-*/%&|^?:<>=!~]/u.test(char);
46
+ }
47
+
48
+ export function isExpressionOperator(char) {
49
+ return /[=,+\-*/%&|^?:<>!~]/u.test(char);
50
+ }
51
+
52
+ export function isIdentifierStart(char) {
53
+ return char !== undefined && IDENTIFIER_START_RE.test(char);
54
+ }
55
+
56
+ export function readIdentifier(code, start) {
57
+ let index = start + 1;
58
+ while (index < code.length && IDENTIFIER_CONTINUE_RE.test(code[index])) index += 1;
59
+ return index;
60
+ }
61
+
62
+ export function isDecimalDigit(char) {
63
+ return char !== undefined && /[0-9]/u.test(char);
64
+ }
65
+
66
+ export function skipNumberLiteral(code, start) {
67
+ let index = start + 1;
68
+ while (index < code.length && /[0-9A-Fa-f_xXn.eE]/u.test(code[index])) index += 1;
69
+ return index;
70
+ }
71
+
72
+ export function skipQuotedLiteral(code, start) {
73
+ const quote = code[start];
74
+ for (let index = start + 1; index < code.length; index += 1) {
75
+ if (code[index] === "\\") {
76
+ index += 1;
77
+ continue;
78
+ }
79
+ if (code[index] === quote) return index + 1;
80
+ }
81
+ return code.length;
82
+ }
83
+
84
+ export function skipTemplateLiteral(code, start) {
85
+ for (let index = start + 1; index < code.length; index += 1) {
86
+ const char = code[index];
87
+ if (char === "\\") {
88
+ index += 1;
89
+ continue;
90
+ }
91
+ if (char === "`") return index + 1;
92
+ if (char === "$" && code[index + 1] === "{") {
93
+ index = skipTemplateExpression(code, index + 2) - 1;
94
+ }
95
+ }
96
+ return code.length;
97
+ }
98
+
99
+ function skipTemplateExpression(code, start) {
100
+ let curly = 1;
101
+ let canStartRegex = true;
102
+ for (let index = start; index < code.length; index += 1) {
103
+ const char = code[index];
104
+ const next = code[index + 1];
105
+ if (char === "/" && next === "/") {
106
+ index = skipLineComment(code, index) - 1;
107
+ continue;
108
+ }
109
+ if (char === "/" && next === "*") {
110
+ index = skipBlockComment(code, index) - 1;
111
+ continue;
112
+ }
113
+ if (char === "'" || char === '"') {
114
+ index = skipQuotedLiteral(code, index) - 1;
115
+ canStartRegex = false;
116
+ continue;
117
+ }
118
+ if (char === "`") {
119
+ index = skipTemplateLiteral(code, index) - 1;
120
+ canStartRegex = false;
121
+ continue;
122
+ }
123
+ if (char === "/" && canStartRegex) {
124
+ index = skipRegexLiteral(code, index) - 1;
125
+ canStartRegex = false;
126
+ continue;
127
+ }
128
+ if (isIdentifierStart(char)) {
129
+ const end = readIdentifier(code, index);
130
+ canStartRegex = REGEX_PREFIX_KEYWORDS.has(code.slice(index, end));
131
+ index = end - 1;
132
+ continue;
133
+ }
134
+ if (char === "{") curly += 1;
135
+ else if (char === "}" && --curly === 0) return index + 1;
136
+ canStartRegex = char === "(" || char === "[" || char === "{" || isExpressionOperator(char) || char === ",";
137
+ }
138
+ return code.length;
139
+ }
140
+
141
+ export function skipRegexLiteral(code, start) {
142
+ let inClass = false;
143
+ for (let index = start + 1; index < code.length; index += 1) {
144
+ const char = code[index];
145
+ if (char === "\\") {
146
+ index += 1;
147
+ continue;
148
+ }
149
+ if (char === "[") {
150
+ inClass = true;
151
+ continue;
152
+ }
153
+ if (char === "]") {
154
+ inClass = false;
155
+ continue;
156
+ }
157
+ if (char === "/" && !inClass) {
158
+ let end = index + 1;
159
+ while (end < code.length && /[A-Za-z]/u.test(code[end])) end += 1;
160
+ return end;
161
+ }
162
+ if (isLineTerminator(char)) return index;
163
+ }
164
+ return code.length;
165
+ }
166
+
167
+ export function skipLineComment(code, start) {
168
+ for (let index = start + 2; index < code.length; index += 1) {
169
+ if (isLineTerminator(code[index])) return index;
170
+ }
171
+ return code.length;
172
+ }
173
+
174
+ export function isLineTerminator(char) {
175
+ return char === "\n" || char === "\r" || char === "\u2028" || char === "\u2029";
176
+ }
177
+
178
+ export function skipBlockComment(code, start) {
179
+ const end = code.indexOf("*/", start + 2);
180
+ return end < 0 ? code.length : end + 2;
181
+ }
@@ -83,6 +83,8 @@ export interface JavaScriptKernelOptions {
83
83
  readonly hostToolNames?: KernelToolNameSource;
84
84
  /** `kernelTools.enabled`; false makes `tool(fn)` refuse with `tools_unavailable`. Unset keeps them on. */
85
85
  readonly kernelToolsEnabled?: boolean;
86
+ /** `jsCells.scope`: install the `scope()` helper (init protocol). Unset leaves it out. */
87
+ readonly scopeHelper?: boolean;
86
88
  /** The kill guard (senpi#5349): where the worker imports the judge from, and the agent host's pid. Absent = guard off. */
87
89
  readonly killGuard?: { readonly moduleUrl: string | URL; readonly agentPid: number };
88
90
  /** Tool names registered in another kernel language, denied as JS kernel-tool identifiers. */