@volter/supercode-frontend-vscode 0.1.61 → 0.1.63

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.
@@ -0,0 +1,230 @@
1
+ // Generated from sdk/interchange/user-provenance.mjs by scripts/user-provenance-copies.mjs (the pre-commit hook
2
+ // runs it): edit that rule, not this file.
3
+ // Whose words a user-role record carries: the person's, or the harness's own.
4
+ // One rule for every reader (the live projection, replay, history, client
5
+ // transcripts, Teams' turn and mail readers, voice, trace). It imports nothing,
6
+ // so a host that cannot resolve this package (the VS Code extension, the browser
7
+ // observer, the hosted viewer, supercode-ui) carries its exact bytes; the
8
+ // pre-commit hook writes those copies (scripts/user-provenance-copies.mjs).
9
+ //
10
+ // The rule reads, in order:
11
+ // 1. a person's marker: the record says a person wrote it. A person who quotes
12
+ // a frame (`<task-notification> showed up in my chat`) or types a reserved
13
+ // notice is still the person.
14
+ // 2. a harness marker: the record says the harness wrote it.
15
+ // 3. only for a record with no marker either way, its text: a reserved notice
16
+ // or a frame the harness opens its unmarked records with.
17
+ // A record with nothing of the three is the person's.
18
+ //
19
+ // Measured (local Claude Code corpus; Claude Code 2.1.312 stream-json):
20
+ // - person records carry `origin.kind = "human"` or `promptSource` typed /
21
+ // queued / sdk; no task notification carries either. Claude Code's relay of a
22
+ // person's line into a busy fork ("The user sent a new message while you were
23
+ // working: …") carries both `isMeta` and `origin.kind = "human"` (15): the
24
+ // person's words, so the person's marker decides. `isMeta` records otherwise
25
+ // carry `promptSource = "system"` only (35,680);
26
+ // - task notifications: `origin.kind = "task-notification"` and
27
+ // `promptSource = "system"` on every user record opening `<task-notification>`
28
+ // (1,964); `commandMode = "task-notification"` on every such `queued_command`
29
+ // attachment (1,586, against 926 `commandMode = "prompt"`, each a person's);
30
+ // - local-command caveats and image-coordinate notes: `isMeta` in the
31
+ // transcript (397 caveats);
32
+ // - the image-coordinate note on the live stream-json door: `isSynthetic: true`
33
+ // and no `isMeta` (the transcript stores the same uuid with `isMeta: true`).
34
+ // The stream carries no person's marker at all: it does not echo the person's
35
+ // prompt (the host publishes that itself), so its records reach step 2 or 3;
36
+ // - compact summaries: `isCompactSummary` (with `isVisibleInTranscriptOnly`);
37
+ // - subagent traffic: `isSidechain` in the transcript, `parent_tool_use_id` on
38
+ // the stream; tool results: `toolUseResult` / `tool_use_result` or a
39
+ // `tool_result` block.
40
+ // Unmarked records, whose text is the only evidence (a 400-transcript sample of
41
+ // unmarked main-thread user text: `<command-name>` 18, `<local-command-stdout>`
42
+ // 16, `<bash-input>` 13, `<bash-stdout>` 13, the interruption notice 13, plain 2):
43
+ // - the interruption record, whose whole text is one of Claude's reserved
44
+ // notices (832 and 195 corpus occurrences of the two below);
45
+ // - slash-command and shell-mode plumbing (`<command-name>`, `<command-message>`,
46
+ // `<local-command-stdout>`, `<bash-input>`, `<bash-stdout>`, …): a local
47
+ // command the terminal ran and its output, not a message to the agent;
48
+ // - a task notification on a live adapter path, which can arrive without the
49
+ // persisted stamps (not yet measured on the stream-json door);
50
+ // - context other harnesses and hosts open a user turn with (AGENTS.md / CLAUDE.md
51
+ // instructions, `<environment_context>`, `<system-reminder>`, …) and
52
+ // supercode's own frames (`<supercode-notice>`, a cross-session notice, an
53
+ // edited-file attachment flattened to text).
54
+ // Frames are openers, never substrings: the tag is the part the harness controls,
55
+ // and a person quoting one mid-sentence is still talking.
56
+
57
+ /** `promptSource` values a person's input carries (Claude Code's, and the hosts'
58
+ * that deliver a person's turn: Teams rooms, terminals, apps). */
59
+ export const PERSON_PROMPT_SOURCES = Object.freeze([
60
+ "typed",
61
+ "queued",
62
+ "sdk",
63
+ "user",
64
+ "human",
65
+ "terminal",
66
+ "api",
67
+ "room",
68
+ "app",
69
+ ]);
70
+
71
+ /** Metadata a host stamps on a turn it delivered for an authenticated person. */
72
+ const PERSON_ATTRIBUTION_KEYS = Object.freeze([
73
+ "source_via",
74
+ "source_principal_id",
75
+ "source_name",
76
+ ]);
77
+
78
+ /** Attachment subtypes that are an envelope the runtime built around a file
79
+ * body. `attachmentType` is Claude Code's subtype, kept by the session loader. */
80
+ const CONTEXT_ATTACHMENT_TYPES = new Set(["file", "edited_text_file", "nested_memory"]);
81
+
82
+ /** Claude's reserved interruption notices, persisted as unmarked user records. */
83
+ export const CLAUDE_INTERRUPTION_TEXTS = Object.freeze([
84
+ "[Request interrupted by user]",
85
+ "[Request interrupted by user for tool use]",
86
+ ]);
87
+
88
+ /** The openers of user-role records the harness or host writes with no marker. */
89
+ const HARNESS_FRAMES = Object.freeze([
90
+ /^<(?:command-name|command-message|local-command-[a-z]+|bash-input|bash-stdout|bash-stderr)>/,
91
+ /^<(?:system-reminder|environment_context|user_instructions|user_info|hook_prompt|supercode-notice)\b/,
92
+ /^(?:# )?(?:AGENTS|CLAUDE)\.md instructions for .+\n\n<INSTRUCTIONS>/,
93
+ /^<recommended_plugins>[\s\S]*<\/recommended_plugins>\s*(?:# )?(?:AGENTS|CLAUDE)\.md instructions for .+\n\n<INSTRUCTIONS>/,
94
+ /^\[Cross-session [a-z ]*notice\]/i,
95
+ /^\[edited file:/,
96
+ ]);
97
+ const TASK_NOTIFICATION_OPENER = "<task-notification>";
98
+
99
+ const TURN_INTERRUPTED_NOTICE = "Turn interrupted.";
100
+ const NOTICE_FIELD_CHARACTERS = 200;
101
+
102
+ // The loader keeps flags as "true"; a host that kept the native boolean is read the same.
103
+ const flag = (metadata, key) =>
104
+ metadata?.[key] === "true" || metadata?.[key] === true;
105
+ const object = (value) =>
106
+ value !== null && typeof value === "object" && !Array.isArray(value);
107
+
108
+ /** Whether a host delivered this turn for an authenticated person. */
109
+ export function isPersonAttributed(metadata) {
110
+ return PERSON_ATTRIBUTION_KEYS.some((key) => metadata?.[key] != null);
111
+ }
112
+
113
+ /** Whether the record carries a person's marker (rule step 1). */
114
+ export function isPersonMarked(metadata) {
115
+ if (!metadata) return false;
116
+ return (
117
+ isPersonAttributed(metadata) ||
118
+ Object.keys(metadata).some((key) => key.startsWith("codex_typed_user")) ||
119
+ PERSON_PROMPT_SOURCES.includes(metadata.promptSource) ||
120
+ metadata.origin === "human" ||
121
+ metadata.commandMode === "prompt"
122
+ );
123
+ }
124
+
125
+ const taskNotificationMarked = (metadata) =>
126
+ metadata?.origin === "task-notification" ||
127
+ metadata?.commandMode === "task-notification";
128
+
129
+ /** Whether the record carries a harness marker (rule step 2). */
130
+ export function isHarnessMarked(metadata) {
131
+ if (!metadata) return false;
132
+ return (
133
+ taskNotificationMarked(metadata) ||
134
+ metadata.promptSource === "system" ||
135
+ flag(metadata, "isMeta") ||
136
+ flag(metadata, "isSynthetic") ||
137
+ flag(metadata, "isCompactSummary") ||
138
+ flag(metadata, "isSidechain") ||
139
+ flag(metadata, "toolResult") ||
140
+ CONTEXT_ATTACHMENT_TYPES.has(metadata.attachmentType)
141
+ );
142
+ }
143
+
144
+ /** Whether `text` is, in full, one of Claude's reserved interruption notices. */
145
+ export function isClaudeInterruption(text) {
146
+ return typeof text === "string" && CLAUDE_INTERRUPTION_TEXTS.includes(text.trim());
147
+ }
148
+
149
+ /** Whether `text` opens with a frame the harness or host writes its unmarked
150
+ * user-role records with (rule step 3; a task notification included). */
151
+ export function isHarnessFrame(text) {
152
+ if (typeof text !== "string") return false;
153
+ const body = text.trimStart();
154
+ return (
155
+ body.startsWith(TASK_NOTIFICATION_OPENER) ||
156
+ HARNESS_FRAMES.some((frame) => frame.test(body))
157
+ );
158
+ }
159
+
160
+ function taskNotice(text) {
161
+ const tag = (name) => {
162
+ const value =
163
+ new RegExp(`<${name}>([\\s\\S]*?)</${name}>`).exec(text)?.[1]?.trim() ?? "";
164
+ return [...value].length <= NOTICE_FIELD_CHARACTERS
165
+ ? value
166
+ : `${[...value].slice(0, NOTICE_FIELD_CHARACTERS - 1).join("")}…`;
167
+ };
168
+ const summary = tag("summary"),
169
+ status = tag("status");
170
+ return `Background task${status ? ` ${status}` : ""}${summary ? `: ${summary}` : ""}`;
171
+ }
172
+
173
+ /**
174
+ * The provenance of one user-role record: `metadata` is the string map the
175
+ * session loader keeps (or `claudeRecordProvenance` reads from a native record);
176
+ * `text` is the record's text. `source` is `"person"` for the person's own words
177
+ * and `"harness"` for a record the harness wrote; `notice` is the short plain-text
178
+ * line a reader shows in place of a harness record, where one is shown at all
179
+ * (each renderer styles it; none shows the record's own text).
180
+ * @param {Record<string, string> | null | undefined} metadata
181
+ * @param {string} [text]
182
+ * @returns {{ source: "person" | "harness", notice?: string }}
183
+ */
184
+ export function userProvenance(metadata, text = "") {
185
+ const body = typeof text === "string" ? text : "";
186
+ if (isPersonMarked(metadata)) return { source: "person" };
187
+ if (taskNotificationMarked(metadata))
188
+ return { source: "harness", notice: taskNotice(body) };
189
+ if (isHarnessMarked(metadata)) return { source: "harness" };
190
+ if (body.trimStart().startsWith(TASK_NOTIFICATION_OPENER))
191
+ return { source: "harness", notice: taskNotice(body) };
192
+ if (isClaudeInterruption(body))
193
+ return { source: "harness", notice: TURN_INTERRUPTED_NOTICE };
194
+ if (isHarnessFrame(body)) return { source: "harness" };
195
+ return { source: "person" };
196
+ }
197
+
198
+ /** Whether a user-role record is the harness's own, not the person's words. */
199
+ export function isHarnessAuthoredUser(metadata, text = "") {
200
+ return userProvenance(metadata, text).source === "harness";
201
+ }
202
+
203
+ /**
204
+ * The rule's metadata read from one whole native Claude Code `user` record, as
205
+ * the stream-json door emits it or the transcript stores it. Reads the record,
206
+ * never one content block of it: the stamps sit beside `message`.
207
+ * @param {unknown} record
208
+ * @returns {Record<string, string>}
209
+ */
210
+ export function claudeRecordProvenance(record) {
211
+ const metadata = {};
212
+ if (!object(record)) return metadata;
213
+ if (typeof record.promptSource === "string")
214
+ metadata.promptSource = record.promptSource;
215
+ for (const key of ["isMeta", "isSynthetic", "isCompactSummary", "isSidechain"])
216
+ if (record[key] === true) metadata[key] = "true";
217
+ if (typeof record.parent_tool_use_id === "string")
218
+ metadata.isSidechain = "true";
219
+ if (object(record.origin) && typeof record.origin.kind === "string")
220
+ metadata.origin = record.origin.kind;
221
+ const content = object(record.message) ? record.message.content : undefined;
222
+ if (
223
+ Object.hasOwn(record, "toolUseResult") ||
224
+ Object.hasOwn(record, "tool_use_result") ||
225
+ (Array.isArray(content) &&
226
+ content.some((block) => object(block) && block.type === "tool_result"))
227
+ )
228
+ metadata.toolResult = "true";
229
+ return metadata;
230
+ }