@creator-notes/cnotes 0.109.0 → 0.110.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.
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
/** "<group> <command>" → bracket. Group names, not aliases. */
|
|
2
|
+
export const INTENT_COMMANDS = {
|
|
3
|
+
"notes create": "workspace",
|
|
4
|
+
"versions create": "workspace",
|
|
5
|
+
"canvas create": "workspace",
|
|
6
|
+
"canvas update": "workspace",
|
|
7
|
+
"canvas place": "workspace",
|
|
8
|
+
"canvas arrange": "workspace",
|
|
9
|
+
"canvas add-node": "workspace",
|
|
10
|
+
"canvas add-text": "workspace",
|
|
11
|
+
"canvas add-section": "workspace",
|
|
12
|
+
"canvas add-list": "workspace",
|
|
13
|
+
"canvas add-link": "workspace",
|
|
14
|
+
"canvas add-quote": "workspace",
|
|
15
|
+
"canvas style-quote": "workspace",
|
|
16
|
+
"canvas bulk-add": "workspace",
|
|
17
|
+
"canvas bulk-move": "workspace",
|
|
18
|
+
"canvas move-node": "workspace",
|
|
19
|
+
"canvas add-edge": "workspace",
|
|
20
|
+
"canvas update-edge": "workspace",
|
|
21
|
+
"canvas update-section": "workspace",
|
|
22
|
+
"canvas update-text": "workspace",
|
|
23
|
+
"canvas bulk-remove": "canvas",
|
|
24
|
+
"canvas remove-node": "canvas",
|
|
25
|
+
"canvas remove-edge": "canvas",
|
|
26
|
+
};
|
|
27
|
+
/**
|
|
28
|
+
* Set on the re-run of the command inside the run intent-runner opened, so
|
|
29
|
+
* that process writes under the run instead of opening another.
|
|
30
|
+
*/
|
|
31
|
+
export const INTENT_CHILD_ENV = "CNOTES_INTENT_CHILD";
|
|
32
|
+
export const INTENT_OPTION_DESCRIPTION = "Timeline headline for this change: opens an agent run for this one command and closes it after, " +
|
|
33
|
+
"so no `operations begin`/`end` is needed. Verb-first, at most 110 characters " +
|
|
34
|
+
'("Added the pricing risks to the Q1 roadmap"). When a run is already open, the write joins it instead.';
|
|
35
|
+
const brackets = new WeakMap();
|
|
36
|
+
/** Add `--intent` to every command in INTENT_COMMANDS. Call after all commands are registered. */
|
|
37
|
+
export function applyIntentOptions(program) {
|
|
38
|
+
for (const group of program.commands) {
|
|
39
|
+
for (const cmd of group.commands) {
|
|
40
|
+
const bracket = INTENT_COMMANDS[`${group.name()} ${cmd.name()}`];
|
|
41
|
+
if (!bracket)
|
|
42
|
+
continue;
|
|
43
|
+
cmd.option("--intent <headline>", INTENT_OPTION_DESCRIPTION);
|
|
44
|
+
brackets.set(cmd, bracket);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
export function intentBracketFor(cmd) {
|
|
49
|
+
return brackets.get(cmd);
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* What `--intent` should do for this invocation. A canvas bracket needs the
|
|
53
|
+
* canvas the command names; without one it falls back to a workspace run.
|
|
54
|
+
*/
|
|
55
|
+
export function decideIntentRun(input) {
|
|
56
|
+
if (!input.intent?.trim() || !input.bracket || input.isChild)
|
|
57
|
+
return { kind: "none" };
|
|
58
|
+
if (input.attachedRunId)
|
|
59
|
+
return { kind: "join", agentRunId: input.attachedRunId };
|
|
60
|
+
if (input.bracket === "canvas" && input.canvasRef) {
|
|
61
|
+
return { kind: "open", bracket: "canvas", canvasRef: input.canvasRef };
|
|
62
|
+
}
|
|
63
|
+
return { kind: "open", bracket: "workspace" };
|
|
64
|
+
}
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import { spawn } from "node:child_process";
|
|
2
|
+
import { ApiClient, resolveActiveAgentRun } from "./api-client.js";
|
|
3
|
+
import { resolveConfig } from "./config.js";
|
|
4
|
+
import { loadConfig, registerAgentRun, unregisterAgentRun } from "./auth-store.js";
|
|
5
|
+
import { resolveHostAgentSession } from "./claude-session.js";
|
|
6
|
+
import { readHeadlineWarnings, reportHeadlineWarnings } from "./headline-warnings.js";
|
|
7
|
+
import { canvasActionPath } from "./canvas-paths.js";
|
|
8
|
+
import { handleError } from "./errors.js";
|
|
9
|
+
import { decideIntentRun, intentBracketFor, INTENT_CHILD_ENV } from "./intent-run.js";
|
|
10
|
+
/**
|
|
11
|
+
* Root preAction hook: when the command carries `--intent`, open a run, re-run
|
|
12
|
+
* this same command line as a child with the run in its environment, close the
|
|
13
|
+
* run, and exit with the child's code. The command never runs in this process.
|
|
14
|
+
*
|
|
15
|
+
* A child process rather than a postAction hook because commands exit through
|
|
16
|
+
* handleError → process.exit, and a run must close even when its write fails
|
|
17
|
+
* (same reasoning as `canvas agent-run wrap`). An unclosed run is reaped as
|
|
18
|
+
* abandoned, which still keeps its writes attributed, but leaves no after
|
|
19
|
+
* snapshot.
|
|
20
|
+
*/
|
|
21
|
+
export async function runUnderIntent(program, actionCommand) {
|
|
22
|
+
const bracket = intentBracketFor(actionCommand);
|
|
23
|
+
const intent = actionCommand.opts().intent;
|
|
24
|
+
if (!bracket || !intent)
|
|
25
|
+
return;
|
|
26
|
+
const cfg = resolveConfig(program.opts());
|
|
27
|
+
const decision = decideIntentRun({
|
|
28
|
+
intent,
|
|
29
|
+
bracket,
|
|
30
|
+
canvasRef: actionCommand.processedArgs[0],
|
|
31
|
+
attachedRunId: resolveActiveAgentRun(cfg.serverUrl)?.agentRunId,
|
|
32
|
+
isChild: process.env[INTENT_CHILD_ENV] === "1",
|
|
33
|
+
});
|
|
34
|
+
if (decision.kind === "none")
|
|
35
|
+
return;
|
|
36
|
+
if (decision.kind === "join") {
|
|
37
|
+
console.error(`# joined the open run ${decision.agentRunId}; --intent is not its headline`);
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
const api = new ApiClient(cfg.serverUrl, cfg.token, cfg.workspaceId);
|
|
41
|
+
const agentSession = await resolveHostAgentSession();
|
|
42
|
+
const canvasPath = decision.canvasRef ? canvasActionPath(decision.canvasRef, cfg.workspaceId) : null;
|
|
43
|
+
let agentRunId;
|
|
44
|
+
let beforeSnapshotId;
|
|
45
|
+
try {
|
|
46
|
+
if (canvasPath) {
|
|
47
|
+
const begin = await api.post(canvasPath, {
|
|
48
|
+
action: "agentRunBegin",
|
|
49
|
+
prompt: intent,
|
|
50
|
+
...(agentSession ? { agentSession } : {}),
|
|
51
|
+
});
|
|
52
|
+
agentRunId = begin.agentRunId;
|
|
53
|
+
beforeSnapshotId = begin.beforeCheckpointId;
|
|
54
|
+
reportHeadlineWarnings(readHeadlineWarnings(begin));
|
|
55
|
+
}
|
|
56
|
+
else {
|
|
57
|
+
const begin = await api.post("/api/operations", {
|
|
58
|
+
action: "begin",
|
|
59
|
+
workspaceId: cfg.workspaceId,
|
|
60
|
+
prompt: intent,
|
|
61
|
+
...(agentSession ? { agentSession } : {}),
|
|
62
|
+
});
|
|
63
|
+
agentRunId = begin.agentRunId;
|
|
64
|
+
reportHeadlineWarnings(readHeadlineWarnings(begin));
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
catch (error) {
|
|
68
|
+
handleError(error, cfg.json);
|
|
69
|
+
}
|
|
70
|
+
// In the registry only, never the active pointer: the run is scoped to the
|
|
71
|
+
// child by its environment, and `operations end` recognises it as ours if the
|
|
72
|
+
// close below fails and the user follows the printed advice.
|
|
73
|
+
registerAgentRun(loadConfig(), cfg.serverUrl, agentRunId, {
|
|
74
|
+
startedAt: Date.now(),
|
|
75
|
+
detached: true,
|
|
76
|
+
...(beforeSnapshotId ? { beforeSnapshotId, canvasId: decision.canvasRef } : {}),
|
|
77
|
+
});
|
|
78
|
+
// Ctrl-C reaches the child through the shared terminal; this process stays
|
|
79
|
+
// alive to close the run once the child is gone.
|
|
80
|
+
const ignoreSigint = () => { };
|
|
81
|
+
process.on("SIGINT", ignoreSigint);
|
|
82
|
+
const child = spawn(process.execPath, [...process.execArgv, ...process.argv.slice(1)], {
|
|
83
|
+
stdio: "inherit",
|
|
84
|
+
env: {
|
|
85
|
+
...process.env,
|
|
86
|
+
[INTENT_CHILD_ENV]: "1",
|
|
87
|
+
CNOTES_AGENT_RUN_ID: agentRunId,
|
|
88
|
+
...(beforeSnapshotId ? { CNOTES_AGENT_BEFORE_SNAPSHOT_ID: beforeSnapshotId } : {}),
|
|
89
|
+
},
|
|
90
|
+
});
|
|
91
|
+
const exitCode = await new Promise((resolve) => {
|
|
92
|
+
child.on("exit", (code, signal) => resolve(signal ? 128 : (code ?? 1)));
|
|
93
|
+
child.on("error", (err) => {
|
|
94
|
+
console.error(`# could not re-run the command under its run: ${err.message}`);
|
|
95
|
+
resolve(1);
|
|
96
|
+
});
|
|
97
|
+
});
|
|
98
|
+
process.off("SIGINT", ignoreSigint);
|
|
99
|
+
try {
|
|
100
|
+
if (canvasPath) {
|
|
101
|
+
await api.post(canvasPath, { action: "agentRunEnd", agentRunId });
|
|
102
|
+
}
|
|
103
|
+
else {
|
|
104
|
+
await api.post("/api/operations", { action: "end", agentRunId });
|
|
105
|
+
}
|
|
106
|
+
unregisterAgentRun(loadConfig(), cfg.serverUrl, agentRunId);
|
|
107
|
+
}
|
|
108
|
+
catch (error) {
|
|
109
|
+
console.error(`# WARNING: the run for --intent did not close: ${error instanceof Error ? error.message : error}`);
|
|
110
|
+
console.error(canvasPath
|
|
111
|
+
? `# close it with: cnotes canvas agent-run end --canvas ${decision.canvasRef} --run-id ${agentRunId}`
|
|
112
|
+
: `# close it with: cnotes operations end --agent-run-id ${agentRunId}`);
|
|
113
|
+
}
|
|
114
|
+
process.exit(exitCode);
|
|
115
|
+
}
|
package/dist/program.js
CHANGED
|
@@ -35,6 +35,8 @@ import { registerClaudeHookCommand } from "./commands/claude-hook.js";
|
|
|
35
35
|
import { loadConfig, getServerConfig } from "./lib/auth-store.js";
|
|
36
36
|
import { parseFields } from "./lib/config.js";
|
|
37
37
|
import { setOutputMode } from "./lib/output.js";
|
|
38
|
+
import { applyIntentOptions } from "./lib/intent-run.js";
|
|
39
|
+
import { runUnderIntent } from "./lib/intent-runner.js";
|
|
38
40
|
/**
|
|
39
41
|
* Appended to Commander's own error text. Agents read stderr, and both errors
|
|
40
42
|
* below used to cost them a wrong conclusion rather than a retry.
|
|
@@ -133,6 +135,9 @@ Env vars:
|
|
|
133
135
|
registerFeedbackCommands(program);
|
|
134
136
|
registerSkillsCommands(program);
|
|
135
137
|
registerClaudeHookCommand(program);
|
|
138
|
+
// `--intent` on a write command brackets it in its own agent run.
|
|
139
|
+
applyIntentOptions(program);
|
|
140
|
+
program.hook("preAction", (_root, actionCommand) => runUnderIntent(program, actionCommand));
|
|
136
141
|
// The root alone keeps excess arguments: an unknown top-level word arrives
|
|
137
142
|
// here as an operand, and the action below reports it as an unknown command
|
|
138
143
|
// (with the version hint) instead of Commander's "too many arguments".
|
package/package.json
CHANGED
package/skills/cnotes/SKILL.md
CHANGED
|
@@ -22,14 +22,14 @@ metadata:
|
|
|
22
22
|
9. **Close the run.** `cnotes operations end`.
|
|
23
23
|
10. **Link the result.** End your reply with the canvas link (see [Surfacing the canvas link](#surfacing-the-canvas-link)).
|
|
24
24
|
|
|
25
|
-
Reads, and a single metadata change (`notes update`), need no run.
|
|
25
|
+
Reads, and a single metadata change (`notes update`), need no run. A task that is ONE write needs no `begin`/`end` either: pass `--intent "<headline>"` on that command (see the fast path).
|
|
26
26
|
|
|
27
27
|
## Fast path: a routine canvas change
|
|
28
28
|
|
|
29
|
-
Adding cards, filling or tidying a section,
|
|
29
|
+
Adding cards, filling or tidying a section, linking two cards or removing one takes two rounds. Each extra call costs the user a model round trip, so batch, and send independent calls together.
|
|
30
30
|
|
|
31
|
-
1. **Read
|
|
32
|
-
2. **
|
|
31
|
+
1. **Read, in parallel:** `cnotes canvas read <canvasId>` (or `cnotes canvas read-section SECTION-n` when the request names one), plus `cnotes types show <Type>` for each type you will write.
|
|
32
|
+
2. **Make the change in one call, with `--intent`.** `--intent "<headline>"` opens an agent run for that one command and closes it after, so the change lands on the timeline as one revertable run with no `operations begin`/`end`. A removal gets a before snapshot automatically, so it can be undone. When a run is already open, the write joins it. To add cards, create and place them together:
|
|
33
33
|
```json
|
|
34
34
|
{"notes":[{"key":"A","type":"Insight","markdown":"# Reviews wait on one person\n..."},
|
|
35
35
|
{"key":"B","type":"Decision","markdown":"# Add a second reviewer\nRests on [@A: Reviews wait on one person](relationship:depends-on)."}],
|
|
@@ -37,10 +37,12 @@ Adding cards, filling or tidying a section, or linking two cards takes three ste
|
|
|
37
37
|
{"kind":"item","type":"note","noteId":"@A"},{"kind":"item","type":"note","noteId":"@B"}]},
|
|
38
38
|
"edges":[{"from":"@B","to":"@A","label":"depends on"}]}
|
|
39
39
|
```
|
|
40
|
-
`
|
|
41
|
-
|
|
40
|
+
Put the new notes in the spec's `notes` array (the `notes create` shape) and place each with `{"kind":"item","type":"note","noteId":"@<key>"}`; edges and anchors say `@<key>` too. Add `--into SECTION-n` to append to an existing frame. Each result comes back under `notes`, graded when the workspace grades: revise only notes below 4.
|
|
41
|
+
`cnotes canvas place <canvasId> --spec spec.json --intent "Added the reviewer bottleneck to the Q1 roadmap"`
|
|
42
|
+
Other single changes take the same flag: `cnotes canvas arrange <canvasId> --section SECTION-7 --intent "Tidied the Risks section"`, `cnotes canvas bulk-remove <canvasId> --nodes '["NOTE-4"]' --intent "Removed the duplicate risk card"`.
|
|
43
|
+
3. **Link the canvas** from the `Link:` line, in the same reply.
|
|
42
44
|
|
|
43
|
-
|
|
45
|
+
Several writes that belong together (a new canvas and its first layout, notes on two canvases) still go in one `operations begin`/`end` run, so the timeline shows one change. Open [references/canvas-layout.md](references/canvas-layout.md) only when the layout needs anchors, origins or several sections; the rules above and step 7 below cover the rest.
|
|
44
46
|
|
|
45
47
|
## Rules that hold on every task
|
|
46
48
|
|
|
@@ -26,6 +26,8 @@ When an AI agent is doing a **batch of work** — creating notes, modifying 2+ e
|
|
|
26
26
|
| Two or more writes that belong to one intent (multi-note batch, canvas build-out, multi-step refactor) | **Yes** |
|
|
27
27
|
| Canvas mutations done as part of an agent task | **Yes** — even one canvas command, if it's part of a larger batch |
|
|
28
28
|
|
|
29
|
+
**One write is its own run with `--intent`.** When the whole task is a single write command (`notes create`, `versions create`, `canvas create`, `place`, `arrange`, an `add-*`, `update-*`, `move-*` or `remove-*` canvas command), pass `--intent "<headline>"` on it instead of bracketing it: the CLI opens a run with that headline, runs the command under it, and closes the run even when the command fails. Removals (`bulk-remove`, `remove-node`, `remove-edge`) get a canvas run with a before snapshot, the same as `canvas agent-run wrap`, so they can be undone. When a run is already open, the write joins it and `--intent` is not used as a headline. `cnotes <command> --help` lists the flag on every command that takes it.
|
|
30
|
+
|
|
29
31
|
## The pattern
|
|
30
32
|
|
|
31
33
|
`cnotes operations begin` opens a run; **every subsequent cnotes write on this server attaches to it automatically**, across any shell, until you call `cnotes operations end`. No env var to export, no eval trick — the active run is persisted in `~/.cnotes/config.json` per server, so stateless callers (separate Bash invocations, disjoint tool calls) all attach correctly.
|