Skip to content
Draft
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
696 changes: 591 additions & 105 deletions apps/agent/src/plugins/control.ts

Large diffs are not rendered by default.

607 changes: 607 additions & 0 deletions apps/agent/src/plugins/control/phone-use.ts

Large diffs are not rendered by default.

267 changes: 267 additions & 0 deletions apps/agent/src/plugins/control/profiles.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,267 @@
// spec/app/control/profile.md, "Story: Apply and restore reversible OS profiles while preserving manual edits"

/** Typed OS setting keys managed by reversible OS profiles. */
export const PROFILE_SETTING_KEYS = [
"dnd_mode",
"notification_silence",
"screen_timeout_seconds",
"home_shortcuts",
"media_volume",
"auto_brightness",
] as const;

/** Typed OS setting key union. */
export type ProfileSettingKey = (typeof PROFILE_SETTING_KEYS)[number];

const SETTING_KEY_SET: ReadonlySet<string> = new Set(PROFILE_SETTING_KEYS);

/** Named reversible OS profile target specification. */
export interface ReversibleProfileSpec {
readonly profileId: string;
readonly displayName: string;
readonly targetValues: Readonly<Partial<Record<ProfileSettingKey, string>>>;
}

/** Single setting diff computed during a pure preview. */
export interface ProfileSettingChange {
readonly key: ProfileSettingKey;
readonly beforeValue: string | undefined;
readonly targetValue: string;
}

/** Pure dry-run preview of a reversible OS profile. */
export interface ProfilePreview {
readonly profileId: string;
readonly displayName: string;
readonly changes: readonly ProfileSettingChange[];
readonly unchangedKeys: readonly ProfileSettingKey[];
}

/** Attribution record for one setting written by an applied profile. */
export interface ProfileReceiptEntry {
readonly key: ProfileSettingKey;
readonly baselineValue: string | undefined;
readonly appliedValue: string;
}

/** Immutable receipt created when applying a reversible OS profile. */
export interface ProfileReceipt {
readonly receiptId: string;
readonly profileId: string;
readonly appliedAtMs: number;
readonly expiresAtMs: number;
readonly entries: readonly ProfileReceiptEntry[];
readonly restored: boolean;
}

/** Record of a manual user edit preserved untouched during restore. */
export interface PreservedManualEdit {
readonly key: ProfileSettingKey;
readonly baselineValue: string | undefined;
readonly profileAppliedValue: string;
readonly currentUserValue: string | undefined;
}

/** Structured outcome returned when restoring a profile receipt. */
export interface ProfileRestoreResult {
readonly ok: boolean;
readonly receiptId: string;
readonly profileId: string;
readonly alreadyRestored: boolean;
readonly restoredKeys: readonly ProfileSettingKey[];
readonly preservedManualEdits: readonly PreservedManualEdit[];
}

/** Whether `key` is a recognized reversible OS profile setting key. */
export function isProfileSettingKey(key: string): key is ProfileSettingKey {
return SETTING_KEY_SET.has(key);
}

/** Resolve a built-in reversible OS profile preset ("focus" or "recording"). */
export function builtInProfileSpec(profileId: string): ReversibleProfileSpec | undefined {
switch (profileId.trim().toLowerCase()) {
case "focus":
return {
profileId: "focus",
displayName: "Focus Mode",
targetValues: {
dnd_mode: "priority",
notification_silence: "enabled",
},
};
case "recording":
return {
profileId: "recording",
displayName: "Recording Mode",
targetValues: {
dnd_mode: "alarms_only",
notification_silence: "enabled",
screen_timeout_seconds: "600",
home_shortcuts: "recording_pack",
},
};
default:
return undefined;
}
}

