@celljs/ta-harness 4.8.0 → 4.9.1

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.
Files changed (146) hide show
  1. package/README.en.md +1 -1
  2. package/README.md +1 -1
  3. package/esm/node/index.d.ts +1 -0
  4. package/esm/node/index.js +1 -0
  5. package/esm/node/package-archive.d.ts +38 -0
  6. package/esm/node/package-archive.js +258 -0
  7. package/esm/node/serve-state.d.ts +8 -0
  8. package/esm/node/serve-state.js +36 -0
  9. package/esm/node/ta-cli.d.ts +2 -0
  10. package/esm/node/ta-cli.js +45 -4
  11. package/esm/node/ta-packages.d.ts +1 -0
  12. package/esm/node/ta-packages.js +15 -1
  13. package/lib/node/index.d.ts +1 -0
  14. package/lib/node/index.js +1 -0
  15. package/lib/node/package-archive.d.ts +39 -0
  16. package/lib/node/package-archive.js +261 -0
  17. package/lib/node/serve-state.d.ts +8 -0
  18. package/lib/node/serve-state.js +36 -0
  19. package/lib/node/ta-cli.d.ts +2 -0
  20. package/lib/node/ta-cli.js +45 -4
  21. package/lib/node/ta-packages.d.ts +1 -0
  22. package/lib/node/ta-packages.js +15 -1
  23. package/package.json +6 -5
  24. package/esm/node/brand-extension.js.map +0 -1
  25. package/esm/node/bug-report.js.map +0 -1
  26. package/esm/node/dino-logo.js.map +0 -1
  27. package/esm/node/exec-extension.js.map +0 -1
  28. package/esm/node/exec-mcp-transport.js.map +0 -1
  29. package/esm/node/exec-operations.js.map +0 -1
  30. package/esm/node/extensions.js.map +0 -1
  31. package/esm/node/glob-util.js.map +0 -1
  32. package/esm/node/index.js.map +0 -1
  33. package/esm/node/pi-types.js.map +0 -1
  34. package/esm/node/remote-context-files.js.map +0 -1
  35. package/esm/node/serve-state.js.map +0 -1
  36. package/esm/node/session-lock.js.map +0 -1
  37. package/esm/node/ta-brand.js.map +0 -1
  38. package/esm/node/ta-cli.js.map +0 -1
  39. package/esm/node/ta-header.js.map +0 -1
  40. package/esm/node/ta-packages.js.map +0 -1
  41. package/lib/node/brand-extension.d.ts.map +0 -1
  42. package/lib/node/brand-extension.js.map +0 -1
  43. package/lib/node/brand-extension.spec.d.ts +0 -2
  44. package/lib/node/brand-extension.spec.d.ts.map +0 -1
  45. package/lib/node/brand-extension.spec.js +0 -57
  46. package/lib/node/brand-extension.spec.js.map +0 -1
  47. package/lib/node/bug-report.d.ts.map +0 -1
  48. package/lib/node/bug-report.js.map +0 -1
  49. package/lib/node/bug-report.spec.d.ts +0 -2
  50. package/lib/node/bug-report.spec.d.ts.map +0 -1
  51. package/lib/node/bug-report.spec.js +0 -11
  52. package/lib/node/bug-report.spec.js.map +0 -1
  53. package/lib/node/dino-logo.d.ts.map +0 -1
  54. package/lib/node/dino-logo.js.map +0 -1
  55. package/lib/node/exec-extension.d.ts.map +0 -1
  56. package/lib/node/exec-extension.js.map +0 -1
  57. package/lib/node/exec-mcp-transport.d.ts.map +0 -1
  58. package/lib/node/exec-mcp-transport.js.map +0 -1
  59. package/lib/node/exec-operations.d.ts.map +0 -1
  60. package/lib/node/exec-operations.js.map +0 -1
  61. package/lib/node/exec-operations.spec.d.ts +0 -2
  62. package/lib/node/exec-operations.spec.d.ts.map +0 -1
  63. package/lib/node/exec-operations.spec.js +0 -77
  64. package/lib/node/exec-operations.spec.js.map +0 -1
  65. package/lib/node/extensions.d.ts.map +0 -1
  66. package/lib/node/extensions.js.map +0 -1
  67. package/lib/node/extensions.spec.d.ts +0 -2
  68. package/lib/node/extensions.spec.d.ts.map +0 -1
  69. package/lib/node/extensions.spec.js +0 -94
  70. package/lib/node/extensions.spec.js.map +0 -1
  71. package/lib/node/glob-util.d.ts.map +0 -1
  72. package/lib/node/glob-util.js.map +0 -1
  73. package/lib/node/glob-util.spec.d.ts +0 -2
  74. package/lib/node/glob-util.spec.d.ts.map +0 -1
  75. package/lib/node/glob-util.spec.js +0 -27
  76. package/lib/node/glob-util.spec.js.map +0 -1
  77. package/lib/node/index.d.ts.map +0 -1
  78. package/lib/node/index.js.map +0 -1
  79. package/lib/node/pi-types.d.ts.map +0 -1
  80. package/lib/node/pi-types.js.map +0 -1
  81. package/lib/node/remote-context-files.d.ts.map +0 -1
  82. package/lib/node/remote-context-files.js.map +0 -1
  83. package/lib/node/remote-context-files.spec.d.ts +0 -2
  84. package/lib/node/remote-context-files.spec.d.ts.map +0 -1
  85. package/lib/node/remote-context-files.spec.js +0 -37
  86. package/lib/node/remote-context-files.spec.js.map +0 -1
  87. package/lib/node/serve-state.d.ts.map +0 -1
  88. package/lib/node/serve-state.js.map +0 -1
  89. package/lib/node/serve-state.spec.d.ts +0 -2
  90. package/lib/node/serve-state.spec.d.ts.map +0 -1
  91. package/lib/node/serve-state.spec.js +0 -35
  92. package/lib/node/serve-state.spec.js.map +0 -1
  93. package/lib/node/session-lock.d.ts.map +0 -1
  94. package/lib/node/session-lock.js.map +0 -1
  95. package/lib/node/session-lock.spec.d.ts +0 -2
  96. package/lib/node/session-lock.spec.d.ts.map +0 -1
  97. package/lib/node/session-lock.spec.js +0 -53
  98. package/lib/node/session-lock.spec.js.map +0 -1
  99. package/lib/node/ta-brand.d.ts.map +0 -1
  100. package/lib/node/ta-brand.js.map +0 -1
  101. package/lib/node/ta-brand.spec.d.ts +0 -2
  102. package/lib/node/ta-brand.spec.d.ts.map +0 -1
  103. package/lib/node/ta-brand.spec.js +0 -43
  104. package/lib/node/ta-brand.spec.js.map +0 -1
  105. package/lib/node/ta-cli.d.ts.map +0 -1
  106. package/lib/node/ta-cli.js.map +0 -1
  107. package/lib/node/ta-header.d.ts.map +0 -1
  108. package/lib/node/ta-header.js.map +0 -1
  109. package/lib/node/ta-header.spec.d.ts +0 -2
  110. package/lib/node/ta-header.spec.d.ts.map +0 -1
  111. package/lib/node/ta-header.spec.js +0 -67
  112. package/lib/node/ta-header.spec.js.map +0 -1
  113. package/lib/node/ta-packages.d.ts.map +0 -1
  114. package/lib/node/ta-packages.js.map +0 -1
  115. package/lib/node/ta-packages.spec.d.ts +0 -2
  116. package/lib/node/ta-packages.spec.d.ts.map +0 -1
  117. package/lib/node/ta-packages.spec.js +0 -89
  118. package/lib/node/ta-packages.spec.js.map +0 -1
  119. package/src/node/brand-extension.spec.ts +0 -63
  120. package/src/node/brand-extension.ts +0 -40
  121. package/src/node/bug-report.spec.ts +0 -9
  122. package/src/node/bug-report.ts +0 -55
  123. package/src/node/dino-logo.ts +0 -70
  124. package/src/node/exec-extension.ts +0 -40
  125. package/src/node/exec-mcp-transport.ts +0 -89
  126. package/src/node/exec-operations.spec.ts +0 -84
  127. package/src/node/exec-operations.ts +0 -123
  128. package/src/node/extensions.spec.ts +0 -110
  129. package/src/node/extensions.ts +0 -69
  130. package/src/node/glob-util.spec.ts +0 -29
  131. package/src/node/glob-util.ts +0 -47
  132. package/src/node/index.ts +0 -16
  133. package/src/node/pi-types.ts +0 -16
  134. package/src/node/remote-context-files.spec.ts +0 -37
  135. package/src/node/remote-context-files.ts +0 -51
  136. package/src/node/serve-state.spec.ts +0 -36
  137. package/src/node/serve-state.ts +0 -51
  138. package/src/node/session-lock.spec.ts +0 -56
  139. package/src/node/session-lock.ts +0 -105
  140. package/src/node/ta-brand.spec.ts +0 -46
  141. package/src/node/ta-brand.ts +0 -51
  142. package/src/node/ta-cli.ts +0 -207
  143. package/src/node/ta-header.spec.ts +0 -74
  144. package/src/node/ta-header.ts +0 -192
  145. package/src/node/ta-packages.spec.ts +0 -95
  146. package/src/node/ta-packages.ts +0 -208
