@orgloop/agentctl 1.0.1 → 1.2.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,66 @@
1
+ import * as fs from "node:fs/promises";
2
+ /**
3
+ * Read the first N lines of a file by reading only `maxBytes` from the start.
4
+ * Avoids allocating the entire file into memory for large JSONL files.
5
+ *
6
+ * @param filePath - Path to the file
7
+ * @param maxLines - Maximum number of lines to return
8
+ * @param maxBytes - Maximum bytes to read from the start (default 8192)
9
+ * @returns Array of complete lines (up to maxLines)
10
+ */
11
+ export async function readHead(filePath, maxLines, maxBytes = 8192) {
12
+ const fh = await fs.open(filePath, "r");
13
+ try {
14
+ const stat = await fh.stat();
15
+ const bytesToRead = Math.min(maxBytes, stat.size);
16
+ if (bytesToRead === 0)
17
+ return [];
18
+ const buf = Buffer.alloc(bytesToRead);
19
+ const { bytesRead } = await fh.read(buf, 0, bytesToRead, 0);
20
+ if (bytesRead === 0)
21
+ return [];
22
+ const text = buf.subarray(0, bytesRead).toString("utf-8");
23
+ const lines = text.split("\n");
24
+ // If we didn't read the whole file, the last chunk may be incomplete — drop it
25
+ if (bytesRead < stat.size) {
26
+ lines.pop();
27
+ }
28
+ return lines.filter((l) => l.length > 0).slice(0, maxLines);
29
+ }
30
+ finally {
31
+ await fh.close();
32
+ }
33
+ }
34
+ /**
35
+ * Read the last N lines of a file by reading only `maxBytes` from the end.
36
+ * Avoids allocating the entire file into memory for large JSONL files.
37
+ *
38
+ * @param filePath - Path to the file
39
+ * @param maxLines - Maximum number of lines to return
40
+ * @param maxBytes - Maximum bytes to read from the end (default 65536)
41
+ * @returns Array of complete lines (up to maxLines, in order)
42
+ */
43
+ export async function readTail(filePath, maxLines, maxBytes = 65536) {
44
+ const fh = await fs.open(filePath, "r");
45
+ try {
46
+ const stat = await fh.stat();
47
+ if (stat.size === 0)
48
+ return [];
49
+ const bytesToRead = Math.min(maxBytes, stat.size);
50
+ const offset = Math.max(0, stat.size - bytesToRead);
51
+ const buf = Buffer.alloc(bytesToRead);
52
+ const { bytesRead } = await fh.read(buf, 0, bytesToRead, offset);
53
+ if (bytesRead === 0)
54
+ return [];
55
+ const text = buf.subarray(0, bytesRead).toString("utf-8");
56
+ const lines = text.split("\n");
57
+ // If we didn't start from the beginning, the first chunk may be a partial line — drop it
58
+ if (offset > 0) {
59
+ lines.shift();
60
+ }
61
+ return lines.filter((l) => l.length > 0).slice(-maxLines);
62
+ }
63
+ finally {
64
+ await fh.close();
65
+ }
66
+ }
@@ -22,3 +22,25 @@ export declare function createWorktree(opts: WorktreeCreateOpts): Promise<Worktr
22
22
  * Remove a git worktree.
23
23
  */
24
24
  export declare function removeWorktree(repo: string, worktreePath: string): Promise<void>;
25
+ /** Info about an existing worktree from `git worktree list` */
26
+ export interface WorktreeListEntry {
27
+ path: string;
28
+ branch: string;
29
+ head: string;
30
+ /** Whether this is the bare/main worktree */
31
+ bare: boolean;
32
+ }
33
+ /**
34
+ * List all git worktrees for a repo.
35
+ * Parses `git worktree list --porcelain` output.
36
+ */
37
+ export declare function listWorktrees(repo: string): Promise<WorktreeListEntry[]>;
38
+ /**
39
+ * Remove a worktree and optionally delete its branch.
40
+ */
41
+ export declare function cleanWorktree(repo: string, worktreePath: string, opts?: {
42
+ deleteBranch?: boolean;
43
+ }): Promise<{
44
+ removedPath: string;
45
+ deletedBranch?: string;
46
+ }>;
package/dist/worktree.js CHANGED
@@ -63,3 +63,71 @@ export async function removeWorktree(repo, worktreePath) {
63
63
  cwd: repoResolved,
64
64
  });
65
65
  }
66
+ /**
67
+ * List all git worktrees for a repo.
68
+ * Parses `git worktree list --porcelain` output.
69
+ */
70
+ export async function listWorktrees(repo) {
71
+ const repoResolved = path.resolve(repo);
72
+ const { stdout } = await execFileAsync("git", ["worktree", "list", "--porcelain"], { cwd: repoResolved });
73
+ const entries = [];
74
+ let current = {};
75
+ for (const line of stdout.split("\n")) {
76
+ if (line.startsWith("worktree ")) {
77
+ if (current.path)
78
+ entries.push(current);
79
+ current = { path: line.replace("worktree ", ""), bare: false };
80
+ }
81
+ else if (line.startsWith("HEAD ")) {
82
+ current.head = line.replace("HEAD ", "");
83
+ }
84
+ else if (line.startsWith("branch ")) {
85
+ current.branch = line.replace("branch refs/heads/", "");
86
+ }
87
+ else if (line === "bare") {
88
+ current.bare = true;
89
+ }
90
+ else if (line === "" && current.path) {
91
+ // End of entry
92
+ }
93
+ }
94
+ if (current.path)
95
+ entries.push(current);
96
+ return entries;
97
+ }
98
+ /**
99
+ * Remove a worktree and optionally delete its branch.
100
+ */
101
+ export async function cleanWorktree(repo, worktreePath, opts) {
102
+ const repoResolved = path.resolve(repo);
103
+ // Get the branch name before removing
104
+ let branch;
105
+ if (opts?.deleteBranch) {
106
+ try {
107
+ const { stdout } = await execFileAsync("git", ["rev-parse", "--abbrev-ref", "HEAD"], { cwd: worktreePath });
108
+ branch = stdout.trim();
109
+ }
110
+ catch {
111
+ // Worktree might be broken — still try to remove
112
+ }
113
+ }
114
+ await execFileAsync("git", ["worktree", "remove", "--force", worktreePath], {
115
+ cwd: repoResolved,
116
+ });
117
+ const result = {
118
+ removedPath: worktreePath,
119
+ };
120
+ // Delete the branch if requested
121
+ if (branch && opts?.deleteBranch) {
122
+ try {
123
+ await execFileAsync("git", ["branch", "-D", branch], {
124
+ cwd: repoResolved,
125
+ });
126
+ result.deletedBranch = branch;
127
+ }
128
+ catch {
129
+ // Branch might already be gone
130
+ }
131
+ }
132
+ return result;
133
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@orgloop/agentctl",
3
- "version": "1.0.1",
3
+ "version": "1.2.0",
4
4
  "description": "Universal agent supervision interface — monitor and control AI coding agents from a single CLI",
5
5
  "type": "module",
6
6
  "bin": {
@@ -49,7 +49,8 @@
49
49
  "commander": "^14.0.3",
50
50
  "tsx": "^4.21.0",
51
51
  "typescript": "^5.9.3",
52
- "vitest": "^4.0.18"
52
+ "vitest": "^4.0.18",
53
+ "yaml": "^2.8.2"
53
54
  },
54
55
  "devDependencies": {
55
56
  "@biomejs/biome": "^2.4.2"