Repository navigation
feat(goal-closeout): invoke terminal hook on stop, retire and archive #9065
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
Open
gcl-coder
wants to merge
5
commits into
loopx-project:main
Choose a base branch
from
gcl-coder:codex/goal-closeout-hook
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,213
−3
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
7423d74
feat(goal-closeout): add bounded terminal-state hook executor
gcl-coder a9b4f10
feat(goal-closeout): invoke terminal hook on stop, retire and archive
gcl-coder 3d784d4
fix(goal-closeout): harden the terminal hook boundary
gcl-coder a00dd37
fix(goal-closeout): run the archive hook after canonical locks release
gcl-coder f925473
Merge branch 'main' into codex/goal-closeout-hook
gcl-coder 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
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 @@ | ||
| """Terminal Goal closeout hooks owned by the machine configuration.""" |
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,146 @@ | ||
| from __future__ import annotations | ||
|
|
||
| from collections.abc import Mapping | ||
| from typing import Any | ||
|
|
||
| from ..machine_configuration.contract import MachineConfigurationNamespace | ||
|
|
||
| GOAL_CLOSEOUT_CONFIGURATION_SCHEMA = "loopx_goal_closeout_configuration_v0" | ||
| DEFAULT_CLOSEOUT_TIMEOUT_SECONDS = 30 | ||
| MAX_CLOSEOUT_TIMEOUT_SECONDS = 600 | ||
|
|
||
|
|
||
| def normalize_goal_closeout_configuration( | ||
| raw: Mapping[str, Any], | ||
| ) -> dict[str, Any]: | ||
| unknown = sorted(set(raw) - {"schema_version", "command", "timeout_seconds"}) | ||
| if unknown: | ||
| raise ValueError( | ||
| "goal_closeout contains unsupported fields: " + ", ".join(unknown) | ||
| ) | ||
| if raw.get("schema_version") != GOAL_CLOSEOUT_CONFIGURATION_SCHEMA: | ||
| raise ValueError( | ||
| "goal_closeout must use " + GOAL_CLOSEOUT_CONFIGURATION_SCHEMA | ||
| ) | ||
| raw_command = raw.get("command", []) | ||
| if not isinstance(raw_command, (list, tuple)): | ||
| raise ValueError("goal_closeout command must be an array of strings") | ||
| command: list[str] = [] | ||
| for part in raw_command: | ||
| if not isinstance(part, str) or not part.strip(): | ||
| raise ValueError( | ||
| "goal_closeout command entries must be non-empty strings" | ||
| ) | ||
| command.append(part) | ||
| raw_timeout = raw.get("timeout_seconds", DEFAULT_CLOSEOUT_TIMEOUT_SECONDS) | ||
| if isinstance(raw_timeout, bool) or not isinstance(raw_timeout, int): | ||
| raise ValueError("goal_closeout timeout_seconds must be an integer") | ||
| if not 1 <= raw_timeout <= MAX_CLOSEOUT_TIMEOUT_SECONDS: | ||
| raise ValueError( | ||
| "goal_closeout timeout_seconds must be between 1 and " | ||
| f"{MAX_CLOSEOUT_TIMEOUT_SECONDS}" | ||
| ) | ||
| return { | ||
| "schema_version": GOAL_CLOSEOUT_CONFIGURATION_SCHEMA, | ||
| "command": command, | ||
| "timeout_seconds": raw_timeout, | ||
| } | ||
|
|
||
|
|
||
| def _public_goal_closeout_configuration( | ||
| value: Mapping[str, Any], | ||
| ) -> dict[str, Any]: | ||
| """Publish whether a command exists, never the local program or its path.""" | ||
|
|
||
| normalized = normalize_goal_closeout_configuration(value) | ||
| return { | ||
| "schema_version": GOAL_CLOSEOUT_CONFIGURATION_SCHEMA, | ||
| "configured": bool(normalized["command"]), | ||
| "timeout_seconds": normalized["timeout_seconds"], | ||
| } | ||
|
|
||
|
|
||
| def _apply_goal_closeout_public_update( | ||
| current: Mapping[str, Any] | None, | ||
| update: Mapping[str, Any], | ||
| ) -> dict[str, Any]: | ||
| """Let the Dashboard change only the public timeout, not the command.""" | ||
|
|
||
| base = ( | ||
| normalize_goal_closeout_configuration(current) | ||
| if current is not None | ||
| else { | ||
| "schema_version": GOAL_CLOSEOUT_CONFIGURATION_SCHEMA, | ||
| "command": [], | ||
| "timeout_seconds": DEFAULT_CLOSEOUT_TIMEOUT_SECONDS, | ||
| } | ||
| ) | ||
| unknown = sorted(set(update) - {"schema_version", "configured", "timeout_seconds"}) | ||
| if unknown: | ||
| raise ValueError( | ||
| "goal_closeout public update contains unsupported fields: " | ||
| + ", ".join(unknown) | ||
| ) | ||
| if "schema_version" in update and ( | ||
| update["schema_version"] != GOAL_CLOSEOUT_CONFIGURATION_SCHEMA | ||
| ): | ||
| raise ValueError( | ||
| "goal_closeout must use " + GOAL_CLOSEOUT_CONFIGURATION_SCHEMA | ||
| ) | ||
| if "configured" in update and bool(update["configured"]) != bool(base["command"]): | ||
| raise ValueError( | ||
| "goal_closeout configured is derived from the locally configured command" | ||
| ) | ||
| merged = dict(base) | ||
| if "timeout_seconds" in update: | ||
| merged["timeout_seconds"] = update["timeout_seconds"] | ||
| return normalize_goal_closeout_configuration(merged) | ||
|
|
||
|
|
||
| def _apply_goal_closeout_local_update( | ||
| current: Mapping[str, Any] | None, | ||
| update: Mapping[str, Any], | ||
| ) -> dict[str, Any]: | ||
| """Let the local operator CLI write the command the Dashboard cannot.""" | ||
|
|
||
| base = ( | ||
| normalize_goal_closeout_configuration(current) | ||
| if current is not None | ||
| else { | ||
| "schema_version": GOAL_CLOSEOUT_CONFIGURATION_SCHEMA, | ||
| "command": [], | ||
| "timeout_seconds": DEFAULT_CLOSEOUT_TIMEOUT_SECONDS, | ||
| } | ||
| ) | ||
| unknown = sorted(set(update) - {"schema_version", "command", "timeout_seconds"}) | ||
| if unknown: | ||
| raise ValueError( | ||
| "goal_closeout update contains unsupported fields: " + ", ".join(unknown) | ||
| ) | ||
| return normalize_goal_closeout_configuration({**base, **dict(update)}) | ||
|
|
||
|
|
||
| def goal_closeout_machine_configuration_namespace() -> MachineConfigurationNamespace: | ||
| return MachineConfigurationNamespace( | ||
| namespace="goal_closeout", | ||
| schema_versions=frozenset({GOAL_CLOSEOUT_CONFIGURATION_SCHEMA}), | ||
| normalize=normalize_goal_closeout_configuration, | ||
| project_public=_public_goal_closeout_configuration, | ||
| apply_public_update=_apply_goal_closeout_public_update, | ||
| local_only_fields=frozenset({"command"}), | ||
| apply_local_update=_apply_goal_closeout_local_update, | ||
| title="Terminal Goal closeout command", | ||
| description=( | ||
| "Optional machine-wide command run after a Goal reaches a terminal " | ||
| "transition (stop, retirement or runtime archive). The command is " | ||
| "best effort, receives one bounded JSON payload on stdin, and can " | ||
| "never block the LoopX command that triggered it. Keep the command " | ||
| "local-private: write it with the machine-config CLI; the Dashboard " | ||
| "only sees whether one is configured and may adjust the timeout." | ||
| ), | ||
| default_configuration={ | ||
| "schema_version": GOAL_CLOSEOUT_CONFIGURATION_SCHEMA, | ||
| "command": [], | ||
| "timeout_seconds": DEFAULT_CLOSEOUT_TIMEOUT_SECONDS, | ||
| }, | ||
| ) |
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 |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| from __future__ import annotations | ||
|
|
||
| from collections.abc import Mapping | ||
| from contextlib import ExitStack, nullcontext | ||
| from dataclasses import dataclass | ||
| from enum import Enum | ||
|
|
@@ -472,6 +473,7 @@ def set_goal_activation_state( | |
| ) | ||
| return payload | ||
| locked_goal = _goal(source_payload, normalized_goal_id) | ||
| closeout_goal = dict(locked_goal) | ||
| locked_state = goal_activation_state(locked_goal) | ||
| if locked_state is not before_state: | ||
| payload.update( | ||
|
|
@@ -544,9 +546,57 @@ def set_goal_activation_state( | |
| (sync_payload or {}).get("recommended_action") | ||
| or f"rerun loopx sync-global --goal-id {normalized_goal_id} after repairing the shared registry route" | ||
| ) | ||
| if ( | ||
| execute | ||
| and target_state is GoalActivationState.STOPPED | ||
| and changed | ||
| and payload.get("ok") | ||
| and payload.get("written") | ||
| and closeout_goal is not None | ||
| ): | ||
| payload["closeout"] = _stop_closeout_payload( | ||
| source_registry=source_registry, | ||
| runtime_root_override=runtime_root_override, | ||
| goal_id=normalized_goal_id, | ||
| closeout_goal=closeout_goal, | ||
| ) | ||
| return payload | ||
|
|
||
|
|
||
| def _stop_closeout_payload( | ||
| *, | ||
| source_registry: Path, | ||
| runtime_root_override: str | None, | ||
| goal_id: str, | ||
| closeout_goal: Mapping[str, Any], | ||
| ) -> dict[str, Any]: | ||
| """Run the terminal hook after a committed stop transition.""" | ||
|
|
||
| from ...lifecycle.goal_closeout import run_goal_closeout | ||
|
|
||
| closeout_runtime_root = resolve_runtime_root( | ||
| load_registry(source_registry), | ||
| runtime_root_override, | ||
| registry_path=source_registry, | ||
| ) | ||
| state_file = closeout_goal.get("state_file") | ||
| if not state_file: | ||
| state_file = ( | ||
| source_registry.parent | ||
| / "goals" | ||
| / goal_id | ||
| / "ACTIVE_GOAL_STATE.md" | ||
| ) | ||
| return run_goal_closeout( | ||
| runtime_root=closeout_runtime_root, | ||
| goal_id=goal_id, | ||
| repo=str(closeout_goal.get("repo")) if closeout_goal.get("repo") else None, | ||
| state_file=str(state_file), | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [P2] 使用注册表解析后的绝对状态路径 相对 |
||
| terminal_reason="stopped", | ||
| trigger="goal-lifecycle-stop", | ||
| ) | ||
|
|
||
|
|
||
| def render_goal_activation_markdown(payload: dict[str, Any]) -> str: | ||
| lines = [ | ||
| "# Goal Activation", | ||
|
|
||
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
Empty file.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[P2] 保留已提交的停止回执
这个
load_registry发生在两份注册表都读回stopped之后。用相同 fixture 只让这次读失败,基线退出 0,当前 head 退出 1 并报告written=false,但写入已经完成。空配置也会进入这里。请从已验证的快照取所需参数,并把剩余准备异常记入钩子诊断。回归用例应在真实goal-lifecycle --operation stop --execute上同时断言退出码、written和两份持久状态。