@@ -1,47 +0,0 @@
1
- export namespace GlobUtil {
2
- /**
3
- * A matcher for the glob syntax of fd, which pi's find tool documents: `*`, `?`, `**`, `{a,b}` and
4
- * `[...]`. A pattern without `/` matches the name of the file at any depth.
5
- */
6
- export function matcher(pattern: string): (path: string) => boolean {
7
- const anywhere = !pattern.includes('/');
8
- const regexp = new RegExp(`^${toRegExp(pattern)}$`);
9
- return path => regexp.test(anywhere ? path.slice(path.lastIndexOf('/') + 1) : path);
10
- }
11
-
12
- function toRegExp(glob: string): string {
13
- let out = '';
14
- let braces = 0;
15
- for (let i = 0; i < glob.length; i++) {
16
- const char = glob[i];
17
- if (char === '*' && glob[i + 1] === '*') {
18
- const slash = glob[i + 2] === '/';
19
- out += slash ? '(?:.*/)?' : '.*';
20
- i += slash ? 2 : 1;
21
- } else if (char === '*') {
22
- out += '[^/]*';
23
- } else if (char === '?') {
24
- out += '[^/]';
25
- } else if (char === '{') {
26
- braces++;
27
- out += '(?:';
28
- } else if (char === '}' && braces) {
29
- braces--;
30
- out += ')';
31
- } else if (char === ',' && braces) {
32
- out += '|';
33
- } else if (char === '[') {
34
- const end = glob.indexOf(']', i + 2);
35
- if (end === -1) {
36
- out += '\\[';
37
- } else {
38
- out += glob.slice(i, end + 1);
39
- i = end;
40
- }
41
- } else {
42
- out += char.replace(/[\\^$.|+()]/g, '\\$&');
43
- }
44
- }
45
- return out;
46
- }
47
- }
package/src/node/index.ts DELETED
@@ -1,16 +0,0 @@
1
- export * from './brand-extension';
2
- export * from './bug-report';
3
- export * from './dino-logo';
4
- export * from './exec-extension';
5
- export * from './exec-mcp-transport';
6
- export * from './exec-operations';
7
- export * from './extensions';
8
- export * from './glob-util';
9
- export * from './pi-types';
10
- export * from './remote-context-files';
11
- export * from './serve-state';
12
- export * from './session-lock';
13
- export * from './ta-brand';
14
- export * from './ta-cli';
15
- export * from './ta-header';
16
- export * from './ta-packages';
@@ -1,16 +0,0 @@
1
- // Only types: importing them does not load pi or @celljs/ai-coding-agent. See PiBundleUtil.
2
- export type { PiSdk } from '@celljs/ai-coding-agent/lib/node/pi/pi-sdk-util';
3
-
4
- export type ExtensionAPI = import('@earendil-works/pi-coding-agent', { with: { 'resolution-mode': 'import' }}).ExtensionAPI;
5
- export type ExtensionContext = import('@earendil-works/pi-coding-agent', { with: { 'resolution-mode': 'import' }}).ExtensionContext;
6
- export type LoadExtensionsResult = import('@earendil-works/pi-coding-agent', { with: { 'resolution-mode': 'import' }}).LoadExtensionsResult;
7
- export type InlineExtension = import('@earendil-works/pi-coding-agent', { with: { 'resolution-mode': 'import' }}).InlineExtension;
8
- export type BashOperations = import('@earendil-works/pi-coding-agent', { with: { 'resolution-mode': 'import' }}).BashOperations;
9
- export type ReadOperations = import('@earendil-works/pi-coding-agent', { with: { 'resolution-mode': 'import' }}).ReadOperations;
10
- export type WriteOperations = import('@earendil-works/pi-coding-agent', { with: { 'resolution-mode': 'import' }}).WriteOperations;
11
- export type EditOperations = import('@earendil-works/pi-coding-agent', { with: { 'resolution-mode': 'import' }}).EditOperations;
12
- export type LsOperations = import('@earendil-works/pi-coding-agent', { with: { 'resolution-mode': 'import' }}).LsOperations;
13
- export type FindOperations = import('@earendil-works/pi-coding-agent', { with: { 'resolution-mode': 'import' }}).FindOperations;
14
- export type McpServerEntry = import('@earendil-works/pi-coding-agent', { with: { 'resolution-mode': 'import' }}).McpServerEntry;
15
- export type McpTransport = import('@earendil-works/pi-mcp', { with: { 'resolution-mode': 'import' }}).McpTransport;
16
- export type JsonRpcMessage = import('@earendil-works/pi-mcp', { with: { 'resolution-mode': 'import' }}).JsonRpcMessage;
@@ -1,37 +0,0 @@
1
- import { describe, expect, it } from 'bun:test';
2
- import { mkdirSync, mkdtempSync, realpathSync, writeFileSync } from 'node:fs';
3
- import { tmpdir } from 'node:os';
4
- import { join } from 'node:path';
5
- import { LocalExecEnvironment } from '@celljs/ta-exec';
6
- import { ExecOperations } from './exec-operations';
7
- import { RemoteContextFiles } from './remote-context-files';
8
-
9
- describe('RemoteContextFiles', () => {
10
- it('lists the directory and those above it, the outermost first', () => {
11
- expect(RemoteContextFiles.directories('/srv/app/')).toEqual(['/', '/srv', '/srv/app']);
12
- });
13
-
14
- it('reads one file per directory, by pi names in pi order', async () => {
15
- const root = realpathSync(mkdtempSync(join(tmpdir(), 'ta-context-')));
16
- const app = join(root, 'app');
17
- mkdirSync(join(app, 'CLAUDE.md'), { recursive: true });
18
- writeFileSync(join(root, 'AGENTS.md'), '\uFEFFroot');
19
- writeFileSync(join(root, 'CLAUDE.md'), 'not this');
20
- writeFileSync(join(app, 'AGENTS.override.md'), 'app');
21
- const files = await RemoteContextFiles.load(new LocalExecEnvironment(app));
22
- expect(files.slice(-2)).toEqual([
23
- { path: join(root, 'AGENTS.md'), content: 'root' },
24
- { path: join(app, 'AGENTS.override.md'), content: 'app' },
25
- ]);
26
- });
27
- });
28
-
29
- describe('ExecOperations', () => {
30
- it('reads a path under the directory of this machine as one under the environment', () => {
31
- const operations = new ExecOperations(new LocalExecEnvironment('/srv/app'), '/home/me/.ta/agent/remote/ssh/dev/srv/app');
32
- expect(operations.map('/home/me/.ta/agent/remote/ssh/dev/srv/app')).toBe('/srv/app');
33
- expect(operations.map('/home/me/.ta/agent/remote/ssh/dev/srv/app/src/a.ts')).toBe('/srv/app/src/a.ts');
34
- expect(operations.map('/home/me/.ta/agent/remote/ssh/dev/srv/apple')).toBe('/home/me/.ta/agent/remote/ssh/dev/srv/apple');
35
- expect(operations.map('/srv/app/b.ts')).toBe('/srv/app/b.ts');
36
- });
37
- });
@@ -1,51 +0,0 @@
1
- import { posix } from 'node:path';
2
- import type { ExecEnvironment } from '@celljs/ta-exec';
3
-
4
- export interface ContextFile {
5
- path: string;
6
- content: string;
7
- }
8
-
9
- /**
10
- * The context files (AGENTS.md...) of a directory of another environment and of the directories above
11
- * it, as pi loads them from this machine's: one per directory, the first of pi's names it has, the
12
- * outermost first. All in one go, so that a host far away costs about one round trip.
13
- */
14
- export namespace RemoteContextFiles {
15
- /** pi's names, in its order. */
16
- export const NAMES = ['AGENTS.override.md', 'AGENTS.md', 'AGENTS.MD', 'CLAUDE.md', 'CLAUDE.MD'];
17
-
18
- /** The directory and those above it, the outermost first. */
19
- export function directories(cwd: string): string[] {
20
- const dirs: string[] = [];
21
- for (let dir = posix.resolve(cwd); ; dir = posix.dirname(dir)) {
22
- dirs.unshift(dir);
23
- if (dir === posix.dirname(dir)) {
24
- return dirs;
25
- }
26
- }
27
- }
28
-
29
- export async function load(environment: ExecEnvironment): Promise<ContextFile[]> {
30
- const found = await Promise.all(
31
- directories(environment.cwd).map(async dir => {
32
- const stats = await Promise.all(NAMES.map(name => environment.stat(posix.join(dir, name)).catch(() => undefined)));
33
- const index = stats.findIndex(stat => stat && !stat.isDirectory);
34
- return index < 0 ? undefined : posix.join(dir, NAMES[index]);
35
- }),
36
- );
37
- const files = await Promise.all(
38
- found.map(async path => {
39
- if (!path) {
40
- return undefined;
41
- }
42
- try {
43
- return { path, content: new TextDecoder().decode(await environment.readFile(path)).replace(/^\uFEFF/, '') };
44
- } catch {
45
- return undefined;
46
- }
47
- }),
48
- );
49
- return files.filter((file): file is ContextFile => file !== undefined);
50
- }
51
- }
@@ -1,36 +0,0 @@
1
- import { afterAll, describe, expect, it } from 'bun:test';
2
- import { mkdtempSync, rmSync, statSync, writeFileSync } from 'node:fs';
3
- import { createServer } from 'node:net';
4
- import { tmpdir } from 'node:os';
5
- import { join } from 'node:path';
6
- import { TaServeStateUtil } from './serve-state';
7
-
8
- describe('TaServeStateUtil', () => {
9
- const dir = mkdtempSync(join(tmpdir(), 'ta-serve-state-'));
10
- afterAll(() => rmSync(dir, { recursive: true, force: true }));
11
-
12
- it('keeps the port and the token in a file only this user reads', () => {
13
- const file = join(dir, 'agent', TaServeStateUtil.FILE);
14
- TaServeStateUtil.write({ port: 4100, token: 'secret' }, file);
15
- expect(TaServeStateUtil.read(file)).toEqual({ port: 4100, token: 'secret' });
16
- expect(statSync(file).mode & 0o777).toBe(0o600);
17
- });
18
-
19
- it('reads nothing from a missing or broken file', () => {
20
- expect(TaServeStateUtil.read(join(dir, 'missing.json'))).toEqual({});
21
- const broken = join(dir, 'broken.json');
22
- writeFileSync(broken, 'not json');
23
- expect(TaServeStateUtil.read(broken)).toEqual({});
24
- writeFileSync(broken, JSON.stringify({ port: 70000, token: '' }));
25
- expect(TaServeStateUtil.read(broken)).toEqual({ port: undefined, token: undefined });
26
- });
27
-
28
- it('knows whether a port is taken', async () => {
29
- const server = createServer();
30
- await new Promise<void>(resolve => server.listen(0, '127.0.0.1', resolve));
31
- const { port } = server.address() as { port: number };
32
- expect(await TaServeStateUtil.isFree(port, '127.0.0.1')).toBe(false);
33
- await new Promise(resolve => server.close(resolve));
34
- expect(await TaServeStateUtil.isFree(port, '127.0.0.1')).toBe(true);
35
- });
36
- });
@@ -1,51 +0,0 @@
1
- import { chmodSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs';
2
- import { createServer } from 'node:net';
3
- import { dirname, join } from 'node:path';
4
- import { TaBrand } from './ta-brand';
5
-
6
- /**
7
- * What `ta serve` keeps across restarts: its port and its token. A web app installed from it (a PWA)
8
- * belongs to one address, and remembers the token it signed in with; both must stay the same.
9
- */
10
- export interface TaServeState {
11
- port?: number;
12
- token?: string;
13
- }
14
-
15
- export namespace TaServeStateUtil {
16
- /** In the agent directory, readable only by this user, like serve.json. Removing it gives a new token. */
17
- export const FILE = 'serve-state.json';
18
-
19
- export function file(agentDir = TaBrand.agentDir()): string {
20
- return join(agentDir, FILE);
21
- }
22
-
23
- /** Nothing when the file is missing or is not what {@link write} wrote. */
24
- export function read(path = file()): TaServeState {
25
- try {
26
- const { port, token } = JSON.parse(readFileSync(path, 'utf8')) as TaServeState;
27
- return {
28
- port: typeof port === 'number' && Number.isInteger(port) && port > 0 && port < 65536 ? port : undefined,
29
- token: typeof token === 'string' && token.length > 0 ? token : undefined,
30
- };
31
- } catch {
32
- return {};
33
- }
34
- }
35
-
36
- export function write(state: TaServeState, path = file()): void {
37
- mkdirSync(dirname(path), { recursive: true });
38
- writeFileSync(path, JSON.stringify(state), { mode: 0o600 });
39
- // The mode above only applies to a new file.
40
- chmodSync(path, 0o600);
41
- }
42
-
43
- /** Whether a server could listen on `port` of `host` now. */
44
- export function isFree(port: number, host: string): Promise<boolean> {
45
- return new Promise(resolve => {
46
- const server = createServer();
47
- server.once('error', () => resolve(false));
48
- server.listen(port, host, () => server.close(() => resolve(true)));
49
- });
50
- }
51
- }
@@ -1,56 +0,0 @@
1
- import { describe, expect, it } from 'bun:test';
2
- import { spawnSync } from 'node:child_process';
3
- import { existsSync, mkdtempSync, readdirSync, writeFileSync } from 'node:fs';
4
- import { hostname, tmpdir } from 'node:os';
5
- import { join } from 'node:path';
6
- import { SessionLock } from './session-lock';
7
-
8
- const sessionFile = () => join(mkdtempSync(join(tmpdir(), 'ta-lock-')), 'sessions', 'a.jsonl');
9
-
10
- describe('SessionLock', () => {
11
- it('lets one owner hold a session and refuses the others', () => {
12
- const file = sessionFile();
13
- const [first, second] = [new SessionLock(), new SessionLock()];
14
- expect(first.acquire(file)).toBe(true);
15
- expect(first.acquire(file)).toBe(true);
16
- expect(second.acquire(file)).toBe(false);
17
- expect(second.isHeldByOther(file)).toBe(true);
18
- expect(first.isHeldByOther(file)).toBe(false);
19
- });
20
-
21
- it('frees the session on release', () => {
22
- const file = sessionFile();
23
- const [first, second] = [new SessionLock(), new SessionLock()];
24
- first.acquire(file);
25
- first.release(file);
26
- expect(existsSync(first.path(file))).toBe(false);
27
- expect(second.acquire(file)).toBe(true);
28
- });
29
-
30
- it('takes over the lock of a process that is gone', () => {
31
- const file = sessionFile();
32
- const lock = new SessionLock();
33
- lock.acquire(file);
34
- writeFileSync(lock.path(file), JSON.stringify({ owner: 'gone', pid: spawnSync('true').pid, host: hostname(), createdAt: 1 }));
35
- const next = new SessionLock();
36
- expect(next.isHeldByOther(file)).toBe(false);
37
- expect(next.acquire(file)).toBe(true);
38
- });
39
-
40
- it('respects the lock of another host, whose processes it cannot see', () => {
41
- const file = sessionFile();
42
- const first = new SessionLock();
43
- first.acquire(file);
44
- writeFileSync(first.path(file), JSON.stringify({ owner: 'other', pid: 1, host: 'another-host', createdAt: 1 }));
45
- expect(new SessionLock().acquire(file)).toBe(false);
46
- });
47
-
48
- it('leaves nothing but the lock behind', () => {
49
- const file = sessionFile();
50
- const lock = new SessionLock();
51
- lock.acquire(file);
52
- expect(readdirSync(join(file, '..'))).toEqual(['a.jsonl.lock']);
53
- lock.releaseAll();
54
- expect(readdirSync(join(file, '..'))).toEqual([]);
55
- });
56
- });
@@ -1,105 +0,0 @@
1
- import { randomUUID } from 'node:crypto';
2
- import { linkSync, mkdirSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
3
- import { hostname } from 'node:os';
4
- import { dirname } from 'node:path';
5
-
6
- interface LockFile {
7
- owner: string;
8
- pid: number;
9
- host: string;
10
- createdAt: number;
11
- }
12
-
13
- /**
14
- * Keeps a session file to one writer: pi appends to it without a lock, so two processes on one session
15
- * would interleave their entries. The lock is a file next to the session, created atomically, and is
16
- * void once the process that made it is gone. Each instance is one owner, so that two open sessions of
17
- * one process exclude each other too.
18
- */
19
- export class SessionLock {
20
- protected readonly owner = randomUUID();
21
- protected readonly held = new Set<string>();
22
-
23
- /** False when another owner holds it. Taking a lock this owner holds succeeds. */
24
- acquire(sessionFile: string): boolean {
25
- if (this.held.has(sessionFile)) {
26
- return true;
27
- }
28
- mkdirSync(dirname(sessionFile), { recursive: true });
29
- // A second round after removing a stale lock; losing that race to another process ends in false.
30
- for (let attempt = 0; attempt < 2; attempt++) {
31
- if (this.create(sessionFile)) {
32
- this.held.add(sessionFile);
33
- return true;
34
- }
35
- if (this.isHeldByOther(sessionFile)) {
36
- return false;
37
- }
38
- rmSync(this.path(sessionFile), { force: true });
39
- }
40
- return false;
41
- }
42
-
43
- isHeldByOther(sessionFile: string): boolean {
44
- const lock = this.read(sessionFile);
45
- return lock !== undefined && lock.owner !== this.owner && this.isAlive(lock);
46
- }
47
-
48
- release(sessionFile: string): void {
49
- if (this.held.delete(sessionFile)) {
50
- rmSync(this.path(sessionFile), { force: true });
51
- }
52
- }
53
-
54
- releaseAll(): void {
55
- for (const sessionFile of [...this.held]) {
56
- this.release(sessionFile);
57
- }
58
- }
59
-
60
- path(sessionFile: string): string {
61
- return `${sessionFile}.lock`;
62
- }
63
-
64
- /** Writes the lock aside and links it into place, so that nobody can read a half-written lock. */
65
- protected create(sessionFile: string): boolean {
66
- const lock: LockFile = { owner: this.owner, pid: process.pid, host: hostname(), createdAt: Date.now() };
67
- const draft = `${this.path(sessionFile)}.${this.owner}`;
68
- writeFileSync(draft, JSON.stringify(lock));
69
- try {
70
- linkSync(draft, this.path(sessionFile));
71
- return true;
72
- } catch (error) {
73
- if ((error as NodeJS.ErrnoException).code === 'EEXIST') {
74
- return false;
75
- }
76
- throw error;
77
- } finally {
78
- rmSync(draft, { force: true });
79
- }
80
- }
81
-
82
- protected read(sessionFile: string): LockFile | undefined {
83
- try {
84
- return JSON.parse(readFileSync(this.path(sessionFile), 'utf8'));
85
- } catch (error) {
86
- if ((error as NodeJS.ErrnoException).code === 'ENOENT') {
87
- return undefined;
88
- }
89
- throw error;
90
- }
91
- }
92
-
93
- protected isAlive(lock: LockFile): boolean {
94
- if (lock.host !== hostname()) {
95
- // Its process cannot be checked from here (a shared volume): respect it.
96
- return true;
97
- }
98
- try {
99
- process.kill(lock.pid, 0);
100
- return true;
101
- } catch (error) {
102
- return (error as NodeJS.ErrnoException).code === 'EPERM';
103
- }
104
- }
105
- }
@@ -1,46 +0,0 @@
1
- import { afterEach, describe, expect, it } from 'bun:test';
2
- import { TaBrand } from './ta-brand';
3
-
4
- describe('TaBrand.activate', () => {
5
- const saved = { ...process.env };
6
- const title = process.title;
7
- const emitWarning = process.emitWarning;
8
-
9
- afterEach(() => {
10
- process.env = { ...saved };
11
- process.title = title;
12
- process.emitWarning = emitWarning;
13
- });
14
-
15
- function clean(): void {
16
- for (const name of [...Object.keys(TaBrand.ENV_ALIASES), ...Object.values(TaBrand.ENV_ALIASES)]) {
17
- delete process.env[name];
18
- }
19
- }
20
-
21
- it("points pi at ta's package directory", () => {
22
- TaBrand.activate('/opt/ta/dist/pi');
23
- expect(process.env.PI_PACKAGE_DIR).toBe('/opt/ta/dist/pi');
24
- expect(process.env.AI_AGENT).toBe('ta');
25
- });
26
-
27
- it("passes the TA_ names of pi's settings on to pi", () => {
28
- clean();
29
- process.env.TA_OFFLINE = '1';
30
- process.env.TA_SELF_UPDATE_COMMAND = 'brew upgrade ta';
31
- TaBrand.activate('/opt/ta/dist/pi');
32
- expect(process.env.PI_OFFLINE).toBe('1');
33
- expect(process.env.PI_SELF_UPDATE_COMMAND).toBe('brew upgrade ta');
34
- });
35
-
36
- it("turns pi's reports to pi.dev off, unless asked for", () => {
37
- clean();
38
- TaBrand.activate('/opt/ta/dist/pi');
39
- expect(process.env.PI_TELEMETRY).toBe('0');
40
- expect(process.env.PI_SKIP_VERSION_CHECK).toBe('1');
41
- clean();
42
- process.env.TA_TELEMETRY = '1';
43
- TaBrand.activate('/opt/ta/dist/pi');
44
- expect(process.env.PI_TELEMETRY).toBe('1');
45
- });
46
- });
@@ -1,51 +0,0 @@
1
- import { readFileSync } from 'node:fs';
2
- import { homedir } from 'node:os';
3
- import { join } from 'node:path';
4
-
5
- export namespace TaBrand {
6
- export const NAME = 'ta';
7
-
8
- /** The settings of pi that ta's help lists as `TA_*`; pi reads only the `PI_*` names. */
9
- export const ENV_ALIASES: Record<string, string> = {
10
- TA_OFFLINE: 'PI_OFFLINE',
11
- TA_SELF_UPDATE_COMMAND: 'PI_SELF_UPDATE_COMMAND',
12
- TA_TELEMETRY: 'PI_TELEMETRY',
13
- TA_SKIP_VERSION_CHECK: 'PI_SKIP_VERSION_CHECK',
14
- };
15
-
16
- /** Where pi keeps ta's files, without loading pi: an idle `ta serve` must not pay for it. */
17
- export function agentDir(): string {
18
- return process.env.TA_CODING_AGENT_DIR ?? join(homedir(), '.ta', 'agent');
19
- }
20
-
21
- /** The version of ta, from the package.json that {@link activate} named. */
22
- export function version(): string {
23
- try {
24
- return JSON.parse(readFileSync(join(process.env.PI_PACKAGE_DIR ?? '', 'package.json'), 'utf8')).version ?? 'unknown';
25
- } catch {
26
- return 'unknown';
27
- }
28
- }
29
-
30
- /**
31
- * Makes pi `ta`: its name, `~/.ta/agent`, project `.ta/` and `TA_CODING_AGENT_DIR` come from the
32
- * `piConfig` of the package.json in `brandDir`, next to pi's assets and ta's patched build of pi
33
- * (see the build of apps/ta), which `PiBundleUtil` then loads. Call it before pi is imported: pi
34
- * reads them when it loads. The rest is what pi's own command does before `main`.
35
- */
36
- export function activate(brandDir: string): void {
37
- process.env.PI_PACKAGE_DIR = brandDir;
38
- for (const [ta, pi] of Object.entries(ENV_ALIASES)) {
39
- if (process.env[ta] !== undefined) {
40
- process.env[pi] = process.env[ta];
41
- }
42
- }
43
- // pi's install telemetry reports to pi.dev; the check for a new release at startup stays off unless asked for.
44
- process.env.PI_SKIP_VERSION_CHECK ??= '1';
45
- process.env.PI_TELEMETRY ??= '0';
46
- process.env.PI_CODING_AGENT = 'true';
47
- process.env.AI_AGENT = NAME;
48
- process.title = NAME;
49
- process.emitWarning = () => undefined;
50
- }
51
- }