summaryrefslogtreecommitdiff
path: root/plugins/code-modernization/hooks/text.ts
diff options
context:
space:
mode:
authorMorgan Lunt <morganl@anthropic.com>2026-09-24 18:19:16 -0700
committerMorgan Lunt <morganl@anthropic.com>2026-09-24 18:19:16 -0700
commit54772aaaabf371dc212480fbf7da216bd65990c7 (patch)
treedf16d07b32801b695d616f16d87c1ad1a37739b5 /plugins/code-modernization/hooks/text.ts
parentc9e990089c6c87cb96e227e998edbada1df82861 (diff)
code-modernization: mark the pane's notes as data and clean the names in them
The pane adds two kinds of text to what the model reads: a note about the file it just read (names, rule titles and delta ids from the analysis files) and a one-line state. Those names were written by analysis agents from the legacy code, so a hostile comment could reach the model through them. Each such value is now cut to one short plain line: control, invisible and bidirectional characters, backticks, angle and square brackets and double quotes are replaced, and long values are trimmed. A note opens by saying its contents are data, never instructions, and closes with an end line no value can forge. The state line says it is a status line and not an instruction, and the next-step command names a module only when the module id is one plain token. The pane lists only system directories whose names the plugin's workflows accept.
Diffstat (limited to 'plugins/code-modernization/hooks/text.ts')
-rw-r--r--plugins/code-modernization/hooks/text.ts37
1 files changed, 37 insertions, 0 deletions
diff --git a/plugins/code-modernization/hooks/text.ts b/plugins/code-modernization/hooks/text.ts
new file mode 100644
index 00000000..ecbf1bc5
--- /dev/null
+++ b/plugins/code-modernization/hooks/text.ts
@@ -0,0 +1,37 @@
+/**
+ * Text that came from the legacy tree, or from an analysis of it, and is about to go in front of the model or into
+ * the prompt box: a node name, a rule title, a module id, a delta id. Such text can hold words written for a model,
+ * so it is not passed on as it is. Control, format (zero-width, bidirectional and tag) and line-separator characters are
+ * replaced too, so nothing in the value is invisible to a person reading the same line.
+ *
+ * `plain` keeps a value to one short line of ordinary characters. Control and invisible characters, backticks,
+ * angle and square brackets and double quotes are replaced, so the value cannot start a new line, open a fence, forge
+ * a note's own header or end line, or hide from a person reading it. `isToken` says whether a name is safe to place in
+ * a command line: no whitespace, quote, `$`, backtick or shell separator, and a letter or digit first.
+ */
+
+const INVISIBLE = /[\p{Cc}\p{Cf}\p{Zl}\p{Zp}]/gu
+
+export function plain(value: unknown, max = 80): string {
+ if (typeof value !== 'string') {
+ return ''
+ }
+
+ const flat = value
+ .replace(INVISIBLE, ' ')
+ .replace(/[`<>[\]]/g, '_')
+ .replace(/"/g, "'")
+ .replace(/\s+/g, ' ')
+ .trim()
+
+ return flat.length <= max ? flat : `${flat.slice(0, Math.max(1, max - 1)).trimEnd()}…`
+}
+
+const TOKEN = /^[A-Za-z0-9][A-Za-z0-9._:/#@+-]{0,79}$/
+
+/** The names the plugin's own workflows accept for a system: letters, digits, hyphen and underscore. */
+const SYSTEM_NAME = /^[A-Za-z0-9][A-Za-z0-9_-]{0,63}$/
+
+export const isToken = (value: string): boolean => TOKEN.test(value)
+
+export const isSystemName = (value: string): boolean => SYSTEM_NAME.test(value)