linkshell-cli 0.2.84 → 0.2.86
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/dist/cli/src/index.js +0 -0
- package/dist/cli/src/runtime/acp/agent-session.js +10 -2
- package/dist/cli/src/runtime/acp/agent-session.js.map +1 -1
- package/dist/cli/src/runtime/acp/agent-workspace.js +11 -2
- package/dist/cli/src/runtime/acp/agent-workspace.js.map +1 -1
- package/dist/cli/src/runtime/bridge-session.d.ts +1 -0
- package/dist/cli/src/runtime/bridge-session.js +111 -32
- package/dist/cli/src/runtime/bridge-session.js.map +1 -1
- package/dist/cli/tsconfig.tsbuildinfo +1 -1
- package/package.json +1 -1
- package/src/runtime/acp/agent-session.ts +12 -2
- package/src/runtime/acp/agent-workspace.ts +13 -2
- package/src/runtime/bridge-session.ts +128 -32
|
@@ -51,6 +51,7 @@ const HOOK_BODY_LIMIT = 256 * 1024;
|
|
|
51
51
|
const PERMISSION_REQUEST_TIMEOUT_MS = Number(
|
|
52
52
|
process.env.LINKSHELL_PERMISSION_TIMEOUT_MS ?? 5 * 60_000,
|
|
53
53
|
);
|
|
54
|
+
const LINKSHELL_PERMISSION_GUARD_MARKER = "LINKSHELL_PERMISSION_GUARD";
|
|
54
55
|
|
|
55
56
|
interface TerminalInstance {
|
|
56
57
|
id: string;
|
|
@@ -90,9 +91,81 @@ type HookPermissionChoice =
|
|
|
90
91
|
optionId?: string;
|
|
91
92
|
};
|
|
92
93
|
|
|
93
|
-
function isLinkShellHookEntry(entry: unknown, marker
|
|
94
|
-
|
|
95
|
-
|
|
94
|
+
function isLinkShellHookEntry(entry: unknown, marker?: string): boolean {
|
|
95
|
+
let raw = "";
|
|
96
|
+
try {
|
|
97
|
+
raw = JSON.stringify(entry);
|
|
98
|
+
} catch {
|
|
99
|
+
raw = String(entry);
|
|
100
|
+
}
|
|
101
|
+
return (
|
|
102
|
+
(marker ? raw.includes(`/hook?m=${marker}`) : false) ||
|
|
103
|
+
raw.includes("/hook?m=lsh-") ||
|
|
104
|
+
(raw.includes("/hook?m=") && raw.includes("LINKSHELL_ID"))
|
|
105
|
+
);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
function withLinkShellHookEntry<T>(
|
|
109
|
+
entries: unknown[] | undefined,
|
|
110
|
+
entry: T,
|
|
111
|
+
priority: "first" | "last",
|
|
112
|
+
): unknown[] {
|
|
113
|
+
const cleaned = (Array.isArray(entries) ? entries : []).filter((item) => !isLinkShellHookEntry(item));
|
|
114
|
+
return priority === "first" ? [entry, ...cleaned] : [...cleaned, entry];
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
function guardPermissionCommandForLinkShell(command: unknown): unknown {
|
|
118
|
+
if (typeof command !== "string") return command;
|
|
119
|
+
if (command.includes(LINKSHELL_PERMISSION_GUARD_MARKER)) return command;
|
|
120
|
+
return [
|
|
121
|
+
`case "\${LINKSHELL_ID:-}" in lsh-*) exit 0 ;; esac`,
|
|
122
|
+
`# ${LINKSHELL_PERMISSION_GUARD_MARKER}`,
|
|
123
|
+
command,
|
|
124
|
+
].join("\n");
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
function guardPermissionHookObjectForLinkShell(
|
|
128
|
+
hook: Record<string, unknown>,
|
|
129
|
+
): Record<string, unknown> {
|
|
130
|
+
if (isLinkShellHookEntry(hook)) return hook;
|
|
131
|
+
const next: Record<string, unknown> = { ...hook };
|
|
132
|
+
if (typeof next.command === "string") {
|
|
133
|
+
next.command = guardPermissionCommandForLinkShell(next.command);
|
|
134
|
+
}
|
|
135
|
+
if (typeof next.bash === "string") {
|
|
136
|
+
next.bash = guardPermissionCommandForLinkShell(next.bash);
|
|
137
|
+
}
|
|
138
|
+
return next;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
function guardPermissionHookEntryForLinkShell(entry: unknown): unknown {
|
|
142
|
+
if (isLinkShellHookEntry(entry)) return entry;
|
|
143
|
+
if (typeof entry === "string") return guardPermissionCommandForLinkShell(entry);
|
|
144
|
+
if (Array.isArray(entry)) return entry.map(guardPermissionHookEntryForLinkShell);
|
|
145
|
+
if (!entry || typeof entry !== "object") return entry;
|
|
146
|
+
|
|
147
|
+
const next = { ...(entry as Record<string, unknown>) };
|
|
148
|
+
if (Array.isArray(next.hooks)) {
|
|
149
|
+
next.hooks = next.hooks.map((hook) =>
|
|
150
|
+
hook && typeof hook === "object" && !Array.isArray(hook)
|
|
151
|
+
? guardPermissionHookObjectForLinkShell(hook as Record<string, unknown>)
|
|
152
|
+
: guardPermissionHookEntryForLinkShell(hook),
|
|
153
|
+
);
|
|
154
|
+
}
|
|
155
|
+
if (typeof next.command === "string" || typeof next.bash === "string") {
|
|
156
|
+
return guardPermissionHookObjectForLinkShell(next);
|
|
157
|
+
}
|
|
158
|
+
return next;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
function withBlockingLinkShellPermissionEntry<T>(
|
|
162
|
+
entries: unknown[] | undefined,
|
|
163
|
+
entry: T,
|
|
164
|
+
): unknown[] {
|
|
165
|
+
const cleaned = (Array.isArray(entries) ? entries : [])
|
|
166
|
+
.filter((item) => !isLinkShellHookEntry(item))
|
|
167
|
+
.map(guardPermissionHookEntryForLinkShell);
|
|
168
|
+
return [entry, ...cleaned];
|
|
96
169
|
}
|
|
97
170
|
|
|
98
171
|
function stringifyHookInput(value: unknown): string {
|
|
@@ -645,6 +718,7 @@ export class BridgeSession {
|
|
|
645
718
|
);
|
|
646
719
|
break;
|
|
647
720
|
}
|
|
721
|
+
if (envelope.type === "agent.prompt") this.refreshAgentPermissionHooks();
|
|
648
722
|
await this.agentSession.handleEnvelope(envelope);
|
|
649
723
|
break;
|
|
650
724
|
}
|
|
@@ -653,8 +727,7 @@ export class BridgeSession {
|
|
|
653
727
|
if (this.resolvePendingPermission(p.requestId, {
|
|
654
728
|
outcome: p.outcome,
|
|
655
729
|
optionId: p.optionId,
|
|
656
|
-
})) {
|
|
657
|
-
this.log(`agent permission response for hook ${p.requestId}: ${p.outcome}:${p.optionId ?? "default"}`);
|
|
730
|
+
}, "agent.permission.response")) {
|
|
658
731
|
break;
|
|
659
732
|
}
|
|
660
733
|
if (!this.agentSession) {
|
|
@@ -714,6 +787,7 @@ export class BridgeSession {
|
|
|
714
787
|
);
|
|
715
788
|
break;
|
|
716
789
|
}
|
|
790
|
+
if (envelope.type === "agent.v2.prompt") this.refreshAgentPermissionHooks();
|
|
717
791
|
await this.agentWorkspace.handleEnvelope(envelope);
|
|
718
792
|
break;
|
|
719
793
|
}
|
|
@@ -731,11 +805,7 @@ export class BridgeSession {
|
|
|
731
805
|
}
|
|
732
806
|
case "permission.decision": {
|
|
733
807
|
const p = envelope.payload as { requestId: string; decision: "allow" | "deny" };
|
|
734
|
-
|
|
735
|
-
this.log(`permission decision for ${p.requestId}: ${p.decision}`);
|
|
736
|
-
} else {
|
|
737
|
-
this.log(`no pending permission for ${p.requestId}`);
|
|
738
|
-
}
|
|
808
|
+
this.resolvePendingPermission(p.requestId, p.decision, "permission.decision");
|
|
739
809
|
break;
|
|
740
810
|
}
|
|
741
811
|
case "tunnel.request": {
|
|
@@ -1134,7 +1204,7 @@ export class BridgeSession {
|
|
|
1134
1204
|
const requestId = `pr-${Date.now()}-${Math.random().toString(36).slice(2, 8)}`;
|
|
1135
1205
|
const permissionSuggestions = hookPermissionSuggestions(event);
|
|
1136
1206
|
const timeout = setTimeout(() => {
|
|
1137
|
-
if (this.resolvePendingPermission(requestId, "deny")) {
|
|
1207
|
+
if (this.resolvePendingPermission(requestId, "deny", "permission.timeout")) {
|
|
1138
1208
|
this.log(`permission request ${requestId} timed out`);
|
|
1139
1209
|
this.sendPermissionSnapshot(terminalId, "thinking", "permission timed out");
|
|
1140
1210
|
}
|
|
@@ -1199,6 +1269,26 @@ export class BridgeSession {
|
|
|
1199
1269
|
return { server, port, configPath };
|
|
1200
1270
|
}
|
|
1201
1271
|
|
|
1272
|
+
private refreshAgentPermissionHooks(): void {
|
|
1273
|
+
const term = this.terminals.get(DEFAULT_TERMINAL_ID);
|
|
1274
|
+
if (!term?.hookPort) return;
|
|
1275
|
+
const marker = term.hookMarker;
|
|
1276
|
+
const curlCmd = `curl -s -X POST "http://127.0.0.1:${term.hookPort}/hook?m=${marker}&lid=$LINKSHELL_ID" -H 'Content-Type: application/json' --data-binary @-`;
|
|
1277
|
+
const agentProvider = normalizeAgentProvider(this.options.agentProvider ?? "codex");
|
|
1278
|
+
try {
|
|
1279
|
+
if (agentProvider === "claude") {
|
|
1280
|
+
this.setupClaudeHooks(DEFAULT_TERMINAL_ID, curlCmd, [], marker);
|
|
1281
|
+
} else {
|
|
1282
|
+
this.setupCodexHooks(DEFAULT_TERMINAL_ID, curlCmd, marker);
|
|
1283
|
+
if (agentProvider === "custom") {
|
|
1284
|
+
this.setupClaudeHooks(DEFAULT_TERMINAL_ID, curlCmd, [], marker);
|
|
1285
|
+
}
|
|
1286
|
+
}
|
|
1287
|
+
} catch (error) {
|
|
1288
|
+
this.log(`failed to refresh agent permission hooks: ${error instanceof Error ? error.message : String(error)}`);
|
|
1289
|
+
}
|
|
1290
|
+
}
|
|
1291
|
+
|
|
1202
1292
|
private setupClaudeHooks(terminalId: string, curlCmd: string, args: string[], marker: string): string {
|
|
1203
1293
|
// Write hooks to ~/.claude/settings.json — Claude Code reads hooks from here
|
|
1204
1294
|
const claudeDir = join(homedir(), ".claude");
|
|
@@ -1233,11 +1323,9 @@ export class BridgeSession {
|
|
|
1233
1323
|
// Append our entries to existing hooks (first remove stale linkshell entries)
|
|
1234
1324
|
const existingHooks = (existing.hooks ?? {}) as Record<string, unknown[]>;
|
|
1235
1325
|
for (const [eventName, entry] of Object.entries(hookEvents)) {
|
|
1236
|
-
|
|
1237
|
-
|
|
1238
|
-
|
|
1239
|
-
arr.push(entry);
|
|
1240
|
-
existingHooks[eventName] = arr;
|
|
1326
|
+
existingHooks[eventName] = eventName === "PermissionRequest"
|
|
1327
|
+
? withBlockingLinkShellPermissionEntry(existingHooks[eventName], entry)
|
|
1328
|
+
: withLinkShellHookEntry(existingHooks[eventName], entry, "last");
|
|
1241
1329
|
}
|
|
1242
1330
|
|
|
1243
1331
|
const merged = { ...existing, hooks: existingHooks };
|
|
@@ -1298,10 +1386,9 @@ export class BridgeSession {
|
|
|
1298
1386
|
try { existing = JSON.parse(readFileSync(hooksPath, "utf8")); } catch { /* doesn't exist yet */ }
|
|
1299
1387
|
const existingHooks = existing.hooks ?? {};
|
|
1300
1388
|
for (const [eventName, entry] of Object.entries(hookEvents)) {
|
|
1301
|
-
|
|
1302
|
-
|
|
1303
|
-
|
|
1304
|
-
existingHooks[eventName] = arr;
|
|
1389
|
+
existingHooks[eventName] = eventName === "PermissionRequest"
|
|
1390
|
+
? withBlockingLinkShellPermissionEntry(existingHooks[eventName], entry)
|
|
1391
|
+
: withLinkShellHookEntry(existingHooks[eventName], entry, "last");
|
|
1305
1392
|
}
|
|
1306
1393
|
|
|
1307
1394
|
writeFileSync(hooksPath, JSON.stringify({ ...existing, hooks: existingHooks }, null, 2));
|
|
@@ -1331,10 +1418,7 @@ export class BridgeSession {
|
|
|
1331
1418
|
|
|
1332
1419
|
const existingHooks = (existing.hooks ?? {}) as Record<string, unknown[]>;
|
|
1333
1420
|
for (const [eventName, entry] of Object.entries(hookEvents)) {
|
|
1334
|
-
|
|
1335
|
-
arr = arr.filter((e) => !isLinkShellHookEntry(e, marker));
|
|
1336
|
-
arr.push(entry);
|
|
1337
|
-
existingHooks[eventName] = arr;
|
|
1421
|
+
existingHooks[eventName] = withLinkShellHookEntry(existingHooks[eventName], entry, "last");
|
|
1338
1422
|
}
|
|
1339
1423
|
|
|
1340
1424
|
existing.hooks = existingHooks;
|
|
@@ -1366,10 +1450,7 @@ export class BridgeSession {
|
|
|
1366
1450
|
try { existing = JSON.parse(readFileSync(hooksPath, "utf8")); } catch { /* doesn't exist yet */ }
|
|
1367
1451
|
const existingHooks = existing.hooks ?? {};
|
|
1368
1452
|
for (const [eventName, entry] of Object.entries(hookEvents)) {
|
|
1369
|
-
|
|
1370
|
-
arr = arr.filter((e) => !isLinkShellHookEntry(e, marker));
|
|
1371
|
-
arr.push(entry);
|
|
1372
|
-
existingHooks[eventName] = arr;
|
|
1453
|
+
existingHooks[eventName] = withLinkShellHookEntry(existingHooks[eventName], entry, "last");
|
|
1373
1454
|
}
|
|
1374
1455
|
|
|
1375
1456
|
writeFileSync(hooksPath, JSON.stringify({ version: 1, hooks: existingHooks }, null, 2));
|
|
@@ -1626,7 +1707,7 @@ export class BridgeSession {
|
|
|
1626
1707
|
|
|
1627
1708
|
/** Auto-resolve a single pending permission (user acted in terminal) */
|
|
1628
1709
|
private autoResolvePending(requestId: string): void {
|
|
1629
|
-
if (this.resolvePendingPermission(requestId, "allow")) {
|
|
1710
|
+
if (this.resolvePendingPermission(requestId, "allow", "terminal.auto")) {
|
|
1630
1711
|
this.log(`auto-resolved pending permission ${requestId} (user acted in terminal)`);
|
|
1631
1712
|
}
|
|
1632
1713
|
}
|
|
@@ -1636,15 +1717,24 @@ export class BridgeSession {
|
|
|
1636
1717
|
const stack = this.permissionStacks.get(terminalId);
|
|
1637
1718
|
if (!stack) return;
|
|
1638
1719
|
for (const entry of [...stack]) {
|
|
1639
|
-
if (this.resolvePendingPermission(entry.requestId, "deny")) {
|
|
1720
|
+
if (this.resolvePendingPermission(entry.requestId, "deny", "terminal.drain")) {
|
|
1640
1721
|
this.log(`drained pending permission ${entry.requestId}`);
|
|
1641
1722
|
}
|
|
1642
1723
|
}
|
|
1643
1724
|
}
|
|
1644
1725
|
|
|
1645
|
-
private resolvePendingPermission(
|
|
1726
|
+
private resolvePendingPermission(
|
|
1727
|
+
requestId: string,
|
|
1728
|
+
choice: HookPermissionChoice,
|
|
1729
|
+
source = "unknown",
|
|
1730
|
+
): boolean {
|
|
1646
1731
|
const pending = this.pendingPermissions.get(requestId);
|
|
1647
|
-
|
|
1732
|
+
const outcome = typeof choice === "string" ? choice : choice.outcome;
|
|
1733
|
+
const optionId = typeof choice === "string" ? undefined : choice.optionId;
|
|
1734
|
+
if (!pending) {
|
|
1735
|
+
this.log(`no pending permission for ${requestId} via ${source}: ${outcome}:${optionId ?? "default"}`);
|
|
1736
|
+
return false;
|
|
1737
|
+
}
|
|
1648
1738
|
this.pendingPermissions.delete(requestId);
|
|
1649
1739
|
clearTimeout(pending.timeout);
|
|
1650
1740
|
pending.resolve(this.formatHookPermissionDecision(pending, choice));
|
|
@@ -1655,6 +1745,12 @@ export class BridgeSession {
|
|
|
1655
1745
|
if (idx >= 0) stack.splice(idx, 1);
|
|
1656
1746
|
if (stack.length === 0) this.permissionStacks.delete(pending.terminalId);
|
|
1657
1747
|
}
|
|
1748
|
+
this.log(`resolved permission ${requestId} via ${source}: ${outcome}:${optionId ?? "default"}`);
|
|
1749
|
+
this.sendPermissionSnapshot(
|
|
1750
|
+
pending.terminalId,
|
|
1751
|
+
"thinking",
|
|
1752
|
+
outcome === "allow" ? "permission allowed" : "permission denied",
|
|
1753
|
+
);
|
|
1658
1754
|
return true;
|
|
1659
1755
|
}
|
|
1660
1756
|
|