Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
137 changes: 122 additions & 15 deletions inc/Runtime/AgentsMdSections.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,7 @@ public static function register(): void {
}

$registry_class = '\DataMachine\Engine\AI\MemoryFileRegistry';
if ( ! is_callable(array( $registry_class, 'register' )) ) {
return;
}
$register = array( $registry_class, 'register' );
$register = array( $registry_class, 'register' );
/** @var callable $register */

call_user_func(
Expand Down Expand Up @@ -149,6 +146,7 @@ private static function render_workspace_policy_intro( string $workspace_path ):
* @param string $default Default policy markdown.
* @param string $workspace_path Resolved DMC workspace root.
*/
/** @var mixed $filtered */
$filtered = apply_filters('datamachine_code_workspace_policy_intro', $default, $workspace_path);
if ( ! is_string($filtered) ) {
return $default;
Expand All @@ -175,6 +173,7 @@ private static function render_workspace_policy_section( string $workspace_path,
* @param string $workspace_path Resolved DMC workspace root.
* @param string $wp WP-CLI command prefix.
*/
/** @var mixed $filtered */
$filtered = apply_filters('datamachine_code_workspace_policy_section', $default, $workspace_path, $wp);
if ( ! is_string($filtered) ) {
return $default;
Expand Down Expand Up @@ -288,10 +287,7 @@ private static function resolve_wp_cli_cmd(): string {
*/
private static function register_section( string $file, string $section, int $priority, callable $callback, array $metadata ): void {
$registry_class = '\DataMachine\Engine\AI\SectionRegistry';
if ( ! is_callable(array( $registry_class, 'register' )) ) {
return;
}
$register = array( $registry_class, 'register' );
$register = array( $registry_class, 'register' );
/** @var callable $register */

call_user_func($register, $file, $section, $priority, $callback, $metadata);
Expand Down Expand Up @@ -374,21 +370,32 @@ private static function render_workspace_inventory_section( string $wp ): string
$mode = 'compact';
}

$lines = array();
$lines = array();
$attention_lines = array();
foreach ( $by_repo as $repo => $bucket ) {
$primary = $bucket['primary'];
$primary = is_array($bucket['primary']) ? $bucket['primary'] : array();
$worktrees = $bucket['worktrees'];
$wt_count = count($worktrees);
$branch = $primary['branch'] ?? null;
$remote = $primary['remote'] ?? null;
$branch_str = ( null !== $branch && '' !== $branch ) ? sprintf(' (`%s`)', $branch) : '';
$freshness = is_array($primary['primary_freshness'] ?? null) ? $primary['primary_freshness'] : null;

$attention = self::format_primary_freshness_attention($repo, $freshness);
if ( '' !== $attention ) {
$attention_lines[] = $attention;
}

if ( 'compact' === $mode ) {
$suffix_parts = array();
$suffix_parts[] = sprintf('%d %s', $wt_count, 1 === $wt_count ? 'worktree' : 'worktrees');
if ( null !== $remote && '' !== $remote ) {
$suffix_parts[] = $remote;
}
$freshness_badge = self::format_primary_freshness_badge($freshness);
if ( '' !== $freshness_badge ) {
$suffix_parts[] = $freshness_badge;
}
$lines[] = sprintf('- **%s**%s — %s', $repo, $branch_str, implode(' · ', $suffix_parts));
continue;
}
Expand All @@ -397,6 +404,10 @@ private static function render_workspace_inventory_section( string $wp ): string
if ( null !== $remote && '' !== $remote ) {
$header .= ' — ' . $remote;
}
$freshness_badge = self::format_primary_freshness_badge($freshness);
if ( '' !== $freshness_badge ) {
$header .= ' · ' . $freshness_badge;
}
$lines[] = $header;

usort(
Expand All @@ -415,11 +426,12 @@ private static function render_workspace_inventory_section( string $wp ): string
}
}

$body = implode("\n", $lines);
$generated_at = gmdate('c');
$workspace_path = $listing['path'];
$agent_slug = self::resolve_agent_slug();
$agent_suffix = '' !== $agent_slug ? ' --agent=' . $agent_slug : '';
$body = implode("\n", $lines);
$attention_block = self::render_primary_freshness_attention_block($attention_lines);
$generated_at = gmdate('c');
$workspace_path = $listing['path'];
$agent_slug = self::resolve_agent_slug();
$agent_suffix = '' !== $agent_slug ? ' --agent=' . $agent_slug : '';

return <<<MD
## Workspace Inventory
Expand All @@ -428,6 +440,101 @@ private static function render_workspace_inventory_section( string $wp ): string

Refresh this file with `{$wp} datamachine memory compose AGENTS.md{$agent_suffix}` after workspace changes if the inventory looks stale.

{$attention_block}

{$body}
MD;
}

private static function primary_freshness_needs_attention( ?array $freshness ): bool {
if ( null === $freshness ) {
return false;
}

$status = (string) ( $freshness['status'] ?? '' );
return in_array($status, array( 'stale', 'diverged', 'detached', 'unknown', 'no_upstream', 'ahead' ), true);
}

private static function primary_freshness_needs_refresh( ?array $freshness ): bool {
if ( null === $freshness ) {
return false;
}

$status = (string) ( $freshness['status'] ?? '' );
return in_array($status, array( 'stale', 'diverged' ), true);
}

private static function format_primary_freshness_badge( ?array $freshness ): string {
if ( ! self::primary_freshness_needs_attention($freshness) ) {
return '';
}

$status = (string) ( $freshness['status'] ?? 'unknown' );
$parts = array( 'primary ' . $status );
if ( isset($freshness['behind']) && is_numeric($freshness['behind']) && (int) $freshness['behind'] > 0 ) {
$parts[] = sprintf('behind %d', (int) $freshness['behind']);
}
if ( isset($freshness['ahead']) && is_numeric($freshness['ahead']) && (int) $freshness['ahead'] > 0 ) {
$parts[] = sprintf('ahead %d', (int) $freshness['ahead']);
}

return implode(', ', $parts);
}

private static function format_primary_freshness_attention( string $repo, ?array $freshness ): string {
if ( ! self::primary_freshness_needs_attention($freshness) ) {
return '';
}

$status = (string) ( $freshness['status'] ?? 'unknown' );
$branch = (string) ( $freshness['branch'] ?? '' );
$upstream = (string) ( $freshness['upstream'] ?? '' );
$details = array();
if ( '' !== $branch ) {
$details[] = sprintf('branch `%s`', $branch);
}
if ( '' !== $upstream ) {
$details[] = sprintf('upstream `%s`', $upstream);
}
if ( isset($freshness['behind']) && is_numeric($freshness['behind']) ) {
$details[] = sprintf('behind %d', (int) $freshness['behind']);
}
if ( isset($freshness['ahead']) && is_numeric($freshness['ahead']) ) {
$details[] = sprintf('ahead %d', (int) $freshness['ahead']);
}

$line = sprintf('- **%s** primary is `%s`', $repo, $status);
if ( ! empty($details) ) {
$line .= ' (' . implode(', ', $details) . ')';
}

$command = (string) ( $freshness['suggested_command'] ?? '' );
if ( '' !== $command && self::primary_freshness_needs_refresh($freshness) ) {
$line .= sprintf('. Refresh: `%s`', $command);
}

return $line . '.';
}

private static function render_primary_freshness_attention_block( array $attention_lines ): string {
if ( empty($attention_lines) ) {
return '';
}

$max_lines = 20;
$shown = array_slice($attention_lines, 0, $max_lines);
$omitted = count($attention_lines) - count($shown);
if ( $omitted > 0 ) {
$shown[] = sprintf('- %d more primary checkout(s) need attention; run `wp datamachine-code workspace list` for the full set.', $omitted);
}

$body = implode("\n", $shown);

return <<<MD
**Primary Checkout Attention**

These primary checkouts may be stale or unsafe to read. Refresh them or create a worktree from an explicit remote ref before using them as source evidence.

{$body}
MD;
}
Expand Down
4 changes: 2 additions & 2 deletions inc/Workspace/WorkspaceCoreUtilities.php
Original file line number Diff line number Diff line change
Expand Up @@ -1091,7 +1091,7 @@ private function protect_directory( string $path ): void {
* The payload mirrors the workspace `name`/`repo` taxonomy used elsewhere
* in this class:
*
* - `op`: one of `clone`, `adopt`, `remove`, `worktree_add`, `worktree_remove`.
* - `op`: one of `clone`, `adopt`, `remove`, `primary_refresh`, `worktree_add`, `worktree_remove`.
* - `repo`: bare repository name (no `@<slug>` suffix).
* - `name`: workspace entry name on disk (`<repo>` for primaries,
* `<repo>@<slug>` for worktrees).
Expand All @@ -1112,7 +1112,7 @@ protected function emit_workspace_changed( string $op, string $repo, string $nam
* @since 0.31.0
*
* @param array{op: string, repo: string, name: string, path: string} $payload {
* @type string $op One of clone|adopt|remove|worktree_add|worktree_remove.
* @type string $op One of clone|adopt|remove|primary_refresh|worktree_add|worktree_remove.
* @type string $repo Bare repository name (no @-suffix).
* @type string $name Workspace entry name (<repo> or <repo>@<slug>).
* @type string $path Absolute path of the workspace entry.
Expand Down
4 changes: 4 additions & 0 deletions inc/Workspace/WorkspaceGitOperations.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,10 @@ public function git_pull( string $handle, bool $allow_dirty = false, bool $allow
return $result;
}

if ( empty($parsed['is_worktree']) ) {
$this->emit_workspace_changed('primary_refresh', $parsed['repo'], $parsed['dir_name'], $repo_path);
}

return array(
'success' => true,
'message' => trim( (string) $result['output']),
Expand Down
129 changes: 129 additions & 0 deletions tests/smoke-agents-md-workspace-freshness.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
<?php

declare(strict_types=1);

namespace DataMachine\Engine\AI {
final class MemoryFileRegistry {
public const LAYER_SHARED = 'shared';

public static function register( string $file, int $priority, array $metadata ): void {}
}

final class SectionRegistry {
public static array $sections = array();

public static function register( string $file, string $section, int $priority, callable $callback, array $metadata ): void {
self::$sections[ $section ] = compact('file', 'section', 'priority', 'callback', 'metadata');
}
}
}

namespace DataMachineCode\Workspace {
final class Workspace {
public function get_path(): string {
return '/tmp/dmc-workspace';
}

public function list_repos(): array {
return array(
'success' => true,
'path' => '/tmp/dmc-workspace',
'repos' => array(
array(
'name' => 'current-repo',
'repo' => 'current-repo',
'git' => true,
'is_worktree' => false,
'branch' => 'main',
'remote' => 'https://example.com/current-repo.git',
'primary_freshness' => array(
'status' => 'current',
'branch' => 'main',
'upstream' => 'origin/main',
'behind' => 0,
'ahead' => 0,
),
),
array(
'name' => 'stale-repo',
'repo' => 'stale-repo',
'git' => true,
'is_worktree' => false,
'branch' => 'trunk',
'remote' => 'https://example.com/stale-repo.git',
'primary_freshness' => array(
'status' => 'stale',
'branch' => 'trunk',
'upstream' => 'origin/trunk',
'behind' => 7,
'ahead' => 0,
'suggested_command' => 'wp datamachine-code workspace git pull stale-repo --allow-primary-refresh',
),
),
array(
'name' => 'stale-repo@fix-example',
'repo' => 'stale-repo',
'git' => true,
'is_worktree' => true,
'branch_slug' => 'fix-example',
'branch' => 'fix/example',
),
),
);
}
}
}

namespace {
if ( ! defined('ABSPATH') ) {
define('ABSPATH', '/var/www/html');
}

function datamachine_agents_md_enabled(): bool {
return true;
}

function is_multisite(): bool {
return false;
}

function apply_filters( string $hook_name, mixed $value, mixed ...$args ): mixed {
return $value;
}

function is_wp_error( mixed $thing ): bool {
return false;
}

function assert_contains( string $needle, string $haystack, string $message ): void {
if ( ! str_contains($haystack, $needle) ) {
throw new RuntimeException($message);
}
}

function assert_not_contains( string $needle, string $haystack, string $message ): void {
if ( str_contains($haystack, $needle) ) {
throw new RuntimeException($message);
}
}

require_once dirname(__DIR__) . '/inc/Runtime/CommandIntrospector.php';
require_once dirname(__DIR__) . '/inc/Runtime/AgentsMdSections.php';

\DataMachineCode\Runtime\AgentsMdSections::register();

$sections = \DataMachine\Engine\AI\SectionRegistry::$sections;
if ( ! isset($sections['workspace-inventory']) ) {
throw new RuntimeException('workspace-inventory section was not registered');
}

$rendered = $sections['workspace-inventory']['callback']();

assert_contains('**Primary Checkout Attention**', $rendered, 'primary freshness attention block missing');
assert_contains('These primary checkouts may be stale or unsafe to read.', $rendered, 'primary freshness guidance missing');
assert_contains('- **stale-repo** primary is `stale` (branch `trunk`, upstream `origin/trunk`, behind 7, ahead 0). Refresh: `wp datamachine-code workspace git pull stale-repo --allow-primary-refresh`.', $rendered, 'stale primary details missing');
assert_contains('- **stale-repo** (`trunk`) — 1 worktree · https://example.com/stale-repo.git · primary stale, behind 7', $rendered, 'compact stale primary badge missing');
assert_not_contains('- **current-repo** primary is `current`', $rendered, 'current primary should not appear in attention block');

fwrite(STDOUT, "agents-md workspace freshness smoke passed\n");
}
Loading
Loading