-
Notifications
You must be signed in to change notification settings - Fork 0
Store immutable public responses for owner-scoped recovery #13
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
dispatch-developer
wants to merge
2
commits into
main
Choose a base branch
from
issue-9-stored-responses
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.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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,112 @@ | ||
| ALTER TABLE continuations ADD COLUMN owner_ref text; | ||
| ALTER TABLE continuations ADD COLUMN turn_id text REFERENCES turns(id) ON DELETE CASCADE; | ||
|
|
||
| UPDATE continuations continuation | ||
| SET owner_ref = conversation.owner_ref | ||
| FROM conversations conversation | ||
| WHERE conversation.id = continuation.conversation_id; | ||
|
|
||
| ALTER TABLE continuations ALTER COLUMN owner_ref SET NOT NULL; | ||
| ALTER TABLE continuations | ||
| DROP CONSTRAINT continuations_tenant_id_agent_ref_response_id_key; | ||
| ALTER TABLE continuations | ||
| ADD CONSTRAINT continuations_tenant_owner_agent_response_key | ||
| UNIQUE (tenant_id, owner_ref, agent_ref, response_id); | ||
|
|
||
| ALTER TABLE conversations | ||
| ADD CONSTRAINT conversations_id_tenant_owner_key | ||
| UNIQUE (id, tenant_id, owner_ref); | ||
| ALTER TABLE turns | ||
| ADD CONSTRAINT turns_id_conversation_agent_key | ||
| UNIQUE (id, conversation_id, agent_ref); | ||
| ALTER TABLE continuations | ||
| ADD CONSTRAINT continuations_identity_link_key | ||
| UNIQUE (id, tenant_id, owner_ref, conversation_id, turn_id, agent_ref, | ||
| response_id, through_seq); | ||
| ALTER TABLE continuations | ||
| ADD CONSTRAINT continuations_owned_conversation_fkey | ||
| FOREIGN KEY (conversation_id, tenant_id, owner_ref) | ||
| REFERENCES conversations (id, tenant_id, owner_ref) ON DELETE CASCADE; | ||
| ALTER TABLE continuations | ||
| ADD CONSTRAINT continuations_turn_link_fkey | ||
| FOREIGN KEY (turn_id, conversation_id, agent_ref) | ||
| REFERENCES turns (id, conversation_id, agent_ref) ON DELETE CASCADE; | ||
|
|
||
| CREATE INDEX continuations_owner_agent_response_idx | ||
| ON continuations (tenant_id, owner_ref, agent_ref, response_id); | ||
|
|
||
| CREATE TABLE stored_responses ( | ||
| id text PRIMARY KEY, | ||
| tenant_id text NOT NULL, | ||
| owner_ref text NOT NULL, | ||
| agent_ref text NOT NULL, | ||
| conversation_id text NOT NULL REFERENCES conversations(id) ON DELETE CASCADE, | ||
| turn_id text NOT NULL REFERENCES turns(id) ON DELETE CASCADE, | ||
| continuation_id text NOT NULL UNIQUE REFERENCES continuations(id) ON DELETE CASCADE, | ||
| response_id text NOT NULL, | ||
| previous_response_id text, | ||
| terminal_status text NOT NULL | ||
| CHECK (terminal_status IN ('completed', 'incomplete', 'failed', 'cancelled')), | ||
| public_response jsonb NOT NULL CHECK (jsonb_typeof(public_response) = 'object'), | ||
| public_response_text text NOT NULL | ||
| CHECK (octet_length(public_response_text) BETWEEN 2 AND 1048576), | ||
| canonical_digest bytea NOT NULL CHECK (octet_length(canonical_digest) = 32), | ||
| schema_marker text NOT NULL, | ||
| canonical_size bigint NOT NULL CHECK (canonical_size BETWEEN 2 AND 1048576), | ||
| through_seq bigint NOT NULL CHECK (through_seq >= 0), | ||
| response_created_at timestamptz NOT NULL, | ||
| terminal_at timestamptz NOT NULL, | ||
| created_at timestamptz NOT NULL DEFAULT now(), | ||
| CHECK (terminal_at >= response_created_at), | ||
| UNIQUE (tenant_id, owner_ref, agent_ref, response_id) | ||
| ); | ||
|
|
||
| ALTER TABLE stored_responses | ||
| ADD CONSTRAINT stored_responses_owned_conversation_fkey | ||
| FOREIGN KEY (conversation_id, tenant_id, owner_ref) | ||
| REFERENCES conversations (id, tenant_id, owner_ref) ON DELETE CASCADE; | ||
| ALTER TABLE stored_responses | ||
| ADD CONSTRAINT stored_responses_turn_link_fkey | ||
| FOREIGN KEY (turn_id, conversation_id, agent_ref) | ||
| REFERENCES turns (id, conversation_id, agent_ref) ON DELETE CASCADE; | ||
| ALTER TABLE stored_responses | ||
| ADD CONSTRAINT stored_responses_continuation_link_fkey | ||
| FOREIGN KEY (continuation_id, tenant_id, owner_ref, conversation_id, turn_id, | ||
| agent_ref, response_id, through_seq) | ||
| REFERENCES continuations | ||
| (id, tenant_id, owner_ref, conversation_id, turn_id, agent_ref, | ||
| response_id, through_seq) ON DELETE CASCADE; | ||
|
|
||
| CREATE INDEX stored_responses_conversation_turn_idx | ||
| ON stored_responses (conversation_id, turn_id); | ||
| CREATE INDEX stored_responses_turn_idx ON stored_responses (turn_id); | ||
|
|
||
| CREATE FUNCTION reject_stored_response_update() RETURNS trigger | ||
| LANGUAGE plpgsql AS $$ | ||
| BEGIN | ||
| RAISE EXCEPTION 'terminal public responses are immutable' | ||
| USING ERRCODE = '55000'; | ||
| END; | ||
| $$; | ||
|
|
||
| CREATE TRIGGER stored_responses_immutable | ||
| BEFORE UPDATE ON stored_responses | ||
| FOR EACH ROW EXECUTE FUNCTION reject_stored_response_update(); | ||
|
|
||
| CREATE FUNCTION reject_stored_response_turn_rewrite() RETURNS trigger | ||
| LANGUAGE plpgsql AS $$ | ||
| BEGIN | ||
| IF EXISTS (SELECT 1 FROM stored_responses WHERE turn_id = OLD.id) | ||
| AND (NEW.status, NEW.response_id, NEW.error, NEW.usage, NEW.completed_at) | ||
| IS DISTINCT FROM | ||
| (OLD.status, OLD.response_id, OLD.error, OLD.usage, OLD.completed_at) THEN | ||
| RAISE EXCEPTION 'a turn with a stored terminal response is immutable' | ||
| USING ERRCODE = '55000'; | ||
| END IF; | ||
| RETURN NEW; | ||
| END; | ||
| $$; | ||
|
|
||
| CREATE TRIGGER turns_stored_response_immutable | ||
| BEFORE UPDATE ON turns | ||
| FOR EACH ROW EXECUTE FUNCTION reject_stored_response_turn_rewrite(); |
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
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.
Blocking: CI for
648d864debdf67f81d42d3ec9787651e267e327cfailed atcargo fmt --check(with formatting diffs insrc/api.rsandsrc/store.rs), so Clippy and tests were skipped. Runcargo fmtand push the resulting changes, then let the full CI suite complete successfully.