/**
* In-memory typed read -> preview -> apply -> restore receipt controller for reversible OS
* profiles. Restores ONLY settings whose current live value still matches what the profile wrote,
* idempotently preserving any manual owner edits made mid-profile.
*/
export class ReversibleProfileStore {
private readonly live = new Map<ProfileSettingKey, string>();
private readonly receipts = new Map<string, ProfileReceipt>();
private readonly restoreHistory = new Map<string, ProfileRestoreResult>();
private nextReceiptSeq = 0;

constructor(initial?: Readonly<Partial<Record<ProfileSettingKey, string>>>) {
if (initial !== undefined) {
for (const key of PROFILE_SETTING_KEYS) {
const val = initial[key];
if (val !== undefined) this.live.set(key, val);
}
}
}

/** Stage 1 (Read): Read current live values without mutating state. */
readCurrent(keys: readonly ProfileSettingKey[] = PROFILE_SETTING_KEYS): Readonly<Partial<Record<ProfileSettingKey, string>>> {
const out: Partial<Record<ProfileSettingKey, string>> = {};
for (const key of keys) {
const value = this.live.get(key);
if (value !== undefined) out[key] = value;
}
return out;
}

/** Record a live manual edit made by the owner or synced from Android settings. */
manualEdit(key: ProfileSettingKey, value: string | undefined): void {
if (value === undefined) {
this.live.delete(key);
} else {
this.live.set(key, value);
}
}

/** Stage 2 (Preview): Pure dry-run diff showing exact changes with zero side effects. */
preview(spec: ReversibleProfileSpec): ProfilePreview {
const changes: ProfileSettingChange[] = [];
const unchangedKeys: ProfileSettingKey[] = [];
for (const key of PROFILE_SETTING_KEYS) {
const target = spec.targetValues[key];
if (target === undefined) continue;
const before = this.live.get(key);
if (before === target) {
unchangedKeys.push(key);
} else {
changes.push({ key, beforeValue: before, targetValue: target });
}
}
return {
profileId: spec.profileId,
displayName: spec.displayName,
changes,
unchangedKeys,
};
}

/** Stage 3 (Apply): Write only changed settings and emit an immutable attribution receipt. */
apply(spec: ReversibleProfileSpec, nowMs: number, durationMs = 0): ProfileReceipt {
const entries: ProfileReceiptEntry[] = [];
for (const key of PROFILE_SETTING_KEYS) {
const target = spec.targetValues[key];
if (target === undefined) continue;
const baseline = this.live.get(key);
if (baseline !== target) {
this.live.set(key, target);
entries.push({ key, baselineValue: baseline, appliedValue: target });
}
}
this.nextReceiptSeq += 1;
const receipt: ProfileReceipt = {
receiptId: `profile-${this.nextReceiptSeq}`,
profileId: spec.profileId,
appliedAtMs: nowMs,
expiresAtMs: durationMs > 0 ? nowMs + durationMs : 0,
entries,
restored: false,
};
this.receipts.set(receipt.receiptId, receipt);
return receipt;
}

/**
* Stage 4 (Restore): Idempotently revert ONLY settings still holding the profile's applied
* value, preserving every manual edit made mid-profile.
*/
restore(receiptId: string): ProfileRestoreResult {
const existing = this.receipts.get(receiptId);
if (existing === undefined) {
return {
ok: false,
receiptId,
profileId: "",
alreadyRestored: false,
restoredKeys: [],
preservedManualEdits: [],
};
}
const priorRestore = this.restoreHistory.get(receiptId);
if (existing.restored && priorRestore !== undefined) {
return { ...priorRestore, alreadyRestored: true };
}
const restoredKeys: ProfileSettingKey[] = [];
const preservedManualEdits: PreservedManualEdit[] = [];
for (const entry of existing.entries) {
const currentValue = this.live.get(entry.key);
if (currentValue === entry.appliedValue) {
if (entry.baselineValue === undefined) {
this.live.delete(entry.key);
} else {
this.live.set(entry.key, entry.baselineValue);
}
restoredKeys.push(entry.key);
} else {
preservedManualEdits.push({
key: entry.key,
baselineValue: entry.baselineValue,
profileAppliedValue: entry.appliedValue,
currentUserValue: currentValue,
});
}
}
this.receipts.set(receiptId, { ...existing, restored: true });
const result: ProfileRestoreResult = {
ok: true,
receiptId,
profileId: existing.profileId,
alreadyRestored: false,
restoredKeys,
preservedManualEdits,
};
this.restoreHistory.set(receiptId, result);
return result;
}

/** Restore every active receipt whose expiry timestamp is at or before `nowMs`. */
restoreExpired(nowMs: number): readonly ProfileRestoreResult[] {
const dueIds: string[] = [];
for (const receipt of this.receipts.values()) {
if (!receipt.restored && receipt.expiresAtMs > 0 && nowMs >= receipt.expiresAtMs) {
dueIds.push(receipt.receiptId);
}
}
return dueIds.map((id) => this.restore(id));
}

/** List active (unrestored) profile receipts. */
activeReceipts(): readonly ProfileReceipt[] {
return [...this.receipts.values()].filter((receipt) => !receipt.restored);
}

/** Return a specific receipt by id, or undefined. */
getReceipt(receiptId: string): ProfileReceipt | undefined {
return this.receipts.get(receiptId);
}
}
Loading
Loading