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
46 changes: 45 additions & 1 deletion plugins/data-machine/inc/Core/Agents/ChatPersonResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public function resolve( string $agent_slug, string $platform, string $actor_id,
'name' => self::CURRENT_USER_SECTION,
'layer' => 'runtime',
'priority' => 0,
'content' => sprintf( 'The current speaker is WordPress user ID %d.', $user_id ),
'content' => self::current_speaker_line( $user_id ),
)
);
}
Expand Down Expand Up @@ -273,6 +273,50 @@ static function ( string $a, string $b ) use ( $candidates ): int {
return $sections;
}

/**
* Build the current-speaker line, including the speaker's live role
* display names on the current site.
*
* Resolved at turn time so it always reflects role changes; scaffolded
* USER.md files deliberately carry no role snapshot.
*
* @param int $user_id Speaker user ID.
* @return string
*/
private static function current_speaker_line( int $user_id ): string {
$roles = self::current_role_names( $user_id );
if ( empty( $roles ) ) {
return sprintf( 'The current speaker is WordPress user ID %d.', $user_id );
}

return sprintf( 'The current speaker is WordPress user ID %d (roles on this site: %s).', $user_id, implode( ', ', $roles ) );
}

/**
* Resolve a user's role display names on the current blog.
*
* @param int $user_id WordPress user ID.
* @return string[] Translated role display names; unknown role slugs fall back to the slug.
*/
private static function current_role_names( int $user_id ): array {
$user = get_userdata( $user_id );
if ( ! $user instanceof \WP_User || empty( $user->roles ) ) {
return array();
}

$role_names = wp_roles()->role_names;
$names = array();
foreach ( $user->roles as $slug ) {
$slug = (string) $slug;
if ( '' === $slug ) {
continue;
}
$names[] = isset( $role_names[ $slug ] ) ? translate_user_role( $role_names[ $slug ] ) : $slug;
}

return array_values( array_unique( $names ) );
}

/**
* Build a fail-closed result.
*
Expand Down
13 changes: 6 additions & 7 deletions plugins/data-machine/inc/setup/scaffolding.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,9 @@ function datamachine_get_scaffold_defaults( string $agent_name = '' ): array {
if ( $admin_email ) {
$user_lines[] = sprintf( '- **Email:** %s', $admin_email );
}
$user_lines[] = '- **Role:** Site Administrator';
$user_about = implode( "\n", $user_lines );
// No Role line: roles change after scaffolding, so a written snapshot goes
// stale. The live roles are supplied per turn by ChatPersonResolver.
$user_about = implode( "\n", $user_lines );

$user = implode(
"\n",
Expand Down Expand Up @@ -375,11 +376,9 @@ function datamachine_scaffold_user_content( string $content, string $filename, a
$about_lines[] = sprintf( '- **Name:** %s', $user->display_name );
$about_lines[] = sprintf( '- **Username:** %s', $user->user_login );

$roles = $user->roles;
if ( ! empty( $roles ) ) {
$role_name = ucfirst( reset( $roles ) );
$about_lines[] = sprintf( '- **Role:** %s', $role_name );
}
// Roles are intentionally not written here: a scaffold-time snapshot goes
// stale when the user's roles change. ChatPersonResolver injects the live
// roles on the current site into each turn instead.

if ( ! empty( $user->user_registered ) ) {
$registered = wp_date( 'F Y', strtotime( $user->user_registered ) );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,50 @@ public function test_viewer_turn_gets_only_own_personal_memory(): void {
$this->assertStringContainsString( (string) $this->viewer_id, $first['content'] );
}

public function test_current_speaker_section_carries_live_role_display_names(): void {
$this->grant( $this->viewer_id, 'viewer' );
ChatIdentityLinks::link( $this->viewer_id, self::PLATFORM, 'v' );

$content = $this->resolve( 'v', 'turn' )['sections'][0]['content'];
$this->assertSame(
sprintf( 'The current speaker is WordPress user ID %d (roles on this site: Subscriber).', $this->viewer_id ),
$content
);

// A role change is reflected on the next turn without any file edit.
$user = new \WP_User( $this->viewer_id );
$user->set_role( 'editor' );
$user->add_role( 'author' );

$content = $this->resolve( 'v', 'turn' )['sections'][0]['content'];
$this->assertSame(
sprintf( 'The current speaker is WordPress user ID %d (roles on this site: Editor, Author).', $this->viewer_id ),
$content
);
$this->assertStringNotContainsString( 'Subscriber', $content );
}

public function test_current_speaker_section_omits_roles_when_user_has_none(): void {
$this->grant( $this->viewer_id, 'viewer' );
ChatIdentityLinks::link( $this->viewer_id, self::PLATFORM, 'v' );
( new \WP_User( $this->viewer_id ) )->set_role( '' );

$this->assertSame(
sprintf( 'The current speaker is WordPress user ID %d.', $this->viewer_id ),
$this->resolve( 'v', 'turn' )['sections'][0]['content']
);
}

public function test_scaffolded_user_md_has_no_role_snapshot(): void {
$content = datamachine_scaffold_user_content( '', 'USER.md', array( 'user_id' => $this->viewer_id ) );

$this->assertStringContainsString( '# User Profile', $content );
$this->assertStringNotContainsString( '**Role:**', $content );

$defaults = datamachine_get_scaffold_defaults();
$this->assertStringNotContainsString( '**Role:**', $defaults['USER.md'] );
}

public function test_turn_never_includes_another_users_memory(): void {
$this->grant( $this->viewer_id, 'viewer' );
$this->grant( $this->other_id, 'viewer' );
Expand Down
Loading