Skip to content

Commit 1146f36

Browse files
tps-embertps-anvil
authored andcommitted
task complete: 39e2f6da-6003-43c2-ad43-3c246271d0a2
1 parent 0940fee commit 1146f36

2 files changed

Lines changed: 31 additions & 7 deletions

File tree

packages/cli/bin/tps.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ async function main() {
309309
action: "logs",
310310
id: getFlag("id") ?? rest[1],
311311
lines: cli.flags.lines as number | undefined,
312-
follow: cli.flags.follow as boolean | undefined,
312+
follow: (cli.flags.follow as boolean | undefined) || process.argv.includes("-f"),
313313
});
314314
} else if (action === "commit") {
315315
// Inline helpers (getAuthor/getRepeatedFlags defined in else block below)

packages/cli/src/commands/agent.ts

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -110,14 +110,22 @@ function tailLines(content: string, count: number): string[] {
110110

111111
async function streamLogUpdates(logPath: string, offset: number): Promise<never> {
112112
let position = offset;
113+
let reading = false;
114+
let pending = false;
113115

114116
const readFromPosition = (): void => {
117+
if (reading) {
118+
pending = true;
119+
return;
120+
}
121+
115122
const size = statSync(logPath).size;
116123
if (size < position) {
117124
position = 0;
118125
}
119126
if (size === position) return;
120127

128+
reading = true;
121129
const stream = createReadStream(logPath, {
122130
encoding: "utf-8",
123131
start: position,
@@ -130,17 +138,33 @@ async function streamLogUpdates(logPath: string, offset: number): Promise<never>
130138

131139
stream.on("end", () => {
132140
position = size;
141+
reading = false;
142+
if (pending) {
143+
pending = false;
144+
readFromPosition();
145+
}
133146
});
134-
};
135147

136-
readFromPosition();
148+
stream.on("error", () => {
149+
reading = false;
150+
});
151+
};
137152

138-
await new Promise<never>((_resolve) => {
139-
watch(logPath, { persistent: true }, (eventType) => {
153+
await new Promise<never>(() => {
154+
const watcher = watch(logPath, { persistent: true }, (eventType) => {
140155
if (eventType === "change" || eventType === "rename") {
141156
readFromPosition();
142157
}
143158
});
159+
160+
const handleSigint = (): void => {
161+
watcher.close();
162+
process.removeListener("SIGINT", handleSigint);
163+
process.stdout.write("\n");
164+
process.exit(0);
165+
};
166+
167+
process.on("SIGINT", handleSigint);
144168
});
145169

146170
process.exit(0);
@@ -149,12 +173,12 @@ async function streamLogUpdates(logPath: string, offset: number): Promise<never>
149173
async function logAgentRuntime(args: AgentArgs): Promise<void> {
150174
const agentId = args.id;
151175
if (!agentId) {
152-
console.error("Usage: tps agent logs --id <agent-id> [--lines <N>] [--follow]");
176+
console.error("Usage: tps agent logs --id <agent-id> [--lines <N>] [--follow|-f]");
153177
process.exit(1);
154178
}
155179

156180
const lineCount = normalizeLogLineCount(args.lines);
157-
const logPath = join(healthcheckHomeDir(), ".tps", "logs", `${agentId}.log`);
181+
const logPath = join(healthcheckHomeDir(), ".tps", "agents", agentId, "session.log");
158182
if (!existsSync(logPath)) {
159183
console.error(`No log file found for ${agentId}`);
160184
process.exit(1);

0 commit comments

Comments
 (0)