Skip to content

Commit 7acd42a

Browse files
authored
fix: harden parser, lifecycle, executor, fs, fetch, and gateway paths from the astra-6 audit (#290)
1 parent b12dfa1 commit 7acd42a

51 files changed

Lines changed: 2654 additions & 554 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎.changeset/atomic-file-writes.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@pymodel/pythinker-code": patch
3+
---
4+
5+
Write files atomically so an interrupted write leaves the previous content intact.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@pymodel/pythinker-code": patch
3+
---
4+
5+
Allow cancelling a prompt while it is still starting.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@pymodel/pythinker-code": patch
3+
---
4+
5+
Fix DSML and Hermes tool calls in streamed responses being dropped, split, or mistaken for quoted documentation depending on how the response was chunked.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@pymodel/pythinker-code": patch
3+
---
4+
5+
Block file reads and writes that reach a sensitive file through a symlink alias.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@pymodel/pythinker-code": patch
3+
---
4+
5+
Report each subagent run's own token usage instead of the agent's lifetime total.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@pymodel/pythinker-code": patch
3+
---
4+
5+
Keep a cancelled tool that ignores the stop signal from overlapping with the next tool on the same file.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@pymodel/pythinker-code": patch
3+
---
4+
5+
Stop downloading a web page as soon as it exceeds the size limit instead of buffering it first.

‎packages/agent-core-v2/src/agent/prompt/promptService.ts‎

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ interface Record extends PromptSnapshot {
187187
readonly launchedDeferred: Deferred<Turn | undefined>;
188188
readonly completionDeferred: Deferred<PromptCompletion>;
189189
handle: PromptHandle;
190+
cancelReason?: Error;
190191
}
191192

192193
function bundledSkillBlockCount(message: ContextMessage): number {
@@ -227,6 +228,7 @@ export const promptLaunchingKey = defineState<boolean>('prompt.launching', () =>
227228
export class AgentPromptService implements IAgentPromptService {
228229
declare readonly _serviceBrand: undefined;
229230
private active: (Record & { turn: Turn }) | undefined;
231+
private launchingRecord: Record | undefined;
230232
private readonly pending: Record[] = [];
231233
private readonly steered = new Map<string, Record[]>();
232234
private readonly reservedPromptIds = new Set<string>();
@@ -455,6 +457,7 @@ export class AgentPromptService implements IAgentPromptService {
455457

456458
abort(promptId: string, reason: Error = userCancellationReason()): boolean {
457459
if (this.active?.id === promptId) { this.loop.cancel(this.active.turn.id, reason); return true; }
460+
if (this.launchingRecord?.id === promptId) { this.launchingRecord.cancelReason ??= reason; return true; }
458461
const index = this.pending.findIndex((item) => item.id === promptId);
459462
if (index < 0) throw new Error2(ErrorCodes.PROMPT_NOT_FOUND, `prompt ${promptId} not found`);
460463
const [item] = this.pending.splice(index, 1) as [Record];
@@ -466,6 +469,11 @@ export class AgentPromptService implements IAgentPromptService {
466469

467470
async drain(reason: Error = userCancellationReason()): Promise<void> {
468471
for (const item of this.pending.slice()) this.abort(item.id, reason);
472+
const launching = this.launchingRecord;
473+
if (launching !== undefined) {
474+
this.abort(launching.id, reason);
475+
await launching.launchedDeferred.promise;
476+
}
469477
if (this.active !== undefined) this.abort(this.active.id, reason);
470478
}
471479

@@ -488,47 +496,62 @@ export class AgentPromptService implements IAgentPromptService {
488496

489497
clear(): void {
490498
for (const item of this.pending.slice()) this.abort(item.id);
499+
if (this.launchingRecord !== undefined) this.abort(this.launchingRecord.id);
491500
if (this.active !== undefined) this.abort(this.active.id);
492501
this.context.clear();
493502
}
494503

495504
private async startNext(): Promise<void> {
496505
if (this.active !== undefined || this.launching || this.steering > 0) return;
497506
const item = this.pending.shift(); if (item === undefined) return;
507+
if (this.fullCompaction.compacting !== null && this.loop.status().state !== 'running') { this.pending.unshift(item); return; }
498508
this.launching = true;
509+
this.launchingRecord = item;
499510
try {
500-
if (this.fullCompaction.compacting !== null && this.loop.status().state !== 'running') { this.pending.unshift(item); return; }
501511
const { message, captions } = this.extractCompressionCaptions(item.message);
502512
await this.materializeDaemonRefs(message);
513+
if (this.settleCancelledLaunch(item)) return;
503514
if (await this.blockedByHook(message, false)) {
504515
this.appendPrompt(message, captions); item.state = 'blocked'; item.launchedDeferred.resolve(undefined);
505516
item.completionDeferred.resolve({ promptId: item.id, result: undefined, state: 'blocked' });
506517
this.publishCompleted(item.id, 'blocked'); return;
507518
}
508-
const turn = (await this.loop.enqueue(
519+
if (this.settleCancelledLaunch(item)) return;
520+
const receipt = this.loop.enqueue(
509521
new PromptStepRequest(
510522
message,
511523
captions,
512524
this.reminder(),
513525
item.maxOutputSize,
514526
item.infiniteRetry,
515527
),
516-
).assigned).turn;
528+
);
529+
const turn = (await receipt.assigned).turn;
517530
if (turn === undefined) { this.pending.unshift(item); return; }
518531
item.state = 'running'; item.launchedDeferred.resolve(turn); this.active = Object.assign(item, { turn });
519532
this.publishStarted(item);
533+
if (item.cancelReason !== undefined) this.loop.cancel(turn.id, item.cancelReason);
520534
void turn.result.then((result) => this.settle(item, result));
521535
} catch {
522536
item.state = 'failed';
523537
item.launchedDeferred.resolve(undefined);
524538
item.completionDeferred.resolve({ promptId: item.id, result: undefined, state: 'failed' });
525539
this.publishCompleted(item.id, 'failed');
526540
} finally {
541+
this.launchingRecord = undefined;
527542
this.launching = false;
528543
if (this.active === undefined) void this.startNext();
529544
}
530545
}
531546

547+
private settleCancelledLaunch(item: Record): boolean {
548+
if (item.cancelReason === undefined) return false;
549+
item.state = 'cancelled'; item.launchedDeferred.resolve(undefined);
550+
item.completionDeferred.resolve({ promptId: item.id, result: undefined, state: 'cancelled' });
551+
this.publishAborted(item.id);
552+
return true;
553+
}
554+
532555
private settle(item: Record, result: TurnResult): void {
533556
if (this.active?.id !== item.id) return;
534557
this.active = undefined;

‎packages/agent-core-v2/src/agent/toolExecutor/toolExecutorService.ts‎

Lines changed: 79 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ import {
5252
type UnavailableToolDescriber,
5353
} from './toolExecutor';
5454
import { ToolCallStarted, ToolProgress, ToolResultEvent } from './toolExecutorEvents';
55-
import { ToolScheduler } from './toolScheduler';
55+
import { ToolScheduler, type OutstandingEffect } from './toolScheduler';
5656

5757
const ABORT_GRACE_MS = 2_000;
5858
const TOOL_OUTPUT_EMPTY = 'Tool output is empty.';
@@ -71,12 +71,15 @@ export interface ToolExecutionTask {
7171
export interface ToolExecutionRunResult {
7272
readonly result: ToolResult;
7373
readonly outcome: ToolExecutionOutcome;
74+
readonly cancelled?: boolean;
75+
readonly effectsSettled?: Promise<void>;
7476
}
7577

7678
interface TimedToolResult {
7779
readonly index: number;
7880
readonly result: ToolResult;
7981
readonly outcome: ToolExecutionOutcome;
82+
readonly cancelled: boolean;
8083
readonly durationMs: number;
8184
}
8285

@@ -111,6 +114,7 @@ export const toolExecutorDupTypeTurnIdKey = defineState<number | undefined>(
111114
export class AgentToolExecutorService implements IAgentToolExecutorService {
112115
declare readonly _serviceBrand: undefined;
113116

117+
private readonly outstandingEffects = new Set<OutstandingEffect>();
114118
private readonly beforeExecuteEmitter = new BeforeToolExecuteEmitter();
115119
readonly onBeforeExecuteTool: Event<BeforeToolExecuteEvent> = this.beforeExecuteEmitter.event;
116120
private readonly willExecuteEmitter = new AsyncEmitter<WillExecuteToolEvent>();
@@ -302,7 +306,13 @@ export class AgentToolExecutorService implements IAgentToolExecutorService {
302306
);
303307

304308
this.dispatchToolResult(call, finalized, options);
305-
this.trackToolCall(call, finalized, timedResult.durationMs, options);
309+
this.trackToolCall(
310+
call,
311+
finalized,
312+
timedResult.durationMs,
313+
options,
314+
timedResult.cancelled || timedResult.outcome === 'aborted',
315+
);
306316

307317
return {
308318
toolCallId: call.toolCall.id,
@@ -316,8 +326,9 @@ export class AgentToolExecutorService implements IAgentToolExecutorService {
316326
result: ToolResult,
317327
durationMs: number,
318328
options: ToolExecutorExecuteOptions,
329+
cancelled: boolean,
319330
): void {
320-
const outcome = toolTelemetryOutcome(result);
331+
const outcome = toolTelemetryOutcome(result, cancelled);
321332
const toolCallId = call.toolCall.id;
322333
const dupType = this.toolCallDupTypes.get(toolCallId) ?? 'normal';
323334
this.toolCallDupTypes.delete(toolCallId);
@@ -385,6 +396,10 @@ export class AgentToolExecutorService implements IAgentToolExecutorService {
385396
return settleError(call.args, call.output, 'preflight-rejected');
386397
}
387398

399+
if (options.signal.aborted) {
400+
return settleError(call.args, abortedToolOutput(call.toolName, options.signal), 'aborted');
401+
}
402+
388403
let execution: ToolExecution;
389404
try {
390405
execution = await call.tool.resolveExecution(call.args);
@@ -458,7 +473,7 @@ export class AgentToolExecutorService implements IAgentToolExecutorService {
458473
tasks: ToolExecutionTask[],
459474
signal: AbortSignal,
460475
): AsyncIterable<TimedToolResult> {
461-
const scheduler = new ToolScheduler<TimedToolResult>();
476+
const scheduler = new ToolScheduler<TimedToolResult>(this.outstandingEffects);
462477
const allResults: Array<Promise<TimedToolResult>> = [];
463478
const pendingResults = new Map<number, Promise<SettledTimedToolResult>>();
464479

@@ -468,13 +483,24 @@ export class AgentToolExecutorService implements IAgentToolExecutorService {
468483
accesses: task.accesses,
469484
start: async () => {
470485
const startedAt = Date.now();
486+
const run = task.execute(signal);
471487
return {
472-
result: task.execute(signal).then(({ result, outcome }) => ({
473-
index,
474-
result,
475-
outcome,
476-
durationMs: Math.max(0, Date.now() - startedAt),
477-
})),
488+
result: run.then(({ result, outcome, cancelled, effectsSettled }) => {
489+
if (effectsSettled !== undefined) {
490+
this.trackOutstandingEffect(task.accesses, effectsSettled);
491+
}
492+
return {
493+
index,
494+
result,
495+
outcome,
496+
cancelled: cancelled === true,
497+
durationMs: Math.max(0, Date.now() - startedAt),
498+
};
499+
}),
500+
effectsSettled: run.then(
501+
(value) => value.effectsSettled,
502+
() => undefined,
503+
),
478504
};
479505
},
480506
});
@@ -501,6 +527,14 @@ export class AgentToolExecutorService implements IAgentToolExecutorService {
501527
}
502528
}
503529

530+
private trackOutstandingEffect(accesses: ToolAccesses, settled: Promise<void>): void {
531+
const effect: OutstandingEffect = { accesses, settled };
532+
this.outstandingEffects.add(effect);
533+
void settled.finally(() => {
534+
this.outstandingEffects.delete(effect);
535+
});
536+
}
537+
504538
private async runSingleExecution(
505539
call: RunnableToolCall,
506540
execution: RunnableToolExecution,
@@ -516,12 +550,14 @@ export class AgentToolExecutorService implements IAgentToolExecutorService {
516550
abortedToolOutput(call.toolName, signal),
517551
).result,
518552
outcome: 'aborted',
553+
cancelled: true,
519554
};
520555
}
521556

522557
let rawResult: ExecutableToolResult;
558+
let executePromise: Promise<ExecutableToolResult>;
523559
try {
524-
const executePromise = execution.execute({
560+
executePromise = execution.execute({
525561
turnId: options.turnId,
526562
toolCallId: call.toolCall.id,
527563
trace: options.trace,
@@ -532,7 +568,20 @@ export class AgentToolExecutorService implements IAgentToolExecutorService {
532568
this.dispatchToolProgress(call, update, options);
533569
},
534570
});
535-
rawResult = await raceWithAbortGrace(executePromise, signal, call.toolName);
571+
const raced = await raceWithAbortGrace(executePromise, signal);
572+
if (raced.graceExpired) {
573+
return {
574+
result: makeErrorToolResult(call, call.args, abortedToolOutput(call.toolName, signal))
575+
.result,
576+
outcome: 'executed',
577+
cancelled: true,
578+
effectsSettled: executePromise.then(
579+
() => undefined,
580+
() => undefined,
581+
),
582+
};
583+
}
584+
rawResult = raced.value;
536585
} catch (error) {
537586
const aborted = isAbortError(error) || signal.aborted;
538587
const output = aborted
@@ -541,6 +590,7 @@ export class AgentToolExecutorService implements IAgentToolExecutorService {
541590
return {
542591
result: makeErrorToolResult(call, call.args, output).result,
543592
outcome: 'executed',
593+
cancelled: aborted,
544594
};
545595
}
546596

@@ -904,28 +954,19 @@ function normalizeToolResult(result: ExecutableToolResult): ToolResult {
904954
return base;
905955
}
906956

907-
function toolTelemetryOutcome(result: ToolResult): 'success' | 'error' | 'cancelled' {
908-
if (result.isError !== true) return 'success';
909-
const text = toolOutputText(result.output).toLowerCase();
910-
return text.includes('aborted') ||
911-
text.includes('cancelled') ||
912-
text.includes('manually interrupted')
913-
? 'cancelled'
914-
: 'error';
957+
function toolTelemetryOutcome(
958+
result: ToolResult,
959+
cancelled: boolean,
960+
): 'success' | 'error' | 'cancelled' {
961+
if (cancelled) return 'cancelled';
962+
return result.isError === true ? 'error' : 'success';
915963
}
916964

917965
function toolTelemetryErrorType(outcome: 'success' | 'error' | 'cancelled'): 'cancelled' | 'error' {
918966
if (outcome === 'cancelled') return 'cancelled';
919967
return 'error';
920968
}
921969

922-
function toolOutputText(output: ToolResult['output']): string {
923-
if (typeof output === 'string') return output;
924-
return output
925-
.filter((part): part is Extract<ContentPart, { type: 'text' }> => part.type === 'text')
926-
.map((part) => part.text)
927-
.join('');
928-
}
929970

930971
function isMediaContentPart(part: ContentPart): boolean {
931972
return part.type === 'image_url' || part.type === 'audio_url' || part.type === 'video_url';
@@ -938,21 +979,21 @@ function abortedToolOutput(toolName: string, signal: AbortSignal): string {
938979
return `Tool "${toolName}" was aborted`;
939980
}
940981

982+
type AbortGraceOutcome<Result> =
983+
| { readonly graceExpired: false; readonly value: Result }
984+
| { readonly graceExpired: true };
985+
941986
async function raceWithAbortGrace<Result>(
942987
executePromise: Promise<Result>,
943988
signal: AbortSignal,
944-
toolName: string,
945-
): Promise<Result> {
989+
): Promise<AbortGraceOutcome<Result>> {
946990
let graceTimer: ReturnType<typeof setTimeout> | undefined;
947991
let onAbort: (() => void) | undefined;
948992

949-
const graceSentinel: Promise<Result> = new Promise((resolve) => {
993+
const graceSentinel: Promise<AbortGraceOutcome<Result>> = new Promise((resolve) => {
950994
const armTimer = (): void => {
951995
graceTimer = setTimeout(() => {
952-
resolve({
953-
output: abortedToolOutput(toolName, signal),
954-
isError: true,
955-
} as unknown as Result);
996+
resolve({ graceExpired: true });
956997
}, ABORT_GRACE_MS);
957998
};
958999
if (signal.aborted) {
@@ -964,7 +1005,10 @@ async function raceWithAbortGrace<Result>(
9641005
});
9651006

9661007
try {
967-
return await Promise.race([executePromise, graceSentinel]);
1008+
return await Promise.race([
1009+
executePromise.then((value) => ({ graceExpired: false, value }) as const),
1010+
graceSentinel,
1011+
]);
9681012
} finally {
9691013
if (graceTimer !== undefined) clearTimeout(graceTimer);
9701014
if (onAbort !== undefined) {

0 commit comments

Comments
 (0)