harbor run -t openthoughts/tasktrove-exp-flat25-pseudocode-v2-5k-pseudocode-2902harbor download openthoughts/tasktrove-exp-flat25-pseudocode-v2-5k-pseudocode-2902BEGIN PSEUDOCODE
CONSTANTS SCOPES := ["global", "host", "environment", "os", "appliance"] // shallow to deepest TYPE_ALIASES := { "attr": "attribute", "attribute": "attribute", "controller": "controller", "firewall": "firewall", "partition": "partition", "route": "route" } INTERNAL_TYPES := ["attribute", "controller", "firewall", "partition", "route"]
DATA STRUCTURES STORE: map<scope, map<type, map<name, Resource>>> // For each scope, per type, per resource name, store Resource // Resource := { type: string, scope: string, name: string, payload: object }
FUNCTION InitStore(): FOR each s IN SCOPES DO STORE[s] := empty map FOR each t IN INTERNAL_TYPES DO STORE[s][t] := empty map END FOR END FOR
FUNCTION NormalizeType(input_type: string) -> string t := LOWERCASE(input_type) IF t IN VALUES of TYPE_ALIASES THEN // Convert to canonical internal key canonical := TYPE_ALIASES[t] IF canonical IN INTERNAL_TYPES THEN RETURN canonical END IF END IF IF t IN INTERNAL_TYPES THEN RETURN t END IF RAISE ERROR "Unknown resource type"
FUNCTION ValidateScope(s: string) -> string IF s IS NULL OR s = "" THEN RETURN "appliance" // default to deepest scope when omitted (per spec: default to appliance) END IF sl := LOWERCASE(s) IF sl IN SCOPES THEN RETURN sl ELSE RAISE ERROR "Unknown scope" END IF
FUNCTION ResourceMatches(res: Resource, type: string, scope: string, name: string) -> boolean RETURN (res.type = type) AND (res.scope = scope) AND (res.name = name)
OPERATIONS
PROCEDURE LoadFromJSON(path: string) data := READ_FILE(path) IF data IS INVALID JSON THEN RAISE ERROR "Invalid JSON" END IF arr := PARSE_JSON(data) IF arr IS NOT LIST THEN RAISE ERROR "Expected a list (array) of resources" END IF FOR EACH item IN arr DO IF item IS NOT DICT THEN RAISE ERROR "Invalid resource entry" END IF
raw_type := item["type"] // required
raw_name := item["name"] // required
payload := item["payload"] // required
raw_scope := item["scope"] // optional
IF raw_type IS NULL OR raw_name IS NULL OR payload IS NULL THEN
RAISE ERROR "Missing required resource fields"
END IF
internal_type := NormalizeType(raw_type)
scope := ValidateScope(raw_scope)
name := STRING(raw_name) // ensure string
// Build resource
res := { type: internal_type, scope: scope, name: name, payload: payload }
// Overwrite semantics: insert or replace existing entry
STORE[scope][internal_type][name] := res
END FOR
END PROCEDURE
PROCEDURE ListResourcesFor(type_label: string, scope_label: string, format_label: string) // Resolve type internal_type := NormalizeType(type_label)
// Resolve scope (empty or omitted => global)
actual_scope := IF scope_label IS NULL OR scope_label = "" THEN "global" ELSE LOWERCASE(scope_label)
// Validate scope
IF actual_scope NOT IN SCOPES THEN
RAISE ERROR "Unknown scope for listing"
// Resolve starting index for scope-collapsed traversal
start_index := INDEX_OF(actual_scope IN SCOPES)
// Collect resources from actual_scope and all deeper scopes
results := empty LIST
FOR i FROM start_index TO LENGTH(SCOPES) - 1 DO
sc := SCOPES[i]
type_map := STORE[sc][internal_type]
IF type_map EXISTS THEN
// Preserve insertion order if possible
FOR EACH res IN VALUES(type_map) DO
APPEND res TO results
END FOR
END IF
END FOR
// Output format handling (default to JSON)
fmt := LOWERCASE(format_label)
IF fmt = "" OR fmt = "output-format=json" THEN
json_output := JSON_ENCODE(results)
PRINT_TO_STDOUT(json_output)
ELSE IF fmt = "raw" THEN
// Optional: define alternative formats if needed
PRINT_TO_STDOUT(STRINGIFY(results))
ELSE
RAISE ERROR "Unsupported output format"
END IF
END PROCEDURE
// TERMINAL I/O wrappers (pseudo) FUNCTION PRINT_TO_STDOUT(text: string) OUTPUT text TO STDOUT END FUNCTION
FUNCTION READ_FILE(path: string) -> string RETURN FILE_CONTENTS_AT(path) END FUNCTION
FUNCTION PARSE_JSON(text: string) -> any RETURN JSON_PARSE(text) END FUNCTION
FUNCTION JSON_ENCODE(value) -> string RETURN JSON_SERIALIZE(value, stable_order=true) END FUNCTION
FUNCTION INDEX_OF(item, list) -> int // Return index of item in list; assume item exists END FUNCTION
MAIN PROGRAM
CALL InitStore()
args := COMMAND_LINE_ARGUMENTS_EXCLUDING_PROGRAM_NAME
IF LENGTH(args) = 0 THEN PRINT_ERROR "Usage: stack load | stack list [] [output-format=json]" EXIT WITH CODE 2 END IF
cmd := LOWERCASE(args[0])
IF cmd = "load" THEN IF LENGTH(args) <> 2 THEN PRINT_ERROR "Usage: stack load " EXIT WITH CODE 2 END IF path := args[1] TRY CALL LoadFromJSON(path) CATCH error PRINT_ERROR error.message EXIT WITH CODE 2 END TRY
ELSE IF cmd = "list" THEN remaining := SLICE(args, 1, END) // tokens after "list" scope_token := NULL type_token := NULL format_token := NULL
// Separate potential flags from positional tokens
positional := []
flags := []
FOR EACH t IN remaining DO
IF t STARTS_WITH "output-format=" THEN
APPEND t TO flags
ELSE
APPEND t TO positional
END IF
END FOR
// Determine scope and type from positional
IF LENGTH(positional) = 0 THEN
PRINT_ERROR "Usage: stack list [<scope>] <type> [output-format=json]"
EXIT WITH CODE 2
ELSE IF LENGTH(positional) = 1 THEN
scope_token := "" // global by omission
type_token := positional[0]
ELSE IF LENGTH(positional) = 2 THEN
// First may be scope or empty; second is type
IF LOWERCASE(positional[0]) IN SCOPES OR positional[0] = "" THEN
scope_token := positional[0]
type_token := positional[1]
ELSE
// Treat as scope omitted; first token is type
scope_token := ""
type_token := positional[0]
END IF
ELSE
PRINT_ERROR "Too many arguments for list"
EXIT WITH CODE 2
END IF
// Parse format flag
format_token := "output-format=json" // default
IF LENGTH(flags) > 0 THEN
// Use the first encountered flag
format_token := flags[0]
END IF
TRY
CALL ListResourcesFor(type_token, scope_token, format_token)
CATCH error
PRINT_ERROR error.message
EXIT WITH CODE 2
END TRY
ELSE PRINT_ERROR "Unknown command" EXIT WITH CODE 2 END IF
END MAIN PROGRAM
END PSEUDOCODE