-
Notifications
You must be signed in to change notification settings - Fork 0
feat: activate Agent Attention as installed plugin #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| { | ||
| "name": "agent-attention", | ||
| "version": "0.0.0", | ||
| "private": true, | ||
| "type": "module", | ||
| "main": "src/main.ts" | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| import { join } from "node:path" | ||
|
|
||
| import { | ||
| invalidAgentAttentionStopInput, | ||
| runAgentAttentionStop, | ||
| type AgentAttentionStopRuntime, | ||
| } from "./stop" | ||
|
|
||
| interface ProcessResult { | ||
| exitCode: number | ||
| stdout: string | ||
| stderr: string | ||
| } | ||
|
|
||
| function pythonExecutable(): string { | ||
| return process.env.AGENT_ATTENTION_PYTHON || "python3" | ||
| } | ||
|
|
||
| async function runPython(arguments_: string[], timeout?: number): Promise<ProcessResult> { | ||
| const owner = join(import.meta.dir, "agent-attention.py") | ||
| try { | ||
| const child = Bun.spawn([pythonExecutable(), owner, ...arguments_], { | ||
| stdin: "inherit", | ||
| stdout: "pipe", | ||
| stderr: "pipe", | ||
| ...(timeout === undefined ? {} : { timeout }), | ||
| }) | ||
| const [stdout, stderr, exitCode] = await Promise.all([ | ||
| new Response(child.stdout).text(), | ||
| new Response(child.stderr).text(), | ||
| child.exited, | ||
| ]) | ||
| return { exitCode, stdout, stderr } | ||
| } catch (error) { | ||
| const detail = error instanceof Error ? error.message : "unknown error" | ||
| return { | ||
| exitCode: 1, | ||
| stdout: `${JSON.stringify({ | ||
| status: "error", | ||
| changed: false, | ||
| retry_safe: false, | ||
| error_category: "missing_python", | ||
| next_safe_action: "Install python3 in a standard system location, then retry.", | ||
| })}\n`, | ||
| stderr: `Agent Attention could not start python3: ${detail}\n`, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| function installedStopRuntime(): AgentAttentionStopRuntime { | ||
| return { | ||
| checkStop: async (threadId) => { | ||
| const result = await runPython(["check-stop", "--thread-id", threadId], 5_000) | ||
| if (result.exitCode !== 0) throw new Error(result.stderr.trim() || "owner check failed") | ||
| const parsed = JSON.parse(result.stdout) as { hook_action?: unknown; reason?: unknown } | ||
| if (parsed.hook_action !== "allow" && parsed.hook_action !== "continue") { | ||
| throw new Error("Agent Attention stop check returned an invalid action") | ||
| } | ||
| return { | ||
| hook_action: parsed.hook_action, | ||
| ...(typeof parsed.reason === "string" ? { reason: parsed.reason } : {}), | ||
| } | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| async function main(): Promise<number> { | ||
| const arguments_ = process.argv.slice(2) | ||
| if (arguments_[0] === "hook-stop") { | ||
| let input: unknown | ||
| try { | ||
| input = await Bun.stdin.json() | ||
| } catch { | ||
| process.stdout.write(`${JSON.stringify(invalidAgentAttentionStopInput())}\n`) | ||
| return 0 | ||
| } | ||
| const output = await runAgentAttentionStop(input, installedStopRuntime()) | ||
| process.stdout.write(`${JSON.stringify(output)}\n`) | ||
| return 0 | ||
| } | ||
| const result = await runPython(arguments_) | ||
| process.stdout.write(result.stdout) | ||
| process.stderr.write(result.stderr) | ||
| return result.exitCode | ||
| } | ||
|
|
||
| process.exit(await main()) | ||
|
myagentdojo marked this conversation as resolved.
Outdated
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| /** Stable Stop fields used to correlate one exact task with owner state. */ | ||
| export interface AgentAttentionStopInput { | ||
| cwd: string | ||
| session_id: string | ||
| stop_hook_active?: boolean | ||
| } | ||
|
|
||
| /** Minimal owner result consumed by the hook adapter. */ | ||
| export interface AgentAttentionStopCheck { | ||
| hook_action: "allow" | "continue" | ||
| reason?: string | ||
| } | ||
|
|
||
| /** Structured owner seam shared by source and installed Stop adapters. */ | ||
| export interface AgentAttentionStopRuntime { | ||
| checkStop: (threadId: string) => Promise<AgentAttentionStopCheck> | ||
| } | ||
|
|
||
| /** Codex Stop output that either permits stop or requests one continuation. */ | ||
| export type AgentAttentionStopOutput = | ||
| | { continue: true; suppressOutput: true } | ||
| | { decision: "block"; reason: string } | ||
|
|
||
| const invalidStopInput: AgentAttentionStopOutput = { | ||
| decision: "block", | ||
| reason: | ||
| "Agent Attention could not correlate this Stop event to structured owner state. Repair the hook payload contract before stopping.", | ||
| } | ||
|
|
||
| /** Validate only stable task-correlation fields needed by structured owner state. */ | ||
| export function isAgentAttentionStopInput(value: unknown): value is AgentAttentionStopInput { | ||
| if (!value || typeof value !== "object" || Array.isArray(value)) return false | ||
| const input = value as Record<string, unknown> | ||
| if (typeof input.cwd !== "string" || input.cwd.trim() === "") return false | ||
| if (typeof input.session_id !== "string" || input.session_id.trim() === "") return false | ||
| return input.stop_hook_active === undefined || typeof input.stop_hook_active === "boolean" | ||
| } | ||
|
|
||
| /** Enforce explicit owner state without reading assistant prose or transcripts. */ | ||
| export async function handleAgentAttentionStop( | ||
| input: AgentAttentionStopInput, | ||
| runtime: AgentAttentionStopRuntime, | ||
| ): Promise<AgentAttentionStopOutput> { | ||
| if (input.stop_hook_active) return { continue: true, suppressOutput: true } | ||
| const check = await runtime.checkStop(input.session_id) | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| if (check.hook_action === "continue") { | ||
| return { | ||
| decision: "block", | ||
| reason: | ||
| check.reason ?? | ||
| "Agent Attention owner state requires an actionable repair before stopping.", | ||
| } | ||
| } | ||
| return { continue: true, suppressOutput: true } | ||
| } | ||
|
|
||
| /** Convert owner failures into one actionable Stop block. */ | ||
| export async function runAgentAttentionStop( | ||
| input: unknown, | ||
| runtime: AgentAttentionStopRuntime, | ||
| ): Promise<AgentAttentionStopOutput> { | ||
| if (!isAgentAttentionStopInput(input)) return invalidStopInput | ||
| try { | ||
| return await handleAgentAttentionStop(input, runtime) | ||
| } catch (error) { | ||
| const detail = error instanceof Error ? error.message : "unknown error" | ||
| return { | ||
| decision: "block", | ||
| reason: `Agent Attention could not verify structured owner state. Repair the owner check before stopping: ${detail}`, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** Return the fail-closed response for malformed hook stdin. */ | ||
| export function invalidAgentAttentionStopInput(): AgentAttentionStopOutput { | ||
| return invalidStopInput | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.