@soleur/opencode 3.357.9 → 3.357.11
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/agents/product/design/ux-design-lead.md +5 -5
- package/package.json +1 -1
- package/skills/brainstorm/references/brainstorm-brand-workshop.md +3 -3
- package/skills/gdpr-gate/NOTICE +1 -1
- package/skills/pencil-setup/SKILL.md +5 -1
- package/skills/pencil-setup/scripts/check_deps.sh +35 -3
- package/skills/pencil-setup/scripts/copy_adapter.sh +11 -5
- package/skills/pencil-setup/scripts/engine-contract-probe.sh +187 -0
- package/skills/pencil-setup/scripts/pencil-error-enrichment.mjs +20 -0
- package/skills/pencil-setup/scripts/pencil-mcp-adapter.mjs +507 -305
- package/skills/pencil-setup/scripts/pencil-repl-surface.mjs +136 -0
- package/skills/pencil-setup/scripts/pencil-response-classify.mjs +3 -0
- package/skills/pencil-setup/scripts/pencil-save-gate.mjs +164 -1
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
// pencil-repl-surface.mjs — the REPL contract between the adapter and
|
|
2
|
+
// @pencil.dev/cli 0.2.9, as pure functions (no imports) so bun tests need no
|
|
3
|
+
// MCP SDK. Retire with #11156 (migration to @pen.dev/cli, whose REPL has a
|
|
4
|
+
// different surface).
|
|
5
|
+
//
|
|
6
|
+
// Why this exists (#11143): the adapter sent `batch_design({ operations })`
|
|
7
|
+
// while the engine takes `{ input }`. The engine drops an unknown key and
|
|
8
|
+
// prints `OK`, so every design call "succeeded" and did nothing. Every REPL
|
|
9
|
+
// command line now flows through `formatReplCommand`, which refuses a tool or
|
|
10
|
+
// key that is not in this table. The table is the signature list printed by
|
|
11
|
+
// `pencil interactive --help` for the pinned version;
|
|
12
|
+
// engine-contract-probe.sh diffs the two on every pin bump.
|
|
13
|
+
|
|
14
|
+
export class ReplContractError extends Error {
|
|
15
|
+
constructor(message) {
|
|
16
|
+
super(message);
|
|
17
|
+
this.name = "ReplContractError";
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
// tool -> allowed parameter keys. `save` is the REPL's own command and takes none.
|
|
22
|
+
export const REPL_SURFACE = Object.freeze({
|
|
23
|
+
batch_design: ["input"],
|
|
24
|
+
batch_get: [
|
|
25
|
+
"includePathGeometry",
|
|
26
|
+
"nodeIds",
|
|
27
|
+
"parentId",
|
|
28
|
+
"patterns",
|
|
29
|
+
"readDepth",
|
|
30
|
+
"resolveInstances",
|
|
31
|
+
"resolveVariables",
|
|
32
|
+
"searchDepth",
|
|
33
|
+
],
|
|
34
|
+
export_html: [
|
|
35
|
+
"format",
|
|
36
|
+
"includeHtmlScaffold",
|
|
37
|
+
"includeLayerIds",
|
|
38
|
+
"includeLayerNames",
|
|
39
|
+
"nodeIds",
|
|
40
|
+
"outputPath",
|
|
41
|
+
],
|
|
42
|
+
export_nodes: ["format", "nodeIds", "outputDir", "quality", "scale"],
|
|
43
|
+
get_editor_state: ["include_schema"],
|
|
44
|
+
get_guidelines: ["category", "name", "params"],
|
|
45
|
+
get_screenshot: ["nodeId"],
|
|
46
|
+
get_variables: [],
|
|
47
|
+
save: [],
|
|
48
|
+
snapshot_layout: ["maxDepth", "parentId", "problemsOnly"],
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
// 0.2.9 no longer defines the short aliases Desktop-style scripts use
|
|
52
|
+
// (`I(...)`, `U(...)`: ReferenceError). `var`, not `const`, so a preamble that
|
|
53
|
+
// runs on every call can re-declare them.
|
|
54
|
+
export const BATCH_DESIGN_PREAMBLE =
|
|
55
|
+
"var I=Insert,U=Update,C=Copy,R=Replace,M=Move,D=Delete,G=Generate;";
|
|
56
|
+
|
|
57
|
+
// JSON.stringify leaves U+2028/U+2029 literal; readline may treat them as line
|
|
58
|
+
// ends, which would split one command across two REPL lines.
|
|
59
|
+
function jsonLine(value) {
|
|
60
|
+
return JSON.stringify(value)
|
|
61
|
+
.replace(/\u0085/g, "\\u0085")
|
|
62
|
+
.replace(/\u2028/g, "\\u2028")
|
|
63
|
+
.replace(/\u2029/g, "\\u2029");
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export function formatReplCommand(toolName, params) {
|
|
67
|
+
if (!Object.hasOwn(REPL_SURFACE, toolName)) {
|
|
68
|
+
throw new ReplContractError(
|
|
69
|
+
`Unknown tool: ${toolName} (not in the @pencil.dev/cli 0.2.9 REPL surface)`
|
|
70
|
+
);
|
|
71
|
+
}
|
|
72
|
+
const given = {};
|
|
73
|
+
for (const [k, v] of Object.entries(params ?? {})) {
|
|
74
|
+
if (v !== undefined) given[k] = v;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
if (toolName === "batch_design") {
|
|
78
|
+
const keys = Object.keys(given);
|
|
79
|
+
// The adapter's MCP schema calls the field `operations`; the engine calls
|
|
80
|
+
// it `input`. Accept only the former here so the two can never both be sent.
|
|
81
|
+
for (const k of keys) {
|
|
82
|
+
if (k !== "operations") {
|
|
83
|
+
throw new ReplContractError(`batch_design: unexpected parameter '${k}' (only 'operations' is accepted)`);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
const ops = given.operations;
|
|
87
|
+
if (typeof ops !== "string" || ops.trim() === "") {
|
|
88
|
+
throw new ReplContractError("batch_design: 'operations' must be a non-empty string");
|
|
89
|
+
}
|
|
90
|
+
return `batch_design({ input: ${jsonLine(`${BATCH_DESIGN_PREAMBLE}\n${ops}`)} })`;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
const allowed = REPL_SURFACE[toolName];
|
|
94
|
+
for (const k of Object.keys(given)) {
|
|
95
|
+
if (!allowed.includes(k)) {
|
|
96
|
+
throw new ReplContractError(
|
|
97
|
+
`${toolName}: unexpected parameter '${k}' (allowed: ${allowed.join(", ") || "none"})`
|
|
98
|
+
);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
if (Object.keys(given).length === 0) return `${toolName}()`;
|
|
102
|
+
return `${toolName}(${jsonLine(given)})`;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
// Decide the `--in` argument for one spawn of `pencil interactive --out <outFile>`.
|
|
106
|
+
//
|
|
107
|
+
// `initialInFile` is the caller's explicit input (a template). The adapter
|
|
108
|
+
// passes it for the open_document spawn only and passes none on a respawn, so a
|
|
109
|
+
// respawn after a crash reloads `outFile` and a template can never overwrite
|
|
110
|
+
// saved edits.
|
|
111
|
+
// Without an explicit input, an existing non-empty `outFile` is reloaded:
|
|
112
|
+
// before this, `open_document({ filePath })` started an empty document at
|
|
113
|
+
// `--out` and the next save replaced the file with 96 bytes.
|
|
114
|
+
//
|
|
115
|
+
// `resumeInFile` is the template a previous spawn of the SAME target loaded
|
|
116
|
+
// from. It applies only while `outFile` holds nothing saved: a child that dies
|
|
117
|
+
// before the first save would otherwise respawn empty and silently drop the
|
|
118
|
+
// template. Saved edits in `outFile` always win over it.
|
|
119
|
+
//
|
|
120
|
+
// `probe` is `{ exists(path): boolean, size(path): number }`, injected so this
|
|
121
|
+
// stays pure.
|
|
122
|
+
export function planSpawn({ outFile, initialInFile, resumeInFile = null, probe }) {
|
|
123
|
+
if (initialInFile) {
|
|
124
|
+
if (!probe.exists(initialInFile)) {
|
|
125
|
+
throw new Error(`Input file not found: ${initialInFile}`);
|
|
126
|
+
}
|
|
127
|
+
return { inFile: initialInFile };
|
|
128
|
+
}
|
|
129
|
+
if (probe.exists(outFile) && probe.size(outFile) > 0) {
|
|
130
|
+
return { inFile: outFile };
|
|
131
|
+
}
|
|
132
|
+
if (resumeInFile && probe.exists(resumeInFile)) {
|
|
133
|
+
return { inFile: resumeInFile };
|
|
134
|
+
}
|
|
135
|
+
return { inFile: null };
|
|
136
|
+
}
|
|
@@ -13,6 +13,9 @@ const ERROR_PATTERNS = [
|
|
|
13
13
|
/^Error:/m,
|
|
14
14
|
/^\[ERROR\]/m,
|
|
15
15
|
/^Invalid properties:/m,
|
|
16
|
+
// @pencil.dev/cli 0.2.9 answers a tool it no longer has with this line; it
|
|
17
|
+
// used to classify as success, and the auto-save ran after it (#11143).
|
|
18
|
+
/^Unknown tool:/m,
|
|
16
19
|
// Auth-failure responses from the pencil REPL. Anchored to start-of-line
|
|
17
20
|
// (via multiline flag) for `Unauthorized` / `HTTP 401` / `Invalid API key`
|
|
18
21
|
// so a node name echoed back in `batch_get` output ("Unauthorized state")
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
// importing the adapter.
|
|
4
4
|
//
|
|
5
5
|
// Why this exists: the adapter previously called save() unconditionally
|
|
6
|
-
// after every mutating op (batch_design, open_document,
|
|
6
|
+
// after every mutating op (batch_design, open_document,
|
|
7
7
|
// etc.). When a mutation errored (auth failure, invalid property), the
|
|
8
8
|
// follow-up save() still ran and wrote a 0-byte or stale .pen file to
|
|
9
9
|
// disk. The caller saw a successful save but no real content — the
|
|
@@ -24,3 +24,166 @@ export function shouldSkipSave(lastClassification) {
|
|
|
24
24
|
}
|
|
25
25
|
return Boolean(lastClassification.isError);
|
|
26
26
|
}
|
|
27
|
+
|
|
28
|
+
// --- Empty-overwrite sentinel (#11143) ---------------------------------
|
|
29
|
+
//
|
|
30
|
+
// The headless engine writes only on `save()`, and a document opened without
|
|
31
|
+
// `--in` is empty, so any save over an existing .pen replaced it with the
|
|
32
|
+
// 96-byte empty document. Every save now goes through `runGuardedSave`: an
|
|
33
|
+
// empty in-memory document is never written over a file that still holds a
|
|
34
|
+
// top-level node, and nothing is written over a file that exists but is not a
|
|
35
|
+
// recognizable .pen document. The check compares the live file with the live
|
|
36
|
+
// document, so there is no stored value for an edit to weaken.
|
|
37
|
+
//
|
|
38
|
+
// Known limits: this catches empty-over-non-empty, not partial truncation or a
|
|
39
|
+
// lower node count, and it cannot see a second writer between the disk read
|
|
40
|
+
// and the engine write.
|
|
41
|
+
|
|
42
|
+
/** The file exists with content, but it is not a document the engine can load back. */
|
|
43
|
+
export const DISK_UNRECOGNIZED = "unrecognized";
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* State of the .pen text on disk:
|
|
47
|
+
* null missing or blank: nothing recoverable to protect
|
|
48
|
+
* a number a parseable document with that many top-level nodes
|
|
49
|
+
* DISK_UNRECOGNIZED non-blank text that is not a .pen document (unparseable
|
|
50
|
+
* JSON, a git conflict, a document with no children array).
|
|
51
|
+
* The engine loads such a file as an EMPTY document without
|
|
52
|
+
* an error, so the next save would destroy hand-recoverable
|
|
53
|
+
* content.
|
|
54
|
+
*/
|
|
55
|
+
export function diskTopLevelCount(text) {
|
|
56
|
+
if (text === null || text === undefined) return null;
|
|
57
|
+
let s = String(text);
|
|
58
|
+
if (s.charCodeAt(0) === 0xfeff) s = s.slice(1);
|
|
59
|
+
if (s.trim() === "") return null;
|
|
60
|
+
let doc;
|
|
61
|
+
try {
|
|
62
|
+
doc = JSON.parse(s);
|
|
63
|
+
} catch {
|
|
64
|
+
return DISK_UNRECOGNIZED;
|
|
65
|
+
}
|
|
66
|
+
if (doc && typeof doc === "object" && Array.isArray(doc.children)) {
|
|
67
|
+
return doc.children.length;
|
|
68
|
+
}
|
|
69
|
+
return DISK_UNRECOGNIZED;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Top-level node count of the in-memory document from a `batch_get()` result.
|
|
74
|
+
* null when the shape cannot be determined (the caller must treat that as
|
|
75
|
+
* unsafe, not as zero). Pass the UNTRUNCATED text: a capped response is not
|
|
76
|
+
* valid JSON and would read as unreadable.
|
|
77
|
+
*/
|
|
78
|
+
export function memoryTopLevelCount(batchGetText) {
|
|
79
|
+
if (batchGetText === null || batchGetText === undefined) return null;
|
|
80
|
+
let parsed;
|
|
81
|
+
try {
|
|
82
|
+
parsed = JSON.parse(String(batchGetText));
|
|
83
|
+
} catch {
|
|
84
|
+
return null;
|
|
85
|
+
}
|
|
86
|
+
if (Array.isArray(parsed)) return parsed.length;
|
|
87
|
+
if (parsed && typeof parsed === "object" && Array.isArray(parsed.nodes)) {
|
|
88
|
+
return parsed.nodes.length;
|
|
89
|
+
}
|
|
90
|
+
return null;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* @returns {{ allow: boolean, reason: string }}
|
|
95
|
+
*/
|
|
96
|
+
export function decideSave({ diskCount, memoryCount }) {
|
|
97
|
+
if (diskCount === null || diskCount === undefined) {
|
|
98
|
+
return { allow: true, reason: "target missing or blank: nothing to protect" };
|
|
99
|
+
}
|
|
100
|
+
if (diskCount === DISK_UNRECOGNIZED) {
|
|
101
|
+
return {
|
|
102
|
+
allow: false,
|
|
103
|
+
reason: "target exists but is not a recognizable .pen document",
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
if (diskCount === 0) {
|
|
107
|
+
return { allow: true, reason: "target holds no nodes: nothing to protect" };
|
|
108
|
+
}
|
|
109
|
+
if (memoryCount === null || memoryCount === undefined) {
|
|
110
|
+
return {
|
|
111
|
+
allow: false,
|
|
112
|
+
reason: `target holds ${diskCount} top-level node(s) and the in-memory document could not be read`,
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
if (memoryCount === 0) {
|
|
116
|
+
return {
|
|
117
|
+
allow: false,
|
|
118
|
+
reason: `target holds ${diskCount} top-level node(s) but the in-memory document is empty`,
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
return { allow: true, reason: `in-memory document has ${memoryCount} top-level node(s)` };
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
export function refusalMessage({ file, source, diskCount, memoryCount, memoryReason, diskReason }) {
|
|
125
|
+
const lines = [`REFUSED save: ${file} was not modified.`];
|
|
126
|
+
if (diskCount === DISK_UNRECOGNIZED) {
|
|
127
|
+
lines.push(
|
|
128
|
+
`It exists but is not a recognizable .pen document${diskReason ? ` (${diskReason})` : ""}: unparseable JSON, git conflict markers, or not a Pencil file. The engine would load it as an empty document and the save would destroy content you may still be able to recover.`,
|
|
129
|
+
"Next step: open_document a new file path and continue the design there. Do not delete or recreate the file to bypass this check."
|
|
130
|
+
);
|
|
131
|
+
} else if (memoryCount === null || memoryCount === undefined) {
|
|
132
|
+
lines.push(
|
|
133
|
+
`It holds ${diskCount} top-level node(s) on disk, but the in-memory document could not be read${memoryReason ? `: ${memoryReason}` : ""}, so the save could not be checked.`,
|
|
134
|
+
"Next step: retry the call; if it keeps failing, open_document a new file path. Do not delete or recreate the file to bypass this check."
|
|
135
|
+
);
|
|
136
|
+
} else {
|
|
137
|
+
lines.push(
|
|
138
|
+
`It holds ${diskCount} top-level node(s) on disk, but the in-memory document has ${memoryCount}; saving would replace the file with an empty document.`,
|
|
139
|
+
"Likely cause: the file was never loaded into this session. Next step: call open_document on this file (it now loads the existing content), then re-apply the edit; to start over instead, open_document a new file path. Do not delete or recreate the file to bypass this check."
|
|
140
|
+
);
|
|
141
|
+
}
|
|
142
|
+
if (source === "auto-save") {
|
|
143
|
+
lines.push(
|
|
144
|
+
"The edit you just made is applied in memory but not persisted, and will be lost on the next open_document."
|
|
145
|
+
);
|
|
146
|
+
}
|
|
147
|
+
return lines.join("\n");
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* The only save path. Dependencies are injected so the ordering (check before
|
|
152
|
+
* save, no save sent on refusal) is unit-testable without the MCP SDK.
|
|
153
|
+
* `readDisk` and `probeMemory` may throw: a throw is an UNKNOWN state, never a
|
|
154
|
+
* clean one, and its message is carried into the refusal.
|
|
155
|
+
*
|
|
156
|
+
* @param {{ file: string, source: "save"|"auto-save", readDisk: () => Promise<string|null>,
|
|
157
|
+
* probeMemory: () => Promise<string|null>, send: () => Promise<string> }} deps
|
|
158
|
+
*/
|
|
159
|
+
export async function runGuardedSave({ file, source, readDisk, probeMemory, send }) {
|
|
160
|
+
let diskCount;
|
|
161
|
+
let diskReason;
|
|
162
|
+
try {
|
|
163
|
+
diskCount = diskTopLevelCount(await readDisk());
|
|
164
|
+
} catch (err) {
|
|
165
|
+
diskCount = DISK_UNRECOGNIZED;
|
|
166
|
+
diskReason = String(err?.message ?? err);
|
|
167
|
+
}
|
|
168
|
+
let memoryCount = null;
|
|
169
|
+
let memoryReason;
|
|
170
|
+
if (typeof diskCount === "number" && diskCount > 0) {
|
|
171
|
+
try {
|
|
172
|
+
memoryCount = memoryTopLevelCount(await probeMemory());
|
|
173
|
+
if (memoryCount === null) memoryReason = "its batch_get() result was not a node list";
|
|
174
|
+
} catch (err) {
|
|
175
|
+
memoryReason = String(err?.message ?? err);
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
const decision = decideSave({ diskCount, memoryCount });
|
|
179
|
+
if (!decision.allow) {
|
|
180
|
+
return {
|
|
181
|
+
saved: false,
|
|
182
|
+
refused: true,
|
|
183
|
+
reason: decision.reason,
|
|
184
|
+
message: refusalMessage({ file, source, diskCount, memoryCount, memoryReason, diskReason }),
|
|
185
|
+
};
|
|
186
|
+
}
|
|
187
|
+
const raw = await send();
|
|
188
|
+
return { saved: true, refused: false, raw };
|
|
189
|
+
}
|