@causyne/agent 1.20.79 → 1.20.81
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/package.json +1 -1
- package/src/cliMode.js +26 -1
- package/src/commands.js +10 -8
- package/src/tui/completion.js +35 -7
- package/src/tui/prompt.js +27 -18
- package/src/tui/workspaceSwitch.js +52 -0
- package/src/tui/workspaceSwitch.ts +79 -0
package/package.json
CHANGED
package/src/cliMode.js
CHANGED
|
@@ -4,6 +4,7 @@ import { getProcessEntitlementGate } from './licensing/entitlements.js';
|
|
|
4
4
|
import { loadConfig, saveConfig } from './config.js';
|
|
5
5
|
import readline from 'readline';
|
|
6
6
|
import { ChatPrompt, isInteractive } from './tui/prompt.js';
|
|
7
|
+
import { switchChatWorkspace } from './tui/workspaceSwitch.js';
|
|
7
8
|
import { handleInput, listCommands, isCommandInput } from './commands.js';
|
|
8
9
|
import { CodeStreamRenderer as ChatRenderer } from './tui/codeStreamRenderer.js';
|
|
9
10
|
import { paint, bold, dim, badge, GLYPHS, terminalWidth } from './tui/theme.js';
|
|
@@ -304,7 +305,7 @@ export async function chatMode({ home = false } = {}) {
|
|
|
304
305
|
// .agents/rules.json.
|
|
305
306
|
const conn = new LocalCLIConnection();
|
|
306
307
|
const taskId = `cli_chat_${Date.now()}`;
|
|
307
|
-
|
|
308
|
+
let loop = new AgentLoop(conn, taskId, null);
|
|
308
309
|
|
|
309
310
|
// The context the CLI never showed: which model is about to spend the user's
|
|
310
311
|
// money, in which autonomy mode, over which paths. All of it was already
|
|
@@ -458,7 +459,31 @@ export async function chatMode({ home = false } = {}) {
|
|
|
458
459
|
// A leading slash is a command, not a task. handleInput returns null for
|
|
459
460
|
// anything else, which is what sends the line to the agent — so a prompt
|
|
460
461
|
// that merely mentions a path like /usr/local is still a task.
|
|
462
|
+
const previousWorkspaceRoot = loop.workspaceRoot;
|
|
461
463
|
const commandResult = await handleInput(task, { surface: 'terminal', loop, prompt });
|
|
464
|
+
if (commandResult?.effect === 'workspace-changed') {
|
|
465
|
+
try {
|
|
466
|
+
// Fresh sandbox AND memory: an old AgentLoop cannot be repointed by
|
|
467
|
+
// changing process.cwd() or assigning only loop.workspaceContext.
|
|
468
|
+
const nextRoot = commandResult.workspaceRoot;
|
|
469
|
+
loop = switchChatWorkspace({
|
|
470
|
+
workspaceRoot: nextRoot,
|
|
471
|
+
createLoop: (workspaceRoot) => new AgentLoop(
|
|
472
|
+
conn, `cli_chat_${Date.now()}`, null, { workspaceRoot }
|
|
473
|
+
),
|
|
474
|
+
prompt, surface, renderer: conn.renderer, headerInfo,
|
|
475
|
+
approvalHandler: makeApprovalHandler(conn, prompt),
|
|
476
|
+
decisionHandler: makeDecisionHandler(conn, prompt)
|
|
477
|
+
});
|
|
478
|
+
conn.renderer.write(`\n Workspace switched to: ${nextRoot}\n`);
|
|
479
|
+
} catch (error) {
|
|
480
|
+
// Roll back process cwd and prompt state if creation/rebinding fails.
|
|
481
|
+
try { process.chdir(previousWorkspaceRoot); } catch { /* report below */ }
|
|
482
|
+
try { prompt?.setWorkingRoot(previousWorkspaceRoot); } catch { /* report below */ }
|
|
483
|
+
conn.renderer.write(`\n Workspace switch failed: ${error.message}\n`);
|
|
484
|
+
}
|
|
485
|
+
continue;
|
|
486
|
+
}
|
|
462
487
|
if (commandResult) {
|
|
463
488
|
// Route command output through the renderer, not console.log.
|
|
464
489
|
//
|
package/src/commands.js
CHANGED
|
@@ -154,20 +154,22 @@ export const COMMANDS = [
|
|
|
154
154
|
if (!stat.isDirectory()) {
|
|
155
155
|
return fail(`Not a directory: ${resolved}`);
|
|
156
156
|
}
|
|
157
|
+
// Do not mutate the existing AgentLoop: its workspaceRoot, Sandbox,
|
|
158
|
+
// MemoryManager and loaded session belong to the previous workspace.
|
|
159
|
+
// The chat controller rebuilds the loop after this explicit command.
|
|
160
|
+
let canonicalRoot;
|
|
157
161
|
try {
|
|
158
|
-
|
|
162
|
+
canonicalRoot = fs.realpathSync(resolved);
|
|
163
|
+
process.chdir(canonicalRoot);
|
|
159
164
|
} catch (err) {
|
|
160
165
|
return fail(`Cannot switch to ${resolved}: ${err.message}`);
|
|
161
166
|
}
|
|
162
167
|
if (ctx.prompt && typeof ctx.prompt.setWorkingRoot === 'function') {
|
|
163
|
-
ctx.prompt.setWorkingRoot(
|
|
168
|
+
ctx.prompt.setWorkingRoot(canonicalRoot);
|
|
164
169
|
}
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
} catch (e) {}
|
|
169
|
-
}
|
|
170
|
-
return ok(`Working directory changed to: ${resolved}`);
|
|
170
|
+
return ok(`Working directory changed to: ${canonicalRoot}`, {
|
|
171
|
+
effect: 'workspace-changed', workspaceRoot: canonicalRoot
|
|
172
|
+
});
|
|
171
173
|
}
|
|
172
174
|
},
|
|
173
175
|
|
package/src/tui/completion.js
CHANGED
|
@@ -269,9 +269,10 @@ export class FileIndex {
|
|
|
269
269
|
async walk() {
|
|
270
270
|
const found = [];
|
|
271
271
|
const queue = [{ dir: this.root, depth: 0 }];
|
|
272
|
+
let next = 0;
|
|
272
273
|
|
|
273
|
-
while (queue.length
|
|
274
|
-
const { dir, depth } = queue
|
|
274
|
+
while (next < queue.length && found.length < MAX_INDEX_ENTRIES) {
|
|
275
|
+
const { dir, depth } = queue[next++];
|
|
275
276
|
let listing;
|
|
276
277
|
try {
|
|
277
278
|
listing = await fsp.readdir(dir, { withFileTypes: true });
|
|
@@ -305,6 +306,23 @@ export class FileIndex {
|
|
|
305
306
|
}
|
|
306
307
|
}
|
|
307
308
|
|
|
309
|
+
/**
|
|
310
|
+
* Resolve both the authorized root and candidate to physical paths before
|
|
311
|
+
* touching a directory. Lexical path.resolve() alone follows symlinks outside
|
|
312
|
+
* the workspace and can leak private filenames through @ completion.
|
|
313
|
+
*/
|
|
314
|
+
function isPhysicalChild(root, candidate) {
|
|
315
|
+
try {
|
|
316
|
+
const base = fs.realpathSync(root);
|
|
317
|
+
const target = fs.realpathSync(candidate);
|
|
318
|
+
const relative = path.relative(base, target);
|
|
319
|
+
return relative === '' || (relative !== '..' &&
|
|
320
|
+
!relative.startsWith(`..${path.sep}`) && !path.isAbsolute(relative));
|
|
321
|
+
} catch {
|
|
322
|
+
return false;
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
|
|
308
326
|
/** Instant, shell-style completion of one directory level. */
|
|
309
327
|
function directoryItems(root, partial) {
|
|
310
328
|
const slash = partial.lastIndexOf('/');
|
|
@@ -313,7 +331,8 @@ function directoryItems(root, partial) {
|
|
|
313
331
|
|
|
314
332
|
// Never offer paths outside the workspace root, whatever the user typed.
|
|
315
333
|
const relativeToRoot = path.relative(root, target);
|
|
316
|
-
if (relativeToRoot
|
|
334
|
+
if (relativeToRoot === '..' || relativeToRoot.startsWith(`..${path.sep}`) ||
|
|
335
|
+
path.isAbsolute(relativeToRoot) || !isPhysicalChild(root, target)) return [];
|
|
317
336
|
|
|
318
337
|
let listing;
|
|
319
338
|
try {
|
|
@@ -325,14 +344,23 @@ function directoryItems(root, partial) {
|
|
|
325
344
|
const items = [];
|
|
326
345
|
for (const entry of listing) {
|
|
327
346
|
if (entry.name.startsWith('.')) continue;
|
|
328
|
-
|
|
329
|
-
|
|
347
|
+
const candidate = path.join(target, entry.name);
|
|
348
|
+
// A lexical entry can be a symlink to a private directory. Do not offer
|
|
349
|
+
// any symlink whose physical target leaves the active workspace.
|
|
350
|
+
if (entry.isSymbolicLink() && !isPhysicalChild(root, candidate)) continue;
|
|
351
|
+
let isDirectory = entry.isDirectory();
|
|
352
|
+
if (entry.isSymbolicLink()) {
|
|
353
|
+
try { isDirectory = fs.statSync(candidate).isDirectory(); }
|
|
354
|
+
catch { continue; }
|
|
355
|
+
}
|
|
356
|
+
if (isDirectory && SKIP_DIRECTORIES.has(entry.name)) continue;
|
|
357
|
+
const value = dirPart + entry.name + (isDirectory ? '/' : '');
|
|
330
358
|
if (!isTerminalSafe(value)) continue;
|
|
331
359
|
items.push({
|
|
332
360
|
value,
|
|
333
361
|
match: value,
|
|
334
362
|
label: value,
|
|
335
|
-
description:
|
|
363
|
+
description: isDirectory ? 'directory' : ''
|
|
336
364
|
});
|
|
337
365
|
}
|
|
338
366
|
return items;
|
|
@@ -449,7 +477,7 @@ export function suggest(text, cursor, options = {}) {
|
|
|
449
477
|
if (focusDir) {
|
|
450
478
|
const focusTarget = path.resolve(root, focusDir);
|
|
451
479
|
try {
|
|
452
|
-
if (
|
|
480
|
+
if (isPhysicalChild(root, focusTarget) && fs.statSync(focusTarget).isDirectory()) {
|
|
453
481
|
const focusedItems = directoryItems(focusTarget, trigger.query).map((item) => ({
|
|
454
482
|
...item,
|
|
455
483
|
value: `${focusDir}/${item.value}`,
|
package/src/tui/prompt.js
CHANGED
|
@@ -785,15 +785,13 @@ export class ChatPrompt {
|
|
|
785
785
|
// Control commands take priority over busy turns so the user is never locked out.
|
|
786
786
|
// Commands like /stop, /new, /model, /session, /sessions, /continuum, /clear can interrupt
|
|
787
787
|
// a stalled/long-running turn and execute immediately.
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
text === '/clear'
|
|
796
|
-
);
|
|
788
|
+
// Match a COMPLETE command token. Prefix tests made unrelated
|
|
789
|
+
// commands such as /newsletter interrupt an active agent turn.
|
|
790
|
+
const commandToken = text.trim().split(/\s+/, 1)[0].toLowerCase();
|
|
791
|
+
const isControlCommand = this.isCommand(text) && new Set([
|
|
792
|
+
'/stop', '/interrupt', '/new', '/model', '/models',
|
|
793
|
+
'/session', '/sessions', '/continuum', '/clear'
|
|
794
|
+
]).has(commandToken);
|
|
797
795
|
|
|
798
796
|
// Immediate abort signals interrupt the running turn directly without queuing as a future task.
|
|
799
797
|
if ((text === '/stop' || text === '/interrupt') && this.busy) {
|
|
@@ -1265,10 +1263,12 @@ export class ChatPrompt {
|
|
|
1265
1263
|
}
|
|
1266
1264
|
|
|
1267
1265
|
cursorOffset() {
|
|
1268
|
-
|
|
1269
|
-
|
|
1270
|
-
|
|
1271
|
-
|
|
1266
|
+
// The editor stores Unicode code points; String.slice() in the completion
|
|
1267
|
+
// provider expects UTF-16 code units. Count units at this API boundary.
|
|
1268
|
+
const before = this.editor.lines.slice(0, this.editor.line)
|
|
1269
|
+
.reduce((total, line) => total + line.join('').length + 1, 0);
|
|
1270
|
+
return before + this.editor.lines[this.editor.line]
|
|
1271
|
+
.slice(0, this.editor.col).join('').length;
|
|
1272
1272
|
}
|
|
1273
1273
|
|
|
1274
1274
|
openCompletion(force = false) {
|
|
@@ -1465,15 +1465,24 @@ export class ChatPrompt {
|
|
|
1465
1465
|
}
|
|
1466
1466
|
|
|
1467
1467
|
setCursorOffset(offset) {
|
|
1468
|
-
|
|
1468
|
+
// Convert completion's UTF-16 offset back into the editor's code-point
|
|
1469
|
+
// column, never splitting a surrogate pair or moving to the wrong row.
|
|
1470
|
+
let remaining = Math.max(0, Number.isFinite(offset) ? offset : 0);
|
|
1469
1471
|
for (let index = 0; index < this.editor.lines.length; index++) {
|
|
1470
|
-
const
|
|
1471
|
-
|
|
1472
|
+
const line = this.editor.lines[index];
|
|
1473
|
+
const units = line.join('').length;
|
|
1474
|
+
if (remaining <= units || index === this.editor.lines.length - 1) {
|
|
1475
|
+
let column = 0;
|
|
1476
|
+
let consumed = 0;
|
|
1477
|
+
while (column < line.length && consumed + line[column].length <= remaining) {
|
|
1478
|
+
consumed += line[column].length;
|
|
1479
|
+
column++;
|
|
1480
|
+
}
|
|
1472
1481
|
this.editor.line = index;
|
|
1473
|
-
this.editor.col =
|
|
1482
|
+
this.editor.col = column;
|
|
1474
1483
|
return;
|
|
1475
1484
|
}
|
|
1476
|
-
remaining -=
|
|
1485
|
+
remaining -= units + 1;
|
|
1477
1486
|
}
|
|
1478
1487
|
}
|
|
1479
1488
|
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Rebind a chat session to a different workspace without mutating the previous
|
|
3
|
+
* agent's sandbox or mixing old workspace memory into the new session.
|
|
4
|
+
*
|
|
5
|
+
* This module owns no disk changes or CLI command parsing. createLoop() is the
|
|
6
|
+
* existing AgentLoop constructor, injected by cliMode for deterministic tests.
|
|
7
|
+
*/
|
|
8
|
+
export function switchChatWorkspace({ workspaceRoot, createLoop, prompt = null, surface = null, renderer = null, headerInfo = null, approvalHandler, decisionHandler }) {
|
|
9
|
+
if (typeof workspaceRoot !== 'string' || !workspaceRoot) {
|
|
10
|
+
throw new TypeError('A nonempty workspace root is required');
|
|
11
|
+
}
|
|
12
|
+
if (typeof createLoop !== 'function') {
|
|
13
|
+
throw new TypeError('createLoop must construct a new AgentLoop');
|
|
14
|
+
}
|
|
15
|
+
// Build the replacement before updating any displayed state. If construction
|
|
16
|
+
// fails, the previous loop and prompt remain intact for caller rollback.
|
|
17
|
+
const replacement = createLoop(workspaceRoot);
|
|
18
|
+
if (!replacement || replacement.workspaceRoot !== workspaceRoot) {
|
|
19
|
+
throw new Error('New agent loop did not adopt the requested workspace root');
|
|
20
|
+
}
|
|
21
|
+
const paths = replacement.sandbox?.workspacePaths;
|
|
22
|
+
if (!replacement.sandbox?.grantAllAccess &&
|
|
23
|
+
(!Array.isArray(paths) || !paths.includes(workspaceRoot))) {
|
|
24
|
+
throw new Error('New agent sandbox did not adopt the requested workspace root');
|
|
25
|
+
}
|
|
26
|
+
replacement.waitForApproval = approvalHandler;
|
|
27
|
+
replacement.waitForDecision = decisionHandler;
|
|
28
|
+
if (prompt) {
|
|
29
|
+
prompt.setWorkingRoot(workspaceRoot);
|
|
30
|
+
prompt.messageActionLoop = replacement;
|
|
31
|
+
// A workspace switch is also a change of task context. Do not transfer
|
|
32
|
+
// queued attachments or active skills from the previous workspace.
|
|
33
|
+
prompt.pendingAttachments = [];
|
|
34
|
+
prompt.completion = null;
|
|
35
|
+
if (typeof prompt.scheduleDraw === 'function')
|
|
36
|
+
prompt.scheduleDraw();
|
|
37
|
+
}
|
|
38
|
+
if (headerInfo) {
|
|
39
|
+
headerInfo.cwd = workspaceRoot;
|
|
40
|
+
headerInfo.workspace = replacement.sandbox?.grantAllAccess
|
|
41
|
+
? 'ALL ACCESS'
|
|
42
|
+
: replacement.sandbox?.workspacePaths?.join(', ') ?? '';
|
|
43
|
+
if (renderer?.status?.setSession)
|
|
44
|
+
renderer.status.setSession(headerInfo);
|
|
45
|
+
if (surface?.setSessionInfo)
|
|
46
|
+
surface.setSessionInfo({
|
|
47
|
+
...headerInfo,
|
|
48
|
+
activeSkill: null
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
return replacement;
|
|
52
|
+
}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
export interface SwitchChatWorkspaceOptions<TLoop = any> {
|
|
2
|
+
workspaceRoot: string;
|
|
3
|
+
createLoop: (workspaceRoot: string) => TLoop;
|
|
4
|
+
prompt?: any;
|
|
5
|
+
surface?: any;
|
|
6
|
+
renderer?: any;
|
|
7
|
+
headerInfo?: any;
|
|
8
|
+
approvalHandler?: any;
|
|
9
|
+
decisionHandler?: any;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Rebind a chat session to a different workspace without mutating the previous
|
|
14
|
+
* agent's sandbox or mixing old workspace memory into the new session.
|
|
15
|
+
*
|
|
16
|
+
* This module owns no disk changes or CLI command parsing. createLoop() is the
|
|
17
|
+
* existing AgentLoop constructor, injected by cliMode for deterministic tests.
|
|
18
|
+
*/
|
|
19
|
+
export function switchChatWorkspace<TLoop extends {
|
|
20
|
+
workspaceRoot?: string;
|
|
21
|
+
sandbox?: { grantAllAccess?: boolean; workspacePaths?: string[] };
|
|
22
|
+
waitForApproval?: any;
|
|
23
|
+
waitForDecision?: any;
|
|
24
|
+
} = any>({
|
|
25
|
+
workspaceRoot,
|
|
26
|
+
createLoop,
|
|
27
|
+
prompt = null,
|
|
28
|
+
surface = null,
|
|
29
|
+
renderer = null,
|
|
30
|
+
headerInfo = null,
|
|
31
|
+
approvalHandler,
|
|
32
|
+
decisionHandler
|
|
33
|
+
}: SwitchChatWorkspaceOptions<TLoop>): TLoop {
|
|
34
|
+
if (typeof workspaceRoot !== 'string' || !workspaceRoot) {
|
|
35
|
+
throw new TypeError('A nonempty workspace root is required');
|
|
36
|
+
}
|
|
37
|
+
if (typeof createLoop !== 'function') {
|
|
38
|
+
throw new TypeError('createLoop must construct a new AgentLoop');
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// Build the replacement before updating any displayed state. If construction
|
|
42
|
+
// fails, the previous loop and prompt remain intact for caller rollback.
|
|
43
|
+
const replacement = createLoop(workspaceRoot);
|
|
44
|
+
if (!replacement || replacement.workspaceRoot !== workspaceRoot) {
|
|
45
|
+
throw new Error('New agent loop did not adopt the requested workspace root');
|
|
46
|
+
}
|
|
47
|
+
const paths = replacement.sandbox?.workspacePaths;
|
|
48
|
+
if (!replacement.sandbox?.grantAllAccess &&
|
|
49
|
+
(!Array.isArray(paths) || !paths.includes(workspaceRoot))) {
|
|
50
|
+
throw new Error('New agent sandbox did not adopt the requested workspace root');
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
replacement.waitForApproval = approvalHandler;
|
|
54
|
+
replacement.waitForDecision = decisionHandler;
|
|
55
|
+
|
|
56
|
+
if (prompt) {
|
|
57
|
+
prompt.setWorkingRoot(workspaceRoot);
|
|
58
|
+
prompt.messageActionLoop = replacement;
|
|
59
|
+
// A workspace switch is also a change of task context. Do not transfer
|
|
60
|
+
// queued attachments or active skills from the previous workspace.
|
|
61
|
+
prompt.pendingAttachments = [];
|
|
62
|
+
prompt.completion = null;
|
|
63
|
+
if (typeof prompt.scheduleDraw === 'function') prompt.scheduleDraw();
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
if (headerInfo) {
|
|
67
|
+
headerInfo.cwd = workspaceRoot;
|
|
68
|
+
headerInfo.workspace = replacement.sandbox?.grantAllAccess
|
|
69
|
+
? 'ALL ACCESS'
|
|
70
|
+
: replacement.sandbox?.workspacePaths?.join(', ') ?? '';
|
|
71
|
+
if (renderer?.status?.setSession) renderer.status.setSession(headerInfo);
|
|
72
|
+
if (surface?.setSessionInfo) surface.setSessionInfo({
|
|
73
|
+
...headerInfo,
|
|
74
|
+
activeSkill: null
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
return replacement;
|
|
79
|
+
}
|