diff --git a/php/Models/WorkspaceState.php b/php/Models/WorkspaceState.php index 5b60e38..a8eac3a 100644 --- a/php/Models/WorkspaceState.php +++ b/php/Models/WorkspaceState.php @@ -92,18 +92,20 @@ class WorkspaceState extends Model } public static function getOrCreate( - AgentPlan $plan, + AgentPlan|int $plan, string $key, mixed $default = null, string $type = self::TYPE_JSON ): self { + $planId = static::planId($plan); + return static::firstOrCreate( - ['agent_plan_id' => $plan->id, 'key' => $key], + ['agent_plan_id' => $planId, 'key' => $key], ['value' => $default, 'type' => $type] ); } - public static function getValue(AgentPlan $plan, string $key, mixed $default = null): mixed + public static function getValue(AgentPlan|int $plan, string $key, mixed $default = null): mixed { $state = static::forPlan($plan)->where('key', $key)->first(); @@ -111,17 +113,37 @@ class WorkspaceState extends Model } public static function setValue( - AgentPlan $plan, + AgentPlan|int $plan, string $key, mixed $value, string $type = self::TYPE_JSON ): self { + $planId = static::planId($plan); + return static::updateOrCreate( - ['agent_plan_id' => $plan->id, 'key' => $key], + ['agent_plan_id' => $planId, 'key' => $key], ['value' => $value, 'type' => $type] ); } + /** + * WorkspaceState::set($plan->id, 'discovered_pattern', 'observer'); + * WorkspaceState::get($plan->id, 'discovered_pattern'); + */ + public static function set( + AgentPlan|int $plan, + string $key, + mixed $value, + string $type = self::TYPE_JSON + ): self { + return static::setValue($plan, $key, $value, $type); + } + + public static function get(AgentPlan|int $plan, string $key, mixed $default = null): mixed + { + return static::getValue($plan, $key, $default); + } + public function setTypedValue(mixed $value): void { $this->update(['value' => $value]); @@ -143,4 +165,9 @@ class WorkspaceState extends Model 'updated_at' => $this->updated_at?->toIso8601String(), ]; } + + private static function planId(AgentPlan|int $plan): int + { + return $plan instanceof AgentPlan ? $plan->id : $plan; + } } diff --git a/php/tests/Feature/WorkspaceStateTest.php b/php/tests/Feature/WorkspaceStateTest.php index 1733294..cf6d2db 100644 --- a/php/tests/Feature/WorkspaceStateTest.php +++ b/php/tests/Feature/WorkspaceStateTest.php @@ -236,6 +236,26 @@ class WorkspaceStateTest extends TestCase $this->assertEquals(['n' => 2], WorkspaceState::getValue($this->plan, 'counter')); } + public function test_set_alias_creates_new_state(): void + { + $state = WorkspaceState::set($this->plan->id, 'discovered_pattern', 'observer'); + + $this->assertDatabaseHas('agent_workspace_states', [ + 'agent_plan_id' => $this->plan->id, + 'key' => 'discovered_pattern', + ]); + $this->assertEquals('observer', $state->value); + } + + public function test_get_alias_returns_stored_value(): void + { + WorkspaceState::set($this->plan->id, 'discovered_pattern', 'observer'); + + $value = WorkspaceState::get($this->plan->id, 'discovered_pattern'); + + $this->assertEquals('observer', $value); + } + // ========================================================================= // MCP output // =========================================